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

前端 未结 8 827
轻奢々
轻奢々 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
    
    0 讨论(0)
  • 2020-12-02 12:59

    Regarding Josh answer: In case you want to do it for a specific address

    hexdump -C {file location}
    

    with some hex value you might have tried to add 0x but it would fail :

    dd: warning: ‘0x’ is a zero multiplier; use ‘00x’ if that is intended
    

    You can achieve this by encapsulating it with $(()) that the terminal will translate as an int value :

    mybinary={file location}
    printf '\x31\xc0\xc3' | dd of=$mybinary bs=1 seek=$((0x100)) count=3 conv=notrunc 
    
    0 讨论(0)
提交回复
热议问题