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 自动刷博客浏览量实例代码
Jun 14 Python
Python实现破解猜数游戏算法示例
Sep 25 Python
python 接口返回的json字符串实例
Mar 27 Python
对命令行模式与python交互模式介绍
May 12 Python
Tensorflow 同时载入多个模型的实例讲解
Jul 27 Python
Numpy 改变数组维度的几种方法小结
Aug 02 Python
Python wxPython库消息对话框MessageDialog用法示例
Sep 03 Python
python celery分布式任务队列的使用详解
Jul 08 Python
pytorch 批次遍历数据集打印数据的例子
Dec 30 Python
python实现飞船游戏的纵向移动
Apr 24 Python
python编写一个会算账的脚本的示例代码
Jun 02 Python
Python使用文件操作实现一个XX信息管理系统的示例
Jul 02 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代码收集表单内容并写入文件的代码
2012/01/29 PHP
PHP中使用匿名函数操作数据库的例子
2014/11/17 PHP
分析 JavaScript 中令人困惑的变量赋值
2007/08/13 Javascript
javascript操作cookie的文章(设置,删除cookies)
2010/04/01 Javascript
jquery下为Event handler传递动态参数的代码
2011/01/06 Javascript
jQuery学习笔记 操作jQuery对象 属性处理
2012/09/19 Javascript
jQuery基本过滤选择器使用介绍
2013/04/18 Javascript
javascript操作table(insertRow,deleteRow,insertCell,deleteCell方法详解)
2013/12/16 Javascript
JS取request值以及自动执行使用示例
2014/02/24 Javascript
Js+Jq获取URL参数的集中方法示例代码
2014/05/20 Javascript
Javascript获取数组中的最大值和最小值的方法汇总
2016/01/01 Javascript
浅谈JS中json数据的处理
2016/06/30 Javascript
vue获取DOM元素并设置属性的两种实现方法
2017/09/30 Javascript
AngularJS使用Filter自定义过滤器控制ng-repeat去除重复功能示例
2018/04/21 Javascript
vue富文本编辑器组件vue-quill-edit使用教程
2018/09/21 Javascript
Three.JS实现三维场景
2018/12/30 Javascript
vue使用keep-alive保持滚动条位置的实现方法
2019/04/09 Javascript
javascript实现简易聊天室
2019/07/12 Javascript
vue项目中使用vue-layer弹框插件的方法
2020/03/11 Javascript
vue实现桌面向网页拖动文件的示例代码(可显示图片/音频/视频)
2021/03/01 Vue.js
[53:52]OG vs EG 2018国际邀请赛淘汰赛BO3 第二场 8.23
2018/08/24 DOTA
python使用pil生成缩略图的方法
2015/03/26 Python
优化Python代码使其加快作用域内的查找
2015/03/30 Python
浅谈Python程序与C++程序的联合使用
2015/04/07 Python
Python中使用asyncio 封装文件读写
2016/09/11 Python
Python2.7版os.path.isdir中文路径返回false的解决方法
2019/06/21 Python
python字符串替换第一个字符串的方法
2019/06/26 Python
Python无头爬虫下载文件的实现
2020/04/02 Python
浅谈CSS3特性查询(Feature Query: @supports)功能简介
2017/07/31 HTML / CSS
大学生求职推荐信
2013/11/27 职场文书
2014入党积极分子批评与自我批评思想报告
2014/10/06 职场文书
酒店办公室主任岗位职责
2015/04/01 职场文书
公司开会通知
2015/04/20 职场文书
赢在执行观后感
2015/06/16 职场文书
认识实习感想
2015/08/10 职场文书
python面向对象版学生信息管理系统
2021/06/24 Python