|
| 1 | +# src/auditing-logging.cr |
| 2 | +# Auditing and Logging tool for Security Mode, written in Crystal. |
| 3 | +# Handles write-only logs, rotation, and optional forwarding. |
| 4 | +# Logs are stored in ~/.hackeros/Security-Mode/logs/security.log |
| 5 | +# Communication can use JSON in /tmp/Security-Mode/logs.json for batch logging. |
| 6 | +# CLI commands: append <message>, rotate, forward <target> |
| 7 | + |
| 8 | +require "json" |
| 9 | +require "file_utils" |
| 10 | +require "process" |
| 11 | + |
| 12 | +# Define log directory and file |
| 13 | +LOG_DIR = File.expand_path("~/.hackeros/Security-Mode/logs") |
| 14 | +LOG_FILE = File.join(LOG_DIR, "security.log") |
| 15 | +MAX_LOG_SIZE = 10 * 1024 * 1024 # 10 MB |
| 16 | + |
| 17 | +# Ensure log directory exists |
| 18 | +def ensure_log_dir |
| 19 | + FileUtils.mkdir_p(LOG_DIR) unless Dir.exists?(LOG_DIR) |
| 20 | +end |
| 21 | + |
| 22 | +# Append a message to the log file with timestamp |
| 23 | +def append_log(message : String) |
| 24 | + ensure_log_dir |
| 25 | + timestamp = Time.utc.to_s("%Y-%m-%d %H:%M:%S UTC") |
| 26 | + log_entry = "[#{timestamp}] #{message}\n" |
| 27 | + File.open(LOG_FILE, "a") do |f| |
| 28 | + f.write(log_entry.to_slice) |
| 29 | + end |
| 30 | + check_rotate |
| 31 | +end |
| 32 | + |
| 33 | +# Check if log needs rotation |
| 34 | +def check_rotate |
| 35 | + if File.exists?(LOG_FILE) && File.size(LOG_FILE) > MAX_LOG_SIZE |
| 36 | + rotate_log |
| 37 | + end |
| 38 | +end |
| 39 | + |
| 40 | +# Rotate the log file |
| 41 | +def rotate_log |
| 42 | + if File.exists?(LOG_FILE) |
| 43 | + timestamp = Time.utc.to_s("%Y%m%d%H%M%S") |
| 44 | + rotated_file = File.join(LOG_DIR, "security_#{timestamp}.log") |
| 45 | + FileUtils.mv(LOG_FILE, rotated_file) |
| 46 | + puts "Rotated log to #{rotated_file}" |
| 47 | + end |
| 48 | +end |
| 49 | + |
| 50 | +# Forward logs to a target (simulated, e.g., file or SIEM endpoint) |
| 51 | +# For simplicity, forward to another file or print |
| 52 | +def forward_logs(target : String) |
| 53 | + if File.exists?(LOG_FILE) |
| 54 | + content = File.read(LOG_FILE) |
| 55 | + if target.starts_with?("file:") |
| 56 | + target_file = target[5..-1].strip |
| 57 | + File.write(target_file, content) |
| 58 | + puts "Forwarded logs to #{target_file}" |
| 59 | + else |
| 60 | + # Simulate SIEM forward, e.g., via HTTP or something, but no internet, so just print |
| 61 | + puts "Forwarding to SIEM #{target}:" |
| 62 | + puts content |
| 63 | + end |
| 64 | + else |
| 65 | + puts "No log file to forward." |
| 66 | + end |
| 67 | +end |
| 68 | + |
| 69 | +# Process logs from JSON file if present |
| 70 | +def process_json_logs |
| 71 | + tmp_dir = "/tmp/Security-Mode" |
| 72 | + json_path = File.join(tmp_dir, "logs.json") |
| 73 | + if File.exists?(json_path) |
| 74 | + json_str = File.read(json_path) |
| 75 | + data = JSON.parse(json_str).as_h |
| 76 | + if logs = data["logs"]? |
| 77 | + logs.as_a.each do |log| |
| 78 | + append_log(log.as_s) |
| 79 | + end |
| 80 | + puts "Processed logs from JSON" |
| 81 | + File.delete(json_path) |
| 82 | + end |
| 83 | + end |
| 84 | +end |
| 85 | + |
| 86 | +# Main CLI parser |
| 87 | +def main |
| 88 | + process_json_logs # Always check for JSON logs first |
| 89 | + |
| 90 | + if ARGV.empty? |
| 91 | + puts "Usage: auditing-logging <command> [args]" |
| 92 | + puts "Commands:" |
| 93 | + puts " append <message> - Append a message to the log" |
| 94 | + puts " rotate - Rotate the log file" |
| 95 | + puts " forward <target> - Forward logs to target (file:path or siem:url)" |
| 96 | + exit(1) |
| 97 | + end |
| 98 | + |
| 99 | + command = ARGV[0].downcase |
| 100 | + |
| 101 | + case command |
| 102 | + when "append" |
| 103 | + if ARGV.size > 1 |
| 104 | + message = ARGV[1..-1].join(" ") |
| 105 | + append_log(message) |
| 106 | + puts "Appended log: #{message}" |
| 107 | + else |
| 108 | + puts "Missing message for append command." |
| 109 | + end |
| 110 | + when "rotate" |
| 111 | + rotate_log |
| 112 | + when "forward" |
| 113 | + if ARGV.size > 1 |
| 114 | + forward_logs(ARGV[1]) |
| 115 | + else |
| 116 | + puts "Missing target for forward command." |
| 117 | + end |
| 118 | + else |
| 119 | + puts "Unknown command: #{command}" |
| 120 | + end |
| 121 | +end |
| 122 | + |
| 123 | +main if __FILE__ == Process.executable_path |
0 commit comments