Security

基本資安

  • CSRF,XSS,SQL Injection,HTML Injection

  • 以 Node.js 舉例,https://blog.risingstack.com/node-js-security-checklist/

    • 設定好你的 HTTP Headers

      • Strict-Transport-Security enforces secure (HTTP over SSL/TLS) connections to the server

      • X-Frame-Options:provides clickjacking protection

      • X-XSS-Protection:enables the Cross-site scripting (XSS) filter built into most recent web browsers

      • X-Content-Type-Options:prevents browsers from MIME-sniffing a response away from the declared content-type

      • Content-Security-Policy:prevents a wide range of attacks, including Cross-site scripting and other cross-site injections

    • Brute Force Protection:例如,限制登入嘗試次數來避免暴力破解

    • 管理好 Session

      • Cookie Flags

        • secure:讓瀏覽器只允許透過 HTTPS 傳 cookie

        • HttpOnly:避免 XSS,讓 cookie 不被 JavaScript 操作存取

      • Cookie Scope

        • domain

        • path

        • expires

    • CSRF(Cross-Site Request Forgery)

    • 資料驗證

      • XSS(Cross-Site Scripting)

        • 分兩種,Reflected 和 Stored Cross site scripting

        • 總之記得驗證使用者回傳的資料,以及幫使用者清掉存在用戶端的資料

      • SQL Injection

        • 總之就是要驗證資料。也要防止被知道後端是怎麼存取資料的,權限也不要開到最大

      • Command Injection

      • 透過安全的傳輸機制

        • SSL Version, Algorithms, Key length

          • ciphers, keys and renegotiation is properly configured

          • certificate validity

        • HSTS(HTTP Strict Transport Security)

      • 防止阻斷式攻擊(Denial of Service)

        • 短時間內的大量耗資源請求就鎖帳號

        • 小心 Regular Expression 式的 DoS 攻擊

      • 正確地處理 Error/Exception,這些訊息有時會不小心透露出敏感資訊

  • 八大前端安全問題(文章日期:2017.10.31),

    http://insights.thoughtworks.cn/eight-security-problems-in-front-end/

    • XSS

    • iframe

    • Clickjacking

    • 瀏覽器自行判斷了內容的 type

    • 前後端模組漏洞

    • SSL Stripping

    • 用戶端數據洩漏

    • CDN 汙染

  • - XSS(長期為 OWASP Web Application Top 10 前三名)
    
        - 目前的前端框架都做了 URL、JavaScript、HTML、CSS、JSON 編碼,幫你省工
    
        - 其他的措施:設定 CSP HTTP Header、開啟瀏覽器 XSS 防禦選項
    - iframe
    
        - 最簡單措施,加上 sandbox
            - <iframe sandbox src="..."> ... </iframe>
        - 「除了允許顯示靜態資源以外,其他什麼都做不了。比如不准提交表單、不准彈窗、不准執行腳本等等,連Origin都會被強制重新分配一個唯一的值,換句話講就是iframe中的頁面訪問它自己的伺服器都會被算作跨域請求。」
    
        - 常用的參數
            - allow-forms:提交 form
            - allow-popups:彈出新的視窗或者標籤頁(例如,window.open(),showModalDialog(),target=”_blank” 等等)
            - allow-scripts:執行 JavaScript
            - allow-same-origin:同源策略
    - Clickjacking
    
        - 防禦方式:X-Frame-Options 這個 HTTP header 的值設為 DENY
    
    - 瀏覽器自行判斷了內容的 type
    
        - 惡意使用者將 JavaScript 偽裝成圖片成功上傳進入伺服器,其他使用者要求該內容時,儘管伺服器會在 Content-Type Header 建議瀏覽器用圖片方式渲染惡意資料,但瀏覽器發現惡意資料是 JavaScript,會自行執行這段 script
    
        - 防禦方式:設定好 X-Content-Type-Options 這個 HTTP header
    
    - 前後端模組漏洞
    
        - 現代 web 開發都用很多模組、框架
    
        - 安全檢查工具:NSP(Node Security Platform),Snyk
    - SSL Stripping
    
        - 因為使用者在第一次輸入網址的時候不一定會輸入 「https:,,」來建立連線,所以在跳轉到 HTTPS 連線的途中,被惡意攔截了
    
        - 防禦方式
    
            - 使用 HSTS(HTTP Strict Transport Security),透過下列的 HTTP Header 以及一份 preload 清單,告知瀏覽器,通信時強制性的使用 HTTPS,這樣就不會有 HTTP 到 HTTPS 的跳轉;也會提醒使用者「是否要繼續不安全的連線」
                - Strict-Transport-Security: max-age=<seconds>; includeSubDomains; preload
    
    - 用戶端數據洩漏
    
        - 儘管前端有將使用者的資料加密再儲存,但也只是提高了一些破解成本而已
    
        - 建議方式:盡量不要儲存敏感訊息在用戶端
    - CDN(Content Delivery Networks)汙染
    
        - 防禦方式
    
            - 使用瀏覽器的 SRI(Subresource Integrity)功能。(Subresource 就是 HTML 頁面中通過 <script> 和 <link> 所指定的資源檔。)
    
            - 每個資源檔都可以有一個 SRI 值,該值由兩部分組成,減號(-)左邊是生成 SRI 值用到的雜湊演算法名,右邊是 Base64 編碼的該資源檔 Hash 值。
    
            - 例:<script src="https://example.js" integrity="sha384-eivAQsRgJIi2KsTdSnfoEGIRTo25NCAqjNJNZalV63WKX3Y51adIzLT4So1pk5tX"></script>

Last updated