簡介
nmap是一個網絡連接端掃描軟件,用來掃描網上電腦開放的網絡連接端。確定哪些服務運行在哪些連接端,並且推斷計算機運行哪個操作系統(這是亦稱
fingerprinting)。它是網絡管理員和hacker必用的軟件之一。
常用參數說明
-iL filename 從文件中讀取待檢測的目標,文件中的表示方法支持機名,ip,網段
-iR hostnum 隨機選取,進行掃描.如果-iR指定爲0,則是無休止的掃描
--exclude host1[, host2] 從掃描任務中需要排除的主機
--exculdefile exclude_file 排除文件中的IP,格式和-iL指定掃描文件的格式相同
主機發現
-sL 僅僅是顯示,掃描的IP數目,不會進行任何掃描
-sn ping掃描,即主機發現
-Pn 不檢測主機存活
-PS/PA/PU/PY[portlist] TCP SYN Ping/TCP ACK Ping/UDP Ping發現
-PE/PP/PM 使用ICMP echo, timestamp and netmask 請求包發現主機
-PO[prococol list] 使用IP協議包探測對方主機是否開啓
-n/-R 不對IP進行域名反向解析/爲所有的IP都進行域名的反響解析
掃描技巧
-sS/sT/sA/sW/sM TCP SYN/TCP connect()/ACK/TCP窗口掃描/TCP Maimon掃描
-sU UDP掃描
-sN/sF/sX TCP Null,FIN,and Xmas掃描
--scanflags 自定義TCP包中的flags
-sI zombie host[:probeport] Idlescan
-sY/sZ SCTP INIT/COOKIE-ECHO 掃描
-sO 使用IP protocol 掃描確定目標機支持的協議類型
-b “FTP relay host” 使用FTP bounce scan
指定端口和掃描順序
-p 特定的端口 -p80,443 或者 -p1-65535
-p U:PORT 掃描udp的某個端口, -p U:53
-F 快速掃描模式,比默認的掃描端口還少
-r 不隨機掃描端口,默認是隨機掃描的
--top-ports "number" 掃描開放概率最高的number個端口,出現的概率需要參考nmap-services文件,ubuntu中該文件位於/usr/share/nmap.nmap默認掃前1000個
--port-ratio "ratio" 掃描指定頻率以上的端口
服務版本識別
-sV 開放版本探測,可以直接使用-A同時打開操作系統探測和版本探測
--version-intensity "level" 設置版本掃描強度,強度水平說明了應該使用哪些探測報文。數值越高,服務越有可能被正確識別。默認是7
--version-light 打開輕量級模式,爲--version-intensity 2的別名
--version-all 嘗試所有探測,爲--version-intensity 9的別名
--version-trace 顯示出詳細的版本偵測過程信息
腳本掃描
-sC 根據端口識別的服務,調用默認腳本
--script=”Lua scripts” 調用的腳本名
--script-args=n1=v1,[n2=v2] 調用的腳本傳遞的參數
--script-args-file=filename 使用文本傳遞參數
--script-trace 顯示所有發送和接收到的數據
--script-updatedb 更新腳本的數據庫
--script-help=”Lua script” 顯示指定腳本的幫助
OS識別
-O 啓用操作系統檢測,-A來同時啓用操作系統檢測和版本檢測
--osscan-limit 針對指定的目標進行操作系統檢測(至少需確知該主機分別有一個open和closed的端口)
--osscan-guess 推測操作系統檢測結果,當Nmap無法確定所檢測的操作系統時,會儘可能地提供最相近的匹配,Nmap默認進行這種匹配
防火牆/IDS躲避和哄騙
-f; --mtu value 指定使用分片、指定數據包的MTU.
-D decoy1,decoy2,ME 使用誘餌隱蔽掃描
-S IP-ADDRESS 源地址欺騙
-e interface 使用指定的接口
-g/ --source-port PROTNUM 使用指定源端口
--proxies url1,[url2],... 使用HTTP或者SOCKS4的代理
--data-length NUM 填充隨機數據讓數據包長度達到NUM
--ip-options OPTIONS 使用指定的IP選項來發送數據包
--ttl VALUE 設置IP time-to-live域
--spoof-mac ADDR/PREFIX/VEBDOR MAC地址僞裝
--badsum 使用錯誤的checksum來發送數據包
Nmap 輸出
-oN 將標準輸出直接寫入指定的文件
-oX 輸出xml文件
-oS 將所有的輸出都改爲大寫
-oG 輸出便於通過bash或者perl處理的格式,非xml
-oA BASENAME 可將掃描結果以標準格式、XML格式和Grep格式一次性輸出
-v 提高輸出信息的詳細度
-d level 設置debug級別,最高是9
--reason 顯示端口處於帶確認狀態的原因
--open 只輸出端口狀態爲open的端口
--packet-trace 顯示所有發送或者接收到的數據包
--iflist 顯示路由信息和接口,便於調試
--log-errors 把日誌等級爲errors/warings的日誌輸出
--append-output 追加到指定的文件
--resume FILENAME 恢復已停止的掃描
--stylesheet PATH/URL 設置XSL樣式表,轉換XML輸出
--webxml 從namp.org得到XML的樣式
--no-sytlesheet 忽略XML聲明的XSL樣式表
其他nmap選項
-6 開啓IPv6
-A OS識別,版本探測,腳本掃描和traceroute
--datedir DIRNAME 說明用戶Nmap數據文件位置
--send-eth / --send-ip 使用原以太網幀發送/在原IP層發送
--privileged 假定用戶具有全部權限
--unprovoleged 假定用戶不具有全部權限,創建原始套接字需要root權限
-V 打印版本信息
-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
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 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
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
用例
掃描網段 (192.168.1.0/24)
nmap -sP 192.168.1.0/24 或者 nmap -sP 192.168.1.*
SYN對全端口進行掃描
在aggressive(4)的時間模板下,同時對開放的端口進行端口識別,並查看相應的服務器版本。
nmap -sS -T4 -p1-65535 -sV 192.168.1.169
在aggressive(4)的時間模板下,探測操作系統的類型和版本,並顯示traceroute的結果。
nmap -sS -T4 -A 192.168.1.169
nmap -sS -T4 -A -O 192.168.1.169
文件中讀取需要掃描的IP列表
nmap -iL ips.txt
掃描的結果輸出處理
將掃描的結果輸出到屏幕,同時存儲一份到output.txt。
nmap -sS -p1-65525 192.168.1.169 -oG output.txt
掃描結果輸出爲html。
nmap -sS -p1-65525 192.168.1.169 --webxml -oX - | xsltproc --output file.html
在子網中發現開放netbios的IP
nmap -sV -v -p139,445 192.168.1.0/24
掃描指定netbios的名稱
nmap -sU --script nbstat.nse -p 137 target
掃描指定的目標,同時檢測相關漏洞
nmap --script-args=unsafe=1 --script smb-check-vulns.nse -p 445 169
nmap腳本使用
常用腳本說明
auth: 負責處理鑑權證書(繞開鑑權)的腳本
broadcast: 在局域網內探查更多服務開啓狀況,如dhcp/dns/sqlserver等服務
brute: 提供暴力破解方式,針對常見的應用如http/snmp等
default: 使用-sC或-A選項掃描時候默認的腳本,提供基本腳本掃描能力
discovery: 對網絡進行更多的信息,如SMB枚舉、SNMP查詢等
dos: 用於進行拒絕服務攻擊
exploit: 利用已知的漏洞入侵系統
external: 利用第三方的數據庫或資源,例如進行whois解析
fuzzer: 模糊測試的腳本,發送異常的包到目標機,探測出潛在漏洞 intrusive: 入侵性的腳本,此類腳本可能引發對方的IDS/IPS的記錄或屏蔽
malware: 探測目標機是否感染了病毒、開啓了後門等信息
safe: 此類與intrusive相反,屬於安全性腳本
version: 負責增強服務與版本掃描(Version Detection)功能的腳本
vuln: 負責檢查目標機是否有常見的漏洞(Vulnerability),如是否有MS08_067
負責處理鑑權證書(繞開鑑權)的腳本,也可以作爲檢測部分應用弱口令
nmap --script=auth 192.168.1.*
提供暴力破解的方式 可對數據庫,smb,snmp等進行簡單密碼的暴力猜解
nmap --script=brute 192.168.1.169
默認的腳本掃描,主要是蒐集各種應用服務的信息,收集到後,可再針對具體服務進行攻擊
nmap --script=default 192.168.1.169 或者 nmap -sC 192.168.1.169
檢查是否存在常見漏洞
nmap --script=vuln 192.168.1.169
在局域網內探查更多服務開啓狀況
nmap -n -p445 --script=broadcast 192.168.1.169
利用第三方的數據庫或資源,例如進行whois解析
-- ...
vnc掃描
nmap --script=realvnc-auth-bypass 192.168.1.169
獲取vnc信息
nmap --script=vnc-info 192.168.1.169
smb掃描
說明:SMB協議是基於TCP-NETBIOS下的,一般端口使用爲139,445。
nmap --script=smb-brute.nse 192.168.1.169
nmap --script=smb-brute.nse --script-args=userdb=/var/passwd,passdb=/var/passwd 192.168.1.169
nmap --script=smb-check-vulns.nse --script-args=unsafe=1 192.168.1.169
nmap -p 445 --script smb-ls --script-args 'share=c$,path=\test,smbuser=administrator,smbpass=fuckyou' 192.168.1.169
nmap -p 445 -n –script=smb-psexec --script-args 'smbuser=administrator,smbpass=fuckyou' 192.168.1.169
nmap -p 445 -n --script=smb-enum-sessions --script-args 'smbuser=administrator,smbpass=fuckyou' 192.168.1.169
nmap -p 445 -n --script=smb-os-discovery --script-args 'smbuser=administrator,smbpass=fuckyou' 192.168.1.169
猜解mssql用戶名和密碼
nmap -p1433 --script=ms-sql-brute --script-args=userdb=/var/passwd,passdb=/var/passwd 192.168.1.169
xp_cmdshell 執行命令
nmap -p 1433 --script ms-sql-xp-cmdshell --script-args mssql.username=sa,mssql.password=sa,ms-sql-xp-cmdshell.cmd="net user" 192.168.1.169
dumphash值
nmap -p 1433 --script ms-sql-dump-hashes --script-args mssql.username=sa,mssql.password=sa 192.168.1.169
掃描root空口令
nmap -p3306 --script=mysql-empty-password 192.168.1.169
列出所有MySQL用戶
nmap -p3306 --script=mysql-users --script-args=mysqluser=root 192.168.1.169
支持同一應用的所有腳本掃描
nmap --script=mysql-* 192.168.1.169
Oracle sid掃描
nmap --script=oracle-sid-brute -p 1521-1560 192.168.1.5
oracle弱口令破解
nmap --script oracle-brute -p 1521 --script-args oracle-brute.sid=ORCL,userdb=/var/passwd,passdb=/var/passwd 192.168.1.5
其他一些比較好用的腳本
nmap --script=broadcast-netbios-master-browser 192.168.137.4 發現網關
nmap -p 873 --script rsync-brute --script-args 'rsync-brute.module=www' 192.168.137.4 破解rsync
nmap --script informix-brute -p 9088 192.168.137.4 informix數據庫破解
nmap -p 5432 --script pgsql-brute 192.168.137.4 pgsql破解
nmap -sU --script snmp-brute 192.168.137.4 snmp破解
nmap -sV --script=telnet-brute 192.168.137.4 telnet破解
nmap --script=http-vuln-cve2010-0738 --script-args 'http-vuln-cve2010-0738.paths={/path1/,/path2/}' <target> jboss autopwn
nmap --script=http-methods.nse 192.168.137.4 檢查http方法
nmap --script http-slowloris --max-parallelism 400 192.168.137.4 dos攻擊,對於處理能力較小的站點還挺好用的 'half-HTTP' connections
nmap --script=samba-vuln-cve-2012-1182 -p 139 192.168.137.4
nmap腳本編寫
需要使用Lua腳本語言編寫。
基於傳統的語言標準,我們寫一個腳本,作用:遇到開放的HTTP端口,就返回”Hello World”。
代碼如下:
portrule = function(host, port)
return port.protocol == "tcp" and port.number == 80 and port.state == "open"
end
action = function(host, port)
return "Hello world !"
end
注意:以
NSE腳本主要由三部分組成:
The Head Section
該部分包含一些元數據,主要描述腳本的功能,作者,影響力,類別及其他。
The Rule Section
該部分定義腳本執行的必要條件。至少包含下面列表中的一個函數:
portrule
hostrule
prerule
postrule
此案例中,重點介紹portrule。portrule能夠在執行操作前,檢查host和port屬性。portrule會利用nmap的API檢查TCP80端口。
The Action Section
該部分定義腳本邏輯。此處案例中,檢測到開放80端口,則打印“HelloWorld”。腳本的輸出內容,會在nmap執行期間顯示出來。
nmap -sS -p 22,80,443 --script ./http-vuln-check.nse 192.168.1.169
詳情參考:
https://nmap.org/book/nse/api.html%20
http://blog.csdn.net/nixawk/article/details/39701019
python-nmap使用
pip install python-nmap
文檔: http://xael.org/pages/python-nmap-en.html
libnmap使用
文檔: https://libnmap.readthedocs.org/en/latest/
實例參考: http://www.freebuf.com/tools/32092.html
nmap源碼分析
結構如下:
Nmap/
├─docs(Nmap相關文檔,包括License、usage說明及XMLschema文件等)
│ ├─licenses
│ └─man-xlate
├─libdnet-stripped(libdnet:簡單的網絡接口開源庫)
│ ├─config
│ ├─include
│ └─src
├─liblinear(LIBLINEAR:負責大型線性分類的開源庫)
│ └─blas
├─liblua(Lua腳本語言源碼庫)
├─libnetutil(Nmap實現的基本的網絡實用函數)
├─libpcap(開源的抓包代碼庫libpcap)
│ ├─bpf
│ ├─ChmodBPF
│ ├─lbl
│ ├─missing
│ ├─msdos
│ ├─NMAP_MODIFICATIONS
│ ├─packaging
│ ├─pcap
│ ├─SUNOS4
│ ├─tests
│ └─Win32
├─libpcre(Perl兼容的正則表達式開源庫libpcre)
├─macosx(該目錄負責支持蘋果的操作系統MACOS X)
│ └─nmap.pmdoc
├─mswin32(該目錄負責支持Windows操作系統)
│ ├─lib
│ ├─license-format
│ ├─NET
│ ├─NETINET
│ ├─nsis
│ ├─OpenSSL
│ ├─pcap-include
│ ├─RPC
│ └─winpcap
├─nbase(Nmap封裝的基礎使用程序庫,包括string/path/random等)
├─ncat(Ncat是Nmap項目組實現的新版的netcat:強大的網絡工具)
│ ├─certs
│ ├─docs
│ └─test
├─ndiff(Ndiff是用於比較Nmap掃描結果的實用命令)
│ ├─docs
│ └─test-scans
├─nmap-update(負責Nmap更新相關操作)
├─nping(Nping是Nmap項目組實現的新版的Hping:網絡探測與構建packet)
│ └─docs
├─nselib(Nmap使用Lua語言編寫的常用的腳本庫)
│ └─data
├─nsock(Nmap實現的並行的SocketEvent處理庫)
│ ├─include
│ └─src
├─scripts(Nmap提供常用的掃描檢查的lua腳本)
├─todo(介紹Nmap項目將來開發的具體任務)
└─zenmap(Nmap的官方的圖形界面程序,由python語言編寫)
├─install_scripts
├─radialnet
├─share
├─test
├─zenmapCore
└─zenmapGUI
- 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
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 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
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
nmap6.0工程內總共包括1300多個文件。
C和C++文件有600多個,主要實現Nmap最核心的功能:主機發現、端口掃描、服務偵測、OS偵測及搭建腳本引擎框架;也包括其他開源項目如libpcap的源碼。
Python文件有100多個,主要實現Zenmap圖形界面,Zenmap會調用到Nmap基本命令,也實現一些新的功能:例如確定網絡拓撲結構、Profile的管理(常用的命令保存爲Profile)等。
Lua與NSE文件400多個,負責構建Nmap腳本引擎及提供常用的掃描腳本。其中NSE格式爲Nmap定製的Lua文件,方便用戶自行編寫腳本進行功能擴展。
XML文件數十個,用於輔助描述Nmap的內容或Zenmap的測試等工作。
其他文件,其他輔助工具操作的文件。
執行流程:
nmap分佈式版本- dnmap
dnmap_server的用法
dnmap_server -h
新建一個nmap 命令文件command.txt
提交作業給服務器機子:
dnmap_server -f ./command.txt
服務端啓動之後,就開始等待客戶端鏈接。
dnmap_client的用法
dnmap_client -h
連接到服務器地址,端口號及客戶端的命名
dnmap_client -s 192.168.1.152 -a 'salve01'
默認情況下掃描結果都被存儲在名爲 "nmap_result/." 目錄下。
參考: http://www.mateslab.com.ar/dnmap-the-distributed-nmap.html
nmap工控分析
工控分析也利用搜索引擎:shodan/zoomeye;下面主要介紹nmap常用的腳本。
nmap -p 44818 --script enip-enumerate.nse 85.132.179.*
nmap --script modicon-info.nse -Pn -p 502 -sV 91.83.43.*
nmap -Pn -n -d --script iec-identify.nse --script-args=iec-identify -p 2404 80.34.253.*
nmap -p 102 --script s7-enumerate -sV 140.207.152.*
nmap -d --script mms-identify.nse --script-args='mms-identify.timeout=500' -p 102 IP
nmap -p 1911 --script fox-info 99.55.238.*
常見腳本資源
github
https://github.com/atimorin/scada-tools
https://github.com/atimorin/PoC2013
https://github.com/drainware/scada-tools
https://github.com/drainware/nmap-scada
exploit-db
https://www.exploit-db.com/exploits/19833/
https://www.exploit-db.com/exploits/19832/
https://www.exploit-db.com/exploits/19831/