0

I need to login to http://auto.vsk.ru/login.aspx making a post request to it from my site. I wrote a js ajax function that sends post request to php script on my server, that sends cross-domain request via cUrl.

post.php

<?php

function request($url,$post, $cook)
{
$ch = curl_init();
$curlConfig = array(
CURLOPT_URL            => $url,
CURLOPT_POST           => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_COOKIEFILE => $cook,
CURLOPT_COOKIEJAR => $cook,
CURLOPT_USERAGENT => '"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 10.0; Trident/7.0; Touch; .NET4.0C; .NET4.0E; Tablet PC 2.0)"',
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_REFERER => $url,
CURLOPT_POSTFIELDS     => $post,
CURLOPT_HEADER => 1,
);
curl_setopt_array($ch,$curlConfig);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}

$result = request($_POST['url'], $_POST['data'], $_POST['cook']);

if ($result === FALSE) 
 echo('error');
else
 echo($result);

?>

Js code:

function postcross(path,data,cook,run)
{
requestsp('post.php','url='+path+'&data='+data+'&cook='+cook, run);
}

function requestp(path, data, run)
{
var http = new XMLHttpRequest();
http.open('POST', path, true);

http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');

http.onreadystatechange = function() 
{  
if(http.readyState == 4 && http.status == 200)
{
    run(http);
}
}

http.send(data);
}

postcross('http://auto.vsk.ru/login.aspx',encodeURIComponent('loginandpassord'),'vskcookies.txt',function(e){
document.getElementById('container').innerText=e.responseText;
});

The html page I getting from response says two things:

  1. My browser is not Internet Explorer, I should switch to it.(actually it works from Google Chrome, at least can login).
  2. My browser doesn’t support cookies.

About the cookies it is very similar to this (veeeery long) question. File vskcookies.txt is created in my server and it is actually updates after post request call, and stores cookies.

About the IE, firstly I thought that the site checks browser from js, but it is wrong, because js doesn’t run at all - I only read html page as a plain text, and it already has that notification about IE.

So wondered what if I make cUrl request wrong? I wrote new php script that shows request headers, here is a source:

head.php

<?php

foreach (getallheaders() as $name => $value) 
{
echo "$name: $value\n";
}

?>

The result of postcross('http://mysite/head.php',encodeURIComponent('loginandpassord'),'vskcookies.txt',function(e){ document.getElementById('container').innerText=e.responseText; }):

Host: my site
User-Agent: "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 10.0; Trident/7.0; Touch; .NET4.0C; .NET4.0E; Tablet PC 2.0)"
Accept: */*
Content-Type: application/x-www-form-urlencoded
Referer: mysite/head
X-1gb-Client-Ip: my ip
X-Forwarded-For: ip, ip, ip
X-Forwarded-Port: 443
X-Forwarded-Proto: https
X-Port: 443
Accept-Encoding: gzip
X-Forwarded-URI: /head
X-Forwarded-Request: POST /head HTTP/1.1
X-Forwarded-Host: my site
X-Forwarded-Server: my site
Content-Length: 823
Connection: close

For some reason there is no Cookie: parameters, but user agent is IE as I mentioned.

Also I tried to replace head.php source with

print_r($_COOKIE);

And got empty array:

enter image description here

Am I doing something wrong, or it is site bot-protection?


Update 1

It is showing cookies only if to pass them through CURLOPT_COOKIE.

So I think I will leave CURLOPT_COOKIEFILE => $cook; as it is, and for CURLOPT_COOKIE something like file_get_contents($cook), although there is useless information. protection?


Important Update 2

Okay, probably I just stupid. Response html page indeed consists messages about IE and offed cookies, but they are in div that is display:none and are displayed on by js.

So, seems my tries fail because of another reasons.

Artur
  • 325
  • 2
  • 16
  • I'm confused about the part where the browser is expected to provide the server's local file system path for cookie jar file. That's the goal? – Álvaro González Sep 11 '20 at 14:15
  • @ÁlvaroGonzález didn’t understand You. You mean php `request` function? As I mentioned in question, the file `vskcookies.txt` in my server stores cookies after request. – Artur Sep 11 '20 at 14:30
  • `postcross('http://auto.vsk.ru/login.aspx',encodeURIComponent('loginandpassord'),'vskcookies.txt',function(e){` — Why must JavaScript send PHP server settings to PHP server? – Álvaro González Sep 11 '20 at 14:32
  • @ÁlvaroGonzález, well in future, I was planning to automatically create cookies files depending on domain name. Currently it is just for comfort. There is a problem? – Artur Sep 11 '20 at 14:36
  • You're asking the user: What file on server do you want me to wipe out? (I'm not saying that's the issue, I'm just trying to understand the code). – Álvaro González Sep 11 '20 at 14:40
  • @ÁlvaroGonzález currently it’s only for me. What do You mean by Your first sentence? The only security thing I need to add is to block domains with `file://`. – Artur Sep 11 '20 at 14:51
  • Perhaps you aren't aware of how `CURLOPT_COOKIEJAR ` works: it's a path, on **server's disc**, where PHP creates a **file** (ref.: [1](https://www.php.net/manual/en/function.curl-setopt.php), [2](https://curl.haxx.se/libcurl/c/CURLOPT_COOKIEJAR.html)). You don't normally read it from `$_POST`, just like you don't read the session directory or the temp directory. Anyway, if you don't plan to upload your code to a hosting provider I guess it's fine. – Álvaro González Sep 12 '20 at 08:47

0 Answers0