This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
сервис_firewall [2020/09/03 08:43] val [Debian/Ubuntu (iptables)] |
сервис_firewall [2025/02/06 12:40] (current) val [nftables] |
||
---|---|---|---|
Line 5: | Line 5: | ||
===== Конфигурация для рабочей станции ===== | ===== Конфигурация для рабочей станции ===== | ||
+ | |||
+ | ==== nftables ==== | ||
+ | |||
+ | * [[https://habr.com/ru/companies/ruvds/articles/580648/|Переход с iptables на nftables. Краткий справочник]] | ||
+ | * [[https://cryptoworld.su/kak-perejti-s-iptables-na-nftables-polnaya-istrukciya/|Как перейти с iptables на Nftables — полная инструкция]] | ||
+ | * [[https://serveradmin.ru/bazovye-nastrojki-nftables-dlya-veb-servera-na-debian/|Набор правил nftables для веб сервера]] | ||
+ | |||
+ | <code> | ||
+ | root@openvpn2:~# systemctl enable nftables.service --now | ||
+ | |||
+ | root@openvpn2:~# cat /etc/nftables.conf | ||
+ | </code><code> | ||
+ | #!/usr/sbin/nft -f | ||
+ | |||
+ | flush ruleset | ||
+ | |||
+ | table inet filter { | ||
+ | chain input { | ||
+ | type filter hook input priority filter; | ||
+ | } | ||
+ | chain forward { | ||
+ | type filter hook forward priority filter; policy drop; | ||
+ | ct state established,related counter accept | ||
+ | # BMSTU DNS noc@bmstu.ru | ||
+ | ip daddr 195.19.32.2 udp dport 53 counter accept | ||
+ | # DMZ NETS noc@bmstu.ru | ||
+ | ip daddr { 195.19.40.0/24, 195.19.50.0/24 } counter accept | ||
+ | # EU eu@bmstu.ru | ||
+ | ip daddr 195.19.33.59 tcp dport { 80, 443 } counter accept | ||
+ | # CAS proxy noc@bmstu.ru | ||
+ | ip daddr 195.19.33.9 tcp dport 8443 counter accept | ||
+ | # lm.bmstu.ru | ||
+ | ip daddr 195.19.33.222 tcp dport 22 counter drop | ||
+ | ip daddr 195.19.33.222 counter accept | ||
+ | |||
+ | } | ||
+ | chain output { | ||
+ | type filter hook output priority filter; | ||
+ | } | ||
+ | } | ||
+ | </code><code> | ||
+ | root@openvpn2:~# systemctl reload nftables.service | ||
+ | |||
+ | root@openvpn2:~# nft list ruleset | ||
+ | </code> | ||
==== Linux (iptables) ==== | ==== Linux (iptables) ==== | ||
- | * [[https://help.ubuntu.com/community/IptablesHowTo]] | + | |
- | * [[http://ru.wikibooks.org/wiki/Iptables]] | + | * [[https://help.ubuntu.com/community/IptablesHowTo|ubuntu.com community IptablesHowTo]] |
- | * [[https://ru.wikipedia.org/wiki/Netfilter]] | + | * [[https://ru.wikibooks.org/wiki/Iptables|Материал из Викиучебника iptables — утилита командной строки]] |
+ | * [[https://ru.wikipedia.org/wiki/Netfilter|Материал из Википедии netfilter — межсетевой экран]] | ||
=== Настройка фильтра === | === Настройка фильтра === | ||
<code> | <code> | ||
- | root@gate:~# cat firewall.sh | + | root@clientN:~# cat firewall.sh |
</code><code> | </code><code> | ||
iptables --flush | iptables --flush | ||
Line 21: | Line 67: | ||
iptables -A INPUT -j DROP | iptables -A INPUT -j DROP | ||
</code><code> | </code><code> | ||
- | root@gate:~# sh firewall.sh | + | root@clientN:~# sh firewall.sh |
</code> | </code> | ||
=== Просмотр правил фильтра === | === Просмотр правил фильтра === | ||
<code> | <code> | ||
- | root@gate:~# iptables -t filter -n -L -v --line-numbers | + | # iptables -t filter -n -L -v --line-numbers |
или | или | ||
- | root@gate:~# iptables -n -L -v --line-numbers | + | # iptables -n -L -v --line-numbers |
+ | </code> | ||
+ | === Удаление правил фильтра === | ||
+ | <code> | ||
+ | iptables -t ТАБЛИЦА -D ЦЕПОЧКА НОМЕР_ПРАВИЛА | ||
</code> | </code> | ||
- | |||
=== Работа с таблицей состояний === | === Работа с таблицей состояний === | ||
Line 41: | Line 90: | ||
</code> | </code> | ||
- | === Сохранение состояния iptables === | + | === Управление состоянием iptables === |
+ | |||
+ | == Вариант 1 == | ||
+ | |||
+ | == Сохранение состояния iptables == | ||
<code> | <code> | ||
root@gate:~# iptables-save > /etc/iptables.rules | root@gate:~# iptables-save > /etc/iptables.rules | ||
</code> | </code> | ||
- | === Восстановление состояния iptables === | + | == Восстановление состояния iptables == |
<code> | <code> | ||
root@gate:~# iptables-restore < /etc/iptables.rules | root@gate:~# iptables-restore < /etc/iptables.rules | ||
</code> | </code> | ||
- | === Восстановление состояния iptables при загрузке === | + | == Восстановление состояния iptables при загрузке == |
- | + | ||
- | == Debian/Ubuntu == | + | |
<code> | <code> | ||
root@gate:~# cat /etc/network/interfaces | root@gate:~# cat /etc/network/interfaces | ||
Line 62: | Line 113: | ||
pre-up iptables-restore < /etc/iptables.rules | pre-up iptables-restore < /etc/iptables.rules | ||
... | ... | ||
+ | </code> | ||
+ | |||
+ | == Вариант 2 == | ||
+ | <code> | ||
+ | # apt install iptables-persistent | ||
+ | |||
+ | # netfilter-persistent save | ||
</code> | </code> | ||
==== CentOS ==== | ==== CentOS ==== | ||
- | === CentOS 7 === | + | === CentOS 7, AlmaLinux 9 === |
* [[https://bozza.ru/art-259.html|Настройка firewalld CentOS 7 с примерами команд]] | * [[https://bozza.ru/art-259.html|Настройка firewalld CentOS 7 с примерами команд]] | ||
Line 166: | Line 224: | ||
#iptables -A FORWARD -i eth1 -p tcp -d 192.168.X.10 --dport 25 -j REJECT | #iptables -A FORWARD -i eth1 -p tcp -d 192.168.X.10 --dport 25 -j REJECT | ||
#iptables -A FORWARD -i eth1 -p tcp -d 192.168.X.10 --dport 25 -j ACCEPT | #iptables -A FORWARD -i eth1 -p tcp -d 192.168.X.10 --dport 25 -j ACCEPT | ||
- | iptables -A FORWARD -i eth1 -p tcp -d 192.168.X.10 --dport 465 -j ACCEPT | + | #iptables -A FORWARD -i eth1 -p tcp -d 192.168.X.10 --dport 465 -j ACCEPT |
- | iptables -A FORWARD -i eth1 -p tcp -d 192.168.X.10 --dport 587 -j ACCEPT | + | #iptables -A FORWARD -i eth1 -p tcp -d 192.168.X.10 --dport 587 -j ACCEPT |
+ | #iptables -A FORWARD -i eth1 -p tcp -d 192.168.X.10 --dport 143 -j ACCEPT | ||
iptables -A FORWARD -i eth1 -p tcp -d 192.168.X.10 --dport 80 -j ACCEPT | iptables -A FORWARD -i eth1 -p tcp -d 192.168.X.10 --dport 80 -j ACCEPT | ||
- | iptables -A FORWARD -i eth1 -p tcp -d 192.168.X.10 --dport 143 -j ACCEPT | + | iptables -A FORWARD -i eth1 -p tcp -d 192.168.X.10 --dport 5222 -j ACCEPT |
#iptables -A FORWARD -i eth1 -p udp -d 192.168.X.10 --dport 5060 -j ACCEPT | #iptables -A FORWARD -i eth1 -p udp -d 192.168.X.10 --dport 5060 -j ACCEPT | ||
#iptables -A FORWARD -i eth1 -p udp -d 192.168.X.10 --dport 4569 -j ACCEPT | #iptables -A FORWARD -i eth1 -p udp -d 192.168.X.10 --dport 4569 -j ACCEPT | ||
Line 187: | Line 247: | ||
conntrack -F | conntrack -F | ||
</code><code> | </code><code> | ||
+ | root@gate:~# apt install conntrack | ||
+ | |||
root@gate:~# sh firewall.sh | root@gate:~# sh firewall.sh | ||
Line 350: | Line 412: | ||
root@gate:~# iptables-save > /etc/iptables.rules | root@gate:~# iptables-save > /etc/iptables.rules | ||
+ | или | ||
+ | root@gate:~# netfilter-persistent save | ||
</code> | </code> | ||
Line 382: | Line 446: | ||
==== Debian/Ubuntu (iptables) ==== | ==== Debian/Ubuntu (iptables) ==== | ||
+ | |||
+ | === Ограничение частоты подключений === | ||
<code> | <code> | ||
root@gate:~# cat firewall.sh | root@gate:~# cat firewall.sh | ||
Line 390: | Line 456: | ||
iptables -A FORWARD -p tcp --dport 80 -i eth1 -m conntrack --ctstate NEW -m recent --update --seconds 60 --hitcount 4 -j DROP | iptables -A FORWARD -p tcp --dport 80 -i eth1 -m conntrack --ctstate NEW -m recent --update --seconds 60 --hitcount 4 -j DROP | ||
iptables -A FORWARD -p tcp --dport 80 -i eth1 -m conntrack --ctstate NEW -m recent --set | iptables -A FORWARD -p tcp --dport 80 -i eth1 -m conntrack --ctstate NEW -m recent --set | ||
+ | |||
+ | #iptables -A FORWARD -p tcp -i eth1 -m conntrack --ctstate NEW -m recent --update --seconds 1 --hitcount 10 -j LOG | ||
+ | #iptables -A FORWARD -p tcp -i eth1 -m conntrack --ctstate NEW -m recent --update --seconds 1 --hitcount 10 -j DROP | ||
+ | #iptables -A FORWARD -p tcp -i eth1 -m conntrack --ctstate NEW -m recent --set | ||
... | ... | ||
</code><code> | </code><code> | ||
root@gate:~# tail -f /var/log/syslog | root@gate:~# tail -f /var/log/syslog | ||
+ | root@gate:~# journalctl -f | ||
root@gate:~# cat /proc/net/xt_recent/DEFAULT | root@gate:~# cat /proc/net/xt_recent/DEFAULT | ||
+ | root@gate:~# watch cat /proc/net/xt_recent/DEFAULT | ||
root@gate:~# echo -10.5.7.1 >/proc/net/xt_recent/DEFAULT | root@gate:~# echo -10.5.7.1 >/proc/net/xt_recent/DEFAULT | ||
Line 401: | Line 473: | ||
</code> | </code> | ||
+ | ==== nftables ==== | ||
+ | |||
+ | === Блокировка абонентов, превысивших частоту подключений === | ||
+ | |||
+ | * [[https://access.redhat.com/documentation/ru-ru/red_hat_enterprise_linux/7/html/security_guide/sec-using_nftables_to_limit_the_amount_of_connections|Using nftables to limit the amount of connections]] | ||
+ | |||
+ | <code> | ||
+ | gate# cat /etc/nftables.conf | ||
+ | </code><code> | ||
+ | ... | ||
+ | table inet filter { | ||
+ | set denylist { | ||
+ | type ipv4_addr | ||
+ | size 65535 | ||
+ | flags dynamic,timeout | ||
+ | timeout 5m | ||
+ | } | ||
+ | ... | ||
+ | chain forward { | ||
+ | type filter hook forward priority filter; policy accept; | ||
+ | ip protocol tcp ct state new,untracked limit rate over 10/second add @denylist { ip saddr } | ||
+ | ip saddr @denylist drop | ||
+ | } | ||
+ | ... | ||
+ | </code> | ||
==== FreeBSD (pf) ==== | ==== FreeBSD (pf) ==== | ||
Line 420: | Line 517: | ||
# pfctl -t fail2ban -T add 172.16.1.254 | # pfctl -t fail2ban -T add 172.16.1.254 | ||
+ | # pfctl -k 172.16.1.254 | ||
# pfctl -t fail2ban -T flush | # pfctl -t fail2ban -T flush | ||
Line 437: | Line 535: | ||
<code> | <code> | ||
[gate:~] # pfctl -vs state | [gate:~] # pfctl -vs state | ||
+ | |||
+ | [gate:~] # pfctl -k 0.0.0.0/0 -k 172.16.1.254 | ||
[gate:~] # pfctl -F states | [gate:~] # pfctl -F states |