Cybercriminals have been performing typosquatting attacks for years, hoping that a typo in a browser’s address bar lands users on their phishing site. LLM hallucinations introduce similar security risks, such as the potential for software to import non-existent dependencies.
LLMs make mistakes and hallucinate facts all the time. When it comes to software dependencies, an LLM might suggest something that sounds right rather than is right. This presents an opportunity for attackers to slip malware into a codebase while posing as a legitimate, LLM-recommended library.
How It Works
LLMs are a useful tool for developers because they can help answer the exact question the developer has. Instead of wading through docs or search results, a developer can get a recommendation and sample code for a task within seconds.
The problem is that LLMs, by nature, are designed to write something that makes sense. Sometimes the right name for a package isn’t the most logical one, leading an LLM to hallucinate a non-existent but plausible package.
Attackers can take advantage of this by registering a package with that name themselves and inserting malicious code into it. This way, when an LLM suggests that package, it can be downloaded and installed without any errors.
Research has found that these hallucinations are often repeated within a specific LLM. A test of 500 prompts known to produce hallucinations across 10 runs found that 58% of hallucinated package names appeared more than once. 43% of hallucinated names appeared across all ten runs. However, the exact package names varied from one LLM to another.
Getting the Right Package
Notably, these hallucinated package names mostly weren’t typosquats, falling within a Levenshtein distance of 1-2 of a real package. Instead, they were plausible but incorrect names, so fuzzy search and typosquatting defenses would miss most of them.
For example, LLMs hallucinated huggingface-cli instead of huggingface_hub[cli] for the Hugging Face CLI Python module. After registering the hallucinated package name as an empty package on PyPI, Bar Lanyado of Lasso Security reported he received about 30k downloads within three months.
Instead, developers should learn to treat these recommendations as untrusted LLM output and independently verify it. Before downloading and installing a library, check the age, source, and maintainers to verify that it makes sense. Additionally, organizations can implement allowlists and internal registry mirrors to help reduce the risk that a developer could accidentally download and install a malicious package.
Educational content, not legal, financial, investment, or professional security advice. Terms
New notes, roughly monthly per shift
No paywall, no membership tier — just an email when something new is published. Pick which shifts you care about.
We'll email you a link to confirm — you're only subscribed once you click it. Unsubscribe anytime. Privacy Policy