内网资产自动收集

这是一个使用Python编写的内网资产自动收集脚本,它能获取主机信息、网络信息,包括IP地址、ARP列表、网络段等,并进行端口扫描。通过执行不同命令收集主机的用户列表、在线用户、系统信息和服务状态。此外,还提供了端口扫描功能,扫描指定IP范围内的主机开放端口,并将结果存储到数据库。
摘要由CSDN通过智能技术生成
#!/usr/bin/python
# -*- coding:utf-8 -*-
import os
import getpass
import time
import socket
import re

import threading
from Queue import Queue
import platform
import types
from subprocess import Popen, PIPE


import struct
import sys
from db import intodb

__all__ = ['Thread', 'Threadpool', 'OK', 'FINISH', 'ERROR']

OK = 0x0
FINISH = 0x1
ERROR = 0x2


class Thread(threading.Thread):
    def __init__(self, worker, task_queue, msg_queue, threadpool):
        super(Thread, self).__init__()
        self.worker = worker
        self.task_queue = task_queue
        self.threadpool = threadpool
        self.msg_queue = msg_queue

    def run(self):
        count = 0
        while True:
            self.threadpool.event.wait()
            if self.task_queue.empty():
                self.threadpool.InActiveOne()
                break
            task = self.task_queue.get()
            try:
                ret = self.worker(task)
                self.msg_queue.put((task, ret))
                if (not ret) and (ret[0] == FINISH):
                    self.threadpool.clearQueue()
            except Exception as e:
                self.msg_queue.put((task, ERROR))
            finally:
                self.task_queue.task_done()


class Threadpool(object):
    def __init__(self, worker, max_threads=10, thread=Thread,
                 queue=Queue, lock=threading.RLock()):
        self.worker = worker
        self.thread = thread
        self.event = threading.Event()
        self.lock = lock
        self.task_queue = queue()
        self.msg_queue = queue()
        self.max_threads = max_threads
        self.active_threads = 0

        self.start()

    def add(self, tasks):
        for task in tasks:
            self.task_queue.put(task)
        len_tasks = self.task_queue.qsize()

        self.lock.acquire()
        create_tasks = self.max_threads - self.active_threads
        if len_tasks < create_tasks:
            create_tasks = len_tasks
        for i in xrange(create_tasks):
            self.ActiveOne()
        self.lock.release()

    def ActiveOne(self):
        self.lock.acquire()
        t = self.thread(self.worker, self.task_queue, self.msg_queue, self)
        t.setDaemon(True)
        t.start()
        self.active_threads += 1
        self.lock.release()

    def InActiveOne(self):
        self.lock.acquire()
        self.active_threads -= 1
        self.lock.release()

    def status(self):
        return self.task_queue.qsize(), self.active_threads

    def join(self):
        self.task_queue.join()

    def printmsg(self):
        pass

    def clearQueue(self):
        self.stop()
        while True:
            if self.task_queue.empty():
                break
            self.task_queue.get()
            self.task_queue.task_done()
        self.start()

    def start(self):
        self.event.set()

    def stop(self):
        self.event.clear()


class InScaner:
    def __init__(self, domain):
        self.NUM = 200
        self._re_IP = r'\d+\.\d+\.\d+\.\d+'
        self._re_startwithIP = r'^\d+\.\d+\.\d+\.\d+.*'
        self._re_network = r'^\d+\.\d+\.\d+'
        self.re_ip = re.compile(self._re_IP)
        self.re_startwithIP = re.compile(self._re_startwithIP)
        self.re_network = re.compile(self._re_network)
        self.host_ip = socket.gethostbyname(socket.gethostname())
        self.domain = domain
        self.path = os.getcwd()
        self.host_hostname = ''  # os.popen('hostnam
#网络资产信息扫描 在渗透测试(特别是内网)中经常需要对目标进行网络资产收集,即对方服务器都有哪些IP,IP上开了哪些端口,端口上运行着哪些服务,此脚本即为实现此过程,相比其他探测脚本有以下优点:1、轻巧简洁,只需python环境,无需安装额外外库。2、扫描完成后生成独立页面报告。 此脚本的大概流程为 ICMP存活探测-->端口开放探测-->端口指纹服务识别-->提取快照(若为WEB)-->生成结果报表 运行环境:python 2.6 + 参数说明 -h 必须输入的参数,支持ip(192.168.1.1),ip段(192.168.1),ip范围指定(192.168.1.1-192.168.1.254),ip列表文件(ip.ini),最多限制一次可扫描65535个IP。 -p 指定要扫描端口列表,多个端口使用,隔开 例如:22,23,80,3306。未指定即使用内置默认端口进行扫描(21,22,23,25,53,80,110,139,143,389,443,445,465,873,993,995,1080,1723,1433,1521,3306,3389,3690,5432,5800,5900,6379,7001,8000,8001,8080,8081,8888,9200,9300,9080,9999,11211,27017) -m 指定线程数量 默认100线程 -t 指定HTTP请求超时时间,默认为10秒,端口扫描超时为值的1/2。 -n 不进行存活探测(ICMP)直接进行扫描。 结果报告保存在当前目录(扫描IP-时间戳.html)。 例子: python NAScan.py -h 10.111.1 python NAScan.py -h 192.168.1.1-192.168.2.111 python NAScan.py -h 10.111.1.22 -p 80,7001,8080 -m 200 -t 6 python NAScan.py -h ip.ini -p port.ini -n 服务识别在server_info.ini文件中配置 格式为:服务名|默认端口|正则 例 ftp|21|^220.*?ftp|^220- 正则为空时则使用端口进行匹配,否则以正则匹配结果为准。 项目地址 https://github.com/ywolf/ 欢迎大家反馈建议和BUG
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值