|
| 1 | +from scapy.all import * |
| 2 | +import os |
| 3 | +import numpy as np |
| 4 | +import pandas as pd |
| 5 | +import matplotlib.pyplot as plt |
| 6 | + |
| 7 | +def read_pcap(pcap_name): |
| 8 | + scapy_cap = rdpcap(pcap_name) |
| 9 | + print('Read ', pcap_name, '. Total Packets: ', len(scapy_cap)) |
| 10 | + return scapy_cap |
| 11 | + |
| 12 | +def create_if_not_present_dir(path): |
| 13 | + if not os.path.exists(path): |
| 14 | + os.makedirs(path) |
| 15 | + |
| 16 | +def filter_pcap(scapy_cap=None, src_ip=None, src_port=None, dest_ip=None, dest_port=None): |
| 17 | + filtered = [] |
| 18 | + if scapy_cap: |
| 19 | + for pkt in scapy_cap: |
| 20 | + if IP in pkt and TCP in pkt: |
| 21 | + if pkt.len > 1400: |
| 22 | + if src_ip: |
| 23 | + if pkt[IP].src != src_ip: |
| 24 | + continue |
| 25 | + if src_port: |
| 26 | + if pkt[TCP].sport != src_port: |
| 27 | + continue |
| 28 | + if dest_ip: |
| 29 | + if pkt[IP].dst != dest_ip: |
| 30 | + continue |
| 31 | + if dest_port: |
| 32 | + if pkt[TCP].dport != dest_port: |
| 33 | + continue |
| 34 | + filtered.append(pkt) |
| 35 | + print('After Filtering No. of Packets: ', len(filtered)) |
| 36 | + return filtered |
| 37 | + |
| 38 | +def get_pkt_seq_n_time(scapy_cap): |
| 39 | + seq_time_list = [] |
| 40 | + for pkt in scapy_cap: |
| 41 | + seq_time_list.append((pkt[TCP].seq, float(pkt.time))) |
| 42 | + print('Fetched sequence number and timestamp') |
| 43 | + return seq_time_list |
| 44 | + |
| 45 | +def save_pcap(pkt_list, name): |
| 46 | + print('Saved as pcap: ', name) |
| 47 | + wrpcap(name, pkt_list) |
| 48 | + |
| 49 | +def most_common(lst): |
| 50 | + return max(set(lst), key=lst.count) |
| 51 | + |
| 52 | +def remove_NaN(pandas_df): |
| 53 | + for column in pandas_df.columns: |
| 54 | + pandas_df = pandas_df[pandas_df[column].notna()] |
| 55 | + print('Removed NaN from columns.') |
| 56 | + return pandas_df |
| 57 | + |
| 58 | +def get_src_port(scapy_cap): |
| 59 | + src_port_list = [] |
| 60 | + for pkt in scapy_cap: |
| 61 | + if TCP in pkt: |
| 62 | + src_port_list.append(pkt[TCP].sport) |
| 63 | + src_port = most_common(src_port_list) |
| 64 | + print('Source Port is: ', src_port) |
| 65 | + return src_port |
| 66 | + |
| 67 | +def draw_hist(data_arr, bins='auto', yscale='log', hist_filename='histogram.jpg'): |
| 68 | + plt_hist = plt |
| 69 | + plt_hist.yscale(yscale) |
| 70 | + plt_hist.xlabel('Delay(ms) (min: ' + str(round(data_arr.min(), 3)) + 'ms max: ' + str(round(data_arr.max(), 3)) + 'ms)') |
| 71 | + plt_hist.ylabel('Frequency') |
| 72 | + plt_hist.hist(data_arr, bins=bins) |
| 73 | + plt_hist.savefig(hist_filename) |
| 74 | + plt_hist.close() |
| 75 | + if data_arr.max() > 20: |
| 76 | + plt_hist = plt |
| 77 | + plt_hist.yscale(yscale) |
| 78 | + plt_hist.xlabel('Delay(ms)') |
| 79 | + plt_hist.ylabel('Frequency') |
| 80 | + plt_hist.xlim([data_arr.min(), data_arr.min() + 10]) |
| 81 | + plt_hist.hist(data_arr, bins=bins) |
| 82 | + plt_hist.savefig(hist_filename.split('.')[0] + '_zoomed.' + hist_filename.split('.')[1]) |
| 83 | + plt_hist.close() |
| 84 | + |
| 85 | +def draw_pdf(data_arr, color='red', label='PDF', bins='auto', pdf_filename='pdf.jpg'): |
| 86 | + count, bins_count = np.histogram(data_arr, bins=bins) |
| 87 | + pdf = count / sum(count) |
| 88 | + plt_pdf = plt |
| 89 | + plt_pdf.xlabel('Delay(ms)') |
| 90 | + plt_pdf.ylabel('PDF') |
| 91 | + plt_pdf.axhline(y = 0.0, color = 'r', linestyle = '-') |
| 92 | + plt_pdf.plot(bins_count[1:], pdf, color=color, label='PDF') |
| 93 | + plt_pdf.savefig(pdf_filename) |
| 94 | + plt_pdf.close() |
| 95 | + if data_arr.max() > 20: |
| 96 | + plt_pdf = plt |
| 97 | + plt_pdf.xlabel('Delay(ms)') |
| 98 | + plt_pdf.ylabel('PDF') |
| 99 | + plt_pdf.xlim([data_arr.min(), data_arr.min() + 10]) |
| 100 | + plt_pdf.axhline(y = 0.0, color = 'r', linestyle = '-') |
| 101 | + plt_pdf.plot(bins_count[1:], pdf, color=color, label='PDF') |
| 102 | + plt_pdf.savefig(pdf_filename.split('.')[0] + '_zoomed.' + pdf_filename.split('.')[1]) |
| 103 | + plt_pdf.close() |
| 104 | + |
| 105 | +def draw_cdf(data_arr, color='blue', label='CDF', bins='auto', cdf_filename='cdf.jpg'): |
| 106 | + count, bins_count = np.histogram(data_arr, bins=bins) |
| 107 | + pdf = count / sum(count) |
| 108 | + cdf = np.cumsum(pdf) |
| 109 | + plt_cdf = plt |
| 110 | + plt_cdf.xlabel('Delay(ms)') |
| 111 | + plt_cdf.ylabel('CDF') |
| 112 | + plt_cdf.axhline(y = 1.0, color = 'r', linestyle = '-') |
| 113 | + plt_cdf.plot(bins_count[1:], cdf, color=color, label="CDF") |
| 114 | + plt_cdf.savefig(cdf_filename) |
| 115 | + plt_cdf.close() |
| 116 | + if data_arr.max() > 20: |
| 117 | + plt_cdf = plt |
| 118 | + plt_cdf.xlabel('Delay(ms)') |
| 119 | + plt_cdf.ylabel('CDF') |
| 120 | + plt_cdf.xlim([data_arr.min(), data_arr.min() + 10]) |
| 121 | + plt_cdf.axhline(y = 1.0, color = 'r', linestyle = '-') |
| 122 | + plt_cdf.plot(bins_count[1:], cdf, color=color, label="CDF") |
| 123 | + plt_cdf.savefig(cdf_filename.split('.')[0] + '_zoomed.' + cdf_filename.split('.')[1]) |
| 124 | + plt_cdf.close() |
| 125 | + |
| 126 | +def draw_pdf_cdf(data_arr, pdf_color='red', cdf_color='blue', bins='auto', pdf_cdf_filename='pdf_cdf.jpg'): |
| 127 | + count, bins_count = np.histogram(data_arr, bins=bins) |
| 128 | + pdf = count / sum(count) |
| 129 | + cdf = np.cumsum(pdf) |
| 130 | + fig, ax1 = plt.subplots() |
| 131 | + color = 'tab:red' |
| 132 | + ax1.set_xlabel('Delay(ms)') |
| 133 | + ax1.set_ylabel('PDF') |
| 134 | + ax1.plot(bins_count[1:], pdf, color=pdf_color, label='PDF') |
| 135 | + ax1.tick_params(axis='y', labelcolor=color) |
| 136 | + ax2 = ax1.twinx() |
| 137 | + color = 'tab:blue' |
| 138 | + ax2.set_ylabel('CDF') |
| 139 | + ax2.plot(bins_count[1:], cdf, color=cdf_color, label='CDF') |
| 140 | + ax2.tick_params(axis='y', labelcolor=color) |
| 141 | + plt.savefig(pdf_cdf_filename) |
| 142 | + plt.close() |
| 143 | + if data_arr.max() > 20: |
| 144 | + fig, ax1 = plt.subplots() |
| 145 | + color = 'tab:red' |
| 146 | + plt.xlim([data_arr.min(), data_arr.min() + 10]) |
| 147 | + ax1.set_xlabel('Delay(ms)') |
| 148 | + ax1.set_ylabel('PDF') |
| 149 | + ax1.plot(bins_count[1:], pdf, color=pdf_color, label='PDF') |
| 150 | + ax1.tick_params(axis='y', labelcolor=color) |
| 151 | + ax2 = ax1.twinx() |
| 152 | + color = 'tab:blue' |
| 153 | + ax2.set_ylabel('CDF') |
| 154 | + ax2.plot(bins_count[1:], cdf, color=cdf_color, label='CDF') |
| 155 | + ax2.tick_params(axis='y', labelcolor=color) |
| 156 | + plt.savefig(pdf_cdf_filename.split('.')[0] + '_zoomed.' + pdf_cdf_filename.split('.')[1]) |
| 157 | + plt.close() |
0 commit comments