socks5proxy.c 28 KB

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