[英]Connection refused on python sockets (port is open, I can connect with nc)
So for personal reasons, I want to connect to a socket I create via telnetlib, I can connect to it from netcat but when I try from python it refuses the connection. 因此出于个人原因,我想连接到通过telnetlib创建的套接字,我可以从netcat连接到它,但是当我从python尝试时,它拒绝连接。
tn.write(b"/usr/bin/nc -l -p 3333 -e /bin/sh\n")
print("netcat listening on 3333 on target, trying to connect")
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((tn.host, 3333))
s.send('ls')
print(s.recv(1024))
s.close()
If I can connect to it via netcat (by putting for example and infinite loop after tn.write()) 如果我可以通过netcat连接到它(例如在tn.write()之后放置无限循环)
You have a classic race condition : 您的比赛条件很经典:
The packet containing the command to start an nc
is sent at only nanoseconds before the connection request (TCP SYN packet) of s.connect
. 包含启动
nc
命令的数据包仅在s.connect
的连接请求(TCP SYN数据包)之前的十亿分之一秒发送。 It can even happen that the SYN
gets to the remote host before the command to start nc
. 甚至可能在命令
nc
启动之前SYN
到达远程主机。
You need to add proper synchronization. 您需要添加适当的同步。 From the code you have shown, there is really no need to use two channels in the first place, so why not send the
ls
via the existing telnet channel to the remote host? 根据您显示的代码,实际上首先不需要使用两个通道,那么为什么不通过现有的telnet通道将
ls
发送到远程主机呢?
If you absolutely must use a second channel, try one of these options: 如果您绝对必须使用第二个频道,请尝试以下选项之一:
import time
and time.sleep(5)
just before calling s.connect
. s.connect
之前添加import time
和s.connect
time.sleep(5)
。 That way, the connection attempt will be deferred by 5 seconds. Also note that your code has three different security vulnerabilities: 另请注意,您的代码具有三个不同的安全漏洞:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.