Uname:Linux ip-10-0-5-176 6.8.0-1057-aws #60~22.04.1-Ubuntu SMP Wed May 27 08:16:59 UTC 2026 x86_64

403WebShell
403Webshell
Server IP : 3.111.61.48  /  Your IP : 216.73.216.67
Web Server : Apache
System : Linux ip-10-0-5-176 6.8.0-1057-aws #60~22.04.1-Ubuntu SMP Wed May 27 08:16:59 UTC 2026 x86_64
User : ubuntu ( 1000)
PHP Version : 8.2.31
Disable Function : exec,passthru,shell_exec,system
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : OFF  |  Sudo : ON  |  Pkexec : ON
Directory :  /etc/script/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ Back ]     

Current File : /etc/script/monitor_files.py
import smtplib
import os
from datetime import datetime
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

# Configuration
MONITOR_DIR = "/var/www/vhost"  # Directory to monitor
SCRIPT_DIR = "/etc/script"  # Directory for script and logs
DEFAULT_FILE_LOG = os.path.join(SCRIPT_DIR, "monitored_default_Store-files.log")  # Default file log
NEW_FILE_LOG = os.path.join(SCRIPT_DIR, "monitored_newly_added-files.log")  # New file log
EMAIL_LOG_FILE = os.path.join(SCRIPT_DIR, "email_alerts.log")  # Email alert log

# Max log file size in bytes (e.g., 25 MB)
MAX_LOG_FILE_SIZE = 25 * 1024 * 1024  # 25 MB

# File extensions to monitor
MONITORED_EXTENSIONS = ('.php', '.exe', '.sh', '.js', '.htaccess', '.html', '.ini', '.bash', '.webp', '.txt', '.png', '.jpeg', '.py', '.xml')

# Email Configuration
sender_email = os.getenv("SENDER_EMAIL", "uptime@arukustech.com")
receiver_email = os.getenv("RECEIVER_EMAIL", "hosting@arukustech.com")
aws_smtp_server = os.getenv("AWS_SMTP_SERVER", "email-smtp.ap-south-1.amazonaws.com")
aws_smtp_port = int(os.getenv("AWS_SMTP_PORT", 587))
aws_smtp_username = os.getenv("AWS_SMTP_USERNAME", "AKIAYOTEITKZPI26TMDM")
aws_smtp_password = os.getenv("AWS_SMTP_PASSWORD", "BLFjkzAixZKQPoRz3owT9eVITObkYtODYxE7z/sU/M5K")

# Load existing file paths from a log file
def load_file_log(file_path):
    if os.path.exists(file_path):
        with open(file_path, 'r') as log_file:
            return set(log_file.read().splitlines())
    return set()

# Get current list of files with specified extensions
def get_current_files():
    files = set()
    for root, dirs, filenames in os.walk(MONITOR_DIR):
        for filename in filenames:
            if filename.endswith(MONITORED_EXTENSIONS):  # Filter by extensions
                file_path = os.path.join(root, filename)
                files.add(file_path)
    return files

# Truncate log file if it exceeds the max size
def truncate_log_file(file_path):
    if os.path.exists(file_path) and os.path.getsize(file_path) > MAX_LOG_FILE_SIZE:
        with open(file_path, 'w') as log:
            log.truncate(0)  # Empty the file if it exceeds the size limit
        print(f"Log file size exceeded 25 MB. Truncated {file_path}")

# Log all files to the "monitored_default_Store-files.log"
def log_all_files(file_paths):
    try:
        # Truncate the log file if it exceeds 25 MB
        truncate_log_file(DEFAULT_FILE_LOG)
        
        with open(DEFAULT_FILE_LOG, 'a') as log:
            for file_path in file_paths:
                log.write(f"{file_path}\n")
        print(f"Logged all files to {DEFAULT_FILE_LOG}: {file_paths}")
    except Exception as e:
        print(f"Failed to log files to {DEFAULT_FILE_LOG}: {e}")

# Log new files to the "monitored_newly_added-files.log"
def log_new_files(file_paths, log_file):
    try:
        with open(log_file, 'a') as log:
            for file_path in file_paths:
                log.write(f"{file_path}\n")
        print(f"Logged new files to {log_file}: {file_paths}")
    except Exception as e:
        print(f"Failed to log new files to {log_file}: {e}")

# Send email alert for the contents of "monitored_newly_added-files.log"
def send_email_alert(files_to_send):
    if not files_to_send:
        return  # No files to report

    subject = "Alert: Arukus WP SRV Suspicious Files Detected"
    body = "<html><body><h2 style='color: red;'>Suspicious Files Detected</h2><ul>"

    for file in files_to_send:
        body += f"<li>{file}</li>"

    body += "</ul><p><strong>Action Required:</strong> Please review the files listed above.</p>"
    body += "<p><strong>Best regards,</strong><br>Arukus Tech Support</p></body></html>"

    msg = MIMEMultipart()
    msg['From'] = sender_email
    msg['To'] = receiver_email
    msg['Subject'] = subject
    msg.attach(MIMEText(body, 'html'))

    try:
        print("Attempting to send email...")
        with smtplib.SMTP(aws_smtp_server, aws_smtp_port) as server:
            server.starttls()
            server.login(aws_smtp_username, aws_smtp_password)
            server.sendmail(sender_email, receiver_email, msg.as_string())
        print("Email sent successfully.")

        # Log email alert
        with open(EMAIL_LOG_FILE, 'a') as email_log:
            email_log.write(f"{datetime.now()} - Email Sent for Files: {', '.join(files_to_send)}\n")
    except Exception as e:
        print(f"Failed to send email: {e}")
        with open(EMAIL_LOG_FILE, 'a') as email_log:
            email_log.write(f"{datetime.now()} - Failed to Send Email - Error: {e}\n")

# Main function to monitor and alert for new files
def main():
    # Load the existing file logs
    default_files = load_file_log(DEFAULT_FILE_LOG)
    newly_added_files = load_file_log(NEW_FILE_LOG)

    # Get the current file paths in the monitored directory
    current_files = get_current_files()

    # Identify new files not in the default log and not already in the new file log
    new_files = current_files - default_files - newly_added_files

    # If `monitored_newly_added-files.log` has entries (i.e., manually not cleared)
    if newly_added_files:
        # Send email with all files from `monitored_newly_added-files.log`
        print("Sending email with files from `monitored_newly_added-files.log`...")
        send_email_alert(newly_added_files)
        # Clear the `monitored_newly_added-files.log` after sending email
        with open(NEW_FILE_LOG, 'w') as log:
            log.truncate(0)
        print(f"Cleared {NEW_FILE_LOG} after email.")

    # If there are new files, log them
    if new_files:
        print(f"New files detected: {new_files}")
        # Log the newly detected files into the new file log
        log_new_files(new_files, NEW_FILE_LOG)
        # Log all files into the default file log (to keep track of them permanently)
        log_all_files(current_files)

if __name__ == "__main__":
    main()

Youez - 2016 - github.com/yon3zu
LinuXploit