標籤:abc direct pass 過濾 開頭 already pos string 上傳
參考文章 : https://www.mudoom.com/php%E5%AE%89%E5%85%A8%E7%BC%96%E7%A0%81/
SQL注入
造成sql注入的原因是因為程式沒有過濾使用者輸入的內容, 本質上是在執行sql時資料和語句混淆,下面舉一個例子:
萬能密碼和萬能使用者名稱
正常寫法 : select * from table_name where username = ‘admin‘ and password = ‘admin‘
萬能密碼 : 利用輸入表單構造sql, select * from table_name where username = ‘admin‘ and password = ‘admin‘ or 1 = 1, 要防止非常簡單, 對密碼進行md5加密比對即可
萬能使用者名稱 : select * from table_name where username = ‘admin‘ or 1 = 1 and password = ‘admin‘ , 絕對不要相信使用者輸入的任何資料。 利用addslashes()轉義, 對單引號, 雙引號, NULL, 反斜線(\)進行轉義.
預防資料庫攻擊正確做法: 關閉所有錯誤提示!!!
1. 不能使用mysqli或PDO時, 可以臨時使用addslashes() 轉義, 前提是資料庫為utf-8編碼, 並且參數位於‘‘內
2. 使用mysqli或pdo先行編譯處理sql, 原理是命令與參數分兩次發送到MYSQL, 這樣MYSQL就能識別參數與命令
$username = ‘root‘;$password = ‘1234abcd‘;$driver_options = array( PDO::MYSQL_ATTR_INIT_COMMAND => ‘SET NAMES UTF8‘, );$pdo = new PDO($dsn, $username, $password, $driver_options);//1.編譯統一的結構$sql = "insert into team values (null, :team_name)";$stmt = $pdo->prepare($sql);$data_list = array( array(‘name‘=>‘國安‘), array(‘name‘=>‘綠地‘), array(‘name‘=>‘恒大‘), array(‘name‘=>‘建業‘), array(‘name‘=>‘魯能‘), array(‘name‘=>‘申花‘), );foreach($data_list as $row) { //2.綁定資料到中間編譯結果 $stmt->bindValue(‘:team_name‘, $row[‘name‘]); //3.執行 $result = $stmt->execute(); var_dump($result);}View Code
3.自己寫一個prepared(不推薦)
function prepare( $query, $args ) { if ( is_null( $query ) ) return; // This is not meant to be foolproof -- but it will catch obviously incorrect usage. if ( strpos( $query, ‘%‘ ) === false ) { _doing_it_wrong( ‘wpdb::prepare‘ , sprintf ( __( ‘The query argument of %s must have a placeholder.‘ ), ‘wpdb::prepare()‘ ), ‘3.9‘ ); } $args = func_get_args(); array_shift( $args ); // If args were passed as an array (as in vsprintf), move them up if ( isset( $args[ 0] ) && is_array( $args[0]) ) $args = $args [0]; $query = str_replace( "‘%s‘", ‘%s‘ , $query ); // in case someone mistakenly already singlequoted it $query = str_replace( ‘"%s"‘, ‘%s‘ , $query ); // doublequote unquoting $query = preg_replace( ‘|(?<!%)%f|‘ , ‘%F‘ , $query ); // Force floats to be locale unaware $query = preg_replace( ‘|(?<!%)%s|‘, "‘%s‘" , $query ); // quote the strings, avoiding escaped strings like %%s array_walk( $args, array( $this, ‘escape_by_ref‘ ) ); return @ vsprintf( $query, $args );}View Code
XSS攻擊
1190000005032978
反射型xss 和 儲存型xss
主要目的:
- 盜取cookie, 擷取敏感資訊
- 冒充他人進行操作, 如加好友, 購物, 發訊息...
- 在訪問量極大的頁面的xss可以攻擊一些小網站,達到DDos的效果
- ...
防禦
堅決不相信任何使用者的輸入。
- 在輸出時使用htmlspecialchars()過濾
- 針對連結類型(片、超鏈等)的輸出也可使用htmlspecialchars進行安全過濾,但如果變數是整個URL則應檢查這個變數是否以http或https開頭,如果不是則自動補齊避免出現偽協議類的XSS攻擊。(例子:<a href=”data:text/html;base64,PHNjcmlwdD5hbGVydCgxKTs8L3NjcmlwdD4=”>xss</a>)
- 富文字編輯器採用白名單, 再針對可能出現xss的標籤進行安全過濾
CSRF
https://www.ibm.com/developerworks/cn/web/1102_niugang_csrf/
攻擊者偽造請求, 舉個例子:
a.com 有一個標籤<img src=”http://b.com?action=del&id=1″>, 使用者請求a.com, 將會產生一個請求 :http://b.com?action=del&id=1,, 瀏覽器會以目前使用者在b.com上的身份(cookie資訊)發起刪除操作
防禦:
1. 對于敏感操作使用隨機token驗證。 token必須足夠隨機,驗證後立即刪除,盡量以post提交防止泄露, 原理: 可以在服務端的session裡面存一個token, 然後在使用者提交的表單中也帶一個token, 這個token不存在cookie中, 攻擊者無法偽造此token, 也就攻擊失敗了。 通常做法是在頁面載入時遍曆整個dom樹,在a和form標籤後面加上token.
2.驗證HTTP Referer欄位, 該欄位記錄了http請求來源地址, 但是不能確保萬無一失,依賴於第三方瀏覽器。
3.在HTTP頭中自訂屬性並驗證
檔案上傳漏洞
白名單上傳, 結合MIME和尾碼檢查檔案類型
使用隨機檔案名稱儲存上傳的檔案,避免因終止符造成的檔案名稱中斷
web安全知識