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

    (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))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值