[3주] 04장 - 종합
악용될 수 있는 소프트웨어 애플리케이션의 결함을 제거하는데 도움이 되는 유틸리티프로그램을 짜보자
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | #danger_track.py #-*- coding: utf-8 -*- from pydbg import * from pydbg.defines import * import utils #접근 위반이 발생한 후에 조사할 명령의 최대 개수 MAX_INSTRUCTIONS = 10 #다음은 모든 위험한 한수에 포함하는 완벽한 리스트는 아니다. dangerous_functions = { "strcpy" : "msvcrt.dll", "strncpy" : "msvcrt.dll", "sprintf" : "msvcrt.dll", "vsprintf" : "msvcrt.dll"} dangerous_functions_resolved = {} crash_encountered = False instruction_count = 0 def danger_handler(dbg): #스택의 내용을 출력한다. #일반적으로 몇 개의 파라미터만을 사용하기 때문에 ESP~ESP+20 내용만 출력해도됨 esp_offset = 0 print "[*] Hit %s" % dangerous_functions_resolved[dbg.context.Eip] print "============================================================" while esp_offset <=20: parameter = dbg.smart_dereference(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() crash_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, instruction) instruction_count +=1 dbg.single_step(True) return DBG_CONTINUE dbg = pydbg() pid = int(raw_input("Enter the PID you wish to monitor :")) dbg.attach(pid) #리스트의 모든 위험한 함수를 호출하고 브레이크포인트설정 for func in dangerous_functions.keys(): func_address = dbg.func_resolve(dangerous_functions[func],func) print "[*] Resolved breakpoint: %s -> 0x%08x" % (func,func_address) dbg.bp_set(func_address, handler = danger_handler) dangerous_functions_resolved[func_address] = func dbg.set_callback( EXCEPTION_ACCESS_VIOLATION, access_violation_handler) dbg.set_callback(EXCEPTION_SINGLE_STEP, single_step_handler) dbg.run() | cs |
buffer_overflow.py에 붙인 결과
restore에서 자꾸 매개변수가 틀리다는 에러가뜨는데 왜그런지는 모르겠다
'Study > 파이썬 해킹 프로그래밍' 카테고리의 다른 글
[3주] 05장 - 윈도우 DEP우회 (0) | 2015.01.26 |
---|---|
[3주] 05장 - Immunity Debugger 공격코드에서 사용할 명령찾기 (0) | 2015.01.26 |
[3주] 04장 - 프로세스 스냅샷 (0) | 2015.01.26 |
[3주] 04장 - 접근 위반 핸들러 (0) | 2015.01.26 |
[3주] 04장 - 브레이크포인트 확장 (0) | 2015.01.26 |