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实现获取操作系统版本信息方法
Apr 08 Python
pandas DataFrame实现几列数据合并成为新的一列方法
Jun 08 Python
python计算列表内各元素的个数实例
Jun 29 Python
python监控进程状态,记录重启时间及进程号的实例
Jul 15 Python
Python使用指定端口进行http请求的例子
Jul 25 Python
Numpy中对向量、矩阵的使用详解
Oct 29 Python
python 实现dict转json并保存文件
Dec 05 Python
Python 模拟动态产生字母验证码图片功能
Dec 24 Python
python图形开发GUI库wxpython使用方法详解
Feb 14 Python
python神经网络编程实现手写数字识别
May 27 Python
Python常用扩展插件使用教程解析
Nov 02 Python
Python下opencv使用hough变换检测直线与圆
Jun 18 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 adodb操作mysql数据库
2009/03/19 PHP
js 模拟气泡屏保效果代码
2010/07/10 Javascript
轻量级 JS ToolTip提示效果
2010/07/20 Javascript
js关于字符长度限制的问题示例探讨
2014/01/24 Javascript
PHP中使用微秒计算脚本执行时间例子
2014/11/19 Javascript
JS实现很酷的水波文字特效实例
2015/02/26 Javascript
JavaScript获取页面中第一个锚定文本的方法
2015/04/03 Javascript
javascript简单实现滑动菜单效果的方法
2015/07/27 Javascript
关于安卓手机微信浏览器中使用XMLHttpRequest 2上传图片显示字节数为0的解决办法
2016/05/17 Javascript
AngularJS基础 ng-readonly 指令简单示例
2016/08/02 Javascript
深入浅析Node环境和浏览器的区别
2018/08/14 Javascript
解决layui 复选框等内置控件不显示的问题
2018/08/14 Javascript
浅析vue中的MVVM实现原理
2019/03/04 Javascript
Vue 动态路由的实现及 Springsecurity 按钮级别的权限控制
2019/09/05 Javascript
vue点击当前路由高亮小案例
2019/09/26 Javascript
js实现随机div颜色位置 类似满天星效果
2019/10/24 Javascript
原生JavaScript之es6中Class的用法分析
2020/02/23 Javascript
[04:00]黄浦江畔,再会英雄——完美世界DOTA2 TI9应援视频
2019/07/31 DOTA
[01:02:26]DOTA2-DPC中国联赛 正赛 SAG vs RNG BO3 第二场 1月18日
2021/03/11 DOTA
Python数据操作方法封装类实例
2017/06/23 Python
Python使用django框架实现多人在线匿名聊天的小程序
2017/11/29 Python
python3+PyQt5实现文档打印功能
2018/04/24 Python
对python 多个分隔符split 的实例详解
2018/12/20 Python
使用python打印十行杨辉三角过程详解
2019/07/10 Python
Pytorch 实现自定义参数层的例子
2019/08/17 Python
Python定义函数时参数有默认值问题解决
2019/12/19 Python
Django实现celery定时任务过程解析
2020/04/21 Python
python闭包与引用以及需要注意的陷阱
2020/09/18 Python
Python如何使用vars返回对象的属性列表
2020/10/17 Python
html5与css3小应用
2013/04/03 HTML / CSS
HTML5全屏(Fullscreen)API详细介绍
2015/04/24 HTML / CSS
上班上网检讨书
2014/01/29 职场文书
领导失职检讨书
2014/02/24 职场文书
驾驶员安全责任书范本
2014/07/24 职场文书
村创先争优活动总结
2014/08/28 职场文书
vue postcss-px2rem 自适应布局
2022/05/15 Vue.js