Skip to content

Instantly share code, notes, and snippets.

@NikitaKarnauhov
Created May 16, 2016 18:02
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save NikitaKarnauhov/5d9129f13e7b0e257cfbe93215751c7a to your computer and use it in GitHub Desktop.
Save NikitaKarnauhov/5d9129f13e7b0e257cfbe93215751c7a to your computer and use it in GitHub Desktop.
Shell script to decode and encode TP-LINK router config files
#!/usr/bin/env bash
# tlrecode.sh
# Decode and encode TP-LINK router config files.
#
# Creative Commons CC0 License:
# http://creativecommons.org/publicdomain/zero/1.0/
#
# To the extent possible under law, the person who associated CC0 with this
# work has waived all copyright and related or neighboring rights to this work.
in=""
out=""
mode=""
# See http://teknoraver.net/software/hacks/tplink/
key="478DA50BF9E3D2CF"
tmp_template="tlrecode.XXXXXXXXXX"
tmp=""
tmp2=""
usage() {
cat <<EOF
tlrecode.sh
Decode and encode TP-LINK router config files.
Usage: tlrecode.sh (-e|-d) [-i] INPUT [-o] OUTPUT
-i, --input Input file name.
-o, --output Output file name.
-d, --decode Decoding mode (default).
-e, --encode Encoding mode.
-h, --help Print this message.
EOF
}
error() {
echo "Error: $1" >&2
exit 1
}
decode() {
openssl enc -d -des-ecb -K "${key}" -nopad -in "${in}" -out "${tmp}" &&
tail -c +17 "${tmp}" > "${tmp2}" &&
tr -d "\000" < "${tmp2}" > "${out}"
}
encode() {
openssl md5 -binary "${in}" > "${tmp}" &&
cat "${tmp}" "${in}" > "${tmp2}" &&
truncate -s %8 "${tmp2}" &&
openssl enc -e -des-ecb -K "${key}" -nopad -in "${tmp2}" -out "${out}"
}
while [ $# -gt 0 ]; do
case "$1" in
-i|--input)
[ -z "${in}" ] || error "input file '${in}' is already set."
[ $# -gt 1 ] || error "option '$1' needs an argument."
in="$2"
shift 2
;;
-o|--output)
[ -z "${out}" ] || error "output file '${out}' is already set."
[ $# -gt 1 ] || error "option '$1' needs an argument."
out="$2"
shift 2
;;
-d|--decode)
[ -z "${mode}" ] || error "mode '${mode}' is already set."
mode="decode"
shift 1
;;
-e|--encode)
[ -z "${mode}" ] || error "mode '${mode}' is already set."
mode="encode"
shift 1
;;
-h|--help)
usage
exit 0
;;
*)
if [ -z "${in}" ]; then
in="$1"
elif [ -z "${out}" ]; then
out="$1"
else
error "unexpected argument '$1'"
fi
shift 1
;;
esac
done
[ -z "${in}" ] && error "no input file given."
[ -f "${in}" ] || error "input file '${in}' does not exist."
[ -z "${out}" ] && error "no output file given."
[ -z "${mode}" ] && mode="decode"
tmp="$(mktemp ${tmp_template})"
tmp2="$(mktemp ${tmp_template})"
if [ "${mode}" == "decode" ]; then
decode
else
encode
fi
result=$?
rm "${tmp}" "${tmp2}"
exit ${result}
@kochfede
Copy link

problem in OSX:

tlrecode.sh: line 53: truncate: command not found
any help?

@rsmartins78
Copy link

problem in OSX:

tlrecode.sh: line 53: truncate: command not found
any help?

brew install truncate

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