command line support (partial) via PEB stomping

This update include support to passing command line parameters to unmanaged exe via PEB stomping.
This technique is not working 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:
https://blog-30cm-tw.translate.goog/2020/08/windows-c-mainargc-argv.html?_x_tr_sl=auto&_x_tr_tl=en&_x_tr_hl=it&_x_tr_pto=wapp

However, 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.
This commit is contained in:
naksyn
2023-07-27 06:44:29 -07:00
parent 63ebe1c4ba
commit db1893910c
160 changed files with 70537 additions and 42 deletions
@@ -0,0 +1,71 @@
from . import windef
from . import winstructs
def bitness():
"""Return 32 or 64"""
import platform
bits = platform.architecture()[0]
return int(bits[:2])
# Use windows.current_process.bitness ? need to fix problem of this imported before the creation of windows.current_process
if bitness() == 32:
winstructs.CONTEXT = winstructs.CONTEXT32
winstructs.PCONTEXT = winstructs.PCONTEXT32
winstructs.LPCONTEXT = winstructs.LPCONTEXT32
winstructs.EXCEPTION_POINTERS = winstructs.EXCEPTION_POINTERS32
winstructs.PEXCEPTION_POINTERS = winstructs.PEXCEPTION_POINTERS32
winstructs.SYSTEM_MODULE = winstructs.SYSTEM_MODULE32
winstructs.SYSTEM_MODULE_INFORMATION = winstructs.SYSTEM_MODULE_INFORMATION32
winstructs.PALPC_PORT_ATTRIBUTES = winstructs.PALPC_PORT_ATTRIBUTES32
winstructs.ALPC_PORT_ATTRIBUTES = winstructs.ALPC_PORT_ATTRIBUTES32
winstructs.PORT_MESSAGE = winstructs.PORT_MESSAGE32
winstructs.PPORT_MESSAGE = winstructs.PPORT_MESSAGE32
# CFGMGR32
winstructs.IRQ_RESOURCE = winstructs.IRQ_RESOURCE_32
# Socket
windef.WSADATA = winstructs.WSADATA32
windef.INVALID_SOCKET = windef.INVALID_SOCKET32
else:
winstructs.CONTEXT = winstructs.CONTEXT64
winstructs.PCONTEXT = winstructs.PCONTEXT64
winstructs.LPCONTEXT = winstructs.LPCONTEXT64
winstructs.EXCEPTION_POINTERS = winstructs.EXCEPTION_POINTERS64
winstructs.PEXCEPTION_POINTERS = winstructs.PEXCEPTION_POINTERS64
winstructs.SYSTEM_MODULE = winstructs.SYSTEM_MODULE64
winstructs.SYSTEM_MODULE_INFORMATION = winstructs.SYSTEM_MODULE_INFORMATION64
winstructs.PALPC_PORT_ATTRIBUTES = winstructs.PALPC_PORT_ATTRIBUTES64
winstructs.ALPC_PORT_ATTRIBUTES = winstructs.ALPC_PORT_ATTRIBUTES64
winstructs.PORT_MESSAGE = winstructs.PORT_MESSAGE64
winstructs.PPORT_MESSAGE = winstructs.PPORT_MESSAGE64
# CFGMGR32
winstructs.IRQ_RESOURCE = winstructs.IRQ_RESOURCE_64
# Socket
windef.WSADATA = winstructs.WSADATA64
windef.INVALID_SOCKET = windef.INVALID_SOCKET64
from . import winfuncs
from . import windef
from . import interfaces
# Fuck it
from .winstructs import *
from .winfuncs import *
from .windef import *
from .interfaces import *
@@ -0,0 +1,22 @@
def pretty_print_ctypes_type(t):
format = "{0}"
if issubclass(t, ctypes.Array):
format = "[{0}" + "* {0}]".format(t._length_)
t = t._type_
if issubclass(t, ctypes._Pointer):
format = format.format("Pointer({0})")
t = t._type_
if issubclass(t, ctypes.Structure):
return format.format(":class:`{0}`".format(t.__name__))
return t
def autodoc_ctypes_struct(struct):
doc = ["fields:"]
for name, type in struct._fields_:
doc.append(" {0} -> {1}".format(name, pretty_print_ctypes_type(type)))
struct.__doc__ = "\n\n".join(doc)
return struct
@@ -0,0 +1,74 @@
import sys
if sys.version_info.major >= 3:
long = int
class Flag(long):
def __new__(cls, name, value):
return super(Flag, cls).__new__(cls, value)
def __init__(self, name, value):
self.name = name
def __repr__(self):
return "{0}({1:#x})".format(self.name, self)
# Custom __str__ removed for multiple reason
# Main one -> it breaks the json encoding of structure with flags :)
# Moving to a new politic -> if people want the name in a string use {x!r}
# The __str__ of security descriptor & guid will change soon as well :)
# __str__ = __repr__
# Fix pickling with protocol 2
def __getnewargs__(self, *args):
return self.name, long(self)
class StrFlag(str):
def __new__(cls, name, value):
if isinstance(value, cls):
return value
return super(StrFlag, cls).__new__(cls, value)
def __init__(self, name, value):
self.name = name
def __repr__(self):
return "{0}({1})".format(self.name, str.__repr__(self))
# __str__ = __repr__
# Fix pickling with protocol 2
def __getnewargs__(self, *args):
return self.name, str.__str__(self)
def make_flag(name, value):
if isinstance(value, (int, long)):
return Flag(name, value)
return StrFlag(name, value)
class FlagMapper(dict):
def __init__(self, *values):
self.update({x:x for x in values})
def __missing__(self, key):
return key
class FlagExatractor(object):
def __init__(self, attr, values):
self.attr = attr
self.attrsize = attr.size * 8
self.mapper = FlagMapper(*values)
def __get__(self, obj, type):
if obj is None:
return self
# Retrieve the real value
value = self.attr.__get__(obj)
generator = (1 << i for i in range(self.attrsize))
return [self.mapper[f] for f in generator if value & f]
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because one or more lines are too long