阅读(3028)

Linux下如何批量屏蔽恶意IP地址防攻击

最后一次修改 2018年02月09日

很多情况下,你可能需要在Linux下屏蔽IP地址。比如,作为一个终端用户,你可能想要免受间谍软件或者IP追踪的困扰。如果你是一名系统管理员,你可能想要禁止垃圾IP地址访问你们的公司邮件服务器。或者你因一些原因想要禁止某些国家访问你的web服务。在许多情况下,然而,你的IP地址屏蔽列表可能会很快地增长到几万的IP。该如何处理这个?

解决方案: ipset + iblocklist2ipset

安装:

官方网站:http://ipset.netfilter.org/install.html

最简单的方法就是yum安装,但是该方法版本比较低,缺少一些使用的模块参数等,所以不大推荐;

yum install ipset -y

   


编译安装:

1.依赖环境:

yum install libmnl libmnl-devel kernel-devel libtool-devel -y

   


(新版本的安装方法:git pull git://git.netfilter.org/libmnl.git 运行./autogen.sh)

(备注:如果只安装libmnl时,会出现下面的报错:

   
checking for libmnl... configure: error: Package requirements (libmnl >= 1) were not met:
No package 'libmnl' found
Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.
Alternatively, you may set the environment variables libmnl_CFLAGS
and libmnl_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.
)


在编译的时候可能提示找不到/lib/modules/2.6.32-431.el6.x86_64/source

经过排查发现这个软连接/lib/modules/2.6.32-431.el6.x86_64/build -->/usr/src/kernels/2.6.32-431.el6.x86_64 不存在

解决办法:重新建立软连接

ln -sb /usr/src/kernels/2.6.32-573.3.1.el6.x86_64 /lib/modules/2.6.32-431.el6.x86_64/build

   


在运行 ./autogen.sh时报错:

找不到 /usr/share/libtool/

解决办法:安装libtool-devel工具包即可 yum install libtool-devel

2.编译安装ipset (linux kernel source code (version >= 2.6.32))

wget -P /usr/local/src <a rel="nofollow" href="http://ipset.netfilter.org/ipset-6.26.tar.bz2" target="_blank">http://ipset.netfilter.org/ipset-6.26.tar.bz2</a>
cd /usr/local/src && tar xjf ipset-6.26.tar.bz2 && cd ipset-6.26
./autogen.sh
./configure
make
make modules
make install
make modules_install


注意:不同linux内核使用不同版本的源码包

附注:linux kernel source code (version >= 2.6.16 or >= 2.4.36)

编译安装:

   
wget -P /usr/local/src <a rel="nofollow" href="http://ipset.netfilter.org/ipset-4.5.tar.bz2" target="_blank">http://ipset.netfilter.org/ipset-4.5.tar.bz2</a>
cd /usr/local/src && tar xf ipset-4.5.tar.bz2 && cd ipset-4.5
make KERNEL_DIR=<a rel="nofollow" href="http://img.xue163.com/lib/modules/" target="_blank">http://img.xue163.com/lib/modules/</a>$(shell uname -r)/build     #$(shell uname -r)使用shell命令获取
make KERNEL_DIR=<a rel="nofollow" href="http://img.xue163.com/lib/modules/" target="_blank">http://img.xue163.com/lib/modules/</a>$(shell uname -r)/build install

   


常用使用命令:


ipset list 查看ip集列表信息
ipset create pythontab hash:ip maxelem 1000000  创建一个IP集pythontab,指定类型为hash:ip,设置ip集最多存储IP数为1000000
ipset add pythontab X.X.X.X  增加一个ip地址到IP集pythontab中去
ipset add pythontab X.X.X.X/24  增加一个网段到IP集pythontab中去
ipset dell pythontab X.X.X.X   删除IP集中指定的IP地址
ipset list 查看当前所有list
ipset save pythontab -f pythontab.txt  将IP集pythontab中的信息保存到当前文件目录下面的文件pythontab.txt中
ipset destroy pythontab   删除指定的IP集pythontab 
ipset restore -f pythontab.txt  将保存的pythontab.txt文件中的IP集信息重新导入到ipset中
其他命令参考 ipset --help
iptable命令参考:
iptables -I INPUT -m set --match-set pythontab src -p tcp --destination-port 80 -j DROP #拒绝ipset IP集pythontab中的地址访问服务器的80端口
service iptables save
service iptables restart


自动IP地址禁用

现在你应该看到了IP集合的强大了。维护IP黑名单是一件繁琐和费时的工作。实际上,有很多免费或者收费的服务可以来帮你完成这个。一个额外的好处是,让我们看看如何自动将IP黑名单加到IP集中。

首先让我们从iblocklist.com得到免费的黑名单

接下来我要使用一个名为iblocklist2ipset的开源Python工具来将黑名单转化成IP集。

首先,你需要安装了pip

使用的下面命令安装iblocklist2ipset。

$ pip install iblocklist2ipset

   


在一些发行版如Fedora,你可能需要运行:

   
$ python-pip install iblocklist2ipset

   


现在到iblocklist.com,抓取任何一个P2P列表的URL(比如"level1"列表)。

下载解压,然后保存为txt文件,比如叫做pythontab.txt, 因为iblocklist2ipset仅支持url获取list,所以把pythontab.txt放到你网站的任意目录。比如:ipset目录

$ iblocklist2ipset generate --ipset pythontab "http://www.pythontab.com/ipset/pythontab.txt" > pythontab.txt

   


上面的命令运行之后,你会得到一个名为pythontab.txt的文件。如果查看它的内容,你会看到像这些:

   
create pythontab hash:net family inet hashsize 131072 maxelem 237302
add pythontab 1.2.4.0/24
add pythontab 1.2.8.0/24
add pythontab 1.9.75.8/32
add pythontab 1.9.96.105/32
add pythontab 1.9.102.251/32
add pythontab 1.9.189.65/32

   


你可以用下面的ipset命令来加载这个文件:

$ ipset restore -f pythontab.txt


现在可以查看自动创建的IP集:

$ ipset list pythontab

   


这样就省去了手动管理的麻烦。