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笔记(2)
Oct 24 Python
python简单实现基于SSL的IRC bot实例
Jun 15 Python
如何准确判断请求是搜索引擎爬虫(蜘蛛)发出的请求
Oct 13 Python
Python性能提升之延迟初始化
Dec 04 Python
Python中selenium实现文件上传所有方法整理总结
Apr 01 Python
python脚本作为Windows服务启动代码详解
Feb 11 Python
解决python 未发现数据源名称并且未指定默认驱动程序的问题
Dec 07 Python
pyqt5实现按钮添加背景图片以及背景图片的切换方法
Jun 13 Python
django使用admin站点上传图片的实例
Jul 28 Python
Django 在iframe里跳转顶层url的例子
Aug 21 Python
Python OpenCV之常用滤波器使用详解
Apr 07 Python
Python安装使用Scrapy框架
Apr 12 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实现递归循环每一个目录
2010/08/08 PHP
ThinkPHP3.1基础知识快速入门
2014/06/19 PHP
PHP strtotime函数用法、实现原理和源码分析
2015/02/04 PHP
64位windows系统下安装Memcache缓存
2015/12/06 PHP
php插件Xajax使用方法详解
2017/08/31 PHP
在云虚拟主机部署thinkphp5项目的步骤详解
2017/12/21 PHP
PHP与Web页面的交互示例详解一
2020/08/04 PHP
中国地区三级联动下拉菜单效果分析
2012/11/15 Javascript
js实现一个省市区三级联动选择框代码分享
2013/03/06 Javascript
JavaScript获取onclick、onchange等事件值的代码
2013/07/22 Javascript
检查输入的是否是数字使用keyCode配合onkeypress事件
2014/01/23 Javascript
nodejs获取本机内网和外网ip地址的实现代码
2014/06/01 NodeJs
深入理解JavaScript系列(22):S.O.L.I.D五大原则之依赖倒置原则DIP详解
2015/03/05 Javascript
Jquery ajax请求导出Excel表格的实现代码
2016/06/08 Javascript
微信小程序 轮播图swiper详解及实例(源码下载)
2017/01/11 Javascript
微信小程序 空白页重定向解决办法
2017/06/27 Javascript
从组件封装看Vue的作用域插槽的实现
2019/02/12 Javascript
利用Vue实现一个markdown编辑器实例代码
2019/05/19 Javascript
jquery实现购物车基本功能
2019/10/25 jQuery
关于vue2强制刷新,解决页面不会重新渲染的问题
2019/10/29 Javascript
微信小程序实现音乐播放器
2019/11/20 Javascript
在VUE style中使用data中的变量的方法
2020/06/19 Javascript
在vant 中使用cell组件 定义图标该图片和位置操作
2020/11/02 Javascript
探究python中open函数的使用
2016/03/01 Python
Python中with及contextlib的用法详解
2017/06/08 Python
Django rest framework实现分页的示例
2018/05/24 Python
Python3标准库之functools管理函数的工具详解
2020/02/27 Python
关于python 的legend图例,参数使用说明
2020/04/17 Python
Django 用户登陆访问限制实例 @login_required
2020/05/13 Python
PyTorch中model.zero_grad()和optimizer.zero_grad()用法
2020/06/24 Python
发现世界上最好的珠宝设计师:JewelStreet
2017/12/17 全球购物
班组长岗位职责范本
2014/01/05 职场文书
大学生关于奋斗的演讲稿
2014/01/09 职场文书
户籍证明的格式
2014/01/13 职场文书
旷课检讨书
2015/01/26 职场文书
分家协议书范本
2016/03/22 职场文书