unfortunately I have a problem concerning injecting svchost. The code looks like that:
#include "Injection.h"
#pragma once
#include <Windows.h>
DLLInjection::DLLInjection()
{
}
void DLLInjection::InjectDLLTosvchost(LPSTR dllPath)
{
STARTUPINFO si = {};
PROCESS_INFORMATION pi = {};
HMODULE k32 = GetModuleHandle("kernel32.dll");
CreateProcess(NULL, "svchost.exe", NULL, NULL, FALSE, CREATE_SUSPENDED, NULL, NULL, &si, &pi);
HANDLE mem = VirtualAllocEx(pi.hProcess, NULL, 260, MEM_COMMIT | MEM_RESERVE , PAGE_READWRITE);
WriteProcessMemory(pi.hProcess, mem, dllPath, 260, NULL);
QueueUserAPC((PAPCFUNC)GetProcAddress(k32, "LoadLibraryA"), pi.hThread, (ULONG_PTR)mem);
QueueUserAPC((PAPCFUNC)GetProcAddress(k32, "ExitThread"), pi.hThread, 0);
ResumeThread(pi.hThread);
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
}
DLLInjection::~DLLInjection()
{
}
the dll which is executed looks pretty much like that:
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include "funkcje.h"
#include <iostream>
#include <shellapi.h>
#include <windows.h>
#include <tchar.h>
using namespace std;
void Hello()
{
MessageBox(NULL, (LPCWSTR)L"poczatkowy messagebox", (LPCWSTR)L"Tytul messagebox", MB_ICONINFORMATION);
char bufor[512];
sprintf(bufor, " -add -all -c \"c:\\Users\\Damian\\Desktop\\wwwtesthttpdev.crt\" -s -r LocalMachine root");
wchar_t bufor2[200];
mbstowcs(bufor2, bufor, strlen(bufor) + 1);
LPWSTR ptr = bufor2;
STARTUPINFO startInfo = { 0 };
PROCESS_INFORMATION processInfo = { 0 };
BOOL bSucces = CreateProcess((LPWSTR)(L"c:\\Program Files\\Microsoft SDKs\\Windows\\v7.1A\\Bin\\certmgr.exe"), ptr, NULL, NULL, 0, 0, NULL, NULL, &startInfo, &processInfo);
if (bSucces)
{
cout << "Process Started" << endl
<< "Process ID: " << processInfo.dwProcessId << endl;
}
else
{
cout << "Error to start a process " << GetLastError() << endl;
}
MessageBox(NULL, (LPCWSTR)L"koncowy messagebox", (LPCWSTR)L"Tytul messagebox", MB_ICONINFORMATION);
cin.get();
}
and the dll is loading fine, because I do have the two messageboxes (one before executing the createProcess command and one after) but the problem is that I do not have rights to successfully make the certmgr.exe command (and it returns the message that certmgr was not successful. If I open the program with administrator rights everything works fine. But it shouldn't work like that. I am trying to get attached to the svchost process which should have administrator rights, but despite doing it I still do not have rights. Can anyone help me answering the question how I can make my program pass the administrator rights to the function that is executed in Dllmain. Thank you in advance!! the error with certmgr
I have also tried with OpenProcess:
bool Process::InjectDll(char * dllName, unsigned int processID)
{
HANDLE pHandle = OpenProcess(PROCESS_ALL_ACCESS, false, processID);
if (pHandle == INVALID_HANDLE_VALUE)
return false;
void * address = VirtualAllocEx(pHandle, NULL, strlen(dllName), MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
if (!WriteProcessMemory(pHandle, address, (LPVOID)dllName, strlen(dllName), NULL))
return false;
HMODULE hK32 = GetModuleHandle("Kernel32");
HANDLE tHandle = CreateRemoteThread(pHandle, NULL, 0,
(LPTHREAD_START_ROUTINE)GetProcAddress(hK32, "LoadLibraryA"),
address, 0, NULL);
WaitForSingleObject(tHandle, INFINITE);
DWORD dllAddress;
GetExitCodeThread(tHandle, &dllAddress);
CloseHandle(tHandle);
VirtualFreeEx(pHandle, address, 0, MEM_RELEASE);
tHandle = CreateRemoteThread(pHandle, NULL, 0,
(LPTHREAD_START_ROUTINE)GetProcAddress(hK32, "FreeLibrary"), (void*
)&dllAddress, 0, NULL);
WaitForSingleObject(tHandle, INFINITE);
CloseHandle(tHandle);
return true;
}
but it also doesn't work