Help with sed to add delimiter to send HEX with netcat

Hello,

I want to send tcpflow dump to a TCP port in HEX data, to send with netcat i need to convert to HEX and add \\x before each HEX bytes, to do this i use this line:

tcpflow -i [interface] -C dst port [port] | xxd -p | sed 's/../&\\\\x/g;s/ $//' | nc [host] [port]

the output on the listening end:

2e\\x2e\\x2e\\x2e\\x54\\x0d\\x22\\x2e\\x2e\\x24\\x52\\x2e\\x2e\\x21\\x2e\\x2e\\x2e\\x2e\\x2e\\x66\\x

The problem is that the \\x delimiter is not added for the first byte as shown above...

thankx for your help.

Try to replace the sed command:

sed 's/../&\\\\x/g;s/ $//' 

with:

fold -2 | awk '{printf "\\\\e" $1}END{print ""}'
1 Like

Hello again and merry Xmass to all, thankx again Franklin52 for your fix

After experimenting with tcp test tool and tcpflow i discovered that tcpflow -C ( print to console ) implies -s witch convert unprintable characters to "." witch i don't want, i patched tcpflow to have -B (send output to stdout) hxxps://launchpadlibrarian.net/11992351/20_stdout-dump.diff now tcpflow -B send the data as it is. (tested with tcp test tool)

I have another problem with pipes after i convert to HEX using xxd -p it seems i cannot pipes the output to anything awk, sed ,netcat nothing works

Is there a way to fix this or is there better way to convert tcpflow stdout binary code and send as hex on a tcp port... C code, perl, python... anything that works on ubuntu 10.04

Thank you

Its ok, the problem was tcpflow, i have patched it so it does not strip non printable character when output is sent to stdout.