def __init__(self):
        Context.__init__(self)

        pyPkgName = config.global_config_get("pkg_name")
        self.__pkgName = String(pyPkgName)
        self.__pkg_mgr = PackageManager(pyPkgName)
        self.__resolver = ContentResolver()
    def __execve(self, mu, filename_ptr, argv_ptr, envp_ptr):
        filename = memory_helpers.read_utf8(mu, filename_ptr)
        ptr = argv_ptr
        params = []
        logger.info("execve run")
        while True:
            off = memory_helpers.read_ptr(mu, ptr)
            param = memory_helpers.read_utf8(mu, off)
            if (len(param) == 0):
                break
            params.append(param)
            ptr += 4
        #
        logging.warning("execve %s %r" % (filename, params))
        cmd = " ".join(params)

        pkg_name = config.global_config_get("pkg_name")
        pm = "pm path %s" % (pkg_name, )
        if (cmd.find(pm) > -1):
            output = "package:/data/app/%s-1.apk" % pkg_name
            logger.info("write to stdout [%s]" % output)
            os.write(1, output.encode("utf-8"))
            sys.exit(0)
        #
        else:
            raise NotImplementedError()
Exemple #3
0
 def getString(emu, resolver, s1):
     print("call getString %r %r" % (resolver, s1))
     pys1 = s1.get_py_string()
     if (pys1 == "android_id"):
         android_id = config.global_config_get("android_id")
         return String(android_id)
     #
     raise NotImplementedError()
     return String("")
    def _open_file(self, filename, mode):
        #define O_RDONLY 00000000
        #define O_WRONLY 00000001
        #define O_RDWR 00000002
        #ifndef O_CREAT
        #define O_CREAT 00000100
        # Special cases, such as /dev/urandom.

        if filename == '/dev/urandom':
            logger.info("File opened '%s'" % filename)
            return self._store_fd('/dev/urandom', None, 'urandom')
        #

        file_path = self.translate_path(filename)
        if (filename.startswith("/proc/")):
            #simulate proc file system
            parent = os.path.dirname(file_path)
            if (not os.path.exists(parent)):
                os.makedirs(parent)
            #
            
            pobj = pcb.get_pcb()
            pid = pobj.get_pid()
            filename2 = filename.replace(str(pid), "self")
            #TODO: move pid to config

            map_path = "/proc/self/maps"
            if (filename2 == map_path):
                with open(file_path, "w") as f:
                    self.__memory_map.dump_maps(f)
                #
            #
            cmdline_path = "/proc/self/cmdline"
            if (filename2 == cmdline_path):
                with open(file_path, "w") as f:
                    #TODO put to config
                    content = config.global_config_get("pkg_name")
                    f.write(content)
                #
            #
        #

        if os.path.isfile(file_path):
            logger.info("File opened '%s'" %filename)
            flags = os.O_RDWR
            if hasattr(os, "O_BINARY"):
                flags |= os.O_BINARY
            if (mode & 100):
                flags | os.O_CREAT
            #
            if (mode & 2000):
                flags | os.O_APPEND
            #
            return self._store_fd(filename, file_path, androidemu.utils.misc_utils.my_open(file_path, flags))
        else:
            logger.warning("File does not exist '%s'" % filename)
            return -1
Exemple #5
0
    def _handle_fstat64(self, mu, fd, buf_ptr):
        """
        These functions return information about a file. No permissions are required on the file itself, but-in the
        case of stat() and lstat() - execute (search) permission is required on all of the directories in path that
        lead to the file.

        fstat() is identical to stat(), except that the file to be stat-ed is specified by the file descriptor fd.
        """
        stats = os.fstat(fd)
        uid = config.global_config_get("uid")
        file_helpers.stat_to_memory2(mu, buf_ptr, stats, uid)

        return 0
    def _handle_fstatat64(self, mu, dirfd, pathname_ptr, buf, flags):
        """
        int fstatat(int dirfd, const char *pathname, struct stat *buf, int flags);

        If the pathname given in pathname is relative, then it is interpreted relative to the directory referred
        to by the file descriptor dirfd (rather than relative to the current working directory of the calling process,
        as is done by stat(2) for a relative pathname).

        If pathname is relative and dirfd is the special value AT_FDCWD,
        then pathname is interpreted relative to the current working directory of the calling process (like stat(2)).

        If pathname is absolute, then dirfd is ignored.

        flags can either be 0, or include one or more of the following flags ..

        On success, fstatat() returns 0. On error, -1 is returned and errno is set to indicate the error.
        """
        pathname = memory_helpers.read_utf8(mu, pathname_ptr)

        logger.info("fstatat64 patename=[%s]" % pathname)
        if not pathname.startswith('/'):
            raise NotImplementedError(
                "Directory file descriptor has not been implemented yet.")

        if not flags == 0:
            if flags & 0x100:  # AT_SYMLINK_NOFOLLOW
                pass
            if flags & 0x800:  # AT_NO_AUTOMOUNT
                pass
            # raise NotImplementedError("Flags has not been implemented yet.")

        logger.info("File fstatat64 '%s'" % pathname)
        pathname = self.translate_path(pathname)

        if not os.path.exists(pathname):
            logger.warning('> File was not found.')
            return -1

        logger.info('> File was found.')

        stat = os.stat(pathname)
        # stat = os.stat(path=file_path, dir_fd=None, follow_symlinks=False)
        uid = config.global_config_get("uid")
        file_helpers.stat_to_memory2(mu, buf, stat, uid)

        return 0
    def __init__(self, mu, syscall_handler):
        self._mu = mu

        self._syscall_handler = syscall_handler
        self._syscall_handler.set_handler(0x2, "fork", 0, self.__fork)
        self._syscall_handler.set_handler(0x0B, "execve", 3, self.__execve)
        self._syscall_handler.set_handler(0x14, "getpid", 0, self._getpid)
        self._syscall_handler.set_handler(0x1A, "ptrace", 4, self.__ptrace)
        self._syscall_handler.set_handler(0x25, "kill", 2, self.__kill)
        self._syscall_handler.set_handler(0x2A, "pipe", 1, self.__pipe)
        self._syscall_handler.set_handler(0x43, "sigaction", 3,
                                          self._handle_sigaction)
        self._syscall_handler.set_handler(0x4E, "gettimeofday", 2,
                                          self._handle_gettimeofday)
        self._syscall_handler.set_handler(0x72, "wait4", 4, self.__wait4)
        self._syscall_handler.set_handler(0x74, "sysinfo", 1, self.__sysinfo)
        self._syscall_handler.set_handler(0x78, "clone", 5, self.__clone)
        self._syscall_handler.set_handler(0xAC, "prctl", 5, self._handle_prctl)
        self._syscall_handler.set_handler(0xAF, "sigprocmask", 3,
                                          self._handle_sigprocmask)
        self._syscall_handler.set_handler(0xBE, "vfork", 0, self.__vfork)
        self._syscall_handler.set_handler(0xC7, "getuid32", 0, self._get_uid)
        self._syscall_handler.set_handler(0xE0, "gettid", 0, self._gettid)
        self._syscall_handler.set_handler(0xF0, "futex", 6, self._handle_futex)
        self._syscall_handler.set_handler(0x10c, "tgkill", 3,
                                          self._handle_tgkill)
        self._syscall_handler.set_handler(0x107, "clock_gettime", 2,
                                          self._handle_clock_gettime)
        self._syscall_handler.set_handler(0x119, "socket", 3, self._socket)
        self._syscall_handler.set_handler(0x11a, "bind", 3, self._bind)
        self._syscall_handler.set_handler(0x11b, "connect", 3, self._connect)
        self._syscall_handler.set_handler(0x126, "setsockopt", 5,
                                          self._setsockopt)
        self._syscall_handler.set_handler(0x159, "getcpu", 3, self._getcpu)
        self._syscall_handler.set_handler(0x166, "dup3", 3, self.__dup3)
        self._syscall_handler.set_handler(0x167, "pipe2", 2, self.__pipe2)
        self._syscall_handler.set_handler(0x178, "process_vm_readv", 6,
                                          self.__process_vm_readv)
        self._syscall_handler.set_handler(0x180, "getrandom", 3,
                                          self._getrandom)
        self._clock_start = time.time()
        self._clock_offset = randint(50000, 100000)
        self._sig_maps = {}
        self.__pcb = pcb.get_pcb()
        self._process_name = config.global_config_get(
            "pkg_name")  #"ChromiumNet10"
    def __init__(self, mu, syscall_handler):
        self._mu = mu
 
        self._syscall_handler = syscall_handler
        self._syscall_handler.set_handler(0x2, "fork", 0, self._fork)
        self._syscall_handler.set_handler(0x14, "getpid", 0, self._getpid)
        self._syscall_handler.set_handler(0x1A, "ptrace", 4, self.__ptrace)
        self._syscall_handler.set_handler(0x25, "kill", 2, self.__kill)
        self._syscall_handler.set_handler(0x2A, "pipe", 1, self._pipe)
        self._syscall_handler.set_handler(0x43, "sigaction", 3, self._handle_sigaction)
        self._syscall_handler.set_handler(0x4E, "gettimeofday", 2, self._handle_gettimeofday)
        self._syscall_handler.set_handler(0x72, "wait4", 4, self.__wait4)
        self._syscall_handler.set_handler(0x74, "sysinfo", 1, self.__sysinfo)
        self._syscall_handler.set_handler(0x78, "clone", 5, self.__clone)
        self._syscall_handler.set_handler(0xAC, "prctl", 5, self._handle_prctl)
        self._syscall_handler.set_handler(0xAF, "sigprocmask", 3, self._handle_sigprocmask)
        self._syscall_handler.set_handler(0xC7, "getuid32", 0, self._get_uid)
        self._syscall_handler.set_handler(0xE0, "gettid", 0, self._gettid)
        self._syscall_handler.set_handler(0xF0, "futex", 6, self._handle_futex)
        self._syscall_handler.set_handler(0x10c, "tgkill", 3, self._handle_tgkill)
        self._syscall_handler.set_handler(0x107, "clock_gettime", 2, self._handle_clock_gettime)
        self._syscall_handler.set_handler(0x119, "socket", 3, self._socket)
        self._syscall_handler.set_handler(0x11a, "bind", 3, self._bind)
        self._syscall_handler.set_handler(0x11b, "connect", 3, self._connect)
        self._syscall_handler.set_handler(0x126, "setsockopt", 5, self._setsockopt)
        self._syscall_handler.set_handler(0x14e, "faccessat", 4, self._faccessat)
        self._syscall_handler.set_handler(0x159, "getcpu", 3, self._getcpu)
        # self._syscall_handler.set_handler(0x180,"null1",0, self._null)
        self._syscall_handler.set_handler(0x167, "pipe2", 2, self.__pipe2)
        self._syscall_handler.set_handler(0x178, "process_vm_readv", 6, self.__process_vm_readv)
        self._syscall_handler.set_handler(0x180, "getrandom", 3, self._getrandom)
        self._clock_start = time.time()
        self._clock_offset = randint(1000, 2000)
        self._socket_id = 0x100000
        self._sockets = dict()
        self._sig_maps = {}
        #TODO read it from config file
        self._process_name = config.global_config_get("pkg_name")
Exemple #9
0
 def getHardwareAddress(self, emu):
     mac = config.global_config_get("mac")
     barr = bytearray(mac)
     arr = Array("B", barr)
     return arr
 def _get_uid(self, mu):
     uid = config.global_config_get("uid")
     return uid
    def _open_file(self, filename, mode):
        #define O_RDONLY 00000000
        #define O_WRONLY 00000001
        #define O_RDWR 00000002
        #ifndef O_CREAT
        #define O_CREAT 00000100
        # Special cases, such as /dev/urandom.

        if filename == '/dev/urandom':
            logger.info("File opened '%s'" % filename)
            #return self.__pcb.alloc_file_fd('/dev/urandom', None, 'urandom')
            raise NotImplementedError
        #

        file_path = self.translate_path(filename)
        if (filename.startswith("/proc/")):
            #simulate proc file system
            parent = os.path.dirname(file_path)
            if (not os.path.exists(parent)):
                os.makedirs(parent)
            #

            pobj = pcb.get_pcb()
            pid = pobj.get_pid()
            filename2 = filename.replace(str(pid), "self")
            #TODO: move pid to config

            map_path = "/proc/self/maps"
            if (filename2 == map_path):
                with open(file_path, "w") as f:
                    self.__memory_map.dump_maps(f)
                #
            #
            cmdline_path = "/proc/self/cmdline"
            if (filename2 == cmdline_path):
                with open(file_path, "w") as f:
                    #TODO put to config
                    content = config.global_config_get("pkg_name")
                    f.write(content)
                #
            #
            cgroup_path = "/proc/self/cgroup"
            if (filename2 == cgroup_path):
                with open(file_path, "w") as f:
                    #TODO put to config
                    uid = config.global_config_get("uid")
                    content = "2:cpu:/apps\n1:cpuacct:/uid/%d\n" % uid
                    f.write(content)
                #
            #

        #
        virtual_file = [
            "/dev/log/main", "/dev/log/events", "/dev/log/radio",
            "/dev/log/system", "/dev/input/event0"
        ]
        if (filename in virtual_file):
            d = os.path.dirname(file_path)
            if (not os.path.exists(d)):
                os.makedirs(d)
            #
            with open(file_path, "w") as f:
                pass
            #
        #
        if os.path.isfile(file_path):
            flags = os.O_RDWR
            if (mode & 100):
                flags |= os.O_CREAT
            #
            if (mode & 2000):
                flags |= os.O_APPEND
            #
            fd = androidemu.utils.misc_utils.my_open(file_path, flags)
            self.__pcb.add_fd(filename, file_path, fd)
            logger.info("openat return fd %d" % fd)
            self.__create_fd_link(fd, file_path)
            return fd
        else:
            logger.warning("File does not exist '%s'" % filename)
            return -1