forked from bettercap/bettercap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dns_spoof_hosts.go
82 lines (67 loc) · 1.49 KB
/
dns_spoof_hosts.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
package dns_spoof
import (
"bufio"
"net"
"os"
"regexp"
"strings"
"github.com/gobwas/glob"
"github.com/evilsocket/islazy/str"
)
var hostsSplitter = regexp.MustCompile(`\s+`)
type HostEntry struct {
Host string
Suffix string
Expr glob.Glob
Address net.IP
}
func (e HostEntry) Matches(host string) bool {
return e.Host == host || strings.HasSuffix(host, e.Suffix) || (e.Expr != nil && e.Expr.Match(host))
}
type Hosts []HostEntry
func NewHostEntry(host string, address net.IP) HostEntry {
entry := HostEntry{
Host: host,
Address: address,
}
if host[0] == '.' {
entry.Suffix = host
} else {
entry.Suffix = "." + host
}
if expr, err := glob.Compile(host); err == nil {
entry.Expr = expr
}
return entry
}
func HostsFromFile(filename string, defaultAddress net.IP) (err error, entries []HostEntry) {
input, err := os.Open(filename)
if err != nil {
return
}
defer input.Close()
scanner := bufio.NewScanner(input)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
line := str.Trim(scanner.Text())
if line == "" || line[0] == '#' {
continue
}
if parts := hostsSplitter.Split(line, 2); len(parts) == 2 {
address := net.ParseIP(parts[0])
domain := parts[1]
entries = append(entries, NewHostEntry(domain, address))
} else {
entries = append(entries, NewHostEntry(line, defaultAddress))
}
}
return
}
func (h Hosts) Resolve(host string) net.IP {
for _, entry := range h {
if entry.Matches(host) {
return entry.Address
}
}
return nil
}