今天在这里给大家分享的一个话题是SQL注入攻击
实际上是分享五快内容:
1,什么是SQL注入攻击
2,SQL注入漏洞产出原因
3, SQL注入的原理
4,黑客是如何利用SQL注入漏洞来进行攻击的
5,针对SQL注入我们应该如何防范
SQL注入顾名思义也就是在某个东西中插入了SQL语句,如果这些恶意的SQL语句一旦执行,那么就会对我们的应用程序照成破坏,这其实就是SQL注入攻击。
那么为什么会产生SQL注入攻击呢?据我总结,大致可以分为以下几点原因:
*使用字符串拼接sql语句
*在应用程序连结数据库时使用权限过大的帐户(sa)
*在数据库中开放了不必要但权力过大的功能(xp_cmdshell)
*未过滤用户的恶意输入
===============================================
下面我们来看一段代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
namespace sqlInjectionTest.admin
{
public partial class Login1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnLogin_Click(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection(@"Integrated Security=SSPI;Persist Security Info=False;Data Source=.\SQLEXPRESS;Initial Catalog=Manage"))
{
con.Open();
if (ConnectionState.Open==con.State)
{
//Response.Write("<script>alert('连接数据库成功!')</script>");
string username = tb_Name.Text.Trim().ToString();
string password = tb_PassWord.Text.Trim().ToString();
string sql_cmd ="select * from Admin where Name='"+username+"' and Pwd='"+password+"'";
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = sql_cmd;
cmd.Connection = con;
using (SqlDataReader reader = cmd.ExecuteReader())
{
if (reader.Read())
{
//Response.Write("<script>alert('登录成功!')</script>");
Session["US"] = username;
Response.Redirect("./LoginIndex.aspx");
}
else
{
Response.Write("<script>alert('登录失败!')</script>");
}
reader.Close();
}
}
}
else
{
Response.Write("<script>alert('连接数据库失败!')</script>");
}
}
}
}
}
=========================================================
我们在用户登录的用户名处写:'or'1'='1'--
string sql_cmd = "select * from Admin where Name='" + username + "'"+"and Pwd='"+password+"'";
则我们原本执行的SQL语句变为:
select * from Admin where Name=''or'1'='1'--' and Pwd=''
这就是一个最简单的SQL注入攻击!