IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

Mybatis检查SQL注入

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

Mybatis 的 Mapper.xml 语句中 parameterType 向SQL语句传参有两种方式:#{ } 和 ${ }。

使用#{ }是来防止SQL注入,使用${ }是用来动态拼接参数。

如何排查出

1. 检查是否有$号

如果你使用的是ide代码编辑器,那么可以通过全局搜索${ , 快速定位到使用${ }拼接SQL的语句,在去找到外部传入参数的入口,闭合sql证明即可。

2. 检查是否有order by语句

同样的在搜索是否使用order by排序语句,如果有一步一步追踪是否有外部参数,未过滤就直接传递到order by语句里面来。

为什么#{ }就安全

#{ } 就类似JDBC的预编译,所以安全。类似如下SQL语句:(JDBC预编译的原理,希望后面能专门去研究一下,并写博文)

  • 使用 ${ }效果是:
select * from testtable where id="1" or true or id  # 1后面就是被攻击者恶意构造的字符

87_1.png

  • 而使用#{ } 的效果是:
select * from testtable where id="1\" or true or id\""         # 1后面就是被攻击者恶意构造的字符

87_2.png

什么情况下用不了#{}

答案是:在order by 排序语句的情况下不行,为什么?

先复习一下order by的用法

order by就是一个排序的工具。

# 这个1就是指第一个列索引,也可以写id (列索引)
select * from testtable ORDER BY 1 ASC #ASC表示按升序排序,DESC表示按降序排序

# 两个代码是一样的
select * from testtable ORDER BY id ASC #ASC表示按升序排序,DESC表示按降序排序

87_3.png

对于order by 我们是用不了#{}的,因为用了这个就会被自动转换成字符串,自动加引号,这样语句就不生效了。

<select id="selectStudentsByName" resultType="Student">
    select id,name,age,score from student order by #{column}
</select>

<!--编译出来的结果如下:-->
select * from table order by 'column'

87_4.png

会发现加上“” 双引号符号后,就没法正常排序了。

如何解决:
使用${},MyBatis就不会修改或转义改字符串。但是这样又不安全,会导致潜在的SQL注入攻击。所以我们需要自己去限制,不允许用户输入一些非法字段,通常只使用白名单方式校验。

总结

即使是安全的sql预编译技术,也是有适用范围的,一些应用场景也是不适用的。当我们在做黑盒或者审计的时候,碰到了预编译处理不了的场景,比如说排序功能的时候就得格外注意了,不然被人order by注入就悲剧了。

文章永久链接:https://tech.souyunku.com/?p=22148


Warning: A non-numeric value encountered in /data/wangzhan/tech.souyunku.com.wp/wp-content/themes/dux/functions-theme.php on line 1154
赞(70) 打赏



未经允许不得转载:搜云库技术团队 » Mybatis检查SQL注入

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码
IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

评论 抢沙发

大前端WP主题 更专业 更方便

联系我们联系我们

觉得文章有用就打赏一下文章作者

微信扫一扫打赏

微信扫一扫打赏


Fatal error: Uncaught Exception: Cache directory not writable. Comet Cache needs this directory please: `/data/wangzhan/tech.souyunku.com.wp/wp-content/cache/comet-cache/cache/https/tech-souyunku-com/index.q`. Set permissions to `755` or higher; `777` might be needed in some cases. in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php:367 Stack trace: #0 [internal function]: WebSharks\CometCache\Classes\AdvancedCache->outputBufferCallbackHandler() #1 /data/wangzhan/tech.souyunku.com.wp/wp-includes/functions.php(5109): ob_end_flush() #2 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(303): wp_ob_end_flush_all() #3 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(327): WP_Hook->apply_filters() #4 /data/wangzhan/tech.souyunku.com.wp/wp-includes/plugin.php(470): WP_Hook->do_action() #5 /data/wangzhan/tech.souyunku.com.wp/wp-includes/load.php(1097): do_action() #6 [internal function]: shutdown_action_hook() #7 {main} thrown in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php on line 367