C#和SQl 注入字符串的攻擊 和 防止注入字符轉的攻擊


--SQl中
--建立ren的數據庫,插入一條信息
create database ren 
go
use ren
go
create table xinxi
(
code nvarchar(20) primary key,--編號
name nvarchar(50)--名字
)
insert into xinxi values('1001','zhangsan')
            for (; ; )
            {
   
                bool b = false;//利用中間變量
                Console.Write("請輸入要修改的編號:");
                string no = Console.ReadLine();
                //查詢展示
                SqlConnection zhancnn = new SqlConnection("server=.;database=ren;user=sa;pwd=123");//連接
                //操作的語句
                SqlCommand zhancmd = zhancnn.CreateCommand();
                zhancmd.CommandText = "select * from xinxi where code='" + no + "'";
                //執行操作的語句
                zhancnn.Open();
                SqlDataReader ss = zhancmd.ExecuteReader();
                if (ss.HasRows)//數據庫中是否有要修改的數據
                {
                    b = true;
                }
                zhancnn.Close();
                if (b == true)//如果有要修改的數據
                {

                    Console.Write("找到【" + no + "】的信息,請輸入要修改的名字:");
                    string mingzi = Console.ReadLine();
                    zhancmd.CommandText = "update xinxi set name='"+mingzi+"' where code='"+no+"'";
                    zhancnn.Open();
                    zhancmd.ExecuteNonQuery();
                    zhancnn.Close();
                    Console.WriteLine("修改完畢!");
                    break;
                }
                else//如果沒有要修改的數據
                {
                    Console.WriteLine("數據庫中沒有該條信息,請輸入正確的編碼!!");
                }

            }
            Console.ReadLine();

執行時,,注意,我就要輸入了:

然后查詢數據庫,查詢全部,就成為了

為了防止這種注入文字攻擊,我們就需要:

//C#中 

for (; ; )
            {
   
                bool b = false;//利用中間變量
                Console.Write("請輸入要修改的編號:");
                string no = Console.ReadLine();
                //查詢展示
                SqlConnection zhancnn = new SqlConnection("server=.;database=ren;user=sa;pwd=123");//連接
                //操作的語句
                SqlCommand zhancmd = zhancnn.CreateCommand();
                zhancmd.CommandText = "select * from xinxi where code='" + no + "'";
                //執行操作的語句
                zhancnn.Open();
                SqlDataReader ss = zhancmd.ExecuteReader();
                if (ss.HasRows)//數據庫中是否有要修改的數據
                {
                    b = true;
                }
                zhancnn.Close();
                if (b == true)//如果有要修改的數據
                {

                    Console.Write("找到【" + no + "】的信息,請輸入要修改的名字:");
                    string mingzi = Console.ReadLine();
                    zhancmd.CommandText = "update xinxi set name=@mingzi where code=@no;";//@變量名:占位符。注意:name=@mingzi沒有引號  zhancmd.Parameters.Clear();//必須先清空里面所有內容 zhancmd.Parameters.Add("@mingzi",mingzi);//類似哈希表。第一個值隨便取,必須跟上邊一致;第二個是變量 zhancmd.Parameters.Add("@no",no);
                    zhancnn.Open();
                    zhancmd.ExecuteNonQuery();
                    zhancnn.Close();
                    Console.WriteLine("修改完畢!");
                    break;
                }
                else//如果沒有要修改的數據
                {
                    Console.WriteLine("數據庫中沒有該條信息,請輸入正確的編碼!!");
                }

            }
            Console.ReadLine();
//如果在執行窗口輸入上跟上次一樣的內容,那么輸出的結果就是把”那句注入的代碼“和”要改的name“ 作為一整個字符串進行處理。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM