C#-黑客-数据库访问-字符串的攻击和防御

C#中用基本的方法对数据库进行增删改查,会被黑客利用,写入其他的代码以实现对数据库的数据进行其他的操作。例如:

对下列数据库的某个信息进行修改操作

修改代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;

namespace 攻击_防御
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建 数据库连接类
            SqlConnection conn = new SqlConnection("server=.;database=Data0928;user=sa;pwd=asdf;");
            //创建 数据库操作类
            SqlCommand cmd = conn.CreateCommand();

            //一、显示Users表中的所有信息
            cmd.CommandText = "select *from Users";

            //在数据库中执行操作
            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
                Console.WriteLine(dr["ids"] + "\t" + dr["Username"] + "\t" + dr["password"] + "\t" + dr["nickname"] + "\t" + dr["sex"] + "\t" + dr["birthday"] + "\t" + dr["nation"] + "\t" + dr["class"] + "\t");
            conn.Close();
            //二、让用户选择要修改的数据
            Console.WriteLine();
            Console.Write("请输入要修改数据的用户名:");
            string uname = Console.ReadLine();

            //在数据库查询有无此信息
            cmd.CommandText = "select *from Users where username=‘" + uname + "‘";
            bool has = false;
            conn.Open();
            SqlDataReader dr1 = cmd.ExecuteReader();
            if (dr1.HasRows)
                has = true;
            conn.Close();

            //提示有无此信息,是否进行修改
            if (has)
            {
                Console.WriteLine("已查到此用户信息,请输入修改后的信息");
                Console.Write("请输入修改的用户名:");
                string xname = Console.ReadLine();
                Console.Write("请输入修改的密码:");
                string xpwd = Console.ReadLine();
                Console.Write("请输入修改的昵称:");
                string xnick = Console.ReadLine();
                Console.Write("请输入修改的性别:");
                bool xsex = Convert.ToBoolean(Console.ReadLine());
                Console.Write("请输入修改的生日:");
                DateTime xbir = Convert.ToDateTime(Console.ReadLine());
                Console.Write("请输入修改的民族:");
                string xnation = Console.ReadLine();
                Console.Write("请输入修改的班级:");
                string xcla = Console.ReadLine();

                //修改信息准备操作
                cmd.CommandText = "update Users set username=‘" + xname + "‘,password=‘" + xpwd + "‘,nickname=‘" + xnick + "‘,sex=‘" + xsex + "‘,birthday=‘" + xbir + "‘,nation=‘" + xnation + "‘,class=‘" + xcla + "‘ where username=‘" + uname + "‘";
                //在数据库中执行操作
                conn.Open();
                int i0 = cmd.ExecuteNonQuery();

                conn.Close();
                //判断是否修改成功
                if (i0 > 0)
                    Console.WriteLine("数据修改成功!");
                else
                    Console.WriteLine("数据修改失败!");

            }
            else
                Console.WriteLine("查无此信息。");

            Console.ReadLine();
        }
    }
}

对数据库数据进行修改操作

修改后数据库数据:

=====================================================================================================

如何对数据库进行字符串攻击?

此时数据库中数据全部被删除,为什么?

在数据库中输入与修改时相同的代码试试

数据库读取到了“delete from Users”字符串,并且它之后的所有代码被“--”注释掉了,并不会执行,所有数据库执行了delete语句,删除了数据库所有信息

=====================================================================================================

针对以上情况,如何防御数据库被修改?——占位符

update Users set 后的语句用占位符代替,比如:set username=‘"+zhangsan+"‘,password=‘"+asdf+"‘ 用set [email protected],[email protected]

代替时,等号后面的单引号也去掉

执行cmd.Parameters.Clera();语句清空集合,然后进行添加,指定占位符的意义

再次进行攻击

数据库的结果为

数据库数据并没有删掉,而删除语句作为字符串被占位符带入数据库中,成功防御了字符串的攻击

时间: 2024-08-08 05:38:56

C#-黑客-数据库访问-字符串的攻击和防御的相关文章

【2017-04-20】Sql字符串注入式攻击与防御

字符串攻击 所谓sql字符串注入式攻击就是在用户输入界面输入通过精心编制的含有某种指令的字符串,来改变C#中连接数据库要执行的sql语句,从而对数据库进行攻击性操作 在用户输入界面输入  a');update Student set Sname ='伊伊';-- 则数据库Sname一列都讲变为  伊伊 原理:用户输入的代码将c#中的sql语句中的sname替换掉了,就变成了下边的黑客想执行的sql语句. insert into Student values('"+sno+"','a')

ADO.NET字符串注入式攻击与防御

一.字符串注入攻击 所谓sql字符串注入式攻击就是在用户输入界面输入一串sql语句,来改变C#中连接数据库要执行的sql语句 通过你写的程序,直接来执行我想要执行的sql语句 例如:在这么一个程序中,sname是需要用户输入的内容. 在用户输入界面输入 a');update 表名 set 列名 ='字段名称;-- 假如你在输入时输入了这句代码:a');update student set sname ='朱利军;-- 原理:用户输入的代码将c#中的sql语句中的sname替换掉了,就变成了下边的

【ASP.NET开发】ASP.NET对SQLServer的通用数据库访问类(转)

/// <summary> /// 数据库访问通用类 /// </summary> public class SqlHelper { private string connectionString; /// <summary> /// 设定数据库访问字符串 /// </summary> public string ConnectionString { set { connectionString = value; } } /// <summary>

ado.not--数据库防字符串注入攻击学习及 练习

数据库防字符串注入攻击:cmd.CommandText = "update student set [email protected],[email protected],[email protected],[email protected] where code = @Scode";cmd.Parameters.Clear();                ---------一个集合cmd.Parameters.Add("@Sname", Sname);cmd.

字符串防攻击,匿名函数

字符串攻击样式: 主要利用获取需用户输入的字符串时,通过输入精心编制的含有某种指令的字符串,从而对数据库进行攻击性操作. ';(内容);-- 那我们如何来防止攻击:主要是用Parameters这个集合 cmd.CommandText = "update Users set [email protected],[email protected],[email protected],[email protected],[email protected],[email protected] where

公共的数据库访问访问类 SqlHelper.cs

/// <summary> /// 类说明:公共的数据库访问访问类 /// </summary> using System; using System.Collections.Generic; using System.Text; using System.Data; using System.Data.SqlClient; using System.Collections; namespace DotNet.Utilities { /// <summary> /// 

在数据库访问项目中使用微软企业库Enterprise Library,实现多种数据库的支持

在我们开发很多项目中,数据访问都是必不可少的,有的需要访问Oracle.SQLServer.Mysql这些常规的数据库,也有可能访问SQLite.Access,或者一些我们可能不常用的PostgreSQL.IBM DB2.或者国产达梦数据库等等,这些数据库的共同特点是关系型数据库,基本上开发的模型都差不多,不过如果我们基于ADO.NET的基础上进行开发的话,那么各种数据库都有自己不同的数据库操作对象,微软企业库Enterprise Library是基于这些不同数据库的操作做的抽象模型,适合多数据

C#---数据库访问通用类、Access数据库操作类、mysql类 .[转]

原文链接 //C# 数据库访问通用类 (ADO.NET)using System;using System.Collections.Generic;using System.Text;using System.Data;using System.Data.SqlClient;using System.Configuration; namespace XXX{    /// <summary>    /// 针对SQL Server数据库操作的通用类           /// </sum

黑客仍在不遗余力地攻击“成人陪护”网站

黑客仍在不遗余力地攻击“成人陪护”网站 (原标题:某宗教黑客仍在不遗余力地攻击“成人陪护”网站) 一名叫ElSurveillance的摩洛哥黑客宣称自己已经污损和窃取了79家护送网站的数据,作为其与去年夏天发起的大规模行动的一部分,不少其反对的“成人陪送”门户网站均遭到了入侵.12月27号的时候,ElSurveillance公布了首批71家网站的数据,次日又披露了另外8家网站.其中部分网站隶属于机构,亦有一些网站属于个人性质. 该黑客在接受DataBreacher.net采访时表示,其旨在用行动