relay.c 37 KB

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