class OperateSample(object):
    """
    the class will save or get the benign or malware sample to mongo
    """

    def __init__(self):
        self.session = MongDBSession()

    def save_sample(self, apk_dir):
        """
        对apk中的源代码作分词,其中的词作为key,出现的次数作为value,存储到mongo中
        :param apk_dir:
        :return:
        """
        try:
            for dir in os.listdir(apk_dir):
                # 每遍历到一个目录就为一个app,将其源代码作为一条记录插到mongo中
                apk_dict = {}
                dir = os.path.join(apk_dir, dir)
                feature.scan_file(dir, apk_dict)
                logger.info(len(apk_dict))
                if len(apk_dict) > 500:
                    sample_train = {
                        "train-word": apk_dict,
                        "description": "this is the malware's source code word",
                        "create": datetime.now()
                    }
                    self.session.insert_one(BENIGN_SOURCE, sample_train)
        except Exception:
            traceback.print_exc()
class OperateSample(object):
    """
    the class will save or get the benign or malware sample to mongo
    """
    def __init__(self):
        self.session = MongDBSession()

    def save_sample(self, apk_dir):
        """
        对apk中的源代码作分词,其中的词作为key,出现的次数作为value,存储到mongo中
        :param apk_dir:
        :return:
        """
        try:
            for dir in os.listdir(apk_dir):
                # 每遍历到一个目录就为一个app,将其源代码作为一条记录插到mongo中
                apk_dict = {}
                dir = os.path.join(apk_dir, dir)
                feature.scan_file(dir, apk_dict)
                logger.info(len(apk_dict))
                if len(apk_dict) > 500:
                    sample_train = {
                        "train-word": apk_dict,
                        "description":
                        "this is the malware's source code word",
                        "create": datetime.now()
                    }
                    self.session.insert_one(BENIGN_SOURCE, sample_train)
        except Exception:
            traceback.print_exc()
예제 #3
0
class OperateSample(object):
    """
    the class will save or get the benign or malware sample to mongo
    """

    def __init__(self):
        self.session = MongDBSession()

    def save_sample(self, apk_dir, app_class, table_name):
        """
        对apk中的源代码作分词,其中的词作为key,出现的次数作为value,存储到mongo中
        :param apk_dir:
        :return:
        """
        count = 0
        try:
            for dir in os.listdir(apk_dir):
                count += 1
                if count > 1000:
                    break
                # 每遍历到一个目录就为一个app,将其源代码作为一条记录插到mongo中
                apk_dict = {}
                apk_name = dir
                apk_class = str(apk_name).split(".")

                if len(apk_class) == 1:
                    apk_class = app_class
                else:
                    apk_class = apk_class[0]
                print apk_class
                dir = os.path.join(apk_dir, dir)
                feature.scan_file(dir, apk_dict)
                logger.info(len(apk_dict))

                if len(apk_dict) > 100:
                    sample_train = {
                        "apk_name": apk_name,
                        "apk_class": apk_class,
                        "train_word": apk_dict,
                        "description": "this is the malware's source code word",
                        "create": datetime.now()
                    }
                    # self.session.insert_one(BENIGN_SOURCE, sample_train)
                    self.session.insert_one(table_name, sample_train)

        except Exception:
            traceback.print_exc()

    def get_sample_api(self, apk_dir):
        """
        遍历源代码获取目标api
        :param apk_dir:
        :return:
        """
        with open(
                "/home/wtq/develop/workspace/gitlab/android-app-security-detector/detector/malware/source_malware_feature.json",
                'r') as f:
            js = json.loads(f.read())

        for dir in os.listdir(apk_dir):
            # 每遍历到一个目录就为一个app,api与cishi
            apk_dict = {}
            print "apk name", dir
            dir = os.path.join(apk_dir, dir)
            feature.scan_file(dir, apk_dict)
            for key in apk_dict.keys():
                if key in js:
                    print key, apk_dict[key]