PHP/File Directory/fsockopen

Материал из Web эксперт
Версия от 07:04, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

fsockopen() function establishes a socket connection, TCP or UDP.

 
Its syntax is: int fsockopen (string host, int port [, int errnumber [, string errstring [, int timeout]]])
errnumber and errstring return error information specific to the attempt to connect to the host. 
<?
function get_the_host($host,$path) {
     $fp = fsockopen($host, 80, &$errno, &$errstr, 30);
     socket_set_blocking($fp, 1);
     fputs($fp,"GET $path HTTP/1.1\r\n");
     fputs($fp,"Host: $host\r\n\r\n");
     $x = 1;
     while($x < 10) :
          $headers = fgets($fp, 4096);
          print $headers;
          $x++;
     endwhile;
     fclose($fp);
}
get_the_host("www.wbex.ru", "/");
?>



fsockopen.php

 
<?php
   $http = fsockopen("www.example.ru",80);
   $req = "GET / HTTP/1.1\r\n";
   $req .= "Host: www.example.ru\r\n";
   $req .= "Connection: Close\r\n\r\n";
   fputs($http, $req);
   while(!feof($http))
   {
      echo fgets($http, 1024);
   }
   fclose($http);
?>



Outputting the Status Lines Returned by Web Servers

 
<html>
<head>
<title>Outputting Server Status Lines</title>
</head>
<body>
<div>
<?php
$to_check = array (
          "www.google.ru" => "/index.html",
          "www.html.ru"    => "/notthere.html",
          "www.java.ru"   => "/nohost.html"
      );
foreach ( $to_check as $host => $page ) {
  print "<p>";
  $fp = @fsockopen( "$host", 80, $errno, $errdesc, 10);
  print "Trying $host<br/>";
  if ( ! $fp ) {
    print "Couldn"t connect to $host:<br/>";
    print "Error: $errno<br/>";
    print "Desc: $errdesc<br/>";
  } else {
    print "Trying to get $page<br/>";
    fputs( $fp, "HEAD $page HTTP/1.0" );
    fputs( $fp, "Host: $host" );
    fputs( $fp, "\r\n" );
    print fgets( $fp, 1024 );
    fclose( $fp );
  }
  print "</p>\n";
}
?>
</div>
</body>
</html>



Retrieving a Web Page Using fsockopen()

 
<html>
<head>
<title>Retrieving a Web page using fsockopen()</title>
</head>
<body>
<div>
<?php
$host = "www.wbex.ru";
$page = "/index.htm";
$fp = fsockopen( "$host", 80, $errno, $errdesc );
if ( ! $fp ) {
  die ( "Couldn"t connect to $host:\nError: $errno\nDesc: $errdesc\n" );
}
$request = "GET $page HTTP/1.0\r\n";
$request .= "Host: $host\r\n";
$request .= "Referer: http://www.wbex.ru/page.html\r\n";
$request .= "User-Agent: PHP test client\r\n\r\n";
$page = array();
fputs ( $fp, $request );
while ( ! feof( $fp ) ) {
  $page[] = fgets( $fp, 1024 );
}
fclose( $fp );
print "the server returned ".(count($page))." lines!";
?>
</div>
</body>
</html>



Scan a server

 
<?php
   ini_set("max_execution_time", 120);
   $rangeStart = 0;
   $rangeStop = 1024;
   $target = "www.example.ru";
   
   $range =range($rangeStart, $rangeStop);
   echo "<p>Scan results for $target</p>";
   foreach ($range as $port) {
      $result = @fsockopen($target, $port,$errno,$errstr,1);
      if ($result) echo "<p>Socket open at port $port</p>";
   }
?>



Sockets Are Files

 
<?
    $fp = fsockopen ("data.org", 80);
    if ($fp) {
            fwrite($fp, "GET / HTTP/1.1\r\nHOST: data.org\r\n\r\n");
            while (!feof($fp)) {
                    print fread($fp,256);
            }
            fclose ($fp);
    } else {
            print "Fatal error\n";
    }
?>



SSL Streams

 
<?php
$conn = @fsockopen("tcp://www.example.ru", 80);
If (!$conn) die("Unable to connect to www.example.ru");
fwrite($conn, "GET / HTTP/1.0\r\n");
fwrite($conn, "Host: www.example.ru\r\n\r\n");
fpassthru($conn);
?>