0

私は基本的なファイルカーバーに取り組んでおり、現在ファイルのバイト位置の計算に固執しています。

次の手順を実行するには、コードが必要であることがわかりました。

  1. 変数で$searchQueryを見つけます
  2. $ searchQueryが見つかったら、残りの文字列を削除します
  3. 変数内に現在存在するフィールドの数を数えます
  4. 16進オフセットと$searchQuery自体を考慮に入れるためのこの変数からのマイナス2
  5. 次に、答えに2を掛けて、正しいバイト数を取得します

この例は次のとおりです。

  1. 「00052a0:b4f1 559c ffd8 ffe0 0010 4a4649460001」内で「ffd8」を見つけます
  2. 変数が「00052a0:b4f1559cffd8」に更新されます
  3. $fieldCountには「4」の値が割り当てられます
  4. $ fieldCount =((fieldCount-2))
  5. $ byteCount =((fieldCount * 2))

私はすべてを行う方法の基本的な考えを持っていますが、変数のフィールドの数を数えます。たとえば、$ searchQueryが見つかるまで、変数に含まれるフィールドの数をどのようにカウントしますか?同様に、文字列の不要な部分を削除した後、フィールドの数をカウントするにはどうすればよいですか?

grepで$searchStringを見つけた後、どのように進めるかわかりません。私の現在のコードは次のようになります。

#!/bin/bash
#***************************************************************
#Name:          fileCarver.sh
#Purpose:       Extracts files hidden within other files
#Author:        
#Date Written:      12/01/2013
#Last Updated:      12/01/2013
#***************************************************************

clear

#Request user input
printf "Please enter the input file name: "
read inputFile
printf "Please enter the search string: "
read searchString

#Search for the required string
searchFunction()
{
    #Search for required string and remove unnecessary characters
    startHexOffset=`xxd $1 | grep $2 | cut -d":" -f 1`
    #Convert the Hex Offset to Decimal
    startDecOffset=$(echo "ibase=16;${startHexOffset^^}" | bc)
}

searchFunction $inputFile $searchString


exit 0

助けてくれてありがとう!

4

2 に答える 2

0

ファイルをより単純な形式で16進数に変換すると、これが簡単になる場合があります。たとえば、次のコマンドを使用できます

hexdump -v -e '/1 "%02x "' $FILE

すべてのバイトが正確に3文字に変換されたファイルを印刷します:2つの16進数とスペース。

ffd8バイトオフセットが接頭辞として付けられたすべてのインスタンスを見つけることができます。

hexdump -v -e '/1 "%02x "' $FILE | grep -Fbo 'ff d8 '

(バイトオフセットは3で割る必要があります。)

ffd8したがって、次を使用した最初のインスタンスからファイル全体をストリーミングできます。

tail -c+$((
  $(hexdump -v -e '/1 "%02x "' $FILE | grep -Fbo 'ff d8 ' | head -n1 | cut -f1 -d:)
  / 3 + 1)) $FILE

(これは、ファイルの表示に使用するものが、画像の終わりに達したときに停止するのに十分な知識があることを前提としています。ただし、同様に最後の終わりマーカーを見つけることができます。)

これはGNUgrepに依存します。標準のPosixgrepには-bオプションがありません。ただし、次のように実行できますawk

tail -c+$(
    hexdump -v -e '/1 "%02x\n"' $FILE |
    awk '/d8/&&p=="ff"{print NR-1;exit}{p=$1}'
  ) $FILE

オプションの説明:

tail    -c+N    file starting at byte number N (first byte is number 1)

hexdump -v      do not compress repeated lines on output
        -e 'FORMAT'  use indicated format for output:
            /1       each format consumes 1 byte
            "%02X "  output two hex digits, including leading 0, using lower case,
                     followed by a space.

grep    -F      pattern is just plain characters, not a regular expression
        -b      print the (0-based) byte offset of the... 
        -o      ... match instead of the line containing the match

cut     -f1     output the first field of each line
        -d:     fields are separated by :
于 2013-01-13T03:32:41.720 に答える
0

試す:

echo "00052a0: b4f1 559c ffd8 ffe0 0010 4a46 4946 0001"| awk '
{
for (a=1;a<=NF; a++) {
    if ($a == "ffd8") {
        print substr($0,0,index($0,$a)+length($a))
        break
        }
    }
}'

出力:00052a0:b4f1 559c ffd8

于 2021-11-28T22:13:52.780 に答える