Implement NtCreateNamedPipeFile with ctypes in Python

0

I'm trying to implement NtCreateNamedPipeFile undocumented function from NTAPI with ctypes, but I can't get it to work (I have very little experience with ctypes).

First, if I provide all arguments from function definition, I get an error "ValueError: Procedure probably called with too many arguments (4 bytes in excess)".

If I remove NamedPipeFileHandle argument (assuming that the handle might be what this function returns), I get "OSError: [WinError 87] The parameter is incorrect".

I tried to tweak types and arguments in my implementation, but no matter what I do, I get the same WinError 87. It's not very descriptive, so it's hard to understand what I'm doing wrong here (probably, many things).

Here's the definitions:

ULONG_PTR = ctypes.wintypes.WPARAM
FILE_READ_DATA = 0x00000001
FILE_WRITE_DATA = 0x00000002
SYNCHRONIZE = 0x00100000
FILE_READ_ATTRIBUTES = 0x80
FILE_CREATE = 0x00000002

def NtError(status):
    err = ntdll.RtlNtStatusToDosError(status)
    return ctypes.WinError(err)


class IO_STATUS_BLOCK(ctypes.Structure):
    class _STATUS(ctypes.Union):
        _fields_ = (('Status',  LONG),
                    ('Pointer', LPVOID))
    _anonymous_ = '_Status',
    _fields_ = (('_Status', _STATUS),
                ('Information', ULONG_PTR))


class UNICODE_STRING(ctypes.Structure):
    _fields_ = (('Length', USHORT),
                ('MaximumLength', USHORT),
                ('Buffer', LPWSTR))

class OBJECT_ATTRIBUTES(ctypes.Structure):
    _fields_ = (
        ('Length', ULONG),
        ('RootDirectory', HANDLE),
        ('ObjectName', UNICODE_STRING),
        ('Attributes', ULONG),
    )

ntdll = ctypes.WinDLL('ntdll')

def _decl(name, ret=None, args=()):
    fn = getattr(ntdll, name)
    fn.restype = ret
    fn.argtypes = args
    return fn

NtCreateNamedPipeFile = _decl('NtCreateNamedPipeFile', LONG,
                              (
                                   POINTER(HANDLE),
                                   DWORD,
                                   POINTER(OBJECT_ATTRIBUTES),
                                   POINTER(IO_STATUS_BLOCK),
                                   ULONG,
                                   ULONG,
                                   ULONG,
                                   BOOL,
                                   BOOL,
                                   BOOL,
                                   ULONG,
                                   ULONG,
                                   ULONG,
                                   LARGE_INTEGER))

And the call itself:

handle = HANDLE()
iosb = IO_STATUS_BLOCK()

us_pipe_name = UNICODE_STRING()
us_pipe_name.Buffer = '\\??\\pipe\\' + name
us_pipe_name.Length = len(us_pipe_name.Buffer)
us_pipe_name.MaximumLength = len(us_pipe_name.Buffer)

obj_attrs = OBJECT_ATTRIBUTES()
obj_attrs.ObjectName = us_pipe_name

status = NtCreateNamedPipeFile(
    # ctypes.byref(handle),  # NamedPipeFileHandle
    FILE_READ_DATA | SYNCHRONIZE,  # DesiredAccess
    ctypes.byref(obj_attrs),  # ObjectAttributes
    ctypes.byref(iosb),  # IoStatusBlock
    0,  # ShareAccess
    FILE_CREATE,  # CreateDisposition
    0,  # CreateOptions
    0,  # WriteModeMessage
    0,  # ReadModeMessage
    1,  # NonBlocking
    1,  # MaxInstances
    1024,  # InBufferSize
    1024,  # OutBufferSize
    0,  # DefaultTimeout
)

if status != 0:
    exception = NtError(status)
    raise exception

What am I doing wrong here?

python
winapi
ctypes
named-pipes
asked on Stack Overflow Jun 12, 2018 by Ray P. • edited Jun 12, 2018 by Ray P.

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0