您的位置:首页 > 理论基础 > 计算机网络

sys和cc攻击防范分享

2016-07-20 12:03 302 查看


调整系统参数挡攻击

第一个参数tcp_synack_retries = 0是关键,表示回应第二个握手包(SYN+ACK包)给客户端IP后,如果收不到第三次握手包(ACK包)后,不进行重试,加快回收“半连接”,不要耗光资源。

不修改这个参数,模拟攻击,10秒后被攻击的80端口即无法服务,机器难以ssh登录; 用命令netstat -na |grep SYN_RECV检测“半连接”hold住180秒;

修改这个参数为0,再模拟攻击,持续10分钟后被攻击的80端口都可以服务,响应稍慢些而已,只是ssh有时也登录不上;检测“半连接”只hold住3秒即释放掉。

修改这个参数为0的副作用:网络状况很差时,如果对方没收到第二个握手包,可能连接服务器失败,但对于一般网站,用户刷新一次页面即可。这些可以在高峰期或网络状况不好时tcpdump抓包验证下。

根据以前的抓包经验,这种情况很少,但为了保险起见,可以只在被tcp洪水攻击时临时启用这个参数。

tcp_synack_retries默认为5,表示重发5次,每次等待30~40秒,即“半连接”默认hold住大约180秒。详细解释:
The tcp_synack_retries setting tells the kernel how many times to retransmit the SYN,ACK reply to

an SYN request. In other words, this tells the system how many times to try to establish a passive

TCP connection that was started by another host.

This variable takes an integer value, but should under no circumstances be larger than 255 for the

same reasons as for the tcp_syn_retries variable. Each retransmission will take aproximately 30-40

seconds. The default value of the tcp_synack_retries variable is 5, and hence the default timeout

of passive TCP connections is aproximately 180 seconds.

之所以可以把tcp_synack_retries改为0,因为客户端还有tcp_syn_retries参数,默认是5,即使服务器端没有重发SYN+ACK包,客户端也会重发SYN握手包。详细解释:
The tcp_syn_retries variable tells the kernel how many times to try to retransmit the initial SYN

packet for an active TCP connection attempt.

This variable takes an integer value, but should not be set higher than 255 since each

retransmission will consume huge amounts of time as well as some amounts of bandwidth. Each

connection retransmission takes aproximately 30-40 seconds. The default setting is 5, which

would lead to an aproximate of 180 seconds delay before the connection times out.

第二个参数net.ipv4.tcp_max_syn_backlog = 200000也重要,具体多少数值受限于内存。

以下配置,第一段参数是最重要的,第二段参数是辅助的,其余参数是其他作用的:

# vi /etc/sysctl.conf

1234567891011121314151617181920#最关键参数,默认为5,修改为0 表示不要重发net.ipv4.tcp_synack_retries = 0#半连接队列长度net.ipv4.tcp_max_syn_backlog = 200000 #系统允许的文件句柄的最大数目,因为连接需要占用文件句柄fs.file-max = 819200#用来应对突发的大并发connect 请求net.core.somaxconn = 65536#最大的TCP 数据接收缓冲(字节)net.core.rmem_max = 1024123000 #最大的TCP 数据发送缓冲(字节)net.core.wmem_max = 16777216#网络设备接收数据包的速率比内核处理这些包的速率快时,允许送到队列的数据包的最大数目net.core.netdev_max_backlog = 165536#本机主动连接其他机器时的端口分配范围net.ipv4.ip_local_port_range = 10000 65535 # ……省略其它……
使配置生效:
# sysctl -p注意,以下参数面对外网时,不要打开。因为副作用很明显,具体原因请google,如果已打开请显式改为0,然后执行sysctl -p关闭。因为经过试验,大量TIME_WAIT状态的连接对系统没太大影响:

1

2

3

4

5

6

7

8

#当出现 半连接 队列溢出时向对方发送syncookies,调大 半连接 队列后没必要

net.ipv4.tcp_syncookies
=
0

#TIME_WAIT状态的连接重用功能

net.ipv4.tcp_tw_reuse
=
0

#时间戳选项,与前面net.ipv4.tcp_tw_reuse参数配合

net.ipv4.tcp_timestamps
=
0

#TIME_WAIT状态的连接回收功能

net.ipv4.tcp_tw_recycle
=
0

为了处理大量连接,还需改大另一个参数:

# vi /etc/security/limits.conf 

在底下添加一行表示允许每个用户都最大可打开409600个文件句柄(包括连接):

*                –       nofile          409600
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  tcp 攻击