relay.c 43 KB

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