how to insert Hex as argument to gdb? (as arg, not as stdin)

Viewed 950

I want to do the next bash command, but actually in gdb (so i can debug it):

myProgram "`echo -en '\x41\x41\x41\x41'`"

I'm trying to do this (in gdb):

(dbg) run "`echo -en "\x41\x41\x41\x41"`"

i DON'T mean the stdin redirect:

echo -en "\x41\x41\x41\x41" > command_output.txt
gdb myProgram
(gdb) run < command_output.txt

how to insert Hex values as an agrument to a program in gdb?

2 Answers

you may try

(dbg) run $(echo -en "\x41\x41\x41\x41")

The gdb set args command can set the arguments. Using eval, which runs its arguments through printf, we can insert almost arbitrary characters using hex escapes. (We're limited by the fact that gdb will typically invoke the target using a shell, so it helps to add single quotes around each argument).

(gdb) eval "set args '%s'", "\x41\x41\x20\x41"
You can't do that without a process to debug.

That's another gdb limitation. An alternative that doesn't make gdb want to allocate memory in the target is to give eval only %c conversions and integer arguments, like this:

(gdb) eval "set args '%c%c%c%c'", 0x41, 0x41, 0x20, 0x41

But having to put the exact number of %c conversions into that format string is tedious, so let's stick with the single %s. We need to start the process, even though we're going to restart it right after we set the args using eval.

(gdb) start
Starting program: /home/mp/argprint

Temporary breakpoint 1, main (argc=1, argv=0x7ffffffee2b8) at argprint.c:4
4               for(int i=0; i < argc; i++) {

(gdb) eval "set args '%s'", "\x41\x41\x20\x41"
(gdb) run
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /home/mp/argprint 'AA A'
arg 0 is <</home/mp/argprint>>
arg 1 is <<AA A>>

If you have an old gdb that doesn't have the eval command but does have Python, you can do this:

(gdb) python gdb.execute("set args '\x41\x41\x20\x41'")
Related