Privia Security was chosen as one of Türkiye's fastest growing companies!

Read the News Read the News
2 August 2019

Windows Post Exploitation with Nishang – Part 3

Windows Post Exploitation with Nishang – Part 3
Windows Post Exploitation with Nishang – Part 3

Nishang is a post-exploitation toolkit developed by Nikhil Mittal. Nishang contains script code written in PowerShell that can be used during post-exploitation operations.

With Nishang’s script code, operations such as port scanning, information gathering, privilege escalation, credential harvesting and backdoor deployment can be performed. Since it is built with PowerShell, it can run on Windows operating systems without requiring any installation. One of the tool’s important features is that the scripts run in memory rather than on the disk.

Escalation

One of the important phases in post-exploitation is “Privilege Escalation”. After the first step is taken by exploiting a machine, it may be necessary to escalate privileges on a machine where only restricted access has been obtained. The Nishang toolkit contains 3 scripts for privilege escalation. These scripts are described below.

Enable-DuplicateToken

Accounts with administrator rights on Windows systems have high privileges on the system and can perform administrative operations. However, there are situations where even administrator accounts encounter access restrictions. Administrator accounts do not have access rights to the “HKLM:\SECURITY” path in the Windows registry. SYSTEM privileges are required to access this path. Using this script, the Access Token used by the “lsass.exe” process running with SYSTEM privileges is duplicated, and SYSTEM-level operations can be performed once it is obtained by the account running this script. The account running this script must have administrator privileges.

Access Token duplication
Figure 2.6.1.1 – Access Token Duplication

Invoke-PsUACMe

This script in Nishang bypasses UAC using the methods found in the repository at https://github.com/hfiref0x/UACME. All UAC bypass methods relate to DLL hijacking, renaming executables and changing their locations. This script is able to bypass UAC based on these methods. The parameters for this script are as follows.

UAC bypass methods table
Table 2.6.2.1 – Methods

Remove-Update

This script in Nishang removes all updates. The “KBID” parameter specifies the ID value of the update, and that update is deleted.

Updates list
Figure 2.6.3.1 – Updates
Remove-Update execution
Figure 2.6.3.2 – Remove-Update

Execution

In certain situations during the post-exploitation phase — such as downloading a file from a remote point to execute commands, or receiving a connection from a different location — an attacker may feel the need to execute a payload or a script file. For this purpose, Nishang provides various methods for executing commands. These methods are described below.

Download-Execute-PS

Using this script in Nishang, a PowerShell script is downloaded from a specified URL address and executed. The URL can be specified with the “ScriptURL” parameter. The script specified by the URL can be run with the arguments specified by the “Arguments” parameter. If the executed script file is not to be saved on the victim machine, the “Nodownload” parameter is used.

Download-Execute-PS execution
Figure 2.7.1.1 – Download-Execute-PS
Meterpreter shell obtained
Figure 2.7.1.2 – Meterpreter Shell

Download_Execute

Using this script in Nishang, an executable file converted to a text file is converted back into a runnable application and executed. The only parameter used for this application is the “URL” parameter, and the file is downloaded from this URL and executed. The “exetotext.ps1” script in Nishang can be used to convert an executable file to a text file.

ExetoText
Figure 2.7.2.1 – ExetoText
Download_Execute
Figure 2.7.2.2 – Download_Execute
Meterpreter shell
Figure 2.7.2.3 – Meterpreter Shell

Execute-Command-MSSQL

Using this script in Nishang, xp_cmdshell is activated by specifying the username and password of the administrator account of the MSSQL Server service running on the remote server, and a PowerShell shell, SQL shell or CMD shell can be obtained from the victim machine. If Windows Authentication is used by MSSQL, Windows Authentication is performed and no username or password is required for this method. The parameters used for this script are described below.

Execute-Command-MSSQL
Figure 2.7.3.1 – Execute-Command-MSSQL

Execute-DNSTXT-Code

Using this script in Nishang, a shell can be obtained by leveraging TXT queries from the DNS server on the system. The script retrieves the payload shell code that enables the attacker machine to connect to the victim machine from DNS TXT records in Base64, decodes it and executes it. The connection is thus established. The Nishang “Out-DnsTxt” script can be used to place shell code in Base64 format in TXT records.

The script requires a minimum of 3 parameters: “shellcode32”, “shellcode64” and “AuthNS”. These parameters specify the domain names containing the TXT records where the 32-bit and 64-bit shell codes have been placed in Base64, and an authoritative nameserver.

Out-DnsTxt
Figure 2.7.4.1 – Out-DnsTxt
Base64 encoded text
Figure 2.7.4.2 – Base64 Text
DNS TXT records
Figure 2.7.4.3 – DNS TXT Records
Execute-DNSTXT-Code
Figure 2.7.4.4 – Execute-DNSTXT-Code
Meterpreter session
Figure 2.7.4.5 – Meterpreter Session

Out-RundllCommand

Using this script in Nishang, commands that can be run by the rundll32.exe application are generated. When the rundll32.exe application runs on the victim machine, a PowerShell session is obtained. After this script is run on the victim machine, a reverse PowerShell session is obtained from the victim machine on the attacker machine where a listening mode has been started. The parameters for this script are as follows.

Out-RundllCommand
Figure 2.7.5.1 – Out-RundllCommand
Netcat listener
Figure 2.7.5.2 – Netcat

Gather

After a machine is compromised by an attacker, the attacker will need to gather information in order to proceed with the post-exploitation phases. To make progress on the system, information such as host details, credentials and similar data will need to be collected. Nishang provides some methods for information gathering. These methods are described below.

Check-VM

Using this script in Nishang, information about the virtual environment in which the machine resides is obtained by examining the registry. The script examines registry keys and running services to check for Hyper-V, VMWare, Virtual PC, VirtualBox, Xen and QEMU.

Check-VM
Figure 2.8.1.1 – Check-VM

Copy-VSS

Using this script in Nishang, copies are taken of the SAM, NTDS.DIT databases and SYSTEM hive used to store credentials on Windows systems. These files cannot be opened because they run continuously alongside the operating system. They can therefore be examined by taking a copy with this script.

Copy-VSS
Figure 2.8.2.1 – Copy-VSS
SAM contents
Figure 2.8.2.2 – SAM Contents

FireBuster

Using this script in Nishang, it can be determined which port is used to exit the network. The script first performs a port scan and finds open ports. It then sends all packets to FireListener, which has started a listening mode.

FireBuster
Figure 2.8.3.1 – FireBuster

FireListener

FireListener starts a listening mode to which FireBuster can send packets.

FireListener
Figure 2.8.4 – FireListener

Get-Information

This script in Nishang is used to obtain information from the target. It extracts information from the registry and using certain commands.

Get-Information
Figure 2.8.5.1 – Get-Information

Get-LSASecret

In Windows systems, LSA (Local Security Authority) is responsible for managing security policies, controlling user accounts, controlling user logins and storing account passwords. LSA-related secrets on Windows systems are stored in encrypted form under “HKLM:\SECURITY\Policy\Secrets”. Using this script, LSA secret information is retrieved from the “HKLM:\SECURITY\Policy\Secrets” location. The Enable-TSDuplicateToken script can be used to retrieve the keys for decrypting the ciphertext. Since this information is held in the 32-bit registry, PowerShell must be run as 32-bit and these scripts must be run via 32-bit PowerShell. Since this script will access HKLM:\SECURITY records, it must be run with SYSTEM rights.

Get-LSASecret
Figure 2.8.6.1 – Get-LSASecret

Show-TargetScreen

Using this script in Nishang, the victim computer’s desktop can be monitored using MJPEG (Motion JPEG). After the script is run, the victim’s desktop can be viewed in real time in MJPEG-compatible browsers. By running this script with the “Reverse” parameter, the browser can redirect the connection to the machine specified by the “IPAddress” and “Port” parameters. To run this script, client-side attack scripts from Nishang will need to be leveraged — the command for this script will be run as an argument of the client-side attack scripts. A listener needs to be started to redirect the connection; “powercat” can be used for this. After starting a listening mode with powercat, the victim machine’s screen image arrives at the TCP port specified with powercat when the client-side attack script is run. This allows real-time monitoring by connecting to this port via the HTTP protocol using an MJPEG-compatible browser.

Powercat
Figure 2.8.7.1 – Powercat
Show-TargetScreen
Figure 2.8.7.2 – Show-TargetScreen

Get-PassHashes

On Windows systems, user-defined passwords are hashed using the LM and NTLM hash algorithms and stored in the SAM database and, if Active Directory Domain Services is installed, in the NTDS.DIT database. Using this script, the ID, LM and NTLM hash information belonging to users is retrieved from the SAM and NTDS.DIT databases. The “PSObjectFormat” parameter can be used to retrieve the information in PSObject format.

Get-PassHashes
Figure 2.8.8.1 – Get-PassHashes

Get-PassHints

On Windows systems, a user setting a password can specify a hint that will appear on the first incorrect attempt to remind them of their password. These hints are stored in the HKLM:\SAM hive. The Get-PassHints script is used to retrieve these hints. Results are printed to the screen in the format “<username>:<hint>”.

Get-PassHints
Figure 2.8.9.1 – Get-PassHints

Get-WLAN-Keys

Using this script, saved WLAN keys can be retrieved in plaintext.

Get-WLAN-Keys
Figure 2.8.10.1 – Get-WLAN-Keys

Get-WebCredentials

In Windows systems, “Credential Manager” was introduced from Windows 7 onwards. In earlier versions, passwords were simply stored for easy access later, but the new version also brought features for backing up and restoring passwords. As a result of these operations, credentials are stored in “Windows Vault”, which is the storage area for the Credential Manager feature. Using this script, web credentials stored in Windows Vault can be retrieved.

Get-WebCredentials
Figure 2.8.11.1 – Get-WebCredentials
Windows Credential Manager
Figure 2.8.11.2 – Windows Credential Manager

Invoke-CredentialPhish

When this script is run on the PowerShell command line, an authentication window opens and does not disappear until the correct credentials are entered. The user’s credentials are thus obtained.

Invoke-CredentialPhish
Figure 2.8.12.1 – Invoke-CredentialPhish
Credential check code
Figure 2.8.12.2 – Credential Check Code

As seen in Figure 2.8.12.2, the retrieval of user credentials and the authentication check take place within a “while” loop. If authentication succeeds, execution exits the code block via “break”. Otherwise, the loop continues indefinitely.

Invoke-Mimikatz

Mimikatz is a tool developed by Benjamin Delpy that enables operations such as retrieving credentials from credential storage areas, retrieving certificates and establishing persistence (e.g. Pass-the-Ticket). This script loads the Mimikatz tool into memory via PowerShell and enables it to run. Post-exploitation operations can thereby be carried out on the system using Mimikatz. The parameters that can be used with this script are as follows.

Retrieving credentials via LSASS
Figure 2.8.13.1 – Retrieving Credentials via LSASS
Custom command
Figure 2.8.13.2 – Custom Command

Invoke-MimikatzWDigestDowngrade

On Windows 8.1, Windows Server 2012 R2 and later operating system versions, the passwords of logged-in users are not stored in plaintext. Using this script, a registry entry is created to enable the use of WDigest with logged-in user credentials. Plaintext passwords of logged-in users can thereby be obtained. After this script is run, the entry “UseLogonCredential” is added to “HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\WDigest” with a value of “1”.

After this script is run in PowerShell, the user’s session is terminated. After the user logs in again, running the “Invoke-Mimikatz” script yields the plaintext passwords of the logged-in users. In this way, the Mimikatz tool can retrieve plaintext passwords from Windows 8.1, Server 2012 R2 and later versions. If an RDP key is in use, this script also targets RDP users. The “RDP” parameter is used for this.

Plaintext password
Figure 2.8.14.1 – Plaintext Password

Invoke-Mimikittenz

This script is used to retrieve information from memory. The ReadProcessMemory() function runs using regex to retrieve information from the victim machine’s memory.

Invoke-Mimikittenz

Invoke-SSIDExfil

This script is used to create a Hosted Network with user credentials. After the script runs, a window opens for entering credentials. This window does not close until authentication succeeds and compels the entry of correct credentials. After correct credentials are entered, the username and password are encoded in plaintext using ROT13 and a network starts with an SSID in the format “ROT13(Domain):ROT13(username):ROT13(password)”. User credentials can thus be captured in plaintext using this method.

Credential entry
Figure 2.8.16.1 – Credential Entry
Network creation
Figure 2.8.16.2 – Network Creation
ROT13 decode
Figure 2.8.16.3 – ROT13 Decode

Keylogger

This script stores every character entered on the keyboard in encoded form in a file called “key.log” created in the %temp% directory. The recorded keystrokes can be saved to a specified user account on Pastebin, Google, TinyPaste, or all three simultaneously. All records saved in encoded form can be decoded using the “Parse_Key” script in Nishang.

Two parameters are required to run the script: “CheckURL” and “MagicString”. A web page link is specified with the “CheckURL” parameter. A text found within that page is specified with the “MagicString” parameter. If the specified text is present on the page, the keylogger will terminate. For this reason, the “CheckURL” parameter should be assigned a link whose content can be changed.

Keylogger
Figure 2.8.17.1 – Keylogger
key.log file contents
Figure 2.8.17.2 – key.log File

Additionally, if this script is run with the “ExfilOption” parameter, the keystrokes are exfiltrated to the specified address.

Data exfiltration
Figure 2.8.17.3 – Data Exfiltration
Sending data to specified address
Figure 2.8.17.4 – Sending Data to Specified Address

MITM

Invoke-Interceptor

Using this script, a proxy server is created, HTTP(S) requests are captured and saved to a file. When the script runs, if a port is not specified with the “ListenPort” parameter, a listening mode starts on port 8081 by default. Data obtained during listening is forwarded to the proxy server specified by the “ProxyServer” and “ProxyPort” parameters. Additionally, all requests captured during listening are saved to a file named “interceptor.log” created in the %temp% directory of the victim computer by default, unless a path is specified with the “LogFile” parameter. Other parameters related to this script are described below:

Invoke-Interceptor
Figure 2.9.1.1 – Invoke-Interceptor
Proxy server
Figure 2.9.1.2 – Proxy Server

You May Be Interested In These