解了。如果您已经下载了 JavaScript 文件,并希望在这些文件中查找AccessKeyID
和AccessKeySecret
,然后记录出现这些项的行号到一个results.txt
文件中,可以使用下面的 Python 脚本来完成此任务:
遍历下载的 JS 文件。
检查每个文件中是否包含AccessKeyID
和AccessKeySecret
。
记录这些项目及其出现的行号到results.txt
文件中。
假设你已经将所有 JS 文件下载到本地的downloaded_js
目录,下面是脚本:
Python复制代码import osimport re# 本地存储JS文件的目录download_dir = 'downloaded_js'# 结果保存文件results_file = 'results.txt'# 用于存储找到的AccessKeyID和AccessKeySecretkeys_found = {}def extract_keys_from_content(js_content, js_file): access_key_id_matches = re.finditer(r'AccessKeyID\s*[:=]\s*[\'"]([^\'"]+)[\'"]', js_content, re.IGNORECASE) access_key_secret_matches = re.finditer(r'AccessKeySecret\s*[:=]\s*[\'"]([^\'"]+)[\'"]', js_content, re.IGNORECASE) lines = js_content.split('\n') if js_file not in keys_found: keys_found[js_file] = {'AccessKeyID': [], 'AccessKeySecret': []} for match in access_key_id_matches: position = match.start() key_value = match.group(1) line_number = js_content.count('\n', 0, position) + 1 keys_found[js_file]['AccessKeyID'].append((line_number, key_value)) for match in access_key_secret_matches: position = match.start() key_value = match.group(1) line_number = js_content.count('\n', 0, position) + 1 keys_found[js_file]['AccessKeySecret'].append((line_number, key_value))# 遍历下载的 JS 文件for js_filename in os.listdir(download_dir): if js_filename.endswith('.js'): js_file_path = os.path.join(download_dir, js_filename) with open(js_file_path, 'r', encoding='utf-8') as file: js_content = file.read() extract_keys_from_content(js_content, js_filename)# 保存结果到文件with open(results_file, 'w', encoding='utf-8') as file: for js_file, keys in keys_found.items(): file.write(f"File: {js_file}\n") if keys['AccessKeyID']: file.write(" AccessKeyID:\n") for line_number, key_value in keys['AccessKeyID']: file.write(f" Line: {line_number}, Value: {key_value}\n") if keys['AccessKeySecret']: file.write(" AccessKeySecret:\n") for line_number, key_value in keys['AccessKeySecret']: file.write(f" Line: {line_number}, Value: {key_value}\n") file.write('\n')print(f"Results saved to {results_file}")
extract_keys_from_content
函数:
从JS文件内容中提取AccessKeyID
和AccessKeySecret
,并记录其行号和对应的值。
os.listdir
:
浏览downloaded_js
目录中的所有文件,处理.js
文件。
结果保存:
保存到results.txt
文件中,包括文件名、行号和对应值。
该脚本保存为find_keys.py
并在包含下载的JS文件的目录中运行,脚本会在results.txt
文件中记录相关信息。
评论列表 (0条)