如何克服Asp.net中存储的跨站点脚本漏洞c#

时间:2017-04-21 08:01:32

标签: c# asp.net gridview eval xss

正在处理的代码部分易受存储的XSS攻击。以下是代码。

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"     OnRowCancelingEdit="GridView1_RowCancelingEdit"    
            OnRowEditing="GridView1_RowEditing"  OnRowUpdating="GridView1_RowUpdating" OnRowDeleting="GridView1_OnRowDeleting"  OnPageIndexChanging="GridView1_PageIndexChanging"  Width ="1000px" class="grid">


       <Columns>   

            <asp:TemplateField HeaderText="User Name">   
                <ItemTemplate>   
                    <asp:Label ID="lbl_Name" runat="server" Text='<%#Eval("Uname") %>'></asp:Label>   
                </ItemTemplate>   
                <EditItemTemplate>   
                    <asp:TextBox ID="txt_Name" runat="server" Text='<%#Eval("Uname") %>'></asp:TextBox>   //this is the line vulnerable to XSS
                </EditItemTemplate>   
            </asp:TemplateField>       </columns>
</asp:GridView>  

背后的代码

  DataTable dt = new DataTable();
        try
        {
            SqlConnection con = new SqlConnection(conn);
            con.Open();
            SqlDataAdapter adapt = new SqlDataAdapter("Select Uid,Uname,Utype,Uemail,ClientName,ProjectName,Ulog from usrtable where ClientName='" + clientname + "' and Utype='Admin' or ClientName='" + clientname + "'and Utype='Normal'", con);
            **adapt.Fill(dt);**//this is again vulnerable
            con.Close();
        }  

 if (dt.Rows.Count > 0)
                {
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                }

我不熟悉XSS。我经历了很多文件。它要求我们对数据进行编码。但在我的情况下,我该如何进行。我在GV中有许多标签和文本框作为项目模板。用它来更新表格行。

3 个答案:

答案 0 :(得分:0)

您正在使用带有追加参数的内联查询进行查询。

检查SQL注入的以下链接:

https://www.w3schools.com/sql/sql_injection.asp

同时检查web.config中的以下标记

<system.web>
  <pages buffer="true" validateRequest="true" />
</system.web>

答案 1 :(得分:0)

存储的XSS可以通过将用户的sessionId或令牌秘密发布到攻击者的站点等方式来伤害您的应用程序用户。

示例:<script src=”http://wrongsite.com/tokenstealer.js”> </script>

您可以防止它:

 <EditItemTemplate>   
                  <asp:TextBox ID="txt_Name" runat="server" Text='<%#Microsoft.Security.Application.Encoder.HtmlEncode(Eval(Uname).ToString()) %>'></asp:TextBox>   //this should protect from Stored XSS
 </EditItemTemplate> 

如果您的.net Framework版本为4.5或更高版本,请使用

System.Web.Security.AntiXss.AntiXssEncoder.HtmlEncode();

答案 2 :(得分:0)

您可以通过这种方式阻止它:

Text='<%#System.Web.HttpUtility.HtmlEncode(Eval(Uname).ToString())%>' 
相关问题