'GRAY HAT PYTHON'

 

Chapter 1

   1.1. 환경설정

   1.2. ctypes


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

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


1.1 환경 설정


Eclipse에 Pydev환경을 설치해 보았습니다. (Python은 이미 설치 되어 있다는 가정)


Eclipse는 아무 생각 없이 Mars.2 Relase(4.5.2)에 깔아 버렸습니다.


http://www.pydev.org/ 사이트에 방문해보니 친절하게 pydev update url이 있습니다.

   Latest version: http://pydev.org/updates
   Nightly build: http://pydev.org/nightly


Eclipse의 help > Install new software 창을 띄워서 [Work with]에 위 latest version을 입력해 줍니다.

그리고 나서 필요한 software를 선택한 이후 설치를 진행하면 끝~ 입니다.





License를 확인하고 accept 클릭 합니다.

설치 중간에 certification관련 warning 팝업도 나오는데 일단 accept하였습니다.



Eclipse를 재시작하고 Window > Preferences 에 가보면

PyDev가 추가된 것을 확인할 수 있습니다.


인제 Python interpreter만 연결해주면 준비 끝



예제로 프로젝트를 하나 생성해 보도록 하겠습니다.


제 PC에는 Python3가 깔려 있기에

아래 Grammar version과 interpreter를 변경하였습니다.



src 폴더를 하나 추가하고, 그 안에 PyDev Module로 python script를 추가 합니다.



간단히 play버튼을 누르면 정상적으로 동작하는 것을 확인할 수 있습니다.




1.2. ctypes


Linux, windows, Max OS와 같은 외부 library를 호출하기 위해 Python에서는 ctypes library를 지원합니다.

아래 공식 documenet를 확인해 봅니다.


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


아래는 ctypes 라이브러리를 이용하여 window의 c runtime 함수인 printf를 이용하여 텍스트 메세지를 출력하는 예제입니다.


chapter1-test.py


from ctypes import *


msvcrt = cdll.msvcrt

message_string = "Hello World!\n"

msvcrt.printf("Testing: %s".encode('ascii'), message_string.encode('ascii'))



Testing: Hello World!


Python3의 경우 모든 string을 unicode로 처리하기 때문에 위와 같이 ascii로 변환이 필요합니다.


Python에서 indent를 가지고 class나 함수의 범위를 구분합니다. 

아래 예제에서 Union선언한 다음 제가 실수로 tap을 value 선언하는 곳 이후에 넣어 두었더니 실행이 안되네요. 주의! 


chapter1-unions.py


from ctypes import *


class barley_amount(Union):

    _fields_ = [

        ("barley_long", c_long),

        ("barley_int", c_int),

        ("barley_char", c_char*8)

    ]

    

value = input("Enter the amount of barley to pu into the beer vat:")

my_barley = barley_amount(int(value))

print ("Barley amount as a long: %ld" % my_barley.barley_long)

print ("Barley amount as a long: %d" % my_barley.barley_int)

print ("Barley amount as a long: %s" % my_barley.barley_char)


Enter the amount of barley to pu into the beer vat:66

Barley amount as a long: 66

Barley amount as a long: 66

Barley amount as a long: b'B'


Python3부터는 raw_input이 input으로 rename되었습니다.


반응형

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

Windows Debugger with PyDev  (0) 2016.06.29
CPU registers and dis-assemble of Python  (0) 2016.06.29
Trojan using Git hub  (0) 2016.03.30
ICMP Decoding with Python  (0) 2016.03.16

+ Recent posts