Commit Graph
1935 Commits
Author SHA1 Message Date
Wesley ShieldsandVictor M. Alvarez eb92e6b5aa Implement "none" keyword (#1559)
Co-authored-by: Victor M. Alvarez <[email protected]>
2021-09-02 20:57:39 +02:00
Victor M. Alvarez 22dec6bc92 Turn off warnings due to the use of bison-specific features not supported by yacc. 2021-09-01 13:56:43 +02:00
Victor M. Alvarez 30542144a1 Fix warning and reformat pe.c. 2021-09-01 11:22:43 +02:00
Victor M. AlvarezandWesley Shields 32c969f1e4 Implement percent "%" operator for string sets. (#1434)
This commit implements the "%" operator and extends the grammar to
recognize it for string sets.

The grammar now recognizes "50% of them" or variations on that. The
percentage number must be between 1 and 100 (inclusive) or the compiler will
return an error.

The implementation uses a new opcode (OP_OF_PERCENT) and this is because the
stack for these looks different from a typical "for_expression _OF_ string set"
rule. In the "for_expression" case (which uses OP_OF) the stack looks like this
(assuming two strings $a and $b):

$a
$b
undefined <-- end of list
2 <-- for_expression

The OP_OF operation checks each of the strings until it hits the end of list and
then compares the number found to whatever is left on the stack (2 in this
case).

The new grammar causes a stack at runtime which looks like this:

50 <-- percent number
$b
$a
undefined <-- end of list

The OP_OF operator won't work in this case because the percent number is sitting
on the top of the stack compared to the for_expression sitting at the bottom of
the stack.

I also cleaned up a minor oversight in compiler.c when printing the
ERROR_INVALID_MODIFIER message. It is the same as the other compiler errors so I
just moved it into the same set of fallthrough case statements.

Lastly, add some test cases to make sure this is working as intended.

Co-authored-by: Wesley Shields <[email protected]>
2021-09-01 01:28:24 +02:00
Wesley Shields 24a5ad1383 Implement the OP_OF_FOUND_IN opcode. (#1554)
This is a combination of "OP_OF" and "OP_FOUND_IN" that allows you to express logic like "all of them in (0..100)". This came out of the discussion in #1483 and likely makes part of that pull request redundant.
2021-08-31 19:34:16 +02:00
Ladislav ZezulaandLadislav Zezula 975af85352 Fixed resource string length and range (#1555)
Closes #1553

Co-authored-by: Ladislav Zezula <[email protected]>
2021-08-30 10:57:16 +02:00
Dominika Regéciová e3d6a4dd1a Penalize atoms with sequences of 0x90 (#1552) 2021-08-26 15:51:40 +02:00
Victor M. Alvarez c5b06fd470 Fix potential memory leak introduced in #1523. 2021-08-26 10:14:35 +02:00
Ladislav ZezulaandLadislav Zezula c70b914068 Fixed issue 1550 (#1551)
Co-authored-by: Ladislav Zezula <[email protected]>
2021-08-26 09:48:34 +02:00
Victor M. Alvarez cfde59f2d7 Minor re-styling. 2021-08-24 11:12:16 +02:00
Hilko Bengen 063cbfb793 Reduce memory pressure caused by Linux process scanning (#1470)
Reading memory pages from /proc/$PID/mem has the unpleasant side
effect of forcing otherwise unused process memory pages into the
target process VM, considerably increasing the target process'
resident set size.

As a typical example, many programs only make use of a subset of code
from shared libraries that they are linked against. The solution
consists of replicating memory mappings mappings from files if they
are directly available through the filesystem. All information needed
to find the file (and to determine if it is indeed identical to the
file mapped by the target process) is available from /proc/$PID/maps.
Only pages that may have been changed according to /proc/$PID/pagemap
are then read via /proc/$PID/mem to overwrite the local mapping.

Mappings whose underlying filesystem object has changed read from
/proc/$PID/mem as before.

Mappings that are not backed by a regular file (block devices, stack
or heap sections, etc.) are assumed to behave like zeroed-out files
and otherwise treated the same.
2021-08-24 10:40:45 +02:00
xbabka01 9d856c9249 Accept any Machine in PE header (#1538) 2021-08-24 10:14:54 +02:00
Wesley Shields cfdb4a8324 Default number_of_imported_functions to 0. (#1546) 2021-08-23 13:00:10 +02:00
Wesley Shields 87bc03d587 Add a math.abs() (#1545)
As mentioned in #1454, this makes checking the distance between two matches
regardless of the order of matches easier.
2021-08-23 12:17:45 +02:00
Victor M. Alvarez 39d5921389 Fix version number. 2021-08-23 12:12:22 +02:00
Wesley ShieldsandVictor M. Alvarez 7e0fdf2608 Fullword fix (#1544)
* Minor re-styling.

* Roll our own is_alnum() equivalent.

Use yr_isalnum() when checking if the match is fullword. See
https://github.com/VirusTotal/yara-python/issues/184 for details of what is
happening here, as this only happens on MacOS python AFAICT.

* Fix word character in RE code.

Use yr_isalnum() in RE code for handling \w.

* Fix styling.

* Roll our own is_alnum() equivalent.

Use yr_isalnum() when checking if the match is fullword. See
https://github.com/VirusTotal/yara-python/issues/184 for details of what is
happening here, as this only happens on MacOS python AFAICT.

* Fix word character in RE code.

Use yr_isalnum() in RE code for handling \w.

* Fix styling.

Co-authored-by: Victor M. Alvarez <[email protected]>
2021-08-23 12:00:51 +02:00
Victor M. Alvarez 7056f9405a Fix buffer overrun caused by wrong type signedness. 2021-08-23 11:59:26 +02:00
Wesley Shields 37bee583c2 Expose YR_DEBUG_VERBOSITY in configure script. (#1542)
Add a --with-debug-verbose option to the configure script which takes a number
greater than 0 as an argument. This gives us the flexibility to make verbosity
of this useful debugging information greater than it's current maximum (2)
without needing to modify the configure script.

Currently the maximum is 2, which gets you a lot of useful runtime debugging
information. A value of 1 is only used in tests. It may be worth it to consider
reworking this system so the numbers describe how much information you want.
For example, 1 could just be function entry and exit, 2 could add arguments to
it. I'll leave changing that up to a future diff if it is desired.

While here, fix a warning in the scanner from using an incorrect format string:

scan.c:644:7: warning: format specifies type 'long long' but the argument has type 'size_t' (aka 'unsigned long') [-Wformat]
      match_offset,
      ^~~~~~~~~~~~~
./include/yara/globals.h:100:29: note: expanded from macro 'YR_DEBUG_FPRINTF'
    fprintf(STREAM, FORMAT, __VA_ARGS__);                      \
                    ~~~~~~  ^~~~~~~~~~~
1 warning generated.
2021-08-23 09:50:05 +02:00
Victor M. Alvarez 4d6ecf83c0 Minor re-styling. 2021-08-20 09:50:14 +02:00
xbabka01 b8650dfa05 Add imports_details and delayed_imports_details (#1523) 2021-08-20 09:42:57 +02:00
Tomáš KenderandTomáš Kender 24db4b346a load version info records with empty value (#1539)
Co-authored-by: Tomáš Kender <[email protected]>
2021-08-17 10:09:59 +02:00
xbabka01andVictor M. Alvarez 21f4c6ec06 Added pe.version_info_list with the list of all version info records (#1509)
Co-authored-by: Victor M. Alvarez <[email protected]>
2021-08-11 09:49:47 +02:00
Marek Milkovič 66c1c7176b Added iequals operator for case-insensitive string comparison (#1536) 2021-08-11 09:40:54 +02:00
xbabka01 a3dea76691 Fix pe rva to offset (#1527) 2021-08-10 10:02:55 +02:00
xbabka01 5f34c1e466 Fix typo from pe.SECTION_FARDATA to SECTION_MEM_FARDATA (#1533) 2021-08-09 14:25:15 +02:00
xbabka01 0c8d91f705 Added missing PE constants (#1520) 2021-08-09 12:10:32 +02:00
Marek Milkovič c19428b00f modules/pe: Added detection of additional characters in section names (#1530)
after the first null

There are certain samples like 0043812838495a45449a0ac61a81b9c16eddca1ad249fb4f7fdb1c4505e9bb34
which contain section names with additional characters after the first
null. This is an useful information which can be used to detect such
anomalies. YARA is not able to process those even though VirusTotal web interface is able to detect them.

PE specification states that section name is "An 8-byte, null-padded UTF-8 encoded string. If the string is exactly 8 characters long, there is no terminating null.".
If you however consider the first null as the terminator then
null-padding requirement from PE specification is not adhered. If you
what we are doing in this commit then you are also going against the
specification because we are not honoring the fact that those strings
are null-terminated. We however found that having more information is
more useful than having less information, hence this PR.

The change is made with backward compatibility in mind so for vast
majority of samples, nothing changes. Their section names will remain
the same. It only affect such weird samples like the one mentioned above
so even though it might break some rules on some specific samples, it
shouldn't really affect anyone that much.
2021-08-09 12:08:52 +02:00
Ladislav ZezulaandLadislav Zezula d5b7a913d1 Issue https://github.com/VirusTotal/yara/issues/1505 (#1506)
Co-authored-by: Ladislav Zezula <[email protected]>
2021-08-09 11:32:56 +02:00
Victor M. Alvarez b77784a50d Reformat code. 2021-08-09 11:30:43 +02:00
Ladislav ZezulaandLadislav Zezula 247ab7346a Issue https://github.com/VirusTotal/yara/issues/1501 revisited (#1507)
Co-authored-by: Ladislav Zezula <[email protected]>
2021-08-09 11:27:25 +02:00
Wesley Shields 0361fac196 Fix issue with warnings. (#1532)
Fix an issue with the TOO_MANY_MATCHES warning where it would set the
STRING_FLAGS_DISABLED bit on the string, but it would never be reset when the
scanner was used to scan another file. This meant that a string that caused
TOO_MANY_MATCHES would be disabled permanently for the lifetime of that scanner,
when the intention was to only disable it for that one scan. A future scan
should have the string re-enabled until it matches too many times again.

I decided to fix this by using a bitmask for each string in the rules. This
allows me to very quickly reset them all without having to touch every string to
unset the STRING_FLAGS_DISABLED flag.

This also returns the STRING_FLAGS_DISABLED flag to it's original meaning, which
was that the string was disabled permanently because it is causing serious
performance problems.

I added a test for all this too.
2021-08-09 09:48:59 +02:00
Victor M. Alvarez 09027e5350 Make EOL of type size_t. 2021-07-09 23:24:51 +02:00
Victor M. Alvarez 4bfca5558a Fix incorrect type passed to va_arg.
The type should be size_t, not yr_arena_off_t.
2021-07-09 23:12:43 +02:00
Victor M. Alvarez c2f6b3b553 Fix incorrect type passed to va_arg. 2021-07-09 22:57:57 +02:00
Victor M. Alvarez 48cc15b447 Replace yr_re_fast_exec with a faster implementation. (#1526)
yr_re_fast_exec used a very naive approach based on backtracking which had extremely bad performance with certain pathological cases. For example the following pattern could take more than a minute when applied to a small file containing the sequence 0xAA 0xAA 0xAA 0xAA three times, with around 100 zeroes following each of those sequences:

{ AA AA AA AA [1-60] 00 [1-60] 00 [1-60] AA AA AA AA [1-60] 00 [1-60] 00 [1-60] AA AA AA AA [1-60] 00 [1-60] BB }

With the new implementation it takes a fraction of a second.
2021-07-08 09:47:47 +02:00
Dominika Regéciová d3f4e631d8 load_files() problem with memory fix (#1518) 2021-06-18 12:18:20 +02:00
Victor M. Alvarez 3c4dfb09a4 Prevent clang-format from complaining about the include order in openbsd.c 2021-06-14 09:34:25 +02:00
pwrdwnsys 26daee2872 Correct OpenBSD include order to fix unknown type errors during compilation (#1486) 2021-06-14 09:28:56 +02:00
Marek Milkovič edb409f06b Fixed parsing of rich signature if it starts immediatelly after MZ header (#1512)
There are certain samples where Rich signature starts immediatelly after
MZ header. This check just allows this offset to be inspected during
the detection loop.
2021-06-09 11:54:13 +02:00
Victor M. Alvarez a1112bb34f Add test case for change in 4c53300454 and fix the same issue for dictionaries. 2021-06-02 09:33:29 +02:00
meme 4c53300454 Check if iterator is YR_UNDEFINED before accessing (#1508) 2021-06-02 09:06:08 +02:00
Ladislav ZezulaandLadislav Zezula 879a6576dd Fixed https://github.com/VirusTotal/yara/issues/1501 (#1502)
Co-authored-by: Ladislav Zezula <[email protected]>
2021-05-21 09:56:45 +02:00
Victor M. Alvarez e87331beef Revert a11a7e5afe and fix buffer run in "macho" module.
The fix attempt in a11a7e5afe was not effective.
2021-05-20 16:15:13 +02:00
Victor M. Alvarez a11a7e5afe Fix buffer overrun in "macho" module. 2021-05-19 10:10:37 +02:00
Victor M. Alvarez eaa4e2ec0e Fix issue #1492. 2021-05-11 11:28:00 +02:00
Orycterope 397db256bc Fix bitwise shift UB in filemap_fd on windows (#1489)
Fixes #1302
2021-05-10 09:26:40 +02:00
Victor M. Alvarez 9e18119fdf Initialize file_size function as NULL for process scans. 2021-04-19 10:04:43 +02:00
Victor M. Alvarez c4b1a38c55 Allow YR_MEMORY_BLOCK_ITERATOR to have a NULL value for file_size function.
If the file_size function is NULL the filesize keyword in a YARA rule will evaluate to undefined. This makes the _undefined_file_size function in proc.c unnecessary.
2021-04-09 13:54:28 +02:00
Victor M. Alvarez 98b4dd3173 Clarify the usage of last_error in YR_MEMORY_BLOCK_ITERATOR. 2021-04-09 13:38:04 +02:00
Victor M. Alvarez 3f179ac2a2 Decouple ABI version from release version.
Until now the release version has been used as the ABI version, but that's not recommended at all. Start tracking the ABI version with its own version number, starting with 8 in order to make clear that this number is not tied to the release version.
2021-04-09 13:05:42 +02:00