I am doing a college assignment right now and am writing code in python to build a "Stealth Port Scanner with Scapy and Python". This is quite difficult for me so I am hoping for help. I did 11/41 subsections, but I think I messed up somewhere. What should I do differently? Below I send the code and a picture of the tasks.
`
target = input("Adres IP: ")
Registered_Ports = range(1023)
open_ports = []
def scanport(port):
port = RandShort()
conf.verb = 0
SYNACKpkt = sr1(IP(dst=target) / TCP(sport=srcport, dport=port, flags="S", timeout=0.5))
flags = SYNACKpkt.getlayer(TCP).flags
if flags == SYNACKpkt:
return True
else:
return False
SynPkt1 = IP() / TCP()
SynPkt1.haslayer(TCP)
1
SynPkt2 = IP() / UDP()
SynPkt2.haslayer(TCP)
0
`
Your code does not handle the case where you don't receive anything in response of the synchronisation packet you send (point 10). If you don't receive an answer then
SYNACKpktwill be assignedNoneandSYNACKpkt.getlayer(TCP)will fail with an exception. So you first has to check that ifSYNACKpkt is None.Moreover, before doing
SYNACKpkt.getlayer(TCP)you must also be sure that the answer has a TCP layer (point 11). Otherwise thisSYNACKpkt.getlayer(TCP)will again fail with an exception.By including these two changes the code becomes: