sql注入攻击能够得逞,往往是因为前台页面传递的参数含有非法字符,导致sql原有逻辑发生改变。
如经典的登录验证例子:
假设数据库中username=admin,password=admin123
public static void main(String[] args) throws Exception {
String name="admin";
String password="admin123";
String sql="select * from user_table where username='"+name+"' and password='"+password+"'";
System.out.println(sql);
}
控制台输出拼接后的sql为:select * from user_table where username='admin' and password='admin123'(达到验证用户名,密码效果)
但是恶意攻击者不知道用户名,密码,通过如下方式仍能通过验证,登陆系统
public static void main(String[] args) throws Exception {
String name="xxx";
String password="xxx' or '1'='1";
String sql="select * from user_table where username='"+name+"' and password='"+password+"'";
System.out.println(sql);
}
控制台输出拼接后的sql为:select * from user_table where username='xxx' and password='xxx' or '1'='1'(达到攻击效果,查询出所有用户信息)
导致拼接逻辑发生改变,是由于单引号【'】造成的,如果我在系统中对单引号【'】进行了过滤,全部转换为全角的单引号【’】或其他字符,如【x】,是否能够完全杜绝sql注入,看网上的例子,要过滤许多关键字,我认为如果过滤了单引号【'】基本上就是无解的,谁能给个例子,我过滤了单引号【'】,还能进行sql注入。
public static void main(String[] args) throws Exception {
String name="xxx";
String password="xxx' or '1'='1";
name=name.replaceAll("'", "x");
password=password.replaceAll("'", "x");
String sql="select * from user_table where username='"+name+"' and password='"+password+"'";
System.out.println(sql);
}
控制台输出拼接后的sql为:select * from user_table where username='xxx' and password='xxxx or x1x=x1'(普通sql,达不到攻击效果)
---------------------
作者:wusr
来源:优快云
原文:https://blog.youkuaiyun.com/u012064609/article/details/17393165
版权声明:本文为博主原创文章,转载请附上博文链接!