How do I attack through the session file?
PHP 4 or newer versions provide support for sessions, whose primary role is to save state information between pages and page in a PHP program. For example, when a user logs on to the site, the fact that he landed and the information about who logged into the site will be saved in session, and when he is browsing the site, all of the PHP code can get these status messages.
In fact, when a session is started (which is actually set in the configuration file to start automatically on the first request), a random "Sessions ID" is generated, and if the remote browser always submits the "session ID" when the request is sent, The session will always be maintained. This is easy to implement with cookies, or it can be done by submitting a form variable (containing the session ID) on each page. PHP program can register a special variable with session, its value will be in the session file after each PHP script is finished, 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 "$session _auth" value to "Shaun", if they are modified, the future script will automatically accept the modified value, which for the stateless web is indeed a good tool, but we should also be careful.
One obvious problem is to make sure that the variables actually come from the session, for example, given the code above, if the following script is the case:
if (!empty ($session _auth)) Grant Access to Site ? > |
The code above assumes that if "$session _auth" is assigned a value from the session rather than from the user input, the attacker can gain access to the site if it is assigned a value through form input. Note the attacker must use this attack method before the session registers the variable, and any form input will be overwritten once the variable is placed in session.
Session data is generally saved in a file (the location is configurable, generally "/tmp"), and the filename is generally similar to "Sess_<session id>", which contains the variable name, variable type, variable value, and some other data. In a multihomed system, because a file is saved as a user who is running a Web server (typically nobody), a malicious site owner can gain access to other sites by creating a session file, and can even check for sensitive information in the session file.
The session mechanism also provides another convenience for attackers to save their input to a file in a remote system. For the above example, the attacker would need to place a file containing PHP code on the remote system, and if the file could not be uploaded, he would usually use the session to assign a value to a variable at his own discretion, and then guess the location of the session file, and he knew that the filename was " Php<session id> ", so just guess the directory, and the directory is generally"/tmp ".
In addition, an attacker could optionally specify "session ID" (such as "Hello") and then use this "session ID" to create a session file (such as "/tmp/sess_hello"), but "session ID" can only be a combination of letters and numbers.
How do I attack through a data type?
PHP has a relatively loose data type, and the type of the variable depends on the context in which they are located. For example, "$hello" begins with a string variable with a value of "", but when evaluated, it becomes the reshaping variable "0", which can sometimes lead to unexpected results. If the value of "$hello" is different for "000" or "0", the result returned by empty () will not be true.
The array in PHP is an associative array, that is, the index of the array is a string type. This means that "$hello [" 000 "]" and "$hello [0]" are also different.
When developing a program, you should consider the above question carefully, for example, we should not test whether a variable is "0" in one place and use empty () to verify it in another place.
How do I attack through an error-prone function? Here's a list of more detailed, error-prone functions:
<php Code Execution >
Require (): reads the contents of the specified file and interprets it as a PHP code
Include (): Ibid.
Eval (): Executes the given string as a PHP code
Preg_replace (): When used with the "/e" switch, the replacement string will be interpreted as PHP code
Enforcement of Orders
EXEC (): Executes the specified command, returning the last row of the execution result
PassThru (): Executes the specified command, returns all results to the client browser
': Executes the specified command, returns all results to an array
System (): Same as PassThru (), but does not process binary data
Popen (): Executes the specified command to connect input or output to the PHP file descriptor
Disclosure of documents
fopen (): Opens the file and corresponds to a PHP file descriptor
ReadFile (): Read the contents of the file, and then output to the client browser
File (): Read the entire contents of the file into an array
How do I enhance PHP security?
All of the attacks we described above are well implemented for the default installed PHP4, but the PHP configuration is very flexible, and by configuring some PHP options, we are likely to be able to resist some of these attacks. Below we classify some configurations according to the difficulty of implementation:
* Low Difficulty
* * Medium and low difficulty
Medium and high difficulty
High Difficulty
If you use all of the options provided by PHP, your PHP will be safe, even for Third-party code, because many of these features are already out of use.
Set "Register_globals" to "off"
This option prevents PHP from creating global variables for user input, that is, if the user submits the form variable "Hello", PHP will not create "$ hello" and will only create "http_get/post_vars[' Hello". This is one of the most important options in PHP, and turning off this option can cause a lot of inconvenience to programming.
Set "Safe_mode" to "on"
Opening this option increases the following restrictions:
1. Limit which commands can be executed
2. Limit which functions can be used
3. File access restrictions based on script ownership and target file ownership
4. Prohibit file upload features
This is a "great" option for ISPs, and it's also a great way to improve PHP security.
* * Set "Open_basedir"
This option prevents file operations outside the specified directory, effectively eliminating local files or attacks by include (), but still requires attention to file uploads and session file attacks.
* * Set "display_errors" to "off" and set "Log_errors" to "on"
This option prevents the error message from being displayed in the Web page, but is logged to the log file, which effectively resists the attacker's detection of the function in the target script.
* Set "Allow_url_fopen" to "off"
This option prevents remote file functionality.