News for hackers. Stuff that matters.
Re:A Big Case of Oops by vital101 on Friday February 26, 2010 @09:08AM (#1153136)
It's easy to tell someone about security, but actually demonstrating vulnerabilities really drives the problem home. That said, something as simple as NOT trusting any input and sanitizing it would solve 90% of these problems.
Re:A Big Case of Oops by by on Friday February 26, 2010 @09:59AM (#1153223)
"Screw input validation, love output normalization."

http://news.ycombinator.com/item?id=785549

Re:A Big Case of Oops by generalk on Friday February 26, 2010 @10:29AM (#1153280)
Output normalization: solves various problems like log injection and XSS

Input validation: solves SQL and other injection issues.

If you're not doing both, you're probably vulnerable somewhere. There is no magic bullet.

Re:A Big Case of Oops by by on Friday February 26, 2010 @10:52AM (#1153330)
Ah, I am classifying a SQL statement as an output that is sent from the program to the database. Anything you put in the SQL will have to be normalised.

For example, you might have a name in the database. What if someone is called O'Beirne? How do we get this into the database? Do we only allow them to have the name OBeirne?

Re:A Big Case of Oops by generalk on Friday February 26, 2010 @11:23AM (#1153419)
Sure, that's fine.

But what if someone passes you an email address that's really 500 comma-separated email addresses?

What if someone passes 'admin=true' to your user update URL?

Your output sanitization doesn't do much for those.

Re:A Big Case of Oops by by on Friday February 26, 2010 @12:02PM (#1153525)
You are quite right, you do need to white-list the valid inputs for reasons other than SQL injection.

I did not explain very clearly. What I was trying to say was that output normalisation will prevent SQL injection attacks.

I am considering the components of the SQL statement, such as the string O'Beirne, to be things that have to be normalised before being output to the database in the SQL statement. This output normalisation, as the other replies to my post have correctly said, is best done for you by a library. It cannot be done properly by restricting the inputs, as my O'Beirne example shows, only by output normalisation of the SQL.

Re:A Big Case of Oops by tptacek on Friday February 26, 2010 @01:22PM (#1153730)
But output normalization will not prevent SQL Injection attacks, so I'm pretty unclear on what you're trying to say.

I think you're trying to say that content neutralization (turning ' into &quot;, for instance) stops SQLI. It might or it might not, depending on the vector (tablespace injection doesn't care about metacharacters, for instance). It's at least more accurate than saying "if you make sure that the web app doesn't spit out [!@#$%^&*(){}:"<>?] you're safe".

Re:A Big Case of Oops by by on Saturday February 27, 2010 @04:26AM (#1155132)
Maybe the words 'output normalisation' are the point of confusion. I am using them as in this thread

http://www.reddit.com/r/programming/comments/86kgp/xss_cross...

which is the context of my quote of larholm above and is a discussion about this page

http://www.owasp.org/index.php/XSS_(Cross_Site_Scripting)_Pr...

Perhaps this is not common usage, but within this context I believe I am correct in saying output normalization is what prevents SQL injection.

larholm goes on to say:

"The lack of output normalization IS the security vulnerability."

"You can either normalize your output for each specific location as you encounter it, or normalize your input once in advance for all current and future output locations."

"The former beats the latter, as it is impossible for you to know how the data will be output in the future."

which also seems correct.

What is "tablespace injection"? I just googled it and there are no references to it anywhere.

http://www.google.com/search?q=%22tablespace+injection%22...

Re:A Big Case of Oops by InclinedPlane on Saturday February 27, 2010 @06:58AM (#1155204)
"But what if someone passes you an email address that's really 500 comma-separated email addresses?"

If I understand the idea of output-normalization correctly you'd keep an email address that's really 500 comma-seperated addresses in the database in exactly that form, but when it came time to send an email you would normalize the value, and at that stage you would check for such a case and handle it as appropriate (i.e. remove all commas, send to only the first, etc.) The point is that you don't try to create a stored value that can be used safely everywhere, rather you make the value safe at each instance where it's used.

P.S. This appears to be the relevant conversation, interesting reading: http://www.reddit.com/r/programming/comments/86kgp/xss_cross...

Re:A Big Case of Oops by crux_ on Friday February 26, 2010 @11:25AM (#1153429)
Ignore that other person saying to escape the quote -- do as the wikipedia article they linked says, and use parametrized queries. This does all the escaping for you, and unlike escaping, makes it much less likely that something will slip between the cracks.

(There's almost never a good reason to construct SQL on the fly.)

Re:A Big Case of Oops by fendale on Friday February 26, 2010 @12:14PM (#1153562)
Exactly correct - and if you are using Oracle and not using parameterised queries (known as bind variables in the Oracle world), you are quickly going to have performance problems too. That's not the case with MYSQL as it doesn't have a cursor cache in the same way Oracle has. I have no idea about Postgres or SQLServer.

A rule of thumb is that if you are concatenting user input strings into your SQL query strings you are doing it wrong.

Re:A Big Case of Oops by tptacek on Friday February 26, 2010 @01:28PM (#1153752)
Remember that parameterized queries aren't a panacea. There are plenty of things that don't parameterize well, such as table names, sort orders, and limits.
Re:A Big Case of Oops by epochwolf on Friday February 26, 2010 @11:45AM (#1153480)
Parametrized queries still use escaping. The only difference is instead of you doing the escaping and query construction you're letting someone else's code handle it for you. (Code I would assume is far better tested than yours)
Re:A Big Case of Oops by tptacek on Friday February 26, 2010 @01:18PM (#1153719)
This isn't really an accurate description of what's happening in a parameterized query. In most cases, parameterized queries have already been parsed by the time inputs are interpolated into them; if you look at the protocol on the wire, you can see the difference between query structure and slots.

SQL Injection vulnerabilities happen when inputs influence the parsing of the query. You can't do that when the database already has an AST for the query.

Parameterized queries are not just a different kind of quoting.

Re:A Big Case of Oops by crux_ on Friday February 26, 2010 @12:35PM (#1153607)
a) More importantly, they enforce a discipline on you: never treat data as code.

b) I'm guessing there are plenty of databases that execute parametrized queries entirely differently than a plaintext ones, including the absence of an escaping step. Since the whole point of quoting + escaping is to demarcate the "data" parts of a query, and you've already done that with parameters, there's no reason a query engine can't use the parameter directly rather than run it through a useless escape then parse-and-unescape cycle.

Re:A Big Case of Oops by smackjer on Friday February 26, 2010 @11:13AM (#1153389)
Re:A Big Case of Oops by tptacek on Friday February 26, 2010 @01:26PM (#1153743)
Escaping will get you killed. Do you know what %u2032 means? +ACc-? %ef%bd%b1? Parameterize the query.
Re:A Big Case of Oops by brlewis on Friday February 26, 2010 @02:09PM (#1153858)
Ignoring the part about PHP not knowing the difference between a byte and a character...%2032? There are databases where something other than ASCII single quote will terminate a string?
Re:A Big Case of Oops by tptacek on Friday February 26, 2010 @02:27PM (#1153906)
Sure. It depends on the database, the way the query is constructed, and the way the handle is initialized. But the bigger problem is what the web stack does to the query before it hits the database.

People have been playing charset games to get past SQL quoting for almost 10 years now, and not just in PHP.

Re:A Big Case of Oops by dstorrs on Friday February 26, 2010 @05:01PM (#1154266)
Not always. A few years ago, I demonstrated for a business owner a simple set of breaks to his website that allowed me to download, install, and execute code on his server and then use it to look and and download every file on the system. He understood that his source code (the "crown jewels" as he called it) was therefore vulnerable, but never did anything about it.