我对端口扫描器进行了编程,以了解有关网络的更多信息
但是由于某种原因,它只为每个扫描的端口返回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"

最佳答案

这是因为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:

关于python - python scapy portscanner的问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24357217/

10-12 18:45