-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Security Solution] Negative lookback is not handled properly during upgrade #204714
Comments
Pinging @elastic/security-detection-rule-management (Team:Detection Rule Management) |
Pinging @elastic/security-detections-response (Team:Detections and Resp) |
Pinging @elastic/security-solution (Team: SecuritySolution) |
Conversion problem
|
Thanks for the diagram and for raising this issue! We don't handle negative lookback and gap remediation properly at that moment. The main idea of gap remediation - it's to check if there is a gap between Positive lookbackRule interval 10 minutes, lookback 1 minute from: 12:49 So, we see the gap between 12:00 and 12:49 - we then try to remediate that up to 4 intervals (40 minutes), meaning that the rule will also query data from 12:09-12:49. Data from 12:00-12:09 will be reported as a gap. Negative lookbackLet's say the rule had 10 minutes interval and -9 minutes lookback time. Let's say we have a previous run at 12:00 and then rule run at 12:10 from:12:09 So there is a gap (9 minutes) between the last run at 12:00 and 12:09, which we try to remediate. Initially, I assumed that we would try to remediate up to 4 intervals, meaning we would query from 12:00-12:10, and in that case, we would neglect negative lookback. But in reality, other things will happen (x-pack/plugins/security_solution/server/lib/detection_engine/rule_types/utils/utils.ts). So first, we need to figure out how many catchup intervals we will need Then we try to get the exact intervals we need to query, and here is the problem. getCatchupTuples - will calculate those intervals, using Meaning we need to catch up 1 time, and it will return So, it means that our rule will query data from [12:00-12:01, 12:09:12:10] At that moment, you would think that we are reporting the gap as Next, we try to calculate Summary for negative lookback time :
Here is my question to a product at the moment:
|
Let's keep things simple for Rule Customization Milestone 3:
|
I agree with the points in the last comment by @xcrzx . We need to keep the flexibility by supporting negative lookback, and not make unexpected changes. We should keep the default behavior if the values are not set. |
Me @Mikaayenson and DE team discussed negative look-back issue at DE tech time meeting. There are the following outcomes
|
Taking into account prebuilt rules won't have negative look-back in newer package versions and UI currently doesn't support negative look-back is there a reason to to support negative lookbacks? The only way users may set negative look-back is Rule Management CRUD API. |
In this case since we do not intend to have OOTB rules with negative lookback, this use-case becomes a rarity. In case there is additional user feedback we can adjust this later. |
Hey @maximpn @xcrzx @approksiu, I think every point of @xcrzx from #204714 (comment) is important to take into account. 1⃣ The rule upgrade workflow doesn't and shouldn't have a goal to suggest "better" rule parameters to users, such as a schedule that would not be causing gaps. Gap remediation is out of the scope of this specific ticket and Milestone 3 in general. 2⃣ The rule upgrade workflow, both at the API level and in the UI, should not reset or change any values coming from neither current nor target rule versions, to any "better" or "default" values. If a prebuilt rule is valid and can be installed, the workflow should respect and retain its parameters. Same for custom rules - if it's possible to create a rule via API with The fact that a few prebuilt rules were adjusted in elastic/detection-rules#4324 doesn't make a big difference. The case of "My First Rule" was pretty valid: we needed a rule that would be executed infrequently but not querying a large time interval. So this was by design, not sure why we changed it @approksiu. 3⃣ In my opinion, the above means that in the upgrade workflow we should allow not only negative lookbacks, but also Perhaps there's a relatively inexpensive way to do this:
|
…lastic#204317) **Fixes: elastic#202715 **Fixes: elastic#204714 ## Summary This PR makes inconsistent/wrong rule's look-back duration prominent for a user. It falls back to a default 1 minute value in rule upgrade workflow. ## Details ### Negative/wrong `lookback` problem There is a difference between rule schedule value in a saved object and value represented to users - Saved object (and rule management API) has `interval`, `from` and `to` fields representing rule schedule. `interval` shows how often a rule runs in task runner. `from` and `to` stored in date math format like `now-10m` represent a date time range used to fetch source events. Task manager strives to run rules exactly every `interval` but it's not always possible due to multiple reasons like system load and various delays. To avoid any gaps to appear `from` point in time usually stands earlier than current time minus `interval`, for example `interval` is `10 minutes` and `from` is `now-12m` meaning rule will analyze events starting from 12 minutes old. `to` represents the latest point in time source events will be analyzed. - Diffable rule and UI represent rule schedule as `interval` and `lookback`. Where `interval` is the same as above and `lookback` and a time duration before current time minus `interval`. For example `interval` is `10 minutes` and lookback is `2 minutes` it means a rule will analyzing events starting with 12 minutes old until the current moment in time. Literally `interval`, `from` and `to` mean a rule runs every `interval` and analyzes events starting from `from` until `to`. Technically `from` and `to` may not have any correlation with `interval`, for example a rule may analyze one year old events. While it's reasonable for manual rule runs and gap remediation the same approach doesn't work well for usual rule schedule. Transformation between `interval`/`from`/`to` and `interval`/`lookback` works only when `to` is equal the current moment in time i.e. `now`. Rule management APIs allow to set any `from` and `to` values resulting in inconsistent rule schedule. Transformed `interval`/`lookback` value won't represent real time interval used to fetch source events for analysis. On top of that negative `lookback` value may puzzle users on the meaning of the negative sign. ### Prebuilt rules with `interval`/`from`/`to` resulting in negative `lookback` Some prebuilt rules have such `interval`, `from` and `to` field values thatnegative `lookback` is expected, for example `Multiple Okta Sessions Detected for a Single User`. It runs every `60 minutes` but has `from` field set to `now-30m` and `to` equals `now`. In the end we have `lookback` equals `to` - `from` - `interval` = `30 minutes` - `60 minutes` = `-30 minutes`. Our UI doesn't handle negative `lookback` values. It simply discards a negative sign and substitutes the rest for editing. In the case above `30 minutes` will be suggested for editing. Saving the form will result in changing `from` to `now-90m` <img width="1712" alt="image" src="/~https://github.com/user-attachments/assets/05519743-9562-4874-8a73-5596eeccacf2" /> ### Changes in this PR This PR mitigates rule schedule inconsistencies caused by `to` fields not using the current point in time i.e. `now`. The following was done - `DiffableRule`'s `rule_schedule` was changed to have `interval`, `from` and `to` fields instead of `interval` and `lookback` - `_perform` rule upgrade API endpoint was adapted to the new `DIffableRule`'s `rule_schedule` - Rule upgrade flyout calculates and shows `interval` and `lookback` in Diff View, readonly view and field form when `lookback` is non-negative and `to` equals `now` - Rule upgrade flyout shows `interval`, `from` and `to` in Diff View, readonly view and field form when `to` isn't equal `now` or calculated `lookback` is negative - Rule upgrade flyout shows a warning when `to` isn't equal `now` or calculated `lookback` is negative - Rule upgrade flyout's JSON Diff shows `interval` and `lookback` when `lookback` is non-negative and `to` equals `now` and shows `interval`, `from` and `to` in any other case - Rule details page shows `interval`, `from` and `to` in Diff View, readonly view and field form when `to` isn't equal `now` or calculated `lookback` is negative - `maxValue` was added to `ScheduleItemField` to have an ability to restrict input at reasonable values ## Screenshots - Rule upgrade workflow (negative look-back) <img width="2558" alt="Screenshot 2025-01-02 at 13 16 59" src="/~https://github.com/user-attachments/assets/b8bf727f-11ca-424f-892b-b024ba7f847a" /> <img width="2553" alt="Screenshot 2025-01-02 at 13 17 20" src="/~https://github.com/user-attachments/assets/9f751ea4-0ce0-4a23-a3b7-0a16494d957e" /> <img width="2558" alt="Screenshot 2025-01-02 at 13 18 24" src="/~https://github.com/user-attachments/assets/6908ab02-4011-4a6e-85ce-e60d5eac7993" /> - Rule upgrade workflow (positive look-back) <img width="2555" alt="Screenshot 2025-01-02 at 13 19 12" src="/~https://github.com/user-attachments/assets/06208210-c6cd-4842-8aef-6ade5d13bd36" /> <img width="2558" alt="Screenshot 2025-01-02 at 13 25 31" src="/~https://github.com/user-attachments/assets/aed38bb0-ccfb-479a-bb3b-e5442c518e63" /> - JSON view <img width="2559" alt="Screenshot 2025-01-02 at 13 31 37" src="/~https://github.com/user-attachments/assets/07575a81-676f-418e-8b98-48eefe11ab00" /> - Rule details page <img width="2555" alt="Screenshot 2025-01-02 at 13 13 16" src="/~https://github.com/user-attachments/assets/e977b752-9d50-4049-917a-af2e8e3f0dfe" /> <img width="2558" alt="Screenshot 2025-01-02 at 13 14 10" src="/~https://github.com/user-attachments/assets/06d6f477-5730-48ca-a240-b5e7592bf173" /> ## How to test? - Ensure the `prebuiltRulesCustomizationEnabled` feature flag is enabled - Allow internal APIs via adding `server.restrictInternalApis: false` to `kibana.dev.yaml` - Clear Elasticsearch data - Run Elasticsearch and Kibana locally (do not open Kibana in a web browser) - Install an outdated version of the `security_detection_engine` Fleet package ```bash curl -X POST --user elastic:changeme -H 'Content-Type: application/json' -H 'kbn-xsrf: 123' -H "elastic-api-version: 2023-10-31" -d '{"force":true}' http://localhost:5601/kbn/api/fleet/epm/packages/security_detection_engine/8.14.1 ``` - Install prebuilt rules ```bash curl -X POST --user elastic:changeme -H 'Content-Type: application/json' -H 'kbn-xsrf: 123' -H "elastic-api-version: 1" -d '{"mode":"ALL_RULES"}' http://localhost:5601/kbn/internal/detection_engine/prebuilt_rules/installation/_perform ``` - Set "inconsistent" rule schedule for `Suspicious File Creation via Kworker` rule by running a query below ```bash curl -X PATCH --user elastic:changeme -H "Content-Type: application/json" -H "elastic-api-version: 2023-10-31" -H "kbn-xsrf: 123" -d '{"rule_id":"ae343298-97bc-47bc-9ea2-5f2ad831c16e","interval":"10m","from":"now-5m","to":"now-2m"}' http://localhost:5601/kbn/api/detection_engine/rules ``` - Open rule upgrade flyout for `Suspicious File Creation via Kworker` rule --------- Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com> (cherry picked from commit 30bb71a) # Conflicts: # .github/CODEOWNERS
Related to: #203476
Summary
Rules are incorrectly marked as customized immediately after an upgrade due to improper handling of the rule interval field.
Steps to Reproduce
8.14.2
Multiple Okta Sessions Detected for a Single User
rule from the package:8.17.1
Expected Result
The rule is upgraded successfully and is not marked as customized.
Actual Result
The rule is immediately marked as customized in the upgrade response:
Initial Analysis
Preliminary analysis suggests that the rule interval field is not handled correctly during the upgrade. The negative interval value provided in the package (
-1800s
) is replaced with0s
, which likely breaks the rule execution schedule and results in the rule being marked as customized.The text was updated successfully, but these errors were encountered: