Path: blob/dev/pkg/input/provider/list/hmap_test.go
2070 views
package list12import (3"net"4"os"5"strconv"6"strings"7"testing"89"github.com/miekg/dns"10"github.com/projectdiscovery/hmap/store/hybrid"11"github.com/projectdiscovery/nuclei/v3/pkg/protocols/common/contextargs"12"github.com/projectdiscovery/nuclei/v3/pkg/protocols/common/protocolstate"13"github.com/projectdiscovery/nuclei/v3/pkg/types"14"github.com/projectdiscovery/nuclei/v3/pkg/utils/expand"15"github.com/projectdiscovery/utils/auth/pdcp"16"github.com/stretchr/testify/require"17)1819func Test_expandCIDR(t *testing.T) {20tests := []struct {21cidr string22expected []string23}{24{25cidr: "173.0.84.0/30",26expected: []string{"173.0.84.0", "173.0.84.1", "173.0.84.2", "173.0.84.3"},27}, {28cidr: "104.154.124.0/29",29expected: []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"},30},31}32for _, tt := range tests {33hm, err := hybrid.New(hybrid.DefaultDiskOptions)34require.Nil(t, err, "could not create temporary input file")35input := &ListInputProvider{hostMap: hm}3637ips := expand.CIDR(tt.cidr)38input.addTargets("", ips)39// scan40got := []string{}41input.hostMap.Scan(func(k, _ []byte) error {42metainput := contextargs.NewMetaInput()43if err := metainput.Unmarshal(string(k)); err != nil {44return err45}46got = append(got, metainput.Input)47return nil48})49require.ElementsMatch(t, tt.expected, got, "could not get correct cidrs")50input.Close()51}52}5354type mockDnsHandler struct{}5556func (m *mockDnsHandler) ServeDNS(w dns.ResponseWriter, r *dns.Msg) {57msg := dns.Msg{}58msg.SetReply(r)59switch r.Question[0].Qtype {60case dns.TypeA:61msg.Authoritative = true62domain := msg.Question[0].Name63msg.Answer = append(msg.Answer, &dns.A{64Hdr: dns.RR_Header{Name: domain, Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 60},65A: net.ParseIP("128.199.158.128"),66})67case dns.TypeAAAA:68msg.Authoritative = true69domain := msg.Question[0].Name70msg.Answer = append(msg.Answer, &dns.AAAA{71Hdr: dns.RR_Header{Name: domain, Rrtype: dns.TypeAAAA, Class: dns.ClassINET, Ttl: 60},72AAAA: net.ParseIP("2400:6180:0:d0::91:1001"),73})74}75_ = w.WriteMsg(&msg)76}7778func Test_scanallips_normalizeStoreInputValue(t *testing.T) {79srv := &dns.Server{Addr: ":" + strconv.Itoa(61234), Net: "udp"}80srv.Handler = &mockDnsHandler{}8182go func() {83err := srv.ListenAndServe()84require.Nil(t, err)85}()8687defaultOpts := types.DefaultOptions()88defaultOpts.InternalResolversList = []string{"127.0.0.1:61234"}89_ = protocolstate.Init(defaultOpts)90type testcase struct {91hostname string92ipv4 bool93ipv6 bool94expected []string95}96tests := []testcase{97{98hostname: "scanme.sh",99ipv4: true,100expected: []string{"128.199.158.128"},101}, {102hostname: "scanme.sh",103ipv6: true,104expected: []string{"2400:6180:0:d0::91:1001"},105},106}107// add extra edge cases108urls := []string{109"https://scanme.sh/",110"http://scanme.sh",111"https://scanme.sh:443/",112"https://scanme.sh:443/somepath",113"http://scanme.sh:80/?with=param",114"scanme.sh/home",115"scanme.sh",116}117resolvedIps := []string{"128.199.158.128", "2400:6180:0:d0::91:1001"}118119for _, v := range urls {120tests = append(tests, testcase{121hostname: v,122ipv4: true,123ipv6: true,124expected: resolvedIps,125})126}127for _, tt := range tests {128hm, err := hybrid.New(hybrid.DefaultDiskOptions)129require.Nil(t, err, "could not create temporary input file")130input := &ListInputProvider{131hostMap: hm,132ipOptions: &ipOptions{133ScanAllIPs: true,134IPV4: tt.ipv4,135IPV6: tt.ipv6,136},137}138139input.Set("", tt.hostname)140// scan141got := []string{}142input.hostMap.Scan(func(k, v []byte) error {143metainput := contextargs.NewMetaInput()144if err := metainput.Unmarshal(string(k)); err != nil {145return err146}147got = append(got, metainput.CustomIP)148return nil149})150require.ElementsMatchf(t, tt.expected, got, "could not get correct ips for hostname %v", tt.hostname)151input.Close()152}153}154155func Test_expandASNInputValue(t *testing.T) {156// skip this test if pdcp keys are not present157h := pdcp.PDCPCredHandler{}158creds, err := h.GetCreds()159if err != nil || creds == nil || creds.APIKey == "" {160t.Logf("Skipping asnmap test as pdcp keys are not present")161t.SkipNow()162}163tests := []struct {164asn string165expectedOutputFile string166}{167{168asn: "AS14421",169expectedOutputFile: "tests/AS14421.txt",170},171{172asn: "AS134029",173expectedOutputFile: "tests/AS134029.txt",174},175}176for _, tt := range tests {177hm, err := hybrid.New(hybrid.DefaultDiskOptions)178require.Nil(t, err, "could not create temporary input file")179input := &ListInputProvider{hostMap: hm}180// get the IP addresses for ASN number181ips := expand.ASN(tt.asn)182input.addTargets("", ips)183// scan the hmap184got := []string{}185input.hostMap.Scan(func(k, v []byte) error {186metainput := contextargs.NewMetaInput()187if err := metainput.Unmarshal(string(k)); err != nil {188return err189}190got = append(got, metainput.Input)191return nil192})193if len(got) == 0 {194// asnmap server is down195t.SkipNow()196}197// read the expected IPs from the file198fileContent, err := os.ReadFile(tt.expectedOutputFile)199require.Nil(t, err, "could not read the expectedOutputFile file")200201items := strings.Split(strings.ReplaceAll(string(fileContent), "\r\n", "\n"), "\n")202203require.ElementsMatch(t, items, got, "could not get correct ips")204}205}206207208