Crafting AI Prompts Framework

INJ Prompt Injection

Package Injection

RISK: HIGH IMPACT: HIGH

When AI coding assistants hallucinate non-existent software libraries, hackers exploit the mistake. This article explains 'Package Hallucination' attacks, where adversaries register fake packages invented by LLMs to trick developers into installing malware.

AI Package Injection (Hallucination Attacks)

Developers increasingly rely on AI coding assistants (like Copilot, ChatGPT, or Cursor) to write boilerplate code and suggest libraries. A dangerous side effect of this is Package Hallucination.

Because Large Language Models (LLMs) predict text based on patterns rather than verified facts, they often invent plausible-sounding software packages that do not actually exist. Hackers exploit this by identifying these frequently hallucinated names and registering them on public repositories (like PyPI or NPM) with malicious code. When a developer copies the AI's suggestion, they unknowingly install malware.

How the Attack Works

  1. Prediction: Attackers prompt popular LLMs with thousands of coding questions to see which non-existent packages they consistently recommend (e.g., `argh-tools`, `fast-crypto-utils`).
  2. Registration: The attacker registers these names on official package managers (NPM, PyPI, RubyGems).
  3. Infection: A legitimate developer asks their AI assistant for help. The AI suggests the malicious package (believing it exists). The developer runs pip install or npm install.
  4. Execution: The package installs successfully (often including empty "dummy" code to avoid suspicion) but runs a background script to steal API keys, SSH credentials, or environment variables.

Typosquatting vs. Hallucination

While classic Typosquatting relies on the user making a typing error (e.g., typing `reqeusts` instead of `requests`), Package Injection relies on the AI making the error. The user trusts the AI's output, assumes the package is real/safe, and installs it without verification.

Risk Assessment

This threat is classified as risk:HIGH impact:HIGH. The risk is rising rapidly as reliance on AI coding tools becomes standard practice. Research has shown that heavily used models can hallucinate packages in up to 20% of coding answers.

The impact is critical because these packages typically execute code immediately upon installation (via `postinstall` scripts). This grants the attacker instant access to the developer's local environment, which often contains high-privilege secrets, production database keys, and cloud credentials. This turns a single developer's mistake into a full Supply Chain Attack.


The three phases

CRAFT

Craft (write) the prompt with the following elements: Context, Register, Acting Role, Format, and Task.

ING

Validate the prompt and ensure it maintains an interactive approach. Keep in mind the importance of non-disclosure and staying goal-driven throughout the process.

AI

Continuously assess and refine the output based on the prompts output to improve the overall quality.

Terms of Service

Before accessing the "Prompt Injections" examples on our website, please read and agree to the following terms of service:

  1. Educational Purpose Only: The "Prompt Injections" examples provided are intended solely for educational purposes. They are meant to help you understand how prompt injections work and how to defend yourself against them.
  2. No Misuse: You agree not to use the provided examples for any malicious or unethical activities. This includes, but is not limited to, using prompt injections to manipulate, deceive, or harm others.
  3. Responsible Use: By accessing these examples, you confirm that your intention is to learn about the risks associated with prompt injections and to enhance your ability to safeguard against them.
  4. Legal Compliance: You agree to comply with all applicable laws and regulations while using the information provided on this website.
  5. No Liability: We are not responsible for any misuse of the information provided on our website. Users are solely responsible for their actions and any consequences that may arise from the use of this information.

By clicking "I agree," you accept these terms for this documentation visit. "I agree & save" remembers your choice for future visits in this browser. If you decline, the examples remain hidden.