relay.c 40 KB

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