Table of Contents
Compile and install:
1. Dependency environment:
2. Compile and install ipset (linux kernel source code (version >= 2.6.32) )
Automatic IP address disabling
Home Operation and Maintenance Linux Operation and Maintenance Case study on how to block malicious IP addresses in batches to prevent attacks under Linux

Case study on how to block malicious IP addresses in batches to prevent attacks under Linux

Jun 07, 2017 am 10:28 AM

In many cases, you may need to block IP addresses under Linux. For example, as an end user, you may want to be protected from spyware or IP tracking. If you are a system administrator, you may want to ban spam IP addresses from accessing your corporate mail server. Or you want to ban certain countries from accessing your web service for some reason. In many cases, however, your IP address blocking list can quickly grow to tens of thousands of IPs. How to deal with this?

Solution: ipset + iblocklist2ipset

Installation:

The simplest method is to install yum, but the version of this method is relatively low and lacks some used module parameters, so it is not recommended;

yum install ipset -y
Copy after login

Compile and install:

1. Dependency environment:

yum install libmnl libmnl-devel kernel-devel libtool-devel -y
Copy after login

(Installation method of new version: git pull git://git.netfilter.org/libmnl.git run ./autogen.sh)

(Note: If only libmnl is installed, the following error will appear:

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.
)
Copy after login

may prompt that /lib/modules/2.6.32- cannot be found during compilation) 431.el6.x86_64/source

After investigation, it was found that this soft link/lib/modules/2.6.32-431.el6.x86_64/build -->/usr/src/kernels/2.6.32- 431.el6.x86_64 does not exist

Solution: Re-establish the soft connection

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

Error when running ./autogen.sh:

Cannot find /usr/share /libtool/

Solution: Install the libtool-devel tool package yum install libtool-devel

2. Compile and install 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
Copy after login

Note: Different linux kernels use different versions of source code packages

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

Compile and install:

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
Copy after login

Commonly used commands:

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
Copy after login

Automatic IP address disabling

Now you should see the power of maintaining IP blacklists. A tedious and time-consuming task. In fact, there are many free or paid services that can do this for you. As an added bonus, let's look at how to automatically add IP blacklists to the IP set. ##First let’s get a free blacklist from iblocklist.com

Next I’m going to use an open source Python tool called iblocklist2ipset to convert the blacklist into an IP set.

First, you need to install pip

Use the following command to install iblocklist2ipset.

$ pip install iblocklist2ipset
Copy after login

On some distributions such as Fedora, you may need to run:

$ python-pip install iblocklist2ipset
Copy after login

Now go to iblocklist.com and grab the URL of any P2P list (such as the "level1" list).

Download and unzip it, then save it as a txt file, for example, called pythontab.txt. Because iblocklist2ipset only supports url to obtain the list, put pythontab.txt in any directory of your website. For example: ipset directory

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

After running the above command, you will get a file named pythontab.txt. If you view its contents, you will see something like this:

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
Copy after login

You can load this file with the following ipset command:

$ ipset restore -f pythontab.txt
Copy after login

You can now view the automatically created IP set:

$ ipset list pythontab
Copy after login

This saves you the trouble of manual management.

Note that the version installed using yum under centos is not the latest version. It may not support the -f parameter and import the blacklist file, so it is recommended to use the source code package to install the latest version

The above is the detailed content of Case study on how to block malicious IP addresses in batches to prevent attacks under Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What computer configuration is required for vscode What computer configuration is required for vscode Apr 15, 2025 pm 09:48 PM

VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

Linux Architecture: Unveiling the 5 Basic Components Linux Architecture: Unveiling the 5 Basic Components Apr 20, 2025 am 12:04 AM

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

vscode terminal usage tutorial vscode terminal usage tutorial Apr 15, 2025 pm 10:09 PM

vscode built-in terminal is a development tool that allows running commands and scripts within the editor to simplify the development process. How to use vscode terminal: Open the terminal with the shortcut key (Ctrl/Cmd). Enter a command or run the script. Use hotkeys (such as Ctrl L to clear the terminal). Change the working directory (such as the cd command). Advanced features include debug mode, automatic code snippet completion, and interactive command history.

How to check the warehouse address of git How to check the warehouse address of git Apr 17, 2025 pm 01:54 PM

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

Where to write code in vscode Where to write code in vscode Apr 15, 2025 pm 09:54 PM

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

How to run java code in notepad How to run java code in notepad Apr 16, 2025 pm 07:39 PM

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

What is the main purpose of Linux? What is the main purpose of Linux? Apr 16, 2025 am 12:19 AM

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

How to run sublime after writing the code How to run sublime after writing the code Apr 16, 2025 am 08:51 AM

There are six ways to run code in Sublime: through hotkeys, menus, build systems, command lines, set default build systems, and custom build commands, and run individual files/projects by right-clicking on projects/files. The build system availability depends on the installation of Sublime Text.

See all articles