iami233
iami233
文章175
标签37
分类4

文章分类

文章归档

Cloudflare批量拉黑IP脚本

Cloudflare批量拉黑IP脚本

由于最近恶意攻击变多,需要批量拉黑大量IP,烦不胜烦所以简单写了个脚本进行自动化拉黑。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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)
本文作者:iami233
本文链接:https://5ime.cn/cloudflare-block-ip.html
版权声明:本文采用 CC BY-NC-SA 3.0 CN 协议进行许可