Skip to content

Commit 2405b64

Browse files
committed
更新
添加请求全量封禁IP方法
1 parent e99d633 commit 2405b64

4 files changed

Lines changed: 86 additions & 21 deletions

File tree

device/sdk/python/README.md

Lines changed: 52 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,55 @@ sec_auto_ban.run()
6161

6262
## 参数说明
6363

64-
| 参数 | 描述 | 是否需要填写 |
65-
| ---------------- | ------------------------- | --------------- |
66-
| server_ip | 核心模块回连IP | 需要 |
67-
| server_port | 核心模块回连端口 | 需要 |
68-
| sk | 设备页面生成的密钥 | 需要 |
69-
| client_type | 模块类型(`alarm`/`block`) | 需要 |
70-
| enable_cidr | 封禁模块是否开启 `Cidr` 封禁,若开启`block_ip()``unblock_ip()`参数将传入`Cidr` | 可选,默认为 `False` |
71-
| alarm_analysis | 告警分析函数 | `alarm`模块必填 |
72-
| block_ip | 封禁函数 | `block`模块必填 |
73-
| unblock_ip | 解禁函数 | `block`模块必填 |
74-
| get_all_block_ip | 获取设备中全部封禁IP函数 | `block`模块可选 |
64+
| 参数 | 描述 | 是否需要填写 |
65+
|------------------------|--------------------------------------------------------------|----------------|
66+
| server_ip | 核心模块回连IP | 需要 |
67+
| server_port | 核心模块回连端口 | 需要 |
68+
| sk | 设备页面生成的密钥 | 需要 |
69+
| client_type | 模块类型(`alarm`/`block`) | 需要 |
70+
| enable_cidr | 封禁模块是否开启 `Cidr` 封禁,若开启`block_ip()``unblock_ip()`参数将传入`Cidr` | 可选,默认为 `False` |
71+
| alarm_analysis | 告警分析函数 | `alarm`模块必填 |
72+
| block_ip | 封禁函数 | `block`模块必填 |
73+
| unblock_ip | 解禁函数 | `block`模块必填 |
74+
| get_all_block_ip | 获取设备中全部封禁IP函数 | `block`模块可选 |
75+
| login_success_callback | 登陆成功回调 | 可选 |
76+
77+
## SDK调用方法
78+
79+
### send_alarm()
80+
81+
告警设备向平台发送告警信息。
82+
83+
eg:
84+
85+
```python
86+
def alarm_analysis(ws_client):
87+
ws_client.send_alarm("攻击IP", "被攻击资产", "攻击方式")
88+
```
89+
90+
or:
91+
92+
```
93+
sec_auto_ban.send_alarm("攻击IP", "被攻击资产", "攻击方式")
94+
```
95+
96+
### send_notify()
97+
98+
向平台发送通知。
99+
100+
eg:
101+
102+
```python
103+
sec_auto_ban.send_notify("封禁失败", "xxx设备无法连接服务器")
104+
```
105+
106+
### send_sync()
107+
108+
封禁设备主动向平台请求全部封禁IP。常用于脚本第一次启动,需同步全量IP场景。
109+
110+
eg:
111+
112+
```python
113+
def login_success_callback():
114+
sec_auto_ban.send_sync()
115+
```

device/sdk/python/SecAutoBan/sec_auto_ban.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,28 @@
55
class SecAutoBan:
66
init = False
77

8-
def __init__(self, server_ip, server_port, sk, client_type, enable_cidr=False, alarm_analysis=None, block_ip=None, unblock_ip=None, get_all_block_ip=None):
8+
def __init__(self, server_ip, server_port, sk, client_type, enable_cidr=False, alarm_analysis=None, block_ip=None, unblock_ip=None, get_all_block_ip=None, login_success_callback=None):
99
self.client_type = client_type
1010
if client_type == "alarm":
1111
if alarm_analysis is None:
1212
util.print("[-] 初始化失败: 未实现告警函数")
1313
return
1414
self.alarm_analysis = alarm_analysis
15-
self.ws_client = WebSocketClient(server_ip, server_port, sk, client_type, enable_cidr, block_ip, unblock_ip, get_all_block_ip)
15+
self.ws_client = WebSocketClient(server_ip, server_port, sk, client_type, enable_cidr, block_ip, unblock_ip, get_all_block_ip, login_success_callback)
1616
self.init = True
1717

1818
def print(self, message):
1919
util.print(message)
2020

21+
def send_alarm(self, ip: str, attack_asset: str, attack_method: str):
22+
self.ws_client.send_alarm(ip, attack_asset, attack_method)
23+
24+
def send_notify(self, title: str, content: str):
25+
self.ws_client.send_notify(title, content)
26+
27+
def send_sync(self):
28+
self.ws_client.send_sync()
29+
2130
def run(self):
2231
if not self.init:
2332
return

device/sdk/python/SecAutoBan/websocket_client.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class WebSocketClient:
1313
send_alarm_ip_list = []
1414
pool = ThreadPool(processes=2)
1515

16-
def __init__(self, server_ip: str, server_port: int, sk: str, client_type: str, enable_cidr=False, block_ip=None, unblock_ip=None, get_all_block_ip=None):
16+
def __init__(self, server_ip: str, server_port: int, sk: str, client_type: str, enable_cidr=False, block_ip=None, unblock_ip=None, get_all_block_ip=None, login_success_callback=None):
1717
self.server_ip = server_ip
1818
self.server_port = server_port
1919
self.sk = sk
@@ -29,6 +29,7 @@ def __init__(self, server_ip: str, server_port: int, sk: str, client_type: str,
2929
self.unblock_ip = unblock_ip
3030
self.get_all_block_ip = get_all_block_ip
3131
self.client_type = client_type
32+
self.login_success_callback = login_success_callback
3233
self.ws = websocket.WebSocketApp(
3334
"ws://" + server_ip + ":" + str(server_port) + "/device",
3435
on_message=self.on_message,
@@ -60,6 +61,8 @@ def on_message(self, w, message):
6061
if message["method"] == "login":
6162
self.is_login = True
6263
util.print("[+] 登录成功,设备名称: " + message["data"]["deviceName"])
64+
if self.login_success_callback is not None:
65+
self.login_success_callback()
6366
if self.client_type == "block":
6467
if message["method"] == "blockCidr":
6568
util.print("[+] 封禁IP: " + message["data"]["cidr"])
@@ -124,9 +127,9 @@ def connect(self):
124127
return
125128
self.pool.apply_async(self.web_socket_d)
126129

127-
def send_alarm(self, ip: str, attackAsset: str, attackMethod: str):
128-
if self.client_type == "block":
129-
util.print("[-] 封禁模块无法发送告警数据")
130+
def send_alarm(self, ip: str, attack_asset: str, attack_method: str):
131+
if self.client_type != "alarm":
132+
util.print("[-] 非告警模块无法发送告警数据")
130133
return
131134
if not self.is_login:
132135
util.print("[-] 未登录成功,无法发送数据")
@@ -140,12 +143,12 @@ def send_alarm(self, ip: str, attackAsset: str, attackMethod: str):
140143
"method": "alarmIp",
141144
"data": {
142145
"ip": ip,
143-
"attackAsset": attackAsset,
144-
"attackMethod": attackMethod
146+
"attackAsset": attack_asset,
147+
"attackMethod": attack_method
145148
}
146149
}
147150
iv = util.random_bytes()
148-
util.print("[+] 发送告警IP: " + ip + "->" + attackAsset + "\t" + attackMethod)
151+
util.print("[+] 发送告警IP: " + ip + "->" + attack_asset + "\t" + attack_method)
149152
self.ws.send(iv + util.aes_cfb_encrypt(self.sk[3:].encode(), iv, json.dumps(send_data).encode()))
150153
def send_notify(self, title: str, content: str):
151154
if not self.is_login:
@@ -159,4 +162,16 @@ def send_notify(self, title: str, content: str):
159162
}
160163
}
161164
iv = util.random_bytes()
165+
self.ws.send(iv + util.aes_cfb_encrypt(self.sk[3:].encode(), iv, json.dumps(send_data).encode()))
166+
def send_sync(self):
167+
if self.client_type != "block":
168+
util.print("[-] 非封禁模块无法请求封禁IP")
169+
return
170+
if not self.is_login:
171+
util.print("[-] 未登录成功,无法发送数据")
172+
return
173+
send_data = {
174+
"method": "syncBlockIp"
175+
}
176+
iv = util.random_bytes()
162177
self.ws.send(iv + util.aes_cfb_encrypt(self.sk[3:].encode(), iv, json.dumps(send_data).encode()))

device/sdk/python/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setuptools.setup(
77
name="SecAutoBan",
8-
version="4.0.3",
8+
version="5.0.0",
99
author="SecReport",
1010
author_email="secaegis@outlook.com",
1111
description="SecAutoBan SDK",

0 commit comments

Comments
 (0)