forked from kgretzky/evilginx2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nameserver.go
100 lines (85 loc) · 2.2 KB
/
nameserver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package core
import (
"context"
"fmt"
"net"
"strconv"
"strings"
"time"
"github.com/miekg/dns"
"github.com/kgretzky/evilginx2/log"
)
type Nameserver struct {
srv *dns.Server
cfg *Config
bind string
serial uint32
ctx context.Context
}
func NewNameserver(cfg *Config) (*Nameserver, error) {
o := &Nameserver{
serial: uint32(time.Now().Unix()),
cfg: cfg,
bind: fmt.Sprintf("%s:%d", cfg.GetServerBindIP(), cfg.GetDnsPort()),
ctx: context.Background(),
}
o.Reset()
return o, nil
}
func (o *Nameserver) Reset() {
dns.HandleFunc(pdom(o.cfg.general.Domain), o.handleRequest)
}
func (o *Nameserver) Start() {
go func() {
o.srv = &dns.Server{Addr: o.bind, Net: "udp"}
if err := o.srv.ListenAndServe(); err != nil {
log.Fatal("Failed to start nameserver on: %s", o.bind)
}
}()
}
func (o *Nameserver) handleRequest(w dns.ResponseWriter, r *dns.Msg) {
m := new(dns.Msg)
m.SetReply(r)
if o.cfg.general.Domain == "" || o.cfg.general.ExternalIpv4 == "" {
return
}
soa := &dns.SOA{
Hdr: dns.RR_Header{Name: pdom(o.cfg.general.Domain), Rrtype: dns.TypeSOA, Class: dns.ClassINET, Ttl: 300},
Ns: "ns1." + pdom(o.cfg.general.Domain),
Mbox: "hostmaster." + pdom(o.cfg.general.Domain),
Serial: o.serial,
Refresh: 900,
Retry: 900,
Expire: 1800,
Minttl: 60,
}
m.Ns = []dns.RR{soa}
fqdn := strings.ToLower(r.Question[0].Name)
switch r.Question[0].Qtype {
case dns.TypeSOA:
log.Debug("DNS SOA: " + fqdn)
m.Answer = append(m.Answer, soa)
case dns.TypeA:
log.Debug("DNS A: " + fqdn + " = " + o.cfg.general.ExternalIpv4)
rr := &dns.A{
Hdr: dns.RR_Header{Name: fqdn, Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 300},
A: net.ParseIP(o.cfg.general.ExternalIpv4),
}
m.Answer = append(m.Answer, rr)
case dns.TypeNS:
log.Debug("DNS NS: " + fqdn)
if fqdn == pdom(o.cfg.general.Domain) {
for _, i := range []int{1, 2} {
rr := &dns.NS{
Hdr: dns.RR_Header{Name: pdom(o.cfg.general.Domain), Rrtype: dns.TypeNS, Class: dns.ClassINET, Ttl: 300},
Ns: "ns" + strconv.Itoa(i) + "." + pdom(o.cfg.general.Domain),
}
m.Answer = append(m.Answer, rr)
}
}
}
w.WriteMsg(m)
}
func pdom(domain string) string {
return domain + "."
}