Python端口扫描程序编辑

时间:2017-03-18 00:11:30

标签: python scapy port-scanning

我一直在为信息安全项目编辑此端口扫描程序。 该代码可以工作,但按顺序抛出第63和34行的错误(Pycharm Edu)。 第63行的错误消息是:'第63行,in checkhost(目标)。我已经看过这个并且无法理解为什么这会特别引发错误,因为它在第34行定义。 第34行的错误消息是:'NameError:全局名称'conf'未定义'。目前尚不清楚为什么这也是一个问题。 任何帮助深表感谢。 Python代码环境是Python 2.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

1 个答案:

答案 0 :(得分:2)

问题在于你的import语句,它应该 是:

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

甚至更好地在将来摆脱可能的类似错误 只需输入scapy:

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

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