SkillPilotAI

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

ToolJobInstall
proxychains4App's TCP connects -> SOCKS4/5, HTTP proxiesapt install proxychains4 · apk add proxychains-ng · brew install proxychains-ng
stunnelPlain 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

strict_chain: SOCKS5 then HTTP
  1. 1.proxychains4 curl on 192.168.195.20: LD_PRELOAD hooks connect()

  2. 2.Connection goes first to SOCKS5 (ssh -D) 192.168.195.30:1080

  3. 3.Then through the HTTP proxy (tinyproxy) 192.168.195.30:8888

  4. 4.Finally reaches web.lab 192.168.195.10:80

  5. 5.web.lab sees the proxy IP 192.168.195.30, not the client

Terminal
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 $U
Output
strict_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 / optionMeaning
proxychains4 prog argsRun prog through the chain
-f file.confSpecific config file
-qQuiet: no chain lines
PROXYCHAINS_CONF_FILE=fConfig via environment
nmap -sT -Pn ...Only TCP connect scans work through it
sudo proxychains4Needed for suid apps (e.g. ssh)

175.3 proxychains4: chain modes and failures

ModeBehaviourLab result
strict_chainAll proxies, in order; one down = whole chain fails1081 down: timeout, curl exit 7
dynamic_chainIn order, dead proxies skipped1081 timeout, then 1080 -> 8888 -> OK
random_chainRandom proxies, chain_len of themchain_len = 1: sometimes 8888, sometimes 1080
Output
[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

  1. PROXYCHAINS_CONF_FILE or -f file
  2. ./proxychains.conf
  3. $HOME/.proxychains/proxychains.conf
  4. /etc/proxychains.conf (Alpine: /etc/proxychains/proxychains.conf)
  5. /etc/proxychains4.conf (reference)
Terminal
cd /cwget -qO- http://web.lab/cgi-bin/ipecho "exit $?"proxychains4 -f dns.conf wget -qO- \  http://web.lab/cgi-bin/ip
Output
wget: 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

stunnel client mode -> server mode
  1. 1.curl sends plain HTTP to 127.0.0.1:8080

  2. 2.stunnel client (192.168.195.20) wraps it in TLS 1.3

  3. 3.Encrypted traffic to stunnel server 192.168.195.10:443

  4. 4.Server decrypts and sends plain HTTP over loopback to 127.0.0.1:80

  5. 5.httpd answers without knowing TLS exists

Output
; 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.lab
Output
web.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)
Output
; 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

Terminal
v() { stunnel "$@" 2>&1; }v -version | sed -n '2,4p'v -version | grep -E '^(debug|sessionCacheT)'v -options | grep -c '^options ='
Output
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 / signalEffect
stunnel FILE / -fd 0Start with a config file / config from stdin
-help / -version / -sockets / -optionsOption meanings, defaults, socket table, TLS options
kill -HUP pidReload config
kill -USR1 pidReopen log (rotation)
kill -USR2 pidLog connections
Win: -install / -start / -stop / -reloadNT 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

Terminal
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.lab
Output
exit 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
OptionMeaning
verifyChain = yesVerify chain up to a root CA in CAfile/CApath
verifyPeer = yesLeaf must match a locally stored cert
checkHost / checkIP / checkEmailLeaf name match
requireCert = yesClient cert required (mutual TLS)
CRLfile / OCSP / OCSPaiaRevocation checks
verify = 0..4Obsolete: use verifyChain/verifyPeer

175.8 stunnel: TLS versions, ciphers and sessions

Output
[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 = 120
Output
Protocol 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

Output
; 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.lab
Output
LOG6[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.