Repository navigation
Expand file tree
/
Copy pathShadowBox.py
More file actions
218 lines (190 loc) · 6.54 KB
/
Copy pathShadowBox.py
File metadata and controls
218 lines (190 loc) · 6.54 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
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
"""
ShadowBox – AI-Powered Silent Cyberattack Simulation Tool
Python version using FastAPI, Docker, Google Cloud, OpenAI API, and CI/CD
Instructions:
1. Install dependencies:
pip install fastapi uvicorn docker openai google-cloud-logging python-dotenv
2. Set environment variables in a .env file or your environment:
OPENAI_API_KEY=your_openai_api_key
GOOGLE_APPLICATION_CREDENTIALS=path_to_your_gcp_service_account.json
GCP_PROJECT_ID=your_gcp_project_id
3. Run the app:
uvicorn CyberSecurity:app --reload
4. Example request:
POST http://localhost:8000/api/v1/simulation/start
{
"environment_image": "python:3.10-slim",
"attack_type": "privilege_escalation"
}
"""
import os
import uuid
from fastapi import FastAPI, APIRouter, Request
from fastapi.responses import JSONResponse
from pydantic import BaseModel
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
import docker
import openai
from google.cloud import logging as gcp_logging
# Initialize OpenAI
openai.api_key = os.getenv("OPENAI_API_KEY")
GCP_PROJECT_ID = os.getenv("GCP_PROJECT_ID")
# Initialize Docker client
docker_client = docker.from_env()
# Initialize Google Cloud Logging client
try:
gcp_logger = gcp_logging.Client(project=GCP_PROJECT_ID)
gcp_log = gcp_logger.logger("shadowbox-simulations")
except Exception:
gcp_log = None
app = FastAPI(
title="ShadowBox AI-Powered Cyberattack Simulation API",
version="1.0.0"
)
router = APIRouter(prefix="/api/v1/simulation")
class SimulationRequest(BaseModel):
environment_image: str
attack_type: str
@router.get("/health")
async def health():
return {"status": "ShadowBox API is running."}
@router.get("/version")
async def version():
return {
"apiVersion": "v1",
"description": "ShadowBox AI-Powered Cyberattack Simulation API"
}
@router.post("/start")
async def start_simulation(request: SimulationRequest):
response = {}
container_id = None
try:
# 1. Create isolated Docker environment
container = docker_client.containers.run(
request.environment_image,
command="sleep 30", # Keep container alive for simulation
detach=True,
tty=True,
auto_remove=True
)
container_id = container.id
# 2. Simulate attack (mocked)
attack_result = simulate_attack(container_id, request.attack_type)
# 3. Analyze with OpenAI
analysis = analyze_vulnerability(attack_result)
# 4. Store results in Google Cloud Logging
log_to_gcp(request, attack_result, analysis, container_id)
response = {
"container_id": container_id,
"attack_result": attack_result,
"analysis": analysis,
"status": "Simulation completed successfully."
}
return JSONResponse(content=response)
except Exception as e:
response = {"error": str(e)}
return JSONResponse(content=response, status_code=500)
finally:
# Clean up container if still running
if container_id:
try:
container = docker_client.containers.get(container_id)
container.stop()
except Exception:
pass
def simulate_attack(container_id, attack_type):
# In real code, execute attack scripts inside the Docker container
# Here, we just return a mock result
return (
f"Attack '{attack_type}' simulated on container {container_id[:12]}. "
"Vulnerabilities found: [CVE-2023-1234, CVE-2022-5678]"
)
def analyze_vulnerability(attack_result):
# In real code, call OpenAI API with the attack_result as prompt
# Here, we use OpenAI GPT-3.5/4 for demonstration
try:
prompt = (
f"Given the following simulated cyberattack result, "
f"analyze the vulnerabilities and provide recommendations:\n\n{attack_result}"
)
completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a cybersecurity expert."},
{"role": "user", "content": prompt}
],
max_tokens=200
)
return completion.choices[0].message.content.strip()
except Exception as e:
return f"OpenAI Analysis Error: {e}"
def log_to_gcp(request, attack_result, analysis, container_id):
if gcp_log:
gcp_log.log_struct({
"environment_image": request.environment_image,
"attack_type": request.attack_type,
"container_id": container_id,
"attack_result": attack_result,
"analysis": analysis
})
else:
print("GCP Logging not configured. Log entry:")
print({
"environment_image": request.environment_image,
"attack_type": request.attack_type,
"container_id": container_id,
"attack_result": attack_result,
"analysis": analysis
})
app.include_router(router)
# Example CI/CD pipeline (GitHub Actions: .github/workflows/ci-cd.yml)
"""
name: ShadowBox CI/CD
on:
push:
branches: [ main ]
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.10'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install fastapi uvicorn docker openai google-cloud-logging python-dotenv
- name: Run tests
run: |
echo "No tests yet"
- name: Build Docker image
run: docker build -t gcr.io/${{ secrets.GCP_PROJECT_ID }}/shadowbox:latest .
- name: Authenticate to Google Cloud
uses: google-github-actions/auth@v1
with:
credentials_json: '${{ secrets.GCP_SA_KEY }}'
- name: Push Docker image
run: |
gcloud auth configure-docker
docker push gcr.io/${{ secrets.GCP_PROJECT_ID }}/shadowbox:latest
- name: Deploy to Cloud Run
run: |
gcloud run deploy shadowbox \
--image gcr.io/${{ secrets.GCP_PROJECT_ID }}/shadowbox:latest \
--platform managed \
--region us-central1 \
--allow-unauthenticated
"""
# Example Dockerfile (place in project root)
"""
FROM python:3.10-slim
WORKDIR /app
COPY . /app
RUN pip install --no-cache-dir fastapi uvicorn docker openai google-cloud-logging python-dotenv
EXPOSE 8000
CMD ["uvicorn", "CyberSecurity:app", "--host", "0.0.0.0", "--port", "8000"]
"""