-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsanitizeLogs.py
100 lines (90 loc) · 2.39 KB
/
sanitizeLogs.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
#! /usr/bin/env python3
'''o365 Audit Log Sanitizer
Redact sensitive fields from Office365 Audit logs
Author: Ian Day
Initial Release: December 8 2019 Version 1.0
'''
import csv
import json
# customize to fit your environment
fileName = 'AuditLog_2019-11-25_2019-12-03.csv'
redactedStr='*REDACTED*'
redactedFields = [
'LogonUserSid',
'Query',
'ModifiedProperties',
'ActorIpAddress',
'MailboxOwnerUPN',
'ListId',
'DestinationFileName',
'Item',
'SessionId',
'WebId',
'MachineId',
'Parameters',
'ApplicationId',
'ExchangeLocations',
'CorrelationId',
'UserKey',
'ClientVersion',
'TargetUserOrGroupName',
'ObjectId',
'Id',
'SourceRelativeUrl',
'ActorContextId',
'DestFolder',
'OriginatingServer',
'SiteUrl',
'MailboxGuid',
'ClientIP',
'IntraSystemId',
'Target',
'ExtendedProperties',
'Actor',
'MachineDomainInfo',
'UserAgent',
'ClientIPAddress',
'UniqueSharingId',
'OrganizationId',
'DestinationRelativeUrl',
'MailboxOwnerSid',
'UserId',
'ListItemUniqueId',
'MailboxOwnerMasterAccountSid',
'InterSystemsId',
'SourceFileName',
'OrganizationName',
'AffectedItems',
'EffectiveOrganization',
'ClientInfoString',
'TargetContextId',
'EventData',
'ClientApplication',
'Site',
'Folder'
]
# required for output file
cleanOutput = []
auditFieldNames = ['CreationDate','UserIds','Operations','AuditData']
with open(fileName, 'r', encoding='latin-1') as inFile:
dictReader = csv.DictReader(inFile, fieldnames=auditFieldNames)
for line in dictReader:
try:
# most data is contained in a large json string
# create dicitonary and loop through fields
record = json.loads(line['AuditData'])
for field in record:
if field in redactedFields:
record[field] = redactedStr
# update current log entry with redacted fields
line['AuditData'] = json.dumps(record)
line['UserIds'] = redactedStr
# add to output variable
cleanOutput.append(line)
except:
continue
# write to file
with open('redacted' + fileName, 'w') as outFile:
outWriter = csv.DictWriter(outFile, fieldnames=auditFieldNames, lineterminator='\n')
outWriter.writeheader()
outWriter.writerows(cleanOutput)