Commit Graph
298 Commits
Author SHA1 Message Date
HoundTheandVictor M. Alvarez b9cd46df15 Process Authenticode signatures using authenticode-parser (#1623)
* Import authenticode-parser into Yara. Rewrite PE signature processing using the authenticode-parser. Update bazel OpenSSL dependency to 1.1.1 from 1.1.0

* Forgot to include structs.c

* Add Avast to authors and myself to contributors

* Set forgotten algorithm_oid

* Fix incorrect indentation

* Fix race condition when initializing openssl objects

* Don't malloc ByteArray with length 0

* Auth. parser update - bug fixes, openssl3.0 support

* Update authenticode parser

Co-authored-by: Victor M. Alvarez <[email protected]>
2022-06-20 12:47:36 +02:00
HoundThe 19ac2efeb8 Integrate tlshc library, calculate telfhash and import hash in ELF module (#1624)
* Add simple_str string implementation

* Integrate tlshc library, calculate telfhash and import hash in the ELF module.

* Hide usage of MD5 into #ifdef crypto
2022-06-20 12:25:33 +02:00
Wesley Shields ee6b2363f7 Add docs about warnings member for python. (#1711) 2022-05-19 23:05:01 +02:00
Victor M. Alvarez 1fe26dcf78 Fix issue in documentation.
Indicate that the `match` method in `yara-python` accepts both a `str` and `bytes`.
2022-05-19 10:42:56 +02:00
Wesley Shields a5903e1d24 Fix some compiler edge cases. (#1709)
* Fix some compiler edge cases.

Add a warning when "0 of them" is used. Point to some new documentation that
explains why "none of them" is preferred.

Handle some edge cases, which are all now errors:

* -1 of them
* "foo" of them
* /foo/ of them

And this one is also an error if the external variable "x" is a negative integer
or a string:

* x of them

I've also made sure this works with a construct like "0 + x of them" where x is
defined as a negative integer.

Add test cases for as many of these as I can. I don't have test cases for the
external variable case, but it does work as expected. Here is the output of a
rule using external variables where the variable is defined to be 1, 0, -1 and
foo:

```
wxs@mbp yara % cat rules/test.yara
rule a {
  strings:
    $a = "FreeBSD"
  condition:
    x of them
}
wxs@mbp yara % ./yara -d x=1 rules/test.yara /bin/ls
a /bin/ls
wxs@mbp yara % ./yara -d x=0 rules/test.yara /bin/ls
warning: rule "a" in rules/test.yara(5): Consider using "none" keyword, it is less ambiguous. Please see https://yara.readthedocs.io/en/stable/writingrules.html#sets-of-strings-1 for an explanation.
wxs@mbp yara % ./yara -d x=-1 rules/test.yara /bin/ls
error: rule "a" in rules/test.yara(5): invalid value in condition: "-1"
wxs@mbp yara % ./yara -d x=foo rules/test.yara /bin/ls
error: rule "a" in rules/test.yara(5): invalid value in condition: "string in for_expression is invalid"
wxs@mbp yara %
```

* More compiler warnings and errors.

If you specify "2 of ($a)" it is now a warning because it will always evaluate
to false, and is quite possibly not what you wanted to write. This is true for
string sets, rule sets and string sets in a range like "2 of ($a) in (0..10)".

If you are using an integer range it is now an error if the lower bound is
greater than the upper bound.

To support rule sets I needed to modify yr_parser_emit_pushes_for_rules() to
use a count parameter, which it will update with the number of pushed rules.
This is identical to how yr_parser_emit_pushes_for_strings() works.

While I'm here, remove the link from the warning message for the "none" keyword.
2022-05-17 18:20:57 +02:00
Victor M. Alvarez 8df8794a5c Fix some typos in documentation. 2022-04-29 22:22:26 +02:00
Wesley Shields e12b889c37 Minor fixes in documentation for ~ operator in hex strings. (#1689) 2022-04-21 17:32:06 +02:00
Shane Huntley 2f96c5ab22 Allow hexstring not operator to be used with wild-cards (#1680) 2022-04-15 12:33:21 +02:00
Shane Huntley 2f6dd0144b Add a "not" operator for hex bytes (#1676)
* Add not operator (~) for bytes in hex strings.

* Describe not hexstring not operator in documentation.

* Basic tests for the hextring NOT operator

* Fix not operator documentation ordering
2022-04-06 11:17:11 +02:00
Wesley Shields f7c66a402b Fix docs for console callback with python. (#1659) 2022-03-07 10:33:03 +01:00
Victor M. Alvarez ebc46daca2 Convert is_dotnet() function back to a field.
This is done for consistency with pe.is_pe, which is a field instead of a function. The initial idea was adding the is_pe() function to the pe module, but that's not possible because a function and a field can not have the same name, and is too late to remove the is_pe field.
2022-02-25 12:09:37 +01:00
HoundThe d6dc91b8f0 Add function pe.is_dotnet() for .NET file detection (#1568) 2022-02-25 11:08:22 +01:00
Jimmy Miller 6e66c01d0a Fix cuckoosandbox url (#1648)
`https://www.cuckoosandbox.org` has a cert error. But `http://cuckoosandbox.org` works just fine.
2022-02-14 09:54:33 +01:00
Wesley Shields 1dab22e9f1 Fix mistake in console docs. (#1644) 2022-02-07 10:18:45 +01:00
Victor M. Alvarez dd6fdf29e0 Define operator precedence in grammar.y exactly as documented.
Add some more test cases for "defined" operator and generate grammar with Bison 3.8.2.
2022-01-12 17:08:36 +01:00
Magno Logan d5db84f2e0 Update gettingstarted.rst (#1601) 2022-01-10 17:14:43 +01:00
John 13044819cf Update docs to show folders that modules are in (#1626)
Old documentation referenced ".c" files that were all in the same folder. However, later versions of YARA have them each in their own folder. Updating the docs to reflect this will mean there is no confusion when trying to create new modules.

Currently adding in `MODULES += modules/demo.c` won't work either, so this will make it easier for people to test the demo module.
2022-01-10 15:40:07 +01:00
Wesley Shields 7fabd955ab Implement rule sets. (#1597)
This adds the ability to use rule sets just like we do with string sets.
Specifically, it enables conditions like the following:

5 of (a, b)
50% of (a*)

where "a" is an existing rule and "b*" is a wildcard rule identifier, which is
expanded out precisely as wildcard string identifiers are.

I chose to make OP_OF and OP_OF_PERCENT emit an argument which is the "type" of
set it is expected to match on. The two acceptable values are OF_STRING_SET and
OF_RULE_SET. When OF_STRING_SET is used then the references on the stack are
expected to be strings and OF_RULE_SET will check if the rules on the stack
match.

I added a yr_parser_emit_pushes_for_rules() which behaves similarly to the
string counterpart. The one big exception is that I can't use yr_rules_foreach
macro since that needs the terminating "null" rule which is not yet added, so
instead I have to iterate the rules manually using yr_arena_get_ptr() to walk
all the currently compiled rules.

* Stricter ordering of rule set in compiler.

Make sure that when we compile a new rule that any existing rule sets do not
match the new rule. Specifically we are making the following a compiler error:

rule a1 { condition: true }
rule b { condition: 1 of (a*) }
rule a2 { condition: true }

This is a compiler error because "a2" is defined after the usage of (a*) in rule
b.

There is still an edge case here. These rules compile:

rule a { condition 1 of (a) }
rule b { condition: 1 of (b*) }

In both cases those rules evaluate to false, which is in line with the behavior
of this rule:

rule c { condition: c }

Given that the behavior around rule sets matches the current behavior I think
this is acceptable.

* Add docs for rule sets.

* Implement hash table iterators.

* Remove old comment about linked list.

* Move variable declaration.

* Iterate over rules better.
2021-12-21 12:46:33 +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 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
Victor M. Alvarez 09d095b1c8 Bump version number to 4.2.0. 2021-10-28 11:29:09 +02:00
xbabka01 dc4e0096c2 Added unary operation defined (#1529)
YARA didn't support any expression which would check value against YR_UNDEFINED constant so checking for non-existence of something can be very problematic. But we run multiple times into issue of people asking us for checking of absence of something.

Because of this we propose operator defined which would be unary operator (like not) which works like this:

defined x evaluates to

true - whenever x != YR_UNDEFINED
false - whenever x == YR_UNDEFINED

Related to issue #835
2021-10-21 10:41:40 +02:00
xbabka01 5cc28d24a2 Add parsing of '/N' section names (#1560) 2021-09-17 09:24:59 +02:00
Wesley Shields e71801644f Implement OP_COUNT_IN. (#1565)
* Implement OP_COUNT_IN.

Add a new instruction (OP_COUNT_IN) which is used to check the number of
occurrences of a string in a range. You can use this with the following syntax:

 #a in (0..100) == 2

This will check that the $a string matches exactly 2 times in the range
provided.

* Add docs for counting strings in a range.
2021-09-16 15:16:33 +02:00
secDre4merandFlorian Roth 0d5f2cd5ef Add additional functions to math module (#1483)
* feat: Implement stats module

Add a new statistics module. For now, the module supports counting
the occurrences of specific bytes (or returning the occurrence rate
as a percentage) and calculating the most common byte for an area.

* Another example

* feat: Implement stats module

Add a new statistics module. For now, the module supports counting
the occurrences of specific bytes (or returning the occurrence rate
as a percentage) and calculating the most common byte for an area.

* refactor: Integrate stats module into math module

Integrate the new functionality into the math module instead
of creating a new module.

* chore: Adjust module name in documentation

* chore: Fix memory leak issue

Fixes a memory leak where a temporary array (used for match counting)
wasn't released properly.

* fix: Remove global char count function, optimize char count

Remove a redundant (and slow) function to count a char globally.
Rework the remaining char count function to be more efficient.
It still requires O(n*m) in worst case and O(n) in best case.

* chore: Update documentation

* chore: Remove string counting function

Due to the new "x of them in ..." syntax, string counting is no
longer required (it already is supported and this function is
slower in most circumstances).

* chore: remove test functions for string count

Co-authored-by: Florian Roth <[email protected]>
2021-09-15 10:42:33 +02:00
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
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
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
xbabka01 b8650dfa05 Add imports_details and delayed_imports_details (#1523) 2021-08-20 09:42:57 +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 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
Steven 60b1b4d4e6 Add warnings_callbacks to Python Rules class summary (#1517)
Spelling corrections
Consistent indents in documentation
2021-08-09 11:50:26 +02:00
KashiwabaYuki 38ed07aaf2 Fix table syntax of writingrules.rsf (#1488)
I fixed syntax of table in writingrules.rst
・“YARA keywords” table has syntax error, and it table couldn't see on the document page.
・I changed that some '*' positions.
2021-07-09 23:51:47 +02:00
KashiwabaYuki 3c93ee1951 Fixed reference URL for Base64. (#1497)
The reference page for Base64 can't use to jump.
Because that URL already changed.
I fixed this URL to "https://www.leeholmes.com/searching-for-content-in-base-64-strings/".
2021-07-09 23:51:31 +02:00
KashiwabaYuki 946ad9b0f2 Add "EM_NONE" at ELF module MACHINE reference (#1513)
I know "EM_NONE" is difined in elf.h.
(https://github.com/VirusTotal/yara/blob/e1360f6cbe3d8daf350018661bc6772bd5b726f2/libyara/include/yara/elf.h)
However this doc has not "EM_NONE".
2021-07-09 23:46:32 +02:00
Victor M. Alvarez c937f4770a Remove incomplete sentence from documentation. 2021-07-05 10:58:28 +02:00
Victor M. Alvarez 0c84e37e24 Use double quotation marks. 2021-06-23 13:23:51 +02:00
Victor M. Alvarez 9e497d2d9a Improve documentation for regular expressions.
Closes #1516
2021-06-23 13:22:28 +02:00
Marek Milkovičandmalvidin 8643a577b5 Updated documentation of new callback data for CALLBACK_TOO_MANY_MATCHES (#1519)
Co-authored-by: malvidin <[email protected]>
2021-06-21 10:44:53 +02:00
Victor M. Alvarez 2cf9899d9d Fix typo. 2021-05-24 11:13:52 +02:00
Victor M. Alvarez 8b864f89de Some improvements to the documentation as suggested in #1503.
Closes #1503.
2021-05-24 09:58:34 +02:00
Victor M. Alvarez 1842271119 Add clarification about async iterations for YR_MEMORY_BLOCK_ITERATOR. 2021-04-15 12:48:43 +02:00
Victor M. Alvarez f749175f54 Rename warning_callback to warnings_callback for coherency with modules_callback. 2021-03-16 10:18:53 +01:00
Victor M. Alvarez 7588ed630f Rename tonumber function to to_number, for consistency with other names. 2021-03-15 12:38:53 +01:00
LiYang 2edd10c735 add math.tonumber function to convert bool to number (#1450)
more data types can be supported if needed, for example
string to number, etc.
2021-03-15 12:36:14 +01:00
Wesley Shields f25f12839a Add docs for yr_scanner_last_error_rule() and yr_scanner_last_error_string(). (#1449) 2021-03-15 12:23:24 +01:00