Python automatic login for campus network

Source: Internet
Author: User
Tags md5 encryption
Below for you to share a Python implementation of the campus network automatic login example, with a good reference value, I hope to be helpful to everyone. Come and see it together.

Because recently want to use Raspberry Pi to make a remote monitoring system, and because the school network needs to log in from the Web and Raspberry Pi is not easy to do a monitor with, so think about a can automatically log on to the campus network of the script program, save every time to open the browser to enter the account password trouble.

1. Tools

Firefox +firedebug plugin, debug plug-ins can be added in the browser, other browsers can also be as long as the browser can monitor the network behavior.

Python+requests Bag

2. Steps

1) first open to the login screen, and then press F12 to open the Firedebug plug-in, at this time debug no record behavior, and then click the Refresh button, then click the Login button, again call debug click on the Console tab, You will find a lot of get methods plus the Last Post method generated by the login,

2) Click on the Post method of the small arrow will find that the browser request header headers Information, this need we save (not the response header),

3) look at the contents of the Post tab, where the variables and parameters need to be saved, and you can see that the password is being added. If you just log in to your account password, Then the program ends here. Replace the data with the data you've captured and log in to the campus network using the code below.

Import requests# Login Address 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 ': ' "The ' 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 (p Ost_addr,data=post_data,headers=post_heaDer 

4) but found a problem is that the above program can only use their own, if you change an account password login then also use the browser to catch the packet, annoying ... Take a closer look at the steps above to find that you want to write a other account without grasping the package can also log on the difficulty is that the construction of the Post_data program password is encrypted, if you can know its encryption, write a generic (in the school) program is very easy.

In fact, this step if the commonly used encryption method, then to view the source code (JS) is relatively easy to obtain its encryption, unfortunately I do not understand, only know a MD5 encryption, So I try to encrypt the password with the Hashlib package in Python and see if it is the same as the captured data, unfortunately even a word Fudu has the same ..., and then I think since the data is from the local to the server to send the encryption process must be done on the client is likely to be through the JS script ( Do not understand the Web programming, only know that JS can be executed at the client, so guess is the JS script to complete the Passward encoding), and then through debug to see the JS code caught.

Open Debug Debugger can see the left side of a line of JS code, through the JS file name can probably guess the role of JS.

5) View the left file name can directly guess the role of Login.js,md5.js,client.js, Usercss.js. Since MD5 is not password encryption, look at other JS code. Fortunately, the first base64 code-sensitive discovery This code is a way of encoding, hurriedly Baidu, found Base64 is a coding method, Baidu Python Base64 code implementation, found that Python has already integrated base64 package, the use of this package to encode the password again .... Found the results and caught the postdata a hair, so write a universal program is just round the corner!!

The complete code is as follows (rugged version):

#!/usr/bin/python3#-*-coding:utf-8-*-"FileName:conNet.py Author:shenhuixiang Copyright (c) 2017,shenhuixiang" Import Base64import Requests "Enter the account password and login network parameters for if the mobile is to fill in CMCC if it is a study number then fill nuist ' user_account= ' 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 ': ' "The ' Cookie ': ' _gscu_1147341576=059821653286gq10; Sunriseusername= ' +user_account+ '; \ sunrisedomain= ' +domain_selection+ '; sunriserememberpassword=true; Sunrisepassword= ' +USER_PASSWATD+';\ Phpsessid=hb0o9bkct2f6ge164oj3vj0me5;think_language=zh-cn ', ' Connection ': ' Keep-alive ',} ' The password is base64 encoded in the parameters of the post in order to find the password encryption method ... Vomiting blood three liters. " post_data={' domain ':D omain_selection, ' enablemacauth ': ' 0 ', ' Password ': Base64.b64encode (User_passwatd.encode ()), ' Username ': User_account} #发送post请求登录网页z =requests.post (post_addr,data=post_data,headers=post_header) # JSON data Z.text to STR is therefore encoded as a byte type in the decoded Unicode type so that the Chinese s=z.text.encode (' utf-8 ') can be output normally. Decode (' Unicode-escape ') Print (s)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.