This commit is contained in:
cilame 2021-10-17 23:26:48 +08:00
parent 7c585a3a32
commit 2d7172434a
3 changed files with 43 additions and 28 deletions

View File

@ -10,45 +10,39 @@ function sendCommand(method, params, source, chainfun){
chrome.debugger.sendCommand(source, method, params, function(result){
if (chrome.runtime.lastError) {
console.error('chrome.runtime.lastError', chrome.runtime.lastError)
} else {
if (chainfun){
chainfun(result)
}
}
} else { if (chainfun){ chainfun(result) } }
});
}
function fillresponse(params, source, body){
sendCommand("Fetch.fulfillRequest", {
requestId: params.requestId, responseCode: params.responseStatusCode, responseHeaders: params.responseHeaders,
body: body, // body 只能传 base64(指定代码)
}, source);
}
chrome.debugger.onEvent.addListener(function (source, method, params){
switch(method){
case "Fetch.requestPaused":
var itheaders = params.responseHeaders;
if (itheaders.find(function(v){return v.name == "Location"})) {
sendCommand("Fetch.continueRequest", { requestId: params.requestId, url: itheaders.value }, source);
break;
}
break; }
if ((params.responseStatusCode || params.responseErrorReason)) {
if (params.responseErrorReason) {
sendCommand("Fetch.failRequest", { requestId: params.requestId, errorReason: params.responseErrorReason }, source);
break;
}
break; }
sendCommand("Fetch.getResponseBody", { requestId: params.requestId }, source, function(result){
if (result.body !== undefined){
// 收到的是 base64 的代码base64 解一下就是原始代码,对这个代码处理一下后续再用 base64 包一层再传入 body
var rescode = atob(result.body)
var resourceType = params.resourceType // 通过这里的类型对获取到的 body 信息进行处理
console.log(rescode.length, Object.keys(result), rescode.slice(0,100))
}
sendCommand("Fetch.fulfillRequest", {
requestId: params.requestId, responseCode: params.responseStatusCode, responseHeaders: params.responseHeaders,
body: result.body, // body 只能传 base64(指定代码)
}, source);
if (result.body !== undefined){ // 收到的 result.body 是 base64(代码) 的代码,使用时需要解码一下
chrome.storage.local.get(["config-fetch_hook"], function (res) {
try{ fillresponse.bind(null, params, source)(btoa(eval((res["config-fetch_hook"]||'')+';fetch_hook')(atob(result.body), params.resourceType, params.request.url))) }
catch(e){ fillresponse.bind(null, params, source)(result.body) }
})
return }
fillresponse.bind(null, params, source)(result.body) // body 只能传 base64(指定代码)
});
break;
}
break; }
}
})
chrome.debugger.onDetach.addListener(function(){
attached = false
})
chrome.debugger.onDetach.addListener(function(){ attached = false })
var attached = false
function AttachDebugger() {
if (attached){ return }
@ -61,8 +55,8 @@ function AttachDebugger() {
// Document, Stylesheet, Image, Media, Font, Script, TextTrack, XHR, Fetch, EventSource, WebSocket, Manifest, SignedExchange, Ping, CSPViolationReport, Preflight, Other
sendCommand("Network.enable", {}, currtab, function(){ sendCommand("Network.setCacheDisabled", {cacheDisabled: true}, currtab)} ) // 确保 Fetch.getResponseBody 一定能收到东西
sendCommand("Fetch.enable", { patterns: [
{urlPattern:"*",resourceType:"Script",requestStage:"Response"},
{urlPattern:"*",resourceType:"Document",requestStage:"Response"},
{urlPattern:"*",resourceType:"Script",requestStage:"Response"}, // 暂时先只 hook Script 类型的脚本
// {urlPattern:"*",resourceType:"Document",requestStage:"Response"},
] }, currtab);
});
}

View File

@ -15,8 +15,9 @@
<body>
<nav id="nav">
<ul>
<button class="act">dom对象 hook 配置</li>
<button>AST混淆解密</li>
<button class="act">dom对象 hook 配置</button>
<button>AST混淆解密</button>
<button>插件 hook 请求返回值并修改</button>
</ul>
</nav>
<div id="container">
@ -104,6 +105,16 @@
<label>code</label>
<textarea id='txt2' style="width: 100%; height: 2056px"></textarea>
</section>
<section class="tab">
<label>Fetch.enable</label>
<div>功能还没有做完,这里希望做成的功能是可以用插件的方式 hook 住代码,直接用插件的形式使用 ast 将原始的代码修改成期望的样子</div>
<div>能直接用插件的方式实现 ast 修改代码,用处还是非常大的。</div>
<HR>
<div>
请定义一个名字为 fetch_hook 函数,接受参数为 1.代码 2.类型 3.urlreturn 修改后的代码
</div>
<textarea id='fetch_hook' data-key="config-fetch_hook" style="width: 100%; height: 500px"></textarea>
</section>
</div>
<script src="options.js"></script>
<script src="tools/babel_asttool.js"></script>

View File

@ -138,4 +138,14 @@ document.getElementById('funcs').addEventListener("change", function(v){
}
})
chrome.storage.local.set(wt)
})
var fetch_hook = document.getElementById('fetch_hook');
chrome.storage.local.get([fetch_hook.dataset.key], function (result) {
fetch_hook.value = result[fetch_hook.dataset.key] || '';
})
fetch_hook.addEventListener("change", function(v){
chrome.storage.local.set({
[v.target.dataset.key]: v.target.value
})
})