大网数据广告活动
服务器运维问答

iptables consumes a lot of cpu when blocking many IPs

· 52CE 我爱测

I run a public UDP based service on AWS. A few weeks ago, alarms starting going off that revealed a UDP flood attack was in effect. A few years back, as result of a previous DDOS attack, I updated the socket code to detect these …

问题原文

原帖链接 · selbie · CC BY-SA 4.0

I run a public UDP based service on AWS. A few weeks ago, alarms starting going off that revealed a UDP flood attack was in effect.

A few years back, as result of a previous DDOS attack, I updated the socket code to detect these types of attacks and to ignore incoming packets from any IP attempting to flood the port with bogus requests.

Fast forward to today. The new attack was an order of magnitude than anything seen previously. So I spun up a few new instances for load balancing and tweaked the code to more aggressively throttle. Crisis averted.

I also got the idea to also try collecting all the IPs of all the flooders and just block each IP via an iptables call below.

sudo iptables -A INPUT -s 1.2.3.4 -j DROP

Where 1.2.3.4 was replaced with an actual IP address. And then I had a script that would ban that would invoke this command for the 2000+ IP addresses that were involved with the flood attack based on other scripts I had run. I did this on one particular AWS instance while leaving the other instances alone. The idea being that it would be more effective to block the offending IP addresses at the firewall level rather than letting the server code itself deal with it.

The result was that CPU usage spiked way up (above 50%) on the instance with all the iptables rules. The other machines were relying on their own software code to drop flooding IPs and were at normal CPU levels. I wound up deleting the rules from this specific instance.

I would think that looking up an IP address in a table would be a cheap operation on the order of O(1) with a reasonably sized hash table. Perhaps it's linear or just the fact that iptables needs to log and do bookkeeping on each blocked packet. r perhaps there's a better way to mass block this many IPs.

What's the best way to block out 2000+ IP addresses without incurring additional CPU overhead?

已采纳解答

原帖链接 · Tomek · CC BY-SA 4.0

The rules, as you wrote it, will be looked up one by one and will take time to process. What you are looking for is likely ipset.

ipset create banned-hosts hash:ip
ipset add banned-hosts 1.2.3.4
:
ipset add banned-hosts 2.4.6.8

and match on it:

iptables -A INPUT -m set --match-set banned-hosts src -j DROP

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-07-07)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。