forked from VirusTotal/yara
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgrammar.y
2846 lines (2338 loc) · 81.8 KB
/
grammar.y
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) 2007-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.
*/
// clang-format off
%{
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <stdlib.h>
#include <stddef.h>
#include <yara/arena.h>
#include <yara/integers.h>
#include <yara/utils.h>
#include <yara/strutils.h>
#include <yara/compiler.h>
#include <yara/object.h>
#include <yara/sizedstr.h>
#include <yara/exec.h>
#include <yara/error.h>
#include <yara/mem.h>
#include <yara/lexer.h>
#include <yara/parser.h>
#if defined(_MSC_VER)
#define llabs _abs64
#endif
#define YYERROR_VERBOSE
#define YYMALLOC yr_malloc
#define YYFREE yr_free
#define FOR_EXPRESSION_ALL 1
#define FOR_EXPRESSION_ANY 2
#define FOR_EXPRESSION_NONE 3
#define FOR_ITERATION_ITERATOR 1
#define FOR_ITERATION_STRING_SET 2
// fail_with_error() is used in parser actions for aborting the parsing with
// an error. If the error is recoverable (like syntax errors), the parser will
// report the error and continue parsing the next rule. If the error is a
// fatal, non-recoverable error, the parser will be completely aborted.
#define fail_with_error(e) \
{ \
compiler->last_error = e; \
yyerror(yyscanner, compiler, NULL); \
switch (e) \
{ \
case ERROR_INSUFFICIENT_MEMORY: \
YYABORT; \
default: \
YYERROR; \
} \
}
// fail_if_error() is used in parser actions for aborting the parsing if an
// error has occurred. See fail_with_error for details.
#define fail_if_error(e) \
if (e != ERROR_SUCCESS) \
{ \
fail_with_error(e); \
}
// check_type(expression, EXPRESSION_TYPE_INTEGER | EXPRESSION_TYPE_FLOAT) is
// used to ensure that the type of "expression" is either integer or float,
// the cleanup statements are executed if the condition is not met.
#define check_type_with_cleanup(expression, expected_type, op, cleanup) \
if (((expression.type) & (expected_type)) == 0) \
{ \
switch(expression.type) \
{ \
case EXPRESSION_TYPE_INTEGER: \
yr_compiler_set_error_extra_info( \
compiler, "wrong type \"integer\" for " op " operator"); \
break; \
case EXPRESSION_TYPE_FLOAT: \
yr_compiler_set_error_extra_info( \
compiler, "wrong type \"float\" for " op " operator"); \
break; \
case EXPRESSION_TYPE_STRING: \
yr_compiler_set_error_extra_info( \
compiler, "wrong type \"string\" for " op " operator"); \
break; \
case EXPRESSION_TYPE_BOOLEAN: \
yr_compiler_set_error_extra_info( \
compiler, "wrong type \"boolean\" for " op " operator"); \
break; \
} \
cleanup; \
compiler->last_error = ERROR_WRONG_TYPE; \
yyerror(yyscanner, compiler, NULL); \
YYERROR; \
}
// check_type(expression, EXPRESSION_TYPE_INTEGER | EXPRESSION_TYPE_FLOAT) is
// used to ensure that the type of "expression" is either integer or float.
#define check_type(expression, expected_type, op) \
check_type_with_cleanup(expression, expected_type, op, )
#define loop_vars_cleanup(loop_index) \
{ \
YR_LOOP_CONTEXT* loop_ctx = &compiler->loop[loop_index]; \
for (int i = 0; i < loop_ctx->vars_count; i++) \
{ \
yr_free((void*) loop_ctx->vars[i].identifier.ptr); \
loop_ctx->vars[i].identifier.ptr = NULL; \
loop_ctx->vars[i].identifier.ref = YR_ARENA_NULL_REF; \
} \
loop_ctx->vars_count = 0; \
} \
// Given a YR_EXPRESSION returns its identifier. It returns identifier.ptr if
// not NULL and relies on identifier.ref if otherwise.
#define expression_identifier(expr) \
((expr).identifier.ptr != NULL ? \
(expr).identifier.ptr : \
(const char*) yr_arena_ref_to_ptr(compiler->arena, &(expr).identifier.ref))
#define DEFAULT_BASE64_ALPHABET \
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
%}
%expect 1 // expect 1 shift/reduce conflicts
// Uncomment this line to print parsing information that can be useful to
// debug YARA's grammar.
// %debug
%name-prefix "yara_yy"
%pure-parser
%parse-param {void *yyscanner}
%parse-param {YR_COMPILER* compiler}
%lex-param {yyscan_t yyscanner}
%lex-param {YR_COMPILER* compiler}
// The parser produces more detailed syntax errors. Accepted values are
// "simple", "verbose", "detailed" and "custom". Introduced in Bison 3.0
// with support for "simple" and "verbose". Values "custom" and "detailed"
// were introduced in Bison 3.6. See:
// https://www.gnu.org/software/bison/manual/html_node/_0025define-Summary.html
%define parse.error verbose
// Token that marks the end of the original file.
%token _END_OF_FILE_ 0 "end of file"
// Token that marks the end of included files, we can't use _END_OF_FILE_
// because bison stops parsing when it sees _END_OF_FILE_, we want to be
// be able to identify the point where an included file ends, but continuing
// parsing any content that follows.
%token _END_OF_INCLUDED_FILE_ "end of included file"
%token _DOT_DOT_ ".."
%token _RULE_ "<rule>"
%token _PRIVATE_ "<private>"
%token _GLOBAL_ "<global>"
%token _META_ "<meta>"
%token <string> _STRINGS_ "<strings>"
%token _CONDITION_ "<condition>"
%token <c_string> _IDENTIFIER_ "identifier"
%token <c_string> _STRING_IDENTIFIER_ "string identifier"
%token <c_string> _STRING_COUNT_ "string count"
%token <c_string> _STRING_OFFSET_ "string offset"
%token <c_string> _STRING_LENGTH_ "string length"
%token <c_string> _STRING_IDENTIFIER_WITH_WILDCARD_
"string identifier with wildcard"
%token <integer> _NUMBER_ "integer number"
%token <double_> _DOUBLE_ "floating point number"
%token <integer> _INTEGER_FUNCTION_ "integer function"
%token <sized_string> _TEXT_STRING_ "text string"
%token <sized_string> _HEX_STRING_ "hex string"
%token <sized_string> _REGEXP_ "regular expression"
%token _ASCII_ "<ascii>"
%token _WIDE_ "<wide>"
%token _XOR_ "<xor>"
%token _BASE64_ "<base64>"
%token _BASE64_WIDE_ "<base64wide>"
%token _NOCASE_ "<nocase>"
%token _FULLWORD_ "<fullword>"
%token _AT_ "<at>"
%token _FILESIZE_ "<filesize>"
%token _ENTRYPOINT_ "<entrypoint>"
%token _ALL_ "<all>"
%token _ANY_ "<any>"
%token _NONE_ "<none>"
%token _IN_ "<in>"
%token _OF_ "<of>"
%token _FOR_ "<for>"
%token _THEM_ "<them>"
%token _MATCHES_ "<matches>"
%token _CONTAINS_ "<contains>"
%token _STARTSWITH_ "<startswith>"
%token _ENDSWITH_ "<endswith>"
%token _ICONTAINS_ "<icontains>"
%token _ISTARTSWITH_ "<istartswith>"
%token _IENDSWITH_ "<iendswith>"
%token _IEQUALS_ "<iequals>"
%token _IMPORT_ "<import>"
%token _TRUE_ "<true>"
%token _FALSE_ "<false>"
%token _OR_ "<or>"
%token _AND_ "<and>"
%token _NOT_ "<not>"
%token _DEFINED_ "<defined>"
%token _EQ_ "=="
%token _NEQ_ "!="
%token _LT_ "<"
%token _LE_ "<="
%token _GT_ ">"
%token _GE_ ">="
%token _SHIFT_LEFT_ "<<"
%token _SHIFT_RIGHT_ ">>"
// Operator precedence and associativity. Higher precedence operators are lower
// in the list. Operators that appear in the same line have the same precedence.
%left _OR_
%left _AND_
%right _NOT_ _DEFINED_
%left _EQ_ _NEQ_ _CONTAINS_ _ICONTAINS_ _STARTSWITH_ _ENDSWITH_ _ISTARTSWITH_ _IENDSWITH_ _IEQUALS_ _MATCHES_
%left _LT_ _LE_ _GT_ _GE_
%left '|'
%left '^'
%left '&'
%left _SHIFT_LEFT_ _SHIFT_RIGHT_
%left '+' '-'
%left '*' '\\' '%'
%right '~' UNARY_MINUS
%type <rule> rule
%type <string> strings
%type <string> string_declaration
%type <string> string_declarations
%type <meta> meta
%type <meta> meta_declaration
%type <meta> meta_declarations
%type <tag> tags
%type <tag> tag_list
%type <modifier> string_modifier
%type <modifier> string_modifiers
%type <modifier> regexp_modifier
%type <modifier> regexp_modifiers
%type <modifier> hex_modifier
%type <modifier> hex_modifiers
%type <integer> integer_set
%type <integer> integer_enumeration
%type <integer> rule_modifier
%type <integer> rule_modifiers
%type <integer> string_enumeration
%type <integer> string_enumeration_item
%type <integer> string_set
%type <integer> for_iteration
%type <integer> rule_enumeration
%type <integer> rule_enumeration_item
%type <integer> rule_set
%type <expression> primary_expression
%type <expression> boolean_expression
%type <expression> expression
%type <expression> identifier
%type <expression> regexp
%type <expression> for_expression
%type <expression> for_quantifier
%type <c_string> arguments
%type <c_string> arguments_list
%destructor { yr_free($$); $$ = NULL; } _IDENTIFIER_
%destructor { yr_free($$); $$ = NULL; } _STRING_COUNT_
%destructor { yr_free($$); $$ = NULL; } _STRING_OFFSET_
%destructor { yr_free($$); $$ = NULL; } _STRING_LENGTH_
%destructor { yr_free($$); $$ = NULL; } _STRING_IDENTIFIER_
%destructor { yr_free($$); $$ = NULL; } _STRING_IDENTIFIER_WITH_WILDCARD_
%destructor { yr_free($$); $$ = NULL; } _TEXT_STRING_
%destructor { yr_free($$); $$ = NULL; } _HEX_STRING_
%destructor { yr_free($$); $$ = NULL; } _REGEXP_
%destructor { yr_free($$); $$ = NULL; } arguments
%destructor { yr_free($$); $$ = NULL; } arguments_list
%destructor {
if ($$.alphabet != NULL)
{
yr_free($$.alphabet);
$$.alphabet = NULL;
}
} string_modifier
%destructor {
if ($$.alphabet != NULL)
{
yr_free($$.alphabet);
$$.alphabet = NULL;
}
} string_modifiers
%union {
YR_EXPRESSION expression;
SIZED_STRING* sized_string;
char* c_string;
int64_t integer;
double double_;
YR_MODIFIER modifier;
YR_ARENA_REF tag;
YR_ARENA_REF rule;
YR_ARENA_REF meta;
YR_ARENA_REF string;
}
%%
rules
: /* empty */
| rules rule
| rules import
| rules error rule /* on error skip until next rule..*/
| rules error import /* .. or import statement */
| rules error "include" /* .. or include statement */
| rules _END_OF_INCLUDED_FILE_
{
_yr_compiler_pop_file_name(compiler);
}
;
import
: _IMPORT_ _TEXT_STRING_
{
int result = yr_parser_reduce_import(yyscanner, $2);
yr_free($2);
fail_if_error(result);
}
;
rule
: rule_modifiers _RULE_ _IDENTIFIER_
{
fail_if_error(yr_parser_reduce_rule_declaration_phase_1(
yyscanner, (int32_t) $1, $3, &$<rule>$));
}
tags '{' meta strings
{
YR_RULE* rule = (YR_RULE*) yr_arena_ref_to_ptr(
compiler->arena, &$<rule>4);
rule->tags = (char*) yr_arena_ref_to_ptr(
compiler->arena, &$5);
rule->metas = (YR_META*) yr_arena_ref_to_ptr(
compiler->arena, &$7);
rule->strings = (YR_STRING*) yr_arena_ref_to_ptr(
compiler->arena, &$8);
}
condition '}'
{
int result = yr_parser_reduce_rule_declaration_phase_2(
yyscanner, &$<rule>4); // rule created in phase 1
yr_free($3);
fail_if_error(result);
}
;
meta
: /* empty */
{
$$ = YR_ARENA_NULL_REF;
}
| _META_ ':' meta_declarations
{
YR_META* meta = yr_arena_get_ptr(
compiler->arena,
YR_METAS_TABLE,
(compiler->current_meta_idx - 1) * sizeof(YR_META));
meta->flags |= META_FLAGS_LAST_IN_RULE;
$$ = $3;
}
;
strings
: /* empty */
{
$$ = YR_ARENA_NULL_REF;
}
| _STRINGS_ ':' string_declarations
{
YR_STRING* string = (YR_STRING*) yr_arena_get_ptr(
compiler->arena,
YR_STRINGS_TABLE,
(compiler->current_string_idx - 1) * sizeof(YR_STRING));
string->flags |= STRING_FLAGS_LAST_IN_RULE;
$$ = $3;
}
;
condition
: _CONDITION_ ':' boolean_expression
;
rule_modifiers
: /* empty */ { $$ = 0; }
| rule_modifiers rule_modifier { $$ = $1 | $2; }
;
rule_modifier
: _PRIVATE_ { $$ = RULE_FLAGS_PRIVATE; }
| _GLOBAL_ { $$ = RULE_FLAGS_GLOBAL; }
;
tags
: /* empty */
{
$$ = YR_ARENA_NULL_REF;
}
| ':' tag_list
{
// Tags list is represented in the arena as a sequence
// of null-terminated strings, the sequence ends with an
// additional null character. Here we write the ending null
//character. Example: tag1\0tag2\0tag3\0\0
fail_if_error(yr_arena_write_string(
yyget_extra(yyscanner)->arena, YR_SZ_POOL, "", NULL));
$$ = $2;
}
;
tag_list
: _IDENTIFIER_
{
int result = yr_arena_write_string(
yyget_extra(yyscanner)->arena, YR_SZ_POOL, $1, &$<tag>$);
yr_free($1);
fail_if_error(result);
}
| tag_list _IDENTIFIER_
{
YR_ARENA_REF ref;
// Write the new tag identifier.
int result = yr_arena_write_string(
yyget_extra(yyscanner)->arena, YR_SZ_POOL, $2, &ref);
yr_free($2);
fail_if_error(result);
// Get the address for the tag identifier just written.
char* new_tag = (char*) yr_arena_ref_to_ptr(
compiler->arena, &ref);
// Take the address of first tag's identifier in the list.
char* tag = (char*) yr_arena_ref_to_ptr(
compiler->arena, &$<tag>$);
// Search for duplicated tags. Tags are written one after
// the other, with zeroes in between (i.e: tag1/0tag2/0tag3)
// that's why can use tag < new_tag as the condition for the
// loop.
while (tag < new_tag)
{
if (strcmp(tag, new_tag) == 0)
{
yr_compiler_set_error_extra_info(compiler, tag);
fail_with_error(ERROR_DUPLICATED_TAG_IDENTIFIER);
}
tag += strlen(tag) + 1;
}
$$ = $1;
}
;
meta_declarations
: meta_declaration { $$ = $1; }
| meta_declarations meta_declaration { $$ = $1; }
;
meta_declaration
: _IDENTIFIER_ '=' _TEXT_STRING_
{
SIZED_STRING* sized_string = $3;
int result = yr_parser_reduce_meta_declaration(
yyscanner,
META_TYPE_STRING,
$1,
sized_string->c_string,
0,
&$<meta>$);
yr_free($1);
yr_free($3);
fail_if_error(result);
}
| _IDENTIFIER_ '=' _NUMBER_
{
int result = yr_parser_reduce_meta_declaration(
yyscanner,
META_TYPE_INTEGER,
$1,
NULL,
$3,
&$<meta>$);
yr_free($1);
fail_if_error(result);
}
| _IDENTIFIER_ '=' '-' _NUMBER_
{
int result = yr_parser_reduce_meta_declaration(
yyscanner,
META_TYPE_INTEGER,
$1,
NULL,
-$4,
&$<meta>$);
yr_free($1);
fail_if_error(result);
}
| _IDENTIFIER_ '=' _TRUE_
{
int result = yr_parser_reduce_meta_declaration(
yyscanner,
META_TYPE_BOOLEAN,
$1,
NULL,
true,
&$<meta>$);
yr_free($1);
fail_if_error(result);
}
| _IDENTIFIER_ '=' _FALSE_
{
int result = yr_parser_reduce_meta_declaration(
yyscanner,
META_TYPE_BOOLEAN,
$1,
NULL,
false,
&$<meta>$);
yr_free($1);
fail_if_error(result);
}
;
string_declarations
: string_declaration { $$ = $1; }
| string_declarations string_declaration { $$ = $1; }
;
string_declaration
: _STRING_IDENTIFIER_ '='
{
compiler->current_line = yyget_lineno(yyscanner);
}
_TEXT_STRING_ string_modifiers
{
int result = yr_parser_reduce_string_declaration(
yyscanner, $5, $1, $4, &$<string>$);
yr_free($1);
yr_free($4);
yr_free($5.alphabet);
fail_if_error(result);
compiler->current_line = 0;
}
| _STRING_IDENTIFIER_ '='
{
compiler->current_line = yyget_lineno(yyscanner);
}
_REGEXP_ regexp_modifiers
{
int result;
$5.flags |= STRING_FLAGS_REGEXP;
result = yr_parser_reduce_string_declaration(
yyscanner, $5, $1, $4, &$<string>$);
yr_free($1);
yr_free($4);
fail_if_error(result);
compiler->current_line = 0;
}
| _STRING_IDENTIFIER_ '='
{
compiler->current_line = yyget_lineno(yyscanner);
}
_HEX_STRING_ hex_modifiers
{
int result;
$5.flags |= STRING_FLAGS_HEXADECIMAL;
result = yr_parser_reduce_string_declaration(
yyscanner, $5, $1, $4, &$<string>$);
yr_free($1);
yr_free($4);
fail_if_error(result);
compiler->current_line = 0;
}
;
string_modifiers
: /* empty */
{
$$.flags = 0;
$$.xor_min = 0;
$$.xor_max = 0;
$$.alphabet = NULL;
}
| string_modifiers string_modifier
{
$$ = $1;
// Only set the xor minimum and maximum if we are dealing with the
// xor modifier. If we don't check for this then we can end up with
// "xor wide" resulting in whatever is on the stack for "wide"
// overwriting the values for xor.
if ($2.flags & STRING_FLAGS_XOR)
{
$$.xor_min = $2.xor_min;
$$.xor_max = $2.xor_max;
}
// Only set the base64 alphabet if we are dealing with the base64
// modifier. If we don't check for this then we can end up with
// "base64 ascii" resulting in whatever is on the stack for "ascii"
// overwriting the values for base64.
if (($2.flags & STRING_FLAGS_BASE64) ||
($2.flags & STRING_FLAGS_BASE64_WIDE))
{
if ($$.alphabet != NULL)
{
if (ss_compare($$.alphabet, $2.alphabet) != 0)
{
yr_compiler_set_error_extra_info(
compiler, "can not specify multiple alphabets");
yr_free($2.alphabet);
yr_free($$.alphabet);
fail_with_error(ERROR_INVALID_MODIFIER);
}
else
{
yr_free($2.alphabet);
}
}
else
{
$$.alphabet = $2.alphabet;
}
}
if ($$.flags & $2.flags)
{
if ($$.alphabet != NULL)
yr_free($$.alphabet);
fail_with_error(ERROR_DUPLICATED_MODIFIER);
}
else
{
$$.flags = $$.flags | $2.flags;
}
}
;
string_modifier
: _WIDE_ { $$.flags = STRING_FLAGS_WIDE; }
| _ASCII_ { $$.flags = STRING_FLAGS_ASCII; }
| _NOCASE_ { $$.flags = STRING_FLAGS_NO_CASE; }
| _FULLWORD_ { $$.flags = STRING_FLAGS_FULL_WORD; }
| _PRIVATE_ { $$.flags = STRING_FLAGS_PRIVATE; }
| _XOR_
{
$$.flags = STRING_FLAGS_XOR;
$$.xor_min = 0;
$$.xor_max = 255;
}
| _XOR_ '(' _NUMBER_ ')'
{
int result = ERROR_SUCCESS;
if ($3 < 0 || $3 > 255)
{
yr_compiler_set_error_extra_info(compiler, "invalid xor range");
result = ERROR_INVALID_MODIFIER;
}
fail_if_error(result);
$$.flags = STRING_FLAGS_XOR;
$$.xor_min = (uint8_t) $3;
$$.xor_max = (uint8_t) $3;
}
/*
* Would love to use range here for consistency in the language but that
* uses a primary expression which pushes a value on the VM stack we don't
* account for.
*/
| _XOR_ '(' _NUMBER_ '-' _NUMBER_ ')'
{
int result = ERROR_SUCCESS;
if ($3 < 0)
{
yr_compiler_set_error_extra_info(
compiler, "lower bound for xor range exceeded (min: 0)");
result = ERROR_INVALID_MODIFIER;
}
if ($5 > 255)
{
yr_compiler_set_error_extra_info(
compiler, "upper bound for xor range exceeded (max: 255)");
result = ERROR_INVALID_MODIFIER;
}
if ($3 > $5)
{
yr_compiler_set_error_extra_info(
compiler, "xor lower bound exceeds upper bound");
result = ERROR_INVALID_MODIFIER;
}
fail_if_error(result);
$$.flags = STRING_FLAGS_XOR;
$$.xor_min = (uint8_t) $3;
$$.xor_max = (uint8_t) $5;
}
| _BASE64_
{
$$.flags = STRING_FLAGS_BASE64;
$$.alphabet = ss_new(DEFAULT_BASE64_ALPHABET);
}
| _BASE64_ '(' _TEXT_STRING_ ')'
{
int result = ERROR_SUCCESS;
if ($3->length != 64)
{
yr_free($3);
yr_compiler_set_error_extra_info(
compiler, "length of base64 alphabet must be 64");
result = ERROR_INVALID_MODIFIER;
}
fail_if_error(result);
$$.flags = STRING_FLAGS_BASE64;
$$.alphabet = $3;
}
| _BASE64_WIDE_
{
$$.flags = STRING_FLAGS_BASE64_WIDE;
$$.alphabet = ss_new(DEFAULT_BASE64_ALPHABET);
}
| _BASE64_WIDE_ '(' _TEXT_STRING_ ')'
{
int result = ERROR_SUCCESS;
if ($3->length != 64)
{
yr_free($3);
yr_compiler_set_error_extra_info(
compiler, "length of base64 alphabet must be 64");
result = ERROR_INVALID_MODIFIER;
}
fail_if_error(result);
$$.flags = STRING_FLAGS_BASE64_WIDE;
$$.alphabet = $3;
}
;
regexp_modifiers
: /* empty */ { $$.flags = 0; }
| regexp_modifiers regexp_modifier
{
if ($1.flags & $2.flags)
{
fail_with_error(ERROR_DUPLICATED_MODIFIER);
}
else
{
$$.flags = $1.flags | $2.flags;
}
}
;
regexp_modifier
: _WIDE_ { $$.flags = STRING_FLAGS_WIDE; }
| _ASCII_ { $$.flags = STRING_FLAGS_ASCII; }
| _NOCASE_ { $$.flags = STRING_FLAGS_NO_CASE; }
| _FULLWORD_ { $$.flags = STRING_FLAGS_FULL_WORD; }
| _PRIVATE_ { $$.flags = STRING_FLAGS_PRIVATE; }
;
hex_modifiers
: /* empty */ { $$.flags = 0; }
| hex_modifiers hex_modifier
{
if ($1.flags & $2.flags)
{
fail_with_error(ERROR_DUPLICATED_MODIFIER);
}
else
{
$$.flags = $1.flags | $2.flags;
}
}
;
hex_modifier
: _PRIVATE_ { $$.flags = STRING_FLAGS_PRIVATE; }
;
identifier
: _IDENTIFIER_
{
YR_EXPRESSION expr;
int result = ERROR_SUCCESS;
int var_index = yr_parser_lookup_loop_variable(yyscanner, $1, &expr);
if (var_index >= 0)
{
// The identifier corresponds to a loop variable.
result = yr_parser_emit_with_arg(
yyscanner,
OP_PUSH_M,
var_index,
NULL,
NULL);
// The expression associated to this identifier is the same one
// associated to the loop variable.
$$ = expr;
}
else
{
// Search for identifier within the global namespace, where the
// externals variables reside.
YR_OBJECT* object = (YR_OBJECT*) yr_hash_table_lookup(
compiler->objects_table, $1, NULL);
YR_NAMESPACE* ns = (YR_NAMESPACE*) yr_arena_get_ptr(
compiler->arena,
YR_NAMESPACES_TABLE,
compiler->current_namespace_idx * sizeof(struct YR_NAMESPACE));
if (object == NULL)
{
// If not found, search within the current namespace.
object = (YR_OBJECT*) yr_hash_table_lookup(
compiler->objects_table, $1, ns->name);
}
if (object != NULL)
{
YR_ARENA_REF ref;
result = _yr_compiler_store_string(
compiler, $1, &ref);
if (result == ERROR_SUCCESS)
result = yr_parser_emit_with_arg_reloc(
yyscanner,
OP_OBJ_LOAD,
yr_arena_ref_to_ptr(compiler->arena, &ref),
NULL,
NULL);
$$.type = EXPRESSION_TYPE_OBJECT;
$$.value.object = object;
$$.identifier.ptr = NULL;
$$.identifier.ref = ref;
}
else
{
uint32_t rule_idx = yr_hash_table_lookup_uint32(
compiler->rules_table, $1, ns->name);
if (rule_idx != UINT32_MAX)
{
result = yr_parser_emit_with_arg(
yyscanner,
OP_PUSH_RULE,
rule_idx,
NULL,
NULL);
YR_RULE* rule = _yr_compiler_get_rule_by_idx(compiler, rule_idx);
yr_arena_ptr_to_ref(compiler->arena, rule->identifier, &$$.identifier.ref);
$$.type = EXPRESSION_TYPE_BOOLEAN;
$$.value.integer = YR_UNDEFINED;
$$.identifier.ptr = NULL;
}
else
{
yr_compiler_set_error_extra_info(compiler, $1);
result = ERROR_UNDEFINED_IDENTIFIER;
}
}
}