Skip to content
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

[SIEM] [Detection Engine] Incorporate large lists to rule execution. #65372

Merged
merged 30 commits into from
May 28, 2020

Conversation

dhurley14
Copy link
Contributor

@dhurley14 dhurley14 commented May 5, 2020

Summary

First run at incorporating lists plugin within detection engine rule execution.
I spent some time refactoring the searchAfterBulkCreate function so that I could easily incorporate the list client into the rule execution flow.

To do:

  • add support for keywords and future list types available in lists plugin (marking this as completed as we are still hammering out the format of the shared json contract for exceptions, but I have a working sample rule with the current contract)
  • utilize list id from the rule params
  • add unit tests for new functionality and update current ones

Getting started:

Add the following to your kibana.yml

# Enable lists feature
xpack.lists.enabled: true
xpack.lists.listIndex: '.lists-{your-name}'
xpack.lists.listItemIndex: '.items-{your-name}'

then...

  • First, set the appropriate env var in order to enable exceptions features export ELASTIC_XPACK_SIEM_LISTS_FEATURE=true and export ELASTIC_XPACK_SIEM_EXCEPTIONS_LISTS=true and start kibana
  • Second, create a list of IP addresses, something that looks like this (DO NOT visit these IP addresses) and import a list of ips from a file named ci-badguys.txt (if you want to name the file something else, you will have to modify the rule json in the next step to point to that). The command should look like this: cd $HOME/kibana/x-pack/plugins/lists/server/scripts && ./import_list_items_by_filename.sh ip ~/ci-badguys.txt
  • Then, from the detection engine scripts folder (cd kibana/x-pack/plugins/siem/server/lib/detection_engine/scripts) run ./post_rule.sh rules/queries/lists/query_with_list_plugin.json

I would also suggest removing the config flags for lists and posting a standard, non exceptions rule using ./post_rule.sh to confirm backwards compatibility without lists plugin enabled.

Checklist

Delete any items that are not applicable to this PR.

For maintainers

@dhurley14 dhurley14 marked this pull request as ready for review May 12, 2020 02:43
@dhurley14 dhurley14 requested review from a team as code owners May 12, 2020 02:43
@dhurley14 dhurley14 self-assigned this May 12, 2020
@dhurley14 dhurley14 changed the title [DRAFT] [SIEM] [Detection Engine] Incorporate large lists to rule execution. [SIEM] [Detection Engine] Incorporate large lists to rule execution. May 12, 2020
@elasticmachine
Copy link
Contributor

Pinging @elastic/siem (Team:SIEM)

@@ -68,6 +69,11 @@ describe('rules_notification_alert_type', () => {
modulesProvider: jest.fn(),
resultsServiceProvider: jest.fn(),
};
const listMock = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is something that would be nice to have as part of the lists plugin itself: https://github.com/elastic/kibana/blob/master/src/core/MIGRATION.md#writing-mocks-for-your-plugin

* First, set the appropriate env var in order to enable exceptions features`export ELASTIC_XPACK_SIEM_LISTS_FEATURE=true` and `export ELASTIC_XPACK_SIEM_EXCEPTIONS_LISTS=true` and start kibana
* Second, import a list of ips from a file called `ci-badguys.txt`. The command should look like this:
`cd $HOME/kibana/x-pack/plugins/lists/server/scripts && ./import_list_items_by_filename.sh ip ~/ci-badguys.txt`
* Then, from the detection engine scripts folder (`cd kibana/x-pack/plugins/siem/server/lib/detection_engine/scripts`) run `./post_rule.sh rules/queries/lists/query_with_list_plugin.json`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 docs!

buildRuleMessage(
`[+] Finished indexing ${result.createdSignalsCount} signals into ${outputIndex}`
)
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 more data for logging

@dhurley14 dhurley14 force-pushed the cp-big-loop branch 3 times, most recently from 0e36a35 to 974c1b9 Compare May 26, 2020 14:44
const res = await filterEventsAgainstList({
logger: mockLogger,
listClient: ({
getListItemByValues: async () => ([] as unknown) as ListItemArraySchema,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need the extra casting you can do:

getListItemByValues: async () => [],

And it will work

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if it's worth putting a listClientMock in the lists plugin? similar to what @rylnd suggested #65372 (comment)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that is a great idea. Doesn't have to be on this PR unless you want to do it. Otherwise I will do it after this PR within lists.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay I'll write up an issue and throw it in the backlog.

…d in the siem plugin and was preventing the registration of the rule alert type. I removed this but once lists is ready for prime time we should consider adding the null check back
…XPACK_SIEM_LISTS_FEATURE env var to true without enabling the lists plugin
…ogic for empty exceptions list, updates types
…n error when exceptionItem is malformed. This will change in the very near future once the new json format for exception lists is incorporated
@dhurley14
Copy link
Contributor Author

jenkins test this

@kibanamachine
Copy link
Contributor

💚 Build Succeeded

History

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

@dhurley14 dhurley14 merged commit 177cda4 into elastic:master May 28, 2020
@dhurley14 dhurley14 deleted the cp-big-loop branch May 28, 2020 19:45
dhurley14 added a commit to dhurley14/kibana that referenced this pull request May 29, 2020
…lastic#65372)

* introduce lists plugin for use by executor

* adds getListClient function on setup

* refactors searchAfterBulkCreate to integrate with the lists plugin so we only generate signals from events not in the list

* fixes type check issues

* fixes unit tests, adds field and other parameters for using lists in executor.

* cleaning up types and exports, updates to match new contracts with lists client from master

* prior to this commit the refactored while loop was doing more search after loops than it needed to and this fixes two bugs in the list filter function where we were returning the wrong count, and we were not accessing the right field on the event

* exception lists are optional

* use exceptions list format, this works with given sample query in scripts

* updates tests and fixes type issues

* updates README doc in detection engine with example for rule with list exception

* adds one test and removes commented out code

* fix sample rule json from 30s to 5m

* fix sample rule json from 30s to 5m

* remove unused import

* more cleanup

* e2e test for prepackaged rules was failing because lists was undefined in the siem plugin and was preventing the registration of the rule alert type. I removed this but once lists is ready for prime time we should consider adding the null check back

* can't reuse the same env var since the tests are setting the ELASTIC_XPACK_SIEM_LISTS_FEATURE env var to true without enabling the lists plugin

* fixes from pr review, still needs more TLC

* exports listspluginsetup type from top-level in lists plugin, fixes logic for empty exceptions list, updates types

* utilize type.is to remove as casting, also do null checks and throw an error when exceptionItem is malformed. This will change in the very near future once the new json format for exception lists is incorporated

* fix type issues after merging master into branch

* update mock

* remove bad null check for ml plugin before registering rule alert type in siem plugin

* prettier linting

* adds test for filter events with list

* pr comments

* adds logic for included vs excluded and updates tests

* update test cases for search after bulk create to default to included for exception lists

* filter out non-list exception items from the loop
dhurley14 added a commit that referenced this pull request May 29, 2020
…tion. (#65372) (#67753)

* introduce lists plugin for use by executor

* adds getListClient function on setup

* refactors searchAfterBulkCreate to integrate with the lists plugin so we only generate signals from events not in the list

* fixes type check issues

* fixes unit tests, adds field and other parameters for using lists in executor.

* cleaning up types and exports, updates to match new contracts with lists client from master

* prior to this commit the refactored while loop was doing more search after loops than it needed to and this fixes two bugs in the list filter function where we were returning the wrong count, and we were not accessing the right field on the event

* exception lists are optional

* use exceptions list format, this works with given sample query in scripts

* updates tests and fixes type issues

* updates README doc in detection engine with example for rule with list exception

* adds one test and removes commented out code

* fix sample rule json from 30s to 5m

* fix sample rule json from 30s to 5m

* remove unused import

* more cleanup

* e2e test for prepackaged rules was failing because lists was undefined in the siem plugin and was preventing the registration of the rule alert type. I removed this but once lists is ready for prime time we should consider adding the null check back

* can't reuse the same env var since the tests are setting the ELASTIC_XPACK_SIEM_LISTS_FEATURE env var to true without enabling the lists plugin

* fixes from pr review, still needs more TLC

* exports listspluginsetup type from top-level in lists plugin, fixes logic for empty exceptions list, updates types

* utilize type.is to remove as casting, also do null checks and throw an error when exceptionItem is malformed. This will change in the very near future once the new json format for exception lists is incorporated

* fix type issues after merging master into branch

* update mock

* remove bad null check for ml plugin before registering rule alert type in siem plugin

* prettier linting

* adds test for filter events with list

* pr comments

* adds logic for included vs excluded and updates tests

* update test cases for search after bulk create to default to included for exception lists

* filter out non-list exception items from the loop
@MindyRS MindyRS added the Team: SecuritySolution Security Solutions Team working on SIEM, Endpoint, Timeline, Resolver, etc. label Sep 23, 2021
@elasticmachine
Copy link
Contributor

Pinging @elastic/security-solution (Team: SecuritySolution)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release_note:enhancement review Team: SecuritySolution Security Solutions Team working on SIEM, Endpoint, Timeline, Resolver, etc. Team:SIEM v7.9.0 v8.0.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants