GithubHelp home page GithubHelp logo

itm4n / vba-runpe Goto Github PK

View Code? Open in Web Editor NEW
787.0 27.0 180.0 5.49 MB

A VBA implementation of the RunPE technique or how to bypass application whitelisting.

License: MIT License

Python 4.74% VBA 95.26%
whitelist-bypass runpe vba-macros

vba-runpe's Introduction

VBA RunPE

Description

A simple yet effective implementation of the RunPE technique in VBA. This code can be used to run executables from the memory of Word or Excel. It is compatible with both 32 bits and 64 bits versions of Microsoft Office 2010 and above.

More info here:
https://itm4n.github.io/vba-runpe-part1/
https://itm4n.github.io/vba-runpe-part2/

Win10_x64_Office2016_x64_PowerShell

Usage 1 - PE file on disk

  1. In the Exploit procedure at the end of the code, set the path of the file you want to execute.
strSrcFile = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"

/!\ If you're using a 32 bits version of Microsoft Office on a 64 bits OS, you must specify 32 bits binaries.

strSrcFile = "C:\Windows\SysWOW64\cmd.exe"
strSrcFile = "C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe"
  1. Specify the command line arguments (optional).
strArguments = "-exec Bypass"

This will be used to form a command line equivalent to:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -exec Bypass
  1. (Optional) Enable View > Immediate Window (Ctrl+G) to check execution and error logs.

  2. Run the Exploit macro!

Usage 2 - Embedded PE

  1. Use pe2vba.py to convert a PE file to VBA. This way, it can be directly embedded into the macro.
user@host:~/Tools/VBA-RunPE$ ./pe2vba.py meterpreter.exe 
[+] Created file 'meterpreter.exe.vba'.
  1. Replace the following code in RunPE.vba with the the content of the .vba file which was generated in the previous step. The Python script converts the PE to VBA and applies the RunPE template automatically (no need to copy/paste manually).
' ================================================================================
'                                ~~~ EMBEDDED PE ~~~
' ================================================================================

' CODE GENRATED BY PE2VBA
' ===== BEGIN PE2VBA =====
Private Function PE() As String
    Dim strPE As String
    strPE = ""
    PE = strPE
End Function
' ===== END PE2VBA =====
  1. (Optional) Enable View > Immediate Window (Ctrl+G) to check execution and error logs.

  2. Run the Exploit macro!

/!\ When using an embedded PE, the macro will automatically switch to this mode because the PE() method will return a non-empty string.

Known issues

  • GetThreadContext() fails with error code 998.

You might get this error if you run this macro from a 64-bits version of Office. As a workaround, you can move the code to a module rather than executing it from the Word Object references. Thanks @joeminicucci for the tip.

================================================================================
[*] Source file: 'C:\Windows\System32\cmd.exe'
[*] Checking source PE...
[*] Creating new process in suspended state...
[*] Retrieving the context of the main thread...
    |__ GetThreadContext() failed (Err: 998)

I have no idea why this workaround works for the moment. I've investigated this a bit though. This error seems to be caused by the CONTEXT structure not being properly aligned in the 64-bits version. I noticed that the size of the structure is also incorrect ([VBA] LenB(CONTEXT) != [C++] sizeof(CONTEXT)) whereas it's fine in the 32-bits version. I have a working solution that allows the GetThreadContext() to return properly but then it breaks some other stuff further in the execution.

Edit 2019-12-15: the definition of the 64-bits version of the CONTEXT structure was indeed incorrect but fixing this didn't fix the bug. So, I implemented a workaround for the 64-bits version. I replaced the CONTEXT structure argument of the GetThreadContext() and SetThreadContext() functions with a Byte Array of the same size.

Edit 2019-12-17: I finally found the problem. My first assumption was correct, the CONTEXT structure must be 16-Bytes aligned in memory. This is something you can control in C by using align(16) in the definition of the structure but you can't control that in VBA. Therefore, GetThreadContext() and SetThreadContext() may "randomly" fail. Byte Arrays on the other hand seem to always be 16-Bytes aligned, that's why this workaround is effective but there is no guarantee, unless I reverse engineer the VBA interpreter/compiler and figure it out?!

  • LongPtr - User Defined Type Not Defined

If you get this error, it means that you are running the macro from an old version of Office (<=2007). The LongPtr type was introduced in VBA7 (Office 2010) along with the support of the 64-bits Windows API. It's very useful for handling pointers without having to worry about the architecture (32-bits / 64-bits).

As a workaround, you can replace all the LongPtr occurences with Long (32-bits) or LongLong (64-bits). Use Ctrl+H in your favorite text editor.

Credits

@hasherezade - Complete RunPE implementation (https://github.com/hasherezade/)

@Zer0Mem0ry - 32 bits RunPE written in C++ (https://github.com/Zer0Mem0ry/RunPE)

@DidierStevens - PE embedding in VBA

Misc

Tests

This code was tested on the following platforms:

  • Windows 7 Pro 32 bits + Office 2010 32 bits
  • Windows 7 Pro 64 bits + Office 2016 32 bits
  • Windows 2008 R2 64 bits + Office 2010 64 bits
  • Windows 10 Pro 64 bits + Office 2016 64 bits

Side notes

Here is a table of correspondence between some Win32 and VBA types:

C++ VBA Arch
BYTE Byte 32 & 64
WORD Integer 32 & 64
DWORD, ULONG, LONG Long 32 & 64
DWORD64 LongLong 64
HANDLE LongPtr(*) 32 & 64
LPSTR String 32 & 64
LPBYTE LongPtr(*) 32 & 64

(*) LongPtr is a "dynamic" type, it is 4 Bytes long in Office 32 bits and 8 Bytes long in Office 64 bits. https://msdn.microsoft.com/fr-fr/library/office/ee691831(v=office.14).aspx

vba-runpe's People

Contributors

itm4n avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

vba-runpe's Issues

Invalid NT Headers, Couldn't get thread context example code

Hi,
While trying VBA-RunPE, I've encountered these two issues:

Issue I) Invalid NT Headers
When setting strSrcFile to "C:\Windows\System32\cmd.exe", the program displayed Invalid NT Headers error.
Screenshot (14)

Issue I)
Then I decided to set strSrcFile to "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe", but this time I've got Couldn't get thread context example code error.
Screenshot (17)

Environment: default windows 10 x64, Microsoft Word 2010 64 bits

GetThreadContext() failed (Err: 998)

Hi, running
Office 365 16.0.12228.20322
Windows 10.0.18362 Build 18362

Can't seem to get it running properly. Any idea?

Thank you!

[*] Source file: 'C:\Windows\SysWOW64\cmd.exe'
[*] Checking source PE...
[-] You're trying to inject a 32 bits binary into a 64 bits process!
================================================================================
[*] Source file: 'C:\Windows\System32\cmd.exe'
[*] Checking source PE...
[*] Creating new process in suspended state...
[*] Retrieving the context of the main thread...
    |__ GetThreadContext() failed (Err:  998)
|__ GetThreadContext() failed (Err:  998)

Procedure too large error

Hello,

When using VBA-RunPE as instructed in the readme:

./pe2vba.py <name.exe>

and inputting the generated .vba into the editor and trying to run I am receiving a "compile error: procedure too large."

Is this expected?

Thanks!

Faile to PE file validation

First of all, Thanks for managing this project.
I tested on assessing the capability of the Windows Defender as VBA on EXCEL. and this project was the most helpful to me. but I found some issues with PE file validation.
The validation routine was perfect but when parsing the file to memory I saw an error occurred.
Fortunately, given my efforts, I was able to resolve the issues. so let me show you my code could you give me PR authority?

Feature request - auto imports

First of all thanks for making this great tool! Would it be possible to detect the neccessary imports and do that automatically? That would make it much easier to run other tools as well.

Thread context gets lost when called through AutoOpen

For Embedded PE mode, if I call the Exploit function directly my payload executes:

================================================================================
[+] Source file: embedded PE
[+] |__ Magic number is OK.
[+] |__ NT Header Signature is valid.
[*] |__ Machine type: 0x8664
[*] Current process: 'C:\Program Files\Microsoft Office\Root\Office16\WINWORD.EXE'
[+] Created new process in suspended state.
[-] |__ Couldn't get thread context.
================================================================================
[+] Source file: embedded PE
[+] |__ Magic number is OK.
[+] |__ NT Header Signature is valid.
[*] |__ Machine type: 0x8664
[*] Current process: 'C:\Program Files\Microsoft Office\Root\Office16\WINWORD.EXE'
[+] Created new process in suspended state.
[+] |__ Got thread context
[+] |__ Got image base address: 0x7FF6C00C0000
[+] Allocated memory for the source image at address: 0x140000000
[*] Writing PE headers
[+] Wrote PE Headers at: 0x140000000 (size: 584)
[*] Writing section '.text'
[*] |__ Image base: 0x140000000
[*] |__ Section virtual address: 0x1000
[*] |__ New address (base+virt.): 0x140001000
[*] |__ Raw data address (buffer): 0x259FC347610
[*] |__ Section size: 4608
[+] Wrote section '.text' at address 0x140001000 (size: 4608)
[*] Writing section '.rdata'
[*] |__ Image base: 0x140000000
[*] |__ Section virtual address: 0x3000
[*] |__ New address (base+virt.): 0x140003000
[*] |__ Raw data address (buffer): 0x259FC348810
[*] |__ Section size: 512
[+] Wrote section '.rdata' at address 0x140003000 (size: 512)
[*] Writing section '.mxzy'
[*] |__ Image base: 0x140000000
[*] |__ Section virtual address: 0x4000
[*] |__ New address (base+virt.): 0x140004000
[*] |__ Raw data address (buffer): 0x259FC348A10
[*] |__ Section size: 35328
[+] Wrote section '.mxzy' at address 0x140004000 (size: 35328)
[*] Modifying context to point to new image base
[*] |__ Where to write new image base address: 0xC802CCD010
[*] |__ Image base address: 0x140000000
[+] Wrote image base address 0x140000000 at address 0xC802CCD010
[*] Applying new context
[*] |__ Set new entry point: 0x140004000
[+] |__ Applied context to the new thread
[*] Resuming suspended process
[+] |__ RunPE complete, successfully resumed thread

Otherwise, if I call it through the AutoOpen function for example, the thread context gets lost:

[+] Source file: embedded PE
[+] |__ Magic number is OK.
[+] |__ NT Header Signature is valid.
[] |__ Machine type: 0x8664
[
] Current process: 'C:\Program Files\Microsoft Office\Root\Office16\WINWORD.EXE'
[+] Created new process in suspended state.
[-] |__ Couldn't get thread context.

I am running the project macro in Office 2016 as a Project, document attached vba:

image

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.