Files
37duAutomation/tests/bankcomm_probe/grab_js_snippets.py
T

81 lines
2.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
"""在浏览器页面上下文里同步抓 bundle js,抽取国密信封/鉴权相关代码片段。
(requests 直连因服务端 TLS 不支持安全重协商失败,改由浏览器同源 XHR 获取。)
"""
import json
import os
import sys
try:
sys.stdout.reconfigure(encoding='utf-8', errors='replace')
except Exception:
pass
from DrissionPage import ChromiumPage
REPO = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
OUT = os.path.join(REPO, 'logs', 'bankcomm_probe', 'snippets.json')
page = ChromiumPage('127.0.0.1:9333')
FILES = [
'/mm/js/bundle.app.d353b068.js',
'/mm/js/bundle.chunk-libs.84ed967f.js',
'/mm/js/bundle.1.b8b6b54d.js',
'/mm/js/bundle.4.8e0b362d.js',
'/mm/js/bundle.8.72ee03e6.js',
'/mm/js/bundle.18.8c325f96.js',
'/mm/js/bundle.21.34ed3f98.js',
'/mm/js/bundle.3.6b64121c.js',
'/mm/js/bundle.2.5f2084a9.js',
]
KWS = ['session/create', 'DigitalEnvelope', 'plainText', 'SecurityType', 'Token-Id',
'interceptors', 'pub/login', 'MSNGTQ/0002', 'MSNGTO/0012']
JS = r"""
var files = %s;
var kws = %s;
var result = {};
for (var fi = 0; fi < files.length; fi++) {
var f = files[fi];
var text = '';
try {
var xhr = new XMLHttpRequest();
xhr.open('GET', f, false);
xhr.send(null);
text = xhr.responseText || '';
} catch (e) {
result[f] = {error: String(e)};
continue;
}
var hits = {};
for (var ki = 0; ki < kws.length; ki++) {
var kw = kws[ki];
var arr = [];
var from = 0;
while (arr.length < 4) {
var i = text.indexOf(kw, from);
if (i < 0) break;
arr.push({at: i, ctx: text.slice(Math.max(0, i - 900), i + 1500)});
from = i + kw.length;
}
if (arr.length) hits[kw] = arr;
}
result[f] = {size: text.length, hits: hits};
}
return JSON.stringify(result);
""" % (json.dumps(FILES), json.dumps(KWS))
raw = page.run_js(JS)
data = json.loads(raw) if raw else {}
with open(OUT, 'w', encoding='utf-8') as f:
json.dump(data, f, ensure_ascii=False, indent=2)
for f, info in data.items():
if 'error' in info:
print(f'{f}: ERROR {info["error"]}')
continue
print(f'{f}: {info["size"] / 1024:.0f}KB 命中关键字 {list((info.get("hits") or {}).keys())}')
print('->', OUT)