小白的SQL注入攻击入门

xiaoxiao2021-02-28  16

SQL注入攻击示意图:

最开始是想找个有漏洞的网站进行SQL攻击,网上了解到inurl:TeachView.asp是一个有漏洞的网址,而且我搜索的时候确实还有很多网站包含了这个漏洞,但是可能应为众所周知的问题,我试着进入了几个网站,有好几个都提示了禁止访问。


下面这个网站用了恒等式or 1=1可以访问:(看url) 并且猜测到了字段名和数据库名为:users和admin


但是下面这个网站就禁止了类似访问:


接下来尝试自己用php写了一个和数据库连接的登录验证网页,尝试sql注入攻击的万能密码登录,图简单就只在数据库添加了一个用户,账户名为li,密码为123,表名为user,数据库名为test: 接下来尝试sql注入攻击,用字符串’or 1 =1#当用户名,密码随便输入也登录上了,因为在登录验证的sql语句中用的直接是select * from users where username=’ nameandpassword= pwd’,直接取了用户输入的用户名和密码,没有做任何处理直接使用,就导致了用’or 1 =1#字符串后sql语句变成了select * from users where username=’’or 1 =1#’ and password=’$pwd’,因为输入的是恒等式,而且#注释掉了password,即无论输入什么密码,都可以登录,sql注入成功。


现在越来越多人了解sql注入攻击,所以大多数网站都对用户提交的参数做了检测,例如不能输入“’”字符之类的,所以如’ or 1=1#这样的用户名是很难破解网站登录的。也了解到现在有很多sql注入的软件,例如sqlmap之类的, 它们能扫描网站的漏洞,并帮助你注入攻击,比起手工注入要简单很多, 但是我还是尝试了解手工注入,这样能更好了解sql注入攻击的含义,由于时间问题,对sqlmap的了解使用并没有很透彻,这里就不截图演示了。 因为没有系统学过php,借鉴了一些经验做了简易登录验证网页,在此附上代码。

Login.php

<!DOCTYPE html> <html> <head> <title>sql注入攻击</title> <meta http-equiv="content-type" content="text/html;charset=utf-8"> </head> <body> <form action="validate.php" method="post"> <fieldset> <legend>sql注入攻击</legend> <table> <tr> <td>用户名:</td> <td><input type="text" name="username"></td> </tr> <tr> <td>密码:</td> <td><input type="text" name="password"></td> </tr> <tr> <td><input type="submit" value="提交"></td> <td><input type="reset" value="重置"></td> </tr> </table> </fieldset> </form> </body> </html>

validate.php

<!DOCTYPE html> <html> <head> <title>登录验证</title> <meta http-equiv="content-type" content="text/html;charset=utf-8"> </head> <body> <?php $conn= mysqli_connect("localhost",'root',"")or die("连接数据库失败");; mysqli_select_db( $conn,"test") or die("您要选择的数据库不存在"); $name=$_POST['username']; $pwd=$_POST['password']; $sql="select * from users where username='$name' and password='$pwd'"; $query=mysqli_query($conn,$sql); $arr=mysqli_fetch_array($query); if(is_array($arr)){ header("Location:manager.php"); } else{ echo "用户名或密码错误,<a href=\"Login.php\">请重新登录</a>"; } ?> </body> </html>

manager.php

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> </head> <body> 登陆成功 </body> </html>

php里很多和Mysql链接的函数都改了,有的是参数不一样,有的是变成了mysqli_……之类的,烦躁。。。。

转载请注明原文地址: https://www.6miu.com/read-301582.html

最新回复(0)