Detecting Malicious npm Packages

Dylan Reyes 9 min read

Malicious npm packages have changed shape over three years. The early ones were crude: a thin README, no repository link, a post-install script piping curl | bash from an attacker's URL. Those still get published and still succeed against organizations with no scanning, but they are not what teams with basic dependency hygiene have to worry about.

The packages that genuinely threaten security-conscious teams are built to survive the glance a developer gives a new dependency before installing it. Their malicious code is arranged to dodge simple pattern matching on install script content.

Knowing which signals separate these packages from honest ones means looking past keyword matches on preinstall and postinstall.

Install scripts, read properly

Post-install scripts remain the most direct route to code execution at install. Unless lifecycle scripts are disabled, npm runs whatever scripts.postinstall, scripts.preinstall, or scripts.install points to in package.json, without asking.

Grepping for curl or wget no longer covers it. Current malicious installers use patterns that frustrate static analysis:

  • URLs or commands assembled at runtime from string fragments: const url = "https://" + base64AtobCall + "/"
  • Use of the built-in child_process module to spawn processes from inside package code rather than a shell script
  • Execution gated on environment variables or system properties, so the hostile branch only wakes up in CI where secrets live
  • Modules loaded dynamically at runtime: require(eval(...)) patterns that resolve a module name from a string

Static analysis has to account for these. We parse the full abstract syntax tree of install script code rather than reading the shell command string, which catches obfuscated network calls, process spawning, and dynamic require. A package that reassembles a URL from concatenated fragments and hands it to https.get is weighted the same as one calling curl outright.

Who published it, and what changed

The publishing account is a weak proxy for legitimacy on its own. Read against other indicators, its history helps.

Some account patterns track with malicious publishes. Account age against first publish is one. Accounts created and used to publish within a short span show up disproportionately among reported malicious packages next to accounts with years of activity behind them. New accounts are not inherently hostile, but a package from one, carrying other risk signals, merits more scrutiny than a package from an account with five years of open-source work.

Ownership transfers weigh more. npm packages can change hands between maintainers. A package one person maintained for two years and whose owner changed recently sits in a different risk category from a stable long-term project. The npm registry API exposes transfer history, and our scans surface packages in a dependency tree where ownership changed in the past twelve months.

Email domain shifts in maintainer metadata follow the same logic. A registered address moving from a personal or organizational domain to free webmail can mark an account takeover, with the attacker editing contact details so the original owner cannot recover access.

Download curves that do not fit

Legitimate packages build download counts through word of mouth, blog posts, documentation links, and time. Their curves run roughly smooth, with bumps when something features them. Detected malicious packages often break from that baseline.

Sometimes the curve inverts: weeks near zero after publication, then a sudden spike. That can mean the package slipped into a dependency tree as a new transitive dependency of a popular package, drawing installs through the side door instead of developers choosing it.

Another shape is no history at all next to a recent publish date and loud README keywords. A package put up yesterday, billed as "the best React form library", with zero downloads, looks different from one with six months of steady growth.

Velocity alone is a weak signal. It strengthens when paired with a new account, an install script full of dynamic patterns, and no linked source repository.

Entropy and structure in the code

Entropy measures how compressible a file is. Normal JavaScript source carries low entropy because its vocabulary of keywords, identifiers, and patterns repeats. Base64 payloads, encrypted blobs, and compressed binaries embedded in source carry far higher entropy, since none of that repetition is present.

Hunting high-entropy strings in JavaScript source reliably finds embedded payloads. An array-utility library has no reason to contain a 500-character base64 string. When one turns up, it needs a look.

Entropy scans do produce false positives. Legitimate packages sometimes embed compiled WebAssembly or large lookup tables that read as high entropy. We apply the scan selectively: high-entropy strings in install scripts, or in modules install scripts load, carry more weight than the same readings in utility code never touched at install time.

What the metadata says

Metadata carries signals worth judging separately from the code. We check the following:

  • Repository field presence: Does the package point to source? A missing repository field proves nothing, but every high-download legitimate package links out. No link, high downloads: unusual.
  • Version publish frequency: Healthy fast-moving projects publish over time. 1.0.0, then 1.0.0-patch, then 1.0.0-patch2 inside a few hours is publish behavior that does not match normal development.
  • Package size against stated functionality: A "lightweight utility" shipping a 2MB tarball is worth opening. The gap between description and artifact is a structural anomaly.
  • License field anomalies: Not a security signal by itself, but a package claiming an open-source license while holding code that conflicts with it suggests the contents were assembled without care.

Signals weighed together

No single signal reliably separates malicious packages from legitimate ones at a tolerable false-positive rate. Each adds evidence, and the combination sets the risk profile.

A package with dynamic require patterns in its post-install script, a new publishing account, no source repository, and high-entropy strings in its install module is not proven malicious. The accumulated weight is enough to block the install for review.

A package with a post-install script but a maintainer holding four years of active history, a linked repository whose code matches, and ordinary download growth scores much lower even though it tripped the install script signal.

That layered approach differs from the "has an install script, flag it" rule some tools apply. Flagging every install script produces alert fatigue, since a substantial share of widely used legitimate packages run them for valid reasons like compiling native addons or generating type definitions. Scoring signals against one another aims at packages that warrant blocking or investigation, not packages that match one shallow heuristic.

Any detection-based method is limited by what we already know to look for. The complementary control is provenance verification, which asks whether what arrived matches what was published instead of whether the attack pattern has been recognized.

Back to Security Insights