Two Seconds After npm install: Investigating a Malicious Install Script
The challenge
A Fleetboard npm install sent data out. Find the install script, decode where it posts. Name the host that got the env variables.
What you'll learn
- Explain when npm runs a dependency's install scripts
- Find which packages ran scripts, from npm's debug log or the lockfile
- Tell a legitimate install script from a malicious one
- Decode a reversed base64 string with shell tools
- Harden installs: ignore-scripts, allowlists, release cooldowns, secret rotation
Skills tested
Prerequisites
- Basic shell commands (ls, cat, grep)
- What npm and package.json are
How it works
A package.json can declare lifecycle scripts. When npm installs a dependency it runs that package's preinstall, install and postinstall scripts automatically, with the installing user's rights and environment. Native modules use this legitimately to build or load a binary; an attacker who can publish a version of a popular package uses it to run code on every developer laptop and CI runner that installs it.
npm records each script it runs in its debug log (info run <package> <event> <path> <command>), and lockfile v3 marks those packages with "hasInstallScript": true. Malicious scripts rarely show their destination in plain text: here the URL is stored reversed and base64-encoded, and only rebuilt at run time.
In September 2025 the Shai-Hulud worm spread through hundreds of npm packages this way: a bundle.js run from postinstall collected secrets and used the npm tokens it found to publish infected versions of the victims' own packages.
Common mistakes
- Answering huecraft.example. The URL in the bundle's banner comment is the library's homepage, the one plain URL in the file. The destination is the encoded string.
- Blaming bcrypt. Its
installscript,node-gyp-build, loads the prebuilt native module for the platform. That is what native packages are expected to do. - Answering registry.npmjs.org. Every
http fetchline in the npm log goes to the public registry: that is npm downloading packages, not the script. - Stopping after base64 -d. The decoded text reads backwards. The code calls
.reverse()on it, so pipe it throughrev. - Trying to run it. Never execute a suspect script to see what it does; this box has no node on purpose.
How to defend against it
- Install with
npm ci --ignore-scripts(orignore-scripts=truein.npmrc) and run the few scripts you need explicitly; pnpm 10 runs no dependency scripts unless they are allowlisted. - Do not auto-merge dependency bumps the hour a version appears: a cooldown (Renovate's
minimumReleaseAge, pnpm'sminimumReleaseAge) lets the registry pull a malicious release first. - Keep long-lived secrets out of developer shells and CI environments; use short-lived, scoped tokens.
- After an incident: block the host, remove the version, and rotate every secret that was in the environment (cloud keys, npm and GitHub tokens).