Showing posts with label linux. Show all posts
Showing posts with label linux. Show all posts

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

Monday, June 5, 2017

Trying to find what process is using a certain port

If you run into the situation where you quickly want to know which process is using a certain (TCP) port, use the following command:

pi@server:~ $ sudo ss -lptn 'sport = :443'
State      Recv-Q Send-Q        Local Address:Port          Peer Address:Port 
LISTEN     0      128                       *:443                      *:*      users:(("nginx",pid=29909,fd=8),("nginx",pid=29908,fd=8),("nginx",pid=29907,fd=8),("nginx",pid=29906,fd=8),("nginx",pid=29905,fd=8))
LISTEN     0      128                      :::443                     :::*      users:(("nginx",pid=29909,fd=9),("nginx",pid=29908,fd=9),("nginx",pid=29907,fd=9),("nginx",pid=29906,fd=9),("nginx",pid=29905,fd=9))

Sunday, May 21, 2017

Linux login as www-data user

If you ever need to login as apache (www-data) user to test permissions on a file share, you can use the following command to impersonate yourself as the Apache user:

root@rp:~# su - www-data -s /bin/bash
www-data@rp:~$ cd /var/lib/phpfina
www-data@rp:/var/lib/phpfina$ vi test

Sunday, February 12, 2017

Building custom Linux kernel (howto)

Just summarizing what I did to build my own custom Debian Linux kernel. As I needed a few modules which where not shipped in the standard Debian kernel. All is based on this tutorial which I ran on a Debian 8.6 with 3.16 kernel. Make sure you're root.

mkdir /root/custom-kernel
cd /root/custom-kernel
apt-get install fakeroot linux-source-3.16 kernel-package libncurses5-dev
tar xf /usr/src/linux-source-3.16.tar.xz
cd linux-source-3.16
make menuconfig
make-kpkg clean
fakeroot make-kpkg --initrd --revision=001
dpkg -i linux-image-3.16.39_001_i386.deb
shutdown -r now

Thursday, January 12, 2017

Alternative way to be testing command line if port is open or closed

Trying to find out on a Linux server or under Busybox (Synology) if a TCP port is open or closed? Use this handy command:

exec 6<>/dev/tcp/127.0.0.1/443 || echo "Port is not open"
exec 6>&- # close output connection
exec 6<&- # close input connection

6 is used as the file descriptor. 0,1,2 are stdin, stdout, and stderr. 5 is sometimes used by Bash for child processes, so 3,4,6,7,8, and 9 should be safe.

Alternatively, if the port you're probing is serving the HTTP(S) protocol:

exec 6<>/dev/tcp/127.0.0.1/443
echo -e "GET / HTTP/1.0\n" >&6
cat <&6

Alternative ways are listed here.

Monday, September 19, 2016

Error installing tftpd-hpa onto Raspian: action "start" failed

What is the issue?

When trying to install the tftpd-hpa package, the installation isn't completed succesfully and the daemon is not running or cannot be started.

root@raspberrypi:/srv/tftp# apt-get install tftpd-hpa
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
  ffmpeg openbsd-inetd
Use 'apt-get autoremove' to remove them.
Suggested packages:
  syslinux-common
The following NEW packages will be installed:
  tftpd-hpa
0 upgraded, 1 newly installed, 0 to remove and 5 not upgraded.
Need to get 0 B/46.1 kB of archives.
After this operation, 142 kB of additional disk space will be used.
Preconfiguring packages ...
Selecting previously unselected package tftpd-hpa.
(Reading database ... 85095 files and directories currently installed.)
Unpacking tftpd-hpa (from .../tftpd-hpa_5.2-4_armhf.deb) ...
Processing triggers for man-db ...
Setting up tftpd-hpa (5.2-4) ...

tftpd-hpa directory (/srv/tftp) already exists, doing nothing.
[....] Starting HPA's tftpd: in.tftpdinvoke-rc.d: initscript tftpd-hpa, action "start" failed.
dpkg: error processing tftpd-hpa (--configure):
 subprocess installed post-installation script returned error exit status 71
Errors were encountered while processing:
 tftpd-hpa
E: Sub-process /usr/bin/dpkg returned an error code (1)

How to fix?

Edit the tftpd-hpa config file, which is normally located under /etc/default:
vi /etc/default/tftpd-hpa
Change the IP from 0.0.0.0 to the real IP of you tftpd-hpa server machine.
Add into the option parameter "--ipv4"

Force a re-installation of tftpd-hpa.

root@raspberrypi:/srv/tftp# apt-get install -f tftpd-hpa
Reading package lists... Done
Building dependency tree
Reading state information... Done
tftpd-hpa is already the newest version.
The following packages were automatically installed and are no longer required:
  ffmpeg openbsd-inetd
Use 'apt-get autoremove' to remove them.
0 upgraded, 0 newly installed, 0 to remove and 5 not upgraded.
1 not fully installed or removed.
After this operation, 0 B of additional disk space will be used.
Do you want to continue [Y/n]? Y
Setting up tftpd-hpa (5.2-4) ...
tftpd user (tftp) already exists, doing nothing.
tftpd-hpa directory (/srv/tftp) already exists, doing nothing.
[ ok ] Starting HPA's tftpd: in.tftpd.

Now tftpd-hpa is starting properly and by default listening onto UDP port 69.