-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtask_dispatch.cpp
122 lines (96 loc) · 3.26 KB
/
task_dispatch.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include "pch.hpp"
#include "config.hpp"
#include "constants.hpp"
#include "heartbeat.hpp"
#include "cpu_counter.hpp"
#include "detection.hpp"
#include "task_dispatch.hpp"
#include "hardware_id.hpp"
#include "identification.hpp"
#include "utils.hpp"
namespace thread = std::this_thread;
using namespace std::chrono_literals;
using constants::DBG;
/**
* \brief Wrapper around heartbeat call
* \param ctx Application context
* \param heartbeat_info Heartbeat information to be sent to the remote server
*/
void HeartbeatWrapper(const Context& ctx, const HeartbeatInfo& heartbeat_info) {
// Override check so that testing can be done without server connection
const auto connection = Heartbeat(ctx, heartbeat_info);
if constexpr (DBG)
return;
if (not connection)
ExitFailure(cfg::ExitCode::NoHeartbeat);
}
/**
* \brief Main loop of anti-cheat module which sends heartbeats and decides which tasks to run
* \param ctx Application context
*/
void TaskDispatch(const Context& ctx) {
// todo: network
const std::vector blacklist{
std::wstring{ L"cheatengine" },
std::wstring{ L"xenos" },
std::wstring{ L"injector" },
std::wstring{ L"destroject" }
};
// Get hardware id
HardwareID hwid;
Log("\nHWID raw: " + hwid.GetRawHWID());
Log("HWID hash: " + hwid.GetHash());
HeartbeatInfo heartbeat_info{
.hwid = hwid.GetHash(),
.username = "Placeholder"
};
const CpuCounter cpu_usage{};
constexpr auto sleep_delay = 1s;
// Main event loop of module
for (auto seconds = 0s; ; ++seconds) {
// Avoid exhausting CPU
if constexpr (cfg::CpuThrottle) {
for (unsigned skip_count = 0; cpu_usage() > cfg::cpu_usage_threshold; ++skip_count) {
// If CPU is still exhausted after several seconds, continue anyways
if (skip_count > 10)
break;
HeartbeatWrapper(ctx, heartbeat_info);
thread::sleep_for(sleep_delay);
}
}
// ReSharper disable once CppTooWideScope
unsigned risk_factor = 0u;
// Task dispatch system to avoid running multiple expensive tasks at once
switch (seconds.count()) {
case 1: {
if (auto arp_hashes = GetArpMacHashes(); arp_hashes) {
if constexpr (DBG)
for (const auto& hash : arp_hashes.value())
Log(hash);
heartbeat_info.arp_hashes = arp_hashes.value();
}
} break;
case 5: {
if (const auto processes_killed = KillBlacklistedProcesses(blacklist)) {
risk_factor += processes_killed * 10u;
heartbeat_info.risk_factor = risk_factor;
}
} break;
case 10: {
} break;
default: {
// Simple anti-debugger check
if constexpr (not DBG) {
if (IsDebuggerPresent()) {
risk_factor += 50u;
ExitFailure(cfg::ExitCode::DebuggerPresent);
}
}
} break;
}
if (seconds > 15s)
seconds = 0s;
HeartbeatWrapper(ctx, heartbeat_info);
thread::sleep_for(sleep_delay);
}
}