Terminal escape character for hexadecimal input

8

5

In my usual gnome-terminal I do a

netcat -u somewhere 1234

to start sending UDP packets to somewhere. I need to transmit the following 5 bytes, written in hexadecimal: "01 00 af 0f e1". Now how do I type a escape sequence into my terminal that causes it to send these exact 5 bytes to stdin of netcat?

Update: Just to clarify. I know several ways of actually inputting the bytes I want to the program. That is not the question. The question is what sequence of keystrokes do I type into my terminal, after the program is launched, to input a hexadecimal character that is not otherwise represented on my keyboard. (Such as 0x00, 0x01, or 0x0f.)

I am starting to think it is simply not possible, however I would be a bit surprised if that is the conclusion.

Bjarke Freund-Hansen

Posted 2009-08-06T13:26:34.257

Reputation: 1 151

Answers

8

You can use echo -e, (which is what I think richardhoskins meant.) Like this:

/bin/echo -n -e "\x01\x00\xaf\x0f\xe1" | netcat -u somewhere 1234
# -n: no newline at end, -e: interpret escapes

If your echo implementation is old, (or simply not GNU-encrusted,) the old-school way is to use octal:

/bin/echo -n -e "\001\000\257\017\341" | netcat -u somewhere 1234

Yes, octal is way, way old-school. Look at ibase and obase in the man for bc. Check to see if echo does the right thing with od ("octal dump") "od -tx1" for hex, "od -to1" for octal.

Anders Eurenius

Posted 2009-08-06T13:26:34.257

Reputation: 301

3

A simple approach, though not quite what you are looking for...

Use a hex editor to create a file with the bytes in that you want to send (you mention gnome so you may already have ghex but any hex editor should be fine). For short hex sequences I usually include the hex as part of the name (e.g. test0x0100af0fe1).

Then just redirect the content into netcat, e.g.:

mas@voco:~$ nc -o testout 127.0.1.1 80 < test0x0100af0fe1
^C
mas@voco:~$ cat testout
> 00000000 01 00 af 0e 10                                  # .....
mas@voco:~$ 

This has the disadvantage of having to prepare a separate file but it makes replication and documentation of tests easier.

mas

Posted 2009-08-06T13:26:34.257

Reputation: 2 431

I will probably end up using a solution like this, but I really thought there was a way to escape input in the terminal so that I may type the hexadecimal representation of a character and the terminal will translate it before sending it as input to the program.

I like the idea of creating files with the content in the filename, it is exactly very short strings I have to transmit, thanks. – Bjarke Freund-Hansen – 2009-08-07T07:11:45.800

1

you have to use perl in terminal example:

./program perl -e' print "\x41\x60\x34\x45"'

it will send this hex address to program. It is used often in buffer overrun as hexa address input. The page does not show everything I typed. Look on google for buffer overflow. Those pages show you everything. Here are missing back single quotes in front of perl one and second at the end of a whole input.

Alcapone

Posted 2009-08-06T13:26:34.257

Reputation:

Nice, Hex to ASCII sender. – YumYumYum – 2012-05-09T10:49:47.413

0

Append \x to the beginning of the byte. e.g. "\x01 \x00 \xaf \x0f \xe1".

Richard Hoskins

Posted 2009-08-06T13:26:34.257

Reputation: 10 260

1Nope, just tried it, it does not escape anything in the terminal. Typing "0xaf" becomes "30 78 61 66 0a" as input to the program. – Bjarke Freund-Hansen – 2009-08-06T15:03:14.440

What shell are you using? – Richard Hoskins – 2009-08-06T15:07:32.450

I am using bash, but I would think only the terminal mattered. The input is going directly to the program (netcat). – Bjarke Freund-Hansen – 2009-08-06T15:10:06.737

Edited to \xHH from 0xHH. – Richard Hoskins – 2009-08-06T15:37:23.373

1Thanks, but again, the input is just "5c 78 61 66 0a". Neither 0x nor \x escapes anything in the terminal. :/ – Bjarke Freund-Hansen – 2009-08-07T07:05:27.547