proxychains and stunnel
proxychains4 forces the TCP connections of any program through a chain of SOCKS4/SOCKS5/HTTP proxies even when the program has no proxy support, and stunnel is a TLS wrapper that turns plain TCP into TLS (client mode) or TLS into plain TCP (server mode) without changing the application.
175.1 What it does, where it runs
| Tool | Job | Install |
|---|---|---|
| proxychains4 | App's TCP connects -> SOCKS4/5, HTTP proxies | apt install proxychains4 · apk add proxychains-ng · brew install proxychains-ng |
| stunnel | Plain TCP <-> TLS (client / server mode) | apt install stunnel4 · brew install stunnel · Windows: official installer only |
- Lab: three Alpine containers on an internal network (no internet): web.lab 192.168.195.10, client 192.168.195.20, proxy 192.168.195.30 (ssh -D SOCKS5 on 1080, tinyproxy HTTP on 8888).
175.2 proxychains4: how it works
- Step 1
1.proxychains4 curl on 192.168.195.20: LD_PRELOAD hooks connect()
- Step 2
2.Connection goes first to SOCKS5 (ssh -D) 192.168.195.30:1080
- Step 3
3.Then through the HTTP proxy (tinyproxy) 192.168.195.30:8888
- Step 4
4.Finally reaches web.lab 192.168.195.10:80
- Step 5
5.web.lab sees the proxy IP 192.168.195.30, not the client
cd /c; U=http://192.168.195.10/cgi-bin/ipgrep -v '^#' strict.confcurl -s $Uproxychains4 -f strict.conf curl -s $Uproxychains4 -q -f strict.conf curl -s $Ustrict_chaintcp_connect_time_out 3000tcp_read_time_out 8000[ProxyList]socks5 192.168.195.30 1080http 192.168.195.30 8888web.lab saw client IP: 192.168.195.20[proxychains] config file found: strict.conf[proxychains] DLL init: proxychains-ng 4.17[proxychains] Strict chain ... 192.168.195.30:1080 ... 192.168.195.30:8888 ... 192.168.195.10:80 ... OKweb.lab saw client IP: 192.168.195.30| Usage / option | Meaning |
|---|---|
| proxychains4 prog args | Run prog through the chain |
| -f file.conf | Specific config file |
| -q | Quiet: no chain lines |
| PROXYCHAINS_CONF_FILE=f | Config via environment |
| nmap -sT -Pn ... | Only TCP connect scans work through it |
| sudo proxychains4 | Needed for suid apps (e.g. ssh) |
175.3 proxychains4: chain modes and failures
| Mode | Behaviour | Lab result |
|---|---|---|
| strict_chain | All proxies, in order; one down = whole chain fails | 1081 down: timeout, curl exit 7 |
| dynamic_chain | In order, dead proxies skipped | 1081 timeout, then 1080 -> 8888 -> OK |
| random_chain | Random proxies, chain_len of them | chain_len = 1: sometimes 8888, sometimes 1080 |
[proxychains] Dynamic chain ... 192.168.195.30:1081 ... timeout[proxychains] Dynamic chain ... 192.168.195.30:1080 ... 192.168.195.30:8888 ... 192.168.195.10:80 ... OK[proxychains] Strict chain ... 192.168.195.30:1080 ... 192.168.195.30:8888 ... 192.168.195.10:81 <--deniedexit 7- timeout on a hop = that proxy is down; <--denied on the last hop = proxies are fine, the target port (81) is closed.
- A ProxyList line is: type IP port [user pass]; socks4, socks5 and http can be mixed in one chain.
175.4 Config search order and proxy_dns
- PROXYCHAINS_CONF_FILE or -f file
- ./proxychains.conf
- $HOME/.proxychains/proxychains.conf
- /etc/proxychains.conf (Alpine: /etc/proxychains/proxychains.conf)
- /etc/proxychains4.conf (reference)
cd /cwget -qO- http://web.lab/cgi-bin/ipecho "exit $?"proxychains4 -f dns.conf wget -qO- \ http://web.lab/cgi-bin/ipwget: bad address 'web.lab'exit 1[proxychains] config file found: dns.conf[proxychains] DLL init: proxychains-ng 4.17[proxychains] Strict chain ... 192.168.195.30:1080 ... web.lab:80 ... OKweb.lab saw client IP: 192.168.195.30- With proxy_dns the name is not resolved locally: it is sent to the proxy, which resolves it (no local DNS leak).
- Caveat: Alpine's curl uses c-ares (its own DNS), so proxy_dns did not work: curl exit 6, while wget worked with the same config.
- UDP, ICMP/ping and static binaries are not proxied (LD_PRELOAD only hooks dynamic programs).
175.5 stunnel: client mode and server mode
- Step 1
1.curl sends plain HTTP to 127.0.0.1:8080
- Step 2
2.stunnel client (192.168.195.20) wraps it in TLS 1.3
- Step 3
3.Encrypted traffic to stunnel server 192.168.195.10:443
- Step 4
4.Server decrypts and sends plain HTTP over loopback to 127.0.0.1:80
- Step 5
5.httpd answers without knowing TLS exists
; client mode: plain in, TLS outforeground = yesdebug = infooutput = /tmp/stunnel-client.logpid = /tmp/stunnel-client.pid [web]client = yesaccept = 127.0.0.1:8080connect = 192.168.195.10:443verifyChain = yesCAfile = /c/ca.crtcheckHost = web.labsni = web.labweb.lab saw client IP: 127.0.0.1LOG6[ui]: Service [web] (FD=9) bound to 127.0.0.1:8080LOG5[0]: Service [web] connected remote server from 192.168.195.20:41116LOG6[0]: CERT: Host name "web.lab" matched with "web.lab"LOG6[0]: TLSv1.3 ciphersuite: TLS_AES_256_GCM_SHA384 (256-bit encryption); TLS outside on 443, plain HTTP inside[https]accept = 443connect = 127.0.0.1:80cert = /lab/web.crtkey = /lab/web.key- Global options go at the top, outside any [service]: foreground = yes (no fork), debug = info, output (log file), pid (PID file).
- Server mode is the default and needs cert and key; client = yes enables client mode.
- Other globals: chroot, include (conf.d in alphabetical order), syslog, compression, fips, engine*, RND*; icon*/taskbar are Windows GUI only.
175.6 stunnel: command line and service options
v() { stunnel "$@" 2>&1; }v -version | sed -n '2,4p'v -version | grep -E '^(debug|sessionCacheT)'v -options | grep -c '^options ='stunnel 5.72 on x86_64-alpine-linux-musl platformCompiled with OpenSSL 3.3.0 9 Apr 2024Running with OpenSSL 3.3.7 7 Apr 2026debug = daemon.noticesessionCacheTimeout = 300 seconds53| Command / signal | Effect |
|---|---|
| stunnel FILE / -fd 0 | Start with a config file / config from stdin |
| -help / -version / -sockets / -options | Option meanings, defaults, socket table, TLS options |
| kill -HUP pid | Reload config |
| kill -USR1 pid | Reopen log (rotation) |
| kill -USR2 pid | Log connections |
| Win: -install / -start / -stop / -reload | NT service control |
- Service options: accept, connect (repeatable), failover = rr|prio for several backends, exec/execArgs to run a local program instead of connect, local (source IP), redirect, socket options.
175.7 stunnel: certificate verification
try() { stunnel -fd 0 2>/dev/null <<C &foreground = yesoutput = /tmp/v[bad]client = yesaccept = 127.0.0.1:8090connect = 192.168.195.10:443verifyChain = yesCAfile = /c/$1checkHost = $2Csleep 1; curl -s 127.0.0.1:8090; echo exit $?sleep 1; grep -E 'CERT|Rej' /tmp/v | cut -c21-kill %1; wait; rm /tmp/v; }try ca.crt wrong.lab; try other.crt web.labexit 56LOG4[0]: CERT: Subject checks failedLOG4[0]: Rejected by CERT at depth=0: CN=web.labexit 56LOG4[0]: CERT: Pre-verification error: unable to get local issuer certificateLOG4[0]: Rejected by CERT at depth=0: CN=web.lab| Option | Meaning |
|---|---|
| verifyChain = yes | Verify chain up to a root CA in CAfile/CApath |
| verifyPeer = yes | Leaf must match a locally stored cert |
| checkHost / checkIP / checkEmail | Leaf name match |
| requireCert = yes | Client cert required (mutual TLS) |
| CRLfile / OCSP / OCSPaia | Revocation checks |
| verify = 0..4 | Obsolete: use verifyChain/verifyPeer |
175.8 stunnel: TLS versions, ciphers and sessions
[tls12-only]accept = 8443connect = 127.0.0.1:80cert = /lab/web.crtkey = /lab/web.keysslVersion = TLSv1.2ciphers = ECDHE-ECDSA-AES128-GCM-SHA256[tls13-chacha]accept = 9443connect = 127.0.0.1:80cert = /lab/web.crtkey = /lab/web.keysslVersionMin = TLSv1.3ciphersuites = TLS_CHACHA20_POLY1305_SHA256sessionCacheTimeout = 120Protocol version: TLSv1.2Ciphersuite: ECDHE-ECDSA-AES128-GCM-SHA256tlsv1 alert protocol versionProtocol version: TLSv1.3Ciphersuite: TLS_CHACHA20_POLY1305_SHA256tlsv1 alert protocol version TLS session ticket lifetime hint: 300 (seconds) TLS session ticket lifetime hint: 120 (seconds)- ciphers = TLS 1.2 and below, ciphersuites = TLS 1.3; a version mismatch gives tlsv1 alert protocol version.
- Also: curves, options = NO_SSLv3, securityLevel (default 2), renegotiation = no (CPU DoS), sessionResume, sessiond, sni, TIMEOUTidle/connect.
175.9 stunnel: application protocols
; client mode via an HTTP proxy (CONNECT)[via-proxy]client = yesaccept = 127.0.0.1:8081connect = 192.168.195.30:8888protocol = connectprotocolHost = web.lab:443verifyChain = yesCAfile = /c/ca.crtcheckHost = web.labsni = web.labLOG6[0]: CONNECT request acceptedLOG6[0]: CERT: Host name "web.lab" matched with "web.lab"[proxy]$ tail /tmp/tinyproxy.log... Request (file descriptor 2): CONNECT web.lab:443 HTTP/1.1- protocol = smtp (server mode, accept 587) answers EHLO with STARTTLS; openssl s_client -starttls smtp upgraded and the plain stub replied 250 web.lab hello / 221 bye.
- Others: imap, pop3, ldap, nntp, pgsql, proxy (HAProxy PROXY v1), socks; protocolUsername/Password/Authentication for proxy auth.
- transparent = source lets the backend see the real client IP but needs root plus iptables and ip rule (not run in the lab).
175.10 Common mistakes
- Thinking proxychains sends all traffic: UDP, ping, static binaries and apps with their own DNS (c-ares curl) escape it.
- Using strict_chain with an unreliable proxy list: one dead proxy breaks everything; use dynamic_chain.
- stunnel client without verifyChain + CAfile + checkHost: encrypted but open to MITM.
- Editing the wrong proxychains.conf: ./ wins over ~/.proxychains and /etc.
175.11 Interview tips
- TLS for a legacy plain app: stunnel client mode, accept = local port, connect = remote TLS port, plus verifyChain, CAfile, checkHost against MITM.
- Second of three proxies down: strict_chain fails, dynamic_chain skips it and connects.
- proxychains works via LD_PRELOAD hooking connect(), so only dynamically linked TCP apps.
- stunnel server mode = TLS termination in front of a plain backend.