Click here to Skip to main content
15,906,624 members
Home / Discussions / Database
   

Database

 
GeneralRe: Prevent queries from SQL Injection attack in SQL Server 2005 Pin
salon29-May-08 3:32
salon29-May-08 3:32 
GeneralRe: Prevent queries from SQL Injection attack in SQL Server 2005 Pin
SomeGuyThatIsMe29-May-08 4:02
SomeGuyThatIsMe29-May-08 4:02 
GeneralRe: Prevent queries from SQL Injection attack in SQL Server 2005 Pin
Alsvha29-May-08 6:11
Alsvha29-May-08 6:11 
GeneralRe: Prevent queries from SQL Injection attack in SQL Server 2005 Pin
SomeGuyThatIsMe29-May-08 7:52
SomeGuyThatIsMe29-May-08 7:52 
GeneralRe: Prevent queries from SQL Injection attack in SQL Server 2005 Pin
Alsvha29-May-08 8:29
Alsvha29-May-08 8:29 
AnswerRe: Prevent queries from SQL Injection attack in SQL Server 2005 Pin
Alsvha29-May-08 6:15
Alsvha29-May-08 6:15 
GeneralRe: Prevent queries from SQL Injection attack in SQL Server 2005 Pin
salon29-May-08 22:41
salon29-May-08 22:41 
AnswerRe: Prevent queries from SQL Injection attack in SQL Server 2005 Pin
Peter Josefsson Sweden30-May-08 1:17
Peter Josefsson Sweden30-May-08 1:17 
Hi!

Let's say this is an old web site built in ASP with VBScript and it's just had an SQL Injection attack (as happened to me a week ago - I have hundreds of old sites running, and sometimes I or a colleague have had a bad day years ago - it tends to come back and bite you). You've stopped the web, cleaned the database (let's say it only appended script tags to some text fields, so it was a quick fix - this is common bot behaviour).

Quick and dirty fix to get it up:

Search for the word "WHERE" in the site source. Replace this:
Recs.Open "SELECT ... FROM ... WHERE x = '" & something & "' ..." ...

with:
Recs.Open "SELECT ... FROM ... WHERE x = '" & Replace(something, "'", "''") & "' ..." ...

and this:
Recs.Open "SELECT ... FROM ... WHERE someid = " & something & " ...", ...

with:
Recs.Open "SELECT ... FROM ... WHERE someid = " & CLng(something) & " ...", ...
'CLng will throw on invalid input!

This is a naive fix and only stops the most common attacks. So... when the site is up, you spend the next day rewriting further (from memory, possibly lots of syntax errors and remembering things wrong, but the concept is correct):
Set cmd = Server.CreateObject("ADODB.Command")
cmd.CommandText = "SELECT ... FROM ... WHERE x = ? ..."
cmd.Parameters.Add cmd.CreateParameter("@p1", ...)
Recs.Open cmd, ...

And yes, you'd want to wrap that in a function, like so:
Recs.Open SqlCmd("SELECT...", Array(param1, param2)), ...

(the hideous Array() construct is because VBScript doesn't know about parameter arrays - also beware that you must analyze the values to figure out the proper types and other parameter metadata in the SqlCmd function)

If you're using .Net, look at the SqlCommand (or DbCommand) object. Same thing. There are several reasons to use parameterized queries:

- SQL Server can cache execution plans for similar queries - this improves performance A LOT if you do the same query over and over with different parameters.

- You're immune to SQL Injection attacks.

- You're code gets easier to read (provided you encapsulate the mechanism properly, otherwise it will bloat the code).

Then again, why don't we just round up all hackers and shoot them? I could even consider molesting them for a while first... Mad | :mad:

Peter the small turnip

(1) It Has To Work. --RFC 1925[^]

QuestionTRIGGER problem!!!!!! Help me! [modified] Pin
Karan_TN29-May-08 2:16
Karan_TN29-May-08 2:16 
AnswerRe: TRIGGER problem!!!!!! Help me! Pin
Peter Josefsson Sweden30-May-08 1:31
Peter Josefsson Sweden30-May-08 1:31 
GeneralRe: TRIGGER problem!!!!!! Help me! Pin
Karan_TN30-May-08 21:16
Karan_TN30-May-08 21:16 
GeneralRe: TRIGGER problem!!!!!! Help me! Pin
Peter Josefsson Sweden2-Jun-08 2:11
Peter Josefsson Sweden2-Jun-08 2:11 
GeneralRe: TRIGGER problem!!!!!! Help me! Pin
Karan_TN6-Jun-08 19:13
Karan_TN6-Jun-08 19:13 
QuestionBuilt in stored procedures Pin
dan!sh 28-May-08 23:53
professional dan!sh 28-May-08 23:53 
AnswerRe: Built in stored procedures Pin
Mark J. Miller29-May-08 3:32
Mark J. Miller29-May-08 3:32 
QuestionCopy data from one table to another problem Pin
soniasan28-May-08 22:42
soniasan28-May-08 22:42 
AnswerRe: Copy data from one table to another problem Pin
ChandraRam28-May-08 23:13
ChandraRam28-May-08 23:13 
GeneralRe: Copy data from one table to another problem Pin
Ashfield29-May-08 1:46
Ashfield29-May-08 1:46 
QuestionMaximum output length for PRINT in SQL 2005 Pin
MatthysDT28-May-08 21:38
MatthysDT28-May-08 21:38 
AnswerRe: Maximum output length for PRINT in SQL 2005 Pin
Ashfield29-May-08 2:16
Ashfield29-May-08 2:16 
QuestionODP.net (oracle data provider) - batches Pin
Member 453749128-May-08 19:42
Member 453749128-May-08 19:42 
Questionmultiple sql queries Pin
twsted f828-May-08 9:28
twsted f828-May-08 9:28 
AnswerRe: multiple sql queries Pin
Blue_Boy28-May-08 13:27
Blue_Boy28-May-08 13:27 
QuestionCould not find file 'C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE' error in VS 2008 Pin
MPCHAM28-May-08 3:49
MPCHAM28-May-08 3:49 
AnswerRe: Could not find file 'C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE' error in VS 2008 Pin
Mike Dimmick2-Jun-08 5:43
Mike Dimmick2-Jun-08 5:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.