relay.c 33 KB

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