-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathopnsense.py
65 lines (55 loc) · 1.95 KB
/
opnsense.py
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import requests
from SecAutoBan import SecAutoBan
def block_ip(ip):
if check_exist_ip(ip):
return
post_data = {
"address": ip
}
url = opnsense_config["url"] + "/api/firewall/alias_util/add/" + opnsense_config["alias_name"]
r = requests.post(url, auth=(opnsense_config["api_key"], opnsense_config["api_secret"]), json=post_data, timeout=60)
if r.status_code != 200:
customize_print("[-] 添加封禁失败")
return
if r.json()["status"] != "done":
customize_print("[-] 添加封禁失败")
def unblock_ip(ip):
url = opnsense_config["url"] + "/api/firewall/alias_util/delete/" + opnsense_config["alias_name"]
post_data = {
"address": ip
}
r = requests.post(url, auth=(opnsense_config["api_key"], opnsense_config["api_secret"]), json=post_data, timeout=60)
if r.status_code != 200:
customize_print("[-] 解除封禁失败")
return
if r.json()["status"] != "done":
customize_print("[-] 解除封禁失败")
def get_all_block_ip() -> list:
ip_list = []
url = opnsense_config["url"] + "/api/firewall/alias_util/list/" + opnsense_config["alias_name"]
r = requests.get(url, auth=(opnsense_config["api_key"], opnsense_config["api_secret"]), timeout=60)
if r.status_code != 200:
customize_print("[-] 获取全量IP失败")
return
for i in r.json()["rows"]:
ip_list.append(i["ip"])
return ip_list
def check_exist_ip(ip) -> bool:
return ip in get_all_block_ip()
if __name__ == "__main__":
opnsense_config = {
"url": "http://xxx.xxx.xxx.xxx",
"api_key": "xxx",
"api_secret": "xxx",
"alias_name": "sec_auto_ban"
}
sec_auto_ban = SecAutoBan(
server_ip="127.0.0.1",
server_port=80,
sk="sk-*****",
client_type="block",
block_ip = block_ip,
unblock_ip = unblock_ip,
get_all_block_ip= get_all_block_ip
)
sec_auto_ban.run()