relay.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317
  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 <string.h>
  12. #include <openssl/bio.h>
  13. #include <openssl/evp.h>
  14. #include "relay.h"
  15. #include "slitheen.h"
  16. #include "flow.h"
  17. #include "crypto.h"
  18. #define DEBUG
  19. /** Called when a TLS application record is received for a
  20. * tagged flow. Upstream packets will be checked for covert
  21. * requests to censored sites, downstream packets will be
  22. * replaced with data from the censored queue or with garbage
  23. *
  24. * Inputs:
  25. * f: the tagged flow
  26. * info: the processed received application packet
  27. *
  28. * Output:
  29. * 0 on success, 1 on failure
  30. */
  31. int replace_packet(flow *f, struct packet_info *info){
  32. if (info == NULL || info->tcp_hdr == NULL){
  33. return 0;
  34. }
  35. #ifdef DEBUG
  36. fprintf(stdout,"Flow: %x:%d > %x:%d (%s)\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), (info->ip_hdr->src.s_addr != f->src_ip.s_addr)? "incoming":"outgoing");
  37. fprintf(stdout,"ID number: %u\n", htonl(info->ip_hdr->id));
  38. fprintf(stdout,"Sequence number: %u\n", htonl(info->tcp_hdr->sequence_num));
  39. fprintf(stdout,"Acknowledgement number: %u\n", htonl(info->tcp_hdr->ack_num));
  40. fflush(stdout);
  41. #endif
  42. if(info->app_data_len <= 0){
  43. return 0;
  44. }
  45. /* if outgoing, decrypt and look at header */
  46. if(info->ip_hdr->src.s_addr == f->src_ip.s_addr){
  47. read_header(f, info);
  48. return 0;
  49. } else {
  50. #ifdef DEBUG
  51. printf("Current sequence number: %d\n", f->downstream_seq_num);
  52. printf("Received sequence number: %d\n", htonl(info->tcp_hdr->sequence_num));
  53. #endif
  54. uint32_t offset = htonl(info->tcp_hdr->sequence_num) - f->downstream_seq_num;
  55. if(offset == 0)
  56. f->downstream_seq_num += info->app_data_len;
  57. /* if incoming, replace with data from queue */
  58. //if(htonl(tcp_hdr->sequence_num) >= f->seq_num){
  59. process_downstream(f, offset, info);
  60. //}//TODO: need to do something about replaying packets (maybe store previously sent data??
  61. #ifdef DEBUG2 //TODO: fix
  62. uint8_t *p = (uint8_t *) info->tcp_hdr;
  63. fprintf(stdout, "ip hdr length: %d\n", htons(info->ip_hdr->len));
  64. fprintf(stdout, "Injecting the following packet:\n");
  65. for(int i=0; i< htons(info->ip_hdr->len)-1; i++){
  66. fprintf(stdout, "%02x ", p[i]);
  67. }
  68. fprintf(stdout, "\n");
  69. fflush(stdout);
  70. #endif
  71. }
  72. return 0;
  73. }
  74. /** Reads the HTTP header of upstream data and searches for
  75. * a covert request in an x-slitheen header. Sends this
  76. * request to the indicated site and saves the response to
  77. * the censored queue
  78. *
  79. * TODO: change this to take SOCKS5 proxy connection and then relay data
  80. * based on stream ID
  81. *
  82. * Inputs:
  83. * f: the tagged flow
  84. * info: the processed received packet
  85. *
  86. * Ouput:
  87. * 0 on success, 1 on failure
  88. */
  89. int read_header(flow *f, struct packet_info *info){
  90. uint8_t *p = info->app_data;
  91. if (info->tcp_hdr == NULL){
  92. return 0;
  93. }
  94. uint8_t *record_ptr = NULL;
  95. struct record_header *record_hdr;
  96. uint32_t record_length;
  97. if(f->upstream_remaining > 0){
  98. //check to see whether the previous record has finished
  99. if(f->upstream_remaining > info->app_data_len){
  100. //ignore entire packet for now
  101. queue_block *new_block = calloc(1, sizeof(queue_block));
  102. uint8_t *block_data = calloc(1, info->app_data_len);
  103. memcpy(block_data, p, info->app_data_len);
  104. new_block->len = info->app_data_len;
  105. new_block->offset = 0;
  106. new_block->data = block_data;
  107. new_block->next = NULL;
  108. //add block to upstream data chain
  109. if(f->upstream_queue == NULL){
  110. f->upstream_queue = new_block;
  111. } else {
  112. queue_block *last = f->upstream_queue;
  113. while(last->next != NULL){
  114. last = last->next;
  115. }
  116. last->next = new_block;
  117. }
  118. f->upstream_remaining -= info->app_data_len;
  119. return 0;
  120. } else {
  121. //process what we have
  122. record_hdr = (struct record_header*) f->upstream_queue->data;
  123. record_length = RECORD_LEN(record_hdr);
  124. record_ptr = calloc(1, record_length+ RECORD_HEADER_LEN);
  125. queue_block *current = f->upstream_queue;
  126. int32_t offset =0;
  127. while(f->upstream_queue != NULL){
  128. memcpy(record_ptr+offset, current->data, current->len);
  129. offset += current->len;
  130. free(current->data);
  131. f->upstream_queue = current->next;
  132. free(current);
  133. current = f->upstream_queue;
  134. }
  135. memcpy(record_ptr+offset, p, f->upstream_remaining);
  136. p = record_ptr;
  137. record_hdr = (struct record_header*) p;
  138. f->upstream_remaining = 0;
  139. }
  140. } else {
  141. //check to see if the new record is too long
  142. record_hdr = (struct record_header*) p;
  143. record_length = RECORD_LEN(record_hdr);
  144. if(record_length > info->app_data_len){
  145. //add info to upstream queue
  146. queue_block *new_block = calloc(1, sizeof(queue_block));
  147. uint8_t *block_data = calloc(1, info->app_data_len);
  148. memcpy(block_data, p, info->app_data_len);
  149. new_block->len = info->app_data_len;
  150. new_block->offset = record_length; //re-appropriate this for len of record
  151. new_block->data = block_data;
  152. new_block->next = NULL;
  153. //add block to upstream queue
  154. if(f->upstream_queue == NULL){
  155. f->upstream_queue = new_block;
  156. } else {
  157. queue_block *last = f->upstream_queue;
  158. while(last->next != NULL){
  159. last = last->next;
  160. }
  161. last->next = new_block;
  162. }
  163. f->upstream_remaining = record_length - new_block->len;
  164. return 0;
  165. }
  166. }
  167. p+= RECORD_HEADER_LEN;
  168. uint8_t *decrypted_data = calloc(1, record_length);
  169. memcpy(decrypted_data, p, record_length);
  170. int32_t decrypted_len = encrypt(f, decrypted_data, decrypted_data, record_length, 0, record_hdr->type, 0);
  171. if(decrypted_len<0){
  172. if(record_ptr != NULL)
  173. free(record_ptr);
  174. free(decrypted_data);
  175. return 0;
  176. }
  177. if(record_hdr->type == 0x15){
  178. printf("received alert\n");
  179. for(int i=0; i<record_length; i++){
  180. printf("%02x ", decrypted_data[i]);
  181. }
  182. fflush(stdout);
  183. }
  184. #ifdef DEBUG
  185. printf("Upstream data: (%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));
  186. printf("%s\n", decrypted_data+EVP_GCM_TLS_EXPLICIT_IV_LEN);
  187. #endif
  188. /* search through decrypted data for x-ignore */
  189. char *header_ptr = strstr((const char *) decrypted_data+EVP_GCM_TLS_EXPLICIT_IV_LEN, "X-Slitheen");
  190. uint8_t *upstream_data;
  191. if(header_ptr == NULL){
  192. printf("Slitheen header not found(%x:%d > %x:%d) \n",info->ip_hdr->src.s_addr,info->tcp_hdr->src_port, info->ip_hdr->dst.s_addr, info->tcp_hdr->dst_port);
  193. fflush(stdout);
  194. if(record_ptr != NULL)
  195. free(record_ptr);
  196. free(decrypted_data);
  197. return 0;
  198. }
  199. #ifdef DEBUG
  200. printf("UPSTREAM: Found x-slitheen header\n");
  201. fflush(stdout);
  202. fprintf(stdout,"UPSTREAM Flow: %x:%d > %x:%d (%s)\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) ,(info->ip_hdr->src.s_addr != f->src_ip.s_addr)? "incoming":"outgoing");
  203. fprintf(stdout, "Sequence number: %d\n", ntohs(info->tcp_hdr->sequence_num));
  204. #endif
  205. header_ptr += strlen("X-Slitheen: ");
  206. if(*header_ptr == '\r' || *header_ptr == '\0'){
  207. printf("No messages\n");
  208. free(decrypted_data);
  209. return 0;
  210. }
  211. int32_t num_messages = 1;
  212. char *messages[50]; //TODO:make not just 10?
  213. messages[0] = header_ptr;
  214. char *c = header_ptr;
  215. while(*c != '\r' && *c != '\0'){
  216. if(*c == ' '){
  217. *c = '\0';
  218. messages[num_messages] = c+1;
  219. num_messages ++;
  220. }
  221. c++;
  222. }
  223. c++;
  224. *c = '\0';
  225. printf("UPSTREAM: Found %d messages\n", num_messages);
  226. for(int i=0; i< num_messages-1; i++){
  227. printf("Decoding message: %s\n", messages[i]);
  228. char *message = messages[i];
  229. //b64 decode the data
  230. int32_t decode_len = strlen(message);
  231. if(message[decode_len-2] == '='){
  232. decode_len = decode_len*3/4 - 2;
  233. } else if(message[decode_len-1] == '='){
  234. decode_len = decode_len*3/4 - 1;
  235. } else {
  236. decode_len = decode_len*3/4;
  237. }
  238. upstream_data = calloc(1, decode_len + 1);
  239. BIO *bio, *b64;
  240. bio = BIO_new_mem_buf(message, -1);
  241. b64 = BIO_new(BIO_f_base64());
  242. bio = BIO_push(b64, bio);
  243. BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
  244. int32_t output_len = BIO_read(bio, upstream_data, strlen(message));
  245. BIO_free_all(bio);
  246. #ifdef DEBUG
  247. printf("Decoded to get %d bytes:\n", output_len);
  248. for(int j=0; j< output_len; j++){
  249. printf("%02x ", upstream_data[j]);
  250. }
  251. printf("\n");
  252. fflush(stdout);
  253. #endif
  254. p = upstream_data;
  255. if(i== 0){
  256. //this is the Slitheen ID
  257. printf("Slitheen ID:");
  258. for(int j=0; j< output_len; j++){
  259. printf("%02x ", p[j]);
  260. }
  261. printf("\n");
  262. //find stream table or create new one
  263. client *last = clients->first;
  264. while(last != NULL){
  265. if(!memcmp(last->slitheen_id, p, output_len)){
  266. f->streams = last->streams;
  267. f->downstream_queue = last->downstream_queue;
  268. printf("Found client in table!\n");
  269. break;
  270. } else {
  271. for(int j=0; j< output_len; j++){
  272. printf("%02x ", last->slitheen_id[j]);
  273. }
  274. printf(" != ");
  275. for(int j=0; j< output_len; j++){
  276. printf("%02x ", p[j]);
  277. }
  278. printf("\n");
  279. }
  280. last = last->next;
  281. }
  282. if(f->streams == NULL){
  283. printf("Making new client\n");
  284. //create new client
  285. client *new_client = calloc(1, sizeof(client));
  286. memcpy(new_client->slitheen_id, p, output_len);
  287. new_client->streams = calloc(1, sizeof(stream_table));
  288. new_client->streams->first = NULL;
  289. new_client->downstream_queue = calloc(1, sizeof(data_queue));
  290. new_client->downstream_queue->first_block = NULL;
  291. new_client->next = NULL;
  292. //add to client table
  293. if(clients->first == NULL){
  294. clients->first = new_client;
  295. } else {
  296. client *last = clients->first;
  297. while(last->next != NULL){
  298. last = last->next;
  299. }
  300. last->next = new_client;
  301. }
  302. //set f's stream table
  303. f->streams = new_client->streams;
  304. f->downstream_queue = new_client->downstream_queue;
  305. }
  306. free(upstream_data);
  307. continue;
  308. }
  309. while(output_len > 0){
  310. struct sl_up_hdr *sl_hdr = (struct sl_up_hdr *) p;
  311. uint8_t stream_id = sl_hdr->stream_id;
  312. uint16_t stream_len = ntohs(sl_hdr->len);
  313. p += sizeof(struct sl_up_hdr);
  314. output_len -= sizeof(struct sl_up_hdr);
  315. stream_table *streams = f->streams;
  316. //If a thread for this stream id exists, get the thread info and pipe data
  317. int32_t stream_pipe = -1;
  318. stream *last = streams->first;
  319. stream *prev = last;
  320. if(streams->first != NULL){
  321. if(last->stream_id == stream_id){
  322. stream_pipe = last->pipefd;
  323. } else {
  324. while(last->next != NULL){
  325. prev = last;
  326. last = last->next;
  327. if(last->stream_id == stream_id){
  328. stream_pipe = last->pipefd;
  329. break;
  330. }
  331. }
  332. }
  333. }
  334. if(stream_pipe != -1){
  335. //check to see if this is a close message
  336. if(stream_len == 0){
  337. close(stream_pipe);
  338. //remove from stream id table
  339. if(last == streams->first){
  340. streams->first = last->next;
  341. } else {
  342. prev->next = last->next;
  343. }
  344. free(last);
  345. break;
  346. }
  347. printf("Found stream id %d\n", last->stream_id);
  348. int32_t bytes_sent = write(stream_pipe, p, stream_len);
  349. } else if(stream_len > 0){
  350. /*Else, spawn a thread to handle the proxy to this site*/
  351. printf("creating new thread for stream id %d\n", stream_id);
  352. pthread_t proxy_thread;
  353. int32_t pipefd[2];
  354. if(pipe(pipefd) < 0){
  355. free(decrypted_data);
  356. if(record_ptr != NULL)
  357. free(record_ptr);
  358. return 1;
  359. }
  360. uint8_t *initial_data = calloc(1,stream_len);
  361. memcpy(initial_data, p, stream_len);
  362. struct proxy_thread_data *thread_data =
  363. calloc(1, sizeof(struct proxy_thread_data));
  364. thread_data->initial_data = initial_data;
  365. thread_data->initial_len = stream_len;
  366. thread_data->stream_id = stream_id;
  367. thread_data->pipefd = pipefd[0];
  368. thread_data->streams = f->streams;
  369. thread_data->downstream_queue = f->downstream_queue;
  370. pthread_create(&proxy_thread, NULL, proxy_covert_site, (void *) thread_data);
  371. pthread_detach(proxy_thread);
  372. //add stream to table
  373. stream *new_stream = calloc(1, sizeof(stream));
  374. new_stream->stream_id = stream_id;
  375. new_stream->pipefd = pipefd[1];
  376. new_stream->next = NULL;
  377. if(streams->first == NULL){
  378. streams->first = new_stream;
  379. } else {
  380. stream *last = streams->first;
  381. while(last->next != NULL){
  382. last = last->next;
  383. }
  384. last->next = new_stream;
  385. }
  386. } else{
  387. printf("Error, stream len 0\n");
  388. break;
  389. }
  390. output_len -= stream_len;
  391. p += stream_len;
  392. }
  393. free(upstream_data);
  394. }
  395. //save a reference to the proxy threads in a global table
  396. free(decrypted_data);
  397. if(record_ptr != NULL)
  398. free(record_ptr);
  399. return 0;
  400. }
  401. /** Called by spawned pthreads in read_header to send upstream
  402. * data to the censored site and receive responses. Downstream
  403. * data is stored in the slitheen id's downstream_queue. Function and
  404. * thread will terminate when the client closes the connection
  405. * to the covert destination
  406. *
  407. * Input:
  408. * A struct that contains the following information:
  409. * - the tagged flow
  410. * - the initial upstream data (including connect request)
  411. * - the read end of the pipe
  412. *
  413. */
  414. void *proxy_covert_site(void *data){
  415. struct proxy_thread_data *thread_data =
  416. (struct proxy_thread_data *) data;
  417. uint8_t *p = thread_data->initial_data;
  418. uint16_t data_len = thread_data->initial_len;
  419. uint8_t stream_id = thread_data->stream_id;
  420. int32_t bytes_sent;
  421. stream_table *streams = thread_data->streams;
  422. data_queue *downstream_queue = thread_data->downstream_queue;
  423. struct socks_req *clnt_req = (struct socks_req *) p;
  424. p += 4;
  425. data_len -= 4;
  426. int32_t handle = -1;
  427. //see if it's a connect request
  428. if(clnt_req->cmd != 0x01){
  429. goto err;
  430. }
  431. struct sockaddr_in dest;
  432. dest.sin_family = AF_INET;
  433. uint8_t domain_len;
  434. switch(clnt_req->addr_type){
  435. case 0x01:
  436. //IPv4
  437. dest.sin_addr.s_addr = *((uint32_t*) p);
  438. p += 4;
  439. data_len -= 4;
  440. break;
  441. case 0x03:
  442. //domain name
  443. domain_len = p[0];
  444. p++;
  445. data_len --;
  446. uint8_t *domain_name = calloc(1, domain_len+1);
  447. memcpy(domain_name, p, domain_len);
  448. domain_name[domain_len] = '\0';
  449. struct hostent *host;
  450. host = gethostbyname((const char *) domain_name);
  451. dest.sin_addr = *((struct in_addr *) host->h_addr);
  452. p += domain_len;
  453. data_len -= domain_len;
  454. free(domain_name);
  455. break;
  456. case 0x04:
  457. //IPv6
  458. goto err;//TODO: fix this
  459. break;
  460. }
  461. //now set the port
  462. dest.sin_port = *((uint16_t *) p);
  463. p += 2;
  464. data_len -= 2;
  465. handle = socket(AF_INET, SOCK_STREAM, 0);
  466. if(handle < 0){
  467. goto err;
  468. }
  469. struct sockaddr_in my_addr;
  470. socklen_t my_addr_len = sizeof(my_addr);
  471. int32_t error = connect (handle, (struct sockaddr *) &dest, sizeof (struct sockaddr));
  472. if(error <0){
  473. goto err;
  474. }
  475. getsockname(handle, (struct sockaddr *) &my_addr, &my_addr_len);
  476. uint8_t *response = calloc(1, 11);
  477. //now send the reply to the client
  478. response[0] = 0x05;
  479. response[1] = 0x00;//TODO: make this accurate
  480. response[2] = 0x00;
  481. response[3] = 0x01;
  482. *((uint32_t *) (response + 4)) = my_addr.sin_addr.s_addr;
  483. *((uint16_t *) (response + 8)) = my_addr.sin_port;
  484. #ifdef DEBUG
  485. printf("Bound to %x:%d\n", my_addr.sin_addr.s_addr, ntohs(my_addr.sin_port));
  486. printf("Downstream response (id %d):\n", stream_id);
  487. for(int i=0; i< 10; i++){
  488. printf("%02x ", response[i]);
  489. }
  490. printf("\n");
  491. fflush(stdout);
  492. #endif
  493. #ifdef OLD
  494. //No longer need to send response
  495. queue_block *new_block = calloc(1, sizeof(queue_block));
  496. new_block->len = 10;
  497. new_block->offset = 0;
  498. new_block->data = response;
  499. new_block->next = NULL;
  500. new_block->stream_id = stream_id;
  501. if(downstream_queue->first_block == NULL){
  502. downstream_queue->first_block = new_block;
  503. }
  504. else{
  505. queue_block *last = downstream_queue->first_block;
  506. while(last->next != NULL)
  507. last = last->next;
  508. last->next = new_block;
  509. }
  510. #endif
  511. //see if there were extra upstream bytes
  512. if(data_len > 0){
  513. printf("Data len is %d\n", data_len);
  514. printf("Upstream bytes: ");
  515. for(int i=0; i< data_len; i++){
  516. printf("%02x ", p[i]);
  517. }
  518. printf("\n");
  519. bytes_sent = send(handle, p,
  520. data_len, 0);
  521. if( bytes_sent <= 0){
  522. goto err;
  523. }
  524. }
  525. uint8_t *buffer = calloc(1, BUFSIZ);
  526. int32_t buffer_len = BUFSIZ;
  527. //now select on reading from the pipe and from the socket
  528. for(;;){
  529. fd_set readfds;
  530. fd_set writefds;
  531. int32_t nfds = (handle > thread_data->pipefd) ?
  532. handle +1 : thread_data->pipefd + 1;
  533. FD_ZERO(&readfds);
  534. FD_ZERO(&writefds);
  535. FD_SET(thread_data->pipefd, &readfds);
  536. FD_SET(handle, &readfds);
  537. FD_SET(handle, &writefds);
  538. if (select(nfds, &readfds, &writefds, NULL, NULL) < 0){
  539. printf("select error\n");
  540. break;
  541. }
  542. if(FD_ISSET(thread_data->pipefd, &readfds) && FD_ISSET(handle, &writefds)){
  543. //we have upstream data ready for writing
  544. int32_t bytes_read = read(thread_data->pipefd, buffer, buffer_len);
  545. if(bytes_read > 0){
  546. #ifdef DEBUG
  547. printf("PROXY (id %d): read %d bytes from pipe\n", stream_id, bytes_read);
  548. for(int i=0; i< bytes_read; i++){
  549. printf("%02x ", buffer[i]);
  550. }
  551. printf("\n");
  552. printf("%s\n", buffer);
  553. #endif
  554. bytes_sent = send(handle, buffer,
  555. bytes_read, 0);
  556. if( bytes_sent <= 0){
  557. break;
  558. } else if (bytes_sent < bytes_read){
  559. //TODO: should update buffer and keep
  560. //track of length of upstream data
  561. break;
  562. }
  563. } else {
  564. break;
  565. }
  566. }
  567. if (FD_ISSET(handle, &readfds)){
  568. //we have downstream data read for saving
  569. int32_t bytes_read;
  570. bytes_read = recv(handle, buffer, buffer_len, 0);
  571. if(bytes_read > 0){
  572. uint8_t *new_data = calloc(1, bytes_read);
  573. memcpy(new_data, buffer, bytes_read);
  574. #ifdef DEBUG
  575. printf("PROXY (id %d): read %d bytes from censored site\n",stream_id, bytes_read);
  576. for(int i=0; i< bytes_read; i++){
  577. printf("%02x ", buffer[i]);
  578. }
  579. printf("\n");
  580. #endif
  581. //make a new queue block
  582. queue_block *new_block = calloc(1, sizeof(queue_block));
  583. new_block->len = bytes_read;
  584. new_block->offset = 0;
  585. new_block->data = new_data;
  586. new_block->next = NULL;
  587. new_block->stream_id = stream_id;
  588. if(downstream_queue->first_block == NULL){
  589. downstream_queue->first_block = new_block;
  590. }
  591. else{
  592. queue_block *last = downstream_queue->first_block;
  593. while(last->next != NULL)
  594. last = last->next;
  595. last->next = new_block;
  596. }
  597. } else {
  598. break;
  599. }
  600. }
  601. }
  602. //remove self from list
  603. stream *last = streams->first;
  604. stream *prev = last;
  605. if(streams->first != NULL){
  606. if(last->stream_id == stream_id){
  607. streams->first = last->next;
  608. printf("Freeing (2) %p\n", last);
  609. free(last);
  610. } else {
  611. while(last->next != NULL){
  612. prev = last;
  613. last = last->next;
  614. if(last->stream_id == stream_id){
  615. prev->next = last->next;
  616. printf("Freeing (2) %p\n", last);
  617. free(last);
  618. break;
  619. }
  620. }
  621. }
  622. }
  623. if(thread_data->initial_data != NULL){
  624. free(thread_data->initial_data);
  625. }
  626. free(thread_data);
  627. free(buffer);
  628. close(handle);
  629. pthread_exit(NULL);
  630. return 0;
  631. err:
  632. //remove self from list
  633. last = streams->first;
  634. prev = last;
  635. if(streams->first != NULL){
  636. if(last->stream_id == stream_id){
  637. streams->first = last->next;
  638. free(last);
  639. } else {
  640. while(last->next != NULL){
  641. prev = last;
  642. last = last->next;
  643. if(last->stream_id == stream_id){
  644. prev->next = last->next;
  645. free(last);
  646. break;
  647. }
  648. }
  649. }
  650. }
  651. if(thread_data->initial_data != NULL){
  652. free(thread_data->initial_data);
  653. }
  654. free(thread_data);
  655. if(handle > 0){
  656. close(handle);
  657. }
  658. pthread_exit(NULL);
  659. return 0;
  660. }
  661. /** Replaces downstream record contents with data from the
  662. * censored queue, padding with garbage bytes if no more
  663. * censored data exists.
  664. *
  665. * Inputs:
  666. * f: the tagged flow
  667. * data: a pointer to the received packet's application
  668. * data
  669. * data_len: the length of the packet's application data
  670. * offset: if the packet is misordered, the number of
  671. * application-level bytes in missing packets
  672. *
  673. * Output:
  674. * Returns 0 on sucess
  675. */
  676. int process_downstream(flow *f, int32_t offset, struct packet_info *info){
  677. uint8_t changed = 0;
  678. uint8_t *p = info->app_data;
  679. uint32_t remaining_packet_len = info->app_data_len;
  680. printf("Application data length: %d at %p\n", info->app_data_len, info->app_data);
  681. if(f->remaining_record_len > 0){
  682. //ignore bytes until the end of the record
  683. if(f->remaining_record_len > remaining_packet_len){ //ignore entire packet
  684. if(f->outbox_len > 0){
  685. changed = 1;
  686. memcpy(p, f->outbox + f->outbox_offset, remaining_packet_len);
  687. f->outbox_len -= remaining_packet_len;
  688. f->outbox_offset += remaining_packet_len;
  689. }
  690. f->remaining_record_len -= remaining_packet_len;
  691. remaining_packet_len -= remaining_packet_len;
  692. } else {
  693. if(f->outbox_len > 0){
  694. changed = 1;
  695. memcpy(p, f->outbox + f->outbox_offset, f->remaining_record_len);
  696. f->outbox_len = 0;
  697. f->outbox_offset=0;
  698. free(f->outbox);
  699. }
  700. p += f->remaining_record_len;
  701. remaining_packet_len -= f->remaining_record_len;
  702. f->remaining_record_len = 0;
  703. }
  704. }
  705. while(remaining_packet_len > 0){ //while bytes remain in the packet
  706. if(remaining_packet_len < RECORD_HEADER_LEN){
  707. #ifdef DEBUG
  708. printf("partial record header: \n");
  709. for(int i= 0; i< remaining_packet_len; i++){
  710. printf("%02x ", p[i]);
  711. }
  712. printf("\n");
  713. fflush(stdout);
  714. #endif
  715. f->partial_record_header = calloc(1, RECORD_HEADER_LEN);
  716. memcpy(f->partial_record_header, p, remaining_packet_len);
  717. f->partial_record_header_len = remaining_packet_len;
  718. remaining_packet_len -= remaining_packet_len;
  719. break;
  720. }
  721. struct record_header *record_hdr;
  722. if(f->partial_record_header_len > 0){
  723. memcpy(f->partial_record_header+ f->partial_record_header_len,
  724. p, RECORD_HEADER_LEN - f->partial_record_header_len);
  725. record_hdr = (struct record_header *) f->partial_record_header;
  726. } else {
  727. record_hdr = (struct record_header*) p;
  728. }
  729. uint32_t record_len = RECORD_LEN(record_hdr);
  730. #ifdef DEBUG
  731. fprintf(stdout,"Flow: %x > %x (%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");
  732. fprintf(stdout,"ID number: %u\n", htonl(info->ip_hdr->id));
  733. fprintf(stdout,"Sequence number: %u\n", htonl(info->tcp_hdr->sequence_num));
  734. fprintf(stdout,"Acknowledgement number: %u\n", htonl(info->tcp_hdr->ack_num));
  735. fprintf(stdout, "Record:\n");
  736. for(int i=0; i< RECORD_HEADER_LEN; i++){
  737. printf("%02x ", ((uint8_t *) record_hdr)[i]);
  738. }
  739. printf("\n");
  740. #endif
  741. p += (RECORD_HEADER_LEN - f->partial_record_header_len);
  742. remaining_packet_len -= (RECORD_HEADER_LEN - f->partial_record_header_len);
  743. uint8_t *record_ptr = p; //points to the beginning of record data
  744. uint32_t remaining_record_len = record_len;
  745. if(record_len > remaining_packet_len){
  746. f->remaining_record_len = record_len - remaining_packet_len;
  747. if(f->httpstate == PARSE_HEADER || f->httpstate == BEGIN_CHUNK || f->httpstate == END_CHUNK){
  748. f->httpstate = FORFEIT_REST;
  749. } else if( f->httpstate == MID_CONTENT || f->httpstate == MID_CHUNK){
  750. f->remaining_response_len -= record_len - 24; //len of IV and padding
  751. if(f->remaining_response_len >= 0 && f->replace_response){
  752. //#ifdef nothing
  753. //make a huge record, encrypt it, and then place it in the outbox
  754. f->outbox = calloc(1, record_len+1);
  755. f->outbox_len = record_len;
  756. f->outbox_offset = 0;
  757. fill_with_downstream(f, f->outbox + EVP_GCM_TLS_EXPLICIT_IV_LEN , record_len - (EVP_GCM_TLS_EXPLICIT_IV_LEN+ 16)); //for now hard coded length of padding. TODO: fix this
  758. //encrypt
  759. int32_t n = encrypt(f, f->outbox, f->outbox,
  760. record_len - 16, 1,
  761. record_hdr->type, 1);
  762. if(n < 0){
  763. fprintf(stdout,"outbox encryption failed\n");
  764. } else {
  765. memcpy(p, f->outbox, remaining_packet_len);
  766. changed = 1;
  767. f->outbox_len -= remaining_packet_len;
  768. f->outbox_offset += remaining_packet_len;
  769. }
  770. //#endif
  771. }
  772. if(f->remaining_response_len == 0){
  773. if(f->httpstate == MID_CHUNK)
  774. f->httpstate = END_CHUNK;
  775. else {
  776. f->httpstate = PARSE_HEADER;
  777. }
  778. }
  779. if(f->remaining_response_len < 0){
  780. f->remaining_response_len = 0;
  781. f->httpstate = FORFEIT_REST;
  782. }
  783. }
  784. remaining_packet_len -= remaining_packet_len;
  785. if(f->partial_record_header_len > 0){
  786. f->partial_record_header_len = 0;
  787. free(f->partial_record_header);
  788. }
  789. break;
  790. }
  791. //now decrypt the record
  792. int32_t n = encrypt(f, record_ptr, record_ptr, record_len, 1,
  793. record_hdr->type, 0);
  794. if(n < 0){
  795. //do something smarter here
  796. if(f->partial_record_header_len > 0){
  797. f->partial_record_header_len = 0;
  798. free(f->partial_record_header);
  799. }
  800. return 0;
  801. }
  802. changed = 1;
  803. #ifdef DEBUG_DOWNSTREAM
  804. printf("Decryption succeeded\n");
  805. printf("Bytes:\n");
  806. for(int i=0; i< n; i++){
  807. printf("%02x ", record_ptr[EVP_GCM_TLS_EXPLICIT_IV_LEN+i]);
  808. }
  809. printf("\n");
  810. printf("Text:\n");
  811. printf("%s\n", record_ptr+EVP_GCM_TLS_EXPLICIT_IV_LEN);
  812. #endif
  813. p += EVP_GCM_TLS_EXPLICIT_IV_LEN;
  814. char *len_ptr, *needle;
  815. remaining_record_len = n;
  816. while(remaining_record_len > 0){
  817. switch(f->httpstate){
  818. case PARSE_HEADER:
  819. //determine whether it's transfer encoded or otherwise
  820. //figure out what the content-type is
  821. len_ptr = strstr((const char *) p, "Content-Type: image");
  822. if(len_ptr != NULL){
  823. f->replace_response = 1;
  824. memcpy(len_ptr + 14, "slitheen", 8);
  825. char *c = len_ptr + 14+8;
  826. while(c[0] != '\r'){
  827. c[0] = ' ';
  828. c++;
  829. }
  830. } else {
  831. f->replace_response = 0;
  832. }
  833. len_ptr = strstr((const char *) p, "Transfer-Encoding");
  834. if(len_ptr != NULL){
  835. if(!memcmp(len_ptr + 19, "chunked", 7)){
  836. //now find end of header
  837. //printf("chunked encoding\n");
  838. len_ptr = strstr((const char *) p, "\r\n\r\n");
  839. if(len_ptr != NULL){
  840. f->httpstate = BEGIN_CHUNK;
  841. remaining_record_len -= (((uint8_t *)len_ptr - p) + 4);
  842. p = (uint8_t *) len_ptr + 4;
  843. }
  844. }
  845. } else {
  846. len_ptr = strstr((const char *) p, "Content-Length");
  847. if(len_ptr != NULL){
  848. len_ptr += 15;
  849. f->remaining_response_len = strtol((const char *) len_ptr, NULL, 10);
  850. //printf("content-length: %d\n", f->remaining_response_len);
  851. len_ptr = strstr((const char *) p, "\r\n\r\n");
  852. if(len_ptr != NULL){
  853. f->httpstate = MID_CONTENT;
  854. remaining_record_len -= (((uint8_t *)len_ptr - p) + 4);
  855. p = (uint8_t *) len_ptr + 4;
  856. } else {
  857. remaining_record_len = 0;
  858. //printf("Missing end of header. Sending to FORFEIT_REST\n");
  859. f->httpstate = FORFEIT_REST;
  860. }
  861. } else {
  862. //printf("No content length of transfer encoding field, sending to FORFEIT_REST\n");
  863. f->httpstate = FORFEIT_REST;
  864. remaining_record_len = 0;
  865. }
  866. }
  867. break;
  868. case MID_CONTENT:
  869. //check if content is replaceable
  870. if(f->remaining_response_len > remaining_record_len){
  871. if(f->replace_response){
  872. fill_with_downstream(f, p, remaining_record_len);
  873. #ifdef DEBUG
  874. printf("Replaced with:\n");
  875. for(int i=0; i< remaining_record_len; i++){
  876. printf("%02x ", p[i]);
  877. }
  878. printf("\n");
  879. #endif
  880. }
  881. f->remaining_response_len -= remaining_record_len;
  882. p += remaining_record_len;
  883. remaining_record_len = 0;
  884. } else {
  885. if(f->replace_response){
  886. fill_with_downstream(f, p, remaining_record_len);
  887. #ifdef DEBUG
  888. printf("Replaced with:\n");
  889. for(int i=0; i< remaining_record_len; i++){
  890. printf("%02x ", p[i]);
  891. }
  892. printf("\n");
  893. #endif
  894. }
  895. remaining_record_len -= f->remaining_response_len;
  896. p += f->remaining_response_len;
  897. f->httpstate = PARSE_HEADER;
  898. f->remaining_response_len = 0;
  899. }
  900. break;
  901. case BEGIN_CHUNK:
  902. {
  903. int32_t chunk_size = strtol((const char *) p, NULL, 16);
  904. if(chunk_size == 0){
  905. f->httpstate = END_BODY;
  906. } else {
  907. f->httpstate = MID_CHUNK;
  908. }
  909. f->remaining_response_len = chunk_size;
  910. needle = strstr((const char *) p, "\r\n");
  911. if(needle != NULL){
  912. remaining_record_len -= ((uint8_t *) needle - p + 2);
  913. p = (uint8_t *) needle + 2;
  914. } else {
  915. remaining_record_len = 0;
  916. printf("Couldn't find chunk, sending to FORFEIT_REST\n");
  917. f->httpstate = FORFEIT_REST;
  918. }
  919. }
  920. break;
  921. case MID_CHUNK:
  922. if(f->remaining_response_len > remaining_record_len){
  923. if(f->replace_response){
  924. fill_with_downstream(f, p, remaining_record_len);
  925. #ifdef DEBUG
  926. printf("Replaced with:\n");
  927. for(int i=0; i< remaining_record_len; i++){
  928. printf("%02x ", p[i]);
  929. }
  930. printf("\n");
  931. #endif
  932. }
  933. f->remaining_response_len -= remaining_record_len;
  934. p += remaining_record_len;
  935. remaining_record_len = 0;
  936. } else {
  937. if(f->replace_response){
  938. fill_with_downstream(f, p, f->remaining_response_len);
  939. #ifdef DEBUG
  940. printf("Replaced with:\n");
  941. for(int i=0; i< f->remaining_response_len; i++){
  942. printf("%02x ", p[i]);
  943. }
  944. printf("\n");
  945. #endif
  946. }
  947. remaining_record_len -= f->remaining_response_len;
  948. p += f->remaining_response_len;
  949. f->remaining_response_len = 0;
  950. f->httpstate = END_CHUNK;
  951. }
  952. break;
  953. case END_CHUNK:
  954. needle = strstr((const char *) p, "\r\n");
  955. if(needle != NULL){
  956. f->httpstate = BEGIN_CHUNK;
  957. p += 2;
  958. remaining_record_len -= 2;
  959. } else {
  960. remaining_record_len = 0;
  961. //printf("Couldn't find end of chunk, sending to FORFEIT_REST\n");
  962. f->httpstate = FORFEIT_REST;
  963. }
  964. break;
  965. case END_BODY:
  966. needle = strstr((const char *) p, "\r\n");
  967. if(needle != NULL){
  968. f->httpstate = PARSE_HEADER;
  969. p += 2;
  970. remaining_record_len -= 2;
  971. } else {
  972. remaining_record_len = 0;
  973. //printf("Couldn't find end of body, sending to FORFEIT_REST\n");
  974. f->httpstate = FORFEIT_REST;
  975. }
  976. break;
  977. case FORFEIT_REST:
  978. case USE_REST:
  979. remaining_record_len = 0;
  980. break;
  981. default:
  982. break;
  983. }
  984. }
  985. if((n = encrypt(f, record_ptr, record_ptr,
  986. n + EVP_GCM_TLS_EXPLICIT_IV_LEN, 1, record_hdr->type,
  987. 1)) < 0){
  988. if(f->partial_record_header_len > 0){
  989. f->partial_record_header_len = 0;
  990. free(f->partial_record_header);
  991. }
  992. return 0;
  993. }
  994. p = record_ptr + record_len;
  995. remaining_packet_len -= record_len;
  996. if(f->partial_record_header_len > 0){
  997. f->partial_record_header_len = 0;
  998. free(f->partial_record_header);
  999. }
  1000. }
  1001. if(changed){
  1002. tcp_checksum(info);
  1003. }
  1004. return 0;
  1005. }
  1006. /** Fills a given pointer with downstream data of the specified length. If no downstream data
  1007. * exists, pads it with garbage bytes. All downstream data is accompanied by a stream id and
  1008. * lengths of both the downstream data and garbage data
  1009. *
  1010. * Inputs:
  1011. * data: a pointer to where the downstream data should be entered
  1012. * length: The length of the downstream data required
  1013. *
  1014. */
  1015. int fill_with_downstream(flow *f, uint8_t *data, int32_t length){
  1016. uint8_t *p = data;
  1017. int32_t remaining = length;
  1018. struct slitheen_header *sl_hdr;
  1019. data_queue *downstream_queue = f->downstream_queue;
  1020. //Fill as much as we can from the censored_queue
  1021. while((remaining > SLITHEEN_HEADER_LEN) && downstream_queue->first_block != NULL){
  1022. queue_block *first_block = downstream_queue->first_block;
  1023. int32_t block_length = first_block->len;
  1024. int32_t offset = first_block->offset;
  1025. #ifdef DEBUG
  1026. printf("Censored queue is at %p.\n", first_block);
  1027. printf("This block has %d bytes left\n", block_length - offset);
  1028. printf("We need %d bytes\n", remaining - SLITHEEN_HEADER_LEN);
  1029. #endif
  1030. sl_hdr = (struct slitheen_header *) p;
  1031. sl_hdr->stream_id = first_block->stream_id;
  1032. sl_hdr->len = 0x00;
  1033. sl_hdr->garbage = 0x00;
  1034. p += SLITHEEN_HEADER_LEN;
  1035. remaining -= SLITHEEN_HEADER_LEN;
  1036. if(block_length > offset + remaining){
  1037. //use part of the block, update offset
  1038. memcpy(p, first_block->data+offset, remaining);
  1039. first_block->offset += remaining;
  1040. p += remaining;
  1041. sl_hdr->len = remaining;
  1042. remaining -= remaining;
  1043. } else {
  1044. //use all of the block and free it
  1045. memcpy(p, first_block->data+offset, block_length - offset);
  1046. free(first_block->data);
  1047. downstream_queue->first_block = first_block->next;
  1048. free(first_block);
  1049. p += (block_length - offset);
  1050. sl_hdr->len = (block_length - offset);
  1051. remaining -= (block_length - offset);
  1052. }
  1053. sl_hdr->len = htons(sl_hdr->len);
  1054. #ifdef DEBUG
  1055. printf("DWNSTRM: slitheen header: ");
  1056. for(int i=0; i< SLITHEEN_HEADER_LEN; i++){
  1057. printf("%02x ",((uint8_t *) sl_hdr)[i]);
  1058. }
  1059. printf("\n");
  1060. printf("Sending %d downstream bytes:", ntohs(sl_hdr->len));
  1061. for(int i=0; i< ntohs(sl_hdr->len); i++){
  1062. printf("%02x ", ((uint8_t *) sl_hdr)[i+SLITHEEN_HEADER_LEN]);
  1063. }
  1064. printf("\n");
  1065. #endif
  1066. }
  1067. //now, if we need more data, fill with garbage
  1068. if(remaining > SLITHEEN_HEADER_LEN ){
  1069. //TODO: note, we may also be receiving misordered packets. Take Ian's suggestion into account here
  1070. sl_hdr = (struct slitheen_header *) p;
  1071. sl_hdr->stream_id = 0x00;
  1072. remaining -= SLITHEEN_HEADER_LEN;
  1073. sl_hdr->len = htons(remaining);
  1074. sl_hdr->garbage = htons(remaining);
  1075. //#ifdef DEBUG
  1076. printf("DWNSTRM: slitheen header: ");
  1077. for(int i=0; i< SLITHEEN_HEADER_LEN; i++){
  1078. printf("%02x ", p[i]);
  1079. }
  1080. printf("\n");
  1081. //#endif
  1082. p += SLITHEEN_HEADER_LEN;
  1083. memset(p, 'A', remaining);
  1084. }
  1085. return 0;
  1086. }
  1087. /** Computes the TCP checksum of the data according to RFC 793
  1088. * sum all 16-bit words in the segment, pad the last word if
  1089. * needed
  1090. *
  1091. * there is a pseudo-header prefixed to the segment and
  1092. * included in the checksum:
  1093. *
  1094. * +--------+--------+--------+--------+
  1095. * | Source Address |
  1096. * +--------+--------+--------+--------+
  1097. * | Destination Address |
  1098. * +--------+--------+--------+--------+
  1099. * | zero | PTCL | TCP Length |
  1100. * +--------+--------+--------+--------+
  1101. */
  1102. uint16_t tcp_checksum(struct packet_info *info){
  1103. uint16_t tcp_length = info->app_data_len + info->size_tcp_hdr;
  1104. struct in_addr src = info->ip_hdr->src;
  1105. struct in_addr dst = info->ip_hdr->dst;
  1106. uint8_t proto = IPPROTO_TCP;
  1107. //set the checksum to zero
  1108. info->tcp_hdr->chksum = 0;
  1109. //sum pseudoheader
  1110. uint32_t sum = (ntohl(src.s_addr)) >> 16;
  1111. sum += (ntohl(src.s_addr)) &0xFFFF;
  1112. sum += (ntohl(dst.s_addr)) >> 16;
  1113. sum += (ntohl(dst.s_addr)) & 0xFFFF;
  1114. sum += proto;
  1115. sum += tcp_length;
  1116. //sum tcp header (with zero-d checksum)
  1117. uint8_t *p = (uint8_t *) info->tcp_hdr;
  1118. for(int i=0; i < info->size_tcp_hdr; i+=2){
  1119. sum += (uint16_t) ((p[i] << 8) + p[i+1]);
  1120. }
  1121. //now sum the application data
  1122. p = info->app_data;
  1123. for(int i=0; i< info->app_data_len-1; i+=2){
  1124. sum += (uint16_t) ((p[i] << 8) + p[i+1]);
  1125. }
  1126. if(info->app_data_len %2 != 0){
  1127. sum += (uint16_t) (p[info->app_data_len - 1]) << 8;
  1128. }
  1129. //now add most significant to last significant bits
  1130. sum = (sum >> 16) + (sum & 0xFFFF);
  1131. sum += sum >>16;
  1132. //now subtract from 0xFF
  1133. sum = 0xFFFF - sum;
  1134. //set chksum to calculated value
  1135. info->tcp_hdr->chksum = ntohs(sum);
  1136. return (uint16_t) sum;
  1137. }