《LINUX實戰:網絡掃描工具nmap 使用》要點:
本文介紹了LINUX實戰:網絡掃描工具nmap 使用,希望對您有用。如果有疑問,可以聯系我們。
本文目次:
1.1 選項闡明
1.2 測驗考試一次掃描
1.3 掃描目標闡明
1.4 端口狀態闡明
1.5 光陰參數優化
1.6 掃描操作體系類型
1.7 快速掃描存活的主機
1.8 快速掃描端口
nmap一般就用來掃描主機是否在線(特別是掃描局域網內存活的機器)、開放了哪些端口.其他的功能用的比擬少,做滲透的人可能要了解的多些.
nmap必要自行安裝.
shell> yum -y install nmap
使用nmap -h可以查看選項和用法.選項非常多,這是功能強大的工具帶來的必然成果,但簡單使用并用不到幾個選項.
Usage: nmap [Scan Type(s)] [Options] {target specification}
TARGET SPECIFICATION:
? Can pass hostnames, IP addresses, networks, etc.
? Ex: scanme.nmap.org, microsoft.com/24, 192.168.0.1; 10.0.0-255.1-254
? -iL <inputfilename>: Input from list of hosts/networks
? -iR <num hosts>: Choose random targets
? --exclude <host1[,host2][,host3],...>: Exclude hosts/networks
? --excludefile <exclude_file>: Exclude list from file
HOST DISCOVERY:
? -sL: List Scan - simply list targets to scan
? -sn: Ping Scan - disable port scan
? -Pn: Treat all hosts as online -- skip host discovery
? -PS/PA/PU/PY[portlist]: TCP SYN/ACK, UDP or SCTP discovery to given ports
? -PE/PP/PM: ICMP echo, timestamp, and netmask request discovery probes
? -PO[protocol list]: IP Protocol Ping
? -PR: ARP ping - does not need HW address -> IP translation
? -n/-R: Never do DNS resolution/Always resolve [default: sometimes]
? --dns-servers <serv1[,serv2],...>: Specify custom DNS servers
? --system-dns: Use OS's DNS resolver
? --traceroute: Trace hop path to each host
SCAN TECHNIQUES:
? -sS/sT/sA/sW/sM: TCP SYN/Connect()/ACK/Window/Maimon scans
? -sU: UDP Scan
? -sN/sF/sX: TCP Null, FIN, and Xmas scans
? --scanflags <flags>: Customize TCP scan flags
? -sI <zombie host[:probeport]>: Idle scan
? -sY/sZ: SCTP INIT/COOKIE-ECHO scans
? -sO: IP protocol scan
? -b <FTP relay host>: FTP bounce scan
PORT SPECIFICATION AND SCAN ORDER:
? -p <port ranges>: Only scan specified ports
? ? Ex: -p22; -p1-65535; -p U:53,111,137,T:21-25,80,139,8080,S:9
? -F: Fast mode - Scan fewer ports than the default scan
? -r: Scan ports consecutively - don't randomize
? --top-ports <number>: Scan <number> most common ports
? --port-ratio <ratio>: Scan ports more common than <ratio>
SERVICE/VERSION DETECTION:
? -sV: Probe open ports to determine service/version info
? -sR: Check what service uses opened ports using RPC scan
? --version-intensity <level>: Set from 0 (light) to 9 (try all probes)
? --version-light: Limit to most likely probes (intensity 2)
? --version-all: Try every single probe (intensity 9)
? --version-trace: Show detailed version scan activity (for debugging)
SCRIPT SCAN:
? -sC: equivalent to --script=default
? --script=<Lua scripts>: <Lua scripts> is a comma separated list of directories, script-files or script-categories
? --script-args=<n1=v1,[n2=v2,...]>: provide arguments to scripts
? --script-trace: Show all data sent and received
? --script-updatedb: Update the script database.
OS DETECTION:
? -O: Enable OS detection
? --osscan-limit: Limit OS detection to promising targets
? --osscan-guess: Guess OS more aggressively
TIMING AND PERFORMANCE:
? Options which take <time> are in seconds, or append 'ms' (milliseconds),
? 's' (seconds), 'm' (minutes), or 'h' (hours) to the value (e.g. 30m).
? -T<0-5>: Set timing template (higher is faster)
? --min-hostgroup/max-hostgroup <size>: Parallel host scan group sizes
? --min-parallelism/max-parallelism <numprobes>: Probe parallelization
? --min-rtt-timeout/max-rtt-timeout/initial-rtt-timeout <time>: Specifies
? ? ? probe round trip time.
? --max-retries <tries>: Caps number of port scan probe retransmissions.
? --host-timeout <time>: Give up on target after this long
? --scan-delay/--max-scan-delay <time>: Adjust delay between probes
? --min-rate <number>: Send packets no slower than <number> per second
? --max-rate <number>: Send packets no faster than <number> per second
FIREWALL/IDS EVASION AND SPOOFING:
? -f; --mtu <val>: fragment packets (optionally w/given MTU)
? -D <decoy1,decoy2[,ME],...>: Cloak a scan with decoys
? -S <IP_Address>: Spoof source address
? -e <iface>: Use specified interface
? -g/--source-port <portnum>: Use given port number
? --data-length <num>: Append random data to sent packets
? --ip-options <options>: Send packets with specified ip options
? --ttl <val>: Set IP time-to-live field
? --spoof-mac <mac address/prefix/vendor name>: Spoof your MAC address
? --badsum: Send packets with a bogus TCP/UDP/SCTP checksum
OUTPUT:
? -oN/-oX/-oS/-oG <file>: Output scan in normal, XML, s|<rIpt kIddi3,
? ? and Grepable format, respectively, to the given filename.
? -oA <basename>: Output in the three major formats at once
? -v: Increase verbosity level (use -vv or more for greater effect)
? -d: Increase debugging level (use -dd or more for greater effect)
? --reason: Display the reason a port is in a particular state
? --open: Only show open (or possibly open) ports
? --packet-trace: Show all packets sent and received
? --iflist: Print host interfaces and routes (for debugging)
? --log-errors: Log errors/warnings to the normal-format output file
? --append-output: Append to rather than clobber specified output files
? --resume <filename>: Resume an aborted scan
? --stylesheet <path/URL>: XSL stylesheet to transform XML output to HTML
? --webxml: Reference stylesheet from Nmap.Org for more portable XML
? --no-stylesheet: Prevent associating of XSL stylesheet w/XML output
MISC:
? -6: Enable IPv6 scanning
? -A: Enable OS detection, version detection, script scanning, and traceroute
? --datadir <dirname>: Specify custom Nmap data file location
? --send-eth/--send-ip: Send using raw ethernet frames or IP packets
? --privileged: Assume that the user is fully privileged
? --unprivileged: Assume the user lacks raw socket privileges
? -V: Print version number
? -h: Print this help summary page.
EXAMPLES:
? nmap -v -A scanme.nmap.org
? nmap -v -sn 192.168.0.0/16 10.0.0.0/8
? nmap -v -iR 10000 -Pn -p 80
常用的就上面標紅的幾個.下面是解釋:
-iL <inputfilename>:從輸入文件中讀取主機或者IP列表作為探測目標 -sn: PING掃描,但是禁止端口掃描.默認總是會掃描端口.禁用端口掃描可以加速掃描主機 -n/-R: 永遠不要/總是進行DNS解析,默認情況下有時會解析 -PE/PP/PM:分別是基于echo/timestamp/netmask的ICMP探測報文方式.使用echo最快 -sS/sT/sA/sW:TCP SYN/Connect()/ACK/Window,其中sT掃描表示TCP掃描 -sU:UDP掃描 -sO:IP掃描 -p <port ranges>: 指定掃描端口 --min-hostgroup/max-hostgroup <size>: 對目標主機進行分組然后組之間并行掃描 --min-parallelism/max-parallelism <numprobes>: 設置并行掃描的探針數量 -oN/-oX/ <file>: 輸出掃描結果到普通文件或XML文件中.輸入到XML文件中的結果是格式化的結果 -v:顯示詳細信息,使用-vv或者更多的v顯示更詳細的信息
nmap掃描一般會比擬慢,特別是掃描非本機的時候.
[root@server2 ~]# nmap 127.0.0.1 Starting Nmap 6.40 ( http://nmap.org ) at 2017-06-20 13:03 CST Nmap scan report for localhost (127.0.0.1) Host is up (0.0000010s latency). Not shown: 998 closed ports PORT STATE SERVICE 22/tcp open ssh 25/tcp open smtp
只掃描出了兩個端口,但是不代表真的只開了兩個端口,這樣不加任何參數的nmap將自動決定掃描1000個高危端口,但哪些是高危端口由nmap決定.從結果中也能看出來,"NOT shown:998 closed ports"表現998個關閉的端口未顯示出來,隨后又顯示了2個open端口,正好1000個.雖說默認只掃描1000個,但常見的端口都能掃描出來.
從虛擬機掃描win主機看看.可以感受到,掃描速度明顯低落了.
[root@server2 ~]# nmap 192.168.0.122 Starting Nmap 6.40 ( http://nmap.org ) at 2017-06-20 13:11 CST Nmap scan report for 192.168.0.122 Host is up (1.2s latency). Not shown: 990 closed ports PORT STATE SERVICE 21/tcp open ftp 135/tcp open msrpc 139/tcp open netbios-ssn 443/tcp open https 445/tcp open microsoft-ds 514/tcp filtered shell 902/tcp open iss-realsecure 912/tcp open apex-mesh 1583/tcp open simbaexpress 5357/tcp open wsdapi Nmap done: 1 IP address (1 host up) scanned in 8.38 seconds
可以指定"-p [1-65535]"來掃描所有端口,或者使用"-p-"選項也是周全掃描.
[root@xuexi ~]# nmap -p- 127.0.0.1
nmap默認總是會掃描端口,可以使用-sn選項禁止掃描端口,以加快掃描主機是否存活.
Nmap支持CIDR風格的地址,Nmap將會掃描所有和該參考IP地址具有相同cidr位數的所有IP地址或主機.
例如192.168.10.0/24將掃描192.168.10.0和192.168.10.255之間的256臺主機,192.168.10.40/24會做同樣的事情.假設主機scanme.nmap.org的IP地址是205.217.153.62,scanme.nmap.org/16將掃描205.217.0.0和205.217.255.255之間的65536個IP地址.掩碼位所允許的最小值是/1,這將會掃描半個互聯網,最年夜值是/32,這將會掃描該主機或IP地址,因為所有主機位都固定了.
CIDR標記位很簡潔但有時候不夠靈活.例如也許想要掃描192.168.0.0/16,但略過任何以".0"或者".255"結束的IP地址,因為它們通常是網段地址或廣播地址.可以用逗號分開的數字或范圍列表為IP地址指定它的范圍.例如"192.168.0-255.1-254"將略過該范圍內以".0"和".255"結束的地址.范圍不必限于最后的8位:"0-255.0-255.13.37"將在整個互聯網范圍內掃描所有以"13.37"結束的地址.
Nmap命令行接受多個主機闡明,它們不必是相同類型.如:
nmap www.hostname.com 192.168.0.0/8 10.0.0,1,3-7.0-255
固然目標通常在命令行指定,下列選項也可用來控制目標的選擇:
從<inputfilename>中讀取目標說明.在命令行輸入一堆主機名顯得很愚笨,然而經常需要這樣.例如DHCP服務器可能導出10000個當前租約列表.列表中的項可以是Nmap在命令行上接受的任何格式(IP地址,主機名,CIDR,IPv6,或者八位字節范圍).每一項必須以一個或多個空格、制表符或換行符分開.如果希望Nmap從標準輸入讀取列表,則使用"-"作為表示/dev/stdin.
指定一個IP地址然后加一個CIDR的掩碼位,如192.168.100.22/24,當然寫成192.168.100.0/24也是一樣的,因為nmap必要的是參考IP.如果掃描的是范圍地址,可以192.168.100.1-254這樣的書寫方式.
[root@xuexi ~]# nmap 192.168.100.1/24 Starting Nmap 6.40 ( http://nmap.org ) at 2017-06-20 13:22 CST Nmap scan report for 192.168.100.1 Host is up (0.00053s latency). Not shown: 992 filtered ports PORT STATE SERVICE 21/tcp open ftp 135/tcp open msrpc 139/tcp open netbios-ssn 443/tcp open https 445/tcp open microsoft-ds 902/tcp open iss-realsecure 912/tcp open apex-mesh 5357/tcp open wsdapi MAC Address: 00:50:56:C0:00:08 (VMware) Nmap scan report for 192.168.100.2 Host is up (0.000018s latency). Not shown: 999 closed ports PORT STATE SERVICE 53/tcp open domain MAC Address: 00:50:56:E2:16:04 (VMware) Nmap scan report for 192.168.100.70 Host is up (0.00014s latency). Not shown: 999 closed ports PORT STATE SERVICE 22/tcp open ssh MAC Address: 00:0C:29:71:81:64 (VMware) Nmap scan report for 192.168.100.254 Host is up (0.000095s latency). All 1000 scanned ports on 192.168.100.254 are filtered MAC Address: 00:50:56:ED:A1:04 (VMware) Nmap scan report for 192.168.100.62 Host is up (0.0000030s latency). Not shown: 999 closed ports PORT STATE SERVICE 22/tcp open ssh Nmap done: 256 IP addresses (5 hosts up) scanned in 7.96 seconds
一般來說,端口全部封閉的很可能不是計算機,而可能是路由器、虛擬網卡等設備.
Nmap功能越來越多,但它賴以成名的是它的核心功能——端口掃描.
Nmap把端口分成六個狀態:open(開放的),closed(封閉的),filtered(被過濾的),unfiltered(未???過濾的),open|filtered(開放或者被過濾的),或者closed|filtered(封閉或者被過濾的).
這些狀態并非端口自己的性質,而是描述Nmap怎樣看待它們.例如,對于同樣的目標機器的135/tcp端口,從同網絡掃描顯示它是開放的,而跨網絡做完全相同的掃描則可能顯示它是filtered(被過濾的).
改善掃描時間的技術有:忽略非關鍵的檢測、升級最新版本的Nmap(文檔中說nmap版本越高性能越好)等.此外,優化時間參數也會帶來實質性的優化,這些參數如下:
TIMING AND PERFORMANCE:
??-T<0-5>: Set timing template (higher is faster)
? --min-hostgroup/max-hostgroup <size>: Parallel host scan group sizes
? --min-parallelism/max-parallelism <numprobes>: Probe parallelization
? --min-rtt-timeout/max-rtt-timeout/initial-rtt-timeout <time>: Specifies probe round trip time.
? --max-retries <tries>: Caps number of port scan probe retransmissions.
? --host-timeout <time>: Give up on target after this long
? --scan-delay/--max-scan-delay <time>: Adjust delay between probes
? --min-rate <number>: Send packets no slower than <number> per second
? --max-rate <number>: Send packets no faster than <number> per second
此中最主要的是前3種:
1.-T<0-5>:這表現直接使用namp提供的掃描模板,不同的模板適用于不同的環境下,默認的模板為"-T 3",具體的看man文檔,其實用的很少.
2.--min-hostgroup <milliseconds>; --max-hostgroup <milliseconds> (調整并行掃描組的年夜小)
Nmap具有并行掃描多主機端口的能力,實現辦法是將所有給定的目標IP按空間分成組,然后一次掃描一個組.通常組分的越大效率越高,但分組的缺點是只有當整個組掃描結束后才會返回該組中主機掃描結果.例如,組的大小定義為50,則只有前50個主機掃描結束后才能得到這50個IP內的結果.
默認方式下,Nmap采取折衷的辦法.開始掃描時的組較小,默認值為5,這樣便于盡快產生結果,隨后增長組的大小,默認最大為1024.但最小和最大確切的值則依賴于所給定的選項.
--max-hostgroup選項用于說明使用最大的組,Nmap不會超出這個大小.--min-hostgroup選項說明最小的組,Nmap會堅持組大于這個值.如果在指定的接口上沒有足夠的目標主機來滿足所指定的最小值,Nmap可能會采用比所指定的值小的組.
這些選項的主要用途是說明一個最小組的大小,使得整個掃描更加快速.通常選擇256來掃描C類網段,對于端口數較多的掃描,超出該值沒有意義,因為它只是分組了,但是cpu資源是有限的.對于端口數較少的掃描,2048或更大的組大小是有贊助的.
3.--min-parallelism <milliseconds>; --max-parallelism <milliseconds> (調整探測報文的并行度,即探針數)
這些選項用于控制主機組的探測報文數量,可用于端口掃描和主機發現.默認狀態下,Nmap基于網絡性能計算一個抱負的并行度,這個值經常改變.如果報文被丟棄,Nmap降低速度,探測報文數量減少.隨著網絡性能的改善,抱負的探測報文數量會緩慢增加.默認狀態下,當網絡不可靠時,抱負的并行度值可能為1,在好的條件下,可能會增長至幾百.
最常見的應用是--min-parallelism值大于1,以加快性能欠安的主機或網絡的掃描.這個選項具有風險,如果過高則影響準確度,同時也會降低Nmap基于網絡條件動態控制并行度的能力.
一般說來,這個值要設置的和--min-hostgroup的值相等或年夜于它性能才會提升.
掃描操作系統.操作系統的掃描有可能會呈現誤報.
要快速掃描存活的主機,必要使用的幾個重要選項是:
-n:永遠不要DNS解析.這個不管是給定地址掃描還是給定網址掃描,加上它速度都邑極速提升
-sn:制止端口掃描
-PE:只根據echo回顯判斷主機在線,這種類型的選項使用越多,速度越慢,如-PM -PP選項都是類似的,但他們速度要慢的多的多,PE有個缺點,不克不及穿透防火墻
--min-hostgroup N:當IP太多時,nmap必要分組,然后并掃描,使用該選項可以指定多少個IP一組
--min-parallelism N:這個參數非常關鍵,為了充足利用系統和網絡資源,設置好合理的探針數.一般來說,設置的越大速度越快,且和min-hostgroup的值相等或大于它性能才會提升
示例一:掃描192.168.100.0/24網段存活的機械
255個局域網地址只用了半秒鐘.可謂是極速.
再測試掃描下以www.baidu.com作為參考地址的地址空間.
可以看到,65535個地址只需19秒就掃描完成了.速度是相當的快.
既然是掃描端口,就不克不及使用-sn選項,也不克不及使用-PE,否則不會返回端口狀態,只會返回哪些主機.
姊妹篇:抓包工具tcpdump用法闡明?
本文永遠更新鏈接地址:
學習更多LINUX教程,請查看站內專欄,如果有LINUX疑問,可以加QQ交流《LINUX實戰:網絡掃描工具nmap 使用》。