relay.c 38 KB

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