forked from davidbombal/red-python-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nmap_port_scanner_ip_obj.py
81 lines (72 loc) · 3.99 KB
/
nmap_port_scanner_ip_obj.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
#!/usr/bin/env python3
#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 import the ipaddress module. We want to use the ipaddress.ip_address(address)
# method to see if we can instantiate a valid ip address to test.
import ipaddress
# We need to create regular expressions to ensure that the input is correctly formatted.
import re
# 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****************************************************************")
# 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 we enter an invalid ip address the try except block will go to the except block and say you entered an invalid ip address.
try:
ip_address_obj = ipaddress.ip_address(ip_add_entered)
# The following line will only execute if the ip is valid.
print("You entered a valid ip address.")
break
except:
print("You entered an invalid ip address")
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: ")
# We pass the port numbers in by removing extra spaces that people sometimes enter. So if you enter 80 - 90 instead of 80-90 the program will still work.
port_range_valid = port_range_pattern.search(port_range.replace(" ",""))
if port_range_valid:
# We're extracting the low end of the port scanner range the user want to scan.
port_min = int(port_range_valid.group(1))
# We're extracting the upper end of the port scanner range the user want to scan.
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}.")