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获取当前计算机cpu数量的方法
Apr 18 Python
Python3.x对JSON的一些操作示例
Sep 01 Python
Python爬虫获取整个站点中的所有外部链接代码示例
Dec 26 Python
Python绘制KS曲线的实现方法
Aug 13 Python
使用Django2快速开发Web项目的详细步骤
Jan 06 Python
python多线程调用exit无法退出的解决方法
Feb 18 Python
python的re模块使用方法详解
Jul 26 Python
pytorch数据预处理错误的解决
Feb 20 Python
pyqt5中动画的使用详解
Apr 01 Python
python如何调用百度识图api
Sep 29 Python
python元组拆包实现方法
Feb 28 Python
浅谈Python中的正则表达式
Jun 28 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
PHP执行zip与rar解压缩方法实现代码
2010/12/05 PHP
php递归获取目录内文件(包含子目录)封装类分享
2013/12/25 PHP
PHP调用.NET的WebService 简单实例
2015/03/27 PHP
thinkPHP5 tablib标签库自定义方法详解
2017/05/10 PHP
Javascript中的数学函数
2007/04/04 Javascript
用javascript控制iframe滚动的代码
2007/04/10 Javascript
关于__defineGetter__ 和__defineSetter__的说明
2007/05/12 Javascript
日期 时间js控件
2009/05/07 Javascript
将string解析为json的几种方式小结
2010/11/11 Javascript
浅谈javascript的调试
2015/01/28 Javascript
2则自己编写的jQuery特效分享
2015/02/26 Javascript
JS实现可关闭的对联广告效果代码
2015/09/14 Javascript
JavaScript文本框脚本编写的注意事项
2016/01/25 Javascript
jQuery实现简单隔行变色的方法
2016/02/20 Javascript
利用PM2部署node.js项目的方法教程
2017/05/10 Javascript
JS实现简单抖动效果
2017/06/01 Javascript
Angular实现的敏感文字自动过滤与提示功能示例
2017/12/29 Javascript
jquery.pagination.js分页使用教程
2018/10/23 jQuery
详解vue-router数据加载与缓存使用总结
2018/10/29 Javascript
js实现select下拉框选择
2020/01/11 Javascript
js实现点击生成随机div
2020/01/16 Javascript
vue 路由缓存 路由嵌套 路由守卫 监听物理返回操作
2020/08/06 Javascript
JavaScript 事件代理需要注意的地方
2020/09/08 Javascript
python3 使用Opencv打开USB摄像头,配置1080P分辨率的操作
2019/12/11 Python
使用gunicorn部署django项目的问题
2020/12/30 Python
轻松掌握CSS3中的字体大小单位rem的使用方法
2016/05/24 HTML / CSS
HTML5实现晶莹剔透的雨滴特效
2014/05/14 HTML / CSS
在SQL Server中创建数据库主要有那种方式
2013/09/10 面试题
介绍下java.util.Arrays类
2012/10/16 面试题
《猴子种树》教学反思
2014/02/14 职场文书
2014年教师节国旗下讲话稿
2014/09/10 职场文书
2014年财政所工作总结
2014/11/22 职场文书
后进生评语大全
2015/01/04 职场文书
党建工作目标管理责任书
2015/01/29 职场文书
小班教师个人总结
2015/02/05 职场文书