聲明
由于傳播、利用此文所提供的資訊而造成的任何直接或者間接的後果及損失,均由使用者本人負責,雷神衆測以及文章作者不為此承擔任何責任。
雷神衆測擁有對此文章的修改和解釋權。如欲轉載或傳播此文章,必須保證此文章的完整性,包括版權聲明等全部内容。未經雷神衆測允許,不得任意修改或者增減此文章内容,不得以任何方式将其用于商業目的。
前言
為了護網需要寫了這麼個小腳本,本文主要介紹下這個自動化工具的實作思路,以及這個工具的使用方法。
No.1
本文主要内容
1、Masscan與NMAP關聯
2、掃描結果解析工具
3、幾款開源工具 Eyewitness、tomcat-weak-password-scanner、漏洞發現、brutespray
NMAP是滲透測試人員廣泛使用的老牌開源工具,功能強大,但掃描速度較慢。Masscan号稱是世界上最快的掃描器,但功能較單一。很多經驗豐富的滲透大佬都會将這兩個東西結合在一起使用。
我們在紅隊前期充分收集目标企業的IP資産後,即可以使用CIDR等IP的表示方法導入掃描器。目前暫不支援目标域名的導入方式有兩個原因:
1、DNS資訊收集的過程有點慢
2、為了盡量保證精準度,暫時不想加入太多功能
在開始之前先介紹一下Masscan的設定,--rate這個參數代表 packets/second 每秒發送的資料包,這個和帶寬沒啥關系。
Masscan -p1-65535 10.0.0.0/24 --rate 100000 -oX scan.xml
--rate <packets-per-second>: specifies the desired rate for trans‐
mitting packets. This can be very small numbers, like 0.1 for
transmitting packets at rates of one every 10 seconds, for very
large numbers like 10000000, which attempts to transmit at 10 mil‐
lion packets/second. In my experience, Windows and can do 250 thou‐
sand packets per second, and latest versions of Linux can do 2.5
million packets per second. The PF_RING driver is needed to get to
25 million packets/second.
那麼如何計算出适用于自己帶寬的--rate設定呢?我算了一下Masscan的掃描一個資料包是68byte:

Masscan的一個包是68位元組,如果我們每秒鐘要發送10w/秒,就需要6.48M,68*100000/1024/1024=6.48M以上的帶寬。通常windows每秒能發25W/秒,linux250W/秒。PF_RING可以達到2500W/秒。我在腳本中使用的是--rate 30000,也就是需要1M的帶寬。
下面說說VPS的相關選擇,在國内的阿裡雲/騰訊雲雖然帶寬足夠,但是我發現機房使用Masscan等發包太快的工具是會導緻丢包的,效果一點不好。而國外的伺服器過于擁擠,通常一般的線路丢包有點嚴重,但一般目标企業的網絡線路都還可以,是以我選擇了不限制使用Masscan的國外vps。
No.2
Masscan與NMAP關聯
BASH腳本:
sudo masscan -p1-65535 --rate 30000 --open -iL $TARGET -oX $NRESULTS_PATH/masscan.xml
sudo rm $WORKING_DIR/paused.conf
open_ports=$(cat $NRESULTS_PATH/masscan.xml | grep portid | cut -d "\"" -f 10 | sort -n | uniq | paste -sd,)
cat $NRESULTS_PATH/masscan.xml | grep portid | cut -d "\"" -f 4 | sort -V | uniq > $WORKING_DIR/nmap_targets.tmp
sudo nmap -sVC -p $open_ports --open -v -Pn -n -T4 -iL $WORKING_DIR/nmap_targets.tmp -oX $NRESULTS_PATH/nmap.xml
sudo rm $WORKING_DIR/nmap_targets.tmp
xsltproc -o $NRESULTS_PATH/nmap-bootstrap.html $WORKING_DIR/bootstrap-nmap.xsl $NRESULTS_PATH/nmap.xml
将Masscan的掃描結果中的端口提取出來放到放到變量open_ports,将IP位址提取放到臨時檔案nmap_targets.tmp中。然後使用nmap -p -iL 調用端口/IP。最後使用xsltproc工具,根據bootstrap-nmap.xsl将nmap的掃描結果生成為友善檢視/搜尋的html格式。
No.3
掃描結果解析工具
有了nmap的xml,就可以提取出來裡面的資訊,進行下一步自動化測試了。在自動化測試之前需要了解一下這個開源的nmap結果解析工具nmap-parse-output:
檢查并安裝Nmap-parse-output解析工具:
if [ -d "nmap-parse-output" ];then
echo -e "${BLUE}[-] Latest version of Nmap-parse-output already installed. Skipping...${RESET}"
else
echo -e "${GREEN}[+] Installing nmap-parse-output.${RESET}"
git clone https://github.com/ernw/nmap-parse-output
fi
提取http/https的url
nmap-parse-output nmap.xml http-ports
nmap-parse-output nmap.xml tls-ports | awk '{print "https://"$1}'
提取服務/産品名稱
nmap-parse-output nmap.xml service-names
nmap-parse-output nmap.xml product
提取所有Tomcat URL
nmap-parse-output nmap.xml search-product "Apache Tomcat"
提取title
nmap-parse-output nmap.xml http-title"
我們把nmap識别出來的http/https的端口、以及所有Apache Tomcat應用全部導入到臨時檔案url.tmp和ip.txt/port.txt中友善下一步的Web自動化測試,再儲存http-title、product、service-names友善檢視,了解目标資訊。
$WORKING_DIR/nmap-parse-output/nmap-parse-output $NRESULTS_PATH/nmap.xml http-ports | tee url.tmp
$WORKING_DIR/nmap-parse-output/nmap-parse-output $NRESULTS_PATH/nmap.xml tls-ports | awk '{print "https://"$1}'|tee -a url.tmp
cat url.tmp |sort|uniq >url_list && rm -rf url.tmp
$WORKING_DIR/nmap-parse-output/nmap-parse-output $NRESULTS_PATH/nmap.xml service-names > $NRESULTS_PATH/service-names.txt
$WORKING_DIR/nmap-parse-output/nmap-parse-output $NRESULTS_PATH/nmap.xml product > $NRESULTS_PATH/product.txt
IFS_old=$IFS;IFS=$'\n'
for line in `$WORKING_DIR/nmap-parse-output/nmap-parse-output $NRESULTS_PATH/nmap.xml http-title`;do echo -e $line;done | tee $NRESULTS_PATH/http-title.txt
IFS=$IFS_old
$WORKING_DIR/nmap-parse-output/nmap-parse-output $NRESULTS_PATH/nmap.xml search-product "Apache Tomcat" | awk -F : '{print $1}'>tomcat-weak-password-scanner/ip.txt
$WORKING_DIR/nmap-parse-output/nmap-parse-output $NRESULTS_PATH/nmap.xml search-product "Apache Tomcat" | awk -F : '{print $2}'>tomcat-weak-password-scanner/port.txt
No.4
Eyewitness
EyeWitness功能:
1、它的快照可以讓我們更直覺地識别資産,通過網站類型快速判斷網段是否屬于目标。
2、更直覺的看所使用的到應用是什麼。
3、判斷是否存在登陸頁面,識别常見登陸背景。
4、RDP登陸賬戶顯示。
檢查并安裝Eyewitness工具:
if [ -d "EyeWitness" ];then
echo -e "${BLUE}[-] Latest version of Eyewitness already installed. Skipping...${RESET}"
echo -e "${GREEN}[+] Installing EyeWitness.${RESET}"
git clone https://github.com/FortyNorthSecurity/EyeWitness && sudo ./EyeWitness/setup/setup.sh
if [ -d "tomcat-weak-password-scanner" ];then
echo -e "${BLUE}[-] Latest version of tomcat-weak-password-scanner already installed. Skipping...${RESET}"
echo -e "${GREEN}[+] Installing tomcat-weak-password-scanner.${RESET}"
git clone https://github.com/magicming200/tomcat-weak-password-scanner
判斷是否存在登入頁面,識别網絡裝置/背景等
判斷高價值目标
更直覺的展示資産
識别rdp登入使用者名
sudo -i python3 $WORKING_DIR/EyeWitness/EyeWitness.py -x $NRESULTS_PATH/nmap.xml --no-prompt -d $ERESULTS_PATH --no-dns --ocr
No.5
漏洞檢測
1、漏洞指紋(Weblogic、Shiro、Strust2、Solr等...)
2、CMS識别(Seeyon、通達、泛微、Discuz、DeDeCms等...)
3、服務爆破(MSSQL、SSH、VNC、FTP、TELNET等...)
漏洞檢測使用的是偉剛寫的漏洞檢測插件,模版如下:
主程式
import sys
import threading
from plugins import *
from termcolor import cprint
from queue import Queue
vul_list = []
web_queue = []
with open(sys.argv[1], "r") as f:
for i in f.readlines():
web_queue.append(i.strip())
f.close()
for vulClass in [shiro,weblogic,struts2,jboss]:
detect = vulClass.Detect
try:
alive_Web_queue = Queue(-1)
for _ in web_queue:
alive_Web_queue.put(_)
threads = []
thread_num = 30
for num in range(1, thread_num + 1):
t = detect(alive_Web_queue, vul_list)
threads.append(t)
t.start()
for t in threads:
t.join()
except Exception as e:
print(r'[-] Load Vul [{}] Error: {}'.format(detect.name, e.args))
continue
插件模版
import requests
from urllib.parse import urlparse
class Detect(threading.Thread):
def __init__(self, alive_Web_queue, vul_list):
threading.Thread.__init__(self)
self.headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0'}
self.alive_Web_queue = alive_Web_queue
self.vul_list = vul_list
def run(self):
while not self.alive_Web_queue.empty():
alive_web = self.alive_Web_queue.get()
self.run_detect(alive_web)
def CVE_2017_10271(self,url):
weblogic_url = url + '/wls-wsat/CoordinatorPortType'
try:
res = requests.get(url=weblogic_url, headers=self.headers, allow_redirects=False, timeout=10)
if 'CoordinatorPortType?wsdl' in res.text:
cprint('[CVE_2017_10271] {}'.format(url), 'red')
self.vul_list.append(['weblogic', url])
else:
print('[-] {}'.format(url))
except Exception as e:
pass
def CVE_2019_2725(self,url):
weblogic_url = url + '/_async/AsyncResponseService'
if 'AsyncResponseService home page' in res.text:
cprint('[CVE-2019-2725] {}'.format(url), 'red')
def CVE_2019_2729(self,url):
weblogic_url = url + '/wls-wsat/CoordinatorPortType11'
if 'CoordinatorPortType11?wsdl' in res.text:
cprint('[CVE-2019-2729] {}'.format(url), 'red')
def run_detect(self, url):
if not urlparse(url).scheme:
url = 'https://' + url
else:
url = url
self.CVE_2017_10271(url)
self.CVE_2019_2725(url)
self.CVE_2019_2729(url)
示範效果
No.6
服務爆破
爆破使用的是brutespray開源工具,可以直接提取nmap的xml掃描結果,然後進行對應的服務爆破。
還有tomcat-weak-password-scanner:
No.7
工具使用
#!/bin/bash
TARGET="$1"
TIME=`date +"%Y%m%d%H%M"`
WORKING_DIR="$(cd "$(dirname "$0")" ; pwd -P)"
NRESULTS_PATH="$WORKING_DIR/$TIME/nresults"
ERESULTS_PATH="$WORKING_DIR/$TIME/eresults"
RED="\033[1;31m"
GREEN="\033[1;32m"
BLUE="\033[1;36m"
YELLOW="\033[1;33m"
RESET="\033[0m"
checkArgs(){
if [[ $# -eq 0 ]]; then
echo -e "\t${RED}[!] ERROR:${RESET} Invalid argument!\n"
echo -e "\t${GREEN}[+] USAGE:${RESET}$0 ip.txt\n"
exit 1
elif [ ! -s $1 ]; then
echo -e "\t${RED}[!] ERROR:${RESET} File is empty and/or does not exists!\n"
}
setupTools(){
echo -e "${GREEN}[+] Setting things up.${RESET}"
sudo apt update -y
#sudo apt upgrade -y
#sudo apt autoremove -y
#sudo apt clean
sudo apt install -y gcc g++ make libpcap-dev xsltproc
echo -e "${GREEN}[+] Creating results directory.${RESET}"
mkdir -p $NRESULTS_PATH
installTools(){
LATEST_MASSCAN="1.0.6"
if [ ! -x "$(command -v masscan)" ]; then
echo -e "${GREEN}[+] Installing Masscan.${RESET}"
git clone https://github.com/robertdavidgraham/masscan
cd masscan
make -j
sudo make -j install
cd $WORKING_DIR
rm -rf masscan
if [ "$LATEST_MASSCAN" == "$(masscan -V | grep "Masscan version" | cut -d " " -f 3)" ]; then
echo -e "${BLUE}[-] Latest version of Masscan already installed. Skipping...${RESET}"
else
echo -e "${GREEN}[+] Upgrading Masscan to the latest version.${RESET}"
git clone https://github.com/robertdavidgraham/masscan
cd masscan
make -j
sudo make -j install
cd $WORKING_DIR
rm -rf masscan*
fi
LATEST_NMAP="$(wget -qO- https://nmap.org/dist/ | grep -oP 'nmap-([0-9\.]+)\.tar\.bz2'| tail -n 1 | grep -oP 'nmap-[0-9\.]+' | grep -oP '[0-9\.]+' | head -c -2)"
if [ ! -x "$(command -v nmap)" ]; then
echo -e "${GREEN}[+] Installing Nmap.${RESET}"
wget https://nmap.org/dist/nmap-$LATEST_NMAP.tar.bz2
bzip2 -cd nmap-$LATEST_NMAP.tar.bz2 | tar xvf -
cd nmap-$LATEST_NMAP
./configure
rm -rf nmap-$LATEST_NMAP*
else
if [ "$LATEST_NMAP" == "$(nmap -V | grep "Nmap version" | cut -d " " -f 3)" ]; then
echo -e "${BLUE}[-] Latest version of Nmap already installed. Skipping...${RESET}"
echo -e "${GREEN}[+] Upgrading Nmap to the latest version.${RESET}"
wget https://nmap.org/dist/nmap-$LATEST_NMAP.tar.bz2
bzip2 -cd nmap-$LATEST_NMAP.tar.bz2 | tar xvf -
cd nmap-$LATEST_NMAP
./configure
rm -rf nmap-$LATEST_NMAP*
fi
if [ -d "nmap-parse-output" ];then
portScan(){
echo -e "${GREEN}[+] Running Masscan.${RESET}"
xsltproc -o $NRESULTS_PATH/masscan.html $WORKING_DIR/bootstrap-masscan.xsl $RESULTS_PATH/masscan.xml
echo -e "${RED}[*] Masscan Done! View the HTML report at $NRESULTS_PATH${RESET}"
echo -e "${GREEN}[+] Running Nmap.${RESET}"
echo -e "${RED}[*] Nmap Done! View the HTML reports at $NRESULTS_PATH${RESET}"
echo -e "${RED}[*] Nmap-parse-output Done!${RESET}"
echo -e "${GREEN}[+] Running Nmap-parse-output.${RESET}"
vulScanner(){
sudo pip install -r requrement.txt
echo -e "${GREEN}[+] Running vul-scanner.${RESET}"
python3 $WORKING_DIR/epfa.py url_list | tee $NRESULTS_PATH/vul_result.txt
echo -e "${GREEN}[+] Running Eyewitness.${RESET}"
sudo -i python3 $WORKING_DIR/EyeWitness/EyeWitness.py -x $NRESULTS_PATH/nmap.xml --no-prompt -d $ERESULTS_PATH --no-dns --ocr
echo -e "${GREEN}[+] Running weak-password-scanner.${RESET}"
cd $WORKING_DIR/tomcat-weak-password-scanner/ && python koala_tomcat_cmd.py -h ip.txt -p port.txt && cd -
checkArgs $TARGET
setupTools
installTools
portScan
vulScanner
No.8
使用方法
echo ip > ip.txt
chmod +x ./recon.sh
./recon.sh ip.txt