slitheen-proxy.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 src 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. #ifdef DEBUG
  111. fprintf(stderr, "injected the following packet:\n");
  112. for(int i=0; i< header->len; i++){
  113. fprintf(stderr, "%02x ", packet[i]);
  114. }
  115. fprintf(stderr, "\n");
  116. #endif
  117. if((info->tcp_hdr != NULL) && (info->ip_hdr != NULL)){
  118. 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));
  119. fprintf(stdout,"ID number: %u\n", htonl(info->ip_hdr->id));
  120. fprintf(stdout,"Sequence number: %u\n", htonl(info->tcp_hdr->sequence_num));
  121. fprintf(stdout,"Acknowledgement number: %u\n", htonl(info->tcp_hdr->ack_num));
  122. fflush(stdout);
  123. }
  124. free(info);//Note: don't free this while a thread is using it
  125. free(tmp_packet);
  126. }
  127. /* This function receives a full ip packet and then:
  128. * 1) identifies the flow
  129. * 2) adds the packet to the flow's data chain
  130. * 3) updates the flow's state
  131. */
  132. void process_packet(struct packet_info *info){
  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);
  136. }
  137. /* Now if flow is in table, update state */
  138. flow *observed;
  139. if((observed = check_flow(info)) != NULL){
  140. if(observed->application){
  141. replace_packet(observed, info);
  142. } else {
  143. /* Pass data to packet chain */
  144. add_packet(observed, info);
  145. }
  146. /* Update TCP state */
  147. if(info->tcp_hdr->flags & (FIN | RST) ){
  148. /* Remove flow from table, connection ended */
  149. remove_flow(observed);
  150. }
  151. }
  152. }
  153. /** This function extracts the ip, tcp, and tls record headers
  154. * from a received packet (if they exist), and put them in
  155. * a packet_info struct
  156. *
  157. */
  158. void extract_packet_headers(uint8_t *packet, struct packet_info *info){
  159. /* First fill in IP header */
  160. uint8_t *p = packet;
  161. p += ETHER_HEADER_LEN; //skip ethernet header
  162. info->ip_hdr = (struct ip_header*) p;
  163. info->size_ip_hdr = IP_HEADER_LEN(info->ip_hdr);
  164. /* Verify this is an IP packet */
  165. if( (info->ip_hdr->versionihl >>4) != 4){
  166. info->ip_hdr = NULL;
  167. info->size_ip_hdr = 0;
  168. info->tcp_hdr = NULL;
  169. info->size_tcp_hdr = 0;
  170. info->record_hdr = NULL;
  171. return;
  172. }
  173. /* If this is a TCP segment, fill in TCP header */
  174. if (info->ip_hdr->proto == IPPROTO_TCP){
  175. p += info->size_ip_hdr; //skip IP header
  176. info->tcp_hdr = (struct tcp_header*) p;
  177. info->size_tcp_hdr = TCP_HEADER_LEN(info->tcp_hdr);
  178. p += info->size_tcp_hdr;
  179. } else {
  180. info->tcp_hdr = NULL;
  181. info->size_tcp_hdr = 0;
  182. info->record_hdr = NULL;
  183. return;
  184. }
  185. /* If the application data contains a TLS record, fill in hdr */
  186. info->app_data_len = htons(info->ip_hdr->len) - (info->size_ip_hdr + info->size_tcp_hdr);
  187. if(info->app_data_len > 0){
  188. info->app_data = p;
  189. info->record_hdr = (struct tls_header*) p;
  190. //check to see if this is a valid record
  191. if((info->record_hdr->type < 0x14) || (info->record_hdr->type > 0x18)){
  192. info->record_hdr = NULL;
  193. }
  194. } else {
  195. info->record_hdr = NULL;
  196. info->app_data = NULL;
  197. }
  198. return;
  199. }
  200. struct packet_info *copy_packet_info(struct packet_info *src_info){
  201. struct packet_info *dst_info = calloc(1, sizeof(struct packet_info));
  202. dst_info->ip_hdr = src_info->ip_hdr;
  203. dst_info->tcp_hdr = src_info->tcp_hdr;
  204. dst_info->size_tcp_hdr = src_info->size_tcp_hdr;
  205. dst_info->size_ip_hdr = src_info->size_ip_hdr;
  206. dst_info->app_data = src_info->app_data;
  207. dst_info->app_data_len = src_info->app_data_len;
  208. return dst_info;
  209. }