コード例 #1
0
ファイル: risk.py プロジェクト: ohyeah521/ApkDetecter-1
def create_system_method_one_score():
    try:
        import fuzzy
    except ImportError:
        error("please install pyfuzzy to use this module !")

    import fuzzy.System
    import fuzzy.InputVariable
    import fuzzy.fuzzify.Plain
    import fuzzy.OutputVariable
    import fuzzy.defuzzify.COGS
    import fuzzy.set.Polygon
    import fuzzy.set.Singleton
    import fuzzy.set.Triangle
    import fuzzy.Adjective
    import fuzzy.operator.Input
    import fuzzy.operator.Compound
    import fuzzy.norm.Min
    import fuzzy.norm.Max
    import fuzzy.Rule

    system = fuzzy.System.System()

    input_Length_MS = fuzzy.InputVariable.InputVariable(fuzzify=fuzzy.fuzzify.Plain.Plain())
    input_AndroidEntropy_MS = fuzzy.InputVariable.InputVariable(fuzzify=fuzzy.fuzzify.Plain.Plain())
    input_JavaEntropy_MS = fuzzy.InputVariable.InputVariable(fuzzify=fuzzy.fuzzify.Plain.Plain())
    input_Permissions_MS = fuzzy.InputVariable.InputVariable(fuzzify=fuzzy.fuzzify.Plain.Plain())

    # Input variables

    # Length
    system.variables["input_Length_MS"] = input_Length_MS
    input_Length_MS.adjectives[LOW_SCORE] = fuzzy.Adjective.Adjective(
        fuzzy.set.Polygon.Polygon([(0.0, 1.0), (50.0, 1.0), (100.0, 0.0)]))
    input_Length_MS.adjectives[AVERAGE_SCORE] = fuzzy.Adjective.Adjective(
        fuzzy.set.Polygon.Polygon([(50.0, 0.0), (100.0, 1.0), (150.0, 1.0), (300.0, 0.0)]))
    input_Length_MS.adjectives[HIGH_SCORE] = fuzzy.Adjective.Adjective(
        fuzzy.set.Polygon.Polygon([(150.0, 0.0), (200.0, 1.0), (300.0, 1.0), (400.0, 0.0)]))
    input_Length_MS.adjectives[PERFECT_SCORE] = fuzzy.Adjective.Adjective(
        fuzzy.set.Polygon.Polygon([(350.0, 0.0), (400.0, 1.0), (500.0, 1.0)]))

    # Android Entropy
    system.variables["input_AndroidEntropy_MS"] = input_AndroidEntropy_MS
    input_AndroidEntropy_MS.adjectives[LOW_SCORE] = fuzzy.Adjective.Adjective(
        fuzzy.set.Polygon.Polygon([(0.0, 1.0), (2.0, 1.0), (4.0, 0.0)]))
    input_AndroidEntropy_MS.adjectives[HIGH_SCORE] = fuzzy.Adjective.Adjective(
        fuzzy.set.Polygon.Polygon([(3.0, 0.0), (4.0, 1.0), (30.0, 1.0)]))

    # Java Entropy
    system.variables["input_JavaEntropy_MS"] = input_JavaEntropy_MS
    input_JavaEntropy_MS.adjectives[LOW_SCORE] = fuzzy.Adjective.Adjective(
        fuzzy.set.Polygon.Polygon([(0.0, 1.0), (2.0, 1.0), (4.0, 0.0)]))
    input_JavaEntropy_MS.adjectives[HIGH_SCORE] = fuzzy.Adjective.Adjective(
        fuzzy.set.Polygon.Polygon([(3.0, 0.0), (4.0, 1.0), (30.0, 1.0)]))

    # Permissions
    system.variables["input_Permissions_MS"] = input_Permissions_MS
    input_Permissions_MS.adjectives[LOW_SCORE] = fuzzy.Adjective.Adjective(
        fuzzy.set.Polygon.Polygon([(0.0, 1.0), (3.0, 1.0), (4.0, 0.0)]))
    input_Permissions_MS.adjectives[AVERAGE_SCORE] = fuzzy.Adjective.Adjective(
        fuzzy.set.Polygon.Polygon([(3.0, 0.0), (4.0, 1.0), (8.0, 1.0), (9.0, 0.0)]))
    input_Permissions_MS.adjectives[HIGH_SCORE] = fuzzy.Adjective.Adjective(
        fuzzy.set.Polygon.Polygon([(8.0, 0.0), (10.0, 1.0), (12.0, 1.0), (13.0, 0.0)]))
    input_Permissions_MS.adjectives[PERFECT_SCORE] = fuzzy.Adjective.Adjective(
        fuzzy.set.Polygon.Polygon([(12.0, 0.0), (13.0, 1.0), (20.0, 1.0)]))

    # Output variables
    output_method_score = fuzzy.OutputVariable.OutputVariable(
        defuzzify=fuzzy.defuzzify.COGS.COGS(),
        description="method one score",
        min=0.0, max=100.0,
    )
    output_method_score.adjectives[NULL_METHOD_SCORE] = fuzzy.Adjective.Adjective(fuzzy.set.Singleton.Singleton(0.0))
    output_method_score.adjectives[AVERAGE_METHOD_SCORE] = fuzzy.Adjective.Adjective(
        fuzzy.set.Singleton.Singleton(50.0))
    output_method_score.adjectives[HIGH_METHOD_SCORE] = fuzzy.Adjective.Adjective(fuzzy.set.Singleton.Singleton(80.0))
    output_method_score.adjectives[PERFECT_METHOD_SCORE] = fuzzy.Adjective.Adjective(
        fuzzy.set.Singleton.Singleton(100.0))

    system.variables["output_method_one_score"] = output_method_score

    add_system_rule(system, "android entropy null", fuzzy.Rule.Rule(
        adjective=[system.variables["output_method_one_score"].adjectives[NULL_METHOD_SCORE]],
        operator=fuzzy.operator.Input.Input(system.variables["input_AndroidEntropy_MS"].adjectives[LOW_SCORE]))
    )

    add_system_rule(system, "java entropy null", fuzzy.Rule.Rule(
        adjective=[system.variables["output_method_one_score"].adjectives[NULL_METHOD_SCORE]],
        operator=fuzzy.operator.Input.Input(system.variables["input_JavaEntropy_MS"].adjectives[LOW_SCORE]))
    )

    add_system_rule(system, "permissions null", fuzzy.Rule.Rule(
        adjective=[system.variables["output_method_one_score"].adjectives[NULL_METHOD_SCORE]],
        operator=fuzzy.operator.Input.Input(system.variables["input_Permissions_MS"].adjectives[LOW_SCORE]))
    )

    add_system_rule(system, "permissions average", fuzzy.Rule.Rule(
        adjective=[system.variables["output_method_one_score"].adjectives[AVERAGE_METHOD_SCORE]],
        operator=fuzzy.operator.Input.Input(system.variables["input_Permissions_MS"].adjectives[AVERAGE_SCORE]))
    )

    add_system_rule(system, "permissions high", fuzzy.Rule.Rule(
        adjective=[system.variables["output_method_one_score"].adjectives[HIGH_METHOD_SCORE]],
        operator=fuzzy.operator.Input.Input(system.variables["input_Permissions_MS"].adjectives[HIGH_SCORE]))
    )

    add_system_rule(system, "permissions perfect", fuzzy.Rule.Rule(
        adjective=[system.variables["output_method_one_score"].adjectives[PERFECT_METHOD_SCORE]],
        operator=fuzzy.operator.Input.Input(system.variables["input_Permissions_MS"].adjectives[PERFECT_SCORE]))
    )

    add_system_rule(system, "length permissions perfect", fuzzy.Rule.Rule(
        adjective=[system.variables["output_method_one_score"].adjectives[PERFECT_METHOD_SCORE]],
        operator=fuzzy.operator.Compound.Compound(
            fuzzy.norm.Min.Min(),
            fuzzy.operator.Input.Input(system.variables["input_Length_MS"].adjectives[PERFECT_SCORE]),
            fuzzy.operator.Input.Input(system.variables["input_Permissions_MS"].adjectives[PERFECT_SCORE]))
    )
    )

    add_system_rule(system, "length AndroidEntropy perfect", fuzzy.Rule.Rule(
        adjective=[system.variables["output_method_one_score"].adjectives[HIGH_METHOD_SCORE]],
        operator=fuzzy.operator.Compound.Compound(
            fuzzy.norm.Min.Min(),
            fuzzy.operator.Input.Input(system.variables["input_Length_MS"].adjectives[PERFECT_SCORE]),
            fuzzy.operator.Input.Input(system.variables["input_AndroidEntropy_MS"].adjectives[HIGH_SCORE]))
    )
    )

    add_system_rule(system, "length JavaEntropy perfect", fuzzy.Rule.Rule(
        adjective=[system.variables["output_method_one_score"].adjectives[HIGH_METHOD_SCORE]],
        operator=fuzzy.operator.Compound.Compound(
            fuzzy.norm.Min.Min(),
            fuzzy.operator.Input.Input(system.variables["input_Length_MS"].adjectives[PERFECT_SCORE]),
            fuzzy.operator.Input.Input(system.variables["input_JavaEntropy_MS"].adjectives[HIGH_SCORE]))
    )
    )

    return system
コード例 #2
0
ファイル: risk.py プロジェクト: ohyeah521/ApkDetecter-1
def create_system_risk():
    try:
        import fuzzy
    except ImportError:
        error("please install pyfuzzy to use this module !")

    import fuzzy.System
    import fuzzy.InputVariable
    import fuzzy.fuzzify.Plain
    import fuzzy.OutputVariable
    import fuzzy.defuzzify.COGS
    import fuzzy.defuzzify.COG
    import fuzzy.defuzzify.MaxRight
    import fuzzy.defuzzify.MaxLeft
    import fuzzy.defuzzify.LM
    import fuzzy.set.Polygon
    import fuzzy.set.Singleton
    import fuzzy.set.Triangle
    import fuzzy.Adjective
    import fuzzy.operator.Input
    import fuzzy.operator.Compound
    import fuzzy.norm.Min
    import fuzzy.norm.Max
    import fuzzy.Rule
    import fuzzy.defuzzify.Dict

    system = fuzzy.System.System()

    input_Dangerous_Risk = fuzzy.InputVariable.InputVariable(fuzzify=fuzzy.fuzzify.Plain.Plain())
    input_Money_Risk = fuzzy.InputVariable.InputVariable(fuzzify=fuzzy.fuzzify.Plain.Plain())
    input_Privacy_Risk = fuzzy.InputVariable.InputVariable(fuzzify=fuzzy.fuzzify.Plain.Plain())
    input_Binary_Risk = fuzzy.InputVariable.InputVariable(fuzzify=fuzzy.fuzzify.Plain.Plain())
    input_Internet_Risk = fuzzy.InputVariable.InputVariable(fuzzify=fuzzy.fuzzify.Plain.Plain())
    input_Dynamic_Risk = fuzzy.InputVariable.InputVariable(fuzzify=fuzzy.fuzzify.Plain.Plain())

    # Input variables

    # Dangerous Risk
    system.variables["input_Dangerous_Risk"] = input_Dangerous_Risk
    input_Dangerous_Risk.adjectives[LOW_RISK] = fuzzy.Adjective.Adjective(
        fuzzy.set.Polygon.Polygon([(0.0, 1.0), (8.0, 1.0), (12.0, 0.0)]))
    input_Dangerous_Risk.adjectives[AVERAGE_RISK] = fuzzy.Adjective.Adjective(
        fuzzy.set.Polygon.Polygon([(8.0, 0.0), (50.0, 1.0), (60.0, 0.0)]))
    input_Dangerous_Risk.adjectives[HIGH_RISK] = fuzzy.Adjective.Adjective(
        fuzzy.set.Polygon.Polygon([(50.0, 0.0), (85.0, 1.0), (95.0, 0.0)]))
    input_Dangerous_Risk.adjectives[UNACCEPTABLE_RISK] = fuzzy.Adjective.Adjective(
        fuzzy.set.Polygon.Polygon([(85.0, 0.0), (100.0, 1.0)]))

    # Money Risk
    system.variables["input_Money_Risk"] = input_Money_Risk
    input_Money_Risk.adjectives[LOW_RISK] = fuzzy.Adjective.Adjective(
        fuzzy.set.Polygon.Polygon([(0.0, 1.0), (2.0, 1.0), (3.0, 0.0)]))
    input_Money_Risk.adjectives[UNACCEPTABLE_RISK] = fuzzy.Adjective.Adjective(
        fuzzy.set.Polygon.Polygon([(4.0, 0.0), (5.0, 1.0), (30.0, 1.0)]))

    # Privacy Risk
    system.variables["input_Privacy_Risk"] = input_Privacy_Risk
    input_Privacy_Risk.adjectives[LOW_RISK] = fuzzy.Adjective.Adjective(
        fuzzy.set.Polygon.Polygon([(0.0, 1.0), (6.0, 1.0), (10.0, 0.0)]))
    input_Privacy_Risk.adjectives[HIGH_RISK] = fuzzy.Adjective.Adjective(
        fuzzy.set.Polygon.Polygon([(6.0, 0.0), (10.0, 1.0), (20.0, 0.0)]))
    input_Privacy_Risk.adjectives[UNACCEPTABLE_RISK] = fuzzy.Adjective.Adjective(
        fuzzy.set.Polygon.Polygon([(15.0, 0.0), (20.0, 1.0), (30.0, 1.0)]))

    # Binary Risk
    system.variables["input_Binary_Risk"] = input_Binary_Risk
    input_Binary_Risk.adjectives[LOW_RISK] = fuzzy.Adjective.Adjective(
        fuzzy.set.Polygon.Polygon([(0.0, 1.0), (6.0, 1.0), (10.0, 0.0)]))
    input_Binary_Risk.adjectives[AVERAGE_RISK] = fuzzy.Adjective.Adjective(
        fuzzy.set.Polygon.Polygon([(6.0, 0.0), (10.0, 1.0), (15.0, 0.0)]))
    input_Binary_Risk.adjectives[HIGH_RISK] = fuzzy.Adjective.Adjective(
        fuzzy.set.Polygon.Polygon([(10.0, 0.0), (20.0, 1.0), (24.0, 0.0)]))
    input_Binary_Risk.adjectives[UNACCEPTABLE_RISK] = fuzzy.Adjective.Adjective(
        fuzzy.set.Polygon.Polygon([(23.0, 0.0), (30.0, 1.0), (40.0, 1.0)]))

    # Internet Risk
    system.variables["input_Internet_Risk"] = input_Internet_Risk
    #input_Internet_Risk.adjectives[LOW_RISK] = fuzzy.Adjective.Adjective( fuzzy.set.Polygon.Polygon([(0.0, 1.0), (1.0, 1.0)]) )
    input_Internet_Risk.adjectives[HIGH_RISK] = fuzzy.Adjective.Adjective(
        fuzzy.set.Polygon.Polygon([(1.0, 0.0), (5.0, 1.0), (30.0, 1.0)]))

    # Dynamic Risk
    system.variables["input_Dynamic_Risk"] = input_Dynamic_Risk
    input_Dynamic_Risk.adjectives[LOW_RISK] = fuzzy.Adjective.Adjective(
        fuzzy.set.Polygon.Polygon([(0.0, 1.0), (2.0, 1.0), (3.0, 0.0)]))
    input_Dynamic_Risk.adjectives[UNACCEPTABLE_RISK] = fuzzy.Adjective.Adjective(
        fuzzy.set.Polygon.Polygon([(4.0, 0.0), (5.0, 1.0), (50.0, 1.0)]))

    # Output variables
    output_malware_risk = fuzzy.OutputVariable.OutputVariable(
        defuzzify=fuzzy.defuzzify.COGS.COGS(),
        description="malware risk",
        min=0.0, max=100.0,
    )

    #output_malware_risk = fuzzy.OutputVariable.OutputVariable(defuzzify=fuzzy.defuzzify.Dict.Dict())

    output_malware_risk.adjectives[NULL_MALWARE_RISK] = fuzzy.Adjective.Adjective(fuzzy.set.Singleton.Singleton(0.0))
    output_malware_risk.adjectives[AVERAGE_MALWARE_RISK] = fuzzy.Adjective.Adjective(
        fuzzy.set.Singleton.Singleton(30.0))
    output_malware_risk.adjectives[HIGH_MALWARE_RISK] = fuzzy.Adjective.Adjective(fuzzy.set.Singleton.Singleton(60.0))
    output_malware_risk.adjectives[UNACCEPTABLE_MALWARE_RISK] = fuzzy.Adjective.Adjective(
        fuzzy.set.Singleton.Singleton(100.0))

    system.variables["output_malware_risk"] = output_malware_risk

    # Rules
    #RULE 0: DYNAMIC 
    add_system_rule(system, "r0", fuzzy.Rule.Rule(
        adjective=[system.variables["output_malware_risk"].adjectives[NULL_MALWARE_RISK]],
        operator=fuzzy.operator.Input.Input(system.variables["input_Dynamic_Risk"].adjectives[LOW_RISK])
    )
    )

    add_system_rule(system, "r0a", fuzzy.Rule.Rule(
        adjective=[system.variables["output_malware_risk"].adjectives[UNACCEPTABLE_MALWARE_RISK]],
        operator=fuzzy.operator.Input.Input(system.variables["input_Dynamic_Risk"].adjectives[UNACCEPTABLE_RISK])
    )
    )


    #RULE 1: MONEY
    add_system_rule(system, "r1", fuzzy.Rule.Rule(
        adjective=[system.variables["output_malware_risk"].adjectives[NULL_MALWARE_RISK]],
        operator=fuzzy.operator.Input.Input(system.variables["input_Money_Risk"].adjectives[LOW_RISK])
    )
    )

    add_system_rule(system, "r1a", fuzzy.Rule.Rule(
        adjective=[system.variables["output_malware_risk"].adjectives[UNACCEPTABLE_MALWARE_RISK]],
        operator=fuzzy.operator.Input.Input(system.variables["input_Money_Risk"].adjectives[UNACCEPTABLE_RISK])
    )
    )

    #RULE 3 : BINARY
    add_system_rule(system, "r3", fuzzy.Rule.Rule(
        adjective=[system.variables["output_malware_risk"].adjectives[AVERAGE_MALWARE_RISK]],
        operator=fuzzy.operator.Input.Input(system.variables["input_Binary_Risk"].adjectives[AVERAGE_RISK])
    )
    )

    add_system_rule(system, "r3a", fuzzy.Rule.Rule(
        adjective=[system.variables["output_malware_risk"].adjectives[HIGH_RISK]],
        operator=fuzzy.operator.Input.Input(system.variables["input_Binary_Risk"].adjectives[HIGH_RISK])
    )
    )

    add_system_rule(system, "r3b", fuzzy.Rule.Rule(
        adjective=[system.variables["output_malware_risk"].adjectives[UNACCEPTABLE_MALWARE_RISK]],
        operator=fuzzy.operator.Input.Input(system.variables["input_Binary_Risk"].adjectives[UNACCEPTABLE_RISK])
    )
    )

    # PRIVACY + INTERNET
    add_system_rule(system, "r5", fuzzy.Rule.Rule(
        adjective=[system.variables["output_malware_risk"].adjectives[HIGH_MALWARE_RISK]],
        operator=fuzzy.operator.Compound.Compound(
            fuzzy.norm.Min.Min(),
            fuzzy.operator.Input.Input(system.variables["input_Privacy_Risk"].adjectives[LOW_RISK]),
            fuzzy.operator.Input.Input(system.variables["input_Internet_Risk"].adjectives[HIGH_RISK]))
    )
    )
    add_system_rule(system, "r5a", fuzzy.Rule.Rule(
        adjective=[system.variables["output_malware_risk"].adjectives[UNACCEPTABLE_MALWARE_RISK]],
        operator=fuzzy.operator.Compound.Compound(
            fuzzy.norm.Min.Min(),
            fuzzy.operator.Input.Input(system.variables["input_Privacy_Risk"].adjectives[HIGH_RISK]),
            fuzzy.operator.Input.Input(system.variables["input_Internet_Risk"].adjectives[HIGH_RISK]))
    )
    )

    add_system_rule(system, "r6", fuzzy.Rule.Rule(
        adjective=[system.variables["output_malware_risk"].adjectives[HIGH_RISK]],
        operator=fuzzy.operator.Input.Input(system.variables["input_Dangerous_Risk"].adjectives[HIGH_RISK])
    )
    )

    add_system_rule(system, "r6a", fuzzy.Rule.Rule(
        adjective=[system.variables["output_malware_risk"].adjectives[UNACCEPTABLE_RISK]],
        operator=fuzzy.operator.Input.Input(system.variables["input_Dangerous_Risk"].adjectives[UNACCEPTABLE_RISK])
    )
    )

    return system