Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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}
@kamalkech
Copy link

kamalkech commented Feb 27, 2018

how use it ?
./tlrecode.sh nameFile

@vale981
Copy link

vale981 commented May 1, 2018

With the TD=W8970B you have to add a Null byte to the config, md5 it finally truncate before the encryption. I'll post a fork.

@m0bYczE
Copy link

m0bYczE commented May 27, 2018

Hello, with the newst version firmware not working.

@kochfede
Copy link

kochfede commented Dec 20, 2018

problem in OSX:

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

@rsmartins78
Copy link

rsmartins78 commented May 4, 2020

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