From f9b8762d49ef16bf2dd7b814d6f1ef06594b2951 Mon Sep 17 00:00:00 2001 From: Brian Potchik Date: Fri, 21 Mar 2025 11:00:15 -0400 Subject: Add some workflow machine control commands to the C++ API. --- binaryninjaapi.h | 36 ++++++++++++++++++++++++++++ workflow.cpp | 73 ++++++++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 91 insertions(+), 18 deletions(-) diff --git a/binaryninjaapi.h b/binaryninjaapi.h index 5fc2f5a6..c83e8ee8 100644 --- a/binaryninjaapi.h +++ b/binaryninjaapi.h @@ -10077,10 +10077,33 @@ namespace BinaryNinja { Ref m_view; Ref m_function; + bool PostRequest(const std::string& command); + public: WorkflowMachine(Ref view); WorkflowMachine(Ref function); + /*! Start the workflow WorkflowMachine + Starts the workflow machine for the given BinaryView or Function. + \return true if the command is accepted, false otherwise. + */ + bool Run(); + + /*! Halt the workflow machine + + Halts analysis at a resumable point. + \return true if the command is accepted, false otherwise. + */ + bool Halt(); + + /*! Reset the workflow machine + + Resets the workflow machine to its initial state. + \return true if the command is accepted, false otherwise. + */ + bool Reset(); + + /*! Enable the workflow machine Re-enables the workflow machine if it is in the Suspend state. @@ -10096,6 +10119,19 @@ namespace BinaryNinja { */ bool Disable(); + /*! Step the workflow machine + + Steps the workflow machine through a single activity. + \return true if the command is accepted, false otherwise. + */ + bool Step(); + + /*! Get the current state of the workflow machine + + Returns the current state of the workflow machine. + \return The current state of the workflow machine + */ + std::string GetState(); std::optional QueryOverride(const std::string& activity); bool SetOverride(const std::string& activity, bool enable); diff --git a/workflow.cpp b/workflow.cpp index 63066baa..9edb6ea6 100644 --- a/workflow.cpp +++ b/workflow.cpp @@ -110,23 +110,12 @@ bool AnalysisContext::Inform(const string& request) } -WorkflowMachine::WorkflowMachine(Ref view): m_view(view) -{ - -} - - -WorkflowMachine::WorkflowMachine(Ref function): m_function(function) -{ - -} - - -bool WorkflowMachine::Enable() +bool WorkflowMachine::PostRequest(const std::string& command) { rapidjson::Document request(rapidjson::kObjectType); rapidjson::Document::AllocatorType& allocator = request.GetAllocator(); - request.AddMember("command", "enable", allocator); + rapidjson::Value commandValue(command.c_str(), command.size(), allocator); + request.AddMember("command", commandValue, allocator); rapidjson::StringBuffer buffer; rapidjson::Writer writer(buffer); request.Accept(writer); @@ -146,11 +135,59 @@ bool WorkflowMachine::Enable() } +WorkflowMachine::WorkflowMachine(Ref view): m_view(view) +{ + +} + + +WorkflowMachine::WorkflowMachine(Ref function): m_function(function) +{ + +} + + +bool WorkflowMachine::Run() +{ + return PostRequest("run"); +} + + +bool WorkflowMachine::Halt() +{ + return PostRequest("halt"); +} + + +bool WorkflowMachine::Reset() +{ + return PostRequest("reset"); +} + + +bool WorkflowMachine::Enable() +{ + return PostRequest("enable"); +} + + bool WorkflowMachine::Disable() +{ + return PostRequest("disable"); +} + + +bool WorkflowMachine::Step() +{ + return PostRequest("step"); +} + + +string WorkflowMachine::GetState() { rapidjson::Document request(rapidjson::kObjectType); rapidjson::Document::AllocatorType& allocator = request.GetAllocator(); - request.AddMember("command", "disable", allocator); + request.AddMember("command", "status", allocator); rapidjson::StringBuffer buffer; rapidjson::Writer writer(buffer); request.Accept(writer); @@ -163,10 +200,10 @@ bool WorkflowMachine::Disable() rapidjson::Document response(rapidjson::kObjectType); response.Parse(jsonResult.c_str()); - if (response.HasMember("commandStatus") && response["commandStatus"].HasMember("accepted")) - return response["commandStatus"]["accepted"].GetBool(); + if (response.HasMember("machineState") && response["machineState"].HasMember("state")) + return response["machineState"]["state"].GetString(); - return false; + return "Invalid"; } -- cgit v1.3.1