socks5proxy.c 21 KB

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