pwnlib.shellcraft.aarch64 — Shellcode for AArch64



Inserts a debugger breakpoint (raises SIGTRAP).


>>> run_assembly(shellcraft.breakpoint()).poll(True)

Crashes the process.


>>> run_assembly(shellcraft.crash()).poll(True)

An infinite loop.


>>> io = run_assembly(shellcraft.infloop())
>>> io.recvall(timeout=1)
>>> io.close()
pwnlib.shellcraft.aarch64.memcpy(dest, src, n)[source]

Copies memory.

  • dest – Destination address
  • src – Source address
  • n – Number of bytes, src)[source]

Move src into dest.

Support for automatically avoiding newline and null bytes has to be done.

If src is a string that is not a register, then it will locally set context.arch to ‘arm’ and use pwnlib.constants.eval() to evaluate the string. Note that this means that this shellcode can change behavior depending on the value of context.os.


>>> print('x0','x1').rstrip())
    mov  x0, x1
>>> print('x0','0').rstrip())
    mov  x0, xzr
>>> print('x0', 5).rstrip())
    mov  x0, #5
>>> print('x0', 0x34532).rstrip())
    /* Set x0 = 214322 = 0x34532 */
    mov  x0, #17714
    movk x0, #3, lsl #16
  • dest (str) – The destination register.
  • src (str) – Either the input register, or an immediate value.
pwnlib.shellcraft.aarch64.push(value, register1='x14', register2='x15')[source]

Pushes a value onto the stack without using null bytes or newline characters.

If src is a string, then we try to evaluate using pwnlib.constants.eval() before determining how to push it.

Note that this means that this shellcode can change behavior depending on the value of context.os.


AArch64 requires that the stack remain 16-byte aligned at all times, so this alignment is preserved.

  • value (int,str) – The value or register to push
  • register1 (str) – Scratch register to use
  • register2 (str) – Second scratch register to use


>>> print(pwnlib.shellcraft.push(0).rstrip())
    /* push 0 */
    mov  x14, xzr
    str x14, [sp, #-16]!
>>> print(pwnlib.shellcraft.push(1).rstrip())
    /* push 1 */
    mov  x14, #1
    str x14, [sp, #-16]!
>>> print(pwnlib.shellcraft.push(256).rstrip())
    /* push 0x100 */
    mov  x14, #256
    str x14, [sp, #-16]!
>>> print(pwnlib.shellcraft.push('SYS_execve').rstrip())
    /* push SYS_execve (0xdd) */
    mov  x14, #221
    str x14, [sp, #-16]!
>>> print(pwnlib.shellcraft.push('SYS_sendfile').rstrip())
    /* push SYS_sendfile (0x47) */
    mov  x14, #71
    str x14, [sp, #-16]!
>>> with context.local(os = 'freebsd'):
...     print(pwnlib.shellcraft.push('SYS_execve').rstrip())
    /* push SYS_execve (0x3b) */
    mov  x14, #59
    str x14, [sp, #-16]!
pwnlib.shellcraft.aarch64.pushstr(string, append_null=True, register1='x14', register2='x15', pretty=None)[source]

Pushes a string onto the stack.

r12 is defined as the inter-procedural scratch register ($ip), so this should not interfere with most usage.

  • string (str) – The string to push.
  • append_null (bool) – Whether to append a single NULL-byte before pushing.
  • register (str) – Temporary register to use. By default, R7 is used.


>>> string = "Hello, world!"
>>> assembly = shellcraft.pushstr(string)
>>> assembly += shellcraft.write(1, 'sp', len(string))
>>> assembly += shellcraft.exit()
>>> ELF.from_assembly(assembly).process().recvall()
b'Hello, world!'
>>> string = "Hello, world! This is a long string! Wow!"
>>> assembly = shellcraft.pushstr(string)
>>> assembly += shellcraft.write(1, 'sp', len(string))
>>> assembly += shellcraft.exit()
>>> ELF.from_assembly(assembly).process().recvall()
b'Hello, world! This is a long string! Wow!'
pwnlib.shellcraft.aarch64.pushstr_array(reg, array, register1='x14', register2='x15')[source]

Pushes an array/envp-style array of pointers onto the stack.

  • reg (str) – Destination register to hold the pointer.
  • array (str,list) – Single argument or list of arguments to push. NULL termination is normalized so that each argument ends with exactly one NULL byte.


>>> assembly = shellcraft.execve("/bin/sh", ["sh", "-c", "echo Hello string $WORLD"], {"WORLD": "World!"})
>>> ELF.from_assembly(assembly).process().recvall()
b'Hello string World!\n'
pwnlib.shellcraft.aarch64.setregs(reg_context, stack_allowed=True)[source]

Sets multiple registers, taking any register dependencies into account (i.e., given eax=1,ebx=eax, set ebx first).

  • reg_context (dict) – Desired register context
  • stack_allowed (bool) – Can the stack be used?


>>> print(shellcraft.setregs({'x0':1, 'x2':'x3'}).rstrip())
    mov  x0, #1
    mov  x2, x3
>>> print(shellcraft.setregs({'x0':'x1', 'x1':'x0', 'x2':'x3'}).rstrip())
    mov  x2, x3
    eor  x0, x0, x1 /* xchg x0, x1 */
    eor  x1, x0, x1
    eor  x0, x0, x1

Inserts a debugger breakpoint (raises SIGTRAP).


>>> run_assembly(shellcraft.breakpoint()).poll(True)
pwnlib.shellcraft.aarch64.xor(key, address, count)[source]

XORs data a constant value.

  • key (int,str) – XOR key either as a 4-byte integer, If a string, length must be a power of two, and not longer than 4 bytes.
  • address (int) – Address of the data (e.g. 0xdead0000, ‘rsp’)
  • count (int) – Number of bytes to XOR.


>>> sc  =, 'sp', 32)
>>> sc += shellcraft.xor(0xdeadbeef, 'sp', 32)
>>> sc += shellcraft.write(1, 'sp', 32)
>>> io = run_assembly(sc)
>>> io.send(cyclic(32))
>>> result = io.recvn(32)
>>> expected = xor(cyclic(32), p32(0xdeadbeef))
>>> result == expected

pwnlib.shellcraft.aarch64.linux, fd=1)[source]

Opens a file and writes its contents to the specified file descriptor.


>>> f = tempfile.mktemp()
>>> write(f, 'This is the flag\n')
>>> shellcode = + shellcraft.exit(0)
>>> run_assembly(shellcode).recvline()
b'This is the flag\n'
pwnlib.shellcraft.aarch64.linux.connect(host, port, network='ipv4')[source]

Connects to the host on the specified port. Network is either ‘ipv4’ or ‘ipv6’. Leaves the connected socket in x12.

pwnlib.shellcraft.aarch64.linux.echo(string, sock='1')[source]

Writes a string to a file descriptor


>>> run_assembly(shellcraft.echo('hello\n', 1)).recvline()

Attempts to fork. If the fork is successful, the parent exits.

pwnlib.shellcraft.aarch64.linux.kill(pid, sig) → str[source]

Invokes the syscall kill.

See ‘man 2 kill’ for more information.

  • pid (pid_t) – pid
  • sig (int) – sig



Kills its parent process until whatever the parent is (probably init) cannot be killed any longer.


Loads a statically-linked ELF into memory and transfers control.

Parameters:address (int) – Address of the ELF as a register or integer.

Loads a statically-linked ELF into memory and transfers control.

Similar to loader.asm but loads an appended ELF.

Parameters:data (str) – If a valid filename, the data is loaded from the named file. Otherwise, this is treated as raw ELF data to append. If None, it is ignored.


The following doctest is commented out because it doesn’t work on Travis for reasons I cannot diagnose. However, it should work just fine :-)

>>> payload = shellcraft.echo(b'Hello, world!\n') + shellcraft.exit(0)
>>> payloadELF = ELF.from_assembly(payload)
>>> loader = shellcraft.loader_append(
>>> loaderELF = ELF.from_assembly(loader, vma=0, shared=True)
>>> loaderELF.process().recvall()
b'Hello, world!\n', flags='O_RDONLY', mode=0)[source]

Opens a file

pwnlib.shellcraft.aarch64.linux.readn(fd, buf, nbytes)[source]

Reads exactly nbytes bytes from file descriptor fd into the buffer buf.

  • fd (int) – fd
  • buf (void) – buf
  • nbytes (size_t) – nbytes[source]

Execute a different process.

>>> p = run_assembly(
>>> p.sendline(b'echo Hello')
>>> p.recv()
pwnlib.shellcraft.aarch64.linux.socket(network='ipv4', proto='tcp')[source]

Creates a new socket

pwnlib.shellcraft.aarch64.linux.stage(fd=0, length=None)[source]

Migrates shellcode to a new buffer.

  • fd (int) – Integer file descriptor to recv data from. Default is stdin (0).
  • length (int) – Optional buffer length. If None, the first pointer-width of data received is the length.


>>> p = run_assembly(shellcraft.stage())
>>> sc = asm(shellcraft.echo("Hello\n", constants.STDOUT_FILENO))
>>> p.pack(len(sc))
>>> p.send(sc)
>>> p.recvline()
pwnlib.shellcraft.aarch64.linux.syscall(syscall=None, arg0=None, arg1=None, arg2=None, arg3=None, arg4=None, arg5=None, arg6=None)[source]
Args: [syscall_number, *args]
Does a syscall

Any of the arguments can be expressions to be evaluated by pwnlib.constants.eval().


>>> print(shellcraft.aarch64.linux.syscall(11, 1, 'sp', 2, 0).rstrip())
    /* call syscall(0xb, 1, 'sp', 2, 0) */
    mov  x0, #1
    mov  x1, sp
    mov  x2, #2
    mov  x3, xzr
    mov  x8, #11
    svc 0
>>> print(shellcraft.aarch64.linux.syscall('SYS_exit', 0).rstrip())
    /* call exit(0) */
    mov  x0, xzr
    mov  x8, #SYS_exit
    svc 0
>>> print(pwnlib.shellcraft.openat(-2, '/home/pwn/flag').rstrip())
    /* openat(fd=-2, file='/home/pwn/flag', oflag=0) */
    /* push b'/home/pwn/flag\x00' */
    /* Set x14 = 8606431000579237935 = 0x77702f656d6f682f */
    mov  x14, #26671
    movk x14, #28015, lsl #16
    movk x14, #12133, lsl #0x20
    movk x14, #30576, lsl #0x30
    /* Set x15 = 113668128124782 = 0x67616c662f6e */
    mov  x15, #12142
    movk x15, #27750, lsl #16
    movk x15, #26465, lsl #0x20
    stp x14, x15, [sp, #-16]!
    mov  x1, sp
    /* Set x0 = -2 = -2 */
    mov  x0, #65534
    movk x0, #65535, lsl #16
    movk x0, #65535, lsl #0x20
    movk x0, #65535, lsl #0x30
    mov  x2, xzr
    /* call openat() */
    mov  x8, #SYS_openat
    svc 0