1. The basic principles of PHP submission data filtering
1) When committing variables into the database, we must use Addslashes () to filter, like our injection problem, a addslashes () will be done. In fact, when it comes to variable values, the Intval () function is also a good choice for filtering strings.
2) Open MAGIC_QUOTES_GPC and Magic_quotes_runtime in php.ini. MAGIC_QUOTES_GPC can turn the quotes in the Get,post,cookie into slashes. Magic_quotes_runtime the data in and out of the database can play the role of format. In fact, as early as the previous injection is crazy, this parameter is very popular.
3) When using system functions, you must use the Escapeshellarg (), escapeshellcmd () parameters to filter, so you can rest assured that the use of system functions.
4) for cross-site, Strip_tags (), Htmlspecialchars () Two parameters are good, for the user submitted with HTML and PHP markup will be converted. For example, the angle brackets "<" will be converted to "<" such harmless characters.
The code is as follows |
|
$new = Htmlspecialchars ("Test", ent_quotes); Strip_tags ($text,); |
5) for the filtering of related functions, just like the previous include (), Unlink,fopen () and so on, as long as you specify the variables you want to perform the action or filter the relevant characters closely, I think this is also invulnerable.
2, PHP simple data filtering
1) Warehousing: Trim ($STR), Addslashes ($STR)
2) Out of stock: Stripslashes ($STR)
3) Display: Htmlspecialchars (NL2BR ($STR))
Take a look at the following example to further discuss the dispatch.php script:
The code is as follows |
|
/* Global Security processing */ Switch ($_get[' task ') { Case ' Print_form ': Include '/inc/presentation/form.inc '; Break Case ' Process_form ': $form _valid = false; Include '/inc/logic/process.inc '; if ($form _valid) { Include '/inc/presentation/end.inc '; } Else { Include '/inc/presentation/form.inc '; } Break Default Include '/inc/presentation/index.inc '; Break } ?> |
If this is the only publicly accessible PHP script, you can be sure that the program is designed to ensure that the initial global security processing cannot be bypassed. It also makes it easy for developers to see the flow of control for specific tasks. For example, it is easy to know that you do not need to browse the entire code: When $form_valid is true, End.inc is the only one that is displayed to the user; Since it was included before Process.inc and was just initialized to false, it can be determined that the internal logic of PROCESS.INC will set it to true, otherwise the form will be displayed again (possibly with related error messages).
Attention
If you use directory-directed files, such as index.php (instead of dispatch.php), you can use URL addresses like this: Http://example.org/?task=print_form.
You can also use Apacheforcetype redirection or mod_rewrite to adjust the URL address: Http://example.org/app/print-form.
Include method
Another way is to use a single module, which is responsible for all the security processing. This module is included in the front (or very top) of all public PHP scripts. Refer to the following script Security.inc
The code is as follows |
|
Switch ($_post[' form ') { Case ' login ': $allowed = Array (); $allowed [] = ' form '; $allowed [] = ' username '; $allowed [] = ' password '; $sent = Array_keys ($_post); if ($allowed = = $sent) { Include '/inc/logic/process.inc '; } Break } ?> |
In this example, each submitted form considers that it should contain the unique validation value of form, and security.inc independently processes 0 of the data that needs to be filtered in the form. The HTML form that implements this requirement is as follows:
An array called $allowed is used to verify which form variables are allowed, and this list should be consistent before the form is processed. Process Control determines what to do, and Process.inc is where the real filtered data arrives.
Attention
A good way to make sure that Security.inc is always included in the first place of each script is to use the Auto_prepend_file setting.
Examples of filtering
Establishing a whitelist is very important for data filtering. Because it is not possible to give an example of each of the form data that you might encounter, some examples can help you get a general idea of this.
The following code validates the e-mail address:
The code is as follows |
|
$clean = Array (); $email _pattern = '/^[^@s<&>]+@ ([-a-z0-9]+.) +[a-z]{2,}$/i '; if (Preg_match ($email _pattern, $_post[' email ')) { $clean [' email '] = $_post[' email ']; } ?> |
The following code ensures that the content of $_post[' color ' is Red,green, or blue:
The code is as follows |
|
$clean = Array (); Switch ($_post[' color ') { Case ' Red ': Case ' green ': Case ' Blue ': $clean [' color '] = $_post[' color ']; Break } ?> |
The following code ensures that $_post[' num ' is an integer:
code as follows |
|
$clean = Array (); if ($_post[' num ') = = Strval (intval ($_post[' num '))) { $clean [' num '] = $_post[' num ']; } ?> |
The following code ensures that $_post[' num ' is a floating-point number (float):
code as follows |
|
$clean = Array (); if ($_post[' num ') = = Strval (floatval ($_post[' num '))) { $clean [' num '] = $_post[' num ']; } ?> |
Name Conversion
Each of the previous examples used array $clean. This is a good habit for developers to judge whether data is potentially a threat. Never save it in $_post or $_get after validating the data, as developers should always be fully skeptical of the data stored in the Super Global array.
What needs to be added is that the use of $clean can help to think about what else is not being filtered, which is more like a whitelist role. The level of security can be increased.
If you only save the validated data in $clean, the only risk to data validation is that the array element you are referencing does not exist, not the unfiltered hazard data.
Time
Once the PHP script starts executing, it means that the HTTP request is all over. At this point, the user has no chance to send data to the script. Therefore, no data can be entered into the script (even if the register_globals is turned on). That's why initializing variables is a very good habit.
Anti-injection
The code is as follows |
|
PHP Whole station Anti-injection program, need to require_once this file in public file Judging MAGIC_QUOTES_GPC Status if (@get_magic_quotes_gpc ()) { $_get = sec ($_get); $_post = sec ($_post); $_cookie = sec ($_cookie); $_files = sec ($_files); } $_server = sec ($_server); Function sec (& $array) { If it is an array, iterate through the array, calling recursively if (Is_array ($array)) { foreach ($array as $k = = $v) { $array [$k] = sec ($v); } } else if (is_string ($array)) { Use the Addslashes function to handle $array = Addslashes ($array); } else if (Is_numeric ($array)) { $array = Intval ($array); } return $array; } Integer Filter function function Num_check ($id) { if (! $id) { Die (' parameter cannot be empty! ' ); }//Is null-judged else if (Inject_check ($id)) { Die (' illegal parameters '); }//Injection judgment else if (! is_numetic ($id)) { Die (' illegal parameters '); } Digital judgment $id = Intval ($id); The whole type of return $id; } Character Filter function function Str_check ($STR) { if (Inject_check ($STR)) { Die (' illegal parameters '); } Injection judgment $str = Htmlspecialchars ($STR); Convert HTML return $str; } function Search_check ($STR) { $str = Str_replace ("_", "_", $str); Filter Out "_" $str = str_replace ("%", "%", $str); Filter out "%" $str = Htmlspecialchars ($STR); Convert HTML return $str; } Form Filter function function Post_check ($str, $min, $max) { if (Isset ($min) && strlen ($STR) < $min) { Die (' minimum $min bytes '); } else if (Isset ($max) && strlen ($STR) > $max) { Die (' Up to $max bytes '); } Return Stripslashes_array ($STR); } Anti-injection function function Inject_check ($sql _str) { Return eregi (' select|inert|update|delete| ' | /*|*|.. /|. /| Union|into|load_file|outfile ', $sql _str); Www.111cn.net for filtration, anti-injection } Function Stripslashes_array (& $array) { if (Is_array ($array)) { foreach ($array as $k = = $v) { $array [$k] = Stripslashes_array ($v); } } else if (is_string ($array)) { $array = Stripslashes ($array); } return $array; } ?> |
http://www.bkjia.com/PHPjc/737681.html www.bkjia.com true http://www.bkjia.com/PHPjc/737681.html techarticle 1, the basic principle of PHP submission Data filtering 1) when committing variables into the database, we have to use addslashes () to filter, like our injection problem, a addslashes () is done. Its ...