Help with sed to add delimiter to send HEX with netcat


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with sed to add delimiter to send HEX with netcat
# 1  
Old 12-23-2011
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:
Code:
tcpflow -i [interface] -C dst port [port] | xxd -p | sed 's/../&\\\\x/g;s/ $//' | nc [host] [port]

the output on the listening end:
Code:
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.
# 2  
Old 12-24-2011
Try to replace the sed command:
Code:
sed 's/../&\\\\x/g;s/ $//'

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

This User Gave Thanks to Franklin52 For This Post:
# 3  
Old 12-25-2011
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

Quote:
patx@Patx:~$ sudo tcpflow -i eth1 -B dst port 20033 | xxd -p | sed 's/../&\\\\x/g'
tcpflow[4726]: listening on eth1
^Ctcpflow[4726]: terminating
Quote:
patx@Patx:~$ sudo tcpflow -i eth1 -B dst port 20033 | xxd -p
tcpflow[4730]: listening on eth1
00a900000056008f80008f078d40f82d00d8d35a0dc994b47b80e41f785f
aeaf3644b47f273580651896ceaba8e0866eda6f15483d72867026e39be8
f4b9cee540de4e525bae440230068d4d1c1e93a7fb808ac315f36b465375
05e862efd1d71281057e7e746716451ea4bdd66499298442d5555bb39726
757ac56f9b19aca3740e88334f9ca96ebb1852b376c862b344ff949e0cc1
^Ctcpflow[4730]: terminating
Quote:
patx@Patx:~$ sudo tcpflow -i eth1 -B dst port 20033 | xxd -p | awk '{printf "\\\\x" $1}END{print ""}'
tcpflow[4733]: listening on eth1
^Ctcpflow[4733]: terminating
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

Last edited by patx; 12-25-2011 at 05:25 PM..
# 4  
Old 12-30-2011
Its ok, the problem was tcpflow, i have patched it so it does not strip non printable character when output is sent to stdout.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Using sed to split hex string

Hi, I'm looking to split the following hex string into rows of four elements. I've tried the following but it doesn't seem to work. How can I tell sed to match based on a pair of number(s) and letter(s), and add a newline every 4 pairs? In addition, I need to add another newline after every... (5 Replies)
Discussion started by: sand1234
5 Replies

2. UNIX for Advanced & Expert Users

Replace hex values using sed command

File lalo.txt contains: Á I need to replace Á by A using sed command. od -x lalo.txt 0000000 c10a 0000002 sed -e 's/\xc1\x0a/A/g' lalo.txt > lalo2.txt Also tried: sed -e 's/\xc3\x81/A/g' lalo.txt > lalo2.txt Output file lalo2.txt still has Á Unix version: SunOS 5.11 ... (9 Replies)
Discussion started by: mrreds
9 Replies

3. Shell Programming and Scripting

How to replace with "sed" some hex values by other hex values?

Assume I have a file \usr\home\\somedir\myfile123.txt and I want to replace all occurencies of the two (concatenated) hex values x'AD' x'A0' bytwo other (concatenated) hex values x'20' x'6E' How can I achieve this with the gnu sed tool? Additional question: Is there a way to let sed show... (1 Reply)
Discussion started by: pstein
1 Replies

4. Shell Programming and Scripting

How add 0 in hex conversion?

Hi, Got the below code in this forum, for i in `cat test`; do printf "%16s\n" $(echo "ibase=10;obase=16;$i" | bc) done Understand, this will not give output with '0'. Need help to get the output with '0'. Regards, Siva (2 Replies)
Discussion started by: ksgnathan
2 Replies

5. Shell Programming and Scripting

SED on cygwin not working with Hex or Octal

Hi, I have downloaded a web page that I need to cleanup before passing to xmlstarlet. Using UltraEdit's HEX utility part of my download is as follows: 3C 2F 61 3E 0A 09 0A 09 09 3C 2F 61 3E which in ASCII is </a> </a> I need to locate this string and replace it with just... (7 Replies)
Discussion started by: dazhoop
7 Replies

6. Shell Programming and Scripting

how to get data from hex file using SED or AWK based on pattern sign

I have a binary (hex) file I need to parse to get some data which are encoded this way: .* b4 . . . 01 12 .* af .* 83 L1 x1 x2 xL 84 L2 y1 y2 yL By another words there is a stream of hexadecimal bytes (in my example separated by space for better readability). I need to get value stored in... (3 Replies)
Discussion started by: sameucho
3 Replies

7. Programming

What is the difference between ios::hex and std::hex?

Hi, Is there really a difference between these two, std::hex and ios::hex?? I stumbled upon reading a line, "std::ios::hex is a bitmask (8 on gcc) and works with setf(). std::hex is the operator". Is this true? Thanks (0 Replies)
Discussion started by: royalibrahim
0 Replies

8. Shell Programming and Scripting

convert hex to binary and send to variable

Folks, can anyone help with a script to convert hex to binary digits, and break the 32 bit binary into packs of 4 and send them to 8 different variables.Any help is sincerely appreciated. Thanks venu Its in korn shell...... (24 Replies)
Discussion started by: venu
24 Replies

9. Programming

After converting the hexstr to Hex and storing the Hex in a char*

Hi All, My main intension of is to convert the Hexstring stored in a char* into hex and then prefixing it with "0x" and suffix it with ',' This has to be done for all the hexstring char* is NULL. Store the result prefixed with "0x" and suffixed with ',' in another char* and pass it to... (1 Reply)
Discussion started by: rvan
1 Replies

10. Shell Programming and Scripting

help with sed to add delimiter and new field to each row

I have a file with millions of rows that I need to add a delimiter and a new field with a zero to the end of each row. (its too big to open and do a find and replace regex) I'm looking for the next line '\n' and need to replace it with a Unit Separator (hex \037) 0 \n. I've tried the... (2 Replies)
Discussion started by: kmac
2 Replies
Login or Register to Ask a Question