import logging
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
import sys
from scapy.all import *
import argparse
parser = argparse.ArgumentParser(add_help=False)
parser.add_argument("--remote_host")
parser.add_argument("--verbose")
parser.add_argument("--help", action='store_true')
args = parser.parse_args()
if args.help:
print("HELP...")
# Define end host and TCP port range
hostInput = args.remote_host
host = socket.gethostbyname(hostInput)
port_range = [21,22,23,25,53,80,110,135,137,138,139,443,1433,1434,8080]
# Send SYN with random Src Port for each Dst port
for dst_port in port_range:
src_port = random.randint(1025,65534)
resp = sr1(
IP(dst=host)/TCP(sport=src_port,dport=dst_port,flags="S"),timeout=1,
verbose=int(args.verbose),
)
if resp is None:
print(f"{host}:{dst_port} is filtered (silently dropped).")
elif(resp.haslayer(TCP)):
if(resp.getlayer(TCP).flags == 0x12):
# Send a gratuitous RST to close the connection
send_rst = sr(
IP(dst=host)/TCP(sport=src_port,dport=dst_port,flags='R'),
timeout=1,
verbose=int(args.verbose),
)
print(f"{host}:{dst_port} is open.")
elif (resp.getlayer(TCP).flags == 0x14):
print(f"{host}:{dst_port} is closed.")
elif(resp.haslayer(ICMP)):
if(
int(resp.getlayer(ICMP).type) == 3 and
int(resp.getlayer(ICMP).code) in [1,2,3,9,10,13]
):
print(f"{host}:{dst_port} is filtered (silently dropped).")
使用--remote_host 127.0.0.1 --verbose 1
运行正常,无错误
使用--help
运行生成:
TypeError: gethostbyname() argument 1 must be str, bytes or bytearray, not None
注:当然,只有当我包括print("Help...")
以下的所有行时,才会出现上述错误
以下是完整的回溯:
Python 3.8.0 (tags/v3.8.0:fa919fd, Oct 14 2019, 19:37:50) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>>
= RESTART: C:Users 0082563AppDataLocalProgramsPythonAT2_PythonNetworkApplications.v1.1aScenario 1logger.py
HELP...
Traceback (most recent call last):
File "C:Users 0082563AppDataLocalProgramsPythonAT2_PythonNetworkApplications.v1.1aScenario 1logger.py", line 48, in <module>
host = socket.gethostbyname(hostInput)
TypeError: gethostbyname() argument 1 must be str, bytes or bytearray, not None
>>>
正如上面评论中所建议的,我需要在之后退出程序
if args.help:
print("HELP...")
因此,在下面的脚本中,用户可以在CLI中添加--remote_host google.com --verbose 0
(或verbose 1表示完整的verbose…(
用户可以在CLI中使用--help
来打印自定义帮助部分,之后程序将退出。
#!/usr/bin/env python
import logging
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
import sys
from scapy.all import *
import argparse
parser = argparse.ArgumentParser(add_help=False)
parser.add_argument("--remote_host")
parser.add_argument("--verbose")
parser.add_argument("--help", action='store_true')
args = parser.parse_args()
if args.help:
print("Help section...")
exit()
# Define end host and TCP port range
hostInput = args.remote_host
host = socket.gethostbyname(hostInput)
port_range = [21,22,23,25,53,80,110,135,137,138,139,443,1433,1434,8080]
# Send SYN with random Src Port for each Dst port
for dst_port in port_range:
src_port = random.randint(1025,65534)
resp = sr1(
IP(dst=host)/TCP(sport=src_port,dport=dst_port,flags="S"),timeout=1,
verbose=int(args.verbose),
)
if resp is None:
print(f"{host}:{dst_port} is filtered (silently dropped).")
elif(resp.haslayer(TCP)):
if(resp.getlayer(TCP).flags == 0x12):
# Send a gratuitous RST to close the connection
send_rst = sr(
IP(dst=host)/TCP(sport=src_port,dport=dst_port,flags='R'),
timeout=1,
verbose=int(args.verbose),
)
print(f"{host}:{dst_port} is open.")
elif (resp.getlayer(TCP).flags == 0x14):
print(f"{host}:{dst_port} is closed.")
elif(resp.haslayer(ICMP)):
if(
int(resp.getlayer(ICMP).type) == 3 and
int(resp.getlayer(ICMP).code) in [1,2,3,9,10,13]
):
print(f"{host}:{dst_port} is filtered (silently dropped).")