2016년 12월 29일 목요일

파이썬 해킹 프로그래밍

Python 해킹 프로그래밍

Immunity pycmd를 사용한 DEP 우회

Immunity 디버거의 파이썬 라이브러리를 이용해 로드된 바이너리에서 원하는 명령을 쉽게 찾는 파이썬 스크립트
form immlib import *

def main(args):

    imm    = Debugger()
    search_code = " ".join(args)
    search_bytes = imm.Assemble(search_code)    # 찾을 명령어를 어셈블함.
    search_results = imm.Search(search_bytes)    # 로드된 바이너리에서 해당 명령을 찾음
    for hit in search_results:
        # 메모리 페이지 및 실행 가능한지 확인
        code_page = imm.getMemoryPagebyAddress(hit)    # 메모리에서 원하는 명령의 메모리 페이지 구함
        access = code_page.getAccess(human = True)    # 실행 가능한 메모리 페이지 인지 확인

        if "execute" in access.lower():
            imm.log( "[*] Found: %s (0x%08x)" % (search_code, hit), address=hit)    # 로그 찾을 이용해 출력

    return "[*] Finished searching for instructions, check the Log window."
실행 명령은 immunity에 아래와 같이 사용할 수 있음.
(작성한 코드는 디버거를 설치한 경로의 PyCommands에 저장한다. C:\Program Files\Immunity Inc\Immunity Debugger\PyCommands)
!findinstruction <찾으려는 명령>

DLL 인젝션

dll_injector.py
import sys

from ctypes import *

PAGE_READWRITE    = 0x04
PROCESS_ALL_ACCESS    = ( 0x000F0000 | 0x00100000 | 0xFFF )
VIRTUAL_MEM    = (0x1000 | 0x2000)

kernel32    = windll.kernel32
pid    = sys.argv[1]
dll_path    = sus.argv[2]
dll_len    = len(dll_path)

#DLL을 인젝션할 프로세스의 핸들을 구함 - DLL의 경로를 저장할 수 있는 충분한 메모리 공간 필요

h_process = kernel32.OpenProcess( PROCESS_ALL_ACCESS, False, int(pid) )

if not h_process:
    print "[*] Couldn't acquire a handle to PID: %s" % pid 
    sys.exit(0)

# DLL의 경로를 저장하기 위한 공간 할당 후 해당 공간에 DLL의 경로를 써 넣는다.
arg_address = kernel32.VirtualAllocEx(h_process, 0, dll_len, VIRTUAL_MEM, PAGE_READWRITE)

# 할당한 공간에 DLL 경로 쓰기
written = c_int(0)
kernel32.WriteProcessMemory(h_process, arg_address, dll_path, dll_len, byref(written))

# LoadLibraryA의 주소 획득
h_kernel32 = kernel32.GetModuleHandleA("kernel32.dll")
h_loadlib = kernel32.GetProcAddress(h_kernel32, "LoadLibraryA")

# 원격 스레드를 생성하고 DLL의 경로를 파라미터로 전달
# LoadLibraryA가 실행되게 만드는 코드

thread_id = c_ulong(0)

if not kernel32.CreateRemoteThread(h_process, None, 0, h_loadlib, arg_address, 0, byref(thread_id)):
    print "[*]Failed to inject the DLL. Exiting."
    sys.exit(0)
print "[*] Remote thread with ID 0x%08x created." % thread_id.value
실행 방법
python dll_injector.py <dll의 경로="">
참조
  • 파이썬 해킹 프로그래밍

댓글 없음:

댓글 쓰기