slitheen-proxy.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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 "rserv.h"
  9. #include "flow.h"
  10. #include "slitheen.h"
  11. #include "util.h"
  12. #include "relay.h"
  13. void got_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet);
  14. void check_handshake(struct packet_info *info, flow f);
  15. void *sniff_packets(void *);
  16. void process_packet(struct packet_info *info);
  17. void extract_packet_headers(uint8_t *packet, struct packet_info *info);
  18. /** Checks a handshake message to see if it is tagged or a
  19. * recognized flow.
  20. * Inputs: The tcp header for the message, its associated flow,
  21. * a pointer to the handshake header, and the length of
  22. * the record.
  23. */
  24. void check_handshake(struct packet_info *info, flow f){
  25. FILE *fp;
  26. int res, i, code;
  27. uint8_t *hello_rand;
  28. const struct handshake_header *handshake_hdr;
  29. byte privkey[PTWIST_BYTES];
  30. byte key[16];
  31. uint8_t *p = info->app_data + RECORD_HEADER_LEN;
  32. handshake_hdr = (struct handshake_header*) p;
  33. code = handshake_hdr->type;
  34. if (code == 0x01){
  35. p += CLIENT_HELLO_HEADER_LEN;
  36. //now pointing to hello random :D
  37. hello_rand = p;
  38. p += 4; //skipping time bytes
  39. /* Load the private key */
  40. fp = fopen("privkey", "rb");
  41. if (fp == NULL) {
  42. perror("fopen");
  43. exit(1);
  44. }
  45. res = fread(privkey, PTWIST_BYTES, 1, fp);
  46. if (res < 1) {
  47. perror("fread");
  48. exit(1);
  49. }
  50. fclose(fp);
  51. /* check tag*/
  52. res = check_tag(key, privkey, p, (const byte *)"context", 7);
  53. if (res) {
  54. printf("Untagged\n");
  55. } else {
  56. fp = fopen("tags", "wb");
  57. if (fp == NULL) {
  58. perror("fopen");
  59. exit(1);
  60. }
  61. //Write tag to file
  62. for(i=0; i< 28; i++){
  63. fprintf(fp, "%02x ", p[i]);
  64. }
  65. fclose(fp);
  66. //Write key to file
  67. fp = fopen("sharedkey", "wb");
  68. if (fp == NULL) {
  69. perror("fopen");
  70. exit(1);
  71. }
  72. for(i=0; i<16;i++){
  73. fprintf(fp, "%02x", key[i]);
  74. }
  75. fclose(fp);
  76. /* Save flow in table */
  77. flow *flow_ptr = add_flow(f);
  78. for(int i=0; i<16; i++){
  79. flow_ptr->key[i] = key[i];
  80. }
  81. memcpy(flow_ptr->client_random, hello_rand, SSL3_RANDOM_SIZE);
  82. printf("Saved new flow\n");
  83. }
  84. }
  85. }
  86. void got_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet){
  87. pcap_t *handle = (pcap_t *) args;
  88. //might modify packet, copy to new pointer
  89. //uint8_t *modified_packet = calloc(1, header->len);
  90. //memcpy(modified_packet, packet, header->len);
  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 an IP packet
  96. if(info->ip_hdr == NULL)
  97. goto end;
  98. /* Packet may be split over multiple frames. In this case,
  99. // reconstruct the packet before trying to read TLS records
  100. if((htons(info->ip_hdr->flagsoff)&MF) || (htons(info->ip_hdr->flagsoff) &0x1fff) ){ // this is a fragment
  101. printf("MF: %d, OFF: %d.\n", htons(info->ip_hdr->flagsoff)&MF, htons(info->ip_hdr->flagsoff)&0x1fff);
  102. printf("Received packet fragment.\n");
  103. u_char *complete_packet;
  104. //save packet fragment
  105. complete_packet = malloc(65535);
  106. int success = report_fragment(p, complete_packet, header->len);
  107. if(success){
  108. process_packet(complete_packet, info);
  109. }
  110. // TODO: handle errors
  111. } else { //not a fragment, add to packet chain */
  112. process_packet(info);
  113. end:
  114. if((pcap_inject(handle, tmp_packet, header->len)) < 0 ){
  115. fprintf(stderr, "Error: %s\n", pcap_geterr(handle));
  116. }
  117. free(info);//Note: don't free this while a thread is using it
  118. #ifdef DEBUG
  119. fprintf(stderr, "injected the following packet:\n");
  120. for(int i=0; i< header->len; i++){
  121. fprintf(stderr, "%02x ", packet[i]);
  122. }
  123. fprintf(stderr, "\n");
  124. #endif
  125. //free(modified_packet);
  126. }
  127. void usage(void){
  128. printf("Usage: slitheen-proxy [internal network interface] [NAT interface]\n");
  129. }
  130. int main(int argc, char *argv[]){
  131. pthread_t t1, t2;
  132. char filter1[33] = "ether src host 08:00:27:e8:9d:d4";
  133. char filter2[33] = "ether src host 08:00:27:e8:9d:d4";
  134. char *dev1 = NULL; /* Device that leads to the internal network */
  135. char *dev2 = NULL; /* Device that leads out to the world */
  136. struct sniff_args outbound;
  137. struct sniff_args inbound;
  138. if (argc != 3) {
  139. usage();
  140. return(2);
  141. }
  142. dev1 = argv[1];
  143. dev2 = argv[2];
  144. snprintf(filter1, 33, "ether src host %s", macaddr);
  145. snprintf(filter2, 33, "ether dst host %s", macaddr);
  146. init_flow_table();
  147. init_fragment_table();
  148. /* Create threads */
  149. outbound.readdev = dev1;
  150. outbound.writedev = dev2;
  151. outbound.filter = filter1;
  152. inbound.readdev = dev2;
  153. inbound.writedev = dev1;
  154. inbound.filter = filter2;
  155. pthread_create(&t1, NULL, sniff_packets, (void *) &outbound);
  156. pthread_create(&t2, NULL, sniff_packets, (void *) &inbound);
  157. pthread_join(t1, NULL);
  158. pthread_join(t2, NULL);
  159. return(0);
  160. }
  161. void *sniff_packets(void *args){
  162. pcap_t *rd_handle;
  163. pcap_t *wr_handle;
  164. char rd_errbuf[BUFSIZ];
  165. char wr_errbuf[BUFSIZ];
  166. struct bpf_program fp;
  167. bpf_u_int32 mask;
  168. bpf_u_int32 net;
  169. char *readdev, *writedev, *filter;
  170. struct sniff_args *arg_st = (struct sniff_args *) args;
  171. readdev = arg_st->readdev;
  172. writedev = arg_st->writedev;
  173. filter = arg_st->filter;
  174. if (pcap_lookupnet(readdev, &net, &mask, rd_errbuf) == -1){
  175. fprintf(stderr, "Can't get netmask for device %s\n", readdev);
  176. exit(2);
  177. }
  178. rd_handle = pcap_open_live(readdev, BUFSIZ, 1, 0, rd_errbuf);
  179. if (rd_handle == NULL){
  180. fprintf(stderr, "Couldn't open device %s: %s\n", readdev, rd_errbuf);
  181. }
  182. if(pcap_datalink(rd_handle) != DLT_EN10MB) {
  183. fprintf(stderr, "Device %s does not provide Ethernet headers - not supported\n", readdev);
  184. exit(2);
  185. }
  186. if(pcap_compile(rd_handle, &fp, filter, 0 , net) == -1){
  187. fprintf(stderr, "Couldn't parse filter %s: %s\n", filter, pcap_geterr(rd_handle));
  188. exit(2);
  189. }
  190. if (pcap_setfilter(rd_handle, &fp) == -1) {
  191. fprintf(stderr, "Couldn't install filter %s: %s\n", filter, pcap_geterr(rd_handle));
  192. exit(2);
  193. }
  194. wr_handle = pcap_open_live(writedev, BUFSIZ, 1, 0, wr_errbuf);
  195. if (wr_handle == NULL){
  196. fprintf(stderr, "Couldn't open device %s: %s\n", writedev, wr_errbuf);
  197. }
  198. /*callback function*/
  199. pcap_loop(rd_handle, -1, got_packet, (unsigned char *) wr_handle);
  200. /*Sniff a packet*/
  201. pcap_close(rd_handle);
  202. return NULL;
  203. }
  204. /* This function receives a full ip packet and then:
  205. * 1) identifies the flow
  206. * 2) adds the packet to the flow's data chain
  207. * 3) updates the flow's state
  208. */
  209. void process_packet(struct packet_info *info){
  210. int index;
  211. flow newFlow;
  212. if (info->tcp_hdr != NULL){
  213. newFlow.src_ip = info->ip_hdr->src;
  214. newFlow.dst_ip = info->ip_hdr->dst;
  215. newFlow.src_port = info->tcp_hdr->src_port;
  216. newFlow.dst_port = info->tcp_hdr->dst_port;
  217. newFlow.seq_num = info->tcp_hdr->sequence_num;
  218. /* Checks to see if this is a possibly tagged hello msg */
  219. if ((info->record_hdr != NULL) && (info->record_hdr->type == HS)){ /* This is a TLS handshake */
  220. check_handshake(info, newFlow);
  221. }
  222. }
  223. /* Now if flow is in table, update state */
  224. if((index = check_flow(newFlow))){
  225. flow *observed = get_flow(index-1);
  226. if(observed->application){
  227. replace_packet(observed, info);
  228. } else {
  229. /* Pass data to packet chain */
  230. add_packet(observed, info);
  231. /* Update flow state */
  232. if(observed->packet_chain != NULL){
  233. update_flow(observed);
  234. }
  235. }
  236. /* Update TCP state */
  237. if(info->tcp_hdr->flags & (FIN | RST) ){
  238. /* Remove flow from table, connection ended */
  239. remove_flow(index);
  240. }
  241. }
  242. }
  243. /** This function extracts the ip, tcp, and tls record headers
  244. * from a received packet (if they exist), and put them in
  245. * a packet_info struct
  246. *
  247. */
  248. void extract_packet_headers(uint8_t *packet, struct packet_info *info){
  249. /* First fill in IP header */
  250. uint8_t *p = packet;
  251. p += ETHER_HEADER_LEN; //skip ethernet header
  252. info->ip_hdr = (struct ip_header*) p;
  253. info->size_ip_hdr = IP_HEADER_LEN(info->ip_hdr);
  254. /* Verify this is an IP packet */
  255. if( (info->ip_hdr->versionihl >>4) != 4){
  256. info->ip_hdr = NULL;
  257. info->size_ip_hdr = 0;
  258. info->tcp_hdr = NULL;
  259. info->size_tcp_hdr = 0;
  260. info->record_hdr = NULL;
  261. return;
  262. }
  263. /* If this is a TCP segment, fill in TCP header */
  264. if (info->ip_hdr->proto == IPPROTO_TCP){
  265. p += info->size_ip_hdr; //skip IP header
  266. info->tcp_hdr = (struct tcp_header*) p;
  267. info->size_tcp_hdr = TCP_HEADER_LEN(info->tcp_hdr);
  268. p += info->size_tcp_hdr;
  269. } else {
  270. info->tcp_hdr = NULL;
  271. info->size_tcp_hdr = 0;
  272. info->record_hdr = NULL;
  273. return;
  274. }
  275. /* If the application data contains a TLS record, fill in hdr */
  276. info->app_data_len = htons(info->ip_hdr->len) - (info->size_ip_hdr + info->size_tcp_hdr);
  277. if(info->app_data_len > 0){
  278. info->app_data = p;
  279. info->record_hdr = (struct tls_header*) p;
  280. //check to see if this is a valid record
  281. if((info->record_hdr->type < 0x14) || (info->record_hdr->type > 0x18)){
  282. info->record_hdr = NULL;
  283. }
  284. } else {
  285. info->record_hdr = NULL;
  286. info->app_data = NULL;
  287. }
  288. return;
  289. }