天天學一個 Linux 命令(91):nc

命令簡介

nc 命令是一款功能強大的網絡工具。它是一個端口掃描工具,也是一款安全工具,還能是一款監測工具,甚至能夠作爲一個簡單的 TCP 代理。centos

語法格式

nc [-hlnruz][-g<網關...>][-G<指向器數目>][-i<延遲秒數>][-o<輸出文件>][-p<通訊端口>]
[-s<來源位址>][-v...][-w<超時秒數>][主機名稱][通訊端口...]

選項說明

-g<網關>      #設置路由器躍程通訊網關,最大可設置8個。
-G<指向器數目> #設置來源路由指向器,其數值爲4的倍數
-i<延遲秒數>  #設置時間間隔,以便傳送信息及掃描通訊端口
-o<輸出文件>  #指定輸出保存到文件中
-p<通訊端口>  #設置本地主機使用的通訊端口
-s<來源位址>  #設置本地主機送出數據包的IP地址
-w<超時秒數>  #設置等待連線的時間
-l  #使用監聽模式
-n  #使用IP地址
-r  #亂數指定本地與遠端主機的通訊端口
-u  #使用UDP傳輸協議
-v  #顯示指令執行過程當中的詳細信息
-z  #使用0輸入/輸出模式
-h  #打印幫助信息

應用舉例

掃描端口安全

[root@centos7 ~]# nc -c -z -w4 192.168.1.199 1-65535
Ncat: Connection refused.

這裏有一個坑,在CentOS7.X 中使用yum install -y nc安裝的nc實際安裝的是nmap-ncat(ncat命令),但ncat這個命令沒有端口掃描功能,但爲什麼在系統中又能夠使用nc命令呢,以下:服務器

[root@centos7 ~]# which nc
/usr/bin/nc
[root@centos7 ~]# ls -l /usr/bin/nc
lrwxrwxrwx 1 root root 22 Aug 20  2020 /usr/bin/nc -> /etc/alternatives/nmap
[root@centos7 ~]# ll /etc/alternatives/nmap 
lrwxrwxrwx 1 root root 13 Aug 20  2020 /etc/alternatives/nmap -> /usr/bin/ncat
[root@centos7 ~]# ll /usr/bin/ncat 
-rwxr-xr-x 1 root root 380184 Aug  8  2019 /usr/bin/ncat

因此,當你在系統中執行nc命令時,實際執行的是ncat命令,解決文案以下:下載 nc軟件包(https://sourceforge.net/proje...),上傳解壓並編譯安裝(你會發現網上好多的教程全是沒有驗證這個坑的)網絡

[root@centos7 ~]# tar zxf netcat-0.7.1.tar.gz
[root@centos7 ~]# cd netcat-0.7.1
[root@centos7 ~]# ./configure
[root@centos7 ~]# make && make install
[root@centos7 ~]# echo $?
[root@centos7 ~]# 0
[root@centos7 ~]# which netcat
/usr/local/bin/netcat
[root@centos7 ~]# ln -s /usr/local/bin/netcat /usr/bin/nc
[root@centos7 ~]# nc --help
GNU netcat 0.7.1, a rewrite of the famous networking tool.
Basic usages:
connect to somewhere:  nc [options] hostname port [port] ...
listen for inbound:    nc -l -p port [options] [hostname] [port] ...
tunnel to somewhere:   nc -L hostname:port -p port [options]
Mandatory arguments to long options are mandatory for short options too.
Options:
  -c, --close                close connection on EOF from stdin
  -e, --exec=PROGRAM         program to exec after connect
  -g, --gateway=LIST         source-routing hop point[s], up to 8
  -G, --pointer=NUM          source-routing pointer: 4, 8, 12, ...
  -h, --help                 display this help and exit
  -i, --interval=SECS        delay interval for lines sent, ports scanned
  -l, --listen               listen mode, for inbound connects
  -L, --tunnel=ADDRESS:PORT  forward local port to remote address
  -n, --dont-resolve         numeric-only IP addresses, no DNS
  -o, --output=FILE          output hexdump traffic to FILE (implies -x)
  -p, --local-port=NUM       local port number
  -r, --randomize            randomize local and remote ports
  -s, --source=ADDRESS       local source address (ip or hostname)
  -t, --tcp                  TCP mode (default)
  -T, --telnet               answer using TELNET negotiation
  -u, --udp                  UDP mode
  -v, --verbose              verbose (use twice to be more verbose)
  -V, --version              output version information and exit
  -x, --hexdump              hexdump incoming and outgoing traffic
  -w, --wait=SECS            timeout for connects and final net reads
  -z, --zero                 zero-I/O mode (used for scanning)
Remote port number can also be specified as range.  Example: '1-1024'

掃描端口(指定範圍)dom

[root@centos7 ~]# nc -v -z -w2 192.168.1.100 1-65535
192.168.1.100 22 (ssh) open
192.168.1.100 19999 (dnp-sec) open

指定端口掃描ssh

[root@centos7 ~]# nc -nvv 192.168.1.100 22
192.168.1.100 22 (ssh) open
SSH-2.0-OpenSSH_7.4
Protocol mismatch.
Total received bytes: 40
Total sent bytes: 1

查看從服務器到目標地址的出站端口是否被防火牆阻斷tcp

[root@centos7 ~]# nc -vz www.baidu.com 443 -w2
www.baidu.com [36.152.44.96] 443 (https) open
[root@centos7 ~]# 
[root@centos7 ~]# nc -vz www.baidu.com 80 -w2
www.baidu.com [36.152.44.96] 80 (http) open

image

相關文章
相關標籤/搜索