Pwntools Tutorial - Assembly

모씨김(mossikim)·2025년 8월 13일

Pwntools

목록 보기
5/8
post-thumbnail

Assembly


  • Basic Assembly
  • Canned Assembly ( Shellcraft )
  • Command-line Tools
    • asm
    • disasm
    • shellcraft
  • Foreign Architectures
    • Canned Assembly
    • Command-line Tools


Basic Assembly

가장 기본적인 예시는 assembly를 shellcode로 변환하는 것이다.

from pwn import *

print repr(asm('xor edi, edi'))
# '1\xff'

print enhex(asm('xor edi, edi'))
# 31ff

Canned Assembly (shellcraft)

shellcraft 모듈은 기본적인 어셈블리 코드를 제공한다. 일반적으로 이 코드는 커스텀이 가능하다.

어떤 shellcraft 템플릿이 있는지는 documentation on RTD 를 참고해라.

from pwn import *
help(shellcraft.sh)
print '---'
print shellcraft.sh()
print '---'
print enhex(asm(shellcraft.sh()))
Help on function sh in module pwnlib.shellcraft.internal:

sh()
    Execute /bin/sh
---
    /* push '/bin///sh\x00' */
    push 0x68
    push 0x732f2f2f
    push 0x6e69622f

    /* call execve('esp', 0, 0) */
    push (SYS_execve) /* 0xb */
    pop eax
    mov ebx, esp
    xor ecx, ecx
    cdq /* edx=0 */
    int 0x80
---
6a68682f2f2f73682f62696e6a0b5889e331c999cd80

Command-line Tools

어셈블리와 상호 작용하기 위한 세가지의 command-line 툴들이 있다.

  • asm
  • disasm
  • shellcraft

asm

asm 도구는 몇가지 출력 포맷 옵션을 제공한다.

출력 위치가 터미널일 때, 기본적으로 hex 인코딩을 사용한다.

$ asm nop
90

출력이 다른 곳에서 이루어진다면, raw data를 사용한다.

$ asm nop | xxd
0000000: 90

만약 command-line에 관련 지시가 전달되지 않는다면, stdin 데이터를 사용한다.

$ echo 'push ebx; pop edi' | asm
535f

마지막으로, asm은 몇가지 다른 출력 옵션을 제공한다.

—format 으로 설정 가능하다.

지원되는 포맷은 raw, hex, string, elf 이다.

$ asm --format=elf 'int3' > ./int3
$ ./halt
Trace/breakpoint trap (core dumped)

disasm

Disasm은 asm의 반대 기능이다.

$ disasm cd80
   0:    cd 80                    int    0x80
$ asm nop | disasm
   0:    90                       nop

shellcraft

shellcraft 는 internal shellcraft module을 대상으로 한 command-line interface이다. command-line 에서, 전체 context를 지정해야 한다. ( arch.os.template )

$ shellcraft i386.linux.sh
6a68682f2f2f73682f62696e6a0b5889e331c999cd80

Foreign Architectures

다른 아키텍처들에서 어셈블링을 하려면 적절한 버전의 binutils 가 설치되어 있어야 한다. installing.md 에서 확인 가능하다.

필요한 변경은 global context에 아키텍처를 세팅하는 것이다. context.md 에서 자세히 확인할 수 있다.

from pwn import *

context.arch = 'arm'

print repr(asm('mov r0, r1'))
# '\x01\x00\xa0\xe1'

print enhex(asm('mov r0, r1'))
# 0100a0e1

Canned Assembly

shellcraft 모듈은 자동적으로 적절한 아키텍처로 변경된다.

from pwn import *

context.arch = 'arm'

print shellcraft.sh()
print enhex(asm(shellcraft.sh()))
    adr r0, bin_sh
    mov r2, #0
    mov r1, r2
    svc SYS_execve
bin_sh: .asciz "/bin/sh"

08008fe20020a0e30210a0e10b0000ef2f62696e2f736800

Command-line tools

—context 명령줄 옵션을 사용함으로써 command line 도구들을 다른 아키텍처에서 어셈블 하는데에 사용할 수 있다.

$ asm --context=arm 'mov r0, r1'
0100a0e1
$ shellcraft arm.linux.sh
08008fe20020a0e30210a0e10b0000ef2f62696e2f736800

Pwntools - official tutorial
Full Reference

0개의 댓글