PHP General anti-injection and injection detailed description

Source: Internet
Author: User

The

as well as the server and MySQL tutorials also need to tighten some security precautions.
for Linux server security settings:
Encrypt passwords, use the "/usr/sbin/authconfig" tool to open the shadow feature of passwords, and encrypt password.
Prohibit access to important files, enter the Linux command interface, and enter at the prompt:
#chmod 600/etc/inetd.conf   //change file attributes to
#chattr +i  /etc/inetd.conf    //Guarantee file owner is root
#chattr –i /etc/inetd.conf     Restrict changes to this file   
prohibit any user from changing to root by using the SU command
Add the following two lines to the beginning of the SU configuration file, the/etc/pam.d/directory:
auth  sufficient  /lib/security/pam_rootok.so Debug
auth  required /lib/security/pam_whell.so group=wheel
Delete all special accounts
#userdel   LP     delete users
#groupdel LP etc     Delete Group
prohibit unused Suid/ Sgid program
#find/-type F (-perm-04000 -o–perm-02000)-execls–lg {};

The code is as follows Copy Code
$arrfiltrate =array ("'", ";", "union", "select", "Insert", "Update", "delete", "Load_file", "outfile");

URL to jump after an error

The code is as follows Copy Code

$strgourl = "";

function Funstringexist ($strfiltrate, $arrfiltrate)
{
foreach ($arrfiltrate as $key => $value)
{
if (eregi ($value, $strfiltrate))
{
return true;
}
}
return false;
}

Merging $_post, $_get and $_cookie

The code is as follows Copy Code
if (function_exists (Array_merge))
{
$arrpostgetcookiesession =array_merge ($http _post_vars, $http _get_vars, $http _cookie_vars);
$string = Implode ("", $arrpostgetcookiesession);
}

Verify

The code is as follows Copy Code
if (Funstringexist ($string, $arrfiltrate))
{
echo "<script language=" "Web Effects" >alert ("hint, illegal character");</script> ";
}
Else
{
echo "<script language=" JavaScript ">window.location=" ". $strgourl." "; </script> ";
}

Second example of anti-injection

The code is as follows Copy Code

PHP Universal Anti-injection security code
Description
Determines whether the passed variable contains illegal characters
such as $_post, $_get
Function:
Anti-injection
**************************/
Illegal characters to filter
$arrfiltrate =array ("'", ";", "union");
The URL to jump after an error is not filled in the default previous page
$strgourl = "";
Whether the value in the array exists
function Funstringexist ($strfiltrate, $arrfiltrate) {
foreach ($arrfiltrate as $key => $value) {
if (eregi ($value, $strfiltrate)) {
return true;
}
}
return false;
}
Merging $_post and $_get
if (function_exists (Array_merge)) {
$arrpostandget =array_merge ($http _post_vars, $http _get_vars);
}else{
foreach ($http _post_vars as $key => $value) {
$arrpostandget []= $value;
}
foreach ($http _get_vars as $key => $value) {
$arrpostandget []= $value;
}
}
Verify Start
foreach ($arrpostandget as $key => $value) {
if (Funstringexist ($value, $arrfiltrate)) {
echo "Alert (/" NEEAO hint, illegal character/");
if (empty ($strgourl)) {
echo "History.go (-1);";
}else{
echo "window.location=/" ". $strgourl." /";";
}
Exit
}
}


Take a look at the details of the injection

Converted to ASCII followed by char (97,108,112,104,97)
The conversion into 16 is 0x616c706861
(We will provide 16 and ASCII conversion tools on the CD)
Okay, just type in the browser:

The code is as follows Copy Code
Http://localhost/site/admin/login.php?
Username=char (97,108,112,104,97)%23


The SQL statement becomes:

The code is as follows Copy Code

SELECT * FROM Alphaaut

Hor where Username=char (97,108,112,104,97) # and password=


As shown in Figure 21


As we expected, he carried it out smoothly and we got what we wanted.
Of course, we can also construct

The code is as follows Copy Code
Http://www.111cn.net/site/admin/login.php?username=0x616c706861%23


The SQL statement becomes:

The code is as follows Copy Code
SELECT * FROM Alphaauthor where username
=0x616c706861%23# and password=


Once again, we are winners. It's a sense of accomplishment.

Maybe you'll ask if we can put the # in char ()
actually char (97,108,112,104,97) equals alpha
Note that Alpha is quoted as an alpha string.
We know in MySQL if the execution

The code is as follows Copy Code
Mysql> SELECT * from Dl_users where username=alpha;
Error 1054 (42S22): Unknown column alpha in WHERE clause


Look at the return error. Because he would think Alpha was a variable. So we have to put quotes on Alpha.
As follows

The code is as follows Copy Code
Mysql> SELECT * from dl_users where username= Alpha;

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.