| 1 | #!/bin/bash |
| 2 | |
| 3 | # Check for the --yes command line argument to skip yes/no prompts |
| 4 | if [ "$1" = "--yes" ] |
| 5 | then |
| 6 | YES=1 |
| 7 | else |
| 8 | YES=0 |
| 9 | fi |
| 10 | |
| 11 | set -eu |
| 12 | set -o nounset |
| 13 | set -x |
| 14 | |
| 15 | if ! which lsb_release > /dev/null |
| 16 | then |
| 17 | lsb_release() { |
| 18 | if [ -f /etc/os-release ] |
| 19 | then |
| 20 | [[ "$1" = "-i" ]] && cat /etc/os-release | grep ^"ID" | cut -d= -f 2 |
| 21 | [[ "$1" = "-r" ]] && cat /etc/os-release | grep "VERSION_ID" | cut -d= -d'"' -f 2 |
| 22 | elif [ -f /etc/lsb-release ] |
| 23 | then |
| 24 | [[ "$1" = "-i" ]] && cat /etc/lsb-release | grep "DISTRIB_ID" | cut -d= -f 2 |
| 25 | [[ "$1" = "-r" ]] && cat /etc/lsb-release | grep "DISTRIB_RELEASE" | cut -d= -f 2 |
| 26 | else |
| 27 | echo Unknown |
| 28 | fi |
| 29 | } |
| 30 | fi |
| 31 | |
| 32 | if [ $YES -eq 0 ] |
| 33 | then |
| 34 | distro="${1:-$(lsb_release -i|cut -f 2)}" |
| 35 | distro_version="${1:-$(lsb_release -r|cut -f 2|cut -c1-2)}" |
| 36 | else |
| 37 | distro="${2:-$(lsb_release -i|cut -f 2)}" |
| 38 | distro_version="${2:-$(lsb_release -r|cut -f 2|cut -c1-2)}" |
| 39 | fi |
| 40 | APTCMD="apt" |
| 41 | APTGETCMD="apt-get" |
| 42 | APT_CANDIDATES="arj build-essential bzip2 cabextract cpio cramfsswap git gzip lhasa liblzma-dev liblzo2-dev locales lzop mtd-utils p7zip p7zip-full python3-distutils python3-setuptools python3-matplotlib python3-capstone python3-pycryptodome python3-gnupg python3-poetry pipx squashfs-tools sleuthkit srecord tar wget zlib1g-dev" |
| 43 | |
| 44 | # Check for root privileges |
| 45 | if [ $UID -eq 0 ] |
| 46 | then |
| 47 | echo "UID is 0, sudo not required" |
| 48 | SUDO="" |
| 49 | else |
| 50 | SUDO="sudo -E" |
| 51 | fi |
| 52 | |
| 53 | install_yaffshiv() |
| 54 | { |
| 55 | git clone --quiet --depth 1 --branch "master" https://github.com/devttys0/yaffshiv |
| 56 | (cd yaffshiv && pipx install .) |
| 57 | rm -rf yaffshiv |
| 58 | } |
| 59 | |
| 60 | install_sasquatch() |
| 61 | { |
| 62 | git clone --quiet --depth 1 --branch "master" https://github.com/devttys0/sasquatch |
| 63 | (cd sasquatch && |
| 64 | wget https://github.com/devttys0/sasquatch/pull/47.patch && |
| 65 | patch -p1 < 47.patch && |
| 66 | ./build.sh) |
| 67 | rm -rf sasquatch |
| 68 | } |
| 69 | |
| 70 | install_cramfstools() |
| 71 | { |
| 72 | # Downloads cramfs tools from sourceforge and installs them to $INSTALL_LOCATION |
| 73 | TIME=`date +%s` |
| 74 | INSTALL_LOCATION=/usr/local/bin |
| 75 | |
| 76 | # https://github.com/torvalds/linux/blob/master/fs/cramfs/README#L106 |
| 77 | git clone --quiet --depth 1 --branch "master" https://github.com/npitre/cramfs-tools |
| 78 | # There is no "make install" |
| 79 | (cd cramfs-tools \ |
| 80 | && make \ |
| 81 | && $SUDO install cramfsck $INSTALL_LOCATION) |
| 82 | |
| 83 | rm -rf cramfs-tools |
| 84 | } |
| 85 | |
| 86 | install_pip_package() |
| 87 | { |
| 88 | PACKAGE="$1" |
| 89 | pipx install $PACKAGE |
| 90 | } |
| 91 | |
| 92 | find_path() |
| 93 | { |
| 94 | FILE_NAME="$1" |
| 95 | |
| 96 | echo -ne "checking for $FILE_NAME..." |
| 97 | which $FILE_NAME > /dev/null |
| 98 | if [ $? -eq 0 ] |
| 99 | then |
| 100 | echo "yes" |
| 101 | return 0 |
| 102 | else |
| 103 | echo "no" |
| 104 | return 1 |
| 105 | fi |
| 106 | } |
| 107 | |
| 108 | # Make sure the user really wants to do this |
| 109 | if [ $YES -eq 0 ] |
| 110 | then |
| 111 | echo "" |
| 112 | echo "WARNING: This script will download and install all required and optional dependencies for binwalk." |
| 113 | echo " This script has only been tested on, and is only intended for, Debian based systems." |
| 114 | echo " Some dependencies are downloaded via unsecure (HTTP) protocols." |
| 115 | echo " This script requires internet access." |
| 116 | echo " This script requires root privileges." |
| 117 | echo "" |
| 118 | if [ "$distro" != Unknown ] |
| 119 | then |
| 120 | echo " $distro $distro_version detected" |
| 121 | else |
| 122 | echo "WARNING: Distro not detected, using package-manager defaults" |
| 123 | fi |
| 124 | echo "" |
| 125 | echo -n "Continue [y/N]? " |
| 126 | read YN |
| 127 | if [ "$(echo "$YN" | grep -i -e 'y' -e 'yes')" == "" ] |
| 128 | then |
| 129 | echo "Quitting..." |
| 130 | exit 1 |
| 131 | fi |
| 132 | elif [ "$distro" != Unknown ] |
| 133 | then |
| 134 | echo "$distro $distro_version detected" |
| 135 | else |
| 136 | echo "WARNING: Distro not detected, using package-manager defaults" |
| 137 | fi |
| 138 | |
| 139 | # Check for supported package managers and set the PKG_* envars appropriately |
| 140 | find_path $APTCMD |
| 141 | if [ $? -eq 1 ] |
| 142 | then |
| 143 | find_path $APTGETCMD |
| 144 | if [ $? -ne 1 ] |
| 145 | then |
| 146 | PKGCMD="$APTGETCMD" |
| 147 | PKGCMD_OPTS="install -y" |
| 148 | PKG_CANDIDATES="$APT_CANDIDATES" |
| 149 | fi |
| 150 | else |
| 151 | if "$APTCMD" install -s -y dpkg > /dev/null |
| 152 | then |
| 153 | PKGCMD="$APTCMD" |
| 154 | PKGCMD_OPTS="install -y" |
| 155 | PKG_CANDIDATES="$APT_CANDIDATES" |
| 156 | else |
| 157 | PKGCMD="$APTGETCMD" |
| 158 | PKGCMD_OPTS="install -y" |
| 159 | PKG_CANDIDATES="$APT_CANDIDATES" |
| 160 | fi |
| 161 | fi |
| 162 | |
| 163 | # Install system packages |
| 164 | $SUDO $PKGCMD $PKGCMD_OPTS $PKG_CANDIDATES |
| 165 | |
| 166 | # Do the install(s) |
| 167 | cd /tmp |
| 168 | if [ $? -ne 0 ] |
| 169 | then |
| 170 | echo "Package installation failed: $PKG_CANDIDATES" |
| 171 | exit 1 |
| 172 | fi |
| 173 | |
| 174 | install_pip_package ubi_reader |
| 175 | install_pip_package jefferson |
| 176 | install_sasquatch |
| 177 | install_yaffshiv |
| 178 | install_cramfstools |
| 179 |