linux的一些命令 -查看cc攻击-网口ip统计等

来源:互联网 发布:spine mac 破解版 编辑:程序博客网 时间:2024/05/17 06:27
Linux判断CC攻击命令详解 
2011年12月23日 ⁄ 安全 ⁄ 暂无评论 
查看所有80端口的连接数 

Java代码  收藏代码
  1. netstat -nat|grep -i '80'|wc -l  
  2. 对连接的IP按连接数量进行排序  
  3.   
  4. netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n  
  5. 查看TCP连接状态  
  6.   
  7. netstat -nat |awk '{print $6}'|sort|uniq -c|sort -rn  
  8. netstat -n | awk '/^tcp/ {++S[$NF]};END {for(a in S) print a, S[a]}'  
  9. netstat -n | awk '/^tcp/ {++state[$NF]}; END {for(key in state) print key,"\t",state[key]}'  
  10. netstat -n | awk '/^tcp/ {++arr[$NF]};END {for(k in arr) print k,"\t",arr[k]}'  
  11. netstat -n |awk '/^tcp/ {print $NF}'|sort|uniq -c|sort -rn  
  12. netstat -ant | awk '{print $NF}' | grep -v '[a-z]' | sort | uniq -c  
  13. 查看80端口连接数最多的20个IP  
  14.   
  15. netstat -anlp|grep 80|grep tcp|awk '{print $5}'|awk -F: '{print $1}'|sort|uniq -c|sort -nr|head -n20  
  16. netstat -ant |awk '/:80/{split($5,ip,":");++A[ip[1]]}END{for(i in A) print A,i}' |sort -rn|head -n20  
  17. 用tcpdump嗅探80端口的访问看看谁最高  
  18.   
  19. tcpdump -i eth0 -tnn dst port 80 -c 1000 | awk -F"." '{print $1″."$2″."$3″."$4}' | sort | uniq -c | sort -nr |head -20  
  20. 查找较多time_wait连接  
  21.   
  22. netstat -n|grep TIME_WAIT|awk '{print $5}'|sort|uniq -c|sort -rn|head -n20  
  23. 查找较多的SYN连接  
  24.   
  25. netstat -an | grep SYN | awk '{print $5}' | awk -F: '{print $1}' | sort | uniq -c | sort -nr | more  



================================ 


防范DDOS攻击脚本 

#防止SYN攻击 轻量级预防 
iptables -N syn-flood 
iptables -A INPUT -p tcp –syn -j syn-flood 
iptables -I syn-flood -p tcp -m limit –limit 3/s –limit-burst 6 -j RETURN 
iptables -A syn-flood -j REJECT 

#防止DOS太多连接进来,可以允许外网网卡每个IP最多15个初始连接,超过的丢弃 
iptables -A INPUT -i eth0 -p tcp –syn -m connlimit –connlimit-above 15 -j DROP 
iptables -A INPUT -p tcp -m state –state ESTABLISHED,RELATED -j ACCEPT 

#用Iptables抵御DDOS (参数与上相同) 
iptables -A INPUT  -p tcp --syn -m limit --limit 12/s --limit-burst 24 -j ACCEPT 
iptables -A FORWARD -p tcp --syn -m limit --limit 1/s -j ACCEPT 

########################################################## 

防范CC攻击 

当apache站点受到严重的cc攻击,我们可以用iptables来防止web服务器被CC攻击,实现自动屏蔽IP的功能。 

1.系统要求 

(1)LINUX 内核版本:2.6.9-42ELsmp或2.6.9-55ELsmp(其它内核版本需要重新编译内核,比较麻烦,但是也是可以实现的)。 

(2)iptables版本:1.3.7 

2. 安装 

安装iptables1.3.7和系统内核版本对应的内核模块kernel-smp-modules-connlimit 

3. 配置相应的iptables规则 

示例如下: 

(1)控制单个IP的最大并发连接数 

Java代码  收藏代码
  1. iptables -I INPUT -p tcp --dport 80 -m connlimit  --connlimit-above 50 -j REJECT  
#允许单个IP的最大连接数为 30 
#默认iptables模块不包含connlimit,需要自己单独编译加载,请参考该地址 
http://sookk8.blog.51cto.com/455855/280372 不编译内核加载connlimit模块 


(2)控制单个IP在一定的时间(比如60秒)内允许新建立的连接数 

Java代码  收藏代码
  1. iptables -A INPUT -p tcp --dport 80 -m recent --name BAD_HTTP_ACCESS --update --seconds 60 --hitcount 30 -j REJECT iptables -A INPUT -p tcp --dport 80 -m recent --name BAD_HTTP_ACCESS --set -j ACCEPT  

#单个IP在60秒内只允许最多新建30个连接 



4. 验证 

(1)工具:flood_connect.c(用来模拟攻击) 

(2)查看效果: 

使用 
Java代码  收藏代码
  1. watch 'netstat -an | grep:21 | grep<模拟攻击客户机的IP>| wc -l'  



实时查看模拟攻击客户机建立起来的连接数, 

使用 
Java代码  收藏代码
  1. watch 'iptables -L -n -v | \grep<模拟攻击客户机的IP>'  



查看模拟攻击客户机被 DROP 的数据包数。 

5.注意 

为了增强iptables防止CC攻击的能力,最好调整一下ipt_recent的参数如下: 

Java代码  收藏代码
  1. #cat/etc/modprobe.conf options ipt_recent ip_list_tot=1000 ip_pkt_list_tot=60  

#记录1000个IP地址,每个地址记录60个数据包 #modprobe ipt_recent 

=========================== 

Nginx 版本信息: 

nginx version: nginx/0.8.53 

Nginx日志配置项: 

Java代码  收藏代码
  1. access_log      /data0/logs/access.log  combined;  


Nginx日志格式: 

Java代码  收藏代码
  1. $remote_addr – $remote_user [$time_local]  $request $status $apache_bytes_sent $http_referer $http_user_agent  
  2.   
  3. 127.0.0.1 - - [24/Mar/2011:12:45:07 +0800"GET /fcgi_bin/xxx.fcgi?id=xxx HTTP/1.0" 200 160 "-" "Mozilla/4.0"  

通过日志查看当天访问页面排前10的url: 

Java代码  收藏代码
  1. #>cat access.log | grep "24/Mar/2011" | awk '{print $7}' | sort | uniq -c | sort -nr | head -n 10  

通过日志查看当天ip连接数,统计ip地址的总连接数 

Java代码  收藏代码
  1. #>cat access.log | grep "24/Mar/2011" | awk '{print $1}' | sort | uniq -c | sort –nr  
  2.   
  3.      38 112.97.192.16  
  4.   
  5.      20 117.136.31.145  
  6.   
  7.      19 112.97.192.31  
  8.   
  9.        3 61.156.31.20  
  10.   
  11.        2 209.213.40.6  
  12.   
  13.        1 222.76.85.28  


通过日志查看当天访问次数最多的10个IP ,只需要在上一个命令后加上head命令 

Java代码  收藏代码
  1. #>cat access.log | grep "24/Mar/2011" |awk '{print $3}'|sort |uniq -c|sort -nr|head –n 10  
  2.   
  3.      38 112.97.192.16  
  4.   
  5.      20 117.136.31.145  
  6.   
  7.      19 112.97.192.31  
  8.   
  9.        3 61.156.31.20  
  10.   
  11.        2 209.213.40.6  
  12.   
  13.        1 222.76.85.28  


通过日志查看当天访问次数最多的10个IP 

Java代码  收藏代码
  1. #>awk '{print $1}' access.log |sort |uniq -c|sort -nr|head  
  2.   
  3.   10680 10.0.21.17  
  4.   
  5.     1702 10.0.20.167  
  6.   
  7.       823 10.0.20.51  
  8.   
  9.       504 10.0.20.255  
  10.   
  11.       215 58.60.188.61  
  12.   
  13.       192 183.17.161.216  
  14.   
  15.         38 112.97.192.16  
  16.   
  17.         20 117.136.31.145  
  18.   
  19.         19 112.97.192.31  
  20.   
  21.           6 113.106.88.10  

通过日志查看当天指定ip访问次数过的url和访问次数: 

Java代码  收藏代码
  1. #>cat access.log | grep "10.0.21.17" | awk '{print $7}' | sort | uniq -c   
  2.   
  3. cat access.log | grep "10.0.21.17" | awk '{print $7}' | uniq -c | sort -nr | head -n 20  
  4.   
  5.     224 /test/themes/default/img/logo_index.gif  
  6.   
  7.     224 /test/themes/default/img/bg_index_head.jpg  
  8.   
  9.     224 /test/themes/default/img/bg_index.gif  
  10.   
  11.     219 /test/vc.php  
  12.   
  13.     219 /  
  14.   
  15.     213 /misc/js/global.js  
  16.   
  17.     211 /misc/jsext/popup.ext.js  
  18.   
  19.     211 /misc/js/common.js  
  20.   
  21.     210 /sladmin/home  
  22.   
  23.     197 /misc/js/flib.js  


通过日志查看当天访问次数最多的时间段 

Java代码  收藏代码
  1. #>awk '{print $4}' access.log | grep "24/Mar/2011" |cut -c 14-18|sort|uniq -c|sort -nr|head  
  2.   
  3.      24 16:49  
  4.   
  5.      19 16:17  
  6.   
  7.      16 16:51  
  8.   
  9.      11 16:48  
  10.   
  11.        4 16:50  
  12.   
  13.        3 16:52  
  14.   
  15.        1 20:09  
  16.   
  17.        1 20:05  
  18.   
  19.        1 20:03  
  20.   
  21.        1 19:55  


rails日志查询ip访问的排行榜 
Ruby代码  收藏代码
  1. cat production.log | grep '2010-10-31' | awk '{print $4}'| sort -u | wc  


----------------- 

持续的监视某块网卡的数据流量 
其中 eht0 对应你想要监视的网卡 bytes 对应中文版系统的“字节” 
1 代表 1秒钟刷新一次 

Java代码  收藏代码
  1. watch -n 1 "/sbin/ifconfig eth0 | grep bytes"  


------------------ 
# sar -n DEV -u 1 10 
看看当前网络流量 

# iostat -t 1 10 
看看当前硬盘读写速度 
---------------------- 

查看文件大小 

du -h --max-depth=1 /路径


转载地址:http://chinacheng.iteye.com/blog/1532729



0 0