Network/Wlan.pm

filter: Statementstmt 100% 9 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 100% 12 / 12 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Network/Whois.pm ↑ Summary Network/Wps.pm »
1 #
2 # $Id$
3 #
4 # network::wlan Brik
5 #
6 package Metabrik::Network::Wlan;
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use base qw(Metabrik::Shell::Command Metabrik::System::Package);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub brik_properties {
13 return {
14 revision => '$Revision$',
15 tags => [ qw(unstable wifi wlan wireless monitor) ],
16 author => 'GomoR <GomoR[at]metabrik.org>',
17 license => 'http://opensource.org/licenses/BSD-3-Clause',
18 attributes => {
19 device => [ qw(device) ],
20 monitor => [ qw(device) ],
21 essid => [ qw(essid) ],
22 key => [ qw(key) ],
23 bitrate => [ qw(bitrate_mb|54MB|130MB) ],
24 _monitor_mode_started => [ ],
25 },
26 attributes_default => {
27 device => 'wlan0',
28 monitor => 'mon0',
29 },
30 commands => {
31 install => [ ], # Inherited
32 scan => [ qw(device|OPTIONAL) ],
33 set_bitrate => [ qw(bitrate|OPTIONAL device|OPTIONAL) ],
34 set_wepkey => [ qw(key|OPTIONAL device|OPTIONAL) ],
35 connect => [ qw(device|OPTIONAL essid|OPTIONAL) ],
36 start_monitor_mode => [ qw(device|OPTIONAL) ],
37 stop_monitor_mode => [ qw(monitor|OPTIONAL) ],
38 },
39 require_binaries => {
40 'sudo', => [ ],
41 'iwlist', => [ ],
42 'iwconfig', => [ ],
43 },
44 optional_binaries => {
45 'airmon-ng' => [ ],
46 },
47 need_packages => {
48 ubuntu => [ qw(aircrack-ng iw) ],
49 debian => [ qw(aircrack-ng iw) ],
50 kali => [ qw(aircrack-ng iw) ],
51 },
52 };
53 }
54  
55 sub scan {
56 my $self = shift;
57 my ($device) = @_;
58  
59 $device ||= $self->device;
60 $self->brik_help_run_undef_arg('scan', $device) or return;
61  
62 $self->log->verbose("scan: using device [$device]");
63  
64 my $cmd = "iwlist $device scan";
65  
66 my $result = $self->capture($cmd);
67  
68 if (@$result > 0) {
69 return $self->_list_ap($result);
70 }
71  
72 return $self->log->error("scan: no result");
73 }
74  
75 sub _list_ap {
76 my $self = shift;
77 my ($scan) = @_;
78  
79 my $ap_hash = {};
80  
81 my $cell = '';
82 my $address = '';
83 my $channel = '';
84 my $frequency = '';
85 my $essid = '';
86 my $encryption = '';
87 my $raw = [];
88 my $quality = '';
89 for my $line (@$scan) {
90 push @$raw, $line;
91  
92 if ($line =~ /^\s+Cell\s+(\d+).*Address:\s+(.*)$/) {
93 # We just hit a new Cell, we reset data.
94 if (length($cell)) {
95 $ap_hash->{"cell_$cell"} = {
96 cell => $cell,
97 address => $address,
98 essid => $essid,
99 encryption => $encryption,
100 raw => $raw,
101 quality => $quality,
102 };
103  
104 $cell = '';
105 $address = '';
106 $channel = '';
107 $frequency = '';
108 $essid = '';
109 $encryption = '';
110 $raw = [];
111 $quality = '';
112  
113 # We put back the current line.
114 push @$raw, $line;
115 }
116 $cell = $1;
117 $address = $2;
118 next;
119 }
120  
121 if ($line =~ /^\s+Channel:(\d+)/) {
122 $channel = $1;
123 next;
124 }
125  
126 if ($line =~ /^\s+Frequency:(\d+\.\d+)/) {
127 $frequency = $1;
128 next;
129 }
130  
131 if ($line =~ /^\s+Quality=(\d+)\/(\d+)/) {
132 $quality = sprintf("%.2f", 100 * $1 / $2);
133 next;
134 }
135  
136 if ($line =~ /^\s+Encryption key:(\S+)/) {
137 my $this = $1;
138 if ($this eq 'off') {
139 $encryption = 0;
140 }
141 elsif ($this eq 'on') {
142 $encryption = 1;
143 }
144 else {
145 $encryption = -1;
146 }
147 }
148  
149 if ($line =~ /^\s+ESSID:"(\S+)"/) {
150 $essid = $1;
151 $self->log->verbose("cell [$cell] address [$address] essid[$essid] encryption[$encryption] quality[$quality] channel[$channel] frequency[$frequency]");
152 next;
153 }
154  
155 }
156  
157 $ap_hash->{"cell_$cell"} = {
158 cell => $cell,
159 address => $address,
160 channel => $channel,
161 frequency => $frequency,
162 essid => $essid,
163 encryption => $encryption,
164 raw => $raw,
165 quality => $quality,
166 };
167  
168 return $ap_hash;
169 }
170  
171 sub connect {
172 my $self = shift;
173 my ($device, $essid) = @_;
174  
175 $device ||= $self->device;
176 $essid ||= $self->essid;
177 $self->brik_help_run_undef_arg('connect', $device) or return;
178 $self->brik_help_run_undef_arg('connect', $essid) or return;
179  
180 my $cmd = "sudo iwconfig $device essid $essid";
181  
182 $self->capture_stderr(1);
183 my $r = $self->capture($cmd) or return;
184  
185 $self->log->verbose("connect: $r");
186  
187 $self->set_bitrate or return;
188  
189 # For WEP, we can use:
190 # "iwconfig $device key $key"
191  
192 return $r;
193 }
194  
195 sub set_bitrate {
196 my $self = shift;
197 my ($bitrate, $device) = @_;
198  
199 $bitrate ||= $self->bitrate;
200 $device ||= $self->device;
201 $self->brik_help_run_undef_arg('set_bitrate', $bitrate) or return;
202 $self->brik_help_run_undef_arg('set_bitrate', $device) or return;
203  
204 my $cmd = "sudo iwconfig $device rate $bitrate";
205  
206 $self->capture_stderr(1);
207  
208 return $self->capture($cmd);
209 }
210  
211 sub set_wepkey {
212 my $self = shift;
213 my ($key, $device) = @_;
214  
215 $key ||= $self->key;
216 $device ||= $self->device;
217 $self->brik_help_run_undef_arg('set_wepkey', $key) or return;
218 $self->brik_help_run_undef_arg('set_wepkey', $device) or return;
219  
220 my $cmd = "sudo iwconfig $device key $key";
221  
222 $self->capture_stderr(1);
223  
224 return $self->capture($cmd);
225 }
226  
227 sub start_monitor_mode {
228 my $self = shift;
229 my ($device) = @_;
230  
231 $device ||= $self->device;
232 $self->brik_help_run_undef_arg('start_monitor_mode', $device) or return;
233  
234 # airmon-ng is optional, so we check here.
235 my $found = $self->brik_has_binary('airmon-ng');
236 if (! $found) {
237 return $self->log->error("start_monitor_mode: you have to install aircrack-ng package");
238 }
239  
240 my $cmd = "sudo airmon-ng start $device";
241  
242 $self->capture_stderr(1);
243  
244 my $r = $self->capture($cmd);
245  
246 if (defined($r)) {
247 my $monitor = '';
248 for my $line (@$r) {
249 if ($line =~ /monitor mode enabled on (\S+)\)/) {
250 $monitor = $1;
251 last;
252 }
253 }
254  
255 if (! length($monitor)) {
256 return $self->log->error("start_monitor_mode: cannot start monitor mode");
257 }
258  
259 if ($monitor !~ /^[a-z]+(?:\d+)?$/) {
260 return $self->log->error("start_monitor_mode: cannot start monitor mode with monitor [$monitor]");
261 }
262  
263 $self->monitor($monitor);
264 $self->_monitor_mode_started(1);
265 }
266  
267 return $self->monitor;
268 }
269  
270 sub stop_monitor_mode {
271 my $self = shift;
272 my ($monitor) = @_;
273  
274 $monitor ||= $self->monitor;
275 my $started = $self->_monitor_mode_started;
276 $self->brik_help_run_undef_arg('start_monitor_mode', $started) or return;
277 $self->brik_help_run_undef_arg('start_monitor_mode', $monitor) or return;
278  
279 # airmon-ng is optional, so we check here.
280 my $found = $self->brik_has_binary('airmon-ng');
281 if (! $found) {
282 return $self->log->error("stop_monitor_mode: you have to install aircrack-ng package");
283 }
284  
285 my $cmd = "sudo airmon-ng stop $monitor";
286  
287 $self->capture_stderr(1);
288  
289 my $r = $self->capture($cmd);
290  
291 if (defined($r)) {
292 $self->_monitor_mode_started(0);
293 }
294  
295 return $r;
296 }
297  
298 1;
299  
300 __END__
« Network/Whois.pm ↑ Summary Network/Wps.pm »