Introduction
Welcome, Hacker Brothers! 😎 How are you all? In today’s blog, we are going to do something exciting! I mean, today we will Build a Python port scanner ! 🕵️♂️🔥 So, how is the josh? 💥
This blog is a must-read for all cybersecurity enthusiasts who are interested in ethical hacking and network security. Port scanning is an essential technique that ethical hackers and security professionals use to detect network vulnerabilities. Along with this, banner grabbing is another powerful method that helps us identify the running services and their versions on a system.
So, without any delay, let’s dive into the world of Port Scanning and Banner Grabbing! 😎
इस ब्लॉग में हम Python का उपयोग करके एक advanced port scanner बनाएंगे, जो banner grabbing भी करेगा। Port scanning एक ज़रूरी तकनीक है, जिसका उपयोग ethical hackers और security professionals किसी भी system के open ports और active services का पता लगाने के लिए करते हैं। Banner grabbing से हम उन services के software version का भी पता कर सकते हैं, जो security vulnerabilities को पहचानने में मदद करता है।
तो चलिए, Port Scanning aur Banner Grabbing की इस दुनिया में कदम रखते हैं! 🚀

What is Port Scanning?
Port scanning is a method used to identify open ports and services running on a target machine. Open ports indicate that certain applications or services are actively listening for incoming connections. Ethical hackers and security professionals use port scanners to detect vulnerabilities and strengthen security.
Port scanning एक तरीका है जिससे किसी target machine के open ports और उन पर चल रही services का पता लगाया जाता है। Open ports इस बात का संकेत देते हैं कि कोई application या service actively incoming connections के लिए सुन रही है। Ethical hackers और security professionals इसका उपयोग vulnerabilities detect करने और security को मजबूत करने के लिए करते हैं।
Port Number | Common Service |
---|---|
21 | FTP |
22 | SSH |
25 | SMTP (Mail) |
80 | HTTP (Web) |
443 | HTTPS (Secure Web) |


Code Overview
This script performs a basic port scan to check which ports are open on a given target IP address. It also attempts to grab the service banner of open ports, which can give useful information about the services running on those ports.
Steps Breakdown:
1. Import Required Libraries

- socket : This library is used to create a connection to a remote server and interact with it through sockets.
- datetime : This is used to print the time when the scanning starts.
2. User Input for Target IP

- This line prompts the user to enter the IP address of the target system that they want to scan.
3. Define grab_banner Function

Explanation (English):
- The grab_banner function attempts to grab the service banner from the open port.
- It sends a basic HTTP request (HEAD / HTTP/1.1 ) to the open port and tries to retrieve” the banner information.
- If no banner is available, it returns “Banner not available“.
4. Port Scanning Function

Explanation (English):
- The port_scan function starts by resolving the domain name of the target to its IP address using socket.gethostbyname().
5. Port Scanning Loop

Explanation (English):
- We print the target IP and the start time of the scan.
- Then, a for loop runs through all possible port numbers (0 to 65535).
- For each port, a TCP connection (SOCK_STREAM) is attempted using sock.connect_ex. If the result is 0 , it means the port is open.
6. Banner Grabbing Attempt for Open Ports

Explanation (English):
- Once a port is identified as open, we attempt to grab the service banner using the grab_banner function.
- If successful, the banner is printed. If the banner grabbing fails, a failure message is displayed.
7. Handling Errors (Exceptions)

Explanation (English):
- If there is an error resolving the hostname, a socket.gaierror will be caught, and an error message will be displayed.
- If there’s any issue with connecting to the server,socket.error will be caught and an appropriate error message will be displayed.
8. Closing the Socket

Explanation (English):
- After checking each port, the socket connection is closed to free up resources.
9. Running the Port Scan

Explanation (English):
- This line calls the port_scan function to start scanning the provided target IP.
How to Use?
- Ensure Python is installed on your system. 2️
- Save the code as port_scanner.py . 3️
- Run the script in the terminal:

- Enter the target IP address.
- The script will scan for open ports and grab service banners! 🎯
Real-World Applications
- Penetration Testing: Detecting open ports and services.
- Vulnerability Assessment: Finding outdated software and security loopholes.
- Network Monitoring: Monitoring open ports on servers and devices.
Conclusion
This Python-based port scanner is a powerful tool for ethical hackers that helps in security testing and network analysis. 🚀
One Comment