Link to project: https://github.com/ByteRecon/Python
Objectives:
- Use Python to create a port scanner to show working knowledge of Python
- Use Python’s built in socket module to demonstrate knowledge of TCP/IP connections
Process:
- First I watched numerous videos and read multiple github repositories on other Python port scanners
- Settled on using socket module instead of nmap so as to show knowledge of TCP connection logic
- Future nmap scanner in the works!
- Imported sockets, ipaddress, and re modules
- Created input validation for port ranges using re.compile and set minimum and maximum port numbers
- Created a while loop for IP address validation
- User is prompted to input IP address
- Input is validated using the ipaddress module
- Created a while loop for port validation
- User is prompted for single (s) or range of ports ( r )
- If statement with while loop validation for single port
- User is prompted for port number
- Port is validated with previously mentioned minimum and maximum values
- If statement with while loop validation for range of ports
- User is prompted for a range of ports in ex. “1-100” format
- Range is validated for format and for the same minimum and maximum values
- If user did not type s or r, the while loop restarts with error message
- If statement with while loop validation for single port
- User is prompted for single (s) or range of ports ( r )
- Created a function called scan_ports which takes in an IP address and a port number
- Then using with for socket creation for TCP connection using the socket module
- Set a timeout for 1 second per port (can be lowered to decrease scanning time for large ranges)
- Then used the connect function from the socket module with the IP and port number called in the function to try to connect to that port
- Exception just passes (potential to add error messages if something else)
- Created if, elif logic to determine for single or range of ports
- If single port, run the scan_ports function on that port and output whether the port was open or closed
- If range of ports, use a for loop to run the scan_ports function on each port in range port_start to port_end
- If a port is open it is appended to an empty list called open_ports
- If a port is closed the function just passes (potential to count closed ports here, or something else for closed ports)
- For loop to print out all the ports in list open_ports
Debugging
An early version hit an infinite loop from a misplaced break that wasn’t scoped to the loop I needed to exit — tracing it also surfaced a subtler bug where invalid top-level input could fall through to later code with variables that were never assigned. Restructured the input flow around independent, explicitly-scoped loops to fix both.
Outcome
Working port scanner with IP and port validation. Demonstrated skills in Python including the use of while and for loops, function creation and implementation, if, elif, else logic, importing standard Python libraries, etc. Demonstrated active and working knowledge of TCP, IP connections with socket module. Code for the project can be found on my github at: https://github.com/ByteRecon/Python. More powerful Nmap version of scanner is currently being worked on.
