In gdb, how can I write a string to memory?

前端 未结 2 1868
借酒劲吻你
借酒劲吻你 2020-12-30 08:24

It is quite straightforward to write ints or hexadecimals to a memory address with gdb:

(gdb) set {int}0x08040000 = 42
(gdb) set {int}0x08040000 = 0xffffffff         


        
相关标签:
2条回答
  • 2020-12-30 09:09

    Use strcpy()

    (gdb) p malloc(20)
    $3 = (void *) 0x6ce81808
    (gdb) p strcpy($3, "my string")
    $4 = 1827149832
    (gdb) x/s $3
    0x6ce81808: "my string"
    
    0 讨论(0)
  • 2020-12-30 09:24

    Say you have the following program:

    int main(void){
        char[] person = "Bob";
        char[] p2 = "Alice";
    
        printf("Hello %s\n");
    }
    

    With GDB you could set a breakpoint in main, and change the person's name via:

    (gdb) set main::person = { 'S', 'a', 'm', 0x00 }
    

    or more susinctly

    (gdb) set main::person = "Sam"
    

    If you want to set memory directly use:

    set {char [4]} 0x08040000 = "Ace"
    

    I'm assuming that since you're poking memory with gdb you know what you're doing, so you know about setting the null bytes for strings etc. Keep in mind if you are trying to change values for an array and you try to put in a string that is longer than what was originally allocated, you have a really good chance that you're going to corrupt memory. (example trying to set main::person to "Dilbert" is going to cause problems

    0 讨论(0)
提交回复
热议问题