Skip to content

Commit

Permalink
Merge pull request #56 from ShreeluSantosh/shreelusantoshbranch
Browse files Browse the repository at this point in the history
Add CLI-based Port Scanner
  • Loading branch information
UTSAVS26 authored Oct 3, 2024
2 parents fd3336b + eb1c7b9 commit 15dbc10
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 0 deletions.
73 changes: 73 additions & 0 deletions Cybersecurity_Tools/CLI-based Port Scanner/README.md
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 Cybersecurity_Tools/CLI-based Port Scanner/port-scanner.py
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()
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,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
Expand Down
3 changes: 3 additions & 0 deletions repo_structure.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,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
Expand Down

0 comments on commit 15dbc10

Please sign in to comment.