httpCookie與Cookie安全

来源:http://www.cnblogs.com/HopeGi/archive/2016/09/22/5894928.html
-Advertisement-
Play Games

Web 應用程式使用的 Cookie 個人認為這裡設置的cookie與訪問cookie的安全性關聯大一點,配置節如下 httpOnlyCookies:預設是false,作用是是否禁用瀏覽器腳本訪問cookie。在Form認證時會頒發一個認證票寫在cookie,最開始我以為這裡設置了則可以訪問,結果並 ...


Web 應用程式使用的 Cookie

個人認為這裡設置的cookie與訪問cookie的安全性關聯大一點,配置節如下

<httpCookies domain="String" 
httpOnlyCookies="true|false" 
requireSSL="true|false" />   

httpOnlyCookies:預設是false,作用是是否禁用瀏覽器腳本訪問cookie。在Form認證時會頒發一個認證票寫在cookie,最開始我以為這裡設置了則可以訪問,結果並不是,個人推斷是這個配置節的設置和Form節點里的配置無關。

下麵來弄個測試一下代碼

服務端添加代碼

Response.SetCookie(new HttpCookie("testcookie", "test"));

瀏覽器端添加腳本

$(function myfunction() {

alert(document.cookie);

}); 

如無意外運行結果

接著把httpOnlyCookies設成true

<httpCookies

httpOnlyCookies="true" />

運行結果

另外HttpCookie類中有個屬性是HttpOnly,把它設成true,結果一樣如上圖所示,空的,估計在驗證票中的cookie也是把HttpOnly設成了true,使得就算在配置中設置了仍然無效

Response.SetCookie(new HttpCookie("testcookie", "test") { HttpOnly=true});

查看源碼得知生成驗證票的代碼中FormsAuthentication.GetAuthCookie方法生成HttpCookie 

 

   

下麵一篇摘自MSDN的老外文章說的就是跨站點腳本攻擊的,記憶中好像看蔣金楠老師的《ASP.NET MVC 4 框架揭秘》也提到過相關的東西,現在看就更明白他說得是啥了,只不過文章比較舊了,老是在提IE6 SP1升級了啥啥啥。

Mitigating Cross-site Scripting With HTTP-only Cookies

One of the more common security problems plaguing(脆弱的) Web servers is cross-site scripting. Cross-site scripting is a server-side vulnerability(漏洞) that is often created when rendering user input as HTML. Cross-site scripting attacks can expose sensitive information about the users of the Web site. In order to help mitigate the risk of cross-site scripting, a new feature has been introduced in Microsoft Internet Explorer 6. This feature is a new attribute for cookies which prevents them from being accessed through client-side script. A cookie with this attribute is called an HTTP-only cookie. Any information contained in an HTTP-only cookie is less likely to be disclosed to a hacker or a malicious Web site. The following example is a header that sets an HTTP-only cookie.


Set-Cookie: USER=123; expires=Wednesday, 09-Nov-99 23:12:40 GMT; HttpOnly

This topic briefly explains cross-site scripting, the potential risk of a cookie that can be accessed through script, and how this risk has been mitigated by HTTP-only cookies in Internet Explorer 6 Service Pack 1 (SP1).

Cross-site Scripting

Cross-site scripting is a common server-side vulnerability which allows a hacker to trick a user into disclosing sensitive information that is normally reserved for a specific Web site. The various steps of a cross-site scripting attack can best be explained with a simple example.

An Example of Cross-site Scripting

To understand how cross-site scripting is typically exploited, consider the following hypothetical example.

The A. Datum Corporation runs a Web site that allows you to track the latest price of your stock portfolio. To add a friendly touch, after logging in to the A. Datum Web site, you are redirected to www.adatum.com/default.asp?name=Brian and a server-side script generates a welcome page that says "Welcome Back Brian!". The stocks in your portfolio are stored in a database, and the Web site places a cookie on your computer containing a key to that database. The cookie is retrieved anytime you visit the A. Datum Web site.

A hacker realizes that the A. Datum Web site suffers from a cross-site scripting bug and decides to exploit this to gather some information about you that you'd rather not disclose; the names of the stocks in your portfolio. The hacker sends you an e-mail that claims you've just won a vacation getaway and all you have to do is "click here" to claim your prize. The URL for the hypertext link iswww.adatum.com/default.asp?name=<script>evilScript()</script>. When you click this link, the Web site tries to be friendly by greeting you, but instead displays, "Welcome Back !". What happened to your name? By clicking the link in the e-mail, you've told the A. Datum Web site that your name is <script>evilScript()</script>. The Web server generated HTML with this "name" embedded and sent it to your browser. Your browser correctly interprets this as script, and because client-side script is typical browser functionality, runs the script without prompting you. If this script instructs the browser to send a cookie containing your stock portfolio to the hacker's computer, it quickly complies. After all, the instruction came from the A. Datum Web site which owns that cookie.

The following image demonstrates this concept visually by showing the process in five steps. First, the user clicks a link embedded in e-mail from the hacker (step 1). This generates a request to a Web site (step 2) which, because of a cross-site scripting bug, complies with the request and sends malicious script back to the user's browser (step 3). The script host executes the malicious code (step 4) and sends the sensitive data to the hacker's computer (step 5).

There are many variations on this example of cross-site scripting. For more examples and further details see Cross-site Scripting.

Protecting Data with HTTP-only Cookies

To mitigate the risk of information disclosure with a cross-site scripting attack, a new attribute is introduced to cookies for Internet Explorer 6 SP1. This attribute specifies that a cookie is not accessible through script. By using HTTP-only cookies, a Web site eliminates the possibility that sensitive information contained in the cookie can be sent to a hacker's computer or Web site with script.

A cookie is set on the client with an HTTP response header. The following example shows the syntax used in this header.


Set-Cookie: <name>=<value>[; <name>=<value>]
[; expires=<date>][; domain=<domain_name>]
[; path=<some_path>][; secure][; HttpOnly]

Note  The HttpOnly attribute is not case sensitive.

If the HttpOnly attribute is included in the response header, the cookie is still sent when the user browses to a Web site in the valid domain. The cookie cannot be accessed through script in Internet Explorer 6 SP1, even by the Web site that set the cookie in the first place. This means that even if a cross-site scripting bug exists, and the user is tricked into clicking a link that exploits this bug, Windows Internet Explorer does not send the cookie to a third party. The information is safe.

Note  The use of HTTP-only cookies is one of several techniques that, when used together, can mitigate the risk of cross-site scripting. Used alone, it cannot completely eliminate the danger of cross-site scripting.

Browser Support for HTTP-only Cookies

If a Web site sets an HTTP-only cookie on a browser that does not support HTTP-only cookies, the cookie is either ignored or downgraded to a traditional, scriptable cookie. This leaves information vulnerable to attack for users of some browsers.

For a company intranet Web page, administrators could require the use of a browser that recognizes HTTP-only cookies for all users. This ensures that information is not disclosed with a cross-site scripting bug.

For a public Web site where it is important to support multiple browsers, consider using client-side script to determine the browser version for each visitor. The Web site can restrict sensitive information to visitors using browsers that mitigate cross-site scripting attacks for cookies. Visitors with browsers that do not support HTTP-only cookies can be given limited information or functionality along with a request to upgrade their software.

When determining the browser version of Internet Explorer, it is important to keep in mind that the user agent string for Internet Explorer 6 SP1 is identical to the user agent string for Internet Explorer 6. Client-side script must also check the minor version number with theappMinorVersion property of the navigator object to determine whether Internet Explorer 6 SP1 is installed.

   

來自 <https://msdn.microsoft.com/zh-CN/Library/ms533046.aspx>


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • • 微博: 小樣兒老師2015 關於 嚴格來說,本文題目應該是我的單片機、DSP和ARM+Linux學習之路,但這個寫法實在太繞口——況且嵌入式往往暗指MCU,DSP,ARM等微處理器加上嵌入式操作系統,所以我認為本文題目是合理的。 這篇文章講了什麼? • 我這些年學單片機、DSP和ARM+Linu ...
  • 創建基於對話框的Win32應用程式(三) —— Checkbox的應用、窗體置頂、設置圖標 上一節創建的窗體應用程式中,我們用到了Button和StaticText這兩個控制項。這一節中我們將學習使用Checkbox控制項,並實現將窗體置頂的功能。 17、首先切換到Reasource View(Ctrl ...
  • 思路: 1、查看有無安裝過mysql 或 2、查看有無安裝包 3、安裝mysql服務端 4、啟動&&停止 a、設置資料庫預設字元 在mysql配置文件/etc/my.cnf中加入default-character-set=utf8 b、設置開機自啟動 c、啟動mysql 5、登錄 a、創建root管 ...
  • 今天,開始記錄我的前端技術生涯! 今天,開始記錄我的博客園生涯! 今天,技術!產品!技術+產品=能力!!! 開啟新的生活!!! ...
  • 創建基於對話框的Win32應用程式(二) —— Button的應用、新建子窗體 可以發現上一節創建的窗體中,點擊OK和Cancel兩個按鈕是沒有任何反應的。現在我們來為他們添加退出對話框的功能。 6、首先定義命令消息響應函數(Dlg_OnCommand)如下: 其中IDOK和IDCANCEL分別是按 ...
  • 一.創建一個空項目 請查看 新建 .NET Core 項目 -- Hello World! 一節,新建一個項目: 二.添加引用並修改配置為 Web API (.NET Core 已將 MVC/Web API 底層代碼及表層編程介面合二為一) 修改 .vscode\launch.json 文件 代碼如 ...
  • 下麵介紹各種List的sort的用法與比較 首先,我們建一個People的實體,有name、age、sex的屬性,我們要排序的欄位是年齡age 新建一個實體類 新建list的數據 1. 第1種排序方法,使用 IComparer 可以看到第一種方法比價麻煩,要新建一個新的類來做 2. 第2種排序方法, ...
  • 一、併發的基本含義 在操作系統中,併發是指一個時間段中有幾個程式都處於已啟動運行到運行完畢之間,且這幾個程式都是在同一個處理機上運行,但任一個時刻點上只有一個程式在處理機上運行。 在關係資料庫中,允許多個用戶同時訪問和更改共用數據的進程。SQL Server 使用鎖定以允許多個用戶同時訪問和更改共用 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...