Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 sqlinjection

Author  Topic 

mike123
Master Smack Fu Yak Hacker

1462 Posts

Posted - 2004-08-03 : 21:40:06
Just wondering if I can get a hand with this one. It's been a long time security worry of mine. Its my only SPROC that is written in dynamic SQL and I *HATE* it. I'm worried about its vunerablity to sql injection.

Is there a better way I can write this SPROC ?

How can I make sure its not vunerable to sqlinjection attacks? I've never been comfortable enough determining its safe on my own.

Thanks alot
mike123


CREATE PROCEDURE [delete_Messages]
(
@MessageIDTXT VarChar(255),
@MessageTo [int]
)
AS SET NOCOUNT ON
Declare @SQL VarChar(500)
Select @SQL = 'DELETE [tblMessage] '
Select @SQL = @SQL + 'WHERE [MessageID] IN (' + @MessageIDTXT +') AND [MessageTo] = ' + CAST(@MessageTo As varchar)
Exec ( @SQL)

GO

timmy
Master Smack Fu Yak Hacker

1242 Posts

Posted - 2004-08-03 : 22:02:08
One way to stop SQL injection is to remove any characters or character sequences that are used as breakout sequences from your parameters. These are generally apostrophes (') and comments (/*), but there's a few more that I can't remember at the moment.

But - the best way of stopping SQL injection is to implement the filtering at the front-end application (I'm assuming it's ASP). For my app, I have a series of custom functions designed to read text fields, currency fields, date fields etc that validate each input and (in the case of strings) clean out script tags and other sequences used by hackers.
You can also use URLScan to good effect for this (again, assuming you use IIS).

Tim
Go to Top of Page

kselvia
Aged Yak Warrior

526 Posts

Posted - 2004-08-03 : 22:25:08
You can rewrite your procedure to not use dynamic SQL and eliminate SQL injection vulnerability.

See Jeff's example here

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=25830&SearchTerms=udf,csv,string

or

http://www.sommarskog.se/arrays-in-sql.html

for more detailed information.


--Ken
"Knowledge is a process of piling up facts; wisdom lies in their simplification."
Go to Top of Page
   

- Advertisement -