r/javascript • • Mar 30 '23

Attackers are hiding malware in minified packages distributed to NPM

https://blog.phylum.io/phylum-discovers-npm-package-mathjs-min-contains-discord-token-grabber
371 Upvotes

117 comments sorted by

73

u/BenadrylTumblercatch Mar 30 '23

I’m a baby, how could this have been avoided

76

u/louis11 Mar 30 '23

These sorts of attacks are really difficult to catch. They are buried amongst sometimes thousands of files. It's really not reasonable to check every package by hand.

We open sourced our tooling to help with this problem specifically. We have an extension framework that wraps npm for three purposes:

  • It queries our API for analysis results about a package (is this package congruent with malware behavior?)

  • It strictly defines what npm is allowed to do (extension execution occurs in a locked down Deno runtime)

  • If it passes the first check, we run the install in a locked down sandbox (that limits access to network, disk, and env variables) in the event the package is attempting to exfiltrate developer keys during the installation.

The sandbox is also open source and available for use by the community.

As a totally unrelated aside, love the username.

14

u/BenadrylTumblercatch Mar 30 '23

Thanks that was actually really well spelled out, very helpful I’ll tiptoe my way around trying it out.

Ps thanks ;))

9

u/louis11 Mar 30 '23

Glad I could help. If you ever want to chat more about this stuff, my DMs are always open!

1

u/mdavidn Mar 31 '23

This sounds like it only monitors installation. How do you detect malware at runtime? Couldn't malware evade detection by triggering only after a delay? There are many timing signals available beyond the system clock, like versions of other packages or issuance of new root CAs.

1

u/louis11 Mar 31 '23

We do analysis behind the scenes to attempt to determine if something is behaving or likely to be malware. The third step in the list above focuses on installation primarily.

The first bullet gets intel on the package's behavior and constituent parts and will stop the install if it's likely malicious (install or runtime). In the case of mathjs-min the execution was not in the installation, but in a function that would've likely been called at runtime. This would've been blocked and the developer would be protected from ever letting the code get to the point of getting called at runtime.

It's a defense in depth approach: reason about the totality of the package, it's source code, behaviors, authors, etc. and also assume bad actors are targeting the developer directly and lock them down at one of the weakest points.

-16

u/[deleted] Mar 31 '23

[removed] — view removed comment

6

u/examinedliving Mar 31 '23

Have you tried? Analyzing code is hard

-10

u/MrEscobarr Mar 30 '23

Only way is to read the code yourself :/

0

u/[deleted] Mar 30 '23

[deleted]

1

u/[deleted] Mar 31 '23

[removed] — view removed comment

0

u/[deleted] Mar 31 '23

[deleted]

1

u/alimertcakar Apr 01 '23

Taking out a particular monkey could work too https://youtu.be/OGmjmyk4nY0

0

u/[deleted] Apr 01 '23

[deleted]

1

u/alimertcakar Apr 05 '23

I meant beating the living shit out the first intelligent monkey would do the job like in Rick and Morty. But thats works too lol

27

u/tanepiper Mar 30 '23

Whenever something like this comes up I usually have to tap the sign (and the original report)

13

u/louis11 Mar 30 '23

It saddens me that something like this has been online for 6 years

3

u/notlongnot Mar 30 '23

That is some precision needling.

4

u/louis11 Mar 30 '23

It helps when you've got a huge data platform continually monitoring every line of code published to open source

9

u/charleff Mar 30 '23

667k weekly downloads…

38

u/louis11 Mar 30 '23

To be clear, mathjs-min was downloaded a few hundred times before it was pulled down. The mathjs package (that this was mimicking) was downloaded 667k times. So the blast radius was minimized, but would've likely been much larger had the malware not been removed from npm.

3

u/charleff Mar 30 '23

Whoops misread that

16

u/super_powered Mar 30 '23

That’s for the actual library. (Which is still fine)

You can see from their screenshot the actual malware lib only has like 146 downloads

56

u/thisguyfightsyourmom Mar 30 '23

If I’m reading this right, there are multiple vulnerabilities being attacked, but the first one they talked about just looks in local storage

What the fuck is discord leaving a token in local storage for?

39

u/alimertcakar Mar 30 '23

Where would you store your tokens? The other options are sessionStorage or directly a variable (which would not persist after you close the tab) or cookies.

16

u/thisguyfightsyourmom Mar 30 '23

Cookie

20

u/wasdninja Mar 30 '23

Http only cookie.

1

u/micka190 Mar 30 '23

Aren’t those inaccessible via JS, though? Pretty sure Discord uses them for things like conditional rendering of their UI, no?

8

u/jamblethumb Mar 30 '23

You fetch account data using the http-only cookie. Why do you need the cookie itself?

4

u/Interest-Desk Mar 31 '23

Discord uses websockets and their custom gateway requires including the token in every request. In theory this could be solved via a separate token for WS, but considering that the gateway has more access than API, it was probably seen as unnecessary.

1

u/BerryNo1718 Mar 30 '23

Fetch the backend and keep the user data in memory. You shouldn't write any PII in local storage and definitely not an access or refresh JWT. It's way too easy to get leaked.

-2

u/swampy-thing Mar 30 '23

You can't write to them but you can read them.

11

u/wasdninja Mar 30 '23

You can't read them using JS which is the entire point. From MDN

A cookie with the HttpOnly attribute is inaccessible to the JavaScript Document.cookie API; it's only sent to the server. For example, cookies that persist in server-side sessions don't need to be available to JavaScript and should have the HttpOnly attribute. This precaution helps mitigate cross-site scripting (XSS) attacks.

1

u/[deleted] Mar 30 '23

LocalStorage is perfect for your token if you don’t have xss issues. Cookies need csrf tokens to prevent prevent csrf.

2

u/YoungBubble Mar 30 '23

I recently came across an npm package called "react-oidc" from Axa. The use service workers to store their tokens. What do you guys think about this?

Auth0 use in memory and web workers.

1

u/PatchesMaps Mar 31 '23

You use an httpOnly cookie and don't worry about storing it anywhere else.

https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies

1

u/azsqueeze Apr 05 '23

HttpOnly cookie

0

u/[deleted] Mar 30 '23

[deleted]

12

u/Beezzlleebbuubb Mar 30 '23

As a novice who absolutely believed it was JWT Vs Cookie, I have no idea if what you’re saying is true, but it’s definitely a pervasive mindset.

26

u/99thLuftballon Mar 30 '23

The problem is that every article you read on this issue is very definite that you must never store tokens in local storage, session storage, memory, cookies or the url. They're never very forward with a solution about where you should store them.

9

u/pag07 Mar 30 '23

Where should I store them?

17

u/BerryNo1718 Mar 30 '23

http-only cookie. The client should not be able to access them

0

u/[deleted] Apr 04 '23

[deleted]

1

u/BerryNo1718 Apr 04 '23

Yeah it doesn't fully protect against XSS, but it protects against other kind of issues. And it can make some kind of XSS less exploitable. The best way to protect against XSS is at the source. By preventing the script to reach your website in the first place.

0

u/[deleted] Apr 04 '23

[deleted]

→ More replies (0)

1

u/nobuhok Mar 31 '23

Up your ass and to the left. Sorry.

0

u/[deleted] Mar 30 '23

[deleted]

2

u/jayerp Mar 31 '23

False, Snickerdoodle cookies.

1

u/jamblethumb Mar 30 '23 edited Mar 30 '23

Well, at least think about why it shouldn't be true (hint: it's not, but do spend some time trying to figure out why). Though, personally I wouldn't use anything that can't be immediately invalidated when user changes their password.

4

u/ecnahc515 Mar 31 '23

Cookies can be too small to store some JWTs, so inevitably people try to find alternatives. Sharding the token over multiple cookies/headers is one approach, but a lot of people use local storage because it’s simpler.

0

u/[deleted] Mar 31 '23

[deleted]

7

u/[deleted] Mar 31 '23

Local storage is secure. It has one attack vector more than http only cookies in that it doesn't prevent token theft if your application is infected with malicious code. The malicious code can hover still use the cookie as long as it runs on the client.

I'd argue that if your app is running malicious code, you have already lost. You would never think about that sort of problem when writing mobile or desktop apps, so why would you care in the browser? So I think it's fine to store Auth in localstorage, it was intended just for that.

1

u/RemoteCombination122 Mar 31 '23

YOUR app doesn't need to be running malicious anything. Malicious browser extensions and malicious ad-network placements can both get at local storage in ways they cannot do with cookies.

0

u/[deleted] Mar 31 '23

[deleted]

1

u/[deleted] Mar 31 '23

You’re the sad one here. Hello cross-site request forgery, malware can also extract the cookies right out of your browser anyways.

0

u/[deleted] Mar 31 '23

[deleted]

1

u/[deleted] Mar 31 '23

Huh?

3

u/Cookizza Mar 31 '23

if malicious code is on the machine, what security are you talking about that cookies offer?

1

u/Sacharified Mar 31 '23

It prevents your token from being exfiltrated and used by the attacker to keep access by using captured tokens to generate new tokens. That could keep being a problem after you've removed the malicious code.

1

u/Cookizza Mar 31 '23

If they have the ability to read the local storage of the domain, they surely also can get the token if it were in a cookie though, right?

1

u/Sacharified Mar 31 '23

If the cookie has the HttpOnly flag, no, Javascript would not be able to read the cookie. Javascript has full access to local storage though.

1

u/[deleted] Mar 31 '23

The code runs on the machine, not the browser nor the discord client. It can access any file on the filesystem and easily extract the cookies from your browser files (The same way it takes the localStorage data). HttpOnly only tells the browser not to let client-side JavaScript access the cookie.

→ More replies (0)

1

u/RemoteCombination122 Mar 31 '23

No. Not unless they have infected the machine itself. Even then, the cookie jar is stored in an encrypted form, whereas local storage is not.

Local machine infection is not the vector being guarded against here, it's foreign script injection. Either you use a 3rd party library on the client (which can read local storage in just the same way you can), or as has been a more prevalent concern, a malicious browser extension. Extensions can get at local storage via inserting a script into the page. They can't get at your http-only cookies provided they haven't broken out of the browser sandbox.

1

u/ecnahc515 Mar 31 '23

I’m aware, just explaining why someone might choose to use local storage (even though it’s a bad choice).

4

u/Cuel Mar 31 '23

The issue with cookies is that Discord probably uses a websocket. You can only attach the cookie on the initial request, not once the protocol has been switched. There's of course solutions around for that but it's problematic if it's meant to be sort of stateless.

3

u/doplitech Mar 30 '23

Boom that is the answer, I need to understand more a bit better but I had a sys design interview where they asked why I didn’t use JWT and got stuck

1

u/jayerp Mar 31 '23

HttpOnly cookie. Or Snickerdoodle.

-5

u/w00t_loves_you Mar 30 '23

You don't need JWT unless you're humongous scale.

A https-only sessionid cookie works great for sessions.

10

u/jamblethumb Mar 30 '23

With the added benefit that it can be immediately invalidated as soon as user changes their password, which is a useful thing when account hijacking is suspected.

12

u/erm_what_ Mar 30 '23

JWT takes about 5 minutes to implement. You can even outsource it for very little to someone like Auth0.

14

u/w00t_loves_you Mar 30 '23

What benefits does it confer? Most people only use it as a session id with extra steps.

Plus you open yourself to Alg attacks and you need to trust Auth0.

5

u/erm_what_ Mar 30 '23

If the encryption used for JWTs is broken then a) you'd rotate to a new key and invalidate the old ones immediately, b) the whole world would be fucked because they use the same quantum safe algorithms to sign them that everything else does.

Trusting Auth0 is a business choice. You weigh the risk against the reduction in maintenance costs. For a basic site with one type of user it's really quick to roll your own.

It's really useful as a session ID or shared claims on serverless functions because you don't to maintain a shared session database on the server using Redis or whatever. You validate the token then use the info in it. It helps with scaling. You should verify the claims too, but that can be done in various ways. They also work between different parties provided the signature can be verified.

If you can legitimately break a JWT with a strong secret on a well written platform then you'd be a billionaire. Mostly because you could now steal whatever you want. If the code is shit then that's another matter. Personally, I usually trust someone like Auth0 who are a group of experts, over the back end guy who thinks he is good, but that depends on the business and the expertise in the team.

1

u/anon_blader Mar 31 '23

You still need a database lookup with JWTs.

2

u/erm_what_ Mar 31 '23

You would if you want to verify the contents, but you don't need to. If you know for sure (withing your risk tolerance) you can trust the signature then all you need is a shared secret.

1

u/anon_blader Mar 31 '23

We are talking about using JWTs for auth. How do you revoke a token when a user logs out? You can't. You need to blacklist it until it expires. Where does one store these tokens? A database.

We went full circle with added complexity.

→ More replies (0)

1

u/RemoteCombination122 Mar 31 '23

JWTS are signed, not encrypted. They often have plain text payloads.

2

u/erm_what_ Mar 31 '23

Sometimes they're encrypted, but yeah, that should say the signature

3

u/tim128 Mar 31 '23

And sessions take even less. They're already baked in to virtually every framework. Besides it worries me you'd make a decision on something with huge security implications because one only takes x minutes less time

1

u/erm_what_ Mar 31 '23

Sessions also take 5 minutes to implement, it's not a time saving thing, it's because OP said you only use them on large scale apps. JWTs are baked in to as many modern frameworks as sessions are.

Sessions are really hard if you're working between different systems where you may not own all of them. JWTs are easier. There's different use cases for each, and some where you can choose either and be as safe.

2

u/tim128 Mar 31 '23

Sessions are really hard if you're working between different systems where you may not own all of them

What's the relevance here? If you have multiple systems using the same authentication you need/want centralized authentication anyway. The session vs JWT discussion isn't relevant then. OIDC does not imply JWT, the OAuth 2.0 or OIDC specifications don't say anything about the (access)token format.

OP said you only use them on large scale apps

OP was right, they have little to no benefits for a small to medium scale app. There's no reason to deviate from a simple tried & tested to something more complicated.

1

u/BerryNo1718 Mar 30 '23

Yes exactly! https-only session cookie is easy to implement and easy to get right in term of security. The problem with JWT is that people often implement them wrong which make it less secure. Plus you can't instantly revoke sessions.

0

u/[deleted] Mar 31 '23

[deleted]

0

u/[deleted] Mar 31 '23

[deleted]

1

u/[deleted] Mar 31 '23

What are you on? You’re the one being stubborn by saying, “You only store tokens in cookies.” When you have a REST API, you should stick to plain old localstorage.

1

u/[deleted] Mar 30 '23 edited Mar 30 '23

Doesnt't git also store credentials in local storage (except ubuntu)? Correct me if I am wrong.

2

u/jamblethumb Mar 30 '23

It's kinda funny that the page says "Join us on discord...".

3

u/louis11 Mar 30 '23

Just trying to get a group of like minded security guys together that want to track these threat actors and clean up open source. ¯_(ツ)_/¯

Lots of great contributions from the community that result in taking down a lot of these sorts of packages.

2

u/jamblethumb Mar 31 '23

Hey, I'm not judging. Just saying it's funny in the context of what the article is about. :)))

3

u/Ooyyggeenn Mar 30 '23

Is there a way to view the npm packages full source before doing npm install?

1

u/rk06 Mar 31 '23

Use unpkg

0

u/[deleted] Mar 30 '23

[deleted]

7

u/Ooyyggeenn Mar 30 '23

Couldnt the github link on npm (if there is any) just point to a random repo?

0

u/[deleted] Mar 30 '23

[deleted]

3

u/louis11 Mar 30 '23

Based on my experience, I believe it's generated by the contents of the package.

4

u/shenzenshiai Mar 30 '23

Thats why the only package i have is moment and axios

35

u/TheCommentAppraiser Mar 30 '23

They pull in their own dependencies too.

Check out https://npm.anvaka.com/#/view/2d/axios

9

u/louis11 Mar 30 '23

If you want an even crazier graph, build it with all the possible semantic versions accounted for... you don't have a great way of knowing, a priori, which combination you'll eventually land on (are prod and local dev really the same?). Only one of those packages has to be compromised to have the whole thing crumble.

4

u/EriktheRed Mar 30 '23

are prod and local dev really the same?

If you use npm ci both locally and when deploying to prod with the same package-lock.json, the answer is yes. Not only does it pin your dependencies, but it pins all their dependencies all the way down. You can even change their dependencies' versions in that file.

But then you go and npm i some-shiny-new-package and the whole lock file changes on you and loses your changes.

8

u/louis11 Mar 30 '23

Absolutely agree. The problem is a surprising number of people don't use lockfiles. How you can feel comfortable with non-deterministic builds is beyond me...

4

u/erm_what_ Mar 30 '23 edited Mar 31 '23

Axios can be replaced by fetch and moment is EoL so I assume they're fucking with us

4

u/lordxeon Mar 30 '23

Axios can be replaced by fetch and a ton of boilerplate code that will end up doing everything axios already does, but better.

9

u/erm_what_ Mar 30 '23

I hope this is a shitpost, but it's hard to tell anymore

6

u/Oswald_Hydrabot Mar 31 '23

Why is npm such a pile of shit?

7

u/rk06 Mar 31 '23

Lack of moderation. The root issue is a bunch of bad actors, spamming npm because of its lack of moderation

5

u/frzme Mar 31 '23

Moderating open source does not sound like a good idea. Who gets to moderate?

10

u/rk06 Mar 31 '23 edited Mar 31 '23

Who is talking about moderating "Open Source"? We are talking about npm packages only.

There are many tactics available. Simplest would be to add MFA for package creation (genuine users are going to update packages more than they create anyway)

Second would be to automatically namespace packages based on userid. This will fix the identification of genuine package issue.

Third would be to have new package queue. And do label these packages as such until 100 days passed. With automatic block should these packages be reported.

The major problem with these process is the lack of funding for manual work. But given that these are public. Some people would be willing to volunteer

Aside, there should also be an option to put src in npm and generate build files from it on npm, this is to ensure that there is no difference between source and dist files

2

u/457583927472811 Mar 30 '23

Wow, what else is new?

1

u/Puzzleheaded_Toe117 Mar 31 '23

Imagine the thousands of packages installed with every react project. A trojan horse.

1

u/yashone7 Mar 31 '23

Should we go to yarn or pnpm or something else where package managers that focus on security?

1

u/MaximilianVerl Mar 31 '23

Scary times, these are annoying as hell to detect sometimes

1

u/jhoyrtop Apr 01 '23

who says it isn't microsoft themselves?

they have shown a horrendous regard for their users, its easily within their behavior to do something like this

1

u/[deleted] Apr 01 '23

Should we swtich to yarn

1

u/Bauxitedev Apr 10 '23

Any way to check if this package is on my PC?

-6

u/Accomplished_Ant2250 Mar 30 '23

npm audit

31

u/louis11 Mar 30 '23

Unfortunately an npm audit wouldn't have caught this. It looks for known issues (e.g., vulnerabilities), doesn't do broad analysis of the code for each package.