Ice3man543 2021-02-27 12:33:27 +05:30
parent 252e0ff771
commit 9294e055d9
2 changed files with 152 additions and 0 deletions

View File

@ -0,0 +1,63 @@
package main
import (
"io/ioutil"
"strconv"
"github.com/miekg/dns"
"github.com/projectdiscovery/nuclei/v2/internal/testutils"
)
var dnsTestCases = map[string]testutils.TestCase{
"dns/basic.yaml": &dnsBasic{},
}
type dnsBasic struct{}
func (h *dnsBasic) ServeDNS(w dns.ResponseWriter, r *dns.Msg) {
msg := dns.Msg{}
msg.SetReply(r)
switch r.Question[0].Qtype {
case dns.TypeCNAME:
msg.Authoritative = true
domain := msg.Question[0].Name
if domain == "test.nuclei." {
msg.Answer = append(msg.Answer, &dns.CNAME{
Hdr: dns.RR_Header{Name: domain, Rrtype: dns.TypeCNAME, Class: dns.ClassINET, Ttl: 60},
Target: "nuclei.works.",
})
}
}
w.WriteMsg(&msg)
}
// Executes executes a test case and returns an error if occurred
func (h *dnsBasic) Execute(filePath string) error {
var routerErr error
srv := &dns.Server{Addr: ":" + strconv.Itoa(54), Net: "udp"}
srv.Handler = h
go func() {
if err := srv.ListenAndServe(); err != nil {
routerErr = err
return
}
}()
err := ioutil.WriteFile("resolvers.txt", []byte("127.0.0.1:54"), 0777)
if err != nil {
return err
}
results, err := testutils.RunNucleiAndGetResults(filePath, "test.nuclei", debug, "-r", "resolvers.txt")
if err != nil {
return err
}
if routerErr != nil {
return routerErr
}
if len(results) != 1 {
return errIncorrectResultsCount(results)
}
return nil
}

View File

@ -0,0 +1,89 @@
package main
import (
"net"
"github.com/projectdiscovery/nuclei/v2/internal/testutils"
)
var networkTestcases = map[string]testutils.TestCase{
"network/basic.yaml": &networkBasic{},
"network/hex.yaml": &networkBasic{},
"network/multi-step.yaml": &networkMultiStep{},
}
type networkBasic struct{}
// Executes executes a test case and returns an error if occurred
func (h *networkBasic) Execute(filePath string) error {
var routerErr error
ts := testutils.NewTCPServer(func(conn net.Conn) {
defer conn.Close()
data := make([]byte, 4)
if _, err := conn.Read(data); err != nil {
routerErr = err
return
}
if string(data) == "PING" {
_, _ = conn.Write([]byte("PONG"))
}
})
defer ts.Close()
results, err := testutils.RunNucleiAndGetResults(filePath, ts.URL, debug)
if err != nil {
return err
}
if routerErr != nil {
return routerErr
}
if len(results) != 1 {
return errIncorrectResultsCount(results)
}
return nil
}
type networkMultiStep struct{}
// Executes executes a test case and returns an error if occurred
func (h *networkMultiStep) Execute(filePath string) error {
var routerErr error
ts := testutils.NewTCPServer(func(conn net.Conn) {
defer conn.Close()
data := make([]byte, 5)
if _, err := conn.Read(data); err != nil {
routerErr = err
return
}
if string(data) == "FIRST" {
_, _ = conn.Write([]byte("PING"))
}
data = make([]byte, 6)
if _, err := conn.Read(data); err != nil {
routerErr = err
return
}
if string(data) == "SECOND" {
_, _ = conn.Write([]byte("PONG"))
}
_, _ = conn.Write([]byte("NUCLEI"))
})
defer ts.Close()
results, err := testutils.RunNucleiAndGetResults(filePath, ts.URL, debug)
if err != nil {
return err
}
if routerErr != nil {
return routerErr
}
if len(results) != 1 {
return errIncorrectResultsCount(results)
}
return nil
}