Slashdot Mirror


The History of SQL Injection, the Hack That Will Never Go Away (vice.com)

An anonymous reader writes with this history of SQL injection attacks. From the Motherboard article: "SQL injection (SQLi) is where hackers typically enter malicious commands into forms on a website to make it churn out juicy bits of data. It's been used to steal the personal details of World Health Organization employees, grab data from the Wall Street Journal, and hit the sites of US federal agencies. 'It's the most easy way to hack,' the pseudonymous hacker w0rm, who was responsible for the Wall Street Journal hack, told Motherboard. The attack took only a 'few hours.' But, for all its simplicity, as well as its effectiveness at siphoning the digital innards of corporations and governments alike, SQLi is relatively easy to defend against. So why, in 2015, is SQLi still leading to some of the biggest breaches around?"

33 of 193 comments (clear)

  1. Everyone has to learn about it. by Anonymous Coward · · Score: 2, Insightful

    Each year brings a fresh crop of computer science graduates into the industry, barely any of them having a clue about attacks like this. Many of them will make these mistakes and learn about defending against them the hard way.

    Maybe a few schools teach about this now. Maybe a few companies will pair senior devs with new devs to transfer this knowledge on the job. Even so, there will be enough new programmers who don't know this, and enough companies who eschew senior talent as a cost-savings measure, that this vulnerability will continue to rear its ugly head.

    1. Re:Everyone has to learn about it. by Rei · · Score: 4, Interesting

      Even older programmers do it - I discovered just last Friday that a senior programmer I work with had written a service that was vulnerable to SQL injection (purely by accident, I was using it and noticed how it was mishandling apostrophes).

      I think most programmers will do it sooner or later until either they've A) internalized the warnings from others, or B) been caught by someone noticing (and potentially exploiting) vulnerable code that they've written. And if neither A) nor B) happen immediately, they may well be serial abusers.

      Also a lot of programmers seem to forget that injection doesn't just apply to SQL. Shell command insertion is also a huge abuse target.

      --
      Hello from Sputnik 2. I am receiving you.
    2. Re:Everyone has to learn about it. by unencode200x · · Score: 4, Insightful

      It's irresponsible to continue to do this. With stored procedures, ORMs (there are some good ones out there, I use Linq a lot), and parameterized queries available in all the major languages I can't help but wonder if people are just incompetent.

      Also, validate and sanitize your input data man. If you're writing code for the web you *have* to do this, no excuses. Albeit, most "web developers" I've seen don't have a clue. Now, get off my lawn!

      --

      Chance favors the prepared mind.
      Perfect is the enemy of good.
    3. Re:Everyone has to learn about it. by OzPeter · · Score: 4, Insightful

      It's irresponsible to continue to do this.

      I was browsing Stack Overflow the other day and looked at an SQL/PHP based question. The poor guy asking the question was obviously a n00b who was just starting to code, and had googled around to find a solution to his problem but it wasn't quite working for him (and hence the SO question).

      From what I saw the problem wasn't that he was a stupid n00b, it was that his googling had turned up horrendously bad PHP code (using ancient DB connection style code, plus totally SQL injection ready) and he didn't know the difference between that code and best practices. So it seems that part of the problem is the act of using google itself and how good code and bad code examples are presented as equals solely based on what ever google's page rank algorithm de jour is. And I can't see how you can fix that without purging google of all the bad code examples.

      --
      I am Slashdot. Are you Slashdot as well?
    4. Re:Everyone has to learn about it. by Opportunist · · Score: 2

      This just in, cargo-cult programming causes security holes.

      That's not really news, is it?

      --
      We used to have a Bill of Rights. Now, with the rights gone, all we have left is the bill.
    5. Re:Everyone has to learn about it. by gman003 · · Score: 2

      That's what PHP frameworks are for. The main thing I look for in a framework is actually just a good DB interface. One that simplifies query construction and automatically escapes data. I've written some surprisingly complex queries using CodeIgniter's Active Database system (Laravel has a very good one too). About the only time I have to write SQL by string concatenation is when I do crazy subselects or unions.

      I don't know why that functionality is bound up with frameworks. I think it's because PHP's managers are trying to forge a simpler, elegant language out of it, reacting to the throw-it-in nature of early PHP, but they've gone overboard with both that, and contradict themselves by keeping broken functionality around for compatibility.

    6. Re:Everyone has to learn about it. by viperidaenz · · Score: 3, Interesting

      What's wrong with prepared statements?
      http://php.net/manual/en/pdo.p...

      That last time I touched PHP, over 10 years ago, you could do this.

      But then there was crap like this: http://php.net/manual/en/funct... Perhaps they planned to make a mysql_fake_escape_string function but instead accidentally called it mysql_escape_string

    7. Re:Everyone has to learn about it. by DerPflanz · · Score: 2

      Damn you are right. I did the simple "mysql php tutorial", and except for the first hit (from W3Schools), ALL of them used the old, deprecated mysql_* functions and were wide open to SQL injection.

      Is there something the programmer's community ought to do? What could we do?

      --
      -- The Internet is a too slow way of doing things, you'd never do without it.
    8. Re:Everyone has to learn about it. by WaffleMonster · · Score: 5, Informative

      It's irresponsible to continue to do this. With stored procedures

      Does using stored procedures solve SQL Injection? Show of hands... all of you who raised yours are part of the problem.

      Also, validate and sanitize your input data man. If you're writing code for the web you *have* to do this, no excuses. Albeit, most "web developers" I've seen don't have a clue. Now, get off my lawn!

      The number of people who incorrectly believe SQL Injection is in any way related to data validation means the problem will never go away. SQL Injection is a failure to enforce context and has got exactly nothing to do with content.

      The data validation misinformation is so prevalent the only way you are probably even reading this is you regularly browse -1 as many of you will have modded my comment into oblivion.

    9. Re:Everyone has to learn about it. by MobyDisk · · Score: 2

      I'm curious to know why a senior programmer was writing code to handle apostrophes in the first place when that is probably built-in to whatever library you use. I'm legitimately interested, if you wouldn't mind following-up with a reply at some point. The answer is probably to the heart of why SQL injection continues to be an issue.

    10. Re:Everyone has to learn about it. by Rei · · Score: 4, Informative

      I think you're confused. They weren't "writing code to handle apostrophes". They wrote a code review-related microapp, and I noticed during the submission of a review that it failed, gave an detailed error message (which I should probably warn him is also bad practice, you don't help would-be hackers out by publicly such information) indicating a particular bad SQL statement, and the statement was "bad" because it wasn't sanitizing the apostrophes in my input. It was immediately clear that he was building his query through simple string concatenation.

      The application was designed for internal usage (though I'm not sure if it was externally accessible)... but even if it wasn't externally accessible, the fact that he would write SQL queries through string concatenation is a problem, in case he ever would write an externally-accessible application for us. I'm not disappointed or upset with him or anything, because lots of people do that, it's a natural way for someone who doesn't realize the risks to do it. People are used to building up strings through concatenation - for example, for displaying text in a GUI. A large chunk of programmers just don't know the risks that exist in certain contexts.

      --
      Hello from Sputnik 2. I am receiving you.
    11. Re:Everyone has to learn about it. by i.r.id10t · · Score: 4, Interesting

      As soon as PHP totally gets rid of the mysql_ family of functions, and forces everyone to convert to mysqli function/objects or PDO, it will fix itself. As a bonus, quite a few folks may make some beer money fixing all of the suddenly broken scripts...

      The problem with search results is that the mysql_ functions for a loong time were the only way to do the task, and with the plethora of tutorials/information out there the sheer number of them overwhelm the "new" stuff (mysqli and/or PDO)

      --
      Don't blame me, I voted for Kodos
    12. Re:Everyone has to learn about it. by hey! · · Score: 2

      What would be nice is if they learned about it before they develop habitual patterns for using a language/platform.

      The problem is that people who teach n00bs want to give them the success experience of updating a database early on, before they've learned about prepared statements and (the much broader topic) of checking user input. If they'd just stop that then over the course of years the problem would become much smaller.

      --
      Post may contain irony: discontinue use if experiencing mood swings, nausea or elevated blood pressure.
    13. Re:Everyone has to learn about it. by LiENUS · · Score: 2

      For the simplest selects and inserts PHP could easily provide an interface that escapes the arguments. That would make it more natural for the lazy programmer to realize when he is doing queries that he has to think about.

      If you're escaping your sql inputs you're part of the problem. SQL injection has been known to work on escaped strings using unicode tricks. Use bound parameters in parameterized queries and its not an issue at all.

    14. Re:Everyone has to learn about it. by gbjbaanb · · Score: 3, Insightful

      The problem is inherent in many systems so you will always make a mistake until the day that you put all your queries into stored procedures.

      Treat the DB as a generic object pool of crap and it'll be that. Treat it like its a precious storage system with its own (customisable) API and you'll do far better.

      But of course, slapping SQL together in the client and sending it to the DB to parse and execute is so much easier everyone does it.

  2. Re:Hackers use the integrate face system by davester666 · · Score: 2

    So, Bobby Tables will have his name tattooed on his face...

    --
    Sleep your way to a whiter smile...date a dentist!
  3. PHP by Bogtha · · Score: 4, Interesting

    So why, in 2015, is SQLi still leading to some of the biggest breaches around?

    Because typical PHP tutorials still teach old, broken ways of doing things and this shows no signs of abating. Go ahead and search the web for things like php mysql tutorial. The top hits are crap like this, written by incompetent developers who don't know what they are doing. PHP developers learn from crap like that, then they go on to write their own tutorials that are the same or worse.

    And before you start, yes, this is something where PHP is stand-out bad. Go ahead and try the same searches with other languages. There is a vast difference in quality of learning materials. I mean, PHP had XSS vulnerabilities in its official tutorials until relatively recently. Newbies don't stand a chance in those circumstances.

    --
    Bogtha Bogtha Bogtha
    1. Re:PHP by phantomfive · · Score: 3, Insightful
      There's a quote from Theo de Raadt that is relevant here:

      “When you know exactly what the APIs are, you’ll spot the bugs very easily. In my mind, it is the same as any other job that requires diligence. Be careful. Humans learn from examples, and yet, in this software programming environment, the tremendous complexity breeds non-obvious mistakes, which we carry along with us, and copy into new chunks of code.
      We’ve even found in man pages where functions were mis-described, and when we found those, lots of programmers had followed the instructions incorrectly”

      --
      "First they came for the slanderers and i said nothing."
    2. Re:PHP by Bogtha · · Score: 5, Informative

      This is not a PHP thing, but a bad-developer thing.

      I guess you didn't read past my first paragraph? Please do.

      You can write the same crap in Java, .NET, Python or any language you want.

      Go and search the web for tutorials in those languages. You will find that the situation is vastly better with these languages compared with PHP.

      That's not PHP's fault.

      It is - on many fronts.

      Firstly, the language promoted for many, many years, a confusion between the various layers of the application. The whole magic quotes nonsense was an attempt to fix a problem relating to the database layer in the HTTP layer. This confused PHP developers for over a decade, and even though it has since been removed, it was in there for so long that an entire generation of PHP developers had their brains twisted out of shape with this confusion.

      Secondly, the official documentation was super bad for years. Security vulnerabilities in the official tutorial for years, for example.

      Thirdly, the API design is so bad it practically pushes unsuspecting developers into the wrong solution. addslashes()? No, use mysql_escape_string(). Oh wait, wasn't that mysql_real_escape_string()? Or perhaps mysql_really_really_i_promise_to_do_it_right_this_time_escape_string()?

      Finally, the PHP community right from the very top embraces shitty practices, like ignoring failing tests in a release build. Again, a source of security vulnerabilities that simply doesn't need to happen.

      Yes, you can write bad code in any language. But that doesn't mean that all languages are equal. PHP is far, far worse at this than its contemporaries and you shouldn't make excuses for it.

      --
      Bogtha Bogtha Bogtha
    3. Re:PHP by PyramidOfDoom · · Score: 3, Informative

      Thirdly, the API design is so bad it practically pushes unsuspecting developers into the wrong solution. addslashes()? No, use mysql_escape_string(). Oh wait, wasn't that mysql_real_escape_string()? Or perhaps mysql_really_really_i_promise_to_do_it_right_this_time_escape_string()?

      To be fair to PHP here, the mysql_escape_string vs mysql_real_escape_string is mostly MySQLs fault (watch http://www.youtube.com/watch?v... if you want an insight into how screwed up MySQLs development was). It's one of their old C libraries that added the mysql_real_escape_string function.

      PHP is not entirely blameless on this front; back in the early days of PHP Rasmus Lerdorf (PHP's creator - read and weep) thought the best way to design an API is to just expose PHP's internal libraries' headers to PHP code, consistency be damned. That's how we got mysql_real_escape_string; Rasmus was just following past form.

      PHP has had SQL injection licked for at least the past 10 years (literally every database driver except the deprecated mysql_* one supports binding values to a query), it's just that all of the tutorials are utter, utter garbage and STILL use mysql_* functions.

      mysql_* is dead as of PHP 7 (which is pretty imminent now), thank god.

      Finally, the PHP community right from the very top embraces shitty practices, like ignoring failing tests in a release build. Again, a source of security vulnerabilities that simply doesn't need to happen.

      It comes from the top down. Try running PHP's own tests some time. You'll be utterly unsurprised by the result. They're also written in PHP, which I feel is perhaps something of a design flaw.

    4. Re:PHP by PyramidOfDoom · · Score: 3, Interesting

      I think that you goading someone into hacking your websites and then claiming that they are secure when they can't is actually quite disingenuous here. To be perfectly honest, from your post history I believe you have some vested interest in this banshee framework you keep linking to.

      I did actually have a quick look at your websites and they don't actually present a lot of attack surface to work with. The only user input that's used is some id parameters from what I can see. It's utterly trivial to secure such a tiny attack surface, so I doubt there's anything much to be worked with unless you really screwed up.

      However, having such little attack surface also implies that there's little reason for PHP to be there at all. You could very easily replace everything with an offline static site generator and just serve html pages. This completely removes all attack surface from your sites and lowers your server requirements.

      Do you have any more complicated websites to look at? Something that actually accepts user input from people that haven't been pre-vetted would be ideal. In my experience, PHP's warts really start to show when you need to start exposing the ability to persist things to random people on the internet.

      Also is your PHP up to date on those sites? There's some rather nasty CVEs against PHP itself that don't give a damn about your code or framework.

  4. Re:PHP and CGI make it too easy... by Aethedor · · Score: 2

    And also too easy to do it right.

    --
    It doesn't have to be like this. All we need to do is make sure we keep talking.
  5. I just don't get how it happens by Anonymous Coward · · Score: 4, Interesting

    You are absolutely right. I see this all the time as the hiring manager in a web shop. I always present candidates with this question:

    1. Find and fix the potential SQL injection vulnerability: // .. /*@var \PDO */
    protected $dbh; //..
    public function getOption($name)
            $sql = "SELECT val FROM options WHERE name = {$name}";
            $stmt = $this->dbh->prepare($sql);
            $stmt->execute();
            return $stmt->fetchColumn();
    }

    For those who don't know PHP, the answer is:
            $sql = "SELECT val FROM options WHERE name = ?";
            $stmt = $this->dbh->prepare($sql);
            $stmt->execute(array($name));

    Almost no candidates out of school even know what I'm asking them to do. About half of people with experience get it right; a quarter of them understand the question and get it wrong, and a quarter don't understand the question. I find that it doesn't matter how many years of experience they have, about a quarter of programmers just don't understand what SQL injection is.

    I just don't get it. I've spent more than a decade programming in PHP, the language that really made SQL injection a thing because it lacked prepared statements for a long, long time and even then a lot of the input escaping functions were broken. Over those years, I've picked up a lot of bad habits; some were dictated by the shortcomings of PHP4 ("dependency injection? what's that?"), others are a side effect of spending all of my time cranking out single purpose scripts that had to work yesterday ("Ctrl+C; ctrl+V").

    Nevertheless, it still blows my my mind when I encounter people in this day and age who aren't using prepared statements. Concatenating SQL is just so... messy. Seriously, it takes two minutes to write a nice, clean, understandable SQL statement as a string, and at most a three line loop to bind the values. If your are concatenating it together, you have a mess of loops and conditions (comma here?) and strings and array manipulations... It is so much more work.

    Yet I still hire jokers who can't do it because I need bodies to fill seats.

    1. Re:I just don't get how it happens by albacrankie · · Score: 2

      "For those who don't know PHP, the answer is:"

      Except when it isn't. What's the source of that $name parameter to your function? Has it been sanitized in any way? I can imagine your interviewees taking a punt on what kind of idiot they are facing. (Looks like he was born in 1955 so the answer is probably x)

  6. Re: The problem STARTS with SQL itself. by ShieldW0lf · · Score: 2

    Myself, I prefer to use stored procedures for all db interactions, and set table access to deny for the account used by the web server. But that's me.

    --
    -1 Uncomfortable Truth
  7. The reason? A lack of programmers by Opportunist · · Score: 3, Interesting

    No, I didn't write "a lack of people who write code". I exactly mean what I wrote: A lack of programmers.

    What we have today is a load of people who learned programming somehow, kinda-sorta, but without understanding just what they're doing. Now add how most of them gains information about how to do things. By looking it up on the internet. And taking the first solution that looks like it works.

    Of course it's easier to simply concat strings than using prepared statements and parameters. It is simply more readily understandable and less convoluted for people who have little knowledge and less time to gain it. And they don't know anything about security and why this could possibly be a security problem.

    These people are cheaper than people who know what they're doing. So much cheaper even that the additional time they need to get anything done is easily compensated. And whether it is a security problem is usually only found out after a security breach happens because, well, whoever hired them has even LESS knowledge about security.

    And since every year a new batch of people comes out of schools that kinda-sorta learned how to kinda-sorta do queries, this problem will mean job security for me 'til retirement.

    --
    We used to have a Bill of Rights. Now, with the rights gone, all we have left is the bill.
    1. Re:The reason? A lack of programmers by edtice1559 · · Score: 2

      Although I mostly agree with you, I feel compelled to put out that in order to write software today, one must know both the technical programming aspects *and* be an expert on the target domain model. The days of a business analyst writing requirements are over. The world changes too fast. If you're implementing tax software, the IRS publishes new rules and you have to be ready on January 1st. "Poor" code that implements the rules correctly is valuable. "Good" code that doesn't quite understand the domain model and gets some things wrong isn't useful. In terms of the triple-constraints, time is *usually* the primary constraint and quality is the tertiary constraint. The "poor" apps survive because their authors get to market and make money. A *good* security solution is one that would make things go *faster* not slower. Then there would be widespread adoption.

  8. I absolutely WAS. Was a CMS. Much better now by raymorris · · Score: 4, Informative

    Absolutely PHP was originally for for non-programmers. It was a CMS written in Perl, for people who couldn't use the Perl templating systems directly. That was a long time ago, of course.

    It was abused as a general programming language, often by people who didn't know about file permissions and couldn't be bothered to learn how to chmod 755. That wouldn't have been a huge problem if they weren't putting the scripts on web sites, for everyone to attack. That was a big problem for several years - non-programmers who didn't want to learn an actual programming language wrote PHP scripts and PUT THEM ON THE INTERNET. It wouldn't really have been a big deal if they wrote scripts for their own desktop or for their local intranet.

    Now that PHP has been used as a general-purpose web programming language for several years, it has been significantly updated to better fit that role. Current PHP is much, much better than it was a few years ago. It's still relatively easy, though, so it -can- be used by people who are clueless. But now it's also used, correctly, by people who are actually competent.

  9. Pay peanuts by TekPolitik · · Score: 4, Insightful

    Because businesses think software development in general, and especially web development, is easy. They hire monkeys and pay peanuts (or sometimes even serious dollars that could get them quality of they could recognise they were being taken for a ride), and we continue to see the most basic errors being repeated across most web sites. Seriously, the quality of web developers generally is absolutely appalling.

  10. Why? by Lumpy · · Score: 2

    So why, in 2015, is SQLi still leading to some of the biggest breaches around?"

    Easy, idiots writing the apps because companies don't want to pay for skilled people that demand honest wages. they instead outsource ti for the lowest bidder and then bitch when they get crap quality because that is all they were willing to pay for.

    --
    Do not look at laser with remaining good eye.
  11. managers like to promote on 3wk projects by Lobachevsky · · Score: 3, Interesting

    There simply isn't any incentive for to build software that will last through some cyber attack some 10 months or 3 years into the future. The current incentives reward sloppily slapping together something that barely functions and gives a demo without crashing. If your demo crashes and makes the boss look bad, you're fired. If your demo works, has slick graphics and no spelling mistakes and the english dialog is polished, you get a raise. You're building software for the boss's demo, you're not building software that's robust, handles edge-cases, and input sanitizes everything. I meant, you could, but you're not getting paid any extra for it.

  12. Well by Greyfox · · Score: 2

    There's a lot more client/server going on, and a lot of programmers haven't discovered that you can't trust the client and are doing all their input validation and sanitation on the client side. As long as that's going on, this sort of attack is going to be popular. '); drop table articles.

    --

    I'm trying to teach myself to set people on fire with my mind... Is it hot in here?

  13. SQL has no place in applikation persistance. by Qbertino · · Score: 2

    I always wonder why people - even professionals (ableit only the non-DB pros) - think SQL is a feasible means for an application to utilise persistance. It isn't. In fact, it's a huge smelly turd for app-persistance and using it so broadly for this sort of work is a really harebrained and abysmally stupid idea.

    That we have to deal with SQL injection problems is one of the countless pieces of crap based on this technology decision.

    SQL was meant as an end-user interface for interacting with relational database - and for that it is absolutely perfect. End of story.

    Using SQL as intermediate for application persistance is one of the most annoying and studidest things in the history of applikation development - for reasons to countless to list them. DB designers are among those who time and time again shake their heads in disbelief when they see the mess devs do with SQL.

    --
    We suffer more in our imagination than in reality. - Seneca