您的位置:首页 > 运维架构 > Linux

linux服务器被攻击处理

2016-05-20 14:18 387 查看
2016年5月12日14点左右接到客服通知说,“网站炸了“。

打开xshell,远程登录服务器,top一看负载,平均负载到了100多,要知道平时也就是2-3左右。
查看是httpd服务跑满了,慢慢的...慢慢的...服务器就宕机了,估计是被人cc攻击或者是ddos攻击。
好吧老实去查看日志:
假设Apache日志文件在这里: /var/log/httpd/access.log 【生成环境已经改动了】
查找请求数请20个IP(常用于查找攻来源):
[root@localhost ~]# netstat -anlp|grep 80|grep tcp|awk '{print $5}'|awk -F: '{print $1}'|sort|uniq -c|sort -nr|head -n20
302700 127.0.0.1
xxxxxxxxxxxxxx
本地的回环地址。

查看timewait
[root@localhost ~]# netstat -n|grep TIME_WAIT|awk '{print $5}'|sort|uniq -c|sort -rn|head -n20
11080 192.168.xx.xx:******
xxxxxxxxxxxxxxxxxxxxx
是数据库的链接地址。
某台服务器最多ip访问数:
[root@localhost log]# cat /var/log/httpd/access.log|awk '{print $1}'|sort|uniq -c|sort -nr|head -10
232111 218.xxx.xxx.xxx
144089 xxx.xxx.xxx.xxx
111629 xxx.xxx.xxx.xxx
79444 59.xxx.xxx.xxx
76718 119.xxx.xxx.xxx
68785 xxx.xxx.208.30
62282 xxx.xxx.236.103
*********************
好吧查到“攻击的IP”了。
一台服务器的同一个ip就20多万次,N台服务器负载就是N*20多万次,这个IP肯定不正常。 绑定次IP,发现可以访问网站,认定是CDN的ip。
查看日志某一个访问地址(动态地址)被刷。
查到的还不是218.xxx.xxx.xxx真正的攻击IP,所以没法做限制。最后只能跟CDN服务商联系,让其在cdn端做限制访问,每个ip每秒只能访问多少次。生效之后服务器负载恢复正常。


小小的总结:

1.网站流量正常,刚开始并没有认定为攻击,给解决问题造成一定延误,流量统计无异常,CNZZ统计14:00之后比平时高,但是如果有新进大的推广渠道也能可以被认定为正常。
2.在nginx上做了限制,但其实是Apache的问题,头痛你去医脚?
## 用户的 IP 地址 $binary_remote_addr 作为 Key,每个 IP 地址最多有 50 个并发连接
## 你想开 几千个连接 刷死我? 超过 50 个连接,直接返回 503 错误给你,根本不处理你的请求了
limit_conn_zone $binary_remote_addr zone=TotalConnLimitZone:10m ;
limit_conn  TotalConnLimitZone  50;
limit_conn_log_level notice;

## 用户的 IP 地址 $binary_remote_addr 作为 Key,每个 IP 地址每秒处理 10 个请求
## 你想用程序每秒几百次的刷我,没戏,再快了就不处理了,直接返回 503 错误给你
limit_req_zone $binary_remote_addr zone=ConnLimitZone:10m  rate=10r/s;
limit_req_log_level notice;
server {
listen       80;
server_name  www.xxxxxx.com xxxxxx.com;
root /wwwroot/web/xxxxxx.com/;
access_log off;
error_log /data/wwwroot/log/www.xxxxxx.com-error.log;

if ($host = 'xxxxxx.com') {
rewrite ^/(.*)$ http://www.xxxxxx.com/$1 permanent;
}

location / {
## 最多 5 个排队, 由于每秒处理 10 个请求 + 5个排队,你一秒最多发送 15 个请求过来,再多就直接返回 503 错误给你了
limit_req zone=ConnLimitZone burst=10 nodelay;

proxy_pass      http://127.0.0.1:xxxxxx; expires      1d;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ~ .*\.(jpg|jpeg|gif|png|ico|asf|avi|fla|flv|mp3|mp4|rm|rmi|rmvb|wav|wma|wmv|7z|aiff|bmp|csv|doc|docx|gz|gzip|mid|xml|zip|mov|mpc|mepg|mpg|ods|odt|pdf|ppt|pptx|pxd|qt|ram|rar|rtf|sdc|sitd|swf|sxc|sxw|tar|tgz|tif|tiff|txt|vsd|xls|xlsx)$ {
expires      30d;
access_log off;
}

location ~ .*\.(js|css)$ {
expires      30d;
access_log off;
}

deny 218.xxx.xxx.xxx;
}
3.在程序中做判断,如果恶意点击出现验证码或者其他提示。
4.后续的工作:在服务器上做限制。 http://www.jb51.net/article/58060.htm 【未测试】
仔细想想,如果在本地测试肯定可以通过,但是在服务器有cdn过来,ip都记录的是cdn的ip,如果限制对正常访问者也会限制。
在CDN层做访问次数限制,并且在服务器中添加cdn的IP白名单,对直接攻击服务器ip的做限制。
5.其他命令:
输出404url地址到/root/404page.txt
awk '($9 ~/404/)' /var/log/httpd/access.log | awk '{print $9,$7}' | sort > /root/404page.txt
访问次数最多的时间段
awk '{print  $4}' /var/log/httpd/access.log |cut -c 14-18|sort|uniq -c|sort -nr|head
查看当天访问排行前10的url:
cat /var/log/httpd/access.log | awk '{print $7}' | sort | uniq -c | sort -nr | head -n 10
查看某天访问排行前10的url:
cat /var/log/httpd/access.log | grep "10/Dec/2010" | awk '{print $7}' | sort | uniq -c | sort -nr | head -n 10


本文出自 “红塔山” 博客,请务必保留此出处http://wenzengliu.blog.51cto.com/9378751/1775389
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: