-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsyspce_output.py
More file actions
319 lines (270 loc) · 14.7 KB
/
syspce_output.py
File metadata and controls
319 lines (270 loc) · 14.7 KB
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
import logging
import win32api
import win32con
import win32security
import win32evtlogutil
import win32evtlog
import os
import ctypes
from syspce_parser import get_action_from_id
from syspce_parser import get_default_parameter_from_id
log = logging.getLogger('sysmoncorrelator')
STD_INPUT_HANDLE = -10
STD_OUTPUT_HANDLE= -11
STD_ERROR_HANDLE = -12
FOREGROUND_BLUE = 0x01 # text color contains blue.
FOREGROUND_GREEN= 0x02 # text color contains green.
FOREGROUND_RED = 0x04 # text color contains red.
FOREGROUND_WHITE= 0x07 # text color contains withe.
FOREGROUND_INTENSITY = 0x08 # text color is intensified.
BACKGROUND_BLUE = 0x10 # background color contains blue.
BACKGROUND_GREEN= 0x20 # background color contains green.
BACKGROUND_RED = 0x40 # background color contains red.
BACKGROUND_INTENSITY = 0x80 # background color is intensified.
std_out_handle = ctypes.windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
"""
Class for printing to stdout or sending to eventlog alerts.
"""
class Output_(object):
def __init__(self, full_log=False):
self.log = log
self.full_log = full_log
def set_color(self, color, handle=std_out_handle):
"""(color) -> BOOL
Example: set_color(FOREGROUND_GREEN | FOREGROUND_INTENSITY)
"""
bool = ctypes.windll.kernel32.SetConsoleTextAttribute(handle, color)
return bool
def format_result_hierarchy(self, res):
"""Print to stdout alerts
res: list of dicctionary entries representing the alerts
[{'Computer': machine name ,'ProcessChain': list of nodes,
'Rulename': name,'RuleID': rule id}, ...]
"""
alerts = []
for anomaly in res:
alert = ""
alert += "\n"
alert += "\tPHE ALERT [%s]: %s\n" % (anomaly['RuleID'],
anomaly['Rulename'])
tab = '--'
ntabs = 0
for process in reversed(anomaly['ProcessChain']):
ntabs +=1
if process.acciones['1'][0]['Alert']:
alert += "\t!%s> %s (%s) [%s] %s\n" % (
tab*ntabs,
get_action_from_id(1),
process.pid,
process.acciones['1'][0]['ProcessTTL'],
process.cmd)
#process.acciones['1'][0]['Alert'] = False
else:
alert += "\t%s> %s (%s) [%s] %s\n" % (tab*ntabs,
get_action_from_id(1),
process.pid,
process.acciones['1'][0]['ProcessTTL'],
process.cmd)
for action_type in process.acciones:
for action in process.acciones[action_type]:
param = get_default_parameter_from_id(\
int(action_type))
if int(action_type) != 1:
if action['Alert']:
alert += "\t!%s> %s %s\n" %\
(tab*(ntabs+2), \
get_action_from_id(int(action_type)),
action[param])
#action['Alert'] = False
else:
if self.full_log:
alert += "\t%s> %s %s\n" %\
(tab*(ntabs+2), \
get_action_from_id(int(action_type)),
action[param])
# Special case for Real Parent
elif action['CreationType'] == 'InjectedThread':
alert += "\t!%s> [I] REAL PARENT %s\n" % \
(tab*(ntabs+2),
action['RealParent'])
alerts.append(alert)
return alerts
def log_result_hierarchy(self, res):
"""Print to stdout alerts
res: list of dicctionary entries representing the alerts
[{'Computer': machine name ,'ProcessChain': list of nodes,
'Rulename': name,'RuleID': rule id}, ...]
"""
for anomaly in res:
self.log.info("")
self.log.info("PHE ALERT [%s]: %s" % (anomaly['RuleID'],
anomaly['Rulename']))
tab = '--'
ntabs = 0
for process in reversed(anomaly['ProcessChain']):
ntabs +=1
if process.acciones['1'][0]['Alert']:
self.set_color(FOREGROUND_GREEN | FOREGROUND_INTENSITY)
self.log.info("!%s> %s (%s) [%s] %s" % (
tab*ntabs,
get_action_from_id(1),
process.pid,
process.acciones['1'][0]['ProcessTTL'],
process.cmd))
self.set_color(FOREGROUND_WHITE | FOREGROUND_INTENSITY)
#process.acciones['1'][0]['Alert'] = False
else:
self.log.info("%s> %s (%s) [%s] %s" % (tab*ntabs,
get_action_from_id(1),
process.pid,
process.acciones['1'][0]['ProcessTTL'],
process.cmd))
for action_type in process.acciones:
for action in process.acciones[action_type]:
param = get_default_parameter_from_id(\
int(action_type))
if int(action_type) != 1:
if action['Alert']:
self.set_color(FOREGROUND_GREEN | FOREGROUND_INTENSITY)
self.log.info("!%s> %s %s" %\
(tab*(ntabs+2), \
get_action_from_id(int(action_type)),
action[param]))
self.set_color(FOREGROUND_WHITE | FOREGROUND_INTENSITY)
#action['Alert'] = False
else:
if self.full_log:
self.log.info("%s> %s %s" %\
(tab*(ntabs+2), \
get_action_from_id(int(action_type)),
action[param]))
# Special case for Real Parent
elif action['CreationType'] == 'InjectedThread':
self.set_color(FOREGROUND_GREEN | FOREGROUND_INTENSITY)
self.log.info("!%s> [I] REAL PARENT %s" % \
(tab*(ntabs+2),
action['RealParent']))
self.set_color(FOREGROUND_WHITE | FOREGROUND_INTENSITY)
def write_result_eventlog_hierarchy(self, res):
ph = win32api.GetCurrentProcess()
th = win32security.OpenProcessToken(ph, win32con.TOKEN_READ)
my_sid = win32security.GetTokenInformation(\
th, win32security.TokenUser)[0]
myType = win32evtlog.EVENTLOG_WARNING_TYPE
applicationName = "SysmonCorrelator"
tab = '--'
data = "Not configured, use -l option"
for anomaly in res:
eventID = anomaly['RuleID']
category = 1
descr = [
"RuleName: " + anomaly['Rulename'],
"RuleID: " + str(anomaly['RuleID']),
"Computer: " + anomaly['Computer'],
"Data: ",
]
alert = ""
alert += "\n"
alert += "\tPHE ALERT [%s]: %s\n" % (anomaly['RuleID'],
anomaly['Rulename'])
tab = '--'
ntabs = 0
for process in reversed(anomaly['ProcessChain']):
ntabs +=1
if process.acciones['1'][0]['Alert']:
alert += "\t!%s> %s (%s) [%s] %s\n" % (
tab*ntabs,
get_action_from_id(1),
process.pid,
process.acciones['1'][0]['ProcessTTL'],
process.cmd)
#process.acciones['1'][0]['Alert'] = False
else:
alert += "\t%s> %s (%s) [%s] %s\n" % (tab*ntabs,
get_action_from_id(1),
process.pid,
process.acciones['1'][0]['ProcessTTL'],
process.cmd)
for action_type in process.acciones:
for action in process.acciones[action_type]:
param = get_default_parameter_from_id(\
int(action_type))
if int(action_type) != 1:
if action['Alert']:
alert += "\t!%s> %s %s\n" %\
(tab*(ntabs+2), \
get_action_from_id(int(action_type)),
action[param])
# Special case for Real Parent
elif action['CreationType'] == 'InjectedThread':
alert += "\t!%s> [I] REAL PARENT %s\n" % \
(tab*(ntabs+2),
action['RealParent'])
descr.append(alert)
self.log.debug("Writing to eventlog: %s" % anomaly)
win32evtlogutil.ReportEvent(applicationName, eventID,
eventCategory=category,
eventType=myType, strings=descr,
data=data, sid=my_sid)
def log_result_baseline(self, pnode, s_actions):
self.log.info("")
self.log.info("BASELINE ENGINE ALERT [%s]: %s" % (pnode.pid,
pnode.ImageFileName ))
self.log.info("--> Process Points: %s" % pnode.points)
self.log.info("--> Parent CL: %s" % pnode.acciones["1"][0]\
["ParentCommandLine"])
for action in s_actions:
if not action.has_key('PointsLeft'):
param = get_action_from_id(int(action['EventType']))
self.log.info("--> %s" % param)
for attr in action:
self.log.info("----> %s: %s" % (attr, action[attr]))
def format_result_baseline(self, pnode, s_actions):
alert_text = '\n'
alert_text += "\tBASELINE ENGINE ALERT [%s]: %s\n" % (pnode.pid,
pnode.ImageFileName )
alert_text += "\t--> Process Points: %s\n" % pnode.points
alert_text += "\t--> Parent CL: %s\n" % pnode.acciones["1"][0]\
["ParentCommandLine"]
for action in s_actions:
if not action.has_key('PointsLeft'):
param = get_action_from_id(int(action['EventType']))
alert_text += "\t--> %s\n" % param
for attr in action:
alert_text += "\t----> %s: %s\n" % (attr, action[attr])
return alert_text
def write_result_eventlog_baseline(self, res):
ph = win32api.GetCurrentProcess()
th = win32security.OpenProcessToken(ph, win32con.TOKEN_READ)
my_sid = win32security.GetTokenInformation(\
th, win32security.TokenUser)[0]
myType = win32evtlog.EVENTLOG_WARNING_TYPE
applicationName = "SysmonCorrelator"
data = "Not configured, use -l option"
eventID = 1
category = 2
descr = ["Engine: Baseline", "Data: "]
descr.append(res)
win32evtlogutil.ReportEvent(applicationName, eventID,
eventCategory=category,
eventType=myType, strings=descr,
data=data, sid=my_sid)
def process_result_hierarchy(self, res):
"""Function for processing results"""
self.write_result_eventlog_hierarchy(res)
self.log_result_hierarchy(res)
def process_result_baseline(self, pnode, s_actions):
"""Function for processing results"""
res = self.format_result_baseline(pnode, s_actions)
self.write_result_eventlog_baseline(res)
self.log_result_baseline(pnode, s_actions)
class bcolors:
HEADER = '\033[95m'
RED = '\033[91m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'