Determining if public proxies are anonymous - php

I would like to set up on my server a service that would determine if a proxy server I scraped off the net is anonymous or not. What I need is just a uri, from which the server would return the request exactly as it was received, and then to check if my public IP is in the response string(in HTTP_X_FORWARDED_FOR for example).
Has anyone has ever done this before?
Any help would be appreciated!

Why not write a simple PHP script and check this for yourself?
<?php
foreach (getallheaders() as $name => $value) {
echo "$name: $value\n";
}
?>
Save it as headers.php and call it in your browser via the proxy server. All the request headers seen by the server will be echo'd on screen.

OK, thanks to Gaurav I got it done with this simple php script (getallheaders() need PECL):
<?php
$headers = array();
foreach($_SERVER as $key => $value) {
if(strpos($key, 'HTTP_') === 0) {
$headers[str_replace(' ', '-', ucwords(str_replace('_', ' ', strtolower(substr($key, 5)))))] = $value;
echo $value;
}
}
?>
If anyone ever needs this..

Related

Unable to read the cookie set with HostOnly=true and HttpOnly=true options using PHP $_COOKIE

How do we read the cookie set with HostOnly=true and HttpOnly=true options using PHP $_COOKIE. This cookie is set via Single Sign On method.
I tried by using the below code but it is not capturing the cookie set with set with HostOnly=true and HttpOnly=true options.
Please suggest me, how I can read this type of cookie information using PHP. Thanks in advance.
PHP Code:
protected function cookieString() {
$string = '';
foreach ($_COOKIE as $k => $v) {
$k = urlencode($k);
if ($string) $string .= "; $k=$v";
else $string .= "$k=$v";
}
return $string;
}
Also I am attaching a rough figure of our application architecture below:

call the exec() php function in the web browser

I have a php function called getServerAddres() and I am trying to execute the exec() from the web browser. I understand this is not the proper way of using the function, I was just a task to exploit a web server using remote code injection. Any help on how to do remote code injection using the exec() through the web browser would be greatly appreciated.
Lets say the login in screen is: https://www.10.10.20.161/test/
function getServerAddress() {
if(isset($_SERVER["SERVER_ADDR"]))
return $_SERVER["SERVER_ADDR"];
else {
// Running CLI
if(stristr(PHP_OS, 'WIN')) {
// Rather hacky way to handle windows servers
exec('ipconfig /all', $catch);
foreach($catch as $line) {
if(eregi('IP Address', $line)) {
// Have seen exec return "multi-line" content, so another hack.
if(count($lineCount = split(':', $line)) == 1) {
list($t, $ip) = split(':', $line);
$ip = trim($ip);
} else {
$parts = explode('IP Address', $line);
$parts = explode('Subnet Mask', $parts[1]);
$parts = explode(': ', $parts[0]);
$ip = trim($parts[1]);
}
if(ip2long($ip > 0)) {
echo 'IP is '.$ip."\n";
return $ip;
} else
; // to-do: Handle this failure condition.
}
}
} else {
$ifconfig = shell_exec('/sbin/ifconfig eth0');
preg_match('/addr:([\d\.]+)/', $ifconfig, $match);
return $match[1];
}
}
}
The php script came from the login.php file.
You dont seem to understand the exec function....
First thing, read the documentation here.
This function gets executed on the server side, and thus cannot be executed on the client side.
If what you want is the information of the host machine, then you can run the command there, and output the result.
Create this file: example.php, and enter this code:
<?php
echo exec('whoami');
?>
Now, upload this file to the host, and make a request:
www.YOURHOST.smg/example.php
And read the result

PHP 5.3 getallheaders

I'm developing a web application for which it is required to capture custom header data sent by clients. In my localhost PHP 5.4 was installed and I'm using getallheaders().
But in my hosting which has PHP 5.3 installed. I can't get the header. I already tried other ways such:
foreach ($_SERVER as $name => $value)
{
if (substr($name, 0, 5) == 'HTTP_')
{
$name = str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))));
$headers[$name] = $value;
} else if ($name == "CONTENT_TYPE") {
$headers["Content-Type"] = $value;
} else if ($name == "CONTENT_LENGTH") {
$headers["Content-Length"] = $value;
}
}
and
apache_request_headers(), apache_response_headers()
Is there any other way? My hosting is using PHP 5.3 (FastCGI).
UPDATE
Well, i got where the problem came from. so first(for proofing the concept), i switch the PHP to run as Apache module, and yes as expected i able to use getallheaders().
after that i tried other rest client/debuger to send custom header(now as FastCGI), and my code is able capture the header(inside $_SERVER). So the question is, why the first REST client able to send header only if the server run as Apache Module.
I'm afraid if one of my user got the same problem while the other is fine.

PHP: Get http status code that own script just sent out via shutdown function

I have a shutdown function that checks to see if a redirect was just issued. From headers_list() I can get the headers sent and see the location header. My question is how would I figure out what http_response_code was used in the header() function. Headers list doesn't have the response code.
Example code to play around with. I don't use redirects in the example code, otherwise it would loop. Main thing is I would like to detect a 301 vs any other kind of redirect. This would be inside drupal (via drupal_goto using hook_exit); but the example code below shows the issue. I have no way of knowing what status number was passed to the browser via header().
<?php
register_shutdown_function('test');
if (mt_rand(0, 1)) {
header('X-test: junk 1', TRUE, 201);
}
else {
header('X-test: junk 0', TRUE, 202);
}
exit();
function test() {
if ($location = test_headers_contain('X-test: ')) {
// Would like to check the status code that was sent out
echo $location . '<br>';
$list = headers_list();
$txt = str_replace(' ', ' ', nl2br(htmlentities(print_r($list, TRUE))));
echo $txt;
}
}
function test_headers_contain($text) {
if (function_exists('headers_list')) {
$list = headers_list();
if (empty($list)) {
return FALSE;
}
foreach ($list as $header) {
$info = stristr($header, $text);
if ($info !== FALSE) {
return $info;
}
}
}
return FALSE;
}
?>
This code outputs this
X-test: junk 1
Array
(
[0] => X-Powered-By: PHP/5.2.10
[1] => X-test: junk 1
)
Revision 302033 added the function http_response_code in response to just the sort of issue you describe, but I'm not certain when it will be included in a release. It's not in 5.3.4. If you have access, you could build a patched version of PHP with this function added. If not, you could request it of whoever on your host does have access.

How to print all information from an HTTP request to the screen, in PHP

I need some PHP code that does a dump of all the information in an HTTP request, including headers and the contents of any information included in a POST request. Basically, a diagnostic tool that spits out exactly what I send to a server.
Does anyone have some code that does this?
To get $_GET, $_POST, $_COOKIE:
print_r($_REQUEST);
If you want the headers:
print_r(apache_request_headers());
Well, you can read the entirety of the POST body like so
echo file_get_contents( 'php://input' );
And, assuming your webserver is Apache, you can read the request headers like so
$requestHeaders = apache_request_headers();
A simple way would be:
<?php
print_r($_SERVER);
print_r($_POST);
print_r($_GET);
print_r($_FILES);
?>
A bit of massaging would be required to get everything in the order you want, and to exclude the variables you are not interested in, but should give you a start.
Nobody mentioned how to dump HTTP headers correctly under any circumstances.
From CGI specification rfc3875, section 4.1.18:
Meta-variables with names beginning with "HTTP_" contain values read
from the client request header fields, if the protocol used is HTTP.
The HTTP header field name is converted to upper case, has all
occurrences of "-" replaced with "" and has "HTTP" prepended to give
the meta-variable name.
foreach ($_SERVER as $key => $value) {
if (strpos($key, 'HTTP_') === 0) {
$chunks = explode('_', $key);
$header = '';
for ($i = 1; $y = sizeof($chunks) - 1, $i < $y; $i++) {
$header .= ucfirst(strtolower($chunks[$i])).'-';
}
$header .= ucfirst(strtolower($chunks[$i])).': '.$value;
echo $header.'<br>';
}
}
Details: http://cmyker.blogspot.com/2012/10/how-to-dump-http-headers-with-php.html
Putting together answers from Peter Bailey and Cmyker you get something like:
<?php
foreach ($_SERVER as $key => $value) {
if (strpos($key, 'HTTP_') === 0) {
$chunks = explode('_', $key);
$header = '';
for ($i = 1; $y = sizeof($chunks) - 1, $i < $y; $i++) {
$header .= ucfirst(strtolower($chunks[$i])).'-';
}
$header .= ucfirst(strtolower($chunks[$i])).': '.$value;
echo $header."\n";
}
}
$body = file_get_contents('php://input');
if ($body != '') {
print("\n$body\n\n");
}
?>
which works with the php -S built-in webserver, which is quite a handy feature of PHP.
If you want actual HTTP Headers (both request and response), give hurl.it a try.
You can use the PHP command apache_request_headers() to get the request headers and apache_response_headers() to get the current response headers. Note that response can be changed later in the PHP script as long as content has not been served.
file_get_contents('php://input') will not always work.
I have a request with in the headers content-length=735 and php://input is empty string. So depends on how good/valid the HTTP request is.
in addition, you can use get_headers(). it doesn't depend on apache..
print_r(get_headers());

Categories