DDOS ellen

DDOS ellen, itteni magyarázat nélkül

#!/bin/bash
. /lib/lsb/init-functions
log_daemon_msg "Configuring System"
sysctl -p /etc/sysctl.close.conf
modprobe ipt_recent ip_list_tot=262143 ip_pkt_list_tot=255
if [ $1 = "restart" ] || [ $1 = "stop" ];
then
log_daemon_msg "Saving blacklist..."
/usr/local/bin/php /etc/recent/copy.php
fi
log_end_msg 0
log_daemon_msg "Initializing IPTables Firewall..."
iptables -F
iptables -X
iptables -A INPUT -j DROP
iptables -N ssh
iptables -N apache
iptables -N bind_dns
iptables -N blacklist
iptables -N other
iptables -N rapache
iptables -N rother

#Blacklist lanc
log_daemon_msg " Blacklist"
iptables -A blacklist -m recent --name blacklist --set
iptables -A blacklist -m recent --name ssh --remove
iptables -A blacklist -m recent --name apache --remove
iptables -A blacklist -m recent --name bind_dns --remove
iptables -A blacklist -m recent --name other --remove
iptables -A blacklist -j DROP

sleep 1

log_end_msg 0

#SSH lanc
log_daemon_msg " SSH"
iptables -A ssh -m recent --rcheck --name ssh --seconds 60 --hitcount 10 -j blacklist
#Eloszor jar erre
iptables -A ssh -m recent --set --name ssh
iptables -A ssh -j ACCEPT

sleep 1

log_end_msg 0

#Apache lanc
log_daemon_msg " Apache"
iptables -A apache -m recent --rcheck --name apache --seconds 5 --hitcount 10 -j blacklist
#iptables -A apache -m recent --rcheck --name apache ! --seconds 5 -j rapache
#iptables -A apache -m recent --rcheck --name apache --seconds 300 --hitcount 1 -j blacklist
#Eloszor jar erre
iptables -A apache -m recent --set --name apache
iptables -A apache -j ACCEPT

sleep 1

log_end_msg 0

#Bind lanc
log_daemon_msg " Bind"

sleep 3

#Percenkent 30at engedunk neki, majd mehet a blacklistbe
iptables -A bind_dns -m recent --rcheck --name bind_dns --seconds 60 --hitcount 30 -j blacklist
#Eloszor jar erre
iptables -A bind_dns -m recent --set --name bind_dns
iptables -A bind_dns -j ACCEPT

sleep 1

log_end_msg 0

#Other lanc
log_daemon_msg " Other"

sleep 3

iptables -A other -m recent --rcheck --name other --seconds 5 --hitcount 10 -j blacklist
#iptables -A other -m recent --rcheck --name other ! --seconds 5 -j rother
#Eloszor jar erre
iptables -A other -m recent --set --name other
iptables -A other -p tcp --dport 21 -j ACCEPT #FTP
iptables -A other -p tcp --dport 20 -j ACCEPT #FTP
iptables -A other -p udp --dport 21 -j ACCEPT #FTP
iptables -A other -p udp --dport 20 -j ACCEPT #FTP
iptables -A other -p tcp --dport 110 -j ACCEPT #POP3
iptables -A other -p tcp --dport 25 -j ACCEPT #SMTP
...
#portscan elleni vedelem
iptables -A other -p tcp --tcp-flags SYN,ACK,FIN,RST RST -m limit --limit 1/s -j ACCEPT

#ping-flood elleni vedelem
iptables -A other -p icmp --icmp-type echo-request -m limit --limit 1/s -j ACCEPT
iptables -A other -p tcp ! --syn -m state --state NEW -j DROP

iptables -A other -j REJECT

sleep 1

log_end_msg 0

#Rapache lanc
#Remove from apache if its not bad

sleep 1

iptables -A rapache -m recent --name apache --remove
iptables -A rapache -j ACCEPT

#Rother lanc
#Remove from other if its not bad

sleep 1

iptables -A rother -m recent --name other --remove
iptables -A rother -j ACCEPT

#Input lanc
log_daemon_msg " Input"
iptables -P INPUT DROP
iptables -A INPUT -i lo -j ACCEPT #Localt engedjuk mar

log_end_msg 0

#Ha blacklistes akkor minden probalkozasnal mehet 5 percre
log_daemon_msg " Input 2"
iptables -A INPUT -p tcp --dport xxxx -j ACCEPT #Webmin
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -m recent --update --name blacklist --seconds 36000 -j DROP
#Ha kivarta az idot akkor torolhetjuk
iptables -A INPUT -m recent --name blacklist --remove
iptables -A INPUT -p tcp --dport 4040 -m state --state NEW -j ssh
iptables -A INPUT -p tcp --dport 80 -m state --state NEW -j apache
iptables -A INPUT -p tcp --dport 53 -m state --state NEW -j bind_dns
iptables -A INPUT -p udp --dport 53 -m state --state NEW -j bind_dns
iptables -A INPUT -m state --state NEW -j other
log_end_msg 0

######>>>>>>OUTPUT<<<<<<#####
log_daemon_msg " Output"
iptables -P OUTPUT ACCEPT
log_end_msg 0

log_daemon_msg "Enabling network..."
if [ $1 = "restart" ] || [ $1 = "start" ];
then
log_daemon_msg "Reloading blacklist"
/usr/local/bin/php /etc/recent/back.php
fi;
sysctl -p
iptables -D INPUT -j DROP
log_end_msg 0

Az rother és rapache lánc használaton kívüli /pill.

copy.php:

$f=fopen("/proc/net/ipt_recent/blacklist","r");
$out=fopen("/etc/recent/blacklist.txt","w+");
while (!feof($f) ) {
$line=fgets($f);
$ip=substr($line,strpos($line,"=")+1);
$ip=substr($ip,0,strpos($ip,"ttl")-1);
fputs($out,"$ip\n");
}
fclose($out);
fclose($f);
?>

back.php

$out="/proc/net/ipt_recent/blacklist";
$f=fopen("/etc/recent/blacklist.txt","r");
while (!feof($f)) {
exec("echo '".fgets($f)."' > $out");
}
fclose($f);
?>

XmIsTeR:Az Iptables szabályokat kicsit bonyolítottam, valamint írtam egy szkriptet(phpben, mivel ebben a legegyszerűbb), ami lementi és újratölti a blacklist-et, emellett egyszer éjszaka 1 órán keresztül minden 80-as portra érkező próbálkozást a blacklistbe raktam(ezek 99%-a támadó kellett, h legyen, a maradék 1%, meg úgyse próbálkozott újra 1 óránként egész éjjel, így másnap már nem vehetett észre semmit). Ezután állítottam a blacklist újrainduló számlálót 10 órára. A mostani kapcsolatok száma a szerveren 20!!. Azért nem mind1...
Szal a szkript:

Forrás: http://hup.hu/node/68698&comments_per_page=9999#comment-753685


Kulcsszavak: Linux, tűzfal, támadás, DDOS, hack
KardiWeb2013-04-11 14:37:12
Üdvözöllek. Létezik nginx verzió is? Laci
kiss Péter2013-04-16 22:56:10
Ez nem apache specifikus. Bármilyen webszerver elé tehető, sőt nem csak webszerver. Más porton is működhet.

Új hozzászólás: