TCP/IP 瑞士军刀 - netcat (篇一)

2020-12-10  本文已影响0人  Chaos_John

欢迎转载,但请在开头或结尾注明原文出处【blog.chaosjohn.com】

前言

本来打算写 端口转发(篇三) - netcat

但是中途发现 netcat 水很深,原因听我细细说来

netcat 被成为 TCP/IP 的瑞士军刀,得益于它虽体积小(几十KB)却在网络下无比强大,在各种场景下都有它发挥的余地,以至于在各大 Linux 发行版 中都默认安装。

原版的 netcat 第一版在 1995年10月28日 被发布,且最后一个版本 1.10 发布于 1996年3月,被称之为 tranditional netcat

随后,为了兼容 POSIX,推出了不同实现的版本,最有名的为 OpenBSD netcatGNU netcat

很有意思的是:

水很深 的原因就在于,如果只用 netcat 进行端口转发,必须用到 GNU 版本的 -e 参数,所以就临时更改一下本文题材,讲解一下 netcat 的使用以及案例,且主要针对 OpenBSD netcat

使用

$ nc -h
OpenBSD netcat (Debian patchlevel 1)
usage: nc [-46CDdFhklNnrStUuvZz] [-I length] [-i interval] [-M ttl]
      [-m minttl] [-O length] [-P proxy_username] [-p source_port]
      [-q seconds] [-s sourceaddr] [-T keyword] [-V rtable] [-W recvlimit]
      [-w timeout] [-X proxy_protocol] [-x proxy_address[:port]]
      [destination] [port]
    Command Summary:
        -4      Use IPv4
        -6      Use IPv6
        -b      Allow broadcast
        -C      Send CRLF as line-ending
        -D      Enable the debug socket option
        -d      Detach from stdin
        -F      Pass socket fd
        -h      This help text
        -I length   TCP receive buffer length
        -i interval Delay interval for lines sent, ports scanned
        -k      Keep inbound sockets open for multiple connects
        -l      Listen mode, for inbound connects
        -M ttl      Outgoing TTL / Hop Limit
        -m minttl   Minimum incoming TTL / Hop Limit
        -N      Shutdown the network socket after EOF on stdin
        -n      Suppress name/port resolutions
        -O length   TCP send buffer length
        -P proxyuser    Username for proxy authentication
        -p port     Specify local port for remote connects
        -q secs     quit after EOF on stdin and delay of secs
        -r      Randomize remote ports
        -S      Enable the TCP MD5 signature option
        -s sourceaddr   Local source address
        -T keyword  TOS value
        -t      Answer TELNET negotiation
        -U      Use UNIX domain socket
        -u      UDP mode
        -V rtable   Specify alternate routing table
        -v      Verbose
        -W recvlimit    Terminate after receiving a number of packets
        -w timeout  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      DCCP mode
        -z      Zero-I/O mode [used for scanning]
    Port numbers can be individual or ranges: lo-hi [inclusive]

我们挑几个常用的参数解释一下:

案例

大部分的案例,笔者都录制了终端的 GIF 动图:

一次性聊天室

一次性聊天室

伪常驻 聊天室

伪常驻聊天室

UDP 聊天室

UDP 聊天室

发起 HTTP 请求(交互式)

发起 HTTP 请求(交互式)

发起 HTTP 请求(命令式)

发起 HTTP 请求(命令式)

文件传输(简单)

文件传输(简单)

文件传输(使用 tar 归档传输多文件)

文件传输(使用 tar 归档传输多文件)

socket通信

socket通信

网络测速(配合 dd 使用)

网络测速(配合 dd 使用)

服务端口诊断

案例背景:公司的 Elastic Search 服务器集群部署在阿里云,其 9200/9300 端口的防火墙直接采用阿里云的安全组配置,内网IP和公司所在的公网IP才可以访问。

本地开发环境如果发现 Elastic Search 连接报错,我们会采用 telnet es.example.com 9200 来作第一步诊断:

Trying 120.27.216.xxx...
Connected to es.example.com.
Escape character is '^]'.
Trying 120.27.216.xxx...
telnet: Unable to connect to remote host: Connection refused

其实可以不用 telnet,而且 telnetmacOS 环境中并没有预装,我们可以改用 nc -vz es.example.com 9200 来替代

Connection to es0.example.com (120.27.216.xxx) 9200 port [tcp/wap-wsp] succeeded!
nc: connect to es0.example.com (120.27.216.xxx) port 9200 (tcp) failed: Connection refused

写在最后

本文演示了一些 OpenBSD netcat 的案例,其实用管道配合其他工具,还能出很多很多的案例,但是吸收了上面的几个案例之后,通过举一反三,相信读者也能深谙 netcat 的强大。

后面笔者会以 GNU netcat 为重点写个 篇二,因其存在 -e 参数,所以其部分内容单独拎出来也可以作为 端口转发(篇三) - GNU netcat

上一篇 下一篇

猜你喜欢

热点阅读