1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
| import hashlib from datetime import datetime
department_tables = { 'HR': ['employee_info', 'salary_data', 'personal_info'], 'Finance': ['financial_reports', 'budget_data', 'payment_records'], 'IT': ['system_logs', 'server_data', 'network_config'], 'Sales': ['customer_data', 'sales_records', 'product_info'] }
sensitive_fields = ['salary', 'ssn', 'phone', 'email', 'address']
users = { 'user1': {'department': 'IT', 'tables': ['server_data', 'system_logs'], 'role': 'user'}, 'user2': {'department': 'Finance', 'tables': ['budget_data', 'payment_records'], 'role': 'user'}, 'user3': {'department': 'IT', 'tables': ['system_logs'], 'role': 'user'}, 'user4': {'department': 'HR', 'tables': ['employee_info', 'personal_info', 'salary_data'], 'role': 'admin'}, 'user5': {'department': 'Sales', 'tables': ['product_info', 'sales_records', 'customer_data'], 'role': 'admin'}, 'user6': {'department': 'Sales', 'tables': ['sales_records'], 'role': 'admin'}, 'user7': {'department': 'Finance', 'tables': ['payment_records', 'budget_data', 'financial_reports'], 'role': 'user'}, 'user8': {'department': 'Finance', 'tables': ['budget_data', 'payment_records', 'financial_reports'], 'role': 'admin'}, 'user9': {'department': 'HR', 'tables': ['employee_info'], 'role': 'user'}, 'user10': {'department': 'Finance', 'tables': ['financial_reports', 'budget_data'], 'role': 'user'}, 'user11': {'department': 'Sales', 'tables': ['sales_records', 'customer_data'], 'role': 'user'}, 'user12': {'department': 'Sales', 'tables': ['customer_data', 'sales_records', 'product_info'], 'role': 'user'}, 'user13': {'department': 'Sales', 'tables': ['customer_data', 'product_info'], 'role': 'user'}, 'user14': {'department': 'HR', 'tables': ['personal_info', 'employee_info'], 'role': 'user'}, 'user15': {'department': 'Sales', 'tables': ['customer_data'], 'role': 'user'}, 'user16': {'department': 'Sales', 'tables': ['product_info', 'customer_data'], 'role': 'user'}, 'user17': {'department': 'Sales', 'tables': ['customer_data'], 'role': 'user'}, 'user18': {'department': 'HR', 'tables': ['personal_info'], 'role': 'user'}, 'user19': {'department': 'Finance', 'tables': ['budget_data', 'payment_records', 'financial_reports'], 'role': 'user'}, 'user20': {'department': 'HR', 'tables': ['salary_data', 'employee_info', 'personal_info'], 'role': 'admin'} }
def parse_log_line(line): parts = line.strip().split(' ') log_id = parts[0] timestamp = ' '.join(parts[1:3]) user = parts[3] action = ' '.join(parts[4:])
if 'QUERY' in action: op_type = 'QUERY' details = action.replace('QUERY ', '') elif 'BACKUP' in action: op_type = 'BACKUP' details = action.replace('BACKUP ', '') else: op_type = action.split(' ')[0] details = action.replace(op_type + ' ', '') if op_type + ' ' in action else ''
return { 'log_id': log_id, 'timestamp': timestamp, 'user': user, 'op_type': op_type, 'details': details }
def check_violations(log): violations = []
try: time_obj = datetime.strptime(log['timestamp'], "%Y-%m-%d %H:%M:%S") if 0 <= time_obj.hour < 5: violations.append('3') except: pass
if log['op_type'] in ['QUERY', 'BACKUP']: user_info = users.get(log['user'], {}) if not user_info: return violations
table_accessed = log['details'].split(' ')[0].split(';')[0]
if table_accessed not in user_info['tables']: violations.append('1')
if 'field=' in log['details']: fields_part = log['details'].split('field=')[1] fields = fields_part.split(',')[0].split(';') for field in fields: if field.strip() in sensitive_fields: violations.append('2') break
if log['op_type'] == 'BACKUP' and user_info['role'] != 'admin': violations.append('4')
return violations
def main(): with open('database_logs.txt', 'r') as f: logs = f.readlines()
violations_list = []
for log_line in logs: try: log = parse_log_line(log_line) violations = check_violations(log) for v in violations: violations_list.append(f"{v}-{log['log_id']}") except Exception as e: print(f"Error processing line: {log_line.strip()} - {str(e)}") continue
violations_list.sort(key=lambda x: int(x.split('-')[1]))
output = ','.join(violations_list) print(f"违规记录: {output}")
md5_hash = hashlib.md5(output.encode()).hexdigest() print(f"flag{{{md5_hash}}}")
if __name__ == '__main__': main()
|