slitheen-proxy.c 6.1 KB

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