在 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
我在 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