relay.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. #include <regex.h>
  5. #include <sys/socket.h>
  6. #include <sys/types.h>
  7. #include <netinet/in.h>
  8. #include <netdb.h>
  9. #include <unistd.h>
  10. #include <pthread.h>
  11. #include <string.h>
  12. #include <openssl/bio.h>
  13. #include <openssl/evp.h>
  14. #include "relay.h"
  15. #include "slitheen.h"
  16. #include "flow.h"
  17. #include "crypto.h"
  18. /** Called when a TLS application record is received for a
  19. * tagged flow. Upstream packets will be checked for covert
  20. * requests to censored sites, downstream packets will be
  21. * replaced with data from the censored queue or with garbage
  22. *
  23. * Inputs:
  24. * f: the tagged flow
  25. * info: the processed received application packet
  26. *
  27. * Output:
  28. * 0 on success, 1 on failure
  29. */
  30. int replace_packet(flow *f, struct packet_info *info){
  31. if (info->tcp_hdr == NULL){
  32. return 0;
  33. }
  34. #ifdef DEBUG
  35. fprintf(stderr,"Flow: %d > %d (%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");
  36. fprintf(stderr,"ID number: %u\n", htonl(info->ip_hdr->id));
  37. fprintf(stderr,"Sequence number: %u\n", htonl(info->tcp_hdr->sequence_num));
  38. fprintf(stderr,"Acknowledgement number: %u\n", htonl(info->tcp_hdr->ack_num));
  39. #endif
  40. if(info->app_data_len <= 0){
  41. return 0;
  42. }
  43. /* if outgoing, decrypt and look at header */
  44. if(info->ip_hdr->src.s_addr == f->src_ip.s_addr){
  45. read_header(f, info);
  46. return 0;
  47. } else {
  48. #ifdef DEBUG
  49. printf("Current sequence number: %d\n", f->seq_num);
  50. printf("Received sequence number: %d\n", htonl(tcp_hdr->sequence_num));
  51. #endif
  52. uint32_t offset = htonl(info->tcp_hdr->sequence_num) - f->seq_num;
  53. if(offset == 0)
  54. f->seq_num += info->app_data_len;
  55. /* if incoming, replace with data from queue */
  56. //if(htonl(tcp_hdr->sequence_num) >= f->seq_num){
  57. process_downstream(f, offset, info);
  58. //}//TODO: need to do something about replaying packets (maybe store previously sent data??
  59. #ifdef DEBUG //TODO: fix
  60. uint8_t *p = (uint8_t *) info->tcp_hdr;
  61. fprintf(stdout, "ip hdr length: %d\n", htons(info->ip_hdr->len));
  62. fprintf(stdout, "Injecting the following packet:\n");
  63. for(int i=0; i< htons(info->ip_hdr->len); i++){
  64. fprintf(stdout, "%02x ", p[i]);
  65. }
  66. fprintf(stdout, "\n");
  67. fflush(stdout);
  68. #endif
  69. }
  70. return 0;
  71. }
  72. /** Reads the HTTP header of upstream data and searches for
  73. * a covert request in an x-slitheen header. Sends this
  74. * request to the indicated site and saves the response to
  75. * the censored queue
  76. *
  77. * TODO: change this to take SOCKS5 proxy connection and then relay data
  78. * based on stream ID
  79. *
  80. * Inputs:
  81. * f: the tagged flow
  82. * info: the processed received packet
  83. *
  84. * Ouput:
  85. * 0 on success, 1 on failure
  86. */
  87. int read_header(flow *f, struct packet_info *info){
  88. uint8_t *p = info->app_data;
  89. if (info->tcp_hdr == NULL){
  90. return 0;
  91. }
  92. struct record_header *record_hdr = (struct record_header*) p;
  93. uint32_t record_length = RECORD_LEN(record_hdr);
  94. uint8_t *decrypted_data = calloc(1, info->app_data_len);
  95. p+= RECORD_HEADER_LEN;
  96. memcpy(decrypted_data, p, record_length);
  97. if(!encrypt(f, decrypted_data, decrypted_data, record_length, 0, record_hdr->type, 0)){
  98. fprintf(stdout,"upstream decryption failed\n");
  99. return 0;
  100. } else {
  101. fprintf(stdout, "upstream decryption succeeded\n");
  102. }
  103. if(record_hdr->type == 0x15){
  104. printf("received alert\n");
  105. for(int i=0; i<record_length; i++){
  106. printf("%02x ", decrypted_data[i]);
  107. }
  108. fflush(stdout);
  109. }
  110. //TODO: re-write this to take a SOCKS connection request
  111. /* search through decrypted data for x-ignore */
  112. char *header_ptr = strstr((const char *) decrypted_data, "X-Slitheen");
  113. uint8_t *upstream_data;
  114. if(header_ptr == NULL){
  115. printf("UPSTREAM: No x-slitheen header found\n");
  116. return 0;
  117. }
  118. printf("UPSTREAM: Found x-slitheen header\n");
  119. fprintf(stdout,"UPSTREAM 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");
  120. fprintf(stdout, "Sequence number: %d\n", info->tcp_hdr->sequence_num);
  121. header_ptr += strlen("X-Slitheen: ");
  122. char *c = header_ptr;
  123. while(*c != '\r'){
  124. c++;
  125. }
  126. c++;
  127. *c = '\0';
  128. printf("upstream data: %s\n", header_ptr);
  129. //b64 decode the data
  130. int32_t decode_len = strlen(header_ptr);
  131. if(header_ptr[decode_len-2] == '='){
  132. decode_len = decode_len*3/4 - 2;
  133. } else if(header_ptr[decode_len-1] == '='){
  134. decode_len = decode_len*3/4 - 1;
  135. } else {
  136. decode_len = decode_len*3/4;
  137. }
  138. upstream_data = calloc(1, decode_len + 1);
  139. BIO *bio, *b64;
  140. bio = BIO_new_mem_buf(header_ptr, -1);
  141. b64 = BIO_new(BIO_f_base64());
  142. bio = BIO_push(b64, bio);
  143. BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
  144. int32_t output_len = BIO_read(bio, upstream_data, strlen(header_ptr));
  145. if(output_len != decode_len)
  146. printf("UH OH, lens dont match\n");
  147. printf("Decoded to get %d bytes:\n", output_len);
  148. for(int i=0; i< output_len; i++){
  149. printf("%02x ", upstream_data[i]);
  150. }
  151. printf("\n");
  152. uint8_t stream_id = upstream_data[0];
  153. p = upstream_data +1;
  154. printf("Received bytes with stream id %d\n", stream_id);
  155. //If a thread for this stream id exists, get the thread info and pipe data
  156. //TODO: fill this in
  157. /*Else, spawn a thread to handle the proxy to this site*/
  158. pthread_t *proxy_thread = calloc(1, sizeof(pthread_t));
  159. int32_t pipefd[2];
  160. if(pipe(pipefd) < 0){
  161. printf("Failed to create pipe for new thread\n");
  162. return 1;
  163. }
  164. struct proxy_thread_data *thread_data =
  165. calloc(1, sizeof(struct proxy_thread_data));
  166. thread_data->initial_data = upstream_data;
  167. thread_data->f = f;
  168. thread_data->pipefd = pipefd[0];
  169. pthread_create(proxy_thread, NULL, proxy_covert_site, (void *) thread_data);
  170. //save a reference to the proxy threads in a global table
  171. /*int32_t bytes_written = write(pipefd[1], message,
  172. strlen( (const char *) message));
  173. if(bytes_written < strlen( (const char *) message)){
  174. printf("failed to write all bytes to pipe\n");
  175. }*/
  176. return 0;
  177. err:
  178. if(upstream_data != NULL){
  179. free(upstream_data);
  180. }
  181. return 1;
  182. }
  183. /** Called by spawned pthreads in read_header to send upstream
  184. * data to the censored site and receive responses. Downstream
  185. * data is stored in the flow's censored_queue. Function and
  186. * thread will terminate when the client closes the connection
  187. * to the covert destination
  188. *
  189. * Input:
  190. * A struct that contains the following information:
  191. * - the tagged flow
  192. * - the initial upstream data (including connect request)
  193. * - the read end of the pipe
  194. *
  195. */
  196. void *proxy_covert_site(void *data){
  197. struct proxy_thread_data *thread_data =
  198. (struct proxy_thread_data *) data;
  199. flow *f = thread_data->f;
  200. uint8_t *p = thread_data->initial_data;
  201. uint8_t stream_id = p[0];
  202. p++;
  203. struct socks_req *clnt_req = (struct socks_req *) p;
  204. p += 4;
  205. //see if it's a connect request
  206. if(clnt_req->cmd != 0x01){
  207. printf("Error: issued a non-connect command\n");
  208. goto err;
  209. }
  210. struct sockaddr_in dest;
  211. dest.sin_family = AF_INET;
  212. uint8_t domain_len;
  213. switch(clnt_req->addr_type){
  214. case 0x01:
  215. //IPv4
  216. dest.sin_addr.s_addr = *((uint32_t*) p);
  217. printf("destination addr: %d\n", ntohl(dest.sin_addr.s_addr));
  218. p += 4;
  219. break;
  220. case 0x03:
  221. //domain name
  222. domain_len = p[0];
  223. p++;
  224. uint8_t *domain_name = calloc(1, domain_len+1);
  225. memcpy(domain_name, p, domain_len);
  226. domain_name[domain_len] = '\0';
  227. struct hostent *host;
  228. host = gethostbyname((const char *) domain_name);
  229. dest.sin_addr = *((struct in_addr *) host->h_addr);
  230. printf("destination addr: %d\n", ntohl(dest.sin_addr.s_addr));
  231. p += domain_len;
  232. free(domain_name);
  233. break;
  234. case 0x04:
  235. //IPv6
  236. goto err;//TODO: fix this
  237. break;
  238. }
  239. //now set the port
  240. dest.sin_port = *((uint16_t *) p);
  241. printf("destination port: %d\n", ntohs(dest.sin_port));
  242. int32_t handle = socket(AF_INET, SOCK_STREAM, 0);
  243. if(handle < 0){
  244. printf("error: constructing socket failed\n");
  245. goto err;
  246. }
  247. struct sockaddr_in my_addr;
  248. socklen_t my_addr_len = sizeof(my_addr);
  249. int32_t error = connect (handle, (struct sockaddr *) &dest, sizeof (struct sockaddr));
  250. if(error <0){
  251. printf("error connecting\n");
  252. goto err;
  253. }
  254. getsockname(handle, (struct sockaddr *) &my_addr, &my_addr_len);
  255. uint8_t *response = calloc(1, 11);
  256. //now send the reply to the client
  257. response[0] = 0x05;
  258. response[1] = 0x00;//TODO: make this accurate
  259. response[2] = 0x00;
  260. response[3] = 0x01;
  261. *((uint32_t *) (response + 4)) = my_addr.sin_addr.s_addr;
  262. *((uint16_t *) (response + 8)) = my_addr.sin_port;
  263. printf("Bound to %x:%d\n", my_addr.sin_addr.s_addr, ntohs(my_addr.sin_port));
  264. printf("Downstream response:\n");
  265. for(int i=0; i< 10; i++){
  266. printf("%02x ", response[i]);
  267. }
  268. printf("\n");
  269. queue_block *new_block = calloc(1, sizeof(queue_block));
  270. printf("Inserting new block into queue: %p\n", new_block);
  271. fflush(stdout);
  272. new_block->len = 10;
  273. new_block->offset = 0;
  274. new_block->data = response;
  275. new_block->next = NULL;
  276. new_block->stream_id = stream_id;
  277. printf("downstream_queue is at %p\n", downstream_queue);
  278. fflush(stdout);
  279. if(downstream_queue->first_block == NULL){
  280. downstream_queue->first_block = new_block;
  281. }
  282. else{
  283. queue_block *last = downstream_queue->first_block;
  284. while(last->next != NULL)
  285. last = last->next;
  286. last->next = new_block;
  287. }
  288. return 0; //slowly test
  289. /*now select on reading from the pipe and from the socket
  290. for(;;){
  291. fd_set readfds;
  292. fd_set writefds;
  293. int32_t nfds = (handle > thread_data->pipefd) ?
  294. handle +1 : thread_data->pipefd + 1;
  295. FD_ZERO(&readfds);
  296. FD_ZERO(&writefds);
  297. FD_SET(thread_data->pipefd, &readfds);
  298. FD_SET(handle, &readfds);
  299. FD_SET(handle, &writefds);
  300. if (select(nfds, &readfds, &writefds, NULL, NULL) < 0){
  301. printf("select error\n");
  302. }
  303. if(FD_ISSET(thread_data->pipefd, &readfds) && FD_ISSET(handle, &writefds)){
  304. //we have upstream data ready for writing
  305. printf("Passing along upstream data\n");
  306. int32_t bytes_read = read(thread_data->pipefd, buffer, buffer_len);
  307. buffer[buffer_len] = '\0';//TODO: remove w/ print
  308. printf("Read from pipe:\n %s\n", buffer);
  309. if(bytes_read > 0){
  310. int32_t bytes_sent = send(handle, buffer,
  311. bytes_read, 0);
  312. if( bytes_sent < 0){
  313. printf("error sending request\n");
  314. break;
  315. } else if (bytes_sent < bytes_read){
  316. //TODO: should update buffer and keep
  317. //track of length of upstream data
  318. printf("sent less than full upstream bytes\n");
  319. break;
  320. }
  321. }
  322. }
  323. if (FD_ISSET(handle, &readfds)){
  324. //we have downstream data read for saving
  325. int32_t bytes_read;
  326. uint8_t *buf = calloc(1, BUFSIZ);
  327. bytes_read = recv(handle, buf, BUFSIZ, 0);
  328. if(bytes_read <= 0){
  329. break;
  330. }
  331. if(bytes_read > 0){
  332. //make a new queue block
  333. new_block = calloc(1, sizeof(queue_block));
  334. new_block->len = bytes_read;
  335. new_block->offset = 0;
  336. new_block->data = buf;
  337. new_block->next = NULL;
  338. if(f->censored_queue == NULL)
  339. f->censored_queue = new_block;
  340. else{
  341. queue_block *last = f->censored_queue;
  342. while(last->next != NULL)
  343. last = last->next;
  344. last->next = new_block;
  345. }
  346. } else {
  347. printf("read 0 bytes\n");
  348. }
  349. }
  350. }
  351. free(thread_data);
  352. free(buffer);
  353. close(handle);
  354. */
  355. return 0;
  356. err:
  357. if(thread_data->initial_data != NULL){
  358. free(thread_data->initial_data);
  359. }
  360. close(handle);
  361. return 0;
  362. }
  363. /** Replaces downstream record contents with data from the
  364. * censored queue, padding with garbage bytes if no more
  365. * censored data exists.
  366. *
  367. * Inputs:
  368. * f: the tagged flow
  369. * data: a pointer to the received packet's application
  370. * data
  371. * data_len: the length of the packet's application data
  372. * offset: if the packet is misordered, the number of
  373. * application-level bytes in missing packets
  374. *
  375. * Output:
  376. * Returns 0 on sucess
  377. */
  378. int process_downstream(flow *f, int32_t offset, struct packet_info *info){
  379. printf("Processing downstream data\n");
  380. uint8_t changed = 0;
  381. uint8_t *p = info->app_data;
  382. uint32_t remaining_packet_len = info->app_data_len;
  383. printf("This packet has %d bytes of application data\n", remaining_packet_len);
  384. printf("Sequence number: %u:%u\n", htonl(info->tcp_hdr->sequence_num), htonl(info->tcp_hdr->sequence_num)+ remaining_packet_len);
  385. fflush(stdout);
  386. if(f->remaining_record_len > 0){
  387. //ignore bytes until the end of the record
  388. if(f->remaining_record_len > remaining_packet_len){ //ignore entire packet
  389. f->remaining_record_len -= remaining_packet_len;
  390. printf("Ignoring %d bytes, %d bytes left to ignore\n", remaining_packet_len, f->remaining_record_len);
  391. remaining_packet_len -= remaining_packet_len;
  392. } else {
  393. p += f->remaining_record_len;
  394. printf("Ignoring %d bytes\n", f->remaining_record_len);
  395. remaining_packet_len -= f->remaining_record_len;
  396. f->remaining_record_len = 0;
  397. }
  398. }
  399. printf("There are now %d bytes remaining\n", remaining_packet_len);
  400. fflush(stdout);
  401. while(remaining_packet_len > 0){ //while bytes remain in the packet
  402. struct record_header *record_hdr = (struct record_header*) p;
  403. uint32_t record_len = RECORD_LEN(record_hdr);
  404. printf("Record length: %d\n", record_len);
  405. fprintf(stdout, "Record:\n");
  406. for(int i=0; i< RECORD_HEADER_LEN; i++){
  407. printf("%02x ", p[i]);
  408. }
  409. printf("\n");
  410. p += RECORD_HEADER_LEN;
  411. remaining_packet_len -= RECORD_HEADER_LEN;
  412. uint8_t *record_ptr = p; //points to the beginning of record data
  413. uint32_t remaining_record_len = record_len;
  414. if(record_len > remaining_packet_len){
  415. //for now, just forfeit this record
  416. printf("Record is too long\n");
  417. f->remaining_record_len = record_len - remaining_packet_len;
  418. remaining_packet_len -= remaining_packet_len;
  419. if(f->httpstate == PARSE_HEADER || f->httpstate == BEGIN_CHUNK || f->httpstate == END_CHUNK){
  420. f->httpstate = FORFEIT_REST;
  421. } else if( f->httpstate == MID_CONTENT || f->httpstate == MID_CHUNK){
  422. f->remaining_response_len -= record_len - 24; //len of IV and padding
  423. printf("remaining response len: %d\n", f->remaining_response_len);
  424. if(f->remaining_response_len == 0){
  425. if(f->httpstate == MID_CHUNK)
  426. f->httpstate = END_CHUNK;
  427. else
  428. f->httpstate = END_BODY;
  429. }
  430. if(f->remaining_response_len < 0){
  431. f->remaining_response_len = 0;
  432. f->httpstate = FORFEIT_REST;
  433. }
  434. }
  435. break;
  436. }
  437. //now decrypt the record
  438. printf("Decrypting record\n");
  439. int32_t n = encrypt(f, record_ptr, record_ptr, record_len, 1,
  440. record_hdr->type, 0);
  441. if(n < 0){
  442. //do something smarter here
  443. fprintf(stdout,"application decryption failed\n");
  444. return 0;
  445. }
  446. changed = 1;
  447. printf("Decryption succeeded\n");
  448. printf("Bytes:\n");
  449. for(int i=0; i< n; i++){
  450. printf("%02x ", record_ptr[EVP_GCM_TLS_EXPLICIT_IV_LEN+i]);
  451. }
  452. printf("\n");
  453. printf("Text:\n");
  454. printf("%s\n", record_ptr+EVP_GCM_TLS_EXPLICIT_IV_LEN);
  455. p += EVP_GCM_TLS_EXPLICIT_IV_LEN;
  456. char *len_ptr, *needle;
  457. remaining_record_len = n;
  458. while(remaining_record_len > 0){
  459. switch(f->httpstate){
  460. case PARSE_HEADER:
  461. //determine whether it's transfer encoded or otherwise
  462. len_ptr = strstr((const char *) p, "Transfer-Encoding");
  463. if(len_ptr != NULL){
  464. if(!memcmp(len_ptr + 19, "chunked", 7)){
  465. printf("SLITHEEN: transfer chunked\n");
  466. //now find end of header
  467. len_ptr = strstr((const char *) p, "\r\n\r\n");
  468. if(len_ptr != NULL){
  469. f->httpstate = BEGIN_CHUNK;
  470. remaining_record_len -= (((uint8_t *)len_ptr - p) + 4);
  471. p = (uint8_t *) len_ptr + 4;
  472. printf("remaining record len: %d\n", remaining_record_len);
  473. }
  474. }
  475. } else {
  476. len_ptr = strstr((const char *) p, "Content-Length");
  477. if(len_ptr != NULL){
  478. len_ptr += 15;
  479. f->remaining_response_len = strtol((const char *) len_ptr, NULL, 10);
  480. printf("SLITHEEN: Content-Length: %d\n", f->remaining_response_len);
  481. len_ptr = strstr((const char *) p, "\r\n\r\n");
  482. if(len_ptr != NULL){
  483. f->httpstate = MID_CONTENT;
  484. remaining_record_len -= (((uint8_t *)len_ptr - p) + 4);
  485. p = (uint8_t *) len_ptr + 4;
  486. printf("remaining record len: %d\n", remaining_record_len);
  487. } else {
  488. printf("UH OH\n");
  489. remaining_record_len = 0;
  490. f->httpstate = FORFEIT_REST;
  491. }
  492. } else {
  493. f->httpstate = FORFEIT_REST;
  494. remaining_record_len = 0;
  495. }
  496. }
  497. //figure out what the content-type is
  498. len_ptr = strstr((const char *) record_ptr, "Content-Type: image");
  499. if(len_ptr != NULL){
  500. printf("SLITHEEN: replaceable content\n");
  501. f->replace_response = 1;
  502. memcpy(len_ptr + 14, "slitheen", 8);
  503. char *c = len_ptr + 14+8;
  504. while(c[0] != '\r'){
  505. c[0] = ' ';
  506. c++;
  507. }
  508. } else {
  509. printf("SLITHEEN: non-replaceable content\n");
  510. f->replace_response = 0;
  511. }
  512. break;
  513. case MID_CONTENT:
  514. //check if content is replaceable
  515. printf("In MID_CONTENT\n");
  516. if(f->remaining_response_len > remaining_record_len){
  517. if(f->replace_response){
  518. fill_with_downstream(f, p, remaining_record_len);
  519. printf("Replaced with:\n");
  520. for(int i=0; i< remaining_record_len; i++){
  521. printf("%02x ", p[i]);
  522. }
  523. printf("\n");
  524. }
  525. f->remaining_response_len -= remaining_record_len;
  526. p += remaining_record_len;
  527. remaining_record_len = 0;
  528. printf("%d bytes left of body\n", f->remaining_response_len);
  529. } else {
  530. if(f->replace_response){
  531. fill_with_downstream(f, p, remaining_record_len);
  532. printf("Replaced with:\n");
  533. for(int i=0; i< remaining_record_len; i++){
  534. printf("%02x ", p[i]);
  535. }
  536. printf("\n");
  537. }
  538. remaining_record_len -= f->remaining_response_len;
  539. p += f->remaining_response_len;
  540. f->httpstate = PARSE_HEADER;
  541. f->remaining_response_len = 0;
  542. printf("rem. response len = %d\n", f->remaining_response_len);
  543. }
  544. break;
  545. case BEGIN_CHUNK:
  546. printf("In BEGIN_CHUNK\n");
  547. int32_t chunk_size = strtol((const char *) p, NULL, 16);
  548. printf("chunk size: %x\n", chunk_size);
  549. if(chunk_size == 0){
  550. f->httpstate = END_BODY;
  551. } else {
  552. f->httpstate = MID_CHUNK;
  553. }
  554. f->remaining_response_len = chunk_size;
  555. needle = strstr((const char *) p, "\r\n");
  556. if(needle != NULL){
  557. remaining_record_len -= ((uint8_t *) needle - p + 2);
  558. p = (uint8_t *) needle + 2;
  559. printf("remaining_record len = %d\n", remaining_record_len);
  560. } else {
  561. printf("UH OH\n");
  562. remaining_record_len = 0;
  563. f->httpstate = FORFEIT_REST;
  564. }
  565. break;
  566. case MID_CHUNK:
  567. printf("In MID_CHUNK\n");
  568. if(f->remaining_response_len > remaining_record_len){
  569. if(f->replace_response){
  570. fill_with_downstream(f, p, remaining_record_len);
  571. printf("Replaced with:\n");
  572. for(int i=0; i< remaining_record_len; i++){
  573. printf("%02x ", p[i]);
  574. }
  575. printf("\n");
  576. }
  577. f->remaining_response_len -= remaining_record_len;
  578. p += remaining_record_len;
  579. remaining_record_len = 0;
  580. printf("%d bytes left of chunk\n", f->remaining_response_len);
  581. } else {
  582. if(f->replace_response){
  583. fill_with_downstream(f, p, remaining_record_len);
  584. printf("Replaced with:\n");
  585. for(int i=0; i< remaining_record_len; i++){
  586. printf("%02x ", p[i]);
  587. }
  588. printf("\n");
  589. }
  590. remaining_record_len -= f->remaining_response_len;
  591. p += f->remaining_response_len;
  592. f->httpstate = END_CHUNK;
  593. printf("rem. record len = %d\n", f->remaining_record_len);
  594. }
  595. break;
  596. case END_CHUNK:
  597. printf("In END_CHUNK\n");
  598. needle = strstr((const char *) p, "\r\n");
  599. if(needle != NULL){
  600. f->httpstate = BEGIN_CHUNK;
  601. p += 2;
  602. remaining_record_len -= 2;
  603. } else {
  604. printf("UH OH\n");
  605. remaining_record_len = 0;
  606. f->httpstate = FORFEIT_REST;
  607. }
  608. break;
  609. case END_BODY:
  610. printf("In END_BODY\n");
  611. needle = strstr((const char *) p, "\r\n");
  612. if(needle != NULL){
  613. f->httpstate = PARSE_HEADER;
  614. p += 2;
  615. remaining_record_len -= 2;
  616. } else {
  617. printf("UH OH\n");
  618. remaining_record_len = 0;
  619. f->httpstate = FORFEIT_REST;
  620. }
  621. break;
  622. case FORFEIT_REST:
  623. printf("In FORFEIT_REST\n");
  624. case USE_REST:
  625. remaining_record_len = 0;
  626. break;
  627. default:
  628. break;
  629. }
  630. }
  631. /*
  632. if(f->remaining_response_len > 0){
  633. if(f->ignore_response){
  634. uint32_t skip_len = (f->remaining_response_len > remaining_record_len) ?
  635. remaining_record_len : f->remaining_response_len;
  636. p += skip_len;
  637. remaining_record_len -= skip_len;
  638. f->remaining_response_len -= skip_len;
  639. } else {//replace with downstream data, for now ignore
  640. uint32_t skip_len = (f->remaining_response_len > remaining_record_len) ?
  641. remaining_record_len : f->remaining_response_len;
  642. p += skip_len;
  643. remaining_record_len -= skip_len;
  644. f->remaining_response_len -= skip_len;
  645. }
  646. }
  647. //cycle through all responses
  648. while(remaining_record_len > 0){
  649. uint32_t response_len = get_response_length(p);
  650. response_len = remaining_record_len;
  651. remaining_record_len -= response_len;
  652. p += response_len;
  653. }
  654. */
  655. printf("Re-encrypting record\n");
  656. if((n = encrypt(f, record_ptr, record_ptr,
  657. n + EVP_GCM_TLS_EXPLICIT_IV_LEN, 1, record_hdr->type,
  658. 1)) < 0){
  659. fprintf(stdout,"encryption failed\n");
  660. return 0;
  661. }
  662. printf("Encryption successful!\n");
  663. p = record_ptr + record_len;
  664. remaining_packet_len -= record_len;
  665. }
  666. /*
  667. uint8_t *p = info->app_data;
  668. int32_t tmp_len = info->app_data_len;
  669. printf("remaining packet length: %d\n", tmp_len);
  670. //step 1: replace record contents
  671. //note: encrypted message will be original message size + EVP_GCM_TLS_EXPLICIT_IV_LEN + 16 byte pad
  672. //first check to see if there's anything in the outbox
  673. if(f->outbox_len > 0){
  674. #ifdef DEBUG
  675. if(f->outbox_len < info->app_data_len){
  676. printf("Next record:\n");
  677. for(int i=0; i< RECORD_HEADER_LEN; i++){
  678. printf("%02x ", p[f->outbox_len+i]);
  679. }
  680. printf("\n");
  681. } else {
  682. printf("Outbox takes up entire packet\n");
  683. }
  684. #endif
  685. //If the outbox contains data we can't change, just forward it as is.
  686. if(f->outbox_data_len > 0){
  687. if(tmp_len >= f->outbox_data_len){
  688. printf("There are %d bytes in the outbox\n", f->outbox_data_len);
  689. memcpy(p, f->outbox, f->outbox_data_len);
  690. p += f->outbox_data_len;
  691. tmp_len -= f->outbox_data_len;
  692. f->outbox_len -= f->outbox_data_len;
  693. f->outbox_data_len = 0;
  694. free(f->outbox);
  695. //now see if there are remainders of the encrypted record to send...
  696. if(tmp_len > 0){
  697. if(tmp_len >= f->outbox_len){
  698. p += f->outbox_len;
  699. tmp_len -= f->outbox_len;
  700. f->outbox_len = 0;
  701. } else {
  702. f->outbox_len -= tmp_len;
  703. tmp_len -= tmp_len;
  704. }
  705. }
  706. } else {
  707. memcpy(p, f->outbox, tmp_len);
  708. uint8_t *tmp = calloc(1, f->outbox_data_len - tmp_len);
  709. f->outbox_data_len -= tmp_len;
  710. f->outbox_len -= tmp_len;
  711. memcpy(tmp, f->outbox + tmp_len, f->outbox_data_len);
  712. free(f->outbox);
  713. f->outbox = tmp;
  714. tmp_len -= tmp_len;
  715. }
  716. } else {
  717. //all we have are ingored bytes
  718. if(tmp_len >= f->outbox_len){
  719. printf("ignoring %d bytes\n", f->outbox_len);
  720. p += f->outbox_len;
  721. tmp_len -= f->outbox_len;
  722. f->outbox_len = 0;
  723. } else {
  724. printf("ignoring %d bytes\n", tmp_len);
  725. f->outbox_len -= tmp_len;
  726. tmp_len -= tmp_len;
  727. }
  728. }
  729. }
  730. while(tmp_len > 0){
  731. printf("remaining packet length: %d\n", tmp_len);
  732. struct record_header *record_hdr = (struct record_header*) p;
  733. uint32_t record_length = RECORD_LEN(record_hdr);
  734. printf("record length: %d\n", record_length);
  735. #ifdef DEBUG
  736. fprintf(stdout, "Record:\n");
  737. for(int i=0; i< RECORD_HEADER_LEN; i++){
  738. printf("%02x ", p[i]);
  739. }
  740. printf("\n");
  741. #endif
  742. p += RECORD_HEADER_LEN;
  743. if(record_length > tmp_len){
  744. //the packet is smaller than the record, we'll just forfeit the data
  745. printf("Cannot see all of record\n");
  746. tmp_len -= RECORD_HEADER_LEN;
  747. f->outbox_len = record_length - tmp_len;
  748. f->outbox_data_len = 0;
  749. printf("ignoring %d bytes\n", tmp_len);
  750. break;
  751. }
  752. if(record_hdr->type != 0x17){
  753. //TODO: might need to decrypt and re-encrypt
  754. //printf("received non-application data\n");
  755. tmp_len -= (record_length+ RECORD_HEADER_LEN);
  756. p += record_length;
  757. continue;
  758. }
  759. //We are at a record boundary: decrypt record and change "leaves"
  760. if(!encrypt(f, p, p, record_length, 1, record_hdr->type, 0)){
  761. fprintf(stdout,"decryption failed\n");
  762. return 0;
  763. }
  764. printf("encryption successful\n");
  765. //look for content type
  766. const char *needle = "Content-Type";
  767. const char *type = strstr((const char *) p, needle);
  768. if(type == NULL){
  769. printf("No content-type header\n");
  770. tmp_len -= RECORD_HEADER_LEN + record_length;
  771. p += record_length;
  772. continue;
  773. }
  774. uint8_t *substr = calloc(1, 32);
  775. memcpy(substr, type, 32);
  776. if(strstr((const char *) substr, "img") == NULL){
  777. //we're ignoring this
  778. printf("This is not a leaf node\n");
  779. tmp_len -= RECORD_HEADER_LEN + record_length;
  780. p += record_length;
  781. free(substr);
  782. continue;
  783. }
  784. free(substr);
  785. if(type != NULL){
  786. while((*type != '\r') && (*type != ';')){
  787. printf("%c", *type);
  788. type ++;
  789. }
  790. printf("\n");
  791. } else {
  792. printf("No content-type header\n");
  793. }
  794. uint8_t *new_record = calloc(1, record_length);
  795. memcpy(new_record, p, record_length);
  796. uint8_t *tmp_p = new_record;
  797. tmp_p += EVP_GCM_TLS_EXPLICIT_IV_LEN;
  798. struct slitheen_header *sl_hdr = (struct slitheen_header *) tmp_p;
  799. sl_hdr->marker = 0x01;
  800. sl_hdr->version = 0x01;
  801. sl_hdr->len = 0x00;
  802. int32_t remaining = record_length - (SLITHEEN_HEADER_LEN
  803. + EVP_GCM_TLS_EXPLICIT_IV_LEN + 16);
  804. tmp_p += SLITHEEN_HEADER_LEN;
  805. //Fill as much as we can from the censored_queue
  806. while((remaining > 0) && f->censored_queue != NULL){
  807. int32_t block_length = f->censored_queue->len;
  808. int32_t offset = f->censored_queue->offset;
  809. #ifdef DEBUG
  810. printf("Censored queue is at %p.\n", f->censored_queue);
  811. printf("This block has %d bytes left\n", block_length - offset);
  812. printf("We need %d bytes\n", remaining);
  813. #endif
  814. if(block_length > offset + remaining){
  815. //use part of the block, update offset
  816. memcpy(tmp_p, f->censored_queue->data+offset, remaining);
  817. f->censored_queue->offset += remaining;
  818. tmp_p += remaining;
  819. sl_hdr->len += remaining;
  820. remaining -= remaining;
  821. } else {
  822. //use all of the block and free it
  823. memcpy(tmp_p, f->censored_queue->data+offset, block_length - offset);
  824. free(f->censored_queue->data);
  825. f->censored_queue = f->censored_queue->next;
  826. tmp_p += (block_length - offset);
  827. sl_hdr->len += (block_length - offset);
  828. remaining -= (block_length - offset);
  829. }
  830. }
  831. sl_hdr->len = htons(sl_hdr->len);
  832. //now, if we need more data, fill with garbage
  833. if(remaining >0 ){
  834. //TODO: note, we may also be receiving misordered packets. Take Ian's suggestion into account here
  835. memset(tmp_p, 'A', remaining);
  836. }
  837. tmp_p = new_record;
  838. #ifdef DEBUG
  839. fprintf(stdout, "copied %d data and %d garbage bytes\n", ntohs(sl_hdr->len), remaining);
  840. printf("Slitheen header\n");
  841. for(int i=0; i<4; i++)
  842. printf("%02x ", tmp_p[EVP_GCM_TLS_EXPLICIT_IV_LEN+i]);
  843. printf("\n");
  844. #endif
  845. reencrypt:
  846. //step 3: encrypt new record
  847. int32_t success;
  848. if((success = encrypt(f, tmp_p, tmp_p, record_length-16, 1, 0x17, 1))< 0){
  849. fprintf(stdout,"encryption failed\n");
  850. return 0;
  851. }
  852. //copy new record into packet
  853. if(record_length +RECORD_HEADER_LEN > tmp_len){
  854. //We have a partial record
  855. memcpy(p, new_record, tmp_len - RECORD_HEADER_LEN);
  856. f->outbox_data_len = record_length - (tmp_len - RECORD_HEADER_LEN);
  857. //save left-overs in outbox
  858. f->outbox = calloc(1, f->outbox_data_len);
  859. memcpy(f->outbox, new_record + (tmp_len - RECORD_HEADER_LEN),
  860. f->outbox_data_len);
  861. free(new_record);
  862. f->outbox_len = f->outbox_data_len;
  863. } else {
  864. memcpy(p, new_record, record_length);
  865. free(new_record);
  866. }
  867. #ifdef DEBUG
  868. //check to see if next record still exists
  869. if(info->app_data_len > record_length + RECORD_HEADER_LEN){
  870. printf("Next record:\n");
  871. for(int i=0; i< RECORD_HEADER_LEN; i++){
  872. printf("%02x ", p[record_length+i]);
  873. }
  874. printf("\n");
  875. } else {
  876. printf("No extra record: %d <= %d + %d\n", data_len, record_length, RECORD_HEADER_LEN);
  877. }
  878. #endif
  879. tmp_len -= record_length+ RECORD_HEADER_LEN;
  880. p += record_length;
  881. }
  882. */
  883. //step 4: recompute TCP checksum
  884. if(changed){
  885. tcp_checksum(info);
  886. printf("Computing checksum\n");
  887. fflush(stdout);
  888. }
  889. return 0;
  890. }
  891. int fill_with_downstream(flow *f, uint8_t *data, int32_t length){
  892. uint8_t *p = data;
  893. int32_t remaining = length;
  894. struct slitheen_header *sl_hdr;
  895. //Fill as much as we can from the censored_queue
  896. while((remaining > SLITHEEN_HEADER_LEN) && downstream_queue->first_block != NULL){
  897. queue_block *first_block = downstream_queue->first_block;
  898. int32_t block_length = first_block->len;
  899. int32_t offset = first_block->offset;
  900. //#ifdef DEBUG
  901. printf("Censored queue is at %p.\n", first_block);
  902. printf("This block has %d bytes left\n", block_length - offset);
  903. printf("We need %d bytes\n", remaining);
  904. //#endif
  905. sl_hdr = (struct slitheen_header *) p;
  906. sl_hdr->stream_id = first_block->stream_id;
  907. sl_hdr->len = 0x00;
  908. sl_hdr->garbage = 0x00;
  909. p += SLITHEEN_HEADER_LEN;
  910. remaining -= SLITHEEN_HEADER_LEN;
  911. if(block_length > offset + remaining){
  912. //use part of the block, update offset
  913. memcpy(p, first_block->data+offset, remaining);
  914. first_block->offset += remaining;
  915. p += remaining;
  916. sl_hdr->len = remaining;
  917. remaining -= remaining;
  918. } else {
  919. //use all of the block and free it
  920. memcpy(p, first_block->data+offset, block_length - offset);
  921. free(first_block->data);
  922. downstream_queue->first_block = first_block->next;
  923. free(first_block);
  924. p += (block_length - offset);
  925. sl_hdr->len = (block_length - offset);
  926. remaining -= (block_length - offset);
  927. }
  928. sl_hdr->len = htons(sl_hdr->len);
  929. }
  930. //now, if we need more data, fill with garbage
  931. if(remaining > SLITHEEN_HEADER_LEN ){
  932. //TODO: note, we may also be receiving misordered packets. Take Ian's suggestion into account here
  933. sl_hdr = (struct slitheen_header *) p;
  934. sl_hdr->stream_id = 0x00;
  935. remaining -= SLITHEEN_HEADER_LEN;
  936. sl_hdr->len = remaining;
  937. sl_hdr->garbage = remaining;
  938. p += SLITHEEN_HEADER_LEN;
  939. memset(p, 'A', remaining);
  940. }
  941. return 0;
  942. }
  943. /** Computes the TCP checksum of the data according to RFC 793
  944. * sum all 16-bit words in the segment, padd the last word if
  945. * needed
  946. *
  947. * there is a pseudo-header prefixed to the segment and
  948. * included in the checksum:
  949. *
  950. * +--------+--------+--------+--------+
  951. * | Source Address |
  952. * +--------+--------+--------+--------+
  953. * | Destination Address |
  954. * +--------+--------+--------+--------+
  955. * | zero | PTCL | TCP Length |
  956. * +--------+--------+--------+--------+
  957. */
  958. uint16_t tcp_checksum(struct packet_info *info){
  959. uint16_t tcp_length = info->app_data_len + info->size_tcp_hdr;
  960. struct in_addr src = info->ip_hdr->src;
  961. struct in_addr dst = info->ip_hdr->dst;
  962. uint8_t proto = IPPROTO_TCP;
  963. //set the checksum to zero
  964. info->tcp_hdr->chksum = 0;
  965. //sum pseudoheader
  966. uint32_t sum = (ntohl(src.s_addr)) >> 16;
  967. sum += (ntohl(src.s_addr)) &0xFFFF;
  968. sum += (ntohl(dst.s_addr)) >> 16;
  969. sum += (ntohl(dst.s_addr)) & 0xFFFF;
  970. sum += proto;
  971. sum += tcp_length;
  972. //sum tcp header (with zero-d checksum)
  973. uint8_t *p = (uint8_t *) info->tcp_hdr;
  974. for(int i=0; i < info->size_tcp_hdr; i+=2){
  975. sum += (uint16_t) ((p[i] << 8) + p[i+1]);
  976. }
  977. //now sum the application data
  978. p = info->app_data;
  979. for(int i=0; i< info->app_data_len-1; i+=2){
  980. sum += (uint16_t) ((p[i] << 8) + p[i+1]);
  981. }
  982. if(info->app_data_len %2 != 0){
  983. sum += (uint16_t) (p[info->app_data_len - 1]) << 8;
  984. }
  985. //now add most significant to last significant bits
  986. sum = (sum >> 16) + (sum & 0xFFFF);
  987. //now subtract from 0xFF
  988. sum = 0xFFFF - sum;
  989. //set chksum to calculated value
  990. info->tcp_hdr->chksum = ntohs(sum);
  991. return (uint16_t) sum;
  992. }