-
Notifications
You must be signed in to change notification settings - Fork 30
/
quicksand.c
331 lines (239 loc) · 10.2 KB
/
quicksand.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
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
/* Copyright (c) 2016, 2017 Tyler McLellan TyLabs.com
* @tylabs
* QuickSand.io - Document malware forensics tool
*
* File quicksand.c May 24 2017
* Original source code available from https://github.com/tylabs/quicksand_lite
*
* Decode and look in streams of Office Documents, RTF, MIME MSO.
* XOR Database attack up to 256 byte keys to find embedded exe's.
* Lite version - doesn't include cryptanalysis module and latest Office CVEs
* Web version at http://quicksand.io/ has full features.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Commercial licensing is available for the full version.
*/
// Dependencies: libzip, yara, zlib
//
#define _CRT_SECURE_NO_WARNINGS
#include "libqs.c"
#include <stdarg.h>
#include "tinydir.h"
#define QS_MAX_BUFFER 1000000
#ifndef DEBUG
#define DEBUG 0
#endif
void qs_do_directory (const char *path, int raw, int json, int drop, int objects) {
FILE *f;
tinydir_dir dir;
tinydir_open(&dir, path);
while (dir.has_next)
{
tinydir_file file;
tinydir_readfile(&dir, &file);
//printf("%s\n", file.name);
if (file.is_dir)
{
if (file.name[0] != '.') {
qs_do_directory(file.path, raw, json, drop, objects);
}
} else {
//run file
printf("Scanning %s...\n", file.path);
f = fopen(file.path, "rb");
if( f == NULL )
{
perror("Error while opening the file.\n");
exit(EXIT_FAILURE);
}
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
fseek(f, 0, SEEK_SET);
unsigned char *string = malloc(fsize + 1);
fread(string, fsize, 1, f);
fclose(f);
struct qs_file *qs_root = NULL;
quicksand_do(string, fsize, quicksand_build_message("root", NULL, &qs_root, QS_FILE_CHILD), &qs_root);
if (qs_root == NULL) {
printf("skip\n");
tinydir_next(&dir);
continue;
}
assert(qs_root != NULL);
if (raw == 1 || json == 0) {
char *buffer = malloc(QS_MAX_BUFFER);
buffer[0] = '\0';
quicksandGraph(buffer, QS_MAX_BUFFER, 0, qs_root);
printf("%s\n", buffer);
}
if (json == 1) {
char *buffer2 = malloc(QS_MAX_BUFFER);
buffer2[0] = '\0';
jwOpen( buffer2, QS_MAX_BUFFER, JW_OBJECT, JW_PRETTY );
quicksand_json(buffer2, QS_MAX_BUFFER, 0, qs_root, FALSE);
//jwEnd();
int err;
err=jwClose();
if( err != JWRITE_OK )
printf( "Error: %s at function call %d\n", jwErrorToString(err), jwErrorPos() );
printf("%s\n", buffer2);
}
if (drop == 1)
quicksandDropFiles(qs_root, &qs_root);
if (objects == 1)
quicksandDropObjects(qs_root, &qs_root);
//quicksandGraph(0, qs_root);
quicksandReset(&qs_root);
printf("\n");
free(string);
}
tinydir_next(&dir);
}
//quicksandDestroy();
tinydir_close(&dir);
}
void printHelp(char *self) {
printf("Please specify a file or directory to process:\n");
printf(" > %s [options] malware.doc | dir\n\n", self);
printf(" Options:\n");
printf(" -h or --help: this message\n");
printf(" -j or --json: output json data\n");
printf(" -m or --math: try math ciphers\n");
printf(" -n or --not: try bitwise not\n");
printf(" -r or --raw: output proprietary text\n");
printf(" -d or --drop: drop extracted executables\n");
printf(" -o or --objects: drop all objects\n");
printf(" -p or --out: [dir] directory to write dropped files\n");
printf(" -y or --yara: skip yara scan for general / malware identification\n");
printf("\n");
printf("\nUse the QS environment variable to point to directory\nwith the quicksand-X.yara files:\n");
printf(" > export QS=/users/analyst/quicksand\n");
printf("\nRule filenames:\n quicksand_exe.yara: Executable detection\n");
printf(" quicksand_exploits.yara: CVE Signatures and active content\n quicksand_general.yara: Malware families on decoded executables\n");
}
int main (int argc, char *argv[])
{
FILE *f;
struct stat *s=malloc(sizeof(struct stat));
//printf("argv[0] = %s\n",argv[0]);
//printf("getenv(QS) = %s\n", getenv("QS"));
if (getenv("QS") != NULL) {
char *expyara = malloc(4096);
snprintf(expyara, 4096, "%s%s%s", getenv("QS"), "/", QUICKSAND_EXPLOITS_YARA);
QUICKSAND_EXPLOITS_YARA = expyara;
char *exeyara = malloc(4096);
snprintf(exeyara, 4096, "%s%s%s", getenv("QS"), "/", QUICKSAND_EXE_YARA);
QUICKSAND_EXE_YARA = exeyara;
char *generalyara = malloc(4096);
snprintf(generalyara, 4096, "%s%s%s", getenv("QS"), "/", QUICKSAND_GENERAL_YARA);
QUICKSAND_GENERAL_YARA = generalyara;
if (!file_exists(QUICKSAND_EXPLOITS_YARA) || !file_exists(QUICKSAND_EXE_YARA)) {
printf("Yara path in QS env variable does not exist.\n%s\n%s\n", QUICKSAND_EXPLOITS_YARA, QUICKSAND_EXE_YARA);
exit(1);
}
} else if (!file_exists(QUICKSAND_EXPLOITS_YARA) || !file_exists(QUICKSAND_EXE_YARA)) {
printf("Unable to locate yara signatures. Use QS environment variable to set directory path.\n%s\n%s\n", QUICKSAND_EXPLOITS_YARA, QUICKSAND_EXE_YARA);
exit(1);
}
if (getenv("QSDIR") != NULL) {
char *qsdir = malloc(4096);
snprintf(qsdir, 4096, "%s", getenv("QSDIR"));
QUICKSAND_OUT_DIR = qsdir;
}
int i,json=0,raw=0,drop=0,objects=0;
char *target = NULL;
for (i=1; i < argc; i++) {
if (strstr(argv[i], "-h") || strstr(argv[i], "--help") ) {
printHelp(argv[0]);
} else if (strstr(argv[i], "-j") || strstr(argv[i], "--json") ) {
json=1;
} else if (strstr(argv[i], "-r") || strstr(argv[i], "--raw") ) {
raw=1;
} else if (strstr(argv[i], "-d") || strstr(argv[i], "--drop") ) {
drop=1;
} else if (strstr(argv[i], "-o") || strstr(argv[i], "--objects") ) {
objects=1;
} else if (strstr(argv[i], "-n") || strstr(argv[i], "--not") ) {
QUICKSAND_NOT = 1;
} else if (strstr(argv[i], "-m") || strstr(argv[i], "--math") ) {
QUICKSAND_MATH = 1;
} else if (strstr(argv[i], "-y") || strstr(argv[i], "--yara") ) {
QUICKSAND_GENERAL_YARA_RUN = 0;
} else if (strstr(argv[i], "-p") || strstr(argv[i], "--out") ) {
if (i+1 < argc) {
char *qsdir = calloc(4096, 1);
snprintf(qsdir, 4096, "%s", argv[i+1]);
QUICKSAND_OUT_DIR = qsdir;
i++;
}
} else {
target = argv[i];
}
}
if (argc > 1) {
quicksandInit();
if(target != NULL && stat(target,s) == 0 )
{
if( s->st_mode & S_IFDIR )
{
//handle directory
qs_do_directory(target, raw, json, drop, objects);
}
else if( s->st_mode & S_IFREG )
{
//run file
f = fopen(target, "rb");
if( f == NULL )
{
perror("Error while opening the file.\n");
exit(EXIT_FAILURE);
}
setbuf(stdout, NULL);
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
fseek(f, 0, SEEK_SET);
unsigned char *string = malloc(fsize + 1);
fread(string, fsize, 1, f);
fclose(f);
struct qs_file *qs_root = NULL;
quicksand_do(string, fsize, quicksand_build_message("root", NULL, &qs_root, QS_FILE_CHILD), &qs_root);
if (qs_root != NULL) {
if (raw == 1 || json == 0) {
char *buffer = malloc(QS_MAX_BUFFER);
buffer[0] = '\0';
quicksandGraph(buffer, QS_MAX_BUFFER, 0, qs_root);
printf("%s\n\n\n", buffer);
}
if (json == 1) {
char *buffer2 = malloc(QS_MAX_BUFFER);
buffer2[0] = '\0';
jwOpen( buffer2, QS_MAX_BUFFER, JW_OBJECT, JW_PRETTY );
quicksand_json(buffer2, QS_MAX_BUFFER, 0, qs_root, FALSE);
//jwEnd();
int err;
err=jwClose();
if( err != JWRITE_OK )
printf( "Error: %s at function call %d\n", jwErrorToString(err), jwErrorPos() );
printf("%s\n", buffer2);
}
if (drop == 1)
quicksandDropFiles(qs_root, &qs_root);
if (objects == 1)
quicksandDropObjects(qs_root, &qs_root);
quicksandReset(&qs_root);
free(string);
free(s);
}
}
} else {
printf("nothing to do\n");
}
quicksandDestroy();
} else {
printHelp(argv[0]);
}
return 0;
}