-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·470 lines (394 loc) · 13.1 KB
/
setup.sh
File metadata and controls
executable file
·470 lines (394 loc) · 13.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
462
463
464
465
466
467
468
469
470
#!/bin/bash
#
# secopsai Setup Script
# Installs and configures the OpenClaw security detection pipeline with optional features.
#
# Usage:
# curl -fsSL https://secopsai.dev/install.sh | bash
# OR
# bash setup.sh
#
if [ -z "${BASH_VERSION:-}" ]; then
echo "This installer requires bash."
echo "Run: curl -fsSL https://secopsai.dev/install.sh | bash"
exit 1
fi
set -euo pipefail
AUTO_YES=0
NON_INTERACTIVE=0
while [[ $# -gt 0 ]]; do
case "$1" in
--help|-h)
cat <<'EOF'
secopsai setup
Usage:
bash setup.sh
bash setup.sh --yes
bash setup.sh --non-interactive
bash setup.sh --help
What it does:
1. Validates local prerequisites
2. Creates .venv and installs dependencies
3. Enables optional benchmark and live-export features
4. Runs validation and initial setup tasks
Notes:
- If run via curl pipe, defaults are used automatically
- OpenClaw CLI is optional for base install
EOF
exit 0
;;
--yes)
AUTO_YES=1
;;
--non-interactive)
NON_INTERACTIVE=1
;;
*)
echo "Unknown option: $1"
echo "Run 'bash setup.sh --help' for usage."
exit 2
;;
esac
shift
done
if [[ ! -t 0 ]]; then
NON_INTERACTIVE=1
fi
# Colors for output
if [[ -t 1 ]]; then
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
else
RED=''
GREEN=''
YELLOW=''
BLUE=''
NC=''
fi
# Configuration
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
VENV_DIR="${SCRIPT_DIR}/.venv"
BACKUP_DIR="${SCRIPT_DIR}/.backups"
TEMP_DIR="${SECOPSAI_TMP_DIR:-${TMPDIR:-/tmp}}"
PREPARE_LOG="${TEMP_DIR%/}/secopsai_prepare.log"
BENCHMARK_LOG="${TEMP_DIR%/}/benchmark.log"
EXPORT_LOG="${TEMP_DIR%/}/export.log"
# Logging functions
log_info() {
echo -e "${BLUE}ℹ${NC} $1"
}
log_success() {
echo -e "${GREEN}✓${NC} $1"
}
log_error() {
echo -e "${RED}✗${NC} $1"
}
log_warn() {
echo -e "${YELLOW}⚠${NC} $1"
}
prompt_yes_no() {
local prompt="$1"
local default="${2:-N}"
if [[ "$AUTO_YES" == "1" ]]; then
[[ "$default" == "Y" ]]
return
fi
if [[ "$NON_INTERACTIVE" == "1" ]]; then
if [[ "$default" == "Y" ]]; then
log_info "$prompt -> defaulting to Yes (non-interactive mode)"
return 0
fi
log_info "$prompt -> defaulting to No (non-interactive mode)"
return 1
fi
if [[ "$default" == "Y" ]]; then
echo -ne "${prompt} (Y/n) "
else
echo -ne "${prompt} (y/N) "
fi
local response=""
if [[ -r /dev/tty ]]; then
read -r response </dev/tty || response=""
else
response=""
fi
if [[ "$default" == "Y" ]]; then
[[ -z "$response" || "$response" == "y" || "$response" == "Y" ]]
else
[[ "$response" == "y" || "$response" == "Y" ]]
fi
}
# ============================================================================
# PHASE 1: PRE-FLIGHT CHECKS
# ============================================================================
phase_preflight_checks() {
log_info "Running pre-flight checks..."
echo ""
local all_passed=true
# Check Python 3
if command -v python3 &> /dev/null; then
local python_version
python_version=$(python3 --version 2>&1 | awk '{print $2}')
log_success "Python 3 found (version $python_version)"
else
log_error "Python 3 is required but not installed"
all_passed=false
fi
# Check pip
if command -v pip3 &> /dev/null; then
log_success "pip3 found"
else
log_error "pip3 is required but not installed"
all_passed=false
fi
# Check OpenClaw
if command -v openclaw &> /dev/null; then
log_success "OpenClaw CLI found"
else
log_warn "OpenClaw CLI not found. Install from: https://docs.openclaw.ai/install"
fi
# Check git
if command -v git &> /dev/null; then
log_success "git found"
else
log_error "git is required but not installed"
all_passed=false
fi
# Check python pip module
if python3 -m pip --version &> /dev/null; then
log_success "Python pip module found"
else
log_error "python3 -m pip is required but not installed"
all_passed=false
fi
echo ""
if [[ "$all_passed" == false ]]; then
log_error "Some prerequisites are missing. Please install them and re-run this script."
exit 1
fi
log_success "All pre-flight checks passed!"
echo ""
}
# ============================================================================
# PHASE 2: PYTHON ENVIRONMENT SETUP
# ============================================================================
phase_setup_environment() {
log_info "Setting up Python environment..."
echo ""
# Create virtual environment
if [[ ! -d "$VENV_DIR" ]]; then
log_info "Creating Python virtual environment..."
python3 -m venv "$VENV_DIR"
log_success "Virtual environment created"
else
log_success "Virtual environment already exists"
fi
# Activate virtual environment
source "$VENV_DIR/bin/activate"
# Upgrade pip
log_info "Upgrading pip..."
python3 -m pip install --quiet --upgrade pip setuptools wheel
log_success "pip upgraded"
# Install requirements
if [[ -f "$SCRIPT_DIR/requirements.txt" ]]; then
log_info "Installing dependencies from requirements.txt..."
python3 -m pip install --quiet -r "$SCRIPT_DIR/requirements.txt"
python3 -m pip install --quiet pytest
# Install secopsai CLI into the venv so `secopsai` is available
if python3 -m pip install --quiet -e "$SCRIPT_DIR"; then
log_success "Dependencies installed (and secopsai CLI installed)"
else
log_warn "Dependencies installed but failed to install secopsai CLI (editable install)"
fi
else
log_warn "requirements.txt not found, skipping dependency installation"
fi
echo ""
}
# ============================================================================
# PHASE 3: FEATURE CONFIGURATION
# ============================================================================
phase_feature_configuration() {
log_info "Configuring optional features..."
echo ""
# Feature 1: Enable optional telemetry surfaces
log_info "Feature 1: Optional Native Telemetry Surfaces"
echo " Enables detection of additional attack surfaces:"
echo " - exec_events: Process-level command execution"
echo " - pairing_events: Agent pairing/approval workflows"
echo " - skills_events: Skill installation/source drift"
echo ""
if prompt_yes_no "Enable optional native surfaces?" "N"; then
export SECOPS_ENABLE_OPTIONAL_SURFACES=1
log_success "Optional surfaces enabled"
else
export SECOPS_ENABLE_OPTIONAL_SURFACES=0
log_success "Optional surfaces disabled"
fi
echo ""
# Feature 2: Benchmark validation
log_info "Feature 2: Benchmark Attack-Mix Generation"
echo " Generates reproducible labeled attack corpus for validation:"
echo " - 80-event dataset with 22 simulated attacks"
echo " - Tests detection rules with known attack patterns"
echo " - Validates F1 score and rule accuracy"
echo ""
if prompt_yes_no "Enable benchmark validation on setup?" "Y"; then
export SECOPS_ENABLE_BENCHMARK=1
log_success "Benchmark validation enabled"
else
export SECOPS_ENABLE_BENCHMARK=0
log_success "Benchmark validation disabled"
fi
echo ""
# Feature 3: Live telemetry export
log_info "Feature 3: Live Telemetry Export"
echo " Exports your local OpenClaw audit logs for detection:"
echo " - Requires OpenClaw CLI and ~/.openclaw/ directory"
echo " - Runs detection pipeline on live telemetry"
echo " - Reports security findings"
echo ""
if prompt_yes_no "Enable live telemetry export?" "N"; then
if ! command -v openclaw &> /dev/null; then
log_error "OpenClaw CLI not found, cannot enable live export"
export SECOPS_ENABLE_LIVE_EXPORT=0
else
export SECOPS_ENABLE_LIVE_EXPORT=1
log_success "Live telemetry export enabled"
fi
else
export SECOPS_ENABLE_LIVE_EXPORT=0
log_success "Live telemetry export disabled"
fi
echo ""
}
# ============================================================================
# PHASE 4: INITIALIZATION & VALIDATION
# ============================================================================
phase_initialization() {
log_info "Initializing secopsai..."
echo ""
# Create backup directory
mkdir -p "$BACKUP_DIR"
# Create data directories
mkdir -p "$SCRIPT_DIR/data/openclaw/raw"
mkdir -p "$SCRIPT_DIR/data/openclaw/replay/labeled"
mkdir -p "$SCRIPT_DIR/data/openclaw/replay/unlabeled"
log_success "Data directories created"
# Ensure baseline regression dataset exists for evaluate.py/tests on fresh clones.
if [[ ! -f "$SCRIPT_DIR/data/events.json" || ! -f "$SCRIPT_DIR/data/events_unlabeled.json" ]]; then
log_info "Generating baseline dataset (data/events*.json)..."
if python3 "$SCRIPT_DIR/prepare.py" > "$PREPARE_LOG" 2>&1; then
log_success "Baseline dataset generated"
else
log_warn "Failed to generate baseline dataset (install may continue)"
log_warn "See $PREPARE_LOG for details"
fi
else
log_success "Baseline dataset already present"
fi
# Run validation tests
log_info "Running validation tests..."
source "$VENV_DIR/bin/activate"
# Generate OpenClaw sample fixtures expected by tests (best-effort).
if python3 "$SCRIPT_DIR/scripts/generate_openclaw_samples.py" >/dev/null 2>&1; then
log_success "OpenClaw sample fixtures ready"
else
log_warn "Failed to generate OpenClaw sample fixtures (tests may fail)"
fi
if python3 -m pytest tests/ -q 2>/dev/null; then
log_success "All validation tests passed"
else
log_warn "Some validation tests failed (this may be OK on first setup)"
fi
echo ""
}
# ============================================================================
# PHASE 5: FEATURE EXECUTION
# ============================================================================
phase_feature_execution() {
log_info "Executing enabled features..."
echo ""
source "$VENV_DIR/bin/activate"
# Benchmark validation
if [[ "$SECOPS_ENABLE_BENCHMARK" == "1" ]]; then
log_info "Generating attack-mix benchmark..."
if python3 generate_openclaw_attack_mix.py --stats > "$BENCHMARK_LOG" 2>&1; then
log_success "Attack-mix benchmark generated"
echo ""
python3 generate_openclaw_attack_mix.py --stats
else
log_warn "Failed to generate benchmark (install may continue)"
fi
echo ""
fi
# Live telemetry export
if [[ "$SECOPS_ENABLE_LIVE_EXPORT" == "1" ]]; then
log_info "Exporting live OpenClaw telemetry..."
if python3 export_real_openclaw_native.py > "$EXPORT_LOG" 2>&1; then
log_success "Live telemetry exported"
else
log_warn "Failed to export live telemetry"
fi
echo ""
fi
}
# ============================================================================
# CLEANUP & SUMMARY
# ============================================================================
summary() {
echo ""
echo "======================================================================="
log_success "Setup complete!"
echo "======================================================================="
echo ""
echo "Next steps:"
echo ""
echo "1. Activate the virtual environment:"
echo " ${BLUE}source $VENV_DIR/bin/activate${NC}"
echo ""
echo "2. Run detection on your OpenClaw logs:"
echo " ${BLUE}python detect.py${NC}"
echo ""
echo "Or use the new CLI (after activating venv):"
echo " ${BLUE}secopsai refresh${NC}"
echo " ${BLUE}secopsai list --severity high${NC}"
echo " ${BLUE}secopsai show OCF-...${NC}"
echo ""
echo "3. Evaluate benchmark performance:"
echo " ${BLUE}python evaluate_openclaw.py --labeled data/openclaw/replay/labeled/attack_mix.json --unlabeled data/openclaw/replay/unlabeled/attack_mix.json --mode benchmark --verbose${NC}"
echo ""
echo "4. Build and view findings:"
echo " ${BLUE}python findings.py${NC}"
echo ""
echo "Documentation:"
echo " ${BLUE}https://docs.secopsai.dev${NC}"
echo ""
echo "Git repository:"
echo " ${BLUE}https://github.com/Techris93/secopsai${NC}"
echo ""
}
# ============================================================================
# MAIN EXECUTION
# ============================================================================
main() {
echo ""
echo "╔════════════════════════════════════════════════════════════════════╗"
echo "║ secopsai Setup & Configuration ║"
echo "║ ║"
echo "║ Installs the OpenClaw security detection pipeline with ║"
echo "║ automated attack detection and benchmark validation. ║"
echo "╚════════════════════════════════════════════════════════════════════╝"
echo ""
phase_preflight_checks
phase_setup_environment
phase_feature_configuration
phase_initialization
phase_feature_execution
summary
}
# Run main
main "$@"