slitheen-proxy.c 7.2 KB

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