Write byte at address (hexedit/modify binary from the command line)

前端 未结 8 876
轻奢々
轻奢々 2020-12-02 12:25

Is there any straightforward way to modify a binary from the commandline? Let\'s say I know that my binary contains 1234abcd and i want to change it to 12FFabcd or FFFFabcd

8条回答
  •  不知归路
    2020-12-02 12:56

    Here's a Bash function replaceByte, which takes the following parameters:

    • the name of the file,
    • an offset of the byte in the file to rewrite, and
    • the new value of the byte (a number).
    #!/bin/bash
    
    # param 1: file
    # param 2: offset
    # param 3: value
    function replaceByte() {
        printf "$(printf '\\x%02X' $3)" | dd of="$1" bs=1 seek=$2 count=1 conv=notrunc &> /dev/null
    }
    
    # Usage:
    # replaceByte 'thefile' $offset 95
    

提交回复
热议问题