›bobbyencoded_
/posts/newsletter/courses/about
/posts/newsletter/courses/about

The Decode

The week's AI & software news, decoded.

Subscribe →

// © 2026 BobbyEncoded Inc. — A Canadian corporation

← Back to Blog

You Never Imported It. It Ran Anyway.

November 19, 2026 · 9 min read

Security, Python, PyPI, Supply Chain, AI Agents, Developer Tools

On Monday evening, November 17, someone published two new versions of agent-framework, the PyPI package for Microsoft's Agent Framework. Versions 1.26.1 and 1.26.2 went up 33 minutes apart, and both carried a credential stealer. PyPI pulled them within the hour. That's a short window, but the package gets about 190,000 downloads a month, and the people who install an agent framework tend to have cloud keys lying around.

The part I want to dig into isn't who did it, which nobody has said yet. It's how the code ran, because it ran on machines where nobody ever wrote import agent_framework.

What happened, in order

Per GitHub's advisory (GHSA-764m-3ppc-5m73) and the write-ups from Elastic, Orca and others, all times UTC on November 17:

TimeEvent
20:35agent-framework 1.26.1 published (a wheel only, no source distribution)
20:41Elastic's supply-chain monitor flags it as malicious
21:081.26.2 published, same stealer under new file names
~21:20Both versions gone from PyPI

1.26.0 is the last clean release. The sibling packages, such as agent-framework-core, weren't affected, according to Orca. Neither bad release changed the framework's code or added a new dependency. Everything happened in the packaging.

How the attacker got publishing access is still unknown. Microsoft told The Hacker News it had "made some repositories private while we investigate malicious content", including the Agent Framework repo. OX Security suggested the attacker hijacked the wheel build, but that's their theory, not a finding. It's also not Microsoft's first PyPI incident this year: three malicious versions of durabletask went up in May.

What it took

The two versions differed slightly, but together they collected:

  • Environment variables whose names look like secrets (anything containing TOKEN, SECRET, KEY, AWS, AZURE, GITHUB, OPENAI and so on), from its own process and, on Linux, from up to 300 other processes via /proc/*/environ.
  • Fresh Azure managed-identity tokens, requested from the instance metadata endpoint for 12 Azure resources. It didn't just copy stored secrets. On a cloud VM or a CI runner with an identity attached, it asked for new ones.
  • Azure DevOps pipeline tokens, and, in 1.26.2, GitHub Actions OIDC tokens from any workflow with id-token: write.
  • Credential files: Azure CLI state, .npmrc, pip.conf, .netrc, .git-credentials, Docker's config.json, Hugging Face tokens and .env files.

All of it was compressed, base64-encoded and posted to a webhook.site URL.

Why "I never imported it" doesn't help

When Python starts, before it runs a single line of your code, the site module scans every site-packages directory for files ending in .pth. These files exist for an ordinary reason: each line names a directory to add to sys.path. Editable installs and namespace packages depend on them.

But there's one more rule, and it has been there for decades: a line that starts with import (followed by a space or tab) isn't treated as a path. Python executes it.

That was the whole trick. Version 1.26.1 dropped agent_framework_meta.pth into site-packages. Version 1.26.2 used af_runtime_diag.pth plus a new top-level module, af_runtime_diag.py. It also restored the legitimate __init__.py byte for byte, so a diff of the framework's own code showed nothing. The hook forked a detached background process and swallowed errors, so the Python command you actually ran behaved normally.

So once the bad version was installed, any Python run in that environment ran the stealer: pytest, pip list, a linter, a one-line script that has nothing to do with agents. On a CI runner, that's every step that touches Python.

Legitimate packages use the same mechanism, which is why it's hard to just ban. setuptools ships distutils-precedence.pth and virtualenv ships _virtualenv.pth. Both are executable lines, and both are fine. What you want is to know which ones you have.

A small scanner

This lists every .pth line that Python will execute at startup. It doesn't judge them. It just makes them visible so a new one stands out. Python first:

"""List .pth files whose lines Python will execute at startup."""
import site
import sys
from pathlib import Path


def site_dirs() -> list[Path]:
    dirs = list(site.getsitepackages())
    if site.ENABLE_USER_SITE:
        dirs.append(site.getusersitepackages())
    return [Path(d) for d in dirs if Path(d).is_dir()]


def executable_lines(pth: Path) -> list[str]:
    # site.py runs a line with exec() if it starts with "import " or "import\t".
    # Every other non-comment line is just added to sys.path.
    text = pth.read_text(encoding="utf-8", errors="replace")
    return [ln.strip() for ln in text.splitlines()
            if ln.startswith(("import ", "import\t"))]


def main() -> None:
    dirs = [Path(a) for a in sys.argv[1:]] or site_dirs()
    found = 0
    for d in dirs:
        for pth in sorted(d.glob("*.pth")):
            for line in executable_lines(pth):
                found += 1
                print(f"{pth.name}: {line[:70]}")
    print(f"{found} executable line(s) in {len(dirs)} director(ies)")


if __name__ == "__main__":
    main()

With no arguments it scans the current interpreter's own site-packages, so run it with the Python from the environment you care about. Run it with -I (isolated mode) so it doesn't pick up the current directory or environment variables. Note that -I doesn't skip .pth processing, so in a compromised environment even the scanner's own start would trigger the hook. For a suspect environment, point it (or the C# version) at the directory from a different interpreter instead.

The same thing in C#, as a .NET 10 file-based app. It takes the directories as arguments, so it never has to start Python at all:

// List .pth files whose lines Python will execute at startup.
// Usage: dotnet run PthScan.cs -- <site-packages dir> [more dirs]

var dirs = args.Where(Directory.Exists).ToList();
var found = 0;

foreach (var dir in dirs)
{
    foreach (var pth in Directory.GetFiles(dir, "*.pth").Order())
    {
        foreach (var line in File.ReadLines(pth))
        {
            // Python's site.py exec()s a line that starts with "import " or "import\t".
            if (!line.StartsWith("import ") && !line.StartsWith("import\t")) continue;
            found++;
            var shown = line.Trim();
            Console.WriteLine($"{Path.GetFileName(pth)}: {shown[..Math.Min(70, shown.Length)]}");
        }
    }
}

Console.WriteLine($"{found} executable line(s) in {dirs.Count} director(ies)");

To test both, I made a folder with the real setuptools and virtualenv .pth files copied from two of my virtual environments, one plain path-only .pth, and a harmless stand-in for a hook (demo_startup_hook.pth, which imports a module that doesn't exist). Both versions print the same thing:

$ python3 -I pth_scan.py demo-site-packages
_virtualenv.pth: import _virtualenv
demo_startup_hook.pth: import demo_startup_hook
distutils-precedence.pth: import os; var = 'SETUPTOOLS_USE_DISTUTILS'; enabled = os.environ.get(
3 executable line(s) in 1 director(ies)

$ dotnet run PthScan.cs -- demo-site-packages
_virtualenv.pth: import _virtualenv
demo_startup_hook.pth: import demo_startup_hook
distutils-precedence.pth: import os; var = 'SETUPTOOLS_USE_DISTUTILS'; enabled = os.environ.get(
3 executable line(s) in 1 director(ies)

The path-only file is correctly ignored. Two of the three hits are normal, and the third is the kind of line you'd want to explain. Save the output for a known-good environment, and a new import line in a later run is worth a look. In a CI image build, that diff is a cheap check.

What to do

If you might have installed 1.26.1 or 1.26.2 (check lockfiles, container images, CI caches and pip show agent-framework in each environment), the advisory's advice is blunt: treat that environment as exposed and rotate every credential it could reach. That covers cloud service principals, CI tokens, GitHub tokens, package-registry tokens and anything in a .env file. Rotate from a clean machine, then check your cloud sign-in logs and CI runs from November 17 onward. The exposure window was roughly 20:35 to 21:20 UTC that day.

Generally:

  1. Pin exact versions with hashes. A loose pin like agent-framework>=1.26 is how a 33-minute window reaches you. pip install --require-hashes -r requirements.txt refuses anything that doesn't match, and uv pip compile --generate-hashes writes the hashes for you. A locked hash also means a re-uploaded version can't quietly replace the one you tested.
  2. Wait before installing brand-new releases. Elastic caught this in six minutes, and PyPI removed it within the hour. A rule like "nothing published in the last few days" would have skipped it entirely. Both installers can do this now: uv pip install --exclude-newer "3 days" ... (or exclude-newer in your uv config), and in pip, pip install --uploaded-prior-to P3D .... Both compare against each file's upload time on the index.
  3. Keep secrets out of the environment Python runs in. The stealer went for environment variables and dotfiles because that's where secrets usually live. Short-lived tokens limit the damage, and so do CI steps that only get id-token: write when they deploy.
  4. Use Trusted Publishing for your own packages, but know its limits. As I wrote about npm's version of it, provenance tells you which pipeline published a release, not that the pipeline was clean. If this attack really came through the build, a valid attestation could have been attached to a malicious wheel.

The uncomfortable lesson is the same one the npm story taught: the attack didn't break anything. It used a feature, working as designed, that most of us had forgotten exists.

Sources

  • GitHub Advisory Database, Malicious code in agent-framework (PyPI), GHSA-764m-3ppc-5m73 (published Nov 18, 2026)
  • Elastic Security Labs, We caught the agent-framework PyPI compromise six minutes after it published (Nov 2026)
  • Orca Security, Microsoft Agent Framework PyPI supply-chain attack: versions 1.26.1 and 1.26.2 steal cloud credentials (Nov 18, 2026)
  • The Hacker News, Attackers compromise Microsoft's Agent Framework PyPI package to steal cloud credentials (Nov 2026)
  • JFrog Security Research, Microsoft's agent-framework hijacked on PyPI via silent .pth startup hook
  • Socket, Microsoft's agent-framework PyPI package compromised with credential stealer
  • Python documentation, site: site-specific configuration hook (the .pth rules)
← Previous

Three Labs, Ten Days: A Builder's Guide to November's Model Wave

Next →

Android Developer Verification, What an Indie Dev Must Do Now

Thoughts on this?

Tell me on X: @bobbyencoded · Share this post on X

Want more like this? Subscribe to The Decode, a weekly email on the AI and developer news worth your time.