-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #56 from ShreeluSantosh/shreelusantoshbranch
Add CLI-based Port Scanner
- Loading branch information
Showing
4 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
## **CLI Based Port Scanner** | ||
|
||
### **Disclaimer** | ||
|
||
This code is purely meant for learning purposes, and not meant to be used with malicious intent. | ||
|
||
### 🎯 **Goal** | ||
|
||
Python Project - This CLI tool allows the user to scan specified ports of a host. | ||
|
||
Modules Used: | ||
1. optparse - to enable command-line arguments in the terminal and their parsing | ||
2. socket - to interact with the host and their ports | ||
|
||
|
||
### 🧾 **Description** | ||
|
||
This CLI tool allows the user to scan specified ports of a host, and can be run in a terminal. The user can enter the host's IP address, and the port numbers (separated by comma - do not include any whitespace between commas and numbers). The tool scans ports one by one and returns a summary of findings for each port. | ||
|
||
### 🧮 **What I had done!** | ||
|
||
1. Imported required libraries. | ||
2. Set up the arguments for the tool using optparse (-H/--Host and -p/--port). | ||
3. Read the user input, and parse them accordingly. | ||
4. Stored the host IP in a variable, and port numbers in a list. | ||
5. Wrote function for sending a message to a port and checking its response. | ||
6. Wrote a function to iterate through the list of port numbers, and initiate the above scan function for each port. | ||
|
||
### 📢 **Conclusion** | ||
|
||
On using the help option, we get the following output: | ||
``` | ||
Usage: port-scanner.py [options] | ||
Options: | ||
-h, --help show this help message and exit | ||
-H TGHOST, --Host=TGHOST | ||
specify target host | ||
-p TGPORT, --port=TGPORT | ||
specify target port[s] separated by comma | ||
``` | ||
|
||
After entering a valid IP address for `certifiedhacker.com` and 3 ports - namely 21, 22, 80, the following output is displayed: | ||
``` | ||
[+] Scan Results for: box5331.bluehost.com | ||
Scanning port 21 | ||
[+] 21/tcp open | ||
[+] 220---------- Welcome to Pure-FTPd [privsep] [TLS] ---------- | ||
220-You are user number 5 of 150 allo | ||
Scanning port 22 | ||
[+] 22/tcp open | ||
[+] SSH-2.0-OpenSSH_7.4 | ||
Protocol mismatch. | ||
Scanning port 80 | ||
[+] 80/tcp open | ||
[+] HTTP/1.1 400 Bad Request | ||
Date: Sat, 14 Sep 2024 06:07:12 GMT | ||
Server: Apache | ||
Content-Length: 347 | ||
``` | ||
|
||
### ✒️ **Your Signature** | ||
|
||
`Shreelu Santosh` | ||
[GitHub Profile](https://github.com/ShreeluSantosh) | ||
|
||
|
||
|
||
|
50 changes: 50 additions & 0 deletions
50
Cybersecurity_Tools/CLI-based Port Scanner/port-scanner.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#import libraries | ||
import optparse | ||
from socket import * | ||
|
||
#function to connect to host and start scanning the specific port | ||
def connScan(tgHost, tgPort): | ||
try: | ||
connSkt = socket(AF_INET, SOCK_STREAM) | ||
connSkt.connect((tgHost, tgPort)) | ||
connSkt.send(b'HelloPython\r\n') | ||
results = connSkt.recv(100) | ||
print(f'[+] {tgPort}/tcp open') | ||
print(f'[+] {results.decode()}') | ||
connSkt.close() | ||
except: | ||
print(f'[-] {tgPort}/tcp closed') | ||
|
||
#function to resolve the host and start port scan | ||
def portScan(tgHost, tgPorts): | ||
try: | ||
tgIP = gethostbyname(tgHost) | ||
except: | ||
print(f"[-] Cannot resolve '{tgHost}': Unknown host") | ||
return | ||
try: | ||
tgName = gethostbyaddr(tgIP) | ||
print(f"\n[+] Scan Results for: {tgName[0]}") | ||
except: | ||
print(f"\n[+] Scan Results for: {tgIP}") | ||
setdefaulttimeout(1) | ||
for tgPort in tgPorts: | ||
print(f"\nScanning port {tgPort}") | ||
connScan(tgHost, tgPort) | ||
|
||
#main function | ||
def main(): | ||
parser = optparse.OptionParser() | ||
parser.add_option('-H', "--Host", dest='tgHost', type='string', help='specify target host') | ||
parser.add_option('-p', "--port", dest='tgPort', type='string', help='specify target port[s] separated by comma') | ||
(options, args) = parser.parse_args() | ||
tgHost = options.tgHost | ||
tgPorts = str(options.tgPort).split(",") | ||
if (tgHost == None) | (tgPorts[0] == None): | ||
print("[-] You must specify a target host and port[s]") | ||
exit(0) | ||
tgPorts = [int(port) for port in tgPorts] | ||
portScan(tgHost, tgPorts) | ||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters