There have been multiple npm package attacks over the last couple of days. The impact has been huge. And it may be even bigger in the future. As developers, we must try to reduce the danger of being compromised by attacks like these. Thankfully, there are some ways of doing that.
Audio and video play straight from the show's own hosting. PodSubs stores and serves no media.
Transcript
From the show. Tap a time to play from there.
There's a good chance you already heard or read about the NPM package attacks that happened over the last couple of days, over the last week essentially. There have been two major attacks on the NPM package ecosystem where collectively, packages with billions of downloads, weekly downloads, have been targeted by attacks that aimed to distribute and
deploy malicious code, steal secrets from your machine, for example, from CICD machines, and so on. Now, in case you haven't really heard or read about these attacks, I'll briefly summarize what happened but then, more importantly, I wanna dive into what that means for you and me, for us developers, because this of course is a kind of attack and a scale of attack
that's ... well, I'm sure it has happened before but it's not something we see every day and it has some serious impact on the NPM ecosystem and how we as developers should interact with those packages, I'd say. But let's start at the basics. It started essentially a week ago with the first big attack which, uh, was less impactful than the second attack to which I'll get. But that first attack essentially worked by
getting access to an account, an NPM account of the maintainer of one NPM package which of course gets used by other packages and so on. And that attack in the end happened through a phishing email, so they used a phishing email to get access to this package maintainer's NPM account and through that access which they got successfully, they were then able to deploy a malicious version of that
package, a package which came with some extra code which in the end deployed a cryptocurrency, uh, token stealer. So the goal of this attack essentially was to get into packages that are then used on websites to steal cryptocurrency in the end from the visitors of these websites. So the goal of this attack was not to compromise the system of people, of developer, uh, developers using
that package or of, um, machine CID- CICD machines that deploy, uh, applications using that package. That was not the goal of this attack. The goal of that attack was to run on websites and to target the users of those websites to steal cryptocurrency, and that wasn't too successful. Uh, from what I read, they were able to steal like $1,000 or so, but still what this attack shows us, of course, how dangerous using
lots of NPM packages can be, and I'll get back to that later, and how vulnerable the entire NPM ecosystem can be. Because even though this first attack wasn't too impactful, it was, of course, a successful attack. And the second attack about which I wanna talk shows us that the threat can be much higher than what resonated or what was done by that first attack. Because that second attack which happened a
couple of days after that first attack which, as far as I know, isn't directly linked to it as it seems, but I'm not 100% sure about that. But that second attack was now not about stealing cryptocurrency. That second attack was about getting on your machine or the machines where applications are deployed, CICD machines, and stealing security credentials, credentials in general, stealing AWS access keys, NPM, uh,
access tokens, GitHub access tokens, and all kinds of credentials, uh, security keys, tokens you might find on those machines. Now that second attack, as it seems, as far as I know, did not start from an phishing, from a phishing mail but instead from a already compromised, um, NPM account from an earlier attack potentially. Whatever it was, they were able to publish a new version of a package,
ironically of a package that wasn't super actively maintained, so that hasn't seen a lot of updates, uh, in, in the past. But they now published a new version of turns out, that package was used by enough other packages to have an impact because this second attack actually now was not about including some code that runs on the deployed website. Instead, that second attack was about running malicious software in the end, a malicious script on the
machine where that compromised package was used in a build process and then that script would actually search for secrets, for credentials, for access tokens on that machine and use those to A, exfiltrate them, to share them with the attackers but B, also to spread. So it was a worm. The goal was to then use these access tokens, for example, to publish the same malicious software on
other packages. So if package B used package A which was compromised, then during the build process of package was...... the chance that this malicious code, which was in package A, would actually get access to the GitHub repository to the NPM account of package B as well and then continue spreading, and that's, of course, extremely dangerous and it was and is an extremely dangerous attack because of this worm, uh, like behavior of because it was
spreading and it was not just stealing those secrets from one affected package, but it was using them to affect more packages. And, therefore, this second attack had a way larger impact than this first attack. Ye-, because of this worm-like behavior, this attack was able to successfully compromise many hundreds of packages because all these packages, of course, typically depend on each other and then you might just be using
one of these packages, but because of the packages internally, suddenly you might be affected as well. So how can you find out if you are affected? Well, a couple of things. Uh, for one, you should, of course, check whether your projects which you built or where you downloaded, um, new versions of the packages over the last week or so, if these projects use one of those, uh, known compromised packages, and I'll put some links below this, uh,
this episode, to help you scan for that. So that's, of course, one thing, and when you look for these packages, not look in your package lock, uh, in your package JSON file, but in your package-lock.json file because that package-lock.json file, that's actually the file that lists all the packages used in your project, including the packages used by packages and it shows you the exact versions used there, which allows you to find out that if you are using one of the compromised packages, if you're using one of the compromised
versions, which, of course, is an important information. So that's one thing to do. Uh, in addition, due to how that attack worked, it seems that it exfiltrated, uh, those stolen secrets mainly by creating a new public GitHub repository in your account if you are affected, uh, which had this name, uh, I, I keep on forgetting it, shayhulut, which had this name. So you wanna check for that and you wanna check your GitHub security logs, to which you also find a
link below, you can use it for your account to check any action related to creating such a repository or switching one of your existing private repositories to public because that's also something this attack did and so on. So these are steps you should take and again you'll find more detailed resources below. That's one thing that you should do right now if you fear that you might be affected. But, of course, this attack also brings up the question, what can we do to mitigate such
attacks or to prevent such attacks in the first place? Of course, that's a pretty big thing with m- with many building blocks because, of course, it all starts with making sure that if you're a package maintainer that your NPM and GitHub accounts are secure and are not easily compromised by using two-factor authentication, though even that is not a 100% guarantee because with phishing there are ways of getting around that too with social engineering and so on, but it is, of course, important. There are a couple of steps you can take
maintainer to help reduce the, the risk of becoming a spreader of software, but, of course, you and me, you might primarily be users of packages, uh, and, therefore, the question is what can we do? And it starts with limiting the amount of packages you're using, I'd say. I'll get to some other recommendations as recommendation I haven't seen enough in all those blog
posts and articles and so on I read because there is a tendency for some developers at least to use too many packages. Um, y- uh, it... and I get it. It's convenient, right? Um, you can solve a lot of problems you might during the development process by bringing in a package that does what you're looking for. Um, instead of writing your own code to solve in a package and you're done with it.
And, of course, for certain tasks, that makes sense. You're using React because you want a package that, uh, makes it way easier to reactively update the DOM and build highly interactive, uh, browser-based web applications, so yeah, you wanna use React probably. For authentication, you probably... you'll wanna use a package. For your backend framework, you probably wanna use a library. So we
all use them, of course. We, uh, we all probably rarely build web applications totally from scratch with just HTML, CSS, and JavaScript and no packages, though that is actually a valid strategy for certain kinds of sites and applications. Just saying. But yeah, we all use packages. But do you really need a package for checking whether a number is odd or even? Not sure about that. And, of course, that's an extreme example, but you get my point. Especially now with AI
available, it's so easy to quickly generate code for problems you might not know the solution immediately, like let's say you need to invert a binary tree, not something you need to do every day, applications, in most applications, but let's say you, you want some efficient algorithm for searching or for inverting a binary tree, whatever.If you need to do something like can bring in a package that does it for you, you can sit
down and try to learn the algorithm and write it from scratch, good exercise, or you can use AI to generate it because exactly the kind of tasks that are super easy for AI. Now I will say, AI-generated code is not necessarily secure and AI also sometimes wants to bring in extra packages, packages. It can be lazy too. But still, uh, y- you really can write more code on your own or with help of AI than you might think. So, that's really one thing that's important
because there is this tendency f- by, for some developers at least, to solve everything by adding a new package, a new library, and I would recommend to try as little packages as possible. That is something I've done for years. I like the challenge of using, yeah, not a lot of packages, not a lot of libraries. I like the challenge of solving problems Now with AI, that's even easier. But of course, I'm also using
packages. And if, due to how those packages work, that they all have their own dependencies, would get affected by, uh, a worm like this, then we're, of course, all in trouble. Uh, I don't even wanna think about that happening. That would probably affect hundreds of millions of machines if such a package would get compromised. But yeah. So limiting the number of packages you use is a good idea. Another thing you might wanna consider
doing is fix your dependency versions. And I'm not necessarily doing that. I'll be 100% honest here. I should be doing it. I will be doing it in the future. I haven't done it, but I've read it a lot related to these attacks and, of course, it makes sense. We typically fix the major versions of our packages. Maybe also the minor versions, but not the patch versions. So in case you don't know, um, many packages, or actually, well, pretty much all these
packages, um, on NPM have, like, a major, a minor, and a patch version number in their number and depending on how you configured your package JSON file when you run NPM install and so on, you either download a very specific version or the latest patch or the latest minor or even the latest major version. And in theory, it makes sense to always download the latest patch version because patch versions, depending on how the maintainer handles
in theory, they don't change the fundamental nature or behavior of a package. They don't have breaking changes they fix security issues or performance issues and so on. So in theory, you wanna stay up to date with patches, but in reality, as attacks like this show us, was spread through a patch update, we might not want to automatically use patch versions either. We might want to fix our versions and then deliberately choose to update our version numbers after carefully
making sure that the new version is safe, not just from a b- breaking change or performance perspective, but also from a security perspective. It is more work, but how much work is it to clean up a compromised system, to rotate all your, uh, compromised tokens and credentials and mitigate that, uh, uh, that danger and, uh, that damage that was done? So yeah, that's one thing. Uh, if you're using PNPM as a package manager,
example, which I don't, you can even set a certain setting, the minimum release age setting, to make sure that you do, for example, automatically download patch versions. But that the package has to be a minimum number of, uh, days old, uh, for example. So that you make sure that you're not downloading patch versions on day one, a couple of hours after they were released. Because with those attacks that happened,
was that they had a relatively small attack window. The attack, thankfully, was detected quite quickly and those compromised versions were unpublished, so the window of attack was relatively small but big enough to affect many, many machines. Now if you were to set a minimum, uh, release age, which again, is available with PNPM, but to my knowledge not with NPM right now, if you were to use this setting,
or with more safety I should say, uh, auto-update for new patch versions and so on. But you would make sure that you only do it after a couple of days to make sure that others have tested the water first and potential security issues might've been detected. It's not a guarantee, but it's an additional, uh, step to make your environment more secure and it allows you to still auto-update, um, for example. So that's another thing you might wanna
consider. Now another thing you should think about or you should do in general, to be very honest, is limit the scope of your access tokens and credentials. It is, of course, super convenient to always create access tokens that have full access to everything. Like, let's talk about AWS. If you run some deployment process that also uses AWS or the deployed app is using AWS and therefore is part of the,
uh, build process, you are exposing your AWS access key and your credentials to the build process to c- bake it into the built application, so to say.It is, of course, convenient to have an AWS access token that gives the application admin access to all your AWS resources because it's your code, right? What could go wrong? Well, something like this attack could go wrong. If that AWS token is ever stolen, like it could be through that attack, then you're in a huge
trouble. You have a token out there in the wild attackers full access to your AWS account, and that's just one example. It's the same, of course, for the other cloud providers. It's the same for AI tokens. I mean, think of, um, some malicious actor stealing your OpenAI access token and then using AI, uh, and you're paying the bill. Of course, you might have some limits in place there, that is not a lot of fun. So as a best practice, you should really try to limit the scope
those access tokens and credentials if it's possible. Not all providers, not all services give you a lot of options there, but many do, and if you're, for example, building an application that only needs to send email through AWS, then your AWS access token should only have access for exactly this action, to exactly this action. Now, in case of AWS and also other services, there are even better ways than using an access token in the first place. You can get
short-lived credential stare as well, uh, but that is beyond the scope of this video. You, of course, might wanna look into such solutions as well if you're using a service like AWS that offers even more secure ways of accessing it than those general access tokens. But if we're talking about access tokens and credentials, limiting the scope is definitely something you might wanna do. And th- of course, if you are affected by the attack, by the way, you wanna rotate all your tokens and all your credentials.
You wanna change them all because you have to consider them compromised. That's, of course, clear. Well, and that's in the end it for my two cents on the attack here. The most important takeaway I have from this attack is really the part with scoping your tokens, limiting the scope, I mean, not automatically updating to the latest patch versions all the time automatically, and maybe my very most important
point, not using too many packages. That is, of course, the best line of defense here because what us is how vulnerable this entire JavaScript ecosystem is. If one package that's used by enough other packages gets compromised and acts as a worm, as it happened in the second attack, then we can quickly have a spreading wildfire that's very hard to contain and that can quickly affect millions or even billions of machines, um, be that
your laptop or the machine in the cloud where you deploy or build your application. And that, of course, is an unbelievable huge threat. So yeah, that is something we should all consider these attacks have shown us. We should make sure that we ramp up our, um, protection, our mitigation strategies, that we make sure that we, uh, limit the, the danger we face on
our machines or, uh, regarding to our credentials and so on. And, uh, yeah, let me know what you think of that, also want me to mention. As I mentioned, I'll put some important links, um, in the description, and, uh, yeah, I'll see you in the next video. Bye.
GPT 6 Astra was released. It's a good model, I have to say. Especially for 3D work it seems to be a real game changer. But due to all the hype, you may be disappointed...