Showing posts with label iptables. Show all posts
Showing posts with label iptables. Show all posts

Monday, March 11, 2019

Making a Wifi router from your RPi and force traffic of your Kodi through it

Sometimes, you can run out of bandwidth in your ethernet connected home network. Yet, connecting to a Wifi hotspot somewhere and serving other devices in your network that are bandwidth intensive (like your Kodi) can overcome this issue.

I have the following devices in scope for this:

  • My regular Internet router is connected through a wired connection (Internal network 192.168.0.0/24)
  • A RPi1 that is connected through eth0 (wired) to the home network (IP 192.168.0.30). It has also a Wifi interface wlan0 that is connected to the public internet.
  • Another RPi2 that is serving Kodi to a TV also connected through eth0 to the home network (IP 192.168.0.40).
RPi1 will be the router that will connect to the Internet through wlan0. RPi2 will be configured to route all of the Internet requests to RPi1 over wired LAN.
Please note that an alternative way to configure Wifi on a RPi (e.g. serving Kodi from OSMC) can be found in this blogpost.
  1. Configure RPi1 to access the Wifi hotspot
    1. Scan your environment for the Wifi network
    2. iwlist wlan0 scan
    3. Edit the Wlan configuration
    4. sudo vi /etc/wpa_supplicant/wpa_supplicant.conf
      Add this config:
      network={
      ssid="ssid"
      scan_ssid=1
      key_mgmt=WPA-EAP
      group=CCMP TKIP
      eap=PEAP
      identity="username"
      password="password"
      phase1="peapver=0"
      phase2="MSCHAPV2"
      }
      
    5. Save and test your config
    6. wpa_cli -i wlan0 reconfigure
    7. Check if your wlan0 device has received an IP address
    8. ifconfig wlan0 or wpa_cli -i wlan0 status
      Output:
      wlan0: flags=-28605  mtu 1500
              inet 151.164.43.34  netmask 255.255.255.0  broadcast 151.164.43.255
              ether b8:27:aa:aa:ff:c1  txqueuelen 1000  (Ethernet)
              RX packets 256109  bytes 319396252 (304.6 MiB)
              RX errors 0  dropped 0  overruns 0  frame 0
              TX packets 187107  bytes 24877570 (23.7 MiB)
              TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    9. Install a cron task to regularly reconfigure the wlan0 interface if the Wifi connection gets disconnected
  2. Create a router of your RPi1
    1. vi /etc/iptables/rules.v4
      Add this config:
      *nat
      -A POSTROUTING -s 192.168.0.0/24 -o wlan0 -j MASQUERADE
      COMMIT
      
      *filter
      -A INPUT -i lo -j ACCEPT
      # allow ssh, so that we do not lock ourselves
      -A INPUT -i eth0 -p tcp -m tcp --dport 22 -j ACCEPT
      # allow incoming traffic to the outgoing connections,
      # et al for clients from the private network
      -A INPUT -m state --state NEW,RELATED,ESTABLISHED -j ACCEPT
      -A OUTPUT -p icmp --icmp-type 8 -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT
      -A INPUT -p icmp --icmp-type 0 -m state --state ESTABLISHED,RELATED -j ACCEPT
      # prohibit everything else incoming
      #-A INPUT -i eth0 -j DROP
      COMMIT
    2. Store your config
    3. iptables-restore < /etc/iptables/rules.v4
    4. Check if the rules are in effect
    5. root@hass:~# iptables -L
      Chain INPUT (policy ACCEPT)
      target     prot opt source               destination         
      ACCEPT     all  --  anywhere             anywhere            
      ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:ssh
      ACCEPT     all  --  anywhere             anywhere             state NEW,RELATED,ESTABLISHED
      ACCEPT     icmp --  anywhere             anywhere             icmp echo-reply state RELATED,ESTABLISHED
      
      Chain FORWARD (policy ACCEPT)
      target     prot opt source               destination         
      
      Chain OUTPUT (policy ACCEPT)
      target     prot opt source               destination         
      ACCEPT     icmp --  anywhere             anywhere             icmp echo-request state NEW,RELATED,ESTABLISHED
      
  3. On your RPi2 add a static route to use RPi1 as the gateway for Internet traffic
    1. Test what external IP is being used on RPi2
    2. root@kodi1:/lib# curl ifconfig.me
      64.160.13.75
    3. Add the route
    4. route add -net default gw 192.168.0.30 netmask 0.0.0.0 dev eth0
    5. Test for your externalIP again, it should be different now
    6. root@kodi1:/lib# curl ifconfig.me
      151.164.43.34
    7. Also test if you can ping to www.google.com from RPi2
    8. Now all Internet traffic on RPi2 (192.168.0.40) will be routed through RPi1 (192.168.0.30)
    9. root@kodi1:~# route -n
      Kernel IP routing table
      Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
      0.0.0.0         192.168.0.30    0.0.0.0         UG    0      0        0 eth0
      192.168.0.0     0.0.0.0         255.255.255.255 UH    0      0        0 eth0
      192.168.0.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0

Sunday, February 25, 2018

An example Iptables rules file for your Raspberry Pi (and have it applied after each reboot)

Having an Iptables firewall rule set applied onto Raspbian/Debian is fairly easy. I'm trying to build something that is easy to manage, has a certain degree of default security applied, yet will allow that I can easily apply it onto several Raspberry Pi devices.
When adding rules to your running Iptables, they become lost each time you'd reboot or restart your firewall. So, I'm trying to overcome that.

First, let's start with my basic rule set file, which is build around a number of services that I need on my Raspberry Pi:

  • SSH server
  • OpenVPN server
  • HTTP(S) server
  • DNS server
  • Samba server
  • Transmission server
Of course, you can add and customize as much as you want, but here's my example iptables.rules.v4 file:

# Generated by iptables-save v1.6.0 on Sun Feb 18 13:27:56 2018
*nat
:PREROUTING ACCEPT [485:82476]
:INPUT ACCEPT [24:2229]
:OUTPUT ACCEPT [192:15907]
:POSTROUTING ACCEPT [192:15907]
-A POSTROUTING -s 10.8.0.0/24 -o eth0 -m comment --comment "Allow OpenVPN routing from source 10.8.0.0 to eth0" -j MASQUERADE
COMMIT
# Completed on Sun Feb 18 13:27:56 2018
# Generated by iptables-save v1.6.0 on Sun Feb 18 13:27:56 2018
*filter
:INPUT DROP [67:11459]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [687:299583]
:f2b-openvpn - [0:0]
:f2b-sshd - [0:0]
-A INPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE -m comment --comment "Block null packets" -j DROP
-A INPUT -p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -m state --state NEW -m comment --comment "Block a syn-flood attack" -j DROP
-A INPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,SYN,RST,PSH,ACK,URG -m comment --comment "Block Xmas packets" -j DROP
# localhost
-A INPUT -i lo -m comment --comment "Allow localhost traffic" -j ACCEPT
-A INPUT -i lo -p tcp -m tcp --dport 4711:4720 -m comment --comment "TODO" -j ACCEPT
# Established connections
-A INPUT -m state --state RELATED,ESTABLISHED -m comment --comment "Allow all established inbound connections" -j ACCEPT
# DNS server
-A INPUT -p udp -m udp --dport 53 -m comment --comment "Allow DNS to this host from anywhere" -j ACCEPT
-A INPUT -p tcp -m tcp --dport 53 -m comment --comment "Allow DNS to this host from anywhere" -j ACCEPT
# OpenVPN server
-A INPUT -p tcp -m multiport --dports 1194 -m comment --comment "Allow OpenVPN to this host from anywhere" -j f2b-openvpn
# SSH server
-A INPUT -p tcp -m multiport --dports 22 -m comment --comment "Allow SSH to this host from anywhere" -j f2b-sshd
-A INPUT -p icmp -m icmp --icmp-type 8 -m comment --comment "Allow ping to this host from anywhere" -j ACCEPT
# HTTP server
-A INPUT -p tcp -m tcp --dport 80 -m comment --comment "Allow HTTP to this host from anywhere" -j ACCEPT
# SSLH multiplexer
-A INPUT -p tcp -m tcp --dport 443 -m comment --comment "Allow HTTPS to this host from anywhere" -j ACCEPT
# SSH server
-A INPUT -p tcp -m tcp --dport 22 -m comment --comment "Allow SSH to this host from anywhere" -j ACCEPT
# Samba server
-A INPUT -p tcp -m multiport --dports 139,445 -m comment --comment "Allow Samba to this host from anywhere" -j ACCEPT
-A INPUT -p udp -m multiport --dports 137,138 -m comment --comment "Allow Samba to this host from anywhere" -j ACCEPT
# Transmission server
-A INPUT -p tcp -m tcp --dport 9091 -m comment --comment "Allow Transmission to this host from anywhere" -j ACCEPT
# Reject rules
-A INPUT -m comment --comment "Reject all other inboud traffic, unless specified" -j REJECT --reject-with icmp-port-unreachable
-A FORWARD -m comment --comment "Reject all other inboud traffic, unless specified" -j REJECT --reject-with icmp-port-unreachable
-A f2b-openvpn -j RETURN
-A f2b-sshd -j RETURN
COMMIT
# Completed on Sun Feb 18 13:27:56 2018

Would you be wanting to be more specific on the rules (e.g. SSH access), you can easily add a source IP to the rule to further limit access.

# SSH server
-A INPUT -s 192.168.1.0/24 -p tcp -m tcp --dport 22 -m comment --comment "Allow SSH to this host from anywhere" -j ACCEPT

We will now be saving this file, so that Iptables can use if after each reboot or restart.
  1. Make youself root:
    sudo -i
  2. Go to the Iptables folder:
    cd /etc/iptables/
  3. Create a backup of the default rule set:
    cp -rp rules.v4 rules.v4.ori
  4. Edit the rules.v4 file with vi, remove all entries and paste the rule set listed above
    vi rules.v4
  5. List your current Iptables rules:
    iptables -L
  6. Import the new rule set:
    iptables-restore < /etc/rules.v4
  7. List your Iptables rules again and you should see the new rule set applied:
    iptables -L
  8. Make the rules survive a reboot by creating this pre-up file:
    vi /etc/network/if-pre-up.d/iptables
  9. Add this content to the file:
    #!/bin/sh
    /sbin/iptables-restore < /etc/iptables.up.rules
  10. Make the file executable:
    chmod +x /etc/network/if-pre-up.d/iptables
  11. Reboot to test
If you later on want to add or change rules, you can change your rules.v4 file, or add them command line to your running configuration. Adding them to your rules.v4 file will make them persist. In the latter case, you'd have to dump the running configuration into your rules.v4 file by applying:
iptables-save > /etc/iptables/rules.v4