VirusTotal / yara-python

The Python interface for YARA
http://virustotal.github.io/yara/
Apache License 2.0
646 stars 179 forks source link

is_dll() function from pe module is not working properly on yara-python versions >= 4.0.0 (up to current 4.0.4) #171

Closed N0fix closed 3 years ago

N0fix commented 3 years ago

Hello,

I have noticed that is_dll() function in pe module from yara project is not working properly on yara-python from version 4.0.0 up to current one (4.0.4).

It seems like for the same version of yara-python and yara (the standalone executable), different results occur when using this specific function.

Reproducible example

$ yara -v 
4.0.2
$ cat dummy.yar
import "pe"
rule match_dll {
  condition: pe.is_dll()
}
$ yara -r dummy.yar kernel32.dll # any dll should give the same result
match_dll kernel32.dll
$ python3.8 -m pip install yara-python==4.0.2
#[installation]
$ cat test_script.py                                                      ~
import yara

r = yara.compile(source="""
import "pe" 
rule a{ 
    condition: pe.is_dll() 
}""")
print(r.match("kernel32.dll")) 
$ python3.8 test_script.py
[] # this should be returning a match

I got the kernel32.dll file used for the example from here. Its SHA256 shasum is : 0b2e367d671073efd70641a198c340b12b1bd813263050ae16b9c48b414775e0.

Further notes

I have been checking version number inside the library used by yara-python after installation (strings -a /home/myuser/myvenv/lib/python3.8/site-packages/yara.cpython-38-x86_64-linux-gnu.so | grep -E '4\.[0-9]' ) and the version of the library used by yara, and they match (4.0.2).
This behavior has been noticed on other computers too, and is not distribution related from what I could test. I tried to install yara in various ways (source code & distribution's package installer), and both confirm the behavior stated above.

After some testing with yara-python different versions packaged to pip, I noticed that this behavior start happening after version 4.0.0 included.

Cheers, Nofix

wxsBSD commented 3 years ago

Thanks for the detailed repro. I'll investigate this tonight.

N0fix commented 3 years ago

Further investigation might indicate that the problem is located here. The characteristics field would not be retrieved (returning YR_UNDEFINED), thus leading to the flag FILE_IS_DLL not to be read.

wxsBSD commented 3 years ago

I think you're close but probably not for the right reasons. I can replicate this problem. Here's what I've discovered so far:

I'm tired tonight, I'll keep digging tomorrow night.

plusvic commented 3 years ago

I found out that if you change the order of include statements in pe.c it works fine. Including yara/modules.h before yara/pe.h is enough to fix it, but didn't find the root cause yet.

wxsBSD commented 3 years ago

@metthal mentioned that this might be related to https://github.com/VirusTotal/yara-python/pull/159/ and he is right. I checked out 4fa2fe8 and then manually applied his patch to it and it's working as expected.

N0fix commented 3 years ago

Closing this issue since #159 has been accepted and merged into master. 4.0.5 seems to be working perfectly well. Thanks for your researches to find the source of this bug.