socks5proxy.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  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 NEW
  22. int proxy_data(int sockfd, uint8_t stream_id, int32_t pipefd);
  23. void *demultiplex_data();
  24. struct __attribute__ ((__packed__)) slitheen_hdr{
  25. uint8_t stream_id;
  26. uint16_t len;
  27. uint16_t garbage_len;
  28. };
  29. #define SLITHEEN_HEADER_LEN 5
  30. struct __attribute__ ((__packed__)) slitheen_up_hdr{
  31. uint8_t stream_id;
  32. uint16_t len;
  33. };
  34. typedef struct connection_st{
  35. int32_t pipe_fd;
  36. uint8_t stream_id;
  37. struct connection_st *next;
  38. } connection;
  39. typedef struct connection_table_st{
  40. connection *first;
  41. } connection_table;
  42. static connection_table *connections;
  43. int main(void){
  44. int listen_socket;
  45. struct sockaddr_in address;
  46. struct sockaddr_in remote_addr;
  47. socklen_t addr_size;
  48. mkfifo("OUS_out", 0666);
  49. /* Spawn process to listen for incoming data from OUS
  50. int32_t demux_pipe[2];
  51. if(pipe(demux_pipe) < 0){
  52. printf("Failed to create pipe for new thread\n");
  53. return 1;
  54. }*/
  55. connections = calloc(1, sizeof(connection_table));
  56. connections->first = NULL;
  57. pthread_t *demux_thread = calloc(1, sizeof(pthread_t));
  58. pthread_create(demux_thread, NULL, demultiplex_data, NULL);
  59. if (!(listen_socket = socket(AF_INET, SOCK_STREAM, 0))){
  60. printf("Error creating socket\n");
  61. fflush(stdout);
  62. return 1;
  63. }
  64. address.sin_family = AF_INET;
  65. address.sin_addr.s_addr = INADDR_ANY;
  66. address.sin_port = htons(1080);
  67. int enable = 1;
  68. if (setsockopt(listen_socket, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable)) <0 ){
  69. printf("Error setting sockopt\n");
  70. return 1;
  71. }
  72. if(bind(listen_socket, (struct sockaddr *) &address, sizeof(address))){
  73. printf("Error binding socket\n");
  74. fflush(stdout);
  75. return 1;
  76. }
  77. if(listen(listen_socket, 10) < 0){
  78. printf("Error listening\n");
  79. fflush(stdout);
  80. close(listen_socket);
  81. exit(1);
  82. }
  83. uint8_t last_id = 1;
  84. printf("Ready for listening\n");
  85. for(;;){
  86. addr_size = sizeof(remote_addr);
  87. int new_socket;
  88. new_socket = accept(listen_socket, (struct sockaddr *) &remote_addr,
  89. &addr_size);
  90. if(new_socket < 0){
  91. perror("accept");
  92. exit(1);
  93. }
  94. printf("New connection\n");
  95. //assign a new stream_id and create a pipe for the session
  96. connection *new_conn = calloc(1, sizeof(connection));
  97. new_conn->stream_id = last_id++;
  98. int32_t pipefd[2];
  99. if(pipe(pipefd) < 0){
  100. printf("Failed to create pipe\n");
  101. continue;
  102. }
  103. new_conn->pipe_fd = pipefd[1];
  104. new_conn->next = NULL;
  105. if(connections->first == NULL){
  106. connections->first = new_conn;
  107. printf("Added first connection with id: %d\n", new_conn->stream_id);
  108. printf("Connection table (%p) has entry %p\n", connections, connections->first);
  109. fflush(stdout);
  110. } else {
  111. connection *last = connections->first;
  112. printf("New incoming connection\n");
  113. fflush(stdout);
  114. while(last->next != NULL){
  115. last = last->next;
  116. }
  117. last->next = new_conn;
  118. printf("Added connection with id: %d at %p\n", new_conn->stream_id, last->next);
  119. fflush(stdout);
  120. }
  121. int pid = fork();
  122. if(pid == 0){ //child
  123. close(listen_socket);
  124. printf("demux reads from pipe fd %d", pipefd[1]);
  125. fflush(stdout);
  126. proxy_data(new_socket, new_conn->stream_id, pipefd[0]);
  127. exit(0);
  128. }
  129. close(new_socket);
  130. }
  131. return 0;
  132. }
  133. struct socks_method_req {
  134. uint8_t version;
  135. uint8_t num_methods;
  136. };
  137. struct socks_req {
  138. uint8_t version;
  139. uint8_t cmd;
  140. uint8_t rsvd;
  141. uint8_t addr_type;
  142. };
  143. //continuously read from the socket and look for a CONNECT message
  144. int proxy_data(int sockfd, uint8_t stream_id, int32_t ous_out){
  145. uint8_t *buffer = calloc(1, BUFSIZ);
  146. uint8_t *response = calloc(1, BUFSIZ);
  147. printf("ous out pipe fd: %d\n", ous_out);
  148. fflush(stdout);
  149. int bytes_read = recv(sockfd, buffer, BUFSIZ-1, 0);
  150. if (bytes_read < 0){
  151. printf("Error reading from socket (fd = %d)\n", sockfd);
  152. fflush(stdout);
  153. goto err;
  154. }
  155. printf("Received %d bytes (id %d):\n", bytes_read, stream_id);
  156. int i;
  157. for(i=0; i< bytes_read; i++){
  158. printf("%02x ", buffer[i]);
  159. }
  160. printf("\n");
  161. fflush(stdout);
  162. //Respond to methods negotiation
  163. struct socks_method_req *clnt_meth = (struct socks_method_req *) buffer;
  164. uint8_t *p = buffer + 2;
  165. if(clnt_meth->version != 0x05){
  166. printf("Client supplied invalid version: %02x\n", clnt_meth->version);
  167. fflush(stdout);
  168. }
  169. int responded = 0;
  170. int bytes_sent;
  171. for(i=0; i< clnt_meth->num_methods; i++){
  172. if(p[0] == 0x00){//send response with METH= 0x00
  173. response[0] = 0x05;
  174. response[1] = 0x00;
  175. send(sockfd, response, 2, 0);
  176. responded = 1;
  177. }
  178. p++;
  179. }
  180. if(!responded){//respond with METH= 0xFF
  181. response[0] = 0x05;
  182. response[1] = 0xFF;
  183. send(sockfd, response, 2, 0);
  184. goto err;
  185. }
  186. //Now wait for a connect request
  187. bytes_read = recv(sockfd, buffer, BUFSIZ-1, 0);
  188. if (bytes_read < 0){
  189. printf("Error reading from socket\n");
  190. fflush(stdout);
  191. goto err;
  192. }
  193. printf("Received %d bytes (id %d):\n", bytes_read, stream_id);
  194. for(i=0; i< bytes_read; i++){
  195. printf("%02x ", buffer[i]);
  196. }
  197. printf("\n");
  198. fflush(stdout);
  199. //Now respond
  200. response[0] = 0x05;
  201. response[1] = 0x00;
  202. response[2] = 0x00;
  203. response[3] = 0x01;
  204. *((uint32_t *) (response + 4)) = 0;
  205. *((uint16_t *) (response + 8)) = 0;
  206. send(sockfd, response, 10, 0);
  207. //wait for first upstream bytes
  208. bytes_read += recv(sockfd, buffer+bytes_read, BUFSIZ-bytes_read-3, 0);
  209. if (bytes_read < 0){
  210. printf("Error reading from socket\n");
  211. fflush(stdout);
  212. goto err;
  213. }
  214. printf("Received %d bytes (id %d):\n", bytes_read, stream_id);
  215. for(i=0; i< bytes_read; i++){
  216. printf("%02x ", buffer[i]);
  217. }
  218. printf("\n");
  219. fflush(stdout);
  220. //pre-pend stream_id and length
  221. memmove(buffer+3, buffer, bytes_read+1);
  222. struct slitheen_up_hdr *up_hdr = (struct slitheen_up_hdr *) buffer;
  223. up_hdr->stream_id = stream_id;
  224. up_hdr->len = htons(bytes_read);
  225. bytes_read+= 3;
  226. //encode bytes for safe transport (b64)
  227. const char *encoded_bytes;
  228. BUF_MEM *buffer_ptr;
  229. BIO *bio, *b64;
  230. b64 = BIO_new(BIO_f_base64());
  231. bio = BIO_new(BIO_s_mem());
  232. bio = BIO_push(b64, bio);
  233. BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
  234. BIO_write(bio, buffer, bytes_read);
  235. BIO_flush(bio);
  236. BIO_get_mem_ptr(bio, &buffer_ptr);
  237. BIO_set_close(bio, BIO_NOCLOSE);
  238. BIO_free_all(bio);
  239. encoded_bytes = (*buffer_ptr).data;
  240. #ifdef NEW
  241. struct sockaddr_in ous_addr;
  242. ous_addr.sin_family = AF_INET;
  243. inet_pton(AF_INET, "127.0.0.1", &(ous_addr.sin_addr));
  244. ous_addr.sin_port = htons(8888);
  245. int32_t ous_in = socket(AF_INET, SOCK_STREAM, 0);
  246. if(ous_in < 0){
  247. printf("Failed to make ous_in socket\n");
  248. return 1;
  249. }
  250. int32_t error = connect(ous_in, (struct sockaddr *) &ous_addr, sizeof (struct sockaddr));
  251. if(error < 0){
  252. printf("Error connecting\n");
  253. return 1;
  254. }
  255. #endif
  256. //send connect request to OUS
  257. #ifdef OLD
  258. int ous_in = open("OUS_in", O_CREAT | O_WRONLY, 0666);
  259. if(ous_in < 0){
  260. printf("Error opening file OUS_in\n");
  261. fflush(stdout);
  262. goto err;
  263. }
  264. lseek(ous_in, 0, SEEK_END);
  265. #endif
  266. #ifdef NEW
  267. uint8_t *message = calloc(1, BUFSIZ);
  268. sprintf(message, "POST / HTTP/1.1\r\nContent-Length: %d\r\n\r\n%s ", strlen(encoded_bytes)+1, encoded_bytes);
  269. bytes_sent = send(ous_in, message, strlen(message), 0);
  270. printf("Wrote %d bytes to OUS_in: %s\n", bytes_sent, message);
  271. #endif
  272. #ifdef OLD
  273. bytes_sent = write(ous_in, encoded_bytes, strlen(encoded_bytes));
  274. bytes_sent += write(ous_in, " ", 1);
  275. #endif
  276. if(bytes_sent < 0){
  277. printf("Error writing to websocket\n");
  278. fflush(stdout);
  279. goto err;
  280. } else {
  281. close(ous_in);
  282. }
  283. p = buffer+sizeof(struct slitheen_up_hdr);
  284. for(i=0; i< bytes_read; i++){
  285. printf("%02x ", p[i]);
  286. }
  287. printf("\n");
  288. fflush(stdout);
  289. struct socks_req *clnt_req = (struct socks_req *) p;
  290. p += 4;
  291. //see if it's a connect request
  292. if(clnt_req->cmd != 0x01){
  293. printf("Error: issued a non-connect command\n");
  294. fflush(stdout);
  295. goto err;
  296. }
  297. printf("Received a connect request from stream id %d\n", stream_id);
  298. fflush(stdout);
  299. //now select on pipe (for downstream data) and the socket (for upstream data)
  300. for(;;){
  301. fd_set readfds;
  302. fd_set writefds;
  303. int32_t nfds = (sockfd > ous_out) ? sockfd +1 : ous_out + 1;
  304. //if(sockfd > ous_out){
  305. // nfds = (sockfd > ous_in) ? sockfd +1 : ous_in + 1;
  306. //} else {
  307. // nfds = (ous_out > ous_in) ? ous_out +1 : ous_in + 1;
  308. //}
  309. FD_ZERO(&readfds);
  310. FD_ZERO(&writefds);
  311. FD_SET(sockfd, &readfds);
  312. FD_SET(ous_out, &readfds);
  313. FD_SET(sockfd, &writefds);
  314. //FD_SET(ous_in, &writefds);
  315. if(select(nfds, &readfds, &writefds, NULL, NULL) <0){
  316. printf("Select error\n");
  317. fflush(stdout);
  318. continue;
  319. }
  320. if(FD_ISSET(sockfd, &readfds)){// && FD_ISSET(ous_in, &writefds)){
  321. bytes_read = recv(sockfd, buffer, BUFSIZ-1, 0);
  322. if (bytes_read < 0){
  323. printf("Error reading from socket (in for loop)\n");
  324. fflush(stdout);
  325. goto err;
  326. }
  327. if(bytes_read == 0){
  328. //socket is closed
  329. printf("Closing connection for stream %d sockfd.\n", stream_id);
  330. fflush(stdout);
  331. //Send close message to slitheen proxy
  332. up_hdr = (struct slitheen_up_hdr *) buffer;
  333. up_hdr->stream_id = stream_id;
  334. up_hdr->len = 0;
  335. bio = BIO_new(BIO_s_mem());
  336. b64 = BIO_new(BIO_f_base64());
  337. bio = BIO_push(b64, bio);
  338. BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
  339. BIO_write(bio, buffer, 20);
  340. BIO_flush(bio);
  341. BIO_get_mem_ptr(bio, &buffer_ptr);
  342. BIO_set_close(bio, BIO_NOCLOSE);
  343. BIO_free_all(bio);
  344. encoded_bytes = (*buffer_ptr).data;
  345. ous_in = socket(AF_INET, SOCK_STREAM, 0);
  346. if(ous_in < 0){
  347. printf("Failed to make ous_in socket\n");
  348. fflush(stdout);
  349. goto err;
  350. }
  351. error = connect(ous_in, (struct sockaddr *) &ous_addr, sizeof (struct sockaddr));
  352. if(error < 0){
  353. printf("Error connecting\n");
  354. fflush(stdout);
  355. goto err;
  356. }
  357. sprintf(message, "POST / HTTP/1.1\r\nContent-Length: %d\r\n\r\n%s ", strlen(encoded_bytes)+1, encoded_bytes);
  358. bytes_sent = send(ous_in, message, strlen(message), 0);
  359. close(ous_in);
  360. goto err;
  361. }
  362. if(bytes_read > 0){
  363. printf("Received %d data bytes from sockfd (id %d):\n", bytes_read, stream_id);
  364. for(i=0; i< bytes_read; i++){
  365. printf("%02x ", buffer[i]);
  366. }
  367. printf("\n");
  368. printf("%s\n", buffer);
  369. fflush(stdout);
  370. memmove(buffer+sizeof(struct slitheen_up_hdr), buffer, bytes_read);
  371. up_hdr = (struct slitheen_up_hdr *) buffer;
  372. up_hdr->stream_id = stream_id;
  373. up_hdr->len = htons(bytes_read);
  374. bytes_read+= 3;
  375. bio = BIO_new(BIO_s_mem());
  376. b64 = BIO_new(BIO_f_base64());
  377. bio = BIO_push(b64, bio);
  378. printf("HERE\n");
  379. fflush(stdout);
  380. BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
  381. BIO_write(bio, buffer, bytes_read);
  382. BIO_flush(bio);
  383. BIO_get_mem_ptr(bio, &buffer_ptr);
  384. BIO_set_close(bio, BIO_NOCLOSE);
  385. BIO_free_all(bio);
  386. encoded_bytes = (*buffer_ptr).data;
  387. #ifdef OLD
  388. int ous_in = open("OUS_in", O_CREAT | O_WRONLY, 0666);
  389. if(ous_in < 0){
  390. printf("Error opening file OUS_in\n");
  391. fflush(stdout);
  392. goto err;
  393. }
  394. lseek(ous_in, 0, SEEK_END);
  395. #endif
  396. #ifdef NEW
  397. ous_in = socket(AF_INET, SOCK_STREAM, 0);
  398. if(ous_in < 0){
  399. printf("Failed to make ous_in socket\n");
  400. return 1;
  401. }
  402. error = connect(ous_in, (struct sockaddr *) &ous_addr, sizeof (struct sockaddr));
  403. if(error < 0){
  404. printf("Error connecting\n");
  405. return 1;
  406. }
  407. sprintf(message, "POST / HTTP/1.1\r\nContent-Length: %d\r\n\r\n%s ", strlen(encoded_bytes)+1, encoded_bytes);
  408. bytes_sent = send(ous_in, message, strlen(message), 0);
  409. printf("Sent to OUS (%d bytes):%s\n",bytes_sent, message);
  410. close(ous_in);
  411. #endif
  412. #ifdef OLD
  413. bytes_sent = write(ous_in, encoded_bytes, strlen(encoded_bytes));
  414. bytes_sent += write(ous_in, " ", 1);
  415. printf("Sent to OUS (%d bytes):%s\n",bytes_sent, encoded_bytes);
  416. close(ous_in);
  417. #endif
  418. }
  419. } else if(FD_ISSET(ous_out, &readfds) && FD_ISSET(sockfd, &writefds)){
  420. bytes_read = read(ous_out, buffer, BUFSIZ-1);
  421. if (bytes_read <= 0){
  422. printf("Error reading from ous_out (in for loop)\n");
  423. fflush(stdout);
  424. goto err;
  425. }
  426. if(bytes_read > 0){
  427. printf("Stream id %d received %d bytes from ous_out:\n", stream_id, bytes_read);
  428. for(i=0; i< bytes_read; i++){
  429. printf("%02x ", buffer[i]);
  430. }
  431. printf("\n");
  432. printf("%s\n", buffer);
  433. fflush(stdout);
  434. bytes_sent = send(sockfd, buffer, bytes_read, 0);
  435. if(bytes_sent <= 0){
  436. printf("Error sending bytes to browser for stream id %d\n", stream_id);
  437. }
  438. printf("Sent to browser (%d bytes from stream id %d):\n", bytes_sent, stream_id);
  439. for(i=0; i< bytes_sent; i++){
  440. printf("%02x ", buffer[i]);
  441. }
  442. printf("\n");
  443. fflush(stdout);
  444. }
  445. }
  446. }
  447. err:
  448. //should also remove stream from table
  449. close(sockfd);
  450. free(buffer);
  451. free(response);
  452. exit(0);
  453. }
  454. void *demultiplex_data(){
  455. int32_t buffer_len = BUFSIZ;
  456. uint8_t *buffer = calloc(1, buffer_len);
  457. uint8_t *p;
  458. printf("Opening OUS_out\n");
  459. int32_t ous_fd = open("OUS_out", O_RDONLY);
  460. printf("Opened.\n");
  461. uint8_t *overflow;
  462. uint32_t overflow_len = 0;
  463. for(;;){
  464. int32_t bytes_read = read(ous_fd, buffer, buffer_len-overflow_len);
  465. if(bytes_read > 0){
  466. int32_t bytes_remaining = bytes_read;
  467. if(overflow_len > 0){
  468. //process first part of slitheen info
  469. printf("Completeing previously read header\n");
  470. memmove(buffer+overflow_len, buffer, bytes_read);
  471. memcpy(buffer, overflow, overflow_len);
  472. bytes_remaining += overflow_len;
  473. free(overflow);
  474. overflow_len = 0;
  475. }
  476. p = buffer;
  477. while(bytes_remaining > 0){
  478. if(bytes_remaining < SLITHEEN_HEADER_LEN){
  479. printf("Partial header: ");
  480. int i;
  481. for(i = 0; i< bytes_remaining; i++){
  482. printf("%02x ", p[i]);
  483. }
  484. printf("\n");
  485. }
  486. struct slitheen_hdr *sl_hdr = (struct slitheen_hdr *) p;
  487. //first see if sl_hdr corresponds to a valid stream. If not, ignore rest of read bytes
  488. #ifdef DEBUG
  489. printf("Slitheen header:\n");
  490. int i;
  491. for(i = 0; i< SLITHEEN_HEADER_LEN; i++){
  492. printf("%02x ", p[i]);
  493. }
  494. printf("\n");
  495. #endif
  496. p += sizeof(struct slitheen_hdr);
  497. if(sl_hdr->stream_id == 0){
  498. #ifdef DEBUG
  499. printf("Garbage bytes\n");
  500. #endif
  501. p += ntohs(sl_hdr->len);
  502. bytes_remaining -= sizeof(struct slitheen_hdr) + ntohs(sl_hdr->len);
  503. continue;
  504. }
  505. int32_t pipe_fd =-1;
  506. if(connections->first == NULL){
  507. printf("There are no connections\n");
  508. } else {
  509. connection *last = connections->first;
  510. if (last->stream_id == sl_hdr->stream_id){
  511. printf("Found stream id %d!\n", sl_hdr->stream_id);
  512. pipe_fd = last->pipe_fd;
  513. printf("Pipe fd: %d\n", pipe_fd);
  514. }
  515. while(last->next != NULL){
  516. last = last->next;
  517. if (last->stream_id == sl_hdr->stream_id){
  518. printf("Found stream id %d!\n", sl_hdr->stream_id);
  519. pipe_fd = last->pipe_fd;
  520. printf("Pipe fd: %d\n", pipe_fd);
  521. }
  522. }
  523. }
  524. if(pipe_fd == -1){
  525. printf("No stream id exists. Possibly invalid header\n");
  526. break;
  527. }
  528. if(ntohs(sl_hdr->len)+ sizeof(struct slitheen_hdr) > bytes_remaining){
  529. overflow = calloc(1, bytes_remaining);
  530. memcpy(overflow, p, bytes_remaining);
  531. overflow_len = bytes_remaining;
  532. bytes_remaining = 0;
  533. break;
  534. }
  535. if(sl_hdr->garbage_len == 0){
  536. printf("Received information for stream id: %d of length: %u\n", sl_hdr->stream_id, ntohs(sl_hdr->len));
  537. int32_t bytes_sent = write(pipe_fd, p, ntohs(sl_hdr->len));
  538. if(bytes_sent <= 0){
  539. printf("Error reading to pipe for stream id %d\n", sl_hdr->stream_id);
  540. }
  541. }
  542. p += ntohs(sl_hdr->len);
  543. bytes_remaining -= sizeof(struct slitheen_hdr) + ntohs(sl_hdr->len);
  544. }
  545. } else {
  546. printf("Error: read %d bytes from OUS_out\n", bytes_read);
  547. printf("Opening OUS_out\n");
  548. close(ous_fd);
  549. ous_fd = open("OUS_out", O_RDONLY);
  550. printf("Opened.\n");
  551. }
  552. }
  553. close(ous_fd);
  554. }