From c398bd1ea423f1f9a4fa9e6e3fcadc9143a99f99 Mon Sep 17 00:00:00 2001 From: Glenn Smith Date: Tue, 6 Sep 2022 19:14:03 -0400 Subject: [Enterprise] C++ api and example --- examples/CMakeLists.txt | 3 + examples/enterprise_test/CMakeLists.txt | 33 ++++++++ examples/enterprise_test/src/enterprise_test.cpp | 101 +++++++++++++++++++++++ 3 files changed, 137 insertions(+) create mode 100644 examples/enterprise_test/CMakeLists.txt create mode 100644 examples/enterprise_test/src/enterprise_test.cpp (limited to 'examples') diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 96e13e00..8a831b46 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -11,4 +11,7 @@ if(NOT DEMO) add_subdirectory(workflows/inliner) add_subdirectory(workflows/tailcall) endif() +if(ENTERPRISE) + add_subdirectory(enterprise_test) +endif() add_subdirectory(x86_extension) diff --git a/examples/enterprise_test/CMakeLists.txt b/examples/enterprise_test/CMakeLists.txt new file mode 100644 index 00000000..9833817c --- /dev/null +++ b/examples/enterprise_test/CMakeLists.txt @@ -0,0 +1,33 @@ +cmake_minimum_required(VERSION 3.9 FATAL_ERROR) + +project(enterprise_test CXX C) + +add_executable(${PROJECT_NAME} + src/enterprise_test.cpp) + +if(NOT BN_API_BUILD_EXAMPLES AND NOT BN_INTERNAL_BUILD) + # Out-of-tree build + find_path( + BN_API_PATH + NAMES binaryninjaapi.h + HINTS ../.. binaryninjaapi $ENV{BN_API_PATH} + REQUIRED + ) + add_subdirectory(${BN_API_PATH} api) +endif() + +target_link_libraries(${PROJECT_NAME} + binaryninjaapi) + +if (NOT WIN32) + target_link_libraries(${PROJECT_NAME} + dl) +endif() + +set_target_properties(${PROJECT_NAME} PROPERTIES + CXX_STANDARD 17 + CXX_VISIBILITY_PRESET hidden + CXX_STANDARD_REQUIRED ON + VISIBILITY_INLINES_HIDDEN ON + POSITION_INDEPENDENT_CODE ON + RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/out/bin) diff --git a/examples/enterprise_test/src/enterprise_test.cpp b/examples/enterprise_test/src/enterprise_test.cpp new file mode 100644 index 00000000..a0bff140 --- /dev/null +++ b/examples/enterprise_test/src/enterprise_test.cpp @@ -0,0 +1,101 @@ +// This is just bin-info but with an enterprise license checkout + +#include + +#include +#include + +#include "binaryninjacore.h" +#include "binaryninjaapi.h" +#include "enterprise.h" + +using namespace BinaryNinja; +using namespace std; + +bool is_file(char* fname) +{ + struct stat buf; + if (stat(fname, &buf) == 0 && (buf.st_mode & S_IFREG) == S_IFREG) + return true; + + return false; +} + +int main(int argc, char* argv[]) +{ + if (argc != 2) + { + cerr << "USAGE: " << argv[0] << " " << endl; + exit(-1); + } + + char* fname = argv[1]; + if (!is_file(fname)) + { + cerr << "Error: " << fname << " is not a regular file" << endl; + exit(-1); + } + + EnterpriseServer::LicenseCheckout _{}; + + /* In order to initiate the bundled plugins properly, the location + * of where bundled plugins directory is must be set. */ + SetBundledPluginDirectory(GetBundledPluginDirectory()); + InitPlugins(); + + Ref bd = new BinaryData(new FileMetadata(), argv[1]); + Ref bv; + for (auto type : BinaryViewType::GetViewTypes()) + { + if (type->IsTypeValidForData(bd) && type->GetName() != "Raw") + { + bv = type->Create(bd); + break; + } + } + + if (!bv || bv->GetTypeName() == "Raw") + { + fprintf(stderr, "Input file does not appear to be an exectuable\n"); + return -1; + } + + bv->UpdateAnalysisAndWait(); + + cout << "Target: " << fname << endl << endl; + cout << "TYPE: " << bv->GetTypeName() << endl; + cout << "START: 0x" << hex << bv->GetStart() << endl; + cout << "ENTRY: 0x" << hex << bv->GetEntryPoint() << endl; + cout << "PLATFORM: " << bv->GetDefaultPlatform()->GetName() << endl; + cout << endl; + + cout << "---------- 10 Functions ----------" << endl; + int x = 0; + for (auto func : bv->GetAnalysisFunctionList()) + { + cout << hex << func->GetStart() << " " << func->GetSymbol()->GetFullName() << endl; + if (++x >= 10) + break; + } + cout << endl; + + cout << "---------- 10 Strings ----------" << endl; + x = 0; + for (auto str_ref : bv->GetStrings()) + { + char* str = (char*)malloc(str_ref.length + 1); + bv->Read(str, str_ref.start, str_ref.length); + str[str_ref.length] = 0; + + cout << hex << str_ref.start << " (" << dec << str_ref.length << ") " << str << endl; + free(str); + + if (++x >= 10) + break; + } + + // Shutting down is required to allow for clean exit of the core + BNShutdown(); + + return 0; +} -- cgit v1.3.1