Pykd——Python extension for WinDbg

来源:互联网 发布:audition cc for mac 编辑:程序博客网 时间:2024/05/22 08:13

在使用windbg脚本的时候,感觉很不方便。有时候想根据断点触发时的栈信息判断是否需要继续,但是没法定义变量来获取那些值,即便使用$tn和$un之类的伪变量获取那些值也很难进行复杂的判断或者运算。所以还是需要有更灵活的语言来辅助,所以找到了Pykd,虽然Immunity可以取代它,但有时不得不用windbg,接下来就让我们来学习下这个插件。

一、安装

从官网(https://pykd.codeplex.com/releases)下载对应版本双击便是。假如是手动安装,只需要将pykd.pyd复制到windbg安装目录的winext目录。

打开windbg,在命令行输入:

.load pykd.pyd!py help!pycmd

测试是否安装成功。

二、使用

可以在!pycmd中进行命令交互,也可以输入脚本:!py file1.py。下面重点说下常用的API:

Functions

  • addr64地址转换
  • appendSymbolPath增加符号表路径
  • attachProcess附加进程
  • breakin相当于CTRL+BREAK
  • bugCheckData
  • callExt调用扩展库函数
  • compareMemory比较内存
  • containingRecord
  • currentTime获取当前时间
  • debugAccess获取访问属性
  • dbgCommand执行windgb内置命令
  • detachProcess
  • dprint打印
  • dprintln印一行
  • expr计算表达式
  • findMemoryRegion
  • findSymbol查找符号,返回符号地址
  • findSymbolAndDisp
  • getCurrentProcess
  • getCurrentProcessId
  • getCurrentThreadId
  • getFrame获取栈帧,返回stackFrame类
  • getImplicitThread
  • getLocals获取局部变量,返回locals类
  • getOffset获取指定符号虚拟地址
  • getParams获取参数
  • getProcessThreads
  • getProcessorMode
  • getProcessorType
  • getSourceFile
  • getSourceLine
  • getStack获取栈信息,返回stackFrame类的列表
  • getStackWow64
  • getSymbolPath
  • getVaProtect
  • go运行
  • is64bitSystem
  • isDumpAnalyzing
  • isKernelDebugging
  • isValid
  • isWindbgExt
  • killProcess
  • lastEvent返回上一个事件类型,即事件类型常量
  • lastException返回上一个异常信息,即exceptionInfo类
  • loadAnsiString读取一段内存,一般返回一个列表
  • loadBytes读取一段内存,一般返回一个列表
  • loadCStr同上.......
  • loadChars
  • loadDWords
  • loadDoubles
  • loadDump
  • loadExt
  • loadFloats
  • loadPtrList
  • loadPtrs
  • loadQWords
  • loadSignBytes
  • loadSignDWords
  • loadSignQWords
  • loadSignWords
  • loadUnicodeString
  • loadWChars
  • loadWStr
  • loadWords
  • pageSize
  • ptrByte读取内存值
  • ptrDWord读取内存值
  • ptrDouble同上.......
  • ptrFloat
  • ptrMWord
  • ptrPtr
  • ptrQWord
  • ptrSignByte
  • ptrSignDWord
  • ptrSignMWord
  • ptrSignQWord
  • ptrSignWord
  • ptrSize
  • ptrWord
  • rdmsr
  • reg读取寄存器值,返回cpuReg类
  • removeBp移除断点
  • removeExt
  • setBp设置断点
  • setCurrentProcess
  • setImplicitThread
  • setProcessorMode
  • setSymSrvDir
  • setSymbolPath
  • sizeofsizeof
  • startProcess打开进程
  • step步入
  • systemUptime
  • trace步过
  • typedVarArray
  • typedVarList
  • writeDump
  • wrmsr

Classes

  • cpuReg调用reg()函数返回此类

    Methods:

    • index寄存器序号
    • name寄存器名称
  • din 输入类
  • disasm汇编与反汇编类

    Methods:

    • __init__
    • asm
    • begin
    • current
    • disasm
    • ea
    • findOffset
    • instruction
    • jump
    • jumprel
    • length
    • reset
  • dout
  • eventHandler事件类,断点触发事件、模块加载事件等等

    Methods:

    • __init__
    • onBreakpoint(long bpNum)返回执行状态常量
    • onException(obj exceptionInfo)返回执行状态常量
    • onExecutionStatusChange(long addr)返回执行状态常量
    • onModuleLoad(long moduleBase,str moduleName)返回执行状态常量
    • onModuleUnload(long moduleBase,str moduleName)返回执行状态常量
  • exceptionInfo异常信息,调用lastException()返回此类。

    Properties:

    • ExceptionAddress
    • ExceptionCode
    • ExceptionFlags
    • ExceptionRecord
    • FirstChance
    • Parameters
  • intBase返回基址
  • locals局部变量,调用getLocal()返回此类。
  • module模块类,输入任意地址或名称
  • stackFrame栈帧类,调用getFrame()返回此类。

    Properties:

    • frameNumber栈帧数
    • frameOffset栈帧地址
    • instructionOffset指令地址
    • locals局部变量
    • params参数
    • returnOffset返回地址
    • stackOffset栈地址
  • typeBuilder
  • typeInfo
  • typedVar

常量:

Breakpoint事件类型lastEvent()返回
ChangeDebuggeeState事件类型lastEvent()返回
ChangeEngineState 事件类型 lastEvent()返回
ChangeSymbolState 事件类型 lastEvent()返回
CreateProcess 事件类型lastEvent()返回
CreateThread 事件类型lastEvent()返回
Exception 事件类型lastEvent()返回
ExitProcess 事件类型lastEvent()返回
ExitThread 事件类型lastEvent()返回
LoadModule 事件类型lastEvent()返回
SessionStatus 事件类型lastEvent()返回
SystemError 事件类型lastEvent()返回
UnloadModule 事件类型lastEvent()返回


BaseException 异常类型
SymbolException 异常类型
TypeException 异常类型
WaitEventException 异常类型
MemoryException  异常类型
WrongEventTypeException 异常类型


DebugAccessIo Debug Access Type
Read Debug Access Type
Execute Debug Access Type
Write Debug Access Type


Debug Attributes of the file
InfoInferred Attributes of the file
FileFlag Attributes of the file
SpecialBuild Attributes of the file
Patched Attributes of the file
PreRelease Attributes of the file
PrivateBuild Attributes of the file

FixedFileInfo Version information for a file

根据以上API,我们完全可以写一个自动化调试脚本。

例1:获取某进程所有加载模块

from pykd import *startProcess("c:\\windows\\system32\\calc.exe",1)modules=dbgCommand('lm 1m').split('\n')for md in modules:    try:        obj_md=module(str(md))        print obj_md    except BaseException:        passwhile 1:    go()

0 0
原创粉丝点击