-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrefactored_ip_scan.py
More file actions
executable file
·225 lines (176 loc) · 6.89 KB
/
Copy pathrefactored_ip_scan.py
File metadata and controls
executable file
·225 lines (176 loc) · 6.89 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/usr/bin/env python3
import re
import sys
import threading
import socket
import argparse
import ipaddress
from scapy.all import *
# from ping3 import ping
# Check for inputted host weather CIDR or IP
""" For CIDR
Run ping For ALl possible ip address then
For all Active ip address run for 65535 ports
"""
""" For IP
Ping to check if the host is active or not
IF active then check for port availability
"""
""" Last option is to do it by hostname
do a hostbyname process to get IP addr and then do the thing
"""
""" Build a Parser to parse the input through CLI
Also Build a Help module with a few examples on how to run
"""
""" The Thing To be Done
1. First get an IP addr or a list of same out of CIDR or by resolving the hostname
2. Check if that host is alive
3. If alive then check for all open ports or look for only specified ports
4. If not return the error message saying host is down
5. Make it robust by input sanitization and displaying error messages
(optional)
1. Create a json file storing all genral port number and services or find a way to determine service running
2. Differentiate between TCP or UDP ports and how to perform check on them.
"""
""" Make the output messages consize and informative
Make it in points and seperate output between two hosts
Try Printing Ouput Along with scanning to see the results live
"""
# Make it all in Socket Programming like for ICMP packets for HOSTS and PORT scanning
""" Regex to check for IP
ipv4_regex = "^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$"
cidr_block_regex = "^([0-9]{1,3}\.){3}[0-9]{1,3}(\/([0-9]|[1-2][0-9]|3[0-2]))?$"
domain_name_regex = "((?=[a-z0-9-]{1,63}\.)(xn--)?[a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,63}"
"""
""" Regex for port number
comma_sep_value_regex = "[0-9]+(,[0-9]+)+$"
range_ports_regex = "(\d+)(?:-(\d+))?$"
single_port_regex = "^(0|[1-9][0-9]{0,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$"
"""
def check_ports(ip):
print("Checking ports for IP:", ip)
# Do Something cheesy here
def active_host(hosts):
# print("Checking Host:",ip)
# active_host = []
# for host in hosts:
# print(host)
ret = ping(host, timeout=1)
if ret != None:
# active_hosts.append(host)
print(host, "is alive")
return
else:
return None
# return active_host
def parse_input():
# take input through parsing
# sanitize input and return
parser = argparse.ArgumentParser(description="Network Scanner")
parser.add_argument("host", help="Hostname/IP/CIDR ")
parser.add_argument("-p", "--port", nargs="*",
help="Specific ports to enumerate for")
inputs = parser.parse_args()
if inputs.port:
return (inputs.host, inputs.port)
else:
return (inputs.host, None)
def validate_ports(ports_input):
# Checking Ports for various types of inputs
# If no ports are specified then choose all 0-65535
# Regex's to validate input ports
comma_sep_value_regex = r"^[0-9]+(,[0-9]+)+$"
range_ports_regex = r"^(\d+)(?:-)(\d+)+$"
individual_port_regex = r"^(0|[1-9][0-9]{0,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$"
if ports_input == None:
ports = [x for x in range(1, 65536)]
# Matching ports_input for comma separated value (22,25,45,98)
elif re.fullmatch(pattern=comma_sep_value_regex, string=ports_input):
value = re.fullmatch(pattern=comma_sep_value_regex, string=ports_input)
ports = value.group().split(',')
ports = [int(port) for port in ports]
if max(ports) > 65535 or min(ports) < 0:
# Show Error
sys.exit(1)
# Matching ports_input for range seperated value ( 22-55 )
elif re.fullmatch(range_ports_regex, ports_input):
value = re.fullmatch(range_ports_regex, ports_input)
start, end = value.group().split('-')
start, end = int(start), int(end)
if start < 0 or start > 65535 or end < 0 or end > 65535:
# Show error
sys.exit(1)
ports = [port for port in range(start, end+1)]
# Matching ports_input for indvidual value ( 22 )
elif re.fullmatch(individual_port_regex, ports_input):
value = re.fullmatch(individual_port_regex, ports_input)
ports = int(value.group())
if ports < 0 or ports > 65535:
# Show Error if
sys.exit(1)
else:
# Display invalid port format
print("Invalid Port format")
return ports
def validate_host(hosts_input):
ipv4_regex = r"^\b((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.|$)){4}\b$"
cidr_block_regex = r"^([0-9]{1,3}\.){3}[0-9]{1,3}(\/([0-9]|[1-2][0-9]|3[0-2]))+$"
domain_name_regex = r"((?=[a-z0-9-]{1,63}\.)(xn--)?[a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,63}"
hosts = []
if re.fullmatch(pattern=ipv4_regex, string=hosts_input):
value = re.fullmatch(pattern=ipv4_regex, string=hosts_input)
host = value.group()
hosts.append(host)
elif re.fullmatch(pattern=cidr_block_regex, string=hosts_input):
value = re.fullmatch(pattern=cidr_block_regex, string=hosts_input)
host = value.group()
net_mask = host.split("/")[1]
if net_mask <= 32 and net_mask >= 24:
network_ip = host.split(".")
network_ip[3] = "".join(["0/", net_mask])
host = ".".join(network_ip)
elif net_mask <= 23 and net_mask >= 12:
network_ip = host.split(".")
network_ip[2] = "0"
network_ip[3] = "".join(["0/", net_mask])
host = ".".join(network_ip)
elif net_mask <= 11 and net_mask >= 2:
network_ip = host.split(".")
network_ip[1] = "0"
network_ip[2] = "0"
network_ip[3] = "".join(["0/", net_mask])
host = ".".join(network_ip)
else:
print("Invalid Netmask")
# Raise Error stating invalid Netmask
net4 = ipaddress.ip_network(host)
for x in net4.hosts():
hosts.append(str(x))
elif re.fullmatch(pattern=domain_name_regex, string=hosts_input):
value = re.fullmatch(pattern=domain_name_regex, string=hosts_input)
host = value.group()
hosts.append(host)
else:
# Display error saying host format is invalid
print("Invalid host format")
hosts = None
return hosts
# take input
hosts_input, ports_input = parse_input()
if ports_input:
ports_input = "".join(ports_input)
hosts = validate_host(hosts_input)
ports = validate_ports(ports_input)
active_hosts = []
for host in hosts:
host = socket.gethostbyname(host)
conf.L3socket = L3RawSocket
resp = sr1(IP(dst=host)/ICMP()/"Hello World!", verbose=0, timeout=1)
if resp:
active_hosts.append(host)
print(host)
print(active_hosts)
# threads = []
# threads.append(threading.Thread(target=active_host, args=(host,active_hosts)) )
# threads[-1].start()
# [x.join() for x in threads]