proxychains-ng/src/libproxychains.c

471 lines
13 KiB
C
Raw Normal View History

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. *
* *
***************************************************************************/
#undef _GNU_SOURCE
2011-02-25 09:40:11 +00:00
#define _GNU_SOURCE
2011-02-25 09:40:11 +00:00
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
2011-02-25 09:40:11 +00:00
#include <errno.h>
#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>
#include <fcntl.h>
2011-02-25 09:40:11 +00:00
#include <dlfcn.h>
#include <pthread.h>
2011-02-25 09:40:11 +00:00
#include "core.h"
#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
close_t true_close;
connect_t true_connect;
gethostbyname_t true_gethostbyname;
getaddrinfo_t true_getaddrinfo;
freeaddrinfo_t true_freeaddrinfo;
getnameinfo_t true_getnameinfo;
gethostbyaddr_t true_gethostbyaddr;
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];
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;
unsigned int proxychains_max_chain = 1;
2011-02-25 09:40:11 +00:00
int proxychains_quiet_mode = 0;
int proxychains_resolver = 0;
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;
pthread_once_t init_once = PTHREAD_ONCE_INIT;
static int init_l = 0;
static inline void get_chain_data(proxy_data * pd, unsigned int *proxy_count, chain_type * ct);
2011-02-25 09:40:11 +00:00
static void* load_sym(char* symname, void* proxyfunc) {
2012-01-27 16:55:37 +00:00
void *funcptr = dlsym(RTLD_NEXT, symname);
2012-04-23 17:51:14 +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
}
if(funcptr == proxyfunc) {
PDEBUG("circular reference detected, aborting!\n");
abort();
}
return funcptr;
2012-04-23 17:51:14 +00:00
}
2012-01-27 16:55:37 +00:00
#define INIT() init_lib_wrapper(__FUNCTION__)
#define SETUP_SYM(X) do { true_ ## X = load_sym( # X, X ); } while(0)
#include "shm.h"
#include "allocator_thread.h"
#include "stringdump.h"
static void do_init(void) {
srand(time(NULL));
dumpstring_init(); // global string garbage can
core_initialize();
at_init();
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
2012-04-23 17:51:14 +00:00
proxychains_write_log(LOG_PREFIX "DLL init\n");
SETUP_SYM(connect);
SETUP_SYM(gethostbyname);
SETUP_SYM(getaddrinfo);
SETUP_SYM(freeaddrinfo);
SETUP_SYM(gethostbyaddr);
SETUP_SYM(getnameinfo);
SETUP_SYM(close);
2011-02-25 09:40:11 +00:00
init_l = 1;
}
#if 0
/* FIXME this is currently unused.
* it is not strictly needed.
* maybe let it be called by a gcc destructor, if that doesnt
* have negative consequences (e.g. when a child calles exit) */
static void unload(void) {
at_close();
core_unload();
}
#endif
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
/* get configuration from config file */
static void get_chain_data(proxy_data * pd, unsigned int *proxy_count, chain_type * ct) {
int count = 0, port_n = 0, list = 0;
char buff[1024], type[1024], host[1024], user[1024];
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];
FILE *file = NULL;
2011-02-25 09:40:11 +00:00
if(proxychains_got_chain_data)
return;
2011-02-25 09:40:11 +00:00
//Some defaults
tcp_read_time_out = 4 * 1000;
tcp_connect_time_out = 10 * 1000;
*ct = DYNAMIC_TYPE;
env = get_config_path(getenv(PROXYCHAINS_CONF_FILE_ENV_VAR), buff, sizeof(buff));
if( ( file = fopen(env, "r") ) == NULL )
{
perror("couldnt read configuration file");
exit(1);
}
2012-01-27 16:55:37 +00:00
env = getenv(PROXYCHAINS_QUIET_MODE_ENV_VAR);
if(env && *env == '1')
proxychains_quiet_mode = 1;
2011-02-25 09:40:11 +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;
memset(&pd[count], 0, sizeof(proxy_data));
2012-01-27 16:55:37 +00:00
pd[count].ps = PLAY_STATE;
port_n = 0;
2012-01-27 16:55:37 +00:00
sscanf(buff, "%s %s %d %s %s", type, host, &port_n, pd[count].user, pd[count].pass);
2012-01-27 16:55:37 +00:00
in_addr_t host_ip = inet_addr(host);
if(host_ip == INADDR_NONE) {
fprintf(stderr, "proxy %s has invalid value or is not numeric\n", host);
exit(1);
}
pd[count].ip.as_int = (uint32_t) host_ip;
pd[count].port = htons((unsigned short) port_n);
2012-01-27 16:55:37 +00:00
if(!strcmp(type, "http")) {
pd[count].pt = HTTP_TYPE;
} else if(!strcmp(type, "socks4")) {
pd[count].pt = SOCKS4_TYPE;
} else if(!strcmp(type, "socks5")) {
pd[count].pt = SOCKS5_TYPE;
2012-01-27 16:55:37 +00:00
} else
continue;
2012-01-27 16:55:37 +00:00
if(pd[count].ip.as_int && port_n && pd[count].ip.as_int != (uint32_t) - 1)
2012-04-24 00:04:02 +00:00
count++;
} else {
if(strstr(buff, "[ProxyList]")) {
list = 1;
2012-01-26 11:44:42 +00:00
} else if(strstr(buff, "random_chain")) {
*ct = RANDOM_TYPE;
2012-01-26 11:44:42 +00:00
} else if(strstr(buff, "strict_chain")) {
*ct = STRICT_TYPE;
2012-01-26 11:44:42 +00:00
} else if(strstr(buff, "dynamic_chain")) {
*ct = DYNAMIC_TYPE;
2013-06-23 05:13:40 +00:00
} else if(strstr(buff, "round_robin_chain")) {
*ct = ROUND_ROBIN_TYPE;
} 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);
} 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);
} else if(strstr(buff, "remote_dns_subnet")) {
2012-01-26 11:44:42 +00:00
sscanf(buff, "%s %d", user, &remote_dns_subnet);
if(remote_dns_subnet >= 256) {
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")) {
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 */
memset(local_in_port, 0, sizeof(local_in_port) / sizeof(local_in_port[0]));
2011-03-22 14:04:52 +00:00
if(sscanf(local_in_addr_port, "%15[^:]:%5s", local_in_addr, local_in_port) < 2) {
PDEBUG("added localnet: netaddr=%s, netmask=%s\n",
local_in_addr, local_netmask);
2011-03-22 14:04:52 +00:00
} else {
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
}
if(num_localnet_addr < MAX_LOCALNET) {
int error;
error =
inet_pton(AF_INET, local_in_addr,
&localnet_addr[num_localnet_addr].in_addr);
if(error <= 0) {
fprintf(stderr, "localnet address error\n");
exit(1);
}
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");
exit(1);
}
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;
}
++num_localnet_addr;
} else {
fprintf(stderr, "# of localnet exceed %d.\n", MAX_LOCALNET);
}
} else if(strstr(buff, "chain_len")) {
char *pc;
int len;
pc = strchr(buff, '=');
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
}
}
}
}
fclose(file);
*proxy_count = count;
proxychains_got_chain_data = 1;
2011-02-25 09:40:11 +00:00
}
/******* HOOK FUNCTIONS *******/
2011-02-25 09:40:11 +00:00
int close(int fd) {
INIT();
/* 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);
}
errno = EBADF;
return -1;
}
int connect(int sock, const struct sockaddr *addr, unsigned int len) {
PFUNC();
int socktype = 0, flags = 0, ret = 0;
socklen_t optlen = 0;
ip_type dest_ip;
#ifdef DEBUG
char str[256];
#endif
struct in_addr *p_addr_in;
2011-03-22 14:04:52 +00:00
unsigned short port;
size_t i;
int remote_dns_connect = 0;
INIT();
optlen = sizeof(socktype);
getsockopt(sock, SOL_SOCKET, SO_TYPE, &socktype, &optlen);
if(!(SOCKFAMILY(*addr) == AF_INET && socktype == SOCK_STREAM))
return true_connect(sock, addr, len);
p_addr_in = &((struct sockaddr_in *) addr)->sin_addr;
port = ntohs(((struct sockaddr_in *) addr)->sin_port);
#ifdef DEBUG
// 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)));
PDEBUG("target: %s\n", inet_ntop(AF_INET, p_addr_in, str, sizeof(str)));
PDEBUG("port: %d\n", port);
#endif
// check if connect called from proxydns
remote_dns_connect = (ntohl(p_addr_in->s_addr) >> 24 == remote_dns_subnet);
for(i = 0; i < num_localnet_addr && !remote_dns_connect; i++) {
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)) {
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");
return true_connect(sock, addr, len);
2011-03-22 14:04:52 +00:00
}
}
}
flags = fcntl(sock, F_GETFL, 0);
2011-02-25 09:40:11 +00:00
if(flags & O_NONBLOCK)
fcntl(sock, F_SETFL, !O_NONBLOCK);
2012-01-27 16:55:37 +00:00
dest_ip.as_int = SOCKADDR(*addr);
2012-01-27 16:55:37 +00:00
ret = connect_proxy_chain(sock,
dest_ip,
SOCKPORT(*addr),
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);
if(ret != SUCCESS)
errno = ECONNREFUSED;
2011-02-25 09:40:11 +00:00
return ret;
}
2012-07-15 23:05:28 +00:00
static struct gethostbyname_data ghbndata;
struct hostent *gethostbyname(const char *name) {
INIT();
2012-01-27 16:55:37 +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;
}
int getaddrinfo(const char *node, const char *service, const struct addrinfo *hints, struct addrinfo **res) {
2011-02-25 09:40:11 +00:00
int ret = 0;
2012-01-27 16:55:37 +00:00
INIT();
2012-01-27 16:55:37 +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)
ret = proxy_getaddrinfo(node, service, hints, res);
else
ret = true_getaddrinfo(node, service, hints, res);
2012-01-27 16:55:37 +00:00
2011-02-25 09:40:11 +00:00
return ret;
}
void freeaddrinfo(struct addrinfo *res) {
INIT();
2012-01-27 16:55:37 +00:00
PDEBUG("freeaddrinfo %p \n", 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
return;
}
int pc_getnameinfo(const struct sockaddr *sa, socklen_t salen,
char *host, socklen_t hostlen, char *serv,
socklen_t servlen, int flags)
2011-02-25 09:40:11 +00:00
{
2012-01-27 19:00:22 +00:00
char ip_buf[16];
2011-02-25 09:40:11 +00:00
int ret = 0;
2012-01-27 16:55:37 +00:00
INIT();
PFUNC();
2012-01-27 16:55:37 +00:00
2011-02-25 09:40:11 +00:00
if(!proxychains_resolver) {
ret = true_getnameinfo(sa, salen, host, hostlen, serv, servlen, flags);
2011-02-25 09:40:11 +00:00
} else {
if(salen < sizeof(struct sockaddr_in) || SOCKFAMILY(*sa) != AF_INET)
return EAI_FAMILY;
2012-01-27 19:00:22 +00:00
if(hostlen) {
pc_stringfromipv4((unsigned char*) &(SOCKADDR_2(*sa)), ip_buf);
if(snprintf(host, hostlen, "%s", ip_buf) >= hostlen)
return EAI_OVERFLOW;
}
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
}
return ret;
}
struct hostent *gethostbyaddr(const void *addr, socklen_t len, int type) {
static char buf[16];
static char ipv4[4];
static char *list[2];
static char *aliases[1];
static struct hostent he;
2012-01-27 16:55:37 +00:00
INIT();
2012-01-27 16:55:37 +00:00
2011-09-04 00:03:47 +00:00
PDEBUG("TODO: proper gethostbyaddr hook\n");
2012-01-27 16:55:37 +00:00
2011-02-25 09:40:11 +00:00
if(!proxychains_resolver)
return true_gethostbyaddr(addr, len, type);
else {
2012-01-27 16:55:37 +00:00
2011-09-04 00:03:47 +00:00
PDEBUG("len %u\n", len);
if(len != 4)
return NULL;
he.h_name = buf;
memcpy(ipv4, addr, 4);
list[0] = ipv4;
list[1] = NULL;
he.h_addr_list = list;
he.h_addrtype = AF_INET;
aliases[0] = NULL;
he.h_aliases = aliases;
he.h_length = 4;
pc_stringfromipv4((unsigned char *) addr, buf);
return &he;
}
2011-02-25 09:40:11 +00:00
return NULL;
}