Example #1
0
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)
Example #2
0
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)