Skip to content

Commit 3aecaf3

Browse files
committed
Add RTR admin script management example.
1 parent ce1cd00 commit 3aecaf3

File tree

4 files changed

+140
-0
lines changed

4 files changed

+140
-0
lines changed

.github/wordlist.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,9 @@ pre
3838
stdout
3939
utilises
4040
zta
41+
RTR
42+
Responders
43+
linux
44+
macOS
45+
Zsh
46+
PowerShell
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
This is a working standalone example of a program to upload a stored script
2+
using the RTR Create Script API and then running it against an agent
3+
via the RTR Execute Admin Command API.
4+
5+
Note that the API client key used for this example will need to be granted
6+
the RTR Administrator permission for this script to run successfully.
7+
8+
For more information on managing RTR scripts as an Administrator, see the
9+
[Manage Real Time Response scripts](https://developer.crowdstrike.com/crowdstrike/docs/real-time-response-apis#manage-real-time-response-scripts)
10+
section of the Falcon developer API documentation.
11+
12+
## Build
13+
```
14+
go get github.com/crowdstrike/gofalcon/examples/falcon_rtr_upload_and_run_script
15+
```
16+
17+
## Setup Environment Variables
18+
```
19+
export FALCON_CLIENT_ID="your_falcon_id"
20+
export FALCON_CLIENT_SECRET="your_falcon_secret"
21+
export FALCON_CLOUD="us-1, us-2, eu-1, us-gov-1, etc"
22+
```
23+
24+
## Usage
25+
```
26+
$ FALCON_CLIENT_ID="abc" FALCON_CLIENT_SECRET="XYZ" FALCON_CLOUD=us-1 \
27+
falcon_rtr_admin_create_and_run_script --permtype group --platforms="linux,mac" \
28+
--script="relative path to script file from current working directory" \
29+
--name="name of the file to use when invoking runscript with the `-CloudFile` option" \
30+
--aid="def"
31+
```
32+
33+
## Notes
34+
35+
### Script types by platform
36+
37+
* Scripts targeting Windows will be interpreted with PowerShell
38+
* Scripts targeting Linux will be interpreted with bash
39+
* Scripts targeting macOS will be interpreted with Zsh
40+
41+
### Permission Type
42+
43+
Valid values for the `permtype` argument are:
44+
45+
* private: script can only be invoked by the user who uploaded it
46+
* group: script can only be invoked by RTR Administrators
47+
* public: script can be invoked by RTR Administrators and RTR Active Responders
48+
49+
Default value for `permtype` is `group`.
50+
51+
### Platforms
52+
53+
The `platforms` argument is a comma-delimited string of one or more of either
54+
"windows", "linux", or "mac".
55+
56+
If not specified, the default for this example code is "linux", though when
57+
using the Falcon API or gofalcon SDK directly the default is "windows".
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/bash
2+
echo "Hello RTR!"
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"flag"
6+
"fmt"
7+
"os"
8+
"strings"
9+
10+
"github.com/crowdstrike/gofalcon/falcon"
11+
"github.com/crowdstrike/gofalcon/pkg/falcon_util"
12+
"github.com/go-openapi/runtime"
13+
)
14+
15+
func main() {
16+
clientId := flag.String("client-id", os.Getenv("FALCON_CLIENT_ID"), "Client ID for accessing CrowdStrike Falcon Platform (default taken from FALCON_CLIENT_ID env)")
17+
clientSecret := flag.String("client-secret", os.Getenv("FALCON_CLIENT_SECRET"), "Client Secret for accessing CrowdStrike Falcon Platform (default taken from FALCON_CLIENT_SECRET)")
18+
clientCloud := flag.String("cloud", os.Getenv("FALCON_CLOUD"), "Falcon cloud abbreviation (us-1, us-2, eu-1, us-gov-1; default taken from FALCON_CLOUD)")
19+
aid := flag.String("aid", os.Getenv("FALCON_AGENT_ID"), "Falcon agent ID on which to run the custom script (default taken from FALCON_AGENT_ID)")
20+
permType := flag.String("permtype", "group", "Permission type (private, group, or public; default is group, which makes the script usable to all RTR Admins)")
21+
platformString := flag.String("platforms", "linux", "The platform(s) the file supports. If specified, can be one or more of [windows, mac, linux] (default for this script is linux)")
22+
script := flag.String("script", "examples/falcon_rtr_admin_create_and_run_script/examplescript.sh", "Relative path to the script to upload (defaults to the script included in this example, assuming cwd is the project root)")
23+
name := flag.String("name", "examplescript.sh", "Name to give to the uploaded script for later invocation (default is examplescript.sh)")
24+
25+
flag.Parse()
26+
if *clientId == "" {
27+
*clientId = falcon_util.PromptUser(`Missing FALCON_CLIENT_ID environment variable. Please provide your OAuth2 API Client ID for authentication with CrowdStrike Falcon platform. Establishing and retrieving OAuth2 API credentials can be performed at https://falcon.crowdstrike.com/support/api-clients-and-keys.
28+
Falcon Client ID`)
29+
}
30+
if *clientSecret == "" {
31+
*clientSecret = falcon_util.PromptUser(`Missing FALCON_CLIENT_SECRET environment variable. Please provide your OAuth2 API Client Secret for authentication with CrowdStrike Falcon platform. Establishing and retrieving OAuth2 API credentials can be performed at https://falcon.crowdstrike.com/support/api-clients-and-keys.
32+
Falcon Client Secret`)
33+
}
34+
if *aid == "" {
35+
*aid = falcon_util.PromptUser(`Missing FALCON_AGENT_ID. Please provide the ID of the agent you would like to communicate with.
36+
Falcon agent ID`)
37+
}
38+
platforms := strings.Split(*platformString, ",")
39+
scriptFile, err := os.Open(*script)
40+
if err != nil {
41+
panic(err)
42+
}
43+
scriptReadCloser := runtime.NamedReader(*name, scriptFile)
44+
45+
client, err := falcon.NewRTR(&falcon.ApiConfig{
46+
ClientId: *clientId,
47+
ClientSecret: *clientSecret,
48+
Cloud: falcon.Cloud(*clientCloud),
49+
Context: context.Background(),
50+
})
51+
if err != nil {
52+
panic(err)
53+
}
54+
55+
// First, create/upload the script for later use via the Create Script API.
56+
err = client.CreateScript(context.Background(), nil, "An example script to demonstrate script management via the RTR Admin APIs.",
57+
*permType, platforms, falcon_util.StrPtr("created example script with gofalcon SDK"), nil, scriptReadCloser)
58+
if err != nil {
59+
panic(falcon.ErrorExplain(err))
60+
}
61+
62+
// Then invoke the script by sending the `runscript` command to the RTR Execute Admin Command API.
63+
session, err := client.NewSession(context.Background(), *aid)
64+
if err != nil {
65+
panic(falcon.ErrorExplain(err))
66+
}
67+
result, err := session.AdminExecuteAndWait(
68+
context.Background(), "runscript", fmt.Sprintf("runscript -CloudFile='%s'", *name))
69+
if err != nil {
70+
panic(falcon.ErrorExplain(err))
71+
}
72+
73+
json, _ := falcon_util.PrettyJson(result)
74+
fmt.Println(json)
75+
}

0 commit comments

Comments
 (0)