Skip to content

Commit e5c40d3

Browse files
author
devseed
committed
x64 memdll support
1 parent b87e84f commit e5c40d3

6 files changed

Lines changed: 538 additions & 144 deletions

File tree

README.md

Lines changed: 98 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,105 @@
11
# MemoryModule
22
A tool to parse and load module in memory, as well as attach a DLL in EXE.
33

4-
## winpe
4+
Most of the functions are inline, so that it can also be used in shellcode.
5+
6+
## compile
7+
8+
```shell
9+
cd ./src/memdll
10+
pip install lief
11+
pip install keystone
12+
make ARCH=i686 # x86 release
13+
make ARCH=x86_64 # x64 release
14+
make ARCH=i686 DEBUG=1 # x86 debug
15+
make ARCH=x86_64 DEBUG=1 # x64 debug
16+
```
17+
18+
## usage
19+
20+
### load DLL in memory
21+
22+
```c
23+
const char *dllpath = "test.dll";
24+
size_t mempesize = 0;
25+
void *memdll = NULL;
26+
27+
// load the pe file in memory and align it to memory align
28+
void *mempe = winpe_memload_file(dllpath, &mempesize, TRUE);
29+
30+
// memory loadlibrary
31+
memdll = winpe_memLoadLibrary(mempe);
32+
winpe_memFreeLibrary(memdll);
33+
34+
// memory loadlibrary at specific address
35+
size_t targetaddr = sizeof(size_t) > 4 ? 0x140030000: 0x90000;
36+
memdll = winpe_memLoadLibraryEx(memdll, targetaddr,
37+
WINPE_LDFLAG_MEMALLOC, (PFN_LoadLibraryA)winpe_findloadlibrarya(),
38+
(PFN_GetProcAddress)winpe_memGetProcAddress);
39+
winpe_memFreeLibrary(memdll);
40+
free(mempe);
41+
```
42+
43+
44+
45+
### attach DLL in exe
46+
47+
```shell
48+
win_injectmemdll.exe exepath dllpath [outpath]
49+
```
50+
51+
## memory module API
552

653
These functions are essential to load memory module in windows.
754

855
```c
56+
/*
57+
similar to LoadlibrayA, will call dllentry
58+
will load the mempe in a valid imagebase
59+
return hmodule base
60+
*/
61+
WINPEDEF WINPE_EXPORT
62+
inline void* STDCALL winpe_memLoadLibrary(void *mempe);
63+
64+
/*
65+
if imagebase==0, will load on mempe, or in imagebase
66+
will load the mempe in a valid imagebase, flag as below:
67+
WINPE_LDFLAG_MEMALLOC 0x1, will alloc memory to imagebase
68+
WINPE_LDFLAG_MEMFIND 0x2, will find a valid space,
69+
must combined with WINPE_LDFLAG_MEMALLOC
70+
return hmodule base
71+
*/
72+
WINPEDEF WINPE_EXPORT
73+
inline void* STDCALL winpe_memLoadLibraryEx(void *mempe,
74+
size_t imagebase, DWORD flag,
75+
PFN_LoadLibraryA pfnLoadLibraryA,
76+
PFN_GetProcAddress pfnGetProcAddress);
77+
78+
/*
79+
similar to FreeLibrary, will call dllentry
80+
return true or false
81+
*/
82+
WINPEDEF WINPE_EXPORT
83+
inline BOOL STDCALL winpe_memFreeLibrary(void *mempe);
84+
85+
/*
86+
FreeLibraryEx with VirtualFree custom function
87+
return true or false
88+
*/
89+
WINPEDEF WINPE_EXPORT
90+
inline BOOL STDCALL winpe_memFreeLibraryEx(void *mempe,
91+
PFN_LoadLibraryA pfnLoadLibraryA,
92+
PFN_GetProcAddress pfnGetProcAddress);
93+
94+
/*
95+
similar to GetProcAddress
96+
return function va
97+
*/
98+
WINPEDEF WINPE_EXPORT
99+
inline PROC STDCALL winpe_memGetProcAddress(
100+
void *mempe, const char *funcname);
101+
102+
// mempe internal functions
9103
/*
10104
load the origin rawpe in memory buffer by mem align
11105
return memsize
@@ -31,21 +125,8 @@ size_t winpe_membindiat(void *mempe,
31125
32126
See `winpe.h` for parsing and loading PE structure in detail.
33127
34-
## compile
128+
## known issues
35129
36-
```shell
37-
cd ./src/memdll
38-
pip install lief
39-
pip install keystone
40-
make ARCH=i686 # x86 release
41-
make ARCH=x86_64 # x64 release
42-
make ARCH=i686 DEBUG=1 # x86 debug
43-
make ARCH=x86_64 DEBUG=1 # x64 debug
44-
```
45-
46-
## usage
47-
48-
```shell
49-
win_injectmemdll exepath dllpath [outpath]
50-
```
130+
* attach x64 DLL to exe crash on calling some windows API
51131
132+
(load x64 DLL in memory after main function doesn't have this problem)

src/memdll/Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# make -j12 ARCH=i686 && make -j12 ARCH=i686 DEBUG=1
2+
# make -j12 ARCH=x86_64 && make -j12 ARCH=x86_64 DEBUG=1
13
LIBPREFIX?=./../../
24
ARCH?=i686
35
PREFIX?=./bin
@@ -60,7 +62,8 @@ prepare:
6062
libwinpe: libwinpe.c
6163
@echo \#\#building $@ ...
6264
$(CC) $< -o $(PREFIX)/$@$(ARCH_POSTFIX)$(DLL_EXT) \
63-
-shared $(CFLAGS) $(LDFLAGS) $(INCS) $(LIBS) $(LIBDIRS)
65+
-shared -Wl,/DEF:$@.def\
66+
$(CFLAGS) $(LDFLAGS) $(INCS) $(LIBS) $(LIBDIRS)
6467

6568
win_injectmemdll_shellcodestub: win_injectmemdll.c libwinpe
6669
python $@.py $< \

src/memdll/libwinpe.def

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
EXPORTS
2+
winpe_appendsecth
3+
winpe_findgetprocaddress
4+
winpe_findkernel32
5+
winpe_findloadlibrarya
6+
winpe_findspace
7+
winpe_imagebaseval
8+
winpe_memFreeLibrary
9+
winpe_memFreeLibraryEx
10+
winpe_memGetProcAddress
11+
winpe_memLoadLibrary
12+
winpe_memLoadLibraryEx
13+
winpe_membindiat
14+
winpe_memfindexp
15+
winpe_memfindiat
16+
winpe_memforwardexp
17+
winpe_memload
18+
winpe_memload_file
19+
winpe_memreloc
20+
winpe_noaslr
21+
winpe_oepval
22+
winpe_overlayload_file
23+
winpe_overlayoffset

0 commit comments

Comments
 (0)