使用sys.argv的scapy端口扫描仪出现问题

2024-10-03 23:23:41 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的python类,我正在尝试制作一个端口扫描程序,当用户在PowerShell上输入IP地址、地址范围或域时,它可以扫描TCP端口

以下是我迄今为止所做的工作:

import sys

from scapy.all import*

if len(sys.argv) < 1 or "-help" in sys.argv:
    print('''
 Welcome to the port scanner

    To use this program enter a IP address, address range or domain.
    And it must also be used in CLI or Powershell
    
    How to use this program:

    Enter the destination IP, address range or domain into
    the CLI or Powershell window
    

               ''' )
    sys.exit()

# User inputs an IP address, address or domain into a CLI or Powershell 
destIP = Net(sys.argv[1])
# Ports that will be scanned: 
# 21 - FTP
# 22 - SSH
# 23 - Telnet
# 25 - SMTP
# 53 - DNS
# 80 - HTTP
# 110 - POP3
# 135 - Windows RPC
# 137 - Windows NetBIOS over TCP
# 138 - Windows NetBIOS over TCP
# 139 - Windows NetBIOS over TCP
# 443 - HTTPS
# 1433 - Microsoft SQL Server
# 1434 - Microsoft SQL Server
# 8080 - HTTP Alternative
PortRange= [21,22,23,25,53,80,110,135,137,138,139,443,1433,1434,8080]



# If user inputs one value this if statement will execute
if len(sys.argv) == 1 in sys.argv:
    for destPort in PortRange:
        # Source port is randomized using the random module from port 1025 to 65334. 
        srcport = random.randint (1025, 65534)
        ans = sr(IP(dst=destIP)/TCP(sport=srcport, dport=destPort),timeout=2,verbose=0)
        # If there is not answer from the port scanned the program will print
        # The port is filtered
        if ans == None:
            print(f"{destIPs}:{destPort} is filtered.")
        # If the port scanned give answers with a SYN ACK the program will print
        # the port is open
        elif (ans.getlayer(TCP).flags == "SA"):
            print(f"{destIPs}:{destPort} is open")
        # If the port gives an answer but resets the connection
        # the program will print the port is closed.
        elif (ans.getlayer(TCP).flags == "RA"):
             print(f"{destIPs}:{destPort} is closed")
        elif (ans.getlayer(TCP).flags == "R"):
            print(f"{destIPs}:{destPort} is closed")

#If user inputs 2 values or -verbose this elif statement will execute
if len(sys.argv) == 2 in sys.argv :
    for destPort in PortRange:
        # Variable used to when user enters verbose into the sys.argv
        variVerbose = 1
        srcport = random.randint (1025, 65534)
        # verbose is equal to 1 to show user the packets being sent.
        ans = sr(IP(dst=destIP)/TCP(sport=srcport, dport=destPort),timeout=2,verbose = variVerbose)

        if ans == None:
            print(f"{destIPs}:{destPort} is filtered.")
        elif (ans.getlayer(TCP).flags == "SA"):
            print(f"{destIPs}:{destPort} is open")
        elif (ans.getlayer(TCP).flags == "RA"):
            print(f"{destIPs}:{destPort} is closed")
        elif (ans.getlayer(TCP).flags == "R"):
            print(f"{destIPs}:{destPort} is closed")

我遇到的问题是,当我输入一个地址时,什么也不会发生,但是当我输入-help时,我的帮助对话框会工作。我错过什么了吗?还是做错了什么


Tags: orthetoinifisportsys
2条回答

这条线错了

if len(sys.argv) == 1 in sys.argv:

只需删除in sys.argv。不知道你为什么把它放在那里。你只需要if len(sys.argv) == 1:

与下面的类似行相同。这些将始终评估为False

表达式len(sys.argv) == 1是一个布尔表达式。它将始终解析为值True或值False。因此,整体表达式是if True in sys.argvif False in sys.argv。这总是会出错,因为in运算符询问左侧的值是否包含在右侧的列表(或其他集合类型)中sys.argv是字符串列表,因此这肯定是错误的

您的代码中有一些拼写错误。代码不执行任何操作,因为检查输入参数的所有if都是错误的。例如:

if len(sys.argv) == 1 in sys.argv:

应该是:

if len(sys.argv) == 2:

sys.argv始终具有您正在运行的脚本的路径,因此sys.argv的len至少为1。 另外,请检查变量名,因为destIPs不存在

我修复了所有代码(仅当只有1个ip设置为输入时的部分):

import sys
import random
from scapy.all import Net, sr, sr1
from scapy.layers.inet import IP, TCP

if len(sys.argv) < 1 or "-help" in sys.argv:
    print('''
 Welcome to the port scanner

    To use this program enter a IP address, address range or domain.
    And it must also be used in CLI or Powershell

    How to use this program:

    Enter the destination IP, address range or domain into
    the CLI or Powershell window


               ''')
    sys.exit()

# User inputs an IP address, address or domain into a CLI or Powershell
destIP = Net(sys.argv[1])
# Ports that will be scanned:
# 21 - FTP
# 22 - SSH
# 23 - Telnet
# 25 - SMTP
# 53 - DNS
# 80 - HTTP
# 110 - POP3
# 135 - Windows RPC
# 137 - Windows NetBIOS over TCP
# 138 - Windows NetBIOS over TCP
# 139 - Windows NetBIOS over TCP
# 443 - HTTPS
# 1433 - Microsoft SQL Server
# 1434 - Microsoft SQL Server
# 8080 - HTTP Alternative
PortRange = [21, 22, 23, 25, 53, 80, 110, 135, 137, 138, 139, 443, 1433, 1434, 8080]

# If user inputs one value this if statement will execute
if len(sys.argv) == 2:
    for destPort in PortRange:
        # Source port is randomized using the random module from port 1025 to 65334.
        srcport = random.randint(1025, 65534)
        tcp_connect_scan_resp = sr1(IP(dst=destIP) / TCP(sport=srcport, dport=destPort, flags="S"), timeout=10)
        # If there is not answer from the port scanned the program will print
        # # The port is filtered
        if tcp_connect_scan_resp is None:
            print(f"{destIP}:{destPort} is filtered.")
        elif tcp_connect_scan_resp.haslayer(TCP):
            # If the port scanned give answers with a SYN ACK the program will print the port is open
            if tcp_connect_scan_resp.getlayer(TCP).flags == 0x12:
                send_rst = sr(IP(dst=destIP) / TCP(sport=srcport, dport=destPort, flags="AR"), timeout=10)
                print(f"{destIP}:{destPort} is open.")
            # If the port gives an answer but resets the connection the program will print the port is closed.
            elif tcp_connect_scan_resp.getlayer(TCP).flags == 0x14:
                print(f"{destIP}:{destPort} is closed.")

相关问题 更多 >