Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

paramiko server mode port forwarding

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?

like image 744
olokki Avatar asked Nov 27 '12 07:11

olokki


People also ask

Does paramiko use OpenSSH?

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?

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.

What is paramiko transport?

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).


1 Answers

Here's a working example from Paramiko's source of reverse port forwarding:

https://github.com/paramiko/paramiko/blob/master/demos/rforward.py

like image 154
Schof Avatar answered Oct 23 '22 15:10

Schof