搞了几天终于把这个搞出来了,代码不到50行,要是别的网站很好模拟,但是这个稍微有点麻烦,抓包工具用了很多,httpfox没有抓全,wireshark抓的又太多不好分析,最后还是使用神器fiddle才把它搞出来。
工具
- 火狐浏览器+firedebug插件,debug插件可在浏览器附加组件中添加,其他浏览器也可以,只要有可以监控浏览器的网络行为插件即可。(notes:这里没有使用到firedebug,而是使用火狐浏览器自带的审查工具)
- Python+requests包
步骤
打开火狐浏览器,输入网址”http://211.85.192.115/srun_portal_pc.php?ac_id=1&
“,打开校园网登录页面,如下:
登录账号和密码,显示登录成功
右键单击【审查元素】-【网络】
构造请求头1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22# 构造头部信息 注意Cookie可能十分重要,而且Cookie会有过期时间(我们学校过期时间是1个月),过期之后,可能需要复制新的Cookie替换。
post_header = {
#'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:66.0) Gecko/20100101 Firefox/66.0',
'Accept': '*/*',
#'Accept-Language': 'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
'Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
'Accept-Encoding': 'gzip, deflate',
#'Content-Type': 'application/x-www-form-urlencoded',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'X-Requested-With': 'XMLHttpRequest',
#'Origin': 'http://wlrz.fudan.edu.cn',
'Referer': 'http://211.85.192.115/srun_portal_pc.php?ac_id=1&',
#'Content-Length': '112',
'Content-Length': '104',
#'Cookie': 'login=YUtl4F5w2GWDfWUA8O0nj3eCm7TrrFp%252FtbchCKzbO84IQczKx%252Fyc5mYGG7s6FQxsyiZbjwUIcJ2ECcqXWO%252BzwX85KrsMq0MDW7tX1eoOzS00eusx19E0245ORqeeZHVwBzEd1DGI%253D',
'Cookie': 'login=请自己获得,这里不提供',
#'Host': 'wlrz.fudan.edu.cn',
'Host': '211.85.192.115',
'Connection': 'keep-alive',
}
构造发送数据1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#此处根据自己校园网Form Data中发送的数据进行更改
action = 'login'
username = '学号'
#password = '加密的密码'
password = '密码xxx'
ac_id = '1'
save_me = '0'
ajax = '1'
#user_ip = '127.131.1.1'
post_data = {
'action': action,
'username': username,
'password': password,
# 'password': base64.b64encode(password.encode()).decode(),
# 'password': base64.b64encode(password.encode()),
'ac_id': ac_id,
# 'user_ip': user_ip,
'save_me': save_me,
'ajax': ajax
}
内容有方式、学号、密码还有其他,其中密码不是明文,这密码应该是加密的,在所有js文件中搜索password,发现有一处函数,验证了是base64加密方式。(注意:以下操作是在在Chrome浏览器中进行的)
在chrome浏览器中,输入账号登录网址”http://211.85.192.115/srun_portal_pc.php?ac_id=1&
“,右键【审查元素】-【网络】,然后刷新
于是开始着手写代码了:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18def login_request():
if not is_net_ok():
print("[03]{} whpu-wlan is offline, request now...".format(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
# password = base64.b64encode(password.encode()).decode() #加密
try:
# 发送post请求登录网页
result = requests.post(post_addr, data=post_data, headers=post_header)
# z.text为str类型的json数据因此先编码成byte类型再解码成unicode类型这样就可以正常输出中文
s = result.text.encode('utf-8').decode('unicode-escape')
print(s)
print("login success!")
except:
print("[00]{} request error, whpu-wlan isnto connected to WIFI...".format(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
else:
print("[02]{} whpu-wlan is online, login sucesss...".format(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
login_request()