<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
    <channel>
        <title>Powershell on Monish Kumar&#39;s Blog</title>
        <link>https://itsmonish.pages.dev/tags/powershell/</link>
        <description>Recent content in Powershell on Monish Kumar&#39;s Blog</description>
        <generator>Hugo -- gohugo.io</generator>
        <language>en-us</language>
        <lastBuildDate>Sat, 26 Apr 2025 17:51:33 +0530</lastBuildDate><atom:link href="https://itsmonish.pages.dev/tags/powershell/index.xml" rel="self" type="application/rss+xml" /><item>
        <title>Forensics - Obfuscation Station</title>
        <link>https://itsmonish.pages.dev/blog/huntress-ctf-2024/forensics-obfuscation-station/</link>
        <pubDate>Sat, 26 Apr 2025 17:51:33 +0530</pubDate>
        
        <guid>https://itsmonish.pages.dev/blog/huntress-ctf-2024/forensics-obfuscation-station/</guid>
        <description>&lt;h1 id=&#34;obfuscation-station&#34;&gt;Obfuscation Station
&lt;/h1&gt;&lt;h2 id=&#34;challenge-statement&#34;&gt;Challenge Statement
&lt;/h2&gt;&lt;p&gt;Author: @resume&lt;/p&gt;
&lt;p&gt;You&amp;rsquo;ve reached the Obfuscation Station!&lt;/p&gt;
&lt;p&gt;Can you decode this PowerShell to find the flag?&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Archive password: &lt;code&gt;infected-station&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Attachment: &lt;a class=&#34;link&#34; href=&#34;https://itsmonish.pages.dev/others/huntressctf-2024/obfuscation-station/challenge.zip&#34; &gt;challenge.zip&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;solution&#34;&gt;Solution
&lt;/h2&gt;&lt;p&gt;Extracting the archive, we have a powershell script with the following contents:&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;https://itsmonish.pages.dev/images/huntressctf-2024/obfuscation-station/1.png&#34;
	
	
	
	loading=&#34;lazy&#34;
	
		alt=&#34;chal.ps1&#34;
	
	
&gt;&lt;/p&gt;
&lt;p&gt;The script seems to decode a base64 data, decompresses it to a stream and converts it to a string and then extracts some characters from an environment variable and joins them. I thought &amp;lsquo;iex&amp;rsquo; and I was right.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;https://itsmonish.pages.dev/images/huntressctf-2024/obfuscation-station/2.png&#34;
	
	
	
	loading=&#34;lazy&#34;
	
		alt=&#34;iex&#34;
	
	
&gt;&lt;/p&gt;
&lt;p&gt;So I tried to put the string into a variable and output it. But the script kept throwing errors at me. So I wrote another script mimicking the same logic, but cleaner. Putting them at &lt;a class=&#34;link&#34; href=&#34;https://itsmonish.pages.dev/others/huntressctf-2024/obfuscation-station/solve.ps1&#34; &gt;solve.ps1&lt;/a&gt;, and executing it, gave the flag.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;https://itsmonish.pages.dev/images/huntressctf-2024/obfuscation-station/3.png&#34;
	
	
	
	loading=&#34;lazy&#34;
	
		alt=&#34;flag&#34;
	
	
&gt;&lt;/p&gt;
</description>
        </item>
        <item>
        <title>Malware - Eco Friendly</title>
        <link>https://itsmonish.pages.dev/blog/huntress-ctf-2024/malware-eco-friendly/</link>
        <pubDate>Sat, 26 Apr 2025 17:51:33 +0530</pubDate>
        
        <guid>https://itsmonish.pages.dev/blog/huntress-ctf-2024/malware-eco-friendly/</guid>
        <description>&lt;h1 id=&#34;eco-friendly&#34;&gt;Eco-Friendly
&lt;/h1&gt;&lt;h2 id=&#34;challenge-statement&#34;&gt;Challenge Statement
&lt;/h2&gt;&lt;p&gt;Author: @JohnHammond&lt;/p&gt;
&lt;p&gt;This sample is good for the environment!&lt;/p&gt;
&lt;p&gt;Attachment: &lt;a class=&#34;link&#34; href=&#34;https://itsmonish.pages.dev/others/huntressctf-2024/eco-friendly/eco_friendly&#34; &gt;eco_friendly&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;solution&#34;&gt;Solution
&lt;/h2&gt;&lt;p&gt;For this challenge, we are given a powershell script, or I should say, one very long line of powershell. Opening the given file we can see an iex (Invoke-Expression) cmdlet that will execute whatever command follows it.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;https://itsmonish.pages.dev/images/huntressctf-2024/eco-friendly/1.png&#34;
	
	
	
	loading=&#34;lazy&#34;
	
		alt=&#34;first half&#34;
	
	
&gt;&lt;/p&gt;
&lt;p&gt;But the argument for the cmdlet seems to a string with random numbers in curly braces. If we scroll down for some time, we can find the end of the string with a option &lt;code&gt;-f&lt;/code&gt; which is used to format strings in powershell.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;https://itsmonish.pages.dev/images/huntressctf-2024/eco-friendly/2.png&#34;
	
	
	
	loading=&#34;lazy&#34;
	
		alt=&#34;second half&#34;
	
	
&gt;&lt;/p&gt;
&lt;p&gt;What follows the &lt;code&gt;-f&lt;/code&gt; is lots of environment variables with an index number. Which means the script uses, fixed environment variables that is uniform across all Windows machine to construct the payload from the individual characters indexed from said environment variables. The random numbers in curly braces are just positional parameters. This type of technique is used to evade detection from anti-malware solutions which may scan for suspicious keywords.&lt;/p&gt;
&lt;p&gt;Now that we know this, we can just replace the &lt;code&gt;iex&lt;/code&gt; at the starting with a &lt;code&gt;Write-Host&lt;/code&gt; cmdlet to write whatever the string will be constructed to rather than executing it.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;https://itsmonish.pages.dev/images/huntressctf-2024/eco-friendly/3.png&#34;
	
	
	
	loading=&#34;lazy&#34;
	
		alt=&#34;payload&#34;
	
	
&gt;&lt;/p&gt;
&lt;p&gt;Doing that, saving it to &lt;a class=&#34;link&#34; href=&#34;https://itsmonish.pages.dev/others/huntressctf-2024/eco-friendly/payload.ps1&#34; &gt;payload.ps1&lt;/a&gt; and executing the script, reveals another level of obfuscation with the same technique. So putting that in file &lt;a class=&#34;link&#34; href=&#34;https://itsmonish.pages.dev/others/huntressctf-2024/eco-friendly/stage1&#34; &gt;stage1&lt;/a&gt; and modifying the &lt;code&gt;iex&lt;/code&gt;  to &lt;code&gt;Write-Host&lt;/code&gt; in &lt;a class=&#34;link&#34; href=&#34;https://itsmonish.pages.dev/others/huntressctf-2024/eco-friendly/stage1_modified.ps1&#34; &gt;stage1_modified.ps1&lt;/a&gt; and executing again leads to another level of same obfuscation. Repeating the same with &lt;a class=&#34;link&#34; href=&#34;https://itsmonish.pages.dev/others/huntressctf-2024/eco-friendly/stage2&#34; &gt;stage2&lt;/a&gt; and &lt;a class=&#34;link&#34; href=&#34;https://itsmonish.pages.dev/others/huntressctf-2024/eco-friendly/stage2_modified.ps1&#34; &gt;stage2_modified.ps1&lt;/a&gt; leads to another level of obfuscation. Doing it again with &lt;a class=&#34;link&#34; href=&#34;https://itsmonish.pages.dev/others/huntressctf-2024/eco-friendly/stage3&#34; &gt;stage3&lt;/a&gt; and &lt;a class=&#34;link&#34; href=&#34;https://itsmonish.pages.dev/others/huntressctf-2024/eco-friendly/stage3_modified.ps1&#34; &gt;stage3_modified.ps1&lt;/a&gt; finally gave away the flag as a comment.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;https://itsmonish.pages.dev/images/huntressctf-2024/eco-friendly/4.png&#34;
	
	
	
	loading=&#34;lazy&#34;
	
		alt=&#34;flag&#34;
	
	
&gt;&lt;/p&gt;
&lt;p&gt;I should have anticipated nested levels of obfuscation and written a script to get the process done, but still unsure of what it might hold, I did it manually. But got the flag at the end.&lt;/p&gt;
</description>
        </item>
        <item>
        <title>Malware - Palimpsest</title>
        <link>https://itsmonish.pages.dev/blog/huntress-ctf-2024/malware-palimpsest/</link>
        <pubDate>Sat, 26 Apr 2025 17:51:33 +0530</pubDate>
        
        <guid>https://itsmonish.pages.dev/blog/huntress-ctf-2024/malware-palimpsest/</guid>
        <description>&lt;h1 id=&#34;palimpsest&#34;&gt;Palimpsest
&lt;/h1&gt;&lt;h2 id=&#34;challenge-statement&#34;&gt;Challenge Statement
&lt;/h2&gt;&lt;p&gt;Author: Adam Rice (@adam.huntress)&lt;/p&gt;
&lt;p&gt;Our IT department was setting up a new workstation and started encountering some strange errors while installing software.&lt;/p&gt;
&lt;p&gt;The technician noticed a strange scheduled task and luckily backed it up and grabbed some log files before wiping the machine!&lt;/p&gt;
&lt;p&gt;Can you figure out what&amp;rsquo;s going on?&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;We&amp;rsquo;ve included the exported scheduled task and log files below.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The archive password is &lt;code&gt;infected-palimpsest&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Attachment: &lt;a class=&#34;link&#34; href=&#34;https://itsmonish.pages.dev/others/huntressctf-2024/palimpsest/Challenge.zip&#34; &gt;Challenge.zip&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;solution&#34;&gt;Solution
&lt;/h2&gt;&lt;p&gt;The attachment archive contains 3 event viewer logs (Application, Security and System) and a &lt;a class=&#34;link&#34; href=&#34;https://itsmonish.pages.dev/others/huntressctf-2024/palimpsest/Updater%20Service.xml&#34; &gt;Updater Service.xml&lt;/a&gt;. I first opened the XML file, which is the scheduled task that the challenge description tells about.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;https://itsmonish.pages.dev/images/huntressctf-2024/palimpsest/1.png&#34;
	
	
	
	loading=&#34;lazy&#34;
	
		alt=&#34;xml file&#34;
	
	
&gt;&lt;/p&gt;
&lt;p&gt;While most of the contents of the XML file is pretty much what you&amp;rsquo;d find in most scheduled tasks, I was more interested in the actions of the task. As we can see, it is a powershell line, that collects the DNS TXT records from the site &lt;code&gt;5aa456e4dbed10b.pyrchdata.com&lt;/code&gt;, base64 decodes it, builds a string from it and executes it. So I queries the DNS TXT records myself to find out base64 data in it.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;https://itsmonish.pages.dev/images/huntressctf-2024/palimpsest/2.png&#34;
	
	
	
	loading=&#34;lazy&#34;
	
		alt=&#34;base64 txt&#34;
	
	
&gt;&lt;/p&gt;
&lt;p&gt;Putting the contents in &lt;a class=&#34;link&#34; href=&#34;https://itsmonish.pages.dev/others/huntressctf-2024/palimpsest/txt_records&#34; &gt;txt_records&lt;/a&gt; and decoding it, yields a powershell script with some obfuscation.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;https://itsmonish.pages.dev/images/huntressctf-2024/palimpsest/3.png&#34;
	
	
	
	loading=&#34;lazy&#34;
	
		alt=&#34;stage1&#34;
	
	
&gt;&lt;/p&gt;
&lt;p&gt;The first brackets reference the shell ID which I&amp;rsquo;m quite sure is constructing &lt;code&gt;iex&lt;/code&gt; cmdlet. Putting that in &lt;a class=&#34;link&#34; href=&#34;https://itsmonish.pages.dev/others/huntressctf-2024/palimpsest/stage1_payload.ps1&#34; &gt;stage1_payload.ps1&lt;/a&gt; and modifying the &lt;code&gt;iex&lt;/code&gt; with &lt;code&gt;Write-Host&lt;/code&gt; should give us the next part of the payload.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;https://itsmonish.pages.dev/images/huntressctf-2024/palimpsest/4.png&#34;
	
	
	
	loading=&#34;lazy&#34;
	
		alt=&#34;stage2&#34;
	
	
&gt;&lt;/p&gt;
&lt;p&gt;Doing that does indeed reveals the next part of the payload. Now we still have an obfuscated script. The first line now tries to get a variable that matches the pattern &lt;code&gt;*mdr*&lt;/code&gt; and constructs something. I was pretty sure that is also &lt;code&gt;iex&lt;/code&gt; as well, but to be sure, I checked. Repeating the same trick we move on to the next stage of payload.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;https://itsmonish.pages.dev/images/huntressctf-2024/palimpsest/5.png&#34;
	
	
	
	loading=&#34;lazy&#34;
	
		alt=&#34;stage3&#34;
	
	
&gt;&lt;/p&gt;
&lt;p&gt;Now this payload doesn&amp;rsquo;t have any base encoded strings, on the contrary &lt;a class=&#34;link&#34; href=&#34;https://itsmonish.pages.dev/others/huntressctf-2024/palimpsest/stage3_payload.ps1&#34; &gt;stage3_payload.ps1&lt;/a&gt; does something else. But I couldn&amp;rsquo;t fully read it because of the garbled format strings. I decided to clean it and substitute those strings to their result, I had &lt;a class=&#34;link&#34; href=&#34;https://itsmonish.pages.dev/others/huntressctf-2024/palimpsest/stage3_cleaned.ps1&#34; &gt;stage3_cleaned.ps1&lt;/a&gt; which was more readable.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;https://itsmonish.pages.dev/images/huntressctf-2024/palimpsest/6.png&#34;
	
	
	
	loading=&#34;lazy&#34;
	
		alt=&#34;stage3 cleaned&#34;
	
	
&gt;&lt;/p&gt;
&lt;p&gt;We can now see clearly, that the payload gets the application event log with source as &lt;code&gt;mslnstaller&lt;/code&gt; (notice the typosquatting) and filters events with InstanceID within 40000 - 65000, extracts &amp;ldquo;data&amp;rdquo; from those logs and writes it to the a file named &lt;code&gt;flag.mp4&lt;/code&gt; in the AppData folder.&lt;/p&gt;
&lt;p&gt;Now we have use for those event log files, opening Application.evtx in windows event viewer and filtering events between 40000 - 65000 with source as &lt;code&gt;mslnstaller&lt;/code&gt; we have exactly 100 such logs.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;https://itsmonish.pages.dev/images/huntressctf-2024/palimpsest/7.png&#34;
	
	
	
	loading=&#34;lazy&#34;
	
		alt=&#34;event viewer logs&#34;
	
	
&gt;&lt;/p&gt;
&lt;p&gt;Every record out of those 100 logs had a data node with some text and binary data. It is fair to assume that the binary data is what is being written to &lt;code&gt;flag.mp4&lt;/code&gt; all we have to do is extract it.&lt;/p&gt;
&lt;p&gt;Now I can write powershell scirpts to some level, I didn&amp;rsquo;t know how to extract fields from saved log files in it at the time of solving this challenge. So converted the .evtx files to .csv using &lt;a class=&#34;link&#34; href=&#34;https://github.com/josephatmwanzia/evtx2csv&#34;  target=&#34;_blank&#34; rel=&#34;noopener&#34;
    &gt;this&lt;/a&gt; tool.&lt;/p&gt;
&lt;p&gt;Once I converted it, it was fairly easy for me to put a python script &lt;a class=&#34;link&#34; href=&#34;https://itsmonish.pages.dev/others/huntressctf-2024/palimpsest/extract_data.py&#34; &gt;extract_data.py&lt;/a&gt; together to extract the binary data and write to a file. The logic behind the script is simple. It opens the csv file and parses it using the &lt;code&gt;csv&lt;/code&gt; module and then checks each row for the condition required as in the powershell script. Then I used &lt;code&gt;unhexlify&lt;/code&gt; from &lt;code&gt;binascii&lt;/code&gt; module to convert the hex to actual binary data and wrote it to a file &lt;a class=&#34;link&#34; href=&#34;https://itsmonish.pages.dev/others/huntressctf-2024/palimpsest/from_evtx_data&#34; &gt;from_evtx_data&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Even though the powershell script tells us it is an mp4, I was still a little suspicious it might be something else. But when I ran &lt;code&gt;file&lt;/code&gt; utility on it, it revealed that it is indeed a MP4 video file. Opening it got me:&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;https://itsmonish.pages.dev/images/huntressctf-2024/palimpsest/8.png&#34;
	
	
	
	loading=&#34;lazy&#34;
	
		alt=&#34;free real estate&#34;
	
	
&gt;&lt;/p&gt;
&lt;p&gt;So it says &amp;ldquo;It&amp;rsquo;s free real estate&amp;rdquo; and yeah the flag is there too. I tried using OCR to get the flag from the video, but it had a few mistakes which I had to correct it manually.&lt;/p&gt;
</description>
        </item>
        
    </channel>
</rss>
