C ++ CreateRemoteThread Access Violation

I am having a problem with the CreateRemoteThread function.

I just created a small console window asking for a specific number to come out (so that it just stays open as long as I want to). Inside this program there is a function whose address I understand.

Now I want to call this function a second program via CreateRemoteThread, but it always says there is an access violation. Both are compiled in the same way.

Here is the code for my remote call:

bool SetPrivileges(){
HANDLE pt; //process token
TOKEN_PRIVILEGES ptp; //process token privileges
if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &pt))
{
    LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &ptp.Privileges[0].Luid);
    ptp.PrivilegeCount = 1;
    ptp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
    if (AdjustTokenPrivileges(pt, false, &ptp, sizeof(ptp), NULL, NULL))
    {
        return true;
    }
}

return false;}

int _tmain(int argc, _TCHAR* argv[])
 {
if (SetPrivileges())
{
    cout << "Enabled custom privileges" << endl;
}
else{
    cout << "Could not enable custom privileges" << endl << GetLastError() << endl;
}

CodeHelper ch;
DWORD processId = ch.GetProcessId("CallMeConsole.exe");
HANDLE proc = OpenProcess(PROCESS_ALL_ACCESS, false, processId);
DWORD Testaddress = 0x008642D0;
HANDLE thread = CreateRemoteThread(proc, NULL, 0, (LPTHREAD_START_ROUTINE)Testaddress, NULL, 0, NULL);
/*if (thread != 0)
{
    WaitForSingleObject(thread, INFINITE);
    CloseHandle(thread);
    CloseHandle(proc);
    cout << "success!" << endl;
}
else{
    cout << "error" << endl;
}*/
return 0;}

      

If I'm right, I just need VirtualAllocEx to reserve memory space if I want to inject some code. But I just want to call a programmatic function.

Do you have any ideas?

Searching didn't really help me. Thank!

+3


source to share


1 answer


For anyone else having the same problem, the code is provided in full. Only my example address went wrong.

Anyway, I hope this is a rough example of how to perform remote functions of the selected program.



Thanks everyone.

0


source







All Articles