forked from cuckoosandbox/cuckoomon
-
Notifications
You must be signed in to change notification settings - Fork 3
/
hook_thread.c
148 lines (125 loc) · 4.21 KB
/
hook_thread.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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
Cuckoo Sandbox - Automated Malware Analysis
Copyright (C) 2010-2012 Cuckoo Sandbox Developers
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <windows.h>
#include "hooking.h"
#include "ntapi.h"
#include "log.h"
#include "pipe.h"
#include "misc.h"
static IS_SUCCESS_HANDLE();
static const char *module_name = "threading";
static void notify_pipe(DWORD process_id)
{
char buf[32] = {}; int len = sizeof(buf);
pipe_write_read(buf, &len, "PID:%d", process_id);
}
HOOKDEF(HANDLE, WINAPI, OpenThread,
__in DWORD dwDesiredAccess,
__in BOOL bInheritHandle,
__in DWORD dwThreadId
) {
HANDLE ret = Old_OpenThread(dwDesiredAccess, bInheritHandle, dwThreadId);
LOQ("ll", "DesiredAccess", dwDesiredAccess, "ThreadId", dwThreadId);
return ret;
}
HOOKDEF(HANDLE, WINAPI, CreateThread,
__in LPSECURITY_ATTRIBUTES lpThreadAttributes,
__in SIZE_T dwStackSize,
__in LPTHREAD_START_ROUTINE lpStartAddress,
__in LPVOID lpParameter,
__in DWORD dwCreationFlags,
__out LPDWORD lpThreadId
) {
HANDLE ret = Old_CreateThread(lpThreadAttributes, dwStackSize,
lpStartAddress, lpParameter, dwCreationFlags, lpThreadId);
LOQ("pplL", "StartRoutine", lpStartAddress, "Parameter", lpParameter,
"CreationFlags", dwCreationFlags, "ThreadId", lpThreadId);
return ret;
}
HOOKDEF(HANDLE, WINAPI, CreateRemoteThread,
__in HANDLE hProcess,
__in LPSECURITY_ATTRIBUTES lpThreadAttributes,
__in SIZE_T dwStackSize,
__in LPTHREAD_START_ROUTINE lpStartAddress,
__in LPVOID lpParameter,
__in DWORD dwCreationFlags,
__out LPDWORD lpThreadId
) {
notify_pipe(GetPidFromProcessHandle(hProcess));
HANDLE ret = Old_CreateRemoteThread(hProcess, lpThreadAttributes,
dwStackSize, lpStartAddress, lpParameter, dwCreationFlags,
lpThreadId);
LOQ("3plL", "ProcessHandle", hProcess, "StartRoutine", lpStartAddress,
"Parameter", lpParameter, "CreationFlags", dwCreationFlags,
"ThreadId", lpThreadId);
return ret;
}
HOOKDEF(BOOL, WINAPI, TerminateThread,
__inout HANDLE hThread,
__in DWORD dwExitCode
) {
IS_SUCCESS_BOOL();
BOOL ret = Old_TerminateThread(hThread, dwExitCode);
LOQ("pl", "ThreadHandle", hThread, "ExitCode", dwExitCode);
return ret;
}
HOOKDEF(VOID, WINAPI, ExitThread,
__in DWORD dwExitCode
) {
IS_SUCCESS_VOID();
int ret = 0;
LOQ("l", "ExitCode", dwExitCode);
Old_ExitThread(dwExitCode);
}
HOOKDEF(NTSTATUS, WINAPI, NtGetContextThread,
__in HANDLE ThreadHandle,
__inout LPCONTEXT Context
) {
IS_SUCCESS_NTSTATUS();
NTSTATUS ret = Old_NtGetContextThread(ThreadHandle, Context);
LOQ("p", "ThreadHandle", ThreadHandle);
return ret;
}
HOOKDEF(NTSTATUS, WINAPI, NtSetContextThread,
__in HANDLE ThreadHandle,
__in const CONTEXT *Context
) {
IS_SUCCESS_NTSTATUS();
NTSTATUS ret = Old_NtSetContextThread(ThreadHandle, Context);
LOQ("p", "ThreadHandle", ThreadHandle);
return ret;
}
HOOKDEF(NTSTATUS, WINAPI, NtSuspendThread,
__in HANDLE ThreadHandle,
__out_opt ULONG *PreviousSuspendCount
) {
IS_SUCCESS_NTSTATUS();
ENSURE_ULONG(PreviousSuspendCount);
NTSTATUS ret = Old_NtSuspendThread(ThreadHandle, PreviousSuspendCount);
LOQ("pL", "ThreadHandle", ThreadHandle,
"SuspendCount", PreviousSuspendCount);
return ret;
}
HOOKDEF(NTSTATUS, WINAPI, NtResumeThread,
__in HANDLE ThreadHandle,
__out_opt ULONG *SuspendCount
) {
IS_SUCCESS_NTSTATUS();
ENSURE_ULONG(SuspendCount);
NTSTATUS ret = Old_NtResumeThread(ThreadHandle, SuspendCount);
LOQ("pL", "ThreadHandle", ThreadHandle, "SuspendCount", SuspendCount);
return ret;
}