Delphi_OD_代码_调试_Delphi反调试技术,以OD为例附核心原代码

1.程序窗口[chuang kou]句柄[ju bing]检测

原理:用FindWindow函数[han shu]查找[cha zhao]具有相同窗口[chuang kou]类名和标题的窗口[chuang kou],如果找到就说明[shuo ming]有OD在运行[yun hang]

//********************************************

//通过查找[cha zhao]窗口[chuang kou]类名来实现检测OllyDBG

//********************************************

function AntiLoader():Boolean;

const

OllyName='OLLYDBG';

var

Hwnd:Thandle;

begin

Hwnd:=FindWindow(OllyName,nil);

if Hwnd<>0 then

Result:=True

else

Result:=False;

end;

procedure TForm1.FormCreate(Sender: TObject);

begin

if AntiLoader then

MessageBox(Handle,'找到调试[tiao shi]器!','提示[ti shi]',MB_OK+MB_ICONINFORMATION)

else

MessageBox(Handle,'未找到调试[tiao shi]器!','提示[ti shi]',MB_OK+MB_ICONINFORMATION)

end;

2.用线程[xian cheng]环境块检测

原理:用ring3级下的调试[tiao shi]器对可执行[zhi hang]程序[zhi hang cheng xu][ke zhi hang cheng xu]进行调试[tiao shi]时,调试[tiao shi]器会把被调试[tiao shi]的可执行[zhi hang]程序[zhi hang cheng xu][ke zhi hang cheng xu]作为一个子线程[xian cheng]进行跟踪[gen zong].这时被调试[tiao shi]的可执行[zhi hang]程序[zhi hang cheng xu][ke zhi hang cheng xu]的PEB结构[jie gou]偏移0x02处的BeingDebugged的值为1,如果可执行[zhi hang]程序[zhi hang cheng xu][ke zhi hang cheng xu]未被调试[tiao shi],则值为0,所以可以利用这个值来检测程序是否被ring3级下的调试[tiao shi]器调试[tiao shi]

//***************************************

//使用PEB结构[jie gou]检测OllyDBG

//***************************************

function AntiLoader():Boolean; //检测调试[tiao shi]器;

var

YInt,NInt:Integer;

begin

asm

mov eax,fs:[$30]

//获取PEB偏移2h处BeingDebugged的值

movzx eax,byte ptr[eax+$2]

or al,al

jz @No

jnz @Yes

@No:

mov NInt,1

@Yes:

Mov YInt,1

end;

if YInt=1 then

Result:=True;

if NInt=1 then

Result:=False;

end;

procedure TForm1.FormCreate(Sender: TObject);

begin

if AntiLoader then

MessageBox(Handle,'发现调试[tiao shi]器!','提示[ti shi]',MB_OK+MB_ICONINFORMATION)

else

MessageBox(Handle,'未发现调试[tiao shi]器!','提示[ti shi]',MB_OK+MB_ICONINFORMATION);

end;

3.用API函数[han shu]IsDebuggerPresent检测

原理:操作系统[xi tong][cao zuo xi tong]将调试[tiao shi]对象[dui xiang]设置[she zhi]为在特殊环境中运行[yun hang],而kernel32.dll中的API函数[han shu]IsDebuggerPresent的功能是用于[yong yu]判断进程[jin cheng]是否处于调试[tiao shi]环境中,这样就可以利用这个API函数[han shu]来查看进程[jin cheng]是否在调试[tiao shi]器中执行[zhi hang]

//****************************************

//利用IsDebuggerPresent函数[han shu]检测OllyDBG

//****************************************

function AntiLoader():Boolean;

var

isDebuggerPresent: function:Boolean;

Addr: THandle;

begin

Addr := LoadLibrary('kernel32.dll');

isDebuggerPresent := GetProcAddress(Addr, 'IsDebuggerPresent');

if isDebuggerPresent then

Result:=True

else

Result:=False;

end;

procedure TForm1.FormCreate(Sender: TObject);

begin

if AntiLoader then

MessageBox(Handle,'发现调试[tiao shi]器!','提示[ti shi]',MB_OK+MB_ICONINFORMATION)

else

MessageBox(Handle,'未发现提示[ti shi]器!','提示[ti shi]',MB_OK+MB_ICONINFORMATION);

end;

4.检查程序[jian cha cheng xu]的父进程[jin cheng]

原理:Windows操作系统[xi tong][cao zuo xi tong]下的GUI可执行[zhi hang]程序[zhi hang cheng xu][ke zhi hang cheng xu]的父进程[jin cheng]都是explorer.exe(CUI可执行[zhi hang]程序[zhi hang cheng xu][ke zhi hang cheng xu]的父进程[jin cheng]是 CMD.exe,系统[xi tong]服务[xi tong fu wu]的父进程[jin cheng]是Service.exe,在实际使用的时候需要根据自己的程序类型[lei xing]来选择[xuan ze]父进程[jin cheng]实现反跟踪[gen zong]),而正被调试[tiao shi]器OD调试[tiao shi]的程序的父进程[jin cheng]是调试[tiao shi]器的执行[zhi hang]程序[zhi hang cheng xu]ollydbg.exe而不是别的.所以可以利用检查父进程[jin cheng]是否为explorer.exe的方法[fang fa]来检测OD.

//***************************************************

//检查父进程[jin cheng]来检测OllyDBG

//***************************************************

function AntiLoader():Boolean;

const

ParentName='\EXPLORER.EXE';

var

hSnap,hProcess:THandle;

szBuffer:array[0..MAX_PATH] of char;

FileName:array[0..MAX_PATH] of char;

Process32:PROCESSENTRY32;

LoopFlag:BOOL;

begin

////得到所有进程[jin cheng]的列表[lie biao]快照[kuai zhao]

hSnap:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);

if hSnap=INVALID_HANDLE_VALUE then

begin

Result:=False;

Exit;

end;

Process32.dwSize:=sizeof(PROCESSENTRY32);

//查找[cha zhao]进程[jin cheng]

LoopFlag:=Process32First(hSnap,Process32);

if LoopFlag=False then

begin

CloseHandle(hSnap);

Result:=False;

Exit;

end;

while Integer(LoopFlag)<>0 do

begin

if Process32.th32ProcessID=GetCurrentProcessId() then

begin

hProcess:=OpenProcess(PROCESS_ALL_ACCESS,FALSE,Process32.th32ParentProcessID);

if hProcess<>0 then

begin

if GetModuleFileNameEx(hProcess,0,FileName,MAX_PATH)<>0 then

begin

//取得系统[xi tong]目录[xi tong mu lu]

GetWindowsDirectory(szBuffer,MAX_PATH);

//合并系统[xi tong]目录[xi tong mu lu]和\EXPLORER.EXE

StrCat(szBuffer,ParentName);

//转换[zhuan huan]成大写以后比较当前调试[tiao shi]程序[tiao shi cheng xu]的进程[jin cheng]是否为父进程[jin cheng]

if UpperCase(String(FileName))<>UpperCase(String(szBuffer)) then

Result:=True

else

Result:=False;

end;

end

else

Result:=False;

end;

LoopFlag:=Process32Next(hSnap,Process32);

end;

CloseHandle(hSnap);

end;

procedure TForm1.FormCreate(Sender: TObject);

begin

if AntiLoader then

MessageBox(Handle,'发现调试[tiao shi]器!','提示[ti shi]',MB_OK+MB_ICONINFORMATION)

else

MessageBox(Handle,'未发现调试[tiao shi]器!','提示[ti shi]',MB_OK+MB_ICONINFORMATION)

end;

5.检查STARTUPINFO结构[jie gou]

原理:Windows操作系统[xi tong][cao zuo xi tong]中的explorer.exe创建进程[jin cheng]的时候会把STARTUPINFO结构[jie gou]中的值设为0,而非explorer.exe创建进程[jin cheng]的时候会忽略这个结构[jie gou]中的值,也就是结构[jie gou]中的值不为0,所以可以利用这个来判断OD是否在调试[tiao shi]程序[tiao shi cheng xu].

/************************************************

//通过检测STARTUPINFO结构[jie gou]来检测OllyDbg

//************************************************

function AntiLoader():Boolean;

var

Info:STARTUPINFO;

begin

GetStartupInfo(Info);

if (Info.dwX<>0) or (Info.dwY<>0) or (Info.dwXCountChars<>0) or (Info.dwYCountChars<>0) or

(Info.dwFillAttribute<>0) or (Info.dwXSize<>0) or (Info.dwYSize<>0) then

Result:=True

else

Result:=False;

end;

procedure TMainFrm.FormCreate(Sender: TObject);

begin

if AntiLoader then

MessageBox(Handle,'发现调试[tiao shi]器!','提示[ti shi]',MB_OK)

else

MessageBox(Handle,'未发现调试[tiao shi]器!','提示[ti shi]',MB_OK);

end;

参考:http://www.cnblogs.com/key-ok/p/4594685.html