Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revisionLast revisionBoth sides next revision | ||
| inbox:firewall:fw3_configurations:fw3_config_examples [2018/09/16 12:23] – bobafetthotmail | docs:guide-user:firewall:fw3_configurations:fw3_config_examples [2023/10/06 04:22] – update link vgaetera | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ===== fw3 Configuration | + | ====== IPv4 firewall examples ====== |
| - | [[ inbox: | + | This section contains a collection of useful |
| - | \\ | + | All of these can be added on the LuCI //Network -> Firewall -> Traffic Rules// page. |
| - | [[ inbox: | + | |
| + | In keeping with the underlying netfilter service, the first matching rule will run its target and (with a couple of exceptions) filtering stops; no subsequent rules are checked. | ||
| + | LuCI has the capability to move rules up and down to sort them correctly. | ||
| + | |||
| + | See [[docs: | ||
| + | These examples cover only IPv4 networks. | ||
| + | |||
| + | The term **station** is used to refer to any electronic device that can source or sink packets through, or to/from, the router. | ||
| + | This can be a web server, mobile phone, tablet, laptop, IoT device on the LAN-side or the WAN-side. | ||
| + | The netfilter rules match stations and traffic types to allow packets to continue through the network stack or not. | ||
| + | |||
| + | Unless otherwise noted, all rules have been tested mostly with [[https:// | ||
| + | The '' | ||
| + | |||
| + | :!: Before modifying rules, be sure to back-up your current ''/ | ||
| + | |||
| + | ===== Opening ports on the OpenWrt router ===== | ||
| + | The default configuration accepts all LAN traffic, but blocks all incoming WAN traffic on ports not currently used for connections or NAT. | ||
| + | The reference topology blocks all LAN and WAN traffic, requiring a rule to open port(s) for a service. | ||
| + | |||
| + | <code bash> | ||
| + | config rule | ||
| + | option target ' | ||
| + | option src ' | ||
| + | option proto ' | ||
| + | option dest_port ' | ||
| + | option name ' | ||
| + | option enabled ' | ||
| + | </ | ||
| + | |||
| + | This example enables stations on the WAN-side to use SSH to access the router (the default destination). | ||
| + | |||
| + | :!: If the WAN-side of the router is connected to the internet this rule allows any public site SSH access to your router. | ||
| + | Once a portscanner discovers the open SSH port it will repeatedly try to break in - even with a strong pub key these attacks can be a nuisance. | ||
| + | |||
| + | ===== Opening ports for selected subnet/host ===== | ||
| + | Use **src_ip** and **dest_ip** options to match on specific subnets. | ||
| + | |||
| + | <code bash> | ||
| + | config rule | ||
| + | option target ' | ||
| + | option src ' | ||
| + | option family ' | ||
| + | option proto ' | ||
| + | option src_ip ' | ||
| + | option dest_port ' | ||
| + | option name ' | ||
| + | option enabled ' | ||
| + | </ | ||
| + | |||
| + | This example enables SSH access to the router from any station in the private '' | ||
| + | It will not match any other src IP address. | ||
| + | |||
| + | :!: When using an IPv4 address set the family to **ipv4**, otherwise firewall warns '' | ||
| + | |||
| + | ===== Block WAN-side networks and ports ===== | ||
| + | When public-facing servers run behind the firewall (e.g. mail server), each is susceptible to attacks: SSH probing, SPAM, screen-scraping, | ||
| + | |||
| + | Customers of the large overseas ISPs (particular China and Vietnam) have made spam attacks into an artform, generating blocks of prose to confuse spam filters, sprinkling emails across many source stations and many subnets. | ||
| + | The best way to counter this is to block the main originating network sending the spam. | ||
| + | |||
| + | <code bash> | ||
| + | config rule | ||
| + | option src ' | ||
| + | option dest ' | ||
| + | option proto ' | ||
| + | option src_ip ' | ||
| + | option dest_port ' | ||
| + | option target ' | ||
| + | option name ' | ||
| + | option enabled ' | ||
| + | </ | ||
| + | |||
| + | In this example, stations in a Beijing network are sending email spam in bursts of three with different content incrementing ipv4 addresses across subnets! | ||
| + | This rule DROPS all incoming traffic on port 25 (SMTP) from any station in their network. | ||
| + | DROP silently discards the packet rather than REJECT which returns a response to the source. | ||
| + | |||
| + | :!: Once the number of blocked networks grows to more than a couple dozen (there are thousands of spamming sites), then adding each to the firewal config becomes prohibitive to manage. | ||
| + | Two alternatives are: | ||
| + | * add individual netfilter rules to ''/ | ||
| + | * use the '' | ||
| + | |||
| + | ===== Using ipset to block LAN-side networks ===== | ||
| + | The example below creates a rule in the netfilter FORWARD chain, rejecting traffic from the LAN-side to the WAN-side on the ports 1000-1100. | ||
| + | |||
| + | <code bash> | ||
| + | config rule | ||
| + | option src ' | ||
| + | option dest ' | ||
| + | option dest_port ' | ||
| + | option proto ' | ||
| + | option target ' | ||
| + | option name ' | ||
| + | option enabled ' | ||
| + | </ | ||
| + | |||
| + | ===== Block LAN-side access to a specific site ===== | ||
| + | The following rule blocks HTTP/S connections from all LAN-side stations to a single public site. | ||
| + | Use a DNS utility ('' | ||
| + | |||
| + | <code bash> | ||
| + | config rule | ||
| + | option src ' | ||
| + | option dest ' | ||
| + | option proto ' | ||
| + | option family ' | ||
| + | option dest_ip ' | ||
| + | option dest_port ' | ||
| + | option target ' | ||
| + | option name ' | ||
| + | option enabled ' | ||
| + | </ | ||
| + | |||
| + | Notice the **dest_port** option has two ports: HTTP and HTTPS. | ||
| + | When there is white space in the list it must be surrounded by single quotes. | ||
| + | |||
| + | If the source or destination is the router itself then the option is not explicitly defined in a rule. | ||
| + | For reference, these rules are added to the netfilter INPUT (to the router) and OUTPUT (from the router) chains. | ||
| + | |||
| + | <code bash> | ||
| + | config rule | ||
| + | option dest ' | ||
| + | option dest_ip ' | ||
| + | option family ' | ||
| + | option proto ' | ||
| + | option target ' | ||
| + | option name ' | ||
| + | option enabled ' | ||
| + | </ | ||
| + | |||
| + | This rule causes netfilter to reject any icmp echo from the router (OUTPUT chain) to the public google DNS server. | ||
| + | This rule is not particularly useful but serves as an illustrative example. | ||
| + | |||
| + | ===== Block access to certain domains based on their names ===== | ||
| + | An example is give at [[docs: | ||
| + | It is also capable to filter DDNS hosts. | ||
| + | It has also the advantage to allow for other subdomains (like www.) by just filtering the root-domain-name (like example.com). | ||
| + | |||
| + | ===== Block access to the Internet for a specific LAN station between certain times ===== | ||
| + | The following rule can be used for parental access control. | ||
| + | |||
| + | <code bash> | ||
| + | config rule | ||
| + | option src ' | ||
| + | option dest ' | ||
| + | option src_mac ' | ||
| + | option proto ' | ||
| + | option start_time ' | ||
| + | option stop_time ' | ||
| + | option utc_time ' | ||
| + | option weekdays ' | ||
| + | option target ' | ||
| + | option name ' | ||
| + | option enabled ' | ||
| + | </ | ||
| + | |||
| + | When this rule is enabled, it will block all TCP and UDP access from STA2 to the internet on weekdays between 21:00 and 09:00. | ||
| + | By default, the time will be UTC unless the '' | ||
| + | |||
| + | These time/date matches use the netfilter '' | ||
| + | Check ''/ | ||
| + | |||
| + | From LuCI this rule can be added by following " | ||
| + | with the desired MAC address and an action of " | ||
| + | |||
| + | :!: Remove the time and day options to always block WAN-side access for the station. | ||
| + | |||
| + | :!: This rule can be created for a single MAC address, not a range. | ||
| + | |||
| + | :!: this type of rule is very useful for mobile devices like smartphones and tablets. | ||
| + | A lot can change in a smartphone but the wifi MAC is **almost** always programmed at the factory. | ||
| + | The MAC **can** be modified by a sophisticated user by doing something similar to the Linux commands: | ||
| + | |||
| + | <code bash> | ||
| + | root> ip link set wlan0 down | ||
| + | root> ip link set address " | ||
| + | root> ip link set wlan0 up | ||
| + | </ | ||
| + | |||
| + | An alternative mechanism to block multiple LAN MACs can be found in the LuCI " | ||
| + | Set the filter for "Allow all except listed" | ||
| + | In the ''/ | ||
| + | |||
| + | ===== IPSec passthrough ===== | ||
| + | This example enables proper forwarding of IPSec traffic through the wan. | ||
| + | The protocol references are: | ||
| + | * '' | ||
| + | * '' | ||
| + | |||
| + | <code bash> | ||
| + | config rule | ||
| + | option src ' | ||
| + | option dest ' | ||
| + | option proto ' | ||
| + | option target ' | ||
| + | |||
| + | config rule | ||
| + | option src ' | ||
| + | option dest ' | ||
| + | option proto ' | ||
| + | option target ' | ||
| + | </ | ||
| + | |||
| + | For some configurations you also have to open port 500/UDP for the ISAKMP protocol. | ||
| + | |||
| + | <code bash> | ||
| + | config rule | ||
| + | option src ' | ||
| + | option dest ' | ||
| + | option proto ' | ||
| + | option src_port ' | ||
| + | option dest_port ' | ||
| + | option target ' | ||
| + | </ | ||
| + | |||
| + | ===== Zone declaration for semi non-UCI interfaces, manually listed in the network config, and forwardings ===== | ||
| + | Scenario: having one or more VPN tunnels using OpenVPN, with the need of defining a zone to forward the traffic between the VPN interfaces and the LAN. | ||
| + | |||
| + | First list the interfaces in **/ | ||
| + | Be careful on the limits of interface naming in terms of name length, [[docs: | ||
| + | |||
| + | <code bash> | ||
| + | config interface ' | ||
| + | option ifname ' | ||
| + | option proto ' | ||
| + | |||
| + | config interface ' | ||
| + | option ifname ' | ||
| + | option proto ' | ||
| + | </ | ||
| + | |||
| + | Then create the zone in **/ | ||
| + | |||
| + | <code bash> | ||
| + | config zone | ||
| + | option name ' | ||
| + | list network ' | ||
| + | list network ' | ||
| + | option input ' | ||
| + | # the traffic towards the router from the interface will be accepted | ||
| + | # (as for the lan communications) | ||
| + | option output ' | ||
| + | # the traffic from the router to the interface will be accepted | ||
| + | option forward ' | ||
| + | # traffic from this zone to other zones is normally rejected | ||
| + | </ | ||
| + | |||
| + | Then we want to communicate with the " | ||
| + | |||
| + | <code bash> | ||
| + | config forwarding | ||
| + | option src ' | ||
| + | option dest ' | ||
| + | # if a packet from lan wants to go to the vpn_tunnel zone | ||
| + | # let it pass | ||
| + | |||
| + | config forwarding | ||
| + | option src ' | ||
| + | option dest ' | ||
| + | # if a packet from vpn_tunnel wants to go to the lan zone | ||
| + | # let it pass | ||
| + | </ | ||
| + | |||
| + | In general remember that forwardings are relying how routing rules are defined, and afterwards which zones are defined on which interfaces. | ||
| + | |||
| + | ===== Zone declaration for non-UCI interfaces ===== | ||
| + | This example declares a zone which matches any Linux network device whose name begins with " | ||
| + | |||
| + | <code bash> | ||
| + | config zone | ||
| + | option name ' | ||
| + | option input ' | ||
| + | option output ' | ||
| + | option forward ' | ||
| + | option device ' | ||
| + | </ | ||
| + | |||
| + | ===== Zone declaration for a specific subnet and protocol ===== | ||
| + | This example declares a zone which maches any TCP stream in the '' | ||
| + | |||
| + | <code bash> | ||
| + | config zone | ||
| + | option name ' | ||
| + | option input ' | ||
| + | option output ' | ||
| + | option forward ' | ||
| + | option subnet ' | ||
| + | option extra ' | ||
| + | </ | ||
| + | |||
| + | |||
| + | ===== Zone declaration for a specific protocol and port ===== | ||
| + | This example declares a zone which maches any TCP stream from and to port '' | ||
| + | |||
| + | <code bash> | ||
| + | config zone | ||
| + | option name ' | ||
| + | option input ' | ||
| + | option output ' | ||
| + | option forward ' | ||
| + | option extra_src ' | ||
| + | option extra_dest ' | ||
| + | </ | ||
| + | |||
| + | ===== Stateful firewall without NAT ===== | ||
| + | <WRAP center round alert 60%> | ||
| + | I have not tested this, but it **seems** reasonable. | ||
| + | |||
| + | In reality, the monthly cost of a block of public IPv4 addresses makes sense for ISPs that distribute the addresses to customers for a fee and larger corporations that need public addresses for their internet presence (e.g. web, mail, name servers, remote offices) | ||
| + | </ | ||
| + | |||
| + | If your LAN is running with public IP addresses, then you definitely don't want NAT (masquerading). | ||
| + | But you may still want to run a stateful firewall on the router, so that stations on the LAN-side are not reachable from the WAN-side. | ||
| + | |||
| + | To do this, add the '' | ||
| + | |||
| + | <code bash> | ||
| + | config zone | ||
| + | option name ' | ||
| + | list network ' | ||
| + | list network ' | ||
| + | option input ' | ||
| + | option output ' | ||
| + | option forward ' | ||
| + | option masq ' | ||
| + | option mtu_fix ' | ||
| + | option conntrack ' | ||
| + | </ | ||
| + | |||
| + | ===== Allow HTTP/HTTPS access from Cloudflare ===== | ||
| + | Here is an example that allows HTTP/HTTPS access from Cloudflare. | ||
| + | Use if your webserver is behind the Cloudflare proxy. | ||
| + | |||
| + | <code bash> | ||
| + | cat << EOF >> / | ||
| + | uci -q delete firewall.cf_proxy.dest_ip | ||
| + | for IPV in 4 6 | ||
| + | do for IP in $(wget -O - \ | ||
| + | " | ||
| + | do uci add_list firewall.cf_proxy.dest_ip=" | ||
| + | done | ||
| + | done | ||
| + | / | ||
| + | EOF | ||
| + | uci -q delete firewall.cf_proxy | ||
| + | uci set firewall.cf_proxy=" | ||
| + | uci set firewall.cf_proxy.name=" | ||
| + | uci set firewall.cf_proxy.src=" | ||
| + | uci add_list firewall.cf_proxy.dest_port=" | ||
| + | uci add_list firewall.cf_proxy.dest_port=" | ||
| + | uci set firewall.cf_proxy.proto=" | ||
| + | uci set firewall.cf_proxy.target=" | ||
| + | uci commit firewall | ||
| + | / | ||
| + | </ | ||