cUrl vk Авторизация

1224
08 ноября 2017, 03:13

Прочитал 100500 страниц информации по авторизации через CURL в ВК.

Но пока безуспешно. Информация устаревшая.

<?php
$login = 'log';
$pass = 'pass';
$vk_html = connect("https://vk.com");
preg_match('/<input type="hidden" name="ip_h" value="(.+)" \/>/i', $vk_html, $ip_h);
preg_match('/<input type="hidden" name="lg_h" value="(.+)" \/>/i', $vk_html, $lg_h);
$array = [
    "act" => "login",
    "role" => "al_frame",
    "expire" => "",
    "recaptcha" => "",
    "captcha_sid" => "",
    "captcha_key" => "",
    "_origin" => "https://vk.com",
    "ip_h" => "$ip_h[0]",
    "lg_h" => "$lg_h[0]",
    "email" => "$login",
    "pass" => "$pass",
];
echo $array = http_build_query($array);
echo "<hr/>";
$otvet = connect("https://login.vk.com/?act=login", $array);
print_r($otvet);
If (!preg_match("/hash=([a-z0-9]{1,32})/", $otvet, $hash)) {
    die("Login incorrect");
}
echo $hash[1];
//$otvet = connect("http://vk.com/login.php?act=slogin&hash=" . $hash[1]);
//preg_match("/remixsid=(.*?);/", $otvet, $sid);
//$cookie = "remixchk=5; remixsid=$sid[1]";
function connect($link, $post = null)
{
    $ip = "185.76.147.151:3128";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $link);
    curl_setopt($ch, CURLOPT_PROXY, $ip); //прокси
    curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTPS);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 0);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
    curl_setopt($ch, CURLOPT_COOKIEJAR, __DIR__ . '/cookies.txt');
    curl_setopt($ch, CURLOPT_COOKIEFILE, __DIR__ . '/cookies.txt');
    if ($post !== null) {
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    }
    $otvet = curl_exec($ch);
    curl_close($ch);
    return $otvet;
} ?>

Пока после curl-а на https://login.vk.com/?act=login получаю

HTTP/1.1 200 Connection established HTTP/1.1 302 Found Server: nginx Date: Mon, 06 Nov 2017 22:42:14 GMT Content-Type: text/html; charset=windows-1251 Content-Length: 0 Connection: keep-alive X-Powered-By: PHP/3.14040 Pragma: no-cache Cache-control: no-store P3P: CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT" Location: https://vk.com/login.php?act=slogin&to=&s=0&m=4&email= Strict-Transport-Security: max-age=15768000

Соответственно я не получаю нужных данных. В случае с браузераполучаю

location:https://vk.com/login.php?act=slogin&to=&s=0&sid=6Le11B8TAAAAACHiybbHy8tMOizhM5vha8JVtP4c&dif=2&email=email

т.е. далее мне нужен этот самый sid.

Попытка работы с мобильной версией
<?php
$login = '';
$pass = '';
$vk_html = connect("https://m.vk.com");
preg_match('/ip_h=(\S*)\&/s', $vk_html, $ip_h);
preg_match('/lg_h=(\S*)\&/s', $vk_html, $lg_h);
$array = [
    "email" => "$login",
    "pass" => "$pass",
];
echo $array = http_build_query($array);
echo "<hr/>";
$otvet = connect("https://login.vk.com/?act=login&_origin=https://m.vk.com&ip_h={$ip_h[1]}&lg_h={$lg_h[1]}&role=pda&utf8=1", $array);
print_r($otvet);
If (!preg_match("/hash=([a-z0-9]{1,32})/", $otvet, $hash)) {
    die("Login incorrect");
}
echo $hash[1];
function connect($link, $post = null)
{
    $ip = "185.76.147.151:3128";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $link);
    curl_setopt($ch, CURLOPT_PROXY, $ip); //прокси
    curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTPS);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 0);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
    curl_setopt($ch, CURLOPT_COOKIEJAR, __DIR__ . '/cookies.txt');
    curl_setopt($ch, CURLOPT_COOKIEFILE, __DIR__ . '/cookies.txt');
    if ($post !== null) {
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    }
    $otvet = curl_exec($ch);
    curl_close($ch);
    return $otvet;
} ?>
Близко в финишу, но ещё нет.

Получаю капчу, вывожу её в картинку. Ввожу(пока всё это выглядит так-себе, но я только собираю информацию о том что и как работает).

Имеем

<?php
function connect($link, $post = null, $ip = "185.76.147.151:3128", $type = CURLPROXY_HTTPS, $user = false)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $link);
    curl_setopt($ch, CURLOPT_PROXY, $ip); //прокси
    curl_setopt($ch, CURLOPT_PROXYTYPE, $type);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    if ($user)
        curl_setopt($ch, CURLOPT_PROXYUSERPWD, $user);
    curl_setopt($ch, CURLOPT_TIMEOUT, 0);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
    curl_setopt($ch, CURLOPT_COOKIEJAR, __DIR__ . '/cookies.txt');
    curl_setopt($ch, CURLOPT_COOKIEFILE, __DIR__ . '/cookies.txt');
    if ($post !== null) {
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    }
    $otvet = curl_exec($ch);
    curl_close($ch);
    return $otvet;
}
function fileConnect($url, $ip = "185.76.147.151:3128", $type = CURLPROXY_HTTPS, $user = false)
{
    $ip = "185.76.147.151:3128";
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_PROXY, $ip); //прокси
    if ($user)
        curl_setopt($ch, CURLOPT_PROXYUSERPWD, $user);
    curl_setopt($ch, CURLOPT_PROXYTYPE, $type);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $img = curl_exec($ch);
    curl_close($ch);
    return $img;
}
$login = '';
$pass = '';
/*
 * WITH CAPTCHA:
 * 185.76.147.151:3128
 *
 *
 * WITHOUT:
 *
 *
 * NO WORKINK:
 * 89.175.148.162:3128
 * 82.97.201.203:8888
 *
 * BAD:
 * 37.230.112.208:7777
 *
 * */
$ip = "185.76.147.151:3128";
$type = CURLPROXY_HTTPS;
$user = false;
if (!isset($_POST['save'])) {
    $vk_html = connect("https://m.vk.com", null, $ip, $type, $user);
    preg_match('/ip_h=(\S*)\&/s', $vk_html, $ip_h);
    preg_match('/lg_h=(\S*)\&/s', $vk_html, $lg_h);
    $array = [
        "email" => "$login",
        "pass" => "$pass",
    ];
    echo $array = http_build_query($array);
    echo "<hr/>";
    $otvet = connect("https://login.vk.com/?act=login&_origin=https://m.vk.com&ip_h={$ip_h[1]}&lg_h={$lg_h[1]}&role=pda&utf8=1", $array, $ip, $type, $user);
    echo $otvet;
    echo "<hr>";
    If (!preg_match("/sid=(\S*)\&dif/s", $otvet, $sid)) {
        die("Login incorrect");
    }
    echo $sid[1];
    echo "<hr>";
    $otvet = connect("https://m.vk.com/login?role=fast&to=&s=0&sid={$sid[1]}&dif=2&email={$login}", null, $ip, $type, $user);

    /*
     * CAPTCHA = https://m.vk.com/captcha.php?s=0&sid=600885884
     * sid = $sid[1] - integers
     * */
    preg_match('/src="\/captcha.php\?(\S*)"/s', $otvet, $captcha);
    parse_str($captcha[1], $out);
    print_r($out);
    echo "<hr>";
    $im = fileConnect("https://m.vk.com/captcha.php?{$captcha[1]}");
    if ($im) {
        $im = imagecreatefromstring($im);
        imagejpeg($im, __DIR__ . "/captcha.jpg");
        echo "<img src='/captcha.jpg'>";
        ?>
        <form method="post">
            <input type="text" name="captcha" placeholder="CAPTCHA"/>
            <input type="hidden" name="login" value="<?= $login ?>"/>
            <input type="hidden" name="pass" value="<?= $pass ?>"/>
            <input type="hidden" name="ip_h" value="<?= $ip_h[1] ?>"/>
            <input type="hidden" name="lg_h" value="<?= $lg_h[1] ?>"/>
            <input type="hidden" name="sid" value="<?= $out['sid'] ?>"/>
            <hr>
            <input type="submit" name="save" value="1">
        </form>
        <?
    }
}
if (isset($_POST['save'])) {
    $login = $_POST['login'];
    $pass = $_POST['pass'];
    $ip_h = $_POST['ip_h'];
    $lg_h = $_POST['lg_h'];
    $sid = $_POST['sid'];
    $captcha = $_POST['captcha'];
    $array = [
        "email" => "$login",
        "pass" => "$pass",
        "captcha_sid" => $sid,
        "captcha_key" => $captcha
    ];
    echo $array = http_build_query($array);
    echo "<hr>";
    $otvet = connect("https://login.vk.com/?act=login&_origin=https://m.vk.com&ip_h={$ip_h}&lg_h={$lg_h}&role=pda&utf8=1", $array, $ip, $type, $user);
    echo $otvet;
    echo "<hr>";

    /*
     * FROM Location top query;
     * GET = https://m.vk.com/login?role=fast&to=&s=1&__q_hash=ce83fdc58664be754115207255e8e17b
     *
     * AND LOGIN FINISHED
     *
     * */
}
?>

Ответ

HTTP/1.1 200 Connection established HTTP/1.1 302 Found Server: nginx Date: Tue, 07 Nov 2017 00:32:55 GMT Content-Type: text/html; charset=windows-1251 Content-Length: 0 Connection: keep-alive X-Powered-By: PHP/3.14040 Pragma: no-cache Cache-control: no-store P3P: CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT" Location: https://m.vk.com/login?role=fast&to=&s=0&sid=6Le0NeLAR0NALDyNIObbHy8tMOiJhM5vh88JVtP4c&dif=2&email=123123&m=2 Strict-Transport-Security: max-age=15768000

Что значит редирект нас на шаг назад, где мы собственно и получили нашу капчу. И никак не нужный нам адресс

https://m.vk.com/login?role=fast&to=&s=1&__q_hash=ce83fdc58664be754115207255e8e17b

Почему так, пока не выяснил. Выглядит будто капча неверная.

Так как капча вылазит не на всех прокси серверах, нашёл те в которых капчи нет. Логин проходит на втором запросе!

READ ALSO
Стоит ли закэшировать меню в Wordpress?

Стоит ли закэшировать меню в Wordpress?

На сайте есть древовидное меню, которое содержит в себе три ступени: Категория А

215
Ссылка на определённую страницу

Ссылка на определённую страницу

Допустим в админке, в разделе "Страницы" создана страница со специфическим контентомИ именно на этой странице нужно подключить определённый...

209
Как изменить view пагинации в Yii2

Как изменить view пагинации в Yii2

есть дата провайдер:

291