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利用beautifulSoup实现爬虫
Sep 29 Python
简单介绍Python中利用生成器实现的并发编程
May 04 Python
python根据京东商品url获取产品价格
Aug 09 Python
Python使用Selenium+BeautifulSoup爬取淘宝搜索页
Feb 24 Python
Python3 单行多行万能正则匹配方法
Jan 07 Python
Django Rest framework三种分页方式详解
Jul 26 Python
Python线程threading模块用法详解
Feb 26 Python
谈谈Python:为什么类中的私有属性可以在外部赋值并访问
Mar 05 Python
Python语法垃圾回收机制原理解析
Mar 25 Python
Keras 在fit_generator训练方式中加入图像random_crop操作
Jul 03 Python
使用OpenCV实现道路车辆计数的使用方法
Jul 15 Python
python实现excel公式格式化的示例代码
Dec 23 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
Zerg兵种介绍
2020/03/14 星际争霸
PHP文本数据库的搜索方法
2006/10/09 PHP
dedecms后台验证码总提示错误的解决方法
2007/03/21 PHP
基于php 随机数的深入理解
2013/06/05 PHP
在openSUSE42.1下编译安装PHP7 的方法
2015/12/24 PHP
PHP基于Redis消息队列实现发布微博的方法
2017/05/03 PHP
tp5.1 实现setInc字段自动加1
2019/10/18 PHP
PHP call_user_func和call_user_func_array函数的简单理解与应用分析
2019/11/25 PHP
jquery实现的一个导航滚动效果具体代码
2013/05/27 Javascript
javascript中定义私有方法说明(private method)
2014/01/27 Javascript
AngularJS中比较两个数组是否相同
2016/08/24 Javascript
JavaScript函数节流的两种写法
2017/04/07 Javascript
Vue生命周期示例详解
2017/04/12 Javascript
在Vue中使用highCharts绘制3d饼图的方法
2018/02/08 Javascript
LayerClose弹窗关闭刷新方法
2018/08/17 Javascript
微信小程序实现复选框效果
2018/12/28 Javascript
Python制作爬虫采集小说
2015/10/25 Python
在Django同1个页面中的多表单处理详解
2017/01/25 Python
python实现一次性封装多条sql语句(begin end)
2020/06/06 Python
python中对二维列表中一维列表的调用方法
2020/06/07 Python
python爬取网易云音乐热歌榜实例代码
2020/08/07 Python
详解python中的lambda与sorted函数
2020/09/04 Python
使用简单的CSS3属性实现炫酷读者墙效果
2014/01/08 HTML / CSS
HTML5 Canvas+JS控制电脑或手机上的摄像头实例
2014/05/03 HTML / CSS
Canvas实现保存图片到本地的示例代码
2018/06/28 HTML / CSS
美国克罗格超市在线购物:Kroger
2019/06/21 全球购物
我的珠宝盒:Ma boîte à bijoux
2019/08/27 全球购物
临床医学系毕业生推荐信
2013/11/09 职场文书
临床医师专业个人自我评价
2014/01/08 职场文书
小学语文业务学习材料
2014/06/02 职场文书
承诺书范本
2015/01/21 职场文书
艺术节开幕词
2015/01/28 职场文书
论文答辩开场白大全
2015/05/27 职场文书
入党转正介绍人意见
2015/06/03 职场文书
2016公务员年度考核评语
2015/12/01 职场文书
Redis 配置文件重要属性的具体使用
2021/05/20 Redis