VirusTotal / yara-python

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

Yara.Error Internal Error 58 #192

Closed abhisheksingh1234 closed 2 years ago

abhisheksingh1234 commented 2 years ago

I was using the below code which compiles Yara, saves it in the memory. It then loads it and scans the files, It was working fine with Python2.7

When I execute the same code with Python3 I get an error

Traceback (most recent call last): File "/home/yaraengine.py", line 24, in YaraInit rules.save(file=buff) yara.Error: internal error: 58

Were there any changes in saving Yara in memory?

yara-python==4.1.3 yara version is 4.0.2



import yara
import os
from io import StringIO
import traceback

buff = StringIO()

class YaraEngine():

    def YaraInit(self):

        try:
            dir_path = os.path.dirname(os.path.realpath(__file__))
            file_path = dir_path + "/rule.yara"
            fh = open(file_path)
            rules = yara.compile(file_path)
            fh.close()
            rules.save(file=buff)
        except Exception as e:
            traceback.print_exc()

    def executeYara(self,name):
        try:
            buff.seek(0)
            rule = yara.load(file=buff)
            matches = rule.match(name)
            return matches
        except Exception as e:
            traceback.print_exc()
~    ```                              
plusvic commented 2 years ago

Error 58 is ERROR_WRITING_FILE, so it looks like StringIO doesn't behave the same way in Python 2.7 and Python 3.x. In order to confirm that this issue is related to StringIO I would try using a real file and see what happens.

plusvic commented 2 years ago

According to the test cases in Python 3.x you may need to use io.BytesIO() instead of io.StringIO(). See this: https://github.com/VirusTotal/yara-python/blob/e58af70ec7280e69e8a4e447729812230921c0e1/tests.py#L1014-L1017

abhisheksingh1234 commented 2 years ago

Thank you it works.