使用 curl + php 登录银行账户

Login bank account using curl + php

我正在使用 curl 加载和登录我的银行帐户,因为我需要自动执行一些任务。

在要求登录之前我已经做了一些报废,但这次我一定是遗漏了一些东西,因为我无法登录。

这是我要登录的网站

https://pi.bnpparibas.es/login/login/jsp/

它有一个虚拟键盘,所以在执行登录之前我加载它并找到我的密码编码。然后我进行第二次 curl 调用,发送 post 数据。这是我使用的代码

// 1# Show login page and get the preview info needed to login

$ch = curl_init("https://pi.bnpparibas.es/login/login/jsp/");

if(!curl_setopt($ch, CURLOPT_VERBOSE, 1))
    echo "error 0";

$curl_log = fopen("C:\wamp64\www\ccm_v2\public\bnp_info.txt", 'a');

if(!curl_setopt($ch, CURLOPT_STDERR, $curl_log))
    echo "error 1";

if(!curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true))
    echo "error 2";

if (!curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2))
    echo "error 3";

if (!curl_setopt($ch, CURLOPT_CAINFO, getcwd() . "/public/pibnpparibases.crt"))
    echo "error 4";

if(!curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1))
    echo "error 5";

if(!curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt'))
    echo "error 6";

if (!curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt'))
    echo "error 7";

if(!curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1))
    echo "error 8";


$content = curl_exec($ch);

curl_close($ch);

此时一切正常,如果我回显 $content 我可以看到我输入 https://pi.bnpparibas.es/login/login/jsp/

时看到的页面

现在我收集资料给post以后

// --> Pre login info from loaded page

require('libs/simple_html_dom.php');

$html = str_get_html($content);


$S = $html->find("input[name=S]")[0]->attr["value"];
$D = $html->find("input[name=D]")[0]->attr["value"];
$menu = $html->find("input[name=menu]")[0]->attr["value"];
$MAP_ID = $html->find("input[name=MAP_ID]")[0]->attr["value"];
$tds = $html->find("td");
$password = self::convertPassword($tds, "1111");
$action2 = $html->find("input[name=action2]")[0]->attr["value"];
$decode = $html->find("input[name=decode]")[0]->attr["value"];
$longMaxPass = $html->find("input[name=longMaxPass]")[0]->attr["value"];
$usuario = "1111";
$NavegadorUserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:53.0) Gecko/20100101 Firefox/53.0";
$NavegadorIdioma = "es-ES";
$NavegadorPlataforma = "Win32";

// <-- Pre login info from loaded page

现在我执行登录

// 2# Perform login

$ch = curl_init("https://pi.bnpparibas.es/sd/?LOGUSUA=1111");

if(!curl_setopt($ch, CURLOPT_VERBOSE, 1))
    echo "error 00";

$curl_log = fopen("C:\wamp64\www\ccm_v2\public\bnp_info.txt", 'a');

if(!curl_setopt($ch, CURLOPT_STDERR, $curl_log))
    echo "error 11";

if(!curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true))
    echo "error 22";

if (!curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2))
    echo "error 33";

if (!curl_setopt($ch, CURLOPT_CAINFO, getcwd() . "/public/pibnpparibases.crt"))
    echo "error 44";

if(!curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1))
    echo "error 55";

if(!curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt'))
    echo "error 66";

if (!curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt'))
    echo "error 77";

if(!curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1))
    echo "error 88";

if (!curl_setopt($ch, CURLOPT_POST, 1))
    echo "error 99";

if(!curl_setopt($ch, CURLOPT_POSTFIELDS, urlencode('S='.$S.'&D='.$D.'&menu='.$menu.'&MAP_ID='.$MAP_ID
    .'&password='.$password.'&action2='.$action2.'&decode='.$decode.'&longMaxPass='.$longMaxPass
    .'&usuario='.$usuario.'&NavegadorUserAgent='.$NavegadorUserAgent.'&NavegadorIdioma='.$NavegadorIdioma
    .'&NavegadorPlataforma='.$NavegadorPlataforma)))
    echo "error 1010";

if(!curl_setopt($ch, CURLOPT_REFERER, "https://pi.bnpparibas.es/login/login/jsp/"))
    echo "error 1111";


$content = curl_exec($ch);

curl_close($ch);

echo $content;

银行总是将我重定向到错误页面

https://pi.bnpparibas.es/navegacion/error.jsp

我什至无法收到当您的凭据错误时显示的错误用户/密码消息。

这是cookie.txt信息

# Netscape HTTP Cookie File
# https://curl.haxx.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.

# Netscape HTTP Cookie File
# https://curl.haxx.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.

        pi.bnpparibas.es    FALSE   /   FALSE   0   PPIPROD 503359680.36895.0000
#HttpOnly_pi.bnpparibas.es  FALSE   /   FALSE   0   JSESSIONID  9D06248AF01C57E387F726F7CEDC149C
    .pi.bnpparibas.es   TRUE    /   FALSE   2483592464  COOKIE_POLICY   "01/05/2017 00:47:44"
pi.bnpparibas.es    FALSE   /navegacion/    FALSE   0   f5_cspm 1234
pi.bnpparibas.es    FALSE   /   FALSE   0   TS014f87e8  018d99cefd813ec246858c85a31844c11ce4f7c414eaed583445c7f528eddc9bb97ff2423d66f9a83fd70f66e6fdfad1d25345a55aa94be64e79c031f221139c5dd41fa358
    .pi.bnpparibas.es   TRUE    /   FALSE   0   TS01520d8b  018d99cefd07df74bddebb07c77599422ee1589bc81f104a36d7b64a9c41b1f0a8a58eb1ee31e8a0f9050e5b83d65fdab80ed6c793229bff6c17955eaa1952b249449ca6a3
pi.bnpparibas.es    FALSE   /   FALSE   0   TS014f87e8_31   01d6cc56d4a69a129dc2d1f15974d95eb216d639e13d62b538da95db1c4261855897ea3ea22052d25f884ef3de2f77a089e5e526530dfee27323a92fa147b7ea0fc4436ee0
pi.bnpparibas.es    FALSE   /   FALSE   0   TS014f87e8_28   01d6cc56d421387b13b7197bf8b5c4edb28b338ce03d62b538da95db1c4261855897ea3ea29a1569cf5b8f2c1a2b13cab36ce3e9c5
    .pi.bnpparibas.es   TRUE    /   FALSE   2483592594  idioma  ES
pi.bnpparibas.es    FALSE   /login/login/jsp/   FALSE   0   f5_cspm 1234

这里是 CURLOPT_STDERR 输出

*   Trying 213.229.172.71...
* Connected to pi.bnpparibas.es (213.229.172.71) port 443 (#0)
* Cipher selection: ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH
    * successfully set certificate verify locations:
*   CAfile: C:\wamp64\www\ccm_v2/public/pibnpparibases.crt
  CApath: none
    * SSL connection using TLSv1.2 / AES256-SHA256
    * Server certificate:
*  subject: 1.3.6.1.4.1.311.60.2.1.3=FR; businessCategory=Private Organization; serialNumber=662 042 449; C=FR; postalCode=75009; ST=Paris; L=Paris; street=16 Boulevard des Italiens; O=BNP PARIBAS SA; OU=Personal Investors; CN=pi.bnpparibas.es
        *  start date: Dec 14 00:00:00 2016 GMT
    *  expire date: Dec 19 23:59:59 2017 GMT
    *  subjectAltName: host "pi.bnpparibas.es" matched cert's "pi.bnpparibas.es"
*  issuer: C=US; O=Symantec Corporation; OU=Symantec Trust Network; CN=Symantec Class 3 EV SSL CA - G3
*  SSL certificate verify ok.
> GET /login/login/jsp/ HTTP/1.1
Host: pi.bnpparibas.es
Accept: */*
Cookie: f5_cspm=1234; COOKIE_POLICY="01/05/2017 00:47:44"; JSESSIONID=9D06248AF01C57E387F726F7CEDC149C; PPIPROD=503359680.36895.0000; TS014f87e8=018d99cefd100fdb6bee5ea7604f63eeee4c2d4013c10d01838421444d70debc87a852d80a49b1f602eccde97c7c1c94d5b156b11ea1ba7de04f2b7078da17444b3933fccb; TS014f87e8_28=01d6cc56d421387b13b7197bf8b5c4edb28b338ce03d62b538da95db1c4261855897ea3ea29a1569cf5b8f2c1a2b13cab36ce3e9c5; TS014f87e8_31=01d6cc56d4a69a129dc2d1f15974d95eb216d639e13d62b538da95db1c4261855897ea3ea22052d25f884ef3de2f77a089e5e526530dfee27323a92fa147b7ea0fc4436ee0; TS01520d8b=018d99cefd07df74bddebb07c77599422ee1589bc81f104a36d7b64a9c41b1f0a8a58eb1ee31e8a0f9050e5b83d65fdab80ed6c793229bff6c17955eaa1952b249449ca6a3; idioma=ES

< HTTP/1.1 200 OK
< X-Powered-By: ASP.NET
< Pragma: no-cache
< Expires: 0
< Cache-Control: no-cache
< Cache-Control: no-store
< Content-Type: text/html;charset=ISO-8859-1
< Date: Mon, 01 May 2017 08:36:56 GMT
* Replaced cookie f5_cspm="1234" for domain pi.bnpparibas.es, path /login/login/jsp/, expire 0
< Set-Cookie: f5_cspm=1234;
* Replaced cookie TS014f87e8="018d99cefd813ec246858c85a31844c11ce4f7c414eaed583445c7f528eddc9bb97ff2423d66f9a83fd70f66e6fdfad1d25345a55aa94be64e79c031f221139c5dd41fa358" for domain pi.bnpparibas.es, path /, expire 0
< Set-Cookie: TS014f87e8=018d99cefd813ec246858c85a31844c11ce4f7c414eaed583445c7f528eddc9bb97ff2423d66f9a83fd70f66e6fdfad1d25345a55aa94be64e79c031f221139c5dd41fa358; Path=/
* Replaced cookie TS014f87e8_31="01d6cc56d4a69a129dc2d1f15974d95eb216d639e13d62b538da95db1c4261855897ea3ea22052d25f884ef3de2f77a089e5e526530dfee27323a92fa147b7ea0fc4436ee0" for domain pi.bnpparibas.es, path /, expire 0
< Set-Cookie: TS014f87e8_31=01d6cc56d4a69a129dc2d1f15974d95eb216d639e13d62b538da95db1c4261855897ea3ea22052d25f884ef3de2f77a089e5e526530dfee27323a92fa147b7ea0fc4436ee0; Path=/
< Transfer-Encoding: chunked
< 
* Connection #0 to host pi.bnpparibas.es left intact
*   Trying 213.229.172.71...
* Connected to pi.bnpparibas.es (213.229.172.71) port 443 (#0)
* Cipher selection: ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH
* successfully set certificate verify locations:
*   CAfile: C:\wamp64\www\ccm_v2/public/pibnpparibases.crt
  CApath: none
* SSL connection using TLSv1.2 / AES256-SHA256
* Server certificate:
*  subject: 1.3.6.1.4.1.311.60.2.1.3=FR; businessCategory=Private Organization; serialNumber=662 042 449; C=FR; postalCode=75009; ST=Paris; L=Paris; street=16 Boulevard des Italiens; O=BNP PARIBAS SA; OU=Personal Investors; CN=pi.bnpparibas.es
*  start date: Dec 14 00:00:00 2016 GMT
*  expire date: Dec 19 23:59:59 2017 GMT
*  subjectAltName: host "pi.bnpparibas.es" matched cert's "pi.bnpparibas.es"
    *  issuer: C=US; O=Symantec Corporation; OU=Symantec Trust Network; CN=Symantec Class 3 EV SSL CA - G3
    *  SSL certificate verify ok.
> POST /sd/?LOGUSUA=1111 HTTP/1.1
Host: pi.bnpparibas.es
Accept: */*
Referer: https://pi.bnpparibas.es/login/login/jsp/
Cookie: COOKIE_POLICY="01/05/2017 00:47:44"; JSESSIONID=9D06248AF01C57E387F726F7CEDC149C; PPIPROD=503359680.36895.0000; TS014f87e8=018d99cefd813ec246858c85a31844c11ce4f7c414eaed583445c7f528eddc9bb97ff2423d66f9a83fd70f66e6fdfad1d25345a55aa94be64e79c031f221139c5dd41fa358; TS014f87e8_28=01d6cc56d421387b13b7197bf8b5c4edb28b338ce03d62b538da95db1c4261855897ea3ea29a1569cf5b8f2c1a2b13cab36ce3e9c5; TS014f87e8_31=01d6cc56d4a69a129dc2d1f15974d95eb216d639e13d62b538da95db1c4261855897ea3ea22052d25f884ef3de2f77a089e5e526530dfee27323a92fa147b7ea0fc4436ee0; TS01520d8b=018d99cefd07df74bddebb07c77599422ee1589bc81f104a36d7b64a9c41b1f0a8a58eb1ee31e8a0f9050e5b83d65fdab80ed6c793229bff6c17955eaa1952b249449ca6a3; idioma=ES
Content-Length: 407
Content-Type: application/x-www-form-urlencoded

* upload completely sent off: 407 out of 407 bytes
< HTTP/1.1 302 Movido tempor?lmente
< X-Powered-By: ASP.NET
< Location: https://pi.bnpparibas.es/navegacion/error.jsp
< Content-Length: 0
< Date: Mon, 01 May 2017 08:36:56 GMT
* Replaced cookie TS014f87e8="018d99cefd813ec246858c85a31844c11ce4f7c414eaed583445c7f528eddc9bb97ff2423d66f9a83fd70f66e6fdfad1d25345a55aa94be64e79c031f221139c5dd41fa358" for domain pi.bnpparibas.es, path /, expire 0
< Set-Cookie: TS014f87e8=018d99cefd813ec246858c85a31844c11ce4f7c414eaed583445c7f528eddc9bb97ff2423d66f9a83fd70f66e6fdfad1d25345a55aa94be64e79c031f221139c5dd41fa358; Path=/
< 
* Connection #0 to host pi.bnpparibas.es left intact
* Issue another request to this URL: 'https://pi.bnpparibas.es/navegacion/error.jsp'
* Switch from POST to GET
* Found bundle for host pi.bnpparibas.es: 0x2579b630640 [can pipeline]
* Re-using existing connection! (#0) with host pi.bnpparibas.es
* Connected to pi.bnpparibas.es (213.229.172.71) port 443 (#0)
> GET /navegacion/error.jsp HTTP/1.1
Host: pi.bnpparibas.es
Accept: */*
Referer: https://pi.bnpparibas.es/login/login/jsp/
Cookie: f5_cspm=1234; COOKIE_POLICY="01/05/2017 00:47:44"; JSESSIONID=9D06248AF01C57E387F726F7CEDC149C; PPIPROD=503359680.36895.0000; TS014f87e8=018d99cefd813ec246858c85a31844c11ce4f7c414eaed583445c7f528eddc9bb97ff2423d66f9a83fd70f66e6fdfad1d25345a55aa94be64e79c031f221139c5dd41fa358; TS014f87e8_28=01d6cc56d421387b13b7197bf8b5c4edb28b338ce03d62b538da95db1c4261855897ea3ea29a1569cf5b8f2c1a2b13cab36ce3e9c5; TS014f87e8_31=01d6cc56d4a69a129dc2d1f15974d95eb216d639e13d62b538da95db1c4261855897ea3ea22052d25f884ef3de2f77a089e5e526530dfee27323a92fa147b7ea0fc4436ee0; TS01520d8b=018d99cefd07df74bddebb07c77599422ee1589bc81f104a36d7b64a9c41b1f0a8a58eb1ee31e8a0f9050e5b83d65fdab80ed6c793229bff6c17955eaa1952b249449ca6a3; idioma=ES

        < HTTP/1.1 200 OK
    < X-Powered-By: ASP.NET
    < Content-Type: text/html;charset=ISO-8859-1
        < Date: Mon, 01 May 2017 08:36:56 GMT
    * Replaced cookie f5_cspm="1234" for domain pi.bnpparibas.es, path /navegacion/, expire 0
    < Set-Cookie: f5_cspm=1234;
        * Replaced cookie TS014f87e8="018d99cefd813ec246858c85a31844c11ce4f7c414eaed583445c7f528eddc9bb97ff2423d66f9a83fd70f66e6fdfad1d25345a55aa94be64e79c031f221139c5dd41fa358" for domain pi.bnpparibas.es, path /, expire 0
        < Set-Cookie: TS014f87e8=018d99cefd813ec246858c85a31844c11ce4f7c414eaed583445c7f528eddc9bb97ff2423d66f9a83fd70f66e6fdfad1d25345a55aa94be64e79c031f221139c5dd41fa358; Path=/
* Replaced cookie TS014f87e8_31="01d6cc56d4a69a129dc2d1f15974d95eb216d639e13d62b538da95db1c4261855897ea3ea22052d25f884ef3de2f77a089e5e526530dfee27323a92fa147b7ea0fc4436ee0" for domain pi.bnpparibas.es, path /, expire 0
            < Set-Cookie: TS014f87e8_31=01d6cc56d4a69a129dc2d1f15974d95eb216d639e13d62b538da95db1c4261855897ea3ea22052d25f884ef3de2f77a089e5e526530dfee27323a92fa147b7ea0fc4436ee0; Path=/
< Transfer-Encoding: chunked
                < 
* Connection #0 to host pi.bnpparibas.es left intact

希望你们比我更有经验执行这种日志记录,并且可以看到我遗漏了什么。

提前致谢

找到我丢失的东西了。我不得不调用函数 http_build_query

$fields = array("S" => $S, "D" => $D, "menu" => $menu, "MAP_ID" => $MAP_ID,
"password" => $password, "action2" => $action2, "decode" => $decode, "longMaxPass" => $longMaxPass,
"usuario" => $usuario, "NavegadorUserAgent" => $NavegadorUserAgent, "NavegadorIdioma" => $NavegadorIdioma,
"NavegadorPlataforma" => $NavegadorPlataforma);

$fields_string = http_build_query($fields);

if(!curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string))
    echo "error 1010";