[原创]K8Cscan for Python 2.0

0x000 简介

K8Cscan扫描器Python版支持Windows和Linux系统

详情参考:https://www.cnblogs.com/k8gege/p/10519321.html

0x001 功能

1.MS17-010漏洞检测

2.SMB协议版本探测

3.存活主机扫描

4.DLL插件扫描(可加载Cscan已发布的DLL,注:该功能仅限Windows且.net版本>=4.0)

PS:由于Cscan功能较多,发现Py可调用.net编写的DLL,偷懒先加载现有插件。

虽然思路一样,但有些功能实现起来不一样,比方其它语言均可ICMP协议实现ping

当然python也可以,但是Py的Socket Raw一定要管理员权限,为了保证任意权限下可用

所以PY版我采用调用系统ping命令这种非常次的方法,来实现内网存活主机的扫描。

没办法我们不是那种可以扛自己的设备去现场接条网线施工的人,打下的权限未必高权限。

很多时候同样的功能由于实现方式不一样,有些工具任意权限下可用,有些仅管理员可用。

0x001 代码

#K8Cscan for python 2.0
#Author: K8gege
#Date: 20190530
#Platform: (Windows & Linux)
#Windows need to .net framework >= 4.0
#Linux not support load ‘netscan40.dll‘ (Maybe Mono is support)
#Usage:
#python K8Cscan.py 192.11.22.40
#python K8Cscan.py 192.11.22.40/24
#python K8Cscan.py 192.11.22.40/24 -t ms17010
#python K8Cscan.py --type=dll 192.11.22.42
#python K8Cscan.py 192.11.22.40/24 -t dll

import platform
import socket
import os
import threading
import time
import telnetlib
import argparse
# import gevent
# from gevent import monkey; monkey.patch_all();
# import socket
# from gevent.pool import Pool 

from mysmb import MYSMB
from impacket import smb, smbconnection, nt_errors
from impacket.uuid import uuidtup_to_bin
from impacket.dcerpc.v5.rpcrt import DCERPCException
from struct import pack
import sys

USERNAME = ‘‘
PASSWORD = ‘‘
NDR64Syntax = (‘71710533-BEBA-4937-8319-B5DBEF9CCC36‘, ‘1.0‘)
MSRPC_UUID_BROWSER  = uuidtup_to_bin((‘6BFFD098-A112-3610-9833-012892020162‘,‘0.0‘))
MSRPC_UUID_SPOOLSS  = uuidtup_to_bin((‘12345678-1234-ABCD-EF00-0123456789AB‘,‘1.0‘))
MSRPC_UUID_NETLOGON = uuidtup_to_bin((‘12345678-1234-ABCD-EF00-01234567CFFB‘,‘1.0‘))
MSRPC_UUID_LSARPC   = uuidtup_to_bin((‘12345778-1234-ABCD-EF00-0123456789AB‘,‘0.0‘))
MSRPC_UUID_SAMR     = uuidtup_to_bin((‘12345778-1234-ABCD-EF00-0123456789AC‘,‘1.0‘))

pipes = {
    ‘browser‘  : MSRPC_UUID_BROWSER,
    ‘spoolss‘  : MSRPC_UUID_SPOOLSS,
    ‘netlogon‘ : MSRPC_UUID_NETLOGON,
    ‘lsarpc‘   : MSRPC_UUID_LSARPC,
    ‘samr‘     : MSRPC_UUID_SAMR,
}

def smbcheck(target):
    if checkPort(target,‘445‘):
        conn = MYSMB(target)
        try:
            conn.login(USERNAME, PASSWORD)
        except smb.SessionError as e:
            #print(‘Login failed: ‘ + nt_errors.ERROR_MESSAGES[e.error_code][0])
            sys.exit()
        finally:
            #print(‘OS: ‘ + conn.get_server_os())
            TragetOS = ‘(‘ + conn.get_server_os()+‘)‘

        tid = conn.tree_connect_andx(‘\\\\‘+target+‘\\‘+‘IPC$‘)
        conn.set_default_tid(tid)

        # test if target is vulnerable
        TRANS_PEEK_NMPIPE = 0x23
        recvPkt = conn.send_trans(pack(‘<H‘, TRANS_PEEK_NMPIPE), maxParameterCount=0xffff, maxDataCount=0x800)
        status = recvPkt.getNTStatus()
        if status == 0xC0000205:  # STATUS_INSUFF_SERVER_RESOURCES
            #print(‘The target is not patched‘)
            CheckResult = ‘MS17-010\t‘+TragetOS

        return CheckResult

def GetSmbVul(ip):
    # output = os.popen(‘ping -%s 1 %s‘%(ptype,ip)).readlines()
    # for w in output:
        # if str(w).upper().find(‘TTL‘)>=0:
            #print "online "+ip
    try:
        SmbVul=smbcheck(ip)
        if SmbVul<>None:
            print(‘%s\t%s‘%(ip,SmbVul))
    except:
        pass
def GetOSname(ip):
    try:
        print(‘%s\t%s\t%s‘%(ip,getHostName(ip)))
    except:
        pass
def ScanSmbVul(ip):
    if ‘/24‘ in ip:
        ipc = (ip.split(‘.‘)[:-1])
        for i in range(1,256):
            add = (‘.‘.join(ipc)+‘.‘+str(i))
            threading._start_new_thread(GetSmbVul,(add,))
            time.sleep(0.1)
    else:
        GetSmbVul(ip)

def checkPort(ip,port):
    server = telnetlib.Telnet()
    try:
        server.open(ip,port)
        #print(‘{0} port {1} is open‘.format(ip, port))
        return True
    except Exception as err:
        #print(‘{0} port {1} is not open‘.format(ip,port))
        return False
    finally:
        server.close()

def getHostName(target):
    try:
      result = socket.gethostbyaddr(target)
      return result[0]
    except socket.herror, e:
      return ‘‘
def getos():
    return platform.system()

try:
    import clr
except:
    pass

def netscan(ip):
	try:
		clr.FindAssembly(‘netscan40.dll‘)
		clr.AddReference(‘netscan40‘)
		from CscanDLL import scan
		print(scan.run(ip)),
	except:
		pass

def Cscan(ip):
    if ‘/24‘ in ip:
        ipc = (ip.split(‘.‘)[:-1])
        for i in range(1,256):
            add = (‘.‘.join(ipc)+‘.‘+str(i))
            threading._start_new_thread(netscan,(add,))
            # if type==‘dll‘:
                # threading._start_new_thread(netscan,(add,))
            # elif type==‘smb‘:
                # threading._start_new_thread(netscan,(add,))
            time.sleep(0.1)
    else:
        netscan(ip)

def CscanSMBver(ip):
    if ‘/24‘ in ip:
        ipc = (ip.split(‘.‘)[:-1])
        for i in range(1,256):
            add = (‘.‘.join(ipc)+‘.‘+str(i))
            threading._start_new_thread(smbVersion,(add,))
            time.sleep(0.1)
    else:
        smbVersion(ip)
def CscanOSname(ip):
    if ‘/24‘ in ip:
        ipc = (ip.split(‘.‘)[:-1])
        for i in range(1,256):
            add = (‘.‘.join(ipc)+‘.‘+str(i))
            threading._start_new_thread(GetOSname,(add,))
            time.sleep(0.1)
    else:
        GetOSname(ip)

def getip():
    return socket.gethostbyname(socket.gethostname())

def pingIP(ip):
    #print ip
    #gevent.sleep(0)
    output = os.popen(‘ping -%s 1 %s‘%(ptype,ip)).readlines()
    for w in output:
        if str(w).upper().find(‘TTL‘)>=0:
            print ip
            # try:
                # SmbVul=smbcheck(ip)
                # if SmbVul==None:
                    # print(‘%s\t%s‘%(ip,getHostName(ip)))
                # else:
                    # print(‘%s\t%s\t%s‘%(ip,getHostName(ip),SmbVul))
            # except:
                # pass

def CpingIP(ip):
    ip=ipc+str(ip)
    pingIP(ip)

def Cping(ip):
    if ‘/24‘ in ip:
        ipc = (ip.split(‘.‘)[:-1])
        for i in range(1,256):
            add = (‘.‘.join(ipc)+‘.‘+str(i))
            threading._start_new_thread(pingIP,(add,))
            time.sleep(0.1)

        #ipcc = scanip.split(‘.‘)[:-1]
        #ipccc = (‘.‘.join(ipcc)+‘.‘)
        #global ipc
        #ipc = ipccc
        #ipc = scanip.split(‘.‘)[:-1]
        #print "ipc: "+ipc

        # pool = Pool(255)
        # pool.map(CpingIP,xrange(1,254))
        # pool.join()
    else:
        pingIP(ip)
def PrintLine():
    print(‘=============================================‘)

from impacket.smbconnection import *
from impacket.nmb import NetBIOSError
import errno

def smbVersion(rhost):
    host = rhost
    port=445
    try:
        smb = SMBConnection(host, host, sess_port=port)
    except NetBIOSError:
        return
    except socket.error, v:
        error_code = v[0]
        if error_code == errno.ECONNREFUSED:
            return
        else:
            return
    dialect = smb.getDialect()
    if dialect == SMB_DIALECT:
        print(host + "\tSMBv1 ")
    elif dialect == SMB2_DIALECT_002:
        print(host + "\tSMBv2.0 ")
    elif dialect == SMB2_DIALECT_21:
        print(host + "\tSMBv2.1 ")
    else:
        print(host + "\tSMBv3.0 ")

ipc=""
if __name__ == ‘__main__‘:

    print(‘K8Cscan 2.0 by k8gege‘)
    parser = argparse.ArgumentParser()
    parser.add_argument(‘ip‘,help=‘IP or IP/24‘)
    parser.add_argument(‘--type‘, ‘-t‘, type=str, choices=[‘ping‘, ‘smbver‘, ‘osname‘,‘ms17010‘,‘dll‘], help=‘Scan Type‘,default=‘ping‘)
    args = parser.parse_args()
    if getos() == ‘Windows‘:
        ptype = ‘n‘
    elif getos() == ‘Linux‘:
        ptype = ‘c‘
    else:
        print(‘The system is not supported.‘)
        sys.exit()
    scanip=args.ip
    if args.type == ‘ping‘:
        print "Scan Online"
        PrintLine()
        Cping(scanip)
    elif args.type == ‘smbver‘:
        print "Scan Smb Version "
        CscanSMBver(scanip)
    elif args.type == ‘dll‘:
        if ptype ==‘n‘:
            if(os.path.exists(‘netscan40.dll‘)):
                print(‘load netscan40.dll (.net >= 4.0)‘)
                PrintLine()
                Cscan(scanip)
            else:
                print(‘load netscan40.dll‘)
        else:
            print(‘The system is not supported.‘)
        sys.exit(1)
    elif args.type == ‘ms17010‘:
        print "Scan MS17-010 VUL \n" + scanip
        ScanSmbVul(scanip)
    elif args.type == ‘osname‘:
        print "Scan hostName \n" + scanip
        CscanOSname(scanip)
    PrintLine()
    print(‘Scan Finished!‘)

0x003 用法

例子1: SMB漏洞之MS17-010扫描

例子2:DLL扫描之Cscan操作系统探测插件

根目录下放osscan.rar中的netscan40.dll即可(其它dll同理,windows下不建议python版)PY受好者请继续

如图所示探测到内网Win7三台、Win10两台、2012一台及主机名、路由一台(开放WEB服务,产品版本等)

例子3:SMB版本探测(可用于判断操作系统,因Linux默认不开启)

SMB版本对应操作系统(可用于判断操作系统,因Linux默认不开启)

Scan Smb Version
192.11.22.118   SMBv3.0   Win8.1
192.11.22.7       SMBv3.0   Win10
192.11.22.41     SMBv2.1   (Win7)

SMBv1   XP or 2003

Kali开启SMB后
192.11.22.54    SMBv3.0
192.11.22.54    KALI            [Win 6.1]
抓包显示为 Windows Server 2003 3790 Service Pack 2

例子4:Kali下使用

 0x004 下载

https://github.com/k8gege/pyCscan/blob/master/K8Cscan.py

https://github.com/k8gege/K8CScan/blob/master/K8Cscan.py

https://github.com/k8gege/K8tools/blob/master/K8Cscan.py

原文地址:https://www.cnblogs.com/k8gege/p/11160718.html

时间: 2024-10-14 12:01:39

[原创]K8Cscan for Python 2.0的相关文章

HOWTO Use Python in the web — Python v3.0.1 documentation

HOWTO Use Python in the web - Python v3.0.1 documentation mod_python? People coming from PHP often find it hard to grasp how to use Python in the web. Their first thought is mostly mod_python because they think that this is the equivalent to mod_php.

Python.Flask.0

1. 吐槽 Python Web 框架 Flask https://blog.tonyseek.com/post/discuss-about-flask-framework/ 2. How To Structure Large Flask Applications https://www.digitalocean.com/community/tutorials/how-to-structure-large-flask-applications Python.Flask.0,布布扣,bubuko.

Python pandas 0.19.1 Indexing and Selecting Data文档翻译

最近在写个性化推荐的论文,经常用到Python来处理数据,被pandas和numpy中的数据选取和索引问题绕的比较迷糊,索性把这篇官方文档翻译出来,方便自查和学习,翻译过程中难免很多不到位的地方,但大致能看懂,错误之处欢迎指正~ Python pandas 0.19.1 Indexing and Selecting Data 原文链接 http://pandas.pydata.org/pandas-docs/stable/indexing.html 数据索引和选取 pandas对象中的轴标签信息

python 2.4 与 python 3.0 的比较

转过来,留着日后查看 [转自:]http://hi.baidu.com/autoitcn/blog/item/5f41973294b5fc4fac4b5f77.html 一. print 从语句变为函数 原:     print   1, 2+3 改为: print ( 1, 2+3 ) 二.range 与 xrange 原 : range( 0, 4 )   结果 是 列表 [0,1,2,3 ] 改为:list( range(0,4) ) 原 : xrange( 0, 4 )    适用于 f

Python人工智能0基础到精通基础班+就业班视频教程

38套大数据,云计算,架构,数据分析师,Hadoop,Spark,Storm,Kafka,人工智能,机器学习,深度学习,项目实战视频教程 视频课程包含: 38套大数据和人工智能精品高级课包含:大数据,云计算,架构,数据挖掘实战,实时推荐系统实战,电视收视率项目实战,实时流统计项目实战,离线电商分析项目实战,Spark大型项目实战用户分析,智能客户系统项目实战,Linux基础,Hadoop,Spark,Storm,Docker,Mapreduce,Kafka,Flume,OpenStack,Hiv

(转)从Python的0.1输出0.1000000000000001说浮点数的二进制

原文地址:http://blog.csdn.net/u012843100/article/details/60885763 今天在学习Python核心编程的时候,十进制浮点数那段看到一个有趣的事情. >>>0.1 0.1000000000000001 为什么会这样?文中是这样解释的: 这是因为语言绝大多数C语言的双精度实现都遵守IEEE754规范,其中52位用于底.因此浮点值只能有52位精度,类似这样的值用二进制表示只能像上面那样被截断.0.1的二进制表示是0.1100110011001

python 2.0 s12 day5

模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需要多个函数才能完成(函数又可以在不同的.py文件中),n个 .py 文件组成的代码集合就称为模块. 如:os 是系统相关的模块:file是文件操作相关的模块 模块分为三种: 自定义模块 内置模块 开源模块 自定义模块 1.定义模块 情景一: 情景二: 情景三: 2.导入模块 Python之所以应用越来越广泛,在

关于python cv2-1.0(ImportError: No module named cv2)

今天看到python 版本的opencv想试一下: 想装个cv2,就直接打了pip install cv2 安装成功. 但是进入python却提示找不到cv2这个模块: ImportError: No module named cv2 折显然不符合常理. 在国外网站上找到个: cv2-1.0 is a spurious, garbage module with nothing in it. See for yourself: https://pypi.python.org/pypi/cv2/1.

【原创】用Python爬取LeetCode的AC代码到Github

在leetCode写了105道题高调膜科,考虑搬迁到自己的GitHub上,做成一个解题题库,面试的时候也可以秀一个 但是!但是! leetCode在线IDE的功能不要太舒服,我直接线上A了不少题,本地没有代码,除非有题调试半天A不来,本地才有存代码 于是我就考虑,直接用Python把leetCode上的AC代码爬下来,然后扔到本地github文件夹里,然后一个同步大法 大概涉及的知识: 0.cookie 1.网站的结构分析 2.脚本登陆 3.脚本爬站 ----------------------