-
Notifications
You must be signed in to change notification settings - Fork 179
/
Copy pathdread.py
410 lines (360 loc) · 16.2 KB
/
dread.py
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
import json
import requests
import time
from anthropic import Anthropic
from mistralai import Mistral, UserMessage
from openai import OpenAI, AzureOpenAI
import streamlit as st
import google.generativeai as genai
from groq import Groq
from utils import process_groq_response, create_reasoning_system_prompt
def dread_json_to_markdown(dread_assessment):
markdown_output = "| Threat Type | Scenario | Damage Potential | Reproducibility | Exploitability | Affected Users | Discoverability | Risk Score |\n"
markdown_output += "|-------------|----------|------------------|-----------------|----------------|----------------|-----------------|-------------|\n"
try:
# Access the list of threats under the "Risk Assessment" key
threats = dread_assessment.get("Risk Assessment", [])
for threat in threats:
# Check if threat is a dictionary
if isinstance(threat, dict):
damage_potential = threat.get('Damage Potential', 0)
reproducibility = threat.get('Reproducibility', 0)
exploitability = threat.get('Exploitability', 0)
affected_users = threat.get('Affected Users', 0)
discoverability = threat.get('Discoverability', 0)
# Calculate the Risk Score
risk_score = (damage_potential + reproducibility + exploitability + affected_users + discoverability) / 5
markdown_output += f"| {threat.get('Threat Type', 'N/A')} | {threat.get('Scenario', 'N/A')} | {damage_potential} | {reproducibility} | {exploitability} | {affected_users} | {discoverability} | {risk_score:.2f} |\n"
else:
raise TypeError(f"Expected a dictionary, got {type(threat)}: {threat}")
except Exception as e:
# Print the error message and type for debugging
st.write(f"Error: {e}")
raise
return markdown_output
# Function to create a prompt to generate mitigating controls
def create_dread_assessment_prompt(threats):
prompt = f"""
Act as a cyber security expert with more than 20 years of experience in threat modeling using STRIDE and DREAD methodologies.
Your task is to produce a DREAD risk assessment for the threats identified in a threat model.
Below is the list of identified threats:
{threats}
When providing the risk assessment, use a JSON formatted response with a top-level key "Risk Assessment" and a list of threats, each with the following sub-keys:
- "Threat Type": A string representing the type of threat (e.g., "Spoofing").
- "Scenario": A string describing the threat scenario.
- "Damage Potential": An integer between 1 and 10.
- "Reproducibility": An integer between 1 and 10.
- "Exploitability": An integer between 1 and 10.
- "Affected Users": An integer between 1 and 10.
- "Discoverability": An integer between 1 and 10.
Assign a value between 1 and 10 for each sub-key based on the DREAD methodology. Use the following scale:
- 1-3: Low
- 4-6: Medium
- 7-10: High
Ensure the JSON response is correctly formatted and does not contain any additional text. Here is an example of the expected JSON response format:
{{
"Risk Assessment": [
{{
"Threat Type": "Spoofing",
"Scenario": "An attacker could create a fake OAuth2 provider and trick users into logging in through it.",
"Damage Potential": 8,
"Reproducibility": 6,
"Exploitability": 5,
"Affected Users": 9,
"Discoverability": 7
}},
{{
"Threat Type": "Spoofing",
"Scenario": "An attacker could intercept the OAuth2 token exchange process through a Man-in-the-Middle (MitM) attack.",
"Damage Potential": 8,
"Reproducibility": 7,
"Exploitability": 6,
"Affected Users": 8,
"Discoverability": 6
}}
]
}}
"""
return prompt
# Function to get DREAD risk assessment from the GPT response.
def get_dread_assessment(api_key, model_name, prompt):
client = OpenAI(api_key=api_key)
# For reasoning models (o1, o3-mini), use a structured system prompt
if model_name in ["o1", "o3-mini"]:
system_prompt = create_reasoning_system_prompt(
task_description="Perform a DREAD risk assessment for the identified security threats.",
approach_description="""1. For each threat in the provided threat model:
- Analyze the threat type and scenario in detail
- Evaluate Damage Potential (1-10):
* Consider direct and indirect damage
* Assess financial, reputational, and operational impact
- Evaluate Reproducibility (1-10):
* Assess how reliably the attack can be reproduced
* Consider required conditions and resources
- Evaluate Exploitability (1-10):
* Analyze technical complexity
* Consider required skills and tools
- Evaluate Affected Users (1-10):
* Determine scope of impact
* Consider both direct and indirect users
- Evaluate Discoverability (1-10):
* Assess how easily the vulnerability can be found
* Consider visibility and detection methods
2. Format output as JSON with 'Risk Assessment' array containing:
- Threat Type
- Scenario
- Numerical scores (1-10) for each DREAD category"""
)
else:
system_prompt = "You are a helpful assistant designed to output JSON."
response = client.chat.completions.create(
model=model_name,
response_format={"type": "json_object"},
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": prompt}
]
)
# Convert the JSON string in the 'content' field to a Python dictionary
try:
dread_assessment = json.loads(response.choices[0].message.content)
except json.JSONDecodeError as e:
st.write(f"JSON decoding error: {e}")
dread_assessment = {}
return dread_assessment
# Function to get DREAD risk assessment from the Azure OpenAI response.
def get_dread_assessment_azure(azure_api_endpoint, azure_api_key, azure_api_version, azure_deployment_name, prompt):
client = AzureOpenAI(
azure_endpoint = azure_api_endpoint,
api_key = azure_api_key,
api_version = azure_api_version,
)
response = client.chat.completions.create(
model = azure_deployment_name,
response_format={"type": "json_object"},
messages=[
{"role": "system", "content": "You are a helpful assistant designed to output JSON."},
{"role": "user", "content": prompt}
]
)
# Convert the JSON string in the 'content' field to a Python dictionary
try:
dread_assessment = json.loads(response.choices[0].message.content)
except json.JSONDecodeError as e:
st.write(f"JSON decoding error: {e}")
dread_assessment = {}
return dread_assessment
# Function to get DREAD risk assessment from the Google model's response.
def get_dread_assessment_google(google_api_key, google_model, prompt):
genai.configure(api_key=google_api_key)
model = genai.GenerativeModel(google_model)
# Create the system message
system_message = "You are a helpful assistant designed to output JSON. Only provide the DREAD risk assessment in JSON format with no additional text. Do not wrap the output in a code block."
# Start a chat session with the system message in the history
chat = model.start_chat(history=[
{"role": "user", "parts": [system_message]},
{"role": "model", "parts": ["Understood. I will provide DREAD risk assessments in JSON format only and will not wrap the output in a code block."]}
])
# Send the actual prompt
response = chat.send_message(
prompt,
safety_settings={
'DANGEROUS': 'block_only_high' # Set safety filter to allow generation of DREAD risk assessments
})
print(response)
try:
# Access the JSON content from the response
dread_assessment = json.loads(response.text)
return dread_assessment
except json.JSONDecodeError as e:
print(f"Error decoding JSON: {str(e)}")
print("Raw JSON string:")
print(response.text)
return {}
# Function to get DREAD risk assessment from the Mistral model's response.
def get_dread_assessment_mistral(mistral_api_key, mistral_model, prompt):
client = Mistral(api_key=mistral_api_key)
response = client.chat.complete(
model=mistral_model,
response_format={"type": "json_object"},
messages=[
UserMessage(content=prompt)
]
)
try:
# Convert the JSON string in the 'content' field to a Python dictionary
dread_assessment = json.loads(response.choices[0].message.content)
except json.JSONDecodeError as e:
print(f"Error decoding JSON: {str(e)}")
print("Raw JSON string:")
print(response.choices[0].message.content)
dread_assessment = {}
return dread_assessment
# Function to get DREAD risk assessment from Ollama hosted LLM.
def get_dread_assessment_ollama(ollama_endpoint, ollama_model, prompt):
"""
Get DREAD risk assessment from Ollama hosted LLM.
Args:
ollama_endpoint (str): The URL of the Ollama endpoint (e.g., 'http://localhost:11434')
ollama_model (str): The name of the model to use
prompt (str): The prompt to send to the model
Returns:
dict: The parsed JSON response containing the DREAD assessment
Raises:
requests.exceptions.RequestException: If there's an error communicating with the Ollama endpoint
json.JSONDecodeError: If the response cannot be parsed as JSON
KeyError: If the response doesn't contain the expected fields
"""
if not ollama_endpoint.endswith('/'):
ollama_endpoint = ollama_endpoint + '/'
url = ollama_endpoint + "api/chat"
max_retries = 3
retry_delay = 2 # seconds
data = {
"model": ollama_model,
"stream": False,
"format": "json",
"messages": [
{
"role": "system",
"content": """You are a cyber security expert with more than 20 years experience of using the DREAD risk assessment methodology to evaluate security threats. Your task is to analyze the provided application description and perform a DREAD assessment.
Please provide your response in JSON format with the following structure:
{
"dread_assessment": [
{
"threat": "Description of the threat",
"damage": "Score and explanation",
"reproducibility": "Score and explanation",
"exploitability": "Score and explanation",
"affected_users": "Score and explanation",
"discoverability": "Score and explanation",
"risk_score": "Calculated total score"
}
]
}"""
},
{
"role": "user",
"content": prompt
}
]
}
for attempt in range(max_retries):
try:
response = requests.post(url, json=data, timeout=60) # Add timeout
response.raise_for_status() # Raise exception for bad status codes
outer_json = response.json()
try:
# Access the 'content' attribute of the 'message' dictionary and parse as JSON
dread_assessment = json.loads(outer_json["message"]["content"])
return dread_assessment
except (json.JSONDecodeError, KeyError) as e:
print(f"Error parsing response as JSON: {str(e)}")
print("Raw response:", outer_json)
if attempt == max_retries - 1: # Last attempt
raise
time.sleep(retry_delay)
continue
except requests.exceptions.RequestException as e:
print(f"Error communicating with Ollama endpoint: {str(e)}")
if attempt == max_retries - 1: # Last attempt
raise
time.sleep(retry_delay)
continue
# Function to get DREAD risk assessment from the Anthropic model's response.
def get_dread_assessment_anthropic(anthropic_api_key, anthropic_model, prompt):
client = Anthropic(api_key=anthropic_api_key)
response = client.messages.create(
model=anthropic_model,
max_tokens=4096,
system="You are a helpful assistant designed to output JSON.",
messages=[
{"role": "user", "content": prompt}
]
)
try:
# Extract the text content from the first content block
response_text = response.content[0].text
# Check if the JSON is complete (should end with a closing brace)
if not response_text.strip().endswith('}'):
raise json.JSONDecodeError("Incomplete JSON response", response_text, len(response_text))
# Parse the JSON string
dread_assessment = json.loads(response_text)
return dread_assessment
except (json.JSONDecodeError, IndexError, AttributeError) as e:
print(f"Error processing response: {str(e)}")
print("Raw response:")
print(response)
return {}
# Function to get DREAD risk assessment from LM Studio Server response.
def get_dread_assessment_lm_studio(lm_studio_endpoint, model_name, prompt):
client = OpenAI(
base_url=f"{lm_studio_endpoint}/v1",
api_key="not-needed" # LM Studio Server doesn't require an API key
)
# Define the expected response structure
dread_schema = {
"type": "json_schema",
"json_schema": {
"name": "dread_assessment_response",
"schema": {
"type": "object",
"properties": {
"Risk Assessment": {
"type": "array",
"items": {
"type": "object",
"properties": {
"Threat Type": {"type": "string"},
"Scenario": {"type": "string"},
"Damage Potential": {"type": "integer", "minimum": 1, "maximum": 10},
"Reproducibility": {"type": "integer", "minimum": 1, "maximum": 10},
"Exploitability": {"type": "integer", "minimum": 1, "maximum": 10},
"Affected Users": {"type": "integer", "minimum": 1, "maximum": 10},
"Discoverability": {"type": "integer", "minimum": 1, "maximum": 10}
},
"required": ["Threat Type", "Scenario", "Damage Potential", "Reproducibility", "Exploitability", "Affected Users", "Discoverability"]
}
}
},
"required": ["Risk Assessment"]
}
}
}
response = client.chat.completions.create(
model=model_name,
response_format=dread_schema,
messages=[
{"role": "system", "content": "You are a helpful assistant designed to output JSON."},
{"role": "user", "content": prompt}
]
)
# Convert the JSON string in the 'content' field to a Python dictionary
try:
dread_assessment = json.loads(response.choices[0].message.content)
except json.JSONDecodeError as e:
st.write(f"JSON decoding error: {e}")
dread_assessment = {}
return dread_assessment
# Function to get DREAD risk assessment from the Groq model's response.
def get_dread_assessment_groq(groq_api_key, groq_model, prompt):
client = Groq(api_key=groq_api_key)
response = client.chat.completions.create(
model=groq_model,
response_format={"type": "json_object"},
messages=[
{"role": "system", "content": "You are a helpful assistant designed to output JSON."},
{"role": "user", "content": prompt}
]
)
# Process the response using our utility function
reasoning, dread_assessment = process_groq_response(
response.choices[0].message.content,
groq_model,
expect_json=True
)
# If we got reasoning, display it in an expander in the UI
if reasoning:
with st.expander("View model's reasoning process", expanded=False):
st.write(reasoning)
return dread_assessment