samedi 27 juin 2015

Convert this C-Code to Delphi-Code

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

Aucun commentaire:

Enregistrer un commentaire