Jade Dungeon

网络工具

netcat

Netcat或者叫nc是Linux下的一个用于调试和检查网络工具包。可用于创建TCP/IP连接, 最大的用途就是用来处理 TCP/UDP 套接字。

这里我们将通过一些实例来学习 netcat 命令。

查看端口是否被打开

$ nc -zv localhost 80
Connection to localhost 80 port [tcp/http] succeeded!

(译注:出现该信息表示80端口已被打开。)

检查 8080 端口是否启用

$ nc -zv localhost 8080
nc: connect to localhost port 8080 (tcp) failed: Connection refused

(译注:该信息显示了8080端口并未打开。)

在服务器-客户端架构上使用Netcat

netcat 工具可运行于服务器模式,侦听指定端口:

$ nc -l 2389

然后你可以使用客户端模式来连接到 2389 端口:

$ nc localhost 2389

现在如果你输入一些文本,它将被发送到服务器端:

$ nc localhost 2389
HI, oschina

在服务器的终端窗口将会显示下面内容:

$ nc -l 2389
HI, oschina

使用 Netcat 来传输文件

netcat 工具还可用来传输文件,在客户端,假设我们有一个 testfile 文件:

$ cat testfile
hello oschina

而在服务器端有一个空文件名为 test

然后我们使用如下命令来启用服务器端:

$ nc -l 2389 > test

紧接着运行客户端:

cat testfile | nc localhost 2389

然后你停止服务器端,你可以查看 test 内容就是刚才客户端传过来的 testfile 文件的 内容:

$ cat test
hello oschina

Netcat 支持超时控制

多数情况我们不希望连接一直保持,那么我们可以使用-w参数来指定连接的空闲超时时间 ,该参数紧接一个数值,代表秒数,如果连接超过指定时间则连接会被终止。

服务器:

nc -l 2389

客户端:

$ nc -w 10 localhost 2389

该连接将在 10 秒后中断。

注意: 不要在服务器端同时使用-w-l参数,因为-w参数将在服务器端无效果。

Netcat 支持 IPv6

netcat 的-4-6参数用来指定IP地址类型,分别是IPv4IPv6

服务器端:

$ nc -4 -l 2389

客户端:

$ nc -4 localhost 2389

然后我们可以使用netstat命令来查看网络的情况:

$ netstat | grep 2389
tcp        0      0 localhost:2389          localhost:50851         ESTABLISHED
tcp        0      0 localhost:50851         localhost:2389          ESTABLISHED

接下来我们看看IPv6的情况:

服务器端:

$ nc -6 -l 2389

客户端:

$ nc -6 localhost 2389

再次运行netstat命令:

$ netstat | grep 2389
tcp6       0      0 localhost:2389          localhost:33234         ESTABLISHED
tcp6       0      0 localhost:33234         localhost:2389          ESTABLISHED

前缀是 tcp6 表示使用的是 IPv6 的地址。

在Netcat中禁止从标准输入中读取数据

该功能使用-d参数,请看下面例子:

服务器端:

$ nc -l 2389

客户端:

$ nc -d localhost 2389
Hi

你输入的 Hi 文本并不会送到服务器端。

强制 Netcat 服务器端保持启动状态

如果连接到服务器的客户端断开连接,那么服务器端也会跟着退出。

服务器端:

$ nc -l 2389

客户端:

$ nc localhost 2389
^C

服务器端:

$ nc -l 2389
$

上述例子中,但客户端断开时服务器端也立即退出。

我们可以通过-k参数来控制让服务器不会因为客户端的断开连接而退出。

服务器端:

$ nc -k -l 2389

客户端:

$ nc localhost 2389
^C

服务器端:

$ nc -k -l 2389

配置Netcat客户端不会因为EOF而退出

Netcat客户端可以通过-q参数来控制接收到EOF后隔多长时间才退出,该参数的单位是 秒:

客户端使用如下方式启动:

nc  -q 5  localhost 2389

现在如果客户端接收到EOF,它将等待5秒后退出。

使用 Netcat 来处理 UDP 协议

netcat默认是使用TCP协议,但也支持UDP,可使用-u参数来启用UDP协议通讯。

服务器端:

$ nc -4 -u -l 2389

客户端:

$ nc -4 -u localhost 2389

这样客户端和服务器端都使用了UDP协议,可通过netstat命令来查看:

$ netstat | grep 2389
udp        0      0 localhost:42634         localhost:2389          ESTABLISHED

nmap

Nmap亦称为Network Mapper(网络映射)是一个开源并且通用的用于Linux系统/网络管理员 的工具。nmap用于探查网络、执行安全扫描、网络核查并且在远程机器上找出开放端口。 它可以扫描在线的主机、操作系统、包过滤器和远程主机上的开放端口。

Nmap 命令使用:

# nmap [Scan Type(s)] [Options] {target specification}

如何在Linux上安装nmap:

# yum install nmap      [基于 Red Hat 的发行版]
$ sudo apt-get install nmap [基于 Debian 的发行版]

Nmap 命令和示例

我会分两个章节讲述NMAP的常见的使用方法,这篇是nmap系列的第一部分。在这个步骤里, 我用两个没有防火墙的服务器来测试nmap命令的工作。

  • 192.168.0.100 – server1.tecmint.com
  • 192.168.0.101 – server2.tecmint.com

带主机名和IP地址扫描系统

nmap工具提供了不同的方法来扫描一个系统。在这个例子中,我使用主机名为 server2.tecmint.com的机器执行扫描来找出所有开放端口,服务和系统上的MAC地址。

使用主机名扫描
[root@server1 ~]$ nmap server2.tecmint.com

Starting Nmap 4.11 ( http://www.insecure.org/nmap/ ) at 2013-11-11 15:42 EST
Interesting ports on server2.tecmint.com (192.168.0.101):
Not shown: 1674 closed ports
PORT     STATE SERVICE
22/tcp   open  ssh
80/tcp   open  http
111/tcp  open  rpcbind
957/tcp  open  unknown
3306/tcp open  mysql
8888/tcp open  sun-answerbook
MAC Address: 08:00:27:D9:8E:D7 (Cadmus Computer Systems)

Nmap finished: 1 IP address (1 host up) scanned in 0.415 seconds
You have new mail in /var/spool/mail/root
使用IP地址扫描
[root@server1 ~]$ nmap 192.168.0.101

Starting Nmap 4.11 ( http://www.insecure.org/nmap/ ) at 2013-11-18 11:04 EST
Interesting ports on server2.tecmint.com (192.168.0.101):
Not shown: 1674 closed ports
PORT     STATE SERVICE
22/tcp   open  ssh
80/tcp   open  http
111/tcp  open  rpcbind
958/tcp  open  unknown
3306/tcp open  mysql
8888/tcp open  sun-answerbook
MAC Address: 08:00:27:D9:8E:D7 (Cadmus Computer Systems)

Nmap finished: 1 IP address (1 host up) scanned in 0.465 seconds
You have new mail in /var/spool/mail/root

使用"-v"选项扫描

你可以看到带-v选项的命令给出了关于远程机器的更多信息。

[root@server1 ~]$ nmap -v server2.tecmint.com

Starting Nmap 4.11 ( http://www.insecure.org/nmap/ ) at 2013-11-11 15:43 EST
Initiating ARP Ping Scan against 192.168.0.101 [1 port] at 15:43
The ARP Ping Scan took 0.01s to scan 1 total hosts.
Initiating SYN Stealth Scan against server2.tecmint.com (192.168.0.101) [1680 ports] at 15:43
Discovered open port 22/tcp on 192.168.0.101
Discovered open port 80/tcp on 192.168.0.101
Discovered open port 8888/tcp on 192.168.0.101
Discovered open port 111/tcp on 192.168.0.101
Discovered open port 3306/tcp on 192.168.0.101
Discovered open port 957/tcp on 192.168.0.101
The SYN Stealth Scan took 0.30s to scan 1680 total ports.
Host server2.tecmint.com (192.168.0.101) appears to be up ... good.
Interesting ports on server2.tecmint.com (192.168.0.101):
Not shown: 1674 closed ports
PORT     STATE SERVICE
22/tcp   open  ssh
80/tcp   open  http
111/tcp  open  rpcbind
957/tcp  open  unknown
3306/tcp open  mysql
8888/tcp open  sun-answerbook
MAC Address: 08:00:27:D9:8E:D7 (Cadmus Computer Systems)

Nmap finished: 1 IP address (1 host up) scanned in 0.485 seconds
               Raw packets sent: 1681 (73.962KB) | Rcvd: 1681 (77.322KB)
扫描多台主机

你可以简单地通过在namap后写上它们的IP地址或者主机名来扫描多台主机。

[root@server1 ~]$ nmap 192.168.0.101 192.168.0.102 192.168.0.103

Starting Nmap 4.11 ( http://www.insecure.org/nmap/ ) at 2013-11-11 16:06 EST
Interesting ports on server2.tecmint.com (192.168.0.101):
Not shown: 1674 closed ports
PORT     STATE SERVICE
22/tcp   open  ssh
80/tcp   open  http
111/tcp  open  rpcbind
957/tcp  open  unknown
3306/tcp open  mysql
8888/tcp open  sun-answerbook
MAC Address: 08:00:27:D9:8E:D7 (Cadmus Computer Systems)
Nmap finished: 3 IP addresses (1 host up) scanned in 0.580 seconds

扫描整个子网

你可以通过通配符来使nmap扫描整个子网或者IP段。

[root@server1 ~]$ nmap 192.168.0.*

Starting Nmap 4.11 ( http://www.insecure.org/nmap/ ) at 2013-11-11 16:11 EST
Interesting ports on server1.tecmint.com (192.168.0.100):
Not shown: 1677 closed ports
PORT    STATE SERVICE
22/tcp  open  ssh
111/tcp open  rpcbind
851/tcp open  unknown

Interesting ports on server2.tecmint.com (192.168.0.101):
Not shown: 1674 closed ports
PORT     STATE SERVICE
22/tcp   open  ssh
80/tcp   open  http
111/tcp  open  rpcbind
957/tcp  open  unknown
3306/tcp open  mysql
8888/tcp open  sun-answerbook
MAC Address: 08:00:27:D9:8E:D7 (Cadmus Computer Systems)

Nmap finished: 256 IP addresses (2 hosts up) scanned in 5.550 seconds
You have new mail in /var/spool/mail/root

从上面的输出你可以看到nmap扫描了整个子网,并给出了网络中在线主机的信息。

使用IP地址的最后一段扫描多台主机

你可以简单地通过指定IP地址的最后8位执行扫描多台主机。比如说,这里我在IP地址为 192.168.0.101, 192.168.0.102192.168.0.103的机器上执行了扫描。

[root@server1 ~]$ nmap 192.168.0.101,102,103

Starting Nmap 4.11 ( http://www.insecure.org/nmap/ ) at 2013-11-11 16:09 EST
Interesting ports on server2.tecmint.com (192.168.0.101):
Not shown: 1674 closed ports
PORT     STATE SERVICE
22/tcp   open  ssh
80/tcp   open  http
111/tcp  open  rpcbind
957/tcp  open  unknown
3306/tcp open  mysql
8888/tcp open  sun-answerbook
MAC Address: 08:00:27:D9:8E:D7 (Cadmus Computer Systems)

Nmap finished: 3 IP addresses (1 host up) scanned in 0.552 seconds
You have new mail in /var/spool/mail/root

从文件中扫描主机列表

如果你有更多的主机要扫描,并且所有的主机都写在一个文件中,你可以直接让namp 读取它并执行扫描。让我们看看要怎么做。

创建一个名为nmaptest.txt的文本文件,并定义所有你想要扫描的IP地址或者服务器的 主机名。

[root@server1 ~]$ cat > nmaptest.txt

localhost
server2.tecmint.com
192.168.0.101

接着,带iL参数运行nmap命令来扫描文件中所有列出的IP地址。

[root@server1 ~]$ nmap -iL nmaptest.txt

Starting Nmap 4.11 ( http://www.insecure.org/nmap/ ) at 2013-11-18 10:58 EST
Interesting ports on localhost.localdomain (127.0.0.1):
Not shown: 1675 closed ports
PORT    STATE SERVICE
22/tcp  open  ssh
25/tcp  open  smtp
111/tcp open  rpcbind
631/tcp open  ipp
857/tcp open  unknown

Interesting ports on server2.tecmint.com (192.168.0.101):
Not shown: 1674 closed ports
PORT     STATE SERVICE
22/tcp   open  ssh
80/tcp   open  http
111/tcp  open  rpcbind
958/tcp  open  unknown
3306/tcp open  mysql
8888/tcp open  sun-answerbook
MAC Address: 08:00:27:D9:8E:D7 (Cadmus Computer Systems)

Interesting ports on server2.tecmint.com (192.168.0.101):
Not shown: 1674 closed ports
PORT     STATE SERVICE
22/tcp   open  ssh
80/tcp   open  http
111/tcp  open  rpcbind
958/tcp  open  unknown
3306/tcp open  mysql
8888/tcp open  sun-answerbook
MAC Address: 08:00:27:D9:8E:D7 (Cadmus Computer Systems)

Nmap finished: 3 IP addresses (3 hosts up) scanned in 2.047 seconds

扫描一个IP范围

在使用nmap扫描时,你可以指定一个IP范围。

[root@server1 ~]$ nmap 192.168.0.101-110

Starting Nmap 4.11 ( http://www.insecure.org/nmap/ ) at 2013-11-11 16:09 EST
Interesting ports on server2.tecmint.com (192.168.0.101):
Not shown: 1674 closed ports
PORT     STATE SERVICE
22/tcp   open  ssh
80/tcp   open  http
111/tcp  open  rpcbind
957/tcp  open  unknown
3306/tcp open  mysql
8888/tcp open  sun-answerbook
MAC Address: 08:00:27:D9:8E:D7 (Cadmus Computer Systems)

Nmap finished: 10 IP addresses (1 host up) scanned in 0.542 seconds

扫描网络时排除部分主机

你可以在执行全网扫描的时候排除一些主机,或者在使用通配符扫描时使用–exclude 选项。

[root@server1 ~]$ nmap 192.168.0.* --exclude 192.168.0.100

Starting Nmap 4.11 ( http://www.insecure.org/nmap/ ) at 2013-11-11 16:16 EST
Interesting ports on server2.tecmint.com (192.168.0.101):
Not shown: 1674 closed ports
PORT     STATE SERVICE
22/tcp   open  ssh
80/tcp   open  http
111/tcp  open  rpcbind
957/tcp  open  unknown
3306/tcp open  mysql
8888/tcp open  sun-answerbook
MAC Address: 08:00:27:D9:8E:D7 (Cadmus Computer Systems)

Nmap finished: 255 IP addresses (1 host up) scanned in 5.313 seconds
You have new mail in /var/spool/mail/root

扫描系统信息

(译注:原文这里提到了traceroute,实在并无此内容,删除之)

使用nmap,你可以检测到运行在远程主机上的操作系统和版本。要启用OS及其版本检测, 我们可以使用带-A选项使用nmap。

[root@server1 ~]$ nmap -A 192.168.0.101

Starting Nmap 4.11 ( http://www.insecure.org/nmap/ ) at 2013-11-11 16:25 EST
Interesting ports on server2.tecmint.com (192.168.0.101):
Not shown: 1674 closed ports
PORT     STATE SERVICE VERSION
22/tcp   open  ssh     OpenSSH 4.3 (protocol 2.0)
80/tcp   open  http    Apache httpd 2.2.3 ((CentOS))
111/tcp  open  rpcbind  2 (rpc #100000)
957/tcp  open  status   1 (rpc #100024)
3306/tcp open  mysql   MySQL (unauthorized)
8888/tcp open  http    lighttpd 1.4.32
MAC Address: 08:00:27:D9:8E:D7 (Cadmus Computer Systems)
No exact OS matches for host (If you know what OS is running on it, see http://www.insecure.org/cgi-bin/nmap-submit.cgi).
TCP/IP fingerprint:
SInfo(V=4.11%P=i686-redhat-linux-gnu%D=11/11%Tm=52814B66%O=22%C=1%M=080027)
TSeq(Class=TR%IPID=Z%TS=1000HZ)
T1(Resp=Y%DF=Y%W=16A0%ACK=S++%Flags=AS%Ops=MNNTNW)
T2(Resp=N)
T3(Resp=Y%DF=Y%W=16A0%ACK=S++%Flags=AS%Ops=MNNTNW)
T4(Resp=Y%DF=Y%W=0%ACK=O%Flags=R%Ops=)
T5(Resp=Y%DF=Y%W=0%ACK=S++%Flags=AR%Ops=)
T6(Resp=Y%DF=Y%W=0%ACK=O%Flags=R%Ops=)
T7(Resp=Y%DF=Y%W=0%ACK=S++%Flags=AR%Ops=)
PU(Resp=Y%DF=N%TOS=C0%IPLEN=164%RIPTL=148%RID=E%RIPCK=E%UCK=E%ULEN=134%DAT=E)

Uptime 0.169 days (since Mon Nov 11 12:22:15 2013)

Nmap finished: 1 IP address (1 host up) scanned in 22.271 seconds
You have new mail in /var/spool/mail/root

在上面的输出中,你可以看到运行在远程主机上操作系统的TCP/IP指纹和更详细的运行在 远程主机上的特定端口和服务。

使用nmap启用系统检测

使用选项-O-osscan-guess同样可以发现OS信息。

[root@server1 ~]$ nmap -O server2.tecmint.com

Starting Nmap 4.11 ( http://www.insecure.org/nmap/ ) at 2013-11-11 17:40 EST
Interesting ports on server2.tecmint.com (192.168.0.101):
Not shown: 1674 closed ports
PORT     STATE SERVICE
22/tcp   open  ssh
80/tcp   open  http
111/tcp  open  rpcbind
957/tcp  open  unknown
3306/tcp open  mysql
8888/tcp open  sun-answerbook
MAC Address: 08:00:27:D9:8E:D7 (Cadmus Computer Systems)
No exact OS matches for host (If you know what OS is running on it, see http://www.insecure.org/cgi-bin/nmap-submit.cgi).
TCP/IP fingerprint:
SInfo(V=4.11%P=i686-redhat-linux-gnu%D=11/11%Tm=52815CF4%O=22%C=1%M=080027)
TSeq(Class=TR%IPID=Z%TS=1000HZ)
T1(Resp=Y%DF=Y%W=16A0%ACK=S++%Flags=AS%Ops=MNNTNW)
T2(Resp=N)
T3(Resp=Y%DF=Y%W=16A0%ACK=S++%Flags=AS%Ops=MNNTNW)
T4(Resp=Y%DF=Y%W=0%ACK=O%Flags=Option -O and -osscan-guess also helps to discover OSR%Ops=)
T5(Resp=Y%DF=Y%W=0%ACK=S++%Flags=AR%Ops=)
T6(Resp=Y%DF=Y%W=0%ACK=O%Flags=R%Ops=)
T7(Resp=Y%DF=Y%W=0%ACK=S++%Flags=AR%Ops=)
PU(Resp=Y%DF=N%TOS=C0%IPLEN=164%RIPTL=148%RID=E%RIPCK=E%UCK=E%ULEN=134%DAT=E)

Uptime 0.221 days (since Mon Nov 11 12:22:16 2013)

Nmap finished: 1 IP address (1 host up) scanned in 11.064 seconds
You have new mail in /var/spool/mail/root

扫描主机来检测防火墙

下面的命令会在远程主机上执行扫描来检测主机上是否使用了任何包过滤器或者防火墙。

[root@server1 ~]$ nmap -sA 192.168.0.101

Starting Nmap 4.11 ( http://www.insecure.org/nmap/ ) at 2013-11-11 16:27 EST
All 1680 scanned ports on server2.tecmint.com (192.168.0.101) are UNfiltered
MAC Address: 08:00:27:D9:8E:D7 (Cadmus Computer Systems)

Nmap finished: 1 IP address (1 host up) scanned in 0.382 seconds
You have new mail in /var/spool/mail/root

扫描主机以检查其受到防火墙保护

扫描检测一个主机是否受到任何包过滤器软件或者防火墙保护。

[root@server1 ~]$ nmap -PN 192.168.0.101

Starting Nmap 4.11 ( http://www.insecure.org/nmap/ ) at 2013-11-11 16:30 EST
Interesting ports on server2.tecmint.com (192.168.0.101):
Not shown: 1674 closed ports
PORT     STATE SERVICE
22/tcp   open  ssh
80/tcp   open  http
111/tcp  open  rpcbind
957/tcp  open  unknown
3306/tcp open  mysql
8888/tcp open  sun-answerbook
MAC Address: 08:00:27:D9:8E:D7 (Cadmus Computer Systems)

Nmap finished: 1 IP address (1 host up) scanned in 0.399 seconds

找出网络中在线主机

-sP选项的帮助下,我们可以简单地检测网络中的主机是否在线,带这个选项后nmap会 跳过端口检测和其他检测。

[root@server1 ~]$ nmap -sP 192.168.0.*

Starting Nmap 4.11 ( http://www.insecure.org/nmap/ ) at 2013-11-18 11:01 EST
Host server1.tecmint.com (192.168.0.100) appears to be up.
Host server2.tecmint.com (192.168.0.101) appears to be up.
MAC Address: 08:00:27:D9:8E:D7 (Cadmus Computer Systems)
Nmap finished: 256 IP addresses (2 hosts up) scanned in 5.109 seconds

执行快速扫描

你可以带-F选项仅扫描所有列在nmap-services文件中的端口。

[root@server1 ~]$ nmap -F 192.168.0.101

Starting Nmap 4.11 ( http://www.insecure.org/nmap/ ) at 2013-11-11 16:47 EST
Interesting ports on server2.tecmint.com (192.168.0.101):
Not shown: 1234 closed ports
PORT     STATE SERVICE
22/tcp   open  ssh
80/tcp   open  http
111/tcp  open  rpcbind
3306/tcp open  mysql
8888/tcp open  sun-answerbook
MAC Address: 08:00:27:D9:8E:D7 (Cadmus Computer Systems)

Nmap finished: 1 IP address (1 host up) scanned in 0.322 seconds

找出nmap版本

你可以使用-V选项找出运行在你机器上的nmap版本。

[root@server1 ~]$ nmap -V

Nmap version 4.11 ( http://www.insecure.org/nmap/ )
You have new mail in /var/spool/mail/root

连续扫描端口

使用-r选项而不随机排列端口的扫描顺序。

[root@server1 ~]$ nmap -r 192.168.0.101

Starting Nmap 4.11 ( http://www.insecure.org/nmap/ ) at 2013-11-11 16:52 EST
Interesting ports on server2.tecmint.com (192.168.0.101):
Not shown: 1674 closed ports
PORT     STATE SERVICE
22/tcp   open  ssh
80/tcp   open  http
111/tcp  open  rpcbind
957/tcp  open  unknown
3306/tcp open  mysql
8888/tcp open  sun-answerbook
MAC Address: 08:00:27:D9:8E:D7 (Cadmus Computer Systems)

Nmap finished: 1 IP address (1 host up) scanned in 0.363 seconds

显示主机接口及路由

你可以使用nmap的–iflist选项来列出本机的主机接口和路由信息。

[root@server1 ~]$ nmap --iflist

Starting Nmap 4.11 ( http://www.insecure.org/nmap/ ) at 2013-11-11 17:07 EST
************************INTERFACES************************
DEV  (SHORT) IP/MASK          TYPE     UP MAC
lo   (lo)    127.0.0.1/8      loopback up
eth0 (eth0)  192.168.0.100/24 ethernet up 08:00:27:11:C7:89

**************************ROUTES**************************
DST/MASK      DEV  GATEWAY
192.168.0.0/0 eth0
169.254.0.0/0 eth0

在上面的输出中,你可以看到上述清单列出了你系统中的已经启用的接口及它们相应的 路由。(译注:这样你就知道可以通过这些接口扫描哪些网络了)

扫描特定端口

nmap使用不同的选项来发现远程机器上的端口。你可以用-p选项指定你想扫描的TCP端口 。默认上,nmap只会扫描TCP端口。

[root@server1 ~]$ nmap -p 80 server2.tecmint.com

Starting Nmap 4.11 ( http://www.insecure.org/nmap/ ) at 2013-11-11 17:12 EST
Interesting ports on server2.tecmint.com (192.168.0.101):
PORT   STATE SERVICE
80/tcp open  http
MAC Address: 08:00:27:D9:8E:D7 (Cadmus Computer Systems)

Nmap finished: 1 IP address (1 host up) sca

扫描TCP端口

当然,你可以指定nmap扫描的端口类型(TCP或UDP)和端口号。

[root@server1 ~]$ nmap -p T:8888,80 server2.tecmint.com

Starting Nmap 4.11 ( http://www.insecure.org/nmap/ ) at 2013-11-11 17:15 EST
Interesting ports on server2.tecmint.com (192.168.0.101):
PORT     STATE SERVICE
80/tcp   open  http
8888/tcp open  sun-answerbook
MAC Address: 08:00:27:D9:8E:D7 (Cadmus Computer Systems)

Nmap finished: 1 IP address (1 host up) scanned in 0.157 seconds

扫描UDP端口

[root@server1 ~]$ nmap -sU 53 server2.tecmint.com

Starting Nmap 4.11 ( http://www.insecure.org/nmap/ ) at 2013-11-11 17:15 EST
Interesting ports on server2.tecmint.com (192.168.0.101):
PORT     STATE SERVICE
53/udp   open  http
8888/udp open  sun-answerbook
MAC Address: 08:00:27:D9:8E:D7 (Cadmus Computer Systems)

Nmap finished: 1 IP address (1 host up) scanned in 0.157 seconds

扫描多个端口

你可以使用「-p」选项来指定多个要扫描的端口。

[root@server1 ~]$ nmap -p 80,443 192.168.0.101

Starting Nmap 4.11 ( http://www.insecure.org/nmap/ ) at 2013-11-18 10:56 EST
Interesting ports on server2.tecmint.com (192.168.0.101):
PORT    STATE  SERVICE
80/tcp  open   http
443/tcp closed https
MAC Address: 08:00:27:D9:8E:D7 (Cadmus Computer Systems)

Nmap finished: 1 IP address (1 host up) scanned in 0.190 seconds

扫描网络的端口范围

你也可以使用表达式指定扫描端口的范围。

[root@server1 ~]$  nmap -p 80-160 192.168.0.101

找出主机服务版本号

我们可以使用-sV选项找出远程主机上运行的服务及其版本号。

[root@server1 ~]$ nmap -sV 192.168.0.101

Starting Nmap 4.11 ( http://www.insecure.org/nmap/ ) at 2013-11-11 17:48 EST
Interesting ports on server2.tecmint.com (192.168.0.101):
Not shown: 1674 closed ports
PORT     STATE SERVICE VERSION
22/tcp   open  ssh     OpenSSH 4.3 (protocol 2.0)
80/tcp   open  http    Apache httpd 2.2.3 ((CentOS))
111/tcp  open  rpcbind  2 (rpc #100000)
957/tcp  open  status   1 (rpc #100024)
3306/tcp open  mysql   MySQL (unauthorized)
8888/tcp open  http    lighttpd 1.4.32
MAC Address: 08:00:27:D9:8E:D7 (Cadmus Computer Systems)

Nmap finished: 1 IP address (1 host up) scanned in 12.624 seconds

使用 TCP ACK (PA) 和 TCP Syn (PS) 扫描远程主机

有时包过滤防火墙阻止了标准ICMPping请求,在这个情况下,我们可以使用 TCP ACK 和 TCP Syn方法来扫描远程主机。

[root@server1 ~]$ nmap -PS 192.168.0.101

Starting Nmap 4.11 ( http://www.insecure.org/nmap/ ) at 2013-11-11 17:51 EST
Interesting ports on server2.tecmint.com (192.168.0.101):
Not shown: 1674 closed ports
PORT     STATE SERVICE
22/tcp   open  ssh
80/tcp   open  http
111/tcp  open  rpcbind
957/tcp  open  unknown
3306/tcp open  mysql
8888/tcp open  sun-answerbook
MAC Address: 08:00:27:D9:8E:D7 (Cadmus Computer Systems)

Nmap finished: 1 IP address (1 host up) scanned in 0.360 seconds
You have new mail in /var/spool/mail/root

用TCP ACK扫描远程主机的特定端口

[root@server1 ~]$ nmap -PA -p 22,80 192.168.0.101

Starting Nmap 4.11 ( http://www.insecure.org/nmap/ ) at 2013-11-11 18:02 EST
Interesting ports on server2.tecmint.com (192.168.0.101):
PORT   STATE SERVICE
22/tcp open  ssh
80/tcp open  http
MAC Address: 08:00:27:D9:8E:D7 (Cadmus Computer Systems)

Nmap finished: 1 IP address (1 host up) scanned in 0.166 seconds
You have new mail in /var/spool/mail/root

用TCP SYN扫描远程主机的特定端口

[root@server1 ~]$ nmap -PS -p 22,80 192.168.0.101

Starting Nmap 4.11 ( http://www.insecure.org/nmap/ ) at 2013-11-11 18:08 EST
Interesting ports on server2.tecmint.com (192.168.0.101):
PORT   STATE SERVICE
22/tcp open  ssh
80/tcp open  http
MAC Address: 08:00:27:D9:8E:D7 (Cadmus Computer Systems)

Nmap finished: 1 IP address (1 host up) scanned in 0.165 seconds
You have new mail in /var/spool/mail/root

执行隐秘扫描

[root@server1 ~]$ nmap -sS 192.168.0.101

Starting Nmap 4.11 ( http://www.insecure.org/nmap/ ) at 2013-11-11 18:10 EST
Interesting ports on server2.tecmint.com (192.168.0.101):
Not shown: 1674 closed ports
PORT     STATE SERVICE
22/tcp   open  ssh
80/tcp   open  http
111/tcp  open  rpcbind
957/tcp  open  unknown
3306/tcp open  mysql
8888/tcp open  sun-answerbook
MAC Address: 08:00:27:D9:8E:D7 (Cadmus Computer Systems)

Nmap finished: 1 IP address (1 host up) scanned in 0.383 seconds
You have new mail in /var/spool/mail/root

用TCP SYN扫描最常用的端口

[root@server1 ~]$ nmap -sT 192.168.0.101

Starting Nmap 4.11 ( http://www.insecure.org/nmap/ ) at 2013-11-11 18:12 EST
Interesting ports on server2.tecmint.com (192.168.0.101):
Not shown: 1674 closed ports
PORT     STATE SERVICE
22/tcp   open  ssh
80/tcp   open  http
111/tcp  open  rpcbind
957/tcp  open  unknown
3306/tcp open  mysql
8888/tcp open  sun-answerbook
MAC Address: 08:00:27:D9:8E:D7 (Cadmus Computer Systems)

Nmap finished: 1 IP address (1 host up) scanned in 0.406 seconds
You have new mail in /var/spool/mail/root

执行tcp空扫描来愚弄防火墙

[root@server1 ~]$ nmap -sN 192.168.0.101

Starting Nmap 4.11 ( http://www.insecure.org/nmap/ ) at 2013-11-11 19:01 EST
Interesting ports on server2.tecmint.com (192.168.0.101):
Not shown: 1674 closed ports
PORT     STATE         SERVICE
22/tcp   open|filtered ssh
80/tcp   open|filtered http
111/tcp  open|filtered rpcbind
957/tcp  open|filtered unknown
3306/tcp open|filtered mysql
8888/tcp open|filtered sun-answerbook
MAC Address: 08:00:27:D9:8E:D7 (Cadmus Computer Systems)

Nmap finished: 1 IP address (1 host up) scanned in 1.584 seconds
You have new mail in /var/spool/mail/root

这些就是目前NMAP 的用法,我会写出更有创造性的NMAP的第二部分。接着,不要走开也 别忘了分享你们有价值的评论。

wget

下载某个目录以及目录下所有文件:

wget -c -r -np -k -L -p http://www.xxx.com/path/

以下是一些常用参数:

  • -c 断点续传
  • -r 递归下载,下载指定网页某一目录下(包括子目录)的所有文件
  • -nH 下载时,同时下载跨域的图片或连接
  • -nd 递归下载时不创建一层一层的目录,把所有的文件下载到当前目录
  • -np 递归下载时不搜索上层目录,如wget -c -r www.xxx.org/pub/path/没有加参数 -np,就会同时下载path的上一级目录pub下的其它文件
  • -k 将绝对链接转为相对链接,下载整个站点后脱机浏览网页,最好加上这个参数
  • -L 递归时不进入其它主机,如wget -c -r www.xxx.org/如果网站内有一个这样的 链接* www.yyy.org,不加参数-L,就会像大火烧山一样,会递归下载www.yyy.org 网站
  • -p 下载网页所需的所有文件,如图片等
  • -A 指定要下载的文件样式列表,多个样式用逗号分隔
  • -i 后面跟一个文件,文件内指明要下载的URL

curl

查看地址位置

该命令会输出并提供 IP 地址 的 ‘地理位置’ 。

$ curl ipinfo.io 

"ip": "xx.xx.xx.xx", "hostname": "triband-del-aa.bbb.cc.ddd.bol.net.in", "city": "null", "region": "null", "country": "IN", "loc": "20,77", "org": "AS17813 Mahanagar Telephone Nigam Ltd".

HTTP请求

参数-v查看详细输出:

curl http://127.0.0.1/func01 -v

代理

curl http://127.0.0.1/func01 -x http://localhost:8888

上传文件

以POST请求体的形式:

curl http://127.0.0.1/func01 --data-binary "@test.data" 

以附件的形式:

curl http://127.0.0.1/func01 -F "@test.data" 

application/x-www-form-urlencoded

最常见的一种 POST 请求,用 curl 发起这种请求也很简单。

curl localhost:3000/api/basic -X POST -d 'hello=world&xxx=yyy&a[]=ooo&a[]=mmm'

还可以把hello=world&xxx=yyy&a[]=ooo&a[]=mmm写在名为data.txt的文件里,然后:

curl localhost:3000/api/basic -X POST -d @data.txt

二者的效果是一样的。

application/json

curl localhost:3000/api/json -X POST --header "Content-Type: application/json" -d '{"hello": "world"}' 

还可以把{"hello": "world"}写在名为data.json的文件里,然后:

curl localhost:3000/api/json -X POST --header "Content-Type: application/json" -d @data.json           

提交表单multipart/form-data

表单里的两个参数"aaa=aaa""bbb=bbb",还有一个文件mySampleData

curl -X POST -F "aaa=aaa" -F "bbb=bbb" -F "mySampleData=@./test.data" "http://localhost/function01"

注意:把文件放表单里提交与把文件放在post请求体里是不一样的。

表单中的文件还可以注明类型与文件名:

"media=@ping.out;filename=ping.txt;type=application/octet-stream"

curl 'http://localhost:8080/sendfile' -F "mySampleData=@ping.out;filename=ping.txt;type=application/octet-stream"

lsof

查看占用指定端口的进程

会输出所用正在使用 80 端口的 进程/服务 的名称。在 80 端口运行下列命令会更好理解 这个命令,它会列出所用运行在该端口的 进程/服务 。

root@localhost:/home/avi# lsof -iTCP:80 -sTCP:LISTEN
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
apache2 1566 root 5u IPv6 5805 0t0 TCP *:www (LISTEN)
apache2 1664 www-data 5u IPv6 5805 0t0 TCP *:www (LISTEN)
apache2 1665 www-data 5u IPv6 5805 0t0 TCP *:www (LISTEN)
apache2 1666 www-data 5u IPv6 5805 0t0 TCP *:www (LISTEN)
apache2 1667 www-data 5u IPv6 5805 0t0 TCP *:www (LISTEN)
apache2 1668 www-data 5u IPv6 5805 0t0 TCP *:www (LISTEN)

同样,你可以检查运行在端口 22 的进程/服务。

root@localhost:/home/avi# lsof -iTCP:22 -sTCP:LISTEN
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sshd 2261 root 3u IPv4 8366 0t0 TCP *:ssh (LISTEN)
sshd 2261 root 4u IPv6 8369 0t0 TCP *:ssh (LISTEN)

tcpdump抓包

說實在的,對於 tcpdump 這個軟體來說,你甚至可以說這個軟體其實就是個駭客軟體, 因為他不但可以分析封包的流向,連封包的內容也可以進行『監聽』, 如果你使用的傳輸資料是明碼的話,不得了,在 router 或 hub 上面就可能被人家監聽走了! 我們在第二章談到的 CSMA/CD 流程中,不是說過有所謂的『監聽軟體』嗎?這個 tcpdump 就是啦! 很可怕吶!所以,我們也要來瞭解一下這個軟體啊!(註:這個 tcpdump 必須使用 root 的身份執行)

[root@www ~]$ tcpdump [-AennqX] [-i 介面] [-w 儲存檔名] [-c 次數] \
                      [-r 檔案] [所欲擷取的封包資料格式]

選項與參數:

  • -A :封包的內容以 ASCII 顯示,通常用來捉取 WWW 的網頁封包資料。
  • -e :使用資料連接層 (OSI 第二層) 的 MAC 封包資料來顯示;
  • -nn:直接以 IP 及 port number 顯示,而非主機名與服務名稱
  • -q :僅列出較為簡短的封包資訊,每一行的內容比較精簡
  • -X :可以列出十六進位 (hex) 以及 ASCII 的封包內容,對於監聽封包內容很有用
  • -i :後面接要『監聽』的網路介面,例如 eth0, lo, ppp0 等等的介面;
  • -w :如果你要將監聽所得的封包資料儲存下來,用這個參數就對了!後面接檔名
  • -r :從後面接的檔案將封包資料讀出來。那個『檔案』是已經存在的檔案,並且這個『檔案』是由 -w 所製作出來的。
  • -c :監聽的封包數,如果沒有這個參數, tcpdump 會持續不斷的監聽,直到使用者輸入 [ctrl]-c 為止。

所欲擷取的封包資料格式:我們可以專門針對某些通訊協定或者是 IP 來源進行封包擷取,那就可以簡化輸出的結果,並取得最有用的資訊。常見的表示方法有:

  • host foo/host 127.0.0.1 :針對單部主機來進行封包擷取
  • net 192.168 :針對某個網域來進行封包的擷取;
  • src host 127.0.0.1/dst net 192.168:同時加上來源(src)或目標(dst)限制
  • tcp port 21:還可以針對通訊協定偵測,如 tcp, udp, arp, ether 等

還可以利用 and 與 or 來進行封包資料的整合顯示呢!

按IP与端口抓包

以 IP 與 port number 捉下 eth0 這個網路卡上的封包,持續 3 秒:

[root@www ~]$ tcpdump -i eth0 -nn
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
17:01:47.360523 IP 192.168.1.101.1937 > 192.168.1.100.22: Flags [.], ack 196, win 65219, 
17:01:47.362139 IP 192.168.1.100.22 > 192.168.1.101.1937: Flags [P.], seq 196:472, ack 1,
17:01:47.363201 IP 192.168.1.100.22 > 192.168.1.101.1937: Flags [P.], seq 472:636, ack 1,
17:01:47.363328 IP 192.168.1.101.1937 > 192.168.1.100.22: Flags [.], ack 636, win 64779,
<==按下 [ctrl]-c 之後結束
6680 packets captured              <==捉下來的封包數量
14250 packets received by filter   <==由過濾所得的總封包數量
7512 packets dropped by kernel     <==被核心所丟棄的封包

如果你是第一次看 tcpdump 的 man page 時,肯定一個頭兩個大,因為 tcpdump 幾乎都是分析封包的表頭資料,使用者如果沒有簡易的網路封包基礎,要看懂粉難吶! 所以,至少你得要回到網路基礎裡面去將 TCP 封包的表頭資料理解理解才好啊! _!至於那個範例一所產生的輸出範例中,我們可以約略區分為數個欄位, 我們以範例一當中第二行來說明一下:

  • 17:01:47.362139:這個是此封包被擷取的時間,『時:分:秒』的單位;
  • IP:透過的通訊協定是 IP ;
  • 192.168.1.100.22 > :傳送端是 192.168.1.100 這個 IP,而傳送的 port number 為 22,你必須要瞭解的是,那個大於 (>) 的符號指的是封包的傳輸方向喔!
  • 192.168.1.101.1937:接收端的 IP 是 192.168.1.101, 且該主機開啟 port 1937 來接收;
  • [P.], seq 196:472:這個封包帶有 PUSH 的資料傳輸標誌, 且傳輸的資料為整體資料的 196~472 byte;
  • ack 1:ACK 的相關資料。

最簡單的說法,就是該封包是由 192.168.1.100 傳到 192.168.1.101,透過的 port 是由 22 到 1937 , 使用的是 PUSH 的旗標,而不是 SYN 之類的主動連線標誌。呵呵!不容易看的懂吧!所以說,上頭才講請務必到 TCP 表頭資料的部分去瞧一瞧的啊!

再來,一個網路狀態很忙的主機上面,你想要取得某部主機對你連線的封包資料而已時, 使用 tcpdump 配合管線命令與正規表示法也可以,不過,畢竟不好捉取! 我們可以透過 tcpdump 的表示法功能,就能夠輕易的將所需要的資料獨立的取出來。 在上面的範例一當中,我們僅針對 eth0 做監聽,所以整個 eth0 介面上面的資料都會被顯示到螢幕上, 不好分析啊!那麼我們可以簡化嗎?例如只取出 port 21 的連線封包,可以這樣做:

[root@www ~]$ tcpdump -i eth0 -nn port 21
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 96 bytes
01:54:37.96 IP 192.168.1.101.1240 > 192.168.1.100.21: . ack 1 win 65535
01:54:37.96 IP 192.168.1.100.21 > 192.168.1.101.1240: P 1:21(20) ack 1 win 5840
01:54:38.12 IP 192.168.1.101.1240 > 192.168.1.100.21: . ack 21 win 65515
01:54:42.79 IP 192.168.1.101.1240 > 192.168.1.100.21: P 1:17(16) ack 21 win 65515
01:54:42.79 IP 192.168.1.100.21 > 192.168.1.101.1240: . ack 17 win 5840
01:54:42.79 IP 192.168.1.100.21 > 192.168.1.101.1240: P 21:55(34) ack 17 win 5840

瞧!這樣就僅提出 port 21 的資訊而已,且仔細看的話,你會發現封包的傳遞都是雙向的, client 端發出『要求』而 server 端則予以『回應』,所以,當然是有去有回啊! 而我們也就可以經過這個封包的流向來瞭解到封包運作的過程。舉例來說:

  • 我們先在一個終端機視窗輸入『 tcpdump -i lo -nn 』 的監聽,
  • 再另開一個終端機視窗來對本機 (127.0.0.1) 登入『ssh localhost』

那麼輸出的結果會是如何?

[root@www ~]$ tcpdump -i lo -nn
 1 tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
 2 listening on lo, link-type EN10MB (Ethernet), capture size 96 bytes
 3 11:02:54.253777 IP 127.0.0.1.32936 > 127.0.0.1.22: S 933696132:933696132(0) 
   win 32767 <mss 16396,sackOK,timestamp 236681316 0,nop,wscale 2>
 4 11:02:54.253831 IP 127.0.0.1.22 > 127.0.0.1.32936: S 920046702:920046702(0) 
   ack 933696133 win 32767 <mss 16396,sackOK,timestamp 236681316 236681316,nop,
   wscale 2>
 5 11:02:54.253871 IP 127.0.0.1.32936 > 127.0.0.1.22: . ack 1 win 8192 <nop,
   nop,timestamp 236681316 236681316>
 6 11:02:54.272124 IP 127.0.0.1.22 > 127.0.0.1.32936: P 1:23(22) ack 1 win 8192 
   <nop,nop,timestamp 236681334 236681316>
 7 11:02:54.272375 IP 127.0.0.1.32936 > 127.0.0.1.22: . ack 23 win 8192 <nop,
   nop,timestamp 236681334 236681334>

上表顯示的頭兩行是 tcpdump 的基本說明,然後:

  • 第 3 行顯示的是『來自 client 端,帶有 SYN 主動連線的封包』,
  • 第 4 行顯示的是『來自 server 端,除了回應 client 端之外(ACK),還帶有 SYN 主動連線的標誌;
  • 第 5 行則顯示 client 端回應 server 確定連線建立 (ACK)
  • 第 6 行以後則開始進入資料傳輸的步驟。

從第 3-5 行的流程來看,熟不熟悉啊?沒錯!那就是三向交握的基礎流程啦!夠有趣吧! 不過 tcpdump 之所以被稱為駭客軟體之一可不止上頭介紹的功能吶! 上面介紹的功能可以用來作為我們主機的封包連線與傳輸的流程分析, 這將有助於我們瞭解到封包的運作,同時瞭解到主機的防火牆設定規則是否有需要修訂的地方。

更神奇的使用要來啦!如果我們使用 tcpdump 在 router 上面監聽『明碼』的傳輸資料時, 例如 FTP 傳輸協定,你覺得會發生什麼問題呢? 我們先在主機端下達『 tcpdump -i lo port 21 -nn -X 』然後再以 ftp 登入本機,並輸入帳號與密碼, 結果你就可以發現如下的狀況:

[root@www ~]$ tcpdump -i lo -nn -X 'port 21'
    0x0000:  4500 0048 2a28 4000 4006 1286 7f00 0001  E..H*(@.@.......
    0x0010:  7f00 0001 0015 80ab 8355 2149 835c d825  .........U!I.\.%
    0x0020:  8018 2000 fe3c 0000 0101 080a 0e2e 0b67  .....<.........g
    0x0030:  0e2e 0b61 3232 3020 2876 7346 5450 6420  ...a220.(vsFTPd.
    0x0040:  322e 302e 3129 0d0a                      2.0.1)..

    0x0000:  4510 0041 d34b 4000 4006 6959 7f00 0001  E..A.K@.@.iY....
    0x0010:  7f00 0001 80ab 0015 835c d825 8355 215d  .........\.%.U!]
    0x0020:  8018 2000 fe35 0000 0101 080a 0e2e 1b37  .....5.........7
    0x0030:  0e2e 0b67 5553 4552 2064 6d74 7361 690d  ...gUSER.dmtsai.
    0x0040:  0a                                       .

    0x0000:  4510 004a d34f 4000 4006 694c 7f00 0001  E..J.O@.@.iL....
    0x0010:  7f00 0001 80ab 0015 835c d832 8355 217f  .........\.2.U!.
    0x0020:  8018 2000 fe3e 0000 0101 080a 0e2e 3227  .....>........2'
    0x0030:  0e2e 1b38 5041 5353 206d 7970 6173 7377  ...8PASS.mypassw
    0x0040:  6f72 6469 7379 6f75 0d0a                 ordisyou..

上面的輸出結果已經被簡化過了,你必須要自行在你的輸出結果當中搜尋相關的字串才行。 從上面輸出結果的特殊字體中,我們可以發現『該 FTP 軟體使用的是 vsftpd ,並且使用者輸入 dmtsai 這個帳號名稱,且密碼是 mypasswordisyou』 嘿嘿!你說可不可怕啊!如果使用的是明碼的方式來傳輸你的網路資料? 所以我們才常常在講啊,網路是很不安全滴!

另外你得瞭解,為了讓網路介面可以讓 tcpdump 監聽,所以執行 tcpdump 時網路介面會啟動在 『錯亂模式 (promiscuous)』,所以你會在 /var/log/messages 裡面看到很多的警告訊息, 通知你說你的網路卡被設定成為錯亂模式!別擔心,那是正常的。至於更多的應用,請參考 man tcpdump 囉!

例題: 如何使用 tcpdump 監聽

  • 來自 eth0 介面卡且
  • 通訊協定為 port 22 ,
  • 封包來源為 192.168.1.101 的封包資料?

答:

tcpdump -i eth0 -nn 'port 22 and src host 192.168.1.101'