1 /*
2 Copyright (c) 2013-2014. The YARA Authors. All Rights Reserved.
3 
4 Redistribution and use in source and binary forms, with or without modification,
5 are permitted provided that the following conditions are met:
6 
7 1. Redistributions of source code must retain the above copyright notice, this
8 list of conditions and the following disclaimer.
9 
10 2. Redistributions in binary form must reproduce the above copyright notice,
11 this list of conditions and the following disclaimer in the documentation and/or
12 other materials provided with the distribution.
13 
14 3. Neither the name of the copyright holder nor the names of its contributors
15 may be used to endorse or promote products derived from this software without
16 specific prior written permission.
17 
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
22 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29 
30 #ifndef YR_EXEC_H
31 #define YR_EXEC_H
32 
33 #include <yara/hash.h>
34 #include <yara/rules.h>
35 #include <yara/scan.h>
36 #include <yara/types.h>
37 
38 #define YR_UNDEFINED    0xFFFABADAFABADAFFLL
39 #define IS_UNDEFINED(x) ((size_t)(x) == (size_t) YR_UNDEFINED)
40 
41 #define OP_ERROR 0
42 #define OP_HALT  255
43 #define OP_NOP   254
44 
45 #define OP_AND                  1
46 #define OP_OR                   2
47 #define OP_NOT                  3
48 #define OP_BITWISE_NOT          4
49 #define OP_BITWISE_AND          5
50 #define OP_BITWISE_OR           6
51 #define OP_BITWISE_XOR          7
52 #define OP_SHL                  8
53 #define OP_SHR                  9
54 #define OP_MOD                  10
55 #define OP_INT_TO_DBL           11
56 #define OP_STR_TO_BOOL          12
57 #define OP_PUSH                 13
58 #define OP_POP                  14
59 #define OP_CALL                 15
60 #define OP_OBJ_LOAD             16
61 #define OP_OBJ_VALUE            17
62 #define OP_OBJ_FIELD            18
63 #define OP_INDEX_ARRAY          19
64 #define OP_COUNT                20
65 #define OP_LENGTH               21
66 #define OP_FOUND                22
67 #define OP_FOUND_AT             23
68 #define OP_FOUND_IN             24
69 #define OP_OFFSET               25
70 #define OP_OF                   26
71 #define OP_PUSH_RULE            27
72 #define OP_INIT_RULE            28
73 #define OP_MATCH_RULE           29
74 #define OP_INCR_M               30
75 #define OP_CLEAR_M              31
76 #define OP_ADD_M                32
77 #define OP_POP_M                33
78 #define OP_PUSH_M               34
79 #define OP_SET_M                35
80 #define OP_SWAPUNDEF            36
81 #define OP_FILESIZE             37
82 #define OP_ENTRYPOINT           38
83 #define OP_UNUSED               39
84 #define OP_MATCHES              40
85 #define OP_IMPORT               41
86 #define OP_LOOKUP_DICT          42
87 #define OP_JUNDEF               43 /* Not used */
88 #define OP_JUNDEF_P             44
89 #define OP_JNUNDEF              45
90 #define OP_JNUNDEF_P            46 /* Not used */
91 #define OP_JFALSE               47
92 #define OP_JFALSE_P             48
93 #define OP_JTRUE                49
94 #define OP_JTRUE_P              50
95 #define OP_JL_P                 51
96 #define OP_JLE_P                52
97 #define OP_ITER_NEXT            53
98 #define OP_ITER_START_ARRAY     54
99 #define OP_ITER_START_DICT      55
100 #define OP_ITER_START_INT_RANGE 56
101 #define OP_ITER_START_INT_ENUM  57
102 #define OP_JZ                   58
103 #define OP_JZ_P                 59
104 #define OP_PUSH_8               60
105 #define OP_PUSH_16              61
106 #define OP_PUSH_32              62
107 #define OP_PUSH_U               63
108 #define OP_CONTAINS             64
109 #define OP_STARTSWITH           65
110 #define OP_ENDSWITH             66
111 #define OP_ICONTAINS            67
112 #define OP_ISTARTSWITH          68
113 #define OP_IENDSWITH            69
114 
115 #define _OP_EQ    0
116 #define _OP_NEQ   1
117 #define _OP_LT    2
118 #define _OP_GT    3
119 #define _OP_LE    4
120 #define _OP_GE    5
121 #define _OP_ADD   6
122 #define _OP_SUB   7
123 #define _OP_MUL   8
124 #define _OP_DIV   9
125 #define _OP_MINUS 10
126 
127 #define OP_INT_BEGIN 100
128 #define OP_INT_EQ    (OP_INT_BEGIN + _OP_EQ)
129 #define OP_INT_NEQ   (OP_INT_BEGIN + _OP_NEQ)
130 #define OP_INT_LT    (OP_INT_BEGIN + _OP_LT)
131 #define OP_INT_GT    (OP_INT_BEGIN + _OP_GT)
132 #define OP_INT_LE    (OP_INT_BEGIN + _OP_LE)
133 #define OP_INT_GE    (OP_INT_BEGIN + _OP_GE)
134 #define OP_INT_ADD   (OP_INT_BEGIN + _OP_ADD)
135 #define OP_INT_SUB   (OP_INT_BEGIN + _OP_SUB)
136 #define OP_INT_MUL   (OP_INT_BEGIN + _OP_MUL)
137 #define OP_INT_DIV   (OP_INT_BEGIN + _OP_DIV)
138 #define OP_INT_MINUS (OP_INT_BEGIN + _OP_MINUS)
139 #define OP_INT_END   OP_INT_MINUS
140 
141 #define OP_DBL_BEGIN 120
142 #define OP_DBL_EQ    (OP_DBL_BEGIN + _OP_EQ)
143 #define OP_DBL_NEQ   (OP_DBL_BEGIN + _OP_NEQ)
144 #define OP_DBL_LT    (OP_DBL_BEGIN + _OP_LT)
145 #define OP_DBL_GT    (OP_DBL_BEGIN + _OP_GT)
146 #define OP_DBL_LE    (OP_DBL_BEGIN + _OP_LE)
147 #define OP_DBL_GE    (OP_DBL_BEGIN + _OP_GE)
148 #define OP_DBL_ADD   (OP_DBL_BEGIN + _OP_ADD)
149 #define OP_DBL_SUB   (OP_DBL_BEGIN + _OP_SUB)
150 #define OP_DBL_MUL   (OP_DBL_BEGIN + _OP_MUL)
151 #define OP_DBL_DIV   (OP_DBL_BEGIN + _OP_DIV)
152 #define OP_DBL_MINUS (OP_DBL_BEGIN + _OP_MINUS)
153 #define OP_DBL_END   OP_DBL_MINUS
154 
155 #define OP_STR_BEGIN 140
156 #define OP_STR_EQ    (OP_STR_BEGIN + _OP_EQ)
157 #define OP_STR_NEQ   (OP_STR_BEGIN + _OP_NEQ)
158 #define OP_STR_LT    (OP_STR_BEGIN + _OP_LT)
159 #define OP_STR_GT    (OP_STR_BEGIN + _OP_GT)
160 #define OP_STR_LE    (OP_STR_BEGIN + _OP_LE)
161 #define OP_STR_GE    (OP_STR_BEGIN + _OP_GE)
162 #define OP_STR_END   OP_STR_GE
163 
164 #define IS_INT_OP(x) ((x) >= OP_INT_BEGIN && (x) <= OP_INT_END)
165 #define IS_DBL_OP(x) ((x) >= OP_DBL_BEGIN && (x) <= OP_DBL_END)
166 #define IS_STR_OP(x) ((x) >= OP_STR_BEGIN && (x) <= OP_STR_END)
167 
168 #define OP_READ_INT 240
169 #define OP_INT8     (OP_READ_INT + 0)
170 #define OP_INT16    (OP_READ_INT + 1)
171 #define OP_INT32    (OP_READ_INT + 2)
172 #define OP_UINT8    (OP_READ_INT + 3)
173 #define OP_UINT16   (OP_READ_INT + 4)
174 #define OP_UINT32   (OP_READ_INT + 5)
175 #define OP_INT8BE   (OP_READ_INT + 6)
176 #define OP_INT16BE  (OP_READ_INT + 7)
177 #define OP_INT32BE  (OP_READ_INT + 8)
178 #define OP_UINT8BE  (OP_READ_INT + 9)
179 #define OP_UINT16BE (OP_READ_INT + 10)
180 #define OP_UINT32BE (OP_READ_INT + 11)
181 
182 #define OPERATION(operator, op1, op2) \
183   (IS_UNDEFINED(op1) || IS_UNDEFINED(op2)) ? (YR_UNDEFINED) : (op1 operator op2)
184 
185 #define COMPARISON(operator, op1, op2) \
186   (IS_UNDEFINED(op1) || IS_UNDEFINED(op2)) ? (0) : (op1 operator op2)
187 
188 int yr_execute_code(YR_SCAN_CONTEXT* context);
189 
190 #endif
191