Python 端口扫描器编辑

Python Port Scanner edit

我一直在为一个信息安全项目编辑这个端口扫描器。 该代码有效,但在第 63 行和第 34 行按顺序抛出错误 (Pycharm Edu)。 第 63 行的错误消息是:'第 63 行,在 检查主机(目标)。我看过这个,但不明白为什么这会抛出一个错误,因为它在第 34 行定义。 第 34 行的错误消息是:'NameError: global name 'conf' is not defined'。也不清楚为什么这是一个问题。 任何帮助深表感谢。 Python代码环境为Python2.7.10

#! /usr/bin/python
from logging import getLogger, ERROR # Import Logging Things
getLogger("scapy.runtime").setLevel(ERROR) # Get Rid if IPv6 Warning
import scapy
import sys
from datetime import datetime # Other stuff
from time import strftime

try:
target = raw_input("[*] Enter Target IP Address: ")
min_port = raw_input("[*] Enter Minumum Port Number: ")
max_port = raw_input("[*] Enter Maximum Port Number: ")
try:
if int(min_port) >= 0 and int(max_port) >= 0 and 
int(max_port) >= int(min_port): # Test for valid range of ports
        pass
    else: # If range didn't raise error, but didn't meet criteria
        print "\n[!] Invalid Range of Ports"
        print "[!] Exiting..."
        sys.exit(1)
except Exception: # If input range raises an error
    print "\n[!] Invalid Range of Ports"
    print "[!] Exiting..."
    sys.exit(1)
except KeyboardInterrupt: # In case the user wants to quit
print "\n[*] User Requested Shutdown..."
print "[*] Exiting..."
sys.exit(1)

ports = range(int(min_port), int(max_port)+1)
start_clock = datetime.now() # Start clock for scan time
SYNACK = 0x12 # Set flag values for later reference
RSTACK = 0x14

def checkhost(target): # Function to check if target is up
conf.verb = 0 # Hide output
try:
    ping = sr1(IP(dst = ip)/ICMP()) # Ping the target
    print "\n[*] Target is Up, Beginning Scan..."
except Exception: # If ping fails
    print "\n[!] Couldn't Resolve Target"
    print "[!] Exiting..."
    sys.exit(1)

def scanport(port): # Function to scan a given port
try:
    srcport = RandShort() # Generate Port Number
    conf.verb = 0 # Hide output
    SYNACKpkt = sr1(IP(dst = target)/TCP(sport = srcport, 
dport = port,flags = "S")) 
pktflags = SYNACKpkt.getlayer(TCP).flags
    if pktflags == SYNACK: # Cross reference Flags
        return True # If open, return true
    else:
        return False
RSTpkt = IP(dst = target)/TCP(sport = srcport, dport = port, 
flags = "R") # Construct RST packet send(RSTpkt)
except KeyboardInterrupt: # In case the user needs to quit
    RSTpkt = IP(dst = target)/TCP(sport = srcport, dport = port, 
flags = "R") send(RSTpkt)
    print "\n[*] User Requested Shutdown..."
    print "[*] Exiting..."
    sys.exit(1)

checkhost(ip) # Run checkhost() function from earlier
print "[*] Scanning Started at " + strftime("%H:%M:%S") + "!\n" 

for port in ports: # Iterate through range of ports
status = scanport(port) # Feed each port into scanning function
if status == True: # Test result
    print "Port " + str(port) + ": Open" # Print status

stop_clock = datetime.now() # Stop clock for scan time
total_time = stop_clock - start_clock # Calculate scan time
print "\n[*] Scanning Finished!" # Confirm scan stop

print "[*] Total Scan Duration: " + str(total_time) # Print scan time

问题出在你的导入语句上,它应该 是:

>>> import scapy
>>> from scapy.all import conf
>>> conf.verb = 0

甚至更好地消除将来可能出现的类似错误 只需将 scapy 导入为:

>>> from scapy.all import *
>>> conf.verb = 0

现在应该可以正常工作了。