How to use python to write the specific value to the specific address in the binary file

博客提到需要一个二进制文件,用符号(Ericconfig_Bin)来标记二进制文件中的地址,可能需一些汇编知识理解相关代码,还涉及将特定值写入特定地址。

    (1) we need a binary file and use symbols(Ericconfig_Bin) to token the address in the binary file, maybe need some     assembly knowledge to understand the following codes.

  .align 8                        // I will analyze this later
  .global Ericonfig_Bin  //define a global variable
  Ericonfig_Bin:
  .word 0x98765432  // define a identification token which used to verify the address in the binary
  .ascii "star"             // "star" code (0x72617473); define a identification token which used to verify the address in the binary
  .rept ( 0x80 - 2 )    // define a space initiated to zero
          .word 0x0
  .endr

(2) write the specific value into the specific address

 def read32(buf, addr):
     return struct.unpack('I', buffer(buf[addr:addr+4]))[0]
  
 def write32(buf, addr, value, mask=(~0x0)):
     data = read32(buf, address)
     data &= ~mask
     value &= mask
     data |= value
     buf[addr:addr+4] = array.array('B', struct.pack('I', data))
  
 ##find and modify.... 
 with open(file_path, 'rb') as f:
     if platform.system() == 'Linux' or sys.platform != 'cli':
         binary_start = bytearray(f.read())
     else:
         binary_start = bytearray(f.read(), encoding='latin-1')
  
 f_length = os.path.getsize(file_path) 
 row = ["1","0", "1", "0"]
 addr = 0
 addr_end = addr + f_length - 0x4           
 while (addr < addr_end) and ((read32(binary_start, addr) != 0x98765432) or (read32(binary_start, addr+4) != 0x72617473)):
        addr += 0x100 
 write32(binary_start, addr, int(row[0],16))
在提供的引用中,有两条提到了“Binary installation directory”相关内容。安装VMware Tools时提示错误“Unable to find the binary installation directory(answer BINDIR) in the installer database file "/etc/vmware-tools/locations"”,表明在安装过程中无法在指定的安装数据库文件里找到二进制安装目录 [^1]。另一种情况是已有VMware Tools版本安装,继续安装时会先卸载当前版本,选择继续后可能会围绕二进制安装目录出现后续相关信息 [^4]。 对于一般软件安装,不同软件的二进制安装目录位置可能不同。像安装VisIt时,运行visit - install脚本进行安装,若有自己构建的VisIt二进制发行版,visit - install脚本可在svn_bin目录找到,不过引用中未明确其二进制安装目录具体位置 [^2]。而Android Studio安装相关问题中,主要强调项目路径不能含中文,否则可能出现指定的Gradle安装目录不存在的错误,也未提及二进制安装目录的位置 [^3]。 ### 代码示例 由于不清楚具体软件,以下是一个简单的Python脚本示例,用于查找可能的二进制安装目录(以在Linux系统查找可执行文件为例): ```python import os def find_binary_dirs(): path_dirs = os.environ.get('PATH').split(os.pathsep) binary_dirs = [] for path in path_dirs: if os.path.isdir(path): for root, dirs, files in os.walk(path): for file in files: if os.access(os.path.join(root, file), os.X_OK): binary_dirs.append(root) break return binary_dirs binary_directories = find_binary_dirs() for dir in binary_directories: print(dir) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值