Beispiel #1
0
def basic_params(T):
    '''
    For a tree T, return the list of basic parameters, roughly of the following form.
        [order, number_of_clones, SetSemBound, SetSemSize, uncounteredBound, uncounteredStrats]
    '''
    result = ['number of nodes: ' + str(T.order())]
    number_of_clones = len(
        [label for label in T.basic_actions('a') if T.isclone(label)])
    result.append('number of repeated basic actions of the proponent: ' +
                  str(number_of_clones))
    # 1. SetSemSize: create the set semantics and get the number of its
    # elements.
    setsem = T.set_semantics()
    SetSemSize = len(setsem)
    result.append('size of the set semantics: ' + str(SetSemSize))
    # 2. SetSemBound
    ba = BasicAssignment()
    # 2.1 Create a basic assignment as defined in Lemma 5 of
    # "Efficient attack–defense tree analysis using Pareto attribute domains".
    for b in T.basic_actions():
        ba[b] = 1
    # 2.2 Perform the bottom-up evaluation of the SetSemBound domain under the
    # above basic assignment.
    SetSemBound = setSemSize.evaluateBU(T, ba)
    result.append('bound on the size of the set semantics: ' +
                  str(SetSemBound))

    return result
Beispiel #2
0
def test_investment():
    if flag:
        n = 15
        T = get_tree(n)
        ba = BasicAssignment()
        for b in T.basic_actions('d'):
            ba[b] = 10
        for b in T.basic_actions('a'):
            ba[b] = int(b)

        i = 2

        budget = 10 * i
        problem = ADTilp(T, ba, budget, 'investment')
        actual_solution = problem.solve()

        assert str(2 * n +
                   1) in actual_solution[1] or str(2 * n +
                                                   2) in actual_solution[1]

        #temp = (2 * n + 3) + (2 * n + 4)
        temp = 4 * n + 7
        print(n, temp)
        # the attacker has to perform both actions (2 * n + 3) and (2 * n +
        # 4)
        assert actual_solution[0] >= temp

        expected_min_investment = temp + i
        assert actual_solution[0] <= expected_min_investment
        # the difference between expected and actual

        assert expected_min_investment - actual_solution[0] in [0, 1]
Beispiel #3
0
def main(trees, number_of_measurements=20):
    # create stuff
    domain = minCost
    neutral = 0
    absorbing = 2**20
    #
    name = 'tree'
    for test_num in trees:
        # modify the name
        if test_num < 10:
            new_name = name + '0' + str(test_num)
        else:
            new_name = name + str(test_num)
        # load tree
        T = ADTree('trees\\' + new_name + '.xml')
        # load cost assignment
        ba = BasicAssignment('cost\\' + new_name + '.txt')
        # feedback
        print('Tree stored in "trees\\' + new_name + '.xml" has been loaded.')
        print('Start, the time is ' + time.ctime() + '.')
        # create output file to store results in
        res_name = 'POST_' + new_name + '_results.txt'
        result = open(res_name, 'w')

        # feedback
        print('Computing basic parameters.')

        # 1. basic parameters
        bparams = basic_params(T)
        num_of_clones = int(bparams[0].split(':')[1].strip())
        for item in bparams:
            result.write(item + '\n')

        # 2. timing

        # 2.1 timing on set semantics
        # feedback
        print('Computing on SET SEMANTICS started, the time is ' +
              time.ctime() + '.')

        result.write('timing when evaluating on set semantics: ' +
                     timingOnSetSem(T, domain, ba, number_of_measurements) +
                     '\n')

        # 2.2 timing using repeated bottom-up
        # feedback
        print('Computing using RBU started, the time is ' + time.ctime() + '.')

        result.write('timing of RBU": ' + timingRBU(
            T, domain, ba, neutral, absorbing, number_of_measurements) + '\n')

        # close the output file
        result.close()

        # feedback
        print('Done, the time is ' + time.ctime() + '.\n')
        print('Results written to ' + res_name + '.\n\n')
    return
Beispiel #4
0
def test_attr_domain_repeated_bottom_up():
    ba = BasicAssignment()
    costdom = AttrDomain(min, lambda x, y: x + y)
    for b in T.basic_actions('a'):
        ba[b] = int(b)
    for b in T.basic_actions('d'):
        ba[b] = 0
    ba[str(2 * n + 1)] = 2**30
    # one action of defender with +infty
    expected_min_cost = 1 + (2 * n + 3) + (2 * n + 4)
    actual_cost_rbu = costdom.evaluateRBU(T, ba, 0, 2**30)
    assert expected_min_cost == actual_cost_rbu
Beispiel #5
0
def test_attr_domain_on_set_semantics():
    ba = BasicAssignment()
    costdom = AttrDomain(min, lambda x, y: x + y)
    for b in T.basic_actions('a'):
        ba[b] = int(b)
    for b in T.basic_actions('d'):
        ba[b] = 0
    ba[str(2 * n + 1)] = 2**30
    # one action of defender with +infty
    expected_min_cost = 1 + (2 * n + 3) + (2 * n + 4)
    actual_cost_ss = costdom.evaluateSS(T, ba)
    assert expected_min_cost == actual_cost_ss
Beispiel #6
0
def generate_assignment_of_cost(T):
    '''
    for the tree T, creates a basic assignment of cost
    '''
    min_cost = 0
    max_cost = 50
    infty = 2**20
    ba = BasicAssignment()
    for b in T.basic_actions('a'):
        b_cost = np.random.randint(min_cost, max_cost + 1)
        ba[b] = b_cost
    for b in T.basic_actions('d'):
        ba[b] = infty
    return ba
Beispiel #7
0
def test_stress():
    for k in range(2, 6):
        T = attack_tree_structured(10, k)
        ba_cost = BasicAssignment()
        for i in range(10):
            # define the basic assignment
            for b in T.basic_actions('a'):
                ba_cost[b] = randint(1, 21)
            # compute cost on set semantics
            costss = minCost.evaluateSS(T, ba_cost)
            # compute cost using repeated bottom-up
            costrbu = minCost.evaluateRBU(
                T, ba_cost, neutralANDp=0, absorbingANDp=2**20)
            #
            assert costss == costrbu
Beispiel #8
0
def prepare():
    '''
    Loads the tree and the basic assignment from files, creates
    appropriate Pareto domain.

    Returns (ADTree, ParetoDomain, BasicAssignment).
    '''
    T = ADTree('reconfigure_power_meter.xml')
    # the following has to be done to avoid a mismatch between the labels
    # obtained from ADTool's .xml (which contain the newline symbol '\n')
    # and the ones from the .txt file storing the basic assignment (where all
    # '\n's are replaced with a space)
    for node in T.dict:
        node.label = node.label.replace('\n', ' ')
    # create the domain for 1 cost and 4 min/max domains
    pd = ParetoDomain(1, 4)
    ba = BasicAssignment('metter_all_attributes.txt')
    return T, pd, ba
Beispiel #9
0
def test_coverage():
    if flag:
        n = 15
        T = get_tree(n)
        ba = BasicAssignment()
        for b in T.basic_actions('d'):
            ba[b] = 10

        i = 2

        budget = 10 * i
        problem = ADTilp(T, ba, budget)
        actual_solution = problem.solve()
        expected_not_countered = n - (i - 1)
        assert actual_solution[0] == expected_not_countered
        # the defender has to implement at least one of the actions 2n+1 and
        # 2n+2
        assert str(2 * n +
                   1) in actual_solution[1] or str(2 * n +
                                                   2) in actual_solution[1]
Beispiel #10
0
def __min_cost_among_unpreventable(T, ba):
    # 1. compute unpreventable attacks in T
    attackers_actions = T.basic_actions('a')
    defenders_actions = T.basic_actions('d')
    ba_for_unprev = BasicAssignment()
    for b in attackers_actions:
        ba_for_unprev[b] = [[b]]
    for b in defenders_actions:
        ba_for_unprev[b] = []
    unpreventable = countStrats.evaluateBU(T, ba_for_unprev)
    # 2. compute the minimal of their costs
    minimum = None
    for strat in unpreventable:
        strat_cost = 0
        for b in strat:
            strat_cost += ba[b]
        if minimum == None:
            minimum = strat_cost
        else:
            minimum = min(minimum, strat_cost)
    return minimum
Beispiel #11
0
def test_extremal_tree():
    ba = BasicAssignment()
    pd = ParetoDomain(m)  # m minimal cost domains
    neutral = [[0 for j in range(m)]]
    absorbing = [[2**30 for j in range(m)]]
    for i in range(1, m + 1):
        ba[str(i)] = [[0 for j in range(m)]]
        ba[str(i)][0][i - 1] = 1
        # defender
        ba[str(m + i)] = absorbing
        # leafs of the attacker
        ba[str(2 * m + i)] = neutral
    #  evaluate
    # 2**m elements in the set semantics
    # half of them are of the form (P, emptyset), each P is unique
    # => 2**(m/2) Pareto optimal strategies, under the above assignment
    res1 = pd.evaluateBU(T, ba)
    res2 = pd.evaluateSS(T, ba)
    res3 = pd.evaluateRBU(T, ba, neutral, absorbing)

    assert len(res1) == 2**(m / 2)
    assert res1 == res2
    assert res2 == res3
Beispiel #12
0
def test_basic_assignment_initialization():
    ba = BasicAssignment()
Beispiel #13
0
def test_basic_assignment_set_values():
    ba = BasicAssignment()
    for b in T.basic_actions('a'):
        ba[b] = int(b)