Hacking Websites with SQL Injection - Computerphile

แชร์
ฝัง
  • เผยแพร่เมื่อ 8 ต.ค. 2013
  • Websites can still be hacked using SQL injection - Tom explains how sites written in PHP (and other languages too) can be vulnerable and have basic security issues.
    More from Tom Scott: / enyay and / tomscott
    Follow the Cookie Trail: • Follow the Cookie Trai...
    CERN Computing Centre & Mouse Farm: • CERN Computing Centre ...
    / computerphile
    / computer_phile
    This video was filmed and edited by Sean Riley.
    Computerphile is a sister project to Brady Haran's Numberphile. See the full list of Brady's video projects at: bit.ly/bradychannels

ความคิดเห็น • 886

  • @RockLou
    @RockLou 9 ปีที่แล้ว +2018

    "; DROP ALL DATABASES;
    dammmit.

  • @Markus9705
    @Markus9705 9 ปีที่แล้ว +2461

    Simply: don't ever ever trust user input.

  • @Kneedragon1962
    @Kneedragon1962 9 ปีที่แล้ว +3972

    One of the better 'funny pictures' I have seen, was a numberplate on a pretty fast car, which had an SQL instruction to drop some tables... hello speed cameras ...

  • @bigsteamfan
    @bigsteamfan 10 ปีที่แล้ว +5341

    He speaks SO LOUD... lol... I think the entire cafe knows how to hack websites by now...

  • @Computerphile
    @Computerphile  10 ปีที่แล้ว +858

    This is the St Pancras Renaissance hotel in London - great that they let us film after our original location fell through - shame they wouldn't let me use lights though! >Sean

  • @hermest99
    @hermest99 8 ปีที่แล้ว +2514

    One finger: "Facebook was originally written in PHP"
    Second finger: All other things.
    Nice one.

  • @capo328
    @capo328 10 ปีที่แล้ว +526

    >"You can read people's passwords..."
    Well hopefully they're hashed anyway

  • @RebeccaSentance
    @RebeccaSentance 8 ปีที่แล้ว +1277

    "It's a hack on top of a hack.... That's a hack, and we've had to put more on top of that, and more on top of that, and more on top of that."
    It's a hack stack!

  • @brettefantomet
    @brettefantomet 8 ปีที่แล้ว +1540

    Tom Scott is awesome! "If you can't explain it to an eight year old, you don't fully understand it yourself!" -Unknown Smart person

  • @MrGeekGamer
    @MrGeekGamer 10 ปีที่แล้ว +352

    "It works, but it's clunky." - PHP in nutshell.

  • @mittfh
    @mittfh 8 ปีที่แล้ว +614

    "Shouldn't work any more but still does."
    Just ask TalkTalk's IT department...

  • @glueee2621
    @glueee2621 8 ปีที่แล้ว +394

    Oh Tom Scott, you always manage to make us feel just a tad bit more paranoid.

  • @epicdman8139
    @epicdman8139 8 ปีที่แล้ว +737

    I am familiar with SQL injections but it doesn't work on any websites that are worth messing with

  • @someitguy2175
    @someitguy2175 9 ปีที่แล้ว +178

    Amazing that he knows I'm a camera, I'm impressed!

  • @rob-8582
    @rob-8582 10 ปีที่แล้ว +206

    This video is good but makes it sound if a website is poorly coded (so SQL injection is possible) that the database server has no security and is an open platter i.e if a malicious user attempts to run a DELETE, DROP DATABASE command they will be able to do this. If the security on the database side has been granted appropriately the user specified in the connection string of the web application will not be able to execute these commands. All users should only be granted permissions required to do the tasks they are going to use. There is no need for a web application needing to have the DROP ALL DATABASES or similar commands. Not trying to water down the risk just making it clear that the problem lies on the developer (code) and administrator side (permissions).

  • @Computerphile
    @Computerphile  10 ปีที่แล้ว +124

    Description amended to be less PHP specific - he does explain using PHP though, however little the PHP specific content >Sean

  • @DaGleese
    @DaGleese 9 ปีที่แล้ว +362

    I literally came to the video to see if he pronounced it as "sequel" or S.Q.L.
    I got my answer instantly! :D

  • @jamesisntmexican
    @jamesisntmexican 8 ปีที่แล้ว +474

    Good ol' Bobby Tables.

  • @yessopie
    @yessopie 10 ปีที่แล้ว +23

    The correct way to think about this: when you are writing code that generates SQL, you need to generate it according to the SQL syntax. When you inject a string into an SQL statement, you need to convert that string into an "SQL string literal". This is done by adding the quotation marks at the beginning and end and escaping any character that has a different meaning in an SQL string literal than in a plain string (backslashes, quotes, etc...). The SQL syntax specification shows you where these string literals are allowed in a statement. If you are putting an integer into your SQL, you need to convert it to an "SQL integer literal", which is usually done just by converting it to a string. (Not an SQL string literal-just a string.)

  • @ignasmixer
    @ignasmixer 9 ปีที่แล้ว +96

    Love the non ordinary video background. Nicely explained topic. Thank you.

  • @Truthiness231
    @Truthiness231 10 ปีที่แล้ว +98

    Oh this should be some nostalgic fun, I remember back in highschool when injecting some code into a text field and... this video is from 2013. This video is from 2013? This video is from 2013! How in holy hell could ANYONE leave such a vulnerable area of security wide open this long?

  • @TheAnig140895
    @TheAnig140895 10 ปีที่แล้ว +12

    Tom is so passionate about this stuff. It is truly amazing to watch him explain stuff.

  • @arosepsy
    @arosepsy 10 ปีที่แล้ว +15

    Really enjoy the lighting and setting of this one. Informative person as well. Seeing a large increase in quality on this channel and it's much appreciated.

  • @luketimothy
    @luketimothy 10 ปีที่แล้ว +4

    Thanks for these videos... In my experience of web programming as an amateur, Security issues have never been something I have come across all that often. It's good to learn more about them.

  • @Vulcapyro
    @Vulcapyro 10 ปีที่แล้ว +12

    "It really shouldn't work anymore, but still does" is the best description of SQL injection I've ever heard.

  • @mayur1234560
    @mayur1234560 10 ปีที่แล้ว +10

    This guy is one of the best on your channel! Get more videos from him!

  • @Zolezify
    @Zolezify 10 ปีที่แล้ว +63

    Actually I love his passion for the language and the whole subject itself. You can practically see the fire in his eyes. great work helped a bunch

  • @NerdyStarProductions
    @NerdyStarProductions 10 ปีที่แล้ว +4

    Great video. Had a web interview a few weeks ago where I needed to know what an SQL injection is, and while I did try and explain it with my limited knowledge, I learned a lot more about it from watching this video. Thanks for the upload, and I'd also love to see more of Tom.

  • @zhevox
    @zhevox 8 ปีที่แล้ว +218

    Robert'); DROP TABLE Students;--

  • @Spideyy2099
    @Spideyy2099 7 ปีที่แล้ว +7

    I love your channel name and the videos are great! It really satisfies my love of technology, but makes me wanna learn more!

  • @WhimsyHeath
    @WhimsyHeath 7 ปีที่แล้ว +90

    funnily enough, I got a SQL course ad for this video.

  • @Chrisgamee
    @Chrisgamee 10 ปีที่แล้ว +7

    This guy is the most entertaining and easy to learn from guy on computerphile! More please!

  • @GrimReaperCalls
    @GrimReaperCalls 10 ปีที่แล้ว +7

    Really liked Tom's way of explaining, I too would like to see more of him!

  • @roflbunnie6
    @roflbunnie6 10 ปีที่แล้ว +4

    Python has taught me really well with strings and escaping. Of course Ive learned more than escaping and learn strings, but I still am thankful for learning it.

  • @dospy1
    @dospy1 10 ปีที่แล้ว +1

    moar videos with this guy pls, he's amazing; he manage to output such concise information with ease

  • @CharlotteWolf
    @CharlotteWolf 10 ปีที่แล้ว

    I quite like the moody lighting, gives it a nice atmosphere, Tom was a joy to listen to aswell.

  • @reinux
    @reinux 9 ปีที่แล้ว +21

    or use an api that discourages raw text queries -- which is good practice both for security as well as for interoperability between different database software.

  • @benjamincuningham
    @benjamincuningham 10 ปีที่แล้ว +2

    Extremely well explained. Great job!

  • @edcameron
    @edcameron 2 ปีที่แล้ว +6

    Just got out of jail because i tried this 5 years ago. THANKS ALOT FOR THE WARNING TOM

  • @LukeBeacon
    @LukeBeacon 10 ปีที่แล้ว +5

    More of this guy.
    I always wonder if the other people in the restaurant (?) cant help but listen in on the riveting conversation going on.

  • @TheDonMacdonald
    @TheDonMacdonald 10 ปีที่แล้ว +1

    Great vid !!
    There is a lot of confusion about prepared statements at the moment.
    It's more than string building for onwards processing.
    The SQL statement is parsed and optimised for execution by the RDBMS.
    The variable is inserted prior to execution by the optimiser.
    In general, it's a good thing to prepare when you can. Just trust me.
    Most DBAs will have a large list of their commonly executed statements.
    It's safer and they gain some element of control over the SQL being executed.

  • @dispatchrabbi
    @dispatchrabbi 10 ปีที่แล้ว +9

    This is a really tough thing to explain to non-technical people, and this guy did a great job of it.

  • @datakaka
    @datakaka 10 ปีที่แล้ว +2

    Great explanation, more videos with Tom please.

  • @Alfakatt
    @Alfakatt 8 ปีที่แล้ว +2

    Love how this turned into a PHP promotional video with our host feeling the need to justify his usage of PHP! :D

  • @_wouter52
    @_wouter52 10 ปีที่แล้ว

    Yay Tom is back! Great video as always!

  • @Firelynx87
    @Firelynx87 10 ปีที่แล้ว

    Great video! i really enjoy listening to Tom!

  • @gdwnet
    @gdwnet 10 ปีที่แล้ว +63

    as hacks go there are worse ones *heavy sigh* - There speaks someone who has had to deal with them! I know that sigh all too well.

  • @ZeeCoder
    @ZeeCoder 10 ปีที่แล้ว +15

    I'd love to see more about security from Tom, he just does a wonderful job explaining things.

  • @aryamanmajumdar7411
    @aryamanmajumdar7411 6 ปีที่แล้ว

    The lighting in this video is really good.

  • @Bukowski208
    @Bukowski208 10 ปีที่แล้ว

    I seriously love this guys enthusiasm

  • @mattdathew2794
    @mattdathew2794 9 ปีที่แล้ว

    thank you so much for uploading this

  • @joshstead6078
    @joshstead6078 8 ปีที่แล้ว +12

    I don't understand who dislikes these videos, no matter what there are people who will dislike every video on youtube

  • @phatcartoon
    @phatcartoon 10 ปีที่แล้ว

    Yes, another TH-cam user suggested the same. I checked it out recently and it's really nice. Thanks.

  • @Mukhambiel
    @Mukhambiel ปีที่แล้ว +2

    Going to show this video to our apprentice. He is going to learn SQL in school soon. Best wishes from germany!

  • @SignalsEverywhere
    @SignalsEverywhere 10 ปีที่แล้ว +5

    Thanks for the video, I have a much much better understanding of how this works now.

  • @QuotePilgrim
    @QuotePilgrim 10 ปีที่แล้ว

    Thank you. The analogy in the beginning is genius.

  • @WaynesvilleRC
    @WaynesvilleRC 10 ปีที่แล้ว +6

    This guy is phenomenal; more of him please!

  • @outercores
    @outercores 3 ปีที่แล้ว +11

    Why does every comment on this video have no reply’s.

  • @tomlxyz
    @tomlxyz 8 ปีที่แล้ว +172

    PHP is only fun at the beginning.

  • @yonas019
    @yonas019 10 ปีที่แล้ว +2

    I really like this guy, very enthusiastic, reminds me of my lecturer for databases

  • @jeba1215
    @jeba1215 10 ปีที่แล้ว

    Loved the video, great work!

  • @lemans1234567890
    @lemans1234567890 8 ปีที่แล้ว +33

    reminds me of xkcd 327
    where a mom names her child
    "Robert'); DROP Table Students;"

  • @sjmww1235
    @sjmww1235 9 ปีที่แล้ว +245

    bobby drop table students anyone

  • @Quimbyrbg
    @Quimbyrbg 10 ปีที่แล้ว +3

    I liked this bloke, he's very passionate about the subject which helps him avoid the boredom that can creep in when listening to a talk on programming.
    Excellent topic too; I'm neck deep in learning PHP at the moment, so I appreciate the heads up about the security risks.

  • @easementh
    @easementh 10 ปีที่แล้ว

    Love the colorgrading on this one.

  • @dunx125
    @dunx125 10 ปีที่แล้ว

    I really like this guy, he speaks with passion and intelligence

  • @icemd24
    @icemd24 8 ปีที่แล้ว +2

    Gonna change all my code to prepared statements right now. Thank you!

  • @DoABarrelRol1l
    @DoABarrelRol1l 4 ปีที่แล้ว +6

    Me and my friend always joked about naming a kid "DropTable" in our IS SQL intro course.
    But I guess now I realized it should be something along the lines of: Frank";Droptable

  • @bobsmith-ov3kn
    @bobsmith-ov3kn 8 ปีที่แล้ว +3

    I am a professional database expert and I know that the language I am a specialist in, DB2, that 2 two things...
    1 - This has been a well known security issue for many years, and any DBM or security expert would be fully aware of these specific issues
    2 - Even if for whatever reason someone was able to input commands they shouldn't be allowed to input..
    2A - There are restrictions on who is allowed to perform what operations and on which table(s)(and even which individual records). Even if someone was able to hack the command stream and input malicious commands, any reasonable database will have very easy to implement restrictions that make sure nobody could ever do or view anything they aren't supposed to, and CERTAINLY not do any sort of potentially seriously disruptful actions like deleting data or dropping all databases, lol. Just a very simple restriction on any potentially powerful commands to only the database admins would nullify any of these potential threats.
    2B - Even IF something catastrophic was performed like "drop all databases" there are methods of restoring the database to previous states, not just by having BACKUPS saved somewhere, but by diagnostic logs that can be used to roll the environment back in time. I suppose it's possible to imagine a malicous attack that was smart enough and all-inclusive enough to delete all backups and relevant data needed to restore the environment, but just keeping a seperate copy of backups on a completely separate system would alleviate that possibility.

  • @phatcartoon
    @phatcartoon 10 ปีที่แล้ว

    Thanks, Sai Vineet, I appreciate the help. :)

  • @IamAmitKT
    @IamAmitKT 7 ปีที่แล้ว

    thanks tom, awesome explanation

  • @abaltazar8
    @abaltazar8 10 ปีที่แล้ว

    He clearly said it wasn't just a PHP specific problem. Great video!

  • @nickt906
    @nickt906 8 ปีที่แล้ว +2

    This guy is great at teaching.

  • @007bistromath
    @007bistromath 10 ปีที่แล้ว

    There's some missing context for this video that would be really interesting. Even though I've used computers for most of my life, I never actually knew what a relational database was or how it works until a few years ago when I asked my instructor about them while taking a class that was only tangentially related to them. I think it's a very interesting topic that more people should know about, because it's pretty much at the conceptual root of everything a modern computer does. Do that video!

  • @ashwith
    @ashwith 10 ปีที่แล้ว +3

    "...someone who uses Windows." The expression at 8:31 is priceless! :D

  • @0096luke
    @0096luke 8 ปีที่แล้ว +183

    There's a fix for this. It's called not using PHP

  • @Yodamanjaro
    @Yodamanjaro 9 ปีที่แล้ว +27

    "Prepared statements" - this is also called parameterized queries, right?

  • @EternalATomik
    @EternalATomik 10 ปีที่แล้ว +2

    You're right. For me, using PDO implicitly means "use it properly" i.e alway prepare the statements.
    Thanks for pointing out.

  • @neongreencoding878
    @neongreencoding878 8 ปีที่แล้ว +2

    Great info! SQL injection is a classic security flaw

  • @josephthedafty
    @josephthedafty 10 ปีที่แล้ว +1

    I freaking love Tom Scott, everyone should.

  • @dementeddr
    @dementeddr 10 ปีที่แล้ว

    I love that he writes things on old continuous-feed paper.

  • @capitaopacoca8454
    @capitaopacoca8454 3 ปีที่แล้ว +12

    This is one of those videos that have few comments but all from 6 years ago. And no replies, for some reason.

  • @apark633
    @apark633 10 ปีที่แล้ว

    I am really liking this guy. more please!

  • @ok_engineer
    @ok_engineer 10 ปีที่แล้ว

    This video is going to be the response to so many stackoverflow questions.

  • @ArnoldsKtm
    @ArnoldsKtm 8 ปีที่แล้ว +6

    In php and all other modern programming languages the classes and functions to communicate with database are made so well I don't really have to worry about escaping the value. (pdo for example. no idea why would anyone still use mysql or mysqli even)

  • @youluvana
    @youluvana 10 ปีที่แล้ว

    On some websites it's intended for you to not be able to save some images or get a popup or have a chance to be redirected instead, when you click on a link. On some lower end websites you can go around all that by looking at the source code. Often there are links in the code that reveal the url of the images or you can figure out how to change the text of the link address (right click -> copy link address) to open it without extra popups.

  • @farissaadat4437
    @farissaadat4437 8 ปีที่แล้ว +8

    The recent ‘Super Mario World (1990)’ speedrun uses code injection in very fascinating ways that I don't understand. There is even a way to play PONG on the game using in-game sprites and complicated code injection.

  • @theNumberOne
    @theNumberOne 10 ปีที่แล้ว

    Of course, in some cases stored procedures/functions can be better, but that often ties you to a particular database/infrastructure stack.
    It's a trade-off in your overall architecture. Lock yourself in with Stored Procedures, or be a little more flexible (useful in products you want to sell, especially to large businesses/enterprises).

  • @Kwpolska
    @Kwpolska 10 ปีที่แล้ว

    And “proper handling” means query parameterization (prepared statements) - which was actually described in this video…
    But you can’t do it with PHP’s mysql_*, hence the “easier in PHP”.

  • @jeebersjumpincryst
    @jeebersjumpincryst 10 ปีที่แล้ว

    this technique was used by the characters in jk rowlings "a casual vacancy" to great effect!

  • @IceMetalPunk
    @IceMetalPunk 10 ปีที่แล้ว +2

    I'd like to hear more detail about other methods of injections. I've always been under the impression that properly escaping your inputs was enough to be safe...

  • @crisunjackalD
    @crisunjackalD 10 ปีที่แล้ว

    SQL was designed under the name SEQUEL (Structured English QUEry Language), but Sequel just so happened to be a trademarked name of a UK company called Hawker Siddeley Aircraft so it had to be changed.
    From that, we can assume both ways of saying are correct since Sequel was the name originally intended but "Es-Kyoo-El" is the amended one.

  • @boenrobot
    @boenrobot 10 ปีที่แล้ว +1

    The real question in that case is what constitutes "its own program".
    You can run JavaScript via a browser, but the JavaScript code doesn't have to call browser specific (e.g. DOM) methods. You could (at least theoretically) write a JS code that works equally with a browser or with a server system (e.g. Node.js) or a build system (e.g. Visual Studio's ".w32" files). The language is all the same in all those environments. Only the non-core APIs of the runtime differ.

  • @Darksaber2213
    @Darksaber2213 9 ปีที่แล้ว +45

    Input: Tom\"; DROP ALL DATABASES;
    Escaped quotes: Tom\\"; DROP ALL DATABASES;
    Computer ignores escape, drops databases.

  • @ghelyar
    @ghelyar 10 ปีที่แล้ว

    SEQUEL was different to SQL and used for ~12 years before the 1986 ANSI spec for SQL, in which the correct pronunciation is specified.
    It may be that the pronunciation was only even defined to avoid copyright infringement, but since it's in the spec the pronunciation is just as well defined as the "SELECT" keyword, for example. Just as you can't swap "choose" or "find" or "get" for "select" in a query, there is a pronunciation that is defined as correct and other pronunciations are non-spec.

  • @Niosus
    @Niosus 10 ปีที่แล้ว

    Luckily that's where frameworks come in. They clean up a lot of the mess by abstracting it away. All security features are handled in a consistent manner, all DB access is thoroughly cleaned, etc. Things are getting better though. PHP 5.5 just came out a few months ago with a bunch of really nice features. They are stepping up their game again.

  • @murialvoid85
    @murialvoid85 ปีที่แล้ว +5

    Sequel? I prefer squirrel injection. Sounds a lot cooler.

  • @Vulcapyro
    @Vulcapyro 10 ปีที่แล้ว +1

    These sorts of practices make me cringe inside. I salute you for your continued efforts.

  • @SergeMatveenko
    @SergeMatveenko 9 ปีที่แล้ว +175

    The one thing to mention: do not ever ever ever use PHP.

  • @IbrahimDayax
    @IbrahimDayax 2 ปีที่แล้ว +3

    Who's here in 2022 when PHP has largely been replaced by Server side JS?