python脚本解析masscan扫描结果

masscan简介

  • 开源、免费的端口扫描器,获取主机开放的端口和端口信息。
  • 速度非常快,6分钟可以扫描整个互联网,1台机器每秒可传输1000万个数据包。
  • 只做SYN扫描、不首先ping主机,没有DNS解析发生,扫描完全随机化

masscan安装

sudo apt-get install git gcc make libpcap-dev
git clone https://github.com/robertdavidgraham/masscan
cd masscan
make

masscan使用方法1

./masscan 172.16.0.0/16 -p0-65535 --max-rate 200000 --banners -oJ result.json
  • -p 扫描所有端口指定
  • --banners 获取banner值
  • --max-rate 每秒发的包数量,根据机器和网络情况自行选择
  • -oJ result.json 将扫描结果输出为json文件


    python脚本解析masscan扫描结果_第1张图片
    image.png

masscan使用方法2

./masscan -p0-65535 -iL ip.txt --max-rate 100000 --banners -oJ f.json
  • -iL 扫描文件中的ip地址


    image.png

    image.png

python脚本解析masscan结果

import json
from openpyxl import Workbook
import xlsxwriter
import socket
def get_list(filepath):
    f = open(filepath,encoding='utf-8')
    c = json.load(f)
    list = []
    for i in c:
        ip = i['ip']
        port = str(i['ports'][0]['port'])
        status = 'open'
        try:
            if i['ports'][0]['service']:
                name = i['ports'][0]['service']['name']
                banner = str(i['ports'][0]['service']['banner'])
        except:
            name = ''
            banner = ''
        line = [ip,port,status,name,banner]
        list.append(line)
    return list
def quchong(l1):
    l2 =[]
    for data1 in l1:
        for data2 in l1:
            if data1[0]==data2[0] and data1[1]==data2[1]:
                if data1[3] ==''and data2[3] !='':
                    # print(data1,data2)
                    l2.append(data1)
    for i in l2:
        try:
            l1.remove(i)
        except:pass
    l1 = [list(t) for t in set(tuple(_) for _ in l1)]
    return l1

def write_excle(list):
    f = xlsxwriter.Workbook('port.xlsx')
    worksheet1 = f.add_worksheet('扫描信息')
    worksheet2 = f.add_worksheet('主机ip列表')
    worksheet1.write(0, 0, 'ip')
    worksheet1.write(0, 1, '端口')
    worksheet1.write(0, 2, '状态')
    worksheet1.write(0, 3, '服务')
    worksheet1.write(0, 4, 'banner')
    worksheet2.write(0, 0, '主机ip')
    newlist= []
    for i in list:
        newlist.append(i[0])
    newlist=set(newlist)
    total1 = 0
    total2 = len(newlist)
    newlist=sorted(newlist, key=socket.inet_aton)
    for index, p in enumerate(list):
        total1+=1
        for j, q in enumerate(p):
            worksheet1.write(index + 1, j, q)
    for index, p in enumerate(newlist):
        worksheet2.write(index + 1, 0, p)
    f.close()
    return total1,total2
if __name__ == '__main__':
    filepath = 'C:/1/result.json'  #填写要解析masscan扫描json格式报告的文件路径
    result = get_list(filepath)
    result = quchong(result)
    sum = write_excle(result)
    print('共检测到存活主机%d个,端口信息%d条'% (sum[1],sum[0]))

C:\Users\m\PycharmProjects\untitled\venv\Scripts\python.exe C:/Users/m/PycharmProjects/untitled/venv/解析masscanjson脚本.py
共检测到存活主机1782个,端口信息17058条
  • excle结果如下图


    python脚本解析masscan扫描结果_第2张图片
    image.png

你可能感兴趣的:(python脚本解析masscan扫描结果)