Skip to content

Commit

Permalink
feat: coredns axfr for dump records
Browse files Browse the repository at this point in the history
  • Loading branch information
Esonhugh committed Mar 18, 2024
1 parent f3801f8 commit 19026f2
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 3 deletions.
51 changes: 51 additions & 0 deletions cmd/axfr/axfr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package axfr

import (
"os"
"strings"

command "github.com/esonhugh/k8spider/cmd"
"github.com/esonhugh/k8spider/define"
"github.com/esonhugh/k8spider/pkg"
"github.com/miekg/dns"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

func init() {
command.RootCmd.AddCommand(AxfrCmd)
}

var AxfrCmd = &cobra.Command{
Use: "axfr",
Short: "axfr is a command to dump every record from dns server",
Run: func(cmd *cobra.Command, args []string) {

if command.Opts.Zone == "" {
log.Warn("zone can't empty")
return
}
zone := dns.Fqdn(command.Opts.Zone)

dnsServer := command.Opts.DnsServer
if command.Opts.DnsServer == "" {
dnsServer = "ns.dns." + command.Opts.Zone + ":53"
} else if len(strings.Split(dnsServer, ":")) < 2 {
dnsServer = dnsServer + ":53"
}

log.Debugf("same command: dig axfr %v @%v", zone, dnsServer)
var records define.Records = pkg.DumpAXFR(zone, dnsServer)
if command.Opts.OutputFile != "" {
f, err := os.OpenFile(command.Opts.OutputFile, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Warnf("OpenFile failed: %v", err)
}
defer f.Close()
records.Print(log.StandardLogger().Writer(), f)
} else {
records.Print(log.StandardLogger().Writer())
}

},
}
1 change: 1 addition & 0 deletions define/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Record struct {
Ip net.IP
SvcDomain string
SrvRecords []SrvRecord
Extra string `json:"Extra,omitempty"`
}

type SrvRecord struct {
Expand Down
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/esonhugh/k8spider
go 1.19

require (
github.com/miekg/dns v1.1.58
github.com/sirupsen/logrus v1.9.0
github.com/spf13/cobra v1.5.0
)
Expand All @@ -11,5 +12,8 @@ require (
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.8.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/net v0.20.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/tools v0.17.0 // indirect
)
13 changes: 11 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/miekg/dns v1.1.58 h1:ca2Hdkz+cDg/7eNF6V56jjzuZ4aCAE+DbVkILdQWG/4=
github.com/miekg/dns v1.1.58/go.mod h1:Ypv+3b/KadlvW9vJfXOTf300O4UqaHFzFCuHz+rPkBY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
Expand All @@ -19,9 +21,16 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0=
golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.20.0 h1:aCL9BSgETF1k+blQaYUBx9hJ9LOGP3gAVemcZlf1Kpo=
golang.org/x/net v0.20.0/go.mod h1:z8BVo6PvndSri0LbOE3hAn0apkU+1YvI6E70E9jsnvY=
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/tools v0.17.0 h1:FvmRgNOcs3kOa+T20R1uhfP9F6HgG2mfxDv1vrx1Htc=
golang.org/x/tools v0.17.0/go.mod h1:xsh6VxdV005rRVaS6SSAf9oiAqljS7UZUacMZ8Bnsps=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"github.com/esonhugh/k8spider/cmd"
_ "github.com/esonhugh/k8spider/cmd/all"
_ "github.com/esonhugh/k8spider/cmd/axfr"
_ "github.com/esonhugh/k8spider/cmd/service"
_ "github.com/esonhugh/k8spider/cmd/subnet"
)
Expand Down
28 changes: 28 additions & 0 deletions pkg/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package pkg

import (
"net"
"strings"

"github.com/esonhugh/k8spider/define"
"github.com/miekg/dns"
log "github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -37,3 +39,29 @@ func ScanSvcForPorts(records []define.Record) []define.Record {
}
return records
}

// default target should be zone
func DumpAXFR(target string, dnsServer string) []define.Record {
t := new(dns.Transfer)
m := new(dns.Msg)
m.SetAxfr(target)
ch, err := t.In(m, dnsServer)
if err != nil {
log.Fatalf("Transfer failed: %v", err)
}
var records []define.Record
for rr := range ch {
if rr.Error != nil {
log.Errorf("Error: %v", rr.Error)
continue
}
for _, r := range rr.RR {
records = append(records, define.Record{
SvcDomain: r.Header().Name,
Extra: strings.Join(strings.Split(r.String(), "\t"), " "),
})
}
log.Debugf("Record: %v", rr.RR)
}
return records
}
5 changes: 5 additions & 0 deletions pkg/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,8 @@ func SRVRecord(svcDomain string) (string, []*net.SRV, error) {
cname, srvs, err := NetResolver.LookupSRV(context.Background(), "", "", svcDomain)
return cname, srvs, err
}

func ARecord(domain string) (ips []net.IP, err error) {
ips, err = net.LookupIP(domain)
return
}

0 comments on commit 19026f2

Please sign in to comment.