Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add OP_OF_FOUND_AT. #1790

Merged
merged 3 commits into from
Sep 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/writingrules.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1221,6 +1221,13 @@ integer range, like this:
all of ($a*) in (filesize-500..filesize)
any of ($a*, $b*) in (1000..2000)

Starting with YARA 4.3.0 it is possible to express a set of strings at a
specific offset, like this:

.. code-block:: yara

any of ($a*) at 0


Applying the same condition to many strings
-------------------------------------------
Expand Down
75 changes: 75 additions & 0 deletions libyara/exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -1742,6 +1742,81 @@ int yr_execute_code(YR_SCAN_CONTEXT* context)
push(r1);
break;

case OP_OF_FOUND_AT:
YR_DEBUG_FPRINTF(
2, stderr, "- case OP_OF_FOUND_AT: // %s()\n", __FUNCTION__);

found = 0;
count = 0;

pop(r2); // Match location
pop(r1); // First string

// Match location must be defined.
if (is_undef(r2))
{
// Remove all the strings.
while (!is_undef(r1)) pop(r1);
// Remove the quantifier at the bottom of the stack.
pop(r1);
r1.i = YR_UNDEFINED;
push(r1);
break;
}

while (!is_undef(r1))
{
#if YR_PARANOID_EXEC
ensure_within_rules_arena(r1.p);
#endif
match = context->matches[r1.s->idx].head;

while (match != NULL)
{
// String match at the desired location?
if (match->base + match->offset == r2.i)
{
found++;
break;
}

// If current match is past desired location, we can stop as matches
// are sorted by offset in increasing order, so all remaining
// matches are past it.
if (match->base + match->offset > r2.i)
break;

match = match->next;
}

count++;
pop(r1);
}

pop(r2); // Quantifier X in expressions like "X of string_set in range"

// Quantifier is "all".
if (is_undef(r2))
{
r1.i = found >= count ? 1 : 0;
}
// Quantifier is 0 or none. This is a special case in which we want
// exactly 0 strings matching. More information at:
// https://github.com/VirusTotal/yara/issues/1695
else if (r2.i == 0)
{
r1.i = found == 0 ? 1 : 0;
}
// In all other cases the number of strings matching should be at least
// the amount specified by the quantifier.
else
{
r1.i = found >= r2.i ? 1 : 0;
}

push(r1);
break;

case OP_FILESIZE:
r1.i = context->file_size;
YR_DEBUG_FPRINTF(
Expand Down
Loading