def open_storage(storage_type, password, columns, *, sieve=None, repres=None): storage_full_path = f'{USBStorage._STORAGE_BASE}/{storage_type}.7z' if not os.path.isfile(storage_full_path): print_critical(f'Storage not found: "{storage_full_path}"') return try: out = _7zip_unpack(storage_full_path, password) except USBRipError as e: print_critical(str(e), errcode=e.errors['errcode'], initial_error=e.errors['initial_error']) return if 'Everything is Ok' in out: base_filename = re.search(r'([^\n ]*\.json)', _7zip_list(storage_full_path, password), re.MULTILINE).group(1) json_file = f'{USBStorage._STORAGE_BASE}/{base_filename}' USBEvents.open_dump(json_file, columns, sieve=sieve, repres=repres) os.remove(json_file) else: print_critical('Undefined behaviour while unpacking storage', initial_error=out)
def open_storage(storage_type, password, columns, *, sieve=None, repres=None): storage_full_path = '{}/{}.7z'.format(USBStorage._STORAGE_BASE, storage_type) if not os.path.isfile(storage_full_path): print_critical( 'Storage not found: \'{}\''.format(storage_full_path)) return try: out = _7zip_unpack(storage_full_path, password) except USBRipError as e: print_critical(str(e), errcode=e.errors['errcode'], initial_error=e.errors['initial_error']) return if 'Everything is Ok' in out: base_filename = re.search(r'Extracting\s*(.*?$)', out, re.MULTILINE).group(1) json_file = '{}/{}'.format(USBStorage._STORAGE_BASE, base_filename) USBEvents.open_dump(json_file, columns, sieve=sieve, repres=repres) os.remove(json_file) else: print_critical('Undefined behaviour while unpacking storage', initial_error=out)
def _get_violation_events(sieve, input_auth, attributes, indent): try: auth = _process_auth_list(input_auth, indent) except json.decoder.JSONDecodeError as e: raise USBRipError( 'Failed to decode authorized device list (JSON)', errors={'initial_error': str(e)} ) if not attributes: attributes = auth.keys() ue = USBEvents() if not ue: return None for event in ue._all_events: try: if any(event[key] not in vals and event[key] is not None for key, vals in zip(attributes, auth.values())): ue._violations.append(event) except KeyError as e: raise USBRipError( 'No such attribute in authorized device list', errors={'initial_error': str(e)} ) return _filter_events(ue._violations, sieve)
def main(): if not len(sys.argv) > 1: print(BANNER + '\n') usbrip_arg_error() arg_parser = get_arg_parser() args = arg_parser.parse_args() if 'quiet' in args and not args.quiet: if cfg.ISATTY: print(BANNER + '\n') else: print(f'# Done as: usbrip {" ".join(sys.argv[1:])}') else: cfg.QUIET = True # ---------------------------------------------------------- # ------------------------- Banner ------------------------- # ---------------------------------------------------------- if args.subparser == 'banner': print(BANNER) # ---------------------------------------------------------- # ----------------------- USB Events ----------------------- # ---------------------------------------------------------- elif args.subparser == 'events' and args.ue_subparser: if os.geteuid() != 0: sys.exit('Permission denied. Retry with sudo') sieve, repres = validate_ue_args(args) # ------------------- USB Events History ------------------- if args.ue_subparser == 'history': timing.begin() ue = USBEvents(args.file) if ue: ue.event_history(args.column, sieve=sieve, repres=repres) # -------------------- USB Events Open --------------------- elif args.ue_subparser == 'open': timing.begin() USBEvents.open_dump(args.input, args.column, sieve=sieve, repres=repres) # ------------------ USB Events GenAuth ------------------- elif args.ue_subparser == 'genauth': timing.begin() ue = USBEvents(args.file) if ue: if ue.generate_auth_json(args.output, args.attribute, sieve=sieve): usbrip_internal_error() else: usbrip_internal_error() # ----------------- USB Events Violations ------------------ elif args.ue_subparser == 'violations': timing.begin() ue = USBEvents(args.file) if ue: ue.search_violations(args.input, args.attribute, args.column, sieve=sieve, repres=repres) # ---------------------------------------------------------- # ---------------------- USB Storage ----------------------- # ---------------------------------------------------------- elif args.subparser == 'storage' and args.us_subparser: if os.geteuid() != 0: sys.exit('Permission denied. Retry with sudo') if any(not os.path.exists(p) for p in ('/opt/usbrip/', '/var/opt/usbrip', '/usr/local/bin/usbrip')): sys.exit( 'The "storage" module can only be used when usbrip is installed via "installer.sh" - https://git.io/fjDPT' ) sieve, repres = validate_us_args(args) timing.begin() config_parser = get_config_parser() us = USBStorage() # -------------------- USB Storage List -------------------- if args.us_subparser == 'list': us.list_storage(args.storage_type, config_parser[args.storage_type]['password']) # -------------------- USB Storage Open -------------------- elif args.us_subparser == 'open': us.open_storage(args.storage_type, config_parser[args.storage_type]['password'], args.column, sieve=sieve, repres=repres) # ------------------- USB Storage Update ------------------- elif args.us_subparser == 'update': if us.update_storage(args.storage_type, config_parser[args.storage_type]['password'], input_auth=args.input, attributes=args.attribute, compression_level=args.lvl, sieve=sieve): usbrip_internal_error() # ------------------- USB Storage Create ------------------- elif args.us_subparser == 'create': if us.create_storage(args.storage_type, config_parser[args.storage_type]['password'], input_auth=args.input, attributes=args.attribute, compression_level=args.lvl, sieve=sieve): usbrip_internal_error() # ------------------- USB Storage Passwd ------------------- elif args.us_subparser == 'passwd': us.change_password(args.storage_type, compression_level=args.lvl) # ---------------------------------------------------------- # ------------------------ USB IDs ------------------------- # ---------------------------------------------------------- elif args.subparser == 'ids' and args.ui_subparser: validate_ui_args(args) timing.begin() ui = USBIDs() # --------------------- USB IDs Search --------------------- if args.ui_subparser == 'search': ui.search_ids(args.vid, args.pid, offline=args.offline) # -------------------- USB IDs Download -------------------- elif args.ui_subparser == 'download': try: usb_ids = ui.prepare_database(offline=False) except USBRipError as e: print_critical(str(e), errcode=e.errors['errcode'], initial_error=e.errors['initial_error']) else: usb_ids.close() else: subparser = ' ' + args.subparser + ' ' usbrip_arg_error(f'Choose one of the usbrip {args.subparser} actions', subparser=subparser)
def main(): if not len(sys.argv) > 1: print(BANNER + '\n') usbrip_arg_error() parser = cmd_line_options() args = parser.parse_args() if 'quiet' in args and not args.quiet: if cfg.ISATTY: print(BANNER + '\n') else: print(f'# Done as: usbrip {" ".join(sys.argv[1:])}') else: cfg.QUIET = True # ---------------------------------------------------------- # ------------------------- Banner ------------------------- # ---------------------------------------------------------- if args.subparser == 'banner': print(BANNER) # ---------------------------------------------------------- # ----------------------- USB Events ----------------------- # ---------------------------------------------------------- elif args.subparser == 'events' and args.ue_subparser: sieve, repres = validate_ue_args(args) # ------------------- USB Events History ------------------- if args.ue_subparser == 'history': timing.begin() ueh = USBEvents(args.file) if ueh: ueh.event_history( args.column, sieve=sieve, repres=repres ) # -------------------- USB Events Open --------------------- elif args.ue_subparser == 'open': timing.begin() USBEvents.open_dump( args.input, args.column, sieve=sieve, repres=repres ) # ------------------ USB Events Gen Auth ------------------- elif args.ue_subparser == 'gen_auth': timing.begin() ueg = USBEvents(args.file) if ueg: if ueg.generate_auth_json( args.output, args.attribute, sieve=sieve ): usbrip_internal_error() else: usbrip_internal_error() # ----------------- USB Events Violations ------------------ elif args.ue_subparser == 'violations': timing.begin() uev = USBEvents(args.file) if uev: uev.search_violations( args.input, args.attribute, args.column, sieve=sieve, repres=repres ) # ---------------------------------------------------------- # ---------------------- USB Storage ----------------------- # ---------------------------------------------------------- elif args.subparser == 'storage' and args.us_subparser: if os.geteuid() != 0: sys.exit('Permission denied. Retry with sudo') sieve, repres = validate_us_args(args) timing.begin() us = USBStorage() # -------------------- USB Storage List -------------------- if args.us_subparser == 'list': us.list_storage( args.storage_type, args.password ) # -------------------- USB Storage Open -------------------- elif args.us_subparser == 'open': us.open_storage( args.storage_type, args.password, args.column, sieve=sieve, repres=repres ) # ------------------- USB Storage Update ------------------- elif args.us_subparser == 'update': if us.update_storage( args.storage_type, args.password, input_auth=args.input, attributes=args.attribute, compression_level=args.lvl, sieve=sieve ): usbrip_internal_error() # ------------------- USB Storage Create ------------------- elif args.us_subparser == 'create': if us.create_storage( args.storage_type, password=args.password, input_auth=args.input, attributes=args.attribute, compression_level=args.lvl, sieve=sieve ): usbrip_internal_error() # ------------------- USB Storage Passwd ------------------- elif args.us_subparser == 'passwd': us.change_password( args.storage_type, args.old, args.new, compression_level=args.lvl ) # ---------------------------------------------------------- # ------------------------ USB IDs ------------------------- # ---------------------------------------------------------- elif args.subparser == 'ids' and args.ui_subparser: validate_ui_args(args) timing.begin() ui = USBIDs() # --------------------- USB IDs Search --------------------- if args.ui_subparser == 'search': ui.search_ids( args.vid, args.pid, offline=args.offline ) # -------------------- USB IDs Download -------------------- elif args.ui_subparser == 'download': try: usb_ids = ui.prepare_database(offline=False) except USBRipError as e: print_critical(str(e), errcode=e.errors['errcode'], initial_error=e.errors['initial_error']) else: usb_ids.close() else: subparser = ' ' + args.subparser + ' ' usbrip_arg_error(f'Choose one of the usbrip {args.subparser} actions', subparser=subparser)
def _get_history_events(sieve): ue = USBEvents() if not ue: return None return _filter_events(ue._all_events, sieve)