PHP带验证码模拟登陆正方教务管理系统 抓取课程表

发布于 / PHP / Comments Off on PHP带验证码模拟登陆正方教务管理系统 抓取课程表

    网上有大量PHP模拟登陆正方教务管理系统的方法。很多方法是行不通的,因为这些方法利用了避免调用验证码,从而使正方系统的session中不存在验证码,从而可以绕过验证码登录。不过正方在去年就修复了这个漏洞,所以,不通过验证码登录正方教务管理系统已经基本上是不可能的了。

    下面这个程序是通过先请求验证码,获得header中set-cookie头的session对应的cookie值,再将cookie与验证码同时传入登录接口,进行登陆。动手能力强的人现在可以试着做一做了,基础不好或者懒得写的可以继续往下看。

    获取验证码的php程序源码:

<?php
error_reporting(0);    //先把错误屏蔽了,不然图片无法显示出来
session_start();    //开启session,之后用来存入获取到的cookies
$url="http://210.31.122.189/CheckCode.aspx";    //这个换成教务系统的地址
function curl_request($url,$post='',$cookie='', $returnCookie=0){
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)');
            curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
            curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
            curl_setopt($curl, CURLOPT_REFERER, "http://210.31.122.189/CheckCode.aspx"); //填写教务系统url
            if($post) {
                curl_setopt($curl, CURLOPT_POST, 1);
                curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));
            }
            if($cookie) {
                curl_setopt($curl, CURLOPT_COOKIE, $cookie);
            }
            curl_setopt($curl, CURLOPT_HEADER, $returnCookie);
            curl_setopt($curl, CURLOPT_TIMEOUT, 20);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            $data = curl_exec($curl);
            if (curl_errno($curl)) {
                return curl_error($curl);
            }
            curl_close($curl);
            if($returnCookie){
                list($header, $body) = explode("\r\n\r\n", $data, 2);
                preg_match_all("/Set\-Cookie:([^;]*);/", $header, $matches);
                $info['cookie']  = substr($matches[1][0], 1);
                $info['content'] = $body;
                return $info;
            }else{
                return $data;
            }
    }
$arr=curl_request($url,'','', 1);
$_SESSION["ck"]=$arr["cookie"];    //存入cookies
echo $arr["content"];    //输出图片

[/hide]

 模拟登陆并抓取课程表转化为json信息的php源码:

[hide]

<?php
error_reporting(0);
$ck=$_SESSION['ck'];
function curl_request($url,$post='',$cookie='', $returnCookie=0){
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)');
            curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
            curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
            curl_setopt($curl, CURLOPT_REFERER, "http://210.31.122.189/default2.aspx"); //填写教务系统url
            if($post) {
                curl_setopt($curl, CURLOPT_POST, 1);
                curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));
            }
            if($cookie) {
                curl_setopt($curl, CURLOPT_COOKIE, $cookie);
            }
            curl_setopt($curl, CURLOPT_HEADER, $returnCookie);
            curl_setopt($curl, CURLOPT_TIMEOUT, 20);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            $data = curl_exec($curl);
            if (curl_errno($curl)) {
                return curl_error($curl);
            }
            curl_close($curl);
            if($returnCookie){
                list($header, $body) = explode("\r\n\r\n", $data, 2);
                preg_match_all("/Set\-Cookie:([^;]*);/", $header, $matches);
                $info['cookie']  = substr($matches[1][0], 1);
                $info['content'] = $body;
                return $info;
            }else{
                return $data;
            }
    }
  
function getView(){
    $url = 'http://210.31.122.189/default2.aspx';
    $result = curl_request($url);
    $pattern = '/<input type="hidden" name="__VIEWSTATE" value="(.*?)" \/>/is';
    preg_match_all($pattern, $result, $matches);
    $res[0] = $matches[1][0];
          
    return $res[0] ;
}

function login($xh,$pwd,$cd,$ck){
        $url = 'http://210.31.122.189/default2.aspx';
        $post['__VIEWSTATE'] = getView();
        $post['txtUserName'] = $xh; //填写学号
        $post['TextBox2'] = $pwd;  //填写密码
        $post['txtSecretCode'] = $cd;
        $post['lbLanguage'] = '';
        $post['hidPdrs'] = '';
        $post['hidsc'] = '';
        $post['RadioButtonList1'] = iconv('utf-8', 'gb2312', '学生');
        $post['Button1'] = iconv('utf-8', 'gb2312', '登录');
        $result = curl_request($url,$post,$ck, 1);
        return $result['cookie'];
    }

function converttoTable($table){
        $list = array(
          'sun' => array(
            '1,2' => '',
            '3,4' => '',
            '5,6' => '',
            '7,8' => '',
            '9,10' => ''
          ),
          'mon' => array(
            '1,2' => '',
            '3,4' => '',
            '5,6' => '',
            '7,8' => '',
            '9,10' => ''
          ),
          'tues' => array(
            '1,2' => '',
            '3,4' => '',
            '5,6' => '',
            '7,8' => '',
            '9,10' => ''
          ),
          'wed' => array(
            '1,2' => '',
            '3,4' => '',
            '5,6' => '',
            '7,8' => '',
            '9,10' => ''
          ),
          'thur' => array(
            '1,2' => '',
            '3,4' => '',
            '5,6' => '',
            '7,8' => '',
            '9,10' => ''
          ),
          'fri' => array(
            '1,2' => '',
            '3,4' => '',
            '5,6' => '',
            '7,8' => '',
            '9,10' => ''
          ),
          'sat' => array(
            '1,2' => '',
            '3,4' => '',
            '5,6' => '',
            '7,8' => '',
            '9,10' => ''
          )
        );
        $week = array("sun"=>"周日","mon"=>"周一","tues"=>"周二","wed"=>"周三","thur"=>"周四","fri"=>"周五","sat"=>"周六");
        $order = array('1,2','3,4','5,6','7,8','9,10');
        foreach ($table as $key => $value) {
          $class = $value;
          foreach ($week as $key => $weekDay) {
            $pos = strpos($class,$weekDay);
            // echo $pos;
            if ($pos) {
              $weekArrayDay = $key; //获取list数组中的第一维key 
              foreach ($order as $key => $orderClass) {
                $pos = strpos($class,$orderClass);
                if ($pos) {
                  $weekArrayOrder = $orderClass; //获取该课程是第几节
                  break;
                }
              }
              break;
            }
          }
          $list[$weekArrayDay][$weekArrayOrder] = $class;
        }
        return $list;
      }
  
function classresult($xh,$cookie){
      date_default_timezone_set("PRC"); //时区设置
      $classList = "";//声明课表变量
      $view = 1;

      //如果密码正确
        if (!empty($view)) {
          $url = "http://210.31.122.189/xskbcx.aspx?xh={$xh}";
          $result = curl_request($url,'',$cookie);  //保存的cookies
      preg_match_all('/<table id="Table1"[\w\W]*?>([\w\W]*?)<\/table>/',$result,$out);
          $table = $out[0][0]; //获取整个课表

          preg_match_all('/<td [\w\W]*?>([\w\W]*?)<\/td>/',$table,$out);
          $td = $out[1];
          $length = count($td);

        //获得课程列表
        for ($i=0; $i < $length; $i++) { 
          $td[$i] = str_replace("<br>", "", $td[$i]);

          $reg = "/{(.*)}/";
        
          if (!preg_match_all($reg, $td[$i], $matches)) {
            unset($td[$i]);
          }
      }

      $td = array_values($td); //将课程列表数组重新索引
      $tdLength = count($td);
      for ($i=0; $i < $tdLength; $i++) { 
        $td[$i] = iconv('GB2312','UTF-8',$td[$i]);
      }
          
      //调用函数
      return converttoTable($td);
        }else{
          return 0;
        }
  }
  
login("学号","密码",验证码,$ck);
echo json_encode(classresult("学号",$ck));
转载原创文章请注明,转载自: 斐斐のBlog » PHP带验证码模拟登陆正方教务管理系统 抓取课程表
评论已关闭