cea-sec / miasm

Reverse engineering framework in Python
https://miasm.re/
GNU General Public License v2.0
3.44k stars 470 forks source link

[Question] Dynamic architecture registration #619

Open guedou opened 6 years ago

guedou commented 6 years ago

I would like to dynamically add a new architecture into Machine, and found this comment while reading the source code.

Do you think that dynamic registration can be implemented, or that's a dead end due to performance issues ?

serpilliere commented 6 years ago

Yes! That's interesting. I will see with @commial some details on the way to implement this: In some Miasm parts, we used an "AbstractMethod" raising in order to enforce the sub class implementations, something like:

raise NotImplementedError("Abstract")
guedou commented 6 years ago

I can implement it if you tell what to do =)

commial commented 6 years ago

Hi @guedou,

Two things here:

def set_x86_16(machine): from ... import ... machine._ir = ...

class Machine(object): architectures = {"x86_16": set_x86_16, "x86_32": set_x86_32, ... }

def __init__(self, arch_name):
    cb = self.architectures.get(arch_name, None)
    if cb is None:
        raise ValueError("...")
    cb(self)

Further add of a new arch

Machine.architectures["my_awesome_arch"] = set_my_awesome_arch

or
```Python
class Machine(object):

    @classmethod
    def register(arch, callback):
        ...

Machine.register("x86_16", ...)
Machine.register("x86_32", ...)

# Further add of a new arch
Machine.register("my_awesome_arch", set_my_awesome_arch)

Is that what you requested?

guedou commented 6 years ago

This is exactly what I meant =)

The main use case will be to add an architecture by adding files into miasm2/arch/NEW_ARCH without modifying anything else. Moreover, it will also be possible to maintain an architecture as an independent Python module, without including the whole miasm source.

commial commented 6 years ago

Your second use case is what I had in mind while writing comments above. Regarding the first use case, we may consider that if an arch is landing in Miasm, you can also modify a few other part. You'll also probably need to modify sandbox, jitter,... In addition, you'll probably end with a weird import tree: Machine from the new arch to add the callback, the architecture from the callback, others architectures from the Machine module.

That being said, a clean separation between architecture specifics and the rest is still a good objective. But some prior work should be done before and is probably not related with this PR