Injecting code to main thread menu

User Tag List

Results 1 to 5 of 5
  1. #1
    kajko's Avatar Member
    Reputation
    4
    Join Date
    Oct 2009
    Posts
    48
    Thanks G/R
    3/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Injecting code to main thread

    Hello,

    I am trying to inject and execute code to main WOW thread.

    After ResumeThread, it crashes with this:

    This application has encountered a critical error:

    ERROR #132 (0x85100084) Fatal exception!

    Program: S:\World of Warcraft\Wow.exe
    ProcessID: 3944
    Exception: ACCESS_VIOLATION

    The instruction at "0x00000000" referenced memory at "0x00000000".
    The memory could not be "executed".
    and here is the code I am using, it does nothing, basically, just want to test code cave injection.

    Code:
    	__declspec(naked) DWORD codeasm()
    	{
    		__asm
    		{
    			//   Placeholder for the return address
    			push 0xDEADBEEF
    
    			pushfd
    			pushad
    
    			popad
    			popfd
    
     			ret
    		};
    	}
    
    	void Manager::doNothing()
    	{
    		unsigned long threadID = m_pMemMan->get_main_thread_id();
    		HANDLE hThread;
    
    		DWORD cbCodeSize = 100;
    
    		hThread = OpenThread((THREAD_GET_CONTEXT | THREAD_SET_CONTEXT | THREAD_SUSPEND_RESUME | THREAD_QUERY_INFORMATION), false, threadID);
    		SuspendThread(hThread);
    
    		LPVOID pCode = VirtualAllocEx(hThread, NULL, cbCodeSize, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
    
    		unsigned long oldIP, oldprot;
    		CONTEXT ctx;
    		ZeroMemory(&ctx, sizeof(ctx));
    		ctx.ContextFlags = CONTEXT_CONTROL;
    		GetThreadContext(hThread, &ctx);
    		oldIP = ctx.Eip;
    		ctx.Eip = (DWORD)pCode;
    		ctx.ContextFlags = CONTEXT_CONTROL;
    
    		VirtualProtect(codeasm, cbCodeSize, PAGE_EXECUTE_READWRITE, &oldprot);
    		// store return address
    		memcpy((void *)((unsigned long)codeasm + 1), &oldIP, sizeof(unsigned long));
    		
    		WriteProcessMemory(hThread, pCode, &codeasm, cbCodeSize, NULL);
    		SetThreadContext(hThread, &ctx);
    
    		ResumeThread(hThread);
    
    		Sleep(1000);
    		CloseHandle(hThread);
    		VirtualFreeEx(hThread, pCode, cbCodeSize, MEM_RELEASE);
    	}
    Assuming I, with your help, make it work:
    Is this better/recommended way or should I go, maybe for DLL injection ?

    Thaks
    Kajko

    Injecting code to main thread
  2. #2
    culino2's Avatar Elite User
    Reputation
    336
    Join Date
    Feb 2013
    Posts
    183
    Thanks G/R
    139/72
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    VirtualAllocEx takes the process handle, not the thread handle.

    Same for VirtualFreeEx, WriteProcessMemory.
    Last edited by culino2; 05-11-2015 at 01:33 PM.

  3. #3
    kajko's Avatar Member
    Reputation
    4
    Join Date
    Oct 2009
    Posts
    48
    Thanks G/R
    3/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by culino2 View Post
    VirtualAllocEx takes the process handle, not the thread handle.

    Same for VirtualFreeEx, WriteProcessMemory.
    Updated:

    Code:
    		unsigned long threadID = m_pMemMan->get_main_thread_id();
    		HANDLE hThread;
    
    		DWORD cbCodeSize = 100;
    
    		hThread = OpenThread((THREAD_GET_CONTEXT | THREAD_SET_CONTEXT | THREAD_SUSPEND_RESUME | THREAD_QUERY_INFORMATION), false, threadID);
    		SuspendThread(hThread);
    
    		LPVOID pCode = VirtualAllocEx(m_pMemMan->hProcess, NULL, cbCodeSize, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
    
    		unsigned long oldIP, oldprot;
    		CONTEXT ctx;
    		ZeroMemory(&ctx, sizeof(ctx));
    		ctx.ContextFlags = CONTEXT_CONTROL;
    		GetThreadContext(hThread, &ctx);
    		oldIP = ctx.Eip;
    		ctx.Eip = (DWORD)pCode;
    		ctx.ContextFlags = CONTEXT_CONTROL;
    
    		VirtualProtect(codeasm, cbCodeSize, PAGE_EXECUTE_READWRITE, &oldprot);
    		// store return address
    		memcpy((void *)((unsigned long)codeasm + 1), &oldIP, sizeof(unsigned long));
    		
    		WriteProcessMemory(m_pMemMan->hProcess, pCode, &codeasm, cbCodeSize, NULL);
    		SetThreadContext(hThread, &ctx);
    
    		ResumeThread(hThread);

    still crash same way

  4. #4
    culino2's Avatar Elite User
    Reputation
    336
    Join Date
    Feb 2013
    Posts
    183
    Thanks G/R
    139/72
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by kajko View Post
    Updated:

    Code:
    		unsigned long threadID = m_pMemMan->get_main_thread_id();
    		HANDLE hThread;
    
    		DWORD cbCodeSize = 100;
    
    		hThread = OpenThread((THREAD_GET_CONTEXT | THREAD_SET_CONTEXT | THREAD_SUSPEND_RESUME | THREAD_QUERY_INFORMATION), false, threadID);
    		SuspendThread(hThread);
    
    		LPVOID pCode = VirtualAllocEx(m_pMemMan->hProcess, NULL, cbCodeSize, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
    
    		unsigned long oldIP, oldprot;
    		CONTEXT ctx;
    		ZeroMemory(&ctx, sizeof(ctx));
    		ctx.ContextFlags = CONTEXT_CONTROL;
    		GetThreadContext(hThread, &ctx);
    		oldIP = ctx.Eip;
    		ctx.Eip = (DWORD)pCode;
    		ctx.ContextFlags = CONTEXT_CONTROL;
    
    		VirtualProtect(codeasm, cbCodeSize, PAGE_EXECUTE_READWRITE, &oldprot);
    		// store return address
    		memcpy((void *)((unsigned long)codeasm + 1), &oldIP, sizeof(unsigned long));
    		
    		WriteProcessMemory(m_pMemMan->hProcess, pCode, &codeasm, cbCodeSize, NULL);
    		SetThreadContext(hThread, &ctx);
    
    		ResumeThread(hThread);

    still crash same way
    Change __declspec(naked) DWORD codeasm() to static __declspec(naked) DWORD codeasm()

    This should do the trick.

  5. #5
    kajko's Avatar Member
    Reputation
    4
    Join Date
    Oct 2009
    Posts
    48
    Thanks G/R
    3/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by culino2 View Post
    Change __declspec(naked) DWORD codeasm() to static __declspec(naked) DWORD codeasm()

    This should do the trick.
    Thanks, that static did the trick.

Similar Threads

  1. [Bot] Injection code into wow. Do you have to call functions from the main thread?
    By Miivers in forum World of Warcraft Bots and Programs
    Replies: 2
    Last Post: 01-13-2014, 02:56 PM
  2. Executing injected code on main thread
    By mozartmclaus in forum Diablo 3 Memory Editing
    Replies: 0
    Last Post: 05-23-2012, 03:04 PM
  3. Out of the main thread
    By Shamun in forum WoW Memory Editing
    Replies: 11
    Last Post: 12-20-2008, 06:36 AM
  4. Bugs when injecting code
    By bigtimt in forum WoW Memory Editing
    Replies: 4
    Last Post: 10-03-2008, 05:33 AM
All times are GMT -5. The time now is 07:17 PM. Powered by vBulletin® Version 4.2.3
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Google Authenticator verification provided by Two-Factor Authentication (Free) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search