I need to convert this C-Code to Delphi-Code and because my Delphi-Knowledge is not good enough I need your help!
My main problem is, that I don't know how to cast pointers / calculate with pointers in Delphi.
Of course i tried to convert it and for whoever is familiar with both languages should this be easy to do :)
Original code (C):
void* GetPayloadExportAddr( LPCWSTR lpPath, HMODULE hPayloadBase, LPCSTR lpFunctionName ) {
// Load payload in our own virtual address space
HMODULE hLoaded = LoadLibrary( lpPath );
if( hLoaded == NULL ) {
return NULL;
} else {
void* lpFunc = GetProcAddress( hLoaded, lpFunctionName );
DWORD dwOffset = (char*)lpFunc - (char*)hLoaded;
FreeLibrary( hLoaded );
return (DWORD)hPayloadBase + dwOffset;
}
}
and
BOOL InitPayload( HANDLE hProcess, LPCWSTR lpPath, HMODULE hPayloadBase, HWND hwndDlg ) {
void* lpInit = GetPayloadExportAddr( lpPath, hPayloadBase, "Init" );
if( lpInit == NULL ) {
return FALSE;
} else {
HANDLE hThread = CreateRemoteThread( hProcess, NULL, 0,
lpInit, hwndDlg, 0, NULL );
if( hThread == NULL ) {
return FALSE;
} else {
CloseHandle( hThread );
}
}
And the partally converted Delphicode:
function GetPayloadExportAddr( lpPath: LPCWSTR; hPayloadBase: HMODULE; lpFunctionName: LPCWSTR) : Pointer;
var
hLoaded: HMODULE;
lpFunc: pointer;
dwOffset: DWORD;
begin
hLoaded := LoadLibrary( lpPath );
if( hLoaded = 0 ) then
begin
Result := 0;
end
else
begin
lpFunc := GetProcAddress( hLoaded, lpFunctionName );
dwOffset := DWORD(PCHAR(lpFunc) - PCHAR(hLoaded));
FreeLibrary( hLoaded );
Result := PDWORD(DWORD(hPayloadBase) + dwOffset);
end;
end;
and
procedure CallStopHack( hProcess: THandle; lpPath: LPCWSTR; hPayloadBase: HMODULE);
var
lpInit : Pointer;
hThread: THandle;
bla:Cardinal;
begin
lpInit := GetPayloadExportAddr(lpPath, hPayloadBase, 'StopSpeedhack');
if( lpInit = nil ) then
begin
Exit;
end
else
begin
hThread := CreateRemoteThread( hProcess, nil, 0,
lpInit, 0, 0, bla);
if( hThread = 0 ) then
begin
Exit;
end
else
begin
CloseHandle( hThread );
end;
end;
end;
I assume that I messed up with the PDWORD()-Cast etc. I'm sorry but I don't know how to cast it correctly.
Thanks in advance! Regards
This should do:
dwOffset := DWORD(lpFunc) - hLoaded;
lpFunc is already a pointer and all you want is the address, hLoaded is already a NativeUint.
and
Result := Ptr(hPayloadBase + dwOffset);
I see a few problems. LPCWSTR
and LPCSTR
are not the same. The first translates to PWideChar
, while the second finally translates to PAnsiChar
(or MarshaledAString
, which is the same).
Also, if you are using Delphi 2009 or higher, you should not use PChar
to cast for pointer math. Use either PByte
or PAnsiChar
, as these are single-byte types.
Note, however, that in Delphi, handles are integral types already, so there is no need to cast them at all:
dwOffset := NativeUInt(lpFunc) - hLoaded;
And later on:
Result := Pointer(hPayloadBase + dwOffset);
Note that I find code that tries to read an offset by subtracting a handle from a pointer (and then uses that offset to add to another handle) rather suspicious. It may work, but it looks like a terrible hack to me.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.