forked from hillu/go-yara
-
Notifications
You must be signed in to change notification settings - Fork 7
/
parser.c
1476 lines (1217 loc) · 40.2 KB
/
parser.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) 2013. The YARA Authors. All Rights Reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <limits.h>
#include <stddef.h>
#include <string.h>
#include <yara_ahocorasick.h>
#include <yara_arena.h>
#include <yara_base64.h>
#include <yara_error.h>
#include <yara_exec.h>
#include <yara_integers.h>
#include <yara_mem.h>
#include <yara_modules.h>
#include <yara_object.h>
#include <yara_parser.h>
#include <yara_re.h>
#include <yara_strutils.h>
#include <yara_utils.h>
#define todigit(x) \
((x) >= 'A' && (x) <= 'F') ? ((uint8_t) (x - 'A' + 10)) \
: ((uint8_t) (x - '0'))
int yr_parser_emit(
yyscan_t yyscanner,
uint8_t instruction,
YR_ARENA_REF* instruction_ref)
{
return yr_arena_write_data(
yyget_extra(yyscanner)->arena,
YR_CODE_SECTION,
&instruction,
sizeof(uint8_t),
instruction_ref);
}
int yr_parser_emit_with_arg_double(
yyscan_t yyscanner,
uint8_t instruction,
double argument,
YR_ARENA_REF* instruction_ref,
YR_ARENA_REF* argument_ref)
{
int result = yr_arena_write_data(
yyget_extra(yyscanner)->arena,
YR_CODE_SECTION,
&instruction,
sizeof(uint8_t),
instruction_ref);
if (result == ERROR_SUCCESS)
result = yr_arena_write_data(
yyget_extra(yyscanner)->arena,
YR_CODE_SECTION,
&argument,
sizeof(double),
argument_ref);
return result;
}
int yr_parser_emit_with_arg_int32(
yyscan_t yyscanner,
uint8_t instruction,
int32_t argument,
YR_ARENA_REF* instruction_ref,
YR_ARENA_REF* argument_ref)
{
int result = yr_arena_write_data(
yyget_extra(yyscanner)->arena,
YR_CODE_SECTION,
&instruction,
sizeof(uint8_t),
instruction_ref);
if (result == ERROR_SUCCESS)
result = yr_arena_write_data(
yyget_extra(yyscanner)->arena,
YR_CODE_SECTION,
&argument,
sizeof(int32_t),
argument_ref);
return result;
}
int yr_parser_emit_with_arg(
yyscan_t yyscanner,
uint8_t instruction,
int64_t argument,
YR_ARENA_REF* instruction_ref,
YR_ARENA_REF* argument_ref)
{
int result = yr_arena_write_data(
yyget_extra(yyscanner)->arena,
YR_CODE_SECTION,
&instruction,
sizeof(uint8_t),
instruction_ref);
if (result == ERROR_SUCCESS)
result = yr_arena_write_data(
yyget_extra(yyscanner)->arena,
YR_CODE_SECTION,
&argument,
sizeof(int64_t),
argument_ref);
return result;
}
int yr_parser_emit_with_arg_reloc(
yyscan_t yyscanner,
uint8_t instruction,
void* argument,
YR_ARENA_REF* instruction_ref,
YR_ARENA_REF* argument_ref)
{
YR_ARENA_REF ref = YR_ARENA_NULL_REF;
DECLARE_REFERENCE(void*, ptr) arg;
memset(&arg, 0, sizeof(arg));
arg.ptr = argument;
int result = yr_arena_write_data(
yyget_extra(yyscanner)->arena,
YR_CODE_SECTION,
&instruction,
sizeof(uint8_t),
instruction_ref);
if (result == ERROR_SUCCESS)
result = yr_arena_write_data(
yyget_extra(yyscanner)->arena,
YR_CODE_SECTION,
&arg,
sizeof(arg),
&ref);
if (result == ERROR_SUCCESS)
result = yr_arena_make_ptr_relocatable(
yyget_extra(yyscanner)->arena, YR_CODE_SECTION, ref.offset, EOL);
if (argument_ref != NULL)
*argument_ref = ref;
return result;
}
int yr_parser_emit_pushes_for_strings(
yyscan_t yyscanner,
const char* identifier,
int* count)
{
YR_COMPILER* compiler = yyget_extra(yyscanner);
YR_RULE* current_rule = _yr_compiler_get_rule_by_idx(
compiler, compiler->current_rule_idx);
YR_STRING* string;
const char* string_identifier;
const char* target_identifier;
int matching = 0;
yr_rule_strings_foreach(current_rule, string)
{
// Don't generate pushes for strings chained to another one, we are
// only interested in non-chained strings or the head of the chain.
if (string->chained_to == NULL)
{
string_identifier = string->identifier;
target_identifier = identifier;
while (*target_identifier != '\0' && *string_identifier != '\0' &&
*target_identifier == *string_identifier)
{
target_identifier++;
string_identifier++;
}
if ((*target_identifier == '\0' && *string_identifier == '\0') ||
*target_identifier == '*')
{
yr_parser_emit_with_arg_reloc(yyscanner, OP_PUSH, string, NULL, NULL);
string->flags |= STRING_FLAGS_REFERENCED;
string->flags &= ~STRING_FLAGS_FIXED_OFFSET;
string->flags &= ~STRING_FLAGS_SINGLE_MATCH;
matching++;
}
}
}
if (count != NULL)
{
*count = matching;
}
if (matching == 0)
{
yr_compiler_set_error_extra_info(
compiler, identifier) return ERROR_UNDEFINED_STRING;
}
return ERROR_SUCCESS;
}
// Emit OP_PUSH_RULE instructions for all rules whose identifier has given
// prefix.
int yr_parser_emit_pushes_for_rules(
yyscan_t yyscanner,
const char* prefix,
int* count)
{
YR_COMPILER* compiler = yyget_extra(yyscanner);
// Make sure the compiler is parsing a rule
assert(compiler->current_rule_idx != UINT32_MAX);
YR_RULE* rule;
int matching = 0;
YR_NAMESPACE* ns = (YR_NAMESPACE*) yr_arena_get_ptr(
compiler->arena,
YR_NAMESPACES_TABLE,
compiler->current_namespace_idx * sizeof(struct YR_NAMESPACE));
// Can't use yr_rules_foreach here as that requires the rules to have been
// finalized (inserting a NULL rule at the end). This is done when
// yr_compiler_get_rules() is called, which also inserts a HALT instruction
// into the current position in the code arena. Obviously we aren't done
// compiling the rules yet so inserting a HALT is a bad idea. To deal with
// this I'm manually walking all the currently compiled rules (up to the
// current rule index) and comparing identifiers to see if it is one we should
// use.
//
// Further, we have to get compiler->current_rule_idx before we start because
// if we emit an OP_PUSH_RULE
rule = yr_arena_get_ptr(compiler->arena, YR_RULES_TABLE, 0);
for (uint32_t i = 0; i <= compiler->current_rule_idx; i++)
{
// Is rule->identifier prefixed by prefix?
if (strncmp(prefix, rule->identifier, strlen(prefix)) == 0)
{
uint32_t rule_idx = yr_hash_table_lookup_uint32(
compiler->rules_table, rule->identifier, ns->name);
if (rule_idx != UINT32_MAX)
{
FAIL_ON_ERROR(yr_parser_emit_with_arg(
yyscanner, OP_PUSH_RULE, rule_idx, NULL, NULL));
matching++;
}
}
rule++;
}
if (count != NULL)
{
*count = matching;
}
if (matching == 0)
{
yr_compiler_set_error_extra_info(compiler, prefix);
return ERROR_UNDEFINED_IDENTIFIER;
}
return ERROR_SUCCESS;
}
int yr_parser_emit_push_const(yyscan_t yyscanner, uint64_t argument)
{
uint8_t opcode[9];
int opcode_len = 1;
if (argument == YR_UNDEFINED)
{
opcode[0] = OP_PUSH_U;
}
else if (argument <= 0xff)
{
opcode[0] = OP_PUSH_8;
opcode[1] = (uint8_t) argument;
opcode_len += sizeof(uint8_t);
}
else if (argument <= 0xffff)
{
opcode[0] = OP_PUSH_16;
uint16_t u = (uint16_t) argument;
memcpy(opcode + 1, &u, sizeof(uint16_t));
opcode_len += sizeof(uint16_t);
}
else if (argument <= 0xffffffff)
{
opcode[0] = OP_PUSH_32;
uint32_t u = (uint32_t) argument;
memcpy(opcode + 1, &u, sizeof(uint32_t));
opcode_len += sizeof(uint32_t);
}
else
{
opcode[0] = OP_PUSH;
memcpy(opcode + 1, &argument, sizeof(uint64_t));
opcode_len += sizeof(uint64_t);
}
return yr_arena_write_data(
yyget_extra(yyscanner)->arena, YR_CODE_SECTION, opcode, opcode_len, NULL);
}
int yr_parser_check_types(
YR_COMPILER* compiler,
YR_OBJECT_FUNCTION* function,
const char* actual_args_fmt)
{
int i;
for (i = 0; i < YR_MAX_OVERLOADED_FUNCTIONS; i++)
{
if (function->prototypes[i].arguments_fmt == NULL)
break;
if (strcmp(function->prototypes[i].arguments_fmt, actual_args_fmt) == 0)
return ERROR_SUCCESS;
}
yr_compiler_set_error_extra_info(compiler, function->identifier)
return ERROR_WRONG_ARGUMENTS;
}
int yr_parser_lookup_string(
yyscan_t yyscanner,
const char* identifier,
YR_STRING** string)
{
YR_COMPILER* compiler = yyget_extra(yyscanner);
YR_RULE* current_rule = _yr_compiler_get_rule_by_idx(
compiler, compiler->current_rule_idx);
yr_rule_strings_foreach(current_rule, *string)
{
// If some string $a gets fragmented into multiple chained
// strings, all those fragments have the same $a identifier
// but we are interested in the heading fragment, which is
// that with chained_to == NULL
if ((*string)->chained_to == NULL &&
strcmp((*string)->identifier, identifier) == 0)
{
return ERROR_SUCCESS;
}
}
yr_compiler_set_error_extra_info(compiler, identifier)
* string = NULL;
return ERROR_UNDEFINED_STRING;
}
////////////////////////////////////////////////////////////////////////////////
// Searches for a variable with the given identifier in the scope of the current
// "for" loop. In case of nested "for" loops the identifier is searched starting
// at the top-level loop and going down thorough the nested loops until the
// current one. This is ok because inner loops can not re-define an identifier
// already defined by an outer loop.
//
// If the variable is found, the return value is the position that the variable
// occupies among all the currently defined variables. If the variable doesn't
// exist the return value is -1.
//
// The function can receive a pointer to a YR_EXPRESSION that will populated
// with information about the variable if found. This pointer can be NULL if
// the caller is not interested in getting that information.
//
int yr_parser_lookup_loop_variable(
yyscan_t yyscanner,
const char* identifier,
YR_EXPRESSION* expr)
{
YR_COMPILER* compiler = yyget_extra(yyscanner);
int i, j;
int var_offset = 0;
for (i = 0; i <= compiler->loop_index; i++)
{
var_offset += compiler->loop[i].vars_internal_count;
for (j = 0; j < compiler->loop[i].vars_count; j++)
{
if (compiler->loop[i].vars[j].identifier.ptr != NULL &&
strcmp(identifier, compiler->loop[i].vars[j].identifier.ptr) == 0)
{
if (expr != NULL)
*expr = compiler->loop[i].vars[j];
return var_offset + j;
}
}
var_offset += compiler->loop[i].vars_count;
}
return -1;
}
static int _yr_parser_write_string(
const char* identifier,
YR_MODIFIER modifier,
YR_COMPILER* compiler,
SIZED_STRING* str,
RE_AST* re_ast,
YR_ARENA_REF* string_ref,
int* min_atom_quality,
int* num_atom)
{
SIZED_STRING* literal_string;
YR_ATOM_LIST_ITEM* atom;
YR_ATOM_LIST_ITEM* atom_list = NULL;
int c, result;
int max_string_len;
bool free_literal = false;
FAIL_ON_ERROR(yr_arena_allocate_struct(
compiler->arena,
YR_STRINGS_TABLE,
sizeof(YR_STRING),
string_ref,
offsetof(YR_STRING, identifier),
offsetof(YR_STRING, string),
offsetof(YR_STRING, chained_to),
EOL));
YR_STRING* string = (YR_STRING*) yr_arena_ref_to_ptr(
compiler->arena, string_ref);
YR_ARENA_REF ref;
FAIL_ON_ERROR(_yr_compiler_store_string(compiler, identifier, &ref));
string->identifier = (const char*) yr_arena_ref_to_ptr(compiler->arena, &ref);
if (modifier.flags & STRING_FLAGS_HEXADECIMAL ||
modifier.flags & STRING_FLAGS_REGEXP ||
modifier.flags & STRING_FLAGS_BASE64 ||
modifier.flags & STRING_FLAGS_BASE64_WIDE)
{
literal_string = yr_re_ast_extract_literal(re_ast);
if (literal_string != NULL)
free_literal = true;
}
else
{
literal_string = str;
}
if (literal_string != NULL)
{
modifier.flags |= STRING_FLAGS_LITERAL;
result = _yr_compiler_store_data(
compiler,
literal_string->c_string,
literal_string->length + 1, // +1 to include terminating NULL
&ref);
string->length = (uint32_t) literal_string->length;
string->string = (uint8_t*) yr_arena_ref_to_ptr(compiler->arena, &ref);
if (result == ERROR_SUCCESS)
{
result = yr_atoms_extract_from_string(
&compiler->atoms_config,
(uint8_t*) literal_string->c_string,
(int32_t) literal_string->length,
modifier,
&atom_list,
min_atom_quality);
}
}
else
{
// Non-literal strings can't be marked as fixed offset because once we
// find a string atom in the scanned data we don't know the offset where
// the string should start, as the non-literal strings can contain
// variable-length portions.
modifier.flags &= ~STRING_FLAGS_FIXED_OFFSET;
// Emit forwards code
result = yr_re_ast_emit_code(re_ast, compiler->arena, false);
// Emit backwards code
if (result == ERROR_SUCCESS)
result = yr_re_ast_emit_code(re_ast, compiler->arena, true);
if (result == ERROR_SUCCESS)
result = yr_atoms_extract_from_re(
&compiler->atoms_config,
re_ast,
modifier,
&atom_list,
min_atom_quality);
}
string->flags = modifier.flags;
string->rule_idx = compiler->current_rule_idx;
string->idx = compiler->current_string_idx;
string->fixed_offset = YR_UNDEFINED;
if (result == ERROR_SUCCESS)
{
// Add the string to Aho-Corasick automaton.
result = yr_ac_add_string(
compiler->automaton,
string,
compiler->current_string_idx,
atom_list,
compiler->arena);
}
if (modifier.flags & STRING_FLAGS_LITERAL)
{
if (modifier.flags & STRING_FLAGS_WIDE)
max_string_len = string->length * 2;
else
max_string_len = string->length;
if (max_string_len <= YR_MAX_ATOM_LENGTH)
string->flags |= STRING_FLAGS_FITS_IN_ATOM;
}
atom = atom_list;
c = 0;
while (atom != NULL)
{
atom = atom->next;
c++;
}
(*num_atom) += c;
compiler->current_string_idx++;
if (free_literal)
yr_free(literal_string);
if (atom_list != NULL)
yr_atoms_list_destroy(atom_list);
return result;
}
static int _yr_parser_check_string_modifiers(
yyscan_t yyscanner,
YR_MODIFIER modifier)
{
YR_COMPILER* compiler = yyget_extra(yyscanner);
// xor and nocase together is not implemented.
if (modifier.flags & STRING_FLAGS_XOR &&
modifier.flags & STRING_FLAGS_NO_CASE)
{
yr_compiler_set_error_extra_info(
compiler, "invalid modifier combination: xor nocase");
return ERROR_INVALID_MODIFIER;
}
// base64 and nocase together is not implemented.
if (modifier.flags & STRING_FLAGS_NO_CASE &&
(modifier.flags & STRING_FLAGS_BASE64 ||
modifier.flags & STRING_FLAGS_BASE64_WIDE))
{
yr_compiler_set_error_extra_info(
compiler,
modifier.flags & STRING_FLAGS_BASE64
? "invalid modifier combination: base64 nocase"
: "invalid modifier combination: base64wide nocase");
return ERROR_INVALID_MODIFIER;
}
// base64 and fullword together is not implemented.
if (modifier.flags & STRING_FLAGS_FULL_WORD &&
(modifier.flags & STRING_FLAGS_BASE64 ||
modifier.flags & STRING_FLAGS_BASE64_WIDE))
{
yr_compiler_set_error_extra_info(
compiler,
modifier.flags & STRING_FLAGS_BASE64
? "invalid modifier combination: base64 fullword"
: "invalid modifier combination: base64wide fullword");
return ERROR_INVALID_MODIFIER;
}
// base64 and xor together is not implemented.
if (modifier.flags & STRING_FLAGS_XOR &&
(modifier.flags & STRING_FLAGS_BASE64 ||
modifier.flags & STRING_FLAGS_BASE64_WIDE))
{
yr_compiler_set_error_extra_info(
compiler,
modifier.flags & STRING_FLAGS_BASE64
? "invalid modifier combination: base64 xor"
: "invalid modifier combination: base64wide xor");
return ERROR_INVALID_MODIFIER;
}
return ERROR_SUCCESS;
}
int yr_parser_reduce_string_declaration(
yyscan_t yyscanner,
YR_MODIFIER modifier,
const char* identifier,
SIZED_STRING* str,
YR_ARENA_REF* string_ref)
{
int result = ERROR_SUCCESS;
int min_atom_quality = YR_MAX_ATOM_QUALITY;
int atom_quality;
char message[512];
int32_t min_gap = 0;
int32_t max_gap = 0;
YR_COMPILER* compiler = yyget_extra(yyscanner);
RE_AST* re_ast = NULL;
RE_AST* remainder_re_ast = NULL;
RE_ERROR re_error;
YR_RULE* current_rule = _yr_compiler_get_rule_by_idx(
compiler, compiler->current_rule_idx);
// Determine if a string with the same identifier was already defined
// by searching for the identifier in strings_table.
uint32_t string_idx = yr_hash_table_lookup_uint32(
compiler->strings_table, identifier, NULL);
// The string was already defined, return an error.
if (string_idx != UINT32_MAX)
{
yr_compiler_set_error_extra_info(compiler, identifier);
return ERROR_DUPLICATED_STRING_IDENTIFIER;
}
// Empty strings are not allowed.
if (str->length == 0)
{
yr_compiler_set_error_extra_info(compiler, identifier);
return ERROR_EMPTY_STRING;
}
if (str->flags & SIZED_STRING_FLAGS_NO_CASE)
modifier.flags |= STRING_FLAGS_NO_CASE;
if (str->flags & SIZED_STRING_FLAGS_DOT_ALL)
modifier.flags |= STRING_FLAGS_DOT_ALL;
// Hex strings are always handled as DOT_ALL regexps.
if (modifier.flags & STRING_FLAGS_HEXADECIMAL)
modifier.flags |= STRING_FLAGS_DOT_ALL;
if (!(modifier.flags & STRING_FLAGS_WIDE) &&
!(modifier.flags & STRING_FLAGS_BASE64 ||
modifier.flags & STRING_FLAGS_BASE64_WIDE))
{
modifier.flags |= STRING_FLAGS_ASCII;
}
// The STRING_FLAGS_SINGLE_MATCH flag indicates that finding
// a single match for the string is enough. This is true in
// most cases, except when the string count (#) and string offset (@)
// operators are used. All strings are marked STRING_FLAGS_SINGLE_MATCH
// initially, and unmarked later if required.
modifier.flags |= STRING_FLAGS_SINGLE_MATCH;
// The STRING_FLAGS_FIXED_OFFSET indicates that the string doesn't
// need to be searched all over the file because the user is using the
// "at" operator. The string must be searched at a fixed offset in the
// file. All strings are marked STRING_FLAGS_FIXED_OFFSET initially,
// and unmarked later if required.
modifier.flags |= STRING_FLAGS_FIXED_OFFSET;
// If string identifier is $ this is an anonymous string, if not add the
// identifier to strings_table.
if (strcmp(identifier, "$") == 0)
{
modifier.flags |= STRING_FLAGS_ANONYMOUS;
}
else
{
FAIL_ON_ERROR(yr_hash_table_add_uint32(
compiler->strings_table,
identifier,
NULL,
compiler->current_string_idx));
}
// Make sure that the the string does not have an invalid combination of
// modifiers.
FAIL_ON_ERROR(_yr_parser_check_string_modifiers(yyscanner, modifier));
if (modifier.flags & STRING_FLAGS_HEXADECIMAL ||
modifier.flags & STRING_FLAGS_REGEXP ||
modifier.flags & STRING_FLAGS_BASE64 ||
modifier.flags & STRING_FLAGS_BASE64_WIDE)
{
if (modifier.flags & STRING_FLAGS_HEXADECIMAL)
result = yr_re_parse_hex(str->c_string, &re_ast, &re_error);
else if (modifier.flags & STRING_FLAGS_REGEXP)
{
int flags = RE_PARSER_FLAG_NONE;
if (compiler->strict_escape)
flags |= RE_PARSER_FLAG_ENABLE_STRICT_ESCAPE_SEQUENCES;
result = yr_re_parse(str->c_string, &re_ast, &re_error, flags);
}
else
result = yr_base64_ast_from_string(str, modifier, &re_ast, &re_error);
if (result != ERROR_SUCCESS)
{
if (result == ERROR_UNKNOWN_ESCAPE_SEQUENCE)
{
yywarning(
yyscanner,
"unknown escape sequence");
}
else
{
snprintf(
message,
sizeof(message),
"invalid %s \"%s\": %s",
(modifier.flags & STRING_FLAGS_HEXADECIMAL) ? "hex string"
: "regular expression",
identifier,
re_error.message);
yr_compiler_set_error_extra_info(compiler, message);
goto _exit;
}
}
if (re_ast->flags & RE_FLAGS_FAST_REGEXP)
modifier.flags |= STRING_FLAGS_FAST_REGEXP;
if (re_ast->flags & RE_FLAGS_GREEDY)
modifier.flags |= STRING_FLAGS_GREEDY_REGEXP;
// Regular expressions in the strings section can't mix greedy and
// ungreedy quantifiers like .* and .*?. That's because these regular
// expressions can be matched forwards and/or backwards depending on the
// atom found, and we need the regexp to be all-greedy or all-ungreedy to
// be able to properly calculate the length of the match.
if ((re_ast->flags & RE_FLAGS_GREEDY) &&
(re_ast->flags & RE_FLAGS_UNGREEDY))
{
result = ERROR_INVALID_REGULAR_EXPRESSION;
yr_compiler_set_error_extra_info(
compiler,
"greedy and ungreedy quantifiers can't be mixed in a regular "
"expression");
goto _exit;
}
if (yr_re_ast_has_unbounded_quantifier_for_dot(re_ast))
{
yywarning(
yyscanner,
"%s contains .*, .+ or .{x,} consider using .{,N}, .{1,N} or {x,N} "
"with a reasonable value for N",
identifier);
}
if (compiler->re_ast_callback != NULL)
{
compiler->re_ast_callback(
current_rule, identifier, re_ast, compiler->re_ast_clbk_user_data);
}
*string_ref = YR_ARENA_NULL_REF;
while (re_ast != NULL)
{
YR_ARENA_REF ref;
uint32_t prev_string_idx = compiler->current_string_idx - 1;
int32_t prev_min_gap = min_gap;
int32_t prev_max_gap = max_gap;
result = yr_re_ast_split_at_chaining_point(
re_ast, &remainder_re_ast, &min_gap, &max_gap);
if (result != ERROR_SUCCESS)
goto _exit;
result = _yr_parser_write_string(
identifier,
modifier,
compiler,
NULL,
re_ast,
&ref,
&atom_quality,
¤t_rule->num_atoms);
if (result != ERROR_SUCCESS)
goto _exit;
if (atom_quality < min_atom_quality)
min_atom_quality = atom_quality;
if (YR_ARENA_IS_NULL_REF(*string_ref))
{
// This is the first string in the chain, the string reference
// returned by this function must point to this string.
*string_ref = ref;
}
else
{
// This is not the first string in the chain, set the appropriate
// flags and fill the chained_to, chain_gap_min and chain_gap_max
// fields.
YR_STRING* prev_string = (YR_STRING*) yr_arena_get_ptr(
compiler->arena,
YR_STRINGS_TABLE,
prev_string_idx * sizeof(YR_STRING));
YR_STRING* new_string = (YR_STRING*) yr_arena_ref_to_ptr(
compiler->arena, &ref);
new_string->chained_to = prev_string;
new_string->chain_gap_min = prev_min_gap;
new_string->chain_gap_max = prev_max_gap;
// A string chained to another one can't have a fixed offset, only the
// head of the string chain can have a fixed offset.
new_string->flags &= ~STRING_FLAGS_FIXED_OFFSET;
// There is a previous string, but that string wasn't marked as part
// of a chain because we can't do that until knowing there will be
// another string, let's flag it now the we know.
prev_string->flags |= STRING_FLAGS_CHAIN_PART;
// There is a previous string, so this string is part of a chain, but
// there will be no more strings because there are no more AST to
// split, which means that this is the chain's tail.
if (remainder_re_ast == NULL)
new_string->flags |= STRING_FLAGS_CHAIN_PART |
STRING_FLAGS_CHAIN_TAIL;
}
yr_re_ast_destroy(re_ast);
re_ast = remainder_re_ast;
}
}
else // not a STRING_FLAGS_HEXADECIMAL or STRING_FLAGS_REGEXP or
// STRING_FLAGS_BASE64 or STRING_FLAGS_BASE64_WIDE
{
result = _yr_parser_write_string(
identifier,
modifier,
compiler,
str,
NULL,
string_ref,
&min_atom_quality,
¤t_rule->num_atoms);
if (result != ERROR_SUCCESS)
goto _exit;
}
if (min_atom_quality < compiler->atoms_config.quality_warning_threshold)
{
yywarning(yyscanner, "string \"%s\" may slow down scanning", identifier);
}
_exit:
if (re_ast != NULL)
yr_re_ast_destroy(re_ast);
if (remainder_re_ast != NULL)
yr_re_ast_destroy(remainder_re_ast);
return result;
}
static int wildcard_iterator(
void* prefix,
size_t prefix_len,
void* _value,
void* data)
{
const char* identifier = (const char*) data;
// If the identifier is prefixed by prefix, then it matches the wildcard.
if (!strncmp(prefix, identifier, prefix_len))
return ERROR_IDENTIFIER_MATCHES_WILDCARD;
return ERROR_SUCCESS;
}
int yr_parser_reduce_rule_declaration_phase_1(
yyscan_t yyscanner,
int32_t flags,
const char* identifier,
YR_ARENA_REF* rule_ref)
{
int result;
YR_FIXUP* fixup;
YR_COMPILER* compiler = yyget_extra(yyscanner);
YR_NAMESPACE* ns = (YR_NAMESPACE*) yr_arena_get_ptr(
compiler->arena,
YR_NAMESPACES_TABLE,
compiler->current_namespace_idx * sizeof(struct YR_NAMESPACE));
if (yr_hash_table_lookup_uint32(
compiler->rules_table, identifier, ns->name) != UINT32_MAX ||
yr_hash_table_lookup(compiler->objects_table, identifier, NULL) != NULL)
{
// A rule or variable with the same identifier already exists, return the
// appropriate error.
yr_compiler_set_error_extra_info(compiler, identifier);
return ERROR_DUPLICATED_IDENTIFIER;
}
// Iterate over all identifiers in wildcard_identifiers_table, and check if
// any of them are a prefix of the identifier being declared. If so, return
// ERROR_IDENTIFIER_MATCHES_WILDCARD.
result = yr_hash_table_iterate(
compiler->wildcard_identifiers_table,
ns->name,
wildcard_iterator,
(void*) identifier);
if (result == ERROR_IDENTIFIER_MATCHES_WILDCARD)
{
// This rule matches an existing wildcard rule set.
yr_compiler_set_error_extra_info(compiler, identifier);
}
FAIL_ON_ERROR(result);
FAIL_ON_ERROR(yr_arena_allocate_struct(
compiler->arena,
YR_RULES_TABLE,
sizeof(YR_RULE),
rule_ref,
offsetof(YR_RULE, identifier),
offsetof(YR_RULE, tags),
offsetof(YR_RULE, strings),
offsetof(YR_RULE, metas),