Tűzfal (firewall)

A Linux gépeken már régi téma a tűzfal. A tűzfal arra való, hogy védelmet nyújtson a magánhálózatoknak illetve a számítógépeknek az Interneten garázdálkodó rosszakaratú támadók ellen. Éppen ezért beszélhetünk hálózat-biztonságról – erről nálam itthon a router-en lévő tűzfal gondoskodik -, illetve hoszt-biztonságról, ami a Linux notebookom-on van telepítve. Ma már általában egyszerűek a beállítások, mind hálózati, mind hoszt szinten. Debian 10 alatt már automatikus az alap tűzfal-konfiguráció.

Régebben az ipchains volt, ma már az ún. iptables van használatban. Nézzük meg a Linux-unkat!

root@gergo1:~# iptables -L -v
Chain INPUT (policy DROP 590 packets, 93015 bytes)
pkts bytes target prot opt in out source destination
0 0 ACCEPT tcp -- any any anywhere anywhere tcp dpt:ssh
256 20230 ACCEPT all -- lo any anywhere anywhere
6008 2038K ACCEPT all -- any any anywhere anywhere ctstate RELATED,ESTABLISHED
0 0 ACCEPT tcp -- any any 192.168.0.0/24 anywhere state NEW tcp dpt:netbios-ns
0 0 ACCEPT tcp -- any any 192.168.0.0/24 anywhere state NEW tcp dpt:netbios-dgm
0 0 ACCEPT tcp -- any any 192.168.0.0/24 anywhere state NEW tcp dpt:netbios-ssn
0 0 ACCEPT tcp -- any any 192.168.0.0/24 anywhere state NEW tcp dpt:microsoft-ds
0 0 ACCEPT icmp -- any any anywhere anywhere icmp echo-request
6 336 ACCEPT tcp -- any any anywhere anywhere tcp dpt:http
Chain FORWARD (policy DROP 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 6272 packets, 1041K bytes)
pkts bytes target prot opt in out source destination
256 20230 ACCEPT all -- any lo anywhere anywhere

Látható, hogy a bemeneten (INPUT) az SSH szerver, a Samba szerver (Microsoft hálózathoz) és a HTTP szerver van engedélyezve + az ICMP ping (leírása később), továbbá természetesen a már létrejött kapcsolatok is. Forward szinten semmi sincs “átengedve”. Nem továbbítunk adatot hálózatok között. Kimeneten (OUTPUT) pedig mindent “kiengedünk”.

Hogy hol van beállítva? Van egy pici konfigurációs file a /etc alatt.

root@gergo1:~# cat /etc/network/iptables.up.rules
# Generated by iptables-save v1.6.0 on Sat Aug 26 03:40:19 2017
*filter
:INPUT DROP [9:1026]
:FORWARD DROP [0:0]
:OUTPUT ACCEPT [22:3482]
-A INPUT -p tcp -m tcp --dport 22 -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
-A OUTPUT -o lo -j ACCEPT
#
# added by gvamosi on 2019-06-07
#
-A INPUT -s 192.168.0.0/24 -m state --state NEW -p tcp --dport 137 -j ACCEPT
-A INPUT -s 192.168.0.0/24 -m state --state NEW -p tcp --dport 138 -j ACCEPT
-A INPUT -s 192.168.0.0/24 -m state --state NEW -p tcp --dport 139 -j ACCEPT
-A INPUT -s 192.168.0.0/24 -m state --state NEW -p tcp --dport 445 -j ACCEPT
-A INPUT -p icmp --icmp-type echo-request -j ACCEPT
#
# added by gvamosi on 2019-09-22
#
-A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
#
COMMIT
# Completed on Sat Aug 26 03:40:19 2017

És hogyan “töltődik be”? Az ún. init script-ek között kell keresnünk a választ (leírásuk később).

root@gergo1:~# cat /etc/init.d/iptables
#!/bin/sh
echo -n "Applying firewall rules.."
/sbin/iptables-restore < /etc/network/iptables.up.rules
echo "done."

Ez a kicsi init script tölti be a rendszer elindulásakor a tűzfal konfigurációját az iptables-restore parancs segítségével. Ennyi az egész. 🙂

Írhatnánk még az ún. DMZ (Demilitarizált Zóna) fogalmáról is. Ez egy olyan terület egy cégnél pl., ahonnan szerverek “kifelé”, a nagyvilágba, azaz az Internet felé is látszódhatnak. Határzóna a belső, védett, és a külső, védtelen hálózat között.

Secure Shell – biztonságos távkapcsolat két számítógép között – ssh, scp egy Mac mini OS X és egy Linux között

Két Linux/UNIX alapú számítógép között a biztonságos kapcsolatot, titkosított adatátvitelt az úgynevezett secure shell, azaz ssh kapcsolat jelenti. Alapvetően kliens-szerver kapcsolat, és rémesen egyszerű, ha az ember egyszer ráérez.

Először is felfedeztem, hogy az sshd, ssh daemon – vagyis a szerver – nincs installálva a Debian 10-em alatt. No ezen könnyű volt segíteni. Debian alatt alapvetően az apt-get install paranccsal lehet valamit telepíteni (rendszergazdaként). Kapásból legenerálja a szükséges kulcsokat, úgyhogy nem sok dolog van vele. 🙂

root@gergo1:~# apt-get install ssh
Reading package lists… Done
Building dependency tree
Reading state information… Done
The following additional packages will be installed:
openssh-server openssh-sftp-server
Suggested packages:
molly-guard monkeysphere rssh ssh-askpass ufw
The following NEW packages will be installed:
openssh-server openssh-sftp-server ssh
0 upgraded, 3 newly installed, 0 to remove and 0 not upgraded.
Need to get 599 kB of archives.
After this operation, 1,829 kB of additional disk space will be used.
Do you want to continue? [Y/n]
Get:1 http://cdn-fastly.deb.debian.org/debian buster/main amd64 openssh-sftp-server amd64 1:7.9p1-10 [44.6 kB]
Get:2 http://cdn-fastly.deb.debian.org/debian buster/main amd64 openssh-server amd64 1:7.9p1-10 [352 kB]
Get:3 http://cdn-fastly.deb.debian.org/debian buster/main amd64 ssh all 1:7.9p1-10 [202 kB]
Fetched 599 kB in 2s (388 kB/s)
Preconfiguring packages …
Selecting previously unselected package openssh-sftp-server.
(Reading database … 488002 files and directories currently installed.)
Preparing to unpack …/openssh-sftp-server_1%3a7.9p1-10_amd64.deb …
Unpacking openssh-sftp-server (1:7.9p1-10) …
Selecting previously unselected package openssh-server.
Preparing to unpack …/openssh-server_1%3a7.9p1-10_amd64.deb …
Unpacking openssh-server (1:7.9p1-10) …
Selecting previously unselected package ssh.
Preparing to unpack …/ssh_1%3a7.9p1-10_all.deb …
Unpacking ssh (1:7.9p1-10) …
Setting up openssh-sftp-server (1:7.9p1-10) …
Setting up openssh-server (1:7.9p1-10) …
Creating config file /etc/ssh/sshd_config with new version
Creating SSH2 RSA key; this may take some time …
2048 SHA256:dOGw/PajE/BBgatmagHg384uQbysjJY2wr6m8WCkfus root@gergo1 (RSA)
Creating SSH2 ECDSA key; this may take some time …
256 SHA256:j8q+pBRwTqbiAvP1X4pL+pHUj7G4aoojCBGv7FYqWIE root@gergo1 (ECDSA)
Creating SSH2 ED25519 key; this may take some time …
256 SHA256:yn8li9pazT1UXV3T+gmo44boWeJU1S6HA7bKDgVpKc0 root@gergo1 (ED25519)
Created symlink /etc/systemd/system/sshd.service → /lib/systemd/system/ssh.service.
Created symlink /etc/systemd/system/multi-user.target.wants/ssh.service → /lib/systemd/system/ssh.service.
rescue-ssh.target is a disabled or a static unit, not starting it.
Setting up ssh (1:7.9p1-10) …
Processing triggers for man-db (2.8.5-2) …
Processing triggers for systemd (241-5) …

Aztán az első “belépés” OS X-ről az ssh paranccsal. A Linux gép ip címe 192.168.0.109, lekérdezni command prompt-ból OS X alatt az “ifconfig“, Debian alatt az “ip addr“, illetve az “ip a” paranccsal lehet. Természetesen a hálózatban “belső ip címeket” használunk, az összes gép “tűzfal mögött” van. 🙂

Mac-mini:~ gvamosi$ ssh 192.168.0.109
The authenticity of host '192.168.0.109 (192.168.0.109)' can't be established.
ECDSA key fingerprint is SHA256:j8q+pBRwTqbiAvP1X4pL+pHUj7G4aoojCBGv7FYqWIE.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.0.109' (ECDSA) to the list of known hosts.
gvamosi@192.168.0.109's password:
Linux gergo1 4.19.0-5-amd64 #1 SMP Debian 4.19.37-5 (2019-06-19) x86_64
The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Wed Sep 4 07:08:30 2019
gvamosi@gergo1:~$

Ezek után a w (who is) paranccsal megnézhetjük a bejelentkezetteket.

gvamosi@gergo1:~$ w
18:21:32 up 2 days, 7:36, 2 users, load average: 0.33, 0.60, 0.61
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
gvamosi tty2 tty2 Sat10 2days 1:24m 0.64s /opt/google/chrome/chrome --type=renderer --field-trial-handle=15503761646981189628,17074726826077816693,131072 --lang=en-US --enable-auto-reload
gvamosi pts/1 192.168.0.113 18:21 10.00s 0.00s 0.00s -bash

Látható, hogy a pts/1-en a 192.168.0.113-as IP címről van egy belépett “távoli” felhasználónk. 🙂

És akkor nézzük meg azt a trükköt, hogy ne kérjen többet jelszót, és másoljunk át scp-vel egy file-t. Ezt ezután akár egy automatikus processz (tipikusan egy script) is elvégezheti, pl. mentésbackup – céljából. Ezekről a későbbiekben írok majd bővebben. 🙂

Mac-mini:~ gvamosi$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/gvamosi/.ssh/id_rsa):
/Users/gvamosi/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/gvamosi/.ssh/id_rsa.
Your public key has been saved in /Users/gvamosi/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:8DpEyLqQO78UnwTlsbdhjxklU7o459DqmUC84Pc6jVs gvamosi@Mac-mini.local
The key's randomart image is:
+---[RSA 2048]----+
| o o.o |
| + + = |
| . = O |
| o o * @ |
|+ = = O S |
|.= * O . |
|o.=.*E+ |
| +.=o+ . |
| o+B. |
+----[SHA256]-----+
Mac-mini:~ gvamosi$ less .ssh/id_rsa.pub
Mac-mini:~ gvamosi$ ssh 192.168.0.109
gvamosi@192.168.0.109's password:
Linux gergo1 4.19.0-5-amd64 #1 SMP Debian 4.19.37-5 (2019-06-19) x86_64
The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Mon Sep 16 17:40:12 2019 from 192.168.0.113
gvamosi@gergo1:~$ vi .ssh/authorized_keys
gvamosi@gergo1:~$ logout
Connection to 192.168.0.109 closed.
Mac-mini:~ gvamosi$ ssh 192.168.0.109
Linux gergo1 4.19.0-5-amd64 #1 SMP Debian 4.19.37-5 (2019-06-19) x86_64
The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Mon Sep 16 17:42:13 2019 from 192.168.0.113
gvamosi@gergo1:~$ logout
Connection to 192.168.0.109 closed.
Mac-mini:~ gvamosi$ scp ownCloud/Photos/San\ Francisco.jpg 192.168.0.109:
San Francisco.jpg 100% 211KB 211.0KB/s 00:00
Mac-mini:~ gvamosi$

Mi is történik? Először kulcsot kenerálunk OS X alatt az ssh-keygen paranccsal. Aztán a publikus rsa kulcsot (RSA: Rivest-Shamir-Adleman – titkosítási rendszer nyilvános kulcsú titkosításhoz, bemutatása később) átmásoljuk a Linuxszerverre” az authorized_keys file-ba.

Utána csodák csodája már nem kér jelszót. 🙂

Ezután scp-vel átmásoljuk a “San Francisco.jpg” file-t.