Skip to content

Instantly share code, notes, and snippets.

@vale981
Forked from NikitaKarnauhov/tlrecode.sh
Last active October 3, 2020 16:23
Show Gist options
  • Save vale981/ebf36a760d3ca9f8f84edc154e33b3fd to your computer and use it in GitHub Desktop.
Save vale981/ebf36a760d3ca9f8f84edc154e33b3fd 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.
# Mod for TD-W8970B(DE)
#
# 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=""
tmp3=""
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() {
cat "${in}" > "${tmp}" &&
truncate -s +1 "${tmp}" &&
openssl md5 -binary "${tmp}" > "${tmp2}" &&
cat "${tmp2}" "${tmp}" > "${tmp3}" && truncate -s %8 "${tmp3}" &&
openssl enc -e -des-ecb -K "${key}" -nopad -in "${tmp3}" -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})"
tmp3="$(mktemp ${tmp_template})"
if [ "${mode}" == "decode" ]; then
decode
else
encode
fi
result=$?
rm "${tmp}" "${tmp2}"
exit ${result}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment