防止被进程附加调试

  1. /**********************防止被进程附加调试*******************************/ 
  2. bool AntiAttach() 
  3.       HMODULE ntdll; // ntdll handle 
  4.       void* pDbgUiRemoteBreakin; // function handle 
  5.       DWORD dwOldProtect; // just for fun 
  6.       DWORD dwCodeSize; // Size of code to copy 
  7.       
  8.       // Get ntdll.dll handle 
  9.       ntdll = GetModuleHandle("ntdll.dll"); 
  10.       if(ntdll) 
  11.       { 
  12.            // Get target function addr 
  13.            pDbgUiRemoteBreakin = GetProcAddress(ntdll, "DbgUiRemoteBreakin"); 
  14.            if(pDbgUiRemoteBreakin) 
  15.            { 
  16.                 __asm 
  17.                 { 
  18.                      // Get code size 
  19.                      lea eax, __CodeToCopyStart 
  20.                           lea ecx, __CodeToCopyEnd 
  21.                           sub ecx, eax 
  22.                           mov dwCodeSize, ecx 
  23.                 } 
  24.                 // Make sure that we have write rights ... 
  25.                 if(VirtualProtect(pDbgUiRemoteBreakin, dwCodeSize, PAGE_EXECUTE_READWRITE, &dwOldProtect)) 
  26.                 { 
  27.                      __asm 
  28.                      { 
  29.                           // Copy code between __CodeToCopyStart and __CodeToCopyEnd 
  30.                           mov edi, pDbgUiRemoteBreakin 
  31.                                lea esi, __CodeToCopyStart 
  32.                                mov ecx, dwCodeSize 
  33.                                rep movsb 
  34.                                // Skip code 
  35.                                jmp __CodeEnd 
  36.                                
  37. __CodeToCopyStart: 
  38.                           lea eax, __CodeToCopyEnd 
  39.                                jmp eax 
  40. __CodeToCopyEnd: 
  41.                      } 
  42.                      
  43.                      // ***CODE*HERE*** 
  44.                      __asm 
  45.                      { 
  46.                           // Clear registers 
  47.                           xor eax, eax 
  48.                                pushfd 
  49.                                mov [esp], eax 
  50.                                popfd 
  51.                                xor ebx, ebx 
  52.                                xor ecx, ecx 
  53.                                xor edx, edx 
  54.                                xor esi, esi 
  55.                                xor edi, edi 
  56.                                xor esp, esp 
  57.                                xor ebp, ebp 
  58.                                // Jump to address 0 
  59.                                jmp eax 
  60.                      } 
  61.                      // *************** 
  62.                      
  63. __CodeEnd:; 
  64.            return true
  65.                 } 
  66.            } 
  67.       } 
  68.       return false;      
  69. void AntiDebug() 
  70.       HANDLE handle=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0); 
  71.       PROCESSENTRY32* info=new PROCESSENTRY32; 
  72. info->dwSize=sizeof(PROCESSENTRY32); 
  73. int i=0; 
  74. //      HTREEITEM hItem[100]; 
  75.       
  76.       int MyParentProcessID=0;//自身的父进程ID 
  77.       int ExplorerProcessID=0;//Explorer.exe的进程ID 
  78.       int WinRarProcessID = 0;//winrar进程ID 
  79.       if (Process32First(handle,info)) 
  80.       { 
  81.            while (Process32Next(handle,info)) 
  82.            { 
  83.                 CString exe; 
  84.                 exe.Format("%s",info->szExeFile); 
  85.                 
  86.                 CString ProcessID; 
  87.                 ProcessID.Format("进程ID:%d",info->th32ProcessID); 
  88.                 
  89.                 CString ParentID; 
  90.                 ParentID.Format("父进程ID:%d",info->th32ParentProcessID); 
  91.                 //比较进程名,这里忽略大小写 
  92.                 if (stricmp(exe.GetBuffer(exe.GetLength()),"WinRAR.exe") == 0) 
  93.                      WinRarProcessID = info->th32ProcessID; 
  94.                 if (stricmp(exe.GetBuffer(exe.GetLength()),"explorer.exe")==0) 
  95.                      ExplorerProcessID=info->th32ProcessID;//获得EXPLORER的进程ID 
  96.                 if (stricmp(exe.GetBuffer(exe.GetLength()),"test.exe")==0) 
  97.                      MyParentProcessID=info->th32ParentProcessID;//获得自身父进程ID 
  98.                 
  99.                 i++; 
  100.            } 
  101.       }      
  102. //      SetDlgItemText(IDC_STATUS,sta);      
  103.       CloseHandle(handle);      
  104.       delete info; 
  105.       //检查自身程序的父进程是否是winrar 
  106.       if(MyParentProcessID != WinRarProcessID) 
  107.       { 
  108.            //检测自身的父进程是否是EXPLORER 
  109.            if (MyParentProcessID!=ExplorerProcessID) 
  110.            { 
  111.                 //           AfxMessageBox("检测到调试器"); 
  112.                 PrintLog("检测到调试器,结束自身"); 
  113. //                exit(0); 
  114.                 //           return ; 
  115.            } 
  116.       } 
  117. //      else 
  118. //           AfxMessageBox("注意:没有检测到调试器..."); 
  119. }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值