【问题标题】:How do I extract Unicode character sequences from an MZ executable file?如何从 MZ 可执行文件中提取 Unicode 字符序列?
【发布时间】:2012-05-25 02:36:28
【问题描述】:

我想从二进制(“.exe”)文件中获取 Unicode 字符串。

当我使用这样的代码时:

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

它有效,但它只返回分隔符号, 所以当我尝试将量词更改为 3 时:

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

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

我只得到 ASCII 符号,所有 Unicode 符号都不见了。

我在哪里做错了,或者我可能对 Unicode 一无所知?


来自 cmets 的代码:

Python:

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

Perl:

$/ = "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"; }

【问题讨论】:

  • 提供这样的二进制文件。没有示例输入,就看不出哪里出了问题。
  • 你是怎么把exe变成unicode字符串的?如果它只是一个普通的字符串,那么这些正则表达式将永远无法工作。如果您正在查看 MS Windows 二进制文件,那么您可能正在查看 UTF-16。
  • 您可能错误地理解了unicode 原则。您正在尝试提取 3+ ASCII 可打印字符组(嗯,它们是 Unicode 子集)。我想你想提取 wide unicode 字符。您是否知道所需字符的编码(UTF-8、UTF-16 等)?
  • 是的,我想提取宽 unicode 字符。我并不总是知道它的编码,但通常情况下它应该是 UTF-8
  • 我希望这可以帮助理解:link这是我使用的代码: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@ArtM,@Nick Craig-Wood

标签: python regex perl unicode


【解决方案1】:

有人已经写了一个实用程序来做你想做的事:

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

【讨论】:

  • 是的,你是对的。但我想使用 Python(或 Perl)编写这样的工具。我几乎做到了,但是我遇到了宽字符的问题,我无法得到包含它们的单词。所以,现在我不能只实现这个选项:-u 仅 Unicode 搜索(Unicode 和 Ascii 是默认的)。我会很乐意提供帮助的。
  • 我现在面临同样的问题,strings 实用程序是我尝试的第一件事,但它没有找到所有 UTF-16 字符串(是的,它们是有效的)。我认为它有一个错误。
【解决方案2】:
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'

【讨论】:

  • 谢谢。 1.“Win32::Exe” - 有用的东西,但它无法获得我需要的所有信息。 2. “$octets” - 我在解码这样的序列时没有问题,我在搜索这样的序列时遇到了问题。我需要将所有字符串查找为 ASCII,例如 UNICODE,例如:示例:字:系统; UNICODE(宽字符):\0053\0079\0073\0074\0065\006D; ASCII:\53\79\73\74\65\6D;而且我不能编写正则表达式来搜索宽字符。 (如果仅在 Regexp 中存在问题)。
  • 废话,当然可以! "\x00\x53\x00\x79\x00\x73\x00\x74\x00\x65\x00\x6D" =~ encode 'UTF16-BE', "\x53\x79\x73\x74\x65\x6D"; # true ### "\x00\x53\x00\x79\x00\x73\x00\x74\x00\x65\x00\x6D" =~ encode 'UTF16-BE', 'System'; # true
  • 这就是我对 Perl 所需要的。谢谢!不幸的是,只能标记一个答案,但这是一个很好的解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-08
  • 1970-01-01
  • 2012-02-04
  • 1970-01-01
相关资源
最近更新 更多