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

[SecuritySolution][SIEM migrations] Add macros and lookups support in the API #199370

Merged
merged 46 commits into from
Nov 18, 2024

Conversation

semd
Copy link
Contributor

@semd semd commented Nov 7, 2024

Summary

Part of: https://github.com/elastic/security-team/issues/10653

Implements the support for resources (macros and lookup lists) for SIEM rule migrations, including the API, the persistence layer and the retrieval for the LLM agent.

Note

This feature needs siemMigrationsEnabled experimental flag enabled to work. Otherwise, no code related to SIEM migrations is executed.

Schema

The resource object schema is:

RuleMigrationResource:
  _id:
    type: string
    description: The resource id, is generated by hashing the migration_id, type, and name.
  migration_id:
    type: string
    description: The migration id
  type:      
    type: string
    description: The type of the rule migration resource.
    enum:
      - macro
      - list
  name:
    type: string
    description: The resource name identifier.
  content:
    type: string
    description: The resource content value.
  metadata:
    type: object
    description: The resource arbitrary metadata.
  updated_at:
    type: string
    description: The moment of the last update
  updated_by:
    type: string
    description: The user who last updated the resource

2 new routes are now exposed:

  • POST /internal/siem_migrations/rules/{migration_id}/resources -> Creates the resources, the ones that already exist are updated.
  • GET /internal/siem_migrations/rules/{migration_id}/resources -> Retrieves all the stored resources for a given migration

Resources index

A new index is created when the resources need to be stored: .kibana-siem-rule-migrations-resources-[spaceId]
The mapping is the same as the schema.

The RuleMigrationsDataClient has been extended to handle two different kinds of objects now: rules and resources.

Resource identifier

The resourceIdentifier module has been implemented (x-pack/plugins/security_solution/common/siem_migrations/rules/resources/splunk_identifier.ts) to extract the resource (macros or lists) names from the queries. There will be a different implementation for each vendor/query_language.

Resource retriever

The resourceRetriever (x-pack/plugins/security_solution/server/lib/siem_migrations/rules/task/util/rule_resource_retriever.ts) has been implemented to retrieve the resources content taking only an original (splunk) query as input, combining the resourceIdentifier and the resources content stored in the index.
It is used by the LLM agent to obtain the resources content when executing the query translation to ES|QL.

The resourceRetriever implementation is recursive, so we can extract all the nested resources, since macros may contain other resources inside (lists or other macros).

LLM Agent

A new agent call has been added to the translation node. The LLM is asked to replace all the resources in the original query with their content, so we have the query with no macro call or lookup list, everything is inline.
With the replaced query the ES|QL translation is executed as usual.

Example:

Original query:

`linux_auditd` `linux_auditd_normalized_proctitle_process`
| rename host as dest 
| where LIKE (process_exec, "%chown %root%") 
| stats count min(_time) as firstTime max(_time) as lastTime by process_exec proctitle normalized_proctitle_delimiter dest 
| `security_content_ctime(firstTime)` 
| `security_content_ctime(lastTime)`
| `linux_auditd_change_file_owner_to_root_filter`

Resources:

`security_content_ctime(1)` -> convert timeformat="%Y-%m-%dT%H:%M:%S" ctime($field$)
`linux_auditd` -> sourcetype="linux:audit"
`linux_auditd_change_file_owner_to_root_filter` -> search *

Inline query:

sourcetype="linux:audit" `linux_auditd_normalized_proctitle_process`
| rename host as dest
| where LIKE (process_exec, "%chown %root%")
| stats count min(_time) as firstTime max(_time) as lastTime by process_exec proctitle normalized_proctitle_delimiter dest
| convert timeformat="%Y-%m-%dT%H:%M:%S" ctime(firstTime)
| convert timeformat="%Y-%m-%dT%H:%M:%S" ctime(lastTime)
| search *

ES|QL translated query:

FROM logs-*
| WHERE process_exec LIKE "*chown *root*"
| STATS count = COUNT(*), firstTime = MIN(@timestamp), lastTime = MAX(@timestamp) BY process_exec, proctitle, normalized_proctitle_delimiter, host.name
| EVAL firstTime = DATE_FORMAT(firstTime, \"yyyy-MM-dd'T'HH:mm:ss\"), lastTime = DATE_FORMAT(lastTime, \"yyyy-MM-dd'T'HH:mm:ss\")

Considerations:

  • logs-* index pattern is used as a temporary workaround while integrations RAG is being implemented.
  • ECS field conversation has not been implemented yet

semd and others added 30 commits November 6, 2024 19:54
…t --include-path /api/status --include-path /api/alerting/rule/ --include-path /api/alerting/rules --include-path /api/actions --include-path /api/security/role --include-path /api/spaces --include-path /api/fleet --update'
throw error;
});
await this.esClient.update({ index, id: _id, doc, refresh: 'wait_for' }).catch((error) => {
this.logger.error(`Error updating rule migration status to failed: ${error.message}`);
Copy link
Contributor

Choose a reason for hiding this comment

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

side note: it would be great to track telemetry on the UI on our success and failure rates and retry count as well


export const MAX_RECURSION_DEPTH = 10;

export class RuleResourceRetriever {
Copy link
Contributor

Choose a reason for hiding this comment

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

👏🏾

@@ -56,4 +56,34 @@ describe('splResourceIdentifier', () => {
expect(result.macro).toEqual(['macro_one', 'my_lookup_table', 'third_macro']);
expect(result.list).toEqual(['real_lookup_list']);
});

it('should ignore macros or lookup tables inside string literals with double quotes', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

👌🏾 Thanks for adding these!

@@ -5,21 +5,42 @@
* 2.0.
*/

/**
* Important:
Copy link
Contributor

Choose a reason for hiding this comment

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

TIL 💡

});

/**
* Wraps a request handler with a check for the license. If the license is not valid, it will
Copy link
Contributor

Choose a reason for hiding this comment

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

👌🏾 nice

Copy link
Contributor

@michaelolo24 michaelolo24 left a comment

Choose a reason for hiding this comment

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

Thank you for addressing the comments! Excited to see the progress on this work!

@elasticmachine
Copy link
Contributor

💛 Build succeeded, but was flaky

Failed CI Steps

Test Failures

  • [job] [logs] FTR Configs #29 / aiops change point detection attaches change point charts to a case

Metrics [docs]

✅ unchanged

History

cc @semd

@semd semd merged commit 4f3bbe8 into elastic:main Nov 18, 2024
44 checks passed
@kibanamachine
Copy link
Contributor

Starting backport for target branches: 8.18, 8.x

https://github.com/elastic/kibana/actions/runs/11899927531

@kibanamachine
Copy link
Contributor

💔 Some backports could not be created

Status Branch Result
8.18 The branch "8.18" does not exist
8.x

Note: Successful backport PRs will be merged automatically after passing CI.

Manual backport

To create the backport manually run:

node scripts/backport --pr 199370

Questions ?

Please refer to the Backport tool documentation

kibanamachine added a commit that referenced this pull request Nov 18, 2024
…ort in the API (#199370) (#200644)

# Backport

This will backport the following commits from `main` to `8.x`:
- [[SecuritySolution][SIEM migrations] Add macros and lookups support in
the API (#199370)](#199370)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Sergi
Massaneda","email":"sergi.massaneda@elastic.co"},"sourceCommit":{"committedDate":"2024-11-18T19:47:32Z","message":"[SecuritySolution][SIEM
migrations] Add macros and lookups support in the API
(#199370)","sha":"4f3bbe8d30a962ddb4e9cd5c2d207dabaa063ffb","branchLabelMapping":{"^v9.0.0$":"main","^v8.17.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","v9.0.0","Team:Threat
Hunting","backport:prev-minor","v8.18.0"],"title":"[SecuritySolution][SIEM
migrations] Add macros and lookups support in the
API","number":199370,"url":"https://github.com/elastic/kibana/pull/199370","mergeCommit":{"message":"[SecuritySolution][SIEM
migrations] Add macros and lookups support in the API
(#199370)","sha":"4f3bbe8d30a962ddb4e9cd5c2d207dabaa063ffb"}},"sourceBranch":"main","suggestedTargetBranches":["8.18"],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/199370","number":199370,"mergeCommit":{"message":"[SecuritySolution][SIEM
migrations] Add macros and lookups support in the API
(#199370)","sha":"4f3bbe8d30a962ddb4e9cd5c2d207dabaa063ffb"}},{"branch":"8.18","label":"v8.18.0","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Sergi Massaneda <sergi.massaneda@elastic.co>
CAWilson94 pushed a commit to CAWilson94/kibana that referenced this pull request Dec 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport:prev-minor Backport to (8.x) the previous minor version (i.e. one version back from main) release_note:skip Skip the PR/issue when compiling release notes Team:Threat Hunting Security Solution Threat Hunting Team v8.17.0 v8.18.0 v9.0.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants