doc:appunti:linux:sa:iptables
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| doc:appunti:linux:sa:iptables [2009/05/07 16:59] – niccolo | doc:appunti:linux:sa:iptables [2020/11/23 15:28] (current) – [Shorewall and DNAT onto a local host] niccolo | ||
|---|---|---|---|
| Line 5: | Line 5: | ||
| Il numero massimo di connessioni che il kernel riesce a gestire è stabilito da '' | Il numero massimo di connessioni che il kernel riesce a gestire è stabilito da '' | ||
| '' | '' | ||
| - | |||
| - | Per stroncare una connessione in essere? | ||
| Vedere questo articolo sul **{{.iptables: | Vedere questo articolo sul **{{.iptables: | ||
| + | Con il tool **'' | ||
| + | |||
| + | < | ||
| + | conntrack -L | ||
| + | conntrack -F | ||
| + | </ | ||
| ===== Accessing a DNAT sever from the local LAN using the public IP address ===== | ===== Accessing a DNAT sever from the local LAN using the public IP address ===== | ||
| Line 34: | Line 38: | ||
| ===== Shorewall and DNAT onto a local host ===== | ===== Shorewall and DNAT onto a local host ===== | ||
| - | A web server is reachable from the internet onto a local host (**192.168.1.5**) via a DNAT rule, local hosts want to use the public address (**130.151.100.69**) to reach the d-natted server. Traffic will be masqueraded by the firewall with its address (**192.168.1.254**) on the local LAN (**eth0**): | + | A web server is reachable from the internet onto a local host (**192.168.1.5**) via a DNAT rule, local hosts want to use the public address (**130.151.100.69**) to reach the d-natted server. Traffic will be masqueraded by the firewall with its address (**192.168.1.254**) on the local LAN (**eth0**, **192.168.1.0/ |
| - | In '' | + | In **/ |
| < | < | ||
| - | #ZONE INTERFACE | + | #ZONE |
| - | loc eth0 detect | + | loc |
| </ | </ | ||
| + | For Shorewall 5 we nedd a line in **/ | ||
| - | In '' | + | < |
| + | # | ||
| + | SNAT(192.168.1.254) | ||
| + | </ | ||
| + | |||
| + | Shorewall 4 instead requires a line in **/ | ||
| < | < | ||
| - | # | + | # |
| - | eth0: | + | eth0: |
| </ | </ | ||
| - | In '' | + | In **/ |
| < | < | ||
| Line 57: | Line 67: | ||
| # | # | ||
| DNAT | DNAT | ||
| + | DNAT | ||
| </ | </ | ||
| + | Mapping different port from outside to inside is handled only in **/ | ||
| + | ===== Shorewall with router in local LAN ===== | ||
| + | Hosts in LAN#1 may access hosts in LAN#2 by just adding a static route to the **192.168.2.0/ | ||
| + | |||
| + | {{shorewall-router-in-lan.png? | ||
| + | |||
| + | You can instead make **two configurations** on the Shorewall firewall. First of all you add the static route into **/ | ||
| + | |||
| + | < | ||
| + | auto eth1 | ||
| + | iface eth1 inet static | ||
| + | address 192.168.1.1 | ||
| + | netmask 255.255.255.0 | ||
| + | up / | ||
| + | down /sbin/route del -net 192.168.2.0/ | ||
| + | </ | ||
| + | |||
| + | then you have to add the **routeback** option for the **eth1** interfaces in the **/ | ||
| + | |||
| + | < | ||
| + | loc eth1 routeback | ||
| + | </ | ||
| ===== Iptables schema ===== | ===== Iptables schema ===== | ||
| Line 83: | Line 116: | ||
| **WARNING** the interface name is required, otherwise the iptable rules are syntactically correct, but ineffective. May be this is a bug of Shorewall 4.0.8. | **WARNING** the interface name is required, otherwise the iptable rules are syntactically correct, but ineffective. May be this is a bug of Shorewall 4.0.8. | ||
| + | |||
| + | ===== Usare iptables per mitigare o bloccare un DNS Amplification Attack ===== | ||
| + | |||
| + | Vedere la definizione del [[wpit> | ||
| + | |||
| + | Con un tool tipo **iptraf** si verifica il traffico DNS cioè **UDP porta 53**, utilizzando la funzione // | ||
| + | |||
| + | < | ||
| + | tcpdump -i eth0 -n 'port 53 and dst host 148.67.69.17' | ||
| + | 16: | ||
| + | 16: | ||
| + | 16: | ||
| + | 16: | ||
| + | 16: | ||
| + | 16: | ||
| + | 16: | ||
| + | </ | ||
| + | |||
| + | l'IP sorgente può essere più di uno, si tratta ovviamente di un IP spooffato e si tratta della reale vittima dell' | ||
| + | |||
| + | In questo caso l' | ||
| + | |||
| + | < | ||
| + | host -t ANY zing.zong.co.ua | wc -c | ||
| + | 10254 | ||
| + | </ | ||
| + | |||
| + | Se la query è identificabile in modo univoco si può rapidamente aggiungere una regola iptables e bloccare tutte le richieste malevole, le relative risposte (che sono il reale peso) non ci saranno. Anzitutto identifichiamo una stringa del payload: | ||
| + | |||
| + | < | ||
| + | tcpdump -X -i eth0 -n 'port 53 and dst host 148.67.69.17 and src host 217.79.182.87' | ||
| + | 16: | ||
| + | 0x0000: | ||
| + | 0x0010: | ||
| + | 0x0020: | ||
| + | 0x0030: | ||
| + | 0x0040: | ||
| + | 16: | ||
| + | 0x0000: | ||
| + | 0x0010: | ||
| + | 0x0020: | ||
| + | 0x0030: | ||
| + | 0x0040: | ||
| + | </ | ||
| + | |||
| + | **Attenzione** che il punto non è il carattere **0x2e** come ci si aspetta: | ||
| + | |||
| + | ^ Hex ^ ASCII ^ | ||
| + | | 047a 696e 6704 7a6f 6e67 0263 6f02 7561 | zing.zong.co.ua | ||
| + | |||
| + | Quindi aggiungiamo la regola **iptables**: | ||
| + | |||
| + | < | ||
| + | / | ||
| + | --algo bm --from 40 --to 56 -j DROP -m comment --comment "DROP DNS Q zing.zong.co.ua" | ||
| + | </ | ||
| + | |||
| + | Discorso diverso se si vuole **limitare il rate delle richieste DNS**, in questo modo si prevengono futuri attacchi, indipendentemente dal payload del pacchetto. Ecco uno script che imposta un limite di 4 richieste al secondo per ogni IP sorgente. Un singolo IP viene considerato whitelisted e non sottoposto al rate: | ||
| + | |||
| + | <code bash> | ||
| + | #!/bin/sh | ||
| + | |||
| + | limit=" | ||
| + | burst=" | ||
| + | expire=" | ||
| + | |||
| + | options=" | ||
| + | --hashlimit-above $limit --hashlimit-burst $burst --hashlimit-htable-expire $expire \ | ||
| + | --hashlimit-mode srcip --hashlimit-srcmask 32" | ||
| + | |||
| + | case " | ||
| + | start) | ||
| + | iptables -I INPUT $options -j DROP | ||
| + | #iptables -I INPUT $options -j LOG --log-level debug | ||
| + | ;; | ||
| + | stop) | ||
| + | iptables -D INPUT $options -j DROP | ||
| + | #iptables -D INPUT $options -j LOG --log-level debug | ||
| + | ;; | ||
| + | *) | ||
| + | echo " | ||
| + | ;; | ||
| + | esac | ||
| + | </ | ||
| + | |||
| + | Nella tabella **''/ | ||
| + | |||
| + | - Conto alla rovescia per rimuovere la entry dalla tabella | ||
| + | - Inirizzo_IP: | ||
| + | - Inirizzo_IP: | ||
| + | - Credito attuale | ||
| + | - Credito massimo: es. (burst 20) * (costo 6400) = 128000 | ||
| + | - Costo: es. 6400 per 5/s, 8000 per 4/s, cioè 32000 / (n/s) | ||
| + | |||
doc/appunti/linux/sa/iptables.1241708370.txt.gz · Last modified: by niccolo
