A socks inbound with a username checked it on SOCKS5 TCP and nowhere else. SOCKS4 has no password, and was accepted anyway, so any client could skip the check by speaking the older protocol. The UDP relay answered datagrams from any address. And a username with no password panicked on the right username, which a release build turns into an abort. With a username set, SOCKS4 is now refused, and the relay drops datagrams from any IP address without an open, authenticated UDP ASSOCIATE connection, as RFC 1928 asks; the association ends with that connection. It is open before the reply goes out, since a client may send its first datagram the moment it reads the reply. A username without a password means an empty password, and a password without a username is a configuration error instead of silently turning authentication off. The UDP check is by IP address, since a client's sending port is not known in advance; the docs say what that leaves open.
12 KiB
Privacy, security and censorship
What a leaf configuration protects, what it leaves visible, and which choices decide the difference. Option details are in configuration.md; this page is about what they mean for the people whose traffic goes through leaf.
None of this makes any configuration safe from a determined censor. Which protocols get noticed or blocked depends on the network, on what else runs there, and on what the censor is looking for this month. Treat every recommendation here as a starting point to test on the networks your users are actually on.
What the proxy hides, and what it does not
With an encrypted protocol and a verified certificate, the network between the user and the proxy cannot read the destinations, the traffic, or the proxy credential. It can still see:
- the proxy's address and port,
- when connections start and end, and how much goes each way,
- whatever the transport shows before encryption starts: for TLS, the server name (unless ECH is used) and the handshake's own fingerprint; what runs inside it, such as the WebSocket upgrade, is not visible,
- any DNS query leaf or the application sends outside the proxy.
Anyone who can read leaf's logs, its API, or its statistics can see more; see Logs and records.
Keeping traffic behind the proxy
Most leaks are not in the protocol but around it: a lookup sent before the proxy is used, or a request sent directly when the proxy is down.
Names
- Give the proxy as an IP address, with the name on its certificate in
tls.serverName. A proxy given by name is looked up over the local network, and with the default resolver that query is plain DNS naming the proxy before any traffic reaches it. If it has to be a name, pin it indns.hosts. - Let the proxy resolve destinations. When the application hands leaf a
name -- SOCKS5 with a domain, HTTP
CONNECT, FakeDNS under TUN -- leaf passes the name to the proxy and never looks it up itself. An application that resolves the name first has already sent the query, over the user's network, wherever its own resolver is. - Leave
router.domainResolveoff. With it on, every name no rule matches is looked up before the outbound is chosen. - Route leaf's own queries through the proxy. A
dns.serversentry withoutdirect:is sent through the router; with aFINALrule to the proxy it goes through the proxy. Prefer DoH with the resolver's address given:doh:resolver.example@192.0.2.53. Without the address, leaf asks the operating system for the resolver's name first. - Avoid
systemwhere lookups must not leave the proxy: it hands every name to the operating system. - The default resolver is
1.1.1.1over plain DNS. That is fine through the proxy and visible anywhere else. Setdns.serversexplicitly.
Where unmatched traffic goes
Traffic no rule matches goes to the first outbound, or to the target of the
FINAL rule. Make that the proxy, and write the exceptions that may go direct
as explicit rules.
When the proxy is down
- A
failoverortryallwith adirectactor sends requests directly when the proxy does not answer -- or, fortryall, all the time. Leavedirectout of them where a direct request would expose the user: the request then fails. - leaf does not retry a failed connection directly on its own. The application might. Check what it does when the proxy is unreachable.
failoverhealth checks contactwww.google.comand1.1.1.1through every actor on a timer,directincluded.
TUN
autotakes over the IPv4 default route only. UnlessENABLE_IPV6=true, IPv6 traffic leaves through the original interface. On a network with IPv6, enable it, refuse IPv6 withTUN_REJECT_IPV6, or remove the host's IPv6 route.- Traffic the operating system routes elsewhere never reaches leaf: other routes, VPNs, software bound to a specific interface.
- With a local SOCKS or HTTP inbound instead, leaf sees only what each application chooses to send it.
The only reliable check is from outside: capture traffic on the user's side of the network, with IPv4 and IPv6, while using the application normally and while the proxy is stopped, and look for anything that is not the proxy.
Certificates
insecure: true, in thetlsoutbound or the TLS plugins, accepts any certificate. Anyone in the path can then pose as the proxy, read the Trojan password and every destination, and change the traffic. It exists for test fixtures. Do not ship it.serverNameis what the certificate is checked against. Set it; left out, it is the next hop's address, which is an IP address in the recommended setup.- The built-in
tlsoutbound andtls-cabi-rstrust a bundled copy of the public web's roots,tls-cabi-gothe operating system's.certificatereplaces either with exactly the CAs given, which is the way to use a private CA -- and means nothing else is trusted. - The DNS-over-HTTPS client always checks against the public roots.
Choosing a protocol
Encryption hides content, not the fact that a protocol is in use. A censor can block a proxy's address once it knows it, without looking at the traffic at all, and can look for traffic that does not resemble what it expects on its network. The questions are what the protocol looks like on the wire, and how that compares with what else the network carries.
- Shadowsocks makes every byte look random from the first packet. On a
network that sees mostly TLS and HTTP that stands out, and censors have acted
on it: the Great Firewall has been documented probing suspected Shadowsocks
servers [1] and blocking fully encrypted traffic that is not recognisably TLS
or HTTP [2]. Do not make it the default for networks with that kind of
filtering.
prefixchanges the first bytes, but identically on every connection, which is a pattern of its own. - Trojan over WebSocket over TLS, with a verified certificate, starts as an ordinary TLS connection and hides the destination and credential inside it, which is the approach of running the real protocol rather than imitating one [3]. It is the better default where ordinary TLS is allowed. It is not invisible: TLS implementations have fingerprints of their own, and leaf's (rustls, or Go's for the Go plugins) differ from browsers' [4]; and the TLS handshake a browser makes inside the tunnel can be recognised from the sizes and timing of the encrypted packets around it [5]. Users in China reported Trojan being blocked at scale in 2022 [6].
- SOCKS5 has no encryption. The destination and any credentials are in the clear. Use it only to reach something on the same machine or inside another transport.
- VLESS has no encryption of its own. Use it inside TLS or REALITY.
- obfs imitates HTTP or TLS headers without being either.
- AnyTLS and ShadowTLS are available as plugins. They were added after the audit and have not been reviewed in the same way.
The proxy's address is the other half. If many users share one address, blocking it cuts all of them off; how quickly a censor finds more addresses depends on how they are handed out, which is the operator's decision rather than leaf's [7].
Long-lived connections stand out on networks where most are short, and
periodic ones -- health checks, amux keeping connections open -- form a
pattern. Whether that matters depends on the network; it is worth measuring
against ordinary traffic from the same vantage point.
Logs and records
- At
infoleaf logs only protocols, tags and timings. Atdebugandtraceit logs source addresses, destinations, sniffed names and process names. Use those levels to diagnose, limit who can read the output, and delete it. MAX_RECENT_CONNECTIONSandHOST_STATSkeep destinations in memory for the API and the FFI. Leave them off unless the application shows them only to the user they belong to.- The API has no authentication and can shut leaf down. Keep
API_LISTENon loopback. - On macOS
LOG_CONSOLE_OUTsends console logging to the system log, where other software can read it.
Local listeners
- Inbounds listen on
127.0.0.1unless told otherwise. On any other address anyone who can reach the port can use the proxy. - The
httpinbound has no authentication. - A
socksinbound's username and password are sent in the clear, and its UDP relay tells clients apart by IP address only: anyone sharing an authenticated client's address can use its association.
Plugins
A plugin runs inside leaf with leaf's privileges and sees every byte it
carries. Ship only plugins built from source you trust, pin each with sha256,
and keep the library, its directory and the configuration writable only by
whoever deploys them. --verify-plugin loads the library to read it, which
runs its code, so use it on builds you were going to deploy anyway. See the
README.
Go plugins each carry their own Go runtime, and leaf keeps them loaded for the
life of the process. An application that ships two Go plugins should test
connections that close during the handshake or with a read or write pending.
The end-to-end suite does this for the Go TLS and Go Trojan plugins in one
chain (plugin-cleanup/all-go/*), checking that every engine instance is
destroyed and that leaf still shuts down; make e2e runs it.
A starting point
A client that sends everything, DNS included, through Trojan over WebSocket over TLS, connecting to the proxy by address and verifying it by name:
{
"log": { "level": "warn", "output": "console" },
"dns": { "servers": ["doh:resolver.example@192.0.2.53"] },
"inbounds": [{
"tag": "socks-in", "protocol": "socks",
"address": "127.0.0.1", "port": 1080, "settings": {}
}],
"outbounds": [
{
"tag": "proxy", "protocol": "chain",
"settings": { "actors": ["tls", "ws", "trojan"] }
},
{
"tag": "tls", "protocol": "tls",
"settings": { "serverName": "proxy.example", "alpn": ["http/1.1"] }
},
{
"tag": "ws", "protocol": "ws",
"settings": { "path": "/proxy", "headers": { "Host": "proxy.example" } }
},
{
"tag": "trojan", "protocol": "trojan",
"settings": { "address": "192.0.2.10", "port": 443, "password": "REPLACE_ME" }
}
],
"router": {
"domainResolve": false,
"rules": [{ "type": "FINAL", "target": "proxy" }]
}
}
- The proxy is reached at
192.0.2.10, so nothing is looked up to find it, and its certificate is checked againstproxy.example. - leaf's own lookups go to the DoH resolver through the proxy, and the resolver's address is given, so nothing is asked of the operating system.
- Everything goes to the proxy. If it is down, requests fail rather than go direct.
warnkeeps connection details out of the log.
Change the addresses, names, path and password; the shape is the point. The
TLS plugins can stand in for the tls outbound, as in
the examples.
References
- Alice et al., How China Detects and Blocks Shadowsocks, IMC 2020. https://gfw.report/publications/imc20/en/
- Wu et al., How the Great Firewall of China Detects and Blocks Fully Encrypted Traffic, USENIX Security 2023. https://www.usenix.org/conference/usenixsecurity23/presentation/wu-mingshi
- Houmansadr et al., The Parrot Is Dead: Observing Unobservable Network Communications, IEEE S&P 2013. https://www.cs.cornell.edu/~shmat/shmat_oak13parrot.pdf
- Frolov and Wustrow, The use of TLS in Censorship Circumvention, NDSS 2019. https://www.ndss-symposium.org/ndss-paper/the-use-of-tls-in-censorship-circumvention/
- Xue et al., Fingerprinting Obfuscated Proxy Traffic with Encapsulated TLS Handshakes, USENIX Security 2024. https://www.usenix.org/conference/usenixsecurity24/presentation/xue-fingerprinting
- Large scale blocking of TLS-based censorship circumvention tools in China, GFW Report, 2022. https://gfw.report/blog/blocking_of_tls_based_circumvention_tools/en/
- Bocovich et al., Snowflake, a censorship circumvention system using temporary WebRTC proxies, USENIX Security 2024. https://www.usenix.org/conference/usenixsecurity24/presentation/bocovich