netcat

netcat

unix utility which reads and writes data across network connections, using TCP or UDP protocol.
connect to somewhere:   nc [-options] hostname port[s] [ports] ... 
listen for inbound: nc -l -p port [-options] [hostname] [port]
options:
    -c shell commands   as `-e'; use /bin/sh to exec [dangerous!!]
    -e filename     program to exec after connect [dangerous!!]
    -b          allow broadcasts
    -g gateway      source-routing hop point[s], up to 8
    -G num          source-routing pointer: 4, 8, 12, ...
    -h          this cruft
    -i secs         delay interval for lines sent, ports scanned
        -k                      set keepalive option on socket
    -l          listen mode, for inbound connects
    -n          numeric-only IP addresses, no DNS
    -o file         hex dump of traffic
    -p port         local port number
    -r          randomize local and remote ports
    -q secs         quit after EOF on stdin and delay of secs
    -s addr         local source address
    -T tos          set Type Of Service
    -t          answer TELNET negotiation
    -u          UDP mode
    -v          verbose [use twice to be more verbose]
    -w secs         timeout for connects and final net reads
    -C          Send CRLF as line-ending
    -z          zero-I/O mode [used for scanning]
port numbers can be individual or ranges: lo-hi [inclusive];
hyphens in port names must be backslash escaped (e.g. 'ftp\-data').


netcat [options] host port

Examples

Port Scanning

netcat -z -v domain.com 1-1000

Communicate through Netcat

# Listener
netcat -l 4444
nc -lvp 4444

# Client
netcat <IP> 4444
nc <IP> 4444 

Send UDP packets to port 514

nc -u 192.168.1.5 514

Send Files through Netcat

# Listener
netcat -l 4444 > received_file
netcat -l 4444 | tar xzvf -

# Client
netcat <IP> 4444 < original_file
tar -czf - * | netcat <IP> 4444

Netcat as a Simple Web Server

netcat -l 8888 < index.html
while true; do nc -l 8888 < index.html; done

Cheat Sheet

https://www.sans.org/security-resources/sec560/netcat_cheat_sheet_v1.pdf.