I need to implement a ssh server using paramiko that only handles '-R' port forwarding requests like this:
ssh -N -T -R 40005:destination_host:22 [email protected]
So far from what i understand i'll have to implement ServerInterface.check_port_forward_request and at some point after, create a socket and listen to the specified port. Any data that comes through the Channel/Connection go to Connection/Channel respectively
class Server (paramiko.ServerInterface):
.
.
.
def check_port_forward_request(self, address, port):
'Check if the requested port forward is allowed'
...
return port
def handler(chan, port):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(('', port))
sock.listen(1)
conn, addr = s.accept()
while True:
r, w, x = select.select([conn, chan], [], [])
if conn in r:
data = conn.recv(1024)
if len(data) == 0:
break
chan.send(data)
if chan in r:
data = chan.recv(1024)
if len(data) == 0:
break
conn.send(data)
chan.close()
conn.close()
verbose('Tunnel closed from %r' % (chan.origin_addr,))
thr = threading.Thread(target=handler, args=(chan,server_port))
thr.setDaemon(True)
thr.start()
Is this the general idea behind implementing server-side paramiko ssh port forwarding? Should i start the thread inside check_port_forward_request or somewhere else?
Paramiko does not itself leverage OpenSSH-style config file directives, but it does implement a parser for the format, which users can honor themselves (and is used by higher-level libraries, such as Fabric).
Is paramiko safe to use? The python package paramiko was scanned for known vulnerabilities and missing license, and no issues were found. Thus the package was deemed as safe to use.
An SSH Transport attaches to a stream (usually a socket), negotiates an encrypted session, authenticates, and then creates stream tunnels, called channels , across the session. Multiple channels can be multiplexed across a single session (and often are, in the case of port forwardings).
Here's a working example from Paramiko's source of reverse port forwarding:
https://github.com/paramiko/paramiko/blob/master/demos/rforward.py
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With