Python实现远程调用MetaSploit的方法

 更新时间:2014年08月22日 11:25:16   投稿:shichen2014  
这篇文章主要介绍了Python实现远程调用MetaSploit的方法,是很有借鉴价值的一个技巧,需要的朋友可以参考下

本文较为详细的讲述了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函数的两种嵌套方法使用

    本文主要介绍了python函数的两种嵌套方法使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-04-04
  • Python playwright学习之自动录制生成脚本

    Python playwright学习之自动录制生成脚本

    playwright 可以支持自动录制生成脚本,也就是说只需要在页面上点点点,就可以自动生成对应的脚本了。本文就来和大家详细聊聊实现方法吧
    2023-02-02
  • Python语法学习之正则表达式的使用详解

    Python语法学习之正则表达式的使用详解

    要想成功的进行字符串的匹配需要使用到正则表达式模块,正则表达式匹配规则以及需要被匹配的字符串。本文详细为大家介绍了如何利用正则表达式实现字符的匹配,感兴趣的可以了解一下
    2022-04-04
  • 10 分钟快速入门 Python3的教程

    10 分钟快速入门 Python3的教程

    这篇文章主要介绍了10 分钟快速入门 Python3的教程,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-01-01
  • Python比较2个时间大小的实现方法

    Python比较2个时间大小的实现方法

    下面小编就为大家分享一篇Python比较2个时间大小的实现方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-04-04
  • Python 中 Meta Classes详解

    Python 中 Meta Classes详解

    首先,在认识metaclass之前,你需要认识下python中的class。python中class的奇怪特性借鉴了smalltalk语言。大多数语言中,classes仅仅是用于描述怎样创建一个对象的代码端。在某种程度上说,python中的class也是这样的。
    2016-02-02
  • Python根据Excel表进行文件重命名的实现示例

    Python根据Excel表进行文件重命名的实现示例

    在日常办公过程中,批量重命名是经常使用的操作,本文主要介绍了Python根据Excel表进行文件重命名,具有一定的参考价值,感兴趣的可以了解一下
    2024-01-01
  • Python DPED机器学习之实现照片美化

    Python DPED机器学习之实现照片美化

    本篇文章主要介绍了利用Python中的DPED工具实现照片一键美化,可以实现照片亮度提高和色彩鲜明度提高,代码简洁易懂,具有一定学习价值,感兴趣的小伙伴可以了解一下
    2021-11-11
  • python如何代码集体右移

    python如何代码集体右移

    在本篇文章里小编给各位分享的是一篇关于python如何代码集体右移的相关知识点文章,需要的朋友们可以学习下。
    2020-07-07
  • 解决Django模板无法使用perms变量问题的方法

    解决Django模板无法使用perms变量问题的方法

    这篇文章主要给大家介绍了关于解决Django模板无法使用perms变量问题的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
    2017-09-09

最新评论