Metasploit

Code Design Pattern

Metasploit uses Facade design pattern which encapsulates/simplifies the complex part of the framework by implementing it as interfaces which makes development really easy and elegant.
I found that the Wikipedia example of facades is descent to be presented

  1. # Complex Parts | Computer framework
  2. class CPU
  3. def freeze; end
  4. def jump(position); end
  5. def execute; end
  6. end
  7. class Memory
  8. def load(position, data); end
  9. end
  10. class HardDrive
  11. def read(lba, size); end
  12. end
  13. # Facade | Interface
  14. class ComputerFacade
  15. def initialize
  16. @processor = CPU.new
  17. @ram = Memory.new
  18. @hd = HardDrive.new
  19. end
  20. def start
  21. @processor.freeze
  22. @ram.load(BOOT_ADDRESS, @hd.read(BOOT_SECTOR, SECTOR_SIZE))
  23. @processor.jump(BOOT_ADDRESS)
  24. @processor.execute
  25. end
  26. end
  27. # Client (The Developer want to use the complex computer framework)
  28. computer_facade = ComputerFacade.new
  29. computer_facade.start

As you can see from the above code, the developer who wants to use the Computer framework don’t have to deal with the complex codebase (classes, methods and calculations) directly. Instead, he will use a simple interface class called ComputerFacade which instantiate(as objects) all classes once you call it.

Another exist example in ruby language itself is open-uri standard library, which encapsulates net/http and uri libraries and makes theme looks like opening ordinary file.
To see how open-uri makes things easy, We’ll write a code that send get request to Ruby.net and get the response with both regular and open-uri way

regular way

  1. require 'net/http'
  2. require 'uri'
  3. url = URI.parse('http://rubyfu.net')
  4. res = Net::HTTP.start(url.host, url.port) {|http|
  5. http.get('/content/index.html')
  6. }
  7. puts res.body

facade way

  1. require "open-uri"
  2. puts open("http://rubyfu.net/content/index.html").read

More about Facade

Metasploit Structure

Metasploit - 图1

As you can see in figure above, Metasploit libraries are working as interface serves all modules, interfaces, tools and plugins. That’s exactly represents what we’ve explained in Code Design Pattern.

  1. mkdir -p $HOME/.msf4/modules/{auxiliary,exploits,post}

Absolute module

Here is a very basic structure of a general module.

I’ll Add some comments for explanation purpose.

  1. ##
  2. # This module requires Metasploit: http://www.metasploit.com/download
  3. # Current source: https://github.com/rapid7/metasploit-framework
  4. ##
  5. require 'msf/core'
  6. ### Module Type ###
  7. class Metasploit3 < Msf::Exploit::Remote
  8. ####################
  9. ### Module Requirements ###
  10. include Exploit::Remote::Tcp
  11. ####################
  12. ### Exploit Rank ####
  13. Rank = ExcellentRanking
  14. ####################
  15. ### Module Information
  16. def initialize(info = {})
  17. super(update_info(
  18. info,
  19. 'Name' => 'Absolute MSF template',
  20. 'Description' => %q{This is an absolute MSF template that shows how all modules look like},
  21. 'License' => MSF_LICENSE,
  22. 'Author' =>
  23. [
  24. 'Rubyfu (@Rubyfu)',
  25. 'Sabri (@KINGSABRI)'
  26. ],
  27. 'References' =>
  28. [
  29. ['URL', 'http://Rubyfu.net'],
  30. ['URL', 'https://github.com/Rubyfu']
  31. ],
  32. 'Platform' => %w{ linux win osx solaris unix bsd android aix},
  33. 'Targets' =>
  34. [
  35. ['Universal', {}]
  36. ],
  37. 'DefaultTarget' => 0,
  38. 'DisclosureDate' => '2015'
  39. ))
  40. # Module Options | show options
  41. register_options(
  42. [
  43. Opt::RPORT(22),
  44. OptString.new('USER', [ true, 'Valid username', 'admin' ]),
  45. OptString.new('PASS', [ true, 'Valid password for username', 'P@ssw0rd' ]),
  46. ], self.class)
  47. # Module Advanced Options | show advanced
  48. register_advanced_options(
  49. [
  50. OptInt.new('THREADS', [true, 'The number of concurrent threads', 5])
  51. ], self.class)
  52. end
  53. ####################
  54. ### Module Operations ###
  55. def exploit # or 'run' for post and auxiliary modules
  56. print_status('Starting Rubyfu')
  57. print_warning("It's just a template.")
  58. print_good('Ruby goes evil!')
  59. print_error("Thank you!")
  60. end
  61. ####################
  62. end

The result is

Metasploit - 图2

Load Metasploit module

To load/reload the Metasploit module you’re working on, you can put the script in your user’s Metasploit path or in the Metasploit framework path

  • User’s Metasploit path

    1. ~/msf4/modules
  • Metasploit framework path

    1. metasploit-framework/modules/

To make Metasploit load/reload the script use one of the following ways

  • Exit from msfconsole then run it again
  • use reload_all to reload all modules
  • If your module is previously loaded and you made changes on it just use reload but you have to be using the module, in another work use [YOUR MODULE]

Note: It’s really important to know the official Metasploit development documentation ( http://www.rubydoc.info/github/rapid7/metasploit-framework/ )

!--- https://www.exploit-db.com/docs/27935.pdf http://www.rubydoc.info/github/rapid7/metasploit-framework https://github.com/rapid7/metasploit-framework/wiki/Exploit-Ranking https://github.com/rapid7/metasploit-framework/wiki https://community.rapid7.com/thread/3126 https://github.com/rapid7/metasploit-framework/wiki/Creating-Metasploit-Framework-LoginScanners --