forked from mrwadams/stride-gpt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdread.py
262 lines (228 loc) · 10.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
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
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)
response = client.chat.completions.create(
model=model_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 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_model, prompt):
url = "http://localhost:11434/api/chat"
max_retries = 3
retry_delay = 2 # seconds
for attempt in range(1, max_retries + 1):
data = {
"model": ollama_model,
"stream": False,
"messages": [
{
"role": "system",
"content": "You are a helpful assistant designed to output JSON. Only provide the DREAD risk assessment in JSON format with no additional text."
},
{
"role": "user",
"content": prompt,
"format": "json"
}
]
}
try:
response = requests.post(url, json=data)
outer_json = response.json()
response_content = outer_json["message"]["content"]
# Attempt to parse JSON
dread_assessment = json.loads(response_content)
return dread_assessment
except json.JSONDecodeError as e:
st.error(f"Attempt {attempt}: Error decoding JSON. Retrying...")
print(f"Error decoding JSON: {str(e)}")
print("Raw JSON string:")
print(response_content)
if attempt < max_retries:
time.sleep(retry_delay)
else:
st.error("Max retries reached. Unable to generate valid JSON response.")
return {}
# This line should never be reached due to the return statements above,
# but it's here as a fallback
return {}
# 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 {}