def cli_profile(pids, process_names, color, request, response): """ Sniff using CFNetowrkDiagnostics.mobileconfig profile. This requires the specific Apple profile to be installed for the sniff to work. """ lockdown = LockdownClient() for entry in OsTraceService(lockdown).syslog(): if entry.label is None or entry.label.subsystem != 'com.apple.CFNetwork' or \ entry.label.category != 'Diagnostics': continue if pids and (entry.pid not in pids): continue if process_names and (posixpath.basename(entry.filename) not in process_names): continue lines = entry.message.split('\n') if len(lines) < 2: continue buf = '' if lines[1].strip().startswith( 'Protocol Enqueue: request') and request: # request print('➡️ ', end='') fields = parse_fields(entry.message) buf += f'{fields["Message"]}\n' for name, value in fields.items(): if name in ('Protocol Enqueue', 'Request', 'Message'): continue buf += f'{name}: {value}\n' elif lines[1].strip().startswith( 'Protocol Received: request') and response: # response print('⬅️ ', end='') fields = parse_fields(entry.message) buf += f'{fields["Response"]} ({fields["Protocol Received"]})\n' for name, value in fields.items(): if name in ('Protocol Received', 'Response'): continue buf += f'{name}: {value}\n' if buf: if color: print( highlight(buf, HttpLexer(), TerminalTrueColorFormatter(style='autumn'))) else: print(buf)
def list_devices(nocolor): """ list connected devices """ mux = usbmux.USBMux() mux.process() connected_devices = [] for device in mux.devices: udid = device.serial lockdown = LockdownClient(udid) connected_devices.append(lockdown.all_values) print_object(connected_devices, colored=not nocolor, default=lambda x: '<non-serializable>')
def getLockdownClient(udid): ret = None logging.info(f'udid={udid}') try: devs = usbmux.list_devices() for d in devs: if udid == d.serial: ret = LockdownClient(udid) break else: logging.error(f'Device({udid}) not found.') except: logging.exception('exception on list devices') logging.info(f'ret={ret}') return ret
def udid(ctx, param, value): if '_PYMOBILEDEVICE3_COMPLETE' in os.environ: # prevent lockdown connection establishment when in autocomplete mode return return LockdownClient(udid=value)
def lockdown(): """ Creates a new lockdown client for each test. """ return LockdownClient()
def __init__(self, lockdown: LockdownClient): self.logger = logging.getLogger(__name__) self.lockdown = lockdown self.service = lockdown.start_service(self.SERVICE_NAME)
def main(out, pids, images, headers, request, response, unique): """ Simple utility to filter out the HAR log messages from device's syslog, assuming HAR logging is enabled. If not, please use the `harlogger` binary beforehand. """ shown_set = set() har = { 'log': { 'version': '0.1', 'creator': { 'name': 'remote-har-listener', 'version': '0.1', }, 'entries': [], } } lockdown = LockdownClient() os_trace_service = OsTraceService(lockdown) try: for line in os_trace_service.syslog(): if line.label is None: continue if line.label.identifier != 'HAR': continue image = os.path.basename(line.image_name) pid = line.pid message = line.message if (len(pids) > 0) and (pid not in pids): continue if (len(images) > 0) and (image not in images): continue try: entry = json.loads(message) except json.decoder.JSONDecodeError: print(f'failed to decode: {message}') continue # artificial HAR information extracted from syslog line entry['image'] = image entry['pid'] = pid if unique: entry_hash = (image, pid, entry['request']['method'], entry['request']['url']) if entry_hash in shown_set: continue shown_set.add(entry_hash) show_har_entry(entry, filter_headers=headers, show_request=request, show_response=response) har['log']['entries'].append(entry) except KeyboardInterrupt: if out: out.write(json.dumps(har, indent=4))
def cli_preference(udid, out, pids, process_names, images, headers, request, response, unique): """ Sniff using the secret com.apple.CFNetwork.plist configuration. This sniff includes the request/response body as well but requires the device to be jailbroken for the sniff to work """ shown_set = set() har = { 'log': { 'version': '0.1', 'creator': { 'name': 'remote-har-listener', 'version': '0.1', }, 'entries': [], } } lockdown = LockdownClient(udid=udid) os_trace_service = OsTraceService(lockdown) try: for line in os_trace_service.syslog(): if line.label is None: continue if line.label.category != 'HAR': continue image = os.path.basename(line.image_name) pid = line.pid message = line.message if (len(pids) > 0) and (pid not in pids): continue if (len(images) > 0) and (image not in images): continue if process_names and (posixpath.basename(line.filename) not in process_names): continue try: entry = json.loads(message) except json.decoder.JSONDecodeError: print(f'failed to decode: {message}') continue # artificial HAR information extracted from syslog line entry['image'] = image entry['pid'] = pid entry['filename'] = line.filename if unique: entry_hash = (image, pid, entry['request']['method'], entry['request']['url']) if entry_hash in shown_set: continue shown_set.add(entry_hash) show_har_entry(entry, filter_headers=headers, show_request=request, show_response=response) har['log']['entries'].append(entry) except KeyboardInterrupt: if out: out.write(json.dumps(har, indent=4))