2014-06-23 97 views
1

我編寫了一個端口掃描程序來了解更多關於網絡的信息
但由於某些原因,它只會爲每個掃描的端口返回port n is closedpython scapy portscanner的問題

import sys 
from scapy.all import * 

target = str(sys.argv[1]) 
beg = int(sys.argv[2]) 
ran = int(sys.argv[3]) 
ptable = [] 
print "beginning port scan on target: " + target 
for x in range(beg,ran): 
    pkt = IP(dst=target)/TCP(dport=x,flags="S") 
    ans,uans = sr(pkt,timeout=0.5) 
    if TCP in ans:  
      if ans[TCP].flags == "SA": 
       ptable.append(1) 

      else: 
       ptable.append(0) 


print "======================== port scan summary ===========================" 
print " " 

for i in range(beg,ran): 

    if ptable[i-1] == 1: 

     print "port " + str(i) + " is open" 
    else: 
     print "port " + str(i) + " is closed" 
+0

在第一個'for'循環中添加'print'來查看'ans [TCP] .flags'的結果 – furas

回答

2

這是因爲ans[TCP].flags返回的數值(而不是字符串,將另外有歧義):

>>> TCP(flags='SA').flags == 'SA' 
False 
>>> TCP(flags='SA').flags 
18 

您應該使用if ans[TCP].flags == 18:而不是== "SA",或更好,但if ans[TCP].flags & 18 == 18: