f-75: ebtables: confinement de interface_ip, ARP et DHCP en FORWARD et OUTPUT, retrait des seules règles présentes #75
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
parent
91cf5e3323
commit
f4790187c4
2 changed files with 89 additions and 0 deletions
|
|
@ -1,8 +1,10 @@
|
|||
package ebtables
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func addRule(args ...string) error {
|
||||
|
|
@ -89,3 +91,63 @@ func DeleteConfineMAC(iface, mac string) error {
|
|||
}
|
||||
return first
|
||||
}
|
||||
|
||||
func gatewayRules(iface, ip string) [][]string {
|
||||
dhcp := []string{"-p", "IPv4", "--ip-protocol", "udp", "--ip-source-port", "67:68", "--ip-destination-port", "67:68", "--ip-source", ip, "-j", "DROP"}
|
||||
return [][]string{
|
||||
{"-t", "filter", "FORWARD", "--out-interface", iface, "-p", "arp", "--arp-op", "Request", "--arp-ip-dst", ip, "-j", "DROP"},
|
||||
{"-t", "filter", "OUTPUT", "--out-interface", iface, "-p", "arp", "--arp-ip-src", ip, "-j", "DROP"},
|
||||
append([]string{"-t", "filter", "FORWARD", "--out-interface", iface}, dhcp...),
|
||||
append([]string{"-t", "filter", "OUTPUT", "--out-interface", iface}, dhcp...),
|
||||
}
|
||||
}
|
||||
|
||||
func ruleExists(rule []string) (bool, error) {
|
||||
err := exec.Command("ebtables", withAction("--check", rule)...).Run()
|
||||
var exit *exec.ExitError
|
||||
switch {
|
||||
case err == nil:
|
||||
return true, nil
|
||||
case errors.As(err, &exit) && exit.ExitCode() == 1:
|
||||
return false, nil
|
||||
default:
|
||||
return false, fmt.Errorf("ebtables --check %s: %w", strings.Join(rule, " "), err)
|
||||
}
|
||||
}
|
||||
|
||||
func ConfineGateway(iface, ip string) error {
|
||||
for _, rule := range gatewayRules(iface, ip) {
|
||||
if err := exec.Command("ebtables", withAction("-A", rule)...).Run(); err != nil {
|
||||
return fmt.Errorf("ebtables confine gateway %s on %s (%s): %w", ip, iface, strings.Join(rule, " "), err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func DeleteConfineGateway(iface, ip string) error {
|
||||
var first error
|
||||
for _, rule := range gatewayRules(iface, ip) {
|
||||
exists, err := ruleExists(rule)
|
||||
if err == nil && exists {
|
||||
err = exec.Command("ebtables", withAction("-D", rule)...).Run()
|
||||
}
|
||||
if err != nil && first == nil {
|
||||
first = fmt.Errorf("ebtables unconfine gateway %s on %s (%s): %w", ip, iface, strings.Join(rule, " "), err)
|
||||
}
|
||||
}
|
||||
return first
|
||||
}
|
||||
|
||||
func MissingGatewayRules(iface, ip string) ([]string, error) {
|
||||
var missing []string
|
||||
for _, rule := range gatewayRules(iface, ip) {
|
||||
exists, err := ruleExists(rule)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !exists {
|
||||
missing = append(missing, strings.Join(rule, " "))
|
||||
}
|
||||
}
|
||||
return missing, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -41,3 +41,30 @@ func TestWithActionDoesNotAlterTheRule(t *testing.T) {
|
|||
t.Errorf("la règle a été modifiée : %q devenu %q", before, strings.Join(rule, " "))
|
||||
}
|
||||
}
|
||||
|
||||
func TestGatewayRules(t *testing.T) {
|
||||
want := []string{
|
||||
"-t filter -A FORWARD --out-interface v-t33a-i -p arp --arp-op Request --arp-ip-dst 10.33.0.1 -j DROP",
|
||||
"-t filter -A OUTPUT --out-interface v-t33a-i -p arp --arp-ip-src 10.33.0.1 -j DROP",
|
||||
"-t filter -A FORWARD --out-interface v-t33a-i -p IPv4 --ip-protocol udp --ip-source-port 67:68 --ip-destination-port 67:68 --ip-source 10.33.0.1 -j DROP",
|
||||
"-t filter -A OUTPUT --out-interface v-t33a-i -p IPv4 --ip-protocol udp --ip-source-port 67:68 --ip-destination-port 67:68 --ip-source 10.33.0.1 -j DROP",
|
||||
}
|
||||
rules := gatewayRules("v-t33a-i", "10.33.0.1")
|
||||
if len(rules) != len(want) {
|
||||
t.Fatalf("%d règles, %d attendues", len(rules), len(want))
|
||||
}
|
||||
for i, rule := range rules {
|
||||
if got := strings.Join(withAction("-A", rule), " "); got != want[i] {
|
||||
t.Errorf("règle %d : %q, %q attendu", i, got, want[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestGatewayRulesDoNotShareSlices(t *testing.T) {
|
||||
rules := gatewayRules("v-x-i", "10.0.0.1")
|
||||
before := strings.Join(rules[2], " ")
|
||||
rules[3][len(rules[3])-1] = "ACCEPT"
|
||||
if strings.Join(rules[2], " ") != before {
|
||||
t.Errorf("les deux règles DHCP partagent leur tableau : %q", strings.Join(rules[2], " "))
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue