relay.c 53 KB

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