SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Is this Vulnerable for SQL injection?..


Is this Vulnerable for SQL injection?..

Author
Message
enriquezreyjoseph
enriquezreyjoseph
SSC-Enthusiastic
SSC-Enthusiastic (185 reputation)SSC-Enthusiastic (185 reputation)SSC-Enthusiastic (185 reputation)SSC-Enthusiastic (185 reputation)SSC-Enthusiastic (185 reputation)SSC-Enthusiastic (185 reputation)SSC-Enthusiastic (185 reputation)SSC-Enthusiastic (185 reputation)

Group: General Forum Members
Points: 185 Visits: 191
Hi Everyone

I hope everyone is having a nice day.

This is my Code



set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go


-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[SearchBiography]

@firstname varchar(50),
@middlename varchar(50),
@lastname varchar(50),
@sex varchar(50),
@status varchar(50),
@sexID int,
@statusID int

AS
BEGIN

SET NOCOUNT ON;
DECLARE @SqlQuery varchar(max) , @SqlQueryFirstName varchar(max),@SqlQueryMiddleName varchar(max), @SqlQueryLastName varchar(max), @SqlQuerySex varchar(max), @SqlQueryStatus varchar(max)

SET @SqlQuery = ''


IF LEN(@sex) > 0
SET @SqlQuerySex = ' AND sex like ''%' + @sex + '%'''
ELSE
SET @SqlQuerySex = ''


IF LEN(@status) > 0
SET @SqlQueryStatus = ' AND status like ''%' + @status + '%'''
ELSE
SET @SqlQueryStatus = ''


IF LEN(@firstname) > 0
SET @SqlQueryFirstName = ' AND firstname like ''%' + @firstname + '%'''
ELSE
SET @SqlQueryFirstName = ''


IF LEN(@middlename) > 0
SET @SqlQueryMiddleName = ' AND middlename like ''%' + @middlename + '%'''
ELSE
SET @SqlQueryMiddleName = ''


IF LEN(@lastname) > 0
SET @SqlQueryLastName =' AND lastname like ''%' + @lastname + '%'''
ELSE
SET @SqlQueryLastName = ''


SET @SqlQuery = 'SELECT * FROM TestMyView WHERE sexID = ' + convert(varchar(20), @sexID) + ' AND statusID = ' + convert(varchar(20), @statusID)
SET @SqlQuery = @SqlQuery + @SqlQuerySex + @SqlQueryStatus + @SqlQueryFirstName + @SqlQueryMiddleName + @SqlQueryLastName


EXEC (@SqlQuery) /* Should i need a parameter here??? */ please tell me :-(
PRINT(@SqlQuery)

END





AndrewSQLDBA
AndrewSQLDBA
SSCarpal Tunnel
SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)

Group: General Forum Members
Points: 4738 Visits: 3427
YES!
Anything that is Dynamic can be. Plus as everyone else has said, it is very poor practice. And poor performing.

I wish that it were illegal for a front-end programmer to even open SQL Server. It isn't illegal for one to perform surgery on themselves. Why don't you try that next time you are ill?

And you wonder why so many SQL DBAs are not being paid nearly enough, while having to deal with crap code that makes a SQL Server come to a crawl. And also given code like this to try and troubleshoot or change, all because the front-end programmer was fired.

Andrew SQLDBA
enriquezreyjoseph
enriquezreyjoseph
SSC-Enthusiastic
SSC-Enthusiastic (185 reputation)SSC-Enthusiastic (185 reputation)SSC-Enthusiastic (185 reputation)SSC-Enthusiastic (185 reputation)SSC-Enthusiastic (185 reputation)SSC-Enthusiastic (185 reputation)SSC-Enthusiastic (185 reputation)SSC-Enthusiastic (185 reputation)

Group: General Forum Members
Points: 185 Visits: 191
AndrewSQLDBA (9/26/2013)
YES!
Anything that is Dynamic can be. Plus as everyone else has said, it is very poor practice. And poor performing.

I wish that it were illegal for a front-end programmer to even open SQL Server. It isn't illegal for one to perform surgery on themselves. Why don't you try that next time you are ill?

And you wonder why so many SQL DBAs are not being paid nearly enough, while having to deal with crap code that makes a SQL Server come to a crawl. And also given code like this to try and troubleshoot or change, all because the front-end programmer was fired.

Andrew SQLDBA


HAHAHAH..thanks andrew..

can you "Re-Code" the above Code??...so that it will not be prone to sql injection...thanks andrew..
Mikael Eriksson SE
Mikael Eriksson SE
SSC Eights!
SSC Eights! (880 reputation)SSC Eights! (880 reputation)SSC Eights! (880 reputation)SSC Eights! (880 reputation)SSC Eights! (880 reputation)SSC Eights! (880 reputation)SSC Eights! (880 reputation)SSC Eights! (880 reputation)

Group: General Forum Members
Points: 880 Visits: 994
You should use sp_executesql with parameters.

Have a look at The Curse and Blessings of Dynamic SQL for a bit more info.



Koen Verbeeck
Koen Verbeeck
SSC Guru
SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)

Group: General Forum Members
Points: 64784 Visits: 13298
The book by Denny Cherry on Securing SQL Server has a chapter on SQL Injection, it might be worth checking it out.

Securing SQL Server, Second Edition: Protecting Your Database from Attackers


How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?
My blog at SQLKover.

MCSE Business Intelligence - Microsoft Data Platform MVP
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search