SQL注入攻擊
阿新 • • 發佈:2022-03-10
昨天開始接觸到CTF、網路安全等知識,對網路安全方面有了初步的瞭解。
今天試一下攻擊之前自己做的winform測試程式。
登入介面:
登入的主要程式碼:
private void button1_Click(object sender, EventArgs e) { if (textBox1.Text == "" || textBox2.Text == "") { MessageBox.Show("賬號密碼不能為空"); return; } SqlConnection conn= ConnDB.DBConnection(); conn.Open(); string sql = @"select * from DnUser where account = '{0}' and password = '{1}'"; sql = string.Format(sql, textBox1.Text, textBox2.Text); SqlCommand cmd = new SqlCommand(sql, conn); string account = (string)cmd.ExecuteScalar(); if (textBox1.Text == account) { Program.account = textBox1.Text; this.DialogResult = DialogResult.OK; conn.Close(); this.Close(); } else { MessageBox.Show(sql+"賬號密碼錯誤"); } }
sql語句是動態拼接的,沒有識別輸入字元的格式,也沒有過濾單引號、括號等特殊字元,這就帶來了安全隱患。
開始攻擊:
在賬號欄輸入以下語句,點選登入,就可以往資料庫插入一條資料。
1'; insert into DnUser values('3','3'); --
查詢資料庫,發現多了一條記錄。
接下來就可以用賬號為3,密碼為3的賬戶登入系統了。