Skip to content

Commit 2c832f5

Browse files
authored
refactor(vardump): use godump lib (#5676)
* refactor(vardump): use `godump` lib also increate limit char to `255`. Signed-off-by: Dwi Siswanto <[email protected]> * feat(vardump): add global var `Limit` Signed-off-by: Dwi Siswanto <[email protected]> * chore(protocols): rm newline Signed-off-by: Dwi Siswanto <[email protected]> * feat(types): add `VarDumpLimit` option Signed-off-by: Dwi Siswanto <[email protected]> * test(vardump): add test cases Signed-off-by: Dwi Siswanto <[email protected]> * chore: tidy up mod Signed-off-by: Dwi Siswanto <[email protected]> --------- Signed-off-by: Dwi Siswanto <[email protected]>
1 parent 53f56e1 commit 2c832f5

File tree

19 files changed

+131
-47
lines changed

19 files changed

+131
-47
lines changed

cmd/nuclei/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@ on extensive configurability, massive extensibility and ease of use.`)
425425
flagSet.StringVar(&memProfile, "profile-mem", "", "generate memory (heap) profile & trace files"),
426426
flagSet.BoolVar(&options.VerboseVerbose, "vv", false, "display templates loaded for scan"),
427427
flagSet.BoolVarP(&options.ShowVarDump, "show-var-dump", "svd", false, "show variables dump for debugging"),
428+
flagSet.IntVarP(&options.VarDumpLimit, "var-dump-limit", "vdl", 255, "limit the number of characters displayed in var dump"),
428429
flagSet.BoolVarP(&options.EnablePprof, "enable-pprof", "ep", false, "enable pprof debugging server"),
429430
flagSet.CallbackVarP(printTemplateVersion, "templates-version", "tv", "shows the version of the installed nuclei-templates"),
430431
flagSet.BoolVarP(&options.HealthCheck, "health-check", "hc", false, "run diagnostic check up"),

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/projectdiscovery/nuclei/v3
22

3-
go 1.21
3+
go 1.21.0
44

55
require (
66
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible
@@ -104,6 +104,7 @@ require (
104104
github.com/shurcooL/graphql v0.0.0-20230722043721-ed46e5a46466
105105
github.com/stretchr/testify v1.9.0
106106
github.com/tarunKoyalwar/goleak v0.0.0-20240429141123-0efa90dbdcf9
107+
github.com/yassinebenaid/godump v0.10.0
107108
github.com/zmap/zgrab2 v0.1.8-0.20230806160807-97ba87c0e706
108109
go.mongodb.org/mongo-driver v1.17.0
109110
golang.org/x/term v0.24.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,6 +1104,8 @@ github.com/xhit/go-str2duration v1.2.0/go.mod h1:3cPSlfZlUHVlneIVfePFWcJZsuwf+P1
11041104
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 h1:nIPpBwaJSVYIxUFsDv3M8ofmx9yWTog9BfvIu0q41lo=
11051105
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8/go.mod h1:HUYIGzjTL3rfEspMxjDjgmT5uz5wzYJKVo23qUhYTos=
11061106
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
1107+
github.com/yassinebenaid/godump v0.10.0 h1:FolBA+Ix5uwUiXYBBYOsf1VkT5+0f4gtFNTkYTiIR08=
1108+
github.com/yassinebenaid/godump v0.10.0/go.mod h1:dc/0w8wmg6kVIvNGAzbKH1Oa54dXQx8SNKh4dPRyW44=
11071109
github.com/yl2chen/cidranger v1.0.2 h1:lbOWZVCG1tCRX4u24kuM1Tb4nHqWkDxwLdoS+SevawU=
11081110
github.com/yl2chen/cidranger v1.0.2/go.mod h1:9U1yz7WPYDwf0vpNWFaeRh0bjwz5RVgRy/9UEQfHl0g=
11091111
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 h1:ilQV1hzziu+LLM3zUTJ0trRztfwgjqKnBWNtSRkbmwM=

internal/runner/options.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ func ParseOptions(options *types.Options) {
6868

6969
if options.ShowVarDump {
7070
vardump.EnableVarDump = true
71+
vardump.Limit = options.VarDumpLimit
7172
}
7273
if options.ShowActions {
7374
gologger.Info().Msgf("Showing available headless actions: ")

pkg/protocols/code/code.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ func (request *Request) ExecuteWithResults(input *contextargs.Context, dynamicVa
235235
gologger.Verbose().Msgf("[%s] Executed code on local machine %v", request.options.TemplateID, input.MetaInput.Input)
236236

237237
if vardump.EnableVarDump {
238-
gologger.Debug().Msgf("Code Protocol request variables: \n%s\n", vardump.DumpVariables(allvars))
238+
gologger.Debug().Msgf("Code Protocol request variables: %s\n", vardump.DumpVariables(allvars))
239239
}
240240

241241
if request.options.Options.Debug || request.options.Options.DebugRequests {

pkg/protocols/common/helpers/eventcreator/eventcreator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func CreateEventWithAdditionalOptions(request protocols.Request, outputEvent out
2424
// Dump response variables if ran in debug mode
2525
if vardump.EnableVarDump {
2626
protoName := cases.Title(language.English).String(request.Type().String())
27-
gologger.Debug().Msgf("%v Protocol response variables: \n%s\n", protoName, vardump.DumpVariables(outputEvent))
27+
gologger.Debug().Msgf("%v Protocol response variables: %s\n", protoName, vardump.DumpVariables(outputEvent))
2828
}
2929
for _, compiledOperator := range request.GetCompiledOperators() {
3030
if compiledOperator != nil {
Lines changed: 49 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,67 @@
11
package vardump
22

33
import (
4-
"strconv"
54
"strings"
65

76
"github.com/projectdiscovery/nuclei/v3/pkg/types"
87
mapsutil "github.com/projectdiscovery/utils/maps"
8+
"github.com/yassinebenaid/godump"
99
)
1010

11-
// EnableVarDump enables var dump for debugging optionally
12-
var EnableVarDump bool
11+
// variables is a map of variables
12+
type variables = map[string]any
1313

14-
// DumpVariables writes the truncated dump of variables to a string
15-
// in a formatted key-value manner.
16-
//
17-
// The values are truncated to return 50 characters from start and end.
18-
func DumpVariables(data map[string]interface{}) string {
19-
var counter int
14+
// DumpVariables dumps the variables in a pretty format
15+
func DumpVariables(data variables) string {
16+
if !EnableVarDump {
17+
return ""
18+
}
19+
20+
d := godump.Dumper{
21+
Indentation: " ",
22+
HidePrivateFields: false,
23+
ShowPrimitiveNamedTypes: true,
24+
}
25+
26+
d.Theme = godump.Theme{
27+
String: godump.RGB{R: 138, G: 201, B: 38},
28+
Quotes: godump.RGB{R: 112, G: 214, B: 255},
29+
Bool: godump.RGB{R: 249, G: 87, B: 56},
30+
Number: godump.RGB{R: 10, G: 178, B: 242},
31+
Types: godump.RGB{R: 0, G: 150, B: 199},
32+
Address: godump.RGB{R: 205, G: 93, B: 0},
33+
PointerTag: godump.RGB{R: 110, G: 110, B: 110},
34+
Nil: godump.RGB{R: 219, G: 57, B: 26},
35+
Func: godump.RGB{R: 160, G: 90, B: 220},
36+
Fields: godump.RGB{R: 189, G: 176, B: 194},
37+
Chan: godump.RGB{R: 195, G: 154, B: 76},
38+
UnsafePointer: godump.RGB{R: 89, G: 193, B: 180},
39+
Braces: godump.RGB{R: 185, G: 86, B: 86},
40+
}
2041

21-
buffer := &strings.Builder{}
22-
buffer.Grow(len(data) * 78) // grow buffer to an approximate size
42+
return d.Sprint(process(data, Limit))
43+
}
2344

24-
builder := &strings.Builder{}
25-
// sort keys for deterministic output
45+
// process is a helper function that processes the variables
46+
// and returns a new map of variables
47+
func process(data variables, limit int) variables {
2648
keys := mapsutil.GetSortedKeys(data)
49+
vars := make(variables)
50+
51+
if limit == 0 {
52+
limit = 255
53+
}
2754

2855
for _, k := range keys {
29-
v := data[k]
30-
valueString := types.ToString(v)
31-
32-
counter++
33-
if len(valueString) > 50 {
34-
builder.Grow(56)
35-
builder.WriteString(valueString[0:25])
36-
builder.WriteString(" .... ")
37-
builder.WriteString(valueString[len(valueString)-25:])
38-
valueString = builder.String()
39-
builder.Reset()
56+
v := types.ToString(data[k])
57+
v = strings.ReplaceAll(strings.ReplaceAll(v, "\r", " "), "\n", " ")
58+
if len(v) > limit {
59+
v = v[:limit]
60+
v += " [...]"
4061
}
41-
valueString = strings.ReplaceAll(strings.ReplaceAll(valueString, "\r", " "), "\n", " ")
42-
43-
buffer.WriteString("\t")
44-
buffer.WriteString(strconv.Itoa(counter))
45-
buffer.WriteString(". ")
46-
buffer.WriteString(k)
47-
buffer.WriteString(" => ")
48-
buffer.WriteString(valueString)
49-
buffer.WriteString("\n")
62+
63+
vars[k] = v
5064
}
51-
final := buffer.String()
52-
return final
65+
66+
return vars
5367
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package vardump
2+
3+
import (
4+
"strings"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestDumpVariables(t *testing.T) {
11+
// Enable var dump for testing
12+
EnableVarDump = true
13+
14+
// Test case
15+
testVars := variables{
16+
"string": "test",
17+
"int": 42,
18+
"bool": true,
19+
"slice": []string{"a", "b", "c"},
20+
}
21+
22+
result := DumpVariables(testVars)
23+
24+
// Assertions
25+
assert.NotEmpty(t, result)
26+
assert.Contains(t, result, "string")
27+
assert.Contains(t, result, "test")
28+
assert.Contains(t, result, "int")
29+
assert.Contains(t, result, "42")
30+
assert.Contains(t, result, "bool")
31+
assert.Contains(t, result, "true")
32+
assert.Contains(t, result, "slice")
33+
assert.Contains(t, result, "a")
34+
assert.Contains(t, result, "b")
35+
assert.Contains(t, result, "c")
36+
37+
// Test with EnableVarDump set to false
38+
EnableVarDump = false
39+
result = DumpVariables(testVars)
40+
assert.Empty(t, result)
41+
}
42+
43+
func TestProcess(t *testing.T) {
44+
testVars := variables{
45+
"short": "short string",
46+
"long": strings.Repeat("a", 300),
47+
"number": 42,
48+
}
49+
50+
processed := process(testVars, 255)
51+
52+
assert.Equal(t, "short string", processed["short"])
53+
assert.Equal(t, strings.Repeat("a", 255)+" [...]", processed["long"])
54+
assert.Equal(t, "42", processed["number"])
55+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package vardump
2+
3+
var (
4+
// EnableVarDump enables var dump for debugging optionally
5+
EnableVarDump bool
6+
// Limit is the maximum characters to be dumped
7+
Limit int = 255
8+
)

pkg/protocols/dns/request.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func (request *Request) ExecuteWithResults(input *contextargs.Context, metadata,
108108
func (request *Request) execute(input *contextargs.Context, domain string, metadata, previous output.InternalEvent, vars map[string]interface{}, callback protocols.OutputEventCallback) error {
109109
var err error
110110
if vardump.EnableVarDump {
111-
gologger.Debug().Msgf("DNS Protocol request variables: \n%s\n", vardump.DumpVariables(vars))
111+
gologger.Debug().Msgf("DNS Protocol request variables: %s\n", vardump.DumpVariables(vars))
112112
}
113113

114114
// Compile each request for the template based on the URL

0 commit comments

Comments
 (0)