巡風掃描器--nascan源碼分析

nascan部分的結構:
在這裏插入圖片描述
lib
init.py
cider.py: CIDR形式IP地址解析;
common.py: 其它方法;
icmp.py: ICMP消息發送類;
log.py: 控制檯信息輸出;
mongo.py: 數據庫連接;
scan.py: 掃描與識別;
start.py: 線程控制;
plugin
masscan.py :masscan調用腳本;
nascan.py: 網絡資產信息抓取引擎;

nascan代碼分析

import thread
from lib.common import *
from lib.start import *
if __name__ == "__main__":
    try:
        CONFIG_INI = get_config()  # 讀取配置,讀取數據庫中整個Config集合數據
        log.write('info', None, 0, u'獲取配置成功')#輸出到控制檯
        STATISTICS = get_statistics()  # 讀取統計信息
        MASSCAN_AC = [0]  #值爲1表示masscan正在掃描
        NACHANGE = [0]   #值爲1表示能進入掃描階段
        thread.start_new_thread(monitor, (CONFIG_INI, STATISTICS, NACHANGE))  # 心跳線程
        thread.start_new_thread(cruise, (STATISTICS, MASSCAN_AC))  # 失效記錄刪除線程
        socket.setdefaulttimeout(int(CONFIG_INI['Timeout']) / 2)  # 設置連接超時
        ac_data = []
        while True:
            now_time = time.localtime()
            now_hour = now_time.tm_hour
            now_day = now_time.tm_mday
            now_date = str(now_time.tm_year) + \
                str(now_time.tm_mon) + str(now_day)
            cy_day, ac_hour = CONFIG_INI['Cycle'].split('|')
            log.write('info', None, 0, u'掃描規則: ' + str(CONFIG_INI['Cycle']))#log.write()函數,格式化了輸出在控制檯界面的信息,並使用了線程鎖,防止信息一時間輸出過多,導致顯示錯行。
            #  判斷是否進入掃描時段或者能直接進入掃描階段
            if (now_hour == int(ac_hour) and now_day % int(cy_day) == 0 and now_date not in ac_data) or NACHANGE[0]:
                ac_data.append(now_date)
                #恢復原值,不能再次進入資產探測,直到新的事件觸發該值改變
                NACHANGE[0] = 0
                log.write('info', None, 0, u'開始掃描')
                # 具體的資產發現操作
                s = start(CONFIG_INI)
                # masscan掃描狀態
                s.masscan_ac = MASSCAN_AC
                s.statistics = STATISTICS
                s.run()
            time.sleep(60)
    except Exception, e:
        print e

1、CONFIG_INI = get_config() # 讀取配置,讀取數據庫中整個Config集合數據
讀取配置,get_config()進去
nascan/lib/common.py

# 信息識別Config集合, 配置統一格式化,返回dict類型
def get_config():
    config = {}
    # Config集合共有vulscan、nascan兩個子集合,獲取Config集合中的nascan子集合的文檔內容
    config_info = mongo.na_db.Config.find_one({"type": "nascan"})
    for name in config_info['config']:
        if name in ['Discern_cms', 'Discern_con', 'Discern_lang', 'Discern_server']:
            '''  cms識別、組件容器識別、語言技術識別、端口服務識別四個部分的文檔內容賦值配按照
            事先定義的格式進一步格式化分離數據,
            方便後續取用.    '''
            config[name] = format_config(name, config_info['config'][name]['value'])
        else:
            config[name] = config_info['config'][name]['value']
    return config
  • 讀取了mongodb裏面的Config集合中的nascan子集合的文檔內容;
    2、get_statistics()讀取統計信息返回時間
    位於nascan/lib/common.py
def get_statistics():
    date_ = datetime.datetime.now().strftime('%Y-%m-%d')
    now_stati = mongo.na_db.Statistics.find_one({"date": date_})
    if not now_stati:
        now_stati = {date_: {"add": 0, "update": 0, "delete": 0}}
        return now_stati
    else:
        return {date_: now_stati['info']}

3、 MASSCAN_AC = [0] #值爲1表示masscan正在掃描
NACHANGE = [0] #值爲1表示能進入掃描階段
4、
thread.start_new_thread(monitor, (CONFIG_INI, STATISTICS, NACHANGE)) # 心跳線程
thread.start_new_thread(cruise, (STATISTICS, MASSCAN_AC)) # 失效記錄刪除線程
socket.setdefaulttimeout(int(CONFIG_INI['Timeout']) / 2) # 設置連接超時

  • monitor–心跳線程
    位於nascan/lib/common.py
def monitor(CONFIG_INI, STATISTICS, NACHANGE):
    while True:#線程通過While True和設定延時,實現了監控資產列表,定時更新數據庫、觸發掃描、清理失效目標等操作。
        try:
            time_ = datetime.datetime.now()
            date_ = time_.strftime('%Y-%m-%d')
            mongo.na_db.Heartbeat.update({"name": "heartbeat"}, {"$set": {"up_time": time_}})
            if date_ not in STATISTICS: STATISTICS[date_] = {"add": 0, "update": 0, "delete": 0}
            mongo.na_db.Statistics.update({"date": date_}, {"$set": {"info": STATISTICS[date_]}}, upsert=True)
            new_config = get_config()#獲取數據庫最新的config集合數據
            if base64.b64encode(CONFIG_INI["Scan_list"]) != base64.b64encode(new_config["Scan_list"]):NACHANGE[0] = 1# 比較掃描目標是否發生了變化, 變化就將值置爲1, 表示需要重新掃描
            CONFIG_INI.clear()
            CONFIG_INI.update(new_config)
        except Exception, e:
            print e
        time.sleep(30)

再一次調用get_config()獲取數據庫config集合中最新的數據;
如果scan_list的base的值發生變化,則將NACHANGE[0] = 1,更新config,重新進行掃描;
- cruise–失效刪除記錄
位於nascan/lib/common.py

def cruise(STATISTICS,MASSCAN_AC):
    while True:
        now_str = datetime.datetime.now()
        week = int(now_str.weekday())
        hour = int(now_str.hour)
        if week >= 1 and week <= 5 and hour >= 9 and hour <= 18:  # 非工作時間不刪除
            try:
                data = mongo.NA_INFO.find().sort("time", 1)
                for history_info in data:
                    while True:
                        if MASSCAN_AC[0]:  # 如果masscan正在掃描即不進行清理
                            time.sleep(10)
                        else:
                            break
                    ip = history_info['ip']
                    port = history_info['port']
                    try:
                        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)#對目標(ip:port)進行sock連接
                        sock.connect((ip, int(port)))
                        sock.close()
                    except Exception, e:
                        time_ = datetime.datetime.now()
                        date_ = time_.strftime('%Y-%m-%d')
                        mongo.NA_INFO.remove({"ip": ip, "port": port})#進行sock連接,連接不上就刪除數據庫INFO裏面的ip和port
                        log.write('info', None, 0, '%s:%s delete' % (ip, port))
                        STATISTICS[date_]['delete'] += 1
                        del history_info["_id"]
                        history_info['del_time'] = time_#然後將刪除時間寫入history_info
                        history_info['type'] = 'delete'
                        mongo.NA_HISTORY.insert(history_info)
            except:
                pass
        time.sleep(3600)

對目標(ip:port)進行sock連接,如果連接不上就刪除數據庫INFO裏面的ip和port,然後將刪除時間寫入history_info;
5、log.write('info', None, 0, u'掃描規則: ' + str(CONFIG_INI['Cycle']))
log.write()函數,格式化了輸出在控制檯界面的信息;
6、if (now_hour == int(ac_hour) and now_day % int(cy_day) == 0 and now_date not in ac_data) or NACHANGE[0]:
ac_data.append(now_date)
如果到達掃描的週期時間或者如果NACHANGE[0]的值爲1,任何一個成立都可以重新掃描;
7、s = start(CONFIG_INI)
進入start()函數,位於nascan/lib/start.py


class start:
    def __init__(self, config):  # 默認配置
        self.config_ini = config
        self.queue = Queue.Queue()
        self.thread = int(self.config_ini['Thread'])
        self.scan_list = self.config_ini['Scan_list'].split('\n')
        self.mode = int(self.config_ini['Masscan'].split('|')[0])
        self.icmp = int(self.config_ini['Port_list'].split('|')[0])
        self.white_list = self.config_ini.get('White_list', '').split('\n')

    def run(self):
        global AC_PORT_LIST
        all_ip_list = []
        for ip in self.scan_list:
            # 解析CIDR形式IP地址
            if "/" in ip:
                ip = cidr.CIDR(ip)
            if not ip:
                continue
            # 獲得完整目標IP地址列表
            ip_list = self.get_ip_list(ip)
            for white_ip in self.white_list:
                if white_ip in ip_list:
                    ip_list.remove(white_ip)
             # 當使用masscan掃描時
            if self.mode == 1:#判斷是否支持masscan掃描
                masscan_path = self.config_ini['Masscan'].split('|')[2]
                masscan_rate = self.config_ini['Masscan'].split('|')[1]
                # 如果用戶在前臺關閉了ICMP存活探測則進行全IP段掃描
                if self.icmp:
                    ip_list = self.get_ac_ip(ip_list) # 默認使用icmp去探測獲得存活主機
                self.masscan_ac[0] = 1  # 可以繼續masscan端口掃描
                # 如果安裝了Masscan即使用Masscan進行全端口掃描
                AC_PORT_LIST = self.masscan(
                    ip_list, masscan_path, masscan_rate)
                if not AC_PORT_LIST:
                    continue
                self.masscan_ac[0] = 0   #不能再次用masscan進行端口掃描
                for ip_str in AC_PORT_LIST.keys():
                    self.queue.put(ip_str)  # ip地址加入隊列
                self.scan_start()  # 開始掃描 開始端口banner獲取和banner比對識別等
            else:
                all_ip_list.extend(ip_list)
                # 不使用masscan時
        if self.mode == 0:
            # 如果啓用存活主機探測功能時,會用icmp echo探測存活的主機ip
            if self.icmp:
                all_ip_list = self.get_ac_ip(all_ip_list)
             # IP地址加入隊列
            for ip_str in all_ip_list:
                self.queue.put(ip_str)  # 加入隊列
            self.scan_start()  # TCP探測模式開始掃描

if self.mode == 1 判斷是否支持masscan掃描,如果支持就使用Masscan進行全端口掃描。如果沒有開啓,將ip添加到all_ip_list這個列表中。
masscan函數
位於nascan/lib/start.py

  def masscan(self, ip, masscan_path, masscan_rate):
        try:
            if len(ip) == 0:
                return
            sys.path.append(sys.path[0] + "/plugin")
            m_scan = __import__("masscan")#動態加載plugin目錄下的masscan
            result = m_scan.run(ip, masscan_path, masscan_rate)
            return result
        except Exception, e:
            print e
            print 'No masscan plugin detected'

動態加載plugin目錄下的masscan.py
masscan.py

def run(ip_list,path,rate):
    try:
        ip_file = open('target.log','w')
        ip_file.write("\n".join(ip_list))
        ip_file.close()
        # 過濾可能導致命令執行的字符,過濾了;|&這三個字符
        path = str(path).translate(None, ';|&`\n')
        rate = str(rate).translate(None, ';|&`\n')
        if not os.path.exists(path):return
        # 將path、rate加到命令後面執行
        os.system("%s -p1-65535 -iL target.log -oL tmp.log --randomize-hosts --rate=%s"%(path,rate))
        result_file = open('tmp.log', 'r')
        result_json = result_file.readlines()
        result_file.close()
        del result_json[0]
        del result_json[-1]
        open_list = {}
        for res in result_json:
            try:
                ip = res.split()[3]
                port = res.split()[2]
                if ip in open_list:
                    open_list[ip].append(port)
                else:
                    open_list[ip] = [port]
            except:pass
        os.remove('target.log')
        os.remove('tmp.log')
        return open_list
    except:
        pass

先過濾可能導致命令執行的字符,過濾了;|&這三個字符;
然後將 將path、rate加到命令後面執行;
將掃描結果保存在tmp.log文件然後讀取裏面的內容;
8、scan_start()函數
不管有沒有使用masscan掃描都會進入scan_start()函數,開始進行掃描;

 def scan_start(self):
        for i in range(self.thread):  # 開始掃描
            t = ThreadNum(self.queue)
            t.setDaemon(True)
            t.mode = self.mode
            t.config_ini = self.config_ini#提供配置信息
            t.statistics = self.statistics#提供統計信息
            t.start()
        self.queue.join()

進入ThreadNum中

class ThreadNum(threading.Thread):
    def __init__(self, queue):
        threading.Thread.__init__(self)
        self.queue = queue

#run()函數,把IP地址和端口號列表傳到另一個scan()函數中
    def run(self):
        while True:
            try:
                # ip地址隊列
                task_host = self.queue.get(block=False)
            except:
                break
            try:
                # 如果使用masscan, 端口就用掃描到的已經開放的端口
                if self.mode:
                    port_list = AC_PORT_LIST[task_host]
                # 沒有使用masscan, 使用默認端口
                else:
                    port_list = self.config_ini['Port_list'].split('|')[
                        1].split('\n')
                # 根據banner識別端口開放的服務
                _s = scan.scan(task_host, port_list)
                _s.config_ini = self.config_ini
                _s.statistics = self.statistics  # 提供統計信# 提供配置信息
                _s.run()
            except Exception, e:
                print e
            finally:
                self.queue.task_done()

_s = scan.scan(task_host, port_list)
這裏ip地址和端口號傳入到另一個scan函數中;
scan()函數
位於/nascan/lib/scan.py

class scan:
    def __init__(self, task_host, port_list):
        self.ip = task_host
        self.port_list = port_list
        self.config_ini = {}

    def run(self):
        self.timeout = int(self.config_ini['Timeout'])
        for _port in self.port_list:
            self.server = ''
            self.banner = ''
            self.port = int(_port)
            # 基礎單端口掃描獲得開放端口banner
            self.scan_port()  # 端口掃描
            if not self.banner:
                continue
            self.server_discern()  #服務識別 使用獲得的banner進行服務類型識別
            # 測試還剩下的一些沒識別出來的端口服務是不是web服務器
            if self.server == '':
                web_info = self.try_web()  # 嘗試web訪問
                if web_info:
                    log.write('web', self.ip, self.port, web_info)
                    time_ = datetime.datetime.now()
                    # Info 集合更新
                    mongo.NA_INFO.update({'ip': self.ip, 'port': self.port},
                                         {"$set": {'banner': self.banner, 'server': 'web', 'webinfo': web_info,
                                                   'time': time_}})

scan的run()函數先進行了端口掃描,然後進入server_discern()函數。

  • self.scan_port() # 端口掃描
 def scan_port(self):
        try:
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            sock.connect((self.ip, self.port))
            time.sleep(0.2)
        except Exception, e:
            return
        try:
            self.banner = sock.recv(1024)
            sock.close()
            if len(self.banner) <= 2:
                self.banner = 'NULL'
        except Exception, e:
            self.banner = 'NULL'
        log.write('portscan', self.ip, self.port, None)
        banner = ''
        hostname = self.ip2hostname(self.ip)
        time_ = datetime.datetime.now()
        date_ = time_.strftime('%Y-%m-%d')
        try:
            banner = unicode(self.banner, errors='replace')
            if self.banner == 'NULL':
                banner = ''
            mongo.NA_INFO.insert({"ip": self.ip, "port": self.port,
                                  "hostname": hostname, "banner": banner, "time": time_})
            self.statistics[date_]['add'] += 1
        except:
            if banner:
                history_info = mongo.NA_INFO.find_and_modify(
                    query={"ip": self.ip, "port": self.port, "banner": {"$ne": banner}}, remove=True)
                if history_info:
                    mongo.NA_INFO.insert(
                        {"ip": self.ip, "port": self.port, "hostname": hostname, "banner": banner, "time": time_})
                    self.statistics[date_]['update'] += 1
                    del history_info["_id"]
                    history_info['del_time'] = time_
                    history_info['type'] = 'update'
                    mongo.NA_HISTORY.insert(history_info)

scan_port()通過socket套接字連接,獲得端口服務返回的banner信息;

  • self.server_discern() #服務識別 使用獲得的banner進行服務類型識別
    進入server_discern()函數
    def server_discern(self):
        for mark_info in self.config_ini['Discern_server']:  # 快速識別
            try:
                # 服務名默認端口識別方法   banner匹配正則表達式
                name, default_port, mode, reg = mark_info
                # 識別模式是default的,只判斷端口號
                if mode == 'default':
                    if int(default_port) == self.port:
                        self.server = name
                # 識別模式是banner的,正則匹配banner
                elif mode == 'banner':
                    matchObj = re.search(reg, self.banner, re.I | re.M)#re.M多行匹配,影響 ^ 和 $;  re.I使匹配對大小寫不敏感;
                    if matchObj:
                        self.server = name
                if self.server:
                    break
            except:
                continue
        # 處理沒識別出來的也不太像(不嚴謹)web的服務
        if not self.server and self.port not in [80, 443, 8080]:
            for mark_info in self.config_ini['Discern_server']:  # 發包識別
                try:
                    name, default_port, mode, reg = mark_info
                    if mode not in ['default', 'banner']:
                        dis_sock = socket.socket(
                            socket.AF_INET, socket.SOCK_STREAM)
                        dis_sock.connect((self.ip, self.port))
                        mode = mode.decode('string_escape')
                        reg = reg.decode('string_escape')
                        dis_sock.send(mode)
                        time.sleep(0.3)
                        dis_recv = dis_sock.recv(1024)
                        dis_sock.close()
                        matchObj = re.search(reg, dis_recv, re.I | re.M)
                        if matchObj:
                            self.server = name
                            break
                except:
                    pass
        if self.server:
            log.write("server", self.ip, self.port, self.server)
            mongo.NA_INFO.update({"ip": self.ip, "port": self.port}, {
                                 "$set": {"server": self.server}})

server_discern()函數,通過正則表達式,依次比較,獲得服務類型;

  • try_web()函數
    def try_web(self):
        title_str, html = '', ''
        try:
            if self.port == 443:
                info = urllib2.urlopen("https://%s:%s" %
                                       (self.ip, self.port), timeout=self.timeout)
            else:
                info = urllib2.urlopen("http://%s:%s" %
                                       (self.ip, self.port), timeout=self.timeout)
            html = info.read()
            header = info.headers
        except urllib2.HTTPError, e:
            html = e.read()
            header = e.headers
        except:
            return
        if not header:
            return
        # 解壓gzip
        if 'Content-Encoding' in header and 'gzip' in header['Content-Encoding']:
            html_data = StringIO.StringIO(html)
            gz = gzip.GzipFile(fileobj=html_data)
            html = gz.read()
        try:
            html_code = self.get_code(header, html).strip()
            if html_code and len(html_code) < 12:
                html = html.decode(html_code).encode('utf-8')
        except:
            pass
        try:
            title = re.search(r'<title>(.*?)</title>', html, flags=re.I | re.M)
            if title:
                title_str = title.group(1)
        except:
            pass
        try:
            web_banner = str(header) + "\r\n\r\n" + html
            self.banner = web_banner
            history_info = mongo.NA_INFO.find_one(
                {"ip": self.ip, "port": self.port})
            if 'server' not in history_info:
                tag = self.get_tag()
                web_info = {'title': title_str, 'tag': tag}
                return web_info
            else:
                if abs(len(history_info['banner'].encode('utf-8')) - len(web_banner)) > len(web_banner) / 60:
                    del history_info['_id']
                    history_info['del_time'] = datetime.datetime.now()
                    mongo.NA_HISTORY.insert(history_info)
                    tag = self.get_tag()
                    web_info = {'title': title_str, 'tag': tag}
                    date_ = datetime.datetime.now().strftime('%Y-%m-%d')
                    self.statistics[date_]['update'] += 1
                    log.write('info', None, 0, '%s:%s update web info' %
                              (self.ip, self.port))
                    return web_info
        except:
            return

nascan代碼大致流程
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章