(self)
Checks root on device
Checks root on device
Checks root on device
def _check_requirements(self):  # pylint: disable=too-many-branches, too-many-statements
        """ Checks root on device
        """
        self._dev_emu = False
        self._is_root = False
        self._is_su = False
        self._alternate_su_binary = False

        if not self._device_serial:
            return

        if self._adb_available:
            # try some command
            date_res = self._do_adb_command('shell date')
            # adb not authorized
            if date_res and 'device unauthorized' in date_res:
                # kill adb daemon
                utils.do_shell_command('adb kill-server')
                utils.show_message_box(
                    'device not authorized! allow access from this computer on the device'
                )

            if date_res and 'no devices/emulators' in date_res:
                self._dev_emu = False
                return
            elif date_res and 'device not found' in date_res:
                self._dev_emu = False
                return
            else:
                self._dev_emu = True

            if self._dev_emu and date_res:
                try:
                    # if date was fine it should end with year
                    # Thu Feb 8 16:47:32 MST 2001
                    date_res = date_res.split(' ')
                    res_year = int(date_res[len(date_res) - 1])
                except ValueError:
                    return  # TODO: raise exceptions

            # try some su command to check for su binary
            res = self._do_adb_command('shell su -c date')
            if res and 'invalid' in res:
                res = self._do_adb_command('shell su 0 date')
                if res:
                    self._alternate_su_binary = True

            if res:
                try:
                    # if su date was fine it should end with year
                    # Thu Feb 8 16:47:32 MST 2001
                    su_res = res.split(' ')
                    res_year = int(su_res[len(su_res) - 1])
                    if res_year:
                        # su cmd is available
                        self._is_su = True

                        # check if both date results matches otherwise its no valid result
                        res_len = len(su_res)
                        date_len = len(date_res)
                        if su_res[res_len - 1] == date_res[date_len -
                                                           1]:  # year
                            if su_res[res_len - 2] == date_res[date_len -
                                                               2]:  # timezone
                                if su_res[res_len - 4] == date_res[date_len -
                                                                   4]:  # day
                                    if su_res[res_len - 5] == date_res[
                                            date_len - 5]:  # month
                                        self._is_root = True

                except ValueError:
                    pass

            res = self._do_adb_command('shell mount | grep system')
            if '/sbin/.magisk/block/system /' in res:
                self._syspart_name = '/sbin/.magisk/mirror/system'
            if '/system_root' in res:
                self._syspart_name = '/system_root'
                if '/sbin/.magisk/block/system_root /' in res:
                    self._syspart_name = '/sbin/.magisk/mirror/system_root'

            # check status of selinux
            res = self._do_adb_command('shell getenforce')
            if res:
                res = res.join(res.split())
                if res != 'Permissive' and res != 'Disabled':
                    self._do_adb_command('shell setenforce 0')

            # nox fix
            res = self.su_cmd('mount -o ro,remount ' + self._syspart_name)
            if res and 'invalid' in res:
                self._alternate_su_binary = True

            # no su -> try if the user is already root
            # on some emulators user is root
            if not self._is_su and self._dev_emu:
                res = self._do_adb_command('shell mount -o ro,remount ' + self._syspart_name)
                if res or res == '':
                    if res and 'not user mountable' in res:
                        # no root user
                        self._is_root = False
                    elif res == '':
                        # cmd executed fine
                        self._is_root = True
                    else:
                        # dont know some other output
                        self._is_root = False
                        # check for uid 0
                        res = self._do_adb_command('shell id')
                        # root should be 0
                        # https://superuser.com/questions/626843/does-the-root-account-always-have-uid-gid-0/626845#626845
                        self._is_root = 'uid=0' in res

            if self._dev_emu:
                # get some infos about the device and keep for later
                self._sdk_version = self._do_adb_command(
                    'shell getprop ro.build.version.sdk')
                if self._sdk_version:
                    self._sdk_version = self._sdk_version.join(
                        self._sdk_version.split())  # cleans '\r\n'
                self._android_version = self._do_adb_command(
                    'shell getprop ro.build.version.release')
                if self._android_version:
                    self._android_version = self._android_version.join(
                        self._android_version.split())

                try:
                    self._oreo_plus = (int(
                        self._android_version.split('.')[0]) >= 8)
                except ValueError:
                    try:
                        self._oreo_plus = (int(self._sdk_version) > 25)
                    except ValueError:
                        pass

                # fix some frida server problems
                # frida default port: 27042
                utils.do_shell_command('adb forward tcp:27042 tcp:27042')

            # check if we have pidof
            res = self._do_adb_command('shell pidof -s pidof')
            self._have_pidof = 'not found' not in res
            res = self._do_adb_command('shell killall')
            self._have_killall = 'not found' not in res

            # check for correct userid
            if self._is_root:
                res = self.su_cmd('id')
                # root should be 0
                # https://superuser.com/questions/626843/does-the-root-account-always-have-uid-gid-0/626845#626845
                self._is_root = 'uid=0' in res
def _check_requirements ( self ) : # pylint: disable=too-many-branches, too-many-statements self . _dev_emu = False self . _is_root = False self . _is_su = False self . _alternate_su_binary = False if not self . _device_serial : return if self . _adb_available : # try some command date_res = self . _do_adb_command ( 'shell date' ) # adb not authorized if date_res and 'device unauthorized' in date_res : # kill adb daemon utils . do_shell_command ( 'adb kill-server' ) utils . show_message_box ( 'device not authorized! allow access from this computer on the device' ) if date_res and 'no devices/emulators' in date_res : self . _dev_emu = False return elif date_res and 'device not found' in date_res : self . _dev_emu = False return else : self . _dev_emu = True if self . _dev_emu and date_res : try : # if date was fine it should end with year # Thu Feb 8 16:47:32 MST 2001 date_res = date_res . split ( ' ' ) res_year = int ( date_res [ len ( date_res ) - 1 ] ) except ValueError : return # TODO: raise exceptions # try some su command to check for su binary res = self . _do_adb_command ( 'shell su -c date' ) if res and 'invalid' in res : res = self . _do_adb_command ( 'shell su 0 date' ) if res : self . _alternate_su_binary = True if res : try : # if su date was fine it should end with year # Thu Feb 8 16:47:32 MST 2001 su_res = res . split ( ' ' ) res_year = int ( su_res [ len ( su_res ) - 1 ] ) if res_year : # su cmd is available self . _is_su = True # check if both date results matches otherwise its no valid result res_len = len ( su_res ) date_len = len ( date_res ) if su_res [ res_len - 1 ] == date_res [ date_len - 1 ] : # year if su_res [ res_len - 2 ] == date_res [ date_len - 2 ] : # timezone if su_res [ res_len - 4 ] == date_res [ date_len - 4 ] : # day if su_res [ res_len - 5 ] == date_res [ date_len - 5 ] : # month self . _is_root = True except ValueError : pass res = self . _do_adb_command ( 'shell mount | grep system' ) if '/sbin/.magisk/block/system /' in res : self . _syspart_name = '/sbin/.magisk/mirror/system' if '/system_root' in res : self . _syspart_name = '/system_root' if '/sbin/.magisk/block/system_root /' in res : self . _syspart_name = '/sbin/.magisk/mirror/system_root' # check status of selinux res = self . _do_adb_command ( 'shell getenforce' ) if res : res = res . join ( res . split ( ) ) if res != 'Permissive' and res != 'Disabled' : self . _do_adb_command ( 'shell setenforce 0' ) # nox fix res = self . su_cmd ( 'mount -o ro,remount ' + self . _syspart_name ) if res and 'invalid' in res : self . _alternate_su_binary = True # no su -> try if the user is already root # on some emulators user is root if not self . _is_su and self . _dev_emu : res = self . _do_adb_command ( 'shell mount -o ro,remount ' + self . _syspart_name ) if res or res == '' : if res and 'not user mountable' in res : # no root user self . _is_root = False elif res == '' : # cmd executed fine self . _is_root = True else : # dont know some other output self . _is_root = False # check for uid 0 res = self . _do_adb_command ( 'shell id' ) # root should be 0 # https://superuser.com/questions/626843/does-the-root-account-always-have-uid-gid-0/626845#626845 self . _is_root = 'uid=0' in res if self . _dev_emu : # get some infos about the device and keep for later self . _sdk_version = self . _do_adb_command ( 'shell getprop ro.build.version.sdk' ) if self . _sdk_version : self . _sdk_version = self . _sdk_version . join ( self . _sdk_version . split ( ) ) # cleans '\r\n' self . _android_version = self . _do_adb_command ( 'shell getprop ro.build.version.release' ) if self . _android_version : self . _android_version = self . _android_version . join ( self . _android_version . split ( ) ) try : self . _oreo_plus = ( int ( self . _android_version . split ( '.' ) [ 0 ] ) >= 8 ) except ValueError : try : self . _oreo_plus = ( int ( self . _sdk_version ) > 25 ) except ValueError : pass # fix some frida server problems # frida default port: 27042 utils . do_shell_command ( 'adb forward tcp:27042 tcp:27042' ) # check if we have pidof res = self . _do_adb_command ( 'shell pidof -s pidof' ) self . _have_pidof = 'not found' not in res res = self . _do_adb_command ( 'shell killall' ) self . _have_killall = 'not found' not in res # check for correct userid if self . _is_root : res = self . su_cmd ( 'id' ) # root should be 0 # https://superuser.com/questions/626843/does-the-root-account-always-have-uid-gid-0/626845#626845 self . _is_root = 'uid=0' in res