本节内容:
php防范SQL注入攻击与XSS攻击
1,SQL注入攻击
XSS攻击
任意执行代码
文件包含以及CSRF.
}
例子:
mysql_connect()("localhost","root","123456")or die("数据库连接失败!");
mysql_select_db("test1");
$user=$_post['uid'];
$pwd=$_POST['pass'];
if(mysql_query()("SELECT * from where
admin
= `username`='$user' or `password`='$pwd'"){
echo "用户成功登陆..";
} eles {
echo "用户名或密码出错";
}
?>
以上代码用于检测用户名或密码是否正确,可是在一些恶意攻击者中提交一些敏感代码,.. post判断注入的方式有2种。
1,在form表单的文本框输入 "or‘1'=1"或者"and 1=1"
查询数据库的语句:
SELECT admin from where login = `user`=''or‘1'=1' or `pass`=‘xxxx'
当然也不会出现什么错误,因为or在sql的语句中代表和,或的意思。当然也会提示错误。
当时已经发现了可以执行SQL语句之后就可以查询当前表的所有信息。例如:正确的管理员账户和密码进行登录入侵。。
修复方式1:
使用javascript脚本过滤特殊字符(不推荐,指标不治本)
如果攻击者禁用了javascript还是可以进行SQL注入攻击。。
修复方式2:
使用mysql的自带函数进行过滤。
// 省略连接数据库等操作。。
$user=mysql_real_escape_string($_POST['user']);
mysql_query("select * from admin whrer `username`='$user'");
?>
二,XSS攻击以及防范。
提交表单:
<intup tyep="text" name="test">
<intup tyep="submit" name="sub" value="提交">
</form>
接收文件:
echo $_POST['test'];
}
模拟使用场景..
加入攻击者提交
<script>alert(document.cookie);</script>
在返回的页面就应该显示当前页面的cookie信息。
可以运用到某些留言板上(提前是没过滤的),然后当管理员审核改条信息时盗取COOKIE信息,并发送到攻击者的空间或者邮箱。。攻击者可以使用cookie修改器进行登陆入侵了。。
修复方案1:使用javascript进行转义
修复方案2:使用php内置函数进行转义
if(empty($_POST['sub'])){
$str=$_POST['test'];
htmlentities($srt);
echo $srt;
}
本节内容:
过滤XSS攻击
以下函数:
过滤用户的输入,保证输入是XSS安全的。
例子:
<?php
/**
* 过滤XSS攻击
* edit: www.
*/
function RemoveXSS($val) {
// remove all non-printable characters. CR(0a) and LF(0b) and TAB(9) are allowed
// this prevents some character re-spacing such as <java\0script>
// note that you have to handle splits with \n, \r, and \t later since they *are* allowed in some inputs
$val = preg_replace('/([\x00-\x08,\x0b-\x0c,\x0e-\x19])/', '', $val);
// straight replacements, the user should never need these since they're normal characters
// this prevents like <IMG src=/blog_article/@avascript_alert/index.html('XSS')>
$search = 'abcdefghijklmnopqrstuvwxyz';
$search .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$search .= '1234567890!@#$%^&*()';
$search .= '~`";:?+/={}[]-_|\'\\';
for ($i = 0; $i < strlen($search); $i++) {
// ;? matches the ;, which is optional
// 0{0,7} matches any padded zeros, which are optional and go up to 8 chars
// @ @ search for the hex values
$val = preg_replace('/(&#[xX]0{0,8}'.dechex(ord($search[$i])).';?)/i', $search[$i], $val); // with a ;
// @ @ 0{0,7} matches '0' zero to seven times
$val = preg_replace('/(?{0,8}'.ord($search[$i]).';?)/', $search[$i], $val); // with a ;
}
// now the only remaining whitespace attacks are \t, \n, and \r
$ra1 = Array('javascript', 'vbscript', 'expression', 'applet', 'meta', 'xml', 'blink', 'link', 'style', 'script', 'embed', 'object', 'iframe', 'frame', 'frameset', 'ilayer', 'layer', 'bgsound', 'title', 'base');
$ra2 = Array('onabort', 'onactivate', 'onafterprint', 'onafterupdate', 'onbeforeactivate', 'onbeforecopy', 'onbeforecut', 'onbeforedeactivate', 'onbeforeeditfocus', 'onbeforepaste', 'onbeforeprint', 'onbeforeunload', 'onbeforeupdate', 'onblur', 'onbounce', 'oncellchange', 'onchange', 'onclick', 'oncontextmenu', 'oncontrolselect', 'oncopy', 'oncut', 'ondataavailable', 'ondatasetchanged', 'ondatasetcomplete', 'ondblclick', 'ondeactivate', 'ondrag', 'ondragend', 'ondragenter', 'ondragleave', 'ondragover', 'ondragstart', 'ondrop', 'onerror', 'onerrorupdate', 'onfilterchange', 'onfinish', 'onfocus', 'onfocusin', 'onfocusout', 'onhelp', 'onkeydown', 'onkeypress', 'onkeyup', 'onlayoutcomplete', 'onload', 'onlosecapture', 'onmousedown', 'onmouseenter', 'onmouseleave', 'onmousemove', 'onmouseout', 'onmouseover', 'onmouseup', 'onmousewheel', 'onmove', 'onmoveend', 'onmovestart', 'onpaste', 'onpropertychange', 'onreadystatechange', 'onreset', 'onresize', 'onresizeend', 'onresizestart', 'onrowenter', 'onrowexit', 'onrowsdelete', 'onrowsinserted', 'onscroll', 'onselect', 'onselectionchange', 'onselectstart', 'onstart', 'onstop', 'onsubmit', 'onunload');
$ra = array_merge($ra1, $ra2);
$found = true; // keep replacing as long as the previous round replaced something
while ($found == true) {
$val_before = $val;
for ($i = 0; $i < sizeof($ra); $i++) {
$pattern = '/';
for ($j = 0; $j < strlen($ra[$i]); $j++) {
if ($j > 0) {
$pattern .= '(';
$pattern .= '(&#[xX]0{0,8}([9ab]);)';
$pattern .= '|';
$pattern .= '|(?{0,8}([9|10|13]);)';
$pattern .= ')*';
}
$pattern .= $ra[$i][$j];
}
$pattern .= '/i';
$replacement = substr($ra[$i], 0, 2).'<x>'.substr($ra[$i], 2); // add in <> to nerf the tag
$val = preg_replace($pattern, $replacement, $val); // filter out the hex tags
if ($val_before == $val) {
// no replacements were made, so exit the loop
$found = false;
}
}
}
return $val;
}
?>
本节内容:
php获取远程图片大小
例子:
<?php
/**
* 取得远程图片文件的大小
* by www.
*/
//用法 echo remote_filesize($url,$user='',$pw='');
$url = "http://www./images/logo.jpg";//图片地址
echo remote_filesize($url,$user='',$pw='');
function remote_filesize($uri,$user='',$pw='')
{
// start output buffering
ob_start();
// initialize curl with given uri
$ch = curl_init($uri); // make sure we get the header
curl_setopt($ch, CURLOPT_HEADER, 1); // make it a http HEAD request
curl_setopt($ch, CURLOPT_NOBODY, 1); // if auth is needed, do it here
if (!empty($user) && !empty($pw))
{
$headers = array('Authorization: Basic ' . base64_encode($user.':'.$pw));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}
$okay = curl_exec($ch);
curl_close($ch); // get the output buffer
$head = ob_get_contents(); // clean the output buffer and return to previous // buffer settings
ob_end_clean(); // gets you the numeric value from the Content-Length // field in the http header
$regex = '/Content-Length:\s([0-9].+?)\s/';
$count = preg_match($regex, $head, $matches); // if there was a Content-Length field, its value // will now be in $matches[1]
if (isset()($matches[1]))
{
$size = $matches[1];
}
else
{
$size = 'unknown';
}
$last_mb = round($size/(1024*1024),3);
$last_kb = round($size/1024,3);
return $last_kb . 'KB / ' . $last_mb.' MB';
}
代码说明:
先CURL获取图片到缓冲区,然后正则获取图片的Content-Length信息。