首页 > 解决方案 > 使用 PHP 在二进制中搜索十六进制标记

问题描述

我正在尝试使用条件(if)语句在搜索的每次迭代中搜索二进制文件中的标识标签,如果搜索成功完成,它将回显一条消息和已识别的十六进制标签的位置。

当我在 CMD 中执行代码时,它没有返回任何响应,除了: echo "test 1"; 回声“测试2”;

我的代码:

<?php

// Author:      Labrador
// Date:        19-07-2020
// Purpose:     To modify the contents of a binary

checkBin(); 

// This function checks the input file to see if it is compatible with the program
function checkBin() {
    
    //Open file in current directory
    $file = file_get_contents(dirname(__FILE__). './file.bin'); 
    echo "test 1";
    //Convert file to hexadecimal
    $ori = strtoupper(bin2hex($file));
    $ori_hex = str_split($ori, 2);
    $ori_hex_length = count($ori_hex); 
    $string_loc = 0;
    echo "test 2";
    // Search for identification tag
    for ($i = 0; $i < ($ori_hex_length - 1); $i++) {
        if (hexdec($ori_hex[$i]) == hexdec("36")) {         
            if (hexdec($ori_hex[$i + 1]) == hexdec("30")) { 
                if (hexdec($ori_hex[$i + 2]) == hexdec("36")) {                 
                    if (hexdec($ori_hex[$i + 3]) == hexdec("59")) {
                        if (hexdec($ori_hex[$i + 4]) == hexdec("30")) {
                            {
                                $string_loc = 0x00000000 + $i;
                                echo "Binary Identified as compatible Software <strong>0x".$string_loc."</strong> \n";
                            }
                        }
                    }
                }
            }
        }
    }

    // Return false if not found
    if ($string_loc==0) 
    { 
        return false;  
        echo "Not Found!";
    }
    echo "test 3";
}

?>

标签: phphex

解决方案


推荐阅读