atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Sat Mar 08, 2008 1:37 pm Post subject: |
|
|
Uh... ok I posted a source code and it posted a link instead to something I haven't seen...
Anyway this is from Patricks sources on GameDeception.net:
| Code: | FARPROC HookImportFunction( HMODULE hModule, const char * szModuleName, const char * szFunctionName, DWORD dwHookFunction )
{
PIMAGE_NT_HEADERS pNtHeader = NULL;
PIMAGE_IMPORT_DESCRIPTOR pImportDescriptor = NULL;
PIMAGE_THUNK_DATA pThunk = NULL;
FARPROC fpFunction = NULL;
HMODULE hOwnerModule = GetModuleHandle( szModuleName );
DWORD dwProtect[2] = { 0 };
BOOL bHooked = FALSE;
if( (fpFunction = GetProcAddress( hOwnerModule, szFunctionName )) == NULL ) { return( NULL ); }
if( ((PIMAGE_DOS_HEADER)hModule)->e_magic != IMAGE_DOS_SIGNATURE ) { return( NULL ); }
pNtHeader = (PIMAGE_NT_HEADERS)((DWORD)hModule + (DWORD)((PIMAGE_DOS_HEADER)hModule)->e_lfanew);
if( pNtHeader->Signature != IMAGE_NT_SIGNATURE ) { return( NULL ); }
pImportDescriptor = (PIMAGE_IMPORT_DESCRIPTOR)((DWORD)hModule + (DWORD)pNtHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress);
while( pImportDescriptor->Name != NULL )
{
if( stricmp( (char*)((DWORD)hModule + (DWORD)pImportDescriptor->Name), szModuleName ) == 0 )
{
if( pImportDescriptor->Name == NULL ) { return( NULL ); }
pThunk = (PIMAGE_THUNK_DATA)((DWORD)hModule + (DWORD)pImportDescriptor->FirstThunk);
while( pThunk->u1.Function != NULL )
{
if( pThunk->u1.Function == (DWORD)fpFunction )
{
if( VirtualProtect( (LPVOID)&pThunk->u1.Function, sizeof( DWORD ), PAGE_EXECUTE_READWRITE, &dwProtect[0] ) == FALSE )
{
return( NULL );
}
if( IsBadWritePtr( (LPVOID)&pThunk->u1.Function, sizeof( DWORD ) ) == 0 )
{
bHooked = TRUE;
pThunk->u1.Function = (DWORD)dwHookFunction;
}
if( VirtualProtect( (LPVOID)&pThunk->u1.Function, sizeof( DWORD ), dwProtect[0] , &dwProtect[1] ) == FALSE )
{
return( NULL );
}
}
if( bHooked == TRUE ) { break; }
pThunk++;
}
}
if( bHooked == TRUE ) { break; }
pImportDescriptor++;
}
if( bHooked == TRUE ) { return( fpFunction ); }
return( NULL );
}
BOOL UnhookImportFunction( HMODULE hModule, const char * szModuleName, const char * szFunctionName, DWORD dwHookFunction )
{
PIMAGE_NT_HEADERS pNtHeader = NULL;
PIMAGE_IMPORT_DESCRIPTOR pImportDescriptor = NULL;
PIMAGE_THUNK_DATA pThunk = NULL;
FARPROC fpFunction = NULL;
HMODULE hOwnerModule = GetModuleHandle( szModuleName );
DWORD dwProtect[2] = { 0 };
BOOL bUnhooked = FALSE;
if( (fpFunction = GetProcAddress( hOwnerModule, szFunctionName )) == NULL ) { return( FALSE ); }
if( ((PIMAGE_DOS_HEADER)hModule)->e_magic != IMAGE_DOS_SIGNATURE ) { return( FALSE ); }
pNtHeader = (PIMAGE_NT_HEADERS)((DWORD)hModule + (DWORD)((PIMAGE_DOS_HEADER)hModule)->e_lfanew);
if( pNtHeader->Signature != IMAGE_NT_SIGNATURE ) { return( FALSE ); }
pImportDescriptor = (PIMAGE_IMPORT_DESCRIPTOR)((DWORD)hModule + (DWORD)pNtHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress);
while( pImportDescriptor->Name != NULL )
{
if( stricmp( (char*)((DWORD)hModule + (DWORD)pImportDescriptor->Name), szModuleName ) == 0 )
{
if( pImportDescriptor->Name == NULL ) { return( FALSE ); }
pThunk = (PIMAGE_THUNK_DATA)((DWORD)hModule + (DWORD)pImportDescriptor->FirstThunk);
while( pThunk->u1.Function != NULL )
{
if( pThunk->u1.Function == (DWORD)dwHookFunction )
{
if( VirtualProtect( (LPVOID)&pThunk->u1.Function, sizeof( DWORD ), PAGE_EXECUTE_READWRITE, &dwProtect[0] ) == FALSE )
{
return( FALSE );
}
if( IsBadWritePtr( (LPVOID)&pThunk->u1.Function, sizeof( DWORD ) ) == 0 )
{
bUnhooked = TRUE;
pThunk->u1.Function = (DWORD)fpFunction;
}
if( VirtualProtect( (LPVOID)&pThunk->u1.Function, sizeof( DWORD ), dwProtect[0] , &dwProtect[1] ) == FALSE )
{
return( FALSE );
}
}
if( bUnhooked == TRUE ) { break; }
pThunk++;
}
}
if( bUnhooked == TRUE ) { break; }
pImportDescriptor++;
}
if( bUnhooked == TRUE ) { return( TRUE ); }
return( FALSE );
} |
_________________
- Retired. |
|