added project 2

This commit is contained in:
Hugh Ratsch
2025-02-09 02:01:58 -06:00
parent d13adede5e
commit 9ec69d3d83
10 changed files with 1146 additions and 20 deletions
@@ -0,0 +1,267 @@
#!/bin/bash
source "$(dirname "$0")/common.sh"
# Challenge categories and levels
declare -A CHALLENGE_CATEGORIES=(
["file"]="File System Operations"
["perm"]="Permissions & Security"
["text"]="Text Processing"
["proc"]="Process Management"
["net"]="Network Operations"
)
# Challenge definitions with detailed metadata
# Format: type|name|difficulty|skills|task|hints|setup_requirements|verification_criteria
declare -A CHALLENGES=(
# Level 1: File Operations
["1.1"]="file_search|Find Recent Files|1|file_ops,search|Find all .conf files modified in the last 24 hours and save the list to recent_configs.txt|Try using find with -mtime|none|output_file=recent_configs.txt;file_count=10"
["1.2"]="file_count|File Statistics|1|file_ops,search|Generate a report showing count of files by extension|Consider using find and awk|none|report_exists=true;extension_count=5"
["1.3"]="file_organize|Smart File Organizer|2|file_ops|Create directories for different file types and sort files accordingly|Look into file command|none|dir_structure=complete;files_sorted=true"
["1.4"]="file_cleanup|System Cleanup|2|file_ops,search|Find and list all empty files and zero-byte files|Combine find with size tests|none|empty_files_found=true;report_generated=true"
["1.5"]="file_dedup|Duplicate Hunter|3|file_ops,search|Find and handle duplicate files using md5sum|Consider using sort and uniq|none|duplicates_identified=true;report_accurate=true"
# Level 2: Permissions
["2.1"]="perm_basic|Permission Fundamentals|1|permissions|Set up basic file permissions following security best practices|Remember user-group-others order|users=app_user1|permissions_correct=true;ownership_correct=true"
["2.2"]="perm_acl|ACL Master|2|permissions|Implement complex ACL rules for multiple users|Look into setfacl usage|users=app_user1,app_user2|acl_rules_correct=true"
["2.3"]="perm_special|Special Permission Bits|2|permissions|Configure SUID/SGID permissions correctly|Understanding special bits|none|special_bits_correct=true"
["2.4"]="perm_recursive|Directory Tree Fix|3|permissions|Fix permissions recursively while maintaining security|Consider find with exec|none|tree_permissions_correct=true"
["2.5"]="perm_audit|Security Auditor|3|permissions|Perform security audit and generate detailed report|Use stat and lsattr|none|audit_complete=true;report_detailed=true"
# Level 3: Text Processing
["3.1"]="text_search|Log Detective|1|text_proc,search|Find and analyze specific patterns in log files|grep with context|none|patterns_found=true;count_correct=true"
["3.2"]="text_extract|Data Extraction Pro|2|text_proc|Extract and format specific fields from log files|awk is your friend|none|fields_extracted=true;format_correct=true"
["3.3"]="text_transform|Format Converter|2|text_proc|Transform data between different formats|sed for substitution|none|transformation_correct=true"
["3.4"]="text_report|Report Generator|3|text_proc|Create a detailed system report from various logs|Combine multiple tools|none|report_complete=true;format_valid=true"
["3.5"]="text_analyze|Pattern Analyzer|3|text_proc,search|Analyze complex error patterns and generate statistics|Use grep with perl regex|none|analysis_complete=true;stats_accurate=true"
)
# Challenge verification functions
verify_challenge() {
local challenge_id=$1
local start_time=$2
IFS='|' read -r type name difficulty skills task hints requirements criteria <<< "${CHALLENGES[$challenge_id]}"
# Check prerequisites
if ! check_requirements "$requirements"; then
log_message "ERROR" "Challenge prerequisites not met"
return 1
}
# Record attempt
record_attempt "$challenge_id" "$start_time"
# Run specific verification
local category=${type%%_*}
case "$category" in
"file") verify_file_challenge "$challenge_id" "$criteria" ;;
"perm") verify_permission_challenge "$challenge_id" "$criteria" ;;
"text") verify_text_challenge "$challenge_id" "$criteria" ;;
*) log_message "ERROR" "Unknown challenge category: $category" ;;
esac
}
# Check challenge prerequisites
check_requirements() {
local requirements=$1
[[ "$requirements" == "none" ]] && return 0
IFS=';' read -ra REQS <<< "$requirements"
for req in "${REQS[@]}"; do
IFS='=' read -r key value <<< "$req"
case "$key" in
"users")
for user in ${value//,/ }; do
if ! id "$user" &>/dev/null; then
log_message "ERROR" "Required user $user not found"
return 1
fi
done
;;
# Add more requirement checks as needed
esac
done
return 0
}
# Verify file-related challenges
verify_file_challenge() {
local challenge_id=$1
local criteria=$2
local result=0
IFS=';' read -ra CHECKS <<< "$criteria"
for check in "${CHECKS[@]}"; do
IFS='=' read -r key value <<< "$check"
case "$key" in
"output_file")
[[ -f "$value" ]] && ((result++))
;;
"file_count")
[[ "$(find "$CHALLENGE_DIR/files" -type f | wc -l)" -eq "$value" ]] && ((result++))
;;
# Add more specific checks
esac
done
verify_result "$challenge_id" "$result" "${#CHECKS[@]}" "File challenge verification"
}
# Verify permission-related challenges
verify_permission_challenge() {
local challenge_id=$1
local criteria=$2
local result=0
IFS=';' read -ra CHECKS <<< "$criteria"
for check in "${CHECKS[@]}"; do
IFS='=' read -r key value <<< "$check"
case "$key" in
"users")
for user in ${value//,/ }; do
if ! id "$user" &>/dev/null; then
log_message "ERROR" "Required user $user not found"
return 1
fi
done
;;
# Add more permission checks as needed
esac
done
verify_result "$challenge_id" "$result" "${#CHECKS[@]}" "Permission challenge verification"
}
# Verify text-related challenges
verify_text_challenge() {
local challenge_id=$1
local criteria=$2
local result=0
IFS=';' read -ra CHECKS <<< "$criteria"
for check in "${CHECKS[@]}"; do
IFS='=' read -r key value <<< "$check"
case "$key" in
"patterns_found")
[[ "$(grep -c "$value" "$CHALLENGE_DIR/files/logs/"*.log)" -ge 1 ]] && ((result++))
;;
"count_correct")
[[ "$(grep -c "$value" "$CHALLENGE_DIR/files/logs/"*.log)" -eq "$value" ]] && ((result++))
;;
# Add more text checks as needed
esac
done
verify_result "$challenge_id" "$result" "${#CHECKS[@]}" "Text challenge verification"
}
# Record challenge attempt
record_attempt() {
local challenge_id=$1
local start_time=$2
local duration=$(($(date +%s) - start_time))
local command_count=$(grep -c "^$start_time" "$COMMAND_LOG")
echo "$challenge_id,$(get_attempt_number "$challenge_id"),$start_time,$command_count,$duration,0" >> "$ATTEMPT_FILE"
}
# Get attempt number for challenge
get_attempt_number() {
local challenge_id=$1
local attempts=$(grep -c "^$challenge_id," "$ATTEMPT_FILE")
echo $((attempts + 1))
}
# Enhanced result verification with detailed feedback
verify_result() {
local challenge_id=$1
local actual=$2
local expected=$3
local message=$4
local end_time=$(date +%s)
if [[ "$actual" == "$expected" ]]; then
log_message "SUCCESS" "Challenge $challenge_id: $message"
update_challenge_status "$challenge_id" "1" "$end_time"
# Calculate bonus points
local duration=$((end_time - start_time))
local command_count=$(get_command_count "$challenge_id")
local bonus_points=$(calculate_bonus_points "$duration" "$command_count")
award_points "$challenge_id" "$((100 + bonus_points))"
return 0
else
log_message "ERROR" "Challenge $challenge_id: Completed $actual/$expected checks"
update_challenge_status "$challenge_id" "0" "$end_time"
return 1
fi
}
# Calculate bonus points based on time and efficiency
calculate_bonus_points() {
local duration=$1
local command_count=$2
local time_bonus=0
local efficiency_bonus=0
# Time bonus
if [[ "$duration" -lt 60 ]]; then
time_bonus=50
elif [[ "$duration" -lt 120 ]]; then
time_bonus=25
fi
# Efficiency bonus
if [[ "$command_count" -le 3 ]]; then
efficiency_bonus=50
elif [[ "$command_count" -le 5 ]]; then
efficiency_bonus=25
fi
echo $((time_bonus + efficiency_bonus))
}
# Get challenge details with color formatting
get_challenge_details() {
local challenge_id=$1
IFS='|' read -r type name difficulty skills task hints requirements _ <<< "${CHALLENGES[$challenge_id]}"
cat << EOF
${BOLD}Challenge ${challenge_id}${NC}: ${YELLOW}$name${NC}
${BLUE}Difficulty${NC}: $(printf '★%.0s' $(seq 1 "$difficulty"))
${BLUE}Skills${NC}: $skills
${BLUE}Task${NC}: $task
${BLUE}Hint${NC}: $hints
EOF
}
# Update challenge status
update_challenge_status() {
local challenge_id=$1
local success=$2
local end_time=$3
# Update attempt status
sed -i "$ s/,0$/,$success/" "$ATTEMPT_FILE"
# Record completion if successful
if [[ "$success" == "1" ]]; then
echo "$challenge_id,$end_time,completed,${CHALLENGES[$challenge_id]}" >> "$PROGRESS_FILE"
fi
}
# List available challenges
list_challenges() {
echo -e "\n${BOLD}Available Challenges:${NC}"
for challenge_id in "${!CHALLENGES[@]}"; do
IFS='|' read -r type name difficulty skills task hints requirements _ <<< "${CHALLENGES[$challenge_id]}"
if ! is_challenge_completed "$challenge_id"; then
echo -e "\n${YELLOW}Challenge $challenge_id${NC} ($difficulty★)"
echo "Type: $type"
echo "Skills: $skills"
echo "Task: $task"
fi
done
}
@@ -0,0 +1,259 @@
#!/bin/bash
# Directory structure
CHALLENGE_DIR=~/challenge
TRACKING_DIR="$CHALLENGE_DIR/.tracking"
DATA_DIR="$TRACKING_DIR/data"
REPORT_DIR="$TRACKING_DIR/reports"
# Data files
PROGRESS_FILE="$DATA_DIR/progress.csv"
SCORE_FILE="$DATA_DIR/score.csv"
LOG_FILE="$DATA_DIR/challenge.log"
COMMAND_LOG="$DATA_DIR/commands.log"
TIME_LOG="$DATA_DIR/time.log"
HINT_FILE="$DATA_DIR/hints_used.csv"
ACHIEVEMENT_FILE="$DATA_DIR/achievements.csv"
ATTEMPT_FILE="$DATA_DIR/attempts.csv"
SKILL_FILE="$DATA_DIR/skills.csv"
# Formatting
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
BOLD='\033[1m'
NC='\033[0m'
# Achievement definitions with categories
declare -A ACHIEVEMENTS=(
# Speed achievements
["speed_demon"]="Complete any challenge in under 1 minute|speed"
["speed_runner"]="Complete 3 challenges in under 5 minutes|speed"
# Efficiency achievements
["efficiency_expert"]="Complete a challenge using minimal commands|efficiency"
["command_master"]="Use advanced command options|efficiency"
# Learning achievements
["self_learner"]="Complete without hints|learning"
["persistent"]="Try until success|learning"
["explorer"]="Try multiple solution approaches|learning"
# Skill achievements
["file_master"]="Master file operations|skill"
["permission_guru"]="Master permission management|skill"
["search_expert"]="Master file searching|skill"
)
# Skill tracking definitions
declare -A SKILLS=(
["file_ops"]="File Operations"
["permissions"]="Permission Management"
["search"]="File Search and Analysis"
["text_proc"]="Text Processing"
["monitoring"]="System Monitoring"
)
# Logging function with severity levels
log_message() {
local severity=$1
local message=$2
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
local color
case $severity in
"INFO") color=$BLUE ;;
"SUCCESS") color=$GREEN ;;
"WARNING") color=$YELLOW ;;
"ERROR") color=$RED ;;
*) color=$NC ;;
esac
echo -e "${color}[$severity]${NC} $message"
echo "[$timestamp] [$severity] $message" >> "$LOG_FILE"
}
# Initialize environment with comprehensive setup
init_environment() {
log_message "INFO" "Initializing enhanced challenge environment..."
# Create directory structure
for dir in "$DATA_DIR" "$REPORT_DIR" "$CHALLENGE_DIR"/{files,backup,temp}; do
if ! mkdir -p "$dir"; then
log_message "ERROR" "Failed to create directory: $dir"
return 1
fi
done
# Initialize data files with headers
cat > "$PROGRESS_FILE" << EOF
challenge_id,timestamp,status,difficulty,skills_used
EOF
cat > "$SCORE_FILE" << EOF
challenge_id,points,timestamp,efficiency_bonus,speed_bonus
EOF
cat > "$ATTEMPT_FILE" << EOF
challenge_id,attempt_number,timestamp,commands_used,duration,success
EOF
cat > "$SKILL_FILE" << EOF
skill_id,level,experience,last_used
EOF
# Initialize skills
for skill in "${!SKILLS[@]}"; do
echo "$skill,1,0,0" >> "$SKILL_FILE"
done
setup_command_tracking
log_message "SUCCESS" "Environment initialized successfully"
}
# Enhanced command tracking
setup_command_tracking() {
local track_cmd='
command_status=$?
cmd=$(history 1 | cut -c 8-)
timestamp=$(date +%s)
echo "$timestamp|$command_status|$cmd" >> '"$COMMAND_LOG"'
analyze_command "$cmd"
'
if ! grep -q "PROMPT_COMMAND" ~/.bashrc; then
echo "PROMPT_COMMAND='$track_cmd'" >> ~/.bashrc
source ~/.bashrc
fi
}
# Command analysis for skill progression
analyze_command() {
local cmd=$1
case "$cmd" in
*find*) update_skill "search" 2 ;;
*chmod*|*chown*) update_skill "permissions" 2 ;;
*grep*|*sed*|*awk*) update_skill "text_proc" 2 ;;
*ps*|*top*|*netstat*) update_skill "monitoring" 2 ;;
esac
}
# Update skill progression
update_skill() {
local skill=$1
local exp_gain=$2
local current_level current_exp
IFS=, read -r _ current_level current_exp _ < <(grep "^$skill," "$SKILL_FILE")
current_exp=$((current_exp + exp_gain))
if [ $current_exp -ge $((current_level * 100)) ]; then
current_level=$((current_level + 1))
current_exp=0
log_message "SUCCESS" "🎓 Skill Level Up: ${SKILLS[$skill]} is now level $current_level!"
fi
sed -i "/^$skill,/c\\$skill,$current_level,$current_exp,$(date +%s)" "$SKILL_FILE"
}
# Enhanced progress tracking with recommendations
show_progress() {
clear
echo -e "\n${BOLD}🎮 Challenge Progress Report${NC}"
echo "=============================="
# Show statistics
show_statistics
# Show achievements by category
show_achievements
# Show skill progression
show_skills
# Show recommendations
show_recommendations
}
# Detailed statistics display
show_statistics() {
local total_score=0
local challenges_completed=0
while IFS=, read -r challenge points _ efficiency_bonus speed_bonus; do
[ -z "$challenge" ] && continue
total_score=$((total_score + points + ${efficiency_bonus:-0} + ${speed_bonus:-0}))
((challenges_completed++))
done < "$SCORE_FILE"
echo -e "\n${BOLD}📊 Statistics${NC}"
echo "----------------"
printf "Challenges Completed: %d/10\n" "$challenges_completed"
printf "Total Score: %d/1500\n" "$total_score"
printf "Average Score: %d\n" "$((total_score / (challenges_completed || 1)))"
}
# Achievement display by category
show_achievements() {
echo -e "\n${BOLD}🏆 Achievements${NC}"
echo "----------------"
local categories=("speed" "efficiency" "learning" "skill")
for category in "${categories[@]}"; do
echo -e "\n${YELLOW}${category^} Achievements:${NC}"
while IFS=, read -r achievement timestamp description; do
[ -z "$achievement" ] && continue
IFS='|' read -r achievement_desc achievement_cat <<< "${ACHIEVEMENTS[$achievement]}"
[[ "$achievement_cat" == "$category" ]] && echo "$achievement_desc"
done < "$ACHIEVEMENT_FILE"
done
}
# Skill progression display
show_skills() {
echo -e "\n${BOLD}🎓 Skills${NC}"
echo "----------------"
while IFS=, read -r skill level exp last_used; do
[ -z "$skill" ] && continue
local skill_name="${SKILLS[$skill]}"
local progress=$((exp * 100 / (level * 100)))
printf "%s (Level %d): [%-20s] %d%%\n" \
"$skill_name" "$level" \
"$(printf '#%.0s' $(seq 1 $((progress / 5))))" \
"$progress"
done < "$SKILL_FILE"
}
# Learning recommendations
show_recommendations() {
echo -e "\n${BOLD}📚 Recommendations${NC}"
echo "----------------"
# Analyze weak skills
local weakest_skill=""
local lowest_level=999
while IFS=, read -r skill level _; do
[ -z "$skill" ] && continue
if [ "$level" -lt "$lowest_level" ]; then
lowest_level=$level
weakest_skill=$skill
fi
done < "$SKILL_FILE"
[ -n "$weakest_skill" ] && echo "Focus on improving: ${SKILLS[$weakest_skill]}"
# Suggest next challenges based on skill levels
echo "Recommended next challenges:"
suggest_challenges
}
# Check if challenge is completed
is_challenge_completed() {
local challenge=$1
grep -q "^$challenge," "$SCORE_FILE" && return 0
return 1
}
@@ -0,0 +1,178 @@
#!/bin/bash
# Import common functions and variables
source "$(dirname "$0")/common.sh"
# Challenge-specific setup
setup_file_challenges() {
log_message "Setting up file system challenges..."
# Create complex directory structure
for dir in {1..5}; do
mkdir -p "$CHALLENGE_DIR/files/project$dir/{src,config,logs,data}"
# Create various file types
touch "$CHALLENGE_DIR/files/project$dir/src/main.py"
touch "$CHALLENGE_DIR/files/project$dir/src/test.py"
echo "debug=true" > "$CHALLENGE_DIR/files/project$dir/config/settings.conf"
# Create log files with realistic content
for severity in INFO WARN ERROR DEBUG; do
for i in {1..10}; do
echo "[$(date -d "@$(($(date +%s) - RANDOM % 86400))" '+%Y-%m-%d %H:%M:%S')] [$severity] Message $i" >> "$CHALLENGE_DIR/files/project$dir/logs/app.log"
done
done
done
}
setup_permission_challenges() {
log_message "Setting up permission challenges..."
# Create test users and groups
for i in {1..3}; do
sudo groupadd -f "app_group$i" 2>/dev/null
sudo useradd -M -N -g "app_group$i" "app_user$i" 2>/dev/null
done
# Create files with specific permissions
mkdir -p "$CHALLENGE_DIR/files/permissions_test"
for i in {1..5}; do
echo "content$i" > "$CHALLENGE_DIR/files/permissions_test/file$i.txt"
chmod $((600 + i)) "$CHALLENGE_DIR/files/permissions_test/file$i.txt"
done
}
create_verification_functions() {
cat << 'EOF' > "$CHALLENGE_DIR/.tracking/verify-challenges.sh"
#!/bin/bash
source "$(dirname "$0")/common.sh"
# Verification functions for each challenge
verify_file_search() {
local challenge_num=$1
local description=$2
local command=$3
local expected=$4
log_message "Verifying challenge $challenge_num: $description"
local result
result=$(eval "$command")
if [[ "$result" == "$expected" ]]; then
award_points $challenge_num 100
return 0
fi
return 1
}
verify_permission_setup() {
local file=$1
local expected_perms=$2
local expected_owner=$3
local actual_perms=$(stat -c "%a" "$file")
local actual_owner=$(stat -c "%U:%G" "$file")
[[ "$actual_perms" == "$expected_perms" && "$actual_owner" == "$expected_owner" ]]
}
# Challenge verification functions
verify_challenge_1() {
# Find recent conf files
verify_file_search 1 "Find recent configuration files" \
"find \$CHALLENGE_DIR/files -name '*.conf' -mtime -1 | wc -l" \
"10"
}
verify_challenge_2() {
# Count ERROR entries in logs
verify_file_search 2 "Count ERROR messages" \
"grep -r 'ERROR' \$CHALLENGE_DIR/files/*/logs | wc -l" \
"15"
}
# Add more verification functions...
EOF
}
create_challenge_tracker() {
cat << 'EOF' > "$CHALLENGE_DIR/.tracking/challenge-tracker.sh"
#!/bin/bash
source "$(dirname "$0")/common.sh"
# Track command usage
track_command() {
local command=$1
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $command" >> "$CHALLENGE_DIR/.tracking/commands.log"
}
# Track time spent
track_time() {
local challenge=$1
local start_time=$2
local end_time=$3
local duration=$((end_time - start_time))
echo "$challenge,$duration" >> "$CHALLENGE_DIR/.tracking/time.log"
}
# Show challenge hints
show_hint() {
local challenge=$1
case $challenge in
1) echo "Try using 'find' with -mtime flag" ;;
2) echo "Consider 'grep' with -r flag" ;;
*) echo "No hint available" ;;
esac
}
# Show challenge status
show_status() {
local total_score=0
local completed=0
echo "Challenge Status:"
echo "----------------"
while IFS=, read -r challenge score; do
total_score=$((total_score + score))
[[ $score -gt 0 ]] && ((completed++))
printf "Challenge %s: %s points\n" "$challenge" "$score"
done < "$SCORE_FILE"
echo "----------------"
printf "Total Score: %s/1500\n" "$total_score"
printf "Completed: %s/10 challenges\n" "$completed"
}
EOF
}
# Main setup function
main() {
init_environment
setup_file_challenges
setup_permission_challenges
create_verification_functions
create_challenge_tracker
log_message "Challenge environment ready!"
cat << EOF
${GREEN}Level 1 Setup Complete!${NC}
Available commands:
- ${YELLOW}check-progress${NC}: Show current progress
- ${YELLOW}verify-challenge <num>${NC}: Verify specific challenge
- ${YELLOW}show-hint <num>${NC}: Get hint for challenge
- ${YELLOW}show-status${NC}: Display challenge status
Start with challenge 1: Find all recent configuration files
Good luck!
EOF
}
# Run setup
main
@@ -0,0 +1,34 @@
#!/bin/bash
CHALLENGE_DIR=~/challenge
PROGRESS_FILE="$CHALLENGE_DIR/.progress"
SCORE_FILE="$CHALLENGE_DIR/.score"
LOG_FILE="$CHALLENGE_DIR/.challenge.log"
# Colors
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'
show_progress() {
local current_score=$(cat "$SCORE_FILE" 2>/dev/null || echo "0")
local total_possible=1500 # Base challenges + bonus
local progress=$((current_score * 100 / total_possible))
echo -e "${GREEN}Challenge Progress${NC}"
echo "----------------------------------------"
echo -e "Current Score: ${YELLOW}$current_score${NC} / $total_possible points"
echo -e "Progress: ${YELLOW}$progress%${NC}"
echo "----------------------------------------"
# Show recent activity
echo -e "\n${GREEN}Recent Activity:${NC}"
tail -n 5 "$LOG_FILE"
# Show incomplete challenges
echo -e "\n${GREEN}Remaining Challenges:${NC}"
"$CHALLENGE_DIR/.tracking/verify-level1.sh" --list-incomplete
}
show_progress