'GRAY HAT PYTHON'

 

Chapter 2. Debugger

   2.1. CPU registers

   2.2. dis-assemble

 

* 본 문서는 'Gray Hat Python' 책의 내용을 살펴보며 개인적으로 정리하고 싶은 내용들을 기록한 페이지입니다. 

  Phtyon3 기준으로 예제는 변경되었습니다. 잘못된 내용이 있거나 추가가 필요한 사항이 있다면 언제든지 알려주시기 바랍니다.

 

2.1 CPU registers

 

간단히 살펴볼겸 Register들에 대해 확인해 보겠습니다.

 

* Intel core

http://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-software-developer-manual-325462.pdf

EAX ( Accumulator register )

EDX ( Data register )

ECX ( Counter register )

ESI ( Source Index )

EDI ( Destination Index )

ESP ( Stack Pointer )

EBP ( Base Pointer )

Segment Registers

Program Status and Control Register

Instruction Pointer

 

* ARM core

http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0553a/CHDBIBGJ.html

R0-R12 ( General Registers )

SP ( Stack Pointer )

LR ( Link Register )

PC ( Program Counter )

PSR ( Program Status Register ) 

ASPR ( Application Program Status Register )

IPSR ( Interrupt Program Status Register )

EPSR ( Execution Program Status Register )

 

 

2.2 dis-assemble

 

2장 내용중 ECX register (count register)는 레지스터 값을 감소시킨다는 문구가 있어서 예제를 직접 de-assemble해보았습니다.

dis.py library에서 bytecode로 disassembling해주는 기능이 있습니다. 

 

https://docs.python.org/3.5/library/dis.html

 

 

chapter2-dis.py

 

import dis

 

def f(x):

    counter = 3

    while counter < 10:

        print ("Loop number: %d" % counter)

        counter +=1

    print ("hello")

 

dis.dis(f)

 

  4           0 LOAD_CONST               1 (3)

              3 STORE_FAST               1 (counter)

 

  5           6 SETUP_LOOP              40 (to 49)

             // while counter < 10

        >>    9 LOAD_FAST                1 (counter)

             12 LOAD_CONST               2 (10)

             15 COMPARE_OP               0 (<)

 // TOS (Top of stack)을 확인

             18 POP_JUMP_IF_FALSE       48

 

  6          21 LOAD_GLOBAL              0 (print)

             24 LOAD_CONST               3 ('Loop number: %d')

             27 LOAD_FAST                1 (counter)

             30 BINARY_MODULO

             31 CALL_FUNCTION            1 (1 positional, 0 keyword pair)

             34 POP_TOP

 

             // counter+1

  7          35 LOAD_FAST                1 (counter)

             38 LOAD_CONST               4 (1)

             41 INPLACE_ADD

             42 STORE_FAST               1 (counter)

 

             // while loop 다시 돌기

             45 JUMP_ABSOLUTE            9

        >>   48 POP_BLOCK

  8     >>   49 LOAD_GLOBAL              0 (print)

 

             52 LOAD_CONST               5 ('hello')

 

             55 CALL_FUNCTION            1 (1 positional, 0 keyword pair)

             58 POP_TOP

             59 LOAD_CONST               0 (None)

             62 RETURN_VALUE

 
dis 만으로는 CPU register들에 대한 동작을 확인할 순 없었습니다.

 

 

반응형

'Security&Encryption > Black Hat' 카테고리의 다른 글

Windows Debugger with PyDev  (0) 2016.06.29
Pydev on Eclipse  (0) 2016.06.28
Trojan using Git hub  (0) 2016.03.30
ICMP Decoding with Python  (0) 2016.03.16

+ Recent posts