<?php
if (!isset($test_ua))
$test_ua = "curl";
/*
* Clean all cookies
*/
function clean_cookies()
{
global $_testns;
$path = __DIR__ . '/jars';
if (!file_exists($path))
return;
rg_log('Cleaning cookies...');
$cookie_jar = $path . '/' . $_testns;
@unlink($cookie_jar);
}
/*
* This is called at the begining of all tests
*/
function prepare_http()
{
clean_cookies();
}
/*
* Data is an array
*/
$http_handles = array();
function do_req($url, &$data, &$headers)
{
global $test_ua, $test_referer;
global $cookie_jar;
global $http_handles;
global $http_client;
global $_testns;
if (!isset($http_client))
$http_client = $_testns;
$path = __DIR__ . '/jars';
if (!file_exists($path))
mkdir($path);
$cookie_jar = $path . '/' . $http_client;
if (!is_array($data))
$data = array();
if (!is_array($headers)) {
rg_log("Headers is not an array, reset it.");
$headers = array();
}
if (!strstr($url, '?'))
$url .= '?rg_debug=1';
else
$url .= '&rg_debug=1';
$url .= '&tid=' . rg_id(10); // to easy identify requests in the logs
rg_log_ml("do_req url[$url] data=" . print_r($data, TRUE)
. "headers=" . print_r($headers, TRUE));
$c = FALSE;
if (isset($http_handles[$http_client]))
$c = $http_handles[$http_client];
if ($c === FALSE) {
$c = curl_init();
$http_handles[$http_client] = $c;
}
curl_setopt($c, CURLOPT_URL, $url);
if (!empty($data)) {
curl_setopt($c, CURLOPT_POST, 1);
curl_setopt($c, CURLOPT_POSTFIELDS, $data);
} else {
curl_setopt($c, CURLOPT_POST, 0);
curl_setopt($c, CURLOPT_POSTFIELDS, '');
}
curl_setopt($c, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($c, CURLOPT_HEADER, 1);
curl_setopt($c, CURLOPT_HTTPHEADER, $headers);
curl_setopt($c, CURLOPT_USERAGENT, $test_ua);
curl_setopt($c, CURLOPT_REFERER, $test_referer);
curl_setopt($c, CURLOPT_CERTINFO, TRUE);
curl_setopt($c, CURLOPT_VERBOSE, TRUE);
curl_setopt($c, CURLOPT_ENCODING , 'gzip');
curl_setopt($c, CURLOPT_COOKIEJAR, $cookie_jar);
curl_setopt($c, CURLOPT_COOKIEFILE, $cookie_jar);
// HTTP/2
if (curl_version()['features'] & CURL_VERSION_HTTP2)
curl_setopt($c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
$err = @fopen('php://temp', 'w');
if ($err !== FALSE) {
curl_setopt($c, CURLOPT_STDERR, $err);
} else {
rg_log('Cannot open stderr redirection!');
}
$r = curl_exec($c);
if ($err !== FALSE) {
rewind($err);
$xerr = @fread($err, 16 * 4096);
fclose($err);
rg_log_ml($xerr);
}
if ($r === FALSE) {
rg_log_ml("Cannot load (url=$url), data: "
. print_r($data, TRUE));
rg_log("curl error: " . curl_error($c));
return FALSE;
}
$ret = array();
$header_size = curl_getinfo($c, CURLINFO_HEADER_SIZE);
$ret['header'] = substr($r, 0, $header_size);
$ret['body'] = substr($r, $header_size);
// Check for XSS
if (stristr($ret['body'], '<xss>')) {
file_put_contents('http_xss.out', $ret['body']);
rg_log("Found <xss> token! Check http_xss.out. Not good!");
exit(1);
}
// Check with tidy
if (!empty($ret['body'])) { // we may have a redirect
// some fixes
$ret['body'] = str_replace('autocomplete="off"', '', $ret['body']);
$ret['body'] = str_replace('<xss>', '|xss|', $ret['body']);
file_put_contents("http.tidy.in", $ret['body']);
$cmd = "tidy -errors -utf8 -file http.tidy.out http.tidy.in";
$r = rg_exec($cmd, '', FALSE, FALSE, FALSE);
if ($r['ok'] != 1) {
rg_log_ml('tidy error: ' . $r['stderr']);
rg_log_ml(file_get_contents('http.tidy.out'));
exit(1);
}
}
// Check if a '@@' is present
if (strstr($ret['body'], '@@')) {
$t = explode('@@', $ret['body']);
$t = explode('@@', $t[1]);
if (!strstr($t[0], ' ')) {
rg_log_ml("We have unresolved variables: [" . $t[0] . "]!");
exit(1);
}
}
// Find cookies
$ret['cookies'] = array();
$x = preg_match_all('/Set-Cookie: (.*?)=(.*?)[;]/',
$ret['header'], $matches, PREG_SET_ORDER);
if ($x !== FALSE) {
foreach ($matches as $junk => $info) {
$k = $info[1];
$v = $info[2];
$ret['cookies'][$k] = $v;
}
}
//rg_log_ml('ret[cookies]: ' . print_r($ret['cookies'], TRUE));
$ret['sid'] = '';
if (isset($ret['cookies']['sidu']))
$ret['sid'] = $ret['cookies']['sidu'];
if (isset($ret['cookies']['sids']))
$ret['sid'] = $ret['cookies']['sids'];
$ret['tokens'] = array();
$x = preg_match_all('/ name="token" value="([a-zA-Z0-9_:]*)"/',
$ret['body'], $matches);
//rg_log_ml('DEBUG: matches: ' . print_r($matches, TRUE));
if (($x === FALSE) || (!isset($matches[1]))) {
//rg_log("CHECK: no token found");
} else {
foreach ($matches[1] as $m) {
$t = explode(':', $m);
if (!isset($t[1])) {
rg_log_ml('body: ' . print_r($ret['body'], TRUE));
rg_log_ml('matches: ' . print_r($matches[1], TRUE));
rg_log('Invalid debug token (no prefix): ' . $m);
exit(1);
}
$ret['tokens'][$t[1]] = $t[0];
}
}
rg_log_ml('DEBUG ret[tokens]: ' . print_r($ret['tokens'], TRUE));
// find logout token
$x = preg_match('/logout\?token=([a-zA-Z0-9:]*)"/', $ret['body'], $matches);
//rg_log_ml('DEBUG: matches[logout]: ' . print_r($matches, TRUE));
if (($x === FALSE) || (!isset($matches[1]))) {
$ret['tokens']['logout'] = '';
} else {
$t = explode(':', $matches[1]);
$ret['tokens']['logout'] = $t[0];
}
$x = preg_match_all('/ class="secret_token">([A-Z0-9]*)</', $ret['body'], $matches);
if (($x !== FALSE) && (isset($matches[1])) && isset($matches[1][0])) {
$ret['totp_secret'] = $matches[1][0];
rg_log('DEBUG ret[totp_secret]=' . $ret['totp_secret']);
}
@rename('http-last.out', 'http-prev.out');
file_put_contents('http-last.out', $ret['body']);
return $ret;
}
/*
* Helper function that will do the login
*/
function test_login($url, $rg_ui)
{
global $test_ua;
// First we need to load the form so we can get the token
$data = array();
$r = do_req($url . "/op/login", $data, $headers);
if ($r === FALSE) {
rg_log('Cannot load login form!');
return FALSE;
}
if (!isset($r['tokens']['login'])) {
rg_log_ml('r: ' . print_r($r, TRUE));
rg_log('Login token not returned!');
return FALSE;
}
$good_token = $r['tokens']['login'];
// Now, post login form
rg_log("Do the real login post request");
$data = array(
"doit" => 1,
"token" => $good_token,
"user" => $rg_ui['username'],
"pass" => $rg_ui['pass'],
"lock_ip" => 1
);
if (isset($rg_ui['t']))
$data['t'] = $rg_ui['t'];
$headers = array();
$r = do_req($url . "/op/login", $data, $headers);
if ($r === FALSE) {
rg_log_ml("Cannot login: " . print_r($r, TRUE));
return FALSE;
}
if (strstr($r['body'], "invalid user")) {
rg_log_ml(print_r($r, TRUE));
rg_log("Login invalid. Check above!");
return FALSE;
}
return $r;
}
/*
* Restore password aaaa for user catab
*/
function test_restore($db)
{
$salt = 'd0a41957b835fbf7bfe63b750db15108cc048259';
$pass = 'aaaa';
$pass = rg_user_pass($salt, $pass);
$sql = "UPDATE users SET salt = '$salt'"
. ", pass = '$pass'"
. ", session_time = 3600"
. " WHERE username = 'catab'";
$res = rg_sql_query($db, $sql);
if ($res == FALSE) {
rg_log("Cannot update (" . rg_sql_error() . ")!");
exit(1);
}
rg_sql_free_result($res);
rg_cache_unset('user::4::info', RG_SOCKET_NO_WAIT);
}
/*
* Set user agent
*/
function test_set_ua($s)
{
global $test_ua;
$test_ua = $s;
}
/*
* Set referer
*/
function test_set_referer($s)
{
global $test_referer;
$test_referer = $s;
}
?>