#include <iostream>
#include <fstream>
#include <Windows.h>
#include <TlHelp32.h>
#include <locale>
#include <vector>
#include <sstream>
std::wstring trim(const std::wstring& str) {
std::wstring::size_type start = str.find_first_not_of(L" \t\n");
if (start == std::wstring::npos) {
return L"";
}
std::wstring::size_type end = str.find_last_not_of(L" \t\n");
return str.substr(start, end - start + 1);
}
class InjectedASI {
public:
InjectedASI(const std::string& filename) : filename(filename) {}
std::string filename;
};
std::vector<InjectedASI> injectedASIs;
class Injector {
public:
Injector(const std::wstring& processName) : processName(processName) {}
void InjectASIs() {
uintptr_t process = GetProcessIdByName(processName);
if (process == 0) {
std::wcerr << L"Процесс " << processName << L" не найден." << std::endl;
return;
}
std::wstring baseDirectory = L".";
std::wstring asiDirectory = baseDirectory + L"\\asi";
WIN32_FIND_DATA findFileData;
HANDLE hFind = FindFirstFile((asiDirectory + L"\\*").c_str(), &findFileData);
if (hFind == INVALID_HANDLE_VALUE) {
std::wcerr << L"не найдены файлы ASI в директории 'asi'." << std::endl;
return;
}
do {
if (!(findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
std::wstring full_path = asiDirectory + L"\\" + std::wstring(findFileData.cFileName);
std::string full_path_str(full_path.begin(), full_path.end());
if (InjectDll(static_cast<DWORD>(process), full_path_str)) {
std::wcout << L"инжект: " << full_path << std::endl;
}
else {
std::wcerr << L"не удалось инжектировать: " << full_path << std::endl;
}
}
} while (FindNextFile(hFind, &findFileData) != 0);
FindClose(hFind);
}
private:
std::wstring processName;
uintptr_t GetProcessIdByName(const std::wstring& targetProcessName) {
HANDLE process_handle = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
PROCESSENTRY32 process_entry;
process_entry.dwSize = sizeof(process_entry);
do {
std::wstring processName = trim(process_entry.szExeFile);
if (_wcsicmp(processName.c_str(), targetProcessName.c_str()) == 0) {
uintptr_t process_id = process_entry.th32ProcessID;
CloseHandle(process_handle);
return process_id;
}
} while (Process32Next(process_handle, &process_entry));
CloseHandle(process_handle);
return 0;
}
bool InjectDll(DWORD processId, const std::string& dllPath) {
HANDLE processHandle = OpenProcess(PROCESS_CREATE_THREAD | PROCESS_QUERY_INFORMATION | PROCESS_VM_READ | PROCESS_VM_WRITE | PROCESS_VM_OPERATION, FALSE, processId);
if (processHandle == NULL) {
std::wcerr << L"не удалось открыть процесс для инъекции" << std::endl;
return false;
}
LPVOID loadLibraryAddress = reinterpret_cast<LPVOID>(GetProcAddress(GetModuleHandle(L"kernel32.dll"), "LoadLibraryA"));
if (loadLibraryAddress == NULL) {
CloseHandle(processHandle);
std::wcerr << L"Не удалось получить адрес функции LoadLibraryA." << std::endl;
return false;
}
LPVOID remoteDllPath = VirtualAllocEx(processHandle, NULL, dllPath.size(), MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
if (remoteDllPath == NULL) {
CloseHandle(processHandle);
std::wcerr << L"не удалось выделить память в целевом процессе." << std::endl;
return false;
}
if (!WriteProcessMemory(processHandle, remoteDllPath, dllPath.c_str(), dllPath.size(), NULL)) {
VirtualFreeEx(processHandle, remoteDllPath, 0, MEM_RELEASE);
CloseHandle(processHandle);
std::wcerr << L"Не удалось записать путь к DLL в целевой процесс." << std::endl;
return false;
}
HANDLE remoteThread = CreateRemoteThread(processHandle, NULL, 0,
reinterpret_cast<LPTHREAD_START_ROUTINE>(loadLibraryAddress), remoteDllPath, 0, NULL);
if (remoteThread == NULL) {
VirtualFreeEx(processHandle, remoteDllPath, 0, MEM_RELEASE);
CloseHandle(processHandle);
std::wcerr << L"не удалось создать удаленный поток в целевом процессе." << std::endl;
return false;
}
WaitForSingleObject(remoteThread, INFINITE);
CloseHandle(remoteThread);
VirtualFreeEx(processHandle, remoteDllPath, 0, MEM_RELEASE);
CloseHandle(processHandle);
return true;
}
};
int main() {
setlocale(LC_ALL, "Russian");
std::wifstream file("process.txt");
if (!file) {
std::wcerr << L"Не удается открыть файл process.txt" << std::endl;
return 1;
}
std::wstring targetProcessName;
if (std::getline(file, targetProcessName)) {
targetProcessName = trim(targetProcessName);
if (targetProcessName.empty()) {
std::wcerr << L"Имя процесса пусто в файле process.txt" << std::endl;
return 1;
}
Injector injector(targetProcessName);
injector.InjectASIs();
}
else {
std::wcerr << L"Имя процесса не найдено в файле process.txt" << std::endl;
return 1;
}
std::wcout << L"Нажмите любййю кнопку, чтобы закрыть программу..." << std::endl;
std::wcin.get();
return 0;
}