I am debugging a C program inside of GDB on linux. The C program prompts the user and then calls read(0,&user_buffer,24)
where user_buffer
is a 24-byte char buffer on the stack. I know that I can send binary data to the program from outside of gdb by e.g. echo -e "\x41\x42\x43\x44" | <executable>
, but is it possible for me to directly write raw bytes to the prompt from within gdb? I've only ever seen this done externally as shown, or using python like python -c 'print("\x00\xFF\xAB")'
When I try to type in something like \x41\x42\x43\x44
to the prompt within GDB, it interprets them as ascii chars. This is important for my security testing.
CodePudding user response:
is it possible for me to directly write raw bytes to the prompt from within gdb?
I don't think so.
What you can do is set a breakpoint on the line immediately after read
, hit A
24 times to make the read
return, and then "stuff" the bytes you want into the buffer from GDB. Given char buf[24] = "";
(gdb) p buf
$1 = "", '\000' <repeats 23 times>
(gdb) set buf = "\x41\x42\x43\x44"
(gdb) p buf
$2 = "ABCD", '\000' <repeats 19 times>
This is also possible without access to source / debug info, but you'll have to cast the pointer that is the 2nd argument to read
to char[24]
.
CodePudding user response:
If you put your desired input in a file, you can redirect standard input with the run
command to use that file.
$ echo -e "\x41\x42\x43\x44" > input.data
$ gdb a.out
# Set breakpoints etc.
(gdb) run < input.data