relay.c 39 KB

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