Module refinery.lib.dotnet.disassembler

A library to disassemble .NET assembly

Expand source code Browse git
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
A library to disassemble .NET assembly
"""
from typing import Iterator, Optional

from refinery.lib.dotnet.disassembler.factory import InstructionFactory
from refinery.lib.dotnet.disassembler.model import Instruction, UnknownInstruction
from refinery.lib.dotnet.disassembler.repository import OpRepository


class Disassembler:
    def __init__(self):
        self._op_repository = OpRepository()
        self._factory = InstructionFactory()

    def disasm(self, data: bytes, max_byte_count: Optional[int] = None) -> Iterator[Instruction]:
        i = 0
        while i < len(data):
            if max_byte_count and i >= max_byte_count:
                break
            op = self._op_repository.lookup(data[i:])
            if op is None:
                raise UnknownInstruction(f"Found at {i}: {data[i:i + 8].hex()}...")
            if op.is_switch:
                ins = self._factory.switch(data[i:], i, op)
            else:
                ins = self._factory.create(data[i: i + len(op)], i, op)
            yield ins
            if op.fixed_length:
                assert len(op) == len(ins)
            i += len(ins)

Sub-modules

refinery.lib.dotnet.disassembler.factory
refinery.lib.dotnet.disassembler.model
refinery.lib.dotnet.disassembler.repository

https://en.wikipedia.org/wiki/List_of_CIL_instructions

Classes

class Disassembler
Expand source code Browse git
class Disassembler:
    def __init__(self):
        self._op_repository = OpRepository()
        self._factory = InstructionFactory()

    def disasm(self, data: bytes, max_byte_count: Optional[int] = None) -> Iterator[Instruction]:
        i = 0
        while i < len(data):
            if max_byte_count and i >= max_byte_count:
                break
            op = self._op_repository.lookup(data[i:])
            if op is None:
                raise UnknownInstruction(f"Found at {i}: {data[i:i + 8].hex()}...")
            if op.is_switch:
                ins = self._factory.switch(data[i:], i, op)
            else:
                ins = self._factory.create(data[i: i + len(op)], i, op)
            yield ins
            if op.fixed_length:
                assert len(op) == len(ins)
            i += len(ins)

Methods

def disasm(self, data, max_byte_count=None)
Expand source code Browse git
def disasm(self, data: bytes, max_byte_count: Optional[int] = None) -> Iterator[Instruction]:
    i = 0
    while i < len(data):
        if max_byte_count and i >= max_byte_count:
            break
        op = self._op_repository.lookup(data[i:])
        if op is None:
            raise UnknownInstruction(f"Found at {i}: {data[i:i + 8].hex()}...")
        if op.is_switch:
            ins = self._factory.switch(data[i:], i, op)
        else:
            ins = self._factory.create(data[i: i + len(op)], i, op)
        yield ins
        if op.fixed_length:
            assert len(op) == len(ins)
        i += len(ins)