|
| 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