2011-02-25 09:40:11 +00:00
|
|
|
/***************************************************************************
|
|
|
|
libproxychains.c - description
|
|
|
|
-------------------
|
|
|
|
begin : Tue May 14 2002
|
|
|
|
copyright : netcreature (C) 2002
|
|
|
|
email : netcreature@users.sourceforge.net
|
|
|
|
***************************************************************************/
|
|
|
|
/* GPL */
|
|
|
|
/***************************************************************************
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
***************************************************************************/
|
2011-09-03 23:45:16 +00:00
|
|
|
|
|
|
|
#undef _GNU_SOURCE
|
2011-02-25 09:40:11 +00:00
|
|
|
#define _GNU_SOURCE
|
2011-09-03 23:45:16 +00:00
|
|
|
|
2011-02-25 09:40:11 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
2011-09-02 17:55:50 +00:00
|
|
|
#include <string.h>
|
2011-02-25 09:40:11 +00:00
|
|
|
#include <errno.h>
|
2012-11-07 15:49:14 +00:00
|
|
|
#include <assert.h>
|
2011-02-25 09:40:11 +00:00
|
|
|
#include <netdb.h>
|
|
|
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
2011-09-02 17:55:50 +00:00
|
|
|
#include <fcntl.h>
|
2011-02-25 09:40:11 +00:00
|
|
|
#include <dlfcn.h>
|
2012-11-07 20:28:09 +00:00
|
|
|
#include <pthread.h>
|
2011-02-25 09:40:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
#include "core.h"
|
2011-09-10 20:32:27 +00:00
|
|
|
#include "common.h"
|
2011-02-25 09:40:11 +00:00
|
|
|
|
|
|
|
#define satosin(x) ((struct sockaddr_in *) &(x))
|
|
|
|
#define SOCKADDR(x) (satosin(x)->sin_addr.s_addr)
|
|
|
|
#define SOCKADDR_2(x) (satosin(x)->sin_addr)
|
|
|
|
#define SOCKPORT(x) (satosin(x)->sin_port)
|
|
|
|
#define SOCKFAMILY(x) (satosin(x)->sin_family)
|
2012-04-23 23:48:17 +00:00
|
|
|
#define MAX_CHAIN 512
|
2011-02-25 09:40:11 +00:00
|
|
|
|
2018-01-10 17:23:36 +00:00
|
|
|
#ifdef IS_SOLARIS
|
|
|
|
#undef connect
|
|
|
|
int __xnet_connect(int sock, const struct sockaddr *addr, unsigned int len);
|
|
|
|
connect_t true___xnet_connect;
|
|
|
|
#endif
|
|
|
|
|
2013-01-21 00:54:45 +00:00
|
|
|
close_t true_close;
|
2012-07-07 22:16:00 +00:00
|
|
|
connect_t true_connect;
|
|
|
|
gethostbyname_t true_gethostbyname;
|
|
|
|
getaddrinfo_t true_getaddrinfo;
|
|
|
|
freeaddrinfo_t true_freeaddrinfo;
|
|
|
|
getnameinfo_t true_getnameinfo;
|
|
|
|
gethostbyaddr_t true_gethostbyaddr;
|
2015-01-23 16:14:28 +00:00
|
|
|
sendto_t true_sendto;
|
2012-07-07 22:16:00 +00:00
|
|
|
|
2011-02-25 09:40:11 +00:00
|
|
|
int tcp_read_time_out;
|
|
|
|
int tcp_connect_time_out;
|
|
|
|
chain_type proxychains_ct;
|
|
|
|
proxy_data proxychains_pd[MAX_CHAIN];
|
2011-09-03 23:45:16 +00:00
|
|
|
unsigned int proxychains_proxy_count = 0;
|
2013-06-23 05:13:40 +00:00
|
|
|
unsigned int proxychains_proxy_offset = 0;
|
2011-02-25 09:40:11 +00:00
|
|
|
int proxychains_got_chain_data = 0;
|
2011-09-03 23:45:16 +00:00
|
|
|
unsigned int proxychains_max_chain = 1;
|
2011-02-25 09:40:11 +00:00
|
|
|
int proxychains_quiet_mode = 0;
|
|
|
|
int proxychains_resolver = 0;
|
2011-02-25 15:16:58 +00:00
|
|
|
localaddr_arg localnet_addr[MAX_LOCALNET];
|
|
|
|
size_t num_localnet_addr = 0;
|
2012-01-26 11:44:42 +00:00
|
|
|
unsigned int remote_dns_subnet = 224;
|
2011-02-25 15:16:58 +00:00
|
|
|
|
2012-04-23 20:14:04 +00:00
|
|
|
pthread_once_t init_once = PTHREAD_ONCE_INIT;
|
2012-11-07 20:28:09 +00:00
|
|
|
|
2012-04-23 20:14:04 +00:00
|
|
|
static int init_l = 0;
|
|
|
|
|
2012-01-27 17:59:44 +00:00
|
|
|
static inline void get_chain_data(proxy_data * pd, unsigned int *proxy_count, chain_type * ct);
|
2011-02-25 09:40:11 +00:00
|
|
|
|
2012-07-07 22:27:46 +00:00
|
|
|
static void* load_sym(char* symname, void* proxyfunc) {
|
2012-01-27 16:55:37 +00:00
|
|
|
|
2012-07-07 22:27:46 +00:00
|
|
|
void *funcptr = dlsym(RTLD_NEXT, symname);
|
2014-07-21 11:18:20 +00:00
|
|
|
|
2012-07-07 22:27:46 +00:00
|
|
|
if(!funcptr) {
|
2012-04-23 17:51:14 +00:00
|
|
|
fprintf(stderr, "Cannot load symbol '%s' %s\n", symname, dlerror());
|
2011-02-25 09:40:11 +00:00
|
|
|
exit(1);
|
|
|
|
} else {
|
2012-07-08 02:09:50 +00:00
|
|
|
PDEBUG("loaded symbol '%s'" " real addr %p wrapped addr %p\n", symname, funcptr, proxyfunc);
|
2011-02-25 09:40:11 +00:00
|
|
|
}
|
2012-07-07 22:27:46 +00:00
|
|
|
if(funcptr == proxyfunc) {
|
2011-09-03 23:45:16 +00:00
|
|
|
PDEBUG("circular reference detected, aborting!\n");
|
|
|
|
abort();
|
|
|
|
}
|
2012-07-07 22:27:46 +00:00
|
|
|
return funcptr;
|
2012-04-23 17:51:14 +00:00
|
|
|
}
|
2012-01-27 16:55:37 +00:00
|
|
|
|
2012-04-23 20:14:04 +00:00
|
|
|
#define INIT() init_lib_wrapper(__FUNCTION__)
|
|
|
|
|
2016-05-26 09:48:32 +00:00
|
|
|
#define SETUP_SYM(X) do { if (! true_ ## X ) true_ ## X = load_sym( # X, X ); } while(0)
|
2012-07-07 22:27:46 +00:00
|
|
|
|
2012-11-07 15:49:14 +00:00
|
|
|
#include "allocator_thread.h"
|
|
|
|
|
2014-07-22 12:10:08 +00:00
|
|
|
const char *proxychains_get_version(void);
|
|
|
|
|
2015-12-02 12:14:58 +00:00
|
|
|
static void setup_hooks(void) {
|
|
|
|
SETUP_SYM(connect);
|
|
|
|
SETUP_SYM(sendto);
|
|
|
|
SETUP_SYM(gethostbyname);
|
|
|
|
SETUP_SYM(getaddrinfo);
|
|
|
|
SETUP_SYM(freeaddrinfo);
|
|
|
|
SETUP_SYM(gethostbyaddr);
|
|
|
|
SETUP_SYM(getnameinfo);
|
|
|
|
SETUP_SYM(close);
|
2018-01-10 17:23:36 +00:00
|
|
|
#ifdef IS_SOLARIS
|
|
|
|
SETUP_SYM(__xnet_connect);
|
|
|
|
#endif
|
2015-12-02 12:14:58 +00:00
|
|
|
}
|
|
|
|
|
2016-05-26 18:01:14 +00:00
|
|
|
static int close_fds[16];
|
|
|
|
static int close_fds_cnt = 0;
|
|
|
|
|
2012-04-23 20:14:04 +00:00
|
|
|
static void do_init(void) {
|
2012-11-07 19:11:14 +00:00
|
|
|
srand(time(NULL));
|
2012-11-07 20:28:09 +00:00
|
|
|
core_initialize();
|
2012-11-07 19:11:14 +00:00
|
|
|
at_init();
|
2014-07-21 11:18:20 +00:00
|
|
|
|
2012-04-23 17:51:14 +00:00
|
|
|
/* read the config file */
|
|
|
|
get_chain_data(proxychains_pd, &proxychains_proxy_count, &proxychains_ct);
|
2013-06-23 05:13:40 +00:00
|
|
|
DUMP_PROXY_CHAIN(proxychains_pd, proxychains_proxy_count);
|
2011-02-25 09:40:11 +00:00
|
|
|
|
2014-07-22 12:10:08 +00:00
|
|
|
proxychains_write_log(LOG_PREFIX "DLL init: proxychains-ng %s\n", proxychains_get_version());
|
2014-07-21 11:18:20 +00:00
|
|
|
|
2015-12-02 12:14:58 +00:00
|
|
|
setup_hooks();
|
2014-07-21 11:18:20 +00:00
|
|
|
|
2016-05-26 18:01:14 +00:00
|
|
|
while(close_fds_cnt) true_close(close_fds[--close_fds_cnt]);
|
|
|
|
|
2011-02-25 09:40:11 +00:00
|
|
|
init_l = 1;
|
|
|
|
}
|
|
|
|
|
2012-04-23 20:14:04 +00:00
|
|
|
static void init_lib_wrapper(const char* caller) {
|
|
|
|
#ifndef DEBUG
|
|
|
|
(void) caller;
|
|
|
|
#endif
|
|
|
|
if(!init_l) PDEBUG("%s called from %s\n", __FUNCTION__, caller);
|
|
|
|
pthread_once(&init_once, do_init);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if we use gcc >= 3, we can instruct the dynamic loader
|
|
|
|
* to call init_lib at link time. otherwise it gets loaded
|
|
|
|
* lazily, which has the disadvantage that there's a potential
|
|
|
|
* race condition if 2 threads call it before init_l is set
|
|
|
|
* and PTHREAD support was disabled */
|
|
|
|
#if __GNUC__ > 2
|
|
|
|
__attribute__((constructor))
|
|
|
|
static void gcc_init(void) {
|
|
|
|
INIT();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-07-26 22:12:00 +00:00
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
RS_PT_NONE = 0,
|
|
|
|
RS_PT_SOCKS4,
|
|
|
|
RS_PT_SOCKS5,
|
|
|
|
RS_PT_HTTP
|
|
|
|
} rs_proxyType;
|
|
|
|
|
|
|
|
/*
|
|
|
|
proxy_from_string() taken from rocksock network I/O library (C) rofl0r
|
|
|
|
valid inputs:
|
|
|
|
socks5://user:password@proxy.domain.com:port
|
|
|
|
socks5://proxy.domain.com:port
|
|
|
|
socks4://proxy.domain.com:port
|
|
|
|
http://user:password@proxy.domain.com:port
|
|
|
|
http://proxy.domain.com:port
|
|
|
|
|
|
|
|
supplying port number is obligatory.
|
|
|
|
user:pass@ part is optional for http and socks5.
|
|
|
|
however, user:pass authentication is currently not implemented for http proxies.
|
|
|
|
return 1 on success, 0 on error.
|
|
|
|
*/
|
|
|
|
static int proxy_from_string(const char *proxystring,
|
|
|
|
char *type_buf,
|
|
|
|
char* host_buf,
|
|
|
|
int *port_n,
|
|
|
|
char *user_buf,
|
|
|
|
char* pass_buf)
|
|
|
|
{
|
|
|
|
const char* p;
|
|
|
|
rs_proxyType proxytype;
|
|
|
|
|
|
|
|
size_t next_token = 6, ul = 0, pl = 0, hl;
|
|
|
|
if(!proxystring[0] || !proxystring[1] || !proxystring[2] || !proxystring[3] || !proxystring[4] || !proxystring[5]) goto inv_string;
|
|
|
|
if(*proxystring == 's') {
|
|
|
|
switch(proxystring[5]) {
|
|
|
|
case '5': proxytype = RS_PT_SOCKS5; break;
|
|
|
|
case '4': proxytype = RS_PT_SOCKS4; break;
|
|
|
|
default: goto inv_string;
|
|
|
|
}
|
|
|
|
} else if(*proxystring == 'h') {
|
|
|
|
proxytype = RS_PT_HTTP;
|
|
|
|
next_token = 4;
|
|
|
|
} else goto inv_string;
|
|
|
|
if(
|
|
|
|
proxystring[next_token++] != ':' ||
|
|
|
|
proxystring[next_token++] != '/' ||
|
|
|
|
proxystring[next_token++] != '/') goto inv_string;
|
|
|
|
const char *at = strchr(proxystring+next_token, '@');
|
|
|
|
if(at) {
|
|
|
|
if(proxytype == RS_PT_SOCKS4)
|
|
|
|
return 0;
|
|
|
|
p = strchr(proxystring+next_token, ':');
|
|
|
|
if(!p || p >= at) goto inv_string;
|
|
|
|
const char *u = proxystring+next_token;
|
|
|
|
ul = p-u;
|
|
|
|
p++;
|
|
|
|
pl = at-p;
|
|
|
|
if(proxytype == RS_PT_SOCKS5 && (ul > 255 || pl > 255))
|
|
|
|
return 0;
|
|
|
|
memcpy(user_buf, u, ul);
|
|
|
|
user_buf[ul]=0;
|
|
|
|
memcpy(pass_buf, p, pl);
|
|
|
|
pass_buf[pl]=0;
|
|
|
|
next_token += 2+ul+pl;
|
|
|
|
} else {
|
|
|
|
user_buf[0]=0;
|
|
|
|
pass_buf[0]=0;
|
|
|
|
}
|
|
|
|
const char* h = proxystring+next_token;
|
|
|
|
p = strchr(h, ':');
|
|
|
|
if(!p) goto inv_string;
|
|
|
|
hl = p-h;
|
|
|
|
if(hl > 255)
|
|
|
|
return 0;
|
|
|
|
memcpy(host_buf, h, hl);
|
|
|
|
host_buf[hl]=0;
|
|
|
|
*port_n = atoi(p+1);
|
|
|
|
switch(proxytype) {
|
|
|
|
case RS_PT_SOCKS4:
|
|
|
|
strcpy(type_buf, "socks4");
|
|
|
|
break;
|
|
|
|
case RS_PT_SOCKS5:
|
|
|
|
strcpy(type_buf, "socks5");
|
|
|
|
break;
|
|
|
|
case RS_PT_HTTP:
|
|
|
|
strcpy(type_buf, "http");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
inv_string:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-23 20:14:04 +00:00
|
|
|
/* get configuration from config file */
|
|
|
|
static void get_chain_data(proxy_data * pd, unsigned int *proxy_count, chain_type * ct) {
|
2012-01-27 17:59:44 +00:00
|
|
|
int count = 0, port_n = 0, list = 0;
|
|
|
|
char buff[1024], type[1024], host[1024], user[1024];
|
2011-02-25 12:21:34 +00:00
|
|
|
char *env;
|
2011-03-22 14:04:52 +00:00
|
|
|
char local_in_addr_port[32];
|
|
|
|
char local_in_addr[32], local_in_port[32], local_netmask[32];
|
2012-07-08 21:32:50 +00:00
|
|
|
FILE *file = NULL;
|
2011-02-25 09:40:11 +00:00
|
|
|
|
|
|
|
if(proxychains_got_chain_data)
|
2012-01-27 17:59:44 +00:00
|
|
|
return;
|
2011-02-25 09:40:11 +00:00
|
|
|
|
|
|
|
//Some defaults
|
2012-01-27 17:59:44 +00:00
|
|
|
tcp_read_time_out = 4 * 1000;
|
|
|
|
tcp_connect_time_out = 10 * 1000;
|
2011-09-03 23:45:16 +00:00
|
|
|
*ct = DYNAMIC_TYPE;
|
2014-07-21 11:18:20 +00:00
|
|
|
|
2012-07-08 21:47:56 +00:00
|
|
|
env = get_config_path(getenv(PROXYCHAINS_CONF_FILE_ENV_VAR), buff, sizeof(buff));
|
2013-04-30 08:06:36 +00:00
|
|
|
if( ( file = fopen(env, "r") ) == NULL )
|
|
|
|
{
|
|
|
|
perror("couldnt read configuration file");
|
|
|
|
exit(1);
|
|
|
|
}
|
2012-01-27 16:55:37 +00:00
|
|
|
|
2012-01-24 07:26:37 +00:00
|
|
|
env = getenv(PROXYCHAINS_QUIET_MODE_ENV_VAR);
|
2012-01-27 17:59:44 +00:00
|
|
|
if(env && *env == '1')
|
|
|
|
proxychains_quiet_mode = 1;
|
2011-02-25 09:40:11 +00:00
|
|
|
|
2012-01-27 17:59:44 +00:00
|
|
|
while(fgets(buff, sizeof(buff), file)) {
|
|
|
|
if(buff[0] != '\n' && buff[strspn(buff, " ")] != '#') {
|
2012-04-24 00:04:02 +00:00
|
|
|
/* proxylist has to come last */
|
2011-02-25 09:40:11 +00:00
|
|
|
if(list) {
|
2012-04-24 00:04:02 +00:00
|
|
|
if(count >= MAX_CHAIN)
|
|
|
|
break;
|
2014-07-21 11:18:20 +00:00
|
|
|
|
2011-09-02 17:55:50 +00:00
|
|
|
memset(&pd[count], 0, sizeof(proxy_data));
|
2012-01-27 16:55:37 +00:00
|
|
|
|
2011-11-06 13:12:50 +00:00
|
|
|
pd[count].ps = PLAY_STATE;
|
|
|
|
port_n = 0;
|
2012-01-27 16:55:37 +00:00
|
|
|
|
2014-07-21 11:17:24 +00:00
|
|
|
int ret = sscanf(buff, "%s %s %d %s %s", type, host, &port_n, pd[count].user, pd[count].pass);
|
|
|
|
if(ret < 3 || ret == EOF) {
|
2018-07-26 22:12:00 +00:00
|
|
|
if(!proxy_from_string(buff, type, host, &port_n, pd[count].user, pd[count].pass)) {
|
|
|
|
inv:
|
|
|
|
fprintf(stderr, "error: invalid item in proxylist section: %s", buff);
|
|
|
|
exit(1);
|
|
|
|
}
|
2014-07-21 11:17:24 +00:00
|
|
|
}
|
2012-01-27 16:55:37 +00:00
|
|
|
|
2015-08-10 15:59:31 +00:00
|
|
|
memset(&pd[count].ip, 0, sizeof(pd[count].ip));
|
|
|
|
pd[count].ip.is_v6 = !!strchr(host, ':');
|
|
|
|
pd[count].port = htons((unsigned short) port_n);
|
|
|
|
ip_type* host_ip = &pd[count].ip;
|
|
|
|
if(1 != inet_pton(host_ip->is_v6 ? AF_INET6 : AF_INET, host, host_ip->addr.v6)) {
|
2013-05-02 11:40:56 +00:00
|
|
|
fprintf(stderr, "proxy %s has invalid value or is not numeric\n", host);
|
|
|
|
exit(1);
|
|
|
|
}
|
2012-01-27 16:55:37 +00:00
|
|
|
|
2012-01-27 17:59:44 +00:00
|
|
|
if(!strcmp(type, "http")) {
|
2011-11-06 13:12:50 +00:00
|
|
|
pd[count].pt = HTTP_TYPE;
|
2012-01-27 17:59:44 +00:00
|
|
|
} else if(!strcmp(type, "socks4")) {
|
2011-11-06 13:12:50 +00:00
|
|
|
pd[count].pt = SOCKS4_TYPE;
|
2012-01-27 17:59:44 +00:00
|
|
|
} else if(!strcmp(type, "socks5")) {
|
2011-11-06 13:12:50 +00:00
|
|
|
pd[count].pt = SOCKS5_TYPE;
|
2012-01-27 16:55:37 +00:00
|
|
|
} else
|
2014-07-21 11:17:24 +00:00
|
|
|
goto inv;
|
2012-01-27 16:55:37 +00:00
|
|
|
|
2015-08-10 15:59:31 +00:00
|
|
|
if(port_n)
|
2012-04-24 00:04:02 +00:00
|
|
|
count++;
|
2012-01-27 17:59:44 +00:00
|
|
|
} else {
|
|
|
|
if(strstr(buff, "[ProxyList]")) {
|
|
|
|
list = 1;
|
2012-01-26 11:44:42 +00:00
|
|
|
} else if(strstr(buff, "random_chain")) {
|
2012-01-27 17:59:44 +00:00
|
|
|
*ct = RANDOM_TYPE;
|
2012-01-26 11:44:42 +00:00
|
|
|
} else if(strstr(buff, "strict_chain")) {
|
2012-01-27 17:59:44 +00:00
|
|
|
*ct = STRICT_TYPE;
|
2012-01-26 11:44:42 +00:00
|
|
|
} else if(strstr(buff, "dynamic_chain")) {
|
2012-01-27 17:59:44 +00:00
|
|
|
*ct = DYNAMIC_TYPE;
|
2013-06-23 05:13:40 +00:00
|
|
|
} else if(strstr(buff, "round_robin_chain")) {
|
|
|
|
*ct = ROUND_ROBIN_TYPE;
|
2012-01-27 17:59:44 +00:00
|
|
|
} else if(strstr(buff, "tcp_read_time_out")) {
|
2012-01-26 11:44:42 +00:00
|
|
|
sscanf(buff, "%s %d", user, &tcp_read_time_out);
|
2012-01-27 17:59:44 +00:00
|
|
|
} else if(strstr(buff, "tcp_connect_time_out")) {
|
2012-01-26 11:44:42 +00:00
|
|
|
sscanf(buff, "%s %d", user, &tcp_connect_time_out);
|
2012-01-27 17:59:44 +00:00
|
|
|
} else if(strstr(buff, "remote_dns_subnet")) {
|
2016-12-09 04:42:55 +00:00
|
|
|
sscanf(buff, "%s %u", user, &remote_dns_subnet);
|
2012-01-26 11:44:42 +00:00
|
|
|
if(remote_dns_subnet >= 256) {
|
2012-01-27 17:59:44 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"remote_dns_subnet: invalid value. requires a number between 0 and 255.\n");
|
2012-01-26 11:44:42 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
} else if(strstr(buff, "localnet")) {
|
2012-01-27 17:59:44 +00:00
|
|
|
if(sscanf(buff, "%s %21[^/]/%15s", user, local_in_addr_port, local_netmask) < 3) {
|
2011-03-22 14:04:52 +00:00
|
|
|
fprintf(stderr, "localnet format error");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
/* clean previously used buffer */
|
2012-01-27 17:59:44 +00:00
|
|
|
memset(local_in_port, 0, sizeof(local_in_port) / sizeof(local_in_port[0]));
|
2011-03-22 14:04:52 +00:00
|
|
|
|
2012-01-27 17:59:44 +00:00
|
|
|
if(sscanf(local_in_addr_port, "%15[^:]:%5s", local_in_addr, local_in_port) < 2) {
|
2012-05-22 12:25:53 +00:00
|
|
|
PDEBUG("added localnet: netaddr=%s, netmask=%s\n",
|
2012-01-27 17:59:44 +00:00
|
|
|
local_in_addr, local_netmask);
|
2011-03-22 14:04:52 +00:00
|
|
|
} else {
|
2012-01-27 17:59:44 +00:00
|
|
|
PDEBUG("added localnet: netaddr=%s, port=%s, netmask=%s\n",
|
|
|
|
local_in_addr, local_in_port, local_netmask);
|
2011-03-22 14:04:52 +00:00
|
|
|
}
|
2012-01-27 17:59:44 +00:00
|
|
|
if(num_localnet_addr < MAX_LOCALNET) {
|
2011-02-25 15:16:58 +00:00
|
|
|
int error;
|
2012-01-27 17:59:44 +00:00
|
|
|
error =
|
|
|
|
inet_pton(AF_INET, local_in_addr,
|
|
|
|
&localnet_addr[num_localnet_addr].in_addr);
|
|
|
|
if(error <= 0) {
|
2011-02-25 15:16:58 +00:00
|
|
|
fprintf(stderr, "localnet address error\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2012-01-27 17:59:44 +00:00
|
|
|
error =
|
|
|
|
inet_pton(AF_INET, local_netmask,
|
|
|
|
&localnet_addr[num_localnet_addr].netmask);
|
|
|
|
if(error <= 0) {
|
2011-03-22 14:04:52 +00:00
|
|
|
fprintf(stderr, "localnet netmask error\n");
|
2011-02-25 15:16:58 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2012-01-27 17:59:44 +00:00
|
|
|
if(local_in_port[0]) {
|
|
|
|
localnet_addr[num_localnet_addr].port =
|
|
|
|
(short) atoi(local_in_port);
|
2011-03-22 14:04:52 +00:00
|
|
|
} else {
|
|
|
|
localnet_addr[num_localnet_addr].port = 0;
|
|
|
|
}
|
2011-02-25 15:16:58 +00:00
|
|
|
++num_localnet_addr;
|
2012-01-27 17:59:44 +00:00
|
|
|
} else {
|
2011-02-25 15:16:58 +00:00
|
|
|
fprintf(stderr, "# of localnet exceed %d.\n", MAX_LOCALNET);
|
|
|
|
}
|
2012-01-27 17:59:44 +00:00
|
|
|
} else if(strstr(buff, "chain_len")) {
|
|
|
|
char *pc;
|
|
|
|
int len;
|
|
|
|
pc = strchr(buff, '=');
|
2016-06-23 08:27:15 +00:00
|
|
|
if(!pc) {
|
|
|
|
fprintf(stderr, "error: missing equals sign '=' in chain_len directive.\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2012-01-27 17:59:44 +00:00
|
|
|
len = atoi(++pc);
|
|
|
|
proxychains_max_chain = (len ? len : 1);
|
|
|
|
} else if(strstr(buff, "quiet_mode")) {
|
|
|
|
proxychains_quiet_mode = 1;
|
|
|
|
} else if(strstr(buff, "proxy_dns")) {
|
|
|
|
proxychains_resolver = 1;
|
2011-02-25 09:40:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-04 12:49:43 +00:00
|
|
|
#ifndef BROKEN_FCLOSE
|
2011-02-25 09:40:11 +00:00
|
|
|
fclose(file);
|
2016-02-04 12:49:43 +00:00
|
|
|
#endif
|
2014-07-21 10:37:01 +00:00
|
|
|
if(!count) {
|
|
|
|
fprintf(stderr, "error: no valid proxy found in config\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2011-09-03 23:45:16 +00:00
|
|
|
*proxy_count = count;
|
|
|
|
proxychains_got_chain_data = 1;
|
2011-02-25 09:40:11 +00:00
|
|
|
}
|
|
|
|
|
2012-04-23 20:14:04 +00:00
|
|
|
/******* HOOK FUNCTIONS *******/
|
2011-02-25 09:40:11 +00:00
|
|
|
|
2013-01-21 00:54:45 +00:00
|
|
|
int close(int fd) {
|
2016-05-26 09:48:32 +00:00
|
|
|
if(!init_l) {
|
2016-05-26 18:01:14 +00:00
|
|
|
if(close_fds_cnt>=(sizeof close_fds/sizeof close_fds[0])) goto err;
|
|
|
|
close_fds[close_fds_cnt++] = fd;
|
|
|
|
errno = 0;
|
|
|
|
return 0;
|
2016-05-26 09:48:32 +00:00
|
|
|
}
|
2013-01-21 00:54:45 +00:00
|
|
|
/* prevent rude programs (like ssh) from closing our pipes */
|
|
|
|
if(fd != req_pipefd[0] && fd != req_pipefd[1] &&
|
|
|
|
fd != resp_pipefd[0] && fd != resp_pipefd[1]) {
|
|
|
|
return true_close(fd);
|
|
|
|
}
|
2016-05-26 18:01:14 +00:00
|
|
|
err:
|
2014-01-08 05:38:59 +00:00
|
|
|
errno = EBADF;
|
2013-01-21 00:54:45 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2015-09-15 20:16:20 +00:00
|
|
|
static int is_v4inv6(const struct in6_addr *a) {
|
2018-01-09 13:30:02 +00:00
|
|
|
return !memcmp(a->s6_addr, "\0\0\0\0\0\0\0\0\0\0\xff\xff", 12);
|
2015-09-15 20:16:20 +00:00
|
|
|
}
|
2012-01-27 17:59:44 +00:00
|
|
|
int connect(int sock, const struct sockaddr *addr, unsigned int len) {
|
2015-06-14 10:16:59 +00:00
|
|
|
INIT();
|
2012-11-07 15:49:14 +00:00
|
|
|
PFUNC();
|
2015-06-14 10:16:59 +00:00
|
|
|
|
2012-01-27 17:59:44 +00:00
|
|
|
int socktype = 0, flags = 0, ret = 0;
|
2011-09-03 23:45:16 +00:00
|
|
|
socklen_t optlen = 0;
|
2011-11-06 13:12:50 +00:00
|
|
|
ip_type dest_ip;
|
2015-06-14 09:53:33 +00:00
|
|
|
DEBUGDECL(char str[256]);
|
|
|
|
|
2011-02-25 15:16:58 +00:00
|
|
|
struct in_addr *p_addr_in;
|
2015-08-10 15:59:31 +00:00
|
|
|
struct in6_addr *p_addr_in6;
|
2011-03-22 14:04:52 +00:00
|
|
|
unsigned short port;
|
2011-02-25 15:16:58 +00:00
|
|
|
size_t i;
|
2012-03-03 06:24:05 +00:00
|
|
|
int remote_dns_connect = 0;
|
2012-01-27 17:59:44 +00:00
|
|
|
optlen = sizeof(socktype);
|
2015-08-10 15:59:31 +00:00
|
|
|
sa_family_t fam = SOCKFAMILY(*addr);
|
2012-01-27 17:59:44 +00:00
|
|
|
getsockopt(sock, SOL_SOCKET, SO_TYPE, &socktype, &optlen);
|
2015-08-10 15:59:31 +00:00
|
|
|
if(!((fam == AF_INET || fam == AF_INET6) && socktype == SOCK_STREAM))
|
2012-01-27 17:59:44 +00:00
|
|
|
return true_connect(sock, addr, len);
|
2011-02-25 15:16:58 +00:00
|
|
|
|
2015-08-10 15:59:31 +00:00
|
|
|
int v6 = dest_ip.is_v6 = fam == AF_INET6;
|
|
|
|
|
2012-01-27 17:59:44 +00:00
|
|
|
p_addr_in = &((struct sockaddr_in *) addr)->sin_addr;
|
2015-08-10 15:59:31 +00:00
|
|
|
p_addr_in6 = &((struct sockaddr_in6 *) addr)->sin6_addr;
|
|
|
|
port = !v6 ? ntohs(((struct sockaddr_in *) addr)->sin_port)
|
|
|
|
: ntohs(((struct sockaddr_in6 *) addr)->sin6_port);
|
2015-09-15 20:16:20 +00:00
|
|
|
struct in_addr v4inv6;
|
|
|
|
if(v6 && is_v4inv6(p_addr_in6)) {
|
2018-01-09 13:30:02 +00:00
|
|
|
memcpy(&v4inv6.s_addr, &p_addr_in6->s6_addr[12], 4);
|
2015-09-15 20:16:20 +00:00
|
|
|
v6 = dest_ip.is_v6 = 0;
|
|
|
|
p_addr_in = &v4inv6;
|
|
|
|
}
|
2011-02-25 15:16:58 +00:00
|
|
|
|
2012-01-27 17:59:44 +00:00
|
|
|
// PDEBUG("localnet: %s; ", inet_ntop(AF_INET,&in_addr_localnet, str, sizeof(str)));
|
|
|
|
// PDEBUG("netmask: %s; " , inet_ntop(AF_INET, &in_addr_netmask, str, sizeof(str)));
|
2015-12-06 12:57:45 +00:00
|
|
|
PDEBUG("target: %s\n", inet_ntop(v6 ? AF_INET6 : AF_INET, v6 ? (void*)p_addr_in6 : (void*)p_addr_in, str, sizeof(str)));
|
2011-09-03 23:45:16 +00:00
|
|
|
PDEBUG("port: %d\n", port);
|
2012-03-03 06:24:05 +00:00
|
|
|
|
|
|
|
// check if connect called from proxydns
|
2015-08-10 15:59:31 +00:00
|
|
|
remote_dns_connect = !v6 && (ntohl(p_addr_in->s_addr) >> 24 == remote_dns_subnet);
|
2012-03-03 06:24:05 +00:00
|
|
|
|
2015-08-10 15:59:31 +00:00
|
|
|
if (!v6) for(i = 0; i < num_localnet_addr && !remote_dns_connect; i++) {
|
2012-01-27 17:59:44 +00:00
|
|
|
if((localnet_addr[i].in_addr.s_addr & localnet_addr[i].netmask.s_addr)
|
|
|
|
== (p_addr_in->s_addr & localnet_addr[i].netmask.s_addr)) {
|
2012-05-22 12:25:53 +00:00
|
|
|
if(!localnet_addr[i].port || localnet_addr[i].port == port) {
|
2011-03-22 14:04:52 +00:00
|
|
|
PDEBUG("accessing localnet using true_connect\n");
|
2012-01-27 17:59:44 +00:00
|
|
|
return true_connect(sock, addr, len);
|
2011-03-22 14:04:52 +00:00
|
|
|
}
|
2011-02-25 15:16:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-06 13:12:50 +00:00
|
|
|
flags = fcntl(sock, F_GETFL, 0);
|
2011-02-25 09:40:11 +00:00
|
|
|
if(flags & O_NONBLOCK)
|
2012-01-27 17:59:44 +00:00
|
|
|
fcntl(sock, F_SETFL, !O_NONBLOCK);
|
2012-01-27 16:55:37 +00:00
|
|
|
|
2015-08-10 15:59:31 +00:00
|
|
|
memcpy(dest_ip.addr.v6, v6 ? (void*)p_addr_in6 : (void*)p_addr_in, v6?16:4);
|
2012-01-27 16:55:37 +00:00
|
|
|
|
2012-01-27 17:59:44 +00:00
|
|
|
ret = connect_proxy_chain(sock,
|
|
|
|
dest_ip,
|
2015-08-10 15:59:31 +00:00
|
|
|
htons(port),
|
2012-01-27 17:59:44 +00:00
|
|
|
proxychains_pd, proxychains_proxy_count, proxychains_ct, proxychains_max_chain);
|
2012-01-27 16:55:37 +00:00
|
|
|
|
2011-02-25 09:40:11 +00:00
|
|
|
fcntl(sock, F_SETFL, flags);
|
2011-11-06 13:12:50 +00:00
|
|
|
if(ret != SUCCESS)
|
2012-01-27 17:59:44 +00:00
|
|
|
errno = ECONNREFUSED;
|
2011-02-25 09:40:11 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-01-10 17:23:36 +00:00
|
|
|
#ifdef IS_SOLARIS
|
|
|
|
int __xnet_connect(int sock, const struct sockaddr *addr, unsigned int len) {
|
|
|
|
return connect(sock, addr, len);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-07-15 23:05:28 +00:00
|
|
|
static struct gethostbyname_data ghbndata;
|
2012-01-27 17:59:44 +00:00
|
|
|
struct hostent *gethostbyname(const char *name) {
|
2012-04-23 20:14:04 +00:00
|
|
|
INIT();
|
2012-01-27 17:59:44 +00:00
|
|
|
PDEBUG("gethostbyname: %s\n", name);
|
2012-01-27 16:55:37 +00:00
|
|
|
|
2011-02-25 09:40:11 +00:00
|
|
|
if(proxychains_resolver)
|
2012-07-15 23:05:28 +00:00
|
|
|
return proxy_gethostbyname(name, &ghbndata);
|
2011-02-25 09:40:11 +00:00
|
|
|
else
|
|
|
|
return true_gethostbyname(name);
|
2012-01-27 16:55:37 +00:00
|
|
|
|
2011-02-25 09:40:11 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2011-09-03 23:45:16 +00:00
|
|
|
|
2012-01-27 17:59:44 +00:00
|
|
|
int getaddrinfo(const char *node, const char *service, const struct addrinfo *hints, struct addrinfo **res) {
|
2012-04-23 20:14:04 +00:00
|
|
|
INIT();
|
2012-11-08 00:18:19 +00:00
|
|
|
PDEBUG("getaddrinfo: %s %s\n", node ? node : "null", service ? service : "null");
|
2012-01-27 16:55:37 +00:00
|
|
|
|
2011-02-25 09:40:11 +00:00
|
|
|
if(proxychains_resolver)
|
2015-06-14 10:16:59 +00:00
|
|
|
return proxy_getaddrinfo(node, service, hints, res);
|
2011-02-25 09:40:11 +00:00
|
|
|
else
|
2015-06-14 10:16:59 +00:00
|
|
|
return true_getaddrinfo(node, service, hints, res);
|
2011-02-25 09:40:11 +00:00
|
|
|
}
|
2011-09-03 23:45:16 +00:00
|
|
|
|
2012-01-27 17:59:44 +00:00
|
|
|
void freeaddrinfo(struct addrinfo *res) {
|
2012-04-23 20:14:04 +00:00
|
|
|
INIT();
|
2016-12-09 04:40:51 +00:00
|
|
|
PDEBUG("freeaddrinfo %p \n", (void *) res);
|
2012-01-27 16:55:37 +00:00
|
|
|
|
2011-02-25 09:40:11 +00:00
|
|
|
if(!proxychains_resolver)
|
|
|
|
true_freeaddrinfo(res);
|
2012-07-15 23:05:28 +00:00
|
|
|
else
|
|
|
|
proxy_freeaddrinfo(res);
|
2011-02-25 09:40:11 +00:00
|
|
|
}
|
2012-01-27 17:59:44 +00:00
|
|
|
|
2014-07-21 11:18:20 +00:00
|
|
|
int pc_getnameinfo(const struct sockaddr *sa, socklen_t salen,
|
|
|
|
char *host, socklen_t hostlen, char *serv,
|
2012-12-17 21:17:00 +00:00
|
|
|
socklen_t servlen, int flags)
|
2011-02-25 09:40:11 +00:00
|
|
|
{
|
2012-04-23 20:14:04 +00:00
|
|
|
INIT();
|
2012-12-17 21:17:00 +00:00
|
|
|
PFUNC();
|
2012-01-27 16:55:37 +00:00
|
|
|
|
2011-02-25 09:40:11 +00:00
|
|
|
if(!proxychains_resolver) {
|
2015-06-14 10:16:59 +00:00
|
|
|
return true_getnameinfo(sa, salen, host, hostlen, serv, servlen, flags);
|
2011-02-25 09:40:11 +00:00
|
|
|
} else {
|
2015-12-06 13:01:56 +00:00
|
|
|
if(!salen || !(SOCKFAMILY(*sa) == AF_INET || SOCKFAMILY(*sa) == AF_INET6))
|
|
|
|
return EAI_FAMILY;
|
|
|
|
int v6 = SOCKFAMILY(*sa) == AF_INET6;
|
|
|
|
if(salen < (v6?sizeof(struct sockaddr_in6):sizeof(struct sockaddr_in)))
|
2012-12-17 22:21:58 +00:00
|
|
|
return EAI_FAMILY;
|
2012-01-27 19:00:22 +00:00
|
|
|
if(hostlen) {
|
2015-12-06 13:01:56 +00:00
|
|
|
unsigned char v4inv6buf[4];
|
|
|
|
const void *ip = v6 ? (void*)&((struct sockaddr_in6*)sa)->sin6_addr
|
|
|
|
: (void*)&((struct sockaddr_in*)sa)->sin_addr;
|
|
|
|
unsigned scopeid = 0;
|
|
|
|
if(v6) {
|
|
|
|
if(is_v4inv6(&((struct sockaddr_in6*)sa)->sin6_addr)) {
|
2018-01-09 13:30:02 +00:00
|
|
|
memcpy(v4inv6buf, &((struct sockaddr_in6*)sa)->sin6_addr.s6_addr[12], 4);
|
2015-12-06 13:01:56 +00:00
|
|
|
ip = v4inv6buf;
|
|
|
|
v6 = 0;
|
|
|
|
} else
|
|
|
|
scopeid = ((struct sockaddr_in6 *)sa)->sin6_scope_id;
|
|
|
|
}
|
|
|
|
if(!inet_ntop(v6?AF_INET6:AF_INET,ip,host,hostlen))
|
2012-12-17 21:41:51 +00:00
|
|
|
return EAI_OVERFLOW;
|
2015-12-06 13:01:56 +00:00
|
|
|
if(scopeid) {
|
|
|
|
size_t l = strlen(host);
|
|
|
|
if(snprintf(host+l, hostlen-l, "%%%u", scopeid) >= hostlen-l)
|
|
|
|
return EAI_OVERFLOW;
|
|
|
|
}
|
2012-12-17 21:41:51 +00:00
|
|
|
}
|
|
|
|
if(servlen) {
|
|
|
|
if(snprintf(serv, servlen, "%d", ntohs(SOCKPORT(*sa))) >= servlen)
|
|
|
|
return EAI_OVERFLOW;
|
2012-01-27 19:00:22 +00:00
|
|
|
}
|
2011-02-25 09:40:11 +00:00
|
|
|
}
|
2015-06-14 10:16:59 +00:00
|
|
|
return 0;
|
2011-02-25 09:40:11 +00:00
|
|
|
}
|
2011-02-25 15:16:58 +00:00
|
|
|
|
2012-01-27 17:59:44 +00:00
|
|
|
struct hostent *gethostbyaddr(const void *addr, socklen_t len, int type) {
|
2015-06-14 10:16:59 +00:00
|
|
|
INIT();
|
|
|
|
PDEBUG("TODO: proper gethostbyaddr hook\n");
|
|
|
|
|
2011-09-03 23:45:16 +00:00
|
|
|
static char buf[16];
|
|
|
|
static char ipv4[4];
|
2012-01-27 17:59:44 +00:00
|
|
|
static char *list[2];
|
2012-11-04 03:58:48 +00:00
|
|
|
static char *aliases[1];
|
2011-09-03 23:45:16 +00:00
|
|
|
static struct hostent he;
|
2012-01-27 16:55:37 +00:00
|
|
|
|
2011-02-25 09:40:11 +00:00
|
|
|
if(!proxychains_resolver)
|
2012-01-27 17:59:44 +00:00
|
|
|
return true_gethostbyaddr(addr, len, type);
|
2011-09-03 23:45:16 +00:00
|
|
|
else {
|
2012-01-27 16:55:37 +00:00
|
|
|
|
2011-09-04 00:03:47 +00:00
|
|
|
PDEBUG("len %u\n", len);
|
2012-01-27 17:59:44 +00:00
|
|
|
if(len != 4)
|
|
|
|
return NULL;
|
2011-09-03 23:45:16 +00:00
|
|
|
he.h_name = buf;
|
|
|
|
memcpy(ipv4, addr, 4);
|
|
|
|
list[0] = ipv4;
|
|
|
|
list[1] = NULL;
|
|
|
|
he.h_addr_list = list;
|
|
|
|
he.h_addrtype = AF_INET;
|
2012-11-04 03:58:48 +00:00
|
|
|
aliases[0] = NULL;
|
|
|
|
he.h_aliases = aliases;
|
2011-09-03 23:45:16 +00:00
|
|
|
he.h_length = 4;
|
2012-01-27 17:59:44 +00:00
|
|
|
pc_stringfromipv4((unsigned char *) addr, buf);
|
2011-09-03 23:45:16 +00:00
|
|
|
return &he;
|
|
|
|
}
|
2011-02-25 09:40:11 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2015-01-23 16:14:28 +00:00
|
|
|
|
2015-02-17 14:27:01 +00:00
|
|
|
#ifndef MSG_FASTOPEN
|
|
|
|
# define MSG_FASTOPEN 0x20000000
|
|
|
|
#endif
|
|
|
|
|
2015-01-23 16:14:28 +00:00
|
|
|
ssize_t sendto(int sockfd, const void *buf, size_t len, int flags,
|
|
|
|
const struct sockaddr *dest_addr, socklen_t addrlen) {
|
2015-06-14 10:16:59 +00:00
|
|
|
INIT();
|
|
|
|
PFUNC();
|
2015-01-23 16:14:28 +00:00
|
|
|
if (flags & MSG_FASTOPEN) {
|
|
|
|
if (!connect(sockfd, dest_addr, addrlen) && errno != EINPROGRESS) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
dest_addr = NULL;
|
|
|
|
addrlen = 0;
|
|
|
|
flags &= ~MSG_FASTOPEN;
|
|
|
|
}
|
|
|
|
return true_sendto(sockfd, buf, len, flags, dest_addr, addrlen);
|
|
|
|
}
|