-
Notifications
You must be signed in to change notification settings - Fork 0
/
R3LogObject.h
382 lines (367 loc) · 15.5 KB
/
R3LogObject.h
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#pragma once
#include <io.h>
#include <stdio.h>
#include <windows.h>
#include <sddl.h>
#include <string>
#include <random>
#include <chrono>
#include <atomic>
#include <sstream>
#include <cassert>
#include <functional>
#include <thread>
#include <mutex>
#include "rapidjson\document.h"
#include "rapidjson\writer.h"
#include "PackageWrapper.h"
namespace cchips {
class CSocketObject : public CBsonWrapper
{
public:
using outputtype = enum _outputtype {
output_invalid = -1,
output_pipe = 0,
output_local,
};
using func_callback = std::function<void(const std::unique_ptr<CRapidJsonWrapper>)>;
using func_getdata = std::function<RAPID_DOC_PAIR()>;
CSocketObject() : m_output_type(output_invalid) { ; }
~CSocketObject() = default;
virtual bool Listen(func_callback& callback) = 0;
virtual bool Connect(func_getdata& getdata) = 0;
virtual void StopListen() = 0;
virtual void StopConnect() = 0;
virtual void Activated() = 0;
static const size_t lpc_buffer_kb;
static const DWORD lpc_server_wait_timeout;
static const DWORD lpc_client_wait_timeout;
static const DWORD lpc_connect_wait_timeout;
static const int lpc_connect_try_times;
// for test
virtual void ClearLogsCount() = 0;
virtual int GetTotalLogs() const = 0;
protected:
void SetOutputType(outputtype type) { m_output_type = type; }
private:
outputtype m_output_type;
};
class CLpcPipeObject : public CSocketObject
{
public:
#define lpc_pipe_name "\\\\.\\pipe\\hipshook_IPC_services"
#define lpc_security_sddl "S:(ML;;NW;;;LW)"
CLpcPipeObject() : m_brunning(false), m_listen_thread(nullptr) {
m_sync_event = CreateEvent(NULL, FALSE, FALSE, NULL);
SetOutputType(CSocketObject::output_pipe);
}
~CLpcPipeObject() {
StopLpcService();
}
const std::string& GetPipeName() const { return lpc_pipe_name; }
bool Listen(CSocketObject::func_callback& callback) override {
assert(m_listen_thread == nullptr);
if (m_listen_thread) return false;
m_listen_thread = std::make_unique<std::thread>(std::bind(&CLpcPipeObject::ListenThread, this, callback));
assert(m_listen_thread != nullptr);
if (!m_listen_thread) return false;
return true;
}
void StopListen() override {
m_brunning = false;
SetEvent(m_sync_event);
if (m_listen_thread && m_listen_thread->joinable())
m_listen_thread->join();
m_listen_thread = nullptr;
return;
}
bool Reconnect() {
auto func_open = [&]() {
int count = 0;
while (count < CSocketObject::lpc_connect_try_times)
{
m_connect_pipe = CreateFileA(lpc_pipe_name, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
if (ValidHandle(m_connect_pipe))
break;
if (DWORD error = GetLastError() != ERROR_PIPE_BUSY || !m_brunning)
{
warning("The log pipe handler connect failed, last error %d.", error);
break;
}
if (!WaitNamedPipeA(lpc_pipe_name, lpc_connect_wait_timeout))
{
if (GetLastError() != ERROR_SEM_TIMEOUT)
{
warning("wait name pipe failed, last error %d.", error);
break;
}
warning("Could not open pipe: 0.1 second wait timed out.");
std::this_thread::sleep_for(std::chrono::microseconds(100));
continue;
}
count++;
}
if (ValidHandle(m_connect_pipe))
return true;
return false;
};
if (!func_open()) return false;
m_brunning = true;
return true;
}
bool Connect(CSocketObject::func_getdata& getdata) override {
m_getdata = getdata;
return Reconnect();
}
void StopConnect() override {
m_brunning = false;
if (ValidHandle(m_connect_pipe)) CloseHandle(m_connect_pipe);
return;
}
void Activated() override {
auto func_write = [&](const char* buffer, int len) {
if (!buffer || !len) return false;
std::lock_guard lock(m_hpipes_mutex);
while (len)
{
DWORD byte_writes = 0;
bool bsuccess = WriteFile(m_connect_pipe, buffer, len, &byte_writes, nullptr);
if (!bsuccess)
{
if (GetLastError() == ERROR_INVALID_HANDLE)
{
// It is possible that malware closes our pipe handle. In that
// case we'll get an invalid handle error. Let's just open a new
// pipe handle.
if (!Reconnect())
break;
}
else
{
warning("CRITICAL:send log by pipe failed. error %d", GetLastError());
break;
}
}
buffer += byte_writes; len -= byte_writes;
}
if (len)
return false;
return true;
};
while(m_brunning) {
std::unique_ptr<bson> data = Pack(m_getdata());
if (data)
{
m_logs_total_count++;
func_write(bson_data(&(*data)), bson_size(&(*data)));
}
else
break;
}
}
//for test
void ClearLogsCount() override { m_logs_total_count = 0; }
int GetTotalLogs() const override { return m_logs_total_count; }
private:
HANDLE StartLPCServer(bool bfirst = true) {
HANDLE hPipe = INVALID_HANDLE_VALUE;
SECURITY_ATTRIBUTES sa;
PSECURITY_ATTRIBUTES psa = NULL;
PSECURITY_DESCRIPTOR psd = NULL;
ULONG size;
// reference: http://stackoverflow.com/questions/3282365/opening-a-named-pipe-in-low-integrity-level
// for ensure all of the be injected samples could write log to tracer by named pipe, the server need low integrity level
using ConvertStringSecurityDescriptorToSecurityDescriptorA_Define = NTSTATUS(WINAPI*)(LPCSTR StringSecurityDescriptor, DWORD StringSDRevision, PSECURITY_DESCRIPTOR *SecurityDescriptor, PULONG SecurityDescriptorSize);
ConvertStringSecurityDescriptorToSecurityDescriptorA_Define tosecuritydescriptora_func = reinterpret_cast<ConvertStringSecurityDescriptorToSecurityDescriptorA_Define>(GetProcAddress(GetModuleHandleA("advapi32"), "ConvertStringSecurityDescriptorToSecurityDescriptorA"));
if (tosecuritydescriptora_func) {
memset(&sa, 0, sizeof(SECURITY_ATTRIBUTES));
if (tosecuritydescriptora_func(lpc_security_sddl, SDDL_REVISION_1, &psd, &size))
{
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.bInheritHandle = FALSE;
sa.lpSecurityDescriptor = psd;
psa = &sa;
}
else {
psd = NULL;
}
}
DWORD open_mode = PIPE_ACCESS_INBOUND | FILE_FLAG_FIRST_PIPE_INSTANCE | FILE_FLAG_OVERLAPPED;
if (!bfirst) open_mode = PIPE_ACCESS_INBOUND | FILE_FLAG_OVERLAPPED;
hPipe = CreateNamedPipeA(
lpc_pipe_name,
// This mode gives the server the equivalent of GENERIC_READ access to the pipe. The client must specify GENERIC_WRITE access when connecting to the pipe.
// Overlapped mode is enabled.
open_mode,
PIPE_TYPE_BYTE | PIPE_WAIT,
PIPE_UNLIMITED_INSTANCES,
(DWORD)(CSocketObject::lpc_buffer_kb * 1024),
(DWORD)(CSocketObject::lpc_buffer_kb * 1024),
NMPWAIT_USE_DEFAULT_WAIT,
psa);
if (psd) LocalFree(psd);
if (hPipe == nullptr) hPipe = INVALID_HANDLE_VALUE;
if (hPipe == INVALID_HANDLE_VALUE)
return INVALID_HANDLE_VALUE;
if (!m_brunning)
{
m_brunning = true;
}
return hPipe;
}
void StopLpcService() {
if (m_sync_event)
CloseHandle(m_sync_event);
m_sync_event = nullptr;
return;
}
DWORD WINAPI ReceiveThread(std::shared_ptr<HANDLE> hpipe, func_callback callback) {
bool bsuccess = false;
DWORD bytes_read = 0;
DWORD error;
if (!hpipe) return 0;
std::vector<char> recv_buffer(CSocketObject::lpc_buffer_kb * 1024);
std::string decode_buffer;
size_t decode_len = 0;
HANDLE read_event = CreateEvent(NULL, FALSE, FALSE, NULL);
OVERLAPPED overlapped = {};
overlapped.hEvent = read_event;
HANDLE handle_array[2];
handle_array[0] = overlapped.hEvent;
handle_array[1] = m_sync_event;
while (m_brunning)
{
bsuccess = ReadFile(*hpipe, &recv_buffer[0], (DWORD)recv_buffer.size(), &bytes_read, nullptr);
if (bsuccess || (error = GetLastError()) == ERROR_MORE_DATA)
{
#ifdef CCHIPS_EXTERNAL_USE
r3_debug_log("ReceiveThread: receive a r3 log.");
#endif
decode_buffer += std::string(&recv_buffer[0], bytes_read);
assert(decode_buffer.length());
if (decode_buffer.length() < sizeof(ULONG))
continue;
decode_len = *reinterpret_cast<ULONG*>(&decode_buffer[0]);
while (decode_len <= decode_buffer.length())
{
m_logs_total_count++;
callback(Unpack(decode_buffer));
decode_buffer = decode_buffer.substr(decode_len);
if (decode_buffer.length() < sizeof(ULONG))
break;
decode_len = *reinterpret_cast<ULONG*>(&decode_buffer[0]);
if (decode_len == 0)
{
decode_buffer.clear();
break;
}
}
}
else if (error == ERROR_BROKEN_PIPE)
{
// If we get the broken pipe error then this pipe connection has
// been terminated for one reason or another.So break from the
// loop and make the socket "inactive", that is, another pipe
// connection can in theory pick it up. (This will only happen in
// cases where malware for some reason broke our pipe connection).
{
std::lock_guard lock(m_hpipes_mutex);
if ((*hpipe) != INVALID_HANDLE_VALUE)
{
FlushFileBuffers(*hpipe);
CloseHandle(*hpipe);
}
*hpipe = INVALID_HANDLE_VALUE;
}
break;
}
else
{
warning("The log pipe handler has failed, last error %d.", error);
break;
}
}
{
std::lock_guard lock(m_hpipes_mutex);
if (*hpipe != INVALID_HANDLE_VALUE)
{
FlushFileBuffers(*hpipe);
DisconnectNamedPipe(*hpipe);
CloseHandle(*hpipe);
*hpipe = INVALID_HANDLE_VALUE;
}
}
return 0;
}
VOID WINAPI ListenThread(func_callback callback) {
std::shared_ptr<HANDLE> hpipe =std::make_shared<HANDLE>(StartLPCServer());
assert(m_sync_event);
if (hpipe == nullptr) return;
if (*hpipe == INVALID_HANDLE_VALUE) return;
if (!m_sync_event) return;
OVERLAPPED overlapped;
overlapped.hEvent = m_sync_event;
std::vector<std::pair<std::unique_ptr<std::thread>, std::shared_ptr<HANDLE>>> receive_threads;
while (m_brunning)
{
DWORD error = 0;
if (ConnectNamedPipe(*hpipe, &overlapped) ?
TRUE : ((error = GetLastError()) == ERROR_PIPE_CONNECTED))
{
#ifdef CCHIPS_EXTERNAL_USE
r3_debug_log("ConnectNamedPipe success!");
#endif
std::unique_ptr<std::thread> receive_thread = std::make_unique<std::thread>(&CLpcPipeObject::ReceiveThread, this, hpipe, callback);
receive_threads.push_back(std::pair<std::unique_ptr<std::thread>, std::shared_ptr<HANDLE>>(std::move(receive_thread), hpipe));
hpipe = std::make_shared<HANDLE>(StartLPCServer(false));
}
else if (error == ERROR_IO_PENDING)
{
while (m_brunning) {
DWORD ret = WaitForSingleObject(overlapped.hEvent, CSocketObject::lpc_server_wait_timeout);
if (ret == WAIT_OBJECT_0) {
break;
}
else if (ret == WAIT_TIMEOUT) {
continue;
}
else {
// other error
m_brunning = false;
break;
}
}
}
}
if (*hpipe != INVALID_HANDLE_VALUE)
CloseHandle(*hpipe);
for (auto& thread : receive_threads)
{
if (thread.first->joinable())
{
{
std::lock_guard lock(m_hpipes_mutex);
if (*thread.second != INVALID_HANDLE_VALUE)
{
FlushFileBuffers(*thread.second);
DisconnectNamedPipe(*thread.second);
CloseHandle(*thread.second);
*thread.second = INVALID_HANDLE_VALUE;
}
}
thread.first->join();
}
}
return;
}
bool ValidHandle(HANDLE hpipe) const { if (hpipe == INVALID_HANDLE_VALUE || hpipe == nullptr) return false; else return true; }
HANDLE m_sync_event = nullptr;
HANDLE m_connect_pipe = nullptr;
CSocketObject::func_getdata m_getdata;
std::atomic_bool m_brunning;
std::unique_ptr<std::thread> m_listen_thread;
std::mutex m_hpipes_mutex;
//for test
std::atomic_int m_logs_total_count = 0;
};
} // namespace cchips