Web Design Forums

Other Languages

Get help with any programming languages other than the ones above.

About SQL Injections



Site of the Month Voting - Now Open. CAST YOUR VOTE NOW!

Reply
 
LinkBack Thread Tools
Old February 13 '03, 11:30 AM (#1)
filburt1 is offline
bored
 
filburt1's Avatar
 
Join Date: July 2002
Location: Maryland, US
Posts: 11,785
filburt1 is a name known to allfilburt1 is a name known to allfilburt1 is a name known to allfilburt1 is a name known to allfilburt1 is a name known to allfilburt1 is a name known to all
About SQL Injections

A good read on a serious potential type of security hole when working with *SQL:

http://www.4guysfromrolla.com/webtech/061902-1.shtml
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old February 13 '03, 06:32 PM (#2)
TheGAME1264 is offline
Mod-son Canadian
 
TheGAME1264's Avatar
 
Join Date: December 2002
Location: Toronto, Ontario
Posts: 3,032
TheGAME1264 has disabled reputation
A code snippet I'd like to add

The following is a function I have created and use for the prevention of SQL injections as well as to allow information containing single or double quotes to be safely stored in a database:

Code:
function SQLComply (Term)

     Term = Trim (Term)
     if Term <> "" then
          Term = Replace (Term, "", """")
          Term = Replace (Term, "'", "''")
          ' Next two lines are included to combat double quotes being stored by mistake.
          Term = Replace (Term, """""""", """")
          Term = Replace (Term, "''''", "''")
     end if
     SQLComply = Term

end function
When you are retrieving form input, call it as follows:

Form_Variable = SQLComply (Request.Form (Form_Element))

This code will work both for SQL Server and for Access. (I've tested it on both.)

Last edited by TheGAME1264; February 13 '03 at 06:34 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old March 25 '03, 04:50 PM (#3)
smoseley is online now
WDF Moderator
 
smoseley's Avatar
 
Join Date: March 2003
Location: Miami, FL
Posts: 8,719
smoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud of
An even better way to prevent these problems is to use Stored Procedures with ADO Command objects rather than executing string SQL directly in your code.

Here's a simple example:

Database Stuff
Code:
CREATE DATABASE test
GO

USE test
GO

CREATE TABLE person
(
  id    INT NOT NULL IDENTITY(1,1),
  name  VARCHAR(255) NOT NULL
)
GO

ALTER TABLE person
  ADD PRIMARY KEY CLUSTERED (id)
GO

CREATE PROCEDURE addPerson
(
	@id INT OUTPUT,
	@name VARCHAR(255) = ''
)
AS
	SET NOCOUNT ON
	INSERT INTO person(name) VALUES(@name)
	SET @id = @@identity
	SET NOCOUNT OFF
GO
VB Stuff
Code:
<%
    Dim oConn
    Dim oCmd
    Set oConn = Server.CreateObject("ADODB.Connection")
    Set oCmd = Server.CreateObject("ADODB.Command")
    oConn.Open "ConnectionString"
    Set oCmd.ActiveConnection = oConn
    oCmd.CommandType = adCmdStoredProc
    oCmd.CommandText = "addPerson"
    oCmd.Parameters.Refresh
    oCmd.Parameters.Item("@name").Value = "Name"
    oCmd.Execute
    Response.Write("id - " & oCmd.Parameters.Item("@id").Value
    oConn.Close
    Set oCmd = Nothing
    Set oConn = Nothing
%>
Ugh... too much typing !!!

Last edited by smoseley; March 25 '03 at 04:53 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old March 25 '03, 05:30 PM (#4)
smoseley is online now
WDF Moderator
 
smoseley's Avatar
 
Join Date: March 2003
Location: Miami, FL
Posts: 8,719
smoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud of
The reason Stored Procedures with ADO Commands are better is that they will only execute one sql command, unless they are DESIGNED to to otherwise (i.e. through dynamic queries built with EXEC).

Additionally, input values are restricted at the SQL Server by parameter type and length. This prevents all forms of SQL injection attacks.

If your parameter is a varchar, and someone places SQL content into it, it will simply insert that content into the corresponding column, etc.

If you're doing something like this:
Code:
sql = "SELECT * FROM articles WHERE id = " & Request("id")
You're opening yourself up to someone supplying a Request value of "0; DELETE FROM articles", which would result in

Code:
sql = "SELECT * FROM articles WHERE id = 0; DELETE FROM articles"
Using Stored Procedures, you would have something like this:
Code:
CREATE PROCEDURE getArticles
(
    @id INT
)
AS
    SELECT * FROM articles WHERE id = @id
GO
it would never even execute, because "0; DELETE FROM articles" is not an int, and wouldn't be accepted by the procedure.

Furthermore, even if @id were a varchar, it would utilize the entire "0; DELETE FROM articles" string for comparison rather than breaking it up into a separate query.

Hope this explains a little better
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old April 20 '03, 11:21 AM (#5)
kleptos is offline
The 6th Jokers Card
 
kleptos's Avatar
 
Join Date: May 2002
Location: The Dark Carnival
Posts: 243
kleptos
Send a message via ICQ to kleptos Send a message via AIM to kleptos
Too bad MySql cant handle procedures.....
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old December 9 '09, 06:16 PM (#6)
alligatortek is offline
WDF Regular
 
alligatortek's Avatar
 
Join Date: December 2009
Location: Chicago, IL
Posts: 100
alligatortek is an unknown quantity at this point
Quote:
Originally Posted by kleptos
Too bad MySql cant handle procedures.....
oh yeah?

http://database-programming.suite101..._and_functions

http://www.databasedesign-resource.c...rocedures.html

http://www.devshed.com/c/a/MySQL/A-D...ures-in-MySQL/

http://www.tutorialized.com/tutorial...Tutorial/36961
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old December 9 '09, 06:24 PM (#7)
Wired is offline
WDF Alien Overlord
 
Wired's Avatar
 
Join Date: April 2003
Posts: 6,369
Wired is just really niceWired is just really niceWired is just really niceWired is just really nice
Send a message via AIM to Wired
DUDE, that post was from 6.5 years ago. Things change!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old December 9 '09, 06:26 PM (#8)
alligatortek is offline
WDF Regular
 
alligatortek's Avatar
 
Join Date: December 2009
Location: Chicago, IL
Posts: 100
alligatortek is an unknown quantity at this point
didn't see the date, sorry I'm new here.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old December 10 '09, 04:23 PM (#9)
smoseley is online now
WDF Moderator
 
smoseley's Avatar
 
Join Date: March 2003
Location: Miami, FL
Posts: 8,719
smoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud of
LOL

Procedures are still iffy with PHP/MySQL. Not sure if they fixed it after 5.2.8, but php had a problem where if a procedure returned a resultset, PHP wouldn't load it.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

  Web Design Forums » Programming Help » Other Languages

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
ASP.NET connecting to SQL Server 2005 bfsog Other Languages 2 July 24 '08 02:13 PM
phpMyAdmin ... sql to php estes53 PHP 7 May 16 '07 09:07 PM
Practicing SQL Wired PHP 0 November 2 '03 02:59 AM
SQL Server 2000 backups to a client machine that doesn't have SQL Server TheGAME1264 Database Systems Help 21 August 11 '03 01:34 AM
Nifty SQL Server @@ Properties smoseley Other Languages 0 March 28 '03 03:22 PM

 
User Infomation
Your Avatar

Site Of The Month
Nominate Your Site Now!

Advertisement
WolfCMS.org

Latest Articles
- by RickM
- by bfsog

Advertisement

Partner Links



All times are GMT -4. The time now is 02:01 PM.


WebDesignForums.net is Copyright © 2010 RikeMedia.

SEO by vBSEO

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164