Skip to content

Commit

Permalink
#9 bgp georedundancy check added
Browse files Browse the repository at this point in the history
  • Loading branch information
5amu committed Jul 10, 2022
1 parent f667d55 commit 0afbcdd
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 7 deletions.
11 changes: 7 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@ module github.com/5amu/dnshunter

go 1.18

require github.com/miekg/dns v1.1.50
require (
github.com/likexian/whois v1.14.2
github.com/miekg/dns v1.1.50
github.com/projectdiscovery/gologger v1.1.4
)

require (
github.com/json-iterator/go v1.1.10 // indirect
github.com/logrusorgru/aurora v2.0.3+incompatible // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/projectdiscovery/gologger v1.1.4 // indirect
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/net v0.0.0-20220708220712-1185a9018129 // indirect
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
golang.org/x/tools v0.1.6-0.20210726203631-07bc1bf47fb2 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
)
13 changes: 11 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
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/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/json-iterator/go v1.1.10 h1:Kz6Cvnvv2wGdaG/V8yMvfkmNiXq9Ya2KUv4rouJJr68=
Expand All @@ -8,6 +9,9 @@ github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfn
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/likexian/gokit v0.25.8 h1:YL5yxnUxVMmJ5PRrMw3Dgr9gNidjXitdPs0/69T4PX0=
github.com/likexian/whois v1.14.2 h1:RFtXK/2PSgl6vG1beXEwB2zCkwUWhy7A9zh258iQTqg=
github.com/likexian/whois v1.14.2/go.mod h1:uEy9dUtYzjm9aSu9Tzbp+c1YEmyjQC90tYWudwvunFk=
github.com/logrusorgru/aurora v2.0.3+incompatible h1:tOpm7WcpBTn4fjmVfgpQq0EfczGlG91VSDkswnjF5A8=
github.com/logrusorgru/aurora v2.0.3+incompatible/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4=
github.com/miekg/dns v1.1.50 h1:DQUfb9uc6smULcREF09Uc+/Gd46YWqJd5DbpPE9xkcA=
Expand All @@ -18,11 +22,13 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJ
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI=
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
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/projectdiscovery/gologger v1.1.4 h1:qWxGUq7ukHWT849uGPkagPKF3yBPYAsTtMKunQ8O2VI=
github.com/projectdiscovery/gologger v1.1.4/go.mod h1:Bhb6Bdx2PV1nMaFLoXNBmHIU85iROS9y1tBuv7T5pMY=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
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=
Expand All @@ -32,8 +38,9 @@ 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/net v0.0.0-20220708220712-1185a9018129 h1:vucSRfWwTsoXro7P+3Cjlr6flUMtzCwzlvkxEQtHHB0=
golang.org/x/net v0.0.0-20220708220712-1185a9018129/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
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=
Expand All @@ -43,8 +50,9 @@ golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7w
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/sys v0.0.0-20220520151302-bc2c85ada10a h1:dGzPydgVsqGcTRVwiLJ1jVbufYwmzD3LfVPLKsKg+0k=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/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=
Expand All @@ -60,4 +68,5 @@ golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8T
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
88 changes: 88 additions & 0 deletions pkg/checks/bgpchecks/georedundancy.go
Original file line number Diff line number Diff line change
@@ -1 +1,89 @@
package bgpchecks

import (
"fmt"
"strings"

"github.com/5amu/dnshunter/pkg/output"
"github.com/5amu/dnshunter/pkg/utils"
"github.com/miekg/dns"
)

type GEOCkeck struct {
description []string
client *dns.Client
output *output.CheckOutput
}

func (c *GEOCkeck) Init(client *dns.Client) error {
c.client = client
c.description = []string{
"For, ASNs, one important thing to check is georedundancy",
"Always check the geographic zone in which an ASN is located.",
"This is important to guarantee availability.",
}
return nil
}

func (c *GEOCkeck) Start(domain string, nameservers *utils.Nameservers) error {
c.output = &output.CheckOutput{
Name: "BGP Georedundancy",
Domain: domain,
Nameservers: nameservers.FQDNs,
Description: c.description,
}

locations := map[string]int{}
for _, fqdn := range nameservers.FQDNs {
var res output.SingleCheckResult
res.Nameserver = fqdn
res.Zone = domain

asn, err := utils.NewASN(nameservers.GetIP(fqdn))
if err != nil {
continue
}

nameSplitted := strings.Split(asn.Name, ",")
if len(nameSplitted) > 1 {
key := strings.ReplaceAll(nameSplitted[1], " ", "")
locations[key] += 1
}
c.output.Results = append(c.output.Results, res)
}

var totalASN, totalGeo int
var countries []string
for k, v := range locations {
totalASN += v
totalGeo += 1
countries = append(countries, k)
}

if totalASN < 2 {
for i := range c.output.Results {
c.output.Results[i].Vulnerable = true
c.output.Results[i].Information = append(c.output.Results[i].Information, "bad georedundancy: 1 location")
}
}

if totalASN/2 > totalGeo {
for i := range c.output.Results {
c.output.Results[i].Vulnerable = true
msg := "ideally, you should have every 1 or 2 ASNs in different countries"
c.output.Results[i].Information = append(c.output.Results[i].Information, msg)
}
}

for i := range c.output.Results {
msg := fmt.Sprintf("%d distrubuted over %d locations", totalASN, totalGeo)
c.output.Results[i].Information = append(c.output.Results[i].Information, msg)
msg = fmt.Sprintf("countries: %v", countries)
c.output.Results[i].Information = append(c.output.Results[i].Information, msg)
}
return nil
}

func (c *GEOCkeck) Results() *output.CheckOutput {
return c.output
}
4 changes: 3 additions & 1 deletion pkg/checks/check.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package checks

import (
"github.com/5amu/dnshunter/pkg/checks/bgpchecks"
"github.com/5amu/dnshunter/pkg/checks/dnschecks"
"github.com/5amu/dnshunter/pkg/output"
"github.com/5amu/dnshunter/pkg/utils"
Expand Down Expand Up @@ -46,7 +47,7 @@ func NewCheck(id string) Check {
case DKIM:
return new(dnschecks.DKIMCheck)
case GEO:
return nil
return new(bgpchecks.GEOCkeck)
case IRR:
return nil
case ROA:
Expand All @@ -66,5 +67,6 @@ func AllChecks() []Check {
new(dnschecks.SPFCheck),
new(dnschecks.DKIMCheck),
new(dnschecks.DMARCCheck),
new(bgpchecks.GEOCkeck),
}
}
2 changes: 2 additions & 0 deletions pkg/defaults/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ const (
// DNSAmplificationThreshold is an arbitrary number that the programmer
// considered to be enough for "response considerably larger than request"
DNSAmplificationThreshold = 5
// DefaultWhoisServer - default whois server
DefaultWhoisServer = "whois.cymru.com"
)
38 changes: 38 additions & 0 deletions pkg/utils/asn.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package utils

import (
"fmt"
"net"
"strings"

"github.com/5amu/dnshunter/pkg/defaults"
"github.com/likexian/whois"
)

type ASN struct {
ID string
IP net.IP
Name string
}

func NewASN(nameserver net.IP) (*ASN, error) {
r, err := whois.Whois(nameserver.String(), defaults.DefaultWhoisServer)
if err != nil {
return nil, err
}

lines := strings.Split(r, "\n")
if len(lines) < 2 {
return nil, fmt.Errorf("no result found")
}

fields := strings.Split(strings.TrimSpace(lines[1]), "|")
if len(fields) != 3 {
return nil, fmt.Errorf("not enough fields")
}
return &ASN{
ID: fields[0],
IP: net.ParseIP(strings.ReplaceAll(fields[1], " ", "")),
Name: fields[2],
}, nil
}

0 comments on commit 0afbcdd

Please sign in to comment.