This code is from a loader I used in the past, not in the DLL itself. It does just what I said above.
Code:
bool injectDLL(DWORD procID, std::string dll) {
HMODULE hLocKernel32 = GetModuleHandle("Kernel32");
FARPROC hLocLoadLibrary = GetProcAddress(hLocKernel32, "LoadLibraryA");
HANDLE hToken;
TOKEN_PRIVILEGES tkp;
if(OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) {
LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &tkp.Privileges[0].Luid);
tkp.PrivilegeCount = 1;
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hToken, 0, &tkp, sizeof(tkp), NULL, NULL);
}
HANDLE hProc = OpenProcess(PROCESS_ALL_ACCESS, FALSE, procID);
dll += '\0';
LPVOID hRemoteMem = VirtualAllocEx(hProc, NULL, dll.size(), MEM_COMMIT, PAGE_READWRITE);
DWORD numBytesWritten;
WriteProcessMemory(hProc, hRemoteMem, dll.c_str(), dll.size(), &numBytesWritten);
HANDLE hRemoteThread = CreateRemoteThread(hProc, NULL, 0, (LPTHREAD_START_ROUTINE)hLocLoadLibrary, hRemoteMem, 0, NULL);
std::cout << hRemoteThread << std::endl;
bool res = false;
if (hRemoteThread) { res = (bool)WaitForSingleObject(hRemoteThread, MAXWAIT) != WAIT_TIMEOUT; }
VirtualFreeEx(hProc, hRemoteMem, dll.size(), MEM_RELEASE);
CloseHandle(hProc);
return res;
}
I guess it depends on which method of injection you're using. The thing I understand about setting debug privileges is that the remote process will (or can) receive notifications that you're debugging it. This could be a problem for detection in the future. There was a detailed write up on how to circumvent this but I can not seem to locate it at this moment. I'll post it on here if I can find it, it's really a good read.