drebew5059
drebew5059
2016-06-18 19:02

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

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • doucheng1884 doucheng1884 5年前

    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
    
    点赞 评论 复制链接分享

相关推荐