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 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
|
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Tests for the tag_linux.txt tagging file."""
import unittest
from plaso.containers import events
from plaso.lib import definitions
from plaso.parsers import fish_history
from plaso.parsers import utmp
from plaso.parsers.jsonl_plugins import docker_layer_config
from plaso.parsers.text_plugins import bash_history
from plaso.parsers.text_plugins import dpkg
from plaso.parsers.text_plugins import selinux
from plaso.parsers.text_plugins import syslog
from plaso.parsers.text_plugins import zsh_extended_history
from tests.data import test_lib
class LinuxTaggingFileTest(test_lib.TaggingFileTestCase):
"""Tests the tag_linux.txt tagging file.
In the tests below the EventData classes are used to catch failing tagging
rules in case event data types are renamed.
"""
_TAG_FILE = 'tag_linux.txt'
def testRuleApplicationExecution(self):
"""Tests the application_execution tagging rule."""
# Test: data_type is 'bash:history:command'
attribute_values_per_name = {}
self._CheckTaggingRule(
bash_history.BashHistoryEventData, attribute_values_per_name,
['application_execution'])
# Test: data_type is 'docker:layer:configuration'
attribute_values_per_name = {}
self._CheckTaggingRule(
docker_layer_config.DockerLayerConfigurationEventData,
attribute_values_per_name, ['application_execution'])
# Test: data_type is 'fish:history:command'
attribute_values_per_name = {}
self._CheckTaggingRule(
fish_history.FishHistoryEventData, attribute_values_per_name,
['application_execution'])
# Test: data_type is 'selinux:line' AND (audit_type is 'EXECVE' OR
# audit_type is 'USER_CMD')
attribute_values_per_name = {
'audit_type': ['EXECVE', 'USER_CMD']}
self._CheckTaggingRule(
selinux.SELinuxLogEventData, attribute_values_per_name,
['application_execution'])
# Test: data_type is 'shell:zsh:history'
attribute_values_per_name = {}
self._CheckTaggingRule(
zsh_extended_history.ZshHistoryEventData, attribute_values_per_name,
['application_execution'])
# Test: data_type is 'syslog:cron:task_run'
attribute_values_per_name = {}
self._CheckTaggingRule(
syslog.SyslogCronTaskRunEventData, attribute_values_per_name,
['application_execution'])
# Test: reporter is 'sudo' AND body contains 'COMMAND='
attribute_values_per_name = {
'body': ['test if my COMMAND=bogus'],
'reporter': ['sudo']}
self._CheckTaggingRule(
syslog.SyslogLineEventData, attribute_values_per_name,
['application_execution'])
# Test: reporter is 'CROND' AND body contains 'CMD'
attribute_values_per_name = {
'body': ['test if my CMD bogus'],
'reporter': ['CROND']}
self._CheckTaggingRule(
syslog.SyslogLineEventData, attribute_values_per_name,
['application_execution'])
def testRuleLogin(self):
"""Tests the login tagging rule."""
# Test: data_type is 'linux:utmp:event' AND type == 7
attribute_values_per_name = {
'type': [7]}
self._CheckTaggingRule(
utmp.UtmpEventData, attribute_values_per_name,
['login'])
# Test: data_type is 'selinux:line' AND audit_type is 'LOGIN'
attribute_values_per_name = {
'audit_type': ['LOGIN']}
self._CheckTaggingRule(
selinux.SELinuxLogEventData, attribute_values_per_name,
['login'])
# Test: reporter is 'login' AND (body contains 'logged in' OR
# body contains 'ROOT LOGIN' OR body contains 'session opened')
attribute_values_per_name = {
'body': ['logged in', 'ROOT LOGIN', 'session opened'],
'reporter': ['login']}
self._CheckTaggingRule(
syslog.SyslogLineEventData, attribute_values_per_name,
['login'])
# Test: reporter is 'sshd' AND (body contains 'session opened' OR
# body contains 'Starting session')
attribute_values_per_name = {
'body': ['session opened', 'Starting session'],
'reporter': ['sshd']}
self._CheckTaggingRule(
syslog.SyslogLineEventData, attribute_values_per_name,
['login'])
# Test: reporter is 'dovecot' AND body contains 'imap-login: Login:'
attribute_values_per_name = {
'body': ['imap-login: Login:'],
'reporter': ['dovecot']}
self._CheckTaggingRule(
syslog.SyslogLineEventData, attribute_values_per_name,
['login'])
# Test: reporter is 'postfix/submission/smtpd' AND body contains 'sasl_'
attribute_values_per_name = {
'body': ['sasl_method=PLAIN, sasl_username='],
'reporter': ['postfix/submission/smtpd']}
self._CheckTaggingRule(
syslog.SyslogLineEventData, attribute_values_per_name,
['login'])
def testRuleLoginFailed(self):
"""Tests the login_failed tagging rule."""
# Test: data_type is 'selinux:line' AND audit_type is 'ANOM_LOGIN_FAILURES'
attribute_values_per_name = {
'audit_type': ['ANOM_LOGIN_FAILURES']}
self._CheckTaggingRule(
selinux.SELinuxLogEventData, attribute_values_per_name,
['login_failed'])
# Test: data_type is 'selinux:line' AND audit_type is 'USER_LOGIN' AND
# body contains 'res=failed'
attribute_values_per_name = {
'audit_type': ['USER_LOGIN'],
'body': ['res=failed']}
self._CheckTaggingRule(
selinux.SELinuxLogEventData, attribute_values_per_name,
['login_failed'])
# Test: data_type is 'syslog:line' AND body contains 'pam_tally2'
attribute_values_per_name = {
'body': ['pam_tally2']}
self._CheckTaggingRule(
syslog.SyslogLineEventData, attribute_values_per_name,
['login_failed'])
# Test: (reporter is 'sshd' OR
# reporter is 'login' OR
# reporter is 'postfix/submission/smtpd' OR
# reporter is 'sudo') AND
# body contains 'uthentication fail'
attribute_values_per_name = {
'body': ['authentication failed', 'authentication failure',
'Authentication failure'],
'reporter': ['login', 'postfix/submission/smtpd', 'sshd', 'sudo']}
self._CheckTaggingRule(
syslog.SyslogLineEventData, attribute_values_per_name,
['login_failed'])
# Test: (reporter is 'xscreensaver' or
# reporter is 'login') AND
# body contains 'FAILED LOGIN'
attribute_values_per_name = {
'body': ['FAILED LOGIN'],
'reporter': ['login', 'xscreensaver']}
self._CheckTaggingRule(
syslog.SyslogLineEventData, attribute_values_per_name,
['login_failed'])
# Test: reporter is 'su' AND body contains 'DENIED'
attribute_values_per_name = {
'body': ['DENIED su from'],
'reporter': ['su']}
self._CheckTaggingRule(
syslog.SyslogLineEventData, attribute_values_per_name,
['login_failed'])
# Test: reporter is 'nologin'
attribute_values_per_name = {
'reporter': ['nologin']}
self._CheckTaggingRule(
syslog.SyslogLineEventData, attribute_values_per_name,
['login_failed'])
def testRuleUserAdd(self):
"""Tests the useradd tagging rule."""
# Test: reporter is 'useradd' AND body contains 'new user'
attribute_values_per_name = {
'reporter': ['useradd'],
'body': ['new user']}
self._CheckTaggingRule(
syslog.SyslogLineEventData, attribute_values_per_name,
['useradd'])
# Test: data_type is 'selinux:line' AND audit_type is 'ADD_USER'
attribute_values_per_name = {
'audit_type': ['ADD_USER']}
self._CheckTaggingRule(
selinux.SELinuxLogEventData, attribute_values_per_name,
['useradd'])
def testRuleGroupAdd(self):
"""Tests the groupadd tagging rule."""
# Test: reporter is 'useradd' AND body contains 'new group'
attribute_values_per_name = {
'reporter': ['useradd'],
'body': ['new group']}
self._CheckTaggingRule(
syslog.SyslogLineEventData, attribute_values_per_name,
['groupadd'])
# Test: data_type is 'selinux:line' AND audit_type is 'ADD_GROUP'
attribute_values_per_name = {
'audit_type': ['ADD_GROUP']}
self._CheckTaggingRule(
selinux.SELinuxLogEventData, attribute_values_per_name,
['groupadd'])
# Test: reporter is 'groupadd'
attribute_values_per_name = {
'reporter': ['groupadd']}
self._CheckTaggingRule(
syslog.SyslogLineEventData, attribute_values_per_name,
['groupadd'])
def testRuleUserDel(self):
"""Tests the userdel tagging rule."""
# Test: reporter is 'userdel' AND body contains 'delete user'
attribute_values_per_name = {
'reporter': ['userdel'],
'body': ['delete user']}
self._CheckTaggingRule(
syslog.SyslogLineEventData, attribute_values_per_name,
['userdel'])
# Test: data_type is 'selinux:line' AND audit_type is 'DEL_USER'
attribute_values_per_name = {
'audit_type': ['DEL_USER']}
self._CheckTaggingRule(
selinux.SELinuxLogEventData, attribute_values_per_name,
['userdel'])
def testRuleGroupDel(self):
"""Tests the groupdel tagging rule."""
# Test: reporter is 'userdel' AND body contains 'removed group'
attribute_values_per_name = {
'reporter': ['userdel'],
'body': ['removed group']}
self._CheckTaggingRule(
syslog.SyslogLineEventData, attribute_values_per_name,
['groupdel'])
# Test: data_type is 'selinux:line' AND audit_type is 'DEL_GROUP'
attribute_values_per_name = {
'audit_type': ['DEL_GROUP']}
self._CheckTaggingRule(
selinux.SELinuxLogEventData, attribute_values_per_name,
['groupdel'])
# Test: reporter is 'groupdel'
attribute_values_per_name = {
'reporter': ['groupdel']}
self._CheckTaggingRule(
syslog.SyslogLineEventData, attribute_values_per_name,
['groupdel'])
def testRuleFirewallChange(self):
"""Tests the firewall_change tagging rule."""
# Test: data_type is 'selinux:line' AND audit_type is 'NETFILTER_CFG'
attribute_values_per_name = {
'audit_type': ['NETFILTER_CFG']}
self._CheckTaggingRule(
selinux.SELinuxLogEventData, attribute_values_per_name,
['firewall_change'])
def testRuleLogout(self):
"""Tests the logout tagging rule."""
# Test: data_type is 'linux:utmp:event' AND type == 8 AND terminal != '' AND
# pid != 0
# Cannot use _CheckTaggingRule here because of terminal != ''
event = events.EventObject()
event.timestamp = self._TEST_TIMESTAMP
event.timestamp_desc = definitions.TIME_DESCRIPTION_UNKNOWN
event_data = utmp.UtmpEventData()
event_data.type = 0
event_data.terminal = 'tty1'
event_data.pid = 1
storage_writer = self._TagEvent(event, event_data, None)
self._CheckLabels(storage_writer, [])
event_data.type = 8
event_data.terminal = ''
storage_writer = self._TagEvent(event, event_data, None)
self._CheckLabels(storage_writer, [])
event_data.terminal = 'tty1'
event_data.pid = 0
storage_writer = self._TagEvent(event, event_data, None)
self._CheckLabels(storage_writer, [])
event_data.pid = 1
storage_writer = self._TagEvent(event, event_data, None)
self._CheckLabels(storage_writer, ['logout'])
# Test: reporter is 'login' AND body contains 'session closed'
attribute_values_per_name = {
'body': ['session closed'],
'reporter': ['login']}
self._CheckTaggingRule(
syslog.SyslogLineEventData, attribute_values_per_name, ['logout'])
# Test: reporter is 'sshd' AND (body contains 'session closed' OR
# body contains 'Close session')
attribute_values_per_name = {
'body': ['Close session', 'session closed'],
'reporter': ['sshd']}
self._CheckTaggingRule(
syslog.SyslogLineEventData, attribute_values_per_name, ['logout'])
# Test: reporter is 'systemd-logind' AND body contains 'logged out'
attribute_values_per_name = {
'body': ['logged out'],
'reporter': ['systemd-logind']}
self._CheckTaggingRule(
syslog.SyslogLineEventData, attribute_values_per_name, ['logout'])
# Test: reporter is 'dovecot' AND body contains 'Logged out'
attribute_values_per_name = {
'body': ['Logged out'],
'reporter': ['dovecot']}
self._CheckTaggingRule(
syslog.SyslogLineEventData, attribute_values_per_name, ['logout'])
# Test: data_type is 'selinux:line' AND audit_type is 'USER_LOGOUT'
attribute_values_per_name = {
'audit_type': ['USER_LOGOUT']}
self._CheckTaggingRule(
selinux.SELinuxLogEventData, attribute_values_per_name,
['logout'])
def testRuleSessionStart(self):
"""Tests the session_start tagging rule."""
# Test: reporter is 'systemd-logind' and body contains 'New session'
attribute_values_per_name = {
'body': ['New session'],
'reporter': ['systemd-logind']}
self._CheckTaggingRule(
syslog.SyslogLineEventData, attribute_values_per_name,
['session_start'])
def testRuleSessionStop(self):
"""Tests the session_stop tagging rule."""
# Test: reporter is 'systemd-logind' and body contains 'Removed session'
attribute_values_per_name = {
'body': ['Removed session'],
'reporter': ['systemd-logind']}
self._CheckTaggingRule(
syslog.SyslogLineEventData, attribute_values_per_name,
['session_stop'])
def testRuleBoot(self):
"""Tests the boot tagging rule."""
# Test: data_type is 'linux:utmp:event' AND type == 2 AND
# terminal is 'system boot' AND username is 'reboot'
attribute_values_per_name = {
'terminal': ['system boot'],
'type': [2],
'username': ['reboot']}
self._CheckTaggingRule(
utmp.UtmpEventData, attribute_values_per_name, ['boot'])
# Test: data_type is 'selinux:line' AND audit_type is 'SYSTEM_BOOT'
attribute_values_per_name = {
'audit_type': ['SYSTEM_BOOT']}
self._CheckTaggingRule(
selinux.SELinuxLogEventData, attribute_values_per_name,
['boot'])
def testRuleShutdown(self):
"""Tests the shutdonw tagging rule."""
# Test: data_type is 'linux:utmp:event' AND type == 1 AND
# (terminal is '~~' OR terminal is 'system boot') AND
# username is 'shutdown'
attribute_values_per_name = {
'terminal': ['~~', 'system boot'],
'type': [1],
'username': ['shutdown']}
self._CheckTaggingRule(
utmp.UtmpEventData, attribute_values_per_name, ['shutdown'])
# Test: data_type is 'selinux:line' AND audit_type is 'SYSTEM_SHUTDOWN'
attribute_values_per_name = {
'audit_type': ['SYSTEM_SHUTDOWN']}
self._CheckTaggingRule(
selinux.SELinuxLogEventData, attribute_values_per_name,
['shutdown'])
def testRuleRunlevel(self):
"""Tests the runlevel tagging rule."""
# Test: data_type is 'linux:utmp:event' AND type == 1 AND
# username is 'runlevel'
attribute_values_per_name = {
'type': [1],
'username': ['runlevel']}
self._CheckTaggingRule(
utmp.UtmpEventData, attribute_values_per_name, ['runlevel'])
# Test: data_type is 'selinux:line' AND audit_type is 'SYSTEM_RUNLEVEL'
attribute_values_per_name = {
'audit_type': ['SYSTEM_RUNLEVEL']}
self._CheckTaggingRule(
selinux.SELinuxLogEventData, attribute_values_per_name,
['runlevel'])
def testRuleDeviceConnection(self):
"""Tests the device_connection tagging rule."""
# Test: reporter is 'kernel' AND body contains 'New USB device found'
attribute_values_per_name = {
'body': ['New USB device found'],
'reporter': ['kernel']}
self._CheckTaggingRule(
syslog.SyslogLineEventData, attribute_values_per_name,
['device_connection'])
def testRuleDeviceDisconnection(self):
"""Tests the device_disconnection tagging rule."""
# Test: reporter is 'kernel' AND body contains 'USB disconnect'
attribute_values_per_name = {
'body': ['USB disconnect'],
'reporter': ['kernel']}
self._CheckTaggingRule(
syslog.SyslogLineEventData, attribute_values_per_name,
['device_disconnection'])
def testRuleApplicationInstall(self):
"""Tests the application_install tagging rule."""
# Test: data_type is 'linux:dpkg_log:entry' AND
# body contains 'status installed'
attribute_values_per_name = {
'body': ['status installed']}
self._CheckTaggingRule(
dpkg.DpkgEventData, attribute_values_per_name,
['application_install'])
def testRuleServiceStart(self):
"""Tests the service_start tagging rule."""
# Test: data_type is 'selinux:line' AND audit_type is 'SERVICE_START'
attribute_values_per_name = {
'audit_type': ['SERVICE_START']}
self._CheckTaggingRule(
selinux.SELinuxLogEventData, attribute_values_per_name,
['service_start'])
def testRuleServiceStop(self):
"""Tests the service_stop tagging rule."""
# Test: data_type is 'selinux:line' AND audit_type is 'SERVICE_STOP'
attribute_values_per_name = {
'audit_type': ['SERVICE_STOP']}
self._CheckTaggingRule(
selinux.SELinuxLogEventData, attribute_values_per_name,
['service_stop'])
def testRulePromiscuous(self):
"""Tests the promiscuous tagging rule."""
# Test: data_type is 'selinux:line' AND audit_type is 'ANOM_PROMISCUOUS'
attribute_values_per_name = {
'audit_type': ['ANOM_PROMISCUOUS']}
self._CheckTaggingRule(
selinux.SELinuxLogEventData, attribute_values_per_name,
['promiscuous'])
# Test: reporter is 'kernel' AND body contains 'promiscuous mode'
attribute_values_per_name = {
'body': ['promiscuous mode'],
'reporter': ['kernel']}
self._CheckTaggingRule(
syslog.SyslogLineEventData, attribute_values_per_name,
['promiscuous'])
def testRuleCrach(self):
"""Tests the crash tagging rule."""
# Test: data_type is 'selinux:line' AND audit_type is 'ANOM_ABEND'
attribute_values_per_name = {
'audit_type': ['ANOM_ABEND']}
self._CheckTaggingRule(
selinux.SELinuxLogEventData, attribute_values_per_name, ['crash'])
# Test: reporter is 'kernel' AND body contains 'segfault'
attribute_values_per_name = {
'body': ['segfault'],
'reporter': ['kernel']}
self._CheckTaggingRule(
syslog.SyslogLineEventData, attribute_values_per_name, ['crash'])
if __name__ == '__main__':
unittest.main()
|