Jump to content
Sign in to follow this  
bornman

CreateRemoteThread não retorna erro mas não funciona

Recommended Posts

Olá, estou aprendendo sobre injeção em processos e estou tentando criar um injetor de dll em C++, utilizando WriteProcessMemory e CreateRemoteThread, as funções não retornam erros porém a dll não é injetada.

Segue o código:

#include "pch.h"
#include <iostream>
#include "Injections.h"

int main(int argc, char *argv[])
{
	const char *dllPath = "example.dll";
	DWORD processId = getProcID(L"process.exe");
	HANDLE procHandle = getProcHandle(processId);
	bool inj = injectDll(procHandle, dllPath);
	CloseHandle(procHandle);
	getchar();
	return 0;
}
#include "pch.h"
#include "Injections.h"
#include <iostream>

DWORD getProcID(const wchar_t* procName) {
	DWORD procId = 0;
	HANDLE hSnap = (CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0));
	if (hSnap != INVALID_HANDLE_VALUE) {
		PROCESSENTRY32 procEntry;
		procEntry.dwSize = sizeof(procEntry);

		if (Process32First(hSnap, &procEntry)) {
			do {
				if (!_wcsicmp(procEntry.szExeFile, procName)) {
					procId = procEntry.th32ProcessID;
					break;
				}
			} while (Process32Next(hSnap, &procEntry));
		}
	}
	CloseHandle(hSnap);
	return procId;
}

HANDLE getProcHandle(DWORD pid) {
	HANDLE h = OpenProcess(PROCESS_CREATE_THREAD | 
						   PROCESS_QUERY_INFORMATION | 
						   PROCESS_VM_READ | 
						   PROCESS_VM_WRITE | 
						   PROCESS_VM_OPERATION, FALSE, pid);

	if (h == NULL) {
		std::cout << "Can't get process handle!" << std::endl;
		exit(-1);
	}

	return h;
}

bool injectDll(HANDLE procHandle, const char *dllPath) {
	LPVOID loadPath = (LPVOID)VirtualAllocEx(procHandle, 0, strlen(dllPath)+1, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);

	if (loadPath == NULL) {
		std::cout << "Can't allocate memory!" << std::endl;
		exit(-1);
	}

	int n = WriteProcessMemory(procHandle, loadPath, dllPath, strlen(dllPath)+1, NULL);

	if (n == 0) {
		std::cout << "Couldn't write memory!" << std::endl;
	}

	HANDLE thread = CreateRemoteThread(procHandle, NULL, 0, (LPTHREAD_START_ROUTINE)LoadLibraryA, loadPath, 0, NULL);

	if (thread != NULL) {
		std::cout << "Injection Sucessfull!" << std::endl;
		return true;
	}

	exit(-1);
}

 

Edited by bornman

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...