Reputation: 11
I programmed a port scanner to learn more about networking
but for some reason it only returns port n is closed
for every port scanned.
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"
Upvotes: 1
Views: 214
Reputation: 6237
This is because ans[TCP].flags
returns the numerical value (rather than a string that would moreover be ambiguous):
>>> TCP(flags='SA').flags == 'SA'
False
>>> TCP(flags='SA').flags
18
You should use if ans[TCP].flags == 18:
instead of == "SA"
, or better yet, if ans[TCP].flags & 18 == 18:
.
Upvotes: 2