Instalacja pakietów
Zadanie 1
Odszukaj plik konfiguracyjny iptables
Modyfikacja firewall’a z linii komend za pomocą polecenia iptables
Instalujemy niezbędne narzędzia za pomocą menedżera pakietów =apt= (dystybucje oparte na /Debianie/ z niego korzystają).
#+begin_src shell :results silent :dir /sudo::
sudo apt install iptables lynx
#+end_src
#+begin_quote
/Przygotowanie środowiska i wstępna konfiguracja firewall’a/
#+end_quote
Więc szukam plik konfiguracyjnego. Używam komendy:
#+begin_src shell :results output code :wrap src text :dir /sudo::
sudo find / -mount -type f -name "iptables"
#+end_src
\\
#+RESULTS:
#+begin_src text
/usr/share/bash-completion/completions/iptables
/var/lib/dpkg/alternatives/iptables
#+end_src
Sprawdziłem także co pliki w sobie zawierają... i doszedłem do wniosku, że nie są to pliki których szukam. Postanowiłem także poszukać odpowiedzi w internecie, gdzie mogę znaleść takie pliki na współczesnych dystyucjach, lecz ścieżki tam podawane, nie istniały na mojej dystrybucji =Debian 11=.
*Uwaga:* Komenda =iptables= zwykle wymaga uprawnień =sudo=, więc równie dobrze możemy zalogować się jako =root= dzięku poleceniu =su= lub =sudo su=, by uniknąć wpisywania =sudo= za każdym razem wraz z podawaniem hasła.\\
Użyte flagi polecenia =iptables=:
| Flaga | Opis |
|-------+----------------------------------------------------------------------------------------------|
| -F | Czyści wszystkie reguły w domyślnej tabeli =filter= |
| -X | Jeśli nie zostanie podany żaden argument, usuwa każdy niewbudowany łańcuch |
| -t | Pozwala sprecyzować tabelę na której będą przeprowadzane operacje |
| -L | Wypisuje wszystkie reguły w wybranym łańcuchu. |
| -P | Ustawia zasady dla wbudowanego łańcucha. Celem zasad jest /ACCEPT/ lub /DROP/ |
| -A | Dołącz jedną lub więcej reguł na końcu wybranego łańcucha. |
| -p | Protokół reguły lub pakietu do sprawdzenia, podawany poprzez nazwę lub numer. |
| -j | Określa cel reguły; czyli co zrobić, jeśli pakiet do niego pasuje. |
| -m | Określa dopasowanie do użycia, czyli moduł rozszerzenia, który testuje określoną właściwość. |
| -d | Specyfikacja miejsca docelowego. |
- Wypisuję reguły lańcucha =filter=:
#+begin_src shell :results output code :wrap src text :dir /sudo::
iptables -L -t filter
#+end_src
\\
#+RESULTS:
#+begin_src text
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
#+end_src
- Czyszczę istniejące tablice:
#+begin_src shell :results output code :wrap src text :dir /sudo::
iptables -F
#+end_src
- Kasuję opróżnioną tablicę:
#+begin_src shell :results output code :wrap src text :dir /sudo::
iptables -X -t filter
#+end_src
- Sprawdzam ponownie aktualny stan łańcucha/tablicy =filter=:
#+begin_src shell :results output code :wrap src text :dir /sudo::
iptables -L -t filter
#+end_src
Jak widać nić się nie zmieniło...
#+RESULTS:
#+begin_src text
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
#+end_src
- Tworzymy więc firewall nie przepuszczający żadnych pakietów wpisując następujące polecenia:
#+begin_src shell :results silent :wrap src text :dir /sudo::
iptables -P INPUT DROP
iptables -P OUTPUT DROP
iptables -P FORWARD DROP
#+end_src
- Sprawdzam ponownie konfigurację firewall'a
#+begin_src shell :results output code :wrap src text :dir /sudo::
iptables -L
#+end_src
\\
#+RESULTS:
#+begin_src text
Chain INPUT (policy DROP)
target prot opt source destination
Chain FORWARD (policy DROP)
target prot opt source destination
Chain OUTPUT (policy DROP)
target prot opt source destination
#+end_src
- Aby zapisać zmiany wpisuję następujące polecenie:
#+begin_src shell :results output code :wrap src text :dir /sudo::
iptables-save -f /etc/network/iptables.up.rules
#+end_src
Podając flagę =-f= możemy skierwać wyjście do pliku. W przypadku nie podania żadnych parametrów, wyjście zostanie wypisane na =STDOUT=, zależnie od środowiska okno terminala lub =tty=.
#+begin_src shell :results output code :wrap src text :dir /sudo::
cat /etc/network/iptables.up.rules
#+end_src
\\
#+RESULTS:
#+begin_src text
# Generated by iptables-save v1.8.7 on Sat Nov 27 20:30:18 2021
,*filter
:INPUT DROP [19:779]
:FORWARD DROP [0:0]
:OUTPUT DROP [30:3727]
COMMIT
# Completed on Sat Nov 27 20:30:18 2021
#+end_src
Uruchamiam usługę. Domyślną ścieżką jaką czyta komenda =iptables-apply= jest właśnie =/etc/network/iptables.up.rules=
#+begin_src shell :results output code :wrap src text :dir /sudo::
iptables-apply
#+end_src
- Teraz możemy przetestować czy nasza zapora działa jak powinna:
#+begin_src shell :results output code :wrap src text
ping 127.0.0.1 -W 5 -c 5
#+end_src
Flaga =-W= oznacza ile sekund komenda =ping= ma czekać na odpowiedź zwrotną. =-c= jak już wiemy, ustawia limit przesłanych pakietów.
#+RESULTS:
#+begin_src text
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
--- 127.0.0.1 ping statistics ---
5 packets transmitted, 0 received, 100% packet loss, time 4080ms
#+end_src
Jeszcze jeden przykład:
#+begin_src shell :results output code :wrap src text
ping 8.8.8.8 -W 5 -c 5
#+end_src
\\
#+RESULTS:
#+begin_src text
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
Zadanie 2
Dopuszczenie przez zaporę jedynie wybranych przez nas protokołów i usług.
--- 8.8.8.8 ping statistics ---
5 packets transmitted, 0 received, 100% packet loss, time 4084ms
#+end_src
Jak widać zapora firewall spełnia swoje zadanie i nie pozwala na odpowiedzi żądań echa.
#+begin_quote
/Udostępnianie wybranych usług/
#+end_quote
składnia. Flaga --dst jest aliasem tej opcj
- Akceptuję ruch pakietów protokołu IMCP:
#+begin_src shell :results silent :dir /sudo::
iptables -A INPUT -p icmp --icmp-type echo-reply -j ACCEPT
iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT
iptables -A OUTPUT -p icmp --icmp-type echo-reply -j ACCEPT
iptables -A OUTPUT -p icmp --icmp-type echo-request -j ACCEPT
#+end_src
- Testuję przy pomocy
ping
, czy reguły zostały wprowadzone pomyślnie:
#+begin_src shell :results output code :wrap src text
ping 127.0.0.1 -c 2 -W 5 && echo -e "\n\n"
ping 8.8.8.8 -c 2 -W 5 && echo -e "\n\n"
ping google.com -c 2 -W 5
#+end_src
\\
#+RESULTS:
#+begin_src text
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.013 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.049 ms
--- 127.0.0.1 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1028ms
rtt min/avg/max/mdev = 0.013/0.031/0.049/0.018 ms
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=113 time=39.6 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=113 time=36.2 ms
--- 8.8.8.8 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1002ms
rtt min/avg/max/mdev = 36.177/37.901/39.626/1.724 ms
PING google.com(waw02s22-in-x0e.1e100.net (2a00:1450:401b:810::200e)) 56 data bytes
64 bytes from waw02s22-in-x0e.1e100.net (2a00:1450:401b:810::200e): icmp_seq=1 ttl=114 time=24.0 ms
64 bytes from waw02s22-in-x0e.1e100.net (2a00:1450:401b:810::200e): icmp_seq=2 ttl=114 time=41.0 ms
Badanie aktualnej konfiguracji firewall'a
--- google.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 24.019/32.502/40.986/8.483 ms
#+end_src
Wszystko jest w porządku.
- Akceptuję ruch pakietów protokołu HTTP:
#+begin_src shell :results silent :dir /sudo::
iptables -A INPUT -p tcp --sport 80 -j ACCEPT
iptables -A OUTPUT -p tcp --dport 80 -j ACCEPT
#+end_src
- Akceptujemy ruch pakietów DNS:
#+begin_src shell :results silent :dir /sudo::
iptables -A INPUT -p udp --sport 53 -j ACCEPT
iptables -A OUTPUT -p udp --dport 53 -j ACCEPT
#+end_src
- Akceptuję ruch pakietów protokołu SMTP (wychodzące) i POP3 (przychodzące):
#+begin_src shell :results silent :dir /sudo::
iptables -A INPUT -p tcp --sport 25 -j ACCEPT
iptables -A OUTPUT -p tcp --dport 25 -j ACCEPT
iptables -A INPUT -p tcp --sport 110 -j ACCEPT
iptables -A OUTPUT -p tcp --dport 110 -j ACCEPT
#+end_src
- Akceptujemy przychodzące i wychodzące pakiety SSH:
#+begin_src shell :results silent :dir /sudo::
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -p tcp --sport 22 -j ACCEPT
iptables -A OUTPUT -p tcp --sport 22 -j ACCEPT
iptables -A OUTPUT -p tcp --dport 22 -j ACCEPT
#+end_src
1) Używam standardowego polecenia:
#+begin_src shell :results output code :wrap src text :dir /sudo::
iptables -L
#+end_src
\\
#+RESULTS:
#+begin_src text
Chain INPUT (policy DROP)
target prot opt source destination
ACCEPT icmp -- anywhere anywhere
ACCEPT icmp -- anywhere anywhere
ACCEPT icmp -- anywhere anywhere
ACCEPT icmp -- anywhere anywhere
ACCEPT icmp -- anywhere anywhere
ACCEPT icmp -- anywhere anywhere icmp echo-reply
ACCEPT icmp -- anywhere anywhere icmp echo-request
ACCEPT icmp -- anywhere anywhere icmp echo-reply
ACCEPT icmp -- anywhere anywhere icmp echo-request
ACCEPT tcp -- anywhere anywhere tcp spt:http
ACCEPT udp -- anywhere anywhere udp spt:domain
ACCEPT tcp -- anywhere anywhere tcp spt:smtp
ACCEPT tcp -- anywhere anywhere tcp spt:pop3
ACCEPT tcp -- anywhere anywhere tcp dpt:ssh
ACCEPT tcp -- anywhere anywhere tcp spt:ssh
Chain FORWARD (policy DROP)
target prot opt source destination
Chain OUTPUT (policy DROP)
target prot opt source destination
ACCEPT icmp -- anywhere anywhere
ACCEPT icmp -- anywhere anywhere
ACCEPT icmp -- anywhere anywhere
ACCEPT icmp -- anywhere anywhere
ACCEPT icmp -- anywhere anywhere icmp echo-reply
ACCEPT icmp -- anywhere anywhere icmp echo-request
ACCEPT icmp -- anywhere anywhere icmp echo-reply
ACCEPT icmp -- anywhere anywhere icmp echo-request
ACCEPT tcp -- anywhere anywhere tcp dpt:http
ACCEPT udp -- anywhere anywhere udp dpt:domain
ACCEPT tcp -- anywhere anywhere tcp dpt:smtp
ACCEPT tcp -- anywhere anywhere tcp dpt:pop3
ACCEPT tcp -- anywhere anywhere tcp spt:ssh
ACCEPT tcp -- anywhere anywhere tcp dpt:ssh
#+end_src
2) Zachowuję aktualną konfigurację firewalla:
#+begin_src shell :results output code :wrap src text :dir /sudo::
iptables-save -f /etc/network/iptables.up.rules
cat /etc/network/iptables.up.rules
#+end_src
\\
#+RESULTS:
#+begin_src text
# Generated by iptables-save v1.8.7 on Sat Nov 27 21:36:52 2021
,*filter
:INPUT DROP [29:1189]
:FORWARD DROP [0:0]
:OUTPUT DROP [1317:83109]
-A INPUT -p icmp -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -p icmp -m icmp --icmp-type 0 -j ACCEPT
-A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
-A INPUT -p icmp -m icmp --icmp-type 0 -j ACCEPT
-A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
-A INPUT -p tcp -m tcp --sport 80 -j ACCEPT
-A INPUT -p udp -m udp --sport 53 -j ACCEPT
-A INPUT -p tcp -m tcp --sport 25 -j ACCEPT
-A INPUT -p tcp -m tcp --sport 110 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 22 -j ACCEPT
-A INPUT -p tcp -m tcp --sport 22 -j ACCEPT
-A OUTPUT -p icmp -j ACCEPT
-A OUTPUT -p icmp -j ACCEPT
-A OUTPUT -p icmp -j ACCEPT
-A OUTPUT -p icmp -j ACCEPT
-A OUTPUT -p icmp -m icmp --icmp-type 0 -j ACCEPT
-A OUTPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
-A OUTPUT -p icmp -m icmp --icmp-type 0 -j ACCEPT
-A OUTPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
-A OUTPUT -p tcp -m tcp --dport 80 -j ACCEPT
-A OUTPUT -p udp -m udp --dport 53 -j ACCEPT
-A OUTPUT -p tcp -m tcp --dport 25 -j ACCEPT
-A OUTPUT -p tcp -m tcp --dport 110 -j ACCEPT
-A OUTPUT -p tcp -m tcp --sport 22 -j ACCEPT
-A OUTPUT -p tcp -m tcp --dport 22 -j ACCEPT
COMMIT
# Completed on Sat Nov 27 21:36:52 2021
#+end_src
3) Uruchamiam serwis iptables
, by reguły zaszły w życie:
#+begin_src shell :results output code :wrap src text
iptables-apply
#+end_src
4) Ponownie dostępność internetu za pomocą polecenia =ping=:
#+begin_src shell :results output code :wrap src text
ping 127.0.0.1 -c 2 -W 5 && echo -e "\n\n"
ping 8.8.8.8 -c 2 -W 5 && echo -e "\n\n"
ping google.com -c 2 -W 5
#+end_src
#+RESULTS:
#+begin_src text
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.046 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.049 ms
--- 127.0.0.1 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1014ms
rtt min/avg/max/mdev = 0.046/0.047/0.049/0.001 ms
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=118 time=56.8 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=118 time=25.3 ms
--- 8.8.8.8 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1002ms
rtt min/avg/max/mdev = 25.256/41.003/56.750/15.747 ms
PING google.com (142.250.75.14) 56(84) bytes of data.
64 bytes from waw07s03-in-f14.1e100.net (142.250.75.14): icmp_seq=1 ttl=118 time=24.3 ms
64 bytes from waw07s03-in-f14.1e100.net (142.250.75.14): icmp_seq=2 ttl=118 time=85.8 ms
Zadanie 3
--- google.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1002ms
rtt min/avg/max/mdev = 24.256/55.044/85.832/30.788 ms
#+end_src
5) Sprawdzam z innego komputera czy obecny komputer odpowie na polecenie =ping=:
#+CAPTION: Lewo: Komputer na którym wykonywałem zadania, Prawo: Laptop
[[./images/horizontally.png]]
\FloatBarrier
6) Sprawdzam czy strony HTTP działają poprawnie poprzez przeglądarkę =lynx=:
#+CAPTION: Przeglądarka =lynx= na stronie http://wmii.uwm.edu.pl
[[./images/lynx.png]]
\FloatBarrier
#+begin_quote
/Przekazywanie pakietów/
#+end_quote
Przekazywania (forward) pakietów oznacza, że komputer pełni rolę bramy przekazującej
filtrowany ruch sieciowy skierowany do komputerów znajdujących się wewnątrz sieci
lokalnej.
- Sprawdzamy czy przekazywanie pakietów IP jest włączone odczytując plik:
#+begin_src shell :results output code :wrap src text :dir /sudo::
cat /proc/sys/net/ipv4/ip_forward
#+end_src
\\
#+RESULTS:
#+begin_src text
0
#+end_src
Wartość 0 oznacza, że przekazywanie jest wyłączone, więc należy je włączyć
- Umożliwiam routing poleceniem nadpisując zero, jedynką:
#+begin_src shell :results output code :wrap src text :dir /sudo::
# cat /proc/sys/net/ipv4/ip_forward
echo 1 > /proc/sys/net/ipv4/ip_forward
#+end_src
\\
#+RESULTS:
#+begin_src text
#+end_src
- Sprawdzam ponownie:
#+begin_src shell :results output code :wrap src text :dir /sudo::
cat /proc/sys/net/ipv4/ip_forward
#+end_src
\\
#+RESULTS:
#+begin_src text
1
#+end_src
Przekazywanie jest włączone.
- Tworzymy następujące reguły =iptables=:
- Brama ma adres IP 192.168.1.1 na interfejsie wlan0. Pozostałe komputery również mają adresy IP z zakresu 192.168.1.x/24.
#+begin_src shell :results silent :dir /sudo::
iptables -t nat -A POSTROUTING -s 192.168.1.0/24 ! -d 192.168.1.0/24 -j MASQUERADE
#+end_src
- Przekierowujemy ruch z połączeń wykonanych z sieci wewnętrznej na pierwotny adres IP.
#+begin_src shell :results silent :dir /sudo::
iptables -A FORWARD -d 192.168.1.0/24 -o wlan0 -j ACCEPT
#+end_src
- Akceptujemy ruch przychodzący z sieci wewnętrznej i kierowany do sieci wewnętrznej.
#+begin_src shell :results silent :dir /sudo::
iptables -A FORWARD -s 192.168.1.0/24 -i wlan0 -j ACCEPT
#+end_src
- Zapisuję i uruchamiam:
#+begin_src shell :results silent :dir /sudo::
iptables-save -f /etc/network/iptables.up.rules
#+end_src
\\
#+begin_src shell :results output code :wrap src text :dir /sudo::
cat /etc/network/iptables.up.rules
#+end_src
\\
#+RESULTS:
#+begin_src text
# Generated by iptables-save v1.8.7 on Sun Nov 28 10:28:35 2021
,*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A FORWARD -d 192.168.1.0/24 -o wlan0 -j ACCEPT
-A FORWARD -s 192.168.1.0/24 -i wlan0 -j ACCEPT
COMMIT
# Completed on Sun Nov 28 10:28:35 2021
# Generated by iptables-save v1.8.7 on Sun Nov 28 10:28:35 2021
,*nat
:PREROUTING ACCEPT [0:0]
:INPUT ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
:POSTROUTING ACCEPT [0:0]
-A POSTROUTING -s 192.168.1.0/24 ! -d 192.168.1.0/24 -j MASQUERADE
COMMIT
# Completed on Sun Nov 28 10:28:35 2021
#+end_src
\\
#+begin_src shell :results silent :dir /sudo::
iptables-apply
#+end_src
- Sprawdzam status usługi iptables:
#+begin_src shell :results output code :wrap src text :dir /sudo::
iptables -L
#+end_src
\\
#+RESULTS:
#+begin_src text
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
ACCEPT all -- anywhere 192.168.1.0/24
ACCEPT all -- 192.168.1.0/24 anywhere
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
#+end_src
- Sprawdzam czy komutery z sieci zewnętrznej mają dostęp do internetu:
#+begin_src shell :results output code :wrap src text
ping google.com -c 8
#+end_src
\\
#+RESULTS:
#+begin_src text
PING google.com (142.250.75.14) 56(84) bytes of data.
64 bytes from waw07s03-in-f14.1e100.net (142.250.75.14): icmp_seq=1 ttl=118 time=25.1 ms
64 bytes from waw07s03-in-f14.1e100.net (142.250.75.14): icmp_seq=2 ttl=118 time=45.8 ms
64 bytes from waw07s03-in-f14.1e100.net (142.250.75.14): icmp_seq=3 ttl=118 time=46.2 ms
64 bytes from waw07s03-in-f14.1e100.net (142.250.75.14): icmp_seq=4 ttl=118 time=32.5 ms
64 bytes from waw07s03-in-f14.1e100.net (142.250.75.14): icmp_seq=5 ttl=118 time=188 ms
64 bytes from waw07s03-in-f14.1e100.net (142.250.75.14): icmp_seq=6 ttl=118 time=87.6 ms
64 bytes from waw07s03-in-f14.1e100.net (142.250.75.14): icmp_seq=7 ttl=118 time=30.2 ms
64 bytes from waw07s03-in-f14.1e100.net (142.250.75.14): icmp_seq=8 ttl=118 time=48.0 ms
--- google.com ping statistics ---
8 packets transmitted, 8 received, 0% packet loss, time 7004ms
rtt min/avg/max/mdev = 25.101/62.944/188.132/50.628 ms
#+end_src
Komputery mają dostęp do internetu.