A SOCKS5 client first negotiates a method on its proxy control connection, then sends a target request. With the no-authentication method there is no separate Auth exchange. Address Type (ATYP) selects an IP address or hostname, affecting resolution for that target; it does not itself guarantee encryption, DNS privacy or anonymity.
1. Source Code Analysis: SOCKS5 Byte Structures in C/Rust
RFC 1928 specifies wire bytes. The C layout below represents only the fixed 4-byte header. A C or Rust tokio implementation still handles partial reads, lengths, byte order and address parsing; the full variable-length request is not one universal packed-memory structure.
#pragma pack(push, 1)
struct socks5_req {
uint8_t ver; // Protocol version: 0x05
uint8_t cmd; // Command: 0x01 (CONNECT), 0x02 (BIND), 0x03 (UDP ASSOCIATE)
uint8_t rsv; // Reserved: 0x00
uint8_t atyp; // Address Type: 0x01 (IPv4), 0x03 (Domain), 0x04 (IPv6)
// Variable length destination address and 2-byte port follow
};
#pragma pack(pop)
The atyp == 0x01 form has 4 IPv4 bytes. With atyp == 0x03, a uint8_t length precedes that many hostname bytes without a NUL terminator, followed by a 2-byte network-order port. Length framing still requires bounds and encoding validation; borrowing buffers versus copying is an implementation choice.
2. Visualizing the DNS Resolution Boundary
Domain-form ATYP delegates that target name to the proxy. It may use caches, hosts files, a custom resolver or the getaddrinfo() library function; NSS and DoH/DoT are not mandated. The diagram’s local UDP 53 and encrypted remote DNS are configuration examples. IP-form requests may originate from address literals without any DNS lookup.
sequenceDiagram
participant Client OS (getaddrinfo)
participant Client App (SOCKS State Machine)
participant Local DNS (UDP 53)
participant Proxy Server
participant Upstream DNS
participant Target Server
rect rgb(255, 240, 240)
Note over Client OS (getaddrinfo),Target Server: Scenario A: ATYP=0x01 (IPv4) - Local DNS Leak
Client App (SOCKS State Machine)->>Client OS (getaddrinfo): resolve(example.com)
Client OS (getaddrinfo)->>Local DNS (UDP 53): DNS Query A Record
Local DNS (UDP 53)-->>Client OS (getaddrinfo): 93.184.216.34
Client App (SOCKS State Machine)->>Proxy Server: CONNECT [0x05 0x01 0x00 0x01 + IP + Port]
Proxy Server->>Target Server: TCP SYN to 93.184.216.34
end
rect rgb(240, 255, 240)
Note over Client OS (getaddrinfo),Target Server: Scenario B: ATYP=0x03 (Domain Name) - Secure Delegation
Client App (SOCKS State Machine)->>Proxy Server: CONNECT [0x05 0x01 0x00 0x03 + Length + example.com + Port]
Proxy Server->>Proxy Server: getaddrinfo(example.com)
Proxy Server->>Upstream DNS: Secure DNS Query (DoH/DoT)
Upstream DNS-->>Proxy Server: 93.184.216.34
Proxy Server->>Target Server: TCP SYN to 93.184.216.34
end
3. Post-Mortem: The Infamous DNS Leak
When policy requires remote resolution, a local plaintext lookup can expose a hostname. Whether iptables or tun2socks captures it depends on configuration. getaddrinfo() may use caches, NSS or a system service rather than directly reading /etc/resolv.conf and emitting UDP 53. SOCKS5 itself does not encrypt its control connection. DNS-name exposure and later TLS SNI are separate observations.
Target-name delegation and DNS-packet forwarding are separate mechanisms. ATYP=0x03 supplies the target name; DNS forwarding can use a suitable DNS proxy, SOCKS UDP ASSOCIATE or a TCP DNS path, with IPv4/IPv6 and bypass behavior handled explicitly. Intercepting UDP 53 alone does not establish system-wide leak prevention or hide every real IP from the routing table.
4. Advanced Tooling: eBPF for DNS Interception
The original XDP fragment below is a limited packet-parsing illustration. Ordinary XDP runs on interface ingress, not as a general filter for locally generated outbound UDP. Egress control needs a hook on the actual path, such as a suitable tc/cgroup egress attachment.
#include <uapi/linux/bpf.h>
#include <linux/if_ether.h>
#include <linux/ip.h>
#include <linux/udp.h>
// eBPF XDP hook to drop and log unproxied DNS queries
SEC("xdp_dns_monitor")
int drop_unproxied_dns(struct xdp_md *ctx) {
void *data_end = (void *)(long)ctx->data_end;
void *data = (void *)(long)ctx->data;
struct ethhdr *eth = data;
if ((void *)(eth + 1) > data_end) return XDP_PASS;
if (eth->h_proto == bpf_htons(ETH_P_IP)) {
struct iphdr *ip = (void *)(eth + 1);
if ((void *)(ip + 1) > data_end) return XDP_PASS;
if (ip->protocol == IPPROTO_UDP) {
struct udphdr *udp = (void *)ip + (ip->ihl * 4);
if ((void *)(udp + 1) > data_end) return XDP_PASS;
// Intercept outgoing port 53
if (udp->dest == bpf_htons(53)) {
bpf_trace_printk("DNS Leak detected! Dropping packet.\\n");
return XDP_DROP;
}
}
}
return XDP_PASS;
}
The fragment checks only untagged IPv4 UDP destination port 53. It omits IPv6, TCP DNS, other resolver channels, fragmentation and full IHL validation, and lacks the matching BPF headers, loader and attachment setup. It is not a fail-closed implementation and does not establish application compliance from ATYP=0x03. Validate each allowed resolver path for the intended policy.
5. State Machine Implementations in Rust
With tokio, follow Handshake, optional Auth and Request according to the selected method, validating response version, status and ATYP. Domain-form BND.ADDR already has a one-byte length bounded at 255; it cannot encode an arbitrary enormous length in that field. Concurrency, timeouts, total buffers and invalid replies still need bounds. A 255-byte field limit alone does not prevent all OOM or DDoS.
6. SOCKS5 DNS Boundary Evidence Matrix
To investigate SOCKS5 DNS leakage, establish where resolution happens. A successful TCP connection alone does not establish that names stayed private. The matrix below separates ATYP, resolution location, UDP behavior, and client-library configuration so readers can check local and proxy-side resolution.
| Check | Evidence to observe | Leak risk | Correct practice |
|---|---|---|---|
| ATYP field | 0x03 domain request, or 0x01/0x04 IP request. |
The application resolves locally and sends only an IP to the proxy. | Use domain-form SOCKS5 requests when remote DNS is required. |
| Local DNS traffic | tcpdump port 53, eBPF DNS probes, and resolver logs. |
The browser or library bypasses the proxy and queries recursive DNS locally. | Enable proxy DNS / remote DNS or isolate the local resolver. |
| UDP ASSOCIATE | SOCKS5 UDP mapping, destination encapsulation, and NAT state. | Only TCP is proxied while DNS or QUIC leaves through local UDP. | Explicitly disable or proxy UDP, then verify QUIC behavior. |
| Client-library configuration | curl, requests, browser, and system proxy settings. | Libraries interpret socks5 and socks5h differently. |
Use a test domain and packet capture to prove the resolution boundary. |
FAQ
Does ATYP=0x03 make the client completely anonymous?
ATYP=0x03 delegates that target name, which remains visible to the proxy. Clients can still pre-resolve or use other resolver channels. SOCKS5, encrypted tunnels, ECH, obfuscation and Tor address different observation points; combining them does not automatically establish anonymity. Evaluate the local network, proxy, exit and destination separately.
References
Related: loopback cannot drop packets, but it retransmitted 62 times — tracing a firewall rule in a layered proxy that protected the wrong address and dropped every local teardown signal.