From eb1c7b98c9aac9cb4bac72c47e7fecc2dc9c5827 Mon Sep 17 00:00:00 2001 From: ShreeluSantosh Date: Thu, 3 Oct 2024 20:05:15 +0530 Subject: [PATCH] Add CLI-based Port Scanner --- .../CLI-based Port Scanner/README.md | 73 +++++++++++++++++++ .../CLI-based Port Scanner/port-scanner.py | 50 +++++++++++++ README.md | 3 + repo_structure.txt | 3 + 4 files changed, 129 insertions(+) create mode 100644 Cybersecurity_Tools/CLI-based Port Scanner/README.md create mode 100644 Cybersecurity_Tools/CLI-based Port Scanner/port-scanner.py diff --git a/Cybersecurity_Tools/CLI-based Port Scanner/README.md b/Cybersecurity_Tools/CLI-based Port Scanner/README.md new file mode 100644 index 00000000..dccee55c --- /dev/null +++ b/Cybersecurity_Tools/CLI-based Port Scanner/README.md @@ -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) + + + + diff --git a/Cybersecurity_Tools/CLI-based Port Scanner/port-scanner.py b/Cybersecurity_Tools/CLI-based Port Scanner/port-scanner.py new file mode 100644 index 00000000..17b405b9 --- /dev/null +++ b/Cybersecurity_Tools/CLI-based Port Scanner/port-scanner.py @@ -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() \ No newline at end of file diff --git a/README.md b/README.md index 8f6ceb4f..772896ed 100644 --- a/README.md +++ b/README.md @@ -112,6 +112,9 @@ The PyVerse repository is organized as follows: โ”œโ”€โ”€ CODE_OF_CONDUCT.md โ”œโ”€โ”€ CONTRIBUTING.md โ”œโ”€โ”€ Cybersecurity_Tools +โ”‚ โ””โ”€โ”€ CLI-based Port Scanner +โ”‚ โ”œโ”€โ”€ port-scanner.py +โ”‚ โ””โ”€โ”€ README.md โ”œโ”€โ”€ Data_Science โ”‚ โ””โ”€โ”€ Data-science.md โ”œโ”€โ”€ Deep_Learning diff --git a/repo_structure.txt b/repo_structure.txt index 48555456..8434929b 100644 --- a/repo_structure.txt +++ b/repo_structure.txt @@ -36,6 +36,9 @@ โ”œโ”€โ”€ CODE_OF_CONDUCT.md โ”œโ”€โ”€ CONTRIBUTING.md โ”œโ”€โ”€ Cybersecurity_Tools +โ”‚ โ””โ”€โ”€ CLI-based Port Scanner +โ”‚ โ”œโ”€โ”€ port-scanner.py +โ”‚ โ””โ”€โ”€ README.md โ”œโ”€โ”€ Data_Science โ”‚ โ””โ”€โ”€ Data-science.md โ”œโ”€โ”€ Deep_Learning