Cloudflare batch block IP script

Original link: https://5ime.cn/cloudflare-block-ip.html

Due to the recent increase in malicious attacks, it is very annoying to block a large number of IPs in batches, so I simply wrote a script to automatically block them.

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
twenty one
twenty two
twenty three
twenty four
25
 import json
import requests
from datetime import date

file = open ( "ip.txt" , "r" )
email = "your cloudflare mail"
apikey = "your cloudflare apikey"
# zoneid = "your cloudflare zone id" # 填写需要拉黑的站点区域ID
# url = "https://api.cloudflare.com/client/v4/zones/"+ zoneid +"/firewall/access_rules/rules" # 单个站点拉黑
url = "https://api.cloudflare.com/client/v4/user/firewall/access_rules/rules" # 所有站点拉黑
mode = "block" # 拉黑类型: challenge, block, whitelist, js_challenge

for line in file:
ip = line.strip()
try :
r = requests.post(url,
headers = { 'X-Auth-Email' : email, 'X-Auth-Key' : apikey, 'Content-Type' : 'application/json' },
json = { "mode" : mode, "configuration" : { "target" : "ip" , "value" : ip}, "notes" : date.today().strftime( "%Y-%m-%d" )})
r = json.loads(r.text)
if r[ "success" ]:
print (ip, "添加成功" )
else :
print (ip, "添加失败" , r[ "errors" ][ 0 ][ "message" ])
except Exception as e:
print (ip, "添加失败" , e)

This article is reprinted from: https://5ime.cn/cloudflare-block-ip.html
This site is for inclusion only, and the copyright belongs to the original author.

Leave a Comment

Your email address will not be published.