Một số câu lệnh kiểm tra server khi bị tấn công DDoS

Một số câu lệnh kiểm tra server khi bị tấn công DDoS

- Kiểm tra số connection trên port 80:


Code:

netstat -n | grep :80 |wc -l


- Kiểm tra số lượng connection đang ở trạng thái SYN_RECV:

Code:

netstat -n | grep :80 | grep SYN_RECV|wc -l


- Hiển thị tất cả các IP đang kết nối và số lượng kết nối từ mỗi IP:

Code:

netstat -an|grep :80 |awk '{print $5}'|cut -d":" -f1|sort|uniq -c|sort -rn


Nếu muốn kiểm tra IP nào mở nhiều SYN thì thêm vào:

Code:

netstat -an|grep :80|grep SYN |awk '{print $5}'|cut -d":" -f1|sort|uniq -c|sort -rn


- Đối với server có nhiều IP, để kiểm tra IP nào đang bị tấn công:

Code:

netstat -plan  | grep  :80 | awk '{print $4}'| cut -d: -f1 |sort |uniq -c


Bài viết trên sẽ có vài trường hợp ra kết quả không đúng nếu như sử dụng kernel mới , IP có dạng '::ffff:192.168.1.1:80' . Để tổng quát hơn các bạn nên dùng như sau cho mọi trường hợp :

- Hiển thị tất cả các IP đang kết nối và số lượng kết nối từ mỗi IP:

Code:

[root@localhost~]# netstat -an | grep ':80' | awk '{print $5}' | sed s/'::ffff:'// | cut -d":" -f1 | sort | uniq -c


Đồng thời mình bổ xung thêm :
- Hiển thị số lượng kết nối mỗi loại (kiểm tra xem có phải bị SYN_FLOOD hay DDoS hay ko) :

Code:

root@localhost [~]# netstat -an | grep :80 | awk '{print $6}' | sort | uniq -c
61 ESTABLISHED
13 FIN_WAIT1
17 FIN_WAIT2
1 LISTEN
25 SYN_RECV
298 TIME_WAIT


Trong trường hợp đang bị tấn công , cần phải quan sát các tham số trên một các real time => dùng lệnh 'watch' :

- Hiển thị tất cả các IP đang kết nối và số lượng kết nối từ mỗi IP:

Code:

[root@localhost~]#watch  "netstat -an | grep ':80' | awk '{print \$5}' | sed s/'::ffff:'// | cut -d\":\" -f1 | sort | uniq -c"


- Hiển thị số lượng kết nối mỗi loại :

Code:

root@localhost [~]# watch "netstat -an | grep :80 | awk '{print \$6}' | sort | uniq -c"

How To Reset Linux Firewall Automatically While Testing ConfigurationWith Remote Server Over SSH Session

Q. I'd like to tell my Linux iptables firewall to flush out the current configuration every 5 minutes. This will help when I'm testing a new rules and configuration options. Some time I find myself locked out of my own remote server. How do I reset Linux firewall automatically without issuing hard reboot?

A. You can easily flush out current configuration using iptables command and shell script combo. There is no built in option for this kind of settings. So you need to write a small shell script and call it from crontab file.

Create a firewall reset shell script


Create a /root/reset.fw script:
#!/bin/bash
# reset.fw - Reset firewall
# set x to 0 - No reset
# set x to 1 - Reset firewall
# ---------------------------------------------------------------------------------------------------------------
# Added support for IPV6 Firewall
# ---------------------------------------------------------------------------------------------------------------
# Written by Vivek Gite <vivek@nixcraft.com>
# ---------------------------------------------------------------------------------------------------------------
# You can copy / paste / redistribute this script under GPL version 2.0 or above
# =============================================================
x=1

# set to true if it is CentOS / RHEL / Fedora box
RHEL=false

### no need to edit below  ###
IPT=/sbin/iptables
IPT6=/sbin/ip6tables

if [ "$x" == "1" ];
then
if [ "$RHEL" == "true" ];
then
# reset firewall using redhat script
/etc/init.d/iptables stop
/etc/init.d/ip6tables stop
else
# for all other Linux distro use following rules to reset firewall
### reset ipv4 iptales ###
$IPT -F
$IPT -X
$IPT -Z
for table in $(</proc/net/ip_tables_names)
do
$IPT -t $table -F
$IPT -t $table -X
$IPT -t $table -Z
done
$IPT -P INPUT ACCEPT
$IPT -P OUTPUT ACCEPT
$IPT -P FORWARD ACCEPT
### reset ipv6 iptales ###
$IPT6 -F
$IPT6 -X
$IPT6 -Z
for table in $(</proc/net/ip6_tables_names)
do
$IPT6 -t $table -F
$IPT6 -t $table -X
$IPT6 -t $table -Z
done
$IPT6 -P INPUT ACCEPT
$IPT6 -P OUTPUT ACCEPT
$IPT6 -P FORWARD ACCEPT
fi
else
:
fi

Set permissions:
# chmod +x /root/reset.fw
Create cronjon to reset current configuration every 5 minutes, enter
# crontab -e
OR
# vi /etc/crontab
Append following settings:
*/5 * * * * root /root/reset.fw >/dev/null 2>&1
Please remember to set x to 0 once a working configuration has been created for your Linux system.

Test Iptables Script Remotely


How do I test iptables based script remotely over ssh session? How do I avoid lock outs?

iptables do not have pf like testing option. However, you can use a shell script and cron combo or just use the following syntax:
# /sbin/service iptables restart; sleep 20; /sbin/service iptables stop &
This way you can recover from bad syntax error. If you have your own script:
# /path/to/your.script.sh; sleep 20; /sbin/service iptables stop &
Above will allow you to test changes for a while and then turn off firewall completely. You will be able to login again using ssh after 20 seconds if locked out.

Source: http://www.cyberciti.biz/faq/linux-iptables-firewall-flushout-configuration-every-5minutes/

 
2012 upshell | Header Image by Game Wallpapers
Avatar Gamezine Designed by Cheapest Tablet PC
Supported by Phones 4u