r/netsec • • Mar 25 '16

SQL Injection Cheat Sheet by Netsparker

https://www.netsparker.com/blog/web-security/sql-injection-cheat-sheet/
566 Upvotes

22 comments sorted by

26

u/Vakieh Mar 26 '16

Who in [current year] is handling injection protection manually? Even lolphp has PDOs to sort all of this out with a single bullet.

Prepared statements with parameters stop every issue on that list, so long as you aren't a moron and remember to use a single entry point for your database.

16

u/IncludeSec Erik Cabetas - Managing Partner, Include Security - @IncludeSec Mar 26 '16

You give developer training too much credit. We see a ton of dynamic sql and blacklist approaches to defend against SQLi.

I don't blame the devs for any of this though. I blame the books, frameworks, and schools they've learned from. Today's devs are standing on the shoulders of the broken giants who came before them.

12

u/twiggiestbowl Mar 26 '16

I agree with using magic like PDO and prepared statements. However I also think that understanding how and why the magic trick works is beneficial, ya know what I mean?

3

u/Vakieh Mar 26 '16

What in this link tells you how PDO works? You absolutely should know how PDO works before you use it, else you might use it wrong.

But 'this is what might happen if you don't use it' is useless information. If you don't handle threading properly all sorts of things can happen, if you don't handle object equality properly all sorts of things can happen, if you don't handle network timeouts properly all sorts of things can happen - it's enough to know that it's bad and you should avoid it, anything more is a useless exercise.

4

u/twiggiestbowl Mar 26 '16

Well, nothing specifically, the article is about injection. I was just pointing out that one should understand injection when using prepared statements and not knowing why. Understanding how PDO actually combats injection via prepared statements is a different story.

Your other points are agreeable.

4

u/arbitrarion Mar 26 '16

I like to think of this information as "management motivation". Only a few years back I found an SQL Injection vulnerability and had to explain why it was bad and we should put in the effort to fix it.

I overheard my boss telling some other manager about it: "now we can run arbitrary sql statements from this text field". The response was "is this a new feature?"

So I guess you can say that this information is only useless if you already have it.

4

u/cybergibbons Mar 26 '16

An awful lot of people. SQL injection and command injection are incredibly common.

4

u/jephthai Mar 28 '16

This sort of indignant, "But who is dumb enough to make vulnerable apps these days?!?!?" attitude is common on /r/netsec. The answer is obviously everyone that still gets caught with SQLi. I still find it in apps that are built using frameworks that feature centralization and protective features -- if you try hard enough you can still make a vulnerable form.

One of my favorite moments last year was a test where there were 183 forms. I made a list of them, and worked through each one testing for SQLi (couldn't use an automated tool because it was forbidden by RoE). Slow-going and monotonous, but that's how app testing can be sometimes.

It was the 181st form that had it -- the only one out of the 183 that was different. My guess is it was an emergency change that had to go in for some customer and somebody took a speedy, pragmatic, but unwise path and made a one-off form. And this was a Fortune 500 household name tech company that has billboards and everything. It happens.

2

u/Vakieh Mar 28 '16

There's a lot about netsec that is hard to get right - it's tedious, it's expensive in both money and manhours, and you often can't measure it effectively. So it really doesn't surprise me that people will make vulnerable apps, for the same reason that 1/ however many planes still fall out of the sky; life sucks sometimes.

But I just can't comprehend how people can fail to read even the first StackOverflow answer to a question where it SCREAMS "no no no use PDO or you will die". The most important stuff has been made so easy to get right.

Then again, I have spent a whole lot of time explaining this document to people.

3

u/juken Mar 26 '16

Plenty of business critical apps were written many years ago.

1

u/networkraptor Mar 31 '16

Your second paragraph is the exact reason why SQLi works, and will most likely continue to work.

2

u/Vakieh Mar 31 '16

so long as you aren't a moron and remember to use a single entry point for your database

This part, you mean?

Shitty programmers write shitty programs, but that's the sort of thing any half decent profiling scanner should be able to pick up on.

1

u/networkraptor Mar 31 '16

This is the kind of institutionalized thought that gets security teams into trouble. Just because it shouldn't happen, doesn't mean that it won't happen.

People bypass processes and do whatever the hell they want. I have seen Fortune 500 companies do the dumbest shit, not patch systems in months (I still see Heartbleed vulns all over), and not get authorization for firewall rules, all because it's a system that generates money for the company. Oh, and you are assuming that people actually scan things.

2

u/Vakieh Mar 31 '16

So do you solve that by stripping characters like this link describes, or do you fix that by fixing the processes involved?

Scans shouldn't be a case of people running them, either. Get them into the infrastructure and have them run themselves.

14

u/tolos Mar 25 '16

What about some tips to avoid things like this? I think I've got parameterized input down (entity framework in c# or sqlalchemy in python, and don't use raw sql queries), but I feel like there are a bunch of other ways for things to go wrong. For instance, I have a website where a user can enter arbitrary text to search a field in the database, and I strip out all [^a-zA-Z0-9 ], or check that a non-negative integer input is actually a non-negative integer, but I'm never sure if there are other things I should do.

13

u/ThisIs_MyName Mar 25 '16

user can enter arbitrary text to search a field in the database, and I strip out all [^a-zA-Z0-9 ]

As long as your DB supports Unicode (and it should!), you don't have to do this. Let them search for emojis if they really want to. If your code can truly determine whether the user is entering silly stuff, just show them a warning saying as such.

check that a non-negative integer input is actually a non-negative integer

That's a good place to start defense in depth. I try to ensure that when a function takes ints as input, it can handle all values in that variable. So if I accidentally call the function without sanitizing negative inputs, the function will throw an exception instead of doing something unexpected.

3

u/[deleted] Mar 25 '16

Do you take in parameters like orderBy from requests? Those cannot be parameterized in a query. If so I would recommend either a whitelist, or do all that sorting in the view.

3

u/moderatorrater Mar 26 '16

or do all that sorting in the view.

That's terrible. Never do that. Whitelist all the things.

2

u/kageurufu Mar 26 '16

When using sqlalchemy, I use my models with getattr and a whitelist.

Basically sa.desc(getattr(model, colname))

1

u/[deleted] Mar 26 '16

Stripping out characters is only really useful if you know you will never have those characters in the database.

There's no harm it not doing it if your other stuff is set up correctly.

0

u/[deleted] Mar 25 '16

[deleted]

2

u/flameswor10 Mar 26 '16

Using PDO with parametized queries should help, but its always a good thing to validate most user input if possible.

1

u/tolos Mar 25 '16 edited Mar 26 '16

I haven't used PHP in 10+ years.

edit: this is just a statement saying I'm not qualified to answer, so someone else will need to chime in. I didn't know lacking experience was so unpopular.