Below are the two “out-of-the-box” sandboxes Miasm currently provides:
A Linux‐style sandbox that emulates syscalls for ELF binaries
A Win32/Win64 sandbox that uses vm_load_pe_and_dependencies under the hood
You can also roll your own “sandbox” by combining a Machine + JIT + linux_env or win_env, but the ready-made classes save you a few boilerplate lines.
1) Linux sandbox (ELF + syscalls)
python
from miasm.analysis.sandbox import SandboxLinux32, SandboxLinux64
# pick 32 or 64 bit
sb = SandboxLinux32(
backend="unicorn", # "python" is pure-python, slower but always available
verbose=True, # print loader messages
syscall_verbose=False # print each syscall as it's invoked
)
# load an ELF + dependencies (via ldd lookup)
entry_va = sb.load_elf("/bin/ls")
# run until the guest does exit(…) / brk() / unmapped access
sb.run()
print("Guest exited with code", sb.exit_code)
# you can also inspect sb.stdout, sb.stderr
Options you can tweak in the constructor:
· backend – “python” / “unicorn” / “qemu”
· verbose – dump mmap/syscall messages
· syscall_verbose – log each syscall + args
· ld_library_paths – list of extra dirs for shared objects
2) Windows sandbox (PE + WinAPI stubs)
python
from miasm.analysis.sandbox import SandboxPE32, SandboxPE64
# choose 32 or 64 bit Win32/Win64
sb = SandboxPE32(
backend="python", # or "unicorn"/"qemu"
verbose=True, # show each DLL mapped
)
# load the EXE + all its DLL tree
entry_va = sb.load_pe("notepad.exe")
# (optionally) override or hook some WinAPI before run()
@sb.hook_winapi("user32.dll!MessageBoxA")
def on_MessageBoxA(jitter):
hwnd, text_ptr, cap_ptr, flags = jitter.func_args_stdcall(
["hWnd", "lpText", "lpCaption", "uType"]
)
text = jitter.vm.get_str(text_ptr)
print("MessageBoxA called with text:", text)
jitter.func_ret_stdcall(0) # return 0
return True # continue execution
# give it a stack (if you didn’t do sb.init_stack() in the constructor)
sb.init_stack(0x20000)
# jump to entry and run
sb.run()
print("Sandbox exit code:", sb.exit_code)
3) DIY sandbox
If you need more control you can just compose:
python
from miasm.analysis.machine import Machine
from miasm.os_dep.linux_env import linux_env_x86_64
# … or …
from miasm.os_dep.win_env import win_env_x86_32
from miasm.os_dep.win_pe import vm_load_pe_and_dependencies
# 1) create Machine + JIT
machine = Machine("x86_64")
jitter = machine.jitter("unicorn")
# 2) give it a stack & optional hooks
jitter.init_stack()
# ... add breakpoints or custom hooks ...
# 3a) Linux ELF
env = linux_env_x86_64(jitter)
# mmap your ELF / libraries yourself or use your own loader…
# 3b) Windows PE
env = win_env_x86_32(jitter)
entry = vm_load_pe_and_dependencies(jitter, "a.exe", win_env=env)
# 4) start
jitter.cpu.RIP = entry # or .EIP for 32-bit
jitter.run()
—
With these building blocks you can spin up a “sandbox” that
• maps your code + deps for you
• handles OS‐level services (syscalls or WinAPI)
• logs, traces, single-steps, snapshot/restore, etc.