Exemplo n.º 1
0
def generateSessions(
    packets: List[Packet],
    direction: AnyStr = 'bidirectional',
    sessionExtractor: Optional[Callable[[Packet], Tuple[AnyStr,
                                                        AnyStr]]] = None
) -> Sessions:
    """
    generate sessions from packets
    :param packets: packet list
    :param direction: 'unidirectional' or 'bidirectional'.
                      If session extractor is not None,
                      this param won't work
    :param sessionExtractor: Optional[Callable[[Packet], (AnyStr, AnyStr)]]
    :return: sessions
    """
    if sessionExtractor is None:
        # use bidirectional session extractor as default
        # unidirectional session key is the bidirectional session key + direction
        sessionExtractor = defaultBidirectionalSessionExtractor
    # to mark the direction, we avoid using sessions provided by scapy
    sessions = defaultdict(list)
    for p in probar(packets, color=progressBarColor):
        sessionKey, pDirection = sessionExtractor(p)
        if direction == 'unidirectional':
            sessionKey = f'{sessionKey} {pDirection}'
        # add additional attribute on packet to mark the direction
        p.pDirection = pDirection
        sessions[sessionKey].append(p)
    return sessions
Exemplo n.º 2
0
def generateFeatures(flows: Flows) -> Tuple[FeatureSet, List[AnyStr]]:
    """Generate Features According to Flows"""
    featureSet: FeatureSet = list()
    for flow in probar(flows, color=progressBarColor):
        flow: Flow
        features = flow2feature(flow)
        featureSet.append(features)
    return featureSet, FeatureExtractor.getAllFeatureNames()
Exemplo n.º 3
0
def test_performance():
    from tqdm import tqdm
    N = 5000000
    print("probar:")
    for i in probar(range(N), color='1', time_interval=0.02):
        pass
    time.sleep(0.1)
    
    print('bar:')
    for idx, i in enumerate(range(N)):
        bar(idx, N, text=f'{idx+1}', color='5')

    print("tqdm:")
    for i in tqdm(range(N)):
        pass
Exemplo n.º 4
0
def test_performance():
    from tqdm import tqdm
    N = 50000000
    print("probar:")

    for i in probar(range(N),  time_interval=0.02, time_zone="Asia/Shanghai"):#color='1',
        pass
    time.sleep(0.1)
    
    # print('bar:')
    # for idx, i in enumerate(range(N)):
    #     bar(idx, N, text='', color='5', time_zone="Europe/Brussels")
    #
    print("tqdm:")
    for i in tqdm(range(N)):
        pass
def packets2features(
    packets: List[Packet],
    direction: AnyStr = 'bidirectional',
    sessionExtractor: Optional[Callable[[Packet], Tuple[AnyStr,
                                                        AnyStr]]] = None,
    flowTimeout=Flow.defaultFlowTimeout,
    activityTimeout=Flow.defaultActivityTimeout
) -> Tuple[FeatureSet, List[AnyStr]]:
    """
    Take packets as input generate features
    :param packets: A list of packets
    :param direction: unidirectional or bidirectional
    :param sessionExtractor: session extractor
    :param flowTimeout: flow timeout in microseconds
    :param activityTimeout: activity timeout in microseconds
    :return: (Feature Set, Feature Names)
    """
    aliveFlows, flows = dict(), list()
    featureSet: FeatureSet = list()
    Flow.defaultFlowTimeout, Flow.defaultActivityTimeout = flowTimeout, activityTimeout
    if sessionExtractor is None:
        # use bidirectional session extractor as default
        # unidirectional session key is the bidirectional session key + direction
        sessionExtractor = defaultBidirectionalSessionExtractor
    for p in probar(packets, color=progressBarColor):
        sessionKey, pDirection = sessionExtractor(p)
        if direction == 'unidirectional':
            sessionKey = f'{sessionKey} {pDirection}'
        # add additional attribute on packet to mark the direction
        p.pDirection = pDirection

        if sessionKey not in aliveFlows:
            aliveFlows[sessionKey] = Flow(sessionKey, p)
        else:
            flow = aliveFlows[sessionKey]
            success = flow.add(p)
            if not success:
                features = flow2feature(flow)
                featureSet.append(features)
                aliveFlows[sessionKey] = Flow(sessionKey, p)
    # flush alive flows to flows
    for sessionKey, aliveFlow in aliveFlows.items():
        features = flow2feature(aliveFlow)
        featureSet.append(features)
    return featureSet, list(featureSet[0].keys())
Exemplo n.º 6
0
def sessions2flows(sessions: Sessions,
                   flowTimeout=Flow.defaultFlowTimeout,
                   activityTimeout=Flow.defaultActivityTimeout) -> Flows:
    aliveFlows, flows = dict(), list()
    Flow.defaultFlowTimeout, Flow.defaultActivityTimeout = flowTimeout, activityTimeout
    # generate flows
    for sessionKey, session in probar(sessions.items(),
                                      color=progressBarColor):
        for p in session:
            if sessionKey not in aliveFlows:
                aliveFlows[sessionKey] = Flow(sessionKey, p)
            else:
                flow = aliveFlows[sessionKey]
                success = flow.add(p)
                if not success:
                    flows.append(flow)
                    aliveFlows[sessionKey] = Flow(sessionKey, p)
    # flush alive flows to flows
    for sessionKey, aliveFlow in aliveFlows.items():
        flows.append(aliveFlow)
    # sort the flows
    flows.sort()
    return flows
Exemplo n.º 7
0
def test_probar3():
    for i in probar(range(1234), symbol_2="o"):
        time.sleep(0.01)
Exemplo n.º 8
0
def test_probar2():
    res = [i for i in probar(range(10), enum=False) ]
    print(res)
Exemplo n.º 9
0
def test_probar1():
    for idx, i in probar(range(1234), enum=True):
        time.sleep(0.0061)