-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpsf.py
101 lines (83 loc) · 3.65 KB
/
psf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# This is another project to build a port scanner framework aimed to learn more about Python programming.
# Author: Michael Cruz Sanchez (Search Engineer @lucidworks)
# Copyright: GPLv3+
import argparse
import json
import nmap
import os
from colorama import Fore, Style
from tabulate import tabulate
def scan(target, ports, os_detection):
# Create an instance of the Nmap PortScanner
scanner = nmap.PortScanner()
# Convert the ports argument to a comma-separated string
ports_str = ','.join(str(port) for port in ports)
# Perform a TCP scan on the target and specified ports, with OS detection if enabled
scanner.scan(target, arguments=f'-p {ports_str} -sV {"-O" if os_detection else ""}')
# Get scan results
if target in scanner.all_hosts():
host = scanner[target]
# Prepare open ports data for tabulate
open_ports = []
for port in host['tcp']:
port_info = host['tcp'][port]
if port_info['state'] == 'open':
open_ports.append([port, port_info['name']])
# Prepare detected OS data
os_info = []
if os_detection and 'osmatch' in host:
for os_match in host['osmatch']:
os_info.append([os_match['name'], os_match['accuracy']])
# Display scan results
if open_ports:
print(f"Open ports found on {target}:\n")
print(tabulate(open_ports, headers=["Port", "Service"], tablefmt="fancy_grid"))
print()
else:
print(f"No open ports found on {target}.")
# Display detected OS
if os_info:
print("Detected OS:\n")
print(tabulate(os_info, headers=["OS", "Accuracy"], tablefmt="fancy_grid"))
def main():
# Check if the scanner is running as root
if os.geteuid() != 0:
print("The port scanner framework requires root privileges to perform certain operations. Please run it as root or use sudo.")
return
# Create the command-line argument parser
parser = argparse.ArgumentParser(description='Port Scanner Framework by Michael Cruz Sanchez')
# Add the JSON file argument
parser.add_argument('-f', '--file', type=str, help='JSON file containing targets, ports, and OS parameters')
# Add optional target and ports arguments
parser.add_argument('-t', '--target', type=str, help='Target IP address')
parser.add_argument('-p', '--ports', type=str, help='Ports to scan (e.g., 80,443 or 1-1024)')
# Parse the command-line arguments
args = parser.parse_args()
# Check if both JSON file and options are provided
if args.file and (args.target or args.ports):
print("Please provide either a JSON file or individual target/ports options, not both.")
return
# Read JSON file if provided
if args.file:
with open(args.file) as json_file:
data = json.load(json_file)
# Iterate over targets in the JSON data
for item in data:
target = item['target']
ports = item['ports']
os_detection = item.get('os_detection', False)
print(f"Scanning target: {target}\n")
scan(target, ports, os_detection)
print("\n")
# Otherwise, use individual target/ports options if provided
elif args.target and args.ports:
target = args.target
ports = [int(port) for port in args.ports.split(',')]
os_detection = False # Modify this as needed
print(f"Scanning target: {target}\n")
scan(target, ports, os_detection)
print("\n")
else:
print("Please provide a JSON file or individual target/ports options.")
if __name__ == "__main__":
main()