由于linux的系统性能稳定、高开放性和高安全性,导致很多用户都习惯使用linux系统,但是即使是用linux搭载运行的网站,也难免会遭受cc攻击,那么Linux如何防止cc攻击呢?这里给大家详细介绍linux下防止cc攻击的一些方法和技巧。
第一种就是利用本机的防火墙来解决可以安装CSF之内的防火墙,这种的弊端是只能防止小规模的CC攻击和DDOS(我的站在阿里云,所以不用太担心DDOS)CC攻击比较猛的话机器也直接CUP跑满了。
第二种方式是安装网站安全狗。点击【流量保护】进入CC攻击防护界面,开启CC攻击防护功能(一般是默认开启),即可进行防护。
网站安全狗免费下载地址,免费使用:http://free.safedog.cn/website_safedog.html
用户可以使用默认设置的参数。在使用过程中,建议根据实际需求对防护规则进行优化,设置相应的CC攻击防护规则。用户可以设置“访问规则”、“会话验证规则”、“代理规则”、“网站白名单"等。如设置访问规则参数,可设置单个IP每10秒允许最大请求的数目为100次,超过此次数,该IP会被冻结即被限制访问5分钟 或者超过这个次数,IP会被放行30分钟。
同时也可添加【网站白名单】,加入网站白名单的网站不受防护规则影响,这里的网站只单独针对CC攻击防护模块进行设置。【注意在设置参数后,建议进行测试下,如有不适,及时调整参数;或者在设置前,建议可咨询下安全狗官方人员进行了解】
下面就谈谈Linux如何防止cc攻击的一些具体操作吧
首先安装CSF防火墙,这个比较简单而且不用改域名什么的,小规模的就直接解决了。
一、安装依赖包:
代码如下复制代码
yum install perl-libwww-perl perl iptables
二、下载并安装 CSF:
代码如下复制代码
wget http://www.configserver.com/free/csf.tgz
tar -xzf csf.tgz
cd csf
sh install.sh
三、测试 CSF 是否能正常工作:
代码如下复制代码
[root@localhost csf]# perl /etc/csf/csftest.pl
Testing ip_tables/iptable_filter...OK
Testing ipt_LOG...OK
Testing ipt_multiport/xt_multiport...OK
Testing ipt_REJECT...OK
Testing ipt_state/xt_state...OK
Testing ipt_limit/xt_limit...OK
Testing ipt_recent...OK
Testing xt_connlimit...OK
Testing ipt_owner/xt_owner...OK
Testing iptable_nat/ipt_REDIRECT...OK
Testing iptable_nat/ipt_DNAT...OK
RESULT: csf should function on this server
四、csf的配置:
CSF的配置文件是
代码如下复制代码
vim /etc/csf/csf.conf
# Allow incoming TCP ports
# 推荐您更改 SSH 的默认端口(22)为其他端口,但请注意一定要把新的端口加到下一行中
TCP_IN = “20,21,47,81,1723,25,53,80,110,143,443,465,587,993,995〃
# Allow outgoing TCP ports同上,把 SSH 的登录端口加到下一行。
# 在某些程序要求打开一定范围的端口的情况下,例如Pureftpd的passive mode,可使用类似 30000:35000 的方式打开30000-35000范围的端口。
TCP_OUT = “20,21,47,81,1723,25,53,80,110,113,443〃
# Allow incoming UDP ports
UDP_IN = “20,21,53〃
# Allow outgoing UDP ports
# To allow outgoing traceroute add 33434:33523 to this list
UDP_OUT = “20,21,53,113,123〃
# Allow incoming PING 是否允许别人ping你的服务器,默认为1,允许。0为不允许。
ICMP_IN = “1〃
以上这些配置大家一看就懂了,下面再介绍几个比较常用的:
免疫某些类型的小规模 DDos 攻击:
# Connection Tracking. This option enables tracking of all connections from IP
# addresses to the server. If the total number of connections is greater than
# this value then the offending IP address is blocked. This can be used to help
# prevent some types of DOS attack.
#
# Care should be taken with this option. It’s entirely possible that you will
# see false-positives. Some protocols can be connection hungry, e.g. FTP, IMAPD
# and HTTP so it could be quite easy to trigger, especially with a lot of
# closed connections in TIME_WAIT. However, for a server that is prone to DOS
# attacks this may be very useful. A reasonable setting for this option might
# be arround 200.
#
# To disable this feature, set this to 0
CT_LIMIT = "200"##固定时间内同一个IP请求的此数
# Connection Tracking interval. Set this to the the number of seconds between
# connection tracking scans
CT_INTERVAL = "30" ##指上面的固定时间,单位为秒
# Send an email alert if an IP address is blocked due to connection tracking
CT_EMAIL_ALERT = "1" ##是否发送邮件
# If you want to make IP blocks permanent then set this to 1, otherwise blocks
# will be temporary and will be cleared after CT_BLOCK_TIME seconds
# 是否对可疑IP采取永久屏蔽,默认为0,即临时性屏蔽。
CT_PERMANENT = "0"
# If you opt for temporary IP blocks for CT, then the following is the interval
# in seconds that the IP will remained blocked for (e.g. 1800 = 30 mins)
# 临时性屏蔽时间
CT_BLOCK_TIME = "1800"
# If you don’t want to count the TIME_WAIT state against the connection count
# then set the following to “1〃
CT_SKIP_TIME_WAIT = "0" ##是否统计TIME_WAIT链接状态
# If you only want to count specific states (e.g. SYN_RECV) then add the states
# to the following as a comma separated list. E.g. “SYN_RECV,TIME_WAIT”
# Leave this option empty to count all states against CT_LIMIT
CT_STATES = "" ##是否分国家来统计,填写的是国家名
# If you only want to count specific ports (e.g. 80,443) then add the ports
# to the following as a comma separated list. E.g. “80,443〃
#
# Leave this option empty to count all ports against CT_LIMIT
# 对什么端口进行检测,为空则检测所有,防止ssh的话可以为空,统计所有的。
CT_PORTS = ""
做了以上设置之后,可以先测试一下。如果没有问题的话,就更改为正式模式,刚才只是测试模式。
# 把默认的1修改为0。
TESTING = "0"
在/etc/csf/下有csf.allow和csf.deny两个文件,
allow是信任的IP,可以把自己的IP写到这里面防止误封。
deny就是被封的IP。
如果有调整需要重启一下cfs服务
这里在介绍一下Linux下判断CC攻击的命令
查看所有80端口的连接数
netstat -nat|grep -i "80"|wc -l
对连接的IP按连接数量进行排序
netstat -anp | grep 'tcp\|udp' | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -nr
netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -nr
#链接最多的前20
netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -nr|head -20
netstat -ntu | awk '{print $5}' | egrep -o "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}" | sort | uniq -c | sort -nr
查找较多time_wait连接
netstat -n|grep TIME_WAIT|awk '{print $5}'|sort|uniq -c|sort -rn|head -n20
查找较多的SYN连接
netstat -an | grep SYN | awk '{print $5}' | awk -F: '{print $1}' | sort | uniq -c | sort -nr | more
查看TCP连接状态
netstat -nat |awk '{print $6}'|sort|uniq -c|sort -rn
netstat -n | awk '/^tcp/ {print $NF}'|sort|uniq -c|sort -rn
netstat -n | awk '/^tcp/ {++S[$NF]};END {for(a in S) print a, S[a]}'
netstat -n | awk '/^tcp/ {++state[$NF]}; END {for(key in state) print key,"\t",state[key]}'
netstat -n | awk '/^tcp/ {++arr[$NF]};END {for(k in arr) print k,"\t",arr[k]}'
netstat -ant | awk '{print $NF}' | grep -v '[a-z]' | sort | uniq -c
查看80端口连接数最多的20个IP
cat /www/web_logs/waitalone.cn_access.log|awk '{print $1}'|sort|uniq -c|sort -nr|head -100
tail -n 10000 /www/web_logs/waitalone.cn_access.log|awk '{print $1}'|sort|uniq -c|sort -nr|head -100
cat /www/web_logs/waitalone.cn_access.log|awk '{print $1}'|sort|uniq -c|sort -nr|head -100
netstat -anlp|grep 80|grep tcp|awk '{print $5}'|awk -F: '{print $1}'|sort|uniq -c|sort -nr|head -n20
netstat -ant |awk '/:80/{split($5,ip,":");++A[ip[1]]}END{for(i in A) print A,i}' |sort -rn|head -n20
用tcpdump嗅探80端口的访问看看谁最高
tcpdump -i eth0 -tnn dst port 80 -c 1000 | awk -F"." '{print $1"."$2"."$3"."$4}' | sort | uniq -c | sort -nr |head -20
查找较多time_wait连接
netstat -n|grep TIME_WAIT|awk '{print $5}'|sort|uniq -c|sort -rn|head -n20
查找较多的SYN连接
netstat -an | grep SYN | awk '{print $5}' | awk -F: '{print $1}' | sort | uniq -c | sort -nr | more
linux下实用iptables封ip段的一些常见命令:
封单个IP的命令是:
iptables -I INPUT -s 211.1.0.0 -j DROP
封IP段的命令是:
iptables -I INPUT -s 211.1.0.0/16 -j DROP
iptables -I INPUT -s 211.2.0.0/16 -j DROP
iptables -I INPUT -s 211.3.0.0/16 -j DROP
封整个段的命令是:
iptables -I INPUT -s 211.0.0.0/8 -j DROP
封几个段的命令是:
iptables -I INPUT -s 61.37.80.0/24 -j DROP
iptables -I INPUT -s 61.37.81.0/24 -j DROP
想在服务器启动自运行的话有三个方法:
1、把它加到/etc/rc.local中
2、iptables-save >/etc/sysconfig/iptables可以把你当前的iptables规则放到/etc/sysconfig/iptables中,系统启动iptables时自动执行。
3、service iptables save 也可以把你当前的iptables规则放/etc/sysconfig/iptables中,系统启动iptables时自动执行。
后两种更好此,一般iptables服务会在network服务之前启来,更安全。
解封的话:
iptables -D INPUT -s IP地址 -j REJECT #此命令执行后提示:no chain/target/match by that name.
要封停一个IP,使用下面这条命令:
iptables -I INPUT -s ***.***.***.*** -j DROP
要解封一个IP,使用下面这条命令:
iptables -D INPUT -s ***.***.***.*** -j DROP
参数-I是表示Insert(添加),-D表示Delete(删除)。后面跟的是规则,INPUT表示入站,***.***.***.***表示要封停的IP,DROP表示放弃连接。
iptables -F 全清掉了
Linux如何防止cc攻击,如果您死linux系统的用户,完全可以依照上面讲述的方法来操作,但是最好在操作之前对系统进行备份,以防因为误操作而导致系统出现问题,毕竟这是一个自由度非常高的操作系统,在操作时一定要谨慎对待,不能有半点疏忽。