How do I interactively type \r\n-terminated query in netcat?

21,670

Solution 1

My netcat has an option -C or --crlf to replace \n by \r\n on stdin.

Alternatively, it depends on what terminal you are using. My default settings can be seen by:

$ stty -a
... lnext = ^V; ...

This shows the literal-next character for input is control-V. So typing control-v followed by control-m will insert a carriage-return (as opposed to a newline, which is what you get when you hit the return or enter key).

Solution 2

You can pipe a string to nc:

echo -ne 'GET / HTTP/1.1\r\nHost: example.com\r\n\r\n' | nc example.com 80

Check with:

$ echo -ne 'GET / HTTP/1.1\r\nHost: example.com\r\n\r\n' | hd
00000000  47 45 54 20 2f 20 48 54  54 50 2f 31 2e 31 0d 0a  |GET / HTTP/1.1..|
00000010  48 6f 73 74 3a 20 65 78  61 6d 70 6c 65 2e 63 6f  |Host: example.co|
00000020  6d 0d 0a 0d 0a                                    |m....|
00000025
Share:
21,670

Related videos on Youtube

Vi.
Author by

Vi.

Updated on September 18, 2022

Comments

  • Vi.
    Vi. almost 2 years
    $ nc example.com 80
    GET / HTTP/1.1
    Host: example.com 
    
    HTTP/1.1 200 OK
    ...
    

    It works, line separator is just 0A here instead of required 0D0A.

    How do I type a 0D0A-separated query in netcat?

    It is easy to do one-time thing with printf manually typing \r\n each time or to implement some todos-like Perl oneliner and pipe it to nc, but maybe there is some easy way I miss?

  • cYrus
    cYrus almost 9 years
    Nice find, didn't know about -C.
  • Jaime Hablutzel
    Jaime Hablutzel over 6 years
    What about sending any byte interactively through netcat, e.g. 0x01, 0x02, etc. Is it possible?.
  • Codebling
    Codebling over 4 years
    This option doesn't seem to be supported on GNU Netcat
  • meuh
    meuh over 4 years
    @CodeBling You can also try socat if you have that: socat READLINE TCP4:example.com:80,crnl
  • Codebling
    Codebling over 4 years
    @meuh good call