Skip to main content

A library for low-level abstract use to high-level.

Project description

🛠 pykasm

pykasm is a Python library for executing Assembly (ASM) code directly from Python scripts, allowing low-level integration with system calls and native libraries.

The goal is to provide a bridge between Python and Assembly, enabling developers to explore optimizations, manipulate memory, and execute machine instructions in a controlled manner.


📦 Installation

You can install via pip (after publication on PyPI):

pip install pykasm

💻 Examples

1️⃣ X86 Linux Example

##################################
#      X86 Linux Example         #
##################################

import pykasm
import ctypes

libc = ctypes.CDLL(None)
puts = libc.puts
puts.argtypes = [ctypes.c_char_p]
puts.restype = ctypes.c_int

msg_buf = ctypes.create_string_buffer(b"Hello, World")
msg_addr = ctypes.addressof(msg_buf)

puts_addr = ctypes.cast(puts, ctypes.c_void_p).value

asm_code = (
    f"mov rdi, {msg_addr};\n"
    f"mov rax, {puts_addr};\n"
    "call rax;\n"
    "ret"
)

pykasm.run_asm(asm_code)

2️⃣ X86 Windows Example

################################## 
#      X86 Windows Example       # 
##################################

import pykasm
import ctypes

msvcrt = ctypes.CDLL("msvcrt.dll")
puts = msvcrt.puts
puts.argtypes = [ctypes.c_char_p]
puts.restype = ctypes.c_int

kernel32 = ctypes.WinDLL("kernel32.dll")
ExitProcess = kernel32.ExitProcess
ExitProcess.argtypes = [ctypes.c_uint]
ExitProcess.restype = None

msg_buf = ctypes.create_string_buffer(b"Hello, World!\n")
msg_addr = ctypes.addressof(msg_buf)

puts_addr = ctypes.cast(puts, ctypes.c_void_p).value
exitprocess_addr = ctypes.cast(ExitProcess, ctypes.c_void_p).value

asm_code = (
    f"mov rcx, {msg_addr}; "
    f"mov rax, {puts_addr}; "
    "call rax; "
    "mov rcx, 0; "
    f"mov rax, {exitprocess_addr}; "
    "call rax; "
)

pykasm.run_asm(asm_code)

2️⃣ ARM64 Android Example

##################################
#     ARM64 Android Example      #
##################################

import pykasm
import ctypes

libc = ctypes.CDLL(None)
puts = libc.puts
puts.argtypes = [ctypes.c_char_p]
puts.restype = ctypes.c_int

_exit = libc.exit
_exit.argtypes = [ctypes.c_int]
_exit.restype = None

msg_buf = ctypes.create_string_buffer(b"Hello, World")
msg_addr = ctypes.addressof(msg_buf)

puts_addr = ctypes.cast(puts, ctypes.c_void_p).value
exit_addr = ctypes.cast(_exit, ctypes.c_void_p).value

def mov_imm64(reg, val):
    parts = [
        (val >> 0) & 0xFFFF,
        (val >> 16) & 0xFFFF,
        (val >> 32) & 0xFFFF,
        (val >> 48) & 0xFFFF,
    ]

    asm = []

    asm.append(f'movz {reg}, #{parts[0]}')

    if parts[1]: asm.append(f'movk {reg}, #{parts[1]}')
    if parts[2]: asm.append(f'movk {reg}, #{parts[2]}')
    if parts[3]: asm.append(f'movk {reg}, #{parts[3]}')

    return "\n".join(asm)

asm_code = '\n'.join([
    mov_imm64('x0', msg_addr),
    mov_imm64('x16', puts_addr),
    "blr x16",
    "mov x0, #0",
    mov_imm64('x16', exit_addr),
    "blr x16",
    "ret",
])

pykasm.run_asm(asm_code)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pykasm-0.1.9.tar.gz (92.7 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pykasm-0.1.9-py3-none-any.whl (93.4 MB view details)

Uploaded Python 3

File details

Details for the file pykasm-0.1.9.tar.gz.

File metadata

  • Download URL: pykasm-0.1.9.tar.gz
  • Upload date:
  • Size: 92.7 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.1

File hashes

Hashes for pykasm-0.1.9.tar.gz
Algorithm Hash digest
SHA256 378f32270a8ba01b30eb79d93bb74c98c7b58467bcd354eb88c1133b89ca3e3f
MD5 e54934baa3bc4166a43fd0c2fa8b4d1f
BLAKE2b-256 d94d2d71ffd4013777cc7b871fb61dcd575a386a96246b3282779dda092fa5ec

See more details on using hashes here.

File details

Details for the file pykasm-0.1.9-py3-none-any.whl.

File metadata

  • Download URL: pykasm-0.1.9-py3-none-any.whl
  • Upload date:
  • Size: 93.4 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.1

File hashes

Hashes for pykasm-0.1.9-py3-none-any.whl
Algorithm Hash digest
SHA256 d352d9a03b3900f1b9290a16662bb42bdcc38e050d748656448ae352b061ee78
MD5 1da2bcfcc5cf658c2b93864121bb2876
BLAKE2b-256 e852bd4d517b085e08c0686b1e58bd7254b81431934d00534c01cde1a5ccd3bc

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page