Making a Port-Scanner in Kali Linux Terminal Using Python
In computer networking, a port is a virtual point where network connections start and end. It’s like an open door of your home, If you don’t close this then anyone can Enter your home. A port scanner is a program that is searching ports in a network and tries to find which ports are virtually open and close. It is common technique hackers or cyber-security experts used to discover open doors or weak points in a network.
How Does a Port Scanner Program Work?
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
The three types of responses are below:
- Open, Accepted: When ports are open, and you can access this system by them.
- Closed, Not Listening: When this port is in use or unavailable at this time
- Filtered, Dropped, Blocked: The computer doesn’t even bother to respond.
Design a Port Scanner on Kali Linux Terminal Using Python
Step 1: Design a Program
At the very first we need to open Our Kali Linux terminal and the very first thing that we should do is find out where our python interpreter is so we command
That’s going to be important when we do this very next step.
Next step we need to open the shell because in there we are going to type our code, so we can type nano command and hit enter.
Now our main program start, we want to import sockets, sockets will tell us the ports how to operate and essentially how to transmit data.
Then we need to input IP addresses and after that, we also need a program to ask for which port we are looking for
ip = raw_input("enter the IP Address: ") port = input("Enter the Port Number: ")
Now we want to Define the socket what type of transmitted data are we are looking for to be more specific what Protocol are we asking for to see.
- socket.AF_INET this socket allows us to see TCP connection
- SOCK_STREAM this allows us to look at UDP protocol that’s essentially streaming video and audio
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
Now we are going to print how our program shows us if the specific port open or either IP is down or The port is closed.
if sock.connect_ex((ip,port)): print "Port ",port, "is closed" else: print"Port",port,"is OPEN"
Now save this program press control key + x key to save this program
Step 2: Compile and Run The program
We all Know How to Run a Script on the Kali Linux terminal
./script name . extention
Look here is the tricky part, We write Our program Perfectly, but it’s Not compiled and Run. So It’s time to give our System Permission.
Chmod 775 is a Linux command which sets permission so that User/Owner can Read, Write and execute. Group can Read Write and execute. Others can Read But can’t Write and execute.
chmod 775 ./port-scanner.py
Now we can Run Our program again, and it will compile and Run Successfully
Test Our program
So for port scanning, We need a specific IP, and we need to Tell Our Program which port we are Looking For after Input Hit Enter
Enter Your Ip Address : 192.168.43.1 Enter The Port Number : 80