forked from Qianlitp/WatchAD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLog.py
229 lines (198 loc) · 7.84 KB
/
Log.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
#!/usr/bin/python3
# coding: utf-8
# author: 9ian1i https://github.com/Qianlitp
"""
单条日志对象封装
"""
from tools.TicketParser import TicketParser
from tools.common.common import datetime_to_common_str, utc_to_local_datetime, md5
class Log(object):
def __init__(self, record, doc_id=None):
self.doc_id = doc_id
self.record = record
self.event_id = record["event_id"]
self.log_time = datetime_to_common_str(utc_to_local_datetime(record["@timestamp"]))
self.utc_log_time = record["@timestamp"]
self.level = record["level"]
self.message = record["message"]
self.record_number = record["record_number"]
self.dc_computer_name = record["computer_name"]
self.dc_host_name = record["beat"]["hostname"]
if "event_data" in record:
self.event_data = record["event_data"]
self.source_info = SourceInfo(record["event_data"])
self.target_info = TargetInfo(record["event_data"])
self.subject_info = SubjectInfo(record["event_data"])
self.ticket_info = TicketInfo(record["event_data"])
self.object_info = ObjectInfo(record["event_data"])
@property
def id(self):
id_str = str(self.event_id)
key_list = sorted(self.record["event_data"].keys())
for key in key_list:
id_str += str(self.record["event_data"][key])
return md5(id_str)
class SourceInfo(object):
def __init__(self, event_data):
self.work_station_name = None
self.ip_address = None
self.port = None
self._field_map = {
"WorkstationName": "work_station_name",
"IpAddress": "ip_address",
"IpPort": "port"
}
for key, value in self._field_map.items():
if key not in event_data:
continue
elif key == "IpAddress":
ip = event_data[key]
if ip.startswith("::ffff:"):
ip = ip.replace("::ffff:", "")
self.__dict__.update({value: ip})
else:
self.__dict__.update({value: event_data[key]})
def get_doc(self):
return {
"work_station_name": self.work_station_name if self.work_station_name else "",
"ip_address": self.ip_address if self.ip_address else "",
"port": self.port if self.port else ""
}
class TargetInfo(object):
def __init__(self, event_data):
self.domain_name = None
self.user_name = None
self.user_sid = None
self.logon_id = None
self.info = None
self.server_name = None
self.sid = None
self.full_user_name = None
self._field_map = {
"TargetDomainName": "domain_name",
"TargetUserName": "user_name",
"TargetUserSid": "user_sid",
"TargetSid": "sid",
"TargetLogonId": "logon_id",
"TargetInfo": "info",
"TargetServerName": "server_name"
}
for key, value in self._field_map.items():
if key not in event_data:
continue
# UserName 中可能存在 @xxx.com 的后缀
if key == "TargetUserName":
if "@" in event_data[key]:
user_name = event_data[key].split("@")[0]
self.__dict__.update({value: user_name})
else:
self.__dict__.update({value: event_data[key]})
self.__dict__.update({"full_user_name": event_data[key]})
elif key in event_data:
self.__dict__.update({value: event_data[key]})
def get_doc(self):
return {
"domain_name": self.domain_name,
"user_name": self.user_name,
"user_sid": self.user_sid,
"sid": self.sid,
"logon_id": self.logon_id,
"info": self.info,
"server_name": self.server_name,
"full_user_name": self.full_user_name
}
class SubjectInfo(object):
def __init__(self, event_data):
self.logon_id = None
self.user_name = None
self.domain_name = None
self.user_sid = None
self.full_user_name = None
self._field_map = {
"SubjectDomainName": "domain_name",
"SubjectUserName": "user_name",
"SubjectUserSid": "user_sid",
"SubjectLogonId": "logon_id",
}
for key, value in self._field_map.items():
if key not in event_data:
continue
# UserName 中可能存在 @xxx.com 的后缀
if key == "SubjectUserName":
if "@" in event_data[key]:
user_name = event_data[key].split("@")[0]
self.__dict__.update({value: user_name})
else:
self.__dict__.update({value: event_data[key]})
self.__dict__.update({"full_user_name": event_data[key]})
elif key in event_data:
self.__dict__.update({value: event_data[key]})
def get_doc(self):
return {
"domain_name": self.domain_name if self.domain_name else "",
"user_name": self.user_name if self.user_name else "",
"user_sid": self.user_sid if self.user_sid else "",
"logon_id": self.logon_id if self.logon_id else "",
"full_user_name": self.full_user_name if self.full_user_name else ""
}
class TicketInfo(object):
def __init__(self, event_data):
self.encryption_type = None
self.encryption_type_detail = None
self.options = None
self.options_detail = None
self.status = None
self._field_map = {
"TicketEncryptionType": "encryption_type",
"TicketOptions": "options",
"Status": "status"
}
ticket_parser = TicketParser()
for key, value in self._field_map.items():
if key not in event_data:
continue
elif key == "TicketEncryptionType":
self.__dict__.update({value: event_data[key]})
self.encryption_type_detail = ticket_parser.encryption_parse(event_data[key])
elif key == "TicketOptions":
self.__dict__.update({value: event_data[key]})
self.options_detail = ticket_parser.option_parse(event_data[key])
else:
self.__dict__.update({value: event_data[key]})
def get_doc(self):
return {
"encryption_type": self.encryption_type if self.encryption_type else "",
"encryption_type_detail": self.encryption_type_detail if self.encryption_type_detail else "",
"options": self.options if self.options else "",
"options_detail": self.options_detail if self.options_detail else "",
"status": self.status if self.status else ""
}
class ObjectInfo(object):
def __init__(self, event_data):
self.dn = None
self.guid = None
self.class_ = None
self.server = None
self.type = None
self.name = None
self._field_map = {
"ObjectDN": "dn",
"ObjectGUID": "guid",
"ObjectClass": "class_",
"ObjectServer": "server",
"ObjectType": "type",
"ObjectName": "name"
}
for key, value in self._field_map.items():
if key not in event_data:
continue
self.__dict__.update({value: event_data[key]})
def get_doc(self):
return {
"dn": self.dn if self.dn else "",
"guid": self.guid if self.guid else "",
"class": self.class_ if self.class_ else "",
"server": self.server if self.server else "",
"type": self.type if self.type else "",
"name": self.name if self.name else ""
}