Python实现远程调用MetaSploit的方法


Posted in Python onAugust 22, 2014

本文较为详细的讲述了Python实现远程调用MetaSploit的方法,对Python的学习来说有很好的参考价值。具体实现方法如下:

(1)安装Python的msgpack类库,MSF官方文档中的数据序列化标准就是参照msgpack。

root@kali:~# apt-get install python-setuptools
root@kali:~# easy_install msgpack-python

 
(2)创建createdb_sql.txt:

create database msf;
create user msf with password 'msf123';
grant all privileges on database msf to msf;

 
(3)在PostgreSQL 执行上述文件:

root@kali:~# /etc/init.d/postgresql start
root@kali:~# sudo -u postgres /usr/bin/psql < createdb_sql.txt

 
(4)创建setup.rc文件

db_connect msf:msf123@127.0.0.1/msf
load msgrpc User=msf Pass='abc123'

 
(5)启动MSF并执行载入文件

root@kali:~# msfconsole -r setup.rc
* SNIP *
[*] Processing setup.rc for ERB directives.
resource (setup.rc)> db_connect msf:msf123@127.0.0.1/msf
[*] Rebuilding the module cache in the background...
resource (setup.rc)> load msgrpc User=msf Pass='abc123'
[*] MSGRPC Service: 127.0.0.1:55552
[*] MSGRPC Username: msf
[*] MSGRPC Password: abc123
[*] Successfully loaded plugin: msgrpc

 
(6)Github上有一个Python的类库,不过很不好用

root@kali:~# git clone git://github.com/SpiderLabs/msfrpc.git msfrpc
root@kali:~# cd msfrpc/python-msfrpc
root@kali:~# python setup.py install

测试代码如下:

#!/usr/bin/env python
import msgpack
import httplib
 
class Msfrpc:
 class MsfError(Exception):
  def __init__(self,msg):
   self.msg = msg
  def __str__(self):
   return repr(self.msg)
 
 class MsfAuthError(MsfError):
  def __init__(self,msg):
   self.msg = msg
  
 def __init__(self,opts=[]):
  self.host = opts.get('host') or "127.0.0.1"
  self.port = opts.get('port') or 55552
  self.uri = opts.get('uri') or "/api/"
  self.ssl = opts.get('ssl') or False
  self.authenticated = False
  self.token = False
  self.headers = {"Content-type" : "binary/message-pack" }
  if self.ssl:
   self.client = httplib.HTTPSConnection(self.host,self.port)
  else:
   self.client = httplib.HTTPConnection(self.host,self.port)
 
 def encode(self,data):
  return msgpack.packb(data)
 def decode(self,data):
  return msgpack.unpackb(data)
 
 def call(self,meth,opts = []):
  if meth != "auth.login":
   if not self.authenticated:
    raise self.MsfAuthError("MsfRPC: Not Authenticated")
 
  if meth != "auth.login":
   opts.insert(0,self.token)
 
  opts.insert(0,meth)
  params = self.encode(opts)
  self.client.request("POST",self.uri,params,self.headers)
  resp = self.client.getresponse()
  return self.decode(resp.read()) 
 
 def login(self,user,password):
  ret = self.call('auth.login',[user,password])
  if ret.get('result') == 'success':
self.authenticated = True
    self.token = ret.get('token')
    return True
  else:
    raise self.MsfAuthError("MsfRPC: Authentication failed")
 
if __name__ == '__main__':
 
 # Create a new instance of the Msfrpc client with the default options
 client = Msfrpc({})
 
 # Login to the msfmsg server using the password "abc123"
 client.login('msf','abc123')
 
 # Get a list of the exploits from the server
 mod = client.call('module.exploits')
 
 # Grab the first item from the modules value of the returned dict
 print "Compatible payloads for : %s\n" % mod['modules'][0]
 
 # Get the list of compatible payloads for the first option
 ret = client.call('module.compatible_payloads',[mod['modules'][0]])
 for i in (ret.get('payloads')):
  print "\t%s" % i

相信本文所述方法对大家的Python学习可以起到一定的学习借鉴作用。

Python 相关文章推荐
Python自定义函数的创建、调用和函数的参数详解
Mar 11 Python
Python json模块使用实例
Apr 11 Python
Python中的探索性数据分析(功能式)
Dec 22 Python
在python中使用with打开多个文件的方法
Jan 07 Python
Python进阶:生成器 懒人版本的迭代器详解
Jun 29 Python
Python安装whl文件过程图解
Feb 18 Python
python GUI库图形界面开发之PyQt5信号与槽的高级使用技巧(自定义信号与槽)详解与实例
Mar 06 Python
python实现引用其他路径包里面的模块
Mar 09 Python
jupyter notebook 添加kernel permission denied的操作
Apr 21 Python
Python内置函数及功能简介汇总
Oct 13 Python
Python获取android设备cpu和内存占用情况
Nov 15 Python
使用Python封装excel操作指南
Jan 29 Python
Python解释执行原理分析
Aug 22 #Python
Python实现的石头剪子布代码分享
Aug 22 #Python
Python使用MD5加密字符串示例
Aug 22 #Python
Python中让MySQL查询结果返回字典类型的方法
Aug 22 #Python
Python安装Imaging报错:The _imaging C module is not installed问题解决方法
Aug 22 #Python
Python with的用法
Aug 22 #Python
Tornado服务器中绑定域名、虚拟主机的方法
Aug 22 #Python
You might like
用PHP实现的四则运算表达式计算实现代码
2011/08/02 PHP
php使用substr()和strpos()联合查找字符串中某一特定字符的方法
2015/05/12 PHP
yii2局部关闭(开启)csrf的验证的实例代码
2017/07/10 PHP
js 覆盖和重载 函数
2009/09/25 Javascript
判断目标是否是window,document,和拥有tagName的Element的代码
2010/05/31 Javascript
使用jQuery不判断浏览器高度解决iframe自适应高度问题
2014/12/16 Javascript
js实现鼠标划过给div加透明度的方法
2015/05/25 Javascript
浅谈JSON.parse()和JSON.stringify()
2015/07/14 Javascript
js实现页面a向页面b传参的方法
2016/05/29 Javascript
js以及jquery实现手风琴效果
2020/04/17 Javascript
js封装成插件_Canvas统计图插件编写实例
2017/09/12 Javascript
Bootstrap Table中的多选框删除功能
2018/07/15 Javascript
VUE-cli3使用 svg-sprite-loader
2018/10/20 Javascript
JavaScript单线程和任务队列原理解析
2020/02/04 Javascript
[03:42]2014DOTA2西雅图国际邀请赛7月9日TOPPLAY
2014/07/09 DOTA
[01:14:05]《加油DOTA》第四期
2014/08/25 DOTA
python str与repr的区别
2013/03/23 Python
python批量提交沙箱问题实例
2014/10/08 Python
django 创建过滤器的实例详解
2017/08/14 Python
python使用生成器实现可迭代对象
2018/03/20 Python
pandas数据清洗,排序,索引设置,数据选取方法
2018/05/18 Python
解决python3捕获cx_oracle抛出的异常错误问题
2018/10/18 Python
解决python3运行selenium下HTMLTestRunner报错的问题
2018/12/27 Python
python itchat给指定联系人发消息的方法
2019/06/11 Python
Python字符串和正则表达式中的反斜杠('\')问题详解
2019/09/03 Python
Python操作Sqlite正确实现方法解析
2020/02/05 Python
美国CVS药店官网:CVS Pharmacy
2018/07/26 全球购物
日本亚马逊官方网站:Amazon.co.jp
2020/04/14 全球购物
装饰资料员岗位职责
2013/12/30 职场文书
2014年党务公开实施方案
2014/02/27 职场文书
教师评职称工作总结2015
2015/04/20 职场文书
社区青年志愿者活动总结
2015/05/06 职场文书
python实现ROA算子边缘检测算法
2021/04/05 Python
Vue过滤器(filter)实现及应用场景详解
2021/06/15 Vue.js
如何使用SQL Server语句创建表
2022/04/12 SQL Server
Python中的matplotlib绘制百分比堆叠柱状图,并为每一个类别设置不同的填充图案
2022/04/20 Python