forked from davidbombal/red-python-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nmap_port_scanner.py
73 lines (64 loc) · 3.35 KB
/
nmap_port_scanner.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
#Use these commands in Kali to install required software:
# sudo apt install python3-pip
# pip install python-nmap
# Import nmap so we can use it for the scan
import nmap
# We need to create regular expressions to ensure that the input is correctly formatted.
import re
# Regular Expression Pattern to recognise IPv4 addresses.
ip_add_pattern = re.compile("^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$")
# Regular Expression Pattern to extract the number of ports you want to scan.
# You have to specify <lowest_port_number>-<highest_port_number> (ex 10-100)
port_range_pattern = re.compile("([0-9]+)-([0-9]+)")
# Initialising the port numbers, will be using the variables later on.
port_min = 0
port_max = 65535
# This port scanner uses the Python nmap module.
# You'll need to install the following to get it work on Linux:
# Step 1: sudo apt install python3-pip
# Step 2: pip install python-nmap
# Basic user interface header
print(r"""______ _ _ ______ _ _
| _ \ (_) | | | ___ \ | | | |
| | | |__ ___ ___ __| | | |_/ / ___ _ __ ___ | |__ __ _| |
| | | / _` \ \ / / |/ _` | | ___ \/ _ \| '_ ` _ \| '_ \ / _` | |
| |/ / (_| |\ V /| | (_| | | |_/ / (_) | | | | | | |_) | (_| | |
|___/ \__,_| \_/ |_|\__,_| \____/ \___/|_| |_| |_|_.__/ \__,_|_|""")
print("\n****************************************************************")
print("\n* Copyright of David Bombal, 2021 *")
print("\n* https://www.davidbombal.com *")
print("\n* https://www.youtube.com/davidbombal *")
print("\n****************************************************************")
open_ports = []
# Ask user to input the ip address they want to scan.
while True:
ip_add_entered = input("\nPlease enter the ip address that you want to scan: ")
if ip_add_pattern.search(ip_add_entered):
print(f"{ip_add_entered} is a valid ip address")
break
while True:
# You can scan 0-65535 ports. This scanner is basic and doesn't use multithreading so scanning
# all the ports is not advised.
print("Please enter the range of ports you want to scan in format: <int>-<int> (ex would be 60-120)")
port_range = input("Enter port range: ")
port_range_valid = port_range_pattern.search(port_range.replace(" ",""))
if port_range_valid:
port_min = int(port_range_valid.group(1))
port_max = int(port_range_valid.group(2))
break
nm = nmap.PortScanner()
# We're looping over all of the ports in the specified range.
for port in range(port_min, port_max + 1):
try:
# The result is quite interesting to look at. You may want to inspect the dictionary it returns.
# It contains what was sent to the command line in addition to the port status we're after.
# For in nmap for port 80 and ip 10.0.0.2 you'd run: nmap -oX - -p 89 -sV 10.0.0.2
result = nm.scan(ip_add_entered, str(port))
# Uncomment following line and look at dictionary
# print(result)
# We extract the port status from the returned object
port_status = (result['scan'][ip_add_entered]['tcp'][port]['state'])
print(f"Port {port} is {port_status}")
except:
# We cannot scan some ports and this ensures the program doesn't crash when we try to scan them.
print(f"Cannot scan port {port}.")