Защита от сканирования портов iptables
Навожу еще один простой способ обезопасить сервер от сканирования. В данном случаи будем использовать только iptables. В зависимости от того, какие у нас работают сервисы на серверах, набор портов будет разный. Для начала создадим свою цепочку, в которую добавим список IP адресов, которым позволено/запрещено иметь доступ к списку портов.
1
2
3
4
|
root@srv-19:~ # iptables -N antiscan root@srv-19:~ # iptables -A antiscan -s 195.34.25.11 -j ACCEPT root@srv-19:~ # iptables -A antiscan -s 195.34.25.12 -j ACCEPT root@srv-19:~ # iptables -A antiscan -j DROP |
В данном случае разрешено доступ только для двух IP-адресов. Теперь нужно добавить в цепочку INPUT список портов, к которым ограничивается доступ. Для начала добавим TCP порты.
1
2
3
4
|
root@srv-19:~ # iptables -I INPUT -m tcp -p tcp --dport 25 -j antiscan root@srv-19:~ # iptables -I INPUT -m tcp -p tcp --dport 82 -j antiscan root@srv-19:~ # iptables -I INPUT -m tcp -p tcp --dport 3306 -j antiscan root@srv-19:~ # iptables -I INPUT -m tcp -p tcp --dport 8083 -j antiscan |
Далее добавим UDP порты.
1
2
|
root@srv-19:~ # iptables -I INPUT -m udp -p udp --dport 111 -j antiscan root@srv-19:~ # iptables -I INPUT -m udp -p udp --dport 161 -j antiscan |
Теперь можно добавить правила в автозагрузку.
1
2
3
4
5
|
root@srv-19:~ # iptables-save > /etc/iptables.rules root@srv-19:~ # cat > /etc/network/if-pre-up.d/iptablesup << EOF #!/bin/bash iptables-restore < /etc/iptables .rules exit 0 EOF root@srv-19:~ # chmod +x /etc/network/if-pre-up.d/iptablesup |
Можно написать маленький скрипт, который добавлять все нужные цепочки и правила.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
root@srv-19:~ # cat antiscan.sh #!/bin/bash WHITEIP=( ` cat /opt/cdn/utils/iptables/whitelist .ip` ) PORTS_TCP=( 25 82 3306 8083 ) PORTS_UDP=( 111 161 ) if [ ! -f /opt/cdn/utils/iptables/whitelist .ip ] then echo "Please add white list /opt/cdn/utils/iptables/whitelist.ip" exit 1 fi iptables -N antiscan && echo "Antiscan chain was added" || echo "Failed to create Antiscan chain" echo "============== Adding white list ===============" for IPs in "${WHITEIP[@]}" do iptables -A antiscan -s ${IPs} -j ACCEPT && echo "${IPs} was added to Antiscan chain" || echo "Failed to add ${IPs} to Antiscan chain" done echo "============== Adding TCP ports ===============" for TCPPORT in "${PORTS_TCP[@]}" do iptables -I INPUT -m tcp -p tcp --dport ${TCPPORT} -j antiscan && echo "Adding Antiscan chain for ${TCPPORT} port to INPUT chain" || echo "Failed to add Antiscan chain for ${TCPPORT} port to INPUT chain" done echo "============== Adding UDP ports ===============" for UDPPORT in "${PORTS_UDP[@]}" do iptables -I INPUT -m udp -p udp --dport ${UDPPORT} -j antiscan && echo "Adding Antiscan chain for ${UDPPORT} port to INPUT chain" || echo "Failed to add Antiscan chain for ${UDPPORT} port to INPUT chain" done iptables -A antiscan -j DROP && echo "Adding DROP rule for Antiscan chain" || echo "Failed to add DROP rule to Antiscan chain" echo "================= ADD rules to autostart ===================" iptables-save | grep - v fail2ban > /etc/iptables .rules cat > /etc/network/if-pre-up .d /iptablesup << EOF #!/bin/bash iptables-restore < /etc/iptables .rules exit 0 EOF chmod +x /etc/network/if-pre-up .d /iptablesup echo "================= DONE ===================" exit 0 |
/opt/cdn/utils/iptables/whitelist.ip – это список IP адресов, которые имеют доступ к портам.
Так же можно написать маленький скрипт для добавления нового IP в белый список.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
root@srv-19:~ # cat antiscan_append.sh #!/bin/bash usage() { echo -e "nUsage: $0 IP-address" echo -e "Example:n $0 82.50.92.45" exit 1 } if [ $ # -lt 1 ] then echo "Please enter IP to add to white list" usage fi echo "============== Check if exist ========================" iptables -C antiscan -s $1 -j ACCEPT && echo "Already exist" && exit 1 echo "============== Adding IP to white list ===============" iptables -I antiscan 1 -s $1 -j ACCEPT && echo "$1 was added to Antiscan chain" || echo "Failed to add $1 to Antiscan chain" echo "================= Saving rules to autostart ===================" iptables-save | grep - v fail2ban > /etc/iptables .rules echo "================= DONE ===================" exit 0 |
Так же можно написать маленький скрипт для добавления нового IP в белый список.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
root@srv-19:~ # cat antiscan_clean.sh #!/bin/bash PORTS_TCP=( 25 82 3306 8083 ) PORTS_UDP=( 111 161 ) iptables -F antiscan && echo "Antiscan chain was flushed" || echo "Failed to flush antiscan chain" for TCPPORT in "${PORTS_TCP[@]}" do iptables -D INPUT -m tcp -p tcp --dport ${TCPPORT} -j antiscan && echo "Delete Antiscan chain for ${TCPPORT} port to INPUT chain" || echo "Failed to del Antiscan chain for ${TCPPORT} port to INPUT chain" done for UDPPORT in "${PORTS_UDP[@]}" do iptables -D INPUT -m udp -p udp --dport ${UDPPORT} -j antiscan && echo "Delete Antiscan chain for ${UDPPORT} port to INPUT chain" || echo "Failed to del Antiscan chain for ${UDPPORT} port to INPUT chain" done iptables -X antiscan && echo "Deleting antiscan chain" || echo "Failed to delete antiscan chain" echo "================= Deleting autostart rules ===================" rm -f /etc/iptables .rules && echo "Deleting /etc/iptables.rules" || echo "Failed to delete /etc/iptables.rules" rm -f /etc/network/if-pre-up .d /iptablesup && echo "Deleting /etc/network/if-pre-up.d/iptablesup" || echo "Failed to delete /etc/network/if-pre-up.d/iptablesup" exit 0 |
Также добавим скрипт для удаления и чистки всех правил
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
root@srv-19:~ # cat antiscan_clean.sh #!/bin/bash PORTS_TCP=( 25 82 3306 8083 ) PORTS_UDP=( 111 161 ) iptables -F antiscan && echo "Antiscan chain was flushed" || echo "Failed to flush antiscan chain" for TCPPORT in "${PORTS_TCP[@]}" do iptables -D INPUT -m tcp -p tcp --dport ${TCPPORT} -j antiscan && echo "Delete Antiscan chain for ${TCPPORT} port to INPUT chain" || echo "Failed to del Antiscan chain for ${TCPPORT} port to INPUT chain" done for UDPPORT in "${PORTS_UDP[@]}" do iptables -D INPUT -m udp -p udp --dport ${UDPPORT} -j antiscan && echo "Delete Antiscan chain for ${UDPPORT} port to INPUT chain" || echo "Failed to del Antiscan chain for ${UDPPORT} port to INPUT chain" done iptables -X antiscan && echo "Deleting antiscan chain" || echo "Failed to delete antiscan chain" echo "================= Deleting autostart rules ===================" rm -f /etc/iptables .rules && echo "Deleting /etc/iptables.rules" || echo "Failed to delete /etc/iptables.rules" rm -f /etc/network/if-pre-up .d /iptablesup && echo "Deleting /etc/network/if-pre-up.d/iptablesup" || echo "Failed to delete /etc/network/if-pre-up.d/iptablesup" exit 0 |
Ну и маленький скрипт для удаления определенного IP со списка позволенных.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
root@srv-19:~ # cat antiscan_del_ip.sh #!/bin/bash echo "============== Check if exist ========================" iptables -C antiscan -s $1 -j ACCEPT if [ $? - eq 0 ] then echo "Entry exist" echo "Deleting..." iptables -D antiscan -s $1 -j ACCEPT && echo "$1 was deleted from Antiscan chain" || echo "Failed to delete $1 from Antiscan chain" echo "================= Saving rules to autostart ===================" iptables-save | grep - v fail2ban > /etc/iptables .rules echo "================= DONE ===================" exit 0 else echo "Entry doesn't exist" exit 1 fi |
Пробуйте, может кому-то понадобится.