反調試總結

// AntiDbg.cpp : 定義控制檯應用程序的入口點。
//

//#include "stdafx.h"
#include <iostream>
#include <Windows.h>
using	namespace std;

enum PROCESSINFOCLASS//

{

	ProcessBasicInformation = 0,

	ProcessQuotaLimits,

	ProcessIoCounters,

	ProcessVmCounters,

	ProcessTimes,

	ProcessBasePriority,

	ProcessRaisePriority,

	ProcessDebugPort = 7,

	ProcessExceptionPort,

	ProcessAccessToken,

	ProcessLdtInformation,

	ProcessLdtSize,

	ProcessDefaultHardErrorMode,

	ProcessIoPortHandlers,

	ProcessPooledUsageAndLimits,

	ProcessWorkingSetWatch,

	ProcessUserModeIOPL,

	ProcessEnableAlignmentFaultFixup,

	ProcessPriorityClass,

	ProcessWx86Information,

	ProcessHandleCount,

	ProcessAffinityMask,

	ProcessPriorityBoost,

	MaxProcessInfoClass,

	ProcessWow64Information = 26,

	ProcessImageFileName = 27,

	ProcessDebugObjectHandle = 30,

	ProcessDebugFlags = 31,

	SystemKernelDebuggerInformation = 35

};

typedef enum _THREADINFOCLASS {
	ThreadBasicInformation,
	ThreadTimes,
	ThreadPriority,
	ThreadBasePriority,
	ThreadAffinityMask,
	ThreadImpersonationToken,
	ThreadDescriptorTableEntry,
	ThreadEnableAlignmentFaultFixup,
	ThreadEventPair_Reusable,
	ThreadQuerySetWin32StartAddress,
	ThreadZeroTlsCell,
	ThreadPerformanceCount,
	ThreadAmILastThread,
	ThreadIdealProcessor,
	ThreadPriorityBoost,
	ThreadSetTlsArrayAddress,
	ThreadIsIoPending,
	ThreadHideFromDebugger,
	ThreadBreakOnTermination,
	MaxThreadInfoClass
} THREADINFOCLASS;

typedef NTSTATUS(WINAPI *NtQueryInformationProcessPtr)(
	HANDLE processHandle,
	PROCESSINFOCLASS processInformationClass,
	PVOID processInformation,
	ULONG processInformationLength,
	PULONG returnLength);

typedef NTSTATUS(*NtSetInformationThreadPtr)(HANDLE threadHandle,
	THREADINFOCLASS threadInformationClass,
	PVOID threadInformation,
	ULONG threadInformationLength);

bool PebNtGlobalFlagsApproach(){ //可用
	int	result;
	__asm{
		mov eax, fs:[30h]
		mov eax, [eax+68h]
		and eax,70
		mov result,eax
	}
	return result!=0;
}

bool HeapFlagsApproach(){ //win7 x64測試沒用
	int result;
	__asm{
		mov eax, fs:[30h]
		mov eax, [eax+18h]
		mov eax, [eax+10h]
		mov result,eax
	}
	return result;
}

bool NtQueryInformationProcessApproach(){ //可用
	int DebugPort = 0;
	HMODULE	hNtdll = LoadLibrary(TEXT("Ntdll.dll"));
	NtQueryInformationProcessPtr NtQueryInformationProcess = (NtQueryInformationProcessPtr)GetProcAddress(hNtdll, "NtQueryInformationProcess");
	if (NtQueryInformationProcess(GetCurrentProcess(), (PROCESSINFOCLASS)7, &DebugPort, sizeof(DebugPort), NULL))
		printf("[ERROR NtQueryInformationProcessApproach] NtQueryInformationProcess failed\n");
	else
		return DebugPort == -1;
	return false;
}

void NtSetInformationThreadApproach(){ //可用
	HMODULE	hNtdll = LoadLibrary(TEXT("Ntdll.dll"));
	NtSetInformationThreadPtr NtSetInformationThread = (NtSetInformationThreadPtr)GetProcAddress(hNtdll, "NtSetInformationThread");
	NtSetInformationThread(GetCurrentThread(), (THREADINFOCLASS)0x11, 0, 0);
}

LONG WINAPI MyUnhandledExceptionFilter(struct _EXCEPTION_POINTERS *pei){
	SetUnhandledExceptionFilter((LPTOP_LEVEL_EXCEPTION_FILTER)
	pei->ContextRecord->Eax);
	// 修改寄存器eip的值
	pei->ContextRecord->Eip += 2;
	// 告訴操作系統,繼續執行進程剩餘的指令(指令保存在eip裏),而不是關閉進程
	return EXCEPTION_CONTINUE_EXECUTION;
}

bool UnhandledExceptionFilterApproach() //實用
{
	
	SetUnhandledExceptionFilter(MyUnhandledExceptionFilter);
	__asm{
		// 將eax清零
		xor eax, eax
		// 觸發一個除零異常
		div eax
	}
	return false;
}

bool	DeleteFiberApproach()//效果不好 win7 x64
{
	char	fib[1024] = { 0 };
	DeleteFiber(fib);
	return GetLastError() != 0x57;
}
int _tmain(int argc, _TCHAR* argv[])
{

	UnhandledExceptionFilterApproach();

	cout << "hello" << endl;
	//system("pause");
	getchar();
	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章