Create text report from content
(report_content)
| 13786 | # Get last 50 lines of scanning logs |
| 13787 | scan_logs = [line.strip() for line in lines[-10:] if line.strip()] |
| 13788 | all_logs.extend(scan_logs) |
| 13789 | |
| 13790 | # 3. Get nmap_vuln_scanner.py logs (vulnerability scanning) |
| 13791 | vuln_scanner_log = os.path.join(shared_data.logsdir, 'nmap_vuln_scanner.py.log') |
| 13792 | if os.path.exists(vuln_scanner_log): |
| 13793 | with open(vuln_scanner_log, 'r', encoding='utf-8', errors='ignore') as f: |
| 13794 | lines = f.readlines() |
| 13795 | # Get last 50 lines of vuln scanner logs |
| 13796 | vuln_logs = [line.strip() for line in lines[-10:] if line.strip()] |
| 13797 | all_logs.extend(vuln_logs) |
| 13798 | |
| 13799 | # 2. Get Ragnar main activity logs from data/logs directory |
| 13800 | logs_dir = shared_data.logsdir |
| 13801 | if os.path.exists(logs_dir): |
| 13802 | skip_console_logs = { |
| 13803 | 'orchestrator.py.log', |
| 13804 | 'scanning.py.log', |
| 13805 | 'nmap_vuln_scanner.py.log', |
| 13806 | 'comment.py.log' |
| 13807 | } |
| 13808 | # Look for recent log files from attack actions |
| 13809 | for log_filename in os.listdir(logs_dir): |
| 13810 | if log_filename.endswith('.log') or log_filename.endswith('.txt'): |
| 13811 | # Skip files we've already processed or explicitly excluded |
| 13812 | if log_filename in skip_console_logs: |
| 13813 | continue |
| 13814 | |
| 13815 | log_path = os.path.join(logs_dir, log_filename) |
| 13816 | try: |
| 13817 | # Get file modification time to show recent files first |
| 13818 | mod_time = os.path.getmtime(log_path) |
| 13819 | # Only show logs from last 24 hours |
| 13820 | if time.time() - mod_time < 86400: # 24 hours |
| 13821 | with open(log_path, 'r', encoding='utf-8', errors='ignore') as f: |
| 13822 | lines = f.readlines() |
| 13823 | recent_lines = [line.strip() for line in lines[-10:] if line.strip()] |
| 13824 | all_logs.extend(recent_lines) |
| 13825 | except Exception as e: |
| 13826 | # Skip files that can't be read |
| 13827 | continue |
| 13828 | |
| 13829 | noise_filters = ( |
| 13830 | 'comment.py - INFO - Comments loaded successfully from cache', |
| 13831 | ) |
| 13832 | if noise_filters: |
| 13833 | all_logs = [ |
| 13834 | log for log in all_logs |
| 13835 | if not any(noise in log for noise in noise_filters) |
| 13836 | ] |
| 13837 | |
| 13838 | # Sort logs chronologically if they have timestamps |
| 13839 | # Logs are in format: YYYY-MM-DD HH:MM:SS - filename - LEVEL - message |
| 13840 | def extract_timestamp(log_line): |
| 13841 | try: |
| 13842 | # Extract timestamp from log line (first 19 characters: YYYY-MM-DD HH:MM:SS) |
| 13843 | if len(log_line) >= 19 and log_line[4] == '-' and log_line[7] == '-': |
| 13844 | timestamp_str = log_line[:19] |
| 13845 | from datetime import datetime |
no test coverage detected