Centos6下iptables配置Xtables-Addons和GeoIP屏蔽某个国家ip

虞修平
2023-12-01
今天服务器上流量猛增,ip都来自于中国,而且是非正常访问的ip,导致php-fpm耗CPU 100%,网站打开非常慢,本来已经使用iptables限制连接数,但由于同一ip的连接数达不到,所以没办法进行限制,只能采用屏蔽某个地区ip的方法了, Xtables-Addons就是这样的模块,只需要编译此模块,而不必编译系统内核,就可以和iptables一起工作,达到过滤某个地区的ip。

第一步,检查系统iptables版本,Xtables-Addons要与iptables版本一致,例如iptables是1.4.7,就需要对应在的Xtables-Addons 1.47
  1. # uname -r
  2. 2.6.32-358.18.1.el6.x86_64
  3. # iptables -V
  4. iptables v1.4.7

那么就要下载Xtables-Addons 1.47了。

另外需要关闭selinux,编辑/etc/selinux/config,修改为disabled,并使其生效:echo 0 > /selinux/enforce。

第二步,安装perl-Text-CSV_XS依赖包
  1. # rpm -Uvh http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm
  2. # yum -y install perl-Text-CSV_XS

第三步,下载和编译xtables-addons模块
  1. # wget http://sourceforge.net/projects/xtables-addons/files/Xtables-addons/1.47/xtables-addons-1.47.tar.xz/download
  2. # tar xf xtables-addons-1.47.tar.xz
  3. # cd xtables-addons-1.47
  4. # ./configure
  5. # make
  6. # make install

假如在./configure时遇到错误, configure: error: Package requirements (xtables >= 1.4.5) were not met: No package 'xtables' found
  1. checking for a BSD-compatible install... /usr/bin/install -c
  2. checking whether build environment is sane... yes
  3. checking for a thread-safe mkdir -p... /bin/mkdir -p
  4. checking for gawk... gawk
  5. checking whether make sets $(MAKE)... yes
  6. checking whether make supports nested variables... yes
  7. checking for gcc... gcc
  8. checking whether the C compiler works... yes
  9. checking for C compiler default output file name... a.out
  10. checking for suffix of executables...
  11. checking whether we are cross compiling... no
  12. checking for suffix of object files... o
  13. checking whether we are using the GNU C compiler... yes
  14. checking whether gcc accepts -g... yes
  15. checking for gcc option to accept ISO C89... none needed
  16. checking for style of include used by make... GNU
  17. checking dependency style of gcc... gcc3
  18. checking whether gcc and cc understand -c and -o together... yes
  19. checking for ar... ar
  20. checking the archiver (ar) interface... ar
  21. checking build system type... x86_64-unknown-linux-gnu
  22. checking host system type... x86_64-unknown-linux-gnu
  23. checking how to print strings... printf
  24. checking for a sed that does not truncate output... /bin/sed
  25. checking for grep that handles long lines and -e... /bin/grep
  26. checking for egrep... /bin/grep -E
  27. checking for fgrep... /bin/grep -F
  28. checking for ld used by gcc... /usr/bin/ld
  29. checking if the linker (/usr/bin/ld) is GNU ld... yes
  30. checking for BSD- or MS-compatible name lister (nm)... /usr/bin/nm -B
  31. checking the name lister (/usr/bin/nm -B) interface... BSD nm
  32. checking whether ln -s works... yes
  33. checking the maximum length of command line arguments... 1966080
  34. checking whether the shell understands some XSI constructs... yes
  35. checking whether the shell understands "+="... yes
  36. checking how to convert x86_64-unknown-linux-gnu file names to x86_64-unknown-linux-gnu format... func_convert_file_noop
  37. checking how to convert x86_64-unknown-linux-gnu file names to toolchain format... func_convert_file_noop
  38. checking for /usr/bin/ld option to reload object files... -r
  39. checking for objdump... objdump
  40. checking how to recognize dependent libraries... pass_all
  41. checking for dlltool... no
  42. checking how to associate runtime and link libraries... printf %s\n
  43. checking for archiver @FILE support... @
  44. checking for strip... strip
  45. checking for ranlib... ranlib
  46. checking command to parse /usr/bin/nm -B output from gcc object... ok
  47. checking for sysroot... no
  48. checking for mt... no
  49. checking if : is a manifest tool... no
  50. checking how to run the C preprocessor... gcc -E
  51. checking for ANSI C header files... yes
  52. checking for sys/types.h... yes
  53. checking for sys/stat.h... yes
  54. checking for stdlib.h... yes
  55. checking for string.h... yes
  56. checking for memory.h... yes
  57. checking for strings.h... yes
  58. checking for inttypes.h... yes
  59. checking for stdint.h... yes
  60. checking for unistd.h... yes
  61. checking for dlfcn.h... yes
  62. checking for objdir... .libs
  63. checking if gcc supports -fno-rtti -fno-exceptions... no
  64. checking for gcc option to produce PIC... -fPIC -DPIC
  65. checking if gcc PIC flag -fPIC -DPIC works... yes
  66. checking if gcc static flag -static works... no
  67. checking if gcc supports -c -o file.o... yes
  68. checking if gcc supports -c -o file.o... (cached) yes
  69. checking whether the gcc linker (/usr/bin/ld -m elf_x86_64) supports shared libraries... yes
  70. checking whether -lc should be explicitly linked in... no
  71. checking dynamic linker characteristics... GNU/Linux ld.so
  72. checking how to hardcode library paths into programs... immediate
  73. checking whether stripping libraries is possible... yes
  74. checking if libtool supports shared libraries... yes
  75. checking whether to build shared libraries... yes
  76. checking whether to build static libraries... no
  77. checking linux/netfilter/x_tables.h usability... yes
  78. checking linux/netfilter/x_tables.h presence... yes
  79. checking for linux/netfilter/x_tables.h... yes
  80. checking for pkg-config... /usr/bin/pkg-config
  81. checking pkg-config is at least version 0.9.0... yes
  82. checking for libxtables... no
  83. configure: error: Package requirements (xtables >= 1.4.5) were not met:
  84.  
  85. No package 'xtables' found
  86.  
  87. Consider adjusting the PKG_CONFIG_PATH environment variable if you
  88. installed software in a non-standard prefix.
  89.  
  90. Alternatively, you may set the environment variables libxtables_CFLAGS
  91. and libxtables_LIBS to avoid the need to call pkg-config.
  92. See the pkg-config man page for more details.
请安装iptables开发包iptables-devel:
  1. # yum -y install iptables-devel

第四步,下载和安装GeoIP模块,你可以到 http://geolite.maxmind.com/download/geoip/database/下载CSV版本,也可以使用xtables-addons目录下geoip目录下的脚本xt_geoip_dl来下载:
  1. # cd geoip/
  2. # ./xt_geoip_dl

将会下载GeoIPv6.csv.gz和GeoIPCountryCSV.zip,并解压缩,得到ip库文件GeoIPv6.csv和GeoIPCountryWhois.csv,接下来就是使用xt_geoip_build编译数据库:
  1. # mkdir -p /usr/share/xt_geoip/ #创建数据库文件默认存放位置
  2. # ./xt_geoip_build -D /usr/share/xt_geoip *.csv #编译数据库文件

完成后,将会生成两个目录BE和LE,目录下保存的文件分别有.iv6和.iv4。

第五步,添加过滤规则,屏蔽中国地区ip:
  1. # iptables -I INPUT -m geoip --src-cc CN -j DROP #注意,这将屏蔽所有端口访问
  2. # iptables -I INPUT -p tcp -m tcp --dport 80 -m geoip --src-cc CN -j DROP #只屏蔽80端口访问

此时,中国地区已经无法访问网站了,可以保存了:service iptables save 
 类似资料: