简单的SQL注释漏洞攻击

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page 
{
    
private string strSql;
    
protected void Page_Load(object sender, EventArgs e)
    
{
        Response.Write(
"万能密码再此:  '+'tom'--");

    }

    
protected void btnLogin_Click(object sender, EventArgs e)
    
{
        
        SqlConnection con 
= new SqlConnection("server=.;database=test;uid=sa;pwd=;");
        strSql 
= "select count(*) from userInfo where userName=" +
            
"'" + txtUserName.Text + "'" + "and userPwd=" + "'" + txtUserPwd.Text + "'";
        con.Open();
        SqlCommand cmd 
= new SqlCommand(strSql, con);
        
int count = Convert.ToInt32( cmd.ExecuteScalar());
        con.Close();
        
if (count > 0)
            lblMessage.Text 
= "登录成功";
        
else
            lblMessage.Text 
= "登录失败";
    }

}

 

两次都没输入密码,第一次是普通的登录

普通登录

第二次利用漏洞进行登录,在用户名文本框中输入 '+'tom'--    ,成功了!

利用漏洞 

数据库脚本

create table userInfo(
userID 
int identity(1,1),
userName 
varchar(20),
userPwd 
varchar(20)
)
insert into userInfo values('jim','12345')
insert into userInfo values('tom','1111')
insert into userInfo values('jam','ssssss')
insert into userInfo values('suse','888')

 

HTML代码:

 

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    
<title></title>
</head>
<body>
    
<form id="form1" runat="server">
    
<div style="text-align: center">
        
<table border="0" cellpadding="0" cellspacing="0">
            
<tr>
                
<td style="width: 73px">
                    用户名:
</td>
                
<td style="width: 100px">
                    
<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox></td>
            
</tr>
            
<tr>
                
<td style="width: 73px">
                    密码:
</td>
                
<td style="width: 100px">
                    
<asp:TextBox ID="txtUserPwd" runat="server"></asp:TextBox></td>
            
</tr>
            
<tr>
                
<td colspan="2">
                    
<asp:Button ID="btnLogin" runat="server" OnClick="btnLogin_Click" Text="登 录" Width="69px" />
                    
<asp:Label ID="lblMessage" runat="server"></asp:Label></td>
            
</tr>
        
</table>
    
    
</div>
    
</form>
</body>
</html>

 

因此,在代码中直接用SQL进行操作是很危险的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值