samuelsimko/honeypot_llm_defense

Python

2

14 commits

updated Apr 8, 2026

See the code

README

==================================================
HONEYPOT LLM DEFENSE
==================================================

A framework for studying, generating, and defending against *honeypot attacks* and *alignment-breaking behaviors* in large language models (LLMs).  
The repository provides utilities for **data generation**, **soft prompt optimization**, **alignment fine-tuning**, and **automated harmfulness evaluation** using judge models.

--------------------------------------------------
πŸ“ Repository Structure
--------------------------------------------------

::

  honeypot_llm_defense/
  β”œβ”€β”€ attacks/
  β”‚   β”œβ”€β”€ behavior_targets/
  β”‚   β”‚   └── augment.py         # Roleplay & stylistic augmentations for harmful task prompts
  β”‚   └── embedding/
  β”‚       └── embedding_attack.py  # Main soft prompt optimization & attack script
  β”œβ”€β”€ data/
  β”‚   β”œβ”€β”€ harmbench_behaviors_text_*.csv  # HarmBench behavior datasets
  β”‚   └── harmbench_targets_*.json        # Target outputs for optimization
  β”œβ”€β”€ defenses/
  β”‚   β”œβ”€β”€ honeypot_hinge.py               # Honeypot hinge loss training
  β”‚   β”œβ”€β”€ honeypot_simple.py              # Simple honeypot alignment defense
  β”‚   └── train_*.py                      # Finetuning utilities
  β”œβ”€β”€ defended_model_sft*/                # Saved supervised fine-tuned models (LoRA adapters)
  β”œβ”€β”€ generation/                         # Data generation & category utilities
  β”œβ”€β”€ judges/
  β”‚   β”œβ”€β”€ strong_reject/                  # StrongReject classifier for model refusals
  β”‚   β”œβ”€β”€ harmbench_judge.py              # HarmBench Llama-2 13B classifier interface
  β”‚   └── embedding_attack.py             # Alternate evaluation integration
  β”œβ”€β”€ scripts/
  β”‚   β”œβ”€β”€ finetune_oss.sh                 # Finetune script for GPT-OSS / LLaMA models
  β”‚   β”œβ”€β”€ run_embedding_attack.sh         # Runs soft prompt optimization benchmark
  β”‚   β”œβ”€β”€ run_honeypot_hinge.sh           # Trains the honeypot hinge defense
  β”‚   β”œβ”€β”€ run_honeypot_simple.sh          # Trains the simple honeypot model
  β”‚   β”œβ”€β”€ test_model.py                   # Quick model interaction test script
  β”‚   └── train_new.sh                    # New defense training helper
  β”œβ”€β”€ results/                            # Attack & evaluation logs
  β”œβ”€β”€ eval.py                             # Model evaluation helper script
  β”œβ”€β”€ defense_finetune.py                 # Core alignment fine-tuning pipeline
  β”œβ”€β”€ tamper.py                           # Analysis / tampering utilities
  β”œβ”€β”€ outputs.jsonl / outputs.csv         # Cached experiment outputs
  └── requirements.txt                    # Python environment dependencies


--------------------------------------------------
βš™οΈ Environment Setup
--------------------------------------------------

**Requirements**
- Python 3.10
- CUDA 12.x compatible GPU

Install dependencies:

.. code-block:: bash

   git clone --recursivee_submodules https://github.com/samuelsimko/honeypot_llm_defense.git
   cd honeypot_llm_defense
   python3 -m venv venv
   source venv/bin/activate
   pip install -r requirements.txt

--------------------------------------------------
🧩 Key Components
--------------------------------------------------

**1️⃣ Honeypot Defenses**

- ``defenses/honeypot_hinge.py``  
  Trains a model to recognize and neutralize honeypot triggers through a hinge-style loss function.

- ``defenses/honeypot_simple.py``  
  Lightweight defense model for baseline comparisons.

Run training:

.. code-block:: bash

   source scripts/run_honeypot_hinge.sh
   # or
   source scripts/run_honeypot_simple.sh


**2️⃣ Soft Prompt Optimization (Attacks)**

Implements gradient-based embedding attacks to find adversarial prefix embeddings that make a model produce a target harmful output.

Main script:
``attacks/embedding/embedding_attack.py``

Run via:

.. code-block:: bash

   source scripts/run_embedding_attack.sh

The attack will:
 - Load a base model (e.g., Meta-Llama-3-8B, GPT-OSS-20B)
 - Optimize a soft prompt embedding for each harmful task
 - Evaluate outputs with:
    - **HarmBench judge** (`cais/HarmBench-Llama-2-13b-cls`)
    - **StrongReject** refusal classifier
 - Save variant-by-variant results to `results/`


**3️⃣ Data Augmentation**

Augment prompt–target pairs for robustness testing using:
``attacks/behavior_targets/augment.py``

The augmentation layer adds:
 - Roleplay or persona modifiers
 - Random capitalization / leetspeak noise
 - Side questions or headers
 - Fake β€œgodmode” jailbreak patterns

Used automatically by `embedding_attack.py` during each optimization run.

**4️⃣ Evaluation Utilities**

Run quick response tests:

.. code-block:: bash

   python scripts/test_model.py --model-path defended_model_sft

Not written in Markdown, so it's shown here as plain text β€” view it formatted on GitHub.

samuelsimko/honeypot_llm_defense

Python

2

14 commits

updated Apr 8, 2026

See the code

README

==================================================
HONEYPOT LLM DEFENSE
==================================================

A framework for studying, generating, and defending against *honeypot attacks* and *alignment-breaking behaviors* in large language models (LLMs).  
The repository provides utilities for **data generation**, **soft prompt optimization**, **alignment fine-tuning**, and **automated harmfulness evaluation** using judge models.

--------------------------------------------------
πŸ“ Repository Structure
--------------------------------------------------

::

  honeypot_llm_defense/
  β”œβ”€β”€ attacks/
  β”‚   β”œβ”€β”€ behavior_targets/
  β”‚   β”‚   └── augment.py         # Roleplay & stylistic augmentations for harmful task prompts
  β”‚   └── embedding/
  β”‚       └── embedding_attack.py  # Main soft prompt optimization & attack script
  β”œβ”€β”€ data/
  β”‚   β”œβ”€β”€ harmbench_behaviors_text_*.csv  # HarmBench behavior datasets
  β”‚   └── harmbench_targets_*.json        # Target outputs for optimization
  β”œβ”€β”€ defenses/
  β”‚   β”œβ”€β”€ honeypot_hinge.py               # Honeypot hinge loss training
  β”‚   β”œβ”€β”€ honeypot_simple.py              # Simple honeypot alignment defense
  β”‚   └── train_*.py                      # Finetuning utilities
  β”œβ”€β”€ defended_model_sft*/                # Saved supervised fine-tuned models (LoRA adapters)
  β”œβ”€β”€ generation/                         # Data generation & category utilities
  β”œβ”€β”€ judges/
  β”‚   β”œβ”€β”€ strong_reject/                  # StrongReject classifier for model refusals
  β”‚   β”œβ”€β”€ harmbench_judge.py              # HarmBench Llama-2 13B classifier interface
  β”‚   └── embedding_attack.py             # Alternate evaluation integration
  β”œβ”€β”€ scripts/
  β”‚   β”œβ”€β”€ finetune_oss.sh                 # Finetune script for GPT-OSS / LLaMA models
  β”‚   β”œβ”€β”€ run_embedding_attack.sh         # Runs soft prompt optimization benchmark
  β”‚   β”œβ”€β”€ run_honeypot_hinge.sh           # Trains the honeypot hinge defense
  β”‚   β”œβ”€β”€ run_honeypot_simple.sh          # Trains the simple honeypot model
  β”‚   β”œβ”€β”€ test_model.py                   # Quick model interaction test script
  β”‚   └── train_new.sh                    # New defense training helper
  β”œβ”€β”€ results/                            # Attack & evaluation logs
  β”œβ”€β”€ eval.py                             # Model evaluation helper script
  β”œβ”€β”€ defense_finetune.py                 # Core alignment fine-tuning pipeline
  β”œβ”€β”€ tamper.py                           # Analysis / tampering utilities
  β”œβ”€β”€ outputs.jsonl / outputs.csv         # Cached experiment outputs
  └── requirements.txt                    # Python environment dependencies


--------------------------------------------------
βš™οΈ Environment Setup
--------------------------------------------------

**Requirements**
- Python 3.10
- CUDA 12.x compatible GPU

Install dependencies:

.. code-block:: bash

   git clone --recursivee_submodules https://github.com/samuelsimko/honeypot_llm_defense.git
   cd honeypot_llm_defense
   python3 -m venv venv
   source venv/bin/activate
   pip install -r requirements.txt

--------------------------------------------------
🧩 Key Components
--------------------------------------------------

**1️⃣ Honeypot Defenses**

- ``defenses/honeypot_hinge.py``  
  Trains a model to recognize and neutralize honeypot triggers through a hinge-style loss function.

- ``defenses/honeypot_simple.py``  
  Lightweight defense model for baseline comparisons.

Run training:

.. code-block:: bash

   source scripts/run_honeypot_hinge.sh
   # or
   source scripts/run_honeypot_simple.sh


**2️⃣ Soft Prompt Optimization (Attacks)**

Implements gradient-based embedding attacks to find adversarial prefix embeddings that make a model produce a target harmful output.

Main script:
``attacks/embedding/embedding_attack.py``

Run via:

.. code-block:: bash

   source scripts/run_embedding_attack.sh

The attack will:
 - Load a base model (e.g., Meta-Llama-3-8B, GPT-OSS-20B)
 - Optimize a soft prompt embedding for each harmful task
 - Evaluate outputs with:
    - **HarmBench judge** (`cais/HarmBench-Llama-2-13b-cls`)
    - **StrongReject** refusal classifier
 - Save variant-by-variant results to `results/`


**3️⃣ Data Augmentation**

Augment prompt–target pairs for robustness testing using:
``attacks/behavior_targets/augment.py``

The augmentation layer adds:
 - Roleplay or persona modifiers
 - Random capitalization / leetspeak noise
 - Side questions or headers
 - Fake β€œgodmode” jailbreak patterns

Used automatically by `embedding_attack.py` during each optimization run.

**4️⃣ Evaluation Utilities**

Run quick response tests:

.. code-block:: bash

   python scripts/test_model.py --model-path defended_model_sft

Not written in Markdown, so it's shown here as plain text β€” view it formatted on GitHub.