PythonForWindows (PFW)

repository·master·Indexed 20 days ago

https://github.com/hakril/pythonforwindows

A library providing Pythonic abstractions and thin ctypes wrappers for Windows OS interaction on X86/X64 architectures. It features tools for process and thread manipulation, system information access via windows.system, Windows API wrappers through winproxy, native code execution with an embedded assembler, security descriptor and token management, digital signature verification via wintrust, and registry interaction. Additionally, it includes a debugger for external and local processes, symbol handling via dbghelp.dll, and ALPC-RPC client/server implementations.

Tokens
138.5K
Snippets
382
Records
660
Agent score
70%

What's inside PythonForWindows

  1. Overview of PythonForWindows

    master

    PythonForWindows (PFW) provides Pythonic abstractions for interacting with Windows OS features on X86/X64 architectures (supporting both 32-bit and 64-bit Python). It aims to thin the barrier between Python and native execution.

    Key features include:

    • An ALPC-RPC Client.
    • Generated ctypes definitions for Windows APIs.

    The library has no external dependencies but relies heavily on the built-in ctypes module.

  2. Overview of the windows module

    master

    The windows module is the primary entry point installed via setup.py. It provides access to the current state of the Windows system and offers several submodules for interfacing with Windows and executing native code.

    Core objects available directly in the windows namespace include:

    • system: An instance of windows.winobject.system.System representing system state.
    • current_process: An instance of windows.winobject.process.CurrentProcess representing the active process.
    • current_thread: An instance of windows.winobject.process.CurrentThread representing the active thread.
  3. Use the windows.alpc module for Advanced Local Procedure Call

    master

    The windows.alpc module provides classes for sending and receiving Advanced Local Procedure Call (ALPC) messages over an ALPC port. It allows for high-performance inter-process communication (IPC) on Windows by providing abstractions for messages, ports, and client/server roles.

    Key components include:

    • Messages: Representing the data being transferred.
    • Ports: The communication channels.
    • Client/Server roles: Classes to implement either the requester (AlpcClient) or the listener (AlpcServer).
  4. Access Windows security structures via windows.security

    master

    The windows.security module provides access to Windows security primitives, specifically SecurityDescriptor, Acl (Access Control List), and Ace (Access Control Entry) structures. It also serves as the official entry point for retrieving the Token class, which is deeply related to security descriptors.

    import windows.security
    
    # Accessing the Token class via the security module
    token_class = windows.security.Token
    print(token_class)
  5. Use the windows.winproxy module for Windows API access

    master

    The windows.winproxy module provides a Pythonic wrapper around various Windows API DLLs. It is built upon ctypes and windows.generated_def.winfuncs.

    Key behaviors:

    • Lazy Loading: The relevant DLL is loaded automatically the first time an API from that DLL is called.
    • Argument Passing: Parameters can be passed using either ordinals (positional arguments) or keyword arguments.
    • Mandatory Parameters: Some functions are 'transparent proxies' where all parameters are mandatory. If a parameter with the default value NeededParameter is not provided, the call will raise a TypeError.
    • Error Handling: If an API call fails, the module raises a subclass of WindowsError (specifically WinproxyError).
  6. Create Python functions from native code with windows.native_exec

    master

    The windows.native_exec module allows you to create Python functions that call native code. It also includes a simple assembler for x86 and x64 architectures.

    Key components:

    • windows.native_exec.create_function: Used to create functions from native code.
    • windows.native_exec.cpuid: Interface for native CPUID instructions.
    • windows.native_exec.simple_x86: X86 assembler.
    • windows.native_exec.simple_x64: X64 assembler.
    • windows.native_exec.nativeutils: Pre-defined native utility functions.
  7. Use windows.pipe for Inter-Process Communication

    master
    The windows.pipe module provides a simplified wrapper around multiprocessing.PipeConnection for Inter-Process Communication (IPC). It improves upon the standard library implementation by allowing you to send and receive objects using a pipe name in a single line and providing a context manager for managing pipe connections safely.
  8. Perform ALPC-based Windows RPC calls

    master

    The windows.rpc module provides tools for Microsoft Remote Procedure Call (MS-RPC) operations using ALPC. It allows you to find interface endpoints, connect to them, bind to interfaces, perform calls, and handle NDR (Network Data Representation) marshalling/unmarshalling.

    Key capabilities include:

    • Finding interface endpoints via UUID.
    • Connecting to an endpoint and binding to an interface.
    • Marshalling data using the windows.rpc.ndr module.
  9. WMI Data Model Abstractions

    master

    The WMI module uses several specialized classes to represent the hierarchy of WMI data:

    • WmiNamespace: Represents a specific WMI namespace (e.g., root\cimv2).
    • WmiObject: Represents a single WMI instance. It supports item access via __getitem__ and __setitem__, and can be invoked via __call__.
    • WmiEnumeration: Represents a collection of WMI objects. It supports iteration (__iter__) and invocation (__call__).
    • WmiCallResult: Represents the result of a WMI method call.
  10. Represent ALPC messages and attributes with AlpcMessage and MessageAttribute

    master
    To communicate via ALPC, you use AlpcMessage to encapsulate the data and MessageAttribute to define metadata or specific properties associated with the message. These classes are the building blocks for the data payload sent between clients and servers.
  11. Access the Event Log API via windows.system.event_log

    master
    The Event Log functionality is managed through an EvtlogManager instance. This instance is accessible via the windows.system.event_log attribute on the System object. The API provides abstractions for interacting with Windows Event Logs, including channels, publishers, and individual events, while maintaining some underlying WinAPI subtleties.