测试网站是本人学校,费话不多说下面开始
首先直接导库,过程中需要时间戳,rsa加密
import requests import re import time from Crypto.PublicKey import RSA from Crypto.Cipher import PKCS1_v1_5
接下来,开始获取token,搜索发现有csrftoken,把那串值通过正则拿下来
token_url = '本人学校网址' headers = { 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9', 'Accept-Language': 'zh,zh-CN;q=0.9', 'Cache-Control': 'max-age=0', 'Connection': 'keep-alive', 'Upgrade-Insecure-Requests': '1', 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36', } r = requests.session() token_page = r.get(token_url,headers=headers).text #获取token rule = r'name="csrftoken" value="(.*?)"/>' token = re.findall(rule,token_page) if token[0] == "": print("token获取错误") else: #print(token[0]) pass
下面包装了三个函数,为rsa加密做准备,根据js分析得知,需要从服务端获取两个参数一个modulus,exponent
进一步查看发现这两个值通过请求这个地址获得
代码实现,不得不说,chatgpt是真的牛逼,我知道这个js的大概,但是不懂怎么用python去实现,结果我直接复制进去chatgpt直接把python代码给我了,牛逼!!!!!!!!!!
# 将base64编码的字符串转化为十六进制编码的字符串 def b64tohex(b64str): import base64 return base64.b64decode(b64str).hex() #将十兛进制编码的密文转化为base64编码的密文 def hex2b64(hexstr): import base64 return base64.b64encode(bytes.fromhex(hexstr)).decode() def rsa_encrypt(modulus,exponent): # 创建 RSA 密钥 modulus = b64tohex(modulus) exponent = b64tohex(exponent) key = RSA.construct((int(modulus, 16), int(exponent, 16))) # 加密 cipher = PKCS1_v1_5.new(key) password = '我的密码' ciphertext = cipher.encrypt(password.encode()) # 将密文转换为base64编码 enPassword = hex2b64(ciphertext.hex()) #print("加密后的密文: ", enPassword) return enPassword
#注意这里还有个时间戳变量,不过我感觉可能没啥用,反正加上就加上
#生成时间毫秒级时间戳
milliseconds = int(round(time.time() * 1000))
#print(milliseconds)
params = {
'time': milliseconds,
}
#获取加密密码的重要参数信息地址 modules , exponnent ,教务系统进行李rsa加密
dir = r.get('两个参数的地址', params=params, headers=headers, verify=False).json()
modulus = dir.get("modulus")
exponent = dir.get("exponent")
password = ''
if modulus=="" or exponent=="":
print("加密的关键参数获取错误")
else:
#print(modulus,exponent)
password += rsa_encrypt(modulus, exponent)
#print(password)
最后将前面获取到的csrftoken和加密后的密码进行和账号一起包装成data,发包拿下登陆后的cookies
data = { 'csrftoken': token[0], 'language': 'zh_CN', 'yhm': '本人学号', 'mm': [ password, password, ], } verify = r.post('学校登录网址',headers=headers, data=data).text account_url = '个人信息页面网址'+str(milliseconds) account_page = r.get(account_url,headers=headers).text #print(account_page) #通过判断是否有修改密码判定是否成功 if "修改密码" in account_page: print("congratulation, login successful!")
最后也是成功successful
开始都快放弃选择selenium了,但是还是尝试查资料搞好了,chatgpt是真的牛逼,开始也不确定到底加密后的密码是不是正确可以用的,尝试下还真是哈哈哈,浅浅记录下!
标签:加密,exponent,登录,python,爬虫,headers,token,print,modulus From: https://www.cnblogs.com/baolongzhanshi/p/17065671.html