-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
170 lines (137 loc) · 3.23 KB
/
main.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package main
import (
"bufio"
"encoding/json"
"flag"
"fmt"
"io"
"os"
"github.com/m-m-adams/squatcobbler/attacks"
"github.com/m-m-adams/squatcobbler/domain"
)
type domaincheck struct {
result domain.AttackDomain
err error
}
func getDomainInfo(modified domain.Domain, whoisLookup bool, c chan domaincheck) {
att, err := domain.AttackFromURL(modified, whoisLookup)
c <- domaincheck{att, err}
}
func unique(input []domain.Domain) (output []domain.Domain) {
inmap := make(map[string]domain.Domain)
for _, dom := range input {
url, _ := dom.ToURL()
inmap[url] = dom
}
for _, v := range inmap {
output = append(output, v)
}
return
}
func generateTypos(input []string) (uniquetypos []domain.Domain) {
squatted := []domain.Domain{}
for _, inputdomain := range input {
original, err := domain.FromURL(inputdomain)
if err != nil {
fmt.Println(err)
} else {
for _, attack := range attacks.All {
squatted = append(squatted, attack(original)...)
}
}
}
uniquetypos = unique(squatted)
return
}
func typosquatter(input []string, output *bufio.Writer, whoisLookup bool) (code int) {
uniquetypos := generateTypos(input)
output.WriteString("[")
results := make(chan domaincheck)
counter := 0
for _, typo := range uniquetypos {
counter++
go getDomainInfo(typo, whoisLookup, results)
}
domainArr := make([]domain.AttackDomain, counter)
for i := 0; i < counter; i++ {
r := <-results
domainArr[i] = r.result
}
for i, d := range domainArr {
fmt.Println(d)
if len(d.IPaddr) > 0 {
att, _ := json.MarshalIndent(d, "", " ")
var err error
if i == (len(domainArr) - 1) {
_, err = output.WriteString(string(att))
if err != nil {
fmt.Println(err)
return -1
}
} else {
_, err = output.WriteString(string(att) + ",")
if err != nil {
fmt.Println(err)
return -1
}
}
if err != nil {
fmt.Println(err)
}
}
}
output.WriteString("]\n")
if output.Buffered() < 4 {
return -1
}
output.Flush()
return 0
}
func readLines(path string) ([]string, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines, scanner.Err()
}
func main() {
inputlist := flag.String("i", "", "a file to read input from or a single domain input")
outputfile := flag.String("o", "", "a file to write output to, otherwise goes to stdout")
whoisLookup := flag.Bool("whois", false, "if true lookup whois for all domains")
flag.Parse()
var domains []string
if len(*inputlist) == 0 {
fmt.Println("No input domains given")
os.Exit(-1)
}
if _, err := os.Stat(*inputlist); os.IsNotExist(err) {
domains = []string{*inputlist}
} else {
domains, err = readLines(*inputlist)
if err != nil {
fmt.Println(err)
panic(err)
}
}
var output io.Writer
if len(*outputfile) == 0 {
output = os.Stdout
writer := bufio.NewWriter(output)
os.Exit(typosquatter(domains, writer, *whoisLookup))
} else {
output, err := os.Create(*outputfile)
if err != nil {
fmt.Println(err)
panic(err)
}
defer output.Close()
writer := bufio.NewWriter(output)
os.Exit(typosquatter(domains, writer, *whoisLookup))
}
}