A Minimal Python Port Scanner Forging your own weapons to solve your own problems makes you a true penetration tester - TJ. O’Connor
01 February 2015
A tcp full connect scanner in Python
Recently I read a wonderful book called Violent Python. The author TJ. O’Connor is a Department of Defense expert in information security and a US army paratrooper. The book is easy to read, is full of useful tips and contains a short review of the most famous malwares or attacks made in the last 30 years. To be honest, the author pleasantly surprised me in showing how easily a penetration tester can build its own tools with Python.
Below is reported the full code of a tcp full scanned written in Python. It’s nice to see that it suits in about 50 lines of code .
Get the hostname and the list of ports. This can be accomplished using optparse, a powerful, extensible, and easy-to-use option parser. Use this library is extremely easy. All the work is done by three functions: OptionParser(),add_option(), and parse_args()
To each port, start a new thread to create a socket and check if the port is open using the function Scan. To use Thread is required to provide function name that be called and the full list of argument:
Create a connection using socket, a Python library that provides access to the BSD socket interface.
Running the script on my MacBook I receive the following result:
socket, Low-level networking interface