Last active
March 3, 2020 22:25
-
-
Save fogti/94b24e85c6c48251ba0e0057c882a8ee to your computer and use it in GitHub Desktop.
inject a shell script into another host and execute it (using dbclient)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DBCLIENT="$(which dbclient)" | |
[ -z "$INJECT_USERNAME" ] && INJECT_USERNAME="root" | |
[ -z "$INJECT_PASSWORD" ] && INJECT_PASSWORD="1q2w3e" | |
doremote() { | |
DROPBEAR_PASSWORD="$INJECT_PASSWORD" "$DBCLIENT" -y -y -A -l "$INJECT_USERNAME" "$INJECT_HOST" "$@" | |
} | |
inject_progr() { | |
echo -n '[ ' | |
case $? in | |
0) echo 'ok ]' ;; | |
*) echo 'failed ]'; return 1 ;; | |
esac | |
} | |
# inject SOURCE | |
inject() { | |
if [ -z "$1" ]; then | |
echo 'USAGE: inject SOURCE' | |
return 1 | |
fi | |
local PSRC=".${1}.isp" | |
local DEST='~/.'"$$_$(basename "$1")" | |
echo "inject: $1 -> $PSRC -> $INJECT_USERNAME@$INJECT_HOST:$DEST" | |
echo -n ' - patch ' | |
cp -f "$1" "$PSRC" | |
if [ "$(tail -n 1 "$1")" != 'rm -f "$0"' ]; then | |
echo 'rm -f "$0"' >> "$PSRC" | |
fi | |
[ -f "$PSRC" ] | |
inject_progr || return 1 | |
echo -n ' - upload ' | |
cat "$PSRC" | doremote "cat > $DEST" &>/dev/null | |
inject_progr || return 1 | |
echo ' - first clean' | |
rm -f "$PSRC" | |
echo -n ' - make executable ' | |
doremote "chmod +x $DEST" &>/dev/null | |
inject_progr || return 1 | |
echo ' - execute' | |
doremote "$DEST" 2>&1 | tail -n +3 | |
echo -n ' ' | |
inject_progr || return 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment