relay.c 40 KB

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