3

バイナリ (".exe") ファイルから Unicode 文字列を取得したいと考えています。

そのようなコードを使用している場合:

    `unicode_str = re.compile( u'[\u0020-\u007e]{1,}',re.UNICODE )`

動作しますが、分離されたシンボルのみを返すため、量指定子を 3 に変更しようとすると:

パイソン: unicode_str = re.compile( u'[\u0020-\u007e]{3,}',re.UNICODE )

パール: my @a = ( $file =~ /[\x{0020}-\x{007e}]{3,}/gs );

ASCII シンボルのみを取得します。Unicode シンボルはすべてなくなりました。

どこで間違いを犯したのでしょうか、それとも Unicode について何も知らないのでしょうか?


コメントからのコード:

パイソン:

File = open( sys.argv[1], "rb" )
FileData = File.read()
File.close()
unicode_str = re.compile( u'[\u0020-\u007e]{3,}',re.UNICODE )
myList = unicode_str.findall(FileData)
for p in myList:
    print p

パール:

$/ = "newline separator";
my $input = shift;
open( File, $input );
my $file = <File>;
close( File );
my @a = ( $file =~ /[\x{0020}-\x{007e}]{3,}/gs );
foreach ( @a ) { print "$_\n"; }
4

2 に答える 2

3

誰かがあなたが望むことをするユーティリティをすでに書いています:

http://technet.microsoft.com/en-us/sysinternals/bb897439.aspx

usage: strings [-a] [-f offset] [-b bytes] [-n length] [-o] [-q] [-s] [-u] <file or directory>

Strings takes wild-card expressions for file names, and additional command line parameters are defined as follows:

-a  Ascii-only search (Unicode and Ascii is default)
-b  Bytes of file to scan
-f  File offset at which to start scanning.
-o  Print offset in file string was located
-n  Minimum string length (default is 3)
-q  Quiet (no banner)
-s  Recurse subdirectories
-u  Unicode-only search (Unicode and Ascii is default)  

To search one or more files for the presence of a particular string using strings use a command like this:

strings * | findstr /i TextToSearchFor

編集:

Python で実装する場合はこれを試してください。ただし、探している Unicode 文字の範囲を決定し、UTF-16LE として検索する必要があります。文字のペアの多くは、有効な印刷可能な Unicode のように見えます。stringsどんなアルゴリズムを使っているか分からない

import re
data = open('c:/users/metolone/util/windiff.exe','rb').read()

# Search for printable ASCII characters encoded as UTF-16LE.
pat = re.compile(ur'(?:[\x20-\x7E][\x00]){3,}')
words = [w.decode('utf-16le') for w in pat.findall(data)]
for w in words:
    print w
于 2012-05-18T00:04:08.377 に答える
0
use Win32::Exe;
my $exe = Win32::Exe->new('foo.exe');
my $inforef = $exe->get_version_info;
printf "%s: %s\n", $_, $inforef->{$_} for qw(Comments CompanyName
    FileDescription FileVersion InternalName LegalCopyright
    LegalTrademarks OriginalFilename ProductName ProductVersion);

一般的な UTF16-BE データを扱う場合は、Encodeライブラリを使用します。

use Encode qw(decode encode);
my $octets = # extracted from the exe
    "\x00\x73\x00\x6f\x00\x66\x00\x74\x00\x20\x00\x43\x00\x6f" .
    "\x00\x70\x00\x6f\x00\x72\x00\x61\x00\x74\x00\x69\x00\x6f";
my $characters = decode 'UTF16-BE', $octets, Encode::FB_CROAK;
# 'soft Coporatio'
于 2012-05-17T17:46:26.260 に答える