Unverified Commit 65378d4d by Wesley Shields Committed by GitHub

Consolidate PRs into single branch (#219)

* Support xor_value in returned strings.

Extend the tuple that represents an instance of a match to include the xor key.
This breaks all existing scripts that are unpacking the tuple, which I'm not
very happy with.

This also updates the submodule to use the latest master so that I can get the
new xor key values.

Also, adds a fix to get yara building here by defining BUCKETS_128 and
CHECKSUM_1B as needed by the new tlsh stuff (discussed with @metthal).

* Add two new objects to yara-python.

Add a StringMatch object, which represents a matched string. It has an
identifier member (this is the string identifier, eg: $a) and an instances
member which contains a list of matched string instances.

It also keeps track of the string flags internally but does not expose them
directly as the string flags contain things that are internal to YARA (eg:
STRING_FLAGS_FITS_IN_ATOM). The reason it keeps track of the string modifiers
is so that it can be extended to allow users to take action based upon certain
flags. For example, there is a "is_xor()" member on StringMatch which will
return True if the string is using the xor modifier. This way users can call
another method (discussed below) to get the plaintext string back.

Add a StringMatchInstance object which represents an instance of a matched
string. It contains the offset, matched data and the xor key used to match the
string (this is ALWAYS set, even to 0 if the string is not an xor string).

There is a "plaintext()" method on the StringMatchInstance objects which will
return a new bytes object with the xor key applied. This allows users to do
something like this:

```
print(instance.plaintext() if string.is_xor() else instance.matched_data)
```

Technically, the plaintext() method will return the matched_data if the xor_key
is 0 so they don't need to do the conditional but this allows them a nice way to
know if the xor_key is worth recording along with the plaintext.

I decided not to implement richcompare for these new objects as it isn't
entirely clear what I would want to do the comparison on.

* Add "matched_length" member.

Add a "matched_length" member to match instances. This is useful when the
"matched_data" member is a subset of the actually matched data.

Add a test for this that sets the max_match_data config to 2 and then checks to
make sure the "matched_length" and "matched_data" members are correct.

* Add modules list to yara object.

Add support for getting the list of available modules. It is available just by
accessing the yara.modules attribute, which contains a list of available
modules.

>>> print('\n'.join(yara.modules))
tests
pe
elf
math
time
console
>>>

Note: This commit also brings in the necessary defines to build the authenticode
parser, which is also done in the xor_value branch. Also, this commit updates
the yara submodule which will likely overwrite the changes done in the xor_value
so I recommend updating the submodule after both are merged.

* Update yara to 65feab41d4cbf4a75338561d8506fc1fa9fa6ba6.

* Fix test using \t in a regex.

* Fix build on Windows in appveyor.

* Actually fix appveyor builds on windows?
parent 42ccdd39
......@@ -155,7 +155,7 @@ build_script:
- "%CMD_IN_ENV% python setup.py build_ext --enable-cuckoo --enable-dotnet
-L../jansson-%JANSSON_VERSION%/build/lib/Release;../openssl/lib
-I../jansson-%JANSSON_VERSION%/build/include;../openssl/include
-DHASH_MODULE,HAVE_LIBCRYPTO
-DHASH_MODULE,HAVE_LIBCRYPTO,BUCKETS_128,CHECKSUM_1B
-llibcrypto"
after_build:
......
......@@ -188,6 +188,12 @@ class BuildExtCommand(build_ext):
exclusions = []
# Needed to build tlsh
module.define_macros.extend([('BUCKETS_128', 1), ('CHECKSUM_1B', 1)])
# Needed to build authenticode parser
module.libraries.append('ssl')
for define in self.define or []:
module.define_macros.append(define)
......@@ -371,4 +377,5 @@ setup(
ext_modules=[Extension(
name='yara',
include_dirs=['yara/libyara/include', 'yara/libyara/', '.'],
define_macros=[('BUCKETS_128', 1), ('CHECKSUM_1B', 1)],
sources=['yara-python.c'])])
......@@ -306,11 +306,12 @@ class TestYara(unittest.TestCase):
matches = rule.match(data=string)
if expected_result == SUCCEED:
self.assertTrue(matches)
_, _, matching_string = matches[0].strings[0]
matching_string = matches[0].strings[0]
instance = matching_string.instances[0]
if sys.version_info[0] >= 3:
self.assertTrue(matching_string == bytes(test[3], 'utf-8'))
self.assertTrue(instance.matched_data == bytes(test[3], 'utf-8'))
else:
self.assertTrue(matching_string == test[3])
self.assertTrue(instance.matched_data == test[3])
else:
self.assertFalse(matches)
......@@ -559,9 +560,13 @@ class TestYara(unittest.TestCase):
matches = rules.match(data='abbb')
if sys.version_info[0] >= 3:
self.assertTrue(matches[0].strings == [(0, '$a', bytes('ab', 'utf-8'))])
self.assertTrue(matches[0].strings[0].identifier == '$a')
self.assertTrue(matches[0].strings[0].instances[0].offset == 0)
self.assertTrue(matches[0].strings[0].instances[0].matched_data == bytes('ab', 'utf-8'))
else:
self.assertTrue(matches[0].strings == [(0, '$a', 'ab')])
self.assertTrue(matches[0].strings[0].identifier == '$a')
self.assertTrue(matches[0].strings[0].instances[0].offset == 0)
self.assertTrue(matches[0].strings[0].instances[0].matched_data == 'ab')
def testCount(self):
......@@ -650,6 +655,58 @@ class TestYara(unittest.TestCase):
'rule test { strings: $a = "ssi" condition: for all i in (1..#a) : (@a[i] == 5) }',
], 'mississipi')
def testXorKey(self):
global rule_data
rule_data = None
def callback(data):
global rule_data
rule_data = data
return yara.CALLBACK_CONTINUE
r = yara.compile(source='rule test { strings: $a = "dummy" xor(1-2) condition: $a }')
r.match(data='etllxfwoo{', callback=callback)
self.assertTrue(rule_data['matches'])
self.assertEqual(rule_data['rule'], 'test')
self.assertEqual(len(rule_data['strings']), 1)
string = rule_data['strings'][0]
self.assertEqual(len(string.instances), 2)
self.assertEqual(string.instances[0].xor_key, 1)
self.assertEqual(string.instances[1].xor_key, 2)
# Make sure plaintext() works.
self.assertTrue(string.instances[0].plaintext() == b'dummy')
# Test that the xor_key for matched strings is 0 if the string is not an xor
# string. We always want to make sure this is set!
def testXorKeyNoXorString(self):
global rule_data
rule_data = None
def callback(data):
global rule_data
rule_data = data
return yara.CALLBACK_CONTINUE
r = yara.compile(source='rule test { strings: $a = "dummy" condition: $a }')
r.match(data='dummy', callback=callback)
self.assertTrue(rule_data['matches'])
self.assertEqual(rule_data['rule'],'test')
self.assertEqual(len(rule_data['strings']), 1)
self.assertEqual(rule_data['strings'][0].instances[0].xor_key, 0)
def testMatchedLength(self):
yara.set_config(max_match_data=2)
r = yara.compile(source='rule test { strings: $a = "dummy" condition: $a }')
matches = r.match(data='dummy')
self.assertEqual(matches[0].strings[0].instances[0].matched_length, 5)
self.assertEqual(matches[0].strings[0].instances[0].matched_data, b'du')
yara.set_config(max_match_data=512)
def testRE(self):
self.assertTrueRules([
......@@ -661,8 +718,8 @@ class TestYara(unittest.TestCase):
'rule test { strings: $a = /(M|N)iss/ nocase condition: $a }',
'rule test { strings: $a = /[M-N]iss/ nocase condition: $a }',
'rule test { strings: $a = /(Mi|ssi)ssippi/ nocase condition: $a }',
'rule test { strings: $a = /ppi\tmi/ condition: $a }',
r'rule test { strings: $a = /ppi\.mi/ condition: $a }',
r'rule test { strings: $a = /ppi\tmi/ condition: $a }',
'rule test { strings: $a = /ppi\.mi/ condition: $a }',
'rule test { strings: $a = /^mississippi/ fullword condition: $a }',
'rule test { strings: $a = /mississippi.*mississippi$/s condition: $a }',
], 'mississippi\tmississippi.mississippi\nmississippi')
......
yara @ 65feab41
Subproject commit b77e4f45b4662af320c999d4ee559e1f3bc61226
Subproject commit 65feab41d4cbf4a75338561d8506fc1fa9fa6ba6
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment