-
Notifications
You must be signed in to change notification settings - Fork 30
/
libqs.c
2548 lines (1863 loc) · 76.2 KB
/
libqs.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
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 2016, 2017 Tyler McLellan TyLabs.com
* @tylabs
* QuickSand.io - Document malware forensics tool
*
* File libqs.c Sep 12 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
//
#include "libqs.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <stdarg.h>
#include <time.h>
#include <zip.h>
#include <zlib.h>
#include "sha2.c"
#include "sha1.c"
#include "md5.c"
#include "jWrite.c"
#include <yara.h>
//#include <libolecf.h> //2.0 macro decompression
//#include "lznt1.c" //2.0 macro decompression
#define KMP_MAX 1024
#ifndef min
#define min(x, y) ((x < y) ? (x) : (y))
#endif
#ifndef CHUNK
#define CHUNK 16384
#endif
#define WHITESPACE 64
#define EQUALS 65
#define INVALID 66
#pragma GCC diagnostic ignored "-Wdeprecated-declarations" //for macosx use of openssl libs
#define QS_FILE_CHILD 1
#define QS_FILE_NEXT 2
//#define DEBUG 1
size_t snprintfcat(char* buf, size_t bufSize, char const* fmt, ...) {
size_t result;
va_list args;
size_t len = strnlen( buf, bufSize);
va_start( args, fmt);
result = vsnprintf( buf + len, bufSize - len, fmt, args);
va_end( args);
return result + len;
}
/* Converts a hex character to its integer value */
char from_hex(char ch) {
return isdigit(ch) ? ch - '0' : tolower(ch) - 'a' + 10;
}
/* Converts an integer value to its hex character*/
char to_hex(char code) {
static char hex[] = "0123456789abcdef";
return hex[code & 15];
}
/* Returns a url-encoded version of str */
/* IMPORTANT: be sure to free() the returned string after use */
char *url_encode(const char *str) {
char *pstr = (char *) str, *buf = malloc(strlen(str) * 3 + 1), *pbuf = buf;
while (*pstr) {
if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' || *pstr == '~')
*pbuf++ = *pstr;
else if (*pstr == ' ')
*pbuf++ = '+';
else
*pbuf++ = '%', *pbuf++ = to_hex(*pstr >> 4), *pbuf++ = to_hex(*pstr & 15);
pstr++;
}
*pbuf = '\0';
return buf;
}
char *str_escape(const char *str) {
char *pstr = (char *) str, *buf = malloc(strlen(str) * 2 + 1), *pbuf = buf;
while (*pstr) {
if (*pstr == '\\') {
*pbuf++ = '\\';
*pbuf++ = *pstr;
} else
*pbuf++ = *pstr;
pstr++;
}
*pbuf = '\0';
return buf;
}
/* Returns a url-decoded version of str */
/* IMPORTANT: be sure to free() the returned string after use */
char *url_decode(const char *str) {
char *pstr = (char *) str, *buf = malloc(strlen(str) + 1), *pbuf = buf;
while (*pstr) {
if (*pstr == '%') {
if (pstr[1] && pstr[2]) {
*pbuf++ = from_hex(pstr[1]) << 4 | from_hex(pstr[2]);
pstr += 2;
}
} else if (*pstr == '+') {
*pbuf++ = ' ';
} else {
*pbuf++ = *pstr;
}
pstr++;
}
*pbuf = '\0';
return buf;
}
struct qs_message* quicksand_build_message(const char* identifier, struct qs_file *parent, struct qs_file **qs_root, int rel) {
struct qs_message *newone = malloc(sizeof(struct qs_message));
newone->identifier = identifier;
newone->parent = parent;
newone->qs_root = *qs_root;
newone->rel = rel;
return newone;
}
int quicksandDedup(struct qs_file *qs_file_iterator, const char *sha256) {
if (qs_file_iterator == NULL)
return FALSE;
if (qs_file_iterator->sha256 != NULL && strstr(qs_file_iterator->sha256, sha256))
return TRUE;
if (qs_file_iterator->next != NULL)
if (quicksandDedup(qs_file_iterator->next, sha256))
return TRUE;
if (qs_file_iterator->child != NULL)
if (quicksandDedup(qs_file_iterator->child, sha256))
return TRUE;
return FALSE;
}
void quicksandGraph(char *buffer, int buf_len, int depth, struct qs_file *qs_file_iterator) {
if (qs_file_iterator == NULL)
snprintfcat(buffer, buf_len, "iterator is null\n");
if (qs_file_iterator->identifier == NULL)
snprintfcat(buffer, buf_len, "identifier is null\n");
if (qs_file_iterator->count > 0) {
snprintfcat(buffer, buf_len, "%*s" "-%d> %s {%d}\n", depth+1, " ", depth, qs_file_iterator->identifier, qs_file_iterator->count);
if (qs_file_iterator->hits != NULL) {
struct qs_hit *qs_hit_iterator;
snprintfcat(buffer, buf_len, "%*s" "md5:%s\n", depth+2, " ", qs_file_iterator->md5);
snprintfcat(buffer, buf_len, "%*s" "sha1:%s\n", depth+2, " ", qs_file_iterator->sha1);
snprintfcat(buffer, buf_len, "%*s" "sha256:%s\n", depth+2, " ", qs_file_iterator->sha256);
snprintfcat(buffer, buf_len, "%*s" "sha512:%s\n", depth+2, " ", qs_file_iterator->sha512);
snprintfcat(buffer, buf_len, "%*s" "head:%s\n", depth+2, " ", qs_file_iterator->head);
if(qs_file_iterator->parent != NULL)
snprintfcat(buffer, buf_len, "%*s" "parentsha256:%s\n", depth+2, " ", qs_file_iterator->parent->sha256);
snprintfcat(buffer, buf_len, "%*s" "size:%lu\n", depth+2, " ", qs_file_iterator->data_len);
for (qs_hit_iterator = qs_file_iterator->hits; qs_hit_iterator->next != NULL; qs_hit_iterator = qs_hit_iterator->next) {
snprintfcat(buffer, buf_len, "%*s" "%s:%s\n", depth+2, " ",qs_hit_iterator->name, qs_hit_iterator->value);
}
snprintfcat(buffer, buf_len, "%*s" "%s:%s\n\n", depth+2, " ",qs_hit_iterator->name, qs_hit_iterator->value);
}
}
if (qs_file_iterator->next != NULL)
quicksandGraph(buffer, buf_len, depth, qs_file_iterator->next);
if (qs_file_iterator->child != NULL)
quicksandGraph(buffer, buf_len, depth+1, qs_file_iterator->child);
}
char hashString(const char *str, const char *content) {
char set[62] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
int i, t=0, l;
for(i=0; i < strlen(str); i++){
t += str[i];
}
for(i=0; i < strlen(content); i++){
t += content[i];
}
return set[t % 62];
}
char hashSize(long unsigned s) {
char set[62] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
int i, t=0, l;
t = s / 49999;
return set[t % 62];
}
void quicksandStructural(char *buffer, int buf_len, int depth, struct qs_file *qs_file_iterator) {
if (qs_file_iterator == NULL)
snprintfcat(buffer, buf_len, "iterator is null\n");
if (qs_file_iterator->identifier == NULL)
snprintfcat(buffer, buf_len, "identifier is null\n");
//if (qs_file_iterator->count > 0) {
if (strcmp(qs_file_iterator->identifier,"xor") == 0) {
struct qs_hit *qs_hit_iterator;
const char *xor="0";
for (qs_hit_iterator = qs_file_iterator->hits; qs_hit_iterator->next != NULL; qs_hit_iterator = qs_hit_iterator->next) {
if (strcmp(qs_hit_iterator->name,"xorkey") == 0 || strcmp(qs_hit_iterator->name,"xortkey") == 0)
xor = qs_hit_iterator->value;
}
snprintfcat(buffer, buf_len, "%c", hashString(qs_file_iterator->identifier,xor));
} else if (strcmp(qs_file_iterator->identifier,"rol") == 0) {
struct qs_hit *qs_hit_iterator;
const char *rol="0";
for (qs_hit_iterator = qs_file_iterator->hits; qs_hit_iterator->next != NULL; qs_hit_iterator = qs_hit_iterator->next) {
if (strcmp(qs_hit_iterator->name,"rol") == 0)
rol = qs_hit_iterator->value;
}
snprintfcat(buffer, buf_len, "%c", hashString(qs_file_iterator->identifier,rol));
} else
snprintfcat(buffer, buf_len, "%c", hashString(qs_file_iterator->identifier, qs_file_iterator->head));
//}
if (qs_file_iterator->next != NULL)
quicksandStructural(buffer, buf_len, depth, qs_file_iterator->next);
if (qs_file_iterator->child != NULL)
quicksandStructural(buffer, buf_len, depth+1, qs_file_iterator->child);
}
void quicksand_json(char *buffer, int buflen, int depth, struct qs_file *qs_file_iterator, int inarray) {
int b=0, c=0, d=0;
if (qs_file_iterator == NULL) {
//jwObj_null( "iterator is null");
return;
}
if (qs_file_iterator->identifier == NULL) {
//jwObj_null( "identifier is null");
return;
}
if (inarray)
jwArr_object();
jwObj_string("identifier", str_escape(qs_file_iterator->identifier));
if (qs_file_iterator->md5 != NULL)
jwObj_string("md5", (char *)qs_file_iterator->md5);
if (qs_file_iterator->sha1 != NULL)
jwObj_string("sha1", (char *)qs_file_iterator->sha1);
if (qs_file_iterator->sha256 != NULL)
jwObj_string("sha256", (char *)qs_file_iterator->sha256);
if (qs_file_iterator->sha512 != NULL)
jwObj_string("sha512", (char *)qs_file_iterator->sha512);
if (qs_file_iterator->head != NULL)
jwObj_string("head", (char *)qs_file_iterator->head);
jwObj_int( "size", (int)qs_file_iterator->data_len);
if(qs_file_iterator->parent != NULL && qs_file_iterator->parent->sha256 != NULL)
jwObj_string("parentsha256", (char *)qs_file_iterator->parent->sha256);
if (qs_file_iterator->count > 0) {
if (qs_file_iterator->hits != NULL) {
struct qs_hit *qs_hit_iterator;
for (qs_hit_iterator = qs_file_iterator->hits; qs_hit_iterator->next != NULL; qs_hit_iterator = qs_hit_iterator->next) {
if (strstr(qs_hit_iterator->name, "yara:exploits") != NULL)
b=1;
else if (strstr(qs_hit_iterator->name, "yara:executable") != NULL)
c=1;
else if (strstr(qs_hit_iterator->name, "yara:general") != NULL)
d=1;
}
if (strstr(qs_hit_iterator->name, "yara:exploits") != NULL)
b=1;
else if (strstr(qs_hit_iterator->name, "yara:executable") != NULL)
c=1;
else if (strstr(qs_hit_iterator->name, "yara:general") != NULL)
d=1;
if (b == 1){
jwObj_array( "yara:exploits" );
for (qs_hit_iterator = qs_file_iterator->hits; qs_hit_iterator->next != NULL; qs_hit_iterator = qs_hit_iterator->next) {
if (strstr(qs_hit_iterator->name, "yara:exploits") != NULL)
jwArr_string((char *)qs_hit_iterator->value);
}
if (strstr(qs_hit_iterator->name, "yara:exploits") != NULL)
jwArr_string((char *)qs_hit_iterator->value);
jwEnd();
}
if (c == 1){
jwObj_array( "yara:executable" );
for (qs_hit_iterator = qs_file_iterator->hits; qs_hit_iterator->next != NULL; qs_hit_iterator = qs_hit_iterator->next) {
if (strstr(qs_hit_iterator->name, "yara:executable") != NULL)
jwArr_string((char *)qs_hit_iterator->value);
}
if (strstr(qs_hit_iterator->name, "yara:executable") != NULL)
jwArr_string((char *)qs_hit_iterator->value);
jwEnd();
}
if (d == 1){
jwObj_array( "yara:general" );
for (qs_hit_iterator = qs_file_iterator->hits; qs_hit_iterator->next != NULL; qs_hit_iterator = qs_hit_iterator->next) {
if (strstr(qs_hit_iterator->name, "yara:general") != NULL)
jwArr_string((char *)qs_hit_iterator->value);
}
if (strstr(qs_hit_iterator->name, "yara:general") != NULL)
jwArr_string((char *)qs_hit_iterator->value);
jwEnd();
}
for (qs_hit_iterator = qs_file_iterator->hits; qs_hit_iterator->next != NULL; qs_hit_iterator = qs_hit_iterator->next) {
if (strstr(qs_hit_iterator->name, "yara:exploits") == NULL && strstr(qs_hit_iterator->name, "yara:executable") == NULL && strstr(qs_hit_iterator->name, "yara:general") == NULL)
jwObj_string((char * )qs_hit_iterator->name, (char *)qs_hit_iterator->value);
}
if (strstr(qs_hit_iterator->name, "yara:exploits") == NULL && strstr(qs_hit_iterator->name, "yara:executable") == NULL && strstr(qs_hit_iterator->name, "yara:general") == NULL)
jwObj_string((char *)qs_hit_iterator->name, (char *)qs_hit_iterator->value);
}
}
if (qs_file_iterator->child != NULL) {
//if (qs_file_iterator->child->hits == NULL)
jwObj_array( "child" );
quicksand_json(buffer, buflen, depth+1, qs_file_iterator->child, 1);
//if (qs_file_iterator->child->hits == NULL)
jwEnd();
}
//if (inarray && qs_file_iterator->count > 0 && qs_file_iterator->hits != NULL ) {
if (inarray)
jwEnd();
//}
if (qs_file_iterator->next != NULL) {
quicksand_json(buffer, buflen, depth, qs_file_iterator->next, 1);
}
}
const char* quicksandsha256(const unsigned char *data, unsigned long data_len) {
SHA256_CTX ctx256;
SHA256_Init(&ctx256);
SHA256_Update(&ctx256, data, data_len);
char *sha256 = malloc(QUICKSAND_SHA256_SIZE*2+1);
SHA256_End(&ctx256, sha256);
return sha256;
}
void quicksandUnallocateHits(struct qs_hit *qs_hit_iterator) {
if (qs_hit_iterator == NULL)
return;
if (qs_hit_iterator->next != NULL)
quicksandUnallocateHits(qs_hit_iterator->next);
qs_hit_iterator->next = NULL;
}
void quicksandUnallocateFiles(struct qs_file *qs_file_iterator) {
if (qs_file_iterator == NULL)
return;
if (qs_file_iterator->next != NULL)
quicksandUnallocateFiles(qs_file_iterator->next);
if (qs_file_iterator->child != NULL)
quicksandUnallocateFiles(qs_file_iterator->child);
////if (qs_file_iterator->data != NULL)
//// free((char *) qs_file_iterator->data);
////if (qs_file_iterator->identifier != NULL)
//// free((char *) qs_file_iterator->identifier);
qs_file_iterator->next = NULL;
qs_file_iterator->child = NULL;
}
int intcmp(const void *aa, const void *bb)
{
const int *a = aa, *b = bb;
return (*a < *b) ? -1 : (*a > *b);
}
//dump drop files to disk
void quicksandDropFiles(struct qs_file *qs_file_iterator, struct qs_file **qs_root) {
int c, locations[KMP_MAX], confirmed[KMP_MAX], conf=0, k, i;
unsigned const char heads[6][32] = {"\xCA\xFE\xBA\xBE",
"\xCE\xFA\xED\xFE",
"\x7F\x45\x4C\x46",
"\x7B\x5crt",
".vbs\x00",
"\x25\x50\x44\x46",
//"\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1"
};
unsigned const char eofs[3][32] = {"\x0A\x25\x25\x45\x4F\x46\x0A",
"\x0D\x0A\x25\x25\x45\x4F\x46\x0D\x0A",
"\x0D\x25\x25\x45\x4F\x46\x0D"};
if (qs_file_iterator == NULL)
return;
//scan current
if (qs_file_iterator->data != NULL) {
//printf("doing %s\n", qs_file_iterator->identifier);
//MZ header
unsigned const char header[] = "MZ";
c = KMPSearch(header, 2, qs_file_iterator->data, (int) qs_file_iterator->data_len, locations);
if (c > 0) {
//printf ("%s: found %d MZ hits\n", qs_file_iterator->identifier, c);
for (i=0; i < c; i++) {
if (qs_file_iterator->data[locations[i]+78] == 'T' && qs_file_iterator->data[locations[i]+79] == 'h' && qs_file_iterator->data[locations[i]+80] == 'i' && qs_file_iterator->data[locations[i]+81] == 's') {
//printf ("good hit for executable header @%d - begin drop sequence alpha 001\n", locations[i]);
confirmed[conf++] = locations[i];
}
}
}
//MZ header transposed
unsigned const char headertr[] = "ZM";
c = KMPSearch(headertr, 2, qs_file_iterator->data, (int) qs_file_iterator->data_len, locations);
if (c > 0) {
//printf ("%s: found %d ZM hits\n", qs_file_iterator->identifier, c);
for (i=0; i < c; i++) {
if (qs_file_iterator->data[locations[i]+78] == 'h' && qs_file_iterator->data[locations[i]+79] == 'T' && qs_file_iterator->data[locations[i]+80] == 's' && qs_file_iterator->data[locations[i]+81] == 'i') {
//printf ("good hit for transposed exe header @%d - begin drop sequence alpha 001\n", locations[i]);
confirmed[conf++] = locations[i];
}
}
}
for ( i = 0; i < (sizeof(heads)/sizeof(heads[0]) ) ; i++ ) {
//printf("start\n");
c = KMPSearch(heads[i], (int) strnlen((char *) heads[i],32), qs_file_iterator->data, (int) qs_file_iterator->data_len, locations);
//printf("true key %d %s [%d]\n", i, key_db[i], c);
for(k=0; k < c; k++) {
if (locations[k] != 0 || qs_file_iterator != *qs_root) {
//printf("sub header location is %d for %d\n", locations[k], i);
confirmed[conf++] = locations[k];
}
}
}
for ( i = 0; i < (sizeof(eofs)/sizeof(eofs[0]) ) ; i++ ) {
//printf("ends\n");
c = KMPSearch(eofs[i], (int) strnlen((char *) eofs[i], 32), qs_file_iterator->data, (int) qs_file_iterator->data_len, locations);
}
qsort(confirmed, conf, sizeof(int), intcmp);
//take list of all hits
for (i=0; i < conf; i++) {
//printf("xheader at %d\n", confirmed[i]);
char *nname = malloc(256);
snprintf(nname, 256, "%s%s_%s_%d.qsdump", QUICKSAND_OUT_DIR, (*qs_root)->sha256, qs_file_iterator->sha256, i+1);
FILE *out = fopen(nname,"wb");
int end = (int ) qs_file_iterator->data_len;
if (i + 1 < conf)
end = confirmed[i+1];
end -= confirmed[i];
if (QUICKSAND_MAX_DROP > 0 && end > QUICKSAND_MAX_DROP)
end = QUICKSAND_MAX_DROP;
//printf("%s dump %s of %d bytes\n", qs_file_iterator->identifier, nname, end);
fwrite(qs_file_iterator->data+confirmed[i], 1, end, out);
fclose(out);
}
}
//drop decoded executables
if (qs_file_iterator->next != NULL)
quicksandDropFiles(qs_file_iterator->next, qs_root);
if (qs_file_iterator->child != NULL)
quicksandDropFiles(qs_file_iterator->child, qs_root);
}
void quicksandDropObjects(struct qs_file *qs_file_iterator, struct qs_file **qs_root) {
if (qs_file_iterator == NULL)
return;
//scan current
if (qs_file_iterator->data != NULL) {
//printf("doing %s\n", qs_file_iterator->identifier);
if (qs_file_iterator != *qs_root) {
char *nname = malloc(256);
snprintf(nname, 256, "%s%s_%s.qsobj", QUICKSAND_OUT_DIR, (*qs_root)->sha256, qs_file_iterator->sha256);
FILE *out = fopen(nname,"wb");
int end = (int ) qs_file_iterator->data_len;
if (QUICKSAND_MAX_DROP > 0 && end > QUICKSAND_MAX_DROP)
end = QUICKSAND_MAX_DROP;
//printf("%s obj %s of %d bytes\n", qs_file_iterator->identifier, nname, end);
fwrite(qs_file_iterator->data, 1, end, out);
fclose(out);
}
}
//drop object
if (qs_file_iterator->next != NULL)
quicksandDropObjects(qs_file_iterator->next, qs_root);
if (qs_file_iterator->child != NULL)
quicksandDropObjects(qs_file_iterator->child, qs_root);
}
void quicksandGeneralScan(struct qs_file *qs_file_iterator, struct qs_file **qs_root) {
if (qs_file_iterator == NULL)
return;
//scan current
if (qs_file_iterator->data != NULL) {
quicksand_yara_mem(qs_file_iterator->data, qs_file_iterator->data_len, QUICKSAND_GENERAL_YARA, "general", quicksand_build_message(qs_file_iterator->identifier,qs_file_iterator,(qs_root),QS_FILE_CHILD) );
}
if (qs_file_iterator->next != NULL)
quicksandGeneralScan(qs_file_iterator->next, qs_root);
if (qs_file_iterator->child != NULL)
quicksandGeneralScan(qs_file_iterator->child, qs_root);
}
void quicksandInit() {
//malware_score=0;
int result = yr_initialize();
//qs_root = NULL;
if (result != ERROR_SUCCESS)
printf("error 1\n");
}
void quicksandReset(struct qs_file **qs_root) {
quicksandUnallocateFiles(*qs_root);
if (*qs_root != NULL)
free(*qs_root);
*qs_root = NULL;
}
void quicksandDestroy() {
yr_finalize();
}
struct qs_hit* quickSandHit(const char *name, const char *value, long unsigned offset) {
struct qs_hit *newHit = malloc(sizeof(struct qs_hit));
newHit->name = name;
newHit->value = value;
newHit->offset = offset;
newHit->next = NULL;
return newHit;
}
void quicksandStore(struct qs_file *target, const char *name1, const char *value1, long unsigned offset) {
const char *name = strdup(name1);
const char *value = strdup(value1);
if (target == NULL)
printf("warning target for store hit is null");
else {
if (target->hits == NULL) {
target->hits = quickSandHit(name, value, offset);
target->count++;
} else {
struct qs_hit *qs_hit_iterator;
for(qs_hit_iterator = target->hits; qs_hit_iterator->next != NULL; qs_hit_iterator = qs_hit_iterator->next) {
if (qs_hit_iterator == NULL) {
printf("warning target iterator for store hit is null");
break;
}
}
qs_hit_iterator->next = quickSandHit(name, value, offset);
target->count++;
}
}
}
int base64decode (char *in, size_t inLen, unsigned char *out, size_t *outLen) {
char *end = in + inLen;
char iter = 0;
size_t buf = 0, len = 0;
while (in < end) {
unsigned char c = db64[*in++];
switch (c) {
case WHITESPACE: continue; /* skip whitespace */
case INVALID: return 1; /* invalid input, return error */
case EQUALS: /* pad character, end of data */
in = end;
continue;
default:
buf = buf << 6 | c;
iter++; // increment the number of iteration
/* If the buffer is full, split it into bytes */
if (iter == 4) {
if ((len += 3) > *outLen) return 1; /* buffer overflow */
*(out++) = (buf >> 16) & 255;
*(out++) = (buf >> 8) & 255;
*(out++) = buf & 255;
buf = 0; iter = 0;
}
}
}
if (iter == 3) {
if ((len += 2) > *outLen) return 1; /* buffer overflow */
*(out++) = (buf >> 10) & 255;
*(out++) = (buf >> 2) & 255;
}
else if (iter == 2) {
if (++len > *outLen) return 1; /* buffer overflow */
*(out++) = (buf >> 4) & 255;
}
*outLen = len; /* modify to reflect the actual output size */
return 0;
}
int inf(const unsigned char *src, int srcLen, unsigned char *dst, int dstLen) {
z_stream strm = {0};
strm.total_in = strm.avail_in = srcLen;
strm.total_out = strm.avail_out = dstLen;
strm.next_in = (Bytef *) src;
strm.next_out = (Bytef *) dst;
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
int err = -1;
int ret = -1;
err = inflateInit2(&strm, -MAX_WBITS);
if (err == Z_OK) {
err = inflate(&strm, Z_FINISH);
if (err == Z_STREAM_END) {
ret = strm.total_out;
}
else {
inflateEnd(&strm);
return err;
}
}
else {
inflateEnd(&strm);
return err;
}
inflateEnd(&strm);
return ret;
}
int unc(const unsigned char *src, int srcLen, unsigned char *dst, int dstLen) {
z_stream strm = {0};
strm.total_in = strm.avail_in = srcLen;
strm.total_out = strm.avail_out = dstLen;
strm.next_in = (Bytef *) src;
strm.next_out = (Bytef *) dst;
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
int err = -1;
int ret = -1;
err = inflateInit(&strm);
if (err == Z_OK) {
err = inflate(&strm, Z_FINISH);
if (err == Z_STREAM_END) {
ret = strm.total_out;
}
else {
inflateEnd(&strm);
return err;
}
}
else {
inflateEnd(&strm);
return err;
}
inflateEnd(&strm);
return ret;
}
unsigned char* quicksand_parse_rtf(const unsigned char *data, unsigned long data_len, unsigned long *new_len)
{
unsigned char* entity = malloc(data_len);
int i, j=0;
unsigned long k=0;
//printf("looking for hex blocks\n");
for (i = 0; i < data_len; i++) {
if (data[i] == '\\') {
//store this and next
//advance
//follow the white rabbit and remove control words
j = 0;
for (j = i+1; j < data_len; j++){
if (isspace(data[j])) {
break;
} else if ((data[j] == '{' || data[j] == '}') && data[j-1] != '\\' ) {
j--;
break;
}
}
i = j;
// for {\* ignore non standard control words
} else if (data[i] == '{' && data[i+1] == '\\' && data[i+2] == '*' ) {
//follow the white rabbit and remove control words
if (data[i+1] == '\\') {
j = 0;
for (j = i+2; j < data_len; j++){
if ((data[j] == '{' || data[j] == '}') && data[j-1] != '\\' ) {
j--;
break;
}
}
i = j;
}
} else if (data[i] == '{' ) {
//follow the white rabbit and remove control words
if (data[i+1] == '\\') {
j = 0;
for (j = i+2; j < data_len; j++){
if (isspace(data[j])) {
break;
} else if ((data[j] == '{' || data[j] == '}') && data[j-1] != '\\' ) {
j--;
break;
}
}
i = j;
}
} else if (data[i] == '}' ) {
//do nothing
} else if (iscntrl(data[i]) && data[i] != 0x0a && data[i] != 0x09 && data[i] != 0x0d && data[i] != '\\' ) {
//do nothing
j = 0;
for (j = i+2; j < data_len; j++){
if ((data[j] == '{' || data[j] == '}') && data[j-1] != '\\' ) {
j--;
break;
}
}
i = j;
} else if (!isgraph(data[i])) {
//do nothing
} else {
//store this
entity[k] = data[i];
k++;
}
}
*new_len = k;
return (entity);
}
void quicksand_extract_blocks(const unsigned char *data, unsigned long data_len, struct qs_message *source, struct qs_file **qs_root)
{
unsigned char* entity = malloc(data_len);
int i, j=0,k=0;;
//printf("looking for hex blocks\n");
for (i = 0; i < data_len; i++) {
if (isxdigit(data[i]) ) {
entity[j] = data[i];
j++;
} else if (data[i] == 0x0a || data[i] == 0x09 || data[i] == 0x0d || data[i] == '\\' || data[i] == '\'') { //data[i] == 0x20 ||
//no nothing
k++;
} else {
if (j / 2 >= QUICKSAND_MIN_FILE_SIZE) {
//printf("found hex block of %d size at %d\n", j, i-j-k);
//send here
unsigned char *decoded_rtf = malloc(j+1);
hex2str(entity, j, decoded_rtf);
char *str = malloc(20);
snprintf(str, 20, "%s%d", "hex@", i-j-k);
quicksand_do(decoded_rtf, j/2, quicksand_build_message(str, source->parent, qs_root, QS_FILE_CHILD), qs_root);
//free(decoded_rtf);
}
j=0;
k=0;
}
}
//check if still in block and send here too
if (j / 2 >= QUICKSAND_MIN_FILE_SIZE) {
//printf("found hex block of %d size at %d\n", j, data_len-j-k);
unsigned char *decoded_rtf = malloc(j+1);
hex2str(entity, j, decoded_rtf);
char *str = malloc(20);
snprintf(str, 20, "%s%lu", "hex@", data_len-j-k);
quicksand_do(decoded_rtf, j/2, quicksand_build_message(str, source->parent, qs_root, QS_FILE_CHILD), qs_root);
//free(decoded_rtf);
}
free(entity);
}
void quicksand_extract_ezip(const unsigned char *data, unsigned long data_len, struct qs_message *source, struct qs_file **qs_root)
{
unsigned char* entity = malloc(data_len+1);
int i, j=0,start=0, noheader=0;
//printf("looking for ezip blocks\n");
if(data_len < 1024)
return;
for (i = 0; i < 1024; i++) {
if (data[i] == 0xD0 && data[i+1] == 0xCF && data[i+2] == 0x11) {
//printf("found docfile header at %d\n",i);
start = i;
noheader = 1;
break;
}
if (i == 1023)
start = 0;
}
//if (!noheader) {
for (i = start; i < data_len; i++) {
if (data[i] == 'P' && data[i+1] == 'K' && data[i+2] == 0x03 && data[i+3] == 0x04) {
for (j = 0; j < data_len-i-3; j++) {
entity[j] = data[i+j];
}
//printf("Found ezip block in %s at %d of %d bytes\n", source->identifier, i, j);
entity[j+1] = '\0';
char *str = malloc(20);
snprintf(str, 20, "%s%d", "zip@", i);
quicksand_do(entity, j, quicksand_build_message(str, source->parent, qs_root, QS_FILE_CHILD), qs_root);
break;
}
}
/*} else {
for (i = start; i < data_len; i+=512) {