快轉到主要內容

IPTABLES 封鎖 IP 黑名单

268 字1 分鐘00
作者Y Cheung
目錄

在服務器上增加 iptables chain#

1iptables -N black_ip_list
2iptables -I INPUT -p tcp -m multiport --dports 80,443 -j black_ip_list

新建 bash 文件 banip.sh#

在這個文件中,首先清空 black_ip_list 中的規則, 然後從 mitchellkrogza/nginx-ultimate-bad-bot-blocker 中獲取最新的IP BLACKLIST 列表,循環每一個IP地址,將其設為拒絕請求。

1#!/bin/bash
2iptables -F black_ip_list
3iptables -A black_ip_list -j RETURN
4
5wget https://raw.githubusercontent.com/mitchellkrogza/nginx-ultimate-bad-bot-blocker/master/_generator_lists/bad-ip-addresses.list -O old.blackip.txt 2>/dev/null
6
7for ip in `cat old.blackip.txt`; do 
8iptables -I black_ip_list 1 -s $ip -j REJECT --reject-with icmp-port-unreachable;
9done

將 banip.sh 文件權限變更為可執行#

1sudo chmod a+x banip.sh

執行 banip.sh 文件#

1sudo ./banip.sh

TIPS:#

  • 可以隔段時間執行一下該scripts,以獲取最新的block ip 列表
  • 解封ip
1iptables -D black_ip_list -s <ip> -j REJECT --reject-with icmp-port-unreachable
  • 查看所有的 iptables chain 列表,並帶序號
1iptables -L INPUT -n --line-numbers
  • 檢視當前 iptable 的所有規則
1iptables -nvL
  • 清空和刪除 chain
1iptables -F black_ip_list
2iptables -X black_ip_list

注意,如果沒有先清空而直接刪除chain會拋出錯誤

1iptables: Too many links.

延伸閱讀:

分享

相關文章

ubuntu vps上的文件权限配置

作者 Y Cheung238 字1 分鐘00

设置目录内所有文件及文件夹(包括子文件夹内)的权限为-rw-r–r–(644): chmod -R 644 * 设置目录内所有文件(不包括子文件夹内)的权限为 …

Fail2ban 配置 Nginx filter

作者 Y Cheung638 字2 分鐘00

簡要紀錄一下目前服務器上的 fail2ban 關於 nginx 的 filter 配置。 惡意爬蟲過濾器 自定義規則 簡要說明一下:

再談網站HTTPS升級

作者 Y Cheung2100 字5 分鐘00

Y.Cheung 曾於 2016 年時寫過一篇《 Centos 7 使用 certbot 安裝Let’s Encrypt SSL 證書 》,最近因爲幫忙升級了幾個站點,使用了一些免 …

忘記 Mysql ROOT 密碼的解決辦法

作者 Y Cheung174 字1 分鐘00

豬腦袋是記不住密碼的。 靠別人也是記不住密碼的。 環境 Ubuntu 操作步驟 修改MySQL配置 在 /etc/my.cnf 中的 [mysqld]段尾處加入以下語句:

ARP攻击啊!!!

作者 Y Cheung360 字1 分鐘00

题外话:(刚收到的圈子消息) 乐观的人看见问题后面的机会,悲观的人只看见机会后面的问题,机会是从来不会主动敲响你的门,无论你等待多少年。它也只会如一阵风一样拂面而过,需要你的反应能 …

↑