Skip to content

Instantly share code, notes, and snippets.

@epcnt19 epcnt19/injector.cpp

Last active Dec 9, 2017
Embed
What would you like to do?
#include "stdafx.h"
#include <iostream>
#include <Windows.h>
using namespace std;
int main(int argc,char *argv[])
{
int pid;
char* dllpath = "C:\\users\\user\\documents\\visual studio 2015\\Projects\\injector\\x64\\Debug\\dllexample.dll";
cout << "Input process id" << endl;
cin >> pid;
HANDLE target_process = OpenProcess(PROCESS_ALL_ACCESS,FALSE,pid);
if (target_process == NULL) {
cout << "Faild to OpenProcess()" << endl;
return -1;
}
LPVOID la_addr = (LPVOID)GetProcAddress(GetModuleHandle(L"kernel32.dll"), "LoadLibraryA");
if (la_addr == NULL) {
cout << "Faild to GetProcAddress()" << endl;
return -1;
}
LPVOID alloc_addr = (LPVOID)VirtualAllocEx(target_process, NULL, strlen(dllpath), MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
if (alloc_addr == NULL) {
cout << "Faild to VirtualAllocEx()" << endl;
return -1;
}
int n = WriteProcessMemory(target_process, alloc_addr, dllpath, strlen(dllpath), NULL);
if (n == 0) {
cout << "Faild to WriteProcessMemory()" << endl;
return -1;
}
HANDLE thread_id = CreateRemoteThread(target_process, NULL, 0, (LPTHREAD_START_ROUTINE)la_addr, alloc_addr,0,NULL);
if (thread_id == NULL) {
cout << "Faild to CreateRemoteThread()" << endl;
cout << "GetLastError " << GetLastError() << endl;
return -1;
}
cout << "Success DLL Injection" << endl;
CloseHandle(target_process);
getchar();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.