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之复习if语句
Oct 02 Python
pytorch: tensor类型的构建与相互转换实例
Jul 26 Python
pip安装py_zipkin时提示的SSL问题对应
Dec 29 Python
基于MATLAB和Python实现MFCC特征参数提取
Aug 13 Python
PYTHON实现SIGN签名的过程解析
Oct 28 Python
Python连接字符串过程详解
Jan 06 Python
Pycharm和Idea支持的vim插件的方法
Feb 21 Python
python新手学习可变和不可变对象
Jun 11 Python
在keras中实现查看其训练loss值
Jun 16 Python
python如何建立全零数组
Jul 19 Python
Python安装Bs4的多种方法
Nov 28 Python
python运算符之与用户交互
Apr 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
PHP中redis的用法深入解析
2014/02/20 PHP
Laravel+jQuery实现AJAX分页效果
2016/09/14 PHP
Laravel框架实现的记录SQL日志功能示例
2018/06/19 PHP
jquery创建div 实现代码
2009/04/27 Javascript
利用百度地图JSAPI生成h7n9禽流感分布图实现代码
2013/04/15 Javascript
关于jQuery新的事件绑定机制on()的使用技巧
2013/04/26 Javascript
javascript中的变量作用域以及变量提升详细介绍
2013/10/24 Javascript
javascript作用域和闭包使用详解
2014/04/25 Javascript
JS+CSS实现美化的下拉列表框效果
2015/08/11 Javascript
Vue.js实现拖放效果的实例
2016/09/30 Javascript
jQuery操作json常用方法示例
2017/01/04 Javascript
在js中做数字字符串补0(js补零)
2017/03/25 Javascript
微信小程序 判断手机号的实现代码
2017/04/19 Javascript
jQuery绑定事件方法及区别(bind,click,on,live,one)
2017/08/14 jQuery
Angular4表单验证代码详解
2017/09/03 Javascript
node.js学习之事件模块Events的使用示例
2017/09/28 Javascript
使用vue-cli+webpack搭建vue开发环境的方法
2017/12/22 Javascript
js实现点击生成随机div
2020/01/16 Javascript
python flask实现分页效果
2017/06/27 Python
python3+dlib实现人脸识别和情绪分析
2018/04/21 Python
Django框架多表查询实例分析
2018/07/04 Python
用python标准库difflib比较两份文件的异同详解
2018/11/16 Python
元组列表字典(莫烦python基础)
2019/04/03 Python
Python实现京东秒杀功能代码
2019/05/16 Python
python腾讯语音合成实现过程解析
2019/08/01 Python
Django 自动生成api接口文档教程
2019/11/19 Python
将SVG图引入到HTML页面的实现
2019/09/20 HTML / CSS
英国度假别墅预订:Sykes Cottages
2017/06/12 全球购物
英国蜡烛、蜡烛配件和家居香氛购买网站:Yankee Candle
2018/12/12 全球购物
聚网科技C++面试笔试题
2015/09/01 面试题
银行委托书范本
2014/04/04 职场文书
个人债务授权委托书
2014/10/17 职场文书
2016年七夕情人节宣传语
2015/11/25 职场文书
先进个人事迹材料(2016推荐版)
2016/03/01 职场文书
Python词云的正确实现方法实例
2021/05/08 Python
Python 可迭代对象 iterable的具体使用
2021/08/07 Python