Skip to content
Tutorials Center
My learning

Topics

Lesson 9 of 37 ยท Architecture and Modules

Options, the Datastore, setg and Resource Scripts

Master Metasploit options: module-level set versus global setg, the datastore, saving settings, and resource scripts that make a lab or test fully repeatable.

Every setting you give a module lives in the datastore. Understanding its two layers (global and module-local) prevents the classic mistake of running a module against the wrong target because an old value was still set. Resource scripts then turn a sequence of commands into a repeatable procedure, which is what makes a test reproducible and reviewable.

Two layers of options

  • set NAME value stores an option for the currently loaded module only. It is forgotten when you back out or change module.
  • setg NAME value stores a global value that every module inherits unless it has its own. unsetg removes it.
TEXT
msf6 > setg RHOSTS 10.10.10.20
RHOSTS => 10.10.10.20
msf6 > use auxiliary/scanner/example/version_scan
msf6 auxiliary(...) > show options     # RHOSTS shows 10.10.10.20
msf6 auxiliary(...) > set RHOSTS 10.10.10.21
msf6 auxiliary(...) > back
msf6 > use auxiliary/scanner/example/other_scan
msf6 auxiliary(...) > show options     # RHOSTS is 10.10.10.20 again
flowchart TD
    M["Module-local value (set)"] -->|"if empty, fall back to"| G["Global value (setg)"]
    G -->|"if empty, fall back to"| D["Module default"]

The most specific setting wins. A stale setg is a common source of surprises.

The command save writes the global datastore and module settings to a config file so the next console start has the same values. That is convenient and also risky: a saved RHOSTS from last week's client may still be there. Prefer explicit resource scripts over save.

Other datastore tips:

  • set with no arguments lists the options currently set.
  • Options are case-insensitive for the name in practice; modules show them in capitals by convention.
  • Some options accept ranges and files: RHOSTS 10.10.10.0/24, RHOSTS 10.10.10.20-30, or RHOSTS file:/path/targets.txt.
  • ConsoleLogging and LogLevel change logging verbosity; ForceExploit and VERBOSE change module behaviour.

Resource scripts

A resource script is a text file of console commands, one per line. Run it from the command line (msfconsole -r file.rc) or inside the console (resource file.rc). makerc file.rc saves the commands you typed in the current session as a script.

TEXT
# lab-recon.rc : a repeatable first look at the lab range
workspace -a lab-recon
setg RHOSTS 10.10.10.0/24
use auxiliary/scanner/example/version_scan
set RPORT 8080
set THREADS 5
run
back
hosts
services

A script is both automation and documentation: a reviewer can read exactly what was done and re-run it against a fresh lab snapshot. Resource scripts can also include embedded Ruby (<ruby> ... </ruby>) for logic, but keep them simple: logic belongs in modules or external scripts.

Resource scripts execute without asking. Put workspace and the scope-defining setg RHOSTS at the top, and never include a target list you have not checked against the contract.

Modelling the two-layer lookup

The stand-in implements the lookup rule above using Python's ChainMap. It shows how a module value overrides a global, and how unset values fall through.

Python Module options override globals

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Running a script safely

The second stand-in "executes" a resource script, but only tracks state: it refuses to run any run-style command if a workspace and RHOSTS were not set first, and it refuses targets outside the allowed range. This is the safeguard to build into your own habits and wrappers.

Python A resource script runner with guards

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Change the setg RHOSTS line to 192.168.0.0/24 and run it again: the guard stops the script.

Habits

  • Start every session with workspace and check show options.
  • Prefer set for per-module values; use setg only for things like RHOSTS or LHOST that you deliberately share.
  • Use unsetg -a (clear all globals) when switching targets.
  • Keep resource scripts in version control with the report; they prove what you did.
  • Avoid saving credentials in scripts; if a script needs secrets, read them from a protected file at run time.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the difference between set and setg?

    set is module-local; setg is global and inherited by every module unless overridden.
  2. 2. How do you run a resource script from the command line?

    -r (or the resource command inside the console) executes the commands in a file.
  3. 3. Why is a stale global option dangerous?

    Globals persist across modules, so old RHOSTS values can hit the wrong target.
  4. 4. What does makerc do?

    makerc writes your typed commands to a file you can replay later.

Summary

  • Options resolve from module-local, then global, then default values.
  • setg is convenient but dangerous when stale; unsetg -a clears it.
  • Resource scripts make tests repeatable and reviewable; put scope checks and a workspace at the top.
  • Do not store credentials in scripts.

Next: Reconnaissance Methodology