-
Notifications
You must be signed in to change notification settings - Fork 3
/
SelfDebugging.c
80 lines (65 loc) · 2.13 KB
/
SelfDebugging.c
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
// SelfDebugging.c
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <windows.h>
int successfulDebugging = -1;
// Method for parent to spawn and debug child process
void spawnAndDebugChild() {
HANDLE hProcess = NULL;
PROCESS_INFORMATION pi;
STARTUPINFO si;
ZeroMemory(&pi, sizeof(PROCESS_INFORMATION));
ZeroMemory(&si, sizeof(STARTUPINFO));
GetStartupInfo(&si);
char commandArgs[1024];
sprintf(commandArgs,"%s %d",GetCommandLine(), (int) GetCurrentProcessId());
CreateProcess(NULL, commandArgs, NULL, NULL, FALSE,
DEBUG_PROCESS, NULL, NULL, &si, &pi);
}
// Parent program
void executeParentProgram() {
spawnAndDebugChild();
DEBUG_EVENT DbgEvent;
while(1) {
WaitForDebugEvent(&DbgEvent, INFINITE);
if (DbgEvent.dwDebugEventCode == EXIT_PROCESS_DEBUG_EVENT) {
ExitProcess(-1);
} else if (DbgEvent.dwDebugEventCode == OUTPUT_DEBUG_STRING_EVENT) {
printf("this is the malicious payload");
ExitProcess(0);
}
ContinueDebugEvent(DbgEvent.dwProcessId, DbgEvent.dwThreadId, DBG_CONTINUE);
}
}
// Method for child to debug parent
void *debugParent (void *pid_void_ptr) {
DWORD *pid_ptr = (DWORD *)pid_void_ptr;
successfulDebugging = DebugActiveProcess(*pid_ptr);
DEBUG_EVENT DbgEvent;
while(1) {
WaitForDebugEvent(&DbgEvent, INFINITE);
ContinueDebugEvent(DbgEvent.dwProcessId, DbgEvent.dwThreadId, DBG_CONTINUE);
}
}
// Child program
void executeChildProgram(DWORD pid) {
pthread_t debugParentThread;
pthread_create(&debugParentThread, NULL, debugParent, &pid);
// Update debugging status for parent thread
while (successfulDebugging == -1) {
if (successfulDebugging == 0) {
ExitProcess(-1);
} else {
OutputDebugString("success");
}
}
pthread_join(debugParentThread, NULL);
}
void main(int argc, char *argv[]) {
if (argc > 1) {
executeChildProgram((DWORD) atoi(argv[1]));
} else {
executeParentProgram();
}
}