mirror of
https://github.com/bettercap/bettercap
synced 2025-08-19 21:13:18 -07:00
Update firewall_linux.go
This commit is contained in:
parent
18c5c5b66e
commit
6939b9a0cc
1 changed files with 19 additions and 117 deletions
|
@ -8,7 +8,6 @@ import (
|
||||||
|
|
||||||
"github.com/bettercap/bettercap/v2/core"
|
"github.com/bettercap/bettercap/v2/core"
|
||||||
"github.com/bettercap/bettercap/v2/network"
|
"github.com/bettercap/bettercap/v2/network"
|
||||||
|
|
||||||
"github.com/evilsocket/islazy/fs"
|
"github.com/evilsocket/islazy/fs"
|
||||||
"github.com/evilsocket/islazy/str"
|
"github.com/evilsocket/islazy/str"
|
||||||
)
|
)
|
||||||
|
@ -32,143 +31,46 @@ func Make(iface *network.Endpoint) FirewallManager {
|
||||||
restore: false,
|
restore: false,
|
||||||
redirections: make(map[string]*Redirection),
|
redirections: make(map[string]*Redirection),
|
||||||
}
|
}
|
||||||
|
|
||||||
firewall.forwarding = firewall.IsForwardingEnabled()
|
firewall.forwarding = firewall.IsForwardingEnabled()
|
||||||
|
|
||||||
return firewall
|
return firewall
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f LinuxFirewall) enableFeature(filename string, enable bool) error {
|
func (f *LinuxFirewall) enableFeature(filename string, enable bool) error {
|
||||||
var value string
|
value := "0"
|
||||||
if enable {
|
if enable {
|
||||||
value = "1"
|
value = "1"
|
||||||
} else {
|
|
||||||
value = "0"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fd, err := os.OpenFile(filename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
|
fd, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY, 0600)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return fmt.Errorf("failed to open file %s: %w", filename, err)
|
||||||
}
|
}
|
||||||
defer fd.Close()
|
defer fd.Close()
|
||||||
|
|
||||||
_, err = fd.WriteString(value)
|
if _, err := fd.WriteString(value); err != nil {
|
||||||
return err
|
return fmt.Errorf("failed to write to file %s: %w", filename, err)
|
||||||
}
|
|
||||||
|
|
||||||
func (f LinuxFirewall) IsForwardingEnabled() bool {
|
|
||||||
|
|
||||||
if out, err := ioutil.ReadFile(IPV4ForwardingFile); err != nil {
|
|
||||||
return false
|
|
||||||
} else {
|
|
||||||
return str.Trim(string(out)) == "1"
|
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f LinuxFirewall) EnableForwarding(enabled bool) error {
|
func (f *LinuxFirewall) IsForwardingEnabled() bool {
|
||||||
|
content, err := ioutil.ReadFile(IPV4ForwardingFile)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return str.Trim(string(content)) == "1"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *LinuxFirewall) EnableForwarding(enabled bool) error {
|
||||||
if err := f.enableFeature(IPV4ForwardingFile, enabled); err != nil {
|
if err := f.enableFeature(IPV4ForwardingFile, enabled); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if fs.Exists(IPV6ForwardingFile) {
|
if fs.Exists(IPV6ForwardingFile) {
|
||||||
return f.enableFeature(IPV6ForwardingFile, enabled)
|
if err := f.enableFeature(IPV6ForwardingFile, enabled); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
f.restore = true
|
f.restore = true
|
||||||
return nil
|
return nil
|
||||||
}
|
|
||||||
|
|
||||||
func (f *LinuxFirewall) getCommandLine(r *Redirection, enabled bool) (cmdLine []string) {
|
|
||||||
action := "-A"
|
|
||||||
destination := ""
|
|
||||||
|
|
||||||
if !enabled {
|
|
||||||
action = "-D"
|
|
||||||
}
|
|
||||||
|
|
||||||
if strings.Count(r.DstAddress, ":") < 2 {
|
|
||||||
destination = r.DstAddress
|
|
||||||
} else {
|
|
||||||
destination = fmt.Sprintf("[%s]", r.DstAddress)
|
|
||||||
}
|
|
||||||
|
|
||||||
if r.SrcAddress == "" {
|
|
||||||
cmdLine = []string{
|
|
||||||
"-t", "nat",
|
|
||||||
action, "PREROUTING",
|
|
||||||
"-i", r.Interface,
|
|
||||||
"-p", r.Protocol,
|
|
||||||
"--dport", fmt.Sprintf("%d", r.SrcPort),
|
|
||||||
"-j", "DNAT",
|
|
||||||
"--to", fmt.Sprintf("%s:%d", destination, r.DstPort),
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
cmdLine = []string{
|
|
||||||
"-t", "nat",
|
|
||||||
action, "PREROUTING",
|
|
||||||
"-i", r.Interface,
|
|
||||||
"-p", r.Protocol,
|
|
||||||
"-d", r.SrcAddress,
|
|
||||||
"--dport", fmt.Sprintf("%d", r.SrcPort),
|
|
||||||
"-j", "DNAT",
|
|
||||||
"--to", fmt.Sprintf("%s:%d", destination, r.DstPort),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *LinuxFirewall) EnableRedirection(r *Redirection, enabled bool) error {
|
|
||||||
cmdLine := f.getCommandLine(r, enabled)
|
|
||||||
rkey := r.String()
|
|
||||||
_, found := f.redirections[rkey]
|
|
||||||
cmd := ""
|
|
||||||
|
|
||||||
if strings.Count(r.DstAddress, ":") < 2 {
|
|
||||||
cmd = "iptables"
|
|
||||||
} else {
|
|
||||||
cmd = "ip6tables"
|
|
||||||
}
|
|
||||||
|
|
||||||
if enabled {
|
|
||||||
if found {
|
|
||||||
return fmt.Errorf("Redirection '%s' already enabled.", rkey)
|
|
||||||
}
|
|
||||||
|
|
||||||
f.redirections[rkey] = r
|
|
||||||
|
|
||||||
// accept all
|
|
||||||
if _, err := core.Exec(cmd, []string{"-P", "FORWARD", "ACCEPT"}); err != nil {
|
|
||||||
return err
|
|
||||||
} else if _, err := core.Exec(cmd, cmdLine); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if !found {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
delete(f.redirections, r.String())
|
|
||||||
|
|
||||||
if _, err := core.Exec(cmd, cmdLine); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f LinuxFirewall) Restore() {
|
|
||||||
if f.restore == false {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
for _, r := range f.redirections {
|
|
||||||
if err := f.EnableRedirection(r, false); err != nil {
|
|
||||||
fmt.Printf("%s", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := f.EnableForwarding(f.forwarding); err != nil {
|
|
||||||
fmt.Printf("%s", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue