多くのファイルで一部のバイトを変更する

多くのファイルで一部のバイトを変更する

.png私のプロジェクトをテストするには、破損したファイルがたくさん必要です。これを行うには、0x054thから0xa00thまでのすべてのバイトを0に設定する必要があります。

.pngファイルにチェックサムを含むブロックが含まれており、チェックサムを更新せずにイメージデータブロック(IDAT)を変更したいと思います。また、画像が表示されたときに見える(黒い)領域が表示されるように大量のバイトを破損させたい(ビュープログラムがチェックサムの不一致を無視すると仮定)。

これが私が今まで得たものです:

#!/bin/sh

# This script reads all .png or .PNG files in the current folder,
# sets all bytes at offsets [0x054, 0xa00] to 0
# and overwrites the files back.

temp_file_ascii=outfile.txt
temp_file_bin=outfile.png
target_dir=.
start_bytes=0x054
stop_bytes=0xa00
len_bytes=$stop_bytes-$start_bytes

for file in $(find "$target_dir" -name "*.png")         #TODO: -name "*.PNG")
do
    # Copy first part of the file unchanged.
    xxd -p -s $start_bytes "$file" > $temp_file_ascii

    # Create some zero bytes, followed by 'a',
    # because I don't know how to add just zeroes.
    echo "$len_bytes: 41" | xxd -r >> $temp_file_ascii

    # Copy the rest of the input file.
    # ??

    mv outfile.png "$file"
done

編集:許可された答えを使用して完成したスクリプト:

#!/bin/sh

if [ "$#" != 3 ]
then
    echo "Usage: "
    echo "break_png.sh <target_dir> <start_offset> <num_zeroed_bytes>"
    exit
fi

for file in $(find "$1" -name "*.png")
do
    dd if=/dev/zero of=$file bs=1 seek=$(($2)) count=$(($3)) conv=notrunc
done

ベストアンサー1

以下を使用してより簡単な操作を実行できますdd

dd if=/dev/zero \
   of="$your_target_file" \
   bs=1 \
   seek="$((start_offset))" \
   count="$((num_zeros))" \
   conv=notrunc

$start_offset消去するバイト範囲の開始(0から始まり、たとえばn番目のバイトから消去、n -1を使用)、および$num_zeros範囲の長さ。$((...))16進数を10進数に変換することを担当します。

(実行できる他のテストは代わりに設定ifされるか、チェックサムを任意のデータで上書きするように設定されます。)/dev/urandom/dev/zero

おすすめ記事