summaryrefslogtreecommitdiff
path: root/examples/bin-info
diff options
context:
space:
mode:
authorbambu <bambu@revengsec.com>2016-07-02 19:42:02 -0400
committerbambu <bambu@revengsec.com>2016-07-02 19:42:02 -0400
commit3e5ce8d9f21378cd6453b6d5106a1acce675ffe1 (patch)
tree62a3d7a369a3e931341e156216fe5d5127fa7302 /examples/bin-info
parent46fe170c40dd0de44b83ebbce673462b5de8760c (diff)
Added another exmample. This one can be compiled and ran from terminal.
Diffstat (limited to 'examples/bin-info')
-rw-r--r--examples/bin-info/Makefile54
-rw-r--r--examples/bin-info/src/bin-info.cpp118
2 files changed, 172 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/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;
+}