Security incident? Suspected breach? 09 71 18 27 69csirt@synacktiv.com

Analyzing a SOCKS connection close bug in chisel and revsocks

Written by Louis-Marie Rauline - 25/11/2026 - in Qualification - Download

Chisel and revsocks are among the tools we regularly use at Synacktiv. This is what motivated us to build an automated test suite to verify the reliability of these tools. In this post, we will show how a bug in the implementation of the SOCKS protocol was discovered in a library used by two different projects, and how it was fixed.

Looking to improve your skills? Discover our trainings sessions! Learn more.

Context

Tools tested

The tools tested are chisel, revsocks and SSH. SSH is known for connecting to remote machines securely, but it can also open a SOCKS tunnel (client-side -D 1080 or server-side -R 1080). Chisel (v1.11.8) and revsocks (v2.9) are two reverse tunneling tools intended for pentesting, to bypass firewalls.

The SOCKS5 protocol

SOCKS5 is a network protocol supported by many tools to establish a TCP or UDP connection to a remote host, through a proxy server. Despite the simplicity of the protocol, the implementation must handle the connection lifecycle properly: transfer, closing, propagation of errors.

In CONNECT mode, the behavior can be summarized as follows:

  • the client sends a CONNECT request (address and port of the target), after an exchange on the authentication methods;
  • the server connects to the target and then relays data in both directions, with no encapsulation beyond TCP or UDP.

TCP diagram

 

 

With TCP, each peer signals to the other that it has nothing left to send (FIN). The connection is fully terminated once both FINs have been exchanged and acknowledged (ACK).

The SOCKS5 protocol (RFC 1928) does not specify how the relay must react to a partial close of one of the connections. The propagation of such a close is left to each implementation.

 

 

Network architecture

The three tools are deployed in a setup of four machines and two isolated networks. The router, with an interface on each network, only lets through connections originating from the LAN side.

Test environment

 

  • a server machine on the WAN network: the chisel/revsocks server, which exposes the SOCKS5 proxy, this is where the netcat, siege, curl and iperf clients run;
  • a client machine on the LAN network: the chisel/revsocks client, which connects to the server on the other side of the router;
  • a services machine on the LAN network: the target services, a netcat listener serving a file and an HTTP server.

This configuration reproduces a scenario encountered in penetration testing, where a client is allowed to connect to a server on the WAN network. The *reverse dynamic forwarding* feature included in OpenSSH (ssh -R 1080 remote) then makes it possible to reach the LAN from the WAN.

Observation

The automated test suite contains the following SOCKS tests:

  • curl and netcat for the download and upload of files;
  • siege against an HTTP server to generate a large number of short connections;
  • iperf to generate a lot of bandwidth and be able to measure it properly;
  • nmap to connect to a large number of different ports.

The netcat test, initially, only verified the content of the transfer: after the transfer, it killed the two netcat processes (the listener and the client going through the proxy) without waiting for them to terminate on their own. A hang therefore remained invisible. It was by adding a wait for a natural termination that a test turned red: on download with chisel and revsocks, the processes never terminated. The goal is therefore to understand why this test does not pass.

Minimal reproduction

To identify the cause more precisely, the goal is to reduce the failing test to a minimal, reproducible and isolated case: a server that pushes the data once and then closes, and a client that never sends anything. Once the bug was observed on the four-machine setup, we reproduced it on a single machine, to isolate the cause. It relies on OpenBSD netcat (nc.openbsd) rather than the traditional netcat (nc.traditional): the options used, -N which closes the write half of the connection at the EOF of standard input and the SOCKS proxy (-X/-x), are only available in this implementation.

Setting up revsocks/chisel

Server

$ revsocks -listen 127.0.0.1:3333 -socks 127.0.0.1:1080 -pass test
2026/10/22 10:22:26 Starting to listen for clients
2026/10/22 10:22:26 Will start listening for clients on 127.0.0.1:1080 and agents on 127.0.0.1:3333 (TLS: true)
2026/10/22 10:22:26 No TLS certificate. Generated random one.
2026/10/22 10:22:36 [127.0.0.1:48014] Got a connection from 127.0.0.1:48014:
2026/10/22 10:22:37 [127.0.0.1:48014] Got Client from 127.0.0.1:48014
2026/10/22 10:22:37 [127.0.0.1:48014] Handshake recognized. Waiting for clients on 127.0.0.1:1080

or

$ chisel server --port 4444 --socks5 --reverse
2026/10/22 10:19:54 server: Reverse tunnelling enabled
2026/10/22 10:19:54 server: Fingerprint t0T9wbYTeH7sVo6DwMjI0bUgQiuDFfrsVxeLwaR+xfc=
2026/10/22 10:19:54 server: Listening on http://0.0.0.0:4444
2026/10/22 10:20:17 server: session#1: Open (user=- addr=127.0.0.1:38928 remotes=R:127.0.0.1:1080:socks)
2026/10/22 10:20:17 server: session#1: tun: proxy#R:127.0.0.1:1080=>socks: Listening

Client

$ revsocks -connect 127.0.0.1:3333 -pass test
2026/10/22 10:22:36 Connecting to the far end. Try 1 of 3
2026/10/22 10:22:36 Connecting to far end
2026/10/22 10:22:36 Starting client

or

$ chisel client 127.0.0.1:4444 R:127.0.0.1:1080:socks
2026/10/22 10:20:17 client: Connecting to ws://127.0.0.1:4444
2026/10/22 10:20:17 client: Connected (Latency 684.801µs)

Netcat processes

$ echo "test" | nc.openbsd -Nlp 10000
  • -lp 10000: listens on port 10000
  • -N: closes the write half of the socket as soon as standard input reaches end of stream (EOF)

The server therefore sends test to any client that connects, then closes the write half of the connection: it sends a FIN but remains able to read until the client closes on its side.

$ nc.openbsd -d -X 5 -x 127.0.0.1:1080 127.0.0.1 10000
test
  • -d: do not attempt to read from standard input. The client now terminates only based on the closing of the network connection.
  • -X 5 -x 127.0.0.1:1080: SOCKS version (5) and address of the SOCKS server

The client asks the SOCKS proxy to connect to the netcat server serving the file. It receives test, but the connection hangs indefinitely.

Control case: curl

With curl, the same scenario passes: the client first sends a GET to the HTTP server, receives the file and closes its connection to the server.

Two possible theories for the bug, based on the two differences between the curl case and the netcat case:

  • the netcat client sends no data, whereas curl sends its GET request;
  • the close comes from the target in the netcat case, whereas in the curl case it is the client that closes after receiving the response.

We rule out the first theory by making the netcat client send data: the bug is still present. So what makes the difference is not the volume of data sent by the client, but the fact that it never closes its connection (it never sends a FIN) while the target, for its part, has closed. The curl case passes precisely because both sides eventually close. However, as we saw in the protocol section, the propagation of the FIN is not specified by the SOCKS protocol: it is the behavior of the relay that we now suspect.

Scope of the bug

  • the netcat server does send its FIN, but it never reaches the netcat client: the close is lost in the relay;
  • the client therefore never receives the end of the connection: it remains open, it can still send data even though the target has requested the close;
  • the behavior is identical with chisel and revsocks; with an SSH SOCKS tunnel, in contrast, the close is properly propagated and the test passes.
Capture Wireshark montrant le bug

 

The FIN sent by the netcat server (packet 15) is properly acknowledged by the relay (packet 18), but the relay sends no FIN to the SOCKS client; the capture ends there, the client connection still open.

Capture Wireshark avec un relai SSH

 

With the SSH SOCKS tunnel, the close is propagated: the relay sends its FIN to the client (packet 17) shortly after the target's FIN (packet 15). The netcat client, which does not read standard input (the -d option), terminates immediately and sends back its FIN (packet 19); the relay closes the connection to the target (packet 21) and the capture ends with a full close of both connections.

To confirm this, `netstat` returns a connection waiting to be closed:

$ netstat -planet
Proto Recv-Q Send-Q Local Address           Foreign Address         State       User       Inode      PID/Program name\
...
tcp        0      0 127.0.0.1:57084         127.0.0.1:10000         CLOSE_WAIT  1000       39383      11169/../revsocks/r
...

The TCP connection opened by the revsocks client (pid 11169) to the netcat server (port 10000) remains stuck in CLOSE_WAIT indefinitely: the FIN sent by netcat is properly received by the kernel, which is what switched the state, but revsocks never propagates it to the SOCKS client, which therefore never acknowledges the close. Why?

Cause

The main hypothesis concerns go-socks5, the SOCKS library common to both tools.

go-socks5/socks5.go#L120

// ServeConn is used to serve a single connection.
func (s *Server) ServeConn(conn net.Conn) error {
	defer conn.Close()
	bufConn := bufio.NewReader(conn)

Both tools use the Server.ServeConn method and pass it a net.Conn object. For both tools this corresponds to their tunnel.

go-socks5/request.go#L170

	// Attempt to connect
	dial := s.config.Dial
	if dial == nil {
		dial = func(ctx context.Context, net_, addr string) (net.Conn, error) {
			return net.Dial(net_, addr)
		}
	}
	target, err := dial(ctx, "tcp", req.realDestAddr.Address())
	...
	defer target.Close()

The connection to the SOCKS destination address is established; in the case of our minimal test this would be 127.0.0.1:10000. The target object is therefore a real TCP connection (*net.TCPConn): in addition to net.Conn, it implements CloseWrite, which closes the write half of the connection. We are sure of this because neither of the two tools passes config.Dial.

go-socks5/request.go#L200

	// Start proxying
	errCh := make(chan error, 2)
	go proxy(target, req.bufConn, errCh)
	go proxy(conn, target, errCh)

	// Wait
	for i := 0; i < 2; i++ {
		e := <-errCh
		if e != nil {
			// return from this function closes target (and conn).
			return e
		}
	}
	return nil

Two goroutines are launched to pass data from one connection to the other. The function waits for the result of both before returning (or the first error). Thanks to defer conn.Close() and defer target.Close(), the connections are closed regardless of the outcome.

go-socks5/request.go#L352

type closeWriter interface {
	CloseWrite() error
}

// proxy is used to suffle data from src to destination, and sends errors
// down a dedicated channel
func proxy(dst io.Writer, src io.Reader, errCh chan error) {
	_, err := io.Copy(dst, src)
	if tcpConn, ok := dst.(closeWriter); ok {
		tcpConn.CloseWrite()
	}
	errCh <- err
}

The goroutine copies data from src to dst. It is blocked on io.Copy until its source returns end of stream (EOF). The fact that nothing more comes from src is propagated by half-closing the destination (CloseWrite()), but only if dst implements it; otherwise the type assertion silently fails and the close is discarded, without error or warning. If the propagation succeeds, the other end reacts by closing its connection, and the second goroutine in turn unblocks.

So the problem is not go-socks5, but the object the tools pass to it: the tunnel stream does not implement CloseWrite, and its absence is not reported anywhere. Yet the bug only manifests if it is the target that closes its connection. Everything therefore depends on who sends the FIN:

  • the target (the netcat server) sends its FIN: it is the proxy(conn, target) goroutine that reaches the EOF and attempts to half-close conn, the tunnel stream. The assertion fails, the FIN does not cross the tunnel, the client therefore never closes, and the second goroutine waits for its EOF indefinitely: the function never returns, the defer never run, and the connection to the target remains in CLOSE_WAIT;
  • the client (netcat) sends its FIN: it is the proxy(target, req.bufConn) goroutine that reaches the EOF, and it is target, the real TCP connection (which implements CloseWrite), that is closed for writing. The FIN is properly propagated to the target: no hang.

Fixes

Both fixes target the same goal: make CloseWrite available on the tunnel stream, so that go-socks5 can propagate the close there.

  • revsocks: replace hashicorp/yamux with libp2p/yamux, whose v1 is a direct drop-in replacement and implements CloseWrite;
  • chisel: fixed by the maintainer (claude) in version 1.12.0, more precisely in this commit, by adding the CloseWrite method that allows closing half-closed connections.

Once the fix is applied, the test serves as a non-regression test: the connection terminates properly following the close on the netcat server side.

Conclusion

The chisel project was not very active and a pull request fixing the bug was already open but had not been taken into account. The maintainer, having resumed a steady pace, took inspiration from the PR to fix the bug.
For revsocks, we did not upstream the fix because it relies on the v1 of libp2p/yamux (2020); a port to v5 would require rewriting the interfaces.
For its part, the suite keeps what it has learned: the natural termination of the processes is now checked, with a timeout to prevent the test from never finishing.

This bug hunt highlights the importance of a good test suite, indispensable in qualification contexts during the Q&A phase. The recent trend toward agentic development also requires having the most stable tools possible to guarantee the autonomy of the agents.