xe1phix

System-Integrity-Analyzer.sh

May 2nd, 2025
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 33.45 KB | Cybersecurity | 0 0
  1. #!/bin/bash
  2. # System Integrity Analyzer
  3. #
  4. # This script performs comprehensive system integrity checks to detect unauthorized
  5. # file modifications, rootkits, and other system compromises.
  6. #
  7. # Options:
  8. # 1. Initialize AIDE database
  9. # 2. Update AIDE database
  10. # 3. Check system integrity with AIDE
  11. # 4. Scan for rootkits with chkrootkit
  12. # 5. Scan for rootkits with rkhunter
  13. # 6. Check for hidden processes
  14. # 7. Check for unauthorized SUID/SGID binaries
  15. # 8. Verify installed package integrity
  16. # 9. Check for suspicious cron jobs
  17. # 10. Check for suspicious kernel modules
  18. # 11. Monitor file changes in real-time
  19. # 12. Check for unauthorized network listeners
  20. # 13. Generate system integrity report
  21. # 14. Schedule automated integrity checks
  22. # 15. Compare current state with baseline
  23. # 16. Exit
  24.  
  25. # Check for required tools
  26. check_required_tools() {
  27.     missing_tools=()
  28.    
  29.     command -v aide >/dev/null 2>&1 || missing_tools+=("aide")
  30.     command -v chkrootkit >/dev/null 2>&1 || missing_tools+=("chkrootkit")
  31.     command -v rkhunter >/dev/null 2>&1 || missing_tools+=("rkhunter")
  32.     command -v inotifywait >/dev/null 2>&1 || missing_tools+=("inotify-tools")
  33.    
  34.     if [ ${#missing_tools[@]} -gt 0 ]; then
  35.         echo "The following required tools are missing:"
  36.         for tool in "${missing_tools[@]}"; do
  37.             echo "- $tool"
  38.         done
  39.         echo "Install them with: apt-get install ${missing_tools[*]}"
  40.         return 1
  41.     fi
  42.     return 0
  43. }
  44.  
  45. # Function to display menu
  46. show_menu() {
  47.     clear
  48.     echo "===== System Integrity Analyzer ====="
  49.     echo "1. Initialize AIDE database"
  50.     echo "2. Update AIDE database"
  51.     echo "3. Check system integrity with AIDE"
  52.     echo "4. Scan for rootkits with chkrootkit"
  53.     echo "5. Scan for rootkits with rkhunter"
  54.     echo "6. Check for hidden processes"
  55.     echo "7. Check for unauthorized SUID/SGID binaries"
  56.     echo "8. Verify installed package integrity"
  57.     echo "9. Check for suspicious cron jobs"
  58.     echo "10. Check for suspicious kernel modules"
  59.     echo "11. Monitor file changes in real-time"
  60.     echo "12. Check for unauthorized network listeners"
  61.     echo "13. Generate system integrity report"
  62.     echo "14. Schedule automated integrity checks"
  63.     echo "15. Compare current state with baseline"
  64.     echo "16. Exit"
  65.     echo "====================================="
  66.     echo "Enter your choice [1-16]: "
  67. }
  68.  
  69. # Function to initialize AIDE database
  70. initialize_aide_database() {
  71.     echo "Initializing AIDE database. This may take some time..."
  72.     sudo aide --init
  73.     if [ $? -eq 0 ]; then
  74.         sudo mv /var/lib/aide/aide.db.new /var/lib/aide/aide.db
  75.         echo "AIDE database initialized successfully."
  76.     else
  77.         echo "Failed to initialize AIDE database."
  78.     fi
  79. }
  80.  
  81. # Function to update AIDE database
  82. update_aide_database() {
  83.     echo "Updating AIDE database. This may take some time..."
  84.     sudo aide --update
  85.     if [ $? -eq 0 ]; then
  86.         sudo mv /var/lib/aide/aide.db.new /var/lib/aide/aide.db
  87.         echo "AIDE database updated successfully."
  88.     else
  89.         echo "Failed to update AIDE database."
  90.     fi
  91. }
  92.  
  93. # Function to check system integrity with AIDE
  94. check_system_integrity() {
  95.     echo "Checking system integrity with AIDE. This may take some time..."
  96.     report_file="aide_report_$(date +%Y%m%d_%H%M%S).txt"
  97.     sudo aide --check > "$report_file" 2>&1
  98.     echo "Integrity check completed. Report saved to $report_file"
  99.    
  100.     # Display summary
  101.     added=$(grep -c "added" "$report_file")
  102.     removed=$(grep -c "removed" "$report_file")
  103.     changed=$(grep -c "changed" "$report_file")
  104.    
  105.     echo "Summary:"
  106.     echo "- Added files: $added"
  107.     echo "- Removed files: $removed"
  108.     echo "- Changed files: $changed"
  109.    
  110.     if [ $added -gt 0 ] || [ $removed -gt 0 ] || [ $changed -gt 0 ]; then
  111.         echo "WARNING: System integrity compromised! Review the report for details."
  112.     else
  113.         echo "System integrity verified. No changes detected."
  114.     fi
  115. }
  116.  
  117.     # Display summary of chkrootkit results
  118.     infected=$(grep -c "INFECTED" "$report_file")
  119.     suspicious=$(grep -c -E "suspicious|WARNING" "$report_file")
  120.    
  121.     echo "Summary:"
  122.     echo "- Infected items: $infected"
  123.     echo "- Suspicious items: $suspicious"
  124.    
  125.     if [ $infected -gt 0 ] || [ $suspicious -gt 0 ]; then
  126.         echo "WARNING: Possible rootkit detected! Review the report for details."
  127.         grep -E "INFECTED|suspicious|WARNING" "$report_file"
  128.     else
  129.         echo "No rootkits detected."
  130.     fi
  131. }
  132.  
  133. # Function to scan for rootkits with rkhunter
  134. scan_with_rkhunter() {
  135.     echo "Scanning for rootkits with rkhunter. This may take some time..."
  136.     report_file="rkhunter_report_$(date +%Y%m%d_%H%M%S).txt"
  137.     sudo rkhunter --checkall --skip-keypress --report-warnings-only > "$report_file" 2>&1
  138.     echo "Rootkit scan completed. Report saved to $report_file"
  139.    
  140.     # Display summary of rkhunter results
  141.     warnings=$(grep -c "Warning" "$report_file")
  142.    
  143.     echo "Summary:"
  144.     echo "- Warnings: $warnings"
  145.    
  146.     if [ $warnings -gt 0 ]; then
  147.         echo "WARNING: Possible security issues detected! Review the report for details."
  148.         grep "Warning" "$report_file"
  149.     else
  150.         echo "No security issues detected."
  151.     fi
  152. }
  153.  
  154. # Function to check for hidden processes
  155. check_hidden_processes() {
  156.     echo "Checking for hidden processes..."
  157.     report_file="hidden_processes_report_$(date +%Y%m%d_%H%M%S).txt"
  158.    
  159.     echo "Hidden Processes Report - $(date)" > "$report_file"
  160.     echo "=================================" >> "$report_file"
  161.    
  162.     # Get list of processes from ps
  163.     ps_pids=$(ps -ef | awk '{print $2}' | sort -n | uniq)
  164.    
  165.     # Get list of processes from /proc
  166.     proc_pids=$(find /proc -maxdepth 1 -regex '/proc/[0-9]+' | awk -F/ '{print $3}' | sort -n | uniq)
  167.    
  168.     # Find processes in /proc but not in ps
  169.     hidden_pids=$(comm -13 <(echo "$ps_pids") <(echo "$proc_pids"))
  170.    
  171.     if [ -n "$hidden_pids" ]; then
  172.         echo "WARNING: Hidden processes detected!" | tee -a "$report_file"
  173.         echo "Hidden PIDs:" | tee -a "$report_file"
  174.         for pid in $hidden_pids; do
  175.             echo "PID: $pid" | tee -a "$report_file"
  176.             if [ -d "/proc/$pid" ]; then
  177.                 echo "  Command: $(cat /proc/$pid/cmdline 2>/dev/null | tr '\0' ' ')" | tee -a "$report_file"
  178.                 echo "  Owner: $(stat -c %U /proc/$pid 2>/dev/null)" | tee -a "$report_file"
  179.                 echo "  Created: $(stat -c %w /proc/$pid 2>/dev/null)" | tee -a "$report_file"
  180.             fi
  181.             echo | tee -a "$report_file"
  182.         done
  183.     else
  184.         echo "No hidden processes detected." | tee -a "$report_file"
  185.     fi
  186.    
  187.     echo "Check completed. Report saved to $report_file"
  188. }
  189.  
  190. # Function to check for unauthorized SUID/SGID binaries
  191. check_suid_sgid() {
  192.     echo "Checking for unauthorized SUID/SGID binaries..."
  193.     report_file="suid_sgid_report_$(date +%Y%m%d_%H%M%S).txt"
  194.    
  195.     echo "SUID/SGID Binary Report - $(date)" > "$report_file"
  196.     echo "=================================" >> "$report_file"
  197.    
  198.     # Create baseline if it doesn't exist
  199.     baseline_file="/var/lib/integrity_checker/suid_sgid_baseline.txt"
  200.     if [ ! -f "$baseline_file" ]; then
  201.         echo "Baseline not found. Creating baseline of SUID/SGID binaries..."
  202.         sudo mkdir -p /var/lib/integrity_checker
  203.         sudo find / -type f \( -perm -4000 -o -perm -2000 \) -exec ls -la {} \; 2>/dev/null | sort > "$baseline_file"
  204.         echo "Baseline created. Run this check again to detect changes."
  205.         return
  206.     fi
  207.    
  208.     # Find current SUID/SGID binaries
  209.     current_file=$(mktemp)
  210.     sudo find / -type f \( -perm -4000 -o -perm -2000 \) -exec ls -la {} \; 2>/dev/null | sort > "$current_file"
  211.    
  212.     # Compare with baseline
  213.     echo "New SUID/SGID binaries (not in baseline):" >> "$report_file"
  214.     diff_output=$(comm -13 "$baseline_file" "$current_file")
  215.     if [ -n "$diff_output" ]; then
  216.         echo "$diff_output" >> "$report_file"
  217.         echo "WARNING: New SUID/SGID binaries detected!" | tee -a "$report_file"
  218.         echo "$diff_output" | head -10 | tee -a "$report_file"
  219.         if [ $(echo "$diff_output" | wc -l) -gt 10 ]; then
  220.             echo "...and $(( $(echo "$diff_output" | wc -l) - 10 )) more. See report for details." | tee -a "$report_file"
  221.         fi
  222.     else
  223.         echo "None found." >> "$report_file"
  224.         echo "No new SUID/SGID binaries detected."
  225.     fi
  226.    
  227.     echo "Missing SUID/SGID binaries (in baseline but not found now):" >> "$report_file"
  228.     missing_output=$(comm -23 "$baseline_file" "$current_file")
  229.     if [ -n "$missing_output" ]; then
  230.         echo "$missing_output" >> "$report_file"
  231.         echo "WARNING: Some SUID/SGID binaries from baseline are missing!" | tee -a "$report_file"
  232.     else
  233.         echo "None missing." >> "$report_file"
  234.     fi
  235.    
  236.     # Clean up
  237.     rm "$current_file"
  238.     echo "Check completed. Report saved to $report_file"
  239. }
  240.  
  241. # Function to verify installed package integrity
  242. verify_package_integrity() {
  243.     echo "Verifying integrity of installed packages. This may take some time..."
  244.     report_file="package_integrity_report_$(date +%Y%m%d_%H%M%S).txt"
  245.    
  246.     echo "Package Integrity Report - $(date)" > "$report_file"
  247.     echo "=================================" >> "$report_file"
  248.    
  249.     # Determine package manager
  250.     if command -v dpkg >/dev/null 2>&1; then
  251.         echo "Using dpkg for package verification..." | tee -a "$report_file"
  252.         echo "Verifying all installed packages..." | tee -a "$report_file"
  253.        
  254.         # Get list of installed packages
  255.         packages=$(dpkg --get-selections | grep -v deinstall | awk '{print $1}')
  256.        
  257.         # Check each package
  258.         failed_packages=0
  259.         for package in $packages; do
  260.             echo -n "Checking $package... "
  261.             output=$(sudo dpkg --verify "$package" 2>&1)
  262.             if [ -n "$output" ]; then
  263.                 echo "FAILED" | tee -a "$report_file"
  264.                 echo "Package: $package" >> "$report_file"
  265.                 echo "$output" >> "$report_file"
  266.                 echo >> "$report_file"
  267.                 failed_packages=$((failed_packages + 1))
  268.             else
  269.                 echo "OK"
  270.             fi
  271.         done
  272.        
  273.         echo "Verification complete. $failed_packages packages failed integrity check." | tee -a "$report_file"
  274.        
  275.     elif command -v rpm >/dev/null 2>&1; then
  276.         echo "Using rpm for package verification..." | tee -a "$report_file"
  277.         echo "Verifying all installed packages..." | tee -a "$report_file"
  278.        
  279.         # Verify all packages and save failures
  280.         output=$(sudo rpm -Va --nomtime --nomode --nomd5 --nolinkto 2>&1)
  281.        
  282.         if [ -n "$output" ]; then
  283.             echo "Failed integrity checks:" >> "$report_file"
  284.             echo "$output" >> "$report_file"
  285.             failed_count=$(echo "$output" | wc -l)
  286.             echo "Verification complete. $failed_count files failed integrity check." | tee -a "$report_file"
  287.             echo "WARNING: Some files failed integrity check! See report for details." | tee -a "$report_file"
  288.         else
  289.             echo "Verification complete. All packages passed integrity check." | tee -a "$report_file"
  290.             echo "All packages passed integrity check."
  291.         fi
  292.     else
  293.         echo "No supported package manager found (dpkg or rpm)." | tee -a "$report_file"
  294.     fi
  295.    
  296.     echo "Package verification completed. Report saved to $report_file"
  297. }
  298.  
  299. # Function to check for suspicious cron jobs
  300. check_suspicious_cron() {
  301.     echo "Checking for suspicious cron jobs..."
  302.     report_file="suspicious_cron_report_$(date +%Y%m%d_%H%M%S).txt"
  303.    
  304.     echo "Suspicious Cron Jobs Report - $(date)" > "$report_file"
  305.     echo "=================================" >> "$report_file"
  306.    
  307.     # Check system crontabs
  308.     echo "System Crontabs:" >> "$report_file"
  309.     for crontab in /etc/crontab /etc/cron.d/* /etc/cron.hourly/* /etc/cron.daily/* /etc/cron.weekly/* /etc/cron.monthly/*; do
  310.         if [ -f "$crontab" ]; then
  311.             echo "Checking $crontab..." | tee -a "$report_file"
  312.             suspicious_entries=$(grep -E '(wget|curl|nc|netcat|bash -i|sh -i|python -c|perl -e|base64|eval|gzip -d|\
  313. b(\/tmp|\/var\/tmp|\/dev\/shm)\/|reverse shell|connect-back|backdoor)' "$crontab" 2>/dev/null)
  314.             if [ -n "$suspicious_entries" ]; then
  315.                 echo "WARNING: Suspicious entries found in $crontab:" | tee -a "$report_file"
  316.                 echo "$suspicious_entries" | tee -a "$report_file"
  317.             fi
  318.         fi
  319.     done
  320.    
  321.     # Check user crontabs
  322.     echo -e "\nUser Crontabs:" >> "$report_file"
  323.     for user in $(cut -f1 -d: /etc/passwd); do
  324.         crontab_file=$(sudo -u "$user" crontab -l 2>/dev/null)
  325.         if [ $? -eq 0 ]; then
  326.             suspicious_entries=$(echo "$crontab_file" | grep -E '(wget|curl|nc|netcat|bash -i|sh -i|python -c|perl -e|base64|eval|gzip -d|\
  327. b(\/tmp|\/var\/tmp|\/dev\/shm)\/|reverse shell|connect-back|backdoor)' 2>/dev/null)
  328.             if [ -n "$suspicious_entries" ]; then
  329.                 echo "WARNING: Suspicious entries found in $user's crontab:" | tee -a "$report_file"
  330.                 echo "$suspicious_entries" | tee -a "$report_file"
  331.             fi
  332.         fi
  333.     done
  334.    
  335.     echo "Cron job check completed. Report saved to $report_file"
  336. }
  337.  
  338. # Function to check for suspicious kernel modules
  339. check_suspicious_modules() {
  340.     echo "Checking for suspicious kernel modules..."
  341.     report_file="suspicious_modules_report_$(date +%Y%m%d_%H%M%S).txt"
  342.    
  343.     echo "Suspicious Kernel Modules Report - $(date)" > "$report_file"
  344.     echo "=================================" >> "$report_file"
  345.    
  346.     # List loaded modules
  347.     echo "Loaded Kernel Modules:" >> "$report_file"
  348.     lsmod | sort >> "$report_file"
  349.    
  350.     # Check for hidden modules (modules not in lsmod but present in /proc/modules)
  351.     echo -e "\nChecking for hidden modules..." | tee -a "$report_file"
  352.     lsmod_modules=$(lsmod | awk '{print $1}' | grep -v "Module" | sort)
  353.     proc_modules=$(cat /proc/modules | awk '{print $1}' | sort)
  354.    
  355.     hidden_modules=$(comm -13 <(echo "$lsmod_modules") <(echo "$proc_modules"))
  356.     if [ -n "$hidden_modules" ]; then
  357.         echo "WARNING: Hidden kernel modules detected!" | tee -a "$report_file"
  358.         echo "$hidden_modules" | tee -a "$report_file"
  359.     else
  360.         echo "No hidden modules detected." | tee -a "$report_file"
  361.     fi
  362.    
  363.     # Check for suspicious module names
  364.     echo -e "\nChecking for suspicious module names..." | tee -a "$report_file"
  365.     suspicious_modules=$(lsmod | grep -E '(hide|backdoor|rootkit|intercept|hook|stealth)' | awk '{print $1}')
  366.     if [ -n "$suspicious_modules" ]; then
  367.         echo "WARNING: Suspicious module names detected!" | tee -a "$report_file"
  368.         echo "$suspicious_modules" | tee -a "$report_file"
  369.     else
  370.         echo "No suspicious module names detected." | tee -a "$report_file"
  371.     fi
  372.    
  373.     # Check module info
  374.     echo -e "\nDetailed information for all modules:" >> "$report_file"
  375.     for module in $(lsmod | awk '{print $1}' | grep -v "Module"); do
  376.         echo -e "\nModule: $module" >> "$report_file"
  377.         modinfo "$module" 2>/dev/null >> "$report_file"
  378.        
  379.         # Check if module is unsigned
  380.         signature=$(modinfo "$module" 2>/dev/null | grep -i "signature")
  381.         if [ -z "$signature" ]; then
  382.             echo "WARNING: Unsigned module $module detected!" | tee -a "$report_file"
  383.         fi
  384.     done
  385.    
  386.     echo "Kernel module check completed. Report saved to $report_file"
  387. }
  388.  
  389. # Function to monitor file changes in real-time
  390. monitor_file_changes() {
  391.     echo "Monitoring file changes in real-time. Press Ctrl+C to stop."
  392.    
  393.     # Check if inotifywait is available
  394.     if ! command -v inotifywait >/dev/null 2>&1; then
  395.         echo "Error: inotifywait not found. Install inotify-tools package."
  396.         return 1
  397.     fi
  398.    
  399.     # Ask for directories to monitor
  400.     read -p "Enter critical directories to monitor (space-separated, e.g., /etc /bin): " directories
  401.    
  402.     # Use default directories if none specified
  403.     if [ -z "$directories" ]; then
  404.         directories="/etc /bin /sbin /usr/bin /usr/sbin /boot"
  405.         echo "Using default directories: $directories"
  406.     fi
  407.    
  408.     echo "Starting real-time file monitoring on: $directories"
  409.     echo "Recording events: create, modify, delete, move, attrib"
  410.     echo "Log file: file_changes_$(date +%Y%m%d_%H%M%S).log"
  411.     echo "Press Ctrl+C to stop monitoring."
  412.    
  413.     # Start monitoring
  414.     inotifywait -m -r -e create,modify,delete,move,attrib $directories | while read path action file; do
  415.         timestamp=$(date +"%Y-%m-%d %H:%M:%S")
  416.         echo "$timestamp | $path | $action | $file" | tee -a "file_changes_$(date +%Y%m%d).log"
  417.     done
  418. }
  419.  
  420. # Function to check for unauthorized network listeners
  421. check_network_listeners() {
  422.     echo "Checking for unauthorized network listeners..."
  423.     report_file="network_listeners_report_$(date +%Y%m%d_%H%M%S).txt"
  424.    
  425.     echo "Network Listeners Report - $(date)" > "$report_file"
  426.     echo "=================================" >> "$report_file"
  427.    
  428.     # Get all listening ports and processes
  429.     echo "Active Network Listeners:" >> "$report_file"
  430.     sudo netstat -tulpn | grep LISTEN >> "$report_file"
  431.    
  432.     # Create baseline if it doesn't exist
  433.     baseline_file="/var/lib/integrity_checker/network_listeners_baseline.txt"
  434.     if [ ! -f "$baseline_file" ]; then
  435.         echo "Baseline not found. Creating baseline of network listeners..."
  436.         sudo mkdir -p /var/lib/integrity_checker
  437.         sudo netstat -tulpn | grep LISTEN | sort > "$baseline_file"
  438.         echo "Baseline created. Run this check again to detect changes."
  439.         return
  440.     fi
  441.    
  442.     # Find current listeners
  443.     current_file=$(mktemp)
  444.     sudo netstat -tulpn | grep LISTEN | sort > "$current_file"
  445.    
  446.     # Compare with baseline
  447.     echo -e "\nNew network listeners (not in baseline):" >> "$report_file"
  448.     diff_output=$(comm -13 "$baseline_file" "$current_file")
  449.     if [ -n "$diff_output" ]; then
  450.         echo "$diff_output" >> "$report_file"
  451.         echo "WARNING: New network listeners detected!" | tee -a "$report_file"
  452.         echo "$diff_output" | tee -a "$report_file"
  453.     else
  454.         echo "None found." >> "$report_file"
  455.         echo "No new network listeners detected."
  456.     fi
  457.    
  458.     # Check for suspicious ports
  459.     echo -e "\nChecking for suspicious ports..." | tee -a "$report_file"
  460.     suspicious_ports=$(grep -E ':(4444|1337|31337|6666|6667|6668|6669|31415|47012)' "$current_file")
  461.     if [ -n "$suspicious_ports" ]; then
  462.         echo "WARNING: Suspicious network ports detected!" | tee -a "$report_file"
  463.         echo "$suspicious_ports" | tee -a "$report_file"
  464.     else
  465.         echo "No suspicious ports detected." | tee -a "$report_file"
  466.     fi
  467.    
  468.     # Clean up
  469.     rm "$current_file"
  470.     echo "Network listener check completed. Report saved to $report_file"
  471. }
  472.  
  473. # Function to generate system integrity report
  474. generate_integrity_report() {
  475.     echo "Generating comprehensive system integrity report..."
  476.     report_dir="system_integrity_report_$(date +%Y%m%d_%H%M%S)"
  477.     mkdir -p "$report_dir"
  478.    
  479.     echo "System Integrity Report - $(date)" > "$report_dir/summary.txt"
  480.     echo "=================================" >> "$report_dir/summary.txt"
  481.    
  482.     # System information
  483.     echo "Collecting system information..."
  484.     echo -e "\n== System Information ==" >> "$report_dir/summary.txt"
  485.     echo "Hostname: $(hostname)" >> "$report_dir/summary.txt"
  486.     echo "Kernel: $(uname -r)" >> "$report_dir/summary.txt"
  487.     echo "Distribution: $(lsb_release -d 2>/dev/null | cut -f2- || cat /etc/*release | head -n1)" >> "$report_dir/summary.txt"
  488.    
  489.     # User accounts
  490.     echo "Analyzing user accounts..."
  491.     echo -e "\n== User Accounts ==" >> "$report_dir/summary.txt"
  492.     echo "Users with UID 0:" >> "$report_dir/summary.txt"
  493.     grep ":0:" /etc/passwd >> "$report_dir/summary.txt"
  494.    
  495.     echo "Recently added users:" >> "$report_dir/summary.txt"
  496.     last_week=$(date -d "7 days ago" +%s)
  497.     for user in $(cut -d: -f1,3 /etc/passwd); do
  498.         username=$(echo "$user" | cut -d: -f1)
  499.         uid=$(echo "$user" | cut -d: -f2)
  500.         if [ -d "/home/$username" ]; then
  501.             create_time=$(stat -c %W "/home/$username")
  502.             if [ "$create_time" -gt "$last_week" ]; then
  503.                 echo "$username (UID: $uid) - created on $(date -d @"$create_time" +"%Y-%m-%d")" >> "$report_dir/summary.txt"
  504.             fi
  505.         fi
  506.     done
  507.    
  508.     # SUID/SGID binaries
  509.     echo "Checking SUID/SGID binaries..."
  510.     echo -e "\n== SUID/SGID Binaries ==" >> "$report_dir/summary.txt"
  511.     echo "Unusual SUID/SGID binaries:" >> "$report_dir/summary.txt"
  512.     find / -type f \( -perm -4000 -o -perm -2000 \) -not -path "/usr/bin/*" -not -path "/usr/sbin/*" -not -path "/bin/*" -not -path "/sbin/*" 2>/dev/null | sort >> "$report_dir/suid_sgid.txt"
  513.     count=$(wc -l < "$report_dir/suid_sgid.txt")
  514.     echo "Found $count unusual SUID/SGID binaries. See suid_sgid.txt for details." >> "$report_dir/summary.txt"
  515.    
  516.     # Network connections
  517.     echo "Analyzing network connections..."
  518.     echo -e "\n== Network Connections ==" >> "$report_dir/summary.txt"
  519.     sudo netstat -tualnp > "$report_dir/network_connections.txt"
  520.     listening=$(grep LISTEN "$report_dir/network_connections.txt" | wc -l)
  521.     established=$(grep ESTABLISHED "$report_dir/network_connections.txt" | wc -l)
  522.     echo "Listening connections: $listening" >> "$report_dir/summary.txt"
  523.     echo "Established connections: $established" >> "$report_dir/summary.txt"
  524.     echo "See network_connections.txt for details." >> "$report_dir/summary.txt"
  525.    
  526.     # Running processes
  527.     echo "Analyzing running processes..."
  528.     echo -e "\n== Running Processes ==" >> "$report_dir/summary.txt"
  529.     ps -eo pid,ppid,user,cmd --sort=pid > "$report_dir/processes.txt"
  530.     process_count=$(wc -l < "$report_dir/processes.txt")
  531.     echo "Total processes: $process_count" >> "$report_dir/summary.txt"
  532.     echo "See processes.txt for details." >> "$report_dir/summary.txt"
  533.    
  534.     # Loaded kernel modules
  535.     echo "Analyzing kernel modules..."
  536.     echo -e "\n== Kernel Modules ==" >> "$report_dir/summary.txt"
  537.     lsmod > "$report_dir/kernel_modules.txt"
  538.     module_count=$(wc -l < "$report_dir/kernel_modules.txt")
  539.     echo "Loaded modules: $module_count" >> "$report_dir/summary.txt"
  540.     echo "See kernel_modules.txt for details." >> "$report_dir/summary.txt"
  541.    
  542.     # Scheduled tasks
  543.     echo "Checking scheduled tasks..."
  544.     echo -e "\n== Scheduled Tasks ==" >> "$report_dir/summary.txt"
  545.     find /etc/cron* -type f | xargs cat 2>/dev/null > "$report_dir/cron_system.txt"
  546.     for user in $(cut -f1 -d: /etc/passwd); do
  547.         sudo -u "$user" crontab -l 2>/dev/null >> "$report_dir/cron_users.txt" || true
  548.     done
  549.    
  550.     # Run quick AIDE check if available
  551.     if command -v aide >/dev/null 2>&1; then
  552.         echo "Running quick AIDE check..."
  553.         echo -e "\n== AIDE Integrity Check ==" >> "$report_dir/summary.txt"
  554.         sudo aide --check 2>&1 | head -20 > "$report_dir/aide_check.txt"
  555.         changes=$(grep -c "changed" "$report_dir/aide_check.txt")
  556.         echo "Files changed: $changes (truncated output, see aide_check.txt)" >> "$report_dir/summary.txt"
  557.     fi
  558.    
  559.     echo "Comprehensive system integrity report generated in directory: $report_dir"
  560. }
  561.  
  562. # Function to schedule automated integrity checks
  563. schedule_automated_checks() {
  564.     echo "Scheduling automated integrity checks..."
  565.    
  566.     # Create the script to be executed by cron
  567.     script_path="/usr/local/bin/integrity_check.sh"
  568.     sudo tee "$script_path" > /dev/null <<'EOF'
  569. #!/bin/bash
  570. # Automated integrity check script
  571.  
  572. REPORT_DIR="/var/log/integrity_reports"
  573. mkdir -p "$REPORT_DIR"
  574.  
  575. # Generate report filename with date
  576. REPORT_FILE="$REPORT_DIR/integrity_report_$(date +%Y%m%d_%H%M%S).txt"
  577. echo "System Integrity Check - $(date)" > "$REPORT_FILE"
  578. echo "=================================" >> "$REPORT_FILE"
  579.  
  580. # Run AIDE check
  581. if command -v aide >/dev/null 2>&1; then
  582.     echo -e "\n== AIDE Integrity Check ==" >> "$REPORT_FILE"
  583.     aide --check 2>&1 >> "$REPORT_FILE"
  584. fi
  585.  
  586. # Check for new SUID/SGID binaries
  587. echo -e "\n== SUID/SGID Binary Check ==" >> "$REPORT_FILE"
  588. find / -type f \( -perm -4000 -o -perm -2000 \) -not -path "/proc/*" 2>/dev/null | sort > /tmp/suid_sgid_current.txt
  589. if [ -f /var/lib/integrity_checker/suid_sgid_baseline.txt ]; then
  590.     echo "New SUID/SGID binaries:" >> "$REPORT_FILE"
  591.     comm -13 /var/lib/integrity_checker/suid_sgid_baseline.txt /tmp/suid_sgid_current.txt >> "$REPORT_FILE"
  592. else
  593.     echo "No baseline found for comparison." >> "$REPORT_FILE"
  594. fi
  595.  
  596. # Check for suspicious network listeners
  597. echo -e "\n== Network Listeners Check ==" >> "$REPORT_FILE"
  598. netstat -tulpn | grep LISTEN >> "$REPORT_FILE"
  599.  
  600. # Check for suspicious kernel modules
  601. echo -e "\n== Kernel Module Check ==" >> "$REPORT_FILE"
  602. lsmod | grep -E '(hide|backdoor|rootkit|intercept|hook|stealth)' >> "$REPORT_FILE" || echo "No suspicious modules found." >> "$REPORT_FILE"
  603.  
  604. # Email the report if mail is available
  605. if command -v mail >/dev/null 2>&1; then
  606.     mail -s "System Integrity Report - $(date +%Y-%m-%d)" root < "$REPORT_FILE"
  607. fi
  608.  
  609. # Rotate reports (keep last 30)
  610. find "$REPORT_DIR" -name "integrity_report_*" -type f | sort | head -n -30 | xargs rm -f
  611. EOF
  612.    
  613.     # Make the script executable
  614.     sudo chmod +x "$script_path"
  615.    
  616.     # Ask for schedule
  617.     echo "How often would you like to run the integrity checks?"
  618.     echo "1. Hourly"
  619.     echo "2. Daily"
  620.     echo "3. Weekly"
  621.     read -p "Select schedule [1-3]: " schedule_choice
  622.    
  623.     # Create cron entry
  624.     case $schedule_choice in
  625.         1) cron_schedule="0 * * * *" ;;  # Hourly
  626.         2) cron_schedule="0 0 * * *" ;;  # Daily at midnight
  627.         3) cron_schedule="0 0 * * 0" ;;  # Weekly on Sunday at midnight
  628.         *) cron_schedule="0 0 * * *" ;;  # Default to daily
  629.     esac
  630.    
  631.     # Add to crontab
  632.     cronfile=$(mktemp)
  633.     sudo crontab -l 2>/dev/null > "$cronfile" || true
  634.     echo "$cron_schedule $script_path" >> "$cronfile"
  635.     sudo crontab "$cronfile"
  636.     rm "$cronfile"
  637.    
  638.     echo "Automated integrity checks scheduled with cron."
  639.     echo "Schedule: $cron_schedule"
  640.     echo "Script installed at: $script_path"
  641.     echo "Reports will be saved to: /var/log/integrity_reports/"
  642. }
  643.  
  644. # Function to compare current state with baseline
  645. compare_with_baseline() {
  646.     echo "Comparing current system state with baseline..."
  647.    
  648.     # Check if baseline exists
  649.     if [ ! -d "/var/lib/integrity_checker" ]; then
  650.         echo "No baseline found. Would you like to create one? (y/n)"
  651.         read create_baseline
  652.         if [[ "$create_baseline" == "y" || "$create_baseline" == "Y" ]]; then
  653.             create_full_baseline
  654.         fi
  655.         return
  656.     fi
  657.    
  658.     report_file="baseline_comparison_$(date +%Y%m%d_%H%M%S).txt"
  659.     echo "System Baseline Comparison - $(date)" > "$report_file"
  660.     echo "=================================" >> "$report_file"
  661.    
  662.     # Compare SUID/SGID binaries
  663.     if [ -f "/var/lib/integrity_checker/suid_sgid_baseline.txt" ]; then
  664.         echo -e "\n== SUID/SGID Binary Comparison ==" >> "$report_file"
  665.         echo "Finding current SUID/SGID binaries..."
  666.         current_suid=$(mktemp)
  667.         sudo find / -type f \( -perm -4000 -o -perm -2000 \) -exec ls -la {} \; 2>/dev/null | sort > "$current_suid"
  668.        
  669.         echo "New SUID/SGID binaries:" >> "$report_file"
  670.         new_suid=$(comm -13 "/var/lib/integrity_checker/suid_sgid_baseline.txt" "$current_suid")
  671.         if [ -n "$new_suid" ]; then
  672.             echo "$new_suid" >> "$report_file"
  673.             echo "WARNING: New SUID/SGID binaries detected!" | tee -a "$report_file"
  674.         else
  675.             echo "None found." >> "$report_file"
  676.         fi
  677.        
  678.         echo "Missing SUID/SGID binaries:" >> "$report_file"
  679.         missing_suid=$(comm -23 "/var/lib/integrity_checker/suid_sgid_baseline.txt" "$current_suid")
  680.         if [ -n "$missing_suid" ]; then
  681.             echo "$missing_suid" >> "$report_file"
  682.             echo "WARNING: Some SUID/SGID binaries from baseline are missing!" | tee -a "$report_file"
  683.         else
  684.             echo "None missing." >> "$report_file"
  685.         fi
  686.        
  687.         rm "$current_suid"
  688.     else
  689.         echo "No SUID/SGID baseline found." >> "$report_file"
  690.     fi
  691.    
  692.     # Compare network listeners
  693.     if [ -f "/var/lib/integrity_checker/network_listeners_baseline.txt" ]; then
  694.         echo -e "\n== Network Listeners Comparison ==" >> "$report_file"
  695.         echo "Finding current network listeners..."
  696.         current_listeners=$(mktemp)
  697.         sudo netstat -tulpn | grep LISTEN | sort > "$current_listeners"
  698.        
  699.         echo "New network listeners:" >> "$report_file"
  700.         new_listeners=$(comm -13 "/var/lib/integrity_checker/network_listeners_baseline.txt" "$current_listeners")
  701.         if [ -n "$new_listeners" ]; then
  702.             echo "$new_listeners" >> "$report_file"
  703.             echo "WARNING: New network listeners detected!" | tee -a "$report_file"
  704.         else
  705.             echo "None found." >> "$report_file"
  706.         fi
  707.        
  708.         echo "Missing network listeners:" >> "$report_file"
  709.         missing_listeners=$(comm -23 "/var/lib/integrity_checker/network_listeners_baseline.txt" "$current_listeners")
  710.         if [ -n "$missing_listeners" ]; then
  711.             echo "$missing_listeners" >> "$report_file"
  712.             echo "WARNING: Some network listeners from baseline are missing!" | tee -a "$report_file"
  713.         else
  714.             echo "None missing." >> "$report_file"
  715.         fi
  716.        
  717.         rm "$current_listeners"
  718.     else
  719.         echo "No network listeners baseline found." >> "$report_file"
  720.     fi
  721.    
  722.     # Compare kernel modules
  723.     if [ -f "/var/lib/integrity_checker/kernel_modules_baseline.txt" ]; then
  724.         echo -e "\n== Kernel Modules Comparison ==" >> "$report_file"
  725.         echo "Finding current kernel modules..."
  726.         current_modules=$(mktemp)
  727.         lsmod | sort > "$current_modules"
  728.        
  729.         echo "New kernel modules:" >> "$report_file"
  730.         new_modules=$(comm -13 "/var/lib/integrity_checker/kernel_modules_baseline.txt" "$current_modules")
  731.         if [ -n "$new_modules" ]; then
  732.             echo "$new_modules" >> "$report_file"
  733.             echo "WARNING: New kernel modules detected!" | tee -a "$report_file"
  734.         else
  735.             echo "None found." >> "$report_file"
  736.         fi
  737.        
  738.         echo "Missing kernel modules:" >> "$report_file"
  739.         missing_modules=$(comm -23 "/var/lib/integrity_checker/kernel_modules_baseline.txt" "$current_modules")
  740.         if [ -n "$missing_modules" ]; then
  741.             echo "$missing_modules" >> "$report_file"
  742.             echo "WARNING: Some kernel modules from baseline are missing!" | tee -a "$report_file"
  743.         else
  744.             echo "None missing." >> "$report_file"
  745.         fi
  746.        
  747.         rm "$current_modules"
  748.     else
  749.         echo "No kernel modules baseline found." >> "$report_file"
  750.     fi
  751.    
  752.     echo "Baseline comparison completed. Report saved to $report_file"
  753. }
  754.  
  755. # Function to create a full system baseline
  756. create_full_baseline() {
  757.     echo "Creating full system baseline..."
  758.    
  759.     # Create baseline directory
  760.     sudo mkdir -p /var/lib/integrity_checker
  761.    
  762.     # SUID/SGID binaries baseline
  763.     echo "Creating SUID/SGID binaries baseline..."
  764.     sudo find / -type f \( -perm -4000 -o -perm -2000 \) -exec ls -la {} \; 2>/dev/null | sort > "/var/lib/integrity_checker/suid_sgid_baseline.txt"
  765.    
  766.     # Network listeners baseline
  767.     echo "Creating network listeners baseline..."
  768.     sudo netstat -tulpn | grep LISTEN | sort > "/var/lib/integrity_checker/network_listeners_baseline.txt"
  769.    
  770.     # Kernel modules baseline
  771.     echo "Creating kernel modules baseline..."
  772.     lsmod | sort > "/var/lib/integrity_checker/kernel_modules_baseline.txt"
  773.    
  774.     # Initialize AIDE database if available
  775.     if command -v aide >/dev/null 2>&1; then
  776.         echo "Initializing AIDE database..."
  777.         sudo aide --init
  778.         sudo cp /var/lib/aide/aide.db.new /var/lib/aide/aide.db
  779.     fi
  780.    
  781.     echo "Full system baseline created successfully."
  782. }
  783.  
  784. # Main function
  785. main() {
  786.     # Check for root privileges
  787.     if [ "$(id -u)" != "0" ]; then
  788.         echo "This script must be run as root or with sudo."
  789.         exit 1
  790.     fi
  791.    
  792.     # Check for required tools
  793.     check_required_tools
  794.    
  795.     while true; do
  796.         show_menu
  797.         read choice
  798.        
  799.         case $choice in
  800.             1) initialize_aide_database ;;
  801.             2) update_aide_database ;;
  802.             3) check_system_integrity ;;
  803.             4) scan_with_chkrootkit ;;
  804.             5) scan_with_rkhunter ;;
  805.             6) check_hidden_processes ;;
  806.             7) check_suid_sgid ;;
  807.             8) verify_package_integrity ;;
  808.             9) check_suspicious_cron ;;
  809.             10) check_suspicious_modules ;;
  810.             11) monitor_file_changes ;;
  811.             12) check_network_listeners ;;
  812.             13) generate_integrity_report ;;
  813.             14) schedule_automated_checks ;;
  814.             15) compare_with_baseline ;;
  815.             16) echo "Exiting..."; exit 0 ;;
  816.             *) echo "Invalid option. Press Enter to continue..."; read ;;
  817.         esac
  818.        
  819.         echo
  820.         echo "Operation completed. Press Enter to continue..."
  821.         read
  822.     done
  823. }
  824.  
  825. # Start the script
  826. main
  827.  
Add Comment
Please, Sign In to add comment