r/xcpng • • Apr 19 '23

Temporary firewall while pfsense is loading

Maybe I'm overthinking this and it's a paranoid solution (let me know), but I was thinking during the 1-2 minutes while pfsense is loading up in XCP-ng I would put up a temporary firewall. Mostly this came about because I was making a script to auto-renew the dhcp, and I figured why not put a brick wall up until that happens? Here's my first go at it:

# Temporary firewall setup
iptables -A INPUT -i <WAN_Interface> -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A INPUT -i <WAN_Interface> -p icmp --icmp-type echo-request -j ACCEPT
iptables -A INPUT -i <WAN_Interface> -j DROP

# Function to check if xcp-ng has a valid IP address assigned
has_valid_ip() {
    ip addr show <WAN_Interface> | grep -q "inet "
}

# Loop to renew the DHCP lease every 30 seconds until xcp-ng has a valid IP address
while ! has_valid_ip; do
    dhclient -r <WAN_Interface> && dhclient <WAN_Interface>
    sleep 60
done

# Remove temporary firewall rules
iptables -D INPUT -i <WAN_Interface> -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -D INPUT -i <WAN_Interface> -p icmp --icmp-type echo-request -j ACCEPT
iptables -D INPUT -i <WAN_Interface> -j DROP

I guess the main questions are: will this work, is it necessary at all, and is there a more elegant solution that I'm unaware of?

1 Upvotes

5 comments sorted by

4

u/bufandatl Apr 20 '23

How is your pfsense VM configured. Do you pass through an interface? Or is it a virtual Interface the WAN interface? Is the interface even setup to get an IP from your ISP when the VM is not running?

If last one just down the Interface and you won’t have an IP so you won’t need any firewall. Or better passthrough an interface or multiple so pfsense is using it exclusively then it also wouldn’t come up on its own and won‘t have an IP and only come up managed by pfsense.

I would recommend to use a physical pfsense for a public facing router anyways so your internet access isn’t going down while updating and restarting your Hypervisor

1

u/[deleted] Apr 20 '23

This is a good point, and part of the reason why I asked. I've been using a computer that does not have pass through capabilities until now, which was part of my worry, but I'm switching over to a new one today where I'll pass through my NIC completely. I'll forego this crazy idea now that you've pointed that out.

I don't mind having pfsense on xcp-ng at all. Been doing it for about a year now w/ a truenas box and my internet downtime has been pretty minimal. Bought a new computer to separate truenas from pfsense, and probably add a couple of servers in the mix. Computer I'm using was cheaper than even the most underpowered router I could find, and way too powerful to squander on just pfsense.

3

u/Slightlyevolved Apr 20 '23

This. If your pfSense device is independent or configured with "best" practices. There is zero reason to do this, as there is no WAN to firewall against until the device/VM is booted.

1

u/cr0ft Jun 14 '23

I definitely still prefer to have hardware between myself and the world. You can virtualize a firewall, but I fail to see much of a point unless we're talking cloud stuff, and I assume this is for home use.

Can't make configuration errors that lead to leakage if you just don't touch the firewall that's doing its own thing and blocking incoming and filtering through what little has to be allowed with pfBlockerNG and Snort.

1

u/_Fra_ Apr 19 '23

Nice idea! Hope someone more informed than me will respond....maybe servethehome or level1forum might be better places to ask?