js經(jīng)驗(yàn)分享 JavaScript反調(diào)試技巧
在此之前,我一直都在研究JavaScript相關(guān)的反調(diào)試技巧。但是當(dāng)我在網(wǎng)上搜索相關(guān)資料時(shí),我發(fā)現(xiàn)網(wǎng)上并沒(méi)有多少關(guān)于這方面的文章,而且就算有也是非常不完整的那種。所以在這篇文章中,我打算跟大家總結(jié)一下關(guān)于JavaScript反調(diào)試技巧方面的內(nèi)容。值得一提的是,其中有些方法已經(jīng)被網(wǎng)絡(luò)犯罪分子廣泛應(yīng)用到惡意軟件之中了。
對(duì)于JavaScript來(lái)說(shuō),你只需要花一點(diǎn)時(shí)間進(jìn)行調(diào)試和分析,你就能夠了解到JavaScript代碼段的功能邏輯。而我們所要討論的內(nèi)容,可以給那些想要分析你JavaScript代碼的人增加一定的難度。不過(guò)我們的技術(shù)跟代碼混淆無(wú)關(guān),我們主要針對(duì)的是如何給代碼主動(dòng)調(diào)試增加困難。
本文所要介紹的技術(shù)方法大致如下:
1. 檢測(cè)未知的執(zhí)行環(huán)境(我們的代碼只想在瀏覽器中被執(zhí)行);
2. 檢測(cè)調(diào)試工具(例如DevTools);
3. 代碼完整性控制;
4. 流完整性控制;
5. 反模擬;
簡(jiǎn)而言之,如果我們檢測(cè)到了“不正?!钡那闆r,程序的運(yùn)行流程將會(huì)改變,并跳轉(zhuǎn)到偽造的代碼塊,并“隱藏”真正的功能代碼。
一、函數(shù)重定義
這是一種最基本也是最常用的代碼反調(diào)試技術(shù)了。在JavaScript中,我們可以對(duì)用于收集信息的函數(shù)進(jìn)行重定義。比如說(shuō),console.log()函數(shù)可以用來(lái)收集函數(shù)和變量等信息,并將其顯示在控制臺(tái)中。如果我們重新定義了這個(gè)函數(shù),我們就可以修改它的行為,并隱藏特定信息或顯示偽造的信息。
我們可以直接在DevTools中運(yùn)行這個(gè)函數(shù)來(lái)了解其功能:
console.log("HelloWorld"); var fake = function() {}; window['console']['log']= fake; console.log("Youcan't see me!");
運(yùn)行后我們將會(huì)看到:
VM48:1 Hello World
你會(huì)發(fā)現(xiàn)第二條信息并沒(méi)有顯示,因?yàn)槲覀冎匦露x了這個(gè)函數(shù),即“禁用”了它原本的功能。但是我們也可以讓它顯示偽造的信息。比如說(shuō)這樣:
console.log("Normalfunction"); //First we save a reference to the original console.log function var original = window['console']['log']; //Next we create our fake function //Basicly we check the argument and if match we call original function with otherparam. // If there is no match pass the argument to the original function var fake = function(argument) { if (argument === "Ka0labs") { original("Spoofed!"); } else { original(argument); } } // We redefine now console.log as our fake function window['console']['log']= fake; //Then we call console.log with any argument console.log("Thisis unaltered"); //Now we should see other text in console different to "Ka0labs" console.log("Ka0labs"); //Aaaand everything still OK console.log("Byebye!");
如果一切正常的話(huà):
Normal function
VM117:11 This is unaltered
VM117:9 Spoofed!
VM117:11 Bye bye!
實(shí)際上,為了控制代碼的執(zhí)行方式,我們還能夠以更加聰明的方式來(lái)修改函數(shù)的功能。比如說(shuō),我們可以基于上述代碼來(lái)構(gòu)建一個(gè)代碼段,并重定義eval函數(shù)。我們可以把JavaScript代碼傳遞給eval函數(shù),接下來(lái)代碼將會(huì)被計(jì)算并執(zhí)行。如果我們重定義了這個(gè)函數(shù),我們就可以運(yùn)行不同的代碼了:
//Just a normal eval eval("console.log('1337')"); //Now we repat the process... var original = eval; var fake = function(argument) { // If the code to be evaluated contains1337... if (argument.indexOf("1337") !==-1) { // ... we just execute a different code original("for (i = 0; i < 10;i++) { console.log(i);}"); } else { original(argument); } } eval= fake; eval("console.log('Weshould see this...')"); //Now we should see the execution of a for loop instead of what is expected eval("console.log('Too1337 for you!')");
運(yùn)行結(jié)果如下:
1337
VM146:1We should see this…
VM147:10
VM147:11
VM147:12
VM147:13
VM147:14
VM147:15
VM147:16
VM147:17
VM147:18
VM147:19
正如之前所說(shuō)的那樣,雖然這種方法非常巧妙,但這也是一種非?;A(chǔ)和常見(jiàn)的方法,所以比較容易被檢測(cè)到。
二、斷點(diǎn)
為了幫助我們了解代碼的功能,JavaScript調(diào)試工具(例如DevTools)都可以通過(guò)設(shè)置斷點(diǎn)的方式阻止腳本代碼執(zhí)行,而斷點(diǎn)也是代碼調(diào)試中最基本的了。
如果你研究過(guò)調(diào)試器或者x86架構(gòu),你可能會(huì)比較熟悉0xCC指令。在JavaScript中,我們有一個(gè)名叫debugger的類(lèi)似指令。當(dāng)我們?cè)诖a中聲明了debugger函數(shù)后,腳本代碼將會(huì)在debugger指令這里停止運(yùn)行。比如說(shuō):
console.log("Seeme!"); debugger; console.log("Seeme!");
很多商業(yè)產(chǎn)品會(huì)在代碼中定義一個(gè)無(wú)限循環(huán)的debugger指令,不過(guò)某些瀏覽器會(huì)屏蔽這種代碼,而有些則不會(huì)。這種方法的主要目的就是讓那些想要調(diào)試你代碼的人感到厭煩,因?yàn)闊o(wú)限循環(huán)意味著代碼會(huì)不斷地彈出窗口來(lái)詢(xún)問(wèn)你是否要繼續(xù)運(yùn)行腳本代碼:
setTimeout(function(){while (true) {eval("debugger")
三、時(shí)間差異
這是一種從傳統(tǒng)反逆向技術(shù)那里借鑒過(guò)來(lái)的基于時(shí)間的反調(diào)試技巧。當(dāng)腳本在DevTools等工具環(huán)境下執(zhí)行時(shí),運(yùn)行速度會(huì)非常慢(時(shí)間久),所以我們就可以根據(jù)運(yùn)行時(shí)間來(lái)判斷腳本當(dāng)前是否正在被調(diào)試。比如說(shuō),我們可以通過(guò)測(cè)量代碼中兩個(gè)設(shè)置點(diǎn)之間的運(yùn)行時(shí)間,然后用這個(gè)值作為參考,如果運(yùn)行時(shí)間超過(guò)這個(gè)值,說(shuō)明腳本當(dāng)前在調(diào)試器中運(yùn)行。
演示代碼如下:
set Interval(function(){ var startTime = performance.now(), check,diff; for (check = 0; check < 1000; check++){ console.log(check); console.clear(); } diff = performance.now() - startTime; if (diff > 200){ alert("Debugger detected!"); } },500);
四、DevTools檢測(cè)(Chrome)
這項(xiàng)技術(shù)利用的是div元素中的id屬性,當(dāng)div元素被發(fā)送至控制臺(tái)(例如console.log(div))時(shí),瀏覽器會(huì)自動(dòng)嘗試獲取其中的元素id。如果代碼在調(diào)用了console.log之后又調(diào)用了getter方法,說(shuō)明控制臺(tái)當(dāng)前正在運(yùn)行。
簡(jiǎn)單的概念驗(yàn)證代碼如下:
let div = document.createElement('div'); let loop = setInterval(() => { console.log(div); console.clear(); }); Object.defineProperty(div,"id", {get: () => { clearInterval(loop); alert("Dev Tools detected!"); }});
五、隱式流完整性控制
當(dāng)我們嘗試對(duì)代碼進(jìn)行反混淆處理時(shí),我們首先會(huì)嘗試重命名某些函數(shù)或變量,但是在JavaScript中我們可以檢測(cè)函數(shù)名是否被修改過(guò),或者說(shuō)我們可以直接通過(guò)堆棧跟蹤來(lái)獲取其原始名稱(chēng)或調(diào)用順序。
arguments.callee.caller
可以幫助我們創(chuàng)建一個(gè)堆棧跟蹤來(lái)存儲(chǔ)之前執(zhí)行過(guò)的函數(shù),演示代碼如下:
function getCallStack() { var stack = "#", total = 0, fn =arguments.callee; while ( (fn = fn.caller) ) { stack = stack + "" +fn.name; total++ } return stack } function test1() { console.log(getCallStack()); } function test2() { test1(); } function test3() { test2(); } function test4() { test3(); } test4();
注意:源代碼的混淆程度越強(qiáng),這個(gè)技術(shù)的效果就越好。
六、代理對(duì)象
代理對(duì)象是目前JavaScript中最有用的一個(gè)工具,這種對(duì)象可以幫助我們了解代碼中的其他對(duì)象,包括修改其行為以及觸發(fā)特定環(huán)境下的對(duì)象活動(dòng)。比如說(shuō),我們可以創(chuàng)建一個(gè)嗲哩對(duì)象并跟蹤每一次document.createElemen調(diào)用,然后記錄下相關(guān)信息:
const handler = { // Our hook to keep the track apply: function (target, thisArg, args){ console.log("Intercepted a call tocreateElement with args: " + args); return target.apply(thisArg, args) } } document.createElement= new Proxy(document.createElement, handler) // Create our proxy object withour hook ready to intercept document.createElement('div');
接下來(lái),我們可以在控制臺(tái)中記錄下相關(guān)參數(shù)和信息:
VM64:3 Intercepted a call to createElement with args: div
我們可以利用這些信息并通過(guò)攔截某些特定函數(shù)來(lái)調(diào)試代碼,但是本文的主要目的是為了介紹反調(diào)試技術(shù),那么我們?nèi)绾螜z測(cè)“對(duì)方”是否使用了代理對(duì)象呢?其實(shí)這就是一場(chǎng)“貓抓老鼠”的游戲,比如說(shuō),我們可以使用相同的代碼段,然后嘗試調(diào)用toString方法并捕獲異常:
//Call a "virgin" createElement: try { document.createElement.toString(); }catch(e){ console.log("I saw your proxy!"); }
信息如下:
"function createElement() { [native code] }"
但是當(dāng)我們使用了代理之后:
//Then apply the hook consthandler = { apply: function (target, thisArg, args){ console.log("Intercepted a call tocreateElement with args: " + args); return target.apply(thisArg, args) } } document.createElement= new Proxy(document.createElement, handler); //Callour not-so-virgin-after-that-party createElement try { document.createElement.toString(); }catch(e) { console.log("I saw your proxy!"); }
沒(méi)錯(cuò),我們確實(shí)可以檢測(cè)到代理:
VM391:13 I saw your proxy!
我們還可以添加toString方法:
const handler = { apply: function (target, thisArg, args){ console.log("Intercepted a call tocreateElement with args: " + args); return target.apply(thisArg, args) } } document.createElement= new Proxy(document.createElement, handler); document.createElement= Function.prototype.toString.bind(document.createElement); //Add toString //Callour not-so-virgin-after-that-party createElement try { document.createElement.toString(); }catch(e) { console.log("I saw your proxy!"); }
現(xiàn)在我們就沒(méi)辦法檢測(cè)到了:
"function createElement() { [native code] }"
就像我說(shuō)的,這就是一場(chǎng)“貓抓老鼠“的游戲。
總結(jié)
希望我所收集到的這些技巧可以對(duì)大家有所幫助,如果你有更好的技巧想跟大家分享,可以直接在文章下方的評(píng)論區(qū)留言,或者在Twitter上艾特我(@TheXC3LL)。
* 參考來(lái)源:x-c3ll,F(xiàn)B小編Alpha_h4ck編譯,轉(zhuǎn)載請(qǐng)注明來(lái)自FreeBuf.COM
相關(guān)文章
Javascript中從學(xué)習(xí)bind到實(shí)現(xiàn)bind的過(guò)程
這篇文章主要介紹了Javascript中從學(xué)習(xí)bind到實(shí)現(xiàn)bind的過(guò)程,有興趣的朋友跟著學(xué)習(xí)下吧。2018-01-01手把手教你搭建ES6的開(kāi)發(fā)運(yùn)行環(huán)境
如今ES6已經(jīng)發(fā)布了有一段時(shí)間了,很多人學(xué)了ES6但是卻沒(méi)運(yùn)行環(huán)境,下面這篇文章主要給大家介紹了關(guān)于搭建ES6運(yùn)行環(huán)境的相關(guān)資料,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-07-07一個(gè)用js實(shí)現(xiàn)控制臺(tái)控件的代碼
一個(gè)用js實(shí)現(xiàn)控制臺(tái)控件的代碼...2007-09-09用javascript編寫(xiě)的第一人稱(chēng)射擊游戲
用javascript編寫(xiě)的第一人稱(chēng)射擊游戲...2007-02-02IE和Firefox下javascript的兼容寫(xiě)法小結(jié)
學(xué)習(xí)js的朋友必須要知道或了解的知識(shí)2008-12-12JS typeof fn === ''function'' && fn()詳解
最近在學(xué)習(xí)js的時(shí)候發(fā)現(xiàn)很多人都喜歡在JS 中存在fn && fn() 執(zhí)行語(yǔ)句或者typeof fn === 'function' && fn(),下面這篇文章就為大家分享一下2020-08-08關(guān)于Error:Unknown?option?'--inline'報(bào)錯(cuò)的解決辦法
這篇文章主要給大家介紹了關(guān)于Error:Unknown?option?'--inline'報(bào)錯(cuò)的解決辦法,文中將解決的辦法介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-09-09