Advanced SQL Injections
Hunting for SQL Errors
Enabling PostgreSQL Logging
Another way to identify the SQL queries which are run, as well as debug your payloads when developing an exploit is to enable SQL logging.
To do so in PostgreSQL, we first need to find postgresql.conf. Usually it is located in /etc/postgresql/<version>/main/, but if you can't find it there you can run:
[!bash!]$ find / -type f -name postgresql.conf 2>/dev/null
/etc/postgresql/13/main/postgresql.conf
Once we've located the file, we have to make the following changes to the file:
- Change
#logging_collector = offtologging_collector = on. This enables the logging collector background process [source]. -
#log_statement = 'none'tolog_statement = 'all'. This makes it so all statement types (SELECT, CREATE, INSERT, ...) are logged [source]. - Uncomment
#log_directory = '...'to define the directory in which the logfiles will be saved [source]. - Uncomment
#log_filename = '...'to define the filename in which logfiles will be saved [source].
Once the changes have been saved, restart PostgreSQL like so:
[!bash!]$ sudo systemctl restart postgresql
At this point, the log file(s) should start appearing in the folder defined by log_directory. We can watch the log messages in near-realtime with the following command:
[!bash!]$ sudo watch -n 1 tail <log_directory>/postgresql-2023-02-14_081533.log
<SNIP>
2023-02-14 09:06:04.819 EST [22510] bbuser@bluebird LOG: execute <unnamed>: SELECT * FROM users WHERE username = $1
2023-02-14 09:06:04.819 EST [22510] bbuser@bluebird DETAIL: parameters: $1 = 'bmdyy'
2023-02-14 09:06:10.423 EST [22510] bbuser@bluebird LOG: execute <unnamed>: SELECT * FROM users WHERE username = $1
2023-02-14 09:06:10.423 EST [22510] bbuser@bluebird DETAIL: parameters: $1 = 'admin'
2023-02-14 09:06:12.999 EST [22510] bbuser@bluebird LOG: execute <unnamed>: SELECT * FROM users WHERE username = $1
2023-02-14 09:06:12.999 EST [22510] bbuser@bluebird DETAIL: parameters: $1 = 'test'
2023-02-14 09:06:16.688 EST [22510] bbuser@bluebird LOG: execute <unnamed>: SELECT * FROM users WHERE username = $1
2023-02-14 09:06:16.688 EST [22510] bbuser@bluebird DETAIL: parameters: $1 = 'itsmaria'
VPN Servers
Warning: Each time you "Switch", your connection keys are regenerated and you must re-download your VPN connection file.
All VM instances associated with the old VPN Server will be terminated when switching to
a new VPN server.
Existing PwnBox instances will automatically switch to the new VPN server.
PROTOCOL
/ 1 spawns left
Questions
Answer the question(s) below to complete this Section and earn cubes!
Click here to spawn the target system!
Target:
Click here to spawn the target system!
SSH to with user "student" and password "academy.hackthebox.com"
+10 Streak pts
Table of Contents
Introduction
Introduction to PostgreSQLIdentifying Vulnerabilities
Decompiling Java Archives Searching for Strings Live-debugging Java Applications Hunting for SQL ErrorsAdvanced SQL Injection Techniques
Common Character Bypasses Error-Based SQL Injection Second-Order SQL InjectionPostgreSQL-Specific Techniques
Reading and Writing Files Command ExecutionDefending Against SQL Injection
Preventing SQL Injection VulnerabilitiesSkills Assessment
Skills AssessmentMy Workstation
OFFLINE
/ 1 spawns left