The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 18, 2019, 11:48:53 PM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: SQL injection Protection  (Read 4268 times)
nutballs
Administrator
Lifer
*****
Offline Offline

Posts: 5627


Back in my day we had 9 planets


View Profile
« on: June 15, 2007, 10:54:41 AM »

So I am retooling my stuff, and was wondering about my SQL injection protection methods.

the things I concern myself with are Single Quotes, which i escape out in MSsql with doubling which is how you're supposed to do it. so ' become ''
for numeric fields i test to make sure the input is numeric.
I also validate on secondary lookups, such as, pulling data from 1 recordset to lookup in another.

but im starting to wonder if there are any other things I need to block?
I am trying to think if there is a way to build a function that will do all my query cleaning for me, but i keep getting stuck on some things.
If i just test the variable input, and it is rejected, like in the case of a numeric for example: 0; DELETE FROM Orders;--
That would mean i need to do error handling on every single SQL query at the point of every query, which sucks if you ask me.

Anyone handle this a decent way for complex, multi-lookup systems? Without using some system specific garbage like Magic Quotes and such.

my thought is prevalidation, but then i still would need to specially handle the error gracefully by wrapping the lookup in an "If valid" test, and then display an explaination to the user. But when you have many lookups in a row, this becomes a bit confusing to deal with.

thoughts?
Logged

I could eat a bowl of Alphabet Soup and shit a better argument than that.
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #1 on: June 15, 2007, 12:01:48 PM »

Of course the most immediate way is to NEVER allow a parameter to get to a SQL statement. The only real exception being in "Search This Site" kind of queries, but then you've limited it to one place you need to watch. A little set of functions around your DB can be helpful too ... if you're expecting and integer, then do something like $myInt = (int)$inputVal - or even better, $myInt = ($inputVall++)-- ...then you can easily trap for a zero when you wanted a non-zero and such...

I personally do a lot of "cases" where the passed parameter works its way through a switch statement and then I build the SQL not the parameter.

If you could put some examples of queries you might be expecting ... and are worried about ... that'd be helpful too

/p
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
nutballs
Administrator
Lifer
*****
Offline Offline

Posts: 5627


Back in my day we had 9 planets


View Profile
« Reply #2 on: June 15, 2007, 02:00:46 PM »

(deleted the stupid graphic cause i was not thinking straight.)

The best example of what I need cleaned is data from rest queries.
But also from user input forms obviously.
But mostly its random input which both adds data to the database, or looks up. The biggest issue though are lookups that trigger further lookups. (secondary lookups)

The problem is not that I cant validate the data, obviously i can. I am trying to figure out a way to make the process easier and simpler to keep track of.

I guess i was just trying to figure out if there was a way to validate as im building the SQL string. I guess I really just am stuck with validation first, then building the sql string.  oh well.

i dont really know what I was thinking actually.
« Last Edit: June 15, 2007, 02:13:15 PM by nutballs » Logged

I could eat a bowl of Alphabet Soup and shit a better argument than that.
nutballs
Administrator
Lifer
*****
Offline Offline

Posts: 5627


Back in my day we had 9 planets


View Profile
« Reply #3 on: June 15, 2007, 02:47:48 PM »

ok, to add to this...

the reason this is a pain in the ass for me is because of doing INSERT statements without knowing which columns the user submitted data for.

take this example.
fname
lname
age
city
state
zip
fav food

all but fname and last name are optional.
so the query generically looks like this:
insert into table (fname,lname,age,city,state,zip,favfood) values ('abe','bigpants',55,'tempe','az',12345,'ice cream')

the problem is that building this query is done in two parts, the column names, and the values.
if they are string, no problem, since the values would just end up being empty quotes.
but the numbers end up being empty, and as such the query would look like this:
insert into table (fname,lname,age,city,state,zip,favfood) values ('abe','bigpants',,'','',,'')

this obviously blows up because of the ,, for the numeric fields.

so either i wrap every single column in an IF statement, which frankly is rediculous on some of my inserts, which are huge.
or
I do a "select * from table where 1=0" which opens the table, returning no records, but does return the columns, and doesnt cost much. but the fact is i have never liked that, because I never understood what it really is doing.

im just trying to find a better way.
Logged

I could eat a bowl of Alphabet Soup and shit a better argument than that.
nop_90
Global Moderator
Lifer
*****
Offline Offline

Posts: 2203


View Profile
« Reply #4 on: June 15, 2007, 04:29:24 PM »

In python you have sqlobject,
it maps sql statements to objects, also it abstracts the db
ROR has something similar

would you not better off to use something like that ?
Logged
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #5 on: June 15, 2007, 10:03:42 PM »

I agree... is there no abstraction layer you can use to describe the dataset rather than talk directly to it? They exist in PHP as well, but I wrote my own since I never knew where the source data was coming from... basically I throw and XML record into a function and it does the update for me. Metadata tells the function what the table is about, so that I can validate and refuse or supplement where necessary (a zero for a blank). As it happens, this metadata is also used to describe CRUD screens as well.
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
nutballs
Administrator
Lifer
*****
Offline Offline

Posts: 5627


Back in my day we had 9 planets


View Profile
« Reply #6 on: June 16, 2007, 07:48:46 AM »

thats what ADO is, but i dont generally go that deep into using the built in functions. i just use the surface stuff like open and addnew, but keep most of the work in the query itself.

i guess its time for an ado refresher.
Logged

I could eat a bowl of Alphabet Soup and shit a better argument than that.
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!