packing-box / docker-packing-box

Docker image gathering packers and tools for making datasets of packed executables and training machine learning models for packing detection
GNU General Public License v3.0
49 stars 10 forks source link

`PatchError` of issue angr CLE with tinyscript #113

Closed AlexVanMechelen closed 7 months ago

AlexVanMechelen commented 7 months ago

Issue

Since the latest pbox-update I performed, I get an issue where the angr CLE patch with tinyscript isn't working anymore.

Traceback

After removing the try except pass around the patching code - here

00:00:04.796 [WARNING] Bad expression: len(binary['cfg']['nodes'])
00:00:04.797 [ERROR] Bad code additions (bad index from tinyscript.helpers import ensure_str)
Traceback (most recent call last):
  File "/home/user/.local/lib/python3.11/site-packages/pbox/core/executable/parsers/__common__.py", line 32, in __getitem__
    v = super().__getitem__(name)
        ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/.local/lib/python3.11/site-packages/pbox/helpers/mixins.py", line 20, in __getitem__
    return getattr(self, name)
           ^^^^^^^^^^^^^^^^^^^
  File "/home/user/.local/lib/python3.11/site-packages/pbox/core/executable/parsers/lief/__common__.py", line 67, in __getattr__
    return super().__getattribute__(name)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'PE' object has no attribute 'cfg'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/user/.local/lib/python3.11/site-packages/pbox/helpers/items.py", line 89, in _exec
    r = eval2(expr, d, {}, whitelist_nodes=WL_NODES + _WL_EXTRA_NODES)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/.local/lib/python3.11/site-packages/tinyscript/helpers/expressions.py", line 122, in eval2
    return __eval(expression, globals, locals, blacklist_builtins, whitelist_nodes)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/.local/lib/python3.11/site-packages/tinyscript/helpers/expressions.py", line 58, in __eval
    return eval(expr, globals, locals)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<string>", line 1, in <module>
  File "/home/user/.local/lib/python3.11/site-packages/pbox/core/executable/parsers/__common__.py", line 40, in __getitem__
    if hasattr(self, "path") and hasattr(self.path, name):
                                 ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/functools.py", line 1001, in __get__
    val = self.func(instance)
          ^^^^^^^^^^^^^^^^^^^
  File "/home/user/.local/lib/python3.11/site-packages/pbox/core/executable/__init__.py", line 245, in cfg
    from .cfg import CFG
  File "/home/user/.local/lib/python3.11/site-packages/pbox/core/executable/cfg/__init__.py", line 2, in <module>
    from .__common__ import *
  File "/home/user/.local/lib/python3.11/site-packages/pbox/core/executable/cfg/__common__.py", line 31, in <module>
    class CFG(GetItemMixin, ResetCachedPropertiesMixin):
  File "/home/user/.local/lib/python3.11/site-packages/pbox/core/executable/cfg/__common__.py", line 32, in CFG
    engines = {k: getattr(angr.engines, "UberEngine" if k in ["default", "vex"] else f"UberEngine{k.capitalize()}") \
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/.local/lib/python3.11/site-packages/pbox/core/executable/cfg/__common__.py", line 32, in <dictcomp>
    engines = {k: getattr(angr.engines, "UberEngine" if k in ["default", "vex"] else f"UberEngine{k.capitalize()}") \
                          ^^^^^^^^^^^^
  File "/home/user/.local/lib/python3.11/site-packages/tinyscript/__conf__.py", line 52, in _load
    postload()
  File "/home/user/.local/lib/python3.11/site-packages/pbox/core/executable/cfg/__common__.py", line 22, in __init_angr
    code.insert_line(PESection.__init__, "from tinyscript.helpers import ensure_str", 0)
  File "/home/user/.local/lib/python3.11/site-packages/tinyscript/preimports/codep.py", line 153, in code_add_line
    return code_add_lines(func, index, addition, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/.local/lib/python3.11/site-packages/tinyscript/preimports/codep.py", line 115, in _wrapper
    f(*args, **kwargs)
  File "/home/user/.local/lib/python3.11/site-packages/tinyscript/preimports/codep.py", line 170, in code_add_lines
    for n, a in __sort_int_text_pairs(old_code, additions, "addition"):
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/.local/lib/python3.11/site-packages/tinyscript/preimports/codep.py", line 84, in __sort_int_text_pairs
    raise PatchError(s + " (bad index {})".format(n))
tinyscript.preimports.codep.PatchError: Bad code additions (bad index from tinyscript.helpers import ensure_str)
AlexVanMechelen commented 7 months ago

The issue still persists. I don't have the rights to reopen this issue.

Traceback

After removing the try except pass around the patching code - here

>>> exe = Executable("/mnt/share/experiments/exp1/datasets/a/files/65fbb0b87e5ffb26095f35e7b27e4932d75fc377fd0a1a64d80a45340615940d")
>>> exe.cfg
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.11/functools.py", line 1001, in __get__
    val = self.func(instance)
          ^^^^^^^^^^^^^^^^^^^
  File "/home/user/.local/lib/python3.11/site-packages/pbox/core/executable/__init__.py", line 245, in cfg
    from .cfg import CFG
  File "/home/user/.local/lib/python3.11/site-packages/pbox/core/executable/cfg/__init__.py", line 2, in <module>
    from .__common__ import *
  File "/home/user/.local/lib/python3.11/site-packages/pbox/core/executable/cfg/__common__.py", line 31, in <module>
    class CFG(GetItemMixin, ResetCachedPropertiesMixin):
  File "/home/user/.local/lib/python3.11/site-packages/pbox/core/executable/cfg/__common__.py", line 32, in CFG
    engines = {k: getattr(angr.engines, "UberEngine" if k in ["default", "vex"] else f"UberEngine{k.capitalize()}") \
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/.local/lib/python3.11/site-packages/pbox/core/executable/cfg/__common__.py", line 32, in <dictcomp>
    engines = {k: getattr(angr.engines, "UberEngine" if k in ["default", "vex"] else f"UberEngine{k.capitalize()}") \
                          ^^^^^^^^^^^^
  File "/home/user/.local/lib/python3.11/site-packages/tinyscript/__conf__.py", line 52, in _load
    postload()
  File "/home/user/.local/lib/python3.11/site-packages/pbox/core/executable/cfg/__common__.py", line 22, in __init_angr
    code.insert_line(PESection.__init__, 0, "from tinyscript.helpers import ensure_str")
  File "/home/user/.local/lib/python3.11/site-packages/tinyscript/preimports/codep.py", line 153, in code_add_line
    return code_add_lines(func, index, addition, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/.local/lib/python3.11/site-packages/tinyscript/preimports/codep.py", line 115, in _wrapper
    f(*args, **kwargs)
  File "/home/user/.local/lib/python3.11/site-packages/tinyscript/preimports/codep.py", line 180, in code_add_lines
    return __apply_code(func, old_code, new_code)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/.local/lib/python3.11/site-packages/tinyscript/preimports/codep.py", line 36, in __apply_code
    patchy.api._set_source(func, new_code)
  File "/home/user/.local/lib/python3.11/site-packages/patchy/api.py", line 305, in _set_source
    new_source = _process_method()
                 ^^^^^^^^^^^^^^^^^
  File "/home/user/.local/lib/python3.11/site-packages/patchy/api.py", line 277, in _process_method
    _def, _ast, fv_body = _process_freevars()
                          ^^^^^^^^^^^^^^^^^^^
  File "/home/user/.local/lib/python3.11/site-packages/patchy/api.py", line 260, in _process_freevars
    _ast.body = _ast.body + fv_force_use  # type: ignore [attr-defined]
                ^^^^^^^^^
AttributeError: 'ImportFrom' object has no attribute 'body'