-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.py
96 lines (71 loc) · 3.63 KB
/
proxy.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import re
from loguru import logger
from mitmproxy import http
import configmanager\
# 目标网站
target_url = "yktyd.wbu.edu.cn"
# 目标URL特征
target_url_pattern = re.compile(r'http://yktyd\.wbu\.edu\.cn/wechat/home/index\.html\?.*')
@logger.catch
def update_config(jsessionid_value, url_value):
try:
config = configmanager.ConfigManager.load_config()
# 更新 JSESSIONID 值
config['WBUPower']['Cookies']['JSESSIONID'] = jsessionid_value
config['WBUPower']['Url'] = url_value
# 写回配置文件
configmanager.ConfigManager.save_config(config)
except NameError:
logger.error("请检查配置文件,确认相关信息是否正确")
@logger.catch
def request(flow: http.HTTPFlow) -> None:
# 检查是否为目标网站
if target_url in flow.request.pretty_host:
# 获取请求的URL
request_url = flow.request.url
# 检查请求的URL是否符合特定特征
if target_url_pattern.search(request_url):
# 提取并打印符合条件的URL
logger.info(f"请求的原始URL: {request_url}")
if 'token=' in flow.request.url:
# new_token = "newToken"
# new_json = "newJson"
# 找到 token json 的起始位置
start_index_token = flow.request.url.find('token=') + len('token=')
start_index_json = flow.request.url.find('json=') + len('json=')
# 找到 token json 值的结束位置
end_index_token = flow.request.url.find('&', start_index_token) if '&' in flow.request.url[
start_index_token:] else None
end_index_json = flow.request.url.find('&', start_index_json) if '&' in flow.request.url[
start_index_json:] else None
# 提取原始 token 的值
old_token = flow.request.url[start_index_token:end_index_token]
logger.info(f"提取的token: {old_token}")
# 提取原始 json 的值
old_json = flow.request.url[start_index_json:end_index_json]
logger.info(f"提取的json: {old_json}")
# 使用新的 token 替换原始 token
# modified_url_token = flow.request.url.replace(old_token, new_token)
# 使用新的 json 替换原始 json
# modified_url_json = modified_url_token.replace(old_json, new_json)
# 伪造请求
# flow.request.url = modified_url_json
# logger.info(f"劫持后的URL: {flow.request.url}")
else:
# 如果 URL 中不存在 token,返回原始 URL
logger.error("没有找到token")
# 获取 Cookies
cookies = flow.request.headers.get("Cookie", "")
# 在 Cookies 中查找 JSESSIONID 的值
jsessionid_match = re.search(r'JSESSIONID=(\w+)', cookies)
if jsessionid_match:
jsessionid_value = jsessionid_match.group(1)
# 输出到控制台
logger.info(f"获取到的 JSESSIONID: {jsessionid_value}")
# 更新配置文件
update_config(jsessionid_value, request_url)
# 启动 mitmproxy
if __name__ == "__main__":
from mitmproxy.tools.main import mitmdump
logger.info("代理已开启,端口8080")
mitmdump(["-s", __file__])