欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python实现远程调用MetaSploit的方法

程序员文章站 2023-11-22 12:28:40
本文较为详细的讲述了python实现远程调用metasploit的方法,对python的学习来说有很好的参考价值。具体实现方法如下: (1)安装python的msgpac...

本文较为详细的讲述了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学习可以起到一定的学习借鉴作用。