-
Notifications
You must be signed in to change notification settings - Fork 14k
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
Fixed segment_injector.rb x64 shellcode #13979
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As one smart researcher once said
Smart, but couldn't install MSF without Kali? 👍
Reverse shell generated by msfvenom. Can you believe I had to download Kali Linux for this shit?
I've not used Kali for any reason for 6 years. So, no, I don't believe that anyone has to "download Kali for this shit". Installing MSF is trivial on stacks of different operating systems.
Anyway, thanks for the submission. I've added a few comments for clarification to various parts of the code. Cheers!
mov r9, 0 | ||
xor ecx, ecx | ||
mov qword ptr [rsp+28h], rcx | ||
mov qword ptr [rsp+20h], rcx |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll admit that this change confuses me a little. I would expect to see two 0
values at [rsp]
and [rsp+8]
to represent parameters 5 and 6. But that's not what this is doing.
Can you please explain why you're writing zeros to the shadow space rather than at the top of the stack?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why you're writing zeros to the shadow space
That's exactly what the previous code did, and this fixes it. Here's one of the Google Images results for "shadow stack x64", feel free to read more about it: https://modexp.files.wordpress.com/2017/01/x64_hs.png?w=640
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can confirm some x86_64 calling conventions I have used put params 5+ on the stack where they would have been under an all-stack (x86-style) convention. The MS docs on calling convention support this as well:
The x64 Application Binary Interface (ABI) uses a four-register fast-call calling convention by default. Space is allocated on the call stack as a shadow store for callees to save those registers. ... Any parameters beyond the first four must be stored on the stack after the shadow store before the call.
Regarding the msfvenom quote, it's not about Kali, but more about the fact that tooling can be annoying. And I believe that what was meant in "download Kali for this shit" was: In any case, I hope nobody's offended. I personally think you're doing a great and important job of making security more accessible, and as a result making people more aware. |
Alright so I was able to reproduce the issue this is fixing. Following the guide: Backdooring While Preserving Functionality wasn't really enough. The key is using a 64-bit payload and 64-bit template file where as the guide uses 32-bit and then injects an x64 payload so you can't follow it verbatim. I used a 64-bit copy of Putty and was able to reproduce this on my first try. Having said that, once I started testing this fix, I found that the latest commit (5877c79) seems to break the Error on commit 5877c79
|
Alright the issue was easy enough for me to fix in commit f69facc. There was a syntax issue with metasm and the alignment was getting ruined again by the followup |
Release NotesFixed an issue with the 64-bit segment injector logic used by 64-bit PE templates. The injector now properly handles the arguments and stack alignment. |
During an evaluation of our ZecOps Crash Analysis solution, one of our partners brought to our attention an interesting crash. He claimed that he followed this guide: Backdooring While Preserving Functionality, but the backdoored app crashed right away. Looking at the crash, we quickly spotted the problem: due to an unaligned stack pointer, the process crashed on an SMID instruction (movaps). For a short explanation, see: https://stackoverflow.com/a/5540149
While composing the pull request, I found another problem - the call to
CreateThread
wasn't done properly. Not only the stack pointer was unaligned and no shadow stack was reserved, but also the two zeros pushed on the stack weren't the actual parameters 5 and 6. The fact that this code even worked is pure accident, probably because the Windows loader happens to set the proper locations on the stack to zero. In any case, that's fixed as well.I did a quick check, the new shellcode should work properly, but I'm not 100% sure about the syntax, I didn't check it with the tooling. As one smart researcher once said:
:)