Skip to content

Instantly share code, notes, and snippets.

@justdoit0823
Last active April 15, 2023 20:23
Show Gist options
  • Save justdoit0823/71a26cec963d08d27c1081609663b959 to your computer and use it in GitHub Desktop.
Save justdoit0823/71a26cec963d08d27c1081609663b959 to your computer and use it in GitHub Desktop.
A collection of examples about using netcat.

Netcat Examples

Establish TCP Connection

  • Connect to network host over TCP connection
$ ncat www.qq.com 80
GET / HTTP/1.1

HTTP/1.1 400 Bad Request
Server: squid/3.5.24
Date: Wed, 24 Jan 2018 04:03:32 GMT
Content-Type: text/html
Content-Length: 173
Connection: close

<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>squid/3.5.24</center>
</body>
</html>
  • Listen for conenctions on TCP port.
$ ncat -l 127.0.0.1 30000
Lebron niubi

$ ncat 127.0.0.1 30000
Lebron niubi

Redirect TCP connection

  • Redirect local TCP port to remote host
$ ncat --sh-exec "ncat www.qq.com 80" -l 30000 --keep-open

$ ncat 127.0.0.1 30000
GET / HTTP/1.1

HTTP/1.1 400 Bad Request
Server: squid/3.5.24
Date: Wed, 24 Jan 2018 04:15:22 GMT
Content-Type: text/html
Content-Length: 173
Connection: close

<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>squid/3.5.24</center>
</body>
</html>
  • Create a bash server
$ ls
contrib  core  __init__.py  __init__.py~  __init__.pyc  __pycache__  utils.py  utils.py~  utils.pyc

$ ncat --exec "/bin/bash" -l 127.0.0.1 30000 --keep-open

$ ncat 127.0.0.1 30000
ls
contrib
core
__init__.py
__init__.py~
__init__.pyc
__pycache__
utils.py
utils.py~
utils.pyc

Proxy

  • Create a http proxy server
$ ncat --proxy-type http -l 127.0.0.1 30000

$ ncat 127.0.0.1 30000
CONNECT www.qq.com:80 HTTP/1.1
Host: www.qq.com:80

HTTP/1.0 200 OK

GET / HTTP/1.1

HTTP/1.1 400 Bad Request
Server: squid/3.5.24
Date: Wed, 24 Jan 2018 04:32:48 GMT
Content-Type: text/html
Content-Length: 173
Connection: close

<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>squid/3.5.24</center>
</body>
</html>

File Transfer

  • Send a file from client to server
$ ncat -l 127.0.0.1 30000 > niubi.file
$ cat niubi.file
Lebron niubi

$ echo 'Lebron niubi' > niubi.file
$ ncat 127.0.0.1 30000 < niubi.file
  • Send a file from server to client
$ echo 'Firefox Quantum great again' > niubi.file
$ cat niubi.file
Firefox Quantum great again
$ ncat -l --send-only 127.0.0.1 30000 < niubi.file

$ ncat --recv-only 127.0.0.1 30000 > niubi.file
$ cat niubi.file
Firefox Quantum great again

Note: netcat is the equivalent on Mac OSX, and some options may be a little different.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment