48 Commits
Author SHA1 Message Date
Cole Munz 8a5a88e463 fix string leak in cli args_free (#2220)
args_parse zeroed every option's value pointer right before returning, so args_free's NULL check always skipped the free. Even without that, args_free was freeing the address of the caller's static variable (e.g. &ext_vars), not the string it points to.

On Windows (_UNICODE builds) each ARGS_OPT_STRING value is a heap copy from unicode_to_ansi(), so -d, -x, -i, atom-quality-table and module-data args leaked on every run. On non-Windows the value just aliases argv, nothing to free.

Removed the value-clearing loop in args_parse (nothing reads .value after parsing except args_free) and rewrote args_free to walk options->count entries and free the actual string pointers, gated on _UNICODE so it's a no-op elsewhere.
2026-07-06 09:43:47 +02:00
Cole Munz c125ac7fa1 cli: honor -w/--no-warnings for the file-too-large skip message (#2219)
scan_dir() prints "skipping <file> ... because it's larger than N
bytes" unconditionally on both the POSIX and Windows paths, ignoring
ignore_warnings even though every other warning in this file checks
it. Guard both prints the same way the other warnings already are.

Fixes #2137
2026-07-06 09:43:33 +02:00
Victor M. Alvarez 0f0c667c05 Fix typo 2025-09-05 09:46:39 +02:00
Victor M. Alvarez 1fb9cfdee5 Fix compilation issue due to wrong operator. 2025-09-05 09:24:23 +02:00
Victor M. Alvarez 6e11b5ae1f fix: implement the --no-follow-symlinks option in Windows.
Until now this option was working only on POSIX systems.
2025-09-03 16:47:17 +02:00
Victor M. Alvarez 2e64b4c12d fix: escape new line and carriage returns characters when printing file names.
Matches on files whose name contains such characters are not properly printed.
2025-05-22 18:51:07 +02:00
Victor M. Alvarez 2a9f61d484 Mitigate stack overflow when scanning very deep directory trees.
Closes #2088.
2024-08-01 11:42:01 +02:00
Victor M. Alvarez b68de47e82 Use YR_MAX_PATH instead of MAX_PATH (#2090)
Replace all instances of `MAX_PATH` with `YR_MAX_PATH`.
2024-08-01 11:29:48 +02:00
Victor M. Alvarez 923368eab3 Prevent infinite recursion while following symlinks.
Closes #2021.
2023-12-10 20:38:03 +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
Tomáš Ďuriš d7e67ff54b Added warning on unknown escape sequences (#1880)
* Added warning on unknown escape sequences

* fixed memory leaks and different output when using regex as operand with strict-escape parameter

* Remove some unwanted characters from strict escaping, add testcases
2023-08-23 15:26:43 +02:00
Mike Goffin 8f40272720 Add CLI flag for disabling console log printing. (#1915)
- Add CLI flag that will disable the console module from printing:
  Short: -q
  Log:   --disable-console-logs
- Update documentation with new CLI flags

Closes #1912
2023-08-18 13:29:59 +02:00
Dominika Regéciová 7f46c88815 Add callback for notifying about slow rules (#1921) 2023-05-25 21:20:46 +02:00
Victor M. Alvarez d408137c2b Fix issue #1896.
When `populate_scan_list` fails the program does not exit immediately, it first finishes the scan threads and destroy the scanners.
2023-03-17 11:58:10 +01:00
Wesley Shields 68d9b6559a Display plaintext when using the -X argument. (#1849)
* Make -X print the plaintext.

Make the -X argument print the plaintext along with the xor key.

Output looks like this:

```
wxs@mbp yara % cat rules/xor.yara
rule a {
  strings:
    $a = "This program cannot"
    $b = "This program cannot" xor(1-255)
  condition:
    any of them
}
wxs@mbp yara % ./yara -s -X rules/xor.yara tests/data/xorwideandascii.out | head -5
a tests/data/xorwideandascii.out
0x4:$a:xor(0x00,This program cannot): This program cannot
0x1c:$b:xor(0x01,This program cannot): Uihr!qsnfs`l!b`oonu
0x34:$b:xor(0x02,This program cannot): Vjkq"rpmepco"acllmv
0x4c:$b:xor(0x03,This program cannot): Wkjp#sqldqbn#`bmmlw
wxs@mbp yara % ./yara -s -X rules/xor.yara tests/data/xorwideandascii.out | tail -5
0x1878:$b:xor(0xfb,This program cannot): \xAF\x93\x92\x88\xDB\x8B\x89\x94\x9C\x89\x9A\x96\xDB\x98\x9A\x95\x95\x94\x8F
0x1891:$b:xor(0xfc,This program cannot): \xA8\x94\x95\x8F\xDC\x8C\x8E\x93\x9B\x8E\x9D\x91\xDC\x9F\x9D\x92\x92\x93\x88
0x18aa:$b:xor(0xfd,This program cannot): \xA9\x95\x94\x8E\xDD\x8D\x8F\x92\x9A\x8F\x9C\x90\xDD\x9E\x9C\x93\x93\x92\x89
0x18c3:$b:xor(0xfe,This program cannot): \xAA\x96\x97\x8D\xDE\x8E\x8C\x91\x99\x8C\x9F\x93\xDE\x9D\x9F\x90\x90\x91\x8A
0x18dc:$b:xor(0xff,This program cannot): \xAB\x97\x96\x8C\xDF\x8F\x8D\x90\x98\x8D\x9E\x92\xDF\x9C\x9E\x91\x91\x90\x8B
wxs@mbp yara %
```

* Update argument description.

* Fix #1851.

This is actually a combination of two bugs. The first bug is that if a string
fits in an atom we never calculated the xor key. The second bug is that if you
specify the xor modifier we were NOT setting the ascii flag on the string, so we
could not calculate the xor key properly one we fixed the first bug.

Tested with:

wxs@mbp yara % cat rules/xor.yara
rule a {
  strings:
    $c = "This" xor
  condition:
    any of them
}
wxs@mbp yara % ./yara -s -X rules/xor.yara tests/data/xorwideandascii.out | head -5
a tests/data/xorwideandascii.out
0x4:$c:xor(0x00,This): This
0x1c:$c:xor(0x01,This): Uihr
0x34:$c:xor(0x02,This): Vjkq
0x4c:$c:xor(0x03,This): Wkjp
wxs@mbp yara %

An interesting side effect of this bug is that you could also trigger it with a
"xor wide" combination of modifiers if the string was short enough to fit in an
atom, which is now also fixed.

wxs@mbp yara % cat rules/xor.yara
rule a {
  strings:
    $c = "Th" xor wide
  condition:
    any of them
}
wxs@mbp yara % ./yara -s -X rules/xor.yara tests/data/xorwideandascii.out | head -5
a tests/data/xorwideandascii.out
0x18f4:$c:xor(0x00,T\x00h\x00): T\x00h\x00
0x191f:$c:xor(0x01,T\x00h\x00): U\x01i\x01
0x194a:$c:xor(0x02,T\x00h\x00): V\x02j\x02
0x1975:$c:xor(0x03,T\x00h\x00): W\x03k\x03
wxs@mbp yara %

Noticed by @melomac, who provided a nice writeup to help point out that this
only happens on "short" strings (which led me to realize it was only on strings
that fit in an atom).

* Add test case for #1851.
2023-01-02 09:40:53 +01:00
Wesley Shields b4f33e2ce9 Print module names. (#1801)
* Print module names.

Add a -M option to the cli which will print the module names to stdout. As the
number of modules has grown it can become confusing to know what modules are
available, so this option will display them on stdout.

NOTE: I'm not sure I like how I implemented this. It seems hackish to expose an
API that just prints things on stdout but it felt even more hackish to walk the
yr_modules_table array from outside of the module code. ;)

Fixes #737.

* Expose access to modules in the API.

This adds a yr_modules_get_table to the libyara API so callers can get access to
the modules. This is currently only used for printing the available module names
in the yara cli. With this change it is now possible to expose this information
in yara-python too.
2022-10-14 10:25:32 +02:00
Marek Milkovič f64079b6c8 Fixed output of YARA on Windows when output is redirected (#1773)
According to the documentation of
[_setmode](https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/setmode?view=msvc-170#remarks),
stream should be flushed before it is switched to another mode. This was
causing issues when you redirected output of YARA on Windows to a pipe
or a file. stdout is line buffered when terminal is attached but
once the output is redirected, it's usually buffered by a certain buffer
size. That's how systems usually do it.
2022-08-24 09:29:47 +02:00
Wesley Shields 0c1cbeec13 XOR Values. (#1745)
* XOR Values.

When the xor modifier is used we have not displayed (or even kept) the xor key.
This diff adds a -X option to the CLI that will display the xor key. To do this
I am recording the xor key in _yr_scan_xor_compare() and _yr_scan_xor_wcompare()
and then populating that in the YR_MATCH structure. This way it is available to
the consumers of libyara to handle how they see fit.

I'll be adding support for exposing this in yara-python if this PR is accepted.

* xor key fixes.

Per suggestion from Victor, always display the xor key when -X is specified,
even if the string is not an xor string. This makes it more consistent to parse
with common tools because the number of fields will always be the same.

Also, specify that it is an xor key using "xor(0x01)" format.
2022-07-21 12:23:35 +02:00
Victor M. Alvarez 1b50330646 Implement the --skip-larger command-line option in Windows. (#1678)
Also allow a 64 bits integer as the argument to --skip-larger both in Linux and Windows.
2022-04-06 11:11:45 +02:00
Hilko Bengen 1f56e30000 Fix for MSVC builds (#1653)
* cli/yara: Replace printf with _tprintf in console output callback

This unbreaks output on MSVC-based builds as discussed in #1594

* cli/yara: Fix MSVC warning about incompatible pointers
2022-02-17 11:42:16 +01:00
Wesley Shields f7908b7e83 Fix issue with argument handling. (#1640)
There is an issue with argument handling that is causing issues when scanning
using certain argument combinations. I believe this is similar to
https://github.com/VirusTotal/yara/pull/927. Here's an example of it broken in
master:

wxs@wxs-mbp yara % cat rules/always_true.yara
rule a { condition: true }
wxs@wxs-mbp yara % ./yara -p 1 rules/always_true.yara /bin
wxs@wxs-mbp yara %

And the same thing with this fix:

wxs@wxs-mbp yara % ./yara -p 1 rules/always_true.yara /bin
    a /bin/cat
    a /bin/echo
    a /bin/launchctl
    a /bin/df
<snip>

Issue spotted by: Conor Quigley (https://github.com/schrodyn)
2022-02-01 09:57:02 +01:00
pwrdwnsys b9c91e55cf remove duplicate include for args.h (#1641) 2022-02-01 09:50:25 +01:00
Wesley Shields 723e3c2114 Add console module. (#1594)
Add a console module which can be used to print things during condition
execution. This is especially useful for debugging complex conditions where
you're following multiple offsets. For example, people sometimes want to express
a PE by doing this:

uint32(uint32(0x3c)) == 0x50450000

What they really want is:

uint32be(uint32(0x3c)) == 0x50450000

Or they want:

uint32(uint32(0x3c)) == 0x00004550

In either case it would be helpful if they could do:

console.hex(uint32(uint32(0x3c)))

Another example of where this could be useful is being able to know what YARA
thinks an imphash is just by doing:

console.log(pe.imphash())

* Make console module a default module.

* Add support for optional string messages.

Override each function so that it can also take an optional string that is
printed at the start of the message. Tested with the following:

import "pe"
import "math"
import "console"

rule a {
  condition:
    console.hex("0x3c: ", uint32(uint32(0x3c))) and
    console.hex(uint32(uint32(0x3c))) and
    console.log("String: ", "foo\x00bar\x01baz") and
    console.log("foo\x00bar\x01baz") and
    console.log("Some integer: ", 987654321) and
    console.log(987654321) and
    console.log("Some float: ", math.MEAN_BYTES) and
    console.log(math.MEAN_BYTES) and
    console.log("Imphash: ", pe.imphash()) and
    console.log(pe.imphash())
}

* Remove duplicate macro definitions.

* Make console functions use callback.

Add a CALLBACK_MSG_CONSOLE_LOG which the console module uses to send a message
to the main callback. Make the console module functions all generate a single
string which is then passed to the callback. This keeps the callback handler
simple: it just prints a string.

* Fix newlines.

* Add docs for console functionality.

* Add docs for console_callback to yara-python.

* Address comments from Victor.
2021-12-21 09:33:28 +01:00
Victor M. Alvarez a36b497926 Fix issue #1616 (#1621)
This is a more comprehensive fix than #1617, it adds new functions to the API for getting/setting uint32 and uint64 settings.  Using these functions is preferable over calling `yr_(get|set)_configuration` directly.
2021-12-20 10:31:12 +01:00
Victor M. Alvarez 3d520eddf2 Merge remote-tracking branch 'origin/master' 2021-11-18 10:30:40 +01:00
Victor M. Alvarez e21982a77f Fix issue #1593.
When specifying a namespace with an absolute file path (starting with /) the file was not being found as the namespace was used as part of the path.
2021-11-18 10:29:53 +01:00
Dominika Regéciová bbdfa4f6c7 fix loading module data (#1590) 2021-11-15 09:58:04 +01:00
Victor M. Alvarez 6284650806 Fix description for --skip-larger command-line option. 2021-10-29 15:35:14 +02:00
Victor M. Alvarez 99da5cd7fe Implement --skip-larger command-line option.
Based in #1211.
2021-10-29 12:08:45 +02:00
secDre4merandVictor M. Alvarez 605b2edf07 Add maximum size for process memory chunk (#1393)
Co-authored-by: Victor M. Alvarez <[email protected]>
2021-10-28 16:38:04 +02:00
Ladislav ZezulaandLadislav Zezula 658b689002 Workspaces needs to be set as UNICODE in order to work (#1557)
* Workspaces needs to be set as UNICODE in order to work

* Added consistency

Co-authored-by: Ladislav Zezula <[email protected]>
2021-08-30 11:04:06 +02:00
Victor M. Alvarez 95d8a4a9f5 Take into account that _tcstol can return a negative number, but a pid is always positive. 2021-08-19 10:09:46 +02:00
Victor M. Alvarez 006368be1c Fix style with clang-format. 2021-08-18 09:23:37 +02:00
Wesley Shields b27d80687e Fix edge case with pid check. (#1541)
A common use case (I've run into this many times, as have some friends) is to
run yara against a file named after the hash of the content. If the hash happens
to start with a number yara.c treats it as a PID, and will fail. The workaround
is to prefix the file with "./" (or whatever the equivalent is on Windows) so
that the strol() check fails.

This diff switches the behavior so it treats the argument as a file first, and
only if that can be opened is it then checked for a PID. Further, to ensure the
PID check is accurate I extended it so that the entire argument must be
converted. If there are any characters left after the conversion then it is not
a valid PID and we will not attempt to scan it as it is (by definition) not a
valid file nor a valid PID.
2021-08-18 09:21:34 +02:00
Victor M. Alvarez 7b5e0a31e7 Fix issue #1534 2021-08-09 22:33:08 +02:00
Victor M. Alvarez 8817f6cc34 Fix warning in Coverity.
The warning was caused by the use of strcmp with a string returned by readlink, which doesn't guarantee that the string is null-terminated. In this case the use of strcmp is not required as we are checking only one character.
2021-06-11 10:14:08 +02:00
Victor M. Alvarez 57057e86e8 Implement support for unicode file names in Windows. (#1491) 2021-05-11 13:00:30 +02:00
Victor M. Alvarez e1360f6cbe Fix issue #1481 (#1482) 2021-04-19 15:05:24 +02:00
Victor M. Alvarez b01d9ae56c Minor style changes. 2021-03-15 12:22:07 +01:00
Arnim Rupp 1239afcbd4 Update yara.c (#1453)
1. skip all symlinks pointing to . so you do "yara -r test.yar /usr/bin" on debian without -N (/usr/bin/X11 points to . )
2. Show 64 instead of 32 hex chars (regex are shown in full so there's enough clutter anyway)
2021-03-15 11:54:32 +01:00
Victor M. Alvarez bcb9567cdc Improve warnings (#1465)
Includes the following changes:

* Print the rule identifier in warnings due to strings matching too much. The string identifier is not enough, common identifiers like $a are not very informative.
* Use the same format for compile time warnings and scan time warnings.
* --no-warnings disables the "too many matches" warning.
* --fail-on-warnings makes yara fail with the "too many matches" warning.
* Use the same message format in yara and yarac.
2021-03-15 11:47:12 +01:00
Wesley Shields 95b43693dc Switch maximum string limit to warning. (#1350)
When we hit the maximum match limit on a string we currently abort all scanning,
which can be problematic since it isn't a good reason to stop all scanning, and
is better off as a warning to the user.

This commit makes it so that when the string match limit is hit a warning is
printed and scanning will continue. The warning message indicates that the
results may be invalid. It is hard to know for certain if they are invalid
because it depends entirely upon the condition. If you use the # or @ operator
then your condition _MAY_ be invalid since the total number of matches and
locations is not known anymore. Just checking that the string exists won't be an
invalid state since we have confirmed the existence of _AT LEAST_ the maximum
number of occurrences, which clearly satisfies the existence of a single
instance of your string.

I decided to implement this as a callback so the user of libyara has some choice
on the default behavior. If they want to consider this a "stop all scanning"
error they are free to return CALLBACK_ERROR in their callback.
2020-12-17 10:10:27 +01:00
Hilko Bengen 77b873ce6f Avoid strtok_s usage (#1383)
Apparently, the C standard library in older Windows versions does not
contain strtok_s (see discussiion for PR #1381). Since strtok_s is
only used in the single-threaded code of YARA itself, reentrancy is
not really needed.
2020-10-31 18:53:14 +01:00
Victor M. Alvarez fa5682a98d Homogenize function comments and remove empty lines. 2020-10-19 12:00:53 +02:00
Victor M. Alvarez 0bce4bd74d Start using clang-format for formatting the code automatically. 2020-10-07 16:27:30 +02:00
Victor M. Alvarez 54a7fc83f3 Fix issue while compiling in Windows.
The "recursive" argument has been replaced by "scan_opts->recursive_search" in #1349, but it wasn't changed in Windows-specific code.
2020-09-25 20:03:13 +02:00
Wesley Shields dc8f644b21 Implement -N (--no-follow-symlinks).
Implement a -N flag which causes yara to not follow any symlinks when scanning,
regardless of the recursive option.
2020-09-04 09:10:22 +02:00
Victor M. Alvarez 8d3f8259ad Move command line tools to their own directory. 2020-06-26 09:17:35 +02:00