Skip to content

Commit 1421ded

Browse files
[pentest] add prompt injection pentesting
1 parent 4d87487 commit 1421ded

File tree

15 files changed

+1718
-3
lines changed

15 files changed

+1718
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.venv
22
dist/
3+
__pycache__

poetry.lock

Lines changed: 454 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "zenguard"
3-
version = "0.1.2"
3+
version = "0.1.3"
44
description = "Plug-and-play production grade security for GenAI applications"
55
authors = ["ZenGuard Team <hello@zenguard.ai>"]
66
license = "MIT"
@@ -9,8 +9,14 @@ readme = "README.md"
99
[tool.poetry.dependencies]
1010
python = "^3.9"
1111
httpx = "^0.27.0"
12+
tqdm = "^4.66.2"
1213

1314

15+
[tool.poetry.group.pentest.dependencies]
16+
openai = "^1.14.2"
17+
rapidfuzz = "^3.7.0"
18+
pandas = "^2.2.1"
19+
1420
[build-system]
1521
requires = ["poetry-core"]
1622
build-backend = "poetry.core.masonry.api"

tests/pentest.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import os
2+
3+
from zenguard import Credentials, Detector, Endpoint, ZenGuard, ZenGuardConfig
4+
5+
if __name__ == "__main__":
6+
api_key = os.environ.get("ZEN_API_KEY")
7+
if not api_key:
8+
raise ValueError("ZEN_API_KEY is not set")
9+
config = ZenGuardConfig(credentials=Credentials(api_key=api_key))
10+
zenguard = ZenGuard(config=config)
11+
zenguard.pentest(endpoint=Endpoint.ZENGUARD, detector=Detector.PROMPT_INJECTION)

zenguard/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
from zenguard.zenguard import ZenGuard, ZenGuardConfig, Credentials, Detector
1+
from zenguard.zenguard import ZenGuard, ZenGuardConfig, Credentials, Detector, Endpoint

zenguard/pentest/__init__.py

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from zenguard.pentest.prompt_injections import config, prompt_data, prompting, run, scoring, visualization
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import hashlib
2+
import json
3+
4+
5+
def hash_str(string):
6+
return hashlib.md5(string.encode()).hexdigest()
7+
8+
9+
def hash_dict(d):
10+
return hash_str(json.dumps(d))
11+
12+
13+
class DeepDict(dict):
14+
def __missing__(self, key):
15+
value = self[key] = type(self)()
16+
return value
17+
18+
def __getitem__(self, key):
19+
values = dict.__getitem__(self, key)
20+
if isinstance(values, dict):
21+
values = DeepDict(values)
22+
if isinstance(values, list):
23+
for i, v in enumerate(values):
24+
if isinstance(v, dict):
25+
values[i] = DeepDict(v)
26+
return values
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from . import prompt_data
2+
3+
default_attack_config = {
4+
"base": {
5+
"text": prompt_data.ALL_OPENAI_PLAYGROUND_PROMPTS,
6+
},
7+
"attack": {
8+
"text": (
9+
prompt_data.goal_hikacking_attacks["ignore-print"],
10+
prompt_data.goal_hikacking_attacks["ignore-say"],
11+
),
12+
"rogue_string": prompt_data.rogue_strings["hate-humans"],
13+
"scoring": "match_rogue_to_output",
14+
"settings": {
15+
"escape": prompt_data.escape_chars["n"],
16+
"delimiter": prompt_data.delimiter_chars["dash"],
17+
"escape_length": 10,
18+
"escape_times": 2,
19+
},
20+
},
21+
"config": {
22+
"id": (0, 1, 2, 3),
23+
},
24+
"visualization": {
25+
"columns": (
26+
"config_model",
27+
"config_temperature",
28+
"prompt_instruction",
29+
"attack_instruction",
30+
"attack_rogue_string",
31+
"config_id",
32+
"score",
33+
)
34+
},
35+
}
36+

0 commit comments

Comments
 (0)