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

157 lines
5.0 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 -*-
"""人工登录 + 脚本点查询,捕获页面真实的响应解密调用(方法名 / 入参 / 结果)。
流程:打开浏览器(固定端口 9333,xq0076 profile)→ 等人工登录 → 进交易流水页 →
注入解密 hook → 脚本点「查询」→ 收集 hook 结果并落盘。
"""
import json
import os
import sys
import time
try:
sys.stdout.reconfigure(encoding='utf-8', errors='replace')
except Exception:
pass
from DrissionPage import ChromiumPage, ChromiumOptions
REPO = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
OUT = os.path.join(REPO, 'logs', 'bankcomm_probe', 'decrypt_capture.json')
PROFILE = os.path.join(os.environ.get('LOCALAPPDATA'), '37duAutomation', 'browser_sessions', 'xq0076')
SITE = 'https://merchantapp.bankcomm.com/mm/'
PORT = 9333
HOOK = r"""
window.__d = [];
function hookDec(obj, path) {
if (!obj || (typeof obj !== 'object' && typeof obj !== 'function')) return;
var names = [];
try {
var cur = obj, d = 0;
while (cur && d < 3) {
Object.getOwnPropertyNames(cur).forEach(function (n) { if (names.indexOf(n) < 0) names.push(n); });
cur = Object.getPrototypeOf(cur); d++;
}
} catch (e) {}
names.forEach(function (n) {
if (!/decrypt|Decrypt|unseal|decodeResp|parseResp|decodeData/i.test(n)) return;
var f;
try { f = obj[n]; } catch (e) { return; }
if (typeof f !== 'function') return;
try {
obj[n] = function () {
var r = f.apply(this, arguments);
try {
var args = [];
for (var i = 0; i < arguments.length; i++) {
var a = arguments[i];
args.push((a && typeof a === 'object') ? JSON.stringify(a).slice(0, 200) : String(a).slice(0, 200));
}
window.__d.push({ t: path + '.' + n, a: args, r: String(r).slice(0, 260) });
} catch (e) {}
return r;
};
} catch (e) {}
});
}
try {
Object.keys(window).forEach(function (k) {
try {
var v = window[k];
if (v && typeof v.getSessionData === 'function') hookDec(v, 'win/' + k);
} catch (e) {}
});
} catch (e) {}
try { hookDec(window.KeyouCryptography, 'Keyou'); } catch (e) {}
try { hookDec(window.KeyouCryptography && window.KeyouCryptography.algorithm, 'Keyou.alg'); } catch (e) {}
try { hookDec(window.CryptoJS, 'CryptoJS'); } catch (e) {}
// 记录 jQuery ajax 的 success 回调(页面业务解密通常在 success 里)
try {
var $ = window.jQuery || window.$;
if ($ && $.ajax && !$.__hooked) {
$.__hooked = true;
var _ajax = $.ajax;
$.ajax = function (opt) {
try {
if (opt && typeof opt.success === 'function') {
var s = opt.success;
opt.success = function (data) {
try { window.__d.push({ t: 'ajax.success', a: [String(data).slice(0, 300)], r: '' }); } catch (e) {}
return s.apply(this, arguments);
};
}
} catch (e) {}
return _ajax.apply(this, arguments);
};
}
} catch (e) {}
return 'ok';
"""
def log(msg):
print(f'[{time.strftime("%H:%M:%S")}] {msg}', flush=True)
def main():
co = ChromiumOptions()
try:
co.set_local_port(PORT)
except Exception:
pass
co.set_user_data_path(PROFILE)
co.set_argument('--start-maximized')
page = ChromiumPage(co)
page.get(SITE + '#/login')
log('浏览器已打开,请在窗口里完成登录(脚本最多等 5 分钟)')
deadline = time.time() + 300
while time.time() < deadline:
try:
if 'login' not in page.url.lower():
break
except Exception:
pass
time.sleep(2)
log(f'当前 URL:{page.url}')
if 'login' in page.url.lower():
log('等待登录超时')
return 1
log('已登录,进入交易流水页…')
page.get(SITE + '#/MENU30060101')
time.sleep(9)
log(f'页面:{page.url}')
log(page.run_js(HOOK))
# 点页面自己的「查询」
clicked = False
for b in page.eles('tag:button'):
try:
if b.states.is_displayed and ''.join((b.text or '').split()) == '查询':
b.click()
clicked = True
break
except Exception:
continue
log(f'点击查询:{clicked}')
time.sleep(12)
raw = page.run_js('return JSON.stringify(window.__d || [])')
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)
log(f'捕获 {len(data)} 条解密调用 -> {OUT}')
for it in data[:10]:
print('-' * 80, flush=True)
print(it.get('t'), flush=True)
for i, a in enumerate(it.get('a') or []):
print(f' arg{i}: {a[:240]}', flush=True)
print(' ret:', (it.get('r') or '')[:280], flush=True)
return 0
if __name__ == '__main__':
sys.exit(main())