天天看点

护网自动化脚本

声明

由于传播、利用此文所提供的信息而造成的任何直接或者间接的后果及损失,均由使用者本人负责,雷神众测以及文章作者不为此承担任何责任。

雷神众测拥有对此文章的修改和解释权。如欲转载或传播此文章,必须保证此文章的完整性,包括版权声明等全部内容。未经雷神众测允许,不得任意修改或者增减此文章内容,不得以任何方式将其用于商业目的。

前言

为了护网需要写了这么个小脚本,本文主要介绍下这个自动化工具的实现思路,以及这个工具的使用方法。

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

护网自动化脚本
护网自动化脚本
护网自动化脚本

继续阅读