Skip to content

Commit 83f3f8f

Browse files
Mikaayensongithub-actions[bot]
authored andcommitted
Update cli documentation for search-alerts (#2051)
* Add cli documentation for search-alerts and table fields (cherry picked from commit 4ef1a1a)
1 parent 1f4722b commit 83f3f8f

File tree

3 files changed

+115
-26
lines changed

3 files changed

+115
-26
lines changed

CLI.md

Lines changed: 104 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
# Command Line Interface (CLI)
22

3-
This covers more advanced CLI use cases and workflows. To [get started](README.md#getting-started) with the CLI, reference
4-
the [README](README.md). Basic use of the CLI such as [creating a rule](CONTRIBUTING.md#creating-a-rule-with-the-cli) or
3+
This covers more advanced CLI use cases and workflows. To [get started](README.md#getting-started) with the CLI, reference
4+
the [README](README.md). Basic use of the CLI such as [creating a rule](CONTRIBUTING.md#creating-a-rule-with-the-cli) or
55
[testing](CONTRIBUTING.md#testing-a-rule-with-the-cli) are referenced in the [contribution guide](CONTRIBUTING.md).
66

77

88
## Using a config file or environment variables
99

10-
CLI commands which are tied to Kibana and Elasticsearch are capable of parsing auth-related keyword args from a config
11-
file or environment variables.
10+
CLI commands which are tied to Kibana and Elasticsearch are capable of parsing auth-related keyword args from a config
11+
file or environment variables.
1212

13-
If a value is set in multiple places, such as config file and environment variable, the order of precedence will be as
13+
If a value is set in multiple places, such as config file and environment variable, the order of precedence will be as
1414
follows:
1515
* explicitly passed args (such as `--user joe`)
1616
* environment variables
@@ -35,10 +35,10 @@ EX: `DR_USER=joe`
3535

3636
## Importing rules into the repo
3737

38-
You can import rules into the repo using the `create-rule` or `import-rules` commands. Both of these commands will
39-
require that the rules are schema-compliant and able to pass full validation. The biggest benefit to using these
40-
commands is that they will strip[*](#note) additional fields[**](#note-2) and prompt for missing required
41-
fields.
38+
You can import rules into the repo using the `create-rule` or `import-rules` commands. Both of these commands will
39+
require that the rules are schema-compliant and able to pass full validation. The biggest benefit to using these
40+
commands is that they will strip[*](#note) additional fields[**](#note-2) and prompt for missing required
41+
fields.
4242

4343
Alternatively, you can manually place rule files in the directory and run tests to validate as well.
4444

@@ -81,10 +81,10 @@ Options:
8181
```
8282

8383
The primary advantage of using this command is the ability to import multiple rules at once. Multiple rule paths can be
84-
specified explicitly with unlimited arguments, recursively within a directory using `-d/--directory`[*](#note-3), or
84+
specified explicitly with unlimited arguments, recursively within a directory using `-d/--directory`[*](#note-3), or
8585
a combination of both.
8686

87-
In addition to the formats mentioned using `create-rule`, this will also accept an `.ndjson`/`jsonl` file
87+
In addition to the formats mentioned using `create-rule`, this will also accept an `.ndjson`/`jsonl` file
8888
containing multiple rules (as would be the case with a bulk export).
8989

9090
This will also strip additional fields and prompt for missing required fields.
@@ -103,6 +103,7 @@ defined, otherwise arguments should be passed to the sub-command as:
103103

104104
`python -m detection-rules kibana -u <username> -p <password> upload-rule <...>`
105105

106+
106107
```console
107108
python -m detection_rules es -h
108109

@@ -122,30 +123,111 @@ Commands:
122123
collect-events Collect events from Elasticsearch.
123124
```
124125

126+
Providers are the name that Elastic Cloud uses to configure authentication in Kibana. When we create deployment, Elastic Cloud configures two providers by default: basic/cloud-basic and saml/cloud-saml (for SSO).
127+
125128
```console
126129
python -m detection_rules kibana -h
127130

131+
█▀▀▄ ▄▄▄ ▄▄▄ ▄▄▄ ▄▄▄ ▄▄▄ ▄▄▄ ▄▄▄ ▄ ▄ █▀▀▄ ▄ ▄ ▄ ▄▄▄ ▄▄▄
132+
█ █ █▄▄ █ █▄▄ █ █ █ █ █ █▀▄ █ █▄▄▀ █ █ █ █▄▄ █▄▄
133+
█▄▄▀ █▄▄ █ █▄▄ █▄▄ █ ▄█▄ █▄█ █ ▀▄█ █ ▀▄ █▄▄█ █▄▄ █▄▄ ▄▄█
134+
128135
Usage: detection_rules kibana [OPTIONS] COMMAND [ARGS]...
129136

130137
Commands for integrating with Kibana.
131138

132139
Options:
140+
--ignore-ssl-errors TEXT
133141
--space TEXT Kibana space
134-
-kp, --kibana-password TEXT
142+
--provider-name TEXT For cloud deployments, Elastic Cloud configures
143+
two providers by default: cloud-basic and
144+
cloud-saml (for SSO)
145+
--provider-type TEXT For cloud deployments, Elastic Cloud configures
146+
two providers by default: basic and saml (for
147+
SSO)
135148
-ku, --kibana-user TEXT
136-
--cloud-id TEXT
137-
-k, --kibana-url TEXT
149+
--kibana-url TEXT
150+
-kp, --kibana-password TEXT
151+
-kc, --kibana-cookie TEXT Cookie from an authed session
152+
--cloud-id TEXT ID of the cloud instance. Defaults the cloud
153+
provider to cloud-basic if this option is
154+
supplied
138155
-h, --help Show this message and exit.
139156

140157
Commands:
141-
upload-rule Upload a list of rule .toml files to Kibana.
158+
search-alerts Search detection engine alerts with KQL.
159+
upload-rule Upload a list of rule .toml files to Kibana.
160+
```
161+
162+
## Searching Kibana for Alerts
163+
164+
Alerts stored in Kibana can be quickly be identified by searching with the `search-alerts` command.
165+
166+
167+
```console
168+
python -m detection_rules kibana search-alerts -h
169+
170+
Kibana client:
171+
Options:
172+
--ignore-ssl-errors TEXT
173+
--space TEXT Kibana space
174+
--provider-name TEXT For cloud deployments, Elastic Cloud configures
175+
two providers by default: cloud-basic and
176+
cloud-saml (for SSO)
177+
--provider-type TEXT For cloud deployments, Elastic Cloud configures
178+
two providers by default: basic and saml (for
179+
SSO)
180+
-ku, --kibana-user TEXT
181+
--kibana-url TEXT
182+
-kp, --kibana-password TEXT
183+
-kc, --kibana-cookie TEXT Cookie from an authed session
184+
--cloud-id TEXT ID of the cloud instance. Defaults the cloud
185+
provider to cloud-basic if this option is
186+
supplied
187+
188+
Usage: detection_rules kibana search-alerts [OPTIONS] [QUERY]
189+
190+
Search detection engine alerts with KQL.
191+
192+
Options:
193+
-d, --date-range <TEXT TEXT>...
194+
Date range to scope search
195+
-c, --columns TEXT Columns to display in table
196+
-e, --extend If columns are specified, extend the
197+
original columns
198+
-h, --help Show this message and exit.
142199
```
143200

201+
Running the following command will print out a table showing any alerts that have been generated recently.
202+
`python3 -m detection_rules kibana --provider-name cloud-basic --kibana-url <url> --kibana-user <username> --kibana-password <password> search-alerts`
144203

204+
```console
205+
206+
█▀▀▄ ▄▄▄ ▄▄▄ ▄▄▄ ▄▄▄ ▄▄▄ ▄▄▄ ▄▄▄ ▄ ▄ █▀▀▄ ▄ ▄ ▄ ▄▄▄ ▄▄▄
207+
█ █ █▄▄ █ █▄▄ █ █ █ █ █ █▀▄ █ █▄▄▀ █ █ █ █▄▄ █▄▄
208+
█▄▄▀ █▄▄ █ █▄▄ █▄▄ █ ▄█▄ █▄█ █ ▀▄█ █ ▀▄ █▄▄█ █▄▄ █▄▄ ▄▄█
209+
210+
===================================================================================================================================
211+
host rule
212+
hostname name @timestamp
213+
===================================================================================================================================
214+
stryker-malwares-MacBook-Pro.local Sudo Heap-Based Buffer Overflow Attempt 2022-06-21T14:08:34.288Z
215+
stryker-malwares-MacBook-Pro.local Suspicious Automator Workflows Execution 2022-06-21T13:58:30.857Z
216+
stryker-malwares-MacBook-Pro.local Privilege Escalation Enumeration via LinPEAS 2022-06-21T13:33:18.218Z
217+
stryker-malwares-MacBook-Pro.local Privilege Escalation Enumeration via LinPEAS 2022-06-21T13:28:14.685Z
218+
stryker-malwares-MacBook-Pro.local Potential Reverse Shell Activity via Terminal 2022-06-21T12:53:00.234Z
219+
stryker-malwares-MacBook-Pro.local Potential Reverse Shell Activity via Terminal 2022-06-21T12:53:00.237Z
220+
stryker-malwares-MacBook-Pro.local Potential Kerberos Attack via Bifrost 2022-06-20T20:33:53.810Z
221+
stryker-malwares-MacBook-Pro.local Potential Kerberos Attack via Bifrost 2022-06-20T20:33:53.813Z
222+
stryker-malwares-MacBook-Pro.local Potential Privilege Escalation via Root Crontab File Modification 2022-06-20T20:23:50.557Z
223+
stryker-malwares-MacBook-Pro.local Download and Execution of JavaScript Payload 2022-06-20T20:18:46.211Z
224+
===================================================================================================================================
225+
226+
```
145227
## Uploading rules to Kibana
146228

147-
Toml formatted rule files can be uploaded as custom rules using the `kibana upload-rule` command. To upload more than one
148-
file, specify multiple files at a time as individual args. This command is meant to support uploading and testing of
229+
Toml formatted rule files can be uploaded as custom rules using the `kibana upload-rule` command. To upload more than one
230+
file, specify multiple files at a time as individual args. This command is meant to support uploading and testing of
149231
rules and is not intended for production use in its current state.
150232

151233
```console
@@ -172,7 +254,7 @@ Options:
172254
(detection-rules-build) (base) ➜ detection-rules git:(rule-loader) ✗
173255
```
174256

175-
Alternatively, rules can be exported into a consolidated ndjson file which can be imported in the Kibana security app
257+
Alternatively, rules can be exported into a consolidated ndjson file which can be imported in the Kibana security app
176258
directly.
177259

178260
```console
@@ -195,13 +277,13 @@ Options:
195277
-h, --help Show this message and exit.
196278
```
197279

198-
_*To load a custom rule, the proper index must be setup first. The simplest way to do this is to click
280+
_*To load a custom rule, the proper index must be setup first. The simplest way to do this is to click
199281
the `Load prebuilt detection rules and timeline templates` button on the `detections` page in the Kibana security app._
200282

201283

202284
## Converting between JSON and TOML
203285

204-
[Importing rules](#importing-rules-into-the-repo) will convert from any supported format to toml. Additionally, the
286+
[Importing rules](#importing-rules-into-the-repo) will convert from any supported format to toml. Additionally, the
205287
command `view-rule` will also allow you to view a converted rule without importing it by specifying the `--rule-format` flag.
206288

207289
To view a rule in JSON format, you can also use the `view-rule` command with the `--api-format` flag, which is the default.
@@ -215,7 +297,7 @@ white space stripped, normalized, sorted, and indented, prior to their json conv
215297
table is also stripped out, as this is meant to be used only in the context of this repository and not in Kibana..
216298

217299
Additionally, the `version` of the rule is added to the file prior to exporting it. This is done to restrict version bumps
218-
to occur intentionally right before we create a release. Versions are auto-incremented based on detected changes in
300+
to occur intentionally right before we create a release. Versions are auto-incremented based on detected changes in
219301
rules. This is based on the hash of the rule in the following format:
220302
* sorted json
221303
* serialized
@@ -229,5 +311,5 @@ As a result, all cases where rules are shown or converted to JSON are not just s
229311
Most of the CLI errors will print a concise, user friendly error. To enable debug mode and see full error stacktraces,
230312
you can define `"debug": true` in your config file, or run `python -m detection-rules -d <commands...>`.
231313

232-
Precedence goes to the flag over the config file, so if debug is enabled in your config and you run
314+
Precedence goes to the flag over the config file, so if debug is enabled in your config and you run
233315
`python -m detection-rules --no-debug`, debugging will be disabled.

detection_rules/kbwrap.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,11 @@ def search_alerts(ctx, query, date_range, columns, extend):
9696
with kibana:
9797
alerts = [a['_source'] for a in Signal.search({'query': kql_query})['hits']['hits']]
9898

99-
table_columns = ['host.hostname', 'signal.rule.name', 'signal.status', 'signal.original_time']
99+
table_columns = ['host.hostname', 'rule.name', '@timestamp']
100+
101+
# check for events with nested signal fields
102+
if alerts and 'signal' in alerts[0]:
103+
table_columns = ['host.hostname', 'signal.rule.name', 'signal.status', 'signal.original_time']
100104
if columns:
101105
columns = list(columns)
102106
table_columns = table_columns + columns if extend else columns

detection_rules/misc.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -338,14 +338,17 @@ def get_kibana_client(cloud_id, kibana_url, kibana_user, kibana_password, kibana
338338

339339
client_options = {
340340
'kibana': {
341-
'cloud_id': click.Option(['--cloud-id'], default=getdefault('cloud_id')),
341+
'cloud_id': click.Option(['--cloud-id'], default=getdefault('cloud_id'),
342+
help="ID of the cloud instance."),
342343
'kibana_cookie': click.Option(['--kibana-cookie', '-kc'], default=getdefault('kibana_cookie'),
343344
help='Cookie from an authed session'),
344345
'kibana_password': click.Option(['--kibana-password', '-kp'], default=getdefault('kibana_password')),
345346
'kibana_url': click.Option(['--kibana-url'], default=getdefault('kibana_url')),
346347
'kibana_user': click.Option(['--kibana-user', '-ku'], default=getdefault('kibana_user')),
347-
'provider_type': click.Option(['--provider-type'], default=getdefault('provider_type')),
348-
'provider_name': click.Option(['--provider-name'], default=getdefault('provider_name')),
348+
'provider_type': click.Option(['--provider-type'], default=getdefault('provider_type'),
349+
help="Elastic Cloud providers: basic and saml (for SSO)"),
350+
'provider_name': click.Option(['--provider-name'], default=getdefault('provider_name'),
351+
help="Elastic Cloud providers: cloud-basic and cloud-saml (for SSO)"),
349352
'space': click.Option(['--space'], default=None, help='Kibana space'),
350353
'ignore_ssl_errors': click.Option(['--ignore-ssl-errors'], default=getdefault('ignore_ssl_errors'))
351354
},

0 commit comments

Comments
 (0)