MVC3----网站安全性
更新:HHH   时间:2023-1-7


 

=======================================一、防止跨站脚本***(XSS)

 

①: @Html.Encode("<script>alert('123')</script>")
编码后:&lt;script&gt;alert(&#39;123&#39;)&lt;/script&gt;

②: @Html.AttributeEncode("<script>alert('123')</script>")
编码后:&lt;script>alert(&#39;123&#39;)&lt;/script>

③: @Html.JavascriptEncode()

③: 使用antixss库防御

 

                                                                                                               

=======================================二、防止跨站请求伪造(CSRF)

 

①: 令牌验证(用于表单验证)
在提交表单中加上@Html.AntiForgeryToken(),在控制器中加上[ValidateAntiforgeryToken]

②: HttpReferrer验证(get、post)

新建一个类,继承AuthorizeAttribute(在提交的时候验证):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace SchoolManageDomw.Models
{
    public class IsPostedThisSiteAttribute:AuthorizeAttribute
    {
        public override void OnAuthorization(AuthorizationContext filterContext)
        {
            if (filterContext.HttpContext != null)
            {
                if (filterContext.HttpContext.Request.UrlReferrer == null)
                    throw new Exception("客户端请求的信息为空");
                if (filterContext.HttpContext.Request.UrlReferrer.Host != "localhost")//MySite.com
                    throw new Exception("不安全的请求");
            }
        }
    }
}

 控制器里使用:

        [IsPostedThisSite]
        public ActionResult LogOff()
        {
            FormsAuthentication.SignOut();
            return RedirectToAction("Index", "Home");
        }

 

 

=======================================三、cookie窃盗

 

1,cookie主要有两种形式:
①:会话cookie:会话cookie存储在浏览器的内容中,在浏览器的每次请求中通过http头传递
②:持久性cookie:持久性cookie存储于计算机的实际文件中,并与会话cookie以相同的方式传递

 

2,使用HttpOnly阻止cookie窃盗
①:web.config文件中对所有cookie进行设置
(停止脚步对站点的cookie访问)
<system.web>
    <httpCookies domain="" httpOnlyCookies="true" requireSSL="false"/>
</system.web>
②:在程序中为编写的每一个cookie单独设置
(除了服务器修改或设置cookie之外,其他一些对cookie的操作均无效)
Response.Cookies["MyCookie"].Value = "userid";
Response.Cookies["MyCookie"].HttpOnly = true;

 

=======================================四、重复提交

 

 ①:使用白名单指定允许绑定的字段
[Bind(Include="Name,Content")]
②:使用黑名单排除禁止绑定的字段
[Bind(Exclude="Price,OrderID")]
可以用在模型类中,也可以用在控制器操作参数中


例:
模型中使用:
[Bind(Include="Name,Content")]
public class product()
控制器操作参数中使用:
 public ActionResult Edit([Bind(Include = "STU_NAME,STU_MONEY")]PersonError p, string returnUrl)

 

 =======================================五、开放重定向

1,例子:

比如你是工商银行,我是一个罪犯。
你的网站叫 www.icbc.com.cn,我建立了一个网站叫 www.icbc888.cn,我让网站看起来和你的一样。
我在论坛上发布这样一个帖子:
快来啊,工商银行派发红包啦,点这里:http://www.icbc.com.cn/account/login?returnUrl=www.icbc888.cn。用户看起来会觉得这个链接是工商银行的,但是当它点进去以后,会被跳转到www.icbc888.cn,后面的事情你可以想象到。

 

2,控制器代码(登录):

returnUrl:重定向的路径

        [HttpPost]
        public ActionResult LogOn(LogOnModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                if (Membership.ValidateUser(model.UserName, model.Password))
                {
                    FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
                   //做安全判断
                    if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                        && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                    {
                        return Redirect(returnUrl);
                    }
                    else
                    {
                        return RedirectToAction("Index", "Home");
                    }
                }
                else
                {
                    ModelState.AddModelError("", "提供的用户名或密码不正确。");
                }
            }
            // 如果我们进行到这一步时某个地方出错,则重新显示表单
            return View(model);
        }

 

=======================================六、适当的错误报告和堆栈跟踪 

 

<customErrors mode="Off"></customErrors>
On:服务器开发的最安全选项,因为它总是隐藏错误提示消息
RemoteOnly:向大多数用户展示一般的错误提示消息,但向拥有服务器访问权限的用户
展示完整的错误提示消息
Off:最容易受到***的选项,它向访问网站的每个用户展示详细的错误提示消息

<customErrors mode="On" redirectMode="ResponseRedirect"  defaultRedirect="~/Home/Index">
<!--服务器返回404错误跳转的指定页面-->
<error redirect="~/Home/Index" statusCode="404"/>
</customErrors>

 

返回编程语言教程...