툴
툴은 위험성이 있는 함수 호출을 찾아내고 그런 함수 호출을 추적한다.
위험하다고 생각되는 함수가 호출되면 스택에서 네 개의 파라미터를 참조한다.
해당 함수가 오버플로우를 발생시킬 수 있는지 판단하고 해당 프로세스의 스냅샷을 만든다.
접근 위반이 발생하면 스크립트는 마지막으로 위험한 함수가 호출된 시점으로 프로세스를 되돌린다.
danger_track.py
-------------------------------------------------------------------
#danger_track.py
from pydbg import *
from pydbg.defines import *
import utils
# 접근 위반이 발생한 후에 조사할 명령의 최대 개수
MAX_INSTRUCTIONS = 10
# 다음은 모든 위험한 함수를 포함하는 완벽한 리스트는 아니다.
dangerous_functions = {
"strcpy" : "msvcrt.dll",
"strncpy" : "msvcrt.dll",
"vsprintf" : "msvcrt.dll"
}
dangerous_functions_resolves = {}
crash_encountered = False
instruction_count = 0
def danger_handler(dbg):
# 스택의 내용을 출력한다.
# 일반적으로 몇 개의 파라미터만을 사용하기 때문에
# ESP ~ ESP+20의 내용을 출력해도 원하는 충분한 정보를 얻을 수 있다.
esp_offset = 0
print "[*] Hit %s" % dangerous_functions_resolves[dbg.context.Eip]
print
"=============================================================="
while esp_offset <=20:
parameter = dbg.smart_derefrence(dbg.context.Esp + esp_offset)
print "[ESP + %d] => %s" %(esp_offset, parameter)
esp_offset +=4
print
"=============================================================="
dbg.suspend_all_threads()
dbg.process_snapshot()
dbg.resume_all_threads()
return DBG_CONTINUE
def access_violation_handler(dbg):
global crash_encountered
# 접근 위반을 처리하고 프로세스를 마지막 위험한 함수가
# 호출된 시점으로 되될린다.
if dbg.dbg.u.Exception.dwFirstChance:
return DBG_EXCEPTION_NOT_HANDLED
crash_bin = utils.crash_binning.crash_binning()
crach_bin.record_crash(dbg)
print crash_bin.crash_synopsis()
if crash_encountered == False:
dbg.suspend_all_threads()
dbg.process_restore()
crash_encountered = True
# 각 스레드에 대한 단일 스텝을 설정한다.
for thread_id in dbg.enumerate_threads():
print "[*] Setting single step for thread : 0x%08x" % thread_id
h_thread = dbg.open_thread(thread_id)
dbg.single_step(True, h_thread)
dbg.close_handle(h_thread)
# 이제 단일 스텝 핸들러에게 제어권을 넘기기 위해
# 프로세스가 다시 실행되게 만든다.
dbg.resume_all_threads()
return DBG_CONTINUE
else :
dbg.terminate_process()
return DBG_EXCEPTION_NOT_HANDLED
def single_step_handler(dbg):
global instruction_count
global crash_encountered
if crash_encountered:
if instruction_count == MAX_INSTRUCTIONS:
dbg.single_step(False)
return DBG_CONTINUE
else :
# 명령을 디스어셈블한다.
instruction = dbg.disasm(dbg.context.Eip)
print "#%d\t0x%08x : %s" % (instruction_count, dbg.context.Eip,
intruction)
---------------------------------------------------------------------
위 소스를 효과적으로 테스트하려면 취약점을 갖고 있는 소프트웨어 어플리케이션에 이 스크립트를 붙이고 어플리케이션이 에러를 발생시키게 만들면 된다.
'파이썬 스터디 과제 > 파이썬 해킹 프로그래밍' 카테고리의 다른 글
5장-2 Immunity 디버거 101 (0) | 2015.01.25 |
---|---|
5장-1 Immunity 디버거 (0) | 2015.01.25 |
4장-3 프로세스 스냅샷 (0) | 2015.01.24 |
4장-2 접근 위반 핸들러 (0) | 2015.01.24 |
4장-1 브레이크포인트 확장 (0) | 2015.01.21 |
댓글