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 dll inject blocker] adding exception(s) 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: Wed Aug 13, 2008 10:56 am    Post subject: [Delphi dll inject blocker] adding exception(s) dll Reply with quote

Here is the source for the absolute blocker:

Code:
procedure hook(target, newfunc:pointer);
var
  jmpto:dword;
    OldProtect: Cardinal; // old protect in memory
begin
  jmpto:=dword(newfunc)-dword(target)-5;
  VirtualProtect(target, 5, PAGE_EXECUTE_READWRITE, @OldProtect);
  pbyte(target)^:=$e9;
  pdword(dword(target)+1)^:=jmpto;
end;

procedure myLdrLoadDll(PathToFile:PAnsiChar; Flags:variant; ModuleFileName:PAnsiChar; var ModuleHandle:THandle);
begin
  MessageBox(0, 'I have blocked your attempt to inject a dll file!!', 'WARNING!', MB_OK);
  ModuleHandle:=0;
end;

procedure Main;
begin
Hook(GetProcAddress(GetModuleHandle('ntdll.dll'), 'LdrLoadDll'), @myLdrLoadDll);
end;


begin
end.


I've found this example in the internet and it is pretty good. They said it blocks even "loadlibrary".

My question is would it be easy to add exceptions for system dlls or dlls which the application loads because they are all blocked too. Will the code be huge? Can anyone give me hints about how/what needs to be done?

Thank you very much.

_________________

Back to top
View user's profile Send private message
jackyyll
Expert Cheater
Reputation: 0

Joined: 28 Jan 2008
Posts: 143
Location: here

PostPosted: Wed Aug 13, 2008 11:26 am    Post subject: Reply with quote

Uhh.. Someone could just open your exe you have this code in and remove it? Or, they could edit the file and have it jump to a code cave before any of the files code gets executed and call LoadLibraryA to load their DLL instead of injecting.. And since it'd be executed first you can't really block that.
Back to top
View user's profile Send private message AIM Address MSN Messenger
dnsi0
I post too much
Reputation: 0

Joined: 04 Jan 2007
Posts: 2674

PostPosted: Wed Aug 13, 2008 11:39 am    Post subject: Reply with quote

These hooks are not really good

Code:
Function LdrLoadDllX(PathToFile:PAnsiChar; Flags:variant;ModuleFileName:PAnsiChar; var ModuleHandle:THandle):bool;
var DblWord:DWORD;
    hHandle:THandle;
begin
hHandle:=LoadLibrary('ntdll.dll');
DblWord:=DWORD(GetProcAddress(hHandle,'LdrLoadDll'))+5;
 asm
 mov edi,edi
 push ebp
 mov esp,ebp
 jmp [DblWord]
 end;
end;


Havent tested but this code bypasses the hook. So... Whats the point? Unless you can do kernel hooks.

To answer your question. All you have to do is loop through all the sys dll functions and hook them.

Then your program becomes GameGuard. You have to iunject your dll into every process for it to work.
Back to top
View user's profile Send private message
lurc
Grandmaster Cheater Supreme
Reputation: 2

Joined: 13 Nov 2006
Posts: 1900

PostPosted: Wed Aug 13, 2008 11:54 am    Post subject: Reply with quote

Of course it blocks LoadLibrary... LoadLibraryW makes a call directly to that function at some point.

And yes you can add exceptions... Just add a couple compares with the ModuleFileName and if they don't match block the attempt.

dnsi0 wrote:
These hooks are not really good

Code:
Function LdrLoadDllX(PathToFile:PAnsiChar; Flags:variant;ModuleFileName:PAnsiChar; var ModuleHandle:THandle):bool;
var DblWord:DWORD;
    hHandle:THandle;
begin
hHandle:=LoadLibrary('ntdll.dll');
DblWord:=DWORD(GetProcAddress(hHandle,'LdrLoadDll'))+5;
 asm
 mov edi,edi
 push ebp
 mov esp,ebp
 jmp [DblWord]
 end;
end;


Havent tested but this code bypasses the hook. So... Whats the point? Unless you can do kernel hooks.

To answer your question. All you have to do is loop through all the sys dll functions and hook them.

Then your program becomes GameGuard. You have to iunject your dll into every process for it to work.


No that code does not bypass the hook. Do you think every single function starts with the hot-patch prefix?

_________________
Back to top
View user's profile Send private message
dnsi0
I post too much
Reputation: 0

Joined: 04 Jan 2007
Posts: 2674

PostPosted: Wed Aug 13, 2008 12:26 pm    Post subject: Reply with quote

ok. SO I have to see what gg actucally hooks before I patch it.


And I found out how to debug gg. XD

All you ahve to do is make a dll that freezes it and then attach a debugger.



debugger.JPG
 Description:
 Filesize:  91.82 KB
 Viewed:  12377 Time(s)

debugger.JPG




Last edited by dnsi0 on Wed Aug 13, 2008 12:33 pm; edited 1 time in total
Back to top
View user's profile Send private message
lurc
Grandmaster Cheater Supreme
Reputation: 2

Joined: 13 Nov 2006
Posts: 1900

PostPosted: Wed Aug 13, 2008 12:29 pm    Post subject: Reply with quote

dnsi0 wrote:
ok. SO I have to see what gg actucally hooks before I patch it.


And I found out how to debug gg. XD

All you ahve to do is make a dll that freezes it and then attach a debugger.


Wrong again.
You have to find out the initial bytes of the function. Rolling Eyes

And.. good luck with that.

_________________
Back to top
View user's profile Send private message
dnsi0
I post too much
Reputation: 0

Joined: 04 Jan 2007
Posts: 2674

PostPosted: Wed Aug 13, 2008 12:33 pm    Post subject: Reply with quote

There I posted a screeny.

And thats what I ment o.o. Find where the function starts o.o
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: Wed Aug 13, 2008 1:12 pm    Post subject: Reply with quote

Thank you lurc. Your idea about ModuleFileName seems good.

But i have some questions.

I've tried adding in procedure myLdrLoadDll
showmessage(modulefilename);

To see if in the procedure i can get the name of the current injected dll but it does not show anything. At that moment i realised that at my procedure i can't get the name of it.

So it should be done in:

procedure Main; ... before or after the hooking or any other way?

_________________

Back to top
View user's profile Send private message
lurc
Grandmaster Cheater Supreme
Reputation: 2

Joined: 13 Nov 2006
Posts: 1900

PostPosted: Wed Aug 13, 2008 2:00 pm    Post subject: Reply with quote

Edit: Nvm.
_________________


Last edited by lurc on Wed Aug 13, 2008 2:35 pm; edited 1 time in total
Back to top
View user's profile Send private message
Zand
Master Cheater
Reputation: 0

Joined: 21 Jul 2006
Posts: 424

PostPosted: Wed Aug 13, 2008 2:27 pm    Post subject: Reply with quote

Actually CreateRemoteThread in DLL injection is used by creating a thread that runs LoadLibrary...
Back to top
View user's profile Send private message
lurc
Grandmaster Cheater Supreme
Reputation: 2

Joined: 13 Nov 2006
Posts: 1900

PostPosted: Wed Aug 13, 2008 2:35 pm    Post subject: Reply with quote

Zand wrote:
Actually CreateRemoteThread in DLL injection is used by creating a thread that runs LoadLibrary...


Rofl, oh yea, I somehow completely forgot about the WriteProcessMemory part of that... Ignore what i said before.

_________________
Back to top
View user's profile Send private message
rapion124
Grandmaster Cheater Supreme
Reputation: 0

Joined: 25 Mar 2007
Posts: 1095

PostPosted: Wed Aug 13, 2008 7:35 pm    Post subject: Reply with quote

Hooking LoadLibraryExW and blocking ALL modules from loading does not work. You should do some filtering of DLL names.

To add exception handling, simply enclose the statements in a try..except block...
Back to top
View user's profile Send private message
Renkokuken
GO Moderator
Reputation: 4

Joined: 22 Oct 2006
Posts: 3249

PostPosted: Wed Aug 13, 2008 8:29 pm    Post subject: Reply with quote

Answered via MSN, I'll also post source if anyone is interested in seeing.
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: Thu Aug 14, 2008 5:06 am    Post subject: Reply with quote

Groot, if you read my thread i am trying to achieve "exceptions for system dll files and so on..." Smile so it will not be pointless.
_________________

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