search
Homephp教程php手册细谈php中SQL注入攻击与XSS攻击

通常在编程中程序员要考虑的问题不仅是代码效率与代码复用性,而且还要考虑一些安全问题

例如: SQL注入攻击
XSS攻击
代码如下:
任意执行代码
文件包含以及CSRF.
}


关于SQL攻击有很多文章还有各种防注入脚本,但是都不能解决SQL注入的根本问题
见代码:

代码如下:
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攻击,我们再来说说XSS攻击以及防范吧。。
提交表单:
代码如下:





接收文件:
代码如下:
if(empty($_POST['sub'])){
echo $_POST['test'];
}

很简单的一段代码,在这里只是模拟了下使用场景..
加入攻击者提交
<script>alert(document.cookie);</script>
在返回的页面就应该显示当前页面的cookie信息。
我们可以运用到某些留言板上(提前是没过滤的),然后当管理员审核改条信息时盗取COOKIE信息,并发送到攻击者的空间或者邮箱。。攻击者可以使用cookie修改器进行登陆入侵了。。
当然解决方案也有很多。。下面就介绍一个最常用的方式吧。
修复方案1:使用javascript进行转义
修复方案2:使用php内置函数进行转义
代码如下:
[code]
if(empty($_POST['sub'])){
$str=$_POST['test'];
htmlentities($srt);
echo $srt;
}
[html]
好了,关于SQL注入攻击和XSS攻击的案例与修复方法就讲的差不多了。
当然还有其他的解决方法:
例如:使用php框架
还有其他的一些方法。。当然了,XSS的运用范围与攻击方式很多也很广。本文只针对php的提交方式进行过滤,还有其他的需要自己去研究 ^_^~
此文出此:Aey uhost team(team.hake.cc),转载请带上版权。

y0umer
2012/6/7
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment