This repository has been archived by the owner on Feb 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
injector.cpp
86 lines (68 loc) · 2.06 KB
/
injector.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// injector.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<Windows.h>
#include"detours.h"
#include <tlhelp32.h>
#include<iostream>
using namespace std;
DWORD MyGetProcessId(LPCTSTR ProcessName)
{
PROCESSENTRY32 pt;
HANDLE hsnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
pt.dwSize = sizeof(PROCESSENTRY32);
if (Process32First(hsnap, &pt))
{
do
{
if (!lstrcmpi(pt.szExeFile, ProcessName))
{
CloseHandle(hsnap);
return pt.th32ProcessID;
}
} while (Process32Next(hsnap, &pt));
}
CloseHandle(hsnap);
return 0;
}
BOOL InjectDynamicLibrary(DWORD processid, char *dllPath)
{
HANDLE hTargetProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, processid);
if (hTargetProcess)
{
LPVOID LoadLibAddr = (LPVOID)GetProcAddress(GetModuleHandle(L"Kernel32.dll"), "LoadLibraryA");
cout << "\nGot Proc addr kernel32";
LPVOID LoadPath = VirtualAllocEx(hTargetProcess, 0, strlen(dllPath)+1, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
cout << "\nload path done";
WriteProcessMemory(hTargetProcess, (LPVOID)LoadPath, dllPath, strlen(dllPath)+1, NULL);
cout << "\nWrite proc memory";
HANDLE RemoteThread = CreateRemoteThread(hTargetProcess, NULL, 0, (LPTHREAD_START_ROUTINE)LoadLibAddr, LoadPath, 0, 0);
cout << "\nRemote thread created";
WaitForSingleObject(RemoteThread, INFINITE);
cout << "\nwait for single object";
/*VirtualFreeEx(hTargetProcess, LoadPath, strlen(dllPath), MEM_RELEASE);
cout << "\nvirtualfreeex";*/
CloseHandle(RemoteThread);
cout << "\nclose handle thread";
CloseHandle(hTargetProcess);
cout << "\nclose handle process ";
return TRUE;
}
else
return FALSE;
}
int main()
{
char dllPath[] = "E:\\dirDLL.dll";
char *pdllPath = dllPath;
DWORD pid = MyGetProcessId(TEXT("HxD.exe"));
wcout << "Process id : " << pid << endl;
cout << "DLL path : " << dllPath << endl;
bool b = InjectDynamicLibrary(pid, dllPath);
if (b == FALSE)
{
cout << "FALSE\n";
}
system("PAUSE");
return 0;
}