Wednesday, September 30, 2020

Port Scanner

Task: Write a port scanner using Scapy

Language: Python

Pre requirements:  Python and scapy.

Once you successfully installed python and scapy, type scapy in your terminal.

Port Scanner

User Input for the program.

    • Destination IP Address
    • Port range
    • Protocol [TCP/UDP]

Output from the program.

    • Entered ports are open or not

Functions inside the Program

    • _PortScanUDP à Scan for UDP port
    • _PortScanTCP à Scan for TCP port
    •  _Print_Status à Print port status

Scan for TCP traffic


User input IP and port are passing to “_PortScanTCP” function.

Then check whether we are getting valid response for TCP packet. If not we assume destination is unreachable and port is closed. If we received valid TCP packet we check the response packet flag. If the destination port is open, we should receive reply packet flag with value 0x12. That is mean SYN-ACK.


If we received reply packet with flag 0x14 (RST-ACK), that is mean destination port is closed.


If the destination port is open, we have to send RST with ACK packet to terminate the communication.



Scan for UDP traffic


User input IP and port are passing to “_PortScanUDP” function.

Then check we are getting valid UDP packet or not. If it is not a valid UDP packet we assume destination port is closed. Else, if we received valid UDP packet, we check the packet layer which has UDP data stream. If it is yes, port is open.


Scan Results




Source Code

Click below like to get source code.

Port Scan Source Code

1 comment:

  1. This is an awesome post which gives almost perfect idea about UDP Port Checker.

    UDP Port Checker

    ReplyDelete