Защита от сканирования портов iptables
Навожу еще один простой способ обезопасить сервер от сканирования. В данном случаи будем использовать только iptables. В зависимости от того, какие у нас работают сервисы на серверах, набор портов будет разный. Для начала создадим свою цепочку, в которую добавим список IP адресов, которым позволено/запрещено иметь доступ к списку портов.
|
1
2
3
4
|
root@srv-19:~# iptables -N antiscanroot@srv-19:~# iptables -A antiscan -s 195.34.25.11 -j ACCEPTroot@srv-19:~# iptables -A antiscan -s 195.34.25.12 -j ACCEPTroot@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 antiscanroot@srv-19:~# iptables -I INPUT -m tcp -p tcp --dport 82 -j antiscanroot@srv-19:~# iptables -I INPUT -m tcp -p tcp --dport 3306 -j antiscanroot@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 antiscanroot@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.rulesroot@srv-19:~# cat > /etc/network/if-pre-up.d/iptablesup << EOF#!/bin/bashiptables-restore </etc/iptables.rulesexit 0 EOFroot@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/bashWHITEIP=( `cat /opt/cdn/utils/iptables/whitelist.ip` )PORTS_TCP=(258233068083 )PORTS_UDP=(111161 )if [ ! -f/opt/cdn/utils/iptables/whitelist.ip ]then echo "Please add white list /opt/cdn/utils/iptables/whitelist.ip" exit 1fiiptables -N antiscan &&echo "Antiscan chain was added" ||echo "Failed to create Antiscan chain"echo "============== Adding white list ==============="for IPsin "${WHITEIP[@]}"do iptables -A antiscan -s ${IPs} -j ACCEPT &&echo "${IPs} was added to Antiscan chain" ||echo "Failed to add ${IPs} to Antiscan chain"doneecho "============== Adding TCP ports ==============="for TCPPORTin "${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"doneecho "============== Adding UDP ports ==============="for UDPPORTin "${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"doneiptables -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.rulescat >/etc/network/if-pre-up.d/iptablesup << EOF#!/bin/bashiptables-restore </etc/iptables.rulesexit 0EOFchmod +x/etc/network/if-pre-up.d/iptablesupecho "================= 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/bashusage() { 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" usagefiecho "============== Check if exist ========================"iptables -C antiscan -s $1 -j ACCEPT &&echo "Already exist" &&exit 1echo "============== 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.rulesecho "================= 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/bashPORTS_TCP=(258233068083 )PORTS_UDP=(111161 )iptables -F antiscan &&echo "Antiscan chain was flushed" ||echo "Failed to flush antiscan chain"for TCPPORTin "${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"donefor UDPPORTin "${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"doneiptables -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/bashPORTS_TCP=(258233068083 )PORTS_UDP=(111161 )iptables -F antiscan &&echo "Antiscan chain was flushed" ||echo "Failed to flush antiscan chain"for TCPPORTin "${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"donefor UDPPORTin "${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"doneiptables -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/bashecho "============== Check if exist ========================"iptables -C antiscan -s $1 -j ACCEPTif [ $? -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 0else echo "Entry doesn't exist" exit 1fi |
Пробуйте, может кому-то понадобится.


