套接字无法与 netcat 通信 bash
socket can't communicate with netcat bash
在虚拟机上,我使用了命令:nc -l -p 8221 -e /bin/bash 并创建了一个 python3 脚本:
def netcat():
print ("starting connection")
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("192.168.1.60", 8221))
while True:
user = input("what to send?: ")
s.sendall(bytes(user, "utf-8"))
time.sleep(5)
word = "bob"
data = s.recv(4096)
if data == b"":
pass
else:
data = data.decode("utf-8")
print ("Received:", repr(data))
print ("Connection closed.")
s.shutdown(socket.SHUT_WR)
s.close()
netcat()
此脚本无效。不工作我的意思是当我 运行 使用我的 python 脚本的命令时,假设 "pwd",它只是加载但从不加载 运行s。
当我 运行 nc 192.168.1.60 8221 而不是 运行ning python 脚本时,它会工作正常。有什么想法吗?
来自input()
的documentation:
The function then reads a line from input, converts it to a string
(stripping a trailing newline), and returns that.
但是 Bash 在规范模式下运行,在新行到达之前不会处理输入。这不会发生,导致 recv
永远阻塞。
在 user = input("what to send?: ")
之后添加一个 + '\n'
来修复它。
在虚拟机上,我使用了命令:nc -l -p 8221 -e /bin/bash 并创建了一个 python3 脚本:
def netcat():
print ("starting connection")
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("192.168.1.60", 8221))
while True:
user = input("what to send?: ")
s.sendall(bytes(user, "utf-8"))
time.sleep(5)
word = "bob"
data = s.recv(4096)
if data == b"":
pass
else:
data = data.decode("utf-8")
print ("Received:", repr(data))
print ("Connection closed.")
s.shutdown(socket.SHUT_WR)
s.close()
netcat()
此脚本无效。不工作我的意思是当我 运行 使用我的 python 脚本的命令时,假设 "pwd",它只是加载但从不加载 运行s。 当我 运行 nc 192.168.1.60 8221 而不是 运行ning python 脚本时,它会工作正常。有什么想法吗?
来自input()
的documentation:
The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that.
但是 Bash 在规范模式下运行,在新行到达之前不会处理输入。这不会发生,导致 recv
永远阻塞。
在 user = input("what to send?: ")
之后添加一个 + '\n'
来修复它。