-
Notifications
You must be signed in to change notification settings - Fork 30
/
agent_db.py
376 lines (301 loc) · 15.3 KB
/
agent_db.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
import datetime
from time import time
from wazuh_testing.db_interface import query_wdb
def clean_table(agent_id, table):
"""Delete all table entries of the agent DB using wazuh_db.
Args:
agent_id (str): Agent ID.
table (str): Table from the agent DB.
"""
query_string = f"agent {agent_id} sql DELETE FROM {table}"
query_wdb(query_string)
def update_last_full_scan(last_scan=0, agent_id='000'):
"""Update the last full scan of an agent.
Args:
last_scan (int): Last scan ID. This is compute by casting to int the result of time().
agent_id (str): Agent ID.
"""
query_string = f"agent {agent_id} sql UPDATE vuln_metadata SET LAST_FULL_SCAN={last_scan}"
query_wdb(query_string)
def update_last_partial_scan(last_scan=0, agent_id='000'):
"""Update the last partial scan of an agent.
Args:
last_scan (int): Last scan ID. This is compute by casting to int the result of time().
agent_id (str): Agent ID.
"""
query_string = f"agent {agent_id} sql UPDATE vuln_metadata SET LAST_PARTIAL_SCAN={last_scan}"
query_wdb(query_string)
def insert_hotfix(agent_id='000', scan_id=int(time()), scan_time=datetime.datetime.now().strftime("%Y/%m/%d %H:%M:%S"),
hotfix='000000', checksum='dummychecksum'):
"""Insert a hotfix.
Args:
agent_id (str): Agent ID.
scan_id (int): Last scan ID.
scan_time (str): Scan date ("%Y/%m/%d %H:%M:%S").
hotfix (str): ID of the hotfix value.
checksum (str): Hotfix checksum.
"""
query_string = f"agent {agent_id} sql INSERT INTO sys_hotfixes (scan_id, scan_time, hotfix, checksum) VALUES " \
f"({scan_id}, '{scan_time}', '{hotfix}', '{checksum}')"
query_wdb(query_string)
def insert_os_info(agent_id='000', scan_id=int(time()), scan_time=datetime.datetime.now().strftime("%Y/%m/%d %H:%M:%S"),
hostname='centos8', architecture='x64', os_name='CentOS Linux', os_version='8.4', os_codename='',
os_major='8', os_minor='4', os_patch='', os_build='', os_platform='centos', sysname='Linux',
release='', version='', os_release='', checksum='dummychecksum', os_display_version='', triaged='0',
reference=''):
"""Insert the OS information in the agent database.
Args:
agent_id (str): Agent ID.
scan_id (int): Id of the last scan.
scan_time (str): Date of the scan with this format "%Y/%m/%d %H:%M:%S".
hostname (str): Name of the host.
architecture (str): Architecture of the host.
os_name (str): Complete name of the OS.
os_version (str): Version of the OS.
os_codename (str): OS codename.
os_major (str): Major version of the OS.
os_minor (str): Minor version of the OS.
os_patch (str): Current patch of the OS.
os_build (str): Build id of the OS.
os_platform (str): OS platform.
sysname (str): System name.
release (str): Release of the OS.
version (str): Version of the OS.
os_release (str): Release of the OS.
checksum (str): Checksum of the OS.
os_display_version (str): Os displayed version
triaged (str): Triaged.
reference (str): OS reference.
"""
query_string = f"agent {agent_id} sql INSERT OR REPLACE INTO sys_osinfo (scan_id, scan_time, hostname, " \
'architecture, os_name, os_version, os_codename, os_major, os_minor, os_patch, os_build, ' \
'os_platform, sysname, release, version, os_release, os_display_version, checksum, reference, ' \
f"triaged) VALUES ({scan_id}, '{scan_time}', '{hostname}', '{architecture}', '{os_name}', " \
f"'{os_version}', '{os_codename}', '{os_major}', '{os_minor}', '{os_patch}', '{os_build}', " \
f"'{os_platform}', '{sysname}', '{release}', '{version}', '{os_release}', '{os_display_version}', " \
f"'{checksum}', '{reference}', {triaged})"
query_wdb(query_string)
def insert_package(agent_id='000', scan_id=int(time()), format='rpm', name='custom-package-0',
priority='', section='Unspecified', size=99, vendor='wazuh-mocking', version='1.0.0-1.el7',
architecture='x64', multiarch='', description='Wazuh mocking packages', source='Wazuh QA tests',
location='', triaged='0', install_time=datetime.datetime.now().strftime("%Y/%m/%d %H:%M:%S"),
scan_time=datetime.datetime.now().strftime("%Y/%m/%d %H:%M:%S"), checksum='dummychecksum',
item_id='dummyitemid'):
"""Insert a package in the agent DB.
Args:
agent_id (str): Agent ID.
scan_id (int): Last scan ID.
format (str): Package format (deb, rpm, ...).
name (str): Package name.
priority (str): Released package priority.
section (str): Package section.
size (int): Package size.
vendor (str): Package vendor.
version (str): Package version.
architecture (str): Package architecture.
multiarch (str): Define if a package may be installed in different architectures.
description (str): Package description.
source (str): Package source.
location (str): Package location.
triaged (str): Times that the package has been installed.
install_time (str): Installation timestamp.
scan_time (str): Scan timestamp.
checksum (str): Package checksum.
item_id (str): Package ID.
"""
arguments = locals()
for key, value in arguments.items():
if type(value) is str:
if value != 'NULL':
arguments[key] = f"'{value}'"
query_wdb(f"agent {agent_id} sql INSERT INTO sys_programs (scan_id, scan_time, format, name, priority, section, "
f"size, vendor, install_time, version, architecture, multiarch, source, description, location, triaged,"
f"checksum, item_id) VALUES ({arguments['scan_id']}, {arguments['scan_time']}, {arguments['format']},"
f"{arguments['name']}, {arguments['priority']}, {arguments['section']}, {arguments['size']},"
f"{arguments['vendor']}, {arguments['install_time']}, {arguments['version']},"
f"{arguments['architecture']}, {arguments['multiarch']}, {arguments['source']}, "
f"{arguments['description']}, {arguments['location']}, {arguments['triaged']}, {arguments['checksum']},"
f"{arguments['item_id']})")
def update_sync_info(agent_id='000', component='syscollector-packages', last_attempt=1, last_completion=1,
n_attempts=0, n_completions=0, last_agent_checksum=''):
"""Update the sync_info table of the specified agent for the selected component.
Args:
agent_id (str): Agent ID.
component (str): Name of the component package.
last_attempt (int): Last attempt of query
last_completion (int): Last completion package
n_attempts (int): Number of attempt.
n_completions (int): Number of completion packets.
last_agent_checksum (str): Checksum of the last agent registered.
"""
query_wdb(f"agent {agent_id} sql UPDATE sync_info SET last_attempt = {last_attempt},"
f"last_completion = {last_completion}, n_attempts = {n_attempts}, n_completions = {n_completions},"
f"last_agent_checksum = '{last_agent_checksum}' where component = '{component}'")
def update_package(version, package, agent_id='000'):
"""Update version of installed package in database.
Used to simulate upgrades and downgrades of the package given.
Args:
version (str): Package version.
package (str): Package name.
agent_id (str): Agent ID.
"""
update_query_string = f'agent {agent_id} sql UPDATE sys_programs SET version="{version}" WHERE name="{package}"'
query_wdb(update_query_string)
def delete_package(package, agent_id='000'):
"""Remove package from database.
Used to simulate uninstall of the package given.
Args:
package (str): Package name.
agent_id (str): Agent ID.
"""
delete_query_string = f'agent {agent_id} sql DELETE FROM sys_programs WHERE name="{package}"'
query_wdb(delete_query_string)
def clean_vulnerabilities_inventory(agent_id='000'):
"""Clean the vulnerabilities inventory from database.
Args:
agent_id (str): Agent ID.
"""
clean_query_string = f"agent {agent_id} sql DELETE from vuln_cves"
query_wdb(clean_query_string)
def modify_agent_scan_timestamp(agent_id='000', timestamp=0, full_scan=True):
"""Update the timestamp of the agent scans in the vuln_metadata table.
Args:
agent_id (str): Agent ID.
timestamp (int): Timestamp value to set.
full_scan (bool): True for set LAST_FULL_SCAN or False to set LAST_SCAN.
"""
scan_type = "LAST_FULL_SCAN" if full_scan else "LAST_PARTIAL_SCAN"
query_wdb(f"agent {agent_id} sql UPDATE vuln_metadata SET {scan_type}={timestamp}")
def delete_os_info(agent_id='000'):
"""Delete the sys_osinfo data from a specific agent.
Args:
agent_id (str): Agent ID.
"""
query_wdb(f"agent {agent_id} sql DELETE FROM sys_osinfo")
def update_os_info(agent_id='000', scan_id=int(time()), scan_time=datetime.datetime.now().strftime("%Y/%m/%d %H:%M:%S"),
hostname='centos8', architecture='x64', os_name='CentOS Linux', os_version='8.4', os_codename='',
os_major='8', os_minor='4', os_patch='', os_build='', os_platform='centos', sysname='Linux',
release='', version='', os_release='', checksum='dummychecksum', os_display_version='', triaged='0',
reference=''):
"""Update the sys_osinfo data from a specific agent.
Args:
agent_id (str): Agent ID.
scan_id (int): Id of the last scan.
scan_time (str): Date of the scan with this format "%Y/%m/%d %H:%M:%S".
hostname (str): Name of the host.
architecture (str): Architecture of the host.
os_name (str): Complete name of the OS.
os_version (str): Version of the OS.
os_codename (str): OS codename.
os_major (str): Major version of the OS.
os_minor (str): Minor version of the OS.
os_patch (str): Current patch of the OS.
os_build (str): Build id of the OS.
os_platform (str): OS platform.
sysname (str): System name.
release (str): Release of the OS.
version (str): Version of the OS.
os_release (str): Release of the OS.
checksum (str): Checksum of the OS.
os_display_version (str): Os displayed version
triaged (str): Triaged.
reference (str): OS reference.
"""
delete_os_info(agent_id)
insert_os_info(**locals())
def clean_sys_programs(agent_id='000'):
"""Clean all the agent packages data from the DB
Args:
agent_id (str): Agent ID.
"""
clean_table(agent_id, 'sys_programs')
def get_vulnerability_status(agent_id='000', package=''):
"""Check the status of a vulnerability in the agent database table.
Args:
agent_id (str): Agent ID.
package (str): Package to be checked.
"""
query = f"agent {agent_id} sql SELECT status FROM vuln_cves WHERE name = '{package}'"
result = query_wdb(query)[0]['status']
return result
def get_packages_number(agent_id='000', package=''):
"""Check the number of packages in the agent database table.
Args:
agent_id (str): Agent ID.
package (str): Package to be checked.
"""
query = f"agent {agent_id} sql SELECT count(*) FROM sys_programs WHERE name = '{package}'"
result = query_wdb(query)[0]['count(*)']
return result
def get_vulnerability_inventory_data(agent_id='000', name=None, status=None, cve=None, version=None, type=None,
architecture=None, severity=None, cvss2_score=None, cvss3_score=None):
"""Get the vulnerability inventory data according to the specified parameters.
Args:
agent_id (str): Agent ID.
name (str): Vulnerability name.
status (str): Vulnerability status.
cve (str): Vulnerability CVE.
version (str): Version.
type (str): Vulnerability type.
architecture (str): Architecture.
severity (str): Vulnerability severity.
cvss2_score (float): CVSS2 score.
cvss3_score (float): CVSS3 score
Returns:
list(dict): Data in the DB.
"""
# Build a dictionary with local variables
query_parameters = locals()
# Remove non query parameters
del query_parameters['agent_id']
# Define the initial query string
query = f"agent {agent_id} sql SELECT * FROM vuln_cves"
# Build the query string according to the specified parameters
first_parameter = True
for item, value in query_parameters.items():
if value is not None:
formated_value = f"'{value}'" if isinstance(value, str) else value
if first_parameter:
query += f" WHERE {item}={formated_value}"
first_parameter = False
else:
query += f" AND {item}={formated_value}"
return query_wdb(query)
def insert_vulnerability_in_agent_inventory(agent_id='000', name='', version='', architecture='', cve='',
detection_time='', severity='None', cvss2_score=0, cvss3_score=0,
reference='', type='PACKAGE', status='PENDING', external_references='',
condition='', title='', published='', updated=''):
"""Insert a vulnerability in the agent vulnerabilities inventory.
Args:
agent_id (str): Agent ID.
name (str): Vulnerability name.
version (str): Version.
architecture (str): Architecture.
cve (str): Vulnerability CVE.
detection_time (str): Vulnerability detection time.
severity (str): Vulnerability severity.
cvss2_score (float): CVSS2 score.
cvss3_score (float): CVSS3 score
reference (str): Vulnerability reference.
type (str): Vulnerability type.
status (str): Vulnerability status.
external_references (str): Vulnerability external reference.
condition (str): Vulnerability condition.
title (str): Vulnerability title.
published (str): Vulnerability published.
updated (str): Vulnerability updated.
"""
query_wdb(f"agent {agent_id} sql INSERT OR REPLACE INTO vuln_cves (name, version, architecture, cve, "
f"detection_time, severity, cvss2_score, cvss3_score, reference, type, status, external_references,"
f" condition, title, published, updated) VALUES ('{name}', '{version}', '{architecture}', '{cve}', "
f"'{detection_time}', '{severity}', {cvss2_score}, {cvss3_score},'{reference}', '{type}', '{status}', "
f"'{external_references}', '{condition}', '{title}', '{published}', '{updated}')")
def get_triaged_value_from_inventory(package_name, agent_id='000'):
"""Check the triaged of a vulnerability in the agent database table.
Args:
package_name (str): Package name.
agent_id (str): Agent ID.
"""
query = f"agent {agent_id} sql SELECT triaged FROM sys_programs WHERE name='{package_name}'"
result = query_wdb(query)[0]['triaged']
return result