【发布时间】:2021-03-10 11:57:29
【问题描述】:
我正在编写一个小型 Portscanner,但它只识别开放的端口 22,而不是端口 80,尽管它是开放的并且有一个网络服务器。有谁知道如何解决这个问题? 代码如下:
"""
PortScanner v0.01
"""
import socket
import threading
import sys
import time
def CheckIfOpen(ip,port):
target = (ip,int(port))
try:
s = socket.create_connection(target)
t = s.recv(1024)
if t:
open('open','a').write(ip+":"+str(port)+"\n")
print("Port: "+str(port)+" open on IP: "+ip+"!\n")
except:
pass
if sys.argv[4]:
threads = int(sys.argv[4])
else:
threads = 100
start = sys.argv[1].split(".")
end = sys.argv[2].split(".")
if int(end[3]) != 255:
end[3] = int(end[3])+1
else:
if int(end[2]) != 255:
end[2] = int(end[2])+1
end[3] = 0
else:
if int(end[1]) != 255:
end[1] = int(end[1])+1
end[2] = 0
end[3] = 0
else:
if int(end[0]) != 255:
end[0] = int(end[0])+1
end[1] = 0
end[2] = 0
end[3] = 0
end = str(end[0])+"."+str(end[1])+"."+str(end[2])+"."+str(end[3])
current = str(start[0])+"."+str(start[1])+"."+str(start[2])+"."+str(start[3])
try:
ports = sys.argv[3].split(",")
except:
ports = sys.argv[3]
while(current != end):
for port in ports:
if threading.active_count() <= int(threads):
T = threading.Thread(target=CheckIfOpen,args=(current,int(port),))
T.start()
else:
time.sleep(0.2)
T = threading.Thread(target=CheckIfOpen,args=(current,int(port),))
T.start()
progress = current.split(".")
if int(progress[3]) != 255:
progress[3] = int(progress[3])+1
else:
if int(progress[2]) != 255:
progress[2] = int(progress[2])+1
progress[3] = 0
else:
if int(end[1]) != 255:
progress[1] = int(progress[1])+1
progress[2] = 0
progress[3] = 0
else:
if int(progress[0]) != 255:
progress[0] = int(progress[0])+1
progress[1] = 0
progress[2] = 0
progress[3] = 0
current = str(progress[0])+"."+str(progress[1])+"."+str(progress[2])+"."+str(progress[3])
T.join()
print("Scan finished!\n")
exit()
我强制连接的方式是错误的吗?我只是因为无聊才编造的,但也许有人可以帮助我。 :)
我用python3写了这个,也用cython编译了它,但是除了端口22之外,没有办法获得其他开放端口,无论是在lan上还是在wan上。 :( 此外,它现在挂断了,同时完成端口扫描:/
edit:\ 通过不从套接字检索消息来修复它,工作代码如下:
"""
PortScanner v0.01
"""
import socket
import threading
import sys
import time
def CheckIfOpen(ip,port):
target = (ip,int(port))
try:
socket.create_connection(target,1.5)
open('open','a').write(ip+":"+str(port)+"\n")
print("Port: "+str(port)+" open on IP: "+ip+"!\n")
except:
print("Port: "+str(port)+" closed on IP: "+ip+"!\n")
if sys.argv[4]:
threads = int(sys.argv[4])
else:
threads = 100
start = sys.argv[1].split(".")
end = sys.argv[2].split(".")
if int(end[3]) != 255:
end[3] = int(end[3])+1
else:
if int(end[2]) != 255:
end[2] = int(end[2])+1
end[3] = 0
else:
if int(end[1]) != 255:
end[1] = int(end[1])+1
end[2] = 0
end[3] = 0
else:
if int(end[0]) != 255:
end[0] = int(end[0])+1
end[1] = 0
end[2] = 0
end[3] = 0
end = str(end[0])+"."+str(end[1])+"."+str(end[2])+"."+str(end[3])
current = str(start[0])+"."+str(start[1])+"."+str(start[2])+"."+str(start[3])
try:
ports = sys.argv[3].split(",")
except:
ports = sys.argv[3]
while(current != end):
for port in ports:
if threading.active_count() <= int(threads):
T = threading.Thread(target=CheckIfOpen,args=(current,int(port),))
T.start()
else:
time.sleep(0.2)
T = threading.Thread(target=CheckIfOpen,args=(current,int(port),))
T.start()
progress = current.split(".")
if int(progress[3]) != 255:
progress[3] = int(progress[3])+1
else:
if int(progress[2]) != 255:
progress[2] = int(progress[2])+1
progress[3] = 0
else:
if int(end[1]) != 255:
progress[1] = int(progress[1])+1
progress[2] = 0
progress[3] = 0
else:
if int(progress[0]) != 255:
progress[0] = int(progress[0])+1
progress[1] = 0
progress[2] = 0
progress[3] = 0
current = str(progress[0])+"."+str(progress[1])+"."+str(progress[2])+"."+str(progress[3])
T.join()
print("Scan finished!\n")
exit()
【问题讨论】:
-
对,Web 服务器在收到格式正确的命令之前不会发送任何内容。