DumpFromOEP.py for Immunity Debugger
PyCommand 디렉토리 아래에 스크립트 파일을 저장한 후, eip가 OEP에 있을 때 커맨드라인에서 다음과 같이 입력하면 실행이 됩니다.
!dumpfromoep [a path of the output file]
OEP까지 직접 찾아가야 하고 IAT도 수동으로 고쳐줘야 합니다. UPX와 UPack으로 패킹된 바이너리는 정확히 덤프하는 걸 확인했습니다.
dumpfromoep.py
__author__ = '6l4ck3y3'
__version__ = '1.0.0'
__contact__ = '6l4ck3y3@gmail.com'
import immlib
import pefile
def main (args):
imm = immlib.Debugger ()
name = imm.getDebuggedName ()
try:
mod = imm.getModule (name)
if not mod:
raise Exception, "Couldn't find %s" % name
except Exception, msg:
return "Error: %s" % msg
pe = pefile.PE (name = mod.getPath ())
memory = imm.readMemory (pe.OPTIONAL_HEADER.ImageBase, pe.OPTIONAL_HEADER.SizeOfImage)
out = pefile.PE (data = memory)
orig_eip = imm.getRegs ()['EIP']
out.FILE_HEADER.NumberOfSections = pe.FILE_HEADER.NumberOfSections
out.OPTIONAL_HEADER.ImageBase = pe.OPTIONAL_HEADER.ImageBase
out.OPTIONAL_HEADER.SizeOfImage = pe.OPTIONAL_HEADER.SizeOfImage
out.OPTIONAL_HEADER.AddressOfEntryPoint = orig_eip - pe.OPTIONAL_HEADER.ImageBase
out.OPTIONAL_HEADER.BaseOfCode = pe.OPTIONAL_HEADER.BaseOfCode
out.OPTIONAL_HEADER.BaseOfData = pe.OPTIONAL_HEADER.BaseOfData
imm.log ("ImageBase = " + hex (out.OPTIONAL_HEADER.ImageBase))
imm.log ("SizeOfImage = " + hex (out.OPTIONAL_HEADER.SizeOfImage))
imm.log ("AddressOfEntryPoint = " + hex (out.OPTIONAL_HEADER.AddressOfEntryPoint))
imm.log ("BaseOfCode = " + hex (out.OPTIONAL_HEADER.BaseOfCode))
imm.log ("BaseOfData= " + hex (out.OPTIONAL_HEADER.BaseOfData))
imm.log ("")
for i in range (pe.FILE_HEADER.NumberOfSections):
out.sections[i].Name = pe.sections[i].Name
out.sections[i].VirtualSize = pe.sections[i].Misc_VirtualSize
out.sections[i].VirtualAddress = pe.sections[i].VirtualAddress
out.sections[i].SizeOfRawData = pe.sections[i].Misc_VirtualSize
out.sections[i].PointerToRawData = pe.sections[i].VirtualAddress
out.sections[i].Characteristics = pe.sections[i].Characteristics
imm.log ("Name = " + repr (out.sections[i].Name))
imm.log ("VirtualSize = " + hex (out.sections[i].VirtualSize))
imm.log ("VirtualAddress = " + hex (out.sections[i].VirtualAddress))
imm.log ("SizeOfRawData = " + hex (out.sections[i].SizeOfRawData))
imm.log ("PointerToRawData = " + hex (out.sections[i].PointerToRawData))
imm.log ("Characteristics = " + hex (out.sections[i].Characteristics))
imm.log ("")
out.write (args[0])