5

golang gorm框架的sql注入漏洞

 1 year ago
source link: https://studygolang.com/articles/29539?fr=sidebar
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

golang gorm框架的sql注入漏洞

keluda · 2020-06-27 18:32:50 · 6153 次点击 · 预计阅读时间 1 分钟 · 大约8小时之前 开始浏览    
这是一个创建于 2020-06-27 18:32:50 的文章,其中的信息可能已经有所发展或是发生改变。

最近的golang项目,orm框架选择的是gorm。今天开始做单元测试,顺便手动检查一下安全问题。结果发现gorm框架是直接拼接的sql语句。gorm框架在golang生态中的影响力非常大。在测试中发现这个问题也感觉比较意外。下面是执行测试的结果:

首先是where查询,在这里我使用的语句是api是Where("user_email=?", email),正常测试时,我输入的是随便一个字符串“[email protected]”,生成的sql语句是:“SELECT * FROM `users` WHERE `users`.`deleted_at` IS NULL AND ((user_email='[email protected]')) ORDER BY `users`.`id` ASC LIMIT 1”。如果不做安全方面的校验,这个生成的语句看上去没有任何问题。

下面我修改一下email的值为"[email protected]' or '1'='1",然后调用这个api,生成的sql语句是:SELECT * FROM `users` WHERE `users`.`deleted_at` IS NULL AND ((user_email='[email protected]' or '1'='1')) ORDER BY `users`.`id` ASC LIMIT 1.在这里,我在where后面调用的是first(gorm链式调用),所以最后生成的sql语句后面又limit1。可以看出,现在生成的语句是被sql注入了的。

后面我又测试了create api,直接传入的要创建的实体,结果依然生成了被注入的sql语句。时间问题,不再赘述。

软件开发接单,软件开发培训,QQ:2695827051


有疑问加站长微信联系(非本文作者)

280

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK