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 multiprocessing.Manager介绍和实例(进程间共享数据)
Nov 21 Python
Python中getattr函数和hasattr函数作用详解
Jun 14 Python
利用python爬取软考试题之ip自动代理
Mar 28 Python
利用Python批量压缩png方法实例(支持过滤个别文件与文件夹)
Jul 30 Python
将python运行结果保存至本地文件中的示例讲解
Jul 11 Python
使用pandas读取文件的实现
Jul 31 Python
给你一面国旗 教你用python画中国国旗
Sep 24 Python
Python猜数字算法题详解
Mar 01 Python
基于Keras 循环训练模型跑数据时内存泄漏的解决方式
Jun 11 Python
Django中F函数的使用示例代码详解
Jul 06 Python
深入了解Python 方法之类方法 &amp; 静态方法
Aug 17 Python
Python pip 常用命令汇总
Oct 19 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
解决File size limit exceeded 错误的方法
2013/06/14 PHP
ThinkPHP中的create方法与自动令牌验证实例教程
2014/08/22 PHP
laravel实现分页样式替换示例代码(增加首、尾页)
2017/09/22 PHP
发两个小东西,ASP/PHP 学习工具。 用JavaScript写的
2007/04/12 Javascript
JQuery 常用操作代码
2010/03/14 Javascript
三种取消选中单选框radio的方法
2014/09/09 Javascript
JS打字效果的动态菜单代码分享
2015/08/21 Javascript
JavaScript统计字符串中每个字符出现次数完整实例
2016/01/28 Javascript
详解使用路由延迟加载 Angular 模块
2017/10/12 Javascript
详解Webstorm 新建.vue文件支持高亮vue语法和es6语法
2017/10/26 Javascript
Python两个整数相除得到浮点数值的方法
2015/03/18 Python
Python3编程实现获取阿里云ECS实例及监控的方法
2017/08/18 Python
flask中使用蓝图将路由分开写在不同文件实例解析
2018/01/19 Python
Python使用pandas处理CSV文件的实例讲解
2018/06/22 Python
python hough变换检测直线的实现方法
2019/07/12 Python
python中列表的含义及用法
2020/05/26 Python
Pandas对DataFrame单列/多列进行运算(map, apply, transform, agg)
2020/06/14 Python
keras 指定程序在某块卡上训练实例
2020/06/22 Python
python如何写try语句
2020/07/14 Python
基于python爬取梨视频实现过程解析
2020/11/09 Python
CSS3 文字动画效果
2020/11/12 HTML / CSS
html5菜单折纸效果
2014/04/22 HTML / CSS
html5+css3进度条倒计时动画特效代码【推荐】
2016/03/08 HTML / CSS
PHP面试题大全
2015/10/16 面试题
高中自我鉴定范文
2013/11/03 职场文书
超市端午节活动方案
2014/01/23 职场文书
优秀高中生事迹材料
2014/02/11 职场文书
白酒营销策划方案
2014/08/17 职场文书
导游词欢迎词
2015/02/02 职场文书
论文致谢词范文
2015/05/14 职场文书
荒岛余生观后感
2015/06/09 职场文书
2016中秋节晚会开场白
2015/11/26 职场文书
生产实习心得体会范文
2016/01/22 职场文书
2019年第四季度财务部门工作计划
2019/11/02 职场文书
Javascript的promise,async和await的区别详解
2022/03/24 Javascript
vue中使用mockjs配置和使用方式
2022/04/06 Vue.js