BlackBone
Windows memory hacking library
 All Classes Functions
Wow64Local.h
1 #pragma once
2 
3 #include "../Include/Winheaders.h"
4 #include "../Include/NativeStructures.h"
5 #include "../Include/Macro.h"
6 #include "../Include/Types.h"
7 
8 #include <vector>
9 #include <type_traits>
10 
11 namespace blackbone
12 {
13 
18 {
19 public:
20  Wow64Local( void );
21  ~Wow64Local( void );
22 
23 
24 // unreferenced formal parameter, invalid operand size
25 #pragma warning(disable : 4409 4100)
26 
33  template< typename... Args >
34  DWORD64 X64Call( ptr_t func, Args... args )
35  {
36 #ifdef _M_IX86
37  int argC = sizeof...(Args);
38  int i = 0;
39  std::vector<DWORD64> vargs{ ((DWORD64)args)... };
40 
41  DWORD64 _rcx = (i < argC) ? vargs[i++] : 0;
42  DWORD64 _rdx = (i < argC) ? vargs[i++] : 0;
43  DWORD64 _r8 = (i < argC) ? vargs[i++] : 0;
44  DWORD64 _r9 = (i < argC) ? vargs[i++] : 0;
45  reg64 _rax;
46  _rax.v = 0;
47 
48  DWORD64 restArgs = (i < argC) ? (DWORD64)&vargs[i] : 0;
49 
50  // conversion to QWORD for easier use in inline assembly
51  DWORD64 _argC = argC - i;
52  DWORD64 _func = func;
53 
54  DWORD back_esp = 0;
55 
56  __asm
57  {
58  mov back_esp, esp
59 
60  ;// align esp to 16
61  and esp, 0xFFFFFFF0
62 
63  X64_Start();
64 
65  ;// fill first four arguments
66  push _rcx
67  X64_Pop(_RCX);
68  push _rdx
69  X64_Pop(_RDX);
70  push _r8
71  X64_Pop(_R8);
72  push _r9
73  X64_Pop(_R9);
74 
75  push edi
76 
77  ;// Preserve R10 for win10 WOW64 call
78  X64_Push( _R10 )
79 
80  push restArgs
81  X64_Pop(_RDI);
82 
83  push _argC
84  X64_Pop(_RAX);
85 
86  ;// put rest of arguments on the stack
87  test eax, eax
88  jz _ls_e
89  lea edi, dword ptr[edi + 8 * eax - 8]
90 
91  _ls:
92  test eax, eax
93  jz _ls_e
94  push dword ptr [edi]
95  sub edi, 8
96  sub eax, 1
97  jmp _ls
98  _ls_e:
99 
100  ;// create stack space for spilling registers
101  sub esp, 0x20
102 
103  call _func
104 
105  ;// cleanup stack
106  push _argC
107  X64_Pop(_RCX);
108  lea esp, dword ptr[esp + 8 * ecx + 0x20]
109 
110  X64_Pop( _R10 )
111  pop edi
112 
113  ;// set return value
114  X64_Push(_RAX);
115  pop _rax.dw[0]
116 
117  X64_End();
118 
119  mov esp, back_esp
120  }
121 
122  return _rax.v;
123 #else
124  return (DWORD64)STATUS_NOT_SUPPORTED;
125 #endif
126  }
127 
134  template<typename ...Args>
135  DWORD64 X64Syscall( int idx, Args... args )
136  {
137  #ifdef _M_IX86
138  int argC = sizeof...(Args);
139  int i = 0;
140  std::vector<DWORD64> vargs{ ((DWORD64)args)... };
141 
142  DWORD64 _rcx = (i < argC) ? vargs[i++] : 0;
143  DWORD64 _rdx = (i < argC) ? vargs[i++] : 0;
144  DWORD64 _r8 = (i < argC) ? vargs[i++] : 0;
145  DWORD64 _r9 = (i < argC) ? vargs[i++] : 0;
146  reg64 _rax;
147  _rax.v = 0;
148 
149  DWORD64 restArgs = (i < argC) ? (DWORD64)&vargs[i] : 0;
150 
151  //conversion to QWORD for easier use in inline assembly
152  DWORD64 _argC = argC - i;
153  DWORD back_esp = 0;
154 
155  __asm
156  {
157  ;//keep original esp in back_esp variable
158  mov back_esp, esp
159 
160  ;//align esp to 8, without aligned stack some syscalls may return errors !
161  and esp, 0xFFFFFFF8
162 
163  X64_Start();
164 
165  ;//fill first four arguments
166  push _rcx
167  X64_Pop(_RCX);
168  push _rdx
169  X64_Pop(_RDX);
170  push _r8
171  X64_Pop(_R8);
172  push _r9
173  X64_Pop(_R9);
174 
175  push edi
176  X64_Push( _R10 )
177 
178  push restArgs
179  X64_Pop(_RDI);
180 
181  push _argC
182  X64_Pop(_RAX);
183 
184  ;//put rest of arguments on the stack
185  test eax, eax
186  jz _ls_e
187  lea edi, dword ptr [edi + 8*eax - 8]
188 
189  _ls:
190  test eax, eax
191  jz _ls_e
192  push dword ptr [edi]
193  sub edi, 8
194  sub eax, 1
195  jmp _ls
196  _ls_e:
197 
198  ;//create stack space for spilling registers
199  sub esp, 0x28
200 
201  mov eax, idx
202  push _rcx
203  X64_Pop( _R10 );
204  EMIT( 0x0F ) EMIT( 0x05 ); // syscall
205 
206  ;//cleanup stack
207  push _argC
208  X64_Pop(_RCX);
209  lea esp, dword ptr [esp + 8*ecx + 0x20]
210 
211  X64_Pop( _R10 )
212  pop edi
213 
214  ;//set return value
215  X64_Push(_RAX);
216  pop _rax.dw[0]
217 
218  X64_End();
219 
220  mov esp, back_esp
221  }
222 
223  return _rax.v;
224  #else
225  return STATUS_NOT_SUPPORTED;
226  #endif
227  }
228 #pragma warning(default : 4409 4100)
229 
236  BLACKBONE_API void memcpy64( DWORD64 /*dst*/, DWORD64 /*src*/, DWORD /*size*/ );
237 
243  BLACKBONE_API DWORD64 getTEB64( _TEB64& out );
244 
251  BLACKBONE_API DWORD64 GetModuleHandle64( const wchar_t* lpModuleName, DWORD* pSize = nullptr );
252 
258  BLACKBONE_API DWORD64 getNTDLL64( DWORD* pSize = nullptr );
259 
264  BLACKBONE_API DWORD64 getLdrGetProcedureAddress();
265 
272  BLACKBONE_API DWORD64 GetProcAddress64( DWORD64 hModule, const char* funcName );
273 
279  BLACKBONE_API DWORD64 LoadLibrary64( const wchar_t* path );
280 
281 private:
282  DWORD64 _ntdll64 = 0; // 64bit ntdll address
283  DWORD64 _LdrGetProcedureAddress = 0; // LdrGetProcedureAddress address in 64bit ntdll
284  DWORD _ntdll64Size = 0; // size of ntdll64 image
285 };
286 
287 }
Definition: NativeStructures.h:82
BLACKBONE_API void memcpy64(DWORD64, DWORD64, DWORD)
Copy memory beyond 4GB limit
Definition: Types.h:78
BLACKBONE_API DWORD64 LoadLibrary64(const wchar_t *path)
Load 64 bit module into current process
Definition: Wow64Local.cpp:229
DWORD64 X64Call(ptr_t func, Args...args)
Call 64 bit function
Definition: Wow64Local.h:34
BLACKBONE_API DWORD64 getTEB64(_TEB64 &out)
Get native bit TEB
Definition: Wow64Local.cpp:65
BLACKBONE_API DWORD64 GetProcAddress64(DWORD64 hModule, const char *funcName)
64 bit implementation of GetProcAddress
Definition: Wow64Local.cpp:203
BLACKBONE_API DWORD64 getNTDLL64(DWORD *pSize=nullptr)
Get 64 bit ntdll base
Definition: Wow64Local.cpp:140
WOW64-x64 interface
Definition: Wow64Local.h:17
BLACKBONE_API DWORD64 getLdrGetProcedureAddress()
Get 'LdrGetProcedureAddress' address
Definition: Wow64Local.cpp:161
BLACKBONE_API DWORD64 GetModuleHandle64(const wchar_t *lpModuleName, DWORD *pSize=nullptr)
Gets 64 bit module base
Definition: Wow64Local.cpp:99
DWORD64 X64Syscall(int idx, Args...args)
Perform a syscall
Definition: Wow64Local.h:135
Definition: AsmHelper32.cpp:6