relay.c 37 KB

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