cidr input support #2665 (#2675)

* Add CIDR input support

- Add expandCIDRInputValue function which accepts the cidr,
  and stores the IPs into hmap. It uses mapcidr to get the expanded IPs
- Add test case to test expandCIDRInputValue and isCIDR function
- Update dsl_test.go which had typo. coz of failing test

* Resolve the requested changes
dev
Shubham Rasal 2022-10-07 14:28:02 +05:30 committed by GitHub
parent b17163e6b2
commit 3ebd1f689b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 63 additions and 0 deletions

View File

@ -15,6 +15,8 @@ import (
"github.com/projectdiscovery/fileutil"
"github.com/projectdiscovery/gologger"
"github.com/projectdiscovery/hmap/store/hybrid"
"github.com/projectdiscovery/iputil"
"github.com/projectdiscovery/mapcidr"
"github.com/projectdiscovery/nuclei/v2/pkg/types"
)
@ -69,6 +71,10 @@ func (i *Input) Close() {
func (i *Input) initializeInputSources(options *types.Options) error {
// Handle targets flags
for _, target := range options.Targets {
if iputil.IsCIDR(target) {
i.expandCIDRInputValue(target)
continue
}
i.normalizeStoreInputValue(target)
}
@ -93,6 +99,10 @@ func (i *Input) initializeInputSources(options *types.Options) error {
func (i *Input) scanInputFromReader(reader io.Reader) {
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
if iputil.IsCIDR(scanner.Text()) {
i.expandCIDRInputValue(scanner.Text())
continue
}
i.normalizeStoreInputValue(scanner.Text())
}
}
@ -134,3 +144,19 @@ func (i *Input) Scan(callback func(value string)) {
i.hostMap.Scan(callbackFunc)
}
}
// expandCIDRInputValue expands CIDR and stores expanded IPs
func (i *Input) expandCIDRInputValue(value string) {
ips, _ := mapcidr.IPAddressesAsStream(value)
for ip := range ips {
if _, ok := i.hostMap.Get(ip); ok {
i.dupeCount++
continue
}
i.inputCount++
_ = i.hostMap.Set(ip, nil)
if i.hostMapStream != nil {
_ = i.hostMapStream.Set([]byte(ip), nil)
}
}
}

View File

@ -0,0 +1,37 @@
package hybrid
import (
"testing"
"github.com/projectdiscovery/hmap/store/hybrid"
"github.com/stretchr/testify/require"
)
func Test_expandCIDRInputValue(t *testing.T) {
tests := []struct {
cidr string
expected []string
}{
{
cidr: "173.0.84.0/30",
expected: []string{"173.0.84.0", "173.0.84.1", "173.0.84.2", "173.0.84.3"},
}, {
cidr: "104.154.124.0/29",
expected: []string{"104.154.124.0", "104.154.124.1", "104.154.124.2", "104.154.124.3", "104.154.124.4", "104.154.124.5", "104.154.124.6", "104.154.124.7"},
},
}
for _, tt := range tests {
hm, err := hybrid.New(hybrid.DefaultDiskOptions)
require.Nil(t, err, "could not create temporary input file")
input := &Input{hostMap: hm}
input.expandCIDRInputValue(tt.cidr)
// scan
got := []string{}
input.hostMap.Scan(func(k, v []byte) error {
got = append(got, string(k))
return nil
})
require.ElementsMatch(t, tt.expected, got, "could not get correct ips")
}
}