Debian Codegraph @

firewall/firewall_windows.go

raw symbols SCIP
1 package firewall
2
3 import (
4 "fmt"
5 "strings"
6
7 "github.com/bettercap/bettercap/core"
8 "github.com/bettercap/bettercap/network"
9 )
10
11 type WindowsFirewall struct {
12 iface *network.Endpoint
13 forwarding bool
14 redirections map[string]*Redirection
15 }
16
17 func Make(iface *network.Endpoint) FirewallManager {
18 firewall := &WindowsFirewall{
19 iface: iface,
20 forwarding: false,
21 redirections: make(map[string]*Redirection, 0),
22 }
23
24 firewall.forwarding = firewall.IsForwardingEnabled()
25
26 return firewall
27 }
28
29 func (f WindowsFirewall) IsForwardingEnabled() bool {
30 if out, err := core.Exec("netsh", []string{"interface", "ipv4", "dump"}); err != nil {
31 fmt.Printf("%s\n", err)
32 return false
33 } else {
34 return strings.Contains(out, "forwarding=enabled")
35 }
36 }
37
38 func (f WindowsFirewall) EnableForwarding(enabled bool) error {
39 v := "enabled"
40 if enabled == false {
41 v = "disabled"
42 }
43
44 if _, err := core.Exec("netsh", []string{"interface", "ipv4", "set", "interface", fmt.Sprintf("%d", f.iface.Index), fmt.Sprintf("forwarding=\"%s\"", v)}); err != nil {
45 return err
46 }
47
48 return nil
49 }
50
51 func (f WindowsFirewall) generateRule(r *Redirection, enabled bool) []string {
52 // https://stackoverflow.com/questions/24646165/netsh-port-forwarding-from-local-port-to-local-port-not-working
53 rule := []string{
54 fmt.Sprintf("listenport=%d", r.SrcPort),
55 }
56
57 if enabled {
58 rule = append(rule, fmt.Sprintf("connectport=%d", r.DstPort))
59 rule = append(rule, fmt.Sprintf("connectaddress=%s", r.DstAddress))
60 rule = append(rule, fmt.Sprintf("protocol=%s", r.Protocol))
61 }
62
63 return rule
64 }
65
66 func (f *WindowsFirewall) AllowPort(port int, address string, proto string, allow bool) error {
67 ruleName := fmt.Sprintf("bettercap-rule-%s-%s-%d", address, proto, port)
68 nameField := fmt.Sprintf(`name="%s"`, ruleName)
69 protoField := fmt.Sprintf("protocol=%s", proto)
70 // ipField := fmt.Sprintf("lolcalip=%s", address)
71 portField := fmt.Sprintf("localport=%d", port)
72
73 cmd := []string{}
74
75 if allow {
76 cmd = []string{"advfirewall", "firewall", "add", "rule", nameField, protoField, "dir=in", portField, "action=allow"}
77 } else {
78 cmd = []string{"advfirewall", "firewall", "delete", "rule", nameField, protoField, portField}
79 }
80
81 if _, err := core.Exec("netsh", cmd); err != nil {
82 return err
83 }
84
85 return nil
86 }
87
88 func (f *WindowsFirewall) EnableRedirection(r *Redirection, enabled bool) error {
89 if err := f.AllowPort(r.SrcPort, r.DstAddress, r.Protocol, enabled); err != nil {
90 return err
91 } else if err := f.AllowPort(r.DstPort, r.DstAddress, r.Protocol, enabled); err != nil {
92 return err
93 }
94
95 rule := f.generateRule(r, enabled)
96 if enabled {
97 rule = append([]string{"interface", "portproxy", "add", "v4tov4"}, rule...)
98 } else {
99 rule = append([]string{"interface", "portproxy", "delete", "v4tov4"}, rule...)
100 }
101
102 if _, err := core.Exec("netsh", rule); err != nil {
103 return err
104 }
105
106 return nil
107 }
108
109 func (f WindowsFirewall) Restore() {
110 for _, r := range f.redirections {
111 if err := f.EnableRedirection(r, false); err != nil {
112 fmt.Printf("%s", err)
113 }
114 }
115
116 if err := f.EnableForwarding(f.forwarding); err != nil {
117 fmt.Printf("%s", err)
118 }
119 }