Post

Assembly Cheatsheet

Registers

Description64-bit Register (8-bytes)8-bit Register (1-bytes)
Data/Arguments Registers  
Syscall Number/Return valueraxal
Callee Savedrbxbl
1st argrdidil
2nd argrsisil
3rd argrdxdl
4th arg - Loop Counterrcxcl
5th argr8r8b
6th argr9r9b
Pointer Registers  
Base Stack Pointerrbpbpl
Current/Top Stack Pointerrspspl
Instruction Pointer ‘call only’ripipl

Assembly and Disassembly

CommandDescription
nasm -f elf64 helloWorld.sAssemble code
ld -o helloWorld helloWorld.oLink code
ld -o fib fib.o -lc --dynamic-linker /lib64/ld-linux-x86-64.so.2Link code with libc functions
objdump -M intel -d helloWorldDisassemble .text section
objdump -M intel --no-show-raw-insn --no-addresses -d helloWorldShow binary assembly code
objdump -sj .data helloWorldDisassemble .data section

GDB

CommandDescription
gdb -q ./helloWorldOpen binary in gdb
info functionsView binary functions
info variablesView binary variables
registersView registers
disas _startDisassemble label/function
b _startBreak label/function
b *0x401000Break address
rRun the binary
x/4xg $ripExamine register “x/ count-format-size $register”
siStep to the next instruction
sStep to the next line of code
niStep to the next function
cContinue to the next break point
patch string 0x402000 "Patched!\\x0a"Patch address value
set $rdx=0x9Set register value

Assembly Instructions

InstructionDescriptionExample
Data Movement  
movMove data or load immediate datamov rax, 1 -> rax = 1
leaLoad an address pointing to the valuelea rax, [rsp+5] -> rax = rsp+5
xchgSwap data between two registers or addressesxchg rax, rbx -> rax = rbx, rbx = rax
Unary Arithmetic Instructions  
incIncrement by 1inc rax -> rax++ or rax += 1 -> rax = 2
decDecrement by 1dec rax -> rax-- or rax -= 1 -> rax = 0
Binary Arithmetic Instructions  
addAdd both operandsadd rax, rbx -> rax = 1 + 1 -> 2
subSubtract Source from Destination (i.e rax = rax - rbx)sub rax, rbx -> rax = 1 - 1 -> 0
imulMultiply both operandsimul rax, rbx -> rax = 1 * 1 -> 1
Bitwise Arithmetic Instructions  
notBitwise NOT (invert all bits, 0->1 and 1->0)not rax -> NOT 00000001 -> 11111110
andBitwise AND (if both bits are 1 -> 1, if bits are different -> 0)and rax, rbx -> 00000001 AND 00000010 -> 00000000
orBitwise OR (if either bit is 1 -> 1, if both are 0 -> 0)or rax, rbx -> 00000001 OR 00000010 -> 00000011
xorBitwise XOR (if bits are the same -> 0, if bits are different -> 1)xor rax, rbx -> 00000001 XOR 00000010 -> 00000011
Loops  
mov rcx, xSets loop (rcx) counter to xmov rcx, 3
loopJumps back to the start of loop until counter reaches 0loop exampleLoop
Branching  
jmpJumps to specified label, address, or locationjmp loop
jzDestination equal to ZeroD = 0
jnzDestination Not equal to ZeroD != 0
jsDestination is NegativeD < 0
jnsDestination is Not Negative (i.e. 0 or positive)D >= 0
jgDestination Greater than SourceD > S
jgeDestination Greater than or Equal SourceD >= S
jlDestination Less than SourceD < S
jleDestination Less than or Equal SourceD <= S
cmpSets RFLAGS by subtracting second operand from first operand (i.e. first - second)cmp rax, rbx -> rax - rbx
Stack  
pushCopies the specified register/address to the top of the stackpush rax
popMoves the item at the top of the stack to the specified register/addresspop rax
Functions  
callpush the next instruction pointer rip to the stack, then jumps to the specified procedurecall printMessage
retpop the address at rsp into rip, then jump to itret

Functions

CommandDescription
cat /usr/include/x86_64-linux-gnu/asm/unistd_64.h \| grep writeLocate write syscall number
man -s 2 writewrite syscall man page
man -s 3 printfprintf libc man page

Syscall Calling Convention

  1. Save registers to stack
  2. Set its syscall number in rax
  3. Set its arguments in the registers
  4. Use the syscall assembly instruction to call it

Function Calling Convention

  1. Save Registers on the stack (Caller Saved)
  2. Pass Function Arguments (like syscalls)
  3. Fix Stack Alignment
  4. Get Function’s Return Value (in rax)

Shellcoding

CommandDescription
pwn asm 'push rax' -c 'amd64'Instruction to shellcode
pwn disasm '50' -c 'amd64'Shellcode to instructions
python3 shellcoder.py helloworldExtract binary shellcode
python3 loader.py '4831..0f05Run shellcode
python assembler.py '4831..0f05Assemble shellcode into binary
Shellcraft 
pwn shellcraft -l 'amd64.linux'List available syscalls
pwn shellcraft amd64.linux.shGenerate syscalls shellcode
pwn shellcraft amd64.linux.sh -rRun syscalls shellcode
Msfvenom 
msfvenom -l payloads \| grep 'linux/x64'List available syscalls
msfvenom -p 'linux/x64/exec' CMD='sh' -a 'x64' --platform 'linux' -f 'hex'Generate syscalls shellcode
msfvenom -p 'linux/x64/exec' CMD='sh' -a 'x64' --platform 'linux' -f 'hex' -e 'x64/xor'Generate encoded syscalls shellcode

Shellcoding Requirements

  1. Does not contain variables
  2. Does not refer to direct memory addresses
  3. Does not contain any NULL bytes 00
This post is licensed under CC BY 4.0 by the author.