惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

雷峰网
雷峰网
N
Netflix TechBlog - Medium
博客园_首页
J
Java Code Geeks
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Y
Y Combinator Blog
腾讯CDC
V
V2EX
Microsoft Security Blog
Microsoft Security Blog
大猫的无限游戏
大猫的无限游戏
Cyberwarzone
Cyberwarzone
N
News and Events Feed by Topic
L
LINUX DO - 最新话题
Schneier on Security
Schneier on Security
Microsoft Azure Blog
Microsoft Azure Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Hacker News: Ask HN
Hacker News: Ask HN
Martin Fowler
Martin Fowler
Google DeepMind News
Google DeepMind News
G
Google Developers Blog
U
Unit 42
WordPress大学
WordPress大学
N
News and Events Feed by Topic
S
Schneier on Security
T
The Blog of Author Tim Ferriss
B
Blog
博客园 - 叶小钗
Forbes - Security
Forbes - Security
F
Fortinet All Blogs
Project Zero
Project Zero
K
Kaspersky official blog
Apple Machine Learning Research
Apple Machine Learning Research
L
LINUX DO - 热门话题
The GitHub Blog
The GitHub Blog
H
Hacker News: Front Page
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
PCI Perspectives
PCI Perspectives
The Register - Security
The Register - Security
www.infosecurity-magazine.com
www.infosecurity-magazine.com
W
WeLiveSecurity
C
Cyber Attacks, Cyber Crime and Cyber Security
罗磊的独立博客
S
Security @ Cisco Blogs
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Tailwind CSS Blog
P
Proofpoint News Feed
S
SegmentFault 最新的问题
D
Docker
量子位
M
MIT News - Artificial intelligence

鼠鼠在碎觉

墨流 ---- 针对 hexo+github pages 的写作客户端 鼠标滚轮坏了?用轻舟代替 | 鼠鼠在碎觉 五一咕咕咕 | 鼠鼠在碎觉 小桃子诞生记 | 鼠鼠在碎觉 光与影:33号远征队——论防沉迷的重要性 | 鼠鼠在碎觉 生性凉薄 | 鼠鼠在碎觉 2025 空白页 | 鼠鼠在碎觉 我将注定孤身一人 | 鼠鼠在碎觉 最终幻想4月之归还----又是月亮惹的祸 | 鼠鼠在碎觉 小老鼠五周岁记 | 鼠鼠在碎觉 服务部署命令备份 | 鼠鼠在碎觉 仙剑奇侠传七----大概也只剩下情怀了(多图杀猫) | 鼠鼠在碎觉 2024 三亚 | 鼠鼠在碎觉 2024 Nothing | 鼠鼠在碎觉 八方旅人----活着,就是一场旅行 | 鼠鼠在碎觉 小老鼠四周岁记 | 鼠鼠在碎觉 写写最近淘宝小程序遇到的坑 | 鼠鼠在碎觉 来自天空的告白书 | 鼠鼠在碎觉 就这样吧 | 鼠鼠在碎觉 十里红妆----花轿 | 鼠鼠在碎觉 古墓丽影:崛起----“不死先知和不死军团最终倒在了我的手下” | 鼠鼠在碎觉
服务器攻击记录 | 鼠鼠在碎觉
唐墨夏 · 2025-06-23 · via 鼠鼠在碎觉

00

身为一个半路出家的前端,本来让我写后端已经够高估我了,磕磕绊绊总算写完上线
完了还得部署上去,服务器这块玩的是真的少
然后没两天,阿里云就连接数报警了,直接几万个连接
然后服务器看着又一切正常,nginx日志看着也没毛病
问鸡皮提 top netstat ss 啥的都没有异常
反正找了半天没找到

01

最后总算是发现了,用的conntrack
conntrack -L | awk '{print $5}' | cut -d= -f2 | cut -d' ' -f1 | sort | uniq -c | sort -nr | head
直接列出了ip 和 连接数
那就好办了,直接iptables封禁
iptables -I INPUT -s 192.168.1.1 -j DROP
iptables -I FORWARD -s 192.168.1.1 -j DROP
然后断开链接
conntrack -D -s 192.168.1.1
保存下iptables
iptables-save > /etc/sysconfig/iptables

如果要给他解封,删除对应记录就行
iptables -D INPUT -s 192.168.1.1 -j DROP
iptables -D FORWARD -s 192.168.1.1 -j DROP
或者先查询记录
iptables -L INPUT -n --line-numbers
获取行数后删除对应的行
iptables -D INPUT 1
FORWARD同理
iptables -L FORWARD -n --line-numbers
iptables -D FORWARD 1

02

能自动的事绝不手动,这必然是要上脚本的
然后继续问鸡皮提,没学过,磕磕绊绊搞了很久
最后手动执行可以了,定时执行又不行了
最后发现是没有设置path

03

记录下脚本备用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/bin/bash
export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
export SHELL=/bin/bash


THRESHOLD=100
WHITELIST=("127.0.0.1" "172.17.0.5")
LOGFILE="/var/log/iptables_syn_block.log"
IPTABLES_SAVE_FILE="/etc/sysconfig/iptables"


mkdir -p "$(dirname "$LOGFILE")"
touch "$LOGFILE"
echo "====== $(date) 脚本启动 ======" >> "$LOGFILE"


get_conn_stats() {

echo "----- 调试:处理后IP列表 -----" >> "$LOGFILE"
conntrack -L 2>/dev/null | awk '{
for(i=1;i<=NF;i++) {
if($i=="src") {print $(i+1)}
else if($i~"src=") {print substr($i,5)}
}
}' | sort | uniq -c | sort -nr | head -n 20
}


save_iptables_rules() {
echo "$(date): 保存iptables规则到 $IPTABLES_SAVE_FILE" >> "$LOGFILE"
iptables-save > "$IPTABLES_SAVE_FILE"
}



ban_ip() {
local IP=$1
local COUNT=$2


if ! iptables -nL INPUT | grep -q "$IP"; then
iptables -I INPUT 1 -s "$IP" -j DROP && \
echo "$(date): [成功] INPUT封禁 $IP (连接数: $COUNT)" >> "$LOGFILE" || \
echo "$(date): [失败] INPUT封禁 $IP 失败" >> "$LOGFILE"
fi


if ! iptables -nL FORWARD | grep -q "$IP"; then
iptables -I FORWARD 1 -s "$IP" -j DROP && \
echo "$(date): [成功] FORWARD封禁 $IP (连接数: $COUNT)" >> "$LOGFILE" || \
echo "$(date): [失败] FORWARD封禁 $IP 失败" >> "$LOGFILE"
fi


conntrack -D -s "$IP" &>/dev/null && \
echo "$(date): 已清除 $IP 的连接追踪" >> "$LOGFILE" || \
echo "$(date): 清除 $IP 连接失败" >> "$LOGFILE"
}


echo "当前连接统计:" | tee -a "$LOGFILE"
STATS=$(get_conn_stats)
echo "$STATS" | tee -a "$LOGFILE"


echo "$STATS" | while read COUNT IP; do
[[ -z "$IP" ]] && continue


if [[ "$IP" =~ ^172\.1[6-9]\. || "$IP" =~ ^172\.2[0-9]\. || "$IP" =~ ^172\.3[0-1]\. ]]; then
echo "$(date): 跳过内网IP $IP" >> "$LOGFILE"
continue
fi


SKIP=false
for WIP in "${WHITELIST[@]}"; do
[[ "$IP" == "$WIP" ]] && SKIP=true && break
done
$SKIP && continue

if (( COUNT > THRESHOLD )); then
echo "正在封禁 $IP (连接数: $COUNT)" | tee -a "$LOGFILE"
ban_ip "$IP" "$COUNT"
fi
done


save_iptables_rules

echo "====== $(date) 执行结束 ======" >> "$LOGFILE"

保存文件 给权限
chmod +x /usr/local/bin/auto_block_syn.sh
创建定时任务 每分钟走一次
crontab -e
* * * * * /usr/local/bin/auto_block_syn.sh
然后看日志应就有记录了

04

可气的是,我写完了,他不来了,可恶啊

版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来源 鼠鼠在碎觉

赞助

  • wechat

    wechat

  • alipay

    alipay