relay.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. #include <regex.h>
  5. #include <sys/socket.h>
  6. #include <sys/types.h>
  7. #include <netinet/in.h>
  8. #include <netdb.h>
  9. #include <unistd.h>
  10. #include <pthread.h>
  11. #include "relay.h"
  12. #include "slitheen.h"
  13. #include "flow.h"
  14. #include "crypto.h"
  15. /** Called when a TLS application record is received for a
  16. * tagged flow. Upstream packets will be checked for covert
  17. * requests to censored sites, downstream packets will be
  18. * replaced with data from the censored queue or with garbage
  19. *
  20. * Inputs:
  21. * f: the tagged flow
  22. * info: the processed received application packet
  23. *
  24. * Output:
  25. * 0 on success, 1 on failure
  26. */
  27. int replace_packet(flow *f, struct packet_info *info){
  28. if (info->tcp_hdr == NULL){
  29. return 0;
  30. }
  31. #ifdef DEBUG
  32. fprintf(stderr,"Flow: %d > %d (%s)\n", info->ip_hdr->src.s_addr, info->ip_hdr->dst.s_addr, (info->ip_hdr->src.s_addr != f->src_ip.s_addr)? "incoming":"outgoing");
  33. fprintf(stderr,"ID number: %u\n", htonl(info->ip_hdr->id));
  34. fprintf(stderr,"Sequence number: %u\n", htonl(info->tcp_hdr->sequence_num));
  35. fprintf(stderr,"Acknowledgement number: %u\n", htonl(info->tcp_hdr->ack_num));
  36. #endif
  37. if(info->app_data_len <= 0){
  38. return 0;
  39. }
  40. /* if outgoing, decrypt and look at header */
  41. if(info->ip_hdr->src.s_addr == f->src_ip.s_addr){
  42. read_header(f, info);
  43. return 0;
  44. } else {
  45. #ifdef DEBUG
  46. printf("Current sequence number: %d\n", f->seq_num);
  47. printf("Received sequence number: %d\n", htonl(tcp_hdr->sequence_num));
  48. #endif
  49. uint32_t offset = htonl(info->tcp_hdr->sequence_num) - f->seq_num;
  50. if(offset == 0)
  51. f->seq_num += info->app_data_len;
  52. /* if incoming, replace with data from queue */
  53. //if(htonl(tcp_hdr->sequence_num) >= f->seq_num){
  54. replace_contents(f, offset, info);
  55. //}//TODO: need to do something about replaying packets (maybe store previously sent data??
  56. #ifdef DEBUG //TODO: fix
  57. uint8_t *p = (uint8_t *) info->tcp_hdr;
  58. fprintf(stdout, "ip hdr length: %d\n", htons(info->ip_hdr->len));
  59. fprintf(stdout, "Injecting the following packet:\n");
  60. for(int i=0; i< htons(info->ip_hdr->len); i++){
  61. fprintf(stdout, "%02x ", p[i]);
  62. }
  63. fprintf(stdout, "\n");
  64. fflush(stdout);
  65. #endif
  66. }
  67. return 0;
  68. }
  69. /** Reads the HTTP header of upstream data and searches for
  70. * a covert request in the x-ignore header. Sends this
  71. * request to the indicated site and saves the response to
  72. * the censored queue
  73. *
  74. * Inputs:
  75. * f: the tagged flow
  76. * info: the processed received packet
  77. *
  78. * Ouput:
  79. * 0 on success, 1 on failure
  80. */
  81. int read_header(flow *f, struct packet_info *info){
  82. uint8_t *p = info->app_data;
  83. if (info->tcp_hdr == NULL){
  84. return 0;
  85. }
  86. struct record_header *record_hdr = (struct record_header*) p;
  87. uint32_t record_length = RECORD_LEN(record_hdr);
  88. uint8_t *decrypted_data = calloc(1, info->app_data_len);
  89. p+= RECORD_HEADER_LEN;
  90. memcpy(decrypted_data, p, record_length);
  91. if(!encrypt(f, decrypted_data, decrypted_data, record_length, 0, record_hdr->type, 0)){
  92. fprintf(stdout,"decryption failed\n");
  93. return 0;
  94. }
  95. if(record_hdr->type == 0x15){
  96. printf("received alert\n");
  97. for(int i=0; i<record_length; i++){
  98. printf("%02x ", decrypted_data[i]);
  99. }
  100. fflush(stdout);
  101. }
  102. //TODO: re-write this to take a SOCKS connection request
  103. /* search through decrypted data for x-ignore */
  104. regex_t r;
  105. const char *regex_text = "x-ignore";
  106. const char *match = (char *) decrypted_data;
  107. if(regcomp(&r, regex_text, REG_EXTENDED|REG_NEWLINE)){
  108. printf("could not compile regex\n");
  109. return 0;
  110. }
  111. regmatch_t m;
  112. if(regexec(&r, match, 1, &m, 0)){
  113. printf("Upstream data: no x-ignore header\n");
  114. return 0;
  115. }
  116. uint8_t *message = decrypted_data;
  117. //remove escape characters
  118. for(int i=m.rm_eo+2; i< strlen(match); i++){
  119. if(match[i] != '\\'){
  120. *(message++) = match[i];
  121. } else if (match[i+1] == 'r') {
  122. *(message++) = '\r';
  123. i++;
  124. } else if (match[i+1] == 'n') {
  125. *(message++) = '\n';
  126. i++;
  127. }
  128. }
  129. *message = '\0';
  130. message = decrypted_data;
  131. regex_t r2;
  132. const char *regex_text2 = "host";
  133. const char *match2 = (char *) decrypted_data;
  134. regmatch_t m2;
  135. if(regcomp(&r2, regex_text2, REG_EXTENDED|REG_NEWLINE)){
  136. printf("could not compile regex\n");
  137. return 0;
  138. }
  139. if(regexec(&r2, match2, 1, &m2, 0)){
  140. printf("no host found\n");
  141. return 0;
  142. }
  143. uint8_t server[50];
  144. for(int i=m2.rm_eo+2; i< strlen(match2); i++){
  145. if(match2[i] != '\n'){
  146. server[i-m2.rm_eo-2] = match2[i];
  147. } else {
  148. server[i-m2.rm_eo-2] = '\0';
  149. break;
  150. }
  151. }
  152. printf("Collecting data from censored site: %s\n", server);
  153. //If a thread for this stream id exists, get the thread info
  154. //TODO: fill this in
  155. //Else, spawn a thread to handle the proxy to this site
  156. pthread_t *proxy_thread = calloc(1, sizeof(pthread_t));
  157. int32_t pipefd[2];
  158. if(pipe(pipefd) < 0){
  159. printf("Failed to create pipe for new thread\n");
  160. return 1;
  161. }
  162. struct proxy_thread_data *thread_data =
  163. calloc(1, sizeof(struct proxy_thread_data));
  164. thread_data->f = f;
  165. memcpy(thread_data->server, server, 50);
  166. printf("Collecting data from censored site: %s\n", thread_data->server);
  167. thread_data->pipefd = pipefd[0];
  168. pthread_create(proxy_thread, NULL, proxy_covert_site, (void *) thread_data);
  169. //save a reference to the proxy threads in the flow
  170. //Now pipe information to the selected or created thread
  171. //TODO: message may not be a string. fix.
  172. int32_t bytes_written = write(pipefd[1], message,
  173. strlen( (const char *) message));
  174. if(bytes_written < strlen( (const char *) message)){
  175. printf("failed to write all bytes to pipe\n");
  176. }
  177. return 0;
  178. }
  179. /** Called by spawned pthreads in read_header to send upstream
  180. * data to the censored site and receive responses. Downstream
  181. * data is stored in the flow's censored_queue. Function and
  182. * thread will terminate when the client closes the connection
  183. * to the covert destination
  184. *
  185. * Input:
  186. * A struct that contains the following information:
  187. * - the tagged flow
  188. * - the name of the server
  189. * - the read end of the pipe
  190. *
  191. */
  192. void *proxy_covert_site(void *data){
  193. struct proxy_thread_data *thread_data =
  194. (struct proxy_thread_data *) data;
  195. flow *f = thread_data->f;
  196. int32_t buffer_len = BUFSIZ;
  197. uint8_t *buffer = calloc(1, BUFSIZ);
  198. /* Send GET request to site */
  199. printf("Collecting data from censored site: %s\n", thread_data->server);
  200. struct hostent *host;
  201. host = gethostbyname((const char *) thread_data->server);
  202. if(host == NULL){
  203. printf("gethostbyname failed\n");
  204. return 0;
  205. }
  206. int32_t handle = socket(AF_INET, SOCK_STREAM, 0);
  207. if(handle < 0){
  208. printf("error: constructing socket failed\n");
  209. return 0;
  210. }
  211. struct sockaddr_in dest;
  212. dest.sin_family = AF_INET;
  213. dest.sin_port = htons(80);
  214. dest.sin_addr = *((struct in_addr *) host->h_addr);
  215. bzero (&(dest.sin_zero), 8);
  216. int32_t error = connect (handle, (struct sockaddr *) &dest, sizeof (struct sockaddr));
  217. if(error <0){
  218. printf("error connecting\n");
  219. return 0;
  220. }
  221. //now select on reading from the pipe and from the socket
  222. for(;;){
  223. fd_set readfds;
  224. fd_set writefds;
  225. int32_t nfds = (handle > thread_data->pipefd) ?
  226. handle +1 : thread_data->pipefd + 1;
  227. FD_ZERO(&readfds);
  228. FD_ZERO(&writefds);
  229. FD_SET(thread_data->pipefd, &readfds);
  230. FD_SET(handle, &readfds);
  231. FD_SET(handle, &writefds);
  232. if (select(nfds, &readfds, &writefds, NULL, NULL) < 0){
  233. printf("select error\n");
  234. }
  235. if(FD_ISSET(thread_data->pipefd, &readfds) && FD_ISSET(handle, &writefds)){
  236. //we have upstream data ready for writing
  237. printf("Passing along upstream data\n");
  238. int32_t bytes_read = read(thread_data->pipefd, buffer, buffer_len);
  239. buffer[buffer_len] = '\0';//TODO: remove w/ print
  240. printf("Read from pipe:\n %s\n", buffer);
  241. if(bytes_read > 0){
  242. int32_t bytes_sent = send(handle, buffer,
  243. bytes_read, 0);
  244. if( bytes_sent < 0){
  245. printf("error sending request\n");
  246. break;
  247. } else if (bytes_sent < bytes_read){
  248. //TODO: should update buffer and keep
  249. //track of length of upstream data
  250. printf("sent less than full upstream bytes\n");
  251. break;
  252. }
  253. }
  254. }
  255. if (FD_ISSET(handle, &readfds)){
  256. //we have downstream data read for saving
  257. int32_t bytes_read;
  258. uint8_t *buf = calloc(1, BUFSIZ);
  259. bytes_read = recv(handle, buf, BUFSIZ, 0);
  260. if(bytes_read <= 0){
  261. break;
  262. }
  263. if(bytes_read > 0){
  264. //make a new queue block
  265. queue_block *new_block = calloc(1, sizeof(queue_block));
  266. new_block->len = bytes_read;
  267. new_block->offset = 0;
  268. new_block->data = buf;
  269. new_block->next = NULL;
  270. if(f->censored_queue == NULL)
  271. f->censored_queue = new_block;
  272. else{
  273. queue_block *last = f->censored_queue;
  274. while(last->next != NULL)
  275. last = last->next;
  276. last->next = new_block;
  277. }
  278. } else {
  279. printf("read 0 bytes\n");
  280. }
  281. }
  282. }
  283. free(thread_data);
  284. free(buffer);
  285. close(handle);
  286. return 0;
  287. }
  288. /** Replaces downstream record contents with data from the
  289. * censored queue, padding with garbage bytes if no more
  290. * censored data exists.
  291. *
  292. * Inputs:
  293. * f: the tagged flow
  294. * data: a pointer to the received packet's application
  295. * data
  296. * data_len: the length of the packet's application data
  297. * offset: if the packet is misordered, the number of
  298. * application-level bytes in missing packets
  299. *
  300. * Output:
  301. * Returns 0 on sucess
  302. */
  303. int replace_contents(flow *f, int32_t offset, struct packet_info *info){
  304. printf("Replacing contents of downstream data\n");
  305. uint8_t *p = info->app_data;
  306. int32_t tmp_len = info->app_data_len;
  307. //step 1: replace record contents
  308. //note: encrypted message will be original message size + EVP_GCM_TLS_EXPLICIT_IV_LEN + 16 byte pad
  309. //first check to see if there's anything in the outbox
  310. if(f->outbox_len > 0){
  311. #ifdef DEBUG
  312. if(f->outbox_len < info->app_data_len){
  313. printf("Next record:\n");
  314. for(int i=0; i< RECORD_HEADER_LEN; i++){
  315. printf("%02x ", p[f->outbox_len+i]);
  316. }
  317. printf("\n");
  318. } else {
  319. printf("Outbox takes up entire packet\n");
  320. }
  321. #endif
  322. if(tmp_len >= f->outbox_len){
  323. memcpy(p, f->outbox, f->outbox_len);
  324. p += f->outbox_len;
  325. tmp_len -= f->outbox_len;
  326. f->outbox_len = 0;
  327. free(f->outbox);
  328. } else {
  329. memcpy(p, f->outbox, tmp_len);
  330. uint8_t *tmp = calloc(1, f->outbox_len - tmp_len);
  331. f->outbox_len -= tmp_len;
  332. memcpy(tmp, f->outbox + tmp_len, f->outbox_len);
  333. free(f->outbox);
  334. f->outbox = tmp;
  335. tmp_len -= tmp_len;
  336. }
  337. }
  338. while(tmp_len > 0){
  339. struct record_header *record_hdr = (struct record_header*) p;
  340. uint32_t record_length = RECORD_LEN(record_hdr);
  341. #ifdef DEBUG
  342. fprintf(stdout, "Record:\n");
  343. for(int i=0; i< RECORD_HEADER_LEN; i++){
  344. printf("%02x ", p[i]);
  345. }
  346. printf("\n");
  347. #endif
  348. p += RECORD_HEADER_LEN;
  349. if(record_hdr->type != 0x17){
  350. //TODO: might need to decrypt and re-encrypt
  351. //printf("received non-application data\n");
  352. tmp_len -= (record_length+ RECORD_HEADER_LEN);
  353. p += record_length;
  354. continue;
  355. }
  356. uint8_t *new_record = calloc(1, record_length);
  357. memcpy(new_record, p, record_length);
  358. uint8_t *tmp_p = new_record;
  359. tmp_p += EVP_GCM_TLS_EXPLICIT_IV_LEN;
  360. struct slitheen_header *sl_hdr = (struct slitheen_header *) tmp_p;
  361. sl_hdr->marker = 0x01;
  362. sl_hdr->version = 0x01;
  363. sl_hdr->len = 0x00;
  364. int32_t remaining = record_length - (SLITHEEN_HEADER_LEN
  365. + EVP_GCM_TLS_EXPLICIT_IV_LEN + 16);
  366. tmp_p += SLITHEEN_HEADER_LEN;
  367. //Fill as much as we can from the censored_queue
  368. while((remaining > 0) && f->censored_queue != NULL){
  369. int32_t block_length = f->censored_queue->len;
  370. int32_t offset = f->censored_queue->offset;
  371. #ifdef DEBUG
  372. printf("Censored queue is at %p.\n", f->censored_queue);
  373. printf("This block as %d bytes left\n", block_length - offset);
  374. printf("We need %d bytes\n", remaining);
  375. #endif
  376. if(block_length > offset + remaining){
  377. //use part of the block, update offset
  378. memcpy(tmp_p, f->censored_queue->data+offset, remaining);
  379. f->censored_queue->offset += remaining;
  380. tmp_p += remaining;
  381. sl_hdr->len += remaining;
  382. remaining -= remaining;
  383. } else {
  384. //use all of the block and free it
  385. memcpy(tmp_p, f->censored_queue->data+offset, block_length - offset);
  386. free(f->censored_queue->data);
  387. f->censored_queue = f->censored_queue->next;
  388. tmp_p += (block_length - offset);
  389. sl_hdr->len += (block_length - offset);
  390. remaining -= (block_length - offset);
  391. }
  392. }
  393. sl_hdr->len = htons(sl_hdr->len);
  394. //now, if we need more data, fill with garbage
  395. if(remaining >0 ){
  396. //TODO: note, we may also be receiving misordered packets. Take Ian's suggestion into account here
  397. memset(tmp_p, 'A', remaining);
  398. }
  399. tmp_p = new_record;
  400. #ifdef DEBUG
  401. fprintf(stdout, "copied %d data and %d garbage bytes\n", ntohs(sl_hdr->len), remaining);
  402. printf("Slitheen header\n");
  403. for(int i=0; i<4; i++)
  404. printf("%02x ", tmp_p[EVP_GCM_TLS_EXPLICIT_IV_LEN+i]);
  405. printf("\n");
  406. #endif
  407. //step 3: encrypt new record
  408. int32_t success;
  409. if((success = encrypt(f, tmp_p, tmp_p, record_length-16, 1, 0x17, 1))< 0){
  410. fprintf(stdout,"encryption failed\n");
  411. return 0;
  412. }
  413. //copy new record into packet
  414. if(record_length +RECORD_HEADER_LEN > tmp_len){
  415. //We have a partial record
  416. memcpy(p, new_record, tmp_len - RECORD_HEADER_LEN);
  417. f->outbox_len = record_length - (tmp_len - RECORD_HEADER_LEN);
  418. //save left-overs in outbox
  419. f->outbox = calloc(1, f->outbox_len);
  420. memcpy(f->outbox, new_record + (tmp_len - RECORD_HEADER_LEN),
  421. f->outbox_len);
  422. free(new_record);
  423. } else {
  424. memcpy(p, new_record, record_length);
  425. free(new_record);
  426. }
  427. #ifdef DEBUG
  428. //check to see if next record still exists
  429. if(info->app_data_len > record_length + RECORD_HEADER_LEN){
  430. printf("Next record:\n");
  431. for(int i=0; i< RECORD_HEADER_LEN; i++){
  432. printf("%02x ", p[record_length+i]);
  433. }
  434. printf("\n");
  435. } else {
  436. printf("No extra record: %d <= %d + %d\n", data_len, record_length, RECORD_HEADER_LEN);
  437. }
  438. #endif
  439. tmp_len -= record_length+ RECORD_HEADER_LEN;
  440. p += record_length;
  441. }
  442. //step 4: recompute TCP checksum
  443. tcp_checksum(info);
  444. return 0;
  445. }
  446. /** Computes the TCP checksum of the data according to RFC 793
  447. * sum all 16-bit words in the segment, padd the last word if
  448. * needed
  449. *
  450. * there is a pseudo-header prefixed to the segment and
  451. * included in the checksum:
  452. *
  453. * +--------+--------+--------+--------+
  454. * | Source Address |
  455. * +--------+--------+--------+--------+
  456. * | Destination Address |
  457. * +--------+--------+--------+--------+
  458. * | zero | PTCL | TCP Length |
  459. * +--------+--------+--------+--------+
  460. */
  461. uint16_t tcp_checksum(struct packet_info *info){
  462. uint16_t tcp_length = info->app_data_len + info->size_tcp_hdr;
  463. struct in_addr src = info->ip_hdr->src;
  464. struct in_addr dst = info->ip_hdr->dst;
  465. uint8_t proto = IPPROTO_TCP;
  466. //set the checksum to zero
  467. info->tcp_hdr->chksum = 0;
  468. //sum pseudoheader
  469. uint32_t sum = (ntohl(src.s_addr)) >> 16;
  470. sum += (ntohl(src.s_addr)) &0xFFFF;
  471. sum += (ntohl(dst.s_addr)) >> 16;
  472. sum += (ntohl(dst.s_addr)) & 0xFFFF;
  473. sum += proto;
  474. sum += tcp_length;
  475. //sum tcp header (with zero-d checksum)
  476. uint8_t *p = (uint8_t *) info->tcp_hdr;
  477. for(int i=0; i < info->size_tcp_hdr; i+=2){
  478. sum += (uint16_t) ((p[i] << 8) + p[i+1]);
  479. }
  480. //now sum the application data
  481. p = info->app_data;
  482. for(int i=0; i< info->app_data_len-1; i+=2){
  483. sum += (uint16_t) ((p[i] << 8) + p[i+1]);
  484. }
  485. if(info->app_data_len %2 != 0){
  486. sum += (uint16_t) (p[info->app_data_len - 1]) << 8;
  487. }
  488. //now add most significant to last significant bits
  489. sum = (sum >> 16) + (sum & 0xFFFF);
  490. //now subtract from 0xFF
  491. sum = 0xFFFF - sum;
  492. //set chksum to calculated value
  493. info->tcp_hdr->chksum = ntohs(sum);
  494. return (uint16_t) sum;
  495. }