Esempio n. 1
0
def main():
    try:
        args = parse_cmdline()
        logger.debug('args: {}'.format(args))

        if args['-i'] == 'The first HCI device':
            args['-i'] = HCI.get_default_hcistr()  # May raise IndexError

        init(args['-i'])

        if args['-m'] == 'br':
            br_scanner = BRScanner(args['-i'])
            br_scanner.inquiry(inquiry_len=args['--inquiry-len'])
        elif args['-m'] == 'lmp':
            LMPScanner(args['-i']).scan(args['BD_ADDR'])
        elif args['-m'] == 'le':
            LEScanner(args['-i']).scan(args['--timeout'],
                                       args['--le-scan-type'], args['--sort'])
        elif args['-m'] == 'sdp':
            SDPScanner(args['-i']).scan(args['BD_ADDR'])
        elif args['-m'] == 'gatt':
            GATTScanner(args['-i']).scan(args['BD_ADDR'], args['--addr-type'],
                                         args['--include-descriptor'])
        elif args['-m'] == 'stack':
            StackScanner(args['-i']).scan(args['BD_ADDR'])
        elif args['-m'] == 'vuln':
            VulnScanner(args['-i']).scan(args['BD_ADDR'], args['--addr-type'])
        else:
            logger.error('Invalid scan mode')
    except IndexError:
        logger.error('There is no available HCI device')
    except BluetoothError as e:
        logger.error('{}'.format(e))
    except RuntimeError as e:
        logger.error('{}'.format(e))
    except (BTLEException, ValueError) as e:
        logger.error('{}'.format(e))
        if 'le on' in str(e):
            print('        No BLE adapter? or missing sudo ?')
    except KeyboardInterrupt:
        logger.info(args['-m'].upper() + " scan canceled\n")
Esempio n. 2
0
def main():
    try:
        args = parse_cmdline()
        logger.debug("__main__.main(), args: {}".format(args))

        if not args['--adv']:
            if args['-i'] == 'The first HCI device':
                exitcode, output = subprocess.getstatusoutput(
                    'systemctl start bluetooth.service')
                if exitcode != 0:
                    logger.error("Failed to start bluetooth.service")
                    sys.exit(exitcode)
                try:
                    args['-i'] = HCI.get_default_hcistr(
                    )  # May raise IndexError
                except IndexError:
                    logger.error('There is no available HCI device')
                    exit(-1)

            init_hci(args['-i'])

        if args['-m'] == 'br':
            br_scanner = BRScanner(args['-i'])
            if args['--lmp-feature']:
                br_scanner.scan_lmp_feature(args['BD_ADDR'])
            else:
                br_scanner = BRScanner(args['-i'])
                br_scanner.inquiry(inquiry_len=args['--inquiry-len'])
        elif args['-m'] == 'le':
            if args['--adv']:
                dev_paths = get_microbit_devpaths()
                LEScanner(microbit_devpaths=dev_paths).sniff_adv(
                    args['--channel'])
            elif args['--ll-feature']:
                LEScanner(args['-i']).scan_ll_feature(args['BD_ADDR'],
                                                      args['--addr-type'])
            else:
                LEScanner(args['-i']).scan_devs(args['--timeout'],
                                                args['--scan-type'],
                                                args['--sort'])
        elif args['-m'] == 'sdp':
            SDPScanner(args['-i']).scan(args['BD_ADDR'])
        elif args['-m'] == 'gatt':
            GATTScanner(args['-i']).scan(args['BD_ADDR'], args['--addr-type'],
                                         args['--include-descriptor'])
        elif args['-m'] == 'stack':
            StackScanner(args['-i']).scan(args['BD_ADDR'])
        elif args['-m'] == 'vuln':
            VulnScanner(args['-i']).scan(args['BD_ADDR'], args['--addr-type'])
        else:
            logger.error('Invalid scan mode')
    except ValueError as e:
        logger.error("{}".format(e))
        exit(1)
    except BluetoothError as e:
        logger.error('{}'.format(e))
    except RuntimeError as e:
        logger.error('{}'.format(e))
    except (BTLEException, ValueError) as e:
        logger.error('{}'.format(e))
        if 'le on' in str(e):
            print("        No BLE adapter? or missing sudo ?")
    except KeyboardInterrupt:
        logger.info("Canceled\n")
Esempio n. 3
0
def main():
    try:
        args = parse_cmdline()
        logger.debug("main(), args: {}".format(args))

        if not args['--adv']:
            # 在不使用 microbit 的情况下,我们需要将选中的 hci 设备配置到一个干净的状态。

            if args['-i'] == 'The default HCI device':
                # 当 user 没有显示指明 hci 设备情况下,我们需要自动获取一个可用的 hci
                # 设备。注意这个设备不一定是 hci0。因为系统中可能只有 hci1,而没有 hci0。
                try:
                    args['-i'] = HCI.get_default_hcistr()
                except IndexError:
                    logger.error('No available HCI device')
                    exit(-1)

            init_hci(args['-i'])

        scan_result = None
        if args['-m'] == 'br':
            br_scanner = BRScanner(args['-i'])
            if args['--lmp-feature']:
                br_scanner.scan_lmp_feature(args['BD_ADDR'])
            else:
                br_scanner = BRScanner(args['-i'])
                br_scanner.inquiry(inquiry_len=args['--inquiry-len'])
        elif args['-m'] == 'le':
            if args['--adv']:
                dev_paths = get_microbit_devpaths()
                LeScanner(microbit_devpaths=dev_paths).sniff_adv(
                    args['--channel'])
            elif args['--ll-feature']:
                LeScanner(args['-i']).scan_ll_feature(args['BD_ADDR'],
                                                      args['--addr-type'],
                                                      args['--timeout'])
            elif args['--smp-feature']:
                LeScanner(args['-i']).detect_pairing_feature(
                    args['BD_ADDR'], args['--addr-type'], args['--timeout'])
            else:
                scan_result = LeScanner(args['-i']).scan_devs(
                    args['--timeout'], args['--scan-type'], args['--sort'])
        elif args['-m'] == 'sdp':
            SDPScanner(args['-i']).scan(args['BD_ADDR'])
        elif args['-m'] == 'gatt':
            scan_result = GattScanner(args['-i'],
                                      args['--io-capability']).scan(
                                          args['BD_ADDR'], args['--addr-type'])
        # elif args['-m'] == 'stack':
        #     StackScanner(args['-i']).scan(args['BD_ADDR'])
        elif args['--clean']:
            BlueScanner(args['-i'])
            clean(BlueScanner(args['-i']).hci_bdaddr, args['BD_ADDR'])
        else:
            logger.error('Invalid scan mode')

        # Prints scan result
        if scan_result is not None:
            print()
            print()
            print(
                blue("----------------" + scan_result.type + " Scan Result" +
                     "----------------"))
            scan_result.print()
            scan_result.store()
    # except (RuntimeError, ValueError, BluetoothError) as e:
    except (RuntimeError, ValueError) as e:
        logger.error("{}: {}".format(e.__class__.__name__, e))
        traceback.print_exc()
        exit(1)
    except (BTLEException) as e:
        logger.error(
            str(e) +
            ("\nNo BLE adapter or missing sudo?" if 'le on' in str(e) else ""))
    except KeyboardInterrupt:
        if args != None and args['-i'] != None:
            output = subprocess.check_output(' '.join(
                ['hciconfig', args['-i'], 'reset']),
                                             stderr=STDOUT,
                                             timeout=60,
                                             shell=True)
        print()
        logger.info("Canceled\n")