relay.c 37 KB

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