PHP Prevent SQL Injection Sample analysis and several common attacks regular expression _php instances

Source: Internet
Author: User

Injection vulnerability Code and analysis

Copy Code code as follows:

<?php
function Customerror ($errno, $errstr, $errfile, $errline)
{
echo "<b>error number:</b> [$errno],error on line $errline in $errfile <br/>";
Die ();
}
Set_error_handler ("Customerror", e_error);
$getfilter = "' | (And|or) \\b.+? (>|<|=|in|like) |\\/\\*.+?\\*\\/|<\\s*script\\b|\\bexec\\b| Union.+? Select| Update.+? Set| Insert\\s+into.+? values| (select| DELETE). +? From| (create| alter| drop| TRUNCATE) \\s+ (table| DATABASE) ";
$postfilter = "\\b (and|or) \\b.{1,6}?" (=|>|<|\\bin\\b|\\blike\\b) |\\/\\*.+?\\*\\/|<\\s*script\\b|\\bexec\\b| Union.+? Select| Update.+? Set| Insert\\s+into.+? values| (select| DELETE). +? From| (create| alter| drop| TRUNCATE) \\s+ (table| DATABASE) ";
$cookiefilter = "\\b (and|or) \\b.{1,6}?" (=|>|<|\\bin\\b|\\blike\\b) |\\/\\*.+?\\*\\/|<\\s*script\\b|\\bexec\\b| Union.+? Select| Update.+? Set| Insert\\s+into.+? values| (select| DELETE). +? From| (create| alter| drop| TRUNCATE) \\s+ (table| DATABASE) ";
function Stopattack ($StrFiltKey, $StrFiltValue, $ArrFiltReq)
{
if (Is_array ($StrFiltValue))
{
$StrFiltValue =implode ($StrFiltValue);
}
if (Preg_match ("/") $ArrFiltReq. " /is ", $StrFiltValue) ==1&&!isset ($_request[' SecurityToken '))
{
Slog ("<br><br> Operation IP:". $_server["REMOTE_ADDR"]. " <br> operation time: ". Strftime ("%y-%m-%d%h:%m:%s ")." <br> Action page: ". $_server[" Php_self "]." <br> Submission Method: ". $_server[" Request_method "." <br> Submit parameters: ". $StrFiltKey." <br> submit data: ". $StrFiltValue);
Print "Result Notice:illegal operation!";
Exit ();
}
}
foreach ($_get as $key => $value)
{
Stopattack ($key, $value, $getfilter);
}
foreach ($_post as $key => $value)
{
Stopattack ($key, $value, $postfilter);
}
foreach ($_cookie as $key => $value)
{
Stopattack ($key, $value, $cookiefilter);
}

function Slog ($logs)
{
$toppath = "log.htm";
$Ts =fopen ($toppath, "A +");
Fputs ($Ts, $logs. " \ r \ n ");
Fclose ($Ts);
}
?>

Sql

Analysis

If you use this function, this function will bypass the standard error handling of PHP, so that you define the error handler (Die ()).

Secondly, if the code has been wrong before the execution, the user-defined program has not been executed, so the user will not be used to write the error processing program.

Then, PHP has a set of error handling mechanism, you can use Set_error_handler () to take over PHP error handling, you can use the Trigger_error () function to actively throw an error.

The Set_error_handler () function sets the user-defined error-handling function. function is used to create the user's own error handling method for the runtime. It needs to first create an error-handling function and then set the error level.

About the usage:

Copy Code code as follows:

function Customerror ($errno, $errstr, $errfile, $errline)

{

echo "<b> error code:</b> [${errno}] ${errstr}\r\n";

Echo Error line: {$errline} file {$errfile}\r\n;

echo "PHP version", Php_version, "(", Php_os, ") \ r \ n";

Die ();

}

Set_error_handler ("Customerror", e_all| E_STRICT);

In this function, you can do anything to do, including the wrong details of the format output, log file.

Copy Code code as follows:

function Slog ($logs)
{
$toppath = "log.htm";
$Ts =fopen ($toppath, "A +");
Fputs ($Ts, $logs. " \ r \ n ");
Fclose ($Ts);
}

Custom error-handling functions must have these four input variables $errno, $errstr, $errfile, $errline.

Errno is a set of constants that represent the wrong rank and a set of integers that correspond to them, but generally use their string values to represent a better semantics. For example e_warning, the binary mask is 4, which indicates a warning message.

Next, the function is passed as a callback parameter to the Set_error_handler. This will take over PHP's native error-handling functions. Note that this type of hosting does not host all kinds of errors, such as E_error, E_parse, E_core_error, e_core_warning, E_compile_error, e_compile_warning, and E_ Part of the strict. These errors are displayed in the most original manner, or are not displayed.

The Stopattack () function writes the pass-through post, get, cookie, regular expression, and call slog () to the log file.

Copy Code code as follows:

$Exec _commond = "(\\s|\\s) * (Exec (\\s|\\+) + (s|x) p\\w+) (\\s|\\s) *";
$Simple _XSS = "(\\s|\\s) * (%3c) |<) ((%2f) |/) *[a-z0-9%]+ ((%3e) |>) (\\s|\\s) *";
$Eval _XSS = "(\\s|\\s) * (%65) |e) (\\s) * (%76) |v) (\\s) * ((%61) |a) (\\s) * ((%6C) |l) (\\s|\\s) *";
$Image _XSS = "(\\s|\\s) * (%3c) |<) ((%69) |i| i| (%49)) ((%6d) |m| m| (%4D)) ((%67) |g| G| (%47)) [^\\n]+ (%3e) |>) (\\s|\\s) * ";
$Script _XSS = "(\\s|\\s) * (%73) |s) (\\s) * ((%63) |c) (\\s) * ((%72) |r) (\\s) * ((%69) |i) (\\s) * (%70) |p) (\\s) * ((%74) |t) (\ s|\\s) * ";
$SQL _injection = "(\\s|\\s) * (%27) | (') | (%3d) | (=)| (/)| (%2f) | (\")| ((%22) | (-|%2d) {2}) | (%23) | (%3b) | (;)) + (\\s|\\s) * ";

When HP encounters an error, it gives the location, number of rows, and reasons for the error script, and many people say it's not a big deal. But the consequences of revealing the actual path are unthinkable, and for some intruders, this information is very important, and in fact there are many servers that are now in question. Some network management simply put the PHP configuration file display_errors set to off to solve, but I think this method is too negative. There are times when we really need PHP to return the wrong information for debugging. And you may need to give the user an account when you make an error, or even navigate to another page. But with the Set_error_handler (), these contradictions can also be resolved.

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.