PHP Security Basics appendix B. Function

Source: Internet
Author: User

Appendix B. Functions

When I write a book.

Due to the large number of functions, it is impossible to describe them correctly and securely. Here I select the function that I think is the most important. The selection criteria include the frequency of use, the danger (Security) during use, and my own experience.

For each function listed, I will provide recommended usage methods. When proposing these methods, I will focus on security. Make adjustments based on your needs in actual use.

When a function is at the same risk as another function, I will provide the information for another function, instead of describing it again.

 

B .1. eval ()

The eval () function is used to parse and run a string using a PHP statement. For example:

<? PHP

 

$ Name = 'chris ';

 

$ String = 'echo "Hello, $ name ";';

Eval ($ string );

 

?>

 

In the above example, $ string is run as a PHP statement, so it is equivalent:

 

<? PHP

 

$ Name = 'chris ';

 

Echo "Hello, $ name ";

 

?>

 

Although eval () is very useful, it is very dangerous to use contaminated data. For example, in the following example, if $ name is contaminated, attackers can run phpCode:

 

<? PHP

 

$ Name = $ _ Get ['name'];

Eval ($ name );

 

?>

 

I recommend that you avoid using eval () when you are not sure whether contaminated data is used for strings interpreted in PHP and, if possible, avoid using eval (). This function should be checked during security review and peer review.

 

B .2. exec ()

As mentioned in chapter 6th, executing shell commands is a very dangerous operation. Using contaminated data when constructing shell commands may cause command injection vulnerabilities.

Avoid using shell command functions whenever possible, but when you need to use them, be sure to only use filtered and escaped data to construct shell commands.

 

<? PHP

 

$ Clean = array ();

$ Shell = array ();

 

/* Filter input ($ command, $ argument )*/

 

$ Shell ['command'] = escapeshellcmd ($ clean ['command']);

$ Shell ['argument'] = escapeshellarg ($ clean ['argument']);

 

$ Last = exec ("{$ shell ['command']} {$ shell ['argument']}", $ output, $ return );

 

?>

 

B .3. file ()

The file () function is one of my favorite methods for reading files. It reads each row of the file as an element of the returned array. It is particularly convenient that you do not need to provide a file handle-you provide a file name, it will do everything for you:

 

<? PHP

 

$ Contents = file ('/tmp/file.txt ');

Print_r ($ contents );

 

?>

 

If the preceding file has two rows, the output is similar to the following:

 

Array

(

[0] => This is Line one.

[1] => This is line two.

)

 

Using the file () function is not particularly dangerous, but when you use it with the allow_url_fopen option, it can read many different types of resources, such as the content of a remote website:

 

<? PHP

 

$ Contents = file ('HTTP: // example.org /');

Print_r ($ contents );

 

?>

 

The output is as follows (with strikethrough ):

 

Array

(

[0] => <HTML>

[1] =>

[2] => <title> Example Web page </title>

[3] =>

[4] => <body>

...

[11] => </body>

[12] =>

)

 

If the file () function call object name is constructed from contaminated data, its content should also be regarded as contaminated. This is because constructing a file name using contaminated data may cause you to open a remote website with malicious data. Once you save the data in a variable, the risk is greatly increased:

 

<? PHP

$ Tainted = file ($ _ post ['filename']);

?>

 

Each element in the $ tainted array has the same risk as $ _ post ['filename']-It is input and must be filtered.

Here, the behavior may be unexpected -- the misuse of $ _ post ['filename'] can change the behavior of the file () function, therefore, it can point to a remote resource instead of a local file.

 

B .4. file_get_contents ()

See "file ()."

 

B .5. fopen ()

See "file ()."

 

B .6. include

As described in Chapter 5th, include is widely used in organizational and modular software design and is very necessary. However, incorrect use of include may cause a major code injection security vulnerability.

It is necessary to use only filtered data in the include statement. This function should be checked during security review and peer review.

 

B .7. passthru ()

See "Exec ()."

 

B .8. phpinfo ()

Phpinfo () will output the page about PhP information, such as the running version number and configuration information. Since the output of phpinfo () provides a lot of information, we recommend that you restrict access to any resources that use this function.

If you use the techniques in Chapter 8 to protect database authentication information, make sure that visitors cannot see the output information generated by phpinfo, this is because it exposes the contents of the Super Global Array $ _ server.

 

B .9. popen ()

See "Exec ()."

 

B .10. preg_replace ()

Preg_replace () is used to replace strings that conform to regular expressions. In some cases, constructing a regular expression using contaminated data is very dangerous because its e modifier will treat the parameters used for replacement as PHP code During replacement. For example ):

 

<? PHP

$ STR = "abcdef ";

$ Se = "len ";

$ Reg = "/ABC/E ";

Echo preg_replace ($ Reg, "strlen (\ $ SE)", $ Str );

?>

 

The following string is output:

3def

 

When the E modifier is used, whether or not it is intended or not, it brings the same risk as eval. This function should be checked during security review and peer review.

 

B .11. proc_open ()

See "Exec ()."

 

B .12. readfile ()

See "file ()."

 

B .13. require

See "include ."

 

B .14. shell_exec ()

See "Exec ()."

 

B .15. system ()

See "Exec ()."

 

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.