我正在寻找一种脚本(或更高级别的编程)语言(或例如Python或类似语言的模块)来轻松分析和操作文件中的二进制数据(例如核心转储),就像Perl允许非常顺利地操作文本文件一样。
我想做的事情包括以各种形式(二进制、十进制、十六进制)呈现任意数据块,将数据从一种字节顺序转换为另一种字节顺序等。也就是说,您通常会使用 C 或汇编来完成这些操作,但我寻找一种语言,可以快速地为高度特定的一次性目的编写小段代码。
有什么建议么?
I am looking for a scripting (or higher level programming) language (or e.g. modules for Python or similar languages) for effortlessly analyzing and manipulating binary data in files (e.g. core dumps), much like Perl allows manipulating text files very smoothly.
Things I want to do include presenting arbitrary chunks of the data in various forms (binary, decimal, hex), convert data from one endianess to another, etc. That is, things you normally would use C or assembly for, but I'm looking for a language which allows for writing tiny pieces of code for highly specific, one-time purposes very quickly.
Any suggestions?
发布评论
评论(11)
看看 python bitstring,它看起来正是你想要的:)
Take a look at python bitstring, it looks like exactly what you want :)
Python bitstring 模块是为此目的而写的。 它允许您获取二进制数据的任意切片,并通过 Python 属性提供多种不同的解释。 它还提供了大量用于构建和修改二进制数据的工具。
例如:
在位串中也有按位读取和导航的函数,就像在文件中一样; 事实上,这可以直接从文件中完成,无需将其读入内存:
还有具有不同字节序的视图以及交换字节序的能力等等 - 看看 手册。
The Python bitstring module was written for this purpose. It lets you take arbitary slices of binary data and offers a number of different interpretations through Python properties. It also gives plenty of tools for constructing and modifying binary data.
For example:
There are also functions for bit-wise reading and navigation in the bitstring, much like in files; in fact this can be done straight from a file without reading it into memory:
There are also views with different endiannesses as well as the ability to swap endianness and much more - take a look at the manual.
我一直使用 010 Editor 查看二进制文件。
它特别适合处理二进制文件。
它有一种易于使用的类似 C 的脚本语言来解析二进制文件并以非常可读的方式呈现它们(作为树、按颜色编码的字段等)。
有一些解析 zip 文件和 bmp 文件的示例脚本。
每当我创建二进制文件格式时,我总是为 010 编辑器编写一个小脚本来查看文件。 如果您有一些带有某些结构的头文件,那么为二进制文件创建一个读取器只需几分钟的时间。
I'm using 010 Editor to view binary files all the time to view binary files.
It's especially geared to work with binary files.
It has an easy to use c-like scripting language to parse binary files and present them in a very readable way (as a tree, fields coded by color, stuff like that)..
There are some example scripts to parse zipfiles and bmpfiles.
Whenever I create a binary file format, I always make a little script for 010 editor to view the files. If you've got some header files with some structs, making a reader for binary files is a matter of minutes.
任何具有打包/解包功能的高级编程语言都可以。 Perl、Python 和 Ruby 3 个都可以做到。 这是个人喜好的问题。 我在每一个中都编写了一些二进制解析,并认为 Ruby 对于这项任务来说是最简单/最优雅的。
Any high-level programming language with pack/unpack functions will do. All 3 Perl, Python and Ruby can do it. It's matter of personal preference. I wrote a bit of binary parsing in each of these and felt that Ruby was easiest/most elegant for this task.
为什么不使用 C 解释器呢? 我总是使用它们来试验片段,但您可以使用它们来编写您所描述的脚本,而不需要太多麻烦。
我一直喜欢EiC。 它已经死了,但该项目最近又复活了。 EiC 的能力令人惊讶,而且速度相当快。 还有CINT。 两者都可以针对不同的平台进行编译,尽管我认为 CINT 在 Windows 上需要 Cygwin。
Why not use a C interpreter? I always used them to experiment with snippets, but you could use one to script something like you describe without too much trouble.
I have always liked EiC. It was dead, but the project has been resurrected lately. EiC is surprisingly capable and reasonably quick. There is also CINT. Both can be compiled for different platforms, though I think CINT needs Cygwin on windows.
Python 的标准库有一些你需要的东西——特别是 array 模块让您轻松读取二进制文件的部分内容、交换字节顺序等; struct 模块允许对二进制字符串进行更细粒度的解释。 但是,两者都没有您所需要的那么丰富:例如,要以字节或半字形式呈现相同的数据,您需要在两个数组之间复制它(numpy第三方插件对于以几种不同的方式解释相同的内存区域来说更加强大),并且,例如,要以十六进制显示一些字节,没有什么太多的“捆绑”超越简单的循环或列表理解,例如
[hex(b) for b in thebytes[start:stop]]
。 我怀疑有可重用的第三方模块可以进一步促进此类任务,但我无法向您指出其中一个......Python's standard library has some of what you require -- the array module in particular lets you easily read parts of binary files, swap endianness, etc; the struct module allows for finer-grained interpretation of binary strings. However, neither is quite as rich as you require: for example, to present the same data as bytes or halfwords, you need to copy it between two arrays (the numpy third-party add-on is much more powerful for interpreting the same area of memory in several different ways), and, for example, to display some bytes in hex there's nothing much "bundled" beyond a simple loop or list comprehension such as
[hex(b) for b in thebytes[start:stop]]
. I suspect there are reusable third-party modules to facilitate such tasks yet further, but I can't point you to one...Forth 在这方面也很擅长,但有点神秘。
Forth can also be pretty good at this, but it's a bit arcane.
好吧,如果速度不是考虑因素,并且您需要 Perl,那么将二进制的每一行转换为一行字符 - 0 和 1。 是的,我知道二进制中没有换行:),但大概你有一些固定的大小——例如按字节或其他单位,用它们你可以分解二进制 blob。
然后只需对该数据使用 perl 字符串处理:)
Well, if speed is not a consideration, and you want perl, then translate each line of binary into a line of chars - 0's and 1's. Yes, I know there are no linefeeds in binary :) but presumably you have some fixed size -- e.g. by byte or some other unit, with which you can break up the binary blob.
Then just use the perl string processing on that data :)
如果您正在进行二进制级别处理,那么它的级别非常低,并且可能需要非常高效并且具有最少的依赖项/安装要求。
所以我会选择 C - 可以很好地处理字节 - 你可能可以在 google 上搜索一些处理字节的库包。
使用像 Erlang 这样的东西会带来低效率、依赖性和其他你可能不希望使用低级库的包袱。
If you're doing binary level processing, it is very low level and likely needs to be very efficient and have minimal dependencies/install requirements.
So I would go with C - handles bytes well - and you can probably google for some library packages that handle bytes.
Going with something like Erlang introduces inefficiencies, dependencies, and other baggage you probably don't want with a low-level library.
好吧,虽然这看起来可能违反直觉,但我发现 erlang 非常适合于此,即由于其强大的支持对于模式匹配,甚至对于字节和位(称为“Erlang 位语法")。 这使得创建非常高级的程序来处理字节甚至位级别的数据检查和操作变得非常容易:
并引用informIT.com:
因此,erlang 内置了对模式匹配的支持,并且它是一种函数式语言,因此具有很强的表现力,例如,请参阅 erlang 中 ueencode 的实现:
有关介绍,请参阅 Erlang 中的位级二进制文件和广义推导式。您可能还想查看以下一些指示:
Well, while it may seem counter-intuitive, I found erlang extremely well-suited for this, namely due to its powerful support for pattern matching, even for bytes and bits (called "Erlang Bit Syntax"). Which makes it very easy to create even very advanced programs that deal with inspecting and manipulating data on a byte- and even on a bit-level:
And to quote informIT.com:
So, erlang with its built-in support for pattern matching and it being a functional language is pretty expressive, see for example the implementation of ueencode in erlang:
For an introduction, see Bitlevel Binaries and Generalized Comprehensions in Erlang.You may also want to check out some of the following pointers:
Perl 的 pack 和 解压 ?
perl's pack and unpack ?