Skip to content

Instantly share code, notes, and snippets.

@joba-1
Forked from wikrie/fritzbox-cert-update.sh
Last active August 24, 2021 15:07
Show Gist options
  • Save joba-1/365b30c6378f2d1f38687fb87ed1f7c9 to your computer and use it in GitHub Desktop.
Save joba-1/365b30c6378f2d1f38687fb87ed1f7c9 to your computer and use it in GitHub Desktop.
Fritzbox Fritz!Box AVM SSL Letsencrypt automatically update
#!/bin/bash
# parameters
USERNAME="maybe empty"
PASSWORD="fritzbox-password"
CERTPATH="path to cert eg /etc/letsencrypt/live/domain.tld/"
CERTPASSWORD="cert password if needed"
HOST="http://fritz.box"
# make and secure a temporary file. Arrange for automatic cleanup
TMP=""
trap 'rm -f "$TMP"' exit
TMP="$(mktemp -t XXXXXX)"
chmod 600 "$TMP"
# login to the box and get a valid SID
CHALLENGE=`wget -q -O - "$HOST/login_sid.lua" | sed -e 's/^.*<Challenge>//' -e 's/<\/Challenge>.*$//'`
HASH="`echo -n \"$CHALLENGE-$PASSWORD\" | iconv -f ASCII -t UTF16LE |md5sum|awk '{print $1}'`"
SID=`wget -q -O - "$HOST/login_sid.lua?sid=0000000000000000&username=$USERNAME&response=$CHALLENGE-$HASH"| sed -e 's/^.*<SID>//' -e 's/<\/SID>.*$//'`
# generate our upload request
BOUNDARY="---------------------------"`date +%Y%m%d%H%M%S`
(
printf -- "--$BOUNDARY\r\n"
printf "Content-Disposition: form-data; name=\"sid\"\r\n\r\n$SID\r\n"
printf -- "--$BOUNDARY\r\n"
printf "Content-Disposition: form-data; name=\"BoxCertPassword\"\r\n\r\n$CERTPASSWORD\r\n"
printf -- "--$BOUNDARY\r\n"
printf "Content-Disposition: form-data; name=\"BoxCertImportFile\"; filename=\"BoxCert.pem\"\r\n"
printf "Content-Type: application/octet-stream\r\n\r\n"
cat $CERTPATH/privkey.pem
cat $CERTPATH/fullchain.pem
printf "\r\n"
printf -- "--$BOUNDARY--"
) >> "$TMP"
# upload the certificate to the box
wget -q -O - "$HOST/cgi-bin/firmwarecfg" --header="Content-type: multipart/form-data boundary=$BOUNDARY" --post-file "$TMP" | grep SSL
@joba-1
Copy link
Author

joba-1 commented Oct 15, 2018

Thanks wikrie, for doing all the hard work. I just did

  • more quoting to be prepared for exotic temp files and passwords.
  • cleanup redirection a bit
  • cleanup temp file even if script is interrupted

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