SQL注入:利用现有应用程序,攻击者将精心构造的SQL语句注入到后台数据库,并使得数据库引擎成功执行。php
SQL注入特色html
SQL注入攻击的危害性前端
SQL注入工具mysql
手工探测SQL漏洞react
经过拼接SQL语句来判断和验证漏洞linux
// 测试语句 id=1' and 1=1 and 1=2 order by num union 联合查询 and 1=2 union select 1,2,3,4 user() database() group_coucat(table_name) from information_schema.tables where table_schema=tableName // table名十六进制 select * from downloads where id=1 and 1=2 union select 1,2,3,4,5,6,group_concat(username) from members;
?id=1 and 1=2 union select 1,2,3,4,5,6,load_file('/etc/passwd') ?id=1 and 1=2 union select 1,2,3,4,5,6,@@version ?id=1 and 1=2 union select 1,2,3,4,5,6,@@version_compile_os ?id=1 and 1=2 union select 1,2,3,4,5,6,@@basedir ?id=1 and 1=2 union select 1,2,3,4,5,6,@@datadir
SQL防护web
代码层防护sql
编码阶段:shell
测试阶段:数据库
产品化阶段:
第三方安全程序
SQL语句验证
function CheckSql($db_string, $querytype = 'select') { global $cfg_cookie_encode; $clean = ''; $error = ''; $old_pos = 0; $pos = -1; $log_file = DEDEINC.'/../data/'.md5($cfg_cookie_encode).'_safe.txt'; $userIP = GetIP(); $getUrl = GetCurUrl(); // 若是是普通查询语句,直接过滤一些特殊语法 if ($querytype=='select') { $notallow1 = "[^0-9a-z@\._-]{1,}(union|sleep|benchmark|load_file|outfile)[^0-9a-z@\.-]{1,}"; // $notallow2 = "--|/\*"; if (eregi($notallow1,$db_string)) { fputs(fopen($log_file,'a+'),"$userIP||$getUrl||$db_string||SelectBreak\r\n"); exit("<font size='5' color='red'>Safe Alert: Request Error step 1 !</font>"); } } // 完整的SQL检查 while (true) { $pos = strpos($db_string, '\'', $pos + 1); if ($pos === false) { break; } $clean .= substr($db_string, $old_pos, $pos - $old_pos); while (true) { $pos1 = strpos($db_string, '\'', $pos + 1); $pos2 = strpos($db_string, '\\', $pos + 1); if ($pos1 === false) { break; } elseif ($pos2 == false || $pos2 > $pos1) { $pos = $pos1; break; } $pos = $pos2 + 1; } $clean .= '$s$'; $old_pos = $pos + 1; } $clean .= substr($db_string, $old_pos); $clean = trim(strtolower(preg_replace(array('~\s+~s' ), array(' '), $clean))); // 老版本的Mysql并不支持union,经常使用的程序里也不使用union,可是一些黑客使用它,因此检查它 if (strpos($clean, 'union') !== false && preg_match('~(^|[^a-z])union($|[^[a-z])~s', $clean) != 0) { $fail = true; $error="union detect"; } // 发布版本的程序可能比较少包括--,#这样的注释,可是黑客常常使用它们 elseif (strpos($clean, '/*') > 2 || strpos($clean, '--') !== false || strpos($clean, '#') !== false) { $fail = true; $error="comment detect"; } // 这些函数不会被使用,可是黑客会用它来操做文件,down掉数据库 elseif (strpos($clean, 'sleep') !== false && preg_match('~(^|[^a-z])sleep($|[^[a-z])~s', $clean) != 0) { $fail = true; $error="slown down detect"; } elseif (strpos($clean, 'benchmark') !== false && preg_match('~(^|[^a-z])benchmark($|[^[a-z])~s', $clean) != 0) { $fail = true; $error="slown down detect"; } elseif (strpos($clean, 'load_file') !== false && preg_match('~(^|[^a-z])load_file($|[^[a-z])~s', $clean) != 0) { $fail = true; $error="file fun detect"; } elseif (strpos($clean, 'into outfile') !== false && preg_match('~(^|[^a-z])into\s+outfile($|[^[a-z])~s', $clean) != 0) { $fail = true; $error="file fun detect"; } // 老版本的MYSQL不支持子查询,程序里可能也用得少,可是黑客可使用它来查询数据库敏感信息 elseif (preg_match('~\([^)]*?select~s', $clean) != 0) { $fail = true; $error="sub select detect"; } }
前端攻击成因
网站的脚本经过最终一些手段生成HTML,若是HTML中含有一些恶意代码。
跨站脚本攻击(XSS)
XSS英文全称(Cross-Site-Scripting)
,为了区别于cascading style sheets
层叠样式表(CSS)所以缩写成XSS
形成结果:
XSS攻击的危害
// 钓鱼攻击 ?id=1'<script>alert(/xss/)</script> ?id=1'<iframe src="http://www.baidu.com"></iframe> // 经过些形式(邮件,分享),来诱发用户访问
// 获取cookie ?id=1'<script>alert(document.cookie)</script> // 经过一段脚本程序(包括document.cookie)将用户的信息发送到远程的恶意网站,恶意网站专门接收信息。(DVWA)
跨站请求伪造(CSRF)
影响关键处理被恶意使用
前端攻击防护
XSS场景:web应用等
CSRF场景:内部网络(路由器),Web应用等
代码层防护:
第三方防护手段:
上传web脚本,可以被服务器解析(web shell问题)
可以执行知足的条件:
合法性判断绕过:
%00
空字符也就是null
// 编辑器的文件上传burpsuite
代理抓包。把访问的请求截断而后修改头信息或者参数,绕过合法性的绕过
文件上传漏洞防护
经过请求的方式,将本地文件包含进去,包含进行的文件都是以PHP文件解析
if ($_GET['page']) { include $_GET['page'] } else { include 'main.php' }
文件包含漏洞防护:
php.ini
配置文件中allow_url_include = off
Apache的安全加固
// 文件类型 <Files ~ ".inc$"> Order allow,deny Deny from all </Files> // 目录访问控制 <Directory ~ "~/var/www/admin"> Order allow,deny Deny from all </Direactory> // 文件类型访问控制 <FilesMatch .(txt)$> Order allow,deny Deny from all </FilesMatch>
PHP的安全加固
修改php.ini
中的配置
Mysql的安全加固
system
权限