asp.net MVC 安全性[筆記]

來源:互聯網
上載者:User

標籤:blog   http   io   os   使用   ar   java   strong   for   

1. 跨站指令碼(XSS)

1.1 介紹

1.1.1 被動注入,利用輸入html,javascript 等資訊偽造連結,圖片等使用提交資訊,調轉頁面等

1.1.2 主動注入,駭客主動參與攻擊,不會傻等倒黴的使用者上鉤

 

1.2 防禦

1.2.1 HTML 編碼

Html.Encode

1.2.2 HTML 屬性編碼

Html.AttributeEncode

1.2.3 url 編碼

URL.Encode

1.2.4 java script 編碼

Ajax.JavaScriptStringEncode

1.2.5 CSS 編碼

 

1.3 使用AntiXSS的NuGet包

install-package AntiXSS

1.3.1 AntiXss使用的是白名單,而asp.net預設是黑名單,著用AntiXSS的安全性比ASP.NET 高

1.3.2 AntiXss注重安全性漏洞, asp.net注重HTML頁面的不被破壞

1.3.3 使用

@using Microsoft.Security.Application

@Encoder.JavaScriptEncode();

 

2.請求偽造(CSPF)

2.1 介紹

 在網站上通過圖片連結等偽造銀行等機構的請求(混淆代理),例如現在很多銀行用的get 請求,url 會在地址欄上顯示,這就給了駭客機會,駭客可以通過任何吸引你的內容,讓你點擊,偽造相同的請求發給銀行,正好你銀行在登陸狀態,就可以轉賬盜取金錢。

2.2 令牌驗證

通過驗證使用者是否資源提交網站資料來達到防禦目的,可以在Html隱藏元素來實現。

MVC 中

@Html.AntiForgeryToken(), 會輸出一個加密值作為隱藏元素

在提交表單時,ActionFilter就會驗證2個值是否匹配:

[ValidateAntiForgertToken]

public ActionResult Register(..)

2.3 等冪的get 請求

僅通過post 請求來修改資料或內容,就可有效防禦全部偽造攻擊。

2.3 HttpReferrer 驗證

ActionFilter 來阻止CSRF攻擊:

[csharp] view plaincopyprint? 
  1. public class IsPostedFromThisSiteAttribute : AuthorizeAttribute  
  2. {  
  3. public override void OnAuthorize(AuthorizationContext filterContext)  
  4. {  
  5. if (filterContext.HttpContext != null)  
  6. {  
  7. if (filterContext.HttpContext.Request.UrlReferrer == null)  
  8. throw new System.Web.HttpException("Invalid submission");  
  9. if (filterContext.HttpContext.Request.UrlReferrer.Host !=  
  10. "mysite.com")  
  11. throw new System.Web.HttpException  
  12. ("This form wasn‘t submitted from this site!");  
  13. }  
  14. }  
  15. }  

 

然後再Register 上面添加自訂的過濾器

 

[csharp] view plaincopyprint? 
  1. [IsPostedFromThisSite]  
  2. public ActionResult Register(…)  

 

 

3.cookie盜竊

3.1 介紹

利用XSS擷取使用者cookie,用使用者的Id登陸目標網站

3.2HttpOnly 阻止

web.config 裡設定

<HttpCookies httpOnlyCookies="true" ...>

3.3單獨設定httponly

Response.Cookies["MyCookie"].HttpOnly=true;

 

4.重複提交

4.1 介紹

MVC 尊順命名約束把輸入元素映射到模型屬性中,惡意使用者可以向查詢字串或提交的表單中添加其他屬性(模型中存在,但使用者沒權操作的屬性)來幹預表單提交。甚至可以得到其他賬戶的許可權。

4.2 使用bind 特性防禦重複提交,下面展示的Product 模型只能映射name 和age 屬性。無權操作其他的屬性。

[Bind(Include="Name","Age")]

pulbic class Product{

......

}

4.3 使用UpdateModel 後TryUpdataModel 的重載本版接受bind 列表。

UpdateModel(product,"product",new string[] {"Name","Age"} ")

4.4 避免直接綁定試圖

使用view model.

asp.net MVC 安全性[筆記]

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.