在linux上的二进制文件中,在x00的特定偏移量之后提取字符串

在linux上的二进制文件中,在x00的特定偏移量之后提取字符串

问题描述:

i seeking for the simplest way of extracting a string inside a binary file on linux (command line). As example in my case the string begins with offset 138 and ends with the first hex 00.

The last days i tried arround with hexdump and also read the documentation about several times. Sadly in all what i tried i only got as result the hex values together with the strings instead of the clean string.

So my question is, what could be the simplest solution? Should i more focus on a scripting language like python, php or is there something i don't know to reach it easier?

我寻求在linux(命令行)上提取二进制文件内的字符串的最简单方法。 例如,在我的情况下,字符串以偏移量138开始,以第一个十六进制00结束。 p>

最后几天我尝试使用hexdump进行循环,并且还多次阅读文档。 可悲的是,在我尝试的所有内容中,我只得到十六进制值和字符串而不是干净的字符串。 p>

所以我的问题是,什么可能是最简单的解决方案? 我是否应该更专注于像python,php这样的脚本语言,还是有什么我不知道的更容易实现它? p> div>

You can do this simply by reading from the file at offset 138 into buffer until you reach 0x00 like so...

// Open the file for read
$fp = fopen($fileName, "rb");
// Set the file pointer to a byte offset of 138 to begin reading
fseek($fp, 138);
$reached = false;
$buffer = "";
// Read into the buffer until we reac 0x00
do {
    $buffer .= fread($fp, 8192);
    $end = strpos($buffer, "\x00");
    if ($end !== false || feof($fp)) {
        $str = substr($buffer, 0, $end);
        $reached = true;
    }
} while(!$reached);

// $str will contain the string you're looking for