Forum Moderators: open

Message Too Old, No Replies

How can I sanitise SQL statements in ASP?

protecting a community driven website

         

Simon606

2:02 pm on Sep 12, 2008 (gmt 0)

10+ Year Member



I have an SQL database attached to a social networking website.

My website relies on user input so my question is how do you protect against the misuse of words like Union, Select, Drop etc without the need to stop users from using them?

ac2112

8:35 am on Sep 13, 2008 (gmt 0)

10+ Year Member



You could just code word all the SQL commands and then replace them on input....

so say we code Union as codeone and then on insert we say

<%Replace(Request.Form("field"),"Union", "codeone")%>

and then reverse it to display the form data when needed...

<%=Replace(RS("field"), "codeone", "Union")%>

be easier to assign the above to variable names first if you are coding a few words...so

strField = Request.Form("field")

then

strField = Replace(strField, "union", "codeone")
strField = Replace(strField, "insert", "codetwo")

etc...

hope it helps...

Simon606

6:07 pm on Sep 15, 2008 (gmt 0)

10+ Year Member



that was well written and very easy to understand. thank you.

ac2112

5:40 pm on Sep 19, 2008 (gmt 0)

10+ Year Member



You are very welcome and I hope it helped...it is probably not the most conventional method but the I think the less popular methods are less likely to be cracked.

Ocean10000

5:06 am on Sep 22, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Why not just use stored procedures to handle properly inserting items into the database. This would be the better bet instead of trying to guess the next major sql injection hack that comes down the road.

Example

public function spGetClientPageList(clientid)
Set cmd = Server.CreateObject("ADODB.Command")
Set cmd.ActiveConnection = connection
cmd.CommandText = "StoredProcedureName"
cmd.CommandType = adCmdStoredProc
cmd.Parameters.Append cmd.CreateParameter("@clientid", adInteger,adParamInput,,clientid)
Set RsRecordset = Server.CreateObject("ADODB.Recordset")
RsRecordset.CursorType = adOpenForwardOnly
RsRecordset.CursorLocation = adUseClient
RsRecordset.Open cmd
set spGetClientPageList = RsRecordset
set cmd = nothing
end function

wingnut

10:03 am on Sep 29, 2008 (gmt 0)

10+ Year Member



If you look through the recent post on SQL Injection attacks (http://www.webmasterworld.com/databases_sql_mysql/3657200.htm) it should help you.