自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

JavaScript反調試,你要知道這六項實用技巧

安全 網(wǎng)站安全
對于JavaScript來說,你只需要花一點時間進行調試和分析,你就能夠了解到JavaScript代碼段的功能邏輯。

在此之前,我一直都在研究JavaScript相關的反調試技巧。但是當我在網(wǎng)上搜索相關資料時,我發(fā)現(xiàn)網(wǎng)上并沒有多少關于這方面的文章,而且就算有也是非常不完整的那種。所以在這篇文章中,我打算跟大家總結一下關于JavaScript反調試技巧方面的內(nèi)容。值得一提的是,其中有些方法已經(jīng)被網(wǎng)絡犯罪分子廣泛應用到惡意軟件之中了。

[[221760]]

對于JavaScript來說,你只需要花一點時間進行調試和分析,你就能夠了解到JavaScript代碼段的功能邏輯。而我們所要討論的內(nèi)容,可以給那些想要分析你JavaScript代碼的人增加一定的難度。不過我們的技術跟代碼混淆無關,我們主要針對的是如何給代碼主動調試增加困難。

本文所要介紹的技術方法大致如下:

  1. 檢測未知的執(zhí)行環(huán)境(我們的代碼只想在瀏覽器中被執(zhí)行);
  2. 檢測調試工具(例如DevTools);
  3. 代碼完整性控制;
  4. 流完整性控制;
  5. 反模擬;

簡而言之,如果我們檢測到了“不正常”的情況,程序的運行流程將會改變,并跳轉到偽造的代碼塊,并“隱藏”真正的功能代碼。

一、函數(shù)重定義

這是一種最基本也是最常用的代碼反調試技術了。在JavaScript中,我們可以對用于收集信息的函數(shù)進行重定義。比如說,console.log()函數(shù)可以用來收集函數(shù)和變量等信息,并將其顯示在控制臺中。如果我們重新定義了這個函數(shù),我們就可以修改它的行為,并隱藏特定信息或顯示偽造的信息。

我們可以直接在DevTools中運行這個函數(shù)來了解其功能:

  1. console.log("HelloWorld"); 
  2. var fake = function() {}; 
  3. window['console']['log']= fake; 
  4. console.log("Youcan't see me!"); 

運行后我們將會看到:

  1. VM48:1 Hello World 

你會發(fā)現(xiàn)第二條信息并沒有顯示,因為我們重新定義了這個函數(shù),即“禁用”了它原本的功能。但是我們也可以讓它顯示偽造的信息。比如說這樣:

  1. console.log("Normalfunction"); 
  2. //First we save a reference to the original console.log function 
  3. var original = window['console']['log']; 
  4. //Next we create our fake function 
  5. //Basicly we check the argument and if match we call original function with otherparam. 
  6. // If there is no match pass the argument to the original function 
  7. var fake = function(argument) { 
  8.     if (argument === "Ka0labs") { 
  9.         original("Spoofed!"); 
  10.     } else { 
  11.         original(argument); 
  12.     } 
  13. // We redefine now console.log as our fake function 
  14. window['console']['log']= fake; 
  15. //Then we call console.log with any argument 
  16. console.log("Thisis unaltered"); 
  17. //Now we should see other text in console different to "Ka0labs" 
  18. console.log("Ka0labs"); 
  19. //Aaaand everything still OK 
  20. console.log("Byebye!"); 

如果一切正常的話:

  1. Normal function 
  2. VM117:11 This is unaltered 
  3. VM117:9 Spoofed! 
  4. VM117:11 Bye bye! 

實際上,為了控制代碼的執(zhí)行方式,我們還能夠以更加聰明的方式來修改函數(shù)的功能。比如說,我們可以基于上述代碼來構建一個代碼段,并重定義eval函數(shù)。我們可以把JavaScript代碼傳遞給eval函數(shù),接下來代碼將會被計算并執(zhí)行。如果我們重定義了這個函數(shù),我們就可以運行不同的代碼了:

  1. //Just a normal eval  
  2. eval("console.log('1337')");  
  3. //Now we repat the process...  
  4. var original = eval 
  5. var fake = function(argument) {  
  6.     // If the code to be evaluated contains1337...  
  7.     if (argument.indexOf("1337") !==-1) {  
  8.         // ... we just execute a different code  
  9.         original("for (i = 0; i < 10;i++) { console.log(i);}");  
  10.     }  
  11.     else {  
  12.         original(argument);  
  13.     }  
  14.  
  15. evalfake 
  16. eval("console.log('Weshould see this...')");  
  17. //Now we should see the execution of a for loop instead of what is expected  
  18. eval("console.log('Too1337 for you!')"); 

運行結果如下:

  1. 1337 
  2. VM146:1We should see this… 
  3. VM147:10 
  4. VM147:11 
  5. VM147:12 
  6. VM147:13 
  7. VM147:14 
  8. VM147:15 
  9. VM147:16 
  10. VM147:17 
  11. VM147:18 
  12. VM147:19 

正如之前所說的那樣,雖然這種方法非常巧妙,但這也是一種非?;A和常見的方法,所以比較容易被檢測到。

二、斷點

為了幫助我們了解代碼的功能,JavaScript調試工具(例如DevTools)都可以通過設置斷點的方式阻止腳本代碼執(zhí)行,而斷點也是代碼調試中最基本的了。

如果你研究過調試器或者x86架構,你可能會比較熟悉0xCC指令。在JavaScript中,我們有一個名叫debugger的類似指令。當我們在代碼中聲明了debugger函數(shù)后,腳本代碼將會在debugger指令這里停止運行。比如說:

  1. console.log("Seeme!"); 
  2. debugger; 
  3. console.log("Seeme!"); 

很多商業(yè)產(chǎn)品會在代碼中定義一個***循環(huán)的debugger指令,不過某些瀏覽器會屏蔽這種代碼,而有些則不會。這種方法的主要目的就是讓那些想要調試你代碼的人感到厭煩,因為***循環(huán)意味著代碼會不斷地彈出窗口來詢問你是否要繼續(xù)運行腳本代碼:

  1. setTimeout(function(){while (true) {eval("debugger") 

三、時間差異

這是一種從傳統(tǒng)反逆向技術那里借鑒過來的基于時間的反調試技巧。當腳本在DevTools等工具環(huán)境下執(zhí)行時,運行速度會非常慢(時間久),所以我們就可以根據(jù)運行時間來判斷腳本當前是否正在被調試。比如說,我們可以通過測量代碼中兩個設置點之間的運行時間,然后用這個值作為參考,如果運行時間超過這個值,說明腳本當前在調試器中運行。

演示代碼如下:

  1. set Interval(function(){ 
  2.   var startTime = performance.now(), check,diff; 
  3.   for (check = 0; check < 1000; check++){ 
  4.     console.log(check); 
  5.     console.clear(); 
  6.   } 
  7.   diff = performance.now() - startTime; 
  8.   if (diff > 200){ 
  9.     alert("Debugger detected!"); 
  10.   } 
  11. },500); 

四、DevTools檢測(Chrome)

這項技術利用的是div元素中的id屬性,當div元素被發(fā)送至控制臺(例如console.log(div))時,瀏覽器會自動嘗試獲取其中的元素id。如果代碼在調用了console.log之后又調用了getter方法,說明控制臺當前正在運行。

簡單的概念驗證代碼如下:

  1. let div = document.createElement('div'); 
  2. let loop = setInterval(() => { 
  3.     console.log(div); 
  4.     console.clear(); 
  5. }); 
  6. Object.defineProperty(div,"id", {get: () => { 
  7.     clearInterval(loop); 
  8.     alert("Dev Tools detected!"); 
  9. }}); 

五、隱式流完整性控制

當我們嘗試對代碼進行反混淆處理時,我們首先會嘗試重命名某些函數(shù)或變量,但是在JavaScript中我們可以檢測函數(shù)名是否被修改過,或者說我們可以直接通過堆棧跟蹤來獲取其原始名稱或調用順序。

arguments.callee.caller可以幫助我們創(chuàng)建一個堆棧跟蹤來存儲之前執(zhí)行過的函數(shù),演示代碼如下:

  1. function getCallStack() { 
  2.     var stack = "#"total = 0fn =arguments.callee; 
  3.     while ( (fnfn = fn.caller) ) { 
  4.         stackstack = stack + "" +fn.name; 
  5.         total++ 
  6.     } 
  7.     return stack 
  8. function test1() { 
  9.     console.log(getCallStack()); 
  10. function test2() { 
  11.     test1(); 
  12. function test3() { 
  13.     test2(); 
  14. function test4() { 
  15.     test3(); 
  16. test4(); 

注意:源代碼的混淆程度越強,這個技術的效果就越好。

六、代理對象

代理對象是目前JavaScript中最有用的一個工具,這種對象可以幫助我們了解代碼中的其他對象,包括修改其行為以及觸發(fā)特定環(huán)境下的對象活動。比如說,我們可以創(chuàng)建一個嗲哩對象并跟蹤每一次document.createElemen調用,然后記錄下相關信息:

  1. const handler = { // Our hook to keep the track 
  2.     apply: function (target, thisArg, args){ 
  3.         console.log("Intercepted a call tocreateElement with args: " + args); 
  4.         return target.apply(thisArg, args) 
  5.     } 
  6.   
  7. document.createElementnew Proxy(document.createElement, handler) // Create our proxy object withour hook ready to intercept 
  8. document.createElement('div'); 

接下來,我們可以在控制臺中記錄下相關參數(shù)和信息:

  1. VM64:3 Intercepted a call to createElement with args: div 

我們可以利用這些信息并通過攔截某些特定函數(shù)來調試代碼,但是本文的主要目的是為了介紹反調試技術,那么我們?nèi)绾螜z測“對方”是否使用了代理對象呢?其實這就是一場“貓抓老鼠”的游戲,比如說,我們可以使用相同的代碼段,然后嘗試調用toString方法并捕獲異常:

  1. //Call a "virgin" createElement: 
  2. try { 
  3.     document.createElement.toString(); 
  4. }catch(e){ 
  5.     console.log("I saw your proxy!"); 

信息如下:

  1. "function createElement() { [native code] }" 

但是當我們使用了代理之后:

  1. //Then apply the hook 
  2. consthandler = { 
  3.     apply: function (target, thisArg, args){ 
  4.         console.log("Intercepted a call tocreateElement with args: " + args); 
  5.         return target.apply(thisArg, args) 
  6.     } 
  7. document.createElementnew Proxy(document.createElement, handler); 
  8.   
  9. //Callour not-so-virgin-after-that-party createElement 
  10. try { 
  11.     document.createElement.toString(); 
  12. }catch(e) { 
  13.     console.log("I saw your proxy!"); 

沒錯,我們確實可以檢測到代理:

  1. VM391:13 I saw your proxy! 

我們還可以添加toString方法:

  1. const handler = { 
  2.     apply: function (target, thisArg, args){ 
  3.         console.log("Intercepted a call tocreateElement with args: " + args); 
  4.         return target.apply(thisArg, args) 
  5.     } 
  6. document.createElementnew Proxy(document.createElement, handler); 
  7. document.createElementFunction.prototype.toString.bind(document.createElement); //Add toString 
  8. //Callour not-so-virgin-after-that-party createElement 
  9. try { 
  10.     document.createElement.toString(); 
  11. }catch(e) { 
  12.     console.log("I saw your proxy!"); 

現(xiàn)在我們就沒辦法檢測到了:

  1. "function createElement() { [native code] }" 

就像我說的,這就是一場“貓抓老鼠“的游戲。

責任編輯:趙寧寧 來源: FreeBuf
相關推薦

2022-07-06 15:51:48

瀏覽器開發(fā)者工具

2022-09-15 07:05:09

Windows電腦技巧

2018-03-12 10:15:10

前端Javascript反調試

2021-04-12 15:54:45

Android 開發(fā)技巧

2023-01-09 17:23:14

CSS技巧

2022-09-20 11:58:27

NpmNode.js

2019-06-14 14:15:07

Javascript調試技巧代碼

2024-04-03 10:29:13

JavaScrip優(yōu)化技巧

2020-07-11 09:45:33

Python編程語言開發(fā)

2010-08-31 15:03:18

網(wǎng)絡釣魚

2024-01-08 18:05:19

PyCharm技巧功能

2009-09-04 10:27:28

Linux實用技巧linux操作系統(tǒng)linux

2022-03-23 09:18:10

Git技巧Linux

2021-01-28 10:04:40

JavaScript開發(fā)技術

2023-03-19 16:02:33

JavaScrip技巧編程語言

2021-01-21 08:00:00

開發(fā)工具VS Code

2023-10-30 09:10:05

DjangoQuerySet

2011-02-21 15:48:19

2009-12-21 15:50:39

2018-09-10 09:26:33

點贊
收藏

51CTO技術棧公眾號