-
Notifications
You must be signed in to change notification settings - Fork 0
/
IPExtractX.py
392 lines (237 loc) · 13 KB
/
IPExtractX.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#!/usr/bin/env python3
##############################################################
## ODFSearch Console | IPExtractX Standalone Python Script ##
##############################################################
## Version: 1.2 ##
## Release Date: 1/09/2024 ##
## Release Author: @Onetrak-Digital-Forensics ##
## Release License: GNUGPL-V3 ##
## Release Status: Public ##
## Release Type: Standalone Python Script ##
##############################################################
try:
import os
import ipaddress
import re
from rich import panel as rpanel
from rich import console as rcon
from rich import print as rprint
import typer as TypeCLI
prog_version = '1.2.0'
cli_main = TypeCLI.Typer(pretty_exceptions_short=True)
rcon_obj = rcon.Console()
except ImportError as Exception:
print(f'Python Environment Error: {Exception}')
raise SystemExit(3)
class IPExtractX:
"""
IPExtractX - ODFSearch Console
====
Init Parameters
====
`input_path`
----
`File` or `Directory` `Path` Of Data To Be Parsed.
`outfile`
----
`File Path` Where The Parser Will Save Positive Content Detection Messages To.
`ws_kwfile`
----
`File Path` Containing A List Format Of Words And/Or False Positive Emails, Seperated By New Lines.
`ipaddr`
----
`Bool`, Where `True` Enables The Detection Of IPV4/IPV6 Type Addresses.\n
Default Is `True` if `NoneType` Is Given
`hn`
----
`Bool`, Where `True` Enables The Detection Of Hostname Type Addresses.
Please Note: May Throw Duplicates Of EMAIL Hostnames. Should NOT Be \n
Used In Conjunction With `emlformat`
`ws`
----
`Bool`, Where `True` Enables The Detection of Keywords, Defined In `ws_kwfile`.\n
Must Specify `ws_kwfile`, Or This Option Becomes Disabled.\n
`emlformat`
----
`Bool`, Where `True` Enables The Detection of Email Type Addresses.\n
`pgpheader`
----
`Bool`, Where `True` Enables The Detection Of Pretty Good Privacy (PGP) Encrypted Email Exports.\n
Method: `BEGIN PGP MESSAGE`\n
`WRITEIO`
----
`Bool`, Where `True` Enables The Writing Of Parser Data Positive To `outfile`.\n
`PRINTIO`
----
`Bool`, Where `True` Enables Parser Output Messages To Print To The Terminal.\n
`APPENDIO`
----
`Bool`, Where `True` Enables The Appending Of `content` Read From `parser.activefn` to `parser.content_list`.\n
WARNING: May Cause Python To `Allocate Large Amounts Of Memory` If Large Quantities Of Textual Data Are Supplied.
Return Object
====
Examples:
>>> from IPExtractX import IPExtractX as IPX
>>> mycustomobj = IPX(content_dir, output_file, keywords_file, detect_ipaddr, detect_hostname, detect_wordsearch, detect_emails, detect_pgpheader).execute_parser()
>>> if mycustomobj.parsed_ipaddr in customiplistobj:
Return Argument
----
The Parser Returns `set()` Versions Of The Detection Definitions. Example, You Parse IP Addresses, The Parser Will Append Postive Captures to `self.parsed_ipaddr` As A List, Then SET To Remove Duplicates.
Return Output
----
`outfile` Saves The Detection Log During Execution If `parser.write_output` Is Given `True`
"""
def __init__(parser, input_path: str, outfile: str, ws_kwfile: str = None, ipaddr: bool = False, hn: bool = False, ws: bool = False, emlformat: bool = False, pgpheader: bool = False, WRITEIO: bool = True, PRINTIO: bool = True, APPENDIO: bool = False):
parser.dirpath = input_path
parser.outfile = outfile
parser.wswordlistfile = ws_kwfile
parser.detect_ipaddressformat = ipaddr
parser.detect_hostnameformat = hn
parser.detect_wordsearchformat = ws
parser.detect_emailformat = emlformat
parser.detect_prettygoodprivacyheaders = pgpheader
parser.pgp_regex_pattern = '(-----BEGIN PGP PUBLIC KEY BLOCK-----)(.*?)(-----END PGP PUBLIC KEY BLOCK-----)'
parser.ipv4_regex_pattern = r"([0-9]{1,3}\.){3}[0-9]{1,3}"
parser.ipv6_regex_pattern = r'\b(?:[0-9a-fA-F]{1,4}(:[0-9a-fA-F]{1,4})*)?::(?:[0-9a-fA-F]{1,4}(:[0-9a-fA-F]{1,4})*)?(?:(?<=::)|(?<=:)(?=\d+\.\d+\.\d+\.\d+)|\b)\b'
parser.hostname_regex_pattern = r'(?:(?:[A-Z0-9](?:[A-Z0-9\-]{0,61}[A-Z0-9])?\.)+[A-Z]{2,6}\.?)'
parser.email_regex_pattern = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b'
parser.verify_dirpath_isdir = os.path.isdir(input_path)
parser.verify_dirpath_isfile = os.path.isfile(input_path)
parser.verify_wswordlistfile = os.path.isfile(ws_kwfile) if ws_kwfile else False
parser.verify_outfile = os.path.isfile(outfile)
parser.printio = PRINTIO
parser.appendio = APPENDIO
parser.writeio = WRITEIO
parser.statistics = {"PGP": 0, "IPV4": 0, "IPV6": 0, "Hostnames": 0, "Keywords": 0, "Emails": 0, "Errors": 0}
parser.parsed_ipaddr = []
parser.parsed_hostnames = []
parser.parsed_emailids = []
parser.fileids_with_pgp = []
parser.content_list = []
parser.activefn = str
parser.progbanner = f""" ___ ____ _____ _ _ __ __\n |_ _| _ \| ____|_ _ | |_ _ __ __ _ ___| |_\ \/ /\n | || |_) | _| \ \/ /| __| '__/ _` |/ __| __|\ /\n | || __/| |___ > < | |_| | | (_| | (__| |_ / \\ \n |___|_| |_____/_/\_\ \__|_| \__,_|\___|\__/_/\_\\ \n\n [GNUGPL_v3] IPExtractX.py - {prog_version}\n Onetrak Digital Forensics Corporation\n"""
def execute_parser(parser):
if parser.printio:
rprint(f'[red bold]{parser.progbanner}[/red bold]')
if parser.verify_dirpath_isdir is False and parser.verify_dirpath_isfile is False:
rprint(f'[red bold] ✘ Input Error: {parser.dirpath} Is a Non-Existent File Path\n')
return parser
keywords = parser.load_keywords()
if keywords == None:
if parser.printio:
rprint('[red bold] ✘ Error: Wordlist File Not Loaded: {parser_verify_wswordlistfile} = {False}[/red bold]')
rprint('[blue bold] Info: os.path.isfile(parser.wswordlistfile) reports {False}[/blue bold]\n')
if parser.verify_outfile:
if parser.writeio:
if parser.printio:
rprint(f'[bold yellow] ✘ Warning: Output File "{parser.outfile}" Exists! Clearing File...[/bold yellow]\n')
os.remove(parser.outfile)
with rcon_obj.status(f'[yellow bold] Running Parser... ', spinner="bouncingBar") as statusanim:
try:
with open(parser.outfile, mode='w') as outfile:
if parser.verify_dirpath_isfile:
parser.parse_file(parser.dirpath, outfile, keywords)
elif parser.verify_dirpath_isdir:
for root, dirs, files in os.walk(parser.dirpath):
for file in files:
statusanim.update(f'[purple bold] Parsing Text File: {parser.activefn}... ', spinner="hamburger")
parser.parse_file(os.path.join(root, file), outfile, keywords)
statusanim.stop()
if parser.writeio:
outfile.write('\n--- STATS ---\n')
for key, value in parser.statistics.items():
outfile.write(f"{key}: {value}\n")
if parser.printio:
rprint('\n[green bold] ✔ Success: Parser Execution Complete![/green bold]')
if parser.writeio == True:
rprint(f'[blue bold] Output File > {parser.outfile}[/blue bold]\n')
if parser.printio:
if parser.statistics['Errors'] > 0:
rprint(f'[red bold] ✘ Preliminary Warning: {parser.statistics["Errors"]} Error(s) Occured During The Parser Execution!!![/red bold]\n')
outfile.close()
parser.parsed_ipaddr = list(set(parser.parsed_ipaddr))
parser.parsed_hostnames = list(set(parser.parsed_hostnames))
parser.parsed_emailids = list(set(parser.parsed_emailids))
parser.fileids_with_pgp = list(set(parser.fileids_with_pgp))
except Exception as ERRRESP:
rprint(f'\n[red bold] ✘ Parser Error: {ERRRESP}\n')
finally:
return parser
def parse_file(parser, input_path, outfile, keywords):
parser.activefn = input_path
try:
with open(input_path, "r") as open_file:
content = open_file.read()
if parser.appendio:
parser.content_list.append(content)
if parser.detect_ipaddressformat:
parser.detect_ip_addresses(content, outfile)
if parser.detect_hostnameformat:
parser.detect_hostnames(content, outfile)
if parser.detect_wordsearchformat:
parser.detect_keywords(content, outfile, keywords)
if parser.detect_emailformat:
parser.detect_emails(content, outfile, keywords)
if parser.detect_prettygoodprivacyheaders and re.search(parser.pgp_regex_pattern, content, re.DOTALL):
if parser.writeio:
outfile.write(f"PGP message found in {input_path}\n")
parser.statistics["PGP"] += 1
parser.fileids_with_pgp.append(parser.activefn)
except Exception as e:
outfile.write(f"Cannot read file {input_path}. Error: {str(e)}\n")
parser.statistics["Errors"] += 1
def detect_ip_addresses(parser, content, outfile):
for pattern in [(parser.ipv4_regex_pattern, "IPV4"), (parser.ipv6_regex_pattern, "IPV6")]:
if pattern[1] == "IPV4" or pattern[1] == "IPV6":
matches = re.finditer(pattern[0], content)
for match in matches:
ip = match.group()
try:
ipaddress.ip_address(ip)
if parser.writeio:
outfile.write(f"{pattern[1]} '{ip}' found in {parser.activefn}\n")
parser.statistics[pattern[1]] += 1
parser.parsed_ipaddr.append(ip)
except ValueError:
pass
def detect_hostnames(parser, content, outfile):
hostnames = re.findall(parser.hostname_regex_pattern, content, re.IGNORECASE)
for hostname in hostnames:
if parser.writeio:
outfile.write(f"Hostname '{hostname}' found in {parser.activefn}\n")
parser.statistics["Hostnames"] += 1
parser.parsed_hostnames.append(hostname)
def detect_keywords(parser, content, outfile, keywords):
if keywords:
for keyword in keywords:
if keyword.lower() in content.lower():
if parser.writeio:
outfile.write(f"Keyword '{keyword}' found in {parser.activefn}\n")
parser.statistics["Keywords"] += 1
def detect_emails(parser, content, outfile, no_email):
emails = re.findall(parser.email_regex_pattern, content)
for email in emails:
if parser.detect_wordsearchformat == True and email in no_email:
pass
else:
if parser.writeio:
outfile.write(f"Email '{email}' found in {parser.activefn}\n")
parser.statistics["Emails"] += 1
parser.parsed_emailids.append(email)
def load_keywords(parser):
if parser.verify_wswordlistfile:
with open(parser.wswordlistfile, mode='r') as wlf:
load_return = [line.strip() for line in wlf.readlines()]
wlf.close()
return load_return
else:
return None
@cli_main.command(name='X', help=f'HTML Mail Regular Expression Search Pattern Detection Software Version {prog_version}\n\nThis Script Is Also An Importable Class!, try: >>> from IPExtractX import IPExtractX')
def extract_main(content_dir: str, keywords_file: str = f'{os.getcwd()}/wordlists/words1.list', output_file: str = 'output.txt', detect_ipaddr: bool = True, detect_hostname: bool = False, detect_wordsearch: bool = False, detect_emails: bool = False, detect_pgpheader: bool = False):
parser_fileop = IPExtractX(content_dir, output_file, keywords_file, detect_ipaddr, detect_hostname, detect_wordsearch, detect_emails, detect_pgpheader).execute_parser()
### Custom Code Goes Here ###
# print(parser_fileop.parsed_ipaddr)
# print(parser_fileop.parsed_hostnames)
raise SystemExit(1)
if __name__ == '__main__':
cli_main()