<jupyter_start><jupyter_text>服务数量统计<jupyter_code>packet_num = 0
record = []
def find_services(packet):
    global packet_num
    packet_num += 1
    if int(packet.tcp.flags_syn) == 1 and int(packet.tcp.flags_ack) == 0:
        service = str(packet.ip.dst) + ":" + str(packet.tcp.dstport)
        if service in record:
            pass
        else:
            record.append(service)
            print "New service found:", service
cap.apply_on_packets(find_services)<jupyter_output><empty_output><jupyter_text>TCP请求统计<jupyter_code>services = {}
for service in record:
    services[service] = {}
    services[service]["syn"] = 1
    services[service]["links"] = {}
    services[service]["complete_link"] = 0
    services[service]["half_link"] = 0
widgets = ['Processing: ', Percentage(), ' ', Bar(marker='#',left='[',right=']'),
           ' ', ETA()] 

pbar = ProgressBar(widgets=widgets, maxval=packet_num+1)
pbar.start()

count = 0

for pkt in cap:
    
    count += 1

    src = str(pkt.ip.src) + ":" + str(pkt.tcp.srcport)
    dst = str(pkt.ip.dst) + ":" + str(pkt.tcp.dstport)

    if (int(pkt.tcp.flags_syn) == 1) and (int(pkt.tcp.flags_ack) == 0):
        if dst in services.keys():
            services[dst]["syn"] += 1
            if (src in services[dst]["links"]):
                if (services[dst]["links"][src]):
                    services[dst]["half_link"] += 1
            services[dst]["links"][src] = False

    if (int(pkt.tcp.flags_syn) == 1) and (int(pkt.tcp.flags_ack) == 1):
        services[src]["links"][dst] = True

    if (int(pkt.tcp.flags_syn) == 0) and (int(pkt.tcp.flags_ack) == 1):
        if (dst in services.keys()):
            if (src in services[dst]["links"]):
                if (services[dst]["links"][src]):
                    services[dst]["complete_link"] += 1
                    del services[dst]["links"][src]
                    
pbar.finish()
for service in services:
    print "Service:", service
    print "#Syn:", services[service]["syn"]
    print "#TCP-Connection:", services[service]["complete_link"]
    print "#Half-Connection:", str(len(services[service]["links"]) + services[service]["half_link"])<jupyter_output><empty_output><jupyter_text>重传率统计<jupyter_code>services = {}
for service in record:
    services[service] = {}
    services[service]["retrans"] = 0
    services[service]["total"] = 0    
widgets = ['Processing: ', Percentage(), ' ', Bar(marker='#',left='[',right=']'),
           ' ', ETA()] 

pbar = ProgressBar(widgets=widgets, maxval=packet_num+1)
pbar.start()

count = 0
for pkt in cap:
    
    count += 1

    src = str(pkt.ip.src) + ":" + str(pkt.tcp.srcport)
    dst = str(pkt.ip.dst) + ":" + str(pkt.tcp.dstport)

    if (pkt.tcp.get('analysis_retransmission')):
        if src in services.keys():
            services[src]["retrans"] += 1
        else:
            services[dst]["retrans"] += 1
            
    if (src in services.keys()):
        services[src]["total"] += 1
    else:
        services[dst]["total"] += 1

pbar.finish()
for service in record:
    print "Service:", service
    print "#Retransmission", services[service]["retrans"]
    print "Retransmission %:", float(services[service]["retrans"]) / services[service]["total"]<jupyter_output><empty_output><jupyter_text>TCP链接重置请求统计<jupyter_code>services = {}
for service in record:
    services[service] = {}
    services[service]["reset_client"] = 0
    services[service]["reset_server"] = 0
widgets = ['Processing: ', Percentage(), ' ', Bar(marker='#',left='[',right=']'),
           ' ', ETA()] 

pbar = ProgressBar(widgets=widgets, maxval=packet_num+1)
pbar.start()

count = 0

for pkt in cap:
    
    count += 1

    src = str(pkt.ip.src) + ":" + str(pkt.tcp.srcport)
    dst = str(pkt.ip.dst) + ":" + str(pkt.tcp.dstport)

    if (int(pkt.tcp.flags_reset) == 1):
        if src in services.keys():
            services[src]["reset_server"] += 1
        else:
            services[dst]["reset_client"] += 1
            
pbar.finish()
for service in record:
    print "Service:", service
    print "#Reset:Client", services[service]["reset_client"]
    print "#Reset:Server", services[service]["reset_server"]<jupyter_output><empty_output><jupyter_text>流量统计<jupyter_code>flow = []
widgets = ['Processing: ', Percentage(), ' ', Bar(marker='#',left='[',right=']'),
           ' ', ETA()] 

pbar = ProgressBar(widgets=widgets, maxval=packet_num+1)
pbar.start()

count = 0

for pkt in cap:
    flow.append((pkt.sniff_time, pkt.length))
    count += 1
    pbar.update(count)
    
pbar.finish()
flow = pd.Series(index=map(lambda x: x[0], flow), data=map(lambda x: float(x[1]), flow))
stat = flow.resample("300s").sum()
plt.figure(figsize=(16, 5))
plt.plot(stat)
plt.plot(stat, 'o', color="purple")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1837588949351193, 'fraction_numerical': 0.009836751778987024, 'mean_word_length': 3.2669642857142858, 'pattern_counts': {'":': 7, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '167899', 'license': 'MIT', 'n_tokens_mistral': 1712, 'n_tokens_neox': 1668, 'n_words': 297, 'path': 'lab1/sniffer_package_analysis.ipynb', 'repo_name': 'TianxiaoHu/Lab-Network-Attacks-and-Countermeasures', 'size': '4778'}