From e10355276be24048eba2c1ef0f4c87b09d862c04 Mon Sep 17 00:00:00 2001 From: DoronZ Date: Sun, 20 Feb 2022 00:44:11 +0200 Subject: [PATCH] refactor: use the correct CF enums --- src/rpcclient/rpcclient/client.py | 5 ++++ src/rpcclient/rpcclient/darwin/client.py | 17 +++++++------ src/rpcclient/rpcclient/darwin/consts.py | 26 ++++++++++++++++++++ src/rpcclient/rpcclient/darwin/ioregistry.py | 3 ++- src/rpcclient/rpcclient/darwin/media.py | 3 ++- src/rpcclient/rpcclient/darwin/network.py | 3 ++- src/rpcclient/rpcclient/darwin/symbol.py | 8 +++--- src/rpcclient/rpcclient/structs/consts.py | 2 ++ 8 files changed, 53 insertions(+), 14 deletions(-) diff --git a/src/rpcclient/rpcclient/client.py b/src/rpcclient/rpcclient/client.py index d9a7346c..18dfeef3 100644 --- a/src/rpcclient/rpcclient/client.py +++ b/src/rpcclient/rpcclient/client.py @@ -6,6 +6,7 @@ import os import sys import typing +from enum import Enum from select import select from socket import socket @@ -124,6 +125,10 @@ def call(self, address: int, argv: typing.List[int] = None) -> Symbol: free_list = [] for arg in argv: + if isinstance(arg, Enum): + # if it's a python enum, then first get its real value and only then attempt to convert + arg = arg.value + tmp = arg if isinstance(arg, bool): diff --git a/src/rpcclient/rpcclient/darwin/client.py b/src/rpcclient/rpcclient/darwin/client.py index 7e989d3d..ebc87245 100644 --- a/src/rpcclient/rpcclient/darwin/client.py +++ b/src/rpcclient/rpcclient/darwin/client.py @@ -7,7 +7,7 @@ from rpcclient.client import Client from rpcclient.darwin import objective_c_class -from rpcclient.darwin.consts import kCFNumberSInt64Type, kCFNumberDoubleType +from rpcclient.darwin.consts import kCFNumberSInt64Type, kCFNumberDoubleType, CFStringEncoding, kCFAllocatorDefault from rpcclient.darwin.fs import DarwinFs from rpcclient.darwin.ioregistry import IORegistry from rpcclient.darwin.location import Location @@ -19,6 +19,7 @@ from rpcclient.darwin.structs import utsname from rpcclient.darwin.symbol import DarwinSymbol from rpcclient.exceptions import RpcClientException, MissingLibraryError +from rpcclient.structs.consts import RTLD_NOW IsaMagic = namedtuple('IsaMagic', 'mask value') ISA_MAGICS = [ @@ -37,7 +38,7 @@ def __init__(self, sock, sysname: str, hostname: str, port: int = None): super().__init__(sock, sysname, hostname, port) self._dlsym_global_handle = -2 # RTLD_GLOBAL - if 0 == self.dlopen("/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation", 2): + if 0 == self.dlopen("/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation", RTLD_NOW): raise MissingLibraryError('failed to load CoreFoundation') self._cf_types = { @@ -106,7 +107,8 @@ def cf(self, o: object): # assuming it's already a cfobject return o elif isinstance(o, str): - return self.symbols.CFStringCreateWithCString(0, o, 0) + return self.symbols.CFStringCreateWithCString(kCFAllocatorDefault, o, + CFStringEncoding.kCFStringEncodingMacRoman) elif isinstance(o, bytes): return self.symbols.CFDataCreate(0, o, len(o)) elif isinstance(o, bool): @@ -117,17 +119,17 @@ def cf(self, o: object): elif isinstance(o, int): with self.safe_malloc(8) as buf: buf[0] = o - return self.symbols.CFNumberCreate(0, kCFNumberSInt64Type, buf) + return self.symbols.CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt64Type, buf) elif isinstance(o, float): with self.safe_malloc(8) as buf: buf.poke(struct.pack(' None: return None def _decode_cfstr(self) -> str: - ptr = self._client.symbols.CFStringGetCStringPtr(self, 0) + ptr = self._client.symbols.CFStringGetCStringPtr(self, CFStringEncoding.kCFStringEncodingMacRoman) if ptr: return ptr.peek_str() with self._client.safe_malloc(4096) as buf: - if not self._client.symbols.CFStringGetCString(self, buf, 4096, 0): + if not self._client.symbols.CFStringGetCString(self, buf, 4096, CFStringEncoding.kCFStringEncodingMacRoman): raise CfSerializationError('CFStringGetCString failed') return buf.peek_str() def _decode_cfbool(self) -> bool: - return bool(self._client.symbols.CFBooleanGetValue(self, 0)) + return bool(self._client.symbols.CFBooleanGetValue(self)) def _decode_cfnumber(self) -> int: with self._client.safe_malloc(200) as buf: diff --git a/src/rpcclient/rpcclient/structs/consts.py b/src/rpcclient/rpcclient/structs/consts.py index fb60155b..982b066a 100644 --- a/src/rpcclient/rpcclient/structs/consts.py +++ b/src/rpcclient/rpcclient/structs/consts.py @@ -37,3 +37,5 @@ DT_LNK = 10 DT_SOCK = 12 DT_WHT = 14 + +RTLD_NOW = 2