drebew5059 2016-06-18 19:02
浏览 269
已采纳

在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 2016-06-18 19:20
    关注

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛