1, the basic principles of data filtering of PHP submission
1 Submit variables into the database, we must use Addslashes () to filter, like our injection problem, a addslashes () is done. In fact, when it comes to variable values, the Intval () function is a good choice for string filtering.
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 data that goes in and out of a database can play a role in formatting. In fact, this parameter is very popular as long ago when the injection was crazy.
| The code is as follows |
Copy Code |
|
.
if (Isset ($_post["F_login"))
{
//Connection Database ...
//... Code slightly ...
//Check if the user exists
$t _struname = $_post["F_uname"];
$t _strpwd = $_post["F_pwd"];
$t _strsql = "SELECT * from tbl_users WHERE username= ' $t _struname ' and password = ' $t _strpwd ' LIMIT 0,1 ';
if ($t _hres = mysql_query ($t _strsql))
{
///after successful query processing. ...
}
}
<body
<form method=post action= ""
Username: <input type= "text" name= "F_uname" size=30><br
Password: <input type=text name= "F_pwd" SIZE=30><BR>
<input type= "Submit" Name= "F_login" value= "login"
</form>
</body> |
3 When using System functions, you must use Escapeshellarg (), escapeshellcmd () parameters to filter, so you can rest assured that the use of system functions.
4 for the Cross Station, Strip_tags (), Htmlspecialchars () Two parameters are good, for the user submitted with HTML and PHP tags will be converted. For example, the angle bracket "<" will be converted to "<" such harmless characters.
| The code is as follows |
Copy Code |
$new = Htmlspecialchars ("<a href= ' test ' >Test</a>", ent_quotes);
Strip_tags ($text,); |
5 for the correlation function filtering, just like the previous include (), Unlink,fopen (), and so on, as long as you have to perform the operation of the variable specified or the relevant characters filter tight, I think this is perfect.
2, PHP simple data filtering
1) Warehousing: Trim ($STR), Addslashes ($STR)
2) out of the library: stripslashes ($STR)
3) Display: Htmlspecialchars (NL2BR ($STR))
I. Type of injection type of attack
There may be many different types of attack motives, but at first glance there seems to be more types. This is very real-if a malicious user discovers a way to execute multiple queries. We will discuss this in detail later in this article.
Such as
If your script is executing a SELECT command, then an attacker can force the display of every row in a table-by injecting a condition such as "1=1" into the WHERE clause, as follows (where the injection portion is shown in bold):
| The code is as follows |
Copy Code |
|
SELECT * FROM wines WHERE variety = ' Lagrein ' OR 1=1; ' |
As we discussed earlier, this may be useful information in itself because it reveals the general structure of the table (which is not achievable by a common record) and potentially displays records that contain confidential information.
An update directive potentially has a more immediate threat. By placing other attributes in the SET clause, an attacker can modify any field in the currently updated record, such as the following example (where the injection portion is shown in bold):
| The code is as follows |
Copy Code |
|
UPDATE Wines SET type= ' red ', ' vintage ' = ' 9999 ' WHERE variety = ' Lagrein ' |
By adding a constant real condition such as 1=1 to a WHERE clause of an update instruction, the scope of the modification can be extended to each record, such as the following example (where the injection portion is shown in bold):
| The code is as follows |
Copy Code |
|
UPDATE Wines SET type= ' red ', ' vintage ' = ' 9999 WHERE variety = ' lagrein ' or 1=1; ' |
The most dangerous instruction may be delete-it is not difficult to imagine. The injection technology is the same as we have seen-by modifying the WHERE clause to extend the scope of the affected record, such as the following example (where the injection portion is shown in bold):
| The code is as follows |
Copy Code |
| DELETE from wines WHERE variety = ' Lagrein ' OR 1=1; ' |
Second, multiple query injection
Multiple query injection will exacerbate potential damage that an attacker could cause-by allowing multiple destructive instructions to be included in a query. When using the MySQL database, an attacker could easily do this by inserting an unexpected terminator into the query-at which point a injected quotation mark (either single or double quotes) marks the end of the desired variable, and then terminates the instruction with a semicolon. Now, an additional attack instruction may be added to the end of the original instruction that is now terminated. The final destructive query might look like this:
The code is as follows:
| The code is as follows |
Copy Code |
|
SELECT * FROM wines WHERE variety = ' Lagrein ';
GRANT all on *.* to ' badguy@% ' identified by ' gotcha '; |
This injection creates a new user Badguy and gives its network privileges (all privileges on all tables), and an "ominous" password is added to this simple SELECT statement. If you follow our recommendations in previous articles-strictly restricting the privileges of users of the process, this should not work because the Web server daemon no longer has the grant privileges you have withdrawn. In theory, however, such an attack could give badguy free authority to implement whatever he does to your database.
Share a written by yourself below
| The code is as follows |
Copy Code |
|
function Phpsql_show ($STR) {
$str = Stripslashes ($STR);
$str = Str_replace ("& #92;", "", $str);
$str = Str_replace ("& #47;", "/", $STR);
$str = Str_replace ("& #32;", "", $str);
$str = Str_replace ("& #44;", ",", $str);
return $str;
}
function Phpsql_post ($STR) {
$str = Stripslashes ($STR);
$str = Str_replace ("|", "& #124;", $STR);
$str = Str_replace ("<", "& #60;", $STR);
$str = Str_replace (">", "& #62;", $STR);
$str = Str_replace ("&nbsp;", "& #32;", $STR);
$str = Str_replace ("", "& #32;", $STR);
$str = Str_replace ("(", "& #40;", $STR);
$str = Str_replace (")", "& #41;", $STR);
$str = Str_replace ("'", "& #96;", $STR);
$str = Str_replace ("'", "& #39;", $STR);
$str = Str_replace (' "," & #34; ", $STR);
$str = Str_replace (",", "& #44;", $STR);
$str = Str_replace ("$", "& #36;", $STR);
$str = Str_replace ("", "& #92;", $STR);
$str = Str_replace ("/", "& #47;", $STR);
return $str;
}
function Phpsql_replace ($STR) {
$str = Stripslashes ($STR);
$str = Str_replace ("'", "& #39;", $STR);
return $str;
} |
To sum up:
* Addslashes () is forcibly added;
* Mysql_real_escape_string () will judge the character set, but the PHP version is required;
* Mysql_escape_string does not consider the current character set of the connection.
The way to prevent SQL injection in DZ is to use the Addslashes function, while in dthmlspecialchars this function there are some substitutions $string = Preg_replace (/& ]{4});)/, &1, this substitution solves the problem of injection, but also solves some problems of Chinese garbled