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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.