Skip to content

Writing Modules

A guide to writing new Metasploit modules. The reference code is under packages/metasploit-framework. The referenced commit is listed in Metasploit Operations Notes.

This guide assumes you will use existing payloads from exploits rather than write payload modules themselves.

  1. Use this page to choose a module type and review the decision table.
  2. Read Shared Steps from top to bottom for the purpose and implementation of each step.
  3. Read the page for your module type for entry methods and examples.
  4. Consult Mixins and APIs, Helper Functions, and send_request_cgi as needed.
PurposePage
Shared steps, one at a timeShared Steps
ExploitExploit
AuxiliaryAuxiliary
PostPost
Mixins and APIsMixins and APIs
Helper functionsHelper Functions
HTTP requestssend_request_cgi
LocationPurpose
~/.msf4/modules/exploits/...Custom exploit
~/.msf4/modules/auxiliary/...Custom auxiliary module
~/.msf4/modules/post/...Custom post module
packages/metasploit-framework/modules/...Upstream reference code
loadpath /path/to/extra/modules
reload_lib -a

The following templates are useful references:

  • modules/exploits/example.rb — TCP exploit
  • modules/exploits/example_webapp.rb — HTTP / web application exploit
  • modules/exploits/example_linux_priv_esc.rb — local privilege escalation
  • modules/auxiliary/example.rb — auxiliary
  • modules/post/multi/general/execute.rb — post module
TypeExample base classEntry methodPayload
exploitMsf::Exploit::Remote / Msf::Exploit::Localcheck (optional), exploitUsed
auxiliaryMsf::Auxiliaryrun, or run_host for a scannerNot used
postMsf::Postrun (SESSION required)Not used (runs in an existing session)
payload(Not written here)—Is itself a payload

Use an exploit for code that opens a new session, an auxiliary module for code that does not, and a post module for work in an existing session.

#StepWhat to doDetails
1Type and pathChoose exploit, auxiliary, or post and its directoryType and Location
2Base class and mixinsAdd communication and execution behavior through inheritance and includeBase Classes and Mixins
3update_infoMetadata such as Name, Platform, Targets, and Notesupdate_info
4register_optionsInputs that users set with setregister_options
5Entry methodcheck / exploit / run / run_hostEntry Methods
6Saving resultsreport_*, store_loot, and credential APIsSaving Results
7CleanupSchedule removal of placed files and related cleanupCleanup

Find the relevant page from a common task.

TaskSuggested typeRead first
Log in over HTTP and retrieve configuration or filesauxiliaryAuxiliary + send_request_cgi
Scan banners or versions across many hostsauxiliary + ScannerScanner
Try username and password listsauxiliary + AuthBruteAuthBrute
Return a shell through HTTP command injectionexploitExploit + CmdStager
Open a session through a TCP binary vulnerabilityexploit + TcpTcp + Exploit
Serve a trap page to a browserexploit + HttpServerHttpServer
Run commands or interact over SSHexploit or auxiliarySSH
Operate or authenticate over SMBauxiliary / exploit + SMBSMB
Enumerate from an existing shell or Meterpreter sessionpostPost + Post Mixins
Upgrade a shell to Meterpreterpost (an existing module also works)Post / operations: SSH/WinRM
Escalate privileges locally from an existing sessionexploit (local)Exploit
Check only whether a vulnerability existscheck in exploit / auxiliaryEntry Methods + AutoCheck
Save retrieved hashes or configuration in the databaseReport with any module typeSaving Results
Remove an uploaded web shell laterexploit + FileDropperCleanup
Set up only a listener handlerExisting exploit/multi/handlerOperations: Handlers and Listeners (no custom module needed)

If unsure, ask three questions: Will this open a new session? Is there already a session? Does it need a payload? Then return to Type and Location.