很多情况下,你可能需要在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 http://ipset.netfilter.org/ipset-6.26.tar.bz2
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 http://ipset.netfilter.org/ipset-4.5.tar.bz2
cd /usr/local/src && tar xf ipset-4.5.tar.bz2 && cd ipset-4.5
make kernel_dir=http://img.xue163.com/lib/modules/$(shell uname -r)/build #$(shell uname -r)使用shell命令获取
make kernel_dir=http://img.xue163.com/lib/modules/$(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
这样就省去了手动管理的麻烦。
注意,在centos下使用yum安装的不是最新版,可能会不支持-f参数,导入黑名单文件,所以建议用源码包安装最新版本
以上就是linux下批量屏蔽恶意ip地址防攻击的方法详解的详细内容。
