136 lines
4.8 KiB
Python
136 lines
4.8 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""探测页面 webpack 运行时,尝试从模块缓存里捞出「请求封装模块」。
|
|
|
|
如果捞到(对象上有 post/get/request 方法),就能在页面上下文直接调它发请求:
|
|
国密加密、Authorization、响应解密全部由页面自己的代码完成,无需逆向算法。
|
|
"""
|
|
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', 'webpack_probe.json')
|
|
|
|
page = ChromiumPage('127.0.0.1:9333')
|
|
|
|
# ① 看 webpack 运行时挂在哪
|
|
ENV = r"""
|
|
var out = {globals: {}, probes: {}};
|
|
Object.keys(window).forEach(function (k) {
|
|
if (/webpack|chunk/i.test(k)) {
|
|
var v = window[k];
|
|
var t = typeof v;
|
|
out.globals[k] = t + (Array.isArray(v) ? '[len=' + v.length + ']' : '');
|
|
}
|
|
});
|
|
['webpackJsonp', '__webpack_require__', 'webpackChunkwebpack', 'webpackChunkapp'].forEach(function (n) {
|
|
try { out.probes[n] = typeof window[n]; } catch (e) { out.probes[n] = 'err'; }
|
|
});
|
|
return JSON.stringify(out);
|
|
"""
|
|
print('--- webpack 运行时 ---', flush=True)
|
|
print(page.run_js(ENV), flush=True)
|
|
|
|
# ② 尝试通过 webpackJsonp.push 注入一个模块以拿到 __webpack_require__
|
|
INJECT = r"""
|
|
function findReq() {
|
|
var wp = window.webpackJsonp;
|
|
if (wp && typeof wp.push === 'function') {
|
|
try {
|
|
wp.push([[987654], {
|
|
987654: function (module, exports, __webpack_require__) {
|
|
window.__probe_require__ = __webpack_require__;
|
|
}
|
|
}, ['chunk-vendors']]);
|
|
} catch (e) { return 'push-fail:' + e; }
|
|
if (window.__probe_require__) return 'ok-jsonp';
|
|
// 再试不带 executeModules
|
|
try {
|
|
wp.push([[987655], {
|
|
987655: function (module, exports, __webpack_require__) {
|
|
window.__probe_require__ = __webpack_require__;
|
|
}
|
|
}]);
|
|
} catch (e) { return 'push-fail2:' + e; }
|
|
if (window.__probe_require__) return 'ok-jsonp2';
|
|
return 'pushed-but-no-require';
|
|
}
|
|
// webpack5: self.webpackChunk* 数组 push
|
|
var names = Object.keys(window).filter(function (k) { return /^webpackChunk/.test(k); });
|
|
for (var i = 0; i < names.length; i++) {
|
|
var arr = window[names[i]];
|
|
if (arr && typeof arr.push === 'function') {
|
|
try {
|
|
arr.push([[987656], {
|
|
987656: function (module, exports, __webpack_require__) {
|
|
window.__probe_require__ = __webpack_require__;
|
|
}
|
|
}, function () {}]);
|
|
} catch (e) { /* ignore */ }
|
|
if (window.__probe_require__) return 'ok5:' + names[i];
|
|
}
|
|
}
|
|
return 'no-webpack-jsonp';
|
|
}
|
|
return findReq();
|
|
"""
|
|
print('--- 注入模块 ---', flush=True)
|
|
print(page.run_js(INJECT), flush=True)
|
|
|
|
# ③ 遍历模块缓存,找请求封装
|
|
SCAN = r"""
|
|
var req = window.__probe_require__;
|
|
if (!req) return JSON.stringify({error: 'no __webpack_require__'});
|
|
var cache = req.c || {};
|
|
var ids = Object.keys(cache);
|
|
var hits = [];
|
|
function describe(obj, path) {
|
|
var keys = [];
|
|
try { keys = Object.keys(obj).slice(0, 25); } catch (e) { return null; }
|
|
var methods = keys.filter(function (k) {
|
|
try { return typeof obj[k] === 'function'; } catch (e) { return false; }
|
|
});
|
|
var interesting = methods.filter(function (k) {
|
|
return /^(post|get|request|ajax|send|fetch|put|del|delete|http)$/i.test(k);
|
|
});
|
|
if (interesting.length) {
|
|
hits.push({ id: path, methods: methods.slice(0, 30), interesting: interesting });
|
|
}
|
|
// 看 default
|
|
try {
|
|
if (obj['default'] && typeof obj['default'] === 'object') {
|
|
var d = obj['default'];
|
|
var dk = Object.keys(d).slice(0, 25);
|
|
var dm = dk.filter(function (k) { try { return typeof d[k] === 'function'; } catch (e) { return false; } });
|
|
var di = dm.filter(function (k) { return /^(post|get|request|ajax|send|fetch|put|del|delete|http)$/i.test(k); });
|
|
if (di.length) hits.push({ id: path + '.default', methods: dm.slice(0, 30), interesting: di,
|
|
isAxios: !!(d.defaults && d.interceptors) });
|
|
}
|
|
} catch (e) {}
|
|
return null;
|
|
}
|
|
for (var i = 0; i < ids.length; i++) {
|
|
var exp = cache[ids[i]] && cache[ids[i]].exports;
|
|
if (!exp || (typeof exp !== 'object' && typeof exp !== 'function')) continue;
|
|
try { describe(exp, ids[i]); } catch (e) {}
|
|
}
|
|
return JSON.stringify({moduleCount: ids.length, hits: hits.slice(0, 60)});
|
|
"""
|
|
print('--- 模块缓存扫描 ---', flush=True)
|
|
raw = page.run_js(SCAN)
|
|
print(raw[:4000] if raw else 'null', flush=True)
|
|
|
|
try:
|
|
with open(OUT, 'w', encoding='utf-8') as f:
|
|
f.write(raw or '')
|
|
print('->', OUT)
|
|
except Exception as e:
|
|
print('写文件失败', e)
|