There are times when you seem to be continually typing
the same commands over and over.
Thankfully, there are some options in the Metasploit framework that can help.
Let's look at repeating actions.
There could be a variety of reasons for repetition, here are some to consider.
The first could be that you encounter the same targets in that environment.
These could be built from the same image and have the
same services and vulnerabilities.
You're repeating the same commands for scanning and exploiting those targets.
You may be called upon to repeat the steps in a test.
Perhaps the client has remediated the issue and ask
that you validate the correction.
You may need to repeat the testing procedures used.
Another reason is that the vulnerability you are attempting to exploit
has to have the right conditions to work. Exploitation requires multiple
attempts in order to finally trigger it.
Finally,
you can be better and more efficient in testing if you
can add automation to the process.
The first approach to consider is scripting.
If you've written any scripts in Unix shells or Python, Ruby, or Perl,
then you're very familiar with the process and the power
scripting has. The Metasploit console has a very similar
environment and can handle scripts.
Meterpreter has some scripts that you can use and more
could be added. Inside the Metasploit console, you can get
started with scripting easily.
First, if you have executed a series of commands since starting the console,
you can make the makerc command save the previously
executed commands to a resource file.
Then you can replay those commands, so to speak, by using the resource command.
You could also write your own resource scripts or
adapt previous scripts to your needs.
Inside the resource scripts,
you could embed Ruby code if you need to, just put it
between these Ruby tags to identify it.
You can even launch Metasploit resource scripts from the system
command line when you're not inside the console.
Meterpreter scripting aids in post exploitation activities on
targets with an active meterpreter session.
The Metasploit framework includes several useful scripts for meterpreter.
The provided scripts are written in Ruby.
If you prefer Python,
there is an option to load a plugin that supports Python for
your own scripts. You can load your own custom scripts from
Meterpreter. Inside the Metasploit console, you may need to
execute a command multiple times.
The repeat command allows you to do that, it can
repeat commands that you specify.
It was originally part of a request related to the heartbleed module.
That test had to be executed multiple times to gather more the random leaked
memory over time. Repeat can execute the same command indefinitely, a specific
number of times, or for a specific amount of time.
Here are commands for executing scripts from the Metasploit console. First,
using the ‑r option and specifying the resource script, the Metasploit
console will execute the commands in the resource script when starting. You
can capture all of the commands executed from the time the console has
started with the makerc command and specifying a file to save them to. You
could execute the commands in one or more resource scripts using the
resource command.
Here are just a few of the meterpreter scripts
included with the Metasploit framework.
You can run your own Metasploit scripts in a similar fashion.
If you're comfortable using Python, first,
you have to load the Python extension.
Then you can either import Python code files to run or execute
Python code specified on the command line.