socks5proxy.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  1. /** SOCKSv5 proxy that listens on port 1080.
  2. *
  3. *
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8. #include <stdint.h>
  9. #include <errno.h>
  10. #include <string.h>
  11. #include <sys/socket.h>
  12. #include <sys/types.h>
  13. #include <sys/stat.h>
  14. #include <netinet/in.h>
  15. #include <netdb.h>
  16. #include <pthread.h>
  17. #include <fcntl.h>
  18. #include <arpa/inet.h>
  19. #include <openssl/bio.h>
  20. #include <openssl/evp.h>
  21. #include <openssl/buffer.h>
  22. #include <openssl/rand.h>
  23. #include "socks5proxy.h"
  24. #include "crypto.h"
  25. #define NEW
  26. int main(void){
  27. int listen_socket;
  28. struct sockaddr_in address;
  29. struct sockaddr_in remote_addr;
  30. socklen_t addr_size;
  31. mkfifo("OUS_out", 0666);
  32. //generate Slitheen ID using Telex tagging method
  33. uint8_t slitheen_id[SLITHEEN_ID_LEN];
  34. RAND_bytes(slitheen_id, SLITHEEN_ID_LEN);
  35. printf("Randomly generated slitheen id: ");
  36. int i;
  37. for(i=0; i< SLITHEEN_ID_LEN; i++){
  38. printf("%02x ", slitheen_id[i]);
  39. }
  40. printf("\n");
  41. // Calculate super encryption keys
  42. generate_super_keys(slitheen_id);
  43. //b64 encode slitheen ID
  44. char *encoded_bytes;
  45. BUF_MEM *buffer_ptr;
  46. BIO *bio, *b64;
  47. b64 = BIO_new(BIO_f_base64());
  48. bio = BIO_new(BIO_s_mem());
  49. bio = BIO_push(b64, bio);
  50. BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
  51. BIO_write(bio, slitheen_id, SLITHEEN_ID_LEN);
  52. BIO_flush(bio);
  53. BIO_get_mem_ptr(bio, &buffer_ptr);
  54. BIO_set_close(bio, BIO_NOCLOSE);
  55. BIO_free_all(bio);
  56. encoded_bytes = (*buffer_ptr).data;
  57. encoded_bytes[(*buffer_ptr).length] = '\0';
  58. printf("Encoded string is length %d, %s\n", (*buffer_ptr).length, encoded_bytes);
  59. //give encoded slitheen ID to ous
  60. struct sockaddr_in ous_addr;
  61. ous_addr.sin_family = AF_INET;
  62. inet_pton(AF_INET, "127.0.0.1", &(ous_addr.sin_addr));
  63. ous_addr.sin_port = htons(8888);
  64. int32_t ous_in = socket(AF_INET, SOCK_STREAM, 0);
  65. if(ous_in < 0){
  66. printf("Failed to make ous_in socket\n");
  67. return 1;
  68. }
  69. int32_t error = connect(ous_in, (struct sockaddr *) &ous_addr, sizeof (struct sockaddr));
  70. if(error < 0){
  71. printf("Error connecting\n");
  72. return 1;
  73. }
  74. char *message = calloc(1, BUFSIZ);
  75. sprintf(message, "POST / HTTP/1.1\r\nContent-Length: %zd\r\n\r\n%s ", strlen(encoded_bytes), encoded_bytes);
  76. int32_t bytes_sent = send(ous_in, message, strlen(message), 0);
  77. printf("Wrote %d bytes to OUS_in:\n %s\n", bytes_sent, message);
  78. free(message);
  79. /* Spawn process to listen for incoming data from OUS
  80. int32_t demux_pipe[2];
  81. if(pipe(demux_pipe) < 0){
  82. printf("Failed to create pipe for new thread\n");
  83. return 1;
  84. }*/
  85. connections = calloc(1, sizeof(connection_table));
  86. connections->first = NULL;
  87. pthread_t *demux_thread = calloc(1, sizeof(pthread_t));
  88. pthread_create(demux_thread, NULL, demultiplex_data, NULL);
  89. if (!(listen_socket = socket(AF_INET, SOCK_STREAM, 0))){
  90. printf("Error creating socket\n");
  91. fflush(stdout);
  92. return 1;
  93. }
  94. address.sin_family = AF_INET;
  95. address.sin_addr.s_addr = INADDR_ANY;
  96. address.sin_port = htons(1080);
  97. int enable = 1;
  98. if (setsockopt(listen_socket, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable)) <0 ){
  99. printf("Error setting sockopt\n");
  100. return 1;
  101. }
  102. if(bind(listen_socket, (struct sockaddr *) &address, sizeof(address))){
  103. printf("Error binding socket\n");
  104. fflush(stdout);
  105. return 1;
  106. }
  107. if(listen(listen_socket, 10) < 0){
  108. printf("Error listening\n");
  109. fflush(stdout);
  110. close(listen_socket);
  111. exit(1);
  112. }
  113. uint8_t last_id = 1;
  114. printf("Ready for listening\n");
  115. for(;;){
  116. addr_size = sizeof(remote_addr);
  117. int new_socket;
  118. new_socket = accept(listen_socket, (struct sockaddr *) &remote_addr,
  119. &addr_size);
  120. if(new_socket < 0){
  121. perror("accept");
  122. exit(1);
  123. }
  124. printf("New connection\n");
  125. //assign a new stream_id and create a pipe for the session
  126. connection *new_conn = calloc(1, sizeof(connection));
  127. new_conn->stream_id = last_id++;
  128. int32_t pipefd[2];
  129. if(pipe(pipefd) < 0){
  130. printf("Failed to create pipe\n");
  131. continue;
  132. }
  133. new_conn->pipe_fd = pipefd[1];
  134. new_conn->next = NULL;
  135. if(connections->first == NULL){
  136. connections->first = new_conn;
  137. printf("Added first connection with id: %d\n", new_conn->stream_id);
  138. printf("Connection table (%p) has entry %p\n", connections, connections->first);
  139. fflush(stdout);
  140. } else {
  141. connection *last = connections->first;
  142. printf("New incoming connection\n");
  143. fflush(stdout);
  144. while(last->next != NULL){
  145. last = last->next;
  146. }
  147. last->next = new_conn;
  148. printf("Added connection with id: %d at %p\n", new_conn->stream_id, last->next);
  149. fflush(stdout);
  150. }
  151. int pid = fork();
  152. if(pid == 0){ //child
  153. close(listen_socket);
  154. printf("demux reads from pipe fd %d", pipefd[1]);
  155. fflush(stdout);
  156. proxy_data(new_socket, new_conn->stream_id, pipefd[0]);
  157. exit(0);
  158. }
  159. close(new_socket);
  160. }
  161. return 0;
  162. }
  163. /*
  164. * Generate the keys for the super encryption layer, based on the slitheen ID
  165. */
  166. int generate_super_keys(uint8_t *secret){
  167. super = calloc(1, sizeof(super_data));
  168. //need 2 encryption keys, 2 ivs, and a mac
  169. //EVP_CIPHER_CTX *hdr_ctx;
  170. //EVP_CIPHER_CTX *bdy_ctx;
  171. EVP_MD_CTX *mac_ctx;
  172. const EVP_MD *md = EVP_sha256();
  173. /* Generate Keys */
  174. uint8_t *hdr_key, *bdy_key;
  175. uint8_t *mac_secret;
  176. EVP_PKEY *mac_key;
  177. int32_t mac_len, key_len;
  178. key_len = EVP_CIPHER_key_length(EVP_aes_256_cbc());
  179. mac_len = EVP_MD_size(md);
  180. int32_t total_len = 2*key_len + mac_len;
  181. uint8_t *key_block = calloc(1, total_len);
  182. PRF(secret, SLITHEEN_SUPER_SECRET_SIZE,
  183. (uint8_t *) SLITHEEN_SUPER_CONST, SLITHEEN_SUPER_CONST_SIZE,
  184. NULL, 0,
  185. NULL, 0,
  186. NULL, 0,
  187. key_block, total_len);
  188. //#ifdef DEBUG
  189. int i;
  190. printf("secret: \n");
  191. for(i=0; i< SLITHEEN_SUPER_SECRET_SIZE; i++){
  192. printf("%02x ", secret[i]);
  193. }
  194. printf("\n");
  195. printf("keyblock: \n");
  196. for(i=0; i< total_len; i++){
  197. printf("%02x ", key_block[i]);
  198. }
  199. printf("\n");
  200. //#endif
  201. hdr_key = key_block;
  202. bdy_key = key_block + key_len;
  203. mac_secret = key_block + 2*key_len;
  204. /* Initialize Cipher Contexts */
  205. //hdr_ctx = EVP_CIPHER_CTX_new();
  206. //bdy_ctx = EVP_CIPHER_CTX_new();
  207. //EVP_CipherInit_ex(hdr_ctx, EVP_aes_128_ecb(), NULL, hdr_key, NULL, 0);
  208. //EVP_CipherInit_ex(bdy_ctx, EVP_aes_256_cbc(), NULL, bdy_key, bdy_iv, 0);
  209. /* Initialize MAC Context */
  210. mac_ctx = EVP_MD_CTX_create();
  211. EVP_DigestInit_ex(mac_ctx, md, NULL);
  212. mac_key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, mac_secret, mac_len);
  213. EVP_DigestSignInit(mac_ctx, NULL, md, NULL, mac_key);
  214. //super->header_ctx = hdr_ctx;
  215. //super->body_ctx = bdy_ctx;
  216. super->header_key = malloc(key_len);
  217. super->body_key = malloc(key_len);
  218. memcpy(super->header_key, hdr_key, key_len);
  219. memcpy(super->body_key, bdy_key, key_len);
  220. super->body_mac_ctx = mac_ctx;
  221. //Free everything
  222. free(key_block);
  223. EVP_PKEY_free(mac_key);
  224. return 0;
  225. }
  226. struct socks_method_req {
  227. uint8_t version;
  228. uint8_t num_methods;
  229. };
  230. struct socks_req {
  231. uint8_t version;
  232. uint8_t cmd;
  233. uint8_t rsvd;
  234. uint8_t addr_type;
  235. };
  236. //continuously read from the socket and look for a CONNECT message
  237. int proxy_data(int sockfd, uint16_t stream_id, int32_t ous_out){
  238. uint8_t *buffer = calloc(1, BUFSIZ);
  239. uint8_t *response = calloc(1, BUFSIZ);
  240. printf("ous out pipe fd: %d\n", ous_out);
  241. fflush(stdout);
  242. int bytes_read = recv(sockfd, buffer, BUFSIZ-1, 0);
  243. if (bytes_read < 0){
  244. printf("Error reading from socket (fd = %d)\n", sockfd);
  245. fflush(stdout);
  246. goto err;
  247. }
  248. printf("Received %d bytes (id %d):\n", bytes_read, stream_id);
  249. int i;
  250. for(i=0; i< bytes_read; i++){
  251. printf("%02x ", buffer[i]);
  252. }
  253. printf("\n");
  254. fflush(stdout);
  255. //Respond to methods negotiation
  256. struct socks_method_req *clnt_meth = (struct socks_method_req *) buffer;
  257. uint8_t *p = buffer + 2;
  258. if(clnt_meth->version != 0x05){
  259. printf("Client supplied invalid version: %02x\n", clnt_meth->version);
  260. fflush(stdout);
  261. }
  262. int responded = 0;
  263. int bytes_sent;
  264. for(i=0; i< clnt_meth->num_methods; i++){
  265. if(p[0] == 0x00){//send response with METH= 0x00
  266. response[0] = 0x05;
  267. response[1] = 0x00;
  268. send(sockfd, response, 2, 0);
  269. responded = 1;
  270. }
  271. p++;
  272. }
  273. if(!responded){//respond with METH= 0xFF
  274. response[0] = 0x05;
  275. response[1] = 0xFF;
  276. send(sockfd, response, 2, 0);
  277. goto err;
  278. }
  279. //Now wait for a connect request
  280. bytes_read = recv(sockfd, buffer, BUFSIZ-1, 0);
  281. if (bytes_read < 0){
  282. printf("Error reading from socket\n");
  283. fflush(stdout);
  284. goto err;
  285. }
  286. printf("Received %d bytes (id %d):\n", bytes_read, stream_id);
  287. for(i=0; i< bytes_read; i++){
  288. printf("%02x ", buffer[i]);
  289. }
  290. printf("\n");
  291. fflush(stdout);
  292. //Now respond
  293. response[0] = 0x05;
  294. response[1] = 0x00;
  295. response[2] = 0x00;
  296. response[3] = 0x01;
  297. *((uint32_t *) (response + 4)) = 0;
  298. *((uint16_t *) (response + 8)) = 0;
  299. send(sockfd, response, 10, 0);
  300. //wait for first upstream bytes
  301. bytes_read += recv(sockfd, buffer+bytes_read, BUFSIZ-bytes_read-3, 0);
  302. if (bytes_read < 0){
  303. printf("Error reading from socket\n");
  304. fflush(stdout);
  305. goto err;
  306. }
  307. printf("Received %d bytes (id %d):\n", bytes_read, stream_id);
  308. for(i=0; i< bytes_read; i++){
  309. printf("%02x ", buffer[i]);
  310. }
  311. printf("\n");
  312. fflush(stdout);
  313. //pre-pend stream_id and length
  314. memmove(buffer+sizeof(struct slitheen_up_hdr), buffer, bytes_read+1);
  315. struct slitheen_up_hdr *up_hdr = (struct slitheen_up_hdr *) buffer;
  316. up_hdr->stream_id = stream_id;
  317. up_hdr->len = htons(bytes_read);
  318. bytes_read+= sizeof(struct slitheen_up_hdr);
  319. //encode bytes for safe transport (b64)
  320. const char *encoded_bytes;
  321. BUF_MEM *buffer_ptr;
  322. BIO *bio, *b64;
  323. b64 = BIO_new(BIO_f_base64());
  324. bio = BIO_new(BIO_s_mem());
  325. bio = BIO_push(b64, bio);
  326. BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
  327. BIO_write(bio, buffer, bytes_read);
  328. BIO_flush(bio);
  329. BIO_get_mem_ptr(bio, &buffer_ptr);
  330. BIO_set_close(bio, BIO_NOCLOSE);
  331. BIO_free_all(bio);
  332. encoded_bytes = (*buffer_ptr).data;
  333. #ifdef NEW
  334. struct sockaddr_in ous_addr;
  335. ous_addr.sin_family = AF_INET;
  336. inet_pton(AF_INET, "127.0.0.1", &(ous_addr.sin_addr));
  337. ous_addr.sin_port = htons(8888);
  338. int32_t ous_in = socket(AF_INET, SOCK_STREAM, 0);
  339. if(ous_in < 0){
  340. printf("Failed to make ous_in socket\n");
  341. return 1;
  342. }
  343. int32_t error = connect(ous_in, (struct sockaddr *) &ous_addr, sizeof (struct sockaddr));
  344. if(error < 0){
  345. printf("Error connecting\n");
  346. return 1;
  347. }
  348. #endif
  349. //send connect request to OUS
  350. #ifdef OLD
  351. int ous_in = open("OUS_in", O_CREAT | O_WRONLY, 0666);
  352. if(ous_in < 0){
  353. printf("Error opening file OUS_in\n");
  354. fflush(stdout);
  355. goto err;
  356. }
  357. lseek(ous_in, 0, SEEK_END);
  358. #endif
  359. #ifdef NEW
  360. char *message = calloc(1, BUFSIZ);
  361. sprintf(message, "POST / HTTP/1.1\r\nContent-Length: %zd\r\n\r\n%s ", strlen(encoded_bytes)+1, encoded_bytes);
  362. bytes_sent = send(ous_in, message, strlen(message), 0);
  363. printf("Wrote %d bytes to OUS_in: %s\n", bytes_sent, message);
  364. #endif
  365. #ifdef OLD
  366. bytes_sent = write(ous_in, encoded_bytes, strlen(encoded_bytes));
  367. bytes_sent += write(ous_in, " ", 1);
  368. #endif
  369. if(bytes_sent < 0){
  370. printf("Error writing to websocket\n");
  371. fflush(stdout);
  372. goto err;
  373. } else {
  374. close(ous_in);
  375. }
  376. p = buffer+sizeof(struct slitheen_up_hdr);
  377. for(i=0; i< bytes_read; i++){
  378. printf("%02x ", p[i]);
  379. }
  380. printf("\n");
  381. fflush(stdout);
  382. struct socks_req *clnt_req = (struct socks_req *) p;
  383. p += 4;
  384. //see if it's a connect request
  385. if(clnt_req->cmd != 0x01){
  386. printf("Error: issued a non-connect command\n");
  387. fflush(stdout);
  388. goto err;
  389. }
  390. printf("Received a connect request from stream id %d\n", stream_id);
  391. fflush(stdout);
  392. //now select on pipe (for downstream data) and the socket (for upstream data)
  393. for(;;){
  394. fd_set readfds;
  395. fd_set writefds;
  396. int32_t nfds = (sockfd > ous_out) ? sockfd +1 : ous_out + 1;
  397. //if(sockfd > ous_out){
  398. // nfds = (sockfd > ous_in) ? sockfd +1 : ous_in + 1;
  399. //} else {
  400. // nfds = (ous_out > ous_in) ? ous_out +1 : ous_in + 1;
  401. //}
  402. FD_ZERO(&readfds);
  403. FD_ZERO(&writefds);
  404. FD_SET(sockfd, &readfds);
  405. FD_SET(ous_out, &readfds);
  406. FD_SET(sockfd, &writefds);
  407. //FD_SET(ous_in, &writefds);
  408. if(select(nfds, &readfds, &writefds, NULL, NULL) <0){
  409. printf("Select error\n");
  410. fflush(stdout);
  411. continue;
  412. }
  413. if(FD_ISSET(sockfd, &readfds)){// && FD_ISSET(ous_in, &writefds)){
  414. bytes_read = recv(sockfd, buffer, BUFSIZ-1, 0);
  415. if (bytes_read < 0){
  416. printf("Error reading from socket (in for loop)\n");
  417. fflush(stdout);
  418. goto err;
  419. }
  420. if(bytes_read == 0){
  421. //socket is closed
  422. printf("Closing connection for stream %d sockfd.\n", stream_id);
  423. fflush(stdout);
  424. //Send close message to slitheen proxy
  425. up_hdr = (struct slitheen_up_hdr *) buffer;
  426. up_hdr->stream_id = stream_id;
  427. up_hdr->len = 0;
  428. bio = BIO_new(BIO_s_mem());
  429. b64 = BIO_new(BIO_f_base64());
  430. bio = BIO_push(b64, bio);
  431. BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
  432. BIO_write(bio, buffer, 20);
  433. BIO_flush(bio);
  434. BIO_get_mem_ptr(bio, &buffer_ptr);
  435. BIO_set_close(bio, BIO_NOCLOSE);
  436. BIO_free_all(bio);
  437. encoded_bytes = (*buffer_ptr).data;
  438. ous_in = socket(AF_INET, SOCK_STREAM, 0);
  439. if(ous_in < 0){
  440. printf("Failed to make ous_in socket\n");
  441. fflush(stdout);
  442. goto err;
  443. }
  444. error = connect(ous_in, (struct sockaddr *) &ous_addr, sizeof (struct sockaddr));
  445. if(error < 0){
  446. printf("Error connecting\n");
  447. fflush(stdout);
  448. goto err;
  449. }
  450. sprintf(message, "POST / HTTP/1.1\r\nContent-Length: %zd\r\n\r\n%s ", strlen(encoded_bytes)+1, encoded_bytes);
  451. bytes_sent = send(ous_in, message, strlen(message), 0);
  452. close(ous_in);
  453. goto err;
  454. }
  455. if(bytes_read > 0){
  456. printf("Received %d data bytes from sockfd (id %d):\n", bytes_read, stream_id);
  457. for(i=0; i< bytes_read; i++){
  458. printf("%02x ", buffer[i]);
  459. }
  460. printf("\n");
  461. printf("%s\n", buffer);
  462. fflush(stdout);
  463. memmove(buffer+sizeof(struct slitheen_up_hdr), buffer, bytes_read);
  464. up_hdr = (struct slitheen_up_hdr *) buffer;
  465. up_hdr->stream_id = stream_id;
  466. up_hdr->len = htons(bytes_read);
  467. bytes_read+= sizeof(struct slitheen_up_hdr);
  468. bio = BIO_new(BIO_s_mem());
  469. b64 = BIO_new(BIO_f_base64());
  470. bio = BIO_push(b64, bio);
  471. printf("HERE\n");
  472. fflush(stdout);
  473. BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
  474. BIO_write(bio, buffer, bytes_read);
  475. BIO_flush(bio);
  476. BIO_get_mem_ptr(bio, &buffer_ptr);
  477. BIO_set_close(bio, BIO_NOCLOSE);
  478. BIO_free_all(bio);
  479. encoded_bytes = (*buffer_ptr).data;
  480. #ifdef OLD
  481. int ous_in = open("OUS_in", O_CREAT | O_WRONLY, 0666);
  482. if(ous_in < 0){
  483. printf("Error opening file OUS_in\n");
  484. fflush(stdout);
  485. goto err;
  486. }
  487. lseek(ous_in, 0, SEEK_END);
  488. #endif
  489. #ifdef NEW
  490. ous_in = socket(AF_INET, SOCK_STREAM, 0);
  491. if(ous_in < 0){
  492. printf("Failed to make ous_in socket\n");
  493. return 1;
  494. }
  495. error = connect(ous_in, (struct sockaddr *) &ous_addr, sizeof (struct sockaddr));
  496. if(error < 0){
  497. printf("Error connecting\n");
  498. return 1;
  499. }
  500. sprintf(message, "POST / HTTP/1.1\r\nContent-Length: %zd\r\n\r\n%s ", strlen(encoded_bytes)+1, encoded_bytes);
  501. bytes_sent = send(ous_in, message, strlen(message), 0);
  502. printf("Sent to OUS (%d bytes):%s\n",bytes_sent, message);
  503. close(ous_in);
  504. #endif
  505. #ifdef OLD
  506. bytes_sent = write(ous_in, encoded_bytes, strlen(encoded_bytes));
  507. bytes_sent += write(ous_in, " ", 1);
  508. printf("Sent to OUS (%d bytes):%s\n",bytes_sent, encoded_bytes);
  509. close(ous_in);
  510. #endif
  511. }
  512. } else if(FD_ISSET(ous_out, &readfds) && FD_ISSET(sockfd, &writefds)){
  513. bytes_read = read(ous_out, buffer, BUFSIZ-1);
  514. if (bytes_read <= 0){
  515. printf("Error reading from ous_out (in for loop)\n");
  516. fflush(stdout);
  517. goto err;
  518. }
  519. if(bytes_read > 0){
  520. printf("Stream id %d received %d bytes from ous_out:\n", stream_id, bytes_read);
  521. for(i=0; i< bytes_read; i++){
  522. printf("%02x ", buffer[i]);
  523. }
  524. printf("\n");
  525. printf("%s\n", buffer);
  526. fflush(stdout);
  527. bytes_sent = send(sockfd, buffer, bytes_read, 0);
  528. if(bytes_sent <= 0){
  529. printf("Error sending bytes to browser for stream id %d\n", stream_id);
  530. }
  531. printf("Sent to browser (%d bytes from stream id %d):\n", bytes_sent, stream_id);
  532. for(i=0; i< bytes_sent; i++){
  533. printf("%02x ", buffer[i]);
  534. }
  535. printf("\n");
  536. fflush(stdout);
  537. }
  538. }
  539. }
  540. err:
  541. //should also remove stream from table
  542. close(sockfd);
  543. free(buffer);
  544. free(response);
  545. exit(0);
  546. }
  547. /* Read blocks of covert data from OUS_out. Determine the stream id and the length of
  548. * the block and then write the data to the correct thread to be passed to the browser
  549. */
  550. void *demultiplex_data(){
  551. int32_t buffer_len = BUFSIZ;
  552. uint8_t *buffer = calloc(1, buffer_len);
  553. uint8_t *p;
  554. printf("Opening OUS_out\n");
  555. int32_t ous_fd = open("OUS_out", O_RDONLY);
  556. printf("Opened.\n");
  557. uint8_t *partial_block;
  558. uint32_t partial_block_len = 0;
  559. uint32_t resource_remaining = 0;
  560. uint64_t expected_next_count = 1;
  561. data_block *saved_data = NULL;
  562. for(;;){
  563. int32_t bytes_read = read(ous_fd, buffer, buffer_len-partial_block_len);
  564. if(bytes_read > 0){
  565. int32_t bytes_remaining = bytes_read;
  566. //printf("Read in %d bytes from OUS_out\n", bytes_remaining);
  567. p = buffer;
  568. //the first value for a new resource will be the resource length,
  569. //followed by a newline
  570. if(resource_remaining > 0){
  571. resource_remaining -= bytes_remaining;
  572. if((bytes_remaining > 0) && (partial_block_len > 0)){
  573. //process first part of slitheen info
  574. memmove(buffer+partial_block_len, buffer, bytes_read);
  575. memcpy(buffer, partial_block, partial_block_len);
  576. bytes_remaining += partial_block_len;
  577. free(partial_block);
  578. partial_block_len = 0;
  579. }
  580. } else {
  581. uint8_t *end_ptr;
  582. resource_remaining = strtol((const char *) p, (char **) &end_ptr, 10);
  583. if(resource_remaining == 0){
  584. printf("UH OH, resource_remaining is zero or there was an error O.o\n");
  585. } else {
  586. bytes_remaining -= (end_ptr - p) + 1;
  587. p += (end_ptr - p) + 1;
  588. if(resource_remaining < bytes_remaining){
  589. resource_remaining = 0;
  590. printf("UH OH, shouldn't be here\n");
  591. } else {
  592. resource_remaining -= bytes_remaining;
  593. }
  594. }
  595. }
  596. while(bytes_remaining > 0){
  597. if(bytes_remaining + resource_remaining < SLITHEEN_HEADER_LEN){
  598. printf("Resource is padded out with garbage\n");
  599. bytes_remaining = 0;
  600. break;
  601. }
  602. if((bytes_remaining < SLITHEEN_HEADER_LEN)){
  603. printf("Partial header: ");
  604. int i;
  605. for(i = 0; i< bytes_remaining; i++){
  606. printf("%02x ", p[i]);
  607. }
  608. printf("\n");
  609. partial_block = calloc(1, bytes_remaining);
  610. memcpy(partial_block, p, bytes_remaining);
  611. partial_block_len = bytes_remaining;
  612. bytes_remaining = 0;
  613. break;
  614. }
  615. super_decrypt(p);
  616. struct slitheen_hdr *sl_hdr = (struct slitheen_hdr *) p;
  617. //first see if sl_hdr corresponds to a valid stream. If not, ignore rest of read bytes
  618. #ifdef DEBUG
  619. printf("Slitheen header:\n");
  620. int i;
  621. for(i = 0; i< SLITHEEN_HEADER_LEN; i++){
  622. printf("%02x ", p[i]);
  623. }
  624. printf("\n");
  625. #endif
  626. if(ntohs(sl_hdr->len) > bytes_remaining){
  627. printf("Received partial block\n");
  628. partial_block = calloc(1, ntohs(sl_hdr->len));
  629. memcpy(partial_block, p, bytes_remaining);
  630. partial_block_len = bytes_remaining;
  631. bytes_remaining = 0;
  632. break;
  633. }
  634. p += SLITHEEN_HEADER_LEN;
  635. bytes_remaining -= SLITHEEN_HEADER_LEN;
  636. if((!sl_hdr->len) && (sl_hdr->garbage)){
  637. #ifdef DEBUG
  638. printf("%d Garbage bytes\n", ntohs(sl_hdr->garbage));
  639. #endif
  640. p += ntohs(sl_hdr->garbage);
  641. bytes_remaining -= ntohs(sl_hdr->garbage);
  642. continue;
  643. }
  644. int32_t pipe_fd =-1;
  645. if(connections->first == NULL){
  646. printf("There are no connections\n");
  647. } else {
  648. connection *last = connections->first;
  649. if (last->stream_id == sl_hdr->stream_id){
  650. printf("Found stream id %d!\n", sl_hdr->stream_id);
  651. pipe_fd = last->pipe_fd;
  652. printf("Pipe fd: %d\n", pipe_fd);
  653. }
  654. while(last->next != NULL){
  655. last = last->next;
  656. if (last->stream_id == sl_hdr->stream_id){
  657. printf("Found stream id %d!\n", sl_hdr->stream_id);
  658. pipe_fd = last->pipe_fd;
  659. printf("Pipe fd: %d\n", pipe_fd);
  660. }
  661. }
  662. }
  663. if(pipe_fd == -1){
  664. printf("No stream id exists. Possibly invalid header\n");
  665. break;
  666. }
  667. printf("Received information for stream id: %d of length: %u\n", sl_hdr->stream_id, ntohs(sl_hdr->len));
  668. //figure out how much to skip
  669. int32_t padding = 0;
  670. if(ntohs(sl_hdr->len) %16){
  671. padding = 16 - ntohs(sl_hdr->len)%16;
  672. }
  673. p += 16; //IV
  674. //check counter to see if we are missing data
  675. if(sl_hdr->counter > expected_next_count){
  676. //save any future data
  677. printf("Received header with count %lu. Expected count %lu.\n",
  678. sl_hdr->counter, expected_next_count);
  679. if(saved_data == NULL){
  680. saved_data = malloc(sizeof(data_block));
  681. saved_data->count = sl_hdr->counter;
  682. saved_data->data = malloc(ntohs(sl_hdr->len));
  683. memcpy(saved_data->data, p, ntohs(sl_hdr->len));
  684. saved_data->next = NULL;
  685. } else {
  686. data_block *last = saved_data;
  687. while(last->next != NULL){
  688. last = last->next;
  689. }
  690. data_block *new_block = malloc(sizeof(data_block));
  691. new_block->count = sl_hdr->counter;
  692. new_block->data = malloc(ntohs(sl_hdr->len));
  693. memcpy(new_block->data, p, ntohs(sl_hdr->len));
  694. new_block->next = NULL;
  695. last->next = new_block;
  696. }
  697. } else {
  698. int32_t bytes_sent = write(pipe_fd, p, ntohs(sl_hdr->len));
  699. if(bytes_sent <= 0){
  700. printf("Error reading to pipe for stream id %d\n",
  701. sl_hdr->stream_id);
  702. }
  703. //increment expected counter
  704. expected_next_count++;
  705. }
  706. //now check to see if there is saved data to write out
  707. if(saved_data != NULL){
  708. data_block *current_block = saved_data;
  709. while((current_block != NULL) &&
  710. (expected_next_count == current_block->count)){
  711. printf("Writing out saved data with count %ld\n",
  712. expected_next_count);
  713. int32_t bytes_sent = write(pipe_fd, p, ntohs(sl_hdr->len));
  714. if(bytes_sent <= 0){
  715. printf("Error reading to pipe for stream id %d\n",
  716. sl_hdr->stream_id);
  717. }
  718. expected_next_count++;
  719. saved_data = current_block->next;
  720. free(current_block);
  721. current_block = saved_data;
  722. }
  723. }
  724. p += ntohs(sl_hdr->len); //encrypted data
  725. p += 16; //mac
  726. p += padding;
  727. p += ntohs(sl_hdr->garbage);
  728. printf("Skipped %d garbage bytes\n", ntohs(sl_hdr->garbage));
  729. fflush(stdout);
  730. bytes_remaining -=
  731. ntohs(sl_hdr->len) + 16 + padding + 16 + ntohs(sl_hdr->garbage);
  732. printf("Bytes remaining: %d, padding: %d\n", bytes_remaining, padding);
  733. }
  734. } else {
  735. printf("Error: read %d bytes from OUS_out\n", bytes_read);
  736. printf("Opening OUS_out\n");
  737. close(ous_fd);
  738. ous_fd = open("OUS_out", O_RDONLY);
  739. printf("Opened.\n");
  740. }
  741. }
  742. close(ous_fd);
  743. }
  744. int super_decrypt(uint8_t *data){
  745. EVP_CIPHER_CTX *bdy_ctx;
  746. EVP_CIPHER_CTX *hdr_ctx;
  747. uint8_t *p = data;
  748. int32_t out_len, len;
  749. uint8_t output[EVP_MAX_MD_SIZE];
  750. size_t mac_len;
  751. int i;
  752. //decrypt header
  753. #ifdef DEBUG
  754. printf("Encrypted header:\n");
  755. for(i=0; i< SLITHEEN_HEADER_LEN; i++){
  756. printf("%02x ", p[i]);
  757. }
  758. printf("\n");
  759. #endif
  760. hdr_ctx = EVP_CIPHER_CTX_new();
  761. EVP_CipherInit_ex(hdr_ctx, EVP_aes_256_ecb(), NULL, super->header_key, NULL, 0);
  762. if(!EVP_CipherUpdate(hdr_ctx, p, &out_len, p, SLITHEEN_HEADER_LEN)){
  763. printf("Decryption failed!");
  764. return 0;
  765. }
  766. EVP_CIPHER_CTX_free(hdr_ctx);
  767. struct slitheen_hdr *sl_hdr = (struct slitheen_hdr *) p;
  768. len = htons(sl_hdr->len);
  769. if(!sl_hdr->len){//there are no data to be decrypted
  770. return 1;
  771. }
  772. printf("Decrypted header (%d bytes):\n", SLITHEEN_HEADER_LEN);
  773. for(i=0; i< SLITHEEN_HEADER_LEN; i++){
  774. printf("%02x ", p[i]);
  775. }
  776. printf("\n");
  777. fflush(stdout);
  778. p += SLITHEEN_HEADER_LEN;
  779. //compute mac
  780. EVP_DigestSignUpdate(super->body_mac_ctx, p, len);
  781. EVP_DigestSignFinal(super->body_mac_ctx, output, &mac_len);
  782. #ifdef DEBUG
  783. printf("Received mac:\n");
  784. for(i=0; i< 16; i++){
  785. printf("%02x ", p[len+i]);
  786. }
  787. printf("\n");
  788. fflush(stdout);
  789. if(memcmp(p+len, output, 16)){
  790. printf("MAC verification failed\n");
  791. return 0;
  792. }
  793. #endif
  794. //decrypt body
  795. bdy_ctx = EVP_CIPHER_CTX_new();
  796. EVP_CipherInit_ex(bdy_ctx, EVP_aes_256_cbc(), NULL, super->body_key, p, 0);
  797. p+=16;//skip IV
  798. printf("Encrypted data (%d bytes):\n", len);
  799. for(i=0; i< len; i++){
  800. printf("%02x ", p[i]);
  801. }
  802. printf("\n");
  803. if(!EVP_CipherUpdate(bdy_ctx, p, &out_len, p, len+16)){
  804. printf("Decryption failed!");
  805. return 0;
  806. }
  807. EVP_CIPHER_CTX_free(bdy_ctx);
  808. printf("Decrypted data (%d bytes):\n", out_len);
  809. for(i=0; i< out_len; i++){
  810. printf("%02x ", p[i]);
  811. }
  812. printf("\n");
  813. fflush(stdout);
  814. p += out_len;
  815. return 1;
  816. }