Building a port scanner in python

Building a port scanner in python

·

3 min read

Building a port scanner in python

Like I mentioned in the previous post, I will be trying to build a basic port scanner based in python. I will be using knowledge regarding python that I regained not too long ago from a course! This won't be a spectacular port scanner like Nmap but it would be something functional that beginners could build :)I made use of the library called SOCKET.Let's get into it, shall we? :)


Import the needed libraries

#!/bin/python3 import sys import socket from datetime import datetime as dt

#Helpful functions def nl(): print('\n') #prints new line when function is called

  • sys for getting arguments input by the user.
  • socket for making a successful connection to the host.
  • dateTime…..for….you know? DateTime purposes!

Define the target

#Define our target if len(sys.argv) == 2: #Checkes if there are only 2 args entered target = socket.gethostbyname(sys.argv[1]) #DNS else:
nl()
print("Invalid number of arguments entered :(")
print("Syntax --> python3 portscanner.py ") nl()

  • If the number of arguments entered matches the number of arguments we require, we let the program continue
  • Else, we alert the user that the input was invalid

Create a banner (optional)

#Add a pretty banner nl() print("~" 50) print("Scanning host ~> {}".format(target)) print("Time started: {}".format(dt.now())) print("~" 50) nl()


Create a try statement to scan ports on the target machine

try: for port in range (21,81): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #Declaring needed methods to a variable for later use socket.setdefaulttimeout(1) #Ends connection if a port does not respond in 1 second result = s.connect_ex((target, port)) #Returns an error indicator; Open port = 0 Closed port = 1

#print("Checking port {}".format(port)) if result == 0: print("<~ Port {} is open and unfiltered ~>".format(port))

#nl() s.close

  • Try’s to connect to all the ports in rage (21,81) [this can be changed to the port that you prefer]
  • Sets default timeout to 1 second to avoid delays
  • Connects to the target using the IP the user entered and the port in the for loop range

Create exceptions for the program

except KeyboardInterrupt: #If the user interupts the program, the following will occur
print("<~ Terminating Scan ~>")
sys.exit()
except socket.gaierror: #If the hostname could not be resolved, the following will occur
print("<~ !Hostname could not be resolved! ~>")
sys.exit()
except socket.gaierror: #If no successful connection could be made with the host, the following will occur
print("<~ !Couldn't connect to server! ~>")
sys.exit()

  • If the user stops the program, it will terminate
  • If the program is unable to resolve the hostname, it will terminate
  • If the program cant initiate a successful connection with the server, it will terminate

Final product

I made some changes to the look of the program and added a place holder to show how long the program ran for.

Building a port scanner in python