mirror of
https://github.com/rofl0r/proxychains-ng
synced 2026-05-13 17:03:07 +08:00
Compare commits
168 Commits
@@ -1,3 +1,6 @@
|
||||
proxychains4
|
||||
*.bz2
|
||||
*.xz
|
||||
*.o
|
||||
*.so
|
||||
*.la
|
||||
@@ -8,6 +11,7 @@
|
||||
*.out
|
||||
*~
|
||||
*.patch
|
||||
version.h
|
||||
|
||||
# Autoconf stuff
|
||||
libtool
|
||||
|
||||
@@ -6,13 +6,17 @@ netcreature@users.sourceforge.net
|
||||
main.c, remote-dns, thread safety, bugfixes, build system,
|
||||
cleanups, mac support
|
||||
rofl0r.
|
||||
https://github.com/rofl0r/proxychains
|
||||
https://github.com/rofl0r/proxychains-ng
|
||||
|
||||
localnet, bugfixes
|
||||
jianing yang.
|
||||
https://github.com/jianingy/proxychains
|
||||
https://sourceforge.net/projects/proxychains/forums/forum/644747/topic/3498696
|
||||
|
||||
round-robin
|
||||
crass.
|
||||
https://github.com/crass/proxychains-ng
|
||||
|
||||
poll_retry (fixes for signal handling)
|
||||
colin cross.
|
||||
https://sourceforge.net/projects/proxychains/forums/forum/644747/topic/2367923
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
GNU GENERAL PUBLIC LICENSE
|
||||
Version 2, June 1991
|
||||
|
||||
Copyright (C) 1989, 1991 Free Software Foundation, Inc.
|
||||
675 Mass Ave, Cambridge, MA 02139, USA
|
||||
Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
Everyone is permitted to copy and distribute verbatim copies
|
||||
of this license document, but changing it is not allowed.
|
||||
|
||||
|
||||
@@ -1,65 +0,0 @@
|
||||
ProxyChains version history (public releases)
|
||||
====================
|
||||
|
||||
ver 3.1
|
||||
|
||||
changed:
|
||||
* dns resolver script fix
|
||||
* prototypes in core.h
|
||||
|
||||
-------------------------------------------------------------------------
|
||||
ver 3.0
|
||||
|
||||
added:
|
||||
* new feature - DNS from behind proxy
|
||||
* proxyresolv - stand alone command
|
||||
* proxychains.conf - new option to enable/disable DNS support
|
||||
|
||||
changed:
|
||||
* bugfixes in core lib
|
||||
* fixed strict chain
|
||||
* fixed random chain
|
||||
* output text
|
||||
* autotools fix
|
||||
|
||||
-------------------------------------------------------------------------
|
||||
ver 2.1
|
||||
* bugfuxes
|
||||
|
||||
-------------------------------------------------------------------------
|
||||
ver 2.0
|
||||
* major core rewrite
|
||||
* new config options
|
||||
|
||||
-------------------------------------------------------------------------
|
||||
|
||||
ver 1.8.2
|
||||
* minor bugfixes
|
||||
* improved compilation on FreeBSD & OpenBSD sysems.
|
||||
* improved compilation on Sun Solaris systems .
|
||||
* cross platform (UNIX) issues
|
||||
|
||||
-------------------------------------------------------------------------
|
||||
|
||||
ver 1.8.0
|
||||
|
||||
added:
|
||||
* Socks5 protocol
|
||||
* Socks4 protocol
|
||||
* HTTP proxy auth basic
|
||||
* Socks4 user auth
|
||||
* Socks5 user/pass auth
|
||||
* more chain options (random, strict, dynamic )
|
||||
* configurable timeout for TCP connect.
|
||||
* configurable timeout for TCP read.
|
||||
* INSTALL file (explains how to install properly)
|
||||
|
||||
changed:
|
||||
* configuration file entries (proxychains.conf)
|
||||
* configuration file lookup
|
||||
|
||||
-------------------------------------------------------------------------
|
||||
|
||||
ver 0.0.1
|
||||
* TCP calls interception
|
||||
* HTTP CONNECT proxy protocol.
|
||||
@@ -15,20 +15,26 @@ sysconfdir=$(prefix)/etc
|
||||
|
||||
SRCS = $(sort $(wildcard src/*.c))
|
||||
OBJS = $(SRCS:.c=.o)
|
||||
LOBJS = src/core.o src/common.o src/libproxychains.o
|
||||
LOBJS = src/nameinfo.o src/version.o \
|
||||
src/core.o src/common.o src/libproxychains.o \
|
||||
src/allocator_thread.o src/ip_type.o \
|
||||
src/hostsreader.o src/hash.o src/debug.o
|
||||
|
||||
CFLAGS += -Wall -O0 -g -std=c99 -D_GNU_SOURCE -pipe -DTHREAD_SAFE
|
||||
LDFLAGS = -shared -fPIC -Wl,--no-as-needed -ldl -lpthread
|
||||
GENH = src/version.h
|
||||
|
||||
CFLAGS += -Wall -O0 -g -std=c99 -D_GNU_SOURCE -pipe
|
||||
NO_AS_NEEDED = -Wl,--no-as-needed
|
||||
LIBDL = -ldl
|
||||
LDFLAGS = -fPIC $(NO_AS_NEEDED) $(LIBDL) -lpthread
|
||||
INC =
|
||||
PIC = -fPIC
|
||||
AR = $(CROSS_COMPILE)ar
|
||||
RANLIB = $(CROSS_COMPILE)ranlib
|
||||
SOCKET_LIBS =
|
||||
|
||||
LDSO_SUFFIX = so
|
||||
LD_SET_SONAME = -Wl,-soname=
|
||||
INSTALL_FLAGS = -D -m
|
||||
|
||||
-include config.mak
|
||||
INSTALL = ./tools/install.sh
|
||||
|
||||
LDSO_PATHNAME = libproxychains4.$(LDSO_SUFFIX)
|
||||
|
||||
@@ -36,7 +42,9 @@ SHARED_LIBS = $(LDSO_PATHNAME)
|
||||
ALL_LIBS = $(SHARED_LIBS)
|
||||
PXCHAINS = proxychains4
|
||||
ALL_TOOLS = $(PXCHAINS)
|
||||
ALL_CONFIGS = src/proxychains.conf
|
||||
|
||||
-include config.mak
|
||||
|
||||
CFLAGS+=$(USER_CFLAGS) $(MAC_CFLAGS)
|
||||
CFLAGS_MAIN=-DLIB_DIR=\"$(libdir)\" -DSYSCONFDIR=\"$(sysconfdir)\" -DDLL_NAME=\"$(LDSO_PATHNAME)\"
|
||||
@@ -44,28 +52,41 @@ CFLAGS_MAIN=-DLIB_DIR=\"$(libdir)\" -DSYSCONFDIR=\"$(sysconfdir)\" -DDLL_NAME=\"
|
||||
|
||||
all: $(ALL_LIBS) $(ALL_TOOLS)
|
||||
|
||||
install-config:
|
||||
install -d $(DESTDIR)/$(sysconfdir)
|
||||
install $(INSTALL_FLAGS) 644 src/proxychains.conf $(DESTDIR)/$(sysconfdir)/
|
||||
install: install-libs install-tools
|
||||
|
||||
install:
|
||||
install -d $(DESTDIR)/$(bindir)/ $(DESTDIR)/$(libdir)/
|
||||
install $(INSTALL_FLAGS) 755 $(ALL_TOOLS) $(DESTDIR)/$(bindir)/
|
||||
install $(INSTALL_FLAGS) 644 $(ALL_LIBS) $(DESTDIR)/$(libdir)/
|
||||
$(DESTDIR)$(bindir)/%: %
|
||||
$(INSTALL) -D -m 755 $< $@
|
||||
|
||||
$(DESTDIR)$(libdir)/%: %
|
||||
$(INSTALL) -D -m 644 $< $@
|
||||
|
||||
$(DESTDIR)$(sysconfdir)/%: src/%
|
||||
$(INSTALL) -D -m 644 $< $@
|
||||
|
||||
install-libs: $(ALL_LIBS:%=$(DESTDIR)$(libdir)/%)
|
||||
install-tools: $(ALL_TOOLS:%=$(DESTDIR)$(bindir)/%)
|
||||
install-config: $(ALL_CONFIGS:src/%=$(DESTDIR)$(sysconfdir)/%)
|
||||
|
||||
clean:
|
||||
rm -f $(ALL_LIBS)
|
||||
rm -f $(ALL_TOOLS)
|
||||
rm -f $(OBJS)
|
||||
rm -f $(GENH)
|
||||
|
||||
src/version.h: $(wildcard VERSION .git)
|
||||
printf '#define VERSION "%s"\n' "$$(sh tools/version.sh)" > $@
|
||||
|
||||
src/version.o: src/version.h
|
||||
|
||||
%.o: %.c
|
||||
$(CC) $(CFLAGS) $(CFLAGS_MAIN) $(INC) $(PIC) -c -o $@ $<
|
||||
$(CC) $(CPPFLAGS) $(CFLAGS) $(CFLAGS_MAIN) $(INC) $(PIC) -c -o $@ $<
|
||||
|
||||
$(LDSO_PATHNAME): $(LOBJS)
|
||||
$(CC) $(LDFLAGS) $(LD_SET_SONAME)$(LDSO_PATHNAME) -o $@ $(LOBJS)
|
||||
$(CC) $(LDFLAGS) $(LD_SET_SONAME)$(LDSO_PATHNAME) $(USER_LDFLAGS) \
|
||||
-shared -o $@ $(LOBJS) $(SOCKET_LIBS)
|
||||
|
||||
$(ALL_TOOLS): $(OBJS)
|
||||
$(CC) src/main.o src/common.o -o $(PXCHAINS)
|
||||
$(CC) src/main.o src/common.o $(USER_LDFLAGS) -o $(PXCHAINS)
|
||||
|
||||
|
||||
.PHONY: all clean install
|
||||
.PHONY: all clean install install-config install-libs install-tools
|
||||
|
||||
@@ -1,62 +1,156 @@
|
||||
ProxyChains ver 4.1 README
|
||||
==========================
|
||||
ProxyChains-NG ver 4.14 README
|
||||
=============================
|
||||
|
||||
ProxyChains is a UNIX program, that hooks network-related libc functions
|
||||
in dynamically linked programs via a preloaded DLL and redirects the
|
||||
connections through SOCKS4a/5 or HTTP proxies.
|
||||
in DYNAMICALLY LINKED programs via a preloaded DLL (dlsym(), LD_PRELOAD)
|
||||
and redirects the connections through SOCKS4a/5 or HTTP proxies.
|
||||
It supports TCP only (no UDP/ICMP etc).
|
||||
|
||||
The way it works is basically a HACK; so it is possible that it doesn't
|
||||
work with your program, especially when it's a script, or starts
|
||||
numerous processes like background daemons or uses dlopen() to load
|
||||
"modules" (bug in glibc dynlinker).
|
||||
It should work with simple compiled (C/C++) dynamically linked programs
|
||||
though.
|
||||
|
||||
If your program doesn't work with proxychains, consider using an
|
||||
iptables based solution instead; this is much more robust.
|
||||
|
||||
Supported Platforms: Linux, BSD, Mac.
|
||||
|
||||
|
||||
*********** ATTENTION ***********
|
||||
|
||||
this program works only on dynamically linked programs.
|
||||
also both proxychains and the program to call must use
|
||||
the same dynamic linker (i.e. same libc)
|
||||
this program can be used to circumvent censorship.
|
||||
doing so can be VERY DANGEROUS in certain countries.
|
||||
|
||||
*********************************
|
||||
ALWAYS MAKE SURE THAT PROXYCHAINS WORKS AS EXPECTED
|
||||
BEFORE USING IT FOR ANYTHING SERIOUS.
|
||||
|
||||
*** Known limitations of the current version: ***
|
||||
this involves both the program and the proxy that you're going to
|
||||
use.
|
||||
|
||||
when a process forks, does a DNS lookup in the child, and then uses
|
||||
the ip in the parent, the corresponding ip mapping will not be found.
|
||||
this is because the fork can't write back into the parents mapping table.
|
||||
IRSSI shows this behaviour, so you have to pass the resolved ip address
|
||||
to it. (you can use the proxyresolv script (requires "dig") to do so)
|
||||
for example, you can connect to some "what is my ip" service
|
||||
like ifconfig.me to make sure that it's not using your real ip.
|
||||
|
||||
this means that you can't currently use tor onion urls for irssi.
|
||||
to solve this issue, an external data store (file, pipe, ...) has to
|
||||
manage the dns <-> ip mapping. of course there has to be proper locking.
|
||||
shm_open, mkstemp, are possible candidates for a file based approach,
|
||||
the other option is to spawn some kind of server process that manages the
|
||||
map lookups. since connect() etc are hooked, this must not be a TCP server.
|
||||
ONLY USE PROXYCHAINS IF YOU KNOW WHAT YOU'RE DOING.
|
||||
|
||||
I am reluctant on doing this change, because the described behaviour
|
||||
seems pretty idiotic (doing a fork only for a DNS lookup), and irssi
|
||||
is currently the only known affected program.
|
||||
THE AUTHORS AND MAINTAINERS OF PROXYCHAINS DO NOT TAKE ANY
|
||||
RESPONSIBILITY FOR ANY ABUSE OR MISUSE OF THIS SOFTWARE AND
|
||||
THE RESULTING CONSEQUENCES.
|
||||
|
||||
*** Installation ***
|
||||
|
||||
# needs a working C compiler, preferably gcc
|
||||
./configure
|
||||
./configure --prefix=/usr --sysconfdir=/etc
|
||||
make
|
||||
sudo make install
|
||||
[optional] sudo make install
|
||||
[optional] sudo make install-config (installs proxychains.conf)
|
||||
|
||||
if you dont install, you can use proxychains from the build directory
|
||||
like this: ./proxychains4 -f src/proxychains.conf telnet google.com 80
|
||||
|
||||
Changelog:
|
||||
----------
|
||||
Version 4.1 adds support for mac os x (i386, x86_64, ppc)
|
||||
all internal functions are threadsafe when compiled with -DTHREAD_SAFE
|
||||
(default).
|
||||
Version 4.14
|
||||
- allow alternative proto://user:pass@ip:port syntax for proxylist
|
||||
- fix endless loop in round robin mode when all proxies are down (#147)
|
||||
- fix compilation on android (#265)
|
||||
- fix fd leak in forked processes (#273)
|
||||
- skip connection attempt to nullrouted ips
|
||||
- allow hostnames for proxylist under specific circumstances
|
||||
|
||||
Version (4.x) removes the dnsresolver script which required a dynamically
|
||||
linked "dig" binary to be present with remote DNS lookup.
|
||||
this speeds up any operation involving DNS, as the old script had to use TCP.
|
||||
additionally it allows to use .onion urls when used with TOR.
|
||||
also it removed the broken autoconf build system with a simple Makefile.
|
||||
there's a ./configure script though for convenience.
|
||||
it also adds support for a config file passed via command line switches/
|
||||
environment variables.
|
||||
Version 4.13
|
||||
- fix robustness of DNS lookup thread and a segfault
|
||||
- fix socks5 user/pass auth on non-conforming servers
|
||||
- fix memory leak
|
||||
- add support for Solaris
|
||||
|
||||
Version (3.x) introduces support for DNS resolving through proxy
|
||||
it supports SOCKS4, SOCKS5 and HTTP CONNECT proxy servers.
|
||||
Auth-types: socks - "user/pass" , http - "basic".
|
||||
Version 4.12
|
||||
- fix several build issues
|
||||
- for MAC
|
||||
- with -pie
|
||||
- with custom CC
|
||||
- compatibility fix for some GUI apps (8870140)
|
||||
- compatibility fix for some HTTP proxies (cf9a16d)
|
||||
- fix several warnings for cleaner build on debian
|
||||
- fix random_chain on OSX (0f6b226)
|
||||
|
||||
Version 4.11
|
||||
- preliminary IPv6 support
|
||||
- fixed bug in hostsreader
|
||||
- preliminary support for usage on OpenBSD (caveat emptor)
|
||||
|
||||
Version 4.10
|
||||
- fix regression in linking order with custom LDFLAGS
|
||||
- fix segfault in DNS mapping code in programs with > ~400 different lookups
|
||||
|
||||
Version 4.9
|
||||
- fix a security issue CVE-2015-3887
|
||||
- add sendto hook to handle MSG_FASTOPEN flag
|
||||
- replace problematic hostentdb with hostsreader
|
||||
- fix compilation on OpenBSD (although doesn't work there)
|
||||
|
||||
Version 4.8.1:
|
||||
- fix regression in 4.8 install-config Makefile target
|
||||
|
||||
Version 4.8:
|
||||
- fix for odd cornercase where getaddrinfo was used with AI_NUMERICHOST
|
||||
to test for a numeric ip instead of resolving it (fixes nmap).
|
||||
- allow usage with programs that rely on LD_PRELOAD themselves
|
||||
- reject wrong entries in config file
|
||||
- print version number on startup
|
||||
|
||||
Version 4.7:
|
||||
- new round_robin chaintype by crass.
|
||||
- fix bug with lazy allocation when GCC constructor was not used.
|
||||
- new configure flag --fat-binary to create a "fat" binary/library on OS X
|
||||
- return EBADF rather than EINTR in close hook.
|
||||
it's legal for a program to retry close() calls when they receive
|
||||
EINTR, which could cause an infinite loop, as seen in chromium.
|
||||
|
||||
Version 4.6:
|
||||
- some cosmetic fixes to Makefile, fix a bug when non-numeric ip was
|
||||
used as proxy server address.
|
||||
|
||||
Version 4.5:
|
||||
- hook close() to prevent OpenSSH from messing with internal infrastructure.
|
||||
this caused ssh client to segfault when proxified.
|
||||
|
||||
Version 4.4:
|
||||
- FreeBSD port
|
||||
- fixes some installation issues on Debian and Mac.
|
||||
|
||||
Version 4.3:
|
||||
- fixes programs that do dns-lookups in child processes (fork()ed),
|
||||
like irssi. to achieve this, support for compilation without pthreads
|
||||
was sacrified.
|
||||
- fixes thread safety for gethostent() calls.
|
||||
- improved DNS handling speed, since hostent db is cached.
|
||||
|
||||
Version 4.2:
|
||||
- fixes compilation issues with ubuntu 12.04 toolchain
|
||||
- fixes segfault in rare codepath
|
||||
|
||||
Version 4.1
|
||||
- support for mac os x (all archs)
|
||||
- all internal functions are threadsafe when compiled with -DTHREAD_SAFE
|
||||
(default).
|
||||
|
||||
Version 4.0
|
||||
- replaced dnsresolver script (which required a dynamically linked "dig"
|
||||
binary to be present) with remote DNS lookup.
|
||||
this speeds up any operation involving DNS, as the old script had to use TCP.
|
||||
additionally it allows to use .onion urls when used with TOR.
|
||||
- removed broken autoconf build system with a simple Makefile.
|
||||
there's a ./configure script though for convenience.
|
||||
it also adds support for a config file passed via command line switches/
|
||||
environment variables.
|
||||
|
||||
Version 3.0
|
||||
- support for DNS resolving through proxy
|
||||
supports SOCKS4, SOCKS5 and HTTP CONNECT proxy servers.
|
||||
Auth-types: socks - "user/pass" , http - "basic".
|
||||
|
||||
When to use it ?
|
||||
1) When the only way to get "outside" from your LAN is through proxy server.
|
||||
@@ -65,7 +159,8 @@ When to use it ?
|
||||
like: your_host <--> proxy1 <--> proxy2 <--> target_host
|
||||
4) To "proxify" some program with no proxy support built-in (like telnet)
|
||||
5) Access intranet from outside via proxy.
|
||||
5) To use DNS behind proxy.
|
||||
6) To use DNS behind proxy.
|
||||
7) To access hidden tor onion services.
|
||||
|
||||
Some cool features:
|
||||
|
||||
@@ -75,10 +170,9 @@ Some cool features:
|
||||
random order from the list ( user defined length of chain ).
|
||||
exact order (as they appear in the list )
|
||||
dynamic order (smart exclude dead proxies from chain)
|
||||
* You can use it with any TCP client application, even network scanners
|
||||
yes, yes - you can make portscan via proxy (or chained proxies)
|
||||
for example with Nmap scanner by fyodor (www.insecire.org/nmap).
|
||||
proxychains nmap -sT -PO -p 80 -iR (find some webservers through proxy)
|
||||
* You can use it with most TCP client applications, possibly even network
|
||||
scanners, as long as they use standard libc functionality.
|
||||
pcap based scanning does not work.
|
||||
* You can use it with servers, like squid, sendmail, or whatever.
|
||||
* DNS resolving through proxy.
|
||||
|
||||
@@ -87,13 +181,13 @@ Configuration:
|
||||
--------------
|
||||
|
||||
proxychains looks for config file in following order:
|
||||
1) file listed in environment variable ${PROXYCHAINS_CONF_FILE} or
|
||||
1) file listed in environment variable PROXYCHAINS_CONF_FILE or
|
||||
provided as a -f argument to proxychains script or binary.
|
||||
2) ./proxychains.conf
|
||||
3) $(HOME)/.proxychains/proxychains.conf
|
||||
4) /etc/proxychains.conf **
|
||||
4) $(sysconfdir)/proxychains.conf **
|
||||
|
||||
**see more in /etc/proxychains.conf
|
||||
** usually /etc/proxychains.conf
|
||||
|
||||
Usage Example:
|
||||
|
||||
@@ -104,9 +198,9 @@ specified by proxychains.conf
|
||||
|
||||
Usage Example:
|
||||
|
||||
$ proxychains -f /etc/proxychains-other.conf targethost2.com
|
||||
$ proxychains -f /etc/proxychains-other.conf telnet targethost2.com
|
||||
|
||||
in this example it will use different configuration file then proxychains.conf
|
||||
in this example it will use different configuration file then proxychains.conf
|
||||
to connect to targethost2.com host.
|
||||
|
||||
Usage Example:
|
||||
@@ -116,3 +210,36 @@ Usage Example:
|
||||
in this example it will resolve targethost.com through proxy(or chained proxies)
|
||||
specified by proxychains.conf
|
||||
|
||||
Known Problems:
|
||||
---------------
|
||||
- newer versions of nmap try to determine the network interface to use
|
||||
even if it's not needed (like when doing simple syn scans which use the
|
||||
standard POSIX socket API. this results in errors when proxychains hands
|
||||
out an ip address to a reserved address space.
|
||||
possible workarounds: disable proxy_dns, use a numeric ip, or use nmap's
|
||||
native support for SOCKS proxies.
|
||||
|
||||
- Mac OS X 10.11 (El Capitan) ships with a new security feature called SIP
|
||||
that prevents hooking of system apps.
|
||||
workarounds are to partially disable SIP by issuing
|
||||
csrutil enable --without debug in recovery mode,
|
||||
or to copy the system binary into the home directory and run it from there.
|
||||
see github issue #78 for details.
|
||||
|
||||
- the glibc dynlinker has a bug or security feature that inhibits dlopen()ed
|
||||
modules from being subject to the same dlsym hooks as installed for the main
|
||||
program. this mainly affects scripting languages such as perl or python
|
||||
that heavily rely on dlopen() for modules written in C to work.
|
||||
there are unconfirmed reports that it works as root though.
|
||||
musl libc is unaffected from the bug.
|
||||
|
||||
|
||||
Community:
|
||||
----------
|
||||
#proxychains on irc.freenode.net
|
||||
|
||||
Donations:
|
||||
----------
|
||||
bitcoins donations are welcome - please send to this address:
|
||||
1C9LBpuy56veBqw5N33sZMoZW8mwCw3tPh
|
||||
|
||||
|
||||
@@ -1,15 +1,76 @@
|
||||
#!/bin/sh
|
||||
|
||||
prefix=/usr/local
|
||||
OUR_CPPFLAGS=
|
||||
|
||||
# Get a temporary filename
|
||||
i=0
|
||||
set -C
|
||||
while : ; do i=$(($i+1))
|
||||
tmpc="./conf$$-$PPID-$i.c"
|
||||
2>|/dev/null > "$tmpc" && break
|
||||
test "$i" -gt 50 && fail "$0: cannot create temporary file $tmpc"
|
||||
done
|
||||
set +C
|
||||
trap 'rm "$tmpc"' EXIT INT QUIT TERM HUP
|
||||
|
||||
check_compile() {
|
||||
printf "checking %s ... " "$1"
|
||||
printf "$3" > "$tmpc"
|
||||
local res=0
|
||||
$CC $OUR_CPPFLAGS $CPPFLAGS $2 $CFLAGS "$tmpc" -o /dev/null >/dev/null 2>&1 \
|
||||
|| res=1
|
||||
test x$res = x0 && \
|
||||
{ printf "yes\n" ; test x"$2" = x || OUR_CPPFLAGS="$OUR_CPPFLAGS $2" ; } \
|
||||
|| printf "no\n"
|
||||
return $res
|
||||
}
|
||||
|
||||
check_define() {
|
||||
printf "checking whether \$CC defines %s ... " "$1"
|
||||
local res=1
|
||||
$CC $OUR_CPPFLAGS $CPPFLAGS $CFLAGS -dM -E - </dev/null | grep "$1" >/dev/null && res=0
|
||||
test x$res = x0 && printf "yes\n" || printf "no\n"
|
||||
return $res
|
||||
}
|
||||
|
||||
check_compile_run() {
|
||||
printf "checking %s ... " "$1"
|
||||
printf "$2" > "$tmpc"
|
||||
local res=0
|
||||
$CC $OUR_CPPFLAGS $CPPFLAGS $CFLAGS "$tmpc" -o "$tmpc".out >/dev/null 2>&1 \
|
||||
|| res=1
|
||||
test x$res = x0 && { "$tmpc".out || res=1 ; }
|
||||
rm -f "$tmpc".out
|
||||
test x$res = x0 && printf "yes\n" || printf "no\n"
|
||||
return $res
|
||||
}
|
||||
|
||||
check_link_silent() {
|
||||
printf "$2" > "$tmpc"
|
||||
$CC $OUR_CPPFLAGS $CPPFLAGS $1 $CFLAGS "$tmpc" -o /dev/null >/dev/null 2>&1
|
||||
}
|
||||
|
||||
check_link() {
|
||||
printf "checking %s ... " "$1"
|
||||
local res=0
|
||||
check_link_silent "$2" "$3" || res=1
|
||||
test x$res = x0 && printf "yes\n" || printf "no\n"
|
||||
return $res
|
||||
}
|
||||
|
||||
usage() {
|
||||
echo "supported arguments"
|
||||
echo "--prefix=/path default: $prefix"
|
||||
echo "--exec_prefix=/path default: $prefix/bin"
|
||||
echo "--bindir=/path default: $prefix/bin"
|
||||
echo "--libdir=/path default: $prefix/lib"
|
||||
echo "--includedir=/path default: $prefix/include"
|
||||
echo "--sysconfdir=/path default: $prefix/etc"
|
||||
echo "--prefix=/path default: $prefix"
|
||||
echo "--exec_prefix=/path default: $prefix/bin"
|
||||
echo "--bindir=/path default: $prefix/bin"
|
||||
echo "--libdir=/path default: $prefix/lib"
|
||||
echo "--includedir=/path default: $prefix/include"
|
||||
echo "--sysconfdir=/path default: $prefix/etc"
|
||||
echo "--ignore-cve default: no"
|
||||
echo " if set to yes ignores CVE-2015-3887 and makes it possible"
|
||||
echo " to preload from current dir (insecure)"
|
||||
echo "--fat-binary : build for both i386 and x86_64 architectures on 64-bit Macs"
|
||||
echo "--help : show this text"
|
||||
exit 1
|
||||
}
|
||||
@@ -21,6 +82,8 @@ spliteq() {
|
||||
# or echo "$arg" | sed 's/[^=]*=//'
|
||||
}
|
||||
|
||||
fat_binary=
|
||||
ignore_cve=no
|
||||
parsearg() {
|
||||
case "$1" in
|
||||
--prefix=*) prefix=`spliteq $1`;;
|
||||
@@ -29,14 +92,13 @@ parsearg() {
|
||||
--libdir=*) libdir=`spliteq $1`;;
|
||||
--includedir=*) includedir=`spliteq $1`;;
|
||||
--sysconfdir=*) sysconfdir=`spliteq $1`;;
|
||||
--ignore-cve) ignore_cve=1;;
|
||||
--ignore-cve=*) ignore_cve=`spliteq $1`;;
|
||||
--fat-binary) fat_binary=1;;
|
||||
--help) usage;;
|
||||
esac
|
||||
}
|
||||
|
||||
ismac() {
|
||||
uname -s | grep Darwin
|
||||
}
|
||||
|
||||
while true ; do
|
||||
case $1 in
|
||||
-*) parsearg "$1"; shift;;
|
||||
@@ -68,21 +130,89 @@ if [ -z "$CC" ] ; then
|
||||
CC=cc
|
||||
fi
|
||||
|
||||
echo CC?=$CC>config.mak
|
||||
[ -z "$CPPFLAGS" ] || echo CPPFLAGS?=$CPPFLAGS>>config.mak
|
||||
[ -z "$CFLAGS" ] || echo USER_CFLAGS?=$CFLAGS>>config.mak
|
||||
echo > config.mak
|
||||
|
||||
bsd_detected=false
|
||||
isbsd() {
|
||||
$bsd_detected
|
||||
}
|
||||
mac_detected=false
|
||||
ismac() {
|
||||
$mac_detected
|
||||
}
|
||||
mac_64=false
|
||||
ismac64() {
|
||||
$mac_64
|
||||
}
|
||||
solaris_detected=false
|
||||
issolaris() {
|
||||
$solaris_detected
|
||||
}
|
||||
|
||||
check_compile 'whether we have GNU-style getservbyname_r()' "-DHAVE_GNU_GETSERVBYNAME_R" \
|
||||
'#define _GNU_SOURCE\n#include <netdb.h>\nint main() {\nstruct servent *se = 0;struct servent se_buf;char buf[1024];\ngetservbyname_r("foo", (void*) 0, &se_buf, buf, sizeof(buf), &se);\nreturn 0;}'
|
||||
|
||||
check_compile 'whether we have pipe2() and O_CLOEXEC' "-DHAVE_PIPE2" \
|
||||
'#define _GNU_SOURCE\n#include <fcntl.h>\n#include <unistd.h>\nint main() {\nint pipefd[2];\npipe2(pipefd, O_CLOEXEC);\nreturn 0;}'
|
||||
|
||||
check_define __APPLE__ && {
|
||||
mac_detected=true
|
||||
check_define __x86_64__ && mac_64=true
|
||||
}
|
||||
check_define __FreeBSD__ && bsd_detected=true
|
||||
check_define __OpenBSD__ && {
|
||||
bsd_detected=true
|
||||
echo "CFLAGS+=-DIS_OPENBSD">>config.mak
|
||||
check_compile_run 'whether OpenBSDs fclose() (illegally) calls close()' \
|
||||
'#include <stdio.h>\n#include<stdlib.h>\nint close(int x){exit(0);}int main(){fclose(stdin);return 1;}' && \
|
||||
OUR_CPPFLAGS="$OUR_CPPFLAGS -DBROKEN_FCLOSE"
|
||||
}
|
||||
check_define __sun && check_define __SVR4 && solaris_detected=true
|
||||
|
||||
echo "CC=$CC">>config.mak
|
||||
[ -z "$CPPFLAGS" ] || echo "CPPFLAGS=$CPPFLAGS">>config.mak
|
||||
[ -z "$CFLAGS" ] || echo "USER_CFLAGS=$CFLAGS">>config.mak
|
||||
[ -z "$LDFLAGS" ] || echo "USER_LDFLAGS=$LDFLAGS">>config.mak
|
||||
echo prefix=$prefix>>config.mak
|
||||
echo exec_prefix=$exec_prefix>>config.mak
|
||||
echo bindir=$bindir>>config.mak
|
||||
echo libdir=$libdir>>config.mak
|
||||
echo includedir=$includedir>>config.mak
|
||||
echo sysconfdir=$sysconfdir>>config.mak
|
||||
[ "$ignore_cve" = "no" ] && echo "CPPFLAGS+= -DSUPER_SECURE">>config.mak
|
||||
[ -z "$OUR_CPPFLAGS" ] || echo "CPPFLAGS+= $OUR_CPPFLAGS" >>config.mak
|
||||
|
||||
check_link "whether we can use -Wl,--no-as-needed" "-Wl,--no-as-needed" \
|
||||
"int main() { return 0; }" || echo NO_AS_NEEDED= >> config.mak
|
||||
|
||||
LD_SONAME_FLAG=
|
||||
printf "checking what's the option to use in linker to set library name ... "
|
||||
for o in --soname -h -soname -install_name; do
|
||||
check_link_silent "-shared -Wl,$o,libconftest.so" "void test_func(int a) {}" && LD_SONAME_FLAG=$o && break
|
||||
done
|
||||
if [ -z "$LD_SONAME_FLAG" ]; then
|
||||
printf '\ncannot find an option to set library name\n'
|
||||
exit 1
|
||||
fi
|
||||
echo "$LD_SONAME_FLAG"
|
||||
echo "LD_SET_SONAME = -Wl,$LD_SONAME_FLAG," >> config.mak
|
||||
|
||||
make_cmd=make
|
||||
if ismac ; then
|
||||
echo LDSO_SUFFIX=dylib>>config.mak
|
||||
echo MAC_CFLAGS+=-DIS_MAC=1>>config.mak
|
||||
echo LD_SET_SONAME=-Wl,-install_name,>>config.mak
|
||||
echo INSTALL_FLAGS=-m>>config.mak
|
||||
if ismac64 && [ "$fat_binary" = 1 ] ; then
|
||||
echo "Configuring a fat binary for i386 and x86_64"
|
||||
echo MAC_CFLAGS+=-arch i386 -arch x86_64>>config.mak
|
||||
echo LDFLAGS+=-arch i386 -arch x86_64>>config.mak
|
||||
fi
|
||||
elif isbsd ; then
|
||||
echo LIBDL=>>config.mak
|
||||
echo "CFLAGS+=-DIS_BSD">>config.mak
|
||||
make_cmd=gmake
|
||||
elif issolaris; then
|
||||
echo "CFLAGS+=-DIS_SOLARIS -D__EXTENSIONS__" >> config.mak
|
||||
echo "SOCKET_LIBS=-lsocket -lnsl" >> config.mak
|
||||
fi
|
||||
|
||||
echo done, now run make \&\& make install
|
||||
|
||||
echo "Done, now run $make_cmd && $make_cmd install"
|
||||
|
||||
Vendored
-11
@@ -1,11 +0,0 @@
|
||||
### config.mak template for proxychains
|
||||
#######################################
|
||||
|
||||
# just copy into proxychains root dir and adapt to your needs.
|
||||
|
||||
prefix = /usr/local/
|
||||
libdir = $(prefix)/lib
|
||||
|
||||
exec_prefix = /usr/local
|
||||
bindir = $(exec_prefix)/bin
|
||||
|
||||
@@ -0,0 +1,368 @@
|
||||
#undef _GNU_SOURCE
|
||||
#define _GNU_SOURCE
|
||||
#undef _POSIX_C_SOURCE
|
||||
#define _DARWIN_C_SOURCE
|
||||
#include <limits.h>
|
||||
#include <pthread.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/select.h>
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <errno.h>
|
||||
#include <sys/mman.h>
|
||||
#include "allocator_thread.h"
|
||||
#include "debug.h"
|
||||
#include "ip_type.h"
|
||||
#include "mutex.h"
|
||||
#include "hash.h"
|
||||
|
||||
/* stuff for our internal translation table */
|
||||
|
||||
typedef struct {
|
||||
uint32_t hash;
|
||||
char* string;
|
||||
} string_hash_tuple;
|
||||
|
||||
typedef struct {
|
||||
uint32_t counter;
|
||||
uint32_t capa;
|
||||
string_hash_tuple** list;
|
||||
} internal_ip_lookup_table;
|
||||
|
||||
static void *dumpstring(char* s, size_t len) {
|
||||
char* p = malloc(len);
|
||||
if(p) memcpy(p, s, len);
|
||||
return p;
|
||||
}
|
||||
|
||||
static pthread_mutex_t *internal_ips_lock;
|
||||
static internal_ip_lookup_table *internal_ips;
|
||||
|
||||
uint32_t index_from_internal_ip(ip_type4 internalip) {
|
||||
PFUNC();
|
||||
ip_type4 tmp = internalip;
|
||||
uint32_t ret;
|
||||
ret = tmp.octet[3] + (tmp.octet[2] << 8) + (tmp.octet[1] << 16);
|
||||
ret -= 1;
|
||||
return ret;
|
||||
}
|
||||
|
||||
char *string_from_internal_ip(ip_type4 internalip) {
|
||||
PFUNC();
|
||||
char *res = NULL;
|
||||
uint32_t index = index_from_internal_ip(internalip);
|
||||
if(index < internal_ips->counter)
|
||||
res = internal_ips->list[index]->string;
|
||||
return res;
|
||||
}
|
||||
|
||||
extern unsigned int remote_dns_subnet;
|
||||
ip_type4 make_internal_ip(uint32_t index) {
|
||||
ip_type4 ret;
|
||||
index++; // so we can start at .0.0.1
|
||||
if(index > 0xFFFFFF)
|
||||
return ip_type_invalid.addr.v4;
|
||||
ret.octet[0] = remote_dns_subnet & 0xFF;
|
||||
ret.octet[1] = (index & 0xFF0000) >> 16;
|
||||
ret.octet[2] = (index & 0xFF00) >> 8;
|
||||
ret.octet[3] = index & 0xFF;
|
||||
return ret;
|
||||
}
|
||||
|
||||
static ip_type4 ip_from_internal_list(char* name, size_t len) {
|
||||
uint32_t hash = dalias_hash((char *) name);
|
||||
size_t i;
|
||||
ip_type4 res;
|
||||
void* new_mem;
|
||||
// see if we already have this dns entry saved.
|
||||
if(internal_ips->counter) {
|
||||
for(i = 0; i < internal_ips->counter; i++) {
|
||||
if(internal_ips->list[i]->hash == hash && !strcmp(name, internal_ips->list[i]->string)) {
|
||||
res = make_internal_ip(i);
|
||||
PDEBUG("got cached ip for %s\n", name);
|
||||
goto have_ip;
|
||||
}
|
||||
}
|
||||
}
|
||||
// grow list if needed.
|
||||
if(internal_ips->capa < internal_ips->counter + 1) {
|
||||
PDEBUG("realloc\n");
|
||||
new_mem = realloc(internal_ips->list, (internal_ips->capa + 16) * sizeof(void *));
|
||||
if(new_mem) {
|
||||
internal_ips->capa += 16;
|
||||
internal_ips->list = new_mem;
|
||||
} else {
|
||||
oom:
|
||||
PDEBUG("out of mem\n");
|
||||
goto err_plus_unlock;
|
||||
}
|
||||
}
|
||||
|
||||
res = make_internal_ip(internal_ips->counter);
|
||||
if(res.as_int == ip_type_invalid.addr.v4.as_int)
|
||||
goto err_plus_unlock;
|
||||
|
||||
string_hash_tuple tmp = { 0 };
|
||||
new_mem = dumpstring((char*) &tmp, sizeof(string_hash_tuple));
|
||||
if(!new_mem)
|
||||
goto oom;
|
||||
|
||||
PDEBUG("creating new entry %d for ip of %s\n", (int) internal_ips->counter, name);
|
||||
|
||||
internal_ips->list[internal_ips->counter] = new_mem;
|
||||
internal_ips->list[internal_ips->counter]->hash = hash;
|
||||
|
||||
new_mem = dumpstring((char*) name, len + 1);
|
||||
|
||||
if(!new_mem) {
|
||||
internal_ips->list[internal_ips->counter] = 0;
|
||||
goto oom;
|
||||
}
|
||||
internal_ips->list[internal_ips->counter]->string = new_mem;
|
||||
|
||||
internal_ips->counter += 1;
|
||||
|
||||
have_ip:
|
||||
|
||||
return res;
|
||||
err_plus_unlock:
|
||||
|
||||
PDEBUG("return err\n");
|
||||
return ip_type_invalid.addr.v4;
|
||||
}
|
||||
|
||||
/* stuff for communication with the allocator thread */
|
||||
|
||||
enum at_msgtype {
|
||||
ATM_GETIP,
|
||||
ATM_GETNAME,
|
||||
ATM_EXIT,
|
||||
};
|
||||
|
||||
enum at_direction {
|
||||
ATD_SERVER = 0,
|
||||
ATD_CLIENT,
|
||||
ATD_MAX,
|
||||
};
|
||||
|
||||
struct at_msghdr {
|
||||
enum at_msgtype msgtype;
|
||||
size_t datalen;
|
||||
};
|
||||
|
||||
static pthread_t allocator_thread;
|
||||
int req_pipefd[2];
|
||||
int resp_pipefd[2];
|
||||
|
||||
static int wait_data(int readfd) {
|
||||
PFUNC();
|
||||
fd_set fds;
|
||||
FD_ZERO(&fds);
|
||||
FD_SET(readfd, &fds);
|
||||
int ret;
|
||||
while((ret = select(readfd+1, &fds, NULL, NULL, NULL)) <= 0) {
|
||||
if(ret < 0) {
|
||||
int e = errno;
|
||||
if(e == EINTR) continue;
|
||||
#ifdef __GLIBC__
|
||||
char emsg[1024];
|
||||
char* x = strerror_r(errno, emsg, sizeof emsg);
|
||||
dprintf(2, "select2: %s\n", x);
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int trywrite(int fd, void* buf, size_t bytes) {
|
||||
ssize_t ret;
|
||||
unsigned char *out = buf;
|
||||
again:
|
||||
ret = write(fd, out, bytes);
|
||||
switch(ret) {
|
||||
case -1:
|
||||
if(errno == EINTR) goto again;
|
||||
case 0:
|
||||
return 0;
|
||||
default:
|
||||
if(ret == bytes || !bytes) return 1;
|
||||
out += ret;
|
||||
bytes -= ret;
|
||||
goto again;
|
||||
}
|
||||
}
|
||||
|
||||
static int sendmessage(enum at_direction dir, struct at_msghdr *hdr, void* data) {
|
||||
static int* destfd[ATD_MAX] = { [ATD_SERVER] = &req_pipefd[1], [ATD_CLIENT] = &resp_pipefd[1] };
|
||||
int ret = trywrite(*destfd[dir], hdr, sizeof *hdr);
|
||||
if(ret && hdr->datalen) {
|
||||
assert(hdr->datalen <= MSG_LEN_MAX);
|
||||
ret = trywrite(*destfd[dir], data, hdr->datalen);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int tryread(int fd, void* buf, size_t bytes) {
|
||||
ssize_t ret;
|
||||
unsigned char *out = buf;
|
||||
again:
|
||||
ret = read(fd, out, bytes);
|
||||
switch(ret) {
|
||||
case -1:
|
||||
if(errno == EINTR) goto again;
|
||||
case 0:
|
||||
return 0;
|
||||
default:
|
||||
if(ret == bytes || !bytes) return 1;
|
||||
out += ret;
|
||||
bytes -= ret;
|
||||
goto again;
|
||||
}
|
||||
}
|
||||
|
||||
static int getmessage(enum at_direction dir, struct at_msghdr *hdr, void* data) {
|
||||
static int* readfd[ATD_MAX] = { [ATD_SERVER] = &req_pipefd[0], [ATD_CLIENT] = &resp_pipefd[0] };
|
||||
ssize_t ret;
|
||||
if((ret = wait_data(*readfd[dir]))) {
|
||||
if(!tryread(*readfd[dir], hdr, sizeof *hdr))
|
||||
return 0;
|
||||
assert(hdr->datalen <= MSG_LEN_MAX);
|
||||
if(hdr->datalen) {
|
||||
ret = tryread(*readfd[dir], data, hdr->datalen);
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void* threadfunc(void* x) {
|
||||
(void) x;
|
||||
int ret;
|
||||
struct at_msghdr msg;
|
||||
union {
|
||||
char host[MSG_LEN_MAX];
|
||||
ip_type4 ip;
|
||||
} readbuf;
|
||||
while((ret = getmessage(ATD_SERVER, &msg, &readbuf))) {
|
||||
switch(msg.msgtype) {
|
||||
case ATM_GETIP:
|
||||
/* client wants an ip for a DNS name. iterate our list and check if we have an existing entry.
|
||||
* if not, create a new one. */
|
||||
readbuf.ip = ip_from_internal_list(readbuf.host, msg.datalen - 1);
|
||||
msg.datalen = sizeof(ip_type4);
|
||||
break;
|
||||
case ATM_GETNAME: {
|
||||
char *host = string_from_internal_ip(readbuf.ip);
|
||||
if(host) {
|
||||
size_t l = strlen(host);
|
||||
assert(l < MSG_LEN_MAX);
|
||||
memcpy(readbuf.host, host, l + 1);
|
||||
msg.datalen = l + 1;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case ATM_EXIT:
|
||||
return 0;
|
||||
default:
|
||||
abort();
|
||||
}
|
||||
ret = sendmessage(ATD_CLIENT, &msg, &readbuf);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* API to access the internal ip mapping */
|
||||
|
||||
ip_type4 at_get_ip_for_host(char* host, size_t len) {
|
||||
ip_type4 readbuf;
|
||||
MUTEX_LOCK(internal_ips_lock);
|
||||
if(len > MSG_LEN_MAX) goto inv;
|
||||
struct at_msghdr msg = {.msgtype = ATM_GETIP, .datalen = len + 1 };
|
||||
if(sendmessage(ATD_SERVER, &msg, host) &&
|
||||
getmessage(ATD_CLIENT, &msg, &readbuf));
|
||||
else {
|
||||
inv:
|
||||
readbuf = ip_type_invalid.addr.v4;
|
||||
}
|
||||
assert(msg.msgtype == ATM_GETIP);
|
||||
MUTEX_UNLOCK(internal_ips_lock);
|
||||
return readbuf;
|
||||
}
|
||||
|
||||
size_t at_get_host_for_ip(ip_type4 ip, char* readbuf) {
|
||||
struct at_msghdr msg = {.msgtype = ATM_GETNAME, .datalen = sizeof(ip_type4) };
|
||||
size_t res = 0;
|
||||
MUTEX_LOCK(internal_ips_lock);
|
||||
if(sendmessage(ATD_SERVER, &msg, &ip) && getmessage(ATD_CLIENT, &msg, readbuf)) {
|
||||
if((ptrdiff_t) msg.datalen <= 0) res = 0;
|
||||
else res = msg.datalen - 1;
|
||||
}
|
||||
assert(msg.msgtype == ATM_GETNAME);
|
||||
MUTEX_UNLOCK(internal_ips_lock);
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
static void initpipe(int* fds) {
|
||||
int retval;
|
||||
|
||||
#ifdef HAVE_PIPE2
|
||||
retval = pipe2(fds, O_CLOEXEC);
|
||||
#else
|
||||
retval = pipe(fds);
|
||||
if(retval == 0) {
|
||||
fcntl(fds[0], F_SETFD, FD_CLOEXEC);
|
||||
fcntl(fds[1], F_SETFD, FD_CLOEXEC);
|
||||
}
|
||||
#endif
|
||||
if(retval == -1) {
|
||||
perror("pipe");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
#ifndef MAX
|
||||
#define MAX(x, y) ((x) > (y) ? (x) : (y))
|
||||
#endif
|
||||
|
||||
#if !defined(PTHREAD_STACK_MIN) || defined(__APPLE__)
|
||||
/* MAC says its min is 8KB, but then crashes in our face. thx hunkOLard */
|
||||
#define PTHREAD_STACK_MIN 64*1024
|
||||
#endif
|
||||
|
||||
/* initialize with pointers to shared memory. these will
|
||||
* be used to place responses and arguments */
|
||||
void at_init(void) {
|
||||
PFUNC();
|
||||
void *shm = mmap(0, 4096, PROT_WRITE|PROT_READ, MAP_ANON|MAP_SHARED, -1, 0);
|
||||
assert(shm);
|
||||
internal_ips_lock = shm;
|
||||
internal_ips = (void*)((char*)shm + 2048);
|
||||
|
||||
MUTEX_INIT(internal_ips_lock);
|
||||
memset(internal_ips, 0, sizeof *internal_ips);
|
||||
initpipe(req_pipefd);
|
||||
initpipe(resp_pipefd);
|
||||
pthread_attr_t allocator_thread_attr;
|
||||
pthread_attr_init(&allocator_thread_attr);
|
||||
pthread_attr_setstacksize(&allocator_thread_attr, MAX(16 * 1024, PTHREAD_STACK_MIN));
|
||||
pthread_create(&allocator_thread, &allocator_thread_attr, threadfunc, 0);
|
||||
pthread_attr_destroy(&allocator_thread_attr);
|
||||
}
|
||||
|
||||
void at_close(void) {
|
||||
PFUNC();
|
||||
const int msg = ATM_EXIT;
|
||||
write(req_pipefd[1], &msg, sizeof(int));
|
||||
pthread_join(allocator_thread, NULL);
|
||||
close(req_pipefd[0]);
|
||||
close(req_pipefd[1]);
|
||||
close(resp_pipefd[0]);
|
||||
close(resp_pipefd[1]);
|
||||
MUTEX_DESTROY(internal_ips_lock);
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
#ifndef ALLOCATOR_THREAD_H
|
||||
#define ALLOCATOR_THREAD_H
|
||||
|
||||
#include <unistd.h>
|
||||
#include "ip_type.h"
|
||||
|
||||
#define MSG_LEN_MAX 256
|
||||
|
||||
extern int req_pipefd[2];
|
||||
extern int resp_pipefd[2];
|
||||
|
||||
void at_init(void);
|
||||
void at_close(void);
|
||||
size_t at_get_host_for_ip(ip_type4 ip, char* readbuf);
|
||||
ip_type4 at_get_ip_for_host(char* host, size_t len);
|
||||
|
||||
//RcB: DEP "allocator_thread.c"
|
||||
#endif
|
||||
|
||||
+47
-3
@@ -3,6 +3,50 @@
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
|
||||
const char *proxy_type_strmap[] = {
|
||||
"http",
|
||||
"socks4",
|
||||
"socks5",
|
||||
};
|
||||
|
||||
const char *chain_type_strmap[] = {
|
||||
"dynamic_chain",
|
||||
"strict_chain",
|
||||
"random_chain",
|
||||
"round_robin_chain",
|
||||
};
|
||||
|
||||
const char *proxy_state_strmap[] = {
|
||||
"play",
|
||||
"down",
|
||||
"blocked",
|
||||
"busy",
|
||||
};
|
||||
|
||||
// stolen from libulz (C) rofl0r
|
||||
void pc_stringfromipv4(unsigned char *ip_buf_4_bytes, char *outbuf_16_bytes) {
|
||||
unsigned char *p;
|
||||
char *o = outbuf_16_bytes;
|
||||
unsigned char n;
|
||||
for(p = ip_buf_4_bytes; p < ip_buf_4_bytes + 4; p++) {
|
||||
n = *p;
|
||||
if(*p >= 100) {
|
||||
if(*p >= 200)
|
||||
*(o++) = '2';
|
||||
else
|
||||
*(o++) = '1';
|
||||
n %= 100;
|
||||
}
|
||||
if(*p >= 10) {
|
||||
*(o++) = (n / 10) + '0';
|
||||
n %= 10;
|
||||
}
|
||||
*(o++) = n + '0';
|
||||
*(o++) = '.';
|
||||
}
|
||||
o[-1] = 0;
|
||||
}
|
||||
|
||||
static int check_path(char *path) {
|
||||
if(!path)
|
||||
return 0;
|
||||
@@ -15,7 +59,7 @@ char *get_config_path(char* default_path, char* pbuf, size_t bufsize) {
|
||||
char *path = default_path;
|
||||
if(check_path(path))
|
||||
goto have;
|
||||
|
||||
|
||||
// priority 1: env var PROXYCHAINS_CONF_FILE
|
||||
path = getenv(PROXYCHAINS_CONF_FILE_ENV_VAR);
|
||||
if(check_path(path))
|
||||
@@ -44,10 +88,10 @@ char *get_config_path(char* default_path, char* pbuf, size_t bufsize) {
|
||||
path = "/etc/" PROXYCHAINS_CONF_FILE;
|
||||
if(check_path(path))
|
||||
goto have;
|
||||
|
||||
|
||||
perror("couldnt find configuration file");
|
||||
exit(1);
|
||||
|
||||
|
||||
return NULL;
|
||||
have:
|
||||
return path;
|
||||
|
||||
+11
-2
@@ -1,3 +1,6 @@
|
||||
#ifndef COMMON_H
|
||||
#define COMMON_H
|
||||
|
||||
#define PROXYCHAINS_CONF_FILE_ENV_VAR "PROXYCHAINS_CONF_FILE"
|
||||
#define PROXYCHAINS_QUIET_MODE_ENV_VAR "PROXYCHAINS_QUIET_MODE"
|
||||
#define PROXYCHAINS_CONF_FILE "proxychains.conf"
|
||||
@@ -8,6 +11,12 @@
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
char *get_config_path(char* default_path, char* pbuf, size_t bufsize);
|
||||
extern const char *proxy_type_strmap[];
|
||||
extern const char *chain_type_strmap[];
|
||||
extern const char *proxy_state_strmap[];
|
||||
|
||||
//RcB: DEP "common.c"
|
||||
char *get_config_path(char* default_path, char* pbuf, size_t bufsize);
|
||||
void pc_stringfromipv4(unsigned char *ip_buf_4_bytes, char *outbuf_16_bytes);
|
||||
|
||||
//RcB: DEP "common.c"
|
||||
#endif
|
||||
|
||||
+326
-370
@@ -27,94 +27,25 @@
|
||||
#include <arpa/inet.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/poll.h>
|
||||
#include <poll.h>
|
||||
#include <sys/wait.h>
|
||||
#include <fcntl.h>
|
||||
#include <time.h>
|
||||
#include <sys/time.h>
|
||||
#include <stdarg.h>
|
||||
#include <assert.h>
|
||||
#ifdef THREAD_SAFE
|
||||
#include <pthread.h>
|
||||
pthread_mutex_t internal_ips_lock;
|
||||
pthread_mutex_t hostdb_lock;
|
||||
#endif
|
||||
|
||||
#include "core.h"
|
||||
#include "common.h"
|
||||
#include "allocator_thread.h"
|
||||
#include "mutex.h"
|
||||
|
||||
extern int tcp_read_time_out;
|
||||
extern int tcp_connect_time_out;
|
||||
extern int proxychains_quiet_mode;
|
||||
extern unsigned int proxychains_proxy_offset;
|
||||
extern unsigned int remote_dns_subnet;
|
||||
|
||||
internal_ip_lookup_table internal_ips = { 0, 0, NULL };
|
||||
|
||||
|
||||
uint32_t dalias_hash(char *s0) {
|
||||
unsigned char *s = (void *) s0;
|
||||
uint_fast32_t h = 0;
|
||||
while(*s) {
|
||||
h = 16 * h + *s++;
|
||||
h ^= h >> 24 & 0xf0;
|
||||
}
|
||||
return h & 0xfffffff;
|
||||
}
|
||||
|
||||
uint32_t index_from_internal_ip(ip_type internalip) {
|
||||
ip_type tmp = internalip;
|
||||
uint32_t ret;
|
||||
ret = tmp.octet[3] + (tmp.octet[2] << 8) + (tmp.octet[1] << 16);
|
||||
ret -= 1;
|
||||
return ret;
|
||||
}
|
||||
|
||||
char *string_from_internal_ip(ip_type internalip) {
|
||||
char *res = NULL;
|
||||
uint32_t index = index_from_internal_ip(internalip);
|
||||
MUTEX_LOCK(&internal_ips_lock);
|
||||
if(index < internal_ips.counter)
|
||||
res = internal_ips.list[index]->string;
|
||||
MUTEX_UNLOCK(&internal_ips_lock);
|
||||
return res;
|
||||
}
|
||||
|
||||
in_addr_t make_internal_ip(uint32_t index) {
|
||||
ip_type ret;
|
||||
index++; // so we can start at .0.0.1
|
||||
if(index > 0xFFFFFF)
|
||||
return (in_addr_t) - 1;
|
||||
ret.octet[0] = remote_dns_subnet & 0xFF;
|
||||
ret.octet[1] = (index & 0xFF0000) >> 16;
|
||||
ret.octet[2] = (index & 0xFF00) >> 8;
|
||||
ret.octet[3] = index & 0xFF;
|
||||
return (in_addr_t) ret.as_int;
|
||||
}
|
||||
|
||||
// stolen from libulz (C) rofl0r
|
||||
void pc_stringfromipv4(unsigned char *ip_buf_4_bytes, char *outbuf_16_bytes) {
|
||||
unsigned char *p;
|
||||
char *o = outbuf_16_bytes;
|
||||
unsigned char n;
|
||||
for(p = ip_buf_4_bytes; p < ip_buf_4_bytes + 4; p++) {
|
||||
n = *p;
|
||||
if(*p >= 100) {
|
||||
if(*p >= 200)
|
||||
*(o++) = '2';
|
||||
else
|
||||
*(o++) = '1';
|
||||
n %= 100;
|
||||
}
|
||||
if(*p >= 10) {
|
||||
*(o++) = (n / 10) + '0';
|
||||
n %= 10;
|
||||
}
|
||||
*(o++) = n + '0';
|
||||
*(o++) = '.';
|
||||
}
|
||||
o[-1] = 0;
|
||||
}
|
||||
|
||||
static int poll_retry(struct pollfd *fds, nfds_t nfsd, int timeout) {
|
||||
int ret;
|
||||
int time_remain = timeout;
|
||||
@@ -176,7 +107,7 @@ static void encode_base_64(char *src, char *dest, int max_len) {
|
||||
}
|
||||
|
||||
void proxychains_write_log(char *str, ...) {
|
||||
char buff[1024*20];
|
||||
char buff[1024*4];
|
||||
va_list arglist;
|
||||
if(!proxychains_quiet_mode) {
|
||||
va_start(arglist, str);
|
||||
@@ -220,6 +151,7 @@ static int timed_connect(int sock, const struct sockaddr *addr, socklen_t len) {
|
||||
int ret, value;
|
||||
socklen_t value_len;
|
||||
struct pollfd pfd[1];
|
||||
PFUNC();
|
||||
|
||||
pfd[0].fd = sock;
|
||||
pfd[0].events = POLLOUT;
|
||||
@@ -258,21 +190,19 @@ static int timed_connect(int sock, const struct sockaddr *addr, socklen_t len) {
|
||||
#define INVALID_INDEX 0xFFFFFFFFU
|
||||
static int tunnel_to(int sock, ip_type ip, unsigned short port, proxy_type pt, char *user, char *pass) {
|
||||
char *dns_name = NULL;
|
||||
char hostnamebuf[MSG_LEN_MAX];
|
||||
size_t dns_len = 0;
|
||||
|
||||
PDEBUG("tunnel_to()\n");
|
||||
PFUNC();
|
||||
|
||||
// we use ip addresses with 224.* to lookup their dns name in our table, to allow remote DNS resolution
|
||||
// the range 224-255.* is reserved, and it won't go outside (unless the app does some other stuff with
|
||||
// the results returned from gethostbyname et al.)
|
||||
// the hardcoded number 224 can now be changed using the config option remote_dns_subnet to i.e. 127
|
||||
if(ip.octet[0] == remote_dns_subnet) {
|
||||
dns_name = string_from_internal_ip(ip);
|
||||
if(!dns_name)
|
||||
goto err;
|
||||
dns_len = strlen(dns_name);
|
||||
if(!dns_len)
|
||||
goto err;
|
||||
if(!ip.is_v6 && ip.addr.v4.octet[0] == remote_dns_subnet) {
|
||||
dns_len = at_get_host_for_ip(ip.addr.v4, hostnamebuf);
|
||||
if(!dns_len) goto err;
|
||||
else dns_name = hostnamebuf;
|
||||
}
|
||||
|
||||
PDEBUG("host dns %s\n", dns_name ? dns_name : "<NULL>");
|
||||
@@ -287,207 +217,201 @@ static int tunnel_to(int sock, ip_type ip, unsigned short port, proxy_type pt, c
|
||||
|
||||
int len;
|
||||
unsigned char buff[BUFF_SIZE];
|
||||
char ip_buf[16];
|
||||
char ip_buf[INET6_ADDRSTRLEN];
|
||||
int v6 = ip.is_v6;
|
||||
|
||||
//memset (buff, 0, sizeof(buff));
|
||||
|
||||
switch (pt) {
|
||||
case HTTP_TYPE:{
|
||||
if(!dns_len) {
|
||||
pc_stringfromipv4(&ip.octet[0], ip_buf);
|
||||
dns_name = ip_buf;
|
||||
}
|
||||
|
||||
snprintf((char *) buff, sizeof(buff), "CONNECT %s:%d HTTP/1.0\r\n", dns_name,
|
||||
ntohs(port));
|
||||
|
||||
if(user[0]) {
|
||||
#define HTTP_AUTH_MAX ((0xFF * 2) + 1 + 1)
|
||||
// 2 * 0xff: username and pass, plus 1 for ':' and 1 for zero terminator.
|
||||
char src[HTTP_AUTH_MAX];
|
||||
char dst[(4 * HTTP_AUTH_MAX)];
|
||||
|
||||
memcpy(src, user, ulen);
|
||||
memcpy(src + ulen, ":", 1);
|
||||
memcpy(src + ulen + 1, pass, passlen);
|
||||
src[ulen + 1 + passlen] = 0;
|
||||
|
||||
encode_base_64(src, dst, sizeof(dst));
|
||||
strcat((char *) buff, "Proxy-Authorization: Basic ");
|
||||
strcat((char *) buff, dst);
|
||||
strcat((char *) buff, "\r\n\r\n");
|
||||
} else
|
||||
strcat((char *) buff, "\r\n");
|
||||
|
||||
len = strlen((char *) buff);
|
||||
|
||||
if(len != send(sock, buff, len, 0))
|
||||
if(!dns_len) {
|
||||
if(!inet_ntop(v6?AF_INET6:AF_INET,ip.addr.v6,ip_buf,sizeof ip_buf)) {
|
||||
proxychains_write_log(LOG_PREFIX "error: ip address conversion failed\n");
|
||||
goto err;
|
||||
|
||||
len = 0;
|
||||
// read header byte by byte.
|
||||
while(len < BUFF_SIZE) {
|
||||
if(1 == read_n_bytes(sock, (char *) (buff + len), 1))
|
||||
len++;
|
||||
else
|
||||
goto err;
|
||||
if(len > 4 &&
|
||||
buff[len - 1] == '\n' &&
|
||||
buff[len - 2] == '\r' && buff[len - 3] == '\n' && buff[len - 4] == '\r')
|
||||
break;
|
||||
}
|
||||
|
||||
// if not ok (200) or response greather than BUFF_SIZE return BLOCKED;
|
||||
if(len == BUFF_SIZE || !(buff[9] == '2' && buff[10] == '0' && buff[11] == '0'))
|
||||
return BLOCKED;
|
||||
|
||||
return SUCCESS;
|
||||
dns_name = ip_buf;
|
||||
}
|
||||
break;
|
||||
#define HTTP_AUTH_MAX ((0xFF * 2) + 1 + 1) /* 2 * 0xff: username and pass, plus 1 for ':' and 1 for zero terminator. */
|
||||
char src[HTTP_AUTH_MAX];
|
||||
char dst[(4 * HTTP_AUTH_MAX)];
|
||||
if(ulen) {
|
||||
snprintf(src, sizeof(src), "%s:%s", user, pass);
|
||||
encode_base_64(src, dst, sizeof(dst));
|
||||
} else dst[0] = 0;
|
||||
|
||||
uint16_t hs_port = ntohs(port);
|
||||
len = snprintf((char *) buff, sizeof(buff),
|
||||
"CONNECT %s:%d HTTP/1.0\r\nHost: %s:%d\r\n%s%s%s\r\n",
|
||||
dns_name, hs_port,
|
||||
dns_name, hs_port,
|
||||
ulen ? "Proxy-Authorization: Basic " : dst,
|
||||
dst, ulen ? "\r\n" : dst);
|
||||
|
||||
if(len < 0 || len != send(sock, buff, len, 0))
|
||||
goto err;
|
||||
|
||||
len = 0;
|
||||
// read header byte by byte.
|
||||
while(len < BUFF_SIZE) {
|
||||
if(1 == read_n_bytes(sock, (char *) (buff + len), 1))
|
||||
len++;
|
||||
else
|
||||
goto err;
|
||||
if(len > 4 &&
|
||||
buff[len - 1] == '\n' &&
|
||||
buff[len - 2] == '\r' && buff[len - 3] == '\n' && buff[len - 4] == '\r')
|
||||
break;
|
||||
}
|
||||
|
||||
// if not ok (200) or response greather than BUFF_SIZE return BLOCKED;
|
||||
if(len == BUFF_SIZE || !(buff[9] == '2' && buff[10] == '0' && buff[11] == '0')) {
|
||||
PDEBUG("HTTP proxy blocked: buff=\"%s\"\n", buff);
|
||||
return BLOCKED;
|
||||
}
|
||||
|
||||
return SUCCESS;
|
||||
}
|
||||
break;
|
||||
case SOCKS4_TYPE:{
|
||||
buff[0] = 4; // socks version
|
||||
buff[1] = 1; // connect command
|
||||
memcpy(&buff[2], &port, 2); // dest port
|
||||
if(dns_len) {
|
||||
ip.octet[0] = 0;
|
||||
ip.octet[1] = 0;
|
||||
ip.octet[2] = 0;
|
||||
ip.octet[3] = 1;
|
||||
}
|
||||
memcpy(&buff[4], &ip, 4); // dest host
|
||||
len = ulen + 1; // username
|
||||
if(len > 1)
|
||||
memcpy(&buff[8], user, len);
|
||||
else {
|
||||
buff[8] = 0;
|
||||
}
|
||||
|
||||
// do socksv4a dns resolution on the server
|
||||
if(dns_len) {
|
||||
memcpy(&buff[8 + len], dns_name, dns_len + 1);
|
||||
len += dns_len + 1;
|
||||
}
|
||||
|
||||
if((len + 8) != write_n_bytes(sock, (char *) buff, (8 + len)))
|
||||
goto err;
|
||||
|
||||
if(8 != read_n_bytes(sock, (char *) buff, 8))
|
||||
goto err;
|
||||
|
||||
if(buff[0] != 0 || buff[1] != 90)
|
||||
return BLOCKED;
|
||||
|
||||
return SUCCESS;
|
||||
if(v6) {
|
||||
proxychains_write_log(LOG_PREFIX "error: SOCKS4 doesn't support ipv6 addresses\n");
|
||||
goto err;
|
||||
}
|
||||
break;
|
||||
buff[0] = 4; // socks version
|
||||
buff[1] = 1; // connect command
|
||||
memcpy(&buff[2], &port, 2); // dest port
|
||||
if(dns_len) {
|
||||
ip.addr.v4.octet[0] = 0;
|
||||
ip.addr.v4.octet[1] = 0;
|
||||
ip.addr.v4.octet[2] = 0;
|
||||
ip.addr.v4.octet[3] = 1;
|
||||
}
|
||||
memcpy(&buff[4], &ip.addr.v4, 4); // dest host
|
||||
len = ulen + 1; // username
|
||||
if(len > 1)
|
||||
memcpy(&buff[8], user, len);
|
||||
else {
|
||||
buff[8] = 0;
|
||||
}
|
||||
|
||||
// do socksv4a dns resolution on the server
|
||||
if(dns_len) {
|
||||
memcpy(&buff[8 + len], dns_name, dns_len + 1);
|
||||
len += dns_len + 1;
|
||||
}
|
||||
|
||||
if((len + 8) != write_n_bytes(sock, (char *) buff, (8 + len)))
|
||||
goto err;
|
||||
|
||||
if(8 != read_n_bytes(sock, (char *) buff, 8))
|
||||
goto err;
|
||||
|
||||
if(buff[0] != 0 || buff[1] != 90)
|
||||
return BLOCKED;
|
||||
|
||||
return SUCCESS;
|
||||
}
|
||||
break;
|
||||
case SOCKS5_TYPE:{
|
||||
if(user) {
|
||||
buff[0] = 5; //version
|
||||
buff[1] = 2; //nomber of methods
|
||||
buff[2] = 0; // no auth method
|
||||
buff[3] = 2; /// auth method -> username / password
|
||||
if(4 != write_n_bytes(sock, (char *) buff, 4))
|
||||
goto err;
|
||||
} else {
|
||||
buff[0] = 5; //version
|
||||
buff[1] = 1; //nomber of methods
|
||||
buff[2] = 0; // no auth method
|
||||
if(3 != write_n_bytes(sock, (char *) buff, 3))
|
||||
goto err;
|
||||
}
|
||||
int n_methods = ulen ? 2 : 1;
|
||||
buff[0] = 5; // version
|
||||
buff[1] = n_methods ; // number of methods
|
||||
buff[2] = 0; // no auth method
|
||||
if(ulen) buff[3] = 2; /// auth method -> username / password
|
||||
if(2+n_methods != write_n_bytes(sock, (char *) buff, 2+n_methods))
|
||||
goto err;
|
||||
|
||||
if(2 != read_n_bytes(sock, (char *) buff, 2))
|
||||
if(2 != read_n_bytes(sock, (char *) buff, 2))
|
||||
goto err;
|
||||
|
||||
if(buff[0] != 5 || (buff[1] != 0 && buff[1] != 2)) {
|
||||
if(buff[0] == 5 && buff[1] == 0xFF)
|
||||
return BLOCKED;
|
||||
else
|
||||
goto err;
|
||||
|
||||
if(buff[0] != 5 || (buff[1] != 0 && buff[1] != 2)) {
|
||||
if(buff[0] == 5 && buff[1] == 0xFF)
|
||||
return BLOCKED;
|
||||
else
|
||||
goto err;
|
||||
}
|
||||
|
||||
if(buff[1] == 2) {
|
||||
// authentication
|
||||
char in[2];
|
||||
char out[515];
|
||||
char *cur = out;
|
||||
size_t c;
|
||||
*cur++ = 1; // version
|
||||
c = ulen & 0xFF;
|
||||
*cur++ = c;
|
||||
memcpy(cur, user, c);
|
||||
cur += c;
|
||||
c = passlen & 0xFF;
|
||||
*cur++ = c;
|
||||
memcpy(cur, pass, c);
|
||||
cur += c;
|
||||
|
||||
if((cur - out) != write_n_bytes(sock, out, cur - out))
|
||||
goto err;
|
||||
|
||||
|
||||
if(2 != read_n_bytes(sock, in, 2))
|
||||
goto err;
|
||||
if(in[0] != 1 || in[1] != 0) {
|
||||
if(in[0] != 1)
|
||||
goto err;
|
||||
else
|
||||
return BLOCKED;
|
||||
}
|
||||
}
|
||||
int buff_iter = 0;
|
||||
buff[buff_iter++] = 5; // version
|
||||
buff[buff_iter++] = 1; // connect
|
||||
buff[buff_iter++] = 0; // reserved
|
||||
|
||||
if(!dns_len) {
|
||||
buff[buff_iter++] = 1; // ip v4
|
||||
memcpy(buff + buff_iter, &ip, 4); // dest host
|
||||
buff_iter += 4;
|
||||
} else {
|
||||
buff[buff_iter++] = 3; //dns
|
||||
buff[buff_iter++] = dns_len & 0xFF;
|
||||
memcpy(buff + buff_iter, dns_name, dns_len);
|
||||
buff_iter += dns_len;
|
||||
}
|
||||
|
||||
memcpy(buff + buff_iter, &port, 2); // dest port
|
||||
buff_iter += 2;
|
||||
|
||||
|
||||
if(buff_iter != write_n_bytes(sock, (char *) buff, buff_iter))
|
||||
goto err;
|
||||
|
||||
if(4 != read_n_bytes(sock, (char *) buff, 4))
|
||||
goto err;
|
||||
|
||||
if(buff[0] != 5 || buff[1] != 0)
|
||||
goto err;
|
||||
|
||||
switch (buff[3]) {
|
||||
|
||||
case 1:
|
||||
len = 4;
|
||||
break;
|
||||
case 4:
|
||||
len = 16;
|
||||
break;
|
||||
case 3:
|
||||
len = 0;
|
||||
if(1 != read_n_bytes(sock, (char *) &len, 1))
|
||||
goto err;
|
||||
break;
|
||||
default:
|
||||
goto err;
|
||||
}
|
||||
|
||||
if(len + 2 != read_n_bytes(sock, (char *) buff, len + 2))
|
||||
goto err;
|
||||
|
||||
return SUCCESS;
|
||||
}
|
||||
break;
|
||||
|
||||
if(buff[1] == 2) {
|
||||
// authentication
|
||||
char in[2];
|
||||
char out[515];
|
||||
char *cur = out;
|
||||
size_t c;
|
||||
*cur++ = 1; // version
|
||||
c = ulen & 0xFF;
|
||||
*cur++ = c;
|
||||
memcpy(cur, user, c);
|
||||
cur += c;
|
||||
c = passlen & 0xFF;
|
||||
*cur++ = c;
|
||||
memcpy(cur, pass, c);
|
||||
cur += c;
|
||||
|
||||
if((cur - out) != write_n_bytes(sock, out, cur - out))
|
||||
goto err;
|
||||
|
||||
|
||||
if(2 != read_n_bytes(sock, in, 2))
|
||||
goto err;
|
||||
/* according to RFC 1929 the version field for the user/pass auth sub-
|
||||
negotiation should be 1, which is kinda counter-intuitive, so there
|
||||
are some socks5 proxies that return 5 instead. other programs like
|
||||
curl work fine when the version is 5, so let's do the same and accept
|
||||
either of them. */
|
||||
if(!(in[0] == 5 || in[0] == 1))
|
||||
goto err;
|
||||
if(in[1] != 0)
|
||||
return BLOCKED;
|
||||
}
|
||||
int buff_iter = 0;
|
||||
buff[buff_iter++] = 5; // version
|
||||
buff[buff_iter++] = 1; // connect
|
||||
buff[buff_iter++] = 0; // reserved
|
||||
|
||||
if(!dns_len) {
|
||||
buff[buff_iter++] = v6 ? 4 : 1; // ip v4/v6
|
||||
memcpy(buff + buff_iter, ip.addr.v6, v6?16:4); // dest host
|
||||
buff_iter += v6?16:4;
|
||||
} else {
|
||||
buff[buff_iter++] = 3; //dns
|
||||
buff[buff_iter++] = dns_len & 0xFF;
|
||||
memcpy(buff + buff_iter, dns_name, dns_len);
|
||||
buff_iter += dns_len;
|
||||
}
|
||||
|
||||
memcpy(buff + buff_iter, &port, 2); // dest port
|
||||
buff_iter += 2;
|
||||
|
||||
|
||||
if(buff_iter != write_n_bytes(sock, (char *) buff, buff_iter))
|
||||
goto err;
|
||||
|
||||
if(4 != read_n_bytes(sock, (char *) buff, 4))
|
||||
goto err;
|
||||
|
||||
if(buff[0] != 5 || buff[1] != 0)
|
||||
goto err;
|
||||
|
||||
switch (buff[3]) {
|
||||
case 1:
|
||||
len = 4;
|
||||
break;
|
||||
case 4:
|
||||
len = 16;
|
||||
break;
|
||||
case 3:
|
||||
len = 0;
|
||||
if(1 != read_n_bytes(sock, (char *) &len, 1))
|
||||
goto err;
|
||||
break;
|
||||
default:
|
||||
goto err;
|
||||
}
|
||||
|
||||
if(len + 2 != read_n_bytes(sock, (char *) buff, len + 2))
|
||||
goto err;
|
||||
|
||||
return SUCCESS;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
err:
|
||||
@@ -498,24 +422,33 @@ static int tunnel_to(int sock, ip_type ip, unsigned short port, proxy_type pt, c
|
||||
#define DT "Dynamic chain"
|
||||
#define ST "Strict chain"
|
||||
#define RT "Random chain"
|
||||
#define RRT "Round Robin chain"
|
||||
|
||||
static int start_chain(int *fd, proxy_data * pd, char *begin_mark) {
|
||||
struct sockaddr_in addr;
|
||||
char ip_buf[16];
|
||||
int v6 = pd->ip.is_v6;
|
||||
|
||||
*fd = socket(PF_INET, SOCK_STREAM, 0);
|
||||
*fd = socket(v6?PF_INET6:PF_INET, SOCK_STREAM, 0);
|
||||
if(*fd == -1)
|
||||
goto error;
|
||||
|
||||
pc_stringfromipv4(&pd->ip.octet[0], ip_buf);
|
||||
char ip_buf[INET6_ADDRSTRLEN];
|
||||
if(!inet_ntop(v6?AF_INET6:AF_INET,pd->ip.addr.v6,ip_buf,sizeof ip_buf))
|
||||
goto error;
|
||||
|
||||
proxychains_write_log(LOG_PREFIX "%s " TP " %s:%d ",
|
||||
begin_mark, ip_buf, htons(pd->port));
|
||||
pd->ps = PLAY_STATE;
|
||||
memset(&addr, 0, sizeof(addr));
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_addr.s_addr = (in_addr_t) pd->ip.as_int;
|
||||
addr.sin_port = pd->port;
|
||||
if(timed_connect(*fd, (struct sockaddr *) &addr, sizeof(addr))) {
|
||||
struct sockaddr_in addr = {
|
||||
.sin_family = AF_INET,
|
||||
.sin_port = pd->port,
|
||||
.sin_addr.s_addr = (in_addr_t) pd->ip.addr.v4.as_int
|
||||
};
|
||||
struct sockaddr_in6 addr6 = {
|
||||
.sin6_family = AF_INET6,
|
||||
.sin6_port = pd->port,
|
||||
};
|
||||
if(v6) memcpy(&addr6.sin6_addr.s6_addr, pd->ip.addr.v6, 16);
|
||||
if(timed_connect(*fd, (struct sockaddr *) (v6?(void*)&addr6:(void*)&addr), v6?sizeof(addr6):sizeof(addr))) {
|
||||
pd->ps = DOWN_STATE;
|
||||
goto error1;
|
||||
}
|
||||
@@ -535,10 +468,9 @@ static proxy_data *select_proxy(select_type how, proxy_data * pd, unsigned int p
|
||||
return NULL;
|
||||
switch (how) {
|
||||
case RANDOMLY:
|
||||
srand(time(NULL));
|
||||
do {
|
||||
k++;
|
||||
i = 0 + (unsigned int) (proxy_count * 1.0 * rand() / (RAND_MAX + 1.0));
|
||||
i = rand() % proxy_count;
|
||||
} while(pd[i].ps != PLAY_STATE && k < proxy_count * 100);
|
||||
break;
|
||||
case FIFOLY:
|
||||
@@ -586,17 +518,23 @@ static unsigned int calc_alive(proxy_data * pd, unsigned int proxy_count) {
|
||||
static int chain_step(int ns, proxy_data * pfrom, proxy_data * pto) {
|
||||
int retcode = -1;
|
||||
char *hostname;
|
||||
char ip_buf[16];
|
||||
char hostname_buf[MSG_LEN_MAX];
|
||||
char ip_buf[INET6_ADDRSTRLEN];
|
||||
int v6 = pto->ip.is_v6;
|
||||
|
||||
PDEBUG("chain_step()\n");
|
||||
PFUNC();
|
||||
|
||||
if(pto->ip.octet[0] == remote_dns_subnet) {
|
||||
hostname = string_from_internal_ip(pto->ip);
|
||||
if(!hostname)
|
||||
goto usenumericip;
|
||||
if(!v6 && pto->ip.addr.v4.octet[0] == remote_dns_subnet) {
|
||||
if(!at_get_host_for_ip(pto->ip.addr.v4, hostname_buf)) goto usenumericip;
|
||||
else hostname = hostname_buf;
|
||||
} else {
|
||||
usenumericip:
|
||||
pc_stringfromipv4(&pto->ip.octet[0], ip_buf);
|
||||
if(!inet_ntop(v6?AF_INET6:AF_INET,pto->ip.addr.v6,ip_buf,sizeof ip_buf)) {
|
||||
pto->ps = DOWN_STATE;
|
||||
proxychains_write_log("<--ip conversion error!\n");
|
||||
close(ns);
|
||||
return SOCKET_ERROR;
|
||||
}
|
||||
hostname = ip_buf;
|
||||
}
|
||||
|
||||
@@ -626,15 +564,20 @@ int connect_proxy_chain(int sock, ip_type target_ip,
|
||||
proxy_data p4;
|
||||
proxy_data *p1, *p2, *p3;
|
||||
int ns = -1;
|
||||
int rc = -1;
|
||||
unsigned int offset = 0;
|
||||
unsigned int alive_count = 0;
|
||||
unsigned int curr_len = 0;
|
||||
unsigned int looped = 0; // went back to start of list in RR mode
|
||||
unsigned int rr_loop_max = 14;
|
||||
|
||||
p3 = &p4;
|
||||
|
||||
PDEBUG("connect_proxy_chain\n");
|
||||
PFUNC();
|
||||
|
||||
again:
|
||||
rc = -1;
|
||||
DUMP_PROXY_CHAIN(pd, proxy_count);
|
||||
|
||||
switch (ct) {
|
||||
case DYNAMIC_TYPE:
|
||||
@@ -661,6 +604,56 @@ int connect_proxy_chain(int sock, ip_type target_ip,
|
||||
goto error;
|
||||
break;
|
||||
|
||||
case ROUND_ROBIN_TYPE:
|
||||
alive_count = calc_alive(pd, proxy_count);
|
||||
offset = proxychains_proxy_offset;
|
||||
if(alive_count < max_chain)
|
||||
goto error_more;
|
||||
PDEBUG("1:rr_offset = %d\n", offset);
|
||||
/* Check from current RR offset til end */
|
||||
for (;rc != SUCCESS;) {
|
||||
if (!(p1 = select_proxy(FIFOLY, pd, proxy_count, &offset))) {
|
||||
/* We've reached the end of the list, go to the start */
|
||||
offset = 0;
|
||||
looped++;
|
||||
if (looped > rr_loop_max) {
|
||||
proxychains_proxy_offset = 0;
|
||||
goto error_more;
|
||||
} else {
|
||||
PDEBUG("rr_type all proxies down, release all\n");
|
||||
release_all(pd, proxy_count);
|
||||
/* Each loop we wait 10ms more */
|
||||
usleep(10000 * looped);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
PDEBUG("2:rr_offset = %d\n", offset);
|
||||
rc=start_chain(&ns, p1, RRT);
|
||||
}
|
||||
/* Create rest of chain using RR */
|
||||
for(curr_len = 1; curr_len < max_chain;) {
|
||||
PDEBUG("3:rr_offset = %d, curr_len = %d, max_chain = %d\n", offset, curr_len, max_chain);
|
||||
p2 = select_proxy(FIFOLY, pd, proxy_count, &offset);
|
||||
if(!p2) {
|
||||
/* Try from the beginning to where we started */
|
||||
offset = 0;
|
||||
continue;
|
||||
} else if(SUCCESS != chain_step(ns, p1, p2)) {
|
||||
PDEBUG("GOTO AGAIN 1\n");
|
||||
goto again;
|
||||
} else
|
||||
p1 = p2;
|
||||
curr_len++;
|
||||
}
|
||||
//proxychains_write_log(TP);
|
||||
p3->ip = target_ip;
|
||||
p3->port = target_port;
|
||||
proxychains_proxy_offset = offset+1;
|
||||
PDEBUG("pd_offset = %d, curr_len = %d\n", proxychains_proxy_offset, curr_len);
|
||||
if(SUCCESS != chain_step(ns, p1, p3))
|
||||
goto error;
|
||||
break;
|
||||
|
||||
case STRICT_TYPE:
|
||||
alive_count = calc_alive(pd, proxy_count);
|
||||
offset = 0;
|
||||
@@ -736,21 +729,24 @@ int connect_proxy_chain(int sock, ip_type target_ip,
|
||||
return -1;
|
||||
}
|
||||
|
||||
static const ip_type local_host = { {127, 0, 0, 1} };
|
||||
static pthread_mutex_t servbyname_lock;
|
||||
void core_initialize(void) {
|
||||
MUTEX_INIT(&servbyname_lock);
|
||||
}
|
||||
|
||||
void core_unload(void) {
|
||||
MUTEX_DESTROY(&servbyname_lock);
|
||||
}
|
||||
|
||||
static void gethostbyname_data_setstring(struct gethostbyname_data* data, char* name) {
|
||||
snprintf(data->addr_name, sizeof(data->addr_name), "%s", name);
|
||||
data->hostent_space.h_name = data->addr_name;
|
||||
}
|
||||
|
||||
extern ip_type4 hostsreader_get_numeric_ip_for_name(const char* name);
|
||||
struct hostent *proxy_gethostbyname(const char *name, struct gethostbyname_data* data) {
|
||||
PFUNC();
|
||||
char buff[256];
|
||||
uint32_t i, hash;
|
||||
// yep, new_mem never gets freed. once you passed a fake ip to the client, you can't "retreat" it
|
||||
void *new_mem;
|
||||
size_t l;
|
||||
|
||||
struct hostent *hp;
|
||||
|
||||
data->resolved_addr_p[0] = (char *) &data->resolved_addr;
|
||||
data->resolved_addr_p[1] = NULL;
|
||||
@@ -768,82 +764,27 @@ struct hostent *proxy_gethostbyname(const char *name, struct gethostbyname_data*
|
||||
if(!strcmp(buff, name)) {
|
||||
data->resolved_addr = inet_addr(buff);
|
||||
if(data->resolved_addr == (in_addr_t) (-1))
|
||||
data->resolved_addr = (in_addr_t) (local_host.as_int);
|
||||
data->resolved_addr = (in_addr_t) (ip_type_localhost.addr.v4.as_int);
|
||||
goto retname;
|
||||
}
|
||||
|
||||
memset(buff, 0, sizeof(buff));
|
||||
|
||||
// this iterates over the "known hosts" db, usually /etc/hosts
|
||||
MUTEX_LOCK(&hostdb_lock);
|
||||
while((hp = gethostent()))
|
||||
if(!strcmp(hp->h_name, name) && hp->h_addrtype == AF_INET && hp->h_length == sizeof(in_addr_t)) {
|
||||
data->resolved_addr = *((in_addr_t*)(hp->h_addr_list[0]));
|
||||
MUTEX_UNLOCK(&hostdb_lock);
|
||||
goto retname;
|
||||
}
|
||||
MUTEX_UNLOCK(&hostdb_lock);
|
||||
|
||||
hash = dalias_hash((char *) name);
|
||||
|
||||
MUTEX_LOCK(&internal_ips_lock);
|
||||
|
||||
// see if we already have this dns entry saved.
|
||||
if(internal_ips.counter) {
|
||||
for(i = 0; i < internal_ips.counter; i++) {
|
||||
if(internal_ips.list[i]->hash == hash && !strcmp(name, internal_ips.list[i]->string)) {
|
||||
data->resolved_addr = make_internal_ip(i);
|
||||
PDEBUG("got cached ip for %s\n", name);
|
||||
goto have_ip;
|
||||
}
|
||||
}
|
||||
ip_type4 hdb_res = hostsreader_get_numeric_ip_for_name(name);
|
||||
if(hdb_res.as_int != ip_type_invalid.addr.v4.as_int) {
|
||||
data->resolved_addr = hdb_res.as_int;
|
||||
goto retname;
|
||||
}
|
||||
// grow list if needed.
|
||||
if(internal_ips.capa < internal_ips.counter + 1) {
|
||||
PDEBUG("realloc\n");
|
||||
new_mem = realloc(internal_ips.list, (internal_ips.capa + 16) * sizeof(void *));
|
||||
if(new_mem) {
|
||||
internal_ips.capa += 16;
|
||||
internal_ips.list = new_mem;
|
||||
} else {
|
||||
oom:
|
||||
proxychains_write_log("out of mem\n");
|
||||
goto err_plus_unlock;
|
||||
}
|
||||
}
|
||||
|
||||
data->resolved_addr = make_internal_ip(internal_ips.counter);
|
||||
if(data->resolved_addr == (in_addr_t) - 1)
|
||||
goto err_plus_unlock;
|
||||
|
||||
l = strlen(name);
|
||||
new_mem = malloc(sizeof(string_hash_tuple) + l + 1);
|
||||
if(!new_mem)
|
||||
goto oom;
|
||||
|
||||
PDEBUG("creating new entry %d for ip of %s\n", (int) internal_ips.counter, name);
|
||||
|
||||
internal_ips.list[internal_ips.counter] = new_mem;
|
||||
internal_ips.list[internal_ips.counter]->hash = hash;
|
||||
internal_ips.list[internal_ips.counter]->string = (char *) new_mem + sizeof(string_hash_tuple);
|
||||
|
||||
memcpy(internal_ips.list[internal_ips.counter]->string, name, l + 1);
|
||||
|
||||
internal_ips.counter += 1;
|
||||
|
||||
have_ip:
|
||||
|
||||
MUTEX_UNLOCK(&internal_ips_lock);
|
||||
|
||||
data->resolved_addr = at_get_ip_for_host((char*) name, strlen(name)).as_int;
|
||||
if(data->resolved_addr == (in_addr_t) ip_type_invalid.addr.v4.as_int) return NULL;
|
||||
|
||||
retname:
|
||||
|
||||
gethostbyname_data_setstring(data, (char*) name);
|
||||
|
||||
PDEBUG("return hostent space\n");
|
||||
|
||||
return &data->hostent_space;
|
||||
|
||||
err_plus_unlock:
|
||||
MUTEX_UNLOCK(&internal_ips_lock);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct addrinfo_data {
|
||||
@@ -853,16 +794,21 @@ struct addrinfo_data {
|
||||
};
|
||||
|
||||
void proxy_freeaddrinfo(struct addrinfo *res) {
|
||||
PFUNC();
|
||||
free(res);
|
||||
}
|
||||
|
||||
#ifdef IS_MAC
|
||||
/* getservbyname on mac is using thread local storage, so we dont need mutex */
|
||||
static int getservbyname_r(const char* name, const char* proto, struct servent* result_buf,
|
||||
static int mygetservbyname_r(const char* name, const char* proto, struct servent* result_buf,
|
||||
char* buf, size_t buflen, struct servent** result) {
|
||||
PFUNC();
|
||||
#ifdef HAVE_GNU_GETSERVBYNAME_R
|
||||
PDEBUG("using host getservbyname_r\n");
|
||||
return getservbyname_r(name, proto, result_buf, buf, buflen, result);
|
||||
#endif
|
||||
struct servent *res;
|
||||
int ret;
|
||||
(void) buf; (void) buflen;
|
||||
MUTEX_LOCK(&servbyname_lock);
|
||||
res = getservbyname(name, proto);
|
||||
if(res) {
|
||||
*result_buf = *res;
|
||||
@@ -872,9 +818,9 @@ static int getservbyname_r(const char* name, const char* proto, struct servent*
|
||||
*result = NULL;
|
||||
ret = ENOENT;
|
||||
}
|
||||
MUTEX_UNLOCK(&servbyname_lock);
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
|
||||
int proxy_getaddrinfo(const char *node, const char *service, const struct addrinfo *hints, struct addrinfo **res) {
|
||||
struct gethostbyname_data ghdata;
|
||||
@@ -885,12 +831,19 @@ int proxy_getaddrinfo(const char *node, const char *service, const struct addrin
|
||||
struct addrinfo *p;
|
||||
char buf[1024];
|
||||
int port;
|
||||
PFUNC();
|
||||
|
||||
// printf("proxy_getaddrinfo node %s service %s\n",node,service);
|
||||
space = calloc(1, sizeof(struct addrinfo_data));
|
||||
if(!space) goto err1;
|
||||
|
||||
|
||||
if(node && !inet_aton(node, &((struct sockaddr_in *) &space->sockaddr_space)->sin_addr)) {
|
||||
/* some folks (nmap) use getaddrinfo() with AI_NUMERICHOST to check whether a string
|
||||
containing a numeric ip was passed. we must return failure in that case. */
|
||||
if(hints && (hints->ai_flags & AI_NUMERICHOST)) {
|
||||
free(space);
|
||||
return EAI_NONAME;
|
||||
}
|
||||
hp = proxy_gethostbyname(node, &ghdata);
|
||||
if(hp)
|
||||
memcpy(&((struct sockaddr_in *) &space->sockaddr_space)->sin_addr,
|
||||
@@ -898,17 +851,17 @@ int proxy_getaddrinfo(const char *node, const char *service, const struct addrin
|
||||
else
|
||||
goto err2;
|
||||
}
|
||||
if(service) getservbyname_r(service, NULL, &se_buf, buf, sizeof(buf), &se);
|
||||
if(service) mygetservbyname_r(service, NULL, &se_buf, buf, sizeof(buf), &se);
|
||||
|
||||
port = se ? se->s_port : htons(atoi(service ? service : "0"));
|
||||
((struct sockaddr_in *) &space->sockaddr_space)->sin_port = port;
|
||||
|
||||
*res = p = &space->addrinfo_space;
|
||||
assert((size_t)p == (size_t) space);
|
||||
|
||||
|
||||
p->ai_addr = &space->sockaddr_space;
|
||||
if(node)
|
||||
strncpy(space->addr_name, node, sizeof(space->addr_name));
|
||||
snprintf(space->addr_name, sizeof(space->addr_name), "%s", node);
|
||||
p->ai_canonname = space->addr_name;
|
||||
p->ai_next = NULL;
|
||||
p->ai_family = space->sockaddr_space.sa_family = AF_INET;
|
||||
@@ -919,9 +872,12 @@ int proxy_getaddrinfo(const char *node, const char *service, const struct addrin
|
||||
p->ai_flags = hints->ai_flags;
|
||||
p->ai_protocol = hints->ai_protocol;
|
||||
} else {
|
||||
#ifndef AI_V4MAPPED
|
||||
#define AI_V4MAPPED 0
|
||||
#endif
|
||||
p->ai_flags = (AI_V4MAPPED | AI_ADDRCONFIG);
|
||||
}
|
||||
|
||||
|
||||
goto out;
|
||||
err2:
|
||||
free(space);
|
||||
|
||||
+15
-38
@@ -14,6 +14,7 @@
|
||||
* *
|
||||
***************************************************************************/
|
||||
|
||||
#include <unistd.h>
|
||||
#include <stdint.h>
|
||||
#include <netinet/in.h>
|
||||
#include <sys/types.h>
|
||||
@@ -25,35 +26,7 @@
|
||||
#define BUFF_SIZE 8*1024 // used to read responses from proxies.
|
||||
#define MAX_LOCALNET 64
|
||||
|
||||
typedef union {
|
||||
unsigned char octet[4];
|
||||
uint32_t as_int;
|
||||
} ip_type;
|
||||
|
||||
typedef struct {
|
||||
uint32_t hash;
|
||||
char* string;
|
||||
} string_hash_tuple;
|
||||
|
||||
typedef struct {
|
||||
uint32_t counter;
|
||||
uint32_t capa;
|
||||
string_hash_tuple** list;
|
||||
} internal_ip_lookup_table;
|
||||
|
||||
extern internal_ip_lookup_table internal_ips;
|
||||
#ifdef THREAD_SAFE
|
||||
#include <pthread.h>
|
||||
extern pthread_mutex_t internal_ips_lock;
|
||||
extern pthread_mutex_t hostdb_lock;
|
||||
# define MUTEX_LOCK(x) pthread_mutex_lock(x)
|
||||
# define MUTEX_UNLOCK(x) pthread_mutex_unlock(x)
|
||||
# define MUTEX_INIT(x,y) pthread_mutex_init(x, y)
|
||||
#else
|
||||
# define MUTEX_LOCK(x)
|
||||
# define MUTEX_UNLOCK(x)
|
||||
# define MUTEX_INIT(x,y)
|
||||
#endif
|
||||
#include "ip_type.h"
|
||||
|
||||
/*error codes*/
|
||||
typedef enum {
|
||||
@@ -74,8 +47,9 @@ typedef enum {
|
||||
typedef enum {
|
||||
DYNAMIC_TYPE,
|
||||
STRICT_TYPE,
|
||||
RANDOM_TYPE}
|
||||
chain_type;
|
||||
RANDOM_TYPE,
|
||||
ROUND_ROBIN_TYPE
|
||||
} chain_type;
|
||||
|
||||
typedef enum {
|
||||
PLAY_STATE,
|
||||
@@ -109,6 +83,7 @@ int connect_proxy_chain (int sock, ip_type target_ip, unsigned short target_port
|
||||
|
||||
void proxychains_write_log(char *str, ...);
|
||||
|
||||
typedef int (*close_t)(int);
|
||||
typedef int (*connect_t)(int, const struct sockaddr *, socklen_t);
|
||||
typedef struct hostent* (*gethostbyname_t)(const char *);
|
||||
typedef int (*freeaddrinfo_t)(struct addrinfo *);
|
||||
@@ -120,6 +95,10 @@ typedef int (*getaddrinfo_t)(const char *, const char *, const struct addrinfo *
|
||||
typedef int (*getnameinfo_t) (const struct sockaddr *, socklen_t, char *,
|
||||
socklen_t, char *, socklen_t, int);
|
||||
|
||||
typedef ssize_t (*sendto_t) (int sockfd, const void *buf, size_t len, int flags,
|
||||
const struct sockaddr *dest_addr, socklen_t addrlen);
|
||||
|
||||
|
||||
|
||||
extern connect_t true_connect;
|
||||
extern gethostbyname_t true_gethostbyname;
|
||||
@@ -141,16 +120,14 @@ int proxy_getaddrinfo(const char *node, const char *service,
|
||||
const struct addrinfo *hints, struct addrinfo **res);
|
||||
void proxy_freeaddrinfo(struct addrinfo *res);
|
||||
|
||||
void pc_stringfromipv4(unsigned char *ip_buf_4_bytes, char *outbuf_16_bytes);
|
||||
void core_initialize(void);
|
||||
void core_unload(void);
|
||||
|
||||
#ifdef DEBUG
|
||||
# define PDEBUG(fmt, args...) do { fprintf(stderr,"DEBUG:"fmt, ## args); fflush(stderr); } while(0)
|
||||
#else
|
||||
# define PDEBUG(fmt, args...) do {} while (0)
|
||||
#endif
|
||||
#include "debug.h"
|
||||
|
||||
#endif
|
||||
|
||||
//RcB: DEP "core.c"
|
||||
//RcB: DEP "libproxychains.c"
|
||||
//RcB: LINK "-Wl,--no-as-needed -ldl -lpthread"
|
||||
//RcB: LINK "-Wl,--no-as-needed -ldl -lpthread"
|
||||
|
||||
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
|
||||
#ifdef DEBUG
|
||||
# include "core.h"
|
||||
# include "common.h"
|
||||
# include "debug.h"
|
||||
#include <arpa/inet.h>
|
||||
|
||||
void DUMP_PROXY_CHAIN(proxy_data *pchain, unsigned int count) {
|
||||
char ip_buf[INET6_ADDRSTRLEN];
|
||||
for (; count; pchain++, count--) {
|
||||
if(!inet_ntop(pchain->ip.is_v6?AF_INET6:AF_INET,pchain->ip.addr.v6,ip_buf,sizeof ip_buf)) {
|
||||
proxychains_write_log(LOG_PREFIX "error: ip address conversion failed\n");
|
||||
continue;
|
||||
}
|
||||
PDEBUG("[%s] %s %s:%d", proxy_state_strmap[pchain->ps],
|
||||
proxy_type_strmap[pchain->pt],
|
||||
ip_buf, htons(pchain->port));
|
||||
if (*pchain->user || *pchain->pass) {
|
||||
PSTDERR(" [u=%s,p=%s]", pchain->user, pchain->pass);
|
||||
}
|
||||
PSTDERR("\n");
|
||||
}
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
// Do not allow this translation unit to end up empty
|
||||
// for non-DEBUG builds, to satisfy ISO C standards.
|
||||
typedef int __appease_iso_compilers__;
|
||||
|
||||
#endif
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
#ifndef DEBUG_H
|
||||
#define DEBUG_H
|
||||
|
||||
#ifdef DEBUG
|
||||
# include <stdio.h>
|
||||
# define PSTDERR(fmt, args...) do { dprintf(2,fmt, ## args); } while(0)
|
||||
# define PDEBUG(fmt, args...) PSTDERR("DEBUG:"fmt, ## args)
|
||||
# define DEBUGDECL(args...) args
|
||||
|
||||
# include "core.h"
|
||||
void DUMP_PROXY_CHAIN(proxy_data *pchain, unsigned int count);
|
||||
|
||||
#else
|
||||
# define PDEBUG(fmt, args...) do {} while (0)
|
||||
# define DEBUGDECL(args...)
|
||||
# define DUMP_PROXY_CHAIN(args...) do {} while (0)
|
||||
#endif
|
||||
|
||||
# define PFUNC() do { PDEBUG("pid[%d]:%s\n", getpid(), __FUNCTION__); } while(0)
|
||||
|
||||
#endif
|
||||
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
#include "hash.h"
|
||||
|
||||
/* dalias' version of the elf hash */
|
||||
uint32_t dalias_hash(char *s0) {
|
||||
unsigned char *s = (void *) s0;
|
||||
uint_fast32_t h = 0;
|
||||
while(*s) {
|
||||
h = 16 * h + *s++;
|
||||
h ^= h >> 24 & 0xf0;
|
||||
}
|
||||
return h & 0xfffffff;
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
#ifndef HASH_H
|
||||
#define HASH_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
uint32_t dalias_hash(char *s0);
|
||||
|
||||
//RcB: DEP "hash.c"
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,123 @@
|
||||
#include <stdio.h>
|
||||
#include <ctype.h>
|
||||
#include <string.h>
|
||||
|
||||
/*
|
||||
simple reader for /etc/hosts
|
||||
it only supports comments, blank lines and lines consisting of an ipv4 hostname pair.
|
||||
this is required so we can return entries from the host db without messing up the
|
||||
non-thread-safe state of libc's gethostent().
|
||||
|
||||
*/
|
||||
|
||||
struct hostsreader {
|
||||
FILE *f;
|
||||
char* ip, *name;
|
||||
};
|
||||
|
||||
int hostsreader_open(struct hostsreader *ctx) {
|
||||
if(!(ctx->f = fopen("/etc/hosts", "r"))) return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
void hostsreader_close(struct hostsreader *ctx) {
|
||||
fclose(ctx->f);
|
||||
}
|
||||
|
||||
static int isnumericipv4(const char* ipstring);
|
||||
int hostsreader_get(struct hostsreader *ctx, char* buf, size_t bufsize) {
|
||||
while(1) {
|
||||
if(!fgets(buf, bufsize, ctx->f)) return 0;
|
||||
if(*buf == '#') continue;
|
||||
char *p = buf;
|
||||
size_t l = bufsize;
|
||||
ctx->ip = p;
|
||||
while(*p && !isspace(*p) && l) {
|
||||
p++;
|
||||
l--;
|
||||
}
|
||||
if(!l || !*p || p == ctx->ip) continue;
|
||||
*p = 0;
|
||||
p++;
|
||||
while(*p && isspace(*p) && l) {
|
||||
p++;
|
||||
l--;
|
||||
}
|
||||
if(!l || !*p) continue;
|
||||
ctx->name = p;
|
||||
while(*p && !isspace(*p) && l) {
|
||||
p++;
|
||||
l--;
|
||||
}
|
||||
if(!l || !*p) continue;
|
||||
*p = 0;
|
||||
if(isnumericipv4(ctx->ip)) return 1;
|
||||
}
|
||||
}
|
||||
|
||||
char* hostsreader_get_ip_for_name(const char* name, char* buf, size_t bufsize) {
|
||||
struct hostsreader ctx;
|
||||
char *res = 0;
|
||||
if(!hostsreader_open(&ctx)) return 0;
|
||||
while(hostsreader_get(&ctx, buf, bufsize)) {
|
||||
if(!strcmp(ctx.name, name)) {
|
||||
res = ctx.ip;
|
||||
break;
|
||||
}
|
||||
}
|
||||
hostsreader_close(&ctx);
|
||||
return res;
|
||||
}
|
||||
|
||||
#include "ip_type.h"
|
||||
#include <netinet/in.h>
|
||||
#include <sys/socket.h>
|
||||
#include <arpa/inet.h>
|
||||
ip_type4 hostsreader_get_numeric_ip_for_name(const char* name) {
|
||||
char *hres;
|
||||
char buf[320];
|
||||
if((hres = hostsreader_get_ip_for_name(name, buf, sizeof buf))) {
|
||||
struct in_addr c;
|
||||
inet_aton(hres, &c);
|
||||
ip_type4 res;
|
||||
memcpy(res.octet, &c.s_addr, 4);
|
||||
return res;
|
||||
} else return ip_type_invalid.addr.v4;
|
||||
}
|
||||
|
||||
#ifdef HOSTSREADER_TEST
|
||||
#include "ip_type.c"
|
||||
int main(int a, char**b) {
|
||||
char buf[256];
|
||||
if(a != 2) return 1;
|
||||
char * ret = hostsreader_get_ip_for_name(b[1], buf, sizeof buf);
|
||||
printf("%s\n", ret ? ret : "null");
|
||||
}
|
||||
#endif
|
||||
|
||||
/* isnumericipv4() taken from libulz */
|
||||
static int isnumericipv4(const char* ipstring) {
|
||||
size_t x = 0, n = 0, d = 0;
|
||||
int wasdot = 0;
|
||||
while(1) {
|
||||
switch(ipstring[x]) {
|
||||
case 0: goto done;
|
||||
case '.':
|
||||
if(!n || wasdot) return 0;
|
||||
d++;
|
||||
wasdot = 1;
|
||||
break;
|
||||
case '0': case '1': case '2': case '3': case '4':
|
||||
case '5': case '6': case '7': case '8': case '9':
|
||||
n++;
|
||||
wasdot = 0;
|
||||
break;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
x++;
|
||||
}
|
||||
done:
|
||||
if(d == 3 && n >= 4 && n <= 12) return 1;
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
#include "ip_type.h"
|
||||
|
||||
const ip_type ip_type_invalid = { .addr.v4.as_int = -1 };
|
||||
const ip_type ip_type_localhost = { .addr.v4.octet = {127, 0, 0, 1} };
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
#ifndef IP_TYPE_H
|
||||
#define IP_TYPE_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
typedef union {
|
||||
unsigned char octet[4];
|
||||
uint32_t as_int;
|
||||
} ip_type4;
|
||||
|
||||
typedef struct {
|
||||
union {
|
||||
ip_type4 v4;
|
||||
unsigned char v6[16];
|
||||
} addr;
|
||||
char is_v6;
|
||||
} ip_type;
|
||||
|
||||
extern const ip_type ip_type_invalid;
|
||||
extern const ip_type ip_type_localhost;
|
||||
|
||||
//RcB: DEP "ip_type.c"
|
||||
#endif
|
||||
+308
-76
@@ -23,6 +23,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <assert.h>
|
||||
#include <netdb.h>
|
||||
|
||||
#include <netinet/in.h>
|
||||
@@ -31,6 +32,7 @@
|
||||
#include <sys/socket.h>
|
||||
#include <fcntl.h>
|
||||
#include <dlfcn.h>
|
||||
#include <pthread.h>
|
||||
|
||||
|
||||
#include "core.h"
|
||||
@@ -43,18 +45,27 @@
|
||||
#define SOCKFAMILY(x) (satosin(x)->sin_family)
|
||||
#define MAX_CHAIN 512
|
||||
|
||||
#ifdef IS_SOLARIS
|
||||
#undef connect
|
||||
int __xnet_connect(int sock, const struct sockaddr *addr, unsigned int len);
|
||||
connect_t true___xnet_connect;
|
||||
#endif
|
||||
|
||||
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;
|
||||
sendto_t true_sendto;
|
||||
|
||||
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;
|
||||
unsigned int proxychains_proxy_offset = 0;
|
||||
int proxychains_got_chain_data = 0;
|
||||
unsigned int proxychains_max_chain = 1;
|
||||
int proxychains_quiet_mode = 0;
|
||||
@@ -63,17 +74,16 @@ localaddr_arg localnet_addr[MAX_LOCALNET];
|
||||
size_t num_localnet_addr = 0;
|
||||
unsigned int remote_dns_subnet = 224;
|
||||
|
||||
#ifdef THREAD_SAFE
|
||||
pthread_once_t init_once = PTHREAD_ONCE_INIT;
|
||||
#endif
|
||||
|
||||
static int init_l = 0;
|
||||
|
||||
static inline void get_chain_data(proxy_data * pd, unsigned int *proxy_count, chain_type * ct);
|
||||
static void get_chain_data(proxy_data * pd, unsigned int *proxy_count, chain_type * ct);
|
||||
|
||||
static void* load_sym(char* symname, void* proxyfunc) {
|
||||
|
||||
void *funcptr = dlsym(RTLD_NEXT, symname);
|
||||
|
||||
|
||||
if(!funcptr) {
|
||||
fprintf(stderr, "Cannot load symbol '%s' %s\n", symname, dlerror());
|
||||
exit(1);
|
||||
@@ -89,23 +99,44 @@ static void* load_sym(char* symname, void* proxyfunc) {
|
||||
|
||||
#define INIT() init_lib_wrapper(__FUNCTION__)
|
||||
|
||||
#define SETUP_SYM(X) do { true_ ## X = load_sym( # X, X ); } while(0)
|
||||
#define SETUP_SYM(X) do { if (! true_ ## X ) true_ ## X = load_sym( # X, X ); } while(0)
|
||||
|
||||
static void do_init(void) {
|
||||
MUTEX_INIT(&internal_ips_lock, NULL);
|
||||
MUTEX_INIT(&hostdb_lock, NULL);
|
||||
/* read the config file */
|
||||
get_chain_data(proxychains_pd, &proxychains_proxy_count, &proxychains_ct);
|
||||
#include "allocator_thread.h"
|
||||
|
||||
proxychains_write_log(LOG_PREFIX "DLL init\n");
|
||||
|
||||
const char *proxychains_get_version(void);
|
||||
|
||||
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);
|
||||
#ifdef IS_SOLARIS
|
||||
SETUP_SYM(__xnet_connect);
|
||||
#endif
|
||||
}
|
||||
|
||||
static int close_fds[16];
|
||||
static int close_fds_cnt = 0;
|
||||
|
||||
static void do_init(void) {
|
||||
srand(time(NULL));
|
||||
core_initialize();
|
||||
at_init();
|
||||
|
||||
/* read the config file */
|
||||
get_chain_data(proxychains_pd, &proxychains_proxy_count, &proxychains_ct);
|
||||
DUMP_PROXY_CHAIN(proxychains_pd, proxychains_proxy_count);
|
||||
|
||||
proxychains_write_log(LOG_PREFIX "DLL init: proxychains-ng %s\n", proxychains_get_version());
|
||||
|
||||
setup_hooks();
|
||||
|
||||
while(close_fds_cnt) true_close(close_fds[--close_fds_cnt]);
|
||||
|
||||
init_l = 1;
|
||||
}
|
||||
|
||||
@@ -113,14 +144,8 @@ static void init_lib_wrapper(const char* caller) {
|
||||
#ifndef DEBUG
|
||||
(void) caller;
|
||||
#endif
|
||||
#ifndef THREAD_SAFE
|
||||
if(init_l) return;
|
||||
PDEBUG("%s called from %s\n", __FUNCTION__, caller);
|
||||
do_init();
|
||||
#else
|
||||
if(!init_l) PDEBUG("%s called from %s\n", __FUNCTION__, caller);
|
||||
pthread_once(&init_once, do_init);
|
||||
#endif
|
||||
}
|
||||
|
||||
/* if we use gcc >= 3, we can instruct the dynamic loader
|
||||
@@ -135,6 +160,107 @@ static void gcc_init(void) {
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
static const char* bool_str(int bool_val) {
|
||||
if(bool_val) return "true";
|
||||
return "false";
|
||||
}
|
||||
|
||||
/* 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;
|
||||
@@ -151,9 +277,13 @@ static void get_chain_data(proxy_data * pd, unsigned int *proxy_count, chain_typ
|
||||
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));
|
||||
file = fopen(env, "r");
|
||||
if( ( file = fopen(env, "r") ) == NULL )
|
||||
{
|
||||
perror("couldnt read configuration file");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
env = getenv(PROXYCHAINS_QUIET_MODE_ENV_VAR);
|
||||
if(env && *env == '1')
|
||||
@@ -165,16 +295,41 @@ static void get_chain_data(proxy_data * pd, unsigned int *proxy_count, chain_typ
|
||||
if(list) {
|
||||
if(count >= MAX_CHAIN)
|
||||
break;
|
||||
|
||||
|
||||
memset(&pd[count], 0, sizeof(proxy_data));
|
||||
|
||||
pd[count].ps = PLAY_STATE;
|
||||
port_n = 0;
|
||||
|
||||
sscanf(buff, "%s %s %d %s %s", type, host, &port_n, pd[count].user, pd[count].pass);
|
||||
int ret = sscanf(buff, "%s %s %d %s %s", type, host, &port_n, pd[count].user, pd[count].pass);
|
||||
if(ret < 3 || ret == EOF) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
pd[count].ip.as_int = (uint32_t) inet_addr(host);
|
||||
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)) {
|
||||
if(*ct == STRICT_TYPE && proxychains_resolver && count > 0) {
|
||||
/* we can allow dns hostnames for all but the first proxy in the list if chaintype is strict, as remote lookup can be done */
|
||||
ip_type4 internal_ip = at_get_ip_for_host(host, strlen(host));
|
||||
pd[count].ip.is_v6 = 0;
|
||||
host_ip->addr.v4 = internal_ip;
|
||||
if(internal_ip.as_int == ip_type_invalid.addr.v4.as_int)
|
||||
goto inv_host;
|
||||
} else {
|
||||
inv_host:
|
||||
fprintf(stderr, "proxy %s has invalid value or is not numeric\n", host);
|
||||
fprintf(stderr, "non-numeric ips are only allowed under the following circumstances:\n");
|
||||
fprintf(stderr, "chaintype == strict (%s), proxy is not first in list (%s), proxy_dns active (%s)\n\n", bool_str(*ct == STRICT_TYPE), bool_str(count > 0), bool_str(proxychains_resolver));
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
if(!strcmp(type, "http")) {
|
||||
pd[count].pt = HTTP_TYPE;
|
||||
@@ -183,9 +338,9 @@ static void get_chain_data(proxy_data * pd, unsigned int *proxy_count, chain_typ
|
||||
} else if(!strcmp(type, "socks5")) {
|
||||
pd[count].pt = SOCKS5_TYPE;
|
||||
} else
|
||||
continue;
|
||||
goto inv;
|
||||
|
||||
if(pd[count].ip.as_int && port_n && pd[count].ip.as_int != (uint32_t) - 1)
|
||||
if(port_n)
|
||||
count++;
|
||||
} else {
|
||||
if(strstr(buff, "[ProxyList]")) {
|
||||
@@ -196,12 +351,14 @@ static void get_chain_data(proxy_data * pd, unsigned int *proxy_count, chain_typ
|
||||
*ct = STRICT_TYPE;
|
||||
} else if(strstr(buff, "dynamic_chain")) {
|
||||
*ct = DYNAMIC_TYPE;
|
||||
} else if(strstr(buff, "round_robin_chain")) {
|
||||
*ct = ROUND_ROBIN_TYPE;
|
||||
} else if(strstr(buff, "tcp_read_time_out")) {
|
||||
sscanf(buff, "%s %d", user, &tcp_read_time_out);
|
||||
} else if(strstr(buff, "tcp_connect_time_out")) {
|
||||
sscanf(buff, "%s %d", user, &tcp_connect_time_out);
|
||||
} else if(strstr(buff, "remote_dns_subnet")) {
|
||||
sscanf(buff, "%s %d", user, &remote_dns_subnet);
|
||||
sscanf(buff, "%s %u", user, &remote_dns_subnet);
|
||||
if(remote_dns_subnet >= 256) {
|
||||
fprintf(stderr,
|
||||
"remote_dns_subnet: invalid value. requires a number between 0 and 255.\n");
|
||||
@@ -252,6 +409,10 @@ static void get_chain_data(proxy_data * pd, unsigned int *proxy_count, chain_typ
|
||||
char *pc;
|
||||
int len;
|
||||
pc = strchr(buff, '=');
|
||||
if(!pc) {
|
||||
fprintf(stderr, "error: missing equals sign '=' in chain_len directive.\n");
|
||||
exit(1);
|
||||
}
|
||||
len = atoi(++pc);
|
||||
proxychains_max_chain = (len ? len : 1);
|
||||
} else if(strstr(buff, "quiet_mode")) {
|
||||
@@ -262,44 +423,85 @@ static void get_chain_data(proxy_data * pd, unsigned int *proxy_count, chain_typ
|
||||
}
|
||||
}
|
||||
}
|
||||
#ifndef BROKEN_FCLOSE
|
||||
fclose(file);
|
||||
#endif
|
||||
if(!count) {
|
||||
fprintf(stderr, "error: no valid proxy found in config\n");
|
||||
exit(1);
|
||||
}
|
||||
*proxy_count = count;
|
||||
proxychains_got_chain_data = 1;
|
||||
PDEBUG("proxy_dns: %s\n", proxychains_resolver ? "ON" : "OFF");
|
||||
}
|
||||
|
||||
/******* HOOK FUNCTIONS *******/
|
||||
|
||||
int close(int fd) {
|
||||
if(!init_l) {
|
||||
if(close_fds_cnt>=(sizeof close_fds/sizeof close_fds[0])) goto err;
|
||||
close_fds[close_fds_cnt++] = fd;
|
||||
errno = 0;
|
||||
return 0;
|
||||
}
|
||||
/* 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);
|
||||
}
|
||||
err:
|
||||
errno = EBADF;
|
||||
return -1;
|
||||
}
|
||||
static int is_v4inv6(const struct in6_addr *a) {
|
||||
return !memcmp(a->s6_addr, "\0\0\0\0\0\0\0\0\0\0\xff\xff", 12);
|
||||
}
|
||||
int connect(int sock, const struct sockaddr *addr, unsigned int len) {
|
||||
INIT();
|
||||
PFUNC();
|
||||
|
||||
int socktype = 0, flags = 0, ret = 0;
|
||||
socklen_t optlen = 0;
|
||||
ip_type dest_ip;
|
||||
#ifdef DEBUG
|
||||
char str[256];
|
||||
#endif
|
||||
DEBUGDECL(char str[256]);
|
||||
|
||||
struct in_addr *p_addr_in;
|
||||
struct in6_addr *p_addr_in6;
|
||||
unsigned short port;
|
||||
size_t i;
|
||||
int remote_dns_connect = 0;
|
||||
INIT();
|
||||
optlen = sizeof(socktype);
|
||||
sa_family_t fam = SOCKFAMILY(*addr);
|
||||
getsockopt(sock, SOL_SOCKET, SO_TYPE, &socktype, &optlen);
|
||||
if(!(SOCKFAMILY(*addr) == AF_INET && socktype == SOCK_STREAM))
|
||||
if(!((fam == AF_INET || fam == AF_INET6) && socktype == SOCK_STREAM))
|
||||
return true_connect(sock, addr, len);
|
||||
|
||||
int v6 = dest_ip.is_v6 = fam == AF_INET6;
|
||||
|
||||
p_addr_in = &((struct sockaddr_in *) addr)->sin_addr;
|
||||
port = ntohs(((struct sockaddr_in *) addr)->sin_port);
|
||||
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);
|
||||
struct in_addr v4inv6;
|
||||
if(v6 && is_v4inv6(p_addr_in6)) {
|
||||
memcpy(&v4inv6.s_addr, &p_addr_in6->s6_addr[12], 4);
|
||||
v6 = dest_ip.is_v6 = 0;
|
||||
p_addr_in = &v4inv6;
|
||||
}
|
||||
if(!v6 && !memcmp(p_addr_in, "\0\0\0\0", 4)) {
|
||||
errno = ECONNREFUSED;
|
||||
return -1;
|
||||
}
|
||||
|
||||
#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("target: %s\n", inet_ntop(v6 ? AF_INET6 : AF_INET, v6 ? (void*)p_addr_in6 : (void*)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);
|
||||
remote_dns_connect = !v6 && (ntohl(p_addr_in->s_addr) >> 24 == remote_dns_subnet);
|
||||
|
||||
for(i = 0; i < num_localnet_addr && !remote_dns_connect; i++) {
|
||||
if (!v6) 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) {
|
||||
@@ -313,11 +515,11 @@ int connect(int sock, const struct sockaddr *addr, unsigned int len) {
|
||||
if(flags & O_NONBLOCK)
|
||||
fcntl(sock, F_SETFL, !O_NONBLOCK);
|
||||
|
||||
dest_ip.as_int = SOCKADDR(*addr);
|
||||
memcpy(dest_ip.addr.v6, v6 ? (void*)p_addr_in6 : (void*)p_addr_in, v6?16:4);
|
||||
|
||||
ret = connect_proxy_chain(sock,
|
||||
dest_ip,
|
||||
SOCKPORT(*addr),
|
||||
htons(port),
|
||||
proxychains_pd, proxychains_proxy_count, proxychains_ct, proxychains_max_chain);
|
||||
|
||||
fcntl(sock, F_SETFL, flags);
|
||||
@@ -326,10 +528,15 @@ int connect(int sock, const struct sockaddr *addr, unsigned int len) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
#ifdef IS_SOLARIS
|
||||
int __xnet_connect(int sock, const struct sockaddr *addr, unsigned int len) {
|
||||
return connect(sock, addr, len);
|
||||
}
|
||||
#endif
|
||||
|
||||
static struct gethostbyname_data ghbndata;
|
||||
struct hostent *gethostbyname(const char *name) {
|
||||
INIT();
|
||||
|
||||
PDEBUG("gethostbyname: %s\n", name);
|
||||
|
||||
if(proxychains_resolver)
|
||||
@@ -341,73 +548,79 @@ struct hostent *gethostbyname(const char *name) {
|
||||
}
|
||||
|
||||
int getaddrinfo(const char *node, const char *service, const struct addrinfo *hints, struct addrinfo **res) {
|
||||
int ret = 0;
|
||||
|
||||
INIT();
|
||||
|
||||
PDEBUG("getaddrinfo: %s %s\n", node, service);
|
||||
PDEBUG("getaddrinfo: %s %s\n", node ? node : "null", service ? service : "null");
|
||||
|
||||
if(proxychains_resolver)
|
||||
ret = proxy_getaddrinfo(node, service, hints, res);
|
||||
return proxy_getaddrinfo(node, service, hints, res);
|
||||
else
|
||||
ret = true_getaddrinfo(node, service, hints, res);
|
||||
|
||||
return ret;
|
||||
return true_getaddrinfo(node, service, hints, res);
|
||||
}
|
||||
|
||||
void freeaddrinfo(struct addrinfo *res) {
|
||||
INIT();
|
||||
|
||||
PDEBUG("freeaddrinfo %p \n", res);
|
||||
PDEBUG("freeaddrinfo %p \n", (void *) res);
|
||||
|
||||
if(!proxychains_resolver)
|
||||
true_freeaddrinfo(res);
|
||||
else
|
||||
proxy_freeaddrinfo(res);
|
||||
return;
|
||||
}
|
||||
|
||||
// work around a buggy prototype in GLIBC. according to the bugtracker it has been fixed in git at 02 May 2011.
|
||||
// 2.14 came out in June 2011 so that should be the first fixed version
|
||||
#if defined(__GLIBC__) && (__GLIBC__ < 3) && (__GLIBC_MINOR__ < 14)
|
||||
int getnameinfo(const struct sockaddr *sa,
|
||||
socklen_t salen, char *host, socklen_t hostlen, char *serv, socklen_t servlen, unsigned int flags)
|
||||
#else
|
||||
int getnameinfo(const struct sockaddr *sa,
|
||||
socklen_t salen, char *host, socklen_t hostlen, char *serv, socklen_t servlen, int flags)
|
||||
#endif
|
||||
int pc_getnameinfo(const struct sockaddr *sa, socklen_t salen,
|
||||
char *host, socklen_t hostlen, char *serv,
|
||||
socklen_t servlen, int flags)
|
||||
{
|
||||
char ip_buf[16];
|
||||
int ret = 0;
|
||||
|
||||
INIT();
|
||||
|
||||
PDEBUG("getnameinfo: %s %s\n", host, serv);
|
||||
PFUNC();
|
||||
|
||||
if(!proxychains_resolver) {
|
||||
ret = true_getnameinfo(sa, salen, host, hostlen, serv, servlen, flags);
|
||||
return true_getnameinfo(sa, salen, host, hostlen, serv, servlen, flags);
|
||||
} else {
|
||||
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)))
|
||||
return EAI_FAMILY;
|
||||
if(hostlen) {
|
||||
pc_stringfromipv4((unsigned char*) &(SOCKADDR_2(*sa)), ip_buf);
|
||||
strncpy(host, ip_buf, hostlen);
|
||||
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)) {
|
||||
memcpy(v4inv6buf, &((struct sockaddr_in6*)sa)->sin6_addr.s6_addr[12], 4);
|
||||
ip = v4inv6buf;
|
||||
v6 = 0;
|
||||
} else
|
||||
scopeid = ((struct sockaddr_in6 *)sa)->sin6_scope_id;
|
||||
}
|
||||
if(!inet_ntop(v6?AF_INET6:AF_INET,ip,host,hostlen))
|
||||
return EAI_OVERFLOW;
|
||||
if(scopeid) {
|
||||
size_t l = strlen(host);
|
||||
if(snprintf(host+l, hostlen-l, "%%%u", scopeid) >= hostlen-l)
|
||||
return EAI_OVERFLOW;
|
||||
}
|
||||
}
|
||||
if(servlen) {
|
||||
if(snprintf(serv, servlen, "%d", ntohs(SOCKPORT(*sa))) >= servlen)
|
||||
return EAI_OVERFLOW;
|
||||
}
|
||||
if(servlen)
|
||||
snprintf(serv, servlen, "%d", ntohs(SOCKPORT(*sa)));
|
||||
}
|
||||
return ret;
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct hostent *gethostbyaddr(const void *addr, socklen_t len, int type) {
|
||||
INIT();
|
||||
PDEBUG("TODO: proper gethostbyaddr hook\n");
|
||||
|
||||
static char buf[16];
|
||||
static char ipv4[4];
|
||||
static char *list[2];
|
||||
static char *aliases[1];
|
||||
static struct hostent he;
|
||||
|
||||
INIT();
|
||||
|
||||
PDEBUG("TODO: proper gethostbyaddr hook\n");
|
||||
|
||||
if(!proxychains_resolver)
|
||||
return true_gethostbyaddr(addr, len, type);
|
||||
else {
|
||||
@@ -429,3 +642,22 @@ struct hostent *gethostbyaddr(const void *addr, socklen_t len, int type) {
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#ifndef MSG_FASTOPEN
|
||||
# define MSG_FASTOPEN 0x20000000
|
||||
#endif
|
||||
|
||||
ssize_t sendto(int sockfd, const void *buf, size_t len, int flags,
|
||||
const struct sockaddr *dest_addr, socklen_t addrlen) {
|
||||
INIT();
|
||||
PFUNC();
|
||||
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);
|
||||
}
|
||||
|
||||
+30
-11
@@ -8,9 +8,9 @@
|
||||
***************************************************************************/
|
||||
|
||||
#undef _POSIX_C_SOURCE
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
#define _POSIX_C_SOURCE 200112L
|
||||
#undef _XOPEN_SOURCE
|
||||
#define _XOPEN_SOURCE 700
|
||||
#define _XOPEN_SOURCE 600
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
@@ -24,7 +24,7 @@
|
||||
static int usage(char **argv) {
|
||||
printf("\nUsage:\t%s -q -f config_file program_name [arguments]\n"
|
||||
"\t-q makes proxychains quiet - this overrides the config setting\n"
|
||||
"\t-f allows to manually specify a configfile to use\n"
|
||||
"\t-f allows one to manually specify a configfile to use\n"
|
||||
"\tfor example : proxychains telnet somehost.com\n" "More help in README file\n\n", argv[0]);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
@@ -33,7 +33,9 @@ static const char *dll_name = DLL_NAME;
|
||||
|
||||
static char own_dir[256];
|
||||
static const char *dll_dirs[] = {
|
||||
#ifndef SUPER_SECURE /* CVE-2015-3887 */
|
||||
".",
|
||||
#endif
|
||||
own_dir,
|
||||
LIB_DIR,
|
||||
"/lib",
|
||||
@@ -48,7 +50,11 @@ static void set_own_dir(const char *argv0) {
|
||||
while(l && argv0[l - 1] != '/')
|
||||
l--;
|
||||
if(l == 0)
|
||||
#ifdef SUPER_SECURE
|
||||
memcpy(own_dir, "/dev/null/", 11);
|
||||
#else
|
||||
memcpy(own_dir, ".", 2);
|
||||
#endif
|
||||
else {
|
||||
memcpy(own_dir, argv0, l - 1);
|
||||
own_dir[l] = 0;
|
||||
@@ -66,6 +72,9 @@ int main(int argc, char *argv[]) {
|
||||
size_t i;
|
||||
const char *prefix = NULL;
|
||||
|
||||
if(argc == 2 && !strcmp(argv[1], "--help"))
|
||||
return usage(argv);
|
||||
|
||||
for(i = 0; i < MAX_COMMANDLINE_FLAGS; i++) {
|
||||
if(start_argv < argc && argv[start_argv][0] == '-') {
|
||||
if(argv[start_argv][1] == 'q') {
|
||||
@@ -89,7 +98,7 @@ int main(int argc, char *argv[]) {
|
||||
|
||||
/* check if path of config file has not been passed via command line */
|
||||
path = get_config_path(path, pbuf, sizeof(pbuf));
|
||||
|
||||
|
||||
if(!quiet)
|
||||
fprintf(stderr, LOG_PREFIX "config file found: %s\n", path);
|
||||
|
||||
@@ -103,7 +112,7 @@ int main(int argc, char *argv[]) {
|
||||
// search DLL
|
||||
|
||||
set_own_dir(argv[0]);
|
||||
|
||||
|
||||
i = 0;
|
||||
|
||||
while(dll_dirs[i]) {
|
||||
@@ -122,14 +131,24 @@ int main(int argc, char *argv[]) {
|
||||
if(!quiet)
|
||||
fprintf(stderr, LOG_PREFIX "preloading %s/%s\n", prefix, dll_name);
|
||||
|
||||
#ifndef IS_MAC
|
||||
snprintf(buf, sizeof(buf), "LD_PRELOAD=%s/%s", prefix, dll_name);
|
||||
putenv(buf);
|
||||
#else
|
||||
snprintf(buf, sizeof(buf), "DYLD_INSERT_LIBRARIES=%s/%s", prefix, dll_name);
|
||||
putenv(buf);
|
||||
#ifdef IS_MAC
|
||||
putenv("DYLD_FORCE_FLAT_NAMESPACE=1");
|
||||
#define LD_PRELOAD_ENV "DYLD_INSERT_LIBRARIES"
|
||||
#define LD_PRELOAD_SEP ":"
|
||||
#else
|
||||
#define LD_PRELOAD_ENV "LD_PRELOAD"
|
||||
/* all historic implementations of BSD and linux dynlinkers seem to support
|
||||
space as LD_PRELOAD separator, with colon added only recently.
|
||||
we use the old syntax for maximum compat */
|
||||
#define LD_PRELOAD_SEP " "
|
||||
#endif
|
||||
char *old_val = getenv(LD_PRELOAD_ENV);
|
||||
snprintf(buf, sizeof(buf), LD_PRELOAD_ENV "=%s/%s%s%s",
|
||||
prefix, dll_name,
|
||||
/* append previous LD_PRELOAD content, if existent */
|
||||
old_val ? LD_PRELOAD_SEP : "",
|
||||
old_val ? old_val : "");
|
||||
putenv(buf);
|
||||
execvp(argv[start_argv], &argv[start_argv]);
|
||||
perror("proxychains can't load process....");
|
||||
|
||||
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
#ifndef MUTEX_H
|
||||
#define MUTEX_H
|
||||
|
||||
#include <pthread.h>
|
||||
# define MUTEX_LOCK(x) pthread_mutex_lock(x)
|
||||
# define MUTEX_UNLOCK(x) pthread_mutex_unlock(x)
|
||||
# define MUTEX_INIT(x) pthread_mutex_init(x, NULL)
|
||||
# define MUTEX_DESTROY(x) pthread_mutex_destroy(x)
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,13 @@
|
||||
#include <sys/socket.h>
|
||||
|
||||
extern int pc_getnameinfo(const void *sa, socklen_t salen,
|
||||
char *host, socklen_t hostlen, char *serv,
|
||||
socklen_t servlen, int flags);
|
||||
|
||||
|
||||
int getnameinfo(const void *sa, socklen_t salen,
|
||||
char *host, socklen_t hostlen, char *serv,
|
||||
socklen_t servlen, int flags) {
|
||||
return pc_getnameinfo(sa, salen, host, hostlen, serv, servlen, flags);
|
||||
}
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
#!/bin/sh
|
||||
echo "ProxyChains-3.1 (http://proxychains.sf.net)"
|
||||
|
||||
usage() {
|
||||
|
||||
echo " usage:"
|
||||
echo " $0 [h] [f config-file] <prog> [args]"
|
||||
exit
|
||||
}
|
||||
|
||||
if [ $# = 0 ] ; then
|
||||
usage
|
||||
fi
|
||||
|
||||
if [ $1 = "-h" ]; then
|
||||
usage
|
||||
fi
|
||||
|
||||
if [ "$1" = "-f" ]; then
|
||||
export PROXYCHAINS_CONF_FILE=$2;
|
||||
shift;
|
||||
shift;
|
||||
fi
|
||||
|
||||
export LD_PRELOAD=libproxychains.so.3
|
||||
exec "$@"
|
||||
+18
-2
@@ -22,13 +22,27 @@ strict_chain
|
||||
# all proxies must be online to play in chain
|
||||
# otherwise EINTR is returned to the app
|
||||
#
|
||||
#round_robin_chain
|
||||
#
|
||||
# Round Robin - Each connection will be done via chained proxies
|
||||
# of chain_len length
|
||||
# all proxies chained in the order as they appear in the list
|
||||
# at least one proxy must be online to play in chain
|
||||
# (dead proxies are skipped).
|
||||
# the start of the current proxy chain is the proxy after the last
|
||||
# proxy in the previously invoked proxy chain.
|
||||
# if the end of the proxy chain is reached while looking for proxies
|
||||
# start at the beginning again.
|
||||
# otherwise EINTR is returned to the app
|
||||
# These semantics are not guaranteed in a multithreaded environment.
|
||||
#
|
||||
#random_chain
|
||||
#
|
||||
# Random - Each connection will be done via random proxy
|
||||
# (or proxy chain, see chain_len) from the list.
|
||||
# this option is good to test your IDS :)
|
||||
|
||||
# Make sense only if random_chain
|
||||
# Make sense only if random_chain or round_robin_chain
|
||||
#chain_len = 2
|
||||
|
||||
# Quiet mode (no output from library)
|
||||
@@ -77,9 +91,11 @@ tcp_connect_time_out 8000
|
||||
# localnet 192.168.0.0/255.255.0.0
|
||||
|
||||
# ProxyList format
|
||||
# type host port [user pass]
|
||||
# type ip port [user pass]
|
||||
# (values separated by 'tab' or 'blank')
|
||||
#
|
||||
# only numeric ipv4 addresses are valid
|
||||
#
|
||||
#
|
||||
# Examples:
|
||||
#
|
||||
|
||||
+6
-4
@@ -1,8 +1,10 @@
|
||||
#!/bin/sh
|
||||
# This script is called by proxychains to resolve DNS names
|
||||
# This is a legacy script that uses "dig" to do DNS lookups via TCP.
|
||||
# it is not actively maintained since proxychains no longer depends
|
||||
# on it. i leave it here as a bonus.
|
||||
|
||||
# DNS server used to resolve names
|
||||
DNS_SERVER=4.2.2.2
|
||||
DNS_SERVER=8.8.8.8
|
||||
|
||||
|
||||
if [ $# = 0 ] ; then
|
||||
@@ -12,5 +14,5 @@ if [ $# = 0 ] ; then
|
||||
fi
|
||||
|
||||
|
||||
export LD_PRELOAD=libproxychains.so
|
||||
dig $1 @$DNS_SERVER +tcp | awk '/A.+[0-9]+\.[0-9]+\.[0-9]/{print $5;}'
|
||||
export LD_PRELOAD=libproxychains4.so
|
||||
dig $1 @$DNS_SERVER +tcp | awk '/A.?[0-9]+\.[0-9]+\.[0-9]/{print $5;}'
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
#include "version.h"
|
||||
static const char version[] = VERSION;
|
||||
const char *proxychains_get_version(void) {
|
||||
return version;
|
||||
}
|
||||
|
||||
+20
-13
@@ -3,30 +3,30 @@
|
||||
#include <netdb.h>
|
||||
#include <netinet/in.h>
|
||||
#include <sys/socket.h>
|
||||
|
||||
|
||||
#ifndef NI_MAXHOST
|
||||
#define NI_MAXHOST 1025
|
||||
#endif
|
||||
|
||||
int main(void) {
|
||||
|
||||
static int doit(const char* host, const char* service) {
|
||||
struct addrinfo *result;
|
||||
struct addrinfo *res;
|
||||
int error;
|
||||
|
||||
|
||||
/* resolve the domain name into a list of addresses */
|
||||
error = getaddrinfo("www.example.com", NULL, NULL, &result);
|
||||
error = getaddrinfo(host, service, NULL, &result);
|
||||
if (error != 0)
|
||||
{
|
||||
{
|
||||
fprintf(stderr, "error in getaddrinfo: %s\n", gai_strerror(error));
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/* loop over all returned results and do inverse lookup */
|
||||
for (res = result; res != NULL; res = res->ai_next)
|
||||
{
|
||||
{
|
||||
char hostname[NI_MAXHOST] = "";
|
||||
|
||||
error = getnameinfo(res->ai_addr, res->ai_addrlen, hostname, NI_MAXHOST, NULL, 0, 0);
|
||||
|
||||
error = getnameinfo(res->ai_addr, res->ai_addrlen, hostname, NI_MAXHOST, NULL, 0, 0);
|
||||
if (error != 0)
|
||||
{
|
||||
fprintf(stderr, "error in getnameinfo: %s\n", gai_strerror(error));
|
||||
@@ -34,8 +34,15 @@ int main(void) {
|
||||
}
|
||||
if (*hostname != '\0')
|
||||
printf("hostname: %s\n", hostname);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
freeaddrinfo(result);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
int ret;
|
||||
ret = doit("www.example.com", NULL);
|
||||
ret = doit("www.example.com", "80");
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#include <netdb.h>
|
||||
#include <stdio.h>
|
||||
#include "../src/core.h"
|
||||
#include "../src/common.h"
|
||||
|
||||
void printhostent(struct hostent *hp) {
|
||||
char ipbuf[16];
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
#include <netdb.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include "../src/common.h"
|
||||
|
||||
/*
|
||||
int gethostent_r(
|
||||
struct hostent *ret, char *buf, size_t buflen,
|
||||
struct hostent **result, int *h_errnop);
|
||||
|
||||
Glibc2 also has reentrant versions gethostent_r(), gethostbyaddr_r(),
|
||||
gethostbyname_r() and gethostbyname2_r().
|
||||
|
||||
The caller supplies a hostent structure ret which will be filled in on success,
|
||||
and a temporary work buffer buf of size buflen.
|
||||
After the call, result will point to the result on success.
|
||||
In case of an error or if no entry is found result will be NULL.
|
||||
The functions return 0 on success and a nonzero error number on failure.
|
||||
In addition to the errors returned by the nonreentrant versions of these functions,
|
||||
if buf is too small, the functions will return ERANGE, and the call should be retried
|
||||
with a larger buffer.
|
||||
The global variable h_errno is not modified, but the address of a variable in which
|
||||
to store error numbers is passed in h_errnop.
|
||||
*/
|
||||
|
||||
void printhostent(struct hostent *hp) {
|
||||
char ipbuf[16];
|
||||
pc_stringfromipv4(hp->h_addr_list[0], ipbuf);
|
||||
printf("alias: %p, len: %d, name: %s, addrlist: %p, addrtype: %d, ip: %s\n",
|
||||
hp->h_aliases,
|
||||
hp->h_length,
|
||||
hp->h_name,
|
||||
hp->h_addr_list,
|
||||
hp->h_addrtype,
|
||||
ipbuf
|
||||
);
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
struct hostent he_buf;
|
||||
struct hostent *he_res;
|
||||
char h_buf[1024];
|
||||
int ch_errno;
|
||||
int ret;
|
||||
do {
|
||||
ret = gethostent_r(&he_buf, h_buf, sizeof(h_buf), &he_res, &ch_errno);
|
||||
printf("ret: %d, h_errno: %d\n", ret, ch_errno);
|
||||
if(ret != 0) {
|
||||
errno = ret;
|
||||
ret = -1;
|
||||
}
|
||||
if(ret == -1) {
|
||||
perror("gethostent_r");
|
||||
break;
|
||||
}
|
||||
if(he_res) {
|
||||
printhostent(he_res);
|
||||
}
|
||||
} while (he_res);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
#include <netdb.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
|
||||
#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)
|
||||
|
||||
#define ASSERT(X) { if(!(X)) printf("ASSERTION FAILED: %s @%s:%d\n", # X, __FILE__, __LINE__); }
|
||||
#define CLR() { hbuf[0] = 0; sbuf[0] = 0; }
|
||||
|
||||
int main() {
|
||||
struct sockaddr_in a = {0}, *sa = &a;
|
||||
char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
|
||||
a.sin_port = htons(80);
|
||||
memcpy( &a.sin_addr , (char[]) {127,0,0,1}, 4);
|
||||
|
||||
int ret;
|
||||
|
||||
if ((ret = getnameinfo((void*)sa, 0, hbuf, sizeof(hbuf), sbuf,
|
||||
sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV)) == 0)
|
||||
printf("host=%s, serv=%s\n", hbuf, sbuf);
|
||||
else
|
||||
printf("%s\n", gai_strerror(ret));
|
||||
|
||||
ASSERT(ret == EAI_FAMILY);
|
||||
CLR();
|
||||
|
||||
if ((ret = getnameinfo((void*)sa, sizeof a, hbuf, sizeof(hbuf), sbuf,
|
||||
sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV)) == 0)
|
||||
printf("host=%s, serv=%s\n", hbuf, sbuf);
|
||||
else
|
||||
printf("%s\n", gai_strerror(ret));
|
||||
|
||||
ASSERT(ret == EAI_FAMILY);
|
||||
CLR();
|
||||
|
||||
SOCKFAMILY(a) = AF_INET;
|
||||
|
||||
if ((ret = getnameinfo((void*)sa, sizeof a, hbuf, 1, sbuf,
|
||||
sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV)) == 0)
|
||||
printf("host=%s, serv=%s\n", hbuf, sbuf);
|
||||
else
|
||||
printf("%s\n", gai_strerror(ret));
|
||||
|
||||
ASSERT(ret == EAI_OVERFLOW);
|
||||
CLR();
|
||||
|
||||
if ((ret = getnameinfo((void*)sa, sizeof a, hbuf, 0, sbuf,
|
||||
1, NI_NUMERICHOST | NI_NUMERICSERV)) == 0)
|
||||
printf("host=%s, serv=%s\n", hbuf, sbuf);
|
||||
else
|
||||
printf("%s\n", gai_strerror(ret));
|
||||
|
||||
ASSERT(ret == EAI_OVERFLOW);
|
||||
CLR();
|
||||
|
||||
if ((ret = getnameinfo((void*)sa, sizeof(a) - 1, hbuf, 0, sbuf,
|
||||
sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV)) == 0)
|
||||
printf("host=%s, serv=%s\n", hbuf, sbuf);
|
||||
else
|
||||
printf("%s\n", gai_strerror(ret));
|
||||
|
||||
ASSERT(ret == EAI_FAMILY);
|
||||
CLR();
|
||||
|
||||
if ((ret = getnameinfo((void*)sa, sizeof a, hbuf, 0, sbuf,
|
||||
sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV)) == 0)
|
||||
printf("host=%s, serv=%s\n", hbuf, sbuf);
|
||||
else
|
||||
printf("%s\n", gai_strerror(ret));
|
||||
|
||||
ASSERT(ret == 0 && !strcmp("80", sbuf));
|
||||
CLR();
|
||||
|
||||
if ((ret = getnameinfo((void*)sa, sizeof a, hbuf, sizeof hbuf, sbuf,
|
||||
0, NI_NUMERICHOST | NI_NUMERICSERV)) == 0)
|
||||
printf("host=%s, serv=%s\n", hbuf, sbuf);
|
||||
else
|
||||
printf("%s\n", gai_strerror(ret));
|
||||
|
||||
ASSERT(ret == 0 && !strcmp("127.0.0.1",hbuf));
|
||||
CLR();
|
||||
|
||||
|
||||
if ((ret = getnameinfo((void*)sa, sizeof a, hbuf, sizeof(hbuf), sbuf,
|
||||
sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV)) == 0)
|
||||
printf("host=%s, serv=%s\n", hbuf, sbuf);
|
||||
else
|
||||
printf("%s\n", gai_strerror(ret));
|
||||
|
||||
ASSERT(ret == 0 && !strcmp("127.0.0.1",hbuf) && !strcmp("80", sbuf));
|
||||
CLR();
|
||||
|
||||
struct sockaddr_in6 b = {0}, *sb = &b;
|
||||
b.sin6_port = htons(8080);
|
||||
b.sin6_family = AF_INET6;
|
||||
|
||||
memcpy(&b.sin6_addr,"\0\0\0\0\0\0\0\0\0\0\xff\xff\xc0\xa8\1\2", 16);
|
||||
|
||||
if ((ret = getnameinfo((void*)sb, sizeof b, hbuf, sizeof(hbuf), sbuf,
|
||||
sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV)) == 0)
|
||||
printf("host=%s, serv=%s\n", hbuf, sbuf);
|
||||
else
|
||||
printf("%s\n", gai_strerror(ret));
|
||||
|
||||
ASSERT(ret == 0 && !strcmp("192.168.1.2",hbuf) && !strcmp("8080", sbuf));
|
||||
CLR();
|
||||
|
||||
b.sin6_scope_id = 3;
|
||||
memcpy(&b.sin6_addr,"\0\0\xaa\0\0\0\0\0\0\0\0\xff\xc0\xa8\1\2", 16);
|
||||
|
||||
if ((ret = getnameinfo((void*)sb, sizeof b, hbuf, sizeof(hbuf), sbuf,
|
||||
sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV | NI_NUMERICSCOPE)) == 0)
|
||||
printf("host=%s, serv=%s\n", hbuf, sbuf);
|
||||
else
|
||||
printf("%s\n", gai_strerror(ret));
|
||||
|
||||
ASSERT(ret == 0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "../src/core.h"
|
||||
#include "../src/common.h"
|
||||
#include <stdio.h>
|
||||
|
||||
void printhostent(struct hostent *hp) {
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netdb.h>
|
||||
|
||||
#ifndef MSG_FASTOPEN
|
||||
# define MSG_FASTOPEN 0x20000000
|
||||
#endif
|
||||
|
||||
void error(const char *msg)
|
||||
{
|
||||
perror(msg);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
if (argc < 4) {
|
||||
printf("Usage: %s host port method(connect or sendto)\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
const char *hostname = argv[1];
|
||||
const int portno = atoi(argv[2]);
|
||||
const char *method = argv[3];
|
||||
char request[BUFSIZ];
|
||||
sprintf(request, "GET / HTTP/1.0\r\nHost: %s\r\n\r\n", hostname);
|
||||
int sockfd, n;
|
||||
struct sockaddr_in serv_addr;
|
||||
struct hostent *server;
|
||||
|
||||
char buffer[BUFSIZ];
|
||||
sockfd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (sockfd < 0) error("ERROR opening socket");
|
||||
server = gethostbyname(hostname);
|
||||
if (server == NULL) {
|
||||
fprintf(stderr, "%s: no such host\n", hostname);
|
||||
return 1;
|
||||
}
|
||||
memset(&serv_addr, 0, sizeof(serv_addr));
|
||||
serv_addr.sin_family = AF_INET;
|
||||
memcpy(&serv_addr.sin_addr.s_addr, server->h_addr, server->h_length);
|
||||
serv_addr.sin_port = htons(portno);
|
||||
if (!strcmp(method, "connect")) {
|
||||
if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
|
||||
error("connect");
|
||||
n = send(sockfd, request, strlen(request), 0);
|
||||
} else if (!strcmp(method, "sendto")) {
|
||||
n = sendto(sockfd, request, strlen(request), MSG_FASTOPEN, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
|
||||
} else {
|
||||
printf("Unknown method %s\n", method);
|
||||
return 1;
|
||||
}
|
||||
if (n < 0)
|
||||
error("send");
|
||||
memset(buffer, 0, BUFSIZ);
|
||||
n = read(sockfd, buffer, BUFSIZ - 1);
|
||||
if (n < 0)
|
||||
error("ERROR reading from socket");
|
||||
printf("%s\n", buffer);
|
||||
close(sockfd);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
#include "../src/shm.h"
|
||||
#include <assert.h>
|
||||
|
||||
#define s(A) (sizeof(A) - 1)
|
||||
#define ss(A) (A), s(A)
|
||||
|
||||
int main() {
|
||||
char buf4096[4096];
|
||||
struct stringpool sp;
|
||||
stringpool_init(&sp);
|
||||
char *r;
|
||||
size_t pos = 0;
|
||||
r = stringpool_add(&sp, ss("AAAAA"));
|
||||
assert(r == sp.start);
|
||||
|
||||
pos += s("AAAAA");
|
||||
assert(sp.alloced == 4096);
|
||||
assert(sp.used == pos);
|
||||
|
||||
r = stringpool_add(&sp, buf4096, sizeof(buf4096));
|
||||
assert(r == sp.start + pos);
|
||||
|
||||
pos += sizeof(buf4096);
|
||||
assert(sp.alloced == 4096 * 2);
|
||||
assert(sp.used == pos);
|
||||
|
||||
r = stringpool_add(&sp, buf4096, 4096 - s("AAAAA"));
|
||||
assert(r == sp.start + pos);
|
||||
pos += 4096 - s("AAAAA");
|
||||
assert(pos == 4096 * 2);
|
||||
|
||||
assert(sp.alloced == 4096 * 2);
|
||||
assert(sp.used == pos);
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <netdb.h>
|
||||
#include <netinet/in.h>
|
||||
#include <sys/socket.h>
|
||||
|
||||
static void v4_to_v6(const struct in_addr *v4, struct in6_addr *v6) {
|
||||
memset(v6, 0, sizeof(*v6));
|
||||
v6->s6_addr[10]=0xff;
|
||||
v6->s6_addr[11]=0xff;
|
||||
memcpy(&v6->s6_addr[12], &v4->s_addr, 4);
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
struct addrinfo *result;
|
||||
struct addrinfo *res;
|
||||
const struct addrinfo hints = { .ai_family = AF_INET };
|
||||
int error, sock;
|
||||
|
||||
/* resolve the domain name into a list of addresses */
|
||||
error = getaddrinfo("www.example.com", NULL, &hints, &result);
|
||||
if (error != 0) {
|
||||
fprintf(stderr, "error in getaddrinfo: %s\n", gai_strerror(error));
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
if((sock=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
|
||||
perror("socket");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
struct sockaddr_in6 a = { .sin6_family = AF_INET6,
|
||||
.sin6_port = htons(80) };
|
||||
v4_to_v6(&((struct sockaddr_in *)result->ai_addr)->sin_addr, &a.sin6_addr);
|
||||
freeaddrinfo(result);
|
||||
|
||||
if((error = connect(sock, (struct sockaddr *)&a, sizeof(a))) == -1) {
|
||||
perror("connect");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
Executable
+64
@@ -0,0 +1,64 @@
|
||||
#!/bin/sh
|
||||
#
|
||||
# This is an actually-safe install command which installs the new
|
||||
# file atomically in the new location, rather than overwriting
|
||||
# existing files.
|
||||
#
|
||||
|
||||
usage() {
|
||||
printf "usage: %s [-D] [-l] [-m mode] src dest\n" "$0" 1>&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
mkdirp=
|
||||
symlink=
|
||||
mode=755
|
||||
|
||||
while getopts Dlm: name ; do
|
||||
case "$name" in
|
||||
D) mkdirp=yes ;;
|
||||
l) symlink=yes ;;
|
||||
m) mode=$OPTARG ;;
|
||||
?) usage ;;
|
||||
esac
|
||||
done
|
||||
shift $(($OPTIND - 1))
|
||||
|
||||
test "$#" -eq 2 || usage
|
||||
src=$1
|
||||
dst=$2
|
||||
tmp="$dst.tmp.$$"
|
||||
|
||||
case "$dst" in
|
||||
*/) printf "%s: %s ends in /\n", "$0" "$dst" 1>&2 ; exit 1 ;;
|
||||
esac
|
||||
|
||||
set -C
|
||||
set -e
|
||||
|
||||
if test "$mkdirp" ; then
|
||||
umask 022
|
||||
case "$2" in
|
||||
*/*) mkdir -p "${dst%/*}" ;;
|
||||
esac
|
||||
fi
|
||||
|
||||
trap 'rm -f "$tmp"' EXIT INT QUIT TERM HUP
|
||||
|
||||
umask 077
|
||||
|
||||
if test "$symlink" ; then
|
||||
ln -s "$1" "$tmp"
|
||||
else
|
||||
cat < "$1" > "$tmp"
|
||||
chmod "$mode" "$tmp"
|
||||
fi
|
||||
|
||||
mv -f "$tmp" "$2"
|
||||
test -d "$2" && {
|
||||
rm -f "$2/$tmp"
|
||||
printf "%s: %s is a directory\n" "$0" "$dst" 1>&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
exit 0
|
||||
@@ -0,0 +1,12 @@
|
||||
#!/bin/sh
|
||||
|
||||
if test -d .git ; then
|
||||
if type git >/dev/null 2>&1 ; then
|
||||
git describe --tags --match 'v[0-9]*' 2>/dev/null \
|
||||
| sed -e 's/^v//' -e 's/-/-git-/'
|
||||
else
|
||||
sed 's/$/-git/' < VERSION
|
||||
fi
|
||||
else
|
||||
cat VERSION
|
||||
fi
|
||||
Reference in New Issue
Block a user