Commit Graph
3132 Commits
Author SHA1 Message Date
Victor M. Alvarez 5bf72f2fb0 Fix issue while parsing .NET resources
.NET resources can be found outside the current file. Before this change those resources were being ignored, but now they are included in the `resources` array. The `name` and `length` fields are kept undefined though. So, external resources have only the `name` field set.
2024-03-18 20:21:12 +01:00
Victor M. Alvarez c009195c1e Apply stricter sanity checks while parsing PE resources.
Corrupted file `c10a733e4899dbd03d537d7832efa71016f618e9fd1bffa35b8265df359a91f8` was producing tons of invalid resource entries.
2024-03-08 12:28:57 +01:00
Victor M. Alvarez a0241546df Remove check in dotnet_is_dotnet that doesn't seem necessary.
This function was checking the first two bytes at the entrypoint of .NET files, if the bytes were not `FF 25` the file was not considered a .NET file. This check was overly restrictive, as some files like `8fa7fe73a65296e9ca8301734a0deaa298cda0b7a65f6b7d2ef6d1c8bbb8cd7a` don't have such files at the entrypoint.
2024-03-07 10:39:43 +01:00
Victor M. Alvarez 8242721801 Fix issue in pe_rva_to_offset for some files.
This function was returning a wrong RVA for files like `67508ec9cbd9f82c6aea8a93ca27fd3684d368664470397755f8d70934d481fb`, when a section's virtual size is smaller than its physical size.
2024-03-07 10:33:33 +01:00
Victor M. Alvarez 9490a0c00c Do not abort resource iteration too early when some entry is truncated.
Failing to parse some resource directory does not mean that we can't keep parsing other directories. For instance, with file `0a65e22cdff348be11739122906355af728f6247fb849f98d69a6f6d51103071` YARA was aborting the parsing too early and extracted a single resource, while the file has more resources that could be parsed correctly.
2024-03-06 12:00:27 +01:00
Victor M. Alvarez 4793b49dd7 Fix issue while parsing PE rich header.
YARA was making the assumption that the 12 bytes following the `DanS` header contained 3 copies of the 32-bits XOR key, but these bytes are actually padding. As these bytes were usually filled with zeroes before applying the XOR key, they appear to contain the key itself after the XOR operation. However, files like `043066108b68b30fc2c475eae8edfafc080be7d451600eaa283d2c750bddbceb` don't contain zeroes in those bytes, and therefore were not satisfying YARA's assumption.

YARA-X was right: https://github.com/VirusTotal/yara-x/blob/6ada059b9e4b328e2341b42304765c5614fc89af/lib/src/modules/pe/parser.rs#L687-L693
2024-03-04 18:26:19 +01:00
Marek Milkovič f51b9d7f6a Updated authenticode parser to latest version (2024-03-02) (#2049)
Upon fixing the latest issue regarding parsing CMS, we figured out we
are not properly extracting all certificates from Microsoft
countersignatures. This should now properly expose them.

Also fixes one memory leak for countersignature parsing.
2024-03-04 09:32:58 +01:00
Victor M. Alvarez a1883addcb Update example in yara-python's documentation.
As discussed in https://github.com/VirusTotal/yara-python/issues/247, the example using `StringIO` worked fine in Python 2, but ti doesn't work in Python 3. Now that Python 2 is mostly dead, update the example.
2024-02-29 13:08:03 +01:00
orbea 2e2cd20551 countersignature: remove TS_VERIFY_CTX_init() call (#2044)
This is redundant and already handled by TS_VERIFY_CTX_new().
Additionally it causes build failures with LibreSSL >= 3.8.0 which
removed this function since it serves no purpose with opaque
TS_VERIFY_CTX where it will zero out an already zero'd out ctx or cause
a leak.

See: https://github.com/libressl/portable/issues/1006
2024-02-26 10:05:06 +01:00
Victor M. Alvarez 8fa55cde4c Bump version number to 4.5.0 v4.5.0 2024-02-13 12:33:22 +01:00
Victor M. Alvarez 4369ca6a1b Fix endianness issues in macho module (#2041)
This is backward compatible change that affects the `magic` field. After this change the value in the `magic` field looks exactly as it looks in the file regardless of the endianness of the current platform, if the file starts with `CA FE BA BE` the value in magic is `0xCAFEBABE`, not `0xBEBAFECA` as it used to be in little-endian architectures.
2024-02-12 15:23:09 +01:00
Victor M. Alvarez f3b3027154 Allow quantifiers {0} and {0,0}.
Regexps with quantifiers `{0}` and `{0,0}` were considered syntactically invalid. These quantifiers are not  actually useful, but they are now allowed for compatibility with other regexp engines.

Closes #2037.
2024-02-12 11:47:17 +01:00
Victor M. Alvarez c1c5d89052 pe.is_signed is now defined for all PE files.
Until now, `pe.is_signed` was undefined for every PE file that was not signed, making impossible to use it in negated conditions, like `pe.is_pe and not pe.is_signed`. With this change, `pe.is_signed` is false for every PE file that is not signed.

Closes #2040
2024-02-12 11:10:49 +01:00
Victor M. Alvarez 9ff9c4c6ca Temporary fix for SIGSEGV caused by authenticode parser.
See: https://github.com/avast/authenticode-parser/issues/19 for details.
2024-02-09 10:47:12 +01:00
Victor M. Alvarez 3342aa06f6 Fix bug in magic module when libmagic returns null pointer.
The pointer returned from `libmagic` was passed directly to `yr_strdup` (a wrapper of `strdup`), but this function has undefined behavior when the argument is null.
2024-02-08 11:28:17 +01:00
Tom Lancaster e4372d3a54 typo fix (#2039) 2024-02-07 16:56:48 +01:00
Victor M. Alvarez 3a2b4d16c5 Upgrade magic library to version 5.45. 2024-02-06 13:53:11 +01:00
Victor M. Alvarez e065be6716 Add test case for magic field in macho module.
This should confirm an issue with the endianness of `magic` field in big-endian platforms.
2024-01-30 21:32:02 +01:00
Victor M. Alvarez f5edf4e5f5 Run the bigendian GitHub workflow with the code from the current commit (#2036)
Instead of downloading the latest master branch, use the code in the current branch. Until know, this workflow was always operating wit the latest commit in the master branch.
2024-01-30 21:29:10 +01:00
Victor M. Alvarez ff4a923fac Enable macho module when running tests 2024-01-30 17:12:43 +01:00
Vincent Thiberville 8695c845d0 fix process scanning on macos by properly listing memory regions (#2033)
The process scanning on macos was broken since 4.2.0 in this commit:

605b2edf07

Logic was added to properly iterate on memory chunks if a memory region
was too big, but this completely broke the iteration. To retrieve a
memory region, the code basically does:

```
current_begin = 0;
for (;;) {
    vm_address_t addr = current_begin;

    mach_vm_region(..., &addr, &size, ...);

    size_t chunk_size = size - (current_begin - addr);
    ...
}
```

It assumes that `addr` is smaller than `current_begin`, which is never
the case outside of chunking. This leads to an iteration on completely
wrong regions, leading to read that either fail because the starting
addr or length is wrong, or that succeed, but do not cover the whole
region.

For example, with a process memory that looks like:

```
1065a6000-1065f2000
1065f2000-1065f6000
1065f6000-1065fa000
...
600000000000-600008000000
600008000000-600010000000
...
7fe537f00000-7fe538000000
7fe538000000-7fe538800000
```

The very first call would have `current_begin=0` and `addr=0x1065a6000`,
leading to underflow when computing `chunk_size`, and trying to iterate
on the region `0x00000000-0x40000000` (as the max memory chunk is 1GB
per default). this will fail to fetch, and will then iterate on
`0x40000000-0x80000000`, then `0x80000000-0xC0000000`, then
`0xC0000000-0x100000000`. We will finally reach the very first real
memory region, but fail to properly compute the region size with another
underflow, etc.

This commit fixes the issue by distinguishing the two cases:
- either `addr > current_begin` : `current_begin` is not in any memory
  region, and we must fast forward.
- or `addr <= current_begin`: we can compute the current chunk by
  diffing the two values.

This was not tested because the test_process_scan was not running on
macos: trying to scan the sh process always fails even when root, and
the test was thus skipped. I hacked the test locally to check if it
worked by replacing the sh process by something else that we can attach
to, and it showed the scanning fails. With this commit, the test does
pass.

I did not commit any modification to the test as I used a custom binary
for this and it needs to be run as root, but it would probably be a good
idea to adapt it.
2024-01-30 10:05:09 +01:00
Marek Milkovič 77b5dfa77e Update authenticode parser (#2034)
This updates authenticode parser to the latest versions which contains
sereral fixes which accumulated over time.

* Several time_t variables replaced with int64_t
* RFC5652 countersignatures are now correctly parsed
* Memory corruption related fixes where authenticode-parser would crash otherwise
2024-01-30 09:51:47 +01:00
Matteo Lodi 28ac3d2947 added IntelOwl reference in the ReadMe (#2029) 2024-01-27 12:45:04 +01:00
Victor M. Alvarez 4d696a91b1 Fix test cases 2024-01-15 22:31:18 +01:00
Victor M. Alvarez 79794c4053 Fix issue with regexps not matching empty strings.
Some regexps like `/(abc|)/` where not matching the empty string even if they should. Matching an empty string is not very useful, as such regexp will match on every offset of every file, but this is fixed for correctness.

Also fixes another issue with the `\b` metacharacter in regexps, which was exposed after fixing the main issue.
2024-01-15 16:51:11 +01:00
Victor M. Alvarez bb28999b33 Autoformat test/util.h 2024-01-15 16:34:12 +01:00
Victor M. Alvarez a3d938b429 Minor style fixes. 2024-01-15 13:07:34 +01:00
Vincent Thiberville 53c83c8dca fix issues with modules fetch of memory blocks in some situations (#2025)
Some modules functions (from hash and math) can access multiple memory
blocks, as long as those are contiguous. For example, when hashing
between offsets 0..1000, blocks that cover this range will be fetched.

This logic however is a bit buggy on the boundary: if the fetched range
ends exactly at the end of a block, the iteration keeps going instead of
ending. This can result in two situations:

- The next block is contiguous: it is then fetched.
  - if the fetch fails, the function fails (and return undefined)
  - if the fetch succeeds, we read 0 bytes from it and
    end the iteration, so a useless fetch was done.
- The next block is not contiguous: the function fails (and return
  undefined).

This is fixed by modifying the condition to detect if there are still
more bytes to fetch.
2024-01-12 16:15:27 +01:00
Victor M. Alvarez 821c979416 Fix issue in read_blob_signed function in dotnet module.
While porting the `dotnet` module to YARA-X I discovered the the implementation of `read_blob_signed` had a subtle bug. As `first_byte` was declared as `uint8_t` instead of `int8_t`, sign extension when casting the value to a `int32_t` was not done, leading to incorrect values.
2023-12-29 15:50:51 +01:00
Victor M. Alvarez c63cbf3d60 Revert commit 330bc74 and fix #2007 in the way suggested in #2010 2023-12-29 12:04:29 +01:00
Victor M. Alvarez 5b6f0f6ecd Add test case for math.mode
Cover the case in which there are two bytes that appear the same number of times. The lowest byte should be the mode.
2023-12-28 18:10:34 +01:00
Victor M. Alvarez a1015aa3a9 math.percentage returns undefined value when the passed value is outside the 0-255 range. 2023-12-28 17:34:23 +01:00
Victor M. Alvarez c6cc3311ce math.count returns undefined value when the passed value is outside the 0-255 range. 2023-12-28 16:44:48 +01:00
Victor M. Alvarez 923368eab3 Prevent infinite recursion while following symlinks.
Closes #2021.
2023-12-10 20:38:03 +01:00
Victor M. Alvarez cddec11934 Fix bug that broke global rules.
PR #1927 didn't take into account that as a side-effect of skipping the evaluation of global rules, the corresponding namespace was not flagged as unsatisfied (i.e: a global rule that evaluates to false prevents all other rules in the namespace from evaluating to true). The issue was not caught by any test case because the test case for global rules were using rules that didn't required any strings to match, and therefore were unaffected by the changes introduced in #1927.

This change makes sure that any global rule that is skipped is considered false, and therefore its namespace is flagged as unsatisfied.

Closes #2022.
2023-12-10 20:27:05 +01:00
Victor M. Alvarez 86239e3798 Revert "Fix Validation error for rule_modifier in grammar.y (#2019)"
This reverts commit 2d103d3ce3. The previous fix introduces shit-reduce and reduce-reduce conflicts in the grammar. The grammar.y file was not correctly processed by `bison`.
2023-12-10 19:14:48 +01:00
Vincent Thiberville 6cdc3fdb93 stop fetching memory blocks when an elf is found (#1996)
When scanning multiple memory blocks, we should stop as soon as a valid
elf is found. This was broken as the "break" applies to the switch, and
not the outer loop. Fix this by reworking the code to remove the switch.

This bug had two repercutions:

- instead of parsing the first memory block that contains an elf, it
  returned the last one, or actually even worse, a mix of all elfs
  depending on which values are set again or not. This however should
  not manifest in normal situations, notably during process scanning, as
  I suppose only one memory region would contain an elf executable.

- the module caused the fetch of all memory blocks instead of stopping
  as soon as one was found, which is worse performance wise.
2023-12-08 00:14:40 +01:00
tibibyte 2d103d3ce3 Fix Validation error for rule_modifier in grammar.y (#2019)
* fixed validation error in grammar.y

rule_modifiers 'private' and 'global' can no longer be used more than once in a rule definition. They can be used exclusively or together in any order.

* Update grammar.y
2023-12-08 00:12:59 +01:00
secDre4mer 19da77ef4f feat: handle SIGBUS / SIGSEGV only when originating from YARA's memory blocks (#2020)
* feat: prevent race conditions on signal handler

If multiple threads were running in parallel (e.g. each using a
YR_SCANNER), race conditions could cause the original signal handler
to not be restored correctly.
Add a mutex that controls access to the (process wide) signal handler
to ensure that the YARA signal handler is only installed once and
only removed when it is no longer needed.

* feat: forward signal to old signal handler

If a signal is received that does not originate from YARA (which is
quite possible in multi-threaded applications using YARA), it is
currently ignored. Especially for SIGSEGV, this may cause unexpected
behaviour in the caller, even in threads that do not use YARA.

Add logic to call the original signal handler to handle this case
more cleanly. It is not perfect - with only a process wide signal
handler, it's probably impossible to have a perfect solution -
but it's better than the current behaviour.

* fix: make exception test more verbose

* fix: only handle signals in memory block data

Some callbacks from YARA code are done from YR_TRYCATCH. Currently,
YARA assumes that SIGBUS / SIGSEGV from these callbacks are due to
the scanned memory range, but this is not necessarily true.
Add logic to identify the currently scanned memory block and only
catch the signal if the SIGBUS / SIGSEGV originated from it.

* chore: simplify signal handler forwarding

* feat: reduce caught signals to required for known OS
2023-12-08 00:12:05 +01:00
Vincent Thiberville cceb5019ba Fix segfault in math module when scanning a process that dies during scan (#2016)
* fix: check nullity of first block in math module

This module retrieves the first block of memory to do some validation on
parameters. However, contrary to for example the hash module, the block
value is not checked. This can lead, if for example scanning a process
and having the process die during the scan, to a SIGSEGV/Null access
violation, as the first block call will return NULL.

This bug manifests in surprising ways. Since yara runs by default
evaluation of condition in a try catch block, this bug is actually
caught by the try catch block, and a surprising "ERROR_COULD_NOT_MAP_FILE"
error is then returned.

In addition, since the try catch bypasses the whole stack, this leads to
a memory leak that can add up as more instances of this bug is triggered.

* fix: check first block in magic module

For the magic module, this bug (see previous commit) is less reachable,
since if the SCAN_FLAGS_PROCESS_MEMORY is set, we do not reach this
code. However, with a custom block iterator and without this flag, the
block value could be NULL.

* doc: update doc example on how to retrieve first memory block
2023-11-29 17:48:48 +01:00
TzeKei Lee 515ed861cf semaphore_destroy: use sem_destroy on POSIX (#2015)
per POSIX semaphore, sem_init should be paired with sem_destroy (unnamed
semaphore), sem_close should be paired with sem_open (named one).

While glibc seems forgiving about this, this will cause hangs or
segfaults with musl (for example, #1887).
2023-11-29 09:28:23 +01:00
Jacob Latonis 2f34978aaf docs: added global examples for count() and mode() (#2014)
I noticed a few of the functions that had global versions to search the whole file were missing examples, I have added them
2023-11-29 02:25:32 +01:00
Andras Gemes b67b6c6074 Add test-magic to .gitignore (#1986)
The existing filter, test-*[^.c], does not ignore filenames ending with
'c', so now a specific entry for test-magic is added.
2023-11-28 00:22:50 +01:00
Andras Gemes 65c2f48174 Fix broken blog link in base64.c (#1987) 2023-11-28 00:21:49 +01:00
Vincent Thiberville 8dfea8fddd fix: fix proc scanning on linux 32bits without LFS (#2005)
The commit dfd6f7b5f brought large file detection in the autoconf file
to fix process scanning on linux in x86 architecture. This is however a
bit flaky, because:
- process scanning won't work on systems that do not have LFS, which
  does not make sense (although I don't really know how autoconf detects
  it or not).
- the fix does not work if the compilation is done without the use of
  autoconf, and `_FILE_OFFSET_BITS` is not defined. This is for example
  the case for yara-rust, where the process scanning does not work on
  linux 32bits for this reason.

This breaks process scanning because without it, the pread function
on the mem file will fail if provided with an offset bigger than
INT32_MAX, which will always happen for processes.

A better fix is to use the pread64 syscall, which always takes a 64-bits
value for the offset. This is the actual linux syscall, so it is
completely safe.

See for example the exact same fix on chromium:

https://groups.google.com/a/chromium.org/g/crashpad-dev/c/IJLo8M2AQs0/m/Edyyau2LAwAJ
2023-11-28 00:21:00 +01:00
Vincent Thiberville 0b08989045 fix: properly check success of pagemap open (#2006)
The pagemap_fd value was not checked, probably a bad copy-paste from the
mem_fd open just above it.
2023-11-28 00:17:36 +01:00
Roman 4a9019836d Fix VS2019 build (string module) (#2009) 2023-11-28 00:16:57 +01:00
Victor M. Alvarez 72dc612b60 Add comment explaining what YY_FATAL_ERROR does
Thanks to @1ndahous3.
2023-11-28 00:16:22 +01:00
Victor M. Alvarez 330bc748e4 Fix issue #2007
By adding `%option nodefault` to flex input files (`.l`) the produced code don't use the ECHO macro.
2023-11-28 00:06:31 +01:00
Victor M. Alvarez 01032a6cb2 Fix bug while calculating the imphash in PE files.
When the file imported DLLs that contained a dot (.) in its name, like `glib-2.0.dll`, the file extension was not properly removed, causing the the imphash to be wrong.
2023-11-17 11:37:35 +01:00