relay.c 37 KB

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