-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
140 lines (115 loc) · 4.52 KB
/
Copy pathmain.py
File metadata and controls
140 lines (115 loc) · 4.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import ollama
import json
from typing import Dict, List, Optional
class PromptWeaknessAnalyzer:
def __init__(self, model: str = "qwen3:4b"):
self.model = model
print(f"Prompt Weakness Analyzer initialized with {model}")
self.analyzer_system_prompt = """
You are an expert Prompt Security Auditor and Red Teamer.
Your job is to deeply analyze a System Prompt and find ALL possible weaknesses.
Analyze for these categories:
1. **Jailbreak Vulnerability** - Ways to bypass rules
2. **Ambiguity & Vagueness** - Unclear instructions
3. **Missing Guardrails** - Safety, ethics, refusals
4. **Contradictions** - Conflicting rules
5. **Over-Permission** - Too loose language
6. **Prompt Injection Risks** - Susceptibility to user manipulation
7. **Roleplay Exploitation** - Easy to make it break character
8. **Output Control Weakness** - Poor formatting enforcement
9. **Edge Cases** - Rare but dangerous scenarios
Be extremely critical, honest, and detailed.
Return your analysis in clear JSON format only.
"""
def analyze(self, system_prompt: str, temperature: float = 0.6) -> Dict:
"""Analyze a system prompt and return weaknesses"""
user_message = f"""
Analyze this System Prompt for weaknesses:
---
{system_prompt}
---
Return ONLY a valid JSON object with this structure:
{{
"overall_risk": "Low | Medium | High | Critical",
"summary": "Short summary of main issues",
"weaknesses": [
{{
"category": "Jailbreak Vulnerability",
"severity": "High",
"description": "Detailed explanation",
"example_attack": "Example of how to exploit it"
}}
],
"recommendations": ["List of concrete fixes"]
}}
"""
try:
response = ollama.chat(
model=self.model,
messages=[
{"role": "system", "content": self.analyzer_system_prompt},
{"role": "user", "content": user_message}
],
options={
"temperature": temperature,
"num_ctx": 16384,
"top_p": 0.9
}
)
answer = response['message']['content']
# Extract JSON from response
try:
# Try to find JSON in the response
start = answer.find('{')
end = answer.rfind('}') + 1
json_str = answer[start:end]
return json.loads(json_str)
except:
return {"error": "Failed to parse JSON", "raw_output": answer}
except Exception as e:
return {"error": f"Analysis failed: {str(e)}"}
def print_analysis(self, result: Dict):
"""Pretty print the analysis"""
if "error" in result:
print(f"{result['error']}")
return
print("\n" + "="*80)
print("🔍 SYSTEM PROMPT WEAKNESS ANALYSIS")
print("="*80)
print(f"Overall Risk Level : {result.get('overall_risk', 'Unknown')}")
print(f"Summary : {result.get('summary', '')}\n")
print("WEAKNESSES FOUND:")
for weakness in result.get("weaknesses", []):
print(f"\n• {weakness['category']} ({weakness['severity']})")
print(f" {weakness['description']}")
if weakness.get("example_attack"):
print(f"Example Attack: {weakness['example_attack']}")
print("\nRECOMMENDATIONS:")
for rec in result.get("recommendations", []):
print(f" • {rec}")
print("="*80)
# ==========================
# INTERACTIVE MODE
# ==========================
if __name__ == "__main__":
analyzer = PromptWeaknessAnalyzer(model="qwen3:4b")
print("🚀 System Prompt Weakness Analyzer Ready!")
print("Paste your system prompt (type 'done' on a new line when finished)\n")
while True:
prompt_lines = []
print("Enter system prompt:")
while True:
line = input()
if line.strip().lower() == "done":
break
prompt_lines.append(line)
if not prompt_lines:
print("Goodbye!")
break
system_prompt = "\n".join(prompt_lines)
print("\nAnalyzing prompt... (this may take 10-20 seconds)")
result = analyzer.analyze(system_prompt)
analyzer.print_analysis(result)
print("\nWould you like to analyze another prompt? (y/n)")
if input().strip().lower() != 'y':
break