Skip to content

Commit 2d7ebe0

Browse files
authored
Add string templatizing from Config function (#306)
Add string templatizing from Config function
1 parent 95496b6 commit 2d7ebe0

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

config/config.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package config
22

33
import (
4+
"bytes"
45
"flag"
56
"fmt"
67
"strings"
8+
"text/template"
79

810
"github.com/vulncheck-oss/go-exploit/c2"
911
"github.com/vulncheck-oss/go-exploit/c2/shelltunnel"
@@ -316,6 +318,49 @@ func (conf *Config) GetBoolFlag(name string) bool {
316318
return *value
317319
}
318320

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+
319364
// Disable automatic start of c2 servers. Manually starting is required after
320365
// this function is called. This is useful when you have an exploit that
321366
// may have multiple stages and you are guaranteed to not need the C2

config/config_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,28 @@ func TestExternalDefaultFlags(t *testing.T) {
7272
t.Error("Unexpected GetBoolFlag results")
7373
}
7474
}
75+
76+
func TestApplyTemplate(t *testing.T) {
77+
conf := config.NewRemoteExploit(
78+
config.ImplementedFeatures{AssetDetection: true, VersionScanning: true, Exploitation: true},
79+
config.CodeExecution, []c2.Impl{}, "Apache", []string{"OFBiz"},
80+
[]string{"cpe:2.3:a:apache:ofbiz"}, "CVE-2024-45507", "HTTP", 80)
81+
82+
conf.CreateStringFlag("teststring2", "default!", "string usage")
83+
conf.CreateUintFlag("testuint2", 99, "uint usage")
84+
conf.CreateIntFlag("testint2", 300, "int usage")
85+
conf.CreateBoolFlag("testbool2", true, "bool usage")
86+
87+
s := conf.ApplyTemplate("{{.CVE}} {{.StringFlagsMap.teststring2}} {{.UintFlagsMap.testuint2}} {{.IntFlagsMap.testint2}} {{.BoolFlagsMap.testbool2}}")
88+
if s == "" {
89+
t.Error("Template returned error")
90+
}
91+
s = conf.ApplyTemplate("{{.CVE}} {{.StringFlagsMap.teststring2}} {{.UintFlagsMap.testuint2}} {{.IntFlagsMap.testint2}} {{.BoolFlagsMap.testbool2}}")
92+
if s == "" {
93+
t.Error("Template returned error")
94+
}
95+
96+
if s != "CVE-2024-45507 default! 99 300 true" {
97+
t.Errorf("'%s' unexpected", s)
98+
}
99+
}

0 commit comments

Comments
 (0)