Direct C/C++ to PB Translation Help
Posted: Thu Jun 05, 2008 6:54 pm
I've tried to manually translate this function from C/C++ to PB but the procedure fails
Heres the Original C/C++ Code:
Here's my PB Code Translation:
Hope someone could help me.
Thanks.

Heres the Original C/C++ Code:
Code: Select all
// This function "replaces" a function with another function
// So, for example, if you do this:
// OriginalWSASendProc = (MyWSASendProc) HookImportedFunction (GetModuleHandle (0), "WS2_32.DLL", "WSASend", (PROC) MyWSASend);
// This will "replaces" WSASend() with MyWSASend(). Every time the app calls WSASend(), MyWSASend() gets called instead.
// This function returns a pointer to the original function.
PROC
HookImportedFunction (HMODULE hModule, // Module to intercept calls from
PSTR FunctionModule, // The dll file that contains the function you want to hook
PSTR FunctionName, // The function that you want to hook
PROC pfnNewProc) // New function, this gets called instead
{
#define MakePtr( cast, ptr, addValue ) (cast)( (DWORD)(ptr)+(DWORD)(addValue))
PROC pfnOriginalProc;
IMAGE_DOS_HEADER *pDosHeader;
IMAGE_NT_HEADERS *pNTHeader;
IMAGE_IMPORT_DESCRIPTOR *pImportDesc;
IMAGE_THUNK_DATA *pThunk;
if (IsBadCodePtr (pfnNewProc)) return NULL;
if (OriginalGetProcAddressProc) {
pfnOriginalProc = OriginalGetProcAddressProc(GetModuleHandle(FunctionModule), FunctionName);
} else {
pfnOriginalProc = GetProcAddress(GetModuleHandle(FunctionModule), FunctionName);
}
if(!pfnOriginalProc) return NULL;
pDosHeader = (PIMAGE_DOS_HEADER)hModule;
if ( IsBadReadPtr(pDosHeader, sizeof(IMAGE_DOS_HEADER)) )
return NULL;
if ( pDosHeader->e_magic != IMAGE_DOS_SIGNATURE )
return NULL;
pNTHeader = MakePtr(PIMAGE_NT_HEADERS, pDosHeader, pDosHeader->e_lfanew);
if ( IsBadReadPtr(pNTHeader, sizeof(IMAGE_NT_HEADERS)) )
return NULL;
if ( pNTHeader->Signature != IMAGE_NT_SIGNATURE )
return NULL;
pImportDesc = MakePtr(PIMAGE_IMPORT_DESCRIPTOR, pDosHeader,
pNTHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress);
if ( pImportDesc == (PIMAGE_IMPORT_DESCRIPTOR)pNTHeader )
return NULL;
while ( pImportDesc->Name ) {
PSTR pszModName = MakePtr(PSTR, pDosHeader, pImportDesc->Name);
if ( stricmp(pszModName, FunctionModule) == 0 )
break;
pImportDesc++;
}
pNTHeader = MakePtr(PIMAGE_NT_HEADERS, pDosHeader, pDosHeader->e_lfanew);
if ( pImportDesc->Name == 0 )
return 0;
pThunk = MakePtr(PIMAGE_THUNK_DATA, pDosHeader, pImportDesc->FirstThunk);
MEMORY_BASIC_INFORMATION mbi_thunk;
while ( pThunk->u1.Function ) {
if ( (DWORD)pThunk->u1.Function == (DWORD)pfnOriginalProc) {
VirtualQuery(pThunk, &mbi_thunk, sizeof(MEMORY_BASIC_INFORMATION));
if (FALSE == VirtualProtect(mbi_thunk.BaseAddress, mbi_thunk.RegionSize, PAGE_READWRITE, &mbi_thunk.Protect))
return NULL;
DWORD * pTemp = (DWORD*)&pThunk->u1.Function;
*pTemp = (DWORD)(pfnNewProc);
VirtualProtect(mbi_thunk.BaseAddress, mbi_thunk.RegionSize,mbi_thunk.Protect, NULL);
break;
}
pThunk++;
}
SYSTEM_INFO si;
DWORD i;
byte *data = NULL;
GetSystemInfo(&si);
LPVOID lpMem = si.lpMinimumApplicationAddress;
while (lpMem < si.lpMaximumApplicationAddress) {
VirtualQuery(lpMem, &mbi_thunk,sizeof(MEMORY_BASIC_INFORMATION));
if ((DWORD)mbi_thunk.BaseAddress <= (DWORD)pDosHeader + pNTHeader->OptionalHeader.SizeOfImage
&& mbi_thunk.State == MEM_COMMIT && mbi_thunk.RegionSize > 0 && !(mbi_thunk.Protect & PAGE_GUARD)) {
if (VirtualProtect(mbi_thunk.BaseAddress, mbi_thunk.RegionSize, PAGE_READWRITE, &mbi_thunk.Protect)) {
data = (byte*)mbi_thunk.BaseAddress;
for (i = 0; i < mbi_thunk.RegionSize - 3; i++) {
if (*(DWORD*)(data+i) == (DWORD)pfnOriginalProc) {
*(DWORD*)(data+i) = (DWORD)pfnNewProc;
}
}
VirtualProtect(mbi_thunk.BaseAddress, mbi_thunk.RegionSize,mbi_thunk.Protect, NULL);
}
}
lpMem = MakePtr(LPVOID, mbi_thunk.BaseAddress, mbi_thunk.RegionSize+1);
}
return pfnOriginalProc;
}
Code: Select all
Procedure.l HookImportedFunction(hModule.l, FunctionModule.s, FunctionName.s, pfnNewProc.l)
Define pfnOriginalProc.l
*pDosHeader.IMAGE_DOS_HEADER
*pNTHeader.IMAGE_NT_HEADERS
*pImportDesc.IMAGE_IMPORT_DESCRIPTOR
*pThunk.IMAGE_THUNK_DATA
If IsBadCodePtr_(pfnNewProc)
ProcedureReturn 0
EndIf
pfnOriginalProc = GetProcAddress_(GetModuleHandle_(FunctionModule), FunctionName)
If Not pfnOriginalProc
ProcedureReturn 0
EndIf
*pDosHeader = hModule
If IsBadReadPtr_(*pDosHeader, SizeOf(IMAGE_DOS_HEADER))
ProcedureReturn 0
EndIf
If Not *pDosHeader\e_magic = IMAGE_DOS_SIGNATURE
;FAILED
ProcedureReturn 0
EndIf
; #define MakePtr( cast, ptr, addValue ) (cast)( (DWORD)(ptr)+(DWORD)(addValue))
; pNTHeader = MakePtr(PIMAGE_NT_HEADERS, pDosHeader, pDosHeader->e_lfanew);
; pNTHeader = (PIMAGE_NT_HEADERS)((DWORD)pDosHeader+(DWORD)pDosHeader->e_lfanew);
*pNTHeader = *pDosHeader + *pDosHeader\e_lfanew
If IsBadReadPtr_(*pNTHeader, SizeOf(IMAGE_NT_HEADERS))
ProcedureReturn 0
EndIf
If Not *pNTHeader\Signature = IMAGE_NT_SIGNATURE
;FAILED
ProcedureReturn 0
EndIf
; #define MakePtr( cast, ptr, addValue ) (cast)( (DWORD)(ptr)+(DWORD)(addValue))
; pImportDesc = MakePtr(PIMAGE_IMPORT_DESCRIPTOR, pDosHeader, pNTHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress);
; pImportDesc = (PIMAGE_IMPORT_DESCRIPTOR)((DWORD)pDosHeader+(DWORD)pNTHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress);
*pImportDesc = *pDosHeader + *pNTHeader\OptionalHeader\DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT]\VirtualAddress
If *pImportDesc = *pNTHeader
ProcedureReturn 0
EndIf
While *pImportDesc\Name
; #define MakePtr( cast, ptr, addValue ) (cast)( (DWORD)(ptr)+(DWORD)(addValue))
; PSTR pszModName = MakePtr(PSTR, pDosHeader, pImportDesc->Name);
; PSTR pszModName = (PSTR)((DWORD)pDosHeader+(DWORD)pImportDesc->Name)
; while ( pImportDesc->Name ) {
; PSTR pszModName = MakePtr(PSTR, pDosHeader, pImportDesc->Name);
; if ( stricmp(pszModName, FunctionModule) == 0 )
; break;
; pImportDesc++;
; }
; Can't translate this block...
; PSTR - Pointer to String?
;Define *pszModName.l = *pDosHeader + *pImportDesc\Name
;If MyStrCmp(pszModName, FunctionModule)
;Break
;EndIf
*pImportDesc = *pImportDesc + 1
Wend
; #define MakePtr( cast, ptr, addValue ) (cast)( (DWORD)(ptr)+(DWORD)(addValue))
; pNTHeader = MakePtr(PIMAGE_NT_HEADERS, pDosHeader, pDosHeader->e_lfanew);
; pNTHeader = (PIMAGE_NT_HEADER)((DWORD)pDosHeader+(DWORD)pDosHeader->e_lfanew));
*pNTHeader = *pDosHeader + *pDosHeader\e_lfanew
If *pImportDesc\Name = 0
;FAILED
ProcedureReturn 0
EndIf
; #define MakePtr( cast, ptr, addValue ) (cast)( (DWORD)(ptr)+(DWORD)(addValue))
; pThunk = MakePtr(PIMAGE_THUNK_DATA, pDosHeader, pImportDesc->FirstThunk);
; pThunk = (PIMAGE_THUNK_DATA)((DWORD)pDosHeader+(DWORD)pImportDesc->FirstThunk);
*pThunk = *pDosHeader + *pImportDesc\FirstThunk
mbi_thunk.MEMORY_BASIC_INFORMATION
While *pThunk\u1\Function
If *pThunk\u1\Function = pfnOriginalProc
VirtualQuery_(pThunk, @mbi_thunk, SizeOf(MEMORY_BASIC_INFORMATION))
If Not VirtualProtect_(mbi_thunk\BaseAddress, mbi_thunk\RegionSize, PAGE_READWRITE, @mbi_thunk\Protect)
ProcedureReturn 0
EndIf
Define *pTemp.l = @*pThunk\u1\Function
*pTemp = *pfnNewProc
VirtualProtect_(mbi_thunk\BaseAddress, mbi_thunk\RegionSize, mbi_thunk\Protect, 0)
Break
EndIf
*pThunk = *pThunk + 1
Wend
si.SYSTEM_INFO
Define i.l = 0
Define *dataz.b = 0
GetSystemInfo_(@si)
Define lpMem = si\lpMinimumApplicationAddress
While lpMem < si\lpMaximumApplicationAddress
VirtualQuery_(lpMem, @mbi_thunk, SizeOf(MEMORY_BASIC_INFORMATION))
If mbi_thunk\BaseAddress <= (*pDosHeader + *pNTHeader\OptionalHeader\SizeOfImage) And mbi_thunk\State = MEM_COMMIT And mbi_thunk\RegionSize > 0 And Not (mbi_thunk\Protect And PAGE_GUARD)
If VirtualProtect_(mbi_thunk\BaseAddress, mbi_thunk\RegionSize, PAGE_READWRITE, @mbi_thunk\Protect)
; Data = (byte*)mbi_thunk.BaseAddress;
*dataz = mbi_thunk\BaseAddress
While i < mbi_thunk\RegionSize - 3
; if (*(DWORD*)(data+i) == (DWORD)pfnOriginalProc) {
; *(DWORD*)(data+i) = (DWORD)pfnNewProc;
; }
If *dataz + i = pfnOriginalProc
*dataz + i = pfnNewProc
EndIf
i = i + 1
Wend
VirtualProtect_(mbi_thunk\BaseAddress, mbi_thunk\RegionSize, mbi_thunk\Protect, 0)
EndIf
EndIf
; #define MakePtr( cast, ptr, addValue ) (cast)( (DWORD)(ptr)+(DWORD)(addValue))
; lpMem = MakePtr(LPVOID, mbi_thunk.BaseAddress, mbi_thunk.RegionSize+1);
; lpMem = (LPVOID)((DWORD)mbi_thunk.BaseAddress+(DWORD)mbi_thunk.RegionSize+1);
lpMem = mbi_thunk\BaseAddress + mbi_thunk\RegionSize + 1
Wend
ProcedureReturn pfnOriginalProc
EndProcedure
Thanks.