slitheen-proxy.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. pthread_exit(NULL);
  45. return(0);
  46. }
  47. void *sniff_packets(void *args){
  48. pcap_t *rd_handle;
  49. pcap_t *wr_handle;
  50. char rd_errbuf[BUFSIZ];
  51. char wr_errbuf[BUFSIZ];
  52. struct bpf_program fp;
  53. bpf_u_int32 mask;
  54. bpf_u_int32 net;
  55. char *readdev, *writedev, *filter;
  56. struct sniff_args *arg_st = (struct sniff_args *) args;
  57. readdev = arg_st->readdev;
  58. writedev = arg_st->writedev;
  59. filter = arg_st->filter;
  60. if (pcap_lookupnet(readdev, &net, &mask, rd_errbuf) == -1){
  61. fprintf(stderr, "Can't get netmask for device %s\n", readdev);
  62. exit(2);
  63. }
  64. rd_handle = pcap_open_live(readdev, BUFSIZ, 1, 0, rd_errbuf);
  65. if (rd_handle == NULL){
  66. fprintf(stderr, "Couldn't open device %s: %s\n", readdev, rd_errbuf);
  67. }
  68. if(pcap_datalink(rd_handle) != DLT_EN10MB) {
  69. fprintf(stderr, "Device %s does not provide Ethernet headers - not supported\n", readdev);
  70. exit(2);
  71. }
  72. if(pcap_compile(rd_handle, &fp, filter, 0 , net) == -1){
  73. fprintf(stderr, "Couldn't parse filter %s: %s\n", filter, pcap_geterr(rd_handle));
  74. exit(2);
  75. }
  76. if (pcap_setfilter(rd_handle, &fp) == -1) {
  77. fprintf(stderr, "Couldn't install filter %s: %s\n", filter, pcap_geterr(rd_handle));
  78. exit(2);
  79. }
  80. wr_handle = pcap_open_live(writedev, BUFSIZ, 1, 0, wr_errbuf);
  81. if (wr_handle == NULL){
  82. fprintf(stderr, "Couldn't open device %s: %s\n", writedev, wr_errbuf);
  83. }
  84. /*callback function*/
  85. pcap_loop(rd_handle, -1, got_packet, (unsigned char *) wr_handle);
  86. /*Sniff a packet*/
  87. pcap_close(rd_handle);
  88. return NULL;
  89. }
  90. void got_packet(uint8_t *args, const struct pcap_pkthdr *header, const uint8_t *packet){
  91. pcap_t *handle = (pcap_t *) args;
  92. struct packet_info *info = calloc(1, sizeof(struct packet_info));
  93. uint8_t *tmp_packet = calloc(1, header->len);
  94. memcpy(tmp_packet, packet, header->len);
  95. extract_packet_headers(tmp_packet, info);
  96. // Check to make sure it is a TCP packet
  97. if((info->ip_hdr == NULL) || (info->tcp_hdr == NULL))
  98. goto end;
  99. process_packet(info);
  100. end:
  101. if((pcap_inject(handle, tmp_packet, header->len)) < 0 ){
  102. fprintf(stderr, "Error: %s\n", pcap_geterr(handle));
  103. }
  104. free(info);//Note: don't free this while a thread is using it
  105. #ifdef DEBUG
  106. fprintf(stderr, "injected the following packet:\n");
  107. for(int i=0; i< header->len; i++){
  108. fprintf(stderr, "%02x ", packet[i]);
  109. }
  110. fprintf(stderr, "\n");
  111. #endif
  112. free(tmp_packet);
  113. }
  114. /* This function receives a full ip packet and then:
  115. * 1) identifies the flow
  116. * 2) adds the packet to the flow's data chain
  117. * 3) updates the flow's state
  118. */
  119. void process_packet(struct packet_info *info){
  120. flow newFlow;
  121. newFlow.src_ip = info->ip_hdr->src;
  122. newFlow.dst_ip = info->ip_hdr->dst;
  123. newFlow.src_port = info->tcp_hdr->src_port;
  124. newFlow.dst_port = info->tcp_hdr->dst_port;
  125. newFlow.seq_num = info->tcp_hdr->sequence_num;
  126. /* Checks to see if this is a possibly tagged hello msg */
  127. if ((info->record_hdr != NULL) && (info->record_hdr->type == HS)){ /* This is a TLS handshake */
  128. check_handshake(info, newFlow);
  129. }
  130. /* Now if flow is in table, update state */
  131. flow *observed;
  132. if((observed = check_flow(newFlow)) != NULL){
  133. if(observed->application){
  134. replace_packet(observed, info);
  135. } else {
  136. /* Pass data to packet chain */
  137. add_packet(observed, info);
  138. /* Update flow state */
  139. if(observed->packet_chain != NULL){
  140. update_flow(observed);
  141. }
  142. }
  143. /* Update TCP state */
  144. if(info->tcp_hdr->flags & (FIN | RST) ){
  145. /* Remove flow from table, connection ended */
  146. remove_flow(observed);
  147. }
  148. }
  149. }
  150. /** This function extracts the ip, tcp, and tls record headers
  151. * from a received packet (if they exist), and put them in
  152. * a packet_info struct
  153. *
  154. */
  155. void extract_packet_headers(uint8_t *packet, struct packet_info *info){
  156. /* First fill in IP header */
  157. uint8_t *p = packet;
  158. p += ETHER_HEADER_LEN; //skip ethernet header
  159. info->ip_hdr = (struct ip_header*) p;
  160. info->size_ip_hdr = IP_HEADER_LEN(info->ip_hdr);
  161. /* Verify this is an IP packet */
  162. if( (info->ip_hdr->versionihl >>4) != 4){
  163. info->ip_hdr = NULL;
  164. info->size_ip_hdr = 0;
  165. info->tcp_hdr = NULL;
  166. info->size_tcp_hdr = 0;
  167. info->record_hdr = NULL;
  168. return;
  169. }
  170. /* If this is a TCP segment, fill in TCP header */
  171. if (info->ip_hdr->proto == IPPROTO_TCP){
  172. p += info->size_ip_hdr; //skip IP header
  173. info->tcp_hdr = (struct tcp_header*) p;
  174. info->size_tcp_hdr = TCP_HEADER_LEN(info->tcp_hdr);
  175. p += info->size_tcp_hdr;
  176. } else {
  177. info->tcp_hdr = NULL;
  178. info->size_tcp_hdr = 0;
  179. info->record_hdr = NULL;
  180. return;
  181. }
  182. /* If the application data contains a TLS record, fill in hdr */
  183. info->app_data_len = htons(info->ip_hdr->len) - (info->size_ip_hdr + info->size_tcp_hdr);
  184. if(info->app_data_len > 0){
  185. info->app_data = p;
  186. info->record_hdr = (struct tls_header*) p;
  187. //check to see if this is a valid record
  188. if((info->record_hdr->type < 0x14) || (info->record_hdr->type > 0x18)){
  189. info->record_hdr = NULL;
  190. }
  191. } else {
  192. info->record_hdr = NULL;
  193. info->app_data = NULL;
  194. }
  195. return;
  196. }