您现在的位置:首页 >> API >> API >> 内容

Delphi反调试技术(以OD为例附核心原代码)(2)

时间:2011/9/3 14:52:23 点击:


4.检查程序的父进程
原理:Windows操作系统下的GUI可执行程序的父进程都是explorer.exe(CUI可执行程序的父进程是CMD.exe,系统服务的父进程是Service.exe,在实际使用的时候需要根据自己的程序类型来选择父进程实现反跟踪),而正被调试器OD调试的程序的父进程是调试器的执行程序ollydbg.exe而不是别的.所以可以利用检查父进程是否为explorer.exe的方法来检测OD.
//***************************************************
//检查父进程来检测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
  ////得到所有进程的列表快照
  hSnap:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  if hSnap=INVALID_HANDLE_VALUE then
  begin
    Result:=False;
    Exit;
  end;
  Process32.dwSize:=sizeof(PROCESSENTRY32);
  //查找进程
  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
                  //取得系统目录
                  GetWindowsDirectory(szBuffer,MAX_PATH);
                  //合并系统目录和\EXPLORER.EXE
                  StrCat(szBuffer,ParentName);
                  //转换成大写以后比较当前调试程序的进程是否为父进程
                  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,'发现调试器!','提示',MB_OK+MB_ICONINFORMATION)
  else
    MessageBox(Handle,'未发现调试器!','提示',MB_OK+MB_ICONINFORMATION)
end;
5.检查STARTUPINFO结构
原理:Windows操作系统中的explorer.exe创建进程的时候会把STARTUPINFO结构中的值设为0,而非explorer.exe创建进程的时候会忽略这个结构中的值,也就是结构中的值不为0,所以可以利用这个来判断OD是否在调试程序.
/************************************************
//通过检测STARTUPINFO结构来检测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,'发现调试器!','提示',MB_OK)
  else
    MessageBox(Handle,'未发现调试器!','提示',MB_OK);
end;

上一页12下一页

作者:网络 来源:转载
共有评论 0相关评论
发表我的评论
  • 大名:
  • 内容:
本类推荐
  • 没有
本类固顶
  • 没有
  • 盒子文章(www.2ccc.com) © 2024 版权所有 All Rights Reserved.
  • 沪ICP备05001939号