【发布时间】: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模块来处理命令行标志。起初它可能看起来更复杂,但如果您将它用于大多数简单的情况,您将编写更少的代码(以及更少的错误代码)来处理您的命令行界面,因此您可以专注于更有趣的事情,例如端口扫描仪本身.