Mar*_* AJ 6 javascript php security curl
我有一个 PHP 脚本,它通过使用打开 http 请求CURL:(如果需要,它还接受标头)
$c = curl_init();
curl_setopt($c, CURLOPT_URL, $url);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
if ($post_paramtrs) {
curl_setopt($c, CURLOPT_POST, TRUE);
curl_setopt($c, CURLOPT_POSTFIELDS, "var1=bla&" . $post_paramtrs);
}
curl_setopt($c, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($c, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; rv:33.0) Gecko/20100101 Firefox/33.0");
curl_setopt($c, CURLOPT_COOKIEJAR, $dirname . 'cookief.txt');
curl_setopt($c, CURLOPT_COOKIEFILE, $dirname . 'cookief.txt');
curl_setopt($c, CURLOPT_MAXREDIRS, 10);
$follow_allowed = (ini_get('open_basedir') || ini_get('safe_mode')) ? false : true;
if ($follow_allowed) {
curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1);
}
curl_setopt($c, CURLOPT_CONNECTTIMEOUT, 9);
curl_setopt($c, CURLOPT_REFERER, $url);
curl_setopt($c, CURLOPT_TIMEOUT, 60);
curl_setopt($c, CURLOPT_AUTOREFERER, true);
curl_setopt($c, CURLOPT_ENCODING, 'gzip,deflate');
$data = curl_exec($c);
$status = curl_getinfo($c);
curl_close($c);Run Code Online (Sandbox Code Playgroud)
它也有效。现在,我想得到linkedin 的搜索结果。这是您可以搜索的页面。如您所见,它发送了一个 ajax 请求来获取数据。例如,如果您要搜索Peter,它会发送此请求:
https://www.linkedin.com/voyager/api/typeahead/hits?q=blended&query=Peter
Run Code Online (Sandbox Code Playgroud)
但是当你手动打开它时,它会失败并抛出这个错误:
CSRF 检查失败。
这意味着我必须在请求中传递这个令牌:
我的问题是什么?我怎样才能找到那个令牌?注意到它不存在于 DOM 中。会被JS创建吗?不管怎样,你对我有什么线索吗?
搜索 API 不适用于匿名用户,因此您需要在执行此请求之前登录并获取有效的身份验证 cookie。
登录并从 cookie 中捕获令牌:li_at
最后,像这样传递 cookie 和 CSRF 令牌:
GET https://www.linkedin.com/voyager/api/typeahead/hits?q=blended&query=Peter
cookie: JSESSIONID=NotImportant;li_at={GRAB_IT_FROM_COOKIE};
csrf-token: NotImportant
Run Code Online (Sandbox Code Playgroud)
LinkedIn服务器会检查JSESSIONID和csrf-token是否相等,因此它的值并不重要。
您可以轻松添加自定义标头以CURLOPT_HTTPHEADER在curl中进行请求
| 归档时间: |
|
| 查看次数: |
1212 次 |
| 最近记录: |