Advertisement
Guest User

Untitled

a guest
Jan 12th, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.02 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. USERNAME="admin"
  4. PASSWORD="MN8M8MQ6"
  5. ADDRESS="192.168.1.1"
  6.  
  7. DEBUG=1
  8.  
  9. SESSIONCOOKIE="SessionID_R3"
  10. LOGINPATH="/api/system/user_login"
  11. DSLPATH="/api/ntwk/WlanBasic"
  12. TIMEOUT=2
  13.  
  14. # Cookie timeout is around 15 - 20 seconds
  15.  
  16. exitError()
  17. {
  18.     if [ $? -ne 0 ]; then
  19.         echo "${1}" 1>&2
  20.         exit 1
  21.     fi
  22. }
  23.  
  24. debugPrint()
  25. {
  26.     if [ ${DEBUG} -eq 1 ]; then
  27.         echo "${1}"
  28.     fi
  29. }
  30.  
  31. LOGIN_PAGE=$(wget -T ${TIMEOUT} -t 1 -q -S -O - http://${ADDRESS} 2>&1)
  32. exitError "Failed to get login page"
  33.  
  34. COOKIE=$(echo -n "${LOGIN_PAGE}" | grep ${SESSIONCOOKIE} | sed -e 's/.*'${SESSIONCOOKIE}'=\([^;]*\).*/\1/')
  35. CSRF_PARAM_TOKEN=$(echo -n "${LOGIN_PAGE}" | grep csrf_ | sed -e 's/.*content="\(.*\)".*/\1/')
  36.  
  37. CSRF_DATA=(${CSRF_PARAM_TOKEN})
  38. CSRF_PARAM=${CSRF_DATA[0]}
  39. CSRF_TOKEN=${CSRF_DATA[1]}
  40.  
  41. # Hash the password
  42. # 1> Hash the password using SHA256
  43. # 2> Base64 encode the hash
  44. # 3> Concatenate the username + base64 encoded password hash + CSRF parameter + CSRF token (HG633 only)
  45. # 4> Hash the concatenated string using SHA256
  46. PASSWORD_HASH=$(echo -n $(echo -n ${PASSWORD} | sha256sum | cut -c -64) | base64 -w 0)
  47. DATA_TO_HASH=${USERNAME}${PASSWORD_HASH}${CSRF_PARAM}${CSRF_TOKEN}
  48. FINAL_HASH=$(echo -n ${DATA_TO_HASH} | sha256sum | cut -c -64)
  49.  
  50. debugPrint "*COOKIE: ${COOKIE}"
  51. debugPrint "*PARAM_TOKEN: ${CSRF_PARAM_TOKEN}"
  52. debugPrint "*PARAM: ${CSRF_PARAM}"
  53. debugPrint "*TOKEN: ${CSRF_TOKEN}"
  54. debugPrint "*PASSHASH: ${PASSWORD_HASH}"
  55. debugPrint "*DATATOHASH: ${DATA_TO_HASH}"
  56. debugPrint "*FINALHASH: ${FINAL_HASH}"
  57.  
  58. # Create JSON string containing the CSRF parameter, CSRF token, username and hashed password
  59. POST_DATA="{\"csrf\":{\"csrf_param\":\"${CSRF_PARAM}\",\"csrf_token\":\"${CSRF_TOKEN}\"},\"data\":{\"UserName\":\"${USERNAME}\",\"Password\":\"${FINAL_HASH}\"}}"
  60. debugPrint "*POSTDATA: ${POST_DATA}"
  61.  
  62. # Do the login
  63. # POST the JSON string and also send the session cookie from the login page
  64. LOGIN_RESPONSE=$(wget -T ${TIMEOUT} -t 1 -q -S -O - --no-cookies --header "Cookie: ${SESSIONCOOKIE}=${COOKIE}" --post-data=${POST_DATA} http://${ADDRESS}${LOGINPATH} 2>&1)
  65. debugPrint "*LOGINRES:"
  66. debugPrint "${LOGIN_RESPONSE}"
  67. exitError "Failed to get login response"
  68.  
  69. # Check login was successful
  70. # If grep doesn't find a match it exits with status 1
  71. TMP=$(echo -n "${LOGIN_RESPONSE}" | grep "\"errorCategory\":\"ok\"")
  72. exitError "Incorrect username or password (possibly locked out or something)"
  73.  
  74. # If something fails then we want the pipe to stop
  75. set -o pipefail
  76.  
  77. # Get new cookie value
  78. # Normally sed will exit with status 0, even if a match it not found. We want it to exit with status 1 which is what the extra q1 stuff does
  79. # http://stackoverflow.com/questions/1665549/have-sed-ignore-non-matching-lines
  80. # Modified to do the quit command instead of delete
  81. POST_DATA="{\"csrf\":{\"csrf_param\":\"${CSRF_PARAM}\",\"csrf_token\":\"${CSRF_TOKEN}\"},\"action\":\"SendSettings\",\"data\":{\"config2g\":{\"ID\":\"InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.\",\"country\":\"GB\",\"power\":100,\"mode\":\"b/g/n\",\"channel\":2,\"wmm\":true,\"nmcs\":\"auto\",\"nbw\":\"20_40\",\"ngi\":\"long\"},\"config5g\":{\"ID\":\"InternetGatewayDevice.LANDevice.1.WLANConfiguration.2.\",\"country\":\"GB\",\"power\":100,\"mode\":\"a/n/ac\",\"channel\":0,\"wmm\":true,\"X_BeamFormingEnable\":true,\"nmcs\":\"auto\",\"nbw\":\"80\",\"ngi\":\"long\"}}}"
  82. COOKIE=$(echo -n "${LOGIN_RESPONSE}" | grep ${SESSIONCOOKIE} | sed -e 's/.*'${SESSIONCOOKIE}'=\([^;]\+\).*/\1/' -e 'tx' -e 'q1' -e ':x')
  83. debugPrint "*COOKIE: ${COOKIE}"
  84. exitError "Invalid cookie"
  85.  
  86. # Get the Wifi stats
  87. #WIFISTATS=$(wget -T ${TIMEOUT} -t 1 -q -O - --no-cookies --header "Cookie: ${SESSIONCOOKIE}=${COOKIE}" --post-data=${POST_DATA} http://${ADDRESS}${DSLPATH} | cut -d '*' -f 2)
  88. WIFISTATS=$(wget -T ${TIMEOUT} -t 1 -q -O - --no-cookies --header "Cookie: ${SESSIONCOOKIE}=${COOKIE}" http://${ADDRESS}${DSLPATH} | cut -d '*' -f 2)
  89. debugPrint "***"
  90. debugPrint "${WIFISTATS}"
  91. debugPrint "***"
  92. exitError "Failed to get WIFISTATS"
  93.  
  94. # Print out the stats
  95. echo "${WIFISTATS}"
  96.  
  97. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement