-
Notifications
You must be signed in to change notification settings - Fork 4
/
cmd.go
249 lines (193 loc) · 4.74 KB
/
cmd.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package cmd
//cmdroot
import (
"bufio"
"flag"
"os"
"time"
"github.com/briandowns/spinner"
"github.com/cheggaaa/pb/v3"
"github.com/moorada/neferpitool/pkg/configuration"
"github.com/moorada/neferpitool/pkg/scanner"
"github.com/moorada/neferpitool/pkg/console"
"github.com/moorada/neferpitool/pkg/db"
"github.com/moorada/neferpitool/pkg/domains"
"github.com/moorada/neferpitool/pkg/generator"
"github.com/moorada/neferpitool/pkg/log"
)
var totaltd int
const PathConfigFolder = "./config"
func CmdRoot() {
//init log
if err := log.ActiveConsoleLog(); err != nil {
panic(err)
} else {
defer log.Close()
}
err := os.MkdirAll(PathConfigFolder, os.ModePerm)
if err != nil {
log.Error(err.Error())
}
//init db
db.InitDB("config/database")
defer db.CloseDB()
// init flags
logs := flag.Bool("logs", false, "Avtive logs on file")
singleTd := flag.String("td", "", "Manage one typodomain")
bg := flag.Bool("bg", false, "Active monitoring in background")
pd := flag.Bool("pd", false, "Check if domains are present")
makeConfig := flag.Bool("mc", false, "Make config file")
importTds := flag.String("it", "", "Import Typos from file - main domain")
pathImportTds := flag.String("p", "", "Import Typos from file - path of the file")
flag.Parse()
if *logs {
if err := log.ActiveDebugLog(); err != nil {
panic(err)
}
}
if *makeConfig {
configuration.MakeConfigFile()
return
}
logDegubInfo()
if *bg {
background()
return
}
if *singleTd != "" {
SingleTdMode(*singleTd)
return
}
if *importTds != "" {
if *pathImportTds != "" {
importTypos(*importTds, *pathImportTds)
return
} else {
log.Error("%s", "Specify the path of the import file: -p /path/example/ ")
}
}
args := flag.Args()
if len(args) < 1 {
MonitorCmd()
return
}
if *pd {
presenceDomains(args)
return
}
if len(args) == 1 {
domain := args[0]
SingleDomainMode(domain)
} else {
multipleDomainsMode(args)
}
os.Exit(0)
}
func presenceDomains(domains []string) {
ds := db.GetMainDomainListFromDB()
dsMap := map[string]bool{}
for _, d := range ds {
dsMap[d.Name] = true
}
for _, s := range domains {
if _, ok := dsMap[s]; ok {
log.Info("%s present", s)
} else {
log.Info("%s NOT present", s)
}
}
}
func multipleDomainsMode(domains []string) {
for _, d := range domains {
addDomainAndHisTypos(d)
console.PrintTableTypoDomains(db.GetTypoDomainListFromDB(d))
}
}
func logDegubInfo() {
// debug info...
totaltd = 0
totald := db.GetMainDomainListFromDB()
domains := ""
for _, d := range totald {
td1d := db.GetTypoDomainListFromDB(d.Name)
domains += d.Name + ", "
totaltd = totaltd + len(td1d)
}
log.Debug("%v domains in db: %s", len(totald), domains)
log.Debug("Number of typodomains in db: %v", totaltd)
//
}
func UpdateTypoDomainsWithProgressBar(tds domains.TypoList) map[string]error {
c := make(chan int, len(tds))
bar := pb.Full.Start(len(tds))
var errs map[string]error
go func(map[string]error) {
errs = scanner.UpdateTypoDomains(tds, c)
}(errs)
for range tds {
<-c
bar.Increment()
}
bar.Finish()
if len(tds) > 0 {
log.Debug("Stats Scansion, Typodomains: %v, Errors: %v, Percentage of errors: %v", len(tds), len(errs), len(errs)*100/len(tds))
} else {
log.Debug("no typodomains")
}
return errs
}
func addDomainAndHisTypos(domain string) {
s := spinner.New(spinner.CharSets[26], 200*time.Millisecond)
s.Prefix = "Generating typodomains "
s.Start()
tds := generator.GetUnfilledTypoDomains(domain)
s.Stop()
errs := UpdateTypoDomainsWithProgressBar(tds)
if len(errs) != 0 {
console.PrintTableErrs(errs)
}
db.AddTypoListToDB(tds)
md := domains.NewLegitDomain(domain)
err := md.Update()
if err != nil {
log.Error("%s, error: %s", domain, err.Error())
}
db.AddLegitDomainToDB(md)
log.Info("Typodomains added to database")
}
func importTypos(domain string, path string) {
s := spinner.New(spinner.CharSets[26], 200*time.Millisecond)
s.Prefix = "Importing typodomains "
s.Start()
var tds domains.TypoList
file, err := os.Open(path)
if err != nil {
log.Error("Importing typos from file, error: %s", domain, err.Error())
}
defer file.Close()
scanner := bufio.NewScanner(file)
var typos []string
for scanner.Scan() {
typos = append(typos, scanner.Text())
}
if err := scanner.Err(); err != nil {
log.Error("Importing typos from file, error: %s", domain, err.Error())
}
if len(typos) > 0 {
for _, t := range typos {
if t != "" {
td := domains.NewTypoDomain(t, domain, "imported")
tds = append(tds, td)
}
}
} else {
log.Error("Empty file, error: %s", domain, err.Error())
}
s.Stop()
errs := UpdateTypoDomainsWithProgressBar(tds)
if len(errs) != 0 {
console.PrintTableErrs(errs)
}
db.AddTypoListToDB(tds)
log.Info("Typodomains imported")
}