nmap通常就用來掃描主機是否在線(特別是掃描局域網內存活的機器)、開放了哪些端口。其餘的功能用的比較少,作滲透的人可能要了解的多些。html
nmap須要自行安裝。ios
shell> yum -y install nmapweb
使用nmap -h能夠查看選項和用法。選項很是多,這是功能強大的工具帶來的必然結果,但簡單使用並用不到幾個選項。shell
Usage: nmap [Scan Type(s)] [Options] {target specification}express
TARGET SPECIFICATION:windows
Can pass hostnames, IP addresses, networks, etc.api
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 targetsapp
--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的值相等或大於它性能纔會提高。
掃描操做系統。操做系統的掃描有可能會出現誤報。
C:\Windows\system32>nmap -O 127.0.0.1 Starting Nmap 7.40 ( https://nmap.org ) at 2017-03-09 13:18 CST Nmap scan report for lmlicenses.wip4.adobe.com (127.0.0.1) Host is up (0.000046s latency). Not shown: 990 closed ports PORT STATE SERVICE 21/tcp open ftp 135/tcp open msrpc 443/tcp open https 445/tcp open microsoft-ds 902/tcp open iss-realsecure 912/tcp open apex-mesh 5357/tcp open wsdapi 5678/tcp open rrac 10000/tcp open snet-sensor-mgmt 65000/tcp open unknown Device type: general purpose Running: Microsoft Windows 10 OS CPE: cpe:/o:microsoft:windows_10 OS details: Microsoft Windows 10 1511 Network Distance: 0 hops OS detection performed. Please report any incorrect results at https://nmap.org/submit/ . Nmap done: 1 IP address (1 host up) scanned in 2.33 seconds
要快速掃描存活的主機,須要使用的幾個重要選項是:
-n:永遠不要DNS解析。這個不論是給定地址掃描仍是給定網址掃描,加上它速度都會極速提高
-sn:禁止端口掃描
-PE:只根據echo回顯判斷主機在線,這種類型的選項使用越多,速度越慢,如-PM -PP選項都是相似的,但他們速度要慢的多的多,PE有個缺點,不能穿透防火牆
--min-hostgroup N:當IP太多時,nmap須要分組,而後並掃描,使用該選項能夠指定多少個IP一組
--min-parallelism N:這個參數很是關鍵,爲了充分利用系統和網絡資源,設置好合理的探針數。通常來講,設置的越大速度越快,且和min-hostgroup的值相等或大於它性能纔會提高
示例一:掃描192.168.100.0/24網段存活的機器
[root@server2 ~]# nmap -sn -n -PE --min-hostgroup 1024 --min-parallelism 1024 192.168.100.1/24 Warning: You specified a highly aggressive --min-hostgroup. Warning: Your --min-parallelism option is pretty high! This can hurt reliability. Starting Nmap 6.40 ( http://nmap.org ) at 2017-06-20 14:30 CST Nmap scan report for 192.168.100.1 Host is up (0.00036s latency). MAC Address: 00:50:56:C0:00:08 (VMware) Nmap scan report for 192.168.100.2 Host is up (0.000051s latency). MAC Address: 00:50:56:E2:16:04 (VMware) Nmap scan report for 192.168.100.70 Host is up (0.000060s latency). MAC Address: 00:0C:29:71:81:64 (VMware) Nmap scan report for 192.168.100.254 Host is up (0.000069s latency). MAC Address: 00:50:56:ED:A1:04 (VMware) Nmap scan report for 192.168.100.62 Host is up. Nmap done: 256 IP addresses (5 hosts up) scanned in 0.26 seconds
255個局域網地址只用了半秒鐘。可謂是極速。
再測試掃描下以www.baidu.com做爲參考地址的地址空間。
[root@server2 ~]# nmap -sn -PE -n --min-hostgroup 1024 --min-parallelism 1024 -oX nmap_output.xml www.baidu.com/16 …….省略部分結果 Nmap scan report for 163.177.81.145 Host is up (0.072s latency). Nmap done: 65536 IP addresses (144 hosts up) scanned in 19.15 seconds
能夠看到,65535個地址只需19秒就掃描完成了。速度是至關的快。
既然是掃描端口,就不能使用-sn選項,也不能使用-PE,不然不會返回端口狀態,只會返回哪些主機。
[root@server2 ~]# nmap -n -p 20-2000 --min-hostgroup 1024 --min-parallelism 1024 192.168.100.70/24 Warning: You specified a highly aggressive --min-hostgroup. Warning: Your --min-parallelism option is pretty high! This can hurt reliability. Starting Nmap 6.40 ( http://nmap.org ) at 2017-06-20 14:52 CST Nmap scan report for 192.168.100.1 Host is up (0.00084s latency). Not shown: 1980 filtered ports PORT STATE SERVICE 21/tcp open ftp 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: 1980 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.000041s latency). Not shown: 1980 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.000035s latency). All 1981 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.0000020s latency). Not shown: 1980 closed ports PORT STATE SERVICE 22/tcp open ssh Nmap done: 256 IP addresses (5 hosts up) scanned in 2.38 seconds
姊妹篇:抓包工具tcpdump用法說明