2

Say for instance I have a 32 element unsigned char array at address 0xdeadbeef. I would like to overwrite the contents of the array in memory. I am not compiled with -g, and so cannot just do a "set [variable name] = [my value]".

Is it possible to set the contents of the memory all at once?

I've seen someone try set *((unsigned char*) 0xdeadbeef) = "abcdefghijklmnop", but this doesn't appear to work.

Alternatively, if it isn't possible (for instance, because how would gdb know to convert that to the hex ascii representation?), is it possible to give multiple bytes, words, etc all at once? For example, I could just calculate the value in hex that I want the array to represent, but can I feed it all at once? Something like: set 0xdeadbeef = 0x4142434445464748495051

7
  • Do you not have a memset or strcpy symbol from your C library, even? Commented Sep 26, 2014 at 16:59
  • No. I am debugging through an already compiled program which I can't just re-compile. If I could do that, I definitely would use those functions :) Commented Sep 26, 2014 at 17:02
  • Right, but usually the C library is dynamically linked, so you have access to the symbol names even if the rest of the program wasn't compiled with debugging information. Commented Sep 26, 2014 at 17:05
  • Ah, that is a good point. So, just have gdb call it and pass in the memory address directly, I take it? Something like this? call memcpy(0xdeadbeef, "mycharacters", [# of mycharacters]) Commented Sep 26, 2014 at 17:09
  • Should work, yup. You might need some casts in there to make everybody happy. Commented Sep 26, 2014 at 17:26

2 Answers 2

6

There's alternative of writing char array in one command, without standard functions like strcpy().

set *(char [CHAR_ARRAY_SIZE] *) <WRITE_ADDRESS> = "YOUR_CHAR_ARRAY" 

where CHAR_ARRAY_SIZE is the size of YOUR_CHAR_ARRAY, plus extra NULL byte (null-terminated string).

e.g.

set *(char [15] *) 0x20018000 = "Write a string" 
Sign up to request clarification or add additional context in comments.

1 Comment

how would i insert an array of doublewords?
3

(Posting this just so the question has an "official" answer)

Carl's statements in the comments are entirely correct. You can do the following in gdb:

call strcpy(0xdeadbeef, "mystring")

This works for any of the functions included in the statically linked C library (memset, strncpy, etc).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.