Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"push ax" misaligns stack from DWORD #28

Open
AlLongley opened this issue Oct 14, 2023 · 3 comments
Open

"push ax" misaligns stack from DWORD #28

AlLongley opened this issue Oct 14, 2023 · 3 comments

Comments

@AlLongley
Copy link

An instruction when pushing potentially non DWORD-aligned strings to the stack can potentially cause the SP to become a value non-divisible by 4. Aside from the serious performance hit from a misaligned stack, NT Kernel calls rightfully fail to execute in non-apparent ways.

ie running "MoveFileA" after misaligning the stack fails with GetErrorCode ERROR_NOACCESS = 998 (0x3E6)

Which deceptively is unrelated to file permissions/access

first_instructions.append("push ax;")

@AlLongley
Copy link
Author

I'm a bit past capacity right this second to properly review, commit and PR this but my workaround for pushing non-aligned strings which would otherwise introduce NULL byte into the payload is the following:

On the 3rd else for whether the string chunk is neither 2 or 4 byte aligned, I pad it with NULLs to become 4 bytes, then store it XOR'd it against 0xFFFFFFFF and XOR it back again just before pushing the full EAX DWORD

                chunk = bytearray.fromhex(target_bytes.ljust(8,"0"))
                xored = bytearray([x^0xFF for x in chunk[::-1]]).hex()
                print(xored)
                first_instructions.append(f"mov eax, 0x{xored};\n")
                first_instructions.append(f"xor eax, 0xFFFFFFFF;\n")

@epi052
Copy link
Owner

epi052 commented Oct 14, 2023

thanks for this!

It's been a long time since I've looked at any of this. Is there a reason why we're using

push ax

instead of

push eax

@epi052
Copy link
Owner

epi052 commented Oct 14, 2023

nvm, actually took the time to read the surrouding code.

do you think i should just apply your suggestion to main?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants