4

我正在尝试搜索二进制文件。通过十六进制编辑器查看文件后,我发现了整个文件的模式。你可以在这里看到它们。如您所见,它们位于文件列表之前和之后。

/% ......C:\Users\\Desktop\test1.pdf..9

/% ......C:\Users\\Desktop\testtesttesttest.pdf..9

我想做的是找到..9(HEX = 000039),然后“备份”直到我找到,/%......(十六进制= 2F25A01C1000000000),然后向前移动x个字节,这样我就可以获取完整路径。我现在拥有的代码如下:

$file = 'C:\Users\<username>\Desktop\bc03160ee1a59fc1.automaticDestinations-ms'
$begin_pattern = '2F25A01C1000000000' #/% ......
$end_pattern = '000039' #..9
$prevBytes = '8'
$bytes = [string]::join('', (gc $file -en byte | % {'{0:x2}' -f $_}))
[regex]::matches($bytes, $end_pattern) |
% {
$i = $_.index - $prevBytes * 2
[string]::join('', $bytes[$i..($i + $prevBytes * 2 - 1)])
}

一些输出大致翻译为:

ffff2e0000002f000000300000003b0000003200000033000000340000003500000036000000370000003800 655c4465736b746f705c466f72656e7369635f426f6f6b735c5b656e5d646566745f6d616e75616c2e706466 0000000000000000000000000000010000000a00000000000000000020410a000000000000000a00000000

ÿÿ./0;2345678?e\Desktop\deft_manual.pdf?

?sic 科学、计算机和互联网.pdf

?ware\Desktop\Dive Into Python 3.pdf?

4

1 回答 1

4

您可以使用 PowerShell 中的System.IO.BinaryReader类。

$path = "<yourPathToTheBinaryFile>"

$binaryReader = New-Object System.IO.BinaryReader([System.IO.File]::Open($path, [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read, [System.IO.FileShare]::ReadWrite))

然后您可以访问所有方法,例如:

$binaryReader.BaseStream.Seek($pos, [System.IO.SeekOrigin]::Begin)

AFAIK,如果不读取字节(使用 ReadBytes)并自己实现搜索,就没有简单的方法来“找到”模式。

于 2012-06-01T07:38:42.453 回答