1

I have a program that takes input from argv[1] so I run it like

./myProgram Hello

But I need to input hexadecimals instead of decimal/text so something like

./myProgram \x48\x65\x6c\x6c\x6f

but the program takes that in as text instead of hexadecimals that form the word Hello.

I can't

python -c "print '\x48\x65\x6c\x6c\x6f'" | ./myProgram 

because the program takes the data in as argv[1]

How do I solve the problem? (I cannot modify the program)

Friedpanseller
  • 654
  • 3
  • 16
  • 31
  • 1
    Parse the text in your program – Jiahao Cai Mar 29 '17 at 05:20
  • 4
    Have you tried `./myProgram 0x48656c6c6f`? (it is rather difficult to guess not knowing what `myProgram` is/does.) The other option, if this is a shell exercise is `./myProgram $(python -c "print '\x48\x65\x6c\x6c\x6f'")` – David C. Rankin Mar 29 '17 at 05:26
  • Thanks @DavidC.Rankin that works! Can't set it as accepted answer though because it's a comment........ – Friedpanseller Mar 29 '17 at 11:28
  • No problem, the goal was to find something that worked for you. Glad I could help. I'll write it up if it remains unanswered in a bit. – David C. Rankin Mar 30 '17 at 02:09

3 Answers3

1

Continuing from the comment, not knowing exactly what was in ./myProgram made it a bit difficult to guess, but there are several logical things to try given what you reported.

As you point out, you need to take the hex input as the first argument to ./myProgram rather than passing it to the program on stdin. You correctly note that python -c "print '\x48\x65\x6c\x6c\x6f'" | ./myProgram fails for that reason. As you know, piping to a program redirects stdout of the initial process to stdin of the subsequent process, rather than as an argument to it.

That is where process substitution can help. When placing a process within $(...) (or the older format within backticks '...'), the output of the program is returned. Therefore, you can provide the output of python -c "print '\x48\x65\x6c\x6c\x6f'" as an argument by:

./myProgram $(python -c "print '\x48\x65\x6c\x6c\x6f'")

Lastly, if myProgram is simply looking for hexadecimal input in string format, then there is no reason for multiple escapes. Simply providing a string representation of the hex number may be enough. Something like the following:

./myProgram 0x48656c6c6f

I'm glad one of the suggestions was the "silver-bullet" you needed. Good luck with your shell use. Let us know if you have any further questions.

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
0

Let's assume you're writting a program as the post is tagged c.

1) command line parameter format will be like following:

./myProgram 0x55 0xAA 0xBB ...

or even

./myProgram 55 66 AA BB ...

2) in your program all input parameters will be available via argv array as strings. You can use sscanf with format %X to read hex values out of them and store them into unsigned integer variables. Also, in the simplest case it will look similar to:

unsigned int var;
sscanf(argv[1], "%X", &var); // will read 0x55
sscanf(argv[2], "%X", &var); // will read 0x66 and so on
dmi
  • 1,424
  • 1
  • 9
  • 9
0

David Rankin's approach works well as long as the input string does not contain whitespace. A space will cause the input to be broken into two arguments. Normally, to avoid this, one would surround the string with double quotes, but that prevents the python execution. I solved this for my case by creating an intermediate variable that I could quote:

foo=$(echo -e "\001 Hi!")
myProgram "$foo"
stark
  • 12,615
  • 3
  • 33
  • 50