Skip to content

Commit

Permalink
Merge pull request #2 from 5amu/golang-implementation
Browse files Browse the repository at this point in the history
Golang implementation
  • Loading branch information
5amu authored Jul 6, 2022
2 parents eaa82cc + 61e61da commit 97d260f
Show file tree
Hide file tree
Showing 25 changed files with 721 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/dist
57 changes: 57 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
project_name: dnshunter

before:
hooks:
- go mod download

builds:
-
main: ./cmd/dnshunter/
binary: dnshunter
goos:
- windows
- linux
- darwin
- freebsd
goarch:
- amd64
- 386
- arm
- arm64
ignore:
- goos: darwin
goarch: 386
- goos: darwin
goarch: arm
- goos: windows
goarch: 386
- goos: windows
goarch: arm
- goos: windows
goarch: arm64
- goos: freebsd
goarch: arm

archives:
-
name_template: "{{ .ProjectName }}_{{ .Os }}_{{ .Arch }}"
replacements:
darwin: macos
386: i386
wrap_in_directory: true
format: zip

checksum:
name_template: "{{ .ProjectName }}_checksums.txt"

changelog:
sort: desc
filters:
exclude:
- '^MERGE'
- "{{ .Tag }}"

release:
github:
owner: 5amu
name: dnshunter
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# ~# dnshunter.sh

Make DNS and BGP assessment easier. Just a script to perform many DNS checks
automatically. It should be improved and perfected. Right now is pretty
accurate, but it might need more refinement.
Make DNS and BGP assessment easier. This implementation in Go grants more speed
It is unfinished, but the old script can be found in [the legacy folder](/legacy)
Just a program to perform many DNS checks automatically. It should be improved and
perfected. Right now is pretty accurate, but it might need more refinement.
84 changes: 84 additions & 0 deletions cmd/dnshunter/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package main

import (
"flag"
"fmt"
"os"

"github.com/5amu/dnshunter/internal/common"
)

func banner() {
fmt.Println("")
fmt.Println(common.Banner(" ·▄▄▄▄ ▐ ▄ .▄▄ · ▄ .▄▄• ▄▌ ▐ ▄ ▄▄▄▄▄▄▄▄ .▄▄▄ "))
fmt.Println(common.Banner(" ██▪ ██ •█▌▐█▐█ ▀. ██▪▐██▪██▌•█▌▐█•██ ▀▄.▀·▀▄ █· "))
fmt.Println(common.Banner(" ▐█· ▐█▌▐█▐▐▌▄▀▀▀█▄██▀▐██▌▐█▌▐█▐▐▌ ▐█.▪▐▀▀▪▄▐▀▀▄ "))
fmt.Println(common.Banner(" ██. ██ ██▐█▌▐█▄▪▐███▌▐▀▐█▄█▌██▐█▌ ▐█▌·▐█▄▄▌▐█•█▌ "))
fmt.Println(common.Banner(" ▀▀▀▀▀• ▀▀ █▪ ▀▀▀▀ ▀▀▀ · ▀▀▀ ▀▀ █▪ ▀▀▀ ▀▀▀ .▀ ▀ "))
fmt.Println(common.Banner(" -by 5amu (https://github.com/5amu)"))
fmt.Println("")
}

func usage() {
fmt.Println("Usage: dnshunter -h|-v [-o <outfile>] [-n <ns-file>] <domain>")
fmt.Println("")
fmt.Println("OPTIONS:")
fmt.Println(" -h|-help show the program usage and exit")
fmt.Println(" -v|-version show the program version and exit")
fmt.Println(" -o save output in JSON format")
fmt.Println(" -n file with nameservers (line separated)")
fmt.Println("")
fmt.Println("POSITIONAL:")
fmt.Println("")
fmt.Println(" <domain> target domain")
fmt.Println("")
}

func main() {

var help1, help2, vers1, vers2 bool
var outfile, nsfile string

mainFlagSet := flag.NewFlagSet("dnshunter", flag.ContinueOnError)
mainFlagSet.BoolVar(&help1, "h", false, "")
mainFlagSet.BoolVar(&help2, "help", false, "show the program usage and exit")
mainFlagSet.BoolVar(&vers1, "v", false, "")
mainFlagSet.BoolVar(&vers2, "version", false, "show the program version and exit")
mainFlagSet.StringVar(&outfile, "o", "", "save output in JSON format")
mainFlagSet.StringVar(&nsfile, "n", "", "file with nameservers (line separated)")

banner()

if err := mainFlagSet.Parse(os.Args[1:]); err != nil {
fmt.Println(common.Error(fmt.Sprintf("%v", err)))
os.Exit(1)
}

if len(os.Args) < 2 {
usage()
fmt.Println(common.Error("not enough arguments"))
os.Exit(1)
}

if help1 || help2 {
usage()
os.Exit(0)
}

if vers1 || vers2 {
fmt.Println(common.Error(fmt.Sprintf("version %v\n", common.DNSHunterVersion)))
os.Exit(0)
}

if len(mainFlagSet.Args()) != 1 {
usage()
fmt.Println(common.Error("please, specify a target domain"))
os.Exit(1)
}

domain := mainFlagSet.Arg(0)
if err := run(outfile, nsfile, domain); err != nil {
fmt.Println(common.Error(fmt.Sprintf("%v", err)))
os.Exit(1)
}
}
78 changes: 78 additions & 0 deletions cmd/dnshunter/runner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package main

import (
"encoding/json"
"fmt"
"os"
"sync"

"github.com/5amu/dnshunter/internal"
"github.com/5amu/dnshunter/internal/common"
"github.com/5amu/dnshunter/internal/output"
"github.com/miekg/dns"
)

func run(outfile string, nsfile string, domain string) error {
c := new(dns.Client)

var err error
var nameservers *common.Nameservers
if nsfile != "" {
nameservers, err = common.NewNameserversFromFile(nsfile)
} else {
nameservers, err = common.NewNameserversFromDomain(domain)
}
if err != nil {
return err
}

initialInfo := common.Info(fmt.Sprintf("scanning domain : %v\n", domain))
initialInfo += common.Info(fmt.Sprintf("using nameservers : %v\n", nameservers.ToFQDNs()))
initialInfo += common.Info(fmt.Sprintf("with IPv4 version : %v\n", nameservers.ToIPv4()))
if outfile != "" {
initialInfo += common.Info(fmt.Sprintf("saving output to : %v\n", outfile))
} else {
initialInfo += common.Info("saving output to : /dev/null\n")
}
fmt.Println(initialInfo)

var wg sync.WaitGroup
resChan := make(chan *output.CheckOutput, 1)
for _, check := range internal.CheckList {
wg.Add(1)
go func(ch internal.Check) {
ch.Init(c)
if err := ch.Start(domain, nameservers); err != nil {
fmt.Println(common.Error(fmt.Sprintf("check failed with error: %v", err)))
}
resChan <- ch.Results()
wg.Done()
}(check)
}

done := make(chan struct{}, 1)
go func() {
wg.Wait()
done <- struct{}{}
}()

var results []*output.CheckOutput
for {
select {
case <-done:
if outfile != "" {
if data, err := json.Marshal(results); err != nil {
return err
} else {
if err := os.WriteFile(outfile, data, 0644); err != nil {
return err
}
}
}
return nil
case r := <-resChan:
fmt.Println(r)
results = append(results, r)
}
}
}
13 changes: 13 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module github.com/5amu/dnshunter

go 1.18

require github.com/miekg/dns v1.1.50

require (
golang.org/x/mod v0.4.2 // indirect
golang.org/x/net v0.0.0-20210726213435-c6fcb2dbf985 // indirect
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
golang.org/x/tools v0.1.6-0.20210726203631-07bc1bf47fb2 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
)
35 changes: 35 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
github.com/miekg/dns v1.1.50 h1:DQUfb9uc6smULcREF09Uc+/Gd46YWqJd5DbpPE9xkcA=
github.com/miekg/dns v1.1.50/go.mod h1:e3IlAVfNqAllflbibAZEWOXOQ+Ynzk/dDozDxY7XnME=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/mod v0.4.2 h1:Gz96sIWK3OalVv/I/qNygP42zyoKp3xptRVCWRFEBvo=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/net v0.0.0-20210726213435-c6fcb2dbf985 h1:4CSI6oo7cOjJKajidEljs9h+uP0rRZBPPPhcCbj5mw8=
golang.org/x/net v0.0.0-20210726213435-c6fcb2dbf985/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.6-0.20210726203631-07bc1bf47fb2 h1:BonxutuHCTL0rBDnZlKjpGIQFTjyUVTexFOdWkB6Fg0=
golang.org/x/tools v0.1.6-0.20210726203631-07bc1bf47fb2/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
1 change: 1 addition & 0 deletions internal/bgpchecks/georedundancy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package bgpchecks
1 change: 1 addition & 0 deletions internal/bgpchecks/irr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package bgpchecks
1 change: 1 addition & 0 deletions internal/bgpchecks/roa.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package bgpchecks
19 changes: 19 additions & 0 deletions internal/check.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package internal

import (
"github.com/5amu/dnshunter/internal/common"
"github.com/5amu/dnshunter/internal/dnschecks"
"github.com/5amu/dnshunter/internal/output"
"github.com/miekg/dns"
)

type Check interface {
Init(client *dns.Client) error
Start(domain string, ns *common.Nameservers) error
Results() *output.CheckOutput
}

var CheckList = []Check{
new(dnschecks.SOACheck),
new(dnschecks.GLUECheck),
}
4 changes: 4 additions & 0 deletions internal/common/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package common

const DNSHunterVersion = "0.1"
const DefaultNameserver = "8.8.8.8"
45 changes: 45 additions & 0 deletions internal/common/io.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package common

import "fmt"

const (
BannerLevel = "banner"
ErrorLevel = "high"
WarnLevel = "warning"
HeaderLevel = "header"
Reset = "reset"
InfoLevel = "info"
)

var colors = map[string]string{
"banner": "\033[0;35m", // purple
"high": "\033[0;31m", // red
"warning": "\033[1;33m", // yellow
"info": "\033[0;36m", // cyan
"header": "\033[0;32m", // green
"reset": "\033[0m", // reset
}

func generic(s string, color string) string {
return fmt.Sprintf("%v%v%v", color, s, colors[Reset])
}

func Banner(s string) string {
return generic(s, colors[BannerLevel])
}

func Info(s string) string {
return generic(s, colors[InfoLevel])
}

func Warn(s string) string {
return generic(fmt.Sprintf("[WARNING]: %v", s), colors[WarnLevel])
}

func Header(s string) string {
return generic(fmt.Sprintf("[!] %v [!]", s), colors[HeaderLevel])
}

func Error(s string) string {
return generic(fmt.Sprintf("[ERROR]: %v", s), colors[ErrorLevel])
}
Loading

0 comments on commit 97d260f

Please sign in to comment.