CSRF的防禦之HttpOnly

來源:互聯網
上載者:User

標籤:style   blog   http   ar   io   color   os   使用   sp   

CSRF攻擊的全稱是跨站請求偽造(cross site request forgery),是一種對網站的惡意利用,儘管聽起來跟XSS跨站指令碼攻擊有點相似,但事實上CSRF與XSS差別很大,XSS利用的是網站內的信任使用者,而CSRF則是通過偽裝來自受信任使用者的請求來利用受信任的網站。你可以這麼理解CSRF攻擊:攻擊者盜用了你的身份,以你的名義向第三方網站發送惡意請求。CRSF能做的事情包括利用你的身份發郵件、發簡訊、進行交易轉賬等等,甚至盜取你的帳號。

 

 

           

 

  C#代碼實現,本想自己寫一個,但是發現萬能的MSDN上避免有個現成的果斷拿來之,MSDN上的囉嗦:

  

該屬性有助於緩解跨網站指令碼威脅,這種威脅可能導致 Cookie 被竊取。 竊取的 Cookie 可以包含標識網站使用者的敏感資訊,如 ASP.NET 會話 ID 或 Forms 身分識別驗證票證,攻擊者可以重播竊取的 Cookie,以便偽裝成使用者或擷取敏感資訊。 如果相容瀏覽器接收到 HttpOnly Cookie,則用戶端指令碼不能對它進行訪問。
警告說明
將 HttpOnly 屬性設定為 true,並不能防止對網路頻道具有存取權限的攻擊者直接存取該 Cookie。 針對這種情況,應考慮使用安全通訊端層 (SSL) 來提供協助。 工作站的安全也很重要,原因是惡意使用者可能使用開啟的瀏覽器視窗或包含持久性 Cookie 的電腦,以合法使用者的標識擷取對網站的訪問。

  

      

<%@ Page Language="C#" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server">    void Page_Load(object sender, EventArgs e)    {        // Create a new HttpCookie.        HttpCookie myHttpCookie = new HttpCookie("LastVisit", DateTime.Now.ToString());        // By default, the HttpOnly property is set to false         // unless specified otherwise in configuration.        myHttpCookie.Name = "MyHttpCookie";        Response.AppendCookie(myHttpCookie);        // Show the name of the cookie.        Response.Write(myHttpCookie.Name);        // Create an HttpOnly cookie.        HttpCookie myHttpOnlyCookie = new HttpCookie("LastVisit", DateTime.Now.ToString());        // Setting the HttpOnly value to true, makes        // this cookie accessible only to ASP.NET.        myHttpOnlyCookie.HttpOnly = true;        myHttpOnlyCookie.Name = "MyHttpOnlyCookie";        Response.AppendCookie(myHttpOnlyCookie);        // Show the name of the HttpOnly cookie.        Response.Write(myHttpOnlyCookie.Name);    }</script><html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server">    <title>ASP.NET Example</title></head><body><script type="text/javascript">function getCookie(NameOfCookie){    if (document.cookie.length > 0) {     begin = document.cookie.indexOf(NameOfCookie+"=");     if (begin != -1)   {     begin += NameOfCookie.length+1;       end = document.cookie.indexOf(";", begin);      if (end == -1) end = document.cookie.length;      return unescape(document.cookie.substring(begin, end));             }   }return null;  }</script><script type="text/javascript">    // This code returns the cookie name.    alert("Getting HTTP Cookie");    alert(getCookie("MyHttpCookie"));    // Because the cookie is set to HttpOnly,    // this returns null.    alert("Getting HTTP Only Cookie");    alert(getCookie("MyHttpOnlyCookie"));</script> </body></html>

 

 相關資料:http://msdn.microsoft.com/zh-cn/library/ms533046(vs.85).aspx

  http://msdn.microsoft.com/zh-cn/library/ms533046(vs.85).aspx

  http://kb.cnblogs.com/page/115136/

  圖片來自陳康賢<<大型分布式網站架構設計與實踐>>

 

CSRF的防禦之HttpOnly

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.