2017-03-18 13 views
1

정보 보안 프로젝트를 위해이 포트 스캐너를 편집했습니다. 코드는 작동하지만 63 번과 34 번 줄에 오류가 발생합니다 (Pycharm Edu). 63 행의 오류 메시지는 '행 63, 의 checkhost (대상)입니다. 나는 이걸 살펴 봤는데 왜 이것이 34 행에 정의되어있는 것처럼 오류를 던질 수 있는지 알 수 없다. 34 행의 오류 메시지는 'NameError : global name'conf 'not defined'이다. 왜 이것이 문제인지 명확하지 않습니다. 도움을 주시면 감사하겠습니다. 파이썬 코드 환경이 파이썬 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 
+2

"conf"라는 이름의 모듈 또는 해당 이름을 포함하는 모듈을 가져 오기로되어있는 것처럼 보입니다. import 문이있는 코드 조각을 모두 제거했는지 확인하십시오. 단지 야생 추측 – repzero

답변

2

, 그것은 해야한다 :

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

또는 더 나은 미래에 가능한 유사한 오류를 제거하는

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

이 지금은 잘 작동합니다 :로 는 scapy를 가져옵니다.

+0

고마워, 지금 작동, 많이 감사합니다. – screencast

+0

도움이 되니 기쁩니다 !!! – coder