Lookman
Prime Üye
- Ticaret Puanı: 1 / 0
- Katılım
- 22 Eyl 2023
- Mesajlar
- 5,408
- Beğeniler
- 1,121
C++:
#include <windows.h>
#include <string>
#include <thread>
#include <atomic>
#include <mutex>
#include <fstream>
#include <sddl.h>
#include <sstream>
#include <iomanip>
#include <vector>
#include <tlhelp32.h>
#include <cstdint>
#pragma pack(push, 1)
struct VGCHeader {
uint32_t msg_type;
uint32_t total_length;
uint32_t cmd_code;
uint8_t reserved[16];
uint32_t data_length;
};
#pragma pack(pop)
static const wchar_t* msg_type_name(uint32_t t) {
switch (t) {
case 0x02: return L"DISCONNECT";
case 0x03: return L"HEARTBEAT-A";
case 0x04: return L"HEARTBEAT-B";
case 0x05: return L"KEEP_ALIVE";
case 0x06: return L"ATTEST_RESP";
case 0x64: return L"INIT(100)";
case 0x65: return L"AUTH(101)";
case 0x66: return L"MSG(102)";
case 0x67: return L"SESSION(103)";
case 0x3E9: return L"ACK(1001)";
default: return L"UNKNOWN";
}
}
constexpr auto SERVICE_NAME = L"vgc";
std::mutex fw_mtx;
std::atomic_bool dns_frozen(false);
typedef LONG(NTAPI* NtSuspendProcess)(IN HANDLE ProcessHandle);
typedef LONG(NTAPI* NtResumeProcess)(IN HANDLE ProcessHandle);
static void SuspendProc(DWORD pid) {
HANDLE h = OpenProcess(PROCESS_SUSPEND_RESUME, FALSE, pid);
if (h) {
HMODULE ntdll = GetModuleHandleW(L"ntdll.dll");
if (ntdll) {
auto fn = (NtSuspendProcess)GetProcAddress(ntdll, "NtSuspendProcess");
if (fn) fn(h);
}
CloseHandle(h);
}
}
static void ResumeProc(DWORD pid) {
HANDLE h = OpenProcess(PROCESS_SUSPEND_RESUME, FALSE, pid);
if (h) {
HMODULE ntdll = GetModuleHandleW(L"ntdll.dll");
if (ntdll) {
auto fn = (NtResumeProcess)GetProcAddress(ntdll, "NtResumeProcess");
if (fn) { fn(h); fn(h); }
}
CloseHandle(h);
}
}
static DWORD GetDnscachePID() {
DWORD pid = 0;
SC_HANDLE scm = OpenSCManagerW(NULL, NULL, SC_MANAGER_CONNECT);
if (scm) {
SC_HANDLE svc = OpenServiceW(scm, L"Dnscache", SERVICE_QUERY_STATUS);
if (svc) {
SERVICE_STATUS_PROCESS ssp;
DWORD needed;
if (QueryServiceStatusEx(svc, SC_STATUS_PROCESS_INFO, (LPBYTE)&ssp, sizeof(ssp), &needed))
pid = ssp.dwProcessId;
CloseServiceHandle(svc);
}
CloseServiceHandle(scm);
}
return pid;
}
static void ApplyFirewallRules() {
const char* cmds[] = {
"netsh int ip delete destinationcache",
"netsh interface ip delete arpcache",
"netsh winhttp reset proxy",
"netsh int ip reset",
"netsh int ip reset all",
"netsh interface ipv6 reset",
"netsh interface ipv4 reset",
"netsh winsock reset",
"netsh winsock reset catalog",
"ipconfig /flushdns",
"arp -d *",
"reg add \"HKLM\\Software\\Microsoft\\Windows NT\\CurrentVersion\\AppCompatFlags\\Layers\" /v \"C:\\Program Files\\Riot Vanguard\\vgc.exe\" /t REG_SZ /d \"~ RUNASADMIN\" /f"
};
for (int i = 0; i < sizeof(cmds) / sizeof(cmds[0]); i++) {
std::string cmd = std::string("cmd.exe /c ") + cmds[i];
STARTUPINFOA si = { sizeof(si) };
PROCESS_INFORMATION pi;
if (CreateProcessA(NULL, (LPSTR)cmd.c_str(), NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &si, &pi)) {
WaitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
}
}
static void RemoveFirewallRules() {
std::string cmd = "cmd.exe /c netsh";
STARTUPINFOA si = { sizeof(si) };
PROCESS_INFORMATION pi;
if (CreateProcessA(NULL, (LPSTR)cmd.c_str(), NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &si, &pi)) {
WaitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
}
static void AdjustDebugPrivilege() {
HANDLE hToken;
TOKEN_PRIVILEGES tp;
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) return;
LUID luid;
if (!LookupPrivilegeValueW(nullptr, L"SeDebugPrivilege", &luid)) { CloseHandle(hToken); return; }
tp.PrivilegeCount = 1;
tp.Privileges[0].Luid = luid;
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hToken, FALSE, &tp, 0, nullptr, nullptr);
CloseHandle(hToken);
}
SERVICE_STATUS g_SvcStatus = {};
SERVICE_STATUS_HANDLE g_SvcHandle = NULL;
HANDLE g_StopEvent = INVALID_HANDLE_VALUE;
std::atomic_bool g_shutdown(false);
std::atomic_bool g_clientGone(false);
std::mutex g_logMtx;
std::wofstream g_logFile;
static void l0g(const std::wstring& msg) {
std::lock_guard<std::mutex> lock(g_logMtx);
if (!g_logFile.is_open()) {
wchar_t exePath[MAX_PATH];
if (GetModuleFileNameW(NULL, exePath, MAX_PATH)) {
std::wstring path(exePath);
size_t pos = path.find_last_of(L"\\/");
if (pos != std::wstring::npos) {
path = path.substr(0, pos + 1) + L"vgcFaker.l0g";
DeleteFileW(path.c_str());
g_logFile.open(path, std::ios::out | std::ios::app);
}
}
}
if (g_logFile.is_open()) {
g_logFile << msg << L"\n";
g_logFile.flush();
}
}
struct unique_handle {
HANDLE h{ INVALID_HANDLE_VALUE };
unique_handle() = default;
explicit unique_handle(HANDLE handle) : h(handle) {}
~unique_handle() { reset(); }
unique_handle(const unique_handle&) = delete;
unique_handle& operator=(const unique_handle&) = delete;
unique_handle(unique_handle&& o) noexcept : h(o.h) { o.h = INVALID_HANDLE_VALUE; }
unique_handle& operator=(unique_handle&& o) noexcept {
if (this != &o) { reset(); h = o.h; o.h = INVALID_HANDLE_VALUE; }
return *this;
}
void reset(HANDLE nh = INVALID_HANDLE_VALUE) {
if (h != INVALID_HANDLE_VALUE && h != NULL) CloseHandle(h);
h = nh;
}
HANDLE get() const { return h; }
explicit operator bool() const { return h != INVALID_HANDLE_VALUE && h != NULL; }
};
void ServiceMain(DWORD argc, LPWSTR* argv);
void WINAPI ServiceCtrlHandler(DWORD);
void PipeThread();
static int WINAPI wmain(int, wchar_t**) {
SERVICE_TABLE_ENTRY tbl[] = {
{ const_cast<LPWSTR>(SERVICE_NAME), (LPSERVICE_MAIN_FUNCTION)ServiceMain },
{ NULL, NULL }
};
if (!StartServiceCtrlDispatcher(tbl)) return 1;
return 0;
}
void ServiceMain(DWORD, LPWSTR*) {
g_SvcHandle = RegisterServiceCtrlHandler(SERVICE_NAME, ServiceCtrlHandler);
if (!g_SvcHandle) return;
g_SvcStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
g_SvcStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
g_SvcStatus.dwCurrentState = SERVICE_START_PENDING;
g_SvcStatus.dwWin32ExitCode = NO_ERROR;
SetServiceStatus(g_SvcHandle, &g_SvcStatus);
g_StopEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (!g_StopEvent) {
g_SvcStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus(g_SvcHandle, &g_SvcStatus);
return;
}
g_SvcStatus.dwCurrentState = SERVICE_RUNNING;
SetServiceStatus(g_SvcHandle, &g_SvcStatus);
AdjustDebugPrivilege();
g_clientGone = false;
g_shutdown = false;
std::thread pt(PipeThread);
while (true) {
if (WaitForSingleObject(g_StopEvent, 100) == WAIT_OBJECT_0) { g_shutdown = true; break; }
if (g_clientGone.load()) { g_shutdown = true; break; }
}
if (pt.joinable()) pt.join();
CloseHandle(g_StopEvent);
g_StopEvent = NULL;
if (g_logFile.is_open()) g_logFile.close();
g_SvcStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus(g_SvcHandle, &g_SvcStatus);
}
void WINAPI ServiceCtrlHandler(DWORD ctrl) {
if (ctrl == SERVICE_CONTROL_STOP && g_SvcStatus.dwCurrentState == SERVICE_RUNNING) {
g_SvcStatus.dwControlsAccepted = 0;
g_SvcStatus.dwCurrentState = SERVICE_STOP_PENDING;
SetServiceStatus(g_SvcHandle, &g_SvcStatus);
g_shutdown = true;
if (g_StopEvent) SetEvent(g_StopEvent);
}
}
static bool ReadPipeAsync(HANDLE pipe, void* buf, DWORD cap, DWORD& out) {
out = 0;
OVERLAPPED ol{};
unique_handle evt(CreateEvent(nullptr, TRUE, FALSE, nullptr));
if (!evt) return false;
ol.hEvent = evt.get();
BOOL ok = ReadFile(pipe, buf, cap, &out, &ol);
if (ok) return true;
DWORD err = GetLastError();
if (err != ERROR_IO_PENDING) { SetLastError(err); return false; }
HANDLE w[2] = { ol.hEvent, g_StopEvent };
DWORD wr = WaitForMultipleObjects(2, w, FALSE, INFINITE);
if (wr == WAIT_OBJECT_0) {
if (!GetOverlappedResult(pipe, &ol, &out, FALSE)) return false;
return true;
}
CancelIoEx(pipe, &ol);
SetLastError(ERROR_OPERATION_ABORTED);
return false;
}
void PipeThread() {
const std::wstring pipeName = L"\\\\.\\pipe\\933823D3-C77B-4BAE-89D7-A92B567236BC";
while (!g_shutdown.load()) {
SECURITY_ATTRIBUTES sa{};
PSECURITY_DESCRIPTOR pSD = nullptr;
if (!ConvertStringSecurityDescriptorToSecurityDescriptorW(
L"D:(A;OICI;GRGW;;;WD)", SDDL_REVISION_1, &pSD, nullptr)) {
Sleep(1000); continue;
}
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor = pSD;
sa.bInheritHandle = FALSE;
unique_handle pipe(CreateNamedPipeW(
pipeName.c_str(),
PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT,
PIPE_UNLIMITED_INSTANCES,
65536, 65536, 0, &sa));
LocalFree(pSD);
if (!pipe || pipe.get() == INVALID_HANDLE_VALUE) { Sleep(1000); continue; }
l0g(L"Waiting for client connection...");
OVERLAPPED connOl{};
unique_handle connEvt(CreateEvent(nullptr, TRUE, FALSE, nullptr));
if (!connEvt) { Sleep(1000); continue; }
connOl.hEvent = connEvt.get();
BOOL connected = ConnectNamedPipe(pipe.get(), &connOl);
if (!connected) {
DWORD err = GetLastError();
if (err == ERROR_IO_PENDING) {
HANDLE wh[2] = { connOl.hEvent, g_StopEvent };
DWORD wr = WaitForMultipleObjects(2, wh, FALSE, INFINITE);
if (wr == WAIT_OBJECT_0) connected = TRUE;
else { CancelIoEx(pipe.get(), &connOl); return; }
}
else if (err == ERROR_PIPE_CONNECTED) {
connected = TRUE;
}
}
if (!connected) continue;
l0g(L"Client connected.");
std::vector<unsigned char> buf(65536);
DWORD nRead = 0;
int msgIdx = 0;
while (!g_shutdown.load()) {
BOOL ok = ReadPipeAsync(pipe.get(), buf.data(), (DWORD)buf.size(), nRead);
if (!ok || nRead == 0) {
DWORD err = GetLastError();
if (err == ERROR_MORE_DATA && nRead > 0) {
l0g(L"WARNING: Message truncated, processing partial data.");
}
else {
if (err == ERROR_BROKEN_PIPE || err == ERROR_NO_DATA)
l0g(L"Client disconnected normally.");
g_clientGone = true;
break;
}
}
msgIdx++;
uint32_t mtype = 0;
if (nRead >= 4) mtype = *(uint32_t*)(buf.data());
std::wstringstream hs;
hs << L"Received message #" << msgIdx << L" [" << msg_type_name(mtype) << L"] (" << nRead << L"B): ";
for (DWORD i = 0; i < nRead && i < 64; ++i)
hs << std::hex << std::setw(2) << std::setfill(L'0') << (int)buf[i] << L' ';
if (nRead > 64) hs << L"... (" << std::dec << nRead << L" bytes total)";
l0g(hs.str());
if (msgIdx == 1) {
if (!dns_frozen.load()) {
dns_frozen = true;
std::thread([]() {
std::lock_guard<std::mutex> lock(fw_mtx);
ApplyFirewallRules();
DWORD pid = GetDnscachePID();
if (pid) { SuspendProc(pid); l0g(L"Dnscache frozen."); }
}).detach();
}
DWORD bw = 0;
BOOL w = WriteFile(pipe.get(), buf.data(), nRead, &bw, nullptr);
FlushFileBuffers(pipe.get());
if (w) l0g(L"Sent INIT echo response.");
}
if (msgIdx == 2) {
unsigned char ackBuf[39] = {
0xE9,0x03,0x00,0x00,0x24,0x00,0x00,0x00,
0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x04,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x65,0x56,0x28
};
DWORD bw = 0;
WriteFile(pipe.get(), ackBuf, sizeof(ackBuf), &bw, nullptr);
FlushFileBuffers(pipe.get());
l0g(L"Sent ACK after AUTH.");
}
if (nRead == 40 && buf[0] == 0x03) {
unsigned char resp[40];
memcpy(resp, buf.data(), 40);
resp[0] = 0x04;
DWORD bw = 0;
WriteFile(pipe.get(), resp, 40, &bw, nullptr);
FlushFileBuffers(pipe.get());
l0g(L"Responding with HeartbeatAck.");
}
else if (msgIdx > 2) {
DWORD bw = 0;
WriteFile(pipe.get(), buf.data(), nRead, &bw, nullptr);
FlushFileBuffers(pipe.get());
}
}
if (g_shutdown.load()) {
unsigned char finBuf[36] = {
0x02,0x00,0x00,0x00,0x24,0x00,0x00,0x00,
0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x04,0x00,0x00,0x00,
0x39,0x05,0x00,0x00
};
DWORD bw = 0;
WriteFile(pipe.get(), finBuf, sizeof(finBuf), &bw, nullptr);
FlushFileBuffers(pipe.get());
break;
}
if (g_clientGone.load()) break;
}
if (dns_frozen.load()) {
dns_frozen = false;
std::lock_guard<std::mutex> lock(fw_mtx);
DWORD pid = GetDnscachePID();
if (pid) ResumeProc(pid);
RemoveFirewallRules();
}
l0g(L"Pipe thread ending.");
}