File: csvReader.cs

package info (click to toggle)
aircrack-ng 1%3A1.5.2-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 17,416 kB
  • sloc: ansic: 67,243; cs: 5,392; python: 2,619; sh: 2,102; makefile: 1,001; asm: 569; cpp: 69
file content (226 lines) | stat: -rw-r--r-- 8,121 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// License: BSD/LGPL
// Copyright (C) 2011-2018 Thomas d'Otreppe
using System;
using System.Collections.Generic;

namespace WirelessPanda.Readers
{
    public class CsvReader : Reader
    {
        /// <summary>
        /// Date format (Same format for 0.x and 1.x)
        /// </summary>
        protected override string DATE_FORMAT
        {
            get
            {
                return "yyyy-MM-dd HH:mm:ss";
            }
        }

        public enum CSVFileFormat
        {
            v0X,
            v1X,
            Unknown
        }

        /// <summary>
        /// Get the file format
        /// </summary>
        public CSVFileFormat FileFormat
        {
            get
            {
                return this._fileFormat;
            }
        }

        private CSVFileFormat _fileFormat = CSVFileFormat.Unknown;

        /// <summary>
        /// Reader type
        /// </summary>
        public override string ReaderType
        {
            get
            {
                return "Airodump-ng CSV";
            }
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="filename">Filename (doesn't need to exist now but MUST when using Read() )</param>
        public CsvReader(string filename) : base(filename) { }

        /// <summary>
        /// Read/Update the content of the file
        /// </summary>
        /// <returns>true if successful</returns>
        /// <exception cref="FormatException">Airodump-ng CSV format unknown</exception>
        public override bool Read()
        {
            // Reset parsing status
            this.ParseSuccess = false;

            // Get the content of the file
            string[] content = this.getStrippedFileContent();

            // Get file format
            this._fileFormat = this.getFormat(content);

            if (this._fileFormat == CSVFileFormat.Unknown)
            {
                throw new FormatException("Airodump-ng CSV format unknown");
            }

            // Parse AP ...
            int i = 2; // Start at line 3 (skipping header)
            for (; i < content.Length && !string.IsNullOrEmpty(content[i]); i++) 
            {
                string [] splitted = content[i].Split(',');

                switch (this._fileFormat)
                {
                    case CSVFileFormat.v0X:
                        if (splitted.Length < 11)
                        {
                            continue;
                        }
                        break;

                    case CSVFileFormat.v1X:
                        if (splitted.Length < 15)
                        {
                            continue;
                        }
                        break;
                }
                AccessPoint ap = new AccessPoint();
                ap.BSSID = splitted[0].Trim();
                ap.FirstTimeSeen = this.parseDateTime(splitted[1]);
                ap.LastTimeSeen = this.parseDateTime(splitted[2]);
                ap.Channel = int.Parse(splitted[3].Trim());
                ap.MaxRate = double.Parse(splitted[4].Trim());
                ap.Privacy = splitted[5].Trim();

                switch (this._fileFormat)
                {
                    case CSVFileFormat.v0X:
                        ap.Power = int.Parse(splitted[6].Trim());
                        ap.Beacons = long.Parse(splitted[7].Trim());
                        ap.DataFrames = ulong.Parse(splitted[8].Trim());
                        ap.IP = splitted[9].Replace(" ", "");
                        ap.ESSID = splitted[10].Substring(1); // TODO: Improve it because it may contain a ','
                        ap.ESSIDLength = (byte)ap.ESSID.Length;
                        break;

                    case CSVFileFormat.v1X:
                        ap.Cipher = splitted[6].Trim();
                        ap.Authentication = splitted[7].Trim();
                        ap.Power = int.Parse(splitted[8].Trim());
                        ap.Beacons = long.Parse(splitted[9].Trim());
                        ap.DataFrames = ulong.Parse(splitted[10].Trim());
                        ap.IP = splitted[11].Replace(" ", "");
                        ap.ESSIDLength = byte.Parse(splitted[12].Trim());
                        ap.ESSID = splitted[13].Substring(1); // TODO: Improve it because it may contain a ','
                        ap.Key = splitted[14];
                        break;
                }

                // Add AP to the list
                this.addAccessPoint(ap);
            }

            // ... Parse stations

            i += 2; // Skip station header
            for (; i < content.Length && !string.IsNullOrEmpty(content[i]); i++)
            {
                string[] splitted = content[i].Split(',');

                // Skip to the next if not long enough
                if (splitted.Length < 6)
                {
                    continue;
                }

                // Parse station information
                Station sta = new Station();
                sta.StationMAC = splitted[0].Trim();
                sta.FirstTimeSeen = this.parseDateTime(splitted[1]);
                sta.LastTimeSeen = this.parseDateTime(splitted[2]);
                sta.Power = int.Parse(splitted[3].Trim());
                sta.NbPackets = ulong.Parse(splitted[4].Trim());
                sta.BSSID = splitted[5].Trim();

                // Get probed ESSID list
                if (splitted.Length > 6 && splitted[6] != "")
                {
                    List<string> list = new List<string>();
                    for (int j = 6; j < splitted.Length; j++)
                    {
                        // There's always a whitespace character before
                        list.Add(splitted[j].Substring(1));
                    }
                    sta.ProbedESSIDsList = list.ToArray();
                }
                else
                {
                    sta.ProbedESSIDs = string.Empty;
                }

                // Add station to the list
                this.addStation(sta);
            }

            // Link them together
            this.LinkAPClients();

            // Parsing was successful
            this.ParseSuccess = true;

            return this.ParseSuccess;
        }

        /// <summary>
        /// Returns the format of the file
        /// </summary>
        /// <param name="content">File content</param>
        /// <returns>CSV File Format</returns>
        /// <exception cref="ArgumentNullException">content is null</exception>
        /// <exception cref="ArgumentException">content is empty</exception>
        private CSVFileFormat getFormat(string[] content)
        {
            // Checks
            if (content == null)
            {
                throw new ArgumentNullException("Cannot determine format without any content");
            }
            if (content.Length == 1 && string.IsNullOrEmpty(content[0]))
            {
                throw new ArgumentException("Cannot determine format without any content");
            }

            // First line is empty and the second line contains the header
            if (content.Length > 2 && string.IsNullOrEmpty(content[0]))
            {
                // Version 1.x
                if (content[1] == "BSSID, First time seen, Last time seen, channel, Speed, Privacy, Cipher, Authentication, Power, # beacons, # IV, LAN IP, ID-length, ESSID, Key")
                {
                    return CSVFileFormat.v1X;
                }

                // Version 0.x
                if (content[1] == "BSSID, First time seen, Last time seen, Channel, Speed, Privacy, Power, # beacons, # data, LAN IP, ESSID")
                {
                    return CSVFileFormat.v0X;
                }
            }

            return CSVFileFormat.Unknown;
        }
    }
}