source: src/django_gheat/gheat/management/commands/netstumbler.py@ 9601

Last change on this file since 9601 was 9601, checked in by rick, 13 years ago

Initial import netstumbler file reader..

  • Property svn:executable set to *
File size: 2.8 KB
Line 
1#!/usr/bin/env python
2#
3# Reading NS1 files - http://www.stumbler.net/ns1files.html
4#
5# Rick van der Zwet <info@rickvanderzwet.nl>
6#
7import sys
8import datetime
9
10from struct import *
11
12def parse_netstumbler(filename):
13 fh = open(filename,'rb')
14
15 def get_int32(size=1):
16 return unpack('<' + 'i'*size,fh.read(4*size))
17
18 def get_uint32(size=1):
19 return unpack('<' + 'I'*size,fh.read(4*size))
20
21 def get_uint64(size=1):
22 return unpack('<' + 'Q'*size,fh.read(8*size))
23
24 def get_uint8(size=1):
25 return unpack('<' + 'B'*size,fh.read(1*size))
26
27 def get_filetime():
28 _FILETIME_null_date = datetime.datetime(1601, 1, 1, 0, 0, 0)
29 ns = unpack('<Q',fh.read(8))[0] * 10
30 sec = ns / 10**8
31 d = datetime.timedelta(seconds=sec)
32 return _FILETIME_null_date + d
33
34 def get_char(size):
35 return fh.read(size)
36
37 def get_double(size=1):
38 return unpack('<' + 'd'*size,fh.read(8*size))
39
40 data = {}
41
42 data["dwSignature"] = get_char(4)
43 data["dwFileVerunpack"] = get_uint32()
44 ApCount = get_uint32()[0]
45 data["ApCount"] = ApCount
46
47 data["aps"] = []
48 for a in range(0,ApCount):
49 ap = {}
50 SSIDLength = get_uint8()[0]
51 ap["SSIDLength"] = SSIDLength
52 ap["SSID"] = get_char(SSIDLength)
53 ap["BSSID"] = map(hex,unpack('BBBBBB',fh.read(6)))
54 ap["MaxSignal"] = get_int32()
55 ap["MinNoise"] = get_int32()
56 ap["MaxSNR"] = get_int32()
57 ap["Flags"] = get_uint32()
58 ap["BeaconInterval"] = get_uint32()
59 ap["FirstSeen"] = get_filetime()
60 ap["LastSeen"] = get_filetime()
61 ap["BestLat"] = get_double()
62 ap["BestLong"] = get_double()
63 DataCount = get_uint32()[0]
64 ap["DataCount"] = DataCount
65 ap["measurement"] = []
66 for c in range(0,DataCount):
67 ms = {}
68 ms["Time"] = get_filetime()
69 ms["Signal"] = get_int32()
70 ms["Noice"] = get_int32()
71 LocationSource = get_int32()[0]
72 ms["Location Source"] = LocationSource
73 if LocationSource == 1:
74 ms["Latitude"] = get_double()
75 ms["Longitude"] = get_double()
76 ms["Altitude"] = get_double()
77 ms["NumStats"] = get_uint32()
78 ms["Speed"] = get_double()
79 ms["Track"] = get_double()
80 ms["MagVariation"] = get_double()
81 ms["Hdop"] = get_double()
82 ap["measurement"].append(ms)
83 NameLength = get_uint8()[0]
84 ap["NameLength"] = NameLength
85 ap["Name"] = get_char(NameLength)
86 ap["Channels"] = get_uint64()
87 ap["LastChannel"] = get_uint32()
88 ap["IPAddress"] = get_uint32()
89 ap["MinSignal"] = get_int32()
90 ap["MaxSignal"] = get_int32()
91 ap["DataRate"] = get_uint32()
92 ap["IPSubnet"] = get_uint32()
93 ap["IPMask"] = get_uint32()
94 ap["ApFlags"] = get_uint32()
95 IELength = get_uint32()[0]
96 ap["IELength"] = IELength
97 ap["InformationElements"] = get_uint8(IELength)
98 data["aps"].append(ap)
99 return data
100
101print parse_netstumbler(sys.argv[1])
Note: See TracBrowser for help on using the repository browser.