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 


C++ Code Injection

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General Gamehacking
View previous topic :: View next topic  
Author Message
PuckaHuck
Newbie cheater
Reputation: 0

Joined: 01 Nov 2015
Posts: 11

PostPosted: Sun Jan 03, 2016 3:49 pm    Post subject: C++ Code Injection Reply with quote

Hi,

I basically want to make a program in C++ that does the same thing as the Auto Assembler in Cheat Engine (With the code injection). I would really like to do this without injecting any DLL, basically I just want to insert some assembly code to a codecave inside the process, and then jump to there from a specific address that I already have.

I have not been able to find so much about this in C++, so I was hoping you could help on how I could do this. Thanks! Smile

#Edit: I figured I have to use WriteProcessMemory and VirtualAllocEx, but I have not yet figured out how to perform the jump to the codecave and back to "where I came from".
Back to top
View user's profile Send private message
Gniarf
Grandmaster Cheater Supreme
Reputation: 43

Joined: 12 Mar 2012
Posts: 1285

PostPosted: Sun Jan 03, 2016 7:39 pm    Post subject: Reply with quote

Do do a jump in a 32bit program, write E9 (byte) followed by {address_where_you_jump_to - address_where_you_jump_from - 5} as a 32 bit int.
The -5 comes from the size of a jmp instruction: 5 bytes.

_________________
DO NOT PM me if you want help on making/fixing/using a hack.
Back to top
View user's profile Send private message
mgostIH
Expert Cheater
Reputation: 3

Joined: 01 Jan 2016
Posts: 159

PostPosted: Mon Jan 04, 2016 5:51 am    Post subject: Reply with quote

I usually use a call (Opcode E8 instead of E9) because it's sure it will take up 5 bytes and it's easy to return to: just put a ret (opcode C3) at the end of your assembly script.

Here's what I use for calculating the jmp gniarf was talking about:
Code:

DWORD calcjmp(PVOID Address,DWORD jmpto){
DWORD RelAddress = (DWORD) (jmpto - (DWORD)Address)-5;
return RelAddress;
}


Seems a little messy with those (DWORD) because VirtualAllocEx returns a PVOID type pointer.

_________________
Do you need to ask me something? Feel free to join my discord server at: https://discord.gg/At4VZXA or ask me something in my YouTube channel: https://www.youtube.com/c/mgostIH
Back to top
View user's profile Send private message
PuckaHuck
Newbie cheater
Reputation: 0

Joined: 01 Nov 2015
Posts: 11

PostPosted: Mon Jan 04, 2016 1:58 pm    Post subject: Reply with quote

Thanks for the help guys, I almost have it working now Smile

I just have one problem though. This is some of the assembly I'm injecting (AT&T syntax):

Code:
movl $0x226, 30(%ebx)
movl %ebx, %eax


The problem is that the value 30 in this:
Code:
movl $0x226, 30(%ebx) // mov [ebx+30], 0x226

gets changed to something else like 22. Any idea why this happens? It works fine if I change it back to 30 afterwards, but I'd like to know why this happens and how I can fix it.
Back to top
View user's profile Send private message
mgostIH
Expert Cheater
Reputation: 3

Joined: 01 Jan 2016
Posts: 159

PostPosted: Mon Jan 04, 2016 2:01 pm    Post subject: Reply with quote

PuckaHuck wrote:
Thanks for the help guys, I almost have it working now :)

I just have one problem though. This is some of the assembly I'm injecting (AT&T syntax):

Code:
movl $0x226, 30(%ebx)
movl %ebx, %eax


The problem is that the value 30 in this:
Code:
movl $0x226, 30(%ebx) // mov [ebx+30], 0x226

gets changed to something else like 22. Any idea why this happens? It works fine if I change it back to 30 afterwards, but I'd like to know why this happens and how I can fix it.


Assuming you are code injecting with a c++ program, you don't input the syntax itself, but instead you write the bytes of the opcodes, so you have to check those instead, to see if you messed something up.

Also, providing some of the source code of your program would help a lot.

_________________
Do you need to ask me something? Feel free to join my discord server at: https://discord.gg/At4VZXA or ask me something in my YouTube channel: https://www.youtube.com/c/mgostIH
Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 152

Joined: 06 Jul 2014
Posts: 4697

PostPosted: Mon Jan 04, 2016 3:05 pm    Post subject: Reply with quote

mgostIH wrote:
Assuming you are code injecting with a c++ program, you don't input the syntax itself, but instead you write the bytes of the opcodes...

What? You can write ASM in C++ just fine. Example here.

PuckaHuck wrote:
...gets changed to something else like 22.

Does it change to random values every time you run it? If not, does it change to exactly 22? If not that either, then what does it change to? I'd guess it's some sort of dec/hex conversion problem depending on what it's changed to.

_________________
I don't know where I'm going, but I'll figure it out when I get there.
Back to top
View user's profile Send private message
PuckaHuck
Newbie cheater
Reputation: 0

Joined: 01 Nov 2015
Posts: 11

PostPosted: Mon Jan 04, 2016 3:17 pm    Post subject: Reply with quote

ParkourPenguin wrote:
mgostIH wrote:
Assuming you are code injecting with a c++ program, you don't input the syntax itself, but instead you write the bytes of the opcodes...

What? You can write ASM in C++ just fine. [/url].

PuckaHuck wrote:
...gets changed to something else like 22.

Does it change to random values every time you run it? If not, does it change to exactly 22? If not that either, then what does it change to? I'd guess it's some sort of dec/hex conversion problem depending on what it's changed to.


I just realized I wrote 30, it's actually 34, which converts to 22 in hex if you write it as a decimal... so yes it was a conversion problem. But I have fixed it now and it works perfectly, thanks for your help guys! Very Happy
Back to top
View user's profile Send private message
mgostIH
Expert Cheater
Reputation: 3

Joined: 01 Jan 2016
Posts: 159

PostPosted: Tue Jan 05, 2016 6:55 am    Post subject: Reply with quote

ParkourPenguin wrote:
mgostIH wrote:
Assuming you are code injecting with a c++ program, you don't input the syntax itself, but instead you write the bytes of the opcodes...

What? You can write ASM in C++ just fine. Example here.



Unless you use dll injection, you can't write asm code itself.
Also, invoking _asm is not something that C++ has by its own, but it's a compiler feature.

_________________
Do you need to ask me something? Feel free to join my discord server at: https://discord.gg/At4VZXA or ask me something in my YouTube channel: https://www.youtube.com/c/mgostIH
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 Gamehacking 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