標籤: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?
- public class IsPostedFromThisSiteAttribute : AuthorizeAttribute
- {
- public override void OnAuthorize(AuthorizationContext filterContext)
- {
- if (filterContext.HttpContext != null)
- {
- if (filterContext.HttpContext.Request.UrlReferrer == null)
- throw new System.Web.HttpException("Invalid submission");
- if (filterContext.HttpContext.Request.UrlReferrer.Host !=
- "mysite.com")
- throw new System.Web.HttpException
- ("This form wasn‘t submitted from this site!");
- }
- }
- }
然後再Register 上面添加自訂的過濾器
[csharp] view plaincopyprint?
- [IsPostedFromThisSite]
- 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 安全性[筆記]