|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | + |
| 4 | +######################################################################### |
| 5 | +# Global |
| 6 | +######################################################################### |
| 7 | + |
| 8 | +# Colors scheme for text formatting |
| 9 | +# Colors for text formatting |
| 10 | +RED=$(tput bold setaf 9) |
| 11 | +GREEN=$(tput bold setaf 10) |
| 12 | +YELLOW=$(tput bold setaf 11) |
| 13 | +BLUE=$(tput bold setaf 12) |
| 14 | +PURPLE=$(tput bold setaf 13) |
| 15 | +CYAN=$(tput bold setaf 14) |
| 16 | +NC=$(tput bold sgr0) |
| 17 | + |
| 18 | + |
| 19 | +#RED=$(tput setaf 1) |
| 20 | +#GREEN=$(tput setaf 2) |
| 21 | +#YELLOW=$(tput setaf 3) |
| 22 | +#BLUE=$(tput setaf 4) |
| 23 | +#PURPLE=$(tput setaf 5) |
| 24 | +#CYAN=$(tput setaf 6) |
| 25 | +#NC=$(tput sgr0) |
| 26 | + |
| 27 | +# Author |
| 28 | +Author=" Ahmad Rasheed" |
| 29 | +# Script Name |
| 30 | +Name="VISION-X Intrusion Detection System" |
| 31 | +# Script Version |
| 32 | +Version=" 0.0.1 (beta)" |
| 33 | +# Script Description |
| 34 | +Description="Monitor & Alert on Suspicious Activities" |
| 35 | +# Script URL |
| 36 | +URL="https://github.com/Ahmad-Rasheed-01/IDS.git" |
| 37 | + |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | +# SETTING-UP DIRECTORIES |
| 42 | + |
| 43 | +# Directory to monitor |
| 44 | +monitor_dir="$HOME/abc" |
| 45 | +# Base file to store initial hashes |
| 46 | +base_file="$HOME/Folder/base_hashes.txt" |
| 47 | +# Log file to store file change events |
| 48 | +log_file="$HOME/Folder/file_change_logs.txt" |
| 49 | +# Temporary file to store already alerted files |
| 50 | +alerted_file="$HOME/Folder/alerted_files.txt" |
| 51 | + |
| 52 | +######################################################################### |
| 53 | +# Functions |
| 54 | +######################################################################### |
| 55 | + |
| 56 | +# Function to print the contents of an ASCII banner from a file |
| 57 | +print_banner() { |
| 58 | + # Specify the path to the ASCII banner file |
| 59 | + banner="banner.txt" |
| 60 | + |
| 61 | + # Check if the banner file exists |
| 62 | + if [ -f "$banner" ]; then |
| 63 | + # Print the contents of the ASCII banner file |
| 64 | + cat "$banner" |
| 65 | + else |
| 66 | + # Print an error message if the banner file is not found |
| 67 | + echo "Banner file not found: $banner" |
| 68 | + fi |
| 69 | +} |
| 70 | + |
| 71 | +# Function to print the Linux-Util banner |
| 72 | +print_linux_util_banner() { |
| 73 | + echo -e "${RED}" |
| 74 | + clear |
| 75 | + print_banner |
| 76 | + echo |
| 77 | + print_message "${NC}-----------------------------------------------------------------------------------------${NC}" |
| 78 | + print_message "${CYAN}Welcome to ${YELLOW}$Name${NC}${CYAN} - ${YELLOW}$Description${NC}" |
| 79 | + print_message "${CYAN}Author:${NC}${YELLOW}$Author${NC}" |
| 80 | + print_message "${CYAN}Version:${NC}${YELLOW}$Version${NC}" |
| 81 | + print_message "${NC}-----------------------------------------------------------------------------------------${NC}" |
| 82 | + echo |
| 83 | +} |
| 84 | + |
| 85 | +# Function to wait for Enter key press to continue |
| 86 | +press_enter() { |
| 87 | + echo -e "${YELLOW}Press Enter to continue...${NC}" |
| 88 | + read -r |
| 89 | + clear |
| 90 | +} |
| 91 | + |
| 92 | +# Function to wait for Enter key press to go back to main menu |
| 93 | +press_enter_back() { |
| 94 | + echo |
| 95 | + echo -e "${YELLOW}Press Enter key to go-back...${NC}" |
| 96 | + # main_menu |
| 97 | + read -r |
| 98 | + # clear |
| 99 | +} |
| 100 | + |
| 101 | + |
| 102 | +# Trap Ctrl+C to display exit message |
| 103 | +trap exit_message INT |
| 104 | + |
| 105 | +# Function to display exit message |
| 106 | +exit_message() { |
| 107 | + print_linux_util_banner |
| 108 | + print_message "${BLUE}" "Thanks for using $Name written by $Author." |
| 109 | + exit |
| 110 | +} |
| 111 | + |
| 112 | +# Function to display colored messages |
| 113 | +print_message() { |
| 114 | + local COLOR=$1 |
| 115 | + local MESSAGE=$2 |
| 116 | + echo -e "${COLOR}${MESSAGE}${NC}" |
| 117 | +} |
| 118 | + |
| 119 | + |
| 120 | +# Function to generate and save file hashes and inodes to the base file |
| 121 | +generate_base_hashes() { |
| 122 | + echo "Base file not found in $monitor_dir. Creating base file..." |
| 123 | + for file_path in "$monitor_dir"/*; do |
| 124 | + if [[ -f "$file_path" ]]; then |
| 125 | + # Calculate hash and inode for current file |
| 126 | + hash=$(sha256sum "$file_path" 2>/dev/null | awk '{print $1}') # find "$monitor_dir" -type f -exec sha256sum {} + | awk '{print $1, $2}' > "$base_file" |
| 127 | + inode=$(stat -c '%i' "$file_path" 2>/dev/null ) |
| 128 | + echo "$hash $inode $file_path" >> "$base_file" |
| 129 | + fi |
| 130 | + done |
| 131 | +} |
| 132 | + |
| 133 | +# Function to log file change events |
| 134 | +log_file_changes() { |
| 135 | + local event="$1" |
| 136 | + local file_path="$2" |
| 137 | + local timestamp=$(date "+%Y-%m-%d %H:%M:%S") |
| 138 | + echo "$event: $file_path $timestamp" >> "$log_file" |
| 139 | +} |
| 140 | + |
| 141 | +# Function to check if the base file exists |
| 142 | +check_base_file() { |
| 143 | + if [[ ! -f "$base_file" ]]; then |
| 144 | + echo "Base file not found in $base_file. Generating base file..." |
| 145 | + generate_base_hashes |
| 146 | + fi |
| 147 | +} |
| 148 | + |
| 149 | +# Function to initialize the alerted file |
| 150 | +initialize_alerted_file() { |
| 151 | + touch "$alerted_file" |
| 152 | +} |
| 153 | + |
| 154 | +# Function to check if a file has already triggered an alert |
| 155 | +is_already_alerted() { |
| 156 | + local file_path="$1" |
| 157 | + if [[ -f "$alerted_file" ]]; then |
| 158 | + grep -qF "$file_path" "$alerted_file" |
| 159 | + else |
| 160 | + return 1 |
| 161 | + fi |
| 162 | +} |
| 163 | + |
| 164 | +# Function to mark a file as already alerted |
| 165 | +mark_as_alerted() { |
| 166 | + local file_path="$1" |
| 167 | + echo "$file_path" >> "$alerted_file" |
| 168 | +} |
| 169 | + |
| 170 | +check_active_conn_log() { |
| 171 | +netstat -ntu |
| 172 | +} |
| 173 | + |
| 174 | +# Function to create/ update the base file |
| 175 | +update_basefile() { |
| 176 | + |
| 177 | + if [[ -f "$base_file" ]]; then |
| 178 | + print_message "${YYELLOW} File already exists. Updating base file..." |
| 179 | + generate_base_hashes |
| 180 | + else |
| 181 | + print_message "${YELLOW} File not found. Creating the BaseFile..." |
| 182 | + generate_base_hashes |
| 183 | + if [[ -f "$base_file" ]]; then |
| 184 | + sleep 2 |
| 185 | + print_message "${YELLOW} BaseFile create successfully." |
| 186 | + press_enter_back |
| 187 | + fi |
| 188 | + fi |
| 189 | +} |
| 190 | +# Function to delete the basefile |
| 191 | +delete_basefile() { |
| 192 | + print_message "${RED} Warning! ${YELLOW}Press Y/y to continue" |
| 193 | + read -r response |
| 194 | + if [[ "$response" == [Yy] ]]; then |
| 195 | + print_message "${YELLOW} Deleting the BaseFile. Please Wait..." |
| 196 | + rm -rf "$base_file" |
| 197 | + sleep 2 |
| 198 | + else |
| 199 | + print_message "${YELLOW} Invalid Input. Going back to main menu..." |
| 200 | + sleep 1 |
| 201 | + main_menu |
| 202 | + #print_message "${YELLOW} Wrong " |
| 203 | + fi |
| 204 | +} |
| 205 | + |
| 206 | + |
| 207 | +# Function to check for file changes |
| 208 | +check_file_changes() { |
| 209 | + local has_changes=false |
| 210 | + local new_files_found=false |
| 211 | + |
| 212 | + # Check if the base file exists |
| 213 | + check_base_file |
| 214 | + |
| 215 | + # Check if the alerted file exists |
| 216 | + if [[ ! -f "$alerted_file" ]]; then |
| 217 | + initialize_alerted_file |
| 218 | + fi |
| 219 | + |
| 220 | + # Iterate over files in monitor_dir |
| 221 | + for file_path in "$monitor_dir"/*; do |
| 222 | + local file_name=$(basename "$file_path") |
| 223 | + local hash |
| 224 | + local inode |
| 225 | + |
| 226 | + # Calculate hash and inode for current file |
| 227 | + hash=$(sha256sum "$file_path" | awk '{print $1}') |
| 228 | + inode=$(stat -c '%i' "$file_path") |
| 229 | + |
| 230 | + # Check if file exists in base file |
| 231 | + if grep -qF "$file_name" "$base_file"; then |
| 232 | + # Check if hash and inode matches |
| 233 | + local existing_entry=$(grep "$file_name" "$base_file") |
| 234 | + local existing_hash=$(echo "$existing_entry" | awk '{print $1}') |
| 235 | + local existing_inode=$(echo "$existing_entry" | awk '{print $2}') |
| 236 | + if [[ "$existing_hash" != "$hash" || "$existing_inode" != "$inode" ]]; then |
| 237 | + if ! is_already_alerted "$file_path"; then |
| 238 | + notify-send "Modification Detected" "Modifications in file $file_name found. $(date)" |
| 239 | + log_file_changes "Modification" "$file_path" |
| 240 | + mark_as_alerted "$file_path" |
| 241 | + fi |
| 242 | + has_changes=true |
| 243 | + fi |
| 244 | + else |
| 245 | + # New file found |
| 246 | + if ! is_already_alerted "$file_path"; then |
| 247 | + notify-send "New File Detected" "New file $file_name found in $monitor_dir. $(date)" |
| 248 | + log_file_changes "New File" "$file_path" |
| 249 | + mark_as_alerted "$file_path" |
| 250 | + fi |
| 251 | + new_files_found=true |
| 252 | + has_changes=true |
| 253 | + fi |
| 254 | + done |
| 255 | + |
| 256 | + |
| 257 | + |
| 258 | + |
| 259 | + # Check for deleted files |
| 260 | + while IFS=' ' read -r existing_hash existing_inode existing_path; do |
| 261 | + if ! [[ -f "$existing_path" ]]; then |
| 262 | + # File deleted |
| 263 | + if ! is_already_alerted "$existing_path"; then |
| 264 | + file_name=$(basename "$existing_path") |
| 265 | + notify-send "File Deleted" "File $file_name has been deleted from $monitor_dir. $(date)" |
| 266 | + log_file_changes "File Deleted" "$existing_path" |
| 267 | + mark_as_alerted "$existing_path" |
| 268 | + fi |
| 269 | + has_changes=true |
| 270 | + fi |
| 271 | + done < "$base_file" |
| 272 | +} |
| 273 | + |
| 274 | +# Function to check file manipulation log file |
| 275 | +check_file_log() { |
| 276 | + # cat "$HOME/Folder/file_change_logs.txt" |
| 277 | + cat "$log_file" |
| 278 | + # sleep 5 |
| 279 | +} |
| 280 | + |
| 281 | +# Main loop to monitor the directory |
| 282 | +#while true; do |
| 283 | + # check_file_changes |
| 284 | + # sleep 5 |
| 285 | +#done |
| 286 | + |
| 287 | + |
| 288 | + |
| 289 | + |
| 290 | +########################################################################## |
| 291 | +# MAIN MENU |
| 292 | +########################################################################## |
| 293 | + |
| 294 | +# Print Banner |
| 295 | + |
| 296 | + |
| 297 | +main_menu() { |
| 298 | + print_linux_util_banner |
| 299 | + |
| 300 | + print_message "${YELLOW}" "Select an option:" |
| 301 | + print_message "${GREEN} 1. ${PURPLE} Check File Manipulation Logs." |
| 302 | + print_message "${GREEN} 2. ${PURPLE} Check Active Network Connections." |
| 303 | + print_message "${GREEN} 3. ${PURPLE} Update BaseFile" |
| 304 | + print_message "${GREEN} 4. ${PURPLE} Delete BaseFile" |
| 305 | + print_message "${GREEN} 5. ${PURPLE} Exit" |
| 306 | + echo |
| 307 | + read -p "Enter your choice: " choice |
| 308 | + echo |
| 309 | + |
| 310 | + case $choice in |
| 311 | + 1) |
| 312 | + check_file_log |
| 313 | + # print_message "${RED}" "Press enter to back to main menu." |
| 314 | + press_enter_back |
| 315 | + main_menu |
| 316 | + ;; |
| 317 | + 2) |
| 318 | + check_active_conn_log |
| 319 | + press_enter_back |
| 320 | + main_menu |
| 321 | + ;; |
| 322 | + 3) update_basefile |
| 323 | + ;; |
| 324 | + 4) delete_basefile |
| 325 | + ;; |
| 326 | + 5) |
| 327 | + print_message "${YELLOW}" "Exiting..." |
| 328 | + sleep 1 |
| 329 | + exit_message |
| 330 | + ;; |
| 331 | + *) |
| 332 | + print_message "${RED}" "Invalid option. Please try again." |
| 333 | + press_enter |
| 334 | + main_menu |
| 335 | + ;; |
| 336 | + esac |
| 337 | +} |
| 338 | + |
| 339 | +######################################################################### |
| 340 | +# Main |
| 341 | +######################################################################### |
| 342 | + |
| 343 | +# Print the main menu |
| 344 | +main_menu |
| 345 | + |
| 346 | +# Main loop to monitor the directory |
| 347 | +while true; do |
| 348 | + check_file_changes |
| 349 | + sleep 5 |
| 350 | +done |
| 351 | + |
0 commit comments