summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorRusty Wagner <rusty@vector35.com>2017-06-23 18:36:37 -0400
committerRusty Wagner <rusty@vector35.com>2017-06-23 18:36:37 -0400
commitc51f3bed6bdef577253feee0e85a71fda1931bd7 (patch)
tree1cf26ff28e0b9bfb6bfbed044ba7ca520d63f862 /examples
parent4b988c0d24c8d8c8dc67485f3aaeb7106eb4af18 (diff)
parentcca0fe6ea60eb7f6b35cc433a6fff96cc65b3ff8 (diff)
Merge branch 'dev'
Diffstat (limited to 'examples')
-rw-r--r--examples/bin-info/Makefile54
-rw-r--r--examples/bin-info/Makefile.win9
-rw-r--r--examples/bin-info/src/bin-info.cpp118
-rw-r--r--examples/breakpoint/CMakeLists.txt45
-rw-r--r--examples/breakpoint/Makefile57
-rw-r--r--examples/breakpoint/Makefile.win16
-rw-r--r--examples/breakpoint/src/breakpoint.cpp36
-rw-r--r--examples/print_syscalls/Makefile54
-rw-r--r--examples/print_syscalls/Makefile.win9
-rw-r--r--examples/print_syscalls/src/arm-syscall.cpp115
10 files changed, 513 insertions, 0 deletions
diff --git a/examples/bin-info/Makefile b/examples/bin-info/Makefile
new file mode 100644
index 00000000..c8b2efc2
--- /dev/null
+++ b/examples/bin-info/Makefile
@@ -0,0 +1,54 @@
+# Path to prebuilt libbinaryninjaapi.a
+BINJA_API_A := ../../bin/libbinaryninjaapi.a
+
+# Path to binaryninjaapi.h and json
+INC := -I../../
+
+UNAME_S := $(shell uname -s)
+ifeq ($(UNAME_S),Linux)
+ # Path to binaryninja install
+ BINJAPATH := $(HOME)/binaryninja/
+ CC := g++
+else
+ BINJAPATH := /Applications/Binary\ Ninja.app/Contents/MacOS
+ CC := $(shell xcrun -f clang++)
+endif
+
+SRCDIR := src
+BUILDDIR := build
+TARGETDIR := bin
+
+TARGETNAME := bininfo
+TARGET := $(TARGETDIR)/$(TARGETNAME)
+
+SRCEXT := cpp
+SOURCES := $(shell find $(SRCDIR) -type f -name *.$(SRCEXT))
+OBJECTS := $(patsubst $(SRCDIR)/%,$(BUILDDIR)/%,$(SOURCES:.$(SRCEXT)=.o))
+
+LIBS := -L $(BINJAPATH) -lbinaryninjacore
+CFLAGS := -c -std=gnu++11 -O2 -Wall -W -fPIC -pipe
+ifeq ($(UNAME_S),Darwin)
+ CFLAGS += -arch x86_64 -pipe -stdlib=libc++
+endif
+
+all: $(TARGET)
+
+ifeq ($(UNAME_S),Linux)
+$(TARGET): $(OBJECTS)
+ @mkdir -p $(TARGETDIR)
+ $(CC) $^ $(BINJA_API_A) $(LIBS) -Wl,-rpath=$(BINJAPATH) -ldl -o $@
+else
+$(TARGET): $(OBJECTS)
+ @mkdir -p $(TARGETDIR)
+ $(CC) $^ $(BINJA_API_A) $(LIBS) -o $@
+ install_name_tool -change @rpath/libbinaryninjacore.dylib $(BINJAPATH)/libbinaryninjacore.dylib $@
+endif
+
+$(BUILDDIR)/%.o: $(SRCDIR)/%.$(SRCEXT)
+ @mkdir -p $(BUILDDIR)
+ $(CC) $(CFLAGS) $(INC) -c -o $@ $<
+
+clean:
+ $(RM) -r $(BUILDDIR) $(TARGETDIR)
+
+.PHONY: clean
diff --git a/examples/bin-info/Makefile.win b/examples/bin-info/Makefile.win
new file mode 100644
index 00000000..b65bfb86
--- /dev/null
+++ b/examples/bin-info/Makefile.win
@@ -0,0 +1,9 @@
+BINJA_API_INC_PATH = ..\..\
+BINJA_API_LIB = ..\..\bin\libbinaryninjaapi.lib
+BINJA_CORE_LIB = "c:\Program Files\Vector35\BinaryNinja\binaryninjacore.lib"
+
+FLAGS = /DWIN32 /D__WIN32__ /EHsc /I$(BINJA_API_INC_PATH) /link $(BINJA_API_LIB) $(BINJA_CORE_LIB)
+
+bininfo: ./src/bin-info.cpp
+ if not exist bin mkdir bin
+ cl ./src/bin-info.cpp $(FLAGS) /Fe:.\bin\bininfo
diff --git a/examples/bin-info/src/bin-info.cpp b/examples/bin-info/src/bin-info.cpp
new file mode 100644
index 00000000..2a9a5e98
--- /dev/null
+++ b/examples/bin-info/src/bin-info.cpp
@@ -0,0 +1,118 @@
+/*
+ * Command line executable file that outputs
+ * some information about the exectuable passed to.
+ */
+
+#include <sys/stat.h>
+
+#include <iostream>
+#include <cstdlib>
+
+#include "binaryninjacore.h"
+#include "binaryninjaapi.h"
+
+using namespace BinaryNinja;
+using namespace std;
+
+#ifndef __WIN32__
+#include <libgen.h>
+#include <dlfcn.h>
+string get_plugins_directory()
+{
+ Dl_info info;
+ if (!dladdr((void *)BNGetBundledPluginDirectory, &info))
+ return NULL;
+
+ stringstream ss;
+ ss << dirname((char *)info.dli_fname) << "/plugins/";
+ return ss.str();
+}
+#else
+string get_plugins_directory()
+{
+ return "C:\\Program Files\\Vector35\\Binary Ninja\\plugins\\";
+}
+#endif
+
+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] << " <file_name>" << endl;
+ exit(-1);
+ }
+
+ char *fname = argv[1];
+ if (!is_file(fname)) {
+ cerr << "Error: " << fname << " is not a regular file" << endl;
+ exit(-1);
+ }
+
+ /* In order to initiate the bundled plugins properly, the location
+ * of where bundled plugins directory is must be set. Since
+ * libbinaryninjacore is in the path get the path to it and use it to
+ * determine the plugins directory */
+ SetBundledPluginDirectory(get_plugins_directory());
+ InitCorePlugins();
+ InitUserPlugins();
+
+ auto bd = BinaryData(new FileMetadata(), fname);
+ BinaryView *bv;
+
+ for (auto type : BinaryViewType::GetViewTypes()) {
+ if (type->IsTypeValidForData(&bd) && type->GetName() != "Raw") {
+ bv = type->Create(&bd);
+ break;
+ }
+ }
+
+ if (bv->GetTypeName() == "Raw"){
+ cerr << "Error: Unable to get any other view type besides Raw";
+ exit(-1);
+ }
+
+ bv->UpdateAnalysis();
+ while (bv->GetAnalysisProgress().state != IdleState);
+
+ 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;
+ }
+
+ return 0;
+}
diff --git a/examples/breakpoint/CMakeLists.txt b/examples/breakpoint/CMakeLists.txt
new file mode 100644
index 00000000..916778c8
--- /dev/null
+++ b/examples/breakpoint/CMakeLists.txt
@@ -0,0 +1,45 @@
+cmake_minimum_required(VERSION 3.1.0 FATAL_ERROR)
+
+project(breakpoint CXX)
+
+add_library(${PROJECT_NAME} SHARED
+ src/breakpoint.cpp)
+
+include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../..)
+
+if(WIN32)
+ set(BINJA_DIR "C:\\Program Files\\Vector35\\Binary Ninja"
+ CACHE PATH "Binary Ninja installation directory")
+ set(BINJA_BIN_DIR "${BINJA_DIR}")
+ set(BINJA_PLUGINS_DIR "$ENV{APPDATA}/Binary Ninja/plugins"
+ CACHE PATH "Binary Ninja user plugins directory")
+elseif(APPLE)
+ set(BINJA_DIR "/Applications/Binary Ninja.app"
+ CACHE PATH "Binary Ninja installation directory")
+ set(BINJA_BIN_DIR "${BINJA_DIR}/Contents/MacOS")
+ set(BINJA_PLUGINS_DIR "$ENV{HOME}/Library/Application Support/Binary Ninja/plugins"
+ CACHE PATH "Binary Ninja user plugins directory")
+else()
+ set(BINJA_DIR "$ENV{HOME}/binaryninja"
+ CACHE PATH "Binary Ninja installation directory")
+ set(BINJA_BIN_DIR "${BINJA_DIR}")
+ set(BINJA_PLUGINS_DIR "$ENV{HOME}/.binaryninja/plugins"
+ CACHE PATH "Binary Ninja user plugins directory")
+endif()
+
+find_library(BINJA_API_LIBRARY binaryninjaapi
+ HINTS ${CMAKE_CURRENT_SOURCE_DIR}/../../bin)
+find_library(BINJA_CORE_LIBRARY binaryninjacore
+ HINTS ${BINJA_BIN_DIR})
+
+target_link_libraries(${PROJECT_NAME}
+ ${BINJA_API_LIBRARY}
+ ${BINJA_CORE_LIBRARY}
+ )
+
+set_target_properties(${PROJECT_NAME} PROPERTIES
+ CXX_STANDARD 11
+ LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../../bin
+ )
+
+install(TARGETS ${PROJECT_NAME} DESTINATION ${BINJA_PLUGINS_DIR})
diff --git a/examples/breakpoint/Makefile b/examples/breakpoint/Makefile
new file mode 100644
index 00000000..d85b63e5
--- /dev/null
+++ b/examples/breakpoint/Makefile
@@ -0,0 +1,57 @@
+# Path to prebuilt libbinaryninjaapi.a
+BINJA_API_A := ../../bin/libbinaryninjaapi.a
+
+# Path to binaryninjaapi.h and json
+INC := -I../../
+
+UNAME_S := $(shell uname -s)
+ifeq ($(UNAME_S),Linux)
+ # Path to binaryninja install
+ BINJAPATH := $(HOME)/binaryninja
+else
+ BINJAPATH := /Applications/Binary\ Ninja.app/Contents/MacOS
+endif
+
+SRCDIR := src
+BUILDDIR := build
+TARGETDIR := bin
+
+TARGETNAME := breakpoint
+TARGET := $(TARGETDIR)/$(TARGETNAME)
+INSTALLBINDIR := $(BINJAPATH)/plugins/
+
+SRCEXT := cpp
+SOURCES := $(shell find $(SRCDIR) -type f -name *.$(SRCEXT))
+OBJECTS := $(patsubst $(SRCDIR)/%,$(BUILDDIR)/%,$(SOURCES:.$(SRCEXT)=.o))
+
+LIBS := -L $(BINJAPATH) -lbinaryninjacore
+CFLAGS := -c -std=gnu++11 -O2 -Wall -W -fPIC -pipe
+ifeq ($(UNAME_S),Linux)
+ CC := g++
+all: $(TARGET).so
+else
+ CC := $(shell xcrun -f clang++)
+ CFLAGS += -arch x86_64 -pipe -stdlib=libc++ -mmacosx-version-min=10.7
+all: $(TARGET).dylib
+endif
+
+$(TARGET).so: $(OBJECTS)
+ @mkdir -p $(TARGETDIR)
+ $(CC) -shared $^ $(BINJA_API_A) $(LIBS) -o $@
+
+$(TARGET).dylib: $(OBJECTS)
+ @mkdir -p $(TARGETDIR)
+ $(CC) -dynamiclib $^ $(BINJA_API_A) $(LIBS) -o $@
+ install_name_tool -id @loader_path/$(TARGETNAME).dylib -add_rpath @loader_path/.. $@
+
+$(BUILDDIR)/%.o: $(SRCDIR)/%.$(SRCEXT)
+ @mkdir -p $(BUILDDIR)
+ $(CC) $(CFLAGS) $(INC) -c -o $@ $<
+
+clean:
+ $(RM) -r $(BUILDDIR) $(TARGETDIR)
+
+install:
+ cp $(TARGET) $(INSTALLBINDIR)
+
+.PHONY: clean
diff --git a/examples/breakpoint/Makefile.win b/examples/breakpoint/Makefile.win
new file mode 100644
index 00000000..00cbbfa1
--- /dev/null
+++ b/examples/breakpoint/Makefile.win
@@ -0,0 +1,16 @@
+BINJA_API_INC_PATH = ..\..\
+BINJA_API_LIB = ..\..\bin\libbinaryninjaapi.lib
+BINJA_CORE_LIB = "c:\Program Files\Vector35\BinaryNinja\binaryninjacore.lib"
+
+FLAGS = /DWIN32 /D__WIN32__ /EHsc /I$(BINJA_API_INC_PATH) /link $(BINJA_API_LIB) $(BINJA_CORE_LIB)
+
+bininfo.dll: ./src/breakpoint.cpp
+ if not exist bin mkdir bin
+ cl ./src/breakpoint.cpp /LD $(FLAGS) /OUT:.\bin\breakpoint.dll
+
+clean:
+ if exist *.obj del *.obj
+ if exist *.exp del *.exp
+ if exist *.lib del *.lib
+ if exist *.dll del *.dll
+ if exist .\bin del /S /Q bin
diff --git a/examples/breakpoint/src/breakpoint.cpp b/examples/breakpoint/src/breakpoint.cpp
new file mode 100644
index 00000000..4fac98ac
--- /dev/null
+++ b/examples/breakpoint/src/breakpoint.cpp
@@ -0,0 +1,36 @@
+#include <inttypes.h>
+#include "binaryninjaapi.h"
+
+using namespace BinaryNinja;
+using namespace std;
+
+void write_breakpoint(BinaryNinja::BinaryView *view, uint64_t start, uint64_t length)
+{
+ // Sample function to show registering a plugin menu item for a range of bytes.
+ // Also possible:
+ // register
+ // register_for_address
+ // register_for_function
+
+ Ref<Architecture> arch = view->GetDefaultArchitecture();
+ string arch_name = arch->GetName();
+
+ if (arch_name.compare(0, 3, "x86") == 0) {
+ string int3s = string(length, '\xcc');
+ view->Write(start, int3s.c_str(), length);
+ } else {
+ LogError("No support for breakpoint on %s", arch_name.c_str());
+ }
+}
+
+extern "C"
+{
+ BINARYNINJAPLUGIN bool CorePluginInit()
+ {
+ // Register the plugin with Binary Ninja
+ PluginCommand::RegisterForRange("Convert to breakpoint",
+ "Fill region with breakpoint instructions.",
+ &write_breakpoint);
+ return true;
+ }
+} \ No newline at end of file
diff --git a/examples/print_syscalls/Makefile b/examples/print_syscalls/Makefile
new file mode 100644
index 00000000..54fd1237
--- /dev/null
+++ b/examples/print_syscalls/Makefile
@@ -0,0 +1,54 @@
+# Path to prebuilt libbinaryninjaapi.a
+BINJA_API_A := ../../bin/libbinaryninjaapi.a
+
+# Path to binaryninjaapi.h and json
+INC := -I../../
+
+UNAME_S := $(shell uname -s)
+ifeq ($(UNAME_S),Linux)
+ # Path to binaryninja install
+ BINJAPATH := $(HOME)/binaryninja/
+ CC := g++
+else
+ BINJAPATH := /Applications/Binary\ Ninja.app/Contents/MacOS
+ CC := $(shell xcrun -f clang++)
+endif
+
+SRCDIR := src
+BUILDDIR := build
+TARGETDIR := bin
+
+TARGETNAME := print_syscalls
+TARGET := $(TARGETDIR)/$(TARGETNAME)
+
+SRCEXT := cpp
+SOURCES := $(shell find $(SRCDIR) -type f -name *.$(SRCEXT))
+OBJECTS := $(patsubst $(SRCDIR)/%,$(BUILDDIR)/%,$(SOURCES:.$(SRCEXT)=.o))
+
+LIBS := -L $(BINJAPATH) -lbinaryninjacore
+CFLAGS := -c -std=gnu++11 -O2 -Wall -W -fPIC -pipe -ggdb
+ifeq ($(UNAME_S),Darwin)
+ CFLAGS += -arch x86_64 -pipe -stdlib=libc++
+endif
+
+all: $(TARGET)
+
+ifeq ($(UNAME_S),Linux)
+$(TARGET): $(OBJECTS)
+ @mkdir -p $(TARGETDIR)
+ $(CC) $^ $(BINJA_API_A) $(LIBS) -Wl,-rpath=$(BINJAPATH) -ldl -o $@
+else
+$(TARGET): $(OBJECTS)
+ @mkdir -p $(TARGETDIR)
+ $(CC) $^ $(BINJA_API_A) $(LIBS) -o $@
+ install_name_tool -change @rpath/libbinaryninjacore.dylib $(BINJAPATH)/libbinaryninjacore.dylib $@
+endif
+
+$(BUILDDIR)/%.o: $(SRCDIR)/%.$(SRCEXT)
+ @mkdir -p $(BUILDDIR)
+ $(CC) $(CFLAGS) $(INC) -c -o $@ $<
+
+clean:
+ $(RM) -r $(BUILDDIR) $(TARGETDIR)
+
+.PHONY: clean
diff --git a/examples/print_syscalls/Makefile.win b/examples/print_syscalls/Makefile.win
new file mode 100644
index 00000000..afc0cbd8
--- /dev/null
+++ b/examples/print_syscalls/Makefile.win
@@ -0,0 +1,9 @@
+BINJA_API_INC_PATH = ..\..\
+BINJA_API_LIB = ..\..\bin\libbinaryninjaapi.lib
+BINJA_CORE_LIB = "c:\Program Files\Vector35\BinaryNinja\binaryninjacore.lib"
+
+FLAGS = /DWIN32 /D__WIN32__ /EHsc /I$(BINJA_API_INC_PATH) /link $(BINJA_API_LIB) $(BINJA_CORE_LIB)
+
+print_syscalls: ./src/arm-syscall.cpp
+ if not exist bin mkdir bin
+ cl ./src/arm-syscall.cpp $(FLAGS) /Fe:.\bin\print_syscalls
diff --git a/examples/print_syscalls/src/arm-syscall.cpp b/examples/print_syscalls/src/arm-syscall.cpp
new file mode 100644
index 00000000..3424619b
--- /dev/null
+++ b/examples/print_syscalls/src/arm-syscall.cpp
@@ -0,0 +1,115 @@
+/*
+ * Outputs the syscall numbers called by a binary.
+ */
+
+#include <sys/stat.h>
+
+#include <iostream>
+#include <cstdlib>
+
+#include "binaryninjacore.h"
+#include "binaryninjaapi.h"
+
+using namespace BinaryNinja;
+using namespace std;
+
+#ifndef __WIN32__
+#include <libgen.h>
+#include <dlfcn.h>
+string get_plugins_directory()
+{
+ Dl_info info;
+ if (!dladdr((void *)BNGetBundledPluginDirectory, &info))
+ return NULL;
+
+ stringstream ss;
+ ss << dirname((char *)info.dli_fname) << "/plugins/";
+ return ss.str();
+}
+#else
+string get_plugins_directory()
+{
+ return "C:\\Program Files\\Vector35\\Binary Ninja\\plugins\\";
+}
+#endif
+
+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] << " <file_name>" << endl;
+ exit(-1);
+ }
+
+ char *fname = argv[1];
+ if (!is_file(fname)) {
+ cerr << "Error: " << fname << " is not a regular file" << endl;
+ exit(-1);
+ }
+
+ /* In order to initiate the bundled plugins properly, the location
+ * of where bundled plugins directory is must be set. Since
+ * libbinaryninjacore is in the path get the path to it and use it to
+ * determine the plugins directory */
+ SetBundledPluginDirectory(get_plugins_directory());
+ InitCorePlugins();
+ InitUserPlugins();
+
+ auto bd = BinaryData(new FileMetadata(), fname);
+ BinaryView *bv = 0;
+
+ for (auto type : BinaryViewType::GetViewTypesForData(&bd)) {
+ if (type->GetName() != "Raw") {
+ bv = type->Create(&bd);
+ break;
+ }
+ }
+
+ if (!bv || bv->GetTypeName() == "Raw"){
+ cerr << "Error: Unable to get any other view type besides Raw";
+ exit(-1);
+ }
+
+ bv->UpdateAnalysis();
+ while (bv->GetAnalysisProgress().state != IdleState);
+
+ auto arch = bv->GetDefaultArchitecture();
+ auto platform = bv->GetDefaultPlatform();
+
+ auto cc = platform->GetSystemCallConvention();
+ if (!cc) {
+ cerr << "Error: No system call conventions found for "
+ << platform->GetName() << endl;
+ exit(-1);
+ }
+
+ auto reg = cc->GetIntegerArgumentRegisters()[0];
+
+ for (Function *func : bv->GetAnalysisFunctionList()) {
+ auto il_func = func->GetLowLevelIL();
+
+ for (size_t i = 0; i < il_func->GetInstructionCount(); i++) {
+ auto instr = (*il_func)[il_func->GetIndexForInstruction(i)];
+
+ if (instr.operation == LLIL_SYSCALL) {
+ auto reg_value = func->GetRegisterValueAtLowLevelILInstruction(i, reg);
+
+ cout << "System call address: 0x"
+ << hex << instr.address
+ << " - "
+ << dec << reg_value.value
+ << endl;
+ }
+ }
+ }
+
+ return 0;
+}