| 1 | package firewall |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "io/ioutil" |
| 6 | "os" |
| 7 | "strings" |
| 8 | |
| 9 | "github.com/bettercap/bettercap/core" |
| 10 | "github.com/bettercap/bettercap/network" |
| 11 | |
| 12 | "github.com/evilsocket/islazy/fs" |
| 13 | "github.com/evilsocket/islazy/str" |
| 14 | ) |
| 15 | |
| 16 | type LinuxFirewall struct { |
| 17 | iface *network.Endpoint |
| 18 | forwarding bool |
| 19 | restore bool |
| 20 | redirections map[string]*Redirection |
| 21 | } |
| 22 | |
| 23 | const ( |
| 24 | IPV4ForwardingFile = "/proc/sys/net/ipv4/ip_forward" |
| 25 | IPV6ForwardingFile = "/proc/sys/net/ipv6/conf/all/forwarding" |
| 26 | ) |
| 27 | |
| 28 | func Make(iface *network.Endpoint) FirewallManager { |
| 29 | firewall := &LinuxFirewall{ |
| 30 | iface: iface, |
| 31 | forwarding: false, |
| 32 | restore: false, |
| 33 | redirections: make(map[string]*Redirection), |
| 34 | } |
| 35 | |
| 36 | firewall.forwarding = firewall.IsForwardingEnabled() |
| 37 | |
| 38 | return firewall |
| 39 | } |
| 40 | |
| 41 | func (f LinuxFirewall) enableFeature(filename string, enable bool) error { |
| 42 | var value string |
| 43 | if enable { |
| 44 | value = "1" |
| 45 | } else { |
| 46 | value = "0" |
| 47 | } |
| 48 | |
| 49 | fd, err := os.OpenFile(filename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600) |
| 50 | if err != nil { |
| 51 | return err |
| 52 | } |
| 53 | defer fd.Close() |
| 54 | |
| 55 | _, err = fd.WriteString(value) |
| 56 | return err |
| 57 | } |
| 58 | |
| 59 | func (f LinuxFirewall) IsForwardingEnabled() bool { |
| 60 | |
| 61 | if out, err := ioutil.ReadFile(IPV4ForwardingFile); err != nil { |
| 62 | return false |
| 63 | } else { |
| 64 | return str.Trim(string(out)) == "1" |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | func (f LinuxFirewall) EnableForwarding(enabled bool) error { |
| 69 | if err := f.enableFeature(IPV4ForwardingFile, enabled); err != nil { |
| 70 | return err |
| 71 | } |
| 72 | |
| 73 | if fs.Exists(IPV6ForwardingFile) { |
| 74 | return f.enableFeature(IPV6ForwardingFile, enabled) |
| 75 | } |
| 76 | |
| 77 | f.restore = true |
| 78 | return nil |
| 79 | } |
| 80 | |
| 81 | func (f *LinuxFirewall) getCommandLine(r *Redirection, enabled bool) (cmdLine []string) { |
| 82 | action := "-A" |
| 83 | destination := "" |
| 84 | |
| 85 | if !enabled { |
| 86 | action = "-D" |
| 87 | } |
| 88 | |
| 89 | if strings.Count(r.DstAddress, ":") < 2 { |
| 90 | destination = r.DstAddress |
| 91 | } else { |
| 92 | destination = fmt.Sprintf("[%s]", r.DstAddress) |
| 93 | } |
| 94 | |
| 95 | if r.SrcAddress == "" { |
| 96 | cmdLine = []string{ |
| 97 | "-t", "nat", |
| 98 | action, "PREROUTING", |
| 99 | "-i", r.Interface, |
| 100 | "-p", r.Protocol, |
| 101 | "--dport", fmt.Sprintf("%d", r.SrcPort), |
| 102 | "-j", "DNAT", |
| 103 | "--to", fmt.Sprintf("%s:%d", destination, r.DstPort), |
| 104 | } |
| 105 | } else { |
| 106 | cmdLine = []string{ |
| 107 | "-t", "nat", |
| 108 | action, "PREROUTING", |
| 109 | "-i", r.Interface, |
| 110 | "-p", r.Protocol, |
| 111 | "-d", r.SrcAddress, |
| 112 | "--dport", fmt.Sprintf("%d", r.SrcPort), |
| 113 | "-j", "DNAT", |
| 114 | "--to", fmt.Sprintf("%s:%d", destination, r.DstPort), |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | return |
| 119 | } |
| 120 | |
| 121 | func (f *LinuxFirewall) EnableRedirection(r *Redirection, enabled bool) error { |
| 122 | cmdLine := f.getCommandLine(r, enabled) |
| 123 | rkey := r.String() |
| 124 | _, found := f.redirections[rkey] |
| 125 | cmd := "" |
| 126 | |
| 127 | if strings.Count(r.DstAddress, ":") < 2 { |
| 128 | cmd = "iptables" |
| 129 | } else { |
| 130 | cmd = "ip6tables" |
| 131 | } |
| 132 | |
| 133 | if enabled { |
| 134 | if found { |
| 135 | return fmt.Errorf("Redirection '%s' already enabled.", rkey) |
| 136 | } |
| 137 | |
| 138 | f.redirections[rkey] = r |
| 139 | |
| 140 | // accept all |
| 141 | if _, err := core.Exec(cmd, []string{"-P", "FORWARD", "ACCEPT"}); err != nil { |
| 142 | return err |
| 143 | } else if _, err := core.Exec(cmd, cmdLine); err != nil { |
| 144 | return err |
| 145 | } |
| 146 | } else { |
| 147 | if !found { |
| 148 | return nil |
| 149 | } |
| 150 | |
| 151 | delete(f.redirections, r.String()) |
| 152 | |
| 153 | if _, err := core.Exec(cmd, cmdLine); err != nil { |
| 154 | return err |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | return nil |
| 159 | } |
| 160 | |
| 161 | func (f LinuxFirewall) Restore() { |
| 162 | if f.restore == false { |
| 163 | return |
| 164 | } |
| 165 | for _, r := range f.redirections { |
| 166 | if err := f.EnableRedirection(r, false); err != nil { |
| 167 | fmt.Printf("%s", err) |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | if err := f.EnableForwarding(f.forwarding); err != nil { |
| 172 | fmt.Printf("%s", err) |
| 173 | } |
| 174 | } |