使用redis来调用iptables,封禁恶意IP

话不多说,通常大多数站点都会有被薅羊毛的情况,防护无非也就是业务层做处理,短时内不再响应恶意请求啦.虽然不响应了,可还是会消耗资源的,比如我要从数据库(当然也可能是内存数据库)去查询下,你是不是恶意的IP. 那么能否网络层或应用层去处理呢?在前几篇文章有写过应用层方案,今天就写下网络层方法.

说起iptables 除非是专业人员,像普通开发者是不会使用的,一堆表一堆链的一看就头疼.所以**RedisPushIptables**就应时而生,开发者不须为iptables复杂语法头疼,只需要像使用redis那样简单,就可使用iptables来阻挡恶意IP地址.

RedisPushIptables是一个redis模块

该模块可以通过 redis 来操作 iptables 的 filter表INPUT链规则的增加和删除,可以用来动态调用防火墙。比如用来防御攻击。

但是前提要以 root 来运行,因为 iptables 需要 root 权限。


git clone https://github.com/limithit/RedisPushIptables.git
cd RedisPushIptables && make

加载模块
MODULE LOAD /path/to/iptablespush.so

语法
accept.insert - Filter table INPUT ADD ACCEPT
accept.delete - Filter table INPUT DEL ACCEPT
drop.insert - Filter table INPUT ADD DROP
drop.delete - Filter table INPUT DEL DROP


127.0.0.1:6379>accept.insert 192.168.188.8
(integer) 13
127.0.0.1:6379>accept.delete 192.168.188.8
(integer) 13
127.0.0.1:6379>drop.delete 192.168.188.8
(integer) 13
127.0.0.1:6379>drop.insert 192.168.188.8
(integer) 13
root@debian:~# iptables -L -n
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
DROP       all  --  192.168.188.8        0.0.0.0/0 
ACCEPT       all  --  192.168.188.8        0.0.0.0/0

从此普通开发也能像运维那样,使用防火墙了.其实我不擅长写作,大伙凑合看吧,就这么多。

原文链接:https://my.oschina.net/MasterXimen/blog/2990886

猜你喜欢

转载自www.cnblogs.com/thatme/p/10230188.html