Benutzer-Werkzeuge

Webseiten-Werkzeuge


metasploit

Dies ist eine alte Version des Dokuments!


Metasploit ist ein Framework für das automatisierte Ausführen von Exploits mit Scriptingfunktionen in Ruby. Siehe auch msfvenom.

Usage

service postgresql start
msfdb init
msfconsole [| armitage]
use exploit/windows/smb/smb_relay
show options|info

# show running sessions
sessions
# Show running jobs
jobs
exploitdb -u // update from git
ls /usr/share/exploitdb // src
searchsploit oracle windows remote

Tips

setg rhosts 192.168.0.1 // set rhosts globally

Ein Modul schreiben

Eine Ruby Datei unter

.msf4/modules/<type>/<name>

Danach

reload_all

Auxiliary

Server Version aus Header abfragen

nano .msf4/modules/auxiliary/scanner/webserver_version.rb
require 'msf/core'
 
class MetasploitModule < Msf::Auxiliary
  include Msf::Exploit::Remote::HttpClient
 
  def initialize(info = {})
    super(update_info(info,
      'Name'        => 'Test Web Version Module',
      'Description' => %q{
        Dieses Modul führt eine einfache HTTP-Anfrage aus und zeigt die Server-Version.
        Es ist ein ungefährliches Beispielmodul zum Lernen.
      },
      'Author'      => ['mbrain'],
      'License'     => MSF_LICENSE
    ))
 
    register_options(
      [
        Opt::RHOSTS(nil),
        Opt::RPORT(80)
      ]
    )
  end
 
  def run
    print_status("Connecting to #{rhost}:#{rport}...")
 
    begin
      res = send_request_cgi({
        'method' => 'GET',
        'uri'    => '/'
      })
 
      if res && res.headers['Server']
        print_good("Server-Version: #{res.headers['Server']}")
      else
        print_warning("Keine Server-Header gefunden.")
      end
 
    rescue ::Rex::ConnectionError
      print_error("Verbindung fehlgeschlagen")
    end
  end
end
msfconsole
reload_all
search webserver_version
use auxiliary/scanner/webserver_version

Exploit

# safe_demo_exploit.rb
# Harmloses Demo-Exploitmodul zum Lernen
 
require 'msf/core'
 
class MetasploitModule < Msf::Exploit::Remote
  Rank = NormalRanking
 
  # Wir benutzen eine einfache TCP-Verbindung
  include Msf::Exploit::Remote::Tcp
 
  def initialize(info = {})
    super(update_info(info,
      'Name'           => 'Safe Demo Exploit (Lernmodul)',
      'Description'    => %q{
        Dieses Modul demonstriert die Struktur eines Exploit-Moduls.
        Es verbindet sich nur mit einem TCP-Dienst, schickt eine Testnachricht
        und meldet "Erfolg", ohne irgendetwas auszunutzen.
      },
      'Author'         => ['mbrain'],
      'License'        => MSF_LICENSE,
      'DisclosureDate' => 'Jan 01 2025',
      'Platform'       => 'unix',
      'Targets'        =>
        [
          [
            'Demo Target',
            {
              'Platform' => 'unix'
            }
          ]
        ],
      'DefaultTarget'  => 0,
      # Wir brauchen keinen richtigen Payload, also minimale Config
      'Payload'        =>
        {
          'Space'       => 0,
          'DisableNops' => true
        }
    ))
 
    register_options(
      [
        Opt::RHOSTS(nil),
        Opt::RPORT(80),
        OptString.new('DEMO_STRING', [ true, 'String, der an den Server gesendet wird', "HELLO FROM METASPLOIT\r\n" ])
      ]
    )
  end
 
  # "check" simuliert nur, dass der Dienst "verwundbar" aussieht
  def check
    vprint_status("Versuche Verbindung zu #{rhost}:#{rport} für Demo-Check...")
    begin
      connect
      print_good("Verbindung hat geklappt – wir tun mal so, als wäre der Dienst verwundbar.")
      disconnect
      return CheckCode::Appears
    rescue ::Rex::ConnectionError
      print_error("Keine Verbindung möglich.")
      return CheckCode::Unknown
    end
  end
 
  # "exploit" wird aufgerufen, wenn du "run" bzw. "exploit" eingibst
  def exploit
    print_status("Starte Demo-Exploit gegen #{rhost}:#{rport} ...")
 
    begin
      connect
      print_status("Sende Demo-String an den Server...")
      sock.put(datastore['DEMO_STRING'])
 
      # Versuche eine Antwort zu lesen (falls vorhanden)
      res = sock.get_once(1024, 3)
 
      if res
        print_good("Server hat geantwortet:")
        print_line(res)
      else
        print_warning("Keine Antwort erhalten (aber das ist für die Demo ok).")
      end
 
      print_good("Demo-Exploit erfolgreich ausgeführt (keine echte Ausnutzung).")
    rescue ::Rex::ConnectionError
      print_error("Verbindung fehlgeschlagen.")
    ensure
      disconnect
    end
  end
end

Sonstige

#MySampleModule
 
class MetasploitModule < Msf::Exploit::Remote
  Rank = NormalRanking
 
  include Exploit::Remote::Tcp
 
  def initialize(info = {})
    super(update_info(info,
      'Name'           => 'My sample Exploit',
      'Description'    => %q{
          This file illustrates how to write a module.
      },
      'License'        => 'UnLicense',
      'Author'         => ['mbrain'],
      'References'     =>
        [
          [ 'CVE', '1234-5678'],
        ],
      'Payload'        =>
        {
          'Space'    => 1000,
          'BadChars' => "\x00",
        },
      'Targets'        =>
        [
          [
            'Windows XP/Vista/7/8',
            {
              'Platform' => 'win',
              'Ret'      => 0x41424344
            }
          ],
        ],
      'DisclosureDate' => "Apr 1 2013",
      'DefaultOptions' =>
      {
        'RPORT' => 80,
        'RHOSTS' => '192.168.0.157'
      },
      'DefaultTarget'  => 0))
 
      register_options([
      Opt::RPORT(4444) # Beispieloption für den Remote-Port
      ])
  end
 
  # Usually this include code for checking
  def check
    Exploit::CheckCode::Vulnerable
  end
 
  def run
    ...
  end
 
  def exploit
    connect
    print_status("Sending #{payload.encoded.length} byte payload...")
    buf  = rand_text_alpha(1024)
    buf << [ target.ret ].pack('V')
    buf << payload.encoded
    sock.put(buf)
    sock.get_once
    handler
  end
 
end
# MySampleModule
 
class MetasploitModule < Msf::Exploit::Remote
  Rank = NormalRanking
 
  include Msf::Exploit::Remote::Tcp
 
  def initialize(info = {})
    super(update_info(info,
      'Name'           => 'My sample Exploit',
      'Description'    => %q{
        This file illustrates how to write a module.
      },
      'License'        => 'UnLicense',
      'Author'         => ['mbrain'],
      'References'     =>
        [
          [ 'CVE', '1234-5678' ],
        ],
      'Payload'        =>
        {
          'Space'    => 1000,
          'BadChars' => "\x00",
        },
      'Targets'        =>
        [
          [
            'Windows XP/Vista/7/8',
            {
              'Platform' => 'win',
              'Ret'      => 0x41424344
            }
          ],
        ],
      'DisclosureDate' => 'Apr 1 2013',
      'DefaultOptions' =>
        {
          'RPORT'  => 80,
          'RHOSTS' => '192.168.0.157'
        },
      'DefaultTarget'  => 0
    ))
 
    register_options(
      [
        Opt::RPORT(4444) # Beispieloption für den Remote-Port
      ]
    )
  end
 
  # Usually this includes code for checking
  def check
    CheckCode::Vulnerable
  end
 
  def exploit
    connect
    print_status("Sending #{payload.encoded.length} byte payload...")
    buf  = rand_text_alpha(1024)
    buf << [ target['Ret'] ].pack('V')
    buf << payload.encoded
    sock.put(buf)
    sock.get_once
    handler
    disconnect
  end
end
require 'msf/core'
 
class MetasploitModule < Msf::Auxiliary
  include Msf::Exploit::Remote::HttpClient
 
  def initialize(info = {})
    super(update_info(info,
      'Name'        => 'Test URL Fetcher',
      'Description' => %q{
        Dieses Modul ruft eine beliebige URI von einem HTTP-Server ab.
        Nützlich zum Lernen, Testen und Debuggen eigener Module.
      },
      'Author'      => ['mbrain'],
      'License'     => MSF_LICENSE
    ))
 
    register_options(
      [
        Opt::RHOSTS(nil),
        Opt::RPORT(80),
        OptString.new('URI', [ true, 'Pfad/URL, die abgerufen werden soll', '/' ])
      ]
    )
  end
 
  def run
    print_status("Rufe http://#{rhost}:#{rport}#{datastore['URI']} ab...")
 
    begin
      res = send_request_cgi({
        'method' => 'GET',
        'uri'    => datastore['URI']
      })
 
      if res
        print_good("Status: #{res.code}")
        print_good("Server-Header: #{res.headers['Server']}") if res.headers['Server']
 
        print_line("")
        print_line("=== Inhalt der Antwort ===")
        print_line(res.body || "<kein Inhalt>")
      else
        print_error("Keine Antwort erhalten.")
      end
 
    rescue ::Rex::ConnectionError
      print_error("Verbindung fehlgeschlagen")
    end
  end
end
# MySampleModule
 
class MetasploitModule < Msf::Exploit::Remote
  Rank = NormalRanking
 
  include Msf::Exploit::Remote::Tcp
 
  def initialize(info = {})
    super(update_info(info,
      'Name'           => 'My sample Exploit',
      'Description'    => %q{
        This file illustrates how to write a module.
      },
      'License'        => 'UnLicense',
      'Author'         => ['mbrain'],
      'References'     =>
        [
          [ 'CVE', '1234-5678' ],
        ],
      'Payload'        =>
        {
          'Space'    => 1000,
          'BadChars' => "\x00",
        },
      'Targets'        =>
        [
          [
            'Windows XP/Vista/7/8',
            {
              'Platform' => 'win',
              'Ret'      => 0x41424344
            }
          ],
        ],
      'DisclosureDate' => 'Apr 1 2013',
      'DefaultOptions' =>
        {
          'RPORT'  => 80,
          'RHOSTS' => '192.168.0.157'
        },
      'DefaultTarget'  => 0
    ))
 
    register_options(
      [
        Opt::RPORT(4444) # Beispieloption für den Remote-Port
      ]
    )
  end
 
  # Usually this includes code for checking
  def check
    CheckCode::Vulnerable
  end
 
def exploit
  print_status("Baue TCP-Verbindung zu #{rhost}:#{rport} auf...")
  connect
 
  uri = '/'
  print_status("Sende langsamen HTTP GET Request an #{rhost}:#{rport}#{uri} ...")
 
  # 1) Request-Line
  sock.put("GET #{uri} HTTP/1.1\r\n")
  sleep(1)  # 1 Sekunde Pause – nur Demo
 
  # 2) Host-Header
  sock.put("Host: #{rhost}\r\n")
  sleep(1)
 
  # 3) User-Agent
  sock.put("User-Agent: SlowDemoMetasploit\r\n")
  sleep(1)
 
  # 4) Connection-Header
  sock.put("Connection: close\r\n")
  sleep(1)
 
  # 5) Leere Zeile zum Abschließen des Headers
  sock.put("\r\n")
 
  print_status("Request komplett geschickt, warte auf Antwort...")
 
  # Antwort lesen (einmalig)
  response = sock.get_once(-1, 5)  # bis zu 5 Sekunden warten
 
  if response
    print_good("Antwort erhalten:")
    print_line(response)
  else
    print_warning("Keine Antwort erhalten (Timeout).")
  end
 
  disconnect
end
end
metasploit.1763156311.txt.gz · Zuletzt geändert: 2025/11/14 22:38 von admin