-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab.sh
More file actions
186 lines (161 loc) · 6.27 KB
/
lab.sh
File metadata and controls
186 lines (161 loc) · 6.27 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
#!/bin/bash
# =============================================================================
# VULNLAB - Vulnerable Applications Laboratory
# Environment for pentesting, security training, and scanner testing
#
# Author: VulnLab Project
# Version: 2.1.0
# =============================================================================
set -e
# Load common library
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "${SCRIPT_DIR}/lib/common.sh"
# Compatibility with old functions (alias)
show_progress() { log_progress "$1"; }
show_success() { log_success "$1"; }
# Banner
show_banner "VULNLAB - Vulnerability Lab" "Vulnerable Applications Laboratory for Pentesting"
# Detect docker compose command
COMPOSE_CMD=$(detect_compose_cmd) || exit 1
# Note: setup_traps removed as it interferes with exit codes in simple scripts
# For more complex scripts with cleanup, enable: setup_traps
# Validate if docker-compose.yml exists
require_file "${SCRIPT_DIR}/docker-compose.yml" || exit 1
# Function to validate if a service exists in compose
validate_service() {
local service="$1"
local all_services
all_services=$($COMPOSE_CMD config --services 2>/dev/null)
if ! echo "$all_services" | grep -qx "$service"; then
log_warn "Service '$service' not found in docker-compose.yml"
return 1
fi
return 0
}
# Main menu
case "${1:-}" in
pull)
show_progress "Pulling all Docker images..."
$COMPOSE_CMD pull --ignore-pull-failures
show_success "Download complete!"
;;
start)
# Shift past the 'start' command
shift
if [ "$#" -gt 0 ]; then
show_progress "Starting a subset of containers (Smoke Test)..."
SERVICES=""
# Validate each service before adding to the list
for svc in "$@"; do
if validate_service "$svc"; then
SERVICES="$SERVICES $svc"
fi
done
SERVICES=$(echo "$SERVICES" | xargs) # Trim whitespace
if [ -z "$SERVICES" ]; then
log_error "No valid services specified."
exit 1
fi
else
show_progress "Starting all containers resiliently..."
SERVICES=$($COMPOSE_CMD config --services)
fi
# Ensure the logs directory exists
ensure_dir logs
# Success/failure counter
SUCCESS_COUNT=0
FAIL_COUNT=0
for SERVICE in $SERVICES; do
echo -e "${COLOR_YELLOW}--> Starting service: $SERVICE...${COLOR_NC}"
# Try to start the service, capturing output
if output=$($COMPOSE_CMD up -d --no-deps "$SERVICE" 2>&1); then
echo -e "${COLOR_GREEN} Service $SERVICE started successfully.${COLOR_NC}"
((SUCCESS_COUNT++)) || true
else
echo -e "${COLOR_RED} ERROR starting service $SERVICE. Saving log to logs/$SERVICE.log${COLOR_NC}"
echo "$output" > "logs/$SERVICE.log"
((FAIL_COUNT++)) || true
fi
done
show_success "Startup process complete!"
echo ""
echo -e "Summary: ${COLOR_GREEN}$SUCCESS_COUNT successful${COLOR_NC} | ${COLOR_RED}$FAIL_COUNT failed${COLOR_NC}"
echo -e "${COLOR_BLUE}Wait for services to initialize completely.${COLOR_NC}"
true # Ensure exit code 0
;;
stop)
show_progress "Stopping all containers..."
$COMPOSE_CMD down
show_success "Containers stopped!"
;;
status)
echo -e "${COLOR_BLUE}=== STATUS OF ALL CONTAINERS ===${COLOR_NC}"
$COMPOSE_CMD ps -a
echo ""
echo -e "${COLOR_BLUE}=== SUMMARY ===${COLOR_NC}"
# Use docker directly for more reliable counting
RUNNING=$(docker ps -q 2>/dev/null | wc -l)
TOTAL=$(docker ps -a -q 2>/dev/null | wc -l)
EXITED=$((TOTAL - RUNNING))
echo -e "Running: ${COLOR_GREEN}$RUNNING${COLOR_NC}"
echo -e "Stopped/Errored: ${COLOR_RED}$EXITED${COLOR_NC}"
echo -e "Total: $TOTAL"
;;
logs)
if [ -z "${2:-}" ]; then
echo "Usage: $0 logs <container-name>"
exit 1
fi
$COMPOSE_CMD logs -f "$2"
;;
ips)
echo -e "${COLOR_BLUE}=== CONTAINER IPs ===${COLOR_NC}"
get_container_ips
;;
scan-targets)
echo -e "${COLOR_BLUE}=== TARGET LIST FOR OPENVAS ===${COLOR_NC}"
echo "Networks: 172.30.0.0/15"
echo ""
echo "Or use specific IPs:"
get_container_ips | awk '{print $3}'
;;
export-targets)
echo -e "${COLOR_BLUE}Exporting IP list to targets.txt...${COLOR_NC}"
get_container_ips | awk '{print $3}' > targets.txt
show_success "File targets.txt created with $(wc -l < targets.txt) targets!"
;;
restart)
show_progress "Restarting all containers..."
$COMPOSE_CMD restart
show_success "Containers restarted!"
;;
clean)
show_progress "Stopping and removing compose containers..."
$COMPOSE_CMD down -v
show_progress "Removing any remaining stopped containers..."
docker container prune -f
show_success "Cleanup complete!"
;;
stats)
echo -e "${COLOR_BLUE}=== RESOURCE STATISTICS ===${COLOR_NC}"
docker stats --no-stream
;;
*)
echo "Usage: $0 {pull|start|stop|status|logs|ips|scan-targets|export-targets|restart|clean|stats}"
echo "For smoke test: $0 start <service1> <service2> ..."
echo ""
echo "Commands:"
echo " pull - Download all Docker images"
echo " start - Start all containers (or a subset)"
echo " stop - Stop all containers"
echo " status - View status of containers"
echo " logs <name> - View logs of a specific container"
echo " ips - List IPs of all containers"
echo " scan-targets - Show list of targets for scanning"
echo " export-targets- Export IPs to targets.txt file"
echo " restart - Restart all containers"
echo " clean - Remove containers and volumes"
echo " stats - View resource usage"
exit 1
;;
esac