实际使用路由器为荣耀路由Pro
下面所述过程,很大部分参考了下面两篇博客
https://blog.csdn.net/Touale/article/details/113571862
https://www.ruterfu.com/2021/04/19/20210419-login-by-codes-huawei-router-WS5200/
其中第一篇博客稍显不好,只是给出简略的过程,若是对于http, js(javascript)不熟悉的人很难上手,实际实现的代码也没有,但还是给出的登录过程的关键数据。第二篇博客就很好的了,给出了整个的加密过程还有最终实现的代码。由于本人多用python,对javascript及java不是很熟悉,稍做尝试,还是用回python。感谢两位博主,特此写上自己的python实现,希望有需要的用得上。
实现过程
导入库
import sys
import requests
from lxml import etree
import json
import hashlib
import hmac
from Crypto import Random
其中requests用于http的交互,etree用于解析网页源码html,并获取相应字段,hashlib和hmac用于实现加密算法,Crypto也是加密算法库,但这里用的不多,仅是用来生成随机字符串
Crypto的安装为pip3 install pycryptodome
获取第一次post中需要的csrf_param,csrf_token,username,firstnonce数据
可查看第二篇博客,有关于post的抓包,这里就不放我的抓包的,用wireshark即可。
重点说明这几个数据来源:
csrf_param,csrf_token为上一次访问回复中的数据,所以第一次post时,应先访问首页来获得这两个数据
username则默认为admin,我的路由器是只有密码输入的。
firstnonce则32位长的随机字节流,在表现上可能是64位的hex码,如在post上,抓包也是,64位(只含0-9,a-f的)
firstNonce = Random.get_random_bytes(32)
result = sess.get("http://192.168.3.1/html/index.html")
update_Information(result)
request_header = {
'Connection': 'keep-alive',
'Cache-Control': 'max-age=0',
'Origin': 'http://192.168.3.1',
'Content-Type': 'application/json;charset=UTF-8',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36',
'Accept': 'application/json, text/javascript, */*; q=0.01',
'X-Requested-With': 'XMLHttpRequest',
'_ResponseFormat': 'JSON',
'Referer': 'http://192.168.3.1/html/index.html',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
}
firstNonce_str = Random.get_random_bytes(32).hex()
firstNonce = bytes(firstNonce_str, encoding='utf-8')
login_post_data = {
'csrf': {'csrf_param': gl_csrf_param, 'csrf_token': gl_csrf_token},
'data': {'username': 'admin', 'firstnonce': firstNonce_str},
}
url_routerlogin = 'http://192.168.3.1/api/system/user_login_nonce' #/api/system/user_login_nonce
deviceLogin =

本文介绍了使用Python登录华为路由器后台的实现过程,包括获取必要的数据、加密算法以及如何通过requests库进行HTTP交互。着重解释了如何处理csrf_param、csrf_token、nonce等关键参数,并提到了PBKDF2加密算法在Python中的应用。

3011

被折叠的 条评论
为什么被折叠?



