Akebi-GC/injector/src/main.cpp

139 lines
3.7 KiB
C++
Raw Normal View History

2022-02-27 11:38:51 +00:00
#include <Windows.h>
#include <iostream>
#include <sstream>
#include <filesystem>
2022-05-17 00:13:41 +00:00
#include <SimpleIni.h>
#include <cheat-base/Logger.h>
2022-02-27 11:38:51 +00:00
#include "injector.h"
#include "util.h"
const std::string GlobalGenshinProcName = "GenshinImpact.exe";
const std::string ChinaGenshinProcName = "YuanShen.exe";
static CSimpleIni ini;
bool OpenGenshinProcess(HANDLE* phProcess, HANDLE* phThread);
2022-02-27 11:38:51 +00:00
int main(int argc, char* argv[])
{
Logger::SetLevel(Logger::Level::Debug, Logger::LoggerType::ConsoleLogger);
2022-05-17 00:13:41 +00:00
auto path = std::filesystem::path(argv[0]).parent_path();
current_path(path);
2022-02-27 11:38:51 +00:00
WaitForCloseProcess(GlobalGenshinProcName);
WaitForCloseProcess(ChinaGenshinProcName);
2022-02-27 11:38:51 +00:00
Sleep(1000); // Wait for unloading all dlls.
2022-02-27 11:38:51 +00:00
ini.SetUnicode();
ini.LoadFile("cfg.ini");
2022-02-27 11:38:51 +00:00
HANDLE hProcess, hThread;
if (!OpenGenshinProcess(&hProcess, &hThread))
{
std::cout << "Failed to open GenshinImpact process." << std::endl;
system("pause");
return 1;
}
2022-02-27 11:38:51 +00:00
current_path(path);
ini.SaveFile("cfg.ini");
std::string filename = (argc == 2 ? argv[1] : "CLibrary.dll");
std::filesystem::path currentDllPath = std::filesystem::current_path() / filename;
2022-05-17 00:13:41 +00:00
#ifdef _DEBUG
std::filesystem::path tempDllPath = std::filesystem::temp_directory_path() / filename;
2022-02-27 11:38:51 +00:00
std::error_code ec;
std::filesystem::copy_file(currentDllPath, tempDllPath, std::filesystem::copy_options::update_existing, ec);
if (ec)
{
LOG_ERROR("Copy dll failed: %s", ec.message().c_str());
std::system("pause");
}
2022-05-17 00:13:41 +00:00
InjectDLL(hProcess, tempDllPath.string());
2022-05-17 00:13:41 +00:00
#else
InjectDLL(hProcess, currentDllPath.string());
2022-05-17 00:13:41 +00:00
#endif
2022-02-27 11:38:51 +00:00
Sleep(2000);
ResumeThread(hThread);
CloseHandle(hProcess);
2022-02-27 11:38:51 +00:00
}
bool OpenGenshinProcess(HANDLE* phProcess, HANDLE* phThread)
2022-05-17 00:13:41 +00:00
{
2022-02-27 11:38:51 +00:00
HANDLE hToken;
BOOL TokenRet = OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &hToken);
2022-07-13 09:06:40 +00:00
if (!TokenRet)
{
LOG_LAST_ERROR("Privilege escalation failed!");
return false;
}
2022-07-13 09:06:40 +00:00
auto filePath = util::GetOrSelectPath(ini, "Inject", "GenshinPath", "genshin path", "Executable\0GenshinImpact.exe;YuanShen.exe\0");
auto commandline = ini.GetValue("Inject", "GenshinCommandLine");
2022-07-13 09:06:40 +00:00
LPSTR lpstr = commandline == nullptr ? nullptr : const_cast<LPSTR>(commandline);
if (!filePath)
return false;
2022-07-13 09:06:40 +00:00
DWORD pid = FindProcessId("explorer.exe");
if (pid == 0)
{
LOG_ERROR("Can't find 'explorer' pid!");
return false;
}
2022-07-13 09:06:40 +00:00
std::string CurrentDirectory = filePath.value();
int pos = CurrentDirectory.rfind("\\", CurrentDirectory.length());
CurrentDirectory = CurrentDirectory.substr(0, pos);
2022-07-13 09:06:40 +00:00
HANDLE handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
2022-07-13 09:06:40 +00:00
SIZE_T lpsize = 0;
InitializeProcThreadAttributeList(NULL, 1, 0, &lpsize);
2022-07-13 09:06:40 +00:00
char* temp = new char[lpsize];
LPPROC_THREAD_ATTRIBUTE_LIST AttributeList = (LPPROC_THREAD_ATTRIBUTE_LIST)temp;
InitializeProcThreadAttributeList(AttributeList, 1, 0, &lpsize);
2022-07-13 09:06:40 +00:00
if (!UpdateProcThreadAttribute(AttributeList, 0, PROC_THREAD_ATTRIBUTE_PARENT_PROCESS,
&handle, sizeof(HANDLE), NULL, NULL))
{
LOG_WARNING("UpdateProcThreadAttribute failed ! (%d).\n", GetLastError());
}
2022-07-13 09:06:40 +00:00
STARTUPINFOEXA si{};
si.StartupInfo.cb = sizeof(si);
si.lpAttributeList = AttributeList;
2022-07-13 09:06:40 +00:00
PROCESS_INFORMATION pi{};
BOOL result = CreateProcessAsUserA(hToken, const_cast<LPSTR>(filePath->data()), lpstr,
2022-07-13 09:06:40 +00:00
0, 0, 0, EXTENDED_STARTUPINFO_PRESENT | CREATE_SUSPENDED, 0,
(LPSTR)CurrentDirectory.data(), (LPSTARTUPINFOA)&si, &pi);
bool isOpened = result;
if (isOpened)
{
ini.SaveFile("cfg.ini");
*phThread = pi.hThread;
*phProcess = pi.hProcess;
}
else
{
LOG_LAST_ERROR("Failed to create game process.");
LOG_ERROR("If you have problem with GenshinImpact.exe path. You can change it manually in cfg.ini.");
}
2022-07-13 09:06:40 +00:00
DeleteProcThreadAttributeList(AttributeList);
2022-07-13 09:06:40 +00:00
delete[] temp;
return isOpened;
2022-02-27 11:38:51 +00:00
}