7

I'd like to write non-ASCII characters (0xfe, 0xed, etc) to a program's standard input.

There are a lot of similar questions to this one, but I didn't find an answer, because:

  • I want to write single-byte characters, not unicode characters
  • I can't pipe the output of echo or something

On OS X¹ you can test with:

nm - -

I'd like to write object files magic bytes (e.g. 0xfeedface) to nm using standard input so I can see how it does behave and I can recode it.

If I use a pipe then the second argument -, which means stdin, will never match any bytes since all the standard input will go to the first one. When using a terminal instead of a pipe, I can type Ctrl + D so the first one gets 'closed' and the second one start reading.

I tried with Ctrl + Shift + U and the Unicode Hex Input of OS X but it doesn't work -- I can't write the desired characters with it.

I also tried with the clipboard with pbcopy but it fails to read/paste non-ASCII or non-unicode characters.

How can I achieve my goal?

Don't hesitate to edit as this was a difficult question to express.

¹ The nm on linux does not handle stdin.

agc
  • 7,973
  • 2
  • 29
  • 50
Bilow
  • 2,194
  • 1
  • 19
  • 34
  • 1
    This depends on the terminal emulator. There may not be any way to type all characters on the keyboard. – Barmar Jan 10 '17 at 00:46

3 Answers3

7

You can echo your desired hex code into a file.

echo -e -n '\xde\xad\xbe\xef\xde\xad\xbe\xef' >/tmp/yo

or

echo -en \\xde\\xad\\xbe\\xef\\xde\\xad\\xbe\\xef >/tmp/yo

and make your executable to read from this file instead of stdin

./executable </tmp/yo

If you don't wan't to create a file, here's an alternative

python -c 'print("\x61\x62\x63\x64")' | /path/to/exe

If you want stdin control to be transferred back (in case if you're trying to execute an interactive shell --> we need a subshell to keep sending inputs to stdin. Otherwise, after the first input, the executable would exit as it is not going to get anything further on stdin)

( python -c 'print("\x61\x62\x63\x64")' ; cat ) | /path/to/exe

Python does some juggling with the bytes. So, incase of Python3, you'll have to do the following :

( python -c 'import sys; sys.stdout.buffer.write(b"\x61\x62\x63\x64")' ; cat ) | /path/to/exe

This answer helped me : https://reverseengineering.stackexchange.com/questions/13928/managing-inputs-for-payload-injection

Ajmal Moochingal
  • 177
  • 1
  • 10
  • 1
    Indeed, months after I realized the question was closely related to reverse engineering. The kind of race condition with `(python -c '...'; cat) | ./bin` or even `(python -c '...'; sleep 0.1; cat) | ./bin` did the trick. Thanks! – Bilow Feb 18 '19 at 13:23
3

Try a util like xxd:

# echo hex 52 to pipe, convert it to binary, which goes to stdout
echo 52 | xxd -r ; echo
R

Or for a more specialized util try ascii2binary (default input is decimal):

# echo dec 52 to pipe, convert it to binary, which goes to stdout
echo 52 | ascii2binary  ; echo
4

# echo base11 52 to pipe, convert it to binary, which goes to stdout
echo 52 | ascii2binary -b 11 ; echo
9

Or dump a series of hex chars, showing what hexdump sees:

echo 7 ef 52 ed 19 | ascii2binary -b h  | \
    hexdump -v  -e '/1  "%_ad#  "' -e '/1 " _%_u\_\n"'
0#   _bel_
1#   _ef_
2#   _R_
3#   _ed_
4#   _em_

See man xxd ascii2binary for the various tricks these utils can do.

agc
  • 7,973
  • 2
  • 29
  • 50
  • Sadly enough I can't pipe the output of those tools to my program because I have to "close" stdin, then kind of re-open it. Piping would not work so I'm wondering how to type or paste non-ascii non-unicode characters to a standard input without pipe – Bilow Jan 10 '17 at 21:23
  • @Bilow, if you have `bash`, try using code with [process substitution](https://www.gnu.org/software/bash/manual/html_node/Process-Substitution.html) like this: `nm <(echo ef ed | ascii2binary -b h) -`, this creates a temporary file from a file descriptor, (containing the output of the code in the parenthesis), which therefore should free the second `-` to receive *stdin*. See also [OS X / Linux: pipe into two processes?](http://stackoverflow.com/questions/10218103/os-x-linux-pipe-into-two-processes) – agc Jan 11 '17 at 04:37
  • Process substitutions are a good idea. But I'd like to test that very particular case, where both inputs are stdin, on the nm and more particularly on programs that students have written. Maybe can't I achieve what I want to do ? – Bilow Jan 11 '17 at 12:49
0

Using bash, you can echo the input,

echo -e -n '\x61\x62\x63\x64' | /path/someFile.sh --nox11

or use cat, which might be more comfortable when there are several lines of prompting:

cat $file | /path/someFile.sh --nox11

You can omit the --nox11, but that might help when the script spawns a new instance of terminal

Note: This will not work with /bin/sh!

kaiya
  • 271
  • 1
  • 3
  • 16