PHP:cURL w / CSRF令牌

时间:2017-02-13 21:38:13

标签: php curl csrf

N.B:在发布之前,我查看了Login with PHP curl and CSRF tokencURL CSRF TokenLogin with CURL php and CSRF token,然后查看了一些内容。

我正在创建一个系统,它具有一个功能(如果可以)来分析来自另一个网站的数据。本网站需要使用用户,密码和登录进行登录。 csrf令牌。请参阅下面的代码。

  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
  curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);

  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  $response = curl_exec($ch);
  if (curl_errno($ch)) die(curl_error($ch));

  $doc = new DOMDocument();
  $doc->loadHTML($response);
  $token = $doc->getElementsByTagName("csrf")->attributes->getNamedItem("value")->value[0];



    echo "TOKEN: {$token} ";
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
  curl_setopt($ch, CURLOPT_POST, true);

  $params = array(
    'username' => $username,
    'password' => $password,
    'csrf' => $token
  );
  curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));

  curl_exec($ch);

  if (curl_errno($ch)) print curl_error($ch);


    curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postinfo);
    $html = curl_exec($ch);
    print("<br />HTML: ".$html);
if (curl_errno($ch)) print curl_error($ch);
    curl_close($ch);

我从上述问题略微修改了脚本,但收到了这些错误:

  

警告:DOMDocument :: loadHTML():htmlParseEntityRef:期待&#39;;&#39;在   实体,行:24英寸   /home/dlip/subdomains$/eportaltest.website.com/public_html/ticket_load.php   第18行

     

警告:DOMDocument :: loadHTML():意外的结束标记:实体中的表单,   行:76英寸   /home/dlip/subdomains$/eportaltest.website.com/public_html/ticket_load.php   第18行

     

警告:DOMDocument :: loadHTML():实体中的标签导航无效,行:124   在   /home/dlip/subdomains$/eportaltest.website.com/public_html/ticket_load.php   第18行

     

警告:DOMDocument :: loadHTML():实体中的标记部分无效,行:   140英寸   /home/dlip/subdomains$/eportaltest.website.com/public_html/ticket_load.php   第18行

     

警告:DOMDocument :: loadHTML():解析属性名称时出错   实体,行:176英寸   /home/dlip/subdomains$/eportaltest.website.com/public_html/ticket_load.php   第18行

按照上述问题的说明,我也尝试过使用

$doc->loadHTML(htmlentities($response))

无济于事。

有没有人有什么线索出错?我理解这可能与网站有关,但我该如何解决这个问题呢?

谢谢:)

1 个答案:

答案 0 :(得分:0)

有点肮脏的答案,但它的作用对我来说没问题......

使用此答案:https://stackoverflow.com/a/21804537/7508539

像这样滚动它:

  $token = trim(get_string_between($response,'<input type="hidden" name="csrf" value="','" />'));

现在可能不得不在他们的网站上爆炸我需要的位,但至少我知道如何解决它而不是毫无目标地盯着我无法解决的错误。

有没有'非黑客'方法来解决这个问题?

相关问题