我编写了一个端口扫描程序,以了解有关网络的更多信息
但由于某种原因,它只为每个扫描的端口返回port n is closed
。
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 :(得分: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:
。