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 -> dll handle & patch byte @ specific o
Goto page 1, 2  Next
 
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: Tue Jun 17, 2008 4:38 pm    Post subject: [Delphi] Help -> dll handle & patch byte @ specific o Reply with quote

So i am creating a dll in Delphi which should do the following:

1. It will be injected manualy in a process
2. It will get a handle of a specific dll allready loaded in the process
3. Will patch some bytes in the dll victim


How can I get a handle of the victim dll and patch byte in a specific offset?

I could simply do the following......

Start new project >> select to be a dll file

between begin/end. insert the following line:

Code:
begin
asm
mov byte ptr [$10001234],$90  // i've just noped something
end;
end.



And when i inject it in the process if the victim dll has started with base address "1000" i will succeed the patching.

My problem is that the victim dll in the process starts allways at different position in the memory (for example "00F51234") how can i get a handle of it then?

Thank all of you!

_________________

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

Joined: 13 Nov 2006
Posts: 1900

PostPosted: Tue Jun 17, 2008 4:44 pm    Post subject: Reply with quote

CreateToolhelp32Snapshot
Module32First
Module32Next

http://msdn.microsoft.com/en-us/library/ms686849.aspx

_________________
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 Jun 17, 2008 5:08 pm    Post subject: Reply with quote

Thank you lurc, this gave me a big hint!

Later i should search for something like your example in delphi.

But here comes another question. Even if i get "where" was loaded "this time" the victim dll how can i write my "special byte" ?

I think i will not be able to use asm, or I am wrong?

_________________

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

Joined: 13 Nov 2006
Posts: 1900

PostPosted: Tue Jun 17, 2008 5:40 pm    Post subject: Reply with quote

If you inject a DLL into the process containing the module you will be able to use inline asm, else you'll have to use an API like WriteProcessMemory
_________________
Back to top
View user's profile Send private message
HalfPrime
Grandmaster Cheater
Reputation: 0

Joined: 12 Mar 2008
Posts: 532
Location: Right there...On your monitor

PostPosted: Tue Jun 17, 2008 6:03 pm    Post subject: Reply with quote

Once you get where it was loaded, you could do
(BYTE*)(modulebaseaddy+theoffset) = Whatyouwantthebytetobe;

_________________
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 472

Joined: 09 May 2003
Posts: 25871
Location: The netherlands

PostPosted: Tue Jun 17, 2008 6:40 pm    Post subject: Reply with quote

don't forget virtualprotect to make the memory writable

also, I think it should be this:
(BYTE*)(modulebaseaddy+theoffset)^ := Whatyouwantthebytetobe;

_________________
Do not ask me about online cheats. I don't know any and wont help finding them.

Like my help? Join me on Patreon so i can keep helping
Back to top
View user's profile Send private message MSN Messenger
HalfPrime
Grandmaster Cheater
Reputation: 0

Joined: 12 Mar 2008
Posts: 532
Location: Right there...On your monitor

PostPosted: Tue Jun 17, 2008 6:54 pm    Post subject: Reply with quote

the ^ would XOR it. In c++, atleast.
_________________
Back to top
View user's profile Send private message
samuri25404
Grandmaster Cheater
Reputation: 7

Joined: 04 May 2007
Posts: 955
Location: Why do you care?

PostPosted: Tue Jun 17, 2008 7:56 pm    Post subject: Reply with quote

HalfPrime wrote:
the ^ would XOR it. In c++, atleast.


That's C++; we're talking Delphi.

In Delphi, ^ is pointer stuff, I believe. Though, wouldn't it be

Code:

(PBYTE)(base + offset)^ := whatever


or

Code:

(BYTE^)(base + offset)^ := whatever


?

I'm a bit rusty, so forgive me if I'm off.

_________________
Wiccaan wrote:

Oh jeez, watchout I'm a bias person! Locked.


Auto Assembly Tuts:
In Depth Tutorial on AA
Extended
Back to top
View user's profile Send private message
rapion124
Grandmaster Cheater Supreme
Reputation: 0

Joined: 25 Mar 2007
Posts: 1095

PostPosted: Tue Jun 17, 2008 8:49 pm    Post subject: Reply with quote

Just do it in C++. Delphi isn't good with this kind of stuff.
Code:
 
*(BYTE *)(dwModuleAddy + dwOffset) = 0x90

Change "DWORD *" to whatever your size is and make appropriate changes to "0x90."
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 472

Joined: 09 May 2003
Posts: 25871
Location: The netherlands

PostPosted: Tue Jun 17, 2008 9:25 pm    Post subject: Reply with quote

Yes, read it wrong, this is the best way:
PBYTE(base + offset)^ := whatever;

_________________
Do not ask me about online cheats. I don't know any and wont help finding them.

Like my help? Join me on Patreon so i can keep helping
Back to top
View user's profile Send private message MSN Messenger
tombana
Master Cheater
Reputation: 2

Joined: 14 Jun 2007
Posts: 456
Location: The Netherlands

PostPosted: Wed Jun 18, 2008 2:33 am    Post subject: Reply with quote

To get the module base address of the victim dll:
GetModuleHandle("NameOfDll.dll")
If you don't know the name of the dll, then do what lurc said.
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 Jun 18, 2008 8:23 am    Post subject: Reply with quote

So far 50% of the entire job is done. I've got the following source:

Quote:
library Project2;

uses
// SysUtils,
// Classes,

windows;

{$R *.res}

var
i : integer;
begin
i := 0;
messagebox(0,'injecting dll...','blahahaha',0);
GetModuleHandle('victim.dll');
i := GetModuleHandle('victim.dll');
if i = 0 then
begin
messagebox(0,'seems that he handle of (victim.dll) was not made...','blahahaha',0)
end
else
begin
messagebox(0,'WOOOHOOOO ..... GOT HANDLE OF THE DLL! Very HappyVery HappyVery Happy','blahahaha',0);
// here i need the code to write the byte '$90' to offset '$1234' in my "victim.dll"
end;
end.


I've tryed the following line and it obviously did not work:

mov byte ptr [$1234],$90

==========================

I've looked for your sujestions and started looking for the following text in google: "PBYTE(base + offset)^ delphi"

I've seen some examples but they were not very helpful to me... seems that i am missing some knowledge.

At least i've stopped at the following website:

http://www.delphihelp.org/catch_dll_functions_calling.html

There is the following example:

Quote:
function MakePtr(base: Dword; Offset: DWORD): Pointer;
begin
Result := Pointer(Base + Offset);
end;



And i am still wondering how to use this method which I searched in google. Seems i need a raw example (copy/pasteable) so I could understand it to it's full.

_________________

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

Joined: 12 Mar 2008
Posts: 532
Location: Right there...On your monitor

PostPosted: Wed Jun 18, 2008 10:44 am    Post subject: Reply with quote

baseaddy += 1234;
mov byte ptr [$baseaddy],$90

_________________
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 Jun 18, 2008 1:20 pm    Post subject: Reply with quote

Well i think i am 80% ready now. I just need the rights to write memory. Because VirtualProtectEx returns false and i get the following error when the dll tries to write the patched memory...




The source:


Quote:
library Project2;

uses
// SysUtils,
// Classes,
windows;

{$R *.res}

var
i : integer;
u : bool;
dwModuleHandle : dword;
hProcess : THandle;
//ProcessID:cardinal;
rights : dword;
const
PatchAddy1 = $00474A42;
begin

i := 0;
messagebox(0,'injecting dll...','blahahaha',0);
GetModuleHandle('opengl32.dll');
i := GetModuleHandle('opengl32.dll');
if i = 0 then
begin
messagebox(0,'seems that he handle of (opengl32.dll) was not made...','blahahaha',0)
end
else
begin

messagebox(0,'WOOOHOOOO ..... GOT HANDLE OF THE DLL!','blahahaha',0);
dwModuleHandle := GetModuleHandle('opengl32.dll');
hProcess := OpenProcess(PROCESS_ALL_ACCESS,FALSE,dwModuleHandle);
VirtualProtectEx(hProcess, ptr(dwModuleHandle + ($1279)),sizeof(Patchaddy1),PAGE_EXECUTE_READWRITE,@Rights);
u := VirtualProtectEx(hProcess, ptr(dwModuleHandle + ($1279)),sizeof(Patchaddy1),PAGE_EXECUTE_READWRITE,@Rights);
if u = false then messagebox(0,'VirtualProtectEx did not succeed..','blahahaha',0);
PBYTE(dwModuleHandle + ($1279))^ := $90;
end;
end.


Where is my mistake? Sad

_________________

Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 472

Joined: 09 May 2003
Posts: 25871
Location: The netherlands

PostPosted: Wed Jun 18, 2008 1:43 pm    Post subject: Reply with quote

remove this line, it's causing a horrible bug in your code
Code:

hProcess := OpenProcess(PROCESS_ALL_ACCESS,FALSE,dwModuleHandle);

(and not only because hProcess is fed a boolean, I wonder why it even compiled)

Remove the Ex from VirtualProtectEx and remove the hProcess

this code will work:
Code:

dwModuleHandle := GetModuleHandle('opengl32.dll');
u:=VirtualProtect(PBYTE(dwModuleHandle + $1279), 1,PAGE_EXECUTE_READWRITE, Rights);
if not u then messagebox(0,'VirtualProtectEx did not succeed..','blahahaha',0);
PBYTE(dwModuleHandle + $1279)^ := $90;
VirtualProtect(PBYTE(dwModuleHandle + $1279), 1,Rights, Rights);

And are you sure about offset $1279 ?

----------------------------
Or your whole code written in a more readable state:
Code:

var
address: pbyte;
u: boolean;
dwModuleHandle: dword;
Rights: dword;
begin
  dwModuleHandle := GetModuleHandle('opengl32.dll');
  if dwModuleHandle<>nil then
  begin
    address:=PBYTE(dwModuleHandle + $1279);
    u:=VirtualProtect(address, 1,PAGE_EXECUTE_READWRITE, Rights);
    if not u then messagebox(0,'VirtualProtect did not succeed..','blahahaha',0);
    address^ := $90;
    VirtualProtect(address, 1,Rights, Rights);
  end else messagebox(0,'seems that he handle of (opengl32.dll) was not made...','blahahaha',0)
end;

_________________
Do not ask me about online cheats. I don't know any and wont help finding them.

Like my help? Join me on Patreon so i can keep helping


Last edited by Dark Byte on Wed Jun 18, 2008 4:19 pm; edited 1 time in total
Back to top
View user's profile Send private message MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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