The Stack

Section's for general approaches on hacking various options in games. No online-related discussions/posts OR warez!
Post Reply
TimFun13
Expert Cheater
Expert Cheater
Posts: 1354
Joined: Fri Mar 03, 2017 12:31 am
Reputation: 6

The Stack

Post by TimFun13 »

[Link]

The Stack



If you're unfamiliar with registries please start here: [Link]



So what is the stack?



Well I hate to use the word in the definition, but it's just a metaphorical stack of bytes; or an abstract data type that serves as a collection of elements, with two principal operations.

PUSH Adds an element to the collection. POP Removes the most recently added element that was not yet removed. The stack is primarily used to store values for a process. It uses a LIFO (last in, first out) behavior. So if we [Link] value A onto the stack and then [Link] value B, when we [Link] it will be value B first then value A with the second [Link].[Link]



[Link]





Working with the Stack

So let's just dig in, if we have some code like this.

Code: Select all

push 123ABC
push 00DEAD
push 00BEEF

pop dword ptr [TestVals]
pop dword ptr [TestVals+4]
pop dword ptr [TestVals+8]
And if we assemble this in some memory.

[Link]



So let's set a breakpoint and watch the stack as we step though the opcode.

Note: You can select a line of opcode in the memory view form and press F5 to set a breakpoint. Note: Step though opcode in the memory view form by pressing F7, after a breakpoint has been hit. Note: to view the stack you may need to select it for viewing. [Link]







Code step 1

[Link]



Code step 2

So it's here that we start to see the values on the stack. [Link]



Code step 3

[Link]



Code step 4

[Link]



Code step 5

And here we can start to see the values being popped in the reverse order that they were pushed. [Link]



Code step 6

[Link]



Code step 7

[Link]



And that's really all there is to the basics of the stack.



The thing to remember is that if you push in some injected code then you will need to pop in order to clean (or sanitize) the stack.





Working with the registries

We can [Link] and [Link] most registries, this is useful for saving and restoring stuff in injected code. The x86 architecture has 8 General-Purpose Registers (GPR), 6 Segment Registers, 1 Flags Register and an Instruction Pointer. 64-bit x86 has additional registers.[Link]

Code: Select all

push eax
push ebx
push ecx
push edx
push esi
push edi
push ebp
push esp

pop esp
pop ebp
pop edi
pop esi
pop edx
pop ecx
pop ebx
pop eax
There is the [Link] and [Link] to save and restore all the generic registries. So the above code could just be written like this.

Code: Select all

pushad

popad
Note: [Link] and [Link] are not available in 64 bit mode.





Working with the Flags registry

We can even [Link] and [Link] the Flags registry, using [Link] and [Link] (for 16 bit registry). This allows us to save and restore the Flags when doing compares (conditional opcodes). The FLAGS register is the status register in Intel x86 microprocessors that contains the current state of the processor. This register is 16 bits wide. Its successors, the EFLAGS and RFLAGS registers, are 32 bits and 64 bits wide, respectively. The wider registers retain compatibility with their smaller predecessors.[Link]

Note: when in 32 bit mode you should use [Link] and [Link] to manipulate the full 32 bits of the EFlags registry. Note: when in 64 bit mode you should use [Link] and [Link] to manipulate the full 32 bits of the EFlags registry.

Code: Select all

pushfd
cmp eax,ebx
movcc eax,ecx
popfd


See Also
  • [Link]
  • [Link]
  • [Link]
  • [Link]
  • [Link]
  • [Link]
  • [Link]
  • [Link]
  • [Link]
  • [Link]
  • [Link]
Sources
  1. [Link]
  2. [Link]
  3. [Link]
Last edited by TimFun13 on Sat May 12, 2018 5:59 pm, edited 3 times in total.

Post Reply

Who is online

Users browsing this forum: No registered users