How-Tos

How-To: Check Every Python Venv on a Box for a Bad Package Version

Leave a comment

When a package like Microsoft’s Agent Framework gets poisoned on PyPI, the first question is “do I have it anywhere?” If you run bots and scripts out of a pile of virtualenvs like I do, here’s a quick sweep.

1. Find every venv

find / -xdev -name pyvenv.cfg 2>/dev/null | xargs -r -n1 dirname > venvs.txt

2. Ask each one for the package and version

PKG=agent-framework
while read v; do
  ver=$("$v/bin/python" -m pip show "$PKG" 2>/dev/null | awk '/^Version:/{print $2}')
  [ -n "$ver" ] && echo "$v $PKG $ver"
done < venvs.txt

Don’t forget the system Python and per-user installs: run python3 -m pip show "$PKG" and python3 -m pip show --user "$PKG" too.

3. Look for unexpected .pth files

A .pth file in site-packages can run code on every Python start, which is how this one worked. List them and look at anything with an import line you don’t recognize:

while read v; do
  grep -l '^import' "$v"/lib/python3*/site-packages/*.pth 2>/dev/null
done < venvs.txt

4. If you get a hit

Don’t just uninstall it. Treat every secret that environment could see as stolen: rotate from a clean machine, rebuild the venv from a hash-pinned lockfile, and check your cloud logs for the window it was installed.

God Bless, and May the Source Be with You!

References & Further Reading

Subscribe (RSS) Follow How-Tos via RSS

Leave a note

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.