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



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

此行错误

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的长度至少为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.")

最新更新