relay.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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 "relay.h"
  11. #include "slitheen.h"
  12. #include "flow.h"
  13. #include "crypto.h"
  14. int replace_packet(flow *f, struct packet_info *info){
  15. if (info->tcp_hdr == NULL){
  16. return 0;
  17. }
  18. #ifdef DEBUG
  19. 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");
  20. fprintf(stderr,"ID number: %u\n", htonl(info->ip_hdr->id));
  21. fprintf(stderr,"Sequence number: %u\n", htonl(info->tcp_hdr->sequence_num));
  22. fprintf(stderr,"Acknowledgement number: %u\n", htonl(info->tcp_hdr->ack_num));
  23. #endif
  24. if(info->app_data_len <= 0){
  25. return 0;
  26. }
  27. /* if outgoing, decrypt and look at header */
  28. if(info->ip_hdr->src.s_addr == f->src_ip.s_addr){
  29. read_header(f, info);
  30. return 0;
  31. } else {
  32. #ifdef DEBUG
  33. printf("Current sequence number: %d\n", f->seq_num);
  34. printf("Received sequence number: %d\n", htonl(tcp_hdr->sequence_num));
  35. #endif
  36. uint32_t offset = htonl(info->tcp_hdr->sequence_num) - f->seq_num;
  37. if(offset == 0)
  38. f->seq_num += info->app_data_len;
  39. /* if incoming, replace with data from queue */
  40. //if(htonl(tcp_hdr->sequence_num) >= f->seq_num){
  41. replace_contents(f, offset, info);
  42. //}//TODO: need to do something about replaying packets (maybe store previously sent data??
  43. #ifdef DEBUG //TODO: fix
  44. uint8_t *p = (uint8_t *) info->tcp_hdr;
  45. fprintf(stdout, "ip hdr length: %d\n", htons(info->ip_hdr->len));
  46. fprintf(stdout, "Injecting the following packet:\n");
  47. for(int i=0; i< htons(info->ip_hdr->len); i++){
  48. fprintf(stdout, "%02x ", p[i]);
  49. }
  50. fprintf(stdout, "\n");
  51. fflush(stdout);
  52. #endif
  53. }
  54. return 0;
  55. }
  56. int read_header(flow *f, struct packet_info *info){
  57. uint8_t *p = info->app_data;
  58. if (info->tcp_hdr == NULL){
  59. return 0;
  60. }
  61. struct record_header *record_hdr = (struct record_header*) p;
  62. uint32_t record_length = RECORD_LEN(record_hdr);
  63. uint8_t *decrypted_data = calloc(1, info->app_data_len);
  64. p+= RECORD_HEADER_LEN;
  65. memcpy(decrypted_data, p, record_length);
  66. if(!encrypt(f, decrypted_data, decrypted_data, record_length, 0, record_hdr->type, 0)){
  67. fprintf(stdout,"decryption failed\n");
  68. return 0;
  69. }
  70. if(record_hdr->type == 0x15){
  71. printf("received alert\n");
  72. for(int i=0; i<record_length; i++){
  73. printf("%02x ", decrypted_data[i]);
  74. }
  75. fflush(stdout);
  76. }
  77. /* search through decrypted data for x-ignore */
  78. regex_t r;
  79. const char *regex_text = "x-ignore";
  80. const char *match = (char *) decrypted_data;
  81. if(regcomp(&r, regex_text, REG_EXTENDED|REG_NEWLINE)){
  82. printf("could not compile regex\n");
  83. return 0;
  84. }
  85. regmatch_t m;
  86. if(regexec(&r, match, 1, &m, 0)){
  87. return 0;
  88. }
  89. uint8_t *message = decrypted_data;
  90. //remove escape characters
  91. for(int i=m.rm_eo+2; i< strlen(match); i++){
  92. if(match[i] != '\\'){
  93. *(message++) = match[i];
  94. } else if (match[i+1] == 'r') {
  95. *(message++) = '\r';
  96. i++;
  97. } else if (match[i+1] == 'n') {
  98. *(message++) = '\n';
  99. i++;
  100. }
  101. }
  102. *message = '\0';
  103. message = decrypted_data;
  104. regex_t r2;
  105. const char *regex_text2 = "host";
  106. const char *match2 = (char *) decrypted_data;
  107. regmatch_t m2;
  108. if(regcomp(&r2, regex_text2, REG_EXTENDED|REG_NEWLINE)){
  109. printf("could not compile regex\n");
  110. return 0;
  111. }
  112. if(regexec(&r2, match2, 1, &m2, 0)){
  113. printf("no host found\n");
  114. return 0;
  115. }
  116. uint8_t server[50];
  117. for(int i=m2.rm_eo+2; i< strlen(match2); i++){
  118. if(match2[i] != '\n'){
  119. server[i-m2.rm_eo-2] = match2[i];
  120. } else {
  121. server[i-m2.rm_eo-2] = '\0';
  122. break;
  123. }
  124. }
  125. /* Send GET request to site */
  126. printf("sending request to site: %s\n", server);
  127. struct hostent *host;
  128. host = gethostbyname((const char *) server);
  129. if(host == NULL){
  130. printf("gethostbyname failed\n");
  131. return 0;
  132. }
  133. int32_t handle = socket(AF_INET, SOCK_STREAM, 0);
  134. if(handle < 0){
  135. printf("error: constructing socket failed\n");
  136. return 0;
  137. }
  138. struct sockaddr_in dest;
  139. dest.sin_family = AF_INET;
  140. dest.sin_port = htons(80);
  141. dest.sin_addr = *((struct in_addr *) host->h_addr);
  142. bzero (&(dest.sin_zero), 8);
  143. int32_t error = connect (handle, (struct sockaddr *) &dest, sizeof (struct sockaddr));
  144. if(error <0){
  145. printf("error connecting\n");
  146. return 0;
  147. }
  148. int32_t bytes_sent = send(handle, message, strlen((const char *) message), 0);
  149. if( bytes_sent < 0){
  150. printf("error sending request\n");
  151. close(handle);
  152. return 0;
  153. } else if (bytes_sent < strlen((const char *) message)){
  154. close(handle);
  155. return 0;
  156. }
  157. int32_t bytes_read;
  158. for(int i=0; i<3; i++){
  159. uint8_t *buf = calloc(1, BUFSIZ);
  160. bytes_read = recv(handle, buf, BUFSIZ, 0);
  161. if(bytes_read <= 0) break;
  162. //make a new queue block
  163. queue_block *new_block = calloc(1, sizeof(queue_block));
  164. new_block->len = bytes_read;
  165. new_block->offset = 0;
  166. new_block->data = buf;
  167. new_block->next = NULL;
  168. if(f->censored_queue == NULL)
  169. f->censored_queue = new_block;
  170. else{
  171. queue_block *last = f->censored_queue;
  172. while(last->next != NULL)
  173. last = last->next;
  174. last->next = new_block;
  175. }
  176. }
  177. close(handle);
  178. return 0;
  179. }
  180. /** Replaces downstream record contents with data from the
  181. * censored queue, padding with garbage bytes if no more
  182. * censored data exists.
  183. *
  184. * Inputs:
  185. * f: the tagged flow
  186. * data: a pointer to the received packet's application
  187. * data
  188. * data_len: the length of the packet's application data
  189. * offset: if the packet is misordered, the number of
  190. * application-level bytes in missing packets
  191. *
  192. * Output:
  193. * Returns 0 on sucess
  194. */
  195. int replace_contents(flow *f, int32_t offset, struct packet_info *info){
  196. uint8_t *p = info->app_data;
  197. int32_t tmp_len = info->app_data_len;
  198. //step 1: replace record contents
  199. //note: encrypted message will be original message size + EVP_GCM_TLS_EXPLICIT_IV_LEN + 16 byte pad
  200. //first check to see if there's anything in the outbox
  201. if(f->outbox_len > 0){
  202. #ifdef DEBUG
  203. if(f->outbox_len < info->app_data_len){
  204. printf("Next record:\n");
  205. for(int i=0; i< RECORD_HEADER_LEN; i++){
  206. printf("%02x ", p[f->outbox_len+i]);
  207. }
  208. printf("\n");
  209. } else {
  210. printf("Outbox takes up entire packet\n");
  211. }
  212. #endif
  213. if(tmp_len >= f->outbox_len){
  214. memcpy(p, f->outbox, f->outbox_len);
  215. p += f->outbox_len;
  216. tmp_len -= f->outbox_len;
  217. f->outbox_len = 0;
  218. free(f->outbox);
  219. } else {
  220. memcpy(p, f->outbox, tmp_len);
  221. uint8_t *tmp = calloc(1, f->outbox_len - tmp_len);
  222. f->outbox_len -= tmp_len;
  223. memcpy(tmp, f->outbox + tmp_len, f->outbox_len);
  224. free(f->outbox);
  225. f->outbox = tmp;
  226. tmp_len -= tmp_len;
  227. }
  228. }
  229. while(tmp_len > 0){
  230. struct record_header *record_hdr = (struct record_header*) p;
  231. uint32_t record_length = RECORD_LEN(record_hdr);
  232. #ifdef DEBUG
  233. fprintf(stdout, "Record:\n");
  234. for(int i=0; i< RECORD_HEADER_LEN; i++){
  235. printf("%02x ", p[i]);
  236. }
  237. printf("\n");
  238. #endif
  239. p += RECORD_HEADER_LEN;
  240. if(record_hdr->type != 0x17){
  241. //TODO: might need to decrypt and re-encrypt
  242. printf("received non-application data\n");
  243. tmp_len -= (record_length+ RECORD_HEADER_LEN);
  244. p += record_length;
  245. continue;
  246. }
  247. uint8_t *new_record = calloc(1, record_length);
  248. memcpy(new_record, p, record_length);
  249. uint8_t *tmp_p = new_record;
  250. tmp_p += EVP_GCM_TLS_EXPLICIT_IV_LEN;
  251. struct slitheen_header *sl_hdr = (struct slitheen_header *) tmp_p;
  252. sl_hdr->marker = 0x01;
  253. sl_hdr->version = 0x01;
  254. sl_hdr->len = 0x00;
  255. int32_t remaining = record_length - (SLITHEEN_HEADER_LEN
  256. + EVP_GCM_TLS_EXPLICIT_IV_LEN + 16);
  257. tmp_p += SLITHEEN_HEADER_LEN;
  258. //Fill as much as we can from the censored_queue
  259. while((remaining > 0) && f->censored_queue != NULL){
  260. int32_t block_length = f->censored_queue->len;
  261. int32_t offset = f->censored_queue->offset;
  262. #ifdef DEBUG
  263. printf("Censored queue is at %p.\n", f->censored_queue);
  264. printf("This block as %d bytes left\n", block_length - offset);
  265. printf("We need %d bytes\n", remaining);
  266. #endif
  267. if(block_length > offset + remaining){
  268. //use part of the block, update offset
  269. memcpy(tmp_p, f->censored_queue->data+offset, remaining);
  270. f->censored_queue->offset += remaining;
  271. tmp_p += remaining;
  272. sl_hdr->len += remaining;
  273. remaining -= remaining;
  274. } else {
  275. //use all of the block and free it
  276. memcpy(tmp_p, f->censored_queue->data+offset, block_length - offset);
  277. free(f->censored_queue->data);
  278. f->censored_queue = f->censored_queue->next;
  279. tmp_p += (block_length - offset);
  280. sl_hdr->len += (block_length - offset);
  281. remaining -= (block_length - offset);
  282. }
  283. }
  284. sl_hdr->len = htons(sl_hdr->len);
  285. //now, if we need more data, fill with garbage
  286. if(remaining >0 ){
  287. //TODO: note, we may also be receiving misordered packets. Take Ian's suggestion into account here
  288. memset(tmp_p, 'A', remaining);
  289. }
  290. tmp_p = new_record;
  291. #ifdef DEBUG
  292. fprintf(stdout, "copied %d data and %d garbage bytes\n", ntohs(sl_hdr->len), remaining);
  293. printf("Slitheen header\n");
  294. for(int i=0; i<4; i++)
  295. printf("%02x ", tmp_p[EVP_GCM_TLS_EXPLICIT_IV_LEN+i]);
  296. printf("\n");
  297. #endif
  298. //step 3: encrypt new record
  299. int32_t success;
  300. if((success = encrypt(f, tmp_p, tmp_p, record_length-16, 1, 0x17, 1))< 0){
  301. fprintf(stdout,"encryption failed\n");
  302. return 0;
  303. }
  304. //copy new record into packet
  305. if(record_length +RECORD_HEADER_LEN > tmp_len){
  306. //We have a partial record
  307. memcpy(p, new_record, tmp_len - RECORD_HEADER_LEN);
  308. f->outbox_len = record_length - (tmp_len - RECORD_HEADER_LEN);
  309. //save left-overs in outbox
  310. f->outbox = calloc(1, f->outbox_len);
  311. memcpy(f->outbox, new_record + (tmp_len - RECORD_HEADER_LEN),
  312. f->outbox_len);
  313. free(new_record);
  314. } else {
  315. memcpy(p, new_record, record_length);
  316. free(new_record);
  317. }
  318. #ifdef DEBUG
  319. //check to see if next record still exists
  320. if(info->app_data_len > record_length + RECORD_HEADER_LEN){
  321. printf("Next record:\n");
  322. for(int i=0; i< RECORD_HEADER_LEN; i++){
  323. printf("%02x ", p[record_length+i]);
  324. }
  325. printf("\n");
  326. } else {
  327. printf("No extra record: %d <= %d + %d\n", data_len, record_length, RECORD_HEADER_LEN);
  328. }
  329. #endif
  330. tmp_len -= record_length+ RECORD_HEADER_LEN;
  331. p += record_length;
  332. }
  333. //step 4: recompute TCP checksum
  334. tcp_checksum(info);
  335. return 0;
  336. }
  337. /** Computes the TCP checksum of the data according to RFC 793
  338. * sum all 16-bit words in the segment, padd the last word if
  339. * needed
  340. *
  341. * there is a pseudo-header prefixed to the segment and
  342. * included in the checksum:
  343. *
  344. * +--------+--------+--------+--------+
  345. * | Source Address |
  346. * +--------+--------+--------+--------+
  347. * | Destination Address |
  348. * +--------+--------+--------+--------+
  349. * | zero | PTCL | TCP Length |
  350. * +--------+--------+--------+--------+
  351. */
  352. uint16_t tcp_checksum(struct packet_info *info){
  353. uint16_t tcp_length = info->app_data_len + info->size_tcp_hdr;
  354. struct in_addr src = info->ip_hdr->src;
  355. struct in_addr dst = info->ip_hdr->dst;
  356. uint8_t proto = IPPROTO_TCP;
  357. //set the checksum to zero
  358. info->tcp_hdr->chksum = 0;
  359. //sum pseudoheader
  360. uint32_t sum = (ntohl(src.s_addr)) >> 16;
  361. sum += (ntohl(src.s_addr)) &0xFFFF;
  362. sum += (ntohl(dst.s_addr)) >> 16;
  363. sum += (ntohl(dst.s_addr)) & 0xFFFF;
  364. sum += proto;
  365. sum += tcp_length;
  366. //sum tcp header (with zero-d checksum)
  367. uint8_t *p = (uint8_t *) info->tcp_hdr;
  368. for(int i=0; i < info->size_tcp_hdr; i+=2){
  369. sum += (uint16_t) ((p[i] << 8) + p[i+1]);
  370. }
  371. //now sum the application data
  372. p = info->app_data;
  373. for(int i=0; i< info->app_data_len-1; i+=2){
  374. sum += (uint16_t) ((p[i] << 8) + p[i+1]);
  375. }
  376. if(info->app_data_len %2 != 0){
  377. sum += (uint16_t) (p[info->app_data_len - 1]) << 8;
  378. }
  379. //now add most significant to last significant bits
  380. sum = (sum >> 16) + (sum & 0xFFFF);
  381. //now subtract from 0xFF
  382. sum = 0xFFFF - sum;
  383. //set chksum to calculated value
  384. info->tcp_hdr->chksum = ntohs(sum);
  385. return (uint16_t) sum;
  386. }