slitheen-proxy.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /* slitheen-proxy.c by Cecylia Bocovich */
  2. #include <pcap.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6. #include <string.h>
  7. #include <pthread.h>
  8. #include <openssl/ssl.h>
  9. #include "util.h"
  10. #include "flow.h"
  11. #include "slitheen.h"
  12. #include "relay.h"
  13. #include "crypto.h"
  14. #include "cryptothread.h"
  15. void usage(void){
  16. printf("Usage: slitheen-proxy [internal network interface] [NAT interface]\n");
  17. }
  18. int main(int argc, char *argv[]){
  19. pthread_t t1, t2;
  20. char *filter1 = ecalloc(1, 33);
  21. char *filter2 = ecalloc(1, 33);
  22. char *dev1 = NULL; /* Device that leads to the internal network */
  23. char *dev2 = NULL; /* Device that leads out to the world */
  24. struct sniff_args outbound;
  25. struct sniff_args inbound;
  26. if (argc != 3) {
  27. usage();
  28. return(2);
  29. }
  30. dev1 = argv[1];
  31. dev2 = argv[2];
  32. snprintf(filter1, 33, "ether src host %s", macaddr1);
  33. snprintf(filter2, 33, "ether src host %s", macaddr2);
  34. if(init_tables()){
  35. exit(1);
  36. }
  37. if(init_session_cache()){
  38. exit(1);
  39. }
  40. init_crypto_locks();
  41. /* Create threads */
  42. outbound.readdev = dev1;
  43. outbound.writedev = dev2;
  44. outbound.filter = filter1;
  45. inbound.readdev = dev2;
  46. inbound.writedev = dev1;
  47. inbound.filter = filter2;
  48. pthread_create(&t1, NULL, sniff_packets, (void *) &outbound);
  49. pthread_create(&t2, NULL, sniff_packets, (void *) &inbound);
  50. pthread_join(t1, NULL);
  51. pthread_join(t2, NULL);
  52. pthread_exit(NULL);
  53. free(filter1);
  54. free(filter2);
  55. crypto_locks_cleanup();
  56. return(0);
  57. }
  58. void *sniff_packets(void *args){
  59. pcap_t *rd_handle;
  60. pcap_t *wr_handle;
  61. char rd_errbuf[BUFSIZ];
  62. char wr_errbuf[BUFSIZ];
  63. struct bpf_program fp;
  64. bpf_u_int32 mask;
  65. bpf_u_int32 net;
  66. char *readdev, *writedev, *filter;
  67. struct sniff_args *arg_st = (struct sniff_args *) args;
  68. readdev = arg_st->readdev;
  69. writedev = arg_st->writedev;
  70. filter = arg_st->filter;
  71. if (pcap_lookupnet(readdev, &net, &mask, rd_errbuf) == -1){
  72. fprintf(stderr, "Can't get netmask for device %s\n", readdev);
  73. exit(2);
  74. }
  75. rd_handle = pcap_open_live(readdev, BUFSIZ, 1, 0, rd_errbuf);
  76. if (rd_handle == NULL){
  77. fprintf(stderr, "Couldn't open device %s: %s\n", readdev, rd_errbuf);
  78. }
  79. if(pcap_datalink(rd_handle) != DLT_EN10MB) {
  80. fprintf(stderr, "Device %s does not provide Ethernet headers - not supported\n", readdev);
  81. exit(2);
  82. }
  83. if(pcap_compile(rd_handle, &fp, filter, 0 , net) == -1){
  84. fprintf(stderr, "Couldn't parse filter %s: %s\n", filter, pcap_geterr(rd_handle));
  85. exit(2);
  86. }
  87. if (pcap_setfilter(rd_handle, &fp) == -1) {
  88. fprintf(stderr, "Couldn't install filter %s: %s\n", filter, pcap_geterr(rd_handle));
  89. exit(2);
  90. }
  91. wr_handle = pcap_open_live(writedev, BUFSIZ, 1, 0, wr_errbuf);
  92. if (wr_handle == NULL){
  93. fprintf(stderr, "Couldn't open device %s: %s\n", writedev, wr_errbuf);
  94. }
  95. /*callback function*/
  96. pcap_loop(rd_handle, -1, got_packet, (unsigned char *) wr_handle);
  97. /*Sniff a packet*/
  98. pcap_close(rd_handle);
  99. return NULL;
  100. }
  101. /**
  102. * Runs when pcap_loop receives a packet from the specified interface
  103. * If the received packet is a tcp packet, processes it and then writes it back out
  104. * to the interface
  105. *
  106. */
  107. void got_packet(uint8_t *args, const struct pcap_pkthdr *header, const uint8_t *packet){
  108. pcap_t *handle = (pcap_t *) args;
  109. struct packet_info *info = emalloc(sizeof(struct packet_info));
  110. uint8_t *tmp_packet = emalloc(header->len);
  111. //printf("Allocated %d bytes to %p\n", header->len, tmp_packet);
  112. memcpy(tmp_packet, packet, header->len);
  113. extract_packet_headers(tmp_packet, info);
  114. // Check to make sure it is a TCP packet
  115. if((info->ip_hdr == NULL) || (info->tcp_hdr == NULL))
  116. goto end;
  117. process_packet(info);
  118. end:
  119. if((pcap_inject(handle, tmp_packet, header->len)) < 0 ){
  120. fprintf(stderr, "Error: %s\n", pcap_geterr(handle));
  121. }
  122. #ifdef DEBUG
  123. fprintf(stderr, "injected the following packet:\n");
  124. for(int i=0; i< header->len; i++){
  125. fprintf(stderr, "%02x ", packet[i]);
  126. }
  127. fprintf(stderr, "\n");
  128. if((info->tcp_hdr != NULL) && (info->ip_hdr != NULL)){
  129. fprintf(stdout,"Injected packet: %x:%d > %x:%d\n", info->ip_hdr->src.s_addr, ntohs(info->tcp_hdr->src_port), info->ip_hdr->dst.s_addr, ntohs(info->tcp_hdr->dst_port));
  130. fprintf(stdout,"ID number: %u\n", htonl(info->ip_hdr->id));
  131. fprintf(stdout,"Sequence number: %u\n", htonl(info->tcp_hdr->sequence_num));
  132. fprintf(stdout,"Acknowledgement number: %u\n", htonl(info->tcp_hdr->ack_num));
  133. fflush(stdout);
  134. }
  135. #endif
  136. free(info);//Note: don't free this while a thread is using it
  137. free(tmp_packet);
  138. }
  139. /* This function receives a full ip packet and then:
  140. * 1) identifies the flow
  141. * 2) adds the packet to the flow's data chain
  142. * 3) updates the flow's state
  143. */
  144. void process_packet(struct packet_info *info){
  145. /* Checks to see if this is a possibly tagged hello msg */
  146. if ((info->record_hdr != NULL) && (info->record_hdr->type == HS)){ /* This is a TLS handshake */
  147. check_handshake(info);
  148. }
  149. /* Now if flow is in table, update state */
  150. flow *observed;
  151. if((observed = check_flow(info)) != NULL){
  152. if(observed->application){
  153. replace_packet(observed, info);
  154. } else {
  155. /* Pass data to packet chain */
  156. add_packet(observed, info);
  157. }
  158. /* Update TCP state */
  159. if(info->tcp_hdr->flags & (FIN | RST) ){
  160. /* Remove flow from table, connection ended */
  161. remove_flow(observed);
  162. }
  163. }
  164. }
  165. /** This function extracts the ip, tcp, and tls record headers
  166. * from a received packet (if they exist), and put them in
  167. * a packet_info struct
  168. *
  169. */
  170. void extract_packet_headers(uint8_t *packet, struct packet_info *info){
  171. /* First fill in IP header */
  172. uint8_t *p = packet;
  173. p += ETHER_HEADER_LEN; //skip ethernet header
  174. info->ip_hdr = (struct ip_header*) p;
  175. info->size_ip_hdr = IP_HEADER_LEN(info->ip_hdr);
  176. /* Verify this is an IP packet */
  177. if( (info->ip_hdr->versionihl >>4) != 4){
  178. info->ip_hdr = NULL;
  179. info->size_ip_hdr = 0;
  180. info->tcp_hdr = NULL;
  181. info->size_tcp_hdr = 0;
  182. info->record_hdr = NULL;
  183. return;
  184. }
  185. /* If this is a TCP segment, fill in TCP header */
  186. if (info->ip_hdr->proto == IPPROTO_TCP){
  187. p += info->size_ip_hdr; //skip IP header
  188. info->tcp_hdr = (struct tcp_header*) p;
  189. info->size_tcp_hdr = TCP_HEADER_LEN(info->tcp_hdr);
  190. p += info->size_tcp_hdr;
  191. } else {
  192. info->tcp_hdr = NULL;
  193. info->size_tcp_hdr = 0;
  194. info->record_hdr = NULL;
  195. return;
  196. }
  197. /* If the application data contains a TLS record, fill in hdr */
  198. info->app_data_len = htons(info->ip_hdr->len) - (info->size_ip_hdr + info->size_tcp_hdr);
  199. if(info->app_data_len > 0){
  200. info->app_data = p;
  201. info->record_hdr = (struct tls_header*) p;
  202. //check to see if this is a valid record
  203. if((info->record_hdr->type < 0x14) || (info->record_hdr->type > 0x18)){
  204. info->record_hdr = NULL;
  205. }
  206. } else {
  207. info->record_hdr = NULL;
  208. info->app_data = NULL;
  209. }
  210. return;
  211. }
  212. /** Copies a packet_info structure and returns a pointer to the duplicate.
  213. */
  214. struct packet_info *copy_packet_info(struct packet_info *src_info){
  215. struct packet_info *dst_info = emalloc(sizeof(struct packet_info));
  216. dst_info->ip_hdr = src_info->ip_hdr;
  217. dst_info->tcp_hdr = src_info->tcp_hdr;
  218. dst_info->size_tcp_hdr = src_info->size_tcp_hdr;
  219. dst_info->size_ip_hdr = src_info->size_ip_hdr;
  220. dst_info->app_data = src_info->app_data;
  221. dst_info->app_data_len = src_info->app_data_len;
  222. return dst_info;
  223. }