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 


Write to stack pointer?
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
Polynomial
Grandmaster Cheater
Reputation: 5

Joined: 17 Feb 2008
Posts: 524
Location: Inside the Intel CET shadow stack

PostPosted: Tue Apr 06, 2010 6:33 pm    Post subject: Write to stack pointer? Reply with quote

Had a thought (albeit whilst drunk) about the stack pointer. Would it be possible to allocate new memory, copy the contents of the stack to that new location, then change esp to the new memory address? Or is ESP read only?
_________________
It's not fun unless every exploit mitigation is enabled.
Please do not reply to my posts with LLM-generated slop; I consider it to be an insult to my time.
Back to top
View user's profile Send private message
Flyte
Peanuts!!!!
Reputation: 6

Joined: 19 Apr 2006
Posts: 1887
Location: Canada

PostPosted: Tue Apr 06, 2010 6:41 pm    Post subject: Reply with quote

Why would you want to? Confused
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 475

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

PostPosted: Tue Apr 06, 2010 7:29 pm    Post subject: Reply with quote

yes, you can do that, but why?
_________________
Tools give you results. Knowledge gives you control.

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

Joined: 17 Feb 2008
Posts: 524
Location: Inside the Intel CET shadow stack

PostPosted: Wed Apr 07, 2010 3:47 pm    Post subject: Reply with quote

It'd be something that I might do when obfuscating. Just an idea anyway.
_________________
It's not fun unless every exploit mitigation is enabled.
Please do not reply to my posts with LLM-generated slop; I consider it to be an insult to my time.
Back to top
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Wed Apr 07, 2010 5:16 pm    Post subject: Reply with quote

You probably shouldn't though, the stack is not just the same as a regular block of memory. For example, at the end of it are some guard pages so if the stack 'overflows' for the first few times, it can be extended.
Back to top
View user's profile Send private message
Polynomial
Grandmaster Cheater
Reputation: 5

Joined: 17 Feb 2008
Posts: 524
Location: Inside the Intel CET shadow stack

PostPosted: Thu Apr 08, 2010 10:16 am    Post subject: Reply with quote

Shouldn't a ReadProcessMemory or NtQueryVirtualMemory call find all the memory on the stack so I can copy to it, stack guards and all? I would expect the OS to be able to find the stack guards easily, since they are offset from ESP.
_________________
It's not fun unless every exploit mitigation is enabled.
Please do not reply to my posts with LLM-generated slop; I consider it to be an insult to my time.
Back to top
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Thu Apr 08, 2010 10:24 am    Post subject: Reply with quote

well yes, you could do that. something else you might like to know though, is that each thread uses its own stack. and if you wanted to relocate a stack, it'd probably be best to suspend that thread, do your work, then continue it

if you were looking for the guard pages, it's probably better to start looking for ebp as well. the offset from esp would be variable. still completely missing the reason as to why you'd want to do this though :/
Back to top
View user's profile Send private message
Polynomial
Grandmaster Cheater
Reputation: 5

Joined: 17 Feb 2008
Posts: 524
Location: Inside the Intel CET shadow stack

PostPosted: Thu Apr 08, 2010 10:41 am    Post subject: Reply with quote

It's pretty much a chaff-style protection. If you came across some code that you know utilizes anti-debugger mechanisms, and you see that it's shifted the stack to a new area of memory, you'd automatically assume that it's some kind of protection method and go analyse it. Eventually you'd realise that it doesn't actually do anything, but it'd nag in the back of your head whenever another mechanism tripped during debugging. I find psychology can be a useful protection mechanism.
_________________
It's not fun unless every exploit mitigation is enabled.
Please do not reply to my posts with LLM-generated slop; I consider it to be an insult to my time.
Back to top
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Thu Apr 08, 2010 11:01 am    Post subject: Reply with quote

btw i think the easiest way of relocating the stack would be to do so from another thread. bearing in mind making any function calls from an existing thread would alter its stack, you'd probably want it to be static during the copying process
Back to top
View user's profile Send private message
Polynomial
Grandmaster Cheater
Reputation: 5

Joined: 17 Feb 2008
Posts: 524
Location: Inside the Intel CET shadow stack

PostPosted: Thu Apr 08, 2010 11:17 am    Post subject: Reply with quote

I'd probably store ESP in a variable somewhere, freeze the thread, use a second thread to ReadProcessMemory the stack and generate the new one, store the new stack pointer in that variable, unfreeze the first thread and then have it write that value to ESP.

Here's a step-by-step to illustrate what I mean:
T1 writes ESP to Var
T1 starts T2
T2 freezes T1
T2 copies stack to NewStack
T2 writes address of NewStack into Var
T2 unfreezes T1
T1 writes Var to ESP

_________________
It's not fun unless every exploit mitigation is enabled.
Please do not reply to my posts with LLM-generated slop; I consider it to be an insult to my time.
Back to top
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Thu Apr 08, 2010 11:19 am    Post subject: Reply with quote

the writing of variable is not necessary. i believe you'd be able to do the whole thing with Get/SetThreadContext()/SuspendThread(), etc.
Back to top
View user's profile Send private message
Polynomial
Grandmaster Cheater
Reputation: 5

Joined: 17 Feb 2008
Posts: 524
Location: Inside the Intel CET shadow stack

PostPosted: Thu Apr 08, 2010 11:22 am    Post subject: Reply with quote

Wasn't aware you could read the stack pointer with GetThreadContext. I'll take a look at the docs and give it a go.
_________________
It's not fun unless every exploit mitigation is enabled.
Please do not reply to my posts with LLM-generated slop; I consider it to be an insult to my time.
Back to top
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Thu Apr 08, 2010 11:36 am    Post subject: Reply with quote

yes, registers count as part of thread's context. think of everything that must be preserved when the processor does a context switch. on my machine, the CONTEXT structure is defined to have the following members :
- ContextFlags
- Dr0
- Dr1
- Dr2
- Dr3
- Dr6
- Dr7
- Eax
- Ebp
- Ebx
- Ecx
- Edi
- Edx
- EFlags
- Eip
- Esi
- Esp
- ExtendedRegisters
- FloatSave
- SegCs
- SegDs
- SegEs
- SegFs
- SegGs
- SegSs

here's a cool example of get/setthreadcontext(). it should explain to you the concepts behind what you're trying to do. in masm32 though :
http://win32assembly.online.fr/tut29.html
Back to top
View user's profile Send private message
Polynomial
Grandmaster Cheater
Reputation: 5

Joined: 17 Feb 2008
Posts: 524
Location: Inside the Intel CET shadow stack

PostPosted: Thu Apr 08, 2010 11:39 am    Post subject: Reply with quote

So it doesn't store SSE registers like xmm1? Seems odd. Or is that covered in ExtendedRegisters?
_________________
It's not fun unless every exploit mitigation is enabled.
Please do not reply to my posts with LLM-generated slop; I consider it to be an insult to my time.
Back to top
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Thu Apr 08, 2010 11:58 am    Post subject: Reply with quote

the ContextFlags member of CONTEXT would tell you which of the other members of the CONTEXT are valid. bear in mind the mmx registers overlay the fpu registers. you should download the latest sdk and look inside your WinNT.h
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
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