Created
August 14, 2019 20:35
-
-
Save spiller1975/e174d73fefd12a640b0428378433bc1d to your computer and use it in GitHub Desktop.
Huawei API script 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from __future__ import unicode_literals | |
#from PyCRC.CRC16 import CRC16 | |
import requests | |
import re | |
import hashlib | |
import base64 | |
import os, sys | |
def reboot(baseurl, session): | |
data='<request><Control>1</Control></request>' | |
r= session.post(baseurl + "api/device/control", data=data) | |
def login(baseurl, username, password): | |
s = requests.Session() | |
r = s.get(baseurl + "html/home.html") | |
csrf_tokens = grep_csrf(r.text) | |
headers_update(s.headers, csrf_tokens[1]) | |
data = login_data(username, password, str(csrf_tokens[1])) | |
r = s.request('POST', baseurl + "api/user/login", data=data) | |
s.headers.update({'__RequestVerificationToken': r.headers["__requestverificationtokenone"]}) | |
return s | |
def headers_update(dictbase, token): | |
dictbase['Accept-Language'] = 'en-US' | |
dictbase['Content-Type'] = 'application/x-www-form-urlencoded' | |
dictbase['X-Requested-With'] = 'XMLHttpRequest' | |
dictbase['__RequestVerificationToken'] = token | |
dictbase['Cache-Control'] = 'no-cache' | |
dictbase['User-Agent'] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/600.8.9 (KHTML, like Gecko) Version/8.0.8 Safari/600.8.9' | |
def grep_csrf(html): | |
pat = re.compile(r".*meta name=\"csrf_token\" content=\"(.*)\"", re.I) | |
matches = (pat.match(line) for line in html.splitlines()) | |
return [m.group(1) for m in matches if m] | |
def login_data(username, password, csrf_token): | |
def encrypt(text): | |
m = hashlib.sha256() | |
m.update(text) | |
return base64.b64encode(m.hexdigest()) | |
password_hash = encrypt(username + encrypt(password) + csrf_token) | |
return '<?xml version "1.0" encoding="UTF-8"?><request><Username>%s</Username><Password>%s</Password><password_type>4</password_type></request>' % (username, password_hash) | |
baseurl = "http://192.168.1.1/" | |
username = "admin" | |
password = "admin" | |
if __name__ == "__main__": | |
print "Trying to log in..." | |
s = login(baseurl, username, password) | |
print "Logged ! Trying to reboot..." | |
reboot(baseurl, s) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment