├── plugins └── deny_ssh_bf.eztables ├── init.d └── eztables ├── eztables.cfg ├── install.sh ├── MANUAL.md ├── README.md ├── EXAMPLES.md ├── LICENSE └── eztables /plugins/deny_ssh_bf.eztables: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | DENY_SSH_BF_PORT="$DENY_SSH_BF_PORT" 4 | 5 | if [ -z "$DENY_SSH_BF" ] 6 | then 7 | DENY_SSH_BF="0" 8 | fi 9 | 10 | if [ -z "$DENY_SSH_BF_IP" ] 11 | then 12 | DENY_SSH_BF="0" 13 | fi 14 | 15 | if [ -z "$DENY_SSH_BF_PORT" ] 16 | then 17 | DENY_SSH_BF_PORT="22" 18 | fi 19 | 20 | if [ -z "$DENY_SSH_WINDOW" ] 21 | then 22 | DENY_SSH_WINDOW="300" 23 | fi 24 | 25 | if [ -z "$DENY_SSH_PACKETS" ] 26 | then 27 | DENY_SSH_PACKETS="10" 28 | fi 29 | 30 | plugin_status deny_ssh_bruteforce "$DENY_SSH_BF" 31 | 32 | if [ "$DENY_SSH_BF" == "1" ] 33 | then 34 | ipt-exec -N DENY_SSH_BRUTEFORCE 35 | ipt-exec -I INPUT 1 -p tcp -d "$DENY_SSH_BF_IP" --dport "$DENY_SSH_BF_PORT" -m state --state new -j DENY_SSH_BRUTEFORCE 36 | ipt-exec -A "DENY_SSH_BRUTEFORCE" -m recent --set --name SSH 37 | ipt-exec -A "DENY_SSH_BRUTEFORCE" -m recent --update --seconds "$DENY_SSH_WINDOW" --hitcount "$DENY_SSH_PACKETS" --name SSH -j LOG --log-level info --log-prefix " SSH BRUTE FORCE ATTACK " 38 | ipt-exec -A "DENY_SSH_BRUTEFORCE" -m recent --update --seconds "$DENY_SSH_WINDOW" --hitcount "$DENY_SSH_PACKETS" --name SSH -j DROP 39 | fi 40 | 41 | -------------------------------------------------------------------------------- /init.d/eztables: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | ### BEGIN INIT INFO 3 | # Provides: EZTABLES 4 | # Required-Start: 5 | # Required-Stop: 6 | # Default-Start: 2 3 4 5 S 7 | # Default-Stop: 0 1 6 8 | # Short-Description: Linux Iptables Firewall Script 9 | # Description: EZTABLES allows you to setup a complex firewall 10 | # based on IPtables with a few easy rules. 11 | ### END INIT INFO 12 | 13 | CONFIG=/etc/eztables/eztables.cfg 14 | 15 | if [ ! -e "$CONFIG" ] 16 | then 17 | echo "Config $CONFIG not found." 18 | exit 1 19 | fi 20 | 21 | stop_eztables () { 22 | 23 | # echo "Stopping EZTABLES..." 24 | /usr/sbin/eztables stop $1 25 | exit $? 26 | } 27 | 28 | start_eztables () { 29 | 30 | # echo "Starting EZTABLES..." 31 | /usr/sbin/eztables start $1 32 | exit $? 33 | } 34 | 35 | usage () { 36 | 37 | echo "Usage: $0 [ start | stop | restart ] " 38 | } 39 | 40 | 41 | case "$1" in 42 | start) 43 | start_eztables $2 44 | ;; 45 | stop) 46 | stop_eztables $2 47 | ;; 48 | restart) 49 | stop_eztables 50 | start_eztables 51 | ;; 52 | *) 53 | usage 54 | ;; 55 | esac 56 | 57 | 58 | -------------------------------------------------------------------------------- /eztables.cfg: -------------------------------------------------------------------------------- 1 | 2 | ENABLE_SYSLOG=1 3 | 4 | DENY_SSH_BF=1 5 | DENY_SSH_BF_IP="$eth0" 6 | DENY_SSH_BF_PORT=22 7 | 8 | 9 | GOOGLE_DNS1=8.8.8.8 10 | GOOGLE_DNS2=8.8.4.4 11 | 12 | DNS_SERVERS=" 13 | 14 | $GOOGLE_DNS1 15 | $GOOGLE_DNS2 16 | " 17 | 18 | WEB=" 19 | 20 | 80/tcp 21 | 443/tcp 22 | " 23 | 24 | DNS=" 25 | 26 | 53/udp 27 | 53/tcp 28 | " 29 | 30 | NTP=" 31 | 32 | 123/udp 33 | " 34 | 35 | SSH=" 36 | 37 | 22/tcp 38 | " 39 | 40 | BASIC_SERVICES=" 41 | 42 | $WEB 43 | $DNS 44 | $NTP 45 | $SSH 46 | " 47 | 48 | APPSERVER1=192.168.123.2 49 | 50 | # 51 | # Permit SSH towards this host. 52 | # 53 | allow_in any $eth0 any "$SSH" 54 | # 55 | # Permit this host to access HTTP, DNS, NTP and SSH on the internet. 56 | # 57 | allow_out $eth0 any any "$BASIC_SERVICES" 58 | 59 | # 60 | # Enable clients within network $eth1_net to access the internet. 61 | # 62 | # nat $eth1_net $eth0 63 | # allow_forward $eth1_net "$DNS_SERVERS" any "$DNS" 64 | # allow_forward $eth1_net any any "$WEB" 65 | 66 | # 67 | # Forward port 80 on external interface $eth0 towards APPSERVER1 68 | # 69 | # port_forward $eth0 "$APPSERVER1" "$WEB" 70 | # 71 | # By default, machines in any network attached to an interface 72 | # can't access any other machine in any other local network. 73 | # If a machine in any of those networks need to access the 74 | # forwarded port, you must explicitly enable this: 75 | # 76 | # allow_forward $eth0_net "$APPSERVER1" any "$WEB" 77 | 78 | # 79 | # In this example ICMP is permitted to any host. 80 | # 81 | allow_icmp any any 82 | 83 | -------------------------------------------------------------------------------- /install.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | BINARY=eztables 4 | CONFIG=eztables.cfg 5 | INITSCRIPT=init.d/eztables 6 | 7 | BINARY_TARGET=/usr/sbin 8 | INITSCRIPT_TARGET=/etc/init.d 9 | CONFIG_TARGET=/etc/eztables 10 | PLUGINS=$CONFIG_TARGET/plugins 11 | 12 | if [ ! -z "$1" ] 13 | then 14 | if [ "$1" == "remove" ] 15 | then 16 | if [ -e /etc/debian_version ] 17 | then 18 | update-rc.d -f eztables remove 19 | elif [ -e /etc/redhat-release ] 20 | then 21 | chkconfig --del eztables 22 | fi 23 | 24 | rm "$BINARY_TARGET/$BINARY" 25 | rm "$CONFIG_TARGET/$CONFIG" 26 | rm "$CONFIG_TARGET/$TARGETS" 27 | rm $INITSCRIPT_TARGET/`basename $INITSCRIPT` 28 | exit 29 | fi 30 | fi 31 | 32 | if [ ! -e "$CONFIG_TARGET" ] 33 | then 34 | mkdir -p "$CONFIG_TARGET" 35 | fi 36 | 37 | cp "$BINARY" "$BINARY_TARGET" 38 | cp "$INITSCRIPT" "$INITSCRIPT_TARGET" 39 | 40 | chmod 755 "$BINARY_TARGET/$BINARY" 41 | chmod 755 "$INITSCRIPT_TARGET/`basename $INITSCRIPT`" 42 | 43 | if [ -e $CONFIG_TARGET/$CONFIG ] 44 | then 45 | echo 46 | echo "-------------------------------------------------------------------" 47 | echo "Existing configuration found. Creating $CONFIG_TARGET/$CONFIG.new." 48 | echo "Update your existing configuration file with the new one or EZTABLES may" 49 | echo "not operate properly due to changes. Press enter to continue." 50 | echo "-------------------------------------------------------------------" 51 | read YN 52 | cp "$CONFIG" "$CONFIG_TARGET/$CONFIG.new" 53 | else 54 | cp "$CONFIG" "$CONFIG_TARGET/$CONFIG" 55 | mkdir "$PLUGINS" 56 | cp plugins/*.eztables "$PLUGINS" 57 | fi 58 | 59 | if [ -e /etc/debian_version ] 60 | then 61 | update-rc.d eztables defaults 99 10 62 | elif [ -e /etc/redhat-release ] 63 | then 64 | chkconfig --levels 2345 eztables on 65 | fi 66 | -------------------------------------------------------------------------------- /MANUAL.md: -------------------------------------------------------------------------------- 1 | # Eztables Manual 2 | 3 | ## Basic syntax for configuration 4 | 5 | This is the basic syntax for every firewall rule: 6 | 7 | ```sh 8 | allow_in 9 | ``` 10 | 11 | ![overview](http://louwrentius.com/static/images/eztables-rules.png) 12 | 13 | There are also additiional commands such as allow_out, deny_in and deny_out. See the manual for more detailed instructions. 14 | 15 | ## Design 16 | 17 | Eztables does not allow any communication between the networks it is connected to. However, if you want to allow a host to access the internet, you need to permit access to 'any' IP-address, which would include the other local networks. 18 | 19 | This is why Eztables on startup detects all networks and generates explicit 'default deny' rules that prevent these networks from talking to each other. These default deny rules take precedence over any rule that permit access from or to any host. 20 | 21 | This is why there are two major CHAINS or rulesets: LOOSE_RULES and STRICT_RULES. Loose rules always contain an 'any' as the source or destination'. These rules are only processed AFTER the default deny rules for security reasons. 22 | 23 | Strict rules are more specific and those rules can be used to allow traffic between hosts in different local networks. These rules are processed before the default deny rules. 24 | 25 | Understanding this design may help you to debug more elaborate firewall configurations. 26 | 27 | ## Minimum required TCP/UDP services 28 | 29 | When setting up firewall rules, it's important to keep in mind that these services are almost always needed: 30 | 31 | - DHCP (tcp/udp port 67/68) 32 | - DNS (tcp/udp port 53) 33 | - NTP (udp port 123) (Time sync) 34 | - HTTP(S) (tcp port 80 / 443) 35 | 36 | See the [examples.md][https://github.com/louwrentius/eztables/blob/master/EXAMPLES.md] file 37 | 38 | ## Overview of all commands 39 | 40 | - allow_in 41 | - allow_out 42 | - deny_in 43 | - deny_out 44 | - allow_forward 45 | - deny_forward 46 | - port_forward (special syntax) 47 | - nat (special syntax) 48 | - allow_icmp (special syntax) 49 | 50 | ## Debugging firewall rules 51 | 52 | To debug issues you may have to read the active iptables configuration (unfortunately). 53 | 54 | If debugging is enabled, all firewall rules are echoed back to the screen. To enable debug mode: 55 | 56 | ``` 57 | /etc/init.d/eztables start debug 58 | ``` 59 | 60 | You can also use this variable: 61 | 62 | ```sh 63 | export eztables_DEBUG=1 64 | ``` 65 | 66 | You can further debug firewall configuration issues with iptables itself. The -v option of iptables shows which rules are 'hit' by traffic. 67 | 68 | ``` 69 | iptables -v -n -L 70 | iptables -v -n -L -t nat 71 | ``` 72 | 73 | To prevent yourself from locking yourself out, test your firewall like this: 74 | 75 | ``` 76 | /etc/init.d/eztables start && sleep 30 && /etc/init.d/eztables stop 77 | ``` 78 | 79 | ## Network address translation (NAT) 80 | 81 | Setting up NAT is trivial. If you just want to allow some computers in a network to access the internet, you don't need to specify the 'interface' option. 82 | Eztables detects the internet interface automatically and if the interface is ommitted, it is assumed that this interface must be used. 83 | 84 | NAT can be used between any network, and in that case, the correct interface must be specified. 85 | 86 | nat 87 | 88 | A NAT rule by itself is not sufficient. By default - due to security reasons - Ezfirewall disables communication between networks. 89 | A forwarding rule must be configured in order to enable internet access. 90 | 91 | allow_forward "$eth1_net" any any "$WEB" 92 | 93 | ## Port Forwarding 94 | 95 | The first example just forwards incoming traffic on the internet-facing firewall interface (port 80) to the same port on the web server. If you omit the destination port, the to-be-forwarded port is used as the destination port. But the second example shows how to map traffic from the external port 80 to the internal port 8080 on the web server. 96 | 97 | port_forward "$FW_EXT" "$WWW_SERVER" 80 98 | 99 | port_forward "$FW_EXT" "$WWW_SERVER" 80 8080 100 | 101 | ## Object groups 102 | 103 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Eztables: simple yet powerful firewall configuration for Linux 2 | 3 |
4 | THIS PROJECT IS ABANDONED. DO NOT USE IT. 5 |
6 | 7 | Eztables allows you to quickly configure a firewall without ever touching iptables. The firewall rule syntax is designed to be easy to read and to apply. 8 | 9 | This is how you allow the entire internet to access your webserver on TCP-port 80: 10 | 11 | ```sh 12 | allow_in any $eth0 any 80/tcp 13 | ``` 14 | 15 | Eztables is designed to be simple, yet powerful. It doesn't matter if you want to protect your laptop, are setting up a home router, or building a company firewall. 16 | 17 | # Features 18 | 19 | * Basic input / output filtering 20 | * Network address translation (NAT) 21 | * Port address translation (PAT) 22 | * Support for VLANs 23 | * Working with Groups / Objects to aggregate hosts and services 24 | * Logging to syslog 25 | * Support for plugins 26 | * Automatically detects all network interfaces 27 | 28 | # Why should I consider using Eztables? 29 | 30 | Eztables has it's own firewall rule syntax that is significantly simpler than using iptables directly. A tool like [UFW](https://help.ubuntu.com/community/UFW) was made with a similar mindset, but it is more host-centric. It was never designed to be used as a general-purpose firewall script. 31 | 32 | Eztables on the other hand, can be used on any Linux box, wether it's a desktop, (virtual) server or network firewall. 33 | 34 | Eztables has support for advanced features like NAT and port-forwarding. But one of the most powerfull features is support for object groups, as found in most commercial firewalls and routers. Object groups are cool because they allow you to group hosts in 'objects' and refer to those objects in your rules. This allows you to keep the number of firewall rules relatively small and comprehensible. 35 | 36 | # Example: basic network 37 | 38 | With these two configuration lines, you can setup a functional home router. 39 | 40 | ```sh 41 | nat $eth1_net $eth0 42 | allow_forward $eth1_net any any any 43 | ``` 44 | 45 | That's easy right? It's just two lines because Eztables can detect all network interfaces and it can also figure out which interface is connected to the internet (eth0). 46 | 47 | This rule will allow SSH access to this router/firewall. 48 | 49 | ```sh 50 | allow_in $eth1_net $eth1 any 22/tcp 51 | ``` 52 | 53 | Note that SSH access is only granted from within the local network connected to the eth1 interface. 54 | 55 | If you also run a DHCP-server on this box, you need to allow clients acces with these rules: 56 | 57 | ```sh 58 | allow_in any $eth1 "$DHCP" "$DHCP" 59 | allow_out $eth1 any "$DHCP" "$DHCP" 60 | ``` 61 | 62 | The "$DHCP" variable should look like this: 63 | 64 | ```sh 65 | DHCP=" 66 | 67/udp 67 | 68/udp 68 | " 69 | ``` 70 | 71 | You may have to setup additional rules if you run a local DNS server or a [proxy server](http://louwrentius.com/setting-up-a-squid-proxy-with-clamav-anti-virus-using-c-icap.html). 72 | 73 | ## Working with object groups 74 | 75 | A main advantage of Eztables over other solutions is the possibility to define and use groups or objects. This is a feature commonly found on all commercial firewall products. 76 | 77 | Working with object groups allows you to keep your firewall ruleset small and simple. Let's take a look at the use of objects and groups. 78 | 79 | ```sh 80 | HTTP_SERVICES=" 81 | 80/tcp 82 | 443/tcp 83 | " 84 | 85 | WEB_SERVER_1=192.168.0.10 86 | WEB_SERVER_2=192.168.0.20 87 | WEB_SERVER_3=192.168.0.30 88 | 89 | WEB_SERVERS=" 90 | $WEB_SERVER_1 91 | $WEB_SERVER_2 92 | $WEB_SERVER_3 93 | " 94 | 95 | allow_in any "$WEB_SERVERS" any "$HTTP_SERVICES" 96 | ``` 97 | 98 | So object groups allow you to define firewall rules in a more abstract form, which is easy to maintain and to expand upon. 99 | 100 | You can even nest objects. For example, you can define an object $BASIC_SERVICES that contains the objects $DNS $HTTP_SERVICES and $NTP. 101 | 102 | ## Installation 103 | 104 | Run the install.sh file like: 105 | 106 | bash install.sh 107 | 108 | After that, review the configuration file: /etc/eztables/eztables.cfg 109 | 110 | Start the firewall like this: 111 | 112 | /etc/init.d/eztables start 113 | 114 | Be carefull not to lock yourself out if you test your rules. 115 | 116 | /etc/init.d/eztables start && sleep 30 && /etc/init.d/eztables stop 117 | 118 | ## Roadmap 119 | 120 | THERE IS NONE - THIS PROJECT IS ABANDONED 121 | 122 | - Traffic shaping plugin 123 | - IPv6 support 124 | - Support for multi-homed networks 125 | - See the issue section for more 126 | -------------------------------------------------------------------------------- /EXAMPLES.md: -------------------------------------------------------------------------------- 1 | # Configuration examples 2 | 3 | ## Minimum Viable Firewall / Router for home use 4 | 5 | Assumptions: 6 | 7 | - eth0 = connected to internet 8 | - eth1 = connected to home network 9 | - The Firewall itself can access the internet 10 | - Firewall management is only allowed from the home network (ssh) 11 | 12 | This firewall permits any out-going internet connections originating from either the firewall or the home network. 13 | 14 | ```sh 15 | 16 | nat $eth1_net $eth0 17 | allow_forward "$eth1_net" any any any 18 | 19 | allow_in $eth1_net $eth1 any "$SSH" 20 | allow_out $eth0 any any any 21 | 22 | ``` 23 | 24 | ## Home network with restrictions on outbound traffic 25 | 26 | - eth0 = connected to internet 27 | - eth1 = connected to home network 28 | - The home network only is allowed to access the most basic services: 29 | - HTTP(S) 30 | - DNS 31 | - NTP 32 | - DHCP 33 | - The Firewall itself has the same restrictions for outbound traffic 34 | - Firewall management is only allowed from the home network (ssh) 35 | - Only web-based email is supported (no SMTP/POP/IMAP) 36 | 37 | ```sh 38 | 39 | WEB=" 40 | 80/tcp 41 | 443/tcp 42 | " 43 | 44 | DNS=" 45 | 53/udp 46 | 53/tcp 47 | " 48 | 49 | NTP="123/udp" 50 | 51 | SSH="22/tcp" 52 | 53 | DHCP=" 54 | 67/udp 55 | 68/udp 56 | " 57 | 58 | BASIC_SERVICES=" 59 | $WEB 60 | $DNS 61 | $NTP 62 | " 63 | 64 | nat $eth1_net $eth0 65 | allow_forward "$eth1_net" any any "$BASIC_SERVICES" 66 | 67 | allow_in $eth1_net $eth1 any "$SSH" 68 | allow_out $eth0 any any "$BASIC_SERVICES" 69 | 70 | # 71 | # Permit DHCP 72 | # 73 | allow_in any $eth1 "$DHCP" "$DHCP" 74 | allow_out $eth1 any "$DHCP" "$DHCP" 75 | 76 | ``` 77 | 78 | 79 | ## Network with extra services NAT and port-forwarding 80 | 81 | In this scenario, the firewall is a DNS-server, NTP-server, PROXY-server and DHCP-server. 82 | 83 | ```sh 84 | ENABLE_SYSLOG=1 85 | 86 | # 87 | # Brute-force protection for SSH service 88 | # 89 | DENY_SSH_BF=1 90 | DENY_SSH_BF_IP="$eth0" 91 | DENY_SSH_BF_PORT=22 92 | 93 | WEBSERVER=192.168.0.20 94 | 95 | PROXY="3128/tcp" 96 | 97 | WEB=" 98 | 80/tcp 99 | 443/tcp 100 | " 101 | 102 | SSH="22/tcp" 103 | 104 | INTERNET_ACCESSIBLE_SERVICES=" 105 | $WEB 106 | $SSH 107 | " 108 | 109 | DNS=" 110 | 53/udp 111 | 53/tcp 112 | " 113 | 114 | NTP="123/udp" 115 | 116 | DHCP=" 117 | 67/udp 118 | 68/udp 119 | " 120 | 121 | MAIL=" 122 | 25/tcp 123 | 2525/tcp 124 | 110/tcp 125 | 995/tcp 126 | 143/tcp 127 | 993/tcp 128 | 587/tcp 129 | 465/tcp 130 | " 131 | 132 | LAN_SERVICES=" 133 | $DNS 134 | $NTP 135 | $SSH 136 | $PROXY 137 | " 138 | 139 | BASIC_SERVICES=" 140 | $WEB 141 | $MAIL 142 | " 143 | 144 | FIREWALL_OUTBOUND=" 145 | $DNS 146 | $NTP 147 | $WEB 148 | " 149 | 150 | 151 | 152 | # 153 | # Services accessible from the internal network 154 | # 155 | allow_in $eth1_net $eth1 any "$LAN_SERVICES" 156 | 157 | # 158 | # Required if you run DHCP 159 | # 160 | allow_in any $eth1 any "$DHCP" 161 | allow_out $eth1 any "$DHCP" "$DHCP" 162 | 163 | # 164 | # Allow the router/firewall to initiate connections to services on the Internet. 165 | # 166 | allow_out $eth0 any any "$FIREWALL_OUTBOUND" 167 | 168 | # 169 | # Basic NAT for the internal network connected to eth1. 170 | # 171 | nat $eth1_net $eth0 172 | 173 | # 174 | # Just NAT is not enough, it must be permitted to forward data. 175 | # 176 | allow_forward "$eth1_net" any any "$BASIC_SERVICES" 177 | 178 | # 179 | # If you only want users to access the internet through the proxy 180 | # you should remove '$WEB' from '$BASIC_SERVICES' 181 | # 182 | # Use WPAD through DNS for automatic client proxy configuration 183 | # 184 | 185 | # 186 | # Portforward port 80 on the internet interface towards the internal webserver 187 | # 188 | port_forward "$eth0" "$WEBSERVER" 80/tcp 189 | 190 | # 191 | # If you run a server in your local LAN and it get's hacked, your entire network 192 | # is then compromised! Consider setting up a DMZ for internet-accessible services. 193 | # 194 | 195 | # 196 | # If the web service is listening on port 8080 on the webserver, you need to 197 | # redirect the port as well: 198 | # 199 | port_forward "$eth0" "$WEBSERVER" 80/tcp 8080/tcp 200 | 201 | ``` 202 | 203 | ## Firewall with LAN and DMZ 204 | 205 | The firewall is connected to the internet and two separate networks. These networks can either be physical networks or VLANs. 206 | 207 | In this scenario we want to setup a webserver within the DMZ. If it gets hacked, an attacker cannot attack / access the LAN. 208 | 209 | The webserver can access some services on the internet for DNS, NTP, updates, etc. 210 | 211 | - Only the IT person can access the firewall. 212 | - No services running on the firewall are exposed to the web server. This would allow a possible point of entry for an attacker. 213 | - Ideally, you would have a separate DNS, NTP and update server within the DMZ, hardened as much as possible and the only system within the DMZ to be permitted to initiate outbound connections to the Internet. 214 | - eth0 = connected to internet 215 | - eth1 = connected to LAN 216 | - eth2 = connected to DMZ 217 | 218 | ```sh 219 | 220 | WEBSERVER=192.168.100.10 221 | 222 | BOFHSTATION=192.168.1.50 223 | 224 | WEB=" 225 | 80/tcp 226 | 443/tcp 227 | " 228 | 229 | DNS=" 230 | 53/udp 231 | 53/tcp 232 | " 233 | 234 | NTP="123/udp" 235 | 236 | SSH="22/tcp" 237 | 238 | DHCP=" 239 | 67/udp 240 | 68/udp 241 | " 242 | 243 | BASIC_SERVICES=" 244 | $WEB 245 | $DNS 246 | $NTP 247 | " 248 | 249 | nat $eth1_net $eth0 250 | allow_forward "$eth1_net" any any "$BASIC_SERVICES" 251 | 252 | allow_in "$BOFHSTATION" $eth1 any "$SSH" 253 | allow_in any $eth1 "$DHCP" "$DHCP" 254 | 255 | allow_out $eth0 any any "$BASIC_SERVICES" 256 | allow_out $eth1 any "$DHCP" "$DHCP" 257 | 258 | # 259 | # We permit the webserver to acces some services on the internet for operation. 260 | # Since the webserver only has a non-routable IP-address, NAT is required. 261 | # 262 | nat $eth2_net $eth0 263 | allow_forward "$eth2_net" any any "$BASIC_SERVICES" 264 | 265 | # 266 | # This rule permits the internet to access the webserver 267 | # 268 | port_forward "$eth0" "$WEBSERVER" 80/tcp 269 | 270 | ``` 271 | 272 | 273 | 274 | 275 | 276 | 277 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | -------------------------------------------------------------------------------- /eztables: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # 3 | # EZTABLES Iptables firewall shell script. 4 | # 5 | # Copyright 2013 (c) Louwrentius 6 | # 7 | # Source code released under the BSD license. 8 | # 9 | 10 | VERSION=1.0 11 | IPT=/sbin/iptables 12 | SOURCED="$0" 13 | ALL_NETWORKS="" 14 | 15 | if [ "$2" == "debug" ] 16 | then 17 | EZTABLES_DEBUG=1 18 | fi 19 | 20 | debug () { 21 | 22 | if [ "$EZTABLES_DEBUG" == "1" ] 23 | then 24 | echo "$@" 25 | fi 26 | } 27 | 28 | debug 29 | debug "============================" 30 | debug "EZTABLES firewall script $VERSION" 31 | debug "============================" 32 | 33 | if [ -z "$EZTABLES_DEBUG" ] 34 | then 35 | EZTABLES_DEBUG=0 36 | fi 37 | 38 | 39 | if [ -z "$EZTABLES_CONFIG" ] 40 | then 41 | EZTABLES_CONFIG="/etc/eztables/eztables.cfg" 42 | fi 43 | 44 | ipt-exec () { 45 | 46 | debug "$IPT $@" 47 | $IPT "$@" 48 | check_for_error "$?" 49 | } 50 | 51 | is_interface_up() { 52 | 53 | STATE=$(cat /sys/class/net/"$1"/operstate) 54 | if [ "$STATE" == "up" ] 55 | then 56 | return 0 57 | else 58 | return 1 59 | fi 60 | } 61 | 62 | cidr2mask() { 63 | local i mask="" 64 | local full_octets=$(($1/8)) 65 | local partial_octet=$(($1%8)) 66 | 67 | for ((i=0;i<4;i+=1)); do 68 | if [ $i -lt $full_octets ]; then 69 | mask+=255 70 | elif [ $i -eq $full_octets ]; then 71 | mask+=$((256 - 2**(8-$partial_octet))) 72 | else 73 | mask+=0 74 | fi 75 | test $i -lt 3 && mask+=. 76 | done 77 | 78 | echo $mask 79 | } 80 | 81 | get_ip () { 82 | 83 | ip -4 ad show dev $x | grep inet | awk '{ print $2 }' | cut -d "/" -f 1 84 | } 85 | 86 | get_mask () { 87 | 88 | MASK=`ip -4 ad show dev $x | grep inet | awk '{ print $2 }' | cut -d "/" -f 2` 89 | cidr2mask $MASK 90 | } 91 | 92 | get_network () { 93 | 94 | ip=$1 95 | nm=$2 96 | 97 | ip4="${ip##*.}" ; x="${ip%.*}" 98 | ip3="${x##*.}" ; x="${x%.*}" 99 | ip2="${x##*.}" ; x="${x%.*}" 100 | ip1="${x##*.}" 101 | 102 | nm4="${nm##*.}" ; x="${nm%.*}" 103 | nm3="${x##*.}" ; x="${x%.*}" 104 | nm2="${x##*.}" ; x="${x%.*}" 105 | nm1="${x##*.}" 106 | 107 | let sn1="$ip1&$nm1" 108 | let sn2="$ip2&$nm2" 109 | let sn3="$ip3&$nm3" 110 | let sn4="$ip1&$nm4" 111 | 112 | subnet="$sn1.$sn2.$sn3.$sn4" 113 | 114 | echo $subnet 115 | } 116 | 117 | mask2cidr() { 118 | nbits=0 119 | IFS=. 120 | for dec in $1 ; do 121 | case $dec in 122 | 255) let nbits+=8;; 123 | 254) let nbits+=7;; 124 | 252) let nbits+=6;; 125 | 248) let nbits+=5;; 126 | 240) let nbits+=4;; 127 | 224) let nbits+=3;; 128 | 192) let nbits+=2;; 129 | 128) let nbits+=1;; 130 | 0);; 131 | *) echo "Error: $dec is not recognised"; exit 1 132 | esac 133 | done 134 | echo "$nbits" 135 | } 136 | 137 | 138 | # 139 | # Create a variable name from every network interface containing it's IP. 140 | # Do this also for the network and cidr netmask. 141 | # 142 | 143 | debug 144 | debug "Detecting network interfaces..." 145 | debug 146 | 147 | #for x in `ifconfig -a | grep encap | awk '{ print $1}'` 148 | for x in `ip link | grep mtu | awk '{ print $2 }' | cut -d ":" -f 1 | cut -d "@" -f 1` 149 | do 150 | if [ "$x" == "lo" ] 151 | then 152 | continue 153 | fi 154 | 155 | if is_interface_up "$x" 156 | then 157 | 158 | RENAMED_INTERFACE=${x//./_} 159 | 160 | declare -A $RENAMED_INTERFACE=$( get_ip $x ) 161 | 162 | if [ -z "${!RENAMED_INTERFACE}" ] 163 | then 164 | continue 165 | fi 166 | 167 | 168 | MASK=$( get_mask $x ) 169 | CIDR=$( mask2cidr $MASK ) 170 | NETWORK=$( get_network ${!RENAMED_INTERFACE} $MASK ) 171 | 172 | declare -A "$RENAMED_INTERFACE"_net="$NETWORK/$CIDR" 173 | 174 | eval FULL_NET=\$${RENAMED_INTERFACE}_net 175 | ALL_NETWORKS="$ALL_NETWORKS $FULL_NET" 176 | 177 | eval tmpnetwork="\$${RENAMED_INTERFACE}_net" 178 | debug "Interface: $x | IP Address: ${!RENAMED_INTERFACE} | Netmask: $MASK | Network: $tmpnetwork" 179 | else 180 | debug "Interface: $x is down." 181 | fi 182 | done 183 | debug 184 | debug "All networks: $ALL_NETWORKS" 185 | 186 | 187 | 188 | init () { 189 | 190 | debug 191 | debug "Init..." 192 | debug 193 | 194 | /sbin/modprobe ip_conntrack 195 | /sbin/modprobe ip_conntrack_ftp 196 | 197 | echo 1 > /proc/sys/net/ipv4/ip_forward 198 | echo 1 > /proc/sys/net/ipv4/conf/all/rp_filter 199 | echo 1 > /proc/sys/net/ipv4/tcp_syncookies 200 | 201 | ipt-exec -A INPUT -i lo -j ACCEPT 202 | ipt-exec -A OUTPUT -o lo -j ACCEPT 203 | 204 | ENABLE_SYSLOG=0 205 | 206 | DEFAULT_POLICY=DROP 207 | STATE_POLICY=ESTABLISHED,RELATED 208 | DENY_POLICY="$DEFAULT_POLICY" 209 | 210 | ipt-exec -N ICMP 211 | ipt-exec -N TCP_SCRUB 212 | ipt-exec -N STRICT_RULES # host/network to host/network 213 | ipt-exec -N DEFAULT_DENY 214 | ipt-exec -N LOOSE_RULES # Any to host/network / any to any / host/network to any 215 | ipt-exec -N DROP_PACKETS 216 | 217 | INTERNET_INTERFACE=$(detect_internet_interface) 218 | 219 | 220 | . "$EZTABLES_CONFIG" 221 | } 222 | 223 | plugin_status () { 224 | 225 | PLUGIN_NAME="$1" 226 | STATUS="$2" 227 | 228 | abort_plugin_status () { 229 | 230 | echo "Wrong argument supplied to $PLUGIN_NAME: $STATUS (1/0) ." 231 | exit 1 232 | } 233 | 234 | if [ -z "$PLUGIN_NAME" ] 235 | then 236 | abort_plugin_status 237 | fi 238 | 239 | if [ -z "$STATUS" ] 240 | then 241 | abort_plugin_status 242 | fi 243 | 244 | if [ "$STATUS" == "1" ] 245 | then 246 | debug "Plugin $PLUGIN_NAME is enabled" 247 | else 248 | debug "Plugin $PLUGIN_NAME is disabled" 249 | fi 250 | } 251 | 252 | init_plugins () { 253 | 254 | debug 255 | debug "Loading plugins..." 256 | debug 257 | 258 | BASE=`dirname $EZTABLES_CONFIG` 259 | PLUGIN_DIR=$BASE/plugins 260 | 261 | RES1=`find $PLUGIN_DIR -perm -o+w | wc -l` 262 | RES2=`find $PLUGIN_DIR -perm -g+w | wc -l` 263 | 264 | if [ "$RES1" == "0" ] && [ "$RES2" == "0" ] 265 | then 266 | for x in `ls -1 $PLUGIN_DIR/*.eztables` 267 | do 268 | . $x 269 | done 270 | else 271 | echo 272 | echo "==============================================================================" 273 | echo "SECURITY ALERT: plugin directory and/or files are writable by group or others!" 274 | echo "Other users on the system could have added executable code to these files." 275 | echo "Review the contents of the plugins!" 276 | echo "==============================================================================" 277 | echo 278 | exit 1 279 | fi 280 | } 281 | 282 | detect_internet_interface () { 283 | ip route | grep default | awk '{ print $5 }' 284 | } 285 | 286 | set_default_policy () { 287 | 288 | debug 289 | debug "Default firewall policy..." 290 | debug 291 | 292 | if [ -z "$DEFAULT_POLICY" ] 293 | then 294 | DEFAULT_POLICY="DROP" 295 | fi 296 | 297 | ipt-exec -P INPUT $DEFAULT_POLICY 298 | ipt-exec -P FORWARD $DEFAULT_POLICY 299 | ipt-exec -P OUTPUT $DEFAULT_POLICY 300 | } 301 | 302 | configure_tcp_scrub () { 303 | 304 | ipt-exec -A TCP_SCRUB -p tcp --tcp-flags SYN,ACK SYN,ACK -m state --state NEW -j DROP_PACKETS 305 | ipt-exec -A TCP_SCRUB -p tcp -m state --state INVALID -j DROP_PACKETS 306 | ipt-exec -A TCP_SCRUB -p tcp -m state --state INVALID -j DROP_PACKETS 307 | ipt-exec -A TCP_SCRUB -p tcp ! --syn -m state --state NEW -j DROP_PACKETS 308 | ipt-exec -A TCP_SCRUB -p tcp ! --syn -m state --state NEW -j DROP_PACKETS 309 | } 310 | 311 | init_main_firewall () { 312 | 313 | debug 314 | debug "Setup basic firwall chains..." 315 | debug 316 | 317 | ipt-exec -A FORWARD -j TCP_SCRUB 318 | ipt-exec -A FORWARD -j ICMP 319 | ipt-exec -A FORWARD -j STRICT_RULES 320 | ipt-exec -A FORWARD -j DEFAULT_DENY 321 | ipt-exec -A FORWARD -j LOOSE_RULES 322 | 323 | ipt-exec -A INPUT -j TCP_SCRUB 324 | ipt-exec -A INPUT -j ICMP 325 | 326 | ipt-exec -A OUTPUT -j TCP_SCRUB 327 | ipt-exec -A OUTPUT -j ICMP 328 | 329 | ipt-exec -A INPUT -m state --state $STATE_POLICY -j ACCEPT 330 | ipt-exec -A OUTPUT -m state --state $STATE_POLICY -j ACCEPT 331 | ipt-exec -A FORWARD -m state --state $STATE_POLICY -j ACCEPT 332 | ipt-exec -A DEFAULT_DENY -m state --state $STATE_POLICY -j ACCEPT 333 | ipt-exec -A LOOSE_RULES -m state --state $STATE_POLICY -j ACCEPT 334 | ipt-exec -A STRICT_RULES -m state --state $STATE_POLICY -j ACCEPT 335 | } 336 | 337 | convert_any_to_port_range () { 338 | 339 | # 340 | # Any is shorthand for 1:65535 or all ports. 341 | # 342 | PORT="$1" 343 | if [ "$PORT" == "any" ] || [ "$PORT" == "ANY" ] 344 | then 345 | echo "1:65535" 346 | else 347 | echo "$PORT" 348 | fi 349 | } 350 | 351 | convert_any_to_ip () { 352 | 353 | # 354 | # Any is shorthand for 0.0.0.0/24 355 | # 356 | 357 | IP="$1" 358 | if [ "$IP" == "any" ] || [ "$IP" == "ANY" ] 359 | then 360 | echo "0.0.0.0/0" 361 | else 362 | echo "$IP" 363 | fi 364 | } 365 | 366 | deny_all_local_networks () { 367 | 368 | # 369 | # By default, make sure that none of the networks 370 | # attached to the machine can talk to each other. 371 | # 372 | 373 | debug 374 | debug "Protect local networks from each other..." 375 | debug 376 | 377 | if [ -z "$ALL_NETWORKS" ] 378 | then 379 | echo "Warning: if you have defined multiple networks, you should set ALL_NETWORKS." 380 | else 381 | for SRC_NETWORK in $ALL_NETWORKS 382 | do 383 | for DEST_NETWORK in $ALL_NETWORKS 384 | do 385 | if [ ! "$SRC_NETWORK" == "$DEST_NETWORK" ] 386 | then 387 | deny_forward "$SRC_NETWORK" "$DEST_NETWORK" any any 388 | fi 389 | done 390 | done 391 | fi 392 | debug 393 | } 394 | 395 | error () { 396 | 397 | INPUT="$@" 398 | 399 | echo "****************************************************************************************" 400 | echo "WARNING: An error occurred, see explanation for details, if any." 401 | echo "****************************************************************************************" 402 | if [ ! -z "$INPUT" ] 403 | then 404 | echo "REASON: $INPUT" 405 | else 406 | echo "No reason provided. Possible configuration file syntax error?" 407 | fi 408 | echo "****************************************************************************************" 409 | stop_firewall 410 | exit 1 411 | } 412 | 413 | nat () { 414 | 415 | INTERNAL_OBJECTS="$1" 416 | EXTERNAL_IP="$2" 417 | INTERFACE="$3" 418 | 419 | if [ -z "$INTERFACE" ] 420 | then 421 | if [ ! -z "$INTERNET_INTERFACE" ] 422 | then 423 | INTERFACE="$INTERNET_INTERFACE" 424 | else 425 | error "No NAT interface provided and INTERNET_INTERFACE not set." 426 | fi 427 | fi 428 | 429 | for SOURCE in $INTERNAL_OBJECTS 430 | do 431 | if [ "$DYNAMIC_IP" == "yes" ] 432 | then 433 | ipt-exec -t nat -A POSTROUTING -s "$SOURCE" -o "$INTERFACE" -j MASQUERADE 434 | else 435 | ipt-exec -t nat -A POSTROUTING -s "$SOURCE" -o "$INTERFACE" -j SNAT --to "$EXTERNAL_IP" 436 | fi 437 | done 438 | } 439 | 440 | check_for_error () { 441 | 442 | ERROR="$1" 443 | MSG="$2" 444 | 445 | if [ ! "$ERROR" == "0" ] 446 | then 447 | error "$MSG" 448 | fi 449 | } 450 | 451 | convert_port_range_to_dash () { 452 | 453 | PORT_RANGE="$1" 454 | PORT_RANGE=`echo "$PORT_RANGE" | sed s/:/-/g` 455 | echo "$PORT_RANGE" 456 | } 457 | 458 | convert_port_range_to_colon () { 459 | 460 | PORT_RANGE="$1" 461 | PORT_RANGE=`echo "$PORT_RANGE" | sed s/-/:/g` 462 | echo "$PORT_RANGE" 463 | } 464 | 465 | port_forward () { 466 | 467 | EXT_IP="$1" 468 | INT_IP="$2" 469 | EXT_PORTS="$3" 470 | INT_PORT="$4" 471 | SOURCE="$5" 472 | 473 | for EXTERNAL_PORT in $EXT_PORTS 474 | do 475 | PROTOCOL=`parse_port "$EXTERNAL_PORT" protocol` 476 | EXT_PORT_PARSED=`parse_port "$EXTERNAL_PORT" port` 477 | 478 | if [ -z "$INT_PORT" ] 479 | then 480 | INTERNAL_PORT=`convert_port_range_to_dash "$EXT_PORT_PARSED"` 481 | else 482 | INTERNAL_PORT_PARSED=`parse_port "$INT_PORT" port` 483 | INTERNAL_PORT=`convert_port_range_to_dash "$INTERNAL_PORT_PARSED"` 484 | fi 485 | 486 | if [ -z "$SOURCE" ] 487 | then 488 | ipt-exec -t nat -A PREROUTING -p "$PROTOCOL" -d "$EXT_IP" --dport "$EXT_PORT_PARSED" -j DNAT --to "$INT_IP":"$INTERNAL_PORT" 489 | else 490 | ipt-exec -t nat -A PREROUTING -p "$PROTOCOL" -s "$SOURCE" -d "$EXT_IP" --dport "$EXT_PORT_PARSED" -j DNAT --to "$INT_IP":"$INTERNAL_PORT" 491 | fi 492 | 493 | CONVERTED_INTERNAL_PORT=`convert_port_range_to_colon "$INTERNAL_PORT"` 494 | allow_forward any "$INT_IP" any "$CONVERTED_INTERNAL_PORT/$PROTOCOL" 495 | done 496 | } 497 | 498 | get_protocol () { 499 | 500 | SRC_PORT="$1" 501 | DST_PORT="$2" 502 | 503 | SRC_PROTOCOL=`parse_port "$SRC_PORT" protocol` 504 | DST_PROTOCOL=`parse_port "$DST_PORT" protocol` 505 | 506 | if [ -z "$SRC_PROTOCOL" ] && [ -z "$DST_PROTOCOL" ] 507 | then 508 | error "ERROR: source and destination protocol are not specified" 509 | fi 510 | 511 | if [ "$SRC_PROTOCOL" == "tcp" ] && [ "$DST_PROTOCOL" == "udp" ] 512 | then 513 | echo different 514 | break 515 | fi 516 | 517 | if [ "$SRC_PROTOCOL" == "udp" ] && [ "$DST_PROTOCOL" == "tcp" ] 518 | then 519 | echo different 520 | break 521 | fi 522 | 523 | if [ "$SRC_PROTOCOL" == "tcp" ] || [ "$DST_PROTOCOL" == "tcp" ] 524 | then 525 | echo tcp 526 | elif [ "$SRC_PROTOCOL" == "udp" ] || [ "$DST_PROTOCOL" == "udp" ] 527 | then 528 | echo udp 529 | elif [ "$SRC_PORT" == "any" ] && [ "$DST_PORT" == "any" ] 530 | then 531 | echo "tcp udp" 532 | fi 533 | } 534 | 535 | execute-firewall () { 536 | 537 | SOURCE="$1" 538 | DESTINATION="$2" 539 | SOURCE_PORT="$3" 540 | DESTINATION_PORT="$4" 541 | PROTOCOL="$5" 542 | POLICY="$6" 543 | CHAIN="$7" 544 | 545 | ipt-exec -A "$CHAIN" -p "$PROTOCOL" -s "$SOURCE" -d "$DESTINATION" --sport "$SOURCE_PORT" --dport "$DESTINATION_PORT" -m state --state NEW -j "$POLICY" 546 | } 547 | 548 | process_object_groups () { 549 | 550 | SOURCES="$1" 551 | DESTINATIONS="$2" 552 | SOURCE_PORTS="$3" 553 | DESTINATION_PORTS="$4" 554 | PROTOCOL="" 555 | CHAIN="$5" 556 | POLICY="$6" 557 | 558 | for SOURCE in $SOURCES 559 | do 560 | SOURCE=`convert_any_to_ip "$SOURCE"` 561 | for DESTINATION in $DESTINATIONS 562 | do 563 | DESTINATION=`convert_any_to_ip "$DESTINATION"` 564 | for SOURCE_PORT in $SOURCE_PORTS 565 | do 566 | for DEST_PORT in $DESTINATION_PORTS 567 | do 568 | PROTOCOLS=`get_protocol "$SOURCE_PORT" "$DEST_PORT"` 569 | if [ ! "$PROTOCOLS" == "different" ] 570 | then 571 | SOURCE_PORT=`parse_port "$SOURCE_PORT" port` 572 | DEST_PORT=`parse_port "$DEST_PORT" port` 573 | 574 | for PROTOCOL in $PROTOCOLS 575 | do 576 | execute-firewall "$SOURCE" "$DESTINATION" "$SOURCE_PORT" "$DEST_PORT" "$PROTOCOL" "$POLICY" "$CHAIN" 577 | done 578 | fi 579 | done 580 | done 581 | done 582 | done 583 | 584 | } 585 | 586 | process_traffic () { 587 | 588 | SOURCES="$1" 589 | DESTINATIONS="$2" 590 | SOURCE_PORTS="$3" 591 | DESTINATION_PORTS="$4" 592 | CHAIN="$5" 593 | POLICY="$6" 594 | 595 | process_object_groups "$SOURCES" "$DESTINATIONS" "$SOURCE_PORTS" "$DESTINATION_PORTS" "$CHAIN" "$POLICY" 596 | } 597 | 598 | allow_in () { 599 | process_traffic "$@" INPUT ACCEPT 600 | } 601 | 602 | allow_out () { 603 | 604 | process_traffic "$@" OUTPUT ACCEPT 605 | } 606 | 607 | allow_forward () { 608 | 609 | SRC="$1" 610 | DST="$2" 611 | CHAIN="" 612 | 613 | if [ "$SRC" == any ] || [ "$DST" == any ] 614 | then 615 | CHAIN="LOOSE_RULES" 616 | else 617 | CHAIN="STRICT_RULES" 618 | fi 619 | 620 | process_traffic "$@" "$CHAIN" ACCEPT 621 | } 622 | 623 | deny_in () { 624 | 625 | process_traffic "$@" INPUT "$DENY_POLICY" 626 | } 627 | 628 | deny_out () { 629 | 630 | process_traffic "$@" OUTPUT "$DENY_POLICY" 631 | } 632 | 633 | deny_forward () { 634 | 635 | process_traffic "$@" DEFAULT_DENY "$DENY_POLICY" 636 | } 637 | 638 | allow_icmp () { 639 | 640 | SRC="$1" 641 | DST="$2" 642 | 643 | if [ -z "$ICMP_TYPES" ] 644 | then 645 | ICMP_TYPES=" 646 | echo-request 647 | echo-reply 648 | destination-unreachable 649 | " 650 | fi 651 | 652 | for source in $SRC 653 | do 654 | host=`convert_any_to_ip "$source"` 655 | for dest in $DST 656 | do 657 | dest=`convert_any_to_ip "$dest"` 658 | for parameter in $ICMP_TYPES 659 | do 660 | ipt-exec -A ICMP -p icmp -s "$host" -d "$dest" --icmp-type "$parameter" -j ACCEPT 661 | done 662 | done 663 | done 664 | } 665 | 666 | block_everything_else () { 667 | 668 | if [ "$ENABLE_SYSLOG" == "1" ] 669 | then 670 | ipt-exec -A INPUT -j DROP_PACKETS 671 | ipt-exec -A FORWARD -j DROP_PACKETS 672 | ipt-exec -A OUTPUT -j DROP_PACKETS 673 | 674 | #ipt-exec -A DROP_PACKETS -p all -j LOG --log-prefix " * BLOCKING * " 675 | fi 676 | ipt-exec -A DROP_PACKETS -j DROP 677 | } 678 | 679 | run_checks () { 680 | 681 | if [ ! -e "$IPT" ] 682 | then 683 | error "$IPT executable not present at ipt-exec please provide correct location." 684 | fi 685 | 686 | if [ ! -e "$EZTABLES_CONFIG" ] 687 | then 688 | error "Ruleset "$EZTABLES_CONFIG" not found." 689 | fi 690 | } 691 | 692 | parse_port () { 693 | 694 | INPUT="$1" 695 | PART="$2" 696 | 697 | if [ "$PART" == "port" ] 698 | then 699 | PART=1 700 | elif [ "$PART" == "protocol" ] 701 | then 702 | PART=2 703 | else 704 | error "$FUNCNAME Error, provide port/protocol as second argument" 705 | fi 706 | 707 | RESULT=`echo "$INPUT" | cut -d "/" -f "$PART"` 708 | 709 | if [ "$PART" == "1" ] && [ -z "$RESULT" ] 710 | then 711 | error "Rule contains incorrect port value." 712 | elif [ "$PART" == "2" ] && [ -z "$RESULT" ] 713 | then 714 | RESULT="all" 715 | fi 716 | if [ "$RESULT" == "any" ] || [ "$RESULT" == "ANY" ] && [ "$PART" == "2" ] 717 | then 718 | RESULT="all" 719 | fi 720 | 721 | RESULT=`convert_any_to_port_range "$RESULT"` 722 | 723 | echo "$RESULT" 724 | } 725 | 726 | lock_file () { 727 | 728 | LOCK_FILE="/var/run/eztables.lock" 729 | 730 | ACTION=$1 731 | if [[ $ACTION == "create" ]] 732 | then 733 | touch $LOCK_FILE 734 | fi 735 | if [[ $ACTION == "remove" ]] 736 | then 737 | if [ -e $LOCK_FILE ] 738 | then 739 | rm $LOCK_FILE 740 | fi 741 | fi 742 | } 743 | 744 | stop_firewall () { 745 | 746 | 747 | debug 748 | debug "Flusing iptables ruleset.." 749 | debug 750 | 751 | 752 | ipt-exec -F 753 | ipt-exec -F -t nat 754 | ipt-exec -X 755 | ipt-exec -P INPUT ACCEPT 756 | ipt-exec -P OUTPUT ACCEPT 757 | ipt-exec -P FORWARD DROP 758 | ipt-exec -F -t mangle 759 | 760 | lock_file remove 761 | } 762 | 763 | check_insecure_configuration () { 764 | 765 | debug 766 | if [ "$DEFAULT_POLICY" == "ACCEPT" ] 767 | then 768 | echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" 769 | echo "!!! SECURITY RISK - WARNING !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" 770 | echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" 771 | echo 772 | echo "DEEFAULT FIREWALL POLICY IS ACCEPT." 773 | echo 774 | echo "ONLY TO BE USED FOR INITIAL TESTING PURPOSES." 775 | echo 776 | echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" 777 | echo "!!! SECURITY RISK - WARNING !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" 778 | echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" 779 | fi 780 | } 781 | 782 | start_firewall () { 783 | 784 | run_checks 785 | stop_firewall 786 | init 787 | init_plugins 788 | check_insecure_configuration 789 | configure_tcp_scrub 790 | deny_all_local_networks 791 | set_default_policy 792 | init_main_firewall 793 | block_everything_else 794 | lock_file create 795 | } 796 | 797 | main () { 798 | 799 | if [ "$1" == "start" ] 800 | then 801 | echo "Starting eztables..." 802 | start_firewall 803 | 804 | elif [ "$1" == "stop" ] 805 | then 806 | echo "Stopping eztables..." 807 | stop_firewall 808 | exit 0 809 | elif [ "$1" == "version" ] 810 | then 811 | echo "EZTABLES version $VERSION" 812 | exit 0 813 | else 814 | echo "Usage: $0 (start/stop)" 815 | exit 1 816 | fi 817 | } 818 | 819 | are_we_sourced () { 820 | 821 | RES=`basename $SOURCED` 822 | 823 | if [ "$RES" = "eztables" ] 824 | then 825 | return 1 826 | else 827 | return 0 828 | fi 829 | } 830 | 831 | if ! are_we_sourced 832 | then 833 | main "$1" 834 | fi 835 | 836 | debug 837 | --------------------------------------------------------------------------------