Amaliy qism
Quyida ko’rib o’tiladigan holatlar va misollar C++ dasturlash tilida keltiriladi.
1. IsDebuggerPresent funksiyasidan foydalanish eng oson va tezkor usullardan hisoblanadi. Undan foydalanishning sodda kodi quyida keltirilgan:
int main()
{
if (IsDebuggerPresent())
{
std::cout << "Stop debugging program!" << std::endl;
exit(-1);
}
return 0;
}
2. CheckRemoteDebuggerPresent funksiyasi funksiya yuqoridagi funksiyaga o’xshash. Nomida masofadagi mashina uchun deyilgan bo’lsada, lokal mashina uchun debaggerni yoqilganini aniqlaydi.
#include "stdafx.h"
#include "windows.h"
int main()
{
BOOL IsDbgPresent = FALSE;
CheckRemoteDebuggerPresent(GetCurrentProcess(), &IsDbgPresent);
if (IsDbgPresent)
{
MessageBox(NULL, TEXT("Please close your debugging application and restart the program"),
TEXT("Debugger Found!"), 0);
ExitProcess(0);
}
else {
MessageBox(NULL, TEXT("Seems Good to me"),
TEXT("No Debugger Found!"), 0);
ExitProcess(0);
}
}
3. NtQueryInformationProcess - Ntdll.dll-dagi xususiy API funksiyasi bo’lib, berilgan jarayon uchun axborotni olishda ishlatiladi. Uning birinchi argumenti process handle; Ikkinchisi funksiya qaytaruvchi jarayon ma’lumot turi. Masalan, ushbu parametr uchun ProcessDebugPort (value 0x7) qiymati kiritilganida, funksiya jarayon debaggerlanganini ko’rsatadi. Agar jarayon debaggerlangan bo’lsa, port raqamini aks holda nolni qaytaradi.
typedef NTSTATUS(NTAPI *pfnNtQueryInformationProcess)(
_In_ HANDLE ProcessHandle,
_In_ UINT ProcessInformationClass,
_Out_ PVOID ProcessInformation,
_In_ ULONG ProcessInformationLength,
_Out_opt_ PULONG ReturnLength
);
const UINT ProcessDebugPort = 7;
int main(int argc, char *argv[])
{
pfnNtQueryInformationProcess NtQueryInformationProcess = NULL;
NTSTATUS status;
DWORD isDebuggerPresent = 0;
HMODULE hNtDll = LoadLibrary(TEXT("ntdll.dll"));
if (NULL != hNtDll)
{
NtQueryInformationProcess =
(pfnNtQueryInformationProcess)GetProcAddress(hNtDll, "NtQueryInformationProcess");
if (NULL != NtQueryInformationProcess)
{
status = NtQueryInformationProcess(
GetCurrentProcess(),
ProcessDebugPort,
&isDebuggerPresent,
sizeof(DWORD),
NULL);
if (status == 0x00000000 && isDebuggerPresent != 0)
{
std::cout << "Stop debugging program!" << std::endl;
exit(-1);
}
}
}
return 0;
}
4. OutputDebugString funksiyasi debaggerga namoyish qilish uchun qatorni yuborishda foydalaniladi. Bu debeggerni mavjudligini tekshirish uchun foydalanish mumkin. Masalan, quyidagi kodda o’zgaruvchiga joriy xatolik kodini o’rnatish uchun SetLastError – dan foydalanilmoqda. Agar OutputDebugString chaqirilsa va debaggerlash mavjud bo’lmasa, GetLastError funksiyasi ushbu xatolik kodini qaytarmaydi. Buning sababi, OutputDebugString funksiyasi mavjud bo’lmasa, u boshqa xatolik kodini o’rnatadi. Agar debagger mavjud bo’lsa, OutputDebugString muvaffaqqiyatli ishlaydi va xatolik qiymati o’zgarmaydi
DWORD errrorValue = 12345;
SetLastError(errorValue);
OutputDebugString(“Test for Debugger”);
if(GetLastError() == errorValue)
{
ExitProcess();
}
else
{
RunSoftware();
}
Do'stlaringiz bilan baham: |