Home Backend Development Python Tutorial Python realizes automatic login to campus network

Python realizes automatic login to campus network

Apr 24, 2018 pm 04:18 PM
python Campus Network Log in

Below I will share with you an example explanation of how to implement automatic login on the campus network using Python. It has a good reference value and I hope it will be helpful to everyone. Let’s come and take a look

Because I recently wanted to use a Raspberry Pi to build a remote monitoring system, and because the school network requires logging in from the web page and it is inconvenient to bring a monitor with the Raspberry Pi, I thought about it. A script program that can automatically log in to the campus network, eliminating the trouble of opening the browser and entering the account and password every time.

1. Tool

Firefox browser firedebug plug-in, debug plug-in can be added to the browser add-on, other browsers can also be used as long as they can monitor the browser's network behavior.

python requests package

2. Steps

1) First open the login interface, and then press f12 to open the firedebug plug-in. At this time, there is no debug function. Record the behavior, then click the refresh button, then click the login button, call up debug again and click the console tab. At this time, you will find many get methods plus the final POST method generated by login, as shown in the figure

2) Click on the small arrow of the POST method and you will find the browser's request header information, which we need to save (not the response header),

3) View the content in the POST tab. The variables and parameters need to be saved. You can see that the password is encrypted. .If you just log in with your own account and password, the program can end here. Replace the data with the data you captured and use the following code to log in to the campus network.

import requests
#登录地址
post_addr="http://a.nuist.edu.cn/index.php/index/login"
#构造头部信息
post_header={
 'Host': 'a.nuist.edu.cn',
 'User-Agent':'Mozilla/5.0 (X11; Linux x86_64; rv:55.0) Gecko/20100101 Firefox/55.0',
 'Accept': 'application/json, text/javascript, */*; q=0.01',
 'Accept-Language':'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
 'Accept-Encoding': 'gzip, deflate',
 'Content-Type': 'application/x-www-form-urlencoded',
 'X-Requested-With':'XMLHttpRequest',
 'Referer':'http://a.nuist.edu.cn/index.php?url=aHR0cDovL2RldGVjdHBvcnRhbC5maXJlZm94LmNvbS9zdWNjZXNzLnR4dA==',
 'Content-Length': '67',
 'Cookie':'_gscu_1147341576=059821653286gq10; sunriseUsername=123441534;\
 sunriseDomain=NUIST;sunriseRememberPassword=true; sunrisePassword=123456;\
 PHPSESSID=hb0o9bkct2f6ge164oj3vj0me5;think_language=zh-CN',
 'Connection':'keep-alive',
}
#构造登录数据
post_data={'domain':'NUIST',
   'enablemacauth':'0',
   'password':'MTgzMzEw',
   'username':'xxxxxxx'
   } 
#发送post请求登录网页
z=requests.post(post_addr,data=post_data,headers=post_header)
Copy after login

4) But I found a problem is that the above program can only be used by yourself. If you change the account and password to log in, you have to use the browser to capture the data packets. , annoying... After carefully reviewing the above steps, I found that the difficulty in writing a program that allows other accounts to log in without packet capture is that the password in the post_data program is encrypted. If you can know its encryption method, write a general one (in this article On-campus) program is very easy.

In fact, if you know more about the commonly used encryption methods in this step, then it is easier to check the source code (js) of the web page to figure out its encryption method. Unfortunately, I don’t understand. , I only know one md5 encryption, so I tried to use the hashlib package in python to encrypt the password and then see if it is the same as the captured data. Unfortunately, not even one character is the same..., then I thought since the data is The encryption process that is sent from the local server to the server must be completed on the client side, most likely through a js script (I don’t know much about web page programming, I only know that js can be executed on the client side, so I guess it is the js script that completes the passward encoding). Then check the captured js code through debug.

Open the debugger and you can see a row of js code on the left. You can roughly guess the role of js through the js file name.

5) Looking at the file name on the left, you can directly guess that the functions include login.js, md5.js, client.js, usercss.js. Since md5 is not a password encryption method, check it out Other js codes. Fortunately, I clicked on the first base64 code and found out that this code is an encoding method. I hurried to Baidu and found that base64 is indeed an encoding method. I struck while the iron was hot by using Baidu python's base64 encoding implementation and found that python had already Integrate the base64 package and use this package to encode the password again... It is found that the result is exactly the same as the captured postdata. At this point, writing a general program is just around the corner!!

The complete code is as follows (Rough version):

#!/usr/bin/python3
# -*- coding: utf-8 -*-
'''
 FileName:conNet.py
 Author:shenhuixiang
 Copyright(c)2017,shenhuixiang
'''
import base64
import requests
'''
输入账号密码和登录的网络
网络参数为如果是移动的则填写CMCC
如果是学号则填NUIST
'''
USER_ACCOUNT='110'
DOMAIN_SELECTION='CMCC'
USER_PASSWATD='123456'
#登录地址
post_addr="http://a.nuist.edu.cn/index.php/index/login"
#构造头部信息
post_header={
 'Host': 'a.nuist.edu.cn',
 'User-Agent':'Mozilla/5.0 (X11; Linux x86_64; rv:55.0) Gecko/20100101 Firefox/55.0',
 'Accept': 'application/json, text/javascript, */*; q=0.01',
 'Accept-Language':'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
 'Accept-Encoding': 'gzip, deflate',
 'Content-Type': 'application/x-www-form-urlencoded',
 'X-Requested-With':'XMLHttpRequest',
 'Referer':'http://a.nuist.edu.cn/index.php?url=aHR0cDovL2RldGVjdHBvcnRhbC5maXJlZm94LmNvbS9zdWNjZXNzLnR4dA==',
 'Content-Length': '67',
 'Cookie':'_gscu_1147341576=059821653286gq10; sunriseUsername='+USER_ACCOUNT+';\
 sunriseDomain='+DOMAIN_SELECTION+';sunriseRememberPassword=true; sunrisePassword='+USER_PASSWATD+';\
 PHPSESSID=hb0o9bkct2f6ge164oj3vj0me5;think_language=zh-CN',
 'Connection':'keep-alive',
}
'''
password在post的参数中经过base64编码,
为了查找password加密方式...吐血三升.
'''
post_data={'domain':DOMAIN_SELECTION,
   'enablemacauth':'0',
   'password':base64.b64encode(USER_PASSWATD.encode()),
   'username':USER_ACCOUNT
   }
#发送post请求登录网页
z=requests.post(post_addr,data=post_data,headers=post_header)
#z.text为str类型的json数据因此先编码成byte类型在解码成unicode型这样就可以正常输出中文
s=z.text.encode('utf-8').decode('unicode-escape')
print(s)
Copy after login

Related recommendations:

Python implements a random call Open the webpage in the browser


The above is the detailed content of Python realizes automatic login to campus network. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

Python vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Can vs code run in Windows 8 Can vs code run in Windows 8 Apr 15, 2025 pm 07:24 PM

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

Can visual studio code be used in python Can visual studio code be used in python Apr 15, 2025 pm 08:18 PM

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

How to run python with notepad How to run python with notepad Apr 16, 2025 pm 07:33 PM

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".

Where to write code in vscode Where to write code in vscode Apr 15, 2025 pm 09:54 PM

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

See all articles