Latest / Talk Python To Me / #168: 10 Python security holes and how to plug them
Transcript
- 0:00Do you write Python software that uses a network, opens files, or even accepts user input?
- 0:04Of course you do. That's what almost all software does. But these actions can let bad actors exploit
- 0:10mistakes and oversights we've made in our code that will allow them to compromise our systems.
- 0:14Python is safer than some languages, but there are plenty of issues to be careful of.
- 0:20That's why Anthony Shaw and Anthony Lingsworth are here to discuss Python security. This is
- 0:25Talk Python To Me, episode 168, recorded June 28th, 2018.
- 0:30Welcome to Talk Python To Me, a weekly podcast on Python, the language, the libraries, the ecosystem,
- 0:49and the personalities. This is your host, Michael Kennedy. Follow me on Twitter, where I'm at
- 0:54M. Kennedy. Keep up with the show and listen to past episodes at talkpython.fm, and follow the
- 0:59show on Twitter via at Talk Python. Anthony Shaw, Anthony Lingsworth, welcome to Talk Python.
- 1:05Hey, Michael. It's great to be back.
- 1:06Hey, Michael. Good to be my first time.
- 1:08Yeah, it's good to have you here. So we have a bit of a name conflict here. So Anthony Shaw's
- 1:14aunt, and Anthony Lingsworth is Anthony for the rest of the show. Hopefully that works for you all out
- 1:20there listening. And we're going to cover something that I think is often overlooked in Python,
- 1:25the whole security side of things, right? I mean, I feel like, well, there's no buffer overflow issues and
- 1:31things like that. So we must be just totally fine, right?
- 1:34No. If only that was the case. If only that was the case.
- 1:38Exactly. There's actually a ton of interesting vulnerabilities and things that are pretty common,
- 1:44I suspect, still in some circumstances that we're going to go through. But before we do,
- 1:49I'd like to hear your guys' story really quick. Anthony, how'd you get into programming in Python?
- 1:54I mainly got into programming Python through networking programming. One of my responsibilities
- 1:58here is interacting with a lot of Cisco and Juniper and other hardware, and Python is what they use.
- 2:04Right on. And Ant, you talked about getting started on Lib Cloud last time, right?
- 2:09Yeah, that was me. I was in Seattle and I got sick, and I was stuck in a hotel for the weekend,
- 2:14so I learned Python. I never looked back since then.
- 2:17Most people say, I got really sick, so I stayed in bed. Other people like you are like,
- 2:23oh, I learned a new programming language because I wasn't feeling well.
- 2:26I had nowhere to go.
- 2:27That's awesome. I totally get it. Let's start first talking about just security in general.
- 2:33Anthony, how do you think we should start thinking about security as software developers
- 2:38and people that run servers and stuff like that?
- 2:41What I tell people is when they start thinking about security, they have to invert how they think.
- 2:46Developers are very good at thinking, how do I build something quickly and that's designed well
- 2:52and that keeps my customers happy? Security is not like that. Security is how do I break something?
- 2:59How do I compromise something? How do I bend the rules?
- 3:03So you almost need a tester style thinking.
- 3:06So you look at what you've built and think, well, how do I make it do things it's not meant to do?
- 3:12I find there's just so much creativity in that.
- 3:15It's really hard to think about.
- 3:17And unless you see some examples of it in action, it's almost hard to conceptualize some of these things.
- 3:23Like I remember the first time I saw the actual code for a buffer overflow exploit.
- 3:30Here's how you overflow the buffer.
- 3:32Here's how you get it to execute those bits that you've dropped in.
- 3:34I'm just like, that is just, that's a different way of thinking, let's say.
- 3:38Yeah. I mean, as you say, some of the people who do this, they're really smart and they get really technical.
- 3:42But I also want to stress that a lot of this stuff is not rocket science.
- 3:47A lot of this is looking at what you do and just trying to keep that mindset.
- 3:52Yeah, I think that's a really good point.
- 3:54It's easy to get overwhelmed thinking, you know, this stuff is super advanced.
- 3:59But actually, 99% of the time, it's like, did you change the password from the default admin password on the database and stuff like that, right?
- 4:07That's where people usually start whacking on it, right?
- 4:10Exactly. Exactly.
- 4:11I mean, most people make a big deal about the super, super complex hacks out there.
- 4:18But if you talk to people like Kevin Mitnick and so on, a lot of the stuff they were doing is social engineering, which is just simple, as you say, password stuff or basic configurations.
- 4:26A lot of the times with security, if you get the basics right, the simple stuff right, the attackers will just go elsewhere because they're looking for the easiest exploit.
- 4:35Yeah, they're just rattling the door to see if it's open, right?
- 4:38Exactly.
- 4:38Yeah. And how about you? What are your thoughts on the sort of security thinking versus developer thinking?
- 4:43Yeah, I mean, I guess that probably the difference between Anthony and I, which is, I guess, confusing because we have the same name.
- 4:50Although we sound quite different, hopefully that'll be more obvious to the viewer if we have different accents.
- 4:55Is that Anthony spends a lot of his time, I guess, looking at things from the security angle, whereas most of my time has been spent from a development angle.
- 5:04And I've been curious about security, I guess, for a long time.
- 5:10My first job was doing sort of system support for servers and dealing with hacked servers was a pretty common occurrence.
- 5:18And I guess learning how people got into the systems, what they did once they got there.
- 5:24And it was kind of shown in the movies, like there's, you know, this person in a dark room with like a green screen and it's all like super technical.
- 5:31And they've always got a hoodie on and they're always playing some sort of like metal music in the background.
- 5:36But actually, when you're more familiar with, I guess, how systems get compromised, it's just people running scripts that just pinging server after server after server looking for known vulnerabilities.
- 5:47And if people haven't configured stuff properly or they've left bad usernames, bad passwords, then they just poke a hole in it and then have their fun, basically.
- 5:55I mean, that takes up a lot of people's time and a lot of the security challenges.
- 5:59So my kind of curiosity has been, how can I write code in a way that I'm not doing things which the attackers are going to be excited about?
- 6:07Like, what mistakes could I be making in my code that they're going to be like, ah, fool, he left this particular vulnerability.
- 6:14He didn't read this obscure documentation page.
- 6:17And it does come across like that to people.
- 6:19Like, it seems hard, which is, I guess, why I put the article together to point out some of the more obvious ones.
- 6:25And they're not that complicated.
- 6:28Yeah, that's a really good point.
- 6:29Most of it isn't that complicated.
- 6:31It's just knowing that what is out there.
- 6:33You know, me personally, I don't think about it the way that you do, Anthony, in terms of like, how can I break things?
- 6:40I think of it more as like a super paranoid, forgetful person, which I'm fairly forgetful in general life as it is, but not paranoid.
- 6:49But I'm always thinking like, oh my gosh, what have I forgotten?
- 6:52What is the thing that is just wide open that I forgot?
- 6:56Did I forget to turn on the firewall that blocks the database port publicly?
- 7:00Did I forget to, you know, validate this one thing?
- 7:04I think, you know, a while ago there was one admin page on my site that didn't check whether this person was an admin or not.
- 7:12And if you could come up with this random URL, you could have found it.
- 7:15I'm like, oh my gosh, I can't believe I forgot that.
- 7:17So that's sort of my feeling is like a constant uneasiness.
- 7:20I know there's thousands of people attacking this site constantly, but throughout, you know, they sort of continuously, I guess.
- 7:27And it's like, well, when will they find the thing I forgot?
- 7:30It's an ego driven security as it's called.
- 7:32But that's cool.
- 7:33That's cool.
- 7:34If it makes you more secure, then go for it, please.
- 7:37Yeah, I think it probably does.
- 7:38I mean, I'm always double checking stuff and I think it does really, really help.
- 7:42So there's a lot of these things you can address.
- 7:44And I think it's probably time to go through this list.
- 7:48So when Brian and I spoke about this on Python Bytes just yesterday, actually it came out today, but we recorded it yesterday.
- 7:56And he was like, well, the ones that were obvious, they're not even here.
- 8:00I mean, some of them are, but some of them are like Python, you know, there's the eval, eval user input.
- 8:05Like, don't do that, right?
- 8:06So I think you have a really interesting list that you put together here.
- 8:10Some of which will be people like, oh yeah, I've heard of that.
- 8:13Others like, really?
- 8:14I didn't know that was a problem.
- 8:16And then they'll frantically pause the podcast and they'll run away and go look at their systems.
- 8:20So you want to take us through the topics and we can sort of spend a little time on each one?
- 8:25Yeah, sure.
- 8:26So I guess I left out some really obvious ones.
- 8:30I mean, eval is probably the worst one.
- 8:32Like you wouldn't just accept input from the internet and pass it through eval, I'd hope.
- 8:36You say that.
- 8:38You say that.
- 8:39I'm sure it's happening all over the place.
- 8:40It's easier to sort that way when you just pass the lambda on the command line.
- 8:44Yeah, the PHP equivalent is the reason that you're getting all those entries in your logs looking for wp-admin.
- 8:51Anyway, so I guess the number one is
- 8:56input injection, which is just common across languages, across frameworks.
- 9:00It's nothing specific to Python.
- 9:05It's basically noticing that you're creating SQL queries and you're taking a parameter and you're injecting it into a string literal.
- 9:13And what they can do is they can escape the quotes you put around the argument or the filter or whatever it was.
- 9:20And they can basically run arbitrary SQL queries on your table.
- 9:24So SQL injection, I think, is pretty well known.
- 9:26It's pretty easy to avoid.
- 9:28There are libraries for doing sensible escaping.
- 9:32There are libraries for basically parameterizing your SQL queries, which is really the way it should be done.
- 9:38But time and time again, that's probably seen as one of the most common things in web applications.
- 9:43And it's just one of those things that won't die, right?
- 9:45Like it's just there's still popular blog posts that have examples of SQL injection attacks all over.
- 9:52And people just read until they get it to work.
- 9:54They don't go to the comments and say, don't do this.
- 9:55Don't do this.
- 9:56Yeah, because it's like when you learn SQL or if you learn how to create SQL queries, it doesn't like there are so many tutorials out there that actually show you the wrong way to do it.
- 10:07So this is why it keeps happening.
- 10:08Yeah, this is why it keeps happening because people are like, this is how you write a SQL query.
- 10:12And then, OK, you want to filter by a particular user or a particular ID or whatever.
- 10:17Here's how you write a where clause.
- 10:18And, oh, let's take that as a parameter.
- 10:20Yeah, yeah, because it's simpler, right?
- 10:22People are learning the wrong way to do it every day.
- 10:24And we're having to unteach them that kind of practice.
- 10:26Luckily, there's a cartoon to remind us.
- 10:29SKCD.
- 10:31Yeah.
- 10:32Yeah.
- 10:32Are you familiar with little Bobby tables?
- 10:34Yeah, absolutely.
- 10:34And I've seen the picture.
- 10:36There's a photograph as well as of a car where someone's put on their license plate.
- 10:40They've actually tried to put SQL injection on their license plate.
- 10:43Get all those license plate readers.
- 10:45That was brilliant.
- 10:47Yeah.
- 10:47So the license plate readers would crash.
- 10:49That's the intention anyway.
- 10:50And then I guess the other version of that is command injection,
- 10:53where you have a script which needs to call a local process on the machine,
- 10:58like an encoder or a specific executable or something like that.
- 11:03I mean, Python has a few ways of doing that in the standard library.
- 11:07There's subprocess.
- 11:08There's popen.
- 11:10There's in the OS module.
- 11:14There's another way.
- 11:15And there's many ways that similar to SQL injection, you can take an argument from somewhere and people can escape that argument and use it to run arbitrary commands on the server.
- 11:25So that's command injection or shell injection is called.
- 11:28That sounds a little bit bad.
- 11:29And that one actually was surprising to me.
- 11:31I wasn't super familiar with that.
- 11:32But if you're going to do popen and pass a string as the command to run, you could easily do dollar sign, dollar sign, next command or semicolon or whatever the separator your shell uses.
- 11:43Because you can issue commands that have multiple operations one after another in a single line, right?
- 11:50Yeah, so the most common example is somebody writing a, basically calling an executable and then giving it an argument.
- 11:56Let's say you want to create a thumbnail or encode an MP4 or something.
- 12:00Then you'd call a local process which does the encoding and you'd give it an argument which is the name of the file you want to encode.
- 12:07So that's straightforward, except that if instead of a file name, they close the quote and then they do a semicolon or an amphosand, amphosand, and then they can put in any other command they like.
- 12:18How could that be wrong?
- 12:19How could it go wrong?
- 12:20And it's funny because I shared this post and I saw some of the discussion forums, people arguing back saying, oh no, that's not a problem.
- 12:27Why, how could that possibly be a vulnerability?
- 12:29And even if you kind of like make it really obvious, people don't look at it like an attacker would look at it.
- 12:35They look at it like, oh, who would misuse that?
- 12:38How could that happen?
- 12:38Exactly.
- 12:39That's flexibility.
- 12:39It's good.
- 12:40It means good things.
- 12:41Yeah, and I think it's really interesting to have these two contrasts, not just SQL injection.
- 12:45And I was going to add too that there's, in addition to the escaping and stuff that Ant is talking about, there's additional things you can do here to protect yourself.
- 12:55So, for example, if you can run as the least privileged account you can.
- 13:00So, don't run as root or administrator, for example.
- 13:03So, if you do happen to have a bug in your application where someone can do command injection, you're going to minimize what it can do.
- 13:11So, there's multiple levels of ways you can protect yourself here as well.
- 13:15I think that's a really good point for a lot of this stuff, right?
- 13:18It's sort of security as layers, not the one big silver bullet, right?
- 13:23Yes, very much so.
- 13:24Yeah.
- 13:24The Python way around that is, well, first of all, avoid doing this if possible.
- 13:29I mean, there's many other ways of doing spawning processes and doing them correctly.
- 13:35But if you have to, if you need to escape the input, then use the shlex module, which is built into the standard library, and it has a utility function for escaping shell commands.
- 13:45Yeah.
- 13:45What I really like about your article, which I don't know if we've said it explicitly, it's 10 common security gotchas in Python and how to avoid them.
- 13:52And we'll put that in the show notes, of course.
- 13:54And for many of these, you have these, oh, did you know there's this other module you can just use that fixes this problem?
- 14:02So, that's, it's really nice that you have sort of some way to deal with this, not just like to put paranoia into people.
- 14:08This episode of Talk Python To Me is brought to you by Test and Code and the Python Bytes podcasts.
- 14:15If you love podcasts in Python, and you're listening to mine, so you must, right?
- 14:19Well, don't miss out on these two other great podcasts.
- 14:23If you're in a hurry and just want the headlines, get the rapid fire Python Bytes podcast that I co-host with Brian Okken over at pythonbytes.fm.
- 14:30Brian also hosts his own show, Test and Code, at testandcode.com, where he explores topics like test-driven development, continuous integration, code coverage, and pytest, of course, as well as agile development, mentoring, public speaking, and lots more in an opinionated mix of interviews and solo episodes.
- 14:47Keep up with the latest news and become a better developer by listening to Python Bytes and Test and Code.
- 14:52The next group of attacks are all related to deserializing or serializing data.
- 15:00And this is a pretty common vector where, let's say you're writing an application that reads configuration from somewhere.
- 15:08And that could be in YAML or JSON or XML, or it's receiving a message from somewhere, or you're basically using it as a way of communicating between different processes.
- 15:19There's different ways you can do that in Python.
- 15:20You could use XML, you could use JSON, you could use YAML, or you could use pickle files.
- 15:25So all four of those have massive gaping holes in them, which you could drive a bus through.
- 15:30So I kind of explained how you could abuse each one of them.
- 15:35I guess people might assume that XML is the safest.
- 15:39Yeah, it seems sort of old school, but XML can get complicated on the edges, you know, like weird namespaces and referring back to itself and other sorts of bizarre things that nobody ever does.
- 15:50Yeah.
- 15:51So the one I kind of explained was the billion, what's called the billion laughs exploit.
- 15:58It's funny.
- 15:58And it's, you know, look, if you're going to get like taken down, at least it should be a funny sort of hack, right?
- 16:04Come on.
- 16:04Got to appreciate it.
- 16:06So this is if you wanted to, if you received an XML payload from somewhere and an attacker has access to put their own XML in there, then basically XML has this thing called entity expansion.
- 16:19I guess the most similar way to think of it is a zip file.
- 16:24And what you could do, imagine you created a text file and it just contained spaces, lots and lots and lots of spaces, like let's say a million spaces.
- 16:34Now, if you compress that into zip, it compresses down to nothing because the way compression algorithms work.
- 16:40But when you extract it, it's like, oh, it's one character.
- 16:43Yeah, exactly.
- 16:44So it can condense down to pretty much nothing.
- 16:47Let's say you compress that into a zip file and then you made 10 of those, you copied it and you pasted it 10 times.
- 16:53And then you copied those 10 zip files and then added them to a zip file and then did that again and again and again.
- 16:58Now, what happens is if you extracted that zip file, it will go and sort of recursively extract the zips.
- 17:06And it can go from something like a one kilobyte zip file to gigabytes or potentially even worse.
- 17:13So the XML version, that's called a zip bomb.
- 17:17The XML version is a similar idea where in XML, you can have basically you can sort of reference things.
- 17:24And so in entity expansion, it can kind of reference something which references something.
- 17:29So it's similar to the zip bomb where you kind of create this tree and then it basically exponentially expands.
- 17:35So when you're trying to deserialize this special message in XML, it can take up gigabytes of RAM.
- 17:40So that is basically the exploit is that it just takes your server offline.
- 17:44And here it is fitting on one screen with word wrapping.
- 17:48Yeah.
- 17:48And it's like eight lines of XML.
- 17:50Yeah.
- 17:50It sort of teaches you that exponential stuff and like factorial type things.
- 17:54They get big fast.
- 17:55Yeah, exactly.
- 17:56So the XML one's pretty bad.
- 17:57There's some other pretty well-known ones with XML.
- 18:01So if you actually look at the standard library documentation for Python, and this is how I came across this.
- 18:06It does say in a big red box that there are known security challenges with XML.
- 18:10And you shouldn't deserialize arbitrary XML documents.
- 18:15Unless you do like soap web services, then what could go wrong?
- 18:18Exactly.
- 18:18So I guess the alternative is you can use a package called defused XML, which is a third party package.
- 18:26But it was actually written by Christian Himes, who's one of the core developers on Python.
- 18:30He's a security expert at Red Hat.
- 18:32So he kind of put together, I guess, a safer version of the XML standard library module.
- 18:37And it's a drop-in replacement.
- 18:38That's awesome.
- 18:39Does it just like not support these self-referential things and some of the other dangerous bits?
- 18:44It's like a limited XML?
- 18:46Stuff that it would be really unlikely you'd have to use anyway.
- 18:48Yeah.
- 18:48Anthony, as a network person, how do you think about these issues?
- 18:52Like, do you scan for these sorts of things?
- 18:55They keep me up at night?
- 18:57Look, there's a whole class of exploits with XML.
- 19:00There's exploits where you can embed schema information into there and do weird stuff with schema.
- 19:06I generally think that if you are going to handle XML, you generally whitelist what you accept.
- 19:12You define your own schema and say, if it doesn't match that schema first, I'm not going to parse it.
- 19:18I'm only going to parse minimal pieces of it.
- 19:20There's also, you can use what's called an XML external entity attack where you reference to a schema or an inclusion from somewhere else.
- 19:29And that somewhere else is relative to your server.
- 19:32So by getting you to upload a piece of XML, they can try to get your server to load something from your internal network and potentially display it back to the attacker.
- 19:41So XML can be used as a reconnaissance tool as well.
- 19:45It's not to say that XML is bad, but you have to follow the advice that is giving you now to use it properly and use it well.
- 19:52Yeah.
- 19:52Stay away from the dangerous bits unless you really need them.
- 19:55And it's rare these days, I think, that you need them.
- 19:58It seems like, you know, early 2000s, XML was supposed to be the answer to all the questions that involved any form of connectivity, right?
- 20:06Like we have all these fancy things with XML.
- 20:10We had all the hype around soap services.
- 20:14You have XSLT going wild being like the way to generate all sorts of stuff.
- 20:18And I feel like we've kind of moved on, but there's still plenty of XML processing around.
- 20:23XML has its place.
- 20:24It's a great metadata standard with a lot of bells and whistles attached.
- 20:29The problem is 90% of people don't need those bells and whistles, but it is what it is.
- 20:34Yeah, I guess I should knock XML.
- 20:36I mean, it is the main bit of traffic on some of my websites with the podcast RSS feed.
- 20:41That thing gets hammered.
- 20:43So I guess I should love XML, but that's the simple version.
- 20:46So another thing that you put in here that I thought was really interesting is people relying on assert to actually do runtime control.
- 20:58This one is pretty obscure, actually, and not that well known.
- 21:02And that is that there's effectively an optimizer.
- 21:06Well, there's a series of optimizations you can do in Python.
- 21:09And you can run Python in optimized mode, which does.
- 21:13I mean, if you look at the history of Python, basically what it tries to do is traverse the syntax tree and kind of make assumptions about how it should be executed.
- 21:22So that if you're doing it in production, it almost removes commands out of your code and skips them or looks at them differently.
- 21:30So this is things like constant crushing constants together, loop on folding.
- 21:35And there's a whole bunch of optimizations.
- 21:37One of those optimizations is to ignore assert statements because really they should be used for debugging.
- 21:43So if you're used to any other programming languages, so sometimes you can basically create like a debug assembly or a runtime assembly where they have debug symbols in one.
- 21:53And it's designed for production and another Python basically has that, but it's by adding an extra flag when you execute the Python file.
- 22:01So it skips certain commands depending on whether it's running an optimized mode or not.
- 22:06So if you use an assert statement to do things like checking that the password is right or checking that the user has the right amount of privileges or whatever, then basically it all runs fine in unit tests and integration tests because you never use optimized mode because you always want the assert statements.
- 22:24And then when you actually deploy it to production, someone says, oh, Python is quicker, like 10%, 20% quicker if we run it in optimized mode.
- 22:32They switch the flag on and all of your security catches and everything like that just disappear.
- 22:39Yeah.
- 22:39I mean, they could be totally sort of silent as well, right?
- 22:42If you were doing like containers, you could depend upon a Docker container and the base image could change in a way.
- 22:47They're like, yeah, we made it a little bit better.
- 22:48It's faster.
- 22:49Oh, great.
- 22:50I love faster.
- 22:51Let's do that.
- 22:51Wait, why can people be on this page again?
- 22:54This is terrible.
- 22:55Yeah, that's pretty scary.
- 22:56So the fix here is don't do that.
- 22:59It's like, doctor, it hurts when I raise my arm.
- 23:01Well, don't do that.
- 23:02Keep your arm down, basically.
- 23:04Like just use a if, you know, not admin, raise exception, permission error, or something like that, right?
- 23:10Yeah, typed exceptions.
- 23:11Yeah, exactly.
- 23:13Which is how it's supposed to be done anyway.
- 23:14Okay.
- 23:15Another one that's pretty interesting is not as common for the web because of the latency, but it's pretty interesting.
- 23:24Timing attacks.
- 23:24Tell us about those.
- 23:25Anthony, do you want to explain this one?
- 23:26So timing attacks, there's two really, two little buckets this falls into.
- 23:31One, if you're doing any form of security sensitive operation, like let's say, for example, you're checking a password is valid.
- 23:38The way a lot of code works is it might say, well, it will say, well, if the password is too short or too long, then don't bother checking the password, just fail out straight away.
- 23:49And an attacker can use this to work out.
- 23:51Well, what is a valid password to decrease the passwords it needs to check?
- 23:55I see.
- 23:56So if you're going to do like a dictionary attack and you realize, well, it's zero milliseconds for anything under seven characters, we'll start there.
- 24:04Exactly.
- 24:05Exactly.
- 24:05Exactly.
- 24:05Exactly.
- 24:05Exactly.
- 24:06So if you're doing particularly some cryptography, the idea is it's meant to have an approximately equal amount of time, irrespective of what input you give it.
- 24:16There's also another set of attacks around time of check, time of update.
- 24:20So if you're doing certain checks and then doing stuff that depends on those checks, make sure those checks are around the same time in code as they are that you're doing it.
- 24:32Because otherwise there is a gap between when you do that check, that security check, for example, and when you do that task or that operation that requires a result of that check, that can be exploited too.
- 24:43So there's kind of two areas here.
- 24:45That's pretty interesting.
- 24:46I hadn't really thought of the sort of information leakage around that.
- 24:51So I want to give a shout out to a Python package that I think just is really amazing.
- 24:55He talked about password checking and stuff called Passlib.
- 24:58Are either of you familiar with Passlib?
- 25:00No, I'm not.
- 25:00Yeah.
- 25:01So Passlib is really awesome because, you know, one of the bits of advice is to get a random salt and mix that in when you generate the password, right?
- 25:11Or when you hash and store the password.
- 25:13And then also make this computationally expensive to guess.
- 25:15So Passlib is like, there's like one function of like encrypt and one function like verify.
- 25:21And it will, you give it a plain text password and it will take that and it will fold it like 150,000 times with custom salt for that particular user.
- 25:31And then it can check it.
- 25:32But it takes like, you know, 0.2 seconds to determine whether it's right or wrong.
- 25:37It doesn't even just check the length.
- 25:39So it's a really nice way to sort of add a few more layers there.
- 25:42Yeah.
- 25:43So the other one I recommended was part of the standard library, but it was only added in 3.5.
- 25:48And that's a module called Secrets.
- 25:50And it has a method called Compare Digest where you can basically give it two values and it says, are they equal or not?
- 25:57So that's where you would say if the actual password equals equals the password the user entered.
- 26:03And statements like that are the type that can be left open to timing attacks.
- 26:07So Compare Digest basically makes it impossible to do that.
- 26:10Yeah, that's cool.
- 26:11So one piece of advice that I have around if you're doing anything like password management or any cryptography stuff, generally speaking, don't write it yourself.
- 26:20Use a library off the shelf that other people have used and vouched for because it's very easy to, if you do crypto wrong, it can be worse than having no crypto.
- 26:32Because for all the false assurances it will give you.
- 26:36Right.
- 26:36You think it's fine, but, you know, I don't understand.
- 26:38I base 64 encoded the password.
- 26:40They can't get that back out of the database.
- 26:42Come on.
- 26:43Of course not.
- 26:44Of course.
- 26:45Yeah.
- 26:46That pass level one that I talked about, by the way, they have like a list of all the support algorithms and the ones they recommend.
- 26:51And they're constantly checking them.
- 26:52So I think it's bcrypt and SHA-512 right now that they're using.
- 26:55It'll be bcrypt and probably the PBK-DF2.
- 26:58They're the two ones that people tend to recommend these days.
- 27:00So, yes.
- 27:01Yeah, nice.
- 27:02So one that I think is kind of scary because it's an exact opposite or in contrast to what makes Python awesome, right?
- 27:12Like, you know, import anti-gravity.
- 27:15It sort of captures the joy of Python where you just import anything and it's just there and it just works.
- 27:21But it's possible that you could have installed something bad, right?
- 27:26Yeah, absolutely.
- 27:27And I think not just install something bad, but the way that Python imports work.
- 27:33They're really flexible.
- 27:33They're really flexible.
- 27:34You can override all sorts of things, including system like almost language keywords.
- 27:40So you can override the print function or the assert statement or you can do all sorts of crazy things in imports.
- 27:47So I think one of the challenges with Python is that if you have a package somewhere in your site packages, basically an import path that does something malicious, it can be very difficult to detect that.
- 27:59And because your site packages is almost like a tier.
- 28:03So you've got like a system level one and then you might have a virtual environment one.
- 28:07And then maybe there's another one that you use as well where you might import stuff from somewhere else in your Python path.
- 28:13Then when you import something, you never actually know unless you go and check what it's imported.
- 28:18Yeah.
- 28:19And it could do really bad stuff like, you know, just while you're talking, I was thinking, OK, so I write a package.
- 28:24The import statement is effectively execute __init__.py.
- 28:29Maybe in that section, I'll see if I could go and find SQLAlchemy or some database connection.
- 28:35And then I'll monkey patch that over for the connect so I can grab your connection string and then pass it along.
- 28:41So you think your app is still working or, I mean, there's all sorts of nefarious stuff you could do, right?
- 28:45Yeah, exactly.
- 28:45And I think it's just people put a lot of trust in a pip install statement in terms of not only what package they installed, but what dependencies that package had.
- 28:56And what dependencies their dependencies had and what ended up getting installed and whether any of those have actually polluted your site packages.
- 29:03So basically, if you're importing even a standard library module, like, is that has something else overloaded that?
- 29:11And is that import basically changing your global namespace?
- 29:17This portion of Talk Python To Me is brought to you by us.
- 29:21Over at Talk Python Training, we're building out our library courses, and that library is growing each month.
- 29:27That's why I want to take a moment and tell you about a special offer we have.
- 29:30It's called the Everything Bundle.
- 29:32With it, you get, well, everything.
- 29:34Every course we have in our library, including the ones yet to be published this calendar year.
- 29:38Many online training companies have subscriptions and discounts if you buy them a year at a time.
- 29:43Our Everything Bundle is like a subscription, but it's way better.
- 29:47With subscriptions, the moment you stop paying, you lose access to everything.
- 29:51With our Everything Bundle, you keep access to our courses forever.
- 29:55Any course published the year you bought the bundle or sooner is indefinitely yours, even if you never renew.
- 30:01Check out what's available at training.talkpython.fm.
- 30:04And if you're looking for a way to support the podcast, taking or recommending one of our courses is the very best way to do this.
- 30:11Thank you.
- 30:12That sort of hints at a really scary scenario because I'm pretty careful what I install, but I don't go and check the dependencies of everything that it depends upon before I use it.
- 30:25I'm like, yeah, this looks like a reputable package.
- 30:27I'll go ahead and use it.
- 30:29But it hints at almost like a supply chain challenge in that.
- 30:34Imagine I find some not very popular package that something popular is based upon.
- 30:40And then I go do a PR that sneaks in some extra sneaky dependency.
- 30:46That is actually the problem that people don't realize.
- 30:48And so when that gets pushed out to be updated, you update your thing.
- 30:52All of a sudden, you're indirectly pulling in this thing because some guy was like, yeah, it looks like a good PR.
- 30:57I'm busy, but we'll take it anyway.
- 30:59Go ahead.
- 30:59Yeah, it certainly can be.
- 31:00So, well, there isn't really one.
- 31:02Again, like the doctor.
- 31:04Don't do that.
- 31:05It hurts when I do this.
- 31:06Don't do that.
- 31:06Well, you do reference a service that I'm a fan of.
- 31:11So tell people about it.
- 31:12Yeah, so pyup.io basically has a sort of a database of packages and versions of those packages which have known vulnerabilities.
- 31:21So this links to one of the other things I put in the article, which was not patching your dependencies, which is a pretty common pattern where people say, okay, I'm going to get all my dependencies and then I'm going to do pip freeze or something.
- 31:34And I'm going to put that into my requirements.txt file.
- 31:37So I've basically pinned which versions do I know work and no one ever touched that file.
- 31:44So like that's probably one of the scariest things, as you said, here's all the versions.
- 31:48These are the ones we tested with.
- 31:50But no one goes and checks each packages or each dependencies website and sees the notification that says, oh, yeah, sorry.
- 31:57There's a massive security hole in version 2.9.6.
- 32:01Please update to 2.9.7.
- 32:03And it's the dependency, maybe the dependency of your dependencies thing.
- 32:07Like even if you were crazy diligent and you went and checked all the stuff you installed, that's not even enough.
- 32:12Yeah, exactly.
- 32:13So pyup.io basically has a database, a freely available database where you can look up bad versions of packages that exist on PyPI.
- 32:22And they have a service where they can actually go and scan your requirements.txt if it's on a GitHub repository.
- 32:29Or they have hooks where you can use it with other services like GitLab.
- 32:33And if they basically say that there are newer versions available or this is a bad version, then they'll give you a notification.
- 32:39So I guess that's one of the better ways of approaching the problem.
- 32:43And this is probably the point in my article that got the most negative feedback from people.
- 32:48They said, oh, of course, you have to pin dependencies.
- 32:50Like, you know, you can't just willy nilly install the latest version.
- 32:55But pyup does it does have version ranges.
- 32:58There's a there's a PEP for that.
- 33:00So you can say, you know, I want this minor version.
- 33:03So I want version 2.8 up to 2.9.
- 33:07And then if they release a 2.8.1 or 2.8.2, then you'll automatically install the right version.
- 33:12And if you hope that people use semantic versioning, the API itself and the functionality shouldn't have changed.
- 33:18And of course, you've got good test coverage anyway.
- 33:20So you'll catch it.
- 33:21That's a really good point.
- 33:22The problem is pip freeze doesn't do that.
- 33:25Right.
- 33:26And that becomes a ton of work on your side of things.
- 33:28I use pyup for my stuff.
- 33:30Like so the training website, the various podcast websites and a few other things.
- 33:33I have it hooked up and I will get weekly pull requests.
- 33:37These three packages have changed and they could be like deep dependencies.
- 33:42Here's their change log.
- 33:43Here's here's the new version.
- 33:44And there's just a PR which patches the frozen packages in my requirements at TXT.
- 33:49It's really nice.
- 33:50Anthony, how do you think about this?
- 33:52Yeah.
- 33:52So one thing I was going to add is that you can never predict when a package that you thought was good may turn bad.
- 33:59People might write a package and think, yeah, this version is great.
- 34:02And then someone somewhere finds an issue with it.
- 34:04So the key here is a regular monitoring of what you're doing and a regular patch cycle.
- 34:10Because you said you never know when something's going to happen.
- 34:13So you need to make sure you're looking for it.
- 34:14And that turnaround time is as short as possible.
- 34:17So the focus here is more on your operations.
- 34:20How do you manage your system as opposed to necessarily how you program it?
- 34:23But nevertheless, that's important.
- 34:25Yeah.
- 34:25I think another step there is you have to proactively be applying these changes.
- 34:31It's one thing to notice there's a problem.
- 34:33It's another to notice that PyUp sent me a PR that says, oh, you're depending on this version of, you know, a JSON parser or something.
- 34:41And it has this vulnerability.
- 34:42You need to fix it.
- 34:43It's another to go, yeah, and now I'm patching the server.
- 34:47You think, look at, what was it, Experian?
- 34:50Right?
- 34:51They were hacked with a known vulnerability.
- 34:54It was, everybody said, this is ultra bad.
- 34:56You need to drop what you're doing, skip lunch and go fix it.
- 34:59And it's just, well, we work at this big company.
- 35:02We can't be bothered to change this because if it breaks, this is not my site.
- 35:06But if I break it trying to fix it, I'm losing my weekend.
- 35:09So I'll let someone else deal with that on Monday when they get in.
- 35:11And then, you know, 143 million people lose all their credit and personal banking details, me included.
- 35:17Ouch.
- 35:18Yeah.
- 35:19But I mean, what are you going to do, right?
- 35:20I think there's like another layer that goes on top of this.
- 35:23Like you have to act.
- 35:24Yes.
- 35:24Yes.
- 35:25I mean, it's a patch management aspect of your server maintenance or system maintenance.
- 35:30I mean, as Ant said, there's tools inside Python, but you need to use them and you need to be using them regularly.
- 35:36You need to have that, you know, I do need to fix it now.
- 35:39So how do I, how do I decrease that turnaround time to as small as possible?
- 35:43Yeah.
- 35:44Yeah.
- 35:44Yeah.
- 35:45And what do you think about speed to actually deploying the fix?
- 35:48Like, are there like continuous delivery bits of magic that you people would be willing to rely upon?
- 35:54Like it says massive security vulnerability.
- 35:56We will let it automatically publish this if the test pass.
- 36:00I'm not sure.
- 36:00I mean, if your test passed, it still needs to go through some sort of UAT.
- 36:04It depends, I guess, on the, it depends on the project and depends on what it's for.
- 36:08Like if it's a, if it's an internal web application, then, you know, it's unlikely, but you also want to make sure that thing gets patched and updated as quickly as possible.
- 36:18The cost of, the cost of, the cost of downtime is one thing, probably smaller than the cost of attack or the cost of compromise.
- 36:25So I guess it's a, it's a, it's a kind of looking at it from the angle.
- 36:28Another thing I guess I want to talk about is that the way that you as a developer talk to operations, assuming you, you have an operations team that you're talking to, and that's not just another hat you have to wear.
- 36:40And the way you talk about what versions of the dependencies your, your package depends on and how brittle that is.
- 36:47Cause something I've heard a lot is people say, never know, don't touch my dependencies.
- 36:51Cause I know that they work and it was probably going to break the code.
- 36:54So from an ops, you know, angle, they're like, well, we're going to look after all the system.
- 37:01And then we're going to look after all the stuff that's, you know, dev have told us not to touch that and to leave it alone.
- 37:06Otherwise it's all going to go really bad.
- 37:08And if, if they touch it and they try and patch something, then the dev team's going to be like, oh, well, you know, you've, you've been changing the code and you've changed the dependencies and we didn't ask you to do that.
- 37:17And that's why it's breaking.
- 37:18So I guess it's, it's a conversation between the two teams or if you've got DevOps and then having a conversation about how you try and automate some of that stuff.
- 37:26Yeah.
- 37:27The more automation, the better.
- 37:28So the next one that you have on your article was surprising to me.
- 37:31I didn't really think about it, but I guess it makes sense.
- 37:34Is it temporary files?
- 37:35Yeah.
- 37:36I'm lucky enough not to have to use those very much in most of the stuff I write these days, but they're actually very common vulnerabilities.
- 37:43So apparently there's a MK temp function that'll generate a temp file.
- 37:46I didn't even know about this.
- 37:47I don't use temp files very much either, I guess.
- 37:49Yeah.
- 37:50It's a, it's an old one.
- 37:51And I came across this when I was doing building the course for moving from Python two to Python three.
- 37:56Cause one of the recommendations is that you don't use the Python two functions for creating temporary files because they have known security issues in them.
- 38:04So kind of digging into that a bit deeper.
- 38:06So there's a temp file module, which is the one that you should use.
- 38:10And there's a function called MKSTemp, which you should use to generate temporary files.
- 38:15Is that for secure?
- 38:16I'm pretty sure S stands for secure.
- 38:18Yeah.
- 38:18Nice.
- 38:20Okay.
- 38:20So the problem is there could be timing attacks or if you hit the time, just right, like you could create the temp file and then you could swap out the data before they read it or other bad things like that.
- 38:30Right?
- 38:30Exactly.
- 38:31So the reason the old one is bad is because it will create the file first and then it will open and load the file.
- 38:38So what can happen is someone can sit there scanning the file directory, the temporary file directory.
- 38:44An attacker can basically sit there looking at it constantly.
- 38:47And as soon as they see a file pop up, so the file handle is being created, then they can go and dump a load of data in there.
- 38:55And then you open it and then it's all changed.
- 38:58Like basically it's a way of them injecting data into your application.
- 39:01Yeah.
- 39:01That sounds bad.
- 39:02So another one, you kind of touched on this before is parsing other file types can be really bad.
- 39:09And I kind of riffed on XML being bad because it was old and complicated, but YAML is kind of new and awesome.
- 39:16That probably is bad, right?
- 39:17You know, this YAML one is so really hardly anybody knows that you can do this because when you look at the example, you think, why would they allow that to happen?
- 39:28Wait, hold on, hold on.
- 39:29I think exactly like maybe you shouldn't tell anybody about this because you're going to tell people they could do it as much as to protect themselves against it.
- 39:36Exactly.
- 39:37No, I'm just kidding.
- 39:38Go ahead.
- 39:38Tell everyone.
- 39:39So I guess the most popular package for reading and writing YAML files is called PyYAML and it has a method called YAML.load or YAML.loads.
- 39:50And if you actually read the documentation, it says in big letters, warning, YAML.load is not safe, essentially, because you can run any Python function by putting special characters in the YAML file.
- 40:05Yeah, like exclamation, exclamation, Python object apply, OS.sys, you know, some form of like Python code or calling some sort of sub process or all sorts of bad things.
- 40:17Like that's incredible that you can do that, that you can just say, shall execute this in my YAML file.
- 40:22So you can basically put a special, it's like two exclamation points and then a special path and then the name of the function in the standard library that you want to execute and the parameters.
- 40:34And then if you call YAML.load and the YAML text or the YAML file has that basically the special syntax in it, it will just execute that locally.
- 40:43So you can imagine like if you had a web application where you were asking people to provide some simple YAML input to describe something or you were loading something to configure something.
- 40:54I mean, an example that I used in there was, you know, I use YAML a lot with Ansible, which is like a network automation tool.
- 41:01And they had a, they had the vulnerability.
- 41:03They had the same vulnerability because in that product called Ansible Vault, they were loading it using YAML.load.
- 41:09And people are like, oh, awesome.
- 41:11I can just like pop a shell on this box instead of providing a password.
- 41:16That seems amazing.
- 41:18Anthony, do you use Ansible or work with YAML and worry about these things?
- 41:22Yes.
- 41:23I mean, this is, this is a perfect example of something that really, it's like for those, if any of your listeners are familiar with JavaScript, you used to have the eval that used to run to load in JSON, but they changed that to parse because eval was inherently unsafe.
- 41:38And this is an example of a function, which is just, it just goes on the, on the, on the do not use list for anything that is serious, right?
- 41:45You just don't use it.
- 41:46If there's no equivalent on it, then, then we look at something, something else because there's an, unless we go and rewrite it, there's, there's no workaround.
- 41:54It's basically like giving people like just dollar prompt access, like type here.
- 41:59What do you want to do on the server?
- 42:00Exactly.
- 42:01Exactly.
- 42:01To be honest with you, this is one of those things.
- 42:03When I read something like this, the first thing in my head is what were they thinking?
- 42:07I am with you.
- 42:08So first.
- 42:09Yeah.
- 42:10So, and tell us the fix.
- 42:11What is your fix here?
- 42:12Okay.
- 42:12So there's two fixes and I only found out about the second one yesterday and I think it might have something to do with this article.
- 42:18The first fix is that you don't use load, use safe underscore load, which.
- 42:24Of course.
- 42:25Of course.
- 42:26So in the same way that you have to use MKSTemp instead of MKTemp, you should use, however, I guess, explicitly saying you want to do things safely is a bit of an anti-pattern.
- 42:37So I did see there was a pull request which has been merged into PyYAML to make the safe version the default.
- 42:44And you have to explicitly ask for the unsafe version.
- 42:47However, I think that's only been merged in the most recent version of 4.1.
- 42:53And then if you ignore all of our prior advice about updating your pip dependencies, then you're never going to see that anyway.
- 42:59These do interplay, don't they?
- 43:01So I was definitely thinking of that API design sort of experience and like, wait, load and then the safe unload is what you're really supposed to do some of the time.
- 43:10Like, this does not help people fall into the pit of success, right?
- 43:16Like, to me, it seems like if you don't know what you're doing, it should default or just lead you down the path of, well, do the safe one.
- 43:24And if they need the crazy, you know, run this shell thing, like, make them hunt for that.
- 43:29Don't make that the default until they know better, right?
- 43:31Sounds like at least they've gotten that working now.
- 43:34Let go.
- 43:35Yeah, exactly.
- 43:35And I mean, the advice that I would give to anybody who is designing their own APIs is security by default.
- 43:41What you do, the most obvious way of doing things, the standard way of doing things should be the secure way.
- 43:47If you have features that are potentially unsafe, by all means, expose them that make the developer or the user specifically ask for it so they can accept and manage the risk.
- 43:59Yeah, that's perfect.
- 44:01One of the ones that I felt is kind of obvious on this list is obvious because I've been around for a while, I guess, is this concept of pickling.
- 44:10And pickling is the act of taking more or less a binary version of a Python object graph, putting it on disk, and then rehydrating it back into objects, right?
- 44:21Yeah, and it's typically used in sort of inter-process or inter-Python version communication or dumping an object somewhere and then loading it again somewhere else.
- 44:31And so it's kind of built into a standard library.
- 44:34It's been around for a while.
- 44:36And before things like JSON kind of became really popular, I guess it's one of the preferred ways of communicating or sharing data between Python processes.
- 44:46The problem is that it has a number of fairly well-documented security vulnerabilities.
- 44:51I mean, if you're loading up a pickle file, you can basically put things in the pickle file, which cause it to run any Python process.
- 45:00Like, if you basically create a new class and then you declare something called dunderreduce, then the contents of the dunderreduce method, if you return back a tuple, you can basically, the tuple that you return can be the name of a standard library function and its arguments.
- 45:16So basically, that's, again, where you do os.system or subprocess.popen, and you basically pass it all the arguments.
- 45:23So instead of loading this nice pickle file and you continuing with your day, basically, you load a pickle file and it just runs commands on your local machine.
- 45:31Yeah, that's not amazing.
- 45:33So, again, one of these sort of don't do this.
- 45:37It's extra hard to check, too, because the thing you're delivered is a binary blob.
- 45:42So how do you know whether it's safe or not?
- 45:44You can't even look at it.
- 45:45It's not even like the YAML file where you're like, wait, wait, wait, what is that double, double, double exclamation mark Python thing?
- 45:50That doesn't look right.
- 45:51Right.
- 45:52This is just, you know, one, zero, one, zero, zero, one.
- 45:54Seems okay.
- 45:55Yeah.
- 45:56So there's not really a workaround for this other than don't use pickle or don't try and unpickle things from an untrusted source.
- 46:04But then I don't even like saying that because it's that kind of assumption which attackers use for using attacks, combining them together.
- 46:13But Anthony knows a lot more about that than I do.
- 46:15Yeah, it's chaining them, right?
- 46:16It's like you lose one to get one layer in and then another and then another.
- 46:20Yeah.
- 46:20Ant's got a very good point.
- 46:22The way a lot of attackers compromise systems is they won't go after the top tier, the most important, the most visible ones.
- 46:30They'll try to look at maybe the second or third tier systems, ones that people may not have paid as much attention to or made as much effort securing.
- 46:40And if they can compromise those, they will use those as a foothold to go after things that people perceive as important.
- 46:48So if I compromise an app and it's communicating via pickles to other apps, for example, that can be how the exploit occurs.
- 46:57So it's good to keep in mind that every system you write has to consider security, as Ant is saying, and follow Ant's advice on looking at these 10 topics.
- 47:06Yeah, I mean, maybe this is some wimpy little app that's just like some reporting thing, but you could use it to get behind the firewall or maybe you could pickle some bad stuff into a shared Redis cache that then the other system might read.
- 47:18And then, you know, you just level up step by step, right?
- 47:21The so-called lateral movement.
- 47:22It is being recorded, you're right.
- 47:23Exactly, exactly.
- 47:24And look, three, four, five hops for the compromises is not unusual.
- 47:28And when you see it going, you realize how good some of these attackers actually are.
- 47:33But I digress.
- 47:34That's right.
- 47:36That's right.
- 47:36So I guess the last two kind of blend together a little bit in about not patching your Python runtime, sort of the main one, what you get if you just go to the open a shell and just type Python 3 or something, or your dependencies as well.
- 47:51And I heard that you don't really need to patch your Python runtime because there's zero days that sometimes appear in like your OS.
- 47:58So you can just like, this is not worth bothering with, right?
- 48:00What was that crazy comment was about?
- 48:04I can't remember.
- 48:04There was a comment on those lines.
- 48:07They were like, oh, but why do we bother?
- 48:09Why do we have to patch Python?
- 48:10Because they're probably just going to hack us anyway.
- 48:11So what's the point?
- 48:12If only.
- 48:13If only it worked that way.
- 48:14I think that's, I mean, we touched on that at the beginning a little bit.
- 48:17And that is sort of a sense of despair that people have.
- 48:20You're like, look, I could do all these things that I'm supposed to do.
- 48:23But soon as there's some sort of zero day or O day in my system, you know, all bets are off.
- 48:28I'm not going to be able to defend against that.
- 48:31I don't know what zero day there is in, you know, NGINX unit.
- 48:36And so forget it.
- 48:37I'm just not going to worry about security.
- 48:39And I can totally understand how people feel that way.
- 48:41But there was some interesting analysis and reports saying that even state sponsored actors of hackers and stuff primarily use super boring stuff, like all the things we've already been talking about, right?
- 48:54Like SQL injection and other boring unpatched things.
- 48:57I mean, zero days, yes, they happen.
- 48:59But as a proportion of total security incidents, they're actually relatively small, particularly for the well-known, well-trusted packages and products that are out there.
- 49:11I think this is one of those cases where people need to get their own house in order before they throw stones, if you know what I mean.
- 49:17Yeah, yeah.
- 49:19But I mean, once again, I mean, most of this stuff is relatively simple once you understand what you need to do.
- 49:25So it's just a question of putting time and effort into it.
- 49:28That's all.
- 49:28I think there's a difference between the NSA or some other country is trying to literally get into your thing or I'm on the Internet and people are rattling the doors to see if they're open.
- 49:39And they're not going to spend a full week trying to get into my system because they know there's thousands that just have SQL injection or some other random thing, right?
- 49:47Yeah, and look, to be honest with you, if the NSA really, really wants to get into your network, the version of the Python package you're using is probably the least of your problems at the moment.
- 49:56So...
- 49:58Yes, exactly.
- 49:59Exactly.
- 50:00Plenty scary, but we're not going to think too much about that, I guess.
- 50:03So what you're telling me is I need to install Python 3.7 because it just came out.
- 50:08Things like that.
- 50:09Yeah, I'd actually say, this is probably controversial, but I wouldn't install Python 3.7 until 3.7.0.1 comes out.
- 50:17Generally, I'd...
- 50:19Yeah, yeah, sure.
- 50:19But maybe, Pat, like if there were like 3.6.6 or 3.6.7, whatever the next one there is.
- 50:263.6.6, yeah.
- 50:27I'd go with 3.6.6.
- 50:28But it's part of that staying on top of things.
- 50:30I wish PyUp actually had a check to say your Python version is 3.6.6 and it should be 3.6.7 or something like that.
- 50:38There is another tool, which is open source as well, called InSpec.
- 50:41And it's made by the same people that brought you Chef.
- 50:46So it's built in Ruby, but it does actually have a bunch of hooks for Python applications.
- 50:51And I know that it does do virtual environments and also checking versions of packages in production.
- 50:58So you can say, make sure that none of our servers in production have this version of Python.
- 51:03Make sure that they don't have these versions of these packages as well.
- 51:06And you can do that in InSpec and basically declaratively saying, here's our security rules.
- 51:11And it will go and scan through your installations for you or your service for you and actually give you back a report.
- 51:17So InSpec, I guess, would be the way to do it or a tool like that.
- 51:21Yeah, that makes a lot of sense.
- 51:22All right.
- 51:23Well, I think hopefully people learned a lot from those 10 items.
- 51:28I mean, there's certainly a couple of them that I had heard of, but others were new to me.
- 51:32And it's always good to be aware of these things.
- 51:34So thanks for putting that together and both of you for sharing it here.
- 51:37So Anthony, how do I sell this to my, you know, I work at a startup.
- 51:42The management just got VC funding.
- 51:45We're supposed to do growth, growth, growth.
- 51:47We're trying to figure out new features so that we get this thing to get out.
- 51:51How do we get ourselves to worry about this?
- 51:53So, well, the first thing is to understand any legal or contractual obligations that you have.
- 51:58So if you're in Europe, for example, you have to follow the GDPR, the new general data protection
- 52:04regulation that's there.
- 52:05If you're handling credit cards, for example, you've got PCI DSS, the payment card industry
- 52:11data security standard.
- 52:12These are the non-negotiable things you have to do.
- 52:15The second thing is if you have any security executives or leaders in your organization,
- 52:21a CISO or if they have corporate policy, security policies need to follow.
- 52:25They're two things.
- 52:27But beyond that, one is just look at the newspaper.
- 52:29Reputational and financial damage is real.
- 52:33It happens.
- 52:34We've all seen or heard of companies that have been hacked and thinking, gee, I wish I weren't
- 52:39them.
- 52:40I wish I was not them.
- 52:41So don't.
- 52:42Don't be them.
- 52:44Exactly.
- 52:46Well, and also I think it comes to do with culture a little bit, right?
- 52:49Like if the culture is do not change this application, do not touch it, because if you break it, you're
- 52:56going to take it down some part of our business and we're going, it's going to be so bad that
- 53:00that thing ossifies.
- 53:01And it's basically like, no, it's kryptonite.
- 53:04Nobody wants to touch it.
- 53:05I'm going to stay away from that.
- 53:06Yeah, it probably is got some security issues, but I'm not touching it because I will be
- 53:10punished to no end if I'm the one who breaks it.
- 53:12I can see that happening a lot.
- 53:14Yeah, it is.
- 53:14And this comes down to your general culture around, as Ant said, test coverage, refactoring.
- 53:20If you have stuff that you can't touch, it's going to cause you security problems.
- 53:25If it's stuff that you can touch, you have a fighting chance of solving them.
- 53:29So that comes down to, once again, to the term that uses risk management here.
- 53:33Is your management happy to accept the risk that you're going to have an app that will
- 53:37eventually cause you a problem?
- 53:38Right.
- 53:39It's sort of the consequence of failure there.
- 53:40I mean, look at WannaCry and Windows XP on a lot of these things.
- 53:45There's so much damage from that, right?
- 53:48Yes.
- 53:48And particularly if you're in a small shop, if you're in a startup, reputation is big early
- 53:54on.
- 53:55So you need to make sure that you can maintain that trust that you're going to need from your
- 53:59first few customers.
- 54:00Yeah.
- 54:01And you probably have an advantage in the sense that you don't have big data centers with legacy
- 54:06machines laying around.
- 54:07You're probably on the cloud using modern tools because you just started.
- 54:10All right.
- 54:11Chances of keeping that upgrade train running is probably easier.
- 54:14Exactly.
- 54:15Exactly.
- 54:15It's all greenfield.
- 54:16You don't have to keep older stuff running.
- 54:18Exactly.
- 54:19Exactly.
- 54:20Yeah.
- 54:20All right.
- 54:21We're getting pretty short on time and I'll be respectful of both of your times, but maybe
- 54:25you could tell us really quick some places to learn more about security.
- 54:28Yeah.
- 54:29I guess one, two projects I want to call out.
- 54:31So there's a number of projects with the same kind of name that's related to goats.
- 54:36I'm not sure where the goat name came from originally, but basically there's a project called
- 54:41Web Goat, which was originally done as a how not to do things, open source project to basically
- 54:47include all of the top 10 most common vulnerabilities.
- 54:52And I can't remember what language it was written in, probably Pearl, but there's a Python version
- 54:56called PyGoat and there's a Django specific one called Djangoat, which I guess we'll have
- 55:02in the show notes.
- 55:05But if you actually look at that, it's got all the Djangoat ones really cool.
- 55:09It's got all the stuff you should never do with Django projects like leaving debug enabled,
- 55:13for example.
- 55:14But basically how to do the, all the kind of vulnerabilities we've talked about as well
- 55:19as the other ones in a project.
- 55:21So it's kind of like a how not to do things project.
- 55:23And I think it's good if you want to learn how things are done.
- 55:27And also it has instructions about how to break it, how to use those vulnerabilities.
- 55:31So I would start there.
- 55:33And then if you know them, then you can start doing things like doing scans through your source
- 55:39code for those particular types of vulnerabilities or those functions and seeing if you can find
- 55:44bugs in other places.
- 55:45Yeah, that makes a lot of sense.
- 55:46I really like this goat series.
- 55:49That's pretty interesting.
- 55:50One thing I want to give a quick shout out to that's in your neighborhood and is a podcast
- 55:55called Risky Business.
- 55:56Have you heard of it?
- 55:56I listen every week.
- 55:57Yeah, I love it.
- 55:58It's fabulous, man.
- 55:59Those guys do such a good job.
- 56:00It's like headline news in security and tech programming security.
- 56:05It's really great.
- 56:06And just listening to that, you learn a lot sort of by osmosis, I think.
- 56:10All right.
- 56:11Well, I think just we're out of time.
- 56:13We could go on and on and maybe we'll have to do another episode on this.
- 56:16But that was really helpful and insightful.
- 56:18So thank you both.
- 56:19Since there's two of you, I'm going to ask two questions at the end, but the same one.
- 56:23We'll just do the notable PyPI package.
- 56:26So is there a package, maybe related to security that people maybe haven't heard of,
- 56:30but you want to throw out there to let people know about it?
- 56:33Anthony, you want to go first?
- 56:34I had a couple, but if one of those things that I think rather than a package, I think people need to take a step back and realize that security is a mindset here in that there's no package that's going to make your app secure.
- 56:48So I'm going to twist the question a little bit here.
- 56:51It's about keeping abreast of what's going on and keeping current and continuing to learn.
- 56:57And it's an area that can be fascinating, can be really interesting.
- 57:00Yeah.
- 57:01Excellent.
- 57:01And?
- 57:02Yeah.
- 57:02This one hasn't actually been released yet, but Brian and I are working on a new pytest plugin called pytest Requests.
- 57:08And by the time this episode goes to air, it'll be out and it'll be wonderful.
- 57:12So I'd recommend you check it out.
- 57:13Oh, that sounds really fun.
- 57:14That's awesome.
- 57:15And also, I want to give a shout out to your Python 3.7 course.
- 57:18You just did a new course on Python 3.7.
- 57:21I thought it was great.
- 57:22Thanks.
- 57:22Yeah, that just came out just in time for the actual release of 3.7, which is good.
- 57:26Yeah, you beat the window by like a week or something.
- 57:29Maybe just tell people super quickly where they can get it and what it's about.
- 57:32So the course is on Pluralsight.
- 57:34If you have access to Pluralsight or you want to sign up for a trial, you should be able to watch the course.
- 57:38It's less than an hour.
- 57:40And I basically go through all the major new features in 3.7, how to use them, how they work, how to configure them, as well as some of the other benefits.
- 57:49So it'll take an hour of your time and you'll be a Python 3.7 expert by the end of it.
- 57:54But you should wait till Python 3.7.1 to actually get going, maybe?
- 57:58Probably, but that'll be weeks away, I reckon.
- 58:00I reckon, yeah.
- 58:01I already installed 3.7, but I have 3.6 on my machine just in case I need it.
- 58:04All right, awesome.
- 58:05Well, yeah, I enjoyed it.
- 58:06So good to let people know about that.
- 58:08All right, both of you, final call to action.
- 58:10People, maybe they're thinking about the software that they have.
- 58:13What's the first thing to do to start to address this?
- 58:16Anthony?
- 58:16The first thing is to take a step back and think, well, what is the most important things that my app is protecting?
- 58:22What are the most important business processes or data that's there?
- 58:25And to think, how can I protect it?
- 58:28How should I protect it?
- 58:29Is what I'm doing what I need to do?
- 58:32All right, excellent.
- 58:32Anthony, anything to throw in on that?
- 58:34Yeah, I'd say just don't be intimidated by security in general and just start simple and be willing to learn and also be willing to learn from people who have a lot more experience, of which there are plenty of people out there.
- 58:46Yeah, it's definitely a different way of thinking and it's pretty fascinating.
- 58:50All right, well, thank you both for being on the show and sharing all this stuff.
- 58:53It was great.
- 58:53Thanks, Mike.
- 58:54Thank you, Mike.
- 58:54Great to be here.
- 58:55Yeah, bye-bye.
- 58:55This has been another episode of Talk Python To Me.
- 58:59Our guests on this episode have been Anthony Shaw and Anthony Langsworth.
- 59:03And this episode has been brought to you by the Test and Code podcast as well as Talk Python Training.
- 59:09Keep up with the latest news and become a better developer by listening to Python Bytes at pythonbytes.fm and Test and Code at testandcode.com.
- 59:17Want to level up your Python?
- 59:19If you're just getting started, try my Python jumpstart by building 10 apps or our brand new 100 days of code in Python.
- 59:26And if you're interested in more than one course, be sure to check out the Everything Bundle.
- 59:30It's like a subscription that never expires.
- 59:32Be sure to subscribe to the show.
- 59:34Open your favorite podcatcher and search for Python.
- 59:36We should be right at the top.
- 59:37You can also find the iTunes feed at /itunes, Google Play feed at /play, and direct RSS feed at /rss on talkpython.fm.
- 59:47This is your host, Michael Kennedy.
- 59:49Thanks so much for listening.
- 59:50I really appreciate it.
- 59:51Now get out there and write some Python code.
- 59:53I'll see you next time.
- 59:53I'll see you next time.