How to attack Common Vulnerabilities in PHP programs (II)

Source: Internet
Author: User
How to attack Common Vulnerabilities in php programs (part 2) Source: www.china4lert.org how to attack Common Vulnerabilities in PHP programs (part 2) original: ShaunClowes <; www.securereality.com. au & gt; translation: analysist & lt; http: how to attack Common Vulnerabilities in php programs (below)
Translation: analysist (analyst)
Source: http://www.china4lert.org

How to attack Common Vulnerabilities in PHP programs (II)

Original: Shaun Clowes Translation: analysist
[Library files]
As we discussed earlier, include () and require () are mainly used to support the code library, because we generally put some frequently used functions into an independent file, this independent file is the code library. when you need to use the functions, you only need to include the code library into the current file.

Initially, when people develop and publish PHP programs, in order to distinguish between the code library and the main program code, they generally set a ". inc, but they soon discovered that this is an error because such files cannot be correctly parsed as PHP code by the PHP interpreter. If we directly request such a file on the server, we will get the source code of the file, because when PHP is used as an Apache module, the PHP interpreter determines whether to parse the file into PHP code based on the file extension. The extension is specified by the site administrator, generally ". php", ". php3", and ". php4 ". If important configuration data is contained in a php file without a proper extension, remote attackers can easily obtain this information.

The simplest solution is to specify a php file extension for each file, which can prevent leakage of source code, but a new problem occurs. by requesting this file, attackers may make the code that should have been run in the context environment run independently, which may lead to all the attacks discussed above.

The following is an obvious example:

In main. php:
$ LibDir = "/libdir ";
$ LangDir = "$ libdir/languages ";

...

Include ("$ libdir/loadlanguage. php ":
?>

In libdir/loadlanguage. php:
...

Include ("$ langDir/$ userLang ");
?>

When "libdir/loadlanguage. php "is" main. php is safe to call, but because "libdir/loadlanguage" has ". therefore, remote attackers can directly request this file and specify the values of "$ langDir" and "$ userLang.
[Session file]
PHP 4 or the latest version provides support for sessions. its main function is to save the status information between pages in a PHP program. For example, when a user logs on to the website, the fact that he logs on to the website and who logs on to the website are stored in the session. when he browses around the website, all PHP code can obtain the status information.

In fact, when a session is started (in fact, it is set to automatically start at the first request in the configuration file), a random "session id" is generated ", if the remote browser always submits this "session id" when sending requests, the session will remain. This is easily achieved through cookies, or by submitting a form variable (including the "session id") on each page. A php program can use session to register a special variable. its value will exist in the session file after each PHP script ends, and will be loaded into the variable before each PHP script starts. The following is a simple example:

Session_destroy (); // Kill any data currently in the session
$ Session_auth = "shaun ";
Session_register ("session_auth"); // Register $ session_auth as a session variable
?>

The new version of PHP will automatically set the value of "$ session_auth" to "shaun". if they are modified, the modified values will be automatically accepted in future scripts, this is indeed a good tool for stateless Web, but we should be careful.

An obvious problem is to ensure that the variables do come from the session. for example, if the above code is given, if the subsequent script is as follows:

If (! Empty ($ session_auth ))
// Grant access to site here
?>

The above code assumes that if "$ session_auth" is set from the session rather than from the user input, if the attacker uses form input to set the bit, you can obtain access to the site. Note that the attacker must use this attack method before the session registers the variable. Once the variable is put into the session, it will overwrite any form input.

Session data is generally stored in files (the location is configurable, generally "/tmp"), and the file name is generally similar to "sess _ This file contains the variable name, variable type, variable value, and some other data. In a multi-host system, files are saved as users running Web servers (generally nobody, therefore, malicious site owners can create a session file to obtain access to other sites, and even check the sensitive information in the session file.

The Session mechanism also provides another convenient place for attackers to store their input in remote system files. for the above example, an attacker needs to place a file containing PHP code in a remote system. if the file cannot be uploaded, the attacker usually uses session to assign a value to a variable as needed, then I guess the location of the session file, and he knows that the file name is "php ", So you only need to guess the directory, and the directory is generally"/tmp ".

In addition, attackers can specify the "session id" (such as "hello") at will, and then use this "session id" to create a session file (such as "/tmp/sess_hello "), however, the "session id" can only be a combination of letters and numbers.

[Data type]
PHP has loose data types, and variable types depend on their context. For example, "$ hello" is a string variable and its value is "". However, when the value is evaluated, it becomes the integer variable "0 ", this may sometimes lead to unexpected results. If the value of "$ hello" is "000" or "0" is different, the results returned by empty () will not be true.

Arrays in PHP are associated arrays, that is, the index of arrays is string type. This means that "$ hello [" 000 "]" and "$ hello [0]" are different.

When developing a program, we should carefully consider the above issues. for example, we should not test whether a variable is "0" in one place, and use empty () in another place () to verify.

[Error-prone functions]
When analyzing vulnerabilities in PHP programs, if we can obtain the source code, we need a list of error-prone functions. If we can remotely change the parameters of these functions, we may find the vulnerabilities. The following is a detailed list of error-prone functions:


Require (): read the content of the specified file and interpret it as PHP code.
Include (): Same as above
Eval (): Run the given string as PHP code.
PReg_replace (): when used with the "/e" switch, the replacement string is interpreted as PHP code.

<命令执行>
Exec (): execute the specified command and return the last line of the execution result.
Passthru (): Run the specified command to return all results to the client browser.
'': Run the specified command to return all results to an array.
System (): Same as passthru (), but does not process binary data
Popen (): Run the specified command to connect the input or output to the PHP file descriptor.

<文件泄露>
Fopen (): open the file and correspond to a php file descriptor.
Readfile (): read the content of the file and output it to the client browser.
File (): Read the entire file into an array.

Note: In fact, this list is not very complete. for example, commands such as "mail ()" may also execute commands, so you need to add it yourself.
[How to Enhance PHP Security]
All the attacks I introduced above can be well implemented for PHP 4 installed by default, but I have already repeated many times. The PHP configuration is very flexible. By configuring some PHP options, we are likely to resist some of these attacks. I will classify some configurations according to the implementation difficulty:

* Low difficulty
** Medium and low difficulty
* ** Medium difficulty
* *** Difficult

The above classification is my opinion, but I can ensure that if you use all the options provided by PHP, your PHP will be safe, this is true even for third-party code, because many of these functions are no longer usable.

* *** Set "register_globals" to "off"
This option will disable PHP from creating global variables for user input. that is to say, if the user submits the form variable "hello", PHP will not create "$ hello ", instead, only "HTTP_GET/POST_VARS ['hello']" will be created. This is an extremely important option in PHP. disabling this option will cause great inconvenience to programming.

* ** Set "safe_mode" to "on"
When this option is enabled, the following restrictions are added:
1. restrict which command can be executed
2. restrict which function can be used
3. file access restrictions based on script ownership and target file ownership
4. Disable file Upload
This is a great option for ISP, and it can greatly improve PHP Security.

** Set "open_basedir"
This option can prevent file operations outside the specified directory, effectively eliminating attacks on local files or remote files by include (). However, you still need to pay attention to file upload and session file attacks.

** Set "display_errors" to "off" and "log_errors" to "on"
This option prohibits the display of error information on the webpage, but records it into the log file, which effectively prevents attackers from detecting functions in the target script.

* Set "allow_url_fopen" to "off"
This option can disable the remote file function, which is highly recommended!

Now, the article is complete. For more information, see http://www.securereality.com.au/studyinscarlet.txt.

<全文完>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.