繁体   English   中英

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

[英]Extract strings after a specific offset to x00 inside a binary file on linux

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

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

所以我的问题是,最简单的解决方案是什么? 我应该更专注于像 python、php 这样的脚本语言,还是有什么我不知道的东西可以更容易地达到它?

您可以简单地通过从偏移量 138 处的文件读取到缓冲区中,直到达到0x00像这样...

// 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM