slitheen-proxy.c 6.6 KB

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