网络端口检测实战

背景

一般在进行网络访问不通的时候需要进行端口测试,在windows环境下,可以使用telnet命令进行检测:

1
telnet www.baidu.com 443

telnet检测仅适用于tcp协议的端口检测,对于udp协议的则毫无办法。

在Liunx和macOS环境下,默认是没有安装的telnet的,但是有nc这工具。试用了一下发现检测效果更佳。

NC网络检测实战

  • tcp检测

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    zhengkaideMacBook-Pro:~ zhengk$ nc -z -v 220.181.38.148 80
    found 0 associations
    found 1 connections:
    1: flags=82<CONNECTED,PREFERRED>
    outif en0
    src 192.168.31.68 port 64654
    dst 220.181.38.148 port 80
    rank info not available
    TCP aux info available

    Connection to 220.181.38.148 port 80 [tcp/http] succeeded!
  • udp检测

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    zhengkaideMacBook-Pro:~ zhengk$ nc -u -z -v 220.181.38.148 80
    found 0 associations
    found 1 connections:
    1: flags=82<CONNECTED,PREFERRED>
    outif (null)
    src 192.168.31.68 port 60536
    dst 220.181.38.148 port 80
    rank info not available

    Connection to 220.181.38.148 port 80 [udp/http] succeeded!
  • 范围端口检测

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    zhengkaideMacBook-Pro:~ zhengk$ nc -z -v -G 2 220.181.38.148 75-80
    nc: connectx to 220.181.38.148 port 75 (tcp) failed: Operation timed out
    nc: connectx to 220.181.38.148 port 76 (tcp) failed: Operation timed out
    nc: connectx to 220.181.38.148 port 77 (tcp) failed: Operation timed out
    nc: connectx to 220.181.38.148 port 78 (tcp) failed: Operation timed out
    nc: connectx to 220.181.38.148 port 79 (tcp) failed: Operation timed out
    found 0 associations
    found 1 connections:
    1: flags=82<CONNECTED,PREFERRED>
    outif en0
    src 192.168.31.68 port 64689
    dst 220.181.38.148 port 80
    rank info not available
    TCP aux info available

    Connection to 220.181.38.148 port 80 [tcp/http] succeeded!

扩展

在macOS的环境下,nc命令的使用参数与linux环境下的参数代表的并不一样,这需要特别注意,常用的几个参数如下:

  • -G:用来指定判断超时的时间,比如-G 2就指2秒就算超时。注意:在linux中,使用-w参数来指定超时
  • -v:用于输出交互过程。
  • -z:在端口扫描的情况下使用,否则不会自动退出结束。

更多的参数配置可以使用-h来获取帮助:

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
zhengkaideMacBook-Pro:~ zhengk$ nc -h
usage: nc [-46AacCDdEFhklMnOortUuvz] [-K tc] [-b boundif] [-i interval] [-p source_port] [--apple-delegate-pid pid] [--apple-delegate-uuid uuid]
[-s source_ip_address] [-w timeout] [-X proxy_version]
[-x proxy_address[:port]] [hostname] [port[s]]
Command Summary:
-4 Use IPv4
-6 Use IPv6
-A Set SO_RECV_ANYIF on socket
-a Set SO_AWDL_UNRESTRICTED on socket
-b ifbound Bind socket to interface
-c Send CRLF as line-ending
-C Don't use cellular connection
-D Enable the debug socket option
-d Detach from stdin
-E Don't use expensive interfaces
-F Do not use flow advisory (flow adv enabled by default)
-G conntimo Connection timeout in seconds
-h This help text
-H keepidle Initial idle timeout in seconds
-I keepintvl Interval for repeating idle timeouts in seconds
-i secs Delay interval for lines sent, ports scanned
-J keepcnt Number of times to repeat idle timeout
-k Keep inbound sockets open for multiple connects
-K tclass Specify traffic class
-l Listen mode, for inbound connects
-L num_probes Number of probes to send before generating a read timeout event
-m Set SO_INTCOPROC_ALLOW on socket
-n Suppress name/port resolutions
-M Use MULTIPATH domain socket
-N num_probes Number of probes to send before generating a write timeout event
-O Use old-style connect instead of connectx
-p port Specify local port for remote connects (cannot use with -l)
-r Randomize remote ports
-s addr Local source address
-t Answer TELNET negotiation
-U Use UNIX domain socket
-u UDP mode
-v Verbose
-w secs Timeout for connects and final net reads
-X proto Proxy protocol: "4", "5" (SOCKS) or "connect"
-x addr[:port] Specify proxy address and port
-z Zero-I/O mode [used for scanning]
-o Issue socket options after connect/bind
--apple-delegate-pid pid Set socket as delegate using pid
Port numbers can be individual or ranges: lo-hi [inclusive]