【问题标题】:Having a problem with my scapy port scanner using sys.argv使用 sys.argv 我的 scapy 端口扫描程序出现问题
【发布时间】:2021-06-25 07:11:49
【问题描述】:

这是针对我的 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 时我的帮助对话框有效。我错过了什么吗?还是做错了什么?

【问题讨论】:

  • 一般来说,您应该尝试使用argparse 模块来处理命令行标志。起初它可能看起来更复杂,但如果您将它用于大多数简单的情况,您将编写更少的代码(以及更少的错误代码)来处理您的命令行界面,因此您可以专注于更有趣的事情,例如端口扫描仪本身.

标签: python scapy


【解决方案1】:

这一行是错误的

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.argv要么是if False in sys.argv。这总是错误的,因为in 运算符询问左侧的值是否包含在右侧的列表(或其他集合类型)中。 sys.argv 是一个字符串列表,所以这肯定是错误的。

【讨论】:

    【解决方案2】:

    您的代码中有一些拼写错误。代码什么也不做,因为检查输入参数的所有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.")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-10
      • 1970-01-01
      • 2010-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多