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
| import re import hashlib import csv from urllib.parse import unquote
def validate_username(username): return re.match(r'^[a-zA-Z0-9]+$', username) is not None
def validate_name(name): return re.match(r'^[\u4e00-\u9fa5]+$', name) is not None
def validate_idcard(idcard): if not re.match(r'^\d{17}[\dXx]$', idcard): return False
factors = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2] checksum = sum(int(idcard[i]) * factors[i] for i in range(17)) check_digit = '10X98765432'[checksum % 11] return idcard[-1].upper() == check_digit
def validate_phone(phone): valid_prefixes = ['734', '735', '736', '737', '738', '739', '747', '748', '750', '751', '752', '757', '758', '759', '772', '778', '782', '783', '784', '787', '788', '795', '798', '730', '731', '732', '740', '745', '746', '755', '756', '766', '767', '771', '775', '776', '785', '786', '796', '733', '749', '753', '773', '774', '777', '780', '781', '789', '790', '791', '793', '799'] return re.match(r'^\d{11}$', phone) and phone[:3] in valid_prefixes
def mask_username(username): if len(username) == 2: return username[0] + "*" else: return username[0] + "*" * (len(username) - 2) + username[-1]
def mask_name(name): if len(name) == 2: return name[0] + "*" else: return name[0] + "*" * (len(name) - 2) + name[-1]
def mask_idcard(idcard): return "*" * 6 + idcard[6:10] + "*" * 8
def mask_phone(phone): return phone[:3] + "*" * 4 + phone[-4:]
def mask_password(password): return hashlib.md5(password.encode()).hexdigest()
with open("error.log", "r") as file: log_data = file.read()
line_pattern = r"username=.*" kv_pattern = r"(\w+)=([^&\s]+)" specific_hex_pattern = r"\\xe6\\x82\\xa8\\xe7\\x9a\\x84\\xe5\\xaf\\x86\\xe7\\xa0\\x81\\xe4\\xb8\\xba"
extracted_data_list = []
lines = log_data.splitlines() for i, line in enumerate(lines): extracted_data = {} data_match = re.search(specific_hex_pattern, line) if data_match: hex_data = line.split(": ")[-1] extracted_data['password'] = hex_data.replace("\\n", "")
if i - 6 >= 0: next_line = lines[i - 6]
if "username=" in next_line: matched_string = re.search(line_pattern, next_line).group(0) kv_pairs = re.findall(kv_pattern, matched_string) for kv in kv_pairs: extracted_data[kv[0]] = unquote(kv[1])
extracted_data_list.append(extracted_data)
fieldnames = ['username', 'password', 'name', 'idcard', 'phone']
with open('output_file.csv', 'w', newline='', encoding='utf-8') as output_file: writer = csv.DictWriter(output_file, fieldnames=fieldnames) writer.writeheader()
for data in extracted_data_list: if (validate_username(data['username']) and validate_name(data['name']) and validate_idcard(data['idcard']) and validate_phone(data['phone'])): data['username'] = mask_username(data['username']) data['password'] = mask_password(data['password']) data['name'] = mask_name(data['name']) data['idcard'] = mask_idcard(data['idcard']) data['phone'] = mask_phone(data['phone'])
writer.writerow(data)
|