Sub topic 12
If You Already Installed a Suspicious Package
This chapter assumes you know how pip and npm install packages, how lockfiles work, and why install scripts are a risk. It covers what to do after a package you did not fully vet has been installed. Acting in the right order matters, because the package may already have run code on your machine.
Decide how far the code could have run
First, work out what the package could have done. The answer depends on how it was installed.
- npm with scripts enabled:
preinstall,install, andpostinstallscripts run duringnpm install. Assume the code ran as soon as the install finished. - npm with
--ignore-scripts: Install-time code did not run. Code can still run when you import orrequirethe package, so check whether anything loaded it. - pip installing a source distribution: Building it can run the package's
setup.py, so code may have run at install time. - pip installing a wheel: Install does not run package code. Code runs when you import the package.
Note three facts: which machine, which user account, and whether the code was imported or executed afterward. A throwaway container is a smaller problem than a laptop holding cloud credentials.
Contain first
Containment limits further damage and comes before cleanup.
- Stop running processes that use the package, including dev servers, watchers, and CI jobs.
- If this happened in CI, cancel the run and pause the pipeline.
- Do not delete the package yet. You may need it as evidence.
- Copy the package files and any lockfile changes somewhere safe for later analysis.
Look at what the package is
Inspect the installed package without running it. For Python:
pip show -f suspicious-package
pip download suspicious-package --no-deps --no-binary :all: -d ./inspectFor npm:
npm view suspicious-package time
npm pack suspicious-package
tar -tzf suspicious-package-*.tgzRead the install scripts, setup.py, and any minified or encoded code. Warning signs include network calls to unfamiliar hosts, reads of environment variables or files such as ~/.ssh and ~/.aws, and base64 blobs that get decoded and executed.
Also check how the name got into your project. Look at the commit or the assistant's output. If the name came from a model, the package probably did not exist before an attacker registered it. A creation date from days or weeks ago supports that.
Assume secrets were exposed
If the install scripts ran, treat every secret reachable from that account as exposed. Rotate them. Do not wait for proof of theft.
| Secret | Action |
|---|---|
| Cloud keys (AWS, GCP, Azure) | Revoke and reissue. Review audit logs for use you do not recognize. |
| SSH keys | Generate new keys and remove the old public keys from servers and Git hosts. |
| Registry tokens (npm, PyPI) | Revoke and reissue. Check for unexpected package publishes. |
| Git host tokens | Revoke. Review recent commits, deploy keys, and workflow changes. |
| `.env` values and API keys | Rotate each one at the issuing service. |
| Browser sessions | Sign out everywhere and change important passwords from a clean device. |
Rotate from a different, trusted machine. A compromised machine can capture the new secrets as you create them.
Check for persistence and spread
Malicious packages often try to stay on the machine. Look for:
- New cron jobs, systemd units, launch agents, or shell profile edits such as changes to
.bashrcor.zshrc. - Unfamiliar processes and outbound connections.
- Modified files in your repository, especially CI configuration and other dependency files.
- Packages you published recently. A stolen registry token can be used to push malicious updates under your name.
If the machine held production access or you find signs of persistence, the safest fix is to wipe and rebuild it, then restore only trusted data.
Remove the package and clean up
After containment and evidence capture:
pip uninstall suspicious-package
npm uninstall suspicious-packageThen rebuild the environment instead of trusting the cleaned one. Delete the virtual environment or node_modules, remove the package from the requirements file or package.json, regenerate the lockfile, and reinstall from the verified list. Use hash pinning and npm ci --ignore-scripts as covered earlier.
Report and record
- Report the package to the registry. PyPI and npm both accept malware reports, and removal protects other developers.
- Tell your security team or team lead, even if you found no damage.
- Write down the timeline: when the install happened, what ran, what you rotated, and how the name was chosen.
- Add the fix to your process. A verification step before install, such as checking the package's age and release history, would have caught this.
Exercise
A teammate ran npm install on a package an assistant suggested. It turns out the package was registered three days ago and has a postinstall script that makes a network request. List the first three actions you would take, in order, and say why rotating secrets cannot wait for proof of theft.
Show answer
First, stop processes that use the package and isolate the machine from the network. Second, copy the package files and lockfile changes as evidence before deleting anything. Third, rotate every secret reachable from that account, using a different trusted machine. The postinstall script ran at install time, so the code had access to environment variables and files such as SSH and cloud credentials. You cannot reliably prove from the machine that nothing was copied, and a stolen secret can be used before you finish investigating, so rotation must not wait.