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中用PIL库批量给图片加上序号的教程
May 06 Python
win10环境下python3.5安装步骤图文教程
Feb 03 Python
Python抓取框架Scrapy爬虫入门:页面提取
Dec 01 Python
Python使用Django实现博客系统完整版
Sep 29 Python
pandas数据框,统计某列数据对应的个数方法
Apr 11 Python
selenium+python实现自动登录脚本
Apr 22 Python
Python爬虫实现验证码登录代码实例
May 10 Python
解决py2exe打包后,总是多显示一个DOS黑色窗口的问题
Jun 21 Python
Python检测端口IP字符串是否合法
Jun 05 Python
python实例化对象的具体方法
Jun 17 Python
Python如何获取文件路径/目录
Sep 22 Python
Python实现8种常用抽样方法
Jun 27 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与MySQL交互使用详解
2006/10/09 PHP
zen cart新进商品的随机排序修改方法
2010/09/10 PHP
PHP使用 Imagick 扩展实现图片合成,圆角处理功能示例
2019/09/09 PHP
Yii redis集合的基本使用教程
2020/06/14 PHP
CLASS_CONFUSION JS混淆 全源码
2007/12/12 Javascript
JavaScript 原型学习总结
2010/10/29 Javascript
利用JS自动打开页面上链接的实现代码
2011/09/25 Javascript
jquery的ajaxSubmit()异步上传图片并保存表单数据演示代码
2013/06/04 Javascript
IE与FireFox的JavaScript兼容问题解决办法
2013/12/31 Javascript
jquery缓动swing liner控制动画过程不同时刻的速度
2014/05/29 Javascript
详谈jQuery中的this和$(this)
2014/11/13 Javascript
Jquery解析json字符串及json数组的方法
2015/05/29 Javascript
JavaScript数据结构链表知识详解
2016/11/21 Javascript
Jquery中attr与prop的区别详解
2017/05/27 jQuery
微信小程序收藏功能的实现代码
2018/06/12 Javascript
Angular5中提取公共组件之radio list的实例代码
2018/07/10 Javascript
微信小程序实现红包雨功能
2018/07/11 Javascript
JavaScript设计模式之门面模式原理与实现方法分析
2020/03/09 Javascript
JavaScript canvas实现雨滴特效
2021/01/10 Javascript
[31:33]2014 DOTA2国际邀请赛中国区预选赛 TongFu VS DT 第一场
2014/05/23 DOTA
Python冒泡排序注意要点实例详解
2016/09/09 Python
Python OpenCV读取png图像转成jpg图像存储的方法
2018/10/28 Python
pyqt5实现按钮添加背景图片以及背景图片的切换方法
2019/06/13 Python
Python数据可视化:箱线图多种库画法
2019/11/06 Python
Windows下Pycharm远程连接虚拟机中Centos下的Python环境(图文教程详解)
2020/03/19 Python
Python-for循环的内部机制
2020/06/12 Python
python3实现语音转文字(语音识别)和文字转语音(语音合成)
2020/10/14 Python
python爬虫 requests-html的使用
2020/11/30 Python
Python中使用Selenium环境安装的方法步骤
2021/02/22 Python
波兰在线儿童和婴儿用品零售商:pinkorblue
2019/06/29 全球购物
恒华伟业笔试面试题
2015/02/26 面试题
汇科协同Java笔试题
2012/03/31 面试题
以幸福为主题的活动方案
2014/08/22 职场文书
死者家属慰问信
2015/03/24 职场文书
2015年节能减排工作总结
2015/05/14 职场文书
三国演义读书笔记
2015/06/25 职场文书