Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pandas as pd # Pandas dataframe
- from scapy.layers.dot11 import Dot11ProbeReq, Dot11Beacon
- from scapy.utils import rdpcap
- import seaborn as sns
- import numpy as np
- import os
- from datetime import datetime
- import matplotlib.pyplot as plt
- MAX_SN = 4096 # Max value for the 802.11 sequence number
- def extractSN(sc):
- hexSC = '0' * (4 - len(hex(sc)[2:])) + hex(sc)[2:] # "normalize" to four digit hexadecimal number
- sn = int(hexSC[:-1], 16)
- return sn
- '''
- addr1 Destination MAC address
- addr2 Source MAC address of sender
- addr3 MAC address of Access Point
- '''
- filename = "raw_data/anecoica_20_01/01_prova.pcapng"
- pcap = rdpcap(filename)
- def derandomization_with_seq(pcap):
- packet = []
- ap = []
- mac_in_chiaro = []
- access_point = []
- time_list = [p.time for p in pcap]
- time_list = np.array(time_list)
- time_threshold = time_list.min() + (15 * 60)
- for pkt in pcap:
- ##### rimozione primi 5 minuti di scansione ############
- if pkt.time > time_threshold:
- # if pkt.haslayer(Dot11Beacon or Dot11ProbeResp or Dot11AssoResp):
- if pkt.haslayer(Dot11Beacon):
- if hasattr(pkt, 'addr2'):
- ap.append(pkt.addr2)
- # print("source address :", pkt.addr2)
- if hasattr(pkt, 'addr1') and pkt.addr1 != 'ff:ff:ff:ff:ff:ff':
- mac_in_chiaro.append(pkt.addr1)
- # print("mac in chiaro: ", pkt.addr1)
- if hasattr(pkt, 'addr3'):
- access_point.append(pkt.addr3)
- # print("Access Point address :", pkt.addr3)
- if pkt.haslayer(Dot11ProbeReq):
- mac = pkt.addr2
- seq = extractSN(pkt.SC)
- power = pkt.dBm_AntSignal
- # print("pkt", mac)
- while pkt:
- if ('ID' and 'len' and 'info') in pkt.fields.keys():
- packet.append([mac, seq, pkt.fields['ID'], pkt.fields['len'], power, pkt.time])
- pkt = pkt.payload
- pkts_df = pd.DataFrame(packet)
- pkts_df = pkts_df.drop_duplicates()
- pkts_df.sort_values(by=[0, 1])
- pkts_df.columns = ['mac', 'seq', 'id', 'len', 'power', 'time']
- ap = set(ap)
- # print(len(pkts_df.mac.unique()))
- pkts_df = pkts_df[~pkts_df.mac.isin(ap)]
- # print(len(pkts_df.mac.unique()))
- # pkts_df = pkts_df[pkts_df.id != 221] # Wi-Fi Protected Access
- # pkts_df = pkts_df[pkts_df.id != 0]
- # pkts_df = pkts_df[pkts_df.id != 238]
- # prova = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 42, 48, 50]
- # pkts_df = pkts_df[pkts_df.id.isin(prova)]
- return ap, mac_in_chiaro, pkts_df
- ap, mac_in_chiaro, df = derandomization_with_seq(pcap)
- print("Number of packets : ", len(df))
- print("Number of AP mac : ", len(ap))
- print("Number of mac_in_chiaro : ", len(mac_in_chiaro))
- print("Number of mac : ", len(df.mac.unique()))
- ## Randomico o no
- df['random'] = df['mac'].apply(lambda x: (bin(int(x[:2], 16))[2:].zfill(8)[6:] in ['10', '11']))
- print("MAC non randomici: ", len(df[df.random == False].mac.unique()))
- print("MAC randomici: ", len(df[df.random == True].mac.unique()))
- df_rand = df[df.random == True]
- df_mac_list = df_rand.iloc[:1000]
- #
- start_df = 0
- df_testing = df_mac_list.iloc[800:870]
- ax = sns.catplot(x='time', y='seq', hue='mac', data=df_testing)
- ax.fig.suptitle('Title')
- plt.show()
- df_testing = df_mac_list.iloc[800:900]
- ax = sns.catplot(x='time', y='seq', hue='mac', data=df_testing)
- ax.fig.suptitle('Title')
- plt.show()
- print(df_testing.head())
- # for period in range(100, 1000, 100):
- #
- # df_testing = df_mac_list.iloc[start_df:period]
- #
- # ax = sns.catplot(x='time', y='seq', hue='mac', data=df_testing)
- # ax.fig.suptitle('{:d}{:d}'.format(start_df,period))
- #
- # plt.show()
- #
- # start_df = period
- #df_mac_list = df_rand.loc[(df_rand['id'] == 50) & (df_rand['len'] == 4)].iloc[:50]
- #sns.catplot(x='time', y='seq', hue='mac', data=df_mac_list)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement