1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > php模拟登陆正方教务管理系统(thinkPHP5.0)

php模拟登陆正方教务管理系统(thinkPHP5.0)

时间:2022-07-08 17:40:13

相关推荐

php模拟登陆正方教务管理系统(thinkPHP5.0)

如果想书写一个爬虫,首先应该通过浏览器将你登陆时,本地与服务器之间的信息传递通过抓包的方式获得,然后通过编程语言去模拟这种信息传递。

首先在浏览器上登陆正方教务系统,用Fiddler进行抓包,发现我们点击登陆首页时,首先向这个网址发送请求

但它的状态值时302,指这是个重定向请求,在向这个连接发送请求后,会由js随机生成一串字符,然后加到上面的url中

实际上我们是像这个网址发送登陆请求,这个网址返回的才是登陆界面

上图中我们可以看到Request URL中附加了一个字段,这个字段是随即的,经过测试,得出正方系统有两种登陆方式,一种是通过cookie来确认用户,另一种是通过随即字符串的形式,现在我们校网所使用的是随机字符串的形式。在第一次向default2.aspx 进行post提交时,可能是通过js产生了随即字符串并被附着在url中,验证通过之后会被重定向到xs_main.aspx页面,访问此页面通过的是get方式。

在这个请求后,系统还会向发送验证码请求,这个请求返回的是验证码图片,网上说屏蔽这个请求就可以登陆,但我们学校的网站好像不行,不输入验证码每次登陆返回的是登陆界面,所以要在php上把验证码获取,并附在post请求上模拟登陆提交到登陆的url

还有两个隐藏的input输入框

就是说登陆的时候,必须把这两个值加在post请求中,否则也是无法登陆的,这两个值也是js随机生成的,所以也需要php获取后加在post请求中。

首先是获取验证码和su并提交

getsecertcode.php

<?phpfunction curl_request($url, $post = '', $referer = ''){//$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_HEADER, 1);curl_setopt($curl, CURLOPT_REFERER, "http://jwgl./default2.aspx");curl_setopt($curl, CURLOPT_TIMEOUT, 10);curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);if ($post) {curl_setopt($curl, CURLOPT_POST, 1);curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));}if ($referer) {curl_setopt($curl, CURLOPT_REFERER, $referer);}$data = curl_exec($curl);curl_close($curl);return $data;}function getView(){$url = 'http://jwgl./default2.aspx';$result = curl_request($url);if (empty($result)) {return array('status' => "0",'message' => "模拟登陆失败,网址可能以改变",);}preg_match('/Location: \/\((.*)\)/', $result, $temp);// var_dump($temp);$pattern = '/<input type="hidden" name="__VIEWSTATE" value="(.*?)" \/>/is';preg_match_all($pattern, $result, $matches);// var_dump($matches);$res[0] = $matches[1][0];$res[1] = $temp[1];$parttern = '/<input type="hidden" name="__VIEWSTATEGENERATOR" value="(.*?)" \/>/is';preg_match_all($parttern, $result, $matches);$res[2] = $matches[1][0];if (empty($res)) {return array('status' => "0",'message' => "获取随即参数或_VIEWSTATE失败",);}return array('status' => "1",'message' => $res,);}$temp = getView()['message'];$ss = "文本框";?><?phpvar_dump($temp);$url='http://jwgl./(' . $temp[1] . ')/CheckCode.aspx' ;echo '<img src='.$url.' \/>';?><form action='/sample/moni/login' method='post'><?php echo $ss; ?>:<input type='text' name='serectCode'><input type='submit' value='提交' name='sub'><input type="hidden" name="data1" value="<?php echo $temp[0] ?>"><input type="hidden" name="data3" value="<?php echo $temp[1] ?>"><input type="hidden" name="data2" value="<?php echo $temp[2] ?>"></form>

获取所有需要的值之后,就是像登陆验证url发送post请求了,首先利用fiddler找到登陆需要的参数

其中值为空的就以空字符串代替

login()

function login(){$this->temp[0] = $_POST['serectCode'];$this->temp[1] = $_POST['data1'];$this->temp[2] = $_POST['data2'];$this->temp[3] = $_POST['data3'];var_dump($this->temp);$url = 'http://jwgl./('.$this->temp[3].')/default2.aspx';//$post['__VIEWSTATE'] = $this->temp[1];$post['__VIEWSTATEGENERATOR'] = $this->temp[2];$post['txtUserName'] = $this->sid;$post['TextBox2'] = $this->password;$post['Textbox1'] = '';$post['txtSecretCode'] = $this->temp[0];$post['lbLanguage'] = '';$post['hidPdrs'] = '';$post['hidsc'] = '';$post['RadioButtonList1'] = iconv('utf-8', 'gb2312', $this->identity);$post['Button1'] = '';//iconv('utf-8', 'gb2312', '登录');$result = $this->curl_request($url, $post);if (empty($result)) {return array('status' => "0",'message' => "模拟登陆失败",);}$url = 'http://jwgl./('.$this->temp[3].')/content.aspx ';$referer = 'http://jwgl./('.$this->temp[3].')/xs_main.aspx?xh=3166016076';$result = $this->curl_request($referer, '', $url);$result = iconv('gb2312', 'utf-8//IGNORE', $result);preg_match('/<span id=\"xhxm\">(.*)<\/span>/', $result, $name);if (empty($name)) {echo '得不到您的姓名';}var_dump($name);$this->getkebiao();}

登陆成功后,则会返回名字信息

失败则返回null

此后一段时间内在登陆只要发送一个带有随机字符串和学号的post请求即可,不用再重新输入验证码(好长时间内都可以,不过第二天好像就不行了)

xh就是学号

此时的随即字符串和上次post提交时的是相同的。这一次信息交换之后,即标志着登陆成功。登陆成功之后,如果想获得学生的个人信息和课表信息,通过再次抓包,来获得需要模拟的信息。方代码的getkebiao就是模拟获取课表

getkebiao()

function getkebiao(){$post_m['xh'] = $this->sid;$post_m['xm'] = iconv('utf-8', 'gb2312', '');$post_m['gnmkdm'] = 'N121603';$temp_info = http_build_query($post_m);$referer = 'http://jwgl./('.$this->temp[3].')/xs_main.aspx?xh=' . $this->sid;$url = 'http://jwgl./('.$this->temp[3].')/xskbcx.aspx?' . $temp_info;$result = $this->curl_request($url, '', $referer);$result = iconv('gb2312', 'utf-8', $result);if (empty($result)) {return array('status' => "0",'message' => "得到课表信息失败",);}print_r($result);$pattern = '/<td align=\"Center\" rowspan=\"2\"( width=\"7%\"){0,1}>([^<]*?)<br>([^<]*?)<br>([^<]*?)<br>([^<]*?)<br>([^<]*?)<\/td>/';preg_match_all($pattern, $result, $mm);$info_tea_cou_a = array_combine($mm[2], $mm[5]);$pattern = '/<td align=\"Center\" rowspan=\"2\"( width=\"7%\"){0,1}>([^<]*?)<br>([^<]*?)<br>([^<]*?)<br>([^<]*?)<br>([^<]*?)<br>(<br><font color=\'red\'>.*?<\/font><br>){0,1}<br>([^<]*?)<br>([^<]*?)<br>([^<]*?)<br>([^<]*?)<br>([^<]*?)(<br><br><font color=\'red\'>.*?<\/font>){0,1}<\/td>/';preg_match_all($pattern, $result, $tt);$info_tea_cou_b = array_combine($tt[2], $tt[5]);$info_tea_cou_c = array_combine($tt[8], $tt[11]);$info_temp = array_merge($info_tea_cou_a, $info_tea_cou_b, $info_tea_cou_c);if (empty($info_temp)) {return array('status' => "0",'message' => "您的课表信息为空",);}return array('status' => "1",'message' => $info_temp,);

要把上面所有的jwgl.换成自己学校的网址

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。