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 相关文章推荐
easy_install python包安装管理工具介绍
Feb 10 Python
paramiko模块安装和使用(远程登录服务器)
Jan 27 Python
Python统计列表中的重复项出现的次数的方法
Aug 18 Python
PyQt5主窗口动态加载Widget实例代码
Feb 07 Python
利用Pandas 创建空的DataFrame方法
Apr 08 Python
python实现基于信息增益的决策树归纳
Dec 18 Python
python对列进行平移变换的方法(shift)
Jan 10 Python
Python数据报表之Excel操作模块用法分析
Mar 11 Python
Linux上使用Python统计每天的键盘输入次数
Apr 17 Python
用OpenCV将视频分解成单帧图片,图片合成视频示例
Dec 10 Python
Python实现迪杰斯特拉算法并生成最短路径的示例代码
Dec 01 Python
python pillow库的基础使用教程
Jan 13 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
DC《小丑》11项提名领跑奥斯卡 Netflix成第92届奥斯卡提名最大赢家
2020/04/09 欧美动漫
基于empty函数的输出详解
2013/06/17 PHP
详细解读php的命名空间(二)
2018/02/21 PHP
yii 框架实现按天,月,年,自定义时间段统计数据的方法分析
2020/04/04 PHP
如何在PHP中读写文件
2020/09/07 PHP
特殊字符、常规符号及其代码对照表
2006/06/26 Javascript
JavaScript中的prototype和constructor简明总结
2014/04/05 Javascript
javascript对中文按照拼音排序代码
2014/08/20 Javascript
js使用DOM设置单选按钮、复选框及下拉菜单的方法
2015/01/20 Javascript
如何实现JavaScript动态加载CSS和JS文件
2020/12/28 Javascript
基于javascript实现精确到毫秒的倒计时限时抢购
2016/04/17 Javascript
基于jquery实现轮播特效
2016/04/22 Javascript
JavaScript实现页面跳转的方式汇总
2016/05/16 Javascript
vue使用jsonp抓取qq音乐数据的方法
2018/06/21 Javascript
在 Vue-CLI 中引入 simple-mock实现简易的 API Mock 接口数据模拟
2018/11/28 Javascript
js属性对象的hasOwnProperty方法的使用
2021/02/05 Javascript
[02:43]2018DOTA2亚洲邀请赛主赛事首日TOP5
2018/04/04 DOTA
[02:10]DOTA2 TI10勇士令状玩法及不朽Ⅰ展示:焕新世界,如你所期
2020/05/29 DOTA
分享Python开发中要注意的十个小贴士
2016/08/30 Python
Python交互环境下实现输入代码
2018/06/22 Python
用Python中的turtle模块画图两只小羊方法
2019/04/09 Python
python wav模块获取采样率 采样点声道量化位数(实例代码)
2020/01/22 Python
新建文件时Pycharm中自动设置头部模板信息的方法
2020/04/17 Python
德国家具在线:Fashion For Home
2017/03/11 全球购物
德国高品质男装及配饰商城:Cultizm(Raw Denim原色牛仔裤)
2018/04/16 全球购物
PHP中如何创建和修改数组
2012/05/02 面试题
关于Assembly命名空间的三个面试题
2015/07/23 面试题
Java的类与C++的类有什么不同
2014/01/18 面试题
学校岗位设置方案
2014/01/16 职场文书
孝老爱亲模范事迹
2014/01/24 职场文书
党校培训自我鉴定范文
2014/04/10 职场文书
安全隐患整改报告
2014/11/06 职场文书
百日宴上的祝酒词
2015/08/10 职场文书
初中生物教学随笔
2015/08/15 职场文书
男方家长婚礼答谢词
2015/09/29 职场文书
2016暑期师德培训心得体会
2016/01/09 职场文书