CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/data/utilities/encrypted_payload/AdjustStack.asm
Views: 1904
1
/*
2
* This code is provided under the 3-clause BSD license below.
3
* ***********************************************************
4
*
5
* Copyright (c) 2013, Matthew Graeber
6
* All rights reserved.
7
*
8
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
9
*
10
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
11
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
12
* The names of its contributors may not be used to endorse or promote products derived from this software without specific prior written permission.
13
*
14
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15
*/
16
17
; Author: Matthew Graeber (@mattifestation)
18
; License: BSD 3-Clause
19
; Syntax: MASM
20
; Build Syntax: ml64 /c /Cx AdjustStack.asm
21
; Output: AdjustStack.obj
22
; Notes: I really wanted to avoid having this external dependency but I couldnt
23
; come up with any other way to guarantee 16-byte stack alignment in 64-bit
24
; shellcode written in C.
25
26
extern ExecutePayload
27
global AlignRSP ; Marking AlignRSP as PUBLIC allows for the function
28
; to be called as an extern in our C code.
29
30
segment .text
31
32
; AlignRSP is a simple call stub that ensures that the stack is 16-byte aligned prior
33
; to calling the entry point of the payload. This is necessary because 64-bit functions
34
; in Windows assume that they were called with 16-byte stack alignment. When amd64
35
; shellcode is executed, you cant be assured that you stack is 16-byte aligned. For example,
36
; if your shellcode lands with 8-byte stack alignment, any call to a Win32 function will likely
37
; crash upon calling any ASM instruction that utilizes XMM registers (which require 16-byte)
38
; alignment.
39
40
AlignRSP:
41
push rsi ; Preserve RSI since were stomping on it
42
mov rsi, rsp ; Save the value of RSP so it can be restored
43
and rsp, 0FFFFFFFFFFFFFFF0h ; Align RSP to 16 bytes
44
sub rsp, 020h ; Allocate homing space for ExecutePayload
45
call ExecutePayload ; Call the entry point of the payload
46
mov rsp, rsi ; Restore the original value of RSP
47
pop rsi ; Restore RSI
48
ret ; Return to caller
49
50