Huy Minh Ha

Software development, Tech and other stuff

Thu 01 January 2015

A netcat cheatsheet

Posted by Ha.Minh in Programming   

NETCAT CHEATSHEET

netcat is a very useful tool for testing/debugging TCP/IP and UDP networking. In this cheetsheet I am sharing some useful examples of netcat usage for everyday purpose.

Test if a particular TCP/UDP port is open

To check if a TCP port is open

    nc -v google.com 80
    # Connection to google.com port 80 [tcp/http] succeeded!

To check if a UDP port is open, simple add option -u

    nc -vu google.com 53
    # Connection to google.com port 53 [udp/domain] succeeded!

Port scan

Scan UDP ports

    nc -vzu google.com 1-65535

To scan TCP ports simply remove the -u

    nc -vz google.com 1-65535

Netcat client server

Open a server that listens to a particular port

    nc -l 2389

Open another client connecting to that port

    nc localhost 2389

Now you can (insecurely) chat between the 2 terminals.

Transfer single file

On the remote server, open a port which output anything it receives to the target file

    nc -l 2389 > test

On the local host, send the file

    cat testfile | nc remotehost 2389

Transfer whole directory

On receiver host

    nc -l 5000 | tar xvf -

On sender host

    tar cvf - /path/to/dir | nc remotehost.com 5000

Transfer whole harddrive

On receiver host

    nc -lp 5000 | sudo dd of=/backup/sdb.img.gz

On sender host

    dd if=/dev/sdb | gzip -c | nc remote_server.com 5000

Create a web proxy for a particular websites

The following commands redirect all incoming TCP/5000 connections to http://www.google.com

    mkfifo proxypipe
    while true; do nc -l 5000 0<proxypipe | nc www.google.com 80 1> proxypipe; done

Launch a remote shell

On remote host

    nc -lp 5000 -e /bin/bash

On localhost host

    nc remotehost 5000

Test network speed

REFERENCES

Brought to you by pelican_git. view original netcat.md


    
 
 

Comments