#!/bin/bash # EasyBox control # Tested on 'EasyBox 804' (Firmware-Version: CIS804-05.02) # Provide your password via environment #PASSWORD="" # Enter hostname/adress of your EasyBox 804: #HOST="easy.box.local" # If you can't reach the Box via http://easy.box.local use it's IP instad: HOST="192.168.0.2" # Notes: # The reconnect duration of the EasyBox 804 is quite long, it can be up to 180 seconds. # fake user agent UA="Mozilla/5.0" # FIXME spaces won't work because of bash parameter handling COOKIEFILE="/tmp/easyBoxCookies.txt" WGET_OPTS=( "--quiet" "--load-cookies $COOKIEFILE" "--save-cookies $COOKIEFILE" "--keep-session-cookies" "--user-agent=\"${UA}\"" "-O -" ) WGET="/usr/bin/wget ${WGET_OPTS[@]}" # * * * NO MORE CHANGES DOWN HERE * * * COMMANDS="wget curl grep awk sed" for COMMAND in $COMMANDS; do if ! command -v $COMMAND &> /dev/null then echo "$COMMAND could not be found!" exit 1 fi done RETVAL="" touch $COOKIEFILE echo "request: http://${HOST}/main.cgi?page=login.html" 1>&2 OUT=$($WGET "http://${HOST}/main.cgi?page=login.html" ) DM_COOKIE=$(echo $OUT | grep dm_cookie | awk -F"dm_cookie='" '{ print $2 }' | awk -F"'" '{ print $1 }') echo "DM_COOKIE = "${DM_COOKIE} 1>&2 WBM_COOKIE=$(cat $COOKIEFILE | grep "wbm_cookie_session_id" | awk -F"\t" '{ print $7 }') echo "WBM_COOKIE = "${WBM_COOKIE} 1>&2 # Create a 'special' cookie file without HOST and PATH (important): # cat step1_cookie.txt | sed "s/${HOST}//g" | sed 's/\t\/\t/\t\t/g' > step1_cookie_special.txt #cat step1_cookie_special.txt # get WAN IP without login if [[ "$1" == "wan" && "$2" == "ip" ]]; then echo "request: http://${HOST}/data_model.cgi" 1>&2 POST_DATA="${DM_COOKIE}1InternetGatewayDevice.WANDevice.6.WANConnectionDevice.4.WANPPPConnection.1.ExternalIPAddress" OUT=$($WGET \ --post-data="${POST_DATA}" \ "http://${HOST}/data_model.cgi") RETVAL=$(echo "$OUT" | xmllint --xpath "//Value/text()" -) fi if [[ "$1" == "wan" && "$2" == "ipv6prefix" ]]; then echo "request: http://${HOST}/data_model.cgi" 1>&2 POST_DATA="${DM_COOKIE}1InternetGatewayDevice.X_JUNGO_COM_TR_181.Device.IP.Interface.33948673.IPv6Prefix.2049.Prefix" OUT=$($WGET \ --post-data="${POST_DATA}" \ "http://${HOST}/data_model.cgi") RETVAL=$(echo "$OUT" | xmllint --xpath "//Value/text()" -) fi echo "$RETVAL" exit 0 # Sending cookies at this step is very important. # If no cookies are sent, a 'new auth_key' is generated, not matching the cookies stored by this bash script. curl -s "http://${HOST}/main.cgi?js=rg_config.js" -b step1_cookie.txt \ -o step1_rg_config.js AUTH_KEY=$(cat step1_rg_config.js | grep "auth_key" | awk -F"auth_key = '" '{ print $2 }' | awk -F"'" '{ print $1 }') echo "AUTH_KEY = "${AUTH_KEY} # The sent md5-encrypted password consists of your 'Easybox-Kennwort' ... # ... and the 'auth_key' from http://easy.box.local/main.cgi?js=rg_config.js ! # The original Javascript-function is defined here: http://easy.box.local/main.cgi?js=wbm_be.js PASSWORD_MD5=$(echo -n ${PASSWORD}${AUTH_KEY} | md5sum | awk '{ print $1 }') echo "PASSWORD_MD5 = "${PASSWORD_MD5} echo -e "${DM_COOKIE_STEP3_STEP1}vodafone${PASSWORD_MD5}0" > step1_soap_data.xml # Now logging in (sending the password): # The DOUBLE APOSTROPHE in >> -H "Cookie: wbm[...]=${...}" << is crucial! curl -s "http://${HOST}/data_model.cgi" --cookie step1_cookie_special.txt --cookie-jar step2_cookies.txt -X POST -H 'Accept: application/xml, text/xml, */*; q=0.01' -H 'Accept-Encoding: gzip, deflate' -H 'Accept-Language: de,en-US;q=0.7,en;q=0.3' -H 'Cache-Control: no-cache' -H 'Connection: keep-alive' -H 'Content-Length: 387' -H 'Content-Type: text/xml; charset="utf-8"' -H "Cookie: wbm_cookie_session_id=${COOKIE_STEP1}" -H "Host: ${HOST}" -H 'Method: POST' -H 'Pragma: no-cache' -H 'Referer: http://${HOST}/main.cgi?page=login.html' -H 'SOAPAction: cwmp:Login' -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36' -H 'X-Requested-With: XMLHttpRequest' \ --data @step1_soap_data.xml \ -o "step2_answer.html" #cat step2_answer.html if [ "1" == "1" ]; then # Step 3: After sending the password COOKIE_STEP3=$(cat step2_cookies.txt | grep "wbm_cookie_session_id" | awk -F"\t" '{ print $7 }') echo "COOKIE_STEP3 = "${COOKIE_STEP3} curl -s "http://${HOST}/main.cgi?page=app.html" -b step2_cookies.txt \ -o "step3_answer.html" DM_COOKIE_STEP3=$(cat step3_answer.html | grep dm_cookie | awk -F"dm_cookie='" '{ print $2 }' | awk -F"'" '{ print $1 }') echo "DM_COOKIE_STEP3 = "$DM_COOKIE_STEP3 # firefox step3_answer.html fi if [ "1" == "1" ]; then # Step 4: Accessing the WAN IP (for testing the successful login without the timeconsuming reconnect) # Only for testing purposes – NOT NECESSARY FOR RECONNECT ! # (Output of WAN iP) echo "${DM_COOKIE_STEP3}1InternetGatewayDevice.WANDevice.6.WANConnectionDevice.4.WANPPPConnection.1.ExternalIPAddress" > step4_soap_data.xml # The DOUBLE APOSTROPHE in >> -H "Cookie: [...]" << is very important! curl -s \ "http://${HOST}/data_model.cgi" \ -b step2_cookies.txt \ -X POST \ -H 'Accept: application/xml, text/xml, */*; q=0.01' \ -H 'Accept-Encoding: gzip, deflate' \ -H 'Accept-Language: de,en-US;q=0.7,en;q=0.3' \ -H 'Cache-Control: no-cache' \ -H 'Connection: keep-alive' \ -H 'Content-Length: 452' \ -H 'Content-Type: text/xml; charset="utf-8"' \ -H "Cookie: wbm_cookie_session_id=${COOKIE_STEP3}" \ -H "Host: ${HOST}" \ -H 'Method: POST' \ -H 'Pragma: no-cache' \ -H "Referer: http://${HOST}/main.cgi?page=app.html" \ -H 'SOAPAction: cwmp:GetParameterValues' \ -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36' \ -H 'X-Requested-With: XMLHttpRequest' \ --data @step4_soap_data.xml \ -o "step4_answer.html" while read_dom; do #echo "$ENTITY => $CONTENT" if [[ $ENTITY = "Value xsi:type=\"xsd:string\"" ]] ; then echo $CONTENT fi done < step4_answer.html # firefox step4_answer.html fi ## TODO test this #if [ "1" == "1" ]; then # Step 5: Reconnect # # echo -e "${DM_COOKIE_STEP3}InternetGatewayDevice.WANDevice.6.X_JUNGO_COM_Reconnect1" > step5_soap_data.xml # # # rm -v step5_answer.html The DOUBLE APOSTROPHE in >> -H "Cookie: [...]" << is very important! # curl "http://${HOST}/data_model.cgi" -b step2_cookies.txt -X POST -H 'Accept: application/xml, text/xml, */*; q=0.01' -H 'Accept-Encoding: gzip, deflate' -H 'Accept-Language: de,en-US;q=0.7,en;q=0.3' -H 'Cache-Control: no-cache' -H 'Connection: keep-alive' -H 'Content-Length: 430' -H 'Content-Type: text/xml; charset="utf-8"' -H "Cookie: wbm_cookie_session_id=${COOKIE_STEP3}" -H "Host: ${HOST}" -H 'Method: POST' -H 'Pragma: no-cache' -H 'Referer: http://${HOST}/main.cgi?page=app.html' -H 'SOAPAction: cwmp:SetParameterValues' -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36' -H 'X-Requested-With: XMLHttpRequest' \ # --data @step5_soap_data.xml \ # -o "step5_answer.html" # ## firefox step5_answer.html #fi exit 0;