Created
August 15, 2019 07:47
-
-
Save spiller1975/4ef8dacc1da3a2c8e5147934f5b25da1 to your computer and use it in GitHub Desktop.
Huawei API
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 changemode(baseurl, session): | |
print "Trying to change 4G mode..." | |
data='<request><NetworkMode>02</NetworkMode></request>' | |
r= session.post(baseurl + "/api/net/net-mode", data=data, allow_redirects=False) | |
#r= session.get(baseurl + "/api/net/net-mode") | |
print r | |
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, allow_redirects=False) | |
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__": | |
s = login(baseurl, username, password) | |
changemode(baseurl, s) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment