relay.c 37 KB

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