-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetcat.py
executable file
·375 lines (237 loc) · 11.7 KB
/
netcat.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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#!/usr/bin/env python3
"""
NetCAT config backup, deployment and monitoring system version 5.5 - 2020, Sebastian Majewski
netcat.py (App version) - module containing global variables, exceptions and shared functions
"""
import re
import sys
import time
from typing import List, Dict, Tuple, Any, Callable, Optional, Union
import loguru # type: ignore
from os import getpid
MAIN_PROCESS_PID = getpid()
from multiprocessing import cpu_count
MAX_WORKERS = 4 # cpu_count()
POLL_FREQUENCY:int = 12
DEBUG: bool = False
SINGLE_PROCESS_MODE: bool = False
LOGGER: Any = None
VERSION: str = "5.5"
YEAR: str = "2020"
DB_TYPE: str = "MongoDB"
#DB_TYPE: str = "DynamoDB"
DBT_INFO = "netcat_info"
DBT_BACKUP = "netcat_backup"
DBT_STATUS = "netcat_status"
class CustomException(Exception):
""" Custom exception class used to raise NetCAT specific exception whenever unrecoverable error occurs """
def split_list(input_list: list, chunk_number: int) -> List[list]:
""" Split input list into list containing number of sublists """
if chunk_len := int(len(input_list) / chunk_number) + bool(len(input_list) % chunk_number):
return [input_list[_:_ + chunk_len] for _ in range(0, len(input_list), chunk_len)]
else:
return []
def encode_command(command: str) -> str:
""" Encode command name to ensure it doesnt contain any weird characters """
from binascii import hexlify
return hexlify(command.encode("utf-8")).decode("utf-8").translate(str.maketrans("1234567890", "ghijklmnop"))
def decode_command(command: str) -> str:
""" Decode command name previously encoded by 'encode_command_name' function """
from binascii import unhexlify
return str(unhexlify(command.translate(str.maketrans("ghijklmnop", "1234567890"))), "utf-8")
def decompress_command_output(command_output: str) -> str:
""" Decompress command output """
from bz2 import decompress
from base64 import b85decode
return str(decompress(b85decode(command_output)), "utf-8")
def compress_device_data(device_data: Dict[str, Any]) -> Dict[str, Any]:
""" Compress command outputs in device data structure """
from bz2 import compress
from base64 import b85encode
if not device_data:
return {}
compressed_device_data: Dict[str, Any] = {
"snapshot_timestamp": device_data.get("snapshot_timestamp"),
"device_name": device_data.get("device_name"),
"device_type": device_data.get("device_type"),
"output_formats": {},
}
for format_name, format_data in device_data.get("output_formats", {}).items():
compressed_device_data["output_formats"][format_name] = {}
for command_name, command_data in format_data.items():
compressed_device_data["output_formats"][format_name][encode_command(command_name)] = str(b85encode(compress(bytes(command_data, "utf-8"))), "utf-8")
return compressed_device_data
def decompress_device_data(compressed_device_data: Dict[str, Any]) -> Dict[str, Any]:
""" Decompress command outputs in device data structure """
from bz2 import decompress
from base64 import b85decode
if not compressed_device_data:
return {}
device_data: Dict[str, Any] = {
"snapshot_timestamp": compressed_device_data.get("snapshot_timestamp"),
"device_name": compressed_device_data.get("device_name"),
"device_type": compressed_device_data.get("device_type"),
"output_formats": {},
}
for format_name, format_data in compressed_device_data.get("output_formats",{}).items():
device_data["output_formats"][format_name] = {}
for command_name, command_data in format_data.items():
device_data["output_formats"][format_name][decode_command(command_name)] = str(decompress(b85decode(command_data)), "utf-8")
return device_data
def http_error(message: str = "", code=400) -> Tuple[str, int]:
""" Returns html formated errror message """
if code == 400:
return f"<html>\n<title>400 Bad request</title>\n<h2>400 Bad request</h2>\n<p>{message}</p>\n</html>\n", 400
if code == 500:
return f"<html>\n<title>500 Server error</title>\n<h2>5.1 Server error</h2>\n<p>{message}</p>\n</html>\n", 500
return f"<html>\n<title>{code} Unknown error</title>\n<h2>{code} Unknown error</h2>\n<p>{message}</p>\n</html>\n", code
def fn() -> str:
""" Returns name of current function. Goes deeper if current fuction name is _, __ or ___ """
for depth in range(1, 4):
name = sys._getframe(depth).f_code.co_name
if name not in {"_", "__", "___"}:
return name + "()"
return "unknown()"
def standardize_mac_address(mac_address: str) -> str:
""" Converting couple different mac address formats to standard one """
# Cisco's DHCP ID thing
if re.search(r"^(01[0-9A-Fa-f]{2}\.)([0-9A-Fa-f]{4}\.){2}[0-9A-Fa-f]{2}$", mac_address):
mac_address = mac_address[2:]
mac_address = re.sub(r"\.", r"", mac_address.upper().strip())
mac_address = ":".join(mac_address[_ : _ + 2] for _ in range(0, len(mac_address), 2))
# Couple various formats
elif re.search(r"^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$", mac_address.strip()) or re.search(
r"^([0-9A-Fa-f]{4}[.]){2}([0-9A-Fa-f]{4})$", mac_address.strip()
):
mac_address = re.sub(r":|-|\.", "", mac_address.upper().strip())
mac_address = ":".join(mac_address[_ : _ + 2] for _ in range(0, len(mac_address), 2))
else:
mac_address = "UNKNOWN"
return mac_address
def setup_logger(debug: bool = False, process_name_length: int = 9, stdout: bool = True) -> None:
""" Setting up logger """
log_level = "DEBUG" if debug else "INFO"
loguru.logger.remove(0)
if stdout:
loguru.logger.add(sys.stdout, colorize=True, level=log_level, format=f"<green>{{time:YYYY-MM-DD HH:mm:ss}}</green> <level>| {{level:7}} "
+ f"|</level> <level>{{extra[process_name]:{process_name_length}}} | {{message}}</level>")
bind_logger("MAIN_PROG")
def bind_logger(process_name: str) -> None:
""" Bind specific process name to logger """
global LOGGER
LOGGER = loguru.logger.bind(process_name=process_name)
def print_device_data(device_data: Dict[str, Any], no_indent: bool = False) -> str:
""" Print device data json format in human readable form """
from datetime import datetime
output = []
if no_indent:
indent = ""
else:
indent = " "
output.append("SECTION: INFO")
output.append("")
output.append(f"{indent}NAME: {device_data.get('device_name')}")
output.append(f"{indent}TYPE: {device_data.get('device_type', '').replace('_', ' ') or None}")
output.append(f"{indent}TIMESTAMP: {device_data.get('snapshot_timestamp')} [{datetime.utcfromtimestamp(int(device_data.get('snapshot_timestamp')))} UTC]") # type: ignore
output.append("")
output.append("")
output.append("SECTION: FORMATS")
output.append("")
for format in device_data.get("output_formats", {}):
output.append(f"{indent}FORMAT: {format.upper().replace('_', ' ')}")
output.append("")
for command in device_data.get("output_formats", {}).get(format):
output.append(f"{indent}{indent}COMMAND: '{command}'")
output.append("")
for command_line in device_data.get("output_formats", {}).get(format, {}).get(command, "").split("\n"):
output.append(f"{indent}{indent}{indent}{command_line}")
output.append("")
output.append("")
return "\n".join(output)
def get_command_output(device_data: Dict[str, Any], command: str) -> str:
""" Function returns command output for given device_data / command """
if command_output := device_data.get("output_formats", {}).get("info", {}).get(command):
return command_output
return ""
def find_regex_sl(*args: Any, **kwargs: Any) -> str:
""" Wrapper for find_regex_ml() function to easily handle cases when we only expect to pull the value(s) from single line of text """
return (find_regex_ml(*args, **kwargs) or [""])[0]
def find_regex_ml(text: str, regex: str, /, *, hint: Optional[str] = None, optional: bool = True) -> List[str]:
""" Find single or multiple values per each of the lines of text. Uses regex grouping mechanism to mark interesting values. """
if hint:
if optional and hint not in text:
return []
if not (text_lines := [_ for _ in text.split("\n") if hint in _]):
return []
else:
text_lines = text.split("\n")
cregex = re.compile(regex)
return [_.groups() if len(_.groups()) > 1 else _.group(1) for __ in text_lines if (_ := cregex.search(__.rstrip("\r")))]
def validate_ip_address(ip_address: str) -> bool:
""" Validate IP address """
from socket import inet_aton
if re.search(r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$", ip_address):
try:
inet_aton(ip_address)
except OSError:
return False
return True
return False
def validate_mac_address(mac_address: str) -> bool:
""" Validate if provided MAC address has valid format """
if not (
re.search(r"^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$", mac_address.strip())
or re.search(r"^([0-9A-Fa-f]{4}[.]){2}([0-9A-Fa-f]{4})$", mac_address.strip())
or re.search(r"^([0-9A-Fa-f]{12})$", mac_address.strip())
):
return False
return True
def validate_search_depth(input_string: str) -> bool:
""" Validate search depth string to prevent any malicious regex action """
if not re.match(r"^[0-9]{1,3}$", input_string):
return False
return True
def validate_http_input(input_string: str) -> bool:
""" Validate input string to prevent any malicious regex action """
if not re.match(r"^[a-zA-Z0-9_\-]{1,25}$", input_string):
return False
return True
def validate_timestamp(input_string: str) -> bool:
""" Validate timestamp string to prevent any malicious regex action """
if not re.match(r"^\d{10}$", input_string):
return False
return True
def convert_mac_to_cisco_format(mac_address: str) -> str:
""" Convert couple different MAC address formats into Cisco's 'aabb.ccdd.eeff' format """
mac_address = re.sub(r":|-|\.| ", "", mac_address.lower().strip())
mac_address = ".".join(mac_address[_ : _ + 4] for _ in range(0, len(mac_address), 4))
return mac_address
def exception_handler(function: Callable[..., Any]) -> Any:
""" Decorator to log exceptions and exit process or forward them for further processing """
from functools import wraps
@wraps(function)
def wrapper(*args, **kwargs):
try:
return function(*args, **kwargs)
except CustomException as exception:
LOGGER.error(f"{exception}")
sys.exit()
except SystemExit:
raise
except:
LOGGER.error(f"Unknown exception '{sys.exc_info()}'")
raise
return wrapper
def execute_data_processing_function(data_list: List[Any], data_processing_function: Callable[..., List[Any]],
*args: Any, max_workers: int = MAX_WORKERS, **kwargs: Any) -> List[Any]:
""" Execute generic data processing function in single or multiprocess manner and return merged list of results """
from concurrent.futures import ProcessPoolExecutor
if not data_list:
return []
if SINGLE_PROCESS_MODE:
results = [data_processing_function(_, *args, **kwargs) for _ in data_list]
else:
with ProcessPoolExecutor(max_workers=min(max_workers, len(data_list))) as executor:
process_pool = [executor.submit(data_processing_function, _, *args, **kwargs) for _ in data_list]
results = [_.result() for _ in process_pool if not _.exception() and _.result()]
return [_ for __ in results for _ in __]