relay.c 55 KB

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