Basic assembly instructions (opcodes) and examples
This article should cover basic assembly instructions to start building Your own scripts. If You do not know how to do code injection yet, read the previous article:
How to beat the Cheat Engine tutorial
Here You will find a simple list about the most important codes that You may use when You try to hack a game. You will also find 2 examples, the first one is how to make a God Mode cheat in an RTS game, Warhammer: Mark of Chaos. The second is a money cheat for C&C Generals which is affecting the human player only, not the AI.
Assembly instructions (Opcodes):
"dec" will decrease the value with 1
inc : increase the value with 1
sub : subtract the second operand from the first,
e.g. sub [ebx+00000310],4 would mean "decrease the value on ebx+0310 with 4".
add : will add the second operand from the first,
e.g. add [ebx+00000310],4 would mean "increase the value on ebx+0310 with 4".
mov : copy the second operand to the first,
e.g. mov [ebx+00000310],4 would mean "change the value on ebx+0310 to 4".
lea: copy the result of the second operand to the first operand
e.g. lea eax,[esi+30] would mean copy esi+30 to eax.
This is good to save pointers.
cmp : compare, 2 registers, or a number and a register.
e.g. cmp esi,2 //compare esi to 2
cmp esi,ecx //compare esi to ecx
The result of the comparing will be stored in a flag. To see more about flags,
check out the CE help file.
jmp : jump to and address
You can use the jmp instructions to jump to a specific address (long jump), or to jump forward or backward x bytes (short jump).
e.g. jmp +0000000A //this instruction would mean to jump forward 10 bytes in the code.
Conditional jumps:
You can use conditional jumps to jump to a specific address, or to jump
forward or backward x bytes.
je : jump to a location if the previous compare's result was equal
e.g.
cmp esi,2
je 0f445566 //if esi is equal to 2, the program will jump to 0f445566 address.
jne: jump to a location if the previous compare's result was not equal
jg : Jump if Greater
jl : Jump if Less
push : save a register or flag in the stack
pop : load a register or flag from the stack
pushad : save all registers in the stack
popad : load all registers from the stack
pushfd : save all flags in the stack
popfd : load all flags from the stack
The stack is a "storage" where You can put values and load them from it. However You are not able to save and load the values in any order. The last value in the stack that You push in will be the first one that You pop out.
For example lets assume ecx=3, edx=2 and the stack stores the following values.
4
5
6
Now we put an instruction like "push ecx". Then the stack will look like this.
3
4
5
6
Now we also put in edx with "push edx". The stack will look like this.
2
3
4
5
6
Now we want to pop a value from the stack, like "pop ecx".
In this case, the program will pull out the first value from the stack, which is 2,
and put it to ecx. So ecx=2 and the stack look like this.
3
4
5
6
If we give a "pop edx" instruction now, edx will be 3, and the stack will look like
4
5
6
The result: The stack is the same as when we have started, but ecx=2 now, and edx=3, we have changed their values with each other.
If You use pushad and pushfd, then popad and popfd, make sure to use them in the correct order.
pushad
pushfd
...
popfd
popad
or
pushfd
pushad
....
popad
popfd
So what You push in first should be popped out first. What happens if You dont?
WRONG example:
pushfd
pushad
....
popfd
popad
Result: You will push the flags into the stack, then push in the registers and with popfd instruction, You will load the value of the registers to the flags, which will totally mess up the program and most likely it will crash.
Why do we use push, pop and the stack anyway?
In assembly, You are not able to use 2 values as operands in the same instruction.
e.g. mov [ecx],[edx] is not a valid instruction. If You wish to move the value on [edx] to [ecx], first You need to put [ecx] to a register, then copy the register to [ecx]. However changing a register's value for Your own puproses without restoring it later will result in errors. Also when You use the cmp instructions, You change the status of the flags, so sometimes it may be needed to restore the flag values too.
For a fine example on how to use the stack, compare, conditional jumps and mov, I will describe how did I make a "God Mode" for my soldiers in an RTS game, Warhammer: Mark of Chaos.
First of all You need to search for a soldier's health and check which code is writing to the address of health. However, this code is changing all of the soldiers' health in the game, including the enemy soldiers. So if You just turn off this code, everyone will be invincible. That would not be useful.
The code which is changing the health is the following:
008d0f08 - d9 56 04 - fst dword ptr [esi+04]
If You look in the memory browser, You will realise that the soldier's data structure is very simple in this game. As You can see it in the code, the health is stored on esi+04. The maximum health of the unit is 4 bytes later, so it is on esi+08. One more information: if the value before the health (esi) is 0, then the unit is Your unit. If it is not, then an enemy unit.
So the data structure is the following:
[esi] = player ID, if it is 0, then the unit is Yours
[esi+04] = health
[esi+08] = max health
With these information, we can plan our script now. It should do the following:
Check if the unit is Your unit.
If it is Your unit, change the healt to the maximum health.
The script will look like this:
fst dword ptr [esi+04] //original code which is changing health
pushad //save the registers
pushfd //save the flags
cmp [esi],0 //check if the player ID is equal to 0
jne +6 //if it is not 0, the program will skip the next 2 lines
mov eax,[esi+08] //copy the value of max health to eax
mov [esi+04],eax //copy eax to the health, so max health=health
popfd //load flags
popad //load registers
Well, thats it. A simple code which will make only Your units invincible, but the enemies will take damage normally. Notice that before I have used cmp, I have saved the flags and before I have changed eax register's value, I have saved the registers too. At the end of the code, I have loaded back the original flags and registers so I was able to use the flags and registers as I wanted in my code, without crashing the program.
Another good example: Unlimited money for the player only in C&C Generals.
It is pretty easy to find the code which is changing the money for the players in Generals, but if You wish to make script that will give You unlimited money for You only, here is a simple hint which is working with most of the games.
When the game is working with the money, it is using different addresses to store Your money and the amount of money that is displayed on Your screen. This means that when You search for money, You will find 2 addresses.
1. The money that You actually have.
2. The amount that You see on the screen in the game.
The game is checking Your money about 10 times in a second and display the correct amount to Your gameplay screen.
1. You have an address where Your money is stored.
2. A code is reading how much money You have on that address.
3. A script is copying the value of the money to the address where You can see the displayed amount of money.
Why is that important?
It is important because only the player's money is displayed on the screen, so the code which is reading how much money do You have in the 2nd step is accessing to Your money only. If You have the code which is accessing to Your money only, You can easily write a script to change Your money, but not for the enemies.
How to find that code?
It is simple. Find the address where Your money is stored, but after that, You need to choose "Find out what accesses this address". You will get the code which is reading from the address, not just the one which is writing to it.
The code which is reading the amount of Your money is:
mov ebx,[eax+38]
Now all You need to do is write a script which is changing Your money when the program
is reading it:
mov ebx,[eax+38] //read Your money
mov [eax+38],000f423f //change Your money to 999999 (which is 000f423f in hex)
Now You have learned another method which will save You from the troubles ahead if You want to find a specific code.
Peace!
Geri
Hozzászólások
For example, let's assume that the pointer is [eax] and eax=00607080, which is an address with a value on it.
mov ecx,[eax]
would copy the value that is stored on the address, stored by eax. In summary, it would copy the value from 00607080 address to ecx.
lea ecx,[eax]
would copy the value of eax to ecx. In summary it would copy 00607080 to ecx.
lea ecx,[eax+4]
would copy 00607080+4 (00607084) to ecx.
So lea instruction is useful when You want to save the address where a value is, while mov is useful when You want to copy the value which is on the address.
Nice tut man .. helped a lot ..
OK I already did my script auto assembly with CE it's good i mean he did the work :)
but when i go back to my main menu it's crashing my game take alook :
[ENABLE]
alloc(newmem,20 48) //2kb should be enough
label(returnher e)
label(originalc ode)
label(exit)
0054B868:
jmp newmem
nop
returnhere:
newmem: //this is allocated memory, you have read,write,exec ute access
mov ecx,5
originalcode:
mov [eax],ecx
mov eax,ecx
mov [edx],eax
exit:
jmp returnhere
[DISABLE]
dealloc(newmem)
0054B868: //our original address
mov [eax],ecx
mov eax,ecx
mov [edx],eax
what is wrong here !!
thanks for your efforts its helped me alot ,,, :)
Thanks for your reply ..
take a look at this
0054B096 - 8B 01 -mov eax,[ecx]
0054B865 - 8B 08 -mov ecx,[eax]
0054B868 - 89 08 -mov [eax],ecx
0054BA55 - DB 01 -fild dword ptr [ecx]
0053FF5A - 8B 11 -mov edx,[ecx]
0054B9E3 - 8B 0B -mov ecx,[ebx]
0054B136 - 8B 01 -mov eax,[ecx]
EAX=03FD60C8=6 //my max lives
EBX=0000000D
ECX=04212D2C=5 //my lives after decreased
EDX=03FD6014
ESI=040CC5C0
EDI=00000004
EBP=0000000E
ESP=0022FE58
EIP=0054BA57
What is the name of the game?
I know what you say >> joking
lets try it again .. ok
the address is 03379D6C
find out what is writes to this address
0054b868 -89 08 -mov [eax],ecx
add to the codelist
find out what addresses this code writes to
03379D6C
then instruction appear in my debug
0054b867 - 49 -dec ecx
extra info
EAX=03379D6C
EBX=0000000C
ECX=00000004
EDX=03228B28
ESI=03BD0840
EDI=00000004
EBP=0000000D
ESP=0022FE58
EIP=0054B868
Probable base pointer =00000000
0054b863 - mov eax,[eax]
0054b865 - mov ecx,[eax]
0054b867 - dec ecx
0054b868 - mov [eax],ecx
0054b86a - mov eax,ecx
show disaaaembler>tools>auto assemble>template>cheat table framework code>code injection
[ENABLE]
//code from here to '[DISABLE]' will be used to enable the cheat
alloc(newmem,20 48) //2kb should be enough
label(returnher e)
label(originalc ode)
label(exit)
0054B867:
jmp newmem
returnhere:
newmem: //this is allocated memory, you have read,write,exec ute access
//place your code here
originalcode:
dec ecx
mov [eax],ecx
mov eax,ecx
exit:
jmp returnhere
[DISABLE]
//code from here till the end of the code will be used to disable the cheat
dealloc(newmem)
0054B867:
dec ecx
mov [eax],ecx
mov eax,ecx
//Alt: db 49 89 08 8B C1
If You find a code which is reading Your health to display it on the screen or to use any other purposes and it is reading Your health only, You can use it to make a fine cheat without too much trouble.
A cikk hozzászólásainak RSS-csatornája.