Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[Delphi] Help with API Hooking DLL

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
h4c0r-BG
Master Cheater
Reputation: 0

Joined: 29 Nov 2006
Posts: 449
Location: The yogurt country

PostPosted: Mon Aug 11, 2008 11:45 am    Post subject: [Delphi] Help with API Hooking DLL Reply with quote

I have been browsing google and found this example. Which is supposed to hook a API and whenever Sleep is called to redirect to my own code.

This is the source:

Quote:
library Project2;

uses
windows; //reduce size Smile

{$R *.res}

procedure hook(target, newfunc:pointer);
var
jmpto:dword;
begin
jmpto:=dword(newfunc)-dword(target)-5;
VirtualProtect(target, 5, PAGE_EXECUTE_READWRITE, nil);
pbyte(target)^:=$e9;
pdword(dword(target)+1)^:=jmpto;
end;

procedure MySleep(time:dword);
begin
MessageBox(0, 'You have called "Sleep!"', 'Good!', MB_OK);
end;

begin
hook(GetProcAddress(GetModuleHandle('kernel32.dll'), 'Sleep'), @MySleep);
end.


And i have the following problem when I inject it in absolutely any process with the same offsets and value (note that after the error the application does not crash but the redirection does not work):



What may be causing the problem and can anybody give me hint(s) or tell me what to fix? Very Happy
Thank you for spending time for reading my thread. Wish you everything good.

_________________

Back to top
View user's profile Send private message
Renkokuken
GO Moderator
Reputation: 4

Joined: 22 Oct 2006
Posts: 3249

PostPosted: Mon Aug 11, 2008 12:37 pm    Post subject: Reply with quote

Look at your VirtualProtect call, then at MSDN's documentation on the final parameter.

MSDN wrote:
lpflOldProtect[out]: A pointer to a variable that receives the previous access protection value of the first page in the specified region of pages. If this parameter is NULL or does not point to a valid variable, the function fails.


Also, it's smart to replace the old protection on the memory region you alter as well.
Back to top
View user's profile Send private message
&Vage
Grandmaster Cheater Supreme
Reputation: 0

Joined: 25 Jul 2008
Posts: 1053

PostPosted: Mon Aug 11, 2008 12:47 pm    Post subject: Reply with quote

Code:

DWORD dwOldProt;
   VirtualProtect((LPVOID*)0x7C802442, 0x5, PAGE_EXECUTE_READWRITE, &dwOldProt);
Back to top
View user's profile Send private message
h4c0r-BG
Master Cheater
Reputation: 0

Joined: 29 Nov 2006
Posts: 449
Location: The yogurt country

PostPosted: Mon Aug 11, 2008 1:44 pm    Post subject: Reply with quote

Renkokuken, thank you very much. The bold text really helped me. Smile

Fix code:

Quote:
//add
var
OldProtect: Cardinal;
//edit
VirtualProtect(target, 5, PAGE_EXECUTE_READWRITE, @OldProtect);


Hope I helped somebody who likes Copy/Paste. Smile

_________________

Back to top
View user's profile Send private message
Renkokuken
GO Moderator
Reputation: 4

Joined: 22 Oct 2006
Posts: 3249

PostPosted: Mon Aug 11, 2008 2:29 pm    Post subject: Reply with quote

:) Glad to help.
Back to top
View user's profile Send private message
rapion124
Grandmaster Cheater Supreme
Reputation: 0

Joined: 25 Mar 2007
Posts: 1095

PostPosted: Tue Aug 12, 2008 8:15 am    Post subject: Reply with quote

The code snippet is an incomplete example of API hooking. Normally, you must return execution control to the API via a trampoline. I assume this is only an example, because the thread that called Sleep() will throw an exception.
Back to top
View user's profile Send private message
lurc
Grandmaster Cheater Supreme
Reputation: 2

Joined: 13 Nov 2006
Posts: 1900

PostPosted: Tue Aug 12, 2008 10:21 am    Post subject: Reply with quote

_void_ wrote:
Code:

DWORD dwOldProt;
   VirtualProtect((LPVOID*)0x7C802442, 0x5, PAGE_EXECUTE_READWRITE, &dwOldProt);


You do know the first paramater is LPVOID (void*).
Using LPVOID* casts as a void** which will cause errors.

_________________
Back to top
View user's profile Send private message
h4c0r-BG
Master Cheater
Reputation: 0

Joined: 29 Nov 2006
Posts: 449
Location: The yogurt country

PostPosted: Tue Aug 12, 2008 2:21 pm    Post subject: Reply with quote

rapion124 wrote:
The code snippet is an incomplete example of API hooking. Normally, you must return execution control to the API via a trampoline. I assume this is only an example, because the thread that called Sleep() will throw an exception.


It is really an example and i am sure it wouldn't be complete, can you show me an example what were you trying to explain? Smile Thank you.

_________________

Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites