Exemple #1
0
 def send_tree(self, chan: paramiko.Channel, path: str) -> typing.Tuple[list, list]:
     """
     Request a "send tree" request to the client given a channel and path
     :param chan: Channel to send the request for
     :param file_path: Path of the request
     :return: Dirs and files in the tree
     """
     chan.send(self.SEND_TREE)
     logging.debug("TREE:tree request sent")
     size = len(path)
     size = str(size)
     size += '.' * int((64 - len(size)))
     chan.send(str(size))
     logging.debug("TREE:tree size sent")
     chan.send(path)
     logging.debug("TREE:tree path sent")
     logging.debug("TREE:waiting for size")
     size = chan.recv(64)
     logging.debug("TREE:got size")
     size = size.decode('utf-8')
     size = size.replace('.', '')
     logging.debug("TREE:wating for tree")
     raw_tree = chan.recv(int(size))
     logging.debug("TREE:got tree")
     dirs, files = pickle.loads(raw_tree)
     return dirs, files
Exemple #2
0
def make_interactive(
    channel: paramiko.Channel
) -> typing.Tuple[int, typing.BinaryIO, typing.BinaryIO]:
    """
    Manages an interactive command
    Takes in a paramiko.Channel shared by stdin, stdout, stderr of a currently running command
    The current interpreter stdin is duplicated and written to the command's stdin
    The command's stdout and stderr are written to the interpreter's stdout and stderr
    and also buffered in a ByteStream for later reading
    Returns (exit status, Stdout buffer, Stderr bufer)
    """
    infd = sys.stdin.fileno()
    channelfd = channel.fileno()
    poll = select.poll()
    poll.register(
        infd, select.POLLIN + select.POLLPRI + select.POLLERR + select.POLLHUP)
    poll.register(
        channelfd,
        select.POLLIN + select.POLLPRI + select.POLLERR + select.POLLHUP)
    stdout = io.BytesIO()
    stderr = io.BytesIO()
    while not channel.exit_status_ready():
        for fd, event in poll.poll(0.5):
            if fd == infd and event & (select.POLLIN + select.POLLPRI):
                # Text available on python stdin
                channel.send(os.read(infd, 4096))
        if channel.recv_ready():
            content = channel.recv(4096)
            sys.stdout.write(content.decode())
            sys.stdout.flush()
            stdout.write(content)
        if channel.recv_stderr_ready():
            content = channel.recv_stderr(4096)
            sys.stderr.write(content.decode())
            sys.stderr.flush()
            stderr.write(content)
    if channel.recv_ready():
        content = channel.recv(4096)
        sys.stdout.write(content.decode())
        sys.stdout.flush()
        stdout.write(content)
    if channel.recv_stderr_ready():
        content = channel.recv_stderr(4096)
        sys.stderr.write(content.decode())
        sys.stderr.flush()
        stderr.write(content)
    stdout.seek(0, 0)
    stderr.seek(0, 0)
    return channel.recv_exit_status(), stdout, stderr
def read_out(chan: paramiko.Channel, wait_time=1) -> str:
    now = time.time()
    out = ''
    while True:
        while chan.recv_ready():
            data = chan.recv(256).decode('utf-8')
            out = out + data
            sys.stdout.write(data)
        #     and time.time() - now > 3
        while not chan.recv_ready() and time.time() - now > wait_time:
            return out
Exemple #4
0
    def _initiate_channel(self, channel: paramiko.Channel, email: str):
        """
        Initiates channel with given channel object and email
        :param channel: SSH Channel object
        :param email: user's email
        """
        data = channel.recv(5)
        data = data.decode("utf-8")
        logging.debug("Data recived: {data}".format(data=data))

        if data == self.READY:
            size = channel.recv(64)
            size = size.decode('utf-8')
            size = size.replace('.', '')
            name = channel.recv(int(size))
            setattr(channel,"anylink_name",name.decode('utf-8'))
            channel.send(self.CONFIRM_READY)
            self.channels[channel] = email
        else:
            channel.close()
Exemple #5
0
    def send_file(self, chan: paramiko.Channel, file_path: str) -> bool:
        """
        Request a "send file" request to the client given a channel and file path
        :param chan: Channel to send the request for
        :param file_path: Path of the file of the request
        :return: File transfer succeeded/faild - True/False
        """
        chan.send(self.SEND_FILE)
        size = str(len(file_path))
        size += '.' * int((64 - len(size)))
        chan.send(str(size))
        chan.send(file_path)
        size = chan.recv(64)
        size = size.decode('utf-8')
        size = size.replace('.', '')
        msg = chan.recv(int(size))
        msg = msg.decode('utf-8')
        logging.debug("Message recived: {msg}".format(msg=msg))
        if msg == "finishfile":
            return True

        return False
Exemple #6
0
def right_forwarder(channel: Channel, local_port: int):
    selector = selectors.DefaultSelector()

    def from_localhost():
        data = sock.recv(1024)
        if len(data) == 0:
            return data
        channel.send(data)
        return data

    def to_localhost():
        data = sock.recv(1024)
        if len(data) == 0:
            return data
        channel.send(data)
        return data


    selector.register(socket, selectors.EVENT_READ, from_localhost)
    selector.register(channel, selectors.EVENT_READ, to_localhost)

    # connect to localhost via sockets
    sock = socket.socket()
    try:
        sock.connect((LOCALHOST, local_port))
    except Exception as e:
        logger.error(f'Forwarding request to {local_port} failed: e')
        return

    # read data from channel or socket and send to opposite site
    while True:
        r, _, _ = select.select([sock, channel], [], [])
        if sock in r:
            data = sock.recv(1024)
            if len(data) == 0:
                break
            channel.send(data)
        if channel in r:
            data = channel.recv(1024)
            if len(data) == 0:
                break
            sock.send(data)
    channel.close()
    sock.close()
    logger.info(f'Tunnel closed from {channel.origin_addr}')