Skip to content

Commit 0fa3399

Browse files
committed
check debugger for parent process too
1 parent d6a3af6 commit 0fa3399

File tree

3 files changed

+46
-24
lines changed

3 files changed

+46
-24
lines changed

src/main.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ DISABLE_OPTIMIZATION FORCE_INLINE uint32_t* get_cksum_data() {
5454

5555
int main(int argc, char* argv[]) {
5656
#ifdef UNTRACEABLE
57-
check_debugger(true);
57+
check_debugger(true, false);
5858
#endif
5959

6060
std::string exe_path = get_exe_path();
@@ -394,7 +394,8 @@ int main(int argc, char* argv[]) {
394394
int rc4_key_len = strlen(rc4_key);
395395
while (script_len > 0) {
396396
#ifdef UNTRACEABLE
397-
check_debugger(false);
397+
check_debugger(false, false);
398+
check_debugger(false, true);
398399
#endif
399400
#ifdef __linux__
400401
check_pipe_reader(fd);

src/untraceable.h

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,28 @@
55
#if defined(__CYGWIN__)
66
#include <Windows.h>
77

8-
FORCE_INLINE void check_debugger(bool full) {
8+
FORCE_INLINE void check_debugger(bool full, bool parent) {
9+
if (parent)
10+
return;
911
if (IsDebuggerPresent()) {
10-
LOGD("debugger present!");
12+
LOGD("debugger present on self process!");
13+
sleep(5);
1114
exit(1);
1215
}
1316
}
1417

1518
#elif defined(__APPLE__)
1619
#include <sys/sysctl.h>
1720

18-
FORCE_INLINE void check_debugger(bool full) {
21+
FORCE_INLINE void check_debugger(bool full, bool parent) {
1922
struct kinfo_proc info;
2023
info.kp_proc.p_flag = 0;
2124
size_t size = sizeof(info);
22-
int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid() };
25+
int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, parent ? getppid() : getpid() };
2326
if (sysctl(mib, 4, &info, &size, nullptr, 0) == 0 &&
2427
(info.kp_proc.p_flag & P_TRACED) != 0) {
25-
LOGD("debugger present!");
28+
LOGD("debugger present on %s process!", parent ? "parent" : "self");
29+
sleep(5);
2630
exit(1);
2731
}
2832
}
@@ -46,9 +50,11 @@ FORCE_INLINE void check_debugger(bool full) {
4650
#endif
4751
#endif
4852

49-
FORCE_INLINE void check_debugger(bool full) {
53+
FORCE_INLINE void check_debugger(bool full, bool parent) {
5054
#ifdef __linux__
51-
std::ifstream ifs(OBF("/proc/self/status"));
55+
char path[128];
56+
snprintf(path, sizeof(path), OBF("/proc/%d/status"), parent ? getppid() : getpid());
57+
std::ifstream ifs(path);
5258
std::string line, needle = OBF("TracerPid:\t");
5359
int tracer_pid = 0;
5460
while (std::getline(ifs, line)) {
@@ -60,7 +66,8 @@ FORCE_INLINE void check_debugger(bool full) {
6066
}
6167
ifs.close();
6268
if (tracer_pid != 0) {
63-
LOGD("found tracer. tracer_pid=%d", tracer_pid);
69+
LOGD("found tracer on %s process. tracer_pid=%d", parent ? "parent" : "self", tracer_pid);
70+
sleep(5);
6471
exit(1);
6572
}
6673
if (!full) {
@@ -75,22 +82,36 @@ FORCE_INLINE void check_debugger(bool full) {
7582
return;
7683
}
7784
#endif
78-
int ppid = getpid();
79-
int p = fork();
80-
if (p < 0) {
81-
LOGE("fork failed");
82-
exit(1);
83-
} else if (p > 0) { // parent process
84-
waitpid(p, 0, 0);
85-
} else {
86-
if (ptrace(PT_ATTACHEXC, ppid, 0, 0) == 0) {
85+
if (parent) {
86+
auto pid = getppid();
87+
if (ptrace(PT_ATTACHEXC, pid, 0, 0) == 0) {
8788
wait(0);
88-
ptrace(PT_DETACH, ppid, 0, 0);
89+
ptrace(PT_DETACH, pid, 0, 0);
90+
} else {
91+
LOGD("parent process being traced!");
92+
sleep(5);
93+
kill(pid, SIGKILL);
94+
exit(1);
95+
}
96+
} else {
97+
auto pid = getpid();
98+
int p = fork();
99+
if (p < 0) {
100+
LOGE("fork failed");
101+
exit(1);
102+
} else if (p > 0) { // parent process
103+
waitpid(p, 0, 0);
89104
} else {
90-
LOGD("being traced!");
91-
kill(ppid, SIGKILL);
105+
if (ptrace(PT_ATTACHEXC, pid, 0, 0) == 0) {
106+
wait(0);
107+
ptrace(PT_DETACH, pid, 0, 0);
108+
} else {
109+
LOGD("self process being traced!");
110+
sleep(5);
111+
kill(pid, SIGKILL);
112+
}
113+
_Exit(0);
92114
}
93-
_Exit(0);
94115
}
95116
}
96117
#endif

src/utils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ FORCE_INLINE void check_pipe_reader(int fd) {
201201
link_dst[size] = '\0';
202202
if (strcmp(link_dst, pipe_dst) == 0) {
203203
LOGD("process %lu is reading our pipe!", pid);
204-
sleep(3);
204+
sleep(5);
205205
exit(1);
206206
}
207207
}

0 commit comments

Comments
 (0)