forked from dropbox/securitybot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery_db.py
More file actions
289 lines (228 loc) · 8.92 KB
/
Copy pathquery_db.py
File metadata and controls
289 lines (228 loc) · 8.92 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
'''
A simple script to allow querying the securitybot DB and
viewing recent alerts, mostly for debugging purposes.
'''
import argparse
import re
from securitybot.sql import SQLEngine
from typing import Any, Dict, List, Sequence
BLACKLIST_QUERY = '''
SELECT ldap
FROM blacklist
'''
IGNORED_QUERY = '''
SELECT ldap, title, reason, until
FROM ignored
'''
IGNORED_FIELDS = ['ldap', 'title', 'reason', 'until']
MAIN_QUERY = '''
SELECT HEX(alerts.hash),
title,
ldap,
reason,
description,
splunk_url,
comment,
performed,
authenticated,
status,
event_time
FROM alerts
JOIN user_responses ON alerts.hash = user_responses.hash
JOIN alert_status ON alerts.hash = alert_status.hash
'''
QUERY_FIELDS = ['hash',
'title',
'ldap',
'reason',
'description',
'splunk_url',
'comment',
'performed',
'authenticated',
'status',
'event_time']
STATUS_WHERE = 'status = %s'
PERFORMED_WHERE = 'performed = %s'
TITLE_WHERE = 'title IN ({0})'
ORDER_BY = 'ORDER BY {0}' # wow
BEFORE = 'event_time <= DATE_ADD(NOW(), INTERVAL %s HOUR)'
AFTER = 'event_time >= DATE_ADD(NOW(), INTERVAL %s HOUR)'
HAS_WHERE = False
def build_in(num_titles):
# type: (int) -> str
return TITLE_WHERE.format(','.join(['%s' for _ in range(num_titles)]))
LIMIT = 'LIMIT %s'
def init():
# type: () -> None
SQLEngine('localhost', 'root', '', 'securitybot')
def main(args):
# type: (Any) -> None
if args.blacklist:
fields, matrix = blacklist(args)
elif args.ignored:
fields, matrix = ignored(args)
else:
fields, matrix = alerts(args)
pretty_print(fields, matrix)
def blacklist(args):
# type: (Any) -> Sequence[Any]
fields = ['ldap']
results = SQLEngine.execute(BLACKLIST_QUERY)
return fields, [list(row) for row in results]
def ignored(alerts):
# type: (Any) -> Sequence[Any]
results = SQLEngine.execute(IGNORED_QUERY)
return IGNORED_FIELDS, [list(row) for row in results]
def alerts(args):
# type: (Any) -> Sequence[Any]
params = [] # type: List[Any]
query = MAIN_QUERY
# Prepare for possible limited status
if args.status is not None:
query += build_where(STATUS_WHERE)
params += args.status
# Prepare for possible limited performed boolean
if args.performed is not None:
query += build_where(PERFORMED_WHERE)
params += args.performed
# Prepare for possible title restrictions
if args.titles is not None:
query += build_where(build_in(len(args.titles)))
params.extend(args.titles)
# Add time bounding
if args.before is not None:
query += build_where(BEFORE)
params += [parse_time(args.before[0])]
if args.after is not None:
query += build_where(AFTER)
params += [parse_time(args.after[0])]
# Append limit restriction and order by
query += build_order_by(args.order[0]) + '\n'
query += LIMIT
params += args.limit
# Perform query
raw_results = SQLEngine.execute(query, params)
results = build_query_dict(raw_results)
to_remove = [] # type: List[str]
# Set extra fields to remove
if args.drop is not None:
to_remove.extend(args.drop)
# Remove hashes if not specified
if not args.hash:
to_remove.append('hash')
# Remove status if one was specified earlier
if args.status is not None:
to_remove.append('status')
# Remove time if not specified
if not args.time:
to_remove.append('event_time')
# Remove URL if not specified
if not args.url:
to_remove.append('splunk_url')
# Anonymize if specified
if args.anon:
to_remove.append('ldap')
# Remove columns
for row in results:
for col in to_remove:
row.pop(col, None)
# Grab list of fields and convert to list of lists
fields = [field for field in QUERY_FIELDS if field not in to_remove]
matrix = [[row[field] for field in fields] for row in results]
return fields, matrix
def build_where(condition):
# type: (str) -> str
'''
Builds another part of a where clause depending on whether any other clauses
have been used yet. Inspects global HAS_WHERE and adds either a WHERE or AND.
'''
global HAS_WHERE
s = ''
if HAS_WHERE:
s += 'AND'
else:
HAS_WHERE = True
s += 'WHERE'
return '{0} {1}\n'.format(s, condition)
def build_order_by(order):
# type (str) -> str
if order in ['event_time', 'ldap', 'title']:
formatted = ORDER_BY.format(order)
if order == 'event_time':
formatted += ' DESC'
return formatted
raise ValueError('{0} is an invalid column to order on.'.format(order))
TIME_REGEX = re.compile(r'(-?[0-9]+)h', flags=re.IGNORECASE)
def parse_time(time):
# type: (str) -> int
'''
Parses a -Xh string to an int.
Within the code, the fact that it's negative is actually optional, but I'd rather
not directly expose that fact.
'''
m = TIME_REGEX.match(time)
if m is None:
raise ValueError('{0} is an invalid time.'.format(time))
return int(m.group(1))
def build_query_dict(results):
# type: (Sequence[Sequence[Any]]) -> List[Dict[str, Any]]
'''Builds a list of dictionaries from the results of a query.'''
return [{field: value for field, value in zip(QUERY_FIELDS, row)} for row in results]
def pretty_print(fields, matrix):
# type: (List[str], List[List[Any]]) -> None
'''Pretty prints a matrix of data.'''
contents = [fields] + matrix
contents = [[str(i) for i in row] for row in contents]
# Pretty print rows
# Find maximum length for each column in the context matrix
lens = [max([len(item) for item in col]) for col in zip(*contents)]
# Prepare formatting string for column sizes
fmt = ' | '.join('{{:{}}}'.format(x) for x in lens)
# Apply formatting to each row in the context string
table = [fmt.format(*row) for row in contents]
# Add header separator
table.insert(1, '-' * len(table[0]))
# Join lines with newline
result = '\n'.join(table)
print result
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Explore the Securitybot DB')
# Which table to query on -- defaults to alerts
parser.add_argument('--blacklist', dest='blacklist', action='store_true',
help='Rather than query alerts, displays the blacklist.')
parser.add_argument('--ignored', dest='ignored', action='store_true',
help='Rather than query alerts, displays currently ignored alerts.')
# Alert arguments
parser.add_argument('--titles', dest='titles', type=str, nargs='+',
help='One or more titles of alerts to grab.')
parser.add_argument('-o', '--order', dest='order', type=str, default=['event_time'], nargs=1,
help='Name of column to order by. Must be one of event_time, ldap, title. '+
'Defaults to event_time.')
parser.add_argument('-s', '--status', dest='status', type=int, nargs=1,
help='The status of the alerts to return. ' +
'0 is new, 1 is in progress, 2 is closed.')
parser.add_argument('-p', '--performed', dest='performed', type=int, nargs=1,
help='0 to select alerts that the user did not perform, 1 otherwise.')
parser.add_argument('-l', '--limit', dest='limit', type=int, default=[25], nargs=1,
help='The maximum number of results to return. Defaults to 25.')
parser.add_argument('--hash', dest='hash', action='store_true',
help='If present, will display hash of alert.')
parser.add_argument('-t', '--time', dest='time', action='store_true',
help='If present, will output time at which alert was first logged.')
parser.add_argument('-u', '--url', dest='url', action='store_true',
help='If present, will output Splunk URL of alert\'s saved search.')
parser.add_argument('-a', '--anon', dest='anon', action='store_true',
help='If present, will anonymize output.')
parser.add_argument('--drop', dest='drop', type=str, nargs='+',
help='One or more extra fields to drop.')
# Time bounding
parser.add_argument('--after', dest='after', type=str, nargs=1,
help='Time range all alerts must be after in negative hours, e.g. -6h. ' +
'Note: you may have to use --after=-6h.')
parser.add_argument('--before', dest='before', type=str, nargs=1,
help='Time range all alerts must be before in negative hours, e.g. -2h. ' +
'Note: you may have to use --before=-2h.')
args = parser.parse_args()
init()
main(args)