Port-scanning with scapy

In the following example, we see that we have defined a analyze_port() function that has as parameters the host and port to analyze.

You can find the following code in the port_scan_scapy.py file:

from scapy.all import sr1, IP, TCP

OPEN_PORTS = []

def analyze_port(host, port):
"""
Function that determines the status of a port: Open / closed
:param host: target
:param port: port to test
:type port: int
"""

print "[ii] Scanning port %s" % port
res = sr1(IP(dst=host)/TCP(dport=port), verbose=False, timeout=0.2)
if res is not None and TCP in res:
if res[TCP].flags == 18:
OPEN_PORTS.append(port)
print "Port %s open" % port

def main():
for x in xrange(0, 80):
analyze_port("domain", x)
print "[*] Open ports:"
for x in OPEN_PORTS:
print " - %s/TCP" % x
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.131.10