Skip to content

Commit d221765

Browse files
committed
Add ssh -L / ssh.connect_remote() workaround when AllowTcpForwarding is disabled
Use a netcat process on the remote to connect to the specified host:port and tunnel the traffic using normal `ssh.process` I/O. This was inspired by the "Circumventing Disabled SSH Port-Forwarding with a Multiplexer" article by @guysv in the Paged Out! zine no. 5. It allows to use `gdb.debug(arg, ssh=ssh)` to debug processes on pwn.college.
1 parent 3eb690b commit d221765

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

pwnlib/tubes/ssh.py

+16
Original file line numberDiff line numberDiff line change
@@ -445,10 +445,25 @@ def __init__(self, parent, host, port, *a, **kw):
445445
self.rhost = host
446446
self.rport = port
447447

448+
import paramiko.ssh_exception
448449
msg = 'Connecting to %s:%d via SSH to %s' % (self.rhost, self.rport, self.host)
449450
with self.waitfor(msg) as h:
450451
try:
451452
self.sock = parent.transport.open_channel('direct-tcpip', (host, port), ('127.0.0.1', 0))
453+
except paramiko.ssh_exception.ChannelException as e:
454+
# Workaround AllowTcpForwarding no in sshd_config
455+
if e.args != (1, 'Administratively prohibited'):
456+
self.exception(str(e))
457+
458+
self.debug('Failed to open channel, trying to connect to remote port manually using netcat.')
459+
if parent.which('nc'):
460+
ncat = 'nc'
461+
elif parent.which('ncat'):
462+
ncat = 'ncat'
463+
else:
464+
self.exception('Could not find ncat or nc on remote. Cannot connect to remote port.')
465+
self.tunnel = parent.process([ncat, host, str(port)])
466+
self.sock = self.tunnel.sock
452467
except Exception as e:
453468
self.exception(str(e))
454469
raise
@@ -949,6 +964,7 @@ def process(self, argv=None, executable=None, tty=True, cwd=None, env=None, igno
949964
self.upload_data(script, tmpfile)
950965
return tmpfile
951966

967+
executable = executable or argv[0]
952968
if self.isEnabledFor(logging.DEBUG):
953969
execve_repr = "execve(%r, %s, %s)" % (executable,
954970
argv,

0 commit comments

Comments
 (0)