Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing assembler code with python

I want to execute assembly code inside a python script. Is that possible?

In C programming would be like this

static inline getesp(){
        __asm__("mov %esp, %eax");
}

But how to do that with Python? Is it possible?

like image 908
Yuda Prawira Avatar asked May 18 '11 06:05

Yuda Prawira


People also ask

How do you implement an assembler in Python?

These work by compiling the assembly and loading it into executable memory at runtime. The first three projects implement x86 assemblers in Python, whereas the last calls out to an external compiler. Show activity on this post. Not sure about the "power" of assembly, really.

Can you compile Python to assembly?

Compile python to C, then use a C compiler of your choice to get it down to assembly. Alternatively, use PyPy, specifying LLVM as the target, and use the LLVM Static Compiler to yield assembly language for your target architecture.

How assembly language is executed?

An assembler is a program that reads assembly language commands and translates then into a sequence of binary instructions, addresses and data values that is called machine code. The machine code is stored in the computer's memory and can be executed by the computer at some later time.


4 Answers

One way you could do this would be to write a (C) extension for Python. You can take a look at this documentation for full details of how to do that.

Another way of developing C-based Python extensions would be to interface directly with an external library using the ctypes module.

In any case, you'd need some C code compiled into either a library or an extension and a way to call it from Python. Clearly for what you want to achieve this is probably not optimal but actually its not that much work to expose a few functions.

like image 116
jkp Avatar answered Oct 18 '22 19:10

jkp


You can embed assembly directly inside your Python program:

  • https://github.com/Maratyszcza/PeachPy
  • https://github.com//pycca/pycca
  • http://codeflow.org/entries/2009/jul/31/pyasm-python-x86-assembler/
  • https://github.com/AmihaiN/pyAsm

These work by compiling the assembly and loading it into executable memory at runtime. The first three projects implement x86-64 or x86 assemblers in Python, whereas the last calls out to an external compiler.

like image 34
Luke Avatar answered Oct 18 '22 21:10

Luke


As a specific example, here is how to call a function which will take an int and return it incremented by one.

To obtain memory with the executable flag set, mmap module is used.

To call the function, ctypes module is used.

To put the machine code into memory, there is hardcoded byte string of x86-64 machine code.

The code will print 43.

In practice, I'd write the code in C shared object library and use inline assembly in C. I'd then use cffi to load and run the library. The advantage of this example is that it is self-contained and only needs the standard Python library.

import ctypes
import mmap

buf = mmap.mmap(-1, mmap.PAGESIZE, prot=mmap.PROT_READ | mmap.PROT_WRITE | mmap.PROT_EXEC)

ftype = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_int)
fpointer = ctypes.c_void_p.from_buffer(buf)

f = ftype(ctypes.addressof(fpointer))

buf.write(
    b'\x8b\xc7'  # mov eax, edi
    b'\x83\xc0\x01'  # add eax, 1
    b'\xc3'  # ret
)

r = f(42)
print(r)

del fpointer
buf.close()
like image 16
user7610 Avatar answered Oct 18 '22 21:10

user7610


Sorry for necroposting but I think that you can write your own DLL using asm and call it's functions from within Python.

like image 6
Крайст Avatar answered Oct 18 '22 19:10

Крайст