How to verify DDOS attack with netstat command on Linux Terminal


Your server appearing pretty slow could be many things from wrong configs, scripts and dodgy hardware – but sometimes it could be because someone is flooding your server with traffic known as DoS ( Denial of Service ) or DDoS ( Distributed Denial of Service ).

Denial-of-service attack (DoS attack) or Distributed Denial-of-service attack (DDoS attack) is an attempt to make a machine or network resource unavailable to its intended users. This attack generally target sites or services hosted on high-profile web servers such as banks, credit card payment gateways, and even root nameservers. DoS attacks are implemented by either forcing the targeted computer to reset, or consuming its resources so that it can no longer provide its services or obstructs the communication media between the users and the victim so that they can no longer communicate adequately.

In this small article you’ll see how to check if your server is under attack from the Linux Terminal with the netstat command




From the man page of netstat “netstat – Print network connections, routing tables, interface statistics, masquerade connections, and multicast memberships”

Some examples with explanation

netstat -na

This display all active Internet connections to the server and only established connections are included.

netstat -an | grep :80 | sort

Show only active Internet connections to the server on port 80, this is the http port and so it’s useful if you have a web server, and sort the results. Useful in detecting a single flood by allowing you to recognize many connections coming from one IP.

netstat -n -p|grep SYN_REC | wc -l

This command is useful to find out how many active SYNC_REC are occurring on the server. The number should be pretty low, preferably less than 5. On DoS attack incidents or mail bombs, the number can jump to pretty high. However, the value always depends on system, so a high value may be average on another server.

netstat -n -p | grep SYN_REC | sort -u

List out the all IP addresses involved instead of just count.

netstat -n -p | grep SYN_REC | awk '{print $5}' | awk -F: '{print $1}'

List all the unique IP addresses of the node that are sending SYN_REC connection status.

netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n

Use netstat command to calculate and count the number of connections each IP address makes to the server.

netstat -anp |grep 'tcp|udp' | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n

List count of number of connections the IPs are connected to the server using TCP or UDP protocol.

netstat -ntu | grep ESTAB | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -nr

Check on ESTABLISHED connections instead of all connections, and displays the connections count for each IP.

netstat -plan|grep :80|awk {'print $5'}|cut -d: -f 1|sort|uniq -c|sort -nk 1

Show and list IP address and its connection count that connect to port 80 on the server. Port 80 is used mainly by HTTP web page request.

How to mitigate a DOS attack

Once that you have found the IP that are attacking your server you can use the following commands to block their connection to your server:

iptables -A INPUT 1 -s $IPADRESS -j DROP/REJECT

Please note that you have to replace $IPADRESS with the IP numbers that you have found with netstat.
After firing the above command, KILL all httpd connections to clean your system and than restart httpd service by
using the following commands:

killall -KILL httpd
 
service httpd start           #For Red Hat systems /etc/init/d/apache2 restart   #For Debian systems





—————————————————————————————       如何在Linux上使用netstat命令查证DDOS***

服务器出现缓慢的状况可能由很多事情导致,比如错误的配置,脚本和差的硬件。但是有时候它可能因为有人对你的服务器用DOS或者DDOS进行洪水***。

DOS***或者DDOS***是试图让机器或者网络资源不可用的***。这种***的***目标网站或者服务通常是托管在高防服务器比如银行,信用卡支付网管,甚至根域名服务器,DOS***的实施通常迫使目标重启计算机或者消耗资源,使他们不再提供服务或者妨碍用户,访客访问。

在这篇小文章中,你可以知道在受到***之后如何在终端中使用netstat命令检查你的服务器。


一些例子和解释

netstat -na显示所有连接到服务器的活跃的网络连接netstat -an | grep :80 | sort只显示连接到80段口的活跃的网络连接,80是http端口,这对于web服务器非常有用,并且对结果排序.对于你从许多的连接中找出单个发动洪水***IP非常有用netstat -n -p|grep SYN_REC | wc -l这个命令对于在服务器上找出活跃的SYNC_REC非常有用,数量应该很低,最好少于5.在dos***和邮件×××,这个数字可能非常高.然而值通常依赖于系统,所以高的值可能平分给另外的服务器.netstat -n -p | grep SYN_REC | sort -u列出所有包含的IP地址而不仅仅是计数.netstat -n -p | grep SYN_REC | awk '{print $5}' | awk -F: '{print $1}'列出所有不同的IP地址节点发送SYN_REC的连接状态netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n使用netstat命令来计算每个IP地址对服务器的连接数量netstat -anp |grep 'tcp|udp' | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n列出使用tcp和udp连接到服务器的数目netstat -ntu | grep ESTAB | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -nr检查ESTABLISHED连接而不是所有连接,这可以每个ip的连接数netstat -plan|grep :80|awk {'print $5'}|cut -d: -f 1|sort|uniq -c|sort -nk 1显示并且列出连接到80端口IP地址和连接数.80被用来作为HTTP


如何缓解DDOS***

当你发现***你服务器的IP你可以使用下面的命令来关闭他们的连接

iptables -A INPUT 1 -s $IPADRESS -j DROP/REJECT



请注意你必须用你使用netstat命令找到的IP数替换$IPADRESS 
在完成以上的命令,使用下面的命令杀掉所有httpd连接,清除你的系统,然后重启httpd服务。

killall -KILL httpd service httpd start           #For Red Hat systems /etc/init/d/apache2 restart   #For Debian systems