The TCP port scan

The first step for any potential hackers is almost always trying to learn which service is open on the network, so they can concentrate their effort on the attack. Of course, we need to open certain ports in order to service our customer; that is part of the risk we need to accept. But we should also close any other open port that needlessly expose a larger attack surface. We can use Scapy to do a simple TCP open port scan to scan our own host.

We can send a SYN packet and see whether the server will return with SYN-ACK:

>>> p = sr1(IP(dst="")/TCP(sport=666,dport=23,flags="S"))>>>[ IP ]### version= 4L ihl= 5L tos= 0x0 len= 40 id= 25373 flags= DF frag= 0L ttl= 62 proto= tcp chksum= 0xc59b src= ...

Get Mastering Python Networking - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.