在前面我的博文一步一步簡單的保護我們的代碼中提到了動态監測程序是否被調試。此間用到了Win Api IsDebuggerPresent。很可惜,某些系統沒有導出該函數,是以,在那篇博文中用彙編模拟了這個API的實作。
今天看reactos033異常處理相關的源碼,在異常處理開始部分,無意中看到了reactos監測程序調試狀态的實作,代碼如下:
[ntoskrnl\include\internal\i386\asmmarco.S]
mov ecx, fs:[KPCR_CURRENT_THREAD]
cld
/* Flush DR7 */
and dword ptr [ebp+KTRAP_FRAME_DR7], 0
/* Check if the thread was being debugged */
test byte ptr [ecx+KTHREAD_DEBUG_ACTIVE], 0xFF
jnz Dr_&Label
既然reactos有這部分代碼,wrk沒理由沒有,經過搜尋,在檔案[base\ntos\ke\i386\kimarco.inc]中找到如下代碼:
ifdef PcPrcbData
mov ecx,PCR[PcPrcbData+PbCurrentThread] ; get current thread address
else
mov ecx,PCR[PcPrcb]
mov ecx,[ecx].PbCurrentThread ; get current thread address
endif
cld
and dword ptr [ebp].TsDr7, 0
test byte ptr [ecx].ThDebugActive, 0ffh ; See if debug registers need saving
jnz Dr_&AssistLabel