示例#1
0
def pktGenerator(pos, rule_list, rule_dict, types, intersect, log):
    if intersect == None:
        return []

    intersect = copy.deepcopy(intersect)
    log = copy.deepcopy(log)
    ret = []

    if pos >= len(rule_list):
        #print "log = ",log
        #print "intersect",intersect
        if len(log) <= 0:
            return ret
        header_space = []
        for v in [v for v in rule_list if v not in log]:
            header_space.append(rule_dict[v])
        TimeLog.GetInstance().addTotal()
        pkt = solver.createPacket(intersect, header_space, types)
        TimeLog.GetInstance().addSolver()
        if pkt['SAT'] == 'Yes':
            ret.append((log, pkt))
        return ret
    #for i in range(pos,len(rule_list)):
    i = pos
    cur = pktGenerator(i + 1, rule_list, rule_dict, types, intersect, log)
    ret += cur

    intersect = header.intersect_molecule(intersect, rule_dict[rule_list[i]])
    #print intersect
    if intersect != None:
        log.append(rule_list[i])
        cur = pktGenerator(i + 1, rule_list, rule_dict, types, intersect, log)
        ret += cur
    return ret
示例#2
0
def packetGenerator(edge_dict, rule_list, types, q, table):
    global postCardQueue
    postCardQueue = q

    global Pid
    Pid = 0

    TimeLog.GetInstance().addTotal()

    V = rule_list.keys()
    E = []
    for v1 in edge_dict:
        for v2 in edge_dict[v1]:
            E.append([v2, v1])

    S = []
    VV = []
    EE = []

    #for v1 in edge_dict:
    #    vset = edge_dict[v1]
    #    for v2 in vset:
    #        S.append([v1,v2])
    rules = rule_list.keys()
    #print len(rules)
    for r1 in range(0, len(rules)):
        for r2 in range(r1 + 1, len(rules)):
            if r1 in table and r2 in table:
                continue
            intersection = header.intersect_molecule(rule_list[rules[r1]],
                                                     rule_list[rules[r2]])
            if intersection != None:
                S.append([rules[r1], rules[r2]])
    while len(S) > 0:
        #index = random.randint(0,len(S)-1)
        index = 0
        v1 = S[index][0]
        v2 = S[index][1]
        #print len(S),index,v1,v2
        #print "S=",S
        #print VV,EE
        header_space = []
        for edge in EE:
            if edge[0] == v1 or edge[0] == v2:
                header_space.append(rule_list[edge[1]])
        intersection = header.intersect_molecule(rule_list[v1], rule_list[v2])
        T = (intersection, header_space)
        while True:
            subtraction = header.subtraction_wrapper(intersection,
                                                     header_space)
            if subtraction == None:
                del S[index]
                break

            TimeLog.GetInstance().addCalc()
            pkt = solver.createPacket(intersection, header_space, types)
            TimeLog.GetInstance().addSolver()
            if pkt['SAT'] == 'No':
                del S[index]
                break

            #print "packet: ",
            types_tmp = {
                "src-port": 16,
                "dst-port": 16,
                "src-ip": 0,
                "dst-ip": 0
            }
            for typ in types_tmp:
                if pkt.has_key(typ):
                    #print typ,pkt[typ],
                    pass  #print typ,pkt[typ],
            #print ""

            TimeLog.GetInstance().addCalc()
            vhit = IssueProbe(pkt, rule_list, v1, v2)
            TimeLog.GetInstance().addSend()
            if vhit == -1:
                del S[index]
                break

            #solver.printpkt(pkt,types)
            if vhit >= 0 and not vhit in VV:
                VV.append(vhit)
            if vhit == v1:
                EE.append([v2, v1])
                del S[index]
                break
            if vhit == v2:
                EE.append([v1, v2])
                del S[index]
                break
            EE.append([v2, vhit])
            EE.append([v1, vhit])
            header_space.append(rule_list[vhit])
            if [v1, vhit] in S:
                S.remove([v1, vhit])
            if [v2, vhit] in S:
                S.remove([v2, vhit])
    #print "VV:",VV
    #print "EE:",EE
    #print "stage 2:"
    VVV = [val for val in V if val not in VV]
    while len(VVV) > 0:
        index = random.randint(0, len(VVV) - 1)
        v = VVV[index]
        T = rule_list[v]
        TT = []
        header_space = []
        #for vv in VV:
        #intersection = header.intersect_molecule(rule_list[vv],rule_list[vv])
        #if intersection != None:
        #header_space.append(intersection)
        #TT.append(vv)
        while True:
            #len(T) > 0 and len(header_space) > 0:
            TimeLog.GetInstance().addCalc()
            #print "solver: ",len(header_space)
            pkt = solver.createPacket(T, header_space, types)
            TimeLog.GetInstance().addSolver()
            if pkt['SAT'] == 'No':
                break
            TimeLog.GetInstance().addCalc()
            vhit = IssueProbe(pkt, rule_list, v, v)
            TimeLog.GetInstance().addSend()
            if vhit == -1:
                break
            if (not vhit in VV) and (vhit in V):
                VV.append(vhit)
            if vhit in VVV:
                VVV.remove(vhit)
            if vhit == v:
                break
            if vhit in TT:
                break
            TT.append(vhit)
            header_space.append(rule_list[vhit])
        if v in VVV:
            VVV.remove(v)
    #print "After leafs' detection:"
    #print "Original Graph"
    #print "V:",V
    #print "E:",E
    #print "Actual Graph"
    #print "VV:",VV
    #print "EE:",EE
    TimeLog.GetInstance().addCalc()
    if len(VV) != len(V):
        return False
    if len([val for val in V if val not in VV]) > 0:
        return False

    #equality for two graph.
    h = {}
    for i, k in enumerate(V):
        h[k] = i
    G1 = [[0] * len(V) for i in range(len(V))]
    for edge in E:
        v1 = h[edge[0]]
        v2 = h[edge[1]]
        G1[v1][v2] = 1
    for i in range(len(V)):
        for j in range(len(V)):
            for k in range(len(V)):
                if G1[i][k] == 1 and G1[k][j] == 1:
                    G1[i][j] = 1
    G2 = [[0] * len(V) for i in range(len(V))]
    for edge in EE:
        v1 = h[edge[0]]
        v2 = h[edge[1]]
        G2[v1][v2] = 1
    for i in range(len(V)):
        for j in range(len(V)):
            for k in range(len(V)):
                if G2[i][k] == 1 and G2[k][j] == 1:
                    G2[i][j] = 1
    ret = True
    for i in range(len(V)):
        if ret == False:
            break
        for j in range(len(V)):
            if G1[i][j] != G2[i][j]:
                ret = False
                break
    #print "Original DAG:",G1
    #print "Actual DAG:",G2
    TimeLog.GetInstance().clock()
    return ret
    import networkx as nx
    G = nx.DiGraph()
    for v in V:
        G.add_node(v)
    for edge in E:
        G.add_edge(edge[1], edge[0])
    GG = nx.DiGraph()
    for v in VV:
        GG.add_node(v)
    for edge in EE:
        GG.add_edge(edge[1], edge[0])
    ret = nx.is_isomorphic(G, GG)

    return ret
示例#3
0
def packetGenerator(edge_dict, rule_list, types, table):
    # containg all pkt, rule paris
    pairs = []
    dag = {}
    for rule in rule_list:
        dag[rule] = []
    for rule1 in edge_dict:
        if not dag.has_key(rule1):
            dag[rule1] = []
        for rule2 in edge_dict[rule1]:
            if not dag.has_key(rule2):
                dag[rule2] = []
            dag[rule2].append(rule1)

    dep = copy.deepcopy(dag)
    #print dag

    header_space = []
    T = {}
    for rule1 in rule_list.keys():
        T[rule1] = []
    # variable rule1 and rule 2 are int.
    while True:
        rule1 = -1
        for rule in dag:
            if len(dag[rule]) == 0:
                rule1 = rule
                break
        #print rule1
        if rule1 == -1:
            break
        for rule in dep[rule1]:
            if len(T[rule]) > 0:
                T[rule1] += T[rule]

        T[rule1].sort()
        T[rule1] = [
            x for i, x in enumerate(T[rule1])
            if not i or T[rule1][i] != T[rule1][i - 1]
        ]

        if edge_dict.has_key(rule1):
            #print "rule has other rule to depend on"
            adj_list = edge_dict[rule1]

            for rule2 in adj_list:
                if rule1 in table and rule2 in table:
                    #print rule1, rule2
                    continue
                intersection = header.intersect_molecule(
                    rule_list[rule1], rule_list[rule2])
                if intersection == None:
                    #print rule1,rule_list[rule1]
                    #print rule2,rule_list[rule2]
                    continue
                #print rule1,rule2
                TimeLog.GetInstance().addCalc()
                packet = solver.createPacket(intersection, T[rule1], types)
                TimeLog.GetInstance().addSolver()
                #print packet
                if packet['SAT'] == 'No':
                    print "1. Dependency Error"
                    #print rule2
                    return False
                # include the packet and its rule pair
                T[rule2].append(intersection)
                #T[rule2].append(rule_list[rule1])

                tu = (rule1, packet)
                if tu not in pairs:
                    pairs.append(tu)

        elif len(dep[rule1]) > 0:
            if rule1 in table:
                del dag[rule1]
                for rule in dag:
                    if rule1 in dag[rule]:
                        dag[rule].remove(rule1)
                continue
            #print "rule has no ther rule depend on"
            TimeLog.GetInstance().addCalc()
            packet = solver.createPacket(rule_list[rule1], T[rule1], types)
            TimeLog.GetInstance().addSolver()
            if packet['SAT'] == 'No':
                print "2. (leaf) Dependency Error"
                #print rule1
                return False

            tu = (rule1, packet)
            if tu not in pairs:
                pairs.append(tu)

        elif len(dep[rule1]) == 0:
            if rule1 in table:
                del dag[rule1]
                for rule in dag:
                    if rule1 in dag[rule]:
                        dag[rule].remove(rule1)
                continue
            #print "rule has no ther rule depend on"
            TimeLog.GetInstance().addCalc()
            packet = solver.createPacket(rule_list[rule1], T[rule1], types)
            TimeLog.GetInstance().addSolver()
            if packet['SAT'] == 'No':
                print "3. (dependent) Dependency Error"
                #print rule1
                return False

            tu = (rule1, packet)
            if tu not in pairs:
                pairs.append(tu)

        #remove visited rules in dag
        del dag[rule1]
        for rule in dag:
            if rule1 in dag[rule]:
                dag[rule].remove(rule1)
    #print pairs
    TimeLog.GetInstance().addCalc()
    return pairs
示例#4
0
def packetGenerator(edge_dict, rule_list, types, q, table, Edge):
    global postCardQueue
    postCardQueue = q
    global Pid
    Pid = 0

    TimeLog.GetInstance().addTotal()

    V = rule_list.keys()
    E = []
    for v1 in edge_dict:
        for v2 in edge_dict[v1]:
            E.append([v2, v1])

    S = []
    VV = table
    EE = Edge
    rules = rule_list.keys()
    for r1 in range(0, len(rules)):
        for r2 in range(r1 + 1, len(rules)):
            if r1 in table and r2 in table:
                continue
            intersection = header.intersect_molecule(rule_list[rules[r1]],
                                                     rule_list[rules[r2]])
            if intersection != None:
                S.append([rules[r1], rules[r2]])
    while len(S) > 0:
        #print "#",len(S),S
        pset = {}
        pindex = {}
        #print len(S)
        #print VV,EE
        for s in S:
            v1 = s[0]
            v2 = s[1]
            header_space = []
            for edge in EE:
                if edge[0] == v1 or edge[0] == v2:
                    header_space.append(rule_list[edge[1]])
            intersection = header.intersect_molecule(rule_list[v1],
                                                     rule_list[v2])

            T = [intersection, header_space]
            subtraction = header.subtraction_wrapper(intersection,
                                                     header_space)
            if subtraction == None:
                S.remove(s)
                continue
            TimeLog.GetInstance().addTotal()
            pkt = solver.createPacket(intersection, header_space, types)
            TimeLog.GetInstance().addSolver()
            if pkt['SAT'] == 'No':
                S.remove(s)
                break
            pindex[Pid] = [v1, v2]
            pset[Pid] = pkt
            pkt['pid'] = Pid
            Pid += 1

        #print len(pset)
        #IssueProbeSet
        TimeLog.GetInstance().addTotal()
        pHits = IssueProbeSet(pset)
        TimeLog.GetInstance().addSend()

        for p in pHits:
            vhit = pHits[p]
            v1 = pindex[p][0]
            v2 = pindex[p][1]
            #for p in pset:
            #print len(pset)
            #pkt = pset[p]
            #v1 = pindex[p][0]
            #v2 = pindex[p][1]
            #TimeLog.GetInstance().addTotal()
            #vhit = IssueProbe(pkt,rule_list,v1,v2)
            #TimeLog.GetInstance().addSend()

            if vhit >= 0 and not vhit in VV:
                VV.append(vhit)
            if vhit == v1:
                EE.append([v2, v1])
                if [v1, v2] in S:
                    S.remove([v1, v2])
                continue
            if vhit == v2:
                EE.append([v1, v2])
                if [v1, v2] in S:
                    S.remove([v1, v2])
                continue
            if vhit >= 0:
                EE.append([v2, vhit])
                EE.append([v1, vhit])
                if [v1, vhit] in S:
                    S.remove([v1, vhit])
                if [v2, vhit] in S:
                    S.remove([v2, vhit])
            if vhit == -1:
                if [v1, v2] in S:
                    S.remove([v1, v2])
    #print "VV:",VV
    #print "EE:",EE
    #print "stage 2:"
    VVV = [val for val in V if val not in VV]
    while len(VVV) > 0:
        #print "#",len(VVV)
        index = random.randint(0, len(VVV) - 1)
        v = VVV[index]
        T = rule_list[v]
        TT = []
        header_space = []
        #for vv in VV:
        #intersection = header.intersect_molecule(rule_list[vv],rule_list[vv])
        #if intersection != None:
        #header_space.append(intersection)
        #TT.append(vv)
        while True:
            #len(T) > 0 and len(header_space) > 0:
            TimeLog.GetInstance().addCalc()
            #print "solver: ",len(header_space)
            pkt = solver.createPacket(T, header_space, types)
            TimeLog.GetInstance().addSolver()
            if pkt['SAT'] == 'No':
                break
            TimeLog.GetInstance().addCalc()
            Pid += 1
            pkt['pid'] = Pid
            vhit = IssueProbe(pkt, rule_list, v, v)
            TimeLog.GetInstance().addSend()
            if vhit == -1:
                break
            if (not vhit in VV) and (vhit in V):
                VV.append(vhit)
            if vhit in VVV:
                VVV.remove(vhit)
            if vhit == v:
                break
            if vhit in TT:
                break
            TT.append(vhit)
            header_space.append(rule_list[vhit])
        if v in VVV:
            VVV.remove(v)
    #print "After leafs' detection:"
    #print "Original Graph"
    #print "V:",V
    #print "E:",E
    #print "Actual Graph"
    #print "VV:",VV
    #print "EE:",EE
    TimeLog.GetInstance().addCalc()
    if len(VV) != len(V):
        return False
    if len([val for val in V if val not in VV]) > 0:
        return False
    #print len(V), len(VV)
    ret = True
    TimeLog.GetInstance().clock()
    return ret

    #equality for two graph.
    h = {}
    for i, k in enumerate(V):
        h[k] = i
    G1 = [[0] * len(V) for i in range(len(V))]
    for edge in E:
        v1 = h[edge[0]]
        v2 = h[edge[1]]
        G1[v1][v2] = 1
    for i in range(len(V)):
        for j in range(len(V)):
            for k in range(len(V)):
                if G1[i][k] == 1 and G1[k][j] == 1:
                    G1[i][j] = 1
    G2 = [[0] * len(V) for i in range(len(V))]
    for edge in EE:
        v1 = h[edge[0]]
        v2 = h[edge[1]]
        G2[v1][v2] = 1
    for i in range(len(V)):
        for j in range(len(V)):
            for k in range(len(V)):
                if G2[i][k] == 1 and G2[k][j] == 1:
                    G2[i][j] = 1
    ret = True
    for i in range(len(V)):
        if ret == False:
            break
        for j in range(len(V)):
            if G1[i][j] != G2[i][j]:
                ret = False
                break
    #print "Original DAG:",G1
    #print "Actual DAG:",G2
    TimeLog.GetInstance().clock()
    return ret