-
Notifications
You must be signed in to change notification settings - Fork 2
/
scan_for_wireless_networks
executable file
·144 lines (131 loc) · 4.12 KB
/
scan_for_wireless_networks
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
#!/usr/bin/perl
use Data::Dumper;
use DBI;
# Example output to be parsed:
#
# Cell 01 - Address: 8A:E4:33:2F:A1:10
# ESSID:"caseydog-guest"
# Protocol:IEEE 802.11bgn
# Mode:Master
# Frequency:2.412 GHz (Channel 1)
# Encryption key:on
# Bit Rates:144 Mb/s
# Extra:rsn_ie=30140100000fac040100000fac040100000fac020000
# IE: IEEE 802.11i/WPA2 Version 1
# Group Cipher : CCMP
# Pairwise Ciphers (1) : CCMP
# Authentication Suites (1) : PSK
# Quality=100/100 Signal level=70/100
# Cell 02 - Address: 88:1F:A1:2F:33:E4
# ESSID:"caseydog"
# Protocol:IEEE 802.11bgn
# ...and so on...
$iwlist_bin = "/sbin/iwlist";
$interface = "wlan1";
#_ Only the king can perform a scan. All peasants are rejected.
$cmd = "sudo $iwlist_bin $interface scanning";
open FIN, "$cmd|" or die "Can't execute: $cmd";
$cell = undef;
while(<FIN>) {
# uncomment to see scan results before writing to the DB
#print $1;
if ( /Cell \d+ - Address: ([0-9A-Fa-f:]+)/ ) {
$cell = &save_last_cell_start_new_cell($cell);
$cell->{mac_address} = $1;
$cell->{mac_address} =~ tr/a-f/A-F/;
} elsif ( /ESSID:"(.*)"/ ) {
$cell->{essid} = $1;
# strip out unwanted chars from the SSID
$cell->{essid} =~ s/'//;
} elsif ( /Protocol:.*802\.11(\w+)/ ) {
$cell->{protocol} = 0;
$cell->{protocol} |= 1 if ( $1 =~ /b/ );
$cell->{protocol} |= 2 if ( $1 =~ /g/ );
$cell->{protocol} |= 4 if ( $1 =~ /n/ );
} elsif ( /Mode:(\w+)/ ) {
if ( $1 eq "Master" ) {
$cell->{mode} = 2;
} elsif ( $1 eq "Ad-Hoc" ) { # TODO: check this
$cell->{mode} = 1;
}
} elsif ( /Frequency:.*Channel (\d+)/ ) {
$cell->{channel} = $1;
} elsif ( /Encryption key:(\w+)/ ) {
if ( $1 eq "on" ) { $cell->{is_encrypted} = 2;
} elsif ( $1 eq "off" ) {
$cell->{is_encrypted} = 1;
}
} elsif ( /IE:\s+(.*)/ ) {
$cell->{encryption_type} = $1;
} elsif ( /Quality=(\d+).*Signal level=(\d+)/ ) {
$cell->{quality} = $1;
$cell->{signal_level} = $2;
}
}
close FIN;
push(@cells, $cell) if (defined $cell);
foreach(@cells) {
print Dumper($_);
}
#
# Write to the database
#
# See for tutorial: http://zetcode.com/db/sqliteperltutorial/
#
$dsn = "dbi:SQLite:dbname=/home/pi/NetBuster/state.db";
$dbh = DBI->connect($dsn, "", "", { RaiseError => 1, AutoCommit => 0 }) or die $DBI::errstr;
foreach $cell (@cells) {
next if ($cell->{essid} eq "" or $cell->{mac_address} eq "");
$sql = "SELECT id FROM WirelessNetworks WHERE mac_address == '$cell->{mac_address}'";
print "Executing: $sql\n";
my $sth = $dbh->prepare("SELECT id FROM WirelessNetworks WHERE mac_address == '$cell->{mac_address}'");
$sth->execute();
my $ids = $sth->fetch();
my $id = undef;
if ($ids) {
$id = $ids->[0];
}
$sth->finish();
if (defined $id) {
push(@current_ids, $id);
my @set;
foreach(keys %$cell) {
next if ($_ eq "mac_address");
push(@set, "$_=\'$cell->{$_}\'");
}
my $set = join ", ", @set;
$sql = "UPDATE WirelessNetworks SET $set WHERE id=$id";
print "Executing: $sql\n";
my $sth = $dbh->prepare($sql);
$sth->execute();
$sth->finish();
} else {
$keys = join ", ", keys %$cell;
$values = join ", ", map { "\'$_\'" } values %$cell;
$sql = "INSERT INTO WirelessNetworks($keys) VALUES ($values)";
print "Executing: $sql\n";
my $sth = $dbh->prepare($sql);
$sth->execute();
$sth->finish();
push(@current_ids, $dbh->sqlite_last_insert_rowid());
}
}
$set = join ", ", @current_ids;
$sql = "DELETE FROM WirelessNetworks WHERE id NOT IN ($set)";
print "Executing: $sql\n";
my $sth = $dbh->prepare($sql);
$sth->execute();
$sth->finish();
$dbh->commit();
$dbh->disconnect();
sub save_last_cell_start_new_cell {
push(@cells, $cell) if (defined $cell);
$cell = {};
foreach(qw/essid mac_address encryption_type/) {
$cell->{$_} = "";
}
foreach(qw/protocol mode channel is_encrypted encryption_type quality signal_level is_favorite/) {
$cell->{$_} = 0;
}
return $cell;
}