forked from chainguard-dev/malcontent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_eval.yara
More file actions
247 lines (190 loc) · 5.36 KB
/
code_eval.yara
File metadata and controls
247 lines (190 loc) · 5.36 KB
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
import "math"
rule js_eval: medium {
meta:
description = "evaluate code dynamically using eval()"
filetypes = "js,ts"
strings:
$val = /eval\([\.\+ _a-zA-Z\"\'\(\,\)]{1,32}/ fullword
$val2 = "eval(this.toString());"
$not_empty = "eval()"
condition:
filesize < 1MB and any of ($val*) and none of ($not*)
}
rule js_eval_fx_str: high {
meta:
description = "evaluate processed string using eval()"
filetypes = "js,ts"
strings:
$val = /eval\(\w{0,16}\([\"\'].{0,16}/
condition:
filesize < 1MB and any of ($val*)
}
rule js_eval_fx_str_multiple: critical {
meta:
description = "multiple evaluations of processed string using eval()"
filetypes = "js,ts"
strings:
$val = /eval\(\w{0,16}\([\"\'].{0,16}/
condition:
filesize < 1MB and #val > 1
}
rule js_eval_response: critical {
meta:
description = "executes code directly from HTTP response"
filetypes = "js,ts"
strings:
$val = /eval\(\w{0,16}\.responseText\)/
condition:
filesize < 1MB and any of ($val*)
}
rule js_eval_near_enough_fromChar: medium {
meta:
description = "Evaluates content via String.fromCharCode"
filetypes = "js,ts"
strings:
$eval = /[\s\{]eval\(/
$decrypt = "String.fromCharCode"
condition:
filesize < 5MB and all of them and math.abs(@eval - @decrypt) > 384
}
rule js_eval_obfuscated_fromChar: high {
meta:
description = "Likely evaluates encrypted content via fromCharCode"
filetypes = "js,ts"
strings:
$eval = /[\s\{]eval\(/
$ref = /fromCharCode\(\w{0,16}\s{0,2}[\-\+\*\^]{0,2}\w{0,16}/
condition:
filesize < 5MB and all of them and math.abs(@eval - @ref) > 384
}
rule js_anonymous_function: medium {
meta:
description = "evaluates code using an anonymous function"
filetypes = "js,ts"
strings:
$func = /\n\s{0,8}\(function\s{0,8}\(\)\s{0,8}\{/
$run = /\n\s{0,8}\}\)\(\);/
condition:
filesize < 5MB and all of them and (@run - @func) > 384
}
rule python_exec: medium {
meta:
description = "evaluate code dynamically using exec()"
filetypes = "py"
strings:
$f_import = "import" fullword
$f_join = ".join("
$f_chr = "chr("
$f_int = "int("
$f_for = /for [a-z] in /
$val = /exec\([\w\ \"\'\.\(\)\[\]]{1,64}/ fullword
$empty = "exec()"
condition:
filesize < 1MB and any of ($f*) and $val and not $empty
}
rule python_exec_near_enough_chr: high {
meta:
description = "Likely executes encoded character content"
filetypes = "py"
strings:
$exec = "exec("
$chr = "chr("
condition:
all of them and math.abs(@chr - @exec) < 768
}
rule python_exec_near_enough_fernet: high {
meta:
description = "Likely executes Fernet encrypted content"
filetypes = "py"
strings:
$exec = "exec("
$fernet = "Fernet("
condition:
all of them and math.abs(@exec - @fernet) < 768
}
rule python_exec_near_enough_decrypt: high {
meta:
description = "Likely executes encrypted content"
filetypes = "py"
strings:
$exec = /\bexec\(/
$decrypt = "decrypt("
condition:
all of them and math.abs(@exec - @decrypt) < 768
}
rule python_exec_chr: critical {
meta:
description = "Executes encoded character content"
filetypes = "py"
strings:
$exec = /exec\(.{0,16}chr\(.{0,16}\[\d[\d\, ]{0,64}/
condition:
filesize < 512KB and all of them
}
rule python_exec_bytes: critical {
meta:
description = "Executes a transformed bytestream"
filetypes = "py"
strings:
$exec = /exec\([\w\.\(]{0,16}\(b['"].{8,16}/
condition:
filesize < 512KB and all of them
}
rule python_exec_complex: high {
meta:
description = "Executes code from a complex expression"
filetypes = "py"
strings:
$exec = /exec\([\w\. =]{1,32}\(.{0,8192}\)\)/ fullword
$not_javascript = "function("
$not_pyparser = "exec(compile(open(self.parsedef).read(), self.parsedef, 'exec'))"
$not_versioneer = "exec(VERSIONEER.decode(), globals())"
condition:
filesize < 512KB and $exec and none of ($not*)
}
rule python_exec_fernet: critical {
meta:
description = "Executes Fernet encrypted content"
filetypes = "py"
strings:
$exec = /exec\(.{0,16}Fernet\(.{0,64}/
condition:
filesize < 512KB and all of them
}
rule shell_eval: medium {
meta:
description = "evaluate shell code dynamically using eval"
filetypes = "bash,sh,zsh"
strings:
$val = /eval \$\w{0,64}/ fullword
$not_fish_completion = "fish completion"
condition:
$val and none of ($not*)
}
rule php_create_function_no_args: high {
meta:
description = "dynamically creates PHP functions without arguments"
filetypes = "php"
strings:
$val = /create_function\([\'\"]{2},\$/
condition:
any of them
}
rule php_at_eval: critical {
meta:
description = "evaluates code in a way that suppresses errors"
filetypes = "php"
strings:
$at_eval = /@\beval\s{0,32}\(\s{0,32}(\$\w{0,32}|\.\s{0,32}"[^"]{0,32}"|\.\s{0,32}'[^']{0,32}'|\w+\(\s{0,32}\))/
$not_empty = "eval()"
condition:
$at_eval and none of ($not*)
}
rule npm_preinstall_eval: critical {
meta:
description = "NPM preinstall evaluates arbitrary code"
strings:
$ref = /\s{2,8}"preinstall": ".{12,256}eval\([\w\.]{1,32}\).{0,256}"/
condition:
filesize < 1KB and $ref
}