text
stringlengths
0
1.99k
SELECT
STRING_AGG(
CONCAT_WS(
',',
attname,
typname,
attlen,
attalign
),
';'
)
FROM pg_attribute
JOIN pg_type
ON pg_attribute.atttypid = pg_type.oid
JOIN pg_class
ON pg_attribute.attrelid = pg_class.oid
WHERE pg_class.relname = 'TABLE_NAME';
--------------------------------------------------------------------------
---[ 2.3 - Cold and Hot data storage
All of the above objects make up the DBMS' cold storage. To access the
data in cold storage through a query, Postgres must first load it in the
RAM cache, a.k.a. hot storage.
The following diagram shows a rough and simplified flow of how the
PostgreSQL accesses the data:
+------------------+ +--------+ +------+ +------+
|Table in RAM cache|------>|Filenode|--+--->|Page 1|---+--->|Item 1|
+------------------+ +--------+ | +------+ | +------+
| |
| +------+ | +------+
+--->|Page 2| +--->|Item 2|
| +------+ | +------+
| ... | ...
| +------+ | +------+
+--->|Page n| +--->|Item n|
+------+ +------+
The DBMS periodically flushes any changes to the data in hot storage to
the filesystem.
These syncs may pose a challenge to us! Since we can only edit the cold
storage of a running database, we risk subsequent hot storage syncs
overwriting our edits. Thus, we must ensure that the table we want to
overwrite has been offloaded from the cache.
--------------------------------------------------------------------------
# -----------------------------
# PostgreSQL configuration file
# -----------------------------
...
# - Memory -
shared_buffers = 128MB # min 128kB
# (change requires restart)
...
--------------------------------------------------------------------------
The default cache size is 128MB. So, if we stress the DB with expensive
queries to other tables/large objects before the flush, we might overflow
the cache and clear our target table from it.
---[ 2.4 - Editing filenodes offline
I've created a tool to parse and modify data stored in filenodes, which
functions independently of the Postgres server that created the filenodes.
We can use it to overwrite target table rows with our desired values.
The editor supports both datatype-assisted and raw parsing modes. The
assisted mode is the preferred option as it allows you to edit the data
safely, without accidentally messing up the whole filenode structure.
The actual parsing implementation is way too lengthy to discuss in this
article, but you can find the sources on GitHub[10], or the source code
in this article if reading online, if you want to dig deeper into it.
You can also check out this article[12] on parsing filenodes in Golang.
--[ 3 - Updating the PostgreSQL data without UPDATE
---[ 3.0 - Identifying target table
So, we are looking to escalate our permissions to those of a DBMS
superuser. Which table should we aim to modify? All Postgres permissions
are stored in the internal table "pg_authid". All CREATE/DROP/ALTER
statements for new roles and users actually modify this table under the
hood. Let's inspect it in a PSQL session under the default super-admin
user:
--------------------------------------------------------------------------
postgres=# SELECT * FROM pg_authid; \x
-[ RECORD 1 ]--+------------------------------------
oid | 3373