Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.03 KB | None | 0 0
  1. import pandas as pd # Pandas dataframe
  2. from scapy.layers.dot11 import Dot11ProbeReq, Dot11Beacon
  3. from scapy.utils import rdpcap
  4. import seaborn as sns
  5. import numpy as np
  6. import os
  7. from datetime import datetime
  8. import matplotlib.pyplot as plt
  9.  
  10. MAX_SN = 4096 # Max value for the 802.11 sequence number
  11.  
  12. def extractSN(sc):
  13. hexSC = '0' * (4 - len(hex(sc)[2:])) + hex(sc)[2:] # "normalize" to four digit hexadecimal number
  14. sn = int(hexSC[:-1], 16)
  15. return sn
  16.  
  17. '''
  18. addr1 Destination MAC address
  19. addr2 Source MAC address of sender
  20. addr3 MAC address of Access Point
  21. '''
  22.  
  23. filename = "raw_data/anecoica_20_01/01_prova.pcapng"
  24.  
  25. pcap = rdpcap(filename)
  26.  
  27. def derandomization_with_seq(pcap):
  28. packet = []
  29. ap = []
  30. mac_in_chiaro = []
  31. access_point = []
  32. time_list = [p.time for p in pcap]
  33. time_list = np.array(time_list)
  34. time_threshold = time_list.min() + (15 * 60)
  35. for pkt in pcap:
  36. ##### rimozione primi 5 minuti di scansione ############
  37. if pkt.time > time_threshold:
  38. # if pkt.haslayer(Dot11Beacon or Dot11ProbeResp or Dot11AssoResp):
  39. if pkt.haslayer(Dot11Beacon):
  40. if hasattr(pkt, 'addr2'):
  41. ap.append(pkt.addr2)
  42. # print("source address :", pkt.addr2)
  43. if hasattr(pkt, 'addr1') and pkt.addr1 != 'ff:ff:ff:ff:ff:ff':
  44. mac_in_chiaro.append(pkt.addr1)
  45. # print("mac in chiaro: ", pkt.addr1)
  46. if hasattr(pkt, 'addr3'):
  47. access_point.append(pkt.addr3)
  48. # print("Access Point address :", pkt.addr3)
  49.  
  50. if pkt.haslayer(Dot11ProbeReq):
  51.  
  52. mac = pkt.addr2
  53. seq = extractSN(pkt.SC)
  54. power = pkt.dBm_AntSignal
  55. # print("pkt", mac)
  56. while pkt:
  57. if ('ID' and 'len' and 'info') in pkt.fields.keys():
  58. packet.append([mac, seq, pkt.fields['ID'], pkt.fields['len'], power, pkt.time])
  59. pkt = pkt.payload
  60.  
  61. pkts_df = pd.DataFrame(packet)
  62. pkts_df = pkts_df.drop_duplicates()
  63. pkts_df.sort_values(by=[0, 1])
  64. pkts_df.columns = ['mac', 'seq', 'id', 'len', 'power', 'time']
  65.  
  66. ap = set(ap)
  67.  
  68. # print(len(pkts_df.mac.unique()))
  69.  
  70. pkts_df = pkts_df[~pkts_df.mac.isin(ap)]
  71.  
  72. # print(len(pkts_df.mac.unique()))
  73.  
  74. # pkts_df = pkts_df[pkts_df.id != 221] # Wi-Fi Protected Access
  75. # pkts_df = pkts_df[pkts_df.id != 0]
  76. # pkts_df = pkts_df[pkts_df.id != 238]
  77. # prova = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 42, 48, 50]
  78. # pkts_df = pkts_df[pkts_df.id.isin(prova)]
  79.  
  80. return ap, mac_in_chiaro, pkts_df
  81.  
  82. ap, mac_in_chiaro, df = derandomization_with_seq(pcap)
  83.  
  84. print("Number of packets : ", len(df))
  85. print("Number of AP mac : ", len(ap))
  86. print("Number of mac_in_chiaro : ", len(mac_in_chiaro))
  87. print("Number of mac : ", len(df.mac.unique()))
  88.  
  89. ## Randomico o no
  90. df['random'] = df['mac'].apply(lambda x: (bin(int(x[:2], 16))[2:].zfill(8)[6:] in ['10', '11']))
  91.  
  92. print("MAC non randomici: ", len(df[df.random == False].mac.unique()))
  93. print("MAC randomici: ", len(df[df.random == True].mac.unique()))
  94.  
  95.  
  96. df_rand = df[df.random == True]
  97.  
  98. df_mac_list = df_rand.iloc[:1000]
  99.  
  100. #
  101. start_df = 0
  102.  
  103. df_testing = df_mac_list.iloc[800:870]
  104.  
  105. ax = sns.catplot(x='time', y='seq', hue='mac', data=df_testing)
  106. ax.fig.suptitle('Title')
  107.  
  108. plt.show()
  109.  
  110.  
  111. df_testing = df_mac_list.iloc[800:900]
  112.  
  113. ax = sns.catplot(x='time', y='seq', hue='mac', data=df_testing)
  114. ax.fig.suptitle('Title')
  115.  
  116. plt.show()
  117.  
  118. print(df_testing.head())
  119.  
  120. # for period in range(100, 1000, 100):
  121. #
  122. # df_testing = df_mac_list.iloc[start_df:period]
  123. #
  124. # ax = sns.catplot(x='time', y='seq', hue='mac', data=df_testing)
  125. # ax.fig.suptitle('{:d}{:d}'.format(start_df,period))
  126. #
  127. # plt.show()
  128. #
  129. # start_df = period
  130.  
  131.  
  132.  
  133. #df_mac_list = df_rand.loc[(df_rand['id'] == 50) & (df_rand['len'] == 4)].iloc[:50]
  134.  
  135. #sns.catplot(x='time', y='seq', hue='mac', data=df_mac_list)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement