-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathcybics.sh
More file actions
executable file
·461 lines (414 loc) · 16.1 KB
/
Copy pathcybics.sh
File metadata and controls
executable file
·461 lines (414 loc) · 16.1 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
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
#!/bin/bash
# Change to script directory (repository root)
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR" || {
echo "Error: Failed to change to script directory"
exit 1
}
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
ORANGE='\033[38;5;208m'
NC='\033[0m' # No Color
# CybICS ASCII Art
print_banner() {
print_message "
██████╗ ██╗ ██╗ ██████╗ ██╗ ██████╗ ███████╗
██╔════╝ ╚██╗ ██╔╝ ██╔══██╗ ██║ ██╔════╝ ██╔════╝
██║ ╚████╔ ╝ ██████╔╝ ██║ ██║ ███████╗
██║ ╚██╔╝ ██╔══██╗ ██║ ██║ ╚════██║
╚██████╔╝ ██║ ██████╔╝ ██║ ╚██████╗ ███████║
╚═════╝ ╚═╝ ╚═════╝ ╚═╝ ╚═════╝ ╚══════╝
" "$ORANGE"
print_message "Cybersecurity testbed for Industrial Control Systems" "$ORANGE"
print_message "========================================" "$ORANGE"
echo
}
# Function to print colored messages
print_message() {
echo -e "${2}${1}${NC}"
}
# Function to show help message
show_help() {
echo "Usage: $0 {start|stop|restart|status|logs|update|clean|compose} [--mode <mode>] [--version <tag>]"
echo ""
echo "Commands:"
echo " start - Start the CybICS virtual environment"
echo " stop - Stop the CybICS virtual environment"
echo " restart - Restart the CybICS virtual environment"
echo " status - Show status of all services"
echo " logs - Show logs from all services"
echo " update - Pull latest Docker images for all services"
echo " clean - Remove all CybICS containers, images, and volumes"
echo " compose - Directly interact with docker compose (e.g., $0 compose ps)"
echo ""
echo "Options:"
echo " --mode <mode> - Specify startup mode (default: full)"
echo " minimal - Core services only (OpenPLC, FUXA, HWIO, OPC UA, S7, Landing)"
echo " full - All services including AI Agent, Engineering WS, Attack Machine"
echo " withoutai - All services except AI Agent"
echo " --version <tag> - Specify Docker image version tag (default: latest)"
echo " Examples: --version 1.0.0, --version abc1234, --version latest"
exit 1
}
# Check if docker-compose or docker compose is available
if ! command -v docker-compose &> /dev/null && ! docker compose version &> /dev/null; then
print_message "Error: Neither 'docker-compose' nor 'docker compose' was found." "$RED"
print_message "Please install Docker Compose:" "$RED"
print_message "1. For Linux:" "$RED"
print_message " sudo apt-get update" "$RED"
print_message " sudo apt-get install docker-compose-plugin" "$RED"
print_message "2. For macOS:" "$RED"
print_message " Install Docker Desktop from https://www.docker.com/products/docker-desktop" "$RED"
print_message "3. For Windows:" "$RED"
print_message " Install Docker Desktop from https://www.docker.com/products/docker-desktop" "$RED"
exit 1
fi
# Use docker compose if available, otherwise fall back to docker-compose
if docker compose version &> /dev/null; then
DOCKER_COMPOSE="docker compose"
else
DOCKER_COMPOSE="docker-compose"
fi
# Function to check if Docker is running
check_docker() {
if ! docker info > /dev/null 2>&1; then
print_message "Error: Docker is not running. Please start Docker first." "$RED"
exit 1
fi
}
# Function to ensure required environment files exist
ensure_env_files() {
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Create .docker.env directory if it doesn't exist
if [ ! -d "$SCRIPT_DIR/.docker.env" ]; then
print_message "Creating .docker.env directory..." "$YELLOW"
mkdir -p "$SCRIPT_DIR/.docker.env"
print_message ".docker.env directory created successfully!" "$GREEN"
fi
# Check if .env exists, if not create it with default values
if [ ! -f .env ]; then
print_message "Creating .env file with default values..." "$YELLOW"
cat > .env << EOF
HOST_UID=1000
HOST_GID=1000
DOCKER_ENV_DIR="$SCRIPT_DIR/.docker.env"
EOF
print_message ".env file created successfully!" "$GREEN"
fi
# Check if .dev.env exists, if not create it with default values
if [ ! -f .dev.env ]; then
print_message "Creating .dev.env file with default values..." "$YELLOW"
cat > .dev.env << 'EOF'
DEVICE_IP=cybics
DEVICE_USER=pi
EOF
print_message ".dev.env file created successfully!" "$GREEN"
fi
}
# Function to find conflicting networks
find_conflicting_networks() {
local target_subnet="$1"
# Extract the network prefix (e.g., 172.19.0 from 172.19.0.0/24)
local prefix=$(echo "$target_subnet" | sed 's/\.[0-9]*\/.*$//')
# Find networks that might overlap
docker network ls --format '{{.Name}}' | while read network; do
local subnet=$(docker network inspect "$network" --format '{{range .IPAM.Config}}{{.Subnet}}{{end}}' 2>/dev/null)
if [[ "$subnet" == *"$prefix"* ]]; then
echo "$network"
fi
done
}
# Function to handle network overlap error
handle_network_overlap() {
local error_output="$1"
# Check if this is a network overlap error
if echo "$error_output" | grep -q "Pool overlaps with other one on this address space"; then
print_message "\nNetwork conflict detected!" "$YELLOW"
print_message "Another Docker network is using the same IP range." "$YELLOW"
# Try to find conflicting networks (check common CybICS subnets)
print_message "\nSearching for conflicting networks..." "$YELLOW"
local conflicting=""
# Check for networks with similar subnets
for network in $(docker network ls --format '{{.Name}}' | grep -v "bridge\|host\|none"); do
local subnet=$(docker network inspect "$network" --format '{{range .IPAM.Config}}{{.Subnet}}{{end}}' 2>/dev/null)
if [[ -n "$subnet" ]]; then
# Check if it's in the 172.18.x.x or 172.19.x.x range (common for CybICS)
if [[ "$subnet" == 172.1[89].* ]] || [[ "$subnet" == 10.0.* ]]; then
if [[ -z "$conflicting" ]]; then
conflicting="$network ($subnet)"
else
conflicting="$conflicting, $network ($subnet)"
fi
fi
fi
done
if [[ -n "$conflicting" ]]; then
print_message "Potentially conflicting networks: $conflicting" "$YELLOW"
fi
# List all custom networks for user reference
print_message "\nAll custom Docker networks:" "$BLUE"
docker network ls --format 'table {{.Name}}\t{{.Driver}}' | grep -v "bridge\|host\|none" | head -20
echo
print_message "Would you like to remove conflicting networks and retry? (y/n)" "$YELLOW"
read -r response
if [[ "$response" =~ ^[Yy]$ ]]; then
print_message "\nWhich network would you like to remove?" "$YELLOW"
print_message "Enter network name (or 'all' to remove all custom networks, 'cancel' to abort):" "$YELLOW"
read -r network_name
if [[ "$network_name" == "cancel" ]]; then
print_message "Operation cancelled." "$YELLOW"
return 1
elif [[ "$network_name" == "all" ]]; then
print_message "Removing all custom networks..." "$YELLOW"
for network in $(docker network ls --format '{{.Name}}' | grep -v "bridge\|host\|none"); do
docker network rm "$network" 2>/dev/null && \
print_message "Removed network: $network" "$GREEN"
done
return 0
elif [[ -n "$network_name" ]]; then
if docker network rm "$network_name" 2>/dev/null; then
print_message "Removed network: $network_name" "$GREEN"
return 0
else
print_message "Failed to remove network: $network_name" "$RED"
print_message "The network might be in use. Try stopping containers first with: ./cybics.sh stop" "$YELLOW"
return 1
fi
fi
fi
return 1
fi
return 1
}
# Function to display available services based on mode
display_services() {
print_message "CybICS virtual environment started successfully!" "$GREEN"
print_message "\nAvailable services:" "$YELLOW"
print_message "Landing page: http://localhost:80" "$GREEN"
print_message "OpenPLC: http://localhost:8080" "$GREEN"
print_message "FUXA: http://localhost:1881" "$GREEN"
print_message "HWIO: http://localhost:8090" "$GREEN"
# Display optional services based on mode
if [[ "$CYBICS_MODE" == "full" || "$CYBICS_MODE" == "withoutai" ]]; then
print_message "Engineering Workstation (VNC): http://localhost:6080/vnc.html" "$GREEN"
print_message "Attack Machine (VNC): http://localhost:6081/vnc.html" "$GREEN"
fi
print_message "OPC UA: opc.tcp://localhost:4840" "$GREEN"
print_message "S7 Communication: localhost:102" "$GREEN"
if [[ "$CYBICS_MODE" == "full" ]]; then
print_message "CybICS AI Agent: Available via Landing Page" "$GREEN"
fi
}
# Function to start the environment
start_environment() {
print_message "Starting CybICS virtual environment..." "$YELLOW"
cd "$SCRIPT_DIR/.devcontainer/virtual" || {
print_message "Error: Cannot access .devcontainer/virtual directory" "$RED"
exit 1
}
# Pull images first with visible progress
print_message "Pulling Docker images (this may take a while on first run)..." "$YELLOW"
if ! $DOCKER_COMPOSE pull; then
print_message "Warning: Some images could not be pulled. Continuing with available images..." "$YELLOW"
fi
# Start containers (capture output for error handling)
print_message "Starting containers..." "$YELLOW"
local output
output=$($DOCKER_COMPOSE up -d 2>&1)
local exit_code=$?
if [ $exit_code -eq 0 ]; then
display_services
else
echo "$output"
# Check for network overlap error and handle it
if handle_network_overlap "$output"; then
print_message "\nRetrying startup..." "$YELLOW"
$DOCKER_COMPOSE up -d
if [ $? -eq 0 ]; then
display_services
return
fi
fi
print_message "Error: Failed to start CybICS virtual environment." "$RED"
exit 1
fi
}
# Function to stop the environment
stop_environment() {
print_message "Stopping CybICS virtual environment..." "$YELLOW"
cd "$SCRIPT_DIR/.devcontainer/virtual" || {
print_message "Error: Cannot access .devcontainer/virtual directory" "$RED"
exit 1
}
$DOCKER_COMPOSE down
if [ $? -eq 0 ]; then
print_message "CybICS virtual environment stopped successfully!" "$GREEN"
else
print_message "Error: Failed to stop CybICS virtual environment." "$RED"
exit 1
fi
}
# Function to show status
show_status() {
print_message "Checking CybICS virtual environment status..." "$YELLOW"
cd "$SCRIPT_DIR/.devcontainer/virtual" || {
print_message "Error: Cannot access .devcontainer/virtual directory" "$RED"
exit 1
}
$DOCKER_COMPOSE ps
}
# Function to show logs
show_logs() {
print_message "Showing logs for CybICS virtual environment..." "$YELLOW"
cd "$SCRIPT_DIR/.devcontainer/virtual" || {
print_message "Error: Cannot access .devcontainer/virtual directory" "$RED"
exit 1
}
$DOCKER_COMPOSE logs -f
}
# Function to remove all CybICS containers
remove_containers() {
print_message "Removing all CybICS containers..." "$YELLOW"
cd "$SCRIPT_DIR/.devcontainer/virtual" || {
print_message "Error: Cannot access .devcontainer/virtual directory" "$RED"
exit 1
}
$DOCKER_COMPOSE down --rmi all --volumes --remove-orphans
if [ $? -eq 0 ]; then
print_message "All CybICS containers, images, and volumes removed successfully!" "$GREEN"
else
print_message "Error: Failed to remove CybICS containers." "$RED"
exit 1
fi
}
# Function to directly interact with docker compose
direct_compose() {
cd "$SCRIPT_DIR/.devcontainer/virtual" || {
print_message "Error: Cannot access .devcontainer/virtual directory" "$RED"
exit 1
}
$DOCKER_COMPOSE "$@"
}
# Function to update (pull latest images)
update_images() {
print_message "Pulling latest CybICS Docker images..." "$YELLOW"
cd "$SCRIPT_DIR/.devcontainer/virtual" || {
print_message "Error: Cannot access .devcontainer/virtual directory" "$RED"
exit 1
}
$DOCKER_COMPOSE pull
if [ $? -eq 0 ]; then
print_message "CybICS Docker images updated successfully!" "$GREEN"
print_message "\nTo apply updates, restart the environment with:" "$YELLOW"
print_message " ./cybics.sh restart" "$BLUE"
else
print_message "Error: Failed to pull CybICS Docker images." "$RED"
exit 1
fi
}
# Main script
print_banner
check_docker
ensure_env_files
print_message "Using Docker Compose command: $DOCKER_COMPOSE" "$YELLOW"
# Parse arguments for version and mode options
CYBICS_VERSION="latest"
CYBICS_MODE="full"
COMMAND=""
while [[ $# -gt 0 ]]; do
case "$1" in
--version)
if [[ -n "$2" && "$2" != --* ]]; then
CYBICS_VERSION="$2"
shift 2
else
print_message "Error: --version requires a tag argument" "$RED"
show_help
fi
;;
--mode)
if [[ -n "$2" && "$2" != --* ]]; then
CYBICS_MODE="$2"
if [[ "$CYBICS_MODE" != "minimal" && "$CYBICS_MODE" != "full" && "$CYBICS_MODE" != "withoutai" ]]; then
print_message "Error: Invalid mode '$CYBICS_MODE'. Must be: minimal, full, or withoutai" "$RED"
show_help
fi
shift 2
else
print_message "Error: --mode requires an argument" "$RED"
show_help
fi
;;
start|stop|restart|status|logs|update|clean|compose)
COMMAND="$1"
shift
# For 'compose' command, save remaining arguments
if [[ "$COMMAND" == "compose" ]]; then
COMPOSE_ARGS="$@"
break
fi
;;
*)
if [[ -z "$COMMAND" ]]; then
show_help
else
# Unknown option after command
print_message "Error: Unknown option: $1" "$RED"
show_help
fi
;;
esac
done
# Export version for docker-compose
export CYBICS_VERSION
# Set COMPOSE_PROFILES based on mode
case "$CYBICS_MODE" in
"minimal")
export COMPOSE_PROFILES=""
;;
"full")
export COMPOSE_PROFILES="full,attack,engineering,ai"
;;
"withoutai")
export COMPOSE_PROFILES="attack,engineering"
;;
esac
if [[ "$CYBICS_VERSION" != "latest" ]]; then
print_message "Using Docker image version: $CYBICS_VERSION" "$BLUE"
fi
if [[ "$CYBICS_MODE" != "full" ]]; then
print_message "Using startup mode: $CYBICS_MODE" "$BLUE"
fi
case "$COMMAND" in
"start")
start_environment
;;
"stop")
stop_environment
;;
"restart")
stop_environment
start_environment
;;
"status")
show_status
;;
"logs")
show_logs
;;
"update")
update_images
;;
"clean")
remove_containers
;;
"compose")
direct_compose $COMPOSE_ARGS
;;
*)
show_help
;;
esac