_php skills of security precaution knowledge based on PHP development

Source: Internet
Author: User

PHP code security and Xss,sql injection are very useful for the security of various sites, especially UGC (User generated Content) sites, forums and e-commerce sites, often the hardest hit of XSS and SQL injection. Here is a brief introduction to some basic programming points, relative to system security, PHP security to prevent more requirements of the user input of the various parameters can be more careful.

Security in the PHP compilation process

Recommended installation of Suhosin patches, must be installed security patches
PHP.ini Security Settings

Copy Code code as follows:

Register_global = Off
MAGIC_QUOTES_GPC = Off
Display_error = Off
Log_error = On
# allow_url_fopen = Off
expose_php = Off
Open_basedir =
Safe_mode = On
Disable_function = Exec,system,passthru,shell_exec,escapeshellarg,escapeshellcmd,proc_close,proc_open,dl,popen, Show_source,get_cfg_var
Safe_mode_include_dir =

DB SQL preprocessing
Mysql_real_escape_string (many phper are still relying on addslashes to prevent SQL injection, but this approach is still problematic for Chinese coding. The problem with addslashes is that hackers can use 0xbf27 instead of single quotes, and 0xbf27 is not a legitimate character in GBK encoding, so addslashes will only 0xbf5c27 as a valid multi-byte character, where the 0xbf5c will still be considered single quotes, See this article specifically). Using the Mysql_real_escape_string function also requires that you specify the correct character set, or you may still have a problem.

Prepare + execute (PDO)
Zendframework can be used in DB class quote or Quoteinto, these two methods are implemented according to various databases without methods, not like mysql_real_escape_string only for MySQL

Processing of user input
You can use the following methods without preserving HTML tags
Strip_tags, delete all HTML tags in string
Htmlspecialchars, only escapes for "<", ">", ";", "'" characters
Htmlentities, escaping all HTML
The following tools can be considered when HTML tags must be preserved:

Copy Code code as follows:

HTML purifier:html Purifier is a standards-compliant HTML filter library written in PHP.
PHP HTML sanitizer:remove Unsafe tags and attributes from HTML code
htmlawed:php code to purify & filter HTML

Uploading files
Using the Is_uploaded_file and Move_uploaded_file functions, use the http_post_files[] array. And to prevent users from uploading PHP scripts by removing the PHP interpretation function of the uploaded directory.
The ZF framework may consider using the File_upload module
Safe handling of Session,cookie and form
Instead of relying on cookies for core validation, important information needs to be encrypted, and the form post is hashed with the transmitted data, such as the form element you are sending:

Copy Code code as follows:

<input type= "hidden" name= "h[name]" value= "<?php echo $Oname?>"/> <input type= "hidden" name= "H[age" Value= "<?php echo $Oage?>"/> <?php $sign = MD5 (' name '. $Oname. ' Age '. $Oage. $secret);?> <input type= " Hidden "name=" hash "value=" <?php echo $sign?> ""/>
Validate the parameters after post back
$str = "";
foreach ($_post[' H '] as $key => $value) {
$str. = $key. $value;
}
if ($_post[' hash ']!= MD5 ($STR. $secret)) {
echo "Hidden form data Modified"; Exit
}

PHP Security Detection Tools (XSS and SQL insertion)
Wapiti-web Application Security Auditor (Wapiti-Compact Site Vulnerability Detection Tool) (SQL INJECTION/XSS attack checking tool)

Installation/use method:
apt-get install libtidy-0.99-0 python-ctypes python-utidylib
Python wapiti.py Http://Your Website url/-M GET_XSS
Pixy:xss and Sqli Scanner for PHP (pixy-php source Defect analysis Tool)
Installation: Apt-get Install Def AULT-JDK

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.