Skip to content

Instantly share code, notes, and snippets.

@d0now
Forked from psifertex/binexport_binja.zsh
Last active September 12, 2023 06:41
Show Gist options
  • Save d0now/632db27727ff58b316086cb28f017b00 to your computer and use it in GitHub Desktop.
Save d0now/632db27727ff58b316086cb28f017b00 to your computer and use it in GitHub Desktop.
BinExport build script for Binary Ninja w/ convenient params
#!/usr/bin/env zsh
# Note:
# CMake, Clang, clang-format, Ninja, and sed are required to build
PRINT_CONFIG_AND_EXIT=0
FORCE_CREATE_LINK=0
while getopts b:d:cfl opt
do
case $opt in
b) BINARY_HASH_FORCE="$OPTARG" ;;
d) DOWNLOADS="$(realpath $OPTARG)" ;;
c) PRINT_CONFIG_AND_EXIT=1 ;;
f) FORCE_CREATE_LINK=1 ;;
l) set -x ;;
*) exit 1 ;;
esac
done
if [ -z "$DOWNLOADS" ] || [ ! -d "$DOWNLOADS" ]
then
if [ -d ~/Downloads ]
then
DOWNLOADS=~/Downloads
else
mkdir ~/downloads
DOWNLOADS=~/downloads
fi
fi
BE_PATH=$DOWNLOADS/binexport
BN_API_PATH=$DOWNLOADS/binaryninja-api
if [ -d ~/.binaryninja ] && [ -f ~/.binaryninja/lastrun ]
then
BN_USER=~/.binaryninja
BN_PATH=`cat ~/.binaryninja/lastrun`
BINARY_HASH=$(sed 's/^.*\///' < $BN_PATH/api_REVISION.txt 2>/dev/null)
CORES=$(nproc --all)
elif [ -d ~/Library/Application\ Support/Binary\ Ninja ] && [ -f ~/Library/Application\ Support/Binary\ Ninja/lastrun ]
then
BN_USER=~/Library/Application\ Support/Binary\ Ninja
BN_PATH=`cat ~/Library/Application\ Support/Binary\ Ninja/lastrun`
BINARY_HASH=$(sed 's/^.*\///' < $BN_PATH/../Resources/api_REVISION.txt 2>/dev/null)
CORES=$(sysctl -n hw.logicalcpu)
else
echo "Failed to find appropriate Binary Ninja user directory."
exit 1
fi
if [ ! -z "$BINARY_HASH_FORCE" ]
then
BINARY_HASH=$BINARY_HASH_FORCE
fi
echo "Configuration:"
echo " DOWNLOADS: $DOWNLOADS"
echo " BE_PATH: $BE_PATH"
echo " BN_API_PATH: $BN_API_PATH"
echo " BN_PATH: $BN_PATH"
echo " BINARY_HASH: $BINARY_HASH"
if [ $PRINT_CONFIG_AND_EXIT -eq 1 ]
then
exit 0
fi
if [ -z "$BINARY_HASH" ]
then
echo "Failed to find appropriate hash for Binary Ninja"
exit 1
fi
echo "\u001b[36m[+] Cloning BinExport & Binary Ninja API..."
echo "\u001b[0m"
if [ -d $BE_PATH ]
then
pushd $BE_PATH
if git fetch --all
then
git reset --hard origin/main # Because previous runs of this script will dirty the repo
echo "BinExport exists, repo updated"
else
echo Not a repo, remove $BE_PATH to continue
exit
fi
popd
else
git clone https://github.com/google/binexport.git $BE_PATH
fi
if [ -d $BN_API_PATH ]
then
pushd $BN_API_PATH
if git fetch --all
then
if git checkout "$BINARY_HASH"
then
git pull
echo "Binary Ninja API exists, repo updated"
else
echo Not a repo or could not match binary hash
exit
fi
fi
popd
else
git clone --recursive --branch dev https://github.com/Vector35/binaryninja-api.git $BN_API_PATH
fi
echo "\u001b[36m[+] Updating the git hash..."
echo "\u001b[0m"
sed -i'' -e "s/1bd42a73e612f50c68d802acda674c21a30e980c/$BINARY_HASH/g" $BE_PATH/cmake/BinExportDeps.cmake
sed -i'' -e "s/2023-05-18/modified/g" $BE_PATH/cmake/BinExportDeps.cmake
echo "\u001b[36m[+] Running regenerate-api-stubs..."
echo "\u001b[0m"
pushd $BE_PATH/third_party/binaryninja_api/
./regenerate-api-stubs.sh $BN_API_PATH
popd
pushd $BE_PATH
echo "\u001b[36m[+] Building BinExport..."
echo "\u001b[0m"
rm -rf build && mkdir build && cd build
cmake .. -G Ninja -DBINEXPORT_BINARYNINJA_CHANNEL=DEV -DCMAKE_BUILD_TYPE=Release "-DCMAKE_INSTALL_PREFIX=$PWD" -DBINEXPORT_ENABLE_IDAPRO=OFF -DBINEXPORT_ENABLE_BINARYNINJA=ON
cmake --build . --config Release -- "-j$CORES"
popd
case "$OSTYPE" in
linux*)
if [ -f ~/.binaryninja/plugins/binexport12_binaryninja.so ]
then
if [ $FORCE_CREATE_LINK -eq 0 ]
then
echo "\u001b[36m[+] Unable to link .so, file already exists"
echo "\u001b[0m"
exit
else
rm ~/.binaryninja/plugin/binexport12_binaryninja.so
fi
fi
echo "\u001b[36m[+] Linking BinExport plugin to Binary Ninja plugin folder"
echo "\u001b[0m"
ln -sf $DOWNLOADS/binexport/build/binaryninja/binexport12_binaryninja.so ~/.binaryninja/plugins
;;
darwin*)
if [ -f "~/Library/Application Support/Binary Ninja/plugins/binexport12_binaryninja.dylib" ]
then
if [ $FORCE_CREATE_LINK -eq 0 ]
then
echo "\u001b[36m[+] Unable to link .dylib, file already exists"
echo "\u001b[0m"
exit
else
rm "~/Library/Application Support/Binary Ninja/plugins/binexport12_binaryninja.dylib"
fi
echo "\u001b[36m[+] Linking BinExport plugin to Binary Ninja plugin folder"
echo "\u001b[0m"
ln -sf $DOWNLOADS/binexport/build/binaryninja/binexport12_binaryninja.dylib "~/Library/Application Support/Binary Ninja/plugins/"
;;
*)
;;
esac
echo "\u001b[32;1m[+] Done!"
echo "\u001b[0m"
@d0now
Copy link
Author

d0now commented Sep 12, 2023

example:

./binexport_binja.sh -b 6a6d93b -d .

parameters

  • -b: force change binary ninja hash
  • -d: force change binexport & binaryninja-api download directory
  • -c: print config and exit
  • -f: force link build result to binary ninja's plugin directory
  • -l: verbose

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment