The following are company name in my database. I am trying to produce on the fly string query that are foolproof against SQL Injection.
I test the safety tolerance level of parameterized query against SQL Injection because I hear rumors it is not 100%.
=====userinput pure strings but with malicious content for SQL injection=========
userinput = "Tubo'; UPDATE STOCK_TYPE SET Description='SQL Injection'" &
"WHERE STock_Type_ID=1; --'"
userinput = "'Some Name''; GRANT CONTROL TO [Malicious User]; PRINT ''Game over!" &
"This system is no longer yours!''" &
"-- Malicious User now can control the database!!!'"
userinput = "!!!@@@###$$$%%%^^^&&&***((()))///```~~~___---+++===:::;;;"""'''|||
\\\}}}>>><<<{{{}}}]]]...,,,[[[]]]///"
=====To search company name with SQL Injected userinput ==========================
Dim SQL = "select p.Name from EntContext.COMPANIes as p Where p.Name like @CompanyName"
ObjectParamters.Add(New ObjectParameter("@CompanyName", userinput))
Dim databaseObjectQuery As ObjectQuery(Of DbDataRecord) =
New ObjectQuery(Of DbDataRecord)(SQL, EntContext)
databaseObjectQuery.Parameters.Add(parameter)
====================================================================
COMPANIes.name database contain exactly these strings to help my testing. By a successful search against user input I think my code is stronger against attack.
But when I search against COMPANIes.name with the exact user input string, only the first userinput produced result.
I narrow the culprit to '[' it doesn't produce result for second and third userinput via LINQPAD
select p.company_id , p.Name
from EntContext.COMPANIes as p
Where p.Name like '%[%'
No result. So do this
select p.company_id , p.Name
from EntContext.COMPANIes as p
Where p.Name like '%/[%' escape '/'
produce the result against second and third user input over matching '[' in company name.
================Attempt to solve programmatically in VB.NET ===============
FilterSQL = " AND COMPANies.Name like @CompanyName escape " + Chr(255)
objectParameters.Add(New ObjectParameter("CompanyName", "%" & Chr(255) &
searchResultParam.ParamValue.Value & "%"))
The simple identifier 'ÿ' must contain basic Latin characters only.
To use UNICODE characters, use an escaped identifier. Near line 1, column 320.
=============================================================================
Are parameterized query 100% foolproof even userinput is coded like this?
ObjectParamters.Add(New ObjectParameter("@CompanyName", userinput))
Was my assumption over second and third userinput correct on escaping this character '[' ?
In parameterized query of LINQ, how do search against this exact stored value?
COMPANIes.Name= "!@#$%^&*()/`~_-+=:;"'|}><{}].,[]/"