r/linuxquestions • u/sofiawylieishot • 3h ago
Can someone confirm this iptables script is safe to use on my router?
I just want to be clear. I have no clue about anything in terms of iptables and gemini/claude wrote this. It's for DD-WRT. I'm trying to make it so br1 and br2 can't access the web UI for DD-WRT. I'm also trying to isolate br1 and br2 from each other and all devices on br2 from seeing each other (wifi). while making it so that you can only access the admin page for the router while plugged into lan3 via ethernet. I ran some tests and it does block pings from br1 and br2 to the router ip (so can't access the router admin page on those bridges). It also blocked br1 from seeing br2 devices. It also seems to block pinging devices that are both connected to br2. I just want someone who knows what they are doing to look at it.
ADMIN_BR="br0"
BR1="br1"
BR2="br2"
# 1. Restrict router HTTPS (443) management to br0; drop 443 from all other interfaces
iptables -C INPUT -i "$ADMIN_BR" -p tcp --dport 443 -j ACCEPT 2>/dev/null || \
iptables -I INPUT 1 -i "$ADMIN_BR" -p tcp --dport 443 -j ACCEPT
iptables -C INPUT ! -i "$ADMIN_BR" -p tcp --dport 443 -j DROP 2>/dev/null || \
iptables -I INPUT 2 ! -i "$ADMIN_BR" -p tcp --dport 443 -j DROP
# 2. Isolate router access on br1 and br2 (allow established, DHCP 67:68, and DNS 53)
for BR in "$BR1" "$BR2"; do
CHAIN="${BR}_ROUTER_IN"
iptables -N "$CHAIN" 2>/dev/null
iptables -F "$CHAIN"
iptables -A "$CHAIN" -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
iptables -A "$CHAIN" -p udp --dport 67:68 -j ACCEPT
iptables -A "$CHAIN" -p udp --dport 53 -j ACCEPT
iptables -A "$CHAIN" -p tcp --dport 53 -j ACCEPT
iptables -A "$CHAIN" -j DROP
iptables -C INPUT -i "$BR" -j "$CHAIN" 2>/dev/null || \
iptables -I INPUT 3 -i "$BR" -j "$CHAIN"
done
# 3. Isolate routed traffic between br0, br1, and br2 ahead of DD-WRT FORWARD rules
FWD_CHAIN="BR_ISOLATION"
iptables -N "$FWD_CHAIN" 2>/dev/null
iptables -F "$FWD_CHAIN"
for PAIR in "${BR1} ${ADMIN_BR}" "${ADMIN_BR} ${BR1}" "${BR2} ${ADMIN_BR}" "${ADMIN_BR} ${BR2}" "${BR1} ${BR2}" "${BR2} ${BR1}"; do
SRC=${PAIR% *}
DST=${PAIR#* }
iptables -A "$FWD_CHAIN" -i "$SRC" -o "$DST" -j DROP
done
iptables -C FORWARD -j "$FWD_CHAIN" 2>/dev/null || \
iptables -I FORWARD 1 -j "$FWD_CHAIN"
2
u/GiggleSamurai 2h ago
Two gaps: the admin rules only check for br0, and on DD-WRT br0 normally holds every LAN port plus the main wifi, so the page isn't limited to lan3 unless lan3 is the only member of br0. And
iptablesonly covers IPv4, so if IPv6 is enabled on the router the same blocks need to be added withip6tables.1
u/ipsirc 2h ago
Dropping ICMP traffic is a so dumb movement.
1
u/billdietrich1 1h ago
There are iptables simulators available; I haven't tried any of them.
You could ask another AI to evaluate the rule-set.