Skip to content

Commit 66d42cc

Browse files
authored
Feature/remove write and send to (#108)
* remove `write-to` and `send-to` and rename `print` to `search` on alerts/security-data cmds * remove unused loggers * update tests * fix case where exception is printed on KeyboardInterrupt and closed pipe. * update readme and changelog * add TCP examples * remove tests for removed func * remove tests for removed loggers * fix bug from change in latest py42 * add note about external tools for more complex requirements * updated docs
1 parent 292a307 commit 66d42cc

File tree

20 files changed

+375
-1032
lines changed

20 files changed

+375
-1032
lines changed

CHANGELOG.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,23 @@ how a consumer would use the library (e.g. adding unit tests, updating documenta
1313
### Changed
1414

1515
- `-i` (`--incremental`) has been removed, use `-c` (`--use-checkpoint`) with a string name for the checkpoint instead.
16+
1617
- The code42cli has been migrated to the [click](https://click.palletsprojects.com) framework. This brings:
17-
- BREAKING CHANGE: Commands that accept multiple values for the same option now must have the option flag provided
18-
before each value:
19-
`--option value1 --option value2` instead of `--option value1 value2` (which was previously possible).
18+
- BREAKING CHANGE: Commands that accept multiple values for the same option now must have the option flag provided before each value:
19+
use `--option value1 --option value2` instead of `--option value1 value2` (which was previously possible).
2020
- Cosmetic changes to error messages, progress bars, and help message formatting.
2121

22+
- The `print` command on the `security-data` and `alerts` command groups has been replaced with the `search` command.
23+
This was a name change only, all other functionality remains the same.
24+
2225
### Added
2326

2427
- Profile can now save multiple alert and file event checkpoints. The name of the checkpoint to be used for a given query should be passed to `-c` (`--use-checkpoint`).
2528

29+
### Removed
30+
31+
- The `write-to` and `send-to` commands on `security-data` and `alerts` command groups.
32+
2633
## 0.7.3 - 2020-06-23
2734

2835
### Fixed

README.md

Lines changed: 83 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ When the `--profile` flag is available on other commands, such as those in `secu
5252
instead of the default one. For example,
5353

5454
```bash
55-
code42 security-data print -b 2020-02-02 --profile MY_SECOND_PROFILE
55+
code42 security-data search -b 2020-02-02 --profile MY_SECOND_PROFILE
5656
```
5757

5858
To see all your profiles, do:
@@ -63,19 +63,18 @@ code42 profile list
6363

6464
## Security Data and Alerts
6565

66-
Using the CLI, you can query for security events and alerts and send them to three possible destination types:
66+
Using the CLI, you can query for security events and alerts just like in the admin console, but the results are output
67+
to stdout so they can be written to a file or piped out to another process (for sending to an external syslog server, for
68+
example).
6769

68-
* stdout
69-
* A file
70-
* A server, such as SysLog
7170

7271
The following examples pertain to security events, but can also be used for alerts by replacing `security-data` with
7372
`alerts`:
7473

7574
To print events to stdout, do:
7675

7776
```bash
78-
code42 security-data print -b <begin_date>
77+
code42 security-data search -b <begin_date>
7978
```
8079

8180
Note that `-b` or `--begin` is usually required.
@@ -85,79 +84,110 @@ And end date can also be given with `-e` or `--end` to query for a specific date
8584
To specify a begin/end time, you can pass a date or a date w/ time as a string:
8685

8786
```bash
88-
code42 security-data print -b '2020-02-02 12:51:00'
87+
code42 security-data search -b '2020-02-02 12:51:00'
8988
```
9089

9190
```bash
92-
code42 security-data print -b '2020-02-02 12:30'
91+
code42 security-data search -b '2020-02-02 12:30'
9392
```
9493

9594
```bash
96-
code42 security-data print -b '2020-02-02 12'
95+
code42 security-data search -b '2020-02-02 12'
9796
```
9897

9998
```bash
100-
code42 security-data print -b 2020-02-02
99+
code42 security-data search -b 2020-02-02
101100
```
102101

103102
or a shorthand string specifying either days, hours, or minutes back from the current time:
104103

105104
```bash
106-
code42 security-data print -b 30d
105+
code42 security-data search -b 30d
107106
```
108107

109108
```bash
110-
code42 security-data print -b 10d -e 12h
109+
code42 security-data search -b 10d -e 12h
111110
```
112111

113-
Begin date will be ignored if provided on subsequent queries using `-i`.
112+
Begin date will be ignored if provided on subsequent queries using `-c/--use-checkpoint`.
114113

115114
Use different format with `-f`:
116115

117116
```bash
118-
code42 security-data print -b 2020-02-02 -f CEF
117+
code42 security-data search -b 2020-02-02 -f CEF
119118
```
120119

121120
The available formats are CEF, JSON, and RAW-JSON.
122121
Currently, CEF format is only supported for security events.
123122

124-
To write events to a file, do:
123+
To write events to a file, just redirect your output:
125124

126125
```bash
127-
code42 security-data write-to filename.txt -b 2020-02-02
126+
code42 security-data search -b 2020-02-02 > filename.txt
128127
```
129128

130-
To send events to a server, do:
129+
To send events to an external server using `netcat` on Linux/Mac:
131130

131+
UDP:
132132
```bash
133-
code42 security-data send-to syslog.company.com -p TCP -b 2020-02-02
133+
code42 security-data search -b 10d | nc -u syslog.company.com 514
134134
```
135135

136-
To only get events that Code42 previously did not observe since you last recorded a checkpoint, use the `-i` flag.
137-
136+
TCP:
138137
```bash
139-
code42 security-data send-to syslog.company.com -i
138+
code42 security-data search -b 10d | nc server.company.com 8080
139+
```
140+
141+
Using `powershell` on Windows:
142+
143+
UDP:
144+
```powershell
145+
# set up connection
146+
$Connection = New-Object System.Net.Sockets.UDPClient("syslog.company.com",514)
147+
148+
# pipe code42 output through connection
149+
code42 security-data search -b 10d | foreach {$Message = [Text.Encoding]::UTF8.GetBytes($_); $Connection.Send($Message, $Message.Length)}
150+
```
151+
152+
TCP:
153+
```powershell
154+
# set up connection
155+
$Connection = New-Object System.Net.Sockets.TcpClient("127.0.0.1","65432")
156+
$Writer = New-Object System.IO.StreamWriter($Connection.GetStream())
157+
158+
# pipe code42 output through connection
159+
code42 security-data search -b 10d | foreach { $Writer.WriteLine($_); $Writer.Flush() }
140160
```
141161

142-
This is only guaranteed if you did not change your query.
162+
Note: For more complex requirements when sending to an external server (SSL, special formatting, etc.), use a dedicated
163+
syslog forwarding tool like `rsyslog` or connection tunneling tool like `stunnel`.
164+
165+
If you want to periodically run the same query, but only retrieve the new events each time, use the
166+
`-c/--use-checkpoint` option with a name for your checkpoint. This stores the timestamp of the query's last event to a
167+
file on disk and uses that as the "begin date" timestamp filter on the next query that uses the same checkpoint name.
168+
Checkpoints are stored per profile.
143169

144-
To send events to a server using a specific profile, do:
170+
Initial run requires a begin date:
171+
```bash
172+
code42 security-data search -b 30d --use-checkpoint my_checkpoint
173+
```
145174

175+
Subsequent runs do not:
146176
```bash
147-
code42 security-data --profile PROFILE_FOR_RECURRING_JOB send-to syslog.company.com -b 2020-02-02 -f CEF -i
177+
code42 security-data search --use-checkpoint my_checkpoint
148178
```
149179

150180
You can also use wildcard for queries, but note, if they are not in quotes, you may get unexpected behavior.
151181

152182
```bash
153-
code42 security-data print --actor "*"
183+
code42 security-data search --actor "*"
154184
```
155185

156-
Each destination-type subcommand shares query parameters
186+
The search query parameters are as follows:
157187

158-
- `-t` (exposure types)
159-
- `-b` (begin date)
160-
- `-e` (end date)
188+
- `-t/--type` (exposure types)
189+
- `-b/--begin` (begin date)
190+
- `-e/--end` (end date)
161191
- `--c42-username`
162192
- `--actor`
163193
- `--md5`
@@ -171,9 +201,29 @@ Each destination-type subcommand shares query parameters
171201
- `--advanced-query` (raw JSON query)
172202

173203
You cannot use other query parameters if you use `--advanced-query`.
174-
To learn more about acceptable arguments, add the `-h` flag to `code42` or any of the destination-type subcommands.
204+
To learn more about acceptable arguments, add the `-h` flag to `code42 security-data`
205+
206+
Saved Searches:
175207

208+
The CLI can also access "saved searches" that are stored in the admin console, and run them via their saved search ID.
176209

210+
Use the `saved-search list` subcommand to list existing searches with their IDs:
211+
212+
```bash
213+
code42 security-data saved-search list
214+
```
215+
216+
The `show` subcommand will give details about the search with the provided ID:
217+
218+
```bash
219+
code42 security-data saved-search show <ID>
220+
```
221+
222+
To get the results of a saved search, use the `--saved-search` option with your search ID on the `search` subcommand:
223+
224+
```bash
225+
code42 security-data search --saved-search <ID>
226+
```
177227

178228
## Detection Lists
179229

@@ -212,7 +262,9 @@ reported.
212262
If you keep getting prompted for your password, try resetting with `code42 profile reset-pw`.
213263
If that doesn't work, delete your credentials file located at ~/.code42cli or the entry in keychain.
214264

215-
## Tab completion
265+
## Shell tab completion
266+
267+
To enable shell autocomplete when you hit `tab` after the first few characters of a command name, do the following:
216268

217269
For Bash, add this to ~/.bashrc:
218270

docs/commands/alerts.md

Lines changed: 4 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
# Alerts
22

3-
## Shared arguments
3+
## search
44

5-
Search args are shared between `print`, `write-to`, and `send-to` commands.
5+
Search for alerts and print them to stdout.
66

7+
Arguments:
78
* `advanced-query`: A raw JSON alerts query. Useful for when the provided query parameters do not satisfy your
89
requirements. WARNING: Using advanced queries is incompatible with other query-building args.
910
* `-b`, `--begin`: The beginning of the date range in which to look for alerts, can be a date/time in yyyy-MM-dd (UTC)
@@ -32,43 +33,9 @@ Search args are shared between `print`, `write-to`, and `send-to` commands.
3233
* `-f`, `--format` (optional): The format used for outputting file events. Available choices= [CEF,JSON,RAW-JSON].
3334
* `-c`, `--use-checkpoint` (optional): Get only file events that were not previously retrieved by writing the timestamp of the last event retrieved to a named checkpoint.
3435

35-
## print
36-
37-
Print file events to stdout.
38-
39-
Arguments:
40-
* search args (note that begin date is often required).
41-
42-
Usage:
43-
```bash
44-
code42 alerts print -b <begin-date> <args>
45-
```
46-
47-
## write-to
48-
49-
Write file events to the file with the given name.
50-
51-
Arguments:
52-
* `output_file`: The name of the local file to send output to.
53-
* search args (note that begin date is often required).
54-
55-
Usage:
56-
```bash
57-
code42 alerts write-to -b 2020-03-01 <rgs>
58-
```
59-
60-
## send-to
61-
62-
Send file events to the given server address.
63-
64-
Arguments:
65-
* `server`: The server address to send output to.
66-
* `protocol` (optional): Protocol used to send logs to server. Available choices= [TCP, UDP].
67-
* search args (note that begin date is often required).
68-
6936
Usage:
7037
```bash
71-
code42 alerts send-to <server> <optional-args> <args>
38+
code42 alerts search -b <begin-date> <options>
7239
```
7340

7441
## clear-checkpoint

docs/commands/securitydata.md

Lines changed: 7 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
# Security Data
22

3-
## Shared arguments
43

5-
Search args are shared between `print`, `write-to`, and `send-to` commands.
4+
## search
65

7-
* `--advanced-query` (optional): A raw JSON file events query. Useful for when the provided query parameters do not
6+
Search for file events and print them to stdout.
7+
8+
Arguments:
9+
* `--advanced-query` (optional | cannot be used with other query options): A raw JSON file events query. Useful for when the provided query parameters do not
810
satisfy your requirements. WARNING: Using advanced queries is incompatible with other query-building args.
11+
* `--saved-search` (optional | cannot be used with other query options): Get events from a saved search filter (created in the Code42 admin console) with the given ID.
912
* `-b`, `--begin` (required except for non-first runs in checkpoint mode): The beginning of the date range in which to
1013
look for file events, can be a date/time in yyyy-MM-dd (UTC) or yyyy-MM-dd HH:MM:SS (UTC+24-hr time) format where
1114
the 'time' portion of the string can be partial (e.g. '2020-01-01 12' or '2020-01-01 01:15') or a short value
@@ -28,45 +31,11 @@ Search args are shared between `print`, `write-to`, and `send-to` commands.
2831
* `-f`, `--format` (optional): The format used for outputting file events. Available choices= [CEF,JSON,RAW-JSON].
2932
* `-c`, `--use-checkpoint` (optional): Get only file events that were not previously retrieved by writing the timestamp of the last event retrieved to a named checkpoint.
3033

31-
32-
## print
33-
34-
Print file events to stdout.
35-
36-
Arguments:
37-
* search args (note that begin date is often required).
38-
39-
Usage:
40-
```bash
41-
code42 security-data print -b <begin-date> <args>
42-
```
43-
44-
## write-to
45-
46-
Write file events to the file with the given name.
47-
48-
Arguments:
49-
* `output_file`: The name of the local file to send output to.
50-
* search args (note that begin date is often required).
51-
5234
Usage:
5335
```bash
54-
code42 security-data write-to -b 2020-03-01 <args>
36+
code42 security-data search -b <begin-date> <options>
5537
```
5638

57-
## send-to
58-
59-
Send file events to the given server address.
60-
61-
Arguments:
62-
* `server`: The server address to send output to.
63-
* `protocol` (optional): Protocol used to send logs to server. Available choices= [TCP, UDP].
64-
* search args (note that begin date is often required).
65-
66-
Usage:
67-
```bash
68-
code42 security-data send-to <server> <optional-server-args> <args>
69-
```
7039

7140
## clear-checkpoint
7241

docs/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ To use the Code42 CLI, you must have:
1313

1414
A Code42 Diamond or Platinum product plan
1515
Endpoint monitoring enabled in the Code42 console
16-
Python version 2.7.x, or 3.5 and later installed
16+
Python version 3.5 and later installed
1717

1818
## Content
1919

docs/userguides/profile.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ When the `--profile` flag is available on other commands, such as those in `secu
2525
instead of the default one. For example,
2626

2727
```bash
28-
code42 security-data print -b 2020-02-02 --profile MY_SECOND_PROFILE
28+
code42 security-data search -b 2020-02-02 --profile MY_SECOND_PROFILE
2929
```
3030

3131
To see all your profiles, do:

0 commit comments

Comments
 (0)