规则配置
pak_num_limit=200
pak_limit_burst=600
ip_max_conn_limit=2
echo 'add ipv4 syn limit .....'
firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 4 -p tcp --dport 22 --tcp-flags SYN,ACK,FIN,RST SYN -m limit --limit $pak_num_limit/s --limit-burst $pak_limit_burst -j ACCEPT
firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 5 -p tcp --dport 22 --tcp-flags SYN,ACK,FIN,RST SYN -j $def_reject_policy
firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 4 -p tcp --dport 80 --tcp-flags SYN,ACK,FIN,RST SYN -m limit --limit $pak_num_limit/s --limit-burst $pak_limit_burst -j ACCEPT
firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 5 -p tcp --dport 80 --tcp-flags SYN,ACK,FIN,RST SYN -j $def_reject_policy
firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 4 -p tcp --dport 443 --tcp-flags SYN,ACK,FIN,RST SYN -m limit --limit $pak_num_limit/s --limit-burst $pak_limit_burst -j ACCEPT
firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 5 -p tcp --dport 443 --tcp-flags SYN,ACK,FIN,RST SYN -j $def_reject_policy
echo 'add ipv6 syn limit .....'
firewall-cmd --direct --permanent --add-rule ipv6 filter INPUT 4 -p tcp --dport 22 --tcp-flags SYN,ACK,FIN,RST SYN -m limit --limit $pak_num_limit/s --limit-burst $pak_limit_burst -j ACCEPT
firewall-cmd --direct --permanent --add-rule ipv6 filter INPUT 5 -p tcp --dport 22 --tcp-flags SYN,ACK,FIN,RST SYN -j $def_reject_policy
firewall-cmd --direct --permanent --add-rule ipv6 filter INPUT 4 -p tcp --dport 80 --tcp-flags SYN,ACK,FIN,RST SYN -m limit --limit $pak_num_limit/s --limit-burst $pak_limit_burst -j ACCEPT
firewall-cmd --direct --permanent --add-rule ipv6 filter INPUT 5 -p tcp --dport 80 --tcp-flags SYN,ACK,FIN,RST SYN -j $def_reject_policy
firewall-cmd --direct --permanent --add-rule ipv6 filter INPUT 4 -p tcp --dport 443 --tcp-flags SYN,ACK,FIN,RST SYN -m limit --limit $pak_num_limit/s --limit-burst $pak_limit_burst -j ACCEPT
firewall-cmd --direct --permanent --add-rule ipv6 filter INPUT 5 -p tcp --dport 443 --tcp-flags SYN,ACK,FIN,RST SYN -j $def_reject_policy
echo 'add ipv4 rst limit .....'
firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 4 -p tcp --dport 22 --tcp-flags SYN,ACK,FIN,RST RST -m limit --limit $pak_num_limit/s --limit-burst $pak_limit_burst -j ACCEPT
firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 5 -p tcp --dport 22 --tcp-flags SYN,ACK,FIN,RST RST -j $def_reject_policy
firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 4 -p tcp --dport 80 --tcp-flags SYN,ACK,FIN,RST RST -m limit --limit $pak_num_limit/s --limit-burst $pak_limit_burst -j ACCEPT
firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 5 -p tcp --dport 80 --tcp-flags SYN,ACK,FIN,RST RST -j $def_reject_policy
firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 4 -p tcp --dport 443 --tcp-flags SYN,ACK,FIN,RST RST -m limit --limit $pak_num_limit/s --limit-burst $pak_limit_burst -j ACCEPT
firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 5 -p tcp --dport 443 --tcp-flags SYN,ACK,FIN,RST RST -j $def_reject_policy
echo 'add ipv6 rst limit .....'
firewall-cmd --direct --permanent --add-rule ipv6 filter INPUT 4 -p tcp --dport 22 --tcp-flags SYN,ACK,FIN,RST RST -m limit --limit $pak_num_limit/s --limit-burst $pak_limit_burst -j ACCEPT
firewall-cmd --direct --permanent --add-rule ipv6 filter INPUT 5 -p tcp --dport 22 --tcp-flags SYN,ACK,FIN,RST RST -j $def_reject_policy
firewall-cmd --direct --permanent --add-rule ipv6 filter INPUT 4 -p tcp --dport 80 --tcp-flags SYN,ACK,FIN,RST RST -m limit --limit $pak_num_limit/s --limit-burst $pak_limit_burst -j ACCEPT
firewall-cmd --direct --permanent --add-rule ipv6 filter INPUT 5 -p tcp --dport 80 --tcp-flags SYN,ACK,FIN,RST RST -j $def_reject_policy
firewall-cmd --direct --permanent --add-rule ipv6 filter INPUT 4 -p tcp --dport 443 --tcp-flags SYN,ACK,FIN,RST RST -m limit --limit $pak_num_limit/s --limit-burst $pak_limit_burst -j ACCEPT
firewall-cmd --direct --permanent --add-rule ipv6 filter INPUT 5 -p tcp --dport 443 --tcp-flags SYN,ACK,FIN,RST RST -j $def_reject_policy
echo 'add ipv6 conn limit .....'
firewall-cmd --direct --permanent --add-rule ipv6 filter INPUT 3 -p tcp --syn --dport 80 -m connlimit --connlimit-above $ip_max_conn_limit -j $def_reject_policy
firewall-cmd --direct --permanent --add-rule ipv6 filter INPUT 3 -p tcp --syn --dport 22 -m connlimit --connlimit-above $ip_max_conn_limit -j $def_reject_policy
firewall-cmd --direct --permanent --add-rule ipv6 filter INPUT 3 -p tcp --syn --dport 443 -m connlimit --connlimit-above $ip_max_conn_limit -j $def_reject_policy
echo 'add ipv4 conn limit .....'
firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 3 -p tcp --syn --dport 80 -m connlimit --connlimit-above $ip_max_conn_limit -j $def_reject_policy
firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 3 -p tcp --syn --dport 22 -m connlimit --connlimit-above $ip_max_conn_limit -j $def_reject_policy
firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 3 -p tcp --syn --dport 443 -m connlimit --connlimit-above $ip_max_conn_limit -j $def_reject_policy
echo 'add ipv4 risk pak drop policy .....'
firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 3 -p tcp --tcp-flags ALL ALL -j $def_reject_policy
firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 3 -p tcp --tcp-flags ALL FIN,PSH,URG -j $def_reject_policy
firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 3 -p tcp --tcp-flags ALL NONE -j $def_reject_policy
firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 3 -p tcp --tcp-flags SYN,RST SYN,RST -j $def_reject_policy
firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 3 -p tcp --tcp-flags SYN,FIN SYN,FIN -j $def_reject_policy
echo 'add ipv6 risk pak drop policy .....'
firewall-cmd --direct --permanent --add-rule ipv6 filter INPUT 3 -p tcp --tcp-flags ALL NONE -j $def_reject_policy
firewall-cmd --direct --permanent --add-rule ipv6 filter INPUT 3 -p tcp --tcp-flags SYN,RST SYN,RST -j $def_reject_policy
firewall-cmd --direct --permanent --add-rule ipv6 filter INPUT 3 -p tcp --tcp-flags SYN,FIN SYN,FIN -j $def_reject_policy
firewall-cmd --direct --permanent --add-rule ipv6 filter INPUT 3 -p tcp --tcp-flags ALL ALL -j $def_reject_policy
firewall-cmd --direct --permanent --add-rule ipv6 filter INPUT 3 -p tcp --tcp-flags ALL FIN,PSH,URG -j $def_reject_policy
规则测试
模拟请求测试 模拟tcp flood请求工具有 hping 和 nping hping 不支持ipv6 因此 最好使用 nping 来进行测试。两个工具都是 kali机的自带工具。可以直接下载kali虚拟机然后进行测试,也可自行百度安装。
– -6 表示测试ipv6 , 默认ipv4 端口 22 tcp请求 请求头标志位 syn fd15:4ba5:5a2b:1008:5bd8:79c3:7405:ca47 是ip地址,-c 100表示连续请求100次 nping -6 --tcp -p 22 --flags syn fd15:4ba5:5a2b:1008:5bd8:79c3:7405:ca47 -c100
最大连接数测试 最大连接数测试可以通过 编写tcp 服务端和客户端程序,在机器上进行连接数量测试,这里使用go语言编写, 服务端代码:
package main
import (
"bufio"
"fmt"
"log"
"net"
"os"
)
func main(){
log.Println("程序运行参数:",os.Args)
if len(os.Args)>1 && len(os.Args[1])>0{
log.Println("====>监听地址端口:",os.Args[1])
}else{
log.Println(" 没有参数!")
return
}
fmt.Println("服务启动...")
listen,err := net.Listen("tcp",os.Args[1])
if(err != nil){
fmt.Println("listen failed,err:",err)
return
}
for{
conn,err := listen.Accept()
if(err != nil){
fmt.Println("accept failed ,err:",err)
continue
}
//开启携程处理
go process(conn)
}
}
func process(conn net.Conn) {
defer conn.Close()
for{
reader:=bufio.NewReader(conn)
var buf [128]byte
n,err := reader.Read(buf[:])
if err!=nil {
fmt.Println("read from client failed err:",err)
break
}
recvStr := string(buf[:n])
fmt.Println("recvStr:",recvStr)
conn.Write([]byte("已收到数据,谢谢"))
}
}
客户端代码:
package main
import (
"bufio"
"fmt"
"log"
"net"
"os"
"strings"
)
func main(){
log.Println("程序运行参数:",os.Args)
if len(os.Args)>1 && len(os.Args[1])>0{
log.Println("====>监听地址端口:",os.Args[1])
}else{
log.Println(" 没有参数!")
return
}
//conn,err:=net.Dial("tcp",fmt.Sprint("192.168.47.10:",os.Args[1]))
conn,err:=net.Dial("tcp",os.Args[1])
if err != nil{
fmt.Println("连接主机错误,err:",err)
return
}
defer conn.Close()
ir:=bufio.NewReader(os.Stdin)
for{
fmt.Print("请输入发送内容:")
input,_ := ir.ReadString('\n')
inputInfo := strings.Trim(input,"\r\n")
if strings.ToUpper(inputInfo)=="Q"{
fmt.Println("收到退出指令,开始推出。。。")
return
}
_,err = conn.Write([]byte(inputInfo))
if err != nil {
return
}
var buf [512]byte
n,err := conn.Read(buf[:])
if err != nil {
return
}
fmt.Println(string(buf[:n]))
}
}
将上述代码分别编译成可执行程序 部署到需要测试的服务器,然后进行连接测试即可。 编译命令
set GOARCH=amd64
set GOOS=linux
go build main.go -o tcpserv
go build main.go -o tcpcli
服务端执行命令:
tcpserv ip:port --运行并监听指定ip和端口 如 192.168.47.10:80
tcpcli ip:port --连接服务端所在的ip和监听的端口 如 192.168.47.10:80
通过如下命令查看 连接数,超过限制连接数时是否可以继续增加连接
[root@localhost ~]
tcp6 0 0 fe80::afb9:dd2b:a39b:22 fe80::f91e:ad1:62:27123 ESTABLISHED
tcp6 0 0 fe80::afb9:dd2b:a39b:22 fe80::f91e:ad1:62:27141 ESTABLISHED
tcp6 0 0 fe80::afb9:dd2b:a39b:22 fe80::f91e:ad1:626:1329 ESTABLISHED
tcp6 0 0 fe80::afb9:dd2b:a39b:22 fe80::f91e:ad1:62:27130 ESTABLISHED
经测试该限制对ip6不起作用。 通过 ip(6)tables -nvL 查看 防火墙各规则命中数,如果超限时 被规则 命中并拦截 证明规则生效,如下该规则 被命中5次
[root@localhost ~]
Chain INPUT (policy ACCEPT 974 packets, 72397 bytes)
pkts bytes target prot opt in out source destination
5 260 REJECT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:22
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 754 packets, 79106 bytes)
pkts bytes target prot opt in out source destination
|