Hỏi về opengl hook

Em có đoạn code thế này nhưng khi injected vào 1 ct nào đó thì bị crash .Ai đó có thể cho em biết nó sai ở đâu được không ạ. Em xin cảm ơn ! :smiley:

library test;
      {$mode delphi}
uses
    SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, Interfaces,
   Classes,
   Windows;
Var
pglBegin : procedure(mode:integer);
procedure DLLEntryPoint(dwReason: DWord);
begin
  case dwReason of
    DLL_PROCESS_ATTACH: ;
    DLL_PROCESS_DETACH: ;
    DLL_THREAD_ATTACH:;
  end;
end;
procedure g_glBegin( mode:integer) ;
begin
    pglBegin(mode) ;

end;

function DetourFunction (src, dst: Pointer; len: Integer): Pointer;
var
  jmp     : PByteArray;
  dwBack  : LongWord;
  jmpaddr : PtrUInt;
  srcarr  : PByteArray;
  i       : Integer;
begin
  srcarr := PByteArray(src);
  GetMem(jmp, len + 5);
  VirtualProtect(src, len, PAGE_EXECUTE_READWRITE, @dwBack);
  Move(src^, jmp^, len);
  jmp^[len]:=$e9;

  jmpaddr:=PtrUInt(@jmp^[len]);
  PPtrUInt( @jmp^[len+1])^ := PtrUInt(src)+PtrUInt(len) - jmpaddr - 5;

  srcarr^[0]:=$e9;
  PPtrUInt( @srcarr^[1] )^ := PtrUInt(dst) - PtrUInt(src) - 5;

  for i:=5 to len-1 do srcarr^[i]:=$90;

  VirtualProtect(src, len, dwBack, @dwBack);
  Result:=jmp;
end;

procedure Hook_opengl();
var
  HInst: HMODULE;

begin
      HInst := GetModuleHandle('opengl32.dll');
      @pglBegin:=DetourFunction(GetProcAddress(HInst,'glBegin'),@g_glBegin,6);
end;
procedure Process_Detach_Hook(dllparam : longint);
begin
  DLLEntryPoint(DLL_PROCESS_DETACH);
end;
procedure Thread_Attach_Hook(dllparam : longint);
begin
  DLLEntryPoint(DLL_THREAD_ATTACH);
end;

procedure Thread_Detach_Hook(dllparam : longint);

begin
  Hook_opengl();
  DLLEntryPoint(DLL_THREAD_DETACH);
end;
begin

  {$ifdef fpc}
    Dll_Process_Detach_Hook:= @Process_Detach_Hook;
    Dll_Thread_Attach_Hook := @Thread_Attach_Hook;
    Dll_Thread_Detach_Hook := @Thread_Detach_Hook;
    //Dll_Process_Detach_Hook := @Process_Attach_Hook;

  {$else }
    DLLProc:= @DLLEntryPoint;
  {$endif}
  DLLEntryPoint(DLL_PROCESS_ATTACH);
end.
[/quote]


library test;
      {$mode delphi}
uses
    SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, Interfaces,
   Classes,
   Windows;
Var
pglBegin : procedure(mode:integer);
procedure DLLEntryPoint(dwReason: DWord);
begin
  case dwReason of
    DLL_PROCESS_ATTACH: ;
    DLL_PROCESS_DETACH: ;
    DLL_THREAD_ATTACH:;
  end;
end;
procedure g_glBegin( mode:integer) ;
begin
    pglBegin(mode) ;

end;

function DetourFunction (src, dst: Pointer; len: Integer): Pointer;
var
  jmp     : PByteArray;
  dwBack  : LongWord;
  jmpaddr : PtrUInt;
  srcarr  : PByteArray;
  i       : Integer;
begin
  srcarr := PByteArray(src);
  GetMem(jmp, len + 5);
  VirtualProtect(src, len, PAGE_EXECUTE_READWRITE, @dwBack);
  Move(src^, jmp^, len);
  jmp^[len]:=$e9;

  jmpaddr:=PtrUInt(@jmp^[len]);
  PPtrUInt( @jmp^[len+1])^ := PtrUInt(src)+PtrUInt(len) - jmpaddr - 5;

  srcarr^[0]:=$e9;
  PPtrUInt( @srcarr^[1] )^ := PtrUInt(dst) - PtrUInt(src) - 5;

  for i:=5 to len-1 do srcarr^[i]:=$90;

  VirtualProtect(src, len, dwBack, @dwBack);
  Result:=jmp;
end;

procedure Hook_opengl();
var
  HInst: HMODULE;

begin
      HInst := GetModuleHandle('opengl32.dll');
      @pglBegin:=DetourFunction(GetProcAddress(HInst,'glBegin'),@g_glBegin,6);
end;
procedure Process_Detach_Hook(dllparam : longint);
begin
  DLLEntryPoint(DLL_PROCESS_DETACH);
end;
procedure Thread_Attach_Hook(dllparam : longint);
begin
  DLLEntryPoint(DLL_THREAD_ATTACH);
end;

procedure Thread_Detach_Hook(dllparam : longint);

begin
  Hook_opengl();
  DLLEntryPoint(DLL_THREAD_DETACH);
end;
begin

  {$ifdef fpc}
    Dll_Process_Detach_Hook:= @Process_Detach_Hook;
    Dll_Thread_Attach_Hook := @Thread_Attach_Hook;
    Dll_Thread_Detach_Hook := @Thread_Detach_Hook;
 

  {$else }
    DLLProc:= @DLLEntryPoint;
  {$endif}
  DLLEntryPoint(DLL_PROCESS_ATTACH);
end.
1 Like

không liên quan nhưng:
Bỏ code vào markdown đi bạn
Bạn đi theo hướng Pas/Del thật á? :scream:

à không :v em chỉ muốn tìm hiểu về nó thôi :smiley: ( )

1 Like
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?