-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshellguard.sh
More file actions
executable file
·586 lines (507 loc) · 18.6 KB
/
shellguard.sh
File metadata and controls
executable file
·586 lines (507 loc) · 18.6 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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
#!/bin/bash
# shellguard.sh - shellguard.sh - shellguard.sh - Your shell's bodyguard - because prevention is better than recovery
#
# Copyright (c) 2025 WRONAI - Tom Sapletta
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# shellguard.sh - shellguard.sh - Your shell's bodyguard - because prevention is better than recovery
#
# shellguard.sh - Your shell's bodyguard - because prevention is better than recovery
#
# ========================================
# PERFORMANCE CONFIGURATION
# ========================================
MAX_FIND_DEPTH=2
MAX_FILES_TO_CHECK=20
SCAN_TIMEOUT=3
MAX_MONITOR_FILES=1000
# ========================================
# COLORS AND ICONS
# ========================================
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# ========================================
# SYSTEM STATE
# ========================================
LLM_GUARD_DIR="$HOME/.llm_guard"
STATE_FILE="$LLM_GUARD_DIR/state.json"
BACKUP_DIR="$LLM_GUARD_DIR/backups"
BLOCKED_PATTERNS_FILE="$LLM_GUARD_DIR/blocked.txt"
# ========================================
# SAFE INITIALIZATION
# ========================================
init_llm_guard() {
# Create directories safely
mkdir -p "$LLM_GUARD_DIR" "$BACKUP_DIR" 2>/dev/null || true
# Check if we have jq, if not - use simpler version
if ! command -v jq >/dev/null 2>&1; then
# Simple state file without jq
cat > "$STATE_FILE" << 'EOF'
{"health_score": 100, "commands_blocked": 0, "last_backup": "", "session_start": ""}
EOF
else
# Normal state file with jq
if [[ ! -f "$STATE_FILE" ]]; then
cat > "$STATE_FILE" << 'EOF'
{
"health_score": 100,
"last_backup": "",
"commands_blocked": 0,
"files_changed": 0,
"session_start": "",
"warnings": []
}
EOF
fi
# Save session start time
jq --arg time "$(date -Iseconds)" '.session_start = $time' "$STATE_FILE" > /tmp/sg_tmp.json && mv /tmp/sg_tmp.json "$STATE_FILE" 2>/dev/null || true
fi
# Create blocked patterns if doesn't exist
if [[ ! -f "$BLOCKED_PATTERNS_FILE" ]]; then
cat > "$BLOCKED_PATTERNS_FILE" << 'EOF'
rm -rf
sudo rm
DROP TABLE
DELETE FROM
os.system(
eval(
exec(
subprocess.call(.*shell=True
--force
--hard
/dev/
mkfs
fdisk
format
EOF
fi
echo -e "${GREEN}🛡️ LLM Guard activated!${NC}"
echo -e "${BLUE}📊 Use 'status' to check system health${NC}"
echo -e "${BLUE}🔒 Use 'backup' before big changes${NC}"
echo -e "${BLUE}🔄 Use 'rollback' if something breaks${NC}"
}
# ========================================
# SECURITY CHECKING
# ========================================
check_dangerous_command() {
local cmd="$1"
while IFS= read -r pattern; do
[[ -z "$pattern" ]] && continue
if [[ "$cmd" == *"$pattern"* ]]; then
echo -e "${RED}🚨 BLOCKED: Dangerous pattern detected: $pattern${NC}"
echo -e "${RED}💀 Command: $cmd${NC}"
# Increase blocked commands counter (simple version)
if command -v jq >/dev/null 2>&1; then
local blocked=$(jq -r '.commands_blocked // 0' "$STATE_FILE" 2>/dev/null || echo "0")
jq --argjson blocked "$((blocked + 1))" '.commands_blocked = $blocked' "$STATE_FILE" > /tmp/sg_tmp.json && mv /tmp/sg_tmp.json "$STATE_FILE" 2>/dev/null || true
fi
return 1
fi
done < "$BLOCKED_PATTERNS_FILE"
return 0
}
# ========================================
# IMPROVED PROJECT HEALTH CHECK
# ========================================
check_project_health() {
echo -e "${BLUE}🔍 Checking project health (smart scan)...${NC}"
local issues=0
local health_score=100
local files_checked=0
# Check current directory file count to adjust scanning
local total_files=$(find . -maxdepth $MAX_FIND_DEPTH -type f 2>/dev/null | wc -l)
local scan_limit=$MAX_FILES_TO_CHECK
if [[ $total_files -gt $MAX_MONITOR_FILES ]]; then
echo -e "${YELLOW}⚠️ Large project detected ($total_files files), using limited scan${NC}"
scan_limit=10
fi
# Check Python syntax - LIMITED SCANNING
local python_files=$(find . -maxdepth $MAX_FIND_DEPTH -name "*.py" -type f 2>/dev/null | head -$scan_limit)
if [[ -n "$python_files" ]]; then
echo -e "${BLUE}🐍 Checking Python syntax ($scan_limit files max)...${NC}"
while IFS= read -r file && [[ $files_checked -lt $scan_limit ]]; do
[[ -z "$file" ]] && continue
if ! timeout $SCAN_TIMEOUT python -m py_compile "$file" 2>/dev/null; then
echo -e "${RED}❌ Syntax error in: $file${NC}"
((issues++))
((health_score -= 10))
fi
((files_checked++))
done <<< "$python_files"
if [[ $files_checked -gt 0 ]]; then
echo -e "${GREEN}✅ Checked $files_checked Python files${NC}"
fi
fi
# Check JavaScript/Node - QUICK CHECK
if [[ -f "package.json" ]]; then
echo -e "${BLUE}📦 Checking Node.js project...${NC}"
if timeout $SCAN_TIMEOUT node -e "JSON.parse(require('fs').readFileSync('package.json', 'utf8'))" 2>/dev/null; then
echo -e "${GREEN}✅ Valid package.json${NC}"
else
echo -e "${RED}❌ Invalid package.json${NC}"
((issues++))
((health_score -= 15))
fi
fi
# Check Git status - FAST
if [[ -d ".git" ]]; then
local changed_files=$(timeout $SCAN_TIMEOUT git status --porcelain 2>/dev/null | wc -l)
if [[ $changed_files -gt 10 ]]; then
echo -e "${YELLOW}⚠️ Many files changed: $changed_files${NC}"
((health_score -= 5))
fi
fi
# Check error logs - LIMITED
local error_logs=$(find . -maxdepth 2 -name "*.log" -type f 2>/dev/null | head -3)
if [[ -n "$error_logs" ]] && [[ "$error_logs" != "" ]]; then
if echo "$error_logs" | xargs timeout $SCAN_TIMEOUT grep -l "ERROR\|FATAL\|CRITICAL" 2>/dev/null | head -1 | grep -q .; then
echo -e "${YELLOW}⚠️ Error logs found${NC}"
((health_score -= 5))
fi
fi
# Save health score
if command -v jq >/dev/null 2>&1; then
jq --argjson score "$health_score" '.health_score = $score' "$STATE_FILE" > /tmp/sg_tmp.json && mv /tmp/sg_tmp.json "$STATE_FILE" 2>/dev/null || true
fi
# Summary
if [[ $issues -eq 0 ]]; then
echo -e "${GREEN}🎉 Project health: GOOD (Score: $health_score/100)${NC}"
return 0
else
echo -e "${RED}🚨 Project health: ISSUES FOUND ($issues issues, Score: $health_score/100)${NC}"
return 1
fi
}
# ========================================
# IMPROVED STATUS DISPLAY
# ========================================
show_status() {
echo -e "${BLUE}📊 LLM GUARD STATUS${NC}"
echo "=================================="
# Safe state reading with fallbacks
local health_score=100
local commands_blocked=0
local last_backup=""
local session_start=""
if command -v jq >/dev/null 2>&1 && [[ -f "$STATE_FILE" ]]; then
health_score=$(jq -r '.health_score // 100' "$STATE_FILE" 2>/dev/null || echo "100")
commands_blocked=$(jq -r '.commands_blocked // 0' "$STATE_FILE" 2>/dev/null || echo "0")
last_backup=$(jq -r '.last_backup // ""' "$STATE_FILE" 2>/dev/null || echo "")
session_start=$(jq -r '.session_start // ""' "$STATE_FILE" 2>/dev/null || echo "")
fi
# Health score with colors
if [[ $health_score -ge 80 ]]; then
echo -e "Health Score: ${GREEN}$health_score/100${NC} ✅"
elif [[ $health_score -ge 60 ]]; then
echo -e "Health Score: ${YELLOW}$health_score/100${NC} ⚠️"
else
echo -e "Health Score: ${RED}$health_score/100${NC} 🚨"
fi
echo -e "Commands Blocked: ${RED}$commands_blocked${NC}"
echo -e "Last Backup: ${BLUE}$last_backup${NC}"
echo -e "Session Started: ${BLUE}$session_start${NC}"
# QUICK check for dangerous files - current directory only
local dangerous_count=$(find . -maxdepth 1 -name "*.py" -type f -exec grep -l "os\.system\|eval(\|exec(" {} \; 2>/dev/null | wc -l)
if [[ $dangerous_count -gt 0 ]]; then
echo -e "${RED}🚨 WARNING: $dangerous_count files with dangerous patterns in current directory!${NC}"
fi
echo "=================================="
}
# ========================================
# QUICK CHECK FOR LLM
# ========================================
check() {
echo -e "${BLUE}🔍 Quick health check...${NC}"
local issues=0
# Very quick check - only obvious problems
if [[ -f "package.json" ]] && ! timeout 2 node -e "JSON.parse(require('fs').readFileSync('package.json', 'utf8'))" 2>/dev/null; then
((issues++))
fi
# Check git status
if [[ -d ".git" ]]; then
local changed=$(timeout 2 git status --porcelain 2>/dev/null | wc -l)
if [[ $changed -gt 20 ]]; then
((issues++))
fi
fi
# Check for obvious Python syntax errors in current directory
if find . -maxdepth 1 -name "*.py" -type f | head -5 | xargs -I {} timeout 2 python -m py_compile {} 2>&1 | grep -q "SyntaxError"; then
((issues++))
fi
if [[ $issues -eq 0 ]]; then
echo -e "${GREEN}✅ SYSTEM OK${NC}"
return 0
else
echo -e "${RED}❌ ISSUES DETECTED${NC}"
echo -e "${YELLOW}Run 'health' for details${NC}"
return 1
fi
}
# ========================================
# AUTO-BACKUP
# ========================================
create_backup() {
local timestamp=$(date +"%Y%m%d_%H%M%S")
local backup_name="backup_$timestamp"
echo -e "${YELLOW}📦 Creating backup: $backup_name${NC}"
# Git backup if it's a repo
if [[ -d ".git" ]]; then
git add . 2>/dev/null || true
if git commit -m "LLM Guard auto-backup $timestamp" 2>/dev/null; then
echo -e "${GREEN}✅ Git backup created${NC}"
fi
fi
# File backup
local backup_path="$BACKUP_DIR/$backup_name"
mkdir -p "$backup_path" 2>/dev/null || true
if command -v rsync >/dev/null 2>&1; then
rsync -a --exclude='.git' --exclude='node_modules' --exclude='__pycache__' \
--exclude='.llm_guard' . "$backup_path/" 2>/dev/null && \
echo -e "${GREEN}✅ File backup created${NC}"
else
cp -r . "$backup_path/" 2>/dev/null && \
echo -e "${GREEN}✅ File backup created${NC}"
fi
# Save backup info
if command -v jq >/dev/null 2>&1; then
jq --arg backup "$backup_name" '.last_backup = $backup' "$STATE_FILE" > /tmp/sg_tmp.json && mv /tmp/sg_tmp.json "$STATE_FILE" 2>/dev/null || true
fi
return 0
}
# ========================================
# ROLLBACK
# ========================================
rollback_changes() {
echo -e "${YELLOW}🔄 Rolling back changes...${NC}"
# Git rollback if possible
if [[ -d ".git" ]] && git log --oneline -1 2>/dev/null | grep -q "LLM Guard auto-backup"; then
if git reset --hard HEAD~1 2>/dev/null; then
echo -e "${GREEN}✅ Git rollback completed${NC}"
return 0
fi
fi
# File rollback from latest backup
local last_backup=""
if command -v jq >/dev/null 2>&1; then
last_backup=$(jq -r '.last_backup // ""' "$STATE_FILE" 2>/dev/null || echo "")
fi
if [[ -n "$last_backup" && -d "$BACKUP_DIR/$last_backup" ]]; then
echo -e "${YELLOW}🔄 Restoring from backup: $last_backup${NC}"
# Remove everything except hidden system files
find . -maxdepth 1 -not -name '.*' -not -name '.git' -exec rm -rf {} + 2>/dev/null || true
# Restore from backup
if cp -r "$BACKUP_DIR/$last_backup/"* . 2>/dev/null; then
echo -e "${GREEN}✅ Files restored from backup${NC}"
return 0
fi
fi
echo -e "${RED}❌ No backup found for rollback${NC}"
return 1
}
# ========================================
# MONITORING
# ========================================
monitor_changes() {
# Check only if it's git repo and only quick check
if [[ -d ".git" ]]; then
local changed=$(timeout 2 git status --porcelain 2>/dev/null | wc -l)
if [[ $changed -gt 15 ]]; then
echo -e "${YELLOW}⚠️ WARNING: $changed files changed! Consider backup.${NC}"
fi
fi
}
start_monitor() {
# Start monitor with longer intervals for large projects
local interval=60
# If many files, increase interval
local file_count=$(find . -maxdepth 2 -type f 2>/dev/null | wc -l)
if [[ $file_count -gt $MAX_MONITOR_FILES ]]; then
interval=300 # 5 minutes for large projects
fi
(
while true; do
sleep $interval
monitor_changes 2>/dev/null || true
done
) &
MONITOR_PID=$!
echo -e "${GREEN}🔍 Background monitor started (PID: $MONITOR_PID)${NC}"
}
stop_monitor() {
if [[ -n "$MONITOR_PID" ]]; then
kill $MONITOR_PID 2>/dev/null || true
echo -e "${BLUE}🔍 Background monitor stopped${NC}"
fi
}
# ========================================
# COMMAND OVERRIDES
# ========================================
# Override rm
rm() {
local cmd="rm $*"
if ! check_dangerous_command "$cmd"; then
echo -e "${RED}Use 'safe_rm' if you really need to delete files${NC}"
return 1
fi
command rm "$@"
}
# Override git with dangerous flags
git() {
local cmd="git $*"
if ! check_dangerous_command "$cmd"; then
echo -e "${RED}Use 'force_git' if you really need this git command${NC}"
return 1
fi
command git "$@"
}
# Override python for dangerous scripts
python() {
local file="$1"
if [[ -f "$file" ]]; then
# Check if file contains dangerous patterns
if timeout 2 grep -q "os\.system\|eval(\|exec(\|subprocess.*shell=True" "$file" 2>/dev/null; then
echo -e "${RED}🚨 Dangerous Python code detected in: $file${NC}"
echo -e "${YELLOW}Content preview:${NC}"
grep -n "os\.system\|eval(\|exec(\|subprocess.*shell=True" "$file" 2>/dev/null | head -3
echo -e "${RED}Use 'force_python $file' to run anyway${NC}"
return 1
fi
fi
command python "$@"
}
# Override npm/yarn
npm() {
local cmd="npm $*"
# Check if it's installing new packages
if [[ "$1" == "install" && "$#" -gt 1 ]]; then
echo -e "${YELLOW}🔍 Installing new packages: ${*:2}${NC}"
echo -e "${BLUE}Creating backup before package installation...${NC}"
create_backup
fi
command npm "$@"
}
# ========================================
# SAFE COMMANDS (bypass blocks)
# ========================================
safe_rm() {
echo -e "${YELLOW}⚠️ Using SAFE RM - creating backup first${NC}"
create_backup
command rm "$@"
}
force_git() {
echo -e "${YELLOW}⚠️ Using FORCE GIT - creating backup first${NC}"
create_backup
command git "$@"
}
force_python() {
echo -e "${YELLOW}⚠️ Using FORCE PYTHON - proceed with caution${NC}"
command python "$@"
}
# ========================================
# HELPER COMMANDS
# ========================================
backup() {
create_backup
}
rollback() {
rollback_changes
}
status() {
show_status
}
health() {
check_project_health
}
# Emergency reset
emergency() {
echo -e "${RED}🚨 EMERGENCY MODE${NC}"
echo -e "${YELLOW}This will rollback to last known good state${NC}"
read -p "Continue? (y/N): " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
rollback_changes
fi
}
# Add pattern to blocklist
block() {
local pattern="$1"
if [[ -n "$pattern" ]]; then
echo "$pattern" >> "$BLOCKED_PATTERNS_FILE"
echo -e "${GREEN}✅ Added pattern to blocklist: $pattern${NC}"
else
echo -e "${RED}Usage: block 'dangerous_pattern'${NC}"
fi
}
# ========================================
# HELP
# ========================================
llm_help() {
echo -e "${BLUE}🛡️ LLM GUARD COMMANDS${NC}"
echo "=================================="
echo -e "${GREEN}status${NC} - Show system status"
echo -e "${GREEN}health${NC} - Check project health"
echo -e "${GREEN}check${NC} - Quick health check"
echo -e "${GREEN}backup${NC} - Create backup"
echo -e "${GREEN}rollback${NC} - Restore from backup"
echo -e "${GREEN}emergency${NC} - Emergency rollback"
echo -e "${GREEN}block 'pattern'${NC} - Add dangerous pattern"
echo ""
echo -e "${YELLOW}SAFE COMMANDS (bypass blocks):${NC}"
echo -e "${GREEN}safe_rm${NC} - Safe delete with backup"
echo -e "${GREEN}force_git${NC} - Force git with backup"
echo -e "${GREEN}force_python${NC} - Force Python execution"
echo ""
echo -e "${BLUE}EXAMPLES FOR LLM:${NC}"
echo " python myfile.py # Checked automatically"
echo " npm install react # Auto-backup before install"
echo " git commit -m 'msg' # Safe git operations"
echo " rm -rf folder # BLOCKED! Use safe_rm instead"
}
# ========================================
# INITIALIZATION WHEN SOURCED
# ========================================
if [[ "${BASH_SOURCE[0]}" != "${0}" ]]; then
# Script is being sourced
init_llm_guard
# Start monitor in background
start_monitor
# Trap on exit to stop monitor
trap 'stop_monitor' EXIT
echo -e "${GREEN}🎯 Type 'llm_help' for commands${NC}"
echo -e "${BLUE}🚀 LLM can now use: python, npm, git (all monitored)${NC}"
echo -e "${YELLOW}🔒 Dangerous commands are blocked automatically${NC}"
fi
# ========================================
# IF RUN AS SCRIPT
# ========================================
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
# Run as script - execute command
case "$1" in
"install")
echo "Installing LLM Guard..."
echo "Add this to your ~/.bashrc or ~/.zshrc:"
echo "source $(realpath "$0")"
;;
"status"|"health"|"check"|"backup"|"rollback"|"emergency")
init_llm_guard >/dev/null 2>&1
$1
;;
*)
echo "Usage: $0 {install|status|health|check|backup|rollback|emergency}"
echo "Or: source $0 (to activate LLM Guard)"
;;
esac
fi