forked from spender-sandbox/cuckoomon-modified
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipe.c
222 lines (194 loc) · 5.74 KB
/
pipe.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
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
/*
Cuckoo Sandbox - Automated Malware Analysis
Copyright (C) 2010-2015 Cuckoo Sandbox Developers, Optiv, Inc. ([email protected])
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 "ntapi.h"
#include "hooking.h"
#include "pipe.h"
#include "utf8.h"
#include "misc.h"
#include "config.h"
#include "log.h"
static int _pipe_utf8x(char **out, unsigned short x)
{
unsigned char buf[3];
int len = utf8_encode(x, buf);
if(*out != NULL) {
memcpy(*out, buf, len);
*out += len;
}
return len;
}
static int _pipe_ascii(char **out, const char *s, int len)
{
int ret = 0;
while (len-- != 0) {
ret += _pipe_utf8x(out, *(unsigned char *) s++);
}
return ret;
}
static int _pipe_unicode(char **out, const wchar_t *s, int len)
{
int ret = 0;
while (len-- != 0) {
ret += _pipe_utf8x(out, *(unsigned short *) s++);
}
return ret;
}
static int _pipe_sprintf(char *out, const char *fmt, va_list args)
{
int ret = 0;
while (*fmt != 0) {
if(*fmt != '%') {
ret += _pipe_utf8x(&out, *fmt++);
continue;
}
if(*++fmt == 'z') {
const char *s = va_arg(args, const char *);
if(s == NULL) return -1;
ret += _pipe_ascii(&out, s, (int)strlen(s));
}
else if (*fmt == 'c') {
char buf[2];
buf[0] = va_arg(args, char);
buf[1] = '\0';
ret += _pipe_ascii(&out, buf, 1);
}
else if(*fmt == 'Z') {
const wchar_t *s = va_arg(args, const wchar_t *);
if(s == NULL) return -1;
ret += _pipe_unicode(&out, s, lstrlenW(s));
}
else if (*fmt == 'F') {
const wchar_t *s = va_arg(args, const wchar_t *);
wchar_t *absolutepath = malloc(32768 * sizeof(wchar_t));
if (s == NULL) return -1;
if (absolutepath) {
ensure_absolute_unicode_path(absolutepath, s);
ret += _pipe_unicode(&out, absolutepath, lstrlenW(absolutepath));
free(absolutepath);
}
else {
return -1;
}
}
else if (*fmt == 's') {
int len = va_arg(args, int);
const char *s = va_arg(args, const char *);
if(s == NULL) return -1;
ret += _pipe_ascii(&out, s, len < 0 ? (int)strlen(s) : len);
}
else if(*fmt == 'S') {
int len = va_arg(args, int);
const wchar_t *s = va_arg(args, const wchar_t *);
if(s == NULL) return -1;
ret += _pipe_unicode(&out, s, len < 0 ? lstrlenW(s) : len);
}
else if(*fmt == 'o') {
UNICODE_STRING *str = va_arg(args, UNICODE_STRING *);
if(str == NULL) return -1;
ret += _pipe_unicode(&out, str->Buffer,
str->Length / sizeof(wchar_t));
}
else if(*fmt == 'O') {
OBJECT_ATTRIBUTES *obj = va_arg(args, OBJECT_ATTRIBUTES *);
wchar_t path[MAX_PATH_PLUS_TOLERANCE];
wchar_t *absolutepath;
if(obj == NULL || obj->ObjectName == NULL) return -1;
absolutepath = malloc(32768 * sizeof(wchar_t));
if (absolutepath) {
path_from_object_attributes(obj, path, (unsigned int)MAX_PATH_PLUS_TOLERANCE);
ensure_absolute_unicode_path(absolutepath, path);
ret += _pipe_unicode(&out, absolutepath, lstrlenW(absolutepath));
free(absolutepath);
}
else {
ret += _pipe_unicode(&out, L"", 0);
}
}
else if(*fmt == 'd') {
char s[32];
num_to_string(s, sizeof(s), va_arg(args, int));
ret += _pipe_ascii(&out, s, (int)strlen(s));
}
else if(*fmt == 'x') {
char s[16];
sprintf(s, "%x", va_arg(args, int));
ret += _pipe_ascii(&out, s, (int)strlen(s));
}
else if (*fmt == 'p') {
char s[18];
sprintf(s, "%p", va_arg(args, void *));
ret += _pipe_ascii(&out, s, (int)strlen(s));
}
else {
const char *msg = "-- UNKNOWN FORMAT STRING -- ";
ret += _pipe_ascii(&out, msg, (int)strlen(msg));
}
fmt++;
}
return ret;
}
// reminder: %s doesn't follow sprintf semantics, use %z instead
int pipe(const char *fmt, ...)
{
va_list args;
int len;
int ret = -1;
lasterror_t lasterror;
va_start(args, fmt);
get_lasterrors(&lasterror);
log_flush();
len = _pipe_sprintf(NULL, fmt, args);
if (len > 0) {
char *buf = calloc(1, len + 1);
_pipe_sprintf(buf, fmt, args);
#ifdef CUCKOODBG_PIPE
char filename[64];
snprintf(filename, sizeof(filename), "c:\\pipe%u.log", GetCurrentProcessId());
FILE *f = fopen(filename, "ab");
if (f) {
fwrite(buf, len, 1, f);
fclose(f);
ret = 0;
}
#else
if (CallNamedPipeW(g_config.pipe_name, buf, len, buf, len,
(unsigned long *)&len, NMPWAIT_WAIT_FOREVER) != 0)
ret = 0;
#endif
free(buf);
}
va_end(args);
set_lasterrors(&lasterror);
return ret;
}
int pipe2(void *out, int *outlen, const char *fmt, ...)
{
va_list args;
int len;
int ret = -1;
va_start(args, fmt);
len = _pipe_sprintf(NULL, fmt, args);
if(len > 0) {
char *buf = calloc(1, len + 1);
_pipe_sprintf(buf, fmt, args);
va_end(args);
if(CallNamedPipeW(g_config.pipe_name, buf, len, out, *outlen,
(DWORD *) outlen, NMPWAIT_WAIT_FOREVER) != 0)
ret = 0;
free(buf);
}
return ret;
}