Filegrabber.
Code:
#!/bin/bash
FAILS=""
MAINEXPORTDIR="/mnt/us/export"
TESTUSRLIB="/usr/lib/"
TESTLIB="/lib/"
BASEUS="/mnt/base-us/"
# fail on missing victim
if [ -z ${@} ]
then
echo "hmm I need a victim... usage: filechecker <binary_file>"
exit 1
fi
# fail on missing victim
if [ ! -e ${@} ]
then
echo "hmm I cannot find: ${@} - you sure? check the file exists."
exit 1
fi
FILES=$(readelf -d ${@} |awk -F\[ ' /Shared library/ {print $2}' | cut -d"]" -f1)
BINARY=$(basename ${@})
EXPORTDIR="${MAINEXPORTDIR}""/""${BINARY}"
mkdir -p $EXPORTDIR/bin
mkdir -p $EXPORTDIR/lib
mkdir -p $EXPORTDIR/kindle-lib
cd $EXPORTDIR
# copy the binary we are interested in
cp -f ${@} "${EXPORTDIR}/bin/${BINARY}"
echo "copying ${@} to ${EXPORTDIR}/bin/${BINARY}"
LIBCOUNT=$(ls -al /lib/* | wc -l)
USRLIBCOUNT=$(ls -al /usr/lib/* | wc -l)
TOTCOUNT=$(ls -Ra1d /* | wc -l)
echo "Searching ${USRLIBCOUNT} user libs, ${LIBCOUNT} libs, ${TOTCOUNT} extended dirs"
for file in $FILES
do echo
MATCH=0
echo $file
#LOOSERESULTS=$(find / -iname '*'"$file"'*')
FASTLIBRESULTS=$(find /lib -iname '*'"$file"'*')
for result in $FASTLIBRESULTS
do
if [[ "${result:0:5}" == $TESTLIB ]]; then
echo "kindle supported at $result"
# do copy the native stuff
cp -f ${result} $EXPORTDIR/kindle-lib/${file}
MATCH=1
break
fi
done
if [ "$MATCH" == "1" ]; then
echo "/lib support detected"
continue
fi
FASTUSRLIBRESULTS=$(find /usr/lib -iname '*'"$file"'*')
for result in $FASTUSRLIBRESULTS
do
if [[ "${result:0:9}" == $TESTUSRLIB ]]; then
echo "kindle supported at $result"
# DO copy the native stuff ????
# Have to check this actually gets the right "native equivalents" when I get a chance
cp -f ${result} $EXPORTDIR/kindle-lib/${file}
MATCH=1
break
fi
done
if [ "$MATCH" == "1" ]; then
echo "/usr/lib support detected"
continue
fi
EXACTRESULTS=$(find / -iname '*'"$file")
for result in $EXACTRESULTS
do
#echo "result:0:13= ${result:0:13} result= $EXPORTDIR/lib/${file}"
if [[ "${result:0:13}" == $BASEUS || "${result}" == $EXPORTDIR/lib/${file} ]] ; then
# dont bother with duplicates or exports
continue
fi
echo "non native result "${result}
cp -f ${result} $EXPORTDIR/lib/${file}
# just copy the first non-native instance for now...
MATCH=1
break
#fi
done
# we got this far
if [ "$MATCH" == "0" ]; then
FAIL="$FAIL""${file} "
fi
done
#remove empty lib dir
if [[ "$(ls -1 $EXPORTDIR/lib | wc -l)" == "0" ]] ; then
rmdir $EXPORTDIR/lib
fi
#remove empty bin dir if we really screwed up
if [[ "$(ls -1 $EXPORTDIR/bin | wc -l)" == "0" ]] ; then
rmdir $EXPORTDIR/bin
fi
#report failures
if [ "$FAIL" != "" ]; then
echo "Failure of some files: $FAIL"
echo "Failed files that have no source for export are: $FAIL" >> $EXPORTDIR/failure.log
echo "Your binary wont work without them, please attempt to locate a copy of them and put them on your device then export again" >> $EXPORTDIR/failure.log
else
touch $EXPORTDIR/export.log
echo "Export complete at $EXPORTDIR ensure you run $BINARY like: cd $EXPORTDIR/bin; LD_LIBRARY_PATH=../lib:\$LD_LIBRARY_PATH ./$BINARY" | tee $EXPORTDIR/export.log
fi
EDIT:
hmmm... needs reworking to move the FASTUSRLIBRESULTS kindle-lib results section into the EXACTRESULTS section I think. This is just ripped straight off an old device.
I'll check at some point. one day. Or someone else can.
could do with testing probably, but it's one of the iterations I used
at some point.
Cheers.
EDIT:
REQUIRES! READELF!!! (forgot this was not included natively)