|
1 | 1 | package config
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "bytes" |
4 | 5 | "flag"
|
5 | 6 | "fmt"
|
6 | 7 | "strings"
|
| 8 | + "text/template" |
7 | 9 |
|
8 | 10 | "github.com/vulncheck-oss/go-exploit/c2"
|
9 | 11 | "github.com/vulncheck-oss/go-exploit/c2/shelltunnel"
|
@@ -316,6 +318,49 @@ func (conf *Config) GetBoolFlag(name string) bool {
|
316 | 318 | return *value
|
317 | 319 | }
|
318 | 320 |
|
| 321 | +// Apply the configuration settings to a Go text template. This will take |
| 322 | +// the `Config` struct and apply it to a `text/template`, allowing for |
| 323 | +// strings to be built directly from the already set configuration |
| 324 | +// variables. |
| 325 | +// |
| 326 | +// s := conf.ApplyTemplate(`CVE: {{.CVE}} - {{.Product}}`) |
| 327 | +// output.PrintStatus(s) // Output: CVE: CVE-2024-1337 - OFBiz |
| 328 | +// |
| 329 | +// Flags that are user defined with CreateStringFlag and other types are |
| 330 | +// directly accessible from their map values, for example if a command line |
| 331 | +// argument is added with conf.CreateStringFlag("output", "do output", |
| 332 | +// "instructions") it will be accessible via the following ApplyTemplate |
| 333 | +// call: |
| 334 | +// |
| 335 | +// conf.ApplyTemplate(`Output flag {{.StringFlagsMap.output}}`) |
| 336 | +// |
| 337 | +// This function only returns the processed string and if a templating |
| 338 | +// error occurs the function emits a framework error and sets the string to |
| 339 | +// an empty string. This makes it harder to process any dynamic content and |
| 340 | +// properly catch errors, but simplifies the return value to only provide a |
| 341 | +// string. |
| 342 | +// |
| 343 | +// This should not be used with potentially attacker controlled input. |
| 344 | +// |
| 345 | +// Some Config types might be complex and will require usage of range |
| 346 | +// components of text/template, follow the package docs if necessary. |
| 347 | +func (conf *Config) ApplyTemplate(name string) string { |
| 348 | + t, err := template.New("config-string-template").Parse(name) |
| 349 | + if err != nil { |
| 350 | + output.PrintfFrameworkError("Could not create template: %s", err.Error()) |
| 351 | + |
| 352 | + return "" |
| 353 | + } |
| 354 | + var buf bytes.Buffer |
| 355 | + if err := t.Execute(&buf, conf); err != nil { |
| 356 | + output.PrintfFrameworkError("Could not apply template: %s", err.Error()) |
| 357 | + |
| 358 | + return "" |
| 359 | + } |
| 360 | + |
| 361 | + return buf.String() |
| 362 | +} |
| 363 | + |
319 | 364 | // Disable automatic start of c2 servers. Manually starting is required after
|
320 | 365 | // this function is called. This is useful when you have an exploit that
|
321 | 366 | // may have multiple stages and you are guaranteed to not need the C2
|
|
0 commit comments