| 1 |
// License: BSD/LGPL |
| 2 |
// Copyright (C) 2011-2018 Thomas d'Otreppe |
| 3 |
using System; |
| 4 |
using System.Collections.Generic; |
| 5 |
|
| 6 |
namespace WirelessPanda.Readers |
| 7 |
{ |
| 8 |
public class CsvReader : Reader |
| 9 |
{ |
| 10 |
/// <summary> |
| 11 |
/// Date format (Same format for 0.x and 1.x) |
| 12 |
/// </summary> |
| 13 |
protected override string DATE_FORMAT |
| 14 |
{ |
| 15 |
get |
| 16 |
{ |
| 17 |
return "yyyy-MM-dd HH:mm:ss"; |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
public enum CSVFileFormat |
| 22 |
{ |
| 23 |
v0X, |
| 24 |
v1X, |
| 25 |
Unknown |
| 26 |
} |
| 27 |
|
| 28 |
/// <summary> |
| 29 |
/// Get the file format |
| 30 |
/// </summary> |
| 31 |
public CSVFileFormat FileFormat |
| 32 |
{ |
| 33 |
get |
| 34 |
{ |
| 35 |
return this._fileFormat; |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
private CSVFileFormat _fileFormat = CSVFileFormat.Unknown; |
| 40 |
|
| 41 |
/// <summary> |
| 42 |
/// Reader type |
| 43 |
/// </summary> |
| 44 |
public override string ReaderType |
| 45 |
{ |
| 46 |
get |
| 47 |
{ |
| 48 |
return "Airodump-ng CSV"; |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
/// <summary> |
| 53 |
/// Constructor |
| 54 |
/// </summary> |
| 55 |
/// <param name="filename">Filename (doesn't need to exist now but MUST when using Read() )</param> |
| 56 |
public CsvReader(string filename) : base(filename) { } |
| 57 |
|
| 58 |
/// <summary> |
| 59 |
/// Read/Update the content of the file |
| 60 |
/// </summary> |
| 61 |
/// <returns>true if successful</returns> |
| 62 |
/// <exception cref="FormatException">Airodump-ng CSV format unknown</exception> |
| 63 |
public override bool Read() |
| 64 |
{ |
| 65 |
// Reset parsing status |
| 66 |
this.ParseSuccess = false; |
| 67 |
|
| 68 |
// Get the content of the file |
| 69 |
string[] content = this.getStrippedFileContent(); |
| 70 |
|
| 71 |
// Get file format |
| 72 |
this._fileFormat = this.getFormat(content); |
| 73 |
|
| 74 |
if (this._fileFormat == CSVFileFormat.Unknown) |
| 75 |
{ |
| 76 |
throw new FormatException("Airodump-ng CSV format unknown"); |
| 77 |
} |
| 78 |
|
| 79 |
// Parse AP ... |
| 80 |
int i = 2; // Start at line 3 (skipping header) |
| 81 |
for (; i < content.Length && !string.IsNullOrEmpty(content[i]); i++) |
| 82 |
{ |
| 83 |
string [] split = content[i].Split(','); |
| 84 |
|
| 85 |
switch (this._fileFormat) |
| 86 |
{ |
| 87 |
case CSVFileFormat.v0X: |
| 88 |
if (split.Length < 11) |
| 89 |
{ |
| 90 |
continue; |
| 91 |
} |
| 92 |
break; |
| 93 |
|
| 94 |
case CSVFileFormat.v1X: |
| 95 |
if (split.Length < 15) |
| 96 |
{ |
| 97 |
continue; |
| 98 |
} |
| 99 |
break; |
| 100 |
} |
| 101 |
AccessPoint ap = new AccessPoint(); |
| 102 |
ap.BSSID = split[0].Trim(); |
| 103 |
ap.FirstTimeSeen = this.parseDateTime(split[1]); |
| 104 |
ap.LastTimeSeen = this.parseDateTime(split[2]); |
| 105 |
ap.Channel = int.Parse(split[3].Trim()); |
| 106 |
ap.MaxRate = double.Parse(split[4].Trim()); |
| 107 |
ap.Privacy = split[5].Trim(); |
| 108 |
|
| 109 |
switch (this._fileFormat) |
| 110 |
{ |
| 111 |
case CSVFileFormat.v0X: |
| 112 |
ap.Power = int.Parse(split[6].Trim()); |
| 113 |
ap.Beacons = long.Parse(split[7].Trim()); |
| 114 |
ap.DataFrames = ulong.Parse(split[8].Trim()); |
| 115 |
ap.IP = split[9].Replace(" ", ""); |
| 116 |
ap.ESSID = split[10].Substring(1); // TODO: Improve it because it may contain a ',' |
| 117 |
ap.ESSIDLength = (byte)ap.ESSID.Length; |
| 118 |
break; |
| 119 |
|
| 120 |
case CSVFileFormat.v1X: |
| 121 |
ap.Cipher = split[6].Trim(); |
| 122 |
ap.Authentication = split[7].Trim(); |
| 123 |
ap.Power = int.Parse(split[8].Trim()); |
| 124 |
ap.Beacons = long.Parse(split[9].Trim()); |
| 125 |
ap.DataFrames = ulong.Parse(split[10].Trim()); |
| 126 |
ap.IP = split[11].Replace(" ", ""); |
| 127 |
ap.ESSIDLength = byte.Parse(split[12].Trim()); |
| 128 |
ap.ESSID = split[13].Substring(1); // TODO: Improve it because it may contain a ',' |
| 129 |
ap.Key = split[14]; |
| 130 |
break; |
| 131 |
} |
| 132 |
|
| 133 |
// Add AP to the list |
| 134 |
this.addAccessPoint(ap); |
| 135 |
} |
| 136 |
|
| 137 |
// ... Parse stations |
| 138 |
|
| 139 |
i += 2; // Skip station header |
| 140 |
for (; i < content.Length && !string.IsNullOrEmpty(content[i]); i++) |
| 141 |
{ |
| 142 |
string[] split = content[i].Split(','); |
| 143 |
|
| 144 |
// Skip to the next if not long enough |
| 145 |
if (split.Length < 6) |
| 146 |
{ |
| 147 |
continue; |
| 148 |
} |
| 149 |
|
| 150 |
// Parse station information |
| 151 |
Station sta = new Station(); |
| 152 |
sta.StationMAC = split[0].Trim(); |
| 153 |
sta.FirstTimeSeen = this.parseDateTime(split[1]); |
| 154 |
sta.LastTimeSeen = this.parseDateTime(split[2]); |
| 155 |
sta.Power = int.Parse(split[3].Trim()); |
| 156 |
sta.NbPackets = ulong.Parse(split[4].Trim()); |
| 157 |
sta.BSSID = split[5].Trim(); |
| 158 |
|
| 159 |
// Get probed ESSID list |
| 160 |
if (split.Length > 6 && split[6] != "") |
| 161 |
{ |
| 162 |
List<string> list = new List<string>(); |
| 163 |
for (int j = 6; j < split.Length; j++) |
| 164 |
{ |
| 165 |
// There's always a whitespace character before |
| 166 |
list.Add(split[j].Substring(1)); |
| 167 |
} |
| 168 |
sta.ProbedESSIDsList = list.ToArray(); |
| 169 |
} |
| 170 |
else |
| 171 |
{ |
| 172 |
sta.ProbedESSIDs = string.Empty; |
| 173 |
} |
| 174 |
|
| 175 |
// Add station to the list |
| 176 |
this.addStation(sta); |
| 177 |
} |
| 178 |
|
| 179 |
// Link them together |
| 180 |
this.LinkAPClients(); |
| 181 |
|
| 182 |
// Parsing was successful |
| 183 |
this.ParseSuccess = true; |
| 184 |
|
| 185 |
return this.ParseSuccess; |
| 186 |
} |
| 187 |
|
| 188 |
/// <summary> |
| 189 |
/// Returns the format of the file |
| 190 |
/// </summary> |
| 191 |
/// <param name="content">File content</param> |
| 192 |
/// <returns>CSV File Format</returns> |
| 193 |
/// <exception cref="ArgumentNullException">content is null</exception> |
| 194 |
/// <exception cref="ArgumentException">content is empty</exception> |
| 195 |
private CSVFileFormat getFormat(string[] content) |
| 196 |
{ |
| 197 |
// Checks |
| 198 |
if (content == null) |
| 199 |
{ |
| 200 |
throw new ArgumentNullException("Cannot determine format without any content"); |
| 201 |
} |
| 202 |
if (content.Length == 1 && string.IsNullOrEmpty(content[0])) |
| 203 |
{ |
| 204 |
throw new ArgumentException("Cannot determine format without any content"); |
| 205 |
} |
| 206 |
|
| 207 |
// First line is empty and the second line contains the header |
| 208 |
if (content.Length > 2 && string.IsNullOrEmpty(content[0])) |
| 209 |
{ |
| 210 |
// Version 1.x |
| 211 |
if (content[1] == "BSSID, First time seen, Last time seen, channel, Speed, Privacy, Cipher, Authentication, Power, # beacons, # IV, LAN IP, ID-length, ESSID, Key") |
| 212 |
{ |
| 213 |
return CSVFileFormat.v1X; |
| 214 |
} |
| 215 |
|
| 216 |
// Version 0.x |
| 217 |
if (content[1] == "BSSID, First time seen, Last time seen, Channel, Speed, Privacy, Power, # beacons, # data, LAN IP, ESSID") |
| 218 |
{ |
| 219 |
return CSVFileFormat.v0X; |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
return CSVFileFormat.Unknown; |
| 224 |
} |
| 225 |
} |
| 226 |
} |