curl php 模拟来源_PHP cURL模拟登录与采集分析过程详解

一、如何分析浏览器信息

(一)工具Firebug:Firefox浏览器扩展

Firefox浏览器自带了开发者工具栏,但是,Firebug的界面会更简洁、更好用一些,下面的分析过程,我们将以Firebug为示例讲解。

Tamper:Firefox浏览器扩展

Tamper是用来记录所有请求信息的扩展,主要用于协助分析。

(二)分析过程

1. 访问目标网站

2. 打开Firebug(快捷键:F12)新安装的需要启用【控制台】、【网络】、【脚本】、【Cookie】

3. 清除【Cookie】

4. 重新访问目标网站

5. 设置【网络】为[保持]状态[保持]状态可以保证页面提交表单刷新后,网络请求依然存在

如果是跨域名跳转,可能导致Firebug控制台消失,此时可以借助Tamper获取请求信息,或者在页面跳转前,快速多次按下Esc按钮(左上角),取消网络的跳转

6. 填写表单,提交登录请求

7. 利用【网络】,分析提交信息【网络】标签下有很多子标签,前面我们已经使用了[保持]标签

如果选用[全部],则会发现有很多图片、css、js的请求,这些都是干扰我们的信息,所以,一般我们只需要关注[HTML]和[XHR]标签即可

[HTML]是页面请求信息

[XHR]是Ajax请求信息

如果重复测试获取信息,[保持]状态下会导致控制台保留前面的请求信息,这时候我们可以通过[清除]标签来清空,再重新进行测试

8. 复制请求的cURL命令

9. 分析命令传输的参数与Cookie和前面页面响应内容的关联性例如:csrf_token是登录常见的传递参数,它需要从上一个页面中解析得到

10. 如果遇到Cookie和响应内容都无法查找到的参数,Ctrl+S保存当前页面为全部,利用文本搜索该参数的位置

Ctrl+S保存当前页面为全部,会得到一个html文件和一个同名的_files目录,移动html文件到目录中,然后进入目录,利用grep或者notepad++(编辑器),进行内容搜索。

如果该参数来源于js函数,需要分析函数的复杂度,能直接翻译成php语言实现自然最好,如果不能,将该js文件中的内容,抽取到nodeServer.js中,利用node来执行js并得到结果。关于nodeServer.js的应用,请参考《基于nodejs实现js后端化处理》

11. 利用cURL命令组装模拟登录程序

复制从前面得到的cURL命令,参考《PHP基于cURL实现自动模拟登录》逐步拼装请求

二、如何利用浏览器cURL命令实现模拟登录

示例1:如何登录知乎

/**

* 模拟登录知乎

* @author Zjmainstay

* @website http://www.zjmainstay.cn/php-curl

* @date 2016/10/14

*/

require_once __DIR__.'/../vendor/autoload.php';

//知乎模拟登录

//账号信息

$email='user_email@email.com';

$password='password';

$autologin=newPHPCurl\CurlAutoLogin();

//登录缓存cookie文件

$loginCookieFile='/tmp/login_cookie.txt';

if(file_exists($loginCookieFile)){

$cookie=file_get_contents($loginCookieFile);

}

//登录信息缓存,知乎频繁登录会出现验证码!!

if(empty($cookie)){

$curl1="curl 'https://www.zhihu.com/' -H 'Host: www.zhihu.com' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:47.0) Gecko/20100101 Firefox/47.0' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' -H 'Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3' -H 'Accept-Encoding: gzip, deflate, br' -H 'Connection: keep-alive'";

$content=$autologin->execCurl($curl1);

//偶然发现csrf没有用,所以直接赋值了useless

$email=urlencode($email);

$curl2="curl 'https://www.zhihu.com/login/email' -H 'Accept: */*' -H 'Accept-Encoding: gzip, deflate, br' -H 'Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3' -H 'Connection: keep-alive' -H 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8' -H 'Cookie: q_c1=0daf021c52de4ef3b6a529ff4f1a6811|1470651590000|1470651590000; _xsrf=4e599680f57fee89a4dd1aebe0e38128; l_cap_id=\"NzFlNjRmZGE3ODJhNDJkMGI1NDc2YzQxYjliZDkwZTE=|1470651590|9d2ff3f78f14a0e32c7938f1d86023736b25a5bd\"; cap_id=\"OWFjNDkzYWYxNmZhNDBjN2JlZWJjNDhiNGUxYTJjNWI=|1470651590|652577038670a9b544e91d6cbcb5a355dfc29eee\"; n_c=1; d_c0=\"ABCAsUV-WgqPTnNCtsTsgzk-RdfrxZVFb8g=|1470651591\"; __utma=51854390.248018333.1470651592.1470651592.1470651592.1; __utmb=51854390.2.10.1470651592; __utmc=51854390; __utmz=51854390.1470651592.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utmv=51854390.000--|3=entry_date=20160808=1; __utmt=1; _za=ee1a9bde-d629-4f05-b670-7adb1ee17a5f; _zap=64a770d4-5a33-4744-9602-4608f0f6f551' -H 'Host: www.zhihu.com' -H 'Referer: https://www.zhihu.com/' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:47.0) Gecko/20100101 Firefox/47.0' -H 'X-Requested-With: XMLHttpRequest' -H 'X-Xsrftoken: 4e599680f57fee89a4dd1aebe0e38128' --data '_xsrf=useless&password={$password}&captcha_type=cn&remember_me=true&email={$email}'";

$content=$autologin->execCurl($curl2);

$autologin->lockLastCookieFile();

$homeUrl='https://www.zhihu.com/';

$isLogin=$autologin->assertContainStr($autologin->getUrl($homeUrl),'退出');

if($isLogin){

echo"已登录";

}else{

exit('未登录,请检查账号密码');

}

file_put_contents($loginCookieFile,$autologin->getLastCookieContent());

}

$autologin->setLastCookieFile($loginCookieFile);

//获取我的收藏(未做登录缓存过期判断处理)

$url='https://www.zhihu.com/collections';

$content=$autologin->getUrl($url);

$pattern='#

]*?zm-item-title[^>]*?>\s*(.*?)#is';

if( preg_match_all($pattern, $content, $matches) ) {

foreach($matches[0] as $key => $val) {

echo "{$matches[2][$key]}({$matches[1][$key]})\n";

}

}

示例2:如何登录博客园

(1)PHP程序部分

/**

* 模拟登录博客园

* @author Zjmainstay

* @website http://www.zjmainstay.cn/php-curl

* @date 2016/10/14

*/

require_once __DIR__.'/../../vendor/autoload.php';

$autologin=newPHPCurl\CurlAutoLogin();

//登录缓存cookie文件

$loginCookieFile='/tmp/cnblog_login_cookie.txt';

if(file_exists($loginCookieFile)){

$cookie=file_get_contents($loginCookieFile);

}

$username='Zjmainstay';

$password='password';

//登录信息缓存,频繁登录会出现验证码!!

if(empty($cookie)){

//登录页

$curl1="curl 'https://passport.cnblogs.com/user/signin?ReturnUrl=http%3A%2F%2Fwww.cnblogs.com%2F' -H 'Host: passport.cnblogs.com' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:48.0) Gecko/20100101 Firefox/48.0' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' -H 'Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3' -H 'Accept-Encoding: gzip, deflate, br' -H 'Referer: http://www.cnblogs.com/' -H 'Connection: keep-alive' -H 'Upgrade-Insecure-Requests: 1'";

$content=$autologin->execCurl($curl1);

//获取VerificationToken

$pattern="#'VerificationToken': '([^']+)'#is";

if(!preg_match($pattern,$content,$match)){

thrownewException('无法解析获得登录页VerificationToken');

}

$verificationToken=$match[1];

//基于node.js获取博客园登录用户名和密码加密结果

$content=file_get_contents("http://localhost:8111?input1={$username}&input2={$password}");

$encryptInfo=json_decode($content,true);

$postData=sprintf('{"input1":"%s","input2":"%s","remember":false}',$encryptInfo['input1'],$encryptInfo['input2']);

$contentLength=strlen($postData);

//提交登录信息

$curl2="curl 'https://passport.cnblogs.com/user/signin' -X POST -H 'Host: passport.cnblogs.com' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:48.0) Gecko/20100101 Firefox/48.0' -H 'Accept: application/json, text/javascript, */*; q=0.01' -H 'Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3' -H 'Accept-Encoding: gzip, deflate, br' -H 'Content-Type: application/json; charset=utf-8' -H 'VerificationToken: {$verificationToken}' -H 'X-Requested-With: XMLHttpRequest' -H 'Referer: https://passport.cnblogs.com/user/signin?ReturnUrl=http%3A%2F%2Fwww.cnblogs.com%2F&AspxAutoDetectCookieSupport=1' -H 'Content-Length: {$contentLength}' -H 'Cookie: AspxAutoDetectCookieSupport=1; SERVERID=d0849c852e6ab8cf0cebe3fa386ea513|1476439733|1476438874' -H 'Connection: keep-alive'";

//通过前置处理传入post数据内容

$content=$autologin->execCurl($curl2,function($parseCurlResult)use($postData){

$parseCurlResult['post']=$postData;

return$parseCurlResult;

});

$autologin->lockLastCookieFile();

$homeUrl='http://i.cnblogs.com/';

$isLogin=$autologin->assertContainStr($autologin->getUrl($homeUrl),'修改密码');

if($isLogin){

echo"已登录\n";

}else{

exit("未登录,请检查账号密码\n");

}

file_put_contents($loginCookieFile,$autologin->getLastCookieContent());

}

$autologin->setLastCookieFile($loginCookieFile);

//获取我的随笔

$url='https://i.cnblogs.com/';

$content=$autologin->getUrl($url);

$pattern='#

\s* ]*>(.*?)#is';

if(preg_match_all($pattern,$content,$matches)){

foreach($matches[0]as$key=>$val){

echo"{$matches[2][$key]}({$matches[1][$key]})\n";

}

}

(2)node.js加密串生成部分

由于js代码字符较多,请点击下载源码:博客园模拟登录加密内容node.js代码

未经同意禁止转载!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值