Skip to content

Commit 8de97ff

Browse files
committed
add mcp get_attack_event tool
1 parent b744948 commit 8de97ff

File tree

6 files changed

+54
-6
lines changed

6 files changed

+54
-6
lines changed

mcp_server/tools/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import os
44
import importlib
55
import logging
6+
67
class ABCTool(ABC):
78
@classmethod
89
@abstractmethod

mcp_server/tools/create_http_application.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ async def run(self, arguments:dict) -> str:
3636
@classmethod
3737
def tool(self) -> Tool:
3838
return Tool(
39-
name="create_http_application",
39+
name="waf_ create_http_application",
4040
description="在雷池 WAF 上创建一个站点应用",
4141
inputSchema=self.model_json_schema()
4242
)

mcp_server/tools/create_ip_custom_rule.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ async def run(self, arguments:dict) -> str:
4949
@classmethod
5050
def tool(self) -> Tool:
5151
return Tool(
52-
name="create_ip_custom_rule",
53-
description="在雷池 WAF 上创建一个 ip 的自定义黑名单或者自定义白名单",
52+
name="waf_create_ip_custom_rule",
53+
description="以 客户端 IP 地址为条件,在雷池 WAF 上创建一个黑/白名单",
5454
inputSchema=self.model_json_schema()
5555
)

mcp_server/tools/create_path_custom_rule.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ async def run(self, arguments:dict) -> str:
4747
@classmethod
4848
def tool(self) -> Tool:
4949
return Tool(
50-
name="create_path_custom_rule",
51-
description="在雷池 WAF 上创建一个 url 路径的自定义黑名单或者自定义白名单",
50+
name="waf_create_path_custom_rule",
51+
description="以 URL Path 为条件,在雷池 WAF 上创建一个黑/白名单",
5252
inputSchema=self.model_json_schema()
5353
)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from pydantic import BaseModel, Field
2+
from utils.request import get_slce_api
3+
from tools import Tool, ABCTool, tools
4+
from urllib.parse import urlparse
5+
@tools.register
6+
class CreateHttpApplication(BaseModel, ABCTool):
7+
ip: str = Field(default="", description="the attacker's client IP address")
8+
size: int = Field(default=10, min=1, max=100, description="the number of results to return")
9+
start: str = Field(default="", description="start time, millisecond timestamp")
10+
end: str = Field(default="", description="end time, millisecond timestamp")
11+
12+
@classmethod
13+
async def run(self, arguments:dict) -> str:
14+
try:
15+
req = CreateHttpApplication.model_validate(arguments)
16+
parsed_upstream = urlparse(req.upstream)
17+
if parsed_upstream.scheme not in ["https", "http"]:
18+
return "invalid upstream scheme"
19+
20+
if parsed_upstream.hostname == "":
21+
return "invalid upstream host"
22+
except Exception as e:
23+
return str(e)
24+
25+
return await get_slce_api(f"api/open/events?page=1&page_size={req.size}&ip={req.ip}&start={req.start}&end={req.end}")
26+
27+
@classmethod
28+
def tool(self) -> Tool:
29+
return Tool(
30+
name="waf_get_attack_events",
31+
description="获取雷池 WAF 所记录的攻击事件",
32+
inputSchema=self.model_json_schema()
33+
)

mcp_server/utils/request.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,21 @@ def check_slce_response(response: httpx.Response) -> str:
1515

1616
return "success"
1717

18-
async def post_slce_api(path: str,req_body: dict) -> str:
18+
async def get_slce_api(path: str) -> str:
19+
if not path.startswith("/"):
20+
path = f"/{path}"
21+
22+
try:
23+
async with AsyncClient(verify=False) as client:
24+
response = await client.get(f"{GLOBAL_CONFIG.SAFELINE_ADDRESS}{path}", json=req_body, headers={
25+
"X-SLCE-API-TOKEN": f"{GLOBAL_CONFIG.SAFELINE_API_TOKEN}"
26+
})
27+
return check_slce_response(response)
28+
except Exception as e:
29+
return str(e)
30+
31+
32+
async def post_slce_api(path: str, req_body: dict) -> str:
1933
if not path.startswith("/"):
2034
path = f"/{path}"
2135

0 commit comments

Comments
 (0)