Provides context-based random comments for ragnar.
| 17 | logger = Logger(name="comment.py", level=logging.DEBUG) |
| 18 | |
| 19 | class Commentaireia: |
| 20 | """Provides context-based random comments for ragnar.""" |
| 21 | def __init__(self): |
| 22 | self.shared_data = shared_data |
| 23 | self.last_comment_time = 0 # Initialize last_comment_time |
| 24 | self.comment_delay = random.randint(self.shared_data.config['comment_delaymin'], self.shared_data.config['comment_delaymax']) # Initialize comment_delay |
| 25 | self.last_theme = None # Initialize last_theme |
| 26 | self.themes = self.load_comments(self.shared_data.commentsfile) # Load themes from JSON file |
| 27 | |
| 28 | def load_comments(self, commentsfile): |
| 29 | """Load comments from a JSON file.""" |
| 30 | cache_file = commentsfile + '.cache' |
| 31 | |
| 32 | # Check if a cached version exists and is newer than the original file |
| 33 | if os.path.exists(cache_file) and os.path.exists(commentsfile) and os.path.getmtime(cache_file) >= os.path.getmtime(commentsfile): |
| 34 | try: |
| 35 | with open(cache_file, 'r') as file: |
| 36 | comments_data = json.load(file) |
| 37 | logger.info("Comments loaded successfully from cache.") |
| 38 | return comments_data |
| 39 | except (FileNotFoundError, json.JSONDecodeError) as e: |
| 40 | logger.warning(f"Cache file is corrupted or not found: {e}. Loading from the original file.") |
| 41 | |
| 42 | # Load from the original file if cache is not used or corrupted |
| 43 | try: |
| 44 | if not os.path.exists(commentsfile): |
| 45 | logger.error(f"Comments file not found: {commentsfile}") |
| 46 | return {"IDLE": ["No comments available"]} |
| 47 | |
| 48 | with open(commentsfile, 'r', encoding='utf-8') as file: |
| 49 | comments_data = json.load(file) |
| 50 | logger.info("Comments loaded successfully from JSON file.") |
| 51 | |
| 52 | # Validate that all required themes exist |
| 53 | # These are all the possible status values that can be set in the system |
| 54 | required_themes = [ |
| 55 | "IDLE", "NetworkScanner", "NmapVulnScanner", "FTPBruteforce", |
| 56 | "TelnetBruteforce", "StealFilesRDP", "StealFilesTelnet", |
| 57 | "StealFilesSMB", "StealFilesFTP", "StealDataSQL", |
| 58 | "StealFilesSSH", "SSHBruteforce", "SMBBruteforce", |
| 59 | "RDPBruteforce", "LogStandalone", "LogStandalone2", |
| 60 | "SQLBruteforce", "ZombifySSH", "LynisPentestSSH" |
| 61 | ] |
| 62 | |
| 63 | for theme in required_themes: |
| 64 | if theme not in comments_data: |
| 65 | logger.warning(f"Required theme '{theme}' not found in comments file, adding fallback") |
| 66 | # Provide contextual fallback comments based on theme type |
| 67 | if "Bruteforce" in theme: |
| 68 | comments_data[theme] = [f"Attempting {theme.replace('Bruteforce', '')} authentication...", |
| 69 | f"Testing {theme.replace('Bruteforce', '')} credentials...", |
| 70 | f"{theme.replace('Bruteforce', '')} attack in progress..."] |
| 71 | elif "StealFiles" in theme: |
| 72 | service = theme.replace('StealFiles', '') |
| 73 | comments_data[theme] = [f"Extracting files via {service}...", |
| 74 | f"Downloading data from {service}...", |
| 75 | f"File theft in progress via {service}..."] |
| 76 | elif "StealData" in theme: |