|
| 1 | +# This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +# License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +# file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 4 | +# Copyright (c) 2017 Mozilla Corporation |
| 5 | +from .positive_alert_test_case import PositiveAlertTestCase |
| 6 | +from .negative_alert_test_case import NegativeAlertTestCase |
| 7 | + |
| 8 | +from .alert_test_suite import AlertTestSuite |
| 9 | + |
| 10 | + |
| 11 | +class TestAlertLdapBruteforce(AlertTestSuite): |
| 12 | + alert_filename = "ldap_bruteforce" |
| 13 | + # This event is the default positive event that will cause the |
| 14 | + # alert to trigger |
| 15 | + default_event = { |
| 16 | + "_source": { |
| 17 | + "category": "ldap", |
| 18 | + "details": { |
| 19 | + "client": "1.2.3.4", |
| 20 | + "requests": [ |
| 21 | + { |
| 22 | + 'verb': 'BIND', |
| 23 | + 'details': [ |
| 24 | + 'method=128' |
| 25 | + 'dn="mail=jsmith@example.com,o=com,dc=example"', |
| 26 | + ] |
| 27 | + } |
| 28 | + ], |
| 29 | + "server": "ldap.example.com", |
| 30 | + "user": "jsmith@example.com", |
| 31 | + "response": { |
| 32 | + "error": 'LDAP_INVALID_CREDENTIALS', |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + # This alert is the expected result from running this task |
| 39 | + default_alert = { |
| 40 | + "category": "ldap", |
| 41 | + "tags": ["ldap"], |
| 42 | + "severity": "WARNING", |
| 43 | + "summary": "LDAP Bruteforce Attack in Progress against user (jsmith@example.com) from the following source ip(s): 1.2.3.4", |
| 44 | + } |
| 45 | + |
| 46 | + # This alert is the expected result from this task against multiple matching events |
| 47 | + default_alert_aggregated = AlertTestSuite.copy(default_alert) |
| 48 | + default_alert_aggregated[ |
| 49 | + "summary" |
| 50 | + ] = "LDAP Bruteforce Attack in Progress against user (jsmith@example.com) from the following source ip(s): 1.2.3.4" |
| 51 | + |
| 52 | + test_cases = [] |
| 53 | + |
| 54 | + test_cases.append( |
| 55 | + PositiveAlertTestCase( |
| 56 | + description="Positive test with default events and default alert expected", |
| 57 | + events=AlertTestSuite.create_events(default_event, 10), |
| 58 | + expected_alert=default_alert, |
| 59 | + ) |
| 60 | + ) |
| 61 | + |
| 62 | + test_cases.append( |
| 63 | + PositiveAlertTestCase( |
| 64 | + description="Positive test with default events and default alert expected - dedup", |
| 65 | + events=AlertTestSuite.create_events(default_event, 2), |
| 66 | + expected_alert=default_alert, |
| 67 | + ) |
| 68 | + ) |
| 69 | + |
| 70 | + events = AlertTestSuite.create_events(default_event, 10) |
| 71 | + for event in events: |
| 72 | + event["_source"]["details"]["response"]["error"] = "LDAP_SUCCESS" |
| 73 | + test_cases.append( |
| 74 | + NegativeAlertTestCase( |
| 75 | + description="Negative test with default negative event", events=events |
| 76 | + ) |
| 77 | + ) |
| 78 | + |
| 79 | + events = AlertTestSuite.create_events(default_event, 10) |
| 80 | + for event in events: |
| 81 | + event["_source"]["category"] = "bad" |
| 82 | + test_cases.append( |
| 83 | + NegativeAlertTestCase( |
| 84 | + description="Negative test case with events with incorrect category", |
| 85 | + events=events, |
| 86 | + ) |
| 87 | + ) |
| 88 | + |
| 89 | + events = AlertTestSuite.create_events(default_event, 10) |
| 90 | + for event in events: |
| 91 | + event["_source"][ |
| 92 | + "utctimestamp" |
| 93 | + ] = AlertTestSuite.subtract_from_timestamp_lambda({"minutes": 241}) |
| 94 | + event["_source"][ |
| 95 | + "receivedtimestamp" |
| 96 | + ] = AlertTestSuite.subtract_from_timestamp_lambda({"minutes": 241}) |
| 97 | + test_cases.append( |
| 98 | + NegativeAlertTestCase( |
| 99 | + description="Negative test case with old timestamp", events=events |
| 100 | + ) |
| 101 | + ) |
0 commit comments