Dim selectString As String = "SELECT * FROM Customers WHERE CustomerID = " & custID
Dim cmd As SqlCommand = New SqlCommand(selectString, conn)
conn.Open()
Dim myReader As SqlDataReader = cmd.ExecuteReader()
myReader.Close()
conn.Close()
攻擊者可為要查詢的 CustomerID 輸入一個值“1;DROP TABLE Customers”。這會導致為此查詢執行以下命令。
SELECT * FROM Customers WHERE CustomerID = 1;DROP TABLE Customers
為了防止 SQL Insertion 攻擊,請驗證來自外部源的輸入,並傳遞列值作為參數,而不是串聯這些值來建立 SQL 陳述式。