如何在 linux/bash 中提取二进制文件的文本部分?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38833090/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to extract text portion of a binary file in linux/bash?
提问by RonPringadi
I have a binary file. If I open it with vi, it shows sequences of human-readable text and binary characters. What is the best way to extract the human-readable portion only using bash?
我有一个二进制文件。如果我用 vi 打开它,它会显示人类可读的文本和二进制字符序列。仅使用 bash 提取人类可读部分的最佳方法是什么?
I was thinking, maybe we can do this over a grep or sed pattern?
我在想,也许我们可以通过 grep 或 sed 模式来做到这一点?
$ cat file1.bin | grep '????' > newfile.txt
回答by Marc B
Use the strings
utility - that's exactly what it's designed for.
使用该strings
实用程序 - 这正是它的设计目的。
回答by Muurder
Here's what I used in a system that didn't have the "strings" utility installed
这是我在没有安装“字符串”实用程序的系统中使用的
cat yourfilename | tr -cd "[:print:]"
This prints the text and removes unprintable characters in one fell swoop, unlike "cat -v filename" which prints only text but requires some postprocessing to remove unwanted stuff. Note that some of the binary data may be printable so you'll still get some gibberish between the good stuff. I think strings removes this gibberish too if you can use that.
这会一举打印文本并删除不可打印的字符,这与“cat -v 文件名”不同,后者仅打印文本但需要进行一些后处理以删除不需要的内容。请注意,某些二进制数据可能是可打印的,因此您仍然会在好东西之间遇到一些胡言乱语。如果您可以使用字符串,我认为字符串也可以消除这种胡言乱语。
回答by John Zhau
If you're on a Debian distro, you can probably get radare2(r2) with just sudo apt install radare2
.
如果你使用的是Debian的发行版,你可以可能得到radare2(R2)只sudo apt install radare2
。
After you've installed r2, either with apt
, some other installer on some other distro, or by following an online guide, you can use rabin2
to extract just the text part of a binary:
在您安装 r2 之后,或者使用apt
其他发行版上的其他安装程序,或者按照在线指南,您可以使用rabin2
仅提取二进制文件的文本部分:
$ rabin2 -z your_binary
$ rabin2 -z your_binary
This is often "better" than just strings
because it outputs just the useful .data
section of the binary. Stuff outside that section isn't always very useful.
这通常比仅仅strings
因为它只输出.data
二进制文件的有用部分“更好” 。该部分之外的内容并不总是很有用。