记录一次解决cc攻击

703 字
4 分钟
记录一次解决cc攻击
录一次 cc 攻击]

虽然没完全解决,但是屏蔽大量攻击,减轻服务器负担.此文档只是简单介绍下 web 防火墙原理

1 泳道图#

image-20220425100551921
image-20220425100551921

2 配置 Nginx#

cat conf.d/wxh.super.co.conf

limit_req_zone $remote_addr zone=allips:10m rate=30r/s; # 每个ip一秒30次
# limit_req zone=allips; #全局限制
server {
server_name wxh.super.com;
......
if ($request_uri ~* "kto0a81u") {
return 503;
} #只要访问以kto0a81u为结尾的连接自动跳到503
location /
{
allow 122.14.1.21; #白名单
limit_req zone=allips #限制访问/user/login链接每个ip一秒10次
try_files $uri $uri/ @router;
index index.html;
}
location @router
{
rewrite ^.*$ /index.html last;
}
}

nginx.config: 这个很关键,主要 fail2ban 用来提取关键字,上面的配置文件访问过道会报出 503 的错误

http {
.....
log_format main
'{"timestamp":"$time_iso8601_ms",'
'"server_ip":"$server_addr",'
'"remote_ip":"$remote_addr",'
'"xff":"$http_x_forwarded_for",'
'"remote_user":"$remote_user",'
'"domain":"$host",'
'"url":"$request_uri",'
'"referer":"$http_referer",'
'"upstreamtime":"$upstream_response_time",'
'"responsetime":"$request_time",'
'"request_method":"$request_method",'
'"status":"$status",'
'"response_length":"$bytes_sent",'
'"request_length":"$request_length",'
'"protocol":"$server_protocol",'
'"upstreamhost":"$upstream_addr",'
'"http_user_agent":"$http_user_agent"'
'}';
.....
}

3 fail2ban#

3.1 安装#

yum -y install epel-release yum -y install fail2ban

3.2 准备 fail2ban,注意,里面任何文件都不要删,否则会报错#

目录结构:

/etc/fail2ban ## fail2ban 服务配置目录 /etc/fail2ban/action.d ## firewalld 、mail 等动作文件目录 /etc/fail2ban/filter.d ## 条件匹配文件目录,过滤日志关键内容 /etc/fail2ban/jail.d ## 规则文件目录,按具体防护项目分成文件 /etc/fail2ban/jail.local ## 默认规则文件 /etc/fail2ban/jail.conf ## fail2ban 防护配置文件 /etc/fail2ban/fail2ban.conf ## fail2ban 配置文件,定义日志级别、日志、sock 文件位置等

Terminal window
# This file is part of the fail2ban-firewalld package to configure the use of
# the firewalld actions as the default actions. You can remove this package
# (along with the empty fail2ban meta-package) if you do not use firewalld
[DEFAULT]
banaction = firewallcmd-ipset #使用firewallcmd
action = %(action_mwl)s #触发后动作

添加 jail.local

Terminal window
cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
vim /etc/fail2ban/jail.local
# banaction行内容替换如下
banaction = firewalld

配置 firewalld

Terminal window
vim /etc/fail2ban/action.d/firewalld.conf
[INCLUDES]
before =
[Definition]
actionstart =
actionstop =
actioncheck =
actionflush = sed -i '/<source address=/d' /etc/firewalld/zones/drop.xml
actionban = firewall-cmd --change-source=<ip> --zone=drop && firewall-cmd --change-source=<ip> --zone=drop --permanent
actionunban = firewall-cmd --remove-source=<ip> --zone=drop && firewall-cmd --remove-source=<ip> --zone=drop --permanent || echo 0
[Init]

3.3 防止 Ssh 暴力破解#

cat /etc/fail2ban/jail.d/sshd.local

Terminal window
[sshd] #名字,查看封禁ip的时候用到
enabled = true #开启状态
filter = sshd #规则名称,必须填写位于filter.d目录里面的规则,sshd是fail2ban内置规则
action = %(action_mwl)s
logpath = /var/log/secure #日志路径

3.4 防止 Cc 登录#

cat /etc/fail2ban/filter.d/nginx.conf

Terminal window
[Definition]
failregex = ^{.*"remote_ip":"<HOST>",.*"status":"503".*}
ignoreregex = # 这里不要配,会损失一半性能

cat /etc/fail2ban/jail.d/nginx.local

Terminal window
[nginx]
enabled = true
port = http,https #端口
filter = nginx
action = %(action_mwl)s
bantime = 86400
findtime = 1
maxretry = 8
logpath = /var/log/nginx/access.log

重启服务 systemctl restart fail2ban fail2ban-client status nginx

Terminal window
Status for the jail: nginx
|- Filter
| |- Currently failed: 0
| |- Total failed: 0
| `- File list: /opt/nginx-1.13.7-prod/logs/openapi.juxinda360.cn_access.log
`- Actions
|- Currently banned: 79
|- Total banned: 79
`- Banned IP list: 1.193.69.212 1.56.17.17 1.56.21.168 1.56.22.168 1.56.23.197 1.56.23.20 1.56.23.27 1.58.169.134 1.62.145.3 106.91.160.102 112.49.224.157 113.127.205.129 113.232.162.157 113.232.165.90 113.232.176.51 113.239.196.205 113.4.124.189 113.4.176.241 113.4.251.111 114.101.211.22 117.136.30.21 117.136.75.203 117.136.81.63 117.28.80.103 119.115.240.68 119.118.115.196 119.118.126.237 119.52.40.18 119.52.40.218 119.52.58.142 119.52.59.80 119.55.114.184 119.55.119.191 119.55.75.28 123.147.248.213 139.214.144.40 175.168.15.107 175.172.176.235 175.174.95.244 182.149.201.235 202.110.40.130 218.24.58.240 218.7.116.157 218.7.116.2 220.195.66.70 220.197.208.38 220.201.199.241 221.200.177.210 221.200.184.244 221.203.85.168 221.206.200.181 221.206.200.209 221.206.200.222 221.206.200.233 221.206.200.236 221.206.200.64 221.206.201.114 221.206.201.124 221.206.201.173 221.206.201.95 221.210.130.139 221.210.130.150 223.104.178.83 223.104.251.59 223.104.3.184 223.104.65.233 223.104.96.86 42.176.11.18 42.176.9.149 42.178.136.167 42.185.16.167 42.185.18.225 42.185.20.30 42.185.20.44 42.185.21.115 42.185.23.104 42.55.16.57 42.59.233.96 42.86.58.84

文章分享

如果这篇文章对你有帮助,欢迎分享给更多人!

记录一次解决cc攻击
https://ccops.cc/posts/a993d703/
作者
蹦蹦站长
发布于
2021-11-15
许可协议
CC BY-NC-SA 4.0

评论区

Profile Image of the Author
蹦蹦站长
一个养兔子的运维
公告
感谢你的来访!希望在这里能找到对你有用的内容!
音乐
封面

音乐

暂未播放

0:00 0:00
暂无歌词
分类
标签
站点统计
文章
81
分类
6
标签
45
总字数
88,456
运行时长
0
最后活动
0 天前
站点信息
构建平台
Local
博客版本
Firefly v6.12.1
文章许可
CC BY-NC-SA 4.0

文章目录