NOTE: This is not a question about what is SQL Injection, but rather a question to clear up what the actual vulnerability, particularly given some specific test cases.
Background: Please see this video by a group called Modern Rouge. Or, you can ignore it, as it's kind of oversimplified for people who don't have technical skills and aren't already familiar with SQL Injection. I will point our the important part when it's needed.
So, for those who don't know what SQL Injection is (I'm just adding this for completeness and to give background to my particular question), Let's say I have PHP code like this:
$query = "SELECT * FROM users WHERE username='$username'";
This code is vulnerable because let's say a malicious end user puts in their username as ' or 1=1;--
, this could make the final query
SELECT * FROM users WHERE username='' or 1=1;--'
Which does not have the intended effect.
So, now watch the video around the 4:00 mark.
The example they give on their test website for SQL injection, they use SQL injection to bypass the password check.
Here's what I don't get, In real life, wouldn't I check the password in PHP instead, making this specific example useless? For example:
if($userRecordFromDB["pwd"] == $pwd) { // User authenticated.
Am I right that SQL injection can't bypass my PHP authentication? What is the vulnerability here? Unless I'm tying the password check to my query (which I'll admit could have been done for the example), even if I have a vulnerable query, my site shouldn't be vulnerable.
Second related question: The video goes on to imply that a single vulnerability could allow attacker access to my whole db! I'm assuming they mean an attacker could do something like so:
SELECT * FROM users WHERE username='' or 1=1; SELECT * FROM creditInformationTable where 1=1--'
$username
of course would be ' or 1=1; SELECT * FROM creditInformationTable where 1=1--
However, this also confuses me. Unless I put this data into a nice table or something for them, wouldn't the data never leave the back-end, even if the query was vulnerable? How could they possibly get this information unless I give it to them inadvertently?
Which leads me to the bigger question: What is the danger of SQL injection? Is it purely theoretical, or are there real cases where an attacker could do something like login or access all the tables in your DB from pure SQL injection?
Edit: Scratch all that. Let me narrow a bit.
How does it leave the back-end? Even if something queries the wrong thing, how would my attacker get it? DB APIs like PDO and SQLI return the information to PHP, NOT the resulting page. Shouldn't a well written PHP script catch that wrong data is there, or, at the very least, not echo it all out to the user?