Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Helper function for IP Formats #3286

Merged
merged 9 commits into from
Feb 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions integration_tests/http/dsl-functions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ requests:
83: {{join(" ", uniq("ab", "cd", "12", "34", "12", "cd"))}}
84: {{split("ab,cd,efg", ",")}}
85: {{split("ab,cd,efg", ",", 2)}}
86: {{ip_format('127.0.0.1', 3)}}
87: {{ip_format('127.0.1.0', 11)}}

extractors:
- type: regex
Expand Down
2 changes: 1 addition & 1 deletion v2/cmd/integration-test/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ func (h *httpDSLFunctions) Execute(filePath string) error {
resultPart = stringsutil.TrimPrefixAny(resultPart, "/", " ", "[")

extracted := strings.Split(resultPart, ",")
numberOfDslFunctions := 85
numberOfDslFunctions := 87
if len(extracted) != numberOfDslFunctions {
return errors.New("incorrect number of results")
}
Expand Down
15 changes: 15 additions & 0 deletions v2/pkg/operators/common/dsl/dsl.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
"github.com/spaolacci/murmur3"

"github.com/projectdiscovery/gologger"
"github.com/projectdiscovery/mapcidr"
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/helpers/deserialization"
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/randomip"
"github.com/projectdiscovery/nuclei/v2/pkg/types"
Expand Down Expand Up @@ -915,6 +916,20 @@ func init() {

return buf.String(), nil
}),
"ip_format": makeDslFunction(2, func(args ...interface{}) (interface{}, error) {
ipFormat, err := strconv.ParseInt(types.ToString(args[1]), 10, 64)
if err != nil {
return nil, err
}
if ipFormat <= 0 || ipFormat > 11 {
return nil, fmt.Errorf("invalid format, format must be in range 1-11")
}
formattedIps := mapcidr.AlterIP(types.ToString(args[0]), []string{types.ToString(args[1])}, 3, false)
if len(formattedIps) == 0 {
return nil, fmt.Errorf("no formatted IP returned")
}
return formattedIps[0], nil
}),
}

dslFunctions = make(map[string]dslFunction, len(tempDslFunctions))
Expand Down
5 changes: 5 additions & 0 deletions v2/pkg/operators/common/dsl/dsl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ func TestGetPrintableDslFunctionSignatures(t *testing.T) {
hmac(arg1, arg2, arg3 interface{}) interface{}
html_escape(arg1 interface{}) interface{}
html_unescape(arg1 interface{}) interface{}
ip_format(arg1, arg2 interface{}) interface{}
join(separator string, elements ...interface{}) string
join(separator string, elements []interface{}) string
json_minify(arg1 interface{}) interface{}
Expand Down Expand Up @@ -270,6 +271,10 @@ func TestDslExpressions(t *testing.T) {
`join(", ", split(hex_encode("abcdefg"), 2))`: "61, 62, 63, 64, 65, 66, 67",
`json_minify("{ \"name\": \"John Doe\", \"foo\": \"bar\" }")`: "{\"foo\":\"bar\",\"name\":\"John Doe\"}",
`json_prettify("{\"foo\":\"bar\",\"name\":\"John Doe\"}")`: "{\n \"foo\": \"bar\",\n \"name\": \"John Doe\"\n}",
`ip_format('127.0.0.1', '1')`: "127.0.0.1",
`ip_format('127.0.0.1', '3')`: "0177.0.0.01",
`ip_format('127.0.0.1', '5')`: "281472812449793",
`ip_format('127.0.1.0', '11')`: "127.0.256",
}

testDslExpressionScenarios(t, dslExpressions)
Expand Down