Take the 2-minute tour ×
Reverse Engineering Stack Exchange is a question and answer site for researchers and developers who explore the principles of a system through analysis of its structure, function, and operation. It's 100% free, no registration required.

It would be very useful to have a pure python library that could assemble x86, x64 and ARM instructions. Do you have any recommendations?

I don't mind if they are not pure-python, but that'd be preferred, thanks!

share|improve this question

2 Answers 2

up vote 2 down vote accepted

Take a look at Pwnies' pwntools. Does all the things you want it to, and has most of it built in already.

https://github.com/pwnies/pwntools

For a few examples of it in use, I put up some writeups from Codegate 2013 and the pwnies do their own writeups as well.

Here's a quick example of "I want to dup file descriptor #4 (generally a socket) to stdin/stdout/stderr, and pop a shell".

#!/usr/bin/env python
from pwn import *
context('arm','linux') # Replace either with i386/amd64/mips/etc or freebsd

sc = shellcode.dupsh(4)
for block in sc.blocks:
  print block.text

print enhex(asm(sc))

# An example assemblying custom assembly.
# This works in x86/amd64/mips/arm/ppc/etc.
print enhex(asm('''
mov r0, #0
bx lr
'''))

Prints out

// Set r9 = 4
ldr r9, =4

// Set r8 = 2
ldr r8, =2

dup_helper:
mov r0, r9
mov r1, r8
svc SYS_dup2
adds r8, #-1
bpl dup_helper
adr r0, bin_sh
mov r2, #0
push {r0, r2}
mov r1, sp
svc SYS_execve
bin_sh: .asciz "/bin/sh"
0490a0e30280a0e30900a0e10810a0e13f0090ef018058e2faffff5a0c008fe20020a0e305002de90d10a0e10b0090ef2f62696e2f736800
0000a0e31eff2fe1

They've also got nifty tools for testing shellcode. For example:

shellcraft -c i386 -c linux echo "Hello world" | demo32 -
Hello world
share|improve this answer

Some python assembler libraries.

Pyasm - Python x86 Assembler
Pyasm is a full-featured dynamic assembler written entirely in Python. By dynamic, it means that it can be used to generate and execute machine code in python at runtime without requiring the generation of object files and linkage. It essentially allow 'inline' assembly in python modules on x86 platforms.

pyasm2
An easy and powerful assembler engine in python.Although its called pyasm2, this is not per se a successor of Pyasm or pyASM. pyasm2 aims to be as flexible as possible, it will support x86, SSE and SSE2.

d00ks
d00ks is an ARM assembler and simulator.

AsmJit-Python
AsmJit is a complete JIT and remote assembler for C++ language. It can generate native code for x86 and x64 architectures and supports the whole x86/x64 instruction set - from legacy MMX to the newest AVX2. It has a type-safe API that allows C++ compiler to do a semantic checks at compile-time even before the assembled code is generated or run.

share|improve this answer
    
None of these assemble x86/x64 and ARM. –  Zach Riggle Jul 14 at 23:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.