Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Example ROP in MIPS

Chen
November 04, 2023

Example ROP in MIPS

Chen

November 04, 2023
Tweet

More Decks by Chen

Other Decks in Technology

Transcript

  1. 34 Example ROP in MIPS (1) ͻ EŽǁůĞƚ͛ƐĚŽŝƚǀŝĂZKW͘^ƚĞƉƐ͗ 1. ^ĞƚΨĂϬƚŽƚŚĞĂĚĚƌĞƐƐŽĨƚŚĞƐƚƌŝŶŐͬ͞ďŝŶͬsh͟

    2. Set $v0 to the syscall number for 'exec' 3. Ask the OS to do the syscall PS1=bash$ SHELL=/bin/bash LSCOLOR=... ... lw $a0, 4($sp) lw $ra, 0($sp) addi $sp, $sp, 8 jr $ra ... 0x3506 0x3500 0x1200 ... lw $a0, 8($sp) lw $a1, 4($sp) lw $ra, 0($sp) addi $sp, $sp, 12 jr $ra ... 0x1800 0x1804 ... syscall ... 0x4880 0x0000 0x0000 0x0000 0x0000 0x9800 ... # (Read a string from the user into a buffer) ... lw $ra, 0($sp) addi $sp, $sp, 4 jr $ra ... Stack (junk) (junk) (junk) (junk) 0x1200 0x1804 0x3506 0x4880 55 Vulnerable IXQFWLRQ¶Vra $sp $pc Buffer overflow occurs buffer ;&ƵŶĐƚŝŽŶ͛ƐĐĂůůĞƌͿ
  2. 35 $ra Example ROP in MIPS (2) ͻ EŽǁůĞƚ͛ƐĚŽŝƚǀŝĂZKW͘^ƚĞƉƐ͗ 1.

    ^ĞƚΨĂϬƚŽƚŚĞĂĚĚƌĞƐƐŽĨƚŚĞƐƚƌŝŶŐͬ͞ďŝŶͬsh͟ 2. Set $v0 to the syscall number for 'exec' 3. Ask the OS to do the syscall PS1=bash$ SHELL=/bin/bash LSCOLOR=... ... lw $a0, 4($sp) lw $ra, 0($sp) addi $sp, $sp, 8 jr $ra ... 0x3506 0x3500 0x1200 ... lw $a0, 8($sp) lw $a1, 4($sp) lw $ra, 0($sp) addi $sp, $sp, 12 jr $ra ... 0x1800 0x1804 ... syscall ... 0x4880 0x0000 0x0000 0x0000 0x0000 0x9800 ... # (Read a string from the user into a buffer) ... lw $ra, 0($sp) addi $sp, $sp, 4 jr $ra ... Stack (junk) (junk) (junk) (junk) 0x1200 0x1804 0x3506 0x4880 55 Vulnerable IXQFWLRQ¶Vra $sp $pc $ra controlled by attacker now buffer ;&ƵŶĐƚŝŽŶ͛ƐĐĂůůĞƌͿ
  3. 36 $ra $pc 0x1200 Example ROP in MIPS (3) ͻ

    EŽǁůĞƚ͛ƐĚŽŝƚǀŝĂZKW͘^ƚĞƉƐ͗ 1. ^ĞƚΨĂϬƚŽƚŚĞĂĚĚƌĞƐƐŽĨƚŚĞƐƚƌŝŶŐͬ͞ďŝŶͬsh͟ 2. Set $v0 to the syscall number for 'exec' 3. Ask the OS to do the syscall PS1=bash$ SHELL=/bin/bash LSCOLOR=... ... lw $a0, 4($sp) lw $ra, 0($sp) addi $sp, $sp, 8 jr $ra ... 0x3506 0x3500 ... lw $a0, 8($sp) lw $a1, 4($sp) lw $ra, 0($sp) addi $sp, $sp, 12 jr $ra ... 0x1800 0x1804 ... syscall ... 0x4880 0x0000 0x0000 0x0000 0x0000 0x9800 ... # (Read a string from the user into a buffer) ... lw $ra, 0($sp) addi $sp, $sp, 4 jr $ra ... Stack (junk) (junk) (junk) (junk) 0x1200 0x1804 0x3506 0x4880 55 Vulnerable IXQFWLRQ¶Vra $sp We go where attacker says buffer ;&ƵŶĐƚŝŽŶ͛ƐĐĂůůĞƌͿ
  4. 37 $ra Example ROP in MIPS (4) ͻ EŽǁůĞƚ͛ƐĚŽŝƚǀŝĂZKW͘^ƚĞƉƐ͗ 1.

    ^ĞƚΨĂϬƚŽƚŚĞĂĚĚƌĞƐƐŽĨƚŚĞƐƚƌŝŶŐͬ͞ďŝŶͬsh͟ 2. Set $v0 to the syscall number for 'exec' 3. Ask the OS to do the syscall PS1=bash$ SHELL=/bin/bash LSCOLOR=... ... lw $a0, 4($sp) lw $ra, 0($sp) addi $sp, $sp, 8 jr $ra ... 0x3506 0x3500 0x1200 ... lw $a0, 8($sp) lw $a1, 4($sp) lw $ra, 0($sp) addi $sp, $sp, 12 jr $ra ... 0x1800 0x1804 ... syscall ... 0x4880 0x0000 0x0000 0x0000 0x0000 0x9800 ... # (Read a string from the user into a buffer) ... lw $ra, 0($sp) addi $sp, $sp, 4 jr $ra ... Stack (junk) (junk) (junk) (junk) 0x1200 0x1804 0x3506 0x4880 55 Vulnerable IXQFWLRQ¶Vra $sp We change $a0 and $ra buffer ;&ƵŶĐƚŝŽŶ͛ƐĐĂůůĞƌͿ $pc Regs $a0 = 0x3506
  5. 38 $ra $pc Example ROP in MIPS (5) ͻ EŽǁůĞƚ͛ƐĚŽŝƚǀŝĂZKW͘^ƚĞƉƐ͗

    1. ^ĞƚΨĂϬƚŽƚŚĞĂĚĚƌĞƐƐŽĨƚŚĞƐƚƌŝŶŐͬ͞ďŝŶͬsh͟ 2. Set $v0 to the syscall number for 'exec' 3. Ask the OS to do the syscall PS1=bash$ SHELL=/bin/bash LSCOLOR=... ... lw $a0, 4($sp) lw $ra, 0($sp) addi $sp, $sp, 8 jr $ra ... 0x3506 0x3500 0x1200 ... lw $a0, 8($sp) lw $a1, 4($sp) lw $ra, 0($sp) addi $sp, $sp, 12 jr $ra ... 0x1800 0x1804 ... syscall ... 0x4880 0x0000 0x0000 0x0000 0x0000 0x9800 ... # (Read a string from the user into a buffer) ... lw $ra, 0($sp) addi $sp, $sp, 4 jr $ra ... Stack (junk) (junk) (junk) (junk) 0x1200 0x1804 0x3506 0x4880 55 Vulnerable IXQFWLRQ¶Vra $sp Now we execute another gadget buffer ;&ƵŶĐƚŝŽŶ͛ƐĐĂůůĞƌͿ Regs $a0 = 0x3506
  6. 39 $ra Example ROP in MIPS (6) ͻ EŽǁůĞƚ͛ƐĚŽŝƚǀŝĂZKW͘^ƚĞƉƐ͗ 1.

    ^ĞƚΨĂϬƚŽƚŚĞĂĚĚƌĞƐƐŽĨƚŚĞƐƚƌŝŶŐͬ͞ďŝŶͬsh͟ 2. Set $v0 to the syscall number for 'exec' 3. Ask the OS to do the syscall PS1=bash$ SHELL=/bin/bash LSCOLOR=... ... lw $a0, 4($sp) lw $ra, 0($sp) addi $sp, $sp, 8 jr $ra ... 0x3506 0x3500 0x1200 ... lw $a0, 8($sp) lw $a1, 4($sp) lw $ra, 0($sp) addi $sp, $sp, 12 jr $ra ... 0x1800 0x1804 ... syscall ... 0x4880 0x0000 0x0000 0x0000 0x0000 0x9800 ... # (Read a string from the user into a buffer) ... lw $ra, 0($sp) addi $sp, $sp, 4 jr $ra ... Stack (junk) (junk) (junk) (junk) 0x1200 0x1804 0x3506 0x4880 55 Vulnerable IXQFWLRQ¶Vra $sp It sets $a1 and $ra buffer ;&ƵŶĐƚŝŽŶ͛ƐĐĂůůĞƌͿ $pc Regs $a0 = 0x3506 $a1 = 55
  7. 40 $ra $pc Example ROP in MIPS (7) ͻ EŽǁůĞƚ͛ƐĚŽŝƚǀŝĂZKW͘^ƚĞƉƐ͗

    1. ^ĞƚΨĂϬƚŽƚŚĞĂĚĚƌĞƐƐŽĨƚŚĞƐƚƌŝŶŐͬ͞ďŝŶͬsh͟ 2. Set $v0 to the syscall number for 'exec' 3. Ask the OS to do the syscall PS1=bash$ SHELL=/bin/bash LSCOLOR=... ... lw $a0, 4($sp) lw $ra, 0($sp) addi $sp, $sp, 8 jr $ra ... 0x3506 0x3500 0x1200 ... lw $a0, 8($sp) lw $a1, 4($sp) lw $ra, 0($sp) addi $sp, $sp, 12 jr $ra ... 0x1800 0x1804 ... syscall ... 0x4880 0x0000 0x0000 0x0000 0x0000 0x9800 ... # (Read a string from the user into a buffer) ... lw $ra, 0($sp) addi $sp, $sp, 4 jr $ra ... Stack (junk) (junk) (junk) (junk) 0x1200 0x1804 0x3506 0x4880 55 Vulnerable IXQFWLRQ¶Vra $sp We execute the final gadget buffer ;&ƵŶĐƚŝŽŶ͛ƐĐĂůůĞƌͿ Regs $a0 = 0x3506 $a1 = 55
  8. 41 $ra $pc Example ROP in MIPS (8) ͻ EŽǁůĞƚ͛ƐĚŽŝƚǀŝĂZKW͘^ƚĞƉƐ͗

    1. ^ĞƚΨĂϬƚŽƚŚĞĂĚĚƌĞƐƐŽĨƚŚĞƐƚƌŝŶŐͬ͞ďŝŶͬsh͟ 2. Set $v0 to the syscall number for 'exec' 3. Ask the OS to do the syscall PS1=bash$ SHELL=/bin/bash LSCOLOR=... ... lw $a0, 4($sp) lw $ra, 0($sp) addi $sp, $sp, 8 jr $ra ... 0x3506 0x3500 0x1200 ... lw $a0, 8($sp) lw $a1, 4($sp) lw $ra, 0($sp) addi $sp, $sp, 12 jr $ra ... 0x1800 0x1804 ... syscall ... 0x4880 0x0000 0x0000 0x0000 0x0000 0x9800 ... # (Read a string from the user into a buffer) ... lw $ra, 0($sp) addi $sp, $sp, 4 jr $ra ... Stack (junk) (junk) (junk) (junk) 0x1200 0x1804 0x3506 0x4880 55 Vulnerable IXQFWLRQ¶Vra $sp Boom: shell buffer ;&ƵŶĐƚŝŽŶ͛ƐĐĂůůĞƌͿ Regs $a0 = 0x3506 $a1 = 55