Skip to content

Commit 185eef9

Browse files
author
devseed
committed
fix x64 crash in dll attach by stack align 0x10
1 parent e5c40d3 commit 185eef9

2 files changed

Lines changed: 11 additions & 14 deletions

File tree

README.md

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# MemoryModule
22
A tool to parse and load module in memory, as well as attach a DLL in EXE.
3-
43
Most of the functions are inline, so that it can also be used in shellcode.
54

65
## compile
@@ -33,7 +32,7 @@ winpe_memFreeLibrary(memdll);
3332

3433
// memory loadlibrary at specific address
3534
size_t targetaddr = sizeof(size_t) > 4 ? 0x140030000: 0x90000;
36-
memdll = winpe_memLoadLibraryEx(memdll, targetaddr,
35+
memdll = winpe_memLoadLibraryEx(mempe, targetaddr,
3736
WINPE_LDFLAG_MEMALLOC, (PFN_LoadLibraryA)winpe_findloadlibrarya(),
3837
(PFN_GetProcAddress)winpe_memGetProcAddress);
3938
winpe_memFreeLibrary(memdll);
@@ -58,7 +57,6 @@ These functions are essential to load memory module in windows.
5857
will load the mempe in a valid imagebase
5958
return hmodule base
6059
*/
61-
WINPEDEF WINPE_EXPORT
6260
inline void* STDCALL winpe_memLoadLibrary(void *mempe);
6361

6462
/*
@@ -69,7 +67,6 @@ inline void* STDCALL winpe_memLoadLibrary(void *mempe);
6967
must combined with WINPE_LDFLAG_MEMALLOC
7068
return hmodule base
7169
*/
72-
WINPEDEF WINPE_EXPORT
7370
inline void* STDCALL winpe_memLoadLibraryEx(void *mempe,
7471
size_t imagebase, DWORD flag,
7572
PFN_LoadLibraryA pfnLoadLibraryA,
@@ -79,14 +76,12 @@ inline void* STDCALL winpe_memLoadLibraryEx(void *mempe,
7976
similar to FreeLibrary, will call dllentry
8077
return true or false
8178
*/
82-
WINPEDEF WINPE_EXPORT
8379
inline BOOL STDCALL winpe_memFreeLibrary(void *mempe);
8480

8581
/*
8682
FreeLibraryEx with VirtualFree custom function
8783
return true or false
8884
*/
89-
WINPEDEF WINPE_EXPORT
9085
inline BOOL STDCALL winpe_memFreeLibraryEx(void *mempe,
9186
PFN_LoadLibraryA pfnLoadLibraryA,
9287
PFN_GetProcAddress pfnGetProcAddress);
@@ -95,7 +90,6 @@ inline BOOL STDCALL winpe_memFreeLibraryEx(void *mempe,
9590
similar to GetProcAddress
9691
return function va
9792
*/
98-
WINPEDEF WINPE_EXPORT
9993
inline PROC STDCALL winpe_memGetProcAddress(
10094
void *mempe, const char *funcname);
10195

@@ -104,21 +98,21 @@ inline PROC STDCALL winpe_memGetProcAddress(
10498
load the origin rawpe in memory buffer by mem align
10599
return memsize
106100
*/
107-
size_t winpe_memload(const void *rawpe, size_t rawsize,
101+
inline size_t winpe_memload(const void *rawpe, size_t rawsize,
108102
void *mempe, size_t memsize, bool_t same_align);
109103

110104

111105
/*
112106
realoc the addrs for the mempe addr as image base
113107
return realoc count
114108
*/
115-
size_t winpe_memreloc(void *mempe, size_t newimagebase);
109+
inline size_t winpe_memreloc(void *mempe, size_t newimagebase);
116110

117111
/*
118112
load the iat for the mempe
119113
return iat count
120114
*/
121-
size_t winpe_membindiat(void *mempe,
115+
inline size_t winpe_membindiat(void *mempe,
122116
PFN_LoadLibraryA pfnLoadLibraryA,
123117
PFN_GetProcAddress pfnGetProcAddress);
124118
```
@@ -127,6 +121,6 @@ See `winpe.h` for parsing and loading PE structure in detail.
127121
128122
## known issues
129123
130-
* attach x64 DLL to exe crash on calling some windows API
131-
132-
(load x64 DLL in memory after main function doesn't have this problem)
124+
* ~~attach x64 DLL to exe crash on calling some windows API~~
125+
problem occured by `movaps xmm0, xmmword ptr ss:[rsp]`
126+
fixed by stack memory align with 0x10

src/memdll/win_injectmemdll_shellcodestub.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
history:
66
v0.1, initial version
77
v0.2, add more function for shellcode
8-
v0.3, x86 and x64 no need to use exe's LoadLibraryA
8+
v0.3, x86 and x64 no need to use exe's LoadLibraryA
9+
v0.3.1, fix x64 attach dll crash by align stack with 0x10
910
"""
1011
import re
1112
import sys
@@ -71,6 +72,7 @@ def gen_oepinit_code64():
7172
push rdx;
7273
push r8;
7374
push r9;
75+
sub rsp, 0x28; // this is for memory 0x10 align
7476
7577
// bind iat
7678
lea rdx, [rbx + findloadlibrarya];
@@ -91,6 +93,7 @@ def gen_oepinit_code64():
9193
call [rbx+dlloepva];
9294
9395
// jmp to origin oep
96+
add rsp, 0x28;
9497
pop r9;
9598
pop r8;
9699
pop rdx;

0 commit comments

Comments
 (0)