【问题标题】:Python3 Portscanner does not recognize open Port 80, but Port 22 alwaysPython Port Scanner 无法识别打开的端口 80,但始终识别端口 22
【发布时间】: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 服务器在收到格式正确的命令之前不会发送任何内容。

标签: python port


【解决方案1】:

这是我的多线程端口扫描解决方案

import socket
import threading
from queue import Queue

target = "1.1.1.1"
#target = "192.168.1.130"

queue = Queue()
open_ports = []

def portscan(port):
    try:
        sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
        sock.connect((target,port))
        return True
    except:
        return False

def fill_queue(port_list):
    for port in port_list:
        queue.put(port)

def worker():
    while not queue.empty():
        port = queue.get()
        if portscan(port):
            print("Port {} is open ! ".format(port))
            open_ports.append(port)

port_list = range(1,65530)
fill_queue(port_list)

thread_list = []

for t in range(50000):  #thread count
    thread = threading.Thread(target=worker)
    thread_list.append(thread)

for thread in thread_list:
    thread.start()

for thread in thread_list:
    thread.join()

print("Open ports are :" , open_ports) 

【讨论】:

  • 谢谢,但我让它为我的局域网扫描 ip 范围,所以,你的单一 ip 解决方案不是我期望使用的,没关系,我已经找到了一个可行的解决方案,就像我上面所说的.无论如何,再次感谢你:)
猜你喜欢
  • 1970-01-01
  • 2021-03-15
  • 2018-11-26
  • 2021-06-20
  • 1970-01-01
  • 1970-01-01
  • 2015-01-02
  • 1970-01-01
相关资源
最近更新 更多