import pefile
from capstone import Cs, CS_ARCH_X86, CS_MODE_64

exe = "GuessNumber.exe"

pe = pefile.PE(exe)

# nájdi kódovú sekciu
text = None

for section in pe.sections:
    name = section.Name.decode(errors="ignore").rstrip("\x00")

    print(
        name,
        "RVA=", hex(section.VirtualAddress),
        "RAW=", hex(section.PointerToRawData)
    )

    if name == ".text":
        text = section
        break

if text is None:
    raise Exception("Nenájdená .text sekcia")

# načítaj bajty sekcie
code = text.get_data()

# vytvor disassembler
md = Cs(
    CS_ARCH_X86,
    CS_MODE_64
)

# disassemblovanie
start_address = pe.OPTIONAL_HEADER.ImageBase + text.VirtualAddress

for insn in md.disasm(code, start_address):
    print(
        "0x%x:\t%s\t%s"
        % (
            insn.address,
            insn.mnemonic,
            insn.op_str
        )
    )
