MMOwned - World of Warcraft Exploits, Hacks, Bots and Guides

Homepage Register FAQ Members Mark Forums Read Advertise Marketplace FPSowned


Go Back   MMOwned - World of Warcraft Exploits, Hacks, Bots and Guides > World of Warcraft > Bots and Programs > WoW Memory Editing
Reload this Page Code Caving WoW [With Code (C++)]
WoW Memory Editing WoW Memory Editing for learning purposes only.

Reply
 
LinkBack Thread Tools
Code Caving WoW [With Code (C++)]
(#1)
Old
blizzo is Offline
Private
Rep Power: 1
Reputation: 6
blizzo is an unknown quantity at this point
 
Posts: 12
Join Date: Jun 2008
Location: England
Code Caving WoW [With Code (C++)] - 06-18-2008

** If anything is unclear, I'm sorry. The code isn't great cos i hacked it together this afternoon just a PoC. Enjoy! **

I dunno if anyone has posted anything about using a code cave to run functions in Wow instead of injecting a DLL, but I'm going to now.

First, for those that don't know what a code cave is:
It is somewhere in a remote process' memory where you put your own code, maybe some space that isn't used or some space that you allocate with VirtualAllocEx and free with VirtualFreeEx

You can insert code in to this space and then run it!

After you insert your code you can pause the current thread, change the instruction pointer to where your code is, then resume the thread. This will run your code.
Of course, if you jump to some code that uses registers/flags and you don't restore them bad things will happen. The easiest way to do that is just to push them on the stack at the start of the injected code and pop them off at the end.
Also you'll probably want to return to where the thread was before your code ran, so you'll need to push the return address on to the stack first.

Code:
		push 0xDEADBABE		//   Placeholder for the return address
		pushfd				//   Save the flags and registers
		pushad
		//-- Something useful goes here :)
		popad				//   Restore the registers and flags
		popfd
		ret					//   Return control to the hijacked thread
Something like that would be a good start.

So... HOWTO:
Here's what i did:
  1. Find the target Window Handle
  2. Find the main thread ID
  3. Open the process PID
  4. Open the thread
  5. Allocate space for the code
  6. Make any changes to the "to-be-injected" code (return address, etc.)
  7. Write the code to the allocated space
  8. Pause the thread
  9. Update the instruction pointer to point the allocated space
  10. Resume the thread
  11. Wait a bit for the code to run
  12. De-allocate the memory allocated for the code
  13. Have a beer

In you code you could call a function or whatever, but you won't be able to get any return values... unless you allocate some space for the result and have you code write that result to that known location and then read it later. So in addition to the above you must:
  1. Allocate space for the result
  2. Edit your code to write to the allocated location
  3. Read the result after the thread is resumed

I have produced some code (in C++) that will do all of this, it can be found here: [Only registered and activated users can see links. ].
It calls the CompareUnitFaction function to get the reaction of one unit's faction to another.
It doesn't do anything fancy and the Unit 1 and Unit 2 base addresses are hardcoded in. But if you don't know how to find them this really isn't for you

*
Credit here to kynox and Shynd for info about CompareUnitFaction
Finding the functions and working out the arguments takes some time and a good disassembler/debugger, personally I like IDA (cos I can do other things - mega drive hehe)
*

The CompareUnitFaction (as it's being called) function takes two arguments, a pointer to the current Unit and a pointer to the second Unit. The current Unit is in the register ECX and the second Unit is pushed on to the stack. In my code it is called by doing:
Code:
		push 0xDEADBABE		//	Place holder for Unit2
		mov ecx, 0xDEADBABE	//	Place holder for Unit1
		mov eax, 0x005D4AB0	//	Offset for the CompareUnitFaction function
		call eax
This will run and return the faction's reaction to you in EAX, I used this code to right that value in to memory where it could be read:
Code:
		mov ebx, 0xDEADBABE	//	Offset to read result from :)
		mov dword ptr [ebx], eax
In the program the place holders are replaced with memcpy after the memory protection has been changed.
injectCode is the function with the asm in it :
void __declspec(naked) injectCode(void){ _asm { ... } }
naked means no prologue or epilogue
Code:
		// Return address
		memcpy((LPVOID)((DWORD)injectCode + 0x01),	&oldIP, 4);
		// Unit2 base address
		memcpy((LPVOID)((DWORD)injectCode + 0x08),	&dwUnit2, 4);
		// Unit1 base address
		memcpy((LPVOID)((DWORD)injectCode + 0x0D),	&dwUnit1, 4);
		// Result address
		memcpy((LPVOID)((DWORD)injectCode + 0x19),	&dwResutlAddress, 4);
OK, think that covers everything. I have NOT tested this on a live server only on an emulated server so I dunno if you'll get disconnected or whatever. I didn't on the emulated server i was on (OpenAscent TRUNK r334/Release-Win-X86).
If i have missed anything or you have any question I will try to answer them as best I can

Edit: Here is an output from the program very verbose hehe
Code:
[?] PID:        2980
[?] TID:        3012
[?] HWND:       0x00460816
[?] Handle:     0x7A4
[?] Code to inject: 0x00401000 - 0x00401030 (0x00000030)
[?] Thread Paused...
[?] Old Instruction Pointer : 0x7C90EB94
[?] Enabled modify for inject-code...OK!
[?] Updating return address (0x00401001 -> 0x7C90EB94)...OK
[?] Updating Unit2 address (0x00401008 -> 0x1070D0F0)...OK
[?] Updating Unit1 address (0x0040100D -> 0x105F8008)...OK
[?] Updating result address (0x00401018 -> 0x08FD0000)...OK
[?] Writing code stub to 0x01710000...pass.
[?] Updating the thread context...pass.
[?] Thread Resumed at 0x01710000...done!
[?] GetFactionHostility(0x105F8008, 0x1070D0F0) = 0x00000004
[?] Closing thread...done
[?] Freeing memory...done.
[?] Closing process...done.

Last edited by blizzo; 06-18-2008 at 12:04 PM.
Reply With Quote

Donate to remove ads.
(#2)
Old
Shynd's Avatar
Shynd is Offline
Master Sergeant
Rep Power: 1
Reputation: 28
Shynd is on a distinguished road
 
Posts: 121
Join Date: May 2008
06-18-2008

Link seems to be dead. Nice work, though.
Reply With Quote
(#3)
Old
blizzo is Offline
Private
Rep Power: 1
Reputation: 6
blizzo is an unknown quantity at this point
 
Posts: 12
Join Date: Jun 2008
Location: England
06-18-2008

OK, link is fixed. Thanks for pointing that out Shynd
Reply With Quote
(#4)
Old
Shynd's Avatar
Shynd is Offline
Master Sergeant
Rep Power: 1
Reputation: 28
Shynd is on a distinguished road
 
Posts: 121
Join Date: May 2008
06-18-2008

Only suggestion I'd make is instead of sleeping a static 2000ms while waiting for it to execute, I'd do a loop while reading memory on your dwResult address, waiting for the result to != 0 (or initialize it as 0xFFFFFFFF and sleep until it != 0xFFFFFFFF).
Code:
DWORD dwResult = 0xFFFFFFFF;
WriteProcessMemory(hProcess, pInjectResult, (LPVOID)dwResult, sizeof(DWORD), NULL);
for (int i = 0; i < 200 && dwResult == 0xFFFFFFFF; i++)
{
    if (!ReadProcessMemory(hProcess, pInjectResult, &dwResult, sizeof(DWORD), &dwBytesRead ))
        break;
    Sleep(10);
}
Slightly more overhead, but you'll get the result as soon as it's available.
Reply With Quote
(#5)
Old
blizzo is Offline
Private
Rep Power: 1
Reputation: 6
blizzo is an unknown quantity at this point
 
Posts: 12
Join Date: Jun 2008
Location: England
06-18-2008

Yes, that is a good idea I have something similar in my new code already. But you're code looks better
However, you do have to wait a bit longer after it has finished to make sure that the flags and registers are popped off the stack and ret is called otherwise Wow will crash cos the memory is deallocated while it's still being executed hehe. The Wow send error report is pretty useful for debugging (no surprise) I don't know enough (anything) about writing a debugger but some kind of break point would be a useful thing, at the return address so you read the result and de-allocate the memory then. Maybe when it's not 4AM
Reply With Quote
(#6)
Old
Shynd's Avatar
Shynd is Offline
Master Sergeant
Rep Power: 1
Reputation: 28
Shynd is on a distinguished road
 
Posts: 121
Join Date: May 2008
06-19-2008

You could also always do: (pseudo code)
Code:
HANDLE hThread = CreateRemoteThread(hProcess, NULL, NULL, (LPTHREAD_START_ROUTINE)codecaveaddress, NULL, NULL, NULL);
WaitForObject(hThread, INFINITE);
DWORD dwExit;
GetExitCodeThread(hThread, &dwExit); //dwExit is now the value of EAX when RETN is hit
Little more overhead but it makes it much easier to handle. If you're only calling it every once in a while, it shouldn't cause any problems.


And writing a debugger isn't too difficult. DebugActiveProcess API for the win. You just have to handle the breakpoints in the correct manner, something that's not documented very many places (if you're still interested, even after understanding how easily debuggers can be detected, let me know and I'll make a post about it).


Edit: Oh yeah,

Last edited by Shynd; 06-19-2008 at 12:31 AM.
Reply With Quote
(#7)
Old
kynox's Avatar
kynox is Offline
Cypher's Pimp

Rep Power: 5
Reputation: 529
kynox is a glorious beacon of lightkynox is a glorious beacon of lightkynox is a glorious beacon of lightkynox is a glorious beacon of lightkynox is a glorious beacon of lightkynox is a glorious beacon of light
 
Posts: 319
Join Date: Dec 2006
Location: Raping your Stack
06-19-2008

Quote:
Originally Posted by Shynd View Post
You could also always do: (pseudo code)
Code:
HANDLE hThread = CreateRemoteThread(hProcess, NULL, NULL, (LPTHREAD_START_ROUTINE)codecaveaddress, NULL, NULL, NULL);
WaitForObject(hThread, INFINITE);
DWORD dwExit;
GetExitCodeThread(hThread, &dwExit); //dwExit is now the value of EAX when RETN is hit
Little more overhead but it makes it much easier to handle. If you're only calling it every once in a while, it shouldn't cause any problems.


And writing a debugger isn't too difficult. DebugActiveProcess API for the win. You just have to handle the breakpoints in the correct manner, something that's not documented very many places (if you're still interested, even after understanding how easily debuggers can be detected, let me know and I'll make a post about it).


Edit: Oh yeah,
This method won't work for any functions that read from the TLS (Being that its Thread local storage). Opening another thread won't have the TLS data in it, though you can put it in yourself


Do not PM me about the ME fix or other ME questions
Reply With Quote
(#8)
Old
Shynd's Avatar
Shynd is Offline
Master Sergeant
Rep Power: 1
Reputation: 28
Shynd is on a distinguished road
 
Posts: 121
Join Date: May 2008
06-19-2008

Good catch, was still thinking in terms of other gamehacking I've done in the past. Hadn't really thought about it =p.
Reply With Quote
(#9)
Old
kynox's Avatar
kynox is Offline
Cypher's Pimp

Rep Power: 5
Reputation: 529
kynox is a glorious beacon of lightkynox is a glorious beacon of lightkynox is a glorious beacon of lightkynox is a glorious beacon of lightkynox is a glorious beacon of lightkynox is a glorious beacon of light
 
Posts: 319
Join Date: Dec 2006
Location: Raping your Stack
06-19-2008

Haha, Yeah, it gave me a real run around when i first started out. In the end though i just hacked up some code to set the TLS in my thead to the s_curMgr pointer. Works a treat


Do not PM me about the ME fix or other ME questions
Reply With Quote
(#10)
Old
blizzo is Offline
Private
Rep Power: 1
Reputation: 6
blizzo is an unknown quantity at this point
 
Posts: 12
Join Date: Jun 2008
Location: England
06-20-2008

Looks good, I will have a go with create remote thread and setting the TLS.
Thanks for the rep Shynd
Reply With Quote
(#11)
Old
blizzo is Offline
Private
Rep Power: 1
Reputation: 6
blizzo is an unknown quantity at this point
 
Posts: 12
Join Date: Jun 2008
Location: England
06-20-2008

I'm not an X86 ASM expert so I dunno if this is the best way to do it, but it appears to work:

This code will load the address of the Object Manager in to EAX. Not really of any use but easy to check to see if it worked.

0x00153C60 is the linear address of the thread-local storage array for the main thread.
FS:0x2C is where the linear address of the thread-local storage is stored, so if you over write it it should be good. Please correct me if i am wrong :]

Code:
AT&T Style
push %fs:0x2C
movl $0x00153C60, %fs:0x2C
movl %fs:0x2C, %eax
movl (%eax), %eax
movl 0x10(%eax), %eax
pop %fs:0x2C
Code:
Intel Style (Untested)
PUSH FS:0x2C
MOV FS:0x2C, 0x00153C60
MOV EAX, FS:0x2C
MOV EAX, DWORD PTR [EAX]
MOV EAX, DWORD PTR [EAX + 0x10]
POP FS:0x2C
I'm not that exactly how you write it in Intel style asm, i have been using GCC and that uses AT&T syntax which is pretty different.
Reply With Quote
Reply

Donate to remove ads.

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are On



Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vBulletin Skin developed by: vBStyles.com


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344