-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVeeamLogAnonymizer.py
649 lines (550 loc) · 22.9 KB
/
VeeamLogAnonymizer.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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = "Julien Mousqueton"
__copyright__ = "Copyright 2025, Julien Mousqueton"
__version__ = "1.2"
# Import necessary modules
import re
import random
import string
import sys
import os
import argparse
import logging
import json
import shutil
import time
import datetime
import glob
# Configure logging
logging.basicConfig(
format='%(asctime)s,%(msecs)d %(levelname)-8s %(message)s',
datefmt='%Y-%m-%d:%H:%M:%S',
level=logging.INFO
)
# Define custom logging functions
def stdlog(msg):
'''Standard info logging'''
logging.info(msg)
def dbglog(msg):
'''Debug logging'''
logging.debug(msg)
def errlog(msg):
'''Error logging'''
logging.error(msg)
def generate_random_string(length=12):
characters = string.ascii_letters + string.digits
return ''.join(random.choice(characters) for _ in range(length))
def replace_string_in_file(input, output, old_value, new_value):
with open(input, 'r') as file:
content = file.read()
pattern = re.compile(re.escape(old_value), re.IGNORECASE)
content = pattern.sub(lambda match: new_value if match.group(0).islower() else new_value.upper(), content)
with open(output, 'w') as file:
file.write(content)
def check_log_contains_line(input_file, line_to_check):
with open(input_file, 'r') as file:
for line in file:
if line_to_check in line:
return True
return False
def get_object_from_location(location):
components = location.split('\\')
return components
def get_element_from_fqdn(fqdn):
result = fqdn.split('.')
return result
def is_fqdn(string):
# Regular expression patterns to match FQDN
fqdn_pattern = r'^[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
if re.match(fqdn_pattern, string):
return True
else:
return False
def is_IP(string):
# Regular expression patterns to match IP addresses
ip_pattern = r'^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$'
if re.match(ip_pattern, string):
return True
else:
return False
def anonymized_IPv4(ip):
ip_address = ip.split(".")
ip_address[0] = "**"
ip_address[1] = "**"
masked_ip = ".".join(ip_address)
return masked_ip
def process_IP(input_file, output_file):
# Define a regular expression pattern to match IP addresses
ip_pattern = r"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b(?!])"
# Read the file
with open(input_file, 'r', encoding='utf-8', errors='ignore') as file:
content = file.read()
# Find all IP addresses in the content using regex
ip_addresses = re.findall(ip_pattern, content)
# Replace the first two numbers with "*"
for ip in ip_addresses:
# Exception for VMware vSphere version
if not ip.startswith(('7.', '8.')):
content = content.replace(ip, anonymized_IPv4(ip))
### another pattern :(
ip_pattern = r'\[::ffff:([\d.]+)\]'
ip_addresses = re.findall(ip_pattern, content)
for ip in ip_addresses:
# Exception for VMware vSphere version
if not ip.startswith(('7.', '8.')):
content = content.replace(ip, anonymized_IPv4(ip))
# Write the modified content back to the file
with open(output_file, 'w') as file:
file.write(content)
def find_pattern(pattern_key,log_file_path,):
try:
with open("patterns.json", "r") as patterns_file:
patterns_dict = json.load(patterns_file)
pattern = patterns_dict.get(pattern_key)
if not pattern:
return f"Pattern key '{pattern_key}' not found in patterns.json."
with open(log_file_path, "r", encoding='utf-8', errors='ignore') as log_file:
log_content = log_file.read()
matches = re.findall(pattern, log_content)
matches = list(set(matches))
if matches:
return matches
else:
return None
except FileNotFoundError:
return f"Error: File '{log_file_path}' not found."
except Exception as e:
return f"An error occurred: {str(e)}"
def extract_domain(email):
# Utilisation d'une expression régulière pour extraire le nom de domaine
match = re.search(r'@([\w.-]+)', email)
# Si une correspondance est trouvée, renvoie le nom de domaine, sinon renvoie None
if match:
return match.group(1)
else:
return None
def update_json_file(object_name, name_of_value, value, output_file):
# Try to read the existing JSON data if the file exists
try:
with open(output_file, 'r') as json_file:
existing_data = json.load(json_file)
except FileNotFoundError:
# pass # If the file doesn't exist, ignore the error
existing_data = {}
# Update the existing data or create a new entry
if object_name in existing_data:
# Check if the name_of_value already exists in the list
found = False
for item in existing_data[object_name]:
if name_of_value in item:
item[name_of_value] = str(value) # Update the existing value
found = True
break
if not found:
existing_data[object_name].append({name_of_value: str(value)}) # Add a new entry
else:
existing_data[object_name] = [{name_of_value: str(value)}]
# Write the updated data to the file
with open(output_file, 'w') as json_file:
json.dump(existing_data, json_file, indent=4)
def main():
parser = argparse.ArgumentParser(description="Anonymize your Veeam Backup & Replication logs.")
parser.add_argument("-i", "--input", dest="input_file", help="Input log file")
parser.add_argument("-d", "--directory", dest="input_directory", help="Input directory containing log files")
parser.add_argument("-o", "--output", dest="output_directory", required=True, help="Output directory for processed log files")
parser.add_argument("-f", "--force", action="store_true", help="Force overwrite if output files exist or force the creation of output directory if not exists")
parser.add_argument("-m","--mapping", action="store_true", help="Display the mapping table of anonymized data")
parser.add_argument("-v", "--verbose", action="store_true", help="Display processing files and other information")
parser.add_argument("-D", "--dictionary", action="store_true", help="output a JSON file with the dictionary of anonymized data")
if not os.path.exists('patterns.json'):
errlog("Error: patterns.json not found.")
sys.exit(1)
args = parser.parse_args()
input_files=[]
verbose = args.verbose
if args.input_file:
input_files.append(args.input_file)
elif args.input_directory:
input_directory = args.input_directory
if not os.path.isdir(input_directory):
errlog('Error : '+ input_directory +' is not a directory')
sys.exit(1)
if not os.path.exists(input_directory):
errlog('Error: Input directory ' + input_directory + ' does not exist.')
sys.exit(1)
# Use os.walk to find all .log files recursively
for root, _, files in os.walk(input_directory):
for filename in files:
if filename.endswith(".log"):
input_files.append(os.path.join(root, filename))
else:
errlog('Error: You must specify either -i or -d.')
sys.exit(1)
output_directory = args.output_directory
# Specify the pattern to match files with a similar format
filename_pattern = 'VeeamAnonymizer-*.json'
# Create the full path pattern by joining the directory and filename pattern
file_pattern = os.path.join(output_directory, filename_pattern)
# Use glob to find files matching the pattern
matching_files = glob.glob(file_pattern)
# Check if any matching files were found
if matching_files:
stdlog("ATTENTION : An old VeeamAnonymizer dictionnary exists in the output directory")
# Init list
VeeamServer = False
VeeamUserList = []
User_set = set()
SMTPServerList = []
SMTPServer_set = set()
vCenterList = []
vCenter_set = set()
DomainList = []
Domain_set = set()
LocationList = []
Location_set = set()
EmailList = []
Email_set = set()
ESXiList = []
ESXi_set = set()
nbfile = 0
stdlog('Collecting information')
for input_file in input_files:
nbfile += 1
filename = os.path.basename(input_file)
dbglog('*** ' + filename)
output_file = os.path.join(output_directory, filename)
if os.path.exists(output_file) and not args.force:
errlog(f'Error: Output file {output_file} already exists. Use -f or --force to overwrite.')
sys.exit(1)
### SMTP
SMTPServers = find_pattern('SMTPServer',input_file)
try:
for SMTPServer in SMTPServers:
if SMTPServer in SMTPServer_set:
continue
SMTPServer_set.add(SMTPServer)
if is_fqdn(SMTPServer):
RandomSMTP = str(generate_random_string())
Domain = '.'.join(get_element_from_fqdn(SMTPServer)[1:])
if Domain not in Domain_set:
Domain_set.add(Domain)
element = (Domain, RandomDomain)
DomainList.append(element)
else:
RandomSMTP = anonymized_IPv4(SMTPServer)
element = (SMTPServer, RandomSMTP)
SMTPServerList.append(element)
except:
pass
if not VeeamServer:
try:
VeeamServer = str(find_pattern('VeeamServer',input_file)[0])
RandomVeeamServer = str(generate_random_string())
except:
pass
### Veeam User
VeeamUsers = find_pattern('VeeamUser',input_file)
try:
for VeeamUser in VeeamUsers:
tmpUser = VeeamUser.split("\\")[1]
tmpRandom = str(generate_random_string())
if tmpUser in User_set:
continue
User_set.add(tmpUser) # Add the unique tmpUser value to the set
element = (tmpUser, tmpRandom)
VeeamUserList.append(element)
except:
pass
### vCenter Server
vCenters = find_pattern('vCenter',input_file)
try:
for vCenter in vCenters:
if len(vCenter) == 0:
continue
if is_fqdn(vCenter):
RandomDomain = str(generate_random_string())
Domain = '.'.join(get_element_from_fqdn(vCenter)[1:])
if Domain not in Domain_set:
Domain_set.add(Domain)
element = (Domain, RandomDomain)
DomainList.append(element)
vCenter = get_element_from_fqdn(vCenter)[0]
RandomvCenter = str(generate_random_string())
else:
RandomvCenter= anonymized_IPv4(vCenter)
if vCenter in vCenter_set:
continue
vCenter_set.add(vCenter) # Add the unique tmpUser value to the set
element = (vCenter, RandomvCenter)
vCenterList.append(element)
except:
pass
### Location
Locations = find_pattern('Location', input_file)
try:
for Location in Locations:
split_data = Location.split('\\')
# Print all parts except the first and last ones
for part in split_data[1:-1]:
RandomLocation = str(generate_random_string())
if part not in Location_set:
Location_set.add(part)
element = (part, RandomLocation)
LocationList.append(element)
except:
pass
#ESXi Server
ESXiServers = find_pattern('ESXiServer',input_file)
try:
for ESXi in ESXiServers:
if is_fqdn(ESXi):
RandomDomain = str(generate_random_string())
Domain = '.'.join(get_element_from_fqdn(ESXi)[1:])
if Domain not in Domain_set:
Domain_set.add(Domain)
element = (Domain, RandomDomain)
DomainList.append(element)
ESXi = get_element_from_fqdn(ESXi)[0]
RandomESXi = str(generate_random_string())
else:
RandomESXi = anonymized_IPv4(ESXi)
if ESXi in ESXi_set:
continue
ESXi_set.add(ESXi)
element = (ESXi, RandomESXi)
ESXiList.append(element)
except:
pass
### email
Emails = find_pattern('Email', input_file)
try:
for Email in Emails:
RandomEmail = str(generate_random_string())
Domain = extract_domain(Email)
if Domain and Domain not in Domain_set:
Domain_set.add(Domain)
RandomDomain = str(generate_random_string())
element = (Domain, RandomDomain)
DomainList.append(element)
if Email in Email_set:
continue
Email_set.add(Email)
RandomEmail = RandomEmail + '@' + RandomDomain
element = (Email, RandomEmail)
EmailList.append(element)
except:
pass
# Clean list
UniqueVeeamUsers = list(sorted(set(VeeamUserList)))
UniqueSMTPSevers = list(set(SMTPServerList))
UniquevCenters = list(sorted(set(vCenterList)))
UniqueEmails = list(set(EmailList))
UniqueESXi = list(sorted(set(ESXiList)))
UniqueLocation = list(sorted(set(LocationList)))
### GET Domain and subdomain
try:
for Domain in DomainList:
_Original, _Random = Domain
parts = _Original.split('.')
if len(parts) > 2:
main_domain = '.'.join(parts[-2:])
if main_domain not in Domain_set:
Domain_set.add(main_domain)
RandomDomain = str(generate_random_string())
element = (main_domain, RandomDomain)
DomainList.append(element)
except:
pass
UniqueDomains = list(set(DomainList))
## Cleaning ESXi
filtered_ESXi = [(original, random) for (original, random) in UniqueESXi if original not in {vc[0] for vc in UniquevCenters}]
if args.dictionary:
if not os.path.exists(output_directory) and args.force:
os.makedirs(output_directory)
current_datetime = datetime.datetime.now()
formatted_datetime = current_datetime.strftime("%Y-%m-%d_%H-%M-%S")
filename = f"VeeamAnonymizer-{formatted_datetime}.json"
outputdictfile = output_directory + "/" + filename
# Add a new entry to the "Vcenter" section
for data in UniqueVeeamUsers:
_Original, _Random = data
update_json_file("VeeamUsers", _Original, _Random, outputdictfile)
for data in UniqueSMTPSevers:
_Original, _Random = data
update_json_file("SMTP Servers", _Original, _Random, outputdictfile)
for data in UniquevCenters:
_Original, _Random = data
update_json_file("vCenter Servers", _Original, _Random, outputdictfile)
for data in UniqueLocation:
_Original, _Random = data
update_json_file("vCenter Location", _Original, _Random, outputdictfile)
for data in UniqueEmails:
_Original, _Random = data
update_json_file("Email address", _Original, _Random, outputdictfile)
for data in filtered_ESXi:
_Original, _Random = data
update_json_file("ESXi hosts", _Original, _Random, outputdictfile)
for data in UniqueDomains:
_Original, _Random = data
update_json_file("Domain names", _Original, _Random, outputdictfile)
stdlog('Json file created')
if args.mapping:
# Show the mapping
## VEEAM
try:
stdlog('* Veeam Server : ' + VeeamServer + ' -> ' + RandomVeeamServer)
except:
pass
### SMTP
try:
for SMTPServer in UniqueSMTPSevers:
_OriginalSMTP, _RandomSMTP = SMTPServer
stdlog('* SMTP Server : ' + _OriginalSMTP + ' -> ' + _RandomSMTP)
except:
pass
### vCenter
try:
for vCenter in UniquevCenters:
_Original, _Random = vCenter
stdlog('* vCenter Server : ' + _Original + ' -> ' + _Random)
except:
pass
# Location
try:
for Location in UniqueLocation:
_Original, _Random = Location
stdlog('* vCenter Location : ' + _Original + ' -> ' + _Random)
except:
pass
### Domain
try:
for Domain in UniqueDomains:
_Original, _Random = Domain
stdlog('* Domain : ' + _Original + ' -> ' + _Random)
except:
pass
### Email
try:
for Email in UniqueEmails:
_Original, _Random = Email
stdlog('* Email : ' + _Original + ' -> ' + _Random)
except:
pass
### Veeam User Accounts
try:
for VeeamUser in UniqueVeeamUsers:
_Original, _Random = VeeamUser
stdlog('* User: ' + _Original + ' -> ' + _Random)
except:
pass
### ESXi
try:
for ESXi in filtered_ESXi:
_Original, _Random = ESXi
stdlog('* ESXi: ' + _Original + ' -> ' + _Random)
except:
pass
###
# Anonymizing
###
i = 0
## UniqueESXi = list(sorted(set(ESXiList)))
stdlog('Processing anonymizing of ' + str(nbfile) + ' file(s) ... ')
for input_file in input_files:
filename = os.path.basename(input_file)
if args.input_file:
output_file = os.path.join(output_directory, filename)
full_output_directory = os.path.dirname(output_file)
else:
output_file = input_file.replace(args.input_directory,args.output_directory,1)
full_output_directory = os.path.dirname(output_file)
if not os.path.exists(full_output_directory) and args.force:
os.makedirs(full_output_directory)
if verbose:
i += 1
file_size_bytes = os.path.getsize(input_file)
file_size_megabytes = round(file_size_bytes / (1024 * 1024),2)
stdlog('- Processing file ['+ str(i) + '/' + str(nbfile) + '] '+ input_file + '(' + str(file_size_megabytes)+ ' Mb)')
try:
shutil.copy(input_file, output_file)
except:
errlog('Fatal Error processing : ' + input_file + ' --> ' + output_file)
sys.exit(1)
try:
replace_string_in_file(output_file,output_file, VeeamServer, RandomVeeamServer)
except:
pass
try:
for ESXi in filtered_ESXi:
_Original, _Random = ESXi
dbglog(' + anonymizing ESXi')
replace_string_in_file(output_file,output_file, _Original, _Random)
except:
pass
try:
for Domain in UniqueDomains:
_Original, _Random = Domain
dbglog(' + anonymizing Domain')
replace_string_in_file(output_file,output_file, _Original, _Random)
except:
pass
try:
for SMTPServer in UniqueSMTPSevers:
_Original, _Random = SMTPServer
dbglog(' + anonymizing SMTPServer')
replace_string_in_file(output_file,output_file, _Original, _Random)
except:
pass
try:
for vCenter in UniquevCenters:
_Original, _Random = vCenter
dbglog(' + anonymizing vCenter')
replace_string_in_file(output_file,output_file, _Original, _Random)
except:
pass
try:
for VeeamUser in UniqueVeeamUsers:
_Original, _Random = VeeamUser
dbglog(' + anonymizing VeeamUser')
replace_string_in_file(output_file,output_file, _Original, _Random)
except:
pass
try:
for Email in UniqueEmails:
_Original, _Random = Email
dbglog(' + anonymizing Email')
replace_string_in_file(output_file,output_file, _Original, _Random)
except:
pass
try:
for Location in UniqueLocation:
_Original, _Random = Location
dbglog(' + anonymizing Location')
replace_string_in_file(output_file,output_file, _Original, _Random)
except:
pass
# For IPs
dbglog(' + anonymizing IP Address')
process_IP(output_file,output_file)
dbglog('- File ' + input_file + ' processed')
end_time = time.time()
elapsed_time = end_time - start_time
minutes = str(int(elapsed_time // 60))
seconds = str(int(elapsed_time % 60))
stdlog('Anonymizng finished in ' + minutes + ' minutes and ' + seconds + ' seconds')
if __name__ == "__main__":
start_time = time.time()
print(
f'''
.-. .-.,---. ,---. .--. ,-. .---. ,--, .--. .-. .-. .---. .-. .-..-. .-. ,-. _____ ,---. ,---.
\ \ / / | .-' | .-' / /\ \ |\ /| | | / .-. ) .' .' / /\ \ | \| |/ .-. ) | \| | \ \_/ )/|\ /||(|/___ / | .-' | .-.\
\ V / | `-. | `-. / /__\ \|(\ / | | | | | |(_)| | __ / /__\ \| | || | |(_)| | | \ (_)|(\ / |(_) / /) | `-. | `-'/
) / | .-' | .-' | __ |(_)\/ | | | | | | | \ \ ( _) | __ || |\ || | | | | |\ | ) ( (_)\/ || | / /(_)| .-' | (
(_) | `--.| `--.| | |)|| \ / | | `--.\ `-' / \ `-) ) | | |)|| | |)|\ `-' / | | |)| | | | \ / || | / /___ | `--.| |\ \
/( __.'/( __.'|_| (_)| |\/| | |( __.')---' )\____/ |_| (_)/( (_) )---' /( (_) /(_| | |\/| |`-'(_____/ /( __.'|_| \)\
(__) (__) '-' '-' (_) (_) (__) (__) (_) (__) (__) '-' '-' (__) v {__version__} (__)
by Julien Mousqueton (@JMousqueton)
'''
)
main()