Masscan號稱是最快的互聯網端口掃描器,最快可以在六分鍾內掃遍互聯網。
masscan的掃描結果類似於nmap(一個很著名的端口掃描器),在內部,它更像scanrand, unicornscan, and ZMap,采用了異步傳輸的方式。它和這些掃描器最主要的區別是,它比這些掃描器更快。而且,masscan更加靈活,它允許自定義任意的地址范和端口范圍。
安裝配置
在Debian/Ubuntu系統中,安裝方法如下:
sudo apt-get install git gcc make libpcap-dev git clone https://github.com/robertdavidgraham/masscan cd masscan make
注:安裝完成后的可執行程序在masscan/bin中
您也可以使用多線程加快編譯速度 make -j
PF_RING
如果您想獲得超過兩百萬每秒的速度,您需要一個英特爾10-Gbps網卡和一個被稱為PF_RING DNA(http://www.ntop.org/products/pf_ring/)的特殊驅動程序。masscan使用PF_RING不要進行重建,你只需要建立以下組件即可:
libpfring.so (安裝於 /usr/lib/libpfring.so) pf_ring.ko (pf_ring自己的內核驅動) ixgbe.ko (英特爾10-gbps網卡驅動) 你不要建立自己的libpcap.so
當masscan檢測到網卡為dna0時,將自動切換為PF_RING模式
自我測試
當安裝完成是可以測試一下是否安裝成功:
make regress bin/masscan --regress selftest: success!
使用方法
使用方法類似於nmap,掃描指定網段范圍的指定端口
# masscan -p80,8080-8100 10.0.0.0/8
這條命令將完成如下工作:
1、掃描10.x.x.x子網,大約有1600萬個地址
2、掃描端口80和8000-8100范圍的端口段
3、打印結果到標准輸出,也可以重定向到一個文件
也可以 –echo 將當前的配置輸出到一個配置文件,利用 -c 來制定配置文件進行掃描
# masscan -p80,8000-8100 10.0.0.0/8 --echo > xxx.conf # masscan -c xxx.conf --rate 1000
Banner獲取
masscan不僅僅只是測試端口是否開放,它還能在完成TCP連接的同時獲取目標應用程序的Banner信息(--banners選項值旗標,或者指紋)。
# masscan 10.0.0.0/8 -p80 --banners --source-ip x.x.x.x
這條命令的意思是掃描10.x.x.x網段80端口的開放信息,並且獲取banner信息。–source-ip 是指定源IP,這個ip必須指定獨立有效的IP地址。
您可以通過配置防火牆來防止masscan的返回不了信息的情況
Linux可以在這樣配置
# iptables -A INPUT -p tcp --dport 60000 -j DROP # masscan 10.0.0.0/8 -p80 --banners --source-port 60000
MAC OS X和BSD可以這樣配置
# sudo ipfw add 1 deny tcp from any to any 60000 in # masscan 10.0.0.0/8 -p80 --banners --source-port 60000
您也可以檢查其他類型的信息,例如Linux-HA工程的一個組件 –-heartbleed,方法和獲取banner是一樣的
掃描全網
您可以對整個互聯網發起掃描,使用的命令如下
# masscan 0.0.0.0/0 -p0-65535
有時候對這個互聯網掃描可能會對你造成一些麻煩,你可能會被請去喝茶。所以,我們可以通過設置黑名單來讓掃描器掃描時忽略一些網段。語法如下:
# masscan 0.0.0.0/0 -p0-65535 --excludefile exclude.txt
你也可以把掃描的結果不輸出到命令行,輸出到指定文件中
# masscan 0.0.0.0/0 -p0-65535 -oX scan.xml
這條命令把掃描的結果保存在一個XML文件中
當前的掃描器使用的是默認的速率100包/秒,如果是掃描全網的話,需要提高速度。
# masscan 0.0.0.0/0 -p0-65535 --max-rate 100000
這條命令將以每秒10萬包的速率進行掃描
上述的這些命令可能輸入起來有點麻煩,我們可以通過創建配置文件,然后用加載配置文件的方式運行。配置文件的內容如下所示:
rate = 100000 output-format = xxx output-status = all output-filename = xxx.xxx ports = 0-65535 range = 0.0.0.0-255.255.255.255 excludefile = exclude.txt
掃描時,用 -c 加載配置文件,這樣的話,你進行重復掃描會省很多的力氣。
結果輸出
主要有5種輸出的格式
1、XML 默認格式 使用-oX <filename> 或者使用 –output-format xml 和 –output-filename <filename>進行指定 2、binary masscan內置格式 masscan --open --banners --readscan output.txt -oX 2.txt 打開顯示模式,讀取output.txt中的數據,並以xml的格式寫到2.txt中
3、grepable nmap格式 使用 -oG <filename> 或者 –output-format grepable 和 –output-filename <filename>進行指定 4、json 使用 -oJ <filename> 或者 –output-format json 和 –output-filename <filename>進行指定 5、list 簡單的列表,每行一個主機端口對。使用-oL <filename> 或者 –output-format list 和 –output-filename <filename>進行指定
By default, masscan produces fairly large text files, but it's easy to convert them into any other format. There are five supported output formats:
-
xml: Just use the parameter
-oX <filename>
. Or, use the parameters--output-format xml
and--output-filename <filename>
. -
binary: This is the masscan builtin format. It produces much smaller files, so that when I scan the Internet my disk doesn't fill up. They need to be parsed, though. The command line option
--readscan
will read binary scan files. Using--readscan
with the-oX
option will produce a XML version of the results file. -
grepable: This is an implementation of the Nmap -oG output that can be easily parsed by command-line tools. Just use the parameter
-oG <filename>
. Or, use the parameters--output-format grepable
and--output-filename <filename>
. -
json: This saves the results in JSON format. Just use the parameter
-oJ <filename>
. Or, use the parameters--output-format json
and--output-filename <filename>
. -
list: This is a simple list with one host and port pair per line. Just use the parameter
-oL <filename>
. Or, use the parameters--output-format list
and--output-filename <filename>
. The format is:
masscan --includefile fujian.txt -p0-1234 -oL scan.list --max-rate 100000 # --includefile fujian.txt讀取一個范圍列表進行掃描
masscan -iL fujian.txt-p0-1234 -oL scan.list --max-rate 100000 # -iL fujian.txt讀取一個范圍列表進行掃描
masscan ip/range -p0-1234 --append-output -oL scan.list --max-rate 100000 # 將掃描結果追加進scan.list文件
<port state> <protocol> <port number> <IP address> <POSIX timestamp> open tcp 80 XXX.XXX.XXX.XXX 1390380064
傳輸速度
masscan的發包速度非常快,在windows中,它的發包速度可以達到每秒30萬包;在Linux中,速度可以達到每秒160萬。masscan在掃描時會隨機選擇目標IP,所以不會對遠程的主機造成壓力。
默認情況下,masscan的發包速度為每秒100包,為了提高速度,可以設置為 –rate 100000
我們注意到zmap和masscan都有一個控制發包速率的參數,為什么需要這個參數呢?不是越高越好嗎?不是的,這個參數的設置直接影響漏報。
一般家用adsl的上行速度在100kb/s-300kb/s 之間,以互聯網最小包60byte計算,100kb/s =1746 pps,也就是說每秒發送數據包約2000個,超出就容易丟包漏報。通過這個公式不難得出在一個家庭adsl環境下且保證准確度,用zmap掃描全部互聯網需要255*255*255*255/2000/3600/24=24天。
詳細參數
<ip/range> IP地址范圍,有三種有效格式,1、單獨的IPv4地址 2、類似"10.0.0.1-10.0.0.233"的范圍地址 3、CIDR地址 類似於"0.0.0.0/0",多個目標可以用都好隔開 -p <ports,--ports <ports>> 指定端口進行掃描 --banners 獲取banner信息,支持少量的協議 --rate <packets-per-second> 指定發包的速率 -c <filename>, --conf <filename> 讀取配置文件進行掃描 --echo 將當前的配置重定向到一個配置文件中 -e <ifname> , --adapter <ifname> 指定用來發包的網卡接口名稱 --adapter-ip <ip-address> 指定發包的IP地址 --adapter-port <port> 指定發包的源端口 --adapter-mac <mac-address> 指定發包的源MAC地址 --router-mac <mac address> 指定網關的MAC地址 --exclude <ip/range> IP地址范圍黑名單,防止masscan掃描 --excludefile <filename> 指定IP地址范圍黑名單文件 --includefile,-iL <filename> 讀取一個范圍列表進行掃描 --ping 掃描應該包含ICMP回應請求 --append-output 以附加的形式輸出到文件 --iflist 列出可用的網絡接口,然后退出 --retries 發送重試的次數,以1秒為間隔 --nmap 打印與nmap兼容的相關信息 --http-user-agent <user-agent> 設置user-agent字段的值 --show [open,close] 告訴要顯示的端口狀態,默認是顯示開放端口 --noshow [open,close] 禁用端口狀態顯示 --pcap <filename> 將接收到的數據包以libpcap格式存儲 --regress 運行回歸測試,測試掃描器是否正常運行 --ttl <num> 指定傳出數據包的TTL值,默認為255 --wait <seconds> 指定發送完包之后的等待時間,默認為10秒 --offline 沒有實際的發包,主要用來測試開銷 -sL 不執行掃描,主要是生成一個隨機地址列表 --readscan <binary-files> 讀取從-oB生成的二進制文件,可以轉化為XML或者JSON格式. --connection-timeout <secs> 抓取banners時指定保持TCP連接的最大秒數,默認是30秒。
參考資料:
http://www.freebuf.com/sectool/112583.html
https://www.ctolib.com/masscan.html