relay.c 54 KB

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