Replies: 2 comments 1 reply
-
I like this, it will make writing more complex rules less verbose. I assume this is meant to be orthogonal to the idea of globals as discussed elsewhere as the implementation of the two are not likely to be similar? |
Beta Was this translation helpful? Give feedback.
-
I mean global identifiers visible across all rules. Think of things like
I think this RFC is great for rule variables, and exposing them to the outside world is not something I had considered, and I am unable to come up with a nice use-case right now. As such, I think |
Beta Was this translation helpful? Give feedback.
-
YARA users sometimes create complex rules that use the same expression multiple times in the rule. When this happens the rule's condition becomes very verbose and repetitive. Let's see an example:
The condition above uses the expressions
pe.sections[0]
andpe.sections[pe.number_of_sections - 1]
multiple times for referring to the first and last sections of the PE respectively. This could be expressed an a more compact and legible way if those expressions could be replaced by identifiers holding the values ofpe.sections[0]
andpe.sections[pe.number_of_sections - 1]
.This RFC proposes the introduction of a new
with
statement aimed to solve this problem, and that would look like this:The syntax for this new statement would be:
The
with
statement can define one or more identifiers and assign them the values of arbitrary expressions. Of course, this expressions can use any identifier that is visible in the scope where thewith
statement resides. Each expression is evaluated once, and the resulting value is reused every time the corresponding identifier is used within the boolean expression.This statement also helps reducing the number of expensive computations, for example:
In the example above the expensive function
hashes.sha256
is called once, but its result is re-used three times in the boolean expression.Note: The
hashes.sha256
has an internal cache that saves the results of previous calls and avoids re-computing the same hashes over and over again. This cache was introduced precisely because YARA lacked a way for storing and re-using the result of a function call.This statement is also useful within loops, for example:
Without the
with
statement the condition should be:Beta Was this translation helpful? Give feedback.
All reactions