浅谈Web安全

SQL注入


如果数据库用的是MyISAM引擎,且注入点是某个会锁表的语句(insert,replace,update,delete),那么整个数据表的访问都会被阻塞。禁用mysql的sleep函数,或者修改sleep上限,拒绝不合理的超长sleep。现实中很少用到这个sleep功能,就算遇到需要sleep的场景,也可以通过外部应用来实现sleep。

防止SQL注入

select * from test where id = $id;

http://localhost/view.php?id=112 or 1=1

select * from test where id = 112 or 1=1;
  • 2.特殊字符转义
username=admin&password=' or sleep(5)-- ddd
select * from TSUser where username = 'admin' and password = '' or sleep(5)-- ddd’
username=admin&password=\' or sleep(5)-- ddd
select * from TSUser where username = 'admin' and password = '\' or sleep(5)-- ddd'
  • 3.使用预编译语句
    在Java中使用PreparedStatement预编译语句可以在创建的时候将指定的SQL语句发送给DBMS完成解析、检查、编译等工作,能有效地防御SQL注入。

**注意:如果使用动态拼接的SQL语句,仍然是不安全的。如下例:

String sql = "select * from TSUser where username = ‘"+ username +"' and password = '"+ password +"'";
PreparedStatement ps = connection.prepareStatement(sql);
ResultSet resultSet = ps.executeQuery();

要想使PreparedStatement防御SQL注入,必须使用它的setter方法(setString、setInt等)

  • 4.框架技术—MyBatis
<select id="getBlogById" resultType="Blog" parameterType="int”>  
        select id,title,author,content  
        from blog where id=#{id}  
</select>  
select id,title,author,content from blog where id = ?     —>执行了预编译
<select id="orderBlog" resultType="Blog" parameterType="map">  
        select id,title,author,content  
        from blog order by ${orderParam}  
</select>  
select id,title,author,content from blog order by id     —>直接替换参数,没有执行预编译

在mybatis中,”${xxx}”这样格式不能避免注入攻击。但涉及到动态表名和列名时,只能使用“${xxx}”这样的参数格式,所以,这样的参数需要我们在代码中手工进行处理来防止注入。在编写mybatis的映射语句时,尽量采用“#{xxx}”这样的格式。若不得不使用“${xxx}”这样的参数,要手工地做好过滤工作,来防止sql注入攻击。

XSS跨站脚本攻击

XSS攻击是在网页中嵌入恶意脚本代码,当用户使用浏览器浏览被嵌入恶意脚本的页面时,恶意代码将会在用户的浏览器上执行

当用户访问一个带有XSS代码的URL请求时,服务器接收数据后处理,然后把带有XSS代码的数据发送到浏览器,浏览器解析这段带有XSS代码的数据后执行恶意脚本。

允许存储用户数据的Web应用程序都可能出现存储型XSS漏洞,当攻击者提交一段XSS代码后,服务器接收并存储。当用户访问某个页面时,这段XSS代码就会被程序读取出来相应给浏览器,造成XSS攻击。

https://files.cnblogs.com/files/umgsai/浅谈Web安全.key.zip

posted @ 2017-04-11 21:48  商商-77  阅读(168)  评论(0)    收藏  举报