init
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
#include "server_pipe.h"
|
||||
#include "Driver.h"
|
||||
#include "check_vmware.h"
|
||||
|
||||
struct ClientInfo
|
||||
{
|
||||
@@ -23,7 +24,7 @@ static constexpr DWORD kFirstClientTimeoutMs = 3000;
|
||||
|
||||
static void ExitServerProcess(UINT exit_code)
|
||||
{
|
||||
printf("[server] ExitProcess code=%u\n", exit_code);
|
||||
//printf("[server] ExitProcess code=%u\n", exit_code);
|
||||
fflush(stdout);
|
||||
ExitProcess(exit_code);
|
||||
}
|
||||
@@ -82,7 +83,7 @@ static void RemoveClient(DWORD id)
|
||||
|
||||
if (InterlockedCompareExchange(&g_seen_client, 0, 0) != 0 && g_clients.empty())
|
||||
{
|
||||
printf("[server] all clients exited, server shutdown\n");
|
||||
//printf("[server] all clients exited, server shutdown\n");
|
||||
fflush(stdout);
|
||||
SetEvent(g_exit_event);
|
||||
}
|
||||
@@ -109,7 +110,7 @@ static void BroadcastExit(const char* reason)
|
||||
msg.type = static_cast<uint32_t>(MsgType::Exit);
|
||||
msg.session_id = static_cast<uint64_t>(g_session_id);
|
||||
|
||||
printf("[server] broadcast exit: %s\n", reason ? reason : "unknown");
|
||||
//printf("[server] broadcast exit: %s\n", reason ? reason : "unknown");
|
||||
fflush(stdout);
|
||||
|
||||
for (auto& client : clients)
|
||||
@@ -181,29 +182,41 @@ static uint32_t HandlePrintf(const std::shared_ptr<ClientInfo>& client, const Ms
|
||||
return ERROR_INVALID_PARAMETER;
|
||||
|
||||
const auto* in = reinterpret_cast<const PrintfRequest*>(req.payload);
|
||||
printf("[server][printf][client=%lu pid=%lu] %s\n", client->id, client->pid, in->text);
|
||||
//printf("[server][printf][client=%lu pid=%lu] %s\n", client->id, client->pid, in->text);
|
||||
fflush(stdout);
|
||||
return ERROR_SUCCESS;
|
||||
}
|
||||
|
||||
static uint32_t HandleLoadDriver(const Msg& req)
|
||||
|
||||
static uint32_t HandleLoadDriver(Msg& req)
|
||||
{
|
||||
return Driver::fn_get_instance()->driver_install() ? ERROR_SUCCESS : ERROR_INVALID_PARAMETER;
|
||||
if (req.input_size < sizeof(LoadDriverRequest))
|
||||
return ERROR_INVALID_PARAMETER;
|
||||
|
||||
auto* in = reinterpret_cast<LoadDriverRequest*>(req.payload);
|
||||
|
||||
DWORD status = ERROR_SUCCESS;
|
||||
|
||||
auto result =
|
||||
Driver::fn_get_instance()->driver_install(&status);
|
||||
|
||||
in->status = status;
|
||||
|
||||
return result ? ERROR_SUCCESS : ERROR_INVALID_PARAMETER;
|
||||
}
|
||||
|
||||
static uint32_t HandleUnloadDriver(const Msg& req)
|
||||
{
|
||||
if (req.input_size < sizeof(UnloadDriverRequest))
|
||||
return ERROR_INVALID_PARAMETER;
|
||||
|
||||
|
||||
const auto* in = reinterpret_cast<const UnloadDriverRequest*>(req.payload);
|
||||
if (!in->service_name[0])
|
||||
return ERROR_INVALID_PARAMETER;
|
||||
|
||||
|
||||
SC_HANDLE scm = OpenSCManagerW(nullptr, nullptr, SC_MANAGER_CONNECT);
|
||||
if (!scm)
|
||||
return GetLastError();
|
||||
|
||||
|
||||
SC_HANDLE service = OpenServiceW(scm, in->service_name, SERVICE_STOP | DELETE | SERVICE_QUERY_STATUS);
|
||||
if (!service)
|
||||
{
|
||||
@@ -211,10 +224,10 @@ static uint32_t HandleUnloadDriver(const Msg& req)
|
||||
CloseServiceHandle(scm);
|
||||
return err;
|
||||
}
|
||||
|
||||
|
||||
SERVICE_STATUS status{};
|
||||
ControlService(service, SERVICE_CONTROL_STOP, &status);
|
||||
|
||||
|
||||
DWORD err = ERROR_SUCCESS;
|
||||
if (!DeleteService(service))
|
||||
{
|
||||
@@ -222,7 +235,7 @@ static uint32_t HandleUnloadDriver(const Msg& req)
|
||||
if (err == ERROR_SERVICE_MARKED_FOR_DELETE)
|
||||
err = ERROR_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
CloseServiceHandle(service);
|
||||
CloseServiceHandle(scm);
|
||||
return err;
|
||||
@@ -234,17 +247,45 @@ static uint32_t HandleBreakpointRemoved(const std::shared_ptr<ClientInfo>& clien
|
||||
return ERROR_INVALID_PARAMETER;
|
||||
|
||||
const auto* in = reinterpret_cast<const BreakpointRemovedRequest*>(req.payload);
|
||||
printf("[server][breakpoint-removed][client=%lu pid=%lu] tid=%u dr0=0x%llx dr7=0x%llx\n",
|
||||
client->id,
|
||||
client->pid,
|
||||
in->thread_id,
|
||||
static_cast<unsigned long long>(in->dr0),
|
||||
static_cast<unsigned long long>(in->dr7));
|
||||
//printf("[server][breakpoint-removed][client=%lu pid=%lu] tid=%u dr0=0x%llx dr7=0x%llx\n",
|
||||
// client->id,
|
||||
// client->pid,
|
||||
// in->thread_id,
|
||||
// static_cast<unsigned long long>(in->dr0),
|
||||
// static_cast<unsigned long long>(in->dr7));
|
||||
fflush(stdout);
|
||||
return ERROR_SUCCESS;
|
||||
}
|
||||
|
||||
static bool DispatchCommand(HANDLE pipe, const std::shared_ptr<ClientInfo>& client, const Msg& req)
|
||||
uint32_t HandleCheckVirtualSystem()
|
||||
{
|
||||
|
||||
BOOL bIsSuc[9]{ 0 };
|
||||
|
||||
bIsSuc[0] = check_vmware::cpuid();
|
||||
bIsSuc[1] = check_vmware::cpuid2();
|
||||
bIsSuc[2] = check_vmware::diskname();
|
||||
bIsSuc[3] = check_vmware::dxgiGpuName();
|
||||
bIsSuc[4] = check_vmware::file();
|
||||
bIsSuc[5] = check_vmware::In();
|
||||
bIsSuc[6] = check_vmware::mousename();
|
||||
bIsSuc[7] = check_vmware::registry();
|
||||
bIsSuc[8] = check_vmware::process();
|
||||
|
||||
for (size_t i = 0; i < 9; i++)
|
||||
{
|
||||
if (bIsSuc[i] == TRUE)
|
||||
return ERROR_SUCCESS;
|
||||
}
|
||||
|
||||
return ERROR_INVALID_FUNCTION;
|
||||
}
|
||||
uint32_t HandleCheckVmxSetting()
|
||||
{
|
||||
return check_vmware::virutal_check_vmcall() ? ERROR_SUCCESS : ERROR_INVALID_FUNCTION;
|
||||
}
|
||||
|
||||
static bool DispatchCommand(HANDLE pipe, const std::shared_ptr<ClientInfo>& client, Msg& req)
|
||||
{
|
||||
if (!client)
|
||||
return SendCommandStatus(pipe, req, ERROR_INVALID_PARAMETER);
|
||||
@@ -261,11 +302,12 @@ static bool DispatchCommand(HANDLE pipe, const std::shared_ptr<ClientInfo>& clie
|
||||
break;
|
||||
|
||||
case CommandId::LoadDriver:
|
||||
|
||||
status = HandleLoadDriver(req);
|
||||
break;
|
||||
return SendCommandReply(pipe, req, status, req.payload, sizeof(LoadDriverRequest));
|
||||
|
||||
case CommandId::UnloadDriver:
|
||||
status = HandleUnloadDriver(req);
|
||||
//status = HandleUnloadDriver(req);
|
||||
break;
|
||||
|
||||
case CommandId::QueryStatus:
|
||||
@@ -274,9 +316,15 @@ static bool DispatchCommand(HANDLE pipe, const std::shared_ptr<ClientInfo>& clie
|
||||
case CommandId::BreakpointRemoved:
|
||||
status = HandleBreakpointRemoved(client, req);
|
||||
break;
|
||||
case CommandId::CheckVMware:
|
||||
status = HandleCheckVirtualSystem();
|
||||
break;
|
||||
case CommandId::CheckVmx:
|
||||
status = HandleCheckVmxSetting();
|
||||
break;
|
||||
default:
|
||||
printf("[server] unknown command=%u from client=%lu pid=%lu\n",
|
||||
req.command, client->id, client->pid);
|
||||
/* printf("[server] unknown command=%u from client=%lu pid=%lu\n",
|
||||
req.command, client->id, client->pid);*/
|
||||
fflush(stdout);
|
||||
status = ERROR_INVALID_FUNCTION;
|
||||
break;
|
||||
@@ -291,7 +339,7 @@ static DWORD WINAPI FirstClientTimeoutProc(LPVOID)
|
||||
if (wait == WAIT_TIMEOUT &&
|
||||
InterlockedCompareExchange(&g_seen_client, 0, 0) == 0)
|
||||
{
|
||||
printf("[server] no client connected in %lu ms, server shutdown\n", kFirstClientTimeoutMs);
|
||||
//printf("[server] no client connected in %lu ms, server shutdown\n", kFirstClientTimeoutMs);
|
||||
fflush(stdout);
|
||||
RequestShutdown("first client timeout");
|
||||
}
|
||||
@@ -317,7 +365,7 @@ static DWORD WINAPI MonitorThreadProc(LPVOID)
|
||||
const LONG64 last = InterlockedCompareExchange64(&client->last_tick, 0, 0);
|
||||
if (last != 0 && now - last > kHeartbeatTimeoutMs)
|
||||
{
|
||||
printf("[server] timeout pid=%lu id=%lu\n", client->pid, client->id);
|
||||
//printf("[server] timeout pid=%lu id=%lu\n", client->pid, client->id);
|
||||
fflush(stdout);
|
||||
InterlockedExchange64(&client->last_tick, now);
|
||||
}
|
||||
@@ -338,8 +386,8 @@ static DWORD WINAPI ClientSessionProc(LPVOID param)
|
||||
}
|
||||
|
||||
auto client = RegisterClient(pipe, hello);
|
||||
printf("[server] client connected id=%lu pid=%lu session=%llu\n",
|
||||
client->id, client->pid, static_cast<unsigned long long>(hello.session_id));
|
||||
/*printf("[server] client connected id=%lu pid=%lu session=%llu\n",
|
||||
client->id, client->pid, static_cast<unsigned long long>(hello.session_id));*/
|
||||
fflush(stdout);
|
||||
|
||||
Msg ack{};
|
||||
@@ -364,7 +412,7 @@ static DWORD WINAPI ClientSessionProc(LPVOID param)
|
||||
Msg msg{};
|
||||
if (!ReadMessage(pipe, msg))
|
||||
{
|
||||
printf("[server] client disconnected id=%lu pid=%lu\n", client->id, client->pid);
|
||||
//printf("[server] client disconnected id=%lu pid=%lu\n", client->id, client->pid);
|
||||
fflush(stdout);
|
||||
RemoveClient(client->id);
|
||||
CloseHandle(pipe);
|
||||
@@ -373,11 +421,11 @@ static DWORD WINAPI ClientSessionProc(LPVOID param)
|
||||
|
||||
if (msg.session_id != static_cast<uint64_t>(g_session_id))
|
||||
{
|
||||
printf("[server] client session mismatch id=%lu pid=%lu msg_session=%llu server_session=%llu\n",
|
||||
/*printf("[server] client session mismatch id=%lu pid=%lu msg_session=%llu server_session=%llu\n",
|
||||
client->id,
|
||||
client->pid,
|
||||
static_cast<unsigned long long>(msg.session_id),
|
||||
static_cast<unsigned long long>(g_session_id));
|
||||
static_cast<unsigned long long>(g_session_id));*/
|
||||
fflush(stdout);
|
||||
RemoveClient(client->id);
|
||||
CloseHandle(pipe);
|
||||
@@ -391,7 +439,7 @@ static DWORD WINAPI ClientSessionProc(LPVOID param)
|
||||
}
|
||||
else if (msg.type == static_cast<uint32_t>(MsgType::Goodbye))
|
||||
{
|
||||
printf("[server] client goodbye id=%lu pid=%lu\n", client->id, client->pid);
|
||||
//printf("[server] client goodbye id=%lu pid=%lu\n", client->id, client->pid);
|
||||
fflush(stdout);
|
||||
RemoveClient(client->id);
|
||||
CloseHandle(pipe);
|
||||
@@ -399,7 +447,7 @@ static DWORD WINAPI ClientSessionProc(LPVOID param)
|
||||
}
|
||||
else if (msg.type == static_cast<uint32_t>(MsgType::Exit))
|
||||
{
|
||||
printf("[server] client exit id=%lu pid=%lu, remove only\n", client->id, client->pid);
|
||||
//printf("[server] client exit id=%lu pid=%lu, remove only\n", client->id, client->pid);
|
||||
fflush(stdout);
|
||||
RemoveClient(client->id);
|
||||
CloseHandle(pipe);
|
||||
@@ -409,8 +457,8 @@ static DWORD WINAPI ClientSessionProc(LPVOID param)
|
||||
{
|
||||
if (!DispatchCommand(pipe, client, msg))
|
||||
{
|
||||
printf("[server] command reply failed id=%lu pid=%lu err=%lu\n",
|
||||
client->id, client->pid, GetLastError());
|
||||
/* printf("[server] command reply failed id=%lu pid=%lu err=%lu\n",
|
||||
client->id, client->pid, GetLastError());*/
|
||||
fflush(stdout);
|
||||
RemoveClient(client->id);
|
||||
CloseHandle(pipe);
|
||||
@@ -441,7 +489,7 @@ static DWORD WINAPI AcceptThreadProc(LPVOID)
|
||||
|
||||
if (pipe == INVALID_HANDLE_VALUE)
|
||||
{
|
||||
printf("[server] CreateNamedPipeW failed: %lu\n", GetLastError());
|
||||
//printf("[server] CreateNamedPipeW failed: %lu\n", GetLastError());
|
||||
Sleep(1000);
|
||||
continue;
|
||||
}
|
||||
@@ -462,7 +510,7 @@ static DWORD WINAPI AcceptThreadProc(LPVOID)
|
||||
HANDLE session_thread = CreateThread(nullptr, 0, ClientSessionProc, pipe, 0, nullptr);
|
||||
if (!session_thread)
|
||||
{
|
||||
printf("[server] ClientSession thread failed: %lu\n", GetLastError());
|
||||
//printf("[server] ClientSession thread failed: %lu\n", GetLastError());
|
||||
CloseHandle(pipe);
|
||||
continue;
|
||||
}
|
||||
@@ -492,16 +540,16 @@ static bool InitializeServer()
|
||||
g_exit_event = CreateEventW(nullptr, TRUE, FALSE, nullptr);
|
||||
if (!g_exit_event)
|
||||
{
|
||||
printf("[server] CreateEventW failed: %lu\n", GetLastError());
|
||||
//printf("[server] CreateEventW failed: %lu\n", GetLastError());
|
||||
return false;
|
||||
}
|
||||
|
||||
g_session_id = (static_cast<LONG64>(GetCurrentProcessId()) << 32) ^ NowMs();
|
||||
|
||||
printf("[server] started protocol=%u session=%llu pipe=%ws\n",
|
||||
/* printf("[server] started protocol=%u session=%llu pipe=%ws\n",
|
||||
kProtocolVersion,
|
||||
static_cast<unsigned long long>(g_session_id),
|
||||
kPipeName);
|
||||
kPipeName);*/
|
||||
fflush(stdout);
|
||||
|
||||
return true;
|
||||
@@ -512,21 +560,21 @@ auto RunServerCore() -> BOOL
|
||||
HANDLE first_client_timeout_thread = CreateThread(nullptr, 0, FirstClientTimeoutProc, nullptr, 0, nullptr);
|
||||
if (!first_client_timeout_thread)
|
||||
{
|
||||
printf("[server] first client timeout thread failed: %lu\n", GetLastError());
|
||||
//printf("[server] first client timeout thread failed: %lu\n", GetLastError());
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
HANDLE monitor_thread = CreateThread(nullptr, 0, MonitorThreadProc, nullptr, 0, nullptr);
|
||||
if (!monitor_thread)
|
||||
{
|
||||
printf("[server] monitor thread failed: %lu\n", GetLastError());
|
||||
//printf("[server] monitor thread failed: %lu\n", GetLastError());
|
||||
return 1;
|
||||
}
|
||||
|
||||
HANDLE accept_thread = CreateThread(nullptr, 0, AcceptThreadProc, nullptr, 0, nullptr);
|
||||
if (!accept_thread)
|
||||
{
|
||||
printf("[server] accept thread failed: %lu\n", GetLastError());
|
||||
//printf("[server] accept thread failed: %lu\n", GetLastError());
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -549,6 +597,7 @@ auto StartServer() -> BOOL
|
||||
{
|
||||
if (!InitializeServer())
|
||||
return FALSE;
|
||||
|
||||
return RunServerCore();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user