[Tutor] Having issues with a simple port scanner using scapy

Chris Verhalen vern_16 at yahoo.com
Fri Jun 25 17:34:17 EDT 2021


I’m new to python andI have no idea where I went wrong to get this port scanner using Scapy to work properly. Any help is appreciated.

import loggingfrom scapy.layers.inet import TCP, ICMP, IPlogging.getLogger("scapy.runtime").setLevel(logging.ERROR) # Disable the annoying No Route found warning !from scapy.all import *ip = "10.0.0.3"closed_ports = 0open_ports = []def is_up(ip): #""" Tests if host is up """ icmp = IP(dst=ip)/ICMP() resp = sr1(icmp, timeout=10) if resp == None: return False else: return Trueif __name__ == '__main__': conf.verb = 0 # Disable verbose in sr(), sr1() methods start_time = time.time() ports = range(1, 1024) if is_up(ip): print("Host %s is up, start scanning" % ip) for port in ports: src_port = RandShort() # Getting a random port as source port p = IP(dst=ip)/TCP(sport=src_port, dport=port, flags='S') # Forging SYN packet resp = sr1(p, timeout=2) # Sending packet if str(type(resp)) == "<type 'NoneType'>": closed += 1 elif resp.haslayer(TCP): if resp.getlayer(TCP).flags == 0x12: send_rst = sr(IP(dst=ip)/TCP(sport=src_port, dport=port, flags='AR'), timeout=1) openp.append(port) elif resp.getlayer(TCP).flags == 0x14: closed += 1 duration = time.time()-start_time print("%s Scan Completed in %fs" % (ip, duration)) if len(openp) != 0: for opp in openp: print("%d open" % pop) print("%d closed ports in %d total port scanned" % (closed, len(ports)) else: print("Host %s is Down" % ip)
line 47    else:    ^SyntaxError: invalid syntax
Process finished with exit code 1


More information about the Tutor mailing list