-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanti_debug.cpp
More file actions
75 lines (63 loc) · 1.53 KB
/
Copy pathanti_debug.cpp
File metadata and controls
75 lines (63 loc) · 1.53 KB
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
#include "shieldcore/anti_debug.hpp"
#include <atomic>
#include <thread>
#include <chrono>
#include <cstdlib>
#if defined(_WIN32)
#include <windows.h>
#else
#include <errno.h>
#include <signal.h>
#include <sys/ptrace.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#endif
namespace shieldcore {
namespace {
std::atomic<bool> debugCheckRunning{false};
std::thread* debugCheckThread = nullptr;
}
bool isDebuggerPresent() {
#if defined(_WIN32)
if (IsDebuggerPresent() != FALSE) {
return true;
}
BOOL remoteDebugger = FALSE;
CheckRemoteDebuggerPresent(GetCurrentProcess(), &remoteDebugger);
if (remoteDebugger != FALSE) {
return true;
}
return false;
#else
errno = 0;
long result = ptrace(PTRACE_TRACEME, 0, nullptr, nullptr);
if (result == -1 && errno == EPERM) {
return true;
}
return false;
#endif
}
bool checkDebuggerPeriodically(int intervalSeconds) {
if (debugCheckRunning.exchange(true)) {
return false;
}
debugCheckThread = new std::thread([intervalSeconds]() {
while (debugCheckRunning.load()) {
std::this_thread::sleep_for(std::chrono::seconds(intervalSeconds));
if (isDebuggerPresent()) {
exit(1);
}
}
});
return true;
}
void stopPeriodicDebugCheck() {
debugCheckRunning.store(false);
if (debugCheckThread && debugCheckThread->joinable()) {
debugCheckThread->join();
delete debugCheckThread;
debugCheckThread = nullptr;
}
}
} // namespace shieldcore