473,399 Members | 4,177 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Protecting SQL injection attacks (text input functino)

I'm learning a bit about the SWL injection issues and want to write a shared
class that I can call from anywhere in my project to 'sanitize' any incoming
text from textfields before sending to the DB.

Is it enough to simply escape single quotes as two single quotes? Ie,
replace ' with ''? Or should I also be checking for things like brackets,
parenthesis and SQL command words (INSERT, UPDATE, DELETE, etc.)?

And...maybe a dumb question, but why doesn't SQL check for these things
automatically?

-Darrel
Nov 18 '05 #1
9 2039
The best way to protect from SQL injection attacks is to utilize
parameterized queries or stored procedures. Sanitizing is fine but I've
always treated it as I do encryption... assume I'm not qualified and
that I'm better off using the existing means that were built by the
"insiders" that know the details (i.e. all the character sequences that
could cause problems).

As for why SQL doesn't filter it out, it is because you are allowed to
submit batches of sql statements for execution. This means that the SQL
server can't tell the difference between you submitting two sql
statements or you submitting one statement that has been hijacked to
hold a second statement of your user's choosing. They provide a means
for mitigating the potential problems this may cause, it is up to us to
take advantage of it.

Have A Better One!

John M Deal, MCP
Necessity Software

Darrel wrote:
I'm learning a bit about the SWL injection issues and want to write a shared
class that I can call from anywhere in my project to 'sanitize' any incoming
text from textfields before sending to the DB.

Is it enough to simply escape single quotes as two single quotes? Ie,
replace ' with ''? Or should I also be checking for things like brackets,
parenthesis and SQL command words (INSERT, UPDATE, DELETE, etc.)?

And...maybe a dumb question, but why doesn't SQL check for these things
automatically?

-Darrel

Nov 18 '05 #2
Darrel,

You are safe if you use the values from the textboxes as query parameters.
In that case you don't need any checking.

SQL can't check for an attack since the whole trick is to send to the server
sql statements with valid syntax.

Eliyahu

"Darrel" <no*****@nospam.com> wrote in message
news:u2**************@TK2MSFTNGP14.phx.gbl...
I'm learning a bit about the SWL injection issues and want to write a shared class that I can call from anywhere in my project to 'sanitize' any incoming text from textfields before sending to the DB.

Is it enough to simply escape single quotes as two single quotes? Ie,
replace ' with ''? Or should I also be checking for things like brackets,
parenthesis and SQL command words (INSERT, UPDATE, DELETE, etc.)?

And...maybe a dumb question, but why doesn't SQL check for these things
automatically?

-Darrel

Nov 18 '05 #3
Escaping quotes is one measure. You should also practice using stored
procedues or parameterized queries if stored procs is not an option (e.g MS
Access). Also, create a sqlcommand or oledbcommand object and specify the
commandtype property to = commandtype.text
Dim cmd as new SqlCommand
cmd.CommandType = CommandType.StoredProcedure
or
cmd.CommandType = CommandType.Text

If you use the Text command type, then you are telling the Database to
process all command as plain text and not as actual sql commands. So, the
single quote should not be a factor it will just be treated like a regular
string.
Hope this helps

"Darrel" wrote:
I'm learning a bit about the SWL injection issues and want to write a shared
class that I can call from anywhere in my project to 'sanitize' any incoming
text from textfields before sending to the DB.

Is it enough to simply escape single quotes as two single quotes? Ie,
replace ' with ''? Or should I also be checking for things like brackets,
parenthesis and SQL command words (INSERT, UPDATE, DELETE, etc.)?

And...maybe a dumb question, but why doesn't SQL check for these things
automatically?

-Darrel

Nov 18 '05 #4
> You are safe if you use the values from the textboxes as query parameters.
In that case you don't need any checking.


Can you explain that? I'm not really sure what a 'query parameter' is.
Right now I have these textboxes:

[firstName]
[lastName]

And then a SQL statement like this:

"INSERT INTO tablename (firstName, lastName) VALUES ('" & firstName.text &
"', '" & lastName.text & "')"

The problem is that any name with an apostrophe, breaks the syntax, so I
need to escape anyways. Is there anything else I need to do, or are these
simply innocuous parameters?

-Darrel
Nov 18 '05 #5
> If you use the Text command type, then you are telling the Database to
process all command as plain text and not as actual sql commands. So, the
single quote should not be a factor it will just be treated like a regular
string.


Ah! That makes sense. Thanks!

-Darrel
Nov 18 '05 #6
You should use ADO.NET parameter objects. They will protect you against SQL
Injection Attacks.

Here's more info:
http://msdn.microsoft.com/library/de...classtopic.asp
http://msdn.microsoft.com/library/de...isualbasic.asp

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net
"Darrel" <no*****@nospam.com> wrote in message
news:u2**************@TK2MSFTNGP14.phx.gbl...
I'm learning a bit about the SWL injection issues and want to write a
shared class that I can call from anywhere in my project to 'sanitize' any
incoming text from textfields before sending to the DB.

Is it enough to simply escape single quotes as two single quotes? Ie,
replace ' with ''? Or should I also be checking for things like brackets,
parenthesis and SQL command words (INSERT, UPDATE, DELETE, etc.)?

And...maybe a dumb question, but why doesn't SQL check for these things
automatically?

-Darrel

Nov 18 '05 #7
this is a perfect example of code that allows sql injection.

just type in the lastname textbox

a') delete tablename select ('a
use sqlcomman and parameters

cmd.CommandText = "INSERT INTO tablename (firstName, lastName) VALUES
('@firstname','@lastname')";
cmd.Parameters.Add("@firstname",SqlDbType.VarChar) .Value = firstName.Text;
cmd.Parameters.Add("@lastname",SqlDbType.VarChar). Value = lastName.Text;

-- bruce (sqlwork.com)

"Darrel" <no*****@nospam.com> wrote in message
news:eP**************@TK2MSFTNGP15.phx.gbl...
| > You are safe if you use the values from the textboxes as query
parameters.
| > In that case you don't need any checking.
|
| Can you explain that? I'm not really sure what a 'query parameter' is.
| Right now I have these textboxes:
|
| [firstName]
| [lastName]
|
| And then a SQL statement like this:
|
| "INSERT INTO tablename (firstName, lastName) VALUES ('" & firstName.text &
| "', '" & lastName.text & "')"
|
| The problem is that any name with an apostrophe, breaks the syntax, so I
| need to escape anyways. Is there anything else I need to do, or are these
| simply innocuous parameters?
|
| -Darrel
|
|
Nov 18 '05 #8
> this is a perfect example of code that allows sql injection.
just type in the lastname textbox
a') delete tablename select ('a
would escaping the single quotes as double remedy that?
use sqlcomman and parameters

cmd.CommandText = "INSERT INTO tablename (firstName, lastName) VALUES
('@firstname','@lastname')";
cmd.Parameters.Add("@firstname",SqlDbType.VarChar) .Value = firstName.Text;
cmd.Parameters.Add("@lastname",SqlDbType.VarChar). Value = lastName.Text;


Ah...so, what does that do, exactly? Does it simply send the same text but
as a non-executable command? Is this different than Tamp's suggestion of
setting the entire command as text?

-Darrel
Nov 18 '05 #9
Dude, it's been explained to you more than once that you need to use ADO.NET
parameter objects.
They are the best practice and will prevent all forms of SQL injection
attacks.
Don't escape anything. That's not a good solution. Parameter objects are
the solution.

Here's more info:
http://msdn.microsoft.com/library/de...classtopic.asp
http://msdn.microsoft.com/library/de...isualbasic.asp

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net
"Darrel" <no*****@nospam.com> wrote in message
news:uk**************@TK2MSFTNGP15.phx.gbl...
this is a perfect example of code that allows sql injection.
just type in the lastname textbox
a') delete tablename select ('a


would escaping the single quotes as double remedy that?
use sqlcomman and parameters

cmd.CommandText = "INSERT INTO tablename (firstName, lastName) VALUES
('@firstname','@lastname')";
cmd.Parameters.Add("@firstname",SqlDbType.VarChar) .Value =
firstName.Text;
cmd.Parameters.Add("@lastname",SqlDbType.VarChar). Value = lastName.Text;


Ah...so, what does that do, exactly? Does it simply send the same text but
as a non-executable command? Is this different than Tamp's suggestion of
setting the entire command as text?

-Darrel

Nov 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Bã§TãRÐ | last post by:
I have been working on this particular project for a little over 2 weeks now. This product contains between 700-900 stored procedures to handle just about all you can imagine within the product. I...
10
by: MattB | last post by:
I have a name lookup form that passes the contents of two text boxes to a sql query. I've noticed that someone can substitute % for letters and wildcard the query. I know I could just disallow that...
5
by: www.douglassdavis.com | last post by:
I have an idea for preventing sql injection attacks, however it would have to be implemented by the database vendor. Let me know if I am on the right track, this totally off base, or already...
10
by: bregent | last post by:
I've seen plenty of articles and utilities for preventing form injections for ASP.NET, but not too much for classic ASP. Are there any good input validation scripts that you use to avoid form...
8
by: stirrell | last post by:
Hello, One problem that I had been having is stopping email injections on contact forms. I did some research, read up on it and felt like I had created a working solution. I hadn't gotten any...
6
by: Tor Erik Soenvisen | last post by:
Hi, How safe is the following code against SQL injection: # Get user privilege digest = sha.new(pw).hexdigest() # Protect against SQL injection by escaping quotes uname = uname.replace("'",...
29
by: sinbuzz | last post by:
Hi, I'm curious about the best way to avoid SQL Injection attacks against my web server. Currently I'm on IIS. I might be willing to switch to something like Apache but I'm not sure if SQL...
5
by: Cheb | last post by:
I am writing a simple 'contact us' email form and I am aware I should protect it from code injection and malicious email hijacks. I have used mysql_escape_string() to remove any newlines in the...
2
by: Jerry Winston | last post by:
We all know SQL injection attacks can easily get break SQL command strings concatenated with unsanitized user input fields: set commandObj = Server.CreateObject("ADODB.Connection") set rs =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.