Table of Contents
1. Save the verification code and Cookie of the login interface
2. Find the server that sent the request and the required parameters
3. Implement the function of accessing the class schedule
4. Summary
5. About CSRF
php simulated login to Zhengfang Academic Affairs System (2018)
Home Backend Development PHP Tutorial PHP implements simulated login to Zhengfang Academic Affairs System

PHP implements simulated login to Zhengfang Academic Affairs System

Mar 30, 2018 pm 01:51 PM
php

This article mainly introduces the login of Zhengfang Academic Affairs System through the curl library of PHP. Since Zhengfang Academic Affairs System may have some updates every year, this article is for 2018. It introduces some methods of simulating login to Zhengfang. I hope it can help. to everyone.


PHP implements simulated login to Zhengfang Academic Affairs System

The content is as follows
PHP implements simulated login to Zhengfang Academic Affairs System
We can see this A request, as can be seen from the name, is the verification code of the page, which is returned from the Zhengfang server when accessing the login interface. I referred to the blogs of other experts who said that the verification code can be intercepted without entering it, but I tested it several times and found that it does not work. Therefore, I feel that Zhengfang should have fixed this bug, so we should enter the verification code honestly.

It should also be noted that there is a parameter Cookie in the above picture. This cookie will be returned every time the page is visited, but this cookie will only take effect after the login is successful, so we need to save it. This cookie, because all operations we perform in the educational system will verify this cookie, which is equivalent to identity authentication, so this cookie is essential.

Below I use php to save cookies and verification codes locally.

    session_start();    $id=session_id();    $_SESSION['id']=$id;    $cookie = dirname(__FILE__) . '/cookie/'.$_SESSION['id'].'.txt'; //cookie路径,将cookie写入一个文件中

    $verify_code_url = "http://jwgl.hbpu.edu.cn/CheckCode.aspx"; //验证码地址
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $verify_code_url);
    curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie);  //保存cookie
    curl_setopt($curl, CURLOPT_HEADER, 0);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);    $img = curl_exec($curl);  //执行curl
    curl_close($curl);    $fp = fopen("verifyCode.jpg","w");  //文件名
    fwrite($fp,$img);  //写入文件
    fclose($fp);
    echo "验证码取出完成,正在休眠,15秒内请把验证码填入code.txt并保存\n";    //停止运行15秒
    sleep(15);
Copy after login
Copy after login

Students who are familiar with PHP should be able to understand the meaning of this code. First create a session, we save the cookie obtained each time in a folder, and obtain the cookie and returned verification code by accessing the domain name. To manually fill in the verification code, we create a code.txt file. After we see the verification code picture in the folder, we manually write it out in the code.txt file. After fifteen seconds, we will send a request to the Zhengfang server.

2. Find the server that sent the request and the required parameters

PHP implements simulated login to Zhengfang Academic Affairs System
You can see a POST request, in which one of the message headers we should pay attention to in the picture above is Referer The purpose of this message header is to prevent CSRF. As for CSRF, I will elaborate on it at the end. Let’s take a look at the POST parameters:
PHP implements simulated login to Zhengfang Academic Affairs System
You should be able to guess most of the parameters. I won’t go into details about the parameters with empty content. What needs to be mentioned is _VIEWSTATE and RadioButtonList1.
The first parameter is the status of the current page. This string is to verify that we are coming from the login interface. This parameter is in the source code of the login page. We extract it through regular expressions.
The second parameter is the type of button, and the content is a GBK code. My type is student.

function login_post($url,$cookie,$post){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);  //不自动输出数据,要echo才行
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);  //重要,抓取跳转后数据
        curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
        curl_setopt($ch, CURLOPT_REFERER, 'http://jwgl.hbpu.edu.cn/');  //重要,302跳转需要referer,可以在Request Headers找到
        curl_setopt($ch, CURLOPT_POSTFIELDS,$post);  //post提交数据
        $result=curl_exec($ch);
        curl_close($ch);        return $result;
    }    $xh='';//此处手动输入学号,上线后通过$_POST得到
    $pw='';//此处手动输入密码,上线后通过$_POST得到
    $code = file_get_contents("code.txt");//把验证码输入到code.txt中后通过此方法取出验证码
    $cookie = dirname(__FILE__) . '/cookie/'.$_SESSION['id'].'.txt';//取出cookie
    $url="http://jwgl.hbpu.edu.cn/default2.aspx";  //教务处地址
    $con1=login_post($url,$cookie,'');
    preg_match_all(&#39;/<input type="hidden" name="__VIEWSTATE" value="([^<>]+)" \/>/&#39;, $con1, $view); //获取__VIEWSTATE字段并存到$view数组中
    $post=array(        &#39;__VIEWSTATE&#39;=>$view[1][0],        &#39;txtUserName&#39;=>$xh,        &#39;TextBox2&#39;=>$pw,        &#39;txtSecretCode&#39;=>$code,        &#39;RadioButtonList1&#39;=>&#39;%D1%A7%C9%FA&#39;,  //“学生”的gbk编码
        &#39;Button1&#39;=>&#39;&#39;,        &#39;lbLanguage&#39;=>&#39;&#39;,        &#39;hidPdrs&#39;=>&#39;&#39;,        &#39;hidsc&#39;=>&#39;&#39;
    );    $con2=login_post($url,$cookie,http_build_query($post)); //将数组连接成字符串
Copy after login
Copy after login

At this point we can access our php file. If your content is as follows, congratulations, you have successfully logged in to Zhengfang.
PHP implements simulated login to Zhengfang Academic Affairs System

3. Implement the function of accessing the class schedule

As mentioned before, even if we successfully log in, we cannot perform the functional operations inside because each section requires different Parameters, or different request addresses, so I will introduce a function of accessing scores here.



Enter the score query section and let’s take a look at the request address and parameters.
PHP implements simulated login to Zhengfang Academic Affairs System
The parameters are as follows:
PHP implements simulated login to Zhengfang Academic Affairs System
Everyone should be able to understand these parameters. The only one is gnmkdm. The content of this parameter is a randomly generated string. Not a required parameter. The other ones have basically been mentioned before, _VEIWSTATE is the same as the method obtained before. The code is given below.

preg_match_all(&#39;/<span id="xhxm">([^<>]+)/&#39;, $con2, $xm);   //正则出的数据存到$xm数组中
    $xm[1][0]=substr($xm[1][0],0,-4);  //字符串截取,获得姓名

    //拼接所需要访问的url,我们需要获取哪个url就去拼接完整的url,此处是获取成绩的url
    //以后如果需要获取别的数据,就去官网找他的url,看需要哪些参数
    $url2="http://jwgl.hbpu.edu.cn/xscj_gc.aspx?xh=".$xh."&xm=".$xm[1][0];    $viewstate=login_post($url2,$cookie,&#39;&#39;);
    preg_match_all(&#39;/<input type="hidden" name="__VIEWSTATE" value="([^<>]+)" \/>/&#39;, $viewstate, $vs);    $state=$vs[1][0];  //$state存放一会post的__VIEWSTATE

    //每个页面都有不同的参数请求,根据不同情况来发送不同的参数
    $post=array(           &#39;__EVENTTARGET&#39;=>&#39;&#39;,           &#39;__EVENTARGUMENT&#39;=>&#39;&#39;,           &#39;__VIEWSTATE&#39;=>$state,           &#39;hidLanguage&#39;=>&#39;&#39;,           &#39;ddlXN&#39;=>&#39;2016-2017&#39;,  //当前学年
           &#39;ddlXQ&#39;=>&#39;1&#39;,  //当前学期
           &#39;ddl_kcxz&#39;=>&#39;&#39;,           &#39;Button1&#39;=>&#39;%B0%B4%D1%A7%C6%DA%B2%E9%D1%AF&#39;  //“学期成绩”的gbk编码,视情况而定
        );    $content=login_post($url2,$cookie,http_build_query($post));    echo $content;
Copy after login
Copy after login

Okay, the code is finished. If your page displays as follows, then you have succeeded.
PHP implements simulated login to Zhengfang Academic Affairs System

4. Summary

In fact, it is a little more troublesome when logging in. After entering, the two necessary parameters Cookie and _VIEWSTATE are required, and the other parameters are left alone. Look at each POST request, and then combine it according to the request format. The above code will not report an error when running. Please copy it and run it to see. But after a few years, we will see if Zhengfang will fix the loopholes. The time interval between reading other blogs before was too long, so I will write an 18-year article. If you still don’t understand anything, you can send me a private message or leave a message in the comment area. Discussions are welcome.

5. About CSRF

The so-called CSRF is cross-site forgery, which means that others steal your identity to send requests to the server. The Referer header mentioned before is to defend against this attack, which means Said, if we want to successfully log in to the Zhengfang Academic Affairs System, we must jump through the login interface page, which means that the address before the jump must be http://jwgl.hbpu.edu.cn/. When testing, everyone uses the academic administration system of their own university, because this address is the academic administration system of my university, and I can successfully log in through my student ID and password. Finally, I posted a blog about CSRF attacks, written by a big shot. Describe CSRF in detail. (Click the link below)
CSRF Attack and Defense

php simulated login to Zhengfang Academic Affairs System (2018)

This article mainly introduces the login of Zhengfang Academic Affairs System through the curl library of php. Since Zhengfang Academic Affairs The system may have some updates every year, so this article is for 2018 and introduces some methods of simulating login to Zhengfang.


PHP implements simulated login to Zhengfang Academic Affairs System
The content is as follows
PHP implements simulated login to Zhengfang Academic Affairs System
We can see such a request, As can be seen from the name, this is the verification code of the page, which is returned from the Zhengfang server when accessing the login interface. I referred to the blogs of other experts who said that the verification code can be intercepted without entering it, but I tested it several times and found that it does not work. Therefore, I feel that Zhengfang should have fixed this bug, so we should enter the verification code honestly.

It should also be noted that there is a parameter Cookie in the above picture. This cookie will be returned every time you visit the page, but this cookie will only take effect after the login is successful, so we need to save this cookie. Because all operations we perform in the educational system will verify this cookie, which is equivalent to identity authentication, this cookie is essential.



Below I use php to save cookies and verification codes locally.

    session_start();    $id=session_id();    $_SESSION[&#39;id&#39;]=$id;    $cookie = dirname(__FILE__) . &#39;/cookie/&#39;.$_SESSION[&#39;id&#39;].&#39;.txt&#39;; //cookie路径,将cookie写入一个文件中

    $verify_code_url = "http://jwgl.hbpu.edu.cn/CheckCode.aspx"; //验证码地址
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $verify_code_url);
    curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie);  //保存cookie
    curl_setopt($curl, CURLOPT_HEADER, 0);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);    $img = curl_exec($curl);  //执行curl
    curl_close($curl);    $fp = fopen("verifyCode.jpg","w");  //文件名
    fwrite($fp,$img);  //写入文件
    fclose($fp);
    echo "验证码取出完成,正在休眠,15秒内请把验证码填入code.txt并保存\n";    //停止运行15秒
    sleep(15);
Copy after login
Copy after login

Students who are familiar with PHP should be able to understand the meaning of this code. First create a session, we save the cookie obtained each time in a folder, and obtain the cookie and returned verification code by accessing the domain name. To manually fill in the verification code, we create a code.txt file. After we see the verification code picture in the folder, we manually write it out in the code.txt file. After fifteen seconds, we will send a request to the Zhengfang server.

2. Find the server that sent the request and the required parameters

PHP implements simulated login to Zhengfang Academic Affairs System
You can see a POST request, in which one of the message headers we should pay attention to in the picture above is Referer The purpose of this message header is to prevent CSRF. As for CSRF, I will elaborate on it at the end. Let’s take a look at the POST parameters:
PHP implements simulated login to Zhengfang Academic Affairs System
You should be able to guess most of the parameters. I won’t go into details about the parameters with empty content. What needs to be mentioned is _VIEWSTATE and RadioButtonList1.
The first parameter is the status of the current page. This string is to verify that we are coming from the login interface. This parameter is in the source code of the login page. We extract it through regular expressions.
The second parameter is the type of button, and the content is a GBK code. My type is student.

function login_post($url,$cookie,$post){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);  //不自动输出数据,要echo才行
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);  //重要,抓取跳转后数据
        curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
        curl_setopt($ch, CURLOPT_REFERER, &#39;http://jwgl.hbpu.edu.cn/&#39;);  //重要,302跳转需要referer,可以在Request Headers找到
        curl_setopt($ch, CURLOPT_POSTFIELDS,$post);  //post提交数据
        $result=curl_exec($ch);
        curl_close($ch);        return $result;
    }    $xh=&#39;&#39;;//此处手动输入学号,上线后通过$_POST得到
    $pw=&#39;&#39;;//此处手动输入密码,上线后通过$_POST得到
    $code = file_get_contents("code.txt");//把验证码输入到code.txt中后通过此方法取出验证码
    $cookie = dirname(__FILE__) . &#39;/cookie/&#39;.$_SESSION[&#39;id&#39;].&#39;.txt&#39;;//取出cookie
    $url="http://jwgl.hbpu.edu.cn/default2.aspx";  //教务处地址
    $con1=login_post($url,$cookie,&#39;&#39;);
    preg_match_all(&#39;/<input type="hidden" name="__VIEWSTATE" value="([^<>]+)" \/>/&#39;, $con1, $view); //获取__VIEWSTATE字段并存到$view数组中
    $post=array(        &#39;__VIEWSTATE&#39;=>$view[1][0],        &#39;txtUserName&#39;=>$xh,        &#39;TextBox2&#39;=>$pw,        &#39;txtSecretCode&#39;=>$code,        &#39;RadioButtonList1&#39;=>&#39;%D1%A7%C9%FA&#39;,  //“学生”的gbk编码
        &#39;Button1&#39;=>&#39;&#39;,        &#39;lbLanguage&#39;=>&#39;&#39;,        &#39;hidPdrs&#39;=>&#39;&#39;,        &#39;hidsc&#39;=>&#39;&#39;
    );    $con2=login_post($url,$cookie,http_build_query($post)); //将数组连接成字符串
Copy after login
Copy after login

At this point we can access our php file. If your content is as follows, congratulations, you have successfully logged in to Zhengfang.
PHP implements simulated login to Zhengfang Academic Affairs System

3. Implement the function of accessing the class schedule

As mentioned before, even if we successfully log in, we cannot perform the functional operations inside because each section requires different Parameters, or different request addresses, so I will introduce a function of accessing scores here.



Enter the score query section and let’s take a look at the request address and parameters.
PHP implements simulated login to Zhengfang Academic Affairs System
The parameters are as follows:
PHP implements simulated login to Zhengfang Academic Affairs System
Everyone should be able to understand these parameters. The only one is gnmkdm. The content of this parameter is a randomly generated string. Not a required parameter. The other ones have basically been mentioned before, _VEIWSTATE is the same as the method obtained before. The code is given below.

preg_match_all(&#39;/<span id="xhxm">([^<>]+)/&#39;, $con2, $xm);   //正则出的数据存到$xm数组中
    $xm[1][0]=substr($xm[1][0],0,-4);  //字符串截取,获得姓名

    //拼接所需要访问的url,我们需要获取哪个url就去拼接完整的url,此处是获取成绩的url
    //以后如果需要获取别的数据,就去官网找他的url,看需要哪些参数
    $url2="http://jwgl.hbpu.edu.cn/xscj_gc.aspx?xh=".$xh."&xm=".$xm[1][0];    $viewstate=login_post($url2,$cookie,&#39;&#39;);
    preg_match_all(&#39;/<input type="hidden" name="__VIEWSTATE" value="([^<>]+)" \/>/&#39;, $viewstate, $vs);    $state=$vs[1][0];  //$state存放一会post的__VIEWSTATE

    //每个页面都有不同的参数请求,根据不同情况来发送不同的参数
    $post=array(           &#39;__EVENTTARGET&#39;=>&#39;&#39;,           &#39;__EVENTARGUMENT&#39;=>&#39;&#39;,           &#39;__VIEWSTATE&#39;=>$state,           &#39;hidLanguage&#39;=>&#39;&#39;,           &#39;ddlXN&#39;=>&#39;2016-2017&#39;,  //当前学年
           &#39;ddlXQ&#39;=>&#39;1&#39;,  //当前学期
           &#39;ddl_kcxz&#39;=>&#39;&#39;,           &#39;Button1&#39;=>&#39;%B0%B4%D1%A7%C6%DA%B2%E9%D1%AF&#39;  //“学期成绩”的gbk编码,视情况而定
        );    $content=login_post($url2,$cookie,http_build_query($post));    echo $content;
Copy after login
Copy after login

Okay, the code is finished. If your page displays as follows, then you have succeeded.
PHP implements simulated login to Zhengfang Academic Affairs System

4. Summary

In fact, it is a little more troublesome when logging in. After entering, the two necessary parameters Cookie and _VIEWSTATE are required. For other parameters, just check each POST request by yourself, and then according to Just combine the request formats. The above code will not report an error when running. Please copy it and run it to see. But after a few years, we will see if Zhengfang will fix the loopholes. The time interval between reading other blogs before was too long, so I will write an 18-year article. If you still don’t understand anything, you can send me a private message or leave a message in the comment area. Discussions are welcome.

5. About CSRF

The so-called CSRF is cross-site forgery, which means that someone else steals your identity to send a request to the server. The request header Referer mentioned before is to defend against this attack. , which means that if we want to successfully log in to the Zhengfang Academic Affairs System, we must jump through the login interface page, which means that the address before the jump must be http://jwgl.hbpu.edu.cn/ . When testing, everyone uses the academic administration system of their own university, because this address is the academic administration system of my university, and I can successfully log in through my student ID and password. Finally, I posted a blog about CSRF attacks, written by a big shot. Describe CSRF in detail. (Click the link below)
CSRF attack and defense.

Related recommendations:

10 recommended articles about the educational administration system

A small program that simulates logging in to the educational administration system to calculate GPA

Use php to implement simulated login of Zhengfang Academic Affairs System

The above is the detailed content of PHP implements simulated login to Zhengfang Academic Affairs System. 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 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles