BitLocker の復号化キーのファイルは utf-16のため grep -R で検索できないのでスクリプトで検索する

投稿者: ytyng 11ヶ月, 2週間 前

Windows のストレージを BitLocker で暗号化する時、そのキーファイルをファイルとして保存できる。

ファイルが複数ある場合、内容の一致するファイルを検索したくて

grep -R 'AABBCC' .

とかしても、ファイルエンコーディングが UTF-16 なのでマッチしない。

マッチさせるテクニックは、下記 StackOverflow に書いてある https://stackoverflow.com/questions/3752913/grepping-binary-files-and-utf16

これによると、おそらく

grep -Ra 'A.A.B.B.C.C.' .

とするとで実現可能そうだが、私は今回はこの方法を行わず Python スクリプトを書いて行った。 python では、 decode('utf16', errors='ignore') して str にすると内容を読める。

import glob
import os

bitlocker_key_save_dir = '<my-mounted-bitlocker-key-save-dir>'

needle = 'AABBCC'


def main():
    # bitlocker_key_save_dir 以下のファイルをサブディレクトリも含めてすべて取得
    for file_path in glob.glob(
        f'{bitlocker_key_save_dir}/**/*', recursive=True
    ):
        # file_path がファイルでなければスキップ
        if not os.path.isfile(file_path):
            continue
        print(f'\r{file_path}', end='', flush=True)
        # 内容を print
        content_bytes = open(file_path, 'rb').read()
        content = content_bytes.decode('utf16', errors='ignore')
        if needle in content:
            print('\n')
            print(content)


if __name__ == '__main__':
    main()
現在未評価

コメント

アーカイブ

2024
2023
2022
2021
2020
2019
2018
2017
2016
2015
2014
2013
2012
2011