Skip to content

Commit 1c86a9d

Browse files
committed
调整代码结构
1 parent 73d638c commit 1c86a9d

File tree

7 files changed

+82
-75
lines changed

7 files changed

+82
-75
lines changed

webhook/app.py

Lines changed: 1 addition & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,22 @@
1-
import datetime
21
import logging
3-
from datetime import datetime
42

53
from flask import Flask, request, redirect
64

75
from config import parse_config, get_config
8-
from executor.executor import dispatch_web_vuln, dispatch_service_vuln, dispatch_statistics
96
from executor.registry import init_plugin
10-
from model.vuln import Statistics, WebVuln, WebParam, WebParamPosition, WebRequest, WebResponse, ServiceVuln
7+
from views.views import process_web_vuln, process_statistics, process_host_vuln
118

129
app = Flask(__name__)
1310

14-
debug = True
1511
logging.basicConfig(format="[%(levelname)s] %(asctime)s %(name)s %(message)s",
1612
datefmt="%Y-%m-%d %H:%M:%S", level=logging.DEBUG)
1713

18-
logger = logging.getLogger("webhook")
19-
20-
21-
def process_web_vuln(instance, data):
22-
"""将 web 漏洞 json 转换为相关 model"""
23-
detail = data["detail"]
24-
p = detail["param"]
25-
if p:
26-
param = WebParam(key=p["key"], value=p["value"], position=WebParamPosition(p["position"]))
27-
else:
28-
param = None
29-
30-
request = []
31-
response = []
32-
extra = {}
33-
34-
for i in range(0, 10):
35-
req_key = f"request{i}" if i else "request"
36-
resp_key = f"response{i}" if i else "response"
37-
req = detail.get(req_key)
38-
resp = detail.get(resp_key)
39-
40-
if req == "" or resp == "":
41-
continue
42-
if req is None or resp is None:
43-
break
44-
request.append(WebRequest(raw=req))
45-
response.append(WebResponse(raw=resp))
46-
47-
# 其他的数据可能是自定义的,就单独拿出来
48-
not_extra_key = ["request", "response", "param", "payload", "url"]
49-
for k, v in detail.items():
50-
for item in not_extra_key:
51-
if item in k:
52-
break
53-
else:
54-
extra[k] = v
55-
56-
vuln = WebVuln(create_time=datetime.fromtimestamp(data["create_time"] / 1000), plugin=data["plugin"],
57-
vuln_class=data["vuln_class"],
58-
url=data["target"]["url"], param=param, request=request, response=response, extra=extra)
59-
dispatch_web_vuln(instance, vuln)
60-
61-
62-
def process_statistics(instance, data):
63-
"""将统计数据 json 转换为相关 json"""
64-
data.pop("type", None)
65-
s = Statistics(**data)
66-
dispatch_statistics(instance, s)
67-
68-
69-
def process_host_vuln(instance, data):
70-
"""将服务漏洞 json 转换为相关 json"""
71-
detail = data["detail"]
72-
extra = {}
73-
74-
not_extra_key = ["host", "port"]
75-
for k, v in detail.items():
76-
for item in not_extra_key:
77-
if item in k:
78-
break
79-
else:
80-
extra[k] = v
81-
82-
vuln = ServiceVuln(create_time=datetime.fromtimestamp(data["create_time"] / 1000), plugin=data["plugin"],
83-
vuln_class=data["vuln_class"], host=detail["host"], port=detail["port"],
84-
extra=extra)
85-
dispatch_service_vuln(instance, vuln)
86-
8714

8815
# 比如可以给一个界面管理当前的插件,看到插件的数据等?
8916
@app.route("/", methods=["GET"])
9017
def index():
9118
return redirect("https://xray.cool/xray/#/api/api")
9219

93-
9420
@app.route("/webhook", methods=["POST"])
9521
def webhook():
9622
token = get_config().server_config.token

webhook/executor/__init__.py

Whitespace-only changes.

webhook/model/__init__.py

Whitespace-only changes.

webhook/model/vuln.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
@dataclass
88
class Statistics:
99
"""统计数据"""
10+
raw_json: dict
1011
# 发现的 url 数量
1112
num_found_urls: int
1213
# 扫描完成的 url 数量
@@ -55,6 +56,7 @@ class WebResponse:
5556
@dataclass
5657
class WebVuln:
5758
"""web 漏洞"""
59+
raw_json: dict
5860
# 创建时间
5961
create_time: datetime
6062
# 这两个数据内部使用其实是 enum,要不要提供给社区?
@@ -78,6 +80,7 @@ class WebVuln:
7880
@dataclass
7981
class ServiceVuln:
8082
"""服务漏洞"""
83+
raw_json: dict
8184
# 同 web 漏洞
8285
create_time: datetime
8386
plugin: str

webhook/plugins/__init__.py

Whitespace-only changes.

webhook/views/__init__.py

Whitespace-only changes.

webhook/views/views.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import datetime
2+
from datetime import datetime
3+
4+
from executor.executor import dispatch_web_vuln, dispatch_service_vuln, dispatch_statistics
5+
from model.vuln import Statistics, WebVuln, WebParam, WebParamPosition, WebRequest, WebResponse, ServiceVuln
6+
7+
8+
def process_web_vuln(instance, data):
9+
"""将 web 漏洞 json 转换为相关 model"""
10+
detail = data["detail"]
11+
p = detail["param"]
12+
if p:
13+
param = WebParam(key=p["key"], value=p["value"], position=WebParamPosition(p["position"]))
14+
else:
15+
param = None
16+
17+
request = []
18+
response = []
19+
extra = {}
20+
21+
for i in range(0, 10):
22+
req_key = f"request{i}" if i else "request"
23+
resp_key = f"response{i}" if i else "response"
24+
req = detail.get(req_key)
25+
resp = detail.get(resp_key)
26+
27+
if req == "" or resp == "":
28+
continue
29+
if req is None or resp is None:
30+
break
31+
request.append(WebRequest(raw=req))
32+
response.append(WebResponse(raw=resp))
33+
34+
# 其他的数据可能是自定义的,就单独拿出来
35+
not_extra_key = ["request", "response", "param", "payload", "url"]
36+
for k, v in detail.items():
37+
for item in not_extra_key:
38+
if item in k:
39+
break
40+
else:
41+
extra[k] = v
42+
43+
vuln = WebVuln(create_time=datetime.fromtimestamp(data["create_time"] / 1000), plugin=data["plugin"],
44+
vuln_class=data["vuln_class"],
45+
url=data["target"]["url"], param=param, request=request, response=response, extra=extra,
46+
raw_json=data)
47+
dispatch_web_vuln(instance, vuln)
48+
49+
50+
def process_statistics(instance, data):
51+
"""将统计数据 json 转换为相关 json"""
52+
s = Statistics(num_found_urls=data["num_found_urls"],
53+
num_scanned_urls=data["num_scanned_urls"],
54+
num_sent_http_requests=data["num_sent_http_requests"],
55+
average_response_time=data["average_response_time"],
56+
ratio_failed_http_requests=data["ratio_failed_http_requests"],
57+
ratio_progress=data["ratio_progress"],
58+
raw_json=data)
59+
dispatch_statistics(instance, s)
60+
61+
62+
def process_host_vuln(instance, data):
63+
"""将服务漏洞 json 转换为相关 json"""
64+
detail = data["detail"]
65+
extra = {}
66+
67+
not_extra_key = ["host", "port"]
68+
for k, v in detail.items():
69+
for item in not_extra_key:
70+
if item in k:
71+
break
72+
else:
73+
extra[k] = v
74+
75+
vuln = ServiceVuln(create_time=datetime.fromtimestamp(data["create_time"] / 1000), plugin=data["plugin"],
76+
vuln_class=data["vuln_class"], host=detail["host"], port=detail["port"],
77+
extra=extra, raw_json=data)
78+
dispatch_service_vuln(instance, vuln)

0 commit comments

Comments
 (0)