
x64dbg 条件断点实战SetLibrarianBreakpointCommandCondition 命令详解与底层实现【免费下载链接】x64dbgAn open-source user mode debugger for Windows. Optimized for reverse engineering and malware analysis.项目地址: https://gitcode.com/gh_mirrors/x6/x64dbg导读SetLibrarianBreakpointCommandCondition是 x64dbg 中用于设置librarian 断点DLL 模块断点命令条件的核心命令。它决定断点命中后关联的脚本命令何时真正执行未指定条件时命令在调试器中断时执行指定条件后则只在条件表达式求值为真时执行。本文以该命令的官方文档为主体结合仓库源码命令注册、断点结构体、命中求值流程、数据库迁移、GUI 调用链深度讲解其语法、实战用法与底层原理帮助你在恶意软件分析、插件加载监控等场景中精准控制自动化命令的执行时机。命令概览librarian 断点与命令条件在 x64dbg 中librarian 断点BPDLL类型对应 DLL 模块加载/卸载断点通过模块名称而非内存地址来标识。与之配套的SetLibrarianBreakpoint*系列命令可以精细控制这类断点的每一个行为维度SetLibrarianBreakpointCommand设置断点命中时要执行的命令SetLibrarianBreakpointCommandCondition设置该命令的执行条件即本文主题SetLibrarianBreakpointCondition设置断点自身的中断条件SetLibrarianBreakpointLog/SetLibrarianBreakpointLogCondition设置日志文本与日志条件。SetLibrarianBreakpointCommandCondition的核心语义摘自 官方文档为设置 librarian 断点的命令条件。当命令条件未指定时命令将在调试器中断时执行否则命令将在条件满足时执行。也就是说command与commandCondition是配对关系命令文本决定做什么命令条件决定什么时候做。语法与参数说明命令语法如下SetLibrarianBreakpointCommandCondition arg1 [, arg2]参数含义必填性说明arg1DLL 名称必填目标模块名例如kernel32.dll。librarian 断点以模块名而非地址为标识BpGetDLLBpAddr 内部会基于文件名解析出断点哈希键[arg2]命令条件表达式可选未指定时使用默认条件等价于无条件命中即执行指定后仅当表达式求值为真时才执行命令结果变量该命令不设置任何结果变量与SetLibrarianBreakpointCommand的行为一致参见 SetLibrarianBreakpointCommand.md。参数含义的源码印证在 breakpoint.h 的BREAKPOINT结构体中两者是相互独立又关联的两个字段std::string commandText; // script command to execute. std::string commandCondition; // condition to execute the command而在 breakpoint.cpp 中BpSetCommandCondition通过BpInfoFromAddr定位到对应BPDLL断点后直接将参数写入commandCondition字段并在持有LockBreakpoints互斥锁的前提下完成更新bool BpSetCommandCondition(duint Address, BP_TYPE Type, const char* Condition) { ASSERT_DEBUGGING(Command function call); EXCLUSIVE_ACQUIRE(LockBreakpoints); BREAKPOINT* bpInfo BpInfoFromAddr(Type, Address); if(!bpInfo) return false; bpInfo-commandCondition Condition; return true; }实战用法示例示例 1无条件执行命中即运行命令不传第二个参数断点命中后命令始终执行SetLibrarianBreakpointCommand kernel32.dll, log \kernel32.dll loaded\ SetLibrarianBreakpointCommandCondition kernel32.dll示例 2条件化执行配合表达式系统仅当条件为真时执行命令。例如只在特定模块加载次数大于某值时执行$breakpointcounter是断点命中计数器变量由命中流程在 debugger.cpp 中通过varset($breakpointcounter, bp.hitcount, true)写入SetLibrarianBreakpointCommandCondition kernel32.dll, $breakpointcounter 1示例 3与命令文本的等价内联形式cmdif在 GUI 的断点信息展示中命令文本与命令条件会被合并为cmdif(condition, command)的内联表达式——从 _exports.cpp 的序列化代码可以看到这一对应关系if(!bp.commandText.empty()) { if(!bp.commandCondition.empty()) { temp_string GuiTranslateText(QT_TRANSLATE_NOOP(DBG, cmdif)); temp_string (; temp_string bp.commandCondition; temp_string , ; } else { temp_string GuiTranslateText(QT_TRANSLATE_NOOP(DBG, cmd)); temp_string (; } // ... commandText }因此在手工编写命令时SetLibrarianBreakpointCommandSetLibrarianBreakpointCommandCondition的组合本质上等价于单条cmdif(condition, command)表达式的拆分写法二者可以按场景灵活选用。命令背后的源码调用链注册与分发该命令在 x64dbg.cpp 中注册为带参数解析的命令true表示需要解析参数dbgcmdnew(SetLibrarianBreakpointCommand, cbDebugSetBPXDLLCommand, true); //set breakpoint command on hit dbgcmdnew(SetLibrarianBreakpointCommandCondition, cbDebugSetBPXDLLCommandCondition, true); //set breakpoint commandCondition参数经解析后进入 cmd-conditional-breakpoint-control.cpp 的分发函数bool cbDebugSetBPXDLLCommand(int argc, char* argv[]) { return cbDebugSetBPXCommandCommon(BPDLL, argc, argv); } bool cbDebugSetBPXDLLCommandCondition(int argc, char* argv[]) { return cbDebugSetBPXCommandConditionCommon(BPDLL, argc, argv); }两者最终都经由cbDebugSetBPXTextCommon落地到断点层的BpSetCommandText/BpSetCommandCondition。值得注意的是普通断点SetBreakpointCommandCondition、硬件断点、内存断点、异常断点共用同一套cbDebugSetBPXCommandConditionCommon实现仅传入的BP_TYPE不同——这解释了为何系列命令的文档结构与语义高度一致。断点命中时的条件求值真正决定命令何时执行的逻辑位于 debugger.cpp 的断点命中流程中if(!bp.commandCondition.empty()) { commandCondition getConditionValue(bp.commandCondition); if(commandCondition -1) { dputs(QT_TRANSLATE_NOOP(DBG, Error when evaluating command condition.)); breakCondition -1; // Force breaking when an error occurs commandCondition 0; // Dont execute any command if an error occurs } } else { // NOTE: This behavior was changed in a breaking way, but too many people were confused if(breakCondition ! -1) commandCondition 1; // If no condition is set, always execute the command else commandCondition 0; // Dont execute any command if an error occurs }从中可以提炼出三条重要规则条件为空 默认执行未设置命令条件时只要断点命中且中断条件无错误命令就必然执行commandCondition 1条件求值出错 拒绝执行条件表达式求值失败返回-1时命令不会执行且调试器会强制中断breakCondition -1同时在日志中打印 Error when evaluating command condition.条件为真才执行条件表达式求值结果非 0 时命令才被真正触发。历史行为迁移从$breakpointcondition到默认 1在 breakpoint.cpp 的数据库加载逻辑中保留了一条关键的历史兼容性说明// On 2023-06-10 the default of the command condition was changed from $breakpointcondition to 1 // If we detect an older database, try to preserve the old behavior. if(migrateCommandCondition !breakpoint.commandText.empty() !breakpoint.commandCondition.empty()) { breakpoint.commandCondition $breakpointcondition; }这意味着早期版本中未显式设置命令条件时默认条件是$breakpointcondition跟随中断条件2023-06-10 起默认改为常量1无条件执行加载旧数据库时会自动迁移以保留旧行为。这一点对于研究旧数据库文件或升级老工程脚本的读者尤为重要。GUI 与数据库中的持久化该命令不仅可在命令行使用GUI 断点管理界面也会生成同名命令来完成属性写入与刷新BreakpointsView.cpp 中librarian 断点编辑框提交时构造SetLibrarianBreakpointCommandCondition %1, \%2\命令字符串Breakpoints.cpp 在批量更新断点属性时同样调用SetLibrarianBreakpointCommandCondition并配合DbgCmdEscape做参数转义。数据库层面commandCondition会以 JSON 字段形式保存与恢复见 breakpoint.cpp 的导出与 L983 的导入因此该配置随数据库持久化重新加载数据库后依然生效。与相关命令的对照命令作用对象功能SetLibrarianBreakpointConditionlibrarian 断点设置断点自身的中断条件是否停止SetLibrarianBreakpointCommandlibrarian 断点设置命中后执行的命令文本SetLibrarianBreakpointCommandConditionlibrarian 断点设置命令的执行条件本文SetLibrarianBreakpointLoglibrarian 断点设置命中时记录的日志文本SetLibrarianBreakpointLogConditionlibrarian 断点设置日志的执行条件典型组合是先用SetLibrarianBreakpointCondition控制何时中断再用SetLibrarianBreakpointCommandSetLibrarianBreakpointCommandCondition控制中断后自动执行什么命令、满足什么额外条件才执行从而把模块加载监控、自动 dump、自动记录调用参数等操作完全脚本化。使用注意事项arg1 必须是模块名librarian 断点以 DLL 名为标识arg1传错模块名会导致找不到目标断点命令静默失败BpInfoFromAddr返回空指针时BpSetCommandCondition返回false参数含特殊字符需转义在 GUI 与脚本中拼接命令时条件或命令文本若包含逗号、引号等特殊字符应使用DbgCmdEscape之类的转义处理参见 Breakpoints.cpp 的用法避免破坏参数解析条件出错会导致强制中断命令条件表达式求值失败不仅不会执行命令还会把breakCondition置为-1强制调试器中断因此条件表达式应确保引用已定义变量、避免除零等运行时错误默认行为随版本变化未设置命令条件时当前版本默认命中即执行若加载旧数据库迁移逻辑可能将其还原为$breakpointcondition语义排查命令未按预期执行时需留意这一差异命令不产生结果变量与多数Set*系列命令一致本命令不写任何$result之类的变量无需也不应在脚本中依赖其返回值做分支判断。【免费下载链接】x64dbgAn open-source user mode debugger for Windows. Optimized for reverse engineering and malware analysis.项目地址: https://gitcode.com/gh_mirrors/x6/x64dbg创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考