
There are many ways to accomplish this. To get a SQL injection to work, the attacker must first inject SQL code and then subvert the web application logic by changing the original query or executing a completely new one. First, the attacker has to inject code outside the expected user input limits, so it does not get executed as simple user input. In the most basic case, this is done by injecting a single quote (') or a double quote (") to escape the limits of user input and inject data directly into the SQL query.
Once an attacker can inject, they have to look for a way to execute a different SQL query. This can be done using SQL code to make up a working query that executes both the intended and the new SQL queries. There are many ways to achieve this, like using stacked queries or using Union queries. Finally, to retrieve our new query's output, we have to interpret it or capture it on the web application's front end.
TBH a lot of this information is refresher.
Some info abot non-relatational databases:
- i.e. NoSQL
No tables, rows, columns or prime keys / relationships / schemas.
NoSQL dbs store data using various storage models depending on the data stored. Lack of defined structure = scalable + flexible. Data not well-defined / structured = nosql good candidate.
- Key-value
- Document based
- Wide-Column
- Graph MongoDB is the most popular exxample of this
Intro MySQL
┌──(root㉿kali)-[~]
└─# mysql -u root -ppassword -P 51745 -h 94.237.52.98 --skip-ssl
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 6
Server version: 10.7.3-MariaDB-1:10.7.3+maria~focal mariadb.org binary distribution
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Support MariaDB developers by giving a star at https://github.com/MariaDB/server
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| employees |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.106 sec)
MariaDB [(none)]>
SHOW GRANTS to see permissions
SHOW CREATE TABLE tablename to get a table ddl
Web application SQL usage
$conn = new mysqli("localhost", "root", "password", "users");
$query = "select * from logins";
$result = $conn->query($query);
Query output stored in $result
Web apps typically use user-input to retrieve data. IE user uses search function to search for other users, input passed to web application, which uses the input to searech databases
$searchInput = $_POST['findUser'];
$query = "select * from logins where username like '%$searchInput'";
$result = $conn->query($query);
If not securely coded, it can lead to a sql injection vulnerability.
The example above does not use any input sanitization.
Review point; sql injection is just escaping the bounds of the web application code logic to execute arbitrary sql statements against the backend server.
 In simple cases:
- output of both intended + new query may be printed from front end and be directly read.
- This is known as
in-bandSQL injection Union-basedandError-based
- This is known as
Union based sql inj, we may have to specify exact location which we can read so the query will direct output Error based sql inj, used when we can get php/sql errors in the front end and intentionally cause a sql error that returns output of our query.
More complicated case we may not get output printed so we can utilize sql logic to retrieve output character by character - known as Blind injection
- Boolean based, and
- Time based
Boolean based - Use SQL conditional statements to control whether page returns any output at all (i.e. original query response)
- If query conditional statement returns true
Time-based
- SQL conditions that delay page response if conditional statement returns true using Sleep() function
Sometimes may not have direct access to output whatsoever - so may have to direct output to a remote location (such as a DNS record) and attempt to retrieve it from there. Known as an out-of-band SQL injection