Port scanning and traceroute with scapy
In the same way we do port-scanning with tools like nmap, we can also execute a simple port scanner that tells us if a specific host and ports, are open, closed or filtered with scapy.
Port scanning with scapy
In the following example, we define the analyze_port()
method, which provides the host
, port
, and verbose_level
parameters. This method is responsible for sending a TCP packet and waiting for its response. When processing the response, the objective is to check within the TCP layer if the received flag corresponds to a port in an open, closed, or filtered state. You can find the following code in the scapy_port_scan.py
file inside the scapy
's port_scanning
folder:
import sys
from scapy.all import *
import logging
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
def analyze_port(host, port, verbose_level):
print("[+] Scanning port %s" % port)
packet = IP(dst=host)/TCP(dport=port,flags...