Advertisement
Guest User

Speedport-W724V-TypA-newFW

a guest
Jan 2nd, 2018
562
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.64 KB | None | 0 0
  1. ## Speedport W724V Typ A Reconnect -- For new Firmware 05011603.05.028 [Nov 2017] and probably also 05011603.05.020 [Dec 2016]
  2. ## Based on Speedport Smart Reconnect by Bizzy13
  3. ##
  4. ## INSTALL INSTRUCTION
  5. ##
  6. ## Windows:
  7. ##    - Download Python 2.7                                                                      https://www.python.org/ftp/python/2.7.14/python-2.7.14.msi
  8. ##    - Install Python 2.7 under C:\Python27\
  9. ##    - Download pycryptodome-3.4.7-cp27-cp27m-win32.whl and save it directly under C:\          https://pypi.python.org/simple/pycryptodome/
  10. ##    - Open the command prompt and type in the following commands
  11. ##          cd C:\Python27\Scripts
  12. ##          pip install C:\pycryptodome-3.4.7-cp27-cp27m-win32.whl
  13. ##    - JDownloader Settings
  14. ##          Reconnect method -> External Batch Reconnect
  15. ##          Interpreter -> C:\Python27\python.exe
  16. ##          Batch-Script -> C:\PATH_TO_RECONNECT_SCRIPT\Reconnect.py
  17. ##
  18. ## Linux:
  19. ##    - Python 2.7 (In the most Linux Distributions already included)
  20. ##    - Download pycryptodome-3.4.7.tar.gz                                                      https://pypi.python.org/simple/pycryptodome/
  21. ##    - Open the Terminal and type in the following command
  22. ##          sudo pip install /PATH_TO_THE_DOWNLOADED_FILE/pycryptodome-3.4.7.tar.gz
  23. ##    - JDownloader Settings
  24. ##          Reconnect method -> External Batch Reconnect
  25. ##          Interpreter -> /usr/bin/python
  26. ##          Batch-Script -> /PATH_TO_RECONNECT_SCRIPT/Reconnect.py
  27. ##
  28.  
  29. ##
  30. ## CONFIG
  31. ##
  32.  
  33. device_password  =  "YourPassword"          # The device password for login
  34. speedport_url    =  "http://speedport.ip/"  # The URL to the Speedport W724V Typ A Configurator
  35. Sleeptime = 5                               # If the Reconnect don't work, change the number to 10 and then try again
  36.  
  37. ##
  38. ## DO NOT CHANGE ANYTHING BELOW THIS LINE
  39. ##
  40.  
  41. from Crypto.Hash import SHA256
  42. import time
  43. import sys
  44. import socket
  45. import json
  46. import urllib
  47. import urllib2
  48. import cookielib
  49.  
  50. login_html = "html/login/index.html"
  51. login_json = "data/Login.json"
  52. connection_json = "data/Connect.json"
  53. connection_html = "html/content/internet/connection.html"
  54. ipinfo_json = "data/InetIP.json"
  55. challenge_val = ""
  56.  
  57. http_header = {"Content-type": "application/x-www-form-urlencoded", "charset": "UTF-8"}
  58. cookies = cookielib.CookieJar()
  59. opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookies))
  60. socket.setdefaulttimeout(7)
  61.  
  62. # URL has to end with slash
  63. if not speedport_url.endswith("/"):
  64.     speedport_url += "/"
  65.  
  66. # Gets the challenge_val token from login page
  67. def get_challenge_val():
  68.     global challenge_val
  69.  
  70.     print("Extracting Random-Key...")
  71.  
  72.     challenge_val = extract(speedport_url + login_html, 'challenge = "', '";')
  73.  
  74.     if not bool(challenge_val):
  75.         sys.exit("Couldn't extract Random-Key from " + speedport_url + login_html)
  76.     else:
  77.         print("Random-Key: "+ challenge_val)
  78.  
  79. # Login with devices password
  80. def login():
  81.     print ("Logging in...")
  82.  
  83.     # Hash password with challenge_val
  84.     sha256_full = SHA256.new()
  85.     sha256_full.update("%s:%s" % (challenge_val, device_password))
  86.     encrypted_password = sha256_full.hexdigest()
  87.  
  88.     # Finally login
  89.     json_string = open_site(speedport_url + login_json, {"csrf_token": "nulltoken", "showpw": 0, "password": encrypted_password, "challengev": challenge_val})
  90.     json_object = string_to_json(json_string)
  91.  
  92.     # Check valid response
  93.     for x in json_object:
  94.         if x["vartype"] == "status":
  95.             if x["varid"] == "login":
  96.                 if x["varvalue"] != "success":
  97.                     sys.exit("Failed to login at URL " + speedport_url + login_json)
  98.             if x["varid"] == "status":
  99.                 if x["varvalue"] != "ok":
  100.                     sys.exit("Failed to login at URL " + speedport_url + login_json)
  101.  
  102.     print("Login successful")
  103.  
  104. # Extract a String
  105. def extract(url, a, b):
  106.     html = open_site(url, None)
  107.     start = html.find(a)
  108.  
  109.     end = html.find(b, start)
  110.     return html[(start + len(a)) : end]
  111.  
  112. # Reconnecting the Speedport
  113. def reconnect():
  114.     # Get old IPv4 address
  115.     oldIP = "Error"
  116.     json_string_oldIP = open_site(speedport_url + ipinfo_json, None)
  117.     json_object = string_to_json(json_string_oldIP)
  118.     for x in json_object:
  119.         if x["varid"] == "public_ip_v4":
  120.             oldIP = x["varvalue"]
  121.  
  122.     csrf_token = get_csrf_token()
  123.  
  124.     print("Disconnecting...")
  125.  
  126.     # Disconnect Speedport with token
  127.     open_site(speedport_url + connection_json, "req_connect=disabled&csrf_token=" + urllib.quote_plus(csrf_token))
  128.  
  129.     time.sleep(3)
  130.  
  131.     print("Connecting...")
  132.     # Connect Speedport with token
  133.     open_site(speedport_url + connection_json, "req_connect=online&csrf_token=" + urllib.quote_plus(csrf_token))
  134.  
  135.     time.sleep(Sleeptime)
  136.  
  137.     # Get new IPv4 address
  138.     newIP = "Error"
  139.     json_string_newIP = open_site(speedport_url + ipinfo_json, None)
  140.     json_object = string_to_json(json_string_newIP)
  141.     for x in json_object:
  142.         if x["varid"] == "public_ip_v4":
  143.             newIP = x["varvalue"]
  144.  
  145.     print("Old IP: " + oldIP)
  146.     print("New IP: " + newIP)
  147.  
  148.     if oldIP == newIP:
  149.         print("Reconnect failed")
  150.     else:
  151.         print("Reconnect successful")
  152.  
  153.     #Log Out
  154.     open_site(speedport_url + login_json, {"logout": "byby", "csrf_token": urllib.quote_plus(csrf_token)})
  155.  
  156.     quit()
  157.  
  158. def get_csrf_token():
  159.     print("Extracting csrf_token...")
  160.  
  161.     html = open_site(speedport_url + connection_html, None)
  162.     start = html.find("csrf_token")
  163.  
  164.     # Found a crsf token?
  165.     if start == -1:
  166.         sys.exit("Couldn't extract csrf_token")
  167.  
  168.     # Get raw token
  169.     end = html.find(";", start)
  170.     ex = html[(start + len("csrf_token =  \"") - 1) : (end - 1)]
  171.  
  172.     print("csrf_token: " + ex)
  173.     return ex
  174.  
  175. # Opens a specific site
  176. def open_site(url, params):
  177.     # Params only for post requests and dicts
  178.     if params != None and type(params) is dict:
  179.         params = urllib.urlencode(params)
  180.  
  181.     # Open URL
  182.     req = urllib2.Request(url, params, http_header)
  183.     res = opener.open(req)
  184.  
  185.     # Return result
  186.     return res.read()
  187.  
  188. # Converts a string to a json object
  189. def string_to_json(string):
  190.     # Replace special tokens
  191.     string = string.strip().replace("\n", "").replace("\t", "")
  192.  
  193.     # Some strings are invalid JSON object (Additional comma at the end...)
  194.     if string[-2] == ",":
  195.         string_list = list(string)
  196.         string_list[-2] = ""
  197.  
  198.         return json.loads("".join(string_list))
  199.  
  200.     return json.loads(string)
  201.  
  202.  
  203. # At first get challenge_val
  204. get_challenge_val()
  205.  
  206. # Then login
  207. login()
  208.  
  209. # Then Reconnecting
  210. reconnect()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement