From 9129a6536ddf99cd11dcc6738010770492f542d8 Mon Sep 17 00:00:00 2001 From: Jordan Wiens Date: Fri, 19 May 2017 17:35:31 -0400 Subject: add install_api script based on @withzombies script --- scripts/install_api.py | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100755 scripts/install_api.py (limited to 'scripts') diff --git a/scripts/install_api.py b/scripts/install_api.py new file mode 100755 index 00000000..cba53e71 --- /dev/null +++ b/scripts/install_api.py @@ -0,0 +1,74 @@ +#!/usr/bin/env python +# +# Thanks to @withzombies for letting us adapt his script +# + +import sys +from os import path +from os import stat +from site import getsitepackages + +try: + import binaryninja + print "Binary Ninja API Installed" + sys.exit(0) +except ImportError: + pass + +if sys.platform == "linux": + userpath = path.expanduser("~/.binaryninja") + lastrun = path.join(userpath, "lastrun") + if path.isfile(lastrun): + lastrunpath = open(lastrun).read().strip() + api_path = path.join(path.dirname(lastrun), "python") + else: + print "Running on linux, but ~/.binaryninja/lastrun does not exist" + sys.exit(0) +elif sys.platform == "darwin": + api_path = "/Applications/Binary Ninja.app/Contents/Resources/python" +else: + # Windows + api_path = "r'C:\Program Files\Vector35\BinaryNinja\python'" + + +def validate_path(path): + try: + stat(path) + except OSError: + return False + + old_path = sys.path + sys.path.append(path) + + try: + from binaryninja import core_version + except ImportError: + sys.path = old_path + return False + + return True + + +while not validate_path(api_path): + print "Binary Ninja not found. Please provide the path to Binary " + \ + "Ninja's install directory" + sys.stdout.write("[{}] ".format(api_path)) + + new_path = sys.stdin.readline().strip() + if len(new_path) == 0: + print "Invalid path" + continue + + if not new_path.endswith('python'): + new_path = path.join(new_path, 'python') + + api_path = new_path + +from binaryninja import core_version +print "Found Binary Ninja core version: {}".format(core_version) + +install_path = getsitepackages()[0] +binaryninja_pth_path = path.join(install_path, 'binaryninja.pth') +open(binaryninja_pth_path, 'wb').write(api_path) + +print "Binary Ninja API installed using {}".format(binaryninja_pth_path) -- cgit v1.3.1 From 0d0564bbfb0e7cd6be095ec28469b7facb107bdf Mon Sep 17 00:00:00 2001 From: Jordan Wiens Date: Tue, 23 May 2017 19:58:43 -0400 Subject: update install_api to use user directories by default --- scripts/install_api.py | 55 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 33 insertions(+), 22 deletions(-) (limited to 'scripts') diff --git a/scripts/install_api.py b/scripts/install_api.py index cba53e71..53679ba5 100755 --- a/scripts/install_api.py +++ b/scripts/install_api.py @@ -4,25 +4,25 @@ # import sys -from os import path -from os import stat -from site import getsitepackages +import os +from site import getsitepackages, getusersitepackages, check_enableusersite try: import binaryninja - print "Binary Ninja API Installed" - sys.exit(0) + print("Binary Ninja API already Installed") + sys.exit(1) except ImportError: pass -if sys.platform == "linux": - userpath = path.expanduser("~/.binaryninja") - lastrun = path.join(userpath, "lastrun") - if path.isfile(lastrun): +if sys.platform.startswith("linux"): + userpath = os.path.expanduser("~/.binaryninja") + lastrun = os.path.join(userpath, "lastrun") + if os.path.isfile(lastrun): lastrunpath = open(lastrun).read().strip() - api_path = path.join(path.dirname(lastrun), "python") + api_path = os.path.join(lastrunpath, "python") + print("Found install folder of {}".format(api_path)) else: - print "Running on linux, but ~/.binaryninja/lastrun does not exist" + print("Running on linux, but ~/.binaryninja/lastrun does not exist") sys.exit(0) elif sys.platform == "darwin": api_path = "/Applications/Binary Ninja.app/Contents/Resources/python" @@ -33,7 +33,7 @@ else: def validate_path(path): try: - stat(path) + os.stat(path) except OSError: return False @@ -42,6 +42,7 @@ def validate_path(path): try: from binaryninja import core_version + print("Found Binary Ninja core version: {}".format(core_version)) except ImportError: sys.path = old_path return False @@ -50,25 +51,35 @@ def validate_path(path): while not validate_path(api_path): - print "Binary Ninja not found. Please provide the path to Binary " + \ - "Ninja's install directory" - sys.stdout.write("[{}] ".format(api_path)) + print("\nBinary Ninja not found. Please provide the path to Binary " + \ + "Ninja's install directory: \n [{}] : ".format(api_path)) new_path = sys.stdin.readline().strip() if len(new_path) == 0: - print "Invalid path" + print("\nInvalid path") continue if not new_path.endswith('python'): - new_path = path.join(new_path, 'python') + new_path = os.path.join(new_path, 'python') api_path = new_path -from binaryninja import core_version -print "Found Binary Ninja core version: {}".format(core_version) +if ( len(sys.argv) > 1 and sys.argv[1].lower() == "root" ): + #write to root site + install_path = getsitepackages()[0] + if not os.access(install_path, os.W_OK): + print("Root install specified but cannot write to {}".format(install_path)) + sys.exit(1) +else: + if check_enableusersite(): + install_path = getusersitepackages() + if not os.path.exists(install_path): + os.makedirs(install_path) + else: + print("Warning, trying to write to user site packages, but check_enableusersite fails.") + sys.exit(1) -install_path = getsitepackages()[0] -binaryninja_pth_path = path.join(install_path, 'binaryninja.pth') +binaryninja_pth_path = os.path.join(install_path, 'binaryninja.pth') open(binaryninja_pth_path, 'wb').write(api_path) -print "Binary Ninja API installed using {}".format(binaryninja_pth_path) +print("Binary Ninja API installed using {}".format(binaryninja_pth_path)) -- cgit v1.3.1 From 7be5f0c6d6f1947e7836d097a7ce8831ba45e7f2 Mon Sep 17 00:00:00 2001 From: Jordan Wiens Date: Tue, 23 May 2017 19:59:20 -0400 Subject: make linux setup script features optional and include api setup as well --- scripts/linux-setup.sh | 205 +++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 166 insertions(+), 39 deletions(-) (limited to 'scripts') diff --git a/scripts/linux-setup.sh b/scripts/linux-setup.sh index 2f5bd489..e56d74dc 100755 --- a/scripts/linux-setup.sh +++ b/scripts/linux-setup.sh @@ -1,73 +1,123 @@ #!/bin/bash -# Note is setup script currently does three things: +# Note is setup script currently does four things: # # 1. It creates a binaryninja.desktop file in ~/.local/share/applications and # copies it to the desktop # 2. It creates a .xml file to add a mime type for .bndb files. # 3. It adds a binaryninja: url handler. +# 4. Creates .pth python file to add binary ninja to your python path -APP="binaryninja" -FILECOMMENT="Binary Ninja Analysis Database" -APPCOMMENT="Binary Ninja: A Reverse Engineering Platform" -BNPATH=$(dirname $(readlink -f "$0")) -EXEC="${BNPATH}/binaryninja" -PNG="${BNPATH}/docs/images/logo.png" -EXT="bndb" -SHARE=/usr/share #For system -SUDO="sudo" #For system -SHARE=~/.local/share #For user only -SUDO="" #For user only -DESKTOPFILE=$SHARE/applications/${APP}.desktop -MIMEFILE=$SHARE/mime/packages/application-x-$APP.xml -IMAGEPATH=$SHARE/pixmaps +setvars() +{ + APP="binaryninja" + FILECOMMENT="Binary Ninja Analysis Database" + APPCOMMENT="Binary Ninja: A Reverse Engineering Platform" + BNPATH=$(dirname $(readlink -f "$0")) + EXEC="${BNPATH}/binaryninja" + PNG="${BNPATH}/docs/images/logo.png" + EXT="bndb" + if [ "$ROOT" == "root" ] + then + SHARE="/usr/share" #For system + SUDO="sudo " #For system + else + SHARE="~/.local/share" #For user only + SUDO="" #For user only + fi + DESKTOPFILE="${SHARE}/applications/${APP}.desktop" + MIMEFILE="${SHARE}/mime/packages/application-x-${APP}.xml" + IMAGEFILE="${SHARE}/pixmaps/application-x-${APP}.png" +} + +usage() +{ + echo "Usage: $0 -[ulpdmrsh] + -u: For uninstall, removes all associations (does NOT remove ~/.binaryninja) + -l: Disable creation ~/.binaryninja/lastrun file + -p: Disable adding python path .pth file + -d: Disable adding desktop launcher + -m: Disable adding mime associations + -r: Run as root to set system wide preferences (requires sudo permissions) + -s: Run in headless mode (equivalent to -d -m) + -h: Display this help +" 1>&2 + exit 1 +} + +lastrun() +{ + #Contains the last run location, but on systems without a UI this ensures + #the UI doesn't have to run once for the core to be available. + if [ -f ~/.binaryninja/lastrun ] + then + echo lastrun already exists, remove to create a new one + else + echo ${BNPATH} > ~/.binaryninja/lastrun + fi +} + +pythonpath() +{ + echo Configuring python path + ${SUDO}python ${BNPATH}/install_api.py $ROOT +} createdesktopfile() { - mkdir -p $SHARE/{mime/packages,applications,pixmaps} + mkdir -p ${SHARE}/{mime/packages,applications,pixmaps} echo Creating .desktop file # Desktop File - echo "[Desktop Entry] -Name=$APP -Exec=$EXEC %u -MimeType=application/x-$APP;x-scheme-handler/$APP; -Icon=$PNG + read -d '' DESKTOP << EOF +[Desktop Entry] +Name=${APP} +Exec=${EXEC} %u +MimeType=application/x-${APP};x-scheme-handler/${APP}; +Icon=${PNG} Terminal=false Type=Application Categories=Utility; -Comment=$APPCOMMENT -" | $SUDO tee $DESKTOPFILE >/dev/null - $SUDO chmod +x $DESKTOPFILE - - $SUDO update-desktop-database $SHARE/applications +Comment=${APPCOMMENT} +EOF + if [ "${ROOT}" == "root" ] + then + echo ${DESKTOP} | $SUDO tee ${DESKTOPFILE} >/dev/null + $SUDO chmod +x ${DESKTOPFILE} + $SUDO update-desktop-database ${SHARE}/applications + else + echo ${DESKTOP} > ~/Desktop/${APP}.desktop + fi } createmime() { echo Creating MIME settings - if [ ! -f $DESKTOPFILE ] + if [ ! -f ${DESKTOPFILE} -a ! -f ~/Desktop/${APP}.desktop ] then createdesktopfile fi echo " - - $FILECOMMENT - + + ${FILECOMMENT} + - + -"| $SUDO tee $MIMEFILE >/dev/null +"| $SUDO tee ${MIMEFILE} >/dev/null #echo Copying icon - #$SUDO cp $PNG $IMAGEPATH/$APP.png - $SUDO cp $PNG $IMAGEPATH/application-x-$APP.png + #$SUDO cp $PNG $IMAGEFILE + if [ "${ROOT}" == "root" ] + then + $SUDO cp ${PNG} ${IMAGEFILE} + $SUDO update-mime-database ${SHARE}/mime + fi - $SUDO update-mime-database $SHARE/mime } addtodesktop() @@ -75,7 +125,84 @@ addtodesktop() cp $DESKTOPFILE ~/Desktop } -#TODO: Make these optional... -createdesktopfile -createmime -addtodesktop +uninstall() +{ + rm -i -r $DESKTOPFILE $MIMEFILE $IMAGEFILE + if [ "$ROOT" == "root" ] + then + $SUDO update-mime-database ${SHARE}/mime + fi + exit 0 +} + + + +ROOT=user +CREATEDESKTOP=true +CREATEMIME=true +ADDTODESKTOP=true +CREATELASTRUN=true +PYTHONPATH=true +UNINSTALL=false + +while [[ $# -ge 1 ]] +do + flag="$1" + + case $flag in + -u) + UNINSTALL=true + ;; + -l) + CREATELASTRUN=false + ;; + -p) + PYTHONPATH=false + ;; + -d) + ADDTODESKTOP=false + ;; + -m) + CREATEMIME=false + ;; + -r) + ROOT=root + ;; + -s) + ADDTODESKTOP=false + CREATEMIME=false + CREATEDESKTOP=false + ;; + -h|*) + usage + ;; + esac + shift +done + +setvars + +if [ "$UNINSTALL" == "true" ] +then + uninstall +fi +if [ "$CREATEDESKTOP" == "true" ] +then + createdesktopfile +fi +if [ "$CREATEMIME" == "true" ] +then + createmime +fi +if [ "$ADDTODESKTOP" == "true" ] +then + addtodesktop +fi +if [ "$CREATELASTRUN" == "true" ] +then + lastrun +fi +if [ "$PYTHONPATH" == "true" ] +then + pythonpath +fi -- cgit v1.3.1